Line data Source code
1 : /* Build expressions with type checking for C++ compiler.
2 : Copyright (C) 1987-2023 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 "gcc-rich-location.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 56766513 : require_complete_type (tree value,
75 : tsubst_flags_t complain /* = tf_warning_or_error */)
76 : {
77 56766513 : tree type;
78 :
79 56766513 : if (processing_template_decl || value == error_mark_node)
80 : return value;
81 :
82 56210832 : if (TREE_CODE (value) == OVERLOAD)
83 0 : type = unknown_type_node;
84 : else
85 56210832 : type = TREE_TYPE (value);
86 :
87 56210832 : if (type == error_mark_node)
88 : return error_mark_node;
89 :
90 : /* First, detect a valid value with a complete type. */
91 56210816 : if (COMPLETE_TYPE_P (type))
92 : return value;
93 :
94 56528 : if (complete_type_or_maybe_complain (type, value, complain))
95 : return value;
96 : else
97 102 : 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 7474232939 : complete_type (tree type)
107 : {
108 7474232939 : 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 7474232939 : if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 : ;
115 2554187508 : else if (TREE_CODE (type) == ARRAY_TYPE)
116 : {
117 394267 : tree t = complete_type (TREE_TYPE (type));
118 394267 : unsigned int needs_constructing, has_nontrivial_dtor;
119 394267 : if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 394117 : layout_type (type);
121 394267 : needs_constructing
122 394267 : = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 394267 : has_nontrivial_dtor
124 394267 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 1182587 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 : {
127 788320 : TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 788320 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 : }
130 : }
131 2553793241 : else if (CLASS_TYPE_P (type))
132 : {
133 2503970111 : if (modules_p ())
134 : /* TYPE could be a class member we've not loaded the definition of. */
135 14718384 : lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
136 :
137 2503970111 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 493953455 : 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 717676934 : complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 : {
151 717676934 : type = complete_type (type);
152 717676931 : if (type == error_mark_node)
153 : /* We already issued an error. */
154 : return NULL_TREE;
155 717676791 : else if (!COMPLETE_TYPE_P (type))
156 : {
157 2850 : if (complain & tf_error)
158 544 : cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
159 2850 : note_failed_type_completion_for_satisfaction (type);
160 2850 : return NULL_TREE;
161 : }
162 : else
163 : return type;
164 : }
165 :
166 : tree
167 91071044 : complete_type_or_else (tree type, tree value)
168 : {
169 91071044 : 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 2818089 : commonparms (tree p1, tree p2)
182 : {
183 2818089 : tree oldargs = p1, newargs, n;
184 2818089 : int i, len;
185 2818089 : int any_change = 0;
186 :
187 2818089 : len = list_length (p1);
188 2818089 : newargs = tree_last (p1);
189 :
190 2818089 : if (newargs == void_list_node)
191 : i = 1;
192 : else
193 : {
194 29137 : i = 0;
195 29137 : newargs = 0;
196 : }
197 :
198 9607241 : for (; i < len; i++)
199 6789152 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200 :
201 : n = newargs;
202 :
203 12396193 : for (i = 0; p1;
204 9578104 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 : {
206 9578332 : if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 : {
208 192 : TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 192 : any_change = 1;
210 : }
211 9577912 : else if (! TREE_PURPOSE (p1))
212 : {
213 9577876 : if (TREE_PURPOSE (p2))
214 : {
215 292884 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 292884 : any_change = 1;
217 : }
218 : }
219 : else
220 : {
221 36 : if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
222 36 : any_change = 1;
223 36 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 : }
225 9578104 : if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 : {
227 2703925 : any_change = 1;
228 2703925 : TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 : }
230 : else
231 6874179 : TREE_VALUE (n) = TREE_VALUE (p1);
232 : }
233 2818089 : if (! any_change)
234 936540 : 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 22921206 : original_type (tree t)
243 : {
244 22921206 : int quals = cp_type_quals (t);
245 22921206 : while (t != error_mark_node
246 23696626 : && TYPE_NAME (t) != NULL_TREE)
247 : {
248 8076498 : tree x = TYPE_NAME (t);
249 8076498 : if (TREE_CODE (x) != TYPE_DECL)
250 : break;
251 8076498 : x = DECL_ORIGINAL_TYPE (x);
252 8076498 : if (x == NULL_TREE)
253 : break;
254 : t = x;
255 : }
256 22921206 : 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 51435 : merge_type_attributes_from (tree type, tree other_type)
264 : {
265 51435 : tree attrs = targetm.merge_type_attributes (type, other_type);
266 51435 : attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
267 51435 : 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 208698 : cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
280 : {
281 208698 : tree mv1 = TYPE_MAIN_VARIANT (t1);
282 208698 : tree mv2 = TYPE_MAIN_VARIANT (t2);
283 208698 : int extended1 = 0;
284 208698 : int extended2 = 0;
285 :
286 208698 : if (mv1 == mv2)
287 : return 0;
288 :
289 1666224 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
290 : {
291 1457946 : if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 144107 : extended1 = i + 1;
293 1457946 : if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 51060 : extended2 = i + 1;
295 : }
296 208278 : if (mv1 == bfloat16_type_node)
297 14611 : extended1 = true;
298 208278 : if (mv2 == bfloat16_type_node)
299 8399 : extended2 = true;
300 208278 : if (extended2 && !extended1)
301 : {
302 48049 : int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 48049 : return ret == 3 ? 3 : -ret;
304 : }
305 :
306 160229 : const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 160229 : const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 160229 : 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 1281832 : int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
315 320458 : ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
316 1281832 : int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
317 320458 : ? 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 160229 : if ((p1 > p2 && fmt1->emax >= fmt2->emax)
322 139567 : || (p1 == p2 && fmt1->emax > fmt2->emax))
323 : return 2;
324 139567 : if ((p1 < p2 && fmt1->emax <= fmt2->emax)
325 101137 : || (p1 == p2 && fmt1->emax < fmt2->emax))
326 : return -2;
327 101137 : if ((p1 > p2 && fmt1->emax < fmt2->emax)
328 100703 : || (p1 < p2 && fmt1->emax > fmt2->emax))
329 : return 3;
330 100269 : 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 100269 : 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 401060 : for (p = &float_type_node; p <= &long_double_type_node; ++p)
375 : {
376 300795 : const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
377 300795 : gcc_assert (fmt3->b == 2);
378 2406360 : int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
379 601590 : ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
380 300795 : if (p1 == p3 && fmt1->emax == fmt3->emax)
381 71765 : ++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 100265 : if (cnt > 1 && mv2 == long_double_type_node)
395 : return -2;
396 : /* Otherwise, they have equal rank, but extended types
397 : (other than std::bfloat16_t) have higher subrank.
398 : std::bfloat16_t shouldn't have equal rank to any standard
399 : floating point type. */
400 : return 1;
401 : }
402 :
403 : /* Return the common type for two arithmetic types T1 and T2 under the
404 : usual arithmetic conversions. The default conversions have already
405 : been applied, and enumerated types converted to their compatible
406 : integer types. */
407 :
408 : static tree
409 61341458 : cp_common_type (tree t1, tree t2)
410 : {
411 61341458 : enum tree_code code1 = TREE_CODE (t1);
412 61341458 : enum tree_code code2 = TREE_CODE (t2);
413 61341458 : tree attributes;
414 61341458 : int i;
415 :
416 :
417 : /* In what follows, we slightly generalize the rules given in [expr] so
418 : as to deal with `long long' and `complex'. First, merge the
419 : attributes. */
420 61341458 : attributes = (*targetm.merge_type_attributes) (t1, t2);
421 :
422 61341458 : if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
423 : {
424 384787 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
425 384787 : return build_type_attribute_variant (t1, attributes);
426 : else
427 : return NULL_TREE;
428 : }
429 :
430 : /* FIXME: Attributes. */
431 60956671 : gcc_assert (ARITHMETIC_TYPE_P (t1)
432 : || VECTOR_TYPE_P (t1)
433 : || UNSCOPED_ENUM_P (t1));
434 60956671 : gcc_assert (ARITHMETIC_TYPE_P (t2)
435 : || VECTOR_TYPE_P (t2)
436 : || UNSCOPED_ENUM_P (t2));
437 :
438 : /* If one type is complex, form the common type of the non-complex
439 : components, then make that complex. Use T1 or T2 if it is the
440 : required type. */
441 60956671 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
442 : {
443 177544 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
444 177544 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
445 177544 : tree subtype
446 177544 : = type_after_usual_arithmetic_conversions (subtype1, subtype2);
447 :
448 177544 : if (subtype == error_mark_node)
449 : return subtype;
450 177544 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
451 144055 : return build_type_attribute_variant (t1, attributes);
452 33489 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
453 33422 : return build_type_attribute_variant (t2, attributes);
454 : else
455 67 : return build_type_attribute_variant (build_complex_type (subtype),
456 67 : attributes);
457 : }
458 :
459 60779127 : if (code1 == VECTOR_TYPE)
460 : {
461 : /* When we get here we should have two vectors of the same size.
462 : Just prefer the unsigned one if present. */
463 51435 : if (TYPE_UNSIGNED (t1))
464 11300 : return merge_type_attributes_from (t1, t2);
465 : else
466 40135 : return merge_type_attributes_from (t2, t1);
467 : }
468 :
469 : /* If only one is real, use it as the result. */
470 60727692 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
471 770024 : return build_type_attribute_variant (t1, attributes);
472 59957668 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
473 749001 : return build_type_attribute_variant (t2, attributes);
474 :
475 59208667 : if (code1 == REAL_TYPE
476 59208667 : && (extended_float_type_p (t1) || extended_float_type_p (t2)))
477 : {
478 9149 : tree mv1 = TYPE_MAIN_VARIANT (t1);
479 9149 : tree mv2 = TYPE_MAIN_VARIANT (t2);
480 9149 : if (mv1 == mv2)
481 8730 : return build_type_attribute_variant (t1, attributes);
482 :
483 419 : int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
484 419 : if (cmpret == 3)
485 0 : return error_mark_node;
486 419 : else if (cmpret >= 0)
487 255 : return build_type_attribute_variant (t1, attributes);
488 : else
489 164 : return build_type_attribute_variant (t2, attributes);
490 : }
491 :
492 : /* Both real or both integers; use the one with greater precision. */
493 59199518 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
494 7739476 : return build_type_attribute_variant (t1, attributes);
495 51460042 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
496 990382 : return build_type_attribute_variant (t2, attributes);
497 :
498 : /* The types are the same; no need to do anything fancy. */
499 50469660 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
500 46528916 : return build_type_attribute_variant (t1, attributes);
501 :
502 3940744 : if (code1 != REAL_TYPE)
503 : {
504 : /* If one is unsigned long long, then convert the other to unsigned
505 : long long. */
506 3940740 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
507 3940740 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
508 79985 : return build_type_attribute_variant (long_long_unsigned_type_node,
509 79985 : attributes);
510 : /* If one is a long long, and the other is an unsigned long, and
511 : long long can represent all the values of an unsigned long, then
512 : convert to a long long. Otherwise, convert to an unsigned long
513 : long. Otherwise, if either operand is long long, convert the
514 : other to long long.
515 :
516 : Since we're here, we know the TYPE_PRECISION is the same;
517 : therefore converting to long long cannot represent all the values
518 : of an unsigned long, so we choose unsigned long long in that
519 : case. */
520 3860755 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
521 3860755 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
522 : {
523 923 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
524 923 : ? long_long_unsigned_type_node
525 465 : : long_long_integer_type_node);
526 465 : return build_type_attribute_variant (t, attributes);
527 : }
528 :
529 : /* Go through the same procedure, but for longs. */
530 3860290 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
531 3860290 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
532 450807 : return build_type_attribute_variant (long_unsigned_type_node,
533 450807 : attributes);
534 3409483 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
535 3409483 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
536 : {
537 69792 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
538 69792 : ? long_unsigned_type_node : long_integer_type_node);
539 35188 : return build_type_attribute_variant (t, attributes);
540 : }
541 :
542 : /* For __intN types, either the type is __int128 (and is lower
543 : priority than the types checked above, but higher than other
544 : 128-bit types) or it's known to not be the same size as other
545 : types (enforced in toplev.cc). Prefer the unsigned type. */
546 6748553 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
547 : {
548 3374295 : if (int_n_enabled_p [i]
549 3374295 : && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
550 3301916 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
551 3301908 : || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
552 3301908 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
553 : {
554 66 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
555 66 : ? int_n_trees[i].unsigned_type
556 37 : : int_n_trees[i].signed_type);
557 37 : return build_type_attribute_variant (t, attributes);
558 : }
559 : }
560 :
561 : /* Otherwise prefer the unsigned one. */
562 3374258 : if (TYPE_UNSIGNED (t1))
563 2718157 : return build_type_attribute_variant (t1, attributes);
564 : else
565 656101 : return build_type_attribute_variant (t2, attributes);
566 : }
567 : else
568 : {
569 4 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
570 4 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
571 0 : return build_type_attribute_variant (long_double_type_node,
572 0 : attributes);
573 4 : if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
574 4 : || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
575 4 : return build_type_attribute_variant (double_type_node,
576 4 : attributes);
577 0 : if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
578 0 : || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
579 0 : return build_type_attribute_variant (float_type_node,
580 0 : attributes);
581 :
582 : /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
583 : the standard C++ floating-point types. Logic earlier in this
584 : function has already eliminated the possibility that
585 : TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
586 : compelling reason to choose one or the other. */
587 0 : return build_type_attribute_variant (t1, attributes);
588 : }
589 : }
590 :
591 : /* T1 and T2 are arithmetic or enumeration types. Return the type
592 : that will result from the "usual arithmetic conversions" on T1 and
593 : T2 as described in [expr]. */
594 :
595 : tree
596 563814 : type_after_usual_arithmetic_conversions (tree t1, tree t2)
597 : {
598 563814 : gcc_assert (ARITHMETIC_TYPE_P (t1)
599 : || VECTOR_TYPE_P (t1)
600 : || UNSCOPED_ENUM_P (t1));
601 563814 : gcc_assert (ARITHMETIC_TYPE_P (t2)
602 : || VECTOR_TYPE_P (t2)
603 : || UNSCOPED_ENUM_P (t2));
604 :
605 : /* Perform the integral promotions. We do not promote real types here. */
606 563814 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
607 386111 : && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
608 : {
609 385873 : t1 = type_promotes_to (t1);
610 385873 : t2 = type_promotes_to (t2);
611 : }
612 :
613 563814 : return cp_common_type (t1, t2);
614 : }
615 :
616 : static void
617 56 : composite_pointer_error (const op_location_t &location,
618 : diagnostic_t kind, tree t1, tree t2,
619 : composite_pointer_operation operation)
620 : {
621 56 : switch (operation)
622 : {
623 44 : case CPO_COMPARISON:
624 44 : emit_diagnostic (kind, location, 0,
625 : "comparison between "
626 : "distinct pointer types %qT and %qT lacks a cast",
627 : t1, t2);
628 44 : break;
629 0 : case CPO_CONVERSION:
630 0 : emit_diagnostic (kind, location, 0,
631 : "conversion between "
632 : "distinct pointer types %qT and %qT lacks a cast",
633 : t1, t2);
634 0 : break;
635 12 : case CPO_CONDITIONAL_EXPR:
636 12 : emit_diagnostic (kind, location, 0,
637 : "conditional expression between "
638 : "distinct pointer types %qT and %qT lacks a cast",
639 : t1, t2);
640 12 : break;
641 0 : default:
642 0 : gcc_unreachable ();
643 : }
644 56 : }
645 :
646 : /* Subroutine of composite_pointer_type to implement the recursive
647 : case. See that function for documentation of the parameters. And ADD_CONST
648 : is used to track adding "const" where needed. */
649 :
650 : static tree
651 1880012 : composite_pointer_type_r (const op_location_t &location,
652 : tree t1, tree t2, bool *add_const,
653 : composite_pointer_operation operation,
654 : tsubst_flags_t complain)
655 : {
656 1880012 : tree pointee1;
657 1880012 : tree pointee2;
658 1880012 : tree result_type;
659 1880012 : tree attributes;
660 :
661 : /* Determine the types pointed to by T1 and T2. */
662 1880012 : if (TYPE_PTR_P (t1))
663 : {
664 1879492 : pointee1 = TREE_TYPE (t1);
665 1879492 : pointee2 = TREE_TYPE (t2);
666 : }
667 : else
668 : {
669 520 : pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
670 520 : pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
671 : }
672 :
673 : /* [expr.type]
674 :
675 : If T1 and T2 are similar types, the result is the cv-combined type of
676 : T1 and T2. */
677 1880012 : if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
678 : result_type = pointee1;
679 36 : else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
680 130 : || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
681 : {
682 36 : result_type = composite_pointer_type_r (location, pointee1, pointee2,
683 : add_const, operation, complain);
684 36 : if (result_type == error_mark_node)
685 : return error_mark_node;
686 : }
687 : else
688 : {
689 94 : if (complain & tf_error)
690 40 : composite_pointer_error (location, DK_PERMERROR,
691 : t1, t2, operation);
692 : else
693 54 : return error_mark_node;
694 40 : result_type = void_type_node;
695 : }
696 1879950 : const int q1 = cp_type_quals (pointee1);
697 1879950 : const int q2 = cp_type_quals (pointee2);
698 1879950 : const int quals = q1 | q2;
699 1879950 : result_type = cp_build_qualified_type (result_type,
700 1879950 : (quals | (*add_const
701 1879950 : ? TYPE_QUAL_CONST
702 : : TYPE_UNQUALIFIED)));
703 : /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
704 : the TLQ). The reason is that both T1 and T2 can then be converted to the
705 : cv-combined type of T1 and T2. */
706 1879950 : if (quals != q1 || quals != q2)
707 365626 : *add_const = true;
708 : /* If the original types were pointers to members, so is the
709 : result. */
710 1879950 : if (TYPE_PTRMEM_P (t1))
711 : {
712 508 : if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
713 : TYPE_PTRMEM_CLASS_TYPE (t2)))
714 : {
715 0 : if (complain & tf_error)
716 0 : composite_pointer_error (location, DK_PERMERROR,
717 : t1, t2, operation);
718 : else
719 0 : return error_mark_node;
720 : }
721 508 : result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
722 : result_type);
723 : }
724 : else
725 1879442 : result_type = build_pointer_type (result_type);
726 :
727 : /* Merge the attributes. */
728 1879950 : attributes = (*targetm.merge_type_attributes) (t1, t2);
729 1879950 : return build_type_attribute_variant (result_type, attributes);
730 : }
731 :
732 : /* Return the composite pointer type (see [expr.type]) for T1 and T2.
733 : ARG1 and ARG2 are the values with those types. The OPERATION is to
734 : describe the operation between the pointer types,
735 : in case an error occurs.
736 :
737 : This routine also implements the computation of a common type for
738 : pointers-to-members as per [expr.eq]. */
739 :
740 : tree
741 1972139 : composite_pointer_type (const op_location_t &location,
742 : tree t1, tree t2, tree arg1, tree arg2,
743 : composite_pointer_operation operation,
744 : tsubst_flags_t complain)
745 : {
746 1972139 : tree class1;
747 1972139 : tree class2;
748 :
749 : /* [expr.type]
750 :
751 : If one operand is a null pointer constant, the composite pointer
752 : type is the type of the other operand. */
753 1972139 : if (null_ptr_cst_p (arg1))
754 : return t2;
755 1971818 : if (null_ptr_cst_p (arg2))
756 : return t1;
757 :
758 : /* We have:
759 :
760 : [expr.type]
761 :
762 : If one of the operands has type "pointer to cv1 void", then
763 : the other has type "pointer to cv2 T", and the composite pointer
764 : type is "pointer to cv12 void", where cv12 is the union of cv1
765 : and cv2.
766 :
767 : If either type is a pointer to void, make sure it is T1. */
768 1964413 : if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
769 : std::swap (t1, t2);
770 :
771 : /* Now, if T1 is a pointer to void, merge the qualifiers. */
772 1964413 : if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
773 : {
774 84293 : tree attributes;
775 84293 : tree result_type;
776 :
777 84293 : if (TYPE_PTRFN_P (t2))
778 : {
779 13 : if (complain & tf_error)
780 : {
781 12 : switch (operation)
782 : {
783 12 : case CPO_COMPARISON:
784 12 : pedwarn (location, OPT_Wpedantic,
785 : "ISO C++ forbids comparison between pointer "
786 : "of type %<void *%> and pointer-to-function");
787 12 : break;
788 0 : case CPO_CONVERSION:
789 0 : pedwarn (location, OPT_Wpedantic,
790 : "ISO C++ forbids conversion between pointer "
791 : "of type %<void *%> and pointer-to-function");
792 0 : break;
793 0 : case CPO_CONDITIONAL_EXPR:
794 0 : pedwarn (location, OPT_Wpedantic,
795 : "ISO C++ forbids conditional expression between "
796 : "pointer of type %<void *%> and "
797 : "pointer-to-function");
798 0 : break;
799 0 : default:
800 0 : gcc_unreachable ();
801 : }
802 : }
803 : else
804 1 : return error_mark_node;
805 : }
806 84292 : result_type
807 84292 : = cp_build_qualified_type (void_type_node,
808 84292 : (cp_type_quals (TREE_TYPE (t1))
809 84292 : | cp_type_quals (TREE_TYPE (t2))));
810 84292 : result_type = build_pointer_type (result_type);
811 : /* Merge the attributes. */
812 84292 : attributes = (*targetm.merge_type_attributes) (t1, t2);
813 84292 : return build_type_attribute_variant (result_type, attributes);
814 : }
815 :
816 1880120 : if (c_dialect_objc () && TYPE_PTR_P (t1)
817 0 : && TYPE_PTR_P (t2))
818 : {
819 0 : if (objc_have_common_type (t1, t2, -3, NULL_TREE))
820 0 : return objc_common_type (t1, t2);
821 : }
822 :
823 : /* if T1 or T2 is "pointer to noexcept function" and the other type is
824 : "pointer to function", where the function types are otherwise the same,
825 : "pointer to function" */
826 1880120 : if (fnptr_conv_p (t1, t2))
827 : return t1;
828 1880071 : if (fnptr_conv_p (t2, t1))
829 : return t2;
830 :
831 : /* [expr.eq] permits the application of a pointer conversion to
832 : bring the pointers to a common type. */
833 1879480 : if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
834 1879480 : && CLASS_TYPE_P (TREE_TYPE (t1))
835 437513 : && CLASS_TYPE_P (TREE_TYPE (t2))
836 2317527 : && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
837 437509 : TREE_TYPE (t2)))
838 : {
839 29637 : class1 = TREE_TYPE (t1);
840 29637 : class2 = TREE_TYPE (t2);
841 :
842 29637 : if (DERIVED_FROM_P (class1, class2))
843 17496 : t2 = (build_pointer_type
844 8748 : (cp_build_qualified_type (class1, cp_type_quals (class2))));
845 20889 : else if (DERIVED_FROM_P (class2, class1))
846 41730 : t1 = (build_pointer_type
847 20865 : (cp_build_qualified_type (class2, cp_type_quals (class1))));
848 : else
849 : {
850 24 : if (complain & tf_error)
851 16 : composite_pointer_error (location, DK_ERROR, t1, t2, operation);
852 24 : return error_mark_node;
853 : }
854 : }
855 : /* [expr.eq] permits the application of a pointer-to-member
856 : conversion to change the class type of one of the types. */
857 1849953 : else if (TYPE_PTRMEM_P (t1)
858 1850491 : && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
859 : TYPE_PTRMEM_CLASS_TYPE (t2)))
860 : {
861 57 : class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
862 57 : class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
863 :
864 57 : if (DERIVED_FROM_P (class1, class2))
865 26 : t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
866 31 : else if (DERIVED_FROM_P (class2, class1))
867 13 : t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
868 : else
869 : {
870 18 : if (complain & tf_error)
871 4 : switch (operation)
872 : {
873 4 : case CPO_COMPARISON:
874 4 : error_at (location, "comparison between distinct "
875 : "pointer-to-member types %qT and %qT lacks a cast",
876 : t1, t2);
877 4 : break;
878 0 : case CPO_CONVERSION:
879 0 : error_at (location, "conversion between distinct "
880 : "pointer-to-member types %qT and %qT lacks a cast",
881 : t1, t2);
882 0 : break;
883 0 : case CPO_CONDITIONAL_EXPR:
884 0 : error_at (location, "conditional expression between distinct "
885 : "pointer-to-member types %qT and %qT lacks a cast",
886 : t1, t2);
887 0 : break;
888 0 : default:
889 0 : gcc_unreachable ();
890 : }
891 18 : return error_mark_node;
892 : }
893 : }
894 :
895 1879976 : bool add_const = false;
896 1879976 : return composite_pointer_type_r (location, t1, t2, &add_const, operation,
897 1879976 : complain);
898 : }
899 :
900 : /* Return the merged type of two types.
901 : We assume that comptypes has already been done and returned 1;
902 : if that isn't so, this may crash.
903 :
904 : This just combines attributes and default arguments; any other
905 : differences would cause the two types to compare unalike. */
906 :
907 : tree
908 19963309 : merge_types (tree t1, tree t2)
909 : {
910 19963309 : enum tree_code code1;
911 19963309 : enum tree_code code2;
912 19963309 : tree attributes;
913 :
914 : /* Save time if the two types are the same. */
915 19963309 : if (t1 == t2)
916 : return t1;
917 11460603 : if (original_type (t1) == original_type (t2))
918 : return t1;
919 :
920 : /* If one type is nonsense, use the other. */
921 11420312 : if (t1 == error_mark_node)
922 : return t2;
923 11420312 : if (t2 == error_mark_node)
924 : return t1;
925 :
926 : /* Handle merging an auto redeclaration with a previous deduced
927 : return type. */
928 11420312 : if (is_auto (t1))
929 : return t2;
930 :
931 : /* Merge the attributes. */
932 11413464 : attributes = (*targetm.merge_type_attributes) (t1, t2);
933 :
934 11413464 : if (TYPE_PTRMEMFUNC_P (t1))
935 20 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
936 11413464 : if (TYPE_PTRMEMFUNC_P (t2))
937 20 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
938 :
939 11413464 : code1 = TREE_CODE (t1);
940 11413464 : code2 = TREE_CODE (t2);
941 11413464 : if (code1 != code2)
942 : {
943 0 : gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
944 0 : if (code1 == TYPENAME_TYPE)
945 : {
946 0 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
947 0 : code1 = TREE_CODE (t1);
948 : }
949 : else
950 : {
951 0 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
952 0 : code2 = TREE_CODE (t2);
953 : }
954 : }
955 :
956 11413464 : switch (code1)
957 : {
958 1988357 : case POINTER_TYPE:
959 1988357 : case REFERENCE_TYPE:
960 : /* For two pointers, do this recursively on the target type. */
961 1988357 : {
962 1988357 : tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
963 1988357 : int quals = cp_type_quals (t1);
964 :
965 1988357 : if (code1 == POINTER_TYPE)
966 : {
967 510827 : t1 = build_pointer_type (target);
968 510827 : if (TREE_CODE (target) == METHOD_TYPE)
969 20 : t1 = build_ptrmemfunc_type (t1);
970 : }
971 : else
972 1477530 : t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
973 1988357 : t1 = build_type_attribute_variant (t1, attributes);
974 1988357 : t1 = cp_build_qualified_type (t1, quals);
975 :
976 1988357 : return t1;
977 : }
978 :
979 15 : case OFFSET_TYPE:
980 15 : {
981 15 : int quals;
982 15 : tree pointee;
983 15 : quals = cp_type_quals (t1);
984 15 : pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
985 15 : TYPE_PTRMEM_POINTED_TO_TYPE (t2));
986 15 : t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
987 : pointee);
988 15 : t1 = cp_build_qualified_type (t1, quals);
989 15 : break;
990 : }
991 :
992 7486 : case ARRAY_TYPE:
993 7486 : {
994 7486 : tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
995 : /* Save space: see if the result is identical to one of the args. */
996 14972 : if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
997 7460 : return build_type_attribute_variant (t1, attributes);
998 44 : if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
999 18 : return build_type_attribute_variant (t2, attributes);
1000 : /* Merge the element types, and have a size if either arg has one. */
1001 8 : t1 = build_cplus_array_type
1002 8 : (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
1003 8 : break;
1004 : }
1005 :
1006 2921885 : case FUNCTION_TYPE:
1007 : /* Function types: prefer the one that specified arg types.
1008 : If both do, merge the arg types. Also merge the return types. */
1009 2921885 : {
1010 2921885 : tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1011 2921885 : tree p1 = TYPE_ARG_TYPES (t1);
1012 2921885 : tree p2 = TYPE_ARG_TYPES (t2);
1013 2921885 : tree parms;
1014 :
1015 : /* Save space: see if the result is identical to one of the args. */
1016 2921885 : if (valtype == TREE_TYPE (t1) && ! p2)
1017 0 : return cp_build_type_attribute_variant (t1, attributes);
1018 2921885 : if (valtype == TREE_TYPE (t2) && ! p1)
1019 0 : return cp_build_type_attribute_variant (t2, attributes);
1020 :
1021 : /* Simple way if one arg fails to specify argument types. */
1022 5843770 : if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1023 : parms = p2;
1024 5636178 : else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1025 : parms = p1;
1026 : else
1027 2818089 : parms = commonparms (p1, p2);
1028 :
1029 2921885 : cp_cv_quals quals = type_memfn_quals (t1);
1030 2921885 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1031 2921885 : gcc_assert (quals == type_memfn_quals (t2));
1032 2921885 : gcc_assert (rqual == type_memfn_rqual (t2));
1033 :
1034 2921885 : tree rval = build_function_type (valtype, parms);
1035 2921885 : rval = apply_memfn_quals (rval, quals);
1036 2921885 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1037 2921885 : TYPE_RAISES_EXCEPTIONS (t2));
1038 2921885 : bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1039 2921885 : t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1040 2921885 : break;
1041 : }
1042 :
1043 2765537 : case METHOD_TYPE:
1044 2765537 : {
1045 : /* Get this value the long way, since TYPE_METHOD_BASETYPE
1046 : is just the main variant of this. */
1047 2765537 : tree basetype = class_of_this_parm (t2);
1048 2765537 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1049 2765537 : TYPE_RAISES_EXCEPTIONS (t2));
1050 2765537 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1051 2765537 : tree t3;
1052 2765537 : bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1053 :
1054 : /* If this was a member function type, get back to the
1055 : original type of type member function (i.e., without
1056 : the class instance variable up front. */
1057 2765537 : t1 = build_function_type (TREE_TYPE (t1),
1058 2765537 : TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1059 2765537 : t2 = build_function_type (TREE_TYPE (t2),
1060 2765537 : TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1061 2765537 : t3 = merge_types (t1, t2);
1062 2765537 : t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1063 2765537 : TYPE_ARG_TYPES (t3));
1064 2765537 : t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1065 2765537 : break;
1066 : }
1067 :
1068 : case TYPENAME_TYPE:
1069 : /* There is no need to merge attributes into a TYPENAME_TYPE.
1070 : When the type is instantiated it will have whatever
1071 : attributes result from the instantiation. */
1072 : return t1;
1073 :
1074 3723360 : default:;
1075 3723360 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1076 : return t1;
1077 15 : else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1078 : return t2;
1079 : break;
1080 : }
1081 :
1082 5687445 : return cp_build_type_attribute_variant (t1, attributes);
1083 : }
1084 :
1085 : /* Return the ARRAY_TYPE type without its domain. */
1086 :
1087 : tree
1088 222 : strip_array_domain (tree type)
1089 : {
1090 222 : tree t2;
1091 222 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1092 222 : if (TYPE_DOMAIN (type) == NULL_TREE)
1093 : return type;
1094 200 : t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
1095 200 : return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
1096 : }
1097 :
1098 : /* Wrapper around cp_common_type that is used by c-common.cc and other
1099 : front end optimizations that remove promotions.
1100 :
1101 : Return the common type for two arithmetic types T1 and T2 under the
1102 : usual arithmetic conversions. The default conversions have already
1103 : been applied, and enumerated types converted to their compatible
1104 : integer types. */
1105 :
1106 : tree
1107 253448 : common_type (tree t1, tree t2)
1108 : {
1109 : /* If one type is nonsense, use the other */
1110 253448 : if (t1 == error_mark_node)
1111 : return t2;
1112 253448 : if (t2 == error_mark_node)
1113 : return t1;
1114 :
1115 253448 : return cp_common_type (t1, t2);
1116 : }
1117 :
1118 : /* Return the common type of two pointer types T1 and T2. This is the
1119 : type for the result of most arithmetic operations if the operands
1120 : have the given two types.
1121 :
1122 : We assume that comp_target_types has already been done and returned
1123 : nonzero; if that isn't so, this may crash. */
1124 :
1125 : tree
1126 707829 : common_pointer_type (tree t1, tree t2)
1127 : {
1128 707829 : gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
1129 : || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
1130 : || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
1131 :
1132 707829 : return composite_pointer_type (input_location, t1, t2,
1133 : error_mark_node, error_mark_node,
1134 707829 : CPO_CONVERSION, tf_warning_or_error);
1135 : }
1136 :
1137 : /* Compare two exception specifier types for exactness or subsetness, if
1138 : allowed. Returns false for mismatch, true for match (same, or
1139 : derived and !exact).
1140 :
1141 : [except.spec] "If a class X ... objects of class X or any class publicly
1142 : and unambiguously derived from X. Similarly, if a pointer type Y * ...
1143 : exceptions of type Y * or that are pointers to any type publicly and
1144 : unambiguously derived from Y. Otherwise a function only allows exceptions
1145 : that have the same type ..."
1146 : This does not mention cv qualifiers and is different to what throw
1147 : [except.throw] and catch [except.catch] will do. They will ignore the
1148 : top level cv qualifiers, and allow qualifiers in the pointer to class
1149 : example.
1150 :
1151 : We implement the letter of the standard. */
1152 :
1153 : static bool
1154 2189 : comp_except_types (tree a, tree b, bool exact)
1155 : {
1156 2189 : if (same_type_p (a, b))
1157 : return true;
1158 989 : else if (!exact)
1159 : {
1160 22 : if (cp_type_quals (a) || cp_type_quals (b))
1161 : return false;
1162 :
1163 22 : if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1164 : {
1165 14 : a = TREE_TYPE (a);
1166 14 : b = TREE_TYPE (b);
1167 14 : if (cp_type_quals (a) || cp_type_quals (b))
1168 : return false;
1169 : }
1170 :
1171 20 : if (TREE_CODE (a) != RECORD_TYPE
1172 20 : || TREE_CODE (b) != RECORD_TYPE)
1173 : return false;
1174 :
1175 18 : if (publicly_uniquely_derived_p (a, b))
1176 : return true;
1177 : }
1178 : return false;
1179 : }
1180 :
1181 : /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1182 : If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1183 : If EXACT is ce_type, the C++17 type compatibility rules apply.
1184 : If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1185 : If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1186 : are unordered, but we've already filtered out duplicates. Most lists will
1187 : be in order, we should try to make use of that. */
1188 :
1189 : bool
1190 1107026756 : comp_except_specs (const_tree t1, const_tree t2, int exact)
1191 : {
1192 1107026756 : const_tree probe;
1193 1107026756 : const_tree base;
1194 1107026756 : int length = 0;
1195 :
1196 1107026756 : if (t1 == t2)
1197 : return true;
1198 :
1199 : /* First handle noexcept. */
1200 378230560 : if (exact < ce_exact)
1201 : {
1202 5021160 : if (exact == ce_type
1203 10011076 : && (canonical_eh_spec (CONST_CAST_TREE (t1))
1204 4989916 : == canonical_eh_spec (CONST_CAST_TREE (t2))))
1205 : return true;
1206 :
1207 : /* noexcept(false) is compatible with no exception-specification,
1208 : and less strict than any spec. */
1209 5018408 : if (t1 == noexcept_false_spec)
1210 99 : return t2 == NULL_TREE || exact == ce_derived;
1211 : /* Even a derived noexcept(false) is compatible with no
1212 : exception-specification. */
1213 5018309 : if (t2 == noexcept_false_spec)
1214 48 : return t1 == NULL_TREE;
1215 :
1216 : /* Otherwise, if we aren't looking for an exact match, noexcept is
1217 : equivalent to throw(). */
1218 5018261 : if (t1 == noexcept_true_spec)
1219 704962 : t1 = empty_except_spec;
1220 5018261 : if (t2 == noexcept_true_spec)
1221 4189614 : t2 = empty_except_spec;
1222 : }
1223 :
1224 : /* If any noexcept is left, it is only comparable to itself;
1225 : either we're looking for an exact match or we're redeclaring a
1226 : template with dependent noexcept. */
1227 367258551 : if ((t1 && TREE_PURPOSE (t1))
1228 391689842 : || (t2 && TREE_PURPOSE (t2)))
1229 371436630 : return (t1 && t2
1230 409650757 : && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1231 :
1232 6791031 : if (t1 == NULL_TREE) /* T1 is ... */
1233 4359973 : return t2 == NULL_TREE || exact == ce_derived;
1234 2431058 : if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1235 2401241 : return t2 != NULL_TREE && !TREE_VALUE (t2);
1236 31879 : if (t2 == NULL_TREE) /* T2 is ... */
1237 : return false;
1238 1921 : if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1239 118 : return exact == ce_derived;
1240 :
1241 : /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1242 : Count how many we find, to determine exactness. For exact matching and
1243 : ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1244 : O(nm). */
1245 3009 : for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1246 : {
1247 2962 : for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1248 : {
1249 2189 : tree a = TREE_VALUE (probe);
1250 2189 : tree b = TREE_VALUE (t2);
1251 :
1252 2189 : if (comp_except_types (a, b, exact))
1253 : {
1254 1206 : if (probe == base && exact > ce_derived)
1255 1136 : base = TREE_CHAIN (probe);
1256 1206 : length++;
1257 1206 : break;
1258 : }
1259 : }
1260 1979 : if (probe == NULL_TREE)
1261 : return false;
1262 : }
1263 1030 : return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1264 : }
1265 :
1266 : /* Compare the array types T1 and T2. CB says how we should behave when
1267 : comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1268 : bounds_either says than any array can be [], bounds_first means that
1269 : onlt T1 can be an array with unknown bounds. STRICT is true if
1270 : qualifiers must match when comparing the types of the array elements. */
1271 :
1272 : static bool
1273 661307 : comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1274 : bool strict)
1275 : {
1276 661307 : tree d1;
1277 661307 : tree d2;
1278 661307 : tree max1, max2;
1279 :
1280 661307 : if (t1 == t2)
1281 : return true;
1282 :
1283 : /* The type of the array elements must be the same. */
1284 661252 : if (strict
1285 662927 : ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1286 1675 : : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1287 : return false;
1288 :
1289 435153 : d1 = TYPE_DOMAIN (t1);
1290 435153 : d2 = TYPE_DOMAIN (t2);
1291 :
1292 435153 : if (d1 == d2)
1293 : return true;
1294 :
1295 : /* If one of the arrays is dimensionless, and the other has a
1296 : dimension, they are of different types. However, it is valid to
1297 : write:
1298 :
1299 : extern int a[];
1300 : int a[3];
1301 :
1302 : by [basic.link]:
1303 :
1304 : declarations for an array object can specify
1305 : array types that differ by the presence or absence of a major
1306 : array bound (_dcl.array_). */
1307 360083 : if (!d1 && d2)
1308 1493 : return cb >= bounds_either;
1309 358590 : else if (d1 && !d2)
1310 5807 : return cb == bounds_either;
1311 :
1312 : /* Check that the dimensions are the same. */
1313 :
1314 352783 : if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1315 : return false;
1316 352779 : max1 = TYPE_MAX_VALUE (d1);
1317 352779 : max2 = TYPE_MAX_VALUE (d2);
1318 :
1319 352779 : if (!cp_tree_equal (max1, max2))
1320 : return false;
1321 :
1322 : return true;
1323 : }
1324 :
1325 : /* Compare the relative position of T1 and T2 into their respective
1326 : template parameter list.
1327 : T1 and T2 must be template parameter types.
1328 : Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1329 :
1330 : static bool
1331 1250777821 : comp_template_parms_position (tree t1, tree t2)
1332 : {
1333 1250777821 : tree index1, index2;
1334 1250777821 : gcc_assert (t1 && t2
1335 : && TREE_CODE (t1) == TREE_CODE (t2)
1336 : && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1337 : || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1338 : || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1339 :
1340 1250777821 : index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1341 1250777821 : index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1342 :
1343 : /* Then compare their relative position. */
1344 1250777821 : if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1345 900368713 : || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1346 1986131134 : || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1347 735353313 : != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1348 : return false;
1349 :
1350 : /* In C++14 we can end up comparing 'auto' to a normal template
1351 : parameter. Don't confuse them. */
1352 707697968 : if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1353 263540995 : return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1354 :
1355 : return true;
1356 : }
1357 :
1358 : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1359 :
1360 : static bool
1361 454 : cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1362 : {
1363 454 : t1 = TYPE_MAIN_VARIANT (t1);
1364 454 : t2 = TYPE_MAIN_VARIANT (t2);
1365 :
1366 454 : if (TYPE_PTR_P (t1)
1367 165 : && TYPE_PTR_P (t2))
1368 : return true;
1369 :
1370 : /* The signedness of the parameter matters only when an integral
1371 : type smaller than int is promoted to int, otherwise only the
1372 : precision of the parameter matters.
1373 : This check should make sure that the callee does not see
1374 : undefined values in argument registers. */
1375 297 : if (INTEGRAL_TYPE_P (t1)
1376 83 : && INTEGRAL_TYPE_P (t2)
1377 56 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1378 349 : && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1379 0 : || !targetm.calls.promote_prototypes (NULL_TREE)
1380 0 : || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1381 52 : return true;
1382 :
1383 245 : return same_type_p (t1, t2);
1384 : }
1385 :
1386 : /* Check if a type cast between two function types can be considered safe. */
1387 :
1388 : static bool
1389 507 : cxx_safe_function_type_cast_p (tree t1, tree t2)
1390 : {
1391 507 : if (TREE_TYPE (t1) == void_type_node &&
1392 432 : TYPE_ARG_TYPES (t1) == void_list_node)
1393 : return true;
1394 :
1395 401 : if (TREE_TYPE (t2) == void_type_node &&
1396 318 : TYPE_ARG_TYPES (t2) == void_list_node)
1397 : return true;
1398 :
1399 168 : if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1400 : return false;
1401 :
1402 137 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1403 376 : t1 && t2;
1404 239 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1405 286 : if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1406 : return false;
1407 :
1408 : return true;
1409 : }
1410 :
1411 : /* Subroutine in comptypes. */
1412 :
1413 : static bool
1414 6783077021 : structural_comptypes (tree t1, tree t2, int strict)
1415 : {
1416 : /* Both should be types that are not obviously the same. */
1417 6783077021 : gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1418 :
1419 : /* Suppress typename resolution under spec_hasher::equal in place of calling
1420 : push_to_top_level there. */
1421 6783077021 : if (!comparing_specializations)
1422 : {
1423 : /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1424 : current instantiation. */
1425 5605364869 : if (TREE_CODE (t1) == TYPENAME_TYPE)
1426 58617590 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1427 :
1428 5605364869 : if (TREE_CODE (t2) == TYPENAME_TYPE)
1429 69724432 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1430 : }
1431 :
1432 6783077021 : if (TYPE_PTRMEMFUNC_P (t1))
1433 22434411 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1434 6783077021 : if (TYPE_PTRMEMFUNC_P (t2))
1435 25099996 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1436 :
1437 : /* Different classes of types can't be compatible. */
1438 6783077021 : if (TREE_CODE (t1) != TREE_CODE (t2))
1439 : return false;
1440 :
1441 : /* Qualifiers must match. For array types, we will check when we
1442 : recur on the array element types. */
1443 4469653953 : if (TREE_CODE (t1) != ARRAY_TYPE
1444 4469653953 : && cp_type_quals (t1) != cp_type_quals (t2))
1445 : return false;
1446 4259005122 : if (TREE_CODE (t1) == FUNCTION_TYPE
1447 4259005122 : && type_memfn_quals (t1) != type_memfn_quals (t2))
1448 : return false;
1449 : /* Need to check this before TYPE_MAIN_VARIANT.
1450 : FIXME function qualifiers should really change the main variant. */
1451 4258978899 : if (FUNC_OR_METHOD_TYPE_P (t1))
1452 : {
1453 26632386 : if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1454 : return false;
1455 13272151 : if (flag_noexcept_type
1456 26392938 : && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1457 13120787 : TYPE_RAISES_EXCEPTIONS (t2),
1458 : ce_type))
1459 : return false;
1460 : }
1461 :
1462 : /* Allow for two different type nodes which have essentially the same
1463 : definition. Note that we already checked for equality of the type
1464 : qualifiers (just above). */
1465 4240726217 : if (TREE_CODE (t1) != ARRAY_TYPE
1466 4240726217 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1467 157644914 : goto check_alias;
1468 :
1469 : /* Compare the types. Return false on known not-same. Break on not
1470 : known. Never return true from this switch -- you'll break
1471 : specialization comparison. */
1472 4083081303 : switch (TREE_CODE (t1))
1473 : {
1474 : case VOID_TYPE:
1475 : case BOOLEAN_TYPE:
1476 : /* All void and bool types are the same. */
1477 : break;
1478 :
1479 269017685 : case OPAQUE_TYPE:
1480 269017685 : case INTEGER_TYPE:
1481 269017685 : case FIXED_POINT_TYPE:
1482 269017685 : case REAL_TYPE:
1483 : /* With these nodes, we can't determine type equivalence by
1484 : looking at what is stored in the nodes themselves, because
1485 : two nodes might have different TYPE_MAIN_VARIANTs but still
1486 : represent the same type. For example, wchar_t and int could
1487 : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1488 : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1489 : and are distinct types. On the other hand, int and the
1490 : following typedef
1491 :
1492 : typedef int INT __attribute((may_alias));
1493 :
1494 : have identical properties, different TYPE_MAIN_VARIANTs, but
1495 : represent the same type. The canonical type system keeps
1496 : track of equivalence in this case, so we fall back on it. */
1497 269017685 : if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1498 : return false;
1499 :
1500 : /* We don't need or want the attribute comparison. */
1501 1171 : goto check_alias;
1502 :
1503 529916 : case TEMPLATE_TEMPLATE_PARM:
1504 529916 : case BOUND_TEMPLATE_TEMPLATE_PARM:
1505 529916 : if (!comp_template_parms_position (t1, t2))
1506 : return false;
1507 446712 : if (!comp_template_parms
1508 893424 : (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1509 1280511 : DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1510 : return false;
1511 310796 : if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1512 : break;
1513 : /* Don't check inheritance. */
1514 : strict = COMPARE_STRICT;
1515 : /* Fall through. */
1516 :
1517 1971205959 : case RECORD_TYPE:
1518 1971205959 : case UNION_TYPE:
1519 3464820894 : if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1520 3550840644 : && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1521 844679015 : || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1522 2988036402 : && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1523 : break;
1524 :
1525 1956224680 : if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1526 : break;
1527 1956223440 : else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1528 : break;
1529 :
1530 : return false;
1531 :
1532 33210 : case OFFSET_TYPE:
1533 33210 : if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1534 : strict & ~COMPARE_REDECLARATION))
1535 : return false;
1536 31627 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1537 : return false;
1538 : break;
1539 :
1540 272169974 : case REFERENCE_TYPE:
1541 272169974 : if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1542 : return false;
1543 : /* fall through to checks for pointer types */
1544 447121225 : gcc_fallthrough ();
1545 :
1546 447121225 : case POINTER_TYPE:
1547 447121225 : if (TYPE_MODE (t1) != TYPE_MODE (t2)
1548 447121225 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1549 393290488 : return false;
1550 : break;
1551 :
1552 8199217 : case METHOD_TYPE:
1553 8199217 : case FUNCTION_TYPE:
1554 : /* Exception specs and memfn_rquals were checked above. */
1555 8199217 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1556 : return false;
1557 7345526 : if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1558 : return false;
1559 : break;
1560 :
1561 659577 : case ARRAY_TYPE:
1562 : /* Target types must match incl. qualifiers. */
1563 659577 : if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1564 659577 : ? bounds_either : bounds_none),
1565 : /*strict=*/true))
1566 : return false;
1567 : break;
1568 :
1569 1250247905 : case TEMPLATE_TYPE_PARM:
1570 : /* If T1 and T2 don't have the same relative position in their
1571 : template parameters set, they can't be equal. */
1572 1250247905 : if (!comp_template_parms_position (t1, t2))
1573 : return false;
1574 : /* If T1 and T2 don't represent the same class template deduction,
1575 : they aren't equal. */
1576 669047979 : if (CLASS_PLACEHOLDER_TEMPLATE (t1)
1577 669047979 : != CLASS_PLACEHOLDER_TEMPLATE (t2))
1578 : return false;
1579 : /* Constrained 'auto's are distinct from parms that don't have the same
1580 : constraints. */
1581 456661032 : if (!equivalent_placeholder_constraints (t1, t2))
1582 : return false;
1583 : break;
1584 :
1585 32513194 : case TYPENAME_TYPE:
1586 65026388 : if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1587 32513194 : TYPENAME_TYPE_FULLNAME (t2)))
1588 : return false;
1589 : /* Qualifiers don't matter on scopes. */
1590 29002230 : if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1591 29002230 : TYPE_CONTEXT (t2)))
1592 : return false;
1593 : break;
1594 :
1595 3502 : case UNBOUND_CLASS_TEMPLATE:
1596 3502 : if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1597 : return false;
1598 3502 : if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1599 : return false;
1600 : break;
1601 :
1602 878912 : case COMPLEX_TYPE:
1603 878912 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1604 : return false;
1605 : break;
1606 :
1607 7532893 : case VECTOR_TYPE:
1608 7532893 : if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1609 13780169 : || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1610 13825845 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1611 1285617 : return false;
1612 : break;
1613 :
1614 4308191 : case TYPE_PACK_EXPANSION:
1615 4308191 : return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1616 : PACK_EXPANSION_PATTERN (t2))
1617 8514842 : && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1618 4206651 : PACK_EXPANSION_EXTRA_ARGS (t2)));
1619 :
1620 1734262 : case DECLTYPE_TYPE:
1621 3468524 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1622 1734262 : != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1623 : return false;
1624 1731576 : if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1625 : return false;
1626 1731576 : if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1627 : return false;
1628 1731576 : if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1629 : return false;
1630 : break;
1631 :
1632 136 : case TRAIT_TYPE:
1633 136 : if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1634 : return false;
1635 136 : if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1636 272 : || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1637 0 : return false;
1638 : break;
1639 :
1640 13 : case TYPEOF_TYPE:
1641 13 : if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1642 : return false;
1643 : break;
1644 :
1645 : default:
1646 : return false;
1647 : }
1648 :
1649 : /* If we get here, we know that from a target independent POV the
1650 : types are the same. Make sure the target attributes are also
1651 : the same. */
1652 537426194 : if (!comp_type_attributes (t1, t2))
1653 : return false;
1654 :
1655 537425839 : check_alias:
1656 695071924 : if (comparing_dependent_aliases)
1657 : {
1658 : /* Don't treat an alias template specialization with dependent
1659 : arguments as equivalent to its underlying type when used as a
1660 : template argument; we need them to be distinct so that we
1661 : substitute into the specialization arguments at instantiation
1662 : time. And aliases can't be equivalent without being ==, so
1663 : we don't need to look any deeper. */
1664 171410123 : ++processing_template_decl;
1665 171410123 : tree dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1666 171410123 : tree dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1667 171410123 : --processing_template_decl;
1668 171410123 : if ((dep1 || dep2) && dep1 != dep2)
1669 : return false;
1670 : }
1671 :
1672 : return true;
1673 : }
1674 :
1675 : /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1676 : is a bitwise-or of the COMPARE_* flags. */
1677 :
1678 : bool
1679 10816355819 : comptypes (tree t1, tree t2, int strict)
1680 : {
1681 10816355819 : gcc_checking_assert (t1 && t2);
1682 :
1683 : /* TYPE_ARGUMENT_PACKS are not really types. */
1684 10816355819 : gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1685 : && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1686 :
1687 10816355819 : if (t1 == t2)
1688 : return true;
1689 :
1690 : /* Suppress errors caused by previously reported errors. */
1691 6783078479 : if (t1 == error_mark_node || t2 == error_mark_node)
1692 : return false;
1693 :
1694 6783078320 : if (strict == COMPARE_STRICT)
1695 : {
1696 5838945946 : if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1697 : /* At least one of the types requires structural equality, so
1698 : perform a deep check. */
1699 344345816 : return structural_comptypes (t1, t2, strict);
1700 :
1701 5494600130 : if (flag_checking && param_use_canonical_types)
1702 : {
1703 5494598831 : bool result = structural_comptypes (t1, t2, strict);
1704 :
1705 5494598831 : if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1706 : /* The two types are structurally equivalent, but their
1707 : canonical types were different. This is a failure of the
1708 : canonical type propagation code.*/
1709 0 : internal_error
1710 0 : ("canonical types differ for identical types %qT and %qT",
1711 : t1, t2);
1712 5494598831 : else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1713 : /* Two types are structurally different, but the canonical
1714 : types are the same. This means we were over-eager in
1715 : assigning canonical types. */
1716 0 : internal_error
1717 0 : ("same canonical type node for different types %qT and %qT",
1718 : t1, t2);
1719 :
1720 : return result;
1721 : }
1722 1299 : if (!flag_checking && param_use_canonical_types)
1723 1299 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1724 : else
1725 0 : return structural_comptypes (t1, t2, strict);
1726 : }
1727 944132374 : else if (strict == COMPARE_STRUCTURAL)
1728 929801016 : return structural_comptypes (t1, t2, COMPARE_STRICT);
1729 : else
1730 14331358 : return structural_comptypes (t1, t2, strict);
1731 : }
1732 :
1733 : /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1734 : top-level qualifiers. */
1735 :
1736 : bool
1737 1163283186 : same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1738 : {
1739 1163283186 : if (type1 == error_mark_node || type2 == error_mark_node)
1740 : return false;
1741 1163283179 : if (type1 == type2)
1742 : return true;
1743 :
1744 645295229 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1745 645295229 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1746 645295229 : return same_type_p (type1, type2);
1747 : }
1748 :
1749 : /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1750 :
1751 : bool
1752 90210678 : similar_type_p (tree type1, tree type2)
1753 : {
1754 90210678 : if (type1 == error_mark_node || type2 == error_mark_node)
1755 : return false;
1756 :
1757 : /* Informally, two types are similar if, ignoring top-level cv-qualification:
1758 : * they are the same type; or
1759 : * they are both pointers, and the pointed-to types are similar; or
1760 : * they are both pointers to member of the same class, and the types of
1761 : the pointed-to members are similar; or
1762 : * they are both arrays of the same size or both arrays of unknown bound,
1763 : and the array element types are similar. */
1764 :
1765 90210678 : if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1766 : return true;
1767 :
1768 44184386 : if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1769 44171651 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1770 44171651 : || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1771 13116 : return comp_ptr_ttypes_const (type1, type2, bounds_either);
1772 :
1773 : return false;
1774 : }
1775 :
1776 : /* Helper function for layout_compatible_type_p and
1777 : is_corresponding_member_aggr. Advance to next members (NULL if
1778 : no further ones) and return true if those members are still part of
1779 : the common initial sequence. */
1780 :
1781 : bool
1782 741 : next_common_initial_sequence (tree &memb1, tree &memb2)
1783 : {
1784 908 : while (memb1)
1785 : {
1786 920 : if (TREE_CODE (memb1) != FIELD_DECL
1787 773 : || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1788 : {
1789 147 : memb1 = DECL_CHAIN (memb1);
1790 147 : continue;
1791 : }
1792 626 : if (DECL_FIELD_IS_BASE (memb1))
1793 : {
1794 20 : memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1795 20 : continue;
1796 : }
1797 : break;
1798 : }
1799 898 : while (memb2)
1800 : {
1801 910 : if (TREE_CODE (memb2) != FIELD_DECL
1802 763 : || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1803 : {
1804 147 : memb2 = DECL_CHAIN (memb2);
1805 147 : continue;
1806 : }
1807 616 : if (DECL_FIELD_IS_BASE (memb2))
1808 : {
1809 10 : memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1810 10 : continue;
1811 : }
1812 : break;
1813 : }
1814 741 : if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1815 : return true;
1816 606 : if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1817 : return false;
1818 606 : if (DECL_BIT_FIELD_TYPE (memb1))
1819 : {
1820 50 : if (!DECL_BIT_FIELD_TYPE (memb2))
1821 : return false;
1822 14 : if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1823 14 : DECL_BIT_FIELD_TYPE (memb2)))
1824 : return false;
1825 14 : if (TYPE_PRECISION (TREE_TYPE (memb1))
1826 14 : != TYPE_PRECISION (TREE_TYPE (memb2)))
1827 : return false;
1828 : }
1829 556 : else if (DECL_BIT_FIELD_TYPE (memb2))
1830 : return false;
1831 546 : else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1832 : return false;
1833 537 : if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1834 537 : != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1835 : return false;
1836 527 : if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1837 : return false;
1838 508 : if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1839 : return false;
1840 : return true;
1841 : }
1842 :
1843 : /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1844 :
1845 : bool
1846 963 : layout_compatible_type_p (tree type1, tree type2)
1847 : {
1848 963 : if (type1 == error_mark_node || type2 == error_mark_node)
1849 : return false;
1850 963 : if (type1 == type2)
1851 : return true;
1852 450 : if (TREE_CODE (type1) != TREE_CODE (type2))
1853 : return false;
1854 :
1855 436 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1856 436 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1857 :
1858 436 : if (TREE_CODE (type1) == ENUMERAL_TYPE)
1859 14 : return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1860 14 : && same_type_p (finish_underlying_type (type1),
1861 : finish_underlying_type (type2)));
1862 :
1863 133 : if (CLASS_TYPE_P (type1)
1864 133 : && std_layout_type_p (type1)
1865 129 : && std_layout_type_p (type2)
1866 551 : && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1867 : {
1868 114 : tree field1 = TYPE_FIELDS (type1);
1869 114 : tree field2 = TYPE_FIELDS (type2);
1870 114 : if (TREE_CODE (type1) == RECORD_TYPE)
1871 : {
1872 343 : while (1)
1873 : {
1874 216 : if (!next_common_initial_sequence (field1, field2))
1875 : return false;
1876 214 : if (field1 == NULL_TREE)
1877 : return true;
1878 127 : field1 = DECL_CHAIN (field1);
1879 127 : field2 = DECL_CHAIN (field2);
1880 : }
1881 : }
1882 : /* Otherwise both types must be union types.
1883 : The standard says:
1884 : "Two standard-layout unions are layout-compatible if they have
1885 : the same number of non-static data members and corresponding
1886 : non-static data members (in any order) have layout-compatible
1887 : types."
1888 : but the code anticipates that bitfield vs. non-bitfield,
1889 : different bitfield widths or presence/absence of
1890 : [[no_unique_address]] should be checked as well. */
1891 25 : auto_vec<tree, 16> vec;
1892 25 : unsigned int count = 0;
1893 113 : for (; field1; field1 = DECL_CHAIN (field1))
1894 88 : if (TREE_CODE (field1) == FIELD_DECL)
1895 58 : count++;
1896 113 : for (; field2; field2 = DECL_CHAIN (field2))
1897 88 : if (TREE_CODE (field2) == FIELD_DECL)
1898 58 : vec.safe_push (field2);
1899 : /* Discussions on core lean towards treating multiple union fields
1900 : of the same type as the same field, so this might need changing
1901 : in the future. */
1902 50 : if (count != vec.length ())
1903 : return false;
1904 107 : for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1905 : {
1906 84 : if (TREE_CODE (field1) != FIELD_DECL)
1907 28 : continue;
1908 56 : unsigned int j;
1909 56 : tree t1 = DECL_BIT_FIELD_TYPE (field1);
1910 56 : if (t1 == NULL_TREE)
1911 55 : t1 = TREE_TYPE (field1);
1912 92 : FOR_EACH_VEC_ELT (vec, j, field2)
1913 : {
1914 90 : tree t2 = DECL_BIT_FIELD_TYPE (field2);
1915 90 : if (t2 == NULL_TREE)
1916 86 : t2 = TREE_TYPE (field2);
1917 90 : if (DECL_BIT_FIELD_TYPE (field1))
1918 : {
1919 2 : if (!DECL_BIT_FIELD_TYPE (field2))
1920 0 : continue;
1921 2 : if (TYPE_PRECISION (TREE_TYPE (field1))
1922 2 : != TYPE_PRECISION (TREE_TYPE (field2)))
1923 2 : continue;
1924 : }
1925 88 : else if (DECL_BIT_FIELD_TYPE (field2))
1926 2 : continue;
1927 86 : if (!layout_compatible_type_p (t1, t2))
1928 32 : continue;
1929 54 : if ((!lookup_attribute ("no_unique_address",
1930 54 : DECL_ATTRIBUTES (field1)))
1931 54 : != !lookup_attribute ("no_unique_address",
1932 54 : DECL_ATTRIBUTES (field2)))
1933 0 : continue;
1934 : break;
1935 : }
1936 112 : if (j == vec.length ())
1937 : return false;
1938 136 : vec.unordered_remove (j);
1939 : }
1940 : return true;
1941 114 : }
1942 :
1943 308 : return same_type_p (type1, type2);
1944 : }
1945 :
1946 : /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1947 :
1948 : bool
1949 68075164 : at_least_as_qualified_p (const_tree type1, const_tree type2)
1950 : {
1951 68075164 : int q1 = cp_type_quals (type1);
1952 68075164 : int q2 = cp_type_quals (type2);
1953 :
1954 : /* All qualifiers for TYPE2 must also appear in TYPE1. */
1955 68075164 : return (q1 & q2) == q2;
1956 : }
1957 :
1958 : /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1959 : more cv-qualified that TYPE1, and 0 otherwise. */
1960 :
1961 : int
1962 4094008 : comp_cv_qualification (int q1, int q2)
1963 : {
1964 4094008 : if (q1 == q2)
1965 : return 0;
1966 :
1967 1865552 : if ((q1 & q2) == q2)
1968 : return 1;
1969 162662 : else if ((q1 & q2) == q1)
1970 162083 : return -1;
1971 :
1972 : return 0;
1973 : }
1974 :
1975 : int
1976 0 : comp_cv_qualification (const_tree type1, const_tree type2)
1977 : {
1978 0 : int q1 = cp_type_quals (type1);
1979 0 : int q2 = cp_type_quals (type2);
1980 0 : return comp_cv_qualification (q1, q2);
1981 : }
1982 :
1983 : /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1984 : subset of the cv-qualification signature of TYPE2, and the types
1985 : are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1986 :
1987 : int
1988 305 : comp_cv_qual_signature (tree type1, tree type2)
1989 : {
1990 305 : if (comp_ptr_ttypes_real (type2, type1, -1))
1991 : return 1;
1992 266 : else if (comp_ptr_ttypes_real (type1, type2, -1))
1993 : return -1;
1994 : else
1995 255 : return 0;
1996 : }
1997 :
1998 : /* Subroutines of `comptypes'. */
1999 :
2000 : /* Return true if two parameter type lists PARMS1 and PARMS2 are
2001 : equivalent in the sense that functions with those parameter types
2002 : can have equivalent types. The two lists must be equivalent,
2003 : element by element. */
2004 :
2005 : bool
2006 467992229 : compparms (const_tree parms1, const_tree parms2)
2007 : {
2008 467992229 : const_tree t1, t2;
2009 :
2010 : /* An unspecified parmlist matches any specified parmlist
2011 : whose argument types don't need default promotions. */
2012 :
2013 467992229 : for (t1 = parms1, t2 = parms2;
2014 774191524 : t1 || t2;
2015 306199295 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2016 : {
2017 : /* If one parmlist is shorter than the other,
2018 : they fail to match. */
2019 732932384 : if (!t1 || !t2)
2020 : return false;
2021 732400026 : if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2022 : return false;
2023 : }
2024 : return true;
2025 : }
2026 :
2027 :
2028 : /* Process a sizeof or alignof expression where the operand is a type.
2029 : STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2030 : or GNU (preferred alignment) semantics; it is ignored if OP is
2031 : SIZEOF_EXPR. */
2032 :
2033 : tree
2034 16239601 : cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2035 : bool std_alignof, bool complain)
2036 : {
2037 16239601 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2038 16239601 : if (type == error_mark_node)
2039 : return error_mark_node;
2040 :
2041 16239599 : type = non_reference (type);
2042 16239599 : if (TREE_CODE (type) == METHOD_TYPE)
2043 : {
2044 0 : if (complain)
2045 : {
2046 0 : pedwarn (loc, OPT_Wpointer_arith,
2047 : "invalid application of %qs to a member function",
2048 0 : OVL_OP_INFO (false, op)->name);
2049 0 : return size_one_node;
2050 : }
2051 : else
2052 : return error_mark_node;
2053 : }
2054 16239599 : else if (VOID_TYPE_P (type) && std_alignof)
2055 : {
2056 12 : if (complain)
2057 12 : error_at (loc, "invalid application of %qs to a void type",
2058 12 : OVL_OP_INFO (false, op)->name);
2059 12 : return error_mark_node;
2060 : }
2061 :
2062 16239587 : bool dependent_p = dependent_type_p (type);
2063 16239587 : if (!dependent_p)
2064 13931745 : complete_type (type);
2065 13931745 : if (dependent_p
2066 : /* VLA types will have a non-constant size. In the body of an
2067 : uninstantiated template, we don't need to try to compute the
2068 : value, because the sizeof expression is not an integral
2069 : constant expression in that case. And, if we do try to
2070 : compute the value, we'll likely end up with SAVE_EXPRs, which
2071 : the template substitution machinery does not expect to see. */
2072 13931745 : || (processing_template_decl
2073 1188434 : && COMPLETE_TYPE_P (type)
2074 1188430 : && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2075 : {
2076 2307842 : tree value = build_min (op, size_type_node, type);
2077 2307842 : TREE_READONLY (value) = 1;
2078 2307842 : if (op == ALIGNOF_EXPR && std_alignof)
2079 252107 : ALIGNOF_EXPR_STD_P (value) = true;
2080 2307842 : SET_EXPR_LOCATION (value, loc);
2081 2307842 : return value;
2082 : }
2083 :
2084 13931745 : return c_sizeof_or_alignof_type (loc, complete_type (type),
2085 : op == SIZEOF_EXPR, std_alignof,
2086 13931745 : complain);
2087 : }
2088 :
2089 : /* Return the size of the type, without producing any warnings for
2090 : types whose size cannot be taken. This routine should be used only
2091 : in some other routine that has already produced a diagnostic about
2092 : using the size of such a type. */
2093 : tree
2094 1175106 : cxx_sizeof_nowarn (tree type)
2095 : {
2096 1175106 : if (TREE_CODE (type) == FUNCTION_TYPE
2097 1175106 : || VOID_TYPE_P (type)
2098 1174995 : || TREE_CODE (type) == ERROR_MARK)
2099 111 : return size_one_node;
2100 1174995 : else if (!COMPLETE_TYPE_P (type))
2101 24 : return size_zero_node;
2102 : else
2103 1174971 : return cxx_sizeof_or_alignof_type (input_location, type,
2104 1174971 : SIZEOF_EXPR, false, false);
2105 : }
2106 :
2107 : /* Process a sizeof expression where the operand is an expression. */
2108 :
2109 : static tree
2110 829088 : cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2111 : {
2112 829088 : if (e == error_mark_node)
2113 : return error_mark_node;
2114 :
2115 828156 : if (instantiation_dependent_uneval_expression_p (e))
2116 : {
2117 152670 : e = build_min (SIZEOF_EXPR, size_type_node, e);
2118 152670 : TREE_SIDE_EFFECTS (e) = 0;
2119 152670 : TREE_READONLY (e) = 1;
2120 152670 : SET_EXPR_LOCATION (e, loc);
2121 :
2122 152670 : return e;
2123 : }
2124 :
2125 675486 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2126 675486 : STRIP_ANY_LOCATION_WRAPPER (e);
2127 :
2128 : /* To get the size of a static data member declared as an array of
2129 : unknown bound, we need to instantiate it. */
2130 675486 : if (VAR_P (e)
2131 250201 : && VAR_HAD_UNKNOWN_BOUND (e)
2132 675584 : && DECL_TEMPLATE_INSTANTIATION (e))
2133 59 : instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
2134 :
2135 675486 : if (TREE_CODE (e) == PARM_DECL
2136 9478 : && DECL_ARRAY_PARAMETER_P (e)
2137 675946 : && (complain & tf_warning))
2138 : {
2139 140 : auto_diagnostic_group d;
2140 140 : if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2141 : "%<sizeof%> on array function parameter %qE "
2142 140 : "will return size of %qT", e, TREE_TYPE (e)))
2143 32 : inform (DECL_SOURCE_LOCATION (e), "declared here");
2144 140 : }
2145 :
2146 675486 : e = mark_type_use (e);
2147 :
2148 675486 : if (bitfield_p (e))
2149 : {
2150 14 : if (complain & tf_error)
2151 6 : error_at (e_loc,
2152 : "invalid application of %<sizeof%> to a bit-field");
2153 : else
2154 8 : return error_mark_node;
2155 6 : e = char_type_node;
2156 : }
2157 675472 : else if (is_overloaded_fn (e))
2158 : {
2159 44 : if (complain & tf_error)
2160 16 : permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2161 : "an expression of function type");
2162 : else
2163 28 : return error_mark_node;
2164 16 : e = char_type_node;
2165 : }
2166 675428 : else if (type_unknown_p (e))
2167 : {
2168 0 : if (complain & tf_error)
2169 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2170 : else
2171 0 : return error_mark_node;
2172 0 : e = char_type_node;
2173 : }
2174 : else
2175 675428 : e = TREE_TYPE (e);
2176 :
2177 675450 : return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2178 675450 : complain & tf_error);
2179 : }
2180 :
2181 : /* Implement the __alignof keyword: Return the minimum required
2182 : alignment of E, measured in bytes. For VAR_DECL's and
2183 : FIELD_DECL's return DECL_ALIGN (which can be set from an
2184 : "aligned" __attribute__ specification). STD_ALIGNOF acts
2185 : like in cxx_sizeof_or_alignof_type. */
2186 :
2187 : static tree
2188 111388 : cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2189 : tsubst_flags_t complain)
2190 : {
2191 111388 : tree t;
2192 :
2193 111388 : if (e == error_mark_node)
2194 : return error_mark_node;
2195 :
2196 111382 : if (processing_template_decl)
2197 : {
2198 21782 : e = build_min (ALIGNOF_EXPR, size_type_node, e);
2199 21782 : TREE_SIDE_EFFECTS (e) = 0;
2200 21782 : TREE_READONLY (e) = 1;
2201 21782 : SET_EXPR_LOCATION (e, loc);
2202 21782 : ALIGNOF_EXPR_STD_P (e) = std_alignof;
2203 :
2204 21782 : return e;
2205 : }
2206 :
2207 89600 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2208 89600 : STRIP_ANY_LOCATION_WRAPPER (e);
2209 :
2210 89600 : e = mark_type_use (e);
2211 :
2212 89600 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2213 89600 : !(complain & tf_error)))
2214 : {
2215 0 : if (!(complain & tf_error))
2216 0 : return error_mark_node;
2217 0 : t = size_one_node;
2218 : }
2219 89600 : else if (VAR_P (e))
2220 12279 : t = size_int (DECL_ALIGN_UNIT (e));
2221 77321 : else if (bitfield_p (e))
2222 : {
2223 8 : if (complain & tf_error)
2224 8 : error_at (e_loc,
2225 : "invalid application of %<__alignof%> to a bit-field");
2226 : else
2227 0 : return error_mark_node;
2228 8 : t = size_one_node;
2229 : }
2230 77313 : else if (TREE_CODE (e) == COMPONENT_REF
2231 77313 : && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2232 45554 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2233 31759 : else if (is_overloaded_fn (e))
2234 : {
2235 8 : if (complain & tf_error)
2236 8 : permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2237 : "an expression of function type");
2238 : else
2239 0 : return error_mark_node;
2240 8 : if (TREE_CODE (e) == FUNCTION_DECL)
2241 4 : t = size_int (DECL_ALIGN_UNIT (e));
2242 : else
2243 4 : t = size_one_node;
2244 : }
2245 31751 : else if (type_unknown_p (e))
2246 : {
2247 0 : if (complain & tf_error)
2248 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2249 : else
2250 0 : return error_mark_node;
2251 0 : t = size_one_node;
2252 : }
2253 : else
2254 31751 : return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2255 : ALIGNOF_EXPR, std_alignof,
2256 31751 : complain & tf_error);
2257 :
2258 57849 : return fold_convert_loc (loc, size_type_node, t);
2259 : }
2260 :
2261 : /* Process a sizeof or alignof expression E with code OP where the operand
2262 : is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2263 :
2264 : tree
2265 940476 : cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2266 : bool std_alignof, bool complain)
2267 : {
2268 940476 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2269 940476 : if (op == SIZEOF_EXPR)
2270 1263172 : return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2271 : else
2272 111415 : return cxx_alignof_expr (loc, e, std_alignof,
2273 111388 : complain? tf_warning_or_error : tf_none);
2274 : }
2275 :
2276 : /* Build a representation of an expression 'alignas(E).' Return the
2277 : folded integer value of E if it is an integral constant expression
2278 : that resolves to a valid alignment. If E depends on a template
2279 : parameter, return a syntactic representation tree of kind
2280 : ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2281 : expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2282 :
2283 : tree
2284 108055 : cxx_alignas_expr (tree e)
2285 : {
2286 108055 : if (e == NULL_TREE || e == error_mark_node
2287 216110 : || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2288 0 : return e;
2289 :
2290 108055 : if (TYPE_P (e))
2291 : /* [dcl.align]/3:
2292 :
2293 : When the alignment-specifier is of the form
2294 : alignas(type-id), it shall have the same effect as
2295 : alignas(alignof(type-id)). */
2296 :
2297 85247 : return cxx_sizeof_or_alignof_type (input_location,
2298 : e, ALIGNOF_EXPR,
2299 : /*std_alignof=*/true,
2300 85247 : /*complain=*/true);
2301 :
2302 : /* If we reach this point, it means the alignas expression if of
2303 : the form "alignas(assignment-expression)", so we should follow
2304 : what is stated by [dcl.align]/2. */
2305 :
2306 22808 : if (value_dependent_expression_p (e))
2307 : /* Leave value-dependent expression alone for now. */
2308 : return e;
2309 :
2310 887 : e = instantiate_non_dependent_expr (e);
2311 887 : e = mark_rvalue_use (e);
2312 :
2313 : /* [dcl.align]/2 says:
2314 :
2315 : the assignment-expression shall be an integral constant
2316 : expression. */
2317 :
2318 887 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2319 : {
2320 6 : error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2321 6 : return error_mark_node;
2322 : }
2323 :
2324 881 : return cxx_constant_value (e);
2325 : }
2326 :
2327 :
2328 : /* EXPR is being used in a context that is not a function call.
2329 : Enforce:
2330 :
2331 : [expr.ref]
2332 :
2333 : The expression can be used only as the left-hand operand of a
2334 : member function call.
2335 :
2336 : [expr.mptr.operator]
2337 :
2338 : If the result of .* or ->* is a function, then that result can be
2339 : used only as the operand for the function call operator ().
2340 :
2341 : by issuing an error message if appropriate. Returns true iff EXPR
2342 : violates these rules. */
2343 :
2344 : bool
2345 669722118 : invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2346 : {
2347 669722118 : if (expr == NULL_TREE)
2348 : return false;
2349 : /* Don't enforce this in MS mode. */
2350 669720903 : if (flag_ms_extensions)
2351 : return false;
2352 669714971 : if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2353 50832809 : expr = get_first_fn (expr);
2354 669714971 : if (TREE_TYPE (expr)
2355 669714971 : && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
2356 : {
2357 107 : if (complain & tf_error)
2358 : {
2359 103 : if (DECL_P (expr))
2360 : {
2361 81 : error_at (loc, "invalid use of non-static member function %qD",
2362 : expr);
2363 81 : inform (DECL_SOURCE_LOCATION (expr), "declared here");
2364 : }
2365 : else
2366 22 : error_at (loc, "invalid use of non-static member function of "
2367 22 : "type %qT", TREE_TYPE (expr));
2368 : }
2369 107 : return true;
2370 : }
2371 : return false;
2372 : }
2373 :
2374 : /* If EXP is a reference to a bit-field, and the type of EXP does not
2375 : match the declared type of the bit-field, return the declared type
2376 : of the bit-field. Otherwise, return NULL_TREE. */
2377 :
2378 : tree
2379 1515751872 : is_bitfield_expr_with_lowered_type (const_tree exp)
2380 : {
2381 2135555870 : switch (TREE_CODE (exp))
2382 : {
2383 2644931 : case COND_EXPR:
2384 5289862 : if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2385 2644924 : ? TREE_OPERAND (exp, 1)
2386 7 : : TREE_OPERAND (exp, 0)))
2387 : return NULL_TREE;
2388 159 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2389 :
2390 414931 : case COMPOUND_EXPR:
2391 414931 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2392 :
2393 193528817 : case MODIFY_EXPR:
2394 193528817 : case SAVE_EXPR:
2395 193528817 : case UNARY_PLUS_EXPR:
2396 193528817 : case PREDECREMENT_EXPR:
2397 193528817 : case PREINCREMENT_EXPR:
2398 193528817 : case POSTDECREMENT_EXPR:
2399 193528817 : case POSTINCREMENT_EXPR:
2400 193528817 : case NEGATE_EXPR:
2401 193528817 : case NON_LVALUE_EXPR:
2402 193528817 : case BIT_NOT_EXPR:
2403 193528817 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2404 :
2405 82673055 : case COMPONENT_REF:
2406 82673055 : {
2407 82673055 : tree field;
2408 :
2409 82673055 : field = TREE_OPERAND (exp, 1);
2410 82673055 : if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2411 : return NULL_TREE;
2412 2086134 : if (same_type_ignoring_top_level_qualifiers_p
2413 2086134 : (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2414 : return NULL_TREE;
2415 1935251 : return DECL_BIT_FIELD_TYPE (field);
2416 : }
2417 :
2418 316662526 : case VAR_DECL:
2419 316662526 : if (DECL_HAS_VALUE_EXPR_P (exp))
2420 133688 : return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2421 133688 : (CONST_CAST_TREE (exp)));
2422 : return NULL_TREE;
2423 :
2424 431125654 : case VIEW_CONVERT_EXPR:
2425 431125654 : if (location_wrapper_p (exp))
2426 425726403 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2427 : else
2428 : return NULL_TREE;
2429 :
2430 : default:
2431 : return NULL_TREE;
2432 : }
2433 : }
2434 :
2435 : /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2436 : bitfield with a lowered type, the type of EXP is returned, rather
2437 : than NULL_TREE. */
2438 :
2439 : tree
2440 680022177 : unlowered_expr_type (const_tree exp)
2441 : {
2442 680022177 : tree type;
2443 680022177 : tree etype = TREE_TYPE (exp);
2444 :
2445 680022177 : type = is_bitfield_expr_with_lowered_type (exp);
2446 680022177 : if (type)
2447 1525500 : type = cp_build_qualified_type (type, cp_type_quals (etype));
2448 : else
2449 : type = etype;
2450 :
2451 680022177 : return type;
2452 : }
2453 :
2454 : /* Perform the conversions in [expr] that apply when an lvalue appears
2455 : in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2456 : function-to-pointer conversions. In addition, bitfield references are
2457 : converted to their declared types. Note that this function does not perform
2458 : the lvalue-to-rvalue conversion for class types. If you need that conversion
2459 : for class types, then you probably need to use force_rvalue.
2460 :
2461 : Although the returned value is being used as an rvalue, this
2462 : function does not wrap the returned expression in a
2463 : NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2464 : that the return value is no longer an lvalue. */
2465 :
2466 : tree
2467 554764888 : decay_conversion (tree exp,
2468 : tsubst_flags_t complain,
2469 : bool reject_builtin /* = true */)
2470 : {
2471 554764888 : tree type;
2472 554764888 : enum tree_code code;
2473 554764888 : location_t loc = cp_expr_loc_or_input_loc (exp);
2474 :
2475 554764888 : type = TREE_TYPE (exp);
2476 554764888 : if (type == error_mark_node)
2477 : return error_mark_node;
2478 :
2479 554764640 : exp = resolve_nondeduced_context_or_error (exp, complain);
2480 :
2481 554764640 : code = TREE_CODE (type);
2482 :
2483 554764640 : if (error_operand_p (exp))
2484 117 : return error_mark_node;
2485 :
2486 554764523 : if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2487 : {
2488 960927 : mark_rvalue_use (exp, loc, reject_builtin);
2489 960927 : return nullptr_node;
2490 : }
2491 :
2492 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2493 : Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2494 553803596 : if (code == VOID_TYPE)
2495 : {
2496 5682 : if (complain & tf_error)
2497 4 : error_at (loc, "void value not ignored as it ought to be");
2498 5682 : return error_mark_node;
2499 : }
2500 553797914 : if (invalid_nonstatic_memfn_p (loc, exp, complain))
2501 4 : return error_mark_node;
2502 553797910 : if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2503 : {
2504 51050670 : exp = mark_lvalue_use (exp);
2505 51050670 : if (reject_builtin && reject_gcc_builtin (exp, loc))
2506 147 : return error_mark_node;
2507 51050523 : return cp_build_addr_expr (exp, complain);
2508 : }
2509 502747240 : if (code == ARRAY_TYPE)
2510 : {
2511 2457153 : tree adr;
2512 2457153 : tree ptrtype;
2513 :
2514 2457153 : exp = mark_lvalue_use (exp);
2515 :
2516 2457153 : if (INDIRECT_REF_P (exp))
2517 31881 : return build_nop (build_pointer_type (TREE_TYPE (type)),
2518 63762 : TREE_OPERAND (exp, 0));
2519 :
2520 2425272 : if (TREE_CODE (exp) == COMPOUND_EXPR)
2521 : {
2522 4 : tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2523 4 : if (op1 == error_mark_node)
2524 : return error_mark_node;
2525 4 : return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2526 8 : TREE_OPERAND (exp, 0), op1);
2527 : }
2528 :
2529 2425268 : if (!obvalue_p (exp)
2530 2425268 : && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2531 : {
2532 0 : if (complain & tf_error)
2533 0 : error_at (loc, "invalid use of non-lvalue array");
2534 0 : return error_mark_node;
2535 : }
2536 :
2537 : /* Don't let an array compound literal decay to a pointer. It can
2538 : still be used to initialize an array or bind to a reference. */
2539 2425268 : if (TREE_CODE (exp) == TARGET_EXPR)
2540 : {
2541 1 : if (complain & tf_error)
2542 1 : error_at (loc, "taking address of temporary array");
2543 1 : return error_mark_node;
2544 : }
2545 :
2546 2425267 : ptrtype = build_pointer_type (TREE_TYPE (type));
2547 :
2548 2425267 : if (VAR_P (exp))
2549 : {
2550 335008 : if (!cxx_mark_addressable (exp))
2551 0 : return error_mark_node;
2552 335008 : adr = build_nop (ptrtype, build_address (exp));
2553 335008 : return adr;
2554 : }
2555 : /* This way is better for a COMPONENT_REF since it can
2556 : simplify the offset for a component. */
2557 4180518 : adr = cp_build_addr_expr (exp, complain);
2558 2090259 : return cp_convert (ptrtype, adr, complain);
2559 : }
2560 :
2561 : /* Otherwise, it's the lvalue-to-rvalue conversion. */
2562 500290087 : exp = mark_rvalue_use (exp, loc, reject_builtin);
2563 :
2564 : /* If a bitfield is used in a context where integral promotion
2565 : applies, then the caller is expected to have used
2566 : default_conversion. That function promotes bitfields correctly
2567 : before calling this function. At this point, if we have a
2568 : bitfield referenced, we may assume that is not subject to
2569 : promotion, and that, therefore, the type of the resulting rvalue
2570 : is the declared type of the bitfield. */
2571 500290087 : exp = convert_bitfield_to_declared_type (exp);
2572 :
2573 : /* We do not call rvalue() here because we do not want to wrap EXP
2574 : in a NON_LVALUE_EXPR. */
2575 :
2576 : /* [basic.lval]
2577 :
2578 : Non-class rvalues always have cv-unqualified types. */
2579 500290087 : type = TREE_TYPE (exp);
2580 500290087 : if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2581 158563074 : exp = build_nop (cv_unqualified (type), exp);
2582 :
2583 500290087 : if (!complete_type_or_maybe_complain (type, exp, complain))
2584 54 : return error_mark_node;
2585 :
2586 : return exp;
2587 : }
2588 :
2589 : /* Perform preparatory conversions, as part of the "usual arithmetic
2590 : conversions". In particular, as per [expr]:
2591 :
2592 : Whenever an lvalue expression appears as an operand of an
2593 : operator that expects the rvalue for that operand, the
2594 : lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2595 : standard conversions are applied to convert the expression to an
2596 : rvalue.
2597 :
2598 : In addition, we perform integral promotions here, as those are
2599 : applied to both operands to a binary operator before determining
2600 : what additional conversions should apply. */
2601 :
2602 : static tree
2603 165790090 : cp_default_conversion (tree exp, tsubst_flags_t complain)
2604 : {
2605 : /* Check for target-specific promotions. */
2606 165790090 : tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2607 165790090 : if (promoted_type)
2608 0 : exp = cp_convert (promoted_type, exp, complain);
2609 : /* Perform the integral promotions first so that bitfield
2610 : expressions (which may promote to "int", even if the bitfield is
2611 : declared "unsigned") are promoted correctly. */
2612 165790090 : else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2613 106717942 : exp = cp_perform_integral_promotions (exp, complain);
2614 : /* Perform the other conversions. */
2615 165790090 : exp = decay_conversion (exp, complain);
2616 :
2617 165790090 : return exp;
2618 : }
2619 :
2620 : /* C version. */
2621 :
2622 : tree
2623 21619125 : default_conversion (tree exp)
2624 : {
2625 21619125 : return cp_default_conversion (exp, tf_warning_or_error);
2626 : }
2627 :
2628 : /* EXPR is an expression with an integral or enumeration type.
2629 : Perform the integral promotions in [conv.prom], and return the
2630 : converted value. */
2631 :
2632 : tree
2633 112383430 : cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2634 : {
2635 112383430 : tree type;
2636 112383430 : tree promoted_type;
2637 :
2638 112383430 : expr = mark_rvalue_use (expr);
2639 112383430 : if (error_operand_p (expr))
2640 3 : return error_mark_node;
2641 :
2642 112383427 : type = TREE_TYPE (expr);
2643 :
2644 : /* [conv.prom]
2645 :
2646 : A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2647 : of type int if int can represent all the values of the bit-field;
2648 : otherwise, it can be converted to unsigned int if unsigned int can
2649 : represent all the values of the bit-field. If the bit-field is larger yet,
2650 : no integral promotion applies to it. If the bit-field has an enumerated
2651 : type, it is treated as any other value of that type for promotion
2652 : purposes. */
2653 112383427 : tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2654 112383427 : if (bitfield_type
2655 112383427 : && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2656 75720 : || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2657 : type = bitfield_type;
2658 :
2659 112383427 : gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2660 : /* Scoped enums don't promote. */
2661 112383427 : if (SCOPED_ENUM_P (type))
2662 : return expr;
2663 112097140 : promoted_type = type_promotes_to (type);
2664 112097140 : if (type != promoted_type)
2665 30390428 : expr = cp_convert (promoted_type, expr, complain);
2666 81706712 : else if (bitfield_type && bitfield_type != type)
2667 : /* Prevent decay_conversion from converting to bitfield_type. */
2668 159 : expr = build_nop (type, expr);
2669 : return expr;
2670 : }
2671 :
2672 : /* C version. */
2673 :
2674 : tree
2675 724467 : perform_integral_promotions (tree expr)
2676 : {
2677 724467 : return cp_perform_integral_promotions (expr, tf_warning_or_error);
2678 : }
2679 :
2680 : /* Returns nonzero iff exp is a STRING_CST or the result of applying
2681 : decay_conversion to one. */
2682 :
2683 : int
2684 2087261 : string_conv_p (const_tree totype, const_tree exp, int warn)
2685 : {
2686 2087261 : tree t;
2687 :
2688 2087261 : if (!TYPE_PTR_P (totype))
2689 : return 0;
2690 :
2691 2087197 : t = TREE_TYPE (totype);
2692 2087197 : if (!same_type_p (t, char_type_node)
2693 1973554 : && !same_type_p (t, char8_type_node)
2694 1968485 : && !same_type_p (t, char16_type_node)
2695 1926284 : && !same_type_p (t, char32_type_node)
2696 3971282 : && !same_type_p (t, wchar_type_node))
2697 : return 0;
2698 :
2699 232013 : location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2700 :
2701 232013 : STRIP_ANY_LOCATION_WRAPPER (exp);
2702 :
2703 232013 : if (TREE_CODE (exp) == STRING_CST)
2704 : {
2705 : /* Make sure that we don't try to convert between char and wide chars. */
2706 119 : if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2707 : return 0;
2708 : }
2709 : else
2710 : {
2711 : /* Is this a string constant which has decayed to 'const char *'? */
2712 231894 : t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2713 231894 : if (!same_type_p (TREE_TYPE (exp), t))
2714 : return 0;
2715 44022 : STRIP_NOPS (exp);
2716 44022 : if (TREE_CODE (exp) != ADDR_EXPR
2717 44022 : || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2718 : return 0;
2719 : }
2720 347 : if (warn)
2721 : {
2722 112 : if (cxx_dialect >= cxx11)
2723 96 : pedwarn (loc, OPT_Wwrite_strings,
2724 : "ISO C++ forbids converting a string constant to %qT",
2725 : totype);
2726 : else
2727 16 : warning_at (loc, OPT_Wwrite_strings,
2728 : "deprecated conversion from string constant to %qT",
2729 : totype);
2730 : }
2731 :
2732 : return 1;
2733 : }
2734 :
2735 : /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2736 : can, for example, use as an lvalue. This code used to be in
2737 : unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2738 : expressions, where we're dealing with aggregates. But now it's again only
2739 : called from unary_complex_lvalue. The case (in particular) that led to
2740 : this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2741 : get it there. */
2742 :
2743 : static tree
2744 23044 : rationalize_conditional_expr (enum tree_code code, tree t,
2745 : tsubst_flags_t complain)
2746 : {
2747 23044 : location_t loc = cp_expr_loc_or_input_loc (t);
2748 :
2749 : /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2750 : the first operand is always the one to be used if both operands
2751 : are equal, so we know what conditional expression this used to be. */
2752 23044 : if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2753 : {
2754 0 : tree op0 = TREE_OPERAND (t, 0);
2755 0 : tree op1 = TREE_OPERAND (t, 1);
2756 :
2757 : /* The following code is incorrect if either operand side-effects. */
2758 0 : gcc_assert (!TREE_SIDE_EFFECTS (op0)
2759 : && !TREE_SIDE_EFFECTS (op1));
2760 0 : return
2761 0 : build_conditional_expr (loc,
2762 : build_x_binary_op (loc,
2763 0 : (TREE_CODE (t) == MIN_EXPR
2764 : ? LE_EXPR : GE_EXPR),
2765 0 : op0, TREE_CODE (op0),
2766 0 : op1, TREE_CODE (op1),
2767 : NULL_TREE,
2768 : /*overload=*/NULL,
2769 : complain),
2770 : cp_build_unary_op (code, op0, false, complain),
2771 : cp_build_unary_op (code, op1, false, complain),
2772 : complain);
2773 : }
2774 :
2775 23044 : tree op1 = TREE_OPERAND (t, 1);
2776 23044 : if (TREE_CODE (op1) != THROW_EXPR)
2777 23040 : op1 = cp_build_unary_op (code, op1, false, complain);
2778 23044 : tree op2 = TREE_OPERAND (t, 2);
2779 23044 : if (TREE_CODE (op2) != THROW_EXPR)
2780 23037 : op2 = cp_build_unary_op (code, op2, false, complain);
2781 :
2782 23044 : return
2783 23044 : build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2784 : }
2785 :
2786 : /* Given the TYPE of an anonymous union field inside T, return the
2787 : FIELD_DECL for the field. If not found return NULL_TREE. Because
2788 : anonymous unions can nest, we must also search all anonymous unions
2789 : that are directly reachable. */
2790 :
2791 : tree
2792 523680 : lookup_anon_field (tree, tree type)
2793 : {
2794 523680 : tree field;
2795 :
2796 523680 : type = TYPE_MAIN_VARIANT (type);
2797 523680 : field = ANON_AGGR_TYPE_FIELD (type);
2798 523680 : gcc_assert (field);
2799 523680 : return field;
2800 : }
2801 :
2802 : /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2803 : expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2804 : non-NULL, it indicates the path to the base used to name MEMBER.
2805 : If PRESERVE_REFERENCE is true, the expression returned will have
2806 : REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2807 : returned will have the type referred to by the reference.
2808 :
2809 : This function does not perform access control; that is either done
2810 : earlier by the parser when the name of MEMBER is resolved to MEMBER
2811 : itself, or later when overload resolution selects one of the
2812 : functions indicated by MEMBER. */
2813 :
2814 : tree
2815 59799517 : build_class_member_access_expr (cp_expr object, tree member,
2816 : tree access_path, bool preserve_reference,
2817 : tsubst_flags_t complain)
2818 : {
2819 59799517 : tree object_type;
2820 59799517 : tree member_scope;
2821 59799517 : tree result = NULL_TREE;
2822 59799517 : tree using_decl = NULL_TREE;
2823 :
2824 59799517 : if (error_operand_p (object) || error_operand_p (member))
2825 49 : return error_mark_node;
2826 :
2827 59799468 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2828 :
2829 : /* [expr.ref]
2830 :
2831 : The type of the first expression shall be "class object" (of a
2832 : complete type). */
2833 59799468 : object_type = TREE_TYPE (object);
2834 59799468 : if (!currently_open_class (object_type)
2835 59799468 : && !complete_type_or_maybe_complain (object_type, object, complain))
2836 0 : return error_mark_node;
2837 59799468 : if (!CLASS_TYPE_P (object_type))
2838 : {
2839 0 : if (complain & tf_error)
2840 : {
2841 0 : if (INDIRECT_TYPE_P (object_type)
2842 0 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
2843 0 : error ("request for member %qD in %qE, which is of pointer "
2844 : "type %qT (maybe you meant to use %<->%> ?)",
2845 : member, object.get_value (), object_type);
2846 : else
2847 0 : error ("request for member %qD in %qE, which is of non-class "
2848 : "type %qT", member, object.get_value (), object_type);
2849 : }
2850 0 : return error_mark_node;
2851 : }
2852 :
2853 : /* The standard does not seem to actually say that MEMBER must be a
2854 : member of OBJECT_TYPE. However, that is clearly what is
2855 : intended. */
2856 59799468 : if (DECL_P (member))
2857 : {
2858 24811639 : member_scope = DECL_CLASS_CONTEXT (member);
2859 24811639 : if (!mark_used (member, complain) && !(complain & tf_error))
2860 0 : return error_mark_node;
2861 :
2862 24811639 : if (TREE_UNAVAILABLE (member))
2863 40 : error_unavailable_use (member, NULL_TREE);
2864 24811599 : else if (TREE_DEPRECATED (member))
2865 40 : warn_deprecated_use (member, NULL_TREE);
2866 : }
2867 : else
2868 34987829 : member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2869 : /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2870 : presently be the anonymous union. Go outwards until we find a
2871 : type related to OBJECT_TYPE. */
2872 60326733 : while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2873 60854409 : && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2874 : object_type))
2875 527265 : member_scope = TYPE_CONTEXT (member_scope);
2876 59799468 : if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2877 : {
2878 4 : if (complain & tf_error)
2879 : {
2880 4 : if (TREE_CODE (member) == FIELD_DECL)
2881 0 : error ("invalid use of non-static data member %qE", member);
2882 : else
2883 4 : error ("%qD is not a member of %qT", member, object_type);
2884 : }
2885 4 : return error_mark_node;
2886 : }
2887 :
2888 : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2889 : `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2890 : in the front end; only _DECLs and _REFs are lvalues in the back end. */
2891 59799464 : if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2892 : {
2893 42 : temp = cp_build_fold_indirect_ref (temp);
2894 42 : if (!lvalue_p (object) && lvalue_p (temp))
2895 : /* Preserve rvalueness. */
2896 6 : temp = move (temp);
2897 42 : object = temp;
2898 : }
2899 :
2900 : /* In [expr.ref], there is an explicit list of the valid choices for
2901 : MEMBER. We check for each of those cases here. */
2902 59799464 : if (VAR_P (member))
2903 : {
2904 : /* A static data member. */
2905 6181 : result = member;
2906 6181 : mark_exp_read (object);
2907 :
2908 6181 : if (tree wrap = maybe_get_tls_wrapper_call (result))
2909 : /* Replace an evaluated use of the thread_local variable with
2910 : a call to its wrapper. */
2911 102 : result = wrap;
2912 :
2913 : /* If OBJECT has side-effects, they are supposed to occur. */
2914 6181 : if (TREE_SIDE_EFFECTS (object))
2915 49 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2916 : }
2917 59793283 : else if (TREE_CODE (member) == FIELD_DECL)
2918 : {
2919 : /* A non-static data member. */
2920 24805362 : bool null_object_p;
2921 24805362 : int type_quals;
2922 24805362 : tree member_type;
2923 :
2924 24805362 : if (INDIRECT_REF_P (object))
2925 13188635 : null_object_p =
2926 13188635 : integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2927 : else
2928 : null_object_p = false;
2929 :
2930 : /* Convert OBJECT to the type of MEMBER. */
2931 24805362 : if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2932 : TYPE_MAIN_VARIANT (member_scope)))
2933 : {
2934 1134109 : tree binfo;
2935 1134109 : base_kind kind;
2936 :
2937 : /* We didn't complain above about a currently open class, but now we
2938 : must: we don't know how to refer to a base member before layout is
2939 : complete. But still don't complain in a template. */
2940 1134109 : if (!cp_unevaluated_operand
2941 1134013 : && !dependent_type_p (object_type)
2942 2061654 : && !complete_type_or_maybe_complain (object_type, object,
2943 : complain))
2944 12 : return error_mark_node;
2945 :
2946 1437635 : binfo = lookup_base (access_path ? access_path : object_type,
2947 : member_scope, ba_unique, &kind, complain);
2948 1134105 : if (binfo == error_mark_node)
2949 : return error_mark_node;
2950 :
2951 : /* It is invalid to try to get to a virtual base of a
2952 : NULL object. The most common cause is invalid use of
2953 : offsetof macro. */
2954 1134105 : if (null_object_p && kind == bk_via_virtual)
2955 : {
2956 8 : if (complain & tf_error)
2957 : {
2958 8 : error ("invalid access to non-static data member %qD in "
2959 : "virtual base of NULL object", member);
2960 : }
2961 8 : return error_mark_node;
2962 : }
2963 :
2964 : /* Convert to the base. */
2965 1134097 : object = build_base_path (PLUS_EXPR, object, binfo,
2966 : /*nonnull=*/1, complain);
2967 : /* If we found the base successfully then we should be able
2968 : to convert to it successfully. */
2969 1134097 : gcc_assert (object != error_mark_node);
2970 : }
2971 :
2972 : /* If MEMBER is from an anonymous aggregate, we have converted
2973 : OBJECT so that it refers to the class containing the
2974 : anonymous union. Generate a reference to the anonymous union
2975 : itself, and recur to find MEMBER. */
2976 49610700 : if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2977 : /* When this code is called from build_field_call, the
2978 : object already has the type of the anonymous union.
2979 : That is because the COMPONENT_REF was already
2980 : constructed, and was then disassembled before calling
2981 : build_field_call. After the function-call code is
2982 : cleaned up, this waste can be eliminated. */
2983 25329383 : && (!same_type_ignoring_top_level_qualifiers_p
2984 524033 : (TREE_TYPE (object), DECL_CONTEXT (member))))
2985 : {
2986 523625 : tree anonymous_union;
2987 :
2988 523625 : anonymous_union = lookup_anon_field (TREE_TYPE (object),
2989 523625 : DECL_CONTEXT (member));
2990 523625 : object = build_class_member_access_expr (object,
2991 : anonymous_union,
2992 : /*access_path=*/NULL_TREE,
2993 : preserve_reference,
2994 : complain);
2995 : }
2996 :
2997 : /* Compute the type of the field, as described in [expr.ref]. */
2998 24805350 : type_quals = TYPE_UNQUALIFIED;
2999 24805350 : member_type = TREE_TYPE (member);
3000 24805350 : if (!TYPE_REF_P (member_type))
3001 : {
3002 24586888 : type_quals = (cp_type_quals (member_type)
3003 24586888 : | cp_type_quals (object_type));
3004 :
3005 : /* A field is const (volatile) if the enclosing object, or the
3006 : field itself, is const (volatile). But, a mutable field is
3007 : not const, even within a const object. */
3008 24586888 : if (DECL_MUTABLE_P (member))
3009 234136 : type_quals &= ~TYPE_QUAL_CONST;
3010 24586888 : member_type = cp_build_qualified_type (member_type, type_quals);
3011 : }
3012 :
3013 24805350 : result = build3_loc (input_location, COMPONENT_REF, member_type,
3014 : object, member, NULL_TREE);
3015 :
3016 : /* Mark the expression const or volatile, as appropriate. Even
3017 : though we've dealt with the type above, we still have to mark the
3018 : expression itself. */
3019 24805350 : if (type_quals & TYPE_QUAL_CONST)
3020 7429541 : TREE_READONLY (result) = 1;
3021 24805350 : if (type_quals & TYPE_QUAL_VOLATILE)
3022 141713 : TREE_THIS_VOLATILE (result) = 1;
3023 : }
3024 34987921 : else if (BASELINK_P (member))
3025 : {
3026 : /* The member is a (possibly overloaded) member function. */
3027 34987825 : tree functions;
3028 34987825 : tree type;
3029 :
3030 : /* If the MEMBER is exactly one static member function, then we
3031 : know the type of the expression. Otherwise, we must wait
3032 : until overload resolution has been performed. */
3033 34987825 : functions = BASELINK_FUNCTIONS (member);
3034 34987825 : if (TREE_CODE (functions) == FUNCTION_DECL
3035 34987825 : && DECL_STATIC_FUNCTION_P (functions))
3036 275054 : type = TREE_TYPE (functions);
3037 : else
3038 34712771 : type = unknown_type_node;
3039 : /* Note that we do not convert OBJECT to the BASELINK_BINFO
3040 : base. That will happen when the function is called. */
3041 34987825 : result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3042 : NULL_TREE);
3043 : }
3044 96 : else if (TREE_CODE (member) == CONST_DECL)
3045 : {
3046 : /* The member is an enumerator. */
3047 82 : result = member;
3048 : /* If OBJECT has side-effects, they are supposed to occur. */
3049 82 : if (TREE_SIDE_EFFECTS (object))
3050 8 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3051 : object, result);
3052 : }
3053 14 : else if ((using_decl = strip_using_decl (member)) != member)
3054 0 : result = build_class_member_access_expr (object,
3055 : using_decl,
3056 : access_path, preserve_reference,
3057 : complain);
3058 : else
3059 : {
3060 14 : if (complain & tf_error)
3061 14 : error ("invalid use of %qD", member);
3062 14 : return error_mark_node;
3063 : }
3064 :
3065 59799438 : if (!preserve_reference)
3066 : /* [expr.ref]
3067 :
3068 : If E2 is declared to have type "reference to T", then ... the
3069 : type of E1.E2 is T. */
3070 56464518 : result = convert_from_reference (result);
3071 :
3072 : return result;
3073 : }
3074 :
3075 : /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3076 : SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3077 :
3078 : tree
3079 81389 : lookup_destructor (tree object, tree scope, tree dtor_name,
3080 : tsubst_flags_t complain)
3081 : {
3082 81389 : tree object_type = TREE_TYPE (object);
3083 81389 : tree dtor_type = TREE_OPERAND (dtor_name, 0);
3084 81389 : tree expr;
3085 :
3086 : /* We've already complained about this destructor. */
3087 81389 : if (dtor_type == error_mark_node)
3088 : return error_mark_node;
3089 :
3090 81381 : if (scope && !check_dtor_name (scope, dtor_type))
3091 : {
3092 4 : if (complain & tf_error)
3093 4 : error ("qualified type %qT does not match destructor name ~%qT",
3094 : scope, dtor_type);
3095 4 : return error_mark_node;
3096 : }
3097 81377 : if (is_auto (dtor_type))
3098 : dtor_type = object_type;
3099 81374 : else if (identifier_p (dtor_type))
3100 : {
3101 : /* In a template, names we can't find a match for are still accepted
3102 : destructor names, and we check them here. */
3103 19 : if (check_dtor_name (object_type, dtor_type))
3104 : dtor_type = object_type;
3105 : else
3106 : {
3107 4 : if (complain & tf_error)
3108 4 : error ("object type %qT does not match destructor name ~%qT",
3109 : object_type, dtor_type);
3110 4 : return error_mark_node;
3111 : }
3112 :
3113 : }
3114 81355 : else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3115 : {
3116 8 : if (complain & tf_error)
3117 8 : error ("the type being destroyed is %qT, but the destructor "
3118 8 : "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3119 8 : return error_mark_node;
3120 : }
3121 81365 : expr = lookup_member (dtor_type, complete_dtor_identifier,
3122 : /*protect=*/1, /*want_type=*/false,
3123 : tf_warning_or_error);
3124 81365 : if (!expr)
3125 : {
3126 6 : if (complain & tf_error)
3127 6 : cxx_incomplete_type_error (dtor_name, dtor_type);
3128 6 : return error_mark_node;
3129 : }
3130 81359 : expr = (adjust_result_of_qualified_name_lookup
3131 81359 : (expr, dtor_type, object_type));
3132 81359 : if (scope == NULL_TREE)
3133 : /* We need to call adjust_result_of_qualified_name_lookup in case the
3134 : destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3135 : that we still get virtual function binding. */
3136 81188 : BASELINK_QUALIFIED_P (expr) = false;
3137 : return expr;
3138 : }
3139 :
3140 : /* An expression of the form "A::template B" has been resolved to
3141 : DECL. Issue a diagnostic if B is not a template or template
3142 : specialization. */
3143 :
3144 : void
3145 3819110 : check_template_keyword (tree decl)
3146 : {
3147 : /* The standard says:
3148 :
3149 : [temp.names]
3150 :
3151 : If a name prefixed by the keyword template is not a member
3152 : template, the program is ill-formed.
3153 :
3154 : DR 228 removed the restriction that the template be a member
3155 : template.
3156 :
3157 : DR 96, if accepted would add the further restriction that explicit
3158 : template arguments must be provided if the template keyword is
3159 : used, but, as of 2005-10-16, that DR is still in "drafting". If
3160 : this DR is accepted, then the semantic checks here can be
3161 : simplified, as the entity named must in fact be a template
3162 : specialization, rather than, as at present, a set of overloaded
3163 : functions containing at least one template function. */
3164 3819110 : if (TREE_CODE (decl) != TEMPLATE_DECL
3165 3819110 : && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3166 : {
3167 3617137 : if (VAR_P (decl))
3168 : {
3169 9502 : if (DECL_USE_TEMPLATE (decl)
3170 9502 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3171 : ;
3172 : else
3173 0 : permerror (input_location, "%qD is not a template", decl);
3174 : }
3175 3607635 : else if (!is_overloaded_fn (decl))
3176 0 : permerror (input_location, "%qD is not a template", decl);
3177 : else
3178 : {
3179 3607635 : bool found = false;
3180 :
3181 3607635 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3182 7215273 : !found && iter; ++iter)
3183 : {
3184 3607638 : tree fn = *iter;
3185 3607638 : if (TREE_CODE (fn) == TEMPLATE_DECL
3186 3607638 : || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3187 3607638 : || (TREE_CODE (fn) == FUNCTION_DECL
3188 19 : && DECL_USE_TEMPLATE (fn)
3189 8 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3190 : found = true;
3191 : }
3192 3607635 : if (!found)
3193 16 : permerror (input_location, "%qD is not a template", decl);
3194 : }
3195 : }
3196 3819110 : }
3197 :
3198 : /* Record that an access failure occurred on BASETYPE_PATH attempting
3199 : to access DECL, where DIAG_DECL should be used for diagnostics. */
3200 :
3201 : void
3202 425 : access_failure_info::record_access_failure (tree basetype_path,
3203 : tree decl, tree diag_decl)
3204 : {
3205 425 : m_was_inaccessible = true;
3206 425 : m_basetype_path = basetype_path;
3207 425 : m_decl = decl;
3208 425 : m_diag_decl = diag_decl;
3209 425 : }
3210 :
3211 : /* If an access failure was recorded, then attempt to locate an
3212 : accessor function for the pertinent field.
3213 : Otherwise, return NULL_TREE. */
3214 :
3215 : tree
3216 53213151 : access_failure_info::get_any_accessor (bool const_p) const
3217 : {
3218 53213151 : if (!was_inaccessible_p ())
3219 : return NULL_TREE;
3220 :
3221 425 : tree accessor
3222 425 : = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3223 425 : if (!accessor)
3224 : return NULL_TREE;
3225 :
3226 : /* The accessor must itself be accessible for it to be a reasonable
3227 : suggestion. */
3228 133 : if (!accessible_p (m_basetype_path, accessor, true))
3229 : return NULL_TREE;
3230 :
3231 : return accessor;
3232 : }
3233 :
3234 : /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3235 : replacing the primary location in RICHLOC with "accessor()". */
3236 :
3237 : void
3238 119 : access_failure_info::add_fixit_hint (rich_location *richloc,
3239 : tree accessor_decl)
3240 : {
3241 119 : pretty_printer pp;
3242 119 : pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3243 119 : pp_string (&pp, "()");
3244 119 : richloc->add_fixit_replace (pp_formatted_text (&pp));
3245 119 : }
3246 :
3247 : /* If an access failure was recorded, then attempt to locate an
3248 : accessor function for the pertinent field, and if one is
3249 : available, add a note and fix-it hint suggesting using it. */
3250 :
3251 : void
3252 53213116 : access_failure_info::maybe_suggest_accessor (bool const_p) const
3253 : {
3254 53213116 : tree accessor = get_any_accessor (const_p);
3255 53213116 : if (accessor == NULL_TREE)
3256 53213018 : return;
3257 98 : rich_location richloc (line_table, input_location);
3258 98 : add_fixit_hint (&richloc, accessor);
3259 98 : inform (&richloc, "field %q#D can be accessed via %q#D",
3260 98 : m_diag_decl, accessor);
3261 98 : }
3262 :
3263 : /* Subroutine of finish_class_member_access_expr.
3264 : Issue an error about NAME not being a member of ACCESS_PATH (or
3265 : OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3266 : names. */
3267 :
3268 : static void
3269 194 : complain_about_unrecognized_member (tree access_path, tree name,
3270 : tree object_type)
3271 : {
3272 : /* Attempt to provide a hint about misspelled names. */
3273 194 : tree guessed_id = lookup_member_fuzzy (access_path, name,
3274 : /*want_type=*/false);
3275 194 : if (guessed_id == NULL_TREE)
3276 : {
3277 : /* No hint. */
3278 106 : error ("%q#T has no member named %qE",
3279 106 : TREE_CODE (access_path) == TREE_BINFO
3280 4 : ? TREE_TYPE (access_path) : object_type, name);
3281 106 : return;
3282 : }
3283 :
3284 88 : location_t bogus_component_loc = input_location;
3285 88 : gcc_rich_location rich_loc (bogus_component_loc);
3286 :
3287 : /* Check that the guessed name is accessible along access_path. */
3288 88 : access_failure_info afi;
3289 88 : lookup_member (access_path, guessed_id, /*protect=*/1,
3290 : /*want_type=*/false, /*complain=*/false,
3291 : &afi);
3292 88 : if (afi.was_inaccessible_p ())
3293 : {
3294 35 : tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3295 35 : if (accessor)
3296 : {
3297 : /* The guessed name isn't directly accessible, but can be accessed
3298 : via an accessor member function. */
3299 21 : afi.add_fixit_hint (&rich_loc, accessor);
3300 21 : error_at (&rich_loc,
3301 : "%q#T has no member named %qE;"
3302 : " did you mean %q#D? (accessible via %q#D)",
3303 21 : TREE_CODE (access_path) == TREE_BINFO
3304 0 : ? TREE_TYPE (access_path) : object_type,
3305 : name, afi.get_diag_decl (), accessor);
3306 : }
3307 : else
3308 : {
3309 : /* The guessed name isn't directly accessible, and no accessor
3310 : member function could be found. */
3311 14 : error_at (&rich_loc,
3312 : "%q#T has no member named %qE;"
3313 : " did you mean %q#D? (not accessible from this context)",
3314 14 : TREE_CODE (access_path) == TREE_BINFO
3315 0 : ? TREE_TYPE (access_path) : object_type,
3316 : name, afi.get_diag_decl ());
3317 14 : complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3318 : afi.get_diag_decl (), false, ak_none);
3319 : }
3320 : }
3321 : else
3322 : {
3323 : /* The guessed name is directly accessible; suggest it. */
3324 53 : rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3325 : guessed_id);
3326 53 : error_at (&rich_loc,
3327 : "%q#T has no member named %qE;"
3328 : " did you mean %qE?",
3329 53 : TREE_CODE (access_path) == TREE_BINFO
3330 0 : ? TREE_TYPE (access_path) : object_type,
3331 : name, guessed_id);
3332 : }
3333 88 : }
3334 :
3335 : /* This function is called by the parser to process a class member
3336 : access expression of the form OBJECT.NAME. NAME is a node used by
3337 : the parser to represent a name; it is not yet a DECL. It may,
3338 : however, be a BASELINK where the BASELINK_FUNCTIONS is a
3339 : TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3340 : there is no reason to do the lookup twice, so the parser keeps the
3341 : BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3342 : be a template via the use of the "A::template B" syntax. */
3343 :
3344 : tree
3345 103367300 : finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3346 : tsubst_flags_t complain)
3347 : {
3348 103367300 : tree expr;
3349 103367300 : tree object_type;
3350 103367300 : tree member;
3351 103367300 : tree access_path = NULL_TREE;
3352 103367300 : tree orig_object = object;
3353 103367300 : tree orig_name = name;
3354 :
3355 103367300 : if (object == error_mark_node || name == error_mark_node)
3356 : return error_mark_node;
3357 :
3358 : /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3359 103366834 : if (!objc_is_public (object, name))
3360 0 : return error_mark_node;
3361 :
3362 103366834 : object_type = TREE_TYPE (object);
3363 :
3364 103366834 : if (processing_template_decl)
3365 : {
3366 89414592 : if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3367 89414592 : type_dependent_object_expression_p (object)
3368 : /* If NAME is "f<args>", where either 'f' or 'args' is
3369 : dependent, then the expression is dependent. */
3370 44049001 : || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3371 332795 : && dependent_template_id_p (TREE_OPERAND (name, 0),
3372 332795 : TREE_OPERAND (name, 1)))
3373 : /* If NAME is "T::X" where "T" is dependent, then the
3374 : expression is dependent. */
3375 43923434 : || (TREE_CODE (name) == SCOPE_REF
3376 79534 : && TYPE_P (TREE_OPERAND (name, 0))
3377 79534 : && dependent_scope_p (TREE_OPERAND (name, 0)))
3378 : /* If NAME is operator T where "T" is dependent, we can't
3379 : lookup until we instantiate the T. */
3380 133258823 : || (TREE_CODE (name) == IDENTIFIER_NODE
3381 43438480 : && IDENTIFIER_CONV_OP_P (name)
3382 43 : && dependent_type_p (TREE_TYPE (name))))
3383 : {
3384 54818719 : dependent:
3385 54818719 : return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3386 54818719 : orig_object, orig_name, NULL_TREE);
3387 : }
3388 43844192 : object = build_non_dependent_expr (object);
3389 : }
3390 13952242 : else if (c_dialect_objc ()
3391 57796434 : && identifier_p (name)
3392 13952242 : && (expr = objc_maybe_build_component_ref (object, name)))
3393 : return expr;
3394 :
3395 : /* [expr.ref]
3396 :
3397 : The type of the first expression shall be "class object" (of a
3398 : complete type). */
3399 57796434 : if (!currently_open_class (object_type)
3400 57796434 : && !complete_type_or_maybe_complain (object_type, object, complain))
3401 50 : return error_mark_node;
3402 57796384 : if (!CLASS_TYPE_P (object_type))
3403 : {
3404 3693 : if (complain & tf_error)
3405 : {
3406 82 : if (INDIRECT_TYPE_P (object_type)
3407 82 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
3408 28 : error ("request for member %qD in %qE, which is of pointer "
3409 : "type %qT (maybe you meant to use %<->%> ?)",
3410 : name, object.get_value (), object_type);
3411 : else
3412 54 : error ("request for member %qD in %qE, which is of non-class "
3413 : "type %qT", name, object.get_value (), object_type);
3414 : }
3415 3693 : return error_mark_node;
3416 : }
3417 :
3418 57792691 : if (BASELINK_P (name))
3419 : /* A member function that has already been looked up. */
3420 : member = name;
3421 : else
3422 : {
3423 53316066 : bool is_template_id = false;
3424 53316066 : tree template_args = NULL_TREE;
3425 53316066 : tree scope = NULL_TREE;
3426 :
3427 53316066 : access_path = object_type;
3428 :
3429 53316066 : if (TREE_CODE (name) == SCOPE_REF)
3430 : {
3431 : /* A qualified name. The qualifying class or namespace `S'
3432 : has already been looked up; it is either a TYPE or a
3433 : NAMESPACE_DECL. */
3434 2044 : scope = TREE_OPERAND (name, 0);
3435 2044 : name = TREE_OPERAND (name, 1);
3436 :
3437 : /* If SCOPE is a namespace, then the qualified name does not
3438 : name a member of OBJECT_TYPE. */
3439 2044 : if (TREE_CODE (scope) == NAMESPACE_DECL)
3440 : {
3441 0 : if (complain & tf_error)
3442 0 : error ("%<%D::%D%> is not a member of %qT",
3443 : scope, name, object_type);
3444 0 : return error_mark_node;
3445 : }
3446 : }
3447 :
3448 53316066 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3449 : {
3450 269009 : is_template_id = true;
3451 269009 : template_args = TREE_OPERAND (name, 1);
3452 269009 : name = TREE_OPERAND (name, 0);
3453 :
3454 532946 : if (!identifier_p (name))
3455 263955 : name = OVL_NAME (name);
3456 : }
3457 :
3458 53316066 : if (scope)
3459 : {
3460 2044 : if (TREE_CODE (scope) == ENUMERAL_TYPE)
3461 : {
3462 12 : gcc_assert (!is_template_id);
3463 : /* Looking up a member enumerator (c++/56793). */
3464 24 : if (!TYPE_CLASS_SCOPE_P (scope)
3465 21 : || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3466 : {
3467 3 : if (complain & tf_error)
3468 3 : error ("%<%D::%D%> is not a member of %qT",
3469 : scope, name, object_type);
3470 3 : return error_mark_node;
3471 : }
3472 9 : tree val = lookup_enumerator (scope, name);
3473 9 : if (!val)
3474 : {
3475 6 : if (complain & tf_error)
3476 6 : error ("%qD is not a member of %qD",
3477 : name, scope);
3478 6 : return error_mark_node;
3479 : }
3480 :
3481 3 : if (TREE_SIDE_EFFECTS (object))
3482 0 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3483 3 : return val;
3484 : }
3485 :
3486 2032 : gcc_assert (CLASS_TYPE_P (scope));
3487 2032 : gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3488 :
3489 2032 : if (constructor_name_p (name, scope))
3490 : {
3491 4 : if (complain & tf_error)
3492 4 : error ("cannot call constructor %<%T::%D%> directly",
3493 : scope, name);
3494 4 : return error_mark_node;
3495 : }
3496 :
3497 : /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3498 2028 : access_path = lookup_base (object_type, scope, ba_check,
3499 : NULL, complain);
3500 2028 : if (access_path == error_mark_node)
3501 : return error_mark_node;
3502 2016 : if (!access_path)
3503 : {
3504 15 : if (any_dependent_bases_p (object_type))
3505 7 : goto dependent;
3506 8 : if (complain & tf_error)
3507 8 : error ("%qT is not a base of %qT", scope, object_type);
3508 8 : return error_mark_node;
3509 : }
3510 : }
3511 :
3512 53316023 : if (TREE_CODE (name) == BIT_NOT_EXPR)
3513 : {
3514 102907 : if (dependent_type_p (object_type))
3515 : /* The destructor isn't declared yet. */
3516 21538 : goto dependent;
3517 81369 : member = lookup_destructor (object, scope, name, complain);
3518 : }
3519 : else
3520 : {
3521 : /* Look up the member. */
3522 53213116 : access_failure_info afi;
3523 53213116 : if (processing_template_decl)
3524 : /* Even though this class member access expression is at this
3525 : point not dependent, the member itself may be dependent, and
3526 : we must not potentially push a access check for a dependent
3527 : member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3528 : ahead of time here; we're going to redo this member lookup at
3529 : instantiation time anyway. */
3530 43645970 : push_deferring_access_checks (dk_no_check);
3531 53213116 : member = lookup_member (access_path, name, /*protect=*/1,
3532 : /*want_type=*/false, complain,
3533 : &afi);
3534 53213116 : if (processing_template_decl)
3535 43645970 : pop_deferring_access_checks ();
3536 53213116 : afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3537 53213116 : if (member == NULL_TREE)
3538 : {
3539 5566716 : if (dependent_type_p (object_type))
3540 : /* Try again at instantiation time. */
3541 9226774 : goto dependent;
3542 3759 : if (complain & tf_error)
3543 194 : complain_about_unrecognized_member (access_path, name,
3544 : object_type);
3545 3785 : return error_mark_node;
3546 : }
3547 47646400 : if (member == error_mark_node)
3548 : return error_mark_node;
3549 47646374 : if (DECL_P (member)
3550 47646374 : && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3551 : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3552 : wrong, so don't use it. */
3553 8 : goto dependent;
3554 47646366 : if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3555 3663809 : goto dependent;
3556 : }
3557 :
3558 44063926 : if (is_template_id)
3559 : {
3560 268996 : tree templ = member;
3561 :
3562 268996 : if (BASELINK_P (templ))
3563 268972 : member = lookup_template_function (templ, template_args);
3564 24 : else if (variable_template_p (templ))
3565 24 : member = (lookup_and_finish_template_variable
3566 24 : (templ, template_args, complain));
3567 : else
3568 : {
3569 0 : if (complain & tf_error)
3570 0 : error ("%qD is not a member template function", name);
3571 0 : return error_mark_node;
3572 : }
3573 : }
3574 : }
3575 :
3576 48540551 : if (TREE_UNAVAILABLE (member))
3577 40 : error_unavailable_use (member, NULL_TREE);
3578 48540511 : else if (TREE_DEPRECATED (member))
3579 40 : warn_deprecated_use (member, NULL_TREE);
3580 :
3581 48540551 : if (template_p)
3582 7063 : check_template_keyword (member);
3583 :
3584 48540551 : expr = build_class_member_access_expr (object, member, access_path,
3585 : /*preserve_reference=*/false,
3586 : complain);
3587 48540551 : if (processing_template_decl && expr != error_mark_node)
3588 : {
3589 34595835 : if (BASELINK_P (member))
3590 : {
3591 25843284 : if (TREE_CODE (orig_name) == SCOPE_REF)
3592 280 : BASELINK_QUALIFIED_P (member) = 1;
3593 : orig_name = member;
3594 : }
3595 34595835 : return build_min_non_dep (COMPONENT_REF, expr,
3596 : orig_object, orig_name,
3597 34595835 : NULL_TREE);
3598 : }
3599 :
3600 : return expr;
3601 : }
3602 :
3603 : /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3604 : type. */
3605 :
3606 : tree
3607 127103 : build_simple_component_ref (tree object, tree member)
3608 : {
3609 127103 : tree type = cp_build_qualified_type (TREE_TYPE (member),
3610 127103 : cp_type_quals (TREE_TYPE (object)));
3611 127103 : return build3_loc (input_location,
3612 : COMPONENT_REF, type,
3613 127103 : object, member, NULL_TREE);
3614 : }
3615 :
3616 : /* Return an expression for the MEMBER_NAME field in the internal
3617 : representation of PTRMEM, a pointer-to-member function. (Each
3618 : pointer-to-member function type gets its own RECORD_TYPE so it is
3619 : more convenient to access the fields by name than by FIELD_DECL.)
3620 : This routine converts the NAME to a FIELD_DECL and then creates the
3621 : node for the complete expression. */
3622 :
3623 : tree
3624 127086 : build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3625 : {
3626 127086 : tree ptrmem_type;
3627 127086 : tree member;
3628 :
3629 127086 : if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3630 : {
3631 247 : for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3632 101 : if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3633 73 : return e.value;
3634 0 : gcc_unreachable ();
3635 : }
3636 :
3637 : /* This code is a stripped down version of
3638 : build_class_member_access_expr. It does not work to use that
3639 : routine directly because it expects the object to be of class
3640 : type. */
3641 127013 : ptrmem_type = TREE_TYPE (ptrmem);
3642 127013 : gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3643 190411 : for (member = TYPE_FIELDS (ptrmem_type); member;
3644 63398 : member = DECL_CHAIN (member))
3645 190411 : if (DECL_NAME (member) == member_name)
3646 : break;
3647 127013 : return build_simple_component_ref (ptrmem, member);
3648 : }
3649 :
3650 : /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3651 : and for any other relevant operator. */
3652 :
3653 : static tree
3654 77328688 : op_unqualified_lookup (tree_code code, bool is_assign)
3655 : {
3656 77328688 : tree lookups = NULL_TREE;
3657 :
3658 77328688 : if (cxx_dialect >= cxx20 && !is_assign)
3659 : {
3660 3236433 : if (code == NE_EXPR)
3661 : {
3662 : /* != can get rewritten in terms of ==. */
3663 207050 : tree fnname = ovl_op_identifier (false, EQ_EXPR);
3664 207050 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3665 199326 : lookups = tree_cons (fnname, fns, lookups);
3666 : }
3667 3029383 : else if (code == GT_EXPR || code == LE_EXPR
3668 3029383 : || code == LT_EXPR || code == GE_EXPR)
3669 : {
3670 : /* These can get rewritten in terms of <=>. */
3671 345667 : tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3672 345667 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3673 276669 : lookups = tree_cons (fnname, fns, lookups);
3674 : }
3675 : }
3676 :
3677 77328688 : tree fnname = ovl_op_identifier (is_assign, code);
3678 77328688 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3679 43908907 : lookups = tree_cons (fnname, fns, lookups);
3680 :
3681 77328688 : if (lookups)
3682 : return lookups;
3683 : else
3684 33398327 : return build_tree_list (NULL_TREE, NULL_TREE);
3685 : }
3686 :
3687 : /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3688 : the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3689 : name lookup for the given operator. */
3690 :
3691 : tree
3692 87154061 : build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3693 : {
3694 87154061 : if (lookups)
3695 : /* We're partially instantiating a dependent operator expression, and
3696 : LOOKUPS is the result of phase 1 name lookup that we performed
3697 : earlier at template definition time, so just reuse the corresponding
3698 : DEPENDENT_OPERATOR_TYPE. */
3699 9825373 : return TREE_TYPE (lookups);
3700 :
3701 : /* Otherwise we're processing a dependent operator expression at template
3702 : definition time, so perform phase 1 name lookup now. */
3703 77328688 : lookups = op_unqualified_lookup (code, is_assign);
3704 :
3705 77328688 : tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3706 77328688 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3707 77328688 : TREE_TYPE (lookups) = type;
3708 77328688 : return type;
3709 : }
3710 :
3711 : /* Given an expression PTR for a pointer, return an expression
3712 : for the value pointed to.
3713 : ERRORSTRING is the name of the operator to appear in error messages.
3714 :
3715 : This function may need to overload OPERATOR_FNNAME.
3716 : Must also handle REFERENCE_TYPEs for C++. */
3717 :
3718 : tree
3719 22727192 : build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3720 : tree lookups, tsubst_flags_t complain)
3721 : {
3722 22727192 : tree orig_expr = expr;
3723 22727192 : tree rval;
3724 22727192 : tree overload = NULL_TREE;
3725 :
3726 22727192 : if (processing_template_decl)
3727 : {
3728 : /* Retain the type if we know the operand is a pointer. */
3729 14053972 : if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3730 : {
3731 7816904 : if (expr == current_class_ptr
3732 7816904 : || (TREE_CODE (expr) == NOP_EXPR
3733 4675470 : && TREE_OPERAND (expr, 0) == current_class_ptr
3734 4668503 : && (same_type_ignoring_top_level_qualifiers_p
3735 4668503 : (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3736 4668503 : return current_class_ref;
3737 3148401 : return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3738 : }
3739 6237068 : if (type_dependent_expression_p (expr))
3740 : {
3741 6136827 : expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3742 6136827 : TREE_TYPE (expr)
3743 6136827 : = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3744 6136827 : return expr;
3745 : }
3746 100241 : expr = build_non_dependent_expr (expr);
3747 : }
3748 :
3749 8773461 : rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3750 : NULL_TREE, NULL_TREE, lookups,
3751 : &overload, complain);
3752 8773461 : if (!rval)
3753 0 : rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3754 :
3755 8773461 : if (processing_template_decl && rval != error_mark_node)
3756 : {
3757 98407 : if (overload != NULL_TREE)
3758 98373 : return (build_min_non_dep_op_overload
3759 98373 : (INDIRECT_REF, rval, overload, orig_expr));
3760 :
3761 34 : return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3762 : }
3763 : else
3764 : return rval;
3765 : }
3766 :
3767 : /* Like c-family strict_aliasing_warning, but don't warn for dependent
3768 : types or expressions. */
3769 :
3770 : static bool
3771 426960 : cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3772 : {
3773 426960 : if (processing_template_decl)
3774 : {
3775 27226 : tree e = expr;
3776 27226 : STRIP_NOPS (e);
3777 27226 : if (dependent_type_p (type) || type_dependent_expression_p (e))
3778 5923 : return false;
3779 : }
3780 421037 : return strict_aliasing_warning (loc, type, expr);
3781 : }
3782 :
3783 : /* The implementation of the above, and of indirection implied by other
3784 : constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3785 :
3786 : static tree
3787 179433306 : cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3788 : tsubst_flags_t complain, bool do_fold)
3789 : {
3790 179433306 : tree pointer, type;
3791 :
3792 : /* RO_NULL should only be used with the folding entry points below, not
3793 : cp_build_indirect_ref. */
3794 179433306 : gcc_checking_assert (errorstring != RO_NULL || do_fold);
3795 :
3796 179433306 : if (ptr == current_class_ptr
3797 179433306 : || (TREE_CODE (ptr) == NOP_EXPR
3798 15348438 : && TREE_OPERAND (ptr, 0) == current_class_ptr
3799 8140455 : && (same_type_ignoring_top_level_qualifiers_p
3800 8140455 : (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3801 8901226 : return current_class_ref;
3802 :
3803 170532080 : pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3804 170532080 : ? ptr : decay_conversion (ptr, complain));
3805 170532080 : if (pointer == error_mark_node)
3806 : return error_mark_node;
3807 :
3808 170526325 : type = TREE_TYPE (pointer);
3809 :
3810 170526325 : if (INDIRECT_TYPE_P (type))
3811 : {
3812 : /* [expr.unary.op]
3813 :
3814 : If the type of the expression is "pointer to T," the type
3815 : of the result is "T." */
3816 170526150 : tree t = TREE_TYPE (type);
3817 :
3818 170526150 : if ((CONVERT_EXPR_P (ptr)
3819 126916653 : || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3820 172239943 : && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3821 : {
3822 : /* If a warning is issued, mark it to avoid duplicates from
3823 : the backend. This only needs to be done at
3824 : warn_strict_aliasing > 2. */
3825 27872591 : if (warn_strict_aliasing > 2
3826 28242978 : && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3827 370387 : type, TREE_OPERAND (ptr, 0)))
3828 8 : suppress_warning (ptr, OPT_Wstrict_aliasing);
3829 : }
3830 :
3831 170526150 : if (VOID_TYPE_P (t))
3832 : {
3833 : /* A pointer to incomplete type (other than cv void) can be
3834 : dereferenced [expr.unary.op]/1 */
3835 48 : if (complain & tf_error)
3836 21 : error_at (loc, "%qT is not a pointer-to-object type", type);
3837 48 : return error_mark_node;
3838 : }
3839 163335231 : else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3840 173033494 : && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3841 : /* The POINTER was something like `&x'. We simplify `*&x' to
3842 : `x'. */
3843 2507392 : return TREE_OPERAND (pointer, 0);
3844 : else
3845 : {
3846 168018710 : tree ref = build1 (INDIRECT_REF, t, pointer);
3847 :
3848 : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3849 : so that we get the proper error message if the result is used
3850 : to assign to. Also, &* is supposed to be a no-op. */
3851 168018710 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3852 168018710 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3853 168018710 : TREE_SIDE_EFFECTS (ref)
3854 168018710 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3855 168018710 : return ref;
3856 : }
3857 : }
3858 175 : else if (!(complain & tf_error))
3859 : /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3860 : ;
3861 : /* `pointer' won't be an error_mark_node if we were given a
3862 : pointer to member, so it's cool to check for this here. */
3863 41 : else if (TYPE_PTRMEM_P (type))
3864 20 : switch (errorstring)
3865 : {
3866 0 : case RO_ARRAY_INDEXING:
3867 0 : error_at (loc,
3868 : "invalid use of array indexing on pointer to member");
3869 0 : break;
3870 16 : case RO_UNARY_STAR:
3871 16 : error_at (loc, "invalid use of unary %<*%> on pointer to member");
3872 16 : break;
3873 0 : case RO_IMPLICIT_CONVERSION:
3874 0 : error_at (loc, "invalid use of implicit conversion on pointer "
3875 : "to member");
3876 0 : break;
3877 4 : case RO_ARROW_STAR:
3878 4 : error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3879 : "class, but is a pointer to member of type %qT", type);
3880 4 : break;
3881 0 : default:
3882 0 : gcc_unreachable ();
3883 : }
3884 21 : else if (pointer != error_mark_node)
3885 21 : invalid_indirection_error (loc, type, errorstring);
3886 :
3887 175 : return error_mark_node;
3888 : }
3889 :
3890 : /* Entry point used by c-common, which expects folding. */
3891 :
3892 : tree
3893 6500 : build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3894 : {
3895 6500 : return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3896 6500 : tf_warning_or_error, true);
3897 : }
3898 :
3899 : /* Entry point used by internal indirection needs that don't correspond to any
3900 : syntactic construct. */
3901 :
3902 : tree
3903 165346076 : cp_build_fold_indirect_ref (tree pointer)
3904 : {
3905 165346076 : return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3906 165346076 : tf_warning_or_error, true);
3907 : }
3908 :
3909 : /* Entry point used by indirection needs that correspond to some syntactic
3910 : construct. */
3911 :
3912 : tree
3913 14080730 : cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3914 : tsubst_flags_t complain)
3915 : {
3916 11620505 : return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3917 : }
3918 :
3919 : /* This handles expressions of the form "a[i]", which denotes
3920 : an array reference.
3921 :
3922 : This is logically equivalent in C to *(a+i), but we may do it differently.
3923 : If A is a variable or a member, we generate a primitive ARRAY_REF.
3924 : This avoids forcing the array out of registers, and can work on
3925 : arrays that are not lvalues (for example, members of structures returned
3926 : by functions).
3927 :
3928 : If INDEX is of some user-defined type, it must be converted to
3929 : integer type. Otherwise, to make a compatible PLUS_EXPR, it
3930 : will inherit the type of the array, which will be some pointer type.
3931 :
3932 : LOC is the location to use in building the array reference. */
3933 :
3934 : tree
3935 3581558 : cp_build_array_ref (location_t loc, tree array, tree idx,
3936 : tsubst_flags_t complain)
3937 : {
3938 3581558 : tree ret;
3939 :
3940 3581558 : if (idx == 0)
3941 : {
3942 0 : if (complain & tf_error)
3943 0 : error_at (loc, "subscript missing in array reference");
3944 0 : return error_mark_node;
3945 : }
3946 :
3947 3581558 : if (TREE_TYPE (array) == error_mark_node
3948 3581558 : || TREE_TYPE (idx) == error_mark_node)
3949 : return error_mark_node;
3950 :
3951 : /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3952 : inside it. */
3953 3581552 : switch (TREE_CODE (array))
3954 : {
3955 32 : case COMPOUND_EXPR:
3956 32 : {
3957 32 : tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3958 : complain);
3959 32 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3960 32 : TREE_OPERAND (array, 0), value);
3961 32 : SET_EXPR_LOCATION (ret, loc);
3962 32 : return ret;
3963 : }
3964 :
3965 15 : case COND_EXPR:
3966 15 : ret = build_conditional_expr
3967 30 : (loc, TREE_OPERAND (array, 0),
3968 15 : cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3969 : complain),
3970 15 : cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3971 : complain),
3972 : complain);
3973 15 : protected_set_expr_location (ret, loc);
3974 15 : return ret;
3975 :
3976 3581505 : default:
3977 3581505 : break;
3978 : }
3979 :
3980 3581505 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3981 :
3982 3581505 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3983 : {
3984 1121272 : tree rval, type;
3985 :
3986 1121272 : warn_array_subscript_with_type_char (loc, idx);
3987 :
3988 1121272 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3989 : {
3990 3 : if (complain & tf_error)
3991 3 : error_at (loc, "array subscript is not an integer");
3992 3 : return error_mark_node;
3993 : }
3994 :
3995 : /* Apply integral promotions *after* noticing character types.
3996 : (It is unclear why we do these promotions -- the standard
3997 : does not say that we should. In fact, the natural thing would
3998 : seem to be to convert IDX to ptrdiff_t; we're performing
3999 : pointer arithmetic.) */
4000 1121269 : idx = cp_perform_integral_promotions (idx, complain);
4001 :
4002 1121269 : idx = maybe_fold_non_dependent_expr (idx, complain);
4003 :
4004 : /* An array that is indexed by a non-constant
4005 : cannot be stored in a register; we must be able to do
4006 : address arithmetic on its address.
4007 : Likewise an array of elements of variable size. */
4008 1121269 : if (TREE_CODE (idx) != INTEGER_CST
4009 1121269 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4010 510186 : && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4011 : != INTEGER_CST)))
4012 : {
4013 612733 : if (!cxx_mark_addressable (array, true))
4014 0 : return error_mark_node;
4015 : }
4016 :
4017 : /* An array that is indexed by a constant value which is not within
4018 : the array bounds cannot be stored in a register either; because we
4019 : would get a crash in store_bit_field/extract_bit_field when trying
4020 : to access a non-existent part of the register. */
4021 1121269 : if (TREE_CODE (idx) == INTEGER_CST
4022 510190 : && TYPE_DOMAIN (TREE_TYPE (array))
4023 1629674 : && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4024 : {
4025 3047 : if (!cxx_mark_addressable (array))
4026 0 : return error_mark_node;
4027 : }
4028 :
4029 : /* Note in C++ it is valid to subscript a `register' array, since
4030 : it is valid to take the address of something with that
4031 : storage specification. */
4032 1121269 : if (extra_warnings)
4033 : {
4034 33493 : tree foo = array;
4035 52598 : while (TREE_CODE (foo) == COMPONENT_REF)
4036 19105 : foo = TREE_OPERAND (foo, 0);
4037 8 : if (VAR_P (foo) && DECL_REGISTER (foo)
4038 33493 : && (complain & tf_warning))
4039 0 : warning_at (loc, OPT_Wextra,
4040 : "subscripting array declared %<register%>");
4041 : }
4042 :
4043 1121269 : type = TREE_TYPE (TREE_TYPE (array));
4044 1121269 : rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4045 : /* Array ref is const/volatile if the array elements are
4046 : or if the array is.. */
4047 3363807 : TREE_READONLY (rval)
4048 1121269 : |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4049 3363807 : TREE_SIDE_EFFECTS (rval)
4050 1121269 : |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4051 2242538 : TREE_THIS_VOLATILE (rval)
4052 1121269 : |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4053 1121269 : ret = require_complete_type (rval, complain);
4054 1121269 : protected_set_expr_location (ret, loc);
4055 1121269 : if (non_lvalue)
4056 5089 : ret = non_lvalue_loc (loc, ret);
4057 1121269 : return ret;
4058 : }
4059 :
4060 2460233 : {
4061 2460233 : tree ar = cp_default_conversion (array, complain);
4062 2460233 : tree ind = cp_default_conversion (idx, complain);
4063 2460233 : tree first = NULL_TREE;
4064 :
4065 2460233 : if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
4066 93646 : ar = first = save_expr (ar);
4067 :
4068 : /* Put the integer in IND to simplify error checking. */
4069 2460233 : if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4070 47 : std::swap (ar, ind);
4071 :
4072 2460233 : if (ar == error_mark_node || ind == error_mark_node)
4073 : return error_mark_node;
4074 :
4075 2460233 : if (!TYPE_PTR_P (TREE_TYPE (ar)))
4076 : {
4077 8 : if (complain & tf_error)
4078 4 : error_at (loc, "subscripted value is neither array nor pointer");
4079 8 : return error_mark_node;
4080 : }
4081 2460225 : if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4082 : {
4083 0 : if (complain & tf_error)
4084 0 : error_at (loc, "array subscript is not an integer");
4085 0 : return error_mark_node;
4086 : }
4087 :
4088 2460225 : warn_array_subscript_with_type_char (loc, idx);
4089 :
4090 2460225 : ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4091 2460225 : if (first)
4092 93646 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4093 2460225 : ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4094 2460225 : protected_set_expr_location (ret, loc);
4095 2460225 : if (non_lvalue)
4096 0 : ret = non_lvalue_loc (loc, ret);
4097 : return ret;
4098 : }
4099 : }
4100 :
4101 : /* Entry point for Obj-C++. */
4102 :
4103 : tree
4104 2352903 : build_array_ref (location_t loc, tree array, tree idx)
4105 : {
4106 2352903 : return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4107 : }
4108 :
4109 : /* Resolve a pointer to member function. INSTANCE is the object
4110 : instance to use, if the member points to a virtual member.
4111 :
4112 : This used to avoid checking for virtual functions if basetype
4113 : has no virtual functions, according to an earlier ANSI draft.
4114 : With the final ISO C++ rules, such an optimization is
4115 : incorrect: A pointer to a derived member can be static_cast
4116 : to pointer-to-base-member, as long as the dynamic object
4117 : later has the right member. So now we only do this optimization
4118 : when we know the dynamic type of the object. */
4119 :
4120 : tree
4121 63264 : get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4122 : tsubst_flags_t complain)
4123 : {
4124 63264 : if (TREE_CODE (function) == OFFSET_REF)
4125 0 : function = TREE_OPERAND (function, 1);
4126 :
4127 63264 : if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4128 : {
4129 63264 : tree idx, delta, e1, e2, e3, vtbl;
4130 63264 : bool nonvirtual;
4131 63264 : tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4132 63264 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4133 :
4134 63264 : tree instance_ptr = *instance_ptrptr;
4135 63264 : tree instance_save_expr = 0;
4136 63264 : if (instance_ptr == error_mark_node)
4137 : {
4138 0 : if (TREE_CODE (function) == PTRMEM_CST)
4139 : {
4140 : /* Extracting the function address from a pmf is only
4141 : allowed with -Wno-pmf-conversions. It only works for
4142 : pmf constants. */
4143 0 : e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4144 0 : e1 = convert (fntype, e1);
4145 0 : return e1;
4146 : }
4147 : else
4148 : {
4149 0 : if (complain & tf_error)
4150 0 : error ("object missing in use of %qE", function);
4151 0 : return error_mark_node;
4152 : }
4153 : }
4154 :
4155 : /* True if we know that the dynamic type of the object doesn't have
4156 : virtual functions, so we can assume the PFN field is a pointer. */
4157 63264 : nonvirtual = (COMPLETE_TYPE_P (basetype)
4158 63241 : && !TYPE_POLYMORPHIC_P (basetype)
4159 85123 : && resolves_to_fixed_type_p (instance_ptr, 0));
4160 :
4161 : /* If we don't really have an object (i.e. in an ill-formed
4162 : conversion from PMF to pointer), we can't resolve virtual
4163 : functions anyway. */
4164 62982 : if (!nonvirtual && is_dummy_object (instance_ptr))
4165 : nonvirtual = true;
4166 :
4167 63264 : if (TREE_SIDE_EFFECTS (instance_ptr))
4168 41508 : instance_ptr = instance_save_expr = save_expr (instance_ptr);
4169 :
4170 63264 : if (TREE_SIDE_EFFECTS (function))
4171 27740 : function = save_expr (function);
4172 :
4173 : /* Start by extracting all the information from the PMF itself. */
4174 63264 : e3 = pfn_from_ptrmemfunc (function);
4175 63264 : delta = delta_from_ptrmemfunc (function);
4176 63264 : idx = build1 (NOP_EXPR, vtable_index_type, e3);
4177 63264 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4178 : {
4179 63264 : int flag_sanitize_save;
4180 63264 : case ptrmemfunc_vbit_in_pfn:
4181 63264 : e1 = cp_build_binary_op (input_location,
4182 : BIT_AND_EXPR, idx, integer_one_node,
4183 : complain);
4184 63264 : idx = cp_build_binary_op (input_location,
4185 : MINUS_EXPR, idx, integer_one_node,
4186 : complain);
4187 63264 : if (idx == error_mark_node)
4188 : return error_mark_node;
4189 63264 : break;
4190 :
4191 : case ptrmemfunc_vbit_in_delta:
4192 : e1 = cp_build_binary_op (input_location,
4193 : BIT_AND_EXPR, delta, integer_one_node,
4194 : complain);
4195 : /* Don't instrument the RSHIFT_EXPR we're about to create because
4196 : we're going to use DELTA number of times, and that wouldn't play
4197 : well with SAVE_EXPRs therein. */
4198 : flag_sanitize_save = flag_sanitize;
4199 : flag_sanitize = 0;
4200 : delta = cp_build_binary_op (input_location,
4201 : RSHIFT_EXPR, delta, integer_one_node,
4202 : complain);
4203 : flag_sanitize = flag_sanitize_save;
4204 : if (delta == error_mark_node)
4205 : return error_mark_node;
4206 : break;
4207 :
4208 : default:
4209 : gcc_unreachable ();
4210 : }
4211 :
4212 63264 : if (e1 == error_mark_node)
4213 : return error_mark_node;
4214 :
4215 : /* Convert down to the right base before using the instance. A
4216 : special case is that in a pointer to member of class C, C may
4217 : be incomplete. In that case, the function will of course be
4218 : a member of C, and no conversion is required. In fact,
4219 : lookup_base will fail in that case, because incomplete
4220 : classes do not have BINFOs. */
4221 63264 : if (!same_type_ignoring_top_level_qualifiers_p
4222 63264 : (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4223 : {
4224 72 : basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4225 : basetype, ba_check, NULL, complain);
4226 72 : instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4227 : 1, complain);
4228 72 : if (instance_ptr == error_mark_node)
4229 : return error_mark_node;
4230 : }
4231 : /* ...and then the delta in the PMF. */
4232 63264 : instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4233 :
4234 : /* Hand back the adjusted 'this' argument to our caller. */
4235 63264 : *instance_ptrptr = instance_ptr;
4236 :
4237 63264 : if (nonvirtual)
4238 : /* Now just return the pointer. */
4239 : return e3;
4240 :
4241 : /* Next extract the vtable pointer from the object. */
4242 62958 : vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4243 : instance_ptr);
4244 62958 : vtbl = cp_build_fold_indirect_ref (vtbl);
4245 62958 : if (vtbl == error_mark_node)
4246 : return error_mark_node;
4247 :
4248 : /* Finally, extract the function pointer from the vtable. */
4249 62958 : e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4250 62958 : e2 = cp_build_fold_indirect_ref (e2);
4251 62958 : if (e2 == error_mark_node)
4252 : return error_mark_node;
4253 62958 : TREE_CONSTANT (e2) = 1;
4254 :
4255 : /* When using function descriptors, the address of the
4256 : vtable entry is treated as a function pointer. */
4257 62958 : if (TARGET_VTABLE_USES_DESCRIPTORS)
4258 : e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4259 : cp_build_addr_expr (e2, complain));
4260 :
4261 62958 : e2 = fold_convert (TREE_TYPE (e3), e2);
4262 62958 : e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4263 62958 : if (e1 == error_mark_node)
4264 : return error_mark_node;
4265 :
4266 : /* Make sure this doesn't get evaluated first inside one of the
4267 : branches of the COND_EXPR. */
4268 62958 : if (instance_save_expr)
4269 41463 : e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4270 : instance_save_expr, e1);
4271 :
4272 : function = e1;
4273 : }
4274 : return function;
4275 : }
4276 :
4277 : /* Used by the C-common bits. */
4278 : tree
4279 0 : build_function_call (location_t /*loc*/,
4280 : tree function, tree params)
4281 : {
4282 0 : return cp_build_function_call (function, params, tf_warning_or_error);
4283 : }
4284 :
4285 : /* Used by the C-common bits. */
4286 : tree
4287 142193 : build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4288 : tree function, vec<tree, va_gc> *params,
4289 : vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4290 : {
4291 142193 : vec<tree, va_gc> *orig_params = params;
4292 142193 : tree ret = cp_build_function_call_vec (function, ¶ms,
4293 : tf_warning_or_error, orig_function);
4294 :
4295 : /* cp_build_function_call_vec can reallocate PARAMS by adding
4296 : default arguments. That should never happen here. Verify
4297 : that. */
4298 142193 : gcc_assert (params == orig_params);
4299 :
4300 142193 : return ret;
4301 : }
4302 :
4303 : /* Build a function call using a tree list of arguments. */
4304 :
4305 : static tree
4306 0 : cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4307 : {
4308 0 : tree ret;
4309 :
4310 0 : releasing_vec vec;
4311 0 : for (; params != NULL_TREE; params = TREE_CHAIN (params))
4312 0 : vec_safe_push (vec, TREE_VALUE (params));
4313 0 : ret = cp_build_function_call_vec (function, &vec, complain);
4314 0 : return ret;
4315 0 : }
4316 :
4317 : /* Build a function call using varargs. */
4318 :
4319 : tree
4320 347241 : cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4321 : {
4322 347241 : va_list args;
4323 347241 : tree ret, t;
4324 :
4325 347241 : releasing_vec vec;
4326 347241 : va_start (args, complain);
4327 853682 : for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4328 506441 : vec_safe_push (vec, t);
4329 347241 : va_end (args);
4330 347241 : ret = cp_build_function_call_vec (function, &vec, complain);
4331 347241 : return ret;
4332 347241 : }
4333 :
4334 : /* Build a function call using a vector of arguments.
4335 : If FUNCTION is the result of resolving an overloaded target built-in,
4336 : ORIG_FNDECL is the original function decl, otherwise it is null.
4337 : PARAMS may be NULL if there are no parameters. This changes the
4338 : contents of PARAMS. */
4339 :
4340 : tree
4341 1437424 : cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4342 : tsubst_flags_t complain, tree orig_fndecl)
4343 : {
4344 1437424 : tree fntype, fndecl;
4345 1437424 : int is_method;
4346 1437424 : tree original = function;
4347 1437424 : int nargs;
4348 1437424 : tree *argarray;
4349 1437424 : tree parm_types;
4350 1437424 : vec<tree, va_gc> *allocated = NULL;
4351 1437424 : tree ret;
4352 :
4353 : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4354 : expressions, like those used for ObjC messenger dispatches. */
4355 1437424 : if (params != NULL && !vec_safe_is_empty (*params))
4356 1099156 : function = objc_rewrite_function_call (function, (**params)[0]);
4357 :
4358 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4359 : Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4360 1437424 : if (TREE_CODE (function) == NOP_EXPR
4361 1437424 : && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4362 12 : function = TREE_OPERAND (function, 0);
4363 :
4364 1437424 : if (TREE_CODE (function) == FUNCTION_DECL)
4365 : {
4366 871447 : if (!mark_used (function, complain))
4367 17 : return error_mark_node;
4368 871430 : fndecl = function;
4369 :
4370 : /* Convert anything with function type to a pointer-to-function. */
4371 871430 : if (DECL_MAIN_P (function))
4372 : {
4373 0 : if (complain & tf_error)
4374 0 : pedwarn (input_location, OPT_Wpedantic,
4375 : "ISO C++ forbids calling %<::main%> from within program");
4376 : else
4377 0 : return error_mark_node;
4378 : }
4379 871430 : function = build_addr_func (function, complain);
4380 : }
4381 : else
4382 : {
4383 565977 : fndecl = NULL_TREE;
4384 :
4385 565977 : function = build_addr_func (function, complain);
4386 : }
4387 :
4388 1437407 : if (function == error_mark_node)
4389 : return error_mark_node;
4390 :
4391 1437276 : fntype = TREE_TYPE (function);
4392 :
4393 1437276 : if (TYPE_PTRMEMFUNC_P (fntype))
4394 : {
4395 20 : if (complain & tf_error)
4396 20 : error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4397 : "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4398 : original, original);
4399 20 : return error_mark_node;
4400 : }
4401 :
4402 2874512 : is_method = (TYPE_PTR_P (fntype)
4403 1437256 : && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4404 :
4405 1437256 : if (!(TYPE_PTRFN_P (fntype)
4406 64446 : || is_method
4407 1228 : || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4408 : {
4409 1228 : if (complain & tf_error)
4410 : {
4411 186 : if (!flag_diagnostics_show_caret)
4412 186 : error_at (input_location,
4413 : "%qE cannot be used as a function", original);
4414 0 : else if (DECL_P (original))
4415 0 : error_at (input_location,
4416 : "%qD cannot be used as a function", original);
4417 : else
4418 0 : error_at (input_location,
4419 : "expression cannot be used as a function");
4420 : }
4421 :
4422 1228 : return error_mark_node;
4423 : }
4424 :
4425 : /* fntype now gets the type of function pointed to. */
4426 1436028 : fntype = TREE_TYPE (fntype);
4427 1436028 : parm_types = TYPE_ARG_TYPES (fntype);
4428 :
4429 1436028 : if (params == NULL)
4430 : {
4431 113666 : allocated = make_tree_vector ();
4432 113666 : params = &allocated;
4433 : }
4434 :
4435 1436028 : nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4436 : complain);
4437 1436028 : if (nargs < 0)
4438 710 : return error_mark_node;
4439 :
4440 1435318 : argarray = (*params)->address ();
4441 :
4442 : /* Check for errors in format strings and inappropriately
4443 : null parameters. */
4444 1435318 : bool warned_p = check_function_arguments (input_location, fndecl, fntype,
4445 : nargs, argarray, NULL);
4446 :
4447 1435318 : ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4448 :
4449 1435318 : if (warned_p)
4450 : {
4451 33 : tree c = extract_call_expr (ret);
4452 33 : if (TREE_CODE (c) == CALL_EXPR)
4453 33 : suppress_warning (c, OPT_Wnonnull);
4454 : }
4455 :
4456 1435318 : if (allocated != NULL)
4457 113666 : release_tree_vector (allocated);
4458 :
4459 : return ret;
4460 : }
4461 :
4462 : /* Subroutine of convert_arguments.
4463 : Print an error message about a wrong number of arguments. */
4464 :
4465 : static void
4466 185 : error_args_num (location_t loc, tree fndecl, bool too_many_p)
4467 : {
4468 185 : if (fndecl)
4469 : {
4470 141 : if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4471 : {
4472 0 : if (DECL_NAME (fndecl) == NULL_TREE
4473 0 : || (DECL_NAME (fndecl)
4474 0 : == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4475 0 : error_at (loc,
4476 : too_many_p
4477 : ? G_("too many arguments to constructor %q#D")
4478 : : G_("too few arguments to constructor %q#D"),
4479 : fndecl);
4480 : else
4481 0 : error_at (loc,
4482 : too_many_p
4483 : ? G_("too many arguments to member function %q#D")
4484 : : G_("too few arguments to member function %q#D"),
4485 : fndecl);
4486 : }
4487 : else
4488 213 : error_at (loc,
4489 : too_many_p
4490 : ? G_("too many arguments to function %q#D")
4491 : : G_("too few arguments to function %q#D"),
4492 : fndecl);
4493 141 : if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4494 89 : inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4495 : }
4496 : else
4497 : {
4498 44 : if (c_dialect_objc () && objc_message_selector ())
4499 0 : error_at (loc,
4500 : too_many_p
4501 : ? G_("too many arguments to method %q#D")
4502 : : G_("too few arguments to method %q#D"),
4503 : objc_message_selector ());
4504 : else
4505 72 : error_at (loc, too_many_p ? G_("too many arguments to function")
4506 : : G_("too few arguments to function"));
4507 : }
4508 185 : }
4509 :
4510 : /* Convert the actual parameter expressions in the list VALUES to the
4511 : types in the list TYPELIST. The converted expressions are stored
4512 : back in the VALUES vector.
4513 : If parmdecls is exhausted, or when an element has NULL as its type,
4514 : perform the default conversions.
4515 :
4516 : NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4517 :
4518 : This is also where warnings about wrong number of args are generated.
4519 :
4520 : Returns the actual number of arguments processed (which might be less
4521 : than the length of the vector), or -1 on error.
4522 :
4523 : In C++, unspecified trailing parameters can be filled in with their
4524 : default arguments, if such were specified. Do so here. */
4525 :
4526 : static int
4527 1436028 : convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4528 : int flags, tsubst_flags_t complain)
4529 : {
4530 1436028 : tree typetail;
4531 1436028 : unsigned int i;
4532 :
4533 : /* Argument passing is always copy-initialization. */
4534 1436028 : flags |= LOOKUP_ONLYCONVERTING;
4535 :
4536 3958857 : for (i = 0, typetail = typelist;
4537 3958857 : i < vec_safe_length (*values);
4538 : i++)
4539 : {
4540 5046538 : tree type = typetail ? TREE_VALUE (typetail) : 0;
4541 2523484 : tree val = (**values)[i];
4542 :
4543 2523484 : if (val == error_mark_node || type == error_mark_node)
4544 : return -1;
4545 :
4546 2523476 : if (type == void_type_node)
4547 : {
4548 142 : if (complain & tf_error)
4549 : {
4550 85 : error_args_num (input_location, fndecl, /*too_many_p=*/true);
4551 85 : return i;
4552 : }
4553 : else
4554 : return -1;
4555 : }
4556 :
4557 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4558 : Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4559 2523334 : if (TREE_CODE (val) == NOP_EXPR
4560 573308 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4561 2523354 : && (type == 0 || !TYPE_REF_P (type)))
4562 20 : val = TREE_OPERAND (val, 0);
4563 :
4564 2523334 : if (type == 0 || !TYPE_REF_P (type))
4565 : {
4566 2393999 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4567 2393999 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4568 17459 : val = decay_conversion (val, complain);
4569 : }
4570 :
4571 2523334 : if (val == error_mark_node)
4572 : return -1;
4573 :
4574 2523334 : if (type != 0)
4575 : {
4576 : /* Formal parm type is specified by a function prototype. */
4577 2522904 : tree parmval;
4578 :
4579 2522904 : if (!COMPLETE_TYPE_P (complete_type (type)))
4580 : {
4581 17 : if (complain & tf_error)
4582 : {
4583 11 : location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4584 11 : if (fndecl)
4585 : {
4586 11 : auto_diagnostic_group d;
4587 11 : error_at (loc,
4588 : "parameter %P of %qD has incomplete type %qT",
4589 : i, fndecl, type);
4590 11 : inform (get_fndecl_argument_location (fndecl, i),
4591 : " declared here");
4592 11 : }
4593 : else
4594 0 : error_at (loc, "parameter %P has incomplete type %qT", i,
4595 : type);
4596 : }
4597 17 : parmval = error_mark_node;
4598 : }
4599 : else
4600 : {
4601 2522887 : parmval = convert_for_initialization
4602 2522887 : (NULL_TREE, type, val, flags,
4603 : ICR_ARGPASS, fndecl, i, complain);
4604 2522887 : parmval = convert_for_arg_passing (type, parmval, complain);
4605 : }
4606 :
4607 2522904 : if (parmval == error_mark_node)
4608 : return -1;
4609 :
4610 2522399 : (**values)[i] = parmval;
4611 : }
4612 : else
4613 : {
4614 430 : int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4615 4 : if (magic)
4616 : {
4617 : /* Don't truncate excess precision to the semantic type. */
4618 0 : if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4619 0 : val = TREE_OPERAND (val, 0);
4620 : /* Don't do ellipsis conversion for __built_in_constant_p
4621 : as this will result in spurious errors for non-trivial
4622 : types. */
4623 0 : val = require_complete_type (val, complain);
4624 : }
4625 : else
4626 430 : val = convert_arg_to_ellipsis (val, complain);
4627 :
4628 430 : (**values)[i] = val;
4629 : }
4630 :
4631 2522829 : if (typetail)
4632 2522399 : typetail = TREE_CHAIN (typetail);
4633 : }
4634 :
4635 1435373 : if (typetail != 0 && typetail != void_list_node)
4636 : {
4637 : /* See if there are default arguments that can be used. Because
4638 : we hold default arguments in the FUNCTION_TYPE (which is so
4639 : wrong), we can see default parameters here from deduced
4640 : contexts (and via typeof) for indirect function calls.
4641 : Fortunately we know whether we have a function decl to
4642 : provide default arguments in a language conformant
4643 : manner. */
4644 72 : if (fndecl && TREE_PURPOSE (typetail)
4645 143 : && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4646 : {
4647 6 : for (; typetail != void_list_node; ++i)
4648 : {
4649 : /* After DR777, with explicit template args we can end up with a
4650 : default argument followed by no default argument. */
4651 6 : if (!TREE_PURPOSE (typetail))
4652 : break;
4653 3 : tree parmval
4654 3 : = convert_default_arg (TREE_VALUE (typetail),
4655 3 : TREE_PURPOSE (typetail),
4656 3 : fndecl, i, complain);
4657 :
4658 3 : if (parmval == error_mark_node)
4659 0 : return -1;
4660 :
4661 3 : vec_safe_push (*values, parmval);
4662 3 : typetail = TREE_CHAIN (typetail);
4663 : /* ends with `...'. */
4664 3 : if (typetail == NULL_TREE)
4665 : break;
4666 : }
4667 : }
4668 :
4669 140 : if (typetail && typetail != void_list_node)
4670 : {
4671 140 : if (complain & tf_error)
4672 100 : error_args_num (input_location, fndecl, /*too_many_p=*/false);
4673 140 : return -1;
4674 : }
4675 : }
4676 :
4677 1435233 : return (int) i;
4678 : }
4679 :
4680 : /* Build a binary-operation expression, after performing default
4681 : conversions on the operands. CODE is the kind of expression to
4682 : build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4683 : are the tree codes which correspond to ARG1 and ARG2 when issuing
4684 : warnings about possibly misplaced parentheses. They may differ
4685 : from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4686 : folding (e.g., if the parser sees "a | 1 + 1", it may call this
4687 : routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4688 : To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4689 : ARG2_CODE as ERROR_MARK. */
4690 :
4691 : tree
4692 127415918 : build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4693 : enum tree_code arg1_code, tree arg2,
4694 : enum tree_code arg2_code, tree lookups,
4695 : tree *overload_p, tsubst_flags_t complain)
4696 : {
4697 127415918 : tree orig_arg1;
4698 127415918 : tree orig_arg2;
4699 127415918 : tree expr;
4700 127415918 : tree overload = NULL_TREE;
4701 :
4702 127415918 : orig_arg1 = arg1;
4703 127415918 : orig_arg2 = arg2;
4704 :
4705 127415918 : if (processing_template_decl)
4706 : {
4707 77161842 : if (type_dependent_expression_p (arg1)
4708 77161842 : || type_dependent_expression_p (arg2))
4709 : {
4710 56806545 : expr = build_min_nt_loc (loc, code, arg1, arg2);
4711 56806545 : TREE_TYPE (expr)
4712 56806545 : = build_dependent_operator_type (lookups, code, false);
4713 56806545 : return expr;
4714 : }
4715 20355297 : arg1 = build_non_dependent_expr (arg1);
4716 20355297 : arg2 = build_non_dependent_expr (arg2);
4717 : }
4718 :
4719 70609373 : if (code == DOTSTAR_EXPR)
4720 77416 : expr = build_m_component_ref (arg1, arg2, complain);
4721 : else
4722 70531957 : expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4723 : lookups, &overload, complain);
4724 :
4725 70609340 : if (overload_p != NULL)
4726 30266030 : *overload_p = overload;
4727 :
4728 : /* Check for cases such as x+y<<z which users are likely to
4729 : misinterpret. But don't warn about obj << x + y, since that is a
4730 : common idiom for I/O. */
4731 70609340 : if (warn_parentheses
4732 936827 : && (complain & tf_warning)
4733 900821 : && !processing_template_decl
4734 618592 : && !error_operand_p (arg1)
4735 618584 : && !error_operand_p (arg2)
4736 71227908 : && (code != LSHIFT_EXPR
4737 63163 : || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4738 618034 : warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4739 : arg2_code, orig_arg2);
4740 :
4741 70609340 : if (processing_template_decl && expr != error_mark_node)
4742 : {
4743 20355079 : if (overload != NULL_TREE)
4744 1210270 : return (build_min_non_dep_op_overload
4745 1210270 : (code, expr, overload, orig_arg1, orig_arg2));
4746 :
4747 19144809 : return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4748 : }
4749 :
4750 : return expr;
4751 : }
4752 :
4753 : /* Build and return an ARRAY_REF expression. */
4754 :
4755 : tree
4756 1236703 : build_x_array_ref (location_t loc, tree arg1, tree arg2,
4757 : tsubst_flags_t complain)
4758 : {
4759 1236703 : tree orig_arg1 = arg1;
4760 1236703 : tree orig_arg2 = arg2;
4761 1236703 : tree expr;
4762 1236703 : tree overload = NULL_TREE;
4763 :
4764 1236703 : if (processing_template_decl)
4765 : {
4766 577 : if (type_dependent_expression_p (arg1)
4767 577 : || type_dependent_expression_p (arg2))
4768 576 : return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4769 576 : NULL_TREE, NULL_TREE);
4770 1 : arg1 = build_non_dependent_expr (arg1);
4771 1 : arg2 = build_non_dependent_expr (arg2);
4772 : }
4773 :
4774 1236127 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4775 : NULL_TREE, NULL_TREE, &overload, complain);
4776 :
4777 1236127 : if (processing_template_decl && expr != error_mark_node)
4778 : {
4779 1 : if (overload != NULL_TREE)
4780 0 : return (build_min_non_dep_op_overload
4781 0 : (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4782 :
4783 1 : return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4784 1 : NULL_TREE, NULL_TREE);
4785 : }
4786 : return expr;
4787 : }
4788 :
4789 : /* Return whether OP is an expression of enum type cast to integer
4790 : type. In C++ even unsigned enum types are cast to signed integer
4791 : types. We do not want to issue warnings about comparisons between
4792 : signed and unsigned types when one of the types is an enum type.
4793 : Those warnings are always false positives in practice. */
4794 :
4795 : static bool
4796 522630 : enum_cast_to_int (tree op)
4797 : {
4798 522630 : if (CONVERT_EXPR_P (op)
4799 10788 : && TREE_TYPE (op) == integer_type_node
4800 652 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4801 522670 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4802 : return true;
4803 :
4804 : /* The cast may have been pushed into a COND_EXPR. */
4805 522598 : if (TREE_CODE (op) == COND_EXPR)
4806 747 : return (enum_cast_to_int (TREE_OPERAND (op, 1))
4807 747 : || enum_cast_to_int (TREE_OPERAND (op, 2)));
4808 :
4809 : return false;
4810 : }
4811 :
4812 : /* For the c-common bits. */
4813 : tree
4814 2234578 : build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4815 : bool /*convert_p*/)
4816 : {
4817 2234578 : return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4818 : }
4819 :
4820 : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4821 : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4822 :
4823 : static tree
4824 6814 : build_vec_cmp (tree_code code, tree type,
4825 : tree arg0, tree arg1)
4826 : {
4827 6814 : tree zero_vec = build_zero_cst (type);
4828 6814 : tree minus_one_vec = build_minus_one_cst (type);
4829 6814 : tree cmp_type = truth_type_for (type);
4830 6814 : tree cmp = build2 (code, cmp_type, arg0, arg1);
4831 6814 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4832 : }
4833 :
4834 : /* Possibly warn about an address never being NULL. */
4835 :
4836 : static void
4837 1067127 : warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4838 : {
4839 : /* Prevent warnings issued for macro expansion. */
4840 1067127 : if (!warn_address
4841 37231 : || (complain & tf_warning) == 0
4842 22114 : || c_inhibit_evaluation_warnings != 0
4843 22074 : || from_macro_expansion_at (location)
4844 1082836 : || warning_suppressed_p (op, OPT_Waddress))
4845 1051461 : return;
4846 :
4847 15666 : if (TREE_CODE (op) == NON_DEPENDENT_EXPR)
4848 18 : op = TREE_OPERAND (op, 0);
4849 :
4850 15666 : tree cop = fold_for_warn (op);
4851 :
4852 15666 : if (TREE_CODE (cop) == NON_LVALUE_EXPR)
4853 : /* Unwrap the expression for C++ 98. */
4854 1 : cop = TREE_OPERAND (cop, 0);
4855 :
4856 15666 : if (TREE_CODE (cop) == PTRMEM_CST)
4857 : {
4858 : /* The address of a nonstatic data member is never null. */
4859 40 : warning_at (location, OPT_Waddress,
4860 : "the address %qE will never be NULL",
4861 : cop);
4862 40 : return;
4863 : }
4864 :
4865 15626 : if (TREE_CODE (cop) == NOP_EXPR)
4866 : {
4867 : /* Allow casts to intptr_t to suppress the warning. */
4868 1087 : tree type = TREE_TYPE (cop);
4869 1087 : if (TREE_CODE (type) == INTEGER_TYPE)
4870 : return;
4871 :
4872 1087 : STRIP_NOPS (cop);
4873 : }
4874 :
4875 15626 : bool warned = false;
4876 15626 : if (TREE_CODE (cop) == ADDR_EXPR)
4877 : {
4878 1045 : cop = TREE_OPERAND (cop, 0);
4879 :
4880 : /* Set to true in the loop below if OP dereferences its operand.
4881 : In such a case the ultimate target need not be a decl for
4882 : the null [in]equality test to be necessarily constant. */
4883 1045 : bool deref = false;
4884 :
4885 : /* Get the outermost array or object, or member. */
4886 1273 : while (handled_component_p (cop))
4887 : {
4888 360 : if (TREE_CODE (cop) == COMPONENT_REF)
4889 : {
4890 : /* Get the member (its address is never null). */
4891 132 : cop = TREE_OPERAND (cop, 1);
4892 132 : break;
4893 : }
4894 :
4895 : /* Get the outer array/object to refer to in the warning. */
4896 228 : cop = TREE_OPERAND (cop, 0);
4897 228 : deref = true;
4898 : }
4899 :
4900 845 : if ((!deref && !decl_with_nonnull_addr_p (cop))
4901 839 : || from_macro_expansion_at (location)
4902 1884 : || warning_suppressed_p (cop, OPT_Waddress))
4903 206 : return;
4904 :
4905 839 : warned = warning_at (location, OPT_Waddress,
4906 : "the address of %qD will never be NULL", cop);
4907 839 : op = cop;
4908 : }
4909 14581 : else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
4910 : {
4911 : /* Adding zero to the null pointer is well-defined in C++. When
4912 : the offset is unknown (i.e., not a constant) warn anyway since
4913 : it's less likely that the pointer operand is null than not. */
4914 126 : tree off = TREE_OPERAND (cop, 1);
4915 126 : if (!integer_zerop (off)
4916 126 : && !warning_suppressed_p (cop, OPT_Waddress))
4917 : {
4918 126 : tree base = TREE_OPERAND (cop, 0);
4919 126 : STRIP_NOPS (base);
4920 126 : if (TYPE_REF_P (TREE_TYPE (base)))
4921 12 : warning_at (location, OPT_Waddress, "the compiler can assume that "
4922 : "the address of %qE will never be NULL", base);
4923 : else
4924 114 : warning_at (location, OPT_Waddress, "comparing the result of "
4925 : "pointer addition %qE and NULL", cop);
4926 : }
4927 126 : return;
4928 : }
4929 14455 : else if (CONVERT_EXPR_P (op)
4930 14455 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4931 : {
4932 82 : STRIP_NOPS (op);
4933 :
4934 82 : if (TREE_CODE (op) == COMPONENT_REF)
4935 32 : op = TREE_OPERAND (op, 1);
4936 :
4937 82 : if (DECL_P (op))
4938 82 : warned = warning_at (location, OPT_Waddress,
4939 : "the compiler can assume that the address of "
4940 : "%qD will never be NULL", op);
4941 : }
4942 :
4943 921 : if (warned && DECL_P (op))
4944 873 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
4945 : }
4946 :
4947 : /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
4948 : the other operand is of a different enumeration type or a floating-point
4949 : type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
4950 : code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
4951 : and LOC is the location for the whole binary expression.
4952 : TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
4953 :
4954 : static void
4955 57496541 : do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
4956 : tree type1)
4957 : {
4958 57496541 : if (TREE_CODE (type0) == ENUMERAL_TYPE
4959 1496398 : && TREE_CODE (type1) == ENUMERAL_TYPE
4960 58676433 : && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
4961 : {
4962 : /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
4963 : Otherwise, warn if -Wenum-conversion is on. */
4964 204 : enum opt_code opt;
4965 204 : if (warn_deprecated_enum_enum_conv)
4966 : opt = OPT_Wdeprecated_enum_enum_conversion;
4967 171 : else if (warn_enum_conversion)
4968 : opt = OPT_Wenum_conversion;
4969 : else
4970 : return;
4971 :
4972 99 : switch (code)
4973 : {
4974 : case GT_EXPR:
4975 : case LT_EXPR:
4976 : case GE_EXPR:
4977 : case LE_EXPR:
4978 : case EQ_EXPR:
4979 : case NE_EXPR:
4980 : /* Comparisons are handled by -Wenum-compare. */
4981 : return;
4982 : case SPACESHIP_EXPR:
4983 : /* This is invalid, don't warn. */
4984 : return;
4985 16 : case BIT_AND_EXPR:
4986 16 : case BIT_IOR_EXPR:
4987 16 : case BIT_XOR_EXPR:
4988 16 : warning_at (loc, opt, "bitwise operation between different "
4989 : "enumeration types %qT and %qT is deprecated",
4990 : type0, type1);
4991 16 : return;
4992 38 : default:
4993 38 : warning_at (loc, opt, "arithmetic between different enumeration "
4994 : "types %qT and %qT is deprecated", type0, type1);
4995 38 : return;
4996 : }
4997 : }
4998 57496337 : else if ((TREE_CODE (type0) == ENUMERAL_TYPE
4999 1496194 : && SCALAR_FLOAT_TYPE_P (type1))
5000 57496264 : || (SCALAR_FLOAT_TYPE_P (type0)
5001 22944004 : && TREE_CODE (type1) == ENUMERAL_TYPE))
5002 : {
5003 147 : const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5004 : /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5005 : Otherwise, warn if -Wenum-conversion is on. */
5006 147 : enum opt_code opt;
5007 147 : if (warn_deprecated_enum_float_conv)
5008 : opt = OPT_Wdeprecated_enum_float_conversion;
5009 126 : else if (warn_enum_conversion)
5010 : opt = OPT_Wenum_conversion;
5011 : else
5012 : return;
5013 :
5014 72 : switch (code)
5015 : {
5016 32 : case GT_EXPR:
5017 32 : case LT_EXPR:
5018 32 : case GE_EXPR:
5019 32 : case LE_EXPR:
5020 32 : case EQ_EXPR:
5021 32 : case NE_EXPR:
5022 32 : if (enum_first_p)
5023 16 : warning_at (loc, opt, "comparison of enumeration type %qT with "
5024 : "floating-point type %qT is deprecated",
5025 : type0, type1);
5026 : else
5027 16 : warning_at (loc, opt, "comparison of floating-point type %qT "
5028 : "with enumeration type %qT is deprecated",
5029 : type0, type1);
5030 32 : return;
5031 : case SPACESHIP_EXPR:
5032 : /* This is invalid, don't warn. */
5033 : return;
5034 36 : default:
5035 36 : if (enum_first_p)
5036 16 : warning_at (loc, opt, "arithmetic between enumeration type %qT "
5037 : "and floating-point type %qT is deprecated",
5038 : type0, type1);
5039 : else
5040 20 : warning_at (loc, opt, "arithmetic between floating-point type %qT "
5041 : "and enumeration type %qT is deprecated",
5042 : type0, type1);
5043 36 : return;
5044 : }
5045 : }
5046 : }
5047 :
5048 : /* Build a binary-operation expression without default conversions.
5049 : CODE is the kind of expression to build.
5050 : LOCATION is the location_t of the operator in the source code.
5051 : This function differs from `build' in several ways:
5052 : the data type of the result is computed and recorded in it,
5053 : warnings are generated if arg data types are invalid,
5054 : special handling for addition and subtraction of pointers is known,
5055 : and some optimization is done (operations on narrow ints
5056 : are done in the narrower type when that gives the same result).
5057 : Constant folding is also done before the result is returned.
5058 :
5059 : Note that the operands will never have enumeral types
5060 : because either they have just had the default conversions performed
5061 : or they have both just been converted to some other type in which
5062 : the arithmetic is to be done.
5063 :
5064 : C++: must do special pointer arithmetic when implementing
5065 : multiple inheritance, and deal with pointer to member functions. */
5066 :
5067 : tree
5068 77932856 : cp_build_binary_op (const op_location_t &location,
5069 : enum tree_code code, tree orig_op0, tree orig_op1,
5070 : tsubst_flags_t complain)
5071 : {
5072 77932856 : tree op0, op1;
5073 77932856 : enum tree_code code0, code1;
5074 77932856 : tree type0, type1, orig_type0, orig_type1;
5075 77932856 : const char *invalid_op_diag;
5076 :
5077 : /* Expression code to give to the expression when it is built.
5078 : Normally this is CODE, which is what the caller asked for,
5079 : but in some special cases we change it. */
5080 77932856 : enum tree_code resultcode = code;
5081 :
5082 : /* Data type in which the computation is to be performed.
5083 : In the simplest cases this is the common type of the arguments. */
5084 77932856 : tree result_type = NULL_TREE;
5085 :
5086 : /* When the computation is in excess precision, the type of the
5087 : final EXCESS_PRECISION_EXPR. */
5088 77932856 : tree semantic_result_type = NULL;
5089 :
5090 : /* Nonzero means operands have already been type-converted
5091 : in whatever way is necessary.
5092 : Zero means they need to be converted to RESULT_TYPE. */
5093 77932856 : int converted = 0;
5094 :
5095 : /* Nonzero means create the expression with this type, rather than
5096 : RESULT_TYPE. */
5097 77932856 : tree build_type = 0;
5098 :
5099 : /* Nonzero means after finally constructing the expression
5100 : convert it to this type. */
5101 77932856 : tree final_type = 0;
5102 :
5103 77932856 : tree result;
5104 :
5105 : /* Nonzero if this is an operation like MIN or MAX which can
5106 : safely be computed in short if both args are promoted shorts.
5107 : Also implies COMMON.
5108 : -1 indicates a bitwise operation; this makes a difference
5109 : in the exact conditions for when it is safe to do the operation
5110 : in a narrower mode. */
5111 77932856 : int shorten = 0;
5112 :
5113 : /* Nonzero if this is a comparison operation;
5114 : if both args are promoted shorts, compare the original shorts.
5115 : Also implies COMMON. */
5116 77932856 : int short_compare = 0;
5117 :
5118 : /* Nonzero if this is a right-shift operation, which can be computed on the
5119 : original short and then promoted if the operand is a promoted short. */
5120 77932856 : int short_shift = 0;
5121 :
5122 : /* Nonzero means set RESULT_TYPE to the common type of the args. */
5123 77932856 : int common = 0;
5124 :
5125 : /* True if both operands have arithmetic type. */
5126 77932856 : bool arithmetic_types_p;
5127 :
5128 : /* Remember whether we're doing / or %. */
5129 77932856 : bool doing_div_or_mod = false;
5130 :
5131 : /* Remember whether we're doing << or >>. */
5132 77932856 : bool doing_shift = false;
5133 :
5134 : /* Tree holding instrumentation expression. */
5135 77932856 : tree instrument_expr = NULL_TREE;
5136 :
5137 : /* True means this is an arithmetic operation that may need excess
5138 : precision. */
5139 77932856 : bool may_need_excess_precision;
5140 :
5141 : /* Apply default conversions. */
5142 77932856 : op0 = resolve_nondeduced_context (orig_op0, complain);
5143 77932856 : op1 = resolve_nondeduced_context (orig_op1, complain);
5144 :
5145 77932856 : if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5146 71180975 : || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5147 69625251 : || code == TRUTH_XOR_EXPR)
5148 : {
5149 8307605 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5150 8307584 : op0 = decay_conversion (op0, complain);
5151 8307605 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5152 8307605 : op1 = decay_conversion (op1, complain);
5153 : }
5154 : else
5155 : {
5156 69625251 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5157 69625232 : op0 = cp_default_conversion (op0, complain);
5158 69625251 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5159 69625247 : op1 = cp_default_conversion (op1, complain);
5160 : }
5161 :
5162 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5163 83143547 : STRIP_TYPE_NOPS (op0);
5164 95233319 : STRIP_TYPE_NOPS (op1);
5165 :
5166 : /* DTRT if one side is an overloaded function, but complain about it. */
5167 77932856 : if (type_unknown_p (op0))
5168 : {
5169 12 : tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5170 12 : if (t != error_mark_node)
5171 : {
5172 0 : if (complain & tf_error)
5173 0 : permerror (location,
5174 : "assuming cast to type %qT from overloaded function",
5175 0 : TREE_TYPE (t));
5176 : op0 = t;
5177 : }
5178 : }
5179 77932856 : if (type_unknown_p (op1))
5180 : {
5181 4 : tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5182 4 : if (t != error_mark_node)
5183 : {
5184 4 : if (complain & tf_error)
5185 4 : permerror (location,
5186 : "assuming cast to type %qT from overloaded function",
5187 4 : TREE_TYPE (t));
5188 : op1 = t;
5189 : }
5190 : }
5191 :
5192 77932856 : orig_type0 = type0 = TREE_TYPE (op0);
5193 77932856 : orig_type1 = type1 = TREE_TYPE (op1);
5194 77932856 : tree non_ep_op0 = op0;
5195 77932856 : tree non_ep_op1 = op1;
5196 :
5197 : /* The expression codes of the data types of the arguments tell us
5198 : whether the arguments are integers, floating, pointers, etc. */
5199 77932856 : code0 = TREE_CODE (type0);
5200 77932856 : code1 = TREE_CODE (type1);
5201 :
5202 : /* If an error was already reported for one of the arguments,
5203 : avoid reporting another error. */
5204 77932856 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5205 121 : return error_mark_node;
5206 :
5207 155865470 : if ((invalid_op_diag
5208 77932735 : = targetm.invalid_binary_op (code, type0, type1)))
5209 : {
5210 0 : if (complain & tf_error)
5211 : {
5212 0 : if (code0 == REAL_TYPE
5213 0 : && code1 == REAL_TYPE
5214 0 : && (extended_float_type_p (type0)
5215 0 : || extended_float_type_p (type1))
5216 0 : && cp_compare_floating_point_conversion_ranks (type0,
5217 : type1) == 3)
5218 : {
5219 0 : rich_location richloc (line_table, location);
5220 0 : binary_op_error (&richloc, code, type0, type1);
5221 0 : }
5222 : else
5223 0 : error (invalid_op_diag);
5224 : }
5225 0 : return error_mark_node;
5226 : }
5227 :
5228 77932735 : switch (code)
5229 : {
5230 : case PLUS_EXPR:
5231 : case MINUS_EXPR:
5232 : case MULT_EXPR:
5233 : case TRUNC_DIV_EXPR:
5234 : case CEIL_DIV_EXPR:
5235 : case FLOOR_DIV_EXPR:
5236 : case ROUND_DIV_EXPR:
5237 : case EXACT_DIV_EXPR:
5238 : may_need_excess_precision = true;
5239 : break;
5240 24618254 : case EQ_EXPR:
5241 24618254 : case NE_EXPR:
5242 24618254 : case LE_EXPR:
5243 24618254 : case GE_EXPR:
5244 24618254 : case LT_EXPR:
5245 24618254 : case GT_EXPR:
5246 24618254 : case SPACESHIP_EXPR:
5247 : /* Excess precision for implicit conversions of integers to
5248 : floating point. */
5249 5534292 : may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5250 30146349 : || ANY_INTEGRAL_TYPE_P (type1));
5251 : break;
5252 : default:
5253 77932735 : may_need_excess_precision = false;
5254 : break;
5255 : }
5256 77932735 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5257 : {
5258 1146 : op0 = TREE_OPERAND (op0, 0);
5259 1146 : type0 = TREE_TYPE (op0);
5260 : }
5261 77931589 : else if (may_need_excess_precision
5262 59927393 : && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5263 20091768 : if (tree eptype = excess_precision_type (type0))
5264 : {
5265 2251 : type0 = eptype;
5266 2251 : op0 = convert (eptype, op0);
5267 : }
5268 77932735 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5269 : {
5270 762 : op1 = TREE_OPERAND (op1, 0);
5271 762 : type1 = TREE_TYPE (op1);
5272 : }
5273 77931973 : else if (may_need_excess_precision
5274 59927627 : && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5275 20071458 : if (tree eptype = excess_precision_type (type1))
5276 : {
5277 2325 : type1 = eptype;
5278 2325 : op1 = convert (eptype, op1);
5279 : }
5280 :
5281 : /* Issue warnings about peculiar, but valid, uses of NULL. */
5282 155865359 : if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5283 : /* It's reasonable to use pointer values as operands of &&
5284 : and ||, so NULL is no exception. */
5285 8831 : && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5286 8811 : && ( /* Both are NULL (or 0) and the operation was not a
5287 : comparison or a pointer subtraction. */
5288 8906 : (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5289 36 : && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5290 : /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5291 8795 : || (!null_ptr_cst_p (orig_op0)
5292 8716 : && !TYPE_PTR_OR_PTRMEM_P (type0))
5293 8779 : || (!null_ptr_cst_p (orig_op1)
5294 59 : && !TYPE_PTR_OR_PTRMEM_P (type1)))
5295 77932787 : && (complain & tf_warning))
5296 : {
5297 52 : location_t loc =
5298 52 : expansion_point_location_if_in_system_header (input_location);
5299 :
5300 52 : warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5301 : }
5302 :
5303 : /* In case when one of the operands of the binary operation is
5304 : a vector and another is a scalar -- convert scalar to vector. */
5305 77991389 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5306 77990492 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5307 : {
5308 996 : enum stv_conv convert_flag
5309 996 : = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5310 : complain & tf_error);
5311 :
5312 996 : switch (convert_flag)
5313 : {
5314 27 : case stv_error:
5315 27 : return error_mark_node;
5316 59 : case stv_firstarg:
5317 59 : {
5318 59 : op0 = convert (TREE_TYPE (type1), op0);
5319 59 : op0 = save_expr (op0);
5320 59 : op0 = build_vector_from_val (type1, op0);
5321 59 : orig_type0 = type0 = TREE_TYPE (op0);
5322 59 : code0 = TREE_CODE (type0);
5323 59 : converted = 1;
5324 59 : break;
5325 : }
5326 770 : case stv_secondarg:
5327 770 : {
5328 770 : op1 = convert (TREE_TYPE (type0), op1);
5329 770 : op1 = save_expr (op1);
5330 770 : op1 = build_vector_from_val (type0, op1);
5331 770 : orig_type1 = type1 = TREE_TYPE (op1);
5332 770 : code1 = TREE_CODE (type1);
5333 770 : converted = 1;
5334 770 : break;
5335 : }
5336 : default:
5337 : break;
5338 : }
5339 : }
5340 :
5341 77932708 : switch (code)
5342 : {
5343 9525344 : case MINUS_EXPR:
5344 : /* Subtraction of two similar pointers.
5345 : We must subtract them as integers, then divide by object size. */
5346 9525344 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5347 10233174 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5348 707830 : TREE_TYPE (type1)))
5349 : {
5350 707829 : result = pointer_diff (location, op0, op1,
5351 : common_pointer_type (type0, type1), complain,
5352 : &instrument_expr);
5353 707829 : if (instrument_expr != NULL)
5354 84 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5355 : instrument_expr, result);
5356 :
5357 707829 : return result;
5358 : }
5359 : /* In all other cases except pointer - int, the usual arithmetic
5360 : rules apply. */
5361 8817515 : else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5362 : {
5363 : common = 1;
5364 : break;
5365 : }
5366 : /* The pointer - int case is just like pointer + int; fall
5367 : through. */
5368 12591115 : gcc_fallthrough ();
5369 12591115 : case PLUS_EXPR:
5370 12591115 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5371 3549007 : && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5372 : {
5373 3548996 : tree ptr_operand;
5374 3548996 : tree int_operand;
5375 3548996 : ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5376 1383 : int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5377 3548996 : if (processing_template_decl)
5378 : {
5379 1366950 : result_type = TREE_TYPE (ptr_operand);
5380 1366950 : break;
5381 : }
5382 2182046 : return cp_pointer_int_sum (location, code,
5383 : ptr_operand,
5384 : int_operand,
5385 2182046 : complain);
5386 : }
5387 : common = 1;
5388 : break;
5389 :
5390 : case MULT_EXPR:
5391 12850880 : common = 1;
5392 : break;
5393 :
5394 5612889 : case TRUNC_DIV_EXPR:
5395 5612889 : case CEIL_DIV_EXPR:
5396 5612889 : case FLOOR_DIV_EXPR:
5397 5612889 : case ROUND_DIV_EXPR:
5398 5612889 : case EXACT_DIV_EXPR:
5399 5612889 : if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5400 : {
5401 31521 : tree type0 = TREE_OPERAND (op0, 0);
5402 31521 : tree type1 = TREE_OPERAND (op1, 0);
5403 31521 : tree first_arg = tree_strip_any_location_wrapper (type0);
5404 31521 : if (!TYPE_P (type0))
5405 31521 : type0 = TREE_TYPE (type0);
5406 31521 : if (!TYPE_P (type1))
5407 31521 : type1 = TREE_TYPE (type1);
5408 31521 : if (INDIRECT_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1))
5409 : {
5410 32 : if (!(TREE_CODE (first_arg) == PARM_DECL
5411 28 : && DECL_ARRAY_PARAMETER_P (first_arg)
5412 4 : && warn_sizeof_array_argument)
5413 52 : && (complain & tf_warning))
5414 : {
5415 24 : auto_diagnostic_group d;
5416 24 : if (warning_at (location, OPT_Wsizeof_pointer_div,
5417 : "division %<sizeof (%T) / sizeof (%T)%> does "
5418 : "not compute the number of array elements",
5419 : type0, type1))
5420 24 : if (DECL_P (first_arg))
5421 24 : inform (DECL_SOURCE_LOCATION (first_arg),
5422 : "first %<sizeof%> operand was declared here");
5423 24 : }
5424 : }
5425 31493 : else if (TREE_CODE (type0) == ARRAY_TYPE
5426 29990 : && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5427 : /* Set by finish_parenthesized_expr. */
5428 29427 : && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5429 60904 : && (complain & tf_warning))
5430 15661 : maybe_warn_sizeof_array_div (location, first_arg, type0,
5431 : op1, non_reference (type1));
5432 : }
5433 :
5434 5612889 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5435 5612889 : || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5436 5612865 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5437 5612865 : || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5438 : {
5439 5612853 : enum tree_code tcode0 = code0, tcode1 = code1;
5440 5612853 : doing_div_or_mod = true;
5441 5612853 : warn_for_div_by_zero (location, fold_for_warn (op1));
5442 :
5443 5612853 : if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5444 43549 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5445 5612853 : if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5446 22545 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5447 :
5448 5612853 : if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5449 : resultcode = RDIV_EXPR;
5450 : else
5451 : {
5452 : /* When dividing two signed integers, we have to promote to int.
5453 : unless we divide by a constant != -1. Note that default
5454 : conversion will have been performed on the operands at this
5455 : point, so we have to dig out the original type to find out if
5456 : it was unsigned. */
5457 1574603 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5458 1574603 : shorten = may_shorten_divmod (op0, stripped_op1);
5459 : }
5460 :
5461 : common = 1;
5462 : }
5463 : break;
5464 :
5465 1195402 : case BIT_AND_EXPR:
5466 1195402 : case BIT_IOR_EXPR:
5467 1195402 : case BIT_XOR_EXPR:
5468 1195402 : if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5469 1195402 : || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5470 17566 : && !VECTOR_FLOAT_TYPE_P (type0)
5471 17542 : && !VECTOR_FLOAT_TYPE_P (type1)))
5472 : shorten = -1;
5473 : break;
5474 :
5475 725020 : case TRUNC_MOD_EXPR:
5476 725020 : case FLOOR_MOD_EXPR:
5477 725020 : doing_div_or_mod = true;
5478 725020 : warn_for_div_by_zero (location, fold_for_warn (op1));
5479 :
5480 725020 : if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5481 16 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5482 725036 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5483 : common = 1;
5484 725004 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5485 : {
5486 : /* Although it would be tempting to shorten always here, that loses
5487 : on some targets, since the modulo instruction is undefined if the
5488 : quotient can't be represented in the computation mode. We shorten
5489 : only if unsigned or if dividing by something we know != -1. */
5490 724972 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5491 724972 : shorten = may_shorten_divmod (op0, stripped_op1);
5492 724972 : common = 1;
5493 : }
5494 : break;
5495 :
5496 8307585 : case TRUTH_ANDIF_EXPR:
5497 8307585 : case TRUTH_ORIF_EXPR:
5498 8307585 : case TRUTH_AND_EXPR:
5499 8307585 : case TRUTH_OR_EXPR:
5500 8307585 : if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5501 : {
5502 4 : if (!COMPARISON_CLASS_P (op1))
5503 4 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5504 : build_zero_cst (type1), complain);
5505 4 : if (code == TRUTH_ANDIF_EXPR)
5506 : {
5507 0 : tree z = build_zero_cst (TREE_TYPE (op1));
5508 0 : return build_conditional_expr (location, op0, op1, z, complain);
5509 : }
5510 4 : else if (code == TRUTH_ORIF_EXPR)
5511 : {
5512 4 : tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5513 4 : return build_conditional_expr (location, op0, m1, op1, complain);
5514 : }
5515 : else
5516 0 : gcc_unreachable ();
5517 : }
5518 8307581 : if (gnu_vector_type_p (type0)
5519 8307581 : && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5520 : {
5521 32 : if (!COMPARISON_CLASS_P (op0))
5522 32 : op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5523 : build_zero_cst (type0), complain);
5524 32 : if (!VECTOR_TYPE_P (type1))
5525 : {
5526 12 : tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5527 12 : tree z = build_zero_cst (TREE_TYPE (op0));
5528 12 : op1 = build_conditional_expr (location, op1, m1, z, complain);
5529 : }
5530 20 : else if (!COMPARISON_CLASS_P (op1))
5531 20 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5532 : build_zero_cst (type1), complain);
5533 :
5534 32 : if (code == TRUTH_ANDIF_EXPR)
5535 : code = BIT_AND_EXPR;
5536 12 : else if (code == TRUTH_ORIF_EXPR)
5537 : code = BIT_IOR_EXPR;
5538 : else
5539 0 : gcc_unreachable ();
5540 :
5541 32 : return cp_build_binary_op (location, code, op0, op1, complain);
5542 : }
5543 :
5544 8307549 : result_type = boolean_type_node;
5545 8307549 : break;
5546 :
5547 : /* Shift operations: result has same type as first operand;
5548 : always convert second operand to int.
5549 : Also set SHORT_SHIFT if shifting rightward. */
5550 :
5551 589152 : case RSHIFT_EXPR:
5552 589152 : if (gnu_vector_type_p (type0)
5553 186 : && code1 == INTEGER_TYPE
5554 589213 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5555 : {
5556 : result_type = type0;
5557 : converted = 1;
5558 : }
5559 589091 : else if (gnu_vector_type_p (type0)
5560 125 : && gnu_vector_type_p (type1)
5561 125 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5562 121 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5563 589212 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5564 : TYPE_VECTOR_SUBPARTS (type1)))
5565 : {
5566 : result_type = type0;
5567 : converted = 1;
5568 : }
5569 588970 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5570 : {
5571 588934 : tree const_op1 = fold_for_warn (op1);
5572 588934 : if (TREE_CODE (const_op1) != INTEGER_CST)
5573 55733 : const_op1 = op1;
5574 588934 : result_type = type0;
5575 588934 : doing_shift = true;
5576 588934 : if (TREE_CODE (const_op1) == INTEGER_CST)
5577 : {
5578 533201 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5579 : {
5580 63 : if ((complain & tf_warning)
5581 63 : && c_inhibit_evaluation_warnings == 0)
5582 47 : warning_at (location, OPT_Wshift_count_negative,
5583 : "right shift count is negative");
5584 : }
5585 : else
5586 : {
5587 533138 : if (!integer_zerop (const_op1))
5588 533047 : short_shift = 1;
5589 :
5590 533138 : if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5591 59 : && (complain & tf_warning)
5592 533197 : && c_inhibit_evaluation_warnings == 0)
5593 39 : warning_at (location, OPT_Wshift_count_overflow,
5594 : "right shift count >= width of type");
5595 : }
5596 : }
5597 : /* Avoid converting op1 to result_type later. */
5598 : converted = 1;
5599 : }
5600 : break;
5601 :
5602 1940621 : case LSHIFT_EXPR:
5603 1940621 : if (gnu_vector_type_p (type0)
5604 202 : && code1 == INTEGER_TYPE
5605 1940672 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5606 : {
5607 : result_type = type0;
5608 : converted = 1;
5609 : }
5610 1940570 : else if (gnu_vector_type_p (type0)
5611 151 : && gnu_vector_type_p (type1)
5612 151 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5613 147 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5614 1940713 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5615 : TYPE_VECTOR_SUBPARTS (type1)))
5616 : {
5617 : result_type = type0;
5618 : converted = 1;
5619 : }
5620 1940427 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5621 : {
5622 1940387 : tree const_op0 = fold_for_warn (op0);
5623 1940387 : if (TREE_CODE (const_op0) != INTEGER_CST)
5624 139696 : const_op0 = op0;
5625 1940387 : tree const_op1 = fold_for_warn (op1);
5626 1940387 : if (TREE_CODE (const_op1) != INTEGER_CST)
5627 159824 : const_op1 = op1;
5628 1940387 : result_type = type0;
5629 1940387 : doing_shift = true;
5630 1940387 : if (TREE_CODE (const_op0) == INTEGER_CST
5631 1800691 : && tree_int_cst_sgn (const_op0) < 0
5632 345 : && !TYPE_OVERFLOW_WRAPS (type0)
5633 238 : && (complain & tf_warning)
5634 1940625 : && c_inhibit_evaluation_warnings == 0)
5635 238 : warning_at (location, OPT_Wshift_negative_value,
5636 : "left shift of negative value");
5637 1940387 : if (TREE_CODE (const_op1) == INTEGER_CST)
5638 : {
5639 1780563 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5640 : {
5641 73 : if ((complain & tf_warning)
5642 73 : && c_inhibit_evaluation_warnings == 0)
5643 51 : warning_at (location, OPT_Wshift_count_negative,
5644 : "left shift count is negative");
5645 : }
5646 1780490 : else if (compare_tree_int (const_op1,
5647 1780490 : TYPE_PRECISION (type0)) >= 0)
5648 : {
5649 109 : if ((complain & tf_warning)
5650 81 : && c_inhibit_evaluation_warnings == 0)
5651 59 : warning_at (location, OPT_Wshift_count_overflow,
5652 : "left shift count >= width of type");
5653 : }
5654 1780381 : else if (TREE_CODE (const_op0) == INTEGER_CST
5655 1741167 : && (complain & tf_warning))
5656 1622919 : maybe_warn_shift_overflow (location, const_op0, const_op1);
5657 : }
5658 : /* Avoid converting op1 to result_type later. */
5659 : converted = 1;
5660 : }
5661 : break;
5662 :
5663 11900124 : case EQ_EXPR:
5664 11900124 : case NE_EXPR:
5665 11900124 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5666 2480 : goto vector_compare;
5667 11897644 : if ((complain & tf_warning)
5668 11004962 : && c_inhibit_evaluation_warnings == 0
5669 22666700 : && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5670 591705 : warning_at (location, OPT_Wfloat_equal,
5671 : "comparing floating-point with %<==%> "
5672 : "or %<!=%> is unsafe");
5673 11897644 : if (complain & tf_warning)
5674 : {
5675 11004962 : tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5676 11004962 : tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5677 11004962 : if ((TREE_CODE (stripped_orig_op0) == STRING_CST
5678 52 : && !integer_zerop (cp_fully_fold (op1)))
5679 11005010 : || (TREE_CODE (stripped_orig_op1) == STRING_CST
5680 75 : && !integer_zerop (cp_fully_fold (op0))))
5681 47 : warning_at (location, OPT_Waddress,
5682 : "comparison with string literal results in "
5683 : "unspecified behavior");
5684 11004915 : else if (warn_array_compare
5685 317728 : && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5686 11005055 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5687 12 : do_warn_array_compare (location, code, stripped_orig_op0,
5688 : stripped_orig_op1);
5689 : }
5690 :
5691 11897644 : build_type = boolean_type_node;
5692 11897644 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5693 11897644 : || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5694 9890380 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5695 9890380 : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5696 : short_compare = 1;
5697 3681 : else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5698 2006808 : && null_ptr_cst_p (orig_op1))
5699 : /* Handle, eg, (void*)0 (c++/43906), and more. */
5700 2967615 : || (code0 == POINTER_TYPE
5701 954730 : && TYPE_PTR_P (type1) && integer_zerop (op1)))
5702 : {
5703 1065657 : if (TYPE_PTR_P (type1))
5704 13986 : result_type = composite_pointer_type (location,
5705 : type0, type1, op0, op1,
5706 : CPO_COMPARISON, complain);
5707 : else
5708 : result_type = type0;
5709 :
5710 1065657 : if (char_type_p (TREE_TYPE (orig_op1)))
5711 : {
5712 10 : auto_diagnostic_group d;
5713 10 : if (warning_at (location, OPT_Wpointer_compare,
5714 : "comparison between pointer and zero character "
5715 : "constant"))
5716 10 : inform (location,
5717 : "did you mean to dereference the pointer?");
5718 10 : }
5719 1065657 : warn_for_null_address (location, op0, complain);
5720 : }
5721 940 : else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5722 943481 : && null_ptr_cst_p (orig_op0))
5723 : /* Handle, eg, (void*)0 (c++/43906), and more. */
5724 1886809 : || (code1 == POINTER_TYPE
5725 941911 : && TYPE_PTR_P (type0) && integer_zerop (op0)))
5726 : {
5727 1249 : if (TYPE_PTR_P (type0))
5728 86 : result_type = composite_pointer_type (location,
5729 : type0, type1, op0, op1,
5730 : CPO_COMPARISON, complain);
5731 : else
5732 : result_type = type1;
5733 :
5734 1249 : if (char_type_p (TREE_TYPE (orig_op0)))
5735 : {
5736 10 : auto_diagnostic_group d;
5737 10 : if (warning_at (location, OPT_Wpointer_compare,
5738 : "comparison between pointer and zero character "
5739 : "constant"))
5740 10 : inform (location,
5741 : "did you mean to dereference the pointer?");
5742 10 : }
5743 1249 : warn_for_null_address (location, op1, complain);
5744 : }
5745 942737 : else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5746 2137 : || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5747 941007 : result_type = composite_pointer_type (location,
5748 : type0, type1, op0, op1,
5749 : CPO_COMPARISON, complain);
5750 1730 : else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5751 : /* One of the operands must be of nullptr_t type. */
5752 57 : result_type = TREE_TYPE (nullptr_node);
5753 1673 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5754 : {
5755 58 : result_type = type0;
5756 58 : if (complain & tf_error)
5757 56 : permerror (location, "ISO C++ forbids comparison between "
5758 : "pointer and integer");
5759 : else
5760 2 : return error_mark_node;
5761 : }
5762 1615 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5763 : {
5764 1225 : result_type = type1;
5765 1225 : if (complain & tf_error)
5766 70 : permerror (location, "ISO C++ forbids comparison between "
5767 : "pointer and integer");
5768 : else
5769 1155 : return error_mark_node;
5770 : }
5771 390 : else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5772 : {
5773 221 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5774 : == ptrmemfunc_vbit_in_delta)
5775 : {
5776 : tree pfn0, delta0, e1, e2;
5777 :
5778 : if (TREE_SIDE_EFFECTS (op0))
5779 : op0 = cp_save_expr (op0);
5780 :
5781 : pfn0 = pfn_from_ptrmemfunc (op0);
5782 : delta0 = delta_from_ptrmemfunc (op0);
5783 : {
5784 : /* If we will warn below about a null-address compare
5785 : involving the orig_op0 ptrmemfunc, we'd likely also
5786 : warn about the pfn0's null-address compare, and
5787 : that would be redundant, so suppress it. */
5788 : warning_sentinel ws (warn_address);
5789 : e1 = cp_build_binary_op (location,
5790 : EQ_EXPR,
5791 : pfn0,
5792 : build_zero_cst (TREE_TYPE (pfn0)),
5793 : complain);
5794 : }
5795 : e2 = cp_build_binary_op (location,
5796 : BIT_AND_EXPR,
5797 : delta0,
5798 : integer_one_node,
5799 : complain);
5800 :
5801 : if (complain & tf_warning)
5802 : maybe_warn_zero_as_null_pointer_constant (op1, input_location);
5803 :
5804 : e2 = cp_build_binary_op (location,
5805 : EQ_EXPR, e2, integer_zero_node,
5806 : complain);
5807 : op0 = cp_build_binary_op (location,
5808 : TRUTH_ANDIF_EXPR, e1, e2,
5809 : complain);
5810 : op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
5811 : }
5812 : else
5813 : {
5814 221 : op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
5815 221 : op1 = cp_convert (TREE_TYPE (op0), op1, complain);
5816 : }
5817 221 : result_type = TREE_TYPE (op0);
5818 :
5819 221 : warn_for_null_address (location, orig_op0, complain);
5820 : }
5821 169 : else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
5822 40 : return cp_build_binary_op (location, code, op1, op0, complain);
5823 129 : else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
5824 : {
5825 105 : tree type;
5826 : /* E will be the final comparison. */
5827 105 : tree e;
5828 : /* E1 and E2 are for scratch. */
5829 105 : tree e1;
5830 105 : tree e2;
5831 105 : tree pfn0;
5832 105 : tree pfn1;
5833 105 : tree delta0;
5834 105 : tree delta1;
5835 :
5836 105 : type = composite_pointer_type (location, type0, type1, op0, op1,
5837 : CPO_COMPARISON, complain);
5838 :
5839 105 : if (!same_type_p (TREE_TYPE (op0), type))
5840 8 : op0 = cp_convert_and_check (type, op0, complain);
5841 105 : if (!same_type_p (TREE_TYPE (op1), type))
5842 12 : op1 = cp_convert_and_check (type, op1, complain);
5843 :
5844 105 : if (op0 == error_mark_node || op1 == error_mark_node)
5845 : return error_mark_node;
5846 :
5847 101 : if (TREE_SIDE_EFFECTS (op0))
5848 34 : op0 = save_expr (op0);
5849 101 : if (TREE_SIDE_EFFECTS (op1))
5850 0 : op1 = save_expr (op1);
5851 :
5852 101 : pfn0 = pfn_from_ptrmemfunc (op0);
5853 101 : pfn0 = cp_fully_fold (pfn0);
5854 : /* Avoid -Waddress warnings (c++/64877). */
5855 101 : if (TREE_CODE (pfn0) == ADDR_EXPR)
5856 11 : suppress_warning (pfn0, OPT_Waddress);
5857 101 : pfn1 = pfn_from_ptrmemfunc (op1);
5858 101 : pfn1 = cp_fully_fold (pfn1);
5859 101 : delta0 = delta_from_ptrmemfunc (op0);
5860 101 : delta1 = delta_from_ptrmemfunc (op1);
5861 101 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5862 : == ptrmemfunc_vbit_in_delta)
5863 : {
5864 : /* We generate:
5865 :
5866 : (op0.pfn == op1.pfn
5867 : && ((op0.delta == op1.delta)
5868 : || (!op0.pfn && op0.delta & 1 == 0
5869 : && op1.delta & 1 == 0))
5870 :
5871 : The reason for the `!op0.pfn' bit is that a NULL
5872 : pointer-to-member is any member with a zero PFN and
5873 : LSB of the DELTA field is 0. */
5874 :
5875 : e1 = cp_build_binary_op (location, BIT_AND_EXPR,
5876 : delta0,
5877 : integer_one_node,
5878 : complain);
5879 : e1 = cp_build_binary_op (location,
5880 : EQ_EXPR, e1, integer_zero_node,
5881 : complain);
5882 : e2 = cp_build_binary_op (location, BIT_AND_EXPR,
5883 : delta1,
5884 : integer_one_node,
5885 : complain);
5886 : e2 = cp_build_binary_op (location,
5887 : EQ_EXPR, e2, integer_zero_node,
5888 : complain);
5889 : e1 = cp_build_binary_op (location,
5890 : TRUTH_ANDIF_EXPR, e2, e1,
5891 : complain);
5892 : e2 = cp_build_binary_op (location, EQ_EXPR,
5893 : pfn0,
5894 : build_zero_cst (TREE_TYPE (pfn0)),
5895 : complain);
5896 : e2 = cp_build_binary_op (location,
5897 : TRUTH_ANDIF_EXPR, e2, e1, complain);
5898 : e1 = cp_build_binary_op (location,
5899 : EQ_EXPR, delta0, delta1, complain);
5900 : e1 = cp_build_binary_op (location,
5901 : TRUTH_ORIF_EXPR, e1, e2, complain);
5902 : }
5903 : else
5904 : {
5905 : /* We generate:
5906 :
5907 : (op0.pfn == op1.pfn
5908 : && (!op0.pfn || op0.delta == op1.delta))
5909 :
5910 : The reason for the `!op0.pfn' bit is that a NULL
5911 : pointer-to-member is any member with a zero PFN; the
5912 : DELTA field is unspecified. */
5913 :
5914 101 : e1 = cp_build_binary_op (location,
5915 : EQ_EXPR, delta0, delta1, complain);
5916 101 : e2 = cp_build_binary_op (location,
5917 : EQ_EXPR,
5918 : pfn0,
5919 101 : build_zero_cst (TREE_TYPE (pfn0)),
5920 : complain);
5921 101 : e1 = cp_build_binary_op (location,
5922 : TRUTH_ORIF_EXPR, e1, e2, complain);
5923 : }
5924 101 : e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
5925 101 : e = cp_build_binary_op (location,
5926 : TRUTH_ANDIF_EXPR, e2, e1, complain);
5927 101 : if (code == EQ_EXPR)
5928 : return e;
5929 32 : return cp_build_binary_op (location,
5930 32 : EQ_EXPR, e, integer_zero_node, complain);
5931 : }
5932 : else
5933 : {
5934 24 : gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
5935 : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
5936 : type1));
5937 24 : gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
5938 : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
5939 : type0));
5940 : }
5941 :
5942 : break;
5943 :
5944 292 : case MAX_EXPR:
5945 292 : case MIN_EXPR:
5946 292 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
5947 292 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
5948 : shorten = 1;
5949 0 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5950 0 : result_type = composite_pointer_type (location,
5951 : type0, type1, op0, op1,
5952 : CPO_COMPARISON, complain);
5953 : break;
5954 :
5955 12718130 : case LE_EXPR:
5956 12718130 : case GE_EXPR:
5957 12718130 : case LT_EXPR:
5958 12718130 : case GT_EXPR:
5959 12718130 : case SPACESHIP_EXPR:
5960 12718130 : if (TREE_CODE (orig_op0) == STRING_CST
5961 12718130 : || TREE_CODE (orig_op1) == STRING_CST)
5962 : {
5963 0 : if (complain & tf_warning)
5964 0 : warning_at (location, OPT_Waddress,
5965 : "comparison with string literal results "
5966 : "in unspecified behavior");
5967 : }
5968 12718130 : else if (warn_array_compare
5969 187709 : && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5970 17 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
5971 17 : && code != SPACESHIP_EXPR
5972 12718146 : && (complain & tf_warning))
5973 16 : do_warn_array_compare (location, code,
5974 : tree_strip_any_location_wrapper (orig_op0),
5975 : tree_strip_any_location_wrapper (orig_op1));
5976 :
5977 12718130 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5978 : {
5979 6823 : vector_compare:
5980 6823 : tree intt;
5981 6823 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5982 6823 : TREE_TYPE (type1))
5983 6823 : && !vector_types_compatible_elements_p (type0, type1))
5984 : {
5985 4 : if (complain & tf_error)
5986 : {
5987 4 : error_at (location, "comparing vectors with different "
5988 : "element types");
5989 4 : inform (location, "operand types are %qT and %qT",
5990 : type0, type1);
5991 : }
5992 4 : return error_mark_node;
5993 : }
5994 :
5995 6819 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5996 13638 : TYPE_VECTOR_SUBPARTS (type1)))
5997 : {
5998 4 : if (complain & tf_error)
5999 : {
6000 4 : error_at (location, "comparing vectors with different "
6001 : "number of elements");
6002 4 : inform (location, "operand types are %qT and %qT",
6003 : type0, type1);
6004 : }
6005 4 : return error_mark_node;
6006 : }
6007 :
6008 : /* It's not precisely specified how the usual arithmetic
6009 : conversions apply to the vector types. Here, we use
6010 : the unsigned type if one of the operands is signed and
6011 : the other one is unsigned. */
6012 6815 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6013 : {
6014 48 : if (!TYPE_UNSIGNED (type0))
6015 48 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6016 : else
6017 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6018 48 : warning_at (location, OPT_Wsign_compare, "comparison between "
6019 : "types %qT and %qT", type0, type1);
6020 : }
6021 :
6022 6815 : if (resultcode == SPACESHIP_EXPR)
6023 : {
6024 1 : if (complain & tf_error)
6025 1 : sorry_at (location, "three-way comparison of vectors");
6026 1 : return error_mark_node;
6027 : }
6028 :
6029 : /* Always construct signed integer vector type. */
6030 6814 : intt = c_common_type_for_size
6031 13628 : (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
6032 6814 : if (!intt)
6033 : {
6034 0 : if (complain & tf_error)
6035 0 : error_at (location, "could not find an integer type "
6036 0 : "of the same size as %qT", TREE_TYPE (type0));
6037 0 : return error_mark_node;
6038 : }
6039 6814 : result_type = build_opaque_vector_type (intt,
6040 6814 : TYPE_VECTOR_SUBPARTS (type0));
6041 6814 : return build_vec_cmp (resultcode, result_type, op0, op1);
6042 : }
6043 12713787 : build_type = boolean_type_node;
6044 12713787 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6045 12713787 : || code0 == ENUMERAL_TYPE)
6046 12413283 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6047 12413283 : || code1 == ENUMERAL_TYPE))
6048 : short_compare = 1;
6049 300594 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6050 300370 : result_type = composite_pointer_type (location,
6051 : type0, type1, op0, op1,
6052 : CPO_COMPARISON, complain);
6053 79 : else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6054 148 : || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6055 318 : || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6056 : {
6057 : /* Core Issue 1512 made this ill-formed. */
6058 181 : if (complain & tf_error)
6059 178 : error_at (location, "ordered comparison of pointer with "
6060 : "integer zero (%qT and %qT)", type0, type1);
6061 181 : return error_mark_node;
6062 : }
6063 43 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6064 : {
6065 0 : result_type = type0;
6066 0 : if (complain & tf_error)
6067 0 : permerror (location, "ISO C++ forbids comparison between "
6068 : "pointer and integer");
6069 : else
6070 0 : return error_mark_node;
6071 : }
6072 43 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6073 : {
6074 32 : result_type = type1;
6075 32 : if (complain & tf_error)
6076 32 : permerror (location, "ISO C++ forbids comparison between "
6077 : "pointer and integer");
6078 : else
6079 0 : return error_mark_node;
6080 : }
6081 :
6082 12713606 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6083 300405 : && !processing_template_decl
6084 12869639 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6085 : {
6086 49 : op0 = save_expr (op0);
6087 49 : op1 = save_expr (op1);
6088 :
6089 49 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6090 49 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6091 : }
6092 :
6093 : break;
6094 :
6095 0 : case UNORDERED_EXPR:
6096 0 : case ORDERED_EXPR:
6097 0 : case UNLT_EXPR:
6098 0 : case UNLE_EXPR:
6099 0 : case UNGT_EXPR:
6100 0 : case UNGE_EXPR:
6101 0 : case UNEQ_EXPR:
6102 0 : build_type = integer_type_node;
6103 0 : if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6104 : {
6105 0 : if (complain & tf_error)
6106 0 : error ("unordered comparison on non-floating-point argument");
6107 0 : return error_mark_node;
6108 : }
6109 : common = 1;
6110 : break;
6111 :
6112 : default:
6113 : break;
6114 : }
6115 :
6116 75034491 : if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6117 12285952 : || code0 == ENUMERAL_TYPE)
6118 63163497 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6119 63163497 : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6120 : arithmetic_types_p = 1;
6121 : else
6122 : {
6123 11971202 : arithmetic_types_p = 0;
6124 : /* Vector arithmetic is only allowed when both sides are vectors. */
6125 11971202 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6126 : {
6127 51743 : if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6128 51743 : || !vector_types_compatible_elements_p (type0, type1))
6129 : {
6130 24 : if (complain & tf_error)
6131 : {
6132 : /* "location" already embeds the locations of the
6133 : operands, so we don't need to add them separately
6134 : to richloc. */
6135 24 : rich_location richloc (line_table, location);
6136 24 : binary_op_error (&richloc, code, type0, type1);
6137 24 : }
6138 24 : return error_mark_node;
6139 : }
6140 : arithmetic_types_p = 1;
6141 : }
6142 : }
6143 : /* Determine the RESULT_TYPE, if it is not already known. */
6144 75034467 : if (!result_type
6145 75034467 : && arithmetic_types_p
6146 60521196 : && (shorten || common || short_compare))
6147 : {
6148 60521102 : result_type = cp_common_type (type0, type1);
6149 60521102 : if (result_type == error_mark_node)
6150 : {
6151 0 : tree t1 = type0;
6152 0 : tree t2 = type1;
6153 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6154 0 : t1 = TREE_TYPE (t1);
6155 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6156 0 : t2 = TREE_TYPE (t2);
6157 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6158 : && TREE_CODE (t2) == REAL_TYPE
6159 : && (extended_float_type_p (t1)
6160 : || extended_float_type_p (t2))
6161 : && cp_compare_floating_point_conversion_ranks
6162 : (t1, t2) == 3);
6163 0 : if (complain & tf_error)
6164 : {
6165 0 : rich_location richloc (line_table, location);
6166 0 : binary_op_error (&richloc, code, type0, type1);
6167 0 : }
6168 0 : return error_mark_node;
6169 : }
6170 60521102 : if (complain & tf_warning)
6171 : {
6172 57496541 : do_warn_double_promotion (result_type, type0, type1,
6173 : "implicit conversion from %qH to %qI "
6174 : "to match other operand of binary "
6175 : "expression",
6176 : location);
6177 57496541 : do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6178 57496541 : TREE_TYPE (orig_op1));
6179 : }
6180 : }
6181 75034467 : if (may_need_excess_precision
6182 57030898 : && (orig_type0 != type0 || orig_type1 != type1)
6183 3368 : && build_type == NULL_TREE
6184 3368 : && result_type)
6185 : {
6186 3094 : gcc_assert (common);
6187 3094 : semantic_result_type = cp_common_type (orig_type0, orig_type1);
6188 3094 : if (semantic_result_type == error_mark_node)
6189 : {
6190 0 : tree t1 = orig_type0;
6191 0 : tree t2 = orig_type1;
6192 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6193 0 : t1 = TREE_TYPE (t1);
6194 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6195 0 : t2 = TREE_TYPE (t2);
6196 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6197 : && TREE_CODE (t2) == REAL_TYPE
6198 : && (extended_float_type_p (t1)
6199 : || extended_float_type_p (t2))
6200 : && cp_compare_floating_point_conversion_ranks
6201 : (t1, t2) == 3);
6202 0 : if (complain & tf_error)
6203 : {
6204 0 : rich_location richloc (line_table, location);
6205 0 : binary_op_error (&richloc, code, type0, type1);
6206 0 : }
6207 0 : return error_mark_node;
6208 : }
6209 : }
6210 :
6211 75034467 : if (code == SPACESHIP_EXPR)
6212 : {
6213 21149 : iloc_sentinel s (location);
6214 :
6215 21149 : tree orig_type0 = TREE_TYPE (orig_op0);
6216 21149 : tree_code orig_code0 = TREE_CODE (orig_type0);
6217 21149 : tree orig_type1 = TREE_TYPE (orig_op1);
6218 21149 : tree_code orig_code1 = TREE_CODE (orig_type1);
6219 21149 : if (!result_type || result_type == error_mark_node)
6220 : /* Nope. */
6221 : result_type = NULL_TREE;
6222 21142 : else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6223 : /* "If one of the operands is of type bool and the other is not, the
6224 : program is ill-formed." */
6225 : result_type = NULL_TREE;
6226 21141 : else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6227 9 : && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6228 : /* We only do array/function-to-pointer conversion if "at least one of
6229 : the operands is of pointer type". */
6230 : result_type = NULL_TREE;
6231 21132 : else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6232 : /* <=> no longer supports equality relations. */
6233 : result_type = NULL_TREE;
6234 21131 : else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6235 21145 : && !(same_type_ignoring_top_level_qualifiers_p
6236 14 : (orig_type0, orig_type1)))
6237 : /* "If both operands have arithmetic types, or one operand has integral
6238 : type and the other operand has unscoped enumeration type, the usual
6239 : arithmetic conversions are applied to the operands." So we don't do
6240 : arithmetic conversions if the operands both have enumeral type. */
6241 : result_type = NULL_TREE;
6242 21126 : else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6243 21124 : || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6244 : /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6245 : [where one is of enumeration type and the other is of a different
6246 : enumeration type or a floating-point type] are ill-formed. */
6247 : result_type = NULL_TREE;
6248 :
6249 21122 : if (result_type)
6250 : {
6251 21122 : build_type = spaceship_type (result_type, complain);
6252 21122 : if (build_type == error_mark_node)
6253 : return error_mark_node;
6254 : }
6255 :
6256 21144 : if (result_type && arithmetic_types_p)
6257 : {
6258 : /* If a narrowing conversion is required, other than from an integral
6259 : type to a floating point type, the program is ill-formed. */
6260 11151 : bool ok = true;
6261 11151 : if (TREE_CODE (result_type) == REAL_TYPE
6262 203 : && CP_INTEGRAL_TYPE_P (orig_type0))
6263 : /* OK */;
6264 11146 : else if (!check_narrowing (result_type, orig_op0, complain))
6265 11151 : ok = false;
6266 11151 : if (TREE_CODE (result_type) == REAL_TYPE
6267 203 : && CP_INTEGRAL_TYPE_P (orig_type1))
6268 : /* OK */;
6269 11146 : else if (!check_narrowing (result_type, orig_op1, complain))
6270 : ok = false;
6271 11151 : if (!ok && !(complain & tf_error))
6272 2 : return error_mark_node;
6273 : }
6274 21149 : }
6275 :
6276 75034460 : if (!result_type)
6277 : {
6278 481 : if (complain & tf_error)
6279 : {
6280 190 : binary_op_rich_location richloc (location,
6281 190 : orig_op0, orig_op1, true);
6282 190 : error_at (&richloc,
6283 : "invalid operands of types %qT and %qT to binary %qO",
6284 190 : TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6285 190 : }
6286 481 : return error_mark_node;
6287 : }
6288 :
6289 : /* If we're in a template, the only thing we need to know is the
6290 : RESULT_TYPE. */
6291 75033979 : if (processing_template_decl)
6292 : {
6293 : /* Since the middle-end checks the type when doing a build2, we
6294 : need to build the tree in pieces. This built tree will never
6295 : get out of the front-end as we replace it when instantiating
6296 : the template. */
6297 32352446 : tree tmp = build2 (resultcode,
6298 : build_type ? build_type : result_type,
6299 : NULL_TREE, op1);
6300 20393481 : TREE_OPERAND (tmp, 0) = op0;
6301 20393481 : if (semantic_result_type)
6302 0 : tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6303 20393481 : return tmp;
6304 : }
6305 :
6306 : /* Remember the original type; RESULT_TYPE might be changed later on
6307 : by shorten_binary_op. */
6308 54640498 : tree orig_type = result_type;
6309 :
6310 54640498 : if (arithmetic_types_p)
6311 : {
6312 48296855 : bool first_complex = (code0 == COMPLEX_TYPE);
6313 48296855 : bool second_complex = (code1 == COMPLEX_TYPE);
6314 48296855 : int none_complex = (!first_complex && !second_complex);
6315 :
6316 : /* Adapted from patch for c/24581. */
6317 48296855 : if (first_complex != second_complex
6318 130878 : && (code == PLUS_EXPR
6319 : || code == MINUS_EXPR
6320 130878 : || code == MULT_EXPR
6321 34528 : || (code == TRUNC_DIV_EXPR && first_complex))
6322 117362 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6323 48414020 : && flag_signed_zeros)
6324 : {
6325 : /* An operation on mixed real/complex operands must be
6326 : handled specially, but the language-independent code can
6327 : more easily optimize the plain complex arithmetic if
6328 : -fno-signed-zeros. */
6329 117165 : tree real_type = TREE_TYPE (result_type);
6330 117165 : tree real, imag;
6331 117165 : if (first_complex)
6332 : {
6333 84075 : if (TREE_TYPE (op0) != result_type)
6334 8 : op0 = cp_convert_and_check (result_type, op0, complain);
6335 84075 : if (TREE_TYPE (op1) != real_type)
6336 32 : op1 = cp_convert_and_check (real_type, op1, complain);
6337 : }
6338 : else
6339 : {
6340 33090 : if (TREE_TYPE (op0) != real_type)
6341 36 : op0 = cp_convert_and_check (real_type, op0, complain);
6342 33090 : if (TREE_TYPE (op1) != result_type)
6343 35 : op1 = cp_convert_and_check (result_type, op1, complain);
6344 : }
6345 117165 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6346 0 : return error_mark_node;
6347 117165 : if (first_complex)
6348 : {
6349 84075 : op0 = save_expr (op0);
6350 84075 : real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6351 84075 : imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6352 84075 : switch (code)
6353 : {
6354 42017 : case MULT_EXPR:
6355 42017 : case TRUNC_DIV_EXPR:
6356 42017 : op1 = save_expr (op1);
6357 42017 : imag = build2 (resultcode, real_type, imag, op1);
6358 : /* Fall through. */
6359 84075 : case PLUS_EXPR:
6360 84075 : case MINUS_EXPR:
6361 84075 : real = build2 (resultcode, real_type, real, op1);
6362 84075 : break;
6363 0 : default:
6364 0 : gcc_unreachable();
6365 : }
6366 : }
6367 : else
6368 : {
6369 33090 : op1 = save_expr (op1);
6370 33090 : real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6371 33090 : imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6372 33090 : switch (code)
6373 : {
6374 701 : case MULT_EXPR:
6375 701 : op0 = save_expr (op0);
6376 701 : imag = build2 (resultcode, real_type, op0, imag);
6377 : /* Fall through. */
6378 17342 : case PLUS_EXPR:
6379 17342 : real = build2 (resultcode, real_type, op0, real);
6380 17342 : break;
6381 15748 : case MINUS_EXPR:
6382 15748 : real = build2 (resultcode, real_type, op0, real);
6383 15748 : imag = build1 (NEGATE_EXPR, real_type, imag);
6384 15748 : break;
6385 0 : default:
6386 0 : gcc_unreachable();
6387 : }
6388 : }
6389 117165 : result = build2 (COMPLEX_EXPR, result_type, real, imag);
6390 117165 : if (semantic_result_type)
6391 32 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6392 : result);
6393 117165 : return result;
6394 : }
6395 :
6396 : /* For certain operations (which identify themselves by shorten != 0)
6397 : if both args were extended from the same smaller type,
6398 : do the arithmetic in that type and then extend.
6399 :
6400 : shorten !=0 and !=1 indicates a bitwise operation.
6401 : For them, this optimization is safe only if
6402 : both args are zero-extended or both are sign-extended.
6403 : Otherwise, we might change the result.
6404 : E.g., (short)-1 | (unsigned short)-1 is (int)-1
6405 : but calculated in (unsigned short) it would be (unsigned short)-1. */
6406 :
6407 48179690 : if (shorten && none_complex)
6408 : {
6409 1779888 : final_type = result_type;
6410 1779888 : result_type = shorten_binary_op (result_type, op0, op1,
6411 : shorten == -1);
6412 : }
6413 :
6414 : /* Shifts can be shortened if shifting right. */
6415 :
6416 48179690 : if (short_shift)
6417 : {
6418 494947 : int unsigned_arg;
6419 494947 : tree arg0 = get_narrower (op0, &unsigned_arg);
6420 : /* We're not really warning here but when we set short_shift we
6421 : used fold_for_warn to fold the operand. */
6422 494947 : tree const_op1 = fold_for_warn (op1);
6423 :
6424 494947 : final_type = result_type;
6425 :
6426 494947 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
6427 462448 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6428 :
6429 494947 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6430 780 : && tree_int_cst_sgn (const_op1) > 0
6431 : /* We can shorten only if the shift count is less than the
6432 : number of bits in the smaller type size. */
6433 780 : && compare_tree_int (const_op1,
6434 780 : TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6435 : /* We cannot drop an unsigned shift after sign-extension. */
6436 495717 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6437 : {
6438 : /* Do an unsigned shift if the operand was zero-extended. */
6439 763 : result_type
6440 763 : = c_common_signed_or_unsigned_type (unsigned_arg,
6441 763 : TREE_TYPE (arg0));
6442 : /* Convert value-to-be-shifted to that type. */
6443 763 : if (TREE_TYPE (op0) != result_type)
6444 763 : op0 = convert (result_type, op0);
6445 : converted = 1;
6446 : }
6447 : }
6448 :
6449 : /* Comparison operations are shortened too but differently.
6450 : They identify themselves by setting short_compare = 1. */
6451 :
6452 48179690 : if (short_compare)
6453 : {
6454 : /* We call shorten_compare only for diagnostics. */
6455 14277652 : tree xop0 = fold_simple (op0);
6456 14277652 : tree xop1 = fold_simple (op1);
6457 14277652 : tree xresult_type = result_type;
6458 14277652 : enum tree_code xresultcode = resultcode;
6459 14277652 : shorten_compare (location, &xop0, &xop1, &xresult_type,
6460 : &xresultcode);
6461 : }
6462 :
6463 33901932 : if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6464 14277882 : && warn_sign_compare
6465 : /* Do not warn until the template is instantiated; we cannot
6466 : bound the ranges of the arguments until that point. */
6467 280489 : && !processing_template_decl
6468 280489 : && (complain & tf_warning)
6469 273292 : && c_inhibit_evaluation_warnings == 0
6470 : /* Even unsigned enum types promote to signed int. We don't
6471 : want to issue -Wsign-compare warnings for this case. */
6472 260568 : && !enum_cast_to_int (orig_op0)
6473 48440258 : && !enum_cast_to_int (orig_op1))
6474 : {
6475 260536 : warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6476 : result_type, resultcode);
6477 : }
6478 : }
6479 :
6480 : /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6481 : Then the expression will be built.
6482 : It will be given type FINAL_TYPE if that is nonzero;
6483 : otherwise, it will be given type RESULT_TYPE. */
6484 54523333 : if (! converted)
6485 : {
6486 52193569 : warning_sentinel w (warn_sign_conversion, short_compare);
6487 52193569 : if (!same_type_p (TREE_TYPE (op0), result_type))
6488 2143783 : op0 = cp_convert_and_check (result_type, op0, complain);
6489 52193569 : if (!same_type_p (TREE_TYPE (op1), result_type))
6490 8921733 : op1 = cp_convert_and_check (result_type, op1, complain);
6491 :
6492 52193569 : if (op0 == error_mark_node || op1 == error_mark_node)
6493 67 : return error_mark_node;
6494 52193569 : }
6495 :
6496 54523266 : if (build_type == NULL_TREE)
6497 38347945 : build_type = result_type;
6498 :
6499 54523266 : if (doing_shift
6500 2329136 : && flag_strong_eval_order == 2
6501 2194476 : && TREE_SIDE_EFFECTS (op1)
6502 54537380 : && !processing_template_decl)
6503 : {
6504 : /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6505 : op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6506 14114 : op0 = cp_save_expr (op0);
6507 14114 : instrument_expr = op0;
6508 : }
6509 :
6510 54523266 : if (sanitize_flags_p ((SANITIZE_SHIFT
6511 : | SANITIZE_DIVIDE
6512 : | SANITIZE_FLOAT_DIVIDE
6513 : | SANITIZE_SI_OVERFLOW))
6514 11739 : && current_function_decl != NULL_TREE
6515 9354 : && !processing_template_decl
6516 54532620 : && (doing_div_or_mod || doing_shift))
6517 : {
6518 : /* OP0 and/or OP1 might have side-effects. */
6519 1017 : op0 = cp_save_expr (op0);
6520 1017 : op1 = cp_save_expr (op1);
6521 1017 : op0 = fold_non_dependent_expr (op0, complain);
6522 1017 : op1 = fold_non_dependent_expr (op1, complain);
6523 1017 : tree instrument_expr1 = NULL_TREE;
6524 1017 : if (doing_div_or_mod
6525 1017 : && sanitize_flags_p (SANITIZE_DIVIDE
6526 : | SANITIZE_FLOAT_DIVIDE
6527 : | SANITIZE_SI_OVERFLOW))
6528 : {
6529 : /* For diagnostics we want to use the promoted types without
6530 : shorten_binary_op. So convert the arguments to the
6531 : original result_type. */
6532 555 : tree cop0 = op0;
6533 555 : tree cop1 = op1;
6534 555 : if (TREE_TYPE (cop0) != orig_type)
6535 8 : cop0 = cp_convert (orig_type, op0, complain);
6536 555 : if (TREE_TYPE (cop1) != orig_type)
6537 37 : cop1 = cp_convert (orig_type, op1, complain);
6538 555 : instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6539 : }
6540 462 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6541 354 : instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6542 1017 : if (instrument_expr != NULL)
6543 18 : instrument_expr = add_stmt_to_compound (instrument_expr,
6544 : instrument_expr1);
6545 : else
6546 999 : instrument_expr = instrument_expr1;
6547 : }
6548 :
6549 54523266 : result = build2_loc (location, resultcode, build_type, op0, op1);
6550 54523266 : if (final_type != 0)
6551 2274835 : result = cp_convert (final_type, result, complain);
6552 :
6553 54523266 : if (instrument_expr != NULL)
6554 14742 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6555 : instrument_expr, result);
6556 :
6557 54523266 : if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6558 14495 : result = get_target_expr (result, complain);
6559 :
6560 54523266 : if (semantic_result_type)
6561 3062 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
6562 :
6563 54523266 : if (!c_inhibit_evaluation_warnings)
6564 : {
6565 50257035 : if (!processing_template_decl)
6566 : {
6567 50257035 : op0 = cp_fully_fold (op0);
6568 : /* Only consider the second argument if the first isn't overflowed. */
6569 50257035 : if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6570 : return result;
6571 13378552 : op1 = cp_fully_fold (op1);
6572 13378552 : if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6573 : return result;
6574 : }
6575 0 : else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6576 0 : || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6577 : return result;
6578 :
6579 9675673 : tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6580 9675673 : if (TREE_OVERFLOW_P (result_ovl))
6581 228 : overflow_warning (location, result_ovl);
6582 : }
6583 :
6584 : return result;
6585 : }
6586 :
6587 : /* Build a VEC_PERM_EXPR.
6588 : This is a simple wrapper for c_build_vec_perm_expr. */
6589 : tree
6590 15069 : build_x_vec_perm_expr (location_t loc,
6591 : tree arg0, tree arg1, tree arg2,
6592 : tsubst_flags_t complain)
6593 : {
6594 15069 : tree orig_arg0 = arg0;
6595 15069 : tree orig_arg1 = arg1;
6596 15069 : tree orig_arg2 = arg2;
6597 15069 : if (processing_template_decl)
6598 : {
6599 14 : if (type_dependent_expression_p (arg0)
6600 8 : || type_dependent_expression_p (arg1)
6601 22 : || type_dependent_expression_p (arg2))
6602 6 : return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6603 8 : arg0 = build_non_dependent_expr (arg0);
6604 8 : if (arg1)
6605 0 : arg1 = build_non_dependent_expr (arg1);
6606 8 : arg2 = build_non_dependent_expr (arg2);
6607 : }
6608 15063 : tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6609 15063 : if (processing_template_decl && exp != error_mark_node)
6610 8 : return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6611 8 : orig_arg1, orig_arg2);
6612 : return exp;
6613 : }
6614 :
6615 : /* Build a VEC_PERM_EXPR.
6616 : This is a simple wrapper for c_build_shufflevector. */
6617 : tree
6618 35116 : build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6619 : tsubst_flags_t complain)
6620 : {
6621 35116 : tree arg0 = (*args)[0];
6622 35116 : tree arg1 = (*args)[1];
6623 35116 : if (processing_template_decl)
6624 : {
6625 39 : for (unsigned i = 0; i < args->length (); ++i)
6626 35 : if (i <= 1
6627 35 : ? type_dependent_expression_p ((*args)[i])
6628 10 : : instantiation_dependent_expression_p ((*args)[i]))
6629 : {
6630 15 : tree exp = build_min_nt_call_vec (NULL, args);
6631 15 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6632 15 : return exp;
6633 : }
6634 4 : arg0 = build_non_dependent_expr (arg0);
6635 4 : arg1 = build_non_dependent_expr (arg1);
6636 : /* ??? Nothing needed for the index arguments? */
6637 : }
6638 35101 : auto_vec<tree, 16> mask;
6639 475073 : for (unsigned i = 2; i < args->length (); ++i)
6640 : {
6641 439972 : tree idx = fold_non_dependent_expr ((*args)[i], complain);
6642 439972 : mask.safe_push (idx);
6643 : }
6644 35101 : tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6645 35101 : if (processing_template_decl && exp != error_mark_node)
6646 : {
6647 4 : exp = build_min_non_dep_call_vec (exp, NULL, args);
6648 4 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6649 : }
6650 35101 : return exp;
6651 35116 : }
6652 :
6653 : /* Return a tree for the sum or difference (RESULTCODE says which)
6654 : of pointer PTROP and integer INTOP. */
6655 :
6656 : static tree
6657 2182046 : cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6658 : tree intop, tsubst_flags_t complain)
6659 : {
6660 2182046 : tree res_type = TREE_TYPE (ptrop);
6661 :
6662 : /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6663 : in certain circumstance (when it's valid to do so). So we need
6664 : to make sure it's complete. We don't need to check here, if we
6665 : can actually complete it at all, as those checks will be done in
6666 : pointer_int_sum() anyway. */
6667 2182046 : complete_type (TREE_TYPE (res_type));
6668 :
6669 2182046 : return pointer_int_sum (loc, resultcode, ptrop,
6670 2182046 : intop, complain & tf_warning_or_error);
6671 : }
6672 :
6673 : /* Return a tree for the difference of pointers OP0 and OP1.
6674 : The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6675 : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6676 :
6677 : static tree
6678 707829 : pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6679 : tsubst_flags_t complain, tree *instrument_expr)
6680 : {
6681 707829 : tree result, inttype;
6682 707829 : tree restype = ptrdiff_type_node;
6683 707829 : tree target_type = TREE_TYPE (ptrtype);
6684 :
6685 707829 : if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6686 7 : return error_mark_node;
6687 :
6688 707822 : if (VOID_TYPE_P (target_type))
6689 : {
6690 0 : if (complain & tf_error)
6691 0 : permerror (loc, "ISO C++ forbids using pointer of "
6692 : "type %<void *%> in subtraction");
6693 : else
6694 0 : return error_mark_node;
6695 : }
6696 707822 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
6697 : {
6698 10 : if (complain & tf_error)
6699 4 : permerror (loc, "ISO C++ forbids using pointer to "
6700 : "a function in subtraction");
6701 : else
6702 6 : return error_mark_node;
6703 : }
6704 707816 : if (TREE_CODE (target_type) == METHOD_TYPE)
6705 : {
6706 0 : if (complain & tf_error)
6707 0 : permerror (loc, "ISO C++ forbids using pointer to "
6708 : "a method in subtraction");
6709 : else
6710 0 : return error_mark_node;
6711 : }
6712 1415632 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6713 707816 : TREE_TYPE (TREE_TYPE (op0)),
6714 707816 : !(complain & tf_error))
6715 1415632 : || !verify_type_context (loc, TCTX_POINTER_ARITH,
6716 707816 : TREE_TYPE (TREE_TYPE (op1)),
6717 : !(complain & tf_error)))
6718 0 : return error_mark_node;
6719 :
6720 : /* Determine integer type result of the subtraction. This will usually
6721 : be the same as the result type (ptrdiff_t), but may need to be a wider
6722 : type if pointers for the address space are wider than ptrdiff_t. */
6723 707816 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6724 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6725 : else
6726 : inttype = restype;
6727 :
6728 707816 : if (!processing_template_decl
6729 707816 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
6730 : {
6731 84 : op0 = save_expr (op0);
6732 84 : op1 = save_expr (op1);
6733 :
6734 84 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
6735 84 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6736 : }
6737 :
6738 : /* First do the subtraction, then build the divide operator
6739 : and only convert at the very end.
6740 : Do not do default conversions in case restype is a short type. */
6741 :
6742 : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6743 : pointers. If some platform cannot provide that, or has a larger
6744 : ptrdiff_type to support differences larger than half the address
6745 : space, cast the pointers to some larger integer type and do the
6746 : computations in that type. */
6747 707816 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6748 0 : op0 = cp_build_binary_op (loc,
6749 : MINUS_EXPR,
6750 : cp_convert (inttype, op0, complain),
6751 : cp_convert (inttype, op1, complain),
6752 : complain);
6753 : else
6754 707816 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
6755 :
6756 : /* This generates an error if op1 is a pointer to an incomplete type. */
6757 707816 : if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
6758 : {
6759 0 : if (complain & tf_error)
6760 0 : error_at (loc, "invalid use of a pointer to an incomplete type in "
6761 : "pointer arithmetic");
6762 : else
6763 0 : return error_mark_node;
6764 : }
6765 :
6766 707816 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
6767 : {
6768 20 : if (complain & tf_error)
6769 20 : error_at (loc, "arithmetic on pointer to an empty aggregate");
6770 : else
6771 0 : return error_mark_node;
6772 : }
6773 :
6774 707816 : op1 = (TYPE_PTROB_P (ptrtype)
6775 1415632 : ? size_in_bytes_loc (loc, target_type)
6776 : : integer_one_node);
6777 :
6778 : /* Do the division. */
6779 :
6780 707816 : result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
6781 : cp_convert (inttype, op1, complain));
6782 707816 : return cp_convert (restype, result, complain);
6783 : }
6784 :
6785 : /* Construct and perhaps optimize a tree representation
6786 : for a unary operation. CODE, a tree_code, specifies the operation
6787 : and XARG is the operand. */
6788 :
6789 : tree
6790 39748543 : build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
6791 : tree lookups, tsubst_flags_t complain)
6792 : {
6793 39748543 : tree orig_expr = xarg;
6794 39748543 : tree exp;
6795 39748543 : int ptrmem = 0;
6796 39748543 : tree overload = NULL_TREE;
6797 :
6798 39748543 : if (processing_template_decl)
6799 : {
6800 26799011 : if (type_dependent_expression_p (xarg))
6801 : {
6802 18590366 : tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
6803 18590366 : TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
6804 18590366 : return e;
6805 : }
6806 :
6807 8208645 : xarg = build_non_dependent_expr (xarg);
6808 : }
6809 :
6810 21158177 : exp = NULL_TREE;
6811 :
6812 : /* [expr.unary.op] says:
6813 :
6814 : The address of an object of incomplete type can be taken.
6815 :
6816 : (And is just the ordinary address operator, not an overloaded
6817 : "operator &".) However, if the type is a template
6818 : specialization, we must complete the type at this point so that
6819 : an overloaded "operator &" will be available if required. */
6820 21158177 : if (code == ADDR_EXPR
6821 2537068 : && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
6822 23694937 : && ((CLASS_TYPE_P (TREE_TYPE (xarg))
6823 1273231 : && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
6824 2536608 : || (TREE_CODE (xarg) == OFFSET_REF)))
6825 : /* Don't look for a function. */;
6826 : else
6827 21110385 : exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
6828 : NULL_TREE, lookups, &overload, complain);
6829 :
6830 21158177 : if (!exp && code == ADDR_EXPR)
6831 : {
6832 2536895 : if (is_overloaded_fn (xarg))
6833 : {
6834 351991 : tree fn = get_first_fn (xarg);
6835 703982 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
6836 : {
6837 16 : if (complain & tf_error)
6838 28 : error_at (loc, DECL_CONSTRUCTOR_P (fn)
6839 : ? G_("taking address of constructor %qD")
6840 : : G_("taking address of destructor %qD"),
6841 : fn);
6842 16 : return error_mark_node;
6843 : }
6844 : }
6845 :
6846 : /* A pointer to member-function can be formed only by saying
6847 : &X::mf. */
6848 2536871 : if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
6849 2581896 : && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
6850 : {
6851 12 : if (TREE_CODE (xarg) != OFFSET_REF
6852 12 : || !TYPE_P (TREE_OPERAND (xarg, 0)))
6853 : {
6854 12 : if (complain & tf_error)
6855 : {
6856 12 : error_at (loc, "invalid use of %qE to form a "
6857 : "pointer-to-member-function", xarg.get_value ());
6858 12 : if (TREE_CODE (xarg) != OFFSET_REF)
6859 8 : inform (loc, " a qualified-id is required");
6860 : }
6861 12 : return error_mark_node;
6862 : }
6863 : else
6864 : {
6865 0 : if (complain & tf_error)
6866 0 : error_at (loc, "parentheses around %qE cannot be used to "
6867 : "form a pointer-to-member-function",
6868 : xarg.get_value ());
6869 : else
6870 0 : return error_mark_node;
6871 0 : PTRMEM_OK_P (xarg) = 1;
6872 : }
6873 : }
6874 :
6875 2536867 : if (TREE_CODE (xarg) == OFFSET_REF)
6876 : {
6877 47620 : ptrmem = PTRMEM_OK_P (xarg);
6878 :
6879 0 : if (!ptrmem && !flag_ms_extensions
6880 47620 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
6881 : {
6882 : /* A single non-static member, make sure we don't allow a
6883 : pointer-to-member. */
6884 0 : xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
6885 0 : TREE_OPERAND (xarg, 0),
6886 0 : ovl_make (TREE_OPERAND (xarg, 1)));
6887 0 : PTRMEM_OK_P (xarg) = ptrmem;
6888 : }
6889 : }
6890 :
6891 5073734 : exp = cp_build_addr_expr_strict (xarg, complain);
6892 :
6893 2536867 : if (TREE_CODE (exp) == PTRMEM_CST)
6894 46873 : PTRMEM_CST_LOCATION (exp) = loc;
6895 : else
6896 2489994 : protected_set_expr_location (exp, loc);
6897 : }
6898 :
6899 21158149 : if (processing_template_decl && exp != error_mark_node)
6900 : {
6901 8208503 : if (overload != NULL_TREE)
6902 105044 : return (build_min_non_dep_op_overload
6903 105044 : (code, exp, overload, orig_expr, integer_zero_node));
6904 :
6905 8103459 : exp = build_min_non_dep (code, exp, orig_expr,
6906 : /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
6907 : }
6908 21053105 : if (TREE_CODE (exp) == ADDR_EXPR)
6909 1937017 : PTRMEM_OK_P (exp) = ptrmem;
6910 : return exp;
6911 : }
6912 :
6913 : /* Construct and perhaps optimize a tree representation
6914 : for __builtin_addressof operation. ARG specifies the operand. */
6915 :
6916 : tree
6917 202123 : cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
6918 : {
6919 202123 : tree orig_expr = arg;
6920 :
6921 202123 : if (processing_template_decl)
6922 : {
6923 47638 : if (type_dependent_expression_p (arg))
6924 47638 : return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
6925 :
6926 0 : arg = build_non_dependent_expr (arg);
6927 : }
6928 :
6929 308970 : tree exp = cp_build_addr_expr_strict (arg, complain);
6930 :
6931 154485 : if (processing_template_decl && exp != error_mark_node)
6932 0 : exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
6933 : return exp;
6934 : }
6935 :
6936 : /* Like c_common_truthvalue_conversion, but handle pointer-to-member
6937 : constants, where a null value is represented by an INTEGER_CST of
6938 : -1. */
6939 :
6940 : tree
6941 4152120 : cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
6942 : {
6943 4152120 : tree type = TREE_TYPE (expr);
6944 4152120 : location_t loc = cp_expr_loc_or_input_loc (expr);
6945 3425824 : if (TYPE_PTR_OR_PTRMEM_P (type)
6946 : /* Avoid ICE on invalid use of non-static member function. */
6947 7577626 : || TREE_CODE (expr) == FUNCTION_DECL)
6948 726614 : return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
6949 : else
6950 3425506 : return c_common_truthvalue_conversion (loc, expr);
6951 : }
6952 :
6953 : /* Returns EXPR contextually converted to bool. */
6954 :
6955 : tree
6956 25080349 : contextual_conv_bool (tree expr, tsubst_flags_t complain)
6957 : {
6958 2894812 : return perform_implicit_conversion_flags (boolean_type_node, expr,
6959 2894812 : complain, LOOKUP_NORMAL);
6960 : }
6961 :
6962 : /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
6963 : is a low-level function; most callers should use maybe_convert_cond. */
6964 :
6965 : tree
6966 22185537 : condition_conversion (tree expr)
6967 : {
6968 22185537 : tree t = contextual_conv_bool (expr, tf_warning_or_error);
6969 22185537 : if (!processing_template_decl)
6970 10621185 : t = fold_build_cleanup_point_expr (boolean_type_node, t);
6971 22185537 : return t;
6972 : }
6973 :
6974 : /* Returns the address of T. This function will fold away
6975 : ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
6976 : most places should use cp_build_addr_expr instead. */
6977 :
6978 : tree
6979 138425902 : build_address (tree t)
6980 : {
6981 138425902 : if (error_operand_p (t) || !cxx_mark_addressable (t))
6982 28 : return error_mark_node;
6983 138425874 : gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
6984 : || processing_template_decl);
6985 138425874 : t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
6986 138425874 : if (TREE_CODE (t) != ADDR_EXPR)
6987 24915992 : t = rvalue (t);
6988 : return t;
6989 : }
6990 :
6991 : /* Return a NOP_EXPR converting EXPR to TYPE. */
6992 :
6993 : tree
6994 262130222 : build_nop (tree type, tree expr)
6995 : {
6996 262130222 : if (type == error_mark_node || error_operand_p (expr))
6997 : return expr;
6998 262130135 : return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
6999 : }
7000 :
7001 : /* Take the address of ARG, whatever that means under C++ semantics.
7002 : If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7003 : and class rvalues as well.
7004 :
7005 : Nothing should call this function directly; instead, callers should use
7006 : cp_build_addr_expr or cp_build_addr_expr_strict. */
7007 :
7008 : static tree
7009 100900042 : cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7010 : {
7011 100900042 : tree argtype;
7012 100900042 : tree val;
7013 :
7014 100900042 : if (!arg || error_operand_p (arg))
7015 13 : return error_mark_node;
7016 :
7017 100900029 : arg = mark_lvalue_use (arg);
7018 100900029 : if (error_operand_p (arg))
7019 3 : return error_mark_node;
7020 :
7021 100900026 : argtype = lvalue_type (arg);
7022 100900026 : location_t loc = cp_expr_loc_or_input_loc (arg);
7023 :
7024 100900026 : gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7025 :
7026 7080010 : if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7027 100900194 : && !really_overloaded_fn (arg))
7028 : {
7029 : /* They're trying to take the address of a unique non-static
7030 : member function. This is ill-formed (except in MS-land),
7031 : but let's try to DTRT.
7032 : Note: We only handle unique functions here because we don't
7033 : want to complain if there's a static overload; non-unique
7034 : cases will be handled by instantiate_type. But we need to
7035 : handle this case here to allow casts on the resulting PMF.
7036 : We could defer this in non-MS mode, but it's easier to give
7037 : a useful error here. */
7038 :
7039 : /* Inside constant member functions, the `this' pointer
7040 : contains an extra const qualifier. TYPE_MAIN_VARIANT
7041 : is used here to remove this const from the diagnostics
7042 : and the created OFFSET_REF. */
7043 48 : tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7044 48 : tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7045 48 : if (!mark_used (fn, complain) && !(complain & tf_error))
7046 0 : return error_mark_node;
7047 :
7048 48 : if (! flag_ms_extensions)
7049 : {
7050 40 : tree name = DECL_NAME (fn);
7051 40 : if (!(complain & tf_error))
7052 0 : return error_mark_node;
7053 40 : else if (current_class_type
7054 40 : && TREE_OPERAND (arg, 0) == current_class_ref)
7055 : /* An expression like &memfn. */
7056 28 : permerror (loc,
7057 : "ISO C++ forbids taking the address of an unqualified"
7058 : " or parenthesized non-static member function to form"
7059 : " a pointer to member function. Say %<&%T::%D%>",
7060 : base, name);
7061 : else
7062 12 : permerror (loc,
7063 : "ISO C++ forbids taking the address of a bound member"
7064 : " function to form a pointer to member function."
7065 : " Say %<&%T::%D%>",
7066 : base, name);
7067 : }
7068 48 : arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7069 : }
7070 :
7071 : /* Uninstantiated types are all functions. Taking the
7072 : address of a function is a no-op, so just return the
7073 : argument. */
7074 100900026 : if (type_unknown_p (arg))
7075 1557 : return build1 (ADDR_EXPR, unknown_type_node, arg);
7076 :
7077 100898469 : if (TREE_CODE (arg) == OFFSET_REF)
7078 : /* We want a pointer to member; bypass all the code for actually taking
7079 : the address of something. */
7080 46933 : goto offset_ref;
7081 :
7082 : /* Anything not already handled and not a true memory reference
7083 : is an error. */
7084 100851536 : if (!FUNC_OR_METHOD_TYPE_P (argtype))
7085 : {
7086 49467657 : cp_lvalue_kind kind = lvalue_kind (arg);
7087 49467657 : if (kind == clk_none)
7088 : {
7089 28 : if (complain & tf_error)
7090 28 : lvalue_error (loc, lv_addressof);
7091 28 : return error_mark_node;
7092 : }
7093 49467629 : if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7094 : {
7095 37 : if (!(complain & tf_error))
7096 3 : return error_mark_node;
7097 : /* Make this a permerror because we used to accept it. */
7098 34 : permerror (loc, "taking address of rvalue");
7099 : }
7100 : }
7101 :
7102 100851505 : if (TYPE_REF_P (argtype))
7103 : {
7104 243 : tree type = build_pointer_type (TREE_TYPE (argtype));
7105 243 : arg = build1 (CONVERT_EXPR, type, arg);
7106 243 : return arg;
7107 : }
7108 100851262 : else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7109 : {
7110 : /* ARM $3.4 */
7111 : /* Apparently a lot of autoconf scripts for C++ packages do this,
7112 : so only complain if -Wpedantic. */
7113 12 : if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7114 12 : pedwarn (loc, OPT_Wpedantic,
7115 : "ISO C++ forbids taking address of function %<::main%>");
7116 0 : else if (flag_pedantic_errors)
7117 0 : return error_mark_node;
7118 : }
7119 :
7120 : /* Let &* cancel out to simplify resulting code. */
7121 100851262 : if (INDIRECT_REF_P (arg))
7122 : {
7123 29612252 : arg = TREE_OPERAND (arg, 0);
7124 29612252 : if (TYPE_REF_P (TREE_TYPE (arg)))
7125 : {
7126 14078998 : tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7127 14078998 : arg = build1 (CONVERT_EXPR, type, arg);
7128 : }
7129 : else
7130 : /* Don't let this be an lvalue. */
7131 15533254 : arg = rvalue (arg);
7132 29612252 : return arg;
7133 : }
7134 :
7135 : /* Handle complex lvalues (when permitted)
7136 : by reduction to simpler cases. */
7137 71239010 : val = unary_complex_lvalue (ADDR_EXPR, arg);
7138 71239010 : if (val != 0)
7139 : return val;
7140 :
7141 70950828 : switch (TREE_CODE (arg))
7142 : {
7143 0 : CASE_CONVERT:
7144 0 : case FLOAT_EXPR:
7145 0 : case FIX_TRUNC_EXPR:
7146 : /* We should have handled this above in the lvalue_kind check. */
7147 0 : gcc_unreachable ();
7148 36247 : break;
7149 :
7150 36247 : case BASELINK:
7151 36247 : arg = BASELINK_FUNCTIONS (arg);
7152 : /* Fall through. */
7153 :
7154 36247 : case OVERLOAD:
7155 36247 : arg = OVL_FIRST (arg);
7156 : break;
7157 :
7158 46933 : case OFFSET_REF:
7159 46933 : offset_ref:
7160 : /* Turn a reference to a non-static data member into a
7161 : pointer-to-member. */
7162 46933 : {
7163 46933 : tree type;
7164 46933 : tree t;
7165 :
7166 46933 : gcc_assert (PTRMEM_OK_P (arg));
7167 :
7168 46933 : t = TREE_OPERAND (arg, 1);
7169 46933 : if (TYPE_REF_P (TREE_TYPE (t)))
7170 : {
7171 18 : if (complain & tf_error)
7172 18 : error_at (loc,
7173 : "cannot create pointer to reference member %qD", t);
7174 18 : return error_mark_node;
7175 : }
7176 :
7177 : /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7178 46915 : if (TREE_CODE (t) == FUNCTION_DECL
7179 45092 : && !DECL_PURE_VIRTUAL_P (t)
7180 91981 : && !mark_used (t, complain) && !(complain & tf_error))
7181 3 : return error_mark_node;
7182 :
7183 46912 : type = build_ptrmem_type (context_for_name_lookup (t),
7184 46912 : TREE_TYPE (t));
7185 46912 : t = make_ptrmem_cst (type, t);
7186 46912 : return t;
7187 : }
7188 :
7189 : default:
7190 : break;
7191 : }
7192 :
7193 70950828 : if (argtype != error_mark_node)
7194 70950828 : argtype = build_pointer_type (argtype);
7195 :
7196 70950828 : if (bitfield_p (arg))
7197 : {
7198 25 : if (complain & tf_error)
7199 25 : error_at (loc, "attempt to take address of bit-field");
7200 25 : return error_mark_node;
7201 : }
7202 :
7203 : /* In a template, we are processing a non-dependent expression
7204 : so we can just form an ADDR_EXPR with the correct type. */
7205 70950803 : if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7206 : {
7207 63870984 : if (!mark_single_function (arg, complain))
7208 3 : return error_mark_node;
7209 63870981 : val = build_address (arg);
7210 63870981 : if (TREE_CODE (arg) == OFFSET_REF)
7211 0 : PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7212 : }
7213 7079819 : else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7214 : {
7215 32 : tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7216 :
7217 : /* We can only get here with a single static member
7218 : function. */
7219 32 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7220 : && DECL_STATIC_FUNCTION_P (fn));
7221 32 : if (!mark_used (fn, complain) && !(complain & tf_error))
7222 0 : return error_mark_node;
7223 32 : val = build_address (fn);
7224 32 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7225 : /* Do not lose object's side effects. */
7226 16 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7227 16 : TREE_OPERAND (arg, 0), val);
7228 : }
7229 : else
7230 : {
7231 7079787 : tree object = TREE_OPERAND (arg, 0);
7232 7079787 : tree field = TREE_OPERAND (arg, 1);
7233 7079787 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7234 : (TREE_TYPE (object), decl_type_context (field)));
7235 7079787 : val = build_address (arg);
7236 : }
7237 :
7238 70950800 : if (TYPE_PTR_P (argtype)
7239 70950800 : && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7240 : {
7241 1140 : build_ptrmemfunc_type (argtype);
7242 1140 : val = build_ptrmemfunc (argtype, val, 0,
7243 : /*c_cast_p=*/false,
7244 : complain);
7245 : }
7246 :
7247 : /* For addresses of immediate functions ensure we have EXPR_LOCATION
7248 : set for possible later diagnostics. */
7249 70950800 : if (TREE_CODE (val) == ADDR_EXPR
7250 70949622 : && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL
7251 173255212 : && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (val, 0)))
7252 40 : SET_EXPR_LOCATION (val, input_location);
7253 :
7254 : return val;
7255 : }
7256 :
7257 : /* Take the address of ARG if it has one, even if it's an rvalue. */
7258 :
7259 : tree
7260 98208690 : cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7261 : {
7262 97881187 : return cp_build_addr_expr_1 (arg, 0, complain);
7263 : }
7264 :
7265 : /* Take the address of ARG, but only if it's an lvalue. */
7266 :
7267 : static tree
7268 2691352 : cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7269 : {
7270 2691352 : return cp_build_addr_expr_1 (arg, 1, complain);
7271 : }
7272 :
7273 : /* C++: Must handle pointers to members.
7274 :
7275 : Perhaps type instantiation should be extended to handle conversion
7276 : from aggregates to types we don't yet know we want? (Or are those
7277 : cases typically errors which should be reported?)
7278 :
7279 : NOCONVERT suppresses the default promotions (such as from short to int). */
7280 :
7281 : tree
7282 18979684 : cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7283 : tsubst_flags_t complain)
7284 : {
7285 : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7286 18979684 : tree arg = xarg;
7287 18979684 : location_t location = cp_expr_loc_or_input_loc (arg);
7288 18979684 : tree argtype = 0;
7289 18979684 : tree eptype = NULL_TREE;
7290 18979684 : const char *errstring = NULL;
7291 18979684 : tree val;
7292 18979684 : const char *invalid_op_diag;
7293 :
7294 18979684 : if (!arg || error_operand_p (arg))
7295 0 : return error_mark_node;
7296 :
7297 18979684 : arg = resolve_nondeduced_context (arg, complain);
7298 :
7299 37959368 : if ((invalid_op_diag
7300 18979684 : = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7301 : ? CONVERT_EXPR
7302 : : code),
7303 18979684 : TREE_TYPE (arg))))
7304 : {
7305 0 : if (complain & tf_error)
7306 0 : error (invalid_op_diag);
7307 0 : return error_mark_node;
7308 : }
7309 :
7310 18979684 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7311 : {
7312 206 : eptype = TREE_TYPE (arg);
7313 206 : arg = TREE_OPERAND (arg, 0);
7314 : }
7315 :
7316 18979684 : switch (code)
7317 : {
7318 1723155 : case UNARY_PLUS_EXPR:
7319 1723155 : case NEGATE_EXPR:
7320 1723155 : {
7321 1723155 : int flags = WANT_ARITH | WANT_ENUM;
7322 : /* Unary plus (but not unary minus) is allowed on pointers. */
7323 1723155 : if (code == UNARY_PLUS_EXPR)
7324 146004 : flags |= WANT_POINTER;
7325 1723155 : arg = build_expr_type_conversion (flags, arg, true);
7326 1723155 : if (!arg)
7327 27 : errstring = (code == NEGATE_EXPR
7328 22 : ? _("wrong type argument to unary minus")
7329 5 : : _("wrong type argument to unary plus"));
7330 : else
7331 : {
7332 1723128 : if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7333 441559 : arg = cp_perform_integral_promotions (arg, complain);
7334 :
7335 : /* Make sure the result is not an lvalue: a unary plus or minus
7336 : expression is always a rvalue. */
7337 1723128 : arg = rvalue (arg);
7338 : }
7339 : }
7340 : break;
7341 :
7342 607918 : case BIT_NOT_EXPR:
7343 607918 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7344 : {
7345 20 : code = CONJ_EXPR;
7346 20 : if (!noconvert)
7347 : {
7348 20 : arg = cp_default_conversion (arg, complain);
7349 20 : if (arg == error_mark_node)
7350 : return error_mark_node;
7351 : }
7352 : }
7353 607898 : else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7354 : | WANT_VECTOR_OR_COMPLEX,
7355 : arg, true)))
7356 16 : errstring = _("wrong type argument to bit-complement");
7357 607882 : else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7358 : {
7359 : /* Warn if the expression has boolean value. */
7360 607682 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7361 86 : && (complain & tf_warning)
7362 607761 : && warning_at (location, OPT_Wbool_operation,
7363 : "%<~%> on an expression of type %<bool%>"))
7364 54 : inform (location, "did you mean to use logical not (%<!%>)?");
7365 607682 : arg = cp_perform_integral_promotions (arg, complain);
7366 : }
7367 200 : else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7368 200 : arg = mark_rvalue_use (arg);
7369 : break;
7370 :
7371 0 : case ABS_EXPR:
7372 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7373 0 : errstring = _("wrong type argument to abs");
7374 0 : else if (!noconvert)
7375 : {
7376 0 : arg = cp_default_conversion (arg, complain);
7377 0 : if (arg == error_mark_node)
7378 : return error_mark_node;
7379 : }
7380 : break;
7381 :
7382 0 : case CONJ_EXPR:
7383 : /* Conjugating a real value is a no-op, but allow it anyway. */
7384 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7385 0 : errstring = _("wrong type argument to conjugation");
7386 0 : else if (!noconvert)
7387 : {
7388 0 : arg = cp_default_conversion (arg, complain);
7389 0 : if (arg == error_mark_node)
7390 : return error_mark_node;
7391 : }
7392 : break;
7393 :
7394 10779124 : case TRUTH_NOT_EXPR:
7395 10779124 : if (gnu_vector_type_p (TREE_TYPE (arg)))
7396 65 : return cp_build_binary_op (input_location, EQ_EXPR, arg,
7397 65 : build_zero_cst (TREE_TYPE (arg)), complain);
7398 10779059 : arg = perform_implicit_conversion (boolean_type_node, arg,
7399 : complain);
7400 10779059 : if (arg != error_mark_node)
7401 : {
7402 10779047 : val = invert_truthvalue_loc (location, arg);
7403 10779047 : if (obvalue_p (val))
7404 16873 : val = non_lvalue_loc (location, val);
7405 10779047 : return val;
7406 : }
7407 12 : errstring = _("in argument to unary !");
7408 12 : break;
7409 :
7410 : case NOP_EXPR:
7411 : break;
7412 :
7413 358851 : case REALPART_EXPR:
7414 358851 : case IMAGPART_EXPR:
7415 358851 : val = build_real_imag_expr (input_location, code, arg);
7416 358851 : if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
7417 0 : val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
7418 0 : TREE_TYPE (eptype), val);
7419 : return val;
7420 :
7421 5194533 : case PREINCREMENT_EXPR:
7422 5194533 : case POSTINCREMENT_EXPR:
7423 5194533 : case PREDECREMENT_EXPR:
7424 5194533 : case POSTDECREMENT_EXPR:
7425 : /* Handle complex lvalues (when permitted)
7426 : by reduction to simpler cases. */
7427 :
7428 5194533 : val = unary_complex_lvalue (code, arg);
7429 5194533 : if (val != 0)
7430 34 : goto return_build_unary_op;
7431 :
7432 5194499 : arg = mark_lvalue_use (arg);
7433 :
7434 : /* Increment or decrement the real part of the value,
7435 : and don't change the imaginary part. */
7436 5194499 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7437 : {
7438 24 : tree real, imag;
7439 :
7440 24 : arg = cp_stabilize_reference (arg);
7441 24 : real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7442 24 : imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7443 24 : real = cp_build_unary_op (code, real, true, complain);
7444 24 : if (real == error_mark_node || imag == error_mark_node)
7445 : return error_mark_node;
7446 20 : val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
7447 20 : goto return_build_unary_op;
7448 : }
7449 :
7450 : /* Report invalid types. */
7451 :
7452 5194475 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7453 : arg, true)))
7454 : {
7455 48 : if (code == PREINCREMENT_EXPR)
7456 12 : errstring = _("no pre-increment operator for type");
7457 36 : else if (code == POSTINCREMENT_EXPR)
7458 12 : errstring = _("no post-increment operator for type");
7459 24 : else if (code == PREDECREMENT_EXPR)
7460 12 : errstring = _("no pre-decrement operator for type");
7461 : else
7462 12 : errstring = _("no post-decrement operator for type");
7463 : break;
7464 : }
7465 5194427 : else if (arg == error_mark_node)
7466 : return error_mark_node;
7467 :
7468 : /* Report something read-only. */
7469 :
7470 5194427 : if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7471 5194427 : || TREE_READONLY (arg))
7472 : {
7473 41 : if (complain & tf_error)
7474 41 : cxx_readonly_error (location, arg,
7475 41 : ((code == PREINCREMENT_EXPR
7476 41 : || code == POSTINCREMENT_EXPR)
7477 : ? lv_increment : lv_decrement));
7478 : else
7479 : return error_mark_node;
7480 : }
7481 :
7482 5194427 : {
7483 5194427 : tree inc;
7484 5194427 : tree declared_type = unlowered_expr_type (arg);
7485 :
7486 5194427 : argtype = TREE_TYPE (arg);
7487 :
7488 : /* ARM $5.2.5 last annotation says this should be forbidden. */
7489 5194427 : if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7490 : {
7491 0 : if (complain & tf_error)
7492 0 : permerror (location, (code == PREINCREMENT_EXPR
7493 0 : || code == POSTINCREMENT_EXPR)
7494 : ? G_("ISO C++ forbids incrementing an enum")
7495 : : G_("ISO C++ forbids decrementing an enum"));
7496 : else
7497 0 : return error_mark_node;
7498 : }
7499 :
7500 : /* Compute the increment. */
7501 :
7502 5194427 : if (TYPE_PTR_P (argtype))
7503 : {
7504 1143630 : tree type = complete_type (TREE_TYPE (argtype));
7505 :
7506 1143630 : if (!COMPLETE_OR_VOID_TYPE_P (type))
7507 : {
7508 13 : if (complain & tf_error)
7509 12 : error_at (location, ((code == PREINCREMENT_EXPR
7510 12 : || code == POSTINCREMENT_EXPR))
7511 : ? G_("cannot increment a pointer to incomplete "
7512 : "type %qT")
7513 : : G_("cannot decrement a pointer to incomplete "
7514 : "type %qT"),
7515 12 : TREE_TYPE (argtype));
7516 : else
7517 1 : return error_mark_node;
7518 : }
7519 1143617 : else if (!TYPE_PTROB_P (argtype))
7520 : {
7521 90 : if (complain & tf_error)
7522 84 : pedwarn (location, OPT_Wpointer_arith,
7523 84 : (code == PREINCREMENT_EXPR
7524 84 : || code == POSTINCREMENT_EXPR)
7525 : ? G_("ISO C++ forbids incrementing a pointer "
7526 : "of type %qT")
7527 : : G_("ISO C++ forbids decrementing a pointer "
7528 : "of type %qT"),
7529 : argtype);
7530 : else
7531 6 : return error_mark_node;
7532 : }
7533 1143527 : else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7534 1143527 : TREE_TYPE (argtype),
7535 1143527 : !(complain & tf_error)))
7536 0 : return error_mark_node;
7537 :
7538 1143623 : inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7539 : }
7540 : else
7541 4050797 : inc = VECTOR_TYPE_P (argtype)
7542 4050797 : ? build_one_cst (argtype)
7543 : : integer_one_node;
7544 :
7545 5194420 : inc = cp_convert (argtype, inc, complain);
7546 :
7547 : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7548 : need to ask Objective-C to build the increment or decrement
7549 : expression for it. */
7550 5194420 : if (objc_is_property_ref (arg))
7551 0 : return objc_build_incr_expr_for_property_ref (input_location, code,
7552 0 : arg, inc);
7553 :
7554 : /* Complain about anything else that is not a true lvalue. */
7555 5194420 : if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7556 5194420 : || code == POSTINCREMENT_EXPR)
7557 : ? lv_increment : lv_decrement),
7558 : complain))
7559 44 : return error_mark_node;
7560 :
7561 : /* [depr.volatile.type] "Postfix ++ and -- expressions and
7562 : prefix ++ and -- expressions of volatile-qualified arithmetic
7563 : and pointer types are deprecated." */
7564 5194021 : if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7565 5194376 : && (complain & tf_warning))
7566 461 : warning_at (location, OPT_Wvolatile,
7567 : "%qs expression of %<volatile%>-qualified type is "
7568 : "deprecated",
7569 : ((code == PREINCREMENT_EXPR
7570 : || code == POSTINCREMENT_EXPR)
7571 : ? "++" : "--"));
7572 :
7573 : /* Forbid using -- or ++ in C++17 on `bool'. */
7574 5194376 : if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7575 : {
7576 118 : if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7577 : {
7578 40 : if (complain & tf_error)
7579 40 : error_at (location,
7580 : "use of an operand of type %qT in %<operator--%> "
7581 : "is forbidden", boolean_type_node);
7582 40 : return error_mark_node;
7583 : }
7584 : else
7585 : {
7586 78 : if (cxx_dialect >= cxx17)
7587 : {
7588 32 : if (complain & tf_error)
7589 30 : error_at (location,
7590 : "use of an operand of type %qT in "
7591 : "%<operator++%> is forbidden in C++17",
7592 : boolean_type_node);
7593 32 : return error_mark_node;
7594 : }
7595 : /* Otherwise, [depr.incr.bool] says this is deprecated. */
7596 46 : else if (complain & tf_warning)
7597 44 : warning_at (location, OPT_Wdeprecated,
7598 : "use of an operand of type %qT "
7599 : "in %<operator++%> is deprecated",
7600 : boolean_type_node);
7601 : }
7602 46 : val = boolean_increment (code, arg);
7603 : }
7604 5194258 : else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7605 : /* An rvalue has no cv-qualifiers. */
7606 761709 : val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7607 : else
7608 4432549 : val = build2 (code, TREE_TYPE (arg), arg, inc);
7609 :
7610 5194304 : TREE_SIDE_EFFECTS (val) = 1;
7611 5194304 : goto return_build_unary_op;
7612 : }
7613 :
7614 316103 : case ADDR_EXPR:
7615 : /* Note that this operation never does default_conversion
7616 : regardless of NOCONVERT. */
7617 316103 : return cp_build_addr_expr (arg, complain);
7618 :
7619 : default:
7620 : break;
7621 : }
7622 :
7623 2331133 : if (!errstring)
7624 : {
7625 2331030 : if (argtype == 0)
7626 2331030 : argtype = TREE_TYPE (arg);
7627 2331030 : val = build1 (code, argtype, arg);
7628 7525388 : return_build_unary_op:
7629 7525388 : if (eptype)
7630 194 : val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
7631 7525388 : return val;
7632 : }
7633 :
7634 103 : if (complain & tf_error)
7635 62 : error_at (location, "%s", errstring);
7636 103 : return error_mark_node;
7637 : }
7638 :
7639 : /* Hook for the c-common bits that build a unary op. */
7640 : tree
7641 5422 : build_unary_op (location_t /*location*/,
7642 : enum tree_code code, tree xarg, bool noconvert)
7643 : {
7644 5422 : return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
7645 : }
7646 :
7647 : /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7648 : so that it is a valid lvalue even for GENERIC by replacing
7649 : (lhs = rhs) with ((lhs = rhs), lhs)
7650 : (--lhs) with ((--lhs), lhs)
7651 : (++lhs) with ((++lhs), lhs)
7652 : and if lhs has side-effects, calling cp_stabilize_reference on it, so
7653 : that it can be evaluated multiple times. */
7654 :
7655 : tree
7656 265042 : genericize_compound_lvalue (tree lvalue)
7657 : {
7658 265042 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7659 128 : lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7660 128 : cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7661 128 : TREE_OPERAND (lvalue, 1));
7662 265042 : return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7663 265042 : lvalue, TREE_OPERAND (lvalue, 0));
7664 : }
7665 :
7666 : /* Apply unary lvalue-demanding operator CODE to the expression ARG
7667 : for certain kinds of expressions which are not really lvalues
7668 : but which we can accept as lvalues.
7669 :
7670 : If ARG is not a kind of expression we can handle, return
7671 : NULL_TREE. */
7672 :
7673 : tree
7674 140922065 : unary_complex_lvalue (enum tree_code code, tree arg)
7675 : {
7676 : /* Inside a template, making these kinds of adjustments is
7677 : pointless; we are only concerned with the type of the
7678 : expression. */
7679 141186905 : if (processing_template_decl)
7680 : return NULL_TREE;
7681 :
7682 : /* Handle (a, b) used as an "lvalue". */
7683 90509429 : if (TREE_CODE (arg) == COMPOUND_EXPR)
7684 : {
7685 265192 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7686 : tf_warning_or_error);
7687 265192 : return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7688 530384 : TREE_OPERAND (arg, 0), real_result);
7689 : }
7690 :
7691 : /* Handle (a ? b : c) used as an "lvalue". */
7692 90244237 : if (TREE_CODE (arg) == COND_EXPR
7693 90244237 : || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7694 23044 : return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7695 :
7696 : /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7697 90221193 : if (TREE_CODE (arg) == MODIFY_EXPR
7698 90221193 : || TREE_CODE (arg) == PREINCREMENT_EXPR
7699 89956429 : || TREE_CODE (arg) == PREDECREMENT_EXPR)
7700 264840 : return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7701 :
7702 89956353 : if (code != ADDR_EXPR)
7703 : return NULL_TREE;
7704 :
7705 : /* Handle (a = b) used as an "lvalue" for `&'. */
7706 87902228 : if (TREE_CODE (arg) == MODIFY_EXPR
7707 87902228 : || TREE_CODE (arg) == INIT_EXPR)
7708 : {
7709 0 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
7710 : tf_warning_or_error);
7711 0 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7712 : arg, real_result);
7713 0 : suppress_warning (arg /* What warning? */);
7714 0 : return arg;
7715 : }
7716 :
7717 136674130 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7718 136673010 : || TREE_CODE (arg) == OFFSET_REF)
7719 : return NULL_TREE;
7720 :
7721 : /* We permit compiler to make function calls returning
7722 : objects of aggregate type look like lvalues. */
7723 48770782 : {
7724 48770782 : tree targ = arg;
7725 :
7726 48770782 : if (TREE_CODE (targ) == SAVE_EXPR)
7727 0 : targ = TREE_OPERAND (targ, 0);
7728 :
7729 48770804 : if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
7730 : {
7731 22 : if (TREE_CODE (arg) == SAVE_EXPR)
7732 : targ = arg;
7733 : else
7734 22 : targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
7735 22 : return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
7736 : }
7737 :
7738 48770760 : if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
7739 0 : return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
7740 0 : TREE_OPERAND (targ, 0), current_function_decl, NULL);
7741 : }
7742 :
7743 : /* Don't let anything else be handled specially. */
7744 : return NULL_TREE;
7745 : }
7746 :
7747 : /* Mark EXP saying that we need to be able to take the
7748 : address of it; it should not be allocated in a register.
7749 : Value is true if successful. ARRAY_REF_P is true if this
7750 : is for ARRAY_REF construction - in that case we don't want
7751 : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7752 : it is fine to use ARRAY_REFs for vector subscripts on vector
7753 : register variables.
7754 :
7755 : C++: we do not allow `current_class_ptr' to be addressable. */
7756 :
7757 : bool
7758 145897095 : cxx_mark_addressable (tree exp, bool array_ref_p)
7759 : {
7760 145897095 : tree x = exp;
7761 :
7762 170172994 : while (1)
7763 170172994 : switch (TREE_CODE (x))
7764 : {
7765 11325264 : case VIEW_CONVERT_EXPR:
7766 11325264 : if (array_ref_p
7767 539260 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7768 11728319 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
7769 : return true;
7770 11322453 : x = TREE_OPERAND (x, 0);
7771 11322453 : break;
7772 :
7773 12762878 : case COMPONENT_REF:
7774 12762878 : if (bitfield_p (x))
7775 12 : error ("attempt to take address of bit-field");
7776 : /* FALLTHRU */
7777 12953446 : case ADDR_EXPR:
7778 12953446 : case ARRAY_REF:
7779 12953446 : case REALPART_EXPR:
7780 12953446 : case IMAGPART_EXPR:
7781 12953446 : x = TREE_OPERAND (x, 0);
7782 12953446 : break;
7783 :
7784 1762958 : case PARM_DECL:
7785 1762958 : if (x == current_class_ptr)
7786 : {
7787 24 : error ("cannot take the address of %<this%>, which is an rvalue expression");
7788 24 : TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
7789 24 : return true;
7790 : }
7791 : /* Fall through. */
7792 :
7793 23843595 : case VAR_DECL:
7794 : /* Caller should not be trying to mark initialized
7795 : constant fields addressable. */
7796 23843595 : gcc_assert (DECL_LANG_SPECIFIC (x) == 0
7797 : || DECL_IN_AGGR_P (x) == 0
7798 : || TREE_STATIC (x)
7799 : || DECL_EXTERNAL (x));
7800 : /* Fall through. */
7801 :
7802 23860847 : case RESULT_DECL:
7803 23860899 : if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
7804 23860891 : && !DECL_ARTIFICIAL (x))
7805 : {
7806 30 : if (VAR_P (x) && DECL_HARD_REGISTER (x))
7807 : {
7808 16 : error
7809 16 : ("address of explicit register variable %qD requested", x);
7810 16 : return false;
7811 : }
7812 14 : else if (extra_warnings)
7813 4 : warning
7814 4 : (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
7815 : }
7816 23860831 : TREE_ADDRESSABLE (x) = 1;
7817 23860831 : return true;
7818 :
7819 72419908 : case CONST_DECL:
7820 72419908 : case FUNCTION_DECL:
7821 72419908 : TREE_ADDRESSABLE (x) = 1;
7822 72419908 : return true;
7823 :
7824 18 : case CONSTRUCTOR:
7825 18 : TREE_ADDRESSABLE (x) = 1;
7826 18 : return true;
7827 :
7828 6124321 : case TARGET_EXPR:
7829 6124321 : TREE_ADDRESSABLE (x) = 1;
7830 6124321 : cxx_mark_addressable (TREE_OPERAND (x, 0));
7831 6124321 : return true;
7832 :
7833 : default:
7834 : return true;
7835 : }
7836 : }
7837 :
7838 : /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
7839 :
7840 : tree
7841 3371581 : build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
7842 : tsubst_flags_t complain)
7843 : {
7844 3371581 : tree orig_ifexp = ifexp;
7845 3371581 : tree orig_op1 = op1;
7846 3371581 : tree orig_op2 = op2;
7847 3371581 : tree expr;
7848 :
7849 3371581 : if (processing_template_decl)
7850 : {
7851 : /* The standard says that the expression is type-dependent if
7852 : IFEXP is type-dependent, even though the eventual type of the
7853 : expression doesn't dependent on IFEXP. */
7854 1639181 : if (type_dependent_expression_p (ifexp)
7855 : /* As a GNU extension, the middle operand may be omitted. */
7856 769287 : || (op1 && type_dependent_expression_p (op1))
7857 2118396 : || type_dependent_expression_p (op2))
7858 1178828 : return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
7859 460353 : ifexp = build_non_dependent_expr (ifexp);
7860 460353 : if (op1)
7861 460276 : op1 = build_non_dependent_expr (op1);
7862 460353 : op2 = build_non_dependent_expr (op2);
7863 : }
7864 :
7865 2192753 : expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
7866 2192753 : if (processing_template_decl && expr != error_mark_node)
7867 : {
7868 460345 : tree min = build_min_non_dep (COND_EXPR, expr,
7869 : orig_ifexp, orig_op1, orig_op2);
7870 460345 : expr = convert_from_reference (min);
7871 : }
7872 : return expr;
7873 : }
7874 :
7875 : /* Given a list of expressions, return a compound expression
7876 : that performs them all and returns the value of the last of them. */
7877 :
7878 : tree
7879 21767606 : build_x_compound_expr_from_list (tree list, expr_list_kind exp,
7880 : tsubst_flags_t complain)
7881 : {
7882 21767606 : tree expr = TREE_VALUE (list);
7883 :
7884 113881 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
7885 21881483 : && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
7886 : {
7887 20 : if (complain & tf_error)
7888 40 : pedwarn (cp_expr_loc_or_input_loc (expr), 0,
7889 : "list-initializer for non-class type must not "
7890 : "be parenthesized");
7891 : else
7892 0 : return error_mark_node;
7893 : }
7894 :
7895 21767606 : if (TREE_CHAIN (list))
7896 : {
7897 27 : if (complain & tf_error)
7898 17 : switch (exp)
7899 : {
7900 14 : case ELK_INIT:
7901 14 : permerror (input_location, "expression list treated as compound "
7902 : "expression in initializer");
7903 14 : break;
7904 3 : case ELK_MEM_INIT:
7905 3 : permerror (input_location, "expression list treated as compound "
7906 : "expression in mem-initializer");
7907 3 : break;
7908 0 : case ELK_FUNC_CAST:
7909 0 : permerror (input_location, "expression list treated as compound "
7910 : "expression in functional cast");
7911 0 : break;
7912 0 : default:
7913 0 : gcc_unreachable ();
7914 : }
7915 : else
7916 10 : return error_mark_node;
7917 :
7918 34 : for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
7919 34 : expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
7920 17 : expr, TREE_VALUE (list), NULL_TREE,
7921 : complain);
7922 : }
7923 :
7924 : return expr;
7925 : }
7926 :
7927 : /* Like build_x_compound_expr_from_list, but using a VEC. */
7928 :
7929 : tree
7930 48906 : build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
7931 : tsubst_flags_t complain)
7932 : {
7933 48906 : if (vec_safe_is_empty (vec))
7934 : return NULL_TREE;
7935 48906 : else if (vec->length () == 1)
7936 48896 : return (*vec)[0];
7937 : else
7938 : {
7939 10 : tree expr;
7940 10 : unsigned int ix;
7941 10 : tree t;
7942 :
7943 10 : if (msg != NULL)
7944 : {
7945 4 : if (complain & tf_error)
7946 0 : permerror (input_location,
7947 : "%s expression list treated as compound expression",
7948 : msg);
7949 : else
7950 4 : return error_mark_node;
7951 : }
7952 :
7953 6 : expr = (*vec)[0];
7954 14 : for (ix = 1; vec->iterate (ix, &t); ++ix)
7955 8 : expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
7956 : t, NULL_TREE, complain);
7957 :
7958 6 : return expr;
7959 : }
7960 : }
7961 :
7962 : /* Handle overloading of the ',' operator when needed. */
7963 :
7964 : tree
7965 1790612 : build_x_compound_expr (location_t loc, tree op1, tree op2,
7966 : tree lookups, tsubst_flags_t complain)
7967 : {
7968 1790612 : tree result;
7969 1790612 : tree orig_op1 = op1;
7970 1790612 : tree orig_op2 = op2;
7971 1790612 : tree overload = NULL_TREE;
7972 :
7973 1790612 : if (processing_template_decl)
7974 : {
7975 1710631 : if (type_dependent_expression_p (op1)
7976 1710631 : || type_dependent_expression_p (op2))
7977 : {
7978 1514330 : result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
7979 1514330 : TREE_TYPE (result)
7980 1514330 : = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
7981 1514330 : return result;
7982 : }
7983 196301 : op1 = build_non_dependent_expr (op1);
7984 196301 : op2 = build_non_dependent_expr (op2);
7985 : }
7986 :
7987 276282 : result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
7988 : NULL_TREE, lookups, &overload, complain);
7989 276282 : if (!result)
7990 269517 : result = cp_build_compound_expr (op1, op2, complain);
7991 :
7992 276282 : if (processing_template_decl && result != error_mark_node)
7993 : {
7994 194478 : if (overload != NULL_TREE)
7995 16 : return (build_min_non_dep_op_overload
7996 16 : (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
7997 :
7998 194462 : return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
7999 : }
8000 :
8001 : return result;
8002 : }
8003 :
8004 : /* Like cp_build_compound_expr, but for the c-common bits. */
8005 :
8006 : tree
8007 10257 : build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8008 : {
8009 10257 : return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8010 : }
8011 :
8012 : /* Build a compound expression. */
8013 :
8014 : tree
8015 281450 : cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8016 : {
8017 281450 : lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8018 :
8019 281450 : if (lhs == error_mark_node || rhs == error_mark_node)
8020 : return error_mark_node;
8021 :
8022 281446 : if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8023 0 : lhs = TREE_OPERAND (lhs, 0);
8024 281446 : tree eptype = NULL_TREE;
8025 281446 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8026 : {
8027 4 : eptype = TREE_TYPE (rhs);
8028 4 : rhs = TREE_OPERAND (rhs, 0);
8029 : }
8030 :
8031 281446 : if (TREE_CODE (rhs) == TARGET_EXPR)
8032 : {
8033 : /* If the rhs is a TARGET_EXPR, then build the compound
8034 : expression inside the target_expr's initializer. This
8035 : helps the compiler to eliminate unnecessary temporaries. */
8036 451 : tree init = TREE_OPERAND (rhs, 1);
8037 :
8038 451 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8039 451 : TREE_OPERAND (rhs, 1) = init;
8040 :
8041 451 : if (eptype)
8042 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8043 451 : return rhs;
8044 : }
8045 :
8046 280995 : if (type_unknown_p (rhs))
8047 : {
8048 47 : if (complain & tf_error)
8049 71 : error_at (cp_expr_loc_or_input_loc (rhs),
8050 : "no context to resolve type of %qE", rhs);
8051 47 : return error_mark_node;
8052 : }
8053 :
8054 280948 : tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8055 280948 : if (eptype)
8056 4 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8057 : return ret;
8058 : }
8059 :
8060 : /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8061 : casts away constness. CAST gives the type of cast. Returns true
8062 : if the cast is ill-formed, false if it is well-formed.
8063 :
8064 : ??? This function warns for casting away any qualifier not just
8065 : const. We would like to specify exactly what qualifiers are casted
8066 : away.
8067 : */
8068 :
8069 : static bool
8070 441123 : check_for_casting_away_constness (location_t loc, tree src_type,
8071 : tree dest_type, enum tree_code cast,
8072 : tsubst_flags_t complain)
8073 : {
8074 : /* C-style casts are allowed to cast away constness. With
8075 : WARN_CAST_QUAL, we still want to issue a warning. */
8076 441123 : if (cast == CAST_EXPR && !warn_cast_qual)
8077 : return false;
8078 :
8079 397502 : if (!casts_away_constness (src_type, dest_type, complain))
8080 : return false;
8081 :
8082 411 : switch (cast)
8083 : {
8084 348 : case CAST_EXPR:
8085 348 : if (complain & tf_warning)
8086 348 : warning_at (loc, OPT_Wcast_qual,
8087 : "cast from type %qT to type %qT casts away qualifiers",
8088 : src_type, dest_type);
8089 : return false;
8090 :
8091 45 : case STATIC_CAST_EXPR:
8092 45 : if (complain & tf_error)
8093 32 : error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8094 : "away qualifiers",
8095 : src_type, dest_type);
8096 : return true;
8097 :
8098 18 : case REINTERPRET_CAST_EXPR:
8099 18 : if (complain & tf_error)
8100 10 : error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8101 : "casts away qualifiers",
8102 : src_type, dest_type);
8103 : return true;
8104 :
8105 0 : default:
8106 0 : gcc_unreachable();
8107 : }
8108 : }
8109 :
8110 : /* Warns if the cast from expression EXPR to type TYPE is useless. */
8111 : void
8112 30518800 : maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8113 : tsubst_flags_t complain)
8114 : {
8115 30518800 : if (warn_useless_cast
8116 191 : && complain & tf_warning)
8117 : {
8118 191 : if (TYPE_REF_P (type)
8119 191 : ? ((TYPE_REF_IS_RVALUE (type)
8120 110 : ? xvalue_p (expr) : lvalue_p (expr))
8121 220 : && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8122 : /* Don't warn when converting a class object to a non-reference type,
8123 : because that's a common way to create a temporary. */
8124 81 : : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8125 126 : warning_at (loc, OPT_Wuseless_cast,
8126 : "useless cast to type %q#T", type);
8127 : }
8128 30518800 : }
8129 :
8130 : /* Warns if the cast ignores cv-qualifiers on TYPE. */
8131 : static void
8132 30515034 : maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8133 : tsubst_flags_t complain)
8134 : {
8135 30515034 : if (warn_ignored_qualifiers
8136 238896 : && complain & tf_warning
8137 238610 : && !CLASS_TYPE_P (type)
8138 30749885 : && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8139 36 : warning_at (loc, OPT_Wignored_qualifiers,
8140 : "type qualifiers ignored on cast result type");
8141 30515034 : }
8142 :
8143 : /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8144 : (another pointer-to-member type in the same hierarchy) and return
8145 : the converted expression. If ALLOW_INVERSE_P is permitted, a
8146 : pointer-to-derived may be converted to pointer-to-base; otherwise,
8147 : only the other direction is permitted. If C_CAST_P is true, this
8148 : conversion is taking place as part of a C-style cast. */
8149 :
8150 : tree
8151 21389 : convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8152 : bool c_cast_p, tsubst_flags_t complain)
8153 : {
8154 21389 : if (same_type_p (type, TREE_TYPE (expr)))
8155 : return expr;
8156 :
8157 21389 : if (TYPE_PTRDATAMEM_P (type))
8158 : {
8159 408 : tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8160 408 : tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8161 408 : tree delta = (get_delta_difference
8162 408 : (obase, nbase,
8163 : allow_inverse_p, c_cast_p, complain));
8164 :
8165 408 : if (delta == error_mark_node)
8166 : return error_mark_node;
8167 :
8168 401 : if (!same_type_p (obase, nbase))
8169 : {
8170 313 : if (TREE_CODE (expr) == PTRMEM_CST)
8171 174 : expr = cplus_expand_constant (expr);
8172 :
8173 313 : tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8174 313 : build_int_cst (TREE_TYPE (expr), -1),
8175 : complain);
8176 313 : tree op1 = build_nop (ptrdiff_type_node, expr);
8177 313 : tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8178 : complain);
8179 :
8180 313 : expr = fold_build3_loc (input_location,
8181 : COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8182 : }
8183 :
8184 401 : return build_nop (type, expr);
8185 : }
8186 : else
8187 20981 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8188 20981 : allow_inverse_p, c_cast_p, complain);
8189 : }
8190 :
8191 : /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8192 : this static_cast is being attempted as one of the possible casts
8193 : allowed by a C-style cast. (In that case, accessibility of base
8194 : classes is not considered, and it is OK to cast away
8195 : constness.) Return the result of the cast. *VALID_P is set to
8196 : indicate whether or not the cast was valid. */
8197 :
8198 : static tree
8199 30363142 : build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8200 : bool *valid_p, tsubst_flags_t complain)
8201 : {
8202 30363142 : tree intype;
8203 30363142 : tree result;
8204 30363142 : cp_lvalue_kind clk;
8205 :
8206 : /* Assume the cast is valid. */
8207 30363142 : *valid_p = true;
8208 :
8209 30363142 : intype = unlowered_expr_type (expr);
8210 :
8211 : /* Save casted types in the function's used types hash table. */
8212 30363142 : used_types_insert (type);
8213 :
8214 : /* A prvalue of non-class type is cv-unqualified. */
8215 30363142 : if (!CLASS_TYPE_P (type))
8216 29752942 : type = cv_unqualified (type);
8217 :
8218 : /* [expr.static.cast]
8219 :
8220 : An lvalue of type "cv1 B", where B is a class type, can be cast
8221 : to type "reference to cv2 D", where D is a class derived (clause
8222 : _class.derived_) from B, if a valid standard conversion from
8223 : "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8224 : same cv-qualification as, or greater cv-qualification than, cv1,
8225 : and B is not a virtual base class of D. */
8226 : /* We check this case before checking the validity of "TYPE t =
8227 : EXPR;" below because for this case:
8228 :
8229 : struct B {};
8230 : struct D : public B { D(const B&); };
8231 : extern B& b;
8232 : void f() { static_cast<const D&>(b); }
8233 :
8234 : we want to avoid constructing a new D. The standard is not
8235 : completely clear about this issue, but our interpretation is
8236 : consistent with other compilers. */
8237 30363142 : if (TYPE_REF_P (type)
8238 1363318 : && CLASS_TYPE_P (TREE_TYPE (type))
8239 1162706 : && CLASS_TYPE_P (intype)
8240 1162692 : && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8241 1162265 : && DERIVED_FROM_P (intype, TREE_TYPE (type))
8242 1138695 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8243 1138695 : build_pointer_type (TYPE_MAIN_VARIANT
8244 : (TREE_TYPE (type))),
8245 : complain)
8246 31501837 : && (c_cast_p
8247 1138663 : || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8248 : {
8249 1138695 : tree base;
8250 :
8251 1138695 : if (processing_template_decl)
8252 : return expr;
8253 :
8254 : /* There is a standard conversion from "D*" to "B*" even if "B"
8255 : is ambiguous or inaccessible. If this is really a
8256 : static_cast, then we check both for inaccessibility and
8257 : ambiguity. However, if this is a static_cast being performed
8258 : because the user wrote a C-style cast, then accessibility is
8259 : not considered. */
8260 2169404 : base = lookup_base (TREE_TYPE (type), intype,
8261 : c_cast_p ? ba_unique : ba_check,
8262 : NULL, complain);
8263 1084718 : expr = cp_build_addr_expr (expr, complain);
8264 :
8265 1084718 : if (sanitize_flags_p (SANITIZE_VPTR))
8266 : {
8267 140 : tree ubsan_check
8268 140 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8269 : intype, expr);
8270 140 : if (ubsan_check)
8271 1084718 : expr = ubsan_check;
8272 : }
8273 :
8274 : /* Convert from "B*" to "D*". This function will check that "B"
8275 : is not a virtual base of "D". Even if we don't have a guarantee
8276 : that expr is NULL, if the static_cast is to a reference type,
8277 : it is UB if it would be NULL, so omit the non-NULL check. */
8278 1084718 : expr = build_base_path (MINUS_EXPR, expr, base,
8279 : /*nonnull=*/flag_delete_null_pointer_checks,
8280 : complain);
8281 :
8282 : /* Convert the pointer to a reference -- but then remember that
8283 : there are no expressions with reference type in C++.
8284 :
8285 : We call rvalue so that there's an actual tree code
8286 : (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8287 : is a variable with the same type, the conversion would get folded
8288 : away, leaving just the variable and causing lvalue_kind to give
8289 : the wrong answer. */
8290 1084718 : expr = cp_fold_convert (type, expr);
8291 :
8292 : /* When -fsanitize=null, make sure to diagnose reference binding to
8293 : NULL even when the reference is converted to pointer later on. */
8294 1084718 : if (sanitize_flags_p (SANITIZE_NULL)
8295 148 : && TREE_CODE (expr) == COND_EXPR
8296 8 : && TREE_OPERAND (expr, 2)
8297 8 : && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8298 1084726 : && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8299 8 : ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8300 :
8301 1084718 : return convert_from_reference (rvalue (expr));
8302 : }
8303 :
8304 : /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8305 : cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8306 29224447 : if (TYPE_REF_P (type)
8307 224623 : && TYPE_REF_IS_RVALUE (type)
8308 180637 : && (clk = real_lvalue_p (expr))
8309 179902 : && reference_compatible_p (TREE_TYPE (type), intype)
8310 29404346 : && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8311 : {
8312 179899 : if (processing_template_decl)
8313 : return expr;
8314 179777 : if (clk == clk_ordinary)
8315 : {
8316 : /* Handle the (non-bit-field) lvalue case here by casting to
8317 : lvalue reference and then changing it to an rvalue reference.
8318 : Casting an xvalue to rvalue reference will be handled by the
8319 : main code path. */
8320 179774 : tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8321 179774 : result = (perform_direct_initialization_if_possible
8322 179774 : (lref, expr, c_cast_p, complain));
8323 179774 : result = build1 (NON_LVALUE_EXPR, type, result);
8324 179774 : return convert_from_reference (result);
8325 : }
8326 : else
8327 : /* For a bit-field or packed field, bind to a temporary. */
8328 3 : expr = rvalue (expr);
8329 : }
8330 :
8331 : /* Resolve overloaded address here rather than once in
8332 : implicit_conversion and again in the inverse code below. */
8333 29044551 : if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8334 : {
8335 23 : expr = instantiate_type (type, expr, complain);
8336 23 : intype = TREE_TYPE (expr);
8337 : }
8338 :
8339 : /* [expr.static.cast]
8340 :
8341 : Any expression can be explicitly converted to type cv void. */
8342 29044551 : if (VOID_TYPE_P (type))
8343 : {
8344 60229 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8345 0 : expr = TREE_OPERAND (expr, 0);
8346 60229 : return convert_to_void (expr, ICV_CAST, complain);
8347 : }
8348 :
8349 : /* [class.abstract]
8350 : An abstract class shall not be used ... as the type of an explicit
8351 : conversion. */
8352 28984322 : if (abstract_virtuals_error (ACU_CAST, type, complain))
8353 6 : return error_mark_node;
8354 :
8355 : /* [expr.static.cast]
8356 :
8357 : An expression e can be explicitly converted to a type T using a
8358 : static_cast of the form static_cast<T>(e) if the declaration T
8359 : t(e);" is well-formed, for some invented temporary variable
8360 : t. */
8361 28984316 : result = perform_direct_initialization_if_possible (type, expr,
8362 : c_cast_p, complain);
8363 : /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8364 : which initialize the first element of the aggregate. We need to handle
8365 : the array case specifically. */
8366 28984316 : if (result == NULL_TREE
8367 3018354 : && cxx_dialect >= cxx20
8368 205544 : && TREE_CODE (type) == ARRAY_TYPE)
8369 : {
8370 : /* Create { EXPR } and perform direct-initialization from it. */
8371 5 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8372 5 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8373 5 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8374 5 : result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8375 : complain);
8376 : }
8377 3018354 : if (result)
8378 : {
8379 25965967 : if (processing_template_decl)
8380 : return expr;
8381 :
8382 25523517 : result = convert_from_reference (result);
8383 :
8384 : /* [expr.static.cast]
8385 :
8386 : If T is a reference type, the result is an lvalue; otherwise,
8387 : the result is an rvalue. */
8388 25523517 : if (!TYPE_REF_P (type))
8389 : {
8390 25478887 : result = rvalue (result);
8391 :
8392 25478887 : if (result == expr && SCALAR_TYPE_P (type))
8393 : /* Leave some record of the cast. */
8394 1894946 : result = build_nop (type, expr);
8395 : }
8396 25523517 : return result;
8397 : }
8398 :
8399 : /* [expr.static.cast]
8400 :
8401 : The inverse of any standard conversion sequence (clause _conv_),
8402 : other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8403 : (_conv.array_), function-to-pointer (_conv.func_), and boolean
8404 : (_conv.bool_) conversions, can be performed explicitly using
8405 : static_cast subject to the restriction that the explicit
8406 : conversion does not cast away constness (_expr.const.cast_), and
8407 : the following additional rules for specific cases: */
8408 : /* For reference, the conversions not excluded are: integral
8409 : promotions, floating-point promotion, integral conversions,
8410 : floating-point conversions, floating-integral conversions,
8411 : pointer conversions, and pointer to member conversions. */
8412 : /* DR 128
8413 :
8414 : A value of integral _or enumeration_ type can be explicitly
8415 : converted to an enumeration type. */
8416 : /* The effect of all that is that any conversion between any two
8417 : types which are integral, floating, or enumeration types can be
8418 : performed. */
8419 3018349 : if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8420 3018349 : || SCALAR_FLOAT_TYPE_P (type))
8421 1242136 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8422 1242136 : || SCALAR_FLOAT_TYPE_P (intype)))
8423 : {
8424 1156699 : if (processing_template_decl)
8425 : return expr;
8426 1112772 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8427 0 : expr = TREE_OPERAND (expr, 0);
8428 1112772 : return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8429 : }
8430 :
8431 516252 : if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8432 512136 : && CLASS_TYPE_P (TREE_TYPE (type))
8433 147269 : && CLASS_TYPE_P (TREE_TYPE (intype))
8434 1894236 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8435 : (TREE_TYPE (intype))),
8436 32586 : build_pointer_type (TYPE_MAIN_VARIANT
8437 : (TREE_TYPE (type))),
8438 : complain))
8439 : {
8440 32292 : tree base;
8441 :
8442 32292 : if (processing_template_decl)
8443 : return expr;
8444 :
8445 32276 : if (!c_cast_p
8446 32276 : && check_for_casting_away_constness (loc, intype, type,
8447 : STATIC_CAST_EXPR,
8448 : complain))
8449 7 : return error_mark_node;
8450 63822 : base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8451 : c_cast_p ? ba_unique : ba_check,
8452 : NULL, complain);
8453 32269 : expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8454 : complain);
8455 :
8456 32269 : if (sanitize_flags_p (SANITIZE_VPTR))
8457 : {
8458 51 : tree ubsan_check
8459 51 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8460 : intype, expr);
8461 51 : if (ubsan_check)
8462 32269 : expr = ubsan_check;
8463 : }
8464 :
8465 32269 : return cp_fold_convert (type, expr);
8466 : }
8467 :
8468 113 : if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8469 1829358 : || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8470 : {
8471 299 : tree c1;
8472 299 : tree c2;
8473 299 : tree t1;
8474 299 : tree t2;
8475 :
8476 299 : c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8477 299 : c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8478 :
8479 299 : if (TYPE_PTRDATAMEM_P (type))
8480 : {
8481 113 : t1 = (build_ptrmem_type
8482 113 : (c1,
8483 113 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8484 113 : t2 = (build_ptrmem_type
8485 113 : (c2,
8486 113 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8487 : }
8488 : else
8489 : {
8490 : t1 = intype;
8491 : t2 = type;
8492 : }
8493 299 : if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8494 : {
8495 196 : if (!c_cast_p
8496 196 : && check_for_casting_away_constness (loc, intype, type,
8497 : STATIC_CAST_EXPR,
8498 : complain))
8499 11 : return error_mark_node;
8500 185 : if (processing_template_decl)
8501 : return expr;
8502 185 : return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8503 185 : c_cast_p, complain);
8504 : }
8505 : }
8506 :
8507 : /* [expr.static.cast]
8508 :
8509 : An rvalue of type "pointer to cv void" can be explicitly
8510 : converted to a pointer to object type. A value of type pointer
8511 : to object converted to "pointer to cv void" and back to the
8512 : original pointer type will have its original value. */
8513 1829162 : if (TYPE_PTR_P (intype)
8514 563093 : && VOID_TYPE_P (TREE_TYPE (intype))
8515 2291787 : && TYPE_PTROB_P (type))
8516 : {
8517 431276 : if (!c_cast_p
8518 431276 : && check_for_casting_away_constness (loc, intype, type,
8519 : STATIC_CAST_EXPR,
8520 : complain))
8521 27 : return error_mark_node;
8522 431249 : if (processing_template_decl)
8523 : return expr;
8524 372375 : return build_nop (type, expr);
8525 : }
8526 :
8527 1397886 : *valid_p = false;
8528 1397886 : return error_mark_node;
8529 : }
8530 :
8531 : /* Return an expression representing static_cast<TYPE>(EXPR). */
8532 :
8533 : tree
8534 7090211 : build_static_cast (location_t loc, tree type, tree oexpr,
8535 : tsubst_flags_t complain)
8536 : {
8537 7090211 : tree expr = oexpr;
8538 7090211 : tree result;
8539 7090211 : bool valid_p;
8540 :
8541 7090211 : if (type == error_mark_node || expr == error_mark_node)
8542 : return error_mark_node;
8543 :
8544 7090182 : bool dependent = (dependent_type_p (type)
8545 7090182 : || type_dependent_expression_p (expr));
8546 4454718 : if (dependent)
8547 : {
8548 3234862 : tmpl:
8549 3234862 : expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8550 : /* We don't know if it will or will not have side effects. */
8551 3234862 : TREE_SIDE_EFFECTS (expr) = 1;
8552 3234862 : result = convert_from_reference (expr);
8553 3234862 : protected_set_expr_location (result, loc);
8554 3234862 : return result;
8555 : }
8556 4454718 : else if (processing_template_decl)
8557 599402 : expr = build_non_dependent_expr (expr);
8558 :
8559 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8560 : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8561 4454718 : if (!TYPE_REF_P (type)
8562 3091672 : && TREE_CODE (expr) == NOP_EXPR
8563 4521688 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8564 64 : expr = TREE_OPERAND (expr, 0);
8565 :
8566 4454718 : result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8567 : &valid_p, complain);
8568 4454718 : if (valid_p)
8569 : {
8570 4454530 : if (result != error_mark_node)
8571 : {
8572 4454413 : maybe_warn_about_useless_cast (loc, type, expr, complain);
8573 4454413 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8574 : }
8575 4454530 : if (processing_template_decl)
8576 599398 : goto tmpl;
8577 3855132 : protected_set_expr_location (result, loc);
8578 3855132 : return result;
8579 : }
8580 :
8581 188 : if (complain & tf_error)
8582 : {
8583 168 : error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8584 168 : TREE_TYPE (expr), type);
8585 168 : if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8586 134 : && CLASS_TYPE_P (TREE_TYPE (type))
8587 207 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8588 23 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8589 23 : "class type %qT is incomplete", TREE_TYPE (type));
8590 168 : tree expr_type = TREE_TYPE (expr);
8591 168 : if (TYPE_PTR_P (expr_type))
8592 42 : expr_type = TREE_TYPE (expr_type);
8593 168 : if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8594 16 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8595 : "class type %qT is incomplete", expr_type);
8596 : }
8597 188 : return error_mark_node;
8598 : }
8599 :
8600 : /* EXPR is an expression with member function or pointer-to-member
8601 : function type. TYPE is a pointer type. Converting EXPR to TYPE is
8602 : not permitted by ISO C++, but we accept it in some modes. If we
8603 : are not in one of those modes, issue a diagnostic. Return the
8604 : converted expression. */
8605 :
8606 : tree
8607 142 : convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8608 : {
8609 142 : tree intype;
8610 142 : tree decl;
8611 :
8612 142 : intype = TREE_TYPE (expr);
8613 142 : gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8614 : || TREE_CODE (intype) == METHOD_TYPE);
8615 :
8616 142 : if (!(complain & tf_warning_or_error))
8617 0 : return error_mark_node;
8618 :
8619 142 : location_t loc = cp_expr_loc_or_input_loc (expr);
8620 :
8621 142 : if (pedantic || warn_pmf2ptr)
8622 120 : pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8623 : "converting from %qH to %qI", intype, type);
8624 :
8625 142 : STRIP_ANY_LOCATION_WRAPPER (expr);
8626 :
8627 142 : if (TREE_CODE (intype) == METHOD_TYPE)
8628 57 : expr = build_addr_func (expr, complain);
8629 85 : else if (TREE_CODE (expr) == PTRMEM_CST)
8630 61 : expr = build_address (PTRMEM_CST_MEMBER (expr));
8631 : else
8632 : {
8633 24 : decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8634 24 : decl = build_address (decl);
8635 24 : expr = get_member_function_from_ptrfunc (&decl, expr, complain);
8636 : }
8637 :
8638 142 : if (expr == error_mark_node)
8639 : return error_mark_node;
8640 :
8641 142 : expr = build_nop (type, expr);
8642 142 : SET_EXPR_LOCATION (expr, loc);
8643 142 : return expr;
8644 : }
8645 :
8646 : /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8647 : constexpr evaluation knows to reject it. */
8648 :
8649 : static tree
8650 69255 : build_nop_reinterpret (tree type, tree expr)
8651 : {
8652 69255 : tree ret = build_nop (type, expr);
8653 69255 : if (ret != expr)
8654 69255 : REINTERPRET_CAST_P (ret) = true;
8655 69255 : return ret;
8656 : }
8657 :
8658 : /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8659 : If C_CAST_P is true, this reinterpret cast is being done as part of
8660 : a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8661 : indicate whether or not reinterpret_cast was valid. */
8662 :
8663 : static tree
8664 1466747 : build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8665 : bool c_cast_p, bool *valid_p,
8666 : tsubst_flags_t complain)
8667 : {
8668 1466747 : tree intype;
8669 :
8670 : /* Assume the cast is invalid. */
8671 1466747 : if (valid_p)
8672 1397749 : *valid_p = true;
8673 :
8674 1466747 : if (type == error_mark_node || error_operand_p (expr))
8675 : return error_mark_node;
8676 :
8677 1466747 : intype = TREE_TYPE (expr);
8678 :
8679 : /* Save casted types in the function's used types hash table. */
8680 1466747 : used_types_insert (type);
8681 :
8682 : /* A prvalue of non-class type is cv-unqualified. */
8683 1466747 : if (!CLASS_TYPE_P (type))
8684 1466735 : type = cv_unqualified (type);
8685 :
8686 : /* [expr.reinterpret.cast]
8687 : A glvalue of type T1, designating an object x, can be cast to the type
8688 : "reference to T2" if an expression of type "pointer to T1" can be
8689 : explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8690 : The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8691 : of type "pointer to T1". No temporary is created, no copy is made, and no
8692 : constructors (11.4.4) or conversion functions (11.4.7) are called. */
8693 1466747 : if (TYPE_REF_P (type))
8694 : {
8695 7508 : if (!glvalue_p (expr))
8696 : {
8697 10 : if (complain & tf_error)
8698 10 : error_at (loc, "invalid cast of a prvalue expression of type "
8699 : "%qT to type %qT",
8700 : intype, type);
8701 10 : return error_mark_node;
8702 : }
8703 :
8704 : /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8705 : "B" are related class types; the reinterpret_cast does not
8706 : adjust the pointer. */
8707 7498 : if (TYPE_PTR_P (intype)
8708 4 : && (complain & tf_warning)
8709 7502 : && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8710 : COMPARE_BASE | COMPARE_DERIVED)))
8711 4 : warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
8712 : intype, type);
8713 :
8714 7498 : expr = cp_build_addr_expr (expr, complain);
8715 :
8716 7498 : if (warn_strict_aliasing > 2)
8717 70 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8718 :
8719 7498 : if (expr != error_mark_node)
8720 7498 : expr = build_reinterpret_cast_1
8721 7498 : (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
8722 : valid_p, complain);
8723 7498 : if (expr != error_mark_node)
8724 : /* cp_build_indirect_ref isn't right for rvalue refs. */
8725 7494 : expr = convert_from_reference (fold_convert (type, expr));
8726 7498 : return expr;
8727 : }
8728 :
8729 : /* As a G++ extension, we consider conversions from member
8730 : functions, and pointers to member functions to
8731 : pointer-to-function and pointer-to-void types. If
8732 : -Wno-pmf-conversions has not been specified,
8733 : convert_member_func_to_ptr will issue an error message. */
8734 284 : if ((TYPE_PTRMEMFUNC_P (intype)
8735 1459052 : || TREE_CODE (intype) == METHOD_TYPE)
8736 244 : && TYPE_PTR_P (type)
8737 1459381 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
8738 86 : || VOID_TYPE_P (TREE_TYPE (type))))
8739 130 : return convert_member_func_to_ptr (type, expr, complain);
8740 :
8741 : /* If the cast is not to a reference type, the lvalue-to-rvalue,
8742 : array-to-pointer, and function-to-pointer conversions are
8743 : performed. */
8744 1459109 : expr = decay_conversion (expr, complain);
8745 :
8746 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8747 : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8748 1459109 : if (TREE_CODE (expr) == NOP_EXPR
8749 1459109 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8750 37 : expr = TREE_OPERAND (expr, 0);
8751 :
8752 1459109 : if (error_operand_p (expr))
8753 37 : return error_mark_node;
8754 :
8755 1459072 : intype = TREE_TYPE (expr);
8756 :
8757 : /* [expr.reinterpret.cast]
8758 : A pointer can be converted to any integral type large enough to
8759 : hold it. ... A value of type std::nullptr_t can be converted to
8760 : an integral type; the conversion has the same meaning and
8761 : validity as a conversion of (void*)0 to the integral type. */
8762 1459072 : if (CP_INTEGRAL_TYPE_P (type)
8763 103163 : && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
8764 : {
8765 101195 : if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
8766 : {
8767 26 : if (complain & tf_error)
8768 18 : permerror (loc, "cast from %qH to %qI loses precision",
8769 : intype, type);
8770 : else
8771 8 : return error_mark_node;
8772 : }
8773 101187 : if (NULLPTR_TYPE_P (intype))
8774 86 : return build_int_cst (type, 0);
8775 : }
8776 : /* [expr.reinterpret.cast]
8777 : A value of integral or enumeration type can be explicitly
8778 : converted to a pointer. */
8779 1357877 : else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
8780 : /* OK */
8781 : ;
8782 1331763 : else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8783 1329776 : || TYPE_PTR_OR_PTRMEM_P (type))
8784 1401520 : && same_type_p (type, intype))
8785 : /* DR 799 */
8786 464 : return rvalue (expr);
8787 1331299 : else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
8788 : {
8789 405 : if ((complain & tf_warning)
8790 810 : && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
8791 405 : TREE_TYPE (intype)))
8792 12 : warning_at (loc, OPT_Wcast_function_type,
8793 : "cast between incompatible function types"
8794 : " from %qH to %qI", intype, type);
8795 405 : return build_nop_reinterpret (type, expr);
8796 : }
8797 1330894 : else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
8798 : {
8799 102 : if ((complain & tf_warning)
8800 204 : && !cxx_safe_function_type_cast_p
8801 102 : (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
8802 102 : TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
8803 66 : warning_at (loc, OPT_Wcast_function_type,
8804 : "cast between incompatible pointer to member types"
8805 : " from %qH to %qI", intype, type);
8806 102 : return build_nop_reinterpret (type, expr);
8807 : }
8808 27 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8809 1330792 : || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
8810 : {
8811 68498 : if (!c_cast_p
8812 68498 : && check_for_casting_away_constness (loc, intype, type,
8813 : REINTERPRET_CAST_EXPR,
8814 : complain))
8815 18 : return error_mark_node;
8816 : /* Warn about possible alignment problems. */
8817 68480 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8818 12 : && (complain & tf_warning)
8819 12 : && !VOID_TYPE_P (type)
8820 12 : && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
8821 12 : && COMPLETE_TYPE_P (TREE_TYPE (type))
8822 12 : && COMPLETE_TYPE_P (TREE_TYPE (intype))
8823 68504 : && min_align_of_type (TREE_TYPE (type))
8824 12 : > min_align_of_type (TREE_TYPE (intype)))
8825 4 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8826 : "increases required alignment of target type",
8827 : intype, type);
8828 :
8829 68480 : if (warn_strict_aliasing <= 2)
8830 : /* strict_aliasing_warning STRIP_NOPs its expr. */
8831 56503 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8832 :
8833 68480 : return build_nop_reinterpret (type, expr);
8834 : }
8835 296 : else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
8836 1262481 : || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
8837 : {
8838 268 : if (complain & tf_warning)
8839 : /* C++11 5.2.10 p8 says that "Converting a function pointer to an
8840 : object pointer type or vice versa is conditionally-supported." */
8841 268 : warning_at (loc, OPT_Wconditionally_supported,
8842 : "casting between pointer-to-function and "
8843 : "pointer-to-object is conditionally-supported");
8844 268 : return build_nop_reinterpret (type, expr);
8845 : }
8846 1262026 : else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
8847 1259991 : return convert_to_vector (type, rvalue (expr));
8848 2035 : else if (gnu_vector_type_p (intype)
8849 2035 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8850 1907 : return convert_to_integer_nofold (type, expr);
8851 : else
8852 : {
8853 128 : if (valid_p)
8854 102 : *valid_p = false;
8855 128 : if (complain & tf_error)
8856 115 : error_at (loc, "invalid cast from type %qT to type %qT",
8857 : intype, type);
8858 128 : return error_mark_node;
8859 : }
8860 :
8861 127215 : expr = cp_convert (type, expr, complain);
8862 127215 : if (TREE_CODE (expr) == NOP_EXPR)
8863 : /* Mark any nop_expr that created as a reintepret_cast. */
8864 0 : REINTERPRET_CAST_P (expr) = true;
8865 : return expr;
8866 : }
8867 :
8868 : tree
8869 342567 : build_reinterpret_cast (location_t loc, tree type, tree expr,
8870 : tsubst_flags_t complain)
8871 : {
8872 342567 : tree r;
8873 :
8874 342567 : if (type == error_mark_node || expr == error_mark_node)
8875 : return error_mark_node;
8876 :
8877 342563 : if (processing_template_decl)
8878 : {
8879 281012 : tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
8880 :
8881 281012 : if (!TREE_SIDE_EFFECTS (t)
8882 281012 : && type_dependent_expression_p (expr))
8883 : /* There might turn out to be side effects inside expr. */
8884 129361 : TREE_SIDE_EFFECTS (t) = 1;
8885 281012 : r = convert_from_reference (t);
8886 281012 : protected_set_expr_location (r, loc);
8887 281012 : return r;
8888 : }
8889 :
8890 61551 : r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8891 : /*valid_p=*/NULL, complain);
8892 61551 : if (r != error_mark_node)
8893 : {
8894 61477 : maybe_warn_about_useless_cast (loc, type, expr, complain);
8895 61477 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8896 : }
8897 61551 : protected_set_expr_location (r, loc);
8898 61551 : return r;
8899 : }
8900 :
8901 : /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
8902 : return an appropriate expression. Otherwise, return
8903 : error_mark_node. If the cast is not valid, and COMPLAIN is true,
8904 : then a diagnostic will be issued. If VALID_P is non-NULL, we are
8905 : performing a C-style cast, its value upon return will indicate
8906 : whether or not the conversion succeeded. */
8907 :
8908 : static tree
8909 25999537 : build_const_cast_1 (location_t loc, tree dst_type, tree expr,
8910 : tsubst_flags_t complain, bool *valid_p)
8911 : {
8912 25999537 : tree src_type;
8913 25999537 : tree reference_type;
8914 :
8915 : /* Callers are responsible for handling error_mark_node as a
8916 : destination type. */
8917 25999537 : gcc_assert (dst_type != error_mark_node);
8918 : /* In a template, callers should be building syntactic
8919 : representations of casts, not using this machinery. */
8920 25999537 : gcc_assert (!processing_template_decl);
8921 :
8922 : /* Assume the conversion is invalid. */
8923 25999537 : if (valid_p)
8924 25939187 : *valid_p = false;
8925 :
8926 25999537 : if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
8927 : {
8928 25649949 : if (complain & tf_error)
8929 11 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8930 : "which is not a pointer, reference, "
8931 : "nor a pointer-to-data-member type", dst_type);
8932 25649949 : return error_mark_node;
8933 : }
8934 :
8935 349588 : if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
8936 : {
8937 667 : if (complain & tf_error)
8938 8 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8939 : "which is a pointer or reference to a function type",
8940 : dst_type);
8941 667 : return error_mark_node;
8942 : }
8943 :
8944 : /* A prvalue of non-class type is cv-unqualified. */
8945 348921 : dst_type = cv_unqualified (dst_type);
8946 :
8947 : /* Save casted types in the function's used types hash table. */
8948 348921 : used_types_insert (dst_type);
8949 :
8950 348921 : src_type = TREE_TYPE (expr);
8951 : /* Expressions do not really have reference types. */
8952 348921 : if (TYPE_REF_P (src_type))
8953 0 : src_type = TREE_TYPE (src_type);
8954 :
8955 : /* [expr.const.cast]
8956 :
8957 : For two object types T1 and T2, if a pointer to T1 can be explicitly
8958 : converted to the type "pointer to T2" using a const_cast, then the
8959 : following conversions can also be made:
8960 :
8961 : -- an lvalue of type T1 can be explicitly converted to an lvalue of
8962 : type T2 using the cast const_cast<T2&>;
8963 :
8964 : -- a glvalue of type T1 can be explicitly converted to an xvalue of
8965 : type T2 using the cast const_cast<T2&&>; and
8966 :
8967 : -- if T1 is a class type, a prvalue of type T1 can be explicitly
8968 : converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
8969 :
8970 348921 : if (TYPE_REF_P (dst_type))
8971 : {
8972 4177 : reference_type = dst_type;
8973 4177 : if (!TYPE_REF_IS_RVALUE (dst_type)
8974 4177 : ? lvalue_p (expr)
8975 215 : : obvalue_p (expr))
8976 : /* OK. */;
8977 : else
8978 : {
8979 40 : if (complain & tf_error)
8980 9 : error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
8981 : "to type %qT",
8982 : src_type, dst_type);
8983 40 : return error_mark_node;
8984 : }
8985 4137 : dst_type = build_pointer_type (TREE_TYPE (dst_type));
8986 4137 : src_type = build_pointer_type (src_type);
8987 : }
8988 : else
8989 : {
8990 344744 : reference_type = NULL_TREE;
8991 : /* If the destination type is not a reference type, the
8992 : lvalue-to-rvalue, array-to-pointer, and function-to-pointer
8993 : conversions are performed. */
8994 344744 : src_type = type_decays_to (src_type);
8995 344744 : if (src_type == error_mark_node)
8996 : return error_mark_node;
8997 : }
8998 :
8999 348881 : if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9000 : {
9001 288029 : if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9002 : {
9003 91032 : if (valid_p)
9004 : {
9005 30763 : *valid_p = true;
9006 : /* This cast is actually a C-style cast. Issue a warning if
9007 : the user is making a potentially unsafe cast. */
9008 30763 : check_for_casting_away_constness (loc, src_type, dst_type,
9009 : CAST_EXPR, complain);
9010 : /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9011 30763 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9012 4 : && (complain & tf_warning)
9013 30771 : && min_align_of_type (TREE_TYPE (dst_type))
9014 4 : > min_align_of_type (TREE_TYPE (src_type)))
9015 4 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9016 : "increases required alignment of target type",
9017 : src_type, dst_type);
9018 : }
9019 91032 : if (reference_type)
9020 : {
9021 3902 : expr = cp_build_addr_expr (expr, complain);
9022 3902 : if (expr == error_mark_node)
9023 : return error_mark_node;
9024 3890 : expr = build_nop (reference_type, expr);
9025 3890 : return convert_from_reference (expr);
9026 : }
9027 : else
9028 : {
9029 87130 : expr = decay_conversion (expr, complain);
9030 87130 : if (expr == error_mark_node)
9031 : return error_mark_node;
9032 :
9033 : /* build_c_cast puts on a NOP_EXPR to make the result not an
9034 : lvalue. Strip such NOP_EXPRs if VALUE is being used in
9035 : non-lvalue context. */
9036 87130 : if (TREE_CODE (expr) == NOP_EXPR
9037 87130 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9038 4 : expr = TREE_OPERAND (expr, 0);
9039 87130 : return build_nop (dst_type, expr);
9040 : }
9041 : }
9042 196997 : else if (valid_p
9043 393949 : && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9044 196952 : TREE_TYPE (src_type)))
9045 13338 : check_for_casting_away_constness (loc, src_type, dst_type,
9046 : CAST_EXPR, complain);
9047 : }
9048 :
9049 257849 : if (complain & tf_error)
9050 21 : error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9051 : src_type, dst_type);
9052 257849 : return error_mark_node;
9053 : }
9054 :
9055 : tree
9056 389570 : build_const_cast (location_t loc, tree type, tree expr,
9057 : tsubst_flags_t complain)
9058 : {
9059 389570 : tree r;
9060 :
9061 389570 : if (type == error_mark_node || error_operand_p (expr))
9062 : return error_mark_node;
9063 :
9064 389562 : if (processing_template_decl)
9065 : {
9066 329212 : tree t = build_min (CONST_CAST_EXPR, type, expr);
9067 :
9068 329212 : if (!TREE_SIDE_EFFECTS (t)
9069 329212 : && type_dependent_expression_p (expr))
9070 : /* There might turn out to be side effects inside expr. */
9071 270860 : TREE_SIDE_EFFECTS (t) = 1;
9072 329212 : r = convert_from_reference (t);
9073 329212 : protected_set_expr_location (r, loc);
9074 329212 : return r;
9075 : }
9076 :
9077 60350 : r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9078 60350 : if (r != error_mark_node)
9079 : {
9080 60269 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9081 60269 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9082 : }
9083 60350 : protected_set_expr_location (r, loc);
9084 60350 : return r;
9085 : }
9086 :
9087 : /* Like cp_build_c_cast, but for the c-common bits. */
9088 :
9089 : tree
9090 0 : build_c_cast (location_t loc, tree type, tree expr)
9091 : {
9092 0 : return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9093 : }
9094 :
9095 : /* Like the "build_c_cast" used for c-common, but using cp_expr to
9096 : preserve location information even for tree nodes that don't
9097 : support it. */
9098 :
9099 : cp_expr
9100 6613052 : build_c_cast (location_t loc, tree type, cp_expr expr)
9101 : {
9102 6613052 : cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9103 6613052 : result.set_location (loc);
9104 6613052 : return result;
9105 : }
9106 :
9107 : /* Build an expression representing an explicit C-style cast to type
9108 : TYPE of expression EXPR. */
9109 :
9110 : tree
9111 27047927 : cp_build_c_cast (location_t loc, tree type, tree expr,
9112 : tsubst_flags_t complain)
9113 : {
9114 27047927 : tree value = expr;
9115 27047927 : tree result;
9116 27047927 : bool valid_p;
9117 :
9118 27047927 : if (type == error_mark_node || error_operand_p (expr))
9119 : return error_mark_node;
9120 :
9121 27047771 : if (processing_template_decl)
9122 : {
9123 1108560 : tree t = build_min (CAST_EXPR, type,
9124 : tree_cons (NULL_TREE, value, NULL_TREE));
9125 : /* We don't know if it will or will not have side effects. */
9126 1108560 : TREE_SIDE_EFFECTS (t) = 1;
9127 1108560 : return convert_from_reference (t);
9128 : }
9129 :
9130 : /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9131 : 'Class') should always be retained, because this information aids
9132 : in method lookup. */
9133 25939211 : if (objc_is_object_ptr (type)
9134 25939211 : && objc_is_object_ptr (TREE_TYPE (expr)))
9135 0 : return build_nop (type, expr);
9136 :
9137 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9138 : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9139 25939211 : if (!TYPE_REF_P (type)
9140 25938745 : && TREE_CODE (value) == NOP_EXPR
9141 26025592 : && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9142 10136 : value = TREE_OPERAND (value, 0);
9143 :
9144 25939211 : if (TREE_CODE (type) == ARRAY_TYPE)
9145 : {
9146 : /* Allow casting from T1* to T2[] because Cfront allows it.
9147 : NIHCL uses it. It is not valid ISO C++ however. */
9148 4 : if (TYPE_PTR_P (TREE_TYPE (expr)))
9149 : {
9150 0 : if (complain & tf_error)
9151 0 : permerror (loc, "ISO C++ forbids casting to an array type %qT",
9152 : type);
9153 : else
9154 0 : return error_mark_node;
9155 0 : type = build_pointer_type (TREE_TYPE (type));
9156 : }
9157 : else
9158 : {
9159 4 : if (complain & tf_error)
9160 4 : error_at (loc, "ISO C++ forbids casting to an array type %qT",
9161 : type);
9162 4 : return error_mark_node;
9163 : }
9164 : }
9165 :
9166 25939207 : if (FUNC_OR_METHOD_TYPE_P (type))
9167 : {
9168 20 : if (complain & tf_error)
9169 20 : error_at (loc, "invalid cast to function type %qT", type);
9170 20 : return error_mark_node;
9171 : }
9172 :
9173 25939187 : if (TYPE_PTR_P (type)
9174 288645 : && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9175 : /* Casting to an integer of smaller size is an error detected elsewhere. */
9176 59644 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9177 : /* Don't warn about converting any constant. */
9178 25956289 : && !TREE_CONSTANT (value))
9179 70 : warning_at (loc, OPT_Wint_to_pointer_cast,
9180 : "cast to pointer from integer of different size");
9181 :
9182 : /* A C-style cast can be a const_cast. */
9183 25939187 : result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9184 : &valid_p);
9185 25939187 : if (valid_p)
9186 : {
9187 30763 : if (result != error_mark_node)
9188 : {
9189 30751 : maybe_warn_about_useless_cast (loc, type, value, complain);
9190 30751 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9191 : }
9192 30763 : return result;
9193 : }
9194 :
9195 : /* Or a static cast. */
9196 25908424 : result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9197 : &valid_p, complain);
9198 : /* Or a reinterpret_cast. */
9199 25908424 : if (!valid_p)
9200 1397698 : result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9201 : &valid_p, complain);
9202 : /* The static_cast or reinterpret_cast may be followed by a
9203 : const_cast. */
9204 25908424 : if (valid_p
9205 : /* A valid cast may result in errors if, for example, a
9206 : conversion to an ambiguous base class is required. */
9207 25908424 : && !error_operand_p (result))
9208 : {
9209 25908124 : tree result_type;
9210 :
9211 25908124 : maybe_warn_about_useless_cast (loc, type, value, complain);
9212 25908124 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9213 :
9214 : /* Non-class rvalues always have cv-unqualified type. */
9215 25908124 : if (!CLASS_TYPE_P (type))
9216 25319021 : type = TYPE_MAIN_VARIANT (type);
9217 25908124 : result_type = TREE_TYPE (result);
9218 25908124 : if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9219 25318757 : result_type = TYPE_MAIN_VARIANT (result_type);
9220 : /* If the type of RESULT does not match TYPE, perform a
9221 : const_cast to make it match. If the static_cast or
9222 : reinterpret_cast succeeded, we will differ by at most
9223 : cv-qualification, so the follow-on const_cast is guaranteed
9224 : to succeed. */
9225 25908124 : if (!same_type_p (non_reference (type), non_reference (result_type)))
9226 : {
9227 0 : result = build_const_cast_1 (loc, type, result, false, &valid_p);
9228 0 : gcc_assert (valid_p);
9229 : }
9230 25908124 : return result;
9231 : }
9232 :
9233 300 : return error_mark_node;
9234 : }
9235 :
9236 : /* Warn when a value is moved to itself with std::move. LHS is the target,
9237 : RHS may be the std::move call, and LOC is the location of the whole
9238 : assignment. */
9239 :
9240 : static void
9241 9485788 : maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9242 : {
9243 9485788 : if (!warn_self_move)
9244 9484563 : return;
9245 :
9246 : /* C++98 doesn't know move. */
9247 191209 : if (cxx_dialect < cxx11)
9248 : return;
9249 :
9250 182535 : if (processing_template_decl)
9251 : return;
9252 :
9253 8799 : if (!REFERENCE_REF_P (rhs)
9254 190225 : || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9255 : return;
9256 1721 : tree fn = TREE_OPERAND (rhs, 0);
9257 1721 : if (!is_std_move_p (fn))
9258 : return;
9259 :
9260 : /* Just a little helper to strip * and various NOPs. */
9261 3675 : auto extract_op = [] (tree &op) {
9262 2450 : STRIP_NOPS (op);
9263 3479 : while (INDIRECT_REF_P (op))
9264 1029 : op = TREE_OPERAND (op, 0);
9265 2450 : op = maybe_undo_parenthesized_ref (op);
9266 2450 : STRIP_ANY_LOCATION_WRAPPER (op);
9267 2450 : };
9268 :
9269 1225 : tree arg = CALL_EXPR_ARG (fn, 0);
9270 1225 : extract_op (arg);
9271 1225 : if (TREE_CODE (arg) == ADDR_EXPR)
9272 608 : arg = TREE_OPERAND (arg, 0);
9273 1225 : tree type = TREE_TYPE (lhs);
9274 1225 : tree orig_lhs = lhs;
9275 1225 : extract_op (lhs);
9276 1225 : if (cp_tree_equal (lhs, arg))
9277 : {
9278 111 : auto_diagnostic_group d;
9279 111 : if (warning_at (loc, OPT_Wself_move,
9280 : "moving %qE of type %qT to itself", orig_lhs, type))
9281 111 : inform (loc, "remove %<std::move%> call");
9282 111 : }
9283 : }
9284 :
9285 : /* For use from the C common bits. */
9286 : tree
9287 6620 : build_modify_expr (location_t location,
9288 : tree lhs, tree /*lhs_origtype*/,
9289 : enum tree_code modifycode,
9290 : location_t /*rhs_location*/, tree rhs,
9291 : tree /*rhs_origtype*/)
9292 : {
9293 6620 : return cp_build_modify_expr (location, lhs, modifycode, rhs,
9294 6620 : tf_warning_or_error);
9295 : }
9296 :
9297 : /* Build an assignment expression of lvalue LHS from value RHS.
9298 : MODIFYCODE is the code for a binary operator that we use
9299 : to combine the old value of LHS with RHS to get the new value.
9300 : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9301 :
9302 : C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9303 :
9304 : tree
9305 14216281 : cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9306 : tree rhs, tsubst_flags_t complain)
9307 : {
9308 14216281 : lhs = mark_lvalue_use_nonread (lhs);
9309 :
9310 14216281 : tree result = NULL_TREE;
9311 14216281 : tree newrhs = rhs;
9312 14216281 : tree lhstype = TREE_TYPE (lhs);
9313 14216281 : tree olhs = lhs;
9314 14216281 : tree olhstype = lhstype;
9315 14216281 : bool plain_assign = (modifycode == NOP_EXPR);
9316 14216281 : bool compound_side_effects_p = false;
9317 14216281 : tree preeval = NULL_TREE;
9318 :
9319 : /* Avoid duplicate error messages from operands that had errors. */
9320 14216281 : if (error_operand_p (lhs) || error_operand_p (rhs))
9321 24 : return error_mark_node;
9322 :
9323 14216342 : while (TREE_CODE (lhs) == COMPOUND_EXPR)
9324 : {
9325 85 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9326 74 : compound_side_effects_p = true;
9327 85 : lhs = TREE_OPERAND (lhs, 1);
9328 : }
9329 :
9330 : /* Handle control structure constructs used as "lvalues". Note that we
9331 : leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9332 14216257 : switch (TREE_CODE (lhs))
9333 : {
9334 : /* Handle --foo = 5; as these are valid constructs in C++. */
9335 28 : case PREDECREMENT_EXPR:
9336 28 : case PREINCREMENT_EXPR:
9337 28 : if (compound_side_effects_p)
9338 12 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9339 28 : lhs = genericize_compound_lvalue (lhs);
9340 140 : maybe_add_compound:
9341 : /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9342 : and looked through the COMPOUND_EXPRs, readd them now around
9343 : the resulting lhs. */
9344 140 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9345 : {
9346 12 : lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9347 12 : tree *ptr = &TREE_OPERAND (lhs, 1);
9348 12 : for (olhs = TREE_OPERAND (olhs, 1);
9349 16 : TREE_CODE (olhs) == COMPOUND_EXPR;
9350 4 : olhs = TREE_OPERAND (olhs, 1))
9351 : {
9352 4 : *ptr = build2 (COMPOUND_EXPR, lhstype,
9353 4 : TREE_OPERAND (olhs, 0), *ptr);
9354 4 : ptr = &TREE_OPERAND (*ptr, 1);
9355 : }
9356 : }
9357 : break;
9358 :
9359 112 : case MODIFY_EXPR:
9360 112 : if (compound_side_effects_p)
9361 0 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9362 112 : lhs = genericize_compound_lvalue (lhs);
9363 112 : goto maybe_add_compound;
9364 :
9365 0 : case MIN_EXPR:
9366 0 : case MAX_EXPR:
9367 : /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9368 : when neither operand has side-effects. */
9369 0 : if (!lvalue_or_else (lhs, lv_assign, complain))
9370 0 : return error_mark_node;
9371 :
9372 0 : gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9373 : && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9374 :
9375 0 : lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9376 0 : build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9377 : boolean_type_node,
9378 0 : TREE_OPERAND (lhs, 0),
9379 0 : TREE_OPERAND (lhs, 1)),
9380 0 : TREE_OPERAND (lhs, 0),
9381 0 : TREE_OPERAND (lhs, 1));
9382 243 : gcc_fallthrough ();
9383 :
9384 : /* Handle (a ? b : c) used as an "lvalue". */
9385 243 : case COND_EXPR:
9386 243 : {
9387 : /* Produce (a ? (b = rhs) : (c = rhs))
9388 : except that the RHS goes through a save-expr
9389 : so the code to compute it is only emitted once. */
9390 243 : if (VOID_TYPE_P (TREE_TYPE (rhs)))
9391 : {
9392 24 : if (complain & tf_error)
9393 32 : error_at (cp_expr_loc_or_loc (rhs, loc),
9394 : "void value not ignored as it ought to be");
9395 24 : return error_mark_node;
9396 : }
9397 :
9398 219 : rhs = stabilize_expr (rhs, &preeval);
9399 :
9400 : /* Check this here to avoid odd errors when trying to convert
9401 : a throw to the type of the COND_EXPR. */
9402 219 : if (!lvalue_or_else (lhs, lv_assign, complain))
9403 8 : return error_mark_node;
9404 :
9405 211 : tree op1 = TREE_OPERAND (lhs, 1);
9406 211 : if (TREE_CODE (op1) != THROW_EXPR)
9407 203 : op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
9408 : /* When sanitizing undefined behavior, even when rhs doesn't need
9409 : stabilization at this point, the sanitization might add extra
9410 : SAVE_EXPRs in there and so make sure there is no tree sharing
9411 : in the rhs, otherwise those SAVE_EXPRs will have initialization
9412 : only in one of the two branches. */
9413 211 : if (sanitize_flags_p (SANITIZE_UNDEFINED
9414 : | SANITIZE_UNDEFINED_NONDEFAULT))
9415 3 : rhs = unshare_expr (rhs);
9416 211 : tree op2 = TREE_OPERAND (lhs, 2);
9417 211 : if (TREE_CODE (op2) != THROW_EXPR)
9418 203 : op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9419 211 : tree cond = build_conditional_expr (input_location,
9420 211 : TREE_OPERAND (lhs, 0), op1, op2,
9421 : complain);
9422 :
9423 211 : if (cond == error_mark_node)
9424 : return cond;
9425 : /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9426 : and looked through the COMPOUND_EXPRs, readd them now around
9427 : the resulting cond before adding the preevaluated rhs. */
9428 211 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9429 : {
9430 20 : cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9431 20 : TREE_OPERAND (olhs, 0), cond);
9432 20 : tree *ptr = &TREE_OPERAND (cond, 1);
9433 20 : for (olhs = TREE_OPERAND (olhs, 1);
9434 24 : TREE_CODE (olhs) == COMPOUND_EXPR;
9435 4 : olhs = TREE_OPERAND (olhs, 1))
9436 : {
9437 4 : *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9438 4 : TREE_OPERAND (olhs, 0), *ptr);
9439 4 : ptr = &TREE_OPERAND (*ptr, 1);
9440 : }
9441 : }
9442 : /* Make sure the code to compute the rhs comes out
9443 : before the split. */
9444 211 : result = cond;
9445 211 : goto ret;
9446 : }
9447 :
9448 : default:
9449 : lhs = olhs;
9450 : break;
9451 : }
9452 :
9453 14216014 : if (modifycode == INIT_EXPR)
9454 : {
9455 2033604 : if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9456 : /* Do the default thing. */;
9457 1922408 : else if (TREE_CODE (rhs) == CONSTRUCTOR)
9458 : {
9459 : /* Compound literal. */
9460 69 : if (! same_type_p (TREE_TYPE (rhs), lhstype))
9461 : /* Call convert to generate an error; see PR 11063. */
9462 0 : rhs = convert (lhstype, rhs);
9463 69 : result = cp_build_init_expr (lhs, rhs);
9464 69 : TREE_SIDE_EFFECTS (result) = 1;
9465 69 : goto ret;
9466 : }
9467 1955554 : else if (! MAYBE_CLASS_TYPE_P (lhstype))
9468 : /* Do the default thing. */;
9469 : else
9470 : {
9471 32768 : releasing_vec rhs_vec = make_tree_vector_single (rhs);
9472 32768 : result = build_special_member_call (lhs, complete_ctor_identifier,
9473 : &rhs_vec, lhstype, LOOKUP_NORMAL,
9474 : complain);
9475 32768 : if (result == NULL_TREE)
9476 0 : return error_mark_node;
9477 32768 : goto ret;
9478 32768 : }
9479 : }
9480 : else
9481 : {
9482 12182410 : lhs = require_complete_type (lhs, complain);
9483 12182410 : if (lhs == error_mark_node)
9484 : return error_mark_node;
9485 :
9486 12182368 : if (modifycode == NOP_EXPR)
9487 : {
9488 9485788 : maybe_warn_self_move (loc, lhs, rhs);
9489 :
9490 9485788 : if (c_dialect_objc ())
9491 : {
9492 0 : result = objc_maybe_build_modify_expr (lhs, rhs);
9493 0 : if (result)
9494 0 : goto ret;
9495 : }
9496 :
9497 : /* `operator=' is not an inheritable operator. */
9498 9940336 : if (! MAYBE_CLASS_TYPE_P (lhstype))
9499 : /* Do the default thing. */;
9500 : else
9501 : {
9502 454138 : result = build_new_op (input_location, MODIFY_EXPR,
9503 : LOOKUP_NORMAL, lhs, rhs,
9504 : make_node (NOP_EXPR), NULL_TREE,
9505 : /*overload=*/NULL, complain);
9506 454138 : if (result == NULL_TREE)
9507 0 : return error_mark_node;
9508 454138 : goto ret;
9509 : }
9510 : lhstype = olhstype;
9511 : }
9512 : else
9513 : {
9514 2696580 : tree init = NULL_TREE;
9515 :
9516 : /* A binary op has been requested. Combine the old LHS
9517 : value with the RHS producing the value we should actually
9518 : store into the LHS. */
9519 2696580 : gcc_assert (!((TYPE_REF_P (lhstype)
9520 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9521 : || MAYBE_CLASS_TYPE_P (lhstype)));
9522 :
9523 : /* Preevaluate the RHS to make sure its evaluation is complete
9524 : before the lvalue-to-rvalue conversion of the LHS:
9525 :
9526 : [expr.ass] With respect to an indeterminately-sequenced
9527 : function call, the operation of a compound assignment is a
9528 : single evaluation. [ Note: Therefore, a function call shall
9529 : not intervene between the lvalue-to-rvalue conversion and the
9530 : side effect associated with any single compound assignment
9531 : operator. -- end note ] */
9532 2696580 : lhs = cp_stabilize_reference (lhs);
9533 2696580 : rhs = decay_conversion (rhs, complain);
9534 2696580 : if (rhs == error_mark_node)
9535 164 : return error_mark_node;
9536 2696576 : rhs = stabilize_expr (rhs, &init);
9537 2696576 : newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9538 2696576 : if (newrhs == error_mark_node)
9539 : {
9540 160 : if (complain & tf_error)
9541 32 : inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9542 32 : modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9543 160 : return error_mark_node;
9544 : }
9545 :
9546 2696416 : if (init)
9547 139956 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9548 :
9549 : /* Now it looks like a plain assignment. */
9550 2696416 : modifycode = NOP_EXPR;
9551 2696416 : if (c_dialect_objc ())
9552 : {
9553 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
9554 0 : if (result)
9555 0 : goto ret;
9556 : }
9557 : }
9558 11728066 : gcc_assert (!TYPE_REF_P (lhstype));
9559 11728066 : gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9560 : }
9561 :
9562 : /* The left-hand side must be an lvalue. */
9563 13728833 : if (!lvalue_or_else (lhs, lv_assign, complain))
9564 176 : return error_mark_node;
9565 :
9566 : /* Warn about modifying something that is `const'. Don't warn if
9567 : this is initialization. */
9568 13728657 : if (modifycode != INIT_EXPR
9569 13728657 : && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9570 : /* Functions are not modifiable, even though they are
9571 : lvalues. */
9572 11712288 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9573 : /* If it's an aggregate and any field is const, then it is
9574 : effectively const. */
9575 11712265 : || (CLASS_TYPE_P (lhstype)
9576 0 : && C_TYPE_FIELDS_READONLY (lhstype))))
9577 : {
9578 15625 : if (complain & tf_error)
9579 79 : cxx_readonly_error (loc, lhs, lv_assign);
9580 15625 : return error_mark_node;
9581 : }
9582 :
9583 : /* If storing into a structure or union member, it may have been given a
9584 : lowered bitfield type. We need to convert to the declared type first,
9585 : so retrieve it now. */
9586 :
9587 13713032 : olhstype = unlowered_expr_type (lhs);
9588 :
9589 : /* Convert new value to destination type. */
9590 :
9591 13713032 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
9592 : {
9593 186 : int from_array;
9594 :
9595 186 : if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9596 : {
9597 12 : if (modifycode != INIT_EXPR)
9598 : {
9599 12 : if (complain & tf_error)
9600 12 : error_at (loc,
9601 : "assigning to an array from an initializer list");
9602 12 : return error_mark_node;
9603 : }
9604 0 : if (check_array_initializer (lhs, lhstype, newrhs))
9605 0 : return error_mark_node;
9606 0 : newrhs = digest_init (lhstype, newrhs, complain);
9607 0 : if (newrhs == error_mark_node)
9608 : return error_mark_node;
9609 : }
9610 :
9611 : /* C++11 8.5/17: "If the destination type is an array of characters,
9612 : an array of char16_t, an array of char32_t, or an array of wchar_t,
9613 : and the initializer is a string literal...". */
9614 174 : else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9615 : == STRING_CST)
9616 44 : && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9617 218 : && modifycode == INIT_EXPR)
9618 : {
9619 24 : newrhs = digest_init (lhstype, newrhs, complain);
9620 24 : if (newrhs == error_mark_node)
9621 : return error_mark_node;
9622 : }
9623 :
9624 150 : else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9625 : TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9626 : {
9627 26 : if (complain & tf_error)
9628 21 : error_at (loc, "incompatible types in assignment of %qT to %qT",
9629 21 : TREE_TYPE (rhs), lhstype);
9630 26 : return error_mark_node;
9631 : }
9632 :
9633 : /* Allow array assignment in compiler-generated code. */
9634 124 : else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
9635 : /* OK, used by coroutines (co-await-initlist1.C). */;
9636 123 : else if (!current_function_decl
9637 123 : || !DECL_DEFAULTED_FN (current_function_decl))
9638 : {
9639 : /* This routine is used for both initialization and assignment.
9640 : Make sure the diagnostic message differentiates the context. */
9641 69 : if (complain & tf_error)
9642 : {
9643 39 : if (modifycode == INIT_EXPR)
9644 11 : error_at (loc, "array used as initializer");
9645 : else
9646 28 : error_at (loc, "invalid array assignment");
9647 : }
9648 69 : return error_mark_node;
9649 : }
9650 :
9651 79 : from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9652 79 : ? 1 + (modifycode != INIT_EXPR): 0;
9653 79 : result = build_vec_init (lhs, NULL_TREE, newrhs,
9654 : /*explicit_value_init_p=*/false,
9655 : from_array, complain);
9656 79 : goto ret;
9657 : }
9658 :
9659 13712846 : if (modifycode == INIT_EXPR)
9660 : /* Calls with INIT_EXPR are all direct-initialization, so don't set
9661 : LOOKUP_ONLYCONVERTING. */
9662 2000708 : newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9663 : ICR_INIT, NULL_TREE, 0,
9664 : complain | tf_no_cleanup);
9665 : else
9666 11712138 : newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9667 : NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9668 :
9669 13712846 : if (!same_type_p (lhstype, olhstype))
9670 55704 : newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9671 :
9672 13712846 : if (modifycode != INIT_EXPR)
9673 : {
9674 11712138 : if (TREE_CODE (newrhs) == CALL_EXPR
9675 11712138 : && TYPE_NEEDS_CONSTRUCTING (lhstype))
9676 0 : newrhs = build_cplus_new (lhstype, newrhs, complain);
9677 :
9678 : /* Can't initialize directly from a TARGET_EXPR, since that would
9679 : cause the lhs to be constructed twice, and possibly result in
9680 : accidental self-initialization. So we force the TARGET_EXPR to be
9681 : expanded without a target. */
9682 11712138 : if (TREE_CODE (newrhs) == TARGET_EXPR)
9683 92 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9684 92 : TREE_OPERAND (newrhs, 0));
9685 : }
9686 :
9687 13712846 : if (newrhs == error_mark_node)
9688 : return error_mark_node;
9689 :
9690 13712166 : if (c_dialect_objc () && flag_objc_gc)
9691 : {
9692 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
9693 :
9694 0 : if (result)
9695 0 : goto ret;
9696 : }
9697 :
9698 15712830 : result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
9699 : lhstype, lhs, newrhs);
9700 13712166 : if (modifycode == INIT_EXPR)
9701 2000664 : set_target_expr_eliding (newrhs);
9702 :
9703 13712166 : TREE_SIDE_EFFECTS (result) = 1;
9704 13712166 : if (!plain_assign)
9705 4697043 : suppress_warning (result, OPT_Wparentheses);
9706 :
9707 9015123 : ret:
9708 14199431 : if (preeval)
9709 39 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
9710 : return result;
9711 : }
9712 :
9713 : cp_expr
9714 34838818 : build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9715 : tree rhs, tree lookups, tsubst_flags_t complain)
9716 : {
9717 34838818 : tree orig_lhs = lhs;
9718 34838818 : tree orig_rhs = rhs;
9719 34838818 : tree overload = NULL_TREE;
9720 :
9721 34838818 : if (lhs == error_mark_node || rhs == error_mark_node)
9722 2612 : return cp_expr (error_mark_node, loc);
9723 :
9724 34836206 : if (processing_template_decl)
9725 : {
9726 24059686 : if (modifycode == NOP_EXPR
9727 4933453 : || type_dependent_expression_p (lhs)
9728 25387837 : || type_dependent_expression_p (rhs))
9729 : {
9730 22939920 : tree op = build_min_nt_loc (loc, modifycode, NULL_TREE, NULL_TREE);
9731 22939920 : tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
9732 22939920 : if (modifycode != NOP_EXPR)
9733 3813687 : TREE_TYPE (rval)
9734 7627374 : = build_dependent_operator_type (lookups, modifycode, true);
9735 22939920 : return rval;
9736 : }
9737 :
9738 1119766 : lhs = build_non_dependent_expr (lhs);
9739 1119766 : rhs = build_non_dependent_expr (rhs);
9740 : }
9741 :
9742 11896286 : if (modifycode != NOP_EXPR)
9743 : {
9744 3589734 : tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
9745 3589734 : tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
9746 3589734 : lhs, rhs, op, lookups, &overload, complain);
9747 3589734 : if (rval)
9748 : {
9749 3589734 : if (rval == error_mark_node)
9750 558 : return rval;
9751 3589176 : suppress_warning (rval /* What warning? */);
9752 3589176 : if (processing_template_decl)
9753 : {
9754 1119766 : if (overload != NULL_TREE)
9755 693060 : return (build_min_non_dep_op_overload
9756 693060 : (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
9757 :
9758 426706 : return (build_min_non_dep
9759 426706 : (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
9760 : }
9761 2469410 : return rval;
9762 : }
9763 : }
9764 8306552 : return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
9765 : }
9766 :
9767 : /* Helper function for get_delta_difference which assumes FROM is a base
9768 : class of TO. Returns a delta for the conversion of pointer-to-member
9769 : of FROM to pointer-to-member of TO. If the conversion is invalid and
9770 : tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9771 : returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9772 : If C_CAST_P is true, this conversion is taking place as part of a
9773 : C-style cast. */
9774 :
9775 : static tree
9776 21489 : get_delta_difference_1 (tree from, tree to, bool c_cast_p,
9777 : tsubst_flags_t complain)
9778 : {
9779 21489 : tree binfo;
9780 21489 : base_kind kind;
9781 :
9782 42650 : binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
9783 : &kind, complain);
9784 :
9785 21489 : if (binfo == error_mark_node)
9786 : {
9787 68 : if (!(complain & tf_error))
9788 : return error_mark_node;
9789 :
9790 68 : inform (input_location, " in pointer to member function conversion");
9791 68 : return size_zero_node;
9792 : }
9793 21421 : else if (binfo)
9794 : {
9795 21233 : if (kind != bk_via_virtual)
9796 21111 : return BINFO_OFFSET (binfo);
9797 : else
9798 : /* FROM is a virtual base class of TO. Issue an error or warning
9799 : depending on whether or not this is a reinterpret cast. */
9800 : {
9801 122 : if (!(complain & tf_error))
9802 : return error_mark_node;
9803 :
9804 104 : error ("pointer to member conversion via virtual base %qT",
9805 104 : BINFO_TYPE (binfo_from_vbase (binfo)));
9806 :
9807 104 : return size_zero_node;
9808 : }
9809 : }
9810 : else
9811 : return NULL_TREE;
9812 : }
9813 :
9814 : /* Get difference in deltas for different pointer to member function
9815 : types. If the conversion is invalid and tf_error is not set in
9816 : COMPLAIN, returns error_mark_node, otherwise returns an integer
9817 : constant of type PTRDIFF_TYPE_NODE and its value is zero if the
9818 : conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
9819 : conversions as well. If C_CAST_P is true this conversion is taking
9820 : place as part of a C-style cast.
9821 :
9822 : Note that the naming of FROM and TO is kind of backwards; the return
9823 : value is what we add to a TO in order to get a FROM. They are named
9824 : this way because we call this function to find out how to convert from
9825 : a pointer to member of FROM to a pointer to member of TO. */
9826 :
9827 : static tree
9828 67551 : get_delta_difference (tree from, tree to,
9829 : bool allow_inverse_p,
9830 : bool c_cast_p, tsubst_flags_t complain)
9831 : {
9832 67551 : tree result;
9833 :
9834 67551 : if (same_type_ignoring_top_level_qualifiers_p (from, to))
9835 : /* Pointer to member of incomplete class is permitted*/
9836 46250 : result = size_zero_node;
9837 : else
9838 21301 : result = get_delta_difference_1 (from, to, c_cast_p, complain);
9839 :
9840 67551 : if (result == error_mark_node)
9841 : return error_mark_node;
9842 :
9843 67533 : if (!result)
9844 : {
9845 188 : if (!allow_inverse_p)
9846 : {
9847 0 : if (!(complain & tf_error))
9848 : return error_mark_node;
9849 :
9850 0 : error_not_base_type (from, to);
9851 0 : inform (input_location, " in pointer to member conversion");
9852 0 : result = size_zero_node;
9853 : }
9854 : else
9855 : {
9856 188 : result = get_delta_difference_1 (to, from, c_cast_p, complain);
9857 :
9858 188 : if (result == error_mark_node)
9859 : return error_mark_node;
9860 :
9861 188 : if (result)
9862 188 : result = size_diffop_loc (input_location,
9863 : size_zero_node, result);
9864 : else
9865 : {
9866 0 : if (!(complain & tf_error))
9867 : return error_mark_node;
9868 :
9869 0 : error_not_base_type (from, to);
9870 0 : inform (input_location, " in pointer to member conversion");
9871 0 : result = size_zero_node;
9872 : }
9873 : }
9874 : }
9875 :
9876 67533 : return convert_to_integer (ptrdiff_type_node, result);
9877 : }
9878 :
9879 : /* Return a constructor for the pointer-to-member-function TYPE using
9880 : the other components as specified. */
9881 :
9882 : tree
9883 46672 : build_ptrmemfunc1 (tree type, tree delta, tree pfn)
9884 : {
9885 46672 : tree u = NULL_TREE;
9886 46672 : tree delta_field;
9887 46672 : tree pfn_field;
9888 46672 : vec<constructor_elt, va_gc> *v;
9889 :
9890 : /* Pull the FIELD_DECLs out of the type. */
9891 46672 : pfn_field = TYPE_FIELDS (type);
9892 46672 : delta_field = DECL_CHAIN (pfn_field);
9893 :
9894 : /* Make sure DELTA has the type we want. */
9895 46672 : delta = convert_and_check (input_location, delta_type_node, delta);
9896 :
9897 : /* Convert to the correct target type if necessary. */
9898 46672 : pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
9899 :
9900 : /* Finish creating the initializer. */
9901 46672 : vec_alloc (v, 2);
9902 46672 : CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
9903 46672 : CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
9904 46672 : u = build_constructor (type, v);
9905 46672 : TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
9906 46672 : TREE_STATIC (u) = (TREE_CONSTANT (u)
9907 46550 : && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
9908 : != NULL_TREE)
9909 93222 : && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
9910 : != NULL_TREE));
9911 46672 : return u;
9912 : }
9913 :
9914 : /* Build a constructor for a pointer to member function. It can be
9915 : used to initialize global variables, local variable, or used
9916 : as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
9917 : want to be.
9918 :
9919 : If FORCE is nonzero, then force this conversion, even if
9920 : we would rather not do it. Usually set when using an explicit
9921 : cast. A C-style cast is being processed iff C_CAST_P is true.
9922 :
9923 : Return error_mark_node, if something goes wrong. */
9924 :
9925 : tree
9926 22820 : build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
9927 : tsubst_flags_t complain)
9928 : {
9929 22820 : tree fn;
9930 22820 : tree pfn_type;
9931 22820 : tree to_type;
9932 :
9933 22820 : if (error_operand_p (pfn))
9934 0 : return error_mark_node;
9935 :
9936 22820 : pfn_type = TREE_TYPE (pfn);
9937 22820 : to_type = build_ptrmemfunc_type (type);
9938 :
9939 : /* Handle multiple conversions of pointer to member functions. */
9940 22820 : if (TYPE_PTRMEMFUNC_P (pfn_type))
9941 : {
9942 20981 : tree delta = NULL_TREE;
9943 20981 : tree npfn = NULL_TREE;
9944 20981 : tree n;
9945 :
9946 20981 : if (!force
9947 20981 : && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
9948 : LOOKUP_NORMAL, complain))
9949 : {
9950 0 : if (complain & tf_error)
9951 0 : error ("invalid conversion to type %qT from type %qT",
9952 : to_type, pfn_type);
9953 : else
9954 0 : return error_mark_node;
9955 : }
9956 :
9957 20981 : n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
9958 20981 : TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
9959 : force,
9960 : c_cast_p, complain);
9961 20981 : if (n == error_mark_node)
9962 : return error_mark_node;
9963 :
9964 20970 : STRIP_ANY_LOCATION_WRAPPER (pfn);
9965 :
9966 : /* We don't have to do any conversion to convert a
9967 : pointer-to-member to its own type. But, we don't want to
9968 : just return a PTRMEM_CST if there's an explicit cast; that
9969 : cast should make the expression an invalid template argument. */
9970 20970 : if (TREE_CODE (pfn) != PTRMEM_CST
9971 20970 : && same_type_p (to_type, pfn_type))
9972 : return pfn;
9973 :
9974 20970 : if (TREE_SIDE_EFFECTS (pfn))
9975 9 : pfn = save_expr (pfn);
9976 :
9977 : /* Obtain the function pointer and the current DELTA. */
9978 20970 : if (TREE_CODE (pfn) == PTRMEM_CST)
9979 20861 : expand_ptrmemfunc_cst (pfn, &delta, &npfn);
9980 : else
9981 : {
9982 109 : npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
9983 109 : delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
9984 : }
9985 :
9986 : /* Just adjust the DELTA field. */
9987 20970 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
9988 : (TREE_TYPE (delta), ptrdiff_type_node));
9989 20970 : if (!integer_zerop (n))
9990 : {
9991 64 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
9992 : n = cp_build_binary_op (input_location,
9993 : LSHIFT_EXPR, n, integer_one_node,
9994 : complain);
9995 64 : delta = cp_build_binary_op (input_location,
9996 : PLUS_EXPR, delta, n, complain);
9997 : }
9998 20970 : return build_ptrmemfunc1 (to_type, delta, npfn);
9999 : }
10000 :
10001 : /* Handle null pointer to member function conversions. */
10002 1839 : if (null_ptr_cst_p (pfn))
10003 : {
10004 699 : pfn = cp_build_c_cast (input_location,
10005 699 : TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10006 : pfn, complain);
10007 699 : return build_ptrmemfunc1 (to_type,
10008 : integer_zero_node,
10009 699 : pfn);
10010 : }
10011 :
10012 1140 : if (type_unknown_p (pfn))
10013 0 : return instantiate_type (type, pfn, complain);
10014 :
10015 1140 : fn = TREE_OPERAND (pfn, 0);
10016 1140 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10017 : /* In a template, we will have preserved the
10018 : OFFSET_REF. */
10019 : || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10020 1140 : return make_ptrmem_cst (to_type, fn);
10021 : }
10022 :
10023 : /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10024 : given by CST.
10025 :
10026 : ??? There is no consistency as to the types returned for the above
10027 : values. Some code acts as if it were a sizetype and some as if it were
10028 : integer_type_node. */
10029 :
10030 : void
10031 46162 : expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10032 : {
10033 46162 : tree type = TREE_TYPE (cst);
10034 46162 : tree fn = PTRMEM_CST_MEMBER (cst);
10035 46162 : tree ptr_class, fn_class;
10036 :
10037 46162 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10038 :
10039 : /* The class that the function belongs to. */
10040 46162 : fn_class = DECL_CONTEXT (fn);
10041 :
10042 : /* The class that we're creating a pointer to member of. */
10043 46162 : ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10044 :
10045 : /* First, calculate the adjustment to the function's class. */
10046 46162 : *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10047 : /*c_cast_p=*/0, tf_warning_or_error);
10048 :
10049 46162 : if (!DECL_VIRTUAL_P (fn))
10050 : {
10051 44989 : tree t = build_addr_func (fn, tf_warning_or_error);
10052 44989 : if (TREE_CODE (t) == ADDR_EXPR)
10053 44989 : SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10054 44989 : *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10055 : }
10056 : else
10057 : {
10058 : /* If we're dealing with a virtual function, we have to adjust 'this'
10059 : again, to point to the base which provides the vtable entry for
10060 : fn; the call will do the opposite adjustment. */
10061 1173 : tree orig_class = DECL_CONTEXT (fn);
10062 1173 : tree binfo = binfo_or_else (orig_class, fn_class);
10063 1173 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10064 : *delta, BINFO_OFFSET (binfo));
10065 :
10066 : /* We set PFN to the vtable offset at which the function can be
10067 : found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10068 : case delta is shifted left, and then incremented). */
10069 1173 : *pfn = DECL_VINDEX (fn);
10070 1173 : *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10071 : TYPE_SIZE_UNIT (vtable_entry_type));
10072 :
10073 1173 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10074 : {
10075 1173 : case ptrmemfunc_vbit_in_pfn:
10076 1173 : *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10077 : integer_one_node);
10078 1173 : break;
10079 :
10080 : case ptrmemfunc_vbit_in_delta:
10081 : *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10082 : *delta, integer_one_node);
10083 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10084 : *delta, integer_one_node);
10085 : break;
10086 :
10087 : default:
10088 : gcc_unreachable ();
10089 : }
10090 :
10091 1173 : *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10092 : }
10093 46162 : }
10094 :
10095 : /* Return an expression for PFN from the pointer-to-member function
10096 : given by T. */
10097 :
10098 : static tree
10099 63466 : pfn_from_ptrmemfunc (tree t)
10100 : {
10101 63466 : if (TREE_CODE (t) == PTRMEM_CST)
10102 : {
10103 149 : tree delta;
10104 149 : tree pfn;
10105 :
10106 149 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10107 149 : if (pfn)
10108 149 : return pfn;
10109 : }
10110 :
10111 63317 : return build_ptrmemfunc_access_expr (t, pfn_identifier);
10112 : }
10113 :
10114 : /* Return an expression for DELTA from the pointer-to-member function
10115 : given by T. */
10116 :
10117 : static tree
10118 63466 : delta_from_ptrmemfunc (tree t)
10119 : {
10120 63466 : if (TREE_CODE (t) == PTRMEM_CST)
10121 : {
10122 149 : tree delta;
10123 149 : tree pfn;
10124 :
10125 149 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10126 149 : if (delta)
10127 149 : return delta;
10128 : }
10129 :
10130 63317 : return build_ptrmemfunc_access_expr (t, delta_identifier);
10131 : }
10132 :
10133 : /* Convert value RHS to type TYPE as preparation for an assignment to
10134 : an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10135 : implicit conversion is. If FNDECL is non-NULL, we are doing the
10136 : conversion in order to pass the PARMNUMth argument of FNDECL.
10137 : If FNDECL is NULL, we are doing the conversion in function pointer
10138 : argument passing, conversion in initialization, etc. */
10139 :
10140 : static tree
10141 58652452 : convert_for_assignment (tree type, tree rhs,
10142 : impl_conv_rhs errtype, tree fndecl, int parmnum,
10143 : tsubst_flags_t complain, int flags)
10144 : {
10145 58652463 : tree rhstype;
10146 58652463 : enum tree_code coder;
10147 :
10148 58652463 : location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10149 58652463 : bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10150 : /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10151 : but preserve location wrappers. */
10152 58652463 : if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10153 58652463 : && !location_wrapper_p (rhs))
10154 16701 : rhs = TREE_OPERAND (rhs, 0);
10155 :
10156 : /* Handle [dcl.init.list] direct-list-initialization from
10157 : single element of enumeration with a fixed underlying type. */
10158 58652463 : if (is_direct_enum_init (type, rhs))
10159 : {
10160 38 : tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10161 38 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10162 : {
10163 20 : warning_sentinel w (warn_useless_cast);
10164 20 : warning_sentinel w2 (warn_ignored_qualifiers);
10165 20 : rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10166 20 : }
10167 : else
10168 18 : rhs = error_mark_node;
10169 : }
10170 :
10171 58652463 : rhstype = TREE_TYPE (rhs);
10172 58652463 : coder = TREE_CODE (rhstype);
10173 :
10174 1084261 : if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10175 59736700 : && vector_types_convertible_p (type, rhstype, true))
10176 : {
10177 1084225 : rhs = mark_rvalue_use (rhs);
10178 1084225 : return convert (type, rhs);
10179 : }
10180 :
10181 57568238 : if (rhs == error_mark_node || rhstype == error_mark_node)
10182 : return error_mark_node;
10183 57568220 : if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10184 : return error_mark_node;
10185 :
10186 : /* The RHS of an assignment cannot have void type. */
10187 57568220 : if (coder == VOID_TYPE)
10188 : {
10189 47 : if (complain & tf_error)
10190 31 : error_at (rhs_loc, "void value not ignored as it ought to be");
10191 47 : return error_mark_node;
10192 : }
10193 :
10194 57568173 : if (c_dialect_objc ())
10195 : {
10196 0 : int parmno;
10197 0 : tree selector;
10198 0 : tree rname = fndecl;
10199 :
10200 0 : switch (errtype)
10201 : {
10202 : case ICR_ASSIGN:
10203 : parmno = -1;
10204 : break;
10205 0 : case ICR_INIT:
10206 0 : parmno = -2;
10207 0 : break;
10208 0 : default:
10209 0 : selector = objc_message_selector ();
10210 0 : parmno = parmnum;
10211 0 : if (selector && parmno > 1)
10212 : {
10213 0 : rname = selector;
10214 0 : parmno -= 1;
10215 : }
10216 : }
10217 :
10218 0 : if (objc_compare_types (type, rhstype, parmno, rname))
10219 : {
10220 0 : rhs = mark_rvalue_use (rhs);
10221 0 : return convert (type, rhs);
10222 : }
10223 : }
10224 :
10225 : /* [expr.ass]
10226 :
10227 : The expression is implicitly converted (clause _conv_) to the
10228 : cv-unqualified type of the left operand.
10229 :
10230 : We allow bad conversions here because by the time we get to this point
10231 : we are committed to doing the conversion. If we end up doing a bad
10232 : conversion, convert_like will complain. */
10233 57568173 : if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10234 : {
10235 : /* When -Wno-pmf-conversions is use, we just silently allow
10236 : conversions from pointers-to-members to plain pointers. If
10237 : the conversion doesn't work, cp_convert will complain. */
10238 1419 : if (!warn_pmf2ptr
10239 12 : && TYPE_PTR_P (type)
10240 1431 : && TYPE_PTRMEMFUNC_P (rhstype))
10241 12 : rhs = cp_convert (strip_top_quals (type), rhs, complain);
10242 : else
10243 : {
10244 1407 : if (complain & tf_error)
10245 : {
10246 : /* If the right-hand side has unknown type, then it is an
10247 : overloaded function. Call instantiate_type to get error
10248 : messages. */
10249 1279 : if (rhstype == unknown_type_node)
10250 : {
10251 171 : tree r = instantiate_type (type, rhs, tf_warning_or_error);
10252 : /* -fpermissive might allow this; recurse. */
10253 171 : if (!seen_error ())
10254 : return convert_for_assignment (type, r, errtype, fndecl,
10255 : parmnum, complain, flags);
10256 : }
10257 1108 : else if (fndecl)
10258 96 : complain_about_bad_argument (rhs_loc,
10259 : rhstype, type,
10260 : fndecl, parmnum);
10261 : else
10262 : {
10263 1012 : range_label_for_type_mismatch label (rhstype, type);
10264 1012 : gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL);
10265 1012 : switch (errtype)
10266 : {
10267 0 : case ICR_DEFAULT_ARGUMENT:
10268 0 : error_at (&richloc,
10269 : "cannot convert %qH to %qI in default argument",
10270 : rhstype, type);
10271 0 : break;
10272 8 : case ICR_ARGPASS:
10273 8 : error_at (&richloc,
10274 : "cannot convert %qH to %qI in argument passing",
10275 : rhstype, type);
10276 8 : break;
10277 0 : case ICR_CONVERTING:
10278 0 : error_at (&richloc, "cannot convert %qH to %qI",
10279 : rhstype, type);
10280 0 : break;
10281 463 : case ICR_INIT:
10282 463 : error_at (&richloc,
10283 : "cannot convert %qH to %qI in initialization",
10284 : rhstype, type);
10285 463 : break;
10286 16 : case ICR_RETURN:
10287 16 : error_at (&richloc, "cannot convert %qH to %qI in return",
10288 : rhstype, type);
10289 16 : break;
10290 525 : case ICR_ASSIGN:
10291 525 : error_at (&richloc,
10292 : "cannot convert %qH to %qI in assignment",
10293 : rhstype, type);
10294 525 : break;
10295 0 : default:
10296 0 : gcc_unreachable();
10297 : }
10298 1012 : }
10299 1268 : if (TYPE_PTR_P (rhstype)
10300 512 : && TYPE_PTR_P (type)
10301 496 : && CLASS_TYPE_P (TREE_TYPE (rhstype))
10302 370 : && CLASS_TYPE_P (TREE_TYPE (type))
10303 1350 : && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10304 4 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10305 : (TREE_TYPE (rhstype))),
10306 4 : "class type %qT is incomplete", TREE_TYPE (rhstype));
10307 : }
10308 1396 : return error_mark_node;
10309 : }
10310 : }
10311 57566766 : if (warn_suggest_attribute_format)
10312 : {
10313 2143 : const enum tree_code codel = TREE_CODE (type);
10314 2143 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10315 576 : && coder == codel
10316 488 : && check_missing_format_attribute (type, rhstype)
10317 2167 : && (complain & tf_warning))
10318 24 : switch (errtype)
10319 : {
10320 0 : case ICR_ARGPASS:
10321 0 : case ICR_DEFAULT_ARGUMENT:
10322 0 : if (fndecl)
10323 0 : warning (OPT_Wsuggest_attribute_format,
10324 : "parameter %qP of %qD might be a candidate "
10325 : "for a format attribute", parmnum, fndecl);
10326 : else
10327 0 : warning (OPT_Wsuggest_attribute_format,
10328 : "parameter might be a candidate "
10329 : "for a format attribute");
10330 : break;
10331 0 : case ICR_CONVERTING:
10332 0 : warning (OPT_Wsuggest_attribute_format,
10333 : "target of conversion might be a candidate "
10334 : "for a format attribute");
10335 0 : break;
10336 8 : case ICR_INIT:
10337 8 : warning (OPT_Wsuggest_attribute_format,
10338 : "target of initialization might be a candidate "
10339 : "for a format attribute");
10340 8 : break;
10341 8 : case ICR_RETURN:
10342 8 : warning (OPT_Wsuggest_attribute_format,
10343 : "return type might be a candidate "
10344 : "for a format attribute");
10345 8 : break;
10346 8 : case ICR_ASSIGN:
10347 8 : warning (OPT_Wsuggest_attribute_format,
10348 : "left-hand side of assignment might be a candidate "
10349 : "for a format attribute");
10350 8 : break;
10351 0 : default:
10352 0 : gcc_unreachable();
10353 : }
10354 : }
10355 :
10356 : /* If -Wparentheses, warn about a = b = c when a has type bool and b
10357 : does not. */
10358 57566766 : if (warn_parentheses
10359 1134238 : && TREE_CODE (type) == BOOLEAN_TYPE
10360 149578 : && TREE_CODE (rhs) == MODIFY_EXPR
10361 142 : && !warning_suppressed_p (rhs, OPT_Wparentheses)
10362 74 : && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
10363 48 : && (complain & tf_warning)
10364 57566814 : && warning_at (rhs_loc, OPT_Wparentheses,
10365 : "suggest parentheses around assignment used as "
10366 : "truth value"))
10367 48 : suppress_warning (rhs, OPT_Wparentheses);
10368 :
10369 57566766 : if (complain & tf_warning)
10370 57118397 : warn_for_address_or_pointer_of_packed_member (type, rhs);
10371 :
10372 57566766 : return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10373 57566766 : complain, flags);
10374 : }
10375 :
10376 : /* Convert RHS to be of type TYPE.
10377 : If EXP is nonzero, it is the target of the initialization.
10378 : ERRTYPE indicates what kind of error the implicit conversion is.
10379 :
10380 : Two major differences between the behavior of
10381 : `convert_for_assignment' and `convert_for_initialization'
10382 : are that references are bashed in the former, while
10383 : copied in the latter, and aggregates are assigned in
10384 : the former (operator=) while initialized in the
10385 : latter (X(X&)).
10386 :
10387 : If using constructor make sure no conversion operator exists, if one does
10388 : exist, an ambiguity exists. */
10389 :
10390 : tree
10391 53668162 : convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10392 : impl_conv_rhs errtype, tree fndecl, int parmnum,
10393 : tsubst_flags_t complain)
10394 : {
10395 53668162 : enum tree_code codel = TREE_CODE (type);
10396 53668162 : tree rhstype;
10397 53668162 : enum tree_code coder;
10398 :
10399 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10400 : Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10401 53668162 : if (TREE_CODE (rhs) == NOP_EXPR
10402 2709382 : && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10403 53896729 : && codel != REFERENCE_TYPE)
10404 228567 : rhs = TREE_OPERAND (rhs, 0);
10405 :
10406 53668162 : if (type == error_mark_node
10407 53668128 : || rhs == error_mark_node
10408 107336232 : || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10409 : return error_mark_node;
10410 :
10411 59414457 : if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10412 : ;
10413 47973152 : else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10414 110105 : && TREE_CODE (type) != ARRAY_TYPE
10415 110105 : && (!TYPE_REF_P (type)
10416 748 : || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10417 47863792 : || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10418 3488 : && !TYPE_REFFN_P (type))
10419 95833630 : || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10420 112678 : rhs = decay_conversion (rhs, complain);
10421 :
10422 53668070 : rhstype = TREE_TYPE (rhs);
10423 53668070 : coder = TREE_CODE (rhstype);
10424 :
10425 53668070 : if (coder == ERROR_MARK)
10426 9 : return error_mark_node;
10427 :
10428 : /* We accept references to incomplete types, so we can
10429 : return here before checking if RHS is of complete type. */
10430 :
10431 53668061 : if (codel == REFERENCE_TYPE)
10432 : {
10433 3023057 : auto_diagnostic_group d;
10434 : /* This should eventually happen in convert_arguments. */
10435 3023057 : int savew = 0, savee = 0;
10436 :
10437 3023057 : if (fndecl)
10438 245407 : savew = warningcount + werrorcount, savee = errorcount;
10439 3023057 : rhs = initialize_reference (type, rhs, flags, complain);
10440 :
10441 3023057 : if (fndecl
10442 245407 : && (warningcount + werrorcount > savew || errorcount > savee))
10443 40 : inform (get_fndecl_argument_location (fndecl, parmnum),
10444 : "in passing argument %P of %qD", parmnum, fndecl);
10445 3023057 : return rhs;
10446 3023057 : }
10447 :
10448 50645004 : if (exp != 0)
10449 2000594 : exp = require_complete_type (exp, complain);
10450 50645004 : if (exp == error_mark_node)
10451 : return error_mark_node;
10452 :
10453 50645004 : type = complete_type (type);
10454 :
10455 50645004 : if (DIRECT_INIT_EXPR_P (type, rhs))
10456 : /* Don't try to do copy-initialization if we already have
10457 : direct-initialization. */
10458 : return rhs;
10459 :
10460 50644088 : if (MAYBE_CLASS_TYPE_P (type))
10461 3703774 : return perform_implicit_conversion_flags (type, rhs, complain, flags);
10462 :
10463 46940314 : return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10464 46940314 : complain, flags);
10465 : }
10466 :
10467 : /* If RETVAL is the address of, or a reference to, a local variable or
10468 : temporary give an appropriate warning and return true. */
10469 :
10470 : static bool
10471 21068927 : maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10472 : {
10473 21069048 : tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10474 21069048 : tree whats_returned = fold_for_warn (retval);
10475 21069048 : if (!loc)
10476 21069057 : loc = cp_expr_loc_or_input_loc (retval);
10477 :
10478 25285090 : for (;;)
10479 : {
10480 25285090 : if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10481 279215 : whats_returned = TREE_OPERAND (whats_returned, 1);
10482 25005875 : else if (CONVERT_EXPR_P (whats_returned)
10483 21079303 : || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10484 3936827 : whats_returned = TREE_OPERAND (whats_returned, 0);
10485 : else
10486 : break;
10487 : }
10488 :
10489 21069048 : if (TREE_CODE (whats_returned) == TARGET_EXPR
10490 21069048 : && is_std_init_list (TREE_TYPE (whats_returned)))
10491 : {
10492 14 : tree init = TARGET_EXPR_INITIAL (whats_returned);
10493 14 : if (TREE_CODE (init) == CONSTRUCTOR)
10494 : /* Pull out the array address. */
10495 6 : whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10496 8 : else if (TREE_CODE (init) == INDIRECT_REF)
10497 : /* The source of a trivial copy looks like *(T*)&var. */
10498 5 : whats_returned = TREE_OPERAND (init, 0);
10499 : else
10500 : return false;
10501 11 : STRIP_NOPS (whats_returned);
10502 : }
10503 :
10504 : /* As a special case, we handle a call to std::move or std::forward. */
10505 21069045 : if (TREE_CODE (whats_returned) == CALL_EXPR
10506 21069045 : && (is_std_move_p (whats_returned)
10507 6070757 : || is_std_forward_p (whats_returned)))
10508 : {
10509 109 : tree arg = CALL_EXPR_ARG (whats_returned, 0);
10510 109 : return maybe_warn_about_returning_address_of_local (arg, loc);
10511 : }
10512 :
10513 21068936 : if (TREE_CODE (whats_returned) != ADDR_EXPR)
10514 : return false;
10515 469389 : whats_returned = TREE_OPERAND (whats_returned, 0);
10516 :
10517 469389 : while (TREE_CODE (whats_returned) == COMPONENT_REF
10518 912645 : || TREE_CODE (whats_returned) == ARRAY_REF)
10519 443256 : whats_returned = TREE_OPERAND (whats_returned, 0);
10520 :
10521 469389 : if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10522 469389 : || TREE_CODE (whats_returned) == TARGET_EXPR)
10523 : {
10524 53 : if (TYPE_REF_P (valtype))
10525 47 : warning_at (loc, OPT_Wreturn_local_addr,
10526 : "returning reference to temporary");
10527 6 : else if (is_std_init_list (valtype))
10528 6 : warning_at (loc, OPT_Winit_list_lifetime,
10529 : "returning temporary %<initializer_list%> does not extend "
10530 : "the lifetime of the underlying array");
10531 53 : return true;
10532 : }
10533 :
10534 469336 : STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10535 :
10536 469336 : if (DECL_P (whats_returned)
10537 35650 : && DECL_NAME (whats_returned)
10538 35650 : && DECL_FUNCTION_SCOPE_P (whats_returned)
10539 9803 : && !is_capture_proxy (whats_returned)
10540 479136 : && !(TREE_STATIC (whats_returned)
10541 : || TREE_PUBLIC (whats_returned)))
10542 : {
10543 151 : if (VAR_P (whats_returned)
10544 116 : && DECL_DECOMPOSITION_P (whats_returned)
10545 38 : && DECL_DECOMP_BASE (whats_returned)
10546 189 : && DECL_HAS_VALUE_EXPR_P (whats_returned))
10547 : {
10548 : /* When returning address of a structured binding, if the structured
10549 : binding is not a reference, continue normally, if it is a
10550 : reference, recurse on the initializer of the structured
10551 : binding. */
10552 38 : tree base = DECL_DECOMP_BASE (whats_returned);
10553 38 : if (TYPE_REF_P (TREE_TYPE (base)))
10554 : {
10555 14 : if (tree init = DECL_INITIAL (base))
10556 : return maybe_warn_about_returning_address_of_local (init, loc);
10557 : else
10558 : return false;
10559 : }
10560 : }
10561 137 : bool w = false;
10562 137 : auto_diagnostic_group d;
10563 137 : if (TYPE_REF_P (valtype))
10564 84 : w = warning_at (loc, OPT_Wreturn_local_addr,
10565 : "reference to local variable %qD returned",
10566 : whats_returned);
10567 53 : else if (is_std_init_list (valtype))
10568 3 : w = warning_at (loc, OPT_Winit_list_lifetime,
10569 : "returning local %<initializer_list%> variable %qD "
10570 : "does not extend the lifetime of the underlying array",
10571 : whats_returned);
10572 50 : else if (POINTER_TYPE_P (valtype)
10573 42 : && TREE_CODE (whats_returned) == LABEL_DECL)
10574 8 : w = warning_at (loc, OPT_Wreturn_local_addr,
10575 : "address of label %qD returned",
10576 : whats_returned);
10577 42 : else if (POINTER_TYPE_P (valtype))
10578 34 : w = warning_at (loc, OPT_Wreturn_local_addr,
10579 : "address of local variable %qD returned",
10580 : whats_returned);
10581 129 : if (w)
10582 115 : inform (DECL_SOURCE_LOCATION (whats_returned),
10583 : "declared here");
10584 137 : return true;
10585 137 : }
10586 :
10587 : return false;
10588 : }
10589 :
10590 : /* Returns true if DECL is in the std namespace. */
10591 :
10592 : bool
10593 40633163 : decl_in_std_namespace_p (tree decl)
10594 : {
10595 47553492 : while (decl)
10596 : {
10597 47289522 : decl = decl_namespace_context (decl);
10598 47289522 : if (DECL_NAMESPACE_STD_P (decl))
10599 : return true;
10600 : /* Allow inline namespaces inside of std namespace, e.g. with
10601 : --enable-symvers=gnu-versioned-namespace std::forward would be
10602 : actually std::_8::forward. */
10603 21634531 : if (!DECL_NAMESPACE_INLINE_P (decl))
10604 : return false;
10605 6920329 : decl = CP_DECL_CONTEXT (decl);
10606 : }
10607 : return false;
10608 : }
10609 :
10610 : /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10611 :
10612 : static bool
10613 6070757 : is_std_forward_p (tree fn)
10614 : {
10615 : /* std::forward only takes one argument. */
10616 6070757 : if (call_expr_nargs (fn) != 1)
10617 : return false;
10618 :
10619 2747825 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10620 2747825 : if (!decl_in_std_namespace_p (fndecl))
10621 : return false;
10622 :
10623 925140 : tree name = DECL_NAME (fndecl);
10624 925140 : return name && id_equal (name, "forward");
10625 : }
10626 :
10627 : /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10628 :
10629 : static bool
10630 6074307 : is_std_move_p (tree fn)
10631 : {
10632 : /* std::move only takes one argument. */
10633 6074307 : if (call_expr_nargs (fn) != 1)
10634 : return false;
10635 :
10636 2751137 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10637 2751137 : if (!decl_in_std_namespace_p (fndecl))
10638 : return false;
10639 :
10640 928236 : tree name = DECL_NAME (fndecl);
10641 928236 : return name && id_equal (name, "move");
10642 : }
10643 :
10644 : /* Returns true if RETVAL is a good candidate for the NRVO as per
10645 : [class.copy.elision]. FUNCTYPE is the type the function is declared
10646 : to return. */
10647 :
10648 : static bool
10649 26823669 : can_do_nrvo_p (tree retval, tree functype)
10650 : {
10651 26823669 : if (functype == error_mark_node)
10652 : return false;
10653 26823632 : if (retval)
10654 25941631 : STRIP_ANY_LOCATION_WRAPPER (retval);
10655 26823632 : tree result = DECL_RESULT (current_function_decl);
10656 26823632 : return (retval != NULL_TREE
10657 25941631 : && !processing_template_decl
10658 : /* Must be a local, automatic variable. */
10659 21397317 : && VAR_P (retval)
10660 1995414 : && DECL_CONTEXT (retval) == current_function_decl
10661 1569745 : && !TREE_STATIC (retval)
10662 : /* And not a lambda or anonymous union proxy. */
10663 1567270 : && !DECL_HAS_VALUE_EXPR_P (retval)
10664 1567085 : && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10665 : /* The cv-unqualified type of the returned value must be the
10666 : same as the cv-unqualified return type of the
10667 : function. */
10668 1566982 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10669 : TYPE_MAIN_VARIANT (functype))
10670 : /* And the returned value must be non-volatile. */
10671 28374992 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
10672 : }
10673 :
10674 : /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
10675 : would otherwise return in memory. */
10676 :
10677 : static bool
10678 26823405 : want_nrvo_p (tree retval, tree functype)
10679 : {
10680 26823405 : return (can_do_nrvo_p (retval, functype)
10681 26823405 : && aggregate_value_p (functype, current_function_decl));
10682 : }
10683 :
10684 : /* Like can_do_nrvo_p, but we check if we're trying to move a class
10685 : prvalue. */
10686 :
10687 : static bool
10688 694 : can_elide_copy_prvalue_p (tree retval, tree functype)
10689 : {
10690 694 : if (functype == error_mark_node)
10691 : return false;
10692 694 : if (retval)
10693 694 : STRIP_ANY_LOCATION_WRAPPER (retval);
10694 694 : return (retval != NULL_TREE
10695 694 : && !glvalue_p (retval)
10696 135 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10697 : TYPE_MAIN_VARIANT (functype))
10698 802 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
10699 : }
10700 :
10701 : /* If we should treat RETVAL, an expression being returned, as if it were
10702 : designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10703 : NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10704 : context (rather than throw). */
10705 :
10706 : tree
10707 3592326 : treat_lvalue_as_rvalue_p (tree expr, bool return_p)
10708 : {
10709 3592326 : if (cxx_dialect == cxx98)
10710 : return NULL_TREE;
10711 :
10712 3584123 : tree retval = expr;
10713 3584123 : STRIP_ANY_LOCATION_WRAPPER (retval);
10714 3584123 : if (REFERENCE_REF_P (retval))
10715 89278 : retval = TREE_OPERAND (retval, 0);
10716 :
10717 : /* An implicitly movable entity is a variable of automatic storage duration
10718 : that is either a non-volatile object or (C++20) an rvalue reference to a
10719 : non-volatile object type. */
10720 3584123 : if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
10721 3119849 : || TREE_CODE (retval) == PARM_DECL)
10722 560726 : && !TREE_STATIC (retval)
10723 558516 : && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
10724 558513 : && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
10725 20783 : || (cxx_dialect >= cxx20
10726 3934 : && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
10727 : return NULL_TREE;
10728 :
10729 : /* If the expression in a return or co_return statement is a (possibly
10730 : parenthesized) id-expression that names an implicitly movable entity
10731 : declared in the body or parameter-declaration-clause of the innermost
10732 : enclosing function or lambda-expression, */
10733 537776 : if (DECL_CONTEXT (retval) != current_function_decl)
10734 : return NULL_TREE;
10735 537761 : if (return_p)
10736 : {
10737 537699 : expr = move (expr);
10738 537699 : if (expr == error_mark_node)
10739 : return NULL_TREE;
10740 537699 : return set_implicit_rvalue_p (expr);
10741 : }
10742 :
10743 : /* if the operand of a throw-expression is a (possibly parenthesized)
10744 : id-expression that names an implicitly movable entity whose scope does not
10745 : extend beyond the compound-statement of the innermost try-block or
10746 : function-try-block (if any) whose compound-statement or ctor-initializer
10747 : encloses the throw-expression, */
10748 :
10749 : /* C++20 added move on throw of parms. */
10750 62 : if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
10751 : return NULL_TREE;
10752 :
10753 58 : for (cp_binding_level *b = current_binding_level;
10754 29 : ; b = b->level_chain)
10755 : {
10756 92 : for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
10757 49 : if (decl == retval)
10758 44 : return set_implicit_rvalue_p (move (expr));
10759 43 : if (b->kind == sk_function_parms || b->kind == sk_try)
10760 : return NULL_TREE;
10761 29 : }
10762 : }
10763 :
10764 : /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
10765 : is true). EXPR is the std::move expression; TYPE is the type of the object
10766 : being initialized. */
10767 :
10768 : void
10769 73345569 : maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
10770 : {
10771 73345569 : if (!(warn_pessimizing_move || warn_redundant_move))
10772 : return;
10773 :
10774 3834406 : const location_t loc = cp_expr_loc_or_input_loc (expr);
10775 :
10776 : /* C++98 doesn't know move. */
10777 3834406 : if (cxx_dialect < cxx11)
10778 : return;
10779 :
10780 : /* Wait until instantiation time, since we can't gauge if we should do
10781 : the NRVO until then. */
10782 3768776 : if (processing_template_decl)
10783 : return;
10784 :
10785 : /* This is only interesting for class types. */
10786 3701518 : if (!CLASS_TYPE_P (type))
10787 : return;
10788 :
10789 59907 : bool wrapped_p = false;
10790 : /* A a = std::move (A()); */
10791 59907 : if (TREE_CODE (expr) == TREE_LIST)
10792 : {
10793 7254 : if (list_length (expr) == 1)
10794 : {
10795 3990 : expr = TREE_VALUE (expr);
10796 3990 : wrapped_p = true;
10797 : }
10798 : else
10799 : return;
10800 : }
10801 : /* A a = {std::move (A())};
10802 : A a{std::move (A())}; */
10803 52653 : else if (TREE_CODE (expr) == CONSTRUCTOR)
10804 : {
10805 3292 : if (CONSTRUCTOR_NELTS (expr) == 1)
10806 : {
10807 424 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
10808 424 : wrapped_p = true;
10809 : }
10810 : else
10811 : return;
10812 : }
10813 :
10814 : /* First, check if this is a call to std::move. */
10815 4032 : if (!REFERENCE_REF_P (expr)
10816 56947 : || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
10817 : return;
10818 1812 : tree fn = TREE_OPERAND (expr, 0);
10819 1812 : if (!is_std_move_p (fn))
10820 : return;
10821 1322 : tree arg = CALL_EXPR_ARG (fn, 0);
10822 1322 : if (TREE_CODE (arg) != NOP_EXPR)
10823 : return;
10824 : /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
10825 : and implicitly-movable warnings. */
10826 1322 : if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
10827 : {
10828 694 : arg = TREE_OPERAND (arg, 0);
10829 694 : arg = TREE_OPERAND (arg, 0);
10830 694 : arg = convert_from_reference (arg);
10831 694 : if (can_elide_copy_prvalue_p (arg, type))
10832 : {
10833 108 : auto_diagnostic_group d;
10834 108 : if (warning_at (loc, OPT_Wpessimizing_move,
10835 : "moving a temporary object prevents copy elision"))
10836 108 : inform (loc, "remove %<std::move%> call");
10837 108 : }
10838 : /* The rest of the warnings is only relevant for when we are returning
10839 : from a function. */
10840 694 : if (!return_p)
10841 : return;
10842 :
10843 264 : tree moved;
10844 : /* Warn if we could do copy elision were it not for the move. */
10845 264 : if (can_do_nrvo_p (arg, type))
10846 : {
10847 48 : auto_diagnostic_group d;
10848 48 : if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
10849 48 : && warning_at (loc, OPT_Wpessimizing_move,
10850 : "moving a local object in a return statement "
10851 : "prevents copy elision"))
10852 42 : inform (loc, "remove %<std::move%> call");
10853 48 : }
10854 : /* Warn if the move is redundant. It is redundant when we would
10855 : do maybe-rvalue overload resolution even without std::move. */
10856 216 : else if (warn_redundant_move
10857 : /* This doesn't apply for return {std::move (t)};. */
10858 136 : && !wrapped_p
10859 133 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
10860 312 : && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
10861 : {
10862 : /* Make sure that overload resolution would actually succeed
10863 : if we removed the std::move call. */
10864 54 : tree t = convert_for_initialization (NULL_TREE, type,
10865 : moved,
10866 : (LOOKUP_NORMAL
10867 : | LOOKUP_ONLYCONVERTING),
10868 : ICR_RETURN, NULL_TREE, 0,
10869 : tf_none);
10870 : /* If this worked, implicit rvalue would work, so the call to
10871 : std::move is redundant. */
10872 54 : if (t != error_mark_node)
10873 : {
10874 54 : auto_diagnostic_group d;
10875 54 : if (warning_at (loc, OPT_Wredundant_move,
10876 : "redundant move in return statement"))
10877 54 : inform (loc, "remove %<std::move%> call");
10878 54 : }
10879 : }
10880 : }
10881 : /* Also try to warn about redundant std::move in code such as
10882 : T f (const T& t)
10883 : {
10884 : return std::move(t);
10885 : }
10886 : for which EXPR will be something like
10887 : *std::move<const T&> ((const struct T &) (const struct T *) t)
10888 : and where the std::move does nothing if T does not have a T(const T&&)
10889 : constructor, because the argument is const. It will not use T(T&&)
10890 : because that would mean losing the const. */
10891 628 : else if (warn_redundant_move
10892 523 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
10893 64 : && TYPE_REF_P (TREE_TYPE (arg))
10894 692 : && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
10895 : {
10896 18 : tree rtype = TREE_TYPE (TREE_TYPE (arg));
10897 18 : if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
10898 9 : return;
10899 : /* Check for the unlikely case there's T(const T&&) (we don't care if
10900 : it's deleted). */
10901 63 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
10902 30 : if (move_fn_p (fn))
10903 : {
10904 15 : tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
10905 15 : if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
10906 6 : return;
10907 : }
10908 9 : auto_diagnostic_group d;
10909 9 : if (return_p
10910 9 : ? warning_at (loc, OPT_Wredundant_move,
10911 : "redundant move in return statement")
10912 3 : : warning_at (loc, OPT_Wredundant_move,
10913 : "redundant move in initialization"))
10914 9 : inform (loc, "remove %<std::move%> call");
10915 9 : }
10916 : }
10917 :
10918 : /* Check that returning RETVAL from the current function is valid.
10919 : Return an expression explicitly showing all conversions required to
10920 : change RETVAL into the function return type, and to assign it to
10921 : the DECL_RESULT for the function. Set *NO_WARNING to true if
10922 : code reaches end of non-void function warning shouldn't be issued
10923 : on this RETURN_EXPR. */
10924 :
10925 : tree
10926 64555636 : check_return_expr (tree retval, bool *no_warning)
10927 : {
10928 64555636 : tree result;
10929 : /* The type actually returned by the function. */
10930 64555636 : tree valtype;
10931 : /* The type the function is declared to return, or void if
10932 : the declared type is incomplete. */
10933 64555636 : tree functype;
10934 64555636 : int fn_returns_value_p;
10935 64555636 : location_t loc = cp_expr_loc_or_input_loc (retval);
10936 :
10937 64555636 : *no_warning = false;
10938 :
10939 : /* A `volatile' function is one that isn't supposed to return, ever.
10940 : (This is a G++ extension, used to get better code for functions
10941 : that call the `volatile' function.) */
10942 64555636 : if (TREE_THIS_VOLATILE (current_function_decl))
10943 4 : warning (0, "function declared %<noreturn%> has a %<return%> statement");
10944 :
10945 : /* Check for various simple errors. */
10946 129111272 : if (DECL_DESTRUCTOR_P (current_function_decl))
10947 : {
10948 443 : if (retval)
10949 4 : error_at (loc, "returning a value from a destructor");
10950 :
10951 443 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
10952 0 : retval = current_class_ptr;
10953 : else
10954 : return NULL_TREE;
10955 : }
10956 64555193 : else if (DECL_CONSTRUCTOR_P (current_function_decl))
10957 : {
10958 14531 : if (in_function_try_handler)
10959 : /* If a return statement appears in a handler of the
10960 : function-try-block of a constructor, the program is ill-formed. */
10961 0 : error ("cannot return from a handler of a function-try-block of a constructor");
10962 14531 : else if (retval)
10963 : /* You can't return a value from a constructor. */
10964 4 : error_at (loc, "returning a value from a constructor");
10965 :
10966 14531 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
10967 0 : retval = current_class_ptr;
10968 : else
10969 : return NULL_TREE;
10970 : }
10971 :
10972 64540662 : const tree saved_retval = retval;
10973 :
10974 64540662 : if (processing_template_decl)
10975 : {
10976 42842083 : current_function_returns_value = 1;
10977 :
10978 42842083 : if (check_for_bare_parameter_packs (retval))
10979 10 : return error_mark_node;
10980 :
10981 : /* If one of the types might be void, we can't tell whether we're
10982 : returning a value. */
10983 77557845 : if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
10984 15886341 : && !FNDECL_USED_AUTO (current_function_decl))
10985 69797805 : || (retval != NULL_TREE
10986 26315025 : && (TREE_TYPE (retval) == NULL_TREE
10987 18488679 : || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
10988 28846819 : goto dependent;
10989 : }
10990 :
10991 35693833 : functype = TREE_TYPE (TREE_TYPE (current_function_decl));
10992 :
10993 : /* Deduce auto return type from a return statement. */
10994 35693833 : if (FNDECL_USED_AUTO (current_function_decl))
10995 : {
10996 58298 : tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
10997 58298 : tree auto_node;
10998 58298 : tree type;
10999 :
11000 58298 : if (!retval && !is_auto (pattern))
11001 : {
11002 : /* Give a helpful error message. */
11003 3 : error ("return-statement with no value, in function returning %qT",
11004 : pattern);
11005 3 : inform (input_location, "only plain %<auto%> return type can be "
11006 : "deduced to %<void%>");
11007 3 : type = error_mark_node;
11008 : }
11009 58295 : else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11010 : {
11011 6 : error ("returning initializer list");
11012 6 : type = error_mark_node;
11013 : }
11014 : else
11015 : {
11016 58289 : if (!retval)
11017 245 : retval = void_node;
11018 58289 : auto_node = type_uses_auto (pattern);
11019 58289 : type = do_auto_deduction (pattern, retval, auto_node,
11020 : tf_warning_or_error, adc_return_type);
11021 : }
11022 :
11023 58298 : if (type == error_mark_node)
11024 : /* Leave it. */;
11025 58150 : else if (functype == pattern)
11026 44602 : apply_deduced_return_type (current_function_decl, type);
11027 13548 : else if (!same_type_p (type, functype))
11028 : {
11029 21 : if (LAMBDA_FUNCTION_P (current_function_decl))
11030 6 : error_at (loc, "inconsistent types %qT and %qT deduced for "
11031 : "lambda return type", functype, type);
11032 : else
11033 9 : error_at (loc, "inconsistent deduction for auto return type: "
11034 : "%qT and then %qT", functype, type);
11035 : }
11036 : functype = type;
11037 : }
11038 :
11039 35693833 : result = DECL_RESULT (current_function_decl);
11040 35693833 : valtype = TREE_TYPE (result);
11041 35693833 : gcc_assert (valtype != NULL_TREE);
11042 35693833 : fn_returns_value_p = !VOID_TYPE_P (valtype);
11043 :
11044 : /* Check for a return statement with no return value in a function
11045 : that's supposed to return a value. */
11046 35693833 : if (!retval && fn_returns_value_p)
11047 : {
11048 34 : if (functype != error_mark_node)
11049 31 : permerror (input_location, "return-statement with no value, in "
11050 : "function returning %qT", valtype);
11051 : /* Remember that this function did return. */
11052 34 : current_function_returns_value = 1;
11053 : /* And signal caller that TREE_NO_WARNING should be set on the
11054 : RETURN_EXPR to avoid control reaches end of non-void function
11055 : warnings in tree-cfg.cc. */
11056 34 : *no_warning = true;
11057 : }
11058 : /* Check for a return statement with a value in a function that
11059 : isn't supposed to return a value. */
11060 35693799 : else if (retval && !fn_returns_value_p)
11061 : {
11062 59487 : if (VOID_TYPE_P (TREE_TYPE (retval)))
11063 : /* You can return a `void' value from a function of `void'
11064 : type. In that case, we have to evaluate the expression for
11065 : its side-effects. */
11066 59457 : finish_expr_stmt (retval);
11067 30 : else if (retval != error_mark_node)
11068 26 : permerror (loc, "return-statement with a value, in function "
11069 : "returning %qT", valtype);
11070 59487 : current_function_returns_null = 1;
11071 :
11072 : /* There's really no value to return, after all. */
11073 59487 : return NULL_TREE;
11074 : }
11075 35634312 : else if (!retval)
11076 : /* Remember that this function can sometimes return without a
11077 : value. */
11078 881987 : current_function_returns_null = 1;
11079 : else
11080 : /* Remember that this function did return a value. */
11081 34752325 : current_function_returns_value = 1;
11082 :
11083 : /* Check for erroneous operands -- but after giving ourselves a
11084 : chance to provide an error about returning a value from a void
11085 : function. */
11086 35634346 : if (error_operand_p (retval))
11087 : {
11088 928 : current_function_return_value = error_mark_node;
11089 928 : return error_mark_node;
11090 : }
11091 :
11092 : /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11093 43146272 : if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11094 22560 : && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11095 1690 : && ! flag_check_new
11096 35635096 : && retval && null_ptr_cst_p (retval))
11097 32 : warning (0, "%<operator new%> must not return NULL unless it is "
11098 : "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11099 :
11100 : /* Effective C++ rule 15. See also start_function. */
11101 35633418 : if (warn_ecpp
11102 56 : && DECL_NAME (current_function_decl) == assign_op_identifier
11103 35633458 : && !type_dependent_expression_p (retval))
11104 : {
11105 36 : bool warn = true;
11106 :
11107 : /* The function return type must be a reference to the current
11108 : class. */
11109 36 : if (TYPE_REF_P (valtype)
11110 60 : && same_type_ignoring_top_level_qualifiers_p
11111 24 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11112 : {
11113 : /* Returning '*this' is obviously OK. */
11114 24 : if (retval == current_class_ref)
11115 : warn = false;
11116 : /* If we are calling a function whose return type is the same of
11117 : the current class reference, it is ok. */
11118 8 : else if (INDIRECT_REF_P (retval)
11119 8 : && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11120 : warn = false;
11121 : }
11122 :
11123 : if (warn)
11124 12 : warning_at (loc, OPT_Weffc__,
11125 : "%<operator=%> should return a reference to %<*this%>");
11126 : }
11127 :
11128 35633418 : if (dependent_type_p (functype)
11129 35633418 : || type_dependent_expression_p (retval))
11130 : {
11131 37656832 : dependent:
11132 : /* We should not have changed the return value. */
11133 37656832 : gcc_assert (retval == saved_retval);
11134 : /* We don't know if this is an lvalue or rvalue use, but
11135 : either way we can mark it as read. */
11136 37656832 : mark_exp_read (retval);
11137 37656832 : return retval;
11138 : }
11139 :
11140 : /* The fabled Named Return Value optimization, as per [class.copy]/15:
11141 :
11142 : [...] For a function with a class return type, if the expression
11143 : in the return statement is the name of a local object, and the cv-
11144 : unqualified type of the local object is the same as the function
11145 : return type, an implementation is permitted to omit creating the tem-
11146 : porary object to hold the function return value [...]
11147 :
11148 : So, if this is a value-returning function that always returns the same
11149 : local variable, remember it.
11150 :
11151 : We choose the first suitable variable even if the function sometimes
11152 : returns something else, but only if the variable is out of scope at the
11153 : other return sites, or else we run the risk of clobbering the variable we
11154 : chose if the other returned expression uses the chosen variable somehow.
11155 :
11156 : We don't currently do this if the first return is a non-variable, as it
11157 : would be complicated to determine whether an NRV selected later was in
11158 : scope at the point of the earlier return. We also don't currently support
11159 : multiple variables with non-overlapping scopes (53637).
11160 :
11161 : See finish_function and finalize_nrv for the rest of this optimization. */
11162 26823405 : tree bare_retval = NULL_TREE;
11163 26823405 : if (retval)
11164 : {
11165 25941401 : retval = maybe_undo_parenthesized_ref (retval);
11166 25941401 : bare_retval = tree_strip_any_location_wrapper (retval);
11167 : }
11168 :
11169 26823405 : bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11170 26823405 : if (fn_returns_value_p && flag_elide_constructors
11171 25941374 : && current_function_return_value != bare_retval)
11172 : {
11173 25940778 : if (named_return_value_okay_p
11174 134187 : && current_function_return_value == NULL_TREE)
11175 113573 : current_function_return_value = bare_retval;
11176 25827205 : else if (current_function_return_value
11177 4633885 : && VAR_P (current_function_return_value)
11178 106 : && DECL_NAME (current_function_return_value)
11179 25827311 : && !decl_in_scope_p (current_function_return_value))
11180 : {
11181 : /* The earlier NRV is out of scope at this point, so it's safe to
11182 42 : leave it alone; the current return can't refer to it. */;
11183 42 : if (named_return_value_okay_p
11184 42 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11185 : {
11186 9 : warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11187 : bare_retval);
11188 9 : suppress_warning (current_function_decl, OPT_Wnrvo);
11189 : }
11190 : }
11191 : else
11192 : {
11193 25827163 : if ((named_return_value_okay_p
11194 25806560 : || (current_function_return_value
11195 4613240 : && current_function_return_value != error_mark_node))
11196 25827204 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11197 : {
11198 20590 : warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11199 : current_function_decl);
11200 20590 : suppress_warning (current_function_decl, OPT_Wnrvo);
11201 : }
11202 25827163 : current_function_return_value = error_mark_node;
11203 : }
11204 : }
11205 :
11206 : /* We don't need to do any conversions when there's nothing being
11207 : returned. */
11208 26823405 : if (!retval)
11209 : return NULL_TREE;
11210 :
11211 25941401 : if (!named_return_value_okay_p)
11212 25806638 : maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11213 :
11214 : /* Do any required conversions. */
11215 51882531 : if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11216 : /* No conversions are required. */
11217 : ;
11218 : else
11219 : {
11220 25941130 : int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11221 :
11222 : /* The functype's return type will have been set to void, if it
11223 : was an incomplete type. Just treat this as 'return;' */
11224 25941130 : if (VOID_TYPE_P (functype))
11225 7 : return error_mark_node;
11226 :
11227 25941123 : if (processing_template_decl)
11228 4544314 : retval = build_non_dependent_expr (retval);
11229 :
11230 : /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11231 : treated as an rvalue for the purposes of overload resolution to
11232 : favor move constructors over copy constructors.
11233 :
11234 : Note that these conditions are similar to, but not as strict as,
11235 : the conditions for the named return value optimization. */
11236 25941123 : bool converted = false;
11237 25941123 : tree moved;
11238 : /* Until C++23, this was only interesting for class type, but in C++23,
11239 : we should do the below when we're converting rom/to a class/reference
11240 : (a non-scalar type). */
11241 25941123 : if ((cxx_dialect < cxx23
11242 3467611 : ? CLASS_TYPE_P (functype)
11243 163385 : : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11244 29449201 : && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11245 : /* In C++20 and earlier we treat the return value as an rvalue
11246 : that can bind to lvalue refs. In C++23, such an expression is just
11247 : an xvalue (see reference_binding). */
11248 : retval = moved;
11249 :
11250 : /* The call in a (lambda) thunk needs no conversions. */
11251 25941123 : if (TREE_CODE (retval) == CALL_EXPR
11252 25941123 : && call_from_lambda_thunk_p (retval))
11253 : converted = true;
11254 :
11255 : /* First convert the value to the function's return type, then
11256 : to the type of return value's location to handle the
11257 : case that functype is smaller than the valtype. */
11258 25940258 : if (!converted)
11259 25940258 : retval = convert_for_initialization
11260 25940258 : (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11261 : tf_warning_or_error);
11262 25941123 : retval = convert (valtype, retval);
11263 :
11264 : /* If the conversion failed, treat this just like `return;'. */
11265 25941123 : if (retval == error_mark_node)
11266 : return retval;
11267 : /* We can't initialize a register from a AGGR_INIT_EXPR. */
11268 25940946 : else if (! cfun->returns_struct
11269 25041598 : && TREE_CODE (retval) == TARGET_EXPR
11270 28149765 : && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
11271 327719 : retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11272 327719 : TREE_OPERAND (retval, 0));
11273 25613227 : else if (!processing_template_decl
11274 21068927 : && maybe_warn_about_returning_address_of_local (retval, loc)
11275 25613417 : && INDIRECT_TYPE_P (valtype))
11276 173 : retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11277 173 : build_zero_cst (TREE_TYPE (retval)));
11278 : }
11279 :
11280 : /* A naive attempt to reduce the number of -Wdangling-reference false
11281 : positives: if we know that this function can return a variable with
11282 : static storage duration rather than one of its parameters, suppress
11283 : the warning. */
11284 25941217 : if (warn_dangling_reference
11285 901670 : && TYPE_REF_P (functype)
11286 23897 : && bare_retval
11287 : && VAR_P (bare_retval)
11288 23897 : && TREE_STATIC (bare_retval))
11289 52 : suppress_warning (current_function_decl, OPT_Wdangling_reference);
11290 :
11291 25941217 : if (processing_template_decl)
11292 : return saved_retval;
11293 :
11294 : /* Actually copy the value returned into the appropriate location. */
11295 21396917 : if (retval && retval != result)
11296 : {
11297 : /* If there's a postcondition for a scalar return value, wrap
11298 : retval in a call to the postcondition function. */
11299 21396646 : if (tree post = apply_postcondition_to_return (retval))
11300 57 : retval = post;
11301 21396646 : retval = cp_build_init_expr (result, retval);
11302 : }
11303 :
11304 21396917 : if (current_function_return_value == bare_retval)
11305 114149 : INIT_EXPR_NRV_P (retval) = true;
11306 :
11307 21396917 : if (tree set = maybe_set_retval_sentinel ())
11308 89742 : retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11309 :
11310 : /* If there's a postcondition for an aggregate return value, call the
11311 : postcondition function after the return object is initialized. */
11312 21396917 : if (tree post = apply_postcondition_to_return (result))
11313 2 : retval = build2 (COMPOUND_EXPR, void_type_node, retval, post);
11314 :
11315 : return retval;
11316 : }
11317 :
11318 :
11319 : /* Returns nonzero if the pointer-type FROM can be converted to the
11320 : pointer-type TO via a qualification conversion. If CONSTP is -1,
11321 : then we return nonzero if the pointers are similar, and the
11322 : cv-qualification signature of FROM is a proper subset of that of TO.
11323 :
11324 : If CONSTP is positive, then all outer pointers have been
11325 : const-qualified. */
11326 :
11327 : static bool
11328 59895612 : comp_ptr_ttypes_real (tree to, tree from, int constp)
11329 : {
11330 59895612 : bool to_more_cv_qualified = false;
11331 59895612 : bool is_opaque_pointer = false;
11332 :
11333 278547 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11334 : {
11335 60174159 : if (TREE_CODE (to) != TREE_CODE (from))
11336 : return false;
11337 :
11338 34708344 : if (TREE_CODE (from) == OFFSET_TYPE
11339 34708450 : && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11340 : TYPE_OFFSET_BASETYPE (to)))
11341 : return false;
11342 :
11343 : /* Const and volatile mean something different for function and
11344 : array types, so the usual checks are not appropriate. We'll
11345 : check the array type elements in further iterations. */
11346 34708344 : if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11347 : {
11348 34473318 : if (!at_least_as_qualified_p (to, from))
11349 : return false;
11350 :
11351 30122799 : if (!at_least_as_qualified_p (from, to))
11352 : {
11353 21739750 : if (constp == 0)
11354 : return false;
11355 : to_more_cv_qualified = true;
11356 : }
11357 :
11358 30122583 : if (constp > 0)
11359 30119505 : constp &= TYPE_READONLY (to);
11360 : }
11361 :
11362 30357609 : if (VECTOR_TYPE_P (to))
11363 6147 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11364 :
11365 : /* P0388R4 allows a conversion from int[N] to int[] but not the
11366 : other way round. When both arrays have bounds but they do
11367 : not match, then no conversion is possible. */
11368 30357609 : if (TREE_CODE (to) == ARRAY_TYPE
11369 30357609 : && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11370 : return false;
11371 :
11372 30357384 : if (!TYPE_PTR_P (to)
11373 30357384 : && !TYPE_PTRDATAMEM_P (to)
11374 : /* CWG 330 says we need to look through arrays. */
11375 30079848 : && TREE_CODE (to) != ARRAY_TYPE)
11376 30078837 : return ((constp >= 0 || to_more_cv_qualified)
11377 30078837 : && (is_opaque_pointer
11378 30078327 : || same_type_ignoring_top_level_qualifiers_p (to, from)));
11379 : }
11380 : }
11381 :
11382 : /* When comparing, say, char ** to char const **, this function takes
11383 : the 'char *' and 'char const *'. Do not pass non-pointer/reference
11384 : types to this function. */
11385 :
11386 : int
11387 59895041 : comp_ptr_ttypes (tree to, tree from)
11388 : {
11389 59895041 : return comp_ptr_ttypes_real (to, from, 1);
11390 : }
11391 :
11392 : /* Returns true iff FNTYPE is a non-class type that involves
11393 : error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11394 : if a parameter type is ill-formed. */
11395 :
11396 : bool
11397 443511 : error_type_p (const_tree type)
11398 : {
11399 474879 : tree t;
11400 :
11401 474879 : switch (TREE_CODE (type))
11402 : {
11403 : case ERROR_MARK:
11404 : return true;
11405 :
11406 31278 : case POINTER_TYPE:
11407 31278 : case REFERENCE_TYPE:
11408 31278 : case OFFSET_TYPE:
11409 31278 : return error_type_p (TREE_TYPE (type));
11410 :
11411 2820 : case FUNCTION_TYPE:
11412 2820 : case METHOD_TYPE:
11413 2820 : if (error_type_p (TREE_TYPE (type)))
11414 : return true;
11415 8277 : for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11416 5461 : if (error_type_p (TREE_VALUE (t)))
11417 : return true;
11418 : return false;
11419 :
11420 143341 : case RECORD_TYPE:
11421 143341 : if (TYPE_PTRMEMFUNC_P (type))
11422 90 : return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11423 : return false;
11424 :
11425 : default:
11426 : return false;
11427 : }
11428 : }
11429 :
11430 : /* Returns true if to and from are (possibly multi-level) pointers to the same
11431 : type or inheritance-related types, regardless of cv-quals. */
11432 :
11433 : bool
11434 42927867 : ptr_reasonably_similar (const_tree to, const_tree from)
11435 : {
11436 2080 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11437 : {
11438 : /* Any target type is similar enough to void. */
11439 42929947 : if (VOID_TYPE_P (to))
11440 965 : return !error_type_p (from);
11441 42928982 : if (VOID_TYPE_P (from))
11442 432115 : return !error_type_p (to);
11443 :
11444 42496867 : if (TREE_CODE (to) != TREE_CODE (from))
11445 : return false;
11446 :
11447 17363734 : if (TREE_CODE (from) == OFFSET_TYPE
11448 17363734 : && comptypes (TYPE_OFFSET_BASETYPE (to),
11449 4 : TYPE_OFFSET_BASETYPE (from),
11450 : COMPARE_BASE | COMPARE_DERIVED))
11451 4 : continue;
11452 :
11453 17363726 : if (VECTOR_TYPE_P (to)
11454 17363726 : && vector_types_convertible_p (to, from, false))
11455 : return true;
11456 :
11457 17363609 : if (TREE_CODE (to) == INTEGER_TYPE
11458 17363609 : && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11459 : return true;
11460 :
11461 17275011 : if (TREE_CODE (to) == FUNCTION_TYPE)
11462 1075 : return !error_type_p (to) && !error_type_p (from);
11463 :
11464 17273936 : if (!TYPE_PTR_P (to))
11465 : {
11466 : /* When either type is incomplete avoid DERIVED_FROM_P,
11467 : which may call complete_type (c++/57942). */
11468 17271860 : bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11469 : return comptypes
11470 17271860 : (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11471 17271860 : b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11472 : }
11473 2080 : }
11474 : }
11475 :
11476 : /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11477 : pointer-to-member types) are the same, ignoring cv-qualification at
11478 : all levels. CB says how we should behave when comparing array bounds. */
11479 :
11480 : bool
11481 437021 : comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11482 : {
11483 437021 : bool is_opaque_pointer = false;
11484 :
11485 438037 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11486 : {
11487 875058 : if (TREE_CODE (to) != TREE_CODE (from))
11488 : return false;
11489 :
11490 615529 : if (TREE_CODE (from) == OFFSET_TYPE
11491 615626 : && same_type_p (TYPE_OFFSET_BASETYPE (from),
11492 : TYPE_OFFSET_BASETYPE (to)))
11493 41 : continue;
11494 :
11495 615447 : if (VECTOR_TYPE_P (to))
11496 11858 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11497 :
11498 615447 : if (TREE_CODE (to) == ARRAY_TYPE
11499 : /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11500 : we must fail. */
11501 615447 : && !comp_array_types (to, from, cb, /*strict=*/false))
11502 : return false;
11503 :
11504 : /* CWG 330 says we need to look through arrays. */
11505 615112 : if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11506 177116 : return (is_opaque_pointer
11507 177116 : || same_type_ignoring_top_level_qualifiers_p (to, from));
11508 : }
11509 : }
11510 :
11511 : /* Returns the type qualifiers for this type, including the qualifiers on the
11512 : elements for an array type. */
11513 :
11514 : int
11515 27913253731 : cp_type_quals (const_tree type)
11516 : {
11517 27913253731 : int quals;
11518 : /* This CONST_CAST is okay because strip_array_types returns its
11519 : argument unmodified and we assign it to a const_tree. */
11520 27913253731 : type = strip_array_types (CONST_CAST_TREE (type));
11521 27913253731 : if (type == error_mark_node
11522 : /* Quals on a FUNCTION_TYPE are memfn quals. */
11523 27880258594 : || TREE_CODE (type) == FUNCTION_TYPE)
11524 : return TYPE_UNQUALIFIED;
11525 27843047854 : quals = TYPE_QUALS (type);
11526 : /* METHOD and REFERENCE_TYPEs should never have quals. */
11527 27843047854 : gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11528 : && !TYPE_REF_P (type))
11529 : || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11530 : == TYPE_UNQUALIFIED));
11531 : return quals;
11532 : }
11533 :
11534 : /* Returns the function-ref-qualifier for TYPE */
11535 :
11536 : cp_ref_qualifier
11537 1902987839 : type_memfn_rqual (const_tree type)
11538 : {
11539 1902987839 : gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11540 :
11541 1902987839 : if (!FUNCTION_REF_QUALIFIED (type))
11542 : return REF_QUAL_NONE;
11543 26548924 : else if (FUNCTION_RVALUE_QUALIFIED (type))
11544 : return REF_QUAL_RVALUE;
11545 : else
11546 13317695 : return REF_QUAL_LVALUE;
11547 : }
11548 :
11549 : /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11550 : METHOD_TYPE. */
11551 :
11552 : int
11553 605016346 : type_memfn_quals (const_tree type)
11554 : {
11555 605016346 : if (TREE_CODE (type) == FUNCTION_TYPE)
11556 46531894 : return TYPE_QUALS (type);
11557 558484452 : else if (TREE_CODE (type) == METHOD_TYPE)
11558 558484452 : return cp_type_quals (class_of_this_parm (type));
11559 : else
11560 0 : gcc_unreachable ();
11561 : }
11562 :
11563 : /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11564 : MEMFN_QUALS and its ref-qualifier to RQUAL. */
11565 :
11566 : tree
11567 31803636 : apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11568 : {
11569 : /* Could handle METHOD_TYPE here if necessary. */
11570 31803636 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11571 31803636 : if (TYPE_QUALS (type) == memfn_quals
11572 31803636 : && type_memfn_rqual (type) == rqual)
11573 : return type;
11574 :
11575 : /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11576 : complex. */
11577 255839 : tree result = build_qualified_type (type, memfn_quals);
11578 255839 : return build_ref_qualified_type (result, rqual);
11579 : }
11580 :
11581 : /* Returns nonzero if TYPE is const or volatile. */
11582 :
11583 : bool
11584 588206330 : cv_qualified_p (const_tree type)
11585 : {
11586 588206330 : int quals = cp_type_quals (type);
11587 495460773 : return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11588 : }
11589 :
11590 : /* Returns nonzero if the TYPE contains a mutable member. */
11591 :
11592 : bool
11593 510335076 : cp_has_mutable_p (const_tree type)
11594 : {
11595 : /* This CONST_CAST is okay because strip_array_types returns its
11596 : argument unmodified and we assign it to a const_tree. */
11597 510335076 : type = strip_array_types (CONST_CAST_TREE(type));
11598 :
11599 510335076 : return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11600 : }
11601 :
11602 : /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11603 : TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11604 : approximation. In particular, consider:
11605 :
11606 : int f();
11607 : struct S { int i; };
11608 : const S s = { f(); }
11609 :
11610 : Here, we will make "s" as TREE_READONLY (because it is declared
11611 : "const") -- only to reverse ourselves upon seeing that the
11612 : initializer is non-constant. */
11613 :
11614 : void
11615 558456362 : cp_apply_type_quals_to_decl (int type_quals, tree decl)
11616 : {
11617 558456362 : tree type = TREE_TYPE (decl);
11618 :
11619 558456362 : if (type == error_mark_node)
11620 : return;
11621 :
11622 542480410 : if (TREE_CODE (decl) == TYPE_DECL)
11623 : return;
11624 :
11625 485063793 : gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11626 : && type_quals != TYPE_UNQUALIFIED));
11627 :
11628 : /* Avoid setting TREE_READONLY incorrectly. */
11629 : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11630 : constructor can produce constant init, so rely on cp_finish_decl to
11631 : clear TREE_READONLY if the variable has non-constant init. */
11632 :
11633 : /* If the type has (or might have) a mutable component, that component
11634 : might be modified. */
11635 485063793 : if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11636 42329280 : type_quals &= ~TYPE_QUAL_CONST;
11637 :
11638 485063793 : c_apply_type_quals_to_decl (type_quals, decl);
11639 : }
11640 :
11641 : /* Subroutine of casts_away_constness. Make T1 and T2 point at
11642 : exemplar types such that casting T1 to T2 is casting away constness
11643 : if and only if there is no implicit conversion from T1 to T2. */
11644 :
11645 : static void
11646 795408 : casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11647 : {
11648 795408 : int quals1;
11649 795408 : int quals2;
11650 :
11651 : /* [expr.const.cast]
11652 :
11653 : For multi-level pointer to members and multi-level mixed pointers
11654 : and pointers to members (conv.qual), the "member" aspect of a
11655 : pointer to member level is ignored when determining if a const
11656 : cv-qualifier has been cast away. */
11657 : /* [expr.const.cast]
11658 :
11659 : For two pointer types:
11660 :
11661 : X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11662 : X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11663 : K is min(N,M)
11664 :
11665 : casting from X1 to X2 casts away constness if, for a non-pointer
11666 : type T there does not exist an implicit conversion (clause
11667 : _conv_) from:
11668 :
11669 : Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11670 :
11671 : to
11672 :
11673 : Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11674 795408 : if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11675 398078 : || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11676 : {
11677 397474 : *t1 = cp_build_qualified_type (void_type_node,
11678 : cp_type_quals (*t1));
11679 397474 : *t2 = cp_build_qualified_type (void_type_node,
11680 : cp_type_quals (*t2));
11681 397474 : return;
11682 : }
11683 :
11684 397934 : quals1 = cp_type_quals (*t1);
11685 397934 : quals2 = cp_type_quals (*t2);
11686 :
11687 397934 : if (TYPE_PTRDATAMEM_P (*t1))
11688 0 : *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
11689 : else
11690 397934 : *t1 = TREE_TYPE (*t1);
11691 397934 : if (TYPE_PTRDATAMEM_P (*t2))
11692 0 : *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
11693 : else
11694 397934 : *t2 = TREE_TYPE (*t2);
11695 :
11696 397934 : casts_away_constness_r (t1, t2, complain);
11697 397934 : *t1 = build_pointer_type (*t1);
11698 397934 : *t2 = build_pointer_type (*t2);
11699 397934 : *t1 = cp_build_qualified_type (*t1, quals1);
11700 397934 : *t2 = cp_build_qualified_type (*t2, quals2);
11701 : }
11702 :
11703 : /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11704 : constness.
11705 :
11706 : ??? This function returns non-zero if casting away qualifiers not
11707 : just const. We would like to return to the caller exactly which
11708 : qualifiers are casted away to give more accurate diagnostics.
11709 : */
11710 :
11711 : static bool
11712 397561 : casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
11713 : {
11714 397561 : if (TYPE_REF_P (t2))
11715 : {
11716 : /* [expr.const.cast]
11717 :
11718 : Casting from an lvalue of type T1 to an lvalue of type T2
11719 : using a reference cast casts away constness if a cast from an
11720 : rvalue of type "pointer to T1" to the type "pointer to T2"
11721 : casts away constness. */
11722 0 : t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
11723 0 : return casts_away_constness (build_pointer_type (t1),
11724 0 : build_pointer_type (TREE_TYPE (t2)),
11725 0 : complain);
11726 : }
11727 :
11728 397561 : if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
11729 : /* [expr.const.cast]
11730 :
11731 : Casting from an rvalue of type "pointer to data member of X
11732 : of type T1" to the type "pointer to data member of Y of type
11733 : T2" casts away constness if a cast from an rvalue of type
11734 : "pointer to T1" to the type "pointer to T2" casts away
11735 : constness. */
11736 59 : return casts_away_constness
11737 59 : (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
11738 59 : build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
11739 59 : complain);
11740 :
11741 : /* Casting away constness is only something that makes sense for
11742 : pointer or reference types. */
11743 397502 : if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
11744 : return false;
11745 :
11746 : /* Top-level qualifiers don't matter. */
11747 397474 : t1 = TYPE_MAIN_VARIANT (t1);
11748 397474 : t2 = TYPE_MAIN_VARIANT (t2);
11749 397474 : casts_away_constness_r (&t1, &t2, complain);
11750 397474 : if (!can_convert (t2, t1, complain))
11751 : return true;
11752 :
11753 : return false;
11754 : }
11755 :
11756 : /* If T is a REFERENCE_TYPE return the type to which T refers.
11757 : Otherwise, return T itself. */
11758 :
11759 : tree
11760 1001340859 : non_reference (tree t)
11761 : {
11762 1001340859 : if (t && TYPE_REF_P (t))
11763 118154986 : t = TREE_TYPE (t);
11764 1001340859 : return t;
11765 : }
11766 :
11767 :
11768 : /* Return nonzero if REF is an lvalue valid for this language;
11769 : otherwise, print an error message and return zero. USE says
11770 : how the lvalue is being used and so selects the error message. */
11771 :
11772 : int
11773 18942154 : lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
11774 : {
11775 18942154 : cp_lvalue_kind kind = lvalue_kind (ref);
11776 :
11777 18942154 : if (kind == clk_none)
11778 : {
11779 145 : if (complain & tf_error)
11780 122 : lvalue_error (cp_expr_loc_or_input_loc (ref), use);
11781 145 : return 0;
11782 : }
11783 18942009 : else if (kind & (clk_rvalueref|clk_class))
11784 : {
11785 103 : if (!(complain & tf_error))
11786 : return 0;
11787 : /* Make this a permerror because we used to accept it. */
11788 20 : permerror (cp_expr_loc_or_input_loc (ref),
11789 : "using rvalue as lvalue");
11790 : }
11791 : return 1;
11792 : }
11793 :
11794 : /* Return true if a user-defined literal operator is a raw operator. */
11795 :
11796 : bool
11797 85509 : check_raw_literal_operator (const_tree decl)
11798 : {
11799 85509 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11800 85509 : tree argtype;
11801 85509 : int arity;
11802 85509 : bool maybe_raw_p = false;
11803 :
11804 : /* Count the number and type of arguments and check for ellipsis. */
11805 85509 : for (argtype = argtypes, arity = 0;
11806 128280 : argtype && argtype != void_list_node;
11807 42771 : ++arity, argtype = TREE_CHAIN (argtype))
11808 : {
11809 42771 : tree t = TREE_VALUE (argtype);
11810 :
11811 42771 : if (same_type_p (t, const_string_type_node))
11812 9 : maybe_raw_p = true;
11813 : }
11814 85509 : if (!argtype)
11815 : return false; /* Found ellipsis. */
11816 :
11817 85509 : if (!maybe_raw_p || arity != 1)
11818 85503 : return false;
11819 :
11820 : return true;
11821 : }
11822 :
11823 :
11824 : /* Return true if a user-defined literal operator has one of the allowed
11825 : argument types. */
11826 :
11827 : bool
11828 195250 : check_literal_operator_args (const_tree decl,
11829 : bool *long_long_unsigned_p, bool *long_double_p)
11830 : {
11831 195250 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11832 :
11833 195250 : *long_long_unsigned_p = false;
11834 195250 : *long_double_p = false;
11835 195250 : if (processing_template_decl || processing_specialization)
11836 42857 : return argtypes == void_list_node;
11837 : else
11838 : {
11839 : tree argtype;
11840 : int arity;
11841 : int max_arity = 2;
11842 :
11843 : /* Count the number and type of arguments and check for ellipsis. */
11844 152326 : for (argtype = argtypes, arity = 0;
11845 304719 : argtype && argtype != void_list_node;
11846 152326 : argtype = TREE_CHAIN (argtype))
11847 : {
11848 152399 : tree t = TREE_VALUE (argtype);
11849 152399 : ++arity;
11850 :
11851 152399 : if (TYPE_PTR_P (t))
11852 : {
11853 67939 : bool maybe_raw_p = false;
11854 67939 : t = TREE_TYPE (t);
11855 67939 : if (cp_type_quals (t) != TYPE_QUAL_CONST)
11856 : return false;
11857 67936 : t = TYPE_MAIN_VARIANT (t);
11858 67936 : if ((maybe_raw_p = same_type_p (t, char_type_node))
11859 51250 : || same_type_p (t, wchar_type_node)
11860 34661 : || same_type_p (t, char8_type_node)
11861 33190 : || same_type_p (t, char16_type_node)
11862 84531 : || same_type_p (t, char32_type_node))
11863 : {
11864 67933 : argtype = TREE_CHAIN (argtype);
11865 67933 : if (!argtype)
11866 : return false;
11867 67933 : t = TREE_VALUE (argtype);
11868 67933 : if (maybe_raw_p && argtype == void_list_node)
11869 : return true;
11870 67887 : else if (same_type_p (t, size_type_node))
11871 : {
11872 67881 : ++arity;
11873 67881 : continue;
11874 : }
11875 : else
11876 : return false;
11877 : }
11878 : }
11879 84460 : else if (same_type_p (t, long_long_unsigned_type_node))
11880 : {
11881 20873 : max_arity = 1;
11882 20873 : *long_long_unsigned_p = true;
11883 : }
11884 63587 : else if (same_type_p (t, long_double_type_node))
11885 : {
11886 63488 : max_arity = 1;
11887 63488 : *long_double_p = true;
11888 : }
11889 99 : else if (same_type_p (t, char_type_node))
11890 : max_arity = 1;
11891 55 : else if (same_type_p (t, wchar_type_node))
11892 : max_arity = 1;
11893 44 : else if (same_type_p (t, char8_type_node))
11894 : max_arity = 1;
11895 40 : else if (same_type_p (t, char16_type_node))
11896 : max_arity = 1;
11897 29 : else if (same_type_p (t, char32_type_node))
11898 : max_arity = 1;
11899 : else
11900 : return false;
11901 : }
11902 152320 : if (!argtype)
11903 : return false; /* Found ellipsis. */
11904 :
11905 152317 : if (arity != max_arity)
11906 : return false;
11907 :
11908 : return true;
11909 : }
11910 : }
11911 :
11912 : /* Always returns false since unlike C90, C++ has no concept of implicit
11913 : function declarations. */
11914 :
11915 : bool
11916 474 : c_decl_implicit (const_tree)
11917 : {
11918 474 : return false;
11919 : }
|