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 : 119172657 : require_complete_type (tree value,
75 : : tsubst_flags_t complain /* = tf_warning_or_error */)
76 : : {
77 : 119172657 : tree type;
78 : :
79 : 119172657 : if (processing_template_decl || value == error_mark_node)
80 : : return value;
81 : :
82 : 110988412 : if (TREE_CODE (value) == OVERLOAD)
83 : 0 : type = unknown_type_node;
84 : : else
85 : 110988412 : type = TREE_TYPE (value);
86 : :
87 : 110988412 : if (type == error_mark_node)
88 : : return error_mark_node;
89 : :
90 : : /* First, detect a valid value with a complete type. */
91 : 110988400 : if (COMPLETE_TYPE_P (type))
92 : : return value;
93 : :
94 : 113764 : if (complete_type_or_maybe_complain (type, value, complain))
95 : : return value;
96 : : else
97 : 88 : return error_mark_node;
98 : : }
99 : :
100 : : /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
101 : : a template instantiation, do the instantiation. Returns TYPE,
102 : : whether or not it could be completed, unless something goes
103 : : horribly wrong, in which case the error_mark_node is returned. */
104 : :
105 : : tree
106 : 12487975755 : complete_type (tree type)
107 : : {
108 : 12487975755 : 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 : 12487975755 : if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 : : ;
115 : 4139528419 : else if (TREE_CODE (type) == ARRAY_TYPE)
116 : : {
117 : 635369 : tree t = complete_type (TREE_TYPE (type));
118 : 635369 : unsigned int needs_constructing, has_nontrivial_dtor;
119 : 635369 : if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 : 630452 : layout_type (type);
121 : 635369 : needs_constructing
122 : 635369 : = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 : 635369 : has_nontrivial_dtor
124 : 635369 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 : 2102346 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 : : {
127 : 1466977 : TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 : 1466977 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 : : }
130 : : }
131 : 4138893050 : else if (CLASS_TYPE_P (type))
132 : : {
133 : 4058000164 : if (modules_p ())
134 : : /* TYPE could be a class member we've not loaded the definition of. */
135 : 13085108 : lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
136 : :
137 : 4058000164 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 : 788728940 : 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 : 1333876655 : complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 : : {
151 : 1333876655 : type = complete_type (type);
152 : 1333876652 : if (type == error_mark_node)
153 : : /* We already issued an error. */
154 : : return NULL_TREE;
155 : 1333876499 : else if (!COMPLETE_TYPE_P (type))
156 : : {
157 : 3803 : if (complain & tf_error)
158 : 472 : cxx_incomplete_type_diagnostic (value, type, diagnostics::kind::error);
159 : 3803 : note_failed_type_completion (type, complain);
160 : 3803 : return NULL_TREE;
161 : : }
162 : : else
163 : : return type;
164 : : }
165 : :
166 : : tree
167 : 143390040 : complete_type_or_else (tree type, tree value)
168 : : {
169 : 143390040 : 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 : 4199245 : commonparms (tree p1, tree p2)
182 : : {
183 : 4199245 : tree oldargs = p1, newargs, n;
184 : 4199245 : int i, len;
185 : 4199245 : int any_change = 0;
186 : :
187 : 4199245 : len = list_length (p1);
188 : 4199245 : newargs = tree_last (p1);
189 : :
190 : 4199245 : if (newargs == void_list_node)
191 : : i = 1;
192 : : else
193 : : {
194 : 38985 : i = 0;
195 : 38985 : newargs = 0;
196 : : }
197 : :
198 : 13992380 : for (; i < len; i++)
199 : 9793135 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200 : :
201 : : n = newargs;
202 : :
203 : 18152640 : for (i = 0; p1;
204 : 13953395 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 : : {
206 : 13953596 : if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 : : {
208 : 168 : TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 : 168 : any_change = 1;
210 : : }
211 : 13953227 : else if (! TREE_PURPOSE (p1))
212 : : {
213 : 13953194 : if (TREE_PURPOSE (p2))
214 : : {
215 : 420773 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 : 420773 : 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 : 13953395 : if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 : : {
227 : 3844807 : any_change = 1;
228 : 3844807 : TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 : : }
230 : : else
231 : 10108588 : TREE_VALUE (n) = TREE_VALUE (p1);
232 : : }
233 : 4199245 : if (! any_change)
234 : 1476763 : 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 : 33004178 : original_type (tree t)
243 : : {
244 : 33004178 : int quals = cp_type_quals (t);
245 : 33004178 : while (t != error_mark_node
246 : 34135492 : && TYPE_NAME (t) != NULL_TREE)
247 : : {
248 : 11497438 : tree x = TYPE_NAME (t);
249 : 11497438 : if (TREE_CODE (x) != TYPE_DECL)
250 : : break;
251 : 11497438 : x = DECL_ORIGINAL_TYPE (x);
252 : 11497438 : if (x == NULL_TREE)
253 : : break;
254 : : t = x;
255 : : }
256 : 33004178 : 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 : 11108245 : cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
280 : : {
281 : 11108245 : tree mv1 = TYPE_MAIN_VARIANT (t1);
282 : 11108245 : tree mv2 = TYPE_MAIN_VARIANT (t2);
283 : 11108245 : int extended1 = 0;
284 : 11108245 : int extended2 = 0;
285 : :
286 : 11108245 : if (mv1 == mv2)
287 : : return 0;
288 : :
289 : 88854640 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
290 : : {
291 : 77747810 : if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 : 4936374 : extended1 = i + 1;
293 : 77747810 : if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 : 4216676 : extended2 = i + 1;
295 : : }
296 : 11106830 : if (mv1 == bfloat16_type_node)
297 : 1141294 : extended1 = true;
298 : 11106830 : if (mv2 == bfloat16_type_node)
299 : 925064 : extended2 = true;
300 : 11106830 : if (extended2 && !extended1)
301 : : {
302 : 4938560 : int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 : 4938560 : return ret == 3 ? 3 : -ret;
304 : : }
305 : :
306 : 6168270 : const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 : 6168270 : const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 : 6168270 : 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 : 49346160 : int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
315 : 12336540 : ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
316 : 49346160 : int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
317 : 12336540 : ? 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 : 6168270 : if ((p1 > p2 && fmt1->emax >= fmt2->emax)
322 : 4749998 : || (p1 == p2 && fmt1->emax > fmt2->emax))
323 : : return 2;
324 : 4749998 : if ((p1 < p2 && fmt1->emax <= fmt2->emax)
325 : 1568255 : || (p1 == p2 && fmt1->emax < fmt2->emax))
326 : : return -2;
327 : 1568255 : if ((p1 > p2 && fmt1->emax < fmt2->emax)
328 : 1559600 : || (p1 < p2 && fmt1->emax > fmt2->emax))
329 : : return 3;
330 : 1550945 : 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 : 1550945 : 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 : 6203764 : for (p = &float_type_node; p <= &long_double_type_node; ++p)
375 : : {
376 : 4652823 : const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
377 : 4652823 : gcc_assert (fmt3->b == 2);
378 : 37222584 : int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
379 : 9305646 : ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
380 : 4652823 : if (p1 == p3 && fmt1->emax == fmt3->emax)
381 : 1356115 : ++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 : 1550941 : 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 : 96546276 : cp_common_type (tree t1, tree t2)
413 : : {
414 : 96546276 : enum tree_code code1 = TREE_CODE (t1);
415 : 96546276 : enum tree_code code2 = TREE_CODE (t2);
416 : 96546276 : tree attributes;
417 : 96546276 : 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 : 96546276 : attributes = (*targetm.merge_type_attributes) (t1, t2);
424 : :
425 : 96546276 : if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
426 : : {
427 : 1454719 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
428 : 1454719 : return build_type_attribute_variant (t1, attributes);
429 : : else
430 : : return NULL_TREE;
431 : : }
432 : :
433 : : /* FIXME: Attributes. */
434 : 95091557 : gcc_assert (ARITHMETIC_TYPE_P (t1)
435 : : || VECTOR_TYPE_P (t1)
436 : : || UNSCOPED_ENUM_P (t1));
437 : 95091557 : 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 : 95091557 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
445 : : {
446 : 229597 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
447 : 229597 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
448 : 229597 : tree subtype
449 : 229597 : = type_after_usual_arithmetic_conversions (subtype1, subtype2);
450 : :
451 : 229597 : if (subtype == error_mark_node)
452 : : return subtype;
453 : 229597 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
454 : 196183 : 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 : 94861960 : 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 : 94802627 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
474 : 1116282 : return build_type_attribute_variant (t1, attributes);
475 : 93686345 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
476 : 1042273 : return build_type_attribute_variant (t2, attributes);
477 : :
478 : 92644072 : if (code1 == REAL_TYPE
479 : 92644072 : && (extended_float_type_p (t1) || extended_float_type_p (t2)))
480 : : {
481 : 202802 : tree mv1 = TYPE_MAIN_VARIANT (t1);
482 : 202802 : tree mv2 = TYPE_MAIN_VARIANT (t2);
483 : 202802 : if (mv1 == mv2)
484 : 199026 : return build_type_attribute_variant (t1, attributes);
485 : :
486 : 3776 : int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
487 : 3776 : if (cmpret == 3)
488 : 0 : return error_mark_node;
489 : 3776 : else if (cmpret >= 0)
490 : 3514 : 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 : 92441270 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
497 : 13441562 : return build_type_attribute_variant (t1, attributes);
498 : 78999708 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
499 : 1504971 : return build_type_attribute_variant (t2, attributes);
500 : :
501 : : /* The types are the same; no need to do anything fancy. */
502 : 77494737 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
503 : 71345840 : return build_type_attribute_variant (t1, attributes);
504 : :
505 : 6148897 : if (code1 != REAL_TYPE)
506 : : {
507 : : /* If one is unsigned long long, then convert the other to unsigned
508 : : long long. */
509 : 6148894 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
510 : 6148894 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
511 : 123373 : return build_type_attribute_variant (long_long_unsigned_type_node,
512 : 123373 : 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 : 6025521 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
524 : 6025521 : || 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 : 6024836 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
534 : 6024836 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
535 : 750241 : return build_type_attribute_variant (long_unsigned_type_node,
536 : 750241 : attributes);
537 : 5274595 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
538 : 5274595 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
539 : : {
540 : 72189 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
541 : 72189 : ? long_unsigned_type_node : long_integer_type_node);
542 : 36469 : 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 : 10476220 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
550 : : {
551 : 5238126 : if (int_n_enabled_p [i]
552 : 5238126 : && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
553 : 5157293 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
554 : 5157289 : || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
555 : 5157289 : || 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 : 5238094 : if (TYPE_UNSIGNED (t1))
566 : 4195965 : return build_type_attribute_variant (t1, attributes);
567 : : else
568 : 1042129 : 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 : 911385 : type_after_usual_arithmetic_conversions (tree t1, tree t2)
600 : : {
601 : 911385 : gcc_assert (ARITHMETIC_TYPE_P (t1)
602 : : || VECTOR_TYPE_P (t1)
603 : : || UNSCOPED_ENUM_P (t1));
604 : 911385 : 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 : 911385 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
610 : 681238 : && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
611 : : {
612 : 680888 : t1 = type_promotes_to (t1);
613 : 680888 : t2 = type_promotes_to (t2);
614 : : }
615 : :
616 : 911385 : 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 : 4185195 : 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 : 4185195 : tree pointee1;
660 : 4185195 : tree pointee2;
661 : 4185195 : tree result_type;
662 : 4185195 : tree attributes;
663 : :
664 : : /* Determine the types pointed to by T1 and T2. */
665 : 4185195 : if (TYPE_PTR_P (t1))
666 : : {
667 : 4184699 : pointee1 = TREE_TYPE (t1);
668 : 4184699 : 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 : 4185195 : 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 : 4185104 : const int q1 = cp_type_quals (pointee1);
700 : 4185104 : const int q2 = cp_type_quals (pointee2);
701 : 4185104 : const int quals = q1 | q2;
702 : 4185104 : result_type = cp_build_qualified_type (result_type,
703 : 4185104 : (quals | (*add_const
704 : 4185104 : ? 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 : 4185104 : if (quals != q1 || quals != q2)
710 : 557758 : *add_const = true;
711 : : /* If the original types were pointers to members, so is the
712 : : result. */
713 : 4185104 : 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 : 4184618 : result_type = build_pointer_type (result_type);
729 : :
730 : : /* Merge the attributes. */
731 : 4185104 : attributes = (*targetm.merge_type_attributes) (t1, t2);
732 : 4185104 : 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 : 4388845 : 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 : 4388845 : tree class1;
750 : 4388845 : 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 : 4388845 : if (null_ptr_cst_p (arg1))
757 : : return t2;
758 : 4388666 : 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 : 4366462 : 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 : 4366462 : if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
776 : : {
777 : 181129 : tree attributes;
778 : 181129 : tree result_type;
779 : :
780 : 181129 : 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 : 181128 : result_type
810 : 181128 : = cp_build_qualified_type (void_type_node,
811 : 181128 : (cp_type_quals (TREE_TYPE (t1))
812 : 181128 : | cp_type_quals (TREE_TYPE (t2))));
813 : 181128 : result_type = build_pointer_type (result_type);
814 : : /* Merge the attributes. */
815 : 181128 : attributes = (*targetm.merge_type_attributes) (t1, t2);
816 : 181128 : return build_type_attribute_variant (result_type, attributes);
817 : : }
818 : :
819 : 4185333 : 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 : 4185333 : if (fnptr_conv_p (t1, t2))
830 : : return t1;
831 : 4185265 : 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 : 4184684 : if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
837 : 4184684 : && CLASS_TYPE_P (TREE_TYPE (t1))
838 : 729612 : && CLASS_TYPE_P (TREE_TYPE (t2))
839 : 4914810 : && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
840 : 729609 : TREE_TYPE (t2)))
841 : : {
842 : 31460 : class1 = TREE_TYPE (t1);
843 : 31460 : class2 = TREE_TYPE (t2);
844 : :
845 : 31460 : if (DERIVED_FROM_P (class1, class2))
846 : 3260 : t2 = (build_pointer_type
847 : 1630 : (cp_build_qualified_type (class1, cp_type_quals (class2))));
848 : 29830 : else if (DERIVED_FROM_P (class2, class1))
849 : 59624 : t1 = (build_pointer_type
850 : 29812 : (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 : 4153344 : else if (TYPE_PTRMEM_P (t1)
862 : 4153861 : && !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 : 4185162 : bool add_const = false;
900 : 4185162 : return composite_pointer_type_r (location, t1, t2, &add_const, operation,
901 : 4185162 : 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 : 29887518 : merge_types (tree t1, tree t2)
913 : : {
914 : 29887518 : enum tree_code code1;
915 : 29887518 : enum tree_code code2;
916 : 29887518 : tree attributes;
917 : :
918 : : /* Save time if the two types are the same. */
919 : 29887518 : if (t1 == t2)
920 : : return t1;
921 : 16502089 : if (original_type (t1) == original_type (t2))
922 : : return t1;
923 : :
924 : : /* If one type is nonsense, use the other. */
925 : 16428411 : if (t1 == error_mark_node)
926 : : return t2;
927 : 16428411 : if (t2 == error_mark_node)
928 : : return t1;
929 : :
930 : : /* Handle merging an auto redeclaration with a previous deduced
931 : : return type. */
932 : 16428411 : if (is_auto (t1))
933 : : return t2;
934 : :
935 : : /* Merge the attributes. */
936 : 16418605 : attributes = (*targetm.merge_type_attributes) (t1, t2);
937 : :
938 : 16418605 : if (TYPE_PTRMEMFUNC_P (t1))
939 : 15 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
940 : 16418605 : if (TYPE_PTRMEMFUNC_P (t2))
941 : 15 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
942 : :
943 : 16418605 : code1 = TREE_CODE (t1);
944 : 16418605 : code2 = TREE_CODE (t2);
945 : 16418605 : 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 : 16418605 : switch (code1)
961 : : {
962 : 2766960 : case POINTER_TYPE:
963 : 2766960 : case REFERENCE_TYPE:
964 : : /* For two pointers, do this recursively on the target type. */
965 : 2766960 : {
966 : 2766960 : tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
967 : 2766960 : int quals = cp_type_quals (t1);
968 : :
969 : 2766960 : if (code1 == POINTER_TYPE)
970 : : {
971 : 736011 : t1 = build_pointer_type (target);
972 : 736011 : if (TREE_CODE (target) == METHOD_TYPE)
973 : 15 : t1 = build_ptrmemfunc_type (t1);
974 : : }
975 : : else
976 : 2030949 : t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
977 : 2766960 : t1 = build_type_attribute_variant (t1, attributes);
978 : 2766960 : t1 = cp_build_qualified_type (t1, quals);
979 : :
980 : 2766960 : 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 : 10865 : case ARRAY_TYPE:
997 : 10865 : {
998 : 10865 : 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 : 21730 : if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
1001 : 10840 : 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 : 4345814 : 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 : 4345814 : {
1014 : 4345814 : tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1015 : 4345814 : tree p1 = TYPE_ARG_TYPES (t1);
1016 : 4345814 : tree p2 = TYPE_ARG_TYPES (t2);
1017 : 4345814 : tree parms;
1018 : :
1019 : : /* Save space: see if the result is identical to one of the args. */
1020 : 4345814 : if (valtype == TREE_TYPE (t1) && ! p2)
1021 : 0 : return cp_build_type_attribute_variant (t1, attributes);
1022 : 4345814 : 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 : 8691628 : if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1027 : : parms = p2;
1028 : 8398490 : else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1029 : : parms = p1;
1030 : : else
1031 : 4199245 : parms = commonparms (p1, p2);
1032 : :
1033 : 4345814 : cp_cv_quals quals = type_memfn_quals (t1);
1034 : 4345814 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1035 : 4345814 : gcc_assert (quals == type_memfn_quals (t2));
1036 : 4345814 : gcc_assert (rqual == type_memfn_rqual (t2));
1037 : :
1038 : 4345814 : tree rval = cp_build_function_type (valtype, parms);
1039 : 4345814 : rval = apply_memfn_quals (rval, quals);
1040 : 4345814 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1041 : 4345814 : TYPE_RAISES_EXCEPTIONS (t2));
1042 : 4345814 : bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1043 : 4345814 : t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1044 : 4345814 : break;
1045 : : }
1046 : :
1047 : 4004987 : case METHOD_TYPE:
1048 : 4004987 : {
1049 : : /* Get this value the long way, since TYPE_METHOD_BASETYPE
1050 : : is just the main variant of this. */
1051 : 4004987 : tree basetype = class_of_this_parm (t2);
1052 : 4004987 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1053 : 4004987 : TYPE_RAISES_EXCEPTIONS (t2));
1054 : 4004987 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1055 : 4004987 : tree t3;
1056 : 4004987 : 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 : 4004987 : t1 = cp_build_function_type (TREE_TYPE (t1),
1062 : 4004987 : TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1063 : 4004987 : t2 = cp_build_function_type (TREE_TYPE (t2),
1064 : 4004987 : TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1065 : 4004987 : t3 = merge_types (t1, t2);
1066 : 4004987 : t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1067 : 4004987 : TYPE_ARG_TYPES (t3));
1068 : 4004987 : t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1069 : 4004987 : 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 : 5280205 : default:;
1079 : 5280205 : 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 : 8350819 : 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 : 457230 : common_type (tree t1, tree t2)
1112 : : {
1113 : : /* If one type is nonsense, use the other */
1114 : 457230 : if (t1 == error_mark_node)
1115 : : return t2;
1116 : 457230 : if (t2 == error_mark_node)
1117 : : return t1;
1118 : :
1119 : 457230 : 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 : 1300945 : common_pointer_type (tree t1, tree t2)
1131 : : {
1132 : 1300945 : 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 : 1300945 : return composite_pointer_type (input_location, t1, t2,
1137 : : error_mark_node, error_mark_node,
1138 : 1300945 : CPO_CONVERSION, tf_warning_or_error);
1139 : : }
1140 : :
1141 : : /* Compare two exception specifier types for exactness or subsetness, if
1142 : : allowed. Returns false for mismatch, true for match (same, or
1143 : : derived and !exact).
1144 : :
1145 : : [except.spec] "If a class X ... objects of class X or any class publicly
1146 : : and unambiguously derived from X. Similarly, if a pointer type Y * ...
1147 : : exceptions of type Y * or that are pointers to any type publicly and
1148 : : unambiguously derived from Y. Otherwise a function only allows exceptions
1149 : : that have the same type ..."
1150 : : This does not mention cv qualifiers and is different to what throw
1151 : : [except.throw] and catch [except.catch] will do. They will ignore the
1152 : : top level cv qualifiers, and allow qualifiers in the pointer to class
1153 : : example.
1154 : :
1155 : : We implement the letter of the standard. */
1156 : :
1157 : : static bool
1158 : 1694 : comp_except_types (tree a, tree b, bool exact)
1159 : : {
1160 : 1694 : if (same_type_p (a, b))
1161 : : return true;
1162 : 508 : else if (!exact)
1163 : : {
1164 : 12 : if (cp_type_quals (a) || cp_type_quals (b))
1165 : 0 : return false;
1166 : :
1167 : 12 : if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1168 : : {
1169 : 7 : a = TREE_TYPE (a);
1170 : 7 : b = TREE_TYPE (b);
1171 : 7 : if (cp_type_quals (a) || cp_type_quals (b))
1172 : 1 : return false;
1173 : : }
1174 : :
1175 : 11 : if (TREE_CODE (a) != RECORD_TYPE
1176 : 11 : || TREE_CODE (b) != RECORD_TYPE)
1177 : : return false;
1178 : :
1179 : 10 : if (publicly_uniquely_derived_p (a, b))
1180 : : return true;
1181 : : }
1182 : : return false;
1183 : : }
1184 : :
1185 : : /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1186 : : If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1187 : : If EXACT is ce_type, the C++17 type compatibility rules apply.
1188 : : If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1189 : : If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1190 : : are unordered, but we've already filtered out duplicates. Most lists will
1191 : : be in order, we should try to make use of that. */
1192 : :
1193 : : bool
1194 : 1439004008 : comp_except_specs (const_tree t1, const_tree t2, int exact)
1195 : : {
1196 : 1439004008 : const_tree probe;
1197 : 1439004008 : const_tree base;
1198 : 1439004008 : int length = 0;
1199 : :
1200 : 1439004008 : if (t1 == t2)
1201 : : return true;
1202 : :
1203 : : /* First handle noexcept. */
1204 : 563563576 : if (exact < ce_exact)
1205 : : {
1206 : 6971744 : if (exact == ce_type
1207 : 13878475 : && (canonical_eh_spec (CONST_CAST_TREE (t1))
1208 : 6906731 : == 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 : 6952399 : 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 : 6952246 : if (t2 == noexcept_false_spec)
1218 : 1704 : return t1 == NULL_TREE;
1219 : :
1220 : : /* Otherwise, if we aren't looking for an exact match, noexcept is
1221 : : equivalent to throw(). */
1222 : 6950542 : if (t1 == noexcept_true_spec)
1223 : 695784 : t1 = empty_except_spec;
1224 : 6950542 : if (t2 == noexcept_true_spec)
1225 : 5985525 : 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 : 540772195 : if ((t1 && TREE_PURPOSE (t1))
1232 : 589095824 : || (t2 && TREE_PURPOSE (t2)))
1233 : 554613968 : return (t1 && t2
1234 : 554613968 : && (exact == ce_exact
1235 : 63857298 : ? TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
1236 : 266282 : : cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))));
1237 : :
1238 : 8928406 : if (t1 == NULL_TREE) /* T1 is ... */
1239 : 6207475 : return t2 == NULL_TREE || exact == ce_derived;
1240 : 2720931 : if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1241 : 2687749 : return t2 != NULL_TREE && !TREE_VALUE (t2);
1242 : 34488 : if (t2 == NULL_TREE) /* T2 is ... */
1243 : : return false;
1244 : 1572 : if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1245 : 76 : return exact == ce_derived;
1246 : :
1247 : : /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1248 : : Count how many we find, to determine exactness. For exact matching and
1249 : : ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1250 : : O(nm). */
1251 : 2685 : for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1252 : : {
1253 : 2092 : for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1254 : : {
1255 : 1694 : tree a = TREE_VALUE (probe);
1256 : 1694 : tree b = TREE_VALUE (t2);
1257 : :
1258 : 1694 : if (comp_except_types (a, b, exact))
1259 : : {
1260 : 1189 : if (probe == base && exact > ce_derived)
1261 : 1154 : base = TREE_CHAIN (probe);
1262 : 1189 : length++;
1263 : 1189 : break;
1264 : : }
1265 : : }
1266 : 1189 : if (probe == NULL_TREE)
1267 : : return false;
1268 : : }
1269 : 1098 : return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1270 : : }
1271 : :
1272 : : /* Compare the array types T1 and T2. CB says how we should behave when
1273 : : comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1274 : : bounds_either says than any array can be [], bounds_first means that
1275 : : onlt T1 can be an array with unknown bounds. STRICT is true if
1276 : : qualifiers must match when comparing the types of the array elements. */
1277 : :
1278 : : static bool
1279 : 1399788 : comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1280 : : bool strict)
1281 : : {
1282 : 1399788 : tree d1;
1283 : 1399788 : tree d2;
1284 : 1399788 : tree max1, max2;
1285 : :
1286 : 1399788 : if (t1 == t2)
1287 : : return true;
1288 : :
1289 : : /* The type of the array elements must be the same. */
1290 : 1399684 : if (strict
1291 : 1399684 : ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1292 : 3119 : : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1293 : : return false;
1294 : :
1295 : 940103 : d1 = TYPE_DOMAIN (t1);
1296 : 940103 : d2 = TYPE_DOMAIN (t2);
1297 : :
1298 : 940103 : 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 : 630412 : if (!d1 && d2)
1314 : 2304 : return cb >= bounds_either;
1315 : 628108 : else if (d1 && !d2)
1316 : 20683 : return cb == bounds_either;
1317 : :
1318 : : /* Check that the dimensions are the same. */
1319 : :
1320 : 607425 : if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1321 : : return false;
1322 : 607404 : max1 = TYPE_MAX_VALUE (d1);
1323 : 607404 : max2 = TYPE_MAX_VALUE (d2);
1324 : :
1325 : 607404 : 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 : 1516497270 : comp_template_parms_position (tree t1, tree t2)
1338 : : {
1339 : 1516497270 : tree index1, index2;
1340 : 1516497270 : 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 : 1516497270 : index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1347 : 1516497270 : index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1348 : :
1349 : : /* Then compare their relative position. */
1350 : 1516497270 : if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1351 : 976771881 : || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1352 : 2188913899 : || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1353 : 672416629 : != 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 : 592038257 : if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1359 : 116058709 : 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 : 6948 : cxx_safe_function_type_cast_p (tree t1, tree t2)
1391 : : {
1392 : 6948 : if (TREE_TYPE (t1) == void_type_node &&
1393 : 6875 : TYPE_ARG_TYPES (t1) == void_list_node)
1394 : : return true;
1395 : :
1396 : 5467 : if (TREE_TYPE (t2) == void_type_node &&
1397 : 5261 : 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 : 12605654539 : structural_comptypes (tree t1, tree t2, int strict)
1416 : : {
1417 : : /* Both should be types that are not obviously the same. */
1418 : 12605654539 : 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 : 12605654539 : if (!comparing_specializations)
1423 : : {
1424 : : /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1425 : : current instantiation. */
1426 : 10529885452 : if (TREE_CODE (t1) == TYPENAME_TYPE)
1427 : 92300339 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1428 : :
1429 : 10529885452 : if (TREE_CODE (t2) == TYPENAME_TYPE)
1430 : 114073575 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1431 : : }
1432 : :
1433 : 12605654539 : if (TYPE_PTRMEMFUNC_P (t1))
1434 : 26945827 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1435 : 12605654539 : if (TYPE_PTRMEMFUNC_P (t2))
1436 : 30404355 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1437 : :
1438 : : /* Different classes of types can't be compatible. */
1439 : 12605654539 : 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 : 8142432651 : if (TREE_CODE (t1) != ARRAY_TYPE
1445 : 8142432651 : && cp_type_quals (t1) != cp_type_quals (t2))
1446 : : return false;
1447 : 7641759110 : if (TREE_CODE (t1) == FUNCTION_TYPE
1448 : 7641759110 : && 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 : 7641650646 : if (FUNC_OR_METHOD_TYPE_P (t1))
1453 : : {
1454 : 33775344 : if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1455 : : return false;
1456 : 17966603 : if (flag_noexcept_type
1457 : 35860858 : && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1458 : 17894255 : 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 : 7619129301 : if (TREE_CODE (t1) != ARRAY_TYPE
1467 : 7619129301 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1468 : 340197547 : 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 : 7278931754 : 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 : 523434776 : case OPAQUE_TYPE:
1481 : 523434776 : case INTEGER_TYPE:
1482 : 523434776 : case FIXED_POINT_TYPE:
1483 : 523434776 : 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 : 523434776 : 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 : 981180 : case TEMPLATE_TEMPLATE_PARM:
1505 : 981180 : case BOUND_TEMPLATE_TEMPLATE_PARM:
1506 : 981180 : if (!comp_template_parms_position (t1, t2))
1507 : : return false;
1508 : 865112 : if (!comp_template_parms
1509 : 1730224 : (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1510 : 2430219 : DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1511 : : return false;
1512 : 616221 : if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1513 : : break;
1514 : : /* Don't check inheritance. */
1515 : : strict = COMPARE_STRICT;
1516 : : /* Fall through. */
1517 : :
1518 : 3658808185 : case RECORD_TYPE:
1519 : 3658808185 : case UNION_TYPE:
1520 : 6438891806 : if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1521 : 2257459632 : && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1522 : 1743445429 : || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1523 : 4172857306 : && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1524 : : break;
1525 : :
1526 : 3618520487 : if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1527 : : break;
1528 : 3618520454 : else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1529 : : break;
1530 : :
1531 : : return false;
1532 : :
1533 : 44521 : case OFFSET_TYPE:
1534 : 44521 : if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1535 : : strict & ~COMPARE_REDECLARATION))
1536 : : return false;
1537 : 42515 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1538 : : return false;
1539 : : break;
1540 : :
1541 : 824157295 : case REFERENCE_TYPE:
1542 : 824157295 : if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1543 : : return false;
1544 : : /* fall through to checks for pointer types */
1545 : 1149134531 : gcc_fallthrough ();
1546 : :
1547 : 1149134531 : case POINTER_TYPE:
1548 : 1149134531 : if (TYPE_MODE (t1) != TYPE_MODE (t2)
1549 : 1149134531 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1550 : 1036565638 : return false;
1551 : : break;
1552 : :
1553 : 3493152 : case FUNCTION_TYPE:
1554 : 3493152 : if (TYPE_NO_NAMED_ARGS_STDARG_P (t1) != TYPE_NO_NAMED_ARGS_STDARG_P (t2))
1555 : : return false;
1556 : : /* FALLTHRU */
1557 : 10933421 : case METHOD_TYPE:
1558 : : /* Exception specs and memfn_rquals were checked above. */
1559 : 10933421 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1560 : : return false;
1561 : 10286915 : if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1562 : : return false;
1563 : : break;
1564 : :
1565 : 1396565 : case ARRAY_TYPE:
1566 : : /* Target types must match incl. qualifiers. */
1567 : 1396565 : if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1568 : 1396565 : ? bounds_either : bounds_none),
1569 : : /*strict=*/true))
1570 : : return false;
1571 : : break;
1572 : :
1573 : 1515516090 : 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 : 1515516090 : 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 : 1031408444 : if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1581 : 515704222 : CLASS_PLACEHOLDER_TEMPLATE (t2)))
1582 : : return false;
1583 : : /* Constrained 'auto's are distinct from parms that don't have the same
1584 : : constraints. */
1585 : 499377248 : if (!equivalent_placeholder_constraints (t1, t2))
1586 : : return false;
1587 : : break;
1588 : :
1589 : 142747227 : case TYPENAME_TYPE:
1590 : 285494454 : if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1591 : 142747227 : TYPENAME_TYPE_FULLNAME (t2)))
1592 : : return false;
1593 : : /* Qualifiers don't matter on scopes. */
1594 : 124681587 : if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1595 : 124681587 : TYPE_CONTEXT (t2)))
1596 : : return false;
1597 : : break;
1598 : :
1599 : 7979 : case UNBOUND_CLASS_TEMPLATE:
1600 : 7979 : if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1601 : : return false;
1602 : 7979 : if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1603 : : return false;
1604 : : break;
1605 : :
1606 : 3317185 : case COMPLEX_TYPE:
1607 : 3317185 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1608 : : return false;
1609 : : break;
1610 : :
1611 : 12632823 : case VECTOR_TYPE:
1612 : 12632823 : if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1613 : 23603104 : || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1614 : 23655707 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1615 : 1662542 : return false;
1616 : : break;
1617 : :
1618 : 6572637 : case TYPE_PACK_EXPANSION:
1619 : 6572637 : return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1620 : : PACK_EXPANSION_PATTERN (t2))
1621 : 12976694 : && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1622 : 6404057 : 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 : 14991465 : case DECLTYPE_TYPE:
1631 : 29982930 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1632 : 14991465 : != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1633 : : return false;
1634 : 14951193 : if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1635 : : return false;
1636 : 14951193 : if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1637 : : return false;
1638 : 14951193 : if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1639 : : return false;
1640 : : break;
1641 : :
1642 : 715568 : case TRAIT_TYPE:
1643 : 715568 : if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1644 : : return false;
1645 : 1327 : if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1646 : 2654 : || !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 : 675595679 : if (!comp_type_attributes (t1, t2))
1663 : : return false;
1664 : :
1665 : 675594535 : check_alias:
1666 : 1015801201 : if (comparing_dependent_aliases
1667 : 1015801201 : && (typedef_variant_p (t1) || typedef_variant_p (t2)))
1668 : : {
1669 : 3177616 : tree dep1 = dependent_opaque_alias_p (t1) ? t1 : NULL_TREE;
1670 : 3177616 : tree dep2 = dependent_opaque_alias_p (t2) ? t2 : NULL_TREE;
1671 : 3177616 : if ((dep1 || dep2)
1672 : 3177616 : && (!(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 : 3177599 : ++processing_template_decl;
1684 : 3177599 : dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1685 : 3177599 : dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1686 : 3177599 : --processing_template_decl;
1687 : 3177599 : 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 : 1675 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1698 : : {
1699 : 1675 : 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 : 19512164409 : comptypes (tree t1, tree t2, int strict)
1707 : : {
1708 : 19512164409 : gcc_checking_assert (t1 && t2);
1709 : :
1710 : : /* TYPE_ARGUMENT_PACKS are not really types. */
1711 : 19512164409 : gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1712 : : && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1713 : :
1714 : 19512164409 : if (t1 == t2)
1715 : : return true;
1716 : :
1717 : : /* Suppress errors caused by previously reported errors. */
1718 : 12605655718 : if (t1 == error_mark_node || t2 == error_mark_node)
1719 : : return false;
1720 : :
1721 : 12605655542 : if (strict == COMPARE_STRICT)
1722 : : {
1723 : 11437725421 : 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 : 980307656 : return structural_comptypes (t1, t2, strict);
1727 : :
1728 : 10457417765 : if (flag_checking && param_use_canonical_types)
1729 : : {
1730 : 10457416762 : bool result = structural_comptypes (t1, t2, strict);
1731 : :
1732 : 10457416762 : 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 : 10457416762 : 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 : 1167930121 : else if (strict == COMPARE_STRUCTURAL)
1755 : 1127838408 : return structural_comptypes (t1, t2, COMPARE_STRICT);
1756 : : else
1757 : 40091713 : 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 : 2913259108 : same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1765 : : {
1766 : 2913259108 : if (type1 == error_mark_node || type2 == error_mark_node)
1767 : : return false;
1768 : 2913259098 : if (type1 == type2)
1769 : : return true;
1770 : :
1771 : 1591238872 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1772 : 1591238872 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1773 : 1591238872 : 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 : 228117771 : similar_type_p (tree type1, tree type2)
1780 : : {
1781 : 228117771 : 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 : 228117771 : if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1793 : : return true;
1794 : :
1795 : 102699469 : if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1796 : 102584275 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1797 : 102584275 : || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1798 : 115943 : 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 : 1996 : next_common_initial_sequence (tree &memb1, tree &memb2)
1810 : : {
1811 : 2456 : while (memb1)
1812 : : {
1813 : 2492 : if (TREE_CODE (memb1) != FIELD_DECL
1814 : 2090 : || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1815 : : {
1816 : 402 : memb1 = DECL_CHAIN (memb1);
1817 : 402 : continue;
1818 : : }
1819 : 1688 : if (DECL_FIELD_IS_BASE (memb1))
1820 : : {
1821 : 58 : memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1822 : 58 : continue;
1823 : : }
1824 : : break;
1825 : : }
1826 : 2426 : while (memb2)
1827 : : {
1828 : 2462 : if (TREE_CODE (memb2) != FIELD_DECL
1829 : 2060 : || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1830 : : {
1831 : 402 : memb2 = DECL_CHAIN (memb2);
1832 : 402 : continue;
1833 : : }
1834 : 1658 : if (DECL_FIELD_IS_BASE (memb2))
1835 : : {
1836 : 28 : memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1837 : 28 : continue;
1838 : : }
1839 : : break;
1840 : : }
1841 : 1996 : if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1842 : : return true;
1843 : 1630 : if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1844 : : return false;
1845 : 1630 : if (DECL_BIT_FIELD_TYPE (memb1))
1846 : : {
1847 : 138 : if (!DECL_BIT_FIELD_TYPE (memb2))
1848 : : return false;
1849 : 39 : if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1850 : 39 : DECL_BIT_FIELD_TYPE (memb2)))
1851 : : return false;
1852 : 39 : if (TYPE_PRECISION (TREE_TYPE (memb1))
1853 : 39 : != TYPE_PRECISION (TREE_TYPE (memb2)))
1854 : : return false;
1855 : : }
1856 : 1492 : else if (DECL_BIT_FIELD_TYPE (memb2))
1857 : : return false;
1858 : 1465 : else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1859 : : return false;
1860 : 1441 : if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1861 : 1441 : != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1862 : : return false;
1863 : 1414 : if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1864 : : return false;
1865 : 1383 : 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 : 2503 : layout_compatible_type_p (tree type1, tree type2)
1874 : : {
1875 : 2503 : if (type1 == error_mark_node || type2 == error_mark_node)
1876 : : return false;
1877 : 2503 : if (type1 == type2)
1878 : : return true;
1879 : 1222 : if (TREE_CODE (type1) != TREE_CODE (type2))
1880 : : return false;
1881 : :
1882 : 1184 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1883 : 1184 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1884 : :
1885 : 1184 : if (TREE_CODE (type1) == ENUMERAL_TYPE)
1886 : 32 : return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1887 : 32 : && same_type_p (finish_underlying_type (type1),
1888 : : finish_underlying_type (type2)));
1889 : :
1890 : 367 : if (CLASS_TYPE_P (type1)
1891 : 367 : && std_layout_type_p (type1)
1892 : 357 : && std_layout_type_p (type2)
1893 : 1509 : && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1894 : : {
1895 : 315 : tree field1 = TYPE_FIELDS (type1);
1896 : 315 : tree field2 = TYPE_FIELDS (type2);
1897 : 315 : if (TREE_CODE (type1) == RECORD_TYPE)
1898 : : {
1899 : 966 : while (1)
1900 : : {
1901 : 606 : if (!next_common_initial_sequence (field1, field2))
1902 : : return false;
1903 : 600 : if (field1 == NULL_TREE)
1904 : : return true;
1905 : 360 : field1 = DECL_CHAIN (field1);
1906 : 360 : field2 = DECL_CHAIN (field2);
1907 : : }
1908 : : }
1909 : : /* Otherwise both types must be union types.
1910 : : The standard says:
1911 : : "Two standard-layout unions are layout-compatible if they have
1912 : : the same number of non-static data members and corresponding
1913 : : non-static data members (in any order) have layout-compatible
1914 : : types."
1915 : : but the code anticipates that bitfield vs. non-bitfield,
1916 : : different bitfield widths or presence/absence of
1917 : : [[no_unique_address]] should be checked as well. */
1918 : 69 : auto_vec<tree, 16> vec;
1919 : 69 : unsigned int count = 0;
1920 : 315 : for (; field1; field1 = DECL_CHAIN (field1))
1921 : 246 : if (TREE_CODE (field1) == FIELD_DECL)
1922 : 162 : count++;
1923 : 315 : for (; field2; field2 = DECL_CHAIN (field2))
1924 : 246 : if (TREE_CODE (field2) == FIELD_DECL)
1925 : 162 : vec.safe_push (field2);
1926 : : /* Discussions on core lean towards treating multiple union fields
1927 : : of the same type as the same field, so this might need changing
1928 : : in the future. */
1929 : 138 : if (count != vec.length ())
1930 : : return false;
1931 : 297 : for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1932 : : {
1933 : 234 : if (TREE_CODE (field1) != FIELD_DECL)
1934 : 78 : continue;
1935 : 156 : unsigned int j;
1936 : 156 : tree t1 = DECL_BIT_FIELD_TYPE (field1);
1937 : 156 : if (t1 == NULL_TREE)
1938 : 153 : t1 = TREE_TYPE (field1);
1939 : 258 : FOR_EACH_VEC_ELT (vec, j, field2)
1940 : : {
1941 : 252 : tree t2 = DECL_BIT_FIELD_TYPE (field2);
1942 : 252 : if (t2 == NULL_TREE)
1943 : 240 : t2 = TREE_TYPE (field2);
1944 : 252 : if (DECL_BIT_FIELD_TYPE (field1))
1945 : : {
1946 : 6 : if (!DECL_BIT_FIELD_TYPE (field2))
1947 : 0 : continue;
1948 : 6 : if (TYPE_PRECISION (TREE_TYPE (field1))
1949 : 6 : != TYPE_PRECISION (TREE_TYPE (field2)))
1950 : 6 : continue;
1951 : : }
1952 : 246 : else if (DECL_BIT_FIELD_TYPE (field2))
1953 : 6 : continue;
1954 : 240 : if (!layout_compatible_type_p (t1, t2))
1955 : 90 : continue;
1956 : 150 : if ((!lookup_attribute ("no_unique_address",
1957 : 150 : DECL_ATTRIBUTES (field1)))
1958 : 150 : != !lookup_attribute ("no_unique_address",
1959 : 150 : DECL_ATTRIBUTES (field2)))
1960 : 0 : continue;
1961 : : break;
1962 : : }
1963 : 312 : if (j == vec.length ())
1964 : : return false;
1965 : 150 : vec.unordered_remove (j);
1966 : : }
1967 : : return true;
1968 : 69 : }
1969 : :
1970 : 837 : return same_type_p (type1, type2);
1971 : : }
1972 : :
1973 : : /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1974 : :
1975 : : bool
1976 : 167554072 : at_least_as_qualified_p (const_tree type1, const_tree type2)
1977 : : {
1978 : 167554072 : int q1 = cp_type_quals (type1);
1979 : 167554072 : int q2 = cp_type_quals (type2);
1980 : :
1981 : : /* All qualifiers for TYPE2 must also appear in TYPE1. */
1982 : 167554072 : return (q1 & q2) == q2;
1983 : : }
1984 : :
1985 : : /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1986 : : more cv-qualified that TYPE1, and 0 otherwise. */
1987 : :
1988 : : int
1989 : 11062095 : comp_cv_qualification (int q1, int q2)
1990 : : {
1991 : 11062095 : if (q1 == q2)
1992 : : return 0;
1993 : :
1994 : 3058385 : if ((q1 & q2) == q2)
1995 : : return 1;
1996 : 253514 : else if ((q1 & q2) == q1)
1997 : 252925 : return -1;
1998 : :
1999 : : return 0;
2000 : : }
2001 : :
2002 : : int
2003 : 0 : comp_cv_qualification (const_tree type1, const_tree type2)
2004 : : {
2005 : 0 : int q1 = cp_type_quals (type1);
2006 : 0 : int q2 = cp_type_quals (type2);
2007 : 0 : return comp_cv_qualification (q1, q2);
2008 : : }
2009 : :
2010 : : /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
2011 : : subset of the cv-qualification signature of TYPE2, and the types
2012 : : are similar. Returns -1 if the other way 'round, and 0 otherwise. */
2013 : :
2014 : : int
2015 : 381 : comp_cv_qual_signature (tree type1, tree type2)
2016 : : {
2017 : 381 : if (comp_ptr_ttypes_real (type2, type1, -1))
2018 : : return 1;
2019 : 295 : else if (comp_ptr_ttypes_real (type1, type2, -1))
2020 : : return -1;
2021 : : else
2022 : 283 : return 0;
2023 : : }
2024 : :
2025 : : /* Subroutines of `comptypes'. */
2026 : :
2027 : : /* Return true if two parameter type lists PARMS1 and PARMS2 are
2028 : : equivalent in the sense that functions with those parameter types
2029 : : can have equivalent types. The two lists must be equivalent,
2030 : : element by element. */
2031 : :
2032 : : bool
2033 : 866306897 : compparms (const_tree parms1, const_tree parms2)
2034 : : {
2035 : 866306897 : const_tree t1, t2;
2036 : :
2037 : : /* An unspecified parmlist matches any specified parmlist
2038 : : whose argument types don't need default promotions. */
2039 : :
2040 : 866306897 : for (t1 = parms1, t2 = parms2;
2041 : 1401640621 : t1 || t2;
2042 : 535333724 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2043 : : {
2044 : : /* If one parmlist is shorter than the other,
2045 : : they fail to match. */
2046 : 1317787397 : if (!t1 || !t2)
2047 : : return false;
2048 : 1317178836 : if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2049 : : return false;
2050 : : }
2051 : : return true;
2052 : : }
2053 : :
2054 : :
2055 : : /* Process a sizeof or alignof expression where the operand is a type.
2056 : : STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2057 : : or GNU (preferred alignment) semantics; it is ignored if OP is
2058 : : SIZEOF_EXPR. */
2059 : :
2060 : : tree
2061 : 22928245 : cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2062 : : bool std_alignof, bool complain)
2063 : : {
2064 : 22928245 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2065 : 22928245 : if (type == error_mark_node)
2066 : : return error_mark_node;
2067 : :
2068 : 22928243 : type = non_reference (type);
2069 : 22928243 : if (TREE_CODE (type) == METHOD_TYPE)
2070 : : {
2071 : 0 : if (complain)
2072 : : {
2073 : 0 : pedwarn (loc, OPT_Wpointer_arith,
2074 : : "invalid application of %qs to a member function",
2075 : 0 : OVL_OP_INFO (false, op)->name);
2076 : 0 : return size_one_node;
2077 : : }
2078 : : else
2079 : 0 : return error_mark_node;
2080 : : }
2081 : 22928243 : else if (VOID_TYPE_P (type) && std_alignof)
2082 : : {
2083 : 14 : if (complain)
2084 : 14 : error_at (loc, "invalid application of %qs to a void type",
2085 : 14 : OVL_OP_INFO (false, op)->name);
2086 : 14 : return error_mark_node;
2087 : : }
2088 : :
2089 : 22928229 : bool dependent_p = dependent_type_p (type);
2090 : 22928229 : if (!dependent_p)
2091 : : {
2092 : 19080981 : complete_type (type);
2093 : 19080981 : if (!COMPLETE_TYPE_P (type))
2094 : : /* Call this here because the incompleteness diagnostic comes from
2095 : : c_sizeof_or_alignof_type instead of
2096 : : complete_type_or_maybe_complain. */
2097 : 190 : note_failed_type_completion (type, complain);
2098 : : }
2099 : 19080981 : if (dependent_p
2100 : : /* VLA types will have a non-constant size. In the body of an
2101 : : uninstantiated template, we don't need to try to compute the
2102 : : value, because the sizeof expression is not an integral
2103 : : constant expression in that case. And, if we do try to
2104 : : compute the value, we'll likely end up with SAVE_EXPRs, which
2105 : : the template substitution machinery does not expect to see. */
2106 : 19080981 : || (processing_template_decl
2107 : 1740141 : && COMPLETE_TYPE_P (type)
2108 : 1740135 : && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2109 : : {
2110 : 3847248 : tree value = build_min (op, size_type_node, type);
2111 : 3847248 : TREE_READONLY (value) = 1;
2112 : 3847248 : if (op == ALIGNOF_EXPR && std_alignof)
2113 : 458443 : ALIGNOF_EXPR_STD_P (value) = true;
2114 : 3847248 : SET_EXPR_LOCATION (value, loc);
2115 : 3847248 : return value;
2116 : : }
2117 : :
2118 : 19080981 : return c_sizeof_or_alignof_type (loc, complete_type (type),
2119 : : op == SIZEOF_EXPR, std_alignof,
2120 : 19080981 : complain & (tf_warning_or_error));
2121 : : }
2122 : :
2123 : : /* Return the size of the type, without producing any warnings for
2124 : : types whose size cannot be taken. This routine should be used only
2125 : : in some other routine that has already produced a diagnostic about
2126 : : using the size of such a type. */
2127 : : tree
2128 : 2271807 : cxx_sizeof_nowarn (tree type)
2129 : : {
2130 : 2271807 : if (TREE_CODE (type) == FUNCTION_TYPE
2131 : 2271807 : || VOID_TYPE_P (type)
2132 : 2271725 : || TREE_CODE (type) == ERROR_MARK)
2133 : 82 : return size_one_node;
2134 : 2271725 : else if (!COMPLETE_TYPE_P (type))
2135 : 19 : return size_zero_node;
2136 : : else
2137 : 2271706 : return cxx_sizeof_or_alignof_type (input_location, type,
2138 : 2271706 : SIZEOF_EXPR, false, false);
2139 : : }
2140 : :
2141 : : /* Process a sizeof expression where the operand is an expression. */
2142 : :
2143 : : static tree
2144 : 1405918 : cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2145 : : {
2146 : 1405918 : if (e == error_mark_node)
2147 : : return error_mark_node;
2148 : :
2149 : 1405179 : if (instantiation_dependent_uneval_expression_p (e))
2150 : : {
2151 : 322508 : e = build_min (SIZEOF_EXPR, size_type_node, e);
2152 : 322508 : TREE_SIDE_EFFECTS (e) = 0;
2153 : 322508 : TREE_READONLY (e) = 1;
2154 : 322508 : SET_EXPR_LOCATION (e, loc);
2155 : :
2156 : 322508 : return e;
2157 : : }
2158 : :
2159 : 1082671 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2160 : 1082671 : STRIP_ANY_LOCATION_WRAPPER (e);
2161 : :
2162 : 1082671 : if (TREE_CODE (e) == PARM_DECL
2163 : 46490 : && DECL_ARRAY_PARAMETER_P (e)
2164 : 1083048 : && (complain & tf_warning))
2165 : : {
2166 : 120 : auto_diagnostic_group d;
2167 : 120 : if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2168 : : "%<sizeof%> on array function parameter %qE "
2169 : 120 : "will return size of %qT", e, TREE_TYPE (e)))
2170 : 24 : inform (DECL_SOURCE_LOCATION (e), "declared here");
2171 : 120 : }
2172 : :
2173 : 1082671 : e = mark_type_use (e);
2174 : :
2175 : 1082671 : if (bitfield_p (e))
2176 : : {
2177 : 12 : if (complain & tf_error)
2178 : 6 : error_at (e_loc,
2179 : : "invalid application of %<sizeof%> to a bit-field");
2180 : : else
2181 : 6 : return error_mark_node;
2182 : 6 : e = char_type_node;
2183 : : }
2184 : 1082659 : else if (is_overloaded_fn (e))
2185 : : {
2186 : 33 : if (complain & tf_error)
2187 : 12 : permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2188 : : "an expression of function type");
2189 : : else
2190 : 21 : return error_mark_node;
2191 : 12 : e = char_type_node;
2192 : : }
2193 : 1082626 : else if (type_unknown_p (e))
2194 : : {
2195 : 0 : if (complain & tf_error)
2196 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2197 : : else
2198 : 0 : return error_mark_node;
2199 : 0 : e = char_type_node;
2200 : : }
2201 : : else
2202 : 1082626 : e = TREE_TYPE (e);
2203 : :
2204 : 1082644 : return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2205 : 1082644 : complain & tf_error);
2206 : : }
2207 : :
2208 : : /* Implement the __alignof keyword: Return the minimum required
2209 : : alignment of E, measured in bytes. For VAR_DECL's and
2210 : : FIELD_DECL's return DECL_ALIGN (which can be set from an
2211 : : "aligned" __attribute__ specification). STD_ALIGNOF acts
2212 : : like in cxx_sizeof_or_alignof_type. */
2213 : :
2214 : : static tree
2215 : 101693 : cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2216 : : tsubst_flags_t complain)
2217 : : {
2218 : 101693 : tree t;
2219 : :
2220 : 101693 : if (e == error_mark_node)
2221 : : return error_mark_node;
2222 : :
2223 : 101685 : if (processing_template_decl)
2224 : : {
2225 : 19980 : e = build_min (ALIGNOF_EXPR, size_type_node, e);
2226 : 19980 : TREE_SIDE_EFFECTS (e) = 0;
2227 : 19980 : TREE_READONLY (e) = 1;
2228 : 19980 : SET_EXPR_LOCATION (e, loc);
2229 : 19980 : ALIGNOF_EXPR_STD_P (e) = std_alignof;
2230 : :
2231 : 19980 : return e;
2232 : : }
2233 : :
2234 : 81705 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2235 : 81705 : STRIP_ANY_LOCATION_WRAPPER (e);
2236 : :
2237 : 81705 : e = mark_type_use (e);
2238 : :
2239 : 81705 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2240 : : !(complain & tf_error)))
2241 : : {
2242 : 0 : if (!(complain & tf_error))
2243 : 0 : return error_mark_node;
2244 : 0 : t = size_one_node;
2245 : : }
2246 : 81705 : else if (VAR_P (e))
2247 : 12270 : t = size_int (DECL_ALIGN_UNIT (e));
2248 : 69435 : else if (bitfield_p (e))
2249 : : {
2250 : 6 : if (complain & tf_error)
2251 : 6 : error_at (e_loc,
2252 : : "invalid application of %<__alignof%> to a bit-field");
2253 : : else
2254 : 0 : return error_mark_node;
2255 : 6 : t = size_one_node;
2256 : : }
2257 : 69429 : else if (TREE_CODE (e) == COMPONENT_REF
2258 : 69429 : && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2259 : 37792 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2260 : 31637 : else if (is_overloaded_fn (e))
2261 : : {
2262 : 6 : if (complain & tf_error)
2263 : 6 : permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2264 : : "an expression of function type");
2265 : : else
2266 : 0 : return error_mark_node;
2267 : 6 : if (TREE_CODE (e) == FUNCTION_DECL)
2268 : 3 : t = size_int (DECL_ALIGN_UNIT (e));
2269 : : else
2270 : 3 : t = size_one_node;
2271 : : }
2272 : 31631 : else if (type_unknown_p (e))
2273 : : {
2274 : 0 : if (complain & tf_error)
2275 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2276 : : else
2277 : 0 : return error_mark_node;
2278 : 0 : t = size_one_node;
2279 : : }
2280 : : else
2281 : 31631 : return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2282 : : ALIGNOF_EXPR, std_alignof,
2283 : 31631 : complain & tf_error);
2284 : :
2285 : 50074 : return fold_convert_loc (loc, size_type_node, t);
2286 : : }
2287 : :
2288 : : /* Process a sizeof or alignof expression E with code OP where the operand
2289 : : is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2290 : :
2291 : : tree
2292 : 1507611 : cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2293 : : bool std_alignof, bool complain)
2294 : : {
2295 : 1507611 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2296 : 1507611 : if (op == SIZEOF_EXPR)
2297 : 2124527 : return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2298 : : else
2299 : 101720 : return cxx_alignof_expr (loc, e, std_alignof,
2300 : 101693 : complain? tf_warning_or_error : tf_none);
2301 : : }
2302 : :
2303 : : /* Build a representation of an expression 'alignas(E).' Return the
2304 : : folded integer value of E if it is an integral constant expression
2305 : : that resolves to a valid alignment. If E depends on a template
2306 : : parameter, return a syntactic representation tree of kind
2307 : : ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2308 : : expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2309 : :
2310 : : tree
2311 : 200136 : cxx_alignas_expr (tree e)
2312 : : {
2313 : 200136 : if (e == NULL_TREE || e == error_mark_node
2314 : 400272 : || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2315 : 0 : return e;
2316 : :
2317 : 200136 : if (TYPE_P (e))
2318 : : /* [dcl.align]/3:
2319 : :
2320 : : When the alignment-specifier is of the form
2321 : : alignas(type-id), it shall have the same effect as
2322 : : alignas(alignof(type-id)). */
2323 : :
2324 : 122808 : return cxx_sizeof_or_alignof_type (input_location,
2325 : : e, ALIGNOF_EXPR,
2326 : : /*std_alignof=*/true,
2327 : 122808 : /*complain=*/true);
2328 : :
2329 : : /* If we reach this point, it means the alignas expression if of
2330 : : the form "alignas(assignment-expression)", so we should follow
2331 : : what is stated by [dcl.align]/2. */
2332 : :
2333 : 77328 : if (value_dependent_expression_p (e))
2334 : : /* Leave value-dependent expression alone for now. */
2335 : : return e;
2336 : :
2337 : 19788 : e = instantiate_non_dependent_expr (e);
2338 : 19788 : e = mark_rvalue_use (e);
2339 : :
2340 : : /* [dcl.align]/2 says:
2341 : :
2342 : : the assignment-expression shall be an integral constant
2343 : : expression. */
2344 : :
2345 : 19788 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2346 : : {
2347 : 18 : error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2348 : 18 : return error_mark_node;
2349 : : }
2350 : :
2351 : 19770 : return cxx_constant_value (e);
2352 : : }
2353 : :
2354 : :
2355 : : /* EXPR is being used in a context that is not a function call.
2356 : : Enforce:
2357 : :
2358 : : [expr.ref]
2359 : :
2360 : : The expression can be used only as the left-hand operand of a
2361 : : member function call.
2362 : :
2363 : : [expr.mptr.operator]
2364 : :
2365 : : If the result of .* or ->* is a function, then that result can be
2366 : : used only as the operand for the function call operator ().
2367 : :
2368 : : by issuing an error message if appropriate. Returns true iff EXPR
2369 : : violates these rules. */
2370 : :
2371 : : bool
2372 : 1231555927 : invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2373 : : {
2374 : 1231555927 : if (expr == NULL_TREE)
2375 : : return false;
2376 : : /* Don't enforce this in MS mode. */
2377 : 1231554484 : if (flag_ms_extensions)
2378 : : return false;
2379 : 1231546423 : if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2380 : 101848168 : expr = get_first_fn (expr);
2381 : 1231546423 : if (TREE_TYPE (expr)
2382 : 1231546423 : && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2383 : : {
2384 : 91 : if (complain & tf_error)
2385 : : {
2386 : 87 : if (DECL_P (expr))
2387 : : {
2388 : 69 : auto_diagnostic_group d;
2389 : 69 : error_at (loc, "invalid use of non-static member function %qD",
2390 : : expr);
2391 : 69 : inform (DECL_SOURCE_LOCATION (expr), "declared here");
2392 : 69 : }
2393 : : else
2394 : 18 : error_at (loc, "invalid use of non-static member function of "
2395 : 18 : "type %qT", TREE_TYPE (expr));
2396 : : }
2397 : 91 : return true;
2398 : : }
2399 : : return false;
2400 : : }
2401 : :
2402 : : /* If EXP is a reference to a bit-field, and the type of EXP does not
2403 : : match the declared type of the bit-field, return the declared type
2404 : : of the bit-field. Otherwise, return NULL_TREE. */
2405 : :
2406 : : tree
2407 : 2864639943 : is_bitfield_expr_with_lowered_type (const_tree exp)
2408 : : {
2409 : 3949395651 : switch (TREE_CODE (exp))
2410 : : {
2411 : 5791926 : case COND_EXPR:
2412 : 11583852 : if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2413 : 5791900 : ? TREE_OPERAND (exp, 1)
2414 : 26 : : TREE_OPERAND (exp, 0)))
2415 : : return NULL_TREE;
2416 : 135 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2417 : :
2418 : 904311 : case COMPOUND_EXPR:
2419 : 904311 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2420 : :
2421 : 353479290 : case MODIFY_EXPR:
2422 : 353479290 : case SAVE_EXPR:
2423 : 353479290 : case UNARY_PLUS_EXPR:
2424 : 353479290 : case PREDECREMENT_EXPR:
2425 : 353479290 : case PREINCREMENT_EXPR:
2426 : 353479290 : case POSTDECREMENT_EXPR:
2427 : 353479290 : case POSTINCREMENT_EXPR:
2428 : 353479290 : case NEGATE_EXPR:
2429 : 353479290 : case NON_LVALUE_EXPR:
2430 : 353479290 : case BIT_NOT_EXPR:
2431 : 353479290 : case CLEANUP_POINT_EXPR:
2432 : 353479290 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2433 : :
2434 : 241955550 : case COMPONENT_REF:
2435 : 241955550 : {
2436 : 241955550 : tree field;
2437 : :
2438 : 241955550 : field = TREE_OPERAND (exp, 1);
2439 : 241955550 : if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2440 : : return NULL_TREE;
2441 : 40821818 : if (same_type_ignoring_top_level_qualifiers_p
2442 : 40821818 : (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2443 : : return NULL_TREE;
2444 : 40670909 : return DECL_BIT_FIELD_TYPE (field);
2445 : : }
2446 : :
2447 : 538261902 : case VAR_DECL:
2448 : 538261902 : if (DECL_HAS_VALUE_EXPR_P (exp))
2449 : 1986268 : return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2450 : 1986268 : (CONST_CAST_TREE (exp)));
2451 : : return NULL_TREE;
2452 : :
2453 : 735838991 : case VIEW_CONVERT_EXPR:
2454 : 735838991 : if (location_wrapper_p (exp))
2455 : 728385704 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2456 : : else
2457 : : return NULL_TREE;
2458 : :
2459 : : default:
2460 : : return NULL_TREE;
2461 : : }
2462 : : }
2463 : :
2464 : : /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2465 : : bitfield with a lowered type, the type of EXP is returned, rather
2466 : : than NULL_TREE. */
2467 : :
2468 : : tree
2469 : 1338641620 : unlowered_expr_type (const_tree exp)
2470 : : {
2471 : 1338641620 : tree type;
2472 : 1338641620 : tree etype = TREE_TYPE (exp);
2473 : :
2474 : 1338641620 : type = is_bitfield_expr_with_lowered_type (exp);
2475 : 1338641620 : if (type)
2476 : 34416991 : type = cp_build_qualified_type (type, cp_type_quals (etype));
2477 : : else
2478 : : type = etype;
2479 : :
2480 : 1338641620 : return type;
2481 : : }
2482 : :
2483 : : /* Perform the conversions in [expr] that apply when an lvalue appears
2484 : : in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2485 : : function-to-pointer conversions. In addition, bitfield references are
2486 : : converted to their declared types. Note that this function does not perform
2487 : : the lvalue-to-rvalue conversion for class types. If you need that conversion
2488 : : for class types, then you probably need to use force_rvalue.
2489 : :
2490 : : Although the returned value is being used as an rvalue, this
2491 : : function does not wrap the returned expression in a
2492 : : NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2493 : : that the return value is no longer an lvalue. */
2494 : :
2495 : : tree
2496 : 984770879 : decay_conversion (tree exp,
2497 : : tsubst_flags_t complain,
2498 : : bool reject_builtin /* = true */)
2499 : : {
2500 : 984770879 : tree type;
2501 : 984770879 : enum tree_code code;
2502 : 984770879 : location_t loc = cp_expr_loc_or_input_loc (exp);
2503 : :
2504 : 984770879 : type = TREE_TYPE (exp);
2505 : 984770879 : if (type == error_mark_node)
2506 : : return error_mark_node;
2507 : :
2508 : 984770669 : exp = resolve_nondeduced_context_or_error (exp, complain);
2509 : :
2510 : 984770669 : code = TREE_CODE (type);
2511 : :
2512 : 984770669 : if (error_operand_p (exp))
2513 : 90 : return error_mark_node;
2514 : :
2515 : 984770579 : if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2516 : : {
2517 : 1496556 : mark_rvalue_use (exp, loc, reject_builtin);
2518 : 1496556 : return nullptr_node;
2519 : : }
2520 : :
2521 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2522 : : Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2523 : 983274023 : if (code == VOID_TYPE)
2524 : : {
2525 : 5787 : if (complain & tf_error)
2526 : 3 : error_at (loc, "void value not ignored as it ought to be");
2527 : 5787 : return error_mark_node;
2528 : : }
2529 : 983268236 : if (invalid_nonstatic_memfn_p (loc, exp, complain))
2530 : 6 : return error_mark_node;
2531 : 983268230 : if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2532 : : {
2533 : 103922938 : exp = mark_lvalue_use (exp);
2534 : 103922938 : if (reject_builtin && reject_gcc_builtin (exp, loc))
2535 : 110 : return error_mark_node;
2536 : 103922828 : return cp_build_addr_expr (exp, complain);
2537 : : }
2538 : 879345292 : if (code == ARRAY_TYPE)
2539 : : {
2540 : 5450766 : tree adr;
2541 : 5450766 : tree ptrtype;
2542 : :
2543 : 5450766 : exp = mark_lvalue_use (exp);
2544 : :
2545 : 5450766 : if (INDIRECT_REF_P (exp))
2546 : 95155 : return build_nop (build_pointer_type (TREE_TYPE (type)),
2547 : 190310 : TREE_OPERAND (exp, 0));
2548 : :
2549 : 5355611 : if (TREE_CODE (exp) == COMPOUND_EXPR)
2550 : : {
2551 : 3 : tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2552 : 3 : if (op1 == error_mark_node)
2553 : : return error_mark_node;
2554 : 3 : return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2555 : 6 : TREE_OPERAND (exp, 0), op1);
2556 : : }
2557 : :
2558 : 5355608 : if (!obvalue_p (exp)
2559 : 5355608 : && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2560 : : {
2561 : 0 : if (complain & tf_error)
2562 : 0 : error_at (loc, "invalid use of non-lvalue array");
2563 : 0 : return error_mark_node;
2564 : : }
2565 : :
2566 : 5355608 : ptrtype = build_pointer_type (TREE_TYPE (type));
2567 : :
2568 : 5355608 : if (VAR_P (exp))
2569 : : {
2570 : 529764 : if (!cxx_mark_addressable (exp))
2571 : 0 : return error_mark_node;
2572 : 529764 : adr = build_nop (ptrtype, build_address (exp));
2573 : 529764 : return adr;
2574 : : }
2575 : : /* This way is better for a COMPONENT_REF since it can
2576 : : simplify the offset for a component. */
2577 : 4825844 : adr = cp_build_addr_expr (exp, complain);
2578 : 4825844 : return cp_convert (ptrtype, adr, complain);
2579 : : }
2580 : :
2581 : : /* Otherwise, it's the lvalue-to-rvalue conversion. */
2582 : 873894526 : exp = mark_rvalue_use (exp, loc, reject_builtin);
2583 : :
2584 : : /* If a bitfield is used in a context where integral promotion
2585 : : applies, then the caller is expected to have used
2586 : : default_conversion. That function promotes bitfields correctly
2587 : : before calling this function. At this point, if we have a
2588 : : bitfield referenced, we may assume that is not subject to
2589 : : promotion, and that, therefore, the type of the resulting rvalue
2590 : : is the declared type of the bitfield. */
2591 : 873894526 : exp = convert_bitfield_to_declared_type (exp);
2592 : :
2593 : : /* We do not call rvalue() here because we do not want to wrap EXP
2594 : : in a NON_LVALUE_EXPR. */
2595 : :
2596 : : /* [basic.lval]
2597 : :
2598 : : Non-class rvalues always have cv-unqualified types. */
2599 : 873894526 : type = TREE_TYPE (exp);
2600 : 873894526 : if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2601 : 267820171 : exp = build_nop (cv_unqualified (type), exp);
2602 : :
2603 : 873894526 : if (!complete_type_or_maybe_complain (type, exp, complain))
2604 : 42 : return error_mark_node;
2605 : :
2606 : : return exp;
2607 : : }
2608 : :
2609 : : /* Perform preparatory conversions, as part of the "usual arithmetic
2610 : : conversions". In particular, as per [expr]:
2611 : :
2612 : : Whenever an lvalue expression appears as an operand of an
2613 : : operator that expects the rvalue for that operand, the
2614 : : lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2615 : : standard conversions are applied to convert the expression to an
2616 : : rvalue.
2617 : :
2618 : : In addition, we perform integral promotions here, as those are
2619 : : applied to both operands to a binary operator before determining
2620 : : what additional conversions should apply. */
2621 : :
2622 : : static tree
2623 : 270091099 : cp_default_conversion (tree exp, tsubst_flags_t complain)
2624 : : {
2625 : : /* Check for target-specific promotions. */
2626 : 270091099 : tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2627 : 270091099 : if (promoted_type)
2628 : 0 : exp = cp_convert (promoted_type, exp, complain);
2629 : : /* Perform the integral promotions first so that bitfield
2630 : : expressions (which may promote to "int", even if the bitfield is
2631 : : declared "unsigned") are promoted correctly. */
2632 : 270091099 : else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2633 : 179499588 : exp = cp_perform_integral_promotions (exp, complain);
2634 : : /* Perform the other conversions. */
2635 : 270091099 : exp = decay_conversion (exp, complain);
2636 : :
2637 : 270091099 : return exp;
2638 : : }
2639 : :
2640 : : /* C version. */
2641 : :
2642 : : tree
2643 : 40313758 : default_conversion (tree exp)
2644 : : {
2645 : 40313758 : return cp_default_conversion (exp, tf_warning_or_error);
2646 : : }
2647 : :
2648 : : /* EXPR is an expression with an integral or enumeration type.
2649 : : Perform the integral promotions in [conv.prom], and return the
2650 : : converted value. */
2651 : :
2652 : : tree
2653 : 185757926 : cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2654 : : {
2655 : 185757926 : tree type;
2656 : 185757926 : tree promoted_type;
2657 : :
2658 : 185757926 : expr = mark_rvalue_use (expr);
2659 : 185757926 : if (error_operand_p (expr))
2660 : 6 : return error_mark_node;
2661 : :
2662 : 185757920 : type = TREE_TYPE (expr);
2663 : :
2664 : : /* [conv.prom]
2665 : :
2666 : : A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2667 : : of type int if int can represent all the values of the bit-field;
2668 : : otherwise, it can be converted to unsigned int if unsigned int can
2669 : : represent all the values of the bit-field. If the bit-field is larger yet,
2670 : : no integral promotion applies to it. If the bit-field has an enumerated
2671 : : type, it is treated as any other value of that type for promotion
2672 : : purposes. */
2673 : 185757920 : tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2674 : 185757920 : if (bitfield_type
2675 : 185757920 : && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2676 : 324889 : || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2677 : : type = bitfield_type;
2678 : :
2679 : 185757920 : gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2680 : : /* Scoped enums don't promote. */
2681 : 185757920 : if (SCOPED_ENUM_P (type))
2682 : : return expr;
2683 : 184642503 : promoted_type = type_promotes_to (type);
2684 : 184642503 : if (type != promoted_type)
2685 : 51130965 : expr = cp_convert (promoted_type, expr, complain);
2686 : 133511538 : else if (bitfield_type && bitfield_type != type)
2687 : : /* Prevent decay_conversion from converting to bitfield_type. */
2688 : 159 : expr = build_nop (type, expr);
2689 : : return expr;
2690 : : }
2691 : :
2692 : : /* C version. */
2693 : :
2694 : : tree
2695 : 2063093 : perform_integral_promotions (tree expr)
2696 : : {
2697 : 2063093 : return cp_perform_integral_promotions (expr, tf_warning_or_error);
2698 : : }
2699 : :
2700 : : /* Returns nonzero iff exp is a STRING_CST or the result of applying
2701 : : decay_conversion to one. */
2702 : :
2703 : : int
2704 : 3230215 : string_conv_p (const_tree totype, const_tree exp, int warn)
2705 : : {
2706 : 3230215 : tree t;
2707 : :
2708 : 3230215 : if (!TYPE_PTR_P (totype))
2709 : : return 0;
2710 : :
2711 : 3230164 : t = TREE_TYPE (totype);
2712 : 3230164 : if (!same_type_p (t, char_type_node)
2713 : 3095901 : && !same_type_p (t, char8_type_node)
2714 : 3078702 : && !same_type_p (t, char16_type_node)
2715 : 3029220 : && !same_type_p (t, char32_type_node)
2716 : 6209903 : && !same_type_p (t, wchar_type_node))
2717 : : return 0;
2718 : :
2719 : 280446 : location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2720 : :
2721 : 280446 : STRIP_ANY_LOCATION_WRAPPER (exp);
2722 : :
2723 : 280446 : if (TREE_CODE (exp) == STRING_CST)
2724 : : {
2725 : : /* Make sure that we don't try to convert between char and wide chars. */
2726 : 108 : if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2727 : : return 0;
2728 : : }
2729 : : else
2730 : : {
2731 : : /* Is this a string constant which has decayed to 'const char *'? */
2732 : 280338 : t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2733 : 280338 : if (!same_type_p (TREE_TYPE (exp), t))
2734 : : return 0;
2735 : 13686 : STRIP_NOPS (exp);
2736 : 13686 : if (TREE_CODE (exp) != ADDR_EXPR
2737 : 13686 : || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2738 : : return 0;
2739 : : }
2740 : 306 : if (warn)
2741 : : {
2742 : 102 : if (cxx_dialect >= cxx11)
2743 : 87 : pedwarn (loc, OPT_Wwrite_strings,
2744 : : "ISO C++ forbids converting a string constant to %qT",
2745 : : totype);
2746 : : else
2747 : 15 : warning_at (loc, OPT_Wwrite_strings,
2748 : : "deprecated conversion from string constant to %qT",
2749 : : totype);
2750 : : }
2751 : :
2752 : : return 1;
2753 : : }
2754 : :
2755 : : /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2756 : : can, for example, use as an lvalue. This code used to be in
2757 : : unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2758 : : expressions, where we're dealing with aggregates. But now it's again only
2759 : : called from unary_complex_lvalue. The case (in particular) that led to
2760 : : this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2761 : : get it there. */
2762 : :
2763 : : static tree
2764 : 102822 : rationalize_conditional_expr (enum tree_code code, tree t,
2765 : : tsubst_flags_t complain)
2766 : : {
2767 : 102822 : location_t loc = cp_expr_loc_or_input_loc (t);
2768 : :
2769 : : /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2770 : : the first operand is always the one to be used if both operands
2771 : : are equal, so we know what conditional expression this used to be. */
2772 : 102822 : if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2773 : : {
2774 : 0 : tree op0 = TREE_OPERAND (t, 0);
2775 : 0 : tree op1 = TREE_OPERAND (t, 1);
2776 : :
2777 : : /* The following code is incorrect if either operand side-effects. */
2778 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (op0)
2779 : : && !TREE_SIDE_EFFECTS (op1));
2780 : 0 : return
2781 : 0 : build_conditional_expr (loc,
2782 : 0 : build_x_binary_op (loc,
2783 : 0 : (TREE_CODE (t) == MIN_EXPR
2784 : : ? LE_EXPR : GE_EXPR),
2785 : 0 : op0, TREE_CODE (op0),
2786 : 0 : op1, TREE_CODE (op1),
2787 : : NULL_TREE,
2788 : : /*overload=*/NULL,
2789 : : complain),
2790 : : cp_build_unary_op (code, op0, false, complain),
2791 : : cp_build_unary_op (code, op1, false, complain),
2792 : : complain);
2793 : : }
2794 : :
2795 : 102822 : tree op1 = TREE_OPERAND (t, 1);
2796 : 102822 : if (TREE_CODE (op1) != THROW_EXPR)
2797 : 102819 : op1 = cp_build_unary_op (code, op1, false, complain);
2798 : 102822 : tree op2 = TREE_OPERAND (t, 2);
2799 : 102822 : if (TREE_CODE (op2) != THROW_EXPR)
2800 : 102816 : op2 = cp_build_unary_op (code, op2, false, complain);
2801 : :
2802 : 102822 : return
2803 : 102822 : build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2804 : : }
2805 : :
2806 : : /* Given the TYPE of an anonymous union field inside T, return the
2807 : : FIELD_DECL for the field. If not found return NULL_TREE. Because
2808 : : anonymous unions can nest, we must also search all anonymous unions
2809 : : that are directly reachable. */
2810 : :
2811 : : tree
2812 : 1138833 : lookup_anon_field (tree, tree type)
2813 : : {
2814 : 1138833 : tree field;
2815 : :
2816 : 1138833 : type = TYPE_MAIN_VARIANT (type);
2817 : 1138833 : field = ANON_AGGR_TYPE_FIELD (type);
2818 : 1138833 : gcc_assert (field);
2819 : 1138833 : return field;
2820 : : }
2821 : :
2822 : : /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2823 : : expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2824 : : non-NULL, it indicates the path to the base used to name MEMBER.
2825 : : If PRESERVE_REFERENCE is true, the expression returned will have
2826 : : REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2827 : : returned will have the type referred to by the reference.
2828 : :
2829 : : This function does not perform access control; that is either done
2830 : : earlier by the parser when the name of MEMBER is resolved to MEMBER
2831 : : itself, or later when overload resolution selects one of the
2832 : : functions indicated by MEMBER. */
2833 : :
2834 : : tree
2835 : 106501483 : build_class_member_access_expr (cp_expr object, tree member,
2836 : : tree access_path, bool preserve_reference,
2837 : : tsubst_flags_t complain)
2838 : : {
2839 : 106501483 : tree object_type;
2840 : 106501483 : tree member_scope;
2841 : 106501483 : tree result = NULL_TREE;
2842 : 106501483 : tree using_decl = NULL_TREE;
2843 : :
2844 : 106501483 : if (error_operand_p (object) || error_operand_p (member))
2845 : 57 : return error_mark_node;
2846 : :
2847 : 106501426 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2848 : :
2849 : : /* [expr.ref]
2850 : :
2851 : : The type of the first expression shall be "class object" (of a
2852 : : complete type). */
2853 : 106501426 : object_type = TREE_TYPE (object);
2854 : 106501426 : if (!currently_open_class (object_type)
2855 : 106501426 : && !complete_type_or_maybe_complain (object_type, object, complain))
2856 : 0 : return error_mark_node;
2857 : 106501426 : if (!CLASS_TYPE_P (object_type))
2858 : : {
2859 : 0 : if (complain & tf_error)
2860 : : {
2861 : 0 : if (INDIRECT_TYPE_P (object_type)
2862 : 0 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
2863 : 0 : error ("request for member %qD in %qE, which is of pointer "
2864 : : "type %qT (maybe you meant to use %<->%> ?)",
2865 : : member, object.get_value (), object_type);
2866 : : else
2867 : 0 : error ("request for member %qD in %qE, which is of non-class "
2868 : : "type %qT", member, object.get_value (), object_type);
2869 : : }
2870 : 0 : return error_mark_node;
2871 : : }
2872 : :
2873 : : /* The standard does not seem to actually say that MEMBER must be a
2874 : : member of OBJECT_TYPE. However, that is clearly what is
2875 : : intended. */
2876 : 106501426 : if (DECL_P (member))
2877 : : {
2878 : 45316634 : member_scope = DECL_CLASS_CONTEXT (member);
2879 : 45316634 : if (!mark_used (member, complain) && !(complain & tf_error))
2880 : 0 : return error_mark_node;
2881 : :
2882 : 45316634 : if (TREE_UNAVAILABLE (member))
2883 : 30 : error_unavailable_use (member, NULL_TREE);
2884 : 45316604 : else if (TREE_DEPRECATED (member))
2885 : 30 : warn_deprecated_use (member, NULL_TREE);
2886 : : }
2887 : : else
2888 : 61184792 : member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2889 : : /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2890 : : presently be the anonymous union. Go outwards until we find a
2891 : : type related to OBJECT_TYPE. */
2892 : 107644097 : while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2893 : 108787179 : && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2894 : : object_type))
2895 : 1142671 : member_scope = TYPE_CONTEXT (member_scope);
2896 : 106501426 : if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2897 : : {
2898 : 3 : if (complain & tf_error)
2899 : : {
2900 : 3 : if (TREE_CODE (member) == FIELD_DECL)
2901 : 0 : error ("invalid use of non-static data member %qE", member);
2902 : : else
2903 : 3 : error ("%qD is not a member of %qT", member, object_type);
2904 : : }
2905 : 3 : return error_mark_node;
2906 : : }
2907 : :
2908 : : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2909 : : `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2910 : : in the front end; only _DECLs and _REFs are lvalues in the back end. */
2911 : 106501423 : if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2912 : : {
2913 : 50 : temp = cp_build_fold_indirect_ref (temp);
2914 : 50 : if (!lvalue_p (object) && lvalue_p (temp))
2915 : : /* Preserve rvalueness. */
2916 : 6 : temp = move (temp);
2917 : 50 : object = temp;
2918 : : }
2919 : :
2920 : : /* In [expr.ref], there is an explicit list of the valid choices for
2921 : : MEMBER. We check for each of those cases here. */
2922 : 106501423 : if (VAR_P (member))
2923 : : {
2924 : : /* A static data member. */
2925 : 98472 : result = member;
2926 : 98472 : mark_exp_read (object);
2927 : :
2928 : 98472 : if (tree wrap = maybe_get_tls_wrapper_call (result))
2929 : : /* Replace an evaluated use of the thread_local variable with
2930 : : a call to its wrapper. */
2931 : 102 : result = wrap;
2932 : :
2933 : : /* If OBJECT has side-effects, they are supposed to occur. */
2934 : 98472 : if (TREE_SIDE_EFFECTS (object))
2935 : 57 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2936 : : }
2937 : 106402951 : else if (TREE_CODE (member) == FIELD_DECL)
2938 : : {
2939 : : /* A non-static data member. */
2940 : 45217963 : bool null_object_p;
2941 : 45217963 : int type_quals;
2942 : 45217963 : tree member_type;
2943 : :
2944 : 45217963 : if (INDIRECT_REF_P (object))
2945 : 34502369 : null_object_p =
2946 : 34502369 : integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2947 : : else
2948 : : null_object_p = false;
2949 : :
2950 : : /* Convert OBJECT to the type of MEMBER. */
2951 : 45217963 : if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2952 : : TYPE_MAIN_VARIANT (member_scope)))
2953 : : {
2954 : 1870722 : tree binfo;
2955 : 1870722 : base_kind kind;
2956 : :
2957 : : /* We didn't complain above about a currently open class, but now we
2958 : : must: we don't know how to refer to a base member before layout is
2959 : : complete. But still don't complain in a template. */
2960 : 1870722 : if (!cp_unevaluated_operand
2961 : 1870199 : && !dependent_type_p (object_type)
2962 : 3435327 : && !complete_type_or_maybe_complain (object_type, object,
2963 : : complain))
2964 : 9 : return error_mark_node;
2965 : :
2966 : 2333022 : binfo = lookup_base (access_path ? access_path : object_type,
2967 : : member_scope, ba_unique, &kind, complain);
2968 : 1870719 : if (binfo == error_mark_node)
2969 : : return error_mark_node;
2970 : :
2971 : : /* It is invalid to try to get to a virtual base of a
2972 : : NULL object. The most common cause is invalid use of
2973 : : offsetof macro. */
2974 : 1870719 : if (null_object_p && kind == bk_via_virtual)
2975 : : {
2976 : 6 : if (complain & tf_error)
2977 : : {
2978 : 6 : error ("invalid access to non-static data member %qD in "
2979 : : "virtual base of NULL object", member);
2980 : : }
2981 : 6 : return error_mark_node;
2982 : : }
2983 : :
2984 : : /* Convert to the base. */
2985 : 1870713 : object = build_base_path (PLUS_EXPR, object, binfo,
2986 : : /*nonnull=*/1, complain);
2987 : : /* If we found the base successfully then we should be able
2988 : : to convert to it successfully. */
2989 : 1870713 : gcc_assert (object != error_mark_node || seen_error ());
2990 : : }
2991 : :
2992 : : /* If MEMBER is from an anonymous aggregate, we have converted
2993 : : OBJECT so that it refers to the class containing the
2994 : : anonymous union. Generate a reference to the anonymous union
2995 : : itself, and recur to find MEMBER. */
2996 : 90435908 : if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2997 : : /* When this code is called from build_field_call, the
2998 : : object already has the type of the anonymous union.
2999 : : That is because the COMPONENT_REF was already
3000 : : constructed, and was then disassembled before calling
3001 : : build_field_call. After the function-call code is
3002 : : cleaned up, this waste can be eliminated. */
3003 : 46357066 : && (!same_type_ignoring_top_level_qualifiers_p
3004 : 1139112 : (TREE_TYPE (object), DECL_CONTEXT (member))))
3005 : : {
3006 : 1138707 : tree anonymous_union;
3007 : :
3008 : 1138707 : anonymous_union = lookup_anon_field (TREE_TYPE (object),
3009 : 1138707 : DECL_CONTEXT (member));
3010 : 1138707 : object = build_class_member_access_expr (object,
3011 : : anonymous_union,
3012 : : /*access_path=*/NULL_TREE,
3013 : : preserve_reference,
3014 : : complain);
3015 : : }
3016 : :
3017 : : /* Compute the type of the field, as described in [expr.ref]. */
3018 : 45217954 : type_quals = TYPE_UNQUALIFIED;
3019 : 45217954 : member_type = TREE_TYPE (member);
3020 : 45217954 : if (!TYPE_REF_P (member_type))
3021 : : {
3022 : 43964443 : type_quals = (cp_type_quals (member_type)
3023 : 43964443 : | cp_type_quals (object_type));
3024 : :
3025 : : /* A field is const (volatile) if the enclosing object, or the
3026 : : field itself, is const (volatile). But, a mutable field is
3027 : : not const, even within a const object. */
3028 : 43964443 : if (DECL_MUTABLE_P (member))
3029 : 332362 : type_quals &= ~TYPE_QUAL_CONST;
3030 : 43964443 : member_type = cp_build_qualified_type (member_type, type_quals);
3031 : : }
3032 : :
3033 : 45217954 : result = build3_loc (input_location, COMPONENT_REF, member_type,
3034 : : object, member, NULL_TREE);
3035 : :
3036 : : /* Mark the expression const or volatile, as appropriate. Even
3037 : : though we've dealt with the type above, we still have to mark the
3038 : : expression itself. */
3039 : 45217954 : if (type_quals & TYPE_QUAL_CONST)
3040 : 14304703 : TREE_READONLY (result) = 1;
3041 : 45217954 : if (type_quals & TYPE_QUAL_VOLATILE)
3042 : 186733 : TREE_THIS_VOLATILE (result) = 1;
3043 : : }
3044 : 61184988 : else if (BASELINK_P (member))
3045 : : {
3046 : : /* The member is a (possibly overloaded) member function. */
3047 : 61184789 : tree functions;
3048 : 61184789 : tree type;
3049 : :
3050 : : /* If the MEMBER is exactly one static member function, then we
3051 : : know the type of the expression. Otherwise, we must wait
3052 : : until overload resolution has been performed. */
3053 : 61184789 : functions = BASELINK_FUNCTIONS (member);
3054 : 61184789 : if (TREE_CODE (functions) == OVERLOAD && OVL_SINGLE_P (functions))
3055 : 61184789 : functions = OVL_FIRST (functions);
3056 : 61184789 : if (TREE_CODE (functions) == FUNCTION_DECL
3057 : 61184789 : && DECL_STATIC_FUNCTION_P (functions))
3058 : 474860 : type = TREE_TYPE (functions);
3059 : : else
3060 : 60709929 : type = unknown_type_node;
3061 : : /* Note that we do not convert OBJECT to the BASELINK_BINFO
3062 : : base. That will happen when the function is called. */
3063 : 61184789 : result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3064 : : NULL_TREE);
3065 : : }
3066 : 199 : else if (TREE_CODE (member) == CONST_DECL)
3067 : : {
3068 : : /* The member is an enumerator. */
3069 : 187 : result = member;
3070 : : /* If OBJECT has side-effects, they are supposed to occur. */
3071 : 187 : if (TREE_SIDE_EFFECTS (object))
3072 : 6 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3073 : : object, result);
3074 : : }
3075 : 12 : else if ((using_decl = strip_using_decl (member)) != member)
3076 : 0 : result = build_class_member_access_expr (object,
3077 : : using_decl,
3078 : : access_path, preserve_reference,
3079 : : complain);
3080 : : else
3081 : : {
3082 : 12 : if (complain & tf_error)
3083 : 12 : error ("invalid use of %qD", member);
3084 : 12 : return error_mark_node;
3085 : : }
3086 : :
3087 : 106501402 : if (!preserve_reference)
3088 : : /* [expr.ref]
3089 : :
3090 : : If E2 is declared to have type "reference to T", then ... the
3091 : : type of E1.E2 is T. */
3092 : 100862737 : result = convert_from_reference (result);
3093 : :
3094 : : return result;
3095 : : }
3096 : :
3097 : : /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3098 : : SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3099 : :
3100 : : tree
3101 : 175287 : lookup_destructor (tree object, tree scope, tree dtor_name,
3102 : : tsubst_flags_t complain)
3103 : : {
3104 : 175287 : tree object_type = TREE_TYPE (object);
3105 : 175287 : tree dtor_type = TREE_OPERAND (dtor_name, 0);
3106 : 175287 : tree expr;
3107 : :
3108 : : /* We've already complained about this destructor. */
3109 : 175287 : if (dtor_type == error_mark_node)
3110 : : return error_mark_node;
3111 : :
3112 : 175281 : if (scope && !check_dtor_name (scope, dtor_type))
3113 : : {
3114 : 3 : if (complain & tf_error)
3115 : 3 : error ("qualified type %qT does not match destructor name ~%qT",
3116 : : scope, dtor_type);
3117 : 3 : return error_mark_node;
3118 : : }
3119 : 175278 : if (is_auto (dtor_type))
3120 : : dtor_type = object_type;
3121 : 175275 : else if (identifier_p (dtor_type))
3122 : : {
3123 : : /* In a template, names we can't find a match for are still accepted
3124 : : destructor names, and we check them here. */
3125 : 23 : if (check_dtor_name (object_type, dtor_type))
3126 : : dtor_type = object_type;
3127 : : else
3128 : : {
3129 : 3 : if (complain & tf_error)
3130 : 3 : error ("object type %qT does not match destructor name ~%qT",
3131 : : object_type, dtor_type);
3132 : 3 : return error_mark_node;
3133 : : }
3134 : :
3135 : : }
3136 : 175252 : else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3137 : : {
3138 : 6 : if (complain & tf_error)
3139 : 6 : error ("the type being destroyed is %qT, but the destructor "
3140 : 6 : "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3141 : 6 : return error_mark_node;
3142 : : }
3143 : 175269 : expr = lookup_member (dtor_type, complete_dtor_identifier,
3144 : : /*protect=*/1, /*want_type=*/false,
3145 : : tf_warning_or_error);
3146 : 175269 : if (!expr)
3147 : : {
3148 : 6 : if (complain & tf_error)
3149 : 6 : cxx_incomplete_type_error (dtor_name, dtor_type);
3150 : 6 : return error_mark_node;
3151 : : }
3152 : 175263 : expr = (adjust_result_of_qualified_name_lookup
3153 : 175263 : (expr, dtor_type, object_type));
3154 : 175263 : if (scope == NULL_TREE)
3155 : : /* We need to call adjust_result_of_qualified_name_lookup in case the
3156 : : destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3157 : : that we still get virtual function binding. */
3158 : 175120 : BASELINK_QUALIFIED_P (expr) = false;
3159 : : return expr;
3160 : : }
3161 : :
3162 : : /* An expression of the form "A::template B" has been resolved to
3163 : : DECL. Issue a diagnostic if B is not a template or template
3164 : : specialization. */
3165 : :
3166 : : void
3167 : 3124121 : check_template_keyword (tree decl)
3168 : : {
3169 : : /* The standard says:
3170 : :
3171 : : [temp.names]
3172 : :
3173 : : If a name prefixed by the keyword template is not a member
3174 : : template, the program is ill-formed.
3175 : :
3176 : : DR 228 removed the restriction that the template be a member
3177 : : template.
3178 : :
3179 : : DR 96, if accepted would add the further restriction that explicit
3180 : : template arguments must be provided if the template keyword is
3181 : : used, but, as of 2005-10-16, that DR is still in "drafting". If
3182 : : this DR is accepted, then the semantic checks here can be
3183 : : simplified, as the entity named must in fact be a template
3184 : : specialization, rather than, as at present, a set of overloaded
3185 : : functions containing at least one template function. */
3186 : 3124121 : if (TREE_CODE (decl) != TEMPLATE_DECL
3187 : 3124121 : && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3188 : : {
3189 : 2797852 : if (VAR_P (decl))
3190 : : {
3191 : 9781 : if (DECL_USE_TEMPLATE (decl)
3192 : 9781 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3193 : : ;
3194 : : else
3195 : 0 : permerror (input_location, "%qD is not a template", decl);
3196 : : }
3197 : 2788071 : else if (!is_overloaded_fn (decl))
3198 : 0 : permerror (input_location, "%qD is not a template", decl);
3199 : : else
3200 : : {
3201 : 2788071 : bool found = false;
3202 : :
3203 : 5576142 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3204 : 5576144 : !found && iter; ++iter)
3205 : : {
3206 : 2788073 : tree fn = *iter;
3207 : 2788073 : if (TREE_CODE (fn) == TEMPLATE_DECL
3208 : 2787677 : || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3209 : 2788087 : || (TREE_CODE (fn) == FUNCTION_DECL
3210 : 14 : && DECL_USE_TEMPLATE (fn)
3211 : 6 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3212 : : found = true;
3213 : : }
3214 : 2788071 : if (!found)
3215 : 12 : permerror (input_location, "%qD is not a template", decl);
3216 : : }
3217 : : }
3218 : 3124121 : }
3219 : :
3220 : : /* Record that an access failure occurred on BASETYPE_PATH attempting
3221 : : to access DECL, where DIAG_DECL should be used for diagnostics. */
3222 : :
3223 : : void
3224 : 412 : access_failure_info::record_access_failure (tree basetype_path,
3225 : : tree decl, tree diag_decl)
3226 : : {
3227 : 412 : m_was_inaccessible = true;
3228 : 412 : m_basetype_path = basetype_path;
3229 : 412 : m_decl = decl;
3230 : 412 : m_diag_decl = diag_decl;
3231 : 412 : }
3232 : :
3233 : : /* If an access failure was recorded, then attempt to locate an
3234 : : accessor function for the pertinent field.
3235 : : Otherwise, return NULL_TREE. */
3236 : :
3237 : : tree
3238 : 88438152 : access_failure_info::get_any_accessor (bool const_p) const
3239 : : {
3240 : 88438152 : if (!was_inaccessible_p ())
3241 : : return NULL_TREE;
3242 : :
3243 : 412 : tree accessor
3244 : 412 : = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3245 : 412 : if (!accessor)
3246 : : return NULL_TREE;
3247 : :
3248 : : /* The accessor must itself be accessible for it to be a reasonable
3249 : : suggestion. */
3250 : 161 : if (!accessible_p (m_basetype_path, accessor, true))
3251 : : return NULL_TREE;
3252 : :
3253 : : return accessor;
3254 : : }
3255 : :
3256 : : /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3257 : : replacing the primary location in RICHLOC with "accessor()". */
3258 : :
3259 : : void
3260 : 140 : access_failure_info::add_fixit_hint (rich_location *richloc,
3261 : : tree accessor_decl)
3262 : : {
3263 : 140 : pretty_printer pp;
3264 : 140 : pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3265 : 140 : pp_string (&pp, "()");
3266 : 140 : richloc->add_fixit_replace (pp_formatted_text (&pp));
3267 : 140 : }
3268 : :
3269 : : /* If an access failure was recorded, then attempt to locate an
3270 : : accessor function for the pertinent field, and if one is
3271 : : available, add a note and fix-it hint suggesting using it. */
3272 : :
3273 : : void
3274 : 88438082 : access_failure_info::maybe_suggest_accessor (bool const_p) const
3275 : : {
3276 : 88438082 : tree accessor = get_any_accessor (const_p);
3277 : 88438082 : if (accessor == NULL_TREE)
3278 : 88437984 : return;
3279 : 98 : rich_location richloc (line_table, input_location);
3280 : 98 : add_fixit_hint (&richloc, accessor);
3281 : 98 : inform (&richloc, "field %q#D can be accessed via %q#D",
3282 : 98 : m_diag_decl, accessor);
3283 : 98 : }
3284 : :
3285 : : /* Subroutine of finish_class_member_access_expr.
3286 : : Issue an error about NAME not being a member of ACCESS_PATH (or
3287 : : OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3288 : : names. */
3289 : :
3290 : : static void
3291 : 308 : complain_about_unrecognized_member (tree access_path, tree name,
3292 : : tree object_type)
3293 : : {
3294 : : /* Attempt to provide a hint about misspelled names. */
3295 : 308 : tree guessed_id = lookup_member_fuzzy (access_path, name,
3296 : : /*want_type=*/false);
3297 : 308 : if (guessed_id == NULL_TREE)
3298 : : {
3299 : : /* No hint. */
3300 : 201 : error ("%q#T has no member named %qE",
3301 : 201 : TREE_CODE (access_path) == TREE_BINFO
3302 : 6 : ? TREE_TYPE (access_path) : object_type, name);
3303 : 201 : return;
3304 : : }
3305 : :
3306 : 107 : location_t bogus_component_loc = input_location;
3307 : 107 : gcc_rich_location rich_loc (bogus_component_loc);
3308 : :
3309 : : /* Check that the guessed name is accessible along access_path. */
3310 : 107 : access_failure_info afi;
3311 : 107 : lookup_member (access_path, guessed_id, /*protect=*/1,
3312 : : /*want_type=*/false, /*complain=*/false,
3313 : : &afi);
3314 : 107 : if (afi.was_inaccessible_p ())
3315 : : {
3316 : 70 : tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3317 : 70 : if (accessor)
3318 : : {
3319 : : /* The guessed name isn't directly accessible, but can be accessed
3320 : : via an accessor member function. */
3321 : 42 : afi.add_fixit_hint (&rich_loc, accessor);
3322 : 42 : error_at (&rich_loc,
3323 : : "%q#T has no member named %qE;"
3324 : : " did you mean %q#D? (accessible via %q#D)",
3325 : 42 : TREE_CODE (access_path) == TREE_BINFO
3326 : 0 : ? TREE_TYPE (access_path) : object_type,
3327 : : name, afi.get_diag_decl (), accessor);
3328 : : }
3329 : : else
3330 : : {
3331 : : /* The guessed name isn't directly accessible, and no accessor
3332 : : member function could be found. */
3333 : 28 : auto_diagnostic_group d;
3334 : 28 : error_at (&rich_loc,
3335 : : "%q#T has no member named %qE;"
3336 : : " did you mean %q#D? (not accessible from this context)",
3337 : 28 : TREE_CODE (access_path) == TREE_BINFO
3338 : 0 : ? TREE_TYPE (access_path) : object_type,
3339 : : name, afi.get_diag_decl ());
3340 : 28 : complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3341 : : afi.get_diag_decl (), false, ak_none);
3342 : 28 : }
3343 : : }
3344 : : else
3345 : : {
3346 : : /* The guessed name is directly accessible; suggest it. */
3347 : 37 : rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3348 : : guessed_id);
3349 : 37 : error_at (&rich_loc,
3350 : : "%q#T has no member named %qE;"
3351 : : " did you mean %qE?",
3352 : 37 : TREE_CODE (access_path) == TREE_BINFO
3353 : 0 : ? TREE_TYPE (access_path) : object_type,
3354 : : name, guessed_id);
3355 : : }
3356 : 107 : }
3357 : :
3358 : : /* This function is called by the parser to process a class member
3359 : : access expression of the form OBJECT.NAME. NAME is a node used by
3360 : : the parser to represent a name; it is not yet a DECL. It may,
3361 : : however, be a BASELINK where the BASELINK_FUNCTIONS is a
3362 : : TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3363 : : there is no reason to do the lookup twice, so the parser keeps the
3364 : : BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3365 : : be a template via the use of the "A::template B" syntax. */
3366 : :
3367 : : tree
3368 : 173066669 : finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3369 : : tsubst_flags_t complain)
3370 : : {
3371 : 173066669 : tree expr;
3372 : 173066669 : tree object_type;
3373 : 173066669 : tree member;
3374 : 173066669 : tree access_path = NULL_TREE;
3375 : 173066669 : tree orig_object = object;
3376 : 173066669 : tree orig_name = name;
3377 : :
3378 : 173066669 : if (object == error_mark_node || name == error_mark_node)
3379 : : return error_mark_node;
3380 : :
3381 : : /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3382 : 173066208 : if (!objc_is_public (object, name))
3383 : 0 : return error_mark_node;
3384 : :
3385 : 173066208 : object_type = TREE_TYPE (object);
3386 : :
3387 : 173066208 : if (processing_template_decl)
3388 : : {
3389 : 144037110 : if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3390 : 144037110 : type_dependent_object_expression_p (object)
3391 : : /* If NAME is "f<args>", where either 'f' or 'args' is
3392 : : dependent, then the expression is dependent. */
3393 : 69698570 : || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3394 : 636781 : && dependent_template_id_p (TREE_OPERAND (name, 0),
3395 : 636781 : TREE_OPERAND (name, 1)))
3396 : : /* If NAME is "T::X" where "T" is dependent, then the
3397 : : expression is dependent. */
3398 : 69374237 : || (TREE_CODE (name) == SCOPE_REF
3399 : 111256 : && TYPE_P (TREE_OPERAND (name, 0))
3400 : 111256 : && dependent_scope_p (TREE_OPERAND (name, 0)))
3401 : : /* If NAME is operator T where "T" is dependent, we can't
3402 : : lookup until we instantiate the T. */
3403 : 213300305 : || (TREE_CODE (name) == IDENTIFIER_NODE
3404 : 68588796 : && IDENTIFIER_CONV_OP_P (name)
3405 : 44 : && dependent_type_p (TREE_TYPE (name))))
3406 : : {
3407 : 88840109 : dependent:
3408 : 88840109 : return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3409 : 88840109 : orig_object, orig_name, NULL_TREE);
3410 : : }
3411 : : }
3412 : 29029098 : else if (c_dialect_objc ()
3413 : 98292255 : && identifier_p (name)
3414 : 29029098 : && (expr = objc_maybe_build_component_ref (object, name)))
3415 : : return expr;
3416 : :
3417 : : /* [expr.ref]
3418 : :
3419 : : The type of the first expression shall be "class object" (of a
3420 : : complete type). */
3421 : 98292255 : if (!currently_open_class (object_type)
3422 : 98292255 : && !complete_type_or_maybe_complain (object_type, object, complain))
3423 : 86 : return error_mark_node;
3424 : 98292169 : if (!CLASS_TYPE_P (object_type))
3425 : : {
3426 : 110287 : if (complain & tf_error)
3427 : : {
3428 : 117 : if (INDIRECT_TYPE_P (object_type)
3429 : 117 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
3430 : 21 : error ("request for member %qD in %qE, which is of pointer "
3431 : : "type %qT (maybe you meant to use %<->%> ?)",
3432 : : name, object.get_value (), object_type);
3433 : : else
3434 : 96 : error ("request for member %qD in %qE, which is of non-class "
3435 : : "type %qT", name, object.get_value (), object_type);
3436 : : }
3437 : 110287 : return error_mark_node;
3438 : : }
3439 : :
3440 : 98181882 : if (BASELINK_P (name))
3441 : : /* A member function that has already been looked up. */
3442 : : member = name;
3443 : : else
3444 : : {
3445 : 88648111 : bool is_template_id = false;
3446 : 88648111 : tree template_args = NULL_TREE;
3447 : 88648111 : tree scope = NULL_TREE;
3448 : :
3449 : 88648111 : access_path = object_type;
3450 : :
3451 : 88648111 : if (TREE_CODE (name) == SCOPE_REF)
3452 : : {
3453 : : /* A qualified name. The qualifying class or namespace `S'
3454 : : has already been looked up; it is either a TYPE or a
3455 : : NAMESPACE_DECL. */
3456 : 1786 : scope = TREE_OPERAND (name, 0);
3457 : 1786 : name = TREE_OPERAND (name, 1);
3458 : :
3459 : : /* If SCOPE is a namespace, then the qualified name does not
3460 : : name a member of OBJECT_TYPE. */
3461 : 1786 : if (TREE_CODE (scope) == NAMESPACE_DECL)
3462 : : {
3463 : 0 : if (complain & tf_error)
3464 : 0 : error ("%<%D::%D%> is not a member of %qT",
3465 : : scope, name, object_type);
3466 : 0 : return error_mark_node;
3467 : : }
3468 : : }
3469 : :
3470 : 88648111 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3471 : : {
3472 : 477299 : is_template_id = true;
3473 : 477299 : template_args = TREE_OPERAND (name, 1);
3474 : 477299 : name = TREE_OPERAND (name, 0);
3475 : :
3476 : 908940 : if (!identifier_p (name))
3477 : 431659 : name = OVL_NAME (name);
3478 : : }
3479 : :
3480 : 88648111 : if (scope)
3481 : : {
3482 : 1786 : if (TREE_CODE (scope) == ENUMERAL_TYPE)
3483 : : {
3484 : 12 : gcc_assert (!is_template_id);
3485 : : /* Looking up a member enumerator (c++/56793). */
3486 : 24 : if (!TYPE_CLASS_SCOPE_P (scope)
3487 : 21 : || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3488 : : {
3489 : 3 : if (complain & tf_error)
3490 : 3 : error ("%<%D::%D%> is not a member of %qT",
3491 : : scope, name, object_type);
3492 : 3 : return error_mark_node;
3493 : : }
3494 : 9 : tree val = lookup_enumerator (scope, name);
3495 : 9 : if (!val)
3496 : : {
3497 : 6 : if (complain & tf_error)
3498 : 6 : error ("%qD is not a member of %qD",
3499 : : name, scope);
3500 : 6 : return error_mark_node;
3501 : : }
3502 : :
3503 : 3 : if (TREE_SIDE_EFFECTS (object))
3504 : 0 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3505 : 3 : return val;
3506 : : }
3507 : :
3508 : 1774 : gcc_assert (CLASS_TYPE_P (scope));
3509 : 1774 : gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3510 : :
3511 : 1774 : if (constructor_name_p (name, scope))
3512 : : {
3513 : 3 : if (complain & tf_error)
3514 : 3 : error ("cannot call constructor %<%T::%D%> directly",
3515 : : scope, name);
3516 : 3 : return error_mark_node;
3517 : : }
3518 : :
3519 : : /* NAME may refer to a static data member, in which case there is
3520 : : one copy of the data member that is shared by all the objects of
3521 : : the class. So NAME can be unambiguously referred to even if
3522 : : there are multiple indirect base classes containing NAME. */
3523 : 5313 : const base_access ba = [scope, name] ()
3524 : : {
3525 : 1771 : if (identifier_p (name))
3526 : : {
3527 : 1634 : tree m = lookup_member (scope, name, /*protect=*/0,
3528 : : /*want_type=*/false, tf_none);
3529 : 1634 : if (!m || shared_member_p (m))
3530 : 32 : return ba_any;
3531 : : }
3532 : : return ba_check;
3533 : 1771 : } ();
3534 : :
3535 : : /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3536 : 1771 : access_path = lookup_base (object_type, scope, ba, NULL, complain);
3537 : 1771 : if (access_path == error_mark_node)
3538 : : return error_mark_node;
3539 : 1759 : if (!access_path)
3540 : : {
3541 : 12 : if (any_dependent_bases_p (object_type))
3542 : 6 : goto dependent;
3543 : 6 : if (complain & tf_error)
3544 : 6 : error ("%qT is not a base of %qT", scope, object_type);
3545 : 6 : return error_mark_node;
3546 : : }
3547 : : }
3548 : :
3549 : 88648072 : if (TREE_CODE (name) == BIT_NOT_EXPR)
3550 : : {
3551 : 209990 : if (dependent_type_p (object_type))
3552 : : /* The destructor isn't declared yet. */
3553 : 34718 : goto dependent;
3554 : 175272 : member = lookup_destructor (object, scope, name, complain);
3555 : : }
3556 : : else
3557 : : {
3558 : : /* Look up the member. */
3559 : 88438082 : {
3560 : 88438082 : auto_diagnostic_group d;
3561 : 88438082 : access_failure_info afi;
3562 : 88438082 : if (processing_template_decl)
3563 : : /* Even though this class member access expression is at this
3564 : : point not dependent, the member itself may be dependent, and
3565 : : we must not potentially push a access check for a dependent
3566 : : member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3567 : : ahead of time here; we're going to redo this member lookup at
3568 : : instantiation time anyway. */
3569 : 68901396 : push_deferring_access_checks (dk_no_check);
3570 : 88438082 : member = lookup_member (access_path, name, /*protect=*/1,
3571 : : /*want_type=*/false, complain,
3572 : : &afi);
3573 : 88438082 : if (processing_template_decl)
3574 : 68901396 : pop_deferring_access_checks ();
3575 : 88438082 : afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3576 : 88438082 : }
3577 : 88438082 : if (member == NULL_TREE)
3578 : : {
3579 : 8454455 : if (dependentish_scope_p (object_type))
3580 : : /* Try again at instantiation time. */
3581 : 8433915 : goto dependent;
3582 : 20540 : if (complain & tf_error)
3583 : 308 : complain_about_unrecognized_member (access_path, name,
3584 : : object_type);
3585 : 20540 : return error_mark_node;
3586 : : }
3587 : 79983627 : if (member == error_mark_node)
3588 : : return error_mark_node;
3589 : 79983583 : if (DECL_P (member)
3590 : 79983583 : && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3591 : : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3592 : : wrong, so don't use it. */
3593 : 6 : goto dependent;
3594 : 79983577 : if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3595 : 5597511 : goto dependent;
3596 : : }
3597 : :
3598 : 74561338 : if (is_template_id)
3599 : : {
3600 : 477289 : tree templ = member;
3601 : :
3602 : 477289 : if (BASELINK_P (templ))
3603 : 477265 : member = lookup_template_function (templ, template_args);
3604 : 24 : else if (variable_template_p (templ))
3605 : 24 : member = (lookup_and_finish_template_variable
3606 : 24 : (templ, template_args, complain));
3607 : : else
3608 : : {
3609 : 0 : if (complain & tf_error)
3610 : 0 : error ("%qD is not a member template function", name);
3611 : 0 : return error_mark_node;
3612 : : }
3613 : : }
3614 : : }
3615 : :
3616 : 84095109 : if (TREE_UNAVAILABLE (member))
3617 : 30 : error_unavailable_use (member, NULL_TREE);
3618 : 84095079 : else if (TREE_DEPRECATED (member))
3619 : 30 : warn_deprecated_use (member, NULL_TREE);
3620 : :
3621 : 84095109 : if (template_p)
3622 : 9940 : check_template_keyword (member);
3623 : :
3624 : 84095109 : expr = build_class_member_access_expr (object, member, access_path,
3625 : : /*preserve_reference=*/false,
3626 : : complain);
3627 : 84095109 : if (processing_template_decl && expr != error_mark_node)
3628 : : {
3629 : 55196925 : if (BASELINK_P (member))
3630 : : {
3631 : 41111919 : if (TREE_CODE (orig_name) == SCOPE_REF)
3632 : 169 : BASELINK_QUALIFIED_P (member) = 1;
3633 : : orig_name = member;
3634 : : }
3635 : 55196925 : return build_min_non_dep (COMPONENT_REF, expr,
3636 : : orig_object, orig_name,
3637 : 55196925 : NULL_TREE);
3638 : : }
3639 : :
3640 : : return expr;
3641 : : }
3642 : :
3643 : : /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3644 : : type. */
3645 : :
3646 : : tree
3647 : 180896 : build_simple_component_ref (tree object, tree member)
3648 : : {
3649 : 180896 : tree type = cp_build_qualified_type (TREE_TYPE (member),
3650 : 180896 : cp_type_quals (TREE_TYPE (object)));
3651 : 180896 : return build3_loc (input_location,
3652 : : COMPONENT_REF, type,
3653 : 180896 : object, member, NULL_TREE);
3654 : : }
3655 : :
3656 : : /* Return an expression for the MEMBER_NAME field in the internal
3657 : : representation of PTRMEM, a pointer-to-member function. (Each
3658 : : pointer-to-member function type gets its own RECORD_TYPE so it is
3659 : : more convenient to access the fields by name than by FIELD_DECL.)
3660 : : This routine converts the NAME to a FIELD_DECL and then creates the
3661 : : node for the complete expression. */
3662 : :
3663 : : tree
3664 : 180800 : build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3665 : : {
3666 : 180800 : tree ptrmem_type;
3667 : 180800 : tree member;
3668 : :
3669 : 180800 : if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3670 : : {
3671 : 198 : for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3672 : 78 : if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3673 : 60 : return e.value;
3674 : 0 : gcc_unreachable ();
3675 : : }
3676 : :
3677 : : /* This code is a stripped down version of
3678 : : build_class_member_access_expr. It does not work to use that
3679 : : routine directly because it expects the object to be of class
3680 : : type. */
3681 : 180740 : ptrmem_type = TREE_TYPE (ptrmem);
3682 : 180740 : gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3683 : 271010 : for (member = TYPE_FIELDS (ptrmem_type); member;
3684 : 90270 : member = DECL_CHAIN (member))
3685 : 271010 : if (DECL_NAME (member) == member_name)
3686 : : break;
3687 : 180740 : return build_simple_component_ref (ptrmem, member);
3688 : : }
3689 : :
3690 : : /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3691 : : and for any other relevant operator. */
3692 : :
3693 : : static tree
3694 : 121088008 : op_unqualified_lookup (tree_code code, bool is_assign)
3695 : : {
3696 : 121088008 : tree lookups = NULL_TREE;
3697 : :
3698 : 121088008 : if (cxx_dialect >= cxx20 && !is_assign)
3699 : : {
3700 : 36585481 : if (code == NE_EXPR)
3701 : : {
3702 : : /* != can get rewritten in terms of ==. */
3703 : 2389392 : tree fnname = ovl_op_identifier (false, EQ_EXPR);
3704 : 2389392 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3705 : 2367584 : lookups = tree_cons (fnname, fns, lookups);
3706 : : }
3707 : 34196089 : else if (code == GT_EXPR || code == LE_EXPR
3708 : 34196089 : || code == LT_EXPR || code == GE_EXPR)
3709 : : {
3710 : : /* These can get rewritten in terms of <=>. */
3711 : 3187820 : tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3712 : 3187820 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3713 : 3008434 : lookups = tree_cons (fnname, fns, lookups);
3714 : : }
3715 : : }
3716 : :
3717 : 121088008 : tree fnname = ovl_op_identifier (is_assign, code);
3718 : 121088008 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3719 : 66829612 : lookups = tree_cons (fnname, fns, lookups);
3720 : :
3721 : 121088008 : if (lookups)
3722 : : return lookups;
3723 : : else
3724 : 54194768 : return build_tree_list (NULL_TREE, NULL_TREE);
3725 : : }
3726 : :
3727 : : /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3728 : : the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3729 : : name lookup for the given operator. */
3730 : :
3731 : : tree
3732 : 130547807 : build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3733 : : {
3734 : 130547807 : if (lookups)
3735 : : /* We're partially instantiating a dependent operator expression, and
3736 : : LOOKUPS is the result of phase 1 name lookup that we performed
3737 : : earlier at template definition time, so just reuse the corresponding
3738 : : DEPENDENT_OPERATOR_TYPE. */
3739 : 9459799 : return TREE_TYPE (lookups);
3740 : :
3741 : : /* Otherwise we're processing a dependent operator expression at template
3742 : : definition time, so perform phase 1 name lookup now. */
3743 : 121088008 : lookups = op_unqualified_lookup (code, is_assign);
3744 : :
3745 : 121088008 : tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3746 : 121088008 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3747 : 121088008 : TREE_TYPE (lookups) = type;
3748 : 121088008 : return type;
3749 : : }
3750 : :
3751 : : /* Given an expression PTR for a pointer, return an expression
3752 : : for the value pointed to.
3753 : : ERRORSTRING is the name of the operator to appear in error messages.
3754 : :
3755 : : This function may need to overload OPERATOR_FNNAME.
3756 : : Must also handle REFERENCE_TYPEs for C++. */
3757 : :
3758 : : tree
3759 : 41799247 : build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3760 : : tree lookups, tsubst_flags_t complain)
3761 : : {
3762 : 41799247 : tree orig_expr = expr;
3763 : 41799247 : tree rval;
3764 : 41799247 : tree overload = NULL_TREE;
3765 : :
3766 : 41799247 : if (processing_template_decl)
3767 : : {
3768 : : /* Retain the type if we know the operand is a pointer. */
3769 : 24070409 : if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3770 : : {
3771 : 12584085 : if (expr == current_class_ptr
3772 : 12584085 : || (TREE_CODE (expr) == NOP_EXPR
3773 : 7908764 : && TREE_OPERAND (expr, 0) == current_class_ptr
3774 : 7897426 : && (same_type_ignoring_top_level_qualifiers_p
3775 : 7897426 : (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3776 : 7897425 : return current_class_ref;
3777 : 4686660 : return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3778 : : }
3779 : 11486324 : if (type_dependent_expression_p (expr))
3780 : : {
3781 : 11335780 : expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3782 : 11335780 : TREE_TYPE (expr)
3783 : 11335780 : = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3784 : 11335780 : return expr;
3785 : : }
3786 : : }
3787 : :
3788 : 17879382 : rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3789 : : NULL_TREE, NULL_TREE, lookups,
3790 : : &overload, complain);
3791 : 17879382 : if (!rval)
3792 : 0 : rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3793 : :
3794 : 17879382 : if (processing_template_decl && rval != error_mark_node)
3795 : : {
3796 : 148711 : if (overload != NULL_TREE)
3797 : 148680 : return (build_min_non_dep_op_overload
3798 : 148680 : (INDIRECT_REF, rval, overload, orig_expr));
3799 : :
3800 : 31 : return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3801 : : }
3802 : : else
3803 : : return rval;
3804 : : }
3805 : :
3806 : : /* Like c-family strict_aliasing_warning, but don't warn for dependent
3807 : : types or expressions. */
3808 : :
3809 : : static bool
3810 : 574642 : cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3811 : : {
3812 : 574642 : if (processing_template_decl)
3813 : : {
3814 : 42129 : tree e = expr;
3815 : 42129 : STRIP_NOPS (e);
3816 : 42129 : if (dependent_type_p (type) || type_dependent_expression_p (e))
3817 : 6840 : return false;
3818 : : }
3819 : 567802 : return strict_aliasing_warning (loc, type, expr);
3820 : : }
3821 : :
3822 : : /* The implementation of the above, and of indirection implied by other
3823 : : constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3824 : :
3825 : : static tree
3826 : 305484773 : cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3827 : : tsubst_flags_t complain, bool do_fold)
3828 : : {
3829 : 305484773 : tree pointer, type;
3830 : :
3831 : : /* RO_NULL should only be used with the folding entry points below, not
3832 : : cp_build_indirect_ref. */
3833 : 305484773 : gcc_checking_assert (errorstring != RO_NULL || do_fold);
3834 : :
3835 : 305484773 : if (ptr == current_class_ptr
3836 : 305484773 : || (TREE_CODE (ptr) == NOP_EXPR
3837 : 31288031 : && TREE_OPERAND (ptr, 0) == current_class_ptr
3838 : 17380572 : && (same_type_ignoring_top_level_qualifiers_p
3839 : 17380572 : (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3840 : 18816041 : return current_class_ref;
3841 : :
3842 : 286668732 : pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3843 : 286668732 : ? ptr : decay_conversion (ptr, complain));
3844 : 286668732 : if (pointer == error_mark_node)
3845 : : return error_mark_node;
3846 : :
3847 : 286662884 : type = TREE_TYPE (pointer);
3848 : :
3849 : 286662884 : if (INDIRECT_TYPE_P (type))
3850 : : {
3851 : : /* [expr.unary.op]
3852 : :
3853 : : If the type of the expression is "pointer to T," the type
3854 : : of the result is "T." */
3855 : 286662538 : tree t = TREE_TYPE (type);
3856 : :
3857 : 271587253 : if ((CONVERT_EXPR_P (ptr)
3858 : 203717550 : || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3859 : 356483407 : && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3860 : : {
3861 : : /* If a warning is issued, mark it to avoid duplicates from
3862 : : the backend. This only needs to be done at
3863 : : warn_strict_aliasing > 2. */
3864 : 60316479 : if (warn_strict_aliasing > 2
3865 : 60811205 : && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3866 : 494726 : type, TREE_OPERAND (ptr, 0)))
3867 : 6 : suppress_warning (ptr, OPT_Wstrict_aliasing);
3868 : : }
3869 : :
3870 : 286662538 : if (VOID_TYPE_P (t))
3871 : : {
3872 : : /* A pointer to incomplete type (other than cv void) can be
3873 : : dereferenced [expr.unary.op]/1 */
3874 : 70 : if (complain & tf_error)
3875 : 19 : error_at (loc, "%qT is not a pointer-to-object type", type);
3876 : 70 : return error_mark_node;
3877 : : }
3878 : 277414458 : else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3879 : 292366164 : && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3880 : : /* The POINTER was something like `&x'. We simplify `*&x' to
3881 : : `x'. This can change the value category: '*&TARGET_EXPR'
3882 : : is an lvalue and folding it into 'TARGET_EXPR' turns it into
3883 : : a prvalue of class type. */
3884 : 5703667 : return TREE_OPERAND (pointer, 0);
3885 : : else
3886 : : {
3887 : 280958801 : tree ref = build1 (INDIRECT_REF, t, pointer);
3888 : :
3889 : : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3890 : : so that we get the proper error message if the result is used
3891 : : to assign to. Also, &* is supposed to be a no-op. */
3892 : 280958801 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3893 : 280958801 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3894 : 280958801 : TREE_SIDE_EFFECTS (ref)
3895 : 280958801 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3896 : 280958801 : return ref;
3897 : : }
3898 : : }
3899 : 346 : else if (!(complain & tf_error))
3900 : : /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3901 : : ;
3902 : : /* `pointer' won't be an error_mark_node if we were given a
3903 : : pointer to member, so it's cool to check for this here. */
3904 : 39 : else if (TYPE_PTRMEM_P (type))
3905 : 15 : switch (errorstring)
3906 : : {
3907 : 0 : case RO_ARRAY_INDEXING:
3908 : 0 : error_at (loc,
3909 : : "invalid use of array indexing on pointer to member");
3910 : 0 : break;
3911 : 12 : case RO_UNARY_STAR:
3912 : 12 : error_at (loc, "invalid use of unary %<*%> on pointer to member");
3913 : 12 : break;
3914 : 0 : case RO_IMPLICIT_CONVERSION:
3915 : 0 : error_at (loc, "invalid use of implicit conversion on pointer "
3916 : : "to member");
3917 : 0 : break;
3918 : 3 : case RO_ARROW_STAR:
3919 : 3 : error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3920 : : "class, but is a pointer to member of type %qT", type);
3921 : 3 : break;
3922 : 0 : default:
3923 : 0 : gcc_unreachable ();
3924 : : }
3925 : 24 : else if (pointer != error_mark_node)
3926 : 24 : invalid_indirection_error (loc, type, errorstring);
3927 : :
3928 : 346 : return error_mark_node;
3929 : : }
3930 : :
3931 : : /* Entry point used by c-common, which expects folding. */
3932 : :
3933 : : tree
3934 : 21345 : build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3935 : : {
3936 : 21345 : return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3937 : 21345 : tf_warning_or_error, true);
3938 : : }
3939 : :
3940 : : /* Entry point used by internal indirection needs that don't correspond to any
3941 : : syntactic construct. */
3942 : :
3943 : : tree
3944 : 280765769 : cp_build_fold_indirect_ref (tree pointer)
3945 : : {
3946 : 280765769 : return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3947 : 280765769 : tf_warning_or_error, true);
3948 : : }
3949 : :
3950 : : /* Entry point used by indirection needs that correspond to some syntactic
3951 : : construct. */
3952 : :
3953 : : tree
3954 : 24697659 : cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3955 : : tsubst_flags_t complain)
3956 : : {
3957 : 24697659 : return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3958 : : }
3959 : :
3960 : : /* This handles expressions of the form "a[i]", which denotes
3961 : : an array reference.
3962 : :
3963 : : This is logically equivalent in C to *(a+i), but we may do it differently.
3964 : : If A is a variable or a member, we generate a primitive ARRAY_REF.
3965 : : This avoids forcing the array out of registers, and can work on
3966 : : arrays that are not lvalues (for example, members of structures returned
3967 : : by functions).
3968 : :
3969 : : If INDEX is of some user-defined type, it must be converted to
3970 : : integer type. Otherwise, to make a compatible PLUS_EXPR, it
3971 : : will inherit the type of the array, which will be some pointer type.
3972 : :
3973 : : LOC is the location to use in building the array reference. */
3974 : :
3975 : : tree
3976 : 5325545 : cp_build_array_ref (location_t loc, tree array, tree idx,
3977 : : tsubst_flags_t complain)
3978 : : {
3979 : 5325545 : tree ret;
3980 : :
3981 : 5325545 : if (idx == 0)
3982 : : {
3983 : 0 : if (complain & tf_error)
3984 : 0 : error_at (loc, "subscript missing in array reference");
3985 : 0 : return error_mark_node;
3986 : : }
3987 : :
3988 : 5325545 : if (TREE_TYPE (array) == error_mark_node
3989 : 5325545 : || TREE_TYPE (idx) == error_mark_node)
3990 : : return error_mark_node;
3991 : :
3992 : : /* 0[array] */
3993 : 5325545 : if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE)
3994 : : {
3995 : 10 : std::swap (array, idx);
3996 : :
3997 : 10 : tree first = NULL_TREE;
3998 : 10 : if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array))
3999 : 9 : idx = first = save_expr (idx);
4000 : 10 : ret = cp_build_array_ref (loc, array, idx, complain);
4001 : :
4002 : 10 : if (first)
4003 : 9 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4004 : 10 : return ret;
4005 : : }
4006 : :
4007 : : /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
4008 : : inside it. */
4009 : 5325535 : switch (TREE_CODE (array))
4010 : : {
4011 : 28 : case COMPOUND_EXPR:
4012 : 28 : {
4013 : 28 : tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
4014 : : complain);
4015 : 28 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
4016 : 28 : TREE_OPERAND (array, 0), value);
4017 : 28 : SET_EXPR_LOCATION (ret, loc);
4018 : 28 : return ret;
4019 : : }
4020 : :
4021 : 64 : case COND_EXPR:
4022 : 64 : tree op0, op1, op2;
4023 : 64 : op0 = TREE_OPERAND (array, 0);
4024 : 64 : op1 = TREE_OPERAND (array, 1);
4025 : 64 : op2 = TREE_OPERAND (array, 2);
4026 : 64 : if (TREE_SIDE_EFFECTS (idx) || !tree_invariant_p (idx))
4027 : : {
4028 : : /* If idx could possibly have some SAVE_EXPRs, turning
4029 : : (op0 ? op1 : op2)[idx] into
4030 : : op0 ? op1[idx] : op2[idx] can lead into temporaries
4031 : : initialized in one conditional path and uninitialized
4032 : : uses of them in the other path.
4033 : : And if idx is a really large expression, evaluating it
4034 : : twice is also not optimal.
4035 : : On the other side, op0 must be sequenced before evaluation
4036 : : of op1 and op2 and for C++17 op0, op1 and op2 must be
4037 : : sequenced before idx.
4038 : : If idx is INTEGER_CST, we can just do the optimization
4039 : : without any SAVE_EXPRs, if op1 and op2 are both ARRAY_TYPE
4040 : : VAR_DECLs or COMPONENT_REFs thereof (so their address
4041 : : is constant or relative to frame), optimize into
4042 : : (SAVE_EXPR <op0>, SAVE_EXPR <idx>, SAVE_EXPR <op0>)
4043 : : ? op1[SAVE_EXPR <idx>] : op2[SAVE_EXPR <idx>]
4044 : : Otherwise avoid this optimization. */
4045 : 49 : if (flag_strong_eval_order == 2)
4046 : : {
4047 : 33 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4048 : : {
4049 : 24 : if (!address_invariant_p (op1) || !address_invariant_p (op2))
4050 : : {
4051 : : /* Force default conversion on array if
4052 : : we can't optimize this and array is ARRAY_TYPE
4053 : : COND_EXPR, we can't leave COND_EXPRs with
4054 : : ARRAY_TYPE in the IL. */
4055 : 8 : array = cp_default_conversion (array, complain);
4056 : 8 : if (error_operand_p (array))
4057 : 0 : return error_mark_node;
4058 : : break;
4059 : : }
4060 : : }
4061 : 16 : else if (!POINTER_TYPE_P (TREE_TYPE (array))
4062 : 2 : || !tree_invariant_p (op1)
4063 : 11 : || !tree_invariant_p (op2))
4064 : : break;
4065 : : }
4066 : 34 : if (TREE_SIDE_EFFECTS (idx))
4067 : : {
4068 : 12 : idx = save_expr (idx);
4069 : 12 : op0 = save_expr (op0);
4070 : 12 : tree tem = build_compound_expr (loc, op0, idx);
4071 : 12 : op0 = build_compound_expr (loc, tem, op0);
4072 : : }
4073 : : }
4074 : 49 : op1 = cp_build_array_ref (loc, op1, idx, complain);
4075 : 49 : op2 = cp_build_array_ref (loc, op2, idx, complain);
4076 : 49 : ret = build_conditional_expr (loc, op0, op1, op2, complain);
4077 : 49 : protected_set_expr_location (ret, loc);
4078 : 49 : return ret;
4079 : :
4080 : : default:
4081 : : break;
4082 : : }
4083 : :
4084 : 5325458 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
4085 : :
4086 : 5325458 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4087 : : {
4088 : 1679445 : tree rval, type;
4089 : :
4090 : 1679445 : warn_array_subscript_with_type_char (loc, idx);
4091 : :
4092 : 1679445 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
4093 : : {
4094 : 3 : if (complain & tf_error)
4095 : 3 : error_at (loc, "array subscript is not an integer");
4096 : 3 : return error_mark_node;
4097 : : }
4098 : :
4099 : : /* Apply integral promotions *after* noticing character types.
4100 : : (It is unclear why we do these promotions -- the standard
4101 : : does not say that we should. In fact, the natural thing would
4102 : : seem to be to convert IDX to ptrdiff_t; we're performing
4103 : : pointer arithmetic.) */
4104 : 1679442 : idx = cp_perform_integral_promotions (idx, complain);
4105 : :
4106 : 1679442 : idx = maybe_fold_non_dependent_expr (idx, complain);
4107 : :
4108 : : /* An array that is indexed by a non-constant
4109 : : cannot be stored in a register; we must be able to do
4110 : : address arithmetic on its address.
4111 : : Likewise an array of elements of variable size. */
4112 : 1679442 : if (TREE_CODE (idx) != INTEGER_CST
4113 : 1679442 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4114 : 836269 : && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4115 : : != INTEGER_CST)))
4116 : : {
4117 : 844424 : if (!cxx_mark_addressable (array, true))
4118 : 0 : return error_mark_node;
4119 : : }
4120 : :
4121 : : /* An array that is indexed by a constant value which is not within
4122 : : the array bounds cannot be stored in a register either; because we
4123 : : would get a crash in store_bit_field/extract_bit_field when trying
4124 : : to access a non-existent part of the register. */
4125 : 1679442 : if (TREE_CODE (idx) == INTEGER_CST
4126 : 836272 : && TYPE_DOMAIN (TREE_TYPE (array))
4127 : 2514328 : && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4128 : : {
4129 : 2836 : if (!cxx_mark_addressable (array))
4130 : 0 : return error_mark_node;
4131 : : }
4132 : :
4133 : : /* Note in C++ it is valid to subscript a `register' array, since
4134 : : it is valid to take the address of something with that
4135 : : storage specification. */
4136 : 1679442 : if (extra_warnings)
4137 : : {
4138 : 37950 : tree foo = array;
4139 : 58112 : while (TREE_CODE (foo) == COMPONENT_REF)
4140 : 20162 : foo = TREE_OPERAND (foo, 0);
4141 : 8 : if (VAR_P (foo) && DECL_REGISTER (foo)
4142 : 37950 : && (complain & tf_warning))
4143 : 0 : warning_at (loc, OPT_Wextra,
4144 : : "subscripting array declared %<register%>");
4145 : : }
4146 : :
4147 : 1679442 : type = TREE_TYPE (TREE_TYPE (array));
4148 : 1679442 : rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4149 : : /* Array ref is const/volatile if the array elements are
4150 : : or if the array is.. */
4151 : 5038326 : TREE_READONLY (rval)
4152 : 1679442 : |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4153 : 5038326 : TREE_SIDE_EFFECTS (rval)
4154 : 1679442 : |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4155 : 3358884 : TREE_THIS_VOLATILE (rval)
4156 : 1679442 : |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4157 : 1679442 : ret = require_complete_type (rval, complain);
4158 : 1679442 : protected_set_expr_location (ret, loc);
4159 : 1679442 : if (non_lvalue)
4160 : 6640 : ret = non_lvalue_loc (loc, ret);
4161 : 1679442 : return ret;
4162 : : }
4163 : :
4164 : 3646013 : {
4165 : 3646013 : tree ar = cp_default_conversion (array, complain);
4166 : 3646013 : tree ind = cp_default_conversion (idx, complain);
4167 : 3646013 : tree first = NULL_TREE;
4168 : :
4169 : 2234651 : if (!processing_template_decl && flag_strong_eval_order == 2
4170 : 5840529 : && TREE_SIDE_EFFECTS (ind))
4171 : 99855 : ar = first = save_expr (ar);
4172 : :
4173 : : /* Put the integer in IND to simplify error checking. */
4174 : 3646013 : if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4175 : 57 : std::swap (ar, ind);
4176 : :
4177 : 3646013 : if (ar == error_mark_node || ind == error_mark_node)
4178 : : return error_mark_node;
4179 : :
4180 : 3646013 : if (!TYPE_PTR_P (TREE_TYPE (ar)))
4181 : : {
4182 : 22 : if (complain & tf_error)
4183 : 3 : error_at (loc, "subscripted value is neither array nor pointer");
4184 : 22 : return error_mark_node;
4185 : : }
4186 : 3645991 : if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4187 : : {
4188 : 0 : if (complain & tf_error)
4189 : 0 : error_at (loc, "array subscript is not an integer");
4190 : 0 : return error_mark_node;
4191 : : }
4192 : :
4193 : 3645991 : warn_array_subscript_with_type_char (loc, idx);
4194 : :
4195 : 3645991 : ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4196 : 3645991 : if (first)
4197 : 99855 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4198 : 3645991 : ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4199 : 3645991 : protected_set_expr_location (ret, loc);
4200 : 3645991 : if (non_lvalue)
4201 : 0 : ret = non_lvalue_loc (loc, ret);
4202 : : return ret;
4203 : : }
4204 : : }
4205 : :
4206 : : /* Entry point for Obj-C++. */
4207 : :
4208 : : tree
4209 : 3577432 : build_array_ref (location_t loc, tree array, tree idx)
4210 : : {
4211 : 3577432 : return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4212 : : }
4213 : :
4214 : : /* Resolve a pointer to member function. INSTANCE is the object
4215 : : instance to use, if the member points to a virtual member.
4216 : :
4217 : : This used to avoid checking for virtual functions if basetype
4218 : : has no virtual functions, according to an earlier ANSI draft.
4219 : : With the final ISO C++ rules, such an optimization is
4220 : : incorrect: A pointer to a derived member can be static_cast
4221 : : to pointer-to-base-member, as long as the dynamic object
4222 : : later has the right member. So now we only do this optimization
4223 : : when we know the dynamic type of the object. */
4224 : :
4225 : : tree
4226 : 90167 : get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4227 : : tsubst_flags_t complain)
4228 : : {
4229 : 90167 : if (TREE_CODE (function) == OFFSET_REF)
4230 : 0 : function = TREE_OPERAND (function, 1);
4231 : :
4232 : 90167 : if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4233 : : {
4234 : 90167 : tree idx, delta, e1, e2, e3, vtbl;
4235 : 90167 : bool nonvirtual;
4236 : 90167 : tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4237 : 90167 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4238 : :
4239 : 90167 : tree instance_ptr = *instance_ptrptr;
4240 : 90167 : tree instance_save_expr = 0;
4241 : 90167 : if (instance_ptr == error_mark_node)
4242 : : {
4243 : 0 : if (TREE_CODE (function) == PTRMEM_CST)
4244 : : {
4245 : : /* Extracting the function address from a pmf is only
4246 : : allowed with -Wno-pmf-conversions. It only works for
4247 : : pmf constants. */
4248 : 0 : e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4249 : 0 : e1 = convert (fntype, e1);
4250 : 0 : return e1;
4251 : : }
4252 : : else
4253 : : {
4254 : 0 : if (complain & tf_error)
4255 : 0 : error ("object missing in use of %qE", function);
4256 : 0 : return error_mark_node;
4257 : : }
4258 : : }
4259 : :
4260 : : /* True if we know that the dynamic type of the object doesn't have
4261 : : virtual functions, so we can assume the PFN field is a pointer. */
4262 : 90167 : nonvirtual = (COMPLETE_TYPE_P (basetype)
4263 : 90119 : && !TYPE_POLYMORPHIC_P (basetype)
4264 : 121086 : && resolves_to_fixed_type_p (instance_ptr, 0));
4265 : :
4266 : : /* If we don't really have an object (i.e. in an ill-formed
4267 : : conversion from PMF to pointer), we can't resolve virtual
4268 : : functions anyway. */
4269 : 89915 : if (!nonvirtual && is_dummy_object (instance_ptr))
4270 : : nonvirtual = true;
4271 : :
4272 : : /* Use force_target_expr even when instance_ptr doesn't have
4273 : : side-effects, unless it is a simple decl or constant, so
4274 : : that we don't ubsan instrument the expression multiple times.
4275 : : Don't use save_expr, as save_expr can avoid building a SAVE_EXPR
4276 : : and building a SAVE_EXPR manually can be optimized away during
4277 : : cp_fold. See PR116449 and PR117259. */
4278 : 90167 : if (TREE_SIDE_EFFECTS (instance_ptr)
4279 : 90167 : || (!nonvirtual
4280 : 50294 : && !DECL_P (instance_ptr)
4281 : 50294 : && !TREE_CONSTANT (instance_ptr)))
4282 : 89669 : instance_ptr = instance_save_expr
4283 : 89669 : = get_internal_target_expr (instance_ptr);
4284 : :
4285 : : /* See above comment. */
4286 : 90167 : if (TREE_SIDE_EFFECTS (function)
4287 : 90167 : || (!nonvirtual
4288 : 69932 : && !DECL_P (function)
4289 : 69920 : && !TREE_CONSTANT (function)))
4290 : 89701 : function = get_internal_target_expr (function);
4291 : :
4292 : : /* Start by extracting all the information from the PMF itself. */
4293 : 90167 : e3 = pfn_from_ptrmemfunc (function);
4294 : 90167 : delta = delta_from_ptrmemfunc (function);
4295 : 90167 : idx = build1 (NOP_EXPR, vtable_index_type, e3);
4296 : 90167 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4297 : : {
4298 : 90167 : sanitize_code_type flag_sanitize_save;
4299 : 90167 : case ptrmemfunc_vbit_in_pfn:
4300 : 90167 : e1 = cp_build_binary_op (input_location,
4301 : : BIT_AND_EXPR, idx, integer_one_node,
4302 : : complain);
4303 : 90167 : idx = cp_build_binary_op (input_location,
4304 : : MINUS_EXPR, idx, integer_one_node,
4305 : : complain);
4306 : 90167 : if (idx == error_mark_node)
4307 : : return error_mark_node;
4308 : 90167 : break;
4309 : :
4310 : : case ptrmemfunc_vbit_in_delta:
4311 : : e1 = cp_build_binary_op (input_location,
4312 : : BIT_AND_EXPR, delta, integer_one_node,
4313 : : complain);
4314 : : /* Don't instrument the RSHIFT_EXPR we're about to create because
4315 : : we're going to use DELTA number of times, and that wouldn't play
4316 : : well with SAVE_EXPRs therein. */
4317 : : flag_sanitize_save = flag_sanitize;
4318 : : flag_sanitize = 0;
4319 : : delta = cp_build_binary_op (input_location,
4320 : : RSHIFT_EXPR, delta, integer_one_node,
4321 : : complain);
4322 : : flag_sanitize = flag_sanitize_save;
4323 : : if (delta == error_mark_node)
4324 : : return error_mark_node;
4325 : : break;
4326 : :
4327 : : default:
4328 : : gcc_unreachable ();
4329 : : }
4330 : :
4331 : 90167 : if (e1 == error_mark_node)
4332 : : return error_mark_node;
4333 : :
4334 : : /* Convert down to the right base before using the instance. A
4335 : : special case is that in a pointer to member of class C, C may
4336 : : be incomplete. In that case, the function will of course be
4337 : : a member of C, and no conversion is required. In fact,
4338 : : lookup_base will fail in that case, because incomplete
4339 : : classes do not have BINFOs. */
4340 : 90167 : if (!same_type_ignoring_top_level_qualifiers_p
4341 : 90167 : (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4342 : : {
4343 : 81 : basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4344 : : basetype, ba_check, NULL, complain);
4345 : 81 : instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4346 : : 1, complain);
4347 : 81 : if (instance_ptr == error_mark_node)
4348 : : return error_mark_node;
4349 : : }
4350 : : /* ...and then the delta in the PMF. */
4351 : 90167 : instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4352 : :
4353 : : /* Hand back the adjusted 'this' argument to our caller. */
4354 : 90167 : *instance_ptrptr = instance_ptr;
4355 : :
4356 : 90167 : if (nonvirtual)
4357 : : /* Now just return the pointer. */
4358 : : return e3;
4359 : :
4360 : : /* Next extract the vtable pointer from the object. */
4361 : 89897 : vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4362 : : instance_ptr);
4363 : 89897 : vtbl = cp_build_fold_indirect_ref (vtbl);
4364 : 89897 : if (vtbl == error_mark_node)
4365 : : return error_mark_node;
4366 : :
4367 : : /* Finally, extract the function pointer from the vtable. */
4368 : 89897 : e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4369 : 89897 : e2 = cp_build_fold_indirect_ref (e2);
4370 : 89897 : if (e2 == error_mark_node)
4371 : : return error_mark_node;
4372 : 89897 : TREE_CONSTANT (e2) = 1;
4373 : :
4374 : : /* When using function descriptors, the address of the
4375 : : vtable entry is treated as a function pointer. */
4376 : 89897 : if (TARGET_VTABLE_USES_DESCRIPTORS)
4377 : : e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4378 : : cp_build_addr_expr (e2, complain));
4379 : :
4380 : 89897 : e2 = fold_convert (TREE_TYPE (e3), e2);
4381 : 89897 : e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4382 : 89897 : if (e1 == error_mark_node)
4383 : : return error_mark_node;
4384 : :
4385 : : /* Make sure this doesn't get evaluated first inside one of the
4386 : : branches of the COND_EXPR. */
4387 : 89897 : if (instance_save_expr)
4388 : 89621 : e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4389 : : instance_save_expr, e1);
4390 : :
4391 : : function = e1;
4392 : : }
4393 : : return function;
4394 : : }
4395 : :
4396 : : /* Used by the C-common bits. */
4397 : : tree
4398 : 0 : build_function_call (location_t /*loc*/,
4399 : : tree function, tree params)
4400 : : {
4401 : 0 : return cp_build_function_call (function, params, tf_warning_or_error);
4402 : : }
4403 : :
4404 : : /* Used by the C-common bits. */
4405 : : tree
4406 : 237195 : build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4407 : : tree function, vec<tree, va_gc> *params,
4408 : : vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4409 : : {
4410 : 237195 : vec<tree, va_gc> *orig_params = params;
4411 : 237195 : tree ret = cp_build_function_call_vec (function, ¶ms,
4412 : : tf_warning_or_error, orig_function);
4413 : :
4414 : : /* cp_build_function_call_vec can reallocate PARAMS by adding
4415 : : default arguments. That should never happen here. Verify
4416 : : that. */
4417 : 237195 : gcc_assert (params == orig_params);
4418 : :
4419 : 237195 : return ret;
4420 : : }
4421 : :
4422 : : /* Build a function call using a tree list of arguments. */
4423 : :
4424 : : static tree
4425 : 0 : cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4426 : : {
4427 : 0 : tree ret;
4428 : :
4429 : 0 : releasing_vec vec;
4430 : 0 : for (; params != NULL_TREE; params = TREE_CHAIN (params))
4431 : 0 : vec_safe_push (vec, TREE_VALUE (params));
4432 : 0 : ret = cp_build_function_call_vec (function, &vec, complain);
4433 : 0 : return ret;
4434 : 0 : }
4435 : :
4436 : : /* Build a function call using varargs. */
4437 : :
4438 : : tree
4439 : 524036 : cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4440 : : {
4441 : 524036 : va_list args;
4442 : 524036 : tree ret, t;
4443 : :
4444 : 524036 : releasing_vec vec;
4445 : 524036 : va_start (args, complain);
4446 : 1293922 : for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4447 : 769886 : vec_safe_push (vec, t);
4448 : 524036 : va_end (args);
4449 : 524036 : ret = cp_build_function_call_vec (function, &vec, complain);
4450 : 524036 : return ret;
4451 : 524036 : }
4452 : :
4453 : : /* C++ implementation of callback for use when checking param types. */
4454 : :
4455 : : bool
4456 : 18 : cp_comp_parm_types (tree wanted_type, tree actual_type)
4457 : : {
4458 : 18 : if (TREE_CODE (wanted_type) == POINTER_TYPE
4459 : 18 : && TREE_CODE (actual_type) == POINTER_TYPE)
4460 : 15 : return same_or_base_type_p (TREE_TYPE (wanted_type),
4461 : : TREE_TYPE (actual_type));
4462 : : return false;
4463 : : }
4464 : :
4465 : : /* Build a function call using a vector of arguments.
4466 : : If FUNCTION is the result of resolving an overloaded target built-in,
4467 : : ORIG_FNDECL is the original function decl, otherwise it is null.
4468 : : PARAMS may be NULL if there are no parameters. This changes the
4469 : : contents of PARAMS. */
4470 : :
4471 : : tree
4472 : 3953508 : cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4473 : : tsubst_flags_t complain, tree orig_fndecl)
4474 : : {
4475 : 3953508 : tree fntype, fndecl;
4476 : 3953508 : int is_method;
4477 : 3953508 : tree original = function;
4478 : 3953508 : int nargs;
4479 : 3953508 : tree *argarray;
4480 : 3953508 : tree parm_types;
4481 : 3953508 : vec<tree, va_gc> *allocated = NULL;
4482 : 3953508 : tree ret;
4483 : :
4484 : : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4485 : : expressions, like those used for ObjC messenger dispatches. */
4486 : 3953508 : if (params != NULL && !vec_safe_is_empty (*params))
4487 : 1762682 : function = objc_rewrite_function_call (function, (**params)[0]);
4488 : :
4489 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4490 : : Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4491 : 3953508 : if (TREE_CODE (function) == NOP_EXPR
4492 : 3953508 : && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4493 : 15 : function = TREE_OPERAND (function, 0);
4494 : :
4495 : 3953508 : if (TREE_CODE (function) == FUNCTION_DECL)
4496 : : {
4497 : 1380203 : if (!mark_used (function, complain))
4498 : 24 : return error_mark_node;
4499 : 1380179 : fndecl = function;
4500 : :
4501 : : /* For target_version semantics, the function set cannot be called
4502 : : if there is no default version in scope. */
4503 : 1380179 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
4504 : : && !is_function_default_version (fndecl))
4505 : : {
4506 : : if (complain & tf_error)
4507 : : error ("no default version in scope");
4508 : : return error_mark_node;
4509 : : }
4510 : :
4511 : : /* Convert anything with function type to a pointer-to-function. */
4512 : 1380179 : if (DECL_MAIN_P (function))
4513 : : {
4514 : 0 : if (complain & tf_error)
4515 : 0 : pedwarn (input_location, OPT_Wpedantic,
4516 : : "ISO C++ forbids calling %<::main%> from within program");
4517 : : else
4518 : 0 : return error_mark_node;
4519 : : }
4520 : 1380179 : function = build_addr_func (function, complain);
4521 : : }
4522 : : else
4523 : : {
4524 : 2573305 : fndecl = NULL_TREE;
4525 : :
4526 : 2573305 : function = build_addr_func (function, complain);
4527 : : }
4528 : :
4529 : 3953484 : if (function == error_mark_node)
4530 : : return error_mark_node;
4531 : :
4532 : 3953382 : fntype = TREE_TYPE (function);
4533 : :
4534 : 3953382 : if (TYPE_PTRMEMFUNC_P (fntype))
4535 : : {
4536 : 15 : if (complain & tf_error)
4537 : 15 : error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4538 : : "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4539 : : original, original);
4540 : 15 : return error_mark_node;
4541 : : }
4542 : :
4543 : 7906734 : is_method = (TYPE_PTR_P (fntype)
4544 : 3953367 : && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4545 : :
4546 : 3953367 : if (!(TYPE_PTRFN_P (fntype)
4547 : 91507 : || is_method
4548 : 1413 : || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4549 : : {
4550 : 1413 : if (complain & tf_error)
4551 : : {
4552 : 174 : if (is_stub_object (original))
4553 : 3 : error_at (input_location,
4554 : : "%qT cannot be used as a function",
4555 : 3 : TREE_TYPE (original));
4556 : 171 : else if (!flag_diagnostics_show_caret)
4557 : 171 : error_at (input_location,
4558 : : "%qE cannot be used as a function", original);
4559 : 0 : else if (DECL_P (original))
4560 : 0 : error_at (input_location,
4561 : : "%qD cannot be used as a function", original);
4562 : : else
4563 : 0 : error_at (input_location,
4564 : : "expression cannot be used as a function");
4565 : : }
4566 : :
4567 : 1413 : return error_mark_node;
4568 : : }
4569 : :
4570 : : /* fntype now gets the type of function pointed to. */
4571 : 3951954 : fntype = TREE_TYPE (fntype);
4572 : 3951954 : parm_types = TYPE_ARG_TYPES (fntype);
4573 : :
4574 : 3951954 : if (params == NULL)
4575 : : {
4576 : 162507 : allocated = make_tree_vector ();
4577 : 162507 : params = &allocated;
4578 : : }
4579 : :
4580 : 3951954 : nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4581 : : complain);
4582 : 3951954 : if (nargs < 0)
4583 : 1654 : return error_mark_node;
4584 : :
4585 : 3950300 : argarray = (*params)->address ();
4586 : :
4587 : : /* Check for errors in format strings and inappropriately
4588 : : null parameters. */
4589 : 3950300 : bool warned_p
4590 : 3950300 : = ((complain & tf_warning)
4591 : 3950300 : && check_function_arguments (input_location, fndecl, fntype,
4592 : : nargs, argarray, NULL,
4593 : 3950300 : cp_comp_parm_types));
4594 : :
4595 : 3950300 : ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4596 : :
4597 : 3950300 : if (warned_p)
4598 : : {
4599 : 27 : tree c = extract_call_expr (ret);
4600 : 27 : if (TREE_CODE (c) == CALL_EXPR)
4601 : 27 : suppress_warning (c, OPT_Wnonnull);
4602 : : }
4603 : :
4604 : 3950300 : if (allocated != NULL)
4605 : 162507 : release_tree_vector (allocated);
4606 : :
4607 : : return ret;
4608 : : }
4609 : :
4610 : : /* Subroutine of convert_arguments.
4611 : : Print an error message about a wrong number of arguments. */
4612 : :
4613 : : static void
4614 : 162 : error_args_num (location_t loc, tree fndecl, bool too_many_p)
4615 : : {
4616 : 162 : if (fndecl)
4617 : : {
4618 : 126 : auto_diagnostic_group d;
4619 : 126 : if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4620 : : {
4621 : 0 : if (DECL_NAME (fndecl) == NULL_TREE
4622 : 0 : || (DECL_NAME (fndecl)
4623 : 0 : == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4624 : 0 : error_at (loc,
4625 : : too_many_p
4626 : : ? G_("too many arguments to constructor %q#D")
4627 : : : G_("too few arguments to constructor %q#D"),
4628 : : fndecl);
4629 : : else
4630 : 0 : error_at (loc,
4631 : : too_many_p
4632 : : ? G_("too many arguments to member function %q#D")
4633 : : : G_("too few arguments to member function %q#D"),
4634 : : fndecl);
4635 : : }
4636 : : else
4637 : 189 : error_at (loc,
4638 : : too_many_p
4639 : : ? G_("too many arguments to function %q#D")
4640 : : : G_("too few arguments to function %q#D"),
4641 : : fndecl);
4642 : 126 : if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4643 : 87 : inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4644 : 126 : }
4645 : : else
4646 : : {
4647 : 36 : if (c_dialect_objc () && objc_message_selector ())
4648 : 0 : error_at (loc,
4649 : : too_many_p
4650 : : ? G_("too many arguments to method %q#D")
4651 : : : G_("too few arguments to method %q#D"),
4652 : : objc_message_selector ());
4653 : : else
4654 : 57 : error_at (loc, too_many_p ? G_("too many arguments to function")
4655 : : : G_("too few arguments to function"));
4656 : : }
4657 : 162 : }
4658 : :
4659 : : /* Convert the actual parameter expressions in the list VALUES to the
4660 : : types in the list TYPELIST. The converted expressions are stored
4661 : : back in the VALUES vector.
4662 : : If parmdecls is exhausted, or when an element has NULL as its type,
4663 : : perform the default conversions.
4664 : :
4665 : : NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4666 : :
4667 : : This is also where warnings about wrong number of args are generated.
4668 : :
4669 : : Returns the actual number of arguments processed (which might be less
4670 : : than the length of the vector), or -1 on error.
4671 : :
4672 : : In C++, unspecified trailing parameters can be filled in with their
4673 : : default arguments, if such were specified. Do so here. */
4674 : :
4675 : : static int
4676 : 3951954 : convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4677 : : int flags, tsubst_flags_t complain)
4678 : : {
4679 : 3951954 : tree typetail;
4680 : 3951954 : unsigned int i;
4681 : :
4682 : : /* Argument passing is always copy-initialization. */
4683 : 3951954 : flags |= LOOKUP_ONLYCONVERTING;
4684 : :
4685 : 7916649 : for (i = 0, typetail = typelist;
4686 : 7916649 : i < vec_safe_length (*values);
4687 : : i++)
4688 : : {
4689 : 7931935 : tree type = typetail ? TREE_VALUE (typetail) : 0;
4690 : 3966192 : tree val = (**values)[i];
4691 : :
4692 : 3966192 : if (val == error_mark_node || type == error_mark_node)
4693 : : return -1;
4694 : :
4695 : 3966186 : if (type == void_type_node)
4696 : : {
4697 : 206 : if (complain & tf_error)
4698 : 78 : error_args_num (input_location, fndecl, /*too_many_p=*/true);
4699 : 206 : return -1;
4700 : : }
4701 : :
4702 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4703 : : Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4704 : 3965980 : if (TREE_CODE (val) == NOP_EXPR
4705 : 958187 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4706 : 3965998 : && (type == 0 || !TYPE_REF_P (type)))
4707 : 18 : val = TREE_OPERAND (val, 0);
4708 : :
4709 : 3965980 : if (type == 0 || !TYPE_REF_P (type))
4710 : : {
4711 : 3744543 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4712 : 3744543 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4713 : 10905 : val = decay_conversion (val, complain);
4714 : : }
4715 : :
4716 : 3965980 : if (val == error_mark_node)
4717 : : return -1;
4718 : :
4719 : 3965980 : if (type != 0)
4720 : : {
4721 : : /* Formal parm type is specified by a function prototype. */
4722 : 3965531 : tree parmval;
4723 : :
4724 : 3965531 : if (!COMPLETE_TYPE_P (complete_type (type)))
4725 : : {
4726 : 18 : if (complain & tf_error)
4727 : : {
4728 : 12 : location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4729 : 12 : if (fndecl)
4730 : : {
4731 : 12 : auto_diagnostic_group d;
4732 : 12 : error_at (loc,
4733 : : "parameter %P of %qD has incomplete type %qT",
4734 : : i, fndecl, type);
4735 : 12 : inform (get_fndecl_argument_location (fndecl, i),
4736 : : " declared here");
4737 : 12 : }
4738 : : else
4739 : 0 : error_at (loc, "parameter %P has incomplete type %qT", i,
4740 : : type);
4741 : : }
4742 : 18 : parmval = error_mark_node;
4743 : : }
4744 : : else
4745 : : {
4746 : 3965513 : parmval = convert_for_initialization
4747 : 3965513 : (NULL_TREE, type, val, flags,
4748 : : ICR_ARGPASS, fndecl, i, complain);
4749 : 3965513 : parmval = convert_for_arg_passing (type, parmval, complain);
4750 : : }
4751 : :
4752 : 3965531 : if (parmval == error_mark_node)
4753 : : return -1;
4754 : :
4755 : 3964246 : (**values)[i] = parmval;
4756 : : }
4757 : : else
4758 : : {
4759 : 449 : int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4760 : 21 : if (magic)
4761 : : {
4762 : : /* Don't truncate excess precision to the semantic type. */
4763 : 0 : if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4764 : 0 : val = TREE_OPERAND (val, 0);
4765 : : /* Don't do ellipsis conversion for __built_in_constant_p
4766 : : as this will result in spurious errors for non-trivial
4767 : : types. */
4768 : 0 : val = require_complete_type (val, complain);
4769 : : }
4770 : : else
4771 : 449 : val = convert_arg_to_ellipsis (val, complain);
4772 : :
4773 : 449 : (**values)[i] = val;
4774 : : }
4775 : :
4776 : 3964695 : if (typetail)
4777 : 3964246 : typetail = TREE_CHAIN (typetail);
4778 : : }
4779 : :
4780 : 3950457 : if (typetail != 0 && typetail != void_list_node)
4781 : : {
4782 : : /* See if there are default arguments that can be used. Because
4783 : : we hold default arguments in the FUNCTION_TYPE (which is so
4784 : : wrong), we can see default parameters here from deduced
4785 : : contexts (and via typeof) for indirect function calls.
4786 : : Fortunately we know whether we have a function decl to
4787 : : provide default arguments in a language conformant
4788 : : manner. */
4789 : 63 : if (fndecl && TREE_PURPOSE (typetail)
4790 : 160 : && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4791 : : {
4792 : 6 : for (; typetail != void_list_node; ++i)
4793 : : {
4794 : : /* After DR777, with explicit template args we can end up with a
4795 : : default argument followed by no default argument. */
4796 : 6 : if (!TREE_PURPOSE (typetail))
4797 : : break;
4798 : 3 : tree parmval
4799 : 3 : = convert_default_arg (TREE_VALUE (typetail),
4800 : 3 : TREE_PURPOSE (typetail),
4801 : 3 : fndecl, i, complain);
4802 : :
4803 : 3 : if (parmval == error_mark_node)
4804 : 0 : return -1;
4805 : :
4806 : 3 : vec_safe_push (*values, parmval);
4807 : 3 : typetail = TREE_CHAIN (typetail);
4808 : : /* ends with `...'. */
4809 : 3 : if (typetail == NULL_TREE)
4810 : : break;
4811 : : }
4812 : : }
4813 : :
4814 : 157 : if (typetail && typetail != void_list_node)
4815 : : {
4816 : 157 : if (complain & tf_error)
4817 : 84 : error_args_num (input_location, fndecl, /*too_many_p=*/false);
4818 : 157 : return -1;
4819 : : }
4820 : : }
4821 : :
4822 : 3950300 : return (int) i;
4823 : : }
4824 : :
4825 : : /* Build a binary-operation expression, after performing default
4826 : : conversions on the operands. CODE is the kind of expression to
4827 : : build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4828 : : are the tree codes which correspond to ARG1 and ARG2 when issuing
4829 : : warnings about possibly misplaced parentheses. They may differ
4830 : : from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4831 : : folding (e.g., if the parser sees "a | 1 + 1", it may call this
4832 : : routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4833 : : To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4834 : : ARG2_CODE as ERROR_MARK. */
4835 : :
4836 : : tree
4837 : 199193925 : build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4838 : : enum tree_code arg1_code, tree arg2,
4839 : : enum tree_code arg2_code, tree lookups,
4840 : : tree *overload_p, tsubst_flags_t complain)
4841 : : {
4842 : 199193925 : tree orig_arg1;
4843 : 199193925 : tree orig_arg2;
4844 : 199193925 : tree expr;
4845 : 199193925 : tree overload = NULL_TREE;
4846 : :
4847 : 199193925 : orig_arg1 = arg1;
4848 : 199193925 : orig_arg2 = arg2;
4849 : :
4850 : 199193925 : if (processing_template_decl)
4851 : : {
4852 : 117823483 : if (type_dependent_expression_p (arg1)
4853 : 117823483 : || type_dependent_expression_p (arg2))
4854 : : {
4855 : 83950959 : expr = build_min_nt_loc (loc, code, arg1, arg2);
4856 : 83950959 : TREE_TYPE (expr)
4857 : 83950959 : = build_dependent_operator_type (lookups, code, false);
4858 : 83950959 : return expr;
4859 : : }
4860 : : }
4861 : :
4862 : 115242966 : if (code == DOTSTAR_EXPR)
4863 : 90478 : expr = build_m_component_ref (arg1, arg2, complain);
4864 : : else
4865 : 115152488 : expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4866 : : lookups, &overload, complain);
4867 : :
4868 : 115242933 : if (overload_p != NULL)
4869 : 48063319 : *overload_p = overload;
4870 : :
4871 : : /* Check for cases such as x+y<<z which users are likely to
4872 : : misinterpret. But don't warn about obj << x + y, since that is a
4873 : : common idiom for I/O. */
4874 : 115242933 : if (warn_parentheses
4875 : 1146446 : && (complain & tf_warning)
4876 : 1098312 : && !processing_template_decl
4877 : 761295 : && !error_operand_p (arg1)
4878 : 761289 : && !error_operand_p (arg2)
4879 : 116004210 : && (code != LSHIFT_EXPR
4880 : 68872 : || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4881 : 760274 : warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4882 : : arg2_code, orig_arg2);
4883 : :
4884 : 115242933 : if (processing_template_decl && expr != error_mark_node)
4885 : : {
4886 : 33872333 : if (overload != NULL_TREE)
4887 : 2355778 : return (build_min_non_dep_op_overload
4888 : 2355778 : (code, expr, overload, orig_arg1, orig_arg2));
4889 : :
4890 : 31516555 : return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4891 : : }
4892 : :
4893 : : return expr;
4894 : : }
4895 : :
4896 : : /* Build and return an ARRAY_REF expression. */
4897 : :
4898 : : tree
4899 : 1996998 : build_x_array_ref (location_t loc, tree arg1, tree arg2,
4900 : : tsubst_flags_t complain)
4901 : : {
4902 : 1996998 : tree orig_arg1 = arg1;
4903 : 1996998 : tree orig_arg2 = arg2;
4904 : 1996998 : tree expr;
4905 : 1996998 : tree overload = NULL_TREE;
4906 : :
4907 : 1996998 : if (processing_template_decl)
4908 : : {
4909 : 688 : if (type_dependent_expression_p (arg1)
4910 : 688 : || type_dependent_expression_p (arg2))
4911 : 655 : return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4912 : 655 : NULL_TREE, NULL_TREE);
4913 : : }
4914 : :
4915 : 1996343 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4916 : : NULL_TREE, NULL_TREE, &overload, complain);
4917 : :
4918 : 1996343 : if (processing_template_decl && expr != error_mark_node)
4919 : : {
4920 : 30 : if (overload != NULL_TREE)
4921 : 16 : return (build_min_non_dep_op_overload
4922 : 16 : (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4923 : :
4924 : 14 : return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4925 : 14 : NULL_TREE, NULL_TREE);
4926 : : }
4927 : : return expr;
4928 : : }
4929 : :
4930 : : /* Build an OpenMP array section reference, creating an exact type for the
4931 : : resulting expression based on the element type and bounds if possible. If
4932 : : we have variable bounds, create an incomplete array type for the result
4933 : : instead. */
4934 : :
4935 : : tree
4936 : 10452 : build_omp_array_section (location_t loc, tree array_expr, tree index,
4937 : : tree length)
4938 : : {
4939 : 10452 : if (TREE_CODE (array_expr) == TYPE_DECL
4940 : 10452 : || type_dependent_expression_p (array_expr))
4941 : 258 : return build3_loc (loc, OMP_ARRAY_SECTION, NULL_TREE, array_expr, index,
4942 : 258 : length);
4943 : :
4944 : 10194 : tree type = TREE_TYPE (array_expr);
4945 : 10194 : gcc_assert (type);
4946 : 10194 : type = non_reference (type);
4947 : :
4948 : 10194 : tree sectype, eltype = TREE_TYPE (type);
4949 : :
4950 : : /* It's not an array or pointer type. Just reuse the type of the
4951 : : original expression as the type of the array section (an error will be
4952 : : raised anyway, later). */
4953 : 10194 : if (eltype == NULL_TREE)
4954 : 39 : sectype = TREE_TYPE (array_expr);
4955 : : else
4956 : : {
4957 : 10155 : tree idxtype = NULL_TREE;
4958 : :
4959 : : /* If we know the integer bounds, create an index type with exact
4960 : : low/high (or zero/length) bounds. Otherwise, create an incomplete
4961 : : array type. (This mostly only affects diagnostics.) */
4962 : 10155 : if (index != NULL_TREE
4963 : 10155 : && length != NULL_TREE
4964 : 7331 : && TREE_CODE (index) == INTEGER_CST
4965 : 6537 : && TREE_CODE (length) == INTEGER_CST)
4966 : : {
4967 : 5042 : tree low = fold_convert (sizetype, index);
4968 : 5042 : tree high = fold_convert (sizetype, length);
4969 : 5042 : high = size_binop (PLUS_EXPR, low, high);
4970 : 5042 : high = size_binop (MINUS_EXPR, high, size_one_node);
4971 : 5042 : idxtype = build_range_type (sizetype, low, high);
4972 : 5042 : }
4973 : 2897 : else if ((index == NULL_TREE || integer_zerop (index))
4974 : 3527 : && length != NULL_TREE
4975 : 7235 : && TREE_CODE (length) == INTEGER_CST)
4976 : 1411 : idxtype = build_index_type (length);
4977 : :
4978 : 10155 : sectype = build_array_type (eltype, idxtype);
4979 : : }
4980 : :
4981 : 10194 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array_expr, index,
4982 : 10194 : length);
4983 : : }
4984 : :
4985 : : /* Return whether OP is an expression of enum type cast to integer
4986 : : type. In C++ even unsigned enum types are cast to signed integer
4987 : : types. We do not want to issue warnings about comparisons between
4988 : : signed and unsigned types when one of the types is an enum type.
4989 : : Those warnings are always false positives in practice. */
4990 : :
4991 : : static bool
4992 : 593486 : enum_cast_to_int (tree op)
4993 : : {
4994 : 581340 : if (CONVERT_EXPR_P (op)
4995 : 13182 : && TREE_TYPE (op) == integer_type_node
4996 : 821 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4997 : 593501 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4998 : : return true;
4999 : :
5000 : : /* The cast may have been pushed into a COND_EXPR. */
5001 : 593479 : if (TREE_CODE (op) == COND_EXPR)
5002 : 761 : return (enum_cast_to_int (TREE_OPERAND (op, 1))
5003 : 761 : || enum_cast_to_int (TREE_OPERAND (op, 2)));
5004 : :
5005 : : return false;
5006 : : }
5007 : :
5008 : : /* For the c-common bits. */
5009 : : tree
5010 : 4071588 : build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
5011 : : bool /*convert_p*/)
5012 : : {
5013 : 4071588 : return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
5014 : : }
5015 : :
5016 : : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
5017 : : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
5018 : :
5019 : : static tree
5020 : 7163 : build_vec_cmp (tree_code code, tree type,
5021 : : tree arg0, tree arg1)
5022 : : {
5023 : 7163 : tree zero_vec = build_zero_cst (type);
5024 : 7163 : tree minus_one_vec = build_minus_one_cst (type);
5025 : 7163 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
5026 : 7163 : tree cmp = build2 (code, cmp_type, arg0, arg1);
5027 : 7163 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
5028 : : }
5029 : :
5030 : : /* Possibly warn about an address never being NULL. */
5031 : :
5032 : : static void
5033 : 1587643 : warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
5034 : : {
5035 : : /* Prevent warnings issued for macro expansion. */
5036 : 1587643 : if (!warn_address
5037 : 40725 : || (complain & tf_warning) == 0
5038 : 24371 : || c_inhibit_evaluation_warnings != 0
5039 : 24331 : || from_macro_expansion_at (location)
5040 : 1605360 : || warning_suppressed_p (op, OPT_Waddress))
5041 : 1570249 : return;
5042 : :
5043 : 17684 : tree cop = fold_for_warn (op);
5044 : :
5045 : 17684 : if (TREE_CODE (cop) == NON_LVALUE_EXPR)
5046 : : /* Unwrap the expression for C++ 98. */
5047 : 1 : cop = TREE_OPERAND (cop, 0);
5048 : :
5049 : 17684 : if (TREE_CODE (cop) == PTRMEM_CST)
5050 : : {
5051 : : /* The address of a nonstatic data member is never null. */
5052 : 30 : warning_at (location, OPT_Waddress,
5053 : : "the address %qE will never be NULL",
5054 : : cop);
5055 : 30 : return;
5056 : : }
5057 : :
5058 : 17654 : if (TREE_CODE (cop) == NOP_EXPR)
5059 : : {
5060 : : /* Allow casts to intptr_t to suppress the warning. */
5061 : 1210 : tree type = TREE_TYPE (cop);
5062 : 1210 : if (TREE_CODE (type) == INTEGER_TYPE)
5063 : : return;
5064 : :
5065 : 1210 : STRIP_NOPS (cop);
5066 : : }
5067 : :
5068 : 17654 : auto_diagnostic_group d;
5069 : 17654 : bool warned = false;
5070 : 17654 : if (TREE_CODE (cop) == ADDR_EXPR)
5071 : : {
5072 : 789 : cop = TREE_OPERAND (cop, 0);
5073 : :
5074 : : /* Set to true in the loop below if OP dereferences its operand.
5075 : : In such a case the ultimate target need not be a decl for
5076 : : the null [in]equality test to be necessarily constant. */
5077 : 789 : bool deref = false;
5078 : :
5079 : : /* Get the outermost array or object, or member. */
5080 : 960 : while (handled_component_p (cop))
5081 : : {
5082 : 270 : if (TREE_CODE (cop) == COMPONENT_REF)
5083 : : {
5084 : : /* Get the member (its address is never null). */
5085 : 99 : cop = TREE_OPERAND (cop, 1);
5086 : 99 : break;
5087 : : }
5088 : :
5089 : : /* Get the outer array/object to refer to in the warning. */
5090 : 171 : cop = TREE_OPERAND (cop, 0);
5091 : 171 : deref = true;
5092 : : }
5093 : :
5094 : 639 : if ((!deref && !decl_with_nonnull_addr_p (cop))
5095 : 631 : || from_macro_expansion_at (location)
5096 : 1420 : || warning_suppressed_p (cop, OPT_Waddress))
5097 : 158 : return;
5098 : :
5099 : 631 : warned = warning_at (location, OPT_Waddress,
5100 : : "the address of %qD will never be NULL", cop);
5101 : 631 : op = cop;
5102 : : }
5103 : 16865 : else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
5104 : : {
5105 : : /* Adding zero to the null pointer is well-defined in C++. When
5106 : : the offset is unknown (i.e., not a constant) warn anyway since
5107 : : it's less likely that the pointer operand is null than not. */
5108 : 102 : tree off = TREE_OPERAND (cop, 1);
5109 : 102 : if (!integer_zerop (off)
5110 : 102 : && !warning_suppressed_p (cop, OPT_Waddress))
5111 : : {
5112 : 102 : tree base = TREE_OPERAND (cop, 0);
5113 : 102 : STRIP_NOPS (base);
5114 : 102 : if (TYPE_REF_P (TREE_TYPE (base)))
5115 : 12 : warning_at (location, OPT_Waddress, "the compiler can assume that "
5116 : : "the address of %qE will never be NULL", base);
5117 : : else
5118 : 90 : warning_at (location, OPT_Waddress, "comparing the result of "
5119 : : "pointer addition %qE and NULL", cop);
5120 : : }
5121 : 102 : return;
5122 : : }
5123 : 15834 : else if (CONVERT_EXPR_P (op)
5124 : 16825 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
5125 : : {
5126 : 62 : STRIP_NOPS (op);
5127 : :
5128 : 62 : if (TREE_CODE (op) == COMPONENT_REF)
5129 : 24 : op = TREE_OPERAND (op, 1);
5130 : :
5131 : 62 : if (DECL_P (op))
5132 : 62 : warned = warning_at (location, OPT_Waddress,
5133 : : "the compiler can assume that the address of "
5134 : : "%qD will never be NULL", op);
5135 : : }
5136 : :
5137 : 693 : if (warned && DECL_P (op))
5138 : 657 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
5139 : 17654 : }
5140 : :
5141 : : /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5142 : : the other operand is of a different enumeration type or a floating-point
5143 : : type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5144 : : code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5145 : : and LOC is the location for the whole binary expression.
5146 : : For C++26 this is ill-formed rather than deprecated.
5147 : : Return true for SFINAE errors.
5148 : : TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5149 : :
5150 : : static bool
5151 : 95113683 : do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5152 : : tree type1, tsubst_flags_t complain)
5153 : : {
5154 : 95113683 : if (TREE_CODE (type0) == ENUMERAL_TYPE
5155 : 2802118 : && TREE_CODE (type1) == ENUMERAL_TYPE
5156 : 97397058 : && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
5157 : : {
5158 : 198 : if (cxx_dialect >= cxx26)
5159 : : {
5160 : 60 : if ((complain & tf_warning_or_error) == 0)
5161 : : return true;
5162 : : }
5163 : 138 : else if ((complain & tf_warning) == 0)
5164 : : return false;
5165 : : /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5166 : : Otherwise, warn if -Wenum-conversion is on. */
5167 : 190 : enum opt_code opt;
5168 : 190 : if (warn_deprecated_enum_enum_conv)
5169 : : opt = OPT_Wdeprecated_enum_enum_conversion;
5170 : 97 : else if (warn_enum_conversion)
5171 : : opt = OPT_Wenum_conversion;
5172 : : else
5173 : : return false;
5174 : :
5175 : 115 : switch (code)
5176 : : {
5177 : : case GT_EXPR:
5178 : : case LT_EXPR:
5179 : : case GE_EXPR:
5180 : : case LE_EXPR:
5181 : : case EQ_EXPR:
5182 : : case NE_EXPR:
5183 : : /* Comparisons are handled by -Wenum-compare. */
5184 : : return false;
5185 : : case SPACESHIP_EXPR:
5186 : : /* This is invalid, don't warn. */
5187 : : return false;
5188 : 17 : case BIT_AND_EXPR:
5189 : 17 : case BIT_IOR_EXPR:
5190 : 17 : case BIT_XOR_EXPR:
5191 : 17 : if (cxx_dialect >= cxx26)
5192 : 5 : pedwarn (loc, opt, "bitwise operation between different "
5193 : : "enumeration types %qT and %qT", type0, type1);
5194 : : else
5195 : 12 : warning_at (loc, opt, "bitwise operation between different "
5196 : : "enumeration types %qT and %qT is deprecated",
5197 : : type0, type1);
5198 : 17 : return false;
5199 : 41 : default:
5200 : 41 : if (cxx_dialect >= cxx26)
5201 : 12 : pedwarn (loc, opt, "arithmetic between different enumeration "
5202 : : "types %qT and %qT", type0, type1);
5203 : : else
5204 : 29 : warning_at (loc, opt, "arithmetic between different enumeration "
5205 : : "types %qT and %qT is deprecated", type0, type1);
5206 : 41 : return false;
5207 : : }
5208 : : }
5209 : 95113485 : else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5210 : 2801920 : && SCALAR_FLOAT_TYPE_P (type1))
5211 : 95113408 : || (SCALAR_FLOAT_TYPE_P (type0)
5212 : 32837411 : && TREE_CODE (type1) == ENUMERAL_TYPE))
5213 : : {
5214 : 154 : if (cxx_dialect >= cxx26)
5215 : : {
5216 : 46 : if ((complain & tf_warning_or_error) == 0)
5217 : : return true;
5218 : : }
5219 : 108 : else if ((complain & tf_warning) == 0)
5220 : : return false;
5221 : 142 : const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5222 : : /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5223 : : Otherwise, warn if -Wenum-conversion is on. */
5224 : 142 : enum opt_code opt;
5225 : 142 : if (warn_deprecated_enum_float_conv)
5226 : : opt = OPT_Wdeprecated_enum_float_conversion;
5227 : 75 : else if (warn_enum_conversion)
5228 : : opt = OPT_Wenum_conversion;
5229 : : else
5230 : : return false;
5231 : :
5232 : 84 : switch (code)
5233 : : {
5234 : 34 : case GT_EXPR:
5235 : 34 : case LT_EXPR:
5236 : 34 : case GE_EXPR:
5237 : 34 : case LE_EXPR:
5238 : 34 : case EQ_EXPR:
5239 : 34 : case NE_EXPR:
5240 : 34 : if (enum_first_p && cxx_dialect >= cxx26)
5241 : 5 : pedwarn (loc, opt, "comparison of enumeration type %qT with "
5242 : : "floating-point type %qT", type0, type1);
5243 : 29 : else if (cxx_dialect >= cxx26)
5244 : 5 : pedwarn (loc, opt, "comparison of floating-point type %qT "
5245 : : "with enumeration type %qT", type0, type1);
5246 : 24 : else if (enum_first_p)
5247 : 12 : warning_at (loc, opt, "comparison of enumeration type %qT with "
5248 : : "floating-point type %qT is deprecated",
5249 : : type0, type1);
5250 : : else
5251 : 12 : warning_at (loc, opt, "comparison of floating-point type %qT "
5252 : : "with enumeration type %qT is deprecated",
5253 : : type0, type1);
5254 : 34 : return false;
5255 : : case SPACESHIP_EXPR:
5256 : : /* This is invalid, don't warn. */
5257 : : return false;
5258 : 38 : default:
5259 : 38 : if (enum_first_p && cxx_dialect >= cxx26)
5260 : 5 : pedwarn (loc, opt, "arithmetic between enumeration type %qT "
5261 : : "and floating-point type %qT", type0, type1);
5262 : 33 : else if (cxx_dialect >= cxx26)
5263 : 6 : pedwarn (loc, opt, "arithmetic between floating-point type %qT "
5264 : : "and enumeration type %qT", type0, type1);
5265 : 27 : else if (enum_first_p)
5266 : 12 : warning_at (loc, opt, "arithmetic between enumeration type %qT "
5267 : : "and floating-point type %qT is deprecated",
5268 : : type0, type1);
5269 : : else
5270 : 15 : warning_at (loc, opt, "arithmetic between floating-point type %qT "
5271 : : "and enumeration type %qT is deprecated",
5272 : : type0, type1);
5273 : 38 : return false;
5274 : : }
5275 : : }
5276 : : return false;
5277 : : }
5278 : :
5279 : : /* Build a binary-operation expression without default conversions.
5280 : : CODE is the kind of expression to build.
5281 : : LOCATION is the location_t of the operator in the source code.
5282 : : This function differs from `build' in several ways:
5283 : : the data type of the result is computed and recorded in it,
5284 : : warnings are generated if arg data types are invalid,
5285 : : special handling for addition and subtraction of pointers is known,
5286 : : and some optimization is done (operations on narrow ints
5287 : : are done in the narrower type when that gives the same result).
5288 : : Constant folding is also done before the result is returned.
5289 : :
5290 : : Note that the operands will never have enumeral types
5291 : : because either they have just had the default conversions performed
5292 : : or they have both just been converted to some other type in which
5293 : : the arithmetic is to be done.
5294 : :
5295 : : C++: must do special pointer arithmetic when implementing
5296 : : multiple inheritance, and deal with pointer to member functions. */
5297 : :
5298 : : tree
5299 : 125389998 : cp_build_binary_op (const op_location_t &location,
5300 : : enum tree_code code, tree orig_op0, tree orig_op1,
5301 : : tsubst_flags_t complain)
5302 : : {
5303 : 125389998 : tree op0, op1;
5304 : 125389998 : enum tree_code code0, code1;
5305 : 125389998 : tree type0, type1, orig_type0, orig_type1;
5306 : 125389998 : const char *invalid_op_diag;
5307 : :
5308 : : /* Expression code to give to the expression when it is built.
5309 : : Normally this is CODE, which is what the caller asked for,
5310 : : but in some special cases we change it. */
5311 : 125389998 : enum tree_code resultcode = code;
5312 : :
5313 : : /* Data type in which the computation is to be performed.
5314 : : In the simplest cases this is the common type of the arguments. */
5315 : 125389998 : tree result_type = NULL_TREE;
5316 : :
5317 : : /* When the computation is in excess precision, the type of the
5318 : : final EXCESS_PRECISION_EXPR. */
5319 : 125389998 : tree semantic_result_type = NULL;
5320 : :
5321 : : /* Nonzero means operands have already been type-converted
5322 : : in whatever way is necessary.
5323 : : Zero means they need to be converted to RESULT_TYPE. */
5324 : 125389998 : int converted = 0;
5325 : :
5326 : : /* Nonzero means create the expression with this type, rather than
5327 : : RESULT_TYPE. */
5328 : 125389998 : tree build_type = 0;
5329 : :
5330 : : /* Nonzero means after finally constructing the expression
5331 : : convert it to this type. */
5332 : 125389998 : tree final_type = 0;
5333 : :
5334 : 125389998 : tree result;
5335 : :
5336 : : /* Nonzero if this is an operation like MIN or MAX which can
5337 : : safely be computed in short if both args are promoted shorts.
5338 : : Also implies COMMON.
5339 : : -1 indicates a bitwise operation; this makes a difference
5340 : : in the exact conditions for when it is safe to do the operation
5341 : : in a narrower mode. */
5342 : 125389998 : int shorten = 0;
5343 : :
5344 : : /* Nonzero if this is a comparison operation;
5345 : : if both args are promoted shorts, compare the original shorts.
5346 : : Also implies COMMON. */
5347 : 125389998 : int short_compare = 0;
5348 : :
5349 : : /* Nonzero if this is a right-shift operation, which can be computed on the
5350 : : original short and then promoted if the operand is a promoted short. */
5351 : 125389998 : int short_shift = 0;
5352 : :
5353 : : /* Nonzero means set RESULT_TYPE to the common type of the args. */
5354 : 125389998 : int common = 0;
5355 : :
5356 : : /* True if both operands have arithmetic type. */
5357 : 125389998 : bool arithmetic_types_p;
5358 : :
5359 : : /* Remember whether we're doing / or %. */
5360 : 125389998 : bool doing_div_or_mod = false;
5361 : :
5362 : : /* Remember whether we're doing << or >>. */
5363 : 125389998 : bool doing_shift = false;
5364 : :
5365 : : /* Tree holding instrumentation expression. */
5366 : 125389998 : tree instrument_expr = NULL_TREE;
5367 : :
5368 : : /* True means this is an arithmetic operation that may need excess
5369 : : precision. */
5370 : 125389998 : bool may_need_excess_precision;
5371 : :
5372 : : /* Apply default conversions. */
5373 : 125389998 : op0 = resolve_nondeduced_context (orig_op0, complain);
5374 : 125389998 : op1 = resolve_nondeduced_context (orig_op1, complain);
5375 : :
5376 : 125389998 : if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5377 : 113990876 : || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5378 : 111242655 : || code == TRUTH_XOR_EXPR)
5379 : : {
5380 : 14147343 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5381 : 14147325 : op0 = decay_conversion (op0, complain);
5382 : 14147343 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5383 : 14147343 : op1 = decay_conversion (op1, complain);
5384 : : }
5385 : : else
5386 : : {
5387 : 111242655 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5388 : 111242640 : op0 = cp_default_conversion (op0, complain);
5389 : 111242655 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5390 : 111242652 : op1 = cp_default_conversion (op1, complain);
5391 : : }
5392 : :
5393 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5394 : 132869036 : STRIP_TYPE_NOPS (op0);
5395 : 153600833 : STRIP_TYPE_NOPS (op1);
5396 : :
5397 : : /* DTRT if one side is an overloaded function, but complain about it. */
5398 : 125389998 : if (type_unknown_p (op0))
5399 : : {
5400 : 9 : tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5401 : 9 : if (t != error_mark_node)
5402 : : {
5403 : 0 : if (complain & tf_error)
5404 : 0 : permerror (location,
5405 : : "assuming cast to type %qT from overloaded function",
5406 : 0 : TREE_TYPE (t));
5407 : : op0 = t;
5408 : : }
5409 : : }
5410 : 125389998 : if (type_unknown_p (op1))
5411 : : {
5412 : 3 : tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5413 : 3 : if (t != error_mark_node)
5414 : : {
5415 : 3 : if (complain & tf_error)
5416 : 3 : permerror (location,
5417 : : "assuming cast to type %qT from overloaded function",
5418 : 3 : TREE_TYPE (t));
5419 : : op1 = t;
5420 : : }
5421 : : }
5422 : :
5423 : 125389998 : orig_type0 = type0 = TREE_TYPE (op0);
5424 : 125389998 : orig_type1 = type1 = TREE_TYPE (op1);
5425 : 125389998 : tree non_ep_op0 = op0;
5426 : 125389998 : tree non_ep_op1 = op1;
5427 : :
5428 : : /* The expression codes of the data types of the arguments tell us
5429 : : whether the arguments are integers, floating, pointers, etc. */
5430 : 125389998 : code0 = TREE_CODE (type0);
5431 : 125389998 : code1 = TREE_CODE (type1);
5432 : :
5433 : : /* If an error was already reported for one of the arguments,
5434 : : avoid reporting another error. */
5435 : 125389998 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5436 : 100 : return error_mark_node;
5437 : :
5438 : 250779796 : if ((invalid_op_diag
5439 : 125389898 : = targetm.invalid_binary_op (code, type0, type1)))
5440 : : {
5441 : 0 : if (complain & tf_error)
5442 : : {
5443 : 0 : if (code0 == REAL_TYPE
5444 : 0 : && code1 == REAL_TYPE
5445 : 0 : && (extended_float_type_p (type0)
5446 : 0 : || extended_float_type_p (type1))
5447 : 0 : && cp_compare_floating_point_conversion_ranks (type0,
5448 : : type1) == 3)
5449 : : {
5450 : 0 : rich_location richloc (line_table, location);
5451 : 0 : binary_op_error (&richloc, code, type0, type1);
5452 : 0 : }
5453 : : else
5454 : 0 : error (invalid_op_diag);
5455 : : }
5456 : 0 : return error_mark_node;
5457 : : }
5458 : :
5459 : 125389898 : switch (code)
5460 : : {
5461 : : case PLUS_EXPR:
5462 : : case MINUS_EXPR:
5463 : : case MULT_EXPR:
5464 : : case TRUNC_DIV_EXPR:
5465 : : case CEIL_DIV_EXPR:
5466 : : case FLOOR_DIV_EXPR:
5467 : : case ROUND_DIV_EXPR:
5468 : : case EXACT_DIV_EXPR:
5469 : : may_need_excess_precision = true;
5470 : : break;
5471 : 42248370 : case EQ_EXPR:
5472 : 42248370 : case NE_EXPR:
5473 : 42248370 : case LE_EXPR:
5474 : 42248370 : case GE_EXPR:
5475 : 42248370 : case LT_EXPR:
5476 : 42248370 : case GT_EXPR:
5477 : 42248370 : case SPACESHIP_EXPR:
5478 : : /* Excess precision for implicit conversions of integers to
5479 : : floating point. */
5480 : 9259875 : may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5481 : 51501652 : || ANY_INTEGRAL_TYPE_P (type1));
5482 : : break;
5483 : : default:
5484 : 125389898 : may_need_excess_precision = false;
5485 : : break;
5486 : : }
5487 : 125389898 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5488 : : {
5489 : 22254 : op0 = TREE_OPERAND (op0, 0);
5490 : 22254 : type0 = TREE_TYPE (op0);
5491 : : }
5492 : 125367644 : else if (may_need_excess_precision
5493 : 95288285 : && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5494 : 28674066 : if (tree eptype = excess_precision_type (type0))
5495 : : {
5496 : 59709 : type0 = eptype;
5497 : 59709 : op0 = convert (eptype, op0);
5498 : : }
5499 : 125389898 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5500 : : {
5501 : 28200 : op1 = TREE_OPERAND (op1, 0);
5502 : 28200 : type1 = TREE_TYPE (op1);
5503 : : }
5504 : 125361698 : else if (may_need_excess_precision
5505 : 95285534 : && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5506 : 28594792 : if (tree eptype = excess_precision_type (type1))
5507 : : {
5508 : 39047 : type1 = eptype;
5509 : 39047 : op1 = convert (eptype, op1);
5510 : : }
5511 : :
5512 : : /* Issue warnings about peculiar, but valid, uses of NULL. */
5513 : 250779711 : if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5514 : : /* It's reasonable to use pointer values as operands of &&
5515 : : and ||, so NULL is no exception. */
5516 : 12411 : && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5517 : 12396 : && ( /* Both are NULL (or 0) and the operation was not a
5518 : : comparison or a pointer subtraction. */
5519 : 12469 : (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5520 : 27 : && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5521 : : /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5522 : 12384 : || (!null_ptr_cst_p (orig_op0)
5523 : 12323 : && !TYPE_PTR_OR_PTRMEM_P (type0))
5524 : 12372 : || (!null_ptr_cst_p (orig_op1)
5525 : 46 : && !TYPE_PTR_OR_PTRMEM_P (type1)))
5526 : 125389937 : && (complain & tf_warning))
5527 : : {
5528 : 39 : location_t loc =
5529 : 39 : expansion_point_location_if_in_system_header (input_location);
5530 : :
5531 : 39 : warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5532 : : }
5533 : :
5534 : : /* In case when one of the operands of the binary operation is
5535 : : a vector and another is a scalar -- convert scalar to vector. */
5536 : 125456752 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5537 : 125455904 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5538 : : {
5539 : 933 : enum stv_conv convert_flag
5540 : 933 : = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5541 : : complain & tf_error);
5542 : :
5543 : 933 : switch (convert_flag)
5544 : : {
5545 : 21 : case stv_error:
5546 : 21 : return error_mark_node;
5547 : 55 : case stv_firstarg:
5548 : 55 : {
5549 : 55 : op0 = convert (TREE_TYPE (type1), op0);
5550 : 55 : op0 = cp_save_expr (op0);
5551 : 55 : op0 = build_vector_from_val (type1, op0);
5552 : 55 : orig_type0 = type0 = TREE_TYPE (op0);
5553 : 55 : code0 = TREE_CODE (type0);
5554 : 55 : converted = 1;
5555 : 55 : break;
5556 : : }
5557 : 752 : case stv_secondarg:
5558 : 752 : {
5559 : 752 : op1 = convert (TREE_TYPE (type0), op1);
5560 : 752 : op1 = cp_save_expr (op1);
5561 : 752 : op1 = build_vector_from_val (type0, op1);
5562 : 752 : orig_type1 = type1 = TREE_TYPE (op1);
5563 : 752 : code1 = TREE_CODE (type1);
5564 : 752 : converted = 1;
5565 : 752 : break;
5566 : : }
5567 : : default:
5568 : : break;
5569 : : }
5570 : : }
5571 : :
5572 : 125389877 : switch (code)
5573 : : {
5574 : 14648982 : case MINUS_EXPR:
5575 : : /* Subtraction of two similar pointers.
5576 : : We must subtract them as integers, then divide by object size. */
5577 : 14648982 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5578 : 15949929 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5579 : 1300947 : TREE_TYPE (type1)))
5580 : : {
5581 : 1300945 : result = pointer_diff (location, op0, op1,
5582 : : common_pointer_type (type0, type1), complain,
5583 : : &instrument_expr);
5584 : 1300945 : if (instrument_expr != NULL)
5585 : 84 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5586 : : instrument_expr, result);
5587 : :
5588 : 1300945 : return result;
5589 : : }
5590 : : /* In all other cases except pointer - int, the usual arithmetic
5591 : : rules apply. */
5592 : 13348037 : else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5593 : : {
5594 : : common = 1;
5595 : : break;
5596 : : }
5597 : : /* The pointer - int case is just like pointer + int; fall
5598 : : through. */
5599 : 19201941 : gcc_fallthrough ();
5600 : 19201941 : case PLUS_EXPR:
5601 : 19201941 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5602 : 6384111 : && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5603 : : {
5604 : 6384102 : tree ptr_operand;
5605 : 6384102 : tree int_operand;
5606 : 6384102 : ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5607 : 41057 : int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5608 : 6384102 : if (processing_template_decl)
5609 : : {
5610 : 2026870 : result_type = TREE_TYPE (ptr_operand);
5611 : 2026870 : break;
5612 : : }
5613 : 4357232 : return cp_pointer_int_sum (location, code,
5614 : : ptr_operand,
5615 : : int_operand,
5616 : 4357232 : complain);
5617 : : }
5618 : : common = 1;
5619 : : break;
5620 : :
5621 : : case MULT_EXPR:
5622 : 119697185 : common = 1;
5623 : : break;
5624 : :
5625 : 9628798 : case TRUNC_DIV_EXPR:
5626 : 9628798 : case CEIL_DIV_EXPR:
5627 : 9628798 : case FLOOR_DIV_EXPR:
5628 : 9628798 : case ROUND_DIV_EXPR:
5629 : 9628798 : case EXACT_DIV_EXPR:
5630 : 9628798 : if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5631 : : {
5632 : 101887 : tree type0 = TREE_OPERAND (op0, 0);
5633 : 101887 : tree type1 = TREE_OPERAND (op1, 0);
5634 : 101887 : tree first_arg = tree_strip_any_location_wrapper (type0);
5635 : 101887 : if (!TYPE_P (type0))
5636 : 81831 : type0 = TREE_TYPE (type0);
5637 : 101887 : if (!TYPE_P (type1))
5638 : 46941 : type1 = TREE_TYPE (type1);
5639 : 101887 : if (type0
5640 : 101887 : && type1
5641 : 81472 : && INDIRECT_TYPE_P (type0)
5642 : 101962 : && same_type_p (TREE_TYPE (type0), type1))
5643 : : {
5644 : 27 : if (!(TREE_CODE (first_arg) == PARM_DECL
5645 : 21 : && DECL_ARRAY_PARAMETER_P (first_arg)
5646 : 3 : && warn_sizeof_array_argument)
5647 : 42 : && (complain & tf_warning))
5648 : : {
5649 : 21 : auto_diagnostic_group d;
5650 : 21 : if (warning_at (location, OPT_Wsizeof_pointer_div,
5651 : : "division %<sizeof (%T) / sizeof (%T)%> does "
5652 : : "not compute the number of array elements",
5653 : : type0, type1))
5654 : 18 : if (DECL_P (first_arg))
5655 : 18 : inform (DECL_SOURCE_LOCATION (first_arg),
5656 : : "first %<sizeof%> operand was declared here");
5657 : 21 : }
5658 : : }
5659 : 101863 : else if (!dependent_type_p (type0)
5660 : 26626 : && !dependent_type_p (type1)
5661 : 26528 : && TREE_CODE (type0) == ARRAY_TYPE
5662 : 22428 : && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5663 : : /* Set by finish_parenthesized_expr. */
5664 : 21781 : && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5665 : 123626 : && (complain & tf_warning))
5666 : 21763 : maybe_warn_sizeof_array_div (location, first_arg, type0,
5667 : : op1, non_reference (type1));
5668 : : }
5669 : :
5670 : 9628798 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5671 : : || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5672 : 9628780 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5673 : : || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5674 : : {
5675 : 9628771 : enum tree_code tcode0 = code0, tcode1 = code1;
5676 : 9628771 : doing_div_or_mod = true;
5677 : 9628771 : warn_for_div_by_zero (location, fold_for_warn (op1));
5678 : :
5679 : 9628771 : if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5680 : 61252 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5681 : 9628771 : if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5682 : 31485 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5683 : :
5684 : 9628771 : if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5685 : : resultcode = RDIV_EXPR;
5686 : : else
5687 : : {
5688 : : /* When dividing two signed integers, we have to promote to int.
5689 : : unless we divide by a constant != -1. Note that default
5690 : : conversion will have been performed on the operands at this
5691 : : point, so we have to dig out the original type to find out if
5692 : : it was unsigned. */
5693 : 3851059 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5694 : 3851059 : shorten = may_shorten_divmod (op0, stripped_op1);
5695 : : }
5696 : :
5697 : : common = 1;
5698 : : }
5699 : : break;
5700 : :
5701 : 2092693 : case BIT_AND_EXPR:
5702 : 2092693 : case BIT_IOR_EXPR:
5703 : 2092693 : case BIT_XOR_EXPR:
5704 : 2092693 : if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5705 : 2092693 : || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5706 : 20780 : && !VECTOR_FLOAT_TYPE_P (type0)
5707 : 20762 : && !VECTOR_FLOAT_TYPE_P (type1)))
5708 : : shorten = -1;
5709 : : break;
5710 : :
5711 : 1194388 : case TRUNC_MOD_EXPR:
5712 : 1194388 : case FLOOR_MOD_EXPR:
5713 : 1194388 : doing_div_or_mod = true;
5714 : 1194388 : warn_for_div_by_zero (location, fold_for_warn (op1));
5715 : :
5716 : 1194388 : if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5717 : 20 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5718 : 1194408 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5719 : : common = 1;
5720 : 1194368 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5721 : : {
5722 : : /* Although it would be tempting to shorten always here, that loses
5723 : : on some targets, since the modulo instruction is undefined if the
5724 : : quotient can't be represented in the computation mode. We shorten
5725 : : only if unsigned or if dividing by something we know != -1. */
5726 : 1194344 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5727 : 1194344 : shorten = may_shorten_divmod (op0, stripped_op1);
5728 : 1194344 : common = 1;
5729 : : }
5730 : : break;
5731 : :
5732 : 14147328 : case TRUTH_ANDIF_EXPR:
5733 : 14147328 : case TRUTH_ORIF_EXPR:
5734 : 14147328 : case TRUTH_AND_EXPR:
5735 : 14147328 : case TRUTH_OR_EXPR:
5736 : 14147328 : if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5737 : : {
5738 : 3 : if (!COMPARISON_CLASS_P (op1))
5739 : 3 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5740 : : build_zero_cst (type1), complain);
5741 : 3 : if (code == TRUTH_ANDIF_EXPR)
5742 : : {
5743 : 0 : tree z = build_zero_cst (TREE_TYPE (op1));
5744 : 0 : return build_conditional_expr (location, op0, op1, z, complain);
5745 : : }
5746 : 3 : else if (code == TRUTH_ORIF_EXPR)
5747 : : {
5748 : 3 : tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5749 : 3 : return build_conditional_expr (location, op0, m1, op1, complain);
5750 : : }
5751 : : else
5752 : 0 : gcc_unreachable ();
5753 : : }
5754 : 14147325 : if (gnu_vector_type_p (type0)
5755 : 14147325 : && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5756 : : {
5757 : 24 : if (!COMPARISON_CLASS_P (op0))
5758 : 24 : op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5759 : : build_zero_cst (type0), complain);
5760 : 24 : if (!VECTOR_TYPE_P (type1))
5761 : : {
5762 : 9 : tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5763 : 9 : tree z = build_zero_cst (TREE_TYPE (op0));
5764 : 9 : op1 = build_conditional_expr (location, op1, m1, z, complain);
5765 : : }
5766 : 15 : else if (!COMPARISON_CLASS_P (op1))
5767 : 15 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5768 : : build_zero_cst (type1), complain);
5769 : :
5770 : 24 : if (code == TRUTH_ANDIF_EXPR)
5771 : : code = BIT_AND_EXPR;
5772 : 9 : else if (code == TRUTH_ORIF_EXPR)
5773 : : code = BIT_IOR_EXPR;
5774 : : else
5775 : 0 : gcc_unreachable ();
5776 : :
5777 : 24 : return cp_build_binary_op (location, code, op0, op1, complain);
5778 : : }
5779 : :
5780 : 14147301 : result_type = boolean_type_node;
5781 : 14147301 : break;
5782 : :
5783 : : /* Shift operations: result has same type as first operand;
5784 : : always convert second operand to int.
5785 : : Also set SHORT_SHIFT if shifting rightward. */
5786 : :
5787 : 934182 : case RSHIFT_EXPR:
5788 : 934182 : if (gnu_vector_type_p (type0)
5789 : 170 : && code1 == INTEGER_TYPE
5790 : 934229 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5791 : : {
5792 : : result_type = type0;
5793 : : converted = 1;
5794 : : }
5795 : 934135 : else if (gnu_vector_type_p (type0)
5796 : 123 : && gnu_vector_type_p (type1)
5797 : 123 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5798 : 120 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5799 : 934255 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5800 : : TYPE_VECTOR_SUBPARTS (type1)))
5801 : : {
5802 : : result_type = type0;
5803 : : converted = 1;
5804 : : }
5805 : 934015 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5806 : : {
5807 : 933988 : tree const_op1 = fold_for_warn (op1);
5808 : 933988 : if (TREE_CODE (const_op1) != INTEGER_CST)
5809 : 80138 : const_op1 = op1;
5810 : 933988 : result_type = type0;
5811 : 933988 : doing_shift = true;
5812 : 933988 : if (TREE_CODE (const_op1) == INTEGER_CST)
5813 : : {
5814 : 853850 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5815 : : {
5816 : 48 : if ((complain & tf_warning)
5817 : 48 : && c_inhibit_evaluation_warnings == 0)
5818 : 36 : warning_at (location, OPT_Wshift_count_negative,
5819 : : "right shift count is negative");
5820 : : }
5821 : : else
5822 : : {
5823 : 853802 : if (!integer_zerop (const_op1))
5824 : 853716 : short_shift = 1;
5825 : :
5826 : 853802 : if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5827 : 46 : && (complain & tf_warning)
5828 : 853848 : && c_inhibit_evaluation_warnings == 0)
5829 : 34 : warning_at (location, OPT_Wshift_count_overflow,
5830 : : "right shift count >= width of type");
5831 : : }
5832 : : }
5833 : : /* Avoid converting op1 to result_type later. */
5834 : : converted = 1;
5835 : : }
5836 : : break;
5837 : :
5838 : 2843031 : case LSHIFT_EXPR:
5839 : 2843031 : if (gnu_vector_type_p (type0)
5840 : 183 : && code1 == INTEGER_TYPE
5841 : 2843068 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5842 : : {
5843 : : result_type = type0;
5844 : : converted = 1;
5845 : : }
5846 : 2842994 : else if (gnu_vector_type_p (type0)
5847 : 146 : && gnu_vector_type_p (type1)
5848 : 146 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5849 : 143 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5850 : 2843134 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5851 : : TYPE_VECTOR_SUBPARTS (type1)))
5852 : : {
5853 : : result_type = type0;
5854 : : converted = 1;
5855 : : }
5856 : 2842854 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5857 : : {
5858 : 2842823 : tree const_op0 = fold_for_warn (op0);
5859 : 2842823 : if (TREE_CODE (const_op0) != INTEGER_CST)
5860 : 147128 : const_op0 = op0;
5861 : 2842823 : tree const_op1 = fold_for_warn (op1);
5862 : 2842823 : if (TREE_CODE (const_op1) != INTEGER_CST)
5863 : 217773 : const_op1 = op1;
5864 : 2842823 : result_type = type0;
5865 : 2842823 : doing_shift = true;
5866 : 2842823 : if (TREE_CODE (const_op0) == INTEGER_CST
5867 : 2695695 : && tree_int_cst_sgn (const_op0) < 0
5868 : 280 : && !TYPE_OVERFLOW_WRAPS (type0)
5869 : 196 : && (complain & tf_warning)
5870 : 2843019 : && c_inhibit_evaluation_warnings == 0)
5871 : 196 : warning_at (location, OPT_Wshift_negative_value,
5872 : : "left shift of negative value");
5873 : 2842823 : if (TREE_CODE (const_op1) == INTEGER_CST)
5874 : : {
5875 : 2625050 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5876 : : {
5877 : 57 : if ((complain & tf_warning)
5878 : 57 : && c_inhibit_evaluation_warnings == 0)
5879 : 39 : warning_at (location, OPT_Wshift_count_negative,
5880 : : "left shift count is negative");
5881 : : }
5882 : 2624993 : else if (compare_tree_int (const_op1,
5883 : 2624993 : TYPE_PRECISION (type0)) >= 0)
5884 : : {
5885 : 83 : if ((complain & tf_warning)
5886 : 61 : && c_inhibit_evaluation_warnings == 0)
5887 : 45 : warning_at (location, OPT_Wshift_count_overflow,
5888 : : "left shift count >= width of type");
5889 : : }
5890 : 2624910 : else if (TREE_CODE (const_op0) == INTEGER_CST
5891 : 2504012 : && (complain & tf_warning))
5892 : 2390786 : maybe_warn_shift_overflow (location, const_op0, const_op1);
5893 : : }
5894 : : /* Avoid converting op1 to result_type later. */
5895 : : converted = 1;
5896 : : }
5897 : : break;
5898 : :
5899 : 20922437 : case EQ_EXPR:
5900 : 20922437 : case NE_EXPR:
5901 : 20922437 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5902 : 2655 : goto vector_compare;
5903 : 20919782 : if ((complain & tf_warning)
5904 : 19462290 : && c_inhibit_evaluation_warnings == 0
5905 : 39719247 : && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5906 : 828723 : warning_at (location, OPT_Wfloat_equal,
5907 : : "comparing floating-point with %<==%> "
5908 : : "or %<!=%> is unsafe");
5909 : 20919782 : {
5910 : 20919782 : tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5911 : 20919782 : tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5912 : 20919782 : if ((complain & tf_warning_or_error)
5913 : 20919782 : && ((TREE_CODE (stripped_orig_op0) == STRING_CST
5914 : 39 : && !integer_zerop (cp_fully_fold (op1)))
5915 : 19747844 : || (TREE_CODE (stripped_orig_op1) == STRING_CST
5916 : 68 : && !integer_zerop (cp_fully_fold (op0)))))
5917 : 47 : warning_at (location, OPT_Waddress,
5918 : : "comparison with string literal results in "
5919 : : "unspecified behavior");
5920 : 20919735 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5921 : 20919735 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5922 : : {
5923 : : /* P2865R5 made array comparisons ill-formed in C++26. */
5924 : 76 : if (complain & tf_warning_or_error)
5925 : 45 : do_warn_array_compare (location, code, stripped_orig_op0,
5926 : : stripped_orig_op1);
5927 : 31 : else if (cxx_dialect >= cxx26)
5928 : 13 : return error_mark_node;
5929 : : }
5930 : : }
5931 : :
5932 : 20919769 : build_type = boolean_type_node;
5933 : 20919769 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5934 : : || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5935 : 17643595 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5936 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5937 : : short_compare = 1;
5938 : 29208 : else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5939 : 3275702 : && null_ptr_cst_p (orig_op1))
5940 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5941 : 5041674 : || (code0 == POINTER_TYPE
5942 : 1708842 : && TYPE_PTR_P (type1) && integer_zerop (op1)))
5943 : : {
5944 : 1586469 : if (TYPE_PTR_P (type1))
5945 : 19981 : result_type = composite_pointer_type (location,
5946 : : type0, type1, op0, op1,
5947 : : CPO_COMPARISON, complain);
5948 : : else
5949 : : result_type = type0;
5950 : :
5951 : 1586469 : if (char_type_p (TREE_TYPE (orig_op1)))
5952 : : {
5953 : 10 : auto_diagnostic_group d;
5954 : 10 : if (warning_at (location, OPT_Wpointer_compare,
5955 : : "comparison between pointer and zero character "
5956 : : "constant"))
5957 : 10 : inform (location,
5958 : : "did you mean to dereference the pointer?");
5959 : 10 : }
5960 : 1586469 : warn_for_null_address (location, op0, complain);
5961 : : }
5962 : 909 : else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5963 : 1717099 : && null_ptr_cst_p (orig_op0))
5964 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5965 : 3434327 : || (code1 == POINTER_TYPE
5966 : 1715830 : && TYPE_PTR_P (type0) && integer_zerop (op0)))
5967 : : {
5968 : 965 : if (TYPE_PTR_P (type0))
5969 : 68 : result_type = composite_pointer_type (location,
5970 : : type0, type1, op0, op1,
5971 : : CPO_COMPARISON, complain);
5972 : : else
5973 : : result_type = type1;
5974 : :
5975 : 965 : if (char_type_p (TREE_TYPE (orig_op0)))
5976 : : {
5977 : 10 : auto_diagnostic_group d;
5978 : 10 : if (warning_at (location, OPT_Wpointer_compare,
5979 : : "comparison between pointer and zero character "
5980 : : "constant"))
5981 : 10 : inform (location,
5982 : : "did you mean to dereference the pointer?");
5983 : 10 : }
5984 : 965 : warn_for_null_address (location, op1, complain);
5985 : : }
5986 : 1716647 : else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5987 : 27912 : || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5988 : 1689107 : result_type = composite_pointer_type (location,
5989 : : type0, type1, op0, op1,
5990 : : CPO_COMPARISON, complain);
5991 : 27540 : else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5992 : : /* One of the operands must be of nullptr_t type. */
5993 : 74 : result_type = TREE_TYPE (nullptr_node);
5994 : 27466 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5995 : : {
5996 : 58 : result_type = type0;
5997 : 58 : if (complain & tf_error)
5998 : 56 : permerror (location, "ISO C++ forbids comparison between "
5999 : : "pointer and integer");
6000 : : else
6001 : 2 : return error_mark_node;
6002 : : }
6003 : 27408 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6004 : : {
6005 : 27027 : result_type = type1;
6006 : 27027 : if (complain & tf_error)
6007 : 69 : permerror (location, "ISO C++ forbids comparison between "
6008 : : "pointer and integer");
6009 : : else
6010 : 26958 : return error_mark_node;
6011 : : }
6012 : 381 : else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
6013 : : {
6014 : 209 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6015 : : == ptrmemfunc_vbit_in_delta)
6016 : : {
6017 : : tree pfn0, delta0, e1, e2;
6018 : :
6019 : : if (TREE_SIDE_EFFECTS (op0))
6020 : : op0 = cp_save_expr (op0);
6021 : :
6022 : : pfn0 = pfn_from_ptrmemfunc (op0);
6023 : : delta0 = delta_from_ptrmemfunc (op0);
6024 : : {
6025 : : /* If we will warn below about a null-address compare
6026 : : involving the orig_op0 ptrmemfunc, we'd likely also
6027 : : warn about the pfn0's null-address compare, and
6028 : : that would be redundant, so suppress it. */
6029 : : warning_sentinel ws (warn_address);
6030 : : e1 = cp_build_binary_op (location,
6031 : : EQ_EXPR,
6032 : : pfn0,
6033 : : build_zero_cst (TREE_TYPE (pfn0)),
6034 : : complain);
6035 : : }
6036 : : e2 = cp_build_binary_op (location,
6037 : : BIT_AND_EXPR,
6038 : : delta0,
6039 : : integer_one_node,
6040 : : complain);
6041 : :
6042 : : if (complain & tf_warning)
6043 : : maybe_warn_zero_as_null_pointer_constant (op1, input_location);
6044 : :
6045 : : e2 = cp_build_binary_op (location,
6046 : : EQ_EXPR, e2, integer_zero_node,
6047 : : complain);
6048 : : op0 = cp_build_binary_op (location,
6049 : : TRUTH_ANDIF_EXPR, e1, e2,
6050 : : complain);
6051 : : op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
6052 : : }
6053 : : else
6054 : : {
6055 : 209 : op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
6056 : 209 : op1 = cp_convert (TREE_TYPE (op0), op1, complain);
6057 : : }
6058 : 209 : result_type = TREE_TYPE (op0);
6059 : :
6060 : 209 : warn_for_null_address (location, orig_op0, complain);
6061 : : }
6062 : 172 : else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
6063 : 36 : return cp_build_binary_op (location, code, op1, op0, complain);
6064 : 136 : else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
6065 : : {
6066 : 115 : tree type;
6067 : : /* E will be the final comparison. */
6068 : 115 : tree e;
6069 : : /* E1 and E2 are for scratch. */
6070 : 115 : tree e1;
6071 : 115 : tree e2;
6072 : 115 : tree pfn0;
6073 : 115 : tree pfn1;
6074 : 115 : tree delta0;
6075 : 115 : tree delta1;
6076 : :
6077 : 115 : type = composite_pointer_type (location, type0, type1, op0, op1,
6078 : : CPO_COMPARISON, complain);
6079 : :
6080 : 115 : if (!same_type_p (TREE_TYPE (op0), type))
6081 : 6 : op0 = cp_convert_and_check (type, op0, complain);
6082 : 115 : if (!same_type_p (TREE_TYPE (op1), type))
6083 : 9 : op1 = cp_convert_and_check (type, op1, complain);
6084 : :
6085 : 115 : if (op0 == error_mark_node || op1 == error_mark_node)
6086 : : return error_mark_node;
6087 : :
6088 : 112 : if (TREE_SIDE_EFFECTS (op0))
6089 : 53 : op0 = cp_save_expr (op0);
6090 : 112 : if (TREE_SIDE_EFFECTS (op1))
6091 : 3 : op1 = cp_save_expr (op1);
6092 : :
6093 : 112 : pfn0 = pfn_from_ptrmemfunc (op0);
6094 : 112 : pfn0 = cp_fully_fold (pfn0);
6095 : : /* Avoid -Waddress warnings (c++/64877). */
6096 : 112 : if (TREE_CODE (pfn0) == ADDR_EXPR)
6097 : 12 : suppress_warning (pfn0, OPT_Waddress);
6098 : 112 : pfn1 = pfn_from_ptrmemfunc (op1);
6099 : 112 : pfn1 = cp_fully_fold (pfn1);
6100 : 112 : delta0 = delta_from_ptrmemfunc (op0);
6101 : 112 : delta1 = delta_from_ptrmemfunc (op1);
6102 : 112 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6103 : : == ptrmemfunc_vbit_in_delta)
6104 : : {
6105 : : /* We generate:
6106 : :
6107 : : (op0.pfn == op1.pfn
6108 : : && ((op0.delta == op1.delta)
6109 : : || (!op0.pfn && op0.delta & 1 == 0
6110 : : && op1.delta & 1 == 0))
6111 : :
6112 : : The reason for the `!op0.pfn' bit is that a NULL
6113 : : pointer-to-member is any member with a zero PFN and
6114 : : LSB of the DELTA field is 0. */
6115 : :
6116 : : e1 = cp_build_binary_op (location, BIT_AND_EXPR,
6117 : : delta0,
6118 : : integer_one_node,
6119 : : complain);
6120 : : e1 = cp_build_binary_op (location,
6121 : : EQ_EXPR, e1, integer_zero_node,
6122 : : complain);
6123 : : e2 = cp_build_binary_op (location, BIT_AND_EXPR,
6124 : : delta1,
6125 : : integer_one_node,
6126 : : complain);
6127 : : e2 = cp_build_binary_op (location,
6128 : : EQ_EXPR, e2, integer_zero_node,
6129 : : complain);
6130 : : e1 = cp_build_binary_op (location,
6131 : : TRUTH_ANDIF_EXPR, e2, e1,
6132 : : complain);
6133 : : e2 = cp_build_binary_op (location, EQ_EXPR,
6134 : : pfn0,
6135 : : build_zero_cst (TREE_TYPE (pfn0)),
6136 : : complain);
6137 : : e2 = cp_build_binary_op (location,
6138 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6139 : : e1 = cp_build_binary_op (location,
6140 : : EQ_EXPR, delta0, delta1, complain);
6141 : : e1 = cp_build_binary_op (location,
6142 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6143 : : }
6144 : : else
6145 : : {
6146 : : /* We generate:
6147 : :
6148 : : (op0.pfn == op1.pfn
6149 : : && (!op0.pfn || op0.delta == op1.delta))
6150 : :
6151 : : The reason for the `!op0.pfn' bit is that a NULL
6152 : : pointer-to-member is any member with a zero PFN; the
6153 : : DELTA field is unspecified. */
6154 : :
6155 : 112 : e1 = cp_build_binary_op (location,
6156 : : EQ_EXPR, delta0, delta1, complain);
6157 : 112 : e2 = cp_build_binary_op (location,
6158 : : EQ_EXPR,
6159 : : pfn0,
6160 : 112 : build_zero_cst (TREE_TYPE (pfn0)),
6161 : : complain);
6162 : 112 : e1 = cp_build_binary_op (location,
6163 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6164 : : }
6165 : 112 : e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
6166 : 112 : e = cp_build_binary_op (location,
6167 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6168 : 112 : if (code == EQ_EXPR)
6169 : : return e;
6170 : 30 : return cp_build_binary_op (location,
6171 : 30 : EQ_EXPR, e, integer_zero_node, complain);
6172 : : }
6173 : : else
6174 : : {
6175 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
6176 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
6177 : : type1));
6178 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
6179 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
6180 : : type0));
6181 : : }
6182 : :
6183 : : break;
6184 : :
6185 : 241 : case MAX_EXPR:
6186 : 241 : case MIN_EXPR:
6187 : 241 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6188 : 241 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6189 : : shorten = 1;
6190 : 0 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6191 : 0 : result_type = composite_pointer_type (location,
6192 : : type0, type1, op0, op1,
6193 : : CPO_COMPARISON, complain);
6194 : : break;
6195 : :
6196 : 21325933 : case LE_EXPR:
6197 : 21325933 : case GE_EXPR:
6198 : 21325933 : case LT_EXPR:
6199 : 21325933 : case GT_EXPR:
6200 : 21325933 : case SPACESHIP_EXPR:
6201 : 21325933 : if (TREE_CODE (orig_op0) == STRING_CST
6202 : 21325933 : || TREE_CODE (orig_op1) == STRING_CST)
6203 : : {
6204 : 0 : if (complain & tf_warning)
6205 : 0 : warning_at (location, OPT_Waddress,
6206 : : "comparison with string literal results "
6207 : : "in unspecified behavior");
6208 : : }
6209 : 21325933 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6210 : 153 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6211 : 21326037 : && code != SPACESHIP_EXPR)
6212 : : {
6213 : 88 : if (complain & tf_warning_or_error)
6214 : 51 : do_warn_array_compare (location, code,
6215 : : tree_strip_any_location_wrapper (orig_op0),
6216 : : tree_strip_any_location_wrapper (orig_op1));
6217 : 37 : else if (cxx_dialect >= cxx26)
6218 : 1 : return error_mark_node;
6219 : : }
6220 : :
6221 : 21325932 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6222 : : {
6223 : 7172 : vector_compare:
6224 : 7172 : tree intt;
6225 : 7172 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6226 : 7172 : TREE_TYPE (type1))
6227 : 7172 : && !vector_types_compatible_elements_p (type0, type1))
6228 : : {
6229 : 3 : if (complain & tf_error)
6230 : : {
6231 : 3 : auto_diagnostic_group d;
6232 : 3 : error_at (location, "comparing vectors with different "
6233 : : "element types");
6234 : 3 : inform (location, "operand types are %qT and %qT",
6235 : : type0, type1);
6236 : 3 : }
6237 : 3 : return error_mark_node;
6238 : : }
6239 : :
6240 : 7169 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
6241 : 14338 : TYPE_VECTOR_SUBPARTS (type1)))
6242 : : {
6243 : 3 : if (complain & tf_error)
6244 : : {
6245 : 3 : auto_diagnostic_group d;
6246 : 3 : error_at (location, "comparing vectors with different "
6247 : : "number of elements");
6248 : 3 : inform (location, "operand types are %qT and %qT",
6249 : : type0, type1);
6250 : 3 : }
6251 : 3 : return error_mark_node;
6252 : : }
6253 : :
6254 : : /* It's not precisely specified how the usual arithmetic
6255 : : conversions apply to the vector types. Here, we use
6256 : : the unsigned type if one of the operands is signed and
6257 : : the other one is unsigned. */
6258 : 7166 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6259 : : {
6260 : 36 : if (!TYPE_UNSIGNED (type0))
6261 : 36 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6262 : : else
6263 : 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6264 : 36 : warning_at (location, OPT_Wsign_compare, "comparison between "
6265 : : "types %qT and %qT", type0, type1);
6266 : : }
6267 : :
6268 : 7166 : if (resultcode == SPACESHIP_EXPR)
6269 : : {
6270 : 3 : if (complain & tf_error)
6271 : 3 : sorry_at (location, "three-way comparison of vectors");
6272 : 3 : return error_mark_node;
6273 : : }
6274 : :
6275 : 7163 : if (VECTOR_BOOLEAN_TYPE_P (type0) && VECTOR_BOOLEAN_TYPE_P (type1))
6276 : : result_type = type0;
6277 : : else
6278 : : {
6279 : : /* Always construct signed integer vector type. */
6280 : 7163 : auto intmode = SCALAR_TYPE_MODE (TREE_TYPE (type0));
6281 : 7163 : auto nelts = TYPE_VECTOR_SUBPARTS (type0);
6282 : :
6283 : 14326 : intt = c_common_type_for_size (GET_MODE_BITSIZE (intmode), 0);
6284 : 7163 : if (!intt)
6285 : : {
6286 : 0 : if (complain & tf_error)
6287 : 0 : error_at (location, "could not find an integer type "
6288 : 0 : "of the same size as %qT", TREE_TYPE (type0));
6289 : 0 : return error_mark_node;
6290 : : }
6291 : 7163 : result_type = build_opaque_vector_type (intt, nelts);
6292 : : }
6293 : 7163 : return build_vec_cmp (resultcode, result_type, op0, op1);
6294 : : }
6295 : 21321415 : build_type = boolean_type_node;
6296 : 21321415 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6297 : : || code0 == ENUMERAL_TYPE)
6298 : 19966498 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6299 : : || code1 == ENUMERAL_TYPE))
6300 : : short_compare = 1;
6301 : 1355004 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6302 : 1354772 : result_type = composite_pointer_type (location,
6303 : : type0, type1, op0, op1,
6304 : : CPO_COMPARISON, complain);
6305 : 74 : else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6306 : 160 : || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6307 : 338 : || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6308 : : {
6309 : : /* Core Issue 1512 made this ill-formed. */
6310 : 191 : if (complain & tf_error)
6311 : 188 : error_at (location, "ordered comparison of pointer with "
6312 : : "integer zero (%qT and %qT)", type0, type1);
6313 : 191 : return error_mark_node;
6314 : : }
6315 : 41 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6316 : : {
6317 : 0 : result_type = type0;
6318 : 0 : if (complain & tf_error)
6319 : 0 : permerror (location, "ISO C++ forbids comparison between "
6320 : : "pointer and integer");
6321 : : else
6322 : 0 : return error_mark_node;
6323 : : }
6324 : 41 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6325 : : {
6326 : 24 : result_type = type1;
6327 : 24 : if (complain & tf_error)
6328 : 24 : permerror (location, "ISO C++ forbids comparison between "
6329 : : "pointer and integer");
6330 : : else
6331 : 0 : return error_mark_node;
6332 : : }
6333 : :
6334 : 21321224 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6335 : 1354798 : && !processing_template_decl
6336 : 22457603 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6337 : : {
6338 : 49 : op0 = cp_save_expr (op0);
6339 : 49 : op1 = cp_save_expr (op1);
6340 : :
6341 : 49 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6342 : 49 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6343 : : }
6344 : :
6345 : : break;
6346 : :
6347 : 0 : case UNORDERED_EXPR:
6348 : 0 : case ORDERED_EXPR:
6349 : 0 : case UNLT_EXPR:
6350 : 0 : case UNLE_EXPR:
6351 : 0 : case UNGT_EXPR:
6352 : 0 : case UNGE_EXPR:
6353 : 0 : case UNEQ_EXPR:
6354 : 0 : build_type = integer_type_node;
6355 : 0 : if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6356 : : {
6357 : 0 : if (complain & tf_error)
6358 : 0 : error ("unordered comparison on non-floating-point argument");
6359 : 0 : return error_mark_node;
6360 : : }
6361 : : common = 1;
6362 : : break;
6363 : :
6364 : : default:
6365 : : break;
6366 : : }
6367 : :
6368 : 119697185 : if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6369 : : || code0 == ENUMERAL_TYPE)
6370 : 99256874 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6371 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6372 : : arithmetic_types_p = 1;
6373 : : else
6374 : : {
6375 : 20681808 : arithmetic_types_p = 0;
6376 : : /* Vector arithmetic is only allowed when both sides are vectors. */
6377 : 20681808 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6378 : : {
6379 : 59626 : if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6380 : 59626 : || !vector_types_compatible_elements_p (type0, type1))
6381 : : {
6382 : 18 : if (complain & tf_error)
6383 : : {
6384 : : /* "location" already embeds the locations of the
6385 : : operands, so we don't need to add them separately
6386 : : to richloc. */
6387 : 18 : rich_location richloc (line_table, location);
6388 : 18 : binary_op_error (&richloc, code, type0, type1);
6389 : 18 : }
6390 : 18 : return error_mark_node;
6391 : : }
6392 : : arithmetic_types_p = 1;
6393 : : }
6394 : : }
6395 : : /* Determine the RESULT_TYPE, if it is not already known. */
6396 : 119697167 : if (!result_type
6397 : 119697167 : && arithmetic_types_p
6398 : 95113760 : && (shorten || common || short_compare))
6399 : : {
6400 : 95113683 : result_type = cp_common_type (type0, type1);
6401 : 95113683 : if (result_type == error_mark_node)
6402 : : {
6403 : 0 : tree t1 = type0;
6404 : 0 : tree t2 = type1;
6405 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6406 : 0 : t1 = TREE_TYPE (t1);
6407 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6408 : 0 : t2 = TREE_TYPE (t2);
6409 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6410 : : && TREE_CODE (t2) == REAL_TYPE
6411 : : && (extended_float_type_p (t1)
6412 : : || extended_float_type_p (t2))
6413 : : && cp_compare_floating_point_conversion_ranks
6414 : : (t1, t2) == 3);
6415 : 0 : if (complain & tf_error)
6416 : : {
6417 : 0 : rich_location richloc (line_table, location);
6418 : 0 : binary_op_error (&richloc, code, type0, type1);
6419 : 0 : }
6420 : 0 : return error_mark_node;
6421 : : }
6422 : 95113683 : if (complain & tf_warning)
6423 : 90099814 : do_warn_double_promotion (result_type, type0, type1,
6424 : : "implicit conversion from %qH to %qI "
6425 : : "to match other operand of binary "
6426 : : "expression", location);
6427 : 95113683 : if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6428 : 95113683 : TREE_TYPE (orig_op1), complain))
6429 : 6 : return error_mark_node;
6430 : : }
6431 : 119697161 : if (may_need_excess_precision
6432 : 89618423 : && (orig_type0 != type0 || orig_type1 != type1)
6433 : 84975 : && build_type == NULL_TREE
6434 : 84975 : && result_type)
6435 : : {
6436 : 63978 : gcc_assert (common);
6437 : 63978 : semantic_result_type = cp_common_type (orig_type0, orig_type1);
6438 : 63978 : if (semantic_result_type == error_mark_node)
6439 : : {
6440 : 0 : tree t1 = orig_type0;
6441 : 0 : tree t2 = orig_type1;
6442 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6443 : 0 : t1 = TREE_TYPE (t1);
6444 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6445 : 0 : t2 = TREE_TYPE (t2);
6446 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6447 : : && TREE_CODE (t2) == REAL_TYPE
6448 : : && (extended_float_type_p (t1)
6449 : : || extended_float_type_p (t2))
6450 : : && cp_compare_floating_point_conversion_ranks
6451 : : (t1, t2) == 3);
6452 : 0 : if (complain & tf_error)
6453 : : {
6454 : 0 : rich_location richloc (line_table, location);
6455 : 0 : binary_op_error (&richloc, code, type0, type1);
6456 : 0 : }
6457 : 0 : return error_mark_node;
6458 : : }
6459 : : }
6460 : :
6461 : 119697161 : if (code == SPACESHIP_EXPR)
6462 : : {
6463 : 225689 : iloc_sentinel s (location);
6464 : :
6465 : 225689 : tree orig_type0 = TREE_TYPE (orig_op0);
6466 : 225689 : tree_code orig_code0 = TREE_CODE (orig_type0);
6467 : 225689 : tree orig_type1 = TREE_TYPE (orig_op1);
6468 : 225689 : tree_code orig_code1 = TREE_CODE (orig_type1);
6469 : 225689 : if (!result_type || result_type == error_mark_node)
6470 : : /* Nope. */
6471 : : result_type = NULL_TREE;
6472 : 225668 : else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6473 : : /* "If one of the operands is of type bool and the other is not, the
6474 : : program is ill-formed." */
6475 : : result_type = NULL_TREE;
6476 : 225665 : else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6477 : 16 : && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6478 : : /* We only do array/function-to-pointer conversion if "at least one of
6479 : : the operands is of pointer type". */
6480 : : result_type = NULL_TREE;
6481 : 225649 : else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6482 : : /* <=> no longer supports equality relations. */
6483 : : result_type = NULL_TREE;
6484 : 225646 : else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6485 : 225682 : && !(same_type_ignoring_top_level_qualifiers_p
6486 : 36 : (orig_type0, orig_type1)))
6487 : : /* "If both operands have arithmetic types, or one operand has integral
6488 : : type and the other operand has unscoped enumeration type, the usual
6489 : : arithmetic conversions are applied to the operands." So we don't do
6490 : : arithmetic conversions if the operands both have enumeral type. */
6491 : : result_type = NULL_TREE;
6492 : 225631 : else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6493 : 225625 : || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6494 : : /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6495 : : [where one is of enumeration type and the other is of a different
6496 : : enumeration type or a floating-point type] are ill-formed. */
6497 : : result_type = NULL_TREE;
6498 : :
6499 : 225619 : if (result_type)
6500 : : {
6501 : 225619 : build_type = spaceship_type (result_type, complain);
6502 : 225619 : if (build_type == error_mark_node)
6503 : : return error_mark_node;
6504 : : }
6505 : :
6506 : 225674 : if (result_type && arithmetic_types_p)
6507 : : {
6508 : : /* If a narrowing conversion is required, other than from an integral
6509 : : type to a floating point type, the program is ill-formed. */
6510 : 83517 : bool ok = true;
6511 : 83517 : if (TREE_CODE (result_type) == REAL_TYPE
6512 : 676 : && CP_INTEGRAL_TYPE_P (orig_type0))
6513 : : /* OK */;
6514 : 83506 : else if (!check_narrowing (result_type, orig_op0, complain))
6515 : 83517 : ok = false;
6516 : 83517 : if (TREE_CODE (result_type) == REAL_TYPE
6517 : 676 : && CP_INTEGRAL_TYPE_P (orig_type1))
6518 : : /* OK */;
6519 : 83506 : else if (!check_narrowing (result_type, orig_op1, complain))
6520 : : ok = false;
6521 : 83517 : if (!ok && !(complain & tf_error))
6522 : 4 : return error_mark_node;
6523 : : }
6524 : 225689 : }
6525 : :
6526 : 119697142 : if (!result_type)
6527 : : {
6528 : 477 : if (complain & tf_error)
6529 : : {
6530 : 206 : binary_op_rich_location richloc (location,
6531 : 206 : orig_op0, orig_op1, true);
6532 : 206 : error_at (&richloc,
6533 : : "invalid operands of types %qT and %qT to binary %qO",
6534 : 206 : TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6535 : 206 : }
6536 : 477 : return error_mark_node;
6537 : : }
6538 : :
6539 : : /* If we're in a template, the only thing we need to know is the
6540 : : RESULT_TYPE. */
6541 : 119696665 : if (processing_template_decl)
6542 : : {
6543 : : /* Since the middle-end checks the type when doing a build2, we
6544 : : need to build the tree in pieces. This built tree will never
6545 : : get out of the front-end as we replace it when instantiating
6546 : : the template. */
6547 : 53067978 : tree tmp = build2 (resultcode,
6548 : : build_type ? build_type : result_type,
6549 : : NULL_TREE, op1);
6550 : 33348384 : TREE_OPERAND (tmp, 0) = op0;
6551 : 33348384 : if (semantic_result_type)
6552 : 0 : tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6553 : 33348384 : return tmp;
6554 : : }
6555 : :
6556 : : /* Remember the original type; RESULT_TYPE might be changed later on
6557 : : by shorten_binary_op. */
6558 : 86348281 : tree orig_type = result_type;
6559 : :
6560 : 86348281 : if (arithmetic_types_p)
6561 : : {
6562 : 74855987 : bool first_complex = (code0 == COMPLEX_TYPE);
6563 : 74855987 : bool second_complex = (code1 == COMPLEX_TYPE);
6564 : 74855987 : int none_complex = (!first_complex && !second_complex);
6565 : :
6566 : : /* Adapted from patch for c/24581. */
6567 : 74855987 : if (first_complex != second_complex
6568 : 165841 : && (code == PLUS_EXPR
6569 : : || code == MINUS_EXPR
6570 : 165841 : || code == MULT_EXPR
6571 : 43280 : || (code == TRUNC_DIV_EXPR && first_complex))
6572 : 152334 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6573 : 75008160 : && flag_signed_zeros)
6574 : : {
6575 : : /* An operation on mixed real/complex operands must be
6576 : : handled specially, but the language-independent code can
6577 : : more easily optimize the plain complex arithmetic if
6578 : : -fno-signed-zeros. */
6579 : 152173 : tree real_type = TREE_TYPE (result_type);
6580 : 152173 : tree real, imag;
6581 : 152173 : if (first_complex)
6582 : : {
6583 : 119125 : if (TREE_TYPE (op0) != result_type)
6584 : 6 : op0 = cp_convert_and_check (result_type, op0, complain);
6585 : 119125 : if (TREE_TYPE (op1) != real_type)
6586 : 26 : op1 = cp_convert_and_check (real_type, op1, complain);
6587 : : }
6588 : : else
6589 : : {
6590 : 33048 : if (TREE_TYPE (op0) != real_type)
6591 : 31 : op0 = cp_convert_and_check (real_type, op0, complain);
6592 : 33048 : if (TREE_TYPE (op1) != result_type)
6593 : 32 : op1 = cp_convert_and_check (result_type, op1, complain);
6594 : : }
6595 : 152173 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6596 : 0 : return error_mark_node;
6597 : 152173 : if (first_complex)
6598 : : {
6599 : 119125 : op0 = cp_save_expr (op0);
6600 : 119125 : real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6601 : 119125 : imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6602 : 119125 : switch (code)
6603 : : {
6604 : 59539 : case MULT_EXPR:
6605 : 59539 : case TRUNC_DIV_EXPR:
6606 : 59539 : op1 = cp_save_expr (op1);
6607 : 59539 : imag = build2 (resultcode, real_type, imag, op1);
6608 : : /* Fall through. */
6609 : 119125 : case PLUS_EXPR:
6610 : 119125 : case MINUS_EXPR:
6611 : 119125 : real = build2 (resultcode, real_type, real, op1);
6612 : 119125 : break;
6613 : 0 : default:
6614 : 0 : gcc_unreachable();
6615 : : }
6616 : : }
6617 : : else
6618 : : {
6619 : 33048 : op1 = cp_save_expr (op1);
6620 : 33048 : real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6621 : 33048 : imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6622 : 33048 : switch (code)
6623 : : {
6624 : 638 : case MULT_EXPR:
6625 : 638 : op0 = cp_save_expr (op0);
6626 : 638 : imag = build2 (resultcode, real_type, op0, imag);
6627 : : /* Fall through. */
6628 : 17301 : case PLUS_EXPR:
6629 : 17301 : real = build2 (resultcode, real_type, op0, real);
6630 : 17301 : break;
6631 : 15747 : case MINUS_EXPR:
6632 : 15747 : real = build2 (resultcode, real_type, op0, real);
6633 : 15747 : imag = build1 (NEGATE_EXPR, real_type, imag);
6634 : 15747 : break;
6635 : 0 : default:
6636 : 0 : gcc_unreachable();
6637 : : }
6638 : : }
6639 : 152173 : result = build2 (COMPLEX_EXPR, result_type, real, imag);
6640 : 152173 : if (semantic_result_type)
6641 : 24 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6642 : : result);
6643 : 152173 : return result;
6644 : : }
6645 : :
6646 : : /* For certain operations (which identify themselves by shorten != 0)
6647 : : if both args were extended from the same smaller type,
6648 : : do the arithmetic in that type and then extend.
6649 : :
6650 : : shorten !=0 and !=1 indicates a bitwise operation.
6651 : : For them, this optimization is safe only if
6652 : : both args are zero-extended or both are sign-extended.
6653 : : Otherwise, we might change the result.
6654 : : E.g., (short)-1 | (unsigned short)-1 is (int)-1
6655 : : but calculated in (unsigned short) it would be (unsigned short)-1. */
6656 : :
6657 : 74703814 : if (shorten && none_complex)
6658 : : {
6659 : 3953677 : final_type = result_type;
6660 : 3953677 : result_type = shorten_binary_op (result_type, op0, op1,
6661 : : shorten == -1);
6662 : : }
6663 : :
6664 : : /* Shifts can be shortened if shifting right. */
6665 : :
6666 : 74703814 : if (short_shift)
6667 : : {
6668 : 758924 : int unsigned_arg;
6669 : 758924 : tree arg0 = get_narrower (op0, &unsigned_arg);
6670 : : /* We're not really warning here but when we set short_shift we
6671 : : used fold_for_warn to fold the operand. */
6672 : 758924 : tree const_op1 = fold_for_warn (op1);
6673 : :
6674 : 758924 : final_type = result_type;
6675 : :
6676 : 758924 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
6677 : 696303 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6678 : :
6679 : 758924 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6680 : 3588 : && tree_int_cst_sgn (const_op1) > 0
6681 : : /* We can shorten only if the shift count is less than the
6682 : : number of bits in the smaller type size. */
6683 : 3588 : && compare_tree_int (const_op1,
6684 : 3588 : TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6685 : : /* We cannot drop an unsigned shift after sign-extension. */
6686 : 762502 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6687 : : {
6688 : : /* Do an unsigned shift if the operand was zero-extended. */
6689 : 3571 : result_type
6690 : 3571 : = c_common_signed_or_unsigned_type (unsigned_arg,
6691 : 3571 : TREE_TYPE (arg0));
6692 : : /* Convert value-to-be-shifted to that type. */
6693 : 3571 : if (TREE_TYPE (op0) != result_type)
6694 : 3571 : op0 = convert (result_type, op0);
6695 : : converted = 1;
6696 : : }
6697 : : }
6698 : :
6699 : : /* Comparison operations are shortened too but differently.
6700 : : They identify themselves by setting short_compare = 1. */
6701 : :
6702 : 74703814 : if (short_compare)
6703 : : {
6704 : : /* We call shorten_compare only for diagnostics. */
6705 : 24417048 : tree xop0 = fold_simple (op0);
6706 : 24417048 : tree xop1 = fold_simple (op1);
6707 : 24417048 : tree xresult_type = result_type;
6708 : 24417048 : enum tree_code xresultcode = resultcode;
6709 : 24417048 : shorten_compare (location, &xop0, &xop1, &xresult_type,
6710 : : &xresultcode);
6711 : : }
6712 : :
6713 : 50286679 : if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6714 : 24417237 : && warn_sign_compare
6715 : : /* Do not warn until the template is instantiated; we cannot
6716 : : bound the ranges of the arguments until that point. */
6717 : 319018 : && !processing_template_decl
6718 : 319018 : && (complain & tf_warning)
6719 : 312052 : && c_inhibit_evaluation_warnings == 0
6720 : : /* Even unsigned enum types promote to signed int. We don't
6721 : : want to issue -Wsign-compare warnings for this case. */
6722 : 295982 : && !enum_cast_to_int (orig_op0)
6723 : 74999796 : && !enum_cast_to_int (orig_op1))
6724 : : {
6725 : 295975 : warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6726 : : result_type, resultcode);
6727 : : }
6728 : : }
6729 : :
6730 : : /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6731 : : Then the expression will be built.
6732 : : It will be given type FINAL_TYPE if that is nonzero;
6733 : : otherwise, it will be given type RESULT_TYPE. */
6734 : 86196108 : if (! converted)
6735 : : {
6736 : 82794453 : warning_sentinel w (warn_sign_conversion, short_compare);
6737 : 82794453 : if (!same_type_p (TREE_TYPE (op0), result_type))
6738 : 3495046 : op0 = cp_convert_and_check (result_type, op0, complain);
6739 : 82794453 : if (!same_type_p (TREE_TYPE (op1), result_type))
6740 : 14815749 : op1 = cp_convert_and_check (result_type, op1, complain);
6741 : :
6742 : 82794453 : if (op0 == error_mark_node || op1 == error_mark_node)
6743 : 65 : return error_mark_node;
6744 : 82794453 : }
6745 : :
6746 : 86196043 : if (build_type == NULL_TREE)
6747 : 57611115 : build_type = result_type;
6748 : :
6749 : 86196043 : if (doing_shift
6750 : 3401073 : && flag_strong_eval_order == 2
6751 : 3311499 : && TREE_SIDE_EFFECTS (op1)
6752 : 86222360 : && !processing_template_decl)
6753 : : {
6754 : : /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6755 : : op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6756 : 26317 : op0 = cp_save_expr (op0);
6757 : 26317 : instrument_expr = op0;
6758 : : }
6759 : :
6760 : 86196043 : if (sanitize_flags_p ((SANITIZE_SHIFT
6761 : : | SANITIZE_DIVIDE
6762 : : | SANITIZE_FLOAT_DIVIDE
6763 : : | SANITIZE_SI_OVERFLOW))
6764 : 12635 : && current_function_decl != NULL_TREE
6765 : 10454 : && !processing_template_decl
6766 : 86206497 : && (doing_div_or_mod || doing_shift))
6767 : : {
6768 : : /* OP0 and/or OP1 might have side-effects. */
6769 : 881 : op0 = cp_save_expr (op0);
6770 : 881 : op1 = cp_save_expr (op1);
6771 : 881 : op0 = fold_non_dependent_expr (op0, complain);
6772 : 881 : op1 = fold_non_dependent_expr (op1, complain);
6773 : 881 : tree instrument_expr1 = NULL_TREE;
6774 : 881 : if (doing_div_or_mod
6775 : 881 : && sanitize_flags_p (SANITIZE_DIVIDE
6776 : : | SANITIZE_FLOAT_DIVIDE
6777 : : | SANITIZE_SI_OVERFLOW))
6778 : : {
6779 : : /* For diagnostics we want to use the promoted types without
6780 : : shorten_binary_op. So convert the arguments to the
6781 : : original result_type. */
6782 : 453 : tree cop0 = op0;
6783 : 453 : tree cop1 = op1;
6784 : 453 : if (TREE_TYPE (cop0) != orig_type)
6785 : 6 : cop0 = cp_convert (orig_type, op0, complain);
6786 : 453 : if (TREE_TYPE (cop1) != orig_type)
6787 : 32 : cop1 = cp_convert (orig_type, op1, complain);
6788 : 453 : instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6789 : : }
6790 : 428 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6791 : 347 : instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6792 : 881 : if (instrument_expr != NULL)
6793 : 18 : instrument_expr = add_stmt_to_compound (instrument_expr,
6794 : : instrument_expr1);
6795 : : else
6796 : 863 : instrument_expr = instrument_expr1;
6797 : : }
6798 : :
6799 : 86196043 : result = build2_loc (location, resultcode, build_type, op0, op1);
6800 : 86196043 : if (final_type != 0)
6801 : 4712601 : result = cp_convert (final_type, result, complain);
6802 : :
6803 : 86196043 : if (instrument_expr != NULL)
6804 : 26819 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6805 : : instrument_expr, result);
6806 : :
6807 : 86196043 : if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6808 : 197726 : result = get_target_expr (result, complain);
6809 : :
6810 : 86196043 : if (semantic_result_type)
6811 : 63954 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
6812 : :
6813 : 86196043 : if (!c_inhibit_evaluation_warnings)
6814 : : {
6815 : 77364962 : if (!processing_template_decl)
6816 : : {
6817 : 77364962 : op0 = cp_fully_fold (op0);
6818 : : /* Only consider the second argument if the first isn't overflowed. */
6819 : 77364962 : if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6820 : : return result;
6821 : 20309930 : op1 = cp_fully_fold (op1);
6822 : 20309930 : if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6823 : : return result;
6824 : : }
6825 : 0 : else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6826 : 0 : || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6827 : : return result;
6828 : :
6829 : 14927784 : tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6830 : 14927784 : if (TREE_OVERFLOW_P (result_ovl))
6831 : 190 : overflow_warning (location, result_ovl);
6832 : : }
6833 : :
6834 : : return result;
6835 : : }
6836 : :
6837 : : /* Build a VEC_PERM_EXPR.
6838 : : This is a simple wrapper for c_build_vec_perm_expr. */
6839 : : tree
6840 : 17494 : build_x_vec_perm_expr (location_t loc,
6841 : : tree arg0, tree arg1, tree arg2,
6842 : : tsubst_flags_t complain)
6843 : : {
6844 : 17494 : tree orig_arg0 = arg0;
6845 : 17494 : tree orig_arg1 = arg1;
6846 : 17494 : tree orig_arg2 = arg2;
6847 : 17494 : if (processing_template_decl)
6848 : : {
6849 : 19 : if (type_dependent_expression_p (arg0)
6850 : 13 : || type_dependent_expression_p (arg1)
6851 : 32 : || type_dependent_expression_p (arg2))
6852 : 6 : return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6853 : : }
6854 : 17488 : tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6855 : 17488 : if (processing_template_decl && exp != error_mark_node)
6856 : 13 : return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6857 : 13 : orig_arg1, orig_arg2);
6858 : : return exp;
6859 : : }
6860 : :
6861 : : /* Build a VEC_PERM_EXPR.
6862 : : This is a simple wrapper for c_build_shufflevector. */
6863 : : tree
6864 : 41175 : build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6865 : : tsubst_flags_t complain)
6866 : : {
6867 : 41175 : tree arg0 = (*args)[0];
6868 : 41175 : tree arg1 = (*args)[1];
6869 : 41175 : if (processing_template_decl)
6870 : : {
6871 : 43 : for (unsigned i = 0; i < args->length (); ++i)
6872 : 40 : if (i <= 1
6873 : 40 : ? type_dependent_expression_p ((*args)[i])
6874 : 9 : : instantiation_dependent_expression_p ((*args)[i]))
6875 : : {
6876 : 22 : tree exp = build_min_nt_call_vec (NULL, args);
6877 : 22 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6878 : 22 : return exp;
6879 : : }
6880 : : }
6881 : 41153 : auto_vec<tree, 16> mask;
6882 : 558709 : for (unsigned i = 2; i < args->length (); ++i)
6883 : : {
6884 : 517556 : tree idx = fold_non_dependent_expr ((*args)[i], complain);
6885 : 517556 : mask.safe_push (idx);
6886 : : }
6887 : 41153 : tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6888 : 41153 : if (processing_template_decl && exp != error_mark_node)
6889 : : {
6890 : 3 : exp = build_min_non_dep_call_vec (exp, NULL, args);
6891 : 3 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6892 : : }
6893 : 41153 : return exp;
6894 : 41153 : }
6895 : :
6896 : : /* Return a tree for the sum or difference (RESULTCODE says which)
6897 : : of pointer PTROP and integer INTOP. */
6898 : :
6899 : : static tree
6900 : 4357232 : cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6901 : : tree intop, tsubst_flags_t complain)
6902 : : {
6903 : 4357232 : tree res_type = TREE_TYPE (ptrop);
6904 : :
6905 : : /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6906 : : in certain circumstance (when it's valid to do so). So we need
6907 : : to make sure it's complete. We don't need to check here, if we
6908 : : can actually complete it at all, as those checks will be done in
6909 : : pointer_int_sum() anyway. */
6910 : 4357232 : complete_type (TREE_TYPE (res_type));
6911 : :
6912 : 4357232 : return pointer_int_sum (loc, resultcode, ptrop,
6913 : 4357232 : intop, complain & tf_warning_or_error);
6914 : : }
6915 : :
6916 : : /* Return a tree for the difference of pointers OP0 and OP1.
6917 : : The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6918 : : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6919 : :
6920 : : static tree
6921 : 1300945 : pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6922 : : tsubst_flags_t complain, tree *instrument_expr)
6923 : : {
6924 : 1300945 : tree result, inttype;
6925 : 1300945 : tree restype = ptrdiff_type_node;
6926 : 1300945 : tree target_type = TREE_TYPE (ptrtype);
6927 : :
6928 : 1300945 : if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6929 : 12 : return error_mark_node;
6930 : :
6931 : 1300933 : if (VOID_TYPE_P (target_type))
6932 : : {
6933 : 0 : if (complain & tf_error)
6934 : 0 : permerror (loc, "ISO C++ forbids using pointer of "
6935 : : "type %<void *%> in subtraction");
6936 : : else
6937 : 0 : return error_mark_node;
6938 : : }
6939 : 1300933 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
6940 : : {
6941 : 15 : if (complain & tf_error)
6942 : 3 : permerror (loc, "ISO C++ forbids using pointer to "
6943 : : "a function in subtraction");
6944 : : else
6945 : 12 : return error_mark_node;
6946 : : }
6947 : 1300921 : if (TREE_CODE (target_type) == METHOD_TYPE)
6948 : : {
6949 : 0 : if (complain & tf_error)
6950 : 0 : permerror (loc, "ISO C++ forbids using pointer to "
6951 : : "a method in subtraction");
6952 : : else
6953 : 0 : return error_mark_node;
6954 : : }
6955 : 2601842 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6956 : 1300921 : TREE_TYPE (TREE_TYPE (op0)),
6957 : : !(complain & tf_error))
6958 : 2601842 : || !verify_type_context (loc, TCTX_POINTER_ARITH,
6959 : 1300921 : TREE_TYPE (TREE_TYPE (op1)),
6960 : : !(complain & tf_error)))
6961 : 0 : return error_mark_node;
6962 : :
6963 : : /* Determine integer type result of the subtraction. This will usually
6964 : : be the same as the result type (ptrdiff_t), but may need to be a wider
6965 : : type if pointers for the address space are wider than ptrdiff_t. */
6966 : 1300921 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6967 : 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6968 : : else
6969 : : inttype = restype;
6970 : :
6971 : 1300921 : if (!processing_template_decl
6972 : 1300921 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
6973 : : {
6974 : 84 : op0 = save_expr (op0);
6975 : 84 : op1 = save_expr (op1);
6976 : :
6977 : 84 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
6978 : 84 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6979 : : }
6980 : :
6981 : : /* First do the subtraction, then build the divide operator
6982 : : and only convert at the very end.
6983 : : Do not do default conversions in case restype is a short type. */
6984 : :
6985 : : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6986 : : pointers. If some platform cannot provide that, or has a larger
6987 : : ptrdiff_type to support differences larger than half the address
6988 : : space, cast the pointers to some larger integer type and do the
6989 : : computations in that type. */
6990 : 1300921 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6991 : 0 : op0 = cp_build_binary_op (loc,
6992 : : MINUS_EXPR,
6993 : : cp_convert (inttype, op0, complain),
6994 : : cp_convert (inttype, op1, complain),
6995 : : complain);
6996 : : else
6997 : 1300921 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
6998 : :
6999 : : /* This generates an error if op1 is a pointer to an incomplete type. */
7000 : 1300921 : if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
7001 : : {
7002 : 0 : if (complain & tf_error)
7003 : 0 : error_at (loc, "invalid use of a pointer to an incomplete type in "
7004 : : "pointer arithmetic");
7005 : : else
7006 : 0 : return error_mark_node;
7007 : : }
7008 : :
7009 : 1300921 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
7010 : : {
7011 : 15 : if (complain & tf_error)
7012 : 15 : error_at (loc, "arithmetic on pointer to an empty aggregate");
7013 : : else
7014 : 0 : return error_mark_node;
7015 : : }
7016 : :
7017 : 2601839 : op1 = (TYPE_PTROB_P (ptrtype)
7018 : 2601839 : ? size_in_bytes_loc (loc, target_type)
7019 : : : integer_one_node);
7020 : :
7021 : : /* Do the division. */
7022 : :
7023 : 1300921 : result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
7024 : : cp_convert (inttype, op1, complain));
7025 : 1300921 : return cp_convert (restype, result, complain);
7026 : : }
7027 : :
7028 : : /* Construct and perhaps optimize a tree representation
7029 : : for a unary operation. CODE, a tree_code, specifies the operation
7030 : : and XARG is the operand. */
7031 : :
7032 : : tree
7033 : 57118771 : build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
7034 : : tree lookups, tsubst_flags_t complain)
7035 : : {
7036 : 57118771 : tree orig_expr = xarg;
7037 : 57118771 : tree exp;
7038 : 57118771 : int ptrmem = 0;
7039 : 57118771 : tree overload = NULL_TREE;
7040 : :
7041 : 57118771 : if (processing_template_decl)
7042 : : {
7043 : 39896106 : if (type_dependent_expression_p (xarg))
7044 : : {
7045 : 26525731 : tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
7046 : 26525731 : TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
7047 : 26525731 : return e;
7048 : : }
7049 : : }
7050 : :
7051 : 30593040 : exp = NULL_TREE;
7052 : :
7053 : : /* [expr.unary.op] says:
7054 : :
7055 : : The address of an object of incomplete type can be taken.
7056 : :
7057 : : (And is just the ordinary address operator, not an overloaded
7058 : : "operator &".) However, if the type is a template
7059 : : specialization, we must complete the type at this point so that
7060 : : an overloaded "operator &" will be available if required. */
7061 : 30593040 : if (code == ADDR_EXPR
7062 : 3434667 : && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
7063 : 34027306 : && ((CLASS_TYPE_P (TREE_TYPE (xarg))
7064 : 1617678 : && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
7065 : 3431821 : || (TREE_CODE (xarg) == OFFSET_REF)))
7066 : : /* Don't look for a function. */;
7067 : : else
7068 : 30524215 : exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
7069 : : NULL_TREE, lookups, &overload, complain);
7070 : :
7071 : 30593040 : if (!exp && code == ADDR_EXPR)
7072 : : {
7073 : 3434375 : if (is_overloaded_fn (xarg))
7074 : : {
7075 : 371972 : tree fn = get_first_fn (xarg);
7076 : 743944 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
7077 : : {
7078 : 12 : if (complain & tf_error)
7079 : 21 : error_at (loc, DECL_CONSTRUCTOR_P (fn)
7080 : : ? G_("taking address of constructor %qD")
7081 : : : G_("taking address of destructor %qD"),
7082 : : fn);
7083 : 12 : return error_mark_node;
7084 : : }
7085 : : }
7086 : :
7087 : : /* A pointer to member-function can be formed only by saying
7088 : : &X::mf. */
7089 : 3434357 : if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
7090 : 3496975 : && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
7091 : : {
7092 : 9 : if (TREE_CODE (xarg) != OFFSET_REF
7093 : 9 : || !TYPE_P (TREE_OPERAND (xarg, 0)))
7094 : : {
7095 : 9 : if (complain & tf_error)
7096 : : {
7097 : 9 : auto_diagnostic_group d;
7098 : 9 : error_at (loc, "invalid use of %qE to form a "
7099 : : "pointer-to-member-function", xarg.get_value ());
7100 : 9 : if (TREE_CODE (xarg) != OFFSET_REF)
7101 : 6 : inform (loc, " a qualified-id is required");
7102 : 9 : }
7103 : 9 : return error_mark_node;
7104 : : }
7105 : : else
7106 : : {
7107 : 0 : if (complain & tf_error)
7108 : 0 : error_at (loc, "parentheses around %qE cannot be used to "
7109 : : "form a pointer-to-member-function",
7110 : : xarg.get_value ());
7111 : : else
7112 : 0 : return error_mark_node;
7113 : 0 : PTRMEM_OK_P (xarg) = 1;
7114 : : }
7115 : : }
7116 : :
7117 : 3434354 : if (TREE_CODE (xarg) == OFFSET_REF)
7118 : : {
7119 : 66365 : ptrmem = PTRMEM_OK_P (xarg);
7120 : :
7121 : 0 : if (!ptrmem && !flag_ms_extensions
7122 : 66365 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
7123 : : {
7124 : : /* A single non-static member, make sure we don't allow a
7125 : : pointer-to-member. */
7126 : 0 : xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
7127 : 0 : TREE_OPERAND (xarg, 0),
7128 : 0 : ovl_make (TREE_OPERAND (xarg, 1)));
7129 : 0 : PTRMEM_OK_P (xarg) = ptrmem;
7130 : : }
7131 : : }
7132 : :
7133 : 6868708 : exp = cp_build_addr_expr_strict (xarg, complain);
7134 : :
7135 : 3434354 : if (TREE_CODE (exp) == PTRMEM_CST)
7136 : 65436 : PTRMEM_CST_LOCATION (exp) = loc;
7137 : : else
7138 : 3368918 : protected_set_expr_location (exp, loc);
7139 : : }
7140 : :
7141 : 30593019 : if (processing_template_decl && exp != error_mark_node)
7142 : : {
7143 : 13370227 : if (overload != NULL_TREE)
7144 : 164915 : return (build_min_non_dep_op_overload
7145 : 164915 : (code, exp, overload, orig_expr, integer_zero_node));
7146 : :
7147 : 13205312 : exp = build_min_non_dep (code, exp, orig_expr,
7148 : : /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
7149 : : }
7150 : 30428104 : if (TREE_CODE (exp) == ADDR_EXPR)
7151 : 2642576 : PTRMEM_OK_P (exp) = ptrmem;
7152 : : return exp;
7153 : : }
7154 : :
7155 : : /* Construct and perhaps optimize a tree representation
7156 : : for __builtin_addressof operation. ARG specifies the operand. */
7157 : :
7158 : : tree
7159 : 480226 : cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7160 : : {
7161 : 480226 : tree orig_expr = arg;
7162 : :
7163 : 480226 : if (processing_template_decl)
7164 : : {
7165 : 184553 : if (type_dependent_expression_p (arg))
7166 : 184553 : return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7167 : : }
7168 : :
7169 : 591346 : tree exp = cp_build_addr_expr_strict (arg, complain);
7170 : :
7171 : 295673 : if (processing_template_decl && exp != error_mark_node)
7172 : 0 : exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
7173 : : return exp;
7174 : : }
7175 : :
7176 : : /* Like c_common_truthvalue_conversion, but handle pointer-to-member
7177 : : constants, where a null value is represented by an INTEGER_CST of
7178 : : -1. */
7179 : :
7180 : : tree
7181 : 7311986 : cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7182 : : {
7183 : 7311986 : tree type = TREE_TYPE (expr);
7184 : 7311986 : location_t loc = cp_expr_loc_or_input_loc (expr);
7185 : 6198456 : if (TYPE_PTR_OR_PTRMEM_P (type)
7186 : : /* Avoid ICE on invalid use of non-static member function. */
7187 : 13510156 : || TREE_CODE (expr) == FUNCTION_DECL)
7188 : 1113816 : return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
7189 : : else
7190 : 6198170 : return c_common_truthvalue_conversion (loc, expr);
7191 : : }
7192 : :
7193 : : /* Returns EXPR contextually converted to bool. */
7194 : :
7195 : : tree
7196 : 47220485 : contextual_conv_bool (tree expr, tsubst_flags_t complain)
7197 : : {
7198 : 47220485 : return perform_implicit_conversion_flags (boolean_type_node, expr,
7199 : 47220485 : complain, LOOKUP_NORMAL);
7200 : : }
7201 : :
7202 : : /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7203 : : is a low-level function; most callers should use maybe_convert_cond. */
7204 : :
7205 : : tree
7206 : 42141892 : condition_conversion (tree expr)
7207 : : {
7208 : 42141892 : tree t = contextual_conv_bool (expr, tf_warning_or_error);
7209 : 42141892 : if (!processing_template_decl)
7210 : 21419148 : t = fold_build_cleanup_point_expr (boolean_type_node, t);
7211 : 42141892 : return t;
7212 : : }
7213 : :
7214 : : /* Returns the address of T. This function will fold away
7215 : : ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7216 : : most places should use cp_build_addr_expr instead. */
7217 : :
7218 : : tree
7219 : 282805327 : build_address (tree t)
7220 : : {
7221 : 282805327 : if (error_operand_p (t) || !cxx_mark_addressable (t))
7222 : 21 : return error_mark_node;
7223 : 282805306 : gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7224 : : || processing_template_decl);
7225 : 282805306 : t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7226 : 282805306 : if (TREE_CODE (t) != ADDR_EXPR)
7227 : 49621979 : t = rvalue (t);
7228 : : return t;
7229 : : }
7230 : :
7231 : : /* Return a NOP_EXPR converting EXPR to TYPE. */
7232 : :
7233 : : tree
7234 : 473420152 : build_nop (tree type, tree expr MEM_STAT_DECL)
7235 : : {
7236 : 473420152 : if (type == error_mark_node || error_operand_p (expr))
7237 : : return expr;
7238 : 473420071 : return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr PASS_MEM_STAT);
7239 : : }
7240 : :
7241 : : /* Take the address of ARG, whatever that means under C++ semantics.
7242 : : If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7243 : : and class rvalues as well.
7244 : :
7245 : : Nothing should call this function directly; instead, callers should use
7246 : : cp_build_addr_expr or cp_build_addr_expr_strict. */
7247 : :
7248 : : static tree
7249 : 218447417 : cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7250 : : {
7251 : 218447417 : tree argtype;
7252 : 218447417 : tree val;
7253 : :
7254 : 218447417 : if (!arg || error_operand_p (arg))
7255 : 12 : return error_mark_node;
7256 : :
7257 : 218447405 : arg = mark_lvalue_use (arg);
7258 : 218447405 : if (error_operand_p (arg))
7259 : 3 : return error_mark_node;
7260 : :
7261 : 218447402 : argtype = lvalue_type (arg);
7262 : 218447402 : location_t loc = cp_expr_loc_or_input_loc (arg);
7263 : :
7264 : 218447402 : gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7265 : :
7266 : 13117022 : if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7267 : 218447596 : && !really_overloaded_fn (arg))
7268 : : {
7269 : : /* They're trying to take the address of a unique non-static
7270 : : member function. This is ill-formed (except in MS-land),
7271 : : but let's try to DTRT.
7272 : : Note: We only handle unique functions here because we don't
7273 : : want to complain if there's a static overload; non-unique
7274 : : cases will be handled by instantiate_type. But we need to
7275 : : handle this case here to allow casts on the resulting PMF.
7276 : : We could defer this in non-MS mode, but it's easier to give
7277 : : a useful error here. */
7278 : :
7279 : : /* Inside constant member functions, the `this' pointer
7280 : : contains an extra const qualifier. TYPE_MAIN_VARIANT
7281 : : is used here to remove this const from the diagnostics
7282 : : and the created OFFSET_REF. */
7283 : 70 : tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7284 : 70 : tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7285 : 70 : if (!mark_used (fn, complain) && !(complain & tf_error))
7286 : 0 : return error_mark_node;
7287 : : /* Until microsoft headers are known to incorrectly take the address of
7288 : : unqualified xobj member functions we should not support this
7289 : : extension.
7290 : : See comment in class.cc:resolve_address_of_overloaded_function for
7291 : : the extended reasoning. */
7292 : 70 : if (!flag_ms_extensions || DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7293 : : {
7294 : 64 : auto_diagnostic_group d;
7295 : 64 : tree name = DECL_NAME (fn);
7296 : 64 : if (!(complain & tf_error))
7297 : 0 : return error_mark_node;
7298 : 64 : else if (current_class_type
7299 : 64 : && TREE_OPERAND (arg, 0) == current_class_ref)
7300 : : /* An expression like &memfn. */
7301 : 31 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7302 : 21 : permerror (loc,
7303 : : "ISO C++ forbids taking the address of an unqualified"
7304 : : " or parenthesized non-static member function to form"
7305 : : " a pointer to member function. Say %<&%T::%D%>",
7306 : : base, name);
7307 : : else
7308 : 10 : error_at (loc,
7309 : : "ISO C++ forbids taking the address of an unqualified"
7310 : : " or parenthesized non-static member function to form"
7311 : : " a pointer to explicit object member function");
7312 : : else
7313 : 33 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7314 : 9 : permerror (loc,
7315 : : "ISO C++ forbids taking the address of a bound member"
7316 : : " function to form a pointer to member function."
7317 : : " Say %<&%T::%D%>",
7318 : : base, name);
7319 : : else
7320 : 24 : error_at (loc,
7321 : : "ISO C++ forbids taking the address of a bound member"
7322 : : " function to form a pointer to explicit object member"
7323 : : " function");
7324 : 64 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7325 : 34 : inform (loc,
7326 : : "a pointer to explicit object member function can only be "
7327 : : "formed with %<&%T::%D%>", base, name);
7328 : 64 : }
7329 : 70 : arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7330 : : }
7331 : :
7332 : : /* Uninstantiated types are all functions. Taking the
7333 : : address of a function is a no-op, so just return the
7334 : : argument. */
7335 : 218447402 : if (type_unknown_p (arg))
7336 : 1932 : return build1 (ADDR_EXPR, unknown_type_node, arg);
7337 : :
7338 : 218445470 : if (TREE_CODE (arg) == OFFSET_REF)
7339 : : /* We want a pointer to member; bypass all the code for actually taking
7340 : : the address of something. */
7341 : 65561 : goto offset_ref;
7342 : :
7343 : : /* Anything not already handled and not a true memory reference
7344 : : is an error. */
7345 : 218379909 : if (!FUNC_OR_METHOD_TYPE_P (argtype))
7346 : : {
7347 : 114097788 : cp_lvalue_kind kind = lvalue_kind (arg);
7348 : 114097788 : if (kind == clk_none)
7349 : : {
7350 : 23 : if (complain & tf_error)
7351 : 23 : lvalue_error (loc, lv_addressof);
7352 : 23 : return error_mark_node;
7353 : : }
7354 : 114097765 : if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7355 : : {
7356 : 36 : if (!(complain & tf_error))
7357 : 9 : return error_mark_node;
7358 : : /* Make this a permerror because we used to accept it. */
7359 : 27 : permerror (loc, "taking address of rvalue");
7360 : : }
7361 : : }
7362 : :
7363 : 218379877 : if (TYPE_REF_P (argtype))
7364 : : {
7365 : 236 : tree type = build_pointer_type (TREE_TYPE (argtype));
7366 : 236 : arg = build1 (CONVERT_EXPR, type, arg);
7367 : 236 : return arg;
7368 : : }
7369 : 218379641 : else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7370 : : {
7371 : : /* ARM $3.4 */
7372 : : /* Apparently a lot of autoconf scripts for C++ packages do this,
7373 : : so only complain if -Wpedantic. */
7374 : 9 : if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7375 : 9 : pedwarn (loc, OPT_Wpedantic,
7376 : : "ISO C++ forbids taking address of function %<::main%>");
7377 : 0 : else if (flag_pedantic_errors)
7378 : 0 : return error_mark_node;
7379 : : }
7380 : :
7381 : : /* Let &* cancel out to simplify resulting code. */
7382 : 218379641 : if (INDIRECT_REF_P (arg))
7383 : : {
7384 : 73346495 : arg = TREE_OPERAND (arg, 0);
7385 : 73346495 : if (TYPE_REF_P (TREE_TYPE (arg)))
7386 : : {
7387 : 43849194 : tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7388 : 43849194 : arg = build1 (CONVERT_EXPR, type, arg);
7389 : : }
7390 : : else
7391 : : /* Don't let this be an lvalue. */
7392 : 29497301 : arg = rvalue (arg);
7393 : 73346495 : return arg;
7394 : : }
7395 : :
7396 : : /* Handle complex lvalues (when permitted)
7397 : : by reduction to simpler cases. */
7398 : 145033146 : val = unary_complex_lvalue (ADDR_EXPR, arg);
7399 : 145033146 : if (val != 0)
7400 : : return val;
7401 : :
7402 : 144543564 : switch (TREE_CODE (arg))
7403 : : {
7404 : 0 : CASE_CONVERT:
7405 : 0 : case FLOAT_EXPR:
7406 : 0 : case FIX_TRUNC_EXPR:
7407 : : /* We should have handled this above in the lvalue_kind check. */
7408 : 0 : gcc_unreachable ();
7409 : 65372 : break;
7410 : :
7411 : 65372 : case BASELINK:
7412 : 65372 : arg = BASELINK_FUNCTIONS (arg);
7413 : : /* Fall through. */
7414 : :
7415 : 65372 : case OVERLOAD:
7416 : 65372 : arg = OVL_FIRST (arg);
7417 : : break;
7418 : :
7419 : 65561 : case OFFSET_REF:
7420 : 65561 : offset_ref:
7421 : : /* Turn a reference to a non-static data member into a
7422 : : pointer-to-member. */
7423 : 65561 : {
7424 : 65561 : tree type;
7425 : 65561 : tree t;
7426 : :
7427 : 65561 : gcc_assert (PTRMEM_OK_P (arg));
7428 : :
7429 : 65561 : t = TREE_OPERAND (arg, 1);
7430 : 65561 : if (TYPE_REF_P (TREE_TYPE (t)))
7431 : : {
7432 : 15 : if (complain & tf_error)
7433 : 15 : error_at (loc,
7434 : : "cannot create pointer to reference member %qD", t);
7435 : 15 : return error_mark_node;
7436 : : }
7437 : :
7438 : : /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7439 : 65546 : if (TREE_CODE (t) == FUNCTION_DECL
7440 : 62746 : && !DECL_PURE_VIRTUAL_P (t)
7441 : 128271 : && !mark_used (t, complain) && !(complain & tf_error))
7442 : 3 : return error_mark_node;
7443 : :
7444 : : /* Pull out the function_decl for a single xobj member function, and
7445 : : let the rest of this function handle it. This is similar to how
7446 : : static member functions are handled in the BASELINK case above. */
7447 : 65543 : if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7448 : : {
7449 : : arg = t;
7450 : : break;
7451 : : }
7452 : :
7453 : 65487 : type = build_ptrmem_type (context_for_name_lookup (t),
7454 : 65487 : TREE_TYPE (t));
7455 : 65487 : t = make_ptrmem_cst (type, t);
7456 : 65487 : return t;
7457 : : }
7458 : :
7459 : : default:
7460 : : break;
7461 : : }
7462 : :
7463 : 144543620 : if (argtype != error_mark_node)
7464 : 144543620 : argtype = build_pointer_type (argtype);
7465 : :
7466 : 144543620 : if (bitfield_p (arg))
7467 : : {
7468 : 33 : if (complain & tf_error)
7469 : 33 : error_at (loc, "attempt to take address of bit-field");
7470 : 33 : return error_mark_node;
7471 : : }
7472 : :
7473 : : /* In a template, we are processing a non-dependent expression
7474 : : so we can just form an ADDR_EXPR with the correct type. */
7475 : 144543587 : if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7476 : : {
7477 : 131728886 : if (!mark_single_function (arg, complain))
7478 : 3 : return error_mark_node;
7479 : 131728883 : val = build_address (arg);
7480 : 131728883 : if (TREE_CODE (arg) == OFFSET_REF)
7481 : 0 : PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7482 : : }
7483 : 12814701 : else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7484 : : {
7485 : 27 : tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7486 : :
7487 : 27 : if (TREE_CODE (fn) == OVERLOAD && OVL_SINGLE_P (fn))
7488 : 27 : fn = OVL_FIRST (fn);
7489 : :
7490 : : /* We can only get here with a single static member
7491 : : function. */
7492 : 27 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7493 : : && DECL_STATIC_FUNCTION_P (fn));
7494 : 27 : if (!mark_used (fn, complain) && !(complain & tf_error))
7495 : 0 : return error_mark_node;
7496 : 27 : val = build_address (fn);
7497 : 27 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7498 : : /* Do not lose object's side effects. */
7499 : 12 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7500 : 12 : TREE_OPERAND (arg, 0), val);
7501 : : }
7502 : : else
7503 : : {
7504 : 12814674 : tree object = TREE_OPERAND (arg, 0);
7505 : 12814674 : tree field = TREE_OPERAND (arg, 1);
7506 : 12814674 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7507 : : (TREE_TYPE (object), decl_type_context (field)));
7508 : 12814674 : val = build_address (arg);
7509 : : }
7510 : :
7511 : 144543584 : if (TYPE_PTR_P (argtype)
7512 : 144543584 : && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7513 : : {
7514 : 1063 : build_ptrmemfunc_type (argtype);
7515 : 1063 : val = build_ptrmemfunc (argtype, val, 0,
7516 : : /*c_cast_p=*/false,
7517 : : complain);
7518 : : }
7519 : :
7520 : : /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7521 : 144543584 : if (TREE_CODE (val) == ADDR_EXPR
7522 : 144543584 : && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7523 : 102184207 : SET_EXPR_LOCATION (val, input_location);
7524 : :
7525 : : return val;
7526 : : }
7527 : :
7528 : : /* Take the address of ARG if it has one, even if it's an rvalue. */
7529 : :
7530 : : tree
7531 : 214717390 : cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7532 : : {
7533 : 214717390 : return cp_build_addr_expr_1 (arg, 0, complain);
7534 : : }
7535 : :
7536 : : /* Take the address of ARG, but only if it's an lvalue. */
7537 : :
7538 : : static tree
7539 : 3730027 : cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7540 : : {
7541 : 3730027 : return cp_build_addr_expr_1 (arg, 1, complain);
7542 : : }
7543 : :
7544 : : /* C++: Must handle pointers to members.
7545 : :
7546 : : Perhaps type instantiation should be extended to handle conversion
7547 : : from aggregates to types we don't yet know we want? (Or are those
7548 : : cases typically errors which should be reported?)
7549 : :
7550 : : NOCONVERT suppresses the default promotions (such as from short to int). */
7551 : :
7552 : : tree
7553 : 27268663 : cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7554 : : tsubst_flags_t complain)
7555 : : {
7556 : : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7557 : 27268663 : tree arg = xarg;
7558 : 27268663 : location_t location = cp_expr_loc_or_input_loc (arg);
7559 : 27268663 : tree argtype = 0;
7560 : 27268663 : tree eptype = NULL_TREE;
7561 : 27268663 : const char *errstring = NULL;
7562 : 27268663 : tree val;
7563 : 27268663 : const char *invalid_op_diag;
7564 : :
7565 : 27268663 : if (!arg || error_operand_p (arg))
7566 : 0 : return error_mark_node;
7567 : :
7568 : 27268663 : arg = resolve_nondeduced_context (arg, complain);
7569 : :
7570 : 54537326 : if ((invalid_op_diag
7571 : 27268663 : = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7572 : : ? CONVERT_EXPR
7573 : : : code),
7574 : 27268663 : TREE_TYPE (arg))))
7575 : : {
7576 : 0 : if (complain & tf_error)
7577 : 0 : error (invalid_op_diag);
7578 : 0 : return error_mark_node;
7579 : : }
7580 : :
7581 : 27268663 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7582 : : {
7583 : 10790 : eptype = TREE_TYPE (arg);
7584 : 10790 : arg = TREE_OPERAND (arg, 0);
7585 : : }
7586 : :
7587 : 27268663 : switch (code)
7588 : : {
7589 : 2426435 : case UNARY_PLUS_EXPR:
7590 : 2426435 : case NEGATE_EXPR:
7591 : 2426435 : {
7592 : 2426435 : int flags = WANT_ARITH | WANT_ENUM;
7593 : : /* Unary plus (but not unary minus) is allowed on pointers. */
7594 : 2426435 : if (code == UNARY_PLUS_EXPR)
7595 : 166916 : flags |= WANT_POINTER;
7596 : 2426435 : arg = build_expr_type_conversion (flags, arg, true);
7597 : 2426435 : if (!arg)
7598 : 30 : errstring = (code == NEGATE_EXPR
7599 : 24 : ? _("wrong type argument to unary minus")
7600 : 6 : : _("wrong type argument to unary plus"));
7601 : : else
7602 : : {
7603 : 2426405 : if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7604 : 615214 : arg = cp_perform_integral_promotions (arg, complain);
7605 : :
7606 : : /* Make sure the result is not an lvalue: a unary plus or minus
7607 : : expression is always a rvalue. */
7608 : 2426405 : arg = rvalue (arg);
7609 : : }
7610 : : }
7611 : : break;
7612 : :
7613 : 874412 : case BIT_NOT_EXPR:
7614 : 874412 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7615 : : {
7616 : 15 : code = CONJ_EXPR;
7617 : 15 : if (!noconvert)
7618 : : {
7619 : 15 : arg = cp_default_conversion (arg, complain);
7620 : 15 : if (arg == error_mark_node)
7621 : : return error_mark_node;
7622 : : }
7623 : : }
7624 : 874397 : else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7625 : : | WANT_VECTOR_OR_COMPLEX,
7626 : : arg, true)))
7627 : 13 : errstring = _("wrong type argument to bit-complement");
7628 : 874384 : else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7629 : : {
7630 : : /* Warn if the expression has boolean value. */
7631 : 874149 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7632 : 874149 : && (complain & tf_warning))
7633 : : {
7634 : 61 : auto_diagnostic_group d;
7635 : 61 : if (warning_at (location, OPT_Wbool_operation,
7636 : : "%<~%> on an expression of type %<bool%>"))
7637 : 42 : inform (location, "did you mean to use logical not (%<!%>)?");
7638 : 61 : }
7639 : 874149 : arg = cp_perform_integral_promotions (arg, complain);
7640 : : }
7641 : 235 : else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7642 : 235 : arg = mark_rvalue_use (arg);
7643 : : break;
7644 : :
7645 : 0 : case ABS_EXPR:
7646 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7647 : 0 : errstring = _("wrong type argument to abs");
7648 : 0 : else if (!noconvert)
7649 : : {
7650 : 0 : arg = cp_default_conversion (arg, complain);
7651 : 0 : if (arg == error_mark_node)
7652 : : return error_mark_node;
7653 : : }
7654 : : break;
7655 : :
7656 : 0 : case CONJ_EXPR:
7657 : : /* Conjugating a real value is a no-op, but allow it anyway. */
7658 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7659 : 0 : errstring = _("wrong type argument to conjugation");
7660 : 0 : else if (!noconvert)
7661 : : {
7662 : 0 : arg = cp_default_conversion (arg, complain);
7663 : 0 : if (arg == error_mark_node)
7664 : : return error_mark_node;
7665 : : }
7666 : : break;
7667 : :
7668 : 15191641 : case TRUTH_NOT_EXPR:
7669 : 15191641 : if (gnu_vector_type_p (TREE_TYPE (arg)))
7670 : 60 : return cp_build_binary_op (input_location, EQ_EXPR, arg,
7671 : 60 : build_zero_cst (TREE_TYPE (arg)), complain);
7672 : 15191581 : arg = perform_implicit_conversion (boolean_type_node, arg,
7673 : : complain);
7674 : 15191581 : if (arg != error_mark_node)
7675 : : {
7676 : 15191570 : if (processing_template_decl)
7677 : 7293833 : return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
7678 : 7897737 : val = invert_truthvalue_loc (location, arg);
7679 : 7897737 : if (obvalue_p (val))
7680 : 12701 : val = non_lvalue_loc (location, val);
7681 : 7897737 : return val;
7682 : : }
7683 : 11 : errstring = _("in argument to unary !");
7684 : 11 : break;
7685 : :
7686 : : case NOP_EXPR:
7687 : : break;
7688 : :
7689 : 469509 : case REALPART_EXPR:
7690 : 469509 : case IMAGPART_EXPR:
7691 : 469509 : val = build_real_imag_expr (input_location, code, arg);
7692 : 469509 : if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
7693 : 0 : val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
7694 : 0 : TREE_TYPE (eptype), val);
7695 : : return val;
7696 : :
7697 : 7710428 : case PREINCREMENT_EXPR:
7698 : 7710428 : case POSTINCREMENT_EXPR:
7699 : 7710428 : case PREDECREMENT_EXPR:
7700 : 7710428 : case POSTDECREMENT_EXPR:
7701 : : /* Handle complex lvalues (when permitted)
7702 : : by reduction to simpler cases. */
7703 : :
7704 : 7710428 : val = unary_complex_lvalue (code, arg);
7705 : 7710428 : if (val != 0)
7706 : 30 : goto return_build_unary_op;
7707 : :
7708 : 7710398 : tree stripped_arg;
7709 : 7710398 : stripped_arg = tree_strip_any_location_wrapper (arg);
7710 : 1408791 : if ((VAR_P (stripped_arg) || TREE_CODE (stripped_arg) == PARM_DECL)
7711 : 7223318 : && !DECL_READ_P (stripped_arg)
7712 : 8807939 : && (VAR_P (stripped_arg) ? warn_unused_but_set_variable
7713 : : : warn_unused_but_set_parameter) > 1)
7714 : : {
7715 : 9661 : arg = mark_lvalue_use (arg);
7716 : 9661 : DECL_READ_P (stripped_arg) = 0;
7717 : : }
7718 : : else
7719 : 7700737 : arg = mark_lvalue_use (arg);
7720 : :
7721 : : /* Increment or decrement the real part of the value,
7722 : : and don't change the imaginary part. */
7723 : 7710398 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7724 : : {
7725 : 18 : tree real, imag;
7726 : :
7727 : 18 : arg = cp_stabilize_reference (arg);
7728 : 18 : real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7729 : 18 : imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7730 : 18 : real = cp_build_unary_op (code, real, true, complain);
7731 : 18 : if (real == error_mark_node || imag == error_mark_node)
7732 : : return error_mark_node;
7733 : 15 : val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
7734 : 15 : goto return_build_unary_op;
7735 : : }
7736 : :
7737 : : /* Report invalid types. */
7738 : :
7739 : 7710380 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7740 : : arg, true)))
7741 : : {
7742 : 36 : if (code == PREINCREMENT_EXPR)
7743 : 9 : errstring = _("no pre-increment operator for type");
7744 : 27 : else if (code == POSTINCREMENT_EXPR)
7745 : 9 : errstring = _("no post-increment operator for type");
7746 : 18 : else if (code == PREDECREMENT_EXPR)
7747 : 9 : errstring = _("no pre-decrement operator for type");
7748 : : else
7749 : 9 : errstring = _("no post-decrement operator for type");
7750 : : break;
7751 : : }
7752 : 7710344 : else if (arg == error_mark_node)
7753 : : return error_mark_node;
7754 : :
7755 : : /* Report something read-only. */
7756 : :
7757 : 7710344 : if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7758 : 7710344 : || TREE_READONLY (arg))
7759 : : {
7760 : 61 : if (complain & tf_error)
7761 : 61 : cxx_readonly_error (location, arg,
7762 : 61 : ((code == PREINCREMENT_EXPR
7763 : 61 : || code == POSTINCREMENT_EXPR)
7764 : : ? lv_increment : lv_decrement));
7765 : : else
7766 : 0 : return error_mark_node;
7767 : : }
7768 : :
7769 : 7710344 : {
7770 : 7710344 : tree inc;
7771 : 7710344 : tree declared_type = unlowered_expr_type (arg);
7772 : :
7773 : 7710344 : argtype = TREE_TYPE (arg);
7774 : :
7775 : : /* ARM $5.2.5 last annotation says this should be forbidden. */
7776 : 7710344 : if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7777 : : {
7778 : 0 : if (complain & tf_error)
7779 : 0 : permerror (location, (code == PREINCREMENT_EXPR
7780 : 0 : || code == POSTINCREMENT_EXPR)
7781 : : ? G_("ISO C++ forbids incrementing an enum")
7782 : : : G_("ISO C++ forbids decrementing an enum"));
7783 : : else
7784 : 0 : return error_mark_node;
7785 : : }
7786 : :
7787 : : /* Compute the increment. */
7788 : :
7789 : 7710344 : if (TYPE_PTR_P (argtype))
7790 : : {
7791 : 2225164 : tree type = complete_type (TREE_TYPE (argtype));
7792 : :
7793 : 2225164 : if (!COMPLETE_OR_VOID_TYPE_P (type))
7794 : : {
7795 : 11 : if (complain & tf_error)
7796 : 9 : error_at (location, ((code == PREINCREMENT_EXPR
7797 : 9 : || code == POSTINCREMENT_EXPR))
7798 : : ? G_("cannot increment a pointer to incomplete "
7799 : : "type %qT")
7800 : : : G_("cannot decrement a pointer to incomplete "
7801 : : "type %qT"),
7802 : 9 : TREE_TYPE (argtype));
7803 : : else
7804 : 2 : return error_mark_node;
7805 : : }
7806 : 2225153 : else if (!TYPE_PTROB_P (argtype))
7807 : : {
7808 : 75 : if (complain & tf_error)
7809 : 63 : pedwarn (location, OPT_Wpointer_arith,
7810 : 63 : (code == PREINCREMENT_EXPR
7811 : 63 : || code == POSTINCREMENT_EXPR)
7812 : : ? G_("ISO C++ forbids incrementing a pointer "
7813 : : "of type %qT")
7814 : : : G_("ISO C++ forbids decrementing a pointer "
7815 : : "of type %qT"),
7816 : : argtype);
7817 : : else
7818 : 12 : return error_mark_node;
7819 : : }
7820 : 2225078 : else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7821 : 2225078 : TREE_TYPE (argtype),
7822 : : !(complain & tf_error)))
7823 : 0 : return error_mark_node;
7824 : :
7825 : 2225150 : inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7826 : : }
7827 : : else
7828 : 5485037 : inc = VECTOR_TYPE_P (argtype)
7829 : 5485180 : ? build_one_cst (argtype)
7830 : : : integer_one_node;
7831 : :
7832 : 7710330 : inc = cp_convert (argtype, inc, complain);
7833 : :
7834 : : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7835 : : need to ask Objective-C to build the increment or decrement
7836 : : expression for it. */
7837 : 7710330 : if (objc_is_property_ref (arg))
7838 : 0 : return objc_build_incr_expr_for_property_ref (input_location, code,
7839 : 0 : arg, inc);
7840 : :
7841 : : /* Complain about anything else that is not a true lvalue. */
7842 : 7710330 : if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7843 : 7710330 : || code == POSTINCREMENT_EXPR)
7844 : : ? lv_increment : lv_decrement),
7845 : : complain))
7846 : 33 : return error_mark_node;
7847 : :
7848 : : /* [depr.volatile.type] "Postfix ++ and -- expressions and
7849 : : prefix ++ and -- expressions of volatile-qualified arithmetic
7850 : : and pointer types are deprecated." */
7851 : 7709948 : if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7852 : 7710297 : && (complain & tf_warning))
7853 : 462 : warning_at (location, OPT_Wvolatile,
7854 : : "%qs expression of %<volatile%>-qualified type is "
7855 : : "deprecated",
7856 : : ((code == PREINCREMENT_EXPR
7857 : : || code == POSTINCREMENT_EXPR)
7858 : : ? "++" : "--"));
7859 : :
7860 : : /* Forbid using -- or ++ in C++17 on `bool'. */
7861 : 7710297 : if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7862 : : {
7863 : 95 : if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7864 : : {
7865 : 33 : if (complain & tf_error)
7866 : 33 : error_at (location,
7867 : : "use of an operand of type %qT in %<operator--%> "
7868 : : "is forbidden", boolean_type_node);
7869 : 33 : return error_mark_node;
7870 : : }
7871 : : else
7872 : : {
7873 : 62 : if (cxx_dialect >= cxx17)
7874 : : {
7875 : 33 : if (complain & tf_error)
7876 : 32 : error_at (location,
7877 : : "use of an operand of type %qT in "
7878 : : "%<operator++%> is forbidden in C++17",
7879 : : boolean_type_node);
7880 : 33 : return error_mark_node;
7881 : : }
7882 : : /* Otherwise, [depr.incr.bool] says this is deprecated. */
7883 : 29 : else if (complain & tf_warning)
7884 : 25 : warning_at (location, OPT_Wdeprecated,
7885 : : "use of an operand of type %qT "
7886 : : "in %<operator++%> is deprecated",
7887 : : boolean_type_node);
7888 : : }
7889 : 29 : val = boolean_increment (code, arg);
7890 : : }
7891 : 7710202 : else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7892 : : /* An rvalue has no cv-qualifiers. */
7893 : 1118232 : val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7894 : : else
7895 : 6591970 : val = build2 (code, TREE_TYPE (arg), arg, inc);
7896 : :
7897 : 7710231 : TREE_SIDE_EFFECTS (val) = 1;
7898 : 7710231 : goto return_build_unary_op;
7899 : : }
7900 : :
7901 : 596238 : case ADDR_EXPR:
7902 : : /* Note that this operation never does default_conversion
7903 : : regardless of NOCONVERT. */
7904 : 596238 : return cp_build_addr_expr (arg, complain);
7905 : :
7906 : : default:
7907 : : break;
7908 : : }
7909 : :
7910 : 3300879 : if (!errstring)
7911 : : {
7912 : 3300804 : if (argtype == 0)
7913 : 3300804 : argtype = TREE_TYPE (arg);
7914 : 3300804 : val = build1 (code, argtype, arg);
7915 : 11011080 : return_build_unary_op:
7916 : 11011080 : if (eptype)
7917 : 10781 : val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
7918 : 11011080 : return val;
7919 : : }
7920 : :
7921 : 90 : if (complain & tf_error)
7922 : 48 : error_at (location, "%s", errstring);
7923 : 90 : return error_mark_node;
7924 : : }
7925 : :
7926 : : /* Hook for the c-common bits that build a unary op. */
7927 : : tree
7928 : 4136 : build_unary_op (location_t /*location*/,
7929 : : enum tree_code code, tree xarg, bool noconvert)
7930 : : {
7931 : 4136 : return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
7932 : : }
7933 : :
7934 : : /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7935 : : so that it is a valid lvalue even for GENERIC by replacing
7936 : : (lhs = rhs) with ((lhs = rhs), lhs)
7937 : : (--lhs) with ((--lhs), lhs)
7938 : : (++lhs) with ((++lhs), lhs)
7939 : : and if lhs has side-effects, calling cp_stabilize_reference on it, so
7940 : : that it can be evaluated multiple times. */
7941 : :
7942 : : tree
7943 : 386570 : genericize_compound_lvalue (tree lvalue)
7944 : : {
7945 : 386570 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7946 : 196 : lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7947 : 196 : cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7948 : 196 : TREE_OPERAND (lvalue, 1));
7949 : 386570 : return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7950 : 386570 : lvalue, TREE_OPERAND (lvalue, 0));
7951 : : }
7952 : :
7953 : : /* Apply unary lvalue-demanding operator CODE to the expression ARG
7954 : : for certain kinds of expressions which are not really lvalues
7955 : : but which we can accept as lvalues.
7956 : :
7957 : : If ARG is not a kind of expression we can handle, return
7958 : : NULL_TREE. */
7959 : :
7960 : : tree
7961 : 267069452 : unary_complex_lvalue (enum tree_code code, tree arg)
7962 : : {
7963 : : /* Inside a template, making these kinds of adjustments is
7964 : : pointless; we are only concerned with the type of the
7965 : : expression. */
7966 : 267455816 : if (processing_template_decl)
7967 : : return NULL_TREE;
7968 : :
7969 : : /* Handle (a, b) used as an "lvalue". */
7970 : 186762232 : if (TREE_CODE (arg) == COMPOUND_EXPR)
7971 : : {
7972 : 386811 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7973 : : tf_warning_or_error);
7974 : 386811 : return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7975 : 773622 : TREE_OPERAND (arg, 0), real_result);
7976 : : }
7977 : :
7978 : : /* Handle (a ? b : c) used as an "lvalue". */
7979 : 186375421 : if (TREE_CODE (arg) == COND_EXPR
7980 : 186272599 : || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7981 : 102822 : return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7982 : :
7983 : : /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7984 : 186272599 : if (TREE_CODE (arg) == MODIFY_EXPR
7985 : 185886418 : || TREE_CODE (arg) == PREINCREMENT_EXPR
7986 : 185886334 : || TREE_CODE (arg) == PREDECREMENT_EXPR)
7987 : 386364 : return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7988 : :
7989 : 185886235 : if (code != ADDR_EXPR)
7990 : : return NULL_TREE;
7991 : :
7992 : : /* Handle (a = b) used as an "lvalue" for `&'. */
7993 : 182762078 : if (TREE_CODE (arg) == MODIFY_EXPR
7994 : 182762078 : || TREE_CODE (arg) == INIT_EXPR)
7995 : : {
7996 : 0 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
7997 : : tf_warning_or_error);
7998 : 0 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7999 : : arg, real_result);
8000 : 0 : suppress_warning (arg /* What warning? */);
8001 : 0 : return arg;
8002 : : }
8003 : :
8004 : 282949113 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
8005 : 282948071 : || TREE_CODE (arg) == OFFSET_REF)
8006 : : return NULL_TREE;
8007 : :
8008 : : /* We permit compiler to make function calls returning
8009 : : objects of aggregate type look like lvalues. */
8010 : 100185993 : {
8011 : 100185993 : tree targ = arg;
8012 : :
8013 : 100185993 : if (TREE_CODE (targ) == SAVE_EXPR)
8014 : 0 : targ = TREE_OPERAND (targ, 0);
8015 : :
8016 : 100185993 : if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
8017 : : {
8018 : 29 : if (TREE_CODE (arg) == SAVE_EXPR)
8019 : : targ = arg;
8020 : : else
8021 : 29 : targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
8022 : 29 : return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
8023 : : }
8024 : :
8025 : 100185964 : if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
8026 : 0 : return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
8027 : 0 : TREE_OPERAND (targ, 0), current_function_decl, NULL);
8028 : : }
8029 : :
8030 : : /* Don't let anything else be handled specially. */
8031 : : return NULL_TREE;
8032 : : }
8033 : :
8034 : : /* Mark EXP saying that we need to be able to take the
8035 : : address of it; it should not be allocated in a register.
8036 : : Value is true if successful. ARRAY_REF_P is true if this
8037 : : is for ARRAY_REF construction - in that case we don't want
8038 : : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
8039 : : it is fine to use ARRAY_REFs for vector subscripts on vector
8040 : : register variables.
8041 : :
8042 : : C++: we do not allow `current_class_ptr' to be addressable. */
8043 : :
8044 : : bool
8045 : 297804600 : cxx_mark_addressable (tree exp, bool array_ref_p)
8046 : : {
8047 : 297804600 : tree x = exp;
8048 : :
8049 : 352310165 : while (1)
8050 : 352310165 : switch (TREE_CODE (x))
8051 : : {
8052 : 27572075 : case VIEW_CONVERT_EXPR:
8053 : 27572075 : if (array_ref_p
8054 : 723671 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8055 : 28127709 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
8056 : : return true;
8057 : 27569178 : x = TREE_OPERAND (x, 0);
8058 : 27569178 : break;
8059 : :
8060 : 26692651 : case COMPONENT_REF:
8061 : 26692651 : if (bitfield_p (x))
8062 : 9 : error ("attempt to take address of bit-field");
8063 : : /* FALLTHRU */
8064 : 26936387 : case ADDR_EXPR:
8065 : 26936387 : case ARRAY_REF:
8066 : 26936387 : case REALPART_EXPR:
8067 : 26936387 : case IMAGPART_EXPR:
8068 : 26936387 : x = TREE_OPERAND (x, 0);
8069 : 26936387 : break;
8070 : :
8071 : 6114262 : case PARM_DECL:
8072 : 6114262 : if (x == current_class_ptr)
8073 : : {
8074 : 18 : error ("cannot take the address of %<this%>, which is an rvalue expression");
8075 : 18 : TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
8076 : 18 : return true;
8077 : : }
8078 : : /* Fall through. */
8079 : :
8080 : 57771479 : case VAR_DECL:
8081 : : /* Caller should not be trying to mark initialized
8082 : : constant fields addressable. */
8083 : 57771479 : gcc_assert (DECL_LANG_SPECIFIC (x) == 0
8084 : : || DECL_IN_AGGR_P (x) == 0
8085 : : || TREE_STATIC (x)
8086 : : || DECL_EXTERNAL (x));
8087 : : /* Fall through. */
8088 : :
8089 : 57841671 : case RESULT_DECL:
8090 : 57841750 : if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
8091 : 57841744 : && !DECL_ARTIFICIAL (x))
8092 : : {
8093 : 22 : if (VAR_P (x) && DECL_HARD_REGISTER (x))
8094 : : {
8095 : 12 : error
8096 : 12 : ("address of explicit register variable %qD requested", x);
8097 : 12 : return false;
8098 : : }
8099 : 10 : else if (extra_warnings)
8100 : 3 : warning
8101 : 3 : (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
8102 : : }
8103 : 57841659 : TREE_ADDRESSABLE (x) = 1;
8104 : 57841659 : return true;
8105 : :
8106 : 147539369 : case CONST_DECL:
8107 : 147539369 : case FUNCTION_DECL:
8108 : 147539369 : TREE_ADDRESSABLE (x) = 1;
8109 : 147539369 : return true;
8110 : :
8111 : 53710 : case CONSTRUCTOR:
8112 : 53710 : TREE_ADDRESSABLE (x) = 1;
8113 : 53710 : return true;
8114 : :
8115 : 12846997 : case TARGET_EXPR:
8116 : 12846997 : TREE_ADDRESSABLE (x) = 1;
8117 : 12846997 : cxx_mark_addressable (TARGET_EXPR_SLOT (x));
8118 : 12846997 : return true;
8119 : :
8120 : : default:
8121 : : return true;
8122 : : }
8123 : : }
8124 : :
8125 : : /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
8126 : :
8127 : : tree
8128 : 6687570 : build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
8129 : : tsubst_flags_t complain)
8130 : : {
8131 : 6687570 : tree orig_ifexp = ifexp;
8132 : 6687570 : tree orig_op1 = op1;
8133 : 6687570 : tree orig_op2 = op2;
8134 : 6687570 : tree expr;
8135 : :
8136 : 6687570 : if (processing_template_decl)
8137 : : {
8138 : : /* The standard says that the expression is type-dependent if
8139 : : IFEXP is type-dependent, even though the eventual type of the
8140 : : expression doesn't dependent on IFEXP. */
8141 : 2708726 : if (type_dependent_expression_p (ifexp)
8142 : : /* As a GNU extension, the middle operand may be omitted. */
8143 : 1352157 : || (op1 && type_dependent_expression_p (op1))
8144 : 3609803 : || type_dependent_expression_p (op2))
8145 : 1837405 : return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
8146 : : }
8147 : :
8148 : 4850165 : expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
8149 : 4850165 : if (processing_template_decl && expr != error_mark_node)
8150 : : {
8151 : 871315 : tree min = build_min_non_dep (COND_EXPR, expr,
8152 : : orig_ifexp, orig_op1, orig_op2);
8153 : 871315 : expr = convert_from_reference (min);
8154 : : }
8155 : : return expr;
8156 : : }
8157 : :
8158 : : /* Given a list of expressions, return a compound expression
8159 : : that performs them all and returns the value of the last of them. */
8160 : :
8161 : : tree
8162 : 31221964 : build_x_compound_expr_from_list (tree list, expr_list_kind exp,
8163 : : tsubst_flags_t complain)
8164 : : {
8165 : 31221964 : tree expr = TREE_VALUE (list);
8166 : :
8167 : 255217 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8168 : 31477169 : && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
8169 : : {
8170 : 19 : if (complain & tf_error)
8171 : 38 : pedwarn (cp_expr_loc_or_input_loc (expr), 0,
8172 : : "list-initializer for non-class type must not "
8173 : : "be parenthesized");
8174 : : else
8175 : 0 : return error_mark_node;
8176 : : }
8177 : :
8178 : 31221964 : if (TREE_CHAIN (list))
8179 : : {
8180 : 24 : if (complain & tf_error)
8181 : 15 : switch (exp)
8182 : : {
8183 : 12 : case ELK_INIT:
8184 : 12 : permerror (input_location, "expression list treated as compound "
8185 : : "expression in initializer");
8186 : 12 : break;
8187 : 2 : case ELK_MEM_INIT:
8188 : 2 : permerror (input_location, "expression list treated as compound "
8189 : : "expression in mem-initializer");
8190 : 2 : break;
8191 : 1 : case ELK_FUNC_CAST:
8192 : 1 : permerror (input_location, "expression list treated as compound "
8193 : : "expression in functional cast");
8194 : 1 : break;
8195 : 0 : default:
8196 : 0 : gcc_unreachable ();
8197 : : }
8198 : : else
8199 : 9 : return error_mark_node;
8200 : :
8201 : 30 : for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
8202 : 30 : expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
8203 : 15 : expr, TREE_VALUE (list), NULL_TREE,
8204 : : complain);
8205 : : }
8206 : :
8207 : : return expr;
8208 : : }
8209 : :
8210 : : /* Like build_x_compound_expr_from_list, but using a VEC. */
8211 : :
8212 : : tree
8213 : 133521 : build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8214 : : tsubst_flags_t complain)
8215 : : {
8216 : 133521 : if (vec_safe_is_empty (vec))
8217 : : return NULL_TREE;
8218 : 133521 : else if (vec->length () == 1)
8219 : 133456 : return (*vec)[0];
8220 : : else
8221 : : {
8222 : 65 : tree expr;
8223 : 65 : unsigned int ix;
8224 : 65 : tree t;
8225 : :
8226 : 65 : if (msg != NULL)
8227 : : {
8228 : 5 : if (complain & tf_error)
8229 : 0 : permerror (input_location,
8230 : : "%s expression list treated as compound expression",
8231 : : msg);
8232 : : else
8233 : 5 : return error_mark_node;
8234 : : }
8235 : :
8236 : 60 : expr = (*vec)[0];
8237 : 134 : for (ix = 1; vec->iterate (ix, &t); ++ix)
8238 : 74 : expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
8239 : : t, NULL_TREE, complain);
8240 : :
8241 : : return expr;
8242 : : }
8243 : : }
8244 : :
8245 : : /* Handle overloading of the ',' operator when needed. */
8246 : :
8247 : : tree
8248 : 2763996 : build_x_compound_expr (location_t loc, tree op1, tree op2,
8249 : : tree lookups, tsubst_flags_t complain)
8250 : : {
8251 : 2763996 : tree result;
8252 : 2763996 : tree orig_op1 = op1;
8253 : 2763996 : tree orig_op2 = op2;
8254 : 2763996 : tree overload = NULL_TREE;
8255 : :
8256 : 2763996 : if (processing_template_decl)
8257 : : {
8258 : 2530983 : if (type_dependent_expression_p (op1)
8259 : 2530983 : || type_dependent_expression_p (op2))
8260 : : {
8261 : 2248973 : result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8262 : 2248973 : TREE_TYPE (result)
8263 : 2248973 : = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
8264 : 2248973 : return result;
8265 : : }
8266 : : }
8267 : :
8268 : 515023 : result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8269 : : NULL_TREE, lookups, &overload, complain);
8270 : 515023 : if (!result)
8271 : 507923 : result = cp_build_compound_expr (op1, op2, complain);
8272 : :
8273 : 515023 : if (processing_template_decl && result != error_mark_node)
8274 : : {
8275 : 280189 : if (overload != NULL_TREE)
8276 : 52 : return (build_min_non_dep_op_overload
8277 : 52 : (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
8278 : :
8279 : 280137 : return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
8280 : : }
8281 : :
8282 : : return result;
8283 : : }
8284 : :
8285 : : /* Like cp_build_compound_expr, but for the c-common bits. */
8286 : :
8287 : : tree
8288 : 11252 : build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8289 : : {
8290 : 11252 : return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8291 : : }
8292 : :
8293 : : /* Build a compound expression. */
8294 : :
8295 : : tree
8296 : 621571 : cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8297 : : {
8298 : 621571 : lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8299 : :
8300 : 621571 : if (lhs == error_mark_node || rhs == error_mark_node)
8301 : : return error_mark_node;
8302 : :
8303 : 621568 : if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8304 : 0 : lhs = TREE_OPERAND (lhs, 0);
8305 : 621568 : tree eptype = NULL_TREE;
8306 : 621568 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8307 : : {
8308 : 3 : eptype = TREE_TYPE (rhs);
8309 : 3 : rhs = TREE_OPERAND (rhs, 0);
8310 : : }
8311 : :
8312 : 621568 : if (TREE_CODE (rhs) == TARGET_EXPR)
8313 : : {
8314 : : /* If the rhs is a TARGET_EXPR, then build the compound
8315 : : expression inside the target_expr's initializer. This
8316 : : helps the compiler to eliminate unnecessary temporaries. */
8317 : 3190 : tree init = TARGET_EXPR_INITIAL (rhs);
8318 : :
8319 : 3190 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8320 : 3190 : TARGET_EXPR_INITIAL (rhs) = init;
8321 : :
8322 : 3190 : if (eptype)
8323 : 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8324 : 3190 : return rhs;
8325 : : }
8326 : :
8327 : 618378 : rhs = resolve_nondeduced_context (rhs, complain);
8328 : :
8329 : 618378 : if (type_unknown_p (rhs))
8330 : : {
8331 : 33 : if (complain & tf_error)
8332 : 51 : error_at (cp_expr_loc_or_input_loc (rhs),
8333 : : "no context to resolve type of %qE", rhs);
8334 : 33 : return error_mark_node;
8335 : : }
8336 : :
8337 : 618345 : tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8338 : 618345 : if (eptype)
8339 : 3 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8340 : : return ret;
8341 : : }
8342 : :
8343 : : /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8344 : : casts away constness. CAST gives the type of cast. Returns true
8345 : : if the cast is ill-formed, false if it is well-formed.
8346 : :
8347 : : ??? This function warns for casting away any qualifier not just
8348 : : const. We would like to specify exactly what qualifiers are casted
8349 : : away.
8350 : : */
8351 : :
8352 : : static bool
8353 : 708423 : check_for_casting_away_constness (location_t loc, tree src_type,
8354 : : tree dest_type, enum tree_code cast,
8355 : : tsubst_flags_t complain)
8356 : : {
8357 : : /* C-style casts are allowed to cast away constness. With
8358 : : WARN_CAST_QUAL, we still want to issue a warning. */
8359 : 708423 : if (cast == CAST_EXPR && !warn_cast_qual)
8360 : : return false;
8361 : :
8362 : 572408 : if (!casts_away_constness (src_type, dest_type, complain))
8363 : : return false;
8364 : :
8365 : 392 : switch (cast)
8366 : : {
8367 : 335 : case CAST_EXPR:
8368 : 335 : if (complain & tf_warning)
8369 : 335 : warning_at (loc, OPT_Wcast_qual,
8370 : : "cast from type %qT to type %qT casts away qualifiers",
8371 : : src_type, dest_type);
8372 : : return false;
8373 : :
8374 : 36 : case STATIC_CAST_EXPR:
8375 : 36 : if (complain & tf_error)
8376 : 24 : error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8377 : : "away qualifiers",
8378 : : src_type, dest_type);
8379 : : return true;
8380 : :
8381 : 21 : case REINTERPRET_CAST_EXPR:
8382 : 21 : if (complain & tf_error)
8383 : 15 : error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8384 : : "casts away qualifiers",
8385 : : src_type, dest_type);
8386 : : return true;
8387 : :
8388 : 0 : default:
8389 : 0 : gcc_unreachable();
8390 : : }
8391 : : }
8392 : :
8393 : : /* Warns if the cast from expression EXPR to type TYPE is useless. */
8394 : : void
8395 : 46136502 : maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8396 : : tsubst_flags_t complain)
8397 : : {
8398 : 46136502 : if (warn_useless_cast
8399 : 152 : && complain & tf_warning)
8400 : : {
8401 : 152 : if (TYPE_REF_P (type)
8402 : 152 : ? ((TYPE_REF_IS_RVALUE (type)
8403 : 86 : ? xvalue_p (expr) : lvalue_p (expr))
8404 : 172 : && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8405 : : /* Don't warn when converting a class object to a non-reference type,
8406 : : because that's a common way to create a temporary. */
8407 : 66 : : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8408 : 96 : warning_at (loc, OPT_Wuseless_cast,
8409 : : "useless cast to type %q#T", type);
8410 : : }
8411 : 46136502 : }
8412 : :
8413 : : /* Warns if the cast ignores cv-qualifiers on TYPE. */
8414 : : static void
8415 : 46126864 : maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8416 : : tsubst_flags_t complain)
8417 : : {
8418 : 46126864 : if (warn_ignored_qualifiers
8419 : 263591 : && complain & tf_warning
8420 : 263141 : && !CLASS_TYPE_P (type)
8421 : 46385222 : && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8422 : 36 : warning_at (loc, OPT_Wignored_qualifiers,
8423 : : "type qualifiers ignored on cast result type");
8424 : 46126864 : }
8425 : :
8426 : : /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8427 : : (another pointer-to-member type in the same hierarchy) and return
8428 : : the converted expression. If ALLOW_INVERSE_P is permitted, a
8429 : : pointer-to-derived may be converted to pointer-to-base; otherwise,
8430 : : only the other direction is permitted. If C_CAST_P is true, this
8431 : : conversion is taking place as part of a C-style cast. */
8432 : :
8433 : : tree
8434 : 30120 : convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8435 : : bool c_cast_p, tsubst_flags_t complain)
8436 : : {
8437 : 30120 : if (same_type_p (type, TREE_TYPE (expr)))
8438 : : return expr;
8439 : :
8440 : 30120 : if (TYPE_PTRDATAMEM_P (type))
8441 : : {
8442 : 364 : tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8443 : 364 : tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8444 : 364 : tree delta = (get_delta_difference
8445 : 364 : (obase, nbase,
8446 : : allow_inverse_p, c_cast_p, complain));
8447 : :
8448 : 364 : if (delta == error_mark_node)
8449 : : return error_mark_node;
8450 : :
8451 : 358 : if (!same_type_p (obase, nbase))
8452 : : {
8453 : 287 : if (TREE_CODE (expr) == PTRMEM_CST)
8454 : 159 : expr = cplus_expand_constant (expr);
8455 : :
8456 : 287 : tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8457 : 287 : build_int_cst (TREE_TYPE (expr), -1),
8458 : : complain);
8459 : 287 : tree op1 = build_nop (ptrdiff_type_node, expr);
8460 : 287 : tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8461 : : complain);
8462 : :
8463 : 287 : expr = fold_build3_loc (input_location,
8464 : : COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8465 : : }
8466 : :
8467 : 358 : return build_nop (type, expr);
8468 : : }
8469 : : else
8470 : 29756 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8471 : 29756 : allow_inverse_p, c_cast_p, complain);
8472 : : }
8473 : :
8474 : : /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8475 : : this static_cast is being attempted as one of the possible casts
8476 : : allowed by a C-style cast. (In that case, accessibility of base
8477 : : classes is not considered, and it is OK to cast away
8478 : : constness.) Return the result of the cast. *VALID_P is set to
8479 : : indicate whether or not the cast was valid. */
8480 : :
8481 : : static tree
8482 : 45760858 : build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8483 : : bool *valid_p, tsubst_flags_t complain)
8484 : : {
8485 : 45760858 : tree intype;
8486 : 45760858 : tree result;
8487 : 45760858 : cp_lvalue_kind clk;
8488 : :
8489 : : /* Assume the cast is valid. */
8490 : 45760858 : *valid_p = true;
8491 : :
8492 : 45760858 : intype = unlowered_expr_type (expr);
8493 : :
8494 : : /* Save casted types in the function's used types hash table. */
8495 : 45760858 : used_types_insert (type);
8496 : :
8497 : : /* A prvalue of non-class type is cv-unqualified. */
8498 : 45760858 : if (!CLASS_TYPE_P (type))
8499 : 43991237 : type = cv_unqualified (type);
8500 : :
8501 : : /* [expr.static.cast]
8502 : :
8503 : : An lvalue of type "cv1 B", where B is a class type, can be cast
8504 : : to type "reference to cv2 D", where D is a class derived (clause
8505 : : _class.derived_) from B, if a valid standard conversion from
8506 : : "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8507 : : same cv-qualification as, or greater cv-qualification than, cv1,
8508 : : and B is not a virtual base class of D. */
8509 : : /* We check this case before checking the validity of "TYPE t =
8510 : : EXPR;" below because for this case:
8511 : :
8512 : : struct B {};
8513 : : struct D : public B { D(const B&); };
8514 : : extern B& b;
8515 : : void f() { static_cast<const D&>(b); }
8516 : :
8517 : : we want to avoid constructing a new D. The standard is not
8518 : : completely clear about this issue, but our interpretation is
8519 : : consistent with other compilers. */
8520 : 45760858 : if (TYPE_REF_P (type)
8521 : 3692035 : && CLASS_TYPE_P (TREE_TYPE (type))
8522 : 2890165 : && CLASS_TYPE_P (intype)
8523 : 2890150 : && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8524 : 2882427 : && DERIVED_FROM_P (intype, TREE_TYPE (type))
8525 : 2849926 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8526 : 2849926 : build_pointer_type (TYPE_MAIN_VARIANT
8527 : : (TREE_TYPE (type))),
8528 : : complain)
8529 : 48610784 : && (c_cast_p
8530 : 2849902 : || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8531 : : {
8532 : 2849926 : tree base;
8533 : :
8534 : 2849926 : if (processing_template_decl)
8535 : : return expr;
8536 : :
8537 : : /* There is a standard conversion from "D*" to "B*" even if "B"
8538 : : is ambiguous or inaccessible. If this is really a
8539 : : static_cast, then we check both for inaccessibility and
8540 : : ambiguity. However, if this is a static_cast being performed
8541 : : because the user wrote a C-style cast, then accessibility is
8542 : : not considered. */
8543 : 5286272 : base = lookup_base (TREE_TYPE (type), intype,
8544 : : c_cast_p ? ba_unique : ba_check,
8545 : : NULL, complain);
8546 : 2643148 : expr = cp_build_addr_expr (expr, complain);
8547 : :
8548 : 2643148 : if (sanitize_flags_p (SANITIZE_VPTR))
8549 : : {
8550 : 483 : tree ubsan_check
8551 : 483 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8552 : : intype, expr);
8553 : 483 : if (ubsan_check)
8554 : 2643148 : expr = ubsan_check;
8555 : : }
8556 : :
8557 : : /* Convert from "B*" to "D*". This function will check that "B"
8558 : : is not a virtual base of "D". Even if we don't have a guarantee
8559 : : that expr is NULL, if the static_cast is to a reference type,
8560 : : it is UB if it would be NULL, so omit the non-NULL check. */
8561 : 2643148 : expr = build_base_path (MINUS_EXPR, expr, base,
8562 : : /*nonnull=*/flag_delete_null_pointer_checks,
8563 : : complain);
8564 : :
8565 : : /* Convert the pointer to a reference -- but then remember that
8566 : : there are no expressions with reference type in C++.
8567 : :
8568 : : We call rvalue so that there's an actual tree code
8569 : : (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8570 : : is a variable with the same type, the conversion would get folded
8571 : : away, leaving just the variable and causing lvalue_kind to give
8572 : : the wrong answer. */
8573 : 2643148 : expr = cp_fold_convert (type, expr);
8574 : :
8575 : : /* When -fsanitize=null, make sure to diagnose reference binding to
8576 : : NULL even when the reference is converted to pointer later on. */
8577 : 2643148 : if (sanitize_flags_p (SANITIZE_NULL)
8578 : 489 : && TREE_CODE (expr) == COND_EXPR
8579 : 6 : && TREE_OPERAND (expr, 2)
8580 : 6 : && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8581 : 2643154 : && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8582 : 6 : ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8583 : :
8584 : 2643148 : return convert_from_reference (rvalue (expr));
8585 : : }
8586 : :
8587 : : /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8588 : : cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8589 : 42910932 : if (TYPE_REF_P (type)
8590 : 842109 : && TYPE_REF_IS_RVALUE (type)
8591 : 460887 : && (clk = real_lvalue_p (expr))
8592 : 457557 : && reference_compatible_p (TREE_TYPE (type), intype)
8593 : 43368486 : && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8594 : : {
8595 : 457554 : if (processing_template_decl)
8596 : : return expr;
8597 : 457337 : if (clk == clk_ordinary)
8598 : : {
8599 : : /* Handle the (non-bit-field) lvalue case here by casting to
8600 : : lvalue reference and then changing it to an rvalue reference.
8601 : : Casting an xvalue to rvalue reference will be handled by the
8602 : : main code path. */
8603 : 457334 : tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8604 : 457334 : result = (perform_direct_initialization_if_possible
8605 : 457334 : (lref, expr, c_cast_p, complain));
8606 : 457334 : result = build1 (NON_LVALUE_EXPR, type, result);
8607 : 457334 : return convert_from_reference (result);
8608 : : }
8609 : : else
8610 : : /* For a bit-field or packed field, bind to a temporary. */
8611 : 3 : expr = rvalue (expr);
8612 : : }
8613 : :
8614 : : /* Resolve overloaded address here rather than once in
8615 : : implicit_conversion and again in the inverse code below. */
8616 : 42453381 : if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8617 : : {
8618 : 18 : expr = instantiate_type (type, expr, complain);
8619 : 18 : intype = TREE_TYPE (expr);
8620 : : }
8621 : :
8622 : : /* [expr.static.cast]
8623 : :
8624 : : Any expression can be explicitly converted to type cv void. */
8625 : 42453381 : if (VOID_TYPE_P (type))
8626 : : {
8627 : 414106 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8628 : 0 : expr = TREE_OPERAND (expr, 0);
8629 : 414106 : return convert_to_void (expr, ICV_CAST, complain);
8630 : : }
8631 : :
8632 : : /* [class.abstract]
8633 : : An abstract class shall not be used ... as the type of an explicit
8634 : : conversion. */
8635 : 42039275 : if (abstract_virtuals_error (ACU_CAST, type, complain))
8636 : 6 : return error_mark_node;
8637 : :
8638 : : /* [expr.static.cast]
8639 : :
8640 : : An expression e can be explicitly converted to a type T using a
8641 : : static_cast of the form static_cast<T>(e) if the declaration T
8642 : : t(e);" is well-formed, for some invented temporary variable
8643 : : t. */
8644 : 42039269 : result = perform_direct_initialization_if_possible (type, expr,
8645 : : c_cast_p, complain);
8646 : : /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8647 : : which initialize the first element of the aggregate. We need to handle
8648 : : the array case specifically. */
8649 : 42039269 : if (result == NULL_TREE
8650 : 4274936 : && cxx_dialect >= cxx20
8651 : 1183193 : && TREE_CODE (type) == ARRAY_TYPE)
8652 : : {
8653 : : /* Create { EXPR } and perform direct-initialization from it. */
8654 : 15 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8655 : 15 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8656 : 15 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8657 : 15 : result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8658 : : complain);
8659 : : }
8660 : 4274936 : if (result)
8661 : : {
8662 : 37764348 : if (processing_template_decl)
8663 : : return expr;
8664 : :
8665 : 37082804 : result = convert_from_reference (result);
8666 : :
8667 : : /* [expr.static.cast]
8668 : :
8669 : : If T is a reference type, the result is an lvalue; otherwise,
8670 : : the result is an rvalue. */
8671 : 37082804 : if (!TYPE_REF_P (type))
8672 : : {
8673 : 36698343 : result = rvalue (result);
8674 : :
8675 : 36698343 : if (result == expr && SCALAR_TYPE_P (type))
8676 : : /* Leave some record of the cast. */
8677 : 2105433 : result = build_nop (type, expr);
8678 : : }
8679 : 37082804 : return result;
8680 : : }
8681 : :
8682 : : /* [expr.static.cast]
8683 : :
8684 : : The inverse of any standard conversion sequence (clause _conv_),
8685 : : other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8686 : : (_conv.array_), function-to-pointer (_conv.func_), and boolean
8687 : : (_conv.bool_) conversions, can be performed explicitly using
8688 : : static_cast subject to the restriction that the explicit
8689 : : conversion does not cast away constness (_expr.const.cast_), and
8690 : : the following additional rules for specific cases: */
8691 : : /* For reference, the conversions not excluded are: integral
8692 : : promotions, floating-point promotion, integral conversions,
8693 : : floating-point conversions, floating-integral conversions,
8694 : : pointer conversions, and pointer to member conversions. */
8695 : : /* DR 128
8696 : :
8697 : : A value of integral _or enumeration_ type can be explicitly
8698 : : converted to an enumeration type. */
8699 : : /* The effect of all that is that any conversion between any two
8700 : : types which are integral, floating, or enumeration types can be
8701 : : performed. */
8702 : 4274921 : if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8703 : 2537418 : || SCALAR_FLOAT_TYPE_P (type))
8704 : 1908344 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8705 : 276829 : || SCALAR_FLOAT_TYPE_P (intype)))
8706 : : {
8707 : 1802344 : if (processing_template_decl)
8708 : : return expr;
8709 : 1728027 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8710 : 0 : expr = TREE_OPERAND (expr, 0);
8711 : : /* [expr.static.cast]: "If the value is not a bit-field, the result
8712 : : refers to the object or the specified base class subobject thereof;
8713 : : otherwise, the lvalue-to-rvalue conversion is applied to the
8714 : : bit-field and the resulting prvalue is used as the operand of the
8715 : : static_cast." There are no prvalue bit-fields; the l-to-r conversion
8716 : : will give us an object of the underlying type of the bit-field. */
8717 : 1728027 : expr = decay_conversion (expr, complain);
8718 : 1728027 : return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8719 : : }
8720 : :
8721 : 741663 : if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8722 : 737288 : && CLASS_TYPE_P (TREE_TYPE (type))
8723 : 203631 : && CLASS_TYPE_P (TREE_TYPE (intype))
8724 : 2511679 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8725 : : (TREE_TYPE (intype))),
8726 : 39102 : build_pointer_type (TYPE_MAIN_VARIANT
8727 : : (TREE_TYPE (type))),
8728 : : complain))
8729 : : {
8730 : 38763 : tree base;
8731 : :
8732 : 38763 : if (processing_template_decl)
8733 : : return expr;
8734 : :
8735 : 38745 : if (!c_cast_p
8736 : 38745 : && check_for_casting_away_constness (loc, intype, type,
8737 : : STATIC_CAST_EXPR,
8738 : : complain))
8739 : 6 : return error_mark_node;
8740 : 76939 : base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8741 : : c_cast_p ? ba_unique : ba_check,
8742 : : NULL, complain);
8743 : 38739 : expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8744 : : complain);
8745 : :
8746 : 38739 : if (sanitize_flags_p (SANITIZE_VPTR))
8747 : : {
8748 : 43 : tree ubsan_check
8749 : 43 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8750 : : intype, expr);
8751 : 43 : if (ubsan_check)
8752 : 38739 : expr = ubsan_check;
8753 : : }
8754 : :
8755 : 38739 : return cp_fold_convert (type, expr);
8756 : : }
8757 : :
8758 : 89 : if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8759 : 2433814 : || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8760 : : {
8761 : 232 : tree c1;
8762 : 232 : tree c2;
8763 : 232 : tree t1;
8764 : 232 : tree t2;
8765 : :
8766 : 232 : c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8767 : 232 : c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8768 : :
8769 : 232 : if (TYPE_PTRDATAMEM_P (type))
8770 : : {
8771 : 89 : t1 = (build_ptrmem_type
8772 : 89 : (c1,
8773 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8774 : 89 : t2 = (build_ptrmem_type
8775 : 89 : (c2,
8776 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8777 : : }
8778 : : else
8779 : : {
8780 : : t1 = intype;
8781 : : t2 = type;
8782 : : }
8783 : 232 : if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8784 : : {
8785 : 154 : if (!c_cast_p
8786 : 154 : && check_for_casting_away_constness (loc, intype, type,
8787 : : STATIC_CAST_EXPR,
8788 : : complain))
8789 : 9 : return error_mark_node;
8790 : 145 : if (processing_template_decl)
8791 : : return expr;
8792 : 145 : return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8793 : 145 : c_cast_p, complain);
8794 : : }
8795 : : }
8796 : :
8797 : : /* [expr.static.cast]
8798 : :
8799 : : An rvalue of type "pointer to cv void" can be explicitly
8800 : : converted to a pointer to object type. A value of type pointer
8801 : : to object converted to "pointer to cv void" and back to the
8802 : : original pointer type will have its original value. */
8803 : 2433660 : if (TYPE_PTR_P (intype)
8804 : 802363 : && VOID_TYPE_P (TREE_TYPE (intype))
8805 : 3103183 : && TYPE_PTROB_P (type))
8806 : : {
8807 : 637929 : if (!c_cast_p
8808 : 637929 : && check_for_casting_away_constness (loc, intype, type,
8809 : : STATIC_CAST_EXPR,
8810 : : complain))
8811 : 21 : return error_mark_node;
8812 : 637908 : if (processing_template_decl)
8813 : : return expr;
8814 : 549082 : return build_nop (type, expr);
8815 : : }
8816 : :
8817 : 1795731 : *valid_p = false;
8818 : 1795731 : return error_mark_node;
8819 : : }
8820 : :
8821 : : /* Return an expression representing static_cast<TYPE>(EXPR). */
8822 : :
8823 : : tree
8824 : 12986419 : build_static_cast (location_t loc, tree type, tree oexpr,
8825 : : tsubst_flags_t complain)
8826 : : {
8827 : 12986419 : tree expr = oexpr;
8828 : 12986419 : tree result;
8829 : 12986419 : bool valid_p;
8830 : :
8831 : 12986419 : if (type == error_mark_node || expr == error_mark_node)
8832 : : return error_mark_node;
8833 : :
8834 : 12986365 : bool dependent = (dependent_type_p (type)
8835 : 12986365 : || type_dependent_expression_p (expr));
8836 : 8927755 : if (dependent)
8837 : : {
8838 : 5110347 : tmpl:
8839 : 5110347 : expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8840 : : /* We don't know if it will or will not have side effects. */
8841 : 5110347 : TREE_SIDE_EFFECTS (expr) = 1;
8842 : 5110347 : result = convert_from_reference (expr);
8843 : 5110347 : protected_set_expr_location (result, loc);
8844 : 5110347 : return result;
8845 : : }
8846 : :
8847 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8848 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8849 : 8927755 : if (!TYPE_REF_P (type)
8850 : 5236939 : && TREE_CODE (expr) == NOP_EXPR
8851 : 9028585 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8852 : 48 : expr = TREE_OPERAND (expr, 0);
8853 : :
8854 : 8927755 : result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8855 : : &valid_p, complain);
8856 : 8927755 : if (valid_p)
8857 : : {
8858 : 8927559 : if (result != error_mark_node)
8859 : : {
8860 : 8927454 : maybe_warn_about_useless_cast (loc, type, expr, complain);
8861 : 8927454 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8862 : : }
8863 : 8927559 : if (processing_template_decl)
8864 : 1051737 : goto tmpl;
8865 : 7875822 : protected_set_expr_location (result, loc);
8866 : 7875822 : return result;
8867 : : }
8868 : :
8869 : 196 : if (complain & tf_error)
8870 : : {
8871 : 163 : auto_diagnostic_group d;
8872 : 163 : error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8873 : 163 : TREE_TYPE (expr), type);
8874 : 163 : if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8875 : 121 : && CLASS_TYPE_P (TREE_TYPE (type))
8876 : 192 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8877 : 17 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8878 : 17 : "class type %qT is incomplete", TREE_TYPE (type));
8879 : 163 : tree expr_type = TREE_TYPE (expr);
8880 : 163 : if (TYPE_PTR_P (expr_type))
8881 : 36 : expr_type = TREE_TYPE (expr_type);
8882 : 163 : if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8883 : 12 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8884 : : "class type %qT is incomplete", expr_type);
8885 : 163 : }
8886 : 196 : return error_mark_node;
8887 : : }
8888 : :
8889 : : /* EXPR is an expression with member function or pointer-to-member
8890 : : function type. TYPE is a pointer type. Converting EXPR to TYPE is
8891 : : not permitted by ISO C++, but we accept it in some modes. If we
8892 : : are not in one of those modes, issue a diagnostic. Return the
8893 : : converted expression. */
8894 : :
8895 : : tree
8896 : 197 : convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8897 : : {
8898 : 197 : tree intype;
8899 : 197 : tree decl;
8900 : :
8901 : 197 : intype = TREE_TYPE (expr);
8902 : 197 : gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8903 : : || TREE_CODE (intype) == METHOD_TYPE);
8904 : :
8905 : 197 : if (!(complain & tf_warning_or_error))
8906 : 0 : return error_mark_node;
8907 : :
8908 : 197 : location_t loc = cp_expr_loc_or_input_loc (expr);
8909 : :
8910 : 197 : if (pedantic || warn_pmf2ptr)
8911 : 271 : pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8912 : : "converting from %qH to %qI", intype, type);
8913 : :
8914 : 197 : STRIP_ANY_LOCATION_WRAPPER (expr);
8915 : :
8916 : 197 : if (TREE_CODE (intype) == METHOD_TYPE)
8917 : 88 : expr = build_addr_func (expr, complain);
8918 : 109 : else if (TREE_CODE (expr) == PTRMEM_CST)
8919 : 91 : expr = build_address (PTRMEM_CST_MEMBER (expr));
8920 : : else
8921 : : {
8922 : 18 : decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8923 : 18 : decl = build_address (decl);
8924 : 18 : expr = get_member_function_from_ptrfunc (&decl, expr, complain);
8925 : : }
8926 : :
8927 : 197 : if (expr == error_mark_node)
8928 : : return error_mark_node;
8929 : :
8930 : 197 : expr = build_nop (type, expr);
8931 : 197 : SET_EXPR_LOCATION (expr, loc);
8932 : 197 : return expr;
8933 : : }
8934 : :
8935 : : /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8936 : : constexpr evaluation knows to reject it. */
8937 : :
8938 : : static tree
8939 : 98622 : build_nop_reinterpret (tree type, tree expr)
8940 : : {
8941 : 98622 : tree ret = build_nop (type, expr);
8942 : 98622 : if (ret != expr)
8943 : 98622 : REINTERPRET_CAST_P (ret) = true;
8944 : 98622 : return ret;
8945 : : }
8946 : :
8947 : : /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8948 : : If C_CAST_P is true, this reinterpret cast is being done as part of
8949 : : a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8950 : : indicate whether or not reinterpret_cast was valid. */
8951 : :
8952 : : static tree
8953 : 1918173 : build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8954 : : bool c_cast_p, bool *valid_p,
8955 : : tsubst_flags_t complain)
8956 : : {
8957 : 1918173 : tree intype;
8958 : :
8959 : : /* Assume the cast is invalid. */
8960 : 1918173 : if (valid_p)
8961 : 1795591 : *valid_p = true;
8962 : :
8963 : 1918173 : if (type == error_mark_node || error_operand_p (expr))
8964 : : return error_mark_node;
8965 : :
8966 : 1918173 : intype = TREE_TYPE (expr);
8967 : :
8968 : : /* Save casted types in the function's used types hash table. */
8969 : 1918173 : used_types_insert (type);
8970 : :
8971 : : /* A prvalue of non-class type is cv-unqualified. */
8972 : 1918173 : if (!CLASS_TYPE_P (type))
8973 : 1918164 : type = cv_unqualified (type);
8974 : :
8975 : : /* [expr.reinterpret.cast]
8976 : : A glvalue of type T1, designating an object x, can be cast to the type
8977 : : "reference to T2" if an expression of type "pointer to T1" can be
8978 : : explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8979 : : The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8980 : : of type "pointer to T1". No temporary is created, no copy is made, and no
8981 : : constructors (11.4.4) or conversion functions (11.4.7) are called. */
8982 : 1918173 : if (TYPE_REF_P (type))
8983 : : {
8984 : 10193 : if (!glvalue_p (expr))
8985 : : {
8986 : 9 : if (complain & tf_error)
8987 : 9 : error_at (loc, "invalid cast of a prvalue expression of type "
8988 : : "%qT to type %qT",
8989 : : intype, type);
8990 : 9 : return error_mark_node;
8991 : : }
8992 : :
8993 : : /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8994 : : "B" are related class types; the reinterpret_cast does not
8995 : : adjust the pointer. */
8996 : 10184 : if (TYPE_PTR_P (intype)
8997 : 3 : && (complain & tf_warning)
8998 : 10187 : && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8999 : : COMPARE_BASE | COMPARE_DERIVED)))
9000 : 3 : warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
9001 : : intype, type);
9002 : :
9003 : 10184 : expr = cp_build_addr_expr (expr, complain);
9004 : :
9005 : 10184 : if (warn_strict_aliasing > 2)
9006 : 71 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9007 : :
9008 : 10184 : if (expr != error_mark_node)
9009 : 10184 : expr = build_reinterpret_cast_1
9010 : 10184 : (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
9011 : : valid_p, complain);
9012 : 10184 : if (expr != error_mark_node)
9013 : : /* cp_build_indirect_ref isn't right for rvalue refs. */
9014 : 10181 : expr = convert_from_reference (fold_convert (type, expr));
9015 : 10184 : return expr;
9016 : : }
9017 : :
9018 : : /* As a G++ extension, we consider conversions from member
9019 : : functions, and pointers to member functions to
9020 : : pointer-to-function and pointer-to-void types. If
9021 : : -Wno-pmf-conversions has not been specified,
9022 : : convert_member_func_to_ptr will issue an error message. */
9023 : 265 : if ((TYPE_PTRMEMFUNC_P (intype)
9024 : 1907792 : || TREE_CODE (intype) == METHOD_TYPE)
9025 : 276 : && TYPE_PTR_P (type)
9026 : 1908177 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
9027 : 155 : || VOID_TYPE_P (TREE_TYPE (type))))
9028 : 188 : return convert_member_func_to_ptr (type, expr, complain);
9029 : :
9030 : : /* If the cast is not to a reference type, the lvalue-to-rvalue,
9031 : : array-to-pointer, and function-to-pointer conversions are
9032 : : performed. */
9033 : 1907792 : expr = decay_conversion (expr, complain);
9034 : :
9035 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9036 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9037 : 1907792 : if (TREE_CODE (expr) == NOP_EXPR
9038 : 1907792 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9039 : 388 : expr = TREE_OPERAND (expr, 0);
9040 : :
9041 : 1907792 : if (error_operand_p (expr))
9042 : 30 : return error_mark_node;
9043 : :
9044 : 1907762 : intype = TREE_TYPE (expr);
9045 : :
9046 : : /* [expr.reinterpret.cast]
9047 : : A pointer can be converted to any integral type large enough to
9048 : : hold it. ... A value of type std::nullptr_t can be converted to
9049 : : an integral type; the conversion has the same meaning and
9050 : : validity as a conversion of (void*)0 to the integral type. */
9051 : 1907762 : if (CP_INTEGRAL_TYPE_P (type)
9052 : 147234 : && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
9053 : : {
9054 : 145322 : if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
9055 : : {
9056 : 21 : if (complain & tf_error)
9057 : 15 : permerror (loc, "cast from %qH to %qI loses precision",
9058 : : intype, type);
9059 : : else
9060 : 6 : return error_mark_node;
9061 : : }
9062 : 145316 : if (NULLPTR_TYPE_P (intype))
9063 : 98 : return build_int_cst (type, 0);
9064 : : }
9065 : : /* [expr.reinterpret.cast]
9066 : : A value of integral or enumeration type can be explicitly
9067 : : converted to a pointer. */
9068 : 1762440 : else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
9069 : : /* OK */
9070 : : ;
9071 : 1726076 : else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
9072 : 1724149 : || TYPE_PTR_OR_PTRMEM_P (type))
9073 : 1825218 : && same_type_p (type, intype))
9074 : : /* DR 799 */
9075 : 488 : return rvalue (expr);
9076 : 1725588 : else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
9077 : : {
9078 : 6869 : if ((complain & tf_warning)
9079 : 13738 : && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
9080 : 6869 : TREE_TYPE (intype)))
9081 : 145 : warning_at (loc, OPT_Wcast_function_type,
9082 : : "cast between incompatible function types"
9083 : : " from %qH to %qI", intype, type);
9084 : 6869 : return build_nop_reinterpret (type, expr);
9085 : : }
9086 : 1718719 : else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
9087 : : {
9088 : 79 : if ((complain & tf_warning)
9089 : 158 : && !cxx_safe_function_type_cast_p
9090 : 79 : (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
9091 : 79 : TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
9092 : 52 : warning_at (loc, OPT_Wcast_function_type,
9093 : : "cast between incompatible pointer to member types"
9094 : : " from %qH to %qI", intype, type);
9095 : 79 : return build_nop_reinterpret (type, expr);
9096 : : }
9097 : 21 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
9098 : 1718640 : || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
9099 : : {
9100 : 91419 : if (!c_cast_p
9101 : 91419 : && check_for_casting_away_constness (loc, intype, type,
9102 : : REINTERPRET_CAST_EXPR,
9103 : : complain))
9104 : 21 : return error_mark_node;
9105 : : /* Warn about possible alignment problems. */
9106 : 91398 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9107 : 24 : && (complain & tf_warning)
9108 : 24 : && !VOID_TYPE_P (type)
9109 : 24 : && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
9110 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (type))
9111 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (intype))
9112 : 91446 : && min_align_of_type (TREE_TYPE (type))
9113 : 24 : > min_align_of_type (TREE_TYPE (intype)))
9114 : 18 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9115 : : "increases required alignment of target type",
9116 : : intype, type);
9117 : :
9118 : 91398 : if (warn_strict_aliasing <= 2)
9119 : : /* strict_aliasing_warning STRIP_NOPs its expr. */
9120 : 79845 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9121 : :
9122 : 91398 : return build_nop_reinterpret (type, expr);
9123 : : }
9124 : 297 : else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
9125 : 1627381 : || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
9126 : : {
9127 : 276 : if (complain & tf_warning)
9128 : : /* C++11 5.2.10 p8 says that "Converting a function pointer to an
9129 : : object pointer type or vice versa is conditionally-supported." */
9130 : 276 : warning_at (loc, OPT_Wconditionally_supported,
9131 : : "casting between pointer-to-function and "
9132 : : "pointer-to-object is conditionally-supported");
9133 : 276 : return build_nop_reinterpret (type, expr);
9134 : : }
9135 : 1626945 : else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
9136 : 1624983 : return convert_to_vector (type, rvalue (expr));
9137 : 1962 : else if (gnu_vector_type_p (intype)
9138 : 1962 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9139 : 1861 : return convert_to_integer_nofold (type, expr);
9140 : : else
9141 : : {
9142 : 101 : if (valid_p)
9143 : 83 : *valid_p = false;
9144 : 101 : if (complain & tf_error)
9145 : 92 : error_at (loc, "invalid cast from type %qT to type %qT",
9146 : : intype, type);
9147 : 101 : return error_mark_node;
9148 : : }
9149 : :
9150 : 181582 : expr = cp_convert (type, expr, complain);
9151 : 181582 : if (TREE_CODE (expr) == NOP_EXPR)
9152 : : /* Mark any nop_expr that created as a reintepret_cast. */
9153 : 0 : REINTERPRET_CAST_P (expr) = true;
9154 : : return expr;
9155 : : }
9156 : :
9157 : : tree
9158 : 592835 : build_reinterpret_cast (location_t loc, tree type, tree expr,
9159 : : tsubst_flags_t complain)
9160 : : {
9161 : 592835 : tree r;
9162 : :
9163 : 592835 : if (type == error_mark_node || expr == error_mark_node)
9164 : : return error_mark_node;
9165 : :
9166 : 592828 : if (processing_template_decl)
9167 : : {
9168 : 480374 : tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
9169 : :
9170 : 480374 : if (!TREE_SIDE_EFFECTS (t)
9171 : 480374 : && type_dependent_expression_p (expr))
9172 : : /* There might turn out to be side effects inside expr. */
9173 : 202216 : TREE_SIDE_EFFECTS (t) = 1;
9174 : 480374 : r = convert_from_reference (t);
9175 : 480374 : protected_set_expr_location (r, loc);
9176 : 480374 : return r;
9177 : : }
9178 : :
9179 : 112454 : r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9180 : : /*valid_p=*/NULL, complain);
9181 : 112454 : if (r != error_mark_node)
9182 : : {
9183 : 112391 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9184 : 112391 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9185 : : }
9186 : 112454 : protected_set_expr_location (r, loc);
9187 : 112454 : return r;
9188 : : }
9189 : :
9190 : : /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9191 : : return an appropriate expression. Otherwise, return
9192 : : error_mark_node. If the cast is not valid, and COMPLAIN is true,
9193 : : then a diagnostic will be issued. If VALID_P is non-NULL, we are
9194 : : performing a C-style cast, its value upon return will indicate
9195 : : whether or not the conversion succeeded. */
9196 : :
9197 : : static tree
9198 : 37087520 : build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9199 : : tsubst_flags_t complain, bool *valid_p)
9200 : : {
9201 : 37087520 : tree src_type;
9202 : 37087520 : tree reference_type;
9203 : :
9204 : : /* Callers are responsible for handling error_mark_node as a
9205 : : destination type. */
9206 : 37087520 : gcc_assert (dst_type != error_mark_node);
9207 : : /* In a template, callers should be building syntactic
9208 : : representations of casts, not using this machinery. */
9209 : 37087520 : gcc_assert (!processing_template_decl);
9210 : :
9211 : : /* Assume the conversion is invalid. */
9212 : 37087520 : if (valid_p)
9213 : 36954917 : *valid_p = false;
9214 : :
9215 : 37087520 : if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9216 : : {
9217 : 36429926 : if (complain & tf_error)
9218 : 9 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9219 : : "which is not a pointer, reference, "
9220 : : "nor a pointer-to-data-member type", dst_type);
9221 : 36429926 : return error_mark_node;
9222 : : }
9223 : :
9224 : 657594 : if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
9225 : : {
9226 : 3949 : if (complain & tf_error)
9227 : 6 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9228 : : "which is a pointer or reference to a function type",
9229 : : dst_type);
9230 : 3949 : return error_mark_node;
9231 : : }
9232 : :
9233 : : /* A prvalue of non-class type is cv-unqualified. */
9234 : 653645 : dst_type = cv_unqualified (dst_type);
9235 : :
9236 : : /* Save casted types in the function's used types hash table. */
9237 : 653645 : used_types_insert (dst_type);
9238 : :
9239 : 653645 : src_type = TREE_TYPE (expr);
9240 : : /* Expressions do not really have reference types. */
9241 : 653645 : if (TYPE_REF_P (src_type))
9242 : 0 : src_type = TREE_TYPE (src_type);
9243 : :
9244 : : /* [expr.const.cast]
9245 : :
9246 : : For two object types T1 and T2, if a pointer to T1 can be explicitly
9247 : : converted to the type "pointer to T2" using a const_cast, then the
9248 : : following conversions can also be made:
9249 : :
9250 : : -- an lvalue of type T1 can be explicitly converted to an lvalue of
9251 : : type T2 using the cast const_cast<T2&>;
9252 : :
9253 : : -- a glvalue of type T1 can be explicitly converted to an xvalue of
9254 : : type T2 using the cast const_cast<T2&&>; and
9255 : :
9256 : : -- if T1 is a class type, a prvalue of type T1 can be explicitly
9257 : : converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9258 : :
9259 : 653645 : if (TYPE_REF_P (dst_type))
9260 : : {
9261 : 63367 : reference_type = dst_type;
9262 : 63367 : if (!TYPE_REF_IS_RVALUE (dst_type)
9263 : 63367 : ? lvalue_p (expr)
9264 : 1140 : : obvalue_p (expr))
9265 : : /* OK. */;
9266 : : else
9267 : : {
9268 : 35 : if (complain & tf_error)
9269 : 9 : error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
9270 : : "to type %qT",
9271 : : src_type, dst_type);
9272 : 35 : return error_mark_node;
9273 : : }
9274 : 63332 : dst_type = build_pointer_type (TREE_TYPE (dst_type));
9275 : 63332 : src_type = build_pointer_type (src_type);
9276 : : }
9277 : : else
9278 : : {
9279 : 590278 : reference_type = NULL_TREE;
9280 : : /* If the destination type is not a reference type, the
9281 : : lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9282 : : conversions are performed. */
9283 : 590278 : src_type = type_decays_to (src_type);
9284 : 590278 : if (src_type == error_mark_node)
9285 : : return error_mark_node;
9286 : : }
9287 : :
9288 : 653610 : if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9289 : : {
9290 : 501881 : if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9291 : : {
9292 : 254339 : if (valid_p)
9293 : : {
9294 : 121814 : *valid_p = true;
9295 : : /* This cast is actually a C-style cast. Issue a warning if
9296 : : the user is making a potentially unsafe cast. */
9297 : 121814 : check_for_casting_away_constness (loc, src_type, dst_type,
9298 : : CAST_EXPR, complain);
9299 : : /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9300 : 121814 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9301 : 3 : && (complain & tf_warning)
9302 : 121820 : && min_align_of_type (TREE_TYPE (dst_type))
9303 : 3 : > min_align_of_type (TREE_TYPE (src_type)))
9304 : 3 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9305 : : "increases required alignment of target type",
9306 : : src_type, dst_type);
9307 : : }
9308 : 254339 : if (reference_type)
9309 : : {
9310 : 62141 : expr = cp_build_addr_expr (expr, complain);
9311 : 62141 : if (expr == error_mark_node)
9312 : : return error_mark_node;
9313 : 62123 : expr = build_nop (reference_type, expr);
9314 : 62123 : return convert_from_reference (expr);
9315 : : }
9316 : : else
9317 : : {
9318 : 192198 : expr = decay_conversion (expr, complain);
9319 : 192198 : if (expr == error_mark_node)
9320 : : return error_mark_node;
9321 : :
9322 : : /* build_c_cast puts on a NOP_EXPR to make the result not an
9323 : : lvalue. Strip such NOP_EXPRs if VALUE is being used in
9324 : : non-lvalue context. */
9325 : 192198 : if (TREE_CODE (expr) == NOP_EXPR
9326 : 192198 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9327 : 3 : expr = TREE_OPERAND (expr, 0);
9328 : 192198 : return build_nop (dst_type, expr);
9329 : : }
9330 : : }
9331 : 247542 : else if (valid_p
9332 : 495036 : && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9333 : 247494 : TREE_TYPE (src_type)))
9334 : 15041 : check_for_casting_away_constness (loc, src_type, dst_type,
9335 : : CAST_EXPR, complain);
9336 : : }
9337 : :
9338 : 399271 : if (complain & tf_error)
9339 : 30 : error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9340 : : src_type, dst_type);
9341 : 399271 : return error_mark_node;
9342 : : }
9343 : :
9344 : : tree
9345 : 567361 : build_const_cast (location_t loc, tree type, tree expr,
9346 : : tsubst_flags_t complain)
9347 : : {
9348 : 567361 : tree r;
9349 : :
9350 : 567361 : if (type == error_mark_node || error_operand_p (expr))
9351 : : return error_mark_node;
9352 : :
9353 : 567353 : if (processing_template_decl)
9354 : : {
9355 : 434750 : tree t = build_min (CONST_CAST_EXPR, type, expr);
9356 : :
9357 : 434750 : if (!TREE_SIDE_EFFECTS (t)
9358 : 434750 : && type_dependent_expression_p (expr))
9359 : : /* There might turn out to be side effects inside expr. */
9360 : 331160 : TREE_SIDE_EFFECTS (t) = 1;
9361 : 434750 : r = convert_from_reference (t);
9362 : 434750 : protected_set_expr_location (r, loc);
9363 : 434750 : return r;
9364 : : }
9365 : :
9366 : 132603 : r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9367 : 132603 : if (r != error_mark_node)
9368 : : {
9369 : 132525 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9370 : 132525 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9371 : : }
9372 : 132603 : protected_set_expr_location (r, loc);
9373 : 132603 : return r;
9374 : : }
9375 : :
9376 : : /* Like cp_build_c_cast, but for the c-common bits. */
9377 : :
9378 : : tree
9379 : 0 : build_c_cast (location_t loc, tree type, tree expr)
9380 : : {
9381 : 0 : return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9382 : : }
9383 : :
9384 : : /* Like the "build_c_cast" used for c-common, but using cp_expr to
9385 : : preserve location information even for tree nodes that don't
9386 : : support it. */
9387 : :
9388 : : cp_expr
9389 : 9320357 : build_c_cast (location_t loc, tree type, cp_expr expr)
9390 : : {
9391 : 9320357 : cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9392 : 9320357 : result.set_location (loc);
9393 : 9320357 : return result;
9394 : : }
9395 : :
9396 : : /* Build an expression representing an explicit C-style cast to type
9397 : : TYPE of expression EXPR. */
9398 : :
9399 : : tree
9400 : 38875547 : cp_build_c_cast (location_t loc, tree type, tree expr,
9401 : : tsubst_flags_t complain)
9402 : : {
9403 : 38875547 : tree value = expr;
9404 : 38875547 : tree result;
9405 : 38875547 : bool valid_p;
9406 : :
9407 : 38875547 : if (type == error_mark_node || error_operand_p (expr))
9408 : : return error_mark_node;
9409 : :
9410 : 38875374 : if (processing_template_decl)
9411 : : {
9412 : 1920448 : tree t = build_min (CAST_EXPR, type,
9413 : : tree_cons (NULL_TREE, value, NULL_TREE));
9414 : : /* We don't know if it will or will not have side effects. */
9415 : 1920448 : TREE_SIDE_EFFECTS (t) = 1;
9416 : 1920448 : return convert_from_reference (t);
9417 : : }
9418 : :
9419 : : /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9420 : : 'Class') should always be retained, because this information aids
9421 : : in method lookup. */
9422 : 36954926 : if (objc_is_object_ptr (type)
9423 : 36954926 : && objc_is_object_ptr (TREE_TYPE (expr)))
9424 : 0 : return build_nop (type, expr);
9425 : :
9426 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9427 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9428 : 36954926 : if (!TYPE_REF_P (type)
9429 : 36952968 : && TREE_CODE (value) == NOP_EXPR
9430 : 37194329 : && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9431 : 110122 : value = TREE_OPERAND (value, 0);
9432 : :
9433 : 36954926 : if (TREE_CODE (type) == ARRAY_TYPE)
9434 : : {
9435 : : /* Allow casting from T1* to T2[] because Cfront allows it.
9436 : : NIHCL uses it. It is not valid ISO C++ however. */
9437 : 3 : if (TYPE_PTR_P (TREE_TYPE (expr)))
9438 : : {
9439 : 0 : if (complain & tf_error)
9440 : 0 : permerror (loc, "ISO C++ forbids casting to an array type %qT",
9441 : : type);
9442 : : else
9443 : 0 : return error_mark_node;
9444 : 0 : type = build_pointer_type (TREE_TYPE (type));
9445 : : }
9446 : : else
9447 : : {
9448 : 3 : if (complain & tf_error)
9449 : 3 : error_at (loc, "ISO C++ forbids casting to an array type %qT",
9450 : : type);
9451 : 3 : return error_mark_node;
9452 : : }
9453 : : }
9454 : :
9455 : 36954923 : if (FUNC_OR_METHOD_TYPE_P (type))
9456 : : {
9457 : 15 : if (complain & tf_error)
9458 : 15 : error_at (loc, "invalid cast to function type %qT", type);
9459 : 15 : return error_mark_node;
9460 : : }
9461 : :
9462 : 36954908 : if (TYPE_PTR_P (type)
9463 : 522896 : && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9464 : : /* Casting to an integer of smaller size is an error detected elsewhere. */
9465 : 150109 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9466 : : /* Don't warn about converting any constant. */
9467 : 37048794 : && !TREE_CONSTANT (value))
9468 : 47 : warning_at (loc, OPT_Wint_to_pointer_cast,
9469 : : "cast to pointer from integer of different size");
9470 : :
9471 : : /* A C-style cast can be a const_cast. */
9472 : 36954908 : result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9473 : : &valid_p);
9474 : 36954908 : if (valid_p)
9475 : : {
9476 : 121805 : if (result != error_mark_node)
9477 : : {
9478 : 121796 : maybe_warn_about_useless_cast (loc, type, value, complain);
9479 : 121796 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9480 : : }
9481 : 9 : else if (complain & tf_error)
9482 : 9 : build_const_cast_1 (loc, type, value, tf_error, &valid_p);
9483 : 121805 : return result;
9484 : : }
9485 : :
9486 : : /* Or a static cast. */
9487 : 36833103 : result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9488 : : &valid_p, complain);
9489 : : /* Or a reinterpret_cast. */
9490 : 36833103 : if (!valid_p)
9491 : 1795535 : result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9492 : : &valid_p, complain);
9493 : : /* The static_cast or reinterpret_cast may be followed by a
9494 : : const_cast. */
9495 : 36833103 : if (valid_p
9496 : : /* A valid cast may result in errors if, for example, a
9497 : : conversion to an ambiguous base class is required. */
9498 : 36833103 : && !error_operand_p (result))
9499 : : {
9500 : 36832698 : tree result_type;
9501 : :
9502 : 36832698 : maybe_warn_about_useless_cast (loc, type, value, complain);
9503 : 36832698 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9504 : :
9505 : : /* Non-class rvalues always have cv-unqualified type. */
9506 : 36832698 : if (!CLASS_TYPE_P (type))
9507 : 35364694 : type = TYPE_MAIN_VARIANT (type);
9508 : 36832698 : result_type = TREE_TYPE (result);
9509 : 36832698 : if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9510 : 35363481 : result_type = TYPE_MAIN_VARIANT (result_type);
9511 : : /* If the type of RESULT does not match TYPE, perform a
9512 : : const_cast to make it match. If the static_cast or
9513 : : reinterpret_cast succeeded, we will differ by at most
9514 : : cv-qualification, so the follow-on const_cast is guaranteed
9515 : : to succeed. */
9516 : 36832698 : if (!same_type_p (non_reference (type), non_reference (result_type)))
9517 : : {
9518 : 0 : result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
9519 : 0 : gcc_assert (valid_p);
9520 : : }
9521 : 36832698 : return result;
9522 : : }
9523 : :
9524 : 405 : return error_mark_node;
9525 : : }
9526 : :
9527 : : /* Warn when a value is moved to itself with std::move. LHS is the target,
9528 : : RHS may be the std::move call, and LOC is the location of the whole
9529 : : assignment. Return true if we warned. */
9530 : :
9531 : : bool
9532 : 25154640 : maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9533 : : {
9534 : 25154640 : if (!warn_self_move)
9535 : : return false;
9536 : :
9537 : : /* C++98 doesn't know move. */
9538 : 348420 : if (cxx_dialect < cxx11)
9539 : : return false;
9540 : :
9541 : 328597 : if (processing_template_decl)
9542 : : return false;
9543 : :
9544 : 23225 : if (!REFERENCE_REF_P (rhs)
9545 : 284055 : || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9546 : : return false;
9547 : 4616 : tree fn = TREE_OPERAND (rhs, 0);
9548 : 4616 : if (!is_std_move_p (fn))
9549 : : return false;
9550 : :
9551 : : /* Just a little helper to strip * and various NOPs. */
9552 : 6417 : auto extract_op = [] (tree &op) {
9553 : 4278 : STRIP_NOPS (op);
9554 : 5475 : while (INDIRECT_REF_P (op))
9555 : 1197 : op = TREE_OPERAND (op, 0);
9556 : 4278 : op = maybe_undo_parenthesized_ref (op);
9557 : 4278 : STRIP_ANY_LOCATION_WRAPPER (op);
9558 : 4278 : };
9559 : :
9560 : 2139 : tree arg = CALL_EXPR_ARG (fn, 0);
9561 : 2139 : extract_op (arg);
9562 : 2139 : if (TREE_CODE (arg) == ADDR_EXPR)
9563 : 1478 : arg = TREE_OPERAND (arg, 0);
9564 : 2139 : tree type = TREE_TYPE (lhs);
9565 : 2139 : tree orig_lhs = lhs;
9566 : 2139 : extract_op (lhs);
9567 : 2139 : if (cp_tree_equal (lhs, arg)
9568 : : /* Also warn in a member-initializer-list, as in : i(std::move(i)). */
9569 : 2139 : || (TREE_CODE (lhs) == FIELD_DECL
9570 : 634 : && TREE_CODE (arg) == COMPONENT_REF
9571 : 294 : && cp_tree_equal (TREE_OPERAND (arg, 0), current_class_ref)
9572 : 6 : && TREE_OPERAND (arg, 1) == lhs))
9573 : 117 : if (warning_at (loc, OPT_Wself_move,
9574 : : "moving %qE of type %qT to itself", orig_lhs, type))
9575 : : return true;
9576 : :
9577 : : return false;
9578 : : }
9579 : :
9580 : : /* For use from the C common bits. */
9581 : : tree
9582 : 4766 : build_modify_expr (location_t location,
9583 : : tree lhs, tree /*lhs_origtype*/,
9584 : : enum tree_code modifycode,
9585 : : location_t /*rhs_location*/, tree rhs,
9586 : : tree /*rhs_origtype*/)
9587 : : {
9588 : 4766 : return cp_build_modify_expr (location, lhs, modifycode, rhs,
9589 : 4766 : tf_warning_or_error);
9590 : : }
9591 : :
9592 : : /* Build an assignment expression of lvalue LHS from value RHS.
9593 : : MODIFYCODE is the code for a binary operator that we use
9594 : : to combine the old value of LHS with RHS to get the new value.
9595 : : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9596 : :
9597 : : C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9598 : :
9599 : : tree
9600 : 32621369 : cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9601 : : tree rhs, tsubst_flags_t complain)
9602 : : {
9603 : 32621369 : lhs = mark_lvalue_use_nonread (lhs);
9604 : :
9605 : 32621369 : tree result = NULL_TREE;
9606 : 32621369 : tree newrhs = rhs;
9607 : 32621369 : tree lhstype = TREE_TYPE (lhs);
9608 : 32621369 : tree olhs = lhs;
9609 : 32621369 : tree olhstype = lhstype;
9610 : 32621369 : bool plain_assign = (modifycode == NOP_EXPR);
9611 : 32621369 : bool compound_side_effects_p = false;
9612 : 32621369 : tree preeval = NULL_TREE;
9613 : :
9614 : : /* Avoid duplicate error messages from operands that had errors. */
9615 : 32621369 : if (error_operand_p (lhs) || error_operand_p (rhs))
9616 : 20 : return error_mark_node;
9617 : :
9618 : 32621431 : while (TREE_CODE (lhs) == COMPOUND_EXPR)
9619 : : {
9620 : 82 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9621 : 70 : compound_side_effects_p = true;
9622 : 82 : lhs = TREE_OPERAND (lhs, 1);
9623 : : }
9624 : :
9625 : : /* Handle control structure constructs used as "lvalues". Note that we
9626 : : leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9627 : 32621349 : switch (TREE_CODE (lhs))
9628 : : {
9629 : : /* Handle --foo = 5; as these are valid constructs in C++. */
9630 : 21 : case PREDECREMENT_EXPR:
9631 : 21 : case PREINCREMENT_EXPR:
9632 : 21 : if (compound_side_effects_p)
9633 : 9 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9634 : 21 : lhs = genericize_compound_lvalue (lhs);
9635 : 149 : maybe_add_compound:
9636 : : /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9637 : : and looked through the COMPOUND_EXPRs, readd them now around
9638 : : the resulting lhs. */
9639 : 149 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9640 : : {
9641 : 9 : lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9642 : 9 : tree *ptr = &TREE_OPERAND (lhs, 1);
9643 : 9 : for (olhs = TREE_OPERAND (olhs, 1);
9644 : 12 : TREE_CODE (olhs) == COMPOUND_EXPR;
9645 : 3 : olhs = TREE_OPERAND (olhs, 1))
9646 : : {
9647 : 3 : *ptr = build2 (COMPOUND_EXPR, lhstype,
9648 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9649 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9650 : : }
9651 : : }
9652 : : break;
9653 : :
9654 : 128 : case MODIFY_EXPR:
9655 : 128 : if (compound_side_effects_p)
9656 : 0 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9657 : 128 : lhs = genericize_compound_lvalue (lhs);
9658 : 128 : goto maybe_add_compound;
9659 : :
9660 : 0 : case MIN_EXPR:
9661 : 0 : case MAX_EXPR:
9662 : : /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9663 : : when neither operand has side-effects. */
9664 : 0 : if (!lvalue_or_else (lhs, lv_assign, complain))
9665 : 0 : return error_mark_node;
9666 : :
9667 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9668 : : && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9669 : :
9670 : 0 : lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9671 : 0 : build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9672 : : boolean_type_node,
9673 : 0 : TREE_OPERAND (lhs, 0),
9674 : 0 : TREE_OPERAND (lhs, 1)),
9675 : 0 : TREE_OPERAND (lhs, 0),
9676 : 0 : TREE_OPERAND (lhs, 1));
9677 : 206 : gcc_fallthrough ();
9678 : :
9679 : : /* Handle (a ? b : c) used as an "lvalue". */
9680 : 206 : case COND_EXPR:
9681 : 206 : {
9682 : : /* Produce (a ? (b = rhs) : (c = rhs))
9683 : : except that the RHS goes through a save-expr
9684 : : so the code to compute it is only emitted once. */
9685 : 206 : if (VOID_TYPE_P (TREE_TYPE (rhs)))
9686 : : {
9687 : 18 : if (complain & tf_error)
9688 : 24 : error_at (cp_expr_loc_or_loc (rhs, loc),
9689 : : "void value not ignored as it ought to be");
9690 : 18 : return error_mark_node;
9691 : : }
9692 : :
9693 : 188 : rhs = stabilize_expr (rhs, &preeval);
9694 : :
9695 : : /* Check this here to avoid odd errors when trying to convert
9696 : : a throw to the type of the COND_EXPR. */
9697 : 188 : if (!lvalue_or_else (lhs, lv_assign, complain))
9698 : 6 : return error_mark_node;
9699 : :
9700 : 182 : tree op1 = TREE_OPERAND (lhs, 1);
9701 : 182 : if (TREE_CODE (op1) != THROW_EXPR)
9702 : 176 : op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
9703 : : /* When sanitizing undefined behavior, even when rhs doesn't need
9704 : : stabilization at this point, the sanitization might add extra
9705 : : SAVE_EXPRs in there and so make sure there is no tree sharing
9706 : : in the rhs, otherwise those SAVE_EXPRs will have initialization
9707 : : only in one of the two branches. */
9708 : 182 : if (sanitize_flags_p (SANITIZE_UNDEFINED
9709 : : | SANITIZE_UNDEFINED_NONDEFAULT))
9710 : 3 : rhs = unshare_expr (rhs);
9711 : 182 : tree op2 = TREE_OPERAND (lhs, 2);
9712 : 182 : if (TREE_CODE (op2) != THROW_EXPR)
9713 : 176 : op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9714 : 182 : tree cond = build_conditional_expr (input_location,
9715 : 182 : TREE_OPERAND (lhs, 0), op1, op2,
9716 : : complain);
9717 : :
9718 : 182 : if (cond == error_mark_node)
9719 : : return cond;
9720 : : /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9721 : : and looked through the COMPOUND_EXPRs, readd them now around
9722 : : the resulting cond before adding the preevaluated rhs. */
9723 : 179 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9724 : : {
9725 : 18 : cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9726 : 18 : TREE_OPERAND (olhs, 0), cond);
9727 : 18 : tree *ptr = &TREE_OPERAND (cond, 1);
9728 : 18 : for (olhs = TREE_OPERAND (olhs, 1);
9729 : 21 : TREE_CODE (olhs) == COMPOUND_EXPR;
9730 : 3 : olhs = TREE_OPERAND (olhs, 1))
9731 : : {
9732 : 3 : *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9733 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9734 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9735 : : }
9736 : : }
9737 : : /* Make sure the code to compute the rhs comes out
9738 : : before the split. */
9739 : 179 : result = cond;
9740 : 179 : goto ret;
9741 : : }
9742 : :
9743 : : default:
9744 : : lhs = olhs;
9745 : : break;
9746 : : }
9747 : :
9748 : 32621143 : if (modifycode == INIT_EXPR)
9749 : : {
9750 : 3349589 : if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9751 : : /* Do the default thing. */;
9752 : 3170827 : else if (TREE_CODE (rhs) == CONSTRUCTOR)
9753 : : {
9754 : : /* Compound literal. */
9755 : 169 : if (! same_type_p (TREE_TYPE (rhs), lhstype))
9756 : : /* Call convert to generate an error; see PR 11063. */
9757 : 0 : rhs = convert (lhstype, rhs);
9758 : 169 : result = cp_build_init_expr (lhs, rhs);
9759 : 169 : TREE_SIDE_EFFECTS (result) = 1;
9760 : 169 : goto ret;
9761 : : }
9762 : 3170658 : else if (! MAYBE_CLASS_TYPE_P (lhstype))
9763 : : /* Do the default thing. */;
9764 : : else
9765 : : {
9766 : 43258 : releasing_vec rhs_vec = make_tree_vector_single (rhs);
9767 : 43258 : result = build_special_member_call (lhs, complete_ctor_identifier,
9768 : : &rhs_vec, lhstype, LOOKUP_NORMAL,
9769 : : complain);
9770 : 43258 : if (result == NULL_TREE)
9771 : 0 : return error_mark_node;
9772 : 43258 : goto ret;
9773 : 43258 : }
9774 : : }
9775 : : else
9776 : : {
9777 : 29271554 : lhs = require_complete_type (lhs, complain);
9778 : 29271554 : if (lhs == error_mark_node)
9779 : : return error_mark_node;
9780 : :
9781 : 29271511 : if (modifycode == NOP_EXPR)
9782 : : {
9783 : 25107664 : maybe_warn_self_move (loc, lhs, rhs);
9784 : :
9785 : 25107664 : if (c_dialect_objc ())
9786 : : {
9787 : 0 : result = objc_maybe_build_modify_expr (lhs, rhs);
9788 : 0 : if (result)
9789 : 0 : goto ret;
9790 : : }
9791 : :
9792 : : /* `operator=' is not an inheritable operator. */
9793 : 25107664 : if (! MAYBE_CLASS_TYPE_P (lhstype))
9794 : : /* Do the default thing. */;
9795 : : else
9796 : : {
9797 : 2507087 : result = build_new_op (input_location, MODIFY_EXPR,
9798 : : LOOKUP_NORMAL, lhs, rhs,
9799 : : make_node (NOP_EXPR), NULL_TREE,
9800 : : /*overload=*/NULL, complain);
9801 : 2507087 : if (result == NULL_TREE)
9802 : 0 : return error_mark_node;
9803 : 2507087 : goto ret;
9804 : : }
9805 : : lhstype = olhstype;
9806 : : }
9807 : : else
9808 : : {
9809 : 4163847 : tree init = NULL_TREE;
9810 : :
9811 : : /* A binary op has been requested. Combine the old LHS
9812 : : value with the RHS producing the value we should actually
9813 : : store into the LHS. */
9814 : 4163847 : gcc_assert (!((TYPE_REF_P (lhstype)
9815 : : && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9816 : : || MAYBE_CLASS_TYPE_P (lhstype)));
9817 : :
9818 : : /* Preevaluate the RHS to make sure its evaluation is complete
9819 : : before the lvalue-to-rvalue conversion of the LHS:
9820 : :
9821 : : [expr.ass] With respect to an indeterminately-sequenced
9822 : : function call, the operation of a compound assignment is a
9823 : : single evaluation. [ Note: Therefore, a function call shall
9824 : : not intervene between the lvalue-to-rvalue conversion and the
9825 : : side effect associated with any single compound assignment
9826 : : operator. -- end note ] */
9827 : 4163847 : lhs = cp_stabilize_reference (lhs);
9828 : 4163847 : rhs = decay_conversion (rhs, complain);
9829 : 4163847 : if (rhs == error_mark_node)
9830 : 123 : return error_mark_node;
9831 : :
9832 : 4163844 : {
9833 : 4163844 : auto_diagnostic_group d;
9834 : 4163844 : rhs = stabilize_expr (rhs, &init);
9835 : 4163844 : bool clear_decl_read = false;
9836 : 4163844 : tree stripped_lhs = tree_strip_any_location_wrapper (lhs);
9837 : 1046240 : if ((VAR_P (stripped_lhs) || TREE_CODE (stripped_lhs) == PARM_DECL)
9838 : 3578677 : && !DECL_READ_P (stripped_lhs)
9839 : 955625 : && (VAR_P (stripped_lhs) ? warn_unused_but_set_variable
9840 : : : warn_unused_but_set_parameter) > 2
9841 : 6863 : && !CLASS_TYPE_P (TREE_TYPE (lhs))
9842 : 4170707 : && !CLASS_TYPE_P (TREE_TYPE (rhs)))
9843 : : {
9844 : 6863 : mark_exp_read (rhs);
9845 : 6863 : if (!DECL_READ_P (stripped_lhs))
9846 : 4163844 : clear_decl_read = true;
9847 : : }
9848 : 4163844 : newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9849 : 4163844 : if (clear_decl_read)
9850 : 6863 : DECL_READ_P (stripped_lhs) = 0;
9851 : 4163844 : if (newrhs == error_mark_node)
9852 : : {
9853 : 120 : if (complain & tf_error)
9854 : 24 : inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9855 : 24 : modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9856 : 120 : return error_mark_node;
9857 : : }
9858 : 4163844 : }
9859 : :
9860 : 4163724 : if (init)
9861 : 282419 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9862 : :
9863 : : /* Now it looks like a plain assignment. */
9864 : 4163724 : modifycode = NOP_EXPR;
9865 : 4163724 : if (c_dialect_objc ())
9866 : : {
9867 : 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
9868 : 0 : if (result)
9869 : 0 : goto ret;
9870 : : }
9871 : : }
9872 : 26764301 : gcc_assert (!TYPE_REF_P (lhstype));
9873 : 26764301 : gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9874 : : }
9875 : :
9876 : : /* The left-hand side must be an lvalue. */
9877 : 30070463 : if (!lvalue_or_else (lhs, lv_assign, complain))
9878 : 154 : return error_mark_node;
9879 : :
9880 : : /* Warn about modifying something that is `const'. Don't warn if
9881 : : this is initialization. */
9882 : 30070309 : if (modifycode != INIT_EXPR
9883 : 30070309 : && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9884 : : /* Functions are not modifiable, even though they are
9885 : : lvalues. */
9886 : 26711388 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9887 : : /* If it's an aggregate and any field is const, then it is
9888 : : effectively const. */
9889 : 26711358 : || (CLASS_TYPE_P (lhstype)
9890 : 0 : && C_TYPE_FIELDS_READONLY (lhstype))))
9891 : : {
9892 : 52789 : if (complain & tf_error)
9893 : 88 : cxx_readonly_error (loc, lhs, lv_assign);
9894 : 52789 : return error_mark_node;
9895 : : }
9896 : :
9897 : : /* If storing into a structure or union member, it may have been given a
9898 : : lowered bitfield type. We need to convert to the declared type first,
9899 : : so retrieve it now. */
9900 : :
9901 : 30017520 : olhstype = unlowered_expr_type (lhs);
9902 : :
9903 : : /* Convert new value to destination type. */
9904 : :
9905 : 30017520 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
9906 : : {
9907 : 186 : int from_array;
9908 : :
9909 : 186 : if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9910 : : {
9911 : 12 : if (modifycode != INIT_EXPR)
9912 : : {
9913 : 12 : if (complain & tf_error)
9914 : 12 : error_at (loc,
9915 : : "assigning to an array from an initializer list");
9916 : 12 : return error_mark_node;
9917 : : }
9918 : 0 : if (check_array_initializer (lhs, lhstype, newrhs))
9919 : 0 : return error_mark_node;
9920 : 0 : newrhs = digest_init (lhstype, newrhs, complain);
9921 : 0 : if (newrhs == error_mark_node)
9922 : : return error_mark_node;
9923 : : }
9924 : :
9925 : : /* C++11 8.5/17: "If the destination type is an array of characters,
9926 : : an array of char16_t, an array of char32_t, or an array of wchar_t,
9927 : : and the initializer is a string literal...". */
9928 : 174 : else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9929 : : == STRING_CST)
9930 : 43 : && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9931 : 217 : && modifycode == INIT_EXPR)
9932 : : {
9933 : 28 : newrhs = digest_init (lhstype, newrhs, complain);
9934 : 28 : if (newrhs == error_mark_node)
9935 : : return error_mark_node;
9936 : : }
9937 : :
9938 : 146 : else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9939 : : TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9940 : : {
9941 : 20 : if (complain & tf_error)
9942 : 15 : error_at (loc, "incompatible types in assignment of %qT to %qT",
9943 : 15 : TREE_TYPE (rhs), lhstype);
9944 : 20 : return error_mark_node;
9945 : : }
9946 : :
9947 : : /* Allow array assignment in compiler-generated code. */
9948 : 126 : else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
9949 : : /* OK, used by coroutines (co-await-initlist1.C). */;
9950 : 120 : else if (!current_function_decl
9951 : 120 : || !DECL_DEFAULTED_FN (current_function_decl))
9952 : : {
9953 : : /* This routine is used for both initialization and assignment.
9954 : : Make sure the diagnostic message differentiates the context. */
9955 : 73 : if (complain & tf_error)
9956 : : {
9957 : 30 : if (modifycode == INIT_EXPR)
9958 : 9 : error_at (loc, "array used as initializer");
9959 : : else
9960 : 21 : error_at (loc, "invalid array assignment");
9961 : : }
9962 : 73 : return error_mark_node;
9963 : : }
9964 : :
9965 : 128 : from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9966 : 81 : ? 1 + (modifycode != INIT_EXPR) : 0;
9967 : 81 : result = build_vec_init (lhs, NULL_TREE, newrhs,
9968 : : /*explicit_value_init_p=*/false,
9969 : : from_array, complain);
9970 : 81 : goto ret;
9971 : : }
9972 : :
9973 : 30017334 : if (modifycode == INIT_EXPR)
9974 : : /* Calls with INIT_EXPR are all direct-initialization, so don't set
9975 : : LOOKUP_ONLYCONVERTING. */
9976 : 3306100 : newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9977 : : ICR_INIT, NULL_TREE, 0,
9978 : : complain | tf_no_cleanup);
9979 : : else
9980 : 26711234 : newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9981 : : NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9982 : :
9983 : 30017334 : if (!same_type_p (lhstype, olhstype))
9984 : 251750 : newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9985 : :
9986 : 30017334 : if (modifycode != INIT_EXPR)
9987 : : {
9988 : 26711234 : if (TREE_CODE (newrhs) == CALL_EXPR
9989 : 26711234 : && TYPE_NEEDS_CONSTRUCTING (lhstype))
9990 : 0 : newrhs = build_cplus_new (lhstype, newrhs, complain);
9991 : :
9992 : : /* Can't initialize directly from a TARGET_EXPR, since that would
9993 : : cause the lhs to be constructed twice, and possibly result in
9994 : : accidental self-initialization. So we force the TARGET_EXPR to be
9995 : : expanded without a target. */
9996 : 26711234 : if (TREE_CODE (newrhs) == TARGET_EXPR)
9997 : 84 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9998 : 84 : TARGET_EXPR_SLOT (newrhs));
9999 : : }
10000 : :
10001 : 30017334 : if (newrhs == error_mark_node)
10002 : : return error_mark_node;
10003 : :
10004 : 30016766 : if (c_dialect_objc () && flag_objc_gc)
10005 : : {
10006 : 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
10007 : :
10008 : 0 : if (result)
10009 : 0 : goto ret;
10010 : : }
10011 : :
10012 : 33322817 : result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
10013 : : lhstype, lhs, newrhs);
10014 : 30016766 : if (modifycode == INIT_EXPR)
10015 : 3306051 : set_target_expr_eliding (newrhs);
10016 : :
10017 : 30016766 : TREE_SIDE_EFFECTS (result) = 1;
10018 : 30016766 : if (!plain_assign)
10019 : 7469745 : suppress_warning (result, OPT_Wparentheses);
10020 : :
10021 : 22547021 : ret:
10022 : 32567540 : if (preeval)
10023 : 33 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
10024 : : return result;
10025 : : }
10026 : :
10027 : : cp_expr
10028 : 59879395 : build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
10029 : : tree rhs, tree lookups, tsubst_flags_t complain)
10030 : : {
10031 : 59879395 : tree orig_lhs = lhs;
10032 : 59879395 : tree orig_rhs = rhs;
10033 : 59879395 : tree overload = NULL_TREE;
10034 : :
10035 : 59879395 : if (lhs == error_mark_node || rhs == error_mark_node)
10036 : 2093 : return cp_expr (error_mark_node, loc);
10037 : :
10038 : 59877302 : tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
10039 : :
10040 : 59877302 : if (processing_template_decl)
10041 : : {
10042 : 40306709 : if (type_dependent_expression_p (lhs)
10043 : 40306709 : || type_dependent_expression_p (rhs))
10044 : : {
10045 : 31029107 : tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
10046 : 31029107 : if (modifycode != NOP_EXPR)
10047 : 5817768 : TREE_TYPE (rval)
10048 : 11635536 : = build_dependent_operator_type (lookups, modifycode, true);
10049 : 31029107 : return rval;
10050 : : }
10051 : : }
10052 : :
10053 : 28848195 : tree rval;
10054 : 28848195 : if (modifycode == NOP_EXPR)
10055 : 22982429 : rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
10056 : : else
10057 : 5865766 : rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
10058 : : lhs, rhs, op, lookups, &overload, complain);
10059 : 28848195 : if (rval == error_mark_node)
10060 : 1995 : return error_mark_node;
10061 : 28846200 : if (processing_template_decl)
10062 : : {
10063 : 9277581 : if (overload != NULL_TREE)
10064 : 1244786 : return (build_min_non_dep_op_overload
10065 : 1244786 : (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
10066 : :
10067 : 8032795 : return (build_min_non_dep
10068 : 8032795 : (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
10069 : : }
10070 : 19568619 : return rval;
10071 : : }
10072 : :
10073 : : /* Helper function for get_delta_difference which assumes FROM is a base
10074 : : class of TO. Returns a delta for the conversion of pointer-to-member
10075 : : of FROM to pointer-to-member of TO. If the conversion is invalid and
10076 : : tf_error is not set in COMPLAIN returns error_mark_node, otherwise
10077 : : returns zero. If FROM is not a base class of TO, returns NULL_TREE.
10078 : : If C_CAST_P is true, this conversion is taking place as part of a
10079 : : C-style cast. */
10080 : :
10081 : : static tree
10082 : 30197 : get_delta_difference_1 (tree from, tree to, bool c_cast_p,
10083 : : tsubst_flags_t complain)
10084 : : {
10085 : 30197 : tree binfo;
10086 : 30197 : base_kind kind;
10087 : :
10088 : 60144 : binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
10089 : : &kind, complain);
10090 : :
10091 : 30197 : if (binfo == error_mark_node)
10092 : : {
10093 : 51 : if (!(complain & tf_error))
10094 : : return error_mark_node;
10095 : :
10096 : 51 : inform (input_location, " in pointer to member function conversion");
10097 : 51 : return size_zero_node;
10098 : : }
10099 : 30146 : else if (binfo)
10100 : : {
10101 : 29998 : if (kind != bk_via_virtual)
10102 : 29905 : return BINFO_OFFSET (binfo);
10103 : : else
10104 : : /* FROM is a virtual base class of TO. Issue an error or warning
10105 : : depending on whether or not this is a reinterpret cast. */
10106 : : {
10107 : 93 : if (!(complain & tf_error))
10108 : : return error_mark_node;
10109 : :
10110 : 78 : error ("pointer to member conversion via virtual base %qT",
10111 : 78 : BINFO_TYPE (binfo_from_vbase (binfo)));
10112 : :
10113 : 78 : return size_zero_node;
10114 : : }
10115 : : }
10116 : : else
10117 : : return NULL_TREE;
10118 : : }
10119 : :
10120 : : /* Get difference in deltas for different pointer to member function
10121 : : types. If the conversion is invalid and tf_error is not set in
10122 : : COMPLAIN, returns error_mark_node, otherwise returns an integer
10123 : : constant of type PTRDIFF_TYPE_NODE and its value is zero if the
10124 : : conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
10125 : : conversions as well. If C_CAST_P is true this conversion is taking
10126 : : place as part of a C-style cast.
10127 : :
10128 : : Note that the naming of FROM and TO is kind of backwards; the return
10129 : : value is what we add to a TO in order to get a FROM. They are named
10130 : : this way because we call this function to find out how to convert from
10131 : : a pointer to member of FROM to a pointer to member of TO. */
10132 : :
10133 : : static tree
10134 : 93860 : get_delta_difference (tree from, tree to,
10135 : : bool allow_inverse_p,
10136 : : bool c_cast_p, tsubst_flags_t complain)
10137 : : {
10138 : 93860 : auto_diagnostic_group d;
10139 : 93860 : tree result;
10140 : :
10141 : 93860 : if (same_type_ignoring_top_level_qualifiers_p (from, to))
10142 : : /* Pointer to member of incomplete class is permitted*/
10143 : 63811 : result = size_zero_node;
10144 : : else
10145 : 30049 : result = get_delta_difference_1 (from, to, c_cast_p, complain);
10146 : :
10147 : 93860 : if (result == error_mark_node)
10148 : : return error_mark_node;
10149 : :
10150 : 93845 : if (!result)
10151 : : {
10152 : 148 : if (!allow_inverse_p)
10153 : : {
10154 : 0 : if (!(complain & tf_error))
10155 : : return error_mark_node;
10156 : :
10157 : 0 : error_not_base_type (from, to);
10158 : 0 : inform (input_location, " in pointer to member conversion");
10159 : 0 : result = size_zero_node;
10160 : : }
10161 : : else
10162 : : {
10163 : 148 : result = get_delta_difference_1 (to, from, c_cast_p, complain);
10164 : :
10165 : 148 : if (result == error_mark_node)
10166 : : return error_mark_node;
10167 : :
10168 : 148 : if (result)
10169 : 148 : result = size_diffop_loc (input_location,
10170 : : size_zero_node, result);
10171 : : else
10172 : : {
10173 : 0 : if (!(complain & tf_error))
10174 : : return error_mark_node;
10175 : :
10176 : 0 : error_not_base_type (from, to);
10177 : 0 : inform (input_location, " in pointer to member conversion");
10178 : 0 : result = size_zero_node;
10179 : : }
10180 : : }
10181 : : }
10182 : :
10183 : 93845 : return convert_to_integer (ptrdiff_type_node, result);
10184 : 93860 : }
10185 : :
10186 : : /* Return a constructor for the pointer-to-member-function TYPE using
10187 : : the other components as specified. */
10188 : :
10189 : : tree
10190 : 64270 : build_ptrmemfunc1 (tree type, tree delta, tree pfn)
10191 : : {
10192 : 64270 : tree u = NULL_TREE;
10193 : 64270 : tree delta_field;
10194 : 64270 : tree pfn_field;
10195 : 64270 : vec<constructor_elt, va_gc> *v;
10196 : :
10197 : : /* Pull the FIELD_DECLs out of the type. */
10198 : 64270 : pfn_field = TYPE_FIELDS (type);
10199 : 64270 : delta_field = DECL_CHAIN (pfn_field);
10200 : :
10201 : : /* Make sure DELTA has the type we want. */
10202 : 64270 : delta = convert_and_check (input_location, delta_type_node, delta);
10203 : :
10204 : : /* Convert to the correct target type if necessary. */
10205 : 64270 : pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10206 : :
10207 : : /* Finish creating the initializer. */
10208 : 64270 : vec_alloc (v, 2);
10209 : 64270 : CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10210 : 64270 : CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10211 : 64270 : u = build_constructor (type, v);
10212 : 64270 : TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10213 : 64270 : TREE_STATIC (u) = (TREE_CONSTANT (u)
10214 : 64168 : && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10215 : : != NULL_TREE)
10216 : 128438 : && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10217 : : != NULL_TREE));
10218 : 64270 : return u;
10219 : : }
10220 : :
10221 : : /* Build a constructor for a pointer to member function. It can be
10222 : : used to initialize global variables, local variable, or used
10223 : : as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10224 : : want to be.
10225 : :
10226 : : If FORCE is nonzero, then force this conversion, even if
10227 : : we would rather not do it. Usually set when using an explicit
10228 : : cast. A C-style cast is being processed iff C_CAST_P is true.
10229 : :
10230 : : Return error_mark_node, if something goes wrong. */
10231 : :
10232 : : tree
10233 : 31643 : build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10234 : : tsubst_flags_t complain)
10235 : : {
10236 : 31643 : tree fn;
10237 : 31643 : tree pfn_type;
10238 : 31643 : tree to_type;
10239 : :
10240 : 31643 : if (error_operand_p (pfn))
10241 : 0 : return error_mark_node;
10242 : :
10243 : 31643 : pfn_type = TREE_TYPE (pfn);
10244 : 31643 : to_type = build_ptrmemfunc_type (type);
10245 : :
10246 : : /* Handle multiple conversions of pointer to member functions. */
10247 : 31643 : if (TYPE_PTRMEMFUNC_P (pfn_type))
10248 : : {
10249 : 29756 : tree delta = NULL_TREE;
10250 : 29756 : tree npfn = NULL_TREE;
10251 : 29756 : tree n;
10252 : :
10253 : 29756 : if (!force
10254 : 29756 : && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
10255 : : LOOKUP_NORMAL, complain))
10256 : : {
10257 : 0 : if (complain & tf_error)
10258 : 0 : error ("invalid conversion to type %qT from type %qT",
10259 : : to_type, pfn_type);
10260 : : else
10261 : 0 : return error_mark_node;
10262 : : }
10263 : :
10264 : 29756 : n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10265 : 29756 : TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10266 : : force,
10267 : : c_cast_p, complain);
10268 : 29756 : if (n == error_mark_node)
10269 : : return error_mark_node;
10270 : :
10271 : 29747 : STRIP_ANY_LOCATION_WRAPPER (pfn);
10272 : :
10273 : : /* We don't have to do any conversion to convert a
10274 : : pointer-to-member to its own type. But, we don't want to
10275 : : just return a PTRMEM_CST if there's an explicit cast; that
10276 : : cast should make the expression an invalid template argument. */
10277 : 29747 : if (TREE_CODE (pfn) != PTRMEM_CST
10278 : 29747 : && same_type_p (to_type, pfn_type))
10279 : : return pfn;
10280 : :
10281 : 29747 : if (TREE_SIDE_EFFECTS (pfn))
10282 : 8 : pfn = save_expr (pfn);
10283 : :
10284 : : /* Obtain the function pointer and the current DELTA. */
10285 : 29747 : if (TREE_CODE (pfn) == PTRMEM_CST)
10286 : 29659 : expand_ptrmemfunc_cst (pfn, &delta, &npfn);
10287 : : else
10288 : : {
10289 : 88 : npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
10290 : 88 : delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
10291 : : }
10292 : :
10293 : : /* Just adjust the DELTA field. */
10294 : 29747 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10295 : : (TREE_TYPE (delta), ptrdiff_type_node));
10296 : 29747 : if (!integer_zerop (n))
10297 : : {
10298 : 50 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
10299 : : n = cp_build_binary_op (input_location,
10300 : : LSHIFT_EXPR, n, integer_one_node,
10301 : : complain);
10302 : 50 : delta = cp_build_binary_op (input_location,
10303 : : PLUS_EXPR, delta, n, complain);
10304 : : }
10305 : 29747 : return build_ptrmemfunc1 (to_type, delta, npfn);
10306 : : }
10307 : :
10308 : : /* Handle null pointer to member function conversions. */
10309 : 1887 : if (null_ptr_cst_p (pfn))
10310 : : {
10311 : 824 : pfn = cp_build_c_cast (input_location,
10312 : 824 : TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10313 : : pfn, complain);
10314 : 824 : return build_ptrmemfunc1 (to_type,
10315 : : integer_zero_node,
10316 : 824 : pfn);
10317 : : }
10318 : :
10319 : 1063 : if (type_unknown_p (pfn))
10320 : 0 : return instantiate_type (type, pfn, complain);
10321 : :
10322 : 1063 : fn = TREE_OPERAND (pfn, 0);
10323 : 1063 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10324 : : /* In a template, we will have preserved the
10325 : : OFFSET_REF. */
10326 : : || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10327 : 1063 : return make_ptrmem_cst (to_type, fn);
10328 : : }
10329 : :
10330 : : /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10331 : : given by CST.
10332 : :
10333 : : ??? There is no consistency as to the types returned for the above
10334 : : values. Some code acts as if it were a sizetype and some as if it were
10335 : : integer_type_node. */
10336 : :
10337 : : void
10338 : 63740 : expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10339 : : {
10340 : 63740 : tree type = TREE_TYPE (cst);
10341 : 63740 : tree fn = PTRMEM_CST_MEMBER (cst);
10342 : 63740 : tree ptr_class, fn_class;
10343 : :
10344 : 63740 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10345 : :
10346 : : /* The class that the function belongs to. */
10347 : 63740 : fn_class = DECL_CONTEXT (fn);
10348 : :
10349 : : /* The class that we're creating a pointer to member of. */
10350 : 63740 : ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10351 : :
10352 : : /* First, calculate the adjustment to the function's class. */
10353 : 63740 : *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10354 : : /*c_cast_p=*/0, tf_warning_or_error);
10355 : :
10356 : 63740 : if (!DECL_VIRTUAL_P (fn))
10357 : : {
10358 : 62512 : tree t = build_addr_func (fn, tf_warning_or_error);
10359 : 62512 : if (TREE_CODE (t) == ADDR_EXPR)
10360 : 62512 : SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10361 : 62512 : *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10362 : : }
10363 : : else
10364 : : {
10365 : : /* If we're dealing with a virtual function, we have to adjust 'this'
10366 : : again, to point to the base which provides the vtable entry for
10367 : : fn; the call will do the opposite adjustment. */
10368 : 1228 : tree orig_class = DECL_CONTEXT (fn);
10369 : 1228 : tree binfo = binfo_or_else (orig_class, fn_class);
10370 : 1228 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10371 : : *delta, BINFO_OFFSET (binfo));
10372 : :
10373 : : /* We set PFN to the vtable offset at which the function can be
10374 : : found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10375 : : case delta is shifted left, and then incremented). */
10376 : 1228 : *pfn = DECL_VINDEX (fn);
10377 : 1228 : *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10378 : : TYPE_SIZE_UNIT (vtable_entry_type));
10379 : :
10380 : 1228 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10381 : : {
10382 : 1228 : case ptrmemfunc_vbit_in_pfn:
10383 : 1228 : *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10384 : : integer_one_node);
10385 : 1228 : break;
10386 : :
10387 : : case ptrmemfunc_vbit_in_delta:
10388 : : *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10389 : : *delta, integer_one_node);
10390 : : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10391 : : *delta, integer_one_node);
10392 : : break;
10393 : :
10394 : : default:
10395 : : gcc_unreachable ();
10396 : : }
10397 : :
10398 : 1228 : *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10399 : : }
10400 : 63740 : }
10401 : :
10402 : : /* Return an expression for PFN from the pointer-to-member function
10403 : : given by T. */
10404 : :
10405 : : static tree
10406 : 90391 : pfn_from_ptrmemfunc (tree t)
10407 : : {
10408 : 90391 : if (TREE_CODE (t) == PTRMEM_CST)
10409 : : {
10410 : 191 : tree delta;
10411 : 191 : tree pfn;
10412 : :
10413 : 191 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10414 : 191 : if (pfn)
10415 : 191 : return pfn;
10416 : : }
10417 : :
10418 : 90200 : return build_ptrmemfunc_access_expr (t, pfn_identifier);
10419 : : }
10420 : :
10421 : : /* Return an expression for DELTA from the pointer-to-member function
10422 : : given by T. */
10423 : :
10424 : : static tree
10425 : 90391 : delta_from_ptrmemfunc (tree t)
10426 : : {
10427 : 90391 : if (TREE_CODE (t) == PTRMEM_CST)
10428 : : {
10429 : 191 : tree delta;
10430 : 191 : tree pfn;
10431 : :
10432 : 191 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10433 : 191 : if (delta)
10434 : 191 : return delta;
10435 : : }
10436 : :
10437 : 90200 : return build_ptrmemfunc_access_expr (t, delta_identifier);
10438 : : }
10439 : :
10440 : : /* Convert value RHS to type TYPE as preparation for an assignment to
10441 : : an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10442 : : implicit conversion is. If FNDECL is non-NULL, we are doing the
10443 : : conversion in order to pass the PARMNUMth argument of FNDECL.
10444 : : If FNDECL is NULL, we are doing the conversion in function pointer
10445 : : argument passing, conversion in initialization, etc. */
10446 : :
10447 : : static tree
10448 : 124275775 : convert_for_assignment (tree type, tree rhs,
10449 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10450 : : tsubst_flags_t complain, int flags)
10451 : : {
10452 : 124275775 : tree rhstype;
10453 : 124275775 : enum tree_code coder;
10454 : :
10455 : 124275775 : location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10456 : 124275775 : bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10457 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10458 : : but preserve location wrappers. */
10459 : 124275775 : if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10460 : 124275775 : && !location_wrapper_p (rhs))
10461 : 8411 : rhs = TREE_OPERAND (rhs, 0);
10462 : :
10463 : : /* Handle [dcl.init.list] direct-list-initialization from
10464 : : single element of enumeration with a fixed underlying type. */
10465 : 124275775 : if (is_direct_enum_init (type, rhs))
10466 : : {
10467 : 20 : tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10468 : 20 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10469 : : {
10470 : 11 : warning_sentinel w (warn_useless_cast);
10471 : 11 : warning_sentinel w2 (warn_ignored_qualifiers);
10472 : 11 : rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10473 : 11 : }
10474 : : else
10475 : 9 : rhs = error_mark_node;
10476 : : }
10477 : :
10478 : 124275775 : rhstype = TREE_TYPE (rhs);
10479 : 124275775 : coder = TREE_CODE (rhstype);
10480 : :
10481 : 1338175 : if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10482 : 125613844 : && vector_types_convertible_p (type, rhstype, true))
10483 : : {
10484 : 1338060 : rhs = mark_rvalue_use (rhs);
10485 : 1338060 : return convert (type, rhs);
10486 : : }
10487 : :
10488 : 122937715 : if (rhs == error_mark_node || rhstype == error_mark_node)
10489 : : return error_mark_node;
10490 : 122937706 : if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10491 : : return error_mark_node;
10492 : :
10493 : : /* The RHS of an assignment cannot have void type. */
10494 : 122937706 : if (coder == VOID_TYPE)
10495 : : {
10496 : 48 : if (complain & tf_error)
10497 : 30 : error_at (rhs_loc, "void value not ignored as it ought to be");
10498 : 48 : return error_mark_node;
10499 : : }
10500 : :
10501 : 122937658 : if (c_dialect_objc ())
10502 : : {
10503 : 0 : int parmno;
10504 : 0 : tree selector;
10505 : 0 : tree rname = fndecl;
10506 : :
10507 : 0 : switch (errtype)
10508 : : {
10509 : : case ICR_ASSIGN:
10510 : : parmno = -1;
10511 : : break;
10512 : 0 : case ICR_INIT:
10513 : 0 : parmno = -2;
10514 : 0 : break;
10515 : 0 : default:
10516 : 0 : selector = objc_message_selector ();
10517 : 0 : parmno = parmnum;
10518 : 0 : if (selector && parmno > 1)
10519 : : {
10520 : 0 : rname = selector;
10521 : 0 : parmno -= 1;
10522 : : }
10523 : : }
10524 : :
10525 : 0 : if (objc_compare_types (type, rhstype, parmno, rname))
10526 : : {
10527 : 0 : rhs = mark_rvalue_use (rhs);
10528 : 0 : return convert (type, rhs);
10529 : : }
10530 : : }
10531 : :
10532 : : /* [expr.ass]
10533 : :
10534 : : The expression is implicitly converted (clause _conv_) to the
10535 : : cv-unqualified type of the left operand.
10536 : :
10537 : : We allow bad conversions here because by the time we get to this point
10538 : : we are committed to doing the conversion. If we end up doing a bad
10539 : : conversion, convert_like will complain. */
10540 : 122937658 : if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10541 : : {
10542 : : /* When -Wno-pmf-conversions is use, we just silently allow
10543 : : conversions from pointers-to-members to plain pointers. If
10544 : : the conversion doesn't work, cp_convert will complain. */
10545 : 1471 : if (!warn_pmf2ptr
10546 : 9 : && TYPE_PTR_P (type)
10547 : 1480 : && TYPE_PTRMEMFUNC_P (rhstype))
10548 : 9 : rhs = cp_convert (strip_top_quals (type), rhs, complain);
10549 : : else
10550 : : {
10551 : 1462 : if (complain & tf_error)
10552 : : {
10553 : 1271 : auto_diagnostic_group d;
10554 : :
10555 : : /* If the right-hand side has unknown type, then it is an
10556 : : overloaded function. Call instantiate_type to get error
10557 : : messages. */
10558 : 1271 : if (rhstype == unknown_type_node)
10559 : : {
10560 : 197 : tree r = instantiate_type (type, rhs, tf_warning_or_error);
10561 : : /* -fpermissive might allow this; recurse. */
10562 : 197 : if (!seen_error ())
10563 : 15 : return convert_for_assignment (type, r, errtype, fndecl,
10564 : : parmnum, complain, flags);
10565 : : }
10566 : 1074 : else if (fndecl)
10567 : 92 : complain_about_bad_argument (rhs_loc,
10568 : : rhstype, type,
10569 : : fndecl, parmnum);
10570 : : else
10571 : : {
10572 : 982 : range_label_for_type_mismatch label (rhstype, type);
10573 : 982 : gcc_rich_location richloc
10574 : : (rhs_loc,
10575 : : has_loc ? &label : NULL,
10576 : 982 : has_loc ? highlight_colors::percent_h : NULL);
10577 : :
10578 : 982 : switch (errtype)
10579 : : {
10580 : 0 : case ICR_DEFAULT_ARGUMENT:
10581 : 0 : error_at (&richloc,
10582 : : "cannot convert %qH to %qI in default argument",
10583 : : rhstype, type);
10584 : 0 : break;
10585 : 6 : case ICR_ARGPASS:
10586 : 6 : error_at (&richloc,
10587 : : "cannot convert %qH to %qI in argument passing",
10588 : : rhstype, type);
10589 : 6 : break;
10590 : 0 : case ICR_CONVERTING:
10591 : 0 : error_at (&richloc, "cannot convert %qH to %qI",
10592 : : rhstype, type);
10593 : 0 : break;
10594 : 544 : case ICR_INIT:
10595 : 544 : error_at (&richloc,
10596 : : "cannot convert %qH to %qI in initialization",
10597 : : rhstype, type);
10598 : 544 : break;
10599 : 18 : case ICR_RETURN:
10600 : 18 : error_at (&richloc, "cannot convert %qH to %qI in return",
10601 : : rhstype, type);
10602 : 18 : break;
10603 : 414 : case ICR_ASSIGN:
10604 : 414 : error_at (&richloc,
10605 : : "cannot convert %qH to %qI in assignment",
10606 : : rhstype, type);
10607 : 414 : break;
10608 : 0 : default:
10609 : 0 : gcc_unreachable();
10610 : : }
10611 : 982 : }
10612 : :
10613 : : /* See if we can be more helpful. */
10614 : 1256 : maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
10615 : :
10616 : 1256 : if (TYPE_PTR_P (rhstype)
10617 : 411 : && TYPE_PTR_P (type)
10618 : 396 : && CLASS_TYPE_P (TREE_TYPE (rhstype))
10619 : 278 : && CLASS_TYPE_P (TREE_TYPE (type))
10620 : 1318 : && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10621 : 3 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10622 : : (TREE_TYPE (rhstype))),
10623 : 3 : "class type %qT is incomplete", TREE_TYPE (rhstype));
10624 : 1271 : }
10625 : 1447 : return error_mark_node;
10626 : : }
10627 : : }
10628 : 122936196 : if (warn_suggest_attribute_format)
10629 : : {
10630 : 2363 : const enum tree_code codel = TREE_CODE (type);
10631 : 2363 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10632 : 592 : && coder == codel
10633 : 504 : && check_missing_format_attribute (type, rhstype)
10634 : 2381 : && (complain & tf_warning))
10635 : 18 : switch (errtype)
10636 : : {
10637 : 0 : case ICR_ARGPASS:
10638 : 0 : case ICR_DEFAULT_ARGUMENT:
10639 : 0 : if (fndecl)
10640 : 0 : warning (OPT_Wsuggest_attribute_format,
10641 : : "parameter %qP of %qD might be a candidate "
10642 : : "for a format attribute", parmnum, fndecl);
10643 : : else
10644 : 0 : warning (OPT_Wsuggest_attribute_format,
10645 : : "parameter might be a candidate "
10646 : : "for a format attribute");
10647 : : break;
10648 : 0 : case ICR_CONVERTING:
10649 : 0 : warning (OPT_Wsuggest_attribute_format,
10650 : : "target of conversion might be a candidate "
10651 : : "for a format attribute");
10652 : 0 : break;
10653 : 6 : case ICR_INIT:
10654 : 6 : warning (OPT_Wsuggest_attribute_format,
10655 : : "target of initialization might be a candidate "
10656 : : "for a format attribute");
10657 : 6 : break;
10658 : 6 : case ICR_RETURN:
10659 : 6 : warning (OPT_Wsuggest_attribute_format,
10660 : : "return type might be a candidate "
10661 : : "for a format attribute");
10662 : 6 : break;
10663 : 6 : case ICR_ASSIGN:
10664 : 6 : warning (OPT_Wsuggest_attribute_format,
10665 : : "left-hand side of assignment might be a candidate "
10666 : : "for a format attribute");
10667 : 6 : break;
10668 : 0 : default:
10669 : 0 : gcc_unreachable();
10670 : : }
10671 : : }
10672 : :
10673 : 122936196 : if (TREE_CODE (type) == BOOLEAN_TYPE)
10674 : 24351705 : maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
10675 : :
10676 : 122936196 : if (complain & tf_warning)
10677 : 121649900 : warn_for_address_of_packed_member (type, rhs);
10678 : :
10679 : 122936196 : return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10680 : 122936196 : complain, flags);
10681 : : }
10682 : :
10683 : : /* Convert RHS to be of type TYPE.
10684 : : If EXP is nonzero, it is the target of the initialization.
10685 : : ERRTYPE indicates what kind of error the implicit conversion is.
10686 : :
10687 : : Two major differences between the behavior of
10688 : : `convert_for_assignment' and `convert_for_initialization'
10689 : : are that references are bashed in the former, while
10690 : : copied in the latter, and aggregates are assigned in
10691 : : the former (operator=) while initialized in the
10692 : : latter (X(X&)).
10693 : :
10694 : : If using constructor make sure no conversion operator exists, if one does
10695 : : exist, an ambiguity exists. */
10696 : :
10697 : : tree
10698 : 109942884 : convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10699 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10700 : : tsubst_flags_t complain)
10701 : : {
10702 : 109942884 : enum tree_code codel = TREE_CODE (type);
10703 : 109942884 : tree rhstype;
10704 : 109942884 : enum tree_code coder;
10705 : :
10706 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10707 : : Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10708 : 109942884 : if (TREE_CODE (rhs) == NOP_EXPR
10709 : 4784736 : && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10710 : 110301637 : && codel != REFERENCE_TYPE)
10711 : 358753 : rhs = TREE_OPERAND (rhs, 0);
10712 : :
10713 : 109942884 : if (type == error_mark_node
10714 : 109942846 : || rhs == error_mark_node
10715 : 219885669 : || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10716 : : return error_mark_node;
10717 : :
10718 : 109942785 : if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10719 : : ;
10720 : 99325518 : else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10721 : 1573878 : && TREE_CODE (type) != ARRAY_TYPE
10722 : 1573878 : && (!TYPE_REF_P (type)
10723 : 1013 : || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10724 : 97752650 : || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10725 : 13541 : && !TYPE_REFFN_P (type))
10726 : 197066316 : || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10727 : 1584726 : rhs = decay_conversion (rhs, complain);
10728 : :
10729 : 109942785 : rhstype = TREE_TYPE (rhs);
10730 : 109942785 : coder = TREE_CODE (rhstype);
10731 : :
10732 : 109942785 : if (coder == ERROR_MARK)
10733 : 9 : return error_mark_node;
10734 : :
10735 : : /* We accept references to incomplete types, so we can
10736 : : return here before checking if RHS is of complete type. */
10737 : :
10738 : 109942776 : if (codel == REFERENCE_TYPE)
10739 : : {
10740 : 5129591 : auto_diagnostic_group d;
10741 : : /* This should eventually happen in convert_arguments. */
10742 : 5129591 : int savew = 0, savee = 0;
10743 : :
10744 : 5129591 : if (fndecl)
10745 : 252036 : savew = warningcount + werrorcount, savee = errorcount;
10746 : 5129591 : rhs = initialize_reference (type, rhs, flags, complain);
10747 : :
10748 : 5129591 : if (fndecl
10749 : 5129591 : && (warningcount + werrorcount > savew || errorcount > savee))
10750 : 36 : inform (get_fndecl_argument_location (fndecl, parmnum),
10751 : : "in passing argument %P of %qD", parmnum, fndecl);
10752 : 5129591 : return rhs;
10753 : 5129591 : }
10754 : :
10755 : 104813185 : if (exp != 0)
10756 : 3306100 : exp = require_complete_type (exp, complain);
10757 : 104813185 : if (exp == error_mark_node)
10758 : : return error_mark_node;
10759 : :
10760 : 104813185 : type = complete_type (type);
10761 : :
10762 : 104813185 : if (DIRECT_INIT_EXPR_P (type, rhs))
10763 : : /* Don't try to do copy-initialization if we already have
10764 : : direct-initialization. */
10765 : : return rhs;
10766 : :
10767 : 104812226 : if (MAYBE_CLASS_TYPE_P (type))
10768 : 7247700 : return perform_implicit_conversion_flags (type, rhs, complain, flags);
10769 : :
10770 : 97564526 : return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10771 : 97564526 : complain, flags);
10772 : : }
10773 : :
10774 : : /* If RETVAL is the address of, or a reference to, a local variable or
10775 : : temporary give an appropriate warning and return true. */
10776 : :
10777 : : static bool
10778 : 36076099 : maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10779 : : {
10780 : 36076786 : tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10781 : 36076786 : tree whats_returned = fold_for_warn (retval);
10782 : 36076786 : if (!loc)
10783 : 36076786 : loc = cp_expr_loc_or_input_loc (retval);
10784 : :
10785 : 43743101 : for (;;)
10786 : : {
10787 : 43743101 : if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10788 : 476908 : whats_returned = TREE_OPERAND (whats_returned, 1);
10789 : 43266193 : else if (CONVERT_EXPR_P (whats_returned)
10790 : 36097209 : || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10791 : 7189407 : whats_returned = TREE_OPERAND (whats_returned, 0);
10792 : : else
10793 : : break;
10794 : : }
10795 : :
10796 : 36076786 : if (TREE_CODE (whats_returned) == TARGET_EXPR
10797 : 36076786 : && is_std_init_list (TREE_TYPE (whats_returned)))
10798 : : {
10799 : 16 : tree init = TARGET_EXPR_INITIAL (whats_returned);
10800 : 16 : if (TREE_CODE (init) == CONSTRUCTOR)
10801 : : /* Pull out the array address. */
10802 : 6 : whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10803 : 10 : else if (TREE_CODE (init) == INDIRECT_REF)
10804 : : /* The source of a trivial copy looks like *(T*)&var. */
10805 : 7 : whats_returned = TREE_OPERAND (init, 0);
10806 : : else
10807 : : return false;
10808 : 13 : STRIP_NOPS (whats_returned);
10809 : : }
10810 : :
10811 : : /* As a special case, we handle a call to std::move or std::forward. */
10812 : 36076783 : if (TREE_CODE (whats_returned) == CALL_EXPR
10813 : 36076783 : && (is_std_move_p (whats_returned)
10814 : 9865398 : || is_std_forward_p (whats_returned)))
10815 : : {
10816 : 675 : tree arg = CALL_EXPR_ARG (whats_returned, 0);
10817 : 675 : return maybe_warn_about_returning_address_of_local (arg, loc);
10818 : : }
10819 : :
10820 : 36076108 : if (TREE_CODE (whats_returned) != ADDR_EXPR)
10821 : : return false;
10822 : 789783 : whats_returned = TREE_OPERAND (whats_returned, 0);
10823 : :
10824 : 789783 : while (TREE_CODE (whats_returned) == COMPONENT_REF
10825 : 1533839 : || TREE_CODE (whats_returned) == ARRAY_REF)
10826 : 744056 : whats_returned = TREE_OPERAND (whats_returned, 0);
10827 : :
10828 : 789783 : if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10829 : 789783 : || TREE_CODE (whats_returned) == TARGET_EXPR)
10830 : : {
10831 : 50 : if (TYPE_REF_P (valtype))
10832 : : /* P2748 made this an error in C++26. */
10833 : 72 : emit_diagnostic ((cxx_dialect >= cxx26
10834 : : ? diagnostics::kind::permerror
10835 : : : diagnostics::kind::warning),
10836 : 41 : loc, OPT_Wreturn_local_addr,
10837 : : "returning reference to temporary");
10838 : 9 : else if (TYPE_PTR_P (valtype))
10839 : 3 : warning_at (loc, OPT_Wreturn_local_addr,
10840 : : "returning pointer to temporary");
10841 : 6 : else if (is_std_init_list (valtype))
10842 : 6 : warning_at (loc, OPT_Winit_list_lifetime,
10843 : : "returning temporary %<initializer_list%> does not extend "
10844 : : "the lifetime of the underlying array");
10845 : 50 : return true;
10846 : : }
10847 : :
10848 : 789733 : STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10849 : :
10850 : 789733 : if (DECL_P (whats_returned)
10851 : 76789 : && DECL_NAME (whats_returned)
10852 : 76789 : && DECL_FUNCTION_SCOPE_P (whats_returned)
10853 : 12623 : && !is_capture_proxy (whats_returned)
10854 : 802335 : && !(TREE_STATIC (whats_returned)
10855 : 146 : || TREE_PUBLIC (whats_returned)))
10856 : : {
10857 : 106 : if (DECL_DECOMPOSITION_P (whats_returned)
10858 : 39 : && !DECL_DECOMP_IS_BASE (whats_returned)
10859 : 185 : && DECL_HAS_VALUE_EXPR_P (whats_returned))
10860 : : {
10861 : : /* When returning address of a structured binding, if the structured
10862 : : binding is not a reference, continue normally, if it is a
10863 : : reference, recurse on the initializer of the structured
10864 : : binding. */
10865 : 39 : tree base = DECL_DECOMP_BASE (whats_returned);
10866 : 39 : if (TYPE_REF_P (TREE_TYPE (base)))
10867 : : {
10868 : 15 : if (tree init = DECL_INITIAL (base))
10869 : : return maybe_warn_about_returning_address_of_local (init, loc);
10870 : : else
10871 : : return false;
10872 : : }
10873 : : }
10874 : 131 : bool w = false;
10875 : 131 : auto_diagnostic_group d;
10876 : 131 : if (TYPE_REF_P (valtype))
10877 : 81 : w = warning_at (loc, OPT_Wreturn_local_addr,
10878 : : "reference to local variable %qD returned",
10879 : : whats_returned);
10880 : 50 : else if (is_std_init_list (valtype))
10881 : 3 : w = warning_at (loc, OPT_Winit_list_lifetime,
10882 : : "returning local %<initializer_list%> variable %qD "
10883 : : "does not extend the lifetime of the underlying array",
10884 : : whats_returned);
10885 : 47 : else if (POINTER_TYPE_P (valtype)
10886 : 41 : && TREE_CODE (whats_returned) == LABEL_DECL)
10887 : 6 : w = warning_at (loc, OPT_Wreturn_local_addr,
10888 : : "address of label %qD returned",
10889 : : whats_returned);
10890 : 41 : else if (POINTER_TYPE_P (valtype))
10891 : 35 : w = warning_at (loc, OPT_Wreturn_local_addr,
10892 : : "address of local variable %qD returned",
10893 : : whats_returned);
10894 : 125 : if (w)
10895 : 103 : inform (DECL_SOURCE_LOCATION (whats_returned),
10896 : : "declared here");
10897 : 131 : return true;
10898 : 131 : }
10899 : :
10900 : : return false;
10901 : : }
10902 : :
10903 : : /* Returns true if DECL is in the std namespace. */
10904 : :
10905 : : bool
10906 : 75372330 : decl_in_std_namespace_p (tree decl)
10907 : : {
10908 : 87648926 : while (decl)
10909 : : {
10910 : 87199027 : decl = decl_namespace_context (decl);
10911 : 87199027 : if (DECL_NAMESPACE_STD_P (decl))
10912 : : return true;
10913 : : /* Allow inline namespaces inside of std namespace, e.g. with
10914 : : --enable-symvers=gnu-versioned-namespace std::forward would be
10915 : : actually std::_8::forward. */
10916 : 41919654 : if (!DECL_NAMESPACE_INLINE_P (decl))
10917 : : return false;
10918 : 12276596 : decl = CP_DECL_CONTEXT (decl);
10919 : : }
10920 : : return false;
10921 : : }
10922 : :
10923 : : /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10924 : :
10925 : : static bool
10926 : 9865398 : is_std_forward_p (tree fn)
10927 : : {
10928 : : /* std::forward only takes one argument. */
10929 : 9865398 : if (call_expr_nargs (fn) != 1)
10930 : : return false;
10931 : :
10932 : 4698158 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10933 : 4698158 : if (!decl_in_std_namespace_p (fndecl))
10934 : : return false;
10935 : :
10936 : 1650696 : tree name = DECL_NAME (fndecl);
10937 : 1650696 : return name && id_equal (name, "forward");
10938 : : }
10939 : :
10940 : : /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10941 : :
10942 : : static bool
10943 : 9872922 : is_std_move_p (tree fn)
10944 : : {
10945 : : /* std::move only takes one argument. */
10946 : 9872922 : if (call_expr_nargs (fn) != 1)
10947 : : return false;
10948 : :
10949 : 4704910 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10950 : 4704910 : if (!decl_in_std_namespace_p (fndecl))
10951 : : return false;
10952 : :
10953 : 1656966 : tree name = DECL_NAME (fndecl);
10954 : 1656966 : return name && id_equal (name, "move");
10955 : : }
10956 : :
10957 : : /* Returns true if RETVAL is a good candidate for the NRVO as per
10958 : : [class.copy.elision]. FUNCTYPE is the type the function is declared
10959 : : to return. */
10960 : :
10961 : : static bool
10962 : 45658473 : can_do_nrvo_p (tree retval, tree functype)
10963 : : {
10964 : 45658473 : if (functype == error_mark_node)
10965 : : return false;
10966 : 45658432 : if (retval)
10967 : 44139417 : STRIP_ANY_LOCATION_WRAPPER (retval);
10968 : 45658432 : tree result = DECL_RESULT (current_function_decl);
10969 : 45658432 : return (retval != NULL_TREE
10970 : 44139417 : && !processing_template_decl
10971 : : /* Must be a local, automatic variable. */
10972 : 36870972 : && VAR_P (retval)
10973 : 3306608 : && DECL_CONTEXT (retval) == current_function_decl
10974 : 2590137 : && !TREE_STATIC (retval)
10975 : : /* And not a lambda or anonymous union proxy. */
10976 : 2587430 : && !DECL_HAS_VALUE_EXPR_P (retval)
10977 : 2587185 : && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10978 : : /* The cv-unqualified type of the returned value must be the
10979 : : same as the cv-unqualified return type of the
10980 : : function. */
10981 : 2587070 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10982 : : TYPE_MAIN_VARIANT (functype))
10983 : : /* And the returned value must be non-volatile. */
10984 : 48234366 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
10985 : : }
10986 : :
10987 : : /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
10988 : : would otherwise return in memory. */
10989 : :
10990 : : static bool
10991 : 45658114 : want_nrvo_p (tree retval, tree functype)
10992 : : {
10993 : 45658114 : return (can_do_nrvo_p (retval, functype)
10994 : 45658114 : && aggregate_value_p (functype, current_function_decl));
10995 : : }
10996 : :
10997 : : /* Like can_do_nrvo_p, but we check if we're trying to move a class
10998 : : prvalue. */
10999 : :
11000 : : static bool
11001 : 1668 : can_elide_copy_prvalue_p (tree retval, tree functype)
11002 : : {
11003 : 1668 : if (functype == error_mark_node)
11004 : : return false;
11005 : 1668 : if (retval)
11006 : 1668 : STRIP_ANY_LOCATION_WRAPPER (retval);
11007 : 1668 : return (retval != NULL_TREE
11008 : 1668 : && !glvalue_p (retval)
11009 : 135 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
11010 : : TYPE_MAIN_VARIANT (functype))
11011 : 1776 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
11012 : : }
11013 : :
11014 : : /* If we should treat RETVAL, an expression being returned, as if it were
11015 : : designated by an rvalue, returns it adjusted accordingly; otherwise, returns
11016 : : NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
11017 : : context (rather than throw). */
11018 : :
11019 : : tree
11020 : 8854039 : treat_lvalue_as_rvalue_p (tree expr, bool return_p)
11021 : : {
11022 : 8854039 : if (cxx_dialect == cxx98)
11023 : : return NULL_TREE;
11024 : :
11025 : 8846280 : tree retval = expr;
11026 : 8846280 : STRIP_ANY_LOCATION_WRAPPER (retval);
11027 : 8846280 : if (REFERENCE_REF_P (retval))
11028 : 624640 : retval = TREE_OPERAND (retval, 0);
11029 : :
11030 : : /* An implicitly movable entity is a variable of automatic storage duration
11031 : : that is either a non-volatile object or (C++20) an rvalue reference to a
11032 : : non-volatile object type. */
11033 : 8846280 : if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
11034 : 7921308 : || TREE_CODE (retval) == PARM_DECL)
11035 : 1256855 : && !TREE_STATIC (retval)
11036 : 1220420 : && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
11037 : 1220415 : && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
11038 : 107436 : || (cxx_dialect >= cxx20
11039 : 89070 : && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
11040 : 7732729 : return NULL_TREE;
11041 : :
11042 : : /* If the expression in a return or co_return statement is a (possibly
11043 : : parenthesized) id-expression that names an implicitly movable entity
11044 : : declared in the body or parameter-declaration-clause of the innermost
11045 : : enclosing function or lambda-expression, */
11046 : 1113551 : if (return_p)
11047 : : {
11048 : 1103701 : if (DECL_CONTEXT (retval) != current_function_decl)
11049 : : return NULL_TREE;
11050 : 1103635 : expr = move (expr);
11051 : 1103635 : if (expr == error_mark_node)
11052 : : return NULL_TREE;
11053 : 1103633 : return set_implicit_rvalue_p (expr);
11054 : : }
11055 : :
11056 : : /* if the id-expression (possibly parenthesized) is the operand of
11057 : : a throw-expression, and names an implicitly movable entity that belongs
11058 : : to a scope that does not contain the compound-statement of the innermost
11059 : : lambda-expression, try-block, or function-try-block (if any) whose
11060 : : compound-statement or ctor-initializer contains the throw-expression. */
11061 : :
11062 : : /* C++20 added move on throw of parms. */
11063 : 9850 : if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
11064 : : return NULL_TREE;
11065 : :
11066 : : /* We don't check for lambda-expression here, because we should not get past
11067 : : the DECL_HAS_VALUE_EXPR_P check above. */
11068 : 2493 : for (cp_binding_level *b = current_binding_level;
11069 : 2584 : b->kind != sk_namespace; b = b->level_chain)
11070 : : {
11071 : 2586 : for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
11072 : 100 : if (decl == retval)
11073 : 88 : return set_implicit_rvalue_p (move (expr));
11074 : 2486 : if (b->kind == sk_try)
11075 : : return NULL_TREE;
11076 : : }
11077 : :
11078 : 10 : return set_implicit_rvalue_p (move (expr));
11079 : : }
11080 : :
11081 : : /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
11082 : : is true). EXPR is the std::move expression; TYPE is the type of the object
11083 : : being initialized. */
11084 : :
11085 : : void
11086 : 130075281 : maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
11087 : : {
11088 : 130075281 : if (!(warn_pessimizing_move || warn_redundant_move))
11089 : : return;
11090 : :
11091 : 4430997 : const location_t loc = cp_expr_loc_or_input_loc (expr);
11092 : :
11093 : : /* C++98 doesn't know move. */
11094 : 4430997 : if (cxx_dialect < cxx11)
11095 : : return;
11096 : :
11097 : : /* Wait until instantiation time, since we can't gauge if we should do
11098 : : the NRVO until then. */
11099 : 4368595 : if (processing_template_decl)
11100 : : return;
11101 : :
11102 : : /* This is only interesting for class types. */
11103 : 4286844 : if (!CLASS_TYPE_P (type))
11104 : : return;
11105 : :
11106 : 96335 : bool wrapped_p = false;
11107 : : /* A a = std::move (A()); */
11108 : 96335 : if (TREE_CODE (expr) == TREE_LIST)
11109 : : {
11110 : 7816 : if (list_length (expr) == 1)
11111 : : {
11112 : 5144 : expr = TREE_VALUE (expr);
11113 : 5144 : wrapped_p = true;
11114 : : }
11115 : : else
11116 : : return;
11117 : : }
11118 : : /* A a = {std::move (A())};
11119 : : A a{std::move (A())}; */
11120 : 88519 : else if (TREE_CODE (expr) == CONSTRUCTOR)
11121 : : {
11122 : 6638 : if (CONSTRUCTOR_NELTS (expr) == 1)
11123 : : {
11124 : 804 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
11125 : 804 : wrapped_p = true;
11126 : : }
11127 : : else
11128 : : return;
11129 : : }
11130 : :
11131 : : /* First, check if this is a call to std::move. */
11132 : 6286 : if (!REFERENCE_REF_P (expr)
11133 : 92629 : || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
11134 : : return;
11135 : 2884 : tree fn = TREE_OPERAND (expr, 0);
11136 : 2884 : if (!is_std_move_p (fn))
11137 : : return;
11138 : 2304 : tree arg = CALL_EXPR_ARG (fn, 0);
11139 : 2304 : if (TREE_CODE (arg) != NOP_EXPR)
11140 : : return;
11141 : : /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
11142 : : and implicitly-movable warnings. */
11143 : 2304 : if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
11144 : : {
11145 : 1668 : arg = TREE_OPERAND (arg, 0);
11146 : 1668 : arg = TREE_OPERAND (arg, 0);
11147 : 1668 : arg = convert_from_reference (arg);
11148 : 1668 : if (can_elide_copy_prvalue_p (arg, type))
11149 : : {
11150 : 108 : auto_diagnostic_group d;
11151 : 108 : if (warning_at (loc, OPT_Wpessimizing_move,
11152 : : "moving a temporary object prevents copy elision"))
11153 : 108 : inform (loc, "remove %<std::move%> call");
11154 : 108 : }
11155 : : /* The rest of the warnings is only relevant for when we are returning
11156 : : from a function. */
11157 : 1668 : if (!return_p)
11158 : : return;
11159 : :
11160 : 359 : tree moved;
11161 : : /* Warn if we could do copy elision were it not for the move. */
11162 : 359 : if (can_do_nrvo_p (arg, type))
11163 : : {
11164 : 48 : auto_diagnostic_group d;
11165 : 48 : if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
11166 : 48 : && warning_at (loc, OPT_Wpessimizing_move,
11167 : : "moving a local object in a return statement "
11168 : : "prevents copy elision"))
11169 : 42 : inform (loc, "remove %<std::move%> call");
11170 : 48 : }
11171 : : /* Warn if the move is redundant. It is redundant when we would
11172 : : do maybe-rvalue overload resolution even without std::move. */
11173 : 311 : else if (warn_redundant_move
11174 : : /* This doesn't apply for return {std::move (t)};. */
11175 : 200 : && !wrapped_p
11176 : 197 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11177 : 407 : && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
11178 : : {
11179 : : /* Make sure that overload resolution would actually succeed
11180 : : if we removed the std::move call. */
11181 : 54 : tree t = convert_for_initialization (NULL_TREE, type,
11182 : : moved,
11183 : : (LOOKUP_NORMAL
11184 : : | LOOKUP_ONLYCONVERTING),
11185 : : ICR_RETURN, NULL_TREE, 0,
11186 : : tf_none);
11187 : : /* If this worked, implicit rvalue would work, so the call to
11188 : : std::move is redundant. */
11189 : 54 : if (t != error_mark_node)
11190 : : {
11191 : 54 : auto_diagnostic_group d;
11192 : 54 : if (warning_at (loc, OPT_Wredundant_move,
11193 : : "redundant move in return statement"))
11194 : 54 : inform (loc, "remove %<std::move%> call");
11195 : 54 : }
11196 : : }
11197 : : }
11198 : : /* Also try to warn about redundant std::move in code such as
11199 : : T f (const T& t)
11200 : : {
11201 : : return std::move(t);
11202 : : }
11203 : : for which EXPR will be something like
11204 : : *std::move<const T&> ((const struct T &) (const struct T *) t)
11205 : : and where the std::move does nothing if T does not have a T(const T&&)
11206 : : constructor, because the argument is const. It will not use T(T&&)
11207 : : because that would mean losing the const. */
11208 : 636 : else if (warn_redundant_move
11209 : 493 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11210 : 64 : && TYPE_REF_P (TREE_TYPE (arg))
11211 : 700 : && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
11212 : : {
11213 : 18 : tree rtype = TREE_TYPE (TREE_TYPE (arg));
11214 : 18 : if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
11215 : 9 : return;
11216 : : /* Check for the unlikely case there's T(const T&&) (we don't care if
11217 : : it's deleted). */
11218 : 54 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
11219 : 30 : if (move_fn_p (fn))
11220 : : {
11221 : 15 : tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11222 : 15 : if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
11223 : 6 : return;
11224 : : }
11225 : 9 : auto_diagnostic_group d;
11226 : 18 : if (return_p
11227 : 9 : ? warning_at (loc, OPT_Wredundant_move,
11228 : : "redundant move in return statement")
11229 : 9 : : warning_at (loc, OPT_Wredundant_move,
11230 : : "redundant move in initialization"))
11231 : 9 : inform (loc, "remove %<std::move%> call");
11232 : 9 : }
11233 : : }
11234 : :
11235 : : /* Check that returning RETVAL from the current function is valid.
11236 : : Return an expression explicitly showing all conversions required to
11237 : : change RETVAL into the function return type, and to assign it to
11238 : : the DECL_RESULT for the function. Set *NO_WARNING to true if
11239 : : code reaches end of non-void function warning shouldn't be issued
11240 : : on this RETURN_EXPR. Set *DANGLING to true if code returns the
11241 : : address of a local variable. */
11242 : :
11243 : : tree
11244 : 106212282 : check_return_expr (tree retval, bool *no_warning, bool *dangling)
11245 : : {
11246 : 106212282 : tree result;
11247 : : /* The type actually returned by the function. */
11248 : 106212282 : tree valtype;
11249 : : /* The type the function is declared to return, or void if
11250 : : the declared type is incomplete. */
11251 : 106212282 : tree functype;
11252 : 106212282 : int fn_returns_value_p;
11253 : 106212282 : location_t loc = cp_expr_loc_or_input_loc (retval);
11254 : :
11255 : 106212282 : *no_warning = false;
11256 : 106212282 : *dangling = false;
11257 : :
11258 : : /* A `volatile' function is one that isn't supposed to return, ever.
11259 : : (This is a G++ extension, used to get better code for functions
11260 : : that call the `volatile' function.) */
11261 : 106212282 : if (TREE_THIS_VOLATILE (current_function_decl))
11262 : 12 : warning (0, "function declared %<noreturn%> has a %<return%> statement");
11263 : :
11264 : : /* Check for various simple errors. */
11265 : 212424564 : if (DECL_DESTRUCTOR_P (current_function_decl))
11266 : : {
11267 : 3387 : if (retval)
11268 : 3 : error_at (loc, "returning a value from a destructor");
11269 : :
11270 : 3387 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11271 : 0 : retval = current_class_ptr;
11272 : : else
11273 : : return NULL_TREE;
11274 : : }
11275 : 106208895 : else if (DECL_CONSTRUCTOR_P (current_function_decl))
11276 : : {
11277 : 56840 : if (in_function_try_handler)
11278 : : /* If a return statement appears in a handler of the
11279 : : function-try-block of a constructor, the program is ill-formed. */
11280 : 0 : error ("cannot return from a handler of a function-try-block of a constructor");
11281 : 56840 : else if (retval)
11282 : : /* You can't return a value from a constructor. */
11283 : 3 : error_at (loc, "returning a value from a constructor");
11284 : :
11285 : 56840 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11286 : 0 : retval = current_class_ptr;
11287 : : else
11288 : : return NULL_TREE;
11289 : : }
11290 : :
11291 : 106152055 : const tree saved_retval = retval;
11292 : :
11293 : 106152055 : if (processing_template_decl)
11294 : : {
11295 : 68597300 : current_function_returns_value = 1;
11296 : :
11297 : 68597300 : if (check_for_bare_parameter_packs (retval))
11298 : 13 : return error_mark_node;
11299 : :
11300 : : /* If one of the types might be void, we can't tell whether we're
11301 : : returning a value. */
11302 : 68597287 : if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11303 : 26824078 : && !FNDECL_USED_AUTO (current_function_decl))
11304 : 41773218 : || (retval != NULL_TREE
11305 : 40683056 : && (TREE_TYPE (retval) == NULL_TREE
11306 : 28460994 : || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11307 : 46485522 : goto dependent;
11308 : : }
11309 : :
11310 : 59666520 : functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11311 : :
11312 : : /* Deduce auto return type from a return statement. */
11313 : 59666520 : if (FNDECL_USED_AUTO (current_function_decl))
11314 : : {
11315 : 968153 : tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11316 : 968153 : tree auto_node;
11317 : 968153 : tree type;
11318 : :
11319 : 968153 : if (!retval && !is_auto (pattern))
11320 : : {
11321 : : /* Give a helpful error message. */
11322 : 3 : auto_diagnostic_group d;
11323 : 3 : error ("return-statement with no value, in function returning %qT",
11324 : : pattern);
11325 : 3 : inform (input_location, "only plain %<auto%> return type can be "
11326 : : "deduced to %<void%>");
11327 : 3 : type = error_mark_node;
11328 : 3 : }
11329 : 968150 : else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11330 : : {
11331 : 6 : error ("returning initializer list");
11332 : 6 : type = error_mark_node;
11333 : : }
11334 : : else
11335 : : {
11336 : 968144 : if (!retval)
11337 : 5359 : retval = void_node;
11338 : 968144 : auto_node = type_uses_auto (pattern);
11339 : 968144 : type = do_auto_deduction (pattern, retval, auto_node,
11340 : : tf_warning_or_error, adc_return_type);
11341 : : }
11342 : :
11343 : 968153 : if (type == error_mark_node)
11344 : : /* Leave it. */;
11345 : 967953 : else if (functype == pattern)
11346 : 614557 : apply_deduced_return_type (current_function_decl, type);
11347 : 353396 : else if (!same_type_p (type, functype))
11348 : : {
11349 : 24 : if (LAMBDA_FUNCTION_P (current_function_decl))
11350 : 6 : error_at (loc, "inconsistent types %qT and %qT deduced for "
11351 : : "lambda return type", functype, type);
11352 : : else
11353 : 12 : error_at (loc, "inconsistent deduction for auto return type: "
11354 : : "%qT and then %qT", functype, type);
11355 : : }
11356 : : functype = type;
11357 : : }
11358 : :
11359 : 59666520 : result = DECL_RESULT (current_function_decl);
11360 : 59666520 : valtype = TREE_TYPE (result);
11361 : 59666520 : gcc_assert (valtype != NULL_TREE);
11362 : 59666520 : fn_returns_value_p = !VOID_TYPE_P (valtype);
11363 : :
11364 : : /* Check for a return statement with no return value in a function
11365 : : that's supposed to return a value. */
11366 : 59666520 : if (!retval && fn_returns_value_p)
11367 : : {
11368 : 35 : if (functype != error_mark_node)
11369 : 32 : permerror (input_location, "return-statement with no value, in "
11370 : : "function returning %qT", valtype);
11371 : : /* Remember that this function did return. */
11372 : 35 : current_function_returns_value = 1;
11373 : : /* But suppress NRV .*/
11374 : 35 : current_function_return_value = error_mark_node;
11375 : : /* And signal caller that TREE_NO_WARNING should be set on the
11376 : : RETURN_EXPR to avoid control reaches end of non-void function
11377 : : warnings in tree-cfg.cc. */
11378 : 35 : *no_warning = true;
11379 : : }
11380 : : /* Check for a return statement with a value in a function that
11381 : : isn't supposed to return a value. */
11382 : 59666485 : else if (retval && !fn_returns_value_p)
11383 : : {
11384 : 264392 : if (VOID_TYPE_P (TREE_TYPE (retval)))
11385 : : /* You can return a `void' value from a function of `void'
11386 : : type. In that case, we have to evaluate the expression for
11387 : : its side-effects. */
11388 : 264355 : finish_expr_stmt (retval);
11389 : 37 : else if (retval != error_mark_node)
11390 : 34 : permerror (loc, "return-statement with a value, in function "
11391 : : "returning %qT", valtype);
11392 : 264392 : current_function_returns_null = 1;
11393 : :
11394 : : /* There's really no value to return, after all. */
11395 : 264392 : return NULL_TREE;
11396 : : }
11397 : 59402093 : else if (!retval)
11398 : : /* Remember that this function can sometimes return without a
11399 : : value. */
11400 : 1518998 : current_function_returns_null = 1;
11401 : : else
11402 : : /* Remember that this function did return a value. */
11403 : 57883095 : current_function_returns_value = 1;
11404 : :
11405 : : /* Check for erroneous operands -- but after giving ourselves a
11406 : : chance to provide an error about returning a value from a void
11407 : : function. */
11408 : 59402128 : if (error_operand_p (retval))
11409 : : {
11410 : 964 : current_function_return_value = error_mark_node;
11411 : 964 : return error_mark_node;
11412 : : }
11413 : :
11414 : : /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11415 : 118802328 : if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11416 : 30724 : && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11417 : 3130 : && ! flag_check_new
11418 : 59404285 : && retval && null_ptr_cst_p (retval))
11419 : 24 : warning (0, "%<operator new%> must not return NULL unless it is "
11420 : : "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11421 : :
11422 : : /* Effective C++ rule 15. See also start_function. */
11423 : 59401164 : if (warn_ecpp
11424 : 42 : && DECL_NAME (current_function_decl) == assign_op_identifier
11425 : 59401194 : && !type_dependent_expression_p (retval))
11426 : : {
11427 : 27 : bool warn = true;
11428 : :
11429 : : /* The function return type must be a reference to the current
11430 : : class. */
11431 : 27 : if (TYPE_REF_P (valtype)
11432 : 45 : && same_type_ignoring_top_level_qualifiers_p
11433 : 18 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11434 : : {
11435 : : /* Returning '*this' is obviously OK. */
11436 : 18 : if (retval == current_class_ref)
11437 : : warn = false;
11438 : : /* If we are calling a function whose return type is the same of
11439 : : the current class reference, it is ok. */
11440 : 6 : else if (INDIRECT_REF_P (retval)
11441 : 6 : && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11442 : : warn = false;
11443 : : }
11444 : :
11445 : : if (warn)
11446 : 9 : warning_at (loc, OPT_Weffc__,
11447 : : "%<operator=%> should return a reference to %<*this%>");
11448 : : }
11449 : :
11450 : 59401164 : if (dependent_type_p (functype)
11451 : 59401164 : || type_dependent_expression_p (retval))
11452 : : {
11453 : 60228572 : dependent:
11454 : : /* We should not have changed the return value. */
11455 : 60228572 : gcc_assert (retval == saved_retval);
11456 : : /* We don't know if this is an lvalue or rvalue use, but
11457 : : either way we can mark it as read. */
11458 : 60228572 : mark_exp_read (retval);
11459 : 60228572 : return retval;
11460 : : }
11461 : :
11462 : : /* The fabled Named Return Value optimization, as per [class.copy]/15:
11463 : :
11464 : : [...] For a function with a class return type, if the expression
11465 : : in the return statement is the name of a local object, and the cv-
11466 : : unqualified type of the local object is the same as the function
11467 : : return type, an implementation is permitted to omit creating the tem-
11468 : : porary object to hold the function return value [...]
11469 : :
11470 : : So, if this is a value-returning function that always returns the same
11471 : : local variable, remember it.
11472 : :
11473 : : We choose the first suitable variable even if the function sometimes
11474 : : returns something else, but only if the variable is out of scope at the
11475 : : other return sites, or else we run the risk of clobbering the variable we
11476 : : chose if the other returned expression uses the chosen variable somehow.
11477 : :
11478 : : We don't currently do this if the first return is a non-variable, as it
11479 : : would be complicated to determine whether an NRV selected later was in
11480 : : scope at the point of the earlier return. We also don't currently support
11481 : : multiple variables with non-overlapping scopes (53637).
11482 : :
11483 : : See finish_function and finalize_nrv for the rest of this optimization. */
11484 : 45658114 : tree bare_retval = NULL_TREE;
11485 : 45658114 : if (retval)
11486 : : {
11487 : 44139096 : retval = maybe_undo_parenthesized_ref (retval);
11488 : 44139096 : bare_retval = tree_strip_any_location_wrapper (retval);
11489 : : }
11490 : :
11491 : 45658114 : bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11492 : 45658114 : if (fn_returns_value_p && flag_elide_constructors
11493 : 44139075 : && current_function_return_value != bare_retval)
11494 : : {
11495 : 44127528 : if (named_return_value_okay_p
11496 : 222785 : && current_function_return_value == NULL_TREE)
11497 : 181626 : current_function_return_value = bare_retval;
11498 : 43945902 : else if (current_function_return_value
11499 : 8290768 : && VAR_P (current_function_return_value)
11500 : 171 : && DECL_NAME (current_function_return_value)
11501 : 43946073 : && !decl_in_scope_p (current_function_return_value))
11502 : : {
11503 : : /* The earlier NRV is out of scope at this point, so it's safe to
11504 : 102 : leave it alone; the current return can't refer to it. */;
11505 : 102 : if (named_return_value_okay_p
11506 : 102 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11507 : : {
11508 : 8 : warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11509 : : bare_retval);
11510 : 8 : suppress_warning (current_function_decl, OPT_Wnrvo);
11511 : : }
11512 : : }
11513 : : else
11514 : : {
11515 : 43945800 : if ((named_return_value_okay_p
11516 : 43904651 : || (current_function_return_value
11517 : 8249517 : && current_function_return_value != error_mark_node))
11518 : 43945847 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11519 : : {
11520 : 41133 : warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11521 : : current_function_decl);
11522 : 41133 : suppress_warning (current_function_decl, OPT_Wnrvo);
11523 : : }
11524 : 43945800 : current_function_return_value = error_mark_node;
11525 : : }
11526 : : }
11527 : :
11528 : : /* We don't need to do any conversions when there's nothing being
11529 : : returned. */
11530 : 45658114 : if (!retval)
11531 : : return NULL_TREE;
11532 : :
11533 : 44139096 : if (!named_return_value_okay_p)
11534 : 43904764 : maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11535 : :
11536 : : /* Do any required conversions. */
11537 : 88278192 : if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11538 : : /* No conversions are required. */
11539 : : ;
11540 : : else
11541 : : {
11542 : 44139096 : int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11543 : :
11544 : : /* The functype's return type will have been set to void, if it
11545 : : was an incomplete type. Just treat this as 'return;' */
11546 : 44139096 : if (VOID_TYPE_P (functype))
11547 : 6 : return error_mark_node;
11548 : :
11549 : : /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11550 : : treated as an rvalue for the purposes of overload resolution to
11551 : : favor move constructors over copy constructors.
11552 : :
11553 : : Note that these conditions are similar to, but not as strict as,
11554 : : the conditions for the named return value optimization. */
11555 : 44139090 : bool converted = false;
11556 : 44139090 : tree moved;
11557 : : /* Until C++23, this was only interesting for class type, but in C++23,
11558 : : we should do the below when we're converting from/to a class/reference
11559 : : (a non-scalar type). */
11560 : 44139090 : if ((cxx_dialect < cxx23
11561 : 4617880 : ? CLASS_TYPE_P (functype)
11562 : 11509773 : : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11563 : 52139487 : && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11564 : : /* In C++20 and earlier we treat the return value as an rvalue
11565 : : that can bind to lvalue refs. In C++23, such an expression is just
11566 : : an xvalue (see reference_binding). */
11567 : : retval = moved;
11568 : :
11569 : : /* The call in a (lambda) thunk needs no conversions. */
11570 : 44139090 : if (TREE_CODE (retval) == CALL_EXPR
11571 : 44139090 : && call_from_lambda_thunk_p (retval))
11572 : : converted = true;
11573 : :
11574 : : /* Don't check copy-initialization for NRV in a coroutine ramp; we
11575 : : implement this case as NRV, but it's specified as directly
11576 : : initializing the return value from get_return_object(). */
11577 : 44139090 : if (DECL_RAMP_P (current_function_decl) && named_return_value_okay_p)
11578 : : converted = true;
11579 : :
11580 : : /* First convert the value to the function's return type, then
11581 : : to the type of return value's location to handle the
11582 : : case that functype is smaller than the valtype. */
11583 : 44138841 : if (!converted)
11584 : 44117840 : retval = convert_for_initialization
11585 : 44117840 : (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11586 : : tf_warning_or_error);
11587 : 44139090 : retval = convert (valtype, retval);
11588 : :
11589 : : /* If the conversion failed, treat this just like `return;'. */
11590 : 44139090 : if (retval == error_mark_node)
11591 : : {
11592 : : /* And suppress NRV. */
11593 : 189 : current_function_return_value = error_mark_node;
11594 : 189 : return retval;
11595 : : }
11596 : : /* We can't initialize a register from a AGGR_INIT_EXPR. */
11597 : 44138901 : else if (! cfun->returns_struct
11598 : 42557607 : && TREE_CODE (retval) == TARGET_EXPR
11599 : 48756678 : && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == AGGR_INIT_EXPR)
11600 : 794369 : retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11601 : 794369 : TARGET_EXPR_SLOT (retval));
11602 : 43344532 : else if (!processing_template_decl
11603 : 36076099 : && maybe_warn_about_returning_address_of_local (retval, loc)
11604 : 43344713 : && INDIRECT_TYPE_P (valtype))
11605 : 166 : *dangling = true;
11606 : : }
11607 : :
11608 : : /* A naive attempt to reduce the number of -Wdangling-reference false
11609 : : positives: if we know that this function can return a variable with
11610 : : static storage duration rather than one of its parameters, suppress
11611 : : the warning. */
11612 : 44138901 : if (warn_dangling_reference
11613 : 382998 : && TYPE_REF_P (functype)
11614 : 25510 : && bare_retval
11615 : 25510 : && VAR_P (bare_retval)
11616 : 50 : && TREE_STATIC (bare_retval))
11617 : 50 : suppress_warning (current_function_decl, OPT_Wdangling_reference);
11618 : :
11619 : 44138901 : if (processing_template_decl)
11620 : : return saved_retval;
11621 : :
11622 : : /* Actually copy the value returned into the appropriate location. */
11623 : 36870468 : if (retval && retval != result)
11624 : 36870468 : retval = cp_build_init_expr (result, retval);
11625 : :
11626 : 36870468 : if (current_function_return_value == bare_retval)
11627 : 193167 : INIT_EXPR_NRV_P (retval) = true;
11628 : :
11629 : 36870468 : if (tree set = maybe_set_retval_sentinel ())
11630 : 139079 : retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11631 : :
11632 : : return retval;
11633 : : }
11634 : :
11635 : :
11636 : : /* Returns nonzero if the pointer-type FROM can be converted to the
11637 : : pointer-type TO via a qualification conversion. If CONSTP is -1,
11638 : : then we return nonzero if the pointers are similar, and the
11639 : : cv-qualification signature of FROM is a proper subset of that of TO.
11640 : :
11641 : : If CONSTP is positive, then all outer pointers have been
11642 : : const-qualified. */
11643 : :
11644 : : static bool
11645 : 132212049 : comp_ptr_ttypes_real (tree to, tree from, int constp)
11646 : : {
11647 : 132212049 : bool to_more_cv_qualified = false;
11648 : 132212049 : bool is_opaque_pointer = false;
11649 : :
11650 : 555697 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11651 : : {
11652 : 132767746 : if (TREE_CODE (to) != TREE_CODE (from))
11653 : : return false;
11654 : :
11655 : 85609798 : if (TREE_CODE (from) == OFFSET_TYPE
11656 : 85609798 : && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11657 : : TYPE_OFFSET_BASETYPE (to)))
11658 : : return false;
11659 : :
11660 : : /* Const and volatile mean something different for function and
11661 : : array types, so the usual checks are not appropriate. We'll
11662 : : check the array type elements in further iterations. */
11663 : 85609798 : if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11664 : : {
11665 : 85377523 : if (!at_least_as_qualified_p (to, from))
11666 : : return false;
11667 : :
11668 : 75479461 : if (!at_least_as_qualified_p (from, to))
11669 : : {
11670 : 52558414 : if (constp == 0)
11671 : : return false;
11672 : : to_more_cv_qualified = true;
11673 : : }
11674 : :
11675 : 75479199 : if (constp > 0)
11676 : 75474550 : constp &= TYPE_READONLY (to);
11677 : : }
11678 : :
11679 : 75711474 : if (VECTOR_TYPE_P (to))
11680 : 7120 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11681 : :
11682 : : /* P0388R4 allows a conversion from int[N] to int[] but not the
11683 : : other way round. When both arrays have bounds but they do
11684 : : not match, then no conversion is possible. */
11685 : 75711474 : if (TREE_CODE (to) == ARRAY_TYPE
11686 : 75711474 : && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11687 : : return false;
11688 : :
11689 : 75711200 : if (!TYPE_PTR_P (to)
11690 : : && !TYPE_PTRDATAMEM_P (to)
11691 : : /* CWG 330 says we need to look through arrays. */
11692 : : && TREE_CODE (to) != ARRAY_TYPE)
11693 : 75155503 : return ((constp >= 0 || to_more_cv_qualified)
11694 : 75155503 : && (is_opaque_pointer
11695 : 75154937 : || same_type_ignoring_top_level_qualifiers_p (to, from)));
11696 : : }
11697 : : }
11698 : :
11699 : : /* When comparing, say, char ** to char const **, this function takes
11700 : : the 'char *' and 'char const *'. Do not pass non-pointer/reference
11701 : : types to this function. */
11702 : :
11703 : : int
11704 : 132211373 : comp_ptr_ttypes (tree to, tree from)
11705 : : {
11706 : 132211373 : return comp_ptr_ttypes_real (to, from, 1);
11707 : : }
11708 : :
11709 : : /* Returns true iff FNTYPE is a non-class type that involves
11710 : : error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11711 : : if a parameter type is ill-formed. */
11712 : :
11713 : : bool
11714 : 651971 : error_type_p (const_tree type)
11715 : : {
11716 : 694421 : tree t;
11717 : :
11718 : 694421 : switch (TREE_CODE (type))
11719 : : {
11720 : : case ERROR_MARK:
11721 : : return true;
11722 : :
11723 : 42361 : case POINTER_TYPE:
11724 : 42361 : case REFERENCE_TYPE:
11725 : 42361 : case OFFSET_TYPE:
11726 : 42361 : return error_type_p (TREE_TYPE (type));
11727 : :
11728 : 2697 : case FUNCTION_TYPE:
11729 : 2697 : case METHOD_TYPE:
11730 : 2697 : if (error_type_p (TREE_TYPE (type)))
11731 : : return true;
11732 : 7207 : for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11733 : 4516 : if (error_type_p (TREE_VALUE (t)))
11734 : : return true;
11735 : : return false;
11736 : :
11737 : 201387 : case RECORD_TYPE:
11738 : 201387 : if (TYPE_PTRMEMFUNC_P (type))
11739 : 89 : return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11740 : : return false;
11741 : :
11742 : : default:
11743 : : return false;
11744 : : }
11745 : : }
11746 : :
11747 : : /* Returns true if to and from are (possibly multi-level) pointers to the same
11748 : : type or inheritance-related types, regardless of cv-quals. */
11749 : :
11750 : : bool
11751 : 93004440 : ptr_reasonably_similar (const_tree to, const_tree from)
11752 : : {
11753 : 1020 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11754 : : {
11755 : : /* Any target type is similar enough to void. */
11756 : 93005460 : if (VOID_TYPE_P (to))
11757 : 1082 : return !error_type_p (from);
11758 : 93004378 : if (VOID_TYPE_P (from))
11759 : 641752 : return !error_type_p (to);
11760 : :
11761 : 92362626 : if (TREE_CODE (to) != TREE_CODE (from))
11762 : : return false;
11763 : :
11764 : 45696921 : if (TREE_CODE (from) == OFFSET_TYPE
11765 : 45696921 : && comptypes (TYPE_OFFSET_BASETYPE (to),
11766 : 3 : TYPE_OFFSET_BASETYPE (from),
11767 : : COMPARE_BASE | COMPARE_DERIVED))
11768 : 3 : continue;
11769 : :
11770 : 45696915 : if (VECTOR_TYPE_P (to)
11771 : 45696915 : && vector_types_convertible_p (to, from, false))
11772 : : return true;
11773 : :
11774 : 45696831 : if (TREE_CODE (to) == INTEGER_TYPE
11775 : 45696831 : && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11776 : : return true;
11777 : :
11778 : 45579316 : if (TREE_CODE (to) == FUNCTION_TYPE)
11779 : 962 : return !error_type_p (to) && !error_type_p (from);
11780 : :
11781 : 45578354 : if (!TYPE_PTR_P (to))
11782 : : {
11783 : : /* When either type is incomplete avoid DERIVED_FROM_P,
11784 : : which may call complete_type (c++/57942). */
11785 : 45577337 : bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11786 : : return comptypes
11787 : 45577337 : (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11788 : 45577337 : b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11789 : : }
11790 : 1020 : }
11791 : : }
11792 : :
11793 : : /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11794 : : pointer-to-member types) are the same, ignoring cv-qualification at
11795 : : all levels. CB says how we should behave when comparing array bounds. */
11796 : :
11797 : : bool
11798 : 815426 : comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11799 : : {
11800 : 815426 : bool is_opaque_pointer = false;
11801 : :
11802 : 816242 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11803 : : {
11804 : 1631668 : if (TREE_CODE (to) != TREE_CODE (from))
11805 : : return false;
11806 : :
11807 : 1256567 : if (TREE_CODE (from) == OFFSET_TYPE
11808 : 1256529 : && same_type_p (TYPE_OFFSET_BASETYPE (from),
11809 : : TYPE_OFFSET_BASETYPE (to)))
11810 : 38 : continue;
11811 : :
11812 : 1256491 : if (VECTOR_TYPE_P (to))
11813 : 8669 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11814 : :
11815 : 1256491 : if (TREE_CODE (to) == ARRAY_TYPE
11816 : : /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11817 : : we must fail. */
11818 : 1256491 : && !comp_array_types (to, from, cb, /*strict=*/false))
11819 : : return false;
11820 : :
11821 : : /* CWG 330 says we need to look through arrays. */
11822 : 1255810 : if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11823 : 439606 : return (is_opaque_pointer
11824 : 439606 : || same_type_ignoring_top_level_qualifiers_p (to, from));
11825 : : }
11826 : : }
11827 : :
11828 : : /* Returns the type qualifiers for this type, including the qualifiers on the
11829 : : elements for an array type. */
11830 : :
11831 : : int
11832 : 49510386595 : cp_type_quals (const_tree type)
11833 : : {
11834 : 49510386595 : int quals;
11835 : : /* This CONST_CAST is okay because strip_array_types returns its
11836 : : argument unmodified and we assign it to a const_tree. */
11837 : 49510386595 : type = strip_array_types (CONST_CAST_TREE (type));
11838 : 49510386595 : if (type == error_mark_node
11839 : : /* Quals on a FUNCTION_TYPE are memfn quals. */
11840 : 49479219223 : || TREE_CODE (type) == FUNCTION_TYPE)
11841 : : return TYPE_UNQUALIFIED;
11842 : 49433569449 : quals = TYPE_QUALS (type);
11843 : : /* METHOD and REFERENCE_TYPEs should never have quals. */
11844 : 49433569449 : gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11845 : : && !TYPE_REF_P (type))
11846 : : || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11847 : : == TYPE_UNQUALIFIED));
11848 : : return quals;
11849 : : }
11850 : :
11851 : : /* Returns the function-ref-qualifier for TYPE */
11852 : :
11853 : : cp_ref_qualifier
11854 : 2770350861 : type_memfn_rqual (const_tree type)
11855 : : {
11856 : 2770350861 : gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11857 : :
11858 : 2770350861 : if (!FUNCTION_REF_QUALIFIED (type))
11859 : : return REF_QUAL_NONE;
11860 : 32652195 : else if (FUNCTION_RVALUE_QUALIFIED (type))
11861 : : return REF_QUAL_RVALUE;
11862 : : else
11863 : 16376283 : return REF_QUAL_LVALUE;
11864 : : }
11865 : :
11866 : : /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11867 : : METHOD_TYPE. */
11868 : :
11869 : : int
11870 : 1002934479 : type_memfn_quals (const_tree type)
11871 : : {
11872 : 1002934479 : if (TREE_CODE (type) == FUNCTION_TYPE)
11873 : 79618116 : return TYPE_QUALS (type);
11874 : 923316363 : else if (TREE_CODE (type) == METHOD_TYPE)
11875 : 923316363 : return cp_type_quals (class_of_this_parm (type));
11876 : : else
11877 : 0 : gcc_unreachable ();
11878 : : }
11879 : :
11880 : : /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11881 : : MEMFN_QUALS and its ref-qualifier to RQUAL. */
11882 : :
11883 : : tree
11884 : 56182635 : apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11885 : : {
11886 : : /* Could handle METHOD_TYPE here if necessary. */
11887 : 56182635 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11888 : 56182635 : if (TYPE_QUALS (type) == memfn_quals
11889 : 56182635 : && type_memfn_rqual (type) == rqual)
11890 : : return type;
11891 : :
11892 : : /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11893 : : complex. */
11894 : 664990 : tree result = build_qualified_type (type, memfn_quals);
11895 : 664990 : return build_ref_qualified_type (result, rqual);
11896 : : }
11897 : :
11898 : : /* Returns nonzero if TYPE is const or volatile. */
11899 : :
11900 : : bool
11901 : 1021595297 : cv_qualified_p (const_tree type)
11902 : : {
11903 : 1021595297 : int quals = cp_type_quals (type);
11904 : 1021595297 : return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11905 : : }
11906 : :
11907 : : /* Returns nonzero if the TYPE contains a mutable member. */
11908 : :
11909 : : bool
11910 : 820073189 : cp_has_mutable_p (const_tree type)
11911 : : {
11912 : : /* This CONST_CAST is okay because strip_array_types returns its
11913 : : argument unmodified and we assign it to a const_tree. */
11914 : 820073189 : type = strip_array_types (CONST_CAST_TREE(type));
11915 : :
11916 : 820073189 : return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11917 : : }
11918 : :
11919 : : /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11920 : : TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11921 : : approximation. In particular, consider:
11922 : :
11923 : : int f();
11924 : : struct S { int i; };
11925 : : const S s = { f(); }
11926 : :
11927 : : Here, we will make "s" as TREE_READONLY (because it is declared
11928 : : "const") -- only to reverse ourselves upon seeing that the
11929 : : initializer is non-constant. */
11930 : :
11931 : : void
11932 : 874850595 : cp_apply_type_quals_to_decl (int type_quals, tree decl)
11933 : : {
11934 : 874850595 : tree type = TREE_TYPE (decl);
11935 : :
11936 : 874850595 : if (type == error_mark_node)
11937 : : return;
11938 : :
11939 : 874849905 : if (TREE_CODE (decl) == TYPE_DECL)
11940 : : return;
11941 : :
11942 : 781010446 : gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11943 : : && type_quals != TYPE_UNQUALIFIED));
11944 : :
11945 : : /* Avoid setting TREE_READONLY incorrectly. */
11946 : : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11947 : : constructor can produce constant init, so rely on cp_finish_decl to
11948 : : clear TREE_READONLY if the variable has non-constant init. */
11949 : :
11950 : : /* If the type has (or might have) a mutable component, that component
11951 : : might be modified. */
11952 : 781010446 : if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11953 : 70127523 : type_quals &= ~TYPE_QUAL_CONST;
11954 : :
11955 : 781010446 : c_apply_type_quals_to_decl (type_quals, decl);
11956 : : }
11957 : :
11958 : : /* Subroutine of casts_away_constness. Make T1 and T2 point at
11959 : : exemplar types such that casting T1 to T2 is casting away constness
11960 : : if and only if there is no implicit conversion from T1 to T2. */
11961 : :
11962 : : static void
11963 : 1145113 : casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11964 : : {
11965 : 1145113 : int quals1;
11966 : 1145113 : int quals2;
11967 : :
11968 : : /* [expr.const.cast]
11969 : :
11970 : : For multi-level pointer to members and multi-level mixed pointers
11971 : : and pointers to members (conv.qual), the "member" aspect of a
11972 : : pointer to member level is ignored when determining if a const
11973 : : cv-qualifier has been cast away. */
11974 : : /* [expr.const.cast]
11975 : :
11976 : : For two pointer types:
11977 : :
11978 : : X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11979 : : X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11980 : : K is min(N,M)
11981 : :
11982 : : casting from X1 to X2 casts away constness if, for a non-pointer
11983 : : type T there does not exist an implicit conversion (clause
11984 : : _conv_) from:
11985 : :
11986 : : Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11987 : :
11988 : : to
11989 : :
11990 : : Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11991 : 1145113 : if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11992 : 572903 : || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11993 : : {
11994 : 572384 : *t1 = cp_build_qualified_type (void_type_node,
11995 : : cp_type_quals (*t1));
11996 : 572384 : *t2 = cp_build_qualified_type (void_type_node,
11997 : : cp_type_quals (*t2));
11998 : 572384 : return;
11999 : : }
12000 : :
12001 : 572729 : quals1 = cp_type_quals (*t1);
12002 : 572729 : quals2 = cp_type_quals (*t2);
12003 : :
12004 : 572729 : if (TYPE_PTRDATAMEM_P (*t1))
12005 : 0 : *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
12006 : : else
12007 : 572729 : *t1 = TREE_TYPE (*t1);
12008 : 572729 : if (TYPE_PTRDATAMEM_P (*t2))
12009 : 0 : *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
12010 : : else
12011 : 572729 : *t2 = TREE_TYPE (*t2);
12012 : :
12013 : 572729 : casts_away_constness_r (t1, t2, complain);
12014 : 572729 : *t1 = build_pointer_type (*t1);
12015 : 572729 : *t2 = build_pointer_type (*t2);
12016 : 572729 : *t1 = cp_build_qualified_type (*t1, quals1);
12017 : 572729 : *t2 = cp_build_qualified_type (*t2, quals2);
12018 : : }
12019 : :
12020 : : /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
12021 : : constness.
12022 : :
12023 : : ??? This function returns non-zero if casting away qualifiers not
12024 : : just const. We would like to return to the caller exactly which
12025 : : qualifiers are casted away to give more accurate diagnostics.
12026 : : */
12027 : :
12028 : : static bool
12029 : 572456 : casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
12030 : : {
12031 : 572456 : if (TYPE_REF_P (t2))
12032 : : {
12033 : : /* [expr.const.cast]
12034 : :
12035 : : Casting from an lvalue of type T1 to an lvalue of type T2
12036 : : using a reference cast casts away constness if a cast from an
12037 : : rvalue of type "pointer to T1" to the type "pointer to T2"
12038 : : casts away constness. */
12039 : 0 : t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
12040 : 0 : return casts_away_constness (build_pointer_type (t1),
12041 : 0 : build_pointer_type (TREE_TYPE (t2)),
12042 : 0 : complain);
12043 : : }
12044 : :
12045 : 572456 : if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
12046 : : /* [expr.const.cast]
12047 : :
12048 : : Casting from an rvalue of type "pointer to data member of X
12049 : : of type T1" to the type "pointer to data member of Y of type
12050 : : T2" casts away constness if a cast from an rvalue of type
12051 : : "pointer to T1" to the type "pointer to T2" casts away
12052 : : constness. */
12053 : 48 : return casts_away_constness
12054 : 48 : (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
12055 : 48 : build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
12056 : 48 : complain);
12057 : :
12058 : : /* Casting away constness is only something that makes sense for
12059 : : pointer or reference types. */
12060 : 572408 : if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
12061 : : return false;
12062 : :
12063 : : /* Top-level qualifiers don't matter. */
12064 : 572384 : t1 = TYPE_MAIN_VARIANT (t1);
12065 : 572384 : t2 = TYPE_MAIN_VARIANT (t2);
12066 : 572384 : casts_away_constness_r (&t1, &t2, complain);
12067 : 572384 : if (!can_convert (t2, t1, complain))
12068 : : return true;
12069 : :
12070 : : return false;
12071 : : }
12072 : :
12073 : : /* If T is a REFERENCE_TYPE return the type to which T refers.
12074 : : Otherwise, return T itself. */
12075 : :
12076 : : tree
12077 : 3066155712 : non_reference (tree t)
12078 : : {
12079 : 3066155712 : if (t && TYPE_REF_P (t))
12080 : 293193433 : t = TREE_TYPE (t);
12081 : 3066155712 : return t;
12082 : : }
12083 : :
12084 : :
12085 : : /* Return nonzero if REF is an lvalue valid for this language;
12086 : : otherwise, print an error message and return zero. USE says
12087 : : how the lvalue is being used and so selects the error message. */
12088 : :
12089 : : int
12090 : 37798797 : lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
12091 : : {
12092 : 37798797 : cp_lvalue_kind kind = lvalue_kind (ref);
12093 : :
12094 : 37798797 : if (kind == clk_none)
12095 : : {
12096 : 128 : if (complain & tf_error)
12097 : 115 : lvalue_error (cp_expr_loc_or_input_loc (ref), use);
12098 : 128 : return 0;
12099 : : }
12100 : 37798669 : else if (kind & (clk_rvalueref|clk_class))
12101 : : {
12102 : 92 : if (!(complain & tf_error))
12103 : : return 0;
12104 : : /* Make this a permerror because we used to accept it. */
12105 : 18 : permerror (cp_expr_loc_or_input_loc (ref),
12106 : : "using rvalue as lvalue");
12107 : : }
12108 : : return 1;
12109 : : }
12110 : :
12111 : : /* Return true if a user-defined literal operator is a raw operator. */
12112 : :
12113 : : bool
12114 : 120298 : check_raw_literal_operator (const_tree decl)
12115 : : {
12116 : 120298 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12117 : 120298 : tree argtype;
12118 : 120298 : int arity;
12119 : 120298 : bool maybe_raw_p = false;
12120 : :
12121 : : /* Count the number and type of arguments and check for ellipsis. */
12122 : 120298 : for (argtype = argtypes, arity = 0;
12123 : 180464 : argtype && argtype != void_list_node;
12124 : 60166 : ++arity, argtype = TREE_CHAIN (argtype))
12125 : : {
12126 : 60166 : tree t = TREE_VALUE (argtype);
12127 : :
12128 : 60166 : if (same_type_p (t, const_string_type_node))
12129 : 9 : maybe_raw_p = true;
12130 : : }
12131 : 120298 : if (!argtype)
12132 : : return false; /* Found ellipsis. */
12133 : :
12134 : 120298 : if (!maybe_raw_p || arity != 1)
12135 : 120292 : return false;
12136 : :
12137 : : return true;
12138 : : }
12139 : :
12140 : :
12141 : : /* Return true if a user-defined literal operator has one of the allowed
12142 : : argument types. */
12143 : :
12144 : : bool
12145 : 283527 : check_literal_operator_args (const_tree decl,
12146 : : bool *long_long_unsigned_p, bool *long_double_p)
12147 : : {
12148 : 283527 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12149 : :
12150 : 283527 : *long_long_unsigned_p = false;
12151 : 283527 : *long_double_p = false;
12152 : 283527 : if (processing_template_decl || processing_specialization)
12153 : 60273 : return argtypes == void_list_node;
12154 : : else
12155 : : {
12156 : : tree argtype;
12157 : : int arity;
12158 : : int max_arity = 2;
12159 : :
12160 : : /* Count the number and type of arguments and check for ellipsis. */
12161 : 223160 : for (argtype = argtypes, arity = 0;
12162 : 446414 : argtype && argtype != void_list_node;
12163 : 223160 : argtype = TREE_CHAIN (argtype))
12164 : : {
12165 : 223260 : tree t = TREE_VALUE (argtype);
12166 : 223260 : ++arity;
12167 : :
12168 : 223260 : if (TYPE_PTR_P (t))
12169 : : {
12170 : 99200 : bool maybe_raw_p = false;
12171 : 99200 : t = TREE_TYPE (t);
12172 : 99200 : if (cp_type_quals (t) != TYPE_QUAL_CONST)
12173 : : return false;
12174 : 99197 : t = TYPE_MAIN_VARIANT (t);
12175 : 99197 : if ((maybe_raw_p = same_type_p (t, char_type_node))
12176 : 75725 : || same_type_p (t, wchar_type_node)
12177 : 52405 : || same_type_p (t, char8_type_node)
12178 : 46652 : || same_type_p (t, char16_type_node)
12179 : 122523 : || same_type_p (t, char32_type_node))
12180 : : {
12181 : 99194 : argtype = TREE_CHAIN (argtype);
12182 : 99194 : if (!argtype)
12183 : : return false;
12184 : 99194 : t = TREE_VALUE (argtype);
12185 : 99194 : if (maybe_raw_p && argtype == void_list_node)
12186 : : return true;
12187 : 99121 : else if (same_type_p (t, size_type_node))
12188 : : {
12189 : 99115 : ++arity;
12190 : 99115 : continue;
12191 : : }
12192 : : else
12193 : : return false;
12194 : : }
12195 : : }
12196 : 124060 : else if (same_type_p (t, long_long_unsigned_type_node))
12197 : : {
12198 : 34287 : max_arity = 1;
12199 : 34287 : *long_long_unsigned_p = true;
12200 : : }
12201 : 89773 : else if (same_type_p (t, long_double_type_node))
12202 : : {
12203 : 89673 : max_arity = 1;
12204 : 89673 : *long_double_p = true;
12205 : : }
12206 : 100 : else if (same_type_p (t, char_type_node))
12207 : : max_arity = 1;
12208 : 56 : else if (same_type_p (t, wchar_type_node))
12209 : : max_arity = 1;
12210 : 45 : else if (same_type_p (t, char8_type_node))
12211 : : max_arity = 1;
12212 : 40 : else if (same_type_p (t, char16_type_node))
12213 : : max_arity = 1;
12214 : 29 : else if (same_type_p (t, char32_type_node))
12215 : : max_arity = 1;
12216 : : else
12217 : : return false;
12218 : : }
12219 : 223154 : if (!argtype)
12220 : : return false; /* Found ellipsis. */
12221 : :
12222 : 223151 : if (arity != max_arity)
12223 : : return false;
12224 : :
12225 : : return true;
12226 : : }
12227 : : }
12228 : :
12229 : : /* Always returns false since unlike C90, C++ has no concept of implicit
12230 : : function declarations. */
12231 : :
12232 : : bool
12233 : 359 : c_decl_implicit (const_tree)
12234 : : {
12235 : 359 : return false;
12236 : : }
|