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 : 123593016 : require_complete_type (tree value,
75 : : tsubst_flags_t complain /* = tf_warning_or_error */)
76 : : {
77 : 123593016 : tree type;
78 : :
79 : 123593016 : if (processing_template_decl || value == error_mark_node)
80 : : return value;
81 : :
82 : 115272094 : if (TREE_CODE (value) == OVERLOAD)
83 : 0 : type = unknown_type_node;
84 : : else
85 : 115272094 : type = TREE_TYPE (value);
86 : :
87 : 115272094 : if (type == error_mark_node)
88 : : return error_mark_node;
89 : :
90 : : /* First, detect a valid value with a complete type. */
91 : 115272082 : if (COMPLETE_TYPE_P (type))
92 : : return value;
93 : :
94 : 117288 : if (complete_type_or_maybe_complain (type, value, complain))
95 : : return value;
96 : : else
97 : 90 : 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 : 12843912814 : complete_type (tree type)
107 : : {
108 : 12843912814 : 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 : 12843912814 : if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 : : ;
115 : 4246419327 : else if (TREE_CODE (type) == ARRAY_TYPE)
116 : : {
117 : 647632 : tree t = complete_type (TREE_TYPE (type));
118 : 647632 : unsigned int needs_constructing, has_nontrivial_dtor;
119 : 647632 : if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 : 642297 : layout_type (type);
121 : 647632 : needs_constructing
122 : 647632 : = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 : 647632 : has_nontrivial_dtor
124 : 647632 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 : 2145525 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 : : {
127 : 1497893 : TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 : 1497893 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 : : }
130 : : }
131 : 4245771695 : else if (CLASS_TYPE_P (type))
132 : : {
133 : 4163272197 : if (modules_p ())
134 : : /* TYPE could be a class member we've not loaded the definition of. */
135 : 13909175 : lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
136 : :
137 : 4163272197 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 : 812652955 : 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 : 1381886351 : complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 : : {
151 : 1381886351 : type = complete_type (type);
152 : 1381886348 : if (type == error_mark_node)
153 : : /* We already issued an error. */
154 : : return NULL_TREE;
155 : 1381886196 : else if (!COMPLETE_TYPE_P (type))
156 : : {
157 : 3801 : if (complain & tf_error)
158 : 474 : cxx_incomplete_type_diagnostic (value, type, diagnostics::kind::error);
159 : 3801 : note_failed_type_completion (type, complain);
160 : 3801 : return NULL_TREE;
161 : : }
162 : : else
163 : : return type;
164 : : }
165 : :
166 : : tree
167 : 146785848 : complete_type_or_else (tree type, tree value)
168 : : {
169 : 146785848 : 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 : 4230396 : commonparms (tree p1, tree p2)
182 : : {
183 : 4230396 : tree oldargs = p1, newargs, n;
184 : 4230396 : int i, len;
185 : 4230396 : int any_change = 0;
186 : :
187 : 4230396 : len = list_length (p1);
188 : 4230396 : newargs = tree_last (p1);
189 : :
190 : 4230396 : if (newargs == void_list_node)
191 : : i = 1;
192 : : else
193 : : {
194 : 39225 : i = 0;
195 : 39225 : newargs = 0;
196 : : }
197 : :
198 : 14087933 : for (; i < len; i++)
199 : 9857537 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200 : :
201 : : n = newargs;
202 : :
203 : 18279104 : for (i = 0; p1;
204 : 14048708 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 : : {
206 : 14048909 : if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 : : {
208 : 168 : TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 : 168 : any_change = 1;
210 : : }
211 : 14048540 : else if (! TREE_PURPOSE (p1))
212 : : {
213 : 14048507 : if (TREE_PURPOSE (p2))
214 : : {
215 : 423570 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 : 423570 : 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 : 14048708 : if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 : : {
227 : 3874470 : any_change = 1;
228 : 3874470 : TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 : : }
230 : : else
231 : 10174238 : TREE_VALUE (n) = TREE_VALUE (p1);
232 : : }
233 : 4230396 : if (! any_change)
234 : 1484699 : 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 : 33231406 : original_type (tree t)
243 : : {
244 : 33231406 : int quals = cp_type_quals (t);
245 : 33231406 : while (t != error_mark_node
246 : 34375098 : && TYPE_NAME (t) != NULL_TREE)
247 : : {
248 : 11584976 : tree x = TYPE_NAME (t);
249 : 11584976 : if (TREE_CODE (x) != TYPE_DECL)
250 : : break;
251 : 11584976 : x = DECL_ORIGINAL_TYPE (x);
252 : 11584976 : if (x == NULL_TREE)
253 : : break;
254 : : t = x;
255 : : }
256 : 33231406 : 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 : 59333 : merge_type_attributes_from (tree type, tree other_type)
264 : : {
265 : 59333 : tree attrs = targetm.merge_type_attributes (type, other_type);
266 : 59333 : attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
267 : 59333 : 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 : 11384146 : cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
280 : : {
281 : 11384146 : tree mv1 = TYPE_MAIN_VARIANT (t1);
282 : 11384146 : tree mv2 = TYPE_MAIN_VARIANT (t2);
283 : 11384146 : int extended1 = 0;
284 : 11384146 : int extended2 = 0;
285 : :
286 : 11384146 : if (mv1 == mv2)
287 : : return 0;
288 : :
289 : 91061848 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
290 : : {
291 : 79679117 : if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 : 5058914 : extended1 = i + 1;
293 : 79679117 : if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 : 4322816 : extended2 = i + 1;
295 : : }
296 : 11382731 : if (mv1 == bfloat16_type_node)
297 : 1165986 : extended1 = true;
298 : 11382731 : if (mv2 == bfloat16_type_node)
299 : 945276 : extended2 = true;
300 : 11382731 : if (extended2 && !extended1)
301 : : {
302 : 5060056 : int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 : 5060056 : return ret == 3 ? 3 : -ret;
304 : : }
305 : :
306 : 6322675 : const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 : 6322675 : const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 : 6322675 : 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 : 50581400 : int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
315 : 12645350 : ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
316 : 50581400 : int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
317 : 12645350 : ? 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 : 6322675 : if ((p1 > p2 && fmt1->emax >= fmt2->emax)
322 : 4865028 : || (p1 == p2 && fmt1->emax > fmt2->emax))
323 : : return 2;
324 : 4865028 : if ((p1 < p2 && fmt1->emax <= fmt2->emax)
325 : 1610122 : || (p1 == p2 && fmt1->emax < fmt2->emax))
326 : : return -2;
327 : 1610122 : if ((p1 > p2 && fmt1->emax < fmt2->emax)
328 : 1601295 : || (p1 < p2 && fmt1->emax > fmt2->emax))
329 : : return 3;
330 : 1592468 : 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 : 1592468 : 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 : 6369856 : for (p = &float_type_node; p <= &long_double_type_node; ++p)
375 : : {
376 : 4777392 : const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
377 : 4777392 : gcc_assert (fmt3->b == 2);
378 : 38219136 : int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
379 : 9554784 : ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
380 : 4777392 : if (p1 == p3 && fmt1->emax == fmt3->emax)
381 : 1385836 : ++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 : 1592464 : 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 : 98803856 : cp_common_type (tree t1, tree t2)
413 : : {
414 : 98803856 : enum tree_code code1 = TREE_CODE (t1);
415 : 98803856 : enum tree_code code2 = TREE_CODE (t2);
416 : 98803856 : tree attributes;
417 : 98803856 : 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 : 98803856 : attributes = (*targetm.merge_type_attributes) (t1, t2);
424 : :
425 : 98803856 : if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
426 : : {
427 : 1538834 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
428 : 1538834 : return build_type_attribute_variant (t1, attributes);
429 : : else
430 : : return NULL_TREE;
431 : : }
432 : :
433 : : /* FIXME: Attributes. */
434 : 97265022 : gcc_assert (ARITHMETIC_TYPE_P (t1)
435 : : || VECTOR_TYPE_P (t1)
436 : : || UNSCOPED_ENUM_P (t1));
437 : 97265022 : 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 : 97265022 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
445 : : {
446 : 230443 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
447 : 230443 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
448 : 230443 : tree subtype
449 : 230443 : = type_after_usual_arithmetic_conversions (subtype1, subtype2);
450 : :
451 : 230443 : if (subtype == error_mark_node)
452 : : return subtype;
453 : 230443 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
454 : 197029 : 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 : 97034579 : 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 : 59333 : if (TYPE_UNSIGNED (t1))
467 : 12801 : return merge_type_attributes_from (t1, t2);
468 : : else
469 : 46532 : return merge_type_attributes_from (t2, t1);
470 : : }
471 : :
472 : : /* If only one is real, use it as the result. */
473 : 96975246 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
474 : 1127793 : return build_type_attribute_variant (t1, attributes);
475 : 95847453 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
476 : 1051433 : return build_type_attribute_variant (t2, attributes);
477 : :
478 : 94796020 : if (code1 == REAL_TYPE
479 : 94796020 : && (extended_float_type_p (t1) || extended_float_type_p (t2)))
480 : : {
481 : 206958 : tree mv1 = TYPE_MAIN_VARIANT (t1);
482 : 206958 : tree mv2 = TYPE_MAIN_VARIANT (t2);
483 : 206958 : if (mv1 == mv2)
484 : 203118 : return build_type_attribute_variant (t1, attributes);
485 : :
486 : 3840 : int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
487 : 3840 : if (cmpret == 3)
488 : 0 : return error_mark_node;
489 : 3840 : else if (cmpret >= 0)
490 : 3578 : 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 : 94589062 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
497 : 14029884 : return build_type_attribute_variant (t1, attributes);
498 : 80559178 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
499 : 1557445 : return build_type_attribute_variant (t2, attributes);
500 : :
501 : : /* The types are the same; no need to do anything fancy. */
502 : 79001733 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
503 : 72707906 : return build_type_attribute_variant (t1, attributes);
504 : :
505 : 6293827 : if (code1 != REAL_TYPE)
506 : : {
507 : : /* If one is unsigned long long, then convert the other to unsigned
508 : : long long. */
509 : 6293824 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
510 : 6293824 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
511 : 126397 : return build_type_attribute_variant (long_long_unsigned_type_node,
512 : 126397 : 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 : 6167427 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
524 : 6167427 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
525 : : {
526 : 1358 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
527 : 685 : ? long_long_unsigned_type_node
528 : 685 : : long_long_integer_type_node);
529 : 685 : return build_type_attribute_variant (t, attributes);
530 : : }
531 : :
532 : : /* Go through the same procedure, but for longs. */
533 : 6166742 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
534 : 6166742 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
535 : 779607 : return build_type_attribute_variant (long_unsigned_type_node,
536 : 779607 : attributes);
537 : 5387135 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
538 : 5387135 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
539 : : {
540 : 74766 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
541 : 74766 : ? long_unsigned_type_node : long_integer_type_node);
542 : 37820 : 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 : 10698598 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
550 : : {
551 : 5349315 : if (int_n_enabled_p [i]
552 : 5349315 : && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
553 : 5265141 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
554 : 5265137 : || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
555 : 5265137 : || 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 : 5349283 : if (TYPE_UNSIGNED (t1))
566 : 4284398 : return build_type_attribute_variant (t1, attributes);
567 : : else
568 : 1064885 : 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 : 934693 : type_after_usual_arithmetic_conversions (tree t1, tree t2)
600 : : {
601 : 934693 : gcc_assert (ARITHMETIC_TYPE_P (t1)
602 : : || VECTOR_TYPE_P (t1)
603 : : || UNSCOPED_ENUM_P (t1));
604 : 934693 : 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 : 934693 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
610 : 703824 : && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
611 : : {
612 : 703528 : t1 = type_promotes_to (t1);
613 : 703528 : t2 = type_promotes_to (t2);
614 : : }
615 : :
616 : 934693 : 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 : 4458359 : 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 : 4458359 : tree pointee1;
660 : 4458359 : tree pointee2;
661 : 4458359 : tree result_type;
662 : 4458359 : tree attributes;
663 : :
664 : : /* Determine the types pointed to by T1 and T2. */
665 : 4458359 : if (TYPE_PTR_P (t1))
666 : : {
667 : 4457863 : pointee1 = TREE_TYPE (t1);
668 : 4457863 : 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 : 4458359 : 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 : 4458268 : const int q1 = cp_type_quals (pointee1);
700 : 4458268 : const int q2 = cp_type_quals (pointee2);
701 : 4458268 : const int quals = q1 | q2;
702 : 4458268 : result_type = cp_build_qualified_type (result_type,
703 : 4458268 : (quals | (*add_const
704 : 4458268 : ? 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 : 4458268 : if (quals != q1 || quals != q2)
710 : 583097 : *add_const = true;
711 : : /* If the original types were pointers to members, so is the
712 : : result. */
713 : 4458268 : 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 : 4457782 : result_type = build_pointer_type (result_type);
729 : :
730 : : /* Merge the attributes. */
731 : 4458268 : attributes = (*targetm.merge_type_attributes) (t1, t2);
732 : 4458268 : 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 : 4669235 : 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 : 4669235 : tree class1;
750 : 4669235 : 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 : 4669235 : if (null_ptr_cst_p (arg1))
757 : : return t2;
758 : 4669074 : 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 : 4646754 : 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 : 4646754 : if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
776 : : {
777 : 188293 : tree attributes;
778 : 188293 : tree result_type;
779 : :
780 : 188293 : 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 : 188292 : result_type
810 : 188292 : = cp_build_qualified_type (void_type_node,
811 : 188292 : (cp_type_quals (TREE_TYPE (t1))
812 : 188292 : | cp_type_quals (TREE_TYPE (t2))));
813 : 188292 : result_type = build_pointer_type (result_type);
814 : : /* Merge the attributes. */
815 : 188292 : attributes = (*targetm.merge_type_attributes) (t1, t2);
816 : 188292 : return build_type_attribute_variant (result_type, attributes);
817 : : }
818 : :
819 : 4458461 : 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 : 4458461 : if (fnptr_conv_p (t1, t2))
830 : : return t1;
831 : 4458411 : 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 : 4457848 : if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
837 : 4457848 : && CLASS_TYPE_P (TREE_TYPE (t1))
838 : 776470 : && CLASS_TYPE_P (TREE_TYPE (t2))
839 : 5234832 : && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
840 : 776467 : TREE_TYPE (t2)))
841 : : {
842 : 31589 : class1 = TREE_TYPE (t1);
843 : 31589 : class2 = TREE_TYPE (t2);
844 : :
845 : 31589 : if (DERIVED_FROM_P (class1, class2))
846 : 3236 : t2 = (build_pointer_type
847 : 1618 : (cp_build_qualified_type (class1, cp_type_quals (class2))));
848 : 29971 : else if (DERIVED_FROM_P (class2, class1))
849 : 59906 : t1 = (build_pointer_type
850 : 29953 : (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 : 4426379 : else if (TYPE_PTRMEM_P (t1)
862 : 4426896 : && !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 : 4458326 : bool add_const = false;
900 : 4458326 : return composite_pointer_type_r (location, t1, t2, &add_const, operation,
901 : 4458326 : 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 : 30116306 : merge_types (tree t1, tree t2)
913 : : {
914 : 30116306 : enum tree_code code1;
915 : 30116306 : enum tree_code code2;
916 : 30116306 : tree attributes;
917 : :
918 : : /* Save time if the two types are the same. */
919 : 30116306 : if (t1 == t2)
920 : : return t1;
921 : 16615703 : if (original_type (t1) == original_type (t2))
922 : : return t1;
923 : :
924 : : /* If one type is nonsense, use the other. */
925 : 16536229 : if (t1 == error_mark_node)
926 : : return t2;
927 : 16536229 : if (t2 == error_mark_node)
928 : : return t1;
929 : :
930 : : /* Handle merging an auto redeclaration with a previous deduced
931 : : return type. */
932 : 16536229 : if (is_auto (t1))
933 : : return t2;
934 : :
935 : : /* Merge the attributes. */
936 : 16526376 : attributes = (*targetm.merge_type_attributes) (t1, t2);
937 : :
938 : 16526376 : if (TYPE_PTRMEMFUNC_P (t1))
939 : 15 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
940 : 16526376 : if (TYPE_PTRMEMFUNC_P (t2))
941 : 15 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
942 : :
943 : 16526376 : code1 = TREE_CODE (t1);
944 : 16526376 : code2 = TREE_CODE (t2);
945 : 16526376 : 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 : 16526376 : switch (code1)
961 : : {
962 : 2784932 : case POINTER_TYPE:
963 : 2784932 : case REFERENCE_TYPE:
964 : : /* For two pointers, do this recursively on the target type. */
965 : 2784932 : {
966 : 2784932 : tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
967 : 2784932 : int quals = cp_type_quals (t1);
968 : :
969 : 2784932 : if (code1 == POINTER_TYPE)
970 : : {
971 : 740901 : t1 = build_pointer_type (target);
972 : 740901 : if (TREE_CODE (target) == METHOD_TYPE)
973 : 15 : t1 = build_ptrmemfunc_type (t1);
974 : : }
975 : : else
976 : 2044031 : t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
977 : 2784932 : t1 = build_type_attribute_variant (t1, attributes);
978 : 2784932 : t1 = cp_build_qualified_type (t1, quals);
979 : :
980 : 2784932 : 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 : 10919 : case ARRAY_TYPE:
997 : 10919 : {
998 : 10919 : 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 : 21838 : if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
1001 : 10894 : return build_type_attribute_variant (t1, attributes);
1002 : 44 : if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
1003 : 19 : 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 : 4377884 : 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 : 4377884 : {
1014 : 4377884 : tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1015 : 4377884 : tree p1 = TYPE_ARG_TYPES (t1);
1016 : 4377884 : tree p2 = TYPE_ARG_TYPES (t2);
1017 : 4377884 : tree parms;
1018 : :
1019 : : /* Save space: see if the result is identical to one of the args. */
1020 : 4377884 : if (valtype == TREE_TYPE (t1) && ! p2)
1021 : 0 : return cp_build_type_attribute_variant (t1, attributes);
1022 : 4377884 : 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 : 8755768 : if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1027 : : parms = p2;
1028 : 8460792 : else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1029 : : parms = p1;
1030 : : else
1031 : 4230396 : parms = commonparms (p1, p2);
1032 : :
1033 : 4377884 : cp_cv_quals quals = type_memfn_quals (t1);
1034 : 4377884 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1035 : 4377884 : gcc_assert (quals == type_memfn_quals (t2));
1036 : 4377884 : gcc_assert (rqual == type_memfn_rqual (t2));
1037 : :
1038 : 4377884 : tree rval = cp_build_function_type (valtype, parms);
1039 : 4377884 : rval = apply_memfn_quals (rval, quals);
1040 : 4377884 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1041 : 4377884 : TYPE_RAISES_EXCEPTIONS (t2));
1042 : 4377884 : bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1043 : 4377884 : t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1044 : 4377884 : break;
1045 : : }
1046 : :
1047 : 4029937 : case METHOD_TYPE:
1048 : 4029937 : {
1049 : : /* Get this value the long way, since TYPE_METHOD_BASETYPE
1050 : : is just the main variant of this. */
1051 : 4029937 : tree basetype = class_of_this_parm (t2);
1052 : 4029937 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1053 : 4029937 : TYPE_RAISES_EXCEPTIONS (t2));
1054 : 4029937 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1055 : 4029937 : tree t3;
1056 : 4029937 : 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 : 4029937 : t1 = cp_build_function_type (TREE_TYPE (t1),
1062 : 4029937 : TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1063 : 4029937 : t2 = cp_build_function_type (TREE_TYPE (t2),
1064 : 4029937 : TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1065 : 4029937 : t3 = merge_types (t1, t2);
1066 : 4029937 : t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1067 : 4029937 : TYPE_ARG_TYPES (t3));
1068 : 4029937 : t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1069 : 4029937 : 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 : 5312883 : default:;
1079 : 5312883 : 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 : 8407839 : return cp_build_type_attribute_variant (t1, attributes);
1087 : : }
1088 : :
1089 : : /* Return the ARRAY_TYPE type without its domain. */
1090 : :
1091 : : tree
1092 : 486 : strip_array_domain (tree type)
1093 : : {
1094 : 486 : tree t2;
1095 : 486 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1096 : 486 : if (TYPE_DOMAIN (type) == NULL_TREE)
1097 : : return type;
1098 : 215 : t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
1099 : 215 : 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 : 479476 : common_type (tree t1, tree t2)
1112 : : {
1113 : : /* If one type is nonsense, use the other */
1114 : 479476 : if (t1 == error_mark_node)
1115 : : return t2;
1116 : 479476 : if (t2 == error_mark_node)
1117 : : return t1;
1118 : :
1119 : 479476 : 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 : 1348748 : common_pointer_type (tree t1, tree t2)
1131 : : {
1132 : 1348748 : 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 : 1348748 : return composite_pointer_type (input_location, t1, t2,
1137 : : error_mark_node, error_mark_node,
1138 : 1348748 : 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 : 1698 : comp_except_types (tree a, tree b, bool exact)
1159 : : {
1160 : 1698 : if (same_type_p (a, b))
1161 : : return true;
1162 : 506 : else if (!exact)
1163 : : {
1164 : 11 : if (cp_type_quals (a) || cp_type_quals (b))
1165 : 0 : return false;
1166 : :
1167 : 11 : 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 : 10 : if (TREE_CODE (a) != RECORD_TYPE
1176 : 10 : || TREE_CODE (b) != RECORD_TYPE)
1177 : : return false;
1178 : :
1179 : 9 : 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 : 1466626723 : comp_except_specs (const_tree t1, const_tree t2, int exact)
1195 : : {
1196 : 1466626723 : const_tree probe;
1197 : 1466626723 : const_tree base;
1198 : 1466626723 : int length = 0;
1199 : :
1200 : 1466626723 : if (t1 == t2)
1201 : : return true;
1202 : :
1203 : : /* First handle noexcept. */
1204 : 576701828 : if (exact < ce_exact)
1205 : : {
1206 : 7298642 : if (exact == ce_type
1207 : 14535242 : && (canonical_eh_spec (CONST_CAST_TREE (t1))
1208 : 7236600 : == 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 : 7279240 : 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 : 7279087 : if (t2 == noexcept_false_spec)
1218 : 1737 : return t1 == NULL_TREE;
1219 : :
1220 : : /* Otherwise, if we aren't looking for an exact match, noexcept is
1221 : : equivalent to throw(). */
1222 : 7277350 : if (t1 == noexcept_true_spec)
1223 : 700906 : t1 = empty_except_spec;
1224 : 7277350 : if (t2 == noexcept_true_spec)
1225 : 6309352 : 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 : 553025558 : if ((t1 && TREE_PURPOSE (t1))
1232 : 603135483 : || (t2 && TREE_PURPOSE (t2)))
1233 : 567410831 : return (t1 && t2
1234 : 567410831 : && (exact == ce_exact
1235 : 65461322 : ? TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
1236 : 264145 : : cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))));
1237 : :
1238 : 9269705 : if (t1 == NULL_TREE) /* T1 is ... */
1239 : 6532342 : return t2 == NULL_TREE || exact == ce_derived;
1240 : 2737363 : if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1241 : 2704107 : return t2 != NULL_TREE && !TREE_VALUE (t2);
1242 : 34562 : if (t2 == NULL_TREE) /* T2 is ... */
1243 : : return false;
1244 : 1576 : 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 : 2695 : for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1252 : : {
1253 : 2094 : for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1254 : : {
1255 : 1698 : tree a = TREE_VALUE (probe);
1256 : 1698 : tree b = TREE_VALUE (t2);
1257 : :
1258 : 1698 : if (comp_except_types (a, b, exact))
1259 : : {
1260 : 1195 : if (probe == base && exact > ce_derived)
1261 : 1160 : base = TREE_CHAIN (probe);
1262 : 1195 : length++;
1263 : 1195 : break;
1264 : : }
1265 : : }
1266 : 1195 : if (probe == NULL_TREE)
1267 : : return false;
1268 : : }
1269 : 1104 : 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 : 1445854 : comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1280 : : bool strict)
1281 : : {
1282 : 1445854 : tree d1;
1283 : 1445854 : tree d2;
1284 : 1445854 : tree max1, max2;
1285 : :
1286 : 1445854 : if (t1 == t2)
1287 : : return true;
1288 : :
1289 : : /* The type of the array elements must be the same. */
1290 : 1445754 : if (strict
1291 : 1445754 : ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1292 : 3758 : : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1293 : : return false;
1294 : :
1295 : 968449 : d1 = TYPE_DOMAIN (t1);
1296 : 968449 : d2 = TYPE_DOMAIN (t2);
1297 : :
1298 : 968449 : 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 : 643569 : if (!d1 && d2)
1314 : 2284 : return cb >= bounds_either;
1315 : 641285 : else if (d1 && !d2)
1316 : 23613 : return cb == bounds_either;
1317 : :
1318 : : /* Check that the dimensions are the same. */
1319 : :
1320 : 617672 : if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1321 : : return false;
1322 : 617651 : max1 = TYPE_MAX_VALUE (d1);
1323 : 617651 : max2 = TYPE_MAX_VALUE (d2);
1324 : :
1325 : 617651 : 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 : 1474243698 : comp_template_parms_position (tree t1, tree t2)
1338 : : {
1339 : 1474243698 : tree index1, index2;
1340 : 1474243698 : 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 : 1474243698 : index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1347 : 1474243698 : index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1348 : :
1349 : : /* Then compare their relative position. */
1350 : 1474243698 : if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1351 : 984569592 : || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1352 : 2195656519 : || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1353 : 721412821 : != 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 : 653422718 : if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1359 : 166724203 : 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 : 512 : cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1368 : : {
1369 : 512 : t1 = TYPE_MAIN_VARIANT (t1);
1370 : 512 : t2 = TYPE_MAIN_VARIANT (t2);
1371 : :
1372 : 512 : 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 : 377 : if (INTEGRAL_TYPE_P (t1)
1380 : 68 : && INTEGRAL_TYPE_P (t2)
1381 : 424 : && 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 : 7356 : cxx_safe_function_type_cast_p (tree t1, tree t2)
1391 : : {
1392 : 7356 : if (TREE_TYPE (t1) == void_type_node &&
1393 : 7283 : TYPE_ARG_TYPES (t1) == void_list_node)
1394 : : return true;
1395 : :
1396 : 5875 : if (TREE_TYPE (t2) == void_type_node &&
1397 : 5669 : TYPE_ARG_TYPES (t2) == void_list_node)
1398 : : return true;
1399 : :
1400 : 273 : if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1401 : : return false;
1402 : :
1403 : 112 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1404 : 315 : 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 : 13181967850 : structural_comptypes (tree t1, tree t2, int strict)
1416 : : {
1417 : : /* Both should be types that are not obviously the same. */
1418 : 13181967850 : 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 : 13181967850 : if (!comparing_specializations)
1423 : : {
1424 : : /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1425 : : current instantiation. */
1426 : 10793955750 : if (TREE_CODE (t1) == TYPENAME_TYPE)
1427 : 94492931 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1428 : :
1429 : 10793955750 : if (TREE_CODE (t2) == TYPENAME_TYPE)
1430 : 117046216 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1431 : : }
1432 : :
1433 : 13181967850 : if (TYPE_PTRMEMFUNC_P (t1))
1434 : 27516524 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1435 : 13181967850 : if (TYPE_PTRMEMFUNC_P (t2))
1436 : 30503493 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1437 : :
1438 : : /* Different classes of types can't be compatible. */
1439 : 13181967850 : 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 : 8309490844 : if (TREE_CODE (t1) != ARRAY_TYPE
1445 : 8309490844 : && cp_type_quals (t1) != cp_type_quals (t2))
1446 : : return false;
1447 : 7791479525 : if (TREE_CODE (t1) == FUNCTION_TYPE
1448 : 7791479525 : && 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 : 7791369389 : if (FUNC_OR_METHOD_TYPE_P (t1))
1453 : : {
1454 : 34301607 : if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1455 : : return false;
1456 : 18415814 : if (flag_noexcept_type
1457 : 36759217 : && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1458 : 18343403 : 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 : 7768443453 : if (TREE_CODE (t1) != ARRAY_TYPE
1467 : 7768443453 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1468 : 352297269 : 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 : 7416146184 : 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 : 545514053 : case OPAQUE_TYPE:
1481 : 545514053 : case INTEGER_TYPE:
1482 : 545514053 : case FIXED_POINT_TYPE:
1483 : 545514053 : 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 : 545514053 : if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1499 : : return false;
1500 : :
1501 : : /* We don't need or want the attribute comparison. */
1502 : 9119 : goto check_alias;
1503 : :
1504 : 1492083 : case TEMPLATE_TEMPLATE_PARM:
1505 : 1492083 : case BOUND_TEMPLATE_TEMPLATE_PARM:
1506 : 1492083 : if (!comp_template_parms_position (t1, t2))
1507 : : return false;
1508 : 1009825 : if (!comp_template_parms
1509 : 2019650 : (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1510 : 2825897 : DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1511 : : return false;
1512 : 685037 : if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1513 : : break;
1514 : : /* Don't check inheritance. */
1515 : : strict = COMPARE_STRICT;
1516 : : /* Fall through. */
1517 : :
1518 : 3744694301 : case RECORD_TYPE:
1519 : 3744694301 : case UNION_TYPE:
1520 : 6591301887 : if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1521 : 2316713472 : && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1522 : 1788201093 : || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1523 : 4273250761 : && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1524 : : break;
1525 : :
1526 : 3702514352 : if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1527 : : break;
1528 : 3702514319 : else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1529 : : break;
1530 : :
1531 : : return false;
1532 : :
1533 : 45268 : case OFFSET_TYPE:
1534 : 45268 : if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1535 : : strict & ~COMPARE_REDECLARATION))
1536 : : return false;
1537 : 42712 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1538 : : return false;
1539 : : break;
1540 : :
1541 : 867152125 : case REFERENCE_TYPE:
1542 : 867152125 : if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1543 : : return false;
1544 : : /* fall through to checks for pointer types */
1545 : 1202917916 : gcc_fallthrough ();
1546 : :
1547 : 1202917916 : case POINTER_TYPE:
1548 : 1202917916 : if (TYPE_MODE (t1) != TYPE_MODE (t2)
1549 : 1202917916 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1550 : 1086736984 : return false;
1551 : : break;
1552 : :
1553 : 3571190 : case FUNCTION_TYPE:
1554 : 3571190 : if (TYPE_NO_NAMED_ARGS_STDARG_P (t1) != TYPE_NO_NAMED_ARGS_STDARG_P (t2))
1555 : : return false;
1556 : : /* FALLTHRU */
1557 : 11053308 : case METHOD_TYPE:
1558 : : /* Exception specs and memfn_rquals were checked above. */
1559 : 11053308 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1560 : : return false;
1561 : 10422699 : if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1562 : : return false;
1563 : : break;
1564 : :
1565 : 1441996 : case ARRAY_TYPE:
1566 : : /* Target types must match incl. qualifiers. */
1567 : 1441996 : if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1568 : 1441996 : ? bounds_either : bounds_none),
1569 : : /*strict=*/true))
1570 : : return false;
1571 : : break;
1572 : :
1573 : 1472751615 : case TEMPLATE_TYPE_PARM:
1574 : : /* If T1 and T2 don't have the same relative position in their
1575 : : template parameters set, they can't be equal. */
1576 : 1472751615 : if (!comp_template_parms_position (t1, t2))
1577 : : return false;
1578 : : /* If T1 and T2 don't represent the same class template deduction,
1579 : : they aren't equal. */
1580 : 1079969276 : if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1581 : 539984638 : CLASS_PLACEHOLDER_TEMPLATE (t2)))
1582 : : return false;
1583 : : /* Constrained 'auto's are distinct from parms that don't have the same
1584 : : constraints. */
1585 : 510928785 : if (!equivalent_placeholder_constraints (t1, t2))
1586 : : return false;
1587 : : break;
1588 : :
1589 : 149172150 : case TYPENAME_TYPE:
1590 : 298344300 : if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1591 : 149172150 : TYPENAME_TYPE_FULLNAME (t2)))
1592 : : return false;
1593 : : /* Qualifiers don't matter on scopes. */
1594 : 130438741 : if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1595 : 130438741 : TYPE_CONTEXT (t2)))
1596 : : return false;
1597 : : break;
1598 : :
1599 : 11403 : case UNBOUND_CLASS_TEMPLATE:
1600 : 11403 : if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1601 : : return false;
1602 : 11403 : if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1603 : : return false;
1604 : : break;
1605 : :
1606 : 3370509 : case COMPLEX_TYPE:
1607 : 3370509 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1608 : : return false;
1609 : : break;
1610 : :
1611 : 12633123 : case VECTOR_TYPE:
1612 : 12633123 : if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1613 : 23603404 : || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1614 : 23656120 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1615 : 1662842 : return false;
1616 : : break;
1617 : :
1618 : 6489181 : case TYPE_PACK_EXPANSION:
1619 : 6489181 : return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1620 : : PACK_EXPANSION_PATTERN (t2))
1621 : 12801491 : && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1622 : 6312310 : PACK_EXPANSION_EXTRA_ARGS (t2)));
1623 : :
1624 : 14 : case PACK_INDEX_TYPE:
1625 : 14 : return (cp_tree_equal (PACK_INDEX_PACK (t1),
1626 : 14 : PACK_INDEX_PACK (t2))
1627 : 16 : && cp_tree_equal (PACK_INDEX_INDEX (t1),
1628 : 2 : PACK_INDEX_INDEX (t2)));
1629 : :
1630 : 15806137 : case DECLTYPE_TYPE:
1631 : 31612274 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1632 : 15806137 : != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1633 : : return false;
1634 : 15765365 : if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1635 : : return false;
1636 : 15765365 : if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1637 : : return false;
1638 : 15765365 : if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1639 : : return false;
1640 : : break;
1641 : :
1642 : 719456 : case TRAIT_TYPE:
1643 : 719456 : if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1644 : : return false;
1645 : 1424 : if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1646 : 2848 : || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1647 : 0 : return false;
1648 : : break;
1649 : :
1650 : 3 : case TYPEOF_TYPE:
1651 : 3 : if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1652 : : return false;
1653 : : break;
1654 : :
1655 : : default:
1656 : : return false;
1657 : : }
1658 : :
1659 : : /* If we get here, we know that from a target independent POV the
1660 : : types are the same. Make sure the target attributes are also
1661 : : the same. */
1662 : 692966732 : if (!comp_type_attributes (t1, t2))
1663 : : return false;
1664 : :
1665 : 692965545 : check_alias:
1666 : 1045271933 : if (comparing_dependent_aliases
1667 : 1045271933 : && (typedef_variant_p (t1) || typedef_variant_p (t2)))
1668 : : {
1669 : 2639255 : tree dep1 = dependent_opaque_alias_p (t1) ? t1 : NULL_TREE;
1670 : 2639255 : tree dep2 = dependent_opaque_alias_p (t2) ? t2 : NULL_TREE;
1671 : 2639255 : if ((dep1 || dep2)
1672 : 2639255 : && (!(dep1 && dep2)
1673 : 0 : || !comp_type_attributes (DECL_ATTRIBUTES (TYPE_NAME (dep1)),
1674 : 0 : DECL_ATTRIBUTES (TYPE_NAME (dep2)))))
1675 : 17 : return false;
1676 : :
1677 : : /* Don't treat an alias template specialization with dependent
1678 : : arguments as equivalent to its underlying type when used as a
1679 : : template argument; we need them to be distinct so that we
1680 : : substitute into the specialization arguments at instantiation
1681 : : time. And aliases can't be equivalent without being ==, so
1682 : : we don't need to look any deeper. */
1683 : 2639238 : ++processing_template_decl;
1684 : 2639238 : dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1685 : 2639238 : dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1686 : 2639238 : --processing_template_decl;
1687 : 2639238 : if ((dep1 || dep2) && dep1 != dep2)
1688 : : return false;
1689 : : }
1690 : :
1691 : : return true;
1692 : : }
1693 : :
1694 : : /* C++ implementation of compatible_types_for_indirection_note_p. */
1695 : :
1696 : : bool
1697 : 1678 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1698 : : {
1699 : 1678 : return same_type_p (type1, type2);
1700 : : }
1701 : :
1702 : : /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1703 : : is a bitwise-or of the COMPARE_* flags. */
1704 : :
1705 : : bool
1706 : 20255222250 : comptypes (tree t1, tree t2, int strict)
1707 : : {
1708 : 20255222250 : gcc_checking_assert (t1 && t2);
1709 : :
1710 : : /* TYPE_ARGUMENT_PACKS are not really types. */
1711 : 20255222250 : gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1712 : : && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1713 : :
1714 : 20255222250 : if (t1 == t2)
1715 : : return true;
1716 : :
1717 : : /* Suppress errors caused by previously reported errors. */
1718 : 13181969029 : if (t1 == error_mark_node || t2 == error_mark_node)
1719 : : return false;
1720 : :
1721 : 13181968853 : if (strict == COMPARE_STRICT)
1722 : : {
1723 : 11783823040 : if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1724 : : /* At least one of the types requires structural equality, so
1725 : : perform a deep check. */
1726 : 1019806604 : return structural_comptypes (t1, t2, strict);
1727 : :
1728 : 10764016436 : if (flag_checking && param_use_canonical_types)
1729 : : {
1730 : 10764015433 : bool result = structural_comptypes (t1, t2, strict);
1731 : :
1732 : 10764015433 : if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1733 : : /* The two types are structurally equivalent, but their
1734 : : canonical types were different. This is a failure of the
1735 : : canonical type propagation code.*/
1736 : 0 : internal_error
1737 : 0 : ("canonical types differ for identical types %qT and %qT",
1738 : : t1, t2);
1739 : 10764015433 : else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1740 : : /* Two types are structurally different, but the canonical
1741 : : types are the same. This means we were over-eager in
1742 : : assigning canonical types. */
1743 : 0 : internal_error
1744 : 0 : ("same canonical type node for different types %qT and %qT",
1745 : : t1, t2);
1746 : :
1747 : : return result;
1748 : : }
1749 : 1003 : if (!flag_checking && param_use_canonical_types)
1750 : 1003 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1751 : : else
1752 : 0 : return structural_comptypes (t1, t2, strict);
1753 : : }
1754 : 1398145813 : else if (strict == COMPARE_STRUCTURAL)
1755 : 1356321920 : return structural_comptypes (t1, t2, COMPARE_STRICT);
1756 : : else
1757 : 41823893 : return structural_comptypes (t1, t2, strict);
1758 : : }
1759 : :
1760 : : /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1761 : : top-level qualifiers. */
1762 : :
1763 : : bool
1764 : 3047722287 : same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1765 : : {
1766 : 3047722287 : if (type1 == error_mark_node || type2 == error_mark_node)
1767 : : return false;
1768 : 3047721544 : if (type1 == type2)
1769 : : return true;
1770 : :
1771 : 1677349819 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1772 : 1677349819 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1773 : 1677349819 : return same_type_p (type1, type2);
1774 : : }
1775 : :
1776 : : /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1777 : :
1778 : : bool
1779 : 238845566 : similar_type_p (tree type1, tree type2)
1780 : : {
1781 : 238845566 : if (type1 == error_mark_node || type2 == error_mark_node)
1782 : : return false;
1783 : :
1784 : : /* Informally, two types are similar if, ignoring top-level cv-qualification:
1785 : : * they are the same type; or
1786 : : * they are both pointers, and the pointed-to types are similar; or
1787 : : * they are both pointers to member of the same class, and the types of
1788 : : the pointed-to members are similar; or
1789 : : * they are both arrays of the same size or both arrays of unknown bound,
1790 : : and the array element types are similar. */
1791 : :
1792 : 238845566 : if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1793 : : return true;
1794 : :
1795 : 107390761 : if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1796 : 107282271 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1797 : 107282271 : || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1798 : 109859 : return comp_ptr_ttypes_const (type1, type2, bounds_either);
1799 : :
1800 : : return false;
1801 : : }
1802 : :
1803 : : /* Helper function for layout_compatible_type_p and
1804 : : is_corresponding_member_aggr. Advance to next members (NULL if
1805 : : no further ones) and return true if those members are still part of
1806 : : the common initial sequence. */
1807 : :
1808 : : bool
1809 : 2215 : next_common_initial_sequence (tree &memb1, tree &memb2)
1810 : : {
1811 : 2740 : while (memb1)
1812 : : {
1813 : 2779 : if (TREE_CODE (memb1) != FIELD_DECL
1814 : 2318 : || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1815 : : {
1816 : 461 : memb1 = DECL_CHAIN (memb1);
1817 : 461 : continue;
1818 : : }
1819 : 1857 : if (DECL_FIELD_IS_BASE (memb1))
1820 : : {
1821 : 64 : memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1822 : 64 : continue;
1823 : : }
1824 : : break;
1825 : : }
1826 : 2701 : while (memb2)
1827 : : {
1828 : 2737 : if (TREE_CODE (memb2) != FIELD_DECL
1829 : 2279 : || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1830 : : {
1831 : 458 : memb2 = DECL_CHAIN (memb2);
1832 : 458 : continue;
1833 : : }
1834 : 1821 : if (DECL_FIELD_IS_BASE (memb2))
1835 : : {
1836 : 28 : memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1837 : 28 : continue;
1838 : : }
1839 : : break;
1840 : : }
1841 : 2215 : if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1842 : : return true;
1843 : 1793 : if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1844 : : return false;
1845 : 1793 : if (DECL_BIT_FIELD_TYPE (memb1))
1846 : : {
1847 : 165 : if (!DECL_BIT_FIELD_TYPE (memb2))
1848 : : return false;
1849 : 66 : if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1850 : 66 : DECL_BIT_FIELD_TYPE (memb2)))
1851 : : return false;
1852 : 66 : if (TYPE_PRECISION (TREE_TYPE (memb1))
1853 : 66 : != TYPE_PRECISION (TREE_TYPE (memb2)))
1854 : : return false;
1855 : : }
1856 : 1628 : else if (DECL_BIT_FIELD_TYPE (memb2))
1857 : : return false;
1858 : 1601 : else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1859 : : return false;
1860 : 1601 : if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1861 : 1601 : != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1862 : : return false;
1863 : 1574 : if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1864 : : return false;
1865 : 1515 : if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1866 : : return false;
1867 : : return true;
1868 : : }
1869 : :
1870 : : /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1871 : :
1872 : : bool
1873 : 2771 : layout_compatible_type_p (tree type1, tree type2, bool explain/*=false*/)
1874 : : {
1875 : 2771 : if (type1 == error_mark_node || type2 == error_mark_node)
1876 : : return false;
1877 : 2771 : if (type1 == type2)
1878 : : return true;
1879 : :
1880 : 1351 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1881 : 1351 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1882 : 1351 : if (same_type_p (type1, type2))
1883 : : return true;
1884 : :
1885 : 646 : if (TREE_CODE (type1) != TREE_CODE (type2)
1886 : 602 : || (TREE_CODE (type1) != ENUMERAL_TYPE
1887 : 564 : && !CLASS_TYPE_P (type1))
1888 : 1099 : || (TREE_CODE (type2) != ENUMERAL_TYPE
1889 : 415 : && !CLASS_TYPE_P (type2)))
1890 : : {
1891 : 193 : if (explain)
1892 : 6 : inform (input_location, "%q#T and %q#T are not both the same type, "
1893 : : "layout-compatible enumerations, or "
1894 : : "layout-compatible standard-layout class types",
1895 : : type1, type2);
1896 : 193 : return false;
1897 : : }
1898 : :
1899 : 453 : if (!std_layout_type_p (type1))
1900 : : {
1901 : 16 : if (explain)
1902 : 3 : inform (location_of (type1),
1903 : : "%qT is not a standard-layout type", type1);
1904 : 16 : return false;
1905 : : }
1906 : 437 : if (!std_layout_type_p (type2))
1907 : : {
1908 : 0 : if (explain)
1909 : 0 : inform (location_of (type2),
1910 : : "%qT is not a standard-layout type", type2);
1911 : 0 : return false;
1912 : : }
1913 : :
1914 : 437 : if (TREE_CODE (type1) == ENUMERAL_TYPE)
1915 : : {
1916 : 38 : tree underlying1 = finish_underlying_type (type1);
1917 : 38 : tree underlying2 = finish_underlying_type (type2);
1918 : 38 : if (!same_type_p (underlying1, underlying2))
1919 : : {
1920 : 25 : if (explain)
1921 : : {
1922 : 3 : inform (location_of (type1),
1923 : : "the underlying type of %qT is %qT, but",
1924 : : type1, underlying1);
1925 : 3 : inform (location_of (type2),
1926 : : "the underlying type of %qT is %qT",
1927 : : type2, underlying2);
1928 : : }
1929 : 25 : return false;
1930 : : }
1931 : : }
1932 : 399 : else if (TREE_CODE (type1) == RECORD_TYPE)
1933 : : {
1934 : 315 : tree field1 = TYPE_FIELDS (type1);
1935 : 315 : tree field2 = TYPE_FIELDS (type2);
1936 : 1179 : while (1)
1937 : : {
1938 : 747 : if (!next_common_initial_sequence (field1, field2))
1939 : : {
1940 : 27 : if (explain)
1941 : : {
1942 : 6 : if (field1 && field2)
1943 : : {
1944 : 6 : inform (DECL_SOURCE_LOCATION (field1),
1945 : : "%qD and %qD do not correspond",
1946 : : field1, field2);
1947 : 6 : inform (DECL_SOURCE_LOCATION (field2),
1948 : : "%qD declared here", field2);
1949 : : }
1950 : 0 : else if (field1)
1951 : 0 : inform (DECL_SOURCE_LOCATION (field1),
1952 : : "%qT has no member corresponding to %qD",
1953 : : type2, field1);
1954 : 0 : else if (field2)
1955 : 0 : inform (DECL_SOURCE_LOCATION (field2),
1956 : : "%qT has no member corresponding to %qD",
1957 : : type1, field2);
1958 : : }
1959 : 27 : return false;
1960 : : }
1961 : 720 : if (field1 == NULL_TREE)
1962 : : break;
1963 : 432 : field1 = DECL_CHAIN (field1);
1964 : 432 : field2 = DECL_CHAIN (field2);
1965 : : }
1966 : : }
1967 : 84 : else if (TREE_CODE (type1) == UNION_TYPE)
1968 : : {
1969 : : /* The standard says:
1970 : : "Two standard-layout unions are layout-compatible if they have
1971 : : the same number of non-static data members and corresponding
1972 : : non-static data members (in any order) have layout-compatible
1973 : : types."
1974 : : but the code anticipates that bitfield vs. non-bitfield,
1975 : : different bitfield widths or presence/absence of
1976 : : [[no_unique_address]] should be checked as well. */
1977 : 84 : tree field1 = TYPE_FIELDS (type1);
1978 : 84 : tree field2 = TYPE_FIELDS (type2);
1979 : 84 : auto_vec<tree, 16> vec;
1980 : 84 : unsigned int count = 0;
1981 : 378 : for (; field1; field1 = DECL_CHAIN (field1))
1982 : 294 : if (TREE_CODE (field1) == FIELD_DECL)
1983 : 195 : count++;
1984 : 372 : for (; field2; field2 = DECL_CHAIN (field2))
1985 : 288 : if (TREE_CODE (field2) == FIELD_DECL)
1986 : 189 : vec.safe_push (field2);
1987 : :
1988 : : /* Discussions on core lean towards treating multiple union fields
1989 : : of the same type as the same field, so this might need changing
1990 : : in the future. */
1991 : 168 : if (count != vec.length ())
1992 : : {
1993 : 6 : if (explain)
1994 : : {
1995 : 3 : inform_n (location_of (type1), count,
1996 : : "%qT has %u field, but",
1997 : : "%qT has %u fields, but",
1998 : : type1, count);
1999 : 6 : inform_n (location_of (type2), vec.length (),
2000 : : "%qT has %u field",
2001 : : "%qT has %u fields",
2002 : : type2, vec.length ());
2003 : : }
2004 : 6 : return false;
2005 : : }
2006 : :
2007 : 324 : for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
2008 : : {
2009 : 258 : if (TREE_CODE (field1) != FIELD_DECL)
2010 : 81 : continue;
2011 : 177 : unsigned int j;
2012 : 177 : tree t1 = DECL_BIT_FIELD_TYPE (field1);
2013 : 177 : if (t1 == NULL_TREE)
2014 : 174 : t1 = TREE_TYPE (field1);
2015 : 297 : FOR_EACH_VEC_ELT (vec, j, field2)
2016 : : {
2017 : 285 : tree t2 = DECL_BIT_FIELD_TYPE (field2);
2018 : 285 : if (t2 == NULL_TREE)
2019 : 273 : t2 = TREE_TYPE (field2);
2020 : 285 : if (DECL_BIT_FIELD_TYPE (field1))
2021 : : {
2022 : 6 : if (!DECL_BIT_FIELD_TYPE (field2))
2023 : 0 : continue;
2024 : 6 : if (TYPE_PRECISION (TREE_TYPE (field1))
2025 : 6 : != TYPE_PRECISION (TREE_TYPE (field2)))
2026 : 6 : continue;
2027 : : }
2028 : 279 : else if (DECL_BIT_FIELD_TYPE (field2))
2029 : 6 : continue;
2030 : 273 : if (!layout_compatible_type_p (t1, t2))
2031 : 108 : continue;
2032 : 165 : if ((!lookup_attribute ("no_unique_address",
2033 : 165 : DECL_ATTRIBUTES (field1)))
2034 : 165 : != !lookup_attribute ("no_unique_address",
2035 : 165 : DECL_ATTRIBUTES (field2)))
2036 : 0 : continue;
2037 : : break;
2038 : : }
2039 : 354 : if (j == vec.length ())
2040 : : {
2041 : 12 : if (explain)
2042 : : {
2043 : 3 : inform (DECL_SOURCE_LOCATION (field1),
2044 : : "%qT has no member corresponding to %qD",
2045 : : type2, field1);
2046 : 3 : inform (location_of (type2), "%qT declared here", type2);
2047 : : }
2048 : 12 : return false;
2049 : : }
2050 : 165 : vec.unordered_remove (j);
2051 : : }
2052 : 84 : }
2053 : : else
2054 : 0 : gcc_unreachable ();
2055 : :
2056 : : return true;
2057 : : }
2058 : :
2059 : : /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
2060 : :
2061 : : bool
2062 : 174556619 : at_least_as_qualified_p (const_tree type1, const_tree type2)
2063 : : {
2064 : 174556619 : int q1 = cp_type_quals (type1);
2065 : 174556619 : int q2 = cp_type_quals (type2);
2066 : :
2067 : : /* All qualifiers for TYPE2 must also appear in TYPE1. */
2068 : 174556619 : return (q1 & q2) == q2;
2069 : : }
2070 : :
2071 : : /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
2072 : : more cv-qualified that TYPE1, and 0 otherwise. */
2073 : :
2074 : : int
2075 : 11531872 : comp_cv_qualification (int q1, int q2)
2076 : : {
2077 : 11531872 : if (q1 == q2)
2078 : : return 0;
2079 : :
2080 : 3119328 : if ((q1 & q2) == q2)
2081 : : return 1;
2082 : 257981 : else if ((q1 & q2) == q1)
2083 : 257396 : return -1;
2084 : :
2085 : : return 0;
2086 : : }
2087 : :
2088 : : int
2089 : 0 : comp_cv_qualification (const_tree type1, const_tree type2)
2090 : : {
2091 : 0 : int q1 = cp_type_quals (type1);
2092 : 0 : int q2 = cp_type_quals (type2);
2093 : 0 : return comp_cv_qualification (q1, q2);
2094 : : }
2095 : :
2096 : : /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
2097 : : subset of the cv-qualification signature of TYPE2, and the types
2098 : : are similar. Returns -1 if the other way 'round, and 0 otherwise. */
2099 : :
2100 : : int
2101 : 381 : comp_cv_qual_signature (tree type1, tree type2)
2102 : : {
2103 : 381 : if (comp_ptr_ttypes_real (type2, type1, -1))
2104 : : return 1;
2105 : 295 : else if (comp_ptr_ttypes_real (type1, type2, -1))
2106 : : return -1;
2107 : : else
2108 : 283 : return 0;
2109 : : }
2110 : :
2111 : : /* Subroutines of `comptypes'. */
2112 : :
2113 : : /* Return true if two parameter type lists PARMS1 and PARMS2 are
2114 : : equivalent in the sense that functions with those parameter types
2115 : : can have equivalent types. The two lists must be equivalent,
2116 : : element by element. */
2117 : :
2118 : : bool
2119 : 887792660 : compparms (const_tree parms1, const_tree parms2)
2120 : : {
2121 : 887792660 : const_tree t1, t2;
2122 : :
2123 : : /* An unspecified parmlist matches any specified parmlist
2124 : : whose argument types don't need default promotions. */
2125 : :
2126 : 887792660 : for (t1 = parms1, t2 = parms2;
2127 : 1434041173 : t1 || t2;
2128 : 546248513 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2129 : : {
2130 : : /* If one parmlist is shorter than the other,
2131 : : they fail to match. */
2132 : 1347899170 : if (!t1 || !t2)
2133 : : return false;
2134 : 1347287622 : if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2135 : : return false;
2136 : : }
2137 : : return true;
2138 : : }
2139 : :
2140 : :
2141 : : /* Process a sizeof or alignof expression where the operand is a type.
2142 : : STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2143 : : or GNU (preferred alignment) semantics; it is ignored if OP is
2144 : : SIZEOF_EXPR. */
2145 : :
2146 : : tree
2147 : 24324535 : cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2148 : : bool std_alignof, bool complain)
2149 : : {
2150 : 24324535 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2151 : 24324535 : if (type == error_mark_node)
2152 : : return error_mark_node;
2153 : :
2154 : 24324533 : type = non_reference (type);
2155 : 24324533 : if (TREE_CODE (type) == METHOD_TYPE)
2156 : : {
2157 : 0 : if (complain)
2158 : : {
2159 : 0 : pedwarn (loc, OPT_Wpointer_arith,
2160 : : "invalid application of %qs to a member function",
2161 : 0 : OVL_OP_INFO (false, op)->name);
2162 : 0 : return size_one_node;
2163 : : }
2164 : : else
2165 : 0 : return error_mark_node;
2166 : : }
2167 : 24324533 : else if (VOID_TYPE_P (type) && std_alignof)
2168 : : {
2169 : 14 : if (complain)
2170 : 14 : error_at (loc, "invalid application of %qs to a void type",
2171 : 14 : OVL_OP_INFO (false, op)->name);
2172 : 14 : return error_mark_node;
2173 : : }
2174 : :
2175 : 24324519 : bool dependent_p = dependent_type_p (type);
2176 : 24324519 : if (!dependent_p)
2177 : : {
2178 : 20362631 : complete_type (type);
2179 : 20362631 : if (!COMPLETE_TYPE_P (type))
2180 : : /* Call this here because the incompleteness diagnostic comes from
2181 : : c_sizeof_or_alignof_type instead of
2182 : : complete_type_or_maybe_complain. */
2183 : 210 : note_failed_type_completion (type, complain);
2184 : : }
2185 : 20362631 : if (dependent_p
2186 : : /* VLA types will have a non-constant size. In the body of an
2187 : : uninstantiated template, we don't need to try to compute the
2188 : : value, because the sizeof expression is not an integral
2189 : : constant expression in that case. And, if we do try to
2190 : : compute the value, we'll likely end up with SAVE_EXPRs, which
2191 : : the template substitution machinery does not expect to see. */
2192 : 20362631 : || (processing_template_decl
2193 : 1786530 : && COMPLETE_TYPE_P (type)
2194 : 1786524 : && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2195 : : {
2196 : 3961888 : tree value = build_min (op, size_type_node, type);
2197 : 3961888 : TREE_READONLY (value) = 1;
2198 : 3961888 : if (op == ALIGNOF_EXPR && std_alignof)
2199 : 467821 : ALIGNOF_EXPR_STD_P (value) = true;
2200 : 3961888 : SET_EXPR_LOCATION (value, loc);
2201 : 3961888 : return value;
2202 : : }
2203 : :
2204 : 20362631 : return c_sizeof_or_alignof_type (loc, complete_type (type),
2205 : : op == SIZEOF_EXPR, std_alignof,
2206 : 20362631 : complain & (tf_warning_or_error));
2207 : : }
2208 : :
2209 : : /* Return the size of the type, without producing any warnings for
2210 : : types whose size cannot be taken. This routine should be used only
2211 : : in some other routine that has already produced a diagnostic about
2212 : : using the size of such a type. */
2213 : : tree
2214 : 2363714 : cxx_sizeof_nowarn (tree type)
2215 : : {
2216 : 2363714 : if (TREE_CODE (type) == FUNCTION_TYPE
2217 : 2363714 : || VOID_TYPE_P (type)
2218 : 2363632 : || TREE_CODE (type) == ERROR_MARK)
2219 : 82 : return size_one_node;
2220 : 2363632 : else if (!COMPLETE_TYPE_P (type))
2221 : 19 : return size_zero_node;
2222 : : else
2223 : 2363613 : return cxx_sizeof_or_alignof_type (input_location, type,
2224 : 2363613 : SIZEOF_EXPR, false, false);
2225 : : }
2226 : :
2227 : : /* Process a sizeof expression where the operand is an expression. */
2228 : :
2229 : : static tree
2230 : 1461251 : cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2231 : : {
2232 : 1461251 : if (e == error_mark_node)
2233 : : return error_mark_node;
2234 : :
2235 : 1460513 : if (instantiation_dependent_uneval_expression_p (e))
2236 : : {
2237 : 330927 : e = build_min (SIZEOF_EXPR, size_type_node, e);
2238 : 330927 : TREE_SIDE_EFFECTS (e) = 0;
2239 : 330927 : TREE_READONLY (e) = 1;
2240 : 330927 : SET_EXPR_LOCATION (e, loc);
2241 : :
2242 : 330927 : return e;
2243 : : }
2244 : :
2245 : 1129586 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2246 : 1129586 : STRIP_ANY_LOCATION_WRAPPER (e);
2247 : :
2248 : 1129586 : if (TREE_CODE (e) == PARM_DECL
2249 : 47490 : && DECL_ARRAY_PARAMETER_P (e)
2250 : 1129963 : && (complain & tf_warning))
2251 : : {
2252 : 120 : auto_diagnostic_group d;
2253 : 120 : if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2254 : : "%<sizeof%> on array function parameter %qE "
2255 : 120 : "will return size of %qT", e, TREE_TYPE (e)))
2256 : 24 : inform (DECL_SOURCE_LOCATION (e), "declared here");
2257 : 120 : }
2258 : :
2259 : 1129586 : e = mark_type_use (e);
2260 : :
2261 : 1129586 : if (bitfield_p (e))
2262 : : {
2263 : 12 : if (complain & tf_error)
2264 : 6 : error_at (e_loc,
2265 : : "invalid application of %<sizeof%> to a bit-field");
2266 : : else
2267 : 6 : return error_mark_node;
2268 : 6 : e = char_type_node;
2269 : : }
2270 : 1129574 : else if (is_overloaded_fn (e))
2271 : : {
2272 : 33 : if (complain & tf_error)
2273 : 12 : permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2274 : : "an expression of function type");
2275 : : else
2276 : 21 : return error_mark_node;
2277 : 12 : e = char_type_node;
2278 : : }
2279 : 1129541 : else if (type_unknown_p (e))
2280 : : {
2281 : 0 : if (complain & tf_error)
2282 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2283 : : else
2284 : 0 : return error_mark_node;
2285 : 0 : e = char_type_node;
2286 : : }
2287 : : else
2288 : 1129541 : e = TREE_TYPE (e);
2289 : :
2290 : 1129559 : return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2291 : 1129559 : complain & tf_error);
2292 : : }
2293 : :
2294 : : /* Implement the __alignof keyword: Return the minimum required
2295 : : alignment of E, measured in bytes. For VAR_DECL's and
2296 : : FIELD_DECL's return DECL_ALIGN (which can be set from an
2297 : : "aligned" __attribute__ specification). STD_ALIGNOF acts
2298 : : like in cxx_sizeof_or_alignof_type. */
2299 : :
2300 : : static tree
2301 : 101787 : cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2302 : : tsubst_flags_t complain)
2303 : : {
2304 : 101787 : tree t;
2305 : :
2306 : 101787 : if (e == error_mark_node)
2307 : : return error_mark_node;
2308 : :
2309 : 101779 : if (processing_template_decl)
2310 : : {
2311 : 20074 : e = build_min (ALIGNOF_EXPR, size_type_node, e);
2312 : 20074 : TREE_SIDE_EFFECTS (e) = 0;
2313 : 20074 : TREE_READONLY (e) = 1;
2314 : 20074 : SET_EXPR_LOCATION (e, loc);
2315 : 20074 : ALIGNOF_EXPR_STD_P (e) = std_alignof;
2316 : :
2317 : 20074 : return e;
2318 : : }
2319 : :
2320 : 81705 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2321 : 81705 : STRIP_ANY_LOCATION_WRAPPER (e);
2322 : :
2323 : 81705 : e = mark_type_use (e);
2324 : :
2325 : 81705 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2326 : : !(complain & tf_error)))
2327 : : {
2328 : 0 : if (!(complain & tf_error))
2329 : 0 : return error_mark_node;
2330 : 0 : t = size_one_node;
2331 : : }
2332 : 81705 : else if (VAR_P (e))
2333 : 12270 : t = size_int (DECL_ALIGN_UNIT (e));
2334 : 69435 : else if (bitfield_p (e))
2335 : : {
2336 : 6 : if (complain & tf_error)
2337 : 6 : error_at (e_loc,
2338 : : "invalid application of %<__alignof%> to a bit-field");
2339 : : else
2340 : 0 : return error_mark_node;
2341 : 6 : t = size_one_node;
2342 : : }
2343 : 69429 : else if (TREE_CODE (e) == COMPONENT_REF
2344 : 69429 : && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2345 : 37792 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2346 : 31637 : else if (is_overloaded_fn (e))
2347 : : {
2348 : 6 : if (complain & tf_error)
2349 : 6 : permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2350 : : "an expression of function type");
2351 : : else
2352 : 0 : return error_mark_node;
2353 : 6 : if (TREE_CODE (e) == FUNCTION_DECL)
2354 : 3 : t = size_int (DECL_ALIGN_UNIT (e));
2355 : : else
2356 : 3 : t = size_one_node;
2357 : : }
2358 : 31631 : else if (type_unknown_p (e))
2359 : : {
2360 : 0 : if (complain & tf_error)
2361 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2362 : : else
2363 : 0 : return error_mark_node;
2364 : 0 : t = size_one_node;
2365 : : }
2366 : : else
2367 : 31631 : return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2368 : : ALIGNOF_EXPR, std_alignof,
2369 : 31631 : complain & tf_error);
2370 : :
2371 : 50074 : return fold_convert_loc (loc, size_type_node, t);
2372 : : }
2373 : :
2374 : : /* Process a sizeof or alignof expression E with code OP where the operand
2375 : : is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2376 : :
2377 : : tree
2378 : 1563038 : cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2379 : : bool std_alignof, bool complain)
2380 : : {
2381 : 1563038 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2382 : 1563038 : if (op == SIZEOF_EXPR)
2383 : 2210561 : return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2384 : : else
2385 : 101814 : return cxx_alignof_expr (loc, e, std_alignof,
2386 : 101787 : complain? tf_warning_or_error : tf_none);
2387 : : }
2388 : :
2389 : : /* Build a representation of an expression 'alignas(E).' Return the
2390 : : folded integer value of E if it is an integral constant expression
2391 : : that resolves to a valid alignment. If E depends on a template
2392 : : parameter, return a syntactic representation tree of kind
2393 : : ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2394 : : expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2395 : :
2396 : : tree
2397 : 203836 : cxx_alignas_expr (tree e)
2398 : : {
2399 : 203836 : if (e == NULL_TREE || e == error_mark_node
2400 : 407672 : || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2401 : 0 : return e;
2402 : :
2403 : 203836 : if (TYPE_P (e))
2404 : : /* [dcl.align]/3:
2405 : :
2406 : : When the alignment-specifier is of the form
2407 : : alignas(type-id), it shall have the same effect as
2408 : : alignas(alignof(type-id)). */
2409 : :
2410 : 124442 : return cxx_sizeof_or_alignof_type (input_location,
2411 : : e, ALIGNOF_EXPR,
2412 : : /*std_alignof=*/true,
2413 : 124442 : /*complain=*/true);
2414 : :
2415 : : /* If we reach this point, it means the alignas expression if of
2416 : : the form "alignas(assignment-expression)", so we should follow
2417 : : what is stated by [dcl.align]/2. */
2418 : :
2419 : 79394 : if (value_dependent_expression_p (e))
2420 : : /* Leave value-dependent expression alone for now. */
2421 : : return e;
2422 : :
2423 : 21315 : e = instantiate_non_dependent_expr (e);
2424 : 21315 : e = mark_rvalue_use (e);
2425 : :
2426 : : /* [dcl.align]/2 says:
2427 : :
2428 : : the assignment-expression shall be an integral constant
2429 : : expression. */
2430 : :
2431 : 21315 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2432 : : {
2433 : 18 : error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2434 : 18 : return error_mark_node;
2435 : : }
2436 : :
2437 : 21297 : return cxx_constant_value (e);
2438 : : }
2439 : :
2440 : :
2441 : : /* EXPR is being used in a context that is not a function call.
2442 : : Enforce:
2443 : :
2444 : : [expr.ref]
2445 : :
2446 : : The expression can be used only as the left-hand operand of a
2447 : : member function call.
2448 : :
2449 : : [expr.mptr.operator]
2450 : :
2451 : : If the result of .* or ->* is a function, then that result can be
2452 : : used only as the operand for the function call operator ().
2453 : :
2454 : : by issuing an error message if appropriate. Returns true iff EXPR
2455 : : violates these rules. */
2456 : :
2457 : : bool
2458 : 1271628970 : invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2459 : : {
2460 : 1271628970 : if (expr == NULL_TREE)
2461 : : return false;
2462 : : /* Don't enforce this in MS mode. */
2463 : 1271627525 : if (flag_ms_extensions)
2464 : : return false;
2465 : 1271619464 : if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2466 : 105532541 : expr = get_first_fn (expr);
2467 : 1271619464 : if (TREE_TYPE (expr)
2468 : 1271619464 : && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2469 : : {
2470 : 96 : if (complain & tf_error)
2471 : : {
2472 : 88 : if (DECL_P (expr))
2473 : : {
2474 : 70 : auto_diagnostic_group d;
2475 : 70 : error_at (loc, "invalid use of non-static member function %qD",
2476 : : expr);
2477 : 70 : inform (DECL_SOURCE_LOCATION (expr), "declared here");
2478 : 70 : }
2479 : : else
2480 : 18 : error_at (loc, "invalid use of non-static member function of "
2481 : 18 : "type %qT", TREE_TYPE (expr));
2482 : : }
2483 : 96 : return true;
2484 : : }
2485 : : return false;
2486 : : }
2487 : :
2488 : : /* If EXP is a reference to a bit-field, and the type of EXP does not
2489 : : match the declared type of the bit-field, return the declared type
2490 : : of the bit-field. Otherwise, return NULL_TREE. */
2491 : :
2492 : : tree
2493 : 2959577038 : is_bitfield_expr_with_lowered_type (const_tree exp)
2494 : : {
2495 : 4078401044 : switch (TREE_CODE (exp))
2496 : : {
2497 : 6074624 : case COND_EXPR:
2498 : 12149248 : if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2499 : 6074598 : ? TREE_OPERAND (exp, 1)
2500 : 26 : : TREE_OPERAND (exp, 0)))
2501 : : return NULL_TREE;
2502 : 135 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2503 : :
2504 : 924143 : case COMPOUND_EXPR:
2505 : 924143 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2506 : :
2507 : 366347239 : case MODIFY_EXPR:
2508 : 366347239 : case SAVE_EXPR:
2509 : 366347239 : case UNARY_PLUS_EXPR:
2510 : 366347239 : case PREDECREMENT_EXPR:
2511 : 366347239 : case PREINCREMENT_EXPR:
2512 : 366347239 : case POSTDECREMENT_EXPR:
2513 : 366347239 : case POSTINCREMENT_EXPR:
2514 : 366347239 : case NEGATE_EXPR:
2515 : 366347239 : case NON_LVALUE_EXPR:
2516 : 366347239 : case BIT_NOT_EXPR:
2517 : 366347239 : case CLEANUP_POINT_EXPR:
2518 : 366347239 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2519 : :
2520 : 250546308 : case COMPONENT_REF:
2521 : 250546308 : {
2522 : 250546308 : tree field;
2523 : :
2524 : 250546308 : field = TREE_OPERAND (exp, 1);
2525 : 250546308 : if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2526 : : return NULL_TREE;
2527 : 44585144 : if (same_type_ignoring_top_level_qualifiers_p
2528 : 44585144 : (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2529 : : return NULL_TREE;
2530 : 44417382 : return DECL_BIT_FIELD_TYPE (field);
2531 : : }
2532 : :
2533 : 554355410 : case VAR_DECL:
2534 : 554355410 : if (DECL_HAS_VALUE_EXPR_P (exp))
2535 : 2072035 : return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2536 : 2072035 : (CONST_CAST_TREE (exp)));
2537 : : return NULL_TREE;
2538 : :
2539 : 756997039 : case VIEW_CONVERT_EXPR:
2540 : 756997039 : if (location_wrapper_p (exp))
2541 : 749480454 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2542 : : else
2543 : : return NULL_TREE;
2544 : :
2545 : : default:
2546 : : return NULL_TREE;
2547 : : }
2548 : : }
2549 : :
2550 : : /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2551 : : bitfield with a lowered type, the type of EXP is returned, rather
2552 : : than NULL_TREE. */
2553 : :
2554 : : tree
2555 : 1385208262 : unlowered_expr_type (const_tree exp)
2556 : : {
2557 : 1385208262 : tree type;
2558 : 1385208262 : tree etype = TREE_TYPE (exp);
2559 : :
2560 : 1385208262 : type = is_bitfield_expr_with_lowered_type (exp);
2561 : 1385208262 : if (type)
2562 : 37598178 : type = cp_build_qualified_type (type, cp_type_quals (etype));
2563 : : else
2564 : : type = etype;
2565 : :
2566 : 1385208262 : return type;
2567 : : }
2568 : :
2569 : : /* Perform the conversions in [expr] that apply when an lvalue appears
2570 : : in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2571 : : function-to-pointer conversions. In addition, bitfield references are
2572 : : converted to their declared types. Note that this function does not perform
2573 : : the lvalue-to-rvalue conversion for class types. If you need that conversion
2574 : : for class types, then you probably need to use force_rvalue.
2575 : :
2576 : : Although the returned value is being used as an rvalue, this
2577 : : function does not wrap the returned expression in a
2578 : : NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2579 : : that the return value is no longer an lvalue. */
2580 : :
2581 : : tree
2582 : 1015292180 : decay_conversion (tree exp,
2583 : : tsubst_flags_t complain,
2584 : : bool reject_builtin /* = true */)
2585 : : {
2586 : 1015292180 : tree type;
2587 : 1015292180 : enum tree_code code;
2588 : 1015292180 : location_t loc = cp_expr_loc_or_input_loc (exp);
2589 : :
2590 : 1015292180 : type = TREE_TYPE (exp);
2591 : 1015292180 : if (type == error_mark_node)
2592 : : return error_mark_node;
2593 : :
2594 : 1015291970 : exp = resolve_nondeduced_context_or_error (exp, complain);
2595 : :
2596 : 1015291970 : code = TREE_CODE (type);
2597 : :
2598 : 1015291970 : if (error_operand_p (exp))
2599 : 90 : return error_mark_node;
2600 : :
2601 : 1015291880 : if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2602 : : {
2603 : 1520977 : mark_rvalue_use (exp, loc, reject_builtin);
2604 : 1520977 : return nullptr_node;
2605 : : }
2606 : :
2607 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2608 : : Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2609 : 1013770903 : if (code == VOID_TYPE)
2610 : : {
2611 : 5787 : if (complain & tf_error)
2612 : 3 : error_at (loc, "void value not ignored as it ought to be");
2613 : 5787 : return error_mark_node;
2614 : : }
2615 : 1013765116 : if (invalid_nonstatic_memfn_p (loc, exp, complain))
2616 : 6 : return error_mark_node;
2617 : 1013765110 : if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2618 : : {
2619 : 107903776 : exp = mark_lvalue_use (exp);
2620 : 107903776 : if (reject_builtin && reject_gcc_builtin (exp, loc))
2621 : 109 : return error_mark_node;
2622 : 107903667 : return cp_build_addr_expr (exp, complain);
2623 : : }
2624 : 905861334 : if (code == ARRAY_TYPE)
2625 : : {
2626 : 5588257 : tree adr;
2627 : 5588257 : tree ptrtype;
2628 : :
2629 : 5588257 : exp = mark_lvalue_use (exp);
2630 : :
2631 : 5588257 : if (INDIRECT_REF_P (exp))
2632 : 98825 : return build_nop (build_pointer_type (TREE_TYPE (type)),
2633 : 197650 : TREE_OPERAND (exp, 0));
2634 : :
2635 : 5489432 : if (TREE_CODE (exp) == COMPOUND_EXPR)
2636 : : {
2637 : 3 : tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2638 : 3 : if (op1 == error_mark_node)
2639 : : return error_mark_node;
2640 : 3 : return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2641 : 6 : TREE_OPERAND (exp, 0), op1);
2642 : : }
2643 : :
2644 : 5489429 : if (!obvalue_p (exp)
2645 : 5489429 : && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2646 : : {
2647 : 0 : if (complain & tf_error)
2648 : 0 : error_at (loc, "invalid use of non-lvalue array");
2649 : 0 : return error_mark_node;
2650 : : }
2651 : :
2652 : 5489429 : ptrtype = build_pointer_type (TREE_TYPE (type));
2653 : :
2654 : 5489429 : if (VAR_P (exp))
2655 : : {
2656 : 539576 : if (!cxx_mark_addressable (exp))
2657 : 0 : return error_mark_node;
2658 : 539576 : adr = build_nop (ptrtype, build_address (exp));
2659 : 539576 : return adr;
2660 : : }
2661 : : /* This way is better for a COMPONENT_REF since it can
2662 : : simplify the offset for a component. */
2663 : 4949853 : adr = cp_build_addr_expr (exp, complain);
2664 : 4949853 : return cp_convert (ptrtype, adr, complain);
2665 : : }
2666 : :
2667 : : /* Otherwise, it's the lvalue-to-rvalue conversion. */
2668 : 900273077 : exp = mark_rvalue_use (exp, loc, reject_builtin);
2669 : :
2670 : : /* If a bitfield is used in a context where integral promotion
2671 : : applies, then the caller is expected to have used
2672 : : default_conversion. That function promotes bitfields correctly
2673 : : before calling this function. At this point, if we have a
2674 : : bitfield referenced, we may assume that is not subject to
2675 : : promotion, and that, therefore, the type of the resulting rvalue
2676 : : is the declared type of the bitfield. */
2677 : 900273077 : exp = convert_bitfield_to_declared_type (exp);
2678 : :
2679 : : /* We do not call rvalue() here because we do not want to wrap EXP
2680 : : in a NON_LVALUE_EXPR. */
2681 : :
2682 : : /* [basic.lval]
2683 : :
2684 : : Non-class rvalues always have cv-unqualified types. */
2685 : 900273077 : type = TREE_TYPE (exp);
2686 : 900273077 : if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2687 : 275717673 : exp = build_nop (cv_unqualified (type), exp);
2688 : :
2689 : 900273077 : if (!complete_type_or_maybe_complain (type, exp, complain))
2690 : 42 : return error_mark_node;
2691 : :
2692 : : return exp;
2693 : : }
2694 : :
2695 : : /* Perform preparatory conversions, as part of the "usual arithmetic
2696 : : conversions". In particular, as per [expr]:
2697 : :
2698 : : Whenever an lvalue expression appears as an operand of an
2699 : : operator that expects the rvalue for that operand, the
2700 : : lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2701 : : standard conversions are applied to convert the expression to an
2702 : : rvalue.
2703 : :
2704 : : In addition, we perform integral promotions here, as those are
2705 : : applied to both operands to a binary operator before determining
2706 : : what additional conversions should apply. */
2707 : :
2708 : : static tree
2709 : 276090750 : cp_default_conversion (tree exp, tsubst_flags_t complain)
2710 : : {
2711 : : /* Check for target-specific promotions. */
2712 : 276090750 : tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2713 : 276090750 : if (promoted_type)
2714 : 0 : exp = cp_convert (promoted_type, exp, complain);
2715 : : /* Perform the integral promotions first so that bitfield
2716 : : expressions (which may promote to "int", even if the bitfield is
2717 : : declared "unsigned") are promoted correctly. */
2718 : 276090750 : else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2719 : 184041277 : exp = cp_perform_integral_promotions (exp, complain);
2720 : : /* Perform the other conversions. */
2721 : 276090750 : exp = decay_conversion (exp, complain);
2722 : :
2723 : 276090750 : return exp;
2724 : : }
2725 : :
2726 : : /* C version. */
2727 : :
2728 : : tree
2729 : 40426719 : default_conversion (tree exp)
2730 : : {
2731 : 40426719 : return cp_default_conversion (exp, tf_warning_or_error);
2732 : : }
2733 : :
2734 : : /* EXPR is an expression with an integral or enumeration type.
2735 : : Perform the integral promotions in [conv.prom], and return the
2736 : : converted value. */
2737 : :
2738 : : tree
2739 : 190456814 : cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2740 : : {
2741 : 190456814 : tree type;
2742 : 190456814 : tree promoted_type;
2743 : :
2744 : 190456814 : expr = mark_rvalue_use (expr);
2745 : 190456814 : if (error_operand_p (expr))
2746 : 6 : return error_mark_node;
2747 : :
2748 : 190456808 : type = TREE_TYPE (expr);
2749 : :
2750 : : /* [conv.prom]
2751 : :
2752 : : A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2753 : : of type int if int can represent all the values of the bit-field;
2754 : : otherwise, it can be converted to unsigned int if unsigned int can
2755 : : represent all the values of the bit-field. If the bit-field is larger yet,
2756 : : no integral promotion applies to it. If the bit-field has an enumerated
2757 : : type, it is treated as any other value of that type for promotion
2758 : : purposes. */
2759 : 190456808 : tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2760 : 190456808 : if (bitfield_type
2761 : 190456808 : && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2762 : 377638 : || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2763 : : type = bitfield_type;
2764 : :
2765 : 190456808 : gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2766 : : /* Scoped enums don't promote. */
2767 : 190456808 : if (SCOPED_ENUM_P (type))
2768 : : return expr;
2769 : 189255489 : promoted_type = type_promotes_to (type);
2770 : 189255489 : if (type != promoted_type)
2771 : 52985678 : expr = cp_convert (promoted_type, expr, complain);
2772 : 136269811 : else if (bitfield_type && bitfield_type != type)
2773 : : /* Prevent decay_conversion from converting to bitfield_type. */
2774 : 159 : expr = build_nop (type, expr);
2775 : : return expr;
2776 : : }
2777 : :
2778 : : /* C version. */
2779 : :
2780 : : tree
2781 : 2150128 : perform_integral_promotions (tree expr)
2782 : : {
2783 : 2150128 : return cp_perform_integral_promotions (expr, tf_warning_or_error);
2784 : : }
2785 : :
2786 : : /* Returns nonzero iff exp is a STRING_CST or the result of applying
2787 : : decay_conversion to one. */
2788 : :
2789 : : int
2790 : 3330787 : string_conv_p (const_tree totype, const_tree exp, int warn)
2791 : : {
2792 : 3330787 : tree t;
2793 : :
2794 : 3330787 : if (!TYPE_PTR_P (totype))
2795 : : return 0;
2796 : :
2797 : 3330736 : t = TREE_TYPE (totype);
2798 : 3330736 : if (!same_type_p (t, char_type_node)
2799 : 3192310 : && !same_type_p (t, char8_type_node)
2800 : 3167455 : && !same_type_p (t, char16_type_node)
2801 : 3116389 : && !same_type_p (t, char32_type_node)
2802 : 6396060 : && !same_type_p (t, wchar_type_node))
2803 : : return 0;
2804 : :
2805 : 299571 : location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2806 : :
2807 : 299571 : STRIP_ANY_LOCATION_WRAPPER (exp);
2808 : :
2809 : 299571 : if (TREE_CODE (exp) == STRING_CST)
2810 : : {
2811 : : /* Make sure that we don't try to convert between char and wide chars. */
2812 : 108 : if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2813 : : return 0;
2814 : : }
2815 : : else
2816 : : {
2817 : : /* Is this a string constant which has decayed to 'const char *'? */
2818 : 299463 : t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2819 : 299463 : if (!same_type_p (TREE_TYPE (exp), t))
2820 : : return 0;
2821 : 13772 : STRIP_NOPS (exp);
2822 : 13772 : if (TREE_CODE (exp) != ADDR_EXPR
2823 : 13772 : || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2824 : : return 0;
2825 : : }
2826 : 306 : if (warn)
2827 : : {
2828 : 102 : if (cxx_dialect >= cxx11)
2829 : 87 : pedwarn (loc, OPT_Wwrite_strings,
2830 : : "ISO C++ forbids converting a string constant to %qT",
2831 : : totype);
2832 : : else
2833 : 15 : warning_at (loc, OPT_Wwrite_strings,
2834 : : "deprecated conversion from string constant to %qT",
2835 : : totype);
2836 : : }
2837 : :
2838 : : return 1;
2839 : : }
2840 : :
2841 : : /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2842 : : can, for example, use as an lvalue. This code used to be in
2843 : : unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2844 : : expressions, where we're dealing with aggregates. But now it's again only
2845 : : called from unary_complex_lvalue. The case (in particular) that led to
2846 : : this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2847 : : get it there. */
2848 : :
2849 : : static tree
2850 : 111738 : rationalize_conditional_expr (enum tree_code code, tree t,
2851 : : tsubst_flags_t complain)
2852 : : {
2853 : 111738 : location_t loc = cp_expr_loc_or_input_loc (t);
2854 : :
2855 : : /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2856 : : the first operand is always the one to be used if both operands
2857 : : are equal, so we know what conditional expression this used to be. */
2858 : 111738 : if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2859 : : {
2860 : 0 : tree op0 = TREE_OPERAND (t, 0);
2861 : 0 : tree op1 = TREE_OPERAND (t, 1);
2862 : :
2863 : : /* The following code is incorrect if either operand side-effects. */
2864 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (op0)
2865 : : && !TREE_SIDE_EFFECTS (op1));
2866 : 0 : return
2867 : 0 : build_conditional_expr (loc,
2868 : 0 : build_x_binary_op (loc,
2869 : 0 : (TREE_CODE (t) == MIN_EXPR
2870 : : ? LE_EXPR : GE_EXPR),
2871 : 0 : op0, TREE_CODE (op0),
2872 : 0 : op1, TREE_CODE (op1),
2873 : : NULL_TREE,
2874 : : /*overload=*/NULL,
2875 : : complain),
2876 : : cp_build_unary_op (code, op0, false, complain),
2877 : : cp_build_unary_op (code, op1, false, complain),
2878 : : complain);
2879 : : }
2880 : :
2881 : 111738 : tree op1 = TREE_OPERAND (t, 1);
2882 : 111738 : if (TREE_CODE (op1) != THROW_EXPR)
2883 : 111735 : op1 = cp_build_unary_op (code, op1, false, complain);
2884 : 111738 : tree op2 = TREE_OPERAND (t, 2);
2885 : 111738 : if (TREE_CODE (op2) != THROW_EXPR)
2886 : 111732 : op2 = cp_build_unary_op (code, op2, false, complain);
2887 : :
2888 : 111738 : return
2889 : 111738 : build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2890 : : }
2891 : :
2892 : : /* Given the TYPE of an anonymous union field inside T, return the
2893 : : FIELD_DECL for the field. If not found return NULL_TREE. Because
2894 : : anonymous unions can nest, we must also search all anonymous unions
2895 : : that are directly reachable. */
2896 : :
2897 : : tree
2898 : 1213994 : lookup_anon_field (tree, tree type)
2899 : : {
2900 : 1213994 : tree field;
2901 : :
2902 : 1213994 : type = TYPE_MAIN_VARIANT (type);
2903 : 1213994 : field = ANON_AGGR_TYPE_FIELD (type);
2904 : 1213994 : gcc_assert (field);
2905 : 1213994 : return field;
2906 : : }
2907 : :
2908 : : /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2909 : : expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2910 : : non-NULL, it indicates the path to the base used to name MEMBER.
2911 : : If PRESERVE_REFERENCE is true, the expression returned will have
2912 : : REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2913 : : returned will have the type referred to by the reference.
2914 : :
2915 : : This function does not perform access control; that is either done
2916 : : earlier by the parser when the name of MEMBER is resolved to MEMBER
2917 : : itself, or later when overload resolution selects one of the
2918 : : functions indicated by MEMBER. */
2919 : :
2920 : : tree
2921 : 109109875 : build_class_member_access_expr (cp_expr object, tree member,
2922 : : tree access_path, bool preserve_reference,
2923 : : tsubst_flags_t complain)
2924 : : {
2925 : 109109875 : tree object_type;
2926 : 109109875 : tree member_scope;
2927 : 109109875 : tree result = NULL_TREE;
2928 : 109109875 : tree using_decl = NULL_TREE;
2929 : :
2930 : 109109875 : if (error_operand_p (object) || error_operand_p (member))
2931 : 57 : return error_mark_node;
2932 : :
2933 : 109109818 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2934 : :
2935 : : /* [expr.ref]
2936 : :
2937 : : The type of the first expression shall be "class object" (of a
2938 : : complete type). */
2939 : 109109818 : object_type = TREE_TYPE (object);
2940 : 109109818 : if (!currently_open_class (object_type)
2941 : 109109818 : && !complete_type_or_maybe_complain (object_type, object, complain))
2942 : 0 : return error_mark_node;
2943 : 109109818 : if (!CLASS_TYPE_P (object_type))
2944 : : {
2945 : 0 : if (complain & tf_error)
2946 : : {
2947 : 0 : if (INDIRECT_TYPE_P (object_type)
2948 : 0 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
2949 : 0 : error ("request for member %qD in %qE, which is of pointer "
2950 : : "type %qT (maybe you meant to use %<->%> ?)",
2951 : : member, object.get_value (), object_type);
2952 : : else
2953 : 0 : error ("request for member %qD in %qE, which is of non-class "
2954 : : "type %qT", member, object.get_value (), object_type);
2955 : : }
2956 : 0 : return error_mark_node;
2957 : : }
2958 : :
2959 : : /* The standard does not seem to actually say that MEMBER must be a
2960 : : member of OBJECT_TYPE. However, that is clearly what is
2961 : : intended. */
2962 : 109109818 : if (DECL_P (member))
2963 : : {
2964 : 46681631 : member_scope = DECL_CLASS_CONTEXT (member);
2965 : 46681631 : if (!mark_used (member, complain) && !(complain & tf_error))
2966 : 0 : return error_mark_node;
2967 : :
2968 : 46681631 : if (TREE_UNAVAILABLE (member))
2969 : 30 : error_unavailable_use (member, NULL_TREE);
2970 : 46681601 : else if (TREE_DEPRECATED (member))
2971 : 30 : warn_deprecated_use (member, NULL_TREE);
2972 : : }
2973 : : else
2974 : 62428187 : member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2975 : : /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2976 : : presently be the anonymous union. Go outwards until we find a
2977 : : type related to OBJECT_TYPE. */
2978 : 110327650 : while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2979 : 111545893 : && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2980 : : object_type))
2981 : 1217832 : member_scope = TYPE_CONTEXT (member_scope);
2982 : 109109818 : if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2983 : : {
2984 : 3 : if (complain & tf_error)
2985 : : {
2986 : 3 : if (TREE_CODE (member) == FIELD_DECL)
2987 : 0 : error ("invalid use of non-static data member %qE", member);
2988 : : else
2989 : 3 : error ("%qD is not a member of %qT", member, object_type);
2990 : : }
2991 : 3 : return error_mark_node;
2992 : : }
2993 : :
2994 : : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2995 : : `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2996 : : in the front end; only _DECLs and _REFs are lvalues in the back end. */
2997 : 109109815 : if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2998 : : {
2999 : 50 : temp = cp_build_fold_indirect_ref (temp);
3000 : 50 : if (!lvalue_p (object) && lvalue_p (temp))
3001 : : /* Preserve rvalueness. */
3002 : 6 : temp = move (temp);
3003 : 50 : object = temp;
3004 : : }
3005 : :
3006 : : /* In [expr.ref], there is an explicit list of the valid choices for
3007 : : MEMBER. We check for each of those cases here. */
3008 : 109109815 : if (VAR_P (member))
3009 : : {
3010 : : /* A static data member. */
3011 : 106299 : result = member;
3012 : 106299 : mark_exp_read (object);
3013 : :
3014 : 106299 : if (tree wrap = maybe_get_tls_wrapper_call (result))
3015 : : /* Replace an evaluated use of the thread_local variable with
3016 : : a call to its wrapper. */
3017 : 102 : result = wrap;
3018 : :
3019 : : /* If OBJECT has side-effects, they are supposed to occur. */
3020 : 106299 : if (TREE_SIDE_EFFECTS (object))
3021 : 57 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
3022 : : }
3023 : 109003516 : else if (TREE_CODE (member) == FIELD_DECL)
3024 : : {
3025 : : /* A non-static data member. */
3026 : 46575133 : bool null_object_p;
3027 : 46575133 : int type_quals;
3028 : 46575133 : tree member_type;
3029 : :
3030 : 46575133 : if (INDIRECT_REF_P (object))
3031 : 35581911 : null_object_p =
3032 : 35581911 : integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
3033 : : else
3034 : : null_object_p = false;
3035 : :
3036 : : /* Convert OBJECT to the type of MEMBER. */
3037 : 46575133 : if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
3038 : : TYPE_MAIN_VARIANT (member_scope)))
3039 : : {
3040 : 1940406 : tree binfo;
3041 : 1940406 : base_kind kind;
3042 : :
3043 : : /* We didn't complain above about a currently open class, but now we
3044 : : must: we don't know how to refer to a base member before layout is
3045 : : complete. But still don't complain in a template. */
3046 : 1940406 : if (!cp_unevaluated_operand
3047 : 1939883 : && !dependent_type_p (object_type)
3048 : 3572212 : && !complete_type_or_maybe_complain (object_type, object,
3049 : : complain))
3050 : 9 : return error_mark_node;
3051 : :
3052 : 2446177 : binfo = lookup_base (access_path ? access_path : object_type,
3053 : : member_scope, ba_unique, &kind, complain);
3054 : 1940403 : if (binfo == error_mark_node)
3055 : : return error_mark_node;
3056 : :
3057 : : /* It is invalid to try to get to a virtual base of a
3058 : : NULL object. The most common cause is invalid use of
3059 : : offsetof macro. */
3060 : 1940403 : if (null_object_p && kind == bk_via_virtual)
3061 : : {
3062 : 6 : if (complain & tf_error)
3063 : : {
3064 : 6 : error ("invalid access to non-static data member %qD in "
3065 : : "virtual base of NULL object", member);
3066 : : }
3067 : 6 : return error_mark_node;
3068 : : }
3069 : :
3070 : : /* Convert to the base. */
3071 : 1940397 : object = build_base_path (PLUS_EXPR, object, binfo,
3072 : : /*nonnull=*/1, complain);
3073 : : /* If we found the base successfully then we should be able
3074 : : to convert to it successfully. */
3075 : 1940397 : gcc_assert (object != error_mark_node || seen_error ());
3076 : : }
3077 : :
3078 : : /* If MEMBER is from an anonymous aggregate, we have converted
3079 : : OBJECT so that it refers to the class containing the
3080 : : anonymous union. Generate a reference to the anonymous union
3081 : : itself, and recur to find MEMBER. */
3082 : 93150248 : if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
3083 : : /* When this code is called from build_field_call, the
3084 : : object already has the type of the anonymous union.
3085 : : That is because the COMPONENT_REF was already
3086 : : constructed, and was then disassembled before calling
3087 : : build_field_call. After the function-call code is
3088 : : cleaned up, this waste can be eliminated. */
3089 : 47789397 : && (!same_type_ignoring_top_level_qualifiers_p
3090 : 1214273 : (TREE_TYPE (object), DECL_CONTEXT (member))))
3091 : : {
3092 : 1213868 : tree anonymous_union;
3093 : :
3094 : 1213868 : anonymous_union = lookup_anon_field (TREE_TYPE (object),
3095 : 1213868 : DECL_CONTEXT (member));
3096 : 1213868 : object = build_class_member_access_expr (object,
3097 : : anonymous_union,
3098 : : /*access_path=*/NULL_TREE,
3099 : : preserve_reference,
3100 : : complain);
3101 : : }
3102 : :
3103 : : /* Compute the type of the field, as described in [expr.ref]. */
3104 : 46575124 : type_quals = TYPE_UNQUALIFIED;
3105 : 46575124 : member_type = TREE_TYPE (member);
3106 : 46575124 : if (!TYPE_REF_P (member_type))
3107 : : {
3108 : 45248463 : type_quals = (cp_type_quals (member_type)
3109 : 45248463 : | cp_type_quals (object_type));
3110 : :
3111 : : /* A field is const (volatile) if the enclosing object, or the
3112 : : field itself, is const (volatile). But, a mutable field is
3113 : : not const, even within a const object. */
3114 : 45248463 : if (DECL_MUTABLE_P (member))
3115 : 334955 : type_quals &= ~TYPE_QUAL_CONST;
3116 : 45248463 : member_type = cp_build_qualified_type (member_type, type_quals);
3117 : : }
3118 : :
3119 : 46575124 : result = build3_loc (input_location, COMPONENT_REF, member_type,
3120 : : object, member, NULL_TREE);
3121 : :
3122 : : /* Mark the expression const or volatile, as appropriate. Even
3123 : : though we've dealt with the type above, we still have to mark the
3124 : : expression itself. */
3125 : 46575124 : if (type_quals & TYPE_QUAL_CONST)
3126 : 14792479 : TREE_READONLY (result) = 1;
3127 : 46575124 : if (type_quals & TYPE_QUAL_VOLATILE)
3128 : 187837 : TREE_THIS_VOLATILE (result) = 1;
3129 : : }
3130 : 62428383 : else if (BASELINK_P (member))
3131 : : {
3132 : : /* The member is a (possibly overloaded) member function. */
3133 : 62428184 : tree functions;
3134 : 62428184 : tree type;
3135 : :
3136 : : /* If the MEMBER is exactly one static member function, then we
3137 : : know the type of the expression. Otherwise, we must wait
3138 : : until overload resolution has been performed. */
3139 : 62428184 : functions = BASELINK_FUNCTIONS (member);
3140 : 62428184 : if (TREE_CODE (functions) == OVERLOAD && OVL_SINGLE_P (functions))
3141 : 62428184 : functions = OVL_FIRST (functions);
3142 : 62428184 : if (TREE_CODE (functions) == FUNCTION_DECL
3143 : 62428184 : && DECL_STATIC_FUNCTION_P (functions))
3144 : 487699 : type = TREE_TYPE (functions);
3145 : : else
3146 : 61940485 : type = unknown_type_node;
3147 : : /* Note that we do not convert OBJECT to the BASELINK_BINFO
3148 : : base. That will happen when the function is called. */
3149 : 62428184 : result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3150 : : NULL_TREE);
3151 : : }
3152 : 199 : else if (TREE_CODE (member) == CONST_DECL)
3153 : : {
3154 : : /* The member is an enumerator. */
3155 : 187 : result = member;
3156 : : /* If OBJECT has side-effects, they are supposed to occur. */
3157 : 187 : if (TREE_SIDE_EFFECTS (object))
3158 : 6 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3159 : : object, result);
3160 : : }
3161 : 12 : else if ((using_decl = strip_using_decl (member)) != member)
3162 : 0 : result = build_class_member_access_expr (object,
3163 : : using_decl,
3164 : : access_path, preserve_reference,
3165 : : complain);
3166 : : else
3167 : : {
3168 : 12 : if (complain & tf_error)
3169 : 12 : error ("invalid use of %qD", member);
3170 : 12 : return error_mark_node;
3171 : : }
3172 : :
3173 : 109109794 : if (!preserve_reference)
3174 : : /* [expr.ref]
3175 : :
3176 : : If E2 is declared to have type "reference to T", then ... the
3177 : : type of E1.E2 is T. */
3178 : 103339759 : result = convert_from_reference (result);
3179 : :
3180 : : return result;
3181 : : }
3182 : :
3183 : : /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3184 : : SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3185 : :
3186 : : tree
3187 : 181727 : lookup_destructor (tree object, tree scope, tree dtor_name,
3188 : : tsubst_flags_t complain)
3189 : : {
3190 : 181727 : tree object_type = TREE_TYPE (object);
3191 : 181727 : tree dtor_type = TREE_OPERAND (dtor_name, 0);
3192 : 181727 : tree expr;
3193 : :
3194 : : /* We've already complained about this destructor. */
3195 : 181727 : if (dtor_type == error_mark_node)
3196 : : return error_mark_node;
3197 : :
3198 : 181721 : if (scope && !check_dtor_name (scope, dtor_type))
3199 : : {
3200 : 3 : if (complain & tf_error)
3201 : 3 : error ("qualified type %qT does not match destructor name ~%qT",
3202 : : scope, dtor_type);
3203 : 3 : return error_mark_node;
3204 : : }
3205 : 181718 : if (is_auto (dtor_type))
3206 : : dtor_type = object_type;
3207 : 181715 : else if (identifier_p (dtor_type))
3208 : : {
3209 : : /* In a template, names we can't find a match for are still accepted
3210 : : destructor names, and we check them here. */
3211 : 23 : if (check_dtor_name (object_type, dtor_type))
3212 : : dtor_type = object_type;
3213 : : else
3214 : : {
3215 : 3 : if (complain & tf_error)
3216 : 3 : error ("object type %qT does not match destructor name ~%qT",
3217 : : object_type, dtor_type);
3218 : 3 : return error_mark_node;
3219 : : }
3220 : :
3221 : : }
3222 : 181692 : else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3223 : : {
3224 : 6 : if (complain & tf_error)
3225 : 6 : error ("the type being destroyed is %qT, but the destructor "
3226 : 6 : "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3227 : 6 : return error_mark_node;
3228 : : }
3229 : 181709 : expr = lookup_member (dtor_type, complete_dtor_identifier,
3230 : : /*protect=*/1, /*want_type=*/false,
3231 : : tf_warning_or_error);
3232 : 181709 : if (!expr)
3233 : : {
3234 : 6 : if (complain & tf_error)
3235 : 6 : cxx_incomplete_type_error (dtor_name, dtor_type);
3236 : 6 : return error_mark_node;
3237 : : }
3238 : 181703 : expr = (adjust_result_of_qualified_name_lookup
3239 : 181703 : (expr, dtor_type, object_type));
3240 : 181703 : if (scope == NULL_TREE)
3241 : : /* We need to call adjust_result_of_qualified_name_lookup in case the
3242 : : destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3243 : : that we still get virtual function binding. */
3244 : 181560 : BASELINK_QUALIFIED_P (expr) = false;
3245 : : return expr;
3246 : : }
3247 : :
3248 : : /* An expression of the form "A::template B" has been resolved to
3249 : : DECL. Issue a diagnostic if B is not a template or template
3250 : : specialization. */
3251 : :
3252 : : void
3253 : 3021516 : check_template_keyword (tree decl)
3254 : : {
3255 : : /* The standard says:
3256 : :
3257 : : [temp.names]
3258 : :
3259 : : If a name prefixed by the keyword template is not a member
3260 : : template, the program is ill-formed.
3261 : :
3262 : : DR 228 removed the restriction that the template be a member
3263 : : template.
3264 : :
3265 : : DR 96, if accepted would add the further restriction that explicit
3266 : : template arguments must be provided if the template keyword is
3267 : : used, but, as of 2005-10-16, that DR is still in "drafting". If
3268 : : this DR is accepted, then the semantic checks here can be
3269 : : simplified, as the entity named must in fact be a template
3270 : : specialization, rather than, as at present, a set of overloaded
3271 : : functions containing at least one template function. */
3272 : 3021516 : if (TREE_CODE (decl) != TEMPLATE_DECL
3273 : 3021516 : && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3274 : : {
3275 : 2720897 : if (VAR_P (decl))
3276 : : {
3277 : 9783 : if (DECL_USE_TEMPLATE (decl)
3278 : 9783 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3279 : : ;
3280 : : else
3281 : 0 : permerror (input_location, "%qD is not a template", decl);
3282 : : }
3283 : 2711114 : else if (!is_overloaded_fn (decl))
3284 : 0 : permerror (input_location, "%qD is not a template", decl);
3285 : : else
3286 : : {
3287 : 2711114 : bool found = false;
3288 : :
3289 : 5422228 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3290 : 5422230 : !found && iter; ++iter)
3291 : : {
3292 : 2711116 : tree fn = *iter;
3293 : 2711116 : if (TREE_CODE (fn) == TEMPLATE_DECL
3294 : 2710720 : || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3295 : 2711130 : || (TREE_CODE (fn) == FUNCTION_DECL
3296 : 14 : && DECL_USE_TEMPLATE (fn)
3297 : 6 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3298 : : found = true;
3299 : : }
3300 : 2711114 : if (!found)
3301 : 12 : permerror (input_location, "%qD is not a template", decl);
3302 : : }
3303 : : }
3304 : 3021516 : }
3305 : :
3306 : : /* Record that an access failure occurred on BASETYPE_PATH attempting
3307 : : to access DECL, where DIAG_DECL should be used for diagnostics. */
3308 : :
3309 : : void
3310 : 412 : access_failure_info::record_access_failure (tree basetype_path,
3311 : : tree decl, tree diag_decl)
3312 : : {
3313 : 412 : m_was_inaccessible = true;
3314 : 412 : m_basetype_path = basetype_path;
3315 : 412 : m_decl = decl;
3316 : 412 : m_diag_decl = diag_decl;
3317 : 412 : }
3318 : :
3319 : : /* If an access failure was recorded, then attempt to locate an
3320 : : accessor function for the pertinent field.
3321 : : Otherwise, return NULL_TREE. */
3322 : :
3323 : : tree
3324 : 90003744 : access_failure_info::get_any_accessor (bool const_p) const
3325 : : {
3326 : 90003744 : if (!was_inaccessible_p ())
3327 : : return NULL_TREE;
3328 : :
3329 : 412 : tree accessor
3330 : 412 : = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3331 : 412 : if (!accessor)
3332 : : return NULL_TREE;
3333 : :
3334 : : /* The accessor must itself be accessible for it to be a reasonable
3335 : : suggestion. */
3336 : 161 : if (!accessible_p (m_basetype_path, accessor, true))
3337 : : return NULL_TREE;
3338 : :
3339 : : return accessor;
3340 : : }
3341 : :
3342 : : /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3343 : : replacing the primary location in RICHLOC with "accessor()". */
3344 : :
3345 : : void
3346 : 140 : access_failure_info::add_fixit_hint (rich_location *richloc,
3347 : : tree accessor_decl)
3348 : : {
3349 : 140 : pretty_printer pp;
3350 : 140 : pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3351 : 140 : pp_string (&pp, "()");
3352 : 140 : richloc->add_fixit_replace (pp_formatted_text (&pp));
3353 : 140 : }
3354 : :
3355 : : /* If an access failure was recorded, then attempt to locate an
3356 : : accessor function for the pertinent field, and if one is
3357 : : available, add a note and fix-it hint suggesting using it. */
3358 : :
3359 : : void
3360 : 90003674 : access_failure_info::maybe_suggest_accessor (bool const_p) const
3361 : : {
3362 : 90003674 : tree accessor = get_any_accessor (const_p);
3363 : 90003674 : if (accessor == NULL_TREE)
3364 : 90003576 : return;
3365 : 98 : rich_location richloc (line_table, input_location);
3366 : 98 : add_fixit_hint (&richloc, accessor);
3367 : 98 : inform (&richloc, "field %q#D can be accessed via %q#D",
3368 : 98 : m_diag_decl, accessor);
3369 : 98 : }
3370 : :
3371 : : /* Subroutine of finish_class_member_access_expr.
3372 : : Issue an error about NAME not being a member of ACCESS_PATH (or
3373 : : OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3374 : : names. */
3375 : :
3376 : : static void
3377 : 308 : complain_about_unrecognized_member (tree access_path, tree name,
3378 : : tree object_type)
3379 : : {
3380 : : /* Attempt to provide a hint about misspelled names. */
3381 : 308 : tree guessed_id = lookup_member_fuzzy (access_path, name,
3382 : : /*want_type=*/false);
3383 : 308 : if (guessed_id == NULL_TREE)
3384 : : {
3385 : : /* No hint. */
3386 : 201 : error ("%q#T has no member named %qE",
3387 : 201 : TREE_CODE (access_path) == TREE_BINFO
3388 : 6 : ? TREE_TYPE (access_path) : object_type, name);
3389 : 201 : return;
3390 : : }
3391 : :
3392 : 107 : location_t bogus_component_loc = input_location;
3393 : 107 : gcc_rich_location rich_loc (bogus_component_loc);
3394 : :
3395 : : /* Check that the guessed name is accessible along access_path. */
3396 : 107 : access_failure_info afi;
3397 : 107 : lookup_member (access_path, guessed_id, /*protect=*/1,
3398 : : /*want_type=*/false, /*complain=*/false,
3399 : : &afi);
3400 : 107 : if (afi.was_inaccessible_p ())
3401 : : {
3402 : 70 : tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3403 : 70 : if (accessor)
3404 : : {
3405 : : /* The guessed name isn't directly accessible, but can be accessed
3406 : : via an accessor member function. */
3407 : 42 : afi.add_fixit_hint (&rich_loc, accessor);
3408 : 42 : error_at (&rich_loc,
3409 : : "%q#T has no member named %qE;"
3410 : : " did you mean %q#D? (accessible via %q#D)",
3411 : 42 : TREE_CODE (access_path) == TREE_BINFO
3412 : 0 : ? TREE_TYPE (access_path) : object_type,
3413 : : name, afi.get_diag_decl (), accessor);
3414 : : }
3415 : : else
3416 : : {
3417 : : /* The guessed name isn't directly accessible, and no accessor
3418 : : member function could be found. */
3419 : 28 : auto_diagnostic_group d;
3420 : 28 : error_at (&rich_loc,
3421 : : "%q#T has no member named %qE;"
3422 : : " did you mean %q#D? (not accessible from this context)",
3423 : 28 : TREE_CODE (access_path) == TREE_BINFO
3424 : 0 : ? TREE_TYPE (access_path) : object_type,
3425 : : name, afi.get_diag_decl ());
3426 : 28 : complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3427 : : afi.get_diag_decl (), false, ak_none);
3428 : 28 : }
3429 : : }
3430 : : else
3431 : : {
3432 : : /* The guessed name is directly accessible; suggest it. */
3433 : 37 : rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3434 : : guessed_id);
3435 : 37 : error_at (&rich_loc,
3436 : : "%q#T has no member named %qE;"
3437 : : " did you mean %qE?",
3438 : 37 : TREE_CODE (access_path) == TREE_BINFO
3439 : 0 : ? TREE_TYPE (access_path) : object_type,
3440 : : name, guessed_id);
3441 : : }
3442 : 107 : }
3443 : :
3444 : : /* This function is called by the parser to process a class member
3445 : : access expression of the form OBJECT.NAME. NAME is a node used by
3446 : : the parser to represent a name; it is not yet a DECL. It may,
3447 : : however, be a BASELINK where the BASELINK_FUNCTIONS is a
3448 : : TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3449 : : there is no reason to do the lookup twice, so the parser keeps the
3450 : : BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3451 : : be a template via the use of the "A::template B" syntax. */
3452 : :
3453 : : tree
3454 : 176351725 : finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3455 : : tsubst_flags_t complain)
3456 : : {
3457 : 176351725 : tree expr;
3458 : 176351725 : tree object_type;
3459 : 176351725 : tree member;
3460 : 176351725 : tree access_path = NULL_TREE;
3461 : 176351725 : tree orig_object = object;
3462 : 176351725 : tree orig_name = name;
3463 : :
3464 : 176351725 : if (object == error_mark_node || name == error_mark_node)
3465 : : return error_mark_node;
3466 : :
3467 : : /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3468 : 176351264 : if (!objc_is_public (object, name))
3469 : 0 : return error_mark_node;
3470 : :
3471 : 176351264 : object_type = TREE_TYPE (object);
3472 : :
3473 : 176351264 : if (processing_template_decl)
3474 : : {
3475 : 146213065 : if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3476 : 146213065 : type_dependent_object_expression_p (object)
3477 : : /* If NAME is "f<args>", where either 'f' or 'args' is
3478 : : dependent, then the expression is dependent. */
3479 : 70513398 : || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3480 : 644316 : && dependent_template_id_p (TREE_OPERAND (name, 0),
3481 : 644316 : TREE_OPERAND (name, 1)))
3482 : : /* If NAME is "T::X" where "T" is dependent, then the
3483 : : expression is dependent. */
3484 : 70183109 : || (TREE_CODE (name) == SCOPE_REF
3485 : 111805 : && TYPE_P (TREE_OPERAND (name, 0))
3486 : 111805 : && dependent_scope_p (TREE_OPERAND (name, 0)))
3487 : : /* If NAME is operator T where "T" is dependent, we can't
3488 : : lookup until we instantiate the T. */
3489 : 216284583 : || (TREE_CODE (name) == IDENTIFIER_NODE
3490 : 69427404 : && IDENTIFIER_CONV_OP_P (name)
3491 : 44 : && dependent_type_p (TREE_TYPE (name))))
3492 : : {
3493 : 90324131 : dependent:
3494 : 90324131 : return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3495 : 90324131 : orig_object, orig_name, NULL_TREE);
3496 : : }
3497 : : }
3498 : 30138199 : else if (c_dialect_objc ()
3499 : 100209679 : && identifier_p (name)
3500 : 30138199 : && (expr = objc_maybe_build_component_ref (object, name)))
3501 : : return expr;
3502 : :
3503 : : /* [expr.ref]
3504 : :
3505 : : The type of the first expression shall be "class object" (of a
3506 : : complete type). */
3507 : 100209679 : if (!currently_open_class (object_type)
3508 : 100209679 : && !complete_type_or_maybe_complain (object_type, object, complain))
3509 : 86 : return error_mark_node;
3510 : 100209593 : if (!CLASS_TYPE_P (object_type))
3511 : : {
3512 : 117110 : if (complain & tf_error)
3513 : : {
3514 : 118 : if (INDIRECT_TYPE_P (object_type)
3515 : 118 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
3516 : 21 : error ("request for member %qD in %qE, which is of pointer "
3517 : : "type %qT (maybe you meant to use %<->%> ?)",
3518 : : name, object.get_value (), object_type);
3519 : : else
3520 : 97 : error ("request for member %qD in %qE, which is of non-class "
3521 : : "type %qT", name, object.get_value (), object_type);
3522 : : }
3523 : 117110 : return error_mark_node;
3524 : : }
3525 : :
3526 : 100092483 : if (BASELINK_P (name))
3527 : : /* A member function that has already been looked up. */
3528 : : member = name;
3529 : : else
3530 : : {
3531 : 90220778 : bool is_template_id = false;
3532 : 90220778 : tree template_args = NULL_TREE;
3533 : 90220778 : tree scope = NULL_TREE;
3534 : :
3535 : 90220778 : access_path = object_type;
3536 : :
3537 : 90220778 : if (TREE_CODE (name) == SCOPE_REF)
3538 : : {
3539 : : /* A qualified name. The qualifying class or namespace `S'
3540 : : has already been looked up; it is either a TYPE or a
3541 : : NAMESPACE_DECL. */
3542 : 1786 : scope = TREE_OPERAND (name, 0);
3543 : 1786 : name = TREE_OPERAND (name, 1);
3544 : :
3545 : : /* If SCOPE is a namespace, then the qualified name does not
3546 : : name a member of OBJECT_TYPE. */
3547 : 1786 : if (TREE_CODE (scope) == NAMESPACE_DECL)
3548 : : {
3549 : 0 : if (complain & tf_error)
3550 : 0 : error ("%<%D::%D%> is not a member of %qT",
3551 : : scope, name, object_type);
3552 : 0 : return error_mark_node;
3553 : : }
3554 : : }
3555 : :
3556 : 90220778 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3557 : : {
3558 : 483704 : is_template_id = true;
3559 : 483704 : template_args = TREE_OPERAND (name, 1);
3560 : 483704 : name = TREE_OPERAND (name, 0);
3561 : :
3562 : 918349 : if (!identifier_p (name))
3563 : 434663 : name = OVL_NAME (name);
3564 : : }
3565 : :
3566 : 90220778 : if (scope)
3567 : : {
3568 : 1786 : if (TREE_CODE (scope) == ENUMERAL_TYPE)
3569 : : {
3570 : 12 : gcc_assert (!is_template_id);
3571 : : /* Looking up a member enumerator (c++/56793). */
3572 : 24 : if (!TYPE_CLASS_SCOPE_P (scope)
3573 : 21 : || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3574 : : {
3575 : 3 : if (complain & tf_error)
3576 : 3 : error ("%<%D::%D%> is not a member of %qT",
3577 : : scope, name, object_type);
3578 : 3 : return error_mark_node;
3579 : : }
3580 : 9 : tree val = lookup_enumerator (scope, name);
3581 : 9 : if (!val)
3582 : : {
3583 : 6 : if (complain & tf_error)
3584 : 6 : error ("%qD is not a member of %qD",
3585 : : name, scope);
3586 : 6 : return error_mark_node;
3587 : : }
3588 : :
3589 : 3 : if (TREE_SIDE_EFFECTS (object))
3590 : 0 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3591 : 3 : return val;
3592 : : }
3593 : :
3594 : 1774 : gcc_assert (CLASS_TYPE_P (scope));
3595 : 1774 : gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3596 : :
3597 : 1774 : if (constructor_name_p (name, scope))
3598 : : {
3599 : 3 : if (complain & tf_error)
3600 : 3 : error ("cannot call constructor %<%T::%D%> directly",
3601 : : scope, name);
3602 : 3 : return error_mark_node;
3603 : : }
3604 : :
3605 : : /* NAME may refer to a static data member, in which case there is
3606 : : one copy of the data member that is shared by all the objects of
3607 : : the class. So NAME can be unambiguously referred to even if
3608 : : there are multiple indirect base classes containing NAME. */
3609 : 5313 : const base_access ba = [scope, name] ()
3610 : : {
3611 : 1771 : if (identifier_p (name))
3612 : : {
3613 : 1634 : tree m = lookup_member (scope, name, /*protect=*/0,
3614 : : /*want_type=*/false, tf_none);
3615 : 1634 : if (!m || shared_member_p (m))
3616 : 32 : return ba_any;
3617 : : }
3618 : : return ba_check;
3619 : 1771 : } ();
3620 : :
3621 : : /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3622 : 1771 : access_path = lookup_base (object_type, scope, ba, NULL, complain);
3623 : 1771 : if (access_path == error_mark_node)
3624 : : return error_mark_node;
3625 : 1759 : if (!access_path)
3626 : : {
3627 : 12 : if (any_dependent_bases_p (object_type))
3628 : 6 : goto dependent;
3629 : 6 : if (complain & tf_error)
3630 : 6 : error ("%qT is not a base of %qT", scope, object_type);
3631 : 6 : return error_mark_node;
3632 : : }
3633 : : }
3634 : :
3635 : 90220739 : if (TREE_CODE (name) == BIT_NOT_EXPR)
3636 : : {
3637 : 217065 : if (dependent_type_p (object_type))
3638 : : /* The destructor isn't declared yet. */
3639 : 35353 : goto dependent;
3640 : 181712 : member = lookup_destructor (object, scope, name, complain);
3641 : : }
3642 : : else
3643 : : {
3644 : : /* Look up the member. */
3645 : 90003674 : {
3646 : 90003674 : auto_diagnostic_group d;
3647 : 90003674 : access_failure_info afi;
3648 : 90003674 : if (processing_template_decl)
3649 : : /* Even though this class member access expression is at this
3650 : : point not dependent, the member itself may be dependent, and
3651 : : we must not potentially push a access check for a dependent
3652 : : member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3653 : : ahead of time here; we're going to redo this member lookup at
3654 : : instantiation time anyway. */
3655 : 69741583 : push_deferring_access_checks (dk_no_check);
3656 : 90003674 : member = lookup_member (access_path, name, /*protect=*/1,
3657 : : /*want_type=*/false, complain,
3658 : : &afi);
3659 : 90003674 : if (processing_template_decl)
3660 : 69741583 : pop_deferring_access_checks ();
3661 : 90003674 : afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3662 : 90003674 : }
3663 : 90003674 : if (member == NULL_TREE)
3664 : : {
3665 : 8538559 : if (dependentish_scope_p (object_type))
3666 : : /* Try again at instantiation time. */
3667 : 8517164 : goto dependent;
3668 : 21395 : if (complain & tf_error)
3669 : 308 : complain_about_unrecognized_member (access_path, name,
3670 : : object_type);
3671 : 21395 : return error_mark_node;
3672 : : }
3673 : 81465115 : if (member == error_mark_node)
3674 : : return error_mark_node;
3675 : 81465071 : if (DECL_P (member)
3676 : 81465071 : && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3677 : : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3678 : : wrong, so don't use it. */
3679 : 6 : goto dependent;
3680 : 81465065 : if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3681 : 5630017 : goto dependent;
3682 : : }
3683 : :
3684 : 76016760 : if (is_template_id)
3685 : : {
3686 : 483694 : tree templ = member;
3687 : :
3688 : 483694 : if (BASELINK_P (templ))
3689 : 483670 : member = lookup_template_function (templ, template_args);
3690 : 24 : else if (variable_template_p (templ))
3691 : 24 : member = (lookup_and_finish_template_variable
3692 : 24 : (templ, template_args, complain));
3693 : : else
3694 : : {
3695 : 0 : if (complain & tf_error)
3696 : 0 : error ("%qD is not a member template function", name);
3697 : 0 : return error_mark_node;
3698 : : }
3699 : : }
3700 : : }
3701 : :
3702 : 85888465 : if (TREE_UNAVAILABLE (member))
3703 : 30 : error_unavailable_use (member, NULL_TREE);
3704 : 85888435 : else if (TREE_DEPRECATED (member))
3705 : 30 : warn_deprecated_use (member, NULL_TREE);
3706 : :
3707 : 85888465 : if (template_p)
3708 : 9987 : check_template_keyword (member);
3709 : :
3710 : 85888465 : expr = build_class_member_access_expr (object, member, access_path,
3711 : : /*preserve_reference=*/false,
3712 : : complain);
3713 : 85888465 : if (processing_template_decl && expr != error_mark_node)
3714 : : {
3715 : 55888858 : if (BASELINK_P (member))
3716 : : {
3717 : 41586872 : if (TREE_CODE (orig_name) == SCOPE_REF)
3718 : 169 : BASELINK_QUALIFIED_P (member) = 1;
3719 : : orig_name = member;
3720 : : }
3721 : 55888858 : return build_min_non_dep (COMPONENT_REF, expr,
3722 : : orig_object, orig_name,
3723 : 55888858 : NULL_TREE);
3724 : : }
3725 : :
3726 : : return expr;
3727 : : }
3728 : :
3729 : : /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3730 : : type. */
3731 : :
3732 : : tree
3733 : 181752 : build_simple_component_ref (tree object, tree member)
3734 : : {
3735 : 181752 : tree type = cp_build_qualified_type (TREE_TYPE (member),
3736 : 181752 : cp_type_quals (TREE_TYPE (object)));
3737 : 181752 : return build3_loc (input_location,
3738 : : COMPONENT_REF, type,
3739 : 181752 : object, member, NULL_TREE);
3740 : : }
3741 : :
3742 : : /* Return an expression for the MEMBER_NAME field in the internal
3743 : : representation of PTRMEM, a pointer-to-member function. (Each
3744 : : pointer-to-member function type gets its own RECORD_TYPE so it is
3745 : : more convenient to access the fields by name than by FIELD_DECL.)
3746 : : This routine converts the NAME to a FIELD_DECL and then creates the
3747 : : node for the complete expression. */
3748 : :
3749 : : tree
3750 : 181656 : build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3751 : : {
3752 : 181656 : tree ptrmem_type;
3753 : 181656 : tree member;
3754 : :
3755 : 181656 : if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3756 : : {
3757 : 198 : for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3758 : 78 : if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3759 : 60 : return e.value;
3760 : 0 : gcc_unreachable ();
3761 : : }
3762 : :
3763 : : /* This code is a stripped down version of
3764 : : build_class_member_access_expr. It does not work to use that
3765 : : routine directly because it expects the object to be of class
3766 : : type. */
3767 : 181596 : ptrmem_type = TREE_TYPE (ptrmem);
3768 : 181596 : gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3769 : 272294 : for (member = TYPE_FIELDS (ptrmem_type); member;
3770 : 90698 : member = DECL_CHAIN (member))
3771 : 272294 : if (DECL_NAME (member) == member_name)
3772 : : break;
3773 : 181596 : return build_simple_component_ref (ptrmem, member);
3774 : : }
3775 : :
3776 : : /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3777 : : and for any other relevant operator. */
3778 : :
3779 : : static tree
3780 : 123078772 : op_unqualified_lookup (tree_code code, bool is_assign)
3781 : : {
3782 : 123078772 : tree lookups = NULL_TREE;
3783 : :
3784 : 123078772 : if (cxx_dialect >= cxx20 && !is_assign)
3785 : : {
3786 : 42855719 : if (code == NE_EXPR)
3787 : : {
3788 : : /* != can get rewritten in terms of ==. */
3789 : 2799824 : tree fnname = ovl_op_identifier (false, EQ_EXPR);
3790 : 2799824 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3791 : 2770750 : lookups = tree_cons (fnname, fns, lookups);
3792 : : }
3793 : 40055895 : else if (code == GT_EXPR || code == LE_EXPR
3794 : 40055895 : || code == LT_EXPR || code == GE_EXPR)
3795 : : {
3796 : : /* These can get rewritten in terms of <=>. */
3797 : 3790732 : tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3798 : 3790732 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3799 : 3529902 : lookups = tree_cons (fnname, fns, lookups);
3800 : : }
3801 : : }
3802 : :
3803 : 123078772 : tree fnname = ovl_op_identifier (is_assign, code);
3804 : 123078772 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3805 : 67850703 : lookups = tree_cons (fnname, fns, lookups);
3806 : :
3807 : 123078772 : if (lookups)
3808 : : return lookups;
3809 : : else
3810 : 55149321 : return build_tree_list (NULL_TREE, NULL_TREE);
3811 : : }
3812 : :
3813 : : /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3814 : : the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3815 : : name lookup for the given operator. */
3816 : :
3817 : : tree
3818 : 132402097 : build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3819 : : {
3820 : 132402097 : if (lookups)
3821 : : /* We're partially instantiating a dependent operator expression, and
3822 : : LOOKUPS is the result of phase 1 name lookup that we performed
3823 : : earlier at template definition time, so just reuse the corresponding
3824 : : DEPENDENT_OPERATOR_TYPE. */
3825 : 9323325 : return TREE_TYPE (lookups);
3826 : :
3827 : : /* Otherwise we're processing a dependent operator expression at template
3828 : : definition time, so perform phase 1 name lookup now. */
3829 : 123078772 : lookups = op_unqualified_lookup (code, is_assign);
3830 : :
3831 : 123078772 : tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3832 : 123078772 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3833 : 123078772 : TREE_TYPE (lookups) = type;
3834 : 123078772 : return type;
3835 : : }
3836 : :
3837 : : /* Given an expression PTR for a pointer, return an expression
3838 : : for the value pointed to.
3839 : : ERRORSTRING is the name of the operator to appear in error messages.
3840 : :
3841 : : This function may need to overload OPERATOR_FNNAME.
3842 : : Must also handle REFERENCE_TYPEs for C++. */
3843 : :
3844 : : tree
3845 : 43207635 : build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3846 : : tree lookups, tsubst_flags_t complain)
3847 : : {
3848 : 43207635 : tree orig_expr = expr;
3849 : 43207635 : tree rval;
3850 : 43207635 : tree overload = NULL_TREE;
3851 : :
3852 : 43207635 : if (processing_template_decl)
3853 : : {
3854 : : /* Retain the type if we know the operand is a pointer. */
3855 : 24642536 : if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3856 : : {
3857 : 12812316 : if (expr == current_class_ptr
3858 : 12812316 : || (TREE_CODE (expr) == NOP_EXPR
3859 : 8080085 : && TREE_OPERAND (expr, 0) == current_class_ptr
3860 : 8068631 : && (same_type_ignoring_top_level_qualifiers_p
3861 : 8068631 : (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3862 : 8068630 : return current_class_ref;
3863 : 4743686 : return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3864 : : }
3865 : 11830220 : if (type_dependent_expression_p (expr))
3866 : : {
3867 : 11678482 : expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3868 : 11678482 : TREE_TYPE (expr)
3869 : 11678482 : = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3870 : 11678482 : return expr;
3871 : : }
3872 : : }
3873 : :
3874 : 18716837 : rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3875 : : NULL_TREE, NULL_TREE, lookups,
3876 : : &overload, complain);
3877 : 18716837 : if (!rval)
3878 : 0 : rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3879 : :
3880 : 18716837 : if (processing_template_decl && rval != error_mark_node)
3881 : : {
3882 : 149905 : if (overload != NULL_TREE)
3883 : 149874 : return (build_min_non_dep_op_overload
3884 : 149874 : (INDIRECT_REF, rval, overload, orig_expr));
3885 : :
3886 : 31 : return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3887 : : }
3888 : : else
3889 : : return rval;
3890 : : }
3891 : :
3892 : : /* Like c-family strict_aliasing_warning, but don't warn for dependent
3893 : : types or expressions. */
3894 : :
3895 : : static bool
3896 : 598703 : cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3897 : : {
3898 : 598703 : if (processing_template_decl)
3899 : : {
3900 : 42527 : tree e = expr;
3901 : 42527 : STRIP_NOPS (e);
3902 : 42527 : if (dependent_type_p (type) || type_dependent_expression_p (e))
3903 : 6909 : return false;
3904 : : }
3905 : 591794 : return strict_aliasing_warning (loc, type, expr);
3906 : : }
3907 : :
3908 : : /* The implementation of the above, and of indirection implied by other
3909 : : constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3910 : :
3911 : : static tree
3912 : 312702345 : cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3913 : : tsubst_flags_t complain, bool do_fold)
3914 : : {
3915 : 312702345 : tree pointer, type;
3916 : :
3917 : : /* RO_NULL should only be used with the folding entry points below, not
3918 : : cp_build_indirect_ref. */
3919 : 312702345 : gcc_checking_assert (errorstring != RO_NULL || do_fold);
3920 : :
3921 : 312702345 : if (ptr == current_class_ptr
3922 : 312702345 : || (TREE_CODE (ptr) == NOP_EXPR
3923 : 32548975 : && TREE_OPERAND (ptr, 0) == current_class_ptr
3924 : 18120274 : && (same_type_ignoring_top_level_qualifiers_p
3925 : 18120274 : (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3926 : 19587129 : return current_class_ref;
3927 : :
3928 : 293115216 : pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3929 : 293115216 : ? ptr : decay_conversion (ptr, complain));
3930 : 293115216 : if (pointer == error_mark_node)
3931 : : return error_mark_node;
3932 : :
3933 : 293109368 : type = TREE_TYPE (pointer);
3934 : :
3935 : 293109368 : if (INDIRECT_TYPE_P (type))
3936 : : {
3937 : : /* [expr.unary.op]
3938 : :
3939 : : If the type of the expression is "pointer to T," the type
3940 : : of the result is "T." */
3941 : 293108973 : tree t = TREE_TYPE (type);
3942 : :
3943 : 277489905 : if ((CONVERT_EXPR_P (ptr)
3944 : 207134855 : || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3945 : 365477387 : && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3946 : : {
3947 : : /* If a warning is issued, mark it to avoid duplicates from
3948 : : the backend. This only needs to be done at
3949 : : warn_strict_aliasing > 2. */
3950 : 62937878 : if (warn_strict_aliasing > 2
3951 : 63454668 : && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3952 : 516790 : type, TREE_OPERAND (ptr, 0)))
3953 : 6 : suppress_warning (ptr, OPT_Wstrict_aliasing);
3954 : : }
3955 : :
3956 : 293108973 : if (VOID_TYPE_P (t))
3957 : : {
3958 : : /* A pointer to incomplete type (other than cv void) can be
3959 : : dereferenced [expr.unary.op]/1 */
3960 : 70 : if (complain & tf_error)
3961 : 19 : error_at (loc, "%qT is not a pointer-to-object type", type);
3962 : 70 : return error_mark_node;
3963 : : }
3964 : 283561792 : else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3965 : 298994596 : && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3966 : : /* The POINTER was something like `&x'. We simplify `*&x' to
3967 : : `x'. This can change the value category: '*&TARGET_EXPR'
3968 : : is an lvalue and folding it into 'TARGET_EXPR' turns it into
3969 : : a prvalue of class type. */
3970 : 5885664 : return TREE_OPERAND (pointer, 0);
3971 : : else
3972 : : {
3973 : 287223239 : tree ref = build1 (INDIRECT_REF, t, pointer);
3974 : :
3975 : : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3976 : : so that we get the proper error message if the result is used
3977 : : to assign to. Also, &* is supposed to be a no-op. */
3978 : 287223239 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3979 : 287223239 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3980 : 287223239 : TREE_SIDE_EFFECTS (ref)
3981 : 287223239 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3982 : 287223239 : return ref;
3983 : : }
3984 : : }
3985 : 395 : else if (!(complain & tf_error))
3986 : : /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3987 : : ;
3988 : : /* `pointer' won't be an error_mark_node if we were given a
3989 : : pointer to member, so it's cool to check for this here. */
3990 : 39 : else if (TYPE_PTRMEM_P (type))
3991 : 15 : switch (errorstring)
3992 : : {
3993 : 0 : case RO_ARRAY_INDEXING:
3994 : 0 : error_at (loc,
3995 : : "invalid use of array indexing on pointer to member");
3996 : 0 : break;
3997 : 12 : case RO_UNARY_STAR:
3998 : 12 : error_at (loc, "invalid use of unary %<*%> on pointer to member");
3999 : 12 : break;
4000 : 0 : case RO_IMPLICIT_CONVERSION:
4001 : 0 : error_at (loc, "invalid use of implicit conversion on pointer "
4002 : : "to member");
4003 : 0 : break;
4004 : 3 : case RO_ARROW_STAR:
4005 : 3 : error_at (loc, "left hand operand of %<->*%> must be a pointer to "
4006 : : "class, but is a pointer to member of type %qT", type);
4007 : 3 : break;
4008 : 0 : default:
4009 : 0 : gcc_unreachable ();
4010 : : }
4011 : 24 : else if (pointer != error_mark_node)
4012 : 24 : invalid_indirection_error (loc, type, errorstring);
4013 : :
4014 : 395 : return error_mark_node;
4015 : : }
4016 : :
4017 : : /* Entry point used by c-common, which expects folding. */
4018 : :
4019 : : tree
4020 : 22867 : build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
4021 : : {
4022 : 22867 : return cp_build_indirect_ref_1 (loc, ptr, errorstring,
4023 : 22867 : tf_warning_or_error, true);
4024 : : }
4025 : :
4026 : : /* Entry point used by internal indirection needs that don't correspond to any
4027 : : syntactic construct. */
4028 : :
4029 : : tree
4030 : 286977195 : cp_build_fold_indirect_ref (tree pointer)
4031 : : {
4032 : 286977195 : return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
4033 : 286977195 : tf_warning_or_error, true);
4034 : : }
4035 : :
4036 : : /* Entry point used by indirection needs that correspond to some syntactic
4037 : : construct. */
4038 : :
4039 : : tree
4040 : 25702283 : cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
4041 : : tsubst_flags_t complain)
4042 : : {
4043 : 25702283 : return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
4044 : : }
4045 : :
4046 : : /* This handles expressions of the form "a[i]", which denotes
4047 : : an array reference.
4048 : :
4049 : : This is logically equivalent in C to *(a+i), but we may do it differently.
4050 : : If A is a variable or a member, we generate a primitive ARRAY_REF.
4051 : : This avoids forcing the array out of registers, and can work on
4052 : : arrays that are not lvalues (for example, members of structures returned
4053 : : by functions).
4054 : :
4055 : : If INDEX is of some user-defined type, it must be converted to
4056 : : integer type. Otherwise, to make a compatible PLUS_EXPR, it
4057 : : will inherit the type of the array, which will be some pointer type.
4058 : :
4059 : : LOC is the location to use in building the array reference. */
4060 : :
4061 : : tree
4062 : 5428379 : cp_build_array_ref (location_t loc, tree array, tree idx,
4063 : : tsubst_flags_t complain)
4064 : : {
4065 : 5428379 : tree ret;
4066 : :
4067 : 5428379 : if (idx == 0)
4068 : : {
4069 : 0 : if (complain & tf_error)
4070 : 0 : error_at (loc, "subscript missing in array reference");
4071 : 0 : return error_mark_node;
4072 : : }
4073 : :
4074 : 5428379 : if (TREE_TYPE (array) == error_mark_node
4075 : 5428379 : || TREE_TYPE (idx) == error_mark_node)
4076 : : return error_mark_node;
4077 : :
4078 : : /* 0[array] */
4079 : 5428379 : if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE)
4080 : : {
4081 : 10 : std::swap (array, idx);
4082 : :
4083 : 10 : tree first = NULL_TREE;
4084 : 10 : if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array))
4085 : 9 : idx = first = save_expr (idx);
4086 : 10 : ret = cp_build_array_ref (loc, array, idx, complain);
4087 : :
4088 : 10 : if (first)
4089 : 9 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4090 : 10 : return ret;
4091 : : }
4092 : :
4093 : : /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
4094 : : inside it. */
4095 : 5428369 : switch (TREE_CODE (array))
4096 : : {
4097 : 28 : case COMPOUND_EXPR:
4098 : 28 : {
4099 : 28 : tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
4100 : : complain);
4101 : 28 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
4102 : 28 : TREE_OPERAND (array, 0), value);
4103 : 28 : SET_EXPR_LOCATION (ret, loc);
4104 : 28 : return ret;
4105 : : }
4106 : :
4107 : 64 : case COND_EXPR:
4108 : 64 : tree op0, op1, op2;
4109 : 64 : op0 = TREE_OPERAND (array, 0);
4110 : 64 : op1 = TREE_OPERAND (array, 1);
4111 : 64 : op2 = TREE_OPERAND (array, 2);
4112 : 64 : if (TREE_SIDE_EFFECTS (idx) || !tree_invariant_p (idx))
4113 : : {
4114 : : /* If idx could possibly have some SAVE_EXPRs, turning
4115 : : (op0 ? op1 : op2)[idx] into
4116 : : op0 ? op1[idx] : op2[idx] can lead into temporaries
4117 : : initialized in one conditional path and uninitialized
4118 : : uses of them in the other path.
4119 : : And if idx is a really large expression, evaluating it
4120 : : twice is also not optimal.
4121 : : On the other side, op0 must be sequenced before evaluation
4122 : : of op1 and op2 and for C++17 op0, op1 and op2 must be
4123 : : sequenced before idx.
4124 : : If idx is INTEGER_CST, we can just do the optimization
4125 : : without any SAVE_EXPRs, if op1 and op2 are both ARRAY_TYPE
4126 : : VAR_DECLs or COMPONENT_REFs thereof (so their address
4127 : : is constant or relative to frame), optimize into
4128 : : (SAVE_EXPR <op0>, SAVE_EXPR <idx>, SAVE_EXPR <op0>)
4129 : : ? op1[SAVE_EXPR <idx>] : op2[SAVE_EXPR <idx>]
4130 : : Otherwise avoid this optimization. */
4131 : 49 : if (flag_strong_eval_order == 2)
4132 : : {
4133 : 33 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4134 : : {
4135 : 24 : if (!address_invariant_p (op1) || !address_invariant_p (op2))
4136 : : {
4137 : : /* Force default conversion on array if
4138 : : we can't optimize this and array is ARRAY_TYPE
4139 : : COND_EXPR, we can't leave COND_EXPRs with
4140 : : ARRAY_TYPE in the IL. */
4141 : 8 : array = cp_default_conversion (array, complain);
4142 : 8 : if (error_operand_p (array))
4143 : 0 : return error_mark_node;
4144 : : break;
4145 : : }
4146 : : }
4147 : 16 : else if (!POINTER_TYPE_P (TREE_TYPE (array))
4148 : 2 : || !tree_invariant_p (op1)
4149 : 11 : || !tree_invariant_p (op2))
4150 : : break;
4151 : : }
4152 : 34 : if (TREE_SIDE_EFFECTS (idx))
4153 : : {
4154 : 12 : idx = save_expr (idx);
4155 : 12 : op0 = save_expr (op0);
4156 : 12 : tree tem = build_compound_expr (loc, op0, idx);
4157 : 12 : op0 = build_compound_expr (loc, tem, op0);
4158 : : }
4159 : : }
4160 : 49 : op1 = cp_build_array_ref (loc, op1, idx, complain);
4161 : 49 : op2 = cp_build_array_ref (loc, op2, idx, complain);
4162 : 49 : ret = build_conditional_expr (loc, op0, op1, op2, complain);
4163 : 49 : protected_set_expr_location (ret, loc);
4164 : 49 : return ret;
4165 : :
4166 : : default:
4167 : : break;
4168 : : }
4169 : :
4170 : 5428292 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
4171 : :
4172 : 5428292 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4173 : : {
4174 : 1698981 : tree rval, type;
4175 : :
4176 : 1698981 : warn_array_subscript_with_type_char (loc, idx);
4177 : :
4178 : 1698981 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
4179 : : {
4180 : 3 : if (complain & tf_error)
4181 : 3 : error_at (loc, "array subscript is not an integer");
4182 : 3 : return error_mark_node;
4183 : : }
4184 : :
4185 : : /* Apply integral promotions *after* noticing character types.
4186 : : (It is unclear why we do these promotions -- the standard
4187 : : does not say that we should. In fact, the natural thing would
4188 : : seem to be to convert IDX to ptrdiff_t; we're performing
4189 : : pointer arithmetic.) */
4190 : 1698978 : idx = cp_perform_integral_promotions (idx, complain);
4191 : :
4192 : 1698978 : idx = maybe_fold_non_dependent_expr (idx, complain);
4193 : :
4194 : : /* An array that is indexed by a non-constant
4195 : : cannot be stored in a register; we must be able to do
4196 : : address arithmetic on its address.
4197 : : Likewise an array of elements of variable size. */
4198 : 1698978 : if (TREE_CODE (idx) != INTEGER_CST
4199 : 1698978 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4200 : 841925 : && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4201 : : != INTEGER_CST)))
4202 : : {
4203 : 858313 : if (!cxx_mark_addressable (array, true))
4204 : 0 : return error_mark_node;
4205 : : }
4206 : :
4207 : : /* An array that is indexed by a constant value which is not within
4208 : : the array bounds cannot be stored in a register either; because we
4209 : : would get a crash in store_bit_field/extract_bit_field when trying
4210 : : to access a non-existent part of the register. */
4211 : 1698978 : if (TREE_CODE (idx) == INTEGER_CST
4212 : 841928 : && TYPE_DOMAIN (TREE_TYPE (array))
4213 : 2539520 : && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4214 : : {
4215 : 2842 : if (!cxx_mark_addressable (array))
4216 : 0 : return error_mark_node;
4217 : : }
4218 : :
4219 : : /* Note in C++ it is valid to subscript a `register' array, since
4220 : : it is valid to take the address of something with that
4221 : : storage specification. */
4222 : 1698978 : if (extra_warnings)
4223 : : {
4224 : 38194 : tree foo = array;
4225 : 58684 : while (TREE_CODE (foo) == COMPONENT_REF)
4226 : 20490 : foo = TREE_OPERAND (foo, 0);
4227 : 8 : if (VAR_P (foo) && DECL_REGISTER (foo)
4228 : 38194 : && (complain & tf_warning))
4229 : 0 : warning_at (loc, OPT_Wextra,
4230 : : "subscripting array declared %<register%>");
4231 : : }
4232 : :
4233 : 1698978 : type = TREE_TYPE (TREE_TYPE (array));
4234 : 1698978 : rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4235 : : /* Array ref is const/volatile if the array elements are
4236 : : or if the array is.. */
4237 : 5096934 : TREE_READONLY (rval)
4238 : 1698978 : |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4239 : 5096934 : TREE_SIDE_EFFECTS (rval)
4240 : 1698978 : |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4241 : 3397956 : TREE_THIS_VOLATILE (rval)
4242 : 1698978 : |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4243 : 1698978 : ret = require_complete_type (rval, complain);
4244 : 1698978 : protected_set_expr_location (ret, loc);
4245 : 1698978 : if (non_lvalue)
4246 : 6640 : ret = non_lvalue_loc (loc, ret);
4247 : 1698978 : return ret;
4248 : : }
4249 : :
4250 : 3729311 : {
4251 : 3729311 : tree ar = cp_default_conversion (array, complain);
4252 : 3729311 : tree ind = cp_default_conversion (idx, complain);
4253 : 3729311 : tree first = NULL_TREE;
4254 : :
4255 : 2306144 : if (!processing_template_decl && flag_strong_eval_order == 2
4256 : 5995172 : && TREE_SIDE_EFFECTS (ind))
4257 : 102309 : ar = first = save_expr (ar);
4258 : :
4259 : : /* Put the integer in IND to simplify error checking. */
4260 : 3729311 : if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4261 : 57 : std::swap (ar, ind);
4262 : :
4263 : 3729311 : if (ar == error_mark_node || ind == error_mark_node)
4264 : : return error_mark_node;
4265 : :
4266 : 3729311 : if (!TYPE_PTR_P (TREE_TYPE (ar)))
4267 : : {
4268 : 22 : if (complain & tf_error)
4269 : 3 : error_at (loc, "subscripted value is neither array nor pointer");
4270 : 22 : return error_mark_node;
4271 : : }
4272 : 3729289 : if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4273 : : {
4274 : 0 : if (complain & tf_error)
4275 : 0 : error_at (loc, "array subscript is not an integer");
4276 : 0 : return error_mark_node;
4277 : : }
4278 : :
4279 : 3729289 : warn_array_subscript_with_type_char (loc, idx);
4280 : :
4281 : 3729289 : ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4282 : 3729289 : if (first)
4283 : 102309 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4284 : 3729289 : ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4285 : 3729289 : protected_set_expr_location (ret, loc);
4286 : 3729289 : if (non_lvalue)
4287 : 0 : ret = non_lvalue_loc (loc, ret);
4288 : : return ret;
4289 : : }
4290 : : }
4291 : :
4292 : : /* Entry point for Obj-C++. */
4293 : :
4294 : : tree
4295 : 3606681 : build_array_ref (location_t loc, tree array, tree idx)
4296 : : {
4297 : 3606681 : return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4298 : : }
4299 : :
4300 : : /* Resolve a pointer to member function. INSTANCE is the object
4301 : : instance to use, if the member points to a virtual member.
4302 : :
4303 : : This used to avoid checking for virtual functions if basetype
4304 : : has no virtual functions, according to an earlier ANSI draft.
4305 : : With the final ISO C++ rules, such an optimization is
4306 : : incorrect: A pointer to a derived member can be static_cast
4307 : : to pointer-to-base-member, as long as the dynamic object
4308 : : later has the right member. So now we only do this optimization
4309 : : when we know the dynamic type of the object. */
4310 : :
4311 : : tree
4312 : 90595 : get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4313 : : tsubst_flags_t complain)
4314 : : {
4315 : 90595 : if (TREE_CODE (function) == OFFSET_REF)
4316 : 0 : function = TREE_OPERAND (function, 1);
4317 : :
4318 : 90595 : if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4319 : : {
4320 : 90595 : tree idx, delta, e1, e2, e3, vtbl;
4321 : 90595 : bool nonvirtual;
4322 : 90595 : tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4323 : 90595 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4324 : :
4325 : 90595 : tree instance_ptr = *instance_ptrptr;
4326 : 90595 : tree instance_save_expr = 0;
4327 : 90595 : if (instance_ptr == error_mark_node)
4328 : : {
4329 : 0 : if (TREE_CODE (function) == PTRMEM_CST)
4330 : : {
4331 : : /* Extracting the function address from a pmf is only
4332 : : allowed with -Wno-pmf-conversions. It only works for
4333 : : pmf constants. */
4334 : 0 : e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4335 : 0 : e1 = convert (fntype, e1);
4336 : 0 : return e1;
4337 : : }
4338 : : else
4339 : : {
4340 : 0 : if (complain & tf_error)
4341 : 0 : error ("object missing in use of %qE", function);
4342 : 0 : return error_mark_node;
4343 : : }
4344 : : }
4345 : :
4346 : : /* True if we know that the dynamic type of the object doesn't have
4347 : : virtual functions, so we can assume the PFN field is a pointer. */
4348 : 90595 : nonvirtual = (COMPLETE_TYPE_P (basetype)
4349 : 90547 : && !TYPE_POLYMORPHIC_P (basetype)
4350 : 121660 : && resolves_to_fixed_type_p (instance_ptr, 0));
4351 : :
4352 : : /* If we don't really have an object (i.e. in an ill-formed
4353 : : conversion from PMF to pointer), we can't resolve virtual
4354 : : functions anyway. */
4355 : 90343 : if (!nonvirtual && is_dummy_object (instance_ptr))
4356 : : nonvirtual = true;
4357 : :
4358 : : /* Use force_target_expr even when instance_ptr doesn't have
4359 : : side-effects, unless it is a simple decl or constant, so
4360 : : that we don't ubsan instrument the expression multiple times.
4361 : : Don't use save_expr, as save_expr can avoid building a SAVE_EXPR
4362 : : and building a SAVE_EXPR manually can be optimized away during
4363 : : cp_fold. See PR116449 and PR117259. */
4364 : 90595 : if (TREE_SIDE_EFFECTS (instance_ptr)
4365 : 90595 : || (!nonvirtual
4366 : 50541 : && !DECL_P (instance_ptr)
4367 : 50541 : && !TREE_CONSTANT (instance_ptr)))
4368 : 90085 : instance_ptr = instance_save_expr
4369 : 90085 : = get_internal_target_expr (instance_ptr);
4370 : :
4371 : : /* See above comment. */
4372 : 90595 : if (TREE_SIDE_EFFECTS (function)
4373 : 90595 : || (!nonvirtual
4374 : 70277 : && !DECL_P (function)
4375 : 70265 : && !TREE_CONSTANT (function)))
4376 : 90129 : function = get_internal_target_expr (function);
4377 : :
4378 : : /* Start by extracting all the information from the PMF itself. */
4379 : 90595 : e3 = pfn_from_ptrmemfunc (function);
4380 : 90595 : delta = delta_from_ptrmemfunc (function);
4381 : 90595 : idx = build1 (NOP_EXPR, vtable_index_type, e3);
4382 : 90595 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4383 : : {
4384 : 90595 : sanitize_code_type flag_sanitize_save;
4385 : 90595 : case ptrmemfunc_vbit_in_pfn:
4386 : 90595 : e1 = cp_build_binary_op (input_location,
4387 : : BIT_AND_EXPR, idx, integer_one_node,
4388 : : complain);
4389 : 90595 : idx = cp_build_binary_op (input_location,
4390 : : MINUS_EXPR, idx, integer_one_node,
4391 : : complain);
4392 : 90595 : if (idx == error_mark_node)
4393 : : return error_mark_node;
4394 : 90595 : break;
4395 : :
4396 : : case ptrmemfunc_vbit_in_delta:
4397 : : e1 = cp_build_binary_op (input_location,
4398 : : BIT_AND_EXPR, delta, integer_one_node,
4399 : : complain);
4400 : : /* Don't instrument the RSHIFT_EXPR we're about to create because
4401 : : we're going to use DELTA number of times, and that wouldn't play
4402 : : well with SAVE_EXPRs therein. */
4403 : : flag_sanitize_save = flag_sanitize;
4404 : : flag_sanitize = 0;
4405 : : delta = cp_build_binary_op (input_location,
4406 : : RSHIFT_EXPR, delta, integer_one_node,
4407 : : complain);
4408 : : flag_sanitize = flag_sanitize_save;
4409 : : if (delta == error_mark_node)
4410 : : return error_mark_node;
4411 : : break;
4412 : :
4413 : : default:
4414 : : gcc_unreachable ();
4415 : : }
4416 : :
4417 : 90595 : if (e1 == error_mark_node)
4418 : : return error_mark_node;
4419 : :
4420 : : /* Convert down to the right base before using the instance. A
4421 : : special case is that in a pointer to member of class C, C may
4422 : : be incomplete. In that case, the function will of course be
4423 : : a member of C, and no conversion is required. In fact,
4424 : : lookup_base will fail in that case, because incomplete
4425 : : classes do not have BINFOs. */
4426 : 90595 : if (!same_type_ignoring_top_level_qualifiers_p
4427 : 90595 : (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4428 : : {
4429 : 81 : basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4430 : : basetype, ba_check, NULL, complain);
4431 : 81 : instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4432 : : 1, complain);
4433 : 81 : if (instance_ptr == error_mark_node)
4434 : : return error_mark_node;
4435 : : }
4436 : : /* ...and then the delta in the PMF. */
4437 : 90595 : instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4438 : :
4439 : : /* Hand back the adjusted 'this' argument to our caller. */
4440 : 90595 : *instance_ptrptr = instance_ptr;
4441 : :
4442 : 90595 : if (nonvirtual)
4443 : : /* Now just return the pointer. */
4444 : : return e3;
4445 : :
4446 : : /* Next extract the vtable pointer from the object. */
4447 : 90325 : vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4448 : : instance_ptr);
4449 : 90325 : vtbl = cp_build_fold_indirect_ref (vtbl);
4450 : 90325 : if (vtbl == error_mark_node)
4451 : : return error_mark_node;
4452 : :
4453 : : /* Finally, extract the function pointer from the vtable. */
4454 : 90325 : e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4455 : 90325 : e2 = cp_build_fold_indirect_ref (e2);
4456 : 90325 : if (e2 == error_mark_node)
4457 : : return error_mark_node;
4458 : 90325 : TREE_CONSTANT (e2) = 1;
4459 : :
4460 : : /* When using function descriptors, the address of the
4461 : : vtable entry is treated as a function pointer. */
4462 : 90325 : if (TARGET_VTABLE_USES_DESCRIPTORS)
4463 : : e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4464 : : cp_build_addr_expr (e2, complain));
4465 : :
4466 : 90325 : e2 = fold_convert (TREE_TYPE (e3), e2);
4467 : 90325 : e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4468 : 90325 : if (e1 == error_mark_node)
4469 : : return error_mark_node;
4470 : :
4471 : : /* Make sure this doesn't get evaluated first inside one of the
4472 : : branches of the COND_EXPR. */
4473 : 90325 : if (instance_save_expr)
4474 : 90037 : e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4475 : : instance_save_expr, e1);
4476 : :
4477 : : function = e1;
4478 : : }
4479 : : return function;
4480 : : }
4481 : :
4482 : : /* Used by the C-common bits. */
4483 : : tree
4484 : 0 : build_function_call (location_t /*loc*/,
4485 : : tree function, tree params)
4486 : : {
4487 : 0 : return cp_build_function_call (function, params, tf_warning_or_error);
4488 : : }
4489 : :
4490 : : /* Used by the C-common bits. */
4491 : : tree
4492 : 241538 : build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4493 : : tree function, vec<tree, va_gc> *params,
4494 : : vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4495 : : {
4496 : 241538 : vec<tree, va_gc> *orig_params = params;
4497 : 241538 : tree ret = cp_build_function_call_vec (function, ¶ms,
4498 : : tf_warning_or_error, orig_function);
4499 : :
4500 : : /* cp_build_function_call_vec can reallocate PARAMS by adding
4501 : : default arguments. That should never happen here. Verify
4502 : : that. */
4503 : 241538 : gcc_assert (params == orig_params);
4504 : :
4505 : 241538 : return ret;
4506 : : }
4507 : :
4508 : : /* Build a function call using a tree list of arguments. */
4509 : :
4510 : : static tree
4511 : 0 : cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4512 : : {
4513 : 0 : tree ret;
4514 : :
4515 : 0 : releasing_vec vec;
4516 : 0 : for (; params != NULL_TREE; params = TREE_CHAIN (params))
4517 : 0 : vec_safe_push (vec, TREE_VALUE (params));
4518 : 0 : ret = cp_build_function_call_vec (function, &vec, complain);
4519 : 0 : return ret;
4520 : 0 : }
4521 : :
4522 : : /* Build a function call using varargs. */
4523 : :
4524 : : tree
4525 : 527040 : cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4526 : : {
4527 : 527040 : va_list args;
4528 : 527040 : tree ret, t;
4529 : :
4530 : 527040 : releasing_vec vec;
4531 : 527040 : va_start (args, complain);
4532 : 1301365 : for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4533 : 774325 : vec_safe_push (vec, t);
4534 : 527040 : va_end (args);
4535 : 527040 : ret = cp_build_function_call_vec (function, &vec, complain);
4536 : 527040 : return ret;
4537 : 527040 : }
4538 : :
4539 : : /* C++ implementation of callback for use when checking param types. */
4540 : :
4541 : : bool
4542 : 18 : cp_comp_parm_types (tree wanted_type, tree actual_type)
4543 : : {
4544 : 18 : if (TREE_CODE (wanted_type) == POINTER_TYPE
4545 : 18 : && TREE_CODE (actual_type) == POINTER_TYPE)
4546 : 15 : return same_or_base_type_p (TREE_TYPE (wanted_type),
4547 : : TREE_TYPE (actual_type));
4548 : : return false;
4549 : : }
4550 : :
4551 : : /* Build a function call using a vector of arguments.
4552 : : If FUNCTION is the result of resolving an overloaded target built-in,
4553 : : ORIG_FNDECL is the original function decl, otherwise it is null.
4554 : : PARAMS may be NULL if there are no parameters. This changes the
4555 : : contents of PARAMS. */
4556 : :
4557 : : tree
4558 : 4271091 : cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4559 : : tsubst_flags_t complain, tree orig_fndecl)
4560 : : {
4561 : 4271091 : tree fntype, fndecl;
4562 : 4271091 : int is_method;
4563 : 4271091 : tree original = function;
4564 : 4271091 : int nargs;
4565 : 4271091 : tree *argarray;
4566 : 4271091 : tree parm_types;
4567 : 4271091 : vec<tree, va_gc> *allocated = NULL;
4568 : 4271091 : tree ret;
4569 : :
4570 : : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4571 : : expressions, like those used for ObjC messenger dispatches. */
4572 : 4271091 : if (params != NULL && !vec_safe_is_empty (*params))
4573 : 1788582 : function = objc_rewrite_function_call (function, (**params)[0]);
4574 : :
4575 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4576 : : Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4577 : 4271091 : if (TREE_CODE (function) == NOP_EXPR
4578 : 4271091 : && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4579 : 15 : function = TREE_OPERAND (function, 0);
4580 : :
4581 : 4271091 : if (TREE_CODE (function) == FUNCTION_DECL)
4582 : : {
4583 : 1397531 : if (!mark_used (function, complain))
4584 : 24 : return error_mark_node;
4585 : 1397507 : fndecl = function;
4586 : :
4587 : : /* For target_version semantics, the function set cannot be called
4588 : : if there is no default version in scope. */
4589 : 1397507 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
4590 : : && !is_function_default_version (fndecl))
4591 : : {
4592 : : if (complain & tf_error)
4593 : : error ("no default version in scope");
4594 : : return error_mark_node;
4595 : : }
4596 : :
4597 : : /* Convert anything with function type to a pointer-to-function. */
4598 : 1397507 : if (DECL_MAIN_P (function))
4599 : : {
4600 : 0 : if (complain & tf_error)
4601 : 0 : pedwarn (input_location, OPT_Wpedantic,
4602 : : "ISO C++ forbids calling %<::main%> from within program");
4603 : : else
4604 : 0 : return error_mark_node;
4605 : : }
4606 : 1397507 : function = build_addr_func (function, complain);
4607 : : }
4608 : : else
4609 : : {
4610 : 2873560 : fndecl = NULL_TREE;
4611 : :
4612 : 2873560 : function = build_addr_func (function, complain);
4613 : : }
4614 : :
4615 : 4271067 : if (function == error_mark_node)
4616 : : return error_mark_node;
4617 : :
4618 : 4270965 : fntype = TREE_TYPE (function);
4619 : :
4620 : 4270965 : if (TYPE_PTRMEMFUNC_P (fntype))
4621 : : {
4622 : 15 : if (complain & tf_error)
4623 : 15 : error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4624 : : "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4625 : : original, original);
4626 : 15 : return error_mark_node;
4627 : : }
4628 : :
4629 : 8541900 : is_method = (TYPE_PTR_P (fntype)
4630 : 4270950 : && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4631 : :
4632 : 4270950 : if (!(TYPE_PTRFN_P (fntype)
4633 : 91935 : || is_method
4634 : 1413 : || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4635 : : {
4636 : 1413 : if (complain & tf_error)
4637 : : {
4638 : 174 : if (is_stub_object (original))
4639 : 3 : error_at (input_location,
4640 : : "%qT cannot be used as a function",
4641 : 3 : TREE_TYPE (original));
4642 : 171 : else if (!flag_diagnostics_show_caret)
4643 : 171 : error_at (input_location,
4644 : : "%qE cannot be used as a function", original);
4645 : 0 : else if (DECL_P (original))
4646 : 0 : error_at (input_location,
4647 : : "%qD cannot be used as a function", original);
4648 : : else
4649 : 0 : error_at (input_location,
4650 : : "expression cannot be used as a function");
4651 : : }
4652 : :
4653 : 1413 : return error_mark_node;
4654 : : }
4655 : :
4656 : : /* fntype now gets the type of function pointed to. */
4657 : 4269537 : fntype = TREE_TYPE (fntype);
4658 : 4269537 : parm_types = TYPE_ARG_TYPES (fntype);
4659 : :
4660 : 4269537 : if (params == NULL)
4661 : : {
4662 : 163450 : allocated = make_tree_vector ();
4663 : 163450 : params = &allocated;
4664 : : }
4665 : :
4666 : 4269537 : nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4667 : : complain);
4668 : 4269537 : if (nargs < 0)
4669 : 1652 : return error_mark_node;
4670 : :
4671 : 4267885 : argarray = (*params)->address ();
4672 : :
4673 : : /* Check for errors in format strings and inappropriately
4674 : : null parameters. */
4675 : 4267885 : bool warned_p
4676 : 4267885 : = ((complain & tf_warning)
4677 : 4267885 : && check_function_arguments (input_location, fndecl, fntype,
4678 : : nargs, argarray, NULL,
4679 : 4267885 : cp_comp_parm_types));
4680 : :
4681 : 4267885 : ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4682 : :
4683 : 4267885 : if (warned_p)
4684 : : {
4685 : 27 : tree c = extract_call_expr (ret);
4686 : 27 : if (TREE_CODE (c) == CALL_EXPR)
4687 : 27 : suppress_warning (c, OPT_Wnonnull);
4688 : : }
4689 : :
4690 : 4267885 : if (allocated != NULL)
4691 : 163450 : release_tree_vector (allocated);
4692 : :
4693 : : return ret;
4694 : : }
4695 : :
4696 : : /* Subroutine of convert_arguments.
4697 : : Print an error message about a wrong number of arguments. */
4698 : :
4699 : : static void
4700 : 162 : error_args_num (location_t loc, tree fndecl, bool too_many_p)
4701 : : {
4702 : 162 : if (fndecl)
4703 : : {
4704 : 126 : auto_diagnostic_group d;
4705 : 126 : if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4706 : : {
4707 : 0 : if (DECL_NAME (fndecl) == NULL_TREE
4708 : 0 : || (DECL_NAME (fndecl)
4709 : 0 : == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4710 : 0 : error_at (loc,
4711 : : too_many_p
4712 : : ? G_("too many arguments to constructor %q#D")
4713 : : : G_("too few arguments to constructor %q#D"),
4714 : : fndecl);
4715 : : else
4716 : 0 : error_at (loc,
4717 : : too_many_p
4718 : : ? G_("too many arguments to member function %q#D")
4719 : : : G_("too few arguments to member function %q#D"),
4720 : : fndecl);
4721 : : }
4722 : : else
4723 : 189 : error_at (loc,
4724 : : too_many_p
4725 : : ? G_("too many arguments to function %q#D")
4726 : : : G_("too few arguments to function %q#D"),
4727 : : fndecl);
4728 : 126 : if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4729 : 87 : inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4730 : 126 : }
4731 : : else
4732 : : {
4733 : 36 : if (c_dialect_objc () && objc_message_selector ())
4734 : 0 : error_at (loc,
4735 : : too_many_p
4736 : : ? G_("too many arguments to method %q#D")
4737 : : : G_("too few arguments to method %q#D"),
4738 : : objc_message_selector ());
4739 : : else
4740 : 57 : error_at (loc, too_many_p ? G_("too many arguments to function")
4741 : : : G_("too few arguments to function"));
4742 : : }
4743 : 162 : }
4744 : :
4745 : : /* Convert the actual parameter expressions in the list VALUES to the
4746 : : types in the list TYPELIST. The converted expressions are stored
4747 : : back in the VALUES vector.
4748 : : If parmdecls is exhausted, or when an element has NULL as its type,
4749 : : perform the default conversions.
4750 : :
4751 : : NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4752 : :
4753 : : This is also where warnings about wrong number of args are generated.
4754 : :
4755 : : Returns the actual number of arguments processed (which might be less
4756 : : than the length of the vector), or -1 on error.
4757 : :
4758 : : In C++, unspecified trailing parameters can be filled in with their
4759 : : default arguments, if such were specified. Do so here. */
4760 : :
4761 : : static int
4762 : 4269537 : convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4763 : : int flags, tsubst_flags_t complain)
4764 : : {
4765 : 4269537 : tree typetail;
4766 : 4269537 : unsigned int i;
4767 : :
4768 : : /* Argument passing is always copy-initialization. */
4769 : 4269537 : flags |= LOOKUP_ONLYCONVERTING;
4770 : :
4771 : 8287179 : for (i = 0, typetail = typelist;
4772 : 8287179 : i < vec_safe_length (*values);
4773 : : i++)
4774 : : {
4775 : 8037828 : tree type = typetail ? TREE_VALUE (typetail) : 0;
4776 : 4019137 : tree val = (**values)[i];
4777 : :
4778 : 4019137 : if (val == error_mark_node || type == error_mark_node)
4779 : : return -1;
4780 : :
4781 : 4019131 : if (type == void_type_node)
4782 : : {
4783 : 206 : if (complain & tf_error)
4784 : 78 : error_args_num (input_location, fndecl, /*too_many_p=*/true);
4785 : 206 : return -1;
4786 : : }
4787 : :
4788 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4789 : : Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4790 : 4018925 : if (TREE_CODE (val) == NOP_EXPR
4791 : 974689 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4792 : 4018943 : && (type == 0 || !TYPE_REF_P (type)))
4793 : 18 : val = TREE_OPERAND (val, 0);
4794 : :
4795 : 4018925 : if (type == 0 || !TYPE_REF_P (type))
4796 : : {
4797 : 3795401 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4798 : 3795401 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4799 : 10940 : val = decay_conversion (val, complain);
4800 : : }
4801 : :
4802 : 4018925 : if (val == error_mark_node)
4803 : : return -1;
4804 : :
4805 : 4018925 : if (type != 0)
4806 : : {
4807 : : /* Formal parm type is specified by a function prototype. */
4808 : 4018479 : tree parmval;
4809 : :
4810 : 4018479 : if (!COMPLETE_TYPE_P (complete_type (type)))
4811 : : {
4812 : 18 : if (complain & tf_error)
4813 : : {
4814 : 12 : location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4815 : 12 : if (fndecl)
4816 : : {
4817 : 12 : auto_diagnostic_group d;
4818 : 12 : error_at (loc,
4819 : : "parameter %P of %qD has incomplete type %qT",
4820 : : i, fndecl, type);
4821 : 12 : inform (get_fndecl_argument_location (fndecl, i),
4822 : : " declared here");
4823 : 12 : }
4824 : : else
4825 : 0 : error_at (loc, "parameter %P has incomplete type %qT", i,
4826 : : type);
4827 : : }
4828 : 18 : parmval = error_mark_node;
4829 : : }
4830 : : else
4831 : : {
4832 : 4018461 : parmval = convert_for_initialization
4833 : 4018461 : (NULL_TREE, type, val, flags,
4834 : : ICR_ARGPASS, fndecl, i, complain);
4835 : 4018461 : parmval = convert_for_arg_passing (type, parmval, complain);
4836 : : }
4837 : :
4838 : 4018479 : if (parmval == error_mark_node)
4839 : : return -1;
4840 : :
4841 : 4017196 : (**values)[i] = parmval;
4842 : : }
4843 : : else
4844 : : {
4845 : 446 : int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4846 : 21 : if (magic)
4847 : : {
4848 : : /* Don't truncate excess precision to the semantic type. */
4849 : 0 : if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4850 : 0 : val = TREE_OPERAND (val, 0);
4851 : : /* Don't do ellipsis conversion for __built_in_constant_p
4852 : : as this will result in spurious errors for non-trivial
4853 : : types. */
4854 : 0 : val = require_complete_type (val, complain);
4855 : : }
4856 : : else
4857 : 446 : val = convert_arg_to_ellipsis (val, complain);
4858 : :
4859 : 446 : (**values)[i] = val;
4860 : : }
4861 : :
4862 : 4017642 : if (typetail)
4863 : 4017196 : typetail = TREE_CHAIN (typetail);
4864 : : }
4865 : :
4866 : 4268042 : if (typetail != 0 && typetail != void_list_node)
4867 : : {
4868 : : /* See if there are default arguments that can be used. Because
4869 : : we hold default arguments in the FUNCTION_TYPE (which is so
4870 : : wrong), we can see default parameters here from deduced
4871 : : contexts (and via typeof) for indirect function calls.
4872 : : Fortunately we know whether we have a function decl to
4873 : : provide default arguments in a language conformant
4874 : : manner. */
4875 : 63 : if (fndecl && TREE_PURPOSE (typetail)
4876 : 160 : && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4877 : : {
4878 : 6 : for (; typetail != void_list_node; ++i)
4879 : : {
4880 : : /* After DR777, with explicit template args we can end up with a
4881 : : default argument followed by no default argument. */
4882 : 6 : if (!TREE_PURPOSE (typetail))
4883 : : break;
4884 : 3 : tree parmval
4885 : 3 : = convert_default_arg (TREE_VALUE (typetail),
4886 : 3 : TREE_PURPOSE (typetail),
4887 : 3 : fndecl, i, complain);
4888 : :
4889 : 3 : if (parmval == error_mark_node)
4890 : 0 : return -1;
4891 : :
4892 : 3 : vec_safe_push (*values, parmval);
4893 : 3 : typetail = TREE_CHAIN (typetail);
4894 : : /* ends with `...'. */
4895 : 3 : if (typetail == NULL_TREE)
4896 : : break;
4897 : : }
4898 : : }
4899 : :
4900 : 157 : if (typetail && typetail != void_list_node)
4901 : : {
4902 : 157 : if (complain & tf_error)
4903 : 84 : error_args_num (input_location, fndecl, /*too_many_p=*/false);
4904 : 157 : return -1;
4905 : : }
4906 : : }
4907 : :
4908 : 4267885 : return (int) i;
4909 : : }
4910 : :
4911 : : /* Build a binary-operation expression, after performing default
4912 : : conversions on the operands. CODE is the kind of expression to
4913 : : build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4914 : : are the tree codes which correspond to ARG1 and ARG2 when issuing
4915 : : warnings about possibly misplaced parentheses. They may differ
4916 : : from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4917 : : folding (e.g., if the parser sees "a | 1 + 1", it may call this
4918 : : routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4919 : : To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4920 : : ARG2_CODE as ERROR_MARK. */
4921 : :
4922 : : tree
4923 : 203393076 : build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4924 : : enum tree_code arg1_code, tree arg2,
4925 : : enum tree_code arg2_code, tree lookups,
4926 : : tree *overload_p, tsubst_flags_t complain)
4927 : : {
4928 : 203393076 : tree orig_arg1;
4929 : 203393076 : tree orig_arg2;
4930 : 203393076 : tree expr;
4931 : 203393076 : tree overload = NULL_TREE;
4932 : :
4933 : 203393076 : orig_arg1 = arg1;
4934 : 203393076 : orig_arg2 = arg2;
4935 : :
4936 : 203393076 : if (processing_template_decl)
4937 : : {
4938 : 119600255 : if (type_dependent_expression_p (arg1)
4939 : 119600255 : || type_dependent_expression_p (arg2))
4940 : : {
4941 : 84928308 : expr = build_min_nt_loc (loc, code, arg1, arg2);
4942 : 84928308 : TREE_TYPE (expr)
4943 : 84928308 : = build_dependent_operator_type (lookups, code, false);
4944 : 84928308 : return expr;
4945 : : }
4946 : : }
4947 : :
4948 : 118464768 : if (code == DOTSTAR_EXPR)
4949 : 90882 : expr = build_m_component_ref (arg1, arg2, complain);
4950 : : else
4951 : 118373886 : expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4952 : : lookups, &overload, complain);
4953 : :
4954 : 118464735 : if (overload_p != NULL)
4955 : 49580057 : *overload_p = overload;
4956 : :
4957 : : /* Check for cases such as x+y<<z which users are likely to
4958 : : misinterpret. But don't warn about obj << x + y, since that is a
4959 : : common idiom for I/O. */
4960 : 118464735 : if (warn_parentheses
4961 : 1191288 : && (complain & tf_warning)
4962 : 1138412 : && !processing_template_decl
4963 : 787507 : && !error_operand_p (arg1)
4964 : 787501 : && !error_operand_p (arg2)
4965 : 119252224 : && (code != LSHIFT_EXPR
4966 : 70066 : || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4967 : 786486 : warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4968 : : arg2_code, orig_arg2);
4969 : :
4970 : 118464735 : if (processing_template_decl && expr != error_mark_node)
4971 : : {
4972 : 34671757 : if (overload != NULL_TREE)
4973 : 2420546 : return (build_min_non_dep_op_overload
4974 : 2420546 : (code, expr, overload, orig_arg1, orig_arg2));
4975 : :
4976 : 32251211 : return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4977 : : }
4978 : :
4979 : : return expr;
4980 : : }
4981 : :
4982 : : /* Build and return an ARRAY_REF expression. */
4983 : :
4984 : : tree
4985 : 2089175 : build_x_array_ref (location_t loc, tree arg1, tree arg2,
4986 : : tsubst_flags_t complain)
4987 : : {
4988 : 2089175 : tree orig_arg1 = arg1;
4989 : 2089175 : tree orig_arg2 = arg2;
4990 : 2089175 : tree expr;
4991 : 2089175 : tree overload = NULL_TREE;
4992 : :
4993 : 2089175 : if (processing_template_decl)
4994 : : {
4995 : 723 : if (type_dependent_expression_p (arg1)
4996 : 723 : || type_dependent_expression_p (arg2))
4997 : 655 : return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4998 : 655 : NULL_TREE, NULL_TREE);
4999 : : }
5000 : :
5001 : 2088520 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
5002 : : NULL_TREE, NULL_TREE, &overload, complain);
5003 : :
5004 : 2088520 : if (processing_template_decl && expr != error_mark_node)
5005 : : {
5006 : 65 : if (overload != NULL_TREE)
5007 : 51 : return (build_min_non_dep_op_overload
5008 : 51 : (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
5009 : :
5010 : 14 : return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
5011 : 14 : NULL_TREE, NULL_TREE);
5012 : : }
5013 : : return expr;
5014 : : }
5015 : :
5016 : : /* Build an OpenMP array section reference, creating an exact type for the
5017 : : resulting expression based on the element type and bounds if possible. If
5018 : : we have variable bounds, create an incomplete array type for the result
5019 : : instead. */
5020 : :
5021 : : tree
5022 : 10452 : build_omp_array_section (location_t loc, tree array_expr, tree index,
5023 : : tree length)
5024 : : {
5025 : 10452 : if (TREE_CODE (array_expr) == TYPE_DECL
5026 : 10452 : || type_dependent_expression_p (array_expr))
5027 : 258 : return build3_loc (loc, OMP_ARRAY_SECTION, NULL_TREE, array_expr, index,
5028 : 258 : length);
5029 : :
5030 : 10194 : tree type = TREE_TYPE (array_expr);
5031 : 10194 : gcc_assert (type);
5032 : 10194 : type = non_reference (type);
5033 : :
5034 : 10194 : tree sectype, eltype = TREE_TYPE (type);
5035 : :
5036 : : /* It's not an array or pointer type. Just reuse the type of the
5037 : : original expression as the type of the array section (an error will be
5038 : : raised anyway, later). */
5039 : 10194 : if (eltype == NULL_TREE)
5040 : 39 : sectype = TREE_TYPE (array_expr);
5041 : : else
5042 : : {
5043 : 10155 : tree idxtype = NULL_TREE;
5044 : :
5045 : : /* If we know the integer bounds, create an index type with exact
5046 : : low/high (or zero/length) bounds. Otherwise, create an incomplete
5047 : : array type. (This mostly only affects diagnostics.) */
5048 : 10155 : if (index != NULL_TREE
5049 : 10155 : && length != NULL_TREE
5050 : 7331 : && TREE_CODE (index) == INTEGER_CST
5051 : 6537 : && TREE_CODE (length) == INTEGER_CST)
5052 : : {
5053 : 5042 : tree low = fold_convert (sizetype, index);
5054 : 5042 : tree high = fold_convert (sizetype, length);
5055 : 5042 : high = size_binop (PLUS_EXPR, low, high);
5056 : 5042 : high = size_binop (MINUS_EXPR, high, size_one_node);
5057 : 5042 : idxtype = build_range_type (sizetype, low, high);
5058 : 5042 : }
5059 : 2897 : else if ((index == NULL_TREE || integer_zerop (index))
5060 : 3527 : && length != NULL_TREE
5061 : 7235 : && TREE_CODE (length) == INTEGER_CST)
5062 : 1411 : idxtype = build_index_type (length);
5063 : :
5064 : 10155 : sectype = build_array_type (eltype, idxtype);
5065 : : }
5066 : :
5067 : 10194 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array_expr, index,
5068 : 10194 : length);
5069 : : }
5070 : :
5071 : : /* Return whether OP is an expression of enum type cast to integer
5072 : : type. In C++ even unsigned enum types are cast to signed integer
5073 : : types. We do not want to issue warnings about comparisons between
5074 : : signed and unsigned types when one of the types is an enum type.
5075 : : Those warnings are always false positives in practice. */
5076 : :
5077 : : static bool
5078 : 617408 : enum_cast_to_int (tree op)
5079 : : {
5080 : 605014 : if (CONVERT_EXPR_P (op)
5081 : 13408 : && TREE_TYPE (op) == integer_type_node
5082 : 886 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
5083 : 617423 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
5084 : : return true;
5085 : :
5086 : : /* The cast may have been pushed into a COND_EXPR. */
5087 : 617401 : if (TREE_CODE (op) == COND_EXPR)
5088 : 763 : return (enum_cast_to_int (TREE_OPERAND (op, 1))
5089 : 763 : || enum_cast_to_int (TREE_OPERAND (op, 2)));
5090 : :
5091 : : return false;
5092 : : }
5093 : :
5094 : : /* For the c-common bits. */
5095 : : tree
5096 : 4233081 : build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
5097 : : bool /*convert_p*/)
5098 : : {
5099 : 4233081 : return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
5100 : : }
5101 : :
5102 : : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
5103 : : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
5104 : :
5105 : : static tree
5106 : 7163 : build_vec_cmp (tree_code code, tree type,
5107 : : tree arg0, tree arg1)
5108 : : {
5109 : 7163 : tree zero_vec = build_zero_cst (type);
5110 : 7163 : tree minus_one_vec = build_minus_one_cst (type);
5111 : 7163 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
5112 : 7163 : tree cmp = build2 (code, cmp_type, arg0, arg1);
5113 : 7163 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
5114 : : }
5115 : :
5116 : : /* Possibly warn about an address never being NULL. */
5117 : :
5118 : : static void
5119 : 1612820 : warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
5120 : : {
5121 : : /* Prevent warnings issued for macro expansion. */
5122 : 1612820 : if (!warn_address
5123 : 41146 : || (complain & tf_warning) == 0
5124 : 24600 : || c_inhibit_evaluation_warnings != 0
5125 : 24584 : || from_macro_expansion_at (location)
5126 : 1630670 : || warning_suppressed_p (op, OPT_Waddress))
5127 : 1595293 : return;
5128 : :
5129 : 17817 : tree cop = fold_for_warn (op);
5130 : :
5131 : 17817 : if (TREE_CODE (cop) == NON_LVALUE_EXPR)
5132 : : /* Unwrap the expression for C++ 98. */
5133 : 1 : cop = TREE_OPERAND (cop, 0);
5134 : :
5135 : 17817 : if (TREE_CODE (cop) == PTRMEM_CST)
5136 : : {
5137 : : /* The address of a nonstatic data member is never null. */
5138 : 30 : warning_at (location, OPT_Waddress,
5139 : : "the address %qE will never be NULL",
5140 : : cop);
5141 : 30 : return;
5142 : : }
5143 : :
5144 : 17787 : if (TREE_CODE (cop) == NOP_EXPR)
5145 : : {
5146 : : /* Allow casts to intptr_t to suppress the warning. */
5147 : 1214 : tree type = TREE_TYPE (cop);
5148 : 1214 : if (TREE_CODE (type) == INTEGER_TYPE)
5149 : : return;
5150 : :
5151 : 1214 : STRIP_NOPS (cop);
5152 : : }
5153 : :
5154 : 17787 : auto_diagnostic_group d;
5155 : 17787 : bool warned = false;
5156 : 17787 : if (TREE_CODE (cop) == ADDR_EXPR)
5157 : : {
5158 : 789 : cop = TREE_OPERAND (cop, 0);
5159 : :
5160 : : /* Set to true in the loop below if OP dereferences its operand.
5161 : : In such a case the ultimate target need not be a decl for
5162 : : the null [in]equality test to be necessarily constant. */
5163 : 789 : bool deref = false;
5164 : :
5165 : : /* Get the outermost array or object, or member. */
5166 : 960 : while (handled_component_p (cop))
5167 : : {
5168 : 270 : if (TREE_CODE (cop) == COMPONENT_REF)
5169 : : {
5170 : : /* Get the member (its address is never null). */
5171 : 99 : cop = TREE_OPERAND (cop, 1);
5172 : 99 : break;
5173 : : }
5174 : :
5175 : : /* Get the outer array/object to refer to in the warning. */
5176 : 171 : cop = TREE_OPERAND (cop, 0);
5177 : 171 : deref = true;
5178 : : }
5179 : :
5180 : 639 : if ((!deref && !decl_with_nonnull_addr_p (cop))
5181 : 631 : || from_macro_expansion_at (location)
5182 : 1420 : || warning_suppressed_p (cop, OPT_Waddress))
5183 : 158 : return;
5184 : :
5185 : 631 : warned = warning_at (location, OPT_Waddress,
5186 : : "the address of %qD will never be NULL", cop);
5187 : 631 : op = cop;
5188 : : }
5189 : 16998 : else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
5190 : : {
5191 : : /* Adding zero to the null pointer is well-defined in C++. When
5192 : : the offset is unknown (i.e., not a constant) warn anyway since
5193 : : it's less likely that the pointer operand is null than not. */
5194 : 102 : tree off = TREE_OPERAND (cop, 1);
5195 : 102 : if (!integer_zerop (off)
5196 : 102 : && !warning_suppressed_p (cop, OPT_Waddress))
5197 : : {
5198 : 102 : tree base = TREE_OPERAND (cop, 0);
5199 : 102 : STRIP_NOPS (base);
5200 : 102 : if (TYPE_REF_P (TREE_TYPE (base)))
5201 : 12 : warning_at (location, OPT_Waddress, "the compiler can assume that "
5202 : : "the address of %qE will never be NULL", base);
5203 : : else
5204 : 90 : warning_at (location, OPT_Waddress, "comparing the result of "
5205 : : "pointer addition %qE and NULL", cop);
5206 : : }
5207 : 102 : return;
5208 : : }
5209 : 15963 : else if (CONVERT_EXPR_P (op)
5210 : 16958 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
5211 : : {
5212 : 62 : STRIP_NOPS (op);
5213 : :
5214 : 62 : if (TREE_CODE (op) == COMPONENT_REF)
5215 : 24 : op = TREE_OPERAND (op, 1);
5216 : :
5217 : 62 : if (DECL_P (op))
5218 : 62 : warned = warning_at (location, OPT_Waddress,
5219 : : "the compiler can assume that the address of "
5220 : : "%qD will never be NULL", op);
5221 : : }
5222 : :
5223 : 693 : if (warned && DECL_P (op))
5224 : 657 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
5225 : 17787 : }
5226 : :
5227 : : /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5228 : : the other operand is of a different enumeration type or a floating-point
5229 : : type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5230 : : code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5231 : : and LOC is the location for the whole binary expression.
5232 : : For C++26 this is ill-formed rather than deprecated.
5233 : : Return true for SFINAE errors.
5234 : : TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5235 : :
5236 : : static bool
5237 : 97324389 : do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5238 : : tree type1, tsubst_flags_t complain)
5239 : : {
5240 : 97324389 : if (TREE_CODE (type0) == ENUMERAL_TYPE
5241 : 2876145 : && TREE_CODE (type1) == ENUMERAL_TYPE
5242 : 99675566 : && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
5243 : : {
5244 : 198 : if (cxx_dialect >= cxx26)
5245 : : {
5246 : 60 : if ((complain & tf_warning_or_error) == 0)
5247 : : return true;
5248 : : }
5249 : 138 : else if ((complain & tf_warning) == 0)
5250 : : return false;
5251 : : /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5252 : : Otherwise, warn if -Wenum-conversion is on. */
5253 : 190 : enum opt_code opt;
5254 : 190 : if (warn_deprecated_enum_enum_conv)
5255 : : opt = OPT_Wdeprecated_enum_enum_conversion;
5256 : 93 : else if (warn_enum_conversion)
5257 : : opt = OPT_Wenum_conversion;
5258 : : else
5259 : : return false;
5260 : :
5261 : 119 : switch (code)
5262 : : {
5263 : : case GT_EXPR:
5264 : : case LT_EXPR:
5265 : : case GE_EXPR:
5266 : : case LE_EXPR:
5267 : : case EQ_EXPR:
5268 : : case NE_EXPR:
5269 : : /* Comparisons are handled by -Wenum-compare. */
5270 : : return false;
5271 : : case SPACESHIP_EXPR:
5272 : : /* This is invalid, don't warn. */
5273 : : return false;
5274 : 17 : case BIT_AND_EXPR:
5275 : 17 : case BIT_IOR_EXPR:
5276 : 17 : case BIT_XOR_EXPR:
5277 : 17 : if (cxx_dialect >= cxx26)
5278 : 5 : pedwarn (loc, opt, "bitwise operation between different "
5279 : : "enumeration types %qT and %qT", type0, type1);
5280 : : else
5281 : 12 : warning_at (loc, opt, "bitwise operation between different "
5282 : : "enumeration types %qT and %qT is deprecated",
5283 : : type0, type1);
5284 : 17 : return false;
5285 : 41 : default:
5286 : 41 : if (cxx_dialect >= cxx26)
5287 : 12 : pedwarn (loc, opt, "arithmetic between different enumeration "
5288 : : "types %qT and %qT", type0, type1);
5289 : : else
5290 : 29 : warning_at (loc, opt, "arithmetic between different enumeration "
5291 : : "types %qT and %qT is deprecated", type0, type1);
5292 : 41 : return false;
5293 : : }
5294 : : }
5295 : 97324191 : else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5296 : 2875947 : && SCALAR_FLOAT_TYPE_P (type1))
5297 : 97324121 : || (SCALAR_FLOAT_TYPE_P (type0)
5298 : 33023410 : && TREE_CODE (type1) == ENUMERAL_TYPE))
5299 : : {
5300 : 147 : if (cxx_dialect >= cxx26)
5301 : : {
5302 : 46 : if ((complain & tf_warning_or_error) == 0)
5303 : : return true;
5304 : : }
5305 : 101 : else if ((complain & tf_warning) == 0)
5306 : : return false;
5307 : 135 : const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5308 : : /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5309 : : Otherwise, warn if -Wenum-conversion is on. */
5310 : 135 : enum opt_code opt;
5311 : 135 : if (warn_deprecated_enum_float_conv)
5312 : : opt = OPT_Wdeprecated_enum_float_conversion;
5313 : 68 : else if (warn_enum_conversion)
5314 : : opt = OPT_Wenum_conversion;
5315 : : else
5316 : : return false;
5317 : :
5318 : 84 : switch (code)
5319 : : {
5320 : 34 : case GT_EXPR:
5321 : 34 : case LT_EXPR:
5322 : 34 : case GE_EXPR:
5323 : 34 : case LE_EXPR:
5324 : 34 : case EQ_EXPR:
5325 : 34 : case NE_EXPR:
5326 : 34 : if (enum_first_p && cxx_dialect >= cxx26)
5327 : 5 : pedwarn (loc, opt, "comparison of enumeration type %qT with "
5328 : : "floating-point type %qT", type0, type1);
5329 : 29 : else if (cxx_dialect >= cxx26)
5330 : 5 : pedwarn (loc, opt, "comparison of floating-point type %qT "
5331 : : "with enumeration type %qT", type0, type1);
5332 : 24 : else if (enum_first_p)
5333 : 12 : warning_at (loc, opt, "comparison of enumeration type %qT with "
5334 : : "floating-point type %qT is deprecated",
5335 : : type0, type1);
5336 : : else
5337 : 12 : warning_at (loc, opt, "comparison of floating-point type %qT "
5338 : : "with enumeration type %qT is deprecated",
5339 : : type0, type1);
5340 : 34 : return false;
5341 : : case SPACESHIP_EXPR:
5342 : : /* This is invalid, don't warn. */
5343 : : return false;
5344 : 38 : default:
5345 : 38 : if (enum_first_p && cxx_dialect >= cxx26)
5346 : 5 : pedwarn (loc, opt, "arithmetic between enumeration type %qT "
5347 : : "and floating-point type %qT", type0, type1);
5348 : 33 : else if (cxx_dialect >= cxx26)
5349 : 6 : pedwarn (loc, opt, "arithmetic between floating-point type %qT "
5350 : : "and enumeration type %qT", type0, type1);
5351 : 27 : else if (enum_first_p)
5352 : 12 : warning_at (loc, opt, "arithmetic between enumeration type %qT "
5353 : : "and floating-point type %qT is deprecated",
5354 : : type0, type1);
5355 : : else
5356 : 15 : warning_at (loc, opt, "arithmetic between floating-point type %qT "
5357 : : "and enumeration type %qT is deprecated",
5358 : : type0, type1);
5359 : 38 : return false;
5360 : : }
5361 : : }
5362 : : return false;
5363 : : }
5364 : :
5365 : : /* Build a binary-operation expression without default conversions.
5366 : : CODE is the kind of expression to build.
5367 : : LOCATION is the location_t of the operator in the source code.
5368 : : This function differs from `build' in several ways:
5369 : : the data type of the result is computed and recorded in it,
5370 : : warnings are generated if arg data types are invalid,
5371 : : special handling for addition and subtraction of pointers is known,
5372 : : and some optimization is done (operations on narrow ints
5373 : : are done in the narrower type when that gives the same result).
5374 : : Constant folding is also done before the result is returned.
5375 : :
5376 : : Note that the operands will never have enumeral types
5377 : : because either they have just had the default conversions performed
5378 : : or they have both just been converted to some other type in which
5379 : : the arithmetic is to be done.
5380 : :
5381 : : C++: must do special pointer arithmetic when implementing
5382 : : multiple inheritance, and deal with pointer to member functions. */
5383 : :
5384 : : tree
5385 : 128766505 : cp_build_binary_op (const op_location_t &location,
5386 : : enum tree_code code, tree orig_op0, tree orig_op1,
5387 : : tsubst_flags_t complain)
5388 : : {
5389 : 128766505 : tree op0, op1;
5390 : 128766505 : enum tree_code code0, code1;
5391 : 128766505 : tree type0, type1, orig_type0, orig_type1;
5392 : 128766505 : const char *invalid_op_diag;
5393 : :
5394 : : /* Expression code to give to the expression when it is built.
5395 : : Normally this is CODE, which is what the caller asked for,
5396 : : but in some special cases we change it. */
5397 : 128766505 : enum tree_code resultcode = code;
5398 : :
5399 : : /* Data type in which the computation is to be performed.
5400 : : In the simplest cases this is the common type of the arguments. */
5401 : 128766505 : tree result_type = NULL_TREE;
5402 : :
5403 : : /* When the computation is in excess precision, the type of the
5404 : : final EXCESS_PRECISION_EXPR. */
5405 : 128766505 : tree semantic_result_type = NULL;
5406 : :
5407 : : /* Nonzero means operands have already been type-converted
5408 : : in whatever way is necessary.
5409 : : Zero means they need to be converted to RESULT_TYPE. */
5410 : 128766505 : int converted = 0;
5411 : :
5412 : : /* Nonzero means create the expression with this type, rather than
5413 : : RESULT_TYPE. */
5414 : 128766505 : tree build_type = 0;
5415 : :
5416 : : /* Nonzero means after finally constructing the expression
5417 : : convert it to this type. */
5418 : 128766505 : tree final_type = 0;
5419 : :
5420 : 128766505 : tree result;
5421 : :
5422 : : /* Nonzero if this is an operation like MIN or MAX which can
5423 : : safely be computed in short if both args are promoted shorts.
5424 : : Also implies COMMON.
5425 : : -1 indicates a bitwise operation; this makes a difference
5426 : : in the exact conditions for when it is safe to do the operation
5427 : : in a narrower mode. */
5428 : 128766505 : int shorten = 0;
5429 : :
5430 : : /* Nonzero if this is a comparison operation;
5431 : : if both args are promoted shorts, compare the original shorts.
5432 : : Also implies COMMON. */
5433 : 128766505 : int short_compare = 0;
5434 : :
5435 : : /* Nonzero if this is a right-shift operation, which can be computed on the
5436 : : original short and then promoted if the operand is a promoted short. */
5437 : 128766505 : int short_shift = 0;
5438 : :
5439 : : /* Nonzero means set RESULT_TYPE to the common type of the args. */
5440 : 128766505 : int common = 0;
5441 : :
5442 : : /* True if both operands have arithmetic type. */
5443 : 128766505 : bool arithmetic_types_p;
5444 : :
5445 : : /* Remember whether we're doing / or %. */
5446 : 128766505 : bool doing_div_or_mod = false;
5447 : :
5448 : : /* Remember whether we're doing << or >>. */
5449 : 128766505 : bool doing_shift = false;
5450 : :
5451 : : /* Tree holding instrumentation expression. */
5452 : 128766505 : tree instrument_expr = NULL_TREE;
5453 : :
5454 : : /* True means this is an arithmetic operation that may need excess
5455 : : precision. */
5456 : 128766505 : bool may_need_excess_precision;
5457 : :
5458 : : /* Apply default conversions. */
5459 : 128766505 : op0 = resolve_nondeduced_context (orig_op0, complain);
5460 : 128766505 : op1 = resolve_nondeduced_context (orig_op1, complain);
5461 : :
5462 : 128766505 : if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5463 : 116930811 : || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5464 : 114102702 : || code == TRUTH_XOR_EXPR)
5465 : : {
5466 : 14663803 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5467 : 14663785 : op0 = decay_conversion (op0, complain);
5468 : 14663803 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5469 : 14663803 : op1 = decay_conversion (op1, complain);
5470 : : }
5471 : : else
5472 : : {
5473 : 114102702 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5474 : 114102687 : op0 = cp_default_conversion (op0, complain);
5475 : 114102702 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5476 : 114102699 : op1 = cp_default_conversion (op1, complain);
5477 : : }
5478 : :
5479 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5480 : 136497277 : STRIP_TYPE_NOPS (op0);
5481 : 157975054 : STRIP_TYPE_NOPS (op1);
5482 : :
5483 : : /* DTRT if one side is an overloaded function, but complain about it. */
5484 : 128766505 : if (type_unknown_p (op0))
5485 : : {
5486 : 9 : tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5487 : 9 : if (t != error_mark_node)
5488 : : {
5489 : 0 : if (complain & tf_error)
5490 : 0 : permerror (location,
5491 : : "assuming cast to type %qT from overloaded function",
5492 : 0 : TREE_TYPE (t));
5493 : : op0 = t;
5494 : : }
5495 : : }
5496 : 128766505 : if (type_unknown_p (op1))
5497 : : {
5498 : 3 : tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5499 : 3 : if (t != error_mark_node)
5500 : : {
5501 : 3 : if (complain & tf_error)
5502 : 3 : permerror (location,
5503 : : "assuming cast to type %qT from overloaded function",
5504 : 3 : TREE_TYPE (t));
5505 : : op1 = t;
5506 : : }
5507 : : }
5508 : :
5509 : 128766505 : orig_type0 = type0 = TREE_TYPE (op0);
5510 : 128766505 : orig_type1 = type1 = TREE_TYPE (op1);
5511 : 128766505 : tree non_ep_op0 = op0;
5512 : 128766505 : tree non_ep_op1 = op1;
5513 : :
5514 : : /* The expression codes of the data types of the arguments tell us
5515 : : whether the arguments are integers, floating, pointers, etc. */
5516 : 128766505 : code0 = TREE_CODE (type0);
5517 : 128766505 : code1 = TREE_CODE (type1);
5518 : :
5519 : : /* If an error was already reported for one of the arguments,
5520 : : avoid reporting another error. */
5521 : 128766505 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5522 : 99 : return error_mark_node;
5523 : :
5524 : 257532812 : if ((invalid_op_diag
5525 : 128766406 : = targetm.invalid_binary_op (code, type0, type1)))
5526 : : {
5527 : 0 : if (complain & tf_error)
5528 : : {
5529 : 0 : if (code0 == REAL_TYPE
5530 : 0 : && code1 == REAL_TYPE
5531 : 0 : && (extended_float_type_p (type0)
5532 : 0 : || extended_float_type_p (type1))
5533 : 0 : && cp_compare_floating_point_conversion_ranks (type0,
5534 : : type1) == 3)
5535 : : {
5536 : 0 : rich_location richloc (line_table, location);
5537 : 0 : binary_op_error (&richloc, code, type0, type1);
5538 : 0 : }
5539 : : else
5540 : 0 : error (invalid_op_diag);
5541 : : }
5542 : 0 : return error_mark_node;
5543 : : }
5544 : :
5545 : 128766406 : switch (code)
5546 : : {
5547 : : case PLUS_EXPR:
5548 : : case MINUS_EXPR:
5549 : : case MULT_EXPR:
5550 : : case TRUNC_DIV_EXPR:
5551 : : case CEIL_DIV_EXPR:
5552 : : case FLOOR_DIV_EXPR:
5553 : : case ROUND_DIV_EXPR:
5554 : : case EXACT_DIV_EXPR:
5555 : : may_need_excess_precision = true;
5556 : : break;
5557 : 43683741 : case EQ_EXPR:
5558 : 43683741 : case NE_EXPR:
5559 : 43683741 : case LE_EXPR:
5560 : 43683741 : case GE_EXPR:
5561 : 43683741 : case LT_EXPR:
5562 : 43683741 : case GT_EXPR:
5563 : 43683741 : case SPACESHIP_EXPR:
5564 : : /* Excess precision for implicit conversions of integers to
5565 : : floating point. */
5566 : 9550378 : may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5567 : 53227526 : || ANY_INTEGRAL_TYPE_P (type1));
5568 : : break;
5569 : : default:
5570 : 128766406 : may_need_excess_precision = false;
5571 : : break;
5572 : : }
5573 : 128766406 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5574 : : {
5575 : 22716 : op0 = TREE_OPERAND (op0, 0);
5576 : 22716 : type0 = TREE_TYPE (op0);
5577 : : }
5578 : 128743690 : else if (may_need_excess_precision
5579 : 97672387 : && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5580 : 28835285 : if (tree eptype = excess_precision_type (type0))
5581 : : {
5582 : 60963 : type0 = eptype;
5583 : 60963 : op0 = convert (eptype, op0);
5584 : : }
5585 : 128766406 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5586 : : {
5587 : 28792 : op1 = TREE_OPERAND (op1, 0);
5588 : 28792 : type1 = TREE_TYPE (op1);
5589 : : }
5590 : 128737614 : else if (may_need_excess_precision
5591 : 97669570 : && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5592 : 28753536 : if (tree eptype = excess_precision_type (type1))
5593 : : {
5594 : 39839 : type1 = eptype;
5595 : 39839 : op1 = convert (eptype, op1);
5596 : : }
5597 : :
5598 : : /* Issue warnings about peculiar, but valid, uses of NULL. */
5599 : 257532727 : if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5600 : : /* It's reasonable to use pointer values as operands of &&
5601 : : and ||, so NULL is no exception. */
5602 : 12480 : && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5603 : 12465 : && ( /* Both are NULL (or 0) and the operation was not a
5604 : : comparison or a pointer subtraction. */
5605 : 12538 : (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5606 : 27 : && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5607 : : /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5608 : 12453 : || (!null_ptr_cst_p (orig_op0)
5609 : 12392 : && !TYPE_PTR_OR_PTRMEM_P (type0))
5610 : 12441 : || (!null_ptr_cst_p (orig_op1)
5611 : 46 : && !TYPE_PTR_OR_PTRMEM_P (type1)))
5612 : 128766445 : && (complain & tf_warning))
5613 : : {
5614 : 39 : location_t loc =
5615 : 39 : expansion_point_location_if_in_system_header (input_location);
5616 : :
5617 : 39 : warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5618 : : }
5619 : :
5620 : : /* In case when one of the operands of the binary operation is
5621 : : a vector and another is a scalar -- convert scalar to vector. */
5622 : 128833260 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5623 : 128832412 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5624 : : {
5625 : 933 : enum stv_conv convert_flag
5626 : 933 : = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5627 : : complain & tf_error);
5628 : :
5629 : 933 : switch (convert_flag)
5630 : : {
5631 : 21 : case stv_error:
5632 : 21 : return error_mark_node;
5633 : 55 : case stv_firstarg:
5634 : 55 : {
5635 : 55 : op0 = convert (TREE_TYPE (type1), op0);
5636 : 55 : op0 = cp_save_expr (op0);
5637 : 55 : op0 = build_vector_from_val (type1, op0);
5638 : 55 : orig_type0 = type0 = TREE_TYPE (op0);
5639 : 55 : code0 = TREE_CODE (type0);
5640 : 55 : converted = 1;
5641 : 55 : break;
5642 : : }
5643 : 752 : case stv_secondarg:
5644 : 752 : {
5645 : 752 : op1 = convert (TREE_TYPE (type0), op1);
5646 : 752 : op1 = cp_save_expr (op1);
5647 : 752 : op1 = build_vector_from_val (type0, op1);
5648 : 752 : orig_type1 = type1 = TREE_TYPE (op1);
5649 : 752 : code1 = TREE_CODE (type1);
5650 : 752 : converted = 1;
5651 : 752 : break;
5652 : : }
5653 : : default:
5654 : : break;
5655 : : }
5656 : : }
5657 : :
5658 : 128766385 : switch (code)
5659 : : {
5660 : 14989771 : case MINUS_EXPR:
5661 : : /* Subtraction of two similar pointers.
5662 : : We must subtract them as integers, then divide by object size. */
5663 : 14989771 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5664 : 16338521 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5665 : 1348750 : TREE_TYPE (type1)))
5666 : : {
5667 : 1348748 : result = pointer_diff (location, op0, op1,
5668 : : common_pointer_type (type0, type1), complain,
5669 : : &instrument_expr);
5670 : 1348748 : if (instrument_expr != NULL)
5671 : 84 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5672 : : instrument_expr, result);
5673 : :
5674 : 1348748 : return result;
5675 : : }
5676 : : /* In all other cases except pointer - int, the usual arithmetic
5677 : : rules apply. */
5678 : 13641023 : else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5679 : : {
5680 : : common = 1;
5681 : : break;
5682 : : }
5683 : : /* The pointer - int case is just like pointer + int; fall
5684 : : through. */
5685 : 19625221 : gcc_fallthrough ();
5686 : 19625221 : case PLUS_EXPR:
5687 : 19625221 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5688 : 6628840 : && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5689 : : {
5690 : 6628831 : tree ptr_operand;
5691 : 6628831 : tree int_operand;
5692 : 6628831 : ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5693 : 50539 : int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5694 : 6628831 : if (processing_template_decl)
5695 : : {
5696 : 2046089 : result_type = TREE_TYPE (ptr_operand);
5697 : 2046089 : break;
5698 : : }
5699 : 4582742 : return cp_pointer_int_sum (location, code,
5700 : : ptr_operand,
5701 : : int_operand,
5702 : 4582742 : complain);
5703 : : }
5704 : : common = 1;
5705 : : break;
5706 : :
5707 : : case MULT_EXPR:
5708 : 122798412 : common = 1;
5709 : : break;
5710 : :
5711 : 9815972 : case TRUNC_DIV_EXPR:
5712 : 9815972 : case CEIL_DIV_EXPR:
5713 : 9815972 : case FLOOR_DIV_EXPR:
5714 : 9815972 : case ROUND_DIV_EXPR:
5715 : 9815972 : case EXACT_DIV_EXPR:
5716 : 9815972 : if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5717 : : {
5718 : 103194 : tree type0 = TREE_OPERAND (op0, 0);
5719 : 103194 : tree type1 = TREE_OPERAND (op1, 0);
5720 : 103194 : tree first_arg = tree_strip_any_location_wrapper (type0);
5721 : 103194 : if (!TYPE_P (type0))
5722 : 82320 : type0 = TREE_TYPE (type0);
5723 : 103194 : if (!TYPE_P (type1))
5724 : 47215 : type1 = TREE_TYPE (type1);
5725 : 103194 : if (type0
5726 : 103194 : && type1
5727 : 82675 : && INDIRECT_TYPE_P (type0)
5728 : 103269 : && same_type_p (TREE_TYPE (type0), type1))
5729 : : {
5730 : 27 : if (!(TREE_CODE (first_arg) == PARM_DECL
5731 : 21 : && DECL_ARRAY_PARAMETER_P (first_arg)
5732 : 3 : && warn_sizeof_array_argument)
5733 : 42 : && (complain & tf_warning))
5734 : : {
5735 : 21 : auto_diagnostic_group d;
5736 : 21 : if (warning_at (location, OPT_Wsizeof_pointer_div,
5737 : : "division %<sizeof (%T) / sizeof (%T)%> does "
5738 : : "not compute the number of array elements",
5739 : : type0, type1))
5740 : 18 : if (DECL_P (first_arg))
5741 : 18 : inform (DECL_SOURCE_LOCATION (first_arg),
5742 : : "first %<sizeof%> operand was declared here");
5743 : 21 : }
5744 : : }
5745 : 103170 : else if (!dependent_type_p (type0)
5746 : 26796 : && !dependent_type_p (type1)
5747 : 26698 : && TREE_CODE (type0) == ARRAY_TYPE
5748 : 22534 : && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5749 : : /* Set by finish_parenthesized_expr. */
5750 : 21887 : && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5751 : 125039 : && (complain & tf_warning))
5752 : 21869 : maybe_warn_sizeof_array_div (location, first_arg, type0,
5753 : : op1, non_reference (type1));
5754 : : }
5755 : :
5756 : 9815972 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5757 : : || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5758 : 9815954 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5759 : : || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5760 : : {
5761 : 9815945 : enum tree_code tcode0 = code0, tcode1 = code1;
5762 : 9815945 : doing_div_or_mod = true;
5763 : 9815945 : warn_for_div_by_zero (location, fold_for_warn (op1));
5764 : :
5765 : 9815945 : if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5766 : 61534 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5767 : 9815945 : if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5768 : 31626 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5769 : :
5770 : 9815945 : if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5771 : : resultcode = RDIV_EXPR;
5772 : : else
5773 : : {
5774 : : /* When dividing two signed integers, we have to promote to int.
5775 : : unless we divide by a constant != -1. Note that default
5776 : : conversion will have been performed on the operands at this
5777 : : point, so we have to dig out the original type to find out if
5778 : : it was unsigned. */
5779 : 4005447 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5780 : 4005447 : shorten = may_shorten_divmod (op0, stripped_op1);
5781 : : }
5782 : :
5783 : : common = 1;
5784 : : }
5785 : : break;
5786 : :
5787 : 2171033 : case BIT_AND_EXPR:
5788 : 2171033 : case BIT_IOR_EXPR:
5789 : 2171033 : case BIT_XOR_EXPR:
5790 : 2171033 : if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5791 : 2171033 : || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5792 : 20780 : && !VECTOR_FLOAT_TYPE_P (type0)
5793 : 20762 : && !VECTOR_FLOAT_TYPE_P (type1)))
5794 : : shorten = -1;
5795 : : break;
5796 : :
5797 : 1213652 : case TRUNC_MOD_EXPR:
5798 : 1213652 : case FLOOR_MOD_EXPR:
5799 : 1213652 : doing_div_or_mod = true;
5800 : 1213652 : warn_for_div_by_zero (location, fold_for_warn (op1));
5801 : :
5802 : 1213652 : if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5803 : 20 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5804 : 1213672 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5805 : : common = 1;
5806 : 1213632 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5807 : : {
5808 : : /* Although it would be tempting to shorten always here, that loses
5809 : : on some targets, since the modulo instruction is undefined if the
5810 : : quotient can't be represented in the computation mode. We shorten
5811 : : only if unsigned or if dividing by something we know != -1. */
5812 : 1213608 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5813 : 1213608 : shorten = may_shorten_divmod (op0, stripped_op1);
5814 : 1213608 : common = 1;
5815 : : }
5816 : : break;
5817 : :
5818 : 14663788 : case TRUTH_ANDIF_EXPR:
5819 : 14663788 : case TRUTH_ORIF_EXPR:
5820 : 14663788 : case TRUTH_AND_EXPR:
5821 : 14663788 : case TRUTH_OR_EXPR:
5822 : 14663788 : if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5823 : : {
5824 : 3 : if (!COMPARISON_CLASS_P (op1))
5825 : 3 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5826 : : build_zero_cst (type1), complain);
5827 : 3 : if (code == TRUTH_ANDIF_EXPR)
5828 : : {
5829 : 0 : tree z = build_zero_cst (TREE_TYPE (op1));
5830 : 0 : return build_conditional_expr (location, op0, op1, z, complain);
5831 : : }
5832 : 3 : else if (code == TRUTH_ORIF_EXPR)
5833 : : {
5834 : 3 : tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5835 : 3 : return build_conditional_expr (location, op0, m1, op1, complain);
5836 : : }
5837 : : else
5838 : 0 : gcc_unreachable ();
5839 : : }
5840 : 14663785 : if (gnu_vector_type_p (type0)
5841 : 14663785 : && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5842 : : {
5843 : 24 : if (!COMPARISON_CLASS_P (op0))
5844 : 24 : op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5845 : : build_zero_cst (type0), complain);
5846 : 24 : if (!VECTOR_TYPE_P (type1))
5847 : : {
5848 : 9 : tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5849 : 9 : tree z = build_zero_cst (TREE_TYPE (op0));
5850 : 9 : op1 = build_conditional_expr (location, op1, m1, z, complain);
5851 : : }
5852 : 15 : else if (!COMPARISON_CLASS_P (op1))
5853 : 15 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5854 : : build_zero_cst (type1), complain);
5855 : :
5856 : 24 : if (code == TRUTH_ANDIF_EXPR)
5857 : : code = BIT_AND_EXPR;
5858 : 9 : else if (code == TRUTH_ORIF_EXPR)
5859 : : code = BIT_IOR_EXPR;
5860 : : else
5861 : 0 : gcc_unreachable ();
5862 : :
5863 : 24 : return cp_build_binary_op (location, code, op0, op1, complain);
5864 : : }
5865 : :
5866 : 14663761 : result_type = boolean_type_node;
5867 : 14663761 : break;
5868 : :
5869 : : /* Shift operations: result has same type as first operand;
5870 : : always convert second operand to int.
5871 : : Also set SHORT_SHIFT if shifting rightward. */
5872 : :
5873 : 950956 : case RSHIFT_EXPR:
5874 : 950956 : if (gnu_vector_type_p (type0)
5875 : 170 : && code1 == INTEGER_TYPE
5876 : 951003 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5877 : : {
5878 : : result_type = type0;
5879 : : converted = 1;
5880 : : }
5881 : 950909 : else if (gnu_vector_type_p (type0)
5882 : 123 : && gnu_vector_type_p (type1)
5883 : 123 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5884 : 120 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5885 : 951029 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5886 : : TYPE_VECTOR_SUBPARTS (type1)))
5887 : : {
5888 : : result_type = type0;
5889 : : converted = 1;
5890 : : }
5891 : 950789 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5892 : : {
5893 : 950762 : tree const_op1 = fold_for_warn (op1);
5894 : 950762 : if (TREE_CODE (const_op1) != INTEGER_CST)
5895 : 84373 : const_op1 = op1;
5896 : 950762 : result_type = type0;
5897 : 950762 : doing_shift = true;
5898 : 950762 : if (TREE_CODE (const_op1) == INTEGER_CST)
5899 : : {
5900 : 866389 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5901 : : {
5902 : 48 : if ((complain & tf_warning)
5903 : 48 : && c_inhibit_evaluation_warnings == 0)
5904 : 36 : warning_at (location, OPT_Wshift_count_negative,
5905 : : "right shift count is negative");
5906 : : }
5907 : : else
5908 : : {
5909 : 866341 : if (!integer_zerop (const_op1))
5910 : 866255 : short_shift = 1;
5911 : :
5912 : 866341 : if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5913 : 46 : && (complain & tf_warning)
5914 : 866387 : && c_inhibit_evaluation_warnings == 0)
5915 : 34 : warning_at (location, OPT_Wshift_count_overflow,
5916 : : "right shift count >= width of type");
5917 : : }
5918 : : }
5919 : : /* Avoid converting op1 to result_type later. */
5920 : : converted = 1;
5921 : : }
5922 : : break;
5923 : :
5924 : 2923208 : case LSHIFT_EXPR:
5925 : 2923208 : if (gnu_vector_type_p (type0)
5926 : 183 : && code1 == INTEGER_TYPE
5927 : 2923245 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5928 : : {
5929 : : result_type = type0;
5930 : : converted = 1;
5931 : : }
5932 : 2923171 : else if (gnu_vector_type_p (type0)
5933 : 146 : && gnu_vector_type_p (type1)
5934 : 146 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5935 : 143 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5936 : 2923311 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5937 : : TYPE_VECTOR_SUBPARTS (type1)))
5938 : : {
5939 : : result_type = type0;
5940 : : converted = 1;
5941 : : }
5942 : 2923031 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5943 : : {
5944 : 2923000 : tree const_op0 = fold_for_warn (op0);
5945 : 2923000 : if (TREE_CODE (const_op0) != INTEGER_CST)
5946 : 167505 : const_op0 = op0;
5947 : 2923000 : tree const_op1 = fold_for_warn (op1);
5948 : 2923000 : if (TREE_CODE (const_op1) != INTEGER_CST)
5949 : 222511 : const_op1 = op1;
5950 : 2923000 : result_type = type0;
5951 : 2923000 : doing_shift = true;
5952 : 2923000 : if (TREE_CODE (const_op0) == INTEGER_CST
5953 : 2755495 : && tree_int_cst_sgn (const_op0) < 0
5954 : 280 : && !TYPE_OVERFLOW_WRAPS (type0)
5955 : 196 : && (complain & tf_warning)
5956 : 2923196 : && c_inhibit_evaluation_warnings == 0)
5957 : 196 : warning_at (location, OPT_Wshift_negative_value,
5958 : : "left shift of negative value");
5959 : 2923000 : if (TREE_CODE (const_op1) == INTEGER_CST)
5960 : : {
5961 : 2700489 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5962 : : {
5963 : 57 : if ((complain & tf_warning)
5964 : 57 : && c_inhibit_evaluation_warnings == 0)
5965 : 39 : warning_at (location, OPT_Wshift_count_negative,
5966 : : "left shift count is negative");
5967 : : }
5968 : 2700432 : else if (compare_tree_int (const_op1,
5969 : 2700432 : TYPE_PRECISION (type0)) >= 0)
5970 : : {
5971 : 83 : if ((complain & tf_warning)
5972 : 61 : && c_inhibit_evaluation_warnings == 0)
5973 : 45 : warning_at (location, OPT_Wshift_count_overflow,
5974 : : "left shift count >= width of type");
5975 : : }
5976 : 2700349 : else if (TREE_CODE (const_op0) == INTEGER_CST
5977 : 2560941 : && (complain & tf_warning))
5978 : 2446522 : maybe_warn_shift_overflow (location, const_op0, const_op1);
5979 : : }
5980 : : /* Avoid converting op1 to result_type later. */
5981 : : converted = 1;
5982 : : }
5983 : : break;
5984 : :
5985 : 21701781 : case EQ_EXPR:
5986 : 21701781 : case NE_EXPR:
5987 : 21701781 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5988 : 2655 : goto vector_compare;
5989 : 21699126 : if ((complain & tf_warning)
5990 : 20119551 : && c_inhibit_evaluation_warnings == 0
5991 : 41101951 : && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5992 : 833647 : warning_at (location, OPT_Wfloat_equal,
5993 : : "comparing floating-point with %<==%> "
5994 : : "or %<!=%> is unsafe");
5995 : 21699126 : {
5996 : 21699126 : tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5997 : 21699126 : tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5998 : 21699126 : if ((complain & tf_warning_or_error)
5999 : 21699126 : && ((TREE_CODE (stripped_orig_op0) == STRING_CST
6000 : 39 : && !integer_zerop (cp_fully_fold (op1)))
6001 : 20418676 : || (TREE_CODE (stripped_orig_op1) == STRING_CST
6002 : 68 : && !integer_zerop (cp_fully_fold (op0)))))
6003 : 47 : warning_at (location, OPT_Waddress,
6004 : : "comparison with string literal results in "
6005 : : "unspecified behavior");
6006 : 21699079 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6007 : 21699079 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
6008 : : {
6009 : : /* P2865R5 made array comparisons ill-formed in C++26. */
6010 : 76 : if (complain & tf_warning_or_error)
6011 : 45 : do_warn_array_compare (location, code, stripped_orig_op0,
6012 : : stripped_orig_op1);
6013 : 31 : else if (cxx_dialect >= cxx26)
6014 : 13 : return error_mark_node;
6015 : : }
6016 : : }
6017 : :
6018 : 21699113 : build_type = boolean_type_node;
6019 : 21699113 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6020 : : || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
6021 : 18312680 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6022 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
6023 : : short_compare = 1;
6024 : 31176 : else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
6025 : 3385961 : && null_ptr_cst_p (orig_op1))
6026 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
6027 : 5241045 : || (code0 == POINTER_TYPE
6028 : 1794018 : && TYPE_PTR_P (type1) && integer_zerop (op1)))
6029 : : {
6030 : 1611646 : if (TYPE_PTR_P (type1))
6031 : 20075 : result_type = composite_pointer_type (location,
6032 : : type0, type1, op0, op1,
6033 : : CPO_COMPARISON, complain);
6034 : : else
6035 : : result_type = type0;
6036 : :
6037 : 1611646 : if (char_type_p (TREE_TYPE (orig_op1)))
6038 : : {
6039 : 10 : auto_diagnostic_group d;
6040 : 10 : if (warning_at (location, OPT_Wpointer_compare,
6041 : : "comparison between pointer and zero character "
6042 : : "constant"))
6043 : 10 : inform (location,
6044 : : "did you mean to dereference the pointer?");
6045 : 10 : }
6046 : 1611646 : warn_for_null_address (location, op0, complain);
6047 : : }
6048 : 909 : else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
6049 : 1804149 : && null_ptr_cst_p (orig_op0))
6050 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
6051 : 3608427 : || (code1 == POINTER_TYPE
6052 : 1802880 : && TYPE_PTR_P (type0) && integer_zerop (op0)))
6053 : : {
6054 : 965 : if (TYPE_PTR_P (type0))
6055 : 68 : result_type = composite_pointer_type (location,
6056 : : type0, type1, op0, op1,
6057 : : CPO_COMPARISON, complain);
6058 : : else
6059 : : result_type = type1;
6060 : :
6061 : 965 : if (char_type_p (TREE_TYPE (orig_op0)))
6062 : : {
6063 : 10 : auto_diagnostic_group d;
6064 : 10 : if (warning_at (location, OPT_Wpointer_compare,
6065 : : "comparison between pointer and zero character "
6066 : : "constant"))
6067 : 10 : inform (location,
6068 : : "did you mean to dereference the pointer?");
6069 : 10 : }
6070 : 965 : warn_for_null_address (location, op1, complain);
6071 : : }
6072 : 1803697 : else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6073 : 29880 : || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
6074 : 1774189 : result_type = composite_pointer_type (location,
6075 : : type0, type1, op0, op1,
6076 : : CPO_COMPARISON, complain);
6077 : 29508 : else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
6078 : : /* One of the operands must be of nullptr_t type. */
6079 : 74 : result_type = TREE_TYPE (nullptr_node);
6080 : 29434 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6081 : : {
6082 : 58 : result_type = type0;
6083 : 58 : if (complain & tf_error)
6084 : 56 : permerror (location, "ISO C++ forbids comparison between "
6085 : : "pointer and integer");
6086 : : else
6087 : 2 : return error_mark_node;
6088 : : }
6089 : 29376 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6090 : : {
6091 : 28995 : result_type = type1;
6092 : 28995 : if (complain & tf_error)
6093 : 69 : permerror (location, "ISO C++ forbids comparison between "
6094 : : "pointer and integer");
6095 : : else
6096 : 28926 : return error_mark_node;
6097 : : }
6098 : 381 : else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
6099 : : {
6100 : 209 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6101 : : == ptrmemfunc_vbit_in_delta)
6102 : : {
6103 : : tree pfn0, delta0, e1, e2;
6104 : :
6105 : : if (TREE_SIDE_EFFECTS (op0))
6106 : : op0 = cp_save_expr (op0);
6107 : :
6108 : : pfn0 = pfn_from_ptrmemfunc (op0);
6109 : : delta0 = delta_from_ptrmemfunc (op0);
6110 : : {
6111 : : /* If we will warn below about a null-address compare
6112 : : involving the orig_op0 ptrmemfunc, we'd likely also
6113 : : warn about the pfn0's null-address compare, and
6114 : : that would be redundant, so suppress it. */
6115 : : warning_sentinel ws (warn_address);
6116 : : e1 = cp_build_binary_op (location,
6117 : : EQ_EXPR,
6118 : : pfn0,
6119 : : build_zero_cst (TREE_TYPE (pfn0)),
6120 : : complain);
6121 : : }
6122 : : e2 = cp_build_binary_op (location,
6123 : : BIT_AND_EXPR,
6124 : : delta0,
6125 : : integer_one_node,
6126 : : complain);
6127 : :
6128 : : if (complain & tf_warning)
6129 : : maybe_warn_zero_as_null_pointer_constant (op1, input_location);
6130 : :
6131 : : e2 = cp_build_binary_op (location,
6132 : : EQ_EXPR, e2, integer_zero_node,
6133 : : complain);
6134 : : op0 = cp_build_binary_op (location,
6135 : : TRUTH_ANDIF_EXPR, e1, e2,
6136 : : complain);
6137 : : op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
6138 : : }
6139 : : else
6140 : : {
6141 : 209 : op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
6142 : 209 : op1 = cp_convert (TREE_TYPE (op0), op1, complain);
6143 : : }
6144 : 209 : result_type = TREE_TYPE (op0);
6145 : :
6146 : 209 : warn_for_null_address (location, orig_op0, complain);
6147 : : }
6148 : 172 : else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
6149 : 36 : return cp_build_binary_op (location, code, op1, op0, complain);
6150 : 136 : else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
6151 : : {
6152 : 115 : tree type;
6153 : : /* E will be the final comparison. */
6154 : 115 : tree e;
6155 : : /* E1 and E2 are for scratch. */
6156 : 115 : tree e1;
6157 : 115 : tree e2;
6158 : 115 : tree pfn0;
6159 : 115 : tree pfn1;
6160 : 115 : tree delta0;
6161 : 115 : tree delta1;
6162 : :
6163 : 115 : type = composite_pointer_type (location, type0, type1, op0, op1,
6164 : : CPO_COMPARISON, complain);
6165 : :
6166 : 115 : if (!same_type_p (TREE_TYPE (op0), type))
6167 : 6 : op0 = cp_convert_and_check (type, op0, complain);
6168 : 115 : if (!same_type_p (TREE_TYPE (op1), type))
6169 : 9 : op1 = cp_convert_and_check (type, op1, complain);
6170 : :
6171 : 115 : if (op0 == error_mark_node || op1 == error_mark_node)
6172 : : return error_mark_node;
6173 : :
6174 : 112 : if (TREE_SIDE_EFFECTS (op0))
6175 : 53 : op0 = cp_save_expr (op0);
6176 : 112 : if (TREE_SIDE_EFFECTS (op1))
6177 : 3 : op1 = cp_save_expr (op1);
6178 : :
6179 : 112 : pfn0 = pfn_from_ptrmemfunc (op0);
6180 : 112 : pfn0 = cp_fully_fold (pfn0);
6181 : : /* Avoid -Waddress warnings (c++/64877). */
6182 : 112 : if (TREE_CODE (pfn0) == ADDR_EXPR)
6183 : 12 : suppress_warning (pfn0, OPT_Waddress);
6184 : 112 : pfn1 = pfn_from_ptrmemfunc (op1);
6185 : 112 : pfn1 = cp_fully_fold (pfn1);
6186 : 112 : delta0 = delta_from_ptrmemfunc (op0);
6187 : 112 : delta1 = delta_from_ptrmemfunc (op1);
6188 : 112 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6189 : : == ptrmemfunc_vbit_in_delta)
6190 : : {
6191 : : /* We generate:
6192 : :
6193 : : (op0.pfn == op1.pfn
6194 : : && ((op0.delta == op1.delta)
6195 : : || (!op0.pfn && op0.delta & 1 == 0
6196 : : && op1.delta & 1 == 0))
6197 : :
6198 : : The reason for the `!op0.pfn' bit is that a NULL
6199 : : pointer-to-member is any member with a zero PFN and
6200 : : LSB of the DELTA field is 0. */
6201 : :
6202 : : e1 = cp_build_binary_op (location, BIT_AND_EXPR,
6203 : : delta0,
6204 : : integer_one_node,
6205 : : complain);
6206 : : e1 = cp_build_binary_op (location,
6207 : : EQ_EXPR, e1, integer_zero_node,
6208 : : complain);
6209 : : e2 = cp_build_binary_op (location, BIT_AND_EXPR,
6210 : : delta1,
6211 : : integer_one_node,
6212 : : complain);
6213 : : e2 = cp_build_binary_op (location,
6214 : : EQ_EXPR, e2, integer_zero_node,
6215 : : complain);
6216 : : e1 = cp_build_binary_op (location,
6217 : : TRUTH_ANDIF_EXPR, e2, e1,
6218 : : complain);
6219 : : e2 = cp_build_binary_op (location, EQ_EXPR,
6220 : : pfn0,
6221 : : build_zero_cst (TREE_TYPE (pfn0)),
6222 : : complain);
6223 : : e2 = cp_build_binary_op (location,
6224 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6225 : : e1 = cp_build_binary_op (location,
6226 : : EQ_EXPR, delta0, delta1, complain);
6227 : : e1 = cp_build_binary_op (location,
6228 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6229 : : }
6230 : : else
6231 : : {
6232 : : /* We generate:
6233 : :
6234 : : (op0.pfn == op1.pfn
6235 : : && (!op0.pfn || op0.delta == op1.delta))
6236 : :
6237 : : The reason for the `!op0.pfn' bit is that a NULL
6238 : : pointer-to-member is any member with a zero PFN; the
6239 : : DELTA field is unspecified. */
6240 : :
6241 : 112 : e1 = cp_build_binary_op (location,
6242 : : EQ_EXPR, delta0, delta1, complain);
6243 : 112 : e2 = cp_build_binary_op (location,
6244 : : EQ_EXPR,
6245 : : pfn0,
6246 : 112 : build_zero_cst (TREE_TYPE (pfn0)),
6247 : : complain);
6248 : 112 : e1 = cp_build_binary_op (location,
6249 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6250 : : }
6251 : 112 : e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
6252 : 112 : e = cp_build_binary_op (location,
6253 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6254 : 112 : if (code == EQ_EXPR)
6255 : : return e;
6256 : 30 : return cp_build_binary_op (location,
6257 : 30 : EQ_EXPR, e, integer_zero_node, complain);
6258 : : }
6259 : : else
6260 : : {
6261 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
6262 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
6263 : : type1));
6264 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
6265 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
6266 : : type0));
6267 : : }
6268 : :
6269 : : break;
6270 : :
6271 : 241 : case MAX_EXPR:
6272 : 241 : case MIN_EXPR:
6273 : 241 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6274 : 241 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6275 : : shorten = 1;
6276 : 0 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6277 : 0 : result_type = composite_pointer_type (location,
6278 : : type0, type1, op0, op1,
6279 : : CPO_COMPARISON, complain);
6280 : : break;
6281 : :
6282 : 21981960 : case LE_EXPR:
6283 : 21981960 : case GE_EXPR:
6284 : 21981960 : case LT_EXPR:
6285 : 21981960 : case GT_EXPR:
6286 : 21981960 : case SPACESHIP_EXPR:
6287 : 21981960 : if (TREE_CODE (orig_op0) == STRING_CST
6288 : 21981960 : || TREE_CODE (orig_op1) == STRING_CST)
6289 : : {
6290 : 0 : if (complain & tf_warning)
6291 : 0 : warning_at (location, OPT_Waddress,
6292 : : "comparison with string literal results "
6293 : : "in unspecified behavior");
6294 : : }
6295 : 21981960 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6296 : 155 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6297 : 21982066 : && code != SPACESHIP_EXPR)
6298 : : {
6299 : 88 : if (complain & tf_warning_or_error)
6300 : 51 : do_warn_array_compare (location, code,
6301 : : tree_strip_any_location_wrapper (orig_op0),
6302 : : tree_strip_any_location_wrapper (orig_op1));
6303 : 37 : else if (cxx_dialect >= cxx26)
6304 : 1 : return error_mark_node;
6305 : : }
6306 : :
6307 : 21981959 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6308 : : {
6309 : 7172 : vector_compare:
6310 : 7172 : tree intt;
6311 : 7172 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6312 : 7172 : TREE_TYPE (type1))
6313 : 7172 : && !vector_types_compatible_elements_p (type0, type1))
6314 : : {
6315 : 3 : if (complain & tf_error)
6316 : : {
6317 : 3 : auto_diagnostic_group d;
6318 : 3 : error_at (location, "comparing vectors with different "
6319 : : "element types");
6320 : 3 : inform (location, "operand types are %qT and %qT",
6321 : : type0, type1);
6322 : 3 : }
6323 : 3 : return error_mark_node;
6324 : : }
6325 : :
6326 : 7169 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
6327 : 14338 : TYPE_VECTOR_SUBPARTS (type1)))
6328 : : {
6329 : 3 : if (complain & tf_error)
6330 : : {
6331 : 3 : auto_diagnostic_group d;
6332 : 3 : error_at (location, "comparing vectors with different "
6333 : : "number of elements");
6334 : 3 : inform (location, "operand types are %qT and %qT",
6335 : : type0, type1);
6336 : 3 : }
6337 : 3 : return error_mark_node;
6338 : : }
6339 : :
6340 : : /* It's not precisely specified how the usual arithmetic
6341 : : conversions apply to the vector types. Here, we use
6342 : : the unsigned type if one of the operands is signed and
6343 : : the other one is unsigned. */
6344 : 7166 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6345 : : {
6346 : 36 : if (!TYPE_UNSIGNED (type0))
6347 : 36 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6348 : : else
6349 : 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6350 : 36 : warning_at (location, OPT_Wsign_compare, "comparison between "
6351 : : "types %qT and %qT", type0, type1);
6352 : : }
6353 : :
6354 : 7166 : if (resultcode == SPACESHIP_EXPR)
6355 : : {
6356 : 3 : if (complain & tf_error)
6357 : 3 : sorry_at (location, "three-way comparison of vectors");
6358 : 3 : return error_mark_node;
6359 : : }
6360 : :
6361 : 7163 : if (VECTOR_BOOLEAN_TYPE_P (type0) && VECTOR_BOOLEAN_TYPE_P (type1))
6362 : : result_type = type0;
6363 : : else
6364 : : {
6365 : : /* Always construct signed integer vector type. */
6366 : 7163 : auto intmode = SCALAR_TYPE_MODE (TREE_TYPE (type0));
6367 : 7163 : auto nelts = TYPE_VECTOR_SUBPARTS (type0);
6368 : :
6369 : 14326 : intt = c_common_type_for_size (GET_MODE_BITSIZE (intmode), 0);
6370 : 7163 : if (!intt)
6371 : : {
6372 : 0 : if (complain & tf_error)
6373 : 0 : error_at (location, "could not find an integer type "
6374 : 0 : "of the same size as %qT", TREE_TYPE (type0));
6375 : 0 : return error_mark_node;
6376 : : }
6377 : 7163 : result_type = build_opaque_vector_type (intt, nelts);
6378 : : }
6379 : 7163 : return build_vec_cmp (resultcode, result_type, op0, op1);
6380 : : }
6381 : 21977442 : build_type = boolean_type_node;
6382 : 21977442 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6383 : : || code0 == ENUMERAL_TYPE)
6384 : 20474893 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6385 : : || code1 == ENUMERAL_TYPE))
6386 : : short_compare = 1;
6387 : 1502636 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6388 : 1502405 : result_type = composite_pointer_type (location,
6389 : : type0, type1, op0, op1,
6390 : : CPO_COMPARISON, complain);
6391 : 73 : else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6392 : 159 : || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6393 : 336 : || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6394 : : {
6395 : : /* Core Issue 1512 made this ill-formed. */
6396 : 191 : if (complain & tf_error)
6397 : 188 : error_at (location, "ordered comparison of pointer with "
6398 : : "integer zero (%qT and %qT)", type0, type1);
6399 : 191 : return error_mark_node;
6400 : : }
6401 : 40 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6402 : : {
6403 : 0 : result_type = type0;
6404 : 0 : if (complain & tf_error)
6405 : 0 : permerror (location, "ISO C++ forbids comparison between "
6406 : : "pointer and integer");
6407 : : else
6408 : 0 : return error_mark_node;
6409 : : }
6410 : 40 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6411 : : {
6412 : 24 : result_type = type1;
6413 : 24 : if (complain & tf_error)
6414 : 24 : permerror (location, "ISO C++ forbids comparison between "
6415 : : "pointer and integer");
6416 : : else
6417 : 0 : return error_mark_node;
6418 : : }
6419 : :
6420 : 21977251 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6421 : 1502430 : && !processing_template_decl
6422 : 23258596 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6423 : : {
6424 : 49 : op0 = cp_save_expr (op0);
6425 : 49 : op1 = cp_save_expr (op1);
6426 : :
6427 : 49 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6428 : 49 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6429 : : }
6430 : :
6431 : : break;
6432 : :
6433 : 0 : case UNORDERED_EXPR:
6434 : 0 : case ORDERED_EXPR:
6435 : 0 : case UNLT_EXPR:
6436 : 0 : case UNLE_EXPR:
6437 : 0 : case UNGT_EXPR:
6438 : 0 : case UNGE_EXPR:
6439 : 0 : case UNEQ_EXPR:
6440 : 0 : build_type = integer_type_node;
6441 : 0 : if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6442 : : {
6443 : 0 : if (complain & tf_error)
6444 : 0 : error ("unordered comparison on non-floating-point argument");
6445 : 0 : return error_mark_node;
6446 : : }
6447 : : common = 1;
6448 : : break;
6449 : :
6450 : : default:
6451 : : break;
6452 : : }
6453 : :
6454 : 122798412 : if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6455 : : || code0 == ENUMERAL_TYPE)
6456 : 101589157 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6457 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6458 : : arithmetic_types_p = 1;
6459 : : else
6460 : : {
6461 : 21465232 : arithmetic_types_p = 0;
6462 : : /* Vector arithmetic is only allowed when both sides are vectors. */
6463 : 21465232 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6464 : : {
6465 : 59626 : if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6466 : 59626 : || !vector_types_compatible_elements_p (type0, type1))
6467 : : {
6468 : 18 : if (complain & tf_error)
6469 : : {
6470 : : /* "location" already embeds the locations of the
6471 : : operands, so we don't need to add them separately
6472 : : to richloc. */
6473 : 18 : rich_location richloc (line_table, location);
6474 : 18 : binary_op_error (&richloc, code, type0, type1);
6475 : 18 : }
6476 : 18 : return error_mark_node;
6477 : : }
6478 : : arithmetic_types_p = 1;
6479 : : }
6480 : : }
6481 : : /* Determine the RESULT_TYPE, if it is not already known. */
6482 : 122798394 : if (!result_type
6483 : 122798394 : && arithmetic_types_p
6484 : 97324466 : && (shorten || common || short_compare))
6485 : : {
6486 : 97324389 : result_type = cp_common_type (type0, type1);
6487 : 97324389 : if (result_type == error_mark_node)
6488 : : {
6489 : 0 : tree t1 = type0;
6490 : 0 : tree t2 = type1;
6491 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6492 : 0 : t1 = TREE_TYPE (t1);
6493 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6494 : 0 : t2 = TREE_TYPE (t2);
6495 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6496 : : && TREE_CODE (t2) == REAL_TYPE
6497 : : && (extended_float_type_p (t1)
6498 : : || extended_float_type_p (t2))
6499 : : && cp_compare_floating_point_conversion_ranks
6500 : : (t1, t2) == 3);
6501 : 0 : if (complain & tf_error)
6502 : : {
6503 : 0 : rich_location richloc (line_table, location);
6504 : 0 : binary_op_error (&richloc, code, type0, type1);
6505 : 0 : }
6506 : 0 : return error_mark_node;
6507 : : }
6508 : 97324389 : if (complain & tf_warning)
6509 : 92091242 : do_warn_double_promotion (result_type, type0, type1,
6510 : : "implicit conversion from %qH to %qI "
6511 : : "to match other operand of binary "
6512 : : "expression", location);
6513 : 97324389 : if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6514 : 97324389 : TREE_TYPE (orig_op1), complain))
6515 : 6 : return error_mark_node;
6516 : : }
6517 : 122798388 : if (may_need_excess_precision
6518 : 91727706 : && (orig_type0 != type0 || orig_type1 != type1)
6519 : 86757 : && build_type == NULL_TREE
6520 : 86757 : && result_type)
6521 : : {
6522 : 65298 : gcc_assert (common);
6523 : 65298 : semantic_result_type = cp_common_type (orig_type0, orig_type1);
6524 : 65298 : if (semantic_result_type == error_mark_node)
6525 : : {
6526 : 0 : tree t1 = orig_type0;
6527 : 0 : tree t2 = orig_type1;
6528 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6529 : 0 : t1 = TREE_TYPE (t1);
6530 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6531 : 0 : t2 = TREE_TYPE (t2);
6532 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6533 : : && TREE_CODE (t2) == REAL_TYPE
6534 : : && (extended_float_type_p (t1)
6535 : : || extended_float_type_p (t2))
6536 : : && cp_compare_floating_point_conversion_ranks
6537 : : (t1, t2) == 3);
6538 : 0 : if (complain & tf_error)
6539 : : {
6540 : 0 : rich_location richloc (line_table, location);
6541 : 0 : binary_op_error (&richloc, code, type0, type1);
6542 : 0 : }
6543 : 0 : return error_mark_node;
6544 : : }
6545 : : }
6546 : :
6547 : 122798388 : if (code == SPACESHIP_EXPR)
6548 : : {
6549 : 258695 : iloc_sentinel s (location);
6550 : :
6551 : 258695 : tree orig_type0 = TREE_TYPE (orig_op0);
6552 : 258695 : tree_code orig_code0 = TREE_CODE (orig_type0);
6553 : 258695 : tree orig_type1 = TREE_TYPE (orig_op1);
6554 : 258695 : tree_code orig_code1 = TREE_CODE (orig_type1);
6555 : 258695 : if (!result_type || result_type == error_mark_node)
6556 : : /* Nope. */
6557 : : result_type = NULL_TREE;
6558 : 258674 : else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6559 : : /* "If one of the operands is of type bool and the other is not, the
6560 : : program is ill-formed." */
6561 : : result_type = NULL_TREE;
6562 : 258671 : else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6563 : 18 : && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6564 : : /* We only do array/function-to-pointer conversion if "at least one of
6565 : : the operands is of pointer type". */
6566 : : result_type = NULL_TREE;
6567 : 258653 : else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6568 : : /* <=> no longer supports equality relations. */
6569 : : result_type = NULL_TREE;
6570 : 258650 : else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6571 : 258686 : && !(same_type_ignoring_top_level_qualifiers_p
6572 : 36 : (orig_type0, orig_type1)))
6573 : : /* "If both operands have arithmetic types, or one operand has integral
6574 : : type and the other operand has unscoped enumeration type, the usual
6575 : : arithmetic conversions are applied to the operands." So we don't do
6576 : : arithmetic conversions if the operands both have enumeral type. */
6577 : : result_type = NULL_TREE;
6578 : 258635 : else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6579 : 258629 : || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6580 : : /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6581 : : [where one is of enumeration type and the other is of a different
6582 : : enumeration type or a floating-point type] are ill-formed. */
6583 : : result_type = NULL_TREE;
6584 : :
6585 : 258623 : if (result_type)
6586 : : {
6587 : 258623 : build_type = spaceship_type (result_type, complain);
6588 : 258623 : if (build_type == error_mark_node)
6589 : : return error_mark_node;
6590 : : }
6591 : :
6592 : 258680 : if (result_type && arithmetic_types_p)
6593 : : {
6594 : : /* If a narrowing conversion is required, other than from an integral
6595 : : type to a floating point type, the program is ill-formed. */
6596 : 99104 : bool ok = true;
6597 : 99104 : if (TREE_CODE (result_type) == REAL_TYPE
6598 : 676 : && CP_INTEGRAL_TYPE_P (orig_type0))
6599 : : /* OK */;
6600 : 99093 : else if (!check_narrowing (result_type, orig_op0, complain))
6601 : 99104 : ok = false;
6602 : 99104 : if (TREE_CODE (result_type) == REAL_TYPE
6603 : 676 : && CP_INTEGRAL_TYPE_P (orig_type1))
6604 : : /* OK */;
6605 : 99093 : else if (!check_narrowing (result_type, orig_op1, complain))
6606 : : ok = false;
6607 : 99104 : if (!ok && !(complain & tf_error))
6608 : 4 : return error_mark_node;
6609 : : }
6610 : 258695 : }
6611 : :
6612 : 122798369 : if (!result_type)
6613 : : {
6614 : 478 : if (complain & tf_error)
6615 : : {
6616 : 207 : binary_op_rich_location richloc (location,
6617 : 207 : orig_op0, orig_op1, true);
6618 : 207 : error_at (&richloc,
6619 : : "invalid operands of types %qT and %qT to binary %qO",
6620 : 207 : TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6621 : 207 : }
6622 : 478 : return error_mark_node;
6623 : : }
6624 : :
6625 : : /* If we're in a template, the only thing we need to know is the
6626 : : RESULT_TYPE. */
6627 : 122797891 : if (processing_template_decl)
6628 : : {
6629 : : /* Since the middle-end checks the type when doing a build2, we
6630 : : need to build the tree in pieces. This built tree will never
6631 : : get out of the front-end as we replace it when instantiating
6632 : : the template. */
6633 : 54300314 : tree tmp = build2 (resultcode,
6634 : : build_type ? build_type : result_type,
6635 : : NULL_TREE, op1);
6636 : 34098061 : TREE_OPERAND (tmp, 0) = op0;
6637 : 34098061 : if (semantic_result_type)
6638 : 0 : tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6639 : 34098061 : return tmp;
6640 : : }
6641 : :
6642 : : /* Remember the original type; RESULT_TYPE might be changed later on
6643 : : by shorten_binary_op. */
6644 : 88699830 : tree orig_type = result_type;
6645 : :
6646 : 88699830 : if (arithmetic_types_p)
6647 : : {
6648 : 76767326 : bool first_complex = (code0 == COMPLEX_TYPE);
6649 : 76767326 : bool second_complex = (code1 == COMPLEX_TYPE);
6650 : 76767326 : int none_complex = (!first_complex && !second_complex);
6651 : :
6652 : : /* Adapted from patch for c/24581. */
6653 : 76767326 : if (first_complex != second_complex
6654 : 166405 : && (code == PLUS_EXPR
6655 : : || code == MINUS_EXPR
6656 : 166405 : || code == MULT_EXPR
6657 : 43421 : || (code == TRUNC_DIV_EXPR && first_complex))
6658 : 152898 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6659 : 76920063 : && flag_signed_zeros)
6660 : : {
6661 : : /* An operation on mixed real/complex operands must be
6662 : : handled specially, but the language-independent code can
6663 : : more easily optimize the plain complex arithmetic if
6664 : : -fno-signed-zeros. */
6665 : 152737 : tree real_type = TREE_TYPE (result_type);
6666 : 152737 : tree real, imag;
6667 : 152737 : if (first_complex)
6668 : : {
6669 : 119689 : if (TREE_TYPE (op0) != result_type)
6670 : 6 : op0 = cp_convert_and_check (result_type, op0, complain);
6671 : 119689 : if (TREE_TYPE (op1) != real_type)
6672 : 26 : op1 = cp_convert_and_check (real_type, op1, complain);
6673 : : }
6674 : : else
6675 : : {
6676 : 33048 : if (TREE_TYPE (op0) != real_type)
6677 : 31 : op0 = cp_convert_and_check (real_type, op0, complain);
6678 : 33048 : if (TREE_TYPE (op1) != result_type)
6679 : 32 : op1 = cp_convert_and_check (result_type, op1, complain);
6680 : : }
6681 : 152737 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6682 : 0 : return error_mark_node;
6683 : 152737 : if (first_complex)
6684 : : {
6685 : 119689 : op0 = cp_save_expr (op0);
6686 : 119689 : real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6687 : 119689 : imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6688 : 119689 : switch (code)
6689 : : {
6690 : 59821 : case MULT_EXPR:
6691 : 59821 : case TRUNC_DIV_EXPR:
6692 : 59821 : op1 = cp_save_expr (op1);
6693 : 59821 : imag = build2 (resultcode, real_type, imag, op1);
6694 : : /* Fall through. */
6695 : 119689 : case PLUS_EXPR:
6696 : 119689 : case MINUS_EXPR:
6697 : 119689 : real = build2 (resultcode, real_type, real, op1);
6698 : 119689 : break;
6699 : 0 : default:
6700 : 0 : gcc_unreachable();
6701 : : }
6702 : : }
6703 : : else
6704 : : {
6705 : 33048 : op1 = cp_save_expr (op1);
6706 : 33048 : real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6707 : 33048 : imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6708 : 33048 : switch (code)
6709 : : {
6710 : 638 : case MULT_EXPR:
6711 : 638 : op0 = cp_save_expr (op0);
6712 : 638 : imag = build2 (resultcode, real_type, op0, imag);
6713 : : /* Fall through. */
6714 : 17301 : case PLUS_EXPR:
6715 : 17301 : real = build2 (resultcode, real_type, op0, real);
6716 : 17301 : break;
6717 : 15747 : case MINUS_EXPR:
6718 : 15747 : real = build2 (resultcode, real_type, op0, real);
6719 : 15747 : imag = build1 (NEGATE_EXPR, real_type, imag);
6720 : 15747 : break;
6721 : 0 : default:
6722 : 0 : gcc_unreachable();
6723 : : }
6724 : : }
6725 : 152737 : result = build2 (COMPLEX_EXPR, result_type, real, imag);
6726 : 152737 : if (semantic_result_type)
6727 : 24 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6728 : : result);
6729 : 152737 : return result;
6730 : : }
6731 : :
6732 : : /* For certain operations (which identify themselves by shorten != 0)
6733 : : if both args were extended from the same smaller type,
6734 : : do the arithmetic in that type and then extend.
6735 : :
6736 : : shorten !=0 and !=1 indicates a bitwise operation.
6737 : : For them, this optimization is safe only if
6738 : : both args are zero-extended or both are sign-extended.
6739 : : Otherwise, we might change the result.
6740 : : E.g., (short)-1 | (unsigned short)-1 is (int)-1
6741 : : but calculated in (unsigned short) it would be (unsigned short)-1. */
6742 : :
6743 : 76614589 : if (shorten && none_complex)
6744 : : {
6745 : 4141379 : final_type = result_type;
6746 : 4141379 : result_type = shorten_binary_op (result_type, op0, op1,
6747 : : shorten == -1);
6748 : : }
6749 : :
6750 : : /* Shifts can be shortened if shifting right. */
6751 : :
6752 : 76614589 : if (short_shift)
6753 : : {
6754 : 767424 : int unsigned_arg;
6755 : 767424 : tree arg0 = get_narrower (op0, &unsigned_arg);
6756 : : /* We're not really warning here but when we set short_shift we
6757 : : used fold_for_warn to fold the operand. */
6758 : 767424 : tree const_op1 = fold_for_warn (op1);
6759 : :
6760 : 767424 : final_type = result_type;
6761 : :
6762 : 767424 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
6763 : 702508 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6764 : :
6765 : 767424 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6766 : 3672 : && tree_int_cst_sgn (const_op1) > 0
6767 : : /* We can shorten only if the shift count is less than the
6768 : : number of bits in the smaller type size. */
6769 : 3672 : && compare_tree_int (const_op1,
6770 : 3672 : TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6771 : : /* We cannot drop an unsigned shift after sign-extension. */
6772 : 771086 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6773 : : {
6774 : : /* Do an unsigned shift if the operand was zero-extended. */
6775 : 3655 : result_type
6776 : 3655 : = c_common_signed_or_unsigned_type (unsigned_arg,
6777 : 3655 : TREE_TYPE (arg0));
6778 : : /* Convert value-to-be-shifted to that type. */
6779 : 3655 : if (TREE_TYPE (op0) != result_type)
6780 : 3655 : op0 = convert (result_type, op0);
6781 : : converted = 1;
6782 : : }
6783 : : }
6784 : :
6785 : : /* Comparison operations are shortened too but differently.
6786 : : They identify themselves by setting short_compare = 1. */
6787 : :
6788 : 76614589 : if (short_compare)
6789 : : {
6790 : : /* We call shorten_compare only for diagnostics. */
6791 : 25329008 : tree xop0 = fold_simple (op0);
6792 : 25329008 : tree xop1 = fold_simple (op1);
6793 : 25329008 : tree xresult_type = result_type;
6794 : 25329008 : enum tree_code xresultcode = resultcode;
6795 : 25329008 : shorten_compare (location, &xop0, &xop1, &xresult_type,
6796 : : &xresultcode);
6797 : : }
6798 : :
6799 : 51285494 : if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6800 : 25329197 : && warn_sign_compare
6801 : : /* Do not warn until the template is instantiated; we cannot
6802 : : bound the ranges of the arguments until that point. */
6803 : 331812 : && !processing_template_decl
6804 : 331812 : && (complain & tf_warning)
6805 : 324425 : && c_inhibit_evaluation_warnings == 0
6806 : : /* Even unsigned enum types promote to signed int. We don't
6807 : : want to issue -Wsign-compare warnings for this case. */
6808 : 307941 : && !enum_cast_to_int (orig_op0)
6809 : 76922530 : && !enum_cast_to_int (orig_op1))
6810 : : {
6811 : 307934 : warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6812 : : result_type, resultcode);
6813 : : }
6814 : : }
6815 : :
6816 : : /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6817 : : Then the expression will be built.
6818 : : It will be given type FINAL_TYPE if that is nonzero;
6819 : : otherwise, it will be given type RESULT_TYPE. */
6820 : 88547093 : if (! converted)
6821 : : {
6822 : 85064699 : warning_sentinel w (warn_sign_conversion, short_compare);
6823 : 85064699 : if (!same_type_p (TREE_TYPE (op0), result_type))
6824 : 3620329 : op0 = cp_convert_and_check (result_type, op0, complain);
6825 : 85064699 : if (!same_type_p (TREE_TYPE (op1), result_type))
6826 : 15474035 : op1 = cp_convert_and_check (result_type, op1, complain);
6827 : :
6828 : 85064699 : if (op0 == error_mark_node || op1 == error_mark_node)
6829 : 65 : return error_mark_node;
6830 : 85064699 : }
6831 : :
6832 : 88547028 : if (build_type == NULL_TREE)
6833 : 58795716 : build_type = result_type;
6834 : :
6835 : 88547028 : if (doing_shift
6836 : 3481812 : && flag_strong_eval_order == 2
6837 : 3391510 : && TREE_SIDE_EFFECTS (op1)
6838 : 88573599 : && !processing_template_decl)
6839 : : {
6840 : : /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6841 : : op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6842 : 26571 : op0 = cp_save_expr (op0);
6843 : 26571 : instrument_expr = op0;
6844 : : }
6845 : :
6846 : 88547028 : if (sanitize_flags_p ((SANITIZE_SHIFT
6847 : : | SANITIZE_DIVIDE
6848 : : | SANITIZE_FLOAT_DIVIDE
6849 : : | SANITIZE_SI_OVERFLOW))
6850 : 13389 : && current_function_decl != NULL_TREE
6851 : 11012 : && !processing_template_decl
6852 : 88558040 : && (doing_div_or_mod || doing_shift))
6853 : : {
6854 : : /* OP0 and/or OP1 might have side-effects. */
6855 : 924 : op0 = cp_save_expr (op0);
6856 : 924 : op1 = cp_save_expr (op1);
6857 : 924 : op0 = fold_non_dependent_expr (op0, complain);
6858 : 924 : op1 = fold_non_dependent_expr (op1, complain);
6859 : 924 : tree instrument_expr1 = NULL_TREE;
6860 : 924 : if (doing_div_or_mod
6861 : 924 : && sanitize_flags_p (SANITIZE_DIVIDE
6862 : : | SANITIZE_FLOAT_DIVIDE
6863 : : | SANITIZE_SI_OVERFLOW))
6864 : : {
6865 : : /* For diagnostics we want to use the promoted types without
6866 : : shorten_binary_op. So convert the arguments to the
6867 : : original result_type. */
6868 : 465 : tree cop0 = op0;
6869 : 465 : tree cop1 = op1;
6870 : 465 : if (TREE_TYPE (cop0) != orig_type)
6871 : 6 : cop0 = cp_convert (orig_type, op0, complain);
6872 : 465 : if (TREE_TYPE (cop1) != orig_type)
6873 : 39 : cop1 = cp_convert (orig_type, op1, complain);
6874 : 465 : instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6875 : : }
6876 : 459 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6877 : 378 : instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6878 : 924 : if (instrument_expr != NULL)
6879 : 18 : instrument_expr = add_stmt_to_compound (instrument_expr,
6880 : : instrument_expr1);
6881 : : else
6882 : 906 : instrument_expr = instrument_expr1;
6883 : : }
6884 : :
6885 : 88547028 : result = build2_loc (location, resultcode, build_type, op0, op1);
6886 : 88547028 : if (final_type != 0)
6887 : 4908803 : result = cp_convert (final_type, result, complain);
6888 : :
6889 : 88547028 : if (instrument_expr != NULL)
6890 : 27069 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6891 : : instrument_expr, result);
6892 : :
6893 : 88547028 : if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6894 : 222582 : result = get_target_expr (result, complain);
6895 : :
6896 : 88547028 : if (semantic_result_type)
6897 : 65274 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
6898 : :
6899 : 88547028 : if (!c_inhibit_evaluation_warnings)
6900 : : {
6901 : 79225549 : if (!processing_template_decl)
6902 : : {
6903 : 79225549 : op0 = cp_fully_fold (op0);
6904 : : /* Only consider the second argument if the first isn't overflowed. */
6905 : 79225549 : if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6906 : : return result;
6907 : 21029958 : op1 = cp_fully_fold (op1);
6908 : 21029958 : if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6909 : : return result;
6910 : : }
6911 : 0 : else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6912 : 0 : || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6913 : : return result;
6914 : :
6915 : 15576690 : tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6916 : 15576690 : if (TREE_OVERFLOW_P (result_ovl))
6917 : 190 : overflow_warning (location, result_ovl);
6918 : : }
6919 : :
6920 : : return result;
6921 : : }
6922 : :
6923 : : /* Build a VEC_PERM_EXPR.
6924 : : This is a simple wrapper for c_build_vec_perm_expr. */
6925 : : tree
6926 : 17494 : build_x_vec_perm_expr (location_t loc,
6927 : : tree arg0, tree arg1, tree arg2,
6928 : : tsubst_flags_t complain)
6929 : : {
6930 : 17494 : tree orig_arg0 = arg0;
6931 : 17494 : tree orig_arg1 = arg1;
6932 : 17494 : tree orig_arg2 = arg2;
6933 : 17494 : if (processing_template_decl)
6934 : : {
6935 : 19 : if (type_dependent_expression_p (arg0)
6936 : 13 : || type_dependent_expression_p (arg1)
6937 : 32 : || type_dependent_expression_p (arg2))
6938 : 6 : return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6939 : : }
6940 : 17488 : tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6941 : 17488 : if (processing_template_decl && exp != error_mark_node)
6942 : 13 : return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6943 : 13 : orig_arg1, orig_arg2);
6944 : : return exp;
6945 : : }
6946 : :
6947 : : /* Build a VEC_PERM_EXPR.
6948 : : This is a simple wrapper for c_build_shufflevector. */
6949 : : tree
6950 : 41175 : build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6951 : : tsubst_flags_t complain)
6952 : : {
6953 : 41175 : tree arg0 = (*args)[0];
6954 : 41175 : tree arg1 = (*args)[1];
6955 : 41175 : if (processing_template_decl)
6956 : : {
6957 : 43 : for (unsigned i = 0; i < args->length (); ++i)
6958 : 40 : if (i <= 1
6959 : 40 : ? type_dependent_expression_p ((*args)[i])
6960 : 9 : : instantiation_dependent_expression_p ((*args)[i]))
6961 : : {
6962 : 22 : tree exp = build_min_nt_call_vec (NULL, args);
6963 : 22 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6964 : 22 : return exp;
6965 : : }
6966 : : }
6967 : 41153 : auto_vec<tree, 16> mask;
6968 : 558709 : for (unsigned i = 2; i < args->length (); ++i)
6969 : : {
6970 : 517556 : tree idx = fold_non_dependent_expr ((*args)[i], complain);
6971 : 517556 : mask.safe_push (idx);
6972 : : }
6973 : 41153 : tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6974 : 41153 : if (processing_template_decl && exp != error_mark_node)
6975 : : {
6976 : 3 : exp = build_min_non_dep_call_vec (exp, NULL, args);
6977 : 3 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6978 : : }
6979 : 41153 : return exp;
6980 : 41153 : }
6981 : :
6982 : : /* Return a tree for the sum or difference (RESULTCODE says which)
6983 : : of pointer PTROP and integer INTOP. */
6984 : :
6985 : : static tree
6986 : 4582742 : cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6987 : : tree intop, tsubst_flags_t complain)
6988 : : {
6989 : 4582742 : tree res_type = TREE_TYPE (ptrop);
6990 : :
6991 : : /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6992 : : in certain circumstance (when it's valid to do so). So we need
6993 : : to make sure it's complete. We don't need to check here, if we
6994 : : can actually complete it at all, as those checks will be done in
6995 : : pointer_int_sum() anyway. */
6996 : 4582742 : complete_type (TREE_TYPE (res_type));
6997 : :
6998 : 4582742 : return pointer_int_sum (loc, resultcode, ptrop,
6999 : 4582742 : intop, complain & tf_warning_or_error);
7000 : : }
7001 : :
7002 : : /* Return a tree for the difference of pointers OP0 and OP1.
7003 : : The resulting tree has type int. If POINTER_SUBTRACT sanitization is
7004 : : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
7005 : :
7006 : : static tree
7007 : 1348748 : pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
7008 : : tsubst_flags_t complain, tree *instrument_expr)
7009 : : {
7010 : 1348748 : tree result, inttype;
7011 : 1348748 : tree restype = ptrdiff_type_node;
7012 : 1348748 : tree target_type = TREE_TYPE (ptrtype);
7013 : :
7014 : 1348748 : if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
7015 : 12 : return error_mark_node;
7016 : :
7017 : 1348736 : if (VOID_TYPE_P (target_type))
7018 : : {
7019 : 0 : if (complain & tf_error)
7020 : 0 : permerror (loc, "ISO C++ forbids using pointer of "
7021 : : "type %<void *%> in subtraction");
7022 : : else
7023 : 0 : return error_mark_node;
7024 : : }
7025 : 1348736 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
7026 : : {
7027 : 15 : if (complain & tf_error)
7028 : 3 : permerror (loc, "ISO C++ forbids using pointer to "
7029 : : "a function in subtraction");
7030 : : else
7031 : 12 : return error_mark_node;
7032 : : }
7033 : 1348724 : if (TREE_CODE (target_type) == METHOD_TYPE)
7034 : : {
7035 : 0 : if (complain & tf_error)
7036 : 0 : permerror (loc, "ISO C++ forbids using pointer to "
7037 : : "a method in subtraction");
7038 : : else
7039 : 0 : return error_mark_node;
7040 : : }
7041 : 2697448 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
7042 : 1348724 : TREE_TYPE (TREE_TYPE (op0)),
7043 : : !(complain & tf_error))
7044 : 2697448 : || !verify_type_context (loc, TCTX_POINTER_ARITH,
7045 : 1348724 : TREE_TYPE (TREE_TYPE (op1)),
7046 : : !(complain & tf_error)))
7047 : 0 : return error_mark_node;
7048 : :
7049 : : /* Determine integer type result of the subtraction. This will usually
7050 : : be the same as the result type (ptrdiff_t), but may need to be a wider
7051 : : type if pointers for the address space are wider than ptrdiff_t. */
7052 : 1348724 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
7053 : 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
7054 : : else
7055 : : inttype = restype;
7056 : :
7057 : 1348724 : if (!processing_template_decl
7058 : 1348724 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
7059 : : {
7060 : 84 : op0 = save_expr (op0);
7061 : 84 : op1 = save_expr (op1);
7062 : :
7063 : 84 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
7064 : 84 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
7065 : : }
7066 : :
7067 : : /* First do the subtraction, then build the divide operator
7068 : : and only convert at the very end.
7069 : : Do not do default conversions in case restype is a short type. */
7070 : :
7071 : : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
7072 : : pointers. If some platform cannot provide that, or has a larger
7073 : : ptrdiff_type to support differences larger than half the address
7074 : : space, cast the pointers to some larger integer type and do the
7075 : : computations in that type. */
7076 : 1348724 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
7077 : 0 : op0 = cp_build_binary_op (loc,
7078 : : MINUS_EXPR,
7079 : : cp_convert (inttype, op0, complain),
7080 : : cp_convert (inttype, op1, complain),
7081 : : complain);
7082 : : else
7083 : 1348724 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
7084 : :
7085 : : /* This generates an error if op1 is a pointer to an incomplete type. */
7086 : 1348724 : if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
7087 : : {
7088 : 0 : if (complain & tf_error)
7089 : 0 : error_at (loc, "invalid use of a pointer to an incomplete type in "
7090 : : "pointer arithmetic");
7091 : : else
7092 : 0 : return error_mark_node;
7093 : : }
7094 : :
7095 : 1348724 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
7096 : : {
7097 : 15 : if (complain & tf_error)
7098 : 15 : error_at (loc, "arithmetic on pointer to an empty aggregate");
7099 : : else
7100 : 0 : return error_mark_node;
7101 : : }
7102 : :
7103 : 2697445 : op1 = (TYPE_PTROB_P (ptrtype)
7104 : 2697445 : ? size_in_bytes_loc (loc, target_type)
7105 : : : integer_one_node);
7106 : :
7107 : : /* Do the division. */
7108 : :
7109 : 1348724 : result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
7110 : : cp_convert (inttype, op1, complain));
7111 : 1348724 : return cp_convert (restype, result, complain);
7112 : : }
7113 : :
7114 : : /* Construct and perhaps optimize a tree representation
7115 : : for a unary operation. CODE, a tree_code, specifies the operation
7116 : : and XARG is the operand. */
7117 : :
7118 : : tree
7119 : 58375083 : build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
7120 : : tree lookups, tsubst_flags_t complain)
7121 : : {
7122 : 58375083 : tree orig_expr = xarg;
7123 : 58375083 : tree exp;
7124 : 58375083 : int ptrmem = 0;
7125 : 58375083 : tree overload = NULL_TREE;
7126 : :
7127 : 58375083 : if (processing_template_decl)
7128 : : {
7129 : 40549071 : if (type_dependent_expression_p (xarg))
7130 : : {
7131 : 26903572 : tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
7132 : 26903572 : TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
7133 : 26903572 : return e;
7134 : : }
7135 : : }
7136 : :
7137 : 31471511 : exp = NULL_TREE;
7138 : :
7139 : : /* [expr.unary.op] says:
7140 : :
7141 : : The address of an object of incomplete type can be taken.
7142 : :
7143 : : (And is just the ordinary address operator, not an overloaded
7144 : : "operator &".) However, if the type is a template
7145 : : specialization, we must complete the type at this point so that
7146 : : an overloaded "operator &" will be available if required. */
7147 : 31471511 : if (code == ADDR_EXPR
7148 : 3486521 : && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
7149 : 34957631 : && ((CLASS_TYPE_P (TREE_TYPE (xarg))
7150 : 1632655 : && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
7151 : 3483466 : || (TREE_CODE (xarg) == OFFSET_REF)))
7152 : : /* Don't look for a function. */;
7153 : : else
7154 : 31402158 : exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
7155 : : NULL_TREE, lookups, &overload, complain);
7156 : :
7157 : 31471511 : if (!exp && code == ADDR_EXPR)
7158 : : {
7159 : 3486229 : if (is_overloaded_fn (xarg))
7160 : : {
7161 : 374422 : tree fn = get_first_fn (xarg);
7162 : 748844 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
7163 : : {
7164 : 12 : if (complain & tf_error)
7165 : 21 : error_at (loc, DECL_CONSTRUCTOR_P (fn)
7166 : : ? G_("taking address of constructor %qD")
7167 : : : G_("taking address of destructor %qD"),
7168 : : fn);
7169 : 12 : return error_mark_node;
7170 : : }
7171 : : }
7172 : :
7173 : : /* A pointer to member-function can be formed only by saying
7174 : : &X::mf. */
7175 : 3486211 : if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
7176 : 3549121 : && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
7177 : : {
7178 : 9 : if (TREE_CODE (xarg) != OFFSET_REF
7179 : 9 : || !TYPE_P (TREE_OPERAND (xarg, 0)))
7180 : : {
7181 : 9 : if (complain & tf_error)
7182 : : {
7183 : 9 : auto_diagnostic_group d;
7184 : 9 : error_at (loc, "invalid use of %qE to form a "
7185 : : "pointer-to-member-function", xarg.get_value ());
7186 : 9 : if (TREE_CODE (xarg) != OFFSET_REF)
7187 : 6 : inform (loc, " a qualified-id is required");
7188 : 9 : }
7189 : 9 : return error_mark_node;
7190 : : }
7191 : : else
7192 : : {
7193 : 0 : if (complain & tf_error)
7194 : 0 : error_at (loc, "parentheses around %qE cannot be used to "
7195 : : "form a pointer-to-member-function",
7196 : : xarg.get_value ());
7197 : : else
7198 : 0 : return error_mark_node;
7199 : 0 : PTRMEM_OK_P (xarg) = 1;
7200 : : }
7201 : : }
7202 : :
7203 : 3486208 : if (TREE_CODE (xarg) == OFFSET_REF)
7204 : : {
7205 : 66684 : ptrmem = PTRMEM_OK_P (xarg);
7206 : :
7207 : 0 : if (!ptrmem && !flag_ms_extensions
7208 : 66684 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
7209 : : {
7210 : : /* A single non-static member, make sure we don't allow a
7211 : : pointer-to-member. */
7212 : 0 : xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
7213 : 0 : TREE_OPERAND (xarg, 0),
7214 : 0 : ovl_make (TREE_OPERAND (xarg, 1)));
7215 : 0 : PTRMEM_OK_P (xarg) = ptrmem;
7216 : : }
7217 : : }
7218 : :
7219 : 6972416 : exp = cp_build_addr_expr_strict (xarg, complain);
7220 : :
7221 : 3486208 : if (TREE_CODE (exp) == PTRMEM_CST)
7222 : 65751 : PTRMEM_CST_LOCATION (exp) = loc;
7223 : : else
7224 : 3420457 : protected_set_expr_location (exp, loc);
7225 : : }
7226 : :
7227 : 31471490 : if (processing_template_decl && exp != error_mark_node)
7228 : : {
7229 : 13645351 : if (overload != NULL_TREE)
7230 : 167239 : return (build_min_non_dep_op_overload
7231 : 167239 : (code, exp, overload, orig_expr, integer_zero_node));
7232 : :
7233 : 13478112 : exp = build_min_non_dep (code, exp, orig_expr,
7234 : : /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
7235 : : }
7236 : 31304251 : if (TREE_CODE (exp) == ADDR_EXPR)
7237 : 2688574 : PTRMEM_OK_P (exp) = ptrmem;
7238 : : return exp;
7239 : : }
7240 : :
7241 : : /* Construct and perhaps optimize a tree representation
7242 : : for __builtin_addressof operation. ARG specifies the operand. */
7243 : :
7244 : : tree
7245 : 510969 : cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7246 : : {
7247 : 510969 : tree orig_expr = arg;
7248 : :
7249 : 510969 : if (processing_template_decl)
7250 : : {
7251 : 189698 : if (type_dependent_expression_p (arg))
7252 : 189698 : return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7253 : : }
7254 : :
7255 : 642542 : tree exp = cp_build_addr_expr_strict (arg, complain);
7256 : :
7257 : 321271 : if (processing_template_decl && exp != error_mark_node)
7258 : 0 : exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
7259 : : return exp;
7260 : : }
7261 : :
7262 : : /* Like c_common_truthvalue_conversion, but handle pointer-to-member
7263 : : constants, where a null value is represented by an INTEGER_CST of
7264 : : -1. */
7265 : :
7266 : : tree
7267 : 7552124 : cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7268 : : {
7269 : 7552124 : tree type = TREE_TYPE (expr);
7270 : 7552124 : location_t loc = cp_expr_loc_or_input_loc (expr);
7271 : 6419491 : if (TYPE_PTR_OR_PTRMEM_P (type)
7272 : : /* Avoid ICE on invalid use of non-static member function. */
7273 : 13971329 : || TREE_CODE (expr) == FUNCTION_DECL)
7274 : 1132919 : return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
7275 : : else
7276 : 6419205 : return c_common_truthvalue_conversion (loc, expr);
7277 : : }
7278 : :
7279 : : /* Returns EXPR contextually converted to bool. */
7280 : :
7281 : : tree
7282 : 49047719 : contextual_conv_bool (tree expr, tsubst_flags_t complain)
7283 : : {
7284 : 49047719 : return perform_implicit_conversion_flags (boolean_type_node, expr,
7285 : 49047719 : complain, LOOKUP_NORMAL);
7286 : : }
7287 : :
7288 : : /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7289 : : is a low-level function; most callers should use maybe_convert_cond. */
7290 : :
7291 : : tree
7292 : 43827672 : condition_conversion (tree expr)
7293 : : {
7294 : 43827672 : tree t = contextual_conv_bool (expr, tf_warning_or_error);
7295 : 43827672 : if (!processing_template_decl)
7296 : 22399464 : t = fold_build_cleanup_point_expr (boolean_type_node, t);
7297 : 43827672 : return t;
7298 : : }
7299 : :
7300 : : /* Returns the address of T. This function will fold away
7301 : : ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7302 : : most places should use cp_build_addr_expr instead. */
7303 : :
7304 : : tree
7305 : 292884519 : build_address (tree t)
7306 : : {
7307 : 292884519 : if (error_operand_p (t) || !cxx_mark_addressable (t))
7308 : 24 : return error_mark_node;
7309 : 292884495 : gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7310 : : || processing_template_decl);
7311 : 292884495 : t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7312 : 292884495 : if (TREE_CODE (t) != ADDR_EXPR)
7313 : 50843862 : t = rvalue (t);
7314 : : return t;
7315 : : }
7316 : :
7317 : : /* Return a NOP_EXPR converting EXPR to TYPE. */
7318 : :
7319 : : tree
7320 : 488052763 : build_nop (tree type, tree expr MEM_STAT_DECL)
7321 : : {
7322 : 488052763 : if (type == error_mark_node || error_operand_p (expr))
7323 : : return expr;
7324 : 488052685 : return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr PASS_MEM_STAT);
7325 : : }
7326 : :
7327 : : /* Take the address of ARG, whatever that means under C++ semantics.
7328 : : If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7329 : : and class rvalues as well.
7330 : :
7331 : : Nothing should call this function directly; instead, callers should use
7332 : : cp_build_addr_expr or cp_build_addr_expr_strict. */
7333 : :
7334 : : static tree
7335 : 227212746 : cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7336 : : {
7337 : 227212746 : tree argtype;
7338 : 227212746 : tree val;
7339 : :
7340 : 227212746 : if (!arg || error_operand_p (arg))
7341 : 12 : return error_mark_node;
7342 : :
7343 : 227212734 : arg = mark_lvalue_use (arg);
7344 : 227212734 : if (error_operand_p (arg))
7345 : 3 : return error_mark_node;
7346 : :
7347 : 227212731 : argtype = lvalue_type (arg);
7348 : 227212731 : location_t loc = cp_expr_loc_or_input_loc (arg);
7349 : :
7350 : 227212731 : gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7351 : :
7352 : 13480485 : if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7353 : 227212925 : && !really_overloaded_fn (arg))
7354 : : {
7355 : : /* They're trying to take the address of a unique non-static
7356 : : member function. This is ill-formed (except in MS-land),
7357 : : but let's try to DTRT.
7358 : : Note: We only handle unique functions here because we don't
7359 : : want to complain if there's a static overload; non-unique
7360 : : cases will be handled by instantiate_type. But we need to
7361 : : handle this case here to allow casts on the resulting PMF.
7362 : : We could defer this in non-MS mode, but it's easier to give
7363 : : a useful error here. */
7364 : :
7365 : : /* Inside constant member functions, the `this' pointer
7366 : : contains an extra const qualifier. TYPE_MAIN_VARIANT
7367 : : is used here to remove this const from the diagnostics
7368 : : and the created OFFSET_REF. */
7369 : 70 : tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7370 : 70 : tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7371 : 70 : if (!mark_used (fn, complain) && !(complain & tf_error))
7372 : 0 : return error_mark_node;
7373 : : /* Until microsoft headers are known to incorrectly take the address of
7374 : : unqualified xobj member functions we should not support this
7375 : : extension.
7376 : : See comment in class.cc:resolve_address_of_overloaded_function for
7377 : : the extended reasoning. */
7378 : 70 : if (!flag_ms_extensions || DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7379 : : {
7380 : 64 : auto_diagnostic_group d;
7381 : 64 : tree name = DECL_NAME (fn);
7382 : 64 : if (!(complain & tf_error))
7383 : 0 : return error_mark_node;
7384 : 64 : else if (current_class_type
7385 : 64 : && TREE_OPERAND (arg, 0) == current_class_ref)
7386 : : /* An expression like &memfn. */
7387 : 31 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7388 : 21 : permerror (loc,
7389 : : "ISO C++ forbids taking the address of an unqualified"
7390 : : " or parenthesized non-static member function to form"
7391 : : " a pointer to member function. Say %<&%T::%D%>",
7392 : : base, name);
7393 : : else
7394 : 10 : error_at (loc,
7395 : : "ISO C++ forbids taking the address of an unqualified"
7396 : : " or parenthesized non-static member function to form"
7397 : : " a pointer to explicit object member function");
7398 : : else
7399 : 33 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7400 : 9 : permerror (loc,
7401 : : "ISO C++ forbids taking the address of a bound member"
7402 : : " function to form a pointer to member function."
7403 : : " Say %<&%T::%D%>",
7404 : : base, name);
7405 : : else
7406 : 24 : error_at (loc,
7407 : : "ISO C++ forbids taking the address of a bound member"
7408 : : " function to form a pointer to explicit object member"
7409 : : " function");
7410 : 64 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7411 : 34 : inform (loc,
7412 : : "a pointer to explicit object member function can only be "
7413 : : "formed with %<&%T::%D%>", base, name);
7414 : 64 : }
7415 : 70 : arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7416 : : }
7417 : :
7418 : : /* Uninstantiated types are all functions. Taking the
7419 : : address of a function is a no-op, so just return the
7420 : : argument. */
7421 : 227212731 : if (type_unknown_p (arg))
7422 : 1936 : return build1 (ADDR_EXPR, unknown_type_node, arg);
7423 : :
7424 : 227210795 : if (TREE_CODE (arg) == OFFSET_REF)
7425 : : /* We want a pointer to member; bypass all the code for actually taking
7426 : : the address of something. */
7427 : 65876 : goto offset_ref;
7428 : :
7429 : : /* Anything not already handled and not a true memory reference
7430 : : is an error. */
7431 : 227144919 : if (!FUNC_OR_METHOD_TYPE_P (argtype))
7432 : : {
7433 : 118879099 : cp_lvalue_kind kind = lvalue_kind (arg);
7434 : 118879099 : if (kind == clk_none)
7435 : : {
7436 : 23 : if (complain & tf_error)
7437 : 23 : lvalue_error (loc, lv_addressof);
7438 : 23 : return error_mark_node;
7439 : : }
7440 : 118879076 : if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7441 : : {
7442 : 36 : if (!(complain & tf_error))
7443 : 9 : return error_mark_node;
7444 : : /* Make this a permerror because we used to accept it. */
7445 : 27 : permerror (loc, "taking address of rvalue");
7446 : : }
7447 : : }
7448 : :
7449 : 227144887 : if (TYPE_REF_P (argtype))
7450 : : {
7451 : 236 : tree type = build_pointer_type (TREE_TYPE (argtype));
7452 : 236 : arg = build1 (CONVERT_EXPR, type, arg);
7453 : 236 : return arg;
7454 : : }
7455 : 227144651 : else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7456 : : {
7457 : : /* ARM $3.4 */
7458 : : /* Apparently a lot of autoconf scripts for C++ packages do this,
7459 : : so only complain if -Wpedantic. */
7460 : 9 : if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7461 : 9 : pedwarn (loc, OPT_Wpedantic,
7462 : : "ISO C++ forbids taking address of function %<::main%>");
7463 : 0 : else if (flag_pedantic_errors)
7464 : 0 : return error_mark_node;
7465 : : }
7466 : :
7467 : : /* Let &* cancel out to simplify resulting code. */
7468 : 227144651 : if (INDIRECT_REF_P (arg))
7469 : : {
7470 : 76740915 : arg = TREE_OPERAND (arg, 0);
7471 : 76740915 : if (TYPE_REF_P (TREE_TYPE (arg)))
7472 : : {
7473 : 46044975 : tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7474 : 46044975 : arg = build1 (CONVERT_EXPR, type, arg);
7475 : : }
7476 : : else
7477 : : /* Don't let this be an lvalue. */
7478 : 30695940 : arg = rvalue (arg);
7479 : 76740915 : return arg;
7480 : : }
7481 : :
7482 : : /* Handle complex lvalues (when permitted)
7483 : : by reduction to simpler cases. */
7484 : 150403736 : val = unary_complex_lvalue (ADDR_EXPR, arg);
7485 : 150403736 : if (val != 0)
7486 : : return val;
7487 : :
7488 : 149901205 : switch (TREE_CODE (arg))
7489 : : {
7490 : 0 : CASE_CONVERT:
7491 : 0 : case FLOAT_EXPR:
7492 : 0 : case FIX_TRUNC_EXPR:
7493 : : /* We should have handled this above in the lvalue_kind check. */
7494 : 0 : gcc_unreachable ();
7495 : 66389 : break;
7496 : :
7497 : 66389 : case BASELINK:
7498 : 66389 : arg = BASELINK_FUNCTIONS (arg);
7499 : : /* Fall through. */
7500 : :
7501 : 66389 : case OVERLOAD:
7502 : 66389 : arg = OVL_FIRST (arg);
7503 : : break;
7504 : :
7505 : 65876 : case OFFSET_REF:
7506 : 65876 : offset_ref:
7507 : : /* Turn a reference to a non-static data member into a
7508 : : pointer-to-member. */
7509 : 65876 : {
7510 : 65876 : tree type;
7511 : 65876 : tree t;
7512 : :
7513 : 65876 : gcc_assert (PTRMEM_OK_P (arg));
7514 : :
7515 : 65876 : t = TREE_OPERAND (arg, 1);
7516 : 65876 : if (TYPE_REF_P (TREE_TYPE (t)))
7517 : : {
7518 : 15 : if (complain & tf_error)
7519 : 15 : error_at (loc,
7520 : : "cannot create pointer to reference member %qD", t);
7521 : 15 : return error_mark_node;
7522 : : }
7523 : :
7524 : : /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7525 : 65861 : if (TREE_CODE (t) == FUNCTION_DECL
7526 : 63038 : && !DECL_PURE_VIRTUAL_P (t)
7527 : 128878 : && !mark_used (t, complain) && !(complain & tf_error))
7528 : 3 : return error_mark_node;
7529 : :
7530 : : /* Pull out the function_decl for a single xobj member function, and
7531 : : let the rest of this function handle it. This is similar to how
7532 : : static member functions are handled in the BASELINK case above. */
7533 : 65858 : if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7534 : : {
7535 : : arg = t;
7536 : : break;
7537 : : }
7538 : :
7539 : 65802 : type = build_ptrmem_type (context_for_name_lookup (t),
7540 : 65802 : TREE_TYPE (t));
7541 : 65802 : t = make_ptrmem_cst (type, t);
7542 : 65802 : return t;
7543 : : }
7544 : :
7545 : : default:
7546 : : break;
7547 : : }
7548 : :
7549 : 149901261 : if (argtype != error_mark_node)
7550 : 149901261 : argtype = build_pointer_type (argtype);
7551 : :
7552 : 149901261 : if (bitfield_p (arg))
7553 : : {
7554 : 33 : if (complain & tf_error)
7555 : 33 : error_at (loc, "attempt to take address of bit-field");
7556 : 33 : return error_mark_node;
7557 : : }
7558 : :
7559 : : /* In a template, we are processing a non-dependent expression
7560 : : so we can just form an ADDR_EXPR with the correct type. */
7561 : 149901228 : if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7562 : : {
7563 : 136728549 : if (!mark_single_function (arg, complain))
7564 : 3 : return error_mark_node;
7565 : 136728546 : val = build_address (arg);
7566 : 136728546 : if (TREE_CODE (arg) == OFFSET_REF)
7567 : 0 : PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7568 : : }
7569 : 13172679 : else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7570 : : {
7571 : 27 : tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7572 : :
7573 : 27 : if (TREE_CODE (fn) == OVERLOAD && OVL_SINGLE_P (fn))
7574 : 27 : fn = OVL_FIRST (fn);
7575 : :
7576 : : /* We can only get here with a single static member
7577 : : function. */
7578 : 27 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7579 : : && DECL_STATIC_FUNCTION_P (fn));
7580 : 27 : if (!mark_used (fn, complain) && !(complain & tf_error))
7581 : 0 : return error_mark_node;
7582 : 27 : val = build_address (fn);
7583 : 27 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7584 : : /* Do not lose object's side effects. */
7585 : 12 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7586 : 12 : TREE_OPERAND (arg, 0), val);
7587 : : }
7588 : : else
7589 : : {
7590 : 13172652 : tree object = TREE_OPERAND (arg, 0);
7591 : 13172652 : tree field = TREE_OPERAND (arg, 1);
7592 : 13172652 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7593 : : (TREE_TYPE (object), decl_type_context (field)));
7594 : 13172652 : val = build_address (arg);
7595 : : }
7596 : :
7597 : 149901225 : if (TYPE_PTR_P (argtype)
7598 : 149901225 : && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7599 : : {
7600 : 1063 : build_ptrmemfunc_type (argtype);
7601 : 1063 : val = build_ptrmemfunc (argtype, val, 0,
7602 : : /*c_cast_p=*/false,
7603 : : complain);
7604 : : }
7605 : :
7606 : : /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7607 : 149901225 : if (TREE_CODE (val) == ADDR_EXPR
7608 : 149901225 : && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7609 : 105871227 : SET_EXPR_LOCATION (val, input_location);
7610 : :
7611 : : return val;
7612 : : }
7613 : :
7614 : : /* Take the address of ARG if it has one, even if it's an rvalue. */
7615 : :
7616 : : tree
7617 : 223405267 : cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7618 : : {
7619 : 223405267 : return cp_build_addr_expr_1 (arg, 0, complain);
7620 : : }
7621 : :
7622 : : /* Take the address of ARG, but only if it's an lvalue. */
7623 : :
7624 : : static tree
7625 : 3807479 : cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7626 : : {
7627 : 3807479 : return cp_build_addr_expr_1 (arg, 1, complain);
7628 : : }
7629 : :
7630 : : /* C++: Must handle pointers to members.
7631 : :
7632 : : Perhaps type instantiation should be extended to handle conversion
7633 : : from aggregates to types we don't yet know we want? (Or are those
7634 : : cases typically errors which should be reported?)
7635 : :
7636 : : NOCONVERT suppresses the default promotions (such as from short to int). */
7637 : :
7638 : : tree
7639 : 28059855 : cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7640 : : tsubst_flags_t complain)
7641 : : {
7642 : : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7643 : 28059855 : tree arg = xarg;
7644 : 28059855 : location_t location = cp_expr_loc_or_input_loc (arg);
7645 : 28059855 : tree argtype = 0;
7646 : 28059855 : tree eptype = NULL_TREE;
7647 : 28059855 : const char *errstring = NULL;
7648 : 28059855 : tree val;
7649 : 28059855 : const char *invalid_op_diag;
7650 : :
7651 : 28059855 : if (!arg || error_operand_p (arg))
7652 : 0 : return error_mark_node;
7653 : :
7654 : 28059855 : arg = resolve_nondeduced_context (arg, complain);
7655 : :
7656 : 56119710 : if ((invalid_op_diag
7657 : 28059855 : = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7658 : : ? CONVERT_EXPR
7659 : : : code),
7660 : 28059855 : TREE_TYPE (arg))))
7661 : : {
7662 : 0 : if (complain & tf_error)
7663 : 0 : error (invalid_op_diag);
7664 : 0 : return error_mark_node;
7665 : : }
7666 : :
7667 : 28059855 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7668 : : {
7669 : 10990 : eptype = TREE_TYPE (arg);
7670 : 10990 : arg = TREE_OPERAND (arg, 0);
7671 : : }
7672 : :
7673 : 28059855 : switch (code)
7674 : : {
7675 : 2457776 : case UNARY_PLUS_EXPR:
7676 : 2457776 : case NEGATE_EXPR:
7677 : 2457776 : {
7678 : 2457776 : int flags = WANT_ARITH | WANT_ENUM;
7679 : : /* Unary plus (but not unary minus) is allowed on pointers. */
7680 : 2457776 : if (code == UNARY_PLUS_EXPR)
7681 : 167660 : flags |= WANT_POINTER;
7682 : 2457776 : arg = build_expr_type_conversion (flags, arg, true);
7683 : 2457776 : if (!arg)
7684 : 33 : errstring = (code == NEGATE_EXPR
7685 : 27 : ? _("wrong type argument to unary minus")
7686 : 6 : : _("wrong type argument to unary plus"));
7687 : : else
7688 : : {
7689 : 2457743 : if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7690 : 635277 : arg = cp_perform_integral_promotions (arg, complain);
7691 : :
7692 : : /* Make sure the result is not an lvalue: a unary plus or minus
7693 : : expression is always a rvalue. */
7694 : 2457743 : arg = rvalue (arg);
7695 : : }
7696 : : }
7697 : : break;
7698 : :
7699 : 899799 : case BIT_NOT_EXPR:
7700 : 899799 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7701 : : {
7702 : 15 : code = CONJ_EXPR;
7703 : 15 : if (!noconvert)
7704 : : {
7705 : 15 : arg = cp_default_conversion (arg, complain);
7706 : 15 : if (arg == error_mark_node)
7707 : : return error_mark_node;
7708 : : }
7709 : : }
7710 : 899784 : else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7711 : : | WANT_VECTOR_OR_COMPLEX,
7712 : : arg, true)))
7713 : 13 : errstring = _("wrong type argument to bit-complement");
7714 : 899771 : else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7715 : : {
7716 : : /* Warn if the expression has boolean value. */
7717 : 899536 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7718 : 899536 : && (complain & tf_warning))
7719 : : {
7720 : 61 : auto_diagnostic_group d;
7721 : 61 : if (warning_at (location, OPT_Wbool_operation,
7722 : : "%<~%> on an expression of type %<bool%>"))
7723 : 42 : inform (location, "did you mean to use logical not (%<!%>)?");
7724 : 61 : }
7725 : 899536 : arg = cp_perform_integral_promotions (arg, complain);
7726 : : }
7727 : 235 : else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7728 : 235 : arg = mark_rvalue_use (arg);
7729 : : break;
7730 : :
7731 : 0 : case ABS_EXPR:
7732 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7733 : 0 : errstring = _("wrong type argument to abs");
7734 : 0 : else if (!noconvert)
7735 : : {
7736 : 0 : arg = cp_default_conversion (arg, complain);
7737 : 0 : if (arg == error_mark_node)
7738 : : return error_mark_node;
7739 : : }
7740 : : break;
7741 : :
7742 : 0 : case CONJ_EXPR:
7743 : : /* Conjugating a real value is a no-op, but allow it anyway. */
7744 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7745 : 0 : errstring = _("wrong type argument to conjugation");
7746 : 0 : else if (!noconvert)
7747 : : {
7748 : 0 : arg = cp_default_conversion (arg, complain);
7749 : 0 : if (arg == error_mark_node)
7750 : : return error_mark_node;
7751 : : }
7752 : : break;
7753 : :
7754 : 15725729 : case TRUTH_NOT_EXPR:
7755 : 15725729 : if (gnu_vector_type_p (TREE_TYPE (arg)))
7756 : 60 : return cp_build_binary_op (input_location, EQ_EXPR, arg,
7757 : 60 : build_zero_cst (TREE_TYPE (arg)), complain);
7758 : 15725669 : arg = perform_implicit_conversion (boolean_type_node, arg,
7759 : : complain);
7760 : 15725669 : if (arg != error_mark_node)
7761 : : {
7762 : 15725659 : if (processing_template_decl)
7763 : 7504042 : return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
7764 : 8221617 : val = invert_truthvalue_loc (location, arg);
7765 : 8221617 : if (obvalue_p (val))
7766 : 12748 : val = non_lvalue_loc (location, val);
7767 : 8221617 : return val;
7768 : : }
7769 : 10 : errstring = _("in argument to unary !");
7770 : 10 : break;
7771 : :
7772 : : case NOP_EXPR:
7773 : : break;
7774 : :
7775 : 471296 : case REALPART_EXPR:
7776 : 471296 : case IMAGPART_EXPR:
7777 : 471296 : val = build_real_imag_expr (input_location, code, arg);
7778 : 471296 : if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
7779 : 0 : val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
7780 : 0 : TREE_TYPE (eptype), val);
7781 : : return val;
7782 : :
7783 : 7887152 : case PREINCREMENT_EXPR:
7784 : 7887152 : case POSTINCREMENT_EXPR:
7785 : 7887152 : case PREDECREMENT_EXPR:
7786 : 7887152 : case POSTDECREMENT_EXPR:
7787 : : /* Handle complex lvalues (when permitted)
7788 : : by reduction to simpler cases. */
7789 : :
7790 : 7887152 : val = unary_complex_lvalue (code, arg);
7791 : 7887152 : if (val != 0)
7792 : 32 : goto return_build_unary_op;
7793 : :
7794 : 7887120 : tree stripped_arg;
7795 : 7887120 : stripped_arg = tree_strip_any_location_wrapper (arg);
7796 : 1493527 : if ((VAR_P (stripped_arg) || TREE_CODE (stripped_arg) == PARM_DECL)
7797 : 7386502 : && !DECL_READ_P (stripped_arg)
7798 : 8993995 : && (VAR_P (stripped_arg) ? warn_unused_but_set_variable
7799 : : : warn_unused_but_set_parameter) > 1)
7800 : : {
7801 : 9676 : arg = mark_lvalue_use (arg);
7802 : 9676 : DECL_READ_P (stripped_arg) = 0;
7803 : : }
7804 : : else
7805 : 7877444 : arg = mark_lvalue_use (arg);
7806 : :
7807 : : /* Increment or decrement the real part of the value,
7808 : : and don't change the imaginary part. */
7809 : 7887120 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7810 : : {
7811 : 18 : tree real, imag;
7812 : :
7813 : 18 : arg = cp_stabilize_reference (arg);
7814 : 18 : real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7815 : 18 : imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7816 : 18 : real = cp_build_unary_op (code, real, true, complain);
7817 : 18 : if (real == error_mark_node || imag == error_mark_node)
7818 : : return error_mark_node;
7819 : 15 : val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
7820 : 15 : goto return_build_unary_op;
7821 : : }
7822 : :
7823 : : /* Report invalid types. */
7824 : :
7825 : 7887102 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7826 : : arg, true)))
7827 : : {
7828 : 36 : if (code == PREINCREMENT_EXPR)
7829 : 9 : errstring = _("no pre-increment operator for type");
7830 : 27 : else if (code == POSTINCREMENT_EXPR)
7831 : 9 : errstring = _("no post-increment operator for type");
7832 : 18 : else if (code == PREDECREMENT_EXPR)
7833 : 9 : errstring = _("no pre-decrement operator for type");
7834 : : else
7835 : 9 : errstring = _("no post-decrement operator for type");
7836 : : break;
7837 : : }
7838 : 7887066 : else if (arg == error_mark_node)
7839 : : return error_mark_node;
7840 : :
7841 : : /* Report something read-only. */
7842 : :
7843 : 7887066 : if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7844 : 7887066 : || TREE_READONLY (arg))
7845 : : {
7846 : 61 : if (complain & tf_error)
7847 : 61 : cxx_readonly_error (location, arg,
7848 : 61 : ((code == PREINCREMENT_EXPR
7849 : 61 : || code == POSTINCREMENT_EXPR)
7850 : : ? lv_increment : lv_decrement));
7851 : : else
7852 : 0 : return error_mark_node;
7853 : : }
7854 : :
7855 : 7887066 : {
7856 : 7887066 : tree inc;
7857 : 7887066 : tree declared_type = unlowered_expr_type (arg);
7858 : :
7859 : 7887066 : argtype = TREE_TYPE (arg);
7860 : :
7861 : : /* ARM $5.2.5 last annotation says this should be forbidden. */
7862 : 7887066 : if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7863 : : {
7864 : 0 : if (complain & tf_error)
7865 : 0 : permerror (location, (code == PREINCREMENT_EXPR
7866 : 0 : || code == POSTINCREMENT_EXPR)
7867 : : ? G_("ISO C++ forbids incrementing an enum")
7868 : : : G_("ISO C++ forbids decrementing an enum"));
7869 : : else
7870 : 0 : return error_mark_node;
7871 : : }
7872 : :
7873 : : /* Compute the increment. */
7874 : :
7875 : 7887066 : if (TYPE_PTR_P (argtype))
7876 : : {
7877 : 2316623 : tree type = complete_type (TREE_TYPE (argtype));
7878 : :
7879 : 2316623 : if (!COMPLETE_OR_VOID_TYPE_P (type))
7880 : : {
7881 : 11 : if (complain & tf_error)
7882 : 9 : error_at (location, ((code == PREINCREMENT_EXPR
7883 : 9 : || code == POSTINCREMENT_EXPR))
7884 : : ? G_("cannot increment a pointer to incomplete "
7885 : : "type %qT")
7886 : : : G_("cannot decrement a pointer to incomplete "
7887 : : "type %qT"),
7888 : 9 : TREE_TYPE (argtype));
7889 : : else
7890 : 2 : return error_mark_node;
7891 : : }
7892 : 2316612 : else if (!TYPE_PTROB_P (argtype))
7893 : : {
7894 : 75 : if (complain & tf_error)
7895 : 63 : pedwarn (location, OPT_Wpointer_arith,
7896 : 63 : (code == PREINCREMENT_EXPR
7897 : 63 : || code == POSTINCREMENT_EXPR)
7898 : : ? G_("ISO C++ forbids incrementing a pointer "
7899 : : "of type %qT")
7900 : : : G_("ISO C++ forbids decrementing a pointer "
7901 : : "of type %qT"),
7902 : : argtype);
7903 : : else
7904 : 12 : return error_mark_node;
7905 : : }
7906 : 2316537 : else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7907 : 2316537 : TREE_TYPE (argtype),
7908 : : !(complain & tf_error)))
7909 : 0 : return error_mark_node;
7910 : :
7911 : 2316609 : inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7912 : : }
7913 : : else
7914 : 5570300 : inc = VECTOR_TYPE_P (argtype)
7915 : 5570443 : ? build_one_cst (argtype)
7916 : : : integer_one_node;
7917 : :
7918 : 7887052 : inc = cp_convert (argtype, inc, complain);
7919 : :
7920 : : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7921 : : need to ask Objective-C to build the increment or decrement
7922 : : expression for it. */
7923 : 7887052 : if (objc_is_property_ref (arg))
7924 : 0 : return objc_build_incr_expr_for_property_ref (input_location, code,
7925 : 0 : arg, inc);
7926 : :
7927 : : /* Complain about anything else that is not a true lvalue. */
7928 : 7887052 : if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7929 : 7887052 : || code == POSTINCREMENT_EXPR)
7930 : : ? lv_increment : lv_decrement),
7931 : : complain))
7932 : 33 : return error_mark_node;
7933 : :
7934 : : /* [depr.volatile.type] "Postfix ++ and -- expressions and
7935 : : prefix ++ and -- expressions of volatile-qualified arithmetic
7936 : : and pointer types are deprecated." */
7937 : 7886734 : if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7938 : 7887019 : && (complain & tf_warning))
7939 : 398 : warning_at (location, OPT_Wvolatile,
7940 : : "%qs expression of %<volatile%>-qualified type is "
7941 : : "deprecated",
7942 : : ((code == PREINCREMENT_EXPR
7943 : : || code == POSTINCREMENT_EXPR)
7944 : : ? "++" : "--"));
7945 : :
7946 : : /* Forbid using -- or ++ in C++17 on `bool'. */
7947 : 7887019 : if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7948 : : {
7949 : 95 : if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7950 : : {
7951 : 33 : if (complain & tf_error)
7952 : 33 : error_at (location,
7953 : : "use of an operand of type %qT in %<operator--%> "
7954 : : "is forbidden", boolean_type_node);
7955 : 33 : return error_mark_node;
7956 : : }
7957 : : else
7958 : : {
7959 : 62 : if (cxx_dialect >= cxx17)
7960 : : {
7961 : 33 : if (complain & tf_error)
7962 : 32 : error_at (location,
7963 : : "use of an operand of type %qT in "
7964 : : "%<operator++%> is forbidden in C++17",
7965 : : boolean_type_node);
7966 : 33 : return error_mark_node;
7967 : : }
7968 : : /* Otherwise, [depr.incr.bool] says this is deprecated. */
7969 : 29 : else if (complain & tf_warning)
7970 : 25 : warning_at (location, OPT_Wdeprecated,
7971 : : "use of an operand of type %qT "
7972 : : "in %<operator++%> is deprecated",
7973 : : boolean_type_node);
7974 : : }
7975 : 29 : val = boolean_increment (code, arg);
7976 : : }
7977 : 7886924 : else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7978 : : /* An rvalue has no cv-qualifiers. */
7979 : 1163694 : val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7980 : : else
7981 : 6723230 : val = build2 (code, TREE_TYPE (arg), arg, inc);
7982 : :
7983 : 7886953 : TREE_SIDE_EFFECTS (val) = 1;
7984 : 7886953 : goto return_build_unary_op;
7985 : : }
7986 : :
7987 : 618103 : case ADDR_EXPR:
7988 : : /* Note that this operation never does default_conversion
7989 : : regardless of NOCONVERT. */
7990 : 618103 : return cp_build_addr_expr (arg, complain);
7991 : :
7992 : : default:
7993 : : break;
7994 : : }
7995 : :
7996 : 3357606 : if (!errstring)
7997 : : {
7998 : 3357529 : if (argtype == 0)
7999 : 3357529 : argtype = TREE_TYPE (arg);
8000 : 3357529 : val = build1 (code, argtype, arg);
8001 : 11244529 : return_build_unary_op:
8002 : 11244529 : if (eptype)
8003 : 10981 : val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
8004 : 11244529 : return val;
8005 : : }
8006 : :
8007 : 92 : if (complain & tf_error)
8008 : 49 : error_at (location, "%s", errstring);
8009 : 92 : return error_mark_node;
8010 : : }
8011 : :
8012 : : /* Hook for the c-common bits that build a unary op. */
8013 : : tree
8014 : 4136 : build_unary_op (location_t /*location*/,
8015 : : enum tree_code code, tree xarg, bool noconvert)
8016 : : {
8017 : 4136 : return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
8018 : : }
8019 : :
8020 : : /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
8021 : : so that it is a valid lvalue even for GENERIC by replacing
8022 : : (lhs = rhs) with ((lhs = rhs), lhs)
8023 : : (--lhs) with ((--lhs), lhs)
8024 : : (++lhs) with ((++lhs), lhs)
8025 : : and if lhs has side-effects, calling cp_stabilize_reference on it, so
8026 : : that it can be evaluated multiple times. */
8027 : :
8028 : : tree
8029 : 389618 : genericize_compound_lvalue (tree lvalue)
8030 : : {
8031 : 389618 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
8032 : 196 : lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
8033 : 196 : cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
8034 : 196 : TREE_OPERAND (lvalue, 1));
8035 : 389618 : return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
8036 : 389618 : lvalue, TREE_OPERAND (lvalue, 0));
8037 : : }
8038 : :
8039 : : /* Apply unary lvalue-demanding operator CODE to the expression ARG
8040 : : for certain kinds of expressions which are not really lvalues
8041 : : but which we can accept as lvalues.
8042 : :
8043 : : If ARG is not a kind of expression we can handle, return
8044 : : NULL_TREE. */
8045 : :
8046 : : tree
8047 : 275406205 : unary_complex_lvalue (enum tree_code code, tree arg)
8048 : : {
8049 : : /* Inside a template, making these kinds of adjustments is
8050 : : pointless; we are only concerned with the type of the
8051 : : expression. */
8052 : 275795617 : if (processing_template_decl)
8053 : : return NULL_TREE;
8054 : :
8055 : : /* Handle (a, b) used as an "lvalue". */
8056 : 193960752 : if (TREE_CODE (arg) == COMPOUND_EXPR)
8057 : : {
8058 : 390846 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
8059 : : tf_warning_or_error);
8060 : 390846 : return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
8061 : 781692 : TREE_OPERAND (arg, 0), real_result);
8062 : : }
8063 : :
8064 : : /* Handle (a ? b : c) used as an "lvalue". */
8065 : 193569906 : if (TREE_CODE (arg) == COND_EXPR
8066 : 193458168 : || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
8067 : 111738 : return rationalize_conditional_expr (code, arg, tf_warning_or_error);
8068 : :
8069 : : /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
8070 : 193458168 : if (TREE_CODE (arg) == MODIFY_EXPR
8071 : 193068939 : || TREE_CODE (arg) == PREINCREMENT_EXPR
8072 : 193068855 : || TREE_CODE (arg) == PREDECREMENT_EXPR)
8073 : 389412 : return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
8074 : :
8075 : 193068756 : if (code != ADDR_EXPR)
8076 : : return NULL_TREE;
8077 : :
8078 : : /* Handle (a = b) used as an "lvalue" for `&'. */
8079 : 189810176 : if (TREE_CODE (arg) == MODIFY_EXPR
8080 : 189810176 : || TREE_CODE (arg) == INIT_EXPR)
8081 : : {
8082 : 0 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
8083 : : tf_warning_or_error);
8084 : 0 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
8085 : : arg, real_result);
8086 : 0 : suppress_warning (arg /* What warning? */);
8087 : 0 : return arg;
8088 : : }
8089 : :
8090 : 293714048 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
8091 : 293713006 : || TREE_CODE (arg) == OFFSET_REF)
8092 : : return NULL_TREE;
8093 : :
8094 : : /* We permit compiler to make function calls returning
8095 : : objects of aggregate type look like lvalues. */
8096 : 103902830 : {
8097 : 103902830 : tree targ = arg;
8098 : :
8099 : 103902830 : if (TREE_CODE (targ) == SAVE_EXPR)
8100 : 0 : targ = TREE_OPERAND (targ, 0);
8101 : :
8102 : 103902830 : if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
8103 : : {
8104 : 29 : if (TREE_CODE (arg) == SAVE_EXPR)
8105 : : targ = arg;
8106 : : else
8107 : 29 : targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
8108 : 29 : return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
8109 : : }
8110 : :
8111 : 103902801 : if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
8112 : 0 : return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
8113 : 0 : TREE_OPERAND (targ, 0), current_function_decl, NULL);
8114 : : }
8115 : :
8116 : : /* Don't let anything else be handled specially. */
8117 : : return NULL_TREE;
8118 : : }
8119 : :
8120 : : /* Mark EXP saying that we need to be able to take the
8121 : : address of it; it should not be allocated in a register.
8122 : : Value is true if successful. ARRAY_REF_P is true if this
8123 : : is for ARRAY_REF construction - in that case we don't want
8124 : : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
8125 : : it is fine to use ARRAY_REFs for vector subscripts on vector
8126 : : register variables.
8127 : :
8128 : : C++: we do not allow `current_class_ptr' to be addressable. */
8129 : :
8130 : : bool
8131 : 308462320 : cxx_mark_addressable (tree exp, bool array_ref_p)
8132 : : {
8133 : 308462320 : tree x = exp;
8134 : :
8135 : 365042694 : while (1)
8136 : 365042694 : switch (TREE_CODE (x))
8137 : : {
8138 : 28876832 : case VIEW_CONVERT_EXPR:
8139 : 28876832 : if (array_ref_p
8140 : 728514 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8141 : 29436369 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
8142 : : return true;
8143 : 28873935 : x = TREE_OPERAND (x, 0);
8144 : 28873935 : break;
8145 : :
8146 : 27458817 : case COMPONENT_REF:
8147 : 27458817 : if (bitfield_p (x))
8148 : 9 : error ("attempt to take address of bit-field");
8149 : : /* FALLTHRU */
8150 : 27706439 : case ADDR_EXPR:
8151 : 27706439 : case ARRAY_REF:
8152 : 27706439 : case REALPART_EXPR:
8153 : 27706439 : case IMAGPART_EXPR:
8154 : 27706439 : x = TREE_OPERAND (x, 0);
8155 : 27706439 : break;
8156 : :
8157 : 6697342 : case PARM_DECL:
8158 : 6697342 : if (x == current_class_ptr)
8159 : : {
8160 : 18 : error ("cannot take the address of %<this%>, which is an rvalue expression");
8161 : 18 : TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
8162 : 18 : return true;
8163 : : }
8164 : : /* Fall through. */
8165 : :
8166 : 60486585 : case VAR_DECL:
8167 : : /* Caller should not be trying to mark initialized
8168 : : constant fields addressable. */
8169 : 60486585 : gcc_assert (DECL_LANG_SPECIFIC (x) == 0
8170 : : || DECL_IN_AGGR_P (x) == 0
8171 : : || TREE_STATIC (x)
8172 : : || DECL_EXTERNAL (x));
8173 : : /* Fall through. */
8174 : :
8175 : 60561551 : case RESULT_DECL:
8176 : 60561642 : if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
8177 : 60561633 : && !DECL_ARTIFICIAL (x))
8178 : : {
8179 : 31 : if (VAR_P (x) && DECL_HARD_REGISTER (x))
8180 : : {
8181 : 15 : error
8182 : 15 : ("address of explicit register variable %qD requested", x);
8183 : 15 : return false;
8184 : : }
8185 : 16 : else if (extra_warnings)
8186 : 3 : warning
8187 : 3 : (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
8188 : : }
8189 : 60561536 : TREE_ADDRESSABLE (x) = 1;
8190 : 60561536 : return true;
8191 : :
8192 : 152955086 : case CONST_DECL:
8193 : 152955086 : case FUNCTION_DECL:
8194 : 152955086 : TREE_ADDRESSABLE (x) = 1;
8195 : 152955086 : return true;
8196 : :
8197 : 54014 : case CONSTRUCTOR:
8198 : 54014 : TREE_ADDRESSABLE (x) = 1;
8199 : 54014 : return true;
8200 : :
8201 : 13362809 : case TARGET_EXPR:
8202 : 13362809 : TREE_ADDRESSABLE (x) = 1;
8203 : 13362809 : cxx_mark_addressable (TARGET_EXPR_SLOT (x));
8204 : 13362809 : return true;
8205 : :
8206 : : default:
8207 : : return true;
8208 : : }
8209 : : }
8210 : :
8211 : : /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
8212 : :
8213 : : tree
8214 : 6986484 : build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
8215 : : tsubst_flags_t complain)
8216 : : {
8217 : 6986484 : tree orig_ifexp = ifexp;
8218 : 6986484 : tree orig_op1 = op1;
8219 : 6986484 : tree orig_op2 = op2;
8220 : 6986484 : tree expr;
8221 : :
8222 : 6986484 : if (processing_template_decl)
8223 : : {
8224 : : /* The standard says that the expression is type-dependent if
8225 : : IFEXP is type-dependent, even though the eventual type of the
8226 : : expression doesn't dependent on IFEXP. */
8227 : 2778626 : if (type_dependent_expression_p (ifexp)
8228 : : /* As a GNU extension, the middle operand may be omitted. */
8229 : 1390609 : || (op1 && type_dependent_expression_p (op1))
8230 : 3707578 : || type_dependent_expression_p (op2))
8231 : 1879612 : return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
8232 : : }
8233 : :
8234 : 5106872 : expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
8235 : 5106872 : if (processing_template_decl && expr != error_mark_node)
8236 : : {
8237 : 899008 : tree min = build_min_non_dep (COND_EXPR, expr,
8238 : : orig_ifexp, orig_op1, orig_op2);
8239 : 899008 : expr = convert_from_reference (min);
8240 : : }
8241 : : return expr;
8242 : : }
8243 : :
8244 : : /* Given a list of expressions, return a compound expression
8245 : : that performs them all and returns the value of the last of them. */
8246 : :
8247 : : tree
8248 : 31728519 : build_x_compound_expr_from_list (tree list, expr_list_kind exp,
8249 : : tsubst_flags_t complain)
8250 : : {
8251 : 31728519 : tree expr = TREE_VALUE (list);
8252 : :
8253 : 266371 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8254 : 31994878 : && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
8255 : : {
8256 : 14 : if (complain & tf_error)
8257 : 28 : pedwarn (cp_expr_loc_or_input_loc (expr), 0,
8258 : : "list-initializer for non-class type must not "
8259 : : "be parenthesized");
8260 : : else
8261 : 0 : return error_mark_node;
8262 : : }
8263 : :
8264 : 31728519 : if (TREE_CHAIN (list))
8265 : : {
8266 : 23 : if (complain & tf_error)
8267 : 14 : switch (exp)
8268 : : {
8269 : 12 : case ELK_INIT:
8270 : 12 : permerror (input_location, "expression list treated as compound "
8271 : : "expression in initializer");
8272 : 12 : break;
8273 : 1 : case ELK_MEM_INIT:
8274 : 1 : permerror (input_location, "expression list treated as compound "
8275 : : "expression in mem-initializer");
8276 : 1 : break;
8277 : 1 : case ELK_FUNC_CAST:
8278 : 1 : permerror (input_location, "expression list treated as compound "
8279 : : "expression in functional cast");
8280 : 1 : break;
8281 : 0 : default:
8282 : 0 : gcc_unreachable ();
8283 : : }
8284 : : else
8285 : 9 : return error_mark_node;
8286 : :
8287 : 28 : for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
8288 : 28 : expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
8289 : 14 : expr, TREE_VALUE (list), NULL_TREE,
8290 : : complain);
8291 : : }
8292 : :
8293 : : return expr;
8294 : : }
8295 : :
8296 : : /* Like build_x_compound_expr_from_list, but using a VEC. */
8297 : :
8298 : : tree
8299 : 171420 : build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8300 : : tsubst_flags_t complain)
8301 : : {
8302 : 171420 : if (vec_safe_is_empty (vec))
8303 : : return NULL_TREE;
8304 : 171420 : else if (vec->length () == 1)
8305 : 171355 : return (*vec)[0];
8306 : : else
8307 : : {
8308 : 65 : tree expr;
8309 : 65 : unsigned int ix;
8310 : 65 : tree t;
8311 : :
8312 : 65 : if (msg != NULL)
8313 : : {
8314 : 5 : if (complain & tf_error)
8315 : 0 : permerror (input_location,
8316 : : "%s expression list treated as compound expression",
8317 : : msg);
8318 : : else
8319 : 5 : return error_mark_node;
8320 : : }
8321 : :
8322 : 60 : expr = (*vec)[0];
8323 : 134 : for (ix = 1; vec->iterate (ix, &t); ++ix)
8324 : 74 : expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
8325 : : t, NULL_TREE, complain);
8326 : :
8327 : : return expr;
8328 : : }
8329 : : }
8330 : :
8331 : : /* Handle overloading of the ',' operator when needed. */
8332 : :
8333 : : tree
8334 : 2806669 : build_x_compound_expr (location_t loc, tree op1, tree op2,
8335 : : tree lookups, tsubst_flags_t complain)
8336 : : {
8337 : 2806669 : tree result;
8338 : 2806669 : tree orig_op1 = op1;
8339 : 2806669 : tree orig_op2 = op2;
8340 : 2806669 : tree overload = NULL_TREE;
8341 : :
8342 : 2806669 : if (processing_template_decl)
8343 : : {
8344 : 2568855 : if (type_dependent_expression_p (op1)
8345 : 2568855 : || type_dependent_expression_p (op2))
8346 : : {
8347 : 2283834 : result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8348 : 2283834 : TREE_TYPE (result)
8349 : 2283834 : = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
8350 : 2283834 : return result;
8351 : : }
8352 : : }
8353 : :
8354 : 522835 : result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8355 : : NULL_TREE, lookups, &overload, complain);
8356 : 522835 : if (!result)
8357 : 515438 : result = cp_build_compound_expr (op1, op2, complain);
8358 : :
8359 : 522835 : if (processing_template_decl && result != error_mark_node)
8360 : : {
8361 : 283200 : if (overload != NULL_TREE)
8362 : 52 : return (build_min_non_dep_op_overload
8363 : 52 : (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
8364 : :
8365 : 283148 : return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
8366 : : }
8367 : :
8368 : : return result;
8369 : : }
8370 : :
8371 : : /* Like cp_build_compound_expr, but for the c-common bits. */
8372 : :
8373 : : tree
8374 : 11255 : build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8375 : : {
8376 : 11255 : return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8377 : : }
8378 : :
8379 : : /* Build a compound expression. */
8380 : :
8381 : : tree
8382 : 642538 : cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8383 : : {
8384 : 642538 : lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8385 : :
8386 : 642538 : if (lhs == error_mark_node || rhs == error_mark_node)
8387 : : return error_mark_node;
8388 : :
8389 : 642535 : if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8390 : 0 : lhs = TREE_OPERAND (lhs, 0);
8391 : 642535 : tree eptype = NULL_TREE;
8392 : 642535 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8393 : : {
8394 : 3 : eptype = TREE_TYPE (rhs);
8395 : 3 : rhs = TREE_OPERAND (rhs, 0);
8396 : : }
8397 : :
8398 : 642535 : if (TREE_CODE (rhs) == TARGET_EXPR)
8399 : : {
8400 : : /* If the rhs is a TARGET_EXPR, then build the compound
8401 : : expression inside the target_expr's initializer. This
8402 : : helps the compiler to eliminate unnecessary temporaries. */
8403 : 10342 : tree init = TARGET_EXPR_INITIAL (rhs);
8404 : :
8405 : 10342 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8406 : 10342 : TARGET_EXPR_INITIAL (rhs) = init;
8407 : :
8408 : 10342 : if (eptype)
8409 : 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8410 : 10342 : return rhs;
8411 : : }
8412 : :
8413 : 632193 : rhs = resolve_nondeduced_context (rhs, complain);
8414 : :
8415 : 632193 : if (type_unknown_p (rhs))
8416 : : {
8417 : 33 : if (complain & tf_error)
8418 : 51 : error_at (cp_expr_loc_or_input_loc (rhs),
8419 : : "no context to resolve type of %qE", rhs);
8420 : 33 : return error_mark_node;
8421 : : }
8422 : :
8423 : 632160 : tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8424 : 632160 : if (eptype)
8425 : 3 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8426 : : return ret;
8427 : : }
8428 : :
8429 : : /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8430 : : casts away constness. CAST gives the type of cast. Returns true
8431 : : if the cast is ill-formed, false if it is well-formed.
8432 : :
8433 : : ??? This function warns for casting away any qualifier not just
8434 : : const. We would like to specify exactly what qualifiers are casted
8435 : : away.
8436 : : */
8437 : :
8438 : : static bool
8439 : 738158 : check_for_casting_away_constness (location_t loc, tree src_type,
8440 : : tree dest_type, enum tree_code cast,
8441 : : tsubst_flags_t complain)
8442 : : {
8443 : : /* C-style casts are allowed to cast away constness. With
8444 : : WARN_CAST_QUAL, we still want to issue a warning. */
8445 : 738158 : if (cast == CAST_EXPR && !warn_cast_qual)
8446 : : return false;
8447 : :
8448 : 595865 : if (!casts_away_constness (src_type, dest_type, complain))
8449 : : return false;
8450 : :
8451 : 392 : switch (cast)
8452 : : {
8453 : 335 : case CAST_EXPR:
8454 : 335 : if (complain & tf_warning)
8455 : 335 : warning_at (loc, OPT_Wcast_qual,
8456 : : "cast from type %qT to type %qT casts away qualifiers",
8457 : : src_type, dest_type);
8458 : : return false;
8459 : :
8460 : 36 : case STATIC_CAST_EXPR:
8461 : 36 : if (complain & tf_error)
8462 : 24 : error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8463 : : "away qualifiers",
8464 : : src_type, dest_type);
8465 : : return true;
8466 : :
8467 : 21 : case REINTERPRET_CAST_EXPR:
8468 : 21 : if (complain & tf_error)
8469 : 15 : error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8470 : : "casts away qualifiers",
8471 : : src_type, dest_type);
8472 : : return true;
8473 : :
8474 : 0 : default:
8475 : 0 : gcc_unreachable();
8476 : : }
8477 : : }
8478 : :
8479 : : /* Warns if the cast from expression EXPR to type TYPE is useless. */
8480 : : void
8481 : 47257848 : maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8482 : : tsubst_flags_t complain)
8483 : : {
8484 : 47257848 : if (warn_useless_cast
8485 : 152 : && complain & tf_warning)
8486 : : {
8487 : 152 : if (TYPE_REF_P (type)
8488 : 152 : ? ((TYPE_REF_IS_RVALUE (type)
8489 : 86 : ? xvalue_p (expr) : lvalue_p (expr))
8490 : 172 : && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8491 : : /* Don't warn when converting a class object to a non-reference type,
8492 : : because that's a common way to create a temporary. */
8493 : 66 : : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8494 : 96 : warning_at (loc, OPT_Wuseless_cast,
8495 : : "useless cast to type %q#T", type);
8496 : : }
8497 : 47257848 : }
8498 : :
8499 : : /* Warns if the cast ignores cv-qualifiers on TYPE. */
8500 : : static void
8501 : 47247798 : maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8502 : : tsubst_flags_t complain)
8503 : : {
8504 : 47247798 : if (warn_ignored_qualifiers
8505 : 267888 : && complain & tf_warning
8506 : 267441 : && !CLASS_TYPE_P (type)
8507 : 47510432 : && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8508 : 36 : warning_at (loc, OPT_Wignored_qualifiers,
8509 : : "type qualifiers ignored on cast result type");
8510 : 47247798 : }
8511 : :
8512 : : /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8513 : : (another pointer-to-member type in the same hierarchy) and return
8514 : : the converted expression. If ALLOW_INVERSE_P is permitted, a
8515 : : pointer-to-derived may be converted to pointer-to-base; otherwise,
8516 : : only the other direction is permitted. If C_CAST_P is true, this
8517 : : conversion is taking place as part of a C-style cast. */
8518 : :
8519 : : tree
8520 : 30261 : convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8521 : : bool c_cast_p, tsubst_flags_t complain)
8522 : : {
8523 : 30261 : if (same_type_p (type, TREE_TYPE (expr)))
8524 : : return expr;
8525 : :
8526 : 30261 : if (TYPE_PTRDATAMEM_P (type))
8527 : : {
8528 : 364 : tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8529 : 364 : tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8530 : 364 : tree delta = (get_delta_difference
8531 : 364 : (obase, nbase,
8532 : : allow_inverse_p, c_cast_p, complain));
8533 : :
8534 : 364 : if (delta == error_mark_node)
8535 : : return error_mark_node;
8536 : :
8537 : 358 : if (!same_type_p (obase, nbase))
8538 : : {
8539 : 287 : if (TREE_CODE (expr) == PTRMEM_CST)
8540 : 159 : expr = cplus_expand_constant (expr);
8541 : :
8542 : 287 : tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8543 : 287 : build_int_cst (TREE_TYPE (expr), -1),
8544 : : complain);
8545 : 287 : tree op1 = build_nop (ptrdiff_type_node, expr);
8546 : 287 : tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8547 : : complain);
8548 : :
8549 : 287 : expr = fold_build3_loc (input_location,
8550 : : COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8551 : : }
8552 : :
8553 : 358 : return build_nop (type, expr);
8554 : : }
8555 : : else
8556 : 29897 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8557 : 29897 : allow_inverse_p, c_cast_p, complain);
8558 : : }
8559 : :
8560 : : /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8561 : : this static_cast is being attempted as one of the possible casts
8562 : : allowed by a C-style cast. (In that case, accessibility of base
8563 : : classes is not considered, and it is OK to cast away
8564 : : constness.) Return the result of the cast. *VALID_P is set to
8565 : : indicate whether or not the cast was valid. */
8566 : :
8567 : : static tree
8568 : 46867929 : build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8569 : : bool *valid_p, tsubst_flags_t complain)
8570 : : {
8571 : 46867929 : tree intype;
8572 : 46867929 : tree result;
8573 : 46867929 : cp_lvalue_kind clk;
8574 : :
8575 : : /* Assume the cast is valid. */
8576 : 46867929 : *valid_p = true;
8577 : :
8578 : 46867929 : intype = unlowered_expr_type (expr);
8579 : :
8580 : : /* Save casted types in the function's used types hash table. */
8581 : 46867929 : used_types_insert (type);
8582 : :
8583 : : /* A prvalue of non-class type is cv-unqualified. */
8584 : 46867929 : if (!CLASS_TYPE_P (type))
8585 : 45005245 : type = cv_unqualified (type);
8586 : :
8587 : : /* [expr.static.cast]
8588 : :
8589 : : An lvalue of type "cv1 B", where B is a class type, can be cast
8590 : : to type "reference to cv2 D", where D is a class derived (clause
8591 : : _class.derived_) from B, if a valid standard conversion from
8592 : : "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8593 : : same cv-qualification as, or greater cv-qualification than, cv1,
8594 : : and B is not a virtual base class of D. */
8595 : : /* We check this case before checking the validity of "TYPE t =
8596 : : EXPR;" below because for this case:
8597 : :
8598 : : struct B {};
8599 : : struct D : public B { D(const B&); };
8600 : : extern B& b;
8601 : : void f() { static_cast<const D&>(b); }
8602 : :
8603 : : we want to avoid constructing a new D. The standard is not
8604 : : completely clear about this issue, but our interpretation is
8605 : : consistent with other compilers. */
8606 : 46867929 : if (TYPE_REF_P (type)
8607 : 3935552 : && CLASS_TYPE_P (TREE_TYPE (type))
8608 : 3018443 : && CLASS_TYPE_P (intype)
8609 : 3018428 : && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8610 : 3010167 : && DERIVED_FROM_P (intype, TREE_TYPE (type))
8611 : 2977483 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8612 : 2977483 : build_pointer_type (TYPE_MAIN_VARIANT
8613 : : (TREE_TYPE (type))),
8614 : : complain)
8615 : 49845412 : && (c_cast_p
8616 : 2977459 : || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8617 : : {
8618 : 2977483 : tree base;
8619 : :
8620 : 2977483 : if (processing_template_decl)
8621 : : return expr;
8622 : :
8623 : : /* There is a standard conversion from "D*" to "B*" even if "B"
8624 : : is ambiguous or inaccessible. If this is really a
8625 : : static_cast, then we check both for inaccessibility and
8626 : : ambiguity. However, if this is a static_cast being performed
8627 : : because the user wrote a C-style cast, then accessibility is
8628 : : not considered. */
8629 : 5535176 : base = lookup_base (TREE_TYPE (type), intype,
8630 : : c_cast_p ? ba_unique : ba_check,
8631 : : NULL, complain);
8632 : 2767600 : expr = cp_build_addr_expr (expr, complain);
8633 : :
8634 : 2767600 : if (sanitize_flags_p (SANITIZE_VPTR))
8635 : : {
8636 : 514 : tree ubsan_check
8637 : 514 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8638 : : intype, expr);
8639 : 514 : if (ubsan_check)
8640 : 2767600 : expr = ubsan_check;
8641 : : }
8642 : :
8643 : : /* Convert from "B*" to "D*". This function will check that "B"
8644 : : is not a virtual base of "D". Even if we don't have a guarantee
8645 : : that expr is NULL, if the static_cast is to a reference type,
8646 : : it is UB if it would be NULL, so omit the non-NULL check. */
8647 : 2767600 : expr = build_base_path (MINUS_EXPR, expr, base,
8648 : : /*nonnull=*/flag_delete_null_pointer_checks,
8649 : : complain);
8650 : :
8651 : : /* Convert the pointer to a reference -- but then remember that
8652 : : there are no expressions with reference type in C++.
8653 : :
8654 : : We call rvalue so that there's an actual tree code
8655 : : (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8656 : : is a variable with the same type, the conversion would get folded
8657 : : away, leaving just the variable and causing lvalue_kind to give
8658 : : the wrong answer. */
8659 : 2767600 : expr = cp_fold_convert (type, expr);
8660 : :
8661 : : /* When -fsanitize=null, make sure to diagnose reference binding to
8662 : : NULL even when the reference is converted to pointer later on. */
8663 : 2767600 : if (sanitize_flags_p (SANITIZE_NULL)
8664 : 520 : && TREE_CODE (expr) == COND_EXPR
8665 : 6 : && TREE_OPERAND (expr, 2)
8666 : 6 : && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8667 : 2767606 : && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8668 : 6 : ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8669 : :
8670 : 2767600 : return convert_from_reference (rvalue (expr));
8671 : : }
8672 : :
8673 : : /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8674 : : cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8675 : 43890446 : if (TYPE_REF_P (type)
8676 : 958069 : && TYPE_REF_IS_RVALUE (type)
8677 : 504026 : && (clk = real_lvalue_p (expr))
8678 : 500041 : && reference_compatible_p (TREE_TYPE (type), intype)
8679 : 44390484 : && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8680 : : {
8681 : 500038 : if (processing_template_decl)
8682 : : return expr;
8683 : 499805 : if (clk == clk_ordinary)
8684 : : {
8685 : : /* Handle the (non-bit-field) lvalue case here by casting to
8686 : : lvalue reference and then changing it to an rvalue reference.
8687 : : Casting an xvalue to rvalue reference will be handled by the
8688 : : main code path. */
8689 : 499802 : tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8690 : 499802 : result = (perform_direct_initialization_if_possible
8691 : 499802 : (lref, expr, c_cast_p, complain));
8692 : 499802 : result = build1 (NON_LVALUE_EXPR, type, result);
8693 : 499802 : return convert_from_reference (result);
8694 : : }
8695 : : else
8696 : : /* For a bit-field or packed field, bind to a temporary. */
8697 : 3 : expr = rvalue (expr);
8698 : : }
8699 : :
8700 : : /* Resolve overloaded address here rather than once in
8701 : : implicit_conversion and again in the inverse code below. */
8702 : 43390411 : if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8703 : : {
8704 : 18 : expr = instantiate_type (type, expr, complain);
8705 : 18 : intype = TREE_TYPE (expr);
8706 : : }
8707 : :
8708 : : /* [expr.static.cast]
8709 : :
8710 : : Any expression can be explicitly converted to type cv void. */
8711 : 43390411 : if (VOID_TYPE_P (type))
8712 : : {
8713 : 429838 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8714 : 0 : expr = TREE_OPERAND (expr, 0);
8715 : 429838 : return convert_to_void (expr, ICV_CAST, complain);
8716 : : }
8717 : :
8718 : : /* [class.abstract]
8719 : : An abstract class shall not be used ... as the type of an explicit
8720 : : conversion. */
8721 : 42960573 : if (abstract_virtuals_error (ACU_CAST, type, complain))
8722 : 6 : return error_mark_node;
8723 : :
8724 : : /* [expr.static.cast]
8725 : :
8726 : : An expression e can be explicitly converted to a type T using a
8727 : : static_cast of the form static_cast<T>(e) if the declaration T
8728 : : t(e);" is well-formed, for some invented temporary variable
8729 : : t. */
8730 : 42960567 : result = perform_direct_initialization_if_possible (type, expr,
8731 : : c_cast_p, complain);
8732 : : /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8733 : : which initialize the first element of the aggregate. We need to handle
8734 : : the array case specifically. */
8735 : 42960567 : if (result == NULL_TREE
8736 : 4349197 : && cxx_dialect >= cxx20
8737 : 1574546 : && TREE_CODE (type) == ARRAY_TYPE)
8738 : : {
8739 : : /* Create { EXPR } and perform direct-initialization from it. */
8740 : 15 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8741 : 15 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8742 : 15 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8743 : 15 : result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8744 : : complain);
8745 : : }
8746 : 4349197 : if (result)
8747 : : {
8748 : 38611385 : if (processing_template_decl)
8749 : : return expr;
8750 : :
8751 : 37914521 : result = convert_from_reference (result);
8752 : :
8753 : : /* [expr.static.cast]
8754 : :
8755 : : If T is a reference type, the result is an lvalue; otherwise,
8756 : : the result is an rvalue. */
8757 : 37914521 : if (!TYPE_REF_P (type))
8758 : : {
8759 : 37456584 : result = rvalue (result);
8760 : :
8761 : 37456584 : if (result == expr && SCALAR_TYPE_P (type))
8762 : : /* Leave some record of the cast. */
8763 : 2150695 : result = build_nop (type, expr);
8764 : : }
8765 : 37914521 : return result;
8766 : : }
8767 : :
8768 : : /* [expr.static.cast]
8769 : :
8770 : : The inverse of any standard conversion sequence (clause _conv_),
8771 : : other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8772 : : (_conv.array_), function-to-pointer (_conv.func_), and boolean
8773 : : (_conv.bool_) conversions, can be performed explicitly using
8774 : : static_cast subject to the restriction that the explicit
8775 : : conversion does not cast away constness (_expr.const.cast_), and
8776 : : the following additional rules for specific cases: */
8777 : : /* For reference, the conversions not excluded are: integral
8778 : : promotions, floating-point promotion, integral conversions,
8779 : : floating-point conversions, floating-integral conversions,
8780 : : pointer conversions, and pointer to member conversions. */
8781 : : /* DR 128
8782 : :
8783 : : A value of integral _or enumeration_ type can be explicitly
8784 : : converted to an enumeration type. */
8785 : : /* The effect of all that is that any conversion between any two
8786 : : types which are integral, floating, or enumeration types can be
8787 : : performed. */
8788 : 4349182 : if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8789 : 2567004 : || SCALAR_FLOAT_TYPE_P (type))
8790 : 1956680 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8791 : 282218 : || SCALAR_FLOAT_TYPE_P (intype)))
8792 : : {
8793 : 1848952 : if (processing_template_decl)
8794 : : return expr;
8795 : 1773625 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8796 : 0 : expr = TREE_OPERAND (expr, 0);
8797 : : /* [expr.static.cast]: "If the value is not a bit-field, the result
8798 : : refers to the object or the specified base class subobject thereof;
8799 : : otherwise, the lvalue-to-rvalue conversion is applied to the
8800 : : bit-field and the resulting prvalue is used as the operand of the
8801 : : static_cast." There are no prvalue bit-fields; the l-to-r conversion
8802 : : will give us an object of the underlying type of the bit-field. */
8803 : 1773625 : expr = decay_conversion (expr, complain);
8804 : 1773625 : return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8805 : : }
8806 : :
8807 : 767582 : if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8808 : 763115 : && CLASS_TYPE_P (TREE_TYPE (type))
8809 : 206482 : && CLASS_TYPE_P (TREE_TYPE (intype))
8810 : 2540154 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8811 : : (TREE_TYPE (intype))),
8812 : 39924 : build_pointer_type (TYPE_MAIN_VARIANT
8813 : : (TREE_TYPE (type))),
8814 : : complain))
8815 : : {
8816 : 39585 : tree base;
8817 : :
8818 : 39585 : if (processing_template_decl)
8819 : : return expr;
8820 : :
8821 : 39567 : if (!c_cast_p
8822 : 39567 : && check_for_casting_away_constness (loc, intype, type,
8823 : : STATIC_CAST_EXPR,
8824 : : complain))
8825 : 6 : return error_mark_node;
8826 : 78583 : base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8827 : : c_cast_p ? ba_unique : ba_check,
8828 : : NULL, complain);
8829 : 39561 : expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8830 : : complain);
8831 : :
8832 : 39561 : if (sanitize_flags_p (SANITIZE_VPTR))
8833 : : {
8834 : 43 : tree ubsan_check
8835 : 43 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8836 : : intype, expr);
8837 : 43 : if (ubsan_check)
8838 : 39561 : expr = ubsan_check;
8839 : : }
8840 : :
8841 : 39561 : return cp_fold_convert (type, expr);
8842 : : }
8843 : :
8844 : 89 : if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8845 : 2460645 : || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8846 : : {
8847 : 232 : tree c1;
8848 : 232 : tree c2;
8849 : 232 : tree t1;
8850 : 232 : tree t2;
8851 : :
8852 : 232 : c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8853 : 232 : c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8854 : :
8855 : 232 : if (TYPE_PTRDATAMEM_P (type))
8856 : : {
8857 : 89 : t1 = (build_ptrmem_type
8858 : 89 : (c1,
8859 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8860 : 89 : t2 = (build_ptrmem_type
8861 : 89 : (c2,
8862 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8863 : : }
8864 : : else
8865 : : {
8866 : : t1 = intype;
8867 : : t2 = type;
8868 : : }
8869 : 232 : if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8870 : : {
8871 : 154 : if (!c_cast_p
8872 : 154 : && check_for_casting_away_constness (loc, intype, type,
8873 : : STATIC_CAST_EXPR,
8874 : : complain))
8875 : 9 : return error_mark_node;
8876 : 145 : if (processing_template_decl)
8877 : : return expr;
8878 : 145 : return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8879 : 145 : c_cast_p, complain);
8880 : : }
8881 : : }
8882 : :
8883 : : /* [expr.static.cast]
8884 : :
8885 : : An rvalue of type "pointer to cv void" can be explicitly
8886 : : converted to a pointer to object type. A value of type pointer
8887 : : to object converted to "pointer to cv void" and back to the
8888 : : original pointer type will have its original value. */
8889 : 2460491 : if (TYPE_PTR_P (intype)
8890 : 829095 : && VOID_TYPE_P (TREE_TYPE (intype))
8891 : 3154008 : && TYPE_PTROB_P (type))
8892 : : {
8893 : 661279 : if (!c_cast_p
8894 : 661279 : && check_for_casting_away_constness (loc, intype, type,
8895 : : STATIC_CAST_EXPR,
8896 : : complain))
8897 : 21 : return error_mark_node;
8898 : 661258 : if (processing_template_decl)
8899 : : return expr;
8900 : 571871 : return build_nop (type, expr);
8901 : : }
8902 : :
8903 : 1799212 : *valid_p = false;
8904 : 1799212 : return error_mark_node;
8905 : : }
8906 : :
8907 : : /* Return an expression representing static_cast<TYPE>(EXPR). */
8908 : :
8909 : : tree
8910 : 13578984 : build_static_cast (location_t loc, tree type, tree oexpr,
8911 : : tsubst_flags_t complain)
8912 : : {
8913 : 13578984 : tree expr = oexpr;
8914 : 13578984 : tree result;
8915 : 13578984 : bool valid_p;
8916 : :
8917 : 13578984 : if (type == error_mark_node || expr == error_mark_node)
8918 : : return error_mark_node;
8919 : :
8920 : 13578930 : bool dependent = (dependent_type_p (type)
8921 : 13578930 : || type_dependent_expression_p (expr));
8922 : 9391313 : if (dependent)
8923 : : {
8924 : 5259366 : tmpl:
8925 : 5259366 : expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8926 : : /* We don't know if it will or will not have side effects. */
8927 : 5259366 : TREE_SIDE_EFFECTS (expr) = 1;
8928 : 5259366 : result = convert_from_reference (expr);
8929 : 5259366 : protected_set_expr_location (result, loc);
8930 : 5259366 : return result;
8931 : : }
8932 : :
8933 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8934 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8935 : 9391313 : if (!TYPE_REF_P (type)
8936 : 5456992 : && TREE_CODE (expr) == NOP_EXPR
8937 : 9493121 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8938 : 48 : expr = TREE_OPERAND (expr, 0);
8939 : :
8940 : 9391313 : result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8941 : : &valid_p, complain);
8942 : 9391313 : if (valid_p)
8943 : : {
8944 : 9391117 : if (result != error_mark_node)
8945 : : {
8946 : 9391012 : maybe_warn_about_useless_cast (loc, type, expr, complain);
8947 : 9391012 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8948 : : }
8949 : 9391117 : if (processing_template_decl)
8950 : 1071749 : goto tmpl;
8951 : 8319368 : protected_set_expr_location (result, loc);
8952 : 8319368 : return result;
8953 : : }
8954 : :
8955 : 196 : if (complain & tf_error)
8956 : : {
8957 : 163 : auto_diagnostic_group d;
8958 : 163 : error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8959 : 163 : TREE_TYPE (expr), type);
8960 : 163 : if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8961 : 121 : && CLASS_TYPE_P (TREE_TYPE (type))
8962 : 192 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8963 : 17 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8964 : 17 : "class type %qT is incomplete", TREE_TYPE (type));
8965 : 163 : tree expr_type = TREE_TYPE (expr);
8966 : 163 : if (TYPE_PTR_P (expr_type))
8967 : 36 : expr_type = TREE_TYPE (expr_type);
8968 : 163 : if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8969 : 12 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8970 : : "class type %qT is incomplete", expr_type);
8971 : 163 : }
8972 : 196 : return error_mark_node;
8973 : : }
8974 : :
8975 : : /* EXPR is an expression with member function or pointer-to-member
8976 : : function type. TYPE is a pointer type. Converting EXPR to TYPE is
8977 : : not permitted by ISO C++, but we accept it in some modes. If we
8978 : : are not in one of those modes, issue a diagnostic. Return the
8979 : : converted expression. */
8980 : :
8981 : : tree
8982 : 197 : convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8983 : : {
8984 : 197 : tree intype;
8985 : 197 : tree decl;
8986 : :
8987 : 197 : intype = TREE_TYPE (expr);
8988 : 197 : gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8989 : : || TREE_CODE (intype) == METHOD_TYPE);
8990 : :
8991 : 197 : if (!(complain & tf_warning_or_error))
8992 : 0 : return error_mark_node;
8993 : :
8994 : 197 : location_t loc = cp_expr_loc_or_input_loc (expr);
8995 : :
8996 : 197 : if (pedantic || warn_pmf2ptr)
8997 : 271 : pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8998 : : "converting from %qH to %qI", intype, type);
8999 : :
9000 : 197 : STRIP_ANY_LOCATION_WRAPPER (expr);
9001 : :
9002 : 197 : if (TREE_CODE (intype) == METHOD_TYPE)
9003 : 88 : expr = build_addr_func (expr, complain);
9004 : 109 : else if (TREE_CODE (expr) == PTRMEM_CST)
9005 : 91 : expr = build_address (PTRMEM_CST_MEMBER (expr));
9006 : : else
9007 : : {
9008 : 18 : decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
9009 : 18 : decl = build_address (decl);
9010 : 18 : expr = get_member_function_from_ptrfunc (&decl, expr, complain);
9011 : : }
9012 : :
9013 : 197 : if (expr == error_mark_node)
9014 : : return error_mark_node;
9015 : :
9016 : 197 : expr = build_nop (type, expr);
9017 : 197 : SET_EXPR_LOCATION (expr, loc);
9018 : 197 : return expr;
9019 : : }
9020 : :
9021 : : /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
9022 : : constexpr evaluation knows to reject it. */
9023 : :
9024 : : static tree
9025 : 101210 : build_nop_reinterpret (tree type, tree expr)
9026 : : {
9027 : 101210 : tree ret = build_nop (type, expr);
9028 : 101210 : if (ret != expr)
9029 : 101210 : REINTERPRET_CAST_P (ret) = true;
9030 : 101210 : return ret;
9031 : : }
9032 : :
9033 : : /* Return a representation for a reinterpret_cast from EXPR to TYPE.
9034 : : If C_CAST_P is true, this reinterpret cast is being done as part of
9035 : : a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
9036 : : indicate whether or not reinterpret_cast was valid. */
9037 : :
9038 : : static tree
9039 : 1925988 : build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
9040 : : bool c_cast_p, bool *valid_p,
9041 : : tsubst_flags_t complain)
9042 : : {
9043 : 1925988 : tree intype;
9044 : :
9045 : : /* Assume the cast is invalid. */
9046 : 1925988 : if (valid_p)
9047 : 1799072 : *valid_p = true;
9048 : :
9049 : 1925988 : if (type == error_mark_node || error_operand_p (expr))
9050 : : return error_mark_node;
9051 : :
9052 : 1925988 : intype = TREE_TYPE (expr);
9053 : :
9054 : : /* Save casted types in the function's used types hash table. */
9055 : 1925988 : used_types_insert (type);
9056 : :
9057 : : /* A prvalue of non-class type is cv-unqualified. */
9058 : 1925988 : if (!CLASS_TYPE_P (type))
9059 : 1925979 : type = cv_unqualified (type);
9060 : :
9061 : : /* [expr.reinterpret.cast]
9062 : : A glvalue of type T1, designating an object x, can be cast to the type
9063 : : "reference to T2" if an expression of type "pointer to T1" can be
9064 : : explicitly converted to the type "pointer to T2" using a reinterpret_cast.
9065 : : The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
9066 : : of type "pointer to T1". No temporary is created, no copy is made, and no
9067 : : constructors (11.4.4) or conversion functions (11.4.7) are called. */
9068 : 1925988 : if (TYPE_REF_P (type))
9069 : : {
9070 : 10242 : if (!glvalue_p (expr))
9071 : : {
9072 : 9 : if (complain & tf_error)
9073 : 9 : error_at (loc, "invalid cast of a prvalue expression of type "
9074 : : "%qT to type %qT",
9075 : : intype, type);
9076 : 9 : return error_mark_node;
9077 : : }
9078 : :
9079 : : /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
9080 : : "B" are related class types; the reinterpret_cast does not
9081 : : adjust the pointer. */
9082 : 10233 : if (TYPE_PTR_P (intype)
9083 : 3 : && (complain & tf_warning)
9084 : 10236 : && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
9085 : : COMPARE_BASE | COMPARE_DERIVED)))
9086 : 3 : warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
9087 : : intype, type);
9088 : :
9089 : 10233 : expr = cp_build_addr_expr (expr, complain);
9090 : :
9091 : 10233 : if (warn_strict_aliasing > 2)
9092 : 71 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9093 : :
9094 : 10233 : if (expr != error_mark_node)
9095 : 10233 : expr = build_reinterpret_cast_1
9096 : 10233 : (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
9097 : : valid_p, complain);
9098 : 10233 : if (expr != error_mark_node)
9099 : : /* cp_build_indirect_ref isn't right for rvalue refs. */
9100 : 10230 : expr = convert_from_reference (fold_convert (type, expr));
9101 : 10233 : return expr;
9102 : : }
9103 : :
9104 : : /* As a G++ extension, we consider conversions from member
9105 : : functions, and pointers to member functions to
9106 : : pointer-to-function and pointer-to-void types. If
9107 : : -Wno-pmf-conversions has not been specified,
9108 : : convert_member_func_to_ptr will issue an error message. */
9109 : 266 : if ((TYPE_PTRMEMFUNC_P (intype)
9110 : 1915558 : || TREE_CODE (intype) == METHOD_TYPE)
9111 : 276 : && TYPE_PTR_P (type)
9112 : 1915943 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
9113 : 155 : || VOID_TYPE_P (TREE_TYPE (type))))
9114 : 188 : return convert_member_func_to_ptr (type, expr, complain);
9115 : :
9116 : : /* If the cast is not to a reference type, the lvalue-to-rvalue,
9117 : : array-to-pointer, and function-to-pointer conversions are
9118 : : performed. */
9119 : 1915558 : expr = decay_conversion (expr, complain);
9120 : :
9121 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9122 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9123 : 1915558 : if (TREE_CODE (expr) == NOP_EXPR
9124 : 1915558 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9125 : 388 : expr = TREE_OPERAND (expr, 0);
9126 : :
9127 : 1915558 : if (error_operand_p (expr))
9128 : 30 : return error_mark_node;
9129 : :
9130 : 1915528 : intype = TREE_TYPE (expr);
9131 : :
9132 : : /* [expr.reinterpret.cast]
9133 : : A pointer can be converted to any integral type large enough to
9134 : : hold it. ... A value of type std::nullptr_t can be converted to
9135 : : an integral type; the conversion has the same meaning and
9136 : : validity as a conversion of (void*)0 to the integral type. */
9137 : 1915528 : if (CP_INTEGRAL_TYPE_P (type)
9138 : 152087 : && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
9139 : : {
9140 : 150174 : if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
9141 : : {
9142 : 21 : if (complain & tf_error)
9143 : 15 : permerror (loc, "cast from %qH to %qI loses precision",
9144 : : intype, type);
9145 : : else
9146 : 6 : return error_mark_node;
9147 : : }
9148 : 150168 : if (NULLPTR_TYPE_P (intype))
9149 : 98 : return build_int_cst (type, 0);
9150 : : }
9151 : : /* [expr.reinterpret.cast]
9152 : : A value of integral or enumeration type can be explicitly
9153 : : converted to a pointer. */
9154 : 1765354 : else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
9155 : : /* OK */
9156 : : ;
9157 : 1728679 : else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
9158 : 1726751 : || TYPE_PTR_OR_PTRMEM_P (type))
9159 : 1830417 : && same_type_p (type, intype))
9160 : : /* DR 799 */
9161 : 496 : return rvalue (expr);
9162 : 1728183 : else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
9163 : : {
9164 : 7277 : if ((complain & tf_warning)
9165 : 14554 : && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
9166 : 7277 : TREE_TYPE (intype)))
9167 : 145 : warning_at (loc, OPT_Wcast_function_type,
9168 : : "cast between incompatible function types"
9169 : : " from %qH to %qI", intype, type);
9170 : 7277 : return build_nop_reinterpret (type, expr);
9171 : : }
9172 : 1720906 : else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
9173 : : {
9174 : 79 : if ((complain & tf_warning)
9175 : 158 : && !cxx_safe_function_type_cast_p
9176 : 79 : (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
9177 : 79 : TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
9178 : 52 : warning_at (loc, OPT_Wcast_function_type,
9179 : : "cast between incompatible pointer to member types"
9180 : : " from %qH to %qI", intype, type);
9181 : 79 : return build_nop_reinterpret (type, expr);
9182 : : }
9183 : 21 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
9184 : 1720827 : || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
9185 : : {
9186 : 93599 : if (!c_cast_p
9187 : 93599 : && check_for_casting_away_constness (loc, intype, type,
9188 : : REINTERPRET_CAST_EXPR,
9189 : : complain))
9190 : 21 : return error_mark_node;
9191 : : /* Warn about possible alignment problems. */
9192 : 93578 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9193 : 24 : && (complain & tf_warning)
9194 : 24 : && !VOID_TYPE_P (type)
9195 : 24 : && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
9196 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (type))
9197 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (intype))
9198 : 93626 : && min_align_of_type (TREE_TYPE (type))
9199 : 24 : > min_align_of_type (TREE_TYPE (intype)))
9200 : 18 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9201 : : "increases required alignment of target type",
9202 : : intype, type);
9203 : :
9204 : 93578 : if (warn_strict_aliasing <= 2)
9205 : : /* strict_aliasing_warning STRIP_NOPs its expr. */
9206 : 81842 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9207 : :
9208 : 93578 : return build_nop_reinterpret (type, expr);
9209 : : }
9210 : 297 : else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
9211 : 1627388 : || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
9212 : : {
9213 : 276 : if (complain & tf_warning)
9214 : : /* C++11 5.2.10 p8 says that "Converting a function pointer to an
9215 : : object pointer type or vice versa is conditionally-supported." */
9216 : 276 : warning_at (loc, OPT_Wconditionally_supported,
9217 : : "casting between pointer-to-function and "
9218 : : "pointer-to-object is conditionally-supported");
9219 : 276 : return build_nop_reinterpret (type, expr);
9220 : : }
9221 : 1626952 : else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
9222 : 1624989 : return convert_to_vector (type, rvalue (expr));
9223 : 1963 : else if (gnu_vector_type_p (intype)
9224 : 1963 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9225 : 1861 : return convert_to_integer_nofold (type, expr);
9226 : : else
9227 : : {
9228 : 102 : if (valid_p)
9229 : 84 : *valid_p = false;
9230 : 102 : if (complain & tf_error)
9231 : 93 : error_at (loc, "invalid cast from type %qT to type %qT",
9232 : : intype, type);
9233 : 102 : return error_mark_node;
9234 : : }
9235 : :
9236 : 186745 : expr = cp_convert (type, expr, complain);
9237 : 186745 : if (TREE_CODE (expr) == NOP_EXPR)
9238 : : /* Mark any nop_expr that created as a reintepret_cast. */
9239 : 0 : REINTERPRET_CAST_P (expr) = true;
9240 : : return expr;
9241 : : }
9242 : :
9243 : : tree
9244 : 612308 : build_reinterpret_cast (location_t loc, tree type, tree expr,
9245 : : tsubst_flags_t complain)
9246 : : {
9247 : 612308 : tree r;
9248 : :
9249 : 612308 : if (type == error_mark_node || expr == error_mark_node)
9250 : : return error_mark_node;
9251 : :
9252 : 612301 : if (processing_template_decl)
9253 : : {
9254 : 495562 : tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
9255 : :
9256 : 495562 : if (!TREE_SIDE_EFFECTS (t)
9257 : 495562 : && type_dependent_expression_p (expr))
9258 : : /* There might turn out to be side effects inside expr. */
9259 : 208980 : TREE_SIDE_EFFECTS (t) = 1;
9260 : 495562 : r = convert_from_reference (t);
9261 : 495562 : protected_set_expr_location (r, loc);
9262 : 495562 : return r;
9263 : : }
9264 : :
9265 : 116739 : r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9266 : : /*valid_p=*/NULL, complain);
9267 : 116739 : if (r != error_mark_node)
9268 : : {
9269 : 116676 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9270 : 116676 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9271 : : }
9272 : 116739 : protected_set_expr_location (r, loc);
9273 : 116739 : return r;
9274 : : }
9275 : :
9276 : : /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9277 : : return an appropriate expression. Otherwise, return
9278 : : error_mark_node. If the cast is not valid, and COMPLAIN is true,
9279 : : then a diagnostic will be issued. If VALID_P is non-NULL, we are
9280 : : performing a C-style cast, its value upon return will indicate
9281 : : whether or not the conversion succeeded. */
9282 : :
9283 : : static tree
9284 : 37740611 : build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9285 : : tsubst_flags_t complain, bool *valid_p)
9286 : : {
9287 : 37740611 : tree src_type;
9288 : 37740611 : tree reference_type;
9289 : :
9290 : : /* Callers are responsible for handling error_mark_node as a
9291 : : destination type. */
9292 : 37740611 : gcc_assert (dst_type != error_mark_node);
9293 : : /* In a template, callers should be building syntactic
9294 : : representations of casts, not using this machinery. */
9295 : 37740611 : gcc_assert (!processing_template_decl);
9296 : :
9297 : : /* Assume the conversion is invalid. */
9298 : 37740611 : if (valid_p)
9299 : 37604704 : *valid_p = false;
9300 : :
9301 : 37740611 : if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9302 : : {
9303 : 37039466 : if (complain & tf_error)
9304 : 9 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9305 : : "which is not a pointer, reference, "
9306 : : "nor a pointer-to-data-member type", dst_type);
9307 : 37039466 : return error_mark_node;
9308 : : }
9309 : :
9310 : 701145 : if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
9311 : : {
9312 : 4097 : if (complain & tf_error)
9313 : 6 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9314 : : "which is a pointer or reference to a function type",
9315 : : dst_type);
9316 : 4097 : return error_mark_node;
9317 : : }
9318 : :
9319 : : /* A prvalue of non-class type is cv-unqualified. */
9320 : 697048 : dst_type = cv_unqualified (dst_type);
9321 : :
9322 : : /* Save casted types in the function's used types hash table. */
9323 : 697048 : used_types_insert (dst_type);
9324 : :
9325 : 697048 : src_type = TREE_TYPE (expr);
9326 : : /* Expressions do not really have reference types. */
9327 : 697048 : if (TYPE_REF_P (src_type))
9328 : 0 : src_type = TREE_TYPE (src_type);
9329 : :
9330 : : /* [expr.const.cast]
9331 : :
9332 : : For two object types T1 and T2, if a pointer to T1 can be explicitly
9333 : : converted to the type "pointer to T2" using a const_cast, then the
9334 : : following conversions can also be made:
9335 : :
9336 : : -- an lvalue of type T1 can be explicitly converted to an lvalue of
9337 : : type T2 using the cast const_cast<T2&>;
9338 : :
9339 : : -- a glvalue of type T1 can be explicitly converted to an xvalue of
9340 : : type T2 using the cast const_cast<T2&&>; and
9341 : :
9342 : : -- if T1 is a class type, a prvalue of type T1 can be explicitly
9343 : : converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9344 : :
9345 : 697048 : if (TYPE_REF_P (dst_type))
9346 : : {
9347 : 66484 : reference_type = dst_type;
9348 : 66484 : if (!TYPE_REF_IS_RVALUE (dst_type)
9349 : 66484 : ? lvalue_p (expr)
9350 : 1210 : : obvalue_p (expr))
9351 : : /* OK. */;
9352 : : else
9353 : : {
9354 : 35 : if (complain & tf_error)
9355 : 9 : error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
9356 : : "to type %qT",
9357 : : src_type, dst_type);
9358 : 35 : return error_mark_node;
9359 : : }
9360 : 66449 : dst_type = build_pointer_type (TREE_TYPE (dst_type));
9361 : 66449 : src_type = build_pointer_type (src_type);
9362 : : }
9363 : : else
9364 : : {
9365 : 630564 : reference_type = NULL_TREE;
9366 : : /* If the destination type is not a reference type, the
9367 : : lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9368 : : conversions are performed. */
9369 : 630564 : src_type = type_decays_to (src_type);
9370 : 630564 : if (src_type == error_mark_node)
9371 : : return error_mark_node;
9372 : : }
9373 : :
9374 : 697013 : if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9375 : : {
9376 : 514083 : if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9377 : : {
9378 : 263917 : if (valid_p)
9379 : : {
9380 : 128088 : *valid_p = true;
9381 : : /* This cast is actually a C-style cast. Issue a warning if
9382 : : the user is making a potentially unsafe cast. */
9383 : 128088 : check_for_casting_away_constness (loc, src_type, dst_type,
9384 : : CAST_EXPR, complain);
9385 : : /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9386 : 128088 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9387 : 3 : && (complain & tf_warning)
9388 : 128094 : && min_align_of_type (TREE_TYPE (dst_type))
9389 : 3 : > min_align_of_type (TREE_TYPE (src_type)))
9390 : 3 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9391 : : "increases required alignment of target type",
9392 : : src_type, dst_type);
9393 : : }
9394 : 263917 : if (reference_type)
9395 : : {
9396 : 65246 : expr = cp_build_addr_expr (expr, complain);
9397 : 65246 : if (expr == error_mark_node)
9398 : : return error_mark_node;
9399 : 65228 : expr = build_nop (reference_type, expr);
9400 : 65228 : return convert_from_reference (expr);
9401 : : }
9402 : : else
9403 : : {
9404 : 198671 : expr = decay_conversion (expr, complain);
9405 : 198671 : if (expr == error_mark_node)
9406 : : return error_mark_node;
9407 : :
9408 : : /* build_c_cast puts on a NOP_EXPR to make the result not an
9409 : : lvalue. Strip such NOP_EXPRs if VALUE is being used in
9410 : : non-lvalue context. */
9411 : 198671 : if (TREE_CODE (expr) == NOP_EXPR
9412 : 198671 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9413 : 3 : expr = TREE_OPERAND (expr, 0);
9414 : 198671 : return build_nop (dst_type, expr);
9415 : : }
9416 : : }
9417 : 250166 : else if (valid_p
9418 : 500284 : && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9419 : 250118 : TREE_TYPE (src_type)))
9420 : 15041 : check_for_casting_away_constness (loc, src_type, dst_type,
9421 : : CAST_EXPR, complain);
9422 : : }
9423 : :
9424 : 433096 : if (complain & tf_error)
9425 : 30 : error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9426 : : src_type, dst_type);
9427 : 433096 : return error_mark_node;
9428 : : }
9429 : :
9430 : : tree
9431 : 579634 : build_const_cast (location_t loc, tree type, tree expr,
9432 : : tsubst_flags_t complain)
9433 : : {
9434 : 579634 : tree r;
9435 : :
9436 : 579634 : if (type == error_mark_node || error_operand_p (expr))
9437 : : return error_mark_node;
9438 : :
9439 : 579626 : if (processing_template_decl)
9440 : : {
9441 : 443719 : tree t = build_min (CONST_CAST_EXPR, type, expr);
9442 : :
9443 : 443719 : if (!TREE_SIDE_EFFECTS (t)
9444 : 443719 : && type_dependent_expression_p (expr))
9445 : : /* There might turn out to be side effects inside expr. */
9446 : 338944 : TREE_SIDE_EFFECTS (t) = 1;
9447 : 443719 : r = convert_from_reference (t);
9448 : 443719 : protected_set_expr_location (r, loc);
9449 : 443719 : return r;
9450 : : }
9451 : :
9452 : 135907 : r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9453 : 135907 : if (r != error_mark_node)
9454 : : {
9455 : 135829 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9456 : 135829 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9457 : : }
9458 : 135907 : protected_set_expr_location (r, loc);
9459 : 135907 : return r;
9460 : : }
9461 : :
9462 : : /* Like cp_build_c_cast, but for the c-common bits. */
9463 : :
9464 : : tree
9465 : 0 : build_c_cast (location_t loc, tree type, tree expr)
9466 : : {
9467 : 0 : return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9468 : : }
9469 : :
9470 : : /* Like the "build_c_cast" used for c-common, but using cp_expr to
9471 : : preserve location information even for tree nodes that don't
9472 : : support it. */
9473 : :
9474 : : cp_expr
9475 : 9534225 : build_c_cast (location_t loc, tree type, cp_expr expr)
9476 : : {
9477 : 9534225 : cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9478 : 9534225 : result.set_location (loc);
9479 : 9534225 : return result;
9480 : : }
9481 : :
9482 : : /* Build an expression representing an explicit C-style cast to type
9483 : : TYPE of expression EXPR. */
9484 : :
9485 : : tree
9486 : 39597794 : cp_build_c_cast (location_t loc, tree type, tree expr,
9487 : : tsubst_flags_t complain)
9488 : : {
9489 : 39597794 : tree value = expr;
9490 : 39597794 : tree result;
9491 : 39597794 : bool valid_p;
9492 : :
9493 : 39597794 : if (type == error_mark_node || error_operand_p (expr))
9494 : : return error_mark_node;
9495 : :
9496 : 39597621 : if (processing_template_decl)
9497 : : {
9498 : 1992908 : tree t = build_min (CAST_EXPR, type,
9499 : : tree_cons (NULL_TREE, value, NULL_TREE));
9500 : : /* We don't know if it will or will not have side effects. */
9501 : 1992908 : TREE_SIDE_EFFECTS (t) = 1;
9502 : 1992908 : return convert_from_reference (t);
9503 : : }
9504 : :
9505 : : /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9506 : : 'Class') should always be retained, because this information aids
9507 : : in method lookup. */
9508 : 37604713 : if (objc_is_object_ptr (type)
9509 : 37604713 : && objc_is_object_ptr (TREE_TYPE (expr)))
9510 : 0 : return build_nop (type, expr);
9511 : :
9512 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9513 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9514 : 37604713 : if (!TYPE_REF_P (type)
9515 : 37602422 : && TREE_CODE (value) == NOP_EXPR
9516 : 37865913 : && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9517 : 124086 : value = TREE_OPERAND (value, 0);
9518 : :
9519 : 37604713 : if (TREE_CODE (type) == ARRAY_TYPE)
9520 : : {
9521 : : /* Allow casting from T1* to T2[] because Cfront allows it.
9522 : : NIHCL uses it. It is not valid ISO C++ however. */
9523 : 3 : if (TYPE_PTR_P (TREE_TYPE (expr)))
9524 : : {
9525 : 0 : if (complain & tf_error)
9526 : 0 : permerror (loc, "ISO C++ forbids casting to an array type %qT",
9527 : : type);
9528 : : else
9529 : 0 : return error_mark_node;
9530 : 0 : type = build_pointer_type (TREE_TYPE (type));
9531 : : }
9532 : : else
9533 : : {
9534 : 3 : if (complain & tf_error)
9535 : 3 : error_at (loc, "ISO C++ forbids casting to an array type %qT",
9536 : : type);
9537 : 3 : return error_mark_node;
9538 : : }
9539 : : }
9540 : :
9541 : 37604710 : if (FUNC_OR_METHOD_TYPE_P (type))
9542 : : {
9543 : 15 : if (complain & tf_error)
9544 : 15 : error_at (loc, "invalid cast to function type %qT", type);
9545 : 15 : return error_mark_node;
9546 : : }
9547 : :
9548 : 37604695 : if (TYPE_PTR_P (type)
9549 : 562808 : && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9550 : : /* Casting to an integer of smaller size is an error detected elsewhere. */
9551 : 181274 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9552 : : /* Don't warn about converting any constant. */
9553 : 37729158 : && !TREE_CONSTANT (value))
9554 : 47 : warning_at (loc, OPT_Wint_to_pointer_cast,
9555 : : "cast to pointer from integer of different size");
9556 : :
9557 : : /* A C-style cast can be a const_cast. */
9558 : 37604695 : result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9559 : : &valid_p);
9560 : 37604695 : if (valid_p)
9561 : : {
9562 : 128079 : if (result != error_mark_node)
9563 : : {
9564 : 128070 : maybe_warn_about_useless_cast (loc, type, value, complain);
9565 : 128070 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9566 : : }
9567 : 9 : else if (complain & tf_error)
9568 : 9 : build_const_cast_1 (loc, type, value, tf_error, &valid_p);
9569 : 128079 : return result;
9570 : : }
9571 : :
9572 : : /* Or a static cast. */
9573 : 37476616 : result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9574 : : &valid_p, complain);
9575 : : /* Or a reinterpret_cast. */
9576 : 37476616 : if (!valid_p)
9577 : 1799016 : result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9578 : : &valid_p, complain);
9579 : : /* The static_cast or reinterpret_cast may be followed by a
9580 : : const_cast. */
9581 : 37476616 : if (valid_p
9582 : : /* A valid cast may result in errors if, for example, a
9583 : : conversion to an ambiguous base class is required. */
9584 : 37476616 : && !error_operand_p (result))
9585 : : {
9586 : 37476211 : tree result_type;
9587 : :
9588 : 37476211 : maybe_warn_about_useless_cast (loc, type, value, complain);
9589 : 37476211 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9590 : :
9591 : : /* Non-class rvalues always have cv-unqualified type. */
9592 : 37476211 : if (!CLASS_TYPE_P (type))
9593 : 35934544 : type = TYPE_MAIN_VARIANT (type);
9594 : 37476211 : result_type = TREE_TYPE (result);
9595 : 37476211 : if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9596 : 35933319 : result_type = TYPE_MAIN_VARIANT (result_type);
9597 : : /* If the type of RESULT does not match TYPE, perform a
9598 : : const_cast to make it match. If the static_cast or
9599 : : reinterpret_cast succeeded, we will differ by at most
9600 : : cv-qualification, so the follow-on const_cast is guaranteed
9601 : : to succeed. */
9602 : 37476211 : if (!same_type_p (non_reference (type), non_reference (result_type)))
9603 : : {
9604 : 0 : result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
9605 : 0 : gcc_assert (valid_p);
9606 : : }
9607 : 37476211 : return result;
9608 : : }
9609 : :
9610 : 405 : return error_mark_node;
9611 : : }
9612 : :
9613 : : /* Warn when a value is moved to itself with std::move. LHS is the target,
9614 : : RHS may be the std::move call, and LOC is the location of the whole
9615 : : assignment. Return true if we warned. */
9616 : :
9617 : : bool
9618 : 25807913 : maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9619 : : {
9620 : 25807913 : if (!warn_self_move)
9621 : : return false;
9622 : :
9623 : : /* C++98 doesn't know move. */
9624 : 353546 : if (cxx_dialect < cxx11)
9625 : : return false;
9626 : :
9627 : 333723 : if (processing_template_decl)
9628 : : return false;
9629 : :
9630 : 24842 : if (!REFERENCE_REF_P (rhs)
9631 : 290354 : || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9632 : : return false;
9633 : 4598 : tree fn = TREE_OPERAND (rhs, 0);
9634 : 4598 : if (!is_std_move_p (fn))
9635 : : return false;
9636 : :
9637 : : /* Just a little helper to strip * and various NOPs. */
9638 : 6417 : auto extract_op = [] (tree &op) {
9639 : 4278 : STRIP_NOPS (op);
9640 : 5475 : while (INDIRECT_REF_P (op))
9641 : 1197 : op = TREE_OPERAND (op, 0);
9642 : 4278 : op = maybe_undo_parenthesized_ref (op);
9643 : 4278 : STRIP_ANY_LOCATION_WRAPPER (op);
9644 : 4278 : };
9645 : :
9646 : 2139 : tree arg = CALL_EXPR_ARG (fn, 0);
9647 : 2139 : extract_op (arg);
9648 : 2139 : if (TREE_CODE (arg) == ADDR_EXPR)
9649 : 1478 : arg = TREE_OPERAND (arg, 0);
9650 : 2139 : tree type = TREE_TYPE (lhs);
9651 : 2139 : tree orig_lhs = lhs;
9652 : 2139 : extract_op (lhs);
9653 : 2139 : if (cp_tree_equal (lhs, arg)
9654 : : /* Also warn in a member-initializer-list, as in : i(std::move(i)). */
9655 : 2139 : || (TREE_CODE (lhs) == FIELD_DECL
9656 : 634 : && TREE_CODE (arg) == COMPONENT_REF
9657 : 294 : && cp_tree_equal (TREE_OPERAND (arg, 0), current_class_ref)
9658 : 6 : && TREE_OPERAND (arg, 1) == lhs))
9659 : 117 : if (warning_at (loc, OPT_Wself_move,
9660 : : "moving %qE of type %qT to itself", orig_lhs, type))
9661 : : return true;
9662 : :
9663 : : return false;
9664 : : }
9665 : :
9666 : : /* For use from the C common bits. */
9667 : : tree
9668 : 4766 : build_modify_expr (location_t location,
9669 : : tree lhs, tree /*lhs_origtype*/,
9670 : : enum tree_code modifycode,
9671 : : location_t /*rhs_location*/, tree rhs,
9672 : : tree /*rhs_origtype*/)
9673 : : {
9674 : 4766 : return cp_build_modify_expr (location, lhs, modifycode, rhs,
9675 : 4766 : tf_warning_or_error);
9676 : : }
9677 : :
9678 : : /* Build an assignment expression of lvalue LHS from value RHS.
9679 : : MODIFYCODE is the code for a binary operator that we use
9680 : : to combine the old value of LHS with RHS to get the new value.
9681 : : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9682 : :
9683 : : C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9684 : :
9685 : : tree
9686 : 33459881 : cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9687 : : tree rhs, tsubst_flags_t complain)
9688 : : {
9689 : 33459881 : lhs = mark_lvalue_use_nonread (lhs);
9690 : :
9691 : 33459881 : tree result = NULL_TREE;
9692 : 33459881 : tree newrhs = rhs;
9693 : 33459881 : tree lhstype = TREE_TYPE (lhs);
9694 : 33459881 : tree olhs = lhs;
9695 : 33459881 : tree olhstype = lhstype;
9696 : 33459881 : bool plain_assign = (modifycode == NOP_EXPR);
9697 : 33459881 : bool compound_side_effects_p = false;
9698 : 33459881 : tree preeval = NULL_TREE;
9699 : :
9700 : : /* Avoid duplicate error messages from operands that had errors. */
9701 : 33459881 : if (error_operand_p (lhs) || error_operand_p (rhs))
9702 : 20 : return error_mark_node;
9703 : :
9704 : 33459943 : while (TREE_CODE (lhs) == COMPOUND_EXPR)
9705 : : {
9706 : 82 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9707 : 70 : compound_side_effects_p = true;
9708 : 82 : lhs = TREE_OPERAND (lhs, 1);
9709 : : }
9710 : :
9711 : : /* Handle control structure constructs used as "lvalues". Note that we
9712 : : leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9713 : 33459861 : switch (TREE_CODE (lhs))
9714 : : {
9715 : : /* Handle --foo = 5; as these are valid constructs in C++. */
9716 : 21 : case PREDECREMENT_EXPR:
9717 : 21 : case PREINCREMENT_EXPR:
9718 : 21 : if (compound_side_effects_p)
9719 : 9 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9720 : 21 : lhs = genericize_compound_lvalue (lhs);
9721 : 149 : maybe_add_compound:
9722 : : /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9723 : : and looked through the COMPOUND_EXPRs, readd them now around
9724 : : the resulting lhs. */
9725 : 149 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9726 : : {
9727 : 9 : lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9728 : 9 : tree *ptr = &TREE_OPERAND (lhs, 1);
9729 : 9 : for (olhs = TREE_OPERAND (olhs, 1);
9730 : 12 : TREE_CODE (olhs) == COMPOUND_EXPR;
9731 : 3 : olhs = TREE_OPERAND (olhs, 1))
9732 : : {
9733 : 3 : *ptr = build2 (COMPOUND_EXPR, lhstype,
9734 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9735 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9736 : : }
9737 : : }
9738 : : break;
9739 : :
9740 : 128 : case MODIFY_EXPR:
9741 : 128 : if (compound_side_effects_p)
9742 : 0 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9743 : 128 : lhs = genericize_compound_lvalue (lhs);
9744 : 128 : goto maybe_add_compound;
9745 : :
9746 : 0 : case MIN_EXPR:
9747 : 0 : case MAX_EXPR:
9748 : : /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9749 : : when neither operand has side-effects. */
9750 : 0 : if (!lvalue_or_else (lhs, lv_assign, complain))
9751 : 0 : return error_mark_node;
9752 : :
9753 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9754 : : && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9755 : :
9756 : 0 : lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9757 : 0 : build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9758 : : boolean_type_node,
9759 : 0 : TREE_OPERAND (lhs, 0),
9760 : 0 : TREE_OPERAND (lhs, 1)),
9761 : 0 : TREE_OPERAND (lhs, 0),
9762 : 0 : TREE_OPERAND (lhs, 1));
9763 : 206 : gcc_fallthrough ();
9764 : :
9765 : : /* Handle (a ? b : c) used as an "lvalue". */
9766 : 206 : case COND_EXPR:
9767 : 206 : {
9768 : : /* Produce (a ? (b = rhs) : (c = rhs))
9769 : : except that the RHS goes through a save-expr
9770 : : so the code to compute it is only emitted once. */
9771 : 206 : if (VOID_TYPE_P (TREE_TYPE (rhs)))
9772 : : {
9773 : 18 : if (complain & tf_error)
9774 : 24 : error_at (cp_expr_loc_or_loc (rhs, loc),
9775 : : "void value not ignored as it ought to be");
9776 : 18 : return error_mark_node;
9777 : : }
9778 : :
9779 : 188 : rhs = stabilize_expr (rhs, &preeval);
9780 : :
9781 : : /* Check this here to avoid odd errors when trying to convert
9782 : : a throw to the type of the COND_EXPR. */
9783 : 188 : if (!lvalue_or_else (lhs, lv_assign, complain))
9784 : 6 : return error_mark_node;
9785 : :
9786 : 182 : tree op1 = TREE_OPERAND (lhs, 1);
9787 : 182 : if (TREE_CODE (op1) != THROW_EXPR)
9788 : 176 : op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
9789 : : /* When sanitizing undefined behavior, even when rhs doesn't need
9790 : : stabilization at this point, the sanitization might add extra
9791 : : SAVE_EXPRs in there and so make sure there is no tree sharing
9792 : : in the rhs, otherwise those SAVE_EXPRs will have initialization
9793 : : only in one of the two branches. */
9794 : 182 : if (sanitize_flags_p (SANITIZE_UNDEFINED
9795 : : | SANITIZE_UNDEFINED_NONDEFAULT))
9796 : 3 : rhs = unshare_expr (rhs);
9797 : 182 : tree op2 = TREE_OPERAND (lhs, 2);
9798 : 182 : if (TREE_CODE (op2) != THROW_EXPR)
9799 : 176 : op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9800 : 182 : tree cond = build_conditional_expr (input_location,
9801 : 182 : TREE_OPERAND (lhs, 0), op1, op2,
9802 : : complain);
9803 : :
9804 : 182 : if (cond == error_mark_node)
9805 : : return cond;
9806 : : /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9807 : : and looked through the COMPOUND_EXPRs, readd them now around
9808 : : the resulting cond before adding the preevaluated rhs. */
9809 : 179 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9810 : : {
9811 : 18 : cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9812 : 18 : TREE_OPERAND (olhs, 0), cond);
9813 : 18 : tree *ptr = &TREE_OPERAND (cond, 1);
9814 : 18 : for (olhs = TREE_OPERAND (olhs, 1);
9815 : 21 : TREE_CODE (olhs) == COMPOUND_EXPR;
9816 : 3 : olhs = TREE_OPERAND (olhs, 1))
9817 : : {
9818 : 3 : *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9819 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9820 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9821 : : }
9822 : : }
9823 : : /* Make sure the code to compute the rhs comes out
9824 : : before the split. */
9825 : 179 : result = cond;
9826 : 179 : goto ret;
9827 : : }
9828 : :
9829 : : default:
9830 : : lhs = olhs;
9831 : : break;
9832 : : }
9833 : :
9834 : 33459655 : if (modifycode == INIT_EXPR)
9835 : : {
9836 : 3460480 : if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9837 : : /* Do the default thing. */;
9838 : 3276803 : else if (TREE_CODE (rhs) == CONSTRUCTOR)
9839 : : {
9840 : : /* Compound literal. */
9841 : 173 : if (! same_type_p (TREE_TYPE (rhs), lhstype))
9842 : : /* Call convert to generate an error; see PR 11063. */
9843 : 0 : rhs = convert (lhstype, rhs);
9844 : 173 : result = cp_build_init_expr (lhs, rhs);
9845 : 173 : TREE_SIDE_EFFECTS (result) = 1;
9846 : 173 : goto ret;
9847 : : }
9848 : 3276630 : else if (! MAYBE_CLASS_TYPE_P (lhstype))
9849 : : /* Do the default thing. */;
9850 : : else
9851 : : {
9852 : 43596 : releasing_vec rhs_vec = make_tree_vector_single (rhs);
9853 : 43596 : result = build_special_member_call (lhs, complete_ctor_identifier,
9854 : : &rhs_vec, lhstype, LOOKUP_NORMAL,
9855 : : complain);
9856 : 43596 : if (result == NULL_TREE)
9857 : 0 : return error_mark_node;
9858 : 43596 : goto ret;
9859 : 43596 : }
9860 : : }
9861 : : else
9862 : : {
9863 : 29999175 : lhs = require_complete_type (lhs, complain);
9864 : 29999175 : if (lhs == error_mark_node)
9865 : : return error_mark_node;
9866 : :
9867 : 29999132 : if (modifycode == NOP_EXPR)
9868 : : {
9869 : 25760165 : maybe_warn_self_move (loc, lhs, rhs);
9870 : :
9871 : 25760165 : if (c_dialect_objc ())
9872 : : {
9873 : 0 : result = objc_maybe_build_modify_expr (lhs, rhs);
9874 : 0 : if (result)
9875 : 0 : goto ret;
9876 : : }
9877 : :
9878 : : /* `operator=' is not an inheritable operator. */
9879 : 25760165 : if (! MAYBE_CLASS_TYPE_P (lhstype))
9880 : : /* Do the default thing. */;
9881 : : else
9882 : : {
9883 : 2649820 : result = build_new_op (input_location, MODIFY_EXPR,
9884 : : LOOKUP_NORMAL, lhs, rhs,
9885 : : make_node (NOP_EXPR), NULL_TREE,
9886 : : /*overload=*/NULL, complain);
9887 : 2649820 : if (result == NULL_TREE)
9888 : 0 : return error_mark_node;
9889 : 2649820 : goto ret;
9890 : : }
9891 : : lhstype = olhstype;
9892 : : }
9893 : : else
9894 : : {
9895 : 4238967 : tree init = NULL_TREE;
9896 : :
9897 : : /* A binary op has been requested. Combine the old LHS
9898 : : value with the RHS producing the value we should actually
9899 : : store into the LHS. */
9900 : 4238967 : gcc_assert (!((TYPE_REF_P (lhstype)
9901 : : && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9902 : : || MAYBE_CLASS_TYPE_P (lhstype)));
9903 : :
9904 : : /* Preevaluate the RHS to make sure its evaluation is complete
9905 : : before the lvalue-to-rvalue conversion of the LHS:
9906 : :
9907 : : [expr.ass] With respect to an indeterminately-sequenced
9908 : : function call, the operation of a compound assignment is a
9909 : : single evaluation. [ Note: Therefore, a function call shall
9910 : : not intervene between the lvalue-to-rvalue conversion and the
9911 : : side effect associated with any single compound assignment
9912 : : operator. -- end note ] */
9913 : 4238967 : lhs = cp_stabilize_reference (lhs);
9914 : 4238967 : rhs = decay_conversion (rhs, complain);
9915 : 4238967 : if (rhs == error_mark_node)
9916 : 123 : return error_mark_node;
9917 : :
9918 : 4238964 : {
9919 : 4238964 : auto_diagnostic_group d;
9920 : 4238964 : rhs = stabilize_expr (rhs, &init);
9921 : 4238964 : bool clear_decl_read = false;
9922 : 4238964 : tree stripped_lhs = tree_strip_any_location_wrapper (lhs);
9923 : 1095759 : if ((VAR_P (stripped_lhs) || TREE_CODE (stripped_lhs) == PARM_DECL)
9924 : 3626547 : && !DECL_READ_P (stripped_lhs)
9925 : 961984 : && (VAR_P (stripped_lhs) ? warn_unused_but_set_variable
9926 : : : warn_unused_but_set_parameter) > 2
9927 : 6851 : && !CLASS_TYPE_P (TREE_TYPE (lhs))
9928 : 4245815 : && !CLASS_TYPE_P (TREE_TYPE (rhs)))
9929 : : {
9930 : 6851 : mark_exp_read (rhs);
9931 : 6851 : if (!DECL_READ_P (stripped_lhs))
9932 : 4238964 : clear_decl_read = true;
9933 : : }
9934 : 4238964 : newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9935 : 4238964 : if (clear_decl_read)
9936 : 6851 : DECL_READ_P (stripped_lhs) = 0;
9937 : 4238964 : if (newrhs == error_mark_node)
9938 : : {
9939 : 120 : if (complain & tf_error)
9940 : 24 : inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9941 : 24 : modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9942 : 120 : return error_mark_node;
9943 : : }
9944 : 4238964 : }
9945 : :
9946 : 4238844 : if (init)
9947 : 288694 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9948 : :
9949 : : /* Now it looks like a plain assignment. */
9950 : 4238844 : modifycode = NOP_EXPR;
9951 : 4238844 : if (c_dialect_objc ())
9952 : : {
9953 : 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
9954 : 0 : if (result)
9955 : 0 : goto ret;
9956 : : }
9957 : : }
9958 : 27349189 : gcc_assert (!TYPE_REF_P (lhstype));
9959 : 27349189 : gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9960 : : }
9961 : :
9962 : : /* The left-hand side must be an lvalue. */
9963 : 30765900 : if (!lvalue_or_else (lhs, lv_assign, complain))
9964 : 154 : return error_mark_node;
9965 : :
9966 : : /* Warn about modifying something that is `const'. Don't warn if
9967 : : this is initialization. */
9968 : 30765746 : if (modifycode != INIT_EXPR
9969 : 30765746 : && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9970 : : /* Functions are not modifiable, even though they are
9971 : : lvalues. */
9972 : 27295398 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9973 : : /* If it's an aggregate and any field is const, then it is
9974 : : effectively const. */
9975 : 27295368 : || (CLASS_TYPE_P (lhstype)
9976 : 0 : && C_TYPE_FIELDS_READONLY (lhstype))))
9977 : : {
9978 : 53667 : if (complain & tf_error)
9979 : 89 : cxx_readonly_error (loc, lhs, lv_assign);
9980 : 53667 : return error_mark_node;
9981 : : }
9982 : :
9983 : : /* If storing into a structure or union member, it may have been given a
9984 : : lowered bitfield type. We need to convert to the declared type first,
9985 : : so retrieve it now. */
9986 : :
9987 : 30712079 : olhstype = unlowered_expr_type (lhs);
9988 : :
9989 : : /* Convert new value to destination type. */
9990 : :
9991 : 30712079 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
9992 : : {
9993 : 183 : int from_array;
9994 : :
9995 : 183 : if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9996 : : {
9997 : 12 : if (modifycode != INIT_EXPR)
9998 : : {
9999 : 12 : if (complain & tf_error)
10000 : 12 : error_at (loc,
10001 : : "assigning to an array from an initializer list");
10002 : 12 : return error_mark_node;
10003 : : }
10004 : 0 : if (check_array_initializer (lhs, lhstype, newrhs))
10005 : 0 : return error_mark_node;
10006 : 0 : newrhs = digest_init (lhstype, newrhs, complain);
10007 : 0 : if (newrhs == error_mark_node)
10008 : : return error_mark_node;
10009 : : }
10010 : :
10011 : : /* C++11 8.5/17: "If the destination type is an array of characters,
10012 : : an array of char16_t, an array of char32_t, or an array of wchar_t,
10013 : : and the initializer is a string literal...". */
10014 : 171 : else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
10015 : : == STRING_CST)
10016 : 43 : && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
10017 : 214 : && modifycode == INIT_EXPR)
10018 : : {
10019 : 28 : newrhs = digest_init (lhstype, newrhs, complain);
10020 : 28 : if (newrhs == error_mark_node)
10021 : : return error_mark_node;
10022 : : }
10023 : :
10024 : 143 : else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
10025 : : TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
10026 : : {
10027 : 17 : if (complain & tf_error)
10028 : 12 : error_at (loc, "incompatible types in assignment of %qT to %qT",
10029 : 12 : TREE_TYPE (rhs), lhstype);
10030 : 17 : return error_mark_node;
10031 : : }
10032 : :
10033 : : /* Allow array assignment in compiler-generated code. */
10034 : 126 : else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
10035 : : /* OK, used by coroutines (co-await-initlist1.C). */;
10036 : 120 : else if (!current_function_decl
10037 : 120 : || !DECL_DEFAULTED_FN (current_function_decl))
10038 : : {
10039 : : /* This routine is used for both initialization and assignment.
10040 : : Make sure the diagnostic message differentiates the context. */
10041 : 73 : if (complain & tf_error)
10042 : : {
10043 : 30 : if (modifycode == INIT_EXPR)
10044 : 9 : error_at (loc, "array used as initializer");
10045 : : else
10046 : 21 : error_at (loc, "invalid array assignment");
10047 : : }
10048 : 73 : return error_mark_node;
10049 : : }
10050 : :
10051 : 128 : from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
10052 : 81 : ? 1 + (modifycode != INIT_EXPR) : 0;
10053 : 81 : result = build_vec_init (lhs, NULL_TREE, newrhs,
10054 : : /*explicit_value_init_p=*/false,
10055 : : from_array, complain);
10056 : 81 : goto ret;
10057 : : }
10058 : :
10059 : 30711896 : if (modifycode == INIT_EXPR)
10060 : : /* Calls with INIT_EXPR are all direct-initialization, so don't set
10061 : : LOOKUP_ONLYCONVERTING. */
10062 : 3416652 : newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
10063 : : ICR_INIT, NULL_TREE, 0,
10064 : : complain | tf_no_cleanup);
10065 : : else
10066 : 27295244 : newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
10067 : : NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
10068 : :
10069 : 30711896 : if (!same_type_p (lhstype, olhstype))
10070 : 281550 : newrhs = cp_convert_and_check (lhstype, newrhs, complain);
10071 : :
10072 : 30711896 : if (modifycode != INIT_EXPR)
10073 : : {
10074 : 27295244 : if (TREE_CODE (newrhs) == CALL_EXPR
10075 : 27295244 : && TYPE_NEEDS_CONSTRUCTING (lhstype))
10076 : 0 : newrhs = build_cplus_new (lhstype, newrhs, complain);
10077 : :
10078 : : /* Can't initialize directly from a TARGET_EXPR, since that would
10079 : : cause the lhs to be constructed twice, and possibly result in
10080 : : accidental self-initialization. So we force the TARGET_EXPR to be
10081 : : expanded without a target. */
10082 : 27295244 : if (TREE_CODE (newrhs) == TARGET_EXPR)
10083 : 84 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
10084 : 84 : TARGET_EXPR_SLOT (newrhs));
10085 : : }
10086 : :
10087 : 30711896 : if (newrhs == error_mark_node)
10088 : : return error_mark_node;
10089 : :
10090 : 30711327 : if (c_dialect_objc () && flag_objc_gc)
10091 : : {
10092 : 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
10093 : :
10094 : 0 : if (result)
10095 : 0 : goto ret;
10096 : : }
10097 : :
10098 : 34127930 : result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
10099 : : lhstype, lhs, newrhs);
10100 : 30711327 : if (modifycode == INIT_EXPR)
10101 : 3416603 : set_target_expr_eliding (newrhs);
10102 : :
10103 : 30711327 : TREE_SIDE_EFFECTS (result) = 1;
10104 : 30711327 : if (!plain_assign)
10105 : 7655417 : suppress_warning (result, OPT_Wparentheses);
10106 : :
10107 : 23055910 : ret:
10108 : 33405176 : if (preeval)
10109 : 33 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
10110 : : return result;
10111 : : }
10112 : :
10113 : : cp_expr
10114 : 61166447 : build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
10115 : : tree rhs, tree lookups, tsubst_flags_t complain)
10116 : : {
10117 : 61166447 : tree orig_lhs = lhs;
10118 : 61166447 : tree orig_rhs = rhs;
10119 : 61166447 : tree overload = NULL_TREE;
10120 : :
10121 : 61166447 : if (lhs == error_mark_node || rhs == error_mark_node)
10122 : 2092 : return cp_expr (error_mark_node, loc);
10123 : :
10124 : 61164355 : tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
10125 : :
10126 : 61164355 : if (processing_template_decl)
10127 : : {
10128 : 40984322 : if (type_dependent_expression_p (lhs)
10129 : 40984322 : || type_dependent_expression_p (rhs))
10130 : : {
10131 : 31545989 : tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
10132 : 31545989 : if (modifycode != NOP_EXPR)
10133 : 5902701 : TREE_TYPE (rval)
10134 : 11805402 : = build_dependent_operator_type (lookups, modifycode, true);
10135 : 31545989 : return rval;
10136 : : }
10137 : : }
10138 : :
10139 : 29618366 : tree rval;
10140 : 29618366 : if (modifycode == NOP_EXPR)
10141 : 23577227 : rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
10142 : : else
10143 : 6041139 : rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
10144 : : lhs, rhs, op, lookups, &overload, complain);
10145 : 29618366 : if (rval == error_mark_node)
10146 : 2002 : return error_mark_node;
10147 : 29616364 : if (processing_template_decl)
10148 : : {
10149 : 9438312 : if (overload != NULL_TREE)
10150 : 1270416 : return (build_min_non_dep_op_overload
10151 : 1270416 : (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
10152 : :
10153 : 8167896 : return (build_min_non_dep
10154 : 8167896 : (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
10155 : : }
10156 : 20178052 : return rval;
10157 : : }
10158 : :
10159 : : /* Helper function for get_delta_difference which assumes FROM is a base
10160 : : class of TO. Returns a delta for the conversion of pointer-to-member
10161 : : of FROM to pointer-to-member of TO. If the conversion is invalid and
10162 : : tf_error is not set in COMPLAIN returns error_mark_node, otherwise
10163 : : returns zero. If FROM is not a base class of TO, returns NULL_TREE.
10164 : : If C_CAST_P is true, this conversion is taking place as part of a
10165 : : C-style cast. */
10166 : :
10167 : : static tree
10168 : 30338 : get_delta_difference_1 (tree from, tree to, bool c_cast_p,
10169 : : tsubst_flags_t complain)
10170 : : {
10171 : 30338 : tree binfo;
10172 : 30338 : base_kind kind;
10173 : :
10174 : 60426 : binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
10175 : : &kind, complain);
10176 : :
10177 : 30338 : if (binfo == error_mark_node)
10178 : : {
10179 : 51 : if (!(complain & tf_error))
10180 : : return error_mark_node;
10181 : :
10182 : 51 : inform (input_location, " in pointer to member function conversion");
10183 : 51 : return size_zero_node;
10184 : : }
10185 : 30287 : else if (binfo)
10186 : : {
10187 : 30139 : if (kind != bk_via_virtual)
10188 : 30046 : return BINFO_OFFSET (binfo);
10189 : : else
10190 : : /* FROM is a virtual base class of TO. Issue an error or warning
10191 : : depending on whether or not this is a reinterpret cast. */
10192 : : {
10193 : 93 : if (!(complain & tf_error))
10194 : : return error_mark_node;
10195 : :
10196 : 78 : error ("pointer to member conversion via virtual base %qT",
10197 : 78 : BINFO_TYPE (binfo_from_vbase (binfo)));
10198 : :
10199 : 78 : return size_zero_node;
10200 : : }
10201 : : }
10202 : : else
10203 : : return NULL_TREE;
10204 : : }
10205 : :
10206 : : /* Get difference in deltas for different pointer to member function
10207 : : types. If the conversion is invalid and tf_error is not set in
10208 : : COMPLAIN, returns error_mark_node, otherwise returns an integer
10209 : : constant of type PTRDIFF_TYPE_NODE and its value is zero if the
10210 : : conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
10211 : : conversions as well. If C_CAST_P is true this conversion is taking
10212 : : place as part of a C-style cast.
10213 : :
10214 : : Note that the naming of FROM and TO is kind of backwards; the return
10215 : : value is what we add to a TO in order to get a FROM. They are named
10216 : : this way because we call this function to find out how to convert from
10217 : : a pointer to member of FROM to a pointer to member of TO. */
10218 : :
10219 : : static tree
10220 : 94409 : get_delta_difference (tree from, tree to,
10221 : : bool allow_inverse_p,
10222 : : bool c_cast_p, tsubst_flags_t complain)
10223 : : {
10224 : 94409 : auto_diagnostic_group d;
10225 : 94409 : tree result;
10226 : :
10227 : 94409 : if (same_type_ignoring_top_level_qualifiers_p (from, to))
10228 : : /* Pointer to member of incomplete class is permitted*/
10229 : 64219 : result = size_zero_node;
10230 : : else
10231 : 30190 : result = get_delta_difference_1 (from, to, c_cast_p, complain);
10232 : :
10233 : 94409 : if (result == error_mark_node)
10234 : : return error_mark_node;
10235 : :
10236 : 94394 : if (!result)
10237 : : {
10238 : 148 : if (!allow_inverse_p)
10239 : : {
10240 : 0 : if (!(complain & tf_error))
10241 : : return error_mark_node;
10242 : :
10243 : 0 : error_not_base_type (from, to);
10244 : 0 : inform (input_location, " in pointer to member conversion");
10245 : 0 : result = size_zero_node;
10246 : : }
10247 : : else
10248 : : {
10249 : 148 : result = get_delta_difference_1 (to, from, c_cast_p, complain);
10250 : :
10251 : 148 : if (result == error_mark_node)
10252 : : return error_mark_node;
10253 : :
10254 : 148 : if (result)
10255 : 148 : result = size_diffop_loc (input_location,
10256 : : size_zero_node, result);
10257 : : else
10258 : : {
10259 : 0 : if (!(complain & tf_error))
10260 : : return error_mark_node;
10261 : :
10262 : 0 : error_not_base_type (from, to);
10263 : 0 : inform (input_location, " in pointer to member conversion");
10264 : 0 : result = size_zero_node;
10265 : : }
10266 : : }
10267 : : }
10268 : :
10269 : 94394 : return convert_to_integer (ptrdiff_type_node, result);
10270 : 94409 : }
10271 : :
10272 : : /* Return a constructor for the pointer-to-member-function TYPE using
10273 : : the other components as specified. */
10274 : :
10275 : : tree
10276 : 64703 : build_ptrmemfunc1 (tree type, tree delta, tree pfn)
10277 : : {
10278 : 64703 : tree u = NULL_TREE;
10279 : 64703 : tree delta_field;
10280 : 64703 : tree pfn_field;
10281 : 64703 : vec<constructor_elt, va_gc> *v;
10282 : :
10283 : : /* Pull the FIELD_DECLs out of the type. */
10284 : 64703 : pfn_field = TYPE_FIELDS (type);
10285 : 64703 : delta_field = DECL_CHAIN (pfn_field);
10286 : :
10287 : : /* Make sure DELTA has the type we want. */
10288 : 64703 : delta = convert_and_check (input_location, delta_type_node, delta);
10289 : :
10290 : : /* Convert to the correct target type if necessary. */
10291 : 64703 : pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10292 : :
10293 : : /* Finish creating the initializer. */
10294 : 64703 : vec_alloc (v, 2);
10295 : 64703 : CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10296 : 64703 : CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10297 : 64703 : u = build_constructor (type, v);
10298 : 64703 : TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10299 : 64703 : TREE_STATIC (u) = (TREE_CONSTANT (u)
10300 : 64601 : && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10301 : : != NULL_TREE)
10302 : 129304 : && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10303 : : != NULL_TREE));
10304 : 64703 : return u;
10305 : : }
10306 : :
10307 : : /* Build a constructor for a pointer to member function. It can be
10308 : : used to initialize global variables, local variable, or used
10309 : : as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10310 : : want to be.
10311 : :
10312 : : If FORCE is nonzero, then force this conversion, even if
10313 : : we would rather not do it. Usually set when using an explicit
10314 : : cast. A C-style cast is being processed iff C_CAST_P is true.
10315 : :
10316 : : Return error_mark_node, if something goes wrong. */
10317 : :
10318 : : tree
10319 : 31809 : build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10320 : : tsubst_flags_t complain)
10321 : : {
10322 : 31809 : tree fn;
10323 : 31809 : tree pfn_type;
10324 : 31809 : tree to_type;
10325 : :
10326 : 31809 : if (error_operand_p (pfn))
10327 : 0 : return error_mark_node;
10328 : :
10329 : 31809 : pfn_type = TREE_TYPE (pfn);
10330 : 31809 : to_type = build_ptrmemfunc_type (type);
10331 : :
10332 : : /* Handle multiple conversions of pointer to member functions. */
10333 : 31809 : if (TYPE_PTRMEMFUNC_P (pfn_type))
10334 : : {
10335 : 29897 : tree delta = NULL_TREE;
10336 : 29897 : tree npfn = NULL_TREE;
10337 : 29897 : tree n;
10338 : :
10339 : 29897 : if (!force
10340 : 29897 : && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
10341 : : LOOKUP_NORMAL, complain))
10342 : : {
10343 : 0 : if (complain & tf_error)
10344 : 0 : error ("invalid conversion to type %qT from type %qT",
10345 : : to_type, pfn_type);
10346 : : else
10347 : 0 : return error_mark_node;
10348 : : }
10349 : :
10350 : 29897 : n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10351 : 29897 : TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10352 : : force,
10353 : : c_cast_p, complain);
10354 : 29897 : if (n == error_mark_node)
10355 : : return error_mark_node;
10356 : :
10357 : 29888 : STRIP_ANY_LOCATION_WRAPPER (pfn);
10358 : :
10359 : : /* We don't have to do any conversion to convert a
10360 : : pointer-to-member to its own type. But, we don't want to
10361 : : just return a PTRMEM_CST if there's an explicit cast; that
10362 : : cast should make the expression an invalid template argument. */
10363 : 29888 : if (TREE_CODE (pfn) != PTRMEM_CST
10364 : 29888 : && same_type_p (to_type, pfn_type))
10365 : : return pfn;
10366 : :
10367 : 29888 : if (TREE_SIDE_EFFECTS (pfn))
10368 : 8 : pfn = save_expr (pfn);
10369 : :
10370 : : /* Obtain the function pointer and the current DELTA. */
10371 : 29888 : if (TREE_CODE (pfn) == PTRMEM_CST)
10372 : 29800 : expand_ptrmemfunc_cst (pfn, &delta, &npfn);
10373 : : else
10374 : : {
10375 : 88 : npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
10376 : 88 : delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
10377 : : }
10378 : :
10379 : : /* Just adjust the DELTA field. */
10380 : 29888 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10381 : : (TREE_TYPE (delta), ptrdiff_type_node));
10382 : 29888 : if (!integer_zerop (n))
10383 : : {
10384 : 50 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
10385 : : n = cp_build_binary_op (input_location,
10386 : : LSHIFT_EXPR, n, integer_one_node,
10387 : : complain);
10388 : 50 : delta = cp_build_binary_op (input_location,
10389 : : PLUS_EXPR, delta, n, complain);
10390 : : }
10391 : 29888 : return build_ptrmemfunc1 (to_type, delta, npfn);
10392 : : }
10393 : :
10394 : : /* Handle null pointer to member function conversions. */
10395 : 1912 : if (null_ptr_cst_p (pfn))
10396 : : {
10397 : 849 : pfn = cp_build_c_cast (input_location,
10398 : 849 : TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10399 : : pfn, complain);
10400 : 849 : return build_ptrmemfunc1 (to_type,
10401 : : integer_zero_node,
10402 : 849 : pfn);
10403 : : }
10404 : :
10405 : 1063 : if (type_unknown_p (pfn))
10406 : 0 : return instantiate_type (type, pfn, complain);
10407 : :
10408 : 1063 : fn = TREE_OPERAND (pfn, 0);
10409 : 1063 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10410 : : /* In a template, we will have preserved the
10411 : : OFFSET_REF. */
10412 : : || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10413 : 1063 : return make_ptrmem_cst (to_type, fn);
10414 : : }
10415 : :
10416 : : /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10417 : : given by CST.
10418 : :
10419 : : ??? There is no consistency as to the types returned for the above
10420 : : values. Some code acts as if it were a sizetype and some as if it were
10421 : : integer_type_node. */
10422 : :
10423 : : void
10424 : 64148 : expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10425 : : {
10426 : 64148 : tree type = TREE_TYPE (cst);
10427 : 64148 : tree fn = PTRMEM_CST_MEMBER (cst);
10428 : 64148 : tree ptr_class, fn_class;
10429 : :
10430 : 64148 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10431 : :
10432 : : /* The class that the function belongs to. */
10433 : 64148 : fn_class = DECL_CONTEXT (fn);
10434 : :
10435 : : /* The class that we're creating a pointer to member of. */
10436 : 64148 : ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10437 : :
10438 : : /* First, calculate the adjustment to the function's class. */
10439 : 64148 : *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10440 : : /*c_cast_p=*/0, tf_warning_or_error);
10441 : :
10442 : 64148 : if (!DECL_VIRTUAL_P (fn))
10443 : : {
10444 : 62920 : tree t = build_addr_func (fn, tf_warning_or_error);
10445 : 62920 : if (TREE_CODE (t) == ADDR_EXPR)
10446 : 62920 : SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10447 : 62920 : *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10448 : : }
10449 : : else
10450 : : {
10451 : : /* If we're dealing with a virtual function, we have to adjust 'this'
10452 : : again, to point to the base which provides the vtable entry for
10453 : : fn; the call will do the opposite adjustment. */
10454 : 1228 : tree orig_class = DECL_CONTEXT (fn);
10455 : 1228 : tree binfo = binfo_or_else (orig_class, fn_class);
10456 : 1228 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10457 : : *delta, BINFO_OFFSET (binfo));
10458 : :
10459 : : /* We set PFN to the vtable offset at which the function can be
10460 : : found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10461 : : case delta is shifted left, and then incremented). */
10462 : 1228 : *pfn = DECL_VINDEX (fn);
10463 : 1228 : *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10464 : : TYPE_SIZE_UNIT (vtable_entry_type));
10465 : :
10466 : 1228 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10467 : : {
10468 : 1228 : case ptrmemfunc_vbit_in_pfn:
10469 : 1228 : *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10470 : : integer_one_node);
10471 : 1228 : break;
10472 : :
10473 : : case ptrmemfunc_vbit_in_delta:
10474 : : *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10475 : : *delta, integer_one_node);
10476 : : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10477 : : *delta, integer_one_node);
10478 : : break;
10479 : :
10480 : : default:
10481 : : gcc_unreachable ();
10482 : : }
10483 : :
10484 : 1228 : *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10485 : : }
10486 : 64148 : }
10487 : :
10488 : : /* Return an expression for PFN from the pointer-to-member function
10489 : : given by T. */
10490 : :
10491 : : static tree
10492 : 90819 : pfn_from_ptrmemfunc (tree t)
10493 : : {
10494 : 90819 : if (TREE_CODE (t) == PTRMEM_CST)
10495 : : {
10496 : 191 : tree delta;
10497 : 191 : tree pfn;
10498 : :
10499 : 191 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10500 : 191 : if (pfn)
10501 : 191 : return pfn;
10502 : : }
10503 : :
10504 : 90628 : return build_ptrmemfunc_access_expr (t, pfn_identifier);
10505 : : }
10506 : :
10507 : : /* Return an expression for DELTA from the pointer-to-member function
10508 : : given by T. */
10509 : :
10510 : : static tree
10511 : 90819 : delta_from_ptrmemfunc (tree t)
10512 : : {
10513 : 90819 : if (TREE_CODE (t) == PTRMEM_CST)
10514 : : {
10515 : 191 : tree delta;
10516 : 191 : tree pfn;
10517 : :
10518 : 191 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10519 : 191 : if (delta)
10520 : 191 : return delta;
10521 : : }
10522 : :
10523 : 90628 : return build_ptrmemfunc_access_expr (t, delta_identifier);
10524 : : }
10525 : :
10526 : : /* Convert value RHS to type TYPE as preparation for an assignment to
10527 : : an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10528 : : implicit conversion is. If FNDECL is non-NULL, we are doing the
10529 : : conversion in order to pass the PARMNUMth argument of FNDECL.
10530 : : If FNDECL is NULL, we are doing the conversion in function pointer
10531 : : argument passing, conversion in initialization, etc. */
10532 : :
10533 : : static tree
10534 : 129719974 : convert_for_assignment (tree type, tree rhs,
10535 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10536 : : tsubst_flags_t complain, int flags)
10537 : : {
10538 : 129719974 : tree rhstype;
10539 : 129719974 : enum tree_code coder;
10540 : :
10541 : 129719974 : location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10542 : 129719974 : bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10543 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10544 : : but preserve location wrappers. */
10545 : 129719974 : if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10546 : 129719974 : && !location_wrapper_p (rhs))
10547 : 8445 : rhs = TREE_OPERAND (rhs, 0);
10548 : :
10549 : : /* Handle [dcl.init.list] direct-list-initialization from
10550 : : single element of enumeration with a fixed underlying type. */
10551 : 129719974 : if (is_direct_enum_init (type, rhs))
10552 : : {
10553 : 20 : tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10554 : 20 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10555 : : {
10556 : 11 : warning_sentinel w (warn_useless_cast);
10557 : 11 : warning_sentinel w2 (warn_ignored_qualifiers);
10558 : 11 : rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10559 : 11 : }
10560 : : else
10561 : 9 : rhs = error_mark_node;
10562 : : }
10563 : :
10564 : 129719974 : rhstype = TREE_TYPE (rhs);
10565 : 129719974 : coder = TREE_CODE (rhstype);
10566 : :
10567 : 1338172 : if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10568 : 131058040 : && vector_types_convertible_p (type, rhstype, true))
10569 : : {
10570 : 1338057 : rhs = mark_rvalue_use (rhs);
10571 : 1338057 : return convert (type, rhs);
10572 : : }
10573 : :
10574 : 128381917 : if (rhs == error_mark_node || rhstype == error_mark_node)
10575 : : return error_mark_node;
10576 : 128381908 : if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10577 : : return error_mark_node;
10578 : :
10579 : : /* The RHS of an assignment cannot have void type. */
10580 : 128381908 : if (coder == VOID_TYPE)
10581 : : {
10582 : 48 : if (complain & tf_error)
10583 : 30 : error_at (rhs_loc, "void value not ignored as it ought to be");
10584 : 48 : return error_mark_node;
10585 : : }
10586 : :
10587 : 128381860 : if (c_dialect_objc ())
10588 : : {
10589 : 0 : int parmno;
10590 : 0 : tree selector;
10591 : 0 : tree rname = fndecl;
10592 : :
10593 : 0 : switch (errtype)
10594 : : {
10595 : : case ICR_ASSIGN:
10596 : : parmno = -1;
10597 : : break;
10598 : 0 : case ICR_INIT:
10599 : 0 : parmno = -2;
10600 : 0 : break;
10601 : 0 : default:
10602 : 0 : selector = objc_message_selector ();
10603 : 0 : parmno = parmnum;
10604 : 0 : if (selector && parmno > 1)
10605 : : {
10606 : 0 : rname = selector;
10607 : 0 : parmno -= 1;
10608 : : }
10609 : : }
10610 : :
10611 : 0 : if (objc_compare_types (type, rhstype, parmno, rname))
10612 : : {
10613 : 0 : rhs = mark_rvalue_use (rhs);
10614 : 0 : return convert (type, rhs);
10615 : : }
10616 : : }
10617 : :
10618 : : /* [expr.ass]
10619 : :
10620 : : The expression is implicitly converted (clause _conv_) to the
10621 : : cv-unqualified type of the left operand.
10622 : :
10623 : : We allow bad conversions here because by the time we get to this point
10624 : : we are committed to doing the conversion. If we end up doing a bad
10625 : : conversion, convert_like will complain. */
10626 : 128381860 : if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10627 : : {
10628 : : /* When -Wno-pmf-conversions is use, we just silently allow
10629 : : conversions from pointers-to-members to plain pointers. If
10630 : : the conversion doesn't work, cp_convert will complain. */
10631 : 1476 : if (!warn_pmf2ptr
10632 : 9 : && TYPE_PTR_P (type)
10633 : 1485 : && TYPE_PTRMEMFUNC_P (rhstype))
10634 : 9 : rhs = cp_convert (strip_top_quals (type), rhs, complain);
10635 : : else
10636 : : {
10637 : 1467 : if (complain & tf_error)
10638 : : {
10639 : 1276 : auto_diagnostic_group d;
10640 : :
10641 : : /* If the right-hand side has unknown type, then it is an
10642 : : overloaded function. Call instantiate_type to get error
10643 : : messages. */
10644 : 1276 : if (rhstype == unknown_type_node)
10645 : : {
10646 : 197 : tree r = instantiate_type (type, rhs, tf_warning_or_error);
10647 : : /* -fpermissive might allow this; recurse. */
10648 : 197 : if (!seen_error ())
10649 : 15 : return convert_for_assignment (type, r, errtype, fndecl,
10650 : : parmnum, complain, flags);
10651 : : }
10652 : 1079 : else if (fndecl)
10653 : 92 : complain_about_bad_argument (rhs_loc,
10654 : : rhstype, type,
10655 : : fndecl, parmnum);
10656 : : else
10657 : : {
10658 : 987 : range_label_for_type_mismatch label (rhstype, type);
10659 : 987 : gcc_rich_location richloc
10660 : : (rhs_loc,
10661 : : has_loc ? &label : NULL,
10662 : 987 : has_loc ? highlight_colors::percent_h : NULL);
10663 : :
10664 : 987 : switch (errtype)
10665 : : {
10666 : 0 : case ICR_DEFAULT_ARGUMENT:
10667 : 0 : error_at (&richloc,
10668 : : "cannot convert %qH to %qI in default argument",
10669 : : rhstype, type);
10670 : 0 : break;
10671 : 6 : case ICR_ARGPASS:
10672 : 6 : error_at (&richloc,
10673 : : "cannot convert %qH to %qI in argument passing",
10674 : : rhstype, type);
10675 : 6 : break;
10676 : 0 : case ICR_CONVERTING:
10677 : 0 : error_at (&richloc, "cannot convert %qH to %qI",
10678 : : rhstype, type);
10679 : 0 : break;
10680 : 549 : case ICR_INIT:
10681 : 549 : error_at (&richloc,
10682 : : "cannot convert %qH to %qI in initialization",
10683 : : rhstype, type);
10684 : 549 : break;
10685 : 18 : case ICR_RETURN:
10686 : 18 : error_at (&richloc, "cannot convert %qH to %qI in return",
10687 : : rhstype, type);
10688 : 18 : break;
10689 : 414 : case ICR_ASSIGN:
10690 : 414 : error_at (&richloc,
10691 : : "cannot convert %qH to %qI in assignment",
10692 : : rhstype, type);
10693 : 414 : break;
10694 : 0 : default:
10695 : 0 : gcc_unreachable();
10696 : : }
10697 : 987 : }
10698 : :
10699 : : /* See if we can be more helpful. */
10700 : 1261 : maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
10701 : :
10702 : 1261 : if (TYPE_PTR_P (rhstype)
10703 : 411 : && TYPE_PTR_P (type)
10704 : 396 : && CLASS_TYPE_P (TREE_TYPE (rhstype))
10705 : 278 : && CLASS_TYPE_P (TREE_TYPE (type))
10706 : 1323 : && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10707 : 3 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10708 : : (TREE_TYPE (rhstype))),
10709 : 3 : "class type %qT is incomplete", TREE_TYPE (rhstype));
10710 : 1276 : }
10711 : 1452 : return error_mark_node;
10712 : : }
10713 : : }
10714 : 128380393 : if (warn_suggest_attribute_format)
10715 : : {
10716 : 2383 : const enum tree_code codel = TREE_CODE (type);
10717 : 2383 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10718 : 596 : && coder == codel
10719 : 508 : && check_missing_format_attribute (type, rhstype)
10720 : 2401 : && (complain & tf_warning))
10721 : 18 : switch (errtype)
10722 : : {
10723 : 0 : case ICR_ARGPASS:
10724 : 0 : case ICR_DEFAULT_ARGUMENT:
10725 : 0 : if (fndecl)
10726 : 0 : warning (OPT_Wsuggest_attribute_format,
10727 : : "parameter %qP of %qD might be a candidate "
10728 : : "for a format attribute", parmnum, fndecl);
10729 : : else
10730 : 0 : warning (OPT_Wsuggest_attribute_format,
10731 : : "parameter might be a candidate "
10732 : : "for a format attribute");
10733 : : break;
10734 : 0 : case ICR_CONVERTING:
10735 : 0 : warning (OPT_Wsuggest_attribute_format,
10736 : : "target of conversion might be a candidate "
10737 : : "for a format attribute");
10738 : 0 : break;
10739 : 6 : case ICR_INIT:
10740 : 6 : warning (OPT_Wsuggest_attribute_format,
10741 : : "target of initialization might be a candidate "
10742 : : "for a format attribute");
10743 : 6 : break;
10744 : 6 : case ICR_RETURN:
10745 : 6 : warning (OPT_Wsuggest_attribute_format,
10746 : : "return type might be a candidate "
10747 : : "for a format attribute");
10748 : 6 : break;
10749 : 6 : case ICR_ASSIGN:
10750 : 6 : warning (OPT_Wsuggest_attribute_format,
10751 : : "left-hand side of assignment might be a candidate "
10752 : : "for a format attribute");
10753 : 6 : break;
10754 : 0 : default:
10755 : 0 : gcc_unreachable();
10756 : : }
10757 : : }
10758 : :
10759 : 128380393 : if (TREE_CODE (type) == BOOLEAN_TYPE)
10760 : 26353476 : maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
10761 : :
10762 : 128380393 : if (complain & tf_warning)
10763 : 126975892 : warn_for_address_of_packed_member (type, rhs);
10764 : :
10765 : 128380393 : return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10766 : 128380393 : complain, flags);
10767 : : }
10768 : :
10769 : : /* Convert RHS to be of type TYPE.
10770 : : If EXP is nonzero, it is the target of the initialization.
10771 : : ERRTYPE indicates what kind of error the implicit conversion is.
10772 : :
10773 : : Two major differences between the behavior of
10774 : : `convert_for_assignment' and `convert_for_initialization'
10775 : : are that references are bashed in the former, while
10776 : : copied in the latter, and aggregates are assigned in
10777 : : the former (operator=) while initialized in the
10778 : : latter (X(X&)).
10779 : :
10780 : : If using constructor make sure no conversion operator exists, if one does
10781 : : exist, an ambiguity exists. */
10782 : :
10783 : : tree
10784 : 115253417 : convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10785 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10786 : : tsubst_flags_t complain)
10787 : : {
10788 : 115253417 : enum tree_code codel = TREE_CODE (type);
10789 : 115253417 : tree rhstype;
10790 : 115253417 : enum tree_code coder;
10791 : :
10792 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10793 : : Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10794 : 115253417 : if (TREE_CODE (rhs) == NOP_EXPR
10795 : 4938038 : && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10796 : 115632201 : && codel != REFERENCE_TYPE)
10797 : 378784 : rhs = TREE_OPERAND (rhs, 0);
10798 : :
10799 : 115253417 : if (type == error_mark_node
10800 : 115253374 : || rhs == error_mark_node
10801 : 230506730 : || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10802 : : return error_mark_node;
10803 : :
10804 : 115253313 : if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10805 : : ;
10806 : 104229359 : else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10807 : 1605647 : && TREE_CODE (type) != ARRAY_TYPE
10808 : 1605647 : && (!TYPE_REF_P (type)
10809 : 1015 : || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10810 : 102624724 : || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10811 : 14056 : && !TYPE_REFFN_P (type))
10812 : 206841749 : || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10813 : 1616975 : rhs = decay_conversion (rhs, complain);
10814 : :
10815 : 115253313 : rhstype = TREE_TYPE (rhs);
10816 : 115253313 : coder = TREE_CODE (rhstype);
10817 : :
10818 : 115253313 : if (coder == ERROR_MARK)
10819 : 9 : return error_mark_node;
10820 : :
10821 : : /* We accept references to incomplete types, so we can
10822 : : return here before checking if RHS is of complete type. */
10823 : :
10824 : 115253304 : if (codel == REFERENCE_TYPE)
10825 : : {
10826 : 5277984 : auto_diagnostic_group d;
10827 : : /* This should eventually happen in convert_arguments. */
10828 : 5277984 : int savew = 0, savee = 0;
10829 : :
10830 : 5277984 : if (fndecl)
10831 : 258116 : savew = warningcount + werrorcount, savee = errorcount;
10832 : 5277984 : rhs = initialize_reference (type, rhs, flags, complain);
10833 : :
10834 : 5277984 : if (fndecl
10835 : 5277984 : && (warningcount + werrorcount > savew || errorcount > savee))
10836 : 36 : inform (get_fndecl_argument_location (fndecl, parmnum),
10837 : : "in passing argument %P of %qD", parmnum, fndecl);
10838 : 5277984 : return rhs;
10839 : 5277984 : }
10840 : :
10841 : 109975320 : if (exp != 0)
10842 : 3416652 : exp = require_complete_type (exp, complain);
10843 : 109975320 : if (exp == error_mark_node)
10844 : : return error_mark_node;
10845 : :
10846 : 109975320 : type = complete_type (type);
10847 : :
10848 : 109975320 : if (DIRECT_INIT_EXPR_P (type, rhs))
10849 : : /* Don't try to do copy-initialization if we already have
10850 : : direct-initialization. */
10851 : : return rhs;
10852 : :
10853 : 109974348 : if (MAYBE_CLASS_TYPE_P (type))
10854 : 7549633 : return perform_implicit_conversion_flags (type, rhs, complain, flags);
10855 : :
10856 : 102424715 : return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10857 : 102424715 : complain, flags);
10858 : : }
10859 : :
10860 : : /* If RETVAL is the address of, or a reference to, a local variable or
10861 : : temporary give an appropriate warning and return true. */
10862 : :
10863 : : static bool
10864 : 37397278 : maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10865 : : {
10866 : 37397972 : tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10867 : 37397972 : tree whats_returned = fold_for_warn (retval);
10868 : 37397972 : if (!loc)
10869 : 37397972 : loc = cp_expr_loc_or_input_loc (retval);
10870 : :
10871 : 45345129 : for (;;)
10872 : : {
10873 : 45345129 : if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10874 : 508080 : whats_returned = TREE_OPERAND (whats_returned, 1);
10875 : 44837049 : else if (CONVERT_EXPR_P (whats_returned)
10876 : 37418884 : || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10877 : 7439077 : whats_returned = TREE_OPERAND (whats_returned, 0);
10878 : : else
10879 : : break;
10880 : : }
10881 : :
10882 : 37397972 : if (TREE_CODE (whats_returned) == TARGET_EXPR
10883 : 37397972 : && is_std_init_list (TREE_TYPE (whats_returned)))
10884 : : {
10885 : 16 : tree init = TARGET_EXPR_INITIAL (whats_returned);
10886 : 16 : if (TREE_CODE (init) == CONSTRUCTOR)
10887 : : /* Pull out the array address. */
10888 : 6 : whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10889 : 10 : else if (TREE_CODE (init) == INDIRECT_REF)
10890 : : /* The source of a trivial copy looks like *(T*)&var. */
10891 : 7 : whats_returned = TREE_OPERAND (init, 0);
10892 : : else
10893 : : return false;
10894 : 13 : STRIP_NOPS (whats_returned);
10895 : : }
10896 : :
10897 : : /* As a special case, we handle a call to std::move or std::forward. */
10898 : 37397969 : if (TREE_CODE (whats_returned) == CALL_EXPR
10899 : 37397969 : && (is_std_move_p (whats_returned)
10900 : 10104496 : || is_std_forward_p (whats_returned)))
10901 : : {
10902 : 682 : tree arg = CALL_EXPR_ARG (whats_returned, 0);
10903 : 682 : return maybe_warn_about_returning_address_of_local (arg, loc);
10904 : : }
10905 : :
10906 : 37397287 : if (TREE_CODE (whats_returned) != ADDR_EXPR)
10907 : : return false;
10908 : 811616 : whats_returned = TREE_OPERAND (whats_returned, 0);
10909 : :
10910 : 811616 : while (TREE_CODE (whats_returned) == COMPONENT_REF
10911 : 1570656 : || TREE_CODE (whats_returned) == ARRAY_REF)
10912 : 759040 : whats_returned = TREE_OPERAND (whats_returned, 0);
10913 : :
10914 : 811616 : if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10915 : 811616 : || TREE_CODE (whats_returned) == TARGET_EXPR)
10916 : : {
10917 : 50 : if (TYPE_REF_P (valtype))
10918 : : /* P2748 made this an error in C++26. */
10919 : 72 : emit_diagnostic ((cxx_dialect >= cxx26
10920 : : ? diagnostics::kind::permerror
10921 : : : diagnostics::kind::warning),
10922 : 41 : loc, OPT_Wreturn_local_addr,
10923 : : "returning reference to temporary");
10924 : 9 : else if (TYPE_PTR_P (valtype))
10925 : 3 : warning_at (loc, OPT_Wreturn_local_addr,
10926 : : "returning pointer to temporary");
10927 : 6 : else if (is_std_init_list (valtype))
10928 : 6 : warning_at (loc, OPT_Winit_list_lifetime,
10929 : : "returning temporary %<initializer_list%> does not extend "
10930 : : "the lifetime of the underlying array");
10931 : 50 : return true;
10932 : : }
10933 : :
10934 : 811566 : STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10935 : :
10936 : 811566 : if (DECL_P (whats_returned)
10937 : 85197 : && DECL_NAME (whats_returned)
10938 : 85197 : && DECL_FUNCTION_SCOPE_P (whats_returned)
10939 : 12701 : && !is_capture_proxy (whats_returned)
10940 : 824246 : && !(TREE_STATIC (whats_returned)
10941 : 146 : || TREE_PUBLIC (whats_returned)))
10942 : : {
10943 : 106 : if (DECL_DECOMPOSITION_P (whats_returned)
10944 : 39 : && !DECL_DECOMP_IS_BASE (whats_returned)
10945 : 185 : && DECL_HAS_VALUE_EXPR_P (whats_returned))
10946 : : {
10947 : : /* When returning address of a structured binding, if the structured
10948 : : binding is not a reference, continue normally, if it is a
10949 : : reference, recurse on the initializer of the structured
10950 : : binding. */
10951 : 39 : tree base = DECL_DECOMP_BASE (whats_returned);
10952 : 39 : if (TYPE_REF_P (TREE_TYPE (base)))
10953 : : {
10954 : 15 : if (tree init = DECL_INITIAL (base))
10955 : : return maybe_warn_about_returning_address_of_local (init, loc);
10956 : : else
10957 : : return false;
10958 : : }
10959 : : }
10960 : 131 : bool w = false;
10961 : 131 : auto_diagnostic_group d;
10962 : 131 : if (TYPE_REF_P (valtype))
10963 : 81 : w = warning_at (loc, OPT_Wreturn_local_addr,
10964 : : "reference to local variable %qD returned",
10965 : : whats_returned);
10966 : 50 : else if (is_std_init_list (valtype))
10967 : 3 : w = warning_at (loc, OPT_Winit_list_lifetime,
10968 : : "returning local %<initializer_list%> variable %qD "
10969 : : "does not extend the lifetime of the underlying array",
10970 : : whats_returned);
10971 : 47 : else if (POINTER_TYPE_P (valtype)
10972 : 41 : && TREE_CODE (whats_returned) == LABEL_DECL)
10973 : 6 : w = warning_at (loc, OPT_Wreturn_local_addr,
10974 : : "address of label %qD returned",
10975 : : whats_returned);
10976 : 41 : else if (POINTER_TYPE_P (valtype))
10977 : 35 : w = warning_at (loc, OPT_Wreturn_local_addr,
10978 : : "address of local variable %qD returned",
10979 : : whats_returned);
10980 : 125 : if (w)
10981 : 103 : inform (DECL_SOURCE_LOCATION (whats_returned),
10982 : : "declared here");
10983 : 131 : return true;
10984 : 131 : }
10985 : :
10986 : : return false;
10987 : : }
10988 : :
10989 : : /* Returns true if DECL is in the std namespace. */
10990 : :
10991 : : bool
10992 : 77301734 : decl_in_std_namespace_p (tree decl)
10993 : : {
10994 : 89892639 : while (decl)
10995 : : {
10996 : 89434304 : decl = decl_namespace_context (decl);
10997 : 89434304 : if (DECL_NAMESPACE_STD_P (decl))
10998 : : return true;
10999 : : /* Allow inline namespaces inside of std namespace, e.g. with
11000 : : --enable-symvers=gnu-versioned-namespace std::forward would be
11001 : : actually std::_8::forward. */
11002 : 43192309 : if (!DECL_NAMESPACE_INLINE_P (decl))
11003 : : return false;
11004 : 12590905 : decl = CP_DECL_CONTEXT (decl);
11005 : : }
11006 : : return false;
11007 : : }
11008 : :
11009 : : /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
11010 : :
11011 : : static bool
11012 : 10104496 : is_std_forward_p (tree fn)
11013 : : {
11014 : : /* std::forward only takes one argument. */
11015 : 10104496 : if (call_expr_nargs (fn) != 1)
11016 : : return false;
11017 : :
11018 : 4804973 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
11019 : 4804973 : if (!decl_in_std_namespace_p (fndecl))
11020 : : return false;
11021 : :
11022 : 1680327 : tree name = DECL_NAME (fndecl);
11023 : 1680327 : return name && id_equal (name, "forward");
11024 : : }
11025 : :
11026 : : /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
11027 : :
11028 : : static bool
11029 : 10112068 : is_std_move_p (tree fn)
11030 : : {
11031 : : /* std::move only takes one argument. */
11032 : 10112068 : if (call_expr_nargs (fn) != 1)
11033 : : return false;
11034 : :
11035 : 4811768 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
11036 : 4811768 : if (!decl_in_std_namespace_p (fndecl))
11037 : : return false;
11038 : :
11039 : 1686640 : tree name = DECL_NAME (fndecl);
11040 : 1686640 : return name && id_equal (name, "move");
11041 : : }
11042 : :
11043 : : /* Returns true if RETVAL is a good candidate for the NRVO as per
11044 : : [class.copy.elision]. FUNCTYPE is the type the function is declared
11045 : : to return. */
11046 : :
11047 : : static bool
11048 : 47290337 : can_do_nrvo_p (tree retval, tree functype)
11049 : : {
11050 : 47290337 : if (functype == error_mark_node)
11051 : : return false;
11052 : 47290291 : if (retval)
11053 : 45739818 : STRIP_ANY_LOCATION_WRAPPER (retval);
11054 : 47290291 : tree result = DECL_RESULT (current_function_decl);
11055 : 47290291 : return (retval != NULL_TREE
11056 : 45739818 : && !processing_template_decl
11057 : : /* Must be a local, automatic variable. */
11058 : 38232231 : && VAR_P (retval)
11059 : 3411368 : && DECL_CONTEXT (retval) == current_function_decl
11060 : 2667765 : && !TREE_STATIC (retval)
11061 : : /* And not a lambda or anonymous union proxy. */
11062 : 2665017 : && !DECL_HAS_VALUE_EXPR_P (retval)
11063 : 2664769 : && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
11064 : : /* The cv-unqualified type of the returned value must be the
11065 : : same as the cv-unqualified return type of the
11066 : : function. */
11067 : 2664654 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
11068 : : TYPE_MAIN_VARIANT (functype))
11069 : : /* And the returned value must be non-volatile. */
11070 : 49943303 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
11071 : : }
11072 : :
11073 : : /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
11074 : : would otherwise return in memory. */
11075 : :
11076 : : static bool
11077 : 47289978 : want_nrvo_p (tree retval, tree functype)
11078 : : {
11079 : 47289978 : return (can_do_nrvo_p (retval, functype)
11080 : 47289978 : && aggregate_value_p (functype, current_function_decl));
11081 : : }
11082 : :
11083 : : /* Like can_do_nrvo_p, but we check if we're trying to move a class
11084 : : prvalue. */
11085 : :
11086 : : static bool
11087 : 1726 : can_elide_copy_prvalue_p (tree retval, tree functype)
11088 : : {
11089 : 1726 : if (functype == error_mark_node)
11090 : : return false;
11091 : 1726 : if (retval)
11092 : 1726 : STRIP_ANY_LOCATION_WRAPPER (retval);
11093 : 1726 : return (retval != NULL_TREE
11094 : 1726 : && !glvalue_p (retval)
11095 : 135 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
11096 : : TYPE_MAIN_VARIANT (functype))
11097 : 1834 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
11098 : : }
11099 : :
11100 : : /* If we should treat RETVAL, an expression being returned, as if it were
11101 : : designated by an rvalue, returns it adjusted accordingly; otherwise, returns
11102 : : NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
11103 : : context (rather than throw). */
11104 : :
11105 : : tree
11106 : 9205829 : treat_lvalue_as_rvalue_p (tree expr, bool return_p)
11107 : : {
11108 : 9205829 : if (cxx_dialect == cxx98)
11109 : : return NULL_TREE;
11110 : :
11111 : 9197921 : tree retval = expr;
11112 : 9197921 : STRIP_ANY_LOCATION_WRAPPER (retval);
11113 : 9197921 : if (REFERENCE_REF_P (retval))
11114 : 638820 : retval = TREE_OPERAND (retval, 0);
11115 : :
11116 : : /* An implicitly movable entity is a variable of automatic storage duration
11117 : : that is either a non-volatile object or (C++20) an rvalue reference to a
11118 : : non-volatile object type. */
11119 : 9197921 : if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
11120 : 8242533 : || TREE_CODE (retval) == PARM_DECL)
11121 : 1330934 : && !TREE_STATIC (retval)
11122 : 1291619 : && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
11123 : 1291614 : && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
11124 : 109949 : || (cxx_dialect >= cxx20
11125 : 93488 : && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
11126 : 8015683 : return NULL_TREE;
11127 : :
11128 : : /* If the expression in a return or co_return statement is a (possibly
11129 : : parenthesized) id-expression that names an implicitly movable entity
11130 : : declared in the body or parameter-declaration-clause of the innermost
11131 : : enclosing function or lambda-expression, */
11132 : 1182238 : if (return_p)
11133 : : {
11134 : 1172342 : if (DECL_CONTEXT (retval) != current_function_decl)
11135 : : return NULL_TREE;
11136 : 1172276 : expr = move (expr);
11137 : 1172276 : if (expr == error_mark_node)
11138 : : return NULL_TREE;
11139 : 1172274 : return set_implicit_rvalue_p (expr);
11140 : : }
11141 : :
11142 : : /* if the id-expression (possibly parenthesized) is the operand of
11143 : : a throw-expression, and names an implicitly movable entity that belongs
11144 : : to a scope that does not contain the compound-statement of the innermost
11145 : : lambda-expression, try-block, or function-try-block (if any) whose
11146 : : compound-statement or ctor-initializer contains the throw-expression. */
11147 : :
11148 : : /* C++20 added move on throw of parms. */
11149 : 9896 : if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
11150 : : return NULL_TREE;
11151 : :
11152 : : /* We don't check for lambda-expression here, because we should not get past
11153 : : the DECL_HAS_VALUE_EXPR_P check above. */
11154 : 2702 : for (cp_binding_level *b = current_binding_level;
11155 : 2796 : b->kind != sk_namespace; b = b->level_chain)
11156 : : {
11157 : 2794 : for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
11158 : 101 : if (decl == retval)
11159 : 89 : return set_implicit_rvalue_p (move (expr));
11160 : 2693 : if (b->kind == sk_try)
11161 : : return NULL_TREE;
11162 : : }
11163 : :
11164 : 14 : return set_implicit_rvalue_p (move (expr));
11165 : : }
11166 : :
11167 : : /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
11168 : : is true). EXPR is the std::move expression; TYPE is the type of the object
11169 : : being initialized. */
11170 : :
11171 : : void
11172 : 134522421 : maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
11173 : : {
11174 : 134522421 : if (!(warn_pessimizing_move || warn_redundant_move))
11175 : : return;
11176 : :
11177 : 4493388 : const location_t loc = cp_expr_loc_or_input_loc (expr);
11178 : :
11179 : : /* C++98 doesn't know move. */
11180 : 4493388 : if (cxx_dialect < cxx11)
11181 : : return;
11182 : :
11183 : : /* Wait until instantiation time, since we can't gauge if we should do
11184 : : the NRVO until then. */
11185 : 4430986 : if (processing_template_decl)
11186 : : return;
11187 : :
11188 : : /* This is only interesting for class types. */
11189 : 4345491 : if (!CLASS_TYPE_P (type))
11190 : : return;
11191 : :
11192 : 102771 : bool wrapped_p = false;
11193 : : /* A a = std::move (A()); */
11194 : 102771 : if (TREE_CODE (expr) == TREE_LIST)
11195 : : {
11196 : 8482 : if (list_length (expr) == 1)
11197 : : {
11198 : 5774 : expr = TREE_VALUE (expr);
11199 : 5774 : wrapped_p = true;
11200 : : }
11201 : : else
11202 : : return;
11203 : : }
11204 : : /* A a = {std::move (A())};
11205 : : A a{std::move (A())}; */
11206 : 94289 : else if (TREE_CODE (expr) == CONSTRUCTOR)
11207 : : {
11208 : 6882 : if (CONSTRUCTOR_NELTS (expr) == 1)
11209 : : {
11210 : 804 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
11211 : 804 : wrapped_p = true;
11212 : : }
11213 : : else
11214 : : return;
11215 : : }
11216 : :
11217 : : /* First, check if this is a call to std::move. */
11218 : 6803 : if (!REFERENCE_REF_P (expr)
11219 : 98898 : || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
11220 : : return;
11221 : 2947 : tree fn = TREE_OPERAND (expr, 0);
11222 : 2947 : if (!is_std_move_p (fn))
11223 : : return;
11224 : 2364 : tree arg = CALL_EXPR_ARG (fn, 0);
11225 : 2364 : if (TREE_CODE (arg) != NOP_EXPR)
11226 : : return;
11227 : : /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
11228 : : and implicitly-movable warnings. */
11229 : 2364 : if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
11230 : : {
11231 : 1726 : arg = TREE_OPERAND (arg, 0);
11232 : 1726 : arg = TREE_OPERAND (arg, 0);
11233 : 1726 : arg = convert_from_reference (arg);
11234 : 1726 : if (can_elide_copy_prvalue_p (arg, type))
11235 : : {
11236 : 108 : auto_diagnostic_group d;
11237 : 108 : if (warning_at (loc, OPT_Wpessimizing_move,
11238 : : "moving a temporary object prevents copy elision"))
11239 : 108 : inform (loc, "remove %<std::move%> call");
11240 : 108 : }
11241 : : /* The rest of the warnings is only relevant for when we are returning
11242 : : from a function. */
11243 : 1726 : if (!return_p)
11244 : : return;
11245 : :
11246 : 359 : tree moved;
11247 : : /* Warn if we could do copy elision were it not for the move. */
11248 : 359 : if (can_do_nrvo_p (arg, type))
11249 : : {
11250 : 48 : auto_diagnostic_group d;
11251 : 48 : if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
11252 : 48 : && warning_at (loc, OPT_Wpessimizing_move,
11253 : : "moving a local object in a return statement "
11254 : : "prevents copy elision"))
11255 : 42 : inform (loc, "remove %<std::move%> call");
11256 : 48 : }
11257 : : /* Warn if the move is redundant. It is redundant when we would
11258 : : do maybe-rvalue overload resolution even without std::move. */
11259 : 311 : else if (warn_redundant_move
11260 : : /* This doesn't apply for return {std::move (t)};. */
11261 : 200 : && !wrapped_p
11262 : 197 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11263 : 407 : && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
11264 : : {
11265 : : /* Make sure that overload resolution would actually succeed
11266 : : if we removed the std::move call. */
11267 : 54 : tree t = convert_for_initialization (NULL_TREE, type,
11268 : : moved,
11269 : : (LOOKUP_NORMAL
11270 : : | LOOKUP_ONLYCONVERTING),
11271 : : ICR_RETURN, NULL_TREE, 0,
11272 : : tf_none);
11273 : : /* If this worked, implicit rvalue would work, so the call to
11274 : : std::move is redundant. */
11275 : 54 : if (t != error_mark_node)
11276 : : {
11277 : 54 : auto_diagnostic_group d;
11278 : 54 : if (warning_at (loc, OPT_Wredundant_move,
11279 : : "redundant move in return statement"))
11280 : 54 : inform (loc, "remove %<std::move%> call");
11281 : 54 : }
11282 : : }
11283 : : }
11284 : : /* Also try to warn about redundant std::move in code such as
11285 : : T f (const T& t)
11286 : : {
11287 : : return std::move(t);
11288 : : }
11289 : : for which EXPR will be something like
11290 : : *std::move<const T&> ((const struct T &) (const struct T *) t)
11291 : : and where the std::move does nothing if T does not have a T(const T&&)
11292 : : constructor, because the argument is const. It will not use T(T&&)
11293 : : because that would mean losing the const. */
11294 : 638 : else if (warn_redundant_move
11295 : 493 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11296 : 64 : && TYPE_REF_P (TREE_TYPE (arg))
11297 : 702 : && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
11298 : : {
11299 : 18 : tree rtype = TREE_TYPE (TREE_TYPE (arg));
11300 : 18 : if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
11301 : 9 : return;
11302 : : /* Check for the unlikely case there's T(const T&&) (we don't care if
11303 : : it's deleted). */
11304 : 54 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
11305 : 30 : if (move_fn_p (fn))
11306 : : {
11307 : 15 : tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11308 : 15 : if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
11309 : 6 : return;
11310 : : }
11311 : 9 : auto_diagnostic_group d;
11312 : 18 : if (return_p
11313 : 9 : ? warning_at (loc, OPT_Wredundant_move,
11314 : : "redundant move in return statement")
11315 : 9 : : warning_at (loc, OPT_Wredundant_move,
11316 : : "redundant move in initialization"))
11317 : 9 : inform (loc, "remove %<std::move%> call");
11318 : 9 : }
11319 : : }
11320 : :
11321 : : /* Check that returning RETVAL from the current function is valid.
11322 : : Return an expression explicitly showing all conversions required to
11323 : : change RETVAL into the function return type, and to assign it to
11324 : : the DECL_RESULT for the function. Set *NO_WARNING to true if
11325 : : code reaches end of non-void function warning shouldn't be issued
11326 : : on this RETURN_EXPR. Set *DANGLING to true if code returns the
11327 : : address of a local variable. */
11328 : :
11329 : : tree
11330 : 109381076 : check_return_expr (tree retval, bool *no_warning, bool *dangling)
11331 : : {
11332 : 109381076 : tree result;
11333 : : /* The type actually returned by the function. */
11334 : 109381076 : tree valtype;
11335 : : /* The type the function is declared to return, or void if
11336 : : the declared type is incomplete. */
11337 : 109381076 : tree functype;
11338 : 109381076 : int fn_returns_value_p;
11339 : 109381076 : location_t loc = cp_expr_loc_or_input_loc (retval);
11340 : :
11341 : 109381076 : *no_warning = false;
11342 : 109381076 : *dangling = false;
11343 : :
11344 : : /* A `volatile' function is one that isn't supposed to return, ever.
11345 : : (This is a G++ extension, used to get better code for functions
11346 : : that call the `volatile' function.) */
11347 : 109381076 : if (TREE_THIS_VOLATILE (current_function_decl))
11348 : 12 : warning (0, "function declared %<noreturn%> has a %<return%> statement");
11349 : :
11350 : : /* Check for various simple errors. */
11351 : 218762152 : if (DECL_DESTRUCTOR_P (current_function_decl))
11352 : : {
11353 : 3451 : if (retval)
11354 : 3 : error_at (loc, "returning a value from a destructor");
11355 : :
11356 : 3451 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11357 : 0 : retval = current_class_ptr;
11358 : : else
11359 : : return NULL_TREE;
11360 : : }
11361 : 109377625 : else if (DECL_CONSTRUCTOR_P (current_function_decl))
11362 : : {
11363 : 58229 : if (in_function_try_handler)
11364 : : /* If a return statement appears in a handler of the
11365 : : function-try-block of a constructor, the program is ill-formed. */
11366 : 0 : error ("cannot return from a handler of a function-try-block of a constructor");
11367 : 58229 : else if (retval)
11368 : : /* You can't return a value from a constructor. */
11369 : 3 : error_at (loc, "returning a value from a constructor");
11370 : :
11371 : 58229 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11372 : 0 : retval = current_class_ptr;
11373 : : else
11374 : : return NULL_TREE;
11375 : : }
11376 : :
11377 : 109319396 : const tree saved_retval = retval;
11378 : :
11379 : 109319396 : if (processing_template_decl)
11380 : : {
11381 : 70372406 : current_function_returns_value = 1;
11382 : :
11383 : 70372406 : if (check_for_bare_parameter_packs (retval))
11384 : 14 : return error_mark_node;
11385 : :
11386 : : /* If one of the types might be void, we can't tell whether we're
11387 : : returning a value. */
11388 : 70372392 : if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11389 : 27838848 : && !FNDECL_USED_AUTO (current_function_decl))
11390 : 42533553 : || (retval != NULL_TREE
11391 : 41429546 : && (TREE_TYPE (retval) == NULL_TREE
11392 : 28945329 : || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11393 : 47781885 : goto dependent;
11394 : : }
11395 : :
11396 : 61537497 : functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11397 : :
11398 : : /* Deduce auto return type from a return statement. */
11399 : 61537497 : if (FNDECL_USED_AUTO (current_function_decl))
11400 : : {
11401 : 1054300 : tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11402 : 1054300 : tree auto_node;
11403 : 1054300 : tree type;
11404 : :
11405 : 1054300 : if (!retval && !is_auto (pattern))
11406 : : {
11407 : : /* Give a helpful error message. */
11408 : 3 : auto_diagnostic_group d;
11409 : 3 : error ("return-statement with no value, in function returning %qT",
11410 : : pattern);
11411 : 3 : inform (input_location, "only plain %<auto%> return type can be "
11412 : : "deduced to %<void%>");
11413 : 3 : type = error_mark_node;
11414 : 3 : }
11415 : 1054297 : else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11416 : : {
11417 : 6 : error ("returning initializer list");
11418 : 6 : type = error_mark_node;
11419 : : }
11420 : : else
11421 : : {
11422 : 1054291 : if (!retval)
11423 : 5767 : retval = void_node;
11424 : 1054291 : auto_node = type_uses_auto (pattern);
11425 : 1054291 : type = do_auto_deduction (pattern, retval, auto_node,
11426 : : tf_warning_or_error, adc_return_type);
11427 : : }
11428 : :
11429 : 1054300 : if (type == error_mark_node)
11430 : : /* Leave it. */;
11431 : 1054093 : else if (functype == pattern)
11432 : 673986 : apply_deduced_return_type (current_function_decl, type);
11433 : 380107 : else if (!same_type_p (type, functype))
11434 : : {
11435 : 24 : if (LAMBDA_FUNCTION_P (current_function_decl))
11436 : 6 : error_at (loc, "inconsistent types %qT and %qT deduced for "
11437 : : "lambda return type", functype, type);
11438 : : else
11439 : 12 : error_at (loc, "inconsistent deduction for auto return type: "
11440 : : "%qT and then %qT", functype, type);
11441 : : }
11442 : : functype = type;
11443 : : }
11444 : :
11445 : 61537497 : result = DECL_RESULT (current_function_decl);
11446 : 61537497 : valtype = TREE_TYPE (result);
11447 : 61537497 : gcc_assert (valtype != NULL_TREE);
11448 : 61537497 : fn_returns_value_p = !VOID_TYPE_P (valtype);
11449 : :
11450 : : /* Check for a return statement with no return value in a function
11451 : : that's supposed to return a value. */
11452 : 61537497 : if (!retval && fn_returns_value_p)
11453 : : {
11454 : 35 : if (functype != error_mark_node)
11455 : 32 : permerror (input_location, "return-statement with no value, in "
11456 : : "function returning %qT", valtype);
11457 : : /* Remember that this function did return. */
11458 : 35 : current_function_returns_value = 1;
11459 : : /* But suppress NRV .*/
11460 : 35 : current_function_return_value = error_mark_node;
11461 : : /* And signal caller that TREE_NO_WARNING should be set on the
11462 : : RETURN_EXPR to avoid control reaches end of non-void function
11463 : : warnings in tree-cfg.cc. */
11464 : 35 : *no_warning = true;
11465 : : }
11466 : : /* Check for a return statement with a value in a function that
11467 : : isn't supposed to return a value. */
11468 : 61537462 : else if (retval && !fn_returns_value_p)
11469 : : {
11470 : 277798 : if (VOID_TYPE_P (TREE_TYPE (retval)))
11471 : : /* You can return a `void' value from a function of `void'
11472 : : type. In that case, we have to evaluate the expression for
11473 : : its side-effects. */
11474 : 277761 : finish_expr_stmt (retval);
11475 : 37 : else if (retval != error_mark_node)
11476 : 34 : permerror (loc, "return-statement with a value, in function "
11477 : : "returning %qT", valtype);
11478 : 277798 : current_function_returns_null = 1;
11479 : :
11480 : : /* There's really no value to return, after all. */
11481 : 277798 : return NULL_TREE;
11482 : : }
11483 : 61259664 : else if (!retval)
11484 : : /* Remember that this function can sometimes return without a
11485 : : value. */
11486 : 1550456 : current_function_returns_null = 1;
11487 : : else
11488 : : /* Remember that this function did return a value. */
11489 : 59709208 : current_function_returns_value = 1;
11490 : :
11491 : : /* Check for erroneous operands -- but after giving ourselves a
11492 : : chance to provide an error about returning a value from a void
11493 : : function. */
11494 : 61259699 : if (error_operand_p (retval))
11495 : : {
11496 : 967 : current_function_return_value = error_mark_node;
11497 : 967 : return error_mark_node;
11498 : : }
11499 : :
11500 : : /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11501 : 122517464 : if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11502 : 31071 : && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11503 : 3165 : && ! flag_check_new
11504 : 61261888 : && retval && null_ptr_cst_p (retval))
11505 : 24 : warning (0, "%<operator new%> must not return NULL unless it is "
11506 : : "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11507 : :
11508 : : /* Effective C++ rule 15. See also start_function. */
11509 : 61258732 : if (warn_ecpp
11510 : 42 : && DECL_NAME (current_function_decl) == assign_op_identifier
11511 : 61258762 : && !type_dependent_expression_p (retval))
11512 : : {
11513 : 27 : bool warn = true;
11514 : :
11515 : : /* The function return type must be a reference to the current
11516 : : class. */
11517 : 27 : if (TYPE_REF_P (valtype)
11518 : 45 : && same_type_ignoring_top_level_qualifiers_p
11519 : 18 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11520 : : {
11521 : : /* Returning '*this' is obviously OK. */
11522 : 18 : if (retval == current_class_ref)
11523 : : warn = false;
11524 : : /* If we are calling a function whose return type is the same of
11525 : : the current class reference, it is ok. */
11526 : 6 : else if (INDIRECT_REF_P (retval)
11527 : 6 : && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11528 : : warn = false;
11529 : : }
11530 : :
11531 : : if (warn)
11532 : 9 : warning_at (loc, OPT_Weffc__,
11533 : : "%<operator=%> should return a reference to %<*this%>");
11534 : : }
11535 : :
11536 : 61258732 : if (dependent_type_p (functype)
11537 : 61258732 : || type_dependent_expression_p (retval))
11538 : : {
11539 : 61750639 : dependent:
11540 : : /* We should not have changed the return value. */
11541 : 61750639 : gcc_assert (retval == saved_retval);
11542 : : /* We don't know if this is an lvalue or rvalue use, but
11543 : : either way we can mark it as read. */
11544 : 61750639 : mark_exp_read (retval);
11545 : 61750639 : return retval;
11546 : : }
11547 : :
11548 : : /* The fabled Named Return Value optimization, as per [class.copy]/15:
11549 : :
11550 : : [...] For a function with a class return type, if the expression
11551 : : in the return statement is the name of a local object, and the cv-
11552 : : unqualified type of the local object is the same as the function
11553 : : return type, an implementation is permitted to omit creating the tem-
11554 : : porary object to hold the function return value [...]
11555 : :
11556 : : So, if this is a value-returning function that always returns the same
11557 : : local variable, remember it.
11558 : :
11559 : : We choose the first suitable variable even if the function sometimes
11560 : : returns something else, but only if the variable is out of scope at the
11561 : : other return sites, or else we run the risk of clobbering the variable we
11562 : : chose if the other returned expression uses the chosen variable somehow.
11563 : :
11564 : : We don't currently do this if the first return is a non-variable, as it
11565 : : would be complicated to determine whether an NRV selected later was in
11566 : : scope at the point of the earlier return. We also don't currently support
11567 : : multiple variables with non-overlapping scopes (53637).
11568 : :
11569 : : See finish_function and finalize_nrv for the rest of this optimization. */
11570 : 47289978 : tree bare_retval = NULL_TREE;
11571 : 47289978 : if (retval)
11572 : : {
11573 : 45739502 : retval = maybe_undo_parenthesized_ref (retval);
11574 : 45739502 : bare_retval = tree_strip_any_location_wrapper (retval);
11575 : : }
11576 : :
11577 : 47289978 : bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11578 : 47289978 : if (fn_returns_value_p && flag_elide_constructors
11579 : 45739481 : && current_function_return_value != bare_retval)
11580 : : {
11581 : 45727083 : if (named_return_value_okay_p
11582 : 231575 : && current_function_return_value == NULL_TREE)
11583 : 190192 : current_function_return_value = bare_retval;
11584 : 45536891 : else if (current_function_return_value
11585 : 8634161 : && VAR_P (current_function_return_value)
11586 : 185 : && DECL_NAME (current_function_return_value)
11587 : 45537076 : && !decl_in_scope_p (current_function_return_value))
11588 : : {
11589 : : /* The earlier NRV is out of scope at this point, so it's safe to
11590 : 116 : leave it alone; the current return can't refer to it. */;
11591 : 116 : if (named_return_value_okay_p
11592 : 116 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11593 : : {
11594 : 8 : warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11595 : : bare_retval);
11596 : 8 : suppress_warning (current_function_decl, OPT_Wnrvo);
11597 : : }
11598 : : }
11599 : : else
11600 : : {
11601 : 45536775 : if ((named_return_value_okay_p
11602 : 45495402 : || (current_function_return_value
11603 : 8592672 : && current_function_return_value != error_mark_node))
11604 : 45536822 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11605 : : {
11606 : 41357 : warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11607 : : current_function_decl);
11608 : 41357 : suppress_warning (current_function_decl, OPT_Wnrvo);
11609 : : }
11610 : 45536775 : current_function_return_value = error_mark_node;
11611 : : }
11612 : : }
11613 : :
11614 : : /* We don't need to do any conversions when there's nothing being
11615 : : returned. */
11616 : 47289978 : if (!retval)
11617 : : return NULL_TREE;
11618 : :
11619 : 45739502 : if (!named_return_value_okay_p)
11620 : 45495529 : maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11621 : :
11622 : : /* Do any required conversions. */
11623 : 91479004 : if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11624 : : /* No conversions are required. */
11625 : : ;
11626 : : else
11627 : : {
11628 : 45739502 : int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11629 : :
11630 : : /* The functype's return type will have been set to void, if it
11631 : : was an incomplete type. Just treat this as 'return;' */
11632 : 45739502 : if (VOID_TYPE_P (functype))
11633 : 6 : return error_mark_node;
11634 : :
11635 : : /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11636 : : treated as an rvalue for the purposes of overload resolution to
11637 : : favor move constructors over copy constructors.
11638 : :
11639 : : Note that these conditions are similar to, but not as strict as,
11640 : : the conditions for the named return value optimization. */
11641 : 45739496 : bool converted = false;
11642 : 45739496 : tree moved;
11643 : : /* Until C++23, this was only interesting for class type, but in C++23,
11644 : : we should do the below when we're converting from/to a class/reference
11645 : : (a non-scalar type). */
11646 : 45739496 : if ((cxx_dialect < cxx23
11647 : 4862112 : ? CLASS_TYPE_P (functype)
11648 : 11872532 : : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11649 : 54053680 : && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11650 : : /* In C++20 and earlier we treat the return value as an rvalue
11651 : : that can bind to lvalue refs. In C++23, such an expression is just
11652 : : an xvalue (see reference_binding). */
11653 : : retval = moved;
11654 : :
11655 : : /* The call in a (lambda) thunk needs no conversions. */
11656 : 45739496 : if (TREE_CODE (retval) == CALL_EXPR
11657 : 45739496 : && call_from_lambda_thunk_p (retval))
11658 : : converted = true;
11659 : :
11660 : : /* Don't check copy-initialization for NRV in a coroutine ramp; we
11661 : : implement this case as NRV, but it's specified as directly
11662 : : initializing the return value from get_return_object(). */
11663 : 45739496 : if (DECL_RAMP_P (current_function_decl) && named_return_value_okay_p)
11664 : : converted = true;
11665 : :
11666 : : /* First convert the value to the function's return type, then
11667 : : to the type of return value's location to handle the
11668 : : case that functype is smaller than the valtype. */
11669 : 45739247 : if (!converted)
11670 : 45710471 : retval = convert_for_initialization
11671 : 45710471 : (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11672 : : tf_warning_or_error);
11673 : 45739496 : retval = convert (valtype, retval);
11674 : :
11675 : : /* If the conversion failed, treat this just like `return;'. */
11676 : 45739496 : if (retval == error_mark_node)
11677 : : {
11678 : : /* And suppress NRV. */
11679 : 194 : current_function_return_value = error_mark_node;
11680 : 194 : return retval;
11681 : : }
11682 : : /* We can't initialize a register from a AGGR_INIT_EXPR. */
11683 : 45739302 : else if (! cfun->returns_struct
11684 : 44057851 : && TREE_CODE (retval) == TARGET_EXPR
11685 : 50525674 : && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == AGGR_INIT_EXPR)
11686 : 834449 : retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11687 : 834449 : TARGET_EXPR_SLOT (retval));
11688 : 44904853 : else if (!processing_template_decl
11689 : 37397278 : && maybe_warn_about_returning_address_of_local (retval, loc)
11690 : 44905034 : && INDIRECT_TYPE_P (valtype))
11691 : 166 : *dangling = true;
11692 : : }
11693 : :
11694 : : /* A naive attempt to reduce the number of -Wdangling-reference false
11695 : : positives: if we know that this function can return a variable with
11696 : : static storage duration rather than one of its parameters, suppress
11697 : : the warning. */
11698 : 45739302 : if (warn_dangling_reference
11699 : 386465 : && TYPE_REF_P (functype)
11700 : 25656 : && bare_retval
11701 : 25656 : && VAR_P (bare_retval)
11702 : 50 : && TREE_STATIC (bare_retval))
11703 : 50 : suppress_warning (current_function_decl, OPT_Wdangling_reference);
11704 : :
11705 : 45739302 : if (processing_template_decl)
11706 : : return saved_retval;
11707 : :
11708 : : /* Actually copy the value returned into the appropriate location. */
11709 : 38231727 : if (retval && retval != result)
11710 : 38231727 : retval = cp_build_init_expr (result, retval);
11711 : :
11712 : 38231727 : if (current_function_return_value == bare_retval)
11713 : 202584 : INIT_EXPR_NRV_P (retval) = true;
11714 : :
11715 : 38231727 : if (tree set = maybe_set_retval_sentinel ())
11716 : 141189 : retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11717 : :
11718 : : return retval;
11719 : : }
11720 : :
11721 : :
11722 : : /* Returns nonzero if the pointer-type FROM can be converted to the
11723 : : pointer-type TO via a qualification conversion. If CONSTP is -1,
11724 : : then we return nonzero if the pointers are similar, and the
11725 : : cv-qualification signature of FROM is a proper subset of that of TO.
11726 : :
11727 : : If CONSTP is positive, then all outer pointers have been
11728 : : const-qualified. */
11729 : :
11730 : : static bool
11731 : 137741231 : comp_ptr_ttypes_real (tree to, tree from, int constp)
11732 : : {
11733 : 137741231 : bool to_more_cv_qualified = false;
11734 : 137741231 : bool is_opaque_pointer = false;
11735 : :
11736 : 568495 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11737 : : {
11738 : 138309726 : if (TREE_CODE (to) != TREE_CODE (from))
11739 : : return false;
11740 : :
11741 : 89181698 : if (TREE_CODE (from) == OFFSET_TYPE
11742 : 89181698 : && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11743 : : TYPE_OFFSET_BASETYPE (to)))
11744 : : return false;
11745 : :
11746 : : /* Const and volatile mean something different for function and
11747 : : array types, so the usual checks are not appropriate. We'll
11748 : : check the array type elements in further iterations. */
11749 : 89181698 : if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11750 : : {
11751 : 88947837 : if (!at_least_as_qualified_p (to, from))
11752 : : return false;
11753 : :
11754 : 78686613 : if (!at_least_as_qualified_p (from, to))
11755 : : {
11756 : 54819676 : if (constp == 0)
11757 : : return false;
11758 : : to_more_cv_qualified = true;
11759 : : }
11760 : :
11761 : 78686351 : if (constp > 0)
11762 : 78681685 : constp &= TYPE_READONLY (to);
11763 : : }
11764 : :
11765 : 78920212 : if (VECTOR_TYPE_P (to))
11766 : 7120 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11767 : :
11768 : : /* P0388R4 allows a conversion from int[N] to int[] but not the
11769 : : other way round. When both arrays have bounds but they do
11770 : : not match, then no conversion is possible. */
11771 : 78920212 : if (TREE_CODE (to) == ARRAY_TYPE
11772 : 78920212 : && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11773 : : return false;
11774 : :
11775 : 78919936 : if (!TYPE_PTR_P (to)
11776 : : && !TYPE_PTRDATAMEM_P (to)
11777 : : /* CWG 330 says we need to look through arrays. */
11778 : : && TREE_CODE (to) != ARRAY_TYPE)
11779 : 78351441 : return ((constp >= 0 || to_more_cv_qualified)
11780 : 78351441 : && (is_opaque_pointer
11781 : 78350875 : || same_type_ignoring_top_level_qualifiers_p (to, from)));
11782 : : }
11783 : : }
11784 : :
11785 : : /* When comparing, say, char ** to char const **, this function takes
11786 : : the 'char *' and 'char const *'. Do not pass non-pointer/reference
11787 : : types to this function. */
11788 : :
11789 : : int
11790 : 137740555 : comp_ptr_ttypes (tree to, tree from)
11791 : : {
11792 : 137740555 : return comp_ptr_ttypes_real (to, from, 1);
11793 : : }
11794 : :
11795 : : /* Returns true iff FNTYPE is a non-class type that involves
11796 : : error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11797 : : if a parameter type is ill-formed. */
11798 : :
11799 : : bool
11800 : 675549 : error_type_p (const_tree type)
11801 : : {
11802 : 718343 : tree t;
11803 : :
11804 : 718343 : switch (TREE_CODE (type))
11805 : : {
11806 : : case ERROR_MARK:
11807 : : return true;
11808 : :
11809 : 42705 : case POINTER_TYPE:
11810 : 42705 : case REFERENCE_TYPE:
11811 : 42705 : case OFFSET_TYPE:
11812 : 42705 : return error_type_p (TREE_TYPE (type));
11813 : :
11814 : 2697 : case FUNCTION_TYPE:
11815 : 2697 : case METHOD_TYPE:
11816 : 2697 : if (error_type_p (TREE_TYPE (type)))
11817 : : return true;
11818 : 7207 : for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11819 : 4516 : if (error_type_p (TREE_VALUE (t)))
11820 : : return true;
11821 : : return false;
11822 : :
11823 : 203677 : case RECORD_TYPE:
11824 : 203677 : if (TYPE_PTRMEMFUNC_P (type))
11825 : 89 : return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11826 : : return false;
11827 : :
11828 : : default:
11829 : : return false;
11830 : : }
11831 : : }
11832 : :
11833 : : /* Returns true if to and from are (possibly multi-level) pointers to the same
11834 : : type or inheritance-related types, regardless of cv-quals. */
11835 : :
11836 : : bool
11837 : 96883684 : ptr_reasonably_similar (const_tree to, const_tree from)
11838 : : {
11839 : 1020 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11840 : : {
11841 : : /* Any target type is similar enough to void. */
11842 : 96884704 : if (VOID_TYPE_P (to))
11843 : 1082 : return !error_type_p (from);
11844 : 96883622 : if (VOID_TYPE_P (from))
11845 : 665330 : return !error_type_p (to);
11846 : :
11847 : 96218292 : if (TREE_CODE (to) != TREE_CODE (from))
11848 : : return false;
11849 : :
11850 : 47604691 : if (TREE_CODE (from) == OFFSET_TYPE
11851 : 47604691 : && comptypes (TYPE_OFFSET_BASETYPE (to),
11852 : 3 : TYPE_OFFSET_BASETYPE (from),
11853 : : COMPARE_BASE | COMPARE_DERIVED))
11854 : 3 : continue;
11855 : :
11856 : 47604685 : if (VECTOR_TYPE_P (to)
11857 : 47604685 : && vector_types_convertible_p (to, from, false))
11858 : : return true;
11859 : :
11860 : 47604601 : if (TREE_CODE (to) == INTEGER_TYPE
11861 : 47604601 : && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11862 : : return true;
11863 : :
11864 : 47480070 : if (TREE_CODE (to) == FUNCTION_TYPE)
11865 : 962 : return !error_type_p (to) && !error_type_p (from);
11866 : :
11867 : 47479108 : if (!TYPE_PTR_P (to))
11868 : : {
11869 : : /* When either type is incomplete avoid DERIVED_FROM_P,
11870 : : which may call complete_type (c++/57942). */
11871 : 47478091 : bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11872 : : return comptypes
11873 : 47478091 : (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11874 : 47478091 : b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11875 : : }
11876 : 1020 : }
11877 : : }
11878 : :
11879 : : /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11880 : : pointer-to-member types) are the same, ignoring cv-qualification at
11881 : : all levels. CB says how we should behave when comparing array bounds. */
11882 : :
11883 : : bool
11884 : 824435 : comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11885 : : {
11886 : 824435 : bool is_opaque_pointer = false;
11887 : :
11888 : 824619 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11889 : : {
11890 : 1649054 : if (TREE_CODE (to) != TREE_CODE (from))
11891 : : return false;
11892 : :
11893 : 1295375 : if (TREE_CODE (from) == OFFSET_TYPE
11894 : 1295335 : && same_type_p (TYPE_OFFSET_BASETYPE (from),
11895 : : TYPE_OFFSET_BASETYPE (to)))
11896 : 40 : continue;
11897 : :
11898 : 1295295 : if (VECTOR_TYPE_P (to))
11899 : 8669 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11900 : :
11901 : 1295295 : if (TREE_CODE (to) == ARRAY_TYPE
11902 : : /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11903 : : we must fail. */
11904 : 1295295 : && !comp_array_types (to, from, cb, /*strict=*/false))
11905 : : return false;
11906 : :
11907 : : /* CWG 330 says we need to look through arrays. */
11908 : 1293994 : if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11909 : 469415 : return (is_opaque_pointer
11910 : 469415 : || same_type_ignoring_top_level_qualifiers_p (to, from));
11911 : : }
11912 : : }
11913 : :
11914 : : /* Returns the type qualifiers for this type, including the qualifiers on the
11915 : : elements for an array type. */
11916 : :
11917 : : int
11918 : 50865959131 : cp_type_quals (const_tree type)
11919 : : {
11920 : 50865959131 : int quals;
11921 : : /* This CONST_CAST is okay because strip_array_types returns its
11922 : : argument unmodified and we assign it to a const_tree. */
11923 : 50865959131 : type = strip_array_types (CONST_CAST_TREE (type));
11924 : 50865959131 : if (type == error_mark_node
11925 : : /* Quals on a FUNCTION_TYPE are memfn quals. */
11926 : 50833877900 : || TREE_CODE (type) == FUNCTION_TYPE)
11927 : : return TYPE_UNQUALIFIED;
11928 : 50785905046 : quals = TYPE_QUALS (type);
11929 : : /* METHOD and REFERENCE_TYPEs should never have quals. */
11930 : 50785905046 : gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11931 : : && !TYPE_REF_P (type))
11932 : : || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11933 : : == TYPE_UNQUALIFIED));
11934 : : return quals;
11935 : : }
11936 : :
11937 : : /* Returns the function-ref-qualifier for TYPE */
11938 : :
11939 : : cp_ref_qualifier
11940 : 2837802938 : type_memfn_rqual (const_tree type)
11941 : : {
11942 : 2837802938 : gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11943 : :
11944 : 2837802938 : if (!FUNCTION_REF_QUALIFIED (type))
11945 : : return REF_QUAL_NONE;
11946 : 32488570 : else if (FUNCTION_RVALUE_QUALIFIED (type))
11947 : : return REF_QUAL_RVALUE;
11948 : : else
11949 : 16293389 : return REF_QUAL_LVALUE;
11950 : : }
11951 : :
11952 : : /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11953 : : METHOD_TYPE. */
11954 : :
11955 : : int
11956 : 1019424965 : type_memfn_quals (const_tree type)
11957 : : {
11958 : 1019424965 : if (TREE_CODE (type) == FUNCTION_TYPE)
11959 : 82691918 : return TYPE_QUALS (type);
11960 : 936733047 : else if (TREE_CODE (type) == METHOD_TYPE)
11961 : 936733047 : return cp_type_quals (class_of_this_parm (type));
11962 : : else
11963 : 0 : gcc_unreachable ();
11964 : : }
11965 : :
11966 : : /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11967 : : MEMFN_QUALS and its ref-qualifier to RQUAL. */
11968 : :
11969 : : tree
11970 : 58095437 : apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11971 : : {
11972 : : /* Could handle METHOD_TYPE here if necessary. */
11973 : 58095437 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11974 : 58095437 : if (TYPE_QUALS (type) == memfn_quals
11975 : 58095437 : && type_memfn_rqual (type) == rqual)
11976 : : return type;
11977 : :
11978 : : /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11979 : : complex. */
11980 : 701417 : tree result = build_qualified_type (type, memfn_quals);
11981 : 701417 : return build_ref_qualified_type (result, rqual);
11982 : : }
11983 : :
11984 : : /* Returns nonzero if TYPE is const or volatile. */
11985 : :
11986 : : bool
11987 : 1051280069 : cv_qualified_p (const_tree type)
11988 : : {
11989 : 1051280069 : int quals = cp_type_quals (type);
11990 : 1051280069 : return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11991 : : }
11992 : :
11993 : : /* Returns nonzero if the TYPE contains a mutable member. */
11994 : :
11995 : : bool
11996 : 838034407 : cp_has_mutable_p (const_tree type)
11997 : : {
11998 : : /* This CONST_CAST is okay because strip_array_types returns its
11999 : : argument unmodified and we assign it to a const_tree. */
12000 : 838034407 : type = strip_array_types (CONST_CAST_TREE(type));
12001 : :
12002 : 838034407 : return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
12003 : : }
12004 : :
12005 : : /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
12006 : : TYPE_QUALS. For a VAR_DECL, this may be an optimistic
12007 : : approximation. In particular, consider:
12008 : :
12009 : : int f();
12010 : : struct S { int i; };
12011 : : const S s = { f(); }
12012 : :
12013 : : Here, we will make "s" as TREE_READONLY (because it is declared
12014 : : "const") -- only to reverse ourselves upon seeing that the
12015 : : initializer is non-constant. */
12016 : :
12017 : : void
12018 : 895011950 : cp_apply_type_quals_to_decl (int type_quals, tree decl)
12019 : : {
12020 : 895011950 : tree type = TREE_TYPE (decl);
12021 : :
12022 : 895011950 : if (type == error_mark_node)
12023 : : return;
12024 : :
12025 : 895011288 : if (TREE_CODE (decl) == TYPE_DECL)
12026 : : return;
12027 : :
12028 : 798015078 : gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
12029 : : && type_quals != TYPE_UNQUALIFIED));
12030 : :
12031 : : /* Avoid setting TREE_READONLY incorrectly. */
12032 : : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
12033 : : constructor can produce constant init, so rely on cp_finish_decl to
12034 : : clear TREE_READONLY if the variable has non-constant init. */
12035 : :
12036 : : /* If the type has (or might have) a mutable component, that component
12037 : : might be modified. */
12038 : 798015078 : if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
12039 : 71717466 : type_quals &= ~TYPE_QUAL_CONST;
12040 : :
12041 : 798015078 : c_apply_type_quals_to_decl (type_quals, decl);
12042 : : }
12043 : :
12044 : : /* Subroutine of casts_away_constness. Make T1 and T2 point at
12045 : : exemplar types such that casting T1 to T2 is casting away constness
12046 : : if and only if there is no implicit conversion from T1 to T2. */
12047 : :
12048 : : static void
12049 : 1192027 : casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
12050 : : {
12051 : 1192027 : int quals1;
12052 : 1192027 : int quals2;
12053 : :
12054 : : /* [expr.const.cast]
12055 : :
12056 : : For multi-level pointer to members and multi-level mixed pointers
12057 : : and pointers to members (conv.qual), the "member" aspect of a
12058 : : pointer to member level is ignored when determining if a const
12059 : : cv-qualifier has been cast away. */
12060 : : /* [expr.const.cast]
12061 : :
12062 : : For two pointer types:
12063 : :
12064 : : X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
12065 : : X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
12066 : : K is min(N,M)
12067 : :
12068 : : casting from X1 to X2 casts away constness if, for a non-pointer
12069 : : type T there does not exist an implicit conversion (clause
12070 : : _conv_) from:
12071 : :
12072 : : Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
12073 : :
12074 : : to
12075 : :
12076 : : Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
12077 : 1192027 : if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
12078 : 596364 : || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
12079 : : {
12080 : 595841 : *t1 = cp_build_qualified_type (void_type_node,
12081 : : cp_type_quals (*t1));
12082 : 595841 : *t2 = cp_build_qualified_type (void_type_node,
12083 : : cp_type_quals (*t2));
12084 : 595841 : return;
12085 : : }
12086 : :
12087 : 596186 : quals1 = cp_type_quals (*t1);
12088 : 596186 : quals2 = cp_type_quals (*t2);
12089 : :
12090 : 596186 : if (TYPE_PTRDATAMEM_P (*t1))
12091 : 0 : *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
12092 : : else
12093 : 596186 : *t1 = TREE_TYPE (*t1);
12094 : 596186 : if (TYPE_PTRDATAMEM_P (*t2))
12095 : 0 : *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
12096 : : else
12097 : 596186 : *t2 = TREE_TYPE (*t2);
12098 : :
12099 : 596186 : casts_away_constness_r (t1, t2, complain);
12100 : 596186 : *t1 = build_pointer_type (*t1);
12101 : 596186 : *t2 = build_pointer_type (*t2);
12102 : 596186 : *t1 = cp_build_qualified_type (*t1, quals1);
12103 : 596186 : *t2 = cp_build_qualified_type (*t2, quals2);
12104 : : }
12105 : :
12106 : : /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
12107 : : constness.
12108 : :
12109 : : ??? This function returns non-zero if casting away qualifiers not
12110 : : just const. We would like to return to the caller exactly which
12111 : : qualifiers are casted away to give more accurate diagnostics.
12112 : : */
12113 : :
12114 : : static bool
12115 : 595913 : casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
12116 : : {
12117 : 595913 : if (TYPE_REF_P (t2))
12118 : : {
12119 : : /* [expr.const.cast]
12120 : :
12121 : : Casting from an lvalue of type T1 to an lvalue of type T2
12122 : : using a reference cast casts away constness if a cast from an
12123 : : rvalue of type "pointer to T1" to the type "pointer to T2"
12124 : : casts away constness. */
12125 : 0 : t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
12126 : 0 : return casts_away_constness (build_pointer_type (t1),
12127 : 0 : build_pointer_type (TREE_TYPE (t2)),
12128 : 0 : complain);
12129 : : }
12130 : :
12131 : 595913 : if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
12132 : : /* [expr.const.cast]
12133 : :
12134 : : Casting from an rvalue of type "pointer to data member of X
12135 : : of type T1" to the type "pointer to data member of Y of type
12136 : : T2" casts away constness if a cast from an rvalue of type
12137 : : "pointer to T1" to the type "pointer to T2" casts away
12138 : : constness. */
12139 : 48 : return casts_away_constness
12140 : 48 : (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
12141 : 48 : build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
12142 : 48 : complain);
12143 : :
12144 : : /* Casting away constness is only something that makes sense for
12145 : : pointer or reference types. */
12146 : 595865 : if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
12147 : : return false;
12148 : :
12149 : : /* Top-level qualifiers don't matter. */
12150 : 595841 : t1 = TYPE_MAIN_VARIANT (t1);
12151 : 595841 : t2 = TYPE_MAIN_VARIANT (t2);
12152 : 595841 : casts_away_constness_r (&t1, &t2, complain);
12153 : 595841 : if (!can_convert (t2, t1, complain))
12154 : : return true;
12155 : :
12156 : : return false;
12157 : : }
12158 : :
12159 : : /* If T is a REFERENCE_TYPE return the type to which T refers.
12160 : : Otherwise, return T itself. */
12161 : :
12162 : : tree
12163 : 3190329534 : non_reference (tree t)
12164 : : {
12165 : 3190329534 : if (t && TYPE_REF_P (t))
12166 : 303680831 : t = TREE_TYPE (t);
12167 : 3190329534 : return t;
12168 : : }
12169 : :
12170 : :
12171 : : /* Return nonzero if REF is an lvalue valid for this language;
12172 : : otherwise, print an error message and return zero. USE says
12173 : : how the lvalue is being used and so selects the error message. */
12174 : :
12175 : : int
12176 : 38671012 : lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
12177 : : {
12178 : 38671012 : cp_lvalue_kind kind = lvalue_kind (ref);
12179 : :
12180 : 38671012 : if (kind == clk_none)
12181 : : {
12182 : 128 : if (complain & tf_error)
12183 : 115 : lvalue_error (cp_expr_loc_or_input_loc (ref), use);
12184 : 128 : return 0;
12185 : : }
12186 : 38670884 : else if (kind & (clk_rvalueref|clk_class))
12187 : : {
12188 : 92 : if (!(complain & tf_error))
12189 : : return 0;
12190 : : /* Make this a permerror because we used to accept it. */
12191 : 18 : permerror (cp_expr_loc_or_input_loc (ref),
12192 : : "using rvalue as lvalue");
12193 : : }
12194 : : return 1;
12195 : : }
12196 : :
12197 : : /* Return true if a user-defined literal operator is a raw operator. */
12198 : :
12199 : : bool
12200 : 120910 : check_raw_literal_operator (const_tree decl)
12201 : : {
12202 : 120910 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12203 : 120910 : tree argtype;
12204 : 120910 : int arity;
12205 : 120910 : bool maybe_raw_p = false;
12206 : :
12207 : : /* Count the number and type of arguments and check for ellipsis. */
12208 : 120910 : for (argtype = argtypes, arity = 0;
12209 : 181382 : argtype && argtype != void_list_node;
12210 : 60472 : ++arity, argtype = TREE_CHAIN (argtype))
12211 : : {
12212 : 60472 : tree t = TREE_VALUE (argtype);
12213 : :
12214 : 60472 : if (same_type_p (t, const_string_type_node))
12215 : 9 : maybe_raw_p = true;
12216 : : }
12217 : 120910 : if (!argtype)
12218 : : return false; /* Found ellipsis. */
12219 : :
12220 : 120910 : if (!maybe_raw_p || arity != 1)
12221 : 120904 : return false;
12222 : :
12223 : : return true;
12224 : : }
12225 : :
12226 : :
12227 : : /* Return true if a user-defined literal operator has one of the allowed
12228 : : argument types. */
12229 : :
12230 : : bool
12231 : 288026 : check_literal_operator_args (const_tree decl,
12232 : : bool *long_long_unsigned_p, bool *long_double_p)
12233 : : {
12234 : 288026 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12235 : :
12236 : 288026 : *long_long_unsigned_p = false;
12237 : 288026 : *long_double_p = false;
12238 : 288026 : if (processing_template_decl || processing_specialization)
12239 : 60579 : return argtypes == void_list_node;
12240 : : else
12241 : : {
12242 : : tree argtype;
12243 : : int arity;
12244 : : int max_arity = 2;
12245 : :
12246 : : /* Count the number and type of arguments and check for ellipsis. */
12247 : 227353 : for (argtype = argtypes, arity = 0;
12248 : 454800 : argtype && argtype != void_list_node;
12249 : 227353 : argtype = TREE_CHAIN (argtype))
12250 : : {
12251 : 227453 : tree t = TREE_VALUE (argtype);
12252 : 227453 : ++arity;
12253 : :
12254 : 227453 : if (TYPE_PTR_P (t))
12255 : : {
12256 : 102387 : bool maybe_raw_p = false;
12257 : 102387 : t = TREE_TYPE (t);
12258 : 102387 : if (cp_type_quals (t) != TYPE_QUAL_CONST)
12259 : : return false;
12260 : 102384 : t = TYPE_MAIN_VARIANT (t);
12261 : 102384 : if ((maybe_raw_p = same_type_p (t, char_type_node))
12262 : 78756 : || same_type_p (t, wchar_type_node)
12263 : 55280 : || same_type_p (t, char8_type_node)
12264 : 46964 : || same_type_p (t, char16_type_node)
12265 : 125866 : || same_type_p (t, char32_type_node))
12266 : : {
12267 : 102381 : argtype = TREE_CHAIN (argtype);
12268 : 102381 : if (!argtype)
12269 : : return false;
12270 : 102381 : t = TREE_VALUE (argtype);
12271 : 102381 : if (maybe_raw_p && argtype == void_list_node)
12272 : : return true;
12273 : 102308 : else if (same_type_p (t, size_type_node))
12274 : : {
12275 : 102302 : ++arity;
12276 : 102302 : continue;
12277 : : }
12278 : : else
12279 : : return false;
12280 : : }
12281 : : }
12282 : 125066 : else if (same_type_p (t, long_long_unsigned_type_node))
12283 : : {
12284 : 34846 : max_arity = 1;
12285 : 34846 : *long_long_unsigned_p = true;
12286 : : }
12287 : 90220 : else if (same_type_p (t, long_double_type_node))
12288 : : {
12289 : 90120 : max_arity = 1;
12290 : 90120 : *long_double_p = true;
12291 : : }
12292 : 100 : else if (same_type_p (t, char_type_node))
12293 : : max_arity = 1;
12294 : 56 : else if (same_type_p (t, wchar_type_node))
12295 : : max_arity = 1;
12296 : 45 : else if (same_type_p (t, char8_type_node))
12297 : : max_arity = 1;
12298 : 40 : else if (same_type_p (t, char16_type_node))
12299 : : max_arity = 1;
12300 : 29 : else if (same_type_p (t, char32_type_node))
12301 : : max_arity = 1;
12302 : : else
12303 : : return false;
12304 : : }
12305 : 227347 : if (!argtype)
12306 : : return false; /* Found ellipsis. */
12307 : :
12308 : 227344 : if (arity != max_arity)
12309 : : return false;
12310 : :
12311 : : return true;
12312 : : }
12313 : : }
12314 : :
12315 : : /* Always returns false since unlike C90, C++ has no concept of implicit
12316 : : function declarations. */
12317 : :
12318 : : bool
12319 : 359 : c_decl_implicit (const_tree)
12320 : : {
12321 : 359 : return false;
12322 : : }
|