Line data Source code
1 : /* Build expressions with type checking for C++ compiler.
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 : Hacked by Michael Tiemann (tiemann@cygnus.com)
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 :
22 : /* This file is part of the C++ front end.
23 : It contains routines to build C++ expressions given their operands,
24 : including computing the types of the result, C and C++ specific error
25 : checks, and some optimization. */
26 :
27 : #include "config.h"
28 : #include "system.h"
29 : #include "coretypes.h"
30 : #include "target.h"
31 : #include "cp-tree.h"
32 : #include "stor-layout.h"
33 : #include "varasm.h"
34 : #include "intl.h"
35 : #include "convert.h"
36 : #include "c-family/c-objc.h"
37 : #include "c-family/c-ubsan.h"
38 : #include "c-family/c-type-mismatch.h"
39 : #include "stringpool.h"
40 : #include "attribs.h"
41 : #include "asan.h"
42 : #include "gimplify.h"
43 :
44 : static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
45 : static tree cp_build_function_call (tree, tree, tsubst_flags_t);
46 : static tree pfn_from_ptrmemfunc (tree);
47 : static tree delta_from_ptrmemfunc (tree);
48 : static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 : tsubst_flags_t, int);
50 : static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
51 : tsubst_flags_t);
52 : static tree rationalize_conditional_expr (enum tree_code, tree,
53 : tsubst_flags_t);
54 : static bool comp_ptr_ttypes_real (tree, tree, int);
55 : static bool comp_except_types (tree, tree, bool);
56 : static bool comp_array_types (const_tree, const_tree, compare_bounds_t, bool);
57 : static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
58 : static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59 : static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60 : static bool casts_away_constness (tree, tree, tsubst_flags_t);
61 : static bool maybe_warn_about_returning_address_of_local (tree, location_t = UNKNOWN_LOCATION);
62 : static void error_args_num (location_t, tree, bool);
63 : static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
64 : tsubst_flags_t);
65 : static bool is_std_move_p (tree);
66 : static bool is_std_forward_p (tree);
67 :
68 : /* Do `exp = require_complete_type (exp);' to make sure exp
69 : does not have an incomplete type. (That includes void types.)
70 : Returns error_mark_node if the VALUE does not have
71 : complete type when this function returns. */
72 :
73 : tree
74 150232759 : require_complete_type (tree value,
75 : tsubst_flags_t complain /* = tf_warning_or_error */)
76 : {
77 150232759 : tree type;
78 :
79 150232759 : if (processing_template_decl || value == error_mark_node)
80 : return value;
81 :
82 139556660 : if (TREE_CODE (value) == OVERLOAD)
83 0 : type = unknown_type_node;
84 : else
85 139556660 : type = TREE_TYPE (value);
86 :
87 139556660 : if (type == error_mark_node)
88 : return error_mark_node;
89 :
90 : /* First, detect a valid value with a complete type. */
91 139556648 : if (COMPLETE_TYPE_P (type))
92 : return value;
93 :
94 134885 : if (complete_type_or_maybe_complain (type, value, complain))
95 : return value;
96 : else
97 213 : 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 15855329767 : complete_type (tree type)
107 : {
108 15855329767 : 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 15855329767 : if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 : ;
115 5178983686 : else if (TREE_CODE (type) == ARRAY_TYPE)
116 : {
117 804701 : tree t = complete_type (TREE_TYPE (type));
118 804701 : unsigned int needs_constructing, has_nontrivial_dtor;
119 804701 : if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 785751 : layout_type (type);
121 804701 : needs_constructing
122 804701 : = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 804701 : has_nontrivial_dtor
124 804701 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 2661428 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 : {
127 1856727 : TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 1856727 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 : }
130 : }
131 5178178985 : else if (CLASS_TYPE_P (type))
132 : {
133 5082517624 : if (modules_p ())
134 : /* TYPE could be a class member we've not loaded the definition of. */
135 19257055 : lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
136 :
137 5082517624 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 975625991 : 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 1710613662 : complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 : {
151 1710613662 : type = complete_type (type);
152 1710613659 : if (type == error_mark_node)
153 : /* We already issued an error. */
154 : return NULL_TREE;
155 1710613452 : else if (!COMPLETE_TYPE_P (type))
156 : {
157 3984 : if (complain & tf_error)
158 476 : cxx_incomplete_type_diagnostic (value, type, diagnostics::kind::error);
159 3984 : note_failed_type_completion (type, complain);
160 3984 : return NULL_TREE;
161 : }
162 : else
163 : return type;
164 : }
165 :
166 : tree
167 172079743 : complete_type_or_else (tree type, tree value)
168 : {
169 172079743 : 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 4327890 : commonparms (tree p1, tree p2)
182 : {
183 4327890 : tree oldargs = p1, newargs, n;
184 4327890 : int i, len;
185 4327890 : int any_change = 0;
186 :
187 4327890 : len = list_length (p1);
188 4327890 : newargs = tree_last (p1);
189 :
190 4327890 : if (newargs == void_list_node)
191 : i = 1;
192 : else
193 : {
194 41619 : i = 0;
195 41619 : newargs = 0;
196 : }
197 :
198 14324724 : for (; i < len; i++)
199 9996834 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200 :
201 : n = newargs;
202 :
203 18610995 : for (i = 0; p1;
204 14283105 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 : {
206 14283312 : if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 : {
208 174 : TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 174 : any_change = 1;
210 : }
211 14282931 : else if (! TREE_PURPOSE (p1))
212 : {
213 14282898 : if (TREE_PURPOSE (p2))
214 : {
215 434967 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 434967 : any_change = 1;
217 : }
218 : }
219 : else
220 : {
221 33 : if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
222 33 : any_change = 1;
223 33 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 : }
225 14283105 : if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 : {
227 4000109 : any_change = 1;
228 4000109 : TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 : }
230 : else
231 10282996 : TREE_VALUE (n) = TREE_VALUE (p1);
232 : }
233 4327890 : if (! any_change)
234 1450324 : 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 33901128 : original_type (tree t)
243 : {
244 33901128 : int quals = cp_type_quals (t);
245 33901128 : while (t != error_mark_node
246 35072381 : && TYPE_NAME (t) != NULL_TREE)
247 : {
248 11832187 : tree x = TYPE_NAME (t);
249 11832187 : if (TREE_CODE (x) != TYPE_DECL)
250 : break;
251 11832187 : x = DECL_ORIGINAL_TYPE (x);
252 11832187 : if (x == NULL_TREE)
253 : break;
254 : t = x;
255 : }
256 33901128 : 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 59861 : merge_type_attributes_from (tree type, tree other_type)
264 : {
265 59861 : tree attrs = targetm.merge_type_attributes (type, other_type);
266 59861 : attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
267 59861 : 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 7153985 : cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
280 : {
281 7153985 : tree mv1 = TYPE_MAIN_VARIANT (t1);
282 7153985 : tree mv2 = TYPE_MAIN_VARIANT (t2);
283 7153985 : int extended1 = 0;
284 7153985 : int extended2 = 0;
285 :
286 7153985 : if (mv1 == mv2)
287 : return 0;
288 :
289 57212552 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
290 : {
291 50060983 : if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 3207117 : extended1 = i + 1;
293 50060983 : if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 2696361 : extended2 = i + 1;
295 : }
296 7151569 : if (mv1 == bfloat16_type_node)
297 746554 : extended1 = true;
298 7151569 : if (mv2 == bfloat16_type_node)
299 599295 : extended2 = true;
300 7151569 : if (extended2 && !extended1)
301 : {
302 3157280 : int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 3157280 : return ret == 3 ? 3 : -ret;
304 : }
305 :
306 3994289 : const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 3994289 : const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 : /* Currently, extended floating point types are only binary, and
309 : they never have a proper subset or superset of values with
310 : decimal floating point types except for the _Float16 vs. _Decimal128
311 : pair, so return 3 for unordered conversion ranks. */
312 3994289 : gcc_assert (fmt1->b == 2);
313 3994289 : if (fmt2->b == 10)
314 : {
315 : /* _Float16 needs at most 21 decimal digits (e.g.
316 : 0x1.a3cp-14f16 is exactly 0.000100076198577880859375DL),
317 : so it is not a proper subset of _Decimal64 but is subset
318 : of _Decimal128. While std::bfloat16_t needs at most 96
319 : decimal digits, so even _Decimal128 doesn't cover it.
320 : _Float32 has at least one value which needs 112 decimal
321 : digits, _Float64 at least 767 decimal digits. */
322 33 : if (fmt1->emin == -13
323 15 : && fmt1->emax == 16
324 15 : && fmt1->p == 11
325 15 : && fmt2->emin == -6142
326 15 : && fmt2->emax == 6145
327 15 : && fmt2->p == 34)
328 : return -2;
329 : return 3;
330 : }
331 3994256 : gcc_assert (fmt2->b == 2);
332 : /* For {ibm,mips}_extended_format formats, the type has variable
333 : precision up to ~2150 bits when the first double is around maximum
334 : representable double and second double is subnormal minimum.
335 : So, e.g. for __ibm128 vs. std::float128_t, they have unordered
336 : ranks. */
337 31954048 : int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
338 7988512 : ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
339 31954048 : int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
340 7988512 : ? fmt2->emax - fmt2->emin + fmt2->p - 1 : fmt2->p);
341 : /* The rank of a floating point type T is greater than the rank of
342 : any floating-point type whose set of values is a proper subset
343 : of the set of values of T. */
344 3994256 : if ((p1 > p2 && fmt1->emax >= fmt2->emax)
345 3109549 : || (p1 == p2 && fmt1->emax > fmt2->emax))
346 : return 2;
347 3109549 : if ((p1 < p2 && fmt1->emax <= fmt2->emax)
348 1044934 : || (p1 == p2 && fmt1->emax < fmt2->emax))
349 : return -2;
350 1044934 : if ((p1 > p2 && fmt1->emax < fmt2->emax)
351 1038841 : || (p1 < p2 && fmt1->emax > fmt2->emax))
352 : return 3;
353 1032850 : if (!extended1 && !extended2)
354 : {
355 : /* The rank of long double is greater than the rank of double, which
356 : is greater than the rank of float. */
357 0 : if (t1 == long_double_type_node)
358 : return 2;
359 0 : else if (t2 == long_double_type_node)
360 : return -2;
361 0 : if (t1 == double_type_node)
362 : return 2;
363 0 : else if (t2 == double_type_node)
364 : return -2;
365 0 : if (t1 == float_type_node)
366 : return 2;
367 0 : else if (t2 == float_type_node)
368 : return -2;
369 : return 0;
370 : }
371 : /* Two extended floating-point types with the same set of values have equal
372 : ranks. */
373 1032850 : if (extended1 && extended2)
374 : {
375 6 : if ((extended1 <= NUM_FLOATN_TYPES) == (extended2 <= NUM_FLOATN_TYPES))
376 : {
377 : /* Prefer higher extendedN value. */
378 0 : if (extended1 > extended2)
379 : return 1;
380 0 : else if (extended1 < extended2)
381 : return -1;
382 : else
383 : return 0;
384 : }
385 6 : else if (extended1 <= NUM_FLOATN_TYPES)
386 : /* Prefer _FloatN type over _FloatMx type. */
387 : return 1;
388 3 : else if (extended2 <= NUM_FLOATN_TYPES)
389 : return -1;
390 : else
391 : return 0;
392 : }
393 :
394 : /* gcc_assert (extended1 && !extended2); */
395 : tree *p;
396 : int cnt = 0;
397 4131376 : for (p = &float_type_node; p <= &long_double_type_node; ++p)
398 : {
399 3098532 : const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
400 3098532 : gcc_assert (fmt3->b == 2);
401 24788256 : int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
402 6197064 : ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
403 3098532 : if (p1 == p3 && fmt1->emax == fmt3->emax)
404 916535 : ++cnt;
405 : }
406 : /* An extended floating-point type with the same set of values
407 : as exactly one cv-unqualified standard floating-point type
408 : has a rank equal to the rank of that standard floating-point
409 : type.
410 :
411 : An extended floating-point type with the same set of values
412 : as more than one cv-unqualified standard floating-point type
413 : has a rank equal to the rank of double.
414 :
415 : Thus, if the latter is true and t2 is long double, t2
416 : has higher rank. */
417 1032844 : if (cnt > 1 && mv2 == long_double_type_node)
418 : return -2;
419 : /* And similarly if t2 is float, t2 has lower rank. */
420 0 : if (cnt > 1 && mv2 == float_type_node)
421 : return 2;
422 : /* Otherwise, they have equal rank, but extended types
423 : (other than std::bfloat16_t) have higher subrank.
424 : std::bfloat16_t shouldn't have equal rank to any standard
425 : floating point type. */
426 : return 1;
427 : }
428 :
429 : /* Return the common type for two arithmetic types T1 and T2 under the
430 : usual arithmetic conversions. The default conversions have already
431 : been applied, and enumerated types converted to their compatible
432 : integer types. */
433 :
434 : static tree
435 114694707 : cp_common_type (tree t1, tree t2)
436 : {
437 114694707 : enum tree_code code1 = TREE_CODE (t1);
438 114694707 : enum tree_code code2 = TREE_CODE (t2);
439 114694707 : tree attributes;
440 114694707 : int i;
441 :
442 :
443 : /* In what follows, we slightly generalize the rules given in [expr] so
444 : as to deal with `long long' and `complex'. First, merge the
445 : attributes. */
446 114694707 : attributes = (*targetm.merge_type_attributes) (t1, t2);
447 :
448 114694707 : if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
449 : {
450 2115904 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
451 2115904 : return build_type_attribute_variant (t1, attributes);
452 : else
453 : return NULL_TREE;
454 : }
455 :
456 : /* FIXME: Attributes. */
457 112578803 : gcc_assert (ARITHMETIC_TYPE_P (t1)
458 : || VECTOR_TYPE_P (t1)
459 : || UNSCOPED_ENUM_P (t1));
460 112578803 : gcc_assert (ARITHMETIC_TYPE_P (t2)
461 : || VECTOR_TYPE_P (t2)
462 : || UNSCOPED_ENUM_P (t2));
463 :
464 : /* If one type is complex, form the common type of the non-complex
465 : components, then make that complex. Use T1 or T2 if it is the
466 : required type. */
467 112578803 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
468 : {
469 222602 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
470 222602 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
471 222602 : tree subtype
472 222602 : = type_after_usual_arithmetic_conversions (subtype1, subtype2);
473 :
474 222602 : if (subtype == error_mark_node)
475 : return subtype;
476 222602 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
477 189194 : return build_type_attribute_variant (t1, attributes);
478 33408 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
479 33348 : return build_type_attribute_variant (t2, attributes);
480 : else
481 60 : return build_type_attribute_variant (build_complex_type (subtype),
482 60 : attributes);
483 : }
484 :
485 112356201 : if (code1 == VECTOR_TYPE)
486 : {
487 : /* When we get here we should have two vectors of the same size.
488 : Just prefer the unsigned one if present. */
489 59861 : if (TYPE_UNSIGNED (t1))
490 12887 : return merge_type_attributes_from (t1, t2);
491 : else
492 46974 : return merge_type_attributes_from (t2, t1);
493 : }
494 :
495 : /* If only one is real, use it as the result. */
496 112296340 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
497 1189998 : return build_type_attribute_variant (t1, attributes);
498 111106342 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
499 1041373 : return build_type_attribute_variant (t2, attributes);
500 :
501 110064969 : if (code1 == REAL_TYPE
502 110064969 : && (extended_float_type_p (t1) || extended_float_type_p (t2)))
503 : {
504 135984 : tree mv1 = TYPE_MAIN_VARIANT (t1);
505 135984 : tree mv2 = TYPE_MAIN_VARIANT (t2);
506 135984 : if (mv1 == mv2)
507 133027 : return build_type_attribute_variant (t1, attributes);
508 :
509 2957 : int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
510 2957 : if (cmpret == 3)
511 6 : return error_mark_node;
512 2951 : else if (cmpret >= 0)
513 2576 : return build_type_attribute_variant (t1, attributes);
514 : else
515 375 : return build_type_attribute_variant (t2, attributes);
516 : }
517 :
518 : /* Both real or both integers; use the one with greater precision. */
519 109928985 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
520 17469082 : return build_type_attribute_variant (t1, attributes);
521 92459903 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
522 1992840 : return build_type_attribute_variant (t2, attributes);
523 :
524 : /* The types are the same; no need to do anything fancy. */
525 90467063 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
526 82979539 : return build_type_attribute_variant (t1, attributes);
527 :
528 7487524 : if (code1 != REAL_TYPE)
529 : {
530 : /* If one is unsigned long long, then convert the other to unsigned
531 : long long. */
532 7487521 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
533 7487521 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
534 158555 : return build_type_attribute_variant (long_long_unsigned_type_node,
535 158555 : attributes);
536 : /* If one is a long long, and the other is an unsigned long, and
537 : long long can represent all the values of an unsigned long, then
538 : convert to a long long. Otherwise, convert to an unsigned long
539 : long. Otherwise, if either operand is long long, convert the
540 : other to long long.
541 :
542 : Since we're here, we know the TYPE_PRECISION is the same;
543 : therefore converting to long long cannot represent all the values
544 : of an unsigned long, so we choose unsigned long long in that
545 : case. */
546 7328966 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
547 7328966 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
548 : {
549 1356 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
550 684 : ? long_long_unsigned_type_node
551 684 : : long_long_integer_type_node);
552 684 : return build_type_attribute_variant (t, attributes);
553 : }
554 :
555 : /* Go through the same procedure, but for longs. */
556 7328282 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
557 7328282 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
558 936073 : return build_type_attribute_variant (long_unsigned_type_node,
559 936073 : attributes);
560 6392209 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
561 6392209 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
562 : {
563 75751 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
564 75751 : ? long_unsigned_type_node : long_integer_type_node);
565 38350 : return build_type_attribute_variant (t, attributes);
566 : }
567 :
568 : /* For __intN types, either the type is __int128 (and is lower
569 : priority than the types checked above, but higher than other
570 : 128-bit types) or it's known to not be the same size as other
571 : types (enforced in toplev.cc). Prefer the unsigned type. */
572 12707686 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
573 : {
574 6353859 : if (int_n_enabled_p [i]
575 6353859 : && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
576 6265215 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
577 6265211 : || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
578 6265211 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
579 : {
580 60 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
581 32 : ? int_n_trees[i].unsigned_type
582 32 : : int_n_trees[i].signed_type);
583 32 : return build_type_attribute_variant (t, attributes);
584 : }
585 : }
586 :
587 : /* Otherwise prefer the unsigned one. */
588 6353827 : if (TYPE_UNSIGNED (t1))
589 5133037 : return build_type_attribute_variant (t1, attributes);
590 : else
591 1220790 : return build_type_attribute_variant (t2, attributes);
592 : }
593 : else
594 : {
595 3 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
596 3 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
597 0 : return build_type_attribute_variant (long_double_type_node,
598 0 : attributes);
599 3 : if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
600 3 : || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
601 3 : return build_type_attribute_variant (double_type_node,
602 3 : attributes);
603 0 : if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
604 0 : || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
605 0 : return build_type_attribute_variant (float_type_node,
606 0 : attributes);
607 :
608 : /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
609 : the standard C++ floating-point types. Logic earlier in this
610 : function has already eliminated the possibility that
611 : TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
612 : compelling reason to choose one or the other. */
613 0 : return build_type_attribute_variant (t1, attributes);
614 : }
615 : }
616 :
617 : /* T1 and T2 are arithmetic or enumeration types. Return the type
618 : that will result from the "usual arithmetic conversions" on T1 and
619 : T2 as described in [expr]. */
620 :
621 : tree
622 1211250 : type_after_usual_arithmetic_conversions (tree t1, tree t2)
623 : {
624 1211250 : gcc_assert (ARITHMETIC_TYPE_P (t1)
625 : || VECTOR_TYPE_P (t1)
626 : || UNSCOPED_ENUM_P (t1));
627 1211250 : gcc_assert (ARITHMETIC_TYPE_P (t2)
628 : || VECTOR_TYPE_P (t2)
629 : || UNSCOPED_ENUM_P (t2));
630 :
631 : /* Perform the integral promotions. We do not promote real types here. */
632 1211250 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
633 988116 : && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
634 : {
635 987797 : t1 = type_promotes_to (t1);
636 987797 : t2 = type_promotes_to (t2);
637 : }
638 :
639 1211250 : return cp_common_type (t1, t2);
640 : }
641 :
642 : static void
643 42 : composite_pointer_error (const op_location_t &location,
644 : enum diagnostics::kind kind, tree t1, tree t2,
645 : composite_pointer_operation operation)
646 : {
647 42 : switch (operation)
648 : {
649 33 : case CPO_COMPARISON:
650 33 : emit_diagnostic (kind, location, 0,
651 : "comparison between "
652 : "distinct pointer types %qT and %qT lacks a cast",
653 : t1, t2);
654 33 : break;
655 0 : case CPO_CONVERSION:
656 0 : emit_diagnostic (kind, location, 0,
657 : "conversion between "
658 : "distinct pointer types %qT and %qT lacks a cast",
659 : t1, t2);
660 0 : break;
661 9 : case CPO_CONDITIONAL_EXPR:
662 9 : emit_diagnostic (kind, location, 0,
663 : "conditional expression between "
664 : "distinct pointer types %qT and %qT lacks a cast",
665 : t1, t2);
666 9 : break;
667 0 : default:
668 0 : gcc_unreachable ();
669 : }
670 42 : }
671 :
672 : /* Subroutine of composite_pointer_type to implement the recursive
673 : case. See that function for documentation of the parameters. And ADD_CONST
674 : is used to track adding "const" where needed. */
675 :
676 : static tree
677 6266570 : composite_pointer_type_r (const op_location_t &location,
678 : tree t1, tree t2, bool *add_const,
679 : composite_pointer_operation operation,
680 : tsubst_flags_t complain)
681 : {
682 6266570 : tree pointee1;
683 6266570 : tree pointee2;
684 6266570 : tree result_type;
685 6266570 : tree attributes;
686 :
687 : /* Determine the types pointed to by T1 and T2. */
688 6266570 : if (TYPE_PTR_P (t1))
689 : {
690 6265999 : pointee1 = TREE_TYPE (t1);
691 6265999 : pointee2 = TREE_TYPE (t2);
692 : }
693 : else
694 : {
695 571 : pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
696 571 : pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
697 : }
698 :
699 : /* [expr.type]
700 :
701 : If T1 and T2 are similar types, the result is the cv-combined type of
702 : T1 and T2. */
703 6266570 : if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
704 : result_type = pointee1;
705 33 : else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
706 134 : || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
707 : {
708 33 : result_type = composite_pointer_type_r (location, pointee1, pointee2,
709 : add_const, operation, complain);
710 33 : if (result_type == error_mark_node)
711 : return error_mark_node;
712 : }
713 : else
714 : {
715 101 : if (complain & tf_error)
716 30 : composite_pointer_error (location, diagnostics::kind::permerror,
717 : t1, t2, operation);
718 : else
719 71 : return error_mark_node;
720 30 : result_type = void_type_node;
721 : }
722 6266493 : const int q1 = cp_type_quals (pointee1);
723 6266493 : const int q2 = cp_type_quals (pointee2);
724 6266493 : const int quals = q1 | q2;
725 6266493 : result_type = cp_build_qualified_type (result_type,
726 6266493 : (quals | (*add_const
727 6266493 : ? TYPE_QUAL_CONST
728 : : TYPE_UNQUALIFIED)));
729 : /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
730 : the TLQ). The reason is that both T1 and T2 can then be converted to the
731 : cv-combined type of T1 and T2. */
732 6266493 : if (quals != q1 || quals != q2)
733 762132 : *add_const = true;
734 : /* If the original types were pointers to members, so is the
735 : result. */
736 6266493 : if (TYPE_PTRMEM_P (t1))
737 : {
738 557 : if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
739 : TYPE_PTRMEM_CLASS_TYPE (t2)))
740 : {
741 0 : if (complain & tf_error)
742 0 : composite_pointer_error (location, diagnostics::kind::permerror,
743 : t1, t2, operation);
744 : else
745 0 : return error_mark_node;
746 : }
747 557 : result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
748 : result_type);
749 : }
750 : else
751 6265936 : result_type = build_pointer_type (result_type);
752 :
753 : /* Merge the attributes. */
754 6266493 : attributes = (*targetm.merge_type_attributes) (t1, t2);
755 6266493 : return build_type_attribute_variant (result_type, attributes);
756 : }
757 :
758 : /* Return the composite pointer type (see [expr.type]) for T1 and T2.
759 : ARG1 and ARG2 are the values with those types. The OPERATION is to
760 : describe the operation between the pointer types,
761 : in case an error occurs.
762 :
763 : This routine also implements the computation of a common type for
764 : pointers-to-members as per [expr.eq]. */
765 :
766 : tree
767 6738587 : composite_pointer_type (const op_location_t &location,
768 : tree t1, tree t2, tree arg1, tree arg2,
769 : composite_pointer_operation operation,
770 : tsubst_flags_t complain)
771 : {
772 6738587 : tree class1;
773 6738587 : tree class2;
774 :
775 : /* [expr.type]
776 :
777 : If one operand is a null pointer constant, the composite pointer
778 : type is the type of the other operand. */
779 6738587 : if (null_ptr_cst_p (arg1))
780 : return t2;
781 6738412 : if (null_ptr_cst_p (arg2))
782 : return t1;
783 :
784 : /* We have:
785 :
786 : [expr.type]
787 :
788 : If one of the operands has type "pointer to cv1 void", then
789 : the other has type "pointer to cv2 T", and the composite pointer
790 : type is "pointer to cv12 void", where cv12 is the union of cv1
791 : and cv2.
792 :
793 : If either type is a pointer to void, make sure it is T1. */
794 6716599 : if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
795 : std::swap (t1, t2);
796 :
797 : /* Now, if T1 is a pointer to void, merge the qualifiers. */
798 6716599 : if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
799 : {
800 449957 : tree attributes;
801 449957 : tree result_type;
802 :
803 449957 : if (TYPE_PTRFN_P (t2))
804 : {
805 11 : if (complain & tf_error)
806 : {
807 9 : switch (operation)
808 : {
809 9 : case CPO_COMPARISON:
810 9 : pedwarn (location, OPT_Wpedantic,
811 : "ISO C++ forbids comparison between pointer "
812 : "of type %<void *%> and pointer-to-function");
813 9 : break;
814 0 : case CPO_CONVERSION:
815 0 : pedwarn (location, OPT_Wpedantic,
816 : "ISO C++ forbids conversion between pointer "
817 : "of type %<void *%> and pointer-to-function");
818 0 : break;
819 0 : case CPO_CONDITIONAL_EXPR:
820 0 : pedwarn (location, OPT_Wpedantic,
821 : "ISO C++ forbids conditional expression between "
822 : "pointer of type %<void *%> and "
823 : "pointer-to-function");
824 0 : break;
825 0 : default:
826 0 : gcc_unreachable ();
827 : }
828 : }
829 : else
830 2 : return error_mark_node;
831 : }
832 449955 : result_type
833 449955 : = cp_build_qualified_type (void_type_node,
834 449955 : (cp_type_quals (TREE_TYPE (t1))
835 449955 : | cp_type_quals (TREE_TYPE (t2))));
836 449955 : result_type = build_pointer_type (result_type);
837 : /* Merge the attributes. */
838 449955 : attributes = (*targetm.merge_type_attributes) (t1, t2);
839 449955 : return build_type_attribute_variant (result_type, attributes);
840 : }
841 :
842 6266642 : if (c_dialect_objc () && TYPE_PTR_P (t1)
843 0 : && TYPE_PTR_P (t2))
844 : {
845 0 : if (objc_have_common_type (t1, t2, -3, NULL_TREE))
846 0 : return objc_common_type (t1, t2);
847 : }
848 :
849 : /* if T1 or T2 is "pointer to noexcept function" and the other type is
850 : "pointer to function", where the function types are otherwise the same,
851 : "pointer to function" */
852 6266642 : if (fnptr_conv_p (t1, t2))
853 : return t1;
854 6266607 : if (fnptr_conv_p (t2, t1))
855 : return t2;
856 :
857 : /* [expr.eq] permits the application of a pointer conversion to
858 : bring the pointers to a common type. */
859 6265984 : if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
860 6265984 : && CLASS_TYPE_P (TREE_TYPE (t1))
861 969023 : && CLASS_TYPE_P (TREE_TYPE (t2))
862 7235596 : && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
863 969020 : TREE_TYPE (t2)))
864 : {
865 30377 : class1 = TREE_TYPE (t1);
866 30377 : class2 = TREE_TYPE (t2);
867 :
868 30377 : if (DERIVED_FROM_P (class1, class2))
869 3184 : t2 = (build_pointer_type
870 1592 : (cp_build_qualified_type (class1, cp_type_quals (class2))));
871 28785 : else if (DERIVED_FROM_P (class2, class1))
872 57534 : t1 = (build_pointer_type
873 28767 : (cp_build_qualified_type (class2, cp_type_quals (class1))));
874 : else
875 : {
876 18 : if (complain & tf_error)
877 12 : composite_pointer_error (location, diagnostics::kind::error,
878 : t1, t2, operation);
879 18 : return error_mark_node;
880 : }
881 : }
882 : /* [expr.eq] permits the application of a pointer-to-member
883 : conversion to change the class type of one of the types. */
884 6235760 : else if (TYPE_PTRMEM_P (t1)
885 6236352 : && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
886 : TYPE_PTRMEM_CLASS_TYPE (t2)))
887 : {
888 79 : class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
889 79 : class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
890 :
891 79 : if (DERIVED_FROM_P (class1, class2))
892 43 : t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
893 36 : else if (DERIVED_FROM_P (class2, class1))
894 15 : t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
895 : else
896 : {
897 21 : if (complain & tf_error)
898 3 : switch (operation)
899 : {
900 3 : case CPO_COMPARISON:
901 3 : error_at (location, "comparison between distinct "
902 : "pointer-to-member types %qT and %qT lacks a cast",
903 : t1, t2);
904 3 : break;
905 0 : case CPO_CONVERSION:
906 0 : error_at (location, "conversion between distinct "
907 : "pointer-to-member types %qT and %qT lacks a cast",
908 : t1, t2);
909 0 : break;
910 0 : case CPO_CONDITIONAL_EXPR:
911 0 : error_at (location, "conditional expression between distinct "
912 : "pointer-to-member types %qT and %qT lacks a cast",
913 : t1, t2);
914 0 : break;
915 0 : default:
916 0 : gcc_unreachable ();
917 : }
918 21 : return error_mark_node;
919 : }
920 : }
921 :
922 6266537 : bool add_const = false;
923 6266537 : return composite_pointer_type_r (location, t1, t2, &add_const, operation,
924 6266537 : complain);
925 : }
926 :
927 : /* Return the merged type of two types.
928 : We assume that comptypes has already been done and returned 1;
929 : if that isn't so, this may crash.
930 :
931 : This just combines attributes and default arguments; any other
932 : differences would cause the two types to compare unalike. */
933 :
934 : tree
935 30924264 : merge_types (tree t1, tree t2)
936 : {
937 30924264 : enum tree_code code1;
938 30924264 : enum tree_code code2;
939 30924264 : tree attributes;
940 :
941 : /* Save time if the two types are the same. */
942 30924264 : if (t1 == t2)
943 : return t1;
944 16950564 : if (original_type (t1) == original_type (t2))
945 : return t1;
946 :
947 : /* If one type is nonsense, use the other. */
948 16824655 : if (t1 == error_mark_node)
949 : return t2;
950 16824655 : if (t2 == error_mark_node)
951 : return t1;
952 :
953 : /* Handle merging an auto redeclaration with a previous deduced
954 : return type. */
955 16824655 : if (is_auto (t1))
956 : return t2;
957 :
958 : /* Merge the attributes. */
959 16809994 : attributes = (*targetm.merge_type_attributes) (t1, t2);
960 :
961 16809994 : if (TYPE_PTRMEMFUNC_P (t1))
962 15 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
963 16809994 : if (TYPE_PTRMEMFUNC_P (t2))
964 15 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
965 :
966 16809994 : code1 = TREE_CODE (t1);
967 16809994 : code2 = TREE_CODE (t2);
968 16809994 : if (code1 != code2)
969 : {
970 0 : gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
971 0 : if (code1 == TYPENAME_TYPE)
972 : {
973 0 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
974 0 : code1 = TREE_CODE (t1);
975 : }
976 : else
977 : {
978 0 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
979 0 : code2 = TREE_CODE (t2);
980 : }
981 : }
982 :
983 16809994 : switch (code1)
984 : {
985 2862264 : case POINTER_TYPE:
986 2862264 : case REFERENCE_TYPE:
987 : /* For two pointers, do this recursively on the target type. */
988 2862264 : {
989 2862264 : tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
990 2862264 : int quals = cp_type_quals (t1);
991 :
992 2862264 : if (code1 == POINTER_TYPE)
993 : {
994 782867 : t1 = build_pointer_type (target);
995 782867 : if (TREE_CODE (target) == METHOD_TYPE)
996 15 : t1 = build_ptrmemfunc_type (t1);
997 : }
998 : else
999 2079397 : t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
1000 2862264 : t1 = build_type_attribute_variant (t1, attributes);
1001 2862264 : t1 = cp_build_qualified_type (t1, quals);
1002 :
1003 2862264 : return t1;
1004 : }
1005 :
1006 12 : case OFFSET_TYPE:
1007 12 : {
1008 12 : int quals;
1009 12 : tree pointee;
1010 12 : quals = cp_type_quals (t1);
1011 12 : pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
1012 12 : TYPE_PTRMEM_POINTED_TO_TYPE (t2));
1013 12 : t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
1014 : pointee);
1015 12 : t1 = cp_build_qualified_type (t1, quals);
1016 12 : break;
1017 : }
1018 :
1019 10676 : case ARRAY_TYPE:
1020 10676 : {
1021 10676 : tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1022 : /* Save space: see if the result is identical to one of the args. */
1023 21352 : if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
1024 10651 : return build_type_attribute_variant (t1, attributes);
1025 44 : if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
1026 19 : return build_type_attribute_variant (t2, attributes);
1027 : /* Merge the element types, and have a size if either arg has one. */
1028 6 : t1 = build_cplus_array_type
1029 6 : (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
1030 6 : break;
1031 : }
1032 :
1033 4482497 : case FUNCTION_TYPE:
1034 : /* Function types: prefer the one that specified arg types.
1035 : If both do, merge the arg types. Also merge the return types. */
1036 4482497 : {
1037 4482497 : tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1038 4482497 : tree p1 = TYPE_ARG_TYPES (t1);
1039 4482497 : tree p2 = TYPE_ARG_TYPES (t2);
1040 4482497 : tree parms;
1041 :
1042 : /* Save space: see if the result is identical to one of the args. */
1043 4482497 : if (valtype == TREE_TYPE (t1) && ! p2)
1044 0 : return cp_build_type_attribute_variant (t1, attributes);
1045 4482497 : if (valtype == TREE_TYPE (t2) && ! p1)
1046 0 : return cp_build_type_attribute_variant (t2, attributes);
1047 :
1048 : /* Simple way if one arg fails to specify argument types. */
1049 8964994 : if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1050 : parms = p2;
1051 8655780 : else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1052 : parms = p1;
1053 : else
1054 4327890 : parms = commonparms (p1, p2);
1055 :
1056 4482497 : cp_cv_quals quals = type_memfn_quals (t1);
1057 4482497 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1058 4482497 : gcc_assert (quals == type_memfn_quals (t2));
1059 4482497 : gcc_assert (rqual == type_memfn_rqual (t2));
1060 :
1061 4482497 : tree rval = cp_build_function_type (valtype, parms);
1062 4482497 : rval = apply_memfn_quals (rval, quals);
1063 4482497 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1064 4482497 : TYPE_RAISES_EXCEPTIONS (t2));
1065 4482497 : bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1066 4482497 : t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1067 4482497 : break;
1068 : }
1069 :
1070 4076949 : case METHOD_TYPE:
1071 4076949 : {
1072 : /* Get this value the long way, since TYPE_METHOD_BASETYPE
1073 : is just the main variant of this. */
1074 4076949 : tree basetype = class_of_this_parm (t2);
1075 4076949 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1076 4076949 : TYPE_RAISES_EXCEPTIONS (t2));
1077 4076949 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1078 4076949 : tree t3;
1079 4076949 : bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1080 :
1081 : /* If this was a member function type, get back to the
1082 : original type of type member function (i.e., without
1083 : the class instance variable up front. */
1084 4076949 : t1 = cp_build_function_type (TREE_TYPE (t1),
1085 4076949 : TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1086 4076949 : t2 = cp_build_function_type (TREE_TYPE (t2),
1087 4076949 : TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1088 4076949 : t3 = merge_types (t1, t2);
1089 4076949 : t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1090 4076949 : TYPE_ARG_TYPES (t3));
1091 4076949 : t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1092 4076949 : break;
1093 : }
1094 :
1095 : case TYPENAME_TYPE:
1096 : /* There is no need to merge attributes into a TYPENAME_TYPE.
1097 : When the type is instantiated it will have whatever
1098 : attributes result from the instantiation. */
1099 : return t1;
1100 :
1101 5367309 : default:;
1102 5367309 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1103 : return t1;
1104 12 : else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1105 : return t2;
1106 : break;
1107 : }
1108 :
1109 8559464 : return cp_build_type_attribute_variant (t1, attributes);
1110 : }
1111 :
1112 : /* Return the ARRAY_TYPE type without its domain. */
1113 :
1114 : tree
1115 391 : strip_array_domain (tree type)
1116 : {
1117 391 : tree t2;
1118 391 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1119 391 : if (TYPE_DOMAIN (type) == NULL_TREE)
1120 : return type;
1121 226 : t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
1122 226 : return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
1123 : }
1124 :
1125 : /* Wrapper around cp_common_type that is used by c-common.cc and other
1126 : front end optimizations that remove promotions.
1127 :
1128 : Return the common type for two arithmetic types T1 and T2 under the
1129 : usual arithmetic conversions. The default conversions have already
1130 : been applied, and enumerated types converted to their compatible
1131 : integer types. */
1132 :
1133 : tree
1134 655285 : common_type (tree t1, tree t2)
1135 : {
1136 : /* If one type is nonsense, use the other */
1137 655285 : if (t1 == error_mark_node)
1138 : return t2;
1139 655285 : if (t2 == error_mark_node)
1140 : return t1;
1141 :
1142 655285 : return cp_common_type (t1, t2);
1143 : }
1144 :
1145 : /* Return the common type of two pointer types T1 and T2. This is the
1146 : type for the result of most arithmetic operations if the operands
1147 : have the given two types.
1148 :
1149 : We assume that comp_target_types has already been done and returned
1150 : nonzero; if that isn't so, this may crash. */
1151 :
1152 : tree
1153 1552709 : common_pointer_type (tree t1, tree t2)
1154 : {
1155 1552709 : gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
1156 : || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
1157 : || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
1158 :
1159 1552709 : return composite_pointer_type (input_location, t1, t2,
1160 : error_mark_node, error_mark_node,
1161 1552709 : CPO_CONVERSION, tf_warning_or_error);
1162 : }
1163 :
1164 : /* Compare two exception specifier types for exactness or subsetness, if
1165 : allowed. Returns false for mismatch, true for match (same, or
1166 : derived and !exact).
1167 :
1168 : [except.spec] "If a class X ... objects of class X or any class publicly
1169 : and unambiguously derived from X. Similarly, if a pointer type Y * ...
1170 : exceptions of type Y * or that are pointers to any type publicly and
1171 : unambiguously derived from Y. Otherwise a function only allows exceptions
1172 : that have the same type ..."
1173 : This does not mention cv qualifiers and is different to what throw
1174 : [except.throw] and catch [except.catch] will do. They will ignore the
1175 : top level cv qualifiers, and allow qualifiers in the pointer to class
1176 : example.
1177 :
1178 : We implement the letter of the standard. */
1179 :
1180 : static bool
1181 1792 : comp_except_types (tree a, tree b, bool exact)
1182 : {
1183 1792 : if (same_type_p (a, b))
1184 : return true;
1185 506 : else if (!exact)
1186 : {
1187 11 : if (cp_type_quals (a) || cp_type_quals (b))
1188 0 : return false;
1189 :
1190 11 : if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1191 : {
1192 7 : a = TREE_TYPE (a);
1193 7 : b = TREE_TYPE (b);
1194 7 : if (cp_type_quals (a) || cp_type_quals (b))
1195 1 : return false;
1196 : }
1197 :
1198 10 : if (TREE_CODE (a) != RECORD_TYPE
1199 10 : || TREE_CODE (b) != RECORD_TYPE)
1200 : return false;
1201 :
1202 9 : if (publicly_uniquely_derived_p (a, b))
1203 : return true;
1204 : }
1205 : return false;
1206 : }
1207 :
1208 : /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1209 : If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1210 : If EXACT is ce_type, the C++17 type compatibility rules apply.
1211 : If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1212 : If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1213 : are unordered, but we've already filtered out duplicates. Most lists will
1214 : be in order, we should try to make use of that. */
1215 :
1216 : bool
1217 1695074282 : comp_except_specs (const_tree t1, const_tree t2, int exact)
1218 : {
1219 1695074282 : const_tree probe;
1220 1695074282 : const_tree base;
1221 1695074282 : int length = 0;
1222 :
1223 1695074282 : if (t1 == t2)
1224 : return true;
1225 :
1226 : /* First handle noexcept. */
1227 692612710 : if (exact < ce_exact)
1228 : {
1229 9739192 : if (exact == ce_type
1230 19433874 : && (canonical_eh_spec (const_cast<tree> (t1))
1231 9694682 : == canonical_eh_spec (const_cast<tree> (t2))))
1232 : return true;
1233 :
1234 : /* noexcept(false) is compatible with no exception-specification,
1235 : and less strict than any spec. */
1236 9708571 : if (t1 == noexcept_false_spec)
1237 231 : return t2 == NULL_TREE || exact == ce_derived;
1238 : /* Even a derived noexcept(false) is compatible with no
1239 : exception-specification. */
1240 9708340 : if (t2 == noexcept_false_spec)
1241 1344 : return t1 == NULL_TREE;
1242 :
1243 : /* Otherwise, if we aren't looking for an exact match, noexcept is
1244 : equivalent to throw(). */
1245 9706996 : if (t1 == noexcept_true_spec)
1246 739212 : t1 = empty_except_spec;
1247 9706996 : if (t2 == noexcept_true_spec)
1248 8772338 : t2 = empty_except_spec;
1249 : }
1250 :
1251 : /* If any noexcept is left, it is only comparable to itself;
1252 : either we're looking for an exact match or we're redeclaring a
1253 : template with dependent noexcept. */
1254 662276252 : if ((t1 && TREE_PURPOSE (t1))
1255 725877769 : || (t2 && TREE_PURPOSE (t2)))
1256 680641912 : return (t1 && t2
1257 680641912 : && (exact == ce_exact
1258 84217342 : ? TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
1259 192427 : : cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))));
1260 :
1261 11938602 : if (t1 == NULL_TREE) /* T1 is ... */
1262 9010371 : return t2 == NULL_TREE || exact == ce_derived;
1263 2928231 : if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1264 2894215 : return t2 != NULL_TREE && !TREE_VALUE (t2);
1265 35424 : if (t2 == NULL_TREE) /* T2 is ... */
1266 : return false;
1267 1670 : if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1268 76 : return exact == ce_derived;
1269 :
1270 : /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1271 : Count how many we find, to determine exactness. For exact matching and
1272 : ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1273 : O(nm). */
1274 2883 : for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1275 : {
1276 2188 : for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1277 : {
1278 1792 : tree a = TREE_VALUE (probe);
1279 1792 : tree b = TREE_VALUE (t2);
1280 :
1281 1792 : if (comp_except_types (a, b, exact))
1282 : {
1283 1289 : if (probe == base && exact > ce_derived)
1284 1254 : base = TREE_CHAIN (probe);
1285 1289 : length++;
1286 1289 : break;
1287 : }
1288 : }
1289 1289 : if (probe == NULL_TREE)
1290 : return false;
1291 : }
1292 1198 : return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1293 : }
1294 :
1295 : /* Compare the array types T1 and T2. CB says how we should behave when
1296 : comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1297 : bounds_either says than any array can be [], bounds_first means that
1298 : onlt T1 can be an array with unknown bounds. STRICT is true if
1299 : qualifiers must match when comparing the types of the array elements. */
1300 :
1301 : static bool
1302 1960914 : comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1303 : bool strict)
1304 : {
1305 1960914 : tree d1;
1306 1960914 : tree d2;
1307 1960914 : tree max1, max2;
1308 :
1309 1960914 : if (t1 == t2)
1310 : return true;
1311 :
1312 : /* The type of the array elements must be the same. */
1313 1960027 : if (strict
1314 1960027 : ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1315 13505 : : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1316 : return false;
1317 :
1318 1271048 : d1 = TYPE_DOMAIN (t1);
1319 1271048 : d2 = TYPE_DOMAIN (t2);
1320 :
1321 1271048 : if (d1 == d2)
1322 : return true;
1323 :
1324 : /* If one of the arrays is dimensionless, and the other has a
1325 : dimension, they are of different types. However, it is valid to
1326 : write:
1327 :
1328 : extern int a[];
1329 : int a[3];
1330 :
1331 : by [basic.link]:
1332 :
1333 : declarations for an array object can specify
1334 : array types that differ by the presence or absence of a major
1335 : array bound (_dcl.array_). */
1336 722280 : if (!d1 && d2)
1337 7838 : return cb >= bounds_either;
1338 714442 : else if (d1 && !d2)
1339 52910 : return cb == bounds_either;
1340 :
1341 : /* Check that the dimensions are the same. */
1342 :
1343 661532 : if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1344 : return false;
1345 661511 : max1 = TYPE_MAX_VALUE (d1);
1346 661511 : max2 = TYPE_MAX_VALUE (d2);
1347 :
1348 661511 : if (!cp_tree_equal (max1, max2))
1349 : return false;
1350 :
1351 : return true;
1352 : }
1353 :
1354 : /* Compare the relative position of T1 and T2 into their respective
1355 : template parameter list.
1356 : T1 and T2 must be template parameter types.
1357 : Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1358 :
1359 : static bool
1360 1864480181 : comp_template_parms_position (tree t1, tree t2)
1361 : {
1362 1864480181 : tree index1, index2;
1363 1864480181 : gcc_assert (t1 && t2
1364 : && TREE_CODE (t1) == TREE_CODE (t2)
1365 : && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1366 : || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1367 : || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1368 :
1369 1864480181 : index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1370 1864480181 : index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1371 :
1372 : /* Then compare their relative position. */
1373 1864480181 : if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1374 1264156567 : || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1375 2767662832 : || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1376 903182651 : != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1377 : return false;
1378 :
1379 : /* In C++14 we can end up comparing 'auto' to a normal template
1380 : parameter. Don't confuse them. */
1381 810102901 : if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1382 230503517 : return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1383 :
1384 : return true;
1385 : }
1386 :
1387 : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1388 :
1389 : static bool
1390 512 : cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1391 : {
1392 512 : t1 = TYPE_MAIN_VARIANT (t1);
1393 512 : t2 = TYPE_MAIN_VARIANT (t2);
1394 :
1395 512 : if (TYPE_PTR_P (t1)
1396 141 : && TYPE_PTR_P (t2))
1397 : return true;
1398 :
1399 : /* Only the precision of the parameter matters. This check should
1400 : make sure that the callee does not see undefined values in argument
1401 : registers. */
1402 377 : if (INTEGRAL_TYPE_P (t1)
1403 68 : && INTEGRAL_TYPE_P (t2)
1404 424 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1405 : return true;
1406 :
1407 333 : return same_type_p (t1, t2);
1408 : }
1409 :
1410 : /* Check if a type cast between two function types can be considered safe. */
1411 :
1412 : static bool
1413 361 : cxx_safe_function_type_cast_p (tree t1, tree t2)
1414 : {
1415 361 : if (TREE_TYPE (t1) == void_type_node &&
1416 280 : TYPE_ARG_TYPES (t1) == void_list_node)
1417 : return true;
1418 :
1419 309 : if (TREE_TYPE (t2) == void_type_node &&
1420 103 : TYPE_ARG_TYPES (t2) == void_list_node)
1421 : return true;
1422 :
1423 273 : if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1424 : return false;
1425 :
1426 112 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1427 315 : t1 && t2;
1428 203 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1429 239 : if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1430 : return false;
1431 :
1432 : return true;
1433 : }
1434 :
1435 : /* Subroutine in comptypes. */
1436 :
1437 : static bool
1438 16805223730 : structural_comptypes (tree t1, tree t2, int strict)
1439 : {
1440 : /* Both should be types that are not obviously the same. */
1441 16805223730 : gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1442 :
1443 : /* Suppress typename resolution under spec_hasher::equal in place of calling
1444 : push_to_top_level there. */
1445 16805223730 : if (!comparing_specializations)
1446 : {
1447 : /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1448 : current instantiation. */
1449 13467365460 : if (TREE_CODE (t1) == TYPENAME_TYPE)
1450 104287357 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1451 :
1452 13467365460 : if (TREE_CODE (t2) == TYPENAME_TYPE)
1453 133332169 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1454 : }
1455 :
1456 16805223730 : if (TYPE_PTRMEMFUNC_P (t1))
1457 26245575 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1458 16805223730 : if (TYPE_PTRMEMFUNC_P (t2))
1459 29488509 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1460 :
1461 : /* Different classes of types can't be compatible. */
1462 16805223730 : if (TREE_CODE (t1) != TREE_CODE (t2))
1463 : return false;
1464 :
1465 : /* Qualifiers must match. For array types, we will check when we
1466 : recur on the array element types. */
1467 11146213539 : if (TREE_CODE (t1) != ARRAY_TYPE
1468 11146213539 : && cp_type_quals (t1) != cp_type_quals (t2))
1469 : return false;
1470 10447032031 : if (TREE_CODE (t1) == FUNCTION_TYPE
1471 10447032031 : && type_memfn_quals (t1) != type_memfn_quals (t2))
1472 : return false;
1473 : /* Need to check this before TYPE_MAIN_VARIANT.
1474 : FIXME function qualifiers should really change the main variant. */
1475 10446951422 : if (FUNC_OR_METHOD_TYPE_P (t1))
1476 : {
1477 38440389 : if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1478 : return false;
1479 23075048 : if (flag_noexcept_type
1480 46069412 : && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1481 22994364 : TYPE_RAISES_EXCEPTIONS (t2),
1482 : ce_type))
1483 : return false;
1484 : }
1485 :
1486 : /* Allow for two different type nodes which have essentially the same
1487 : definition. Note that we already checked for equality of the type
1488 : qualifiers (just above). */
1489 10422074372 : if (TREE_CODE (t1) != ARRAY_TYPE
1490 10422074372 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1491 510655379 : goto check_alias;
1492 :
1493 : /* Compare the types. Return false on known not-same. Break on not
1494 : known. Never return true from this switch -- you'll break
1495 : specialization comparison. */
1496 9911418993 : switch (TREE_CODE (t1))
1497 : {
1498 : case VOID_TYPE:
1499 : case BOOLEAN_TYPE:
1500 : /* All void and bool types are the same. */
1501 : break;
1502 :
1503 821304668 : case OPAQUE_TYPE:
1504 821304668 : case INTEGER_TYPE:
1505 821304668 : case FIXED_POINT_TYPE:
1506 821304668 : case REAL_TYPE:
1507 : /* With these nodes, we can't determine type equivalence by
1508 : looking at what is stored in the nodes themselves, because
1509 : two nodes might have different TYPE_MAIN_VARIANTs but still
1510 : represent the same type. For example, wchar_t and int could
1511 : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1512 : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1513 : and are distinct types. On the other hand, int and the
1514 : following typedef
1515 :
1516 : typedef int INT __attribute((may_alias));
1517 :
1518 : have identical properties, different TYPE_MAIN_VARIANTs, but
1519 : represent the same type. The canonical type system keeps
1520 : track of equivalence in this case, so we fall back on it. */
1521 821304668 : if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1522 : return false;
1523 :
1524 : /* We don't need or want the attribute comparison. */
1525 9635 : goto check_alias;
1526 :
1527 1718323 : case TEMPLATE_TEMPLATE_PARM:
1528 1718323 : case BOUND_TEMPLATE_TEMPLATE_PARM:
1529 1718323 : if (!comp_template_parms_position (t1, t2))
1530 : return false;
1531 1326499 : if (!comp_template_parms
1532 2652998 : (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1533 3761977 : DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1534 : return false;
1535 931480 : if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1536 : break;
1537 : /* Don't check inheritance. */
1538 : strict = COMPARE_STRICT;
1539 : /* Fall through. */
1540 :
1541 4623407436 : case RECORD_TYPE:
1542 4623407436 : case UNION_TYPE:
1543 8132500427 : if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1544 2916811005 : && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1545 2059445285 : || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1546 5480845479 : && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1547 : break;
1548 :
1549 4561281821 : if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1550 : break;
1551 4561281787 : else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1552 : break;
1553 :
1554 : return false;
1555 :
1556 59876 : case OFFSET_TYPE:
1557 59876 : if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1558 : strict & ~COMPARE_REDECLARATION))
1559 : return false;
1560 58047 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1561 : return false;
1562 : break;
1563 :
1564 1453462126 : case REFERENCE_TYPE:
1565 1453462126 : if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1566 : return false;
1567 : /* fall through to checks for pointer types */
1568 1808228799 : gcc_fallthrough ();
1569 :
1570 1808228799 : case POINTER_TYPE:
1571 1808228799 : if (TYPE_MODE (t1) != TYPE_MODE (t2)
1572 1808228799 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1573 1651514711 : return false;
1574 : break;
1575 :
1576 5811254 : case FUNCTION_TYPE:
1577 5811254 : if (TYPE_NO_NAMED_ARGS_STDARG_P (t1) != TYPE_NO_NAMED_ARGS_STDARG_P (t2))
1578 : return false;
1579 : /* FALLTHRU */
1580 13250926 : case METHOD_TYPE:
1581 : /* Exception specs and memfn_rquals were checked above. */
1582 13250926 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1583 : return false;
1584 12607172 : if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1585 : return false;
1586 : break;
1587 :
1588 1946522 : case ARRAY_TYPE:
1589 : /* Target types must match incl. qualifiers. */
1590 1946522 : if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1591 1946522 : ? bounds_either : bounds_none),
1592 : /*strict=*/true))
1593 : return false;
1594 : break;
1595 :
1596 1862761858 : case TEMPLATE_TYPE_PARM:
1597 : /* If T1 and T2 don't have the same relative position in their
1598 : template parameters set, they can't be equal. */
1599 1862761858 : if (!comp_template_parms_position (t1, t2))
1600 : return false;
1601 : /* If T1 and T2 don't represent the same class template deduction,
1602 : they aren't equal. */
1603 1292865294 : if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1604 646432647 : CLASS_PLACEHOLDER_TEMPLATE (t2)))
1605 : return false;
1606 : /* Constrained 'auto's are distinct from parms that don't have the same
1607 : constraints. */
1608 616145780 : if (!equivalent_placeholder_constraints (t1, t2))
1609 : return false;
1610 : break;
1611 :
1612 302711322 : case TYPENAME_TYPE:
1613 605422644 : if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1614 302711322 : TYPENAME_TYPE_FULLNAME (t2)))
1615 : return false;
1616 : /* Qualifiers don't matter on scopes. */
1617 265015470 : if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1618 265015470 : TYPE_CONTEXT (t2)))
1619 : return false;
1620 : break;
1621 :
1622 28797 : case UNBOUND_CLASS_TEMPLATE:
1623 28797 : if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1624 : return false;
1625 28797 : if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1626 : return false;
1627 : break;
1628 :
1629 2513705 : case COMPLEX_TYPE:
1630 2513705 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1631 : return false;
1632 : break;
1633 :
1634 12651150 : case VECTOR_TYPE:
1635 12651150 : if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1636 23616337 : || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1637 23676480 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1638 1685963 : return false;
1639 : break;
1640 :
1641 7902079 : case TYPE_PACK_EXPANSION:
1642 7902079 : return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1643 : PACK_EXPANSION_PATTERN (t2))
1644 15604484 : && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1645 7702405 : PACK_EXPANSION_EXTRA_ARGS (t2)));
1646 :
1647 34 : case PACK_INDEX_TYPE:
1648 34 : return (cp_tree_equal (PACK_INDEX_PACK (t1),
1649 34 : PACK_INDEX_PACK (t2))
1650 40 : && cp_tree_equal (PACK_INDEX_INDEX (t1),
1651 6 : PACK_INDEX_INDEX (t2)));
1652 :
1653 33168163 : case DECLTYPE_TYPE:
1654 66336326 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1655 33168163 : != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1656 : return false;
1657 32993873 : if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1658 : return false;
1659 32993873 : if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1660 : return false;
1661 32993873 : if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1662 : return false;
1663 : break;
1664 :
1665 764356 : case TRAIT_TYPE:
1666 764356 : if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1667 : return false;
1668 1454 : if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1669 2908 : || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1670 0 : return false;
1671 : break;
1672 :
1673 3 : case TYPEOF_TYPE:
1674 3 : if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1675 : return false;
1676 : break;
1677 :
1678 : default:
1679 : return false;
1680 : }
1681 :
1682 : /* If we get here, we know that from a target independent POV the
1683 : types are the same. Make sure the target attributes are also
1684 : the same. */
1685 860617525 : if (!comp_type_attributes (t1, t2))
1686 : return false;
1687 :
1688 860616338 : check_alias:
1689 1371281352 : if (comparing_dependent_aliases
1690 1371281352 : && (typedef_variant_p (t1) || typedef_variant_p (t2)))
1691 : {
1692 5164424 : tree dep1 = dependent_opaque_alias_p (t1) ? t1 : NULL_TREE;
1693 5164424 : tree dep2 = dependent_opaque_alias_p (t2) ? t2 : NULL_TREE;
1694 5164424 : if ((dep1 || dep2)
1695 5164424 : && (!(dep1 && dep2)
1696 0 : || !comp_type_attributes (DECL_ATTRIBUTES (TYPE_NAME (dep1)),
1697 0 : DECL_ATTRIBUTES (TYPE_NAME (dep2)))))
1698 66 : return false;
1699 :
1700 : /* Don't treat an alias template specialization with dependent
1701 : arguments as equivalent to its underlying type when used as a
1702 : template argument; we need them to be distinct so that we
1703 : substitute into the specialization arguments at instantiation
1704 : time. And aliases can't be equivalent without being ==, so
1705 : we don't need to look any deeper. */
1706 5164358 : ++processing_template_decl;
1707 5164358 : dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1708 5164358 : dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1709 5164358 : --processing_template_decl;
1710 5164358 : if ((dep1 || dep2) && dep1 != dep2)
1711 : return false;
1712 : }
1713 :
1714 : return true;
1715 : }
1716 :
1717 : /* C++ implementation of compatible_types_for_indirection_note_p. */
1718 :
1719 : bool
1720 1611 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1721 : {
1722 1611 : return same_type_p (type1, type2);
1723 : }
1724 :
1725 : /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1726 : is a bitwise-or of the COMPARE_* flags. */
1727 :
1728 : bool
1729 25412053174 : comptypes (tree t1, tree t2, int strict)
1730 : {
1731 25412053174 : gcc_checking_assert (t1 && t2);
1732 :
1733 : /* TYPE_ARGUMENT_PACKS are not really types. */
1734 25412053174 : gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1735 : && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1736 :
1737 25412053174 : if (t1 == t2)
1738 : return true;
1739 :
1740 : /* Suppress errors caused by previously reported errors. */
1741 16805224895 : if (t1 == error_mark_node || t2 == error_mark_node)
1742 : return false;
1743 :
1744 16805224733 : if (strict == COMPARE_STRICT)
1745 : {
1746 15292760622 : if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1747 : /* At least one of the types requires structural equality, so
1748 : perform a deep check. */
1749 1784094232 : return structural_comptypes (t1, t2, strict);
1750 :
1751 13508666390 : if (flag_checking && param_use_canonical_types)
1752 : {
1753 13508665387 : bool result = structural_comptypes (t1, t2, strict);
1754 :
1755 13508665387 : if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1756 : /* The two types are structurally equivalent, but their
1757 : canonical types were different. This is a failure of the
1758 : canonical type propagation code.*/
1759 0 : internal_error
1760 0 : ("canonical types differ for identical types %qT and %qT",
1761 : t1, t2);
1762 13508665387 : else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1763 : /* Two types are structurally different, but the canonical
1764 : types are the same. This means we were over-eager in
1765 : assigning canonical types. */
1766 0 : internal_error
1767 0 : ("same canonical type node for different types %qT and %qT",
1768 : t1, t2);
1769 :
1770 : return result;
1771 : }
1772 1003 : if (!flag_checking && param_use_canonical_types)
1773 1003 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1774 : else
1775 0 : return structural_comptypes (t1, t2, strict);
1776 : }
1777 1512464111 : else if (strict == COMPARE_STRUCTURAL)
1778 1445585112 : return structural_comptypes (t1, t2, COMPARE_STRICT);
1779 : else
1780 66878999 : return structural_comptypes (t1, t2, strict);
1781 : }
1782 :
1783 : /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1784 : top-level qualifiers. */
1785 :
1786 : bool
1787 4391676926 : same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1788 : {
1789 4391676926 : if (type1 == error_mark_node || type2 == error_mark_node)
1790 : return false;
1791 4391676142 : if (type1 == type2)
1792 : return true;
1793 :
1794 2463584805 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1795 2463584805 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1796 2463584805 : return same_type_p (type1, type2);
1797 : }
1798 :
1799 : /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1800 :
1801 : bool
1802 379627982 : similar_type_p (tree type1, tree type2)
1803 : {
1804 379627982 : if (type1 == error_mark_node || type2 == error_mark_node)
1805 : return false;
1806 :
1807 : /* Informally, two types are similar if, ignoring top-level cv-qualification:
1808 : * they are the same type; or
1809 : * they are both pointers, and the pointed-to types are similar; or
1810 : * they are both pointers to member of the same class, and the types of
1811 : the pointed-to members are similar; or
1812 : * they are both arrays of the same size or both arrays of unknown bound,
1813 : and the array element types are similar. */
1814 :
1815 379627982 : if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1816 : return true;
1817 :
1818 157922824 : if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1819 157670569 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1820 157668703 : || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1821 256357 : return comp_ptr_ttypes_const (type1, type2, bounds_either);
1822 :
1823 : return false;
1824 : }
1825 :
1826 : /* Helper function for layout_compatible_type_p and
1827 : is_corresponding_member_aggr. Advance to next members (NULL if
1828 : no further ones) and return true if those members are still part of
1829 : the common initial sequence. */
1830 :
1831 : bool
1832 2210 : next_common_initial_sequence (tree &memb1, tree &memb2)
1833 : {
1834 2733 : while (memb1)
1835 : {
1836 2772 : if (TREE_CODE (memb1) != FIELD_DECL
1837 2313 : || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1838 : {
1839 459 : memb1 = DECL_CHAIN (memb1);
1840 459 : continue;
1841 : }
1842 1854 : if (DECL_FIELD_IS_BASE (memb1))
1843 : {
1844 64 : memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1845 64 : continue;
1846 : }
1847 : break;
1848 : }
1849 2694 : while (memb2)
1850 : {
1851 2730 : if (TREE_CODE (memb2) != FIELD_DECL
1852 2274 : || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1853 : {
1854 456 : memb2 = DECL_CHAIN (memb2);
1855 456 : continue;
1856 : }
1857 1818 : if (DECL_FIELD_IS_BASE (memb2))
1858 : {
1859 28 : memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1860 28 : continue;
1861 : }
1862 : break;
1863 : }
1864 2210 : if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1865 : return true;
1866 1790 : if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1867 : return false;
1868 1790 : if (DECL_BIT_FIELD_TYPE (memb1))
1869 : {
1870 165 : if (!DECL_BIT_FIELD_TYPE (memb2))
1871 : return false;
1872 66 : if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1873 66 : DECL_BIT_FIELD_TYPE (memb2)))
1874 : return false;
1875 66 : if (TYPE_PRECISION (TREE_TYPE (memb1))
1876 66 : != TYPE_PRECISION (TREE_TYPE (memb2)))
1877 : return false;
1878 : }
1879 1625 : else if (DECL_BIT_FIELD_TYPE (memb2))
1880 : return false;
1881 1598 : else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1882 : return false;
1883 1598 : if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1884 1598 : != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1885 : return false;
1886 1571 : if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1887 : return false;
1888 1512 : if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1889 : return false;
1890 : return true;
1891 : }
1892 :
1893 : /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1894 :
1895 : bool
1896 2773 : layout_compatible_type_p (tree type1, tree type2, bool explain/*=false*/)
1897 : {
1898 2773 : if (type1 == error_mark_node || type2 == error_mark_node)
1899 : return false;
1900 2773 : if (type1 == type2)
1901 : return true;
1902 :
1903 1359 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1904 1359 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1905 1359 : if (same_type_p (type1, type2))
1906 : return true;
1907 :
1908 654 : if (TREE_CODE (type1) != TREE_CODE (type2)
1909 602 : || (TREE_CODE (type1) != ENUMERAL_TYPE
1910 564 : && !CLASS_TYPE_P (type1))
1911 1107 : || (TREE_CODE (type2) != ENUMERAL_TYPE
1912 415 : && !CLASS_TYPE_P (type2)))
1913 : {
1914 201 : if (explain)
1915 6 : inform (input_location, "%q#T and %q#T are not both the same type, "
1916 : "layout-compatible enumerations, or "
1917 : "layout-compatible standard-layout class types",
1918 : type1, type2);
1919 201 : return false;
1920 : }
1921 :
1922 453 : if (!std_layout_type_p (type1))
1923 : {
1924 16 : if (explain)
1925 3 : inform (location_of (type1),
1926 : "%qT is not a standard-layout type", type1);
1927 16 : return false;
1928 : }
1929 437 : if (!std_layout_type_p (type2))
1930 : {
1931 0 : if (explain)
1932 0 : inform (location_of (type2),
1933 : "%qT is not a standard-layout type", type2);
1934 0 : return false;
1935 : }
1936 :
1937 437 : if (TREE_CODE (type1) == ENUMERAL_TYPE)
1938 : {
1939 38 : tree underlying1 = finish_underlying_type (type1);
1940 38 : tree underlying2 = finish_underlying_type (type2);
1941 38 : if (!same_type_p (underlying1, underlying2))
1942 : {
1943 25 : if (explain)
1944 : {
1945 3 : inform (location_of (type1),
1946 : "the underlying type of %qT is %qT, but",
1947 : type1, underlying1);
1948 3 : inform (location_of (type2),
1949 : "the underlying type of %qT is %qT",
1950 : type2, underlying2);
1951 : }
1952 25 : return false;
1953 : }
1954 : }
1955 399 : else if (TREE_CODE (type1) == RECORD_TYPE)
1956 : {
1957 315 : tree field1 = TYPE_FIELDS (type1);
1958 315 : tree field2 = TYPE_FIELDS (type2);
1959 1179 : while (1)
1960 : {
1961 747 : if (!next_common_initial_sequence (field1, field2))
1962 : {
1963 27 : if (explain)
1964 : {
1965 6 : if (field1 && field2)
1966 : {
1967 6 : inform (DECL_SOURCE_LOCATION (field1),
1968 : "%qD and %qD do not correspond",
1969 : field1, field2);
1970 6 : inform (DECL_SOURCE_LOCATION (field2),
1971 : "%qD declared here", field2);
1972 : }
1973 0 : else if (field1)
1974 0 : inform (DECL_SOURCE_LOCATION (field1),
1975 : "%qT has no member corresponding to %qD",
1976 : type2, field1);
1977 0 : else if (field2)
1978 0 : inform (DECL_SOURCE_LOCATION (field2),
1979 : "%qT has no member corresponding to %qD",
1980 : type1, field2);
1981 : }
1982 27 : return false;
1983 : }
1984 720 : if (field1 == NULL_TREE)
1985 : break;
1986 432 : field1 = DECL_CHAIN (field1);
1987 432 : field2 = DECL_CHAIN (field2);
1988 : }
1989 : }
1990 84 : else if (TREE_CODE (type1) == UNION_TYPE)
1991 : {
1992 : /* The standard says:
1993 : "Two standard-layout unions are layout-compatible if they have
1994 : the same number of non-static data members and corresponding
1995 : non-static data members (in any order) have layout-compatible
1996 : types."
1997 : but the code anticipates that bitfield vs. non-bitfield,
1998 : different bitfield widths or presence/absence of
1999 : [[no_unique_address]] should be checked as well. */
2000 84 : tree field1 = TYPE_FIELDS (type1);
2001 84 : tree field2 = TYPE_FIELDS (type2);
2002 84 : auto_vec<tree, 16> vec;
2003 84 : unsigned int count = 0;
2004 378 : for (; field1; field1 = DECL_CHAIN (field1))
2005 294 : if (TREE_CODE (field1) == FIELD_DECL)
2006 195 : count++;
2007 372 : for (; field2; field2 = DECL_CHAIN (field2))
2008 288 : if (TREE_CODE (field2) == FIELD_DECL)
2009 189 : vec.safe_push (field2);
2010 :
2011 : /* Discussions on core lean towards treating multiple union fields
2012 : of the same type as the same field, so this might need changing
2013 : in the future. */
2014 168 : if (count != vec.length ())
2015 : {
2016 6 : if (explain)
2017 : {
2018 3 : inform_n (location_of (type1), count,
2019 : "%qT has %u field, but",
2020 : "%qT has %u fields, but",
2021 : type1, count);
2022 6 : inform_n (location_of (type2), vec.length (),
2023 : "%qT has %u field",
2024 : "%qT has %u fields",
2025 : type2, vec.length ());
2026 : }
2027 6 : return false;
2028 : }
2029 :
2030 324 : for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
2031 : {
2032 258 : if (TREE_CODE (field1) != FIELD_DECL)
2033 81 : continue;
2034 177 : unsigned int j;
2035 177 : tree t1 = DECL_BIT_FIELD_TYPE (field1);
2036 177 : if (t1 == NULL_TREE)
2037 174 : t1 = TREE_TYPE (field1);
2038 297 : FOR_EACH_VEC_ELT (vec, j, field2)
2039 : {
2040 285 : tree t2 = DECL_BIT_FIELD_TYPE (field2);
2041 285 : if (t2 == NULL_TREE)
2042 273 : t2 = TREE_TYPE (field2);
2043 285 : if (DECL_BIT_FIELD_TYPE (field1))
2044 : {
2045 6 : if (!DECL_BIT_FIELD_TYPE (field2))
2046 0 : continue;
2047 6 : if (TYPE_PRECISION (TREE_TYPE (field1))
2048 6 : != TYPE_PRECISION (TREE_TYPE (field2)))
2049 6 : continue;
2050 : }
2051 279 : else if (DECL_BIT_FIELD_TYPE (field2))
2052 6 : continue;
2053 273 : if (!layout_compatible_type_p (t1, t2))
2054 108 : continue;
2055 165 : if ((!lookup_attribute ("no_unique_address",
2056 165 : DECL_ATTRIBUTES (field1)))
2057 165 : != !lookup_attribute ("no_unique_address",
2058 165 : DECL_ATTRIBUTES (field2)))
2059 0 : continue;
2060 : break;
2061 : }
2062 354 : if (j == vec.length ())
2063 : {
2064 12 : if (explain)
2065 : {
2066 3 : inform (DECL_SOURCE_LOCATION (field1),
2067 : "%qT has no member corresponding to %qD",
2068 : type2, field1);
2069 3 : inform (location_of (type2), "%qT declared here", type2);
2070 : }
2071 12 : return false;
2072 : }
2073 165 : vec.unordered_remove (j);
2074 : }
2075 84 : }
2076 : else
2077 0 : gcc_unreachable ();
2078 :
2079 : return true;
2080 : }
2081 :
2082 : /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
2083 :
2084 : bool
2085 253543677 : at_least_as_qualified_p (const_tree type1, const_tree type2)
2086 : {
2087 253543677 : int q1 = cp_type_quals (type1);
2088 253543677 : int q2 = cp_type_quals (type2);
2089 :
2090 : /* All qualifiers for TYPE2 must also appear in TYPE1. */
2091 253543677 : return (q1 & q2) == q2;
2092 : }
2093 :
2094 : /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
2095 : more cv-qualified that TYPE1, and 0 otherwise. */
2096 :
2097 : int
2098 15378464 : comp_cv_qualification (int q1, int q2)
2099 : {
2100 15378464 : if (q1 == q2)
2101 : return 0;
2102 :
2103 3668061 : if ((q1 & q2) == q2)
2104 : return 1;
2105 364756 : else if ((q1 & q2) == q1)
2106 364171 : return -1;
2107 :
2108 : return 0;
2109 : }
2110 :
2111 : int
2112 0 : comp_cv_qualification (const_tree type1, const_tree type2)
2113 : {
2114 0 : int q1 = cp_type_quals (type1);
2115 0 : int q2 = cp_type_quals (type2);
2116 0 : return comp_cv_qualification (q1, q2);
2117 : }
2118 :
2119 : /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
2120 : subset of the cv-qualification signature of TYPE2, and the types
2121 : are similar. Returns -1 if the other way 'round, and 0 otherwise. */
2122 :
2123 : int
2124 355 : comp_cv_qual_signature (tree type1, tree type2)
2125 : {
2126 355 : if (comp_ptr_ttypes_real (type2, type1, -1))
2127 : return 1;
2128 281 : else if (comp_ptr_ttypes_real (type1, type2, -1))
2129 : return -1;
2130 : else
2131 269 : return 0;
2132 : }
2133 :
2134 : /* Subroutines of `comptypes'. */
2135 :
2136 : /* Return true if two parameter type lists PARMS1 and PARMS2 are
2137 : equivalent in the sense that functions with those parameter types
2138 : can have equivalent types. The two lists must be equivalent,
2139 : element by element. */
2140 :
2141 : bool
2142 1111439343 : compparms (const_tree parms1, const_tree parms2)
2143 : {
2144 1111439343 : const_tree t1, t2;
2145 :
2146 : /* An unspecified parmlist matches any specified parmlist
2147 : whose argument types don't need default promotions. */
2148 :
2149 1111439343 : for (t1 = parms1, t2 = parms2;
2150 1769347629 : t1 || t2;
2151 657908286 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2152 : {
2153 : /* If one parmlist is shorter than the other,
2154 : they fail to match. */
2155 1656349069 : if (!t1 || !t2)
2156 : return false;
2157 1655753467 : if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2158 : return false;
2159 : }
2160 : return true;
2161 : }
2162 :
2163 :
2164 : /* Process a sizeof or alignof expression where the operand is a type.
2165 : STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2166 : or GNU (preferred alignment) semantics; it is ignored if OP is
2167 : SIZEOF_EXPR. */
2168 :
2169 : tree
2170 29046881 : cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2171 : bool std_alignof, bool complain)
2172 : {
2173 29046881 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2174 29046881 : if (type == error_mark_node)
2175 : return error_mark_node;
2176 :
2177 29046879 : type = non_reference (type);
2178 29046879 : if (TREE_CODE (type) == METHOD_TYPE)
2179 : {
2180 0 : if (complain)
2181 : {
2182 0 : pedwarn (loc, OPT_Wpointer_arith,
2183 : "invalid application of %qs to a member function",
2184 0 : OVL_OP_INFO (false, op)->name);
2185 0 : return size_one_node;
2186 : }
2187 : else
2188 0 : return error_mark_node;
2189 : }
2190 29046879 : else if (VOID_TYPE_P (type) && std_alignof)
2191 : {
2192 12 : if (complain)
2193 12 : error_at (loc, "invalid application of %qs to a void type",
2194 12 : OVL_OP_INFO (false, op)->name);
2195 12 : return error_mark_node;
2196 : }
2197 :
2198 29046867 : bool dependent_p = dependent_type_p (type);
2199 29046867 : if (!dependent_p)
2200 : {
2201 23963988 : complete_type (type);
2202 23963988 : if (!COMPLETE_TYPE_P (type))
2203 : /* Call this here because the incompleteness diagnostic comes from
2204 : c_sizeof_or_alignof_type instead of
2205 : complete_type_or_maybe_complain. */
2206 285 : note_failed_type_completion (type, complain);
2207 : }
2208 23963988 : if (dependent_p
2209 : /* VLA types will have a non-constant size. In the body of an
2210 : uninstantiated template, we don't need to try to compute the
2211 : value, because the sizeof expression is not an integral
2212 : constant expression in that case. And, if we do try to
2213 : compute the value, we'll likely end up with SAVE_EXPRs, which
2214 : the template substitution machinery does not expect to see. */
2215 23963988 : || (processing_template_decl
2216 2108106 : && COMPLETE_TYPE_P (type)
2217 2108100 : && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2218 : {
2219 5082879 : tree value = build_min (op, size_type_node, type);
2220 5082879 : TREE_READONLY (value) = 1;
2221 5082879 : if (op == ALIGNOF_EXPR && std_alignof)
2222 587116 : ALIGNOF_EXPR_STD_P (value) = true;
2223 5082879 : SET_EXPR_LOCATION (value, loc);
2224 5082879 : return value;
2225 : }
2226 :
2227 23963988 : return c_sizeof_or_alignof_type (loc, complete_type (type),
2228 : op == SIZEOF_EXPR, std_alignof,
2229 23963988 : complain & (tf_warning_or_error));
2230 : }
2231 :
2232 : /* Return the size of the type, without producing any warnings for
2233 : types whose size cannot be taken. This routine should be used only
2234 : in some other routine that has already produced a diagnostic about
2235 : using the size of such a type. */
2236 : tree
2237 2869628 : cxx_sizeof_nowarn (tree type)
2238 : {
2239 2869628 : if (TREE_CODE (type) == FUNCTION_TYPE
2240 2869628 : || VOID_TYPE_P (type)
2241 2869546 : || TREE_CODE (type) == ERROR_MARK)
2242 82 : return size_one_node;
2243 2869546 : else if (!COMPLETE_TYPE_P (type))
2244 25 : return size_zero_node;
2245 : else
2246 2869521 : return cxx_sizeof_or_alignof_type (input_location, type,
2247 2869521 : SIZEOF_EXPR, false, false);
2248 : }
2249 :
2250 : /* Process a sizeof expression where the operand is an expression. */
2251 :
2252 : static tree
2253 1292875 : cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2254 : {
2255 1292875 : if (e == error_mark_node)
2256 : return error_mark_node;
2257 :
2258 1292140 : if (instantiation_dependent_uneval_expression_p (e))
2259 : {
2260 369352 : e = build_min (SIZEOF_EXPR, size_type_node, e);
2261 369352 : TREE_SIDE_EFFECTS (e) = 0;
2262 369352 : TREE_READONLY (e) = 1;
2263 369352 : SET_EXPR_LOCATION (e, loc);
2264 :
2265 369352 : return e;
2266 : }
2267 :
2268 922788 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2269 922788 : STRIP_ANY_LOCATION_WRAPPER (e);
2270 :
2271 922788 : if (TREE_CODE (e) == PARM_DECL
2272 35442 : && DECL_ARRAY_PARAMETER_P (e)
2273 923165 : && (complain & tf_warning))
2274 : {
2275 120 : auto_diagnostic_group d;
2276 120 : if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2277 : "%<sizeof%> on array function parameter %qE "
2278 120 : "will return size of %qT", e, TREE_TYPE (e)))
2279 24 : inform (DECL_SOURCE_LOCATION (e), "declared here");
2280 120 : }
2281 :
2282 922788 : e = mark_type_use (e);
2283 :
2284 922788 : if (bitfield_p (e))
2285 : {
2286 12 : if (complain & tf_error)
2287 6 : error_at (e_loc,
2288 : "invalid application of %<sizeof%> to a bit-field");
2289 : else
2290 6 : return error_mark_node;
2291 6 : e = char_type_node;
2292 : }
2293 922776 : else if (is_overloaded_fn (e))
2294 : {
2295 33 : if (complain & tf_error)
2296 12 : permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2297 : "an expression of function type");
2298 : else
2299 21 : return error_mark_node;
2300 12 : e = char_type_node;
2301 : }
2302 922743 : else if (type_unknown_p (e))
2303 : {
2304 0 : if (complain & tf_error)
2305 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2306 : else
2307 0 : return error_mark_node;
2308 0 : e = char_type_node;
2309 : }
2310 : else
2311 922743 : e = TREE_TYPE (e);
2312 :
2313 922761 : return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2314 922761 : complain & tf_error);
2315 : }
2316 :
2317 : /* Implement the __alignof keyword: Return the minimum required
2318 : alignment of E, measured in bytes. For VAR_DECL's and
2319 : FIELD_DECL's return DECL_ALIGN (which can be set from an
2320 : "aligned" __attribute__ specification). STD_ALIGNOF acts
2321 : like in cxx_sizeof_or_alignof_type. */
2322 :
2323 : static tree
2324 100939 : cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2325 : tsubst_flags_t complain)
2326 : {
2327 100939 : tree t;
2328 :
2329 100939 : if (e == error_mark_node)
2330 : return error_mark_node;
2331 :
2332 100931 : if (processing_template_decl)
2333 : {
2334 19224 : e = build_min (ALIGNOF_EXPR, size_type_node, e);
2335 19224 : TREE_SIDE_EFFECTS (e) = 0;
2336 19224 : TREE_READONLY (e) = 1;
2337 19224 : SET_EXPR_LOCATION (e, loc);
2338 19224 : ALIGNOF_EXPR_STD_P (e) = std_alignof;
2339 :
2340 19224 : return e;
2341 : }
2342 :
2343 81707 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2344 81707 : STRIP_ANY_LOCATION_WRAPPER (e);
2345 :
2346 81707 : e = mark_type_use (e);
2347 :
2348 81707 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2349 : !(complain & tf_error)))
2350 : {
2351 0 : if (!(complain & tf_error))
2352 0 : return error_mark_node;
2353 0 : t = size_one_node;
2354 : }
2355 81707 : else if (VAR_P (e))
2356 12270 : t = size_int (DECL_ALIGN_UNIT (e));
2357 69437 : else if (bitfield_p (e))
2358 : {
2359 6 : if (complain & tf_error)
2360 6 : error_at (e_loc,
2361 : "invalid application of %<__alignof%> to a bit-field");
2362 : else
2363 0 : return error_mark_node;
2364 6 : t = size_one_node;
2365 : }
2366 69431 : else if (TREE_CODE (e) == COMPONENT_REF
2367 69431 : && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2368 37792 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2369 31639 : else if (is_overloaded_fn (e))
2370 : {
2371 6 : if (complain & tf_error)
2372 6 : permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2373 : "an expression of function type");
2374 : else
2375 0 : return error_mark_node;
2376 6 : if (TREE_CODE (e) == FUNCTION_DECL)
2377 3 : t = size_int (DECL_ALIGN_UNIT (e));
2378 : else
2379 3 : t = size_one_node;
2380 : }
2381 31633 : else if (type_unknown_p (e))
2382 : {
2383 0 : if (complain & tf_error)
2384 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2385 : else
2386 0 : return error_mark_node;
2387 0 : t = size_one_node;
2388 : }
2389 : else
2390 31633 : return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2391 : ALIGNOF_EXPR, std_alignof,
2392 31633 : complain & tf_error);
2393 :
2394 50074 : return fold_convert_loc (loc, size_type_node, t);
2395 : }
2396 :
2397 : /* Process a sizeof or alignof expression E with code OP where the operand
2398 : is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2399 :
2400 : tree
2401 1393814 : cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2402 : bool std_alignof, bool complain)
2403 : {
2404 1393814 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2405 1393814 : if (op == SIZEOF_EXPR)
2406 1882475 : return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2407 : else
2408 100966 : return cxx_alignof_expr (loc, e, std_alignof,
2409 100939 : complain? tf_warning_or_error : tf_none);
2410 : }
2411 :
2412 : /* Build a representation of an expression 'alignas(E).' Return the
2413 : folded integer value of E if it is an integral constant expression
2414 : that resolves to a valid alignment. If E depends on a template
2415 : parameter, return a syntactic representation tree of kind
2416 : ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2417 : expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2418 :
2419 : tree
2420 278376 : cxx_alignas_expr (tree e)
2421 : {
2422 278376 : if (e == NULL_TREE || e == error_mark_node
2423 556752 : || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2424 0 : return e;
2425 :
2426 278376 : if (TYPE_P (e))
2427 : /* [dcl.align]/3:
2428 :
2429 : When the alignment-specifier is of the form
2430 : alignas(type-id), it shall have the same effect as
2431 : alignas(alignof(type-id)). */
2432 :
2433 146119 : return cxx_sizeof_or_alignof_type (input_location,
2434 : e, ALIGNOF_EXPR,
2435 : /*std_alignof=*/true,
2436 146119 : /*complain=*/true);
2437 :
2438 : /* If we reach this point, it means the alignas expression if of
2439 : the form "alignas(assignment-expression)", so we should follow
2440 : what is stated by [dcl.align]/2. */
2441 :
2442 132257 : if (value_dependent_expression_p (e))
2443 : /* Leave value-dependent expression alone for now. */
2444 : return e;
2445 :
2446 68005 : e = instantiate_non_dependent_expr (e);
2447 68005 : e = mark_rvalue_use (e);
2448 :
2449 : /* [dcl.align]/2 says:
2450 :
2451 : the assignment-expression shall be an integral constant
2452 : expression. */
2453 :
2454 68005 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2455 : {
2456 18 : error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2457 18 : return error_mark_node;
2458 : }
2459 :
2460 67987 : return cxx_constant_value (e);
2461 : }
2462 :
2463 :
2464 : /* EXPR is being used in a context that is not a function call.
2465 : Enforce:
2466 :
2467 : [expr.ref]
2468 :
2469 : The expression can be used only as the left-hand operand of a
2470 : member function call.
2471 :
2472 : [expr.mptr.operator]
2473 :
2474 : If the result of .* or ->* is a function, then that result can be
2475 : used only as the operand for the function call operator ().
2476 :
2477 : by issuing an error message if appropriate. Returns true iff EXPR
2478 : violates these rules. */
2479 :
2480 : bool
2481 1542580175 : invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2482 : {
2483 1542580175 : if (expr == NULL_TREE)
2484 : return false;
2485 : /* Don't enforce this in MS mode. */
2486 1542578733 : if (flag_ms_extensions)
2487 : return false;
2488 1542570672 : if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2489 128638484 : expr = get_first_fn (expr);
2490 1542570672 : if (TREE_TYPE (expr)
2491 1542570672 : && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2492 : {
2493 96 : if (complain & tf_error)
2494 : {
2495 88 : if (DECL_P (expr))
2496 : {
2497 70 : auto_diagnostic_group d;
2498 70 : error_at (loc, "invalid use of non-static member function %qD",
2499 : expr);
2500 70 : inform (DECL_SOURCE_LOCATION (expr), "declared here");
2501 70 : }
2502 : else
2503 18 : error_at (loc, "invalid use of non-static member function of "
2504 18 : "type %qT", TREE_TYPE (expr));
2505 : }
2506 96 : return true;
2507 : }
2508 : return false;
2509 : }
2510 :
2511 : /* If EXP is a reference to a bit-field, and the type of EXP does not
2512 : match the declared type of the bit-field, return the declared type
2513 : of the bit-field. Otherwise, return NULL_TREE. */
2514 :
2515 : tree
2516 3654978137 : is_bitfield_expr_with_lowered_type (const_tree exp)
2517 : {
2518 4988842176 : switch (TREE_CODE (exp))
2519 : {
2520 8806042 : case COND_EXPR:
2521 17612084 : if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2522 8806016 : ? TREE_OPERAND (exp, 1)
2523 26 : : TREE_OPERAND (exp, 0)))
2524 : return NULL_TREE;
2525 141 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2526 :
2527 952591 : case COMPOUND_EXPR:
2528 952591 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2529 :
2530 425812542 : case MODIFY_EXPR:
2531 425812542 : case SAVE_EXPR:
2532 425812542 : case UNARY_PLUS_EXPR:
2533 425812542 : case PREDECREMENT_EXPR:
2534 425812542 : case PREINCREMENT_EXPR:
2535 425812542 : case POSTDECREMENT_EXPR:
2536 425812542 : case POSTINCREMENT_EXPR:
2537 425812542 : case NEGATE_EXPR:
2538 425812542 : case NON_LVALUE_EXPR:
2539 425812542 : case BIT_NOT_EXPR:
2540 425812542 : case CLEANUP_POINT_EXPR:
2541 425812542 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2542 :
2543 262749191 : case COMPONENT_REF:
2544 262749191 : {
2545 262749191 : tree field;
2546 :
2547 262749191 : field = TREE_OPERAND (exp, 1);
2548 262749191 : if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2549 : return NULL_TREE;
2550 20523013 : if (same_type_ignoring_top_level_qualifiers_p
2551 20523013 : (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2552 : return NULL_TREE;
2553 20355106 : return DECL_BIT_FIELD_TYPE (field);
2554 : }
2555 :
2556 685574257 : case VAR_DECL:
2557 685574257 : if (DECL_HAS_VALUE_EXPR_P (exp))
2558 1345758 : return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2559 1345758 : (const_cast<tree> (exp)));
2560 : return NULL_TREE;
2561 :
2562 912863660 : case VIEW_CONVERT_EXPR:
2563 912863660 : if (location_wrapper_p (exp))
2564 905753007 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2565 : else
2566 : return NULL_TREE;
2567 :
2568 : default:
2569 : return NULL_TREE;
2570 : }
2571 : }
2572 :
2573 : /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2574 : bitfield with a lowered type, the type of EXP is returned, rather
2575 : than NULL_TREE. */
2576 :
2577 : tree
2578 1733282987 : unlowered_expr_type (const_tree exp)
2579 : {
2580 1733282987 : tree type;
2581 1733282987 : tree etype = TREE_TYPE (exp);
2582 :
2583 1733282987 : type = is_bitfield_expr_with_lowered_type (exp);
2584 1733282987 : if (type)
2585 16281747 : type = cp_build_qualified_type (type, cp_type_quals (etype));
2586 : else
2587 : type = etype;
2588 :
2589 1733282987 : return type;
2590 : }
2591 :
2592 : /* Perform the conversions in [expr] that apply when an lvalue appears
2593 : in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2594 : function-to-pointer conversions. In addition, bitfield references are
2595 : converted to their declared types. Note that this function does not perform
2596 : the lvalue-to-rvalue conversion for class types. If you need that conversion
2597 : for class types, then you probably need to use force_rvalue.
2598 :
2599 : Although the returned value is being used as an rvalue, this
2600 : function does not wrap the returned expression in a
2601 : NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2602 : that the return value is no longer an lvalue. */
2603 :
2604 : tree
2605 1221483836 : decay_conversion (tree exp,
2606 : tsubst_flags_t complain,
2607 : bool reject_builtin /* = true */)
2608 : {
2609 1221483836 : tree type;
2610 1221483836 : enum tree_code code;
2611 1221483836 : location_t loc = cp_expr_loc_or_input_loc (exp);
2612 :
2613 1221483836 : type = TREE_TYPE (exp);
2614 1221483836 : if (type == error_mark_node)
2615 : return error_mark_node;
2616 :
2617 1221483622 : exp = resolve_nondeduced_context_or_error (exp, complain);
2618 :
2619 1221483622 : code = TREE_CODE (type);
2620 :
2621 1221483622 : if (error_operand_p (exp))
2622 90 : return error_mark_node;
2623 :
2624 1221483532 : if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2625 : {
2626 1791423 : mark_rvalue_use (exp, loc, reject_builtin);
2627 1791423 : return nullptr_node;
2628 : }
2629 :
2630 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2631 : Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2632 1219692109 : if (code == VOID_TYPE)
2633 : {
2634 5798 : if (complain & tf_error)
2635 3 : error_at (loc, "void value not ignored as it ought to be");
2636 5798 : return error_mark_node;
2637 : }
2638 1219686311 : if (invalid_nonstatic_memfn_p (loc, exp, complain))
2639 6 : return error_mark_node;
2640 1219686305 : if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2641 : {
2642 134256778 : exp = mark_lvalue_use (exp);
2643 134256778 : if (reject_builtin && reject_gcc_builtin (exp, loc))
2644 109 : return error_mark_node;
2645 134256669 : return cp_build_addr_expr (exp, complain);
2646 : }
2647 1085429527 : if (code == ARRAY_TYPE)
2648 : {
2649 6779300 : tree adr;
2650 6779300 : tree ptrtype;
2651 :
2652 6779300 : exp = mark_lvalue_use (exp);
2653 :
2654 6779300 : if (INDIRECT_REF_P (exp))
2655 169031 : return build_nop (build_pointer_type (TREE_TYPE (type)),
2656 338062 : TREE_OPERAND (exp, 0));
2657 :
2658 6610269 : if (TREE_CODE (exp) == COMPOUND_EXPR)
2659 : {
2660 3 : tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2661 3 : if (op1 == error_mark_node)
2662 : return error_mark_node;
2663 3 : return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2664 6 : TREE_OPERAND (exp, 0), op1);
2665 : }
2666 :
2667 6610266 : if (!obvalue_p (exp)
2668 6610266 : && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2669 : {
2670 0 : if (complain & tf_error)
2671 0 : error_at (loc, "invalid use of non-lvalue array");
2672 0 : return error_mark_node;
2673 : }
2674 :
2675 6610266 : ptrtype = build_pointer_type (TREE_TYPE (type));
2676 :
2677 6610266 : if (VAR_P (exp))
2678 : {
2679 578636 : if (!cxx_mark_addressable (exp))
2680 0 : return error_mark_node;
2681 578636 : adr = build_nop (ptrtype, build_address (exp));
2682 578636 : return adr;
2683 : }
2684 : /* This way is better for a COMPONENT_REF since it can
2685 : simplify the offset for a component. */
2686 6031630 : adr = cp_build_addr_expr (exp, complain);
2687 6031630 : return cp_convert (ptrtype, adr, complain);
2688 : }
2689 :
2690 : /* Otherwise, it's the lvalue-to-rvalue conversion. */
2691 1078650227 : exp = mark_rvalue_use (exp, loc, reject_builtin);
2692 :
2693 : /* If a bitfield is used in a context where integral promotion
2694 : applies, then the caller is expected to have used
2695 : default_conversion. That function promotes bitfields correctly
2696 : before calling this function. At this point, if we have a
2697 : bitfield referenced, we may assume that is not subject to
2698 : promotion, and that, therefore, the type of the resulting rvalue
2699 : is the declared type of the bitfield. */
2700 1078650227 : exp = convert_bitfield_to_declared_type (exp);
2701 :
2702 : /* We do not call rvalue() here because we do not want to wrap EXP
2703 : in a NON_LVALUE_EXPR. */
2704 :
2705 : /* [basic.lval]
2706 :
2707 : Non-class rvalues always have cv-unqualified types. */
2708 1078650227 : type = TREE_TYPE (exp);
2709 1078650227 : if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2710 333558275 : exp = build_nop (cv_unqualified (type), exp);
2711 :
2712 1078650227 : if (!complete_type_or_maybe_complain (type, exp, complain))
2713 42 : return error_mark_node;
2714 :
2715 : return exp;
2716 : }
2717 :
2718 : /* Perform preparatory conversions, as part of the "usual arithmetic
2719 : conversions". In particular, as per [expr]:
2720 :
2721 : Whenever an lvalue expression appears as an operand of an
2722 : operator that expects the rvalue for that operand, the
2723 : lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2724 : standard conversions are applied to convert the expression to an
2725 : rvalue.
2726 :
2727 : In addition, we perform integral promotions here, as those are
2728 : applied to both operands to a binary operator before determining
2729 : what additional conversions should apply. */
2730 :
2731 : static tree
2732 318125807 : cp_default_conversion (tree exp, tsubst_flags_t complain)
2733 : {
2734 : /* Check for target-specific promotions. */
2735 318125807 : tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2736 318125807 : if (promoted_type)
2737 0 : exp = cp_convert (promoted_type, exp, complain);
2738 : /* Perform the integral promotions first so that bitfield
2739 : expressions (which may promote to "int", even if the bitfield is
2740 : declared "unsigned") are promoted correctly. */
2741 318125807 : else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2742 220588850 : exp = cp_perform_integral_promotions (exp, complain);
2743 : /* Perform the other conversions. */
2744 318125807 : exp = decay_conversion (exp, complain);
2745 :
2746 318125807 : return exp;
2747 : }
2748 :
2749 : /* C version. */
2750 :
2751 : tree
2752 41736244 : default_conversion (tree exp)
2753 : {
2754 41736244 : return cp_default_conversion (exp, tf_warning_or_error);
2755 : }
2756 :
2757 : /* EXPR is an expression with an integral or enumeration type.
2758 : Perform the integral promotions in [conv.prom], and return the
2759 : converted value. */
2760 :
2761 : tree
2762 226910853 : cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2763 : {
2764 226910853 : tree type;
2765 226910853 : tree promoted_type;
2766 :
2767 226910853 : expr = mark_rvalue_use (expr);
2768 226910853 : if (error_operand_p (expr))
2769 6 : return error_mark_node;
2770 :
2771 226910847 : type = TREE_TYPE (expr);
2772 :
2773 : /* [conv.prom]
2774 :
2775 : A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2776 : of type int if int can represent all the values of the bit-field;
2777 : otherwise, it can be converted to unsigned int if unsigned int can
2778 : represent all the values of the bit-field. If the bit-field is larger yet,
2779 : no integral promotion applies to it. If the bit-field has an enumerated
2780 : type, it is treated as any other value of that type for promotion
2781 : purposes. */
2782 226910847 : tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2783 226910847 : if (bitfield_type
2784 226910847 : && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2785 543751 : || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2786 : type = bitfield_type;
2787 :
2788 226910847 : gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2789 : /* Scoped enums don't promote. */
2790 226910847 : if (SCOPED_ENUM_P (type))
2791 : return expr;
2792 226350259 : promoted_type = type_promotes_to (type);
2793 226350259 : if (type != promoted_type)
2794 68377033 : expr = cp_convert (promoted_type, expr, complain);
2795 157973226 : else if (bitfield_type && bitfield_type != type)
2796 : /* Prevent decay_conversion from converting to bitfield_type. */
2797 159 : expr = build_nop (type, expr);
2798 : return expr;
2799 : }
2800 :
2801 : /* C version. */
2802 :
2803 : tree
2804 1792675 : perform_integral_promotions (tree expr)
2805 : {
2806 1792675 : return cp_perform_integral_promotions (expr, tf_warning_or_error);
2807 : }
2808 :
2809 : /* Returns nonzero iff exp is a STRING_CST or the result of applying
2810 : decay_conversion to one. */
2811 :
2812 : int
2813 3872781 : string_conv_p (const_tree totype, const_tree exp, int warn)
2814 : {
2815 3872781 : tree t;
2816 :
2817 3872781 : if (!TYPE_PTR_P (totype))
2818 : return 0;
2819 :
2820 3871890 : t = TREE_TYPE (totype);
2821 3871890 : if (!same_type_p (t, char_type_node)
2822 3719136 : && !same_type_p (t, char8_type_node)
2823 3654286 : && !same_type_p (t, char16_type_node)
2824 3601375 : && !same_type_p (t, char32_type_node)
2825 7420355 : && !same_type_p (t, wchar_type_node))
2826 : return 0;
2827 :
2828 365500 : location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2829 :
2830 365500 : STRIP_ANY_LOCATION_WRAPPER (exp);
2831 :
2832 365500 : if (TREE_CODE (exp) == STRING_CST)
2833 : {
2834 : /* Make sure that we don't try to convert between char and wide chars. */
2835 108 : if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2836 : return 0;
2837 : }
2838 : else
2839 : {
2840 : /* Is this a string constant which has decayed to 'const char *'? */
2841 365392 : t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2842 365392 : if (!same_type_p (TREE_TYPE (exp), t))
2843 : return 0;
2844 14883 : STRIP_NOPS (exp);
2845 14883 : if (TREE_CODE (exp) != ADDR_EXPR
2846 14883 : || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2847 : return 0;
2848 : }
2849 306 : if (warn)
2850 : {
2851 102 : if (cxx_dialect >= cxx11)
2852 87 : pedwarn (loc, OPT_Wwrite_strings,
2853 : "ISO C++ forbids converting a string constant to %qT",
2854 : totype);
2855 : else
2856 15 : warning_at (loc, OPT_Wwrite_strings,
2857 : "deprecated conversion from string constant to %qT",
2858 : totype);
2859 : }
2860 :
2861 : return 1;
2862 : }
2863 :
2864 : /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2865 : can, for example, use as an lvalue. This code used to be in
2866 : unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2867 : expressions, where we're dealing with aggregates. But now it's again only
2868 : called from unary_complex_lvalue. The case (in particular) that led to
2869 : this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2870 : get it there. */
2871 :
2872 : static tree
2873 87263 : rationalize_conditional_expr (enum tree_code code, tree t,
2874 : tsubst_flags_t complain)
2875 : {
2876 87263 : location_t loc = cp_expr_loc_or_input_loc (t);
2877 :
2878 : /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2879 : the first operand is always the one to be used if both operands
2880 : are equal, so we know what conditional expression this used to be. */
2881 87263 : if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2882 : {
2883 0 : tree op0 = TREE_OPERAND (t, 0);
2884 0 : tree op1 = TREE_OPERAND (t, 1);
2885 :
2886 : /* The following code is incorrect if either operand side-effects. */
2887 0 : gcc_assert (!TREE_SIDE_EFFECTS (op0)
2888 : && !TREE_SIDE_EFFECTS (op1));
2889 0 : return
2890 0 : build_conditional_expr (loc,
2891 0 : build_x_binary_op (loc,
2892 0 : (TREE_CODE (t) == MIN_EXPR
2893 : ? LE_EXPR : GE_EXPR),
2894 0 : op0, TREE_CODE (op0),
2895 0 : op1, TREE_CODE (op1),
2896 : NULL_TREE,
2897 : /*overload=*/NULL,
2898 : complain),
2899 : cp_build_unary_op (code, op0, false, complain),
2900 : cp_build_unary_op (code, op1, false, complain),
2901 : complain);
2902 : }
2903 :
2904 87263 : tree op1 = TREE_OPERAND (t, 1);
2905 87263 : if (TREE_CODE (op1) != THROW_EXPR)
2906 87260 : op1 = cp_build_unary_op (code, op1, false, complain);
2907 87263 : tree op2 = TREE_OPERAND (t, 2);
2908 87263 : if (TREE_CODE (op2) != THROW_EXPR)
2909 87257 : op2 = cp_build_unary_op (code, op2, false, complain);
2910 :
2911 87263 : return
2912 87263 : build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2913 : }
2914 :
2915 : /* Given the TYPE of an anonymous union field inside T, return the
2916 : FIELD_DECL for the field. If not found return NULL_TREE. Because
2917 : anonymous unions can nest, we must also search all anonymous unions
2918 : that are directly reachable. */
2919 :
2920 : tree
2921 1535130 : lookup_anon_field (tree, tree type)
2922 : {
2923 1535130 : tree field;
2924 :
2925 1535130 : type = TYPE_MAIN_VARIANT (type);
2926 1535130 : field = ANON_AGGR_TYPE_FIELD (type);
2927 1535130 : gcc_assert (field);
2928 1535130 : return field;
2929 : }
2930 :
2931 : /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2932 : expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2933 : non-NULL, it indicates the path to the base used to name MEMBER.
2934 : If PRESERVE_REFERENCE is true, the expression returned will have
2935 : REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2936 : returned will have the type referred to by the reference.
2937 :
2938 : This function does not perform access control; that is either done
2939 : earlier by the parser when the name of MEMBER is resolved to MEMBER
2940 : itself, or later when overload resolution selects one of the
2941 : functions indicated by MEMBER. */
2942 :
2943 : tree
2944 124519852 : build_class_member_access_expr (cp_expr object, tree member,
2945 : tree access_path, bool preserve_reference,
2946 : tsubst_flags_t complain)
2947 : {
2948 124519852 : tree object_type;
2949 124519852 : tree member_scope;
2950 124519852 : tree result = NULL_TREE;
2951 124519852 : tree using_decl = NULL_TREE;
2952 :
2953 124519852 : if (error_operand_p (object) || error_operand_p (member))
2954 59 : return error_mark_node;
2955 :
2956 124519793 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2957 :
2958 : /* [expr.ref]
2959 :
2960 : The type of the first expression shall be "class object" (of a
2961 : complete type). */
2962 124519793 : object_type = TREE_TYPE (object);
2963 124519793 : if (!currently_open_class (object_type)
2964 124519793 : && !complete_type_or_maybe_complain (object_type, object, complain))
2965 0 : return error_mark_node;
2966 124519793 : if (!CLASS_TYPE_P (object_type))
2967 : {
2968 0 : if (complain & tf_error)
2969 : {
2970 0 : if (INDIRECT_TYPE_P (object_type)
2971 0 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
2972 0 : error ("request for member %qD in %qE, which is of pointer "
2973 : "type %qT (maybe you meant to use %<->%> ?)",
2974 : member, object.get_value (), object_type);
2975 : else
2976 0 : error ("request for member %qD in %qE, which is of non-class "
2977 : "type %qT", member, object.get_value (), object_type);
2978 : }
2979 0 : return error_mark_node;
2980 : }
2981 :
2982 : /* The standard does not seem to actually say that MEMBER must be a
2983 : member of OBJECT_TYPE. However, that is clearly what is
2984 : intended. */
2985 124519793 : if (DECL_P (member))
2986 : {
2987 53903969 : member_scope = DECL_CLASS_CONTEXT (member);
2988 53903969 : if (!mark_used (member, complain) && !(complain & tf_error))
2989 0 : return error_mark_node;
2990 :
2991 53903969 : if (TREE_UNAVAILABLE (member))
2992 30 : error_unavailable_use (member, NULL_TREE);
2993 53903939 : else if (TREE_DEPRECATED (member))
2994 30 : warn_deprecated_use (member, NULL_TREE);
2995 : }
2996 : else
2997 70615824 : member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2998 : /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2999 : presently be the anonymous union. Go outwards until we find a
3000 : type related to OBJECT_TYPE. */
3001 126058530 : while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
3002 127597823 : && !same_type_ignoring_top_level_qualifiers_p (member_scope,
3003 : object_type))
3004 1538737 : member_scope = TYPE_CONTEXT (member_scope);
3005 124519793 : if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
3006 : {
3007 19 : if (complain & tf_error)
3008 : {
3009 19 : if (TREE_CODE (member) == FIELD_DECL)
3010 4 : error ("invalid use of non-static data member %qE", member);
3011 : else
3012 15 : error ("%qD is not a member of %qT", member, object_type);
3013 : }
3014 19 : return error_mark_node;
3015 : }
3016 :
3017 : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
3018 : `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
3019 : in the front end; only _DECLs and _REFs are lvalues in the back end. */
3020 124519774 : if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
3021 : {
3022 44 : temp = cp_build_fold_indirect_ref (temp);
3023 44 : if (!lvalue_p (object) && lvalue_p (temp))
3024 : /* Preserve rvalueness. */
3025 6 : temp = move (temp);
3026 44 : object = temp;
3027 : }
3028 :
3029 : /* In [expr.ref], there is an explicit list of the valid choices for
3030 : MEMBER. We check for each of those cases here. */
3031 124519774 : if (VAR_P (member))
3032 : {
3033 : /* A static data member. */
3034 102227 : result = member;
3035 102227 : mark_exp_read (object);
3036 :
3037 102227 : if (tree wrap = maybe_get_tls_wrapper_call (result))
3038 : /* Replace an evaluated use of the thread_local variable with
3039 : a call to its wrapper. */
3040 102 : result = wrap;
3041 :
3042 : /* If OBJECT has side-effects, they are supposed to occur. */
3043 102227 : if (TREE_SIDE_EFFECTS (object))
3044 4827 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
3045 : }
3046 124417547 : else if (TREE_CODE (member) == FIELD_DECL)
3047 : {
3048 : /* A non-static data member. */
3049 53801511 : bool null_object_p;
3050 53801511 : int type_quals;
3051 53801511 : tree member_type;
3052 :
3053 53801511 : if (INDIRECT_REF_P (object))
3054 40659265 : null_object_p =
3055 40659265 : integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
3056 : else
3057 : null_object_p = false;
3058 :
3059 : /* Convert OBJECT to the type of MEMBER. */
3060 53801511 : if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
3061 : TYPE_MAIN_VARIANT (member_scope)))
3062 : {
3063 2043505 : tree binfo;
3064 2043505 : base_kind kind;
3065 :
3066 : /* We didn't complain above about a currently open class, but now we
3067 : must: we don't know how to refer to a base member before layout is
3068 : complete. But still don't complain in a template. */
3069 2043505 : if (!cp_unevaluated_operand
3070 2043020 : && !dependent_type_p (object_type)
3071 3795122 : && !complete_type_or_maybe_complain (object_type, object,
3072 : complain))
3073 17 : return error_mark_node;
3074 :
3075 2770505 : binfo = lookup_base (access_path ? access_path : object_type,
3076 : member_scope, ba_unique, &kind, complain);
3077 2043502 : if (binfo == error_mark_node)
3078 : return error_mark_node;
3079 :
3080 : /* It is invalid to try to get to a virtual base of a
3081 : NULL object. The most common cause is invalid use of
3082 : offsetof macro. */
3083 2043494 : if (null_object_p && kind == bk_via_virtual)
3084 : {
3085 6 : if (complain & tf_error)
3086 : {
3087 6 : error ("invalid access to non-static data member %qD in "
3088 : "virtual base of NULL object", member);
3089 : }
3090 6 : return error_mark_node;
3091 : }
3092 :
3093 : /* Convert to the base. */
3094 2043488 : object = build_base_path (PLUS_EXPR, object, binfo,
3095 : /*nonnull=*/1, complain);
3096 : /* If we found the base successfully then we should be able
3097 : to convert to it successfully. */
3098 2043488 : gcc_assert (object != error_mark_node || seen_error ());
3099 : }
3100 :
3101 : /* If MEMBER is from an anonymous aggregate, we have converted
3102 : OBJECT so that it refers to the class containing the
3103 : anonymous union. Generate a reference to the anonymous union
3104 : itself, and recur to find MEMBER. */
3105 107602988 : if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
3106 : /* When this code is called from build_field_call, the
3107 : object already has the type of the anonymous union.
3108 : That is because the COMPONENT_REF was already
3109 : constructed, and was then disassembled before calling
3110 : build_field_call. After the function-call code is
3111 : cleaned up, this waste can be eliminated. */
3112 55337034 : && (!same_type_ignoring_top_level_qualifiers_p
3113 1535540 : (TREE_TYPE (object), DECL_CONTEXT (member))))
3114 : {
3115 1534990 : tree anonymous_union;
3116 :
3117 1534990 : anonymous_union = lookup_anon_field (TREE_TYPE (object),
3118 1534990 : DECL_CONTEXT (member));
3119 1534990 : object = build_class_member_access_expr (object,
3120 : anonymous_union,
3121 : /*access_path=*/NULL_TREE,
3122 : preserve_reference,
3123 : complain);
3124 : }
3125 :
3126 : /* Compute the type of the field, as described in [expr.ref]. */
3127 53801494 : type_quals = TYPE_UNQUALIFIED;
3128 53801494 : member_type = TREE_TYPE (member);
3129 53801494 : if (!TYPE_REF_P (member_type))
3130 : {
3131 52840419 : type_quals = (cp_type_quals (member_type)
3132 52840419 : | cp_type_quals (object_type));
3133 :
3134 : /* A field is const (volatile) if the enclosing object, or the
3135 : field itself, is const (volatile). But, a mutable field is
3136 : not const, even within a const object. */
3137 52840419 : if (DECL_MUTABLE_P (member))
3138 327516 : type_quals &= ~TYPE_QUAL_CONST;
3139 52840419 : member_type = cp_build_qualified_type (member_type, type_quals);
3140 : }
3141 :
3142 53801494 : result = build3_loc (input_location, COMPONENT_REF, member_type,
3143 : object, member, NULL_TREE);
3144 :
3145 : /* Mark the expression const or volatile, as appropriate. Even
3146 : though we've dealt with the type above, we still have to mark the
3147 : expression itself. */
3148 53801494 : if (type_quals & TYPE_QUAL_CONST)
3149 16765723 : TREE_READONLY (result) = 1;
3150 53801494 : if (type_quals & TYPE_QUAL_VOLATILE)
3151 186726 : TREE_THIS_VOLATILE (result) = 1;
3152 : }
3153 70616036 : else if (BASELINK_P (member))
3154 : {
3155 : /* The member is a (possibly overloaded) member function. */
3156 70615809 : tree functions;
3157 70615809 : tree type;
3158 :
3159 : /* If the MEMBER is exactly one static member function, then we
3160 : know the type of the expression. Otherwise, we must wait
3161 : until overload resolution has been performed. */
3162 70615809 : functions = BASELINK_FUNCTIONS (member);
3163 70615809 : if (TREE_CODE (functions) == OVERLOAD && OVL_SINGLE_P (functions))
3164 70615809 : functions = OVL_FIRST (functions);
3165 70615809 : if (TREE_CODE (functions) == FUNCTION_DECL
3166 70615809 : && DECL_STATIC_FUNCTION_P (functions))
3167 634169 : type = TREE_TYPE (functions);
3168 : else
3169 69981640 : type = unknown_type_node;
3170 : /* Note that we do not convert OBJECT to the BASELINK_BINFO
3171 : base. That will happen when the function is called. */
3172 70615809 : result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3173 : NULL_TREE);
3174 : }
3175 227 : else if (TREE_CODE (member) == CONST_DECL)
3176 : {
3177 : /* The member is an enumerator. */
3178 215 : result = member;
3179 : /* If OBJECT has side-effects, they are supposed to occur. */
3180 215 : if (TREE_SIDE_EFFECTS (object))
3181 6 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3182 : object, result);
3183 : }
3184 12 : else if ((using_decl = strip_using_decl (member)) != member)
3185 0 : result = build_class_member_access_expr (object,
3186 : using_decl,
3187 : access_path, preserve_reference,
3188 : complain);
3189 : else
3190 : {
3191 12 : if (complain & tf_error)
3192 12 : error ("invalid use of %qD", member);
3193 12 : return error_mark_node;
3194 : }
3195 :
3196 124519745 : if (!preserve_reference)
3197 : /* [expr.ref]
3198 :
3199 : If E2 is declared to have type "reference to T", then ... the
3200 : type of E1.E2 is T. */
3201 117574306 : result = convert_from_reference (result);
3202 :
3203 : return result;
3204 : }
3205 :
3206 : /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3207 : SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3208 :
3209 : tree
3210 218857 : lookup_destructor (tree object, tree scope, tree dtor_name,
3211 : tsubst_flags_t complain)
3212 : {
3213 218857 : tree object_type = TREE_TYPE (object);
3214 218857 : tree dtor_type = TREE_OPERAND (dtor_name, 0);
3215 218857 : tree expr;
3216 :
3217 : /* We've already complained about this destructor. */
3218 218857 : if (dtor_type == error_mark_node)
3219 : return error_mark_node;
3220 :
3221 218851 : if (scope && !check_dtor_name (scope, dtor_type))
3222 : {
3223 3 : if (complain & tf_error)
3224 3 : error ("qualified type %qT does not match destructor name ~%qT",
3225 : scope, dtor_type);
3226 3 : return error_mark_node;
3227 : }
3228 218848 : if (is_auto (dtor_type))
3229 : dtor_type = object_type;
3230 218845 : else if (identifier_p (dtor_type))
3231 : {
3232 : /* In a template, names we can't find a match for are still accepted
3233 : destructor names, and we check them here. */
3234 23 : if (check_dtor_name (object_type, dtor_type))
3235 : dtor_type = object_type;
3236 : else
3237 : {
3238 3 : if (complain & tf_error)
3239 3 : error ("object type %qT does not match destructor name ~%qT",
3240 : object_type, dtor_type);
3241 3 : return error_mark_node;
3242 : }
3243 :
3244 : }
3245 218822 : else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3246 : {
3247 8 : if (complain & tf_error)
3248 8 : error ("the type being destroyed is %qT, but the destructor "
3249 8 : "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3250 8 : return error_mark_node;
3251 : }
3252 218837 : expr = lookup_member (dtor_type, complete_dtor_identifier,
3253 : /*protect=*/1, /*want_type=*/false,
3254 : tf_warning_or_error);
3255 218837 : if (!expr)
3256 : {
3257 6 : if (complain & tf_error)
3258 6 : cxx_incomplete_type_error (dtor_name, dtor_type);
3259 6 : return error_mark_node;
3260 : }
3261 218831 : expr = (adjust_result_of_qualified_name_lookup
3262 218831 : (expr, dtor_type, object_type));
3263 218831 : if (scope == NULL_TREE)
3264 : /* We need to call adjust_result_of_qualified_name_lookup in case the
3265 : destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3266 : that we still get virtual function binding. */
3267 218688 : BASELINK_QUALIFIED_P (expr) = false;
3268 : return expr;
3269 : }
3270 :
3271 : /* An expression of the form "A::template B" has been resolved to
3272 : DECL. Issue a diagnostic if B is not a template or template
3273 : specialization. */
3274 :
3275 : void
3276 278554 : check_template_keyword (tree decl)
3277 : {
3278 : /* The standard says:
3279 :
3280 : [temp.names]
3281 :
3282 : If a name prefixed by the keyword template is not a member
3283 : template, the program is ill-formed.
3284 :
3285 : DR 228 removed the restriction that the template be a member
3286 : template.
3287 :
3288 : DR 96, if accepted would add the further restriction that explicit
3289 : template arguments must be provided if the template keyword is
3290 : used, but, as of 2005-10-16, that DR is still in "drafting". If
3291 : this DR is accepted, then the semantic checks here can be
3292 : simplified, as the entity named must in fact be a template
3293 : specialization, rather than, as at present, a set of overloaded
3294 : functions containing at least one template function. */
3295 278554 : if (TREE_CODE (decl) != TEMPLATE_DECL
3296 278554 : && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3297 : {
3298 67808 : if (VAR_P (decl))
3299 : {
3300 9729 : if (DECL_USE_TEMPLATE (decl)
3301 9729 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3302 : ;
3303 : else
3304 0 : permerror (input_location, "%qD is not a template", decl);
3305 : }
3306 58079 : else if (!is_overloaded_fn (decl))
3307 0 : permerror (input_location, "%qD is not a template", decl);
3308 : else
3309 : {
3310 58079 : bool found = false;
3311 :
3312 116158 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3313 116160 : !found && iter; ++iter)
3314 : {
3315 58081 : tree fn = *iter;
3316 58081 : if (TREE_CODE (fn) == TEMPLATE_DECL
3317 57685 : || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3318 58095 : || (TREE_CODE (fn) == FUNCTION_DECL
3319 14 : && DECL_USE_TEMPLATE (fn)
3320 6 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3321 : found = true;
3322 : }
3323 58079 : if (!found)
3324 12 : permerror (input_location, "%qD is not a template", decl);
3325 : }
3326 : }
3327 278554 : }
3328 :
3329 : /* Record that an access failure occurred on BASETYPE_PATH attempting
3330 : to access DECL, where DIAG_DECL should be used for diagnostics. */
3331 :
3332 : void
3333 412 : access_failure_info::record_access_failure (tree basetype_path,
3334 : tree decl, tree diag_decl)
3335 : {
3336 412 : m_was_inaccessible = true;
3337 412 : m_basetype_path = basetype_path;
3338 412 : m_decl = decl;
3339 412 : m_diag_decl = diag_decl;
3340 412 : }
3341 :
3342 : /* If an access failure was recorded, then attempt to locate an
3343 : accessor function for the pertinent field.
3344 : Otherwise, return NULL_TREE. */
3345 :
3346 : tree
3347 99815183 : access_failure_info::get_any_accessor (bool const_p) const
3348 : {
3349 99815183 : if (!was_inaccessible_p ())
3350 : return NULL_TREE;
3351 :
3352 412 : tree accessor
3353 412 : = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3354 412 : if (!accessor)
3355 : return NULL_TREE;
3356 :
3357 : /* The accessor must itself be accessible for it to be a reasonable
3358 : suggestion. */
3359 161 : if (!accessible_p (m_basetype_path, accessor, true))
3360 : return NULL_TREE;
3361 :
3362 : return accessor;
3363 : }
3364 :
3365 : /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3366 : replacing the primary location in RICHLOC with "accessor()". */
3367 :
3368 : void
3369 140 : access_failure_info::add_fixit_hint (rich_location *richloc,
3370 : tree accessor_decl)
3371 : {
3372 140 : pretty_printer pp;
3373 140 : pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3374 140 : pp_string (&pp, "()");
3375 140 : richloc->add_fixit_replace (pp_formatted_text (&pp));
3376 140 : }
3377 :
3378 : /* If an access failure was recorded, then attempt to locate an
3379 : accessor function for the pertinent field, and if one is
3380 : available, add a note and fix-it hint suggesting using it. */
3381 :
3382 : void
3383 99815113 : access_failure_info::maybe_suggest_accessor (bool const_p) const
3384 : {
3385 99815113 : tree accessor = get_any_accessor (const_p);
3386 99815113 : if (accessor == NULL_TREE)
3387 99815015 : return;
3388 98 : rich_location richloc (line_table, input_location);
3389 98 : add_fixit_hint (&richloc, accessor);
3390 98 : inform (&richloc, "field %q#D can be accessed via %q#D",
3391 98 : m_diag_decl, accessor);
3392 98 : }
3393 :
3394 : /* Subroutine of finish_class_member_access_expr.
3395 : Issue an error about NAME not being a member of ACCESS_PATH (or
3396 : OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3397 : names. */
3398 :
3399 : static void
3400 330 : complain_about_unrecognized_member (tree access_path, tree name,
3401 : tree object_type)
3402 : {
3403 : /* Attempt to provide a hint about misspelled names. */
3404 330 : tree guessed_id = lookup_member_fuzzy (access_path, name,
3405 : /*want_type=*/false);
3406 330 : if (guessed_id == NULL_TREE)
3407 : {
3408 : /* No hint. */
3409 223 : error ("%q#T has no member named %qE",
3410 223 : TREE_CODE (access_path) == TREE_BINFO
3411 6 : ? TREE_TYPE (access_path) : object_type, name);
3412 223 : return;
3413 : }
3414 :
3415 107 : location_t bogus_component_loc = input_location;
3416 107 : gcc_rich_location rich_loc (bogus_component_loc);
3417 :
3418 : /* Check that the guessed name is accessible along access_path. */
3419 107 : access_failure_info afi;
3420 107 : lookup_member (access_path, guessed_id, /*protect=*/1,
3421 : /*want_type=*/false, /*complain=*/false,
3422 : &afi);
3423 107 : if (afi.was_inaccessible_p ())
3424 : {
3425 70 : tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3426 70 : if (accessor)
3427 : {
3428 : /* The guessed name isn't directly accessible, but can be accessed
3429 : via an accessor member function. */
3430 42 : afi.add_fixit_hint (&rich_loc, accessor);
3431 42 : error_at (&rich_loc,
3432 : "%q#T has no member named %qE;"
3433 : " did you mean %q#D? (accessible via %q#D)",
3434 42 : TREE_CODE (access_path) == TREE_BINFO
3435 0 : ? TREE_TYPE (access_path) : object_type,
3436 : name, afi.get_diag_decl (), accessor);
3437 : }
3438 : else
3439 : {
3440 : /* The guessed name isn't directly accessible, and no accessor
3441 : member function could be found. */
3442 28 : auto_diagnostic_group d;
3443 28 : error_at (&rich_loc,
3444 : "%q#T has no member named %qE;"
3445 : " did you mean %q#D? (not accessible from this context)",
3446 28 : TREE_CODE (access_path) == TREE_BINFO
3447 0 : ? TREE_TYPE (access_path) : object_type,
3448 : name, afi.get_diag_decl ());
3449 28 : complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3450 : afi.get_diag_decl (), false, ak_none);
3451 28 : }
3452 : }
3453 : else
3454 : {
3455 : /* The guessed name is directly accessible; suggest it. */
3456 37 : rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3457 : guessed_id);
3458 37 : error_at (&rich_loc,
3459 : "%q#T has no member named %qE;"
3460 : " did you mean %qE?",
3461 37 : TREE_CODE (access_path) == TREE_BINFO
3462 0 : ? TREE_TYPE (access_path) : object_type,
3463 : name, guessed_id);
3464 : }
3465 107 : }
3466 :
3467 : /* This function is called by the parser to process a class member
3468 : access expression of the form OBJECT.NAME. NAME is a node used by
3469 : the parser to represent a name; it is not yet a DECL, except when
3470 : SPLICE_P. It may, however, be a BASELINK where the BASELINK_FUNCTIONS
3471 : is a TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3472 : there is no reason to do the lookup twice, so the parser keeps the
3473 : BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3474 : be a template via the use of the "A::template B" syntax. SPLICE_P
3475 : is true if NAME was designated by a splice-expression. */
3476 :
3477 : tree
3478 199125789 : finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3479 : tsubst_flags_t complain,
3480 : bool splice_p/*=false*/)
3481 : {
3482 199125789 : tree expr;
3483 199125789 : tree object_type;
3484 199125789 : tree member;
3485 199125789 : tree access_path = NULL_TREE;
3486 199125789 : tree orig_object = object;
3487 199125789 : tree orig_name = name;
3488 :
3489 199125789 : if (object == error_mark_node || name == error_mark_node)
3490 : return error_mark_node;
3491 :
3492 : /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3493 199125246 : if (!objc_is_public (object, name))
3494 0 : return error_mark_node;
3495 :
3496 199125246 : object_type = TREE_TYPE (object);
3497 :
3498 199125246 : if (processing_template_decl)
3499 : {
3500 164627548 : if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3501 164627548 : type_dependent_object_expression_p (object)
3502 : /* If NAME is "f<args>", where either 'f' or 'args' is
3503 : dependent, then the expression is dependent. */
3504 77572061 : || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3505 625369 : && dependent_template_id_p (TREE_OPERAND (name, 0),
3506 625369 : TREE_OPERAND (name, 1)))
3507 : /* If NAME is "T::X" where "T" is dependent, then the
3508 : expression is dependent. */
3509 77251928 : || (TREE_CODE (name) == SCOPE_REF
3510 107414 : && TYPE_P (TREE_OPERAND (name, 0))
3511 107414 : && dependent_scope_p (TREE_OPERAND (name, 0)))
3512 : /* If NAME is operator T where "T" is dependent, we can't
3513 : lookup until we instantiate the T. */
3514 77144737 : || (TREE_CODE (name) == IDENTIFIER_NODE
3515 76502201 : && IDENTIFIER_CONV_OP_P (name)
3516 100 : && dependent_type_p (TREE_TYPE (name)))
3517 : /* This is OBJECT.[:R:], which is dependent. */
3518 77144643 : || dependent_splice_p (name)
3519 : /* This is OBJECT.[:T::R:], which is dependent. */
3520 241771769 : || (TREE_CODE (name) == SCOPE_REF
3521 223 : && dependent_splice_p (TREE_OPERAND (name, 1))))
3522 : {
3523 102282745 : dependent:
3524 102282745 : expr = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3525 : orig_object, orig_name, NULL_TREE);
3526 102282745 : COMPONENT_REF_SPLICE_P (expr) = splice_p;
3527 102282745 : return expr;
3528 : }
3529 : }
3530 34497698 : else if (c_dialect_objc ()
3531 111641919 : && identifier_p (name)
3532 34497698 : && (expr = objc_maybe_build_component_ref (object, name)))
3533 : return expr;
3534 :
3535 : /* [expr.ref]
3536 :
3537 : The type of the first expression shall be "class object" (of a
3538 : complete type). */
3539 111641919 : if (!currently_open_class (object_type)
3540 111641919 : && !complete_type_or_maybe_complain (object_type, object, complain))
3541 123 : return error_mark_node;
3542 111641796 : if (!CLASS_TYPE_P (object_type))
3543 : {
3544 287140 : if (complain & tf_error)
3545 : {
3546 135 : if (INDIRECT_TYPE_P (object_type)
3547 135 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
3548 23 : error ("request for member %qD in %qE, which is of pointer "
3549 : "type %qT (maybe you meant to use %<->%> ?)",
3550 : name, object.get_value (), object_type);
3551 : else
3552 112 : error ("request for member %qD in %qE, which is of non-class "
3553 : "type %qT", name, object.get_value (), object_type);
3554 : }
3555 287140 : return error_mark_node;
3556 : }
3557 :
3558 111354656 : if (BASELINK_P (name))
3559 : /* A member function that has already been looked up. */
3560 : member = name;
3561 100083793 : else if (TREE_CODE (name) == TREE_BINFO)
3562 : {
3563 : /* Splicing a base class subobject. We can only get here via
3564 : e1.[:e2:]. */
3565 92 : expr = build_base_path (PLUS_EXPR, object, name, /*nonnull=*/true,
3566 : complain);
3567 92 : if (expr == error_mark_node && (complain & tf_error))
3568 4 : error_not_base_type (BINFO_TYPE (name), object_type);
3569 92 : return expr;
3570 : }
3571 : else
3572 : {
3573 100083701 : bool is_template_id = false;
3574 100083701 : tree template_args = NULL_TREE;
3575 100083701 : tree scope = NULL_TREE;
3576 :
3577 100083701 : access_path = object_type;
3578 :
3579 100083701 : if (TREE_CODE (name) == SCOPE_REF)
3580 : {
3581 : /* A qualified name. The qualifying class or namespace `S'
3582 : has already been looked up; it is either a TYPE or a
3583 : NAMESPACE_DECL. */
3584 1787 : scope = TREE_OPERAND (name, 0);
3585 1787 : name = TREE_OPERAND (name, 1);
3586 :
3587 : /* If SCOPE is a namespace, then the qualified name does not
3588 : name a member of OBJECT_TYPE. */
3589 1787 : if (TREE_CODE (scope) == NAMESPACE_DECL)
3590 : {
3591 0 : if (complain & tf_error)
3592 0 : error ("%<%D::%D%> is not a member of %qT",
3593 : scope, name, object_type);
3594 0 : return error_mark_node;
3595 : }
3596 : }
3597 100081914 : else if (splice_p && valid_splice_for_member_access_p (name))
3598 : {
3599 1336 : scope = context_for_name_lookup (OVL_FIRST (name));
3600 1336 : if (!CLASS_TYPE_P (scope))
3601 : {
3602 10 : if (complain & tf_error)
3603 : {
3604 10 : auto_diagnostic_group d;
3605 10 : error ("%q#D is not a member of %qT", name, object_type);
3606 20 : inform (DECL_SOURCE_LOCATION (OVL_FIRST (name)), "declared here");
3607 10 : }
3608 10 : return error_mark_node;
3609 : }
3610 : }
3611 :
3612 100083691 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3613 : {
3614 463800 : is_template_id = true;
3615 463800 : template_args = TREE_OPERAND (name, 1);
3616 463800 : name = TREE_OPERAND (name, 0);
3617 :
3618 463800 : if (splice_p
3619 465258 : && (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (name))
3620 162 : || variable_template_p (name)))
3621 486 : scope = DECL_CONTEXT (OVL_FIRST (name));
3622 898054 : else if (!identifier_p (name))
3623 434810 : name = OVL_NAME (name);
3624 : }
3625 :
3626 100083691 : if (scope)
3627 : {
3628 3599 : if (TREE_CODE (scope) == ENUMERAL_TYPE)
3629 : {
3630 12 : gcc_assert (!is_template_id);
3631 : /* Looking up a member enumerator (c++/56793). */
3632 24 : if (!TYPE_CLASS_SCOPE_P (scope)
3633 21 : || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3634 : {
3635 3 : if (complain & tf_error)
3636 3 : error ("%<%D::%D%> is not a member of %qT",
3637 : scope, name, object_type);
3638 3 : return error_mark_node;
3639 : }
3640 9 : tree val;
3641 9 : if (splice_p && TREE_CODE (name) == CONST_DECL)
3642 : val = name;
3643 : else
3644 9 : val = lookup_enumerator (scope, name);
3645 9 : if (!val)
3646 : {
3647 6 : if (complain & tf_error)
3648 6 : error ("%qD is not a member of %qD",
3649 : name, scope);
3650 6 : return error_mark_node;
3651 : }
3652 :
3653 3 : if (TREE_SIDE_EFFECTS (object))
3654 0 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3655 3 : return val;
3656 : }
3657 :
3658 3587 : gcc_assert (CLASS_TYPE_P (scope));
3659 3587 : gcc_assert (identifier_p (name)
3660 : || TREE_CODE (name) == BIT_NOT_EXPR
3661 : || (splice_p && valid_splice_for_member_access_p (name)));
3662 :
3663 3587 : if (constructor_name_p (name, scope))
3664 : {
3665 3 : if (complain & tf_error)
3666 3 : error ("cannot call constructor %<%T::%D%> directly",
3667 : scope, name);
3668 3 : return error_mark_node;
3669 : }
3670 :
3671 : /* NAME may refer to a static data member, in which case there is
3672 : one copy of the data member that is shared by all the objects of
3673 : the class. So NAME can be unambiguously referred to even if
3674 : there are multiple indirect base classes containing NAME. */
3675 10752 : const base_access ba = [scope, name, splice_p] ()
3676 : {
3677 : /* [class.access.base]/5: A member m is accessible at the
3678 : point R when designated in class N if
3679 : -- m is designated by a splice-expression */
3680 3584 : if (splice_p)
3681 : return ba_unique;
3682 1772 : if (identifier_p (name))
3683 : {
3684 1635 : tree m = lookup_member (scope, name, /*protect=*/0,
3685 : /*want_type=*/false, tf_none);
3686 1635 : if (!m || shared_member_p (m))
3687 32 : return ba_any;
3688 : }
3689 : return ba_check;
3690 3584 : } ();
3691 :
3692 : /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3693 3584 : access_path = lookup_base (object_type, scope, ba, NULL, complain);
3694 3584 : if (access_path == error_mark_node)
3695 : return error_mark_node;
3696 3554 : if (!access_path)
3697 : {
3698 36 : if (any_dependent_bases_p (object_type))
3699 6 : goto dependent;
3700 30 : if (complain & tf_error)
3701 30 : error ("%qT is not a base of %qT", scope, object_type);
3702 30 : return error_mark_node;
3703 : }
3704 : }
3705 :
3706 100083610 : if (TREE_CODE (name) == BIT_NOT_EXPR)
3707 : {
3708 266727 : if (dependent_type_p (object_type))
3709 : /* The destructor isn't declared yet. */
3710 47885 : goto dependent;
3711 218842 : member = lookup_destructor (object, scope, name, complain);
3712 : }
3713 99816883 : else if (splice_p && valid_splice_for_member_access_p (name))
3714 : {
3715 2634 : member = name;
3716 1770 : name = OVL_FIRST (name);
3717 1770 : if (any_dependent_type_attributes_p (DECL_ATTRIBUTES (name)))
3718 : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3719 : wrong, so don't use it. */
3720 0 : goto dependent;
3721 1770 : if (is_overloaded_fn (name))
3722 1414 : member
3723 1414 : = build_baselink (access_path, TYPE_BINFO (object_type), member,
3724 1414 : IDENTIFIER_CONV_OP_P (DECL_NAME (name))
3725 0 : ? TREE_TYPE (DECL_NAME (name)) : NULL_TREE);
3726 : }
3727 : else
3728 : {
3729 : /* Look up the member. */
3730 99815113 : {
3731 99815113 : auto_diagnostic_group d;
3732 99815113 : access_failure_info afi;
3733 99815113 : if (processing_template_decl)
3734 : /* Even though this class member access expression is at this
3735 : point not dependent, the member itself may be dependent, and
3736 : we must not potentially push a access check for a dependent
3737 : member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3738 : ahead of time here; we're going to redo this member lookup at
3739 : instantiation time anyway. */
3740 76807384 : push_deferring_access_checks (dk_no_check);
3741 99815113 : member = lookup_member (access_path, name, /*protect=*/1,
3742 : /*want_type=*/false, complain,
3743 : &afi);
3744 99815113 : if (processing_template_decl)
3745 76807384 : pop_deferring_access_checks ();
3746 99815113 : afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3747 99815113 : }
3748 99815113 : if (member == NULL_TREE)
3749 : {
3750 9416901 : if (dependentish_scope_p (object_type))
3751 : /* Try again at instantiation time. */
3752 9393170 : goto dependent;
3753 23731 : if (complain & tf_error)
3754 330 : complain_about_unrecognized_member (access_path, name,
3755 : object_type);
3756 23731 : return error_mark_node;
3757 : }
3758 90398212 : if (member == error_mark_node)
3759 : return error_mark_node;
3760 90398166 : if (DECL_P (member)
3761 90398166 : && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3762 : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3763 : wrong, so don't use it. */
3764 6 : goto dependent;
3765 90398160 : if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3766 5358351 : goto dependent;
3767 : }
3768 :
3769 85260421 : if (is_template_id)
3770 : {
3771 463510 : tree templ = member;
3772 :
3773 463510 : if (BASELINK_P (templ))
3774 463256 : member = lookup_template_function (templ, template_args);
3775 254 : else if (variable_template_p (templ))
3776 254 : member = (lookup_and_finish_template_variable
3777 254 : (templ, template_args, complain));
3778 : else
3779 : {
3780 0 : if (complain & tf_error)
3781 0 : error ("%qD is not a member template function", name);
3782 0 : return error_mark_node;
3783 : }
3784 : }
3785 : }
3786 :
3787 96531284 : if (TREE_UNAVAILABLE (member))
3788 30 : error_unavailable_use (member, NULL_TREE);
3789 96531254 : else if (TREE_DEPRECATED (member))
3790 30 : warn_deprecated_use (member, NULL_TREE);
3791 :
3792 96531284 : if (template_p)
3793 9732 : check_template_keyword (member);
3794 :
3795 96531284 : expr = build_class_member_access_expr (object, member, access_path,
3796 : /*preserve_reference=*/false,
3797 : complain);
3798 96531284 : if (processing_template_decl && expr != error_mark_node)
3799 : {
3800 62344715 : if (BASELINK_P (member))
3801 : {
3802 46536691 : if (TREE_CODE (orig_name) == SCOPE_REF)
3803 175 : BASELINK_QUALIFIED_P (member) = 1;
3804 : orig_name = member;
3805 : }
3806 62344715 : expr = build_min_non_dep (COMPONENT_REF, expr,
3807 : orig_object, orig_name,
3808 : NULL_TREE);
3809 124689430 : COMPONENT_REF_SPLICE_P (STRIP_REFERENCE_REF (expr)) = splice_p;
3810 : }
3811 :
3812 : return expr;
3813 : }
3814 :
3815 : /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3816 : type. */
3817 :
3818 : tree
3819 174129 : build_simple_component_ref (tree object, tree member)
3820 : {
3821 174129 : tree type = cp_build_qualified_type (TREE_TYPE (member),
3822 174129 : cp_type_quals (TREE_TYPE (object)));
3823 174129 : return build3_loc (input_location,
3824 : COMPONENT_REF, type,
3825 174129 : object, member, NULL_TREE);
3826 : }
3827 :
3828 : /* Return an expression for the MEMBER_NAME field in the internal
3829 : representation of PTRMEM, a pointer-to-member function. (Each
3830 : pointer-to-member function type gets its own RECORD_TYPE so it is
3831 : more convenient to access the fields by name than by FIELD_DECL.)
3832 : This routine converts the NAME to a FIELD_DECL and then creates the
3833 : node for the complete expression. */
3834 :
3835 : tree
3836 174045 : build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3837 : {
3838 174045 : tree ptrmem_type;
3839 174045 : tree member;
3840 :
3841 174045 : if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3842 : {
3843 234 : for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3844 90 : if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3845 72 : return e.value;
3846 0 : gcc_unreachable ();
3847 : }
3848 :
3849 : /* This code is a stripped down version of
3850 : build_class_member_access_expr. It does not work to use that
3851 : routine directly because it expects the object to be of class
3852 : type. */
3853 173973 : ptrmem_type = TREE_TYPE (ptrmem);
3854 173973 : gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3855 260852 : for (member = TYPE_FIELDS (ptrmem_type); member;
3856 86879 : member = DECL_CHAIN (member))
3857 260852 : if (DECL_NAME (member) == member_name)
3858 : break;
3859 173973 : return build_simple_component_ref (ptrmem, member);
3860 : }
3861 :
3862 : /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3863 : and for any other relevant operator. */
3864 :
3865 : static tree
3866 143946989 : op_unqualified_lookup (tree_code code, bool is_assign)
3867 : {
3868 143946989 : tree lookups = NULL_TREE;
3869 :
3870 143946989 : if (cxx_dialect >= cxx20 && !is_assign)
3871 : {
3872 136064655 : if (code == NE_EXPR)
3873 : {
3874 : /* != can get rewritten in terms of ==. */
3875 8664877 : tree fnname = ovl_op_identifier (false, EQ_EXPR);
3876 8664877 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3877 8562335 : lookups = tree_cons (fnname, fns, lookups);
3878 : }
3879 127399778 : else if (code == GT_EXPR || code == LE_EXPR
3880 127399778 : || code == LT_EXPR || code == GE_EXPR)
3881 : {
3882 : /* These can get rewritten in terms of <=>. */
3883 11836254 : tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3884 11836254 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3885 11186470 : lookups = tree_cons (fnname, fns, lookups);
3886 : }
3887 : }
3888 :
3889 143946989 : tree fnname = ovl_op_identifier (is_assign, code);
3890 143946989 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3891 78055086 : lookups = tree_cons (fnname, fns, lookups);
3892 :
3893 143946989 : if (lookups)
3894 : return lookups;
3895 : else
3896 65659327 : return build_tree_list (NULL_TREE, NULL_TREE);
3897 : }
3898 :
3899 : /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3900 : the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3901 : name lookup for the given operator. */
3902 :
3903 : tree
3904 150361151 : build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3905 : {
3906 150361151 : if (lookups)
3907 : /* We're partially instantiating a dependent operator expression, and
3908 : LOOKUPS is the result of phase 1 name lookup that we performed
3909 : earlier at template definition time, so just reuse the corresponding
3910 : DEPENDENT_OPERATOR_TYPE. */
3911 6414162 : return TREE_TYPE (lookups);
3912 :
3913 : /* Otherwise we're processing a dependent operator expression at template
3914 : definition time, so perform phase 1 name lookup now. */
3915 143946989 : lookups = op_unqualified_lookup (code, is_assign);
3916 :
3917 143946989 : tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3918 143946989 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3919 143946989 : TREE_TYPE (lookups) = type;
3920 143946989 : return type;
3921 : }
3922 :
3923 : /* Given an expression PTR for a pointer, return an expression
3924 : for the value pointed to.
3925 : ERRORSTRING is the name of the operator to appear in error messages.
3926 :
3927 : This function may need to overload OPERATOR_FNNAME.
3928 : Must also handle REFERENCE_TYPEs for C++. */
3929 :
3930 : tree
3931 51955017 : build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3932 : tree lookups, tsubst_flags_t complain)
3933 : {
3934 51955017 : tree orig_expr = expr;
3935 51955017 : tree rval;
3936 51955017 : tree overload = NULL_TREE;
3937 :
3938 51955017 : if (processing_template_decl)
3939 : {
3940 : /* Retain the type if we know the operand is a pointer. */
3941 30155167 : if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3942 : {
3943 14808340 : if (expr == current_class_ptr
3944 14808340 : || (TREE_CODE (expr) == NOP_EXPR
3945 9828437 : && TREE_OPERAND (expr, 0) == current_class_ptr
3946 9714394 : && (same_type_ignoring_top_level_qualifiers_p
3947 9714394 : (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3948 9714393 : return current_class_ref;
3949 5093947 : return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3950 : }
3951 15346827 : if (type_dependent_expression_p (expr))
3952 : {
3953 15205497 : expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3954 15205497 : TREE_TYPE (expr)
3955 15205497 : = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3956 15205497 : return expr;
3957 : }
3958 : }
3959 :
3960 21941180 : rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3961 : NULL_TREE, NULL_TREE, lookups,
3962 : &overload, complain);
3963 21941180 : if (!rval)
3964 0 : rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3965 :
3966 21941180 : if (processing_template_decl && rval != error_mark_node)
3967 : {
3968 139497 : if (overload != NULL_TREE)
3969 139466 : return (build_min_non_dep_op_overload
3970 139466 : (INDIRECT_REF, rval, overload, orig_expr));
3971 :
3972 31 : return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3973 : }
3974 : else
3975 : return rval;
3976 : }
3977 :
3978 : /* Like c-family strict_aliasing_warning, but don't warn for dependent
3979 : types or expressions. */
3980 :
3981 : static bool
3982 649811 : cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3983 : {
3984 649811 : if (processing_template_decl)
3985 : {
3986 45476 : tree e = expr;
3987 45476 : STRIP_NOPS (e);
3988 45476 : if (dependent_type_p (type) || type_dependent_expression_p (e))
3989 7412 : return false;
3990 : }
3991 642399 : return strict_aliasing_warning (loc, type, expr);
3992 : }
3993 :
3994 : /* The implementation of the above, and of indirection implied by other
3995 : constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3996 :
3997 : static tree
3998 368409419 : cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3999 : tsubst_flags_t complain, bool do_fold)
4000 : {
4001 368409419 : tree pointer, type;
4002 :
4003 : /* RO_NULL should only be used with the folding entry points below, not
4004 : cp_build_indirect_ref. */
4005 368409419 : gcc_checking_assert (errorstring != RO_NULL || do_fold);
4006 :
4007 368409419 : if (ptr == current_class_ptr
4008 368409419 : || (TREE_CODE (ptr) == NOP_EXPR
4009 39657611 : && TREE_OPERAND (ptr, 0) == current_class_ptr
4010 21022623 : && (same_type_ignoring_top_level_qualifiers_p
4011 21022623 : (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
4012 22558615 : return current_class_ref;
4013 :
4014 345850804 : pointer = (TYPE_REF_P (TREE_TYPE (ptr))
4015 345850804 : ? ptr : decay_conversion (ptr, complain));
4016 345850804 : if (pointer == error_mark_node)
4017 : return error_mark_node;
4018 :
4019 345844945 : type = TREE_TYPE (pointer);
4020 :
4021 345844945 : if (INDIRECT_TYPE_P (type))
4022 : {
4023 : /* [expr.unary.op]
4024 :
4025 : If the type of the expression is "pointer to T," the type
4026 : of the result is "T." */
4027 345844353 : tree t = TREE_TYPE (type);
4028 :
4029 325871648 : if ((CONVERT_EXPR_P (ptr)
4030 238129184 : || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
4031 436022303 : && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
4032 : {
4033 : /* If a warning is issued, mark it to avoid duplicates from
4034 : the backend. This only needs to be done at
4035 : warn_strict_aliasing > 2. */
4036 86239881 : if (warn_strict_aliasing > 2
4037 86785795 : && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
4038 545914 : type, TREE_OPERAND (ptr, 0)))
4039 6 : suppress_warning (ptr, OPT_Wstrict_aliasing);
4040 : }
4041 :
4042 345844353 : if (VOID_TYPE_P (t))
4043 : {
4044 : /* A pointer to incomplete type (other than cv void) can be
4045 : dereferenced [expr.unary.op]/1 */
4046 48 : if (complain & tf_error)
4047 19 : error_at (loc, "%qT is not a pointer-to-object type", type);
4048 48 : return error_mark_node;
4049 : }
4050 334682337 : else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
4051 353395924 : && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
4052 : /* The POINTER was something like `&x'. We simplify `*&x' to
4053 : `x'. This can change the value category: '*&TARGET_EXPR'
4054 : is an lvalue and folding it into 'TARGET_EXPR' turns it into
4055 : a prvalue of class type. */
4056 7551541 : return TREE_OPERAND (pointer, 0);
4057 : else
4058 : {
4059 338292764 : tree ref = build1 (INDIRECT_REF, t, pointer);
4060 :
4061 : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
4062 : so that we get the proper error message if the result is used
4063 : to assign to. Also, &* is supposed to be a no-op. */
4064 338292764 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
4065 338292764 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
4066 338292764 : TREE_SIDE_EFFECTS (ref)
4067 338292764 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
4068 338292764 : return ref;
4069 : }
4070 : }
4071 592 : else if (!(complain & tf_error))
4072 : /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
4073 : ;
4074 : /* `pointer' won't be an error_mark_node if we were given a
4075 : pointer to member, so it's cool to check for this here. */
4076 41 : else if (TYPE_PTRMEM_P (type))
4077 15 : switch (errorstring)
4078 : {
4079 0 : case RO_ARRAY_INDEXING:
4080 0 : error_at (loc,
4081 : "invalid use of array indexing on pointer to member");
4082 0 : break;
4083 12 : case RO_UNARY_STAR:
4084 12 : error_at (loc, "invalid use of unary %<*%> on pointer to member");
4085 12 : break;
4086 0 : case RO_IMPLICIT_CONVERSION:
4087 0 : error_at (loc, "invalid use of implicit conversion on pointer "
4088 : "to member");
4089 0 : break;
4090 3 : case RO_ARROW_STAR:
4091 3 : error_at (loc, "left hand operand of %<->*%> must be a pointer to "
4092 : "class, but is a pointer to member of type %qT", type);
4093 3 : break;
4094 0 : default:
4095 0 : gcc_unreachable ();
4096 : }
4097 26 : else if (pointer != error_mark_node)
4098 26 : invalid_indirection_error (loc, type, errorstring);
4099 :
4100 592 : return error_mark_node;
4101 : }
4102 :
4103 : /* Entry point used by c-common, which expects folding. */
4104 :
4105 : tree
4106 70036 : build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
4107 : {
4108 70036 : return cp_build_indirect_ref_1 (loc, ptr, errorstring,
4109 70036 : tf_warning_or_error, true);
4110 : }
4111 :
4112 : /* Entry point used by internal indirection needs that don't correspond to any
4113 : syntactic construct. */
4114 :
4115 : tree
4116 338512286 : cp_build_fold_indirect_ref (tree pointer)
4117 : {
4118 338512286 : return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
4119 338512286 : tf_warning_or_error, true);
4120 : }
4121 :
4122 : /* Entry point used by indirection needs that correspond to some syntactic
4123 : construct. */
4124 :
4125 : tree
4126 29827097 : cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
4127 : tsubst_flags_t complain)
4128 : {
4129 29827097 : return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
4130 : }
4131 :
4132 : /* This handles expressions of the form "a[i]", which denotes
4133 : an array reference.
4134 :
4135 : This is logically equivalent in C to *(a+i), but we may do it differently.
4136 : If A is a variable or a member, we generate a primitive ARRAY_REF.
4137 : This avoids forcing the array out of registers, and can work on
4138 : arrays that are not lvalues (for example, members of structures returned
4139 : by functions).
4140 :
4141 : If INDEX is of some user-defined type, it must be converted to
4142 : integer type. Otherwise, to make a compatible PLUS_EXPR, it
4143 : will inherit the type of the array, which will be some pointer type.
4144 :
4145 : LOC is the location to use in building the array reference. */
4146 :
4147 : tree
4148 6264945 : cp_build_array_ref (location_t loc, tree array, tree idx,
4149 : tsubst_flags_t complain)
4150 : {
4151 6264945 : tree ret;
4152 :
4153 6264945 : if (idx == 0)
4154 : {
4155 0 : if (complain & tf_error)
4156 0 : error_at (loc, "subscript missing in array reference");
4157 0 : return error_mark_node;
4158 : }
4159 :
4160 6264945 : if (TREE_TYPE (array) == error_mark_node
4161 6264945 : || TREE_TYPE (idx) == error_mark_node)
4162 : return error_mark_node;
4163 :
4164 : /* 0[array] */
4165 6264945 : if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE
4166 6264945 : && TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
4167 : {
4168 10 : std::swap (array, idx);
4169 :
4170 10 : tree first = NULL_TREE;
4171 10 : if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array))
4172 9 : idx = first = save_expr (idx);
4173 10 : ret = cp_build_array_ref (loc, array, idx, complain);
4174 :
4175 10 : if (first)
4176 9 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4177 10 : return ret;
4178 : }
4179 :
4180 : /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
4181 : inside it. */
4182 6264935 : switch (TREE_CODE (array))
4183 : {
4184 28 : case COMPOUND_EXPR:
4185 28 : {
4186 28 : tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
4187 : complain);
4188 28 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
4189 28 : TREE_OPERAND (array, 0), value);
4190 28 : SET_EXPR_LOCATION (ret, loc);
4191 28 : return ret;
4192 : }
4193 :
4194 73 : case COND_EXPR:
4195 73 : tree op0, op1, op2;
4196 73 : op0 = TREE_OPERAND (array, 0);
4197 73 : op1 = TREE_OPERAND (array, 1);
4198 73 : op2 = TREE_OPERAND (array, 2);
4199 73 : if (TREE_SIDE_EFFECTS (idx) || !tree_invariant_p (idx))
4200 : {
4201 : /* If idx could possibly have some SAVE_EXPRs, turning
4202 : (op0 ? op1 : op2)[idx] into
4203 : op0 ? op1[idx] : op2[idx] can lead into temporaries
4204 : initialized in one conditional path and uninitialized
4205 : uses of them in the other path.
4206 : And if idx is a really large expression, evaluating it
4207 : twice is also not optimal.
4208 : On the other side, op0 must be sequenced before evaluation
4209 : of op1 and op2 and for C++17 op0, op1 and op2 must be
4210 : sequenced before idx.
4211 : If idx is INTEGER_CST, we can just do the optimization
4212 : without any SAVE_EXPRs, if op1 and op2 are both ARRAY_TYPE
4213 : VAR_DECLs or COMPONENT_REFs thereof (so their address
4214 : is constant or relative to frame), optimize into
4215 : (SAVE_EXPR <op0>, SAVE_EXPR <idx>, SAVE_EXPR <op0>)
4216 : ? op1[SAVE_EXPR <idx>] : op2[SAVE_EXPR <idx>]
4217 : Otherwise avoid this optimization. */
4218 58 : if (flag_strong_eval_order == 2)
4219 : {
4220 39 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4221 : {
4222 30 : if (!address_invariant_p (op1) || !address_invariant_p (op2))
4223 : {
4224 : /* Force default conversion on array if
4225 : we can't optimize this and array is ARRAY_TYPE
4226 : COND_EXPR, we can't leave COND_EXPRs with
4227 : ARRAY_TYPE in the IL. */
4228 8 : array = cp_default_conversion (array, complain);
4229 8 : if (error_operand_p (array))
4230 0 : return error_mark_node;
4231 : break;
4232 : }
4233 : }
4234 16 : else if (!POINTER_TYPE_P (TREE_TYPE (array))
4235 2 : || !tree_invariant_p (op1)
4236 11 : || !tree_invariant_p (op2))
4237 : break;
4238 : }
4239 43 : if (TREE_SIDE_EFFECTS (idx))
4240 : {
4241 18 : idx = save_expr (idx);
4242 18 : op0 = save_expr (op0);
4243 18 : warning_sentinel w (warn_unused_value);
4244 18 : tree tem = build_compound_expr (loc, op0, idx);
4245 18 : op0 = build_compound_expr (loc, tem, op0);
4246 18 : }
4247 : }
4248 58 : op1 = cp_build_array_ref (loc, op1, idx, complain);
4249 58 : op2 = cp_build_array_ref (loc, op2, idx, complain);
4250 58 : ret = build_conditional_expr (loc, op0, op1, op2, complain);
4251 58 : protected_set_expr_location (ret, loc);
4252 58 : return ret;
4253 :
4254 : default:
4255 : break;
4256 : }
4257 :
4258 6264849 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
4259 :
4260 6264849 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4261 : {
4262 1928272 : tree rval, type;
4263 :
4264 1928272 : warn_array_subscript_with_type_char (loc, idx);
4265 :
4266 1928272 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
4267 : {
4268 6 : if (complain & tf_error)
4269 6 : error_at (loc, "array subscript is not an integer");
4270 6 : return error_mark_node;
4271 : }
4272 :
4273 : /* Apply integral promotions *after* noticing character types.
4274 : (It is unclear why we do these promotions -- the standard
4275 : does not say that we should. In fact, the natural thing would
4276 : seem to be to convert IDX to ptrdiff_t; we're performing
4277 : pointer arithmetic.) */
4278 1928266 : idx = cp_perform_integral_promotions (idx, complain);
4279 :
4280 1928266 : idx = maybe_fold_non_dependent_expr (idx, complain);
4281 :
4282 : /* An array that is indexed by a non-constant
4283 : cannot be stored in a register; we must be able to do
4284 : address arithmetic on its address.
4285 : Likewise an array of elements of variable size. */
4286 1928266 : if (TREE_CODE (idx) != INTEGER_CST
4287 1928266 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4288 934036 : && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4289 : != INTEGER_CST)))
4290 : {
4291 995490 : if (!cxx_mark_addressable (array, true))
4292 0 : return error_mark_node;
4293 : }
4294 :
4295 : /* An array that is indexed by a constant value which is not within
4296 : the array bounds cannot be stored in a register either; because we
4297 : would get a crash in store_bit_field/extract_bit_field when trying
4298 : to access a non-existent part of the register. */
4299 1928266 : if (TREE_CODE (idx) == INTEGER_CST
4300 934039 : && TYPE_DOMAIN (TREE_TYPE (array))
4301 2860916 : && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4302 : {
4303 3124 : if (!cxx_mark_addressable (array))
4304 0 : return error_mark_node;
4305 : }
4306 :
4307 : /* Note in C++ it is valid to subscript a `register' array, since
4308 : it is valid to take the address of something with that
4309 : storage specification. */
4310 1928266 : if (extra_warnings)
4311 : {
4312 38844 : tree foo = array;
4313 59524 : while (TREE_CODE (foo) == COMPONENT_REF)
4314 20680 : foo = TREE_OPERAND (foo, 0);
4315 8 : if (VAR_P (foo) && DECL_REGISTER (foo)
4316 38844 : && (complain & tf_warning))
4317 0 : warning_at (loc, OPT_Wextra,
4318 : "subscripting array declared %<register%>");
4319 : }
4320 :
4321 1928266 : type = TREE_TYPE (TREE_TYPE (array));
4322 1928266 : rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4323 : /* Array ref is const/volatile if the array elements are
4324 : or if the array is.. */
4325 5784798 : TREE_READONLY (rval)
4326 1928266 : |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4327 5784798 : TREE_SIDE_EFFECTS (rval)
4328 1928266 : |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4329 3856532 : TREE_THIS_VOLATILE (rval)
4330 1928266 : |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4331 1928266 : ret = require_complete_type (rval, complain);
4332 1928266 : protected_set_expr_location (ret, loc);
4333 1928266 : if (non_lvalue)
4334 38599 : ret = non_lvalue_loc (loc, ret);
4335 1928266 : return ret;
4336 : }
4337 :
4338 4336577 : {
4339 4336577 : tree ar = cp_default_conversion (array, complain);
4340 4336577 : tree ind = cp_default_conversion (idx, complain);
4341 4336577 : tree first = NULL_TREE;
4342 :
4343 2804261 : if (!processing_template_decl && flag_strong_eval_order == 2
4344 7098793 : && TREE_SIDE_EFFECTS (ind))
4345 89113 : ar = first = save_expr (ar);
4346 :
4347 : /* Put the integer in IND to simplify error checking. */
4348 4336577 : if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4349 57 : std::swap (ar, ind);
4350 :
4351 4336577 : if (ar == error_mark_node || ind == error_mark_node)
4352 : return error_mark_node;
4353 :
4354 4336577 : if (!TYPE_PTR_P (TREE_TYPE (ar)))
4355 : {
4356 74 : if (complain & tf_error)
4357 3 : error_at (loc, "subscripted value is neither array nor pointer");
4358 74 : return error_mark_node;
4359 : }
4360 4336503 : if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4361 : {
4362 0 : if (complain & tf_error)
4363 0 : error_at (loc, "array subscript is not an integer");
4364 0 : return error_mark_node;
4365 : }
4366 :
4367 4336503 : warn_array_subscript_with_type_char (loc, idx);
4368 :
4369 4336503 : ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4370 4336503 : if (first)
4371 89113 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4372 4336503 : ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4373 4336503 : protected_set_expr_location (ret, loc);
4374 4336503 : if (non_lvalue)
4375 0 : ret = non_lvalue_loc (loc, ret);
4376 : return ret;
4377 : }
4378 : }
4379 :
4380 : /* Entry point for Obj-C++. */
4381 :
4382 : tree
4383 3787656 : build_array_ref (location_t loc, tree array, tree idx)
4384 : {
4385 3787656 : return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4386 : }
4387 :
4388 : /* Resolve a pointer to member function. INSTANCE is the object
4389 : instance to use, if the member points to a virtual member.
4390 :
4391 : This used to avoid checking for virtual functions if basetype
4392 : has no virtual functions, according to an earlier ANSI draft.
4393 : With the final ISO C++ rules, such an optimization is
4394 : incorrect: A pointer to a derived member can be static_cast
4395 : to pointer-to-base-member, as long as the dynamic object
4396 : later has the right member. So now we only do this optimization
4397 : when we know the dynamic type of the object. */
4398 :
4399 : tree
4400 86736 : get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4401 : tsubst_flags_t complain)
4402 : {
4403 86736 : if (TREE_CODE (function) == OFFSET_REF)
4404 0 : function = TREE_OPERAND (function, 1);
4405 :
4406 86736 : if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4407 : {
4408 86736 : tree idx, delta, e1, e2, e3, vtbl;
4409 86736 : bool nonvirtual;
4410 86736 : tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4411 86736 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4412 :
4413 86736 : tree instance_ptr = *instance_ptrptr;
4414 86736 : tree instance_save_expr = 0;
4415 86736 : if (instance_ptr == error_mark_node)
4416 : {
4417 0 : if (TREE_CODE (function) == PTRMEM_CST)
4418 : {
4419 : /* Extracting the function address from a pmf is only
4420 : allowed with -Wno-pmf-conversions. It only works for
4421 : pmf constants. */
4422 0 : e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4423 0 : e1 = convert (fntype, e1);
4424 0 : return e1;
4425 : }
4426 : else
4427 : {
4428 0 : if (complain & tf_error)
4429 0 : error ("object missing in use of %qE", function);
4430 0 : return error_mark_node;
4431 : }
4432 : }
4433 :
4434 : /* True if we know that the dynamic type of the object doesn't have
4435 : virtual functions, so we can assume the PFN field is a pointer. */
4436 86736 : nonvirtual = (COMPLETE_TYPE_P (basetype)
4437 86700 : && !TYPE_POLYMORPHIC_P (basetype)
4438 116620 : && resolves_to_fixed_type_p (instance_ptr, 0));
4439 :
4440 : /* If we don't really have an object (i.e. in an ill-formed
4441 : conversion from PMF to pointer), we can't resolve virtual
4442 : functions anyway. */
4443 86482 : if (!nonvirtual && is_dummy_object (instance_ptr))
4444 : nonvirtual = true;
4445 :
4446 : /* Use force_target_expr even when instance_ptr doesn't have
4447 : side-effects, unless it is a simple decl or constant, so
4448 : that we don't ubsan instrument the expression multiple times.
4449 : Don't use save_expr, as save_expr can avoid building a SAVE_EXPR
4450 : and building a SAVE_EXPR manually can be optimized away during
4451 : cp_fold. See PR116449 and PR117259. */
4452 86736 : if (TREE_SIDE_EFFECTS (instance_ptr)
4453 86736 : || (!nonvirtual
4454 48376 : && !DECL_P (instance_ptr)
4455 48376 : && !TREE_CONSTANT (instance_ptr)))
4456 86163 : instance_ptr = instance_save_expr
4457 86163 : = get_internal_target_expr (instance_ptr);
4458 :
4459 : /* See above comment. */
4460 86736 : if (TREE_SIDE_EFFECTS (function)
4461 86736 : || (!nonvirtual
4462 67250 : && !DECL_P (function)
4463 67238 : && !TREE_CONSTANT (function)))
4464 86282 : function = get_internal_target_expr (function);
4465 :
4466 : /* Start by extracting all the information from the PMF itself. */
4467 86736 : e3 = pfn_from_ptrmemfunc (function);
4468 86736 : delta = delta_from_ptrmemfunc (function);
4469 86736 : idx = build1 (NOP_EXPR, vtable_index_type, e3);
4470 86736 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4471 : {
4472 86736 : sanitize_code_type flag_sanitize_save;
4473 86736 : case ptrmemfunc_vbit_in_pfn:
4474 86736 : e1 = cp_build_binary_op (input_location,
4475 : BIT_AND_EXPR, idx, integer_one_node,
4476 : complain);
4477 86736 : idx = cp_build_binary_op (input_location,
4478 : MINUS_EXPR, idx, integer_one_node,
4479 : complain);
4480 86736 : if (idx == error_mark_node)
4481 : return error_mark_node;
4482 86736 : break;
4483 :
4484 : case ptrmemfunc_vbit_in_delta:
4485 : e1 = cp_build_binary_op (input_location,
4486 : BIT_AND_EXPR, delta, integer_one_node,
4487 : complain);
4488 : /* Don't instrument the RSHIFT_EXPR we're about to create because
4489 : we're going to use DELTA number of times, and that wouldn't play
4490 : well with SAVE_EXPRs therein. */
4491 : flag_sanitize_save = flag_sanitize;
4492 : flag_sanitize = 0;
4493 : delta = cp_build_binary_op (input_location,
4494 : RSHIFT_EXPR, delta, integer_one_node,
4495 : complain);
4496 : flag_sanitize = flag_sanitize_save;
4497 : if (delta == error_mark_node)
4498 : return error_mark_node;
4499 : break;
4500 :
4501 : default:
4502 : gcc_unreachable ();
4503 : }
4504 :
4505 86736 : if (e1 == error_mark_node)
4506 : return error_mark_node;
4507 :
4508 : /* Convert down to the right base before using the instance. A
4509 : special case is that in a pointer to member of class C, C may
4510 : be incomplete. In that case, the function will of course be
4511 : a member of C, and no conversion is required. In fact,
4512 : lookup_base will fail in that case, because incomplete
4513 : classes do not have BINFOs. */
4514 86736 : if (!same_type_ignoring_top_level_qualifiers_p
4515 86736 : (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4516 : {
4517 81 : basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4518 : basetype, ba_check, NULL, complain);
4519 81 : instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4520 : 1, complain);
4521 81 : if (instance_ptr == error_mark_node)
4522 : return error_mark_node;
4523 : }
4524 : /* ...and then the delta in the PMF. */
4525 86736 : instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4526 :
4527 : /* Hand back the adjusted 'this' argument to our caller. */
4528 86736 : *instance_ptrptr = instance_ptr;
4529 :
4530 86736 : if (nonvirtual)
4531 : /* Now just return the pointer. */
4532 : return e3;
4533 :
4534 : /* Next extract the vtable pointer from the object. */
4535 86464 : vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4536 : instance_ptr);
4537 86464 : vtbl = cp_build_fold_indirect_ref (vtbl);
4538 86464 : if (vtbl == error_mark_node)
4539 : return error_mark_node;
4540 :
4541 : /* Finally, extract the function pointer from the vtable. */
4542 86464 : e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4543 86464 : e2 = cp_build_fold_indirect_ref (e2);
4544 86464 : if (e2 == error_mark_node)
4545 : return error_mark_node;
4546 86464 : TREE_CONSTANT (e2) = 1;
4547 :
4548 : /* When using function descriptors, the address of the
4549 : vtable entry is treated as a function pointer. */
4550 86464 : if (TARGET_VTABLE_USES_DESCRIPTORS)
4551 : e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4552 : cp_build_addr_expr (e2, complain));
4553 :
4554 86464 : e2 = fold_convert (TREE_TYPE (e3), e2);
4555 86464 : e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4556 86464 : if (e1 == error_mark_node)
4557 : return error_mark_node;
4558 :
4559 : /* Make sure this doesn't get evaluated first inside one of the
4560 : branches of the COND_EXPR. */
4561 86464 : if (instance_save_expr)
4562 86115 : e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4563 : instance_save_expr, e1);
4564 :
4565 : function = e1;
4566 : }
4567 : return function;
4568 : }
4569 :
4570 : /* Used by the C-common bits. */
4571 : tree
4572 0 : build_function_call (location_t /*loc*/,
4573 : tree function, tree params)
4574 : {
4575 0 : return cp_build_function_call (function, params, tf_warning_or_error);
4576 : }
4577 :
4578 : /* Used by the C-common bits. */
4579 : tree
4580 341997 : build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4581 : tree function, vec<tree, va_gc> *params,
4582 : vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4583 : {
4584 341997 : vec<tree, va_gc> *orig_params = params;
4585 341997 : tree ret = cp_build_function_call_vec (function, ¶ms,
4586 : tf_warning_or_error, orig_function);
4587 :
4588 : /* cp_build_function_call_vec can reallocate PARAMS by adding
4589 : default arguments. That should never happen here. Verify
4590 : that. */
4591 341997 : gcc_assert (params == orig_params);
4592 :
4593 341997 : return ret;
4594 : }
4595 :
4596 : /* Build a function call using a tree list of arguments. */
4597 :
4598 : static tree
4599 0 : cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4600 : {
4601 0 : tree ret;
4602 :
4603 0 : releasing_vec vec;
4604 0 : for (; params != NULL_TREE; params = TREE_CHAIN (params))
4605 0 : vec_safe_push (vec, TREE_VALUE (params));
4606 0 : ret = cp_build_function_call_vec (function, &vec, complain);
4607 0 : return ret;
4608 0 : }
4609 :
4610 : /* Build a function call using varargs. */
4611 :
4612 : tree
4613 603927 : cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4614 : {
4615 603927 : va_list args;
4616 603927 : tree ret, t;
4617 :
4618 603927 : releasing_vec vec;
4619 603927 : va_start (args, complain);
4620 1508909 : for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4621 904982 : vec_safe_push (vec, t);
4622 603927 : va_end (args);
4623 603927 : ret = cp_build_function_call_vec (function, &vec, complain);
4624 603927 : return ret;
4625 603927 : }
4626 :
4627 : /* C++ implementation of callback for use when checking param types. */
4628 :
4629 : bool
4630 18 : cp_comp_parm_types (tree wanted_type, tree actual_type)
4631 : {
4632 18 : if (TREE_CODE (wanted_type) == POINTER_TYPE
4633 18 : && TREE_CODE (actual_type) == POINTER_TYPE)
4634 15 : return same_or_base_type_p (TREE_TYPE (wanted_type),
4635 : TREE_TYPE (actual_type));
4636 : return false;
4637 : }
4638 :
4639 : /* Build a function call using a vector of arguments.
4640 : If FUNCTION is the result of resolving an overloaded target built-in,
4641 : ORIG_FNDECL is the original function decl, otherwise it is null.
4642 : PARAMS may be NULL if there are no parameters. This changes the
4643 : contents of PARAMS. */
4644 :
4645 : tree
4646 7802472 : cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4647 : tsubst_flags_t complain, tree orig_fndecl)
4648 : {
4649 7802472 : tree fntype, fndecl;
4650 7802472 : int is_method;
4651 7802472 : tree original = function;
4652 7802472 : int nargs;
4653 7802472 : tree *argarray;
4654 7802472 : tree parm_types;
4655 7802472 : vec<tree, va_gc> *allocated = NULL;
4656 7802472 : tree ret;
4657 :
4658 : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4659 : expressions, like those used for ObjC messenger dispatches. */
4660 7802472 : if (params != NULL && !vec_safe_is_empty (*params))
4661 2225649 : function = objc_rewrite_function_call (function, (**params)[0]);
4662 :
4663 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4664 : Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4665 7802472 : if (TREE_CODE (function) == NOP_EXPR
4666 7802472 : && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4667 15 : function = TREE_OPERAND (function, 0);
4668 :
4669 7802472 : if (TREE_CODE (function) == FUNCTION_DECL)
4670 : {
4671 1675384 : if (!mark_used (function, complain))
4672 24 : return error_mark_node;
4673 1675360 : fndecl = function;
4674 :
4675 : /* For target_version semantics, the function set cannot be called
4676 : if there is no default version in scope. */
4677 1675360 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
4678 : && !is_function_default_version (fndecl))
4679 : {
4680 : if (complain & tf_error)
4681 : error ("no default version in scope");
4682 : return error_mark_node;
4683 : }
4684 :
4685 : /* Convert anything with function type to a pointer-to-function. */
4686 1675360 : if (DECL_MAIN_P (function))
4687 : {
4688 0 : if (complain & tf_error)
4689 0 : pedwarn (input_location, OPT_Wpedantic,
4690 : "ISO C++ forbids calling %<::main%> from within program");
4691 : else
4692 0 : return error_mark_node;
4693 : }
4694 1675360 : function = build_addr_func (function, complain);
4695 : }
4696 : else
4697 : {
4698 6127088 : fndecl = NULL_TREE;
4699 :
4700 6127088 : function = build_addr_func (function, complain);
4701 : }
4702 :
4703 7802448 : if (function == error_mark_node)
4704 : return error_mark_node;
4705 :
4706 7802346 : fntype = TREE_TYPE (function);
4707 :
4708 7802346 : if (TYPE_PTRMEMFUNC_P (fntype))
4709 : {
4710 15 : if (complain & tf_error)
4711 15 : error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4712 : "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4713 : original, original);
4714 15 : return error_mark_node;
4715 : }
4716 :
4717 15604662 : is_method = (TYPE_PTR_P (fntype)
4718 7802331 : && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4719 :
4720 7802331 : if (!(TYPE_PTRFN_P (fntype)
4721 88948 : || is_method
4722 2269 : || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4723 : {
4724 2269 : if (complain & tf_error)
4725 : {
4726 178 : if (is_stub_object (original))
4727 3 : error_at (input_location,
4728 : "%qT cannot be used as a function",
4729 3 : TREE_TYPE (original));
4730 175 : else if (!flag_diagnostics_show_caret)
4731 175 : error_at (input_location,
4732 : "%qE cannot be used as a function", original);
4733 0 : else if (DECL_P (original))
4734 0 : error_at (input_location,
4735 : "%qD cannot be used as a function", original);
4736 : else
4737 0 : error_at (input_location,
4738 : "expression cannot be used as a function");
4739 : }
4740 :
4741 2269 : return error_mark_node;
4742 : }
4743 :
4744 : /* fntype now gets the type of function pointed to. */
4745 7800062 : fntype = TREE_TYPE (fntype);
4746 7800062 : parm_types = TYPE_ARG_TYPES (fntype);
4747 :
4748 7800062 : if (params == NULL)
4749 : {
4750 158147 : allocated = make_tree_vector ();
4751 158147 : params = &allocated;
4752 : }
4753 :
4754 7800062 : nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4755 : complain);
4756 7800062 : if (nargs < 0)
4757 1964 : return error_mark_node;
4758 :
4759 7798098 : argarray = (*params)->address ();
4760 :
4761 : /* Check for errors in format strings and inappropriately
4762 : null parameters. */
4763 7798098 : bool warned_p
4764 7798098 : = ((complain & tf_warning)
4765 7798098 : && check_function_arguments (input_location, fndecl, fntype,
4766 : nargs, argarray, NULL,
4767 7798098 : cp_comp_parm_types));
4768 :
4769 7798098 : ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4770 :
4771 7798098 : if (warned_p)
4772 : {
4773 27 : tree c = extract_call_expr (ret);
4774 27 : if (TREE_CODE (c) == CALL_EXPR)
4775 27 : suppress_warning (c, OPT_Wnonnull);
4776 : }
4777 :
4778 7798098 : if (allocated != NULL)
4779 158147 : release_tree_vector (allocated);
4780 :
4781 : return ret;
4782 : }
4783 :
4784 : /* Subroutine of convert_arguments.
4785 : Print an error message about a wrong number of arguments. */
4786 :
4787 : static void
4788 162 : error_args_num (location_t loc, tree fndecl, bool too_many_p)
4789 : {
4790 162 : if (fndecl)
4791 : {
4792 126 : auto_diagnostic_group d;
4793 126 : if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4794 : {
4795 0 : if (DECL_NAME (fndecl) == NULL_TREE
4796 0 : || (DECL_NAME (fndecl)
4797 0 : == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4798 0 : error_at (loc,
4799 : too_many_p
4800 : ? G_("too many arguments to constructor %q#D")
4801 : : G_("too few arguments to constructor %q#D"),
4802 : fndecl);
4803 : else
4804 0 : error_at (loc,
4805 : too_many_p
4806 : ? G_("too many arguments to member function %q#D")
4807 : : G_("too few arguments to member function %q#D"),
4808 : fndecl);
4809 : }
4810 : else
4811 189 : error_at (loc,
4812 : too_many_p
4813 : ? G_("too many arguments to function %q#D")
4814 : : G_("too few arguments to function %q#D"),
4815 : fndecl);
4816 126 : if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4817 87 : inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4818 126 : }
4819 : else
4820 : {
4821 36 : if (c_dialect_objc () && objc_message_selector ())
4822 0 : error_at (loc,
4823 : too_many_p
4824 : ? G_("too many arguments to method %q#D")
4825 : : G_("too few arguments to method %q#D"),
4826 : objc_message_selector ());
4827 : else
4828 57 : error_at (loc, too_many_p ? G_("too many arguments to function")
4829 : : G_("too few arguments to function"));
4830 : }
4831 162 : }
4832 :
4833 : /* Convert the actual parameter expressions in the list VALUES to the
4834 : types in the list TYPELIST. The converted expressions are stored
4835 : back in the VALUES vector.
4836 : If parmdecls is exhausted, or when an element has NULL as its type,
4837 : perform the default conversions.
4838 :
4839 : NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4840 :
4841 : This is also where warnings about wrong number of args are generated.
4842 :
4843 : Returns the actual number of arguments processed (which might be less
4844 : than the length of the vector), or -1 on error.
4845 :
4846 : In C++, unspecified trailing parameters can be filled in with their
4847 : default arguments, if such were specified. Do so here. */
4848 :
4849 : static int
4850 7800062 : convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4851 : int flags, tsubst_flags_t complain)
4852 : {
4853 7800062 : tree typetail;
4854 7800062 : unsigned int i;
4855 :
4856 : /* Argument passing is always copy-initialization. */
4857 7800062 : flags |= LOOKUP_ONLYCONVERTING;
4858 :
4859 12637425 : for (i = 0, typetail = typelist;
4860 12637425 : i < vec_safe_length (*values);
4861 : i++)
4862 : {
4863 9677868 : tree type = typetail ? TREE_VALUE (typetail) : 0;
4864 4839157 : tree val = (**values)[i];
4865 :
4866 4839157 : if (val == error_mark_node || type == error_mark_node)
4867 : return -1;
4868 :
4869 4839151 : if (type == void_type_node)
4870 : {
4871 212 : if (complain & tf_error)
4872 78 : error_args_num (input_location, fndecl, /*too_many_p=*/true);
4873 212 : return -1;
4874 : }
4875 :
4876 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4877 : Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4878 4838939 : if (TREE_CODE (val) == NOP_EXPR
4879 1263360 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4880 4838957 : && (type == 0 || !TYPE_REF_P (type)))
4881 18 : val = TREE_OPERAND (val, 0);
4882 :
4883 4838939 : if (type == 0 || !TYPE_REF_P (type))
4884 : {
4885 4633088 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4886 4633088 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4887 11461 : val = decay_conversion (val, complain);
4888 : }
4889 :
4890 4838939 : if (val == error_mark_node)
4891 : return -1;
4892 :
4893 4838939 : if (type != 0)
4894 : {
4895 : /* Formal parm type is specified by a function prototype. */
4896 4838493 : tree parmval;
4897 :
4898 4838493 : if (!COMPLETE_TYPE_P (complete_type (type)))
4899 : {
4900 18 : if (complain & tf_error)
4901 : {
4902 12 : location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4903 12 : if (fndecl)
4904 : {
4905 12 : auto_diagnostic_group d;
4906 12 : error_at (loc,
4907 : "parameter %P of %qD has incomplete type %qT",
4908 : i, fndecl, type);
4909 12 : inform (get_fndecl_argument_location (fndecl, i),
4910 : " declared here");
4911 12 : }
4912 : else
4913 0 : error_at (loc, "parameter %P has incomplete type %qT", i,
4914 : type);
4915 : }
4916 18 : parmval = error_mark_node;
4917 : }
4918 : else
4919 : {
4920 4838475 : parmval = convert_for_initialization
4921 4838475 : (NULL_TREE, type, val, flags,
4922 : ICR_ARGPASS, fndecl, i, complain);
4923 4838475 : parmval = convert_for_arg_passing (type, parmval, complain);
4924 : }
4925 :
4926 4838493 : if (parmval == error_mark_node)
4927 : return -1;
4928 :
4929 4836917 : (**values)[i] = parmval;
4930 : }
4931 : else
4932 : {
4933 446 : int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4934 21 : if (magic)
4935 : {
4936 : /* Don't truncate excess precision to the semantic type. */
4937 0 : if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4938 0 : val = TREE_OPERAND (val, 0);
4939 : /* Don't do ellipsis conversion for __built_in_constant_p
4940 : as this will result in spurious errors for non-trivial
4941 : types. */
4942 0 : val = require_complete_type (val, complain);
4943 : }
4944 : else
4945 446 : val = convert_arg_to_ellipsis (val, complain);
4946 :
4947 446 : (**values)[i] = val;
4948 : }
4949 :
4950 4837363 : if (typetail)
4951 4836917 : typetail = TREE_CHAIN (typetail);
4952 : }
4953 :
4954 7798268 : if (typetail != 0 && typetail != void_list_node)
4955 : {
4956 : /* See if there are default arguments that can be used. Because
4957 : we hold default arguments in the FUNCTION_TYPE (which is so
4958 : wrong), we can see default parameters here from deduced
4959 : contexts (and via typeof) for indirect function calls.
4960 : Fortunately we know whether we have a function decl to
4961 : provide default arguments in a language conformant
4962 : manner. */
4963 63 : if (fndecl && TREE_PURPOSE (typetail)
4964 173 : && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4965 : {
4966 6 : for (; typetail != void_list_node; ++i)
4967 : {
4968 : /* After DR777, with explicit template args we can end up with a
4969 : default argument followed by no default argument. */
4970 6 : if (!TREE_PURPOSE (typetail))
4971 : break;
4972 3 : tree parmval
4973 3 : = convert_default_arg (TREE_VALUE (typetail),
4974 3 : TREE_PURPOSE (typetail),
4975 3 : fndecl, i, complain);
4976 :
4977 3 : if (parmval == error_mark_node)
4978 0 : return -1;
4979 :
4980 3 : vec_safe_push (*values, parmval);
4981 3 : typetail = TREE_CHAIN (typetail);
4982 : /* ends with `...'. */
4983 3 : if (typetail == NULL_TREE)
4984 : break;
4985 : }
4986 : }
4987 :
4988 170 : if (typetail && typetail != void_list_node)
4989 : {
4990 170 : if (complain & tf_error)
4991 84 : error_args_num (input_location, fndecl, /*too_many_p=*/false);
4992 170 : return -1;
4993 : }
4994 : }
4995 :
4996 7798098 : return (int) i;
4997 : }
4998 :
4999 : /* Build a binary-operation expression, after performing default
5000 : conversions on the operands. CODE is the kind of expression to
5001 : build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
5002 : are the tree codes which correspond to ARG1 and ARG2 when issuing
5003 : warnings about possibly misplaced parentheses. They may differ
5004 : from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
5005 : folding (e.g., if the parser sees "a | 1 + 1", it may call this
5006 : routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
5007 : To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
5008 : ARG2_CODE as ERROR_MARK. */
5009 :
5010 : tree
5011 238179821 : build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
5012 : enum tree_code arg1_code, tree arg2,
5013 : enum tree_code arg2_code, tree lookups,
5014 : tree *overload_p, tsubst_flags_t complain)
5015 : {
5016 238179821 : tree orig_arg1;
5017 238179821 : tree orig_arg2;
5018 238179821 : tree expr;
5019 238179821 : tree overload = NULL_TREE;
5020 :
5021 238179821 : orig_arg1 = arg1;
5022 238179821 : orig_arg2 = arg2;
5023 :
5024 238179821 : if (processing_template_decl)
5025 : {
5026 139638705 : if (type_dependent_expression_p (arg1)
5027 139638705 : || type_dependent_expression_p (arg2))
5028 : {
5029 95785780 : expr = build_min_nt_loc (loc, code, arg1, arg2);
5030 95785780 : TREE_TYPE (expr)
5031 95785780 : = build_dependent_operator_type (lookups, code, false);
5032 95785780 : return expr;
5033 : }
5034 : }
5035 :
5036 142394041 : if (code == DOTSTAR_EXPR)
5037 86944 : expr = build_m_component_ref (arg1, arg2, complain);
5038 : else
5039 142307097 : expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
5040 : lookups, &overload, complain);
5041 :
5042 142394008 : if (overload_p != NULL)
5043 63429862 : *overload_p = overload;
5044 :
5045 : /* Check for cases such as x+y<<z which users are likely to
5046 : misinterpret. But don't warn about obj << x + y, since that is a
5047 : common idiom for I/O. */
5048 142394008 : if (warn_parentheses
5049 1247951 : && (complain & tf_warning)
5050 1191307 : && !processing_template_decl
5051 816768 : && !error_operand_p (arg1)
5052 816762 : && !error_operand_p (arg2)
5053 143210758 : && (code != LSHIFT_EXPR
5054 71167 : || !CLASS_TYPE_P (TREE_TYPE (arg1))))
5055 815703 : warn_about_parentheses (loc, code, arg1_code, orig_arg1,
5056 : arg2_code, orig_arg2);
5057 :
5058 142394008 : if (processing_template_decl && expr != error_mark_node)
5059 : {
5060 43852694 : if (overload != NULL_TREE)
5061 3955042 : return (build_min_non_dep_op_overload
5062 3955042 : (code, expr, overload, orig_arg1, orig_arg2));
5063 :
5064 39897652 : return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
5065 : }
5066 :
5067 : return expr;
5068 : }
5069 :
5070 : /* Build and return an ARRAY_REF expression. */
5071 :
5072 : tree
5073 2630226 : build_x_array_ref (location_t loc, tree arg1, tree arg2,
5074 : tsubst_flags_t complain)
5075 : {
5076 2630226 : tree orig_arg1 = arg1;
5077 2630226 : tree orig_arg2 = arg2;
5078 2630226 : tree expr;
5079 2630226 : tree overload = NULL_TREE;
5080 :
5081 2630226 : if (processing_template_decl)
5082 : {
5083 9506 : if (type_dependent_expression_p (arg1)
5084 9506 : || type_dependent_expression_p (arg2))
5085 4558 : return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
5086 4558 : NULL_TREE, NULL_TREE);
5087 : }
5088 :
5089 2625668 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
5090 : NULL_TREE, NULL_TREE, &overload, complain);
5091 :
5092 2625668 : if (processing_template_decl && expr != error_mark_node)
5093 : {
5094 4945 : if (overload != NULL_TREE)
5095 4936 : return (build_min_non_dep_op_overload
5096 4936 : (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
5097 :
5098 9 : return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
5099 9 : NULL_TREE, NULL_TREE);
5100 : }
5101 : return expr;
5102 : }
5103 :
5104 : /* Build an OpenMP array section reference, creating an exact type for the
5105 : resulting expression based on the element type and bounds if possible. If
5106 : we have variable bounds, create an incomplete array type for the result
5107 : instead. */
5108 :
5109 : tree
5110 10506 : build_omp_array_section (location_t loc, tree array_expr, tree index,
5111 : tree length)
5112 : {
5113 10506 : if (TREE_CODE (array_expr) == TYPE_DECL
5114 10506 : || type_dependent_expression_p (array_expr))
5115 258 : return build3_loc (loc, OMP_ARRAY_SECTION, NULL_TREE, array_expr, index,
5116 258 : length);
5117 :
5118 10248 : tree type = TREE_TYPE (array_expr);
5119 10248 : gcc_assert (type);
5120 10248 : type = non_reference (type);
5121 :
5122 10248 : tree sectype, eltype = TREE_TYPE (type);
5123 :
5124 : /* It's not an array or pointer type. Just reuse the type of the
5125 : original expression as the type of the array section (an error will be
5126 : raised anyway, later). */
5127 10248 : if (eltype == NULL_TREE)
5128 39 : sectype = TREE_TYPE (array_expr);
5129 : else
5130 : {
5131 10209 : tree idxtype = NULL_TREE;
5132 :
5133 : /* If we know the integer bounds, create an index type with exact
5134 : low/high (or zero/length) bounds. Otherwise, create an incomplete
5135 : array type. (This mostly only affects diagnostics.) */
5136 10209 : if (index != NULL_TREE
5137 10209 : && length != NULL_TREE
5138 7409 : && TREE_CODE (index) == INTEGER_CST
5139 6612 : && TREE_CODE (length) == INTEGER_CST)
5140 : {
5141 5111 : tree low = fold_convert (sizetype, index);
5142 5111 : tree high = fold_convert (sizetype, length);
5143 5111 : high = size_binop (PLUS_EXPR, low, high);
5144 5111 : high = size_binop (MINUS_EXPR, high, size_one_node);
5145 5111 : idxtype = build_range_type (sizetype, low, high);
5146 5111 : }
5147 2906 : else if ((index == NULL_TREE || integer_zerop (index))
5148 3509 : && length != NULL_TREE
5149 7199 : && TREE_CODE (length) == INTEGER_CST)
5150 1387 : idxtype = build_index_type (length);
5151 :
5152 10209 : sectype = build_array_type (eltype, idxtype);
5153 : }
5154 :
5155 10248 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array_expr, index,
5156 10248 : length);
5157 : }
5158 :
5159 : /* Return whether OP is an expression of enum type cast to integer
5160 : type. In C++ even unsigned enum types are cast to signed integer
5161 : types. We do not want to issue warnings about comparisons between
5162 : signed and unsigned types when one of the types is an enum type.
5163 : Those warnings are always false positives in practice. */
5164 :
5165 : static bool
5166 639464 : enum_cast_to_int (tree op)
5167 : {
5168 626490 : if (CONVERT_EXPR_P (op)
5169 14001 : && TREE_TYPE (op) == integer_type_node
5170 917 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
5171 639479 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
5172 : return true;
5173 :
5174 : /* The cast may have been pushed into a COND_EXPR. */
5175 639457 : if (TREE_CODE (op) == COND_EXPR)
5176 765 : return (enum_cast_to_int (TREE_OPERAND (op, 1))
5177 765 : || enum_cast_to_int (TREE_OPERAND (op, 2)));
5178 :
5179 : return false;
5180 : }
5181 :
5182 : /* For the c-common bits. */
5183 : tree
5184 4986739 : build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
5185 : bool /*convert_p*/)
5186 : {
5187 4986739 : return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
5188 : }
5189 :
5190 : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
5191 : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
5192 :
5193 : static tree
5194 8218 : build_vec_cmp (tree_code code, tree type,
5195 : tree arg0, tree arg1)
5196 : {
5197 8218 : tree zero_vec = build_zero_cst (type);
5198 8218 : tree minus_one_vec = build_minus_one_cst (type);
5199 8218 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
5200 8218 : tree cmp = build2 (code, cmp_type, arg0, arg1);
5201 8218 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
5202 : }
5203 :
5204 : /* Possibly warn about an address never being NULL. */
5205 :
5206 : static void
5207 1846780 : warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
5208 : {
5209 : /* Prevent warnings issued for macro expansion. */
5210 1846780 : if (!warn_address
5211 41707 : || (complain & tf_warning) == 0
5212 24936 : || c_inhibit_evaluation_warnings != 0
5213 24920 : || from_macro_expansion_at (location)
5214 1864969 : || warning_suppressed_p (op, OPT_Waddress))
5215 1828914 : return;
5216 :
5217 18156 : tree cop = fold_for_warn (op);
5218 :
5219 18156 : if (TREE_CODE (cop) == NON_LVALUE_EXPR)
5220 : /* Unwrap the expression for C++ 98. */
5221 1 : cop = TREE_OPERAND (cop, 0);
5222 :
5223 18156 : if (TREE_CODE (cop) == PTRMEM_CST)
5224 : {
5225 : /* The address of a nonstatic data member is never null. */
5226 30 : warning_at (location, OPT_Waddress,
5227 : "the address %qE will never be NULL",
5228 : cop);
5229 30 : return;
5230 : }
5231 :
5232 18126 : if (TREE_CODE (cop) == NOP_EXPR)
5233 : {
5234 : /* Allow casts to intptr_t to suppress the warning. */
5235 1243 : tree type = TREE_TYPE (cop);
5236 1243 : if (TREE_CODE (type) == INTEGER_TYPE)
5237 : return;
5238 :
5239 1243 : STRIP_NOPS (cop);
5240 : }
5241 :
5242 18126 : auto_diagnostic_group d;
5243 18126 : bool warned = false;
5244 18126 : if (TREE_CODE (cop) == ADDR_EXPR)
5245 : {
5246 789 : cop = TREE_OPERAND (cop, 0);
5247 :
5248 : /* Set to true in the loop below if OP dereferences its operand.
5249 : In such a case the ultimate target need not be a decl for
5250 : the null [in]equality test to be necessarily constant. */
5251 789 : bool deref = false;
5252 :
5253 : /* Get the outermost array or object, or member. */
5254 960 : while (handled_component_p (cop))
5255 : {
5256 270 : if (TREE_CODE (cop) == COMPONENT_REF)
5257 : {
5258 : /* Get the member (its address is never null). */
5259 99 : cop = TREE_OPERAND (cop, 1);
5260 99 : break;
5261 : }
5262 :
5263 : /* Get the outer array/object to refer to in the warning. */
5264 171 : cop = TREE_OPERAND (cop, 0);
5265 171 : deref = true;
5266 : }
5267 :
5268 639 : if ((!deref && !decl_with_nonnull_addr_p (cop))
5269 631 : || from_macro_expansion_at (location)
5270 1420 : || warning_suppressed_p (cop, OPT_Waddress))
5271 158 : return;
5272 :
5273 631 : warned = warning_at (location, OPT_Waddress,
5274 : "the address of %qD will never be NULL", cop);
5275 631 : op = cop;
5276 : }
5277 17337 : else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
5278 : {
5279 : /* Adding zero to the null pointer is well-defined in C++. When
5280 : the offset is unknown (i.e., not a constant) warn anyway since
5281 : it's less likely that the pointer operand is null than not. */
5282 102 : tree off = TREE_OPERAND (cop, 1);
5283 102 : if (!integer_zerop (off)
5284 102 : && !warning_suppressed_p (cop, OPT_Waddress))
5285 : {
5286 102 : tree base = TREE_OPERAND (cop, 0);
5287 102 : STRIP_NOPS (base);
5288 102 : if (TYPE_REF_P (TREE_TYPE (base)))
5289 12 : warning_at (location, OPT_Waddress, "the compiler can assume that "
5290 : "the address of %qE will never be NULL", base);
5291 : else
5292 90 : warning_at (location, OPT_Waddress, "comparing the result of "
5293 : "pointer addition %qE and NULL", cop);
5294 : }
5295 102 : return;
5296 : }
5297 16273 : else if (CONVERT_EXPR_P (op)
5298 17297 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
5299 : {
5300 62 : STRIP_NOPS (op);
5301 :
5302 62 : if (TREE_CODE (op) == COMPONENT_REF)
5303 24 : op = TREE_OPERAND (op, 1);
5304 :
5305 62 : if (DECL_P (op))
5306 62 : warned = warning_at (location, OPT_Waddress,
5307 : "the compiler can assume that the address of "
5308 : "%qD will never be NULL", op);
5309 : }
5310 :
5311 693 : if (warned && DECL_P (op))
5312 657 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
5313 18126 : }
5314 :
5315 : /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5316 : the other operand is of a different enumeration type or a floating-point
5317 : type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5318 : code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5319 : and LOC is the location for the whole binary expression.
5320 : For C++26 this is ill-formed rather than deprecated.
5321 : Return true for SFINAE errors.
5322 : TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5323 :
5324 : static bool
5325 112785494 : do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5326 : tree type1, tsubst_flags_t complain)
5327 : {
5328 112785494 : if (TREE_CODE (type0) == ENUMERAL_TYPE
5329 3336724 : && TREE_CODE (type1) == ENUMERAL_TYPE
5330 115584864 : && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
5331 : {
5332 200 : if (cxx_dialect >= cxx26)
5333 : {
5334 65 : if ((complain & tf_warning_or_error) == 0)
5335 : return true;
5336 : }
5337 135 : else if ((complain & tf_warning) == 0)
5338 : return false;
5339 : /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5340 : Otherwise, warn if -Wenum-conversion is on. */
5341 193 : enum opt_code opt;
5342 193 : if (warn_deprecated_enum_enum_conv)
5343 : opt = OPT_Wdeprecated_enum_enum_conversion;
5344 93 : else if (warn_enum_conversion)
5345 : opt = OPT_Wenum_conversion;
5346 : else
5347 : return false;
5348 :
5349 122 : switch (code)
5350 : {
5351 : case GT_EXPR:
5352 : case LT_EXPR:
5353 : case GE_EXPR:
5354 : case LE_EXPR:
5355 : case EQ_EXPR:
5356 : case NE_EXPR:
5357 : /* Comparisons are handled by -Wenum-compare. */
5358 : return false;
5359 : case SPACESHIP_EXPR:
5360 : /* This is invalid, don't warn. */
5361 : return false;
5362 18 : case BIT_AND_EXPR:
5363 18 : case BIT_IOR_EXPR:
5364 18 : case BIT_XOR_EXPR:
5365 18 : if (cxx_dialect >= cxx26)
5366 6 : pedwarn (loc, opt, "bitwise operation between different "
5367 : "enumeration types %qT and %qT", type0, type1);
5368 : else
5369 12 : warning_at (loc, opt, "bitwise operation between different "
5370 : "enumeration types %qT and %qT is deprecated",
5371 : type0, type1);
5372 18 : return false;
5373 42 : default:
5374 42 : if (cxx_dialect >= cxx26)
5375 13 : pedwarn (loc, opt, "arithmetic between different enumeration "
5376 : "types %qT and %qT", type0, type1);
5377 : else
5378 29 : warning_at (loc, opt, "arithmetic between different enumeration "
5379 : "types %qT and %qT is deprecated", type0, type1);
5380 42 : return false;
5381 : }
5382 : }
5383 112785294 : else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5384 3336524 : && SCALAR_FLOAT_TYPE_P (type1))
5385 112785222 : || (SCALAR_FLOAT_TYPE_P (type0)
5386 31820989 : && TREE_CODE (type1) == ENUMERAL_TYPE))
5387 : {
5388 151 : if (cxx_dialect >= cxx26)
5389 : {
5390 54 : if ((complain & tf_warning_or_error) == 0)
5391 : return true;
5392 : }
5393 97 : else if ((complain & tf_warning) == 0)
5394 : return false;
5395 139 : const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5396 : /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5397 : Otherwise, warn if -Wenum-conversion is on. */
5398 139 : enum opt_code opt;
5399 139 : if (warn_deprecated_enum_float_conv)
5400 : opt = OPT_Wdeprecated_enum_float_conversion;
5401 68 : else if (warn_enum_conversion)
5402 : opt = OPT_Wenum_conversion;
5403 : else
5404 : return false;
5405 :
5406 88 : switch (code)
5407 : {
5408 36 : case GT_EXPR:
5409 36 : case LT_EXPR:
5410 36 : case GE_EXPR:
5411 36 : case LE_EXPR:
5412 36 : case EQ_EXPR:
5413 36 : case NE_EXPR:
5414 36 : if (enum_first_p && cxx_dialect >= cxx26)
5415 6 : pedwarn (loc, opt, "comparison of enumeration type %qT with "
5416 : "floating-point type %qT", type0, type1);
5417 30 : else if (cxx_dialect >= cxx26)
5418 6 : pedwarn (loc, opt, "comparison of floating-point type %qT "
5419 : "with enumeration type %qT", type0, type1);
5420 24 : else if (enum_first_p)
5421 12 : warning_at (loc, opt, "comparison of enumeration type %qT with "
5422 : "floating-point type %qT is deprecated",
5423 : type0, type1);
5424 : else
5425 12 : warning_at (loc, opt, "comparison of floating-point type %qT "
5426 : "with enumeration type %qT is deprecated",
5427 : type0, type1);
5428 36 : return false;
5429 : case SPACESHIP_EXPR:
5430 : /* This is invalid, don't warn. */
5431 : return false;
5432 40 : default:
5433 40 : if (enum_first_p && cxx_dialect >= cxx26)
5434 6 : pedwarn (loc, opt, "arithmetic between enumeration type %qT "
5435 : "and floating-point type %qT", type0, type1);
5436 34 : else if (cxx_dialect >= cxx26)
5437 7 : pedwarn (loc, opt, "arithmetic between floating-point type %qT "
5438 : "and enumeration type %qT", type0, type1);
5439 27 : else if (enum_first_p)
5440 12 : warning_at (loc, opt, "arithmetic between enumeration type %qT "
5441 : "and floating-point type %qT is deprecated",
5442 : type0, type1);
5443 : else
5444 15 : warning_at (loc, opt, "arithmetic between floating-point type %qT "
5445 : "and enumeration type %qT is deprecated",
5446 : type0, type1);
5447 40 : return false;
5448 : }
5449 : }
5450 : return false;
5451 : }
5452 :
5453 : /* Build a binary-operation expression without default conversions.
5454 : CODE is the kind of expression to build.
5455 : LOCATION is the location_t of the operator in the source code.
5456 : This function differs from `build' in several ways:
5457 : the data type of the result is computed and recorded in it,
5458 : warnings are generated if arg data types are invalid,
5459 : special handling for addition and subtraction of pointers is known,
5460 : and some optimization is done (operations on narrow ints
5461 : are done in the narrower type when that gives the same result).
5462 : Constant folding is also done before the result is returned.
5463 :
5464 : Note that the operands will never have enumeral types
5465 : because either they have just had the default conversions performed
5466 : or they have both just been converted to some other type in which
5467 : the arithmetic is to be done.
5468 :
5469 : C++: must do special pointer arithmetic when implementing
5470 : multiple inheritance, and deal with pointer to member functions. */
5471 :
5472 : tree
5473 152112652 : cp_build_binary_op (const op_location_t &location,
5474 : enum tree_code code, tree orig_op0, tree orig_op1,
5475 : tsubst_flags_t complain)
5476 : {
5477 152112652 : tree op0, op1;
5478 152112652 : enum tree_code code0, code1;
5479 152112652 : tree type0, type1, orig_type0, orig_type1;
5480 152112652 : const char *invalid_op_diag;
5481 :
5482 : /* Expression code to give to the expression when it is built.
5483 : Normally this is CODE, which is what the caller asked for,
5484 : but in some special cases we change it. */
5485 152112652 : enum tree_code resultcode = code;
5486 :
5487 : /* Data type in which the computation is to be performed.
5488 : In the simplest cases this is the common type of the arguments. */
5489 152112652 : tree result_type = NULL_TREE;
5490 :
5491 : /* When the computation is in excess precision, the type of the
5492 : final EXCESS_PRECISION_EXPR. */
5493 152112652 : tree semantic_result_type = NULL;
5494 :
5495 : /* Nonzero means operands have already been type-converted
5496 : in whatever way is necessary.
5497 : Zero means they need to be converted to RESULT_TYPE. */
5498 152112652 : int converted = 0;
5499 :
5500 : /* Nonzero means create the expression with this type, rather than
5501 : RESULT_TYPE. */
5502 152112652 : tree build_type = 0;
5503 :
5504 : /* Nonzero means after finally constructing the expression
5505 : convert it to this type. */
5506 152112652 : tree final_type = 0;
5507 :
5508 152112652 : tree result;
5509 :
5510 : /* Nonzero if this is an operation like MIN or MAX which can
5511 : safely be computed in short if both args are promoted shorts.
5512 : Also implies COMMON.
5513 : -1 indicates a bitwise operation; this makes a difference
5514 : in the exact conditions for when it is safe to do the operation
5515 : in a narrower mode. */
5516 152112652 : int shorten = 0;
5517 :
5518 : /* Nonzero if this is a comparison operation;
5519 : if both args are promoted shorts, compare the original shorts.
5520 : Also implies COMMON. */
5521 152112652 : int short_compare = 0;
5522 :
5523 : /* Nonzero if this is a right-shift operation, which can be computed on the
5524 : original short and then promoted if the operand is a promoted short. */
5525 152112652 : int short_shift = 0;
5526 :
5527 : /* Nonzero means set RESULT_TYPE to the common type of the args. */
5528 152112652 : int common = 0;
5529 :
5530 : /* True if both operands have arithmetic type. */
5531 152112652 : bool arithmetic_types_p;
5532 :
5533 : /* Remember whether we're doing / or %. */
5534 152112652 : bool doing_div_or_mod = false;
5535 :
5536 : /* Remember whether we're doing << or >>. */
5537 152112652 : bool doing_shift = false;
5538 :
5539 : /* Tree holding instrumentation expression. */
5540 152112652 : tree instrument_expr = NULL_TREE;
5541 :
5542 : /* True means this is an arithmetic operation that may need excess
5543 : precision. */
5544 152112652 : bool may_need_excess_precision;
5545 :
5546 : /* Apply default conversions. */
5547 152112652 : op0 = resolve_nondeduced_context (orig_op0, complain);
5548 152112652 : op1 = resolve_nondeduced_context (orig_op1, complain);
5549 :
5550 152112652 : if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5551 137460940 : || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5552 133858227 : || code == TRUTH_XOR_EXPR)
5553 : {
5554 18254425 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5555 18254407 : op0 = decay_conversion (op0, complain);
5556 18254425 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5557 18254425 : op1 = decay_conversion (op1, complain);
5558 : }
5559 : else
5560 : {
5561 133858227 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5562 133858178 : op0 = cp_default_conversion (op0, complain);
5563 133858227 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5564 133858208 : op1 = cp_default_conversion (op1, complain);
5565 : }
5566 :
5567 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5568 160899567 : STRIP_TYPE_NOPS (op0);
5569 186872016 : STRIP_TYPE_NOPS (op1);
5570 :
5571 : /* DTRT if one side is an overloaded function, but complain about it. */
5572 152112652 : if (type_unknown_p (op0))
5573 : {
5574 9 : tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5575 9 : if (t != error_mark_node)
5576 : {
5577 0 : if (complain & tf_error)
5578 0 : permerror (location,
5579 : "assuming cast to type %qT from overloaded function",
5580 0 : TREE_TYPE (t));
5581 : op0 = t;
5582 : }
5583 : }
5584 152112652 : if (type_unknown_p (op1))
5585 : {
5586 3 : tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5587 3 : if (t != error_mark_node)
5588 : {
5589 3 : if (complain & tf_error)
5590 3 : permerror (location,
5591 : "assuming cast to type %qT from overloaded function",
5592 3 : TREE_TYPE (t));
5593 : op1 = t;
5594 : }
5595 : }
5596 :
5597 152112652 : orig_type0 = type0 = TREE_TYPE (op0);
5598 152112652 : orig_type1 = type1 = TREE_TYPE (op1);
5599 152112652 : tree non_ep_op0 = op0;
5600 152112652 : tree non_ep_op1 = op1;
5601 :
5602 : /* The expression codes of the data types of the arguments tell us
5603 : whether the arguments are integers, floating, pointers, etc. */
5604 152112652 : code0 = TREE_CODE (type0);
5605 152112652 : code1 = TREE_CODE (type1);
5606 :
5607 : /* If an error was already reported for one of the arguments,
5608 : avoid reporting another error. */
5609 152112652 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5610 101 : return error_mark_node;
5611 :
5612 304225102 : if ((invalid_op_diag
5613 152112551 : = targetm.invalid_binary_op (code, type0, type1)))
5614 : {
5615 0 : if (complain & tf_error)
5616 : {
5617 0 : if (code0 == REAL_TYPE
5618 0 : && code1 == REAL_TYPE
5619 0 : && (extended_float_type_p (type0)
5620 0 : || extended_float_type_p (type1))
5621 0 : && cp_compare_floating_point_conversion_ranks (type0,
5622 : type1) == 3)
5623 : {
5624 0 : rich_location richloc (line_table, location);
5625 0 : binary_op_error (&richloc, code, type0, type1);
5626 0 : }
5627 : else
5628 0 : error (invalid_op_diag);
5629 : }
5630 0 : return error_mark_node;
5631 : }
5632 :
5633 152112551 : switch (code)
5634 : {
5635 : case PLUS_EXPR:
5636 : case MINUS_EXPR:
5637 : case MULT_EXPR:
5638 : case TRUNC_DIV_EXPR:
5639 : case CEIL_DIV_EXPR:
5640 : case FLOOR_DIV_EXPR:
5641 : case ROUND_DIV_EXPR:
5642 : case EXACT_DIV_EXPR:
5643 : may_need_excess_precision = true;
5644 : break;
5645 53814948 : case EQ_EXPR:
5646 53814948 : case NE_EXPR:
5647 53814948 : case LE_EXPR:
5648 53814948 : case GE_EXPR:
5649 53814948 : case LT_EXPR:
5650 53814948 : case GT_EXPR:
5651 53814948 : case SPACESHIP_EXPR:
5652 : /* Excess precision for implicit conversions of integers to
5653 : floating point. */
5654 11624160 : may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5655 65431568 : || ANY_INTEGRAL_TYPE_P (type1));
5656 : break;
5657 : default:
5658 152112551 : may_need_excess_precision = false;
5659 : break;
5660 : }
5661 152112551 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5662 : {
5663 14709 : op0 = TREE_OPERAND (op0, 0);
5664 14709 : type0 = TREE_TYPE (op0);
5665 : }
5666 152097842 : else if (may_need_excess_precision
5667 113804973 : && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5668 27763335 : if (tree eptype = excess_precision_type (type0))
5669 : {
5670 39385 : type0 = eptype;
5671 39385 : op0 = convert (eptype, op0);
5672 : }
5673 152112551 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5674 : {
5675 18577 : op1 = TREE_OPERAND (op1, 0);
5676 18577 : type1 = TREE_TYPE (op1);
5677 : }
5678 152093974 : else if (may_need_excess_precision
5679 113803275 : && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5680 27611634 : if (tree eptype = excess_precision_type (type1))
5681 : {
5682 26254 : type1 = eptype;
5683 26254 : op1 = convert (eptype, op1);
5684 : }
5685 :
5686 : /* Issue warnings about peculiar, but valid, uses of NULL. */
5687 304225017 : if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5688 : /* It's reasonable to use pointer values as operands of &&
5689 : and ||, so NULL is no exception. */
5690 12447 : && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5691 12432 : && ( /* Both are NULL (or 0) and the operation was not a
5692 : comparison or a pointer subtraction. */
5693 12505 : (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5694 27 : && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5695 : /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5696 12420 : || (!null_ptr_cst_p (orig_op0)
5697 12359 : && !TYPE_PTR_OR_PTRMEM_P (type0))
5698 12408 : || (!null_ptr_cst_p (orig_op1)
5699 46 : && !TYPE_PTR_OR_PTRMEM_P (type1)))
5700 152112590 : && (complain & tf_warning))
5701 : {
5702 39 : location_t loc =
5703 39 : expansion_point_location_if_in_system_header (input_location);
5704 :
5705 39 : warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5706 : }
5707 :
5708 : /* In case when one of the operands of the binary operation is
5709 : a vector and another is a scalar -- convert scalar to vector. */
5710 152181108 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5711 152179973 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5712 : {
5713 1220 : enum stv_conv convert_flag
5714 1220 : = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5715 : complain & tf_error);
5716 :
5717 1220 : switch (convert_flag)
5718 : {
5719 21 : case stv_error:
5720 21 : return error_mark_node;
5721 55 : case stv_firstarg:
5722 55 : {
5723 55 : op0 = convert (TREE_TYPE (type1), op0);
5724 55 : op0 = cp_save_expr (op0);
5725 55 : op0 = build_vector_from_val (type1, op0);
5726 55 : orig_type0 = type0 = TREE_TYPE (op0);
5727 55 : code0 = TREE_CODE (type0);
5728 55 : converted = 1;
5729 55 : break;
5730 : }
5731 1007 : case stv_secondarg:
5732 1007 : {
5733 1007 : op1 = convert (TREE_TYPE (type0), op1);
5734 1007 : op1 = cp_save_expr (op1);
5735 1007 : op1 = build_vector_from_val (type0, op1);
5736 1007 : orig_type1 = type1 = TREE_TYPE (op1);
5737 1007 : code1 = TREE_CODE (type1);
5738 1007 : converted = 1;
5739 1007 : break;
5740 : }
5741 : default:
5742 : break;
5743 : }
5744 : }
5745 :
5746 152112530 : switch (code)
5747 : {
5748 16708834 : case MINUS_EXPR:
5749 : /* Subtraction of two similar pointers.
5750 : We must subtract them as integers, then divide by object size. */
5751 16708834 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5752 18261546 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5753 1552712 : TREE_TYPE (type1)))
5754 : {
5755 1552709 : result = pointer_diff (location, op0, op1,
5756 : common_pointer_type (type0, type1), complain,
5757 : &instrument_expr);
5758 1552709 : if (instrument_expr != NULL)
5759 84 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5760 : instrument_expr, result);
5761 :
5762 1552709 : return result;
5763 : }
5764 : /* In all other cases except pointer - int, the usual arithmetic
5765 : rules apply. */
5766 15156125 : else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5767 : {
5768 : common = 1;
5769 : break;
5770 : }
5771 : /* The pointer - int case is just like pointer + int; fall
5772 : through. */
5773 21994073 : gcc_fallthrough ();
5774 21994073 : case PLUS_EXPR:
5775 21994073 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5776 8076556 : && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5777 : {
5778 8076547 : tree ptr_operand;
5779 8076547 : tree int_operand;
5780 8076547 : ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5781 130387 : int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5782 8076547 : if (processing_template_decl)
5783 : {
5784 2274916 : result_type = TREE_TYPE (ptr_operand);
5785 2274916 : break;
5786 : }
5787 5801631 : return cp_pointer_int_sum (location, code,
5788 : ptr_operand,
5789 : int_operand,
5790 5801631 : complain);
5791 : }
5792 : common = 1;
5793 : break;
5794 :
5795 : case MULT_EXPR:
5796 144711127 : common = 1;
5797 : break;
5798 :
5799 12420210 : case TRUNC_DIV_EXPR:
5800 12420210 : case CEIL_DIV_EXPR:
5801 12420210 : case FLOOR_DIV_EXPR:
5802 12420210 : case ROUND_DIV_EXPR:
5803 12420210 : case EXACT_DIV_EXPR:
5804 12420210 : if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5805 : {
5806 129545 : tree type0 = TREE_OPERAND (op0, 0);
5807 129545 : tree type1 = TREE_OPERAND (op1, 0);
5808 129545 : tree first_arg = tree_strip_any_location_wrapper (type0);
5809 129545 : if (!TYPE_P (type0))
5810 86887 : type0 = TREE_TYPE (type0);
5811 129545 : if (!TYPE_P (type1))
5812 50346 : type1 = TREE_TYPE (type1);
5813 129545 : if (type0
5814 129545 : && type1
5815 109804 : && INDIRECT_TYPE_P (type0)
5816 129620 : && same_type_p (TREE_TYPE (type0), type1))
5817 : {
5818 27 : if (!(TREE_CODE (first_arg) == PARM_DECL
5819 21 : && DECL_ARRAY_PARAMETER_P (first_arg)
5820 3 : && warn_sizeof_array_argument)
5821 42 : && (complain & tf_warning))
5822 : {
5823 21 : auto_diagnostic_group d;
5824 21 : if (warning_at (location, OPT_Wsizeof_pointer_div,
5825 : "division %<sizeof (%T) / sizeof (%T)%> does "
5826 : "not compute the number of array elements",
5827 : type0, type1))
5828 18 : if (DECL_P (first_arg))
5829 18 : inform (DECL_SOURCE_LOCATION (first_arg),
5830 : "first %<sizeof%> operand was declared here");
5831 21 : }
5832 : }
5833 129521 : else if (!dependent_type_p (type0)
5834 30708 : && !dependent_type_p (type1)
5835 30608 : && TREE_CODE (type0) == ARRAY_TYPE
5836 21626 : && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5837 : /* Set by finish_parenthesized_expr. */
5838 20975 : && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5839 150478 : && (complain & tf_warning))
5840 20957 : maybe_warn_sizeof_array_div (location, first_arg, type0,
5841 : op1, non_reference (type1));
5842 : }
5843 :
5844 12420210 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5845 : || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5846 12420192 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5847 : || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5848 : {
5849 12420183 : enum tree_code tcode0 = code0, tcode1 = code1;
5850 12420183 : doing_div_or_mod = true;
5851 12420183 : warn_for_div_by_zero (location, fold_for_warn (op1));
5852 :
5853 12420183 : if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5854 58930 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5855 12420183 : if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5856 30324 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5857 :
5858 12420186 : if (!((tcode0 == INTEGER_TYPE
5859 5443045 : || (tcode0 == ENUMERAL_TYPE && code0 == VECTOR_TYPE))
5860 : && (tcode1 == INTEGER_TYPE
5861 97439 : || (tcode1 == ENUMERAL_TYPE && code1 == VECTOR_TYPE))))
5862 : resultcode = RDIV_EXPR;
5863 : else
5864 : {
5865 : /* When dividing two signed integers, we have to promote to int.
5866 : unless we divide by a constant != -1. Note that default
5867 : conversion will have been performed on the operands at this
5868 : point, so we have to dig out the original type to find out if
5869 : it was unsigned. */
5870 6879705 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5871 6879705 : shorten = may_shorten_divmod (op0, stripped_op1);
5872 : }
5873 :
5874 : common = 1;
5875 : }
5876 : break;
5877 :
5878 2951343 : case BIT_AND_EXPR:
5879 2951343 : case BIT_IOR_EXPR:
5880 2951343 : case BIT_XOR_EXPR:
5881 2951343 : if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5882 2951343 : || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5883 21143 : && !VECTOR_FLOAT_TYPE_P (type0)
5884 21125 : && !VECTOR_FLOAT_TYPE_P (type1)))
5885 : shorten = -1;
5886 : break;
5887 :
5888 1573655 : case TRUNC_MOD_EXPR:
5889 1573655 : case FLOOR_MOD_EXPR:
5890 1573655 : doing_div_or_mod = true;
5891 1573655 : warn_for_div_by_zero (location, fold_for_warn (op1));
5892 :
5893 1573655 : if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5894 36 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5895 1573691 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5896 : common = 1;
5897 1573619 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5898 : {
5899 : /* Although it would be tempting to shorten always here, that loses
5900 : on some targets, since the modulo instruction is undefined if the
5901 : quotient can't be represented in the computation mode. We shorten
5902 : only if unsigned or if dividing by something we know != -1. */
5903 1573595 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5904 1573595 : shorten = may_shorten_divmod (op0, stripped_op1);
5905 1573595 : common = 1;
5906 : }
5907 : break;
5908 :
5909 18254410 : case TRUTH_ANDIF_EXPR:
5910 18254410 : case TRUTH_ORIF_EXPR:
5911 18254410 : case TRUTH_AND_EXPR:
5912 18254410 : case TRUTH_OR_EXPR:
5913 18254410 : if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5914 : {
5915 3 : if (!COMPARISON_CLASS_P (op1))
5916 3 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5917 : build_zero_cst (type1), complain);
5918 3 : if (code == TRUTH_ANDIF_EXPR)
5919 : {
5920 0 : tree z = build_zero_cst (TREE_TYPE (op1));
5921 0 : return build_conditional_expr (location, op0, op1, z, complain);
5922 : }
5923 3 : else if (code == TRUTH_ORIF_EXPR)
5924 : {
5925 3 : tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5926 3 : return build_conditional_expr (location, op0, m1, op1, complain);
5927 : }
5928 : else
5929 0 : gcc_unreachable ();
5930 : }
5931 18254407 : if (gnu_vector_type_p (type0)
5932 18254407 : && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5933 : {
5934 24 : if (!COMPARISON_CLASS_P (op0))
5935 24 : op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5936 : build_zero_cst (type0), complain);
5937 24 : if (!VECTOR_TYPE_P (type1))
5938 : {
5939 9 : tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5940 9 : tree z = build_zero_cst (TREE_TYPE (op0));
5941 9 : op1 = build_conditional_expr (location, op1, m1, z, complain);
5942 : }
5943 15 : else if (!COMPARISON_CLASS_P (op1))
5944 15 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5945 : build_zero_cst (type1), complain);
5946 :
5947 24 : if (code == TRUTH_ANDIF_EXPR)
5948 : code = BIT_AND_EXPR;
5949 9 : else if (code == TRUTH_ORIF_EXPR)
5950 : code = BIT_IOR_EXPR;
5951 : else
5952 0 : gcc_unreachable ();
5953 :
5954 24 : return cp_build_binary_op (location, code, op0, op1, complain);
5955 : }
5956 :
5957 18254383 : if (warn_constant_logical_operand
5958 131189 : && (complain & tf_warning)
5959 128762 : && (code0 == INTEGER_TYPE || code0 == ENUMERAL_TYPE)
5960 11192 : && (code1 == INTEGER_TYPE || code1 == ENUMERAL_TYPE))
5961 : {
5962 4861 : tree cop0 = fold_for_warn (op0), cop1 = fold_for_warn (op1);
5963 4861 : const char *name
5964 : = ((code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
5965 : ? "&&" : "||");
5966 4946 : auto enum_other_than_0_1 = [] (tree type) {
5967 85 : if (TREE_CODE (type) != ENUMERAL_TYPE)
5968 : return false;
5969 154 : for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l))
5970 : {
5971 125 : tree v = DECL_INITIAL (TREE_VALUE (l));
5972 125 : if (!integer_zerop (v) && !integer_onep (v))
5973 : return true;
5974 : }
5975 : return false;
5976 : };
5977 14535 : auto diagnose_constant_logical_operand = [=] (tree val, tree type) {
5978 9674 : if (TREE_CODE (val) != INTEGER_CST || integer_zerop (val))
5979 9517 : return false;
5980 157 : if (integer_onep (val) && !enum_other_than_0_1 (type))
5981 : return false;
5982 96 : gcc_rich_location richloc (location);
5983 96 : richloc.add_fixit_replace (name + 1);
5984 96 : auto_diagnostic_group d;
5985 96 : if (warning_at (location, OPT_Wconstant_logical_operand,
5986 : "use of logical %qs with constant operand %qE",
5987 : name, val))
5988 96 : inform (&richloc, "use %qs for bitwise operation", name + 1);
5989 96 : return true;
5990 96 : };
5991 4861 : if (!diagnose_constant_logical_operand (cop1, orig_type1))
5992 4813 : diagnose_constant_logical_operand (cop0, orig_type0);
5993 : }
5994 :
5995 18254383 : result_type = boolean_type_node;
5996 18254383 : break;
5997 :
5998 : /* Shift operations: result has same type as first operand;
5999 : always convert second operand to int.
6000 : Also set SHORT_SHIFT if shifting rightward. */
6001 :
6002 1147371 : case RSHIFT_EXPR:
6003 1147371 : if (gnu_vector_type_p (type0)
6004 202 : && code1 == INTEGER_TYPE
6005 1147436 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
6006 : {
6007 : result_type = type0;
6008 : converted = 1;
6009 : }
6010 1147306 : else if (gnu_vector_type_p (type0)
6011 137 : && gnu_vector_type_p (type1)
6012 137 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
6013 134 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
6014 1147440 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
6015 : TYPE_VECTOR_SUBPARTS (type1)))
6016 : {
6017 : result_type = type0;
6018 : converted = 1;
6019 : }
6020 1147172 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6021 : {
6022 1147145 : tree const_op1 = fold_for_warn (op1);
6023 1147145 : if (TREE_CODE (const_op1) != INTEGER_CST)
6024 105712 : const_op1 = op1;
6025 1147145 : result_type = type0;
6026 1147145 : doing_shift = true;
6027 1147145 : if (TREE_CODE (const_op1) == INTEGER_CST)
6028 : {
6029 1041433 : if (tree_int_cst_lt (const_op1, integer_zero_node))
6030 : {
6031 48 : if ((complain & tf_warning)
6032 48 : && c_inhibit_evaluation_warnings == 0)
6033 36 : warning_at (location, OPT_Wshift_count_negative,
6034 : "right shift count is negative");
6035 : }
6036 : else
6037 : {
6038 1041385 : if (!integer_zerop (const_op1))
6039 1041141 : short_shift = 1;
6040 :
6041 1041385 : if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
6042 46 : && (complain & tf_warning)
6043 1041431 : && c_inhibit_evaluation_warnings == 0)
6044 34 : warning_at (location, OPT_Wshift_count_overflow,
6045 : "right shift count >= width of type");
6046 : }
6047 : }
6048 : /* Avoid converting op1 to result_type later. */
6049 : converted = 1;
6050 : }
6051 : break;
6052 :
6053 3253591 : case LSHIFT_EXPR:
6054 3253591 : if (gnu_vector_type_p (type0)
6055 271 : && code1 == INTEGER_TYPE
6056 3253642 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
6057 : {
6058 : result_type = type0;
6059 : converted = 1;
6060 : }
6061 3253540 : else if (gnu_vector_type_p (type0)
6062 220 : && gnu_vector_type_p (type1)
6063 220 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
6064 217 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
6065 3253754 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
6066 : TYPE_VECTOR_SUBPARTS (type1)))
6067 : {
6068 : result_type = type0;
6069 : converted = 1;
6070 : }
6071 3253326 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6072 : {
6073 3253226 : tree const_op0 = fold_for_warn (op0);
6074 3253226 : if (TREE_CODE (const_op0) != INTEGER_CST)
6075 280782 : const_op0 = op0;
6076 3253226 : tree const_op1 = fold_for_warn (op1);
6077 3253226 : if (TREE_CODE (const_op1) != INTEGER_CST)
6078 231065 : const_op1 = op1;
6079 3253226 : result_type = type0;
6080 3253226 : doing_shift = true;
6081 3253226 : if (TREE_CODE (const_op0) == INTEGER_CST
6082 2972444 : && tree_int_cst_sgn (const_op0) < 0
6083 280 : && !TYPE_OVERFLOW_WRAPS (type0)
6084 196 : && (complain & tf_warning)
6085 3253422 : && c_inhibit_evaluation_warnings == 0)
6086 196 : warning_at (location, OPT_Wshift_negative_value,
6087 : "left shift of negative value");
6088 3253226 : if (TREE_CODE (const_op1) == INTEGER_CST)
6089 : {
6090 3022161 : if (tree_int_cst_lt (const_op1, integer_zero_node))
6091 : {
6092 57 : if ((complain & tf_warning)
6093 57 : && c_inhibit_evaluation_warnings == 0)
6094 39 : warning_at (location, OPT_Wshift_count_negative,
6095 : "left shift count is negative");
6096 : }
6097 3022104 : else if (compare_tree_int (const_op1,
6098 3022104 : TYPE_PRECISION (type0)) >= 0)
6099 : {
6100 83 : if ((complain & tf_warning)
6101 61 : && c_inhibit_evaluation_warnings == 0)
6102 45 : warning_at (location, OPT_Wshift_count_overflow,
6103 : "left shift count >= width of type");
6104 : }
6105 3022021 : else if (TREE_CODE (const_op0) == INTEGER_CST
6106 2780943 : && (complain & tf_warning))
6107 2668138 : maybe_warn_shift_overflow (location, const_op0, const_op1);
6108 : }
6109 : /* Avoid converting op1 to result_type later. */
6110 : converted = 1;
6111 : }
6112 : break;
6113 :
6114 27091832 : case EQ_EXPR:
6115 27091832 : case NE_EXPR:
6116 27091832 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6117 3333 : goto vector_compare;
6118 27088499 : if ((complain & tf_warning)
6119 24061173 : && c_inhibit_evaluation_warnings == 0
6120 49992653 : && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
6121 810286 : warning_at (location, OPT_Wfloat_equal,
6122 : "comparing floating-point with %<==%> "
6123 : "or %<!=%> is unsafe");
6124 27088499 : {
6125 27088499 : tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
6126 27088499 : tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
6127 27088499 : if ((complain & tf_warning_or_error)
6128 27088499 : && ((TREE_CODE (stripped_orig_op0) == STRING_CST
6129 39 : && !integer_zerop (cp_fully_fold (op1)))
6130 24555785 : || (TREE_CODE (stripped_orig_op1) == STRING_CST
6131 68 : && !integer_zerop (cp_fully_fold (op0)))))
6132 47 : warning_at (location, OPT_Waddress,
6133 : "comparison with string literal results in "
6134 : "unspecified behavior");
6135 27088452 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6136 27088452 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
6137 : {
6138 : /* P2865R5 made array comparisons ill-formed in C++26. */
6139 68 : if (complain & tf_warning_or_error)
6140 45 : do_warn_array_compare (location, code, stripped_orig_op0,
6141 : stripped_orig_op1);
6142 23 : else if (cxx_dialect >= cxx26)
6143 1 : return error_mark_node;
6144 : }
6145 : }
6146 :
6147 27088498 : build_type = boolean_type_node;
6148 27088498 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6149 : || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
6150 22811329 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6151 : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
6152 : short_compare = 1;
6153 45327 : else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
6154 4272109 : && null_ptr_cst_p (orig_op1))
6155 : /* Handle, eg, (void*)0 (c++/43906), and more. */
6156 6806792 : || (code0 == POINTER_TYPE
6157 2445377 : && TYPE_PTR_P (type1) && integer_zerop (op1)))
6158 : {
6159 1845589 : if (TYPE_PTR_P (type1))
6160 19255 : result_type = composite_pointer_type (location,
6161 : type0, type1, op0, op1,
6162 : CPO_COMPARISON, complain);
6163 : else
6164 : result_type = type0;
6165 :
6166 1845589 : if (char_type_p (TREE_TYPE (orig_op1)))
6167 : {
6168 10 : auto_diagnostic_group d;
6169 10 : if (warning_at (location, OPT_Wpointer_compare,
6170 : "comparison between pointer and zero character "
6171 : "constant"))
6172 10 : inform (location,
6173 : "did you mean to dereference the pointer?");
6174 10 : }
6175 1845589 : warn_for_null_address (location, op0, complain);
6176 : }
6177 5528 : else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
6178 2465868 : && null_ptr_cst_p (orig_op0))
6179 : /* Handle, eg, (void*)0 (c++/43906), and more. */
6180 4941055 : || (code1 == POINTER_TYPE
6181 2464577 : && TYPE_PTR_P (type0) && integer_zerop (op0)))
6182 : {
6183 961 : if (TYPE_PTR_P (type0))
6184 68 : result_type = composite_pointer_type (location,
6185 : type0, type1, op0, op1,
6186 : CPO_COMPARISON, complain);
6187 : else
6188 : result_type = type1;
6189 :
6190 961 : if (char_type_p (TREE_TYPE (orig_op0)))
6191 : {
6192 10 : auto_diagnostic_group d;
6193 10 : if (warning_at (location, OPT_Wpointer_compare,
6194 : "comparison between pointer and zero character "
6195 : "constant"))
6196 10 : inform (location,
6197 : "did you mean to dereference the pointer?");
6198 10 : }
6199 961 : warn_for_null_address (location, op1, complain);
6200 : }
6201 2470013 : else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6202 44017 : || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
6203 2426394 : result_type = composite_pointer_type (location,
6204 : type0, type1, op0, op1,
6205 : CPO_COMPARISON, complain);
6206 43619 : else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
6207 : /* One of the operands must be of nullptr_t type. */
6208 69 : result_type = TREE_TYPE (nullptr_node);
6209 43550 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6210 : {
6211 58 : result_type = type0;
6212 58 : if (complain & tf_error)
6213 56 : permerror (location, "ISO C++ forbids comparison between "
6214 : "pointer and integer");
6215 : else
6216 2 : return error_mark_node;
6217 : }
6218 43492 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6219 : {
6220 38513 : result_type = type1;
6221 38513 : if (complain & tf_error)
6222 69 : permerror (location, "ISO C++ forbids comparison between "
6223 : "pointer and integer");
6224 : else
6225 38444 : return error_mark_node;
6226 : }
6227 4979 : else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
6228 : {
6229 230 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6230 : == ptrmemfunc_vbit_in_delta)
6231 : {
6232 : tree pfn0, delta0, e1, e2;
6233 :
6234 : if (TREE_SIDE_EFFECTS (op0))
6235 : op0 = cp_save_expr (op0);
6236 :
6237 : pfn0 = pfn_from_ptrmemfunc (op0);
6238 : delta0 = delta_from_ptrmemfunc (op0);
6239 : {
6240 : /* If we will warn below about a null-address compare
6241 : involving the orig_op0 ptrmemfunc, we'd likely also
6242 : warn about the pfn0's null-address compare, and
6243 : that would be redundant, so suppress it. */
6244 : warning_sentinel ws (warn_address);
6245 : e1 = cp_build_binary_op (location,
6246 : EQ_EXPR,
6247 : pfn0,
6248 : build_zero_cst (TREE_TYPE (pfn0)),
6249 : complain);
6250 : }
6251 : e2 = cp_build_binary_op (location,
6252 : BIT_AND_EXPR,
6253 : delta0,
6254 : integer_one_node,
6255 : complain);
6256 :
6257 : if (complain & tf_warning)
6258 : maybe_warn_zero_as_null_pointer_constant (op1, input_location);
6259 :
6260 : e2 = cp_build_binary_op (location,
6261 : EQ_EXPR, e2, integer_zero_node,
6262 : complain);
6263 : op0 = cp_build_binary_op (location,
6264 : TRUTH_ANDIF_EXPR, e1, e2,
6265 : complain);
6266 : op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
6267 : }
6268 : else
6269 : {
6270 230 : op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
6271 230 : op1 = cp_convert (TREE_TYPE (op0), op1, complain);
6272 : }
6273 230 : result_type = TREE_TYPE (op0);
6274 :
6275 230 : warn_for_null_address (location, orig_op0, complain);
6276 : }
6277 4749 : else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
6278 36 : return cp_build_binary_op (location, code, op1, op0, complain);
6279 4713 : else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
6280 : {
6281 135 : tree type;
6282 : /* E will be the final comparison. */
6283 135 : tree e;
6284 : /* E1 and E2 are for scratch. */
6285 135 : tree e1;
6286 135 : tree e2;
6287 135 : tree pfn0;
6288 135 : tree pfn1;
6289 135 : tree delta0;
6290 135 : tree delta1;
6291 :
6292 135 : type = composite_pointer_type (location, type0, type1, op0, op1,
6293 : CPO_COMPARISON, complain);
6294 :
6295 135 : if (!same_type_p (TREE_TYPE (op0), type))
6296 6 : op0 = cp_convert_and_check (type, op0, complain);
6297 135 : if (!same_type_p (TREE_TYPE (op1), type))
6298 9 : op1 = cp_convert_and_check (type, op1, complain);
6299 :
6300 135 : if (op0 == error_mark_node || op1 == error_mark_node)
6301 : return error_mark_node;
6302 :
6303 132 : if (TREE_SIDE_EFFECTS (op0))
6304 69 : op0 = cp_save_expr (op0);
6305 132 : if (TREE_SIDE_EFFECTS (op1))
6306 3 : op1 = cp_save_expr (op1);
6307 :
6308 132 : pfn0 = pfn_from_ptrmemfunc (op0);
6309 132 : pfn0 = cp_fully_fold (pfn0);
6310 : /* Avoid -Waddress warnings (c++/64877). */
6311 132 : if (TREE_CODE (pfn0) == ADDR_EXPR)
6312 20 : suppress_warning (pfn0, OPT_Waddress);
6313 132 : pfn1 = pfn_from_ptrmemfunc (op1);
6314 132 : pfn1 = cp_fully_fold (pfn1);
6315 132 : delta0 = delta_from_ptrmemfunc (op0);
6316 132 : delta1 = delta_from_ptrmemfunc (op1);
6317 132 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6318 : == ptrmemfunc_vbit_in_delta)
6319 : {
6320 : /* We generate:
6321 :
6322 : (op0.pfn == op1.pfn
6323 : && ((op0.delta == op1.delta)
6324 : || (!op0.pfn && op0.delta & 1 == 0
6325 : && op1.delta & 1 == 0))
6326 :
6327 : The reason for the `!op0.pfn' bit is that a NULL
6328 : pointer-to-member is any member with a zero PFN and
6329 : LSB of the DELTA field is 0. */
6330 :
6331 : e1 = cp_build_binary_op (location, BIT_AND_EXPR,
6332 : delta0,
6333 : integer_one_node,
6334 : complain);
6335 : e1 = cp_build_binary_op (location,
6336 : EQ_EXPR, e1, integer_zero_node,
6337 : complain);
6338 : e2 = cp_build_binary_op (location, BIT_AND_EXPR,
6339 : delta1,
6340 : integer_one_node,
6341 : complain);
6342 : e2 = cp_build_binary_op (location,
6343 : EQ_EXPR, e2, integer_zero_node,
6344 : complain);
6345 : e1 = cp_build_binary_op (location,
6346 : TRUTH_ANDIF_EXPR, e2, e1,
6347 : complain);
6348 : e2 = cp_build_binary_op (location, EQ_EXPR,
6349 : pfn0,
6350 : build_zero_cst (TREE_TYPE (pfn0)),
6351 : complain);
6352 : e2 = cp_build_binary_op (location,
6353 : TRUTH_ANDIF_EXPR, e2, e1, complain);
6354 : e1 = cp_build_binary_op (location,
6355 : EQ_EXPR, delta0, delta1, complain);
6356 : e1 = cp_build_binary_op (location,
6357 : TRUTH_ORIF_EXPR, e1, e2, complain);
6358 : }
6359 : else
6360 : {
6361 : /* We generate:
6362 :
6363 : (op0.pfn == op1.pfn
6364 : && (!op0.pfn || op0.delta == op1.delta))
6365 :
6366 : The reason for the `!op0.pfn' bit is that a NULL
6367 : pointer-to-member is any member with a zero PFN; the
6368 : DELTA field is unspecified. */
6369 :
6370 132 : e1 = cp_build_binary_op (location,
6371 : EQ_EXPR, delta0, delta1, complain);
6372 132 : e2 = cp_build_binary_op (location,
6373 : EQ_EXPR,
6374 : pfn0,
6375 132 : build_zero_cst (TREE_TYPE (pfn0)),
6376 : complain);
6377 132 : e1 = cp_build_binary_op (location,
6378 : TRUTH_ORIF_EXPR, e1, e2, complain);
6379 : }
6380 132 : e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
6381 132 : e = cp_build_binary_op (location,
6382 : TRUTH_ANDIF_EXPR, e2, e1, complain);
6383 132 : if (code == EQ_EXPR)
6384 : return e;
6385 30 : return cp_build_binary_op (location,
6386 30 : EQ_EXPR, e, integer_zero_node, complain);
6387 : }
6388 : /* [expr.eq]: "If both operands are of type std::meta::info,
6389 : comparison is defined as follows..." */
6390 4578 : else if (code0 == META_TYPE && code1 == META_TYPE)
6391 : result_type = type0;
6392 : else
6393 : {
6394 28 : gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
6395 : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
6396 : type1));
6397 28 : gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
6398 : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
6399 : type0));
6400 : }
6401 :
6402 : break;
6403 :
6404 241 : case MAX_EXPR:
6405 241 : case MIN_EXPR:
6406 241 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6407 241 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6408 : shorten = 1;
6409 0 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6410 0 : result_type = composite_pointer_type (location,
6411 : type0, type1, op0, op1,
6412 : CPO_COMPARISON, complain);
6413 : break;
6414 :
6415 26723116 : case LE_EXPR:
6416 26723116 : case GE_EXPR:
6417 26723116 : case LT_EXPR:
6418 26723116 : case GT_EXPR:
6419 26723116 : case SPACESHIP_EXPR:
6420 26723116 : if (TREE_CODE (orig_op0) == STRING_CST
6421 26723116 : || TREE_CODE (orig_op1) == STRING_CST)
6422 : {
6423 0 : if (complain & tf_warning)
6424 0 : warning_at (location, OPT_Waddress,
6425 : "comparison with string literal results "
6426 : "in unspecified behavior");
6427 : }
6428 26723116 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6429 224 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6430 26723231 : && code != SPACESHIP_EXPR)
6431 : {
6432 96 : if (complain & tf_warning_or_error)
6433 51 : do_warn_array_compare (location, code,
6434 : tree_strip_any_location_wrapper (orig_op0),
6435 : tree_strip_any_location_wrapper (orig_op1));
6436 45 : else if (cxx_dialect >= cxx26)
6437 1 : return error_mark_node;
6438 : }
6439 :
6440 26723115 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6441 : {
6442 8227 : vector_compare:
6443 8227 : tree intt;
6444 8227 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6445 8227 : TREE_TYPE (type1))
6446 8227 : && !vector_types_compatible_elements_p (type0, type1))
6447 : {
6448 3 : if (complain & tf_error)
6449 : {
6450 3 : auto_diagnostic_group d;
6451 3 : error_at (location, "comparing vectors with different "
6452 : "element types");
6453 3 : inform (location, "operand types are %qT and %qT",
6454 : type0, type1);
6455 3 : }
6456 3 : return error_mark_node;
6457 : }
6458 :
6459 8224 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
6460 16448 : TYPE_VECTOR_SUBPARTS (type1)))
6461 : {
6462 3 : if (complain & tf_error)
6463 : {
6464 3 : auto_diagnostic_group d;
6465 3 : error_at (location, "comparing vectors with different "
6466 : "number of elements");
6467 3 : inform (location, "operand types are %qT and %qT",
6468 : type0, type1);
6469 3 : }
6470 3 : return error_mark_node;
6471 : }
6472 :
6473 : /* It's not precisely specified how the usual arithmetic
6474 : conversions apply to the vector types. Here, we use
6475 : the unsigned type if one of the operands is signed and
6476 : the other one is unsigned. */
6477 8221 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6478 : {
6479 36 : if (!TYPE_UNSIGNED (type0))
6480 36 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6481 : else
6482 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6483 36 : warning_at (location, OPT_Wsign_compare, "comparison between "
6484 : "types %qT and %qT", type0, type1);
6485 : }
6486 :
6487 8221 : if (resultcode == SPACESHIP_EXPR)
6488 : {
6489 3 : if (complain & tf_error)
6490 3 : sorry_at (location, "three-way comparison of vectors");
6491 3 : return error_mark_node;
6492 : }
6493 :
6494 8218 : if (VECTOR_BOOLEAN_TYPE_P (type0) && VECTOR_BOOLEAN_TYPE_P (type1))
6495 : result_type = type0;
6496 : else
6497 : {
6498 : /* Always construct signed integer vector type. */
6499 8218 : auto intmode = SCALAR_TYPE_MODE (TREE_TYPE (type0));
6500 8218 : auto nelts = TYPE_VECTOR_SUBPARTS (type0);
6501 :
6502 16436 : intt = c_common_type_for_size (GET_MODE_BITSIZE (intmode), 0);
6503 8218 : if (!intt)
6504 : {
6505 0 : if (complain & tf_error)
6506 0 : error_at (location, "could not find an integer type "
6507 0 : "of the same size as %qT", TREE_TYPE (type0));
6508 0 : return error_mark_node;
6509 : }
6510 8218 : result_type = build_opaque_vector_type (intt, nelts);
6511 : }
6512 8218 : return build_vec_cmp (resultcode, result_type, op0, op1);
6513 : }
6514 26718221 : build_type = boolean_type_node;
6515 26718221 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6516 : || code0 == ENUMERAL_TYPE)
6517 24001050 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6518 : || code1 == ENUMERAL_TYPE))
6519 : short_compare = 1;
6520 2717274 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6521 2716994 : result_type = composite_pointer_type (location,
6522 : type0, type1, op0, op1,
6523 : CPO_COMPARISON, complain);
6524 73 : else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6525 208 : || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6526 434 : || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6527 : {
6528 : /* Core Issue 1512 made this ill-formed. */
6529 190 : if (complain & tf_error)
6530 186 : error_at (location, "ordered comparison of pointer with "
6531 : "integer zero (%qT and %qT)", type0, type1);
6532 190 : return error_mark_node;
6533 : }
6534 90 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6535 : {
6536 0 : result_type = type0;
6537 0 : if (complain & tf_error)
6538 0 : permerror (location, "ISO C++ forbids comparison between "
6539 : "pointer and integer");
6540 : else
6541 0 : return error_mark_node;
6542 : }
6543 90 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6544 : {
6545 24 : result_type = type1;
6546 24 : if (complain & tf_error)
6547 24 : permerror (location, "ISO C++ forbids comparison between "
6548 : "pointer and integer");
6549 : else
6550 0 : return error_mark_node;
6551 : }
6552 :
6553 26718031 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6554 2717019 : && !processing_template_decl
6555 29213958 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6556 : {
6557 49 : op0 = cp_save_expr (op0);
6558 49 : op1 = cp_save_expr (op1);
6559 :
6560 49 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6561 49 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6562 : }
6563 :
6564 : break;
6565 :
6566 0 : case UNORDERED_EXPR:
6567 0 : case ORDERED_EXPR:
6568 0 : case UNLT_EXPR:
6569 0 : case UNLE_EXPR:
6570 0 : case UNGT_EXPR:
6571 0 : case UNGE_EXPR:
6572 0 : case UNEQ_EXPR:
6573 0 : build_type = integer_type_node;
6574 0 : if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6575 : {
6576 0 : if (complain & tf_error)
6577 0 : error ("unordered comparison on non-floating-point argument");
6578 0 : return error_mark_node;
6579 : }
6580 : common = 1;
6581 : break;
6582 :
6583 : default:
6584 : break;
6585 : }
6586 :
6587 144711127 : if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6588 : || code0 == ENUMERAL_TYPE)
6589 117676175 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6590 : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6591 : arithmetic_types_p = 1;
6592 : else
6593 : {
6594 27436158 : arithmetic_types_p = 0;
6595 : /* Vector arithmetic is only allowed when both sides are vectors. */
6596 27436158 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6597 : {
6598 60242 : if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6599 60242 : || !vector_types_compatible_elements_p (type0, type1))
6600 : {
6601 18 : if (complain & tf_error)
6602 : {
6603 : /* "location" already embeds the locations of the
6604 : operands, so we don't need to add them separately
6605 : to richloc. */
6606 18 : rich_location richloc (line_table, location);
6607 18 : binary_op_error (&richloc, code, type0, type1);
6608 18 : }
6609 18 : return error_mark_node;
6610 : }
6611 : arithmetic_types_p = 1;
6612 : }
6613 : }
6614 : /* Determine the RESULT_TYPE, if it is not already known. */
6615 144711109 : if (!result_type
6616 144711109 : && arithmetic_types_p
6617 112785647 : && (shorten || common || short_compare))
6618 : {
6619 112785500 : result_type = cp_common_type (type0, type1);
6620 112785500 : if (result_type == error_mark_node)
6621 : {
6622 6 : tree t1 = type0;
6623 6 : tree t2 = type1;
6624 6 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6625 0 : t1 = TREE_TYPE (t1);
6626 6 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6627 0 : t2 = TREE_TYPE (t2);
6628 6 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6629 : && TREE_CODE (t2) == REAL_TYPE
6630 : && (extended_float_type_p (t1)
6631 : || extended_float_type_p (t2))
6632 : && cp_compare_floating_point_conversion_ranks
6633 : (t1, t2) == 3);
6634 6 : if (complain & tf_error)
6635 : {
6636 6 : rich_location richloc (line_table, location);
6637 6 : binary_op_error (&richloc, code, type0, type1);
6638 6 : }
6639 6 : return error_mark_node;
6640 : }
6641 112785494 : if (complain & tf_warning)
6642 102669284 : do_warn_double_promotion (result_type, type0, type1,
6643 : "implicit conversion from %qH to %qI "
6644 : "to match other operand of binary "
6645 : "expression", location);
6646 112785494 : if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6647 112785494 : TREE_TYPE (orig_op1), complain))
6648 12 : return error_mark_node;
6649 : }
6650 144711091 : if (may_need_excess_precision
6651 106418959 : && (orig_type0 != type0 || orig_type1 != type1)
6652 56128 : && build_type == NULL_TREE
6653 56128 : && result_type)
6654 : {
6655 42672 : gcc_assert (common);
6656 42672 : semantic_result_type = cp_common_type (orig_type0, orig_type1);
6657 42672 : if (semantic_result_type == error_mark_node)
6658 : {
6659 0 : tree t1 = orig_type0;
6660 0 : tree t2 = orig_type1;
6661 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6662 0 : t1 = TREE_TYPE (t1);
6663 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6664 0 : t2 = TREE_TYPE (t2);
6665 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6666 : && TREE_CODE (t2) == REAL_TYPE
6667 : && (extended_float_type_p (t1)
6668 : || extended_float_type_p (t2))
6669 : && cp_compare_floating_point_conversion_ranks
6670 : (t1, t2) == 3);
6671 0 : if (complain & tf_error)
6672 : {
6673 0 : rich_location richloc (line_table, location);
6674 0 : binary_op_error (&richloc, code, type0, type1);
6675 0 : }
6676 0 : return error_mark_node;
6677 : }
6678 : }
6679 :
6680 144711091 : if (code == SPACESHIP_EXPR)
6681 : {
6682 622147 : iloc_sentinel s (location);
6683 :
6684 622147 : tree orig_type0 = TREE_TYPE (orig_op0);
6685 622147 : tree_code orig_code0 = TREE_CODE (orig_type0);
6686 622147 : tree orig_type1 = TREE_TYPE (orig_op1);
6687 622147 : tree_code orig_code1 = TREE_CODE (orig_type1);
6688 622147 : if (!result_type || result_type == error_mark_node)
6689 : /* Nope. */
6690 : result_type = NULL_TREE;
6691 622126 : else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6692 : /* "If one of the operands is of type bool and the other is not, the
6693 : program is ill-formed." */
6694 : result_type = NULL_TREE;
6695 622123 : else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6696 19 : && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6697 : /* We only do array/function-to-pointer conversion if "at least one of
6698 : the operands is of pointer type". */
6699 : result_type = NULL_TREE;
6700 622104 : else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6701 : /* <=> no longer supports equality relations. */
6702 : result_type = NULL_TREE;
6703 622101 : else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6704 622131 : && !(same_type_ignoring_top_level_qualifiers_p
6705 30 : (orig_type0, orig_type1)))
6706 : /* "If both operands have arithmetic types, or one operand has integral
6707 : type and the other operand has unscoped enumeration type, the usual
6708 : arithmetic conversions are applied to the operands." So we don't do
6709 : arithmetic conversions if the operands both have enumeral type. */
6710 : result_type = NULL_TREE;
6711 622086 : else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6712 622080 : || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6713 : /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6714 : [where one is of enumeration type and the other is of a different
6715 : enumeration type or a floating-point type] are ill-formed. */
6716 : result_type = NULL_TREE;
6717 :
6718 622074 : if (result_type)
6719 : {
6720 622074 : build_type = spaceship_type (result_type, complain);
6721 622074 : if (build_type == error_mark_node)
6722 : return error_mark_node;
6723 : }
6724 :
6725 622132 : if (result_type && arithmetic_types_p)
6726 : {
6727 : /* If a narrowing conversion is required, other than from an integral
6728 : type to a floating point type, the program is ill-formed. */
6729 291148 : bool ok = true;
6730 291148 : if (TREE_CODE (result_type) == REAL_TYPE
6731 603 : && CP_INTEGRAL_TYPE_P (orig_type0))
6732 : /* OK */;
6733 291141 : else if (!check_narrowing (result_type, orig_op0, complain))
6734 291148 : ok = false;
6735 291148 : if (TREE_CODE (result_type) == REAL_TYPE
6736 603 : && CP_INTEGRAL_TYPE_P (orig_type1))
6737 : /* OK */;
6738 291141 : else if (!check_narrowing (result_type, orig_op1, complain))
6739 : ok = false;
6740 291148 : if (!ok && !(complain & tf_error))
6741 2 : return error_mark_node;
6742 : }
6743 622147 : }
6744 :
6745 144711074 : if (!result_type)
6746 : {
6747 606 : if (complain & tf_error)
6748 : {
6749 221 : binary_op_rich_location richloc (location,
6750 221 : orig_op0, orig_op1, true);
6751 221 : error_at (&richloc,
6752 : "invalid operands of types %qT and %qT to binary %qO",
6753 221 : TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6754 221 : }
6755 606 : return error_mark_node;
6756 : }
6757 :
6758 : /* If we're in a template, the only thing we need to know is the
6759 : RESULT_TYPE. */
6760 144710468 : if (processing_template_decl)
6761 : {
6762 : /* Since the middle-end checks the type when doing a build2, we
6763 : need to build the tree in pieces. This built tree will never
6764 : get out of the front-end as we replace it when instantiating
6765 : the template. */
6766 67095710 : tree tmp = build2 (resultcode,
6767 : build_type ? build_type : result_type,
6768 : NULL_TREE, op1);
6769 41929417 : TREE_OPERAND (tmp, 0) = op0;
6770 41929417 : if (semantic_result_type)
6771 72 : tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6772 41929417 : return tmp;
6773 : }
6774 :
6775 : /* Remember the original type; RESULT_TYPE might be changed later on
6776 : by shorten_binary_op. */
6777 102781051 : tree orig_type = result_type;
6778 :
6779 102781051 : if (arithmetic_types_p)
6780 : {
6781 88090849 : bool first_complex = (code0 == COMPLEX_TYPE);
6782 88090849 : bool second_complex = (code1 == COMPLEX_TYPE);
6783 88090849 : int none_complex = (!first_complex && !second_complex);
6784 :
6785 : /* Adapted from patch for c/24581. */
6786 88090849 : if (first_complex != second_complex
6787 161199 : && (code == PLUS_EXPR
6788 : || code == MINUS_EXPR
6789 161199 : || code == MULT_EXPR
6790 42119 : || (code == TRUNC_DIV_EXPR && first_complex))
6791 147692 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6792 88238380 : && flag_signed_zeros)
6793 : {
6794 : /* An operation on mixed real/complex operands must be
6795 : handled specially, but the language-independent code can
6796 : more easily optimize the plain complex arithmetic if
6797 : -fno-signed-zeros. */
6798 147531 : tree real_type = TREE_TYPE (result_type);
6799 147531 : tree real, imag;
6800 147531 : if (first_complex)
6801 : {
6802 114483 : if (TREE_TYPE (op0) != result_type)
6803 6 : op0 = cp_convert_and_check (result_type, op0, complain);
6804 114483 : if (TREE_TYPE (op1) != real_type)
6805 26 : op1 = cp_convert_and_check (real_type, op1, complain);
6806 : }
6807 : else
6808 : {
6809 33048 : if (TREE_TYPE (op0) != real_type)
6810 31 : op0 = cp_convert_and_check (real_type, op0, complain);
6811 33048 : if (TREE_TYPE (op1) != result_type)
6812 32 : op1 = cp_convert_and_check (result_type, op1, complain);
6813 : }
6814 147531 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6815 0 : return error_mark_node;
6816 147531 : if (first_complex)
6817 : {
6818 114483 : op0 = cp_save_expr (op0);
6819 114483 : real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6820 114483 : imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6821 114483 : switch (code)
6822 : {
6823 57219 : case MULT_EXPR:
6824 57219 : case TRUNC_DIV_EXPR:
6825 57219 : op1 = cp_save_expr (op1);
6826 57219 : imag = build2 (resultcode, real_type, imag, op1);
6827 : /* Fall through. */
6828 114483 : case PLUS_EXPR:
6829 114483 : case MINUS_EXPR:
6830 114483 : real = build2 (resultcode, real_type, real, op1);
6831 114483 : break;
6832 0 : default:
6833 0 : gcc_unreachable();
6834 : }
6835 : }
6836 : else
6837 : {
6838 33048 : op1 = cp_save_expr (op1);
6839 33048 : real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6840 33048 : imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6841 33048 : switch (code)
6842 : {
6843 638 : case MULT_EXPR:
6844 638 : op0 = cp_save_expr (op0);
6845 638 : imag = build2 (resultcode, real_type, op0, imag);
6846 : /* Fall through. */
6847 17301 : case PLUS_EXPR:
6848 17301 : real = build2 (resultcode, real_type, op0, real);
6849 17301 : break;
6850 15747 : case MINUS_EXPR:
6851 15747 : real = build2 (resultcode, real_type, op0, real);
6852 15747 : imag = build1 (NEGATE_EXPR, real_type, imag);
6853 15747 : break;
6854 0 : default:
6855 0 : gcc_unreachable();
6856 : }
6857 : }
6858 147531 : result = build2 (COMPLEX_EXPR, result_type, real, imag);
6859 147531 : if (semantic_result_type)
6860 26 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6861 : result);
6862 147531 : return result;
6863 : }
6864 :
6865 : /* For certain operations (which identify themselves by shorten != 0)
6866 : if both args were extended from the same smaller type,
6867 : do the arithmetic in that type and then extend.
6868 :
6869 : shorten !=0 and !=1 indicates a bitwise operation.
6870 : For them, this optimization is safe only if
6871 : both args are zero-extended or both are sign-extended.
6872 : Otherwise, we might change the result.
6873 : E.g., (short)-1 | (unsigned short)-1 is (int)-1
6874 : but calculated in (unsigned short) it would be (unsigned short)-1. */
6875 :
6876 87943318 : if (shorten && none_complex)
6877 : {
6878 6989402 : final_type = result_type;
6879 6989402 : result_type = shorten_binary_op (result_type, op0, op1,
6880 : shorten == -1);
6881 : }
6882 :
6883 : /* Shifts can be shortened if shifting right. */
6884 :
6885 87943318 : if (short_shift)
6886 : {
6887 864611 : int unsigned_arg;
6888 864611 : tree arg0 = get_narrower (op0, &unsigned_arg);
6889 : /* We're not really warning here but when we set short_shift we
6890 : used fold_for_warn to fold the operand. */
6891 864611 : tree const_op1 = fold_for_warn (op1);
6892 :
6893 864611 : final_type = result_type;
6894 :
6895 864611 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
6896 784650 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6897 :
6898 864611 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6899 2702 : && tree_int_cst_sgn (const_op1) > 0
6900 : /* We can shorten only if the shift count is less than the
6901 : number of bits in the smaller type size. */
6902 2702 : && compare_tree_int (const_op1,
6903 2702 : TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6904 : /* We cannot drop an unsigned shift after sign-extension. */
6905 867273 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6906 : {
6907 : /* Do an unsigned shift if the operand was zero-extended. */
6908 2655 : result_type
6909 2655 : = c_common_signed_or_unsigned_type (unsigned_arg,
6910 2655 : TREE_TYPE (arg0));
6911 : /* Convert value-to-be-shifted to that type. */
6912 2655 : if (TREE_TYPE (op0) != result_type)
6913 2655 : op0 = convert (result_type, op0);
6914 : converted = 1;
6915 : }
6916 : }
6917 :
6918 : /* Comparison operations are shortened too but differently.
6919 : They identify themselves by setting short_compare = 1. */
6920 :
6921 87943318 : if (short_compare)
6922 : {
6923 : /* We call shorten_compare only for diagnostics. */
6924 30455320 : tree xop0 = fold_simple (op0);
6925 30455320 : tree xop1 = fold_simple (op1);
6926 30455320 : tree xresult_type = result_type;
6927 30455320 : enum tree_code xresultcode = resultcode;
6928 30455320 : shorten_compare (location, &xop0, &xop1, &xresult_type,
6929 : &xresultcode);
6930 : }
6931 :
6932 57487911 : if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6933 30455509 : && warn_sign_compare
6934 : /* Do not warn until the template is instantiated; we cannot
6935 : bound the ranges of the arguments until that point. */
6936 343321 : && !processing_template_decl
6937 343321 : && (complain & tf_warning)
6938 335398 : && c_inhibit_evaluation_warnings == 0
6939 : /* Even unsigned enum types promote to signed int. We don't
6940 : want to issue -Wsign-compare warnings for this case. */
6941 318967 : && !enum_cast_to_int (orig_op0)
6942 88262285 : && !enum_cast_to_int (orig_op1))
6943 : {
6944 318960 : warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6945 : result_type, resultcode);
6946 : }
6947 : }
6948 :
6949 : /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6950 : Then the expression will be built.
6951 : It will be given type FINAL_TYPE if that is nonzero;
6952 : otherwise, it will be given type RESULT_TYPE. */
6953 102633520 : if (! converted)
6954 : {
6955 98816808 : warning_sentinel w (warn_sign_conversion, short_compare);
6956 98816808 : if (!same_type_p (TREE_TYPE (op0), result_type))
6957 4314542 : op0 = cp_convert_and_check (result_type, op0, complain);
6958 98816808 : if (!same_type_p (TREE_TYPE (op1), result_type))
6959 19226979 : op1 = cp_convert_and_check (result_type, op1, complain);
6960 :
6961 98816808 : if (op0 == error_mark_node || op1 == error_mark_node)
6962 58 : return error_mark_node;
6963 98816808 : }
6964 :
6965 102633462 : if (build_type == NULL_TREE)
6966 65628890 : build_type = result_type;
6967 :
6968 102633462 : if (doing_shift
6969 3815975 : && flag_strong_eval_order == 2
6970 3716788 : && TREE_SIDE_EFFECTS (op1)
6971 102656182 : && !processing_template_decl)
6972 : {
6973 : /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6974 : op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6975 22720 : op0 = cp_save_expr (op0);
6976 22720 : instrument_expr = op0;
6977 : }
6978 :
6979 102633462 : if (sanitize_flags_p ((SANITIZE_SHIFT
6980 : | SANITIZE_DIVIDE
6981 : | SANITIZE_FLOAT_DIVIDE
6982 : | SANITIZE_SI_OVERFLOW))
6983 13487 : && current_function_decl != NULL_TREE
6984 11093 : && !processing_template_decl
6985 102644555 : && (doing_div_or_mod || doing_shift))
6986 : {
6987 : /* OP0 and/or OP1 might have side-effects. */
6988 939 : op0 = cp_save_expr (op0);
6989 939 : op1 = cp_save_expr (op1);
6990 939 : op0 = fold_non_dependent_expr (op0, complain);
6991 939 : op1 = fold_non_dependent_expr (op1, complain);
6992 939 : tree instrument_expr1 = NULL_TREE;
6993 939 : if (doing_div_or_mod
6994 939 : && sanitize_flags_p (SANITIZE_DIVIDE
6995 : | SANITIZE_FLOAT_DIVIDE
6996 : | SANITIZE_SI_OVERFLOW))
6997 : {
6998 : /* For diagnostics we want to use the promoted types without
6999 : shorten_binary_op. So convert the arguments to the
7000 : original result_type. */
7001 475 : tree cop0 = op0;
7002 475 : tree cop1 = op1;
7003 475 : if (TREE_TYPE (cop0) != orig_type)
7004 6 : cop0 = cp_convert (orig_type, op0, complain);
7005 475 : if (TREE_TYPE (cop1) != orig_type)
7006 39 : cop1 = cp_convert (orig_type, op1, complain);
7007 475 : instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
7008 : }
7009 464 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
7010 383 : instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
7011 939 : if (instrument_expr != NULL)
7012 18 : instrument_expr = add_stmt_to_compound (instrument_expr,
7013 : instrument_expr1);
7014 : else
7015 921 : instrument_expr = instrument_expr1;
7016 : }
7017 :
7018 102633462 : result = build2_loc (location, resultcode, build_type, op0, op1);
7019 102633462 : if (final_type != 0)
7020 7854013 : result = cp_convert (final_type, result, complain);
7021 :
7022 102633462 : if (instrument_expr != NULL)
7023 23218 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
7024 : instrument_expr, result);
7025 :
7026 102633462 : if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
7027 522461 : result = get_target_expr (result, complain);
7028 :
7029 102633462 : if (semantic_result_type)
7030 42574 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
7031 :
7032 102633462 : if (!c_inhibit_evaluation_warnings)
7033 : {
7034 87060774 : if (!processing_template_decl)
7035 : {
7036 87060774 : op0 = cp_fully_fold (op0);
7037 : /* Only consider the second argument if the first isn't overflowed. */
7038 87060774 : if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
7039 : return result;
7040 24500232 : op1 = cp_fully_fold (op1);
7041 24500232 : if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
7042 : return result;
7043 : }
7044 0 : else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
7045 0 : || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
7046 : return result;
7047 :
7048 18955705 : tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
7049 18955705 : if (TREE_OVERFLOW_P (result_ovl))
7050 190 : overflow_warning (location, result_ovl);
7051 : }
7052 :
7053 : return result;
7054 : }
7055 :
7056 : /* Build a VEC_PERM_EXPR.
7057 : This is a simple wrapper for c_build_vec_perm_expr. */
7058 : tree
7059 17402 : build_x_vec_perm_expr (location_t loc,
7060 : tree arg0, tree arg1, tree arg2,
7061 : tsubst_flags_t complain)
7062 : {
7063 17402 : tree orig_arg0 = arg0;
7064 17402 : tree orig_arg1 = arg1;
7065 17402 : tree orig_arg2 = arg2;
7066 17402 : if (processing_template_decl)
7067 : {
7068 19 : if (type_dependent_expression_p (arg0)
7069 13 : || type_dependent_expression_p (arg1)
7070 32 : || type_dependent_expression_p (arg2))
7071 6 : return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
7072 : }
7073 17396 : tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
7074 17396 : if (processing_template_decl && exp != error_mark_node)
7075 13 : return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
7076 13 : orig_arg1, orig_arg2);
7077 : return exp;
7078 : }
7079 :
7080 : /* Build a VEC_PERM_EXPR.
7081 : This is a simple wrapper for c_build_shufflevector. */
7082 : tree
7083 42187 : build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
7084 : tsubst_flags_t complain)
7085 : {
7086 42187 : tree arg0 = (*args)[0];
7087 42187 : tree arg1 = (*args)[1];
7088 42187 : if (processing_template_decl)
7089 : {
7090 743 : for (unsigned i = 0; i < args->length (); ++i)
7091 712 : if (i <= 1
7092 712 : ? type_dependent_expression_p ((*args)[i])
7093 121 : : instantiation_dependent_expression_p ((*args)[i]))
7094 : {
7095 526 : tree exp = build_min_nt_call_vec (NULL, args);
7096 526 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
7097 526 : return exp;
7098 : }
7099 : }
7100 41661 : auto_vec<tree, 16> mask;
7101 561197 : for (unsigned i = 2; i < args->length (); ++i)
7102 : {
7103 519536 : tree idx = fold_non_dependent_expr ((*args)[i], complain);
7104 519536 : mask.safe_push (idx);
7105 : }
7106 41661 : tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
7107 41661 : if (processing_template_decl && exp != error_mark_node)
7108 : {
7109 31 : exp = build_min_non_dep_call_vec (exp, NULL, args);
7110 31 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
7111 : }
7112 41661 : return exp;
7113 41661 : }
7114 :
7115 : /* Build __builtin_bswapg (ARG) or __builtin_bitreverseg (ARG). */
7116 : tree
7117 162 : build_x_bswapg_bitreverseg (location_t loc, internal_fn ifn,
7118 : vec<tree, va_gc> *args, tsubst_flags_t complain)
7119 : {
7120 81 : const char *name
7121 162 : = ifn == IFN_BSWAP ? "__builtin_bswapg" : "__builtin_bitreverseg";
7122 162 : if (args->length () != 1)
7123 : {
7124 18 : if (complain & tf_error)
7125 18 : error_at (loc, "wrong number of arguments to %qs", name);
7126 18 : return error_mark_node;
7127 : }
7128 144 : tree arg = (*args)[0];
7129 144 : if (error_operand_p (arg))
7130 6 : return error_mark_node;
7131 138 : if (type_dependent_expression_p (arg))
7132 : {
7133 0 : tree exp = build_min_nt_call_vec (NULL, args);
7134 0 : CALL_EXPR_IFN (exp) = ifn;
7135 0 : return exp;
7136 : }
7137 138 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (arg));
7138 138 : if (!INTEGRAL_TYPE_P (type))
7139 : {
7140 24 : if (complain & tf_error)
7141 24 : error_at (loc, "%qs operand not an integral type", name);
7142 24 : return error_mark_node;
7143 : }
7144 114 : if (TREE_CODE (type) == ENUMERAL_TYPE)
7145 : {
7146 30 : if (complain & tf_error)
7147 30 : error_at (loc, "argument %u in call to function "
7148 : "%qs has enumerated type", 1, name);
7149 30 : return error_mark_node;
7150 : }
7151 84 : if (TREE_CODE (type) == BOOLEAN_TYPE)
7152 : {
7153 6 : if (complain & tf_error)
7154 6 : error_at (loc, "argument %u in call to function "
7155 : "%qs has boolean type", 1, name);
7156 6 : return error_mark_node;
7157 : }
7158 78 : if (!TYPE_UNSIGNED (type))
7159 : {
7160 12 : if (complain & tf_error)
7161 12 : error_at (loc, "argument 1 in call to function "
7162 : "%qs has signed type", name);
7163 12 : return error_mark_node;
7164 : }
7165 66 : if (ifn == IFN_BSWAP && (TYPE_PRECISION (type) % 8) != 0)
7166 : {
7167 0 : if (complain & tf_error)
7168 0 : error_at (loc, "precision %d of argument 1 to function "
7169 : "%qs is not a multiple of 8",
7170 0 : TYPE_PRECISION (type), name);
7171 0 : return error_mark_node;
7172 : }
7173 66 : tree exp = build_call_expr_internal_loc (loc, ifn, type, 1, arg);
7174 66 : if (processing_template_decl)
7175 : {
7176 0 : exp = build_min_non_dep_call_vec (exp, NULL, args);
7177 0 : CALL_EXPR_IFN (exp) = ifn;
7178 : }
7179 : return exp;
7180 : }
7181 :
7182 : /* Return a tree for the sum or difference (RESULTCODE says which)
7183 : of pointer PTROP and integer INTOP. */
7184 :
7185 : static tree
7186 5801631 : cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
7187 : tree intop, tsubst_flags_t complain)
7188 : {
7189 5801631 : tree res_type = TREE_TYPE (ptrop);
7190 :
7191 : /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
7192 : in certain circumstance (when it's valid to do so). So we need
7193 : to make sure it's complete. We don't need to check here, if we
7194 : can actually complete it at all, as those checks will be done in
7195 : pointer_int_sum() anyway. */
7196 5801631 : complete_type (TREE_TYPE (res_type));
7197 :
7198 5801631 : return pointer_int_sum (loc, resultcode, ptrop,
7199 5801631 : intop, complain & tf_warning_or_error);
7200 : }
7201 :
7202 : /* Return a tree for the difference of pointers OP0 and OP1.
7203 : The resulting tree has type int. If POINTER_SUBTRACT sanitization is
7204 : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
7205 :
7206 : static tree
7207 1552709 : pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
7208 : tsubst_flags_t complain, tree *instrument_expr)
7209 : {
7210 1552709 : tree result, inttype;
7211 1552709 : tree restype = ptrdiff_type_node;
7212 1552709 : tree target_type = TREE_TYPE (ptrtype);
7213 :
7214 1552709 : if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
7215 9 : return error_mark_node;
7216 :
7217 1552700 : if (VOID_TYPE_P (target_type))
7218 : {
7219 0 : if (complain & tf_error)
7220 0 : permerror (loc, "ISO C++ forbids using pointer of "
7221 : "type %<void *%> in subtraction");
7222 : else
7223 0 : return error_mark_node;
7224 : }
7225 1552700 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
7226 : {
7227 9 : if (complain & tf_error)
7228 3 : permerror (loc, "ISO C++ forbids using pointer to "
7229 : "a function in subtraction");
7230 : else
7231 6 : return error_mark_node;
7232 : }
7233 1552694 : if (TREE_CODE (target_type) == METHOD_TYPE)
7234 : {
7235 0 : if (complain & tf_error)
7236 0 : permerror (loc, "ISO C++ forbids using pointer to "
7237 : "a method in subtraction");
7238 : else
7239 0 : return error_mark_node;
7240 : }
7241 3105388 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
7242 1552694 : TREE_TYPE (TREE_TYPE (op0)),
7243 : !(complain & tf_error))
7244 3105388 : || !verify_type_context (loc, TCTX_POINTER_ARITH,
7245 1552694 : TREE_TYPE (TREE_TYPE (op1)),
7246 : !(complain & tf_error)))
7247 0 : return error_mark_node;
7248 :
7249 : /* Determine integer type result of the subtraction. This will usually
7250 : be the same as the result type (ptrdiff_t), but may need to be a wider
7251 : type if pointers for the address space are wider than ptrdiff_t. */
7252 1552694 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
7253 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
7254 : else
7255 : inttype = restype;
7256 :
7257 1552694 : if (!processing_template_decl
7258 1552694 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
7259 : {
7260 84 : op0 = save_expr (op0);
7261 84 : op1 = save_expr (op1);
7262 :
7263 84 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
7264 84 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
7265 : }
7266 :
7267 : /* First do the subtraction, then build the divide operator
7268 : and only convert at the very end.
7269 : Do not do default conversions in case restype is a short type. */
7270 :
7271 : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
7272 : pointers. If some platform cannot provide that, or has a larger
7273 : ptrdiff_type to support differences larger than half the address
7274 : space, cast the pointers to some larger integer type and do the
7275 : computations in that type. */
7276 1552694 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
7277 0 : op0 = cp_build_binary_op (loc,
7278 : MINUS_EXPR,
7279 : cp_convert (inttype, op0, complain),
7280 : cp_convert (inttype, op1, complain),
7281 : complain);
7282 : else
7283 1552694 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
7284 :
7285 : /* This generates an error if op1 is a pointer to an incomplete type. */
7286 1552694 : if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
7287 : {
7288 0 : if (complain & tf_error)
7289 0 : error_at (loc, "invalid use of a pointer to an incomplete type in "
7290 : "pointer arithmetic");
7291 : else
7292 0 : return error_mark_node;
7293 : }
7294 :
7295 1552694 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
7296 : {
7297 15 : if (complain & tf_error)
7298 15 : error_at (loc, "arithmetic on pointer to an empty aggregate");
7299 : else
7300 0 : return error_mark_node;
7301 : }
7302 :
7303 3105385 : op1 = (TYPE_PTROB_P (ptrtype)
7304 3105385 : ? size_in_bytes_loc (loc, target_type)
7305 : : integer_one_node);
7306 :
7307 : /* Do the division. */
7308 :
7309 1552694 : result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
7310 : cp_convert (inttype, op1, complain));
7311 1552694 : return cp_convert (restype, result, complain);
7312 : }
7313 :
7314 : /* Construct and perhaps optimize a tree representation
7315 : for a unary operation. CODE, a tree_code, specifies the operation
7316 : and XARG is the operand. */
7317 :
7318 : tree
7319 65847395 : build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
7320 : tree lookups, tsubst_flags_t complain)
7321 : {
7322 65847395 : tree orig_expr = xarg;
7323 65847395 : tree exp;
7324 65847395 : int ptrmem = 0;
7325 65847395 : tree overload = NULL_TREE;
7326 :
7327 65847395 : if (processing_template_decl)
7328 : {
7329 44878165 : if (type_dependent_expression_p (xarg))
7330 : {
7331 29337218 : tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
7332 29337218 : TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
7333 29337218 : return e;
7334 : }
7335 : }
7336 :
7337 36510177 : exp = NULL_TREE;
7338 :
7339 : /* [expr.unary.op] says:
7340 :
7341 : The address of an object of incomplete type can be taken.
7342 :
7343 : (And is just the ordinary address operator, not an overloaded
7344 : "operator &".) However, if the type is a template
7345 : specialization, we must complete the type at this point so that
7346 : an overloaded "operator &" will be available if required. */
7347 36510177 : if (code == ADDR_EXPR
7348 4235561 : && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
7349 40745323 : && ((CLASS_TYPE_P (TREE_TYPE (xarg))
7350 1853157 : && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
7351 4225750 : || (TREE_CODE (xarg) == OFFSET_REF)))
7352 : /* Don't look for a function. */;
7353 : else
7354 36436839 : exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
7355 : NULL_TREE, lookups, &overload, complain);
7356 :
7357 36510177 : if (!exp && code == ADDR_EXPR)
7358 : {
7359 4235148 : if (is_overloaded_fn (xarg))
7360 : {
7361 391481 : tree fn = get_first_fn (xarg);
7362 782962 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
7363 : {
7364 12 : if (complain & tf_error)
7365 21 : error_at (loc, DECL_CONSTRUCTOR_P (fn)
7366 : ? G_("taking address of constructor %qD")
7367 : : G_("taking address of destructor %qD"),
7368 : fn);
7369 12 : return error_mark_node;
7370 : }
7371 : }
7372 :
7373 : /* A pointer to member-function can be formed only by saying
7374 : &X::mf. */
7375 4235130 : if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
7376 4295297 : && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
7377 : {
7378 9 : if (TREE_CODE (xarg) != OFFSET_REF
7379 9 : || !TYPE_P (TREE_OPERAND (xarg, 0)))
7380 : {
7381 9 : if (complain & tf_error)
7382 : {
7383 9 : auto_diagnostic_group d;
7384 9 : error_at (loc, "invalid use of %qE to form a "
7385 : "pointer-to-member-function", xarg.get_value ());
7386 9 : if (TREE_CODE (xarg) != OFFSET_REF)
7387 6 : inform (loc, " a qualified-id is required");
7388 9 : }
7389 9 : return error_mark_node;
7390 : }
7391 : else
7392 : {
7393 0 : if (complain & tf_error)
7394 0 : error_at (loc, "parentheses around %qE cannot be used to "
7395 : "form a pointer-to-member-function",
7396 : xarg.get_value ());
7397 : else
7398 0 : return error_mark_node;
7399 0 : PTRMEM_OK_P (xarg) = 1;
7400 : }
7401 : }
7402 :
7403 4235127 : if (TREE_CODE (xarg) == OFFSET_REF)
7404 : {
7405 63927 : ptrmem = PTRMEM_OK_P (xarg);
7406 :
7407 0 : if (!ptrmem && !flag_ms_extensions
7408 63927 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
7409 : {
7410 : /* A single non-static member, make sure we don't allow a
7411 : pointer-to-member. */
7412 0 : xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
7413 0 : TREE_OPERAND (xarg, 0),
7414 0 : ovl_make (TREE_OPERAND (xarg, 1)));
7415 0 : PTRMEM_OK_P (xarg) = ptrmem;
7416 : }
7417 : }
7418 :
7419 8470254 : exp = cp_build_addr_expr_strict (xarg, complain);
7420 :
7421 4235127 : if (TREE_CODE (exp) == PTRMEM_CST)
7422 62800 : PTRMEM_CST_LOCATION (exp) = loc;
7423 : else
7424 4172327 : protected_set_expr_location (exp, loc);
7425 : }
7426 :
7427 36510156 : if (processing_template_decl && exp != error_mark_node)
7428 : {
7429 15540786 : if (overload != NULL_TREE)
7430 195039 : return (build_min_non_dep_op_overload
7431 195039 : (code, exp, overload, orig_expr, integer_zero_node));
7432 :
7433 15345747 : exp = build_min_non_dep (code, exp, orig_expr,
7434 : /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
7435 : }
7436 36315117 : if (TREE_CODE (exp) == ADDR_EXPR)
7437 3406612 : PTRMEM_OK_P (exp) = ptrmem;
7438 : return exp;
7439 : }
7440 :
7441 : /* Construct and perhaps optimize a tree representation
7442 : for __builtin_addressof operation. ARG specifies the operand. */
7443 :
7444 : tree
7445 704707 : cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7446 : {
7447 704707 : tree orig_expr = arg;
7448 :
7449 704707 : if (processing_template_decl)
7450 : {
7451 186606 : if (type_dependent_expression_p (arg))
7452 186606 : return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7453 : }
7454 :
7455 1036202 : tree exp = cp_build_addr_expr_strict (arg, complain);
7456 :
7457 518101 : if (processing_template_decl && exp != error_mark_node)
7458 0 : exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
7459 : return exp;
7460 : }
7461 :
7462 : /* Like c_common_truthvalue_conversion, but handle pointer-to-member
7463 : constants, where a null value is represented by an INTEGER_CST of
7464 : -1. */
7465 :
7466 : tree
7467 8399744 : cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7468 : {
7469 8399744 : tree type = TREE_TYPE (expr);
7470 8399744 : location_t loc = cp_expr_loc_or_input_loc (expr);
7471 7093775 : if (TYPE_PTR_OR_PTRMEM_P (type)
7472 : /* Avoid ICE on invalid use of non-static member function. */
7473 15493231 : || TREE_CODE (expr) == FUNCTION_DECL)
7474 1306257 : return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
7475 : else
7476 7093487 : return c_common_truthvalue_conversion (loc, expr);
7477 : }
7478 :
7479 : /* Returns EXPR contextually converted to bool. */
7480 :
7481 : tree
7482 79606809 : contextual_conv_bool (tree expr, tsubst_flags_t complain)
7483 : {
7484 79606809 : return perform_implicit_conversion_flags (boolean_type_node, expr,
7485 79606809 : complain, LOOKUP_NORMAL);
7486 : }
7487 :
7488 : /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7489 : is a low-level function; most callers should use maybe_convert_cond. */
7490 :
7491 : tree
7492 54240452 : condition_conversion (tree expr)
7493 : {
7494 54240452 : tree t = contextual_conv_bool (expr, tf_warning_or_error);
7495 54240452 : if (!processing_template_decl)
7496 26967687 : t = fold_build_cleanup_point_expr (boolean_type_node, t);
7497 54240452 : return t;
7498 : }
7499 :
7500 : /* Returns the address of T. This function will fold away
7501 : ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7502 : most places should use cp_build_addr_expr instead. */
7503 :
7504 : tree
7505 358446969 : build_address (tree t)
7506 : {
7507 358446969 : if (error_operand_p (t) || !cxx_mark_addressable (t))
7508 24 : return error_mark_node;
7509 358446945 : gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7510 : || processing_template_decl);
7511 358446945 : t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7512 358446945 : if (TREE_CODE (t) != ADDR_EXPR)
7513 62347055 : t = rvalue (t);
7514 : return t;
7515 : }
7516 :
7517 : /* Return a NOP_EXPR converting EXPR to TYPE. */
7518 :
7519 : tree
7520 592897959 : build_nop (tree type, tree expr MEM_STAT_DECL)
7521 : {
7522 592897959 : if (type == error_mark_node || error_operand_p (expr))
7523 : return expr;
7524 592897885 : return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr PASS_MEM_STAT);
7525 : }
7526 :
7527 : /* Take the address of ARG, whatever that means under C++ semantics.
7528 : If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7529 : and class rvalues as well.
7530 :
7531 : Nothing should call this function directly; instead, callers should use
7532 : cp_build_addr_expr or cp_build_addr_expr_strict. */
7533 :
7534 : static tree
7535 284146980 : cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7536 : {
7537 284146980 : tree argtype;
7538 284146980 : tree val;
7539 :
7540 284146980 : if (!arg || error_operand_p (arg))
7541 12 : return error_mark_node;
7542 :
7543 284146968 : arg = mark_lvalue_use (arg);
7544 284146968 : if (error_operand_p (arg))
7545 3 : return error_mark_node;
7546 :
7547 284146965 : argtype = lvalue_type (arg);
7548 284146965 : location_t loc = cp_expr_loc_or_input_loc (arg);
7549 :
7550 284146965 : gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7551 :
7552 16208041 : if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7553 284147193 : && !really_overloaded_fn (arg))
7554 : {
7555 : /* They're trying to take the address of a unique non-static
7556 : member function. This is ill-formed (except in MS-land),
7557 : but let's try to DTRT.
7558 : Note: We only handle unique functions here because we don't
7559 : want to complain if there's a static overload; non-unique
7560 : cases will be handled by instantiate_type. But we need to
7561 : handle this case here to allow casts on the resulting PMF.
7562 : We could defer this in non-MS mode, but it's easier to give
7563 : a useful error here. */
7564 :
7565 : /* Inside constant member functions, the `this' pointer
7566 : contains an extra const qualifier. TYPE_MAIN_VARIANT
7567 : is used here to remove this const from the diagnostics
7568 : and the created OFFSET_REF. */
7569 87 : tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7570 87 : tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7571 87 : if (!mark_used (fn, complain) && !(complain & tf_error))
7572 0 : return error_mark_node;
7573 : /* Until microsoft headers are known to incorrectly take the address of
7574 : unqualified xobj member functions we should not support this
7575 : extension.
7576 : See comment in class.cc:resolve_address_of_overloaded_function for
7577 : the extended reasoning. */
7578 87 : if (!flag_ms_extensions || DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7579 : {
7580 81 : auto_diagnostic_group d;
7581 81 : tree name = DECL_NAME (fn);
7582 81 : if (!(complain & tf_error))
7583 0 : return error_mark_node;
7584 81 : else if (current_class_type
7585 81 : && TREE_OPERAND (arg, 0) == current_class_ref)
7586 : /* An expression like &memfn. */
7587 36 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7588 21 : permerror (loc,
7589 : "ISO C++ forbids taking the address of an unqualified"
7590 : " or parenthesized non-static member function to form"
7591 : " a pointer to member function. Say %<&%T::%D%>",
7592 : base, name);
7593 : else
7594 15 : error_at (loc,
7595 : "ISO C++ forbids taking the address of an unqualified"
7596 : " or parenthesized non-static member function to form"
7597 : " a pointer to explicit object member function");
7598 : else
7599 45 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7600 9 : permerror (loc,
7601 : "ISO C++ forbids taking the address of a bound member"
7602 : " function to form a pointer to member function."
7603 : " Say %<&%T::%D%>",
7604 : base, name);
7605 : else
7606 36 : error_at (loc,
7607 : "ISO C++ forbids taking the address of a bound member"
7608 : " function to form a pointer to explicit object member"
7609 : " function");
7610 81 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7611 51 : inform (loc,
7612 : "a pointer to explicit object member function can only be "
7613 : "formed with %<&%T::%D%>", base, name);
7614 81 : }
7615 87 : arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7616 : }
7617 :
7618 : /* Uninstantiated types are all functions. Taking the
7619 : address of a function is a no-op, so just return the
7620 : argument. */
7621 284146965 : if (type_unknown_p (arg))
7622 3269 : return build1 (ADDR_EXPR, unknown_type_node, arg);
7623 :
7624 284143696 : if (TREE_CODE (arg) == OFFSET_REF)
7625 : /* We want a pointer to member; bypass all the code for actually taking
7626 : the address of something. */
7627 63017 : goto offset_ref;
7628 :
7629 : /* Anything not already handled and not a true memory reference
7630 : is an error. */
7631 284080679 : if (!FUNC_OR_METHOD_TYPE_P (argtype))
7632 : {
7633 149441149 : cp_lvalue_kind kind = lvalue_kind (arg);
7634 149441149 : if (kind == clk_none)
7635 : {
7636 25 : if (complain & tf_error)
7637 25 : lvalue_error (loc, lv_addressof);
7638 25 : return error_mark_node;
7639 : }
7640 149441124 : if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7641 : {
7642 39 : if (!(complain & tf_error))
7643 12 : return error_mark_node;
7644 : /* Make this a permerror because we used to accept it. */
7645 27 : permerror (loc, "taking address of rvalue");
7646 : }
7647 : }
7648 :
7649 284080642 : if (TYPE_REF_P (argtype))
7650 : {
7651 238 : tree type = build_pointer_type (TREE_TYPE (argtype));
7652 238 : arg = build1 (CONVERT_EXPR, type, arg);
7653 238 : return arg;
7654 : }
7655 284080404 : else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7656 : {
7657 : /* ARM $3.4 */
7658 : /* Apparently a lot of autoconf scripts for C++ packages do this,
7659 : so only complain if -Wpedantic. */
7660 9 : if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7661 9 : pedwarn (loc, OPT_Wpedantic,
7662 : "ISO C++ forbids taking address of function %<::main%>");
7663 0 : else if (flag_pedantic_errors)
7664 0 : return error_mark_node;
7665 : }
7666 :
7667 : /* Let &* cancel out to simplify resulting code. */
7668 284080404 : if (INDIRECT_REF_P (arg))
7669 : {
7670 102262552 : arg = TREE_OPERAND (arg, 0);
7671 102262552 : if (TYPE_REF_P (TREE_TYPE (arg)))
7672 : {
7673 62736205 : tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7674 62736205 : arg = build1 (CONVERT_EXPR, type, arg);
7675 : }
7676 : else
7677 : /* Don't let this be an lvalue. */
7678 39526347 : arg = rvalue (arg);
7679 102262552 : return arg;
7680 : }
7681 :
7682 : /* Handle complex lvalues (when permitted)
7683 : by reduction to simpler cases. */
7684 181817852 : val = unary_complex_lvalue (ADDR_EXPR, arg);
7685 181817852 : if (val != 0)
7686 : return val;
7687 :
7688 181317299 : switch (TREE_CODE (arg))
7689 : {
7690 0 : CASE_CONVERT:
7691 0 : case FLOAT_EXPR:
7692 0 : case FIX_TRUNC_EXPR:
7693 : /* We should have handled this above in the lvalue_kind check. */
7694 0 : gcc_unreachable ();
7695 81440 : break;
7696 :
7697 81440 : case BASELINK:
7698 81440 : arg = BASELINK_FUNCTIONS (arg);
7699 : /* Fall through. */
7700 :
7701 81440 : case OVERLOAD:
7702 81440 : arg = OVL_FIRST (arg);
7703 : break;
7704 :
7705 63017 : case OFFSET_REF:
7706 63017 : offset_ref:
7707 : /* Turn a reference to a non-static data member into a
7708 : pointer-to-member. */
7709 63017 : {
7710 63017 : tree type;
7711 63017 : tree t;
7712 :
7713 63017 : gcc_assert (PTRMEM_OK_P (arg));
7714 :
7715 63017 : t = TREE_OPERAND (arg, 1);
7716 63017 : if (TYPE_REF_P (TREE_TYPE (t)))
7717 : {
7718 15 : if (complain & tf_error)
7719 15 : error_at (loc,
7720 : "cannot create pointer to reference member %qD", t);
7721 15 : return error_mark_node;
7722 : }
7723 :
7724 : /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7725 63002 : if (TREE_CODE (t) == FUNCTION_DECL
7726 60369 : && !DECL_PURE_VIRTUAL_P (t)
7727 123348 : && !mark_used (t, complain) && !(complain & tf_error))
7728 3 : return error_mark_node;
7729 :
7730 : /* Pull out the function_decl for a single xobj member function, and
7731 : let the rest of this function handle it. This is similar to how
7732 : static member functions are handled in the BASELINK case above. */
7733 62999 : if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7734 : {
7735 : arg = t;
7736 : break;
7737 : }
7738 :
7739 62903 : type = build_ptrmem_type (context_for_name_lookup (t),
7740 62903 : TREE_TYPE (t));
7741 62903 : t = make_ptrmem_cst (type, t);
7742 62903 : return t;
7743 : }
7744 :
7745 : default:
7746 : break;
7747 : }
7748 :
7749 181317395 : if (argtype != error_mark_node)
7750 181317395 : argtype = build_pointer_type (argtype);
7751 :
7752 181317395 : if (bitfield_p (arg))
7753 : {
7754 33 : if (complain & tf_error)
7755 33 : error_at (loc, "attempt to take address of bit-field");
7756 33 : return error_mark_node;
7757 : }
7758 :
7759 : /* In a template, we are processing a non-dependent expression
7760 : so we can just form an ADDR_EXPR with the correct type. */
7761 181317362 : if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7762 : {
7763 165594527 : if (!mark_single_function (arg, complain))
7764 3 : return error_mark_node;
7765 165594524 : val = build_address (arg);
7766 165594524 : if (TREE_CODE (arg) == OFFSET_REF)
7767 0 : PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7768 : }
7769 15722835 : else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7770 : {
7771 27 : tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7772 :
7773 27 : if (TREE_CODE (fn) == OVERLOAD && OVL_SINGLE_P (fn))
7774 27 : fn = OVL_FIRST (fn);
7775 :
7776 : /* We can only get here with a single static member
7777 : function. */
7778 27 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7779 : && DECL_STATIC_FUNCTION_P (fn));
7780 27 : if (!mark_used (fn, complain) && !(complain & tf_error))
7781 0 : return error_mark_node;
7782 27 : val = build_address (fn);
7783 27 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7784 : /* Do not lose object's side effects. */
7785 12 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7786 12 : TREE_OPERAND (arg, 0), val);
7787 : }
7788 : else
7789 : {
7790 15722808 : tree object = TREE_OPERAND (arg, 0);
7791 15722808 : tree field = TREE_OPERAND (arg, 1);
7792 15722808 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7793 : (TREE_TYPE (object), decl_type_context (field)));
7794 15722808 : val = build_address (arg);
7795 : }
7796 :
7797 181317359 : if (TYPE_PTR_P (argtype)
7798 181317359 : && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7799 : {
7800 1120 : build_ptrmemfunc_type (argtype);
7801 1120 : val = build_ptrmemfunc (argtype, val, 0,
7802 : /*c_cast_p=*/false,
7803 : complain);
7804 : }
7805 :
7806 : /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7807 181317359 : if (TREE_CODE (val) == ADDR_EXPR
7808 181317359 : && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7809 128998720 : SET_EXPR_LOCATION (val, input_location);
7810 :
7811 : return val;
7812 : }
7813 :
7814 : /* Take the address of ARG if it has one, even if it's an rvalue. */
7815 :
7816 : tree
7817 279393752 : cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7818 : {
7819 279393752 : return cp_build_addr_expr_1 (arg, 0, complain);
7820 : }
7821 :
7822 : /* Take the address of ARG, but only if it's an lvalue. */
7823 :
7824 : static tree
7825 4753228 : cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7826 : {
7827 4753228 : return cp_build_addr_expr_1 (arg, 1, complain);
7828 : }
7829 :
7830 : /* C++: Must handle pointers to members.
7831 :
7832 : Perhaps type instantiation should be extended to handle conversion
7833 : from aggregates to types we don't yet know we want? (Or are those
7834 : cases typically errors which should be reported?)
7835 :
7836 : NOCONVERT suppresses the default promotions (such as from short to int). */
7837 :
7838 : tree
7839 31951166 : cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7840 : tsubst_flags_t complain)
7841 : {
7842 : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7843 31951166 : tree arg = xarg;
7844 31951166 : location_t location = cp_expr_loc_or_input_loc (arg);
7845 31951166 : tree argtype = 0;
7846 31951166 : tree eptype = NULL_TREE;
7847 31951166 : const char *errstring = NULL;
7848 31951166 : tree val;
7849 31951166 : const char *invalid_op_diag;
7850 :
7851 31951166 : if (!arg || error_operand_p (arg))
7852 0 : return error_mark_node;
7853 :
7854 31951166 : arg = resolve_nondeduced_context (arg, complain);
7855 :
7856 63902332 : if ((invalid_op_diag
7857 31951166 : = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7858 : ? CONVERT_EXPR
7859 : : code),
7860 31951166 : TREE_TYPE (arg))))
7861 : {
7862 0 : if (complain & tf_error)
7863 0 : error (invalid_op_diag);
7864 0 : return error_mark_node;
7865 : }
7866 :
7867 31951166 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7868 : {
7869 9318 : eptype = TREE_TYPE (arg);
7870 9318 : arg = TREE_OPERAND (arg, 0);
7871 : }
7872 :
7873 31951166 : switch (code)
7874 : {
7875 2447709 : case UNARY_PLUS_EXPR:
7876 2447709 : case NEGATE_EXPR:
7877 2447709 : {
7878 2447709 : int flags = WANT_ARITH | WANT_ENUM;
7879 : /* Unary plus (but not unary minus) is allowed on pointers. */
7880 2447709 : if (code == UNARY_PLUS_EXPR)
7881 164148 : flags |= WANT_POINTER;
7882 2447709 : arg = build_expr_type_conversion (flags, arg, true);
7883 2447709 : if (!arg)
7884 35 : errstring = (code == NEGATE_EXPR
7885 27 : ? _("wrong type argument to unary minus")
7886 8 : : _("wrong type argument to unary plus"));
7887 : else
7888 : {
7889 2447674 : if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7890 700583 : arg = cp_perform_integral_promotions (arg, complain);
7891 :
7892 : /* Make sure the result is not an lvalue: a unary plus or minus
7893 : expression is always a rvalue. */
7894 2447674 : arg = rvalue (arg);
7895 : }
7896 : }
7897 : break;
7898 :
7899 971379 : case BIT_NOT_EXPR:
7900 971379 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7901 : {
7902 15 : code = CONJ_EXPR;
7903 15 : if (!noconvert)
7904 : {
7905 15 : arg = cp_default_conversion (arg, complain);
7906 15 : if (arg == error_mark_node)
7907 : return error_mark_node;
7908 : }
7909 : }
7910 971364 : else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7911 : | WANT_VECTOR_OR_COMPLEX,
7912 : arg, true)))
7913 20 : errstring = _("wrong type argument to bit-complement");
7914 971344 : else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7915 : {
7916 : /* Warn if the expression has boolean value. */
7917 970843 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7918 970843 : && (complain & tf_warning))
7919 : {
7920 61 : auto_diagnostic_group d;
7921 61 : if (warning_at (location, OPT_Wbool_operation,
7922 : "%<~%> on an expression of type %<bool%>"))
7923 42 : inform (location, "did you mean to use logical not (%<!%>)?");
7924 61 : }
7925 970843 : arg = cp_perform_integral_promotions (arg, complain);
7926 : }
7927 501 : else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7928 501 : arg = mark_rvalue_use (arg);
7929 : break;
7930 :
7931 0 : case ABS_EXPR:
7932 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7933 0 : errstring = _("wrong type argument to abs");
7934 0 : else if (!noconvert)
7935 : {
7936 0 : arg = cp_default_conversion (arg, complain);
7937 0 : if (arg == error_mark_node)
7938 : return error_mark_node;
7939 : }
7940 : break;
7941 :
7942 0 : case CONJ_EXPR:
7943 : /* Conjugating a real value is a no-op, but allow it anyway. */
7944 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7945 0 : errstring = _("wrong type argument to conjugation");
7946 0 : else if (!noconvert)
7947 : {
7948 0 : arg = cp_default_conversion (arg, complain);
7949 0 : if (arg == error_mark_node)
7950 : return error_mark_node;
7951 : }
7952 : break;
7953 :
7954 18713527 : case TRUTH_NOT_EXPR:
7955 18713527 : if (gnu_vector_type_p (TREE_TYPE (arg)))
7956 60 : return cp_build_binary_op (input_location, EQ_EXPR, arg,
7957 60 : build_zero_cst (TREE_TYPE (arg)), complain);
7958 18713467 : arg = contextual_conv_bool (arg, complain);
7959 18713467 : if (arg != error_mark_node)
7960 : {
7961 18713457 : if (processing_template_decl)
7962 8753578 : return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
7963 9959879 : val = invert_truthvalue_loc (location, arg);
7964 9959879 : if (obvalue_p (val))
7965 12306 : val = non_lvalue_loc (location, val);
7966 9959879 : return val;
7967 : }
7968 10 : errstring = _("in argument to unary !");
7969 10 : break;
7970 :
7971 : case NOP_EXPR:
7972 : break;
7973 :
7974 454922 : case REALPART_EXPR:
7975 454922 : case IMAGPART_EXPR:
7976 454922 : val = build_real_imag_expr (input_location, code, arg);
7977 454922 : if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
7978 0 : val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
7979 0 : TREE_TYPE (eptype), val);
7980 : return val;
7981 :
7982 8771979 : case PREINCREMENT_EXPR:
7983 8771979 : case POSTINCREMENT_EXPR:
7984 8771979 : case PREDECREMENT_EXPR:
7985 8771979 : case POSTDECREMENT_EXPR:
7986 : /* Handle complex lvalues (when permitted)
7987 : by reduction to simpler cases. */
7988 :
7989 8771979 : val = unary_complex_lvalue (code, arg);
7990 8771979 : if (val != 0)
7991 38 : goto return_build_unary_op;
7992 :
7993 8771941 : tree stripped_arg;
7994 8771941 : stripped_arg = tree_strip_any_location_wrapper (arg);
7995 2203300 : if ((VAR_P (stripped_arg) || TREE_CODE (stripped_arg) == PARM_DECL)
7996 8125221 : && !DECL_READ_P (stripped_arg)
7997 9918464 : && (VAR_P (stripped_arg) ? warn_unused_but_set_variable
7998 : : warn_unused_but_set_parameter) > 1)
7999 : {
8000 9809 : arg = mark_lvalue_use (arg);
8001 9809 : DECL_READ_P (stripped_arg) = 0;
8002 : }
8003 : else
8004 8762132 : arg = mark_lvalue_use (arg);
8005 :
8006 : /* Increment or decrement the real part of the value,
8007 : and don't change the imaginary part. */
8008 8771941 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
8009 : {
8010 18 : tree real, imag;
8011 :
8012 18 : arg = cp_stabilize_reference (arg);
8013 18 : real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
8014 18 : imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
8015 18 : real = cp_build_unary_op (code, real, true, complain);
8016 18 : if (real == error_mark_node || imag == error_mark_node)
8017 : return error_mark_node;
8018 15 : val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
8019 15 : goto return_build_unary_op;
8020 : }
8021 :
8022 : /* Report invalid types. */
8023 :
8024 8771923 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
8025 : arg, true)))
8026 : {
8027 36 : if (code == PREINCREMENT_EXPR)
8028 9 : errstring = _("no pre-increment operator for type");
8029 27 : else if (code == POSTINCREMENT_EXPR)
8030 9 : errstring = _("no post-increment operator for type");
8031 18 : else if (code == PREDECREMENT_EXPR)
8032 9 : errstring = _("no pre-decrement operator for type");
8033 : else
8034 9 : errstring = _("no post-decrement operator for type");
8035 : break;
8036 : }
8037 8771887 : else if (arg == error_mark_node)
8038 : return error_mark_node;
8039 :
8040 : /* Report something read-only. */
8041 :
8042 8771884 : if (CP_TYPE_CONST_P (TREE_TYPE (arg))
8043 8771884 : || TREE_READONLY (arg))
8044 : {
8045 164 : if (complain & tf_error)
8046 164 : cxx_readonly_error (location, arg,
8047 164 : ((code == PREINCREMENT_EXPR
8048 164 : || code == POSTINCREMENT_EXPR)
8049 : ? lv_increment : lv_decrement));
8050 : else
8051 0 : return error_mark_node;
8052 : }
8053 :
8054 8771884 : {
8055 8771884 : tree inc;
8056 8771884 : tree declared_type = unlowered_expr_type (arg);
8057 :
8058 8771884 : argtype = TREE_TYPE (arg);
8059 :
8060 : /* ARM $5.2.5 last annotation says this should be forbidden. */
8061 8771884 : if (TREE_CODE (argtype) == ENUMERAL_TYPE)
8062 : {
8063 0 : if (complain & tf_error)
8064 0 : permerror (location, (code == PREINCREMENT_EXPR
8065 0 : || code == POSTINCREMENT_EXPR)
8066 : ? G_("ISO C++ forbids incrementing an enum")
8067 : : G_("ISO C++ forbids decrementing an enum"));
8068 : else
8069 0 : return error_mark_node;
8070 : }
8071 :
8072 : /* Compute the increment. */
8073 :
8074 8771884 : if (TYPE_PTR_P (argtype))
8075 : {
8076 2811108 : tree type = complete_type (TREE_TYPE (argtype));
8077 :
8078 2811108 : if (!COMPLETE_OR_VOID_TYPE_P (type))
8079 : {
8080 10 : if (complain & tf_error)
8081 9 : error_at (location, ((code == PREINCREMENT_EXPR
8082 9 : || code == POSTINCREMENT_EXPR))
8083 : ? G_("cannot increment a pointer to incomplete "
8084 : "type %qT")
8085 : : G_("cannot decrement a pointer to incomplete "
8086 : "type %qT"),
8087 9 : TREE_TYPE (argtype));
8088 : else
8089 1 : return error_mark_node;
8090 : }
8091 2811098 : else if (!TYPE_PTROB_P (argtype))
8092 : {
8093 69 : if (complain & tf_error)
8094 63 : pedwarn (location, OPT_Wpointer_arith,
8095 63 : (code == PREINCREMENT_EXPR
8096 63 : || code == POSTINCREMENT_EXPR)
8097 : ? G_("ISO C++ forbids incrementing a pointer "
8098 : "of type %qT")
8099 : : G_("ISO C++ forbids decrementing a pointer "
8100 : "of type %qT"),
8101 : argtype);
8102 : else
8103 6 : return error_mark_node;
8104 : }
8105 2811029 : else if (!verify_type_context (location, TCTX_POINTER_ARITH,
8106 2811029 : TREE_TYPE (argtype),
8107 : !(complain & tf_error)))
8108 0 : return error_mark_node;
8109 :
8110 2811101 : inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
8111 : }
8112 : else
8113 5960633 : inc = VECTOR_TYPE_P (argtype)
8114 5960776 : ? build_one_cst (argtype)
8115 : : integer_one_node;
8116 :
8117 8771877 : inc = cp_convert (argtype, inc, complain);
8118 :
8119 : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
8120 : need to ask Objective-C to build the increment or decrement
8121 : expression for it. */
8122 8771877 : if (objc_is_property_ref (arg))
8123 0 : return objc_build_incr_expr_for_property_ref (input_location, code,
8124 0 : arg, inc);
8125 :
8126 : /* Complain about anything else that is not a true lvalue. */
8127 8771877 : if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
8128 8771877 : || code == POSTINCREMENT_EXPR)
8129 : ? lv_increment : lv_decrement),
8130 : complain))
8131 38 : return error_mark_node;
8132 :
8133 : /* [depr.volatile.type] "Postfix ++ and -- expressions and
8134 : prefix ++ and -- expressions of volatile-qualified arithmetic
8135 : and pointer types are deprecated." */
8136 8771554 : if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
8137 8771839 : && (complain & tf_warning))
8138 398 : warning_at (location, OPT_Wvolatile,
8139 : "%qs expression of %<volatile%>-qualified type is "
8140 : "deprecated",
8141 : ((code == PREINCREMENT_EXPR
8142 : || code == POSTINCREMENT_EXPR)
8143 : ? "++" : "--"));
8144 :
8145 : /* Forbid using -- or ++ in C++17 on `bool'. */
8146 8771839 : if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
8147 : {
8148 95 : if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
8149 : {
8150 33 : if (complain & tf_error)
8151 33 : error_at (location,
8152 : "use of an operand of type %qT in %<operator--%> "
8153 : "is forbidden", boolean_type_node);
8154 33 : return error_mark_node;
8155 : }
8156 : else
8157 : {
8158 62 : if (cxx_dialect >= cxx17)
8159 : {
8160 33 : if (complain & tf_error)
8161 32 : error_at (location,
8162 : "use of an operand of type %qT in "
8163 : "%<operator++%> is forbidden in C++17",
8164 : boolean_type_node);
8165 33 : return error_mark_node;
8166 : }
8167 : /* Otherwise, [depr.incr.bool] says this is deprecated. */
8168 29 : else if (complain & tf_warning)
8169 25 : warning_at (location, OPT_Wdeprecated,
8170 : "use of an operand of type %qT "
8171 : "in %<operator++%> is deprecated",
8172 : boolean_type_node);
8173 : }
8174 29 : val = boolean_increment (code, arg);
8175 : }
8176 8771744 : else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
8177 : /* An rvalue has no cv-qualifiers. */
8178 1422363 : val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
8179 : else
8180 7349381 : val = build2 (code, TREE_TYPE (arg), arg, inc);
8181 :
8182 8771773 : TREE_SIDE_EFFECTS (val) = 1;
8183 8771773 : goto return_build_unary_op;
8184 : }
8185 :
8186 591650 : case ADDR_EXPR:
8187 : /* Note that this operation never does default_conversion
8188 : regardless of NOCONVERT. */
8189 591650 : return cp_build_addr_expr (arg, complain);
8190 :
8191 : default:
8192 : break;
8193 : }
8194 :
8195 3419119 : if (!errstring)
8196 : {
8197 3419033 : if (argtype == 0)
8198 3419033 : argtype = TREE_TYPE (arg);
8199 3419033 : val = build1 (code, argtype, arg);
8200 12190859 : return_build_unary_op:
8201 12190859 : if (eptype)
8202 9309 : val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
8203 12190859 : return val;
8204 : }
8205 :
8206 101 : if (complain & tf_error)
8207 54 : error_at (location, "%s", errstring);
8208 101 : return error_mark_node;
8209 : }
8210 :
8211 : /* Hook for the c-common bits that build a unary op. */
8212 : tree
8213 4139 : build_unary_op (location_t /*location*/,
8214 : enum tree_code code, tree xarg, bool noconvert)
8215 : {
8216 4139 : return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
8217 : }
8218 :
8219 : /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
8220 : so that it is a valid lvalue even for GENERIC by replacing
8221 : (lhs = rhs) with ((lhs = rhs), lhs)
8222 : (--lhs) with ((--lhs), lhs)
8223 : (++lhs) with ((++lhs), lhs)
8224 : and if lhs has side-effects, calling cp_stabilize_reference on it, so
8225 : that it can be evaluated multiple times. */
8226 :
8227 : tree
8228 411907 : genericize_compound_lvalue (tree lvalue)
8229 : {
8230 411907 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
8231 199 : lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
8232 199 : cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
8233 199 : TREE_OPERAND (lvalue, 1));
8234 411907 : return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
8235 411907 : lvalue, TREE_OPERAND (lvalue, 0));
8236 : }
8237 :
8238 : /* Apply unary lvalue-demanding operator CODE to the expression ARG
8239 : for certain kinds of expressions which are not really lvalues
8240 : but which we can accept as lvalues.
8241 :
8242 : If ARG is not a kind of expression we can handle, return
8243 : NULL_TREE. */
8244 :
8245 : tree
8246 323791326 : unary_complex_lvalue (enum tree_code code, tree arg)
8247 : {
8248 : /* Inside a template, making these kinds of adjustments is
8249 : pointless; we are only concerned with the type of the
8250 : expression. */
8251 324203003 : if (processing_template_decl)
8252 : return NULL_TREE;
8253 :
8254 : /* Handle (a, b) used as an "lvalue". */
8255 232815061 : if (TREE_CODE (arg) == COMPOUND_EXPR)
8256 : {
8257 413346 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
8258 : tf_warning_or_error);
8259 413346 : return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
8260 826692 : TREE_OPERAND (arg, 0), real_result);
8261 : }
8262 :
8263 : /* Handle (a ? b : c) used as an "lvalue". */
8264 232401715 : if (TREE_CODE (arg) == COND_EXPR
8265 232314452 : || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
8266 87263 : return rationalize_conditional_expr (code, arg, tf_warning_or_error);
8267 :
8268 : /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
8269 232314452 : if (TREE_CODE (arg) == MODIFY_EXPR
8270 231902901 : || TREE_CODE (arg) == PREINCREMENT_EXPR
8271 231902838 : || TREE_CODE (arg) == PREDECREMENT_EXPR)
8272 411677 : return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
8273 :
8274 231902775 : if (code != ADDR_EXPR)
8275 : return NULL_TREE;
8276 :
8277 : /* Handle (a = b) used as an "lvalue" for `&'. */
8278 227927668 : if (TREE_CODE (arg) == MODIFY_EXPR
8279 227927668 : || TREE_CODE (arg) == INIT_EXPR)
8280 : {
8281 0 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
8282 : tf_warning_or_error);
8283 0 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
8284 : arg, real_result);
8285 0 : suppress_warning (arg /* What warning? */);
8286 0 : return arg;
8287 : }
8288 :
8289 349198828 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
8290 349197729 : || TREE_CODE (arg) == OFFSET_REF)
8291 : return NULL_TREE;
8292 :
8293 : /* We permit compiler to make function calls returning
8294 : objects of aggregate type look like lvalues. */
8295 121270061 : {
8296 121270061 : tree targ = arg;
8297 :
8298 121270061 : if (TREE_CODE (targ) == SAVE_EXPR)
8299 0 : targ = TREE_OPERAND (targ, 0);
8300 :
8301 121270061 : if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
8302 : {
8303 26 : if (TREE_CODE (arg) == SAVE_EXPR)
8304 : targ = arg;
8305 : else
8306 26 : targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
8307 26 : return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
8308 : }
8309 :
8310 121270035 : if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
8311 0 : return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
8312 0 : TREE_OPERAND (targ, 0), current_function_decl, NULL);
8313 : }
8314 :
8315 : /* Don't let anything else be handled specially. */
8316 : return NULL_TREE;
8317 : }
8318 :
8319 : /* Mark EXP saying that we need to be able to take the
8320 : address of it; it should not be allocated in a register.
8321 : Value is true if successful. ARRAY_REF_P is true if this
8322 : is for ARRAY_REF construction - in that case we don't want
8323 : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
8324 : it is fine to use ARRAY_REFs for vector subscripts on vector
8325 : register variables.
8326 :
8327 : C++: we do not allow `current_class_ptr' to be addressable. */
8328 :
8329 : bool
8330 378700441 : cxx_mark_addressable (tree exp, bool array_ref_p)
8331 : {
8332 378700441 : tree x = exp;
8333 :
8334 442692942 : while (1)
8335 442692942 : switch (TREE_CODE (x))
8336 : {
8337 31969685 : case VIEW_CONVERT_EXPR:
8338 31969685 : if (array_ref_p
8339 811731 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8340 32585483 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
8341 : return true;
8342 31966418 : x = TREE_OPERAND (x, 0);
8343 31966418 : break;
8344 :
8345 31556024 : case COMPONENT_REF:
8346 31556024 : if (bitfield_p (x))
8347 9 : error ("attempt to take address of bit-field");
8348 : /* FALLTHRU */
8349 32026083 : case ADDR_EXPR:
8350 32026083 : case ARRAY_REF:
8351 32026083 : case REALPART_EXPR:
8352 32026083 : case IMAGPART_EXPR:
8353 32026083 : x = TREE_OPERAND (x, 0);
8354 32026083 : break;
8355 :
8356 8371932 : case PARM_DECL:
8357 8371932 : if (x == current_class_ptr)
8358 : {
8359 18 : error ("cannot take the address of %<this%>, which is an rvalue expression");
8360 18 : TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
8361 18 : return true;
8362 : }
8363 : /* Fall through. */
8364 :
8365 75914173 : case VAR_DECL:
8366 : /* Caller should not be trying to mark initialized
8367 : constant fields addressable. */
8368 75914173 : gcc_assert (DECL_LANG_SPECIFIC (x) == 0
8369 : || DECL_IN_AGGR_P (x) == 0
8370 : || TREE_STATIC (x)
8371 : || DECL_EXTERNAL (x));
8372 75914173 : if (VAR_P (x)
8373 67542259 : && DECL_ANON_UNION_VAR_P (x)
8374 75914256 : && !TREE_ADDRESSABLE (x))
8375 39 : cxx_mark_addressable (DECL_VALUE_EXPR (x));
8376 : /* Fall through. */
8377 :
8378 76122693 : case RESULT_DECL:
8379 76122823 : if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
8380 76122814 : && !DECL_ARTIFICIAL (x))
8381 : {
8382 31 : if (VAR_P (x) && DECL_HARD_REGISTER (x))
8383 : {
8384 15 : error
8385 15 : ("address of explicit register variable %qD requested", x);
8386 15 : return false;
8387 : }
8388 16 : else if (extra_warnings)
8389 3 : warning
8390 3 : (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
8391 : }
8392 76122678 : TREE_ADDRESSABLE (x) = 1;
8393 76122678 : return true;
8394 :
8395 186440320 : case CONST_DECL:
8396 186440320 : case FUNCTION_DECL:
8397 186440320 : TREE_ADDRESSABLE (x) = 1;
8398 186440320 : return true;
8399 :
8400 56834 : case CONSTRUCTOR:
8401 56834 : TREE_ADDRESSABLE (x) = 1;
8402 56834 : return true;
8403 :
8404 17483148 : case TARGET_EXPR:
8405 17483148 : TREE_ADDRESSABLE (x) = 1;
8406 17483148 : cxx_mark_addressable (TARGET_EXPR_SLOT (x));
8407 17483148 : return true;
8408 :
8409 : default:
8410 : return true;
8411 : }
8412 : }
8413 :
8414 : /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
8415 :
8416 : tree
8417 9894653 : build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
8418 : tsubst_flags_t complain)
8419 : {
8420 9894653 : tree orig_ifexp = ifexp;
8421 9894653 : tree orig_op1 = op1;
8422 9894653 : tree orig_op2 = op2;
8423 9894653 : tree expr;
8424 :
8425 9894653 : if (processing_template_decl)
8426 : {
8427 : /* The standard says that the expression is type-dependent if
8428 : IFEXP is type-dependent, even though the eventual type of the
8429 : expression doesn't dependent on IFEXP. */
8430 3531971 : if (type_dependent_expression_p (ifexp)
8431 : /* As a GNU extension, the middle operand may be omitted. */
8432 1936566 : || (op1 && type_dependent_expression_p (op1))
8433 4945686 : || type_dependent_expression_p (op2))
8434 2147888 : return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
8435 : }
8436 :
8437 7746765 : expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
8438 7746765 : if (processing_template_decl && expr != error_mark_node)
8439 : {
8440 1384077 : tree min = build_min_non_dep (COND_EXPR, expr,
8441 : orig_ifexp, orig_op1, orig_op2);
8442 1384077 : expr = convert_from_reference (min);
8443 : }
8444 : return expr;
8445 : }
8446 :
8447 : /* Given a list of expressions, return a compound expression
8448 : that performs them all and returns the value of the last of them. */
8449 :
8450 : tree
8451 32640044 : build_x_compound_expr_from_list (tree list, expr_list_kind exp,
8452 : tsubst_flags_t complain)
8453 : {
8454 32640044 : tree expr = TREE_VALUE (list);
8455 :
8456 490715 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8457 33130747 : && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
8458 : {
8459 14 : if (complain & tf_error)
8460 28 : pedwarn (cp_expr_loc_or_input_loc (expr), 0,
8461 : "list-initializer for non-class type must not "
8462 : "be parenthesized");
8463 : else
8464 0 : return error_mark_node;
8465 : }
8466 :
8467 32640044 : if (TREE_CHAIN (list))
8468 : {
8469 23 : if (complain & tf_error)
8470 14 : switch (exp)
8471 : {
8472 12 : case ELK_INIT:
8473 12 : permerror (input_location, "expression list treated as compound "
8474 : "expression in initializer");
8475 12 : break;
8476 1 : case ELK_MEM_INIT:
8477 1 : permerror (input_location, "expression list treated as compound "
8478 : "expression in mem-initializer");
8479 1 : break;
8480 1 : case ELK_FUNC_CAST:
8481 1 : permerror (input_location, "expression list treated as compound "
8482 : "expression in functional cast");
8483 1 : break;
8484 0 : default:
8485 0 : gcc_unreachable ();
8486 : }
8487 : else
8488 9 : return error_mark_node;
8489 :
8490 28 : for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
8491 28 : expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
8492 14 : expr, TREE_VALUE (list), NULL_TREE,
8493 : complain);
8494 : }
8495 :
8496 : return expr;
8497 : }
8498 :
8499 : /* Like build_x_compound_expr_from_list, but using a VEC. */
8500 :
8501 : tree
8502 419360 : build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8503 : tsubst_flags_t complain)
8504 : {
8505 419360 : if (vec_safe_is_empty (vec))
8506 : return NULL_TREE;
8507 419360 : else if (vec->length () == 1)
8508 419296 : return (*vec)[0];
8509 : else
8510 : {
8511 64 : tree expr;
8512 64 : unsigned int ix;
8513 64 : tree t;
8514 :
8515 64 : if (msg != NULL)
8516 : {
8517 4 : if (complain & tf_error)
8518 0 : permerror (input_location,
8519 : "%s expression list treated as compound expression",
8520 : msg);
8521 : else
8522 4 : return error_mark_node;
8523 : }
8524 :
8525 60 : expr = (*vec)[0];
8526 134 : for (ix = 1; vec->iterate (ix, &t); ++ix)
8527 74 : expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
8528 : t, NULL_TREE, complain);
8529 :
8530 : return expr;
8531 : }
8532 : }
8533 :
8534 : /* Handle overloading of the ',' operator when needed. */
8535 :
8536 : tree
8537 3195933 : build_x_compound_expr (location_t loc, tree op1, tree op2,
8538 : tree lookups, tsubst_flags_t complain)
8539 : {
8540 3195933 : tree result;
8541 3195933 : tree orig_op1 = op1;
8542 3195933 : tree orig_op2 = op2;
8543 3195933 : tree overload = NULL_TREE;
8544 :
8545 3195933 : if (processing_template_decl)
8546 : {
8547 2937199 : if (type_dependent_expression_p (op1)
8548 2937199 : || type_dependent_expression_p (op2))
8549 : {
8550 2664178 : result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8551 2664178 : TREE_TYPE (result)
8552 2664178 : = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
8553 2664178 : return result;
8554 : }
8555 : }
8556 :
8557 531755 : result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8558 : NULL_TREE, lookups, &overload, complain);
8559 531755 : if (!result)
8560 523134 : result = cp_build_compound_expr (op1, op2, complain);
8561 :
8562 531755 : if (processing_template_decl && result != error_mark_node)
8563 : {
8564 271200 : if (overload != NULL_TREE)
8565 72 : return (build_min_non_dep_op_overload
8566 72 : (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
8567 :
8568 271128 : return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
8569 : }
8570 :
8571 : return result;
8572 : }
8573 :
8574 : /* Like cp_build_compound_expr, but for the c-common bits. */
8575 :
8576 : tree
8577 21303 : build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8578 : {
8579 21303 : return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8580 : }
8581 :
8582 : /* Build a compound expression. */
8583 :
8584 : tree
8585 687347 : cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8586 : {
8587 687347 : lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8588 :
8589 687347 : if (lhs == error_mark_node || rhs == error_mark_node)
8590 : return error_mark_node;
8591 :
8592 687344 : if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8593 0 : lhs = TREE_OPERAND (lhs, 0);
8594 687344 : tree eptype = NULL_TREE;
8595 687344 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8596 : {
8597 3 : eptype = TREE_TYPE (rhs);
8598 3 : rhs = TREE_OPERAND (rhs, 0);
8599 : }
8600 :
8601 687344 : if (TREE_CODE (rhs) == TARGET_EXPR)
8602 : {
8603 : /* If the rhs is a TARGET_EXPR, then build the compound
8604 : expression inside the target_expr's initializer. This
8605 : helps the compiler to eliminate unnecessary temporaries. */
8606 31786 : tree init = TARGET_EXPR_INITIAL (rhs);
8607 :
8608 31786 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8609 31786 : TARGET_EXPR_INITIAL (rhs) = init;
8610 :
8611 31786 : if (eptype)
8612 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8613 31786 : return rhs;
8614 : }
8615 :
8616 655558 : rhs = resolve_nondeduced_context (rhs, complain);
8617 :
8618 655558 : if (type_unknown_p (rhs))
8619 : {
8620 33 : if (complain & tf_error)
8621 51 : error_at (cp_expr_loc_or_input_loc (rhs),
8622 : "no context to resolve type of %qE", rhs);
8623 33 : return error_mark_node;
8624 : }
8625 :
8626 655525 : tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8627 655525 : if (eptype)
8628 3 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8629 : return ret;
8630 : }
8631 :
8632 : /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8633 : casts away constness. CAST gives the type of cast. Returns true
8634 : if the cast is ill-formed, false if it is well-formed.
8635 :
8636 : ??? This function warns for casting away any qualifier not just
8637 : const. We would like to specify exactly what qualifiers are casted
8638 : away.
8639 : */
8640 :
8641 : static bool
8642 878348 : check_for_casting_away_constness (location_t loc, tree src_type,
8643 : tree dest_type, enum tree_code cast,
8644 : tsubst_flags_t complain)
8645 : {
8646 : /* C-style casts are allowed to cast away constness. With
8647 : WARN_CAST_QUAL, we still want to issue a warning. */
8648 878348 : if (cast == CAST_EXPR && !warn_cast_qual)
8649 : return false;
8650 :
8651 664086 : if (!casts_away_constness (src_type, dest_type, complain))
8652 : return false;
8653 :
8654 400 : switch (cast)
8655 : {
8656 343 : case CAST_EXPR:
8657 343 : if (complain & tf_warning)
8658 343 : warning_at (loc, OPT_Wcast_qual,
8659 : "cast from type %qT to type %qT casts away qualifiers",
8660 : src_type, dest_type);
8661 : return false;
8662 :
8663 36 : case STATIC_CAST_EXPR:
8664 36 : if (complain & tf_error)
8665 24 : error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8666 : "away qualifiers",
8667 : src_type, dest_type);
8668 : return true;
8669 :
8670 21 : case REINTERPRET_CAST_EXPR:
8671 21 : if (complain & tf_error)
8672 15 : error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8673 : "casts away qualifiers",
8674 : src_type, dest_type);
8675 : return true;
8676 :
8677 0 : default:
8678 0 : gcc_unreachable();
8679 : }
8680 : }
8681 :
8682 : /* Warns if the cast from expression EXPR to type TYPE is useless. */
8683 : void
8684 51992260 : maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8685 : tsubst_flags_t complain)
8686 : {
8687 51992260 : if (warn_useless_cast
8688 152 : && complain & tf_warning)
8689 : {
8690 152 : if (TYPE_REF_P (type)
8691 152 : ? ((TYPE_REF_IS_RVALUE (type)
8692 86 : ? xvalue_p (expr) : lvalue_p (expr))
8693 172 : && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8694 : /* Don't warn when converting a class object to a non-reference type,
8695 : because that's a common way to create a temporary. */
8696 66 : : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8697 96 : warning_at (loc, OPT_Wuseless_cast,
8698 : "useless cast to type %q#T", type);
8699 : }
8700 51992260 : }
8701 :
8702 : /* Warns if the cast ignores cv-qualifiers on TYPE. */
8703 : static void
8704 51985295 : maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8705 : tsubst_flags_t complain)
8706 : {
8707 51985295 : if (warn_ignored_qualifiers
8708 274168 : && complain & tf_warning
8709 273676 : && !CLASS_TYPE_P (type)
8710 52253638 : && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8711 36 : warning_at (loc, OPT_Wignored_qualifiers,
8712 : "type qualifiers ignored on cast result type");
8713 51985295 : }
8714 :
8715 : /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8716 : (another pointer-to-member type in the same hierarchy) and return
8717 : the converted expression. If ALLOW_INVERSE_P is permitted, a
8718 : pointer-to-derived may be converted to pointer-to-base; otherwise,
8719 : only the other direction is permitted. If C_CAST_P is true, this
8720 : conversion is taking place as part of a C-style cast. */
8721 :
8722 : tree
8723 28992 : convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8724 : bool c_cast_p, tsubst_flags_t complain)
8725 : {
8726 28992 : if (same_type_p (type, TREE_TYPE (expr)))
8727 : return expr;
8728 :
8729 28992 : if (TYPE_PTRDATAMEM_P (type))
8730 : {
8731 398 : tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8732 398 : tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8733 398 : tree delta = (get_delta_difference
8734 398 : (obase, nbase,
8735 : allow_inverse_p, c_cast_p, complain));
8736 :
8737 398 : if (delta == error_mark_node)
8738 : return error_mark_node;
8739 :
8740 392 : if (!same_type_p (obase, nbase))
8741 : {
8742 299 : if (TREE_CODE (expr) == PTRMEM_CST)
8743 157 : expr = cplus_expand_constant (expr);
8744 :
8745 299 : tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8746 299 : build_int_cst (TREE_TYPE (expr), -1),
8747 : complain);
8748 299 : tree op1 = build_nop (ptrdiff_type_node, expr);
8749 299 : tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8750 : complain);
8751 :
8752 299 : expr = fold_build3_loc (input_location,
8753 : COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8754 : }
8755 :
8756 392 : return build_nop (type, expr);
8757 : }
8758 : else
8759 28594 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8760 28594 : allow_inverse_p, c_cast_p, complain);
8761 : }
8762 :
8763 : /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8764 : this static_cast is being attempted as one of the possible casts
8765 : allowed by a C-style cast. (In that case, accessibility of base
8766 : classes is not considered, and it is OK to cast away
8767 : constness.) Return the result of the cast. *VALID_P is set to
8768 : indicate whether or not the cast was valid. */
8769 :
8770 : static tree
8771 51442866 : build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8772 : bool *valid_p, tsubst_flags_t complain)
8773 : {
8774 51442866 : tree intype;
8775 51442866 : tree result;
8776 51442866 : cp_lvalue_kind clk;
8777 :
8778 : /* Assume the cast is valid. */
8779 51442866 : *valid_p = true;
8780 :
8781 51442866 : intype = unlowered_expr_type (expr);
8782 :
8783 : /* Save casted types in the function's used types hash table. */
8784 51442866 : used_types_insert (type);
8785 :
8786 : /* A prvalue of non-class type is cv-unqualified. */
8787 51442866 : if (!CLASS_TYPE_P (type))
8788 48235040 : type = cv_unqualified (type);
8789 :
8790 : /* [expr.static.cast]
8791 :
8792 : An lvalue of type "cv1 B", where B is a class type, can be cast
8793 : to type "reference to cv2 D", where D is a class derived (clause
8794 : _class.derived_) from B, if a valid standard conversion from
8795 : "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8796 : same cv-qualification as, or greater cv-qualification than, cv1,
8797 : and B is not a virtual base class of D. */
8798 : /* We check this case before checking the validity of "TYPE t =
8799 : EXPR;" below because for this case:
8800 :
8801 : struct B {};
8802 : struct D : public B { D(const B&); };
8803 : extern B& b;
8804 : void f() { static_cast<const D&>(b); }
8805 :
8806 : we want to avoid constructing a new D. The standard is not
8807 : completely clear about this issue, but our interpretation is
8808 : consistent with other compilers. */
8809 51442866 : if (TYPE_REF_P (type)
8810 5800927 : && CLASS_TYPE_P (TREE_TYPE (type))
8811 3793949 : && CLASS_TYPE_P (intype)
8812 3793932 : && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8813 3780823 : && DERIVED_FROM_P (intype, TREE_TYPE (type))
8814 3758518 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8815 3758518 : build_pointer_type (TYPE_MAIN_VARIANT
8816 : (TREE_TYPE (type))),
8817 : complain)
8818 55201384 : && (c_cast_p
8819 3758494 : || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8820 : {
8821 3758518 : tree base;
8822 :
8823 3758518 : if (processing_template_decl)
8824 : return expr;
8825 :
8826 : /* There is a standard conversion from "D*" to "B*" even if "B"
8827 : is ambiguous or inaccessible. If this is really a
8828 : static_cast, then we check both for inaccessibility and
8829 : ambiguity. However, if this is a static_cast being performed
8830 : because the user wrote a C-style cast, then accessibility is
8831 : not considered. */
8832 7037598 : base = lookup_base (TREE_TYPE (type), intype,
8833 : c_cast_p ? ba_unique : ba_check,
8834 : NULL, complain);
8835 3518811 : expr = cp_build_addr_expr (expr, complain);
8836 :
8837 3518811 : if (sanitize_flags_p (SANITIZE_VPTR))
8838 : {
8839 492 : tree ubsan_check
8840 492 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8841 : intype, expr);
8842 492 : if (ubsan_check)
8843 3518811 : expr = ubsan_check;
8844 : }
8845 :
8846 : /* Convert from "B*" to "D*". This function will check that "B"
8847 : is not a virtual base of "D". Even if we don't have a guarantee
8848 : that expr is NULL, if the static_cast is to a reference type,
8849 : it is UB if it would be NULL, so omit the non-NULL check. */
8850 3518811 : expr = build_base_path (MINUS_EXPR, expr, base,
8851 : /*nonnull=*/flag_delete_null_pointer_checks,
8852 : complain);
8853 :
8854 : /* Convert the pointer to a reference -- but then remember that
8855 : there are no expressions with reference type in C++.
8856 :
8857 : We call rvalue so that there's an actual tree code
8858 : (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8859 : is a variable with the same type, the conversion would get folded
8860 : away, leaving just the variable and causing lvalue_kind to give
8861 : the wrong answer. */
8862 3518811 : expr = cp_fold_convert (type, expr);
8863 :
8864 : /* When -fsanitize=null, make sure to diagnose reference binding to
8865 : NULL even when the reference is converted to pointer later on. */
8866 3518811 : if (sanitize_flags_p (SANITIZE_NULL)
8867 498 : && TREE_CODE (expr) == COND_EXPR
8868 6 : && TREE_OPERAND (expr, 2)
8869 6 : && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8870 3518817 : && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8871 6 : ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8872 :
8873 3518811 : return convert_from_reference (rvalue (expr));
8874 : }
8875 :
8876 : /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8877 : cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8878 47684348 : if (TYPE_REF_P (type)
8879 2042409 : && TYPE_REF_IS_RVALUE (type)
8880 862468 : && (clk = real_lvalue_p (expr))
8881 850467 : && reference_compatible_p (TREE_TYPE (type), intype)
8882 48534808 : && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8883 : {
8884 850460 : if (processing_template_decl)
8885 : return expr;
8886 849694 : if (clk == clk_ordinary)
8887 : {
8888 : /* Handle the (non-bit-field) lvalue case here by casting to
8889 : lvalue reference and then changing it to an rvalue reference.
8890 : Casting an xvalue to rvalue reference will be handled by the
8891 : main code path. */
8892 849691 : tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8893 849691 : result = (perform_direct_initialization_if_possible
8894 849691 : (lref, expr, c_cast_p, complain));
8895 849691 : result = build1 (NON_LVALUE_EXPR, type, result);
8896 849691 : return convert_from_reference (result);
8897 : }
8898 : else
8899 : /* For a bit-field or packed field, bind to a temporary. */
8900 3 : expr = rvalue (expr);
8901 : }
8902 :
8903 : /* Resolve overloaded address here rather than once in
8904 : implicit_conversion and again in the inverse code below. */
8905 46833891 : if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8906 : {
8907 21 : expr = instantiate_type (type, expr, complain);
8908 21 : intype = TREE_TYPE (expr);
8909 : }
8910 :
8911 : /* [expr.static.cast]
8912 :
8913 : Any expression can be explicitly converted to type cv void. */
8914 46833891 : if (VOID_TYPE_P (type))
8915 : {
8916 594256 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8917 0 : expr = TREE_OPERAND (expr, 0);
8918 594256 : return convert_to_void (expr, ICV_CAST, complain);
8919 : }
8920 :
8921 : /* [class.abstract]
8922 : An abstract class shall not be used ... as the type of an explicit
8923 : conversion. */
8924 46239635 : if (abstract_virtuals_error (ACU_CAST, type, complain))
8925 6 : return error_mark_node;
8926 :
8927 : /* [expr.static.cast]
8928 :
8929 : An expression e can be explicitly converted to a type T using a
8930 : static_cast of the form static_cast<T>(e) if the declaration T
8931 : t(e);" is well-formed, for some invented temporary variable
8932 : t. */
8933 46239629 : result = perform_direct_initialization_if_possible (type, expr,
8934 : c_cast_p, complain);
8935 : /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8936 : which initialize the first element of the aggregate. We need to handle
8937 : the array case specifically. */
8938 46239629 : if (result == NULL_TREE
8939 4698227 : && cxx_dialect >= cxx20
8940 3377969 : && TREE_CODE (type) == ARRAY_TYPE)
8941 : {
8942 : /* Create { EXPR } and perform direct-initialization from it. */
8943 15 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8944 15 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8945 15 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8946 15 : result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8947 : complain);
8948 : }
8949 4698227 : if (result)
8950 : {
8951 41541417 : if (processing_template_decl)
8952 : return expr;
8953 :
8954 40824691 : result = convert_from_reference (result);
8955 :
8956 : /* [expr.static.cast]
8957 :
8958 : If T is a reference type, the result is an lvalue; otherwise,
8959 : the result is an rvalue. */
8960 40824691 : if (!TYPE_REF_P (type))
8961 : {
8962 39632836 : result = rvalue (result);
8963 :
8964 39632836 : if (result == expr && SCALAR_TYPE_P (type))
8965 : /* Leave some record of the cast. */
8966 2436243 : result = build_nop (type, expr);
8967 : }
8968 40824691 : return result;
8969 : }
8970 :
8971 : /* [expr.static.cast]
8972 :
8973 : The inverse of any standard conversion sequence (clause _conv_),
8974 : other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8975 : (_conv.array_), function-to-pointer (_conv.func_), and boolean
8976 : (_conv.bool_) conversions, can be performed explicitly using
8977 : static_cast subject to the restriction that the explicit
8978 : conversion does not cast away constness (_expr.const.cast_), and
8979 : the following additional rules for specific cases: */
8980 : /* For reference, the conversions not excluded are: integral
8981 : promotions, floating-point promotion, integral conversions,
8982 : floating-point conversions, floating-integral conversions,
8983 : pointer conversions, and pointer to member conversions. */
8984 : /* DR 128
8985 :
8986 : A value of integral _or enumeration_ type can be explicitly
8987 : converted to an enumeration type. */
8988 : /* The effect of all that is that any conversion between any two
8989 : types which are integral, floating, or enumeration types can be
8990 : performed. */
8991 4698212 : if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8992 2564178 : || SCALAR_FLOAT_TYPE_P (type))
8993 2250622 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8994 218015 : || SCALAR_FLOAT_TYPE_P (intype)))
8995 : {
8996 2149089 : if (processing_template_decl)
8997 : return expr;
8998 2055667 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8999 0 : expr = TREE_OPERAND (expr, 0);
9000 : /* [expr.static.cast]: "If the value is not a bit-field, the result
9001 : refers to the object or the specified base class subobject thereof;
9002 : otherwise, the lvalue-to-rvalue conversion is applied to the
9003 : bit-field and the resulting prvalue is used as the operand of the
9004 : static_cast." There are no prvalue bit-fields; the l-to-r conversion
9005 : will give us an object of the underlying type of the bit-field. */
9006 2055667 : expr = decay_conversion (expr, complain);
9007 2055667 : return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
9008 : }
9009 :
9010 819353 : if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
9011 814862 : && CLASS_TYPE_P (TREE_TYPE (type))
9012 189715 : && CLASS_TYPE_P (TREE_TYPE (intype))
9013 2583975 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
9014 : (TREE_TYPE (intype))),
9015 34852 : build_pointer_type (TYPE_MAIN_VARIANT
9016 : (TREE_TYPE (type))),
9017 : complain))
9018 : {
9019 34513 : tree base;
9020 :
9021 34513 : if (processing_template_decl)
9022 : return expr;
9023 :
9024 34495 : if (!c_cast_p
9025 34495 : && check_for_casting_away_constness (loc, intype, type,
9026 : STATIC_CAST_EXPR,
9027 : complain))
9028 6 : return error_mark_node;
9029 68436 : base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
9030 : c_cast_p ? ba_unique : ba_check,
9031 : NULL, complain);
9032 34489 : expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
9033 : complain);
9034 :
9035 34489 : if (sanitize_flags_p (SANITIZE_VPTR))
9036 : {
9037 43 : tree ubsan_check
9038 43 : = cp_ubsan_maybe_instrument_downcast (loc, type,
9039 : intype, expr);
9040 43 : if (ubsan_check)
9041 34489 : expr = ubsan_check;
9042 : }
9043 :
9044 34489 : return cp_fold_convert (type, expr);
9045 : }
9046 :
9047 89 : if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
9048 2514610 : || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
9049 : {
9050 232 : tree c1;
9051 232 : tree c2;
9052 232 : tree t1;
9053 232 : tree t2;
9054 :
9055 232 : c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
9056 232 : c2 = TYPE_PTRMEM_CLASS_TYPE (type);
9057 :
9058 232 : if (TYPE_PTRDATAMEM_P (type))
9059 : {
9060 89 : t1 = (build_ptrmem_type
9061 89 : (c1,
9062 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
9063 89 : t2 = (build_ptrmem_type
9064 89 : (c2,
9065 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
9066 : }
9067 : else
9068 : {
9069 : t1 = intype;
9070 : t2 = type;
9071 : }
9072 232 : if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
9073 : {
9074 154 : if (!c_cast_p
9075 154 : && check_for_casting_away_constness (loc, intype, type,
9076 : STATIC_CAST_EXPR,
9077 : complain))
9078 9 : return error_mark_node;
9079 145 : if (processing_template_decl)
9080 : return expr;
9081 145 : return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
9082 145 : c_cast_p, complain);
9083 : }
9084 : }
9085 :
9086 : /* [expr.static.cast]
9087 :
9088 : An rvalue of type "pointer to cv void" can be explicitly
9089 : converted to a pointer to object type. A value of type pointer
9090 : to object converted to "pointer to cv void" and back to the
9091 : original pointer type will have its original value. */
9092 2514456 : if (TYPE_PTR_P (intype)
9093 879628 : && VOID_TYPE_P (TREE_TYPE (intype))
9094 3256708 : && TYPE_PTROB_P (type))
9095 : {
9096 709992 : if (!c_cast_p
9097 709992 : && check_for_casting_away_constness (loc, intype, type,
9098 : STATIC_CAST_EXPR,
9099 : complain))
9100 21 : return error_mark_node;
9101 709971 : if (processing_template_decl)
9102 : return expr;
9103 623467 : return build_nop (type, expr);
9104 : }
9105 :
9106 1804464 : *valid_p = false;
9107 1804464 : return error_mark_node;
9108 : }
9109 :
9110 : /* Return an expression representing static_cast<TYPE>(EXPR). */
9111 :
9112 : tree
9113 17707065 : build_static_cast (location_t loc, tree type, tree oexpr,
9114 : tsubst_flags_t complain)
9115 : {
9116 17707065 : tree expr = oexpr;
9117 17707065 : tree result;
9118 17707065 : bool valid_p;
9119 :
9120 17707065 : if (type == error_mark_node || expr == error_mark_node)
9121 : return error_mark_node;
9122 :
9123 17706809 : bool dependent = (dependent_type_p (type)
9124 17706809 : || type_dependent_expression_p (expr));
9125 12345906 : if (dependent)
9126 : {
9127 6498083 : tmpl:
9128 6498083 : expr = build_min (STATIC_CAST_EXPR, type, oexpr);
9129 : /* We don't know if it will or will not have side effects. */
9130 6498083 : TREE_SIDE_EFFECTS (expr) = 1;
9131 6498083 : result = convert_from_reference (expr);
9132 6498083 : protected_set_expr_location (result, loc);
9133 6498083 : return result;
9134 : }
9135 :
9136 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9137 : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9138 12345906 : if (!TYPE_REF_P (type)
9139 6546318 : && TREE_CODE (expr) == NOP_EXPR
9140 12457808 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9141 86 : expr = TREE_OPERAND (expr, 0);
9142 :
9143 12345906 : result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9144 : &valid_p, complain);
9145 12345906 : if (valid_p)
9146 : {
9147 12345616 : if (result != error_mark_node)
9148 : {
9149 12345511 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9150 12345511 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9151 : }
9152 12345616 : if (processing_template_decl)
9153 1137180 : goto tmpl;
9154 11208436 : protected_set_expr_location (result, loc);
9155 11208436 : return result;
9156 : }
9157 :
9158 290 : if (complain & tf_error)
9159 : {
9160 163 : auto_diagnostic_group d;
9161 163 : error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
9162 163 : TREE_TYPE (expr), type);
9163 163 : if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
9164 121 : && CLASS_TYPE_P (TREE_TYPE (type))
9165 192 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
9166 17 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
9167 17 : "class type %qT is incomplete", TREE_TYPE (type));
9168 163 : tree expr_type = TREE_TYPE (expr);
9169 163 : if (TYPE_PTR_P (expr_type))
9170 35 : expr_type = TREE_TYPE (expr_type);
9171 163 : if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
9172 12 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
9173 : "class type %qT is incomplete", expr_type);
9174 163 : }
9175 290 : return error_mark_node;
9176 : }
9177 :
9178 : /* EXPR is an expression with member function or pointer-to-member
9179 : function type. TYPE is a pointer type. Converting EXPR to TYPE is
9180 : not permitted by ISO C++, but we accept it in some modes. If we
9181 : are not in one of those modes, issue a diagnostic. Return the
9182 : converted expression. */
9183 :
9184 : tree
9185 165 : convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
9186 : {
9187 165 : tree intype;
9188 165 : tree decl;
9189 :
9190 165 : intype = TREE_TYPE (expr);
9191 165 : gcc_assert (TYPE_PTRMEMFUNC_P (intype)
9192 : || TREE_CODE (intype) == METHOD_TYPE);
9193 :
9194 165 : if (!(complain & tf_warning_or_error))
9195 0 : return error_mark_node;
9196 :
9197 165 : location_t loc = cp_expr_loc_or_input_loc (expr);
9198 :
9199 165 : if (pedantic || warn_pmf2ptr)
9200 207 : pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
9201 : "converting from %qH to %qI", intype, type);
9202 :
9203 165 : STRIP_ANY_LOCATION_WRAPPER (expr);
9204 :
9205 165 : if (TREE_CODE (intype) == METHOD_TYPE)
9206 72 : expr = build_addr_func (expr, complain);
9207 93 : else if (TREE_CODE (expr) == PTRMEM_CST)
9208 75 : expr = build_address (PTRMEM_CST_MEMBER (expr));
9209 : else
9210 : {
9211 18 : decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
9212 18 : decl = build_address (decl);
9213 18 : expr = get_member_function_from_ptrfunc (&decl, expr, complain);
9214 : }
9215 :
9216 165 : if (expr == error_mark_node)
9217 : return error_mark_node;
9218 :
9219 165 : expr = build_nop (type, expr);
9220 165 : SET_EXPR_LOCATION (expr, loc);
9221 165 : return expr;
9222 : }
9223 :
9224 : /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
9225 : constexpr evaluation knows to reject it. */
9226 :
9227 : static tree
9228 116297 : build_nop_reinterpret (tree type, tree expr)
9229 : {
9230 116297 : tree ret = build_nop (type, expr);
9231 116297 : if (ret != expr)
9232 116297 : REINTERPRET_CAST_P (ret) = true;
9233 116297 : return ret;
9234 : }
9235 :
9236 : /* Return a representation for a reinterpret_cast from EXPR to TYPE.
9237 : If C_CAST_P is true, this reinterpret cast is being done as part of
9238 : a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
9239 : indicate whether or not reinterpret_cast was valid. */
9240 :
9241 : static tree
9242 1959667 : build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
9243 : bool c_cast_p, bool *valid_p,
9244 : tsubst_flags_t complain)
9245 : {
9246 1959667 : tree intype;
9247 :
9248 : /* Assume the cast is invalid. */
9249 1959667 : if (valid_p)
9250 1804230 : *valid_p = true;
9251 :
9252 1959667 : if (type == error_mark_node || error_operand_p (expr))
9253 : return error_mark_node;
9254 :
9255 1959667 : intype = TREE_TYPE (expr);
9256 :
9257 : /* Save casted types in the function's used types hash table. */
9258 1959667 : used_types_insert (type);
9259 :
9260 : /* A prvalue of non-class type is cv-unqualified. */
9261 1959667 : if (!CLASS_TYPE_P (type))
9262 1959658 : type = cv_unqualified (type);
9263 :
9264 : /* [expr.reinterpret.cast]
9265 : A glvalue of type T1, designating an object x, can be cast to the type
9266 : "reference to T2" if an expression of type "pointer to T1" can be
9267 : explicitly converted to the type "pointer to T2" using a reinterpret_cast.
9268 : The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
9269 : of type "pointer to T1". No temporary is created, no copy is made, and no
9270 : constructors (11.4.4) or conversion functions (11.4.7) are called. */
9271 1959667 : if (TYPE_REF_P (type))
9272 : {
9273 9831 : if (!glvalue_p (expr))
9274 : {
9275 9 : if (complain & tf_error)
9276 9 : error_at (loc, "invalid cast of a prvalue expression of type "
9277 : "%qT to type %qT",
9278 : intype, type);
9279 9 : return error_mark_node;
9280 : }
9281 :
9282 : /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
9283 : "B" are related class types; the reinterpret_cast does not
9284 : adjust the pointer. */
9285 9822 : if (TYPE_PTR_P (intype)
9286 3 : && (complain & tf_warning)
9287 9825 : && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
9288 : COMPARE_BASE | COMPARE_DERIVED)))
9289 3 : warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
9290 : intype, type);
9291 :
9292 9822 : expr = cp_build_addr_expr (expr, complain);
9293 :
9294 9822 : if (warn_strict_aliasing > 2)
9295 72 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9296 :
9297 9822 : if (expr != error_mark_node)
9298 9822 : expr = build_reinterpret_cast_1
9299 9822 : (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
9300 : valid_p, complain);
9301 9822 : if (expr != error_mark_node)
9302 : /* cp_build_indirect_ref isn't right for rvalue refs. */
9303 9819 : expr = convert_from_reference (fold_convert (type, expr));
9304 9822 : return expr;
9305 : }
9306 :
9307 : /* As a G++ extension, we consider conversions from member
9308 : functions, and pointers to member functions to
9309 : pointer-to-function and pointer-to-void types. If
9310 : -Wno-pmf-conversions has not been specified,
9311 : convert_member_func_to_ptr will issue an error message. */
9312 250 : if ((TYPE_PTRMEMFUNC_P (intype)
9313 1949664 : || TREE_CODE (intype) == METHOD_TYPE)
9314 244 : && TYPE_PTR_P (type)
9315 1950001 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
9316 123 : || VOID_TYPE_P (TREE_TYPE (type))))
9317 156 : return convert_member_func_to_ptr (type, expr, complain);
9318 :
9319 : /* If the cast is not to a reference type, the lvalue-to-rvalue,
9320 : array-to-pointer, and function-to-pointer conversions are
9321 : performed. */
9322 1949680 : expr = decay_conversion (expr, complain);
9323 :
9324 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9325 : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9326 1949680 : if (TREE_CODE (expr) == NOP_EXPR
9327 1949680 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9328 388 : expr = TREE_OPERAND (expr, 0);
9329 :
9330 1949680 : if (error_operand_p (expr))
9331 30 : return error_mark_node;
9332 :
9333 1949650 : intype = TREE_TYPE (expr);
9334 :
9335 : /* [expr.reinterpret.cast]
9336 : A pointer can be converted to any integral type large enough to
9337 : hold it. ... A value of type std::nullptr_t can be converted to
9338 : an integral type; the conversion has the same meaning and
9339 : validity as a conversion of (void*)0 to the integral type. */
9340 1949650 : if (CP_INTEGRAL_TYPE_P (type)
9341 168908 : && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
9342 : {
9343 167001 : if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
9344 : {
9345 21 : if (complain & tf_error)
9346 15 : permerror (loc, "cast from %qH to %qI loses precision",
9347 : intype, type);
9348 : else
9349 6 : return error_mark_node;
9350 : }
9351 166995 : if (NULLPTR_TYPE_P (intype))
9352 98 : return build_int_cst (type, 0);
9353 : }
9354 : /* [expr.reinterpret.cast]
9355 : A value of integral or enumeration type can be explicitly
9356 : converted to a pointer. */
9357 1782649 : else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
9358 : /* OK */
9359 : ;
9360 1747690 : else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
9361 1745768 : || TYPE_PTR_OR_PTRMEM_P (type))
9362 1864546 : && same_type_p (type, intype))
9363 : /* DR 799 */
9364 527 : return rvalue (expr);
9365 1747163 : else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
9366 : {
9367 282 : if ((complain & tf_warning)
9368 564 : && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
9369 282 : TREE_TYPE (intype)))
9370 145 : warning_at (loc, OPT_Wcast_function_type,
9371 : "cast between incompatible function types"
9372 : " from %qH to %qI", intype, type);
9373 282 : return build_nop_reinterpret (type, expr);
9374 : }
9375 1746881 : else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
9376 : {
9377 79 : if ((complain & tf_warning)
9378 158 : && !cxx_safe_function_type_cast_p
9379 79 : (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
9380 79 : TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
9381 52 : warning_at (loc, OPT_Wcast_function_type,
9382 : "cast between incompatible pointer to member types"
9383 : " from %qH to %qI", intype, type);
9384 79 : return build_nop_reinterpret (type, expr);
9385 : }
9386 21 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
9387 1746802 : || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
9388 : {
9389 115681 : if (!c_cast_p
9390 115681 : && check_for_casting_away_constness (loc, intype, type,
9391 : REINTERPRET_CAST_EXPR,
9392 : complain))
9393 21 : return error_mark_node;
9394 : /* Warn about possible alignment problems. */
9395 115660 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9396 24 : && (complain & tf_warning)
9397 24 : && !VOID_TYPE_P (type)
9398 24 : && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
9399 24 : && COMPLETE_TYPE_P (TREE_TYPE (type))
9400 24 : && COMPLETE_TYPE_P (TREE_TYPE (intype))
9401 115708 : && min_align_of_type (TREE_TYPE (type))
9402 24 : > min_align_of_type (TREE_TYPE (intype)))
9403 18 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9404 : "increases required alignment of target type",
9405 : intype, type);
9406 :
9407 115660 : if (warn_strict_aliasing <= 2)
9408 : /* strict_aliasing_warning STRIP_NOPs its expr. */
9409 103825 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9410 :
9411 115660 : return build_nop_reinterpret (type, expr);
9412 : }
9413 297 : else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
9414 1631281 : || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
9415 : {
9416 276 : if (complain & tf_warning)
9417 : /* C++11 5.2.10 p8 says that "Converting a function pointer to an
9418 : object pointer type or vice versa is conditionally-supported." */
9419 276 : warning_at (loc, OPT_Wconditionally_supported,
9420 : "casting between pointer-to-function and "
9421 : "pointer-to-object is conditionally-supported");
9422 276 : return build_nop_reinterpret (type, expr);
9423 : }
9424 1630845 : else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
9425 1628888 : return convert_to_vector (type, rvalue (expr));
9426 1957 : else if (gnu_vector_type_p (intype)
9427 1957 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9428 1855 : return convert_to_integer_nofold (type, expr);
9429 : else
9430 : {
9431 102 : if (valid_p)
9432 84 : *valid_p = false;
9433 102 : if (complain & tf_error)
9434 93 : error_at (loc, "invalid cast from type %qT to type %qT",
9435 : intype, type);
9436 102 : return error_mark_node;
9437 : }
9438 :
9439 201856 : expr = cp_convert (type, expr, complain);
9440 201856 : if (TREE_CODE (expr) == NOP_EXPR)
9441 : /* Mark any nop_expr that created as a reintepret_cast. */
9442 0 : REINTERPRET_CAST_P (expr) = true;
9443 : return expr;
9444 : }
9445 :
9446 : tree
9447 774378 : build_reinterpret_cast (location_t loc, tree type, tree expr,
9448 : tsubst_flags_t complain)
9449 : {
9450 774378 : tree r;
9451 :
9452 774378 : if (type == error_mark_node || expr == error_mark_node)
9453 : return error_mark_node;
9454 :
9455 774371 : if (processing_template_decl)
9456 : {
9457 628700 : tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
9458 :
9459 628700 : if (!TREE_SIDE_EFFECTS (t)
9460 628700 : && type_dependent_expression_p (expr))
9461 : /* There might turn out to be side effects inside expr. */
9462 295485 : TREE_SIDE_EFFECTS (t) = 1;
9463 628700 : r = convert_from_reference (t);
9464 628700 : protected_set_expr_location (r, loc);
9465 628700 : return r;
9466 : }
9467 :
9468 145671 : r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9469 : /*valid_p=*/NULL, complain);
9470 145671 : if (r != error_mark_node)
9471 : {
9472 145608 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9473 145608 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9474 : }
9475 145671 : protected_set_expr_location (r, loc);
9476 145671 : return r;
9477 : }
9478 :
9479 : /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9480 : return an appropriate expression. Otherwise, return
9481 : error_mark_node. If the cast is not valid, and COMPLAIN is true,
9482 : then a diagnostic will be issued. If VALID_P is non-NULL, we are
9483 : performing a C-style cast, its value upon return will indicate
9484 : whether or not the conversion succeeded. */
9485 :
9486 : static tree
9487 39494691 : build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9488 : tsubst_flags_t complain, bool *valid_p)
9489 : {
9490 39494691 : tree src_type;
9491 39494691 : tree reference_type;
9492 :
9493 : /* Callers are responsible for handling error_mark_node as a
9494 : destination type. */
9495 39494691 : gcc_assert (dst_type != error_mark_node);
9496 : /* In a template, callers should be building syntactic
9497 : representations of casts, not using this machinery. */
9498 39494691 : gcc_assert (!processing_template_decl);
9499 :
9500 : /* Assume the conversion is invalid. */
9501 39494691 : if (valid_p)
9502 39297235 : *valid_p = false;
9503 :
9504 39494691 : if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9505 : {
9506 38407965 : if (complain & tf_error)
9507 9 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9508 : "which is not a pointer, reference, "
9509 : "nor a pointer-to-data-member type", dst_type);
9510 38407965 : return error_mark_node;
9511 : }
9512 :
9513 1086726 : if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
9514 : {
9515 4173 : if (complain & tf_error)
9516 6 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9517 : "which is a pointer or reference to a function type",
9518 : dst_type);
9519 4173 : return error_mark_node;
9520 : }
9521 :
9522 : /* A prvalue of non-class type is cv-unqualified. */
9523 1082553 : dst_type = cv_unqualified (dst_type);
9524 :
9525 : /* Save casted types in the function's used types hash table. */
9526 1082553 : used_types_insert (dst_type);
9527 :
9528 1082553 : src_type = TREE_TYPE (expr);
9529 : /* Expressions do not really have reference types. */
9530 1082553 : if (TYPE_REF_P (src_type))
9531 0 : src_type = TREE_TYPE (src_type);
9532 :
9533 : /* [expr.const.cast]
9534 :
9535 : For two object types T1 and T2, if a pointer to T1 can be explicitly
9536 : converted to the type "pointer to T2" using a const_cast, then the
9537 : following conversions can also be made:
9538 :
9539 : -- an lvalue of type T1 can be explicitly converted to an lvalue of
9540 : type T2 using the cast const_cast<T2&>;
9541 :
9542 : -- a glvalue of type T1 can be explicitly converted to an xvalue of
9543 : type T2 using the cast const_cast<T2&&>; and
9544 :
9545 : -- if T1 is a class type, a prvalue of type T1 can be explicitly
9546 : converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9547 :
9548 1082553 : if (TYPE_REF_P (dst_type))
9549 : {
9550 124317 : reference_type = dst_type;
9551 124317 : if (!TYPE_REF_IS_RVALUE (dst_type)
9552 124317 : ? lvalue_p (expr)
9553 1560 : : obvalue_p (expr))
9554 : /* OK. */;
9555 : else
9556 : {
9557 35 : if (complain & tf_error)
9558 9 : error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
9559 : "to type %qT",
9560 : src_type, dst_type);
9561 35 : return error_mark_node;
9562 : }
9563 124282 : dst_type = build_pointer_type (TREE_TYPE (dst_type));
9564 124282 : src_type = build_pointer_type (src_type);
9565 : }
9566 : else
9567 : {
9568 958236 : reference_type = NULL_TREE;
9569 : /* If the destination type is not a reference type, the
9570 : lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9571 : conversions are performed. */
9572 958236 : src_type = type_decays_to (src_type);
9573 958236 : if (src_type == error_mark_node)
9574 : return error_mark_node;
9575 : }
9576 :
9577 1082518 : if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9578 : {
9579 640989 : if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9580 : {
9581 397653 : if (valid_p)
9582 : {
9583 200275 : *valid_p = true;
9584 : /* This cast is actually a C-style cast. Issue a warning if
9585 : the user is making a potentially unsafe cast. */
9586 200275 : check_for_casting_away_constness (loc, src_type, dst_type,
9587 : CAST_EXPR, complain);
9588 : /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9589 200275 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9590 3 : && (complain & tf_warning)
9591 200281 : && min_align_of_type (TREE_TYPE (dst_type))
9592 3 : > min_align_of_type (TREE_TYPE (src_type)))
9593 3 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9594 : "increases required alignment of target type",
9595 : src_type, dst_type);
9596 : }
9597 397653 : if (reference_type)
9598 : {
9599 122971 : expr = cp_build_addr_expr (expr, complain);
9600 122971 : if (expr == error_mark_node)
9601 : return error_mark_node;
9602 122953 : expr = build_nop (reference_type, expr);
9603 122953 : return convert_from_reference (expr);
9604 : }
9605 : else
9606 : {
9607 274682 : expr = decay_conversion (expr, complain);
9608 274682 : if (expr == error_mark_node)
9609 : return error_mark_node;
9610 :
9611 : /* build_c_cast puts on a NOP_EXPR to make the result not an
9612 : lvalue. Strip such NOP_EXPRs if VALUE is being used in
9613 : non-lvalue context. */
9614 274682 : if (TREE_CODE (expr) == NOP_EXPR
9615 274682 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9616 3 : expr = TREE_OPERAND (expr, 0);
9617 274682 : return build_nop (dst_type, expr);
9618 : }
9619 : }
9620 243336 : else if (valid_p
9621 486624 : && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9622 243288 : TREE_TYPE (src_type)))
9623 14976 : check_for_casting_away_constness (loc, src_type, dst_type,
9624 : CAST_EXPR, complain);
9625 : }
9626 :
9627 684865 : if (complain & tf_error)
9628 30 : error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9629 : src_type, dst_type);
9630 684865 : return error_mark_node;
9631 : }
9632 :
9633 : tree
9634 695750 : build_const_cast (location_t loc, tree type, tree expr,
9635 : tsubst_flags_t complain)
9636 : {
9637 695750 : tree r;
9638 :
9639 695750 : if (type == error_mark_node || error_operand_p (expr))
9640 : return error_mark_node;
9641 :
9642 695742 : if (processing_template_decl)
9643 : {
9644 498286 : tree t = build_min (CONST_CAST_EXPR, type, expr);
9645 :
9646 498286 : if (!TREE_SIDE_EFFECTS (t)
9647 498286 : && type_dependent_expression_p (expr))
9648 : /* There might turn out to be side effects inside expr. */
9649 382299 : TREE_SIDE_EFFECTS (t) = 1;
9650 498286 : r = convert_from_reference (t);
9651 498286 : protected_set_expr_location (r, loc);
9652 498286 : return r;
9653 : }
9654 :
9655 197456 : r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9656 197456 : if (r != error_mark_node)
9657 : {
9658 197378 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9659 197378 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9660 : }
9661 197456 : protected_set_expr_location (r, loc);
9662 197456 : return r;
9663 : }
9664 :
9665 : /* Like cp_build_c_cast, but for the c-common bits. */
9666 :
9667 : tree
9668 0 : build_c_cast (location_t loc, tree type, tree expr)
9669 : {
9670 0 : return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9671 : }
9672 :
9673 : /* Like the "build_c_cast" used for c-common, but using cp_expr to
9674 : preserve location information even for tree nodes that don't
9675 : support it. */
9676 :
9677 : cp_expr
9678 10672794 : build_c_cast (location_t loc, tree type, cp_expr expr)
9679 : {
9680 10672794 : cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9681 10672794 : result.set_location (loc);
9682 10672794 : return result;
9683 : }
9684 :
9685 : /* Build an expression representing an explicit C-style cast to type
9686 : TYPE of expression EXPR. */
9687 :
9688 : tree
9689 42155268 : cp_build_c_cast (location_t loc, tree type, tree expr,
9690 : tsubst_flags_t complain)
9691 : {
9692 42155268 : tree value = expr;
9693 42155268 : tree result;
9694 42155268 : bool valid_p;
9695 :
9696 42155268 : if (type == error_mark_node || error_operand_p (expr))
9697 : return error_mark_node;
9698 :
9699 42155080 : if (processing_template_decl)
9700 : {
9701 2857836 : tree t = build_min (CAST_EXPR, type,
9702 : tree_cons (NULL_TREE, value, NULL_TREE));
9703 : /* We don't know if it will or will not have side effects. */
9704 2857836 : TREE_SIDE_EFFECTS (t) = 1;
9705 2857836 : return convert_from_reference (t);
9706 : }
9707 :
9708 : /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9709 : 'Class') should always be retained, because this information aids
9710 : in method lookup. */
9711 39297244 : if (objc_is_object_ptr (type)
9712 39297244 : && objc_is_object_ptr (TREE_TYPE (expr)))
9713 0 : return build_nop (type, expr);
9714 :
9715 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9716 : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9717 39297244 : if (!TYPE_REF_P (type)
9718 39294496 : && TREE_CODE (value) == NOP_EXPR
9719 39774558 : && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9720 241170 : value = TREE_OPERAND (value, 0);
9721 :
9722 39297244 : if (TREE_CODE (type) == ARRAY_TYPE)
9723 : {
9724 : /* Allow casting from T1* to T2[] because Cfront allows it.
9725 : NIHCL uses it. It is not valid ISO C++ however. */
9726 3 : if (TYPE_PTR_P (TREE_TYPE (expr)))
9727 : {
9728 0 : if (complain & tf_error)
9729 0 : permerror (loc, "ISO C++ forbids casting to an array type %qT",
9730 : type);
9731 : else
9732 0 : return error_mark_node;
9733 0 : type = build_pointer_type (TREE_TYPE (type));
9734 : }
9735 : else
9736 : {
9737 3 : if (complain & tf_error)
9738 3 : error_at (loc, "ISO C++ forbids casting to an array type %qT",
9739 : type);
9740 3 : return error_mark_node;
9741 : }
9742 : }
9743 :
9744 39297241 : if (FUNC_OR_METHOD_TYPE_P (type))
9745 : {
9746 15 : if (complain & tf_error)
9747 15 : error_at (loc, "invalid cast to function type %qT", type);
9748 15 : return error_mark_node;
9749 : }
9750 :
9751 39297226 : if (TYPE_PTR_P (type)
9752 886385 : && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9753 : /* Casting to an integer of smaller size is an error detected elsewhere. */
9754 440118 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9755 : /* Don't warn about converting any constant. */
9756 39680567 : && !TREE_CONSTANT (value))
9757 47 : warning_at (loc, OPT_Wint_to_pointer_cast,
9758 : "cast to pointer from integer of different size");
9759 :
9760 : /* A C-style cast can be a const_cast. */
9761 39297226 : result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9762 : &valid_p);
9763 39297226 : if (valid_p)
9764 : {
9765 200266 : if (result != error_mark_node)
9766 : {
9767 200257 : maybe_warn_about_useless_cast (loc, type, value, complain);
9768 200257 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9769 : }
9770 9 : else if (complain & tf_error)
9771 9 : build_const_cast_1 (loc, type, value, tf_error, &valid_p);
9772 200266 : return result;
9773 : }
9774 :
9775 : /* Or a static cast. */
9776 39096960 : result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9777 : &valid_p, complain);
9778 : /* Or a reinterpret_cast. */
9779 39096960 : if (!valid_p)
9780 1804174 : result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9781 : &valid_p, complain);
9782 : /* The static_cast or reinterpret_cast may be followed by a
9783 : const_cast. */
9784 39096960 : if (valid_p
9785 : /* A valid cast may result in errors if, for example, a
9786 : conversion to an ambiguous base class is required. */
9787 39096960 : && !error_operand_p (result))
9788 : {
9789 39096541 : tree result_type;
9790 :
9791 39096541 : maybe_warn_about_useless_cast (loc, type, value, complain);
9792 39096541 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9793 :
9794 : /* Non-class rvalues always have cv-unqualified type. */
9795 39096541 : if (!CLASS_TYPE_P (type))
9796 36281928 : type = TYPE_MAIN_VARIANT (type);
9797 39096541 : result_type = TREE_TYPE (result);
9798 39096541 : if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9799 36280595 : result_type = TYPE_MAIN_VARIANT (result_type);
9800 : /* If the type of RESULT does not match TYPE, perform a
9801 : const_cast to make it match. If the static_cast or
9802 : reinterpret_cast succeeded, we will differ by at most
9803 : cv-qualification, so the follow-on const_cast is guaranteed
9804 : to succeed. */
9805 39096541 : if (!same_type_p (non_reference (type), non_reference (result_type)))
9806 : {
9807 0 : result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
9808 0 : gcc_assert (valid_p);
9809 : }
9810 39096541 : return result;
9811 : }
9812 :
9813 419 : return error_mark_node;
9814 : }
9815 :
9816 : /* Warn when a value is moved to itself with std::move. LHS is the target,
9817 : RHS may be the std::move call, and LOC is the location of the whole
9818 : assignment. Return true if we warned. */
9819 :
9820 : bool
9821 29218805 : maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9822 : {
9823 29218805 : if (!warn_self_move)
9824 : return false;
9825 :
9826 : /* C++98 doesn't know move. */
9827 365854 : if (cxx_dialect < cxx11)
9828 : return false;
9829 :
9830 345968 : if (processing_template_decl)
9831 : return false;
9832 :
9833 26454 : if (!REFERENCE_REF_P (rhs)
9834 301627 : || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9835 : return false;
9836 4710 : tree fn = TREE_OPERAND (rhs, 0);
9837 4710 : if (!is_std_move_p (fn))
9838 : return false;
9839 :
9840 : /* Just a little helper to strip * and various NOPs. */
9841 6519 : auto extract_op = [] (tree &op) {
9842 4346 : STRIP_NOPS (op);
9843 5567 : while (INDIRECT_REF_P (op))
9844 1221 : op = TREE_OPERAND (op, 0);
9845 4346 : op = maybe_undo_parenthesized_ref (op);
9846 4346 : STRIP_ANY_LOCATION_WRAPPER (op);
9847 4346 : };
9848 :
9849 2173 : tree arg = CALL_EXPR_ARG (fn, 0);
9850 2173 : extract_op (arg);
9851 2173 : if (TREE_CODE (arg) == ADDR_EXPR)
9852 1497 : arg = TREE_OPERAND (arg, 0);
9853 2173 : tree type = TREE_TYPE (lhs);
9854 2173 : tree orig_lhs = lhs;
9855 2173 : extract_op (lhs);
9856 2173 : if (cp_tree_equal (lhs, arg)
9857 : /* Also warn in a member-initializer-list, as in : i(std::move(i)). */
9858 2173 : || (TREE_CODE (lhs) == FIELD_DECL
9859 640 : && TREE_CODE (arg) == COMPONENT_REF
9860 305 : && cp_tree_equal (TREE_OPERAND (arg, 0), current_class_ref)
9861 6 : && TREE_OPERAND (arg, 1) == lhs))
9862 117 : if (warning_at (loc, OPT_Wself_move,
9863 : "moving %qE of type %qT to itself", orig_lhs, type))
9864 : return true;
9865 :
9866 : return false;
9867 : }
9868 :
9869 : /* For use from the C common bits. */
9870 : tree
9871 4772 : build_modify_expr (location_t location,
9872 : tree lhs, tree /*lhs_origtype*/,
9873 : enum tree_code modifycode,
9874 : location_t /*rhs_location*/, tree rhs,
9875 : tree /*rhs_origtype*/)
9876 : {
9877 4772 : return cp_build_modify_expr (location, lhs, modifycode, rhs,
9878 4772 : tf_warning_or_error);
9879 : }
9880 :
9881 : /* Build an assignment expression of lvalue LHS from value RHS.
9882 : MODIFYCODE is the code for a binary operator that we use
9883 : to combine the old value of LHS with RHS to get the new value.
9884 : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9885 :
9886 : C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9887 :
9888 : tree
9889 38143308 : cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9890 : tree rhs, tsubst_flags_t complain)
9891 : {
9892 38143308 : lhs = mark_lvalue_use_nonread (lhs);
9893 :
9894 38143308 : tree result = NULL_TREE;
9895 38143308 : tree newrhs = rhs;
9896 38143308 : tree lhstype = TREE_TYPE (lhs);
9897 38143308 : tree olhs = lhs;
9898 38143308 : tree olhstype = lhstype;
9899 38143308 : bool plain_assign = (modifycode == NOP_EXPR);
9900 38143308 : bool compound_side_effects_p = false;
9901 38143308 : tree preeval = NULL_TREE;
9902 :
9903 : /* Avoid duplicate error messages from operands that had errors. */
9904 38143308 : if (error_operand_p (lhs) || error_operand_p (rhs))
9905 19 : return error_mark_node;
9906 :
9907 38143371 : while (TREE_CODE (lhs) == COMPOUND_EXPR)
9908 : {
9909 82 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9910 70 : compound_side_effects_p = true;
9911 82 : lhs = TREE_OPERAND (lhs, 1);
9912 : }
9913 :
9914 : /* Handle control structure constructs used as "lvalues". Note that we
9915 : leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9916 38143289 : switch (TREE_CODE (lhs))
9917 : {
9918 : /* Handle --foo = 5; as these are valid constructs in C++. */
9919 27 : case PREDECREMENT_EXPR:
9920 27 : case PREINCREMENT_EXPR:
9921 27 : if (compound_side_effects_p)
9922 9 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9923 27 : lhs = genericize_compound_lvalue (lhs);
9924 173 : maybe_add_compound:
9925 : /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9926 : and looked through the COMPOUND_EXPRs, readd them now around
9927 : the resulting lhs. */
9928 173 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9929 : {
9930 9 : lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9931 9 : tree *ptr = &TREE_OPERAND (lhs, 1);
9932 9 : for (olhs = TREE_OPERAND (olhs, 1);
9933 12 : TREE_CODE (olhs) == COMPOUND_EXPR;
9934 3 : olhs = TREE_OPERAND (olhs, 1))
9935 : {
9936 3 : *ptr = build2 (COMPOUND_EXPR, lhstype,
9937 3 : TREE_OPERAND (olhs, 0), *ptr);
9938 3 : ptr = &TREE_OPERAND (*ptr, 1);
9939 : }
9940 : }
9941 : break;
9942 :
9943 146 : case MODIFY_EXPR:
9944 146 : if (compound_side_effects_p)
9945 0 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9946 146 : lhs = genericize_compound_lvalue (lhs);
9947 146 : goto maybe_add_compound;
9948 :
9949 0 : case MIN_EXPR:
9950 0 : case MAX_EXPR:
9951 : /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9952 : when neither operand has side-effects. */
9953 0 : if (!lvalue_or_else (lhs, lv_assign, complain))
9954 0 : return error_mark_node;
9955 :
9956 0 : gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9957 : && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9958 :
9959 0 : lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9960 0 : build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9961 : boolean_type_node,
9962 0 : TREE_OPERAND (lhs, 0),
9963 0 : TREE_OPERAND (lhs, 1)),
9964 0 : TREE_OPERAND (lhs, 0),
9965 0 : TREE_OPERAND (lhs, 1));
9966 212 : gcc_fallthrough ();
9967 :
9968 : /* Handle (a ? b : c) used as an "lvalue". */
9969 212 : case COND_EXPR:
9970 212 : {
9971 : /* Produce (a ? (b = rhs) : (c = rhs))
9972 : except that the RHS goes through a save-expr
9973 : so the code to compute it is only emitted once. */
9974 212 : if (VOID_TYPE_P (TREE_TYPE (rhs)))
9975 : {
9976 18 : if (complain & tf_error)
9977 24 : error_at (cp_expr_loc_or_loc (rhs, loc),
9978 : "void value not ignored as it ought to be");
9979 18 : return error_mark_node;
9980 : }
9981 :
9982 194 : rhs = stabilize_expr (rhs, &preeval);
9983 :
9984 : /* Check this here to avoid odd errors when trying to convert
9985 : a throw to the type of the COND_EXPR. */
9986 194 : if (!lvalue_or_else (lhs, lv_assign, complain))
9987 6 : return error_mark_node;
9988 :
9989 188 : tree op1 = TREE_OPERAND (lhs, 1);
9990 188 : if (TREE_CODE (op1) != THROW_EXPR)
9991 182 : op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
9992 : /* When sanitizing undefined behavior, even when rhs doesn't need
9993 : stabilization at this point, the sanitization might add extra
9994 : SAVE_EXPRs in there and so make sure there is no tree sharing
9995 : in the rhs, otherwise those SAVE_EXPRs will have initialization
9996 : only in one of the two branches. */
9997 188 : if (sanitize_flags_p (SANITIZE_UNDEFINED
9998 : | SANITIZE_UNDEFINED_NONDEFAULT))
9999 3 : rhs = unshare_expr (rhs);
10000 188 : tree op2 = TREE_OPERAND (lhs, 2);
10001 188 : if (TREE_CODE (op2) != THROW_EXPR)
10002 182 : op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
10003 188 : tree cond = build_conditional_expr (input_location,
10004 188 : TREE_OPERAND (lhs, 0), op1, op2,
10005 : complain);
10006 :
10007 188 : if (cond == error_mark_node)
10008 : return cond;
10009 : /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
10010 : and looked through the COMPOUND_EXPRs, readd them now around
10011 : the resulting cond before adding the preevaluated rhs. */
10012 185 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
10013 : {
10014 18 : cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
10015 18 : TREE_OPERAND (olhs, 0), cond);
10016 18 : tree *ptr = &TREE_OPERAND (cond, 1);
10017 18 : for (olhs = TREE_OPERAND (olhs, 1);
10018 21 : TREE_CODE (olhs) == COMPOUND_EXPR;
10019 3 : olhs = TREE_OPERAND (olhs, 1))
10020 : {
10021 3 : *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
10022 3 : TREE_OPERAND (olhs, 0), *ptr);
10023 3 : ptr = &TREE_OPERAND (*ptr, 1);
10024 : }
10025 : }
10026 : /* Make sure the code to compute the rhs comes out
10027 : before the split. */
10028 185 : result = cond;
10029 185 : goto ret;
10030 : }
10031 :
10032 : default:
10033 : lhs = olhs;
10034 : break;
10035 : }
10036 :
10037 38143077 : if (modifycode == INIT_EXPR)
10038 : {
10039 4342729 : if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10040 : /* Do the default thing. */;
10041 4135197 : else if (TREE_CODE (rhs) == CONSTRUCTOR)
10042 : {
10043 : /* Compound literal. */
10044 98 : if (! same_type_p (TREE_TYPE (rhs), lhstype))
10045 : /* Call convert to generate an error; see PR 11063. */
10046 0 : rhs = convert (lhstype, rhs);
10047 98 : result = cp_build_init_expr (lhs, rhs);
10048 98 : TREE_SIDE_EFFECTS (result) = 1;
10049 98 : goto ret;
10050 : }
10051 4135099 : else if (! MAYBE_CLASS_TYPE_P (lhstype))
10052 : /* Do the default thing. */;
10053 : else
10054 : {
10055 41546 : releasing_vec rhs_vec = make_tree_vector_single (rhs);
10056 41546 : result = build_special_member_call (lhs, complete_ctor_identifier,
10057 : &rhs_vec, lhstype, LOOKUP_NORMAL,
10058 : complain);
10059 41546 : if (result == NULL_TREE)
10060 0 : return error_mark_node;
10061 41546 : goto ret;
10062 41546 : }
10063 : }
10064 : else
10065 : {
10066 33800348 : lhs = require_complete_type (lhs, complain);
10067 33800348 : if (lhs == error_mark_node)
10068 : return error_mark_node;
10069 :
10070 33800182 : if (modifycode == NOP_EXPR)
10071 : {
10072 29169650 : maybe_warn_self_move (loc, lhs, rhs);
10073 :
10074 29169650 : if (c_dialect_objc ())
10075 : {
10076 0 : result = objc_maybe_build_modify_expr (lhs, rhs);
10077 0 : if (result)
10078 0 : goto ret;
10079 : }
10080 :
10081 : /* `operator=' is not an inheritable operator. */
10082 29169650 : if (! MAYBE_CLASS_TYPE_P (lhstype))
10083 : /* Do the default thing. */;
10084 : else
10085 : {
10086 3508234 : result = build_new_op (input_location, MODIFY_EXPR,
10087 : LOOKUP_NORMAL, lhs, rhs,
10088 : make_node (NOP_EXPR), NULL_TREE,
10089 : /*overload=*/NULL, complain);
10090 3508234 : if (result == NULL_TREE)
10091 0 : return error_mark_node;
10092 3508234 : goto ret;
10093 : }
10094 : lhstype = olhstype;
10095 : }
10096 : else
10097 : {
10098 4630532 : tree init = NULL_TREE;
10099 :
10100 : /* A binary op has been requested. Combine the old LHS
10101 : value with the RHS producing the value we should actually
10102 : store into the LHS. */
10103 4630532 : gcc_assert (!((TYPE_REF_P (lhstype)
10104 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
10105 : || MAYBE_CLASS_TYPE_P (lhstype)));
10106 :
10107 : /* Preevaluate the RHS to make sure its evaluation is complete
10108 : before the lvalue-to-rvalue conversion of the LHS:
10109 :
10110 : [expr.ass] With respect to an indeterminately-sequenced
10111 : function call, the operation of a compound assignment is a
10112 : single evaluation. [ Note: Therefore, a function call shall
10113 : not intervene between the lvalue-to-rvalue conversion and the
10114 : side effect associated with any single compound assignment
10115 : operator. -- end note ] */
10116 4630532 : lhs = cp_stabilize_reference (lhs);
10117 4630532 : rhs = decay_conversion (rhs, complain);
10118 4630532 : if (rhs == error_mark_node)
10119 123 : return error_mark_node;
10120 :
10121 4630529 : {
10122 4630529 : auto_diagnostic_group d;
10123 4630529 : rhs = stabilize_expr (rhs, &init);
10124 4630529 : bool clear_decl_read = false;
10125 4630529 : tree stripped_lhs = tree_strip_any_location_wrapper (lhs);
10126 1431847 : if ((VAR_P (stripped_lhs) || TREE_CODE (stripped_lhs) == PARM_DECL)
10127 3890083 : && !DECL_READ_P (stripped_lhs)
10128 986556 : && (VAR_P (stripped_lhs) ? warn_unused_but_set_variable
10129 : : warn_unused_but_set_parameter) > 2
10130 7070 : && !CLASS_TYPE_P (TREE_TYPE (lhs))
10131 4637599 : && !CLASS_TYPE_P (TREE_TYPE (rhs)))
10132 : {
10133 7070 : mark_exp_read (rhs);
10134 7070 : if (!DECL_READ_P (stripped_lhs))
10135 4630529 : clear_decl_read = true;
10136 : }
10137 4630529 : newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
10138 4630529 : if (clear_decl_read)
10139 7070 : DECL_READ_P (stripped_lhs) = 0;
10140 4630529 : if (newrhs == error_mark_node)
10141 : {
10142 120 : if (complain & tf_error)
10143 24 : inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
10144 24 : modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
10145 120 : return error_mark_node;
10146 : }
10147 4630529 : }
10148 :
10149 4630409 : if (init)
10150 265706 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
10151 :
10152 : /* Now it looks like a plain assignment. */
10153 4630409 : modifycode = NOP_EXPR;
10154 4630409 : if (c_dialect_objc ())
10155 : {
10156 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
10157 0 : if (result)
10158 0 : goto ret;
10159 : }
10160 : }
10161 30291825 : gcc_assert (!TYPE_REF_P (lhstype));
10162 30291825 : gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
10163 : }
10164 :
10165 : /* The left-hand side must be an lvalue. */
10166 34592910 : if (!lvalue_or_else (lhs, lv_assign, complain))
10167 839 : return error_mark_node;
10168 :
10169 : /* Warn about modifying something that is `const'. Don't warn if
10170 : this is initialization. */
10171 34592071 : if (modifycode != INIT_EXPR
10172 34592071 : && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
10173 : /* Functions are not modifiable, even though they are
10174 : lvalues. */
10175 30249274 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
10176 : /* If it's an aggregate and any field is const, then it is
10177 : effectively const. */
10178 30249196 : || (CLASS_TYPE_P (lhstype)
10179 0 : && C_TYPE_FIELDS_READONLY (lhstype))))
10180 : {
10181 41790 : if (complain & tf_error)
10182 89 : cxx_readonly_error (loc, lhs, lv_assign);
10183 41790 : return error_mark_node;
10184 : }
10185 :
10186 : /* If storing into a structure or union member, it may have been given a
10187 : lowered bitfield type. We need to convert to the declared type first,
10188 : so retrieve it now. */
10189 :
10190 34550281 : olhstype = unlowered_expr_type (lhs);
10191 :
10192 : /* Convert new value to destination type. */
10193 :
10194 34550281 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
10195 : {
10196 222 : int from_array;
10197 :
10198 222 : if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
10199 : {
10200 12 : if (modifycode != INIT_EXPR)
10201 : {
10202 12 : if (complain & tf_error)
10203 12 : error_at (loc,
10204 : "assigning to an array from an initializer list");
10205 12 : return error_mark_node;
10206 : }
10207 0 : if (check_array_initializer (lhs, lhstype, newrhs))
10208 0 : return error_mark_node;
10209 0 : newrhs = digest_init (lhstype, newrhs, complain);
10210 0 : if (newrhs == error_mark_node)
10211 : return error_mark_node;
10212 : }
10213 :
10214 : /* C++11 8.5/17: "If the destination type is an array of characters,
10215 : an array of char16_t, an array of char32_t, or an array of wchar_t,
10216 : and the initializer is a string literal...". */
10217 210 : else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
10218 : == STRING_CST)
10219 43 : && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
10220 253 : && modifycode == INIT_EXPR)
10221 : {
10222 28 : newrhs = digest_init (lhstype, newrhs, complain);
10223 28 : if (newrhs == error_mark_node)
10224 : return error_mark_node;
10225 : }
10226 :
10227 182 : else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
10228 : TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
10229 : {
10230 27 : if (complain & tf_error)
10231 12 : error_at (loc, "incompatible types in assignment of %qT to %qT",
10232 12 : TREE_TYPE (rhs), lhstype);
10233 27 : return error_mark_node;
10234 : }
10235 :
10236 : /* Allow array assignment in compiler-generated code. */
10237 155 : else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
10238 : /* OK, used by coroutines (co-await-initlist1.C). */;
10239 149 : else if (!current_function_decl
10240 149 : || !DECL_DEFAULTED_FN (current_function_decl))
10241 : {
10242 : /* This routine is used for both initialization and assignment.
10243 : Make sure the diagnostic message differentiates the context. */
10244 99 : if (complain & tf_error)
10245 : {
10246 30 : if (modifycode == INIT_EXPR)
10247 9 : error_at (loc, "array used as initializer");
10248 : else
10249 21 : error_at (loc, "invalid array assignment");
10250 : }
10251 99 : return error_mark_node;
10252 : }
10253 :
10254 131 : from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
10255 84 : ? 1 + (modifycode != INIT_EXPR) : 0;
10256 84 : result = build_vec_init (lhs, NULL_TREE, newrhs,
10257 : /*explicit_value_init_p=*/false,
10258 : from_array, complain);
10259 84 : goto ret;
10260 : }
10261 :
10262 34550059 : if (modifycode == INIT_EXPR)
10263 : /* Calls with INIT_EXPR are all direct-initialization, so don't set
10264 : LOOKUP_ONLYCONVERTING. */
10265 4301026 : newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
10266 : ICR_INIT, NULL_TREE, 0,
10267 : complain | tf_no_cleanup);
10268 : else
10269 30249033 : newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
10270 : NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
10271 :
10272 34550059 : if (!same_type_p (lhstype, olhstype))
10273 405614 : newrhs = cp_convert_and_check (lhstype, newrhs, complain);
10274 :
10275 34550059 : if (modifycode != INIT_EXPR)
10276 : {
10277 30249033 : if (TREE_CODE (newrhs) == CALL_EXPR
10278 30249033 : && TYPE_NEEDS_CONSTRUCTING (lhstype))
10279 0 : newrhs = build_cplus_new (lhstype, newrhs, complain);
10280 :
10281 : /* Can't initialize directly from a TARGET_EXPR, since that would
10282 : cause the lhs to be constructed twice, and possibly result in
10283 : accidental self-initialization. So we force the TARGET_EXPR to be
10284 : expanded without a target. */
10285 30249033 : if (TREE_CODE (newrhs) == TARGET_EXPR)
10286 118 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
10287 118 : TARGET_EXPR_SLOT (newrhs));
10288 : }
10289 :
10290 34550059 : if (newrhs == error_mark_node)
10291 : return error_mark_node;
10292 :
10293 34549357 : if (c_dialect_objc () && flag_objc_gc)
10294 : {
10295 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
10296 :
10297 0 : if (result)
10298 0 : goto ret;
10299 : }
10300 :
10301 38850334 : result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
10302 : lhstype, lhs, newrhs);
10303 34549357 : if (modifycode == INIT_EXPR)
10304 4300977 : set_target_expr_eliding (newrhs);
10305 :
10306 34549357 : TREE_SIDE_EFFECTS (result) = 1;
10307 34549357 : if (!plain_assign)
10308 8931356 : suppress_warning (result, OPT_Wparentheses);
10309 :
10310 25618001 : ret:
10311 38099504 : if (preeval)
10312 39 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
10313 : return result;
10314 : }
10315 :
10316 : cp_expr
10317 71415149 : build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
10318 : tree rhs, tree lookups, tsubst_flags_t complain)
10319 : {
10320 71415149 : tree orig_lhs = lhs;
10321 71415149 : tree orig_rhs = rhs;
10322 71415149 : tree overload = NULL_TREE;
10323 :
10324 71415149 : if (lhs == error_mark_node || rhs == error_mark_node)
10325 2192 : return cp_expr (error_mark_node, loc);
10326 :
10327 71412957 : tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
10328 :
10329 71412957 : if (processing_template_decl)
10330 : {
10331 49947662 : if (type_dependent_expression_p (lhs)
10332 49947662 : || type_dependent_expression_p (rhs))
10333 : {
10334 37442455 : tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
10335 37442455 : if (modifycode != NOP_EXPR)
10336 6326507 : TREE_TYPE (rval)
10337 12653014 : = build_dependent_operator_type (lookups, modifycode, true);
10338 37442455 : return rval;
10339 : }
10340 : }
10341 :
10342 33970502 : tree rval;
10343 33970502 : if (modifycode == NOP_EXPR)
10344 26400509 : rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
10345 : else
10346 7569993 : rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
10347 : lhs, rhs, op, lookups, &overload, complain);
10348 33970502 : if (rval == error_mark_node)
10349 7257 : return error_mark_node;
10350 33963245 : if (processing_template_decl)
10351 : {
10352 12505186 : if (overload != NULL_TREE)
10353 1977941 : return (build_min_non_dep_op_overload
10354 1977941 : (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
10355 :
10356 10527245 : return (build_min_non_dep
10357 10527245 : (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
10358 : }
10359 21458059 : return rval;
10360 : }
10361 :
10362 : /* Helper function for get_delta_difference which assumes FROM is a base
10363 : class of TO. Returns a delta for the conversion of pointer-to-member
10364 : of FROM to pointer-to-member of TO. If the conversion is invalid and
10365 : tf_error is not set in COMPLAIN returns error_mark_node, otherwise
10366 : returns zero. If FROM is not a base class of TO, returns NULL_TREE.
10367 : If C_CAST_P is true, this conversion is taking place as part of a
10368 : C-style cast. */
10369 :
10370 : static tree
10371 29043 : get_delta_difference_1 (tree from, tree to, bool c_cast_p,
10372 : tsubst_flags_t complain)
10373 : {
10374 29043 : tree binfo;
10375 29043 : base_kind kind;
10376 :
10377 57836 : binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
10378 : &kind, complain);
10379 :
10380 29043 : if (binfo == error_mark_node)
10381 : {
10382 51 : if (!(complain & tf_error))
10383 : return error_mark_node;
10384 :
10385 51 : inform (input_location, " in pointer to member function conversion");
10386 51 : return size_zero_node;
10387 : }
10388 28992 : else if (binfo)
10389 : {
10390 28844 : if (kind != bk_via_virtual)
10391 28751 : return BINFO_OFFSET (binfo);
10392 : else
10393 : /* FROM is a virtual base class of TO. Issue an error or warning
10394 : depending on whether or not this is a reinterpret cast. */
10395 : {
10396 93 : if (!(complain & tf_error))
10397 : return error_mark_node;
10398 :
10399 78 : error ("pointer to member conversion via virtual base %qT",
10400 78 : BINFO_TYPE (binfo_from_vbase (binfo)));
10401 :
10402 78 : return size_zero_node;
10403 : }
10404 : }
10405 : else
10406 : return NULL_TREE;
10407 : }
10408 :
10409 : /* Get difference in deltas for different pointer to member function
10410 : types. If the conversion is invalid and tf_error is not set in
10411 : COMPLAIN, returns error_mark_node, otherwise returns an integer
10412 : constant of type PTRDIFF_TYPE_NODE and its value is zero if the
10413 : conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
10414 : conversions as well. If C_CAST_P is true this conversion is taking
10415 : place as part of a C-style cast.
10416 :
10417 : Note that the naming of FROM and TO is kind of backwards; the return
10418 : value is what we add to a TO in order to get a FROM. They are named
10419 : this way because we call this function to find out how to convert from
10420 : a pointer to member of FROM to a pointer to member of TO. */
10421 :
10422 : static tree
10423 90598 : get_delta_difference (tree from, tree to,
10424 : bool allow_inverse_p,
10425 : bool c_cast_p, tsubst_flags_t complain)
10426 : {
10427 90598 : auto_diagnostic_group d;
10428 90598 : tree result;
10429 :
10430 90598 : if (same_type_ignoring_top_level_qualifiers_p (from, to))
10431 : /* Pointer to member of incomplete class is permitted*/
10432 61703 : result = size_zero_node;
10433 : else
10434 28895 : result = get_delta_difference_1 (from, to, c_cast_p, complain);
10435 :
10436 90598 : if (result == error_mark_node)
10437 : return error_mark_node;
10438 :
10439 90583 : if (!result)
10440 : {
10441 148 : if (!allow_inverse_p)
10442 : {
10443 0 : if (!(complain & tf_error))
10444 : return error_mark_node;
10445 :
10446 0 : error_not_base_type (from, to);
10447 0 : inform (input_location, " in pointer to member conversion");
10448 0 : result = size_zero_node;
10449 : }
10450 : else
10451 : {
10452 148 : result = get_delta_difference_1 (to, from, c_cast_p, complain);
10453 :
10454 148 : if (result == error_mark_node)
10455 : return error_mark_node;
10456 :
10457 148 : if (result)
10458 148 : result = size_diffop_loc (input_location,
10459 : size_zero_node, result);
10460 : else
10461 : {
10462 0 : if (!(complain & tf_error))
10463 : return error_mark_node;
10464 :
10465 0 : error_not_base_type (from, to);
10466 0 : inform (input_location, " in pointer to member conversion");
10467 0 : result = size_zero_node;
10468 : }
10469 : }
10470 : }
10471 :
10472 90583 : return convert_to_integer (ptrdiff_type_node, result);
10473 90598 : }
10474 :
10475 : /* Return a constructor for the pointer-to-member-function TYPE using
10476 : the other components as specified. */
10477 :
10478 : tree
10479 61952 : build_ptrmemfunc1 (tree type, tree delta, tree pfn)
10480 : {
10481 61952 : tree u = NULL_TREE;
10482 61952 : tree delta_field;
10483 61952 : tree pfn_field;
10484 61952 : vec<constructor_elt, va_gc> *v;
10485 :
10486 : /* Pull the FIELD_DECLs out of the type. */
10487 61952 : pfn_field = TYPE_FIELDS (type);
10488 61952 : delta_field = DECL_CHAIN (pfn_field);
10489 :
10490 : /* Make sure DELTA has the type we want. */
10491 61952 : delta = convert_and_check (input_location, delta_type_node, delta);
10492 :
10493 : /* Convert to the correct target type if necessary. */
10494 61952 : pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10495 :
10496 : /* Finish creating the initializer. */
10497 61952 : vec_alloc (v, 2);
10498 61952 : CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10499 61952 : CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10500 61952 : u = build_constructor (type, v);
10501 61952 : TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10502 61952 : TREE_STATIC (u) = (TREE_CONSTANT (u)
10503 61830 : && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10504 : != NULL_TREE)
10505 123782 : && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10506 : != NULL_TREE));
10507 61952 : return u;
10508 : }
10509 :
10510 : /* Build a constructor for a pointer to member function. It can be
10511 : used to initialize global variables, local variable, or used
10512 : as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10513 : want to be.
10514 :
10515 : If FORCE is nonzero, then force this conversion, even if
10516 : we would rather not do it. Usually set when using an explicit
10517 : cast. A C-style cast is being processed iff C_CAST_P is true.
10518 :
10519 : Return error_mark_node, if something goes wrong. */
10520 :
10521 : tree
10522 30364 : build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10523 : tsubst_flags_t complain)
10524 : {
10525 30364 : tree fn;
10526 30364 : tree pfn_type;
10527 30364 : tree to_type;
10528 :
10529 30364 : if (error_operand_p (pfn))
10530 0 : return error_mark_node;
10531 :
10532 30364 : pfn_type = TREE_TYPE (pfn);
10533 30364 : to_type = build_ptrmemfunc_type (type);
10534 :
10535 : /* Handle multiple conversions of pointer to member functions. */
10536 30364 : if (TYPE_PTRMEMFUNC_P (pfn_type))
10537 : {
10538 28594 : tree delta = NULL_TREE;
10539 28594 : tree npfn = NULL_TREE;
10540 28594 : tree n;
10541 :
10542 28594 : if (!force
10543 28594 : && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
10544 : LOOKUP_NORMAL, complain))
10545 : {
10546 0 : if (complain & tf_error)
10547 0 : error ("invalid conversion to type %qT from type %qT",
10548 : to_type, pfn_type);
10549 : else
10550 0 : return error_mark_node;
10551 : }
10552 :
10553 28594 : n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10554 28594 : TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10555 : force,
10556 : c_cast_p, complain);
10557 28594 : if (n == error_mark_node)
10558 : return error_mark_node;
10559 :
10560 28585 : STRIP_ANY_LOCATION_WRAPPER (pfn);
10561 :
10562 : /* We don't have to do any conversion to convert a
10563 : pointer-to-member to its own type. But, we don't want to
10564 : just return a PTRMEM_CST if there's an explicit cast; that
10565 : cast should make the expression an invalid template argument. */
10566 28585 : if (TREE_CODE (pfn) != PTRMEM_CST
10567 28585 : && same_type_p (to_type, pfn_type))
10568 : return pfn;
10569 :
10570 28585 : if (TREE_SIDE_EFFECTS (pfn))
10571 18 : pfn = save_expr (pfn);
10572 :
10573 : /* Obtain the function pointer and the current DELTA. */
10574 28585 : if (TREE_CODE (pfn) == PTRMEM_CST)
10575 28487 : expand_ptrmemfunc_cst (pfn, &delta, &npfn);
10576 : else
10577 : {
10578 98 : npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
10579 98 : delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
10580 : }
10581 :
10582 : /* Just adjust the DELTA field. */
10583 28585 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10584 : (TREE_TYPE (delta), ptrdiff_type_node));
10585 28585 : if (!integer_zerop (n))
10586 : {
10587 50 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
10588 : n = cp_build_binary_op (input_location,
10589 : LSHIFT_EXPR, n, integer_one_node,
10590 : complain);
10591 50 : delta = cp_build_binary_op (input_location,
10592 : PLUS_EXPR, delta, n, complain);
10593 : }
10594 28585 : return build_ptrmemfunc1 (to_type, delta, npfn);
10595 : }
10596 :
10597 : /* Handle null pointer to member function conversions. */
10598 1770 : if (null_ptr_cst_p (pfn))
10599 : {
10600 650 : pfn = cp_build_c_cast (input_location,
10601 650 : TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10602 : pfn, complain);
10603 650 : return build_ptrmemfunc1 (to_type,
10604 : integer_zero_node,
10605 650 : pfn);
10606 : }
10607 :
10608 1120 : if (type_unknown_p (pfn))
10609 0 : return instantiate_type (type, pfn, complain);
10610 :
10611 1120 : fn = TREE_OPERAND (pfn, 0);
10612 1120 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10613 : /* In a template, we will have preserved the
10614 : OFFSET_REF. */
10615 : || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10616 1120 : return make_ptrmem_cst (to_type, fn);
10617 : }
10618 :
10619 : /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10620 : given by CST.
10621 :
10622 : ??? There is no consistency as to the types returned for the above
10623 : values. Some code acts as if it were a sizetype and some as if it were
10624 : integer_type_node. */
10625 :
10626 : void
10627 61606 : expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10628 : {
10629 61606 : tree type = TREE_TYPE (cst);
10630 61606 : tree fn = PTRMEM_CST_MEMBER (cst);
10631 61606 : tree ptr_class, fn_class;
10632 :
10633 61606 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10634 :
10635 : /* The class that the function belongs to. */
10636 61606 : fn_class = DECL_CONTEXT (fn);
10637 :
10638 : /* The class that we're creating a pointer to member of. */
10639 61606 : ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10640 :
10641 : /* First, calculate the adjustment to the function's class. */
10642 61606 : *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10643 : /*c_cast_p=*/0, tf_warning_or_error);
10644 :
10645 61606 : if (!DECL_VIRTUAL_P (fn))
10646 : {
10647 60493 : tree t = build_addr_func (fn, tf_warning_or_error);
10648 60493 : if (TREE_CODE (t) == ADDR_EXPR)
10649 60493 : SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10650 60493 : *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10651 : }
10652 : else
10653 : {
10654 : /* If we're dealing with a virtual function, we have to adjust 'this'
10655 : again, to point to the base which provides the vtable entry for
10656 : fn; the call will do the opposite adjustment. */
10657 1113 : tree orig_class = DECL_CONTEXT (fn);
10658 1113 : tree binfo = binfo_or_else (orig_class, fn_class);
10659 1113 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10660 : *delta, BINFO_OFFSET (binfo));
10661 :
10662 : /* We set PFN to the vtable offset at which the function can be
10663 : found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10664 : case delta is shifted left, and then incremented). */
10665 1113 : *pfn = DECL_VINDEX (fn);
10666 1113 : *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10667 : TYPE_SIZE_UNIT (vtable_entry_type));
10668 :
10669 1113 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10670 : {
10671 1113 : case ptrmemfunc_vbit_in_pfn:
10672 1113 : *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10673 : integer_one_node);
10674 1113 : break;
10675 :
10676 : case ptrmemfunc_vbit_in_delta:
10677 : *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10678 : *delta, integer_one_node);
10679 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10680 : *delta, integer_one_node);
10681 : break;
10682 :
10683 : default:
10684 : gcc_unreachable ();
10685 : }
10686 :
10687 1113 : *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10688 : }
10689 61606 : }
10690 :
10691 : /* Return an expression for PFN from the pointer-to-member function
10692 : given by T. */
10693 :
10694 : static tree
10695 87000 : pfn_from_ptrmemfunc (tree t)
10696 : {
10697 87000 : if (TREE_CODE (t) == PTRMEM_CST)
10698 : {
10699 201 : tree delta;
10700 201 : tree pfn;
10701 :
10702 201 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10703 201 : if (pfn)
10704 201 : return pfn;
10705 : }
10706 :
10707 86799 : return build_ptrmemfunc_access_expr (t, pfn_identifier);
10708 : }
10709 :
10710 : /* Return an expression for DELTA from the pointer-to-member function
10711 : given by T. */
10712 :
10713 : static tree
10714 87000 : delta_from_ptrmemfunc (tree t)
10715 : {
10716 87000 : if (TREE_CODE (t) == PTRMEM_CST)
10717 : {
10718 201 : tree delta;
10719 201 : tree pfn;
10720 :
10721 201 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10722 201 : if (delta)
10723 201 : return delta;
10724 : }
10725 :
10726 86799 : return build_ptrmemfunc_access_expr (t, delta_identifier);
10727 : }
10728 :
10729 : /* Convert value RHS to type TYPE as preparation for an assignment to
10730 : an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10731 : implicit conversion is. If FNDECL is non-NULL, we are doing the
10732 : conversion in order to pass the PARMNUMth argument of FNDECL.
10733 : If FNDECL is NULL, we are doing the conversion in function pointer
10734 : argument passing, conversion in initialization, etc. */
10735 :
10736 : static tree
10737 184253705 : convert_for_assignment (tree type, tree rhs,
10738 : impl_conv_rhs errtype, tree fndecl, int parmnum,
10739 : tsubst_flags_t complain, int flags)
10740 : {
10741 184253705 : tree rhstype;
10742 184253705 : enum tree_code coder;
10743 :
10744 184253705 : location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10745 184253705 : bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10746 : /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10747 : but preserve location wrappers. */
10748 184253705 : if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10749 184253705 : && !location_wrapper_p (rhs))
10750 8542 : rhs = TREE_OPERAND (rhs, 0);
10751 :
10752 : /* Handle [dcl.init.list] direct-list-initialization from
10753 : single element of enumeration with a fixed underlying type. */
10754 184253705 : if (is_direct_enum_init (type, rhs))
10755 : {
10756 20 : tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10757 20 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10758 : {
10759 11 : warning_sentinel w (warn_useless_cast);
10760 11 : warning_sentinel w2 (warn_ignored_qualifiers);
10761 11 : rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10762 11 : }
10763 : else
10764 9 : rhs = error_mark_node;
10765 : }
10766 :
10767 184253705 : rhstype = TREE_TYPE (rhs);
10768 184253705 : coder = TREE_CODE (rhstype);
10769 :
10770 1351610 : if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10771 185605209 : && vector_types_convertible_p (type, rhstype, true))
10772 : {
10773 1351495 : rhs = mark_rvalue_use (rhs);
10774 1351495 : return convert (type, rhs);
10775 : }
10776 :
10777 182902210 : if (rhs == error_mark_node || rhstype == error_mark_node)
10778 : return error_mark_node;
10779 182902201 : if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10780 : return error_mark_node;
10781 :
10782 : /* The RHS of an assignment cannot have void type. */
10783 182902201 : if (coder == VOID_TYPE)
10784 : {
10785 50 : if (complain & tf_error)
10786 30 : error_at (rhs_loc, "void value not ignored as it ought to be");
10787 50 : return error_mark_node;
10788 : }
10789 :
10790 182902151 : if (c_dialect_objc ())
10791 : {
10792 0 : int parmno;
10793 0 : tree selector;
10794 0 : tree rname = fndecl;
10795 :
10796 0 : switch (errtype)
10797 : {
10798 : case ICR_ASSIGN:
10799 : parmno = -1;
10800 : break;
10801 0 : case ICR_INIT:
10802 0 : parmno = -2;
10803 0 : break;
10804 0 : default:
10805 0 : selector = objc_message_selector ();
10806 0 : parmno = parmnum;
10807 0 : if (selector && parmno > 1)
10808 : {
10809 0 : rname = selector;
10810 0 : parmno -= 1;
10811 : }
10812 : }
10813 :
10814 0 : if (objc_compare_types (type, rhstype, parmno, rname))
10815 : {
10816 0 : rhs = mark_rvalue_use (rhs);
10817 0 : return convert (type, rhs);
10818 : }
10819 : }
10820 :
10821 : /* [expr.ass]
10822 :
10823 : The expression is implicitly converted (clause _conv_) to the
10824 : cv-unqualified type of the left operand.
10825 :
10826 : We allow bad conversions here because by the time we get to this point
10827 : we are committed to doing the conversion. If we end up doing a bad
10828 : conversion, convert_like will complain. */
10829 182902151 : if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10830 : {
10831 : /* When -Wno-pmf-conversions is use, we just silently allow
10832 : conversions from pointers-to-members to plain pointers. If
10833 : the conversion doesn't work, cp_convert will complain. */
10834 1640 : if (!warn_pmf2ptr
10835 9 : && TYPE_PTR_P (type)
10836 1649 : && TYPE_PTRMEMFUNC_P (rhstype))
10837 9 : rhs = cp_convert (strip_top_quals (type), rhs, complain);
10838 : else
10839 : {
10840 1631 : if (complain & tf_error)
10841 : {
10842 1316 : auto_diagnostic_group d;
10843 :
10844 : /* If the right-hand side has unknown type, then it is an
10845 : overloaded function. Call instantiate_type to get error
10846 : messages. */
10847 1316 : if (rhstype == unknown_type_node)
10848 : {
10849 225 : tree r = instantiate_type (type, rhs, tf_warning_or_error);
10850 : /* -fpermissive might allow this; recurse. */
10851 225 : if (!seen_error ())
10852 15 : return convert_for_assignment (type, r, errtype, fndecl,
10853 : parmnum, complain, flags);
10854 : }
10855 1091 : else if (fndecl)
10856 92 : complain_about_bad_argument (rhs_loc,
10857 : rhstype, type,
10858 : fndecl, parmnum);
10859 : else
10860 : {
10861 999 : range_label_for_type_mismatch label (rhstype, type);
10862 999 : gcc_rich_location richloc
10863 : (rhs_loc,
10864 : has_loc ? &label : NULL,
10865 999 : has_loc ? highlight_colors::percent_h : NULL);
10866 :
10867 999 : switch (errtype)
10868 : {
10869 0 : case ICR_DEFAULT_ARGUMENT:
10870 0 : error_at (&richloc,
10871 : "cannot convert %qH to %qI in default argument",
10872 : rhstype, type);
10873 0 : break;
10874 6 : case ICR_ARGPASS:
10875 6 : error_at (&richloc,
10876 : "cannot convert %qH to %qI in argument passing",
10877 : rhstype, type);
10878 6 : break;
10879 0 : case ICR_CONVERTING:
10880 0 : error_at (&richloc, "cannot convert %qH to %qI",
10881 : rhstype, type);
10882 0 : break;
10883 561 : case ICR_INIT:
10884 561 : error_at (&richloc,
10885 : "cannot convert %qH to %qI in initialization",
10886 : rhstype, type);
10887 561 : break;
10888 18 : case ICR_RETURN:
10889 18 : error_at (&richloc, "cannot convert %qH to %qI in return",
10890 : rhstype, type);
10891 18 : break;
10892 414 : case ICR_ASSIGN:
10893 414 : error_at (&richloc,
10894 : "cannot convert %qH to %qI in assignment",
10895 : rhstype, type);
10896 414 : break;
10897 0 : default:
10898 0 : gcc_unreachable();
10899 : }
10900 999 : }
10901 :
10902 : /* See if we can be more helpful. */
10903 1301 : maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
10904 :
10905 1301 : if (TYPE_PTR_P (rhstype)
10906 413 : && TYPE_PTR_P (type)
10907 398 : && CLASS_TYPE_P (TREE_TYPE (rhstype))
10908 280 : && CLASS_TYPE_P (TREE_TYPE (type))
10909 1365 : && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10910 3 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10911 : (TREE_TYPE (rhstype))),
10912 3 : "class type %qT is incomplete", TREE_TYPE (rhstype));
10913 1316 : }
10914 1616 : return error_mark_node;
10915 : }
10916 : }
10917 182900520 : if (warn_suggest_attribute_format)
10918 : {
10919 2383 : const enum tree_code codel = TREE_CODE (type);
10920 2383 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10921 596 : && coder == codel
10922 508 : && check_missing_format_attribute (type, rhstype)
10923 2401 : && (complain & tf_warning))
10924 18 : switch (errtype)
10925 : {
10926 0 : case ICR_ARGPASS:
10927 0 : case ICR_DEFAULT_ARGUMENT:
10928 0 : if (fndecl)
10929 0 : warning (OPT_Wsuggest_attribute_format,
10930 : "parameter %qP of %qD might be a candidate "
10931 : "for a format attribute", parmnum, fndecl);
10932 : else
10933 0 : warning (OPT_Wsuggest_attribute_format,
10934 : "parameter might be a candidate "
10935 : "for a format attribute");
10936 : break;
10937 0 : case ICR_CONVERTING:
10938 0 : warning (OPT_Wsuggest_attribute_format,
10939 : "target of conversion might be a candidate "
10940 : "for a format attribute");
10941 0 : break;
10942 6 : case ICR_INIT:
10943 6 : warning (OPT_Wsuggest_attribute_format,
10944 : "target of initialization might be a candidate "
10945 : "for a format attribute");
10946 6 : break;
10947 6 : case ICR_RETURN:
10948 6 : warning (OPT_Wsuggest_attribute_format,
10949 : "return type might be a candidate "
10950 : "for a format attribute");
10951 6 : break;
10952 6 : case ICR_ASSIGN:
10953 6 : warning (OPT_Wsuggest_attribute_format,
10954 : "left-hand side of assignment might be a candidate "
10955 : "for a format attribute");
10956 6 : break;
10957 0 : default:
10958 0 : gcc_unreachable();
10959 : }
10960 : }
10961 :
10962 182900520 : if (TREE_CODE (type) == BOOLEAN_TYPE)
10963 37674191 : maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
10964 :
10965 182900520 : if (complain & tf_warning)
10966 180310271 : warn_for_address_of_packed_member (type, rhs);
10967 :
10968 182900520 : return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10969 182900520 : complain, flags);
10970 : }
10971 :
10972 : /* Convert RHS to be of type TYPE.
10973 : If EXP is nonzero, it is the target of the initialization.
10974 : ERRTYPE indicates what kind of error the implicit conversion is.
10975 :
10976 : Two major differences between the behavior of
10977 : `convert_for_assignment' and `convert_for_initialization'
10978 : are that references are bashed in the former, while
10979 : copied in the latter, and aggregates are assigned in
10980 : the former (operator=) while initialized in the
10981 : latter (X(X&)).
10982 :
10983 : If using constructor make sure no conversion operator exists, if one does
10984 : exist, an ambiguity exists. */
10985 :
10986 : tree
10987 170428498 : convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10988 : impl_conv_rhs errtype, tree fndecl, int parmnum,
10989 : tsubst_flags_t complain)
10990 : {
10991 170428498 : enum tree_code codel = TREE_CODE (type);
10992 170428498 : tree rhstype;
10993 170428498 : enum tree_code coder;
10994 :
10995 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10996 : Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10997 170428498 : if (TREE_CODE (rhs) == NOP_EXPR
10998 5976576 : && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10999 170922610 : && codel != REFERENCE_TYPE)
11000 494112 : rhs = TREE_OPERAND (rhs, 0);
11001 :
11002 170428498 : if (type == error_mark_node
11003 170428455 : || rhs == error_mark_node
11004 340856884 : || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
11005 : return error_mark_node;
11006 :
11007 170428386 : if (MAYBE_CLASS_TYPE_P (non_reference (type)))
11008 : ;
11009 155963207 : else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
11010 2355439 : && TREE_CODE (type) != ARRAY_TYPE
11011 2355439 : && (!TYPE_REF_P (type)
11012 1089 : || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
11013 153608854 : || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
11014 25931 : && !TYPE_REFFN_P (type))
11015 309547324 : || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
11016 2379096 : rhs = decay_conversion (rhs, complain);
11017 :
11018 170428386 : rhstype = TREE_TYPE (rhs);
11019 170428386 : coder = TREE_CODE (rhstype);
11020 :
11021 170428386 : if (coder == ERROR_MARK)
11022 9 : return error_mark_node;
11023 :
11024 : /* We accept references to incomplete types, so we can
11025 : return here before checking if RHS is of complete type. */
11026 :
11027 170428377 : if (codel == REFERENCE_TYPE)
11028 : {
11029 6050686 : auto_diagnostic_group d;
11030 : /* This should eventually happen in convert_arguments. */
11031 6050686 : int savew = 0, savee = 0;
11032 :
11033 6050686 : if (fndecl)
11034 298327 : savew = warningcount + werrorcount, savee = errorcount;
11035 6050686 : rhs = initialize_reference (type, rhs, flags, complain);
11036 :
11037 6050686 : if (fndecl
11038 6050686 : && (warningcount + werrorcount > savew || errorcount > savee))
11039 36 : inform (get_fndecl_argument_location (fndecl, parmnum),
11040 : "in passing argument %P of %qD", parmnum, fndecl);
11041 6050686 : return rhs;
11042 6050686 : }
11043 :
11044 164377691 : if (exp != 0)
11045 4301026 : exp = require_complete_type (exp, complain);
11046 164377691 : if (exp == error_mark_node)
11047 : return error_mark_node;
11048 :
11049 164377691 : type = complete_type (type);
11050 :
11051 164377691 : if (DIRECT_INIT_EXPR_P (type, rhs))
11052 : /* Don't try to do copy-initialization if we already have
11053 : direct-initialization. */
11054 : return rhs;
11055 :
11056 164376266 : if (MAYBE_CLASS_TYPE_P (type))
11057 10371609 : return perform_implicit_conversion_flags (type, rhs, complain, flags);
11058 :
11059 154004657 : return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
11060 154004657 : complain, flags);
11061 : }
11062 :
11063 : /* If RETVAL is the address of, or a reference to, a local variable or
11064 : temporary give an appropriate warning and return true. */
11065 :
11066 : static bool
11067 43929228 : maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
11068 : {
11069 43930239 : tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
11070 43930239 : tree whats_returned = fold_for_warn (retval);
11071 43930239 : if (!loc)
11072 43930239 : loc = cp_expr_loc_or_input_loc (retval);
11073 :
11074 53534662 : for (;;)
11075 : {
11076 53534662 : if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
11077 786356 : whats_returned = TREE_OPERAND (whats_returned, 1);
11078 52748306 : else if (CONVERT_EXPR_P (whats_returned)
11079 43948065 : || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
11080 8818067 : whats_returned = TREE_OPERAND (whats_returned, 0);
11081 : else
11082 : break;
11083 : }
11084 :
11085 43930239 : if (TREE_CODE (whats_returned) == TARGET_EXPR
11086 43930239 : && is_std_init_list (TREE_TYPE (whats_returned)))
11087 : {
11088 16 : tree init = TARGET_EXPR_INITIAL (whats_returned);
11089 16 : if (TREE_CODE (init) == CONSTRUCTOR)
11090 : /* Pull out the array address. */
11091 7 : whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
11092 9 : else if (TREE_CODE (init) == INDIRECT_REF)
11093 : /* The source of a trivial copy looks like *(T*)&var. */
11094 6 : whats_returned = TREE_OPERAND (init, 0);
11095 : else
11096 : return false;
11097 13 : STRIP_NOPS (whats_returned);
11098 : }
11099 :
11100 : /* As a special case, we handle a call to std::move or std::forward. */
11101 43930236 : if (TREE_CODE (whats_returned) == CALL_EXPR
11102 43930236 : && (is_std_move_p (whats_returned)
11103 10463316 : || is_std_forward_p (whats_returned)))
11104 : {
11105 999 : tree arg = CALL_EXPR_ARG (whats_returned, 0);
11106 999 : return maybe_warn_about_returning_address_of_local (arg, loc);
11107 : }
11108 :
11109 43929237 : if (TREE_CODE (whats_returned) != ADDR_EXPR)
11110 : return false;
11111 924538 : whats_returned = TREE_OPERAND (whats_returned, 0);
11112 :
11113 924538 : while (TREE_CODE (whats_returned) == COMPONENT_REF
11114 1795742 : || TREE_CODE (whats_returned) == ARRAY_REF)
11115 871204 : whats_returned = TREE_OPERAND (whats_returned, 0);
11116 :
11117 924538 : if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
11118 924538 : || TREE_CODE (whats_returned) == TARGET_EXPR)
11119 : {
11120 50 : if (TYPE_REF_P (valtype))
11121 : /* P2748 made this an error in C++26. */
11122 72 : emit_diagnostic ((cxx_dialect >= cxx26
11123 : ? diagnostics::kind::permerror
11124 : : diagnostics::kind::warning),
11125 41 : loc, OPT_Wreturn_local_addr,
11126 : "returning reference to temporary");
11127 9 : else if (TYPE_PTR_P (valtype))
11128 3 : warning_at (loc, OPT_Wreturn_local_addr,
11129 : "returning pointer to temporary");
11130 6 : else if (is_std_init_list (valtype))
11131 6 : warning_at (loc, OPT_Winit_list_lifetime,
11132 : "returning temporary %<initializer_list%> does not extend "
11133 : "the lifetime of the underlying array");
11134 50 : return true;
11135 : }
11136 :
11137 924488 : STRIP_ANY_LOCATION_WRAPPER (whats_returned);
11138 :
11139 924488 : if (DECL_P (whats_returned)
11140 119484 : && DECL_NAME (whats_returned)
11141 119484 : && DECL_FUNCTION_SCOPE_P (whats_returned)
11142 12423 : && !is_capture_proxy (whats_returned)
11143 936881 : && !(TREE_STATIC (whats_returned)
11144 155 : || TREE_PUBLIC (whats_returned)))
11145 : {
11146 108 : if (DECL_DECOMPOSITION_P (whats_returned)
11147 39 : && !DECL_DECOMP_IS_BASE (whats_returned)
11148 194 : && DECL_HAS_VALUE_EXPR_P (whats_returned))
11149 : {
11150 : /* When returning address of a structured binding, if the structured
11151 : binding is not a reference, continue normally, if it is a
11152 : reference, recurse on the initializer of the structured
11153 : binding. */
11154 39 : tree base = DECL_DECOMP_BASE (whats_returned);
11155 39 : if (TYPE_REF_P (TREE_TYPE (base)))
11156 : {
11157 15 : if (tree init = DECL_INITIAL (base))
11158 : return maybe_warn_about_returning_address_of_local (init, loc);
11159 : else
11160 : return false;
11161 : }
11162 : }
11163 140 : bool w = false;
11164 140 : auto_diagnostic_group d;
11165 140 : if (TYPE_REF_P (valtype))
11166 89 : w = warning_at (loc, OPT_Wreturn_local_addr,
11167 : "reference to local variable %qD returned",
11168 : whats_returned);
11169 51 : else if (is_std_init_list (valtype))
11170 3 : w = warning_at (loc, OPT_Winit_list_lifetime,
11171 : "returning local %<initializer_list%> variable %qD "
11172 : "does not extend the lifetime of the underlying array",
11173 : whats_returned);
11174 48 : else if (POINTER_TYPE_P (valtype)
11175 42 : && TREE_CODE (whats_returned) == LABEL_DECL)
11176 6 : w = warning_at (loc, OPT_Wreturn_local_addr,
11177 : "address of label %qD returned",
11178 : whats_returned);
11179 42 : else if (POINTER_TYPE_P (valtype))
11180 36 : w = warning_at (loc, OPT_Wreturn_local_addr,
11181 : "address of local variable %qD returned",
11182 : whats_returned);
11183 134 : if (w)
11184 111 : inform (DECL_SOURCE_LOCATION (whats_returned),
11185 : "declared here");
11186 140 : return true;
11187 140 : }
11188 :
11189 : return false;
11190 : }
11191 :
11192 : /* Returns true if DECL is in the std namespace. */
11193 :
11194 : bool
11195 87290188 : decl_in_std_namespace_p (tree decl)
11196 : {
11197 101458666 : while (decl)
11198 : {
11199 101038368 : decl = decl_namespace_context (decl);
11200 101038368 : if (DECL_NAMESPACE_STD_P (decl))
11201 : return true;
11202 : /* Allow inline namespaces inside of std namespace, e.g. with
11203 : --enable-symvers=gnu-versioned-namespace std::forward would be
11204 : actually std::_8::forward. */
11205 47759235 : if (!DECL_NAMESPACE_INLINE_P (decl))
11206 : return false;
11207 14168478 : decl = CP_DECL_CONTEXT (decl);
11208 : }
11209 : return false;
11210 : }
11211 :
11212 : /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
11213 :
11214 : static bool
11215 10463316 : is_std_forward_p (tree fn)
11216 : {
11217 : /* std::forward only takes one argument. */
11218 10463316 : if (call_expr_nargs (fn) != 1)
11219 : return false;
11220 :
11221 4849431 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
11222 4849431 : if (!decl_in_std_namespace_p (fndecl))
11223 : return false;
11224 :
11225 1823963 : tree name = DECL_NAME (fndecl);
11226 1823963 : return name && id_equal (name, "forward");
11227 : }
11228 :
11229 : /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
11230 :
11231 : static bool
11232 10470689 : is_std_move_p (tree fn)
11233 : {
11234 : /* std::move only takes one argument. */
11235 10470689 : if (call_expr_nargs (fn) != 1)
11236 : return false;
11237 :
11238 4856097 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
11239 4856097 : if (!decl_in_std_namespace_p (fndecl))
11240 : return false;
11241 :
11242 1830147 : tree name = DECL_NAME (fndecl);
11243 1830147 : return name && id_equal (name, "move");
11244 : }
11245 :
11246 : /* Returns true if RETVAL is a good candidate for the NRVO as per
11247 : [class.copy.elision]. FUNCTYPE is the type the function is declared
11248 : to return. */
11249 :
11250 : static bool
11251 56863192 : can_do_nrvo_p (tree retval, tree functype)
11252 : {
11253 56863192 : if (functype == error_mark_node)
11254 : return false;
11255 56863146 : if (retval)
11256 55147101 : STRIP_ANY_LOCATION_WRAPPER (retval);
11257 56863146 : tree result = DECL_RESULT (current_function_decl);
11258 56863146 : return (retval != NULL_TREE
11259 55147101 : && !processing_template_decl
11260 : /* Must be a local, automatic variable. */
11261 45431078 : && VAR_P (retval)
11262 3988724 : && DECL_CONTEXT (retval) == current_function_decl
11263 2915540 : && !TREE_STATIC (retval)
11264 : /* And not a lambda or anonymous union proxy. */
11265 2912629 : && !DECL_HAS_VALUE_EXPR_P (retval)
11266 2912363 : && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
11267 : /* The cv-unqualified type of the returned value must be the
11268 : same as the cv-unqualified return type of the
11269 : function. */
11270 2912240 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
11271 : TYPE_MAIN_VARIANT (functype))
11272 : /* And the returned value must be non-volatile. */
11273 59751821 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
11274 : }
11275 :
11276 : /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
11277 : would otherwise return in memory. */
11278 :
11279 : static bool
11280 56862822 : want_nrvo_p (tree retval, tree functype)
11281 : {
11282 56862822 : return (can_do_nrvo_p (retval, functype)
11283 56862822 : && aggregate_value_p (functype, current_function_decl));
11284 : }
11285 :
11286 : /* Like can_do_nrvo_p, but we check if we're trying to move a class
11287 : prvalue. */
11288 :
11289 : static bool
11290 1383 : can_elide_copy_prvalue_p (tree retval, tree functype)
11291 : {
11292 1383 : if (functype == error_mark_node)
11293 : return false;
11294 1383 : if (retval)
11295 1383 : STRIP_ANY_LOCATION_WRAPPER (retval);
11296 1383 : return (retval != NULL_TREE
11297 1383 : && !glvalue_p (retval)
11298 135 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
11299 : TYPE_MAIN_VARIANT (functype))
11300 1491 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
11301 : }
11302 :
11303 : /* If we should treat RETVAL, an expression being returned, as if it were
11304 : designated by an rvalue, returns it adjusted accordingly; otherwise, returns
11305 : NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
11306 : context (rather than throw). */
11307 :
11308 : tree
11309 11523999 : treat_lvalue_as_rvalue_p (tree expr, bool return_p)
11310 : {
11311 11523999 : if (cxx_dialect == cxx98)
11312 : return NULL_TREE;
11313 :
11314 11514617 : tree retval = expr;
11315 11514617 : STRIP_ANY_LOCATION_WRAPPER (retval);
11316 11514617 : if (REFERENCE_REF_P (retval))
11317 570113 : retval = TREE_OPERAND (retval, 0);
11318 :
11319 : /* An implicitly movable entity is a variable of automatic storage duration
11320 : that is either a non-volatile object or (C++20) an rvalue reference to a
11321 : non-volatile object type. */
11322 11514617 : if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
11323 10235075 : || TREE_CODE (retval) == PARM_DECL)
11324 1847922 : && !TREE_STATIC (retval)
11325 1753486 : && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
11326 1753481 : && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
11327 127397 : || (cxx_dialect >= cxx20
11328 126849 : && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
11329 9887686 : return NULL_TREE;
11330 :
11331 : /* If the expression in a return or co_return statement is a (possibly
11332 : parenthesized) id-expression that names an implicitly movable entity
11333 : declared in the body or parameter-declaration-clause of the innermost
11334 : enclosing function or lambda-expression, */
11335 1626931 : if (return_p)
11336 : {
11337 1617464 : if (DECL_CONTEXT (retval) != current_function_decl)
11338 : return NULL_TREE;
11339 1617397 : expr = move (expr);
11340 1617397 : if (expr == error_mark_node)
11341 : return NULL_TREE;
11342 1617395 : return set_implicit_rvalue_p (expr);
11343 : }
11344 :
11345 : /* if the id-expression (possibly parenthesized) is the operand of
11346 : a throw-expression, and names an implicitly movable entity that belongs
11347 : to a scope that does not contain the compound-statement of the innermost
11348 : lambda-expression, try-block, or function-try-block (if any) whose
11349 : compound-statement or ctor-initializer contains the throw-expression. */
11350 :
11351 : /* C++20 added move on throw of parms. */
11352 9467 : if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
11353 : return NULL_TREE;
11354 :
11355 : /* We don't check for lambda-expression here, because we should not get past
11356 : the DECL_HAS_VALUE_EXPR_P check above. */
11357 9451 : for (cp_binding_level *b = current_binding_level;
11358 9547 : b->kind != sk_namespace; b = b->level_chain)
11359 : {
11360 9545 : for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
11361 103 : if (decl == retval)
11362 91 : return set_implicit_rvalue_p (move (expr));
11363 9442 : if (b->kind == sk_try)
11364 : return NULL_TREE;
11365 : }
11366 :
11367 14 : return set_implicit_rvalue_p (move (expr));
11368 : }
11369 :
11370 : /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
11371 : is true). EXPR is the std::move expression; TYPE is the type of the object
11372 : being initialized. */
11373 :
11374 : void
11375 156101380 : maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
11376 : {
11377 156101380 : if (!(warn_pessimizing_move || warn_redundant_move))
11378 : return;
11379 :
11380 4555758 : const location_t loc = cp_expr_loc_or_input_loc (expr);
11381 :
11382 : /* C++98 doesn't know move. */
11383 4555758 : if (cxx_dialect < cxx11)
11384 : return;
11385 :
11386 : /* Wait until instantiation time, since we can't gauge if we should do
11387 : the NRVO until then. */
11388 4493113 : if (processing_template_decl)
11389 : return;
11390 :
11391 : /* This is only interesting for class types. */
11392 4404192 : if (!CLASS_TYPE_P (type))
11393 : return;
11394 :
11395 110973 : bool wrapped_p = false;
11396 : /* A a = std::move (A()); */
11397 110973 : if (TREE_CODE (expr) == TREE_LIST)
11398 : {
11399 8896 : if (list_length (expr) == 1)
11400 : {
11401 5971 : expr = TREE_VALUE (expr);
11402 5971 : wrapped_p = true;
11403 : }
11404 : else
11405 : return;
11406 : }
11407 : /* A a = {std::move (A())};
11408 : A a{std::move (A())}; */
11409 102077 : else if (TREE_CODE (expr) == CONSTRUCTOR)
11410 : {
11411 7927 : if (CONSTRUCTOR_NELTS (expr) == 1)
11412 : {
11413 887 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
11414 887 : wrapped_p = true;
11415 : }
11416 : else
11417 : return;
11418 : }
11419 :
11420 : /* First, check if this is a call to std::move. */
11421 6743 : if (!REFERENCE_REF_P (expr)
11422 105730 : || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
11423 : return;
11424 2626 : tree fn = TREE_OPERAND (expr, 0);
11425 2626 : if (!is_std_move_p (fn))
11426 : return;
11427 2026 : tree arg = CALL_EXPR_ARG (fn, 0);
11428 2026 : if (TREE_CODE (arg) != NOP_EXPR)
11429 : return;
11430 : /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
11431 : and implicitly-movable warnings. */
11432 2026 : if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
11433 : {
11434 1383 : arg = TREE_OPERAND (arg, 0);
11435 1383 : arg = TREE_OPERAND (arg, 0);
11436 1383 : arg = convert_from_reference (arg);
11437 1383 : if (can_elide_copy_prvalue_p (arg, type))
11438 : {
11439 108 : auto_diagnostic_group d;
11440 108 : if (warning_at (loc, OPT_Wpessimizing_move,
11441 : "moving a temporary object prevents copy elision"))
11442 108 : inform (loc, "remove %<std::move%> call");
11443 108 : }
11444 : /* The rest of the warnings is only relevant for when we are returning
11445 : from a function. */
11446 1383 : if (!return_p)
11447 : return;
11448 :
11449 370 : tree moved;
11450 : /* Warn if we could do copy elision were it not for the move. */
11451 370 : if (can_do_nrvo_p (arg, type))
11452 : {
11453 48 : auto_diagnostic_group d;
11454 48 : if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
11455 48 : && warning_at (loc, OPT_Wpessimizing_move,
11456 : "moving a local object in a return statement "
11457 : "prevents copy elision"))
11458 42 : inform (loc, "remove %<std::move%> call");
11459 48 : }
11460 : /* Warn if the move is redundant. It is redundant when we would
11461 : do maybe-rvalue overload resolution even without std::move. */
11462 322 : else if (warn_redundant_move
11463 : /* This doesn't apply for return {std::move (t)};. */
11464 224 : && !wrapped_p
11465 221 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11466 418 : && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
11467 : {
11468 : /* Make sure that overload resolution would actually succeed
11469 : if we removed the std::move call. */
11470 54 : tree t = convert_for_initialization (NULL_TREE, type,
11471 : moved,
11472 : (LOOKUP_NORMAL
11473 : | LOOKUP_ONLYCONVERTING),
11474 : ICR_RETURN, NULL_TREE, 0,
11475 : tf_none);
11476 : /* If this worked, implicit rvalue would work, so the call to
11477 : std::move is redundant. */
11478 54 : if (t != error_mark_node)
11479 : {
11480 54 : auto_diagnostic_group d;
11481 54 : if (warning_at (loc, OPT_Wredundant_move,
11482 : "redundant move in return statement"))
11483 54 : inform (loc, "remove %<std::move%> call");
11484 54 : }
11485 : }
11486 : }
11487 : /* Also try to warn about redundant std::move in code such as
11488 : T f (const T& t)
11489 : {
11490 : return std::move(t);
11491 : }
11492 : for which EXPR will be something like
11493 : *std::move<const T&> ((const struct T &) (const struct T *) t)
11494 : and where the std::move does nothing if T does not have a T(const T&&)
11495 : constructor, because the argument is const. It will not use T(T&&)
11496 : because that would mean losing the const. */
11497 643 : else if (warn_redundant_move
11498 521 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11499 66 : && TYPE_REF_P (TREE_TYPE (arg))
11500 709 : && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
11501 : {
11502 18 : tree rtype = TREE_TYPE (TREE_TYPE (arg));
11503 18 : if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
11504 9 : return;
11505 : /* Check for the unlikely case there's T(const T&&) (we don't care if
11506 : it's deleted). */
11507 54 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
11508 30 : if (move_fn_p (fn))
11509 : {
11510 15 : tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11511 15 : if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
11512 6 : return;
11513 : }
11514 9 : auto_diagnostic_group d;
11515 18 : if (return_p
11516 9 : ? warning_at (loc, OPT_Wredundant_move,
11517 : "redundant move in return statement")
11518 9 : : warning_at (loc, OPT_Wredundant_move,
11519 : "redundant move in initialization"))
11520 9 : inform (loc, "remove %<std::move%> call");
11521 9 : }
11522 : }
11523 :
11524 : /* Check that returning RETVAL from the current function is valid.
11525 : Return an expression explicitly showing all conversions required to
11526 : change RETVAL into the function return type, and to assign it to
11527 : the DECL_RESULT for the function. Set *NO_WARNING to true if
11528 : code reaches end of non-void function warning shouldn't be issued
11529 : on this RETURN_EXPR. Set *DANGLING to true if code returns the
11530 : address of a local variable. */
11531 :
11532 : tree
11533 131341477 : check_return_expr (tree retval, bool *no_warning, bool *dangling)
11534 : {
11535 131341477 : tree result;
11536 : /* The type actually returned by the function. */
11537 131341477 : tree valtype;
11538 : /* The type the function is declared to return, or void if
11539 : the declared type is incomplete. */
11540 131341477 : tree functype;
11541 131341477 : int fn_returns_value_p;
11542 131341477 : location_t loc = cp_expr_loc_or_input_loc (retval);
11543 :
11544 131341477 : *no_warning = false;
11545 131341477 : *dangling = false;
11546 :
11547 : /* A `volatile' function is one that isn't supposed to return, ever.
11548 : (This is a G++ extension, used to get better code for functions
11549 : that call the `volatile' function.) */
11550 131341477 : if (TREE_THIS_VOLATILE (current_function_decl))
11551 12 : warning (0, "function declared %<noreturn%> has a %<return%> statement");
11552 :
11553 : /* Check for various simple errors. */
11554 262682954 : if (DECL_DESTRUCTOR_P (current_function_decl))
11555 : {
11556 3304 : if (retval)
11557 3 : error_at (loc, "returning a value from a destructor");
11558 :
11559 3304 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11560 0 : retval = current_class_ptr;
11561 : else
11562 : return NULL_TREE;
11563 : }
11564 131338173 : else if (DECL_CONSTRUCTOR_P (current_function_decl))
11565 : {
11566 162480 : if (in_function_try_handler)
11567 : /* If a return statement appears in a handler of the
11568 : function-try-block of a constructor, the program is ill-formed. */
11569 0 : error ("cannot return from a handler of a function-try-block of a constructor");
11570 162480 : else if (retval)
11571 : /* You can't return a value from a constructor. */
11572 3 : error_at (loc, "returning a value from a constructor");
11573 :
11574 162480 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11575 0 : retval = current_class_ptr;
11576 : else
11577 : return NULL_TREE;
11578 : }
11579 :
11580 131175693 : const tree saved_retval = retval;
11581 :
11582 131175693 : if (processing_template_decl)
11583 : {
11584 85045826 : current_function_returns_value = 1;
11585 :
11586 85045826 : if (check_for_bare_parameter_packs (retval))
11587 14 : return error_mark_node;
11588 :
11589 : /* If one of the types might be void, we can't tell whether we're
11590 : returning a value. */
11591 85045812 : if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11592 35827543 : && !FNDECL_USED_AUTO (current_function_decl))
11593 49218278 : || (retval != NULL_TREE
11594 48034868 : && (TREE_TYPE (retval) == NULL_TREE
11595 33379213 : || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11596 57681182 : goto dependent;
11597 : }
11598 :
11599 73494497 : functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11600 :
11601 : /* Deduce auto return type from a return statement. */
11602 73494497 : if (FNDECL_USED_AUTO (current_function_decl))
11603 : {
11604 1128002 : tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11605 1128002 : tree auto_node;
11606 1128002 : tree type;
11607 :
11608 1128002 : if (!retval && !is_auto (pattern))
11609 : {
11610 : /* Give a helpful error message. */
11611 3 : auto_diagnostic_group d;
11612 3 : error ("return-statement with no value, in function returning %qT",
11613 : pattern);
11614 3 : inform (input_location, "only plain %<auto%> return type can be "
11615 : "deduced to %<void%>");
11616 3 : type = error_mark_node;
11617 3 : }
11618 1127999 : else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11619 : {
11620 6 : error ("returning initializer list");
11621 6 : type = error_mark_node;
11622 : }
11623 : else
11624 : {
11625 1127993 : if (!retval)
11626 2514 : retval = void_node;
11627 1127993 : auto_node = type_uses_auto (pattern);
11628 1127993 : type = do_auto_deduction (pattern, retval, auto_node,
11629 : tf_warning_or_error, adc_return_type);
11630 : }
11631 :
11632 1128002 : if (type == error_mark_node)
11633 : /* Leave it. */;
11634 1127760 : else if (functype == pattern)
11635 904634 : apply_deduced_return_type (current_function_decl, type);
11636 223126 : else if (!same_type_p (type, functype))
11637 : {
11638 24 : if (LAMBDA_FUNCTION_P (current_function_decl))
11639 6 : error_at (loc, "inconsistent types %qT and %qT deduced for "
11640 : "lambda return type", functype, type);
11641 : else
11642 12 : error_at (loc, "inconsistent deduction for auto return type: "
11643 : "%qT and then %qT", functype, type);
11644 : }
11645 : functype = type;
11646 : }
11647 :
11648 73494497 : result = DECL_RESULT (current_function_decl);
11649 73494497 : valtype = TREE_TYPE (result);
11650 73494497 : gcc_assert (valtype != NULL_TREE);
11651 73494497 : fn_returns_value_p = !VOID_TYPE_P (valtype);
11652 :
11653 : /* Check for a return statement with no return value in a function
11654 : that's supposed to return a value. */
11655 73494497 : if (!retval && fn_returns_value_p)
11656 : {
11657 33 : if (functype != error_mark_node)
11658 30 : permerror (input_location, "return-statement with no value, in "
11659 : "function returning %qT", valtype);
11660 : /* Remember that this function did return. */
11661 33 : current_function_returns_value = 1;
11662 : /* But suppress NRV .*/
11663 33 : current_function_return_value = error_mark_node;
11664 : /* And signal caller that TREE_NO_WARNING should be set on the
11665 : RETURN_EXPR to avoid control reaches end of non-void function
11666 : warnings in tree-cfg.cc. */
11667 33 : *no_warning = true;
11668 : }
11669 : /* Check for a return statement with a value in a function that
11670 : isn't supposed to return a value. */
11671 73494464 : else if (retval && !fn_returns_value_p)
11672 : {
11673 175167 : if (VOID_TYPE_P (TREE_TYPE (retval)))
11674 : /* You can return a `void' value from a function of `void'
11675 : type. In that case, we have to evaluate the expression for
11676 : its side-effects. */
11677 175128 : finish_expr_stmt (retval);
11678 39 : else if (retval != error_mark_node)
11679 36 : permerror (loc, "return-statement with a value, in function "
11680 : "returning %qT", valtype);
11681 175167 : current_function_returns_null = 1;
11682 :
11683 : /* There's really no value to return, after all. */
11684 175167 : return NULL_TREE;
11685 : }
11686 73319297 : else if (!retval)
11687 : /* Remember that this function can sometimes return without a
11688 : value. */
11689 1716030 : current_function_returns_null = 1;
11690 : else
11691 : /* Remember that this function did return a value. */
11692 71603267 : current_function_returns_value = 1;
11693 :
11694 : /* Check for erroneous operands -- but after giving ourselves a
11695 : chance to provide an error about returning a value from a void
11696 : function. */
11697 73319330 : if (error_operand_p (retval))
11698 : {
11699 1072 : current_function_return_value = error_mark_node;
11700 1072 : return error_mark_node;
11701 : }
11702 :
11703 : /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11704 146636516 : if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11705 32684 : && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11706 2601 : && ! flag_check_new
11707 73320850 : && retval && null_ptr_cst_p (retval))
11708 24 : warning (0, "%<operator new%> must not return NULL unless it is "
11709 : "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11710 :
11711 : /* Effective C++ rule 15. See also start_function. */
11712 73318258 : if (warn_ecpp
11713 42 : && DECL_NAME (current_function_decl) == assign_op_identifier
11714 73318288 : && !type_dependent_expression_p (retval))
11715 : {
11716 27 : bool warn = true;
11717 :
11718 : /* The function return type must be a reference to the current
11719 : class. */
11720 27 : if (TYPE_REF_P (valtype)
11721 45 : && same_type_ignoring_top_level_qualifiers_p
11722 18 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11723 : {
11724 : /* Returning '*this' is obviously OK. */
11725 18 : if (retval == current_class_ref)
11726 : warn = false;
11727 : /* If we are calling a function whose return type is the same of
11728 : the current class reference, it is ok. */
11729 6 : else if (INDIRECT_REF_P (retval)
11730 6 : && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11731 : warn = false;
11732 : }
11733 :
11734 : if (warn)
11735 9 : warning_at (loc, OPT_Weffc__,
11736 : "%<operator=%> should return a reference to %<*this%>");
11737 : }
11738 :
11739 73318258 : if (dependent_type_p (functype)
11740 73318258 : || type_dependent_expression_p (retval))
11741 : {
11742 74136618 : dependent:
11743 : /* We should not have changed the return value. */
11744 74136618 : gcc_assert (retval == saved_retval);
11745 : /* We don't know if this is an lvalue or rvalue use, but
11746 : either way we can mark it as read. */
11747 74136618 : mark_exp_read (retval);
11748 74136618 : return retval;
11749 : }
11750 :
11751 : /* The fabled Named Return Value optimization, as per [class.copy]/15:
11752 :
11753 : [...] For a function with a class return type, if the expression
11754 : in the return statement is the name of a local object, and the cv-
11755 : unqualified type of the local object is the same as the function
11756 : return type, an implementation is permitted to omit creating the tem-
11757 : porary object to hold the function return value [...]
11758 :
11759 : So, if this is a value-returning function that always returns the same
11760 : local variable, remember it.
11761 :
11762 : We choose the first suitable variable even if the function sometimes
11763 : returns something else, but only if the variable is out of scope at the
11764 : other return sites, or else we run the risk of clobbering the variable we
11765 : chose if the other returned expression uses the chosen variable somehow.
11766 :
11767 : We don't currently do this if the first return is a non-variable, as it
11768 : would be complicated to determine whether an NRV selected later was in
11769 : scope at the point of the earlier return. We also don't currently support
11770 : multiple variables with non-overlapping scopes (53637).
11771 :
11772 : See finish_function and finalize_nrv for the rest of this optimization. */
11773 56862822 : tree bare_retval = NULL_TREE;
11774 56862822 : if (retval)
11775 : {
11776 55146774 : retval = maybe_undo_parenthesized_ref (retval);
11777 55146774 : bare_retval = tree_strip_any_location_wrapper (retval);
11778 : }
11779 :
11780 56862822 : bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11781 56862822 : if (fn_returns_value_p && flag_elide_constructors
11782 55146751 : && current_function_return_value != bare_retval)
11783 : {
11784 55140782 : if (named_return_value_okay_p
11785 273472 : && current_function_return_value == NULL_TREE)
11786 232493 : current_function_return_value = bare_retval;
11787 54908289 : else if (current_function_return_value
11788 11083482 : && VAR_P (current_function_return_value)
11789 204 : && DECL_NAME (current_function_return_value)
11790 54908493 : && !decl_in_scope_p (current_function_return_value))
11791 : {
11792 : /* The earlier NRV is out of scope at this point, so it's safe to
11793 141 : leave it alone; the current return can't refer to it. */;
11794 141 : if (named_return_value_okay_p
11795 141 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11796 : {
11797 12 : warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11798 : bare_retval);
11799 12 : suppress_warning (current_function_decl, OPT_Wnrvo);
11800 : }
11801 : }
11802 : else
11803 : {
11804 54908148 : if ((named_return_value_okay_p
11805 54867183 : || (current_function_return_value
11806 11042376 : && current_function_return_value != error_mark_node))
11807 54908189 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11808 : {
11809 40929 : warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11810 : current_function_decl);
11811 40929 : suppress_warning (current_function_decl, OPT_Wnrvo);
11812 : }
11813 54908148 : current_function_return_value = error_mark_node;
11814 : }
11815 : }
11816 :
11817 : /* We don't need to do any conversions when there's nothing being
11818 : returned. */
11819 56862822 : if (!retval)
11820 : return NULL_TREE;
11821 :
11822 55146774 : if (!named_return_value_okay_p)
11823 54867333 : maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11824 :
11825 : /* Do any required conversions. */
11826 110293548 : if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11827 : /* No conversions are required. */
11828 : ;
11829 : else
11830 : {
11831 55146774 : int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11832 :
11833 : /* The functype's return type will have been set to void, if it
11834 : was an incomplete type. Just treat this as 'return;' */
11835 55146774 : if (VOID_TYPE_P (functype))
11836 6 : return error_mark_node;
11837 :
11838 : /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11839 : treated as an rvalue for the purposes of overload resolution to
11840 : favor move constructors over copy constructors.
11841 :
11842 : Note that these conditions are similar to, but not as strict as,
11843 : the conditions for the named return value optimization. */
11844 55146768 : bool converted = false;
11845 55146768 : tree moved;
11846 : /* Until C++23, this was only interesting for class type, but in C++23,
11847 : we should do the below when we're converting from/to a class/reference
11848 : (a non-scalar type). */
11849 55146768 : if ((cxx_dialect < cxx23
11850 7820077 : ? CLASS_TYPE_P (functype)
11851 9619668 : : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11852 65892165 : && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11853 : /* In C++20 and earlier we treat the return value as an rvalue
11854 : that can bind to lvalue refs. In C++23, such an expression is just
11855 : an xvalue (see reference_binding). */
11856 : retval = moved;
11857 :
11858 : /* The call in a (lambda) thunk needs no conversions. */
11859 55146768 : if (TREE_CODE (retval) == CALL_EXPR
11860 55146768 : && call_from_lambda_thunk_p (retval))
11861 : converted = true;
11862 :
11863 : /* Don't check copy-initialization for NRV in a coroutine ramp; we
11864 : implement this case as NRV, but it's specified as directly
11865 : initializing the return value from get_return_object(). */
11866 55146768 : if (DECL_RAMP_P (current_function_decl) && named_return_value_okay_p)
11867 : converted = true;
11868 :
11869 : /* First convert the value to the function's return type, then
11870 : to the type of return value's location to handle the
11871 : case that functype is smaller than the valtype. */
11872 55146506 : if (!converted)
11873 55090504 : retval = convert_for_initialization
11874 55090504 : (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11875 : tf_warning_or_error);
11876 55146768 : retval = convert (valtype, retval);
11877 :
11878 : /* If the conversion failed, treat this just like `return;'. */
11879 55146768 : if (retval == error_mark_node)
11880 : {
11881 : /* And suppress NRV. */
11882 213 : current_function_return_value = error_mark_node;
11883 213 : return retval;
11884 : }
11885 : /* We can't initialize a register from a AGGR_INIT_EXPR. */
11886 55146555 : else if (! cfun->returns_struct
11887 52974583 : && TREE_CODE (retval) == TARGET_EXPR
11888 61827678 : && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == AGGR_INIT_EXPR)
11889 1501316 : retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11890 1501316 : TARGET_EXPR_SLOT (retval));
11891 53645239 : else if (!processing_template_decl
11892 43929228 : && maybe_warn_about_returning_address_of_local (retval, loc)
11893 53645429 : && INDIRECT_TYPE_P (valtype))
11894 175 : *dangling = true;
11895 : }
11896 :
11897 55146555 : if (check_out_of_consteval_use (retval))
11898 : {
11899 12 : current_function_return_value = error_mark_node;
11900 12 : return error_mark_node;
11901 : }
11902 :
11903 : /* A naive attempt to reduce the number of -Wdangling-reference false
11904 : positives: if we know that this function can return a variable with
11905 : static storage duration rather than one of its parameters, suppress
11906 : the warning. */
11907 55146543 : if (warn_dangling_reference
11908 413420 : && TYPE_REF_P (functype)
11909 27554 : && bare_retval
11910 27554 : && VAR_P (bare_retval)
11911 50 : && TREE_STATIC (bare_retval))
11912 50 : suppress_warning (current_function_decl, OPT_Wdangling_reference);
11913 :
11914 55146543 : if (processing_template_decl)
11915 : return saved_retval;
11916 :
11917 : /* Actually copy the value returned into the appropriate location. */
11918 45430534 : if (retval && retval != result)
11919 45430534 : retval = cp_build_init_expr (result, retval);
11920 :
11921 45430534 : if (current_function_return_value == bare_retval)
11922 238456 : INIT_EXPR_NRV_P (retval) = true;
11923 :
11924 45430534 : if (tree set = maybe_set_retval_sentinel ())
11925 134889 : retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11926 :
11927 : return retval;
11928 : }
11929 :
11930 :
11931 : /* Returns nonzero if the pointer-type FROM can be converted to the
11932 : pointer-type TO via a qualification conversion. If CONSTP is -1,
11933 : then we return nonzero if the pointers are similar, and the
11934 : cv-qualification signature of FROM is a proper subset of that of TO.
11935 :
11936 : If CONSTP is positive, then all outer pointers have been
11937 : const-qualified. */
11938 :
11939 : static bool
11940 193195328 : comp_ptr_ttypes_real (tree to, tree from, int constp)
11941 : {
11942 193195328 : bool to_more_cv_qualified = false;
11943 193195328 : bool is_opaque_pointer = false;
11944 :
11945 666380 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11946 : {
11947 193861708 : if (TREE_CODE (to) != TREE_CODE (from))
11948 : return false;
11949 :
11950 129826296 : if (TREE_CODE (from) == OFFSET_TYPE
11951 129826296 : && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11952 : TYPE_OFFSET_BASETYPE (to)))
11953 : return false;
11954 :
11955 : /* Const and volatile mean something different for function and
11956 : array types, so the usual checks are not appropriate. We'll
11957 : check the array type elements in further iterations. */
11958 129826296 : if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11959 : {
11960 129569948 : if (!at_least_as_qualified_p (to, from))
11961 : return false;
11962 :
11963 115299208 : if (!at_least_as_qualified_p (from, to))
11964 : {
11965 78026131 : if (constp == 0)
11966 : return false;
11967 : to_more_cv_qualified = true;
11968 : }
11969 :
11970 115298943 : if (constp > 0)
11971 115294713 : constp &= TYPE_READONLY (to);
11972 : }
11973 :
11974 115555291 : if (VECTOR_TYPE_P (to))
11975 10121 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11976 :
11977 : /* P0388R4 allows a conversion from int[N] to int[] but not the
11978 : other way round. When both arrays have bounds but they do
11979 : not match, then no conversion is possible. */
11980 115555291 : if (TREE_CODE (to) == ARRAY_TYPE
11981 115555291 : && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11982 : return false;
11983 :
11984 115554453 : if (!TYPE_PTR_P (to)
11985 : && !TYPE_PTRDATAMEM_P (to)
11986 : /* CWG 330 says we need to look through arrays. */
11987 : && TREE_CODE (to) != ARRAY_TYPE)
11988 114888073 : return ((constp >= 0 || to_more_cv_qualified)
11989 114888073 : && (is_opaque_pointer
11990 114887535 : || same_type_ignoring_top_level_qualifiers_p (to, from)));
11991 : }
11992 : }
11993 :
11994 : /* When comparing, say, char ** to char const **, this function takes
11995 : the 'char *' and 'char const *'. Do not pass non-pointer/reference
11996 : types to this function. */
11997 :
11998 : int
11999 193194692 : comp_ptr_ttypes (tree to, tree from)
12000 : {
12001 193194692 : return comp_ptr_ttypes_real (to, from, 1);
12002 : }
12003 :
12004 : /* Returns true iff FNTYPE is a non-class type that involves
12005 : error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
12006 : if a parameter type is ill-formed. */
12007 :
12008 : bool
12009 725622 : error_type_p (const_tree type)
12010 : {
12011 766420 : tree t;
12012 :
12013 766420 : switch (TREE_CODE (type))
12014 : {
12015 : case ERROR_MARK:
12016 : return true;
12017 :
12018 40709 : case POINTER_TYPE:
12019 40709 : case REFERENCE_TYPE:
12020 40709 : case OFFSET_TYPE:
12021 40709 : return error_type_p (TREE_TYPE (type));
12022 :
12023 3252 : case FUNCTION_TYPE:
12024 3252 : case METHOD_TYPE:
12025 3252 : if (error_type_p (TREE_TYPE (type)))
12026 : return true;
12027 8794 : for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
12028 5548 : if (error_type_p (TREE_VALUE (t)))
12029 : return true;
12030 : return false;
12031 :
12032 191302 : case RECORD_TYPE:
12033 191302 : if (TYPE_PTRMEMFUNC_P (type))
12034 89 : return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
12035 : return false;
12036 :
12037 : default:
12038 : return false;
12039 : }
12040 : }
12041 :
12042 : /* Returns true if to and from are (possibly multi-level) pointers to the same
12043 : type or inheritance-related types, regardless of cv-quals. */
12044 :
12045 : bool
12046 138546944 : ptr_reasonably_similar (const_tree to, const_tree from)
12047 : {
12048 1443 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
12049 : {
12050 : /* Any target type is similar enough to void. */
12051 138548387 : if (VOID_TYPE_P (to))
12052 2047 : return !error_type_p (from);
12053 138546340 : if (VOID_TYPE_P (from))
12054 712493 : return !error_type_p (to);
12055 :
12056 137833847 : if (TREE_CODE (to) != TREE_CODE (from))
12057 : return false;
12058 :
12059 74370624 : if (TREE_CODE (from) == OFFSET_TYPE
12060 74370624 : && comptypes (TYPE_OFFSET_BASETYPE (to),
12061 3 : TYPE_OFFSET_BASETYPE (from),
12062 : COMPARE_BASE | COMPARE_DERIVED))
12063 3 : continue;
12064 :
12065 74370618 : if (VECTOR_TYPE_P (to)
12066 74370618 : && vector_types_convertible_p (to, from, false))
12067 : return true;
12068 :
12069 74370534 : if (TREE_CODE (to) == INTEGER_TYPE
12070 74370534 : && TYPE_PRECISION (to) == TYPE_PRECISION (from))
12071 : return true;
12072 :
12073 74176216 : if (TREE_CODE (to) == FUNCTION_TYPE)
12074 1141 : return !error_type_p (to) && !error_type_p (from);
12075 :
12076 74175075 : if (!TYPE_PTR_P (to))
12077 : {
12078 : /* When either type is incomplete avoid DERIVED_FROM_P,
12079 : which may call complete_type (c++/57942). */
12080 74173635 : bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
12081 : return comptypes
12082 74173635 : (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
12083 74173635 : b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
12084 : }
12085 1443 : }
12086 : }
12087 :
12088 : /* Return true if TO and FROM (both of which are POINTER_TYPEs or
12089 : pointer-to-member types) are the same, ignoring cv-qualification at
12090 : all levels. CB says how we should behave when comparing array bounds. */
12091 :
12092 : bool
12093 1095496 : comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
12094 : {
12095 1095496 : bool is_opaque_pointer = false;
12096 :
12097 1098669 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
12098 : {
12099 2194165 : if (TREE_CODE (to) != TREE_CODE (from))
12100 : return false;
12101 :
12102 1861014 : if (TREE_CODE (from) == OFFSET_TYPE
12103 1859044 : && same_type_p (TYPE_OFFSET_BASETYPE (from),
12104 : TYPE_OFFSET_BASETYPE (to)))
12105 1970 : continue;
12106 :
12107 1857074 : if (VECTOR_TYPE_P (to))
12108 8632 : is_opaque_pointer = vector_targets_convertible_p (to, from);
12109 :
12110 1857074 : if (TREE_CODE (to) == ARRAY_TYPE
12111 : /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
12112 : we must fail. */
12113 1857074 : && !comp_array_types (to, from, cb, /*strict=*/false))
12114 : return false;
12115 :
12116 : /* CWG 330 says we need to look through arrays. */
12117 1854918 : if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
12118 758219 : return (is_opaque_pointer
12119 758219 : || same_type_ignoring_top_level_qualifiers_p (to, from));
12120 : }
12121 : }
12122 :
12123 : /* Returns the type qualifiers for this type, including the qualifiers on the
12124 : elements for an array type. */
12125 :
12126 : int
12127 64710252666 : cp_type_quals (const_tree type)
12128 : {
12129 64710252666 : int quals;
12130 : /* This CONST_CAST is okay because strip_array_types returns its
12131 : argument unmodified and we assign it to a const_tree. */
12132 64710252666 : type = strip_array_types (const_cast<tree> (type));
12133 64710252666 : if (type == error_mark_node
12134 : /* Quals on a FUNCTION_TYPE are memfn quals. */
12135 64674180707 : || TREE_CODE (type) == FUNCTION_TYPE)
12136 : return TYPE_UNQUALIFIED;
12137 64603076681 : quals = TYPE_QUALS (type);
12138 : /* METHOD and REFERENCE_TYPEs should never have quals. */
12139 64603076681 : gcc_assert ((TREE_CODE (type) != METHOD_TYPE
12140 : && !TYPE_REF_P (type))
12141 : || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
12142 : == TYPE_UNQUALIFIED));
12143 : return quals;
12144 : }
12145 :
12146 : /* Returns the function-ref-qualifier for TYPE */
12147 :
12148 : cp_ref_qualifier
12149 3485343737 : type_memfn_rqual (const_tree type)
12150 : {
12151 3485343737 : gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
12152 :
12153 3485343737 : if (!FUNCTION_REF_QUALIFIED (type))
12154 : return REF_QUAL_NONE;
12155 32336082 : else if (FUNCTION_RVALUE_QUALIFIED (type))
12156 : return REF_QUAL_RVALUE;
12157 : else
12158 16064366 : return REF_QUAL_LVALUE;
12159 : }
12160 :
12161 : /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
12162 : METHOD_TYPE. */
12163 :
12164 : int
12165 1146303893 : type_memfn_quals (const_tree type)
12166 : {
12167 1146303893 : if (TREE_CODE (type) == FUNCTION_TYPE)
12168 113712145 : return TYPE_QUALS (type);
12169 1032591748 : else if (TREE_CODE (type) == METHOD_TYPE)
12170 1032591748 : return cp_type_quals (class_of_this_parm (type));
12171 : else
12172 0 : gcc_unreachable ();
12173 : }
12174 :
12175 : /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
12176 : MEMFN_QUALS and its ref-qualifier to RQUAL. */
12177 :
12178 : tree
12179 75797704 : apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
12180 : {
12181 : /* Could handle METHOD_TYPE here if necessary. */
12182 75797704 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
12183 75797704 : if (TYPE_QUALS (type) == memfn_quals
12184 75797704 : && type_memfn_rqual (type) == rqual)
12185 : return type;
12186 :
12187 : /* This should really have a different TYPE_MAIN_VARIANT, but that gets
12188 : complex. */
12189 682589 : tree result = build_qualified_type (type, memfn_quals);
12190 682589 : return build_ref_qualified_type (result, rqual);
12191 : }
12192 :
12193 : /* Returns nonzero if TYPE is const or volatile. */
12194 :
12195 : bool
12196 1254772007 : cv_qualified_p (const_tree type)
12197 : {
12198 1254772007 : int quals = cp_type_quals (type);
12199 1254772007 : return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
12200 : }
12201 :
12202 : /* Returns nonzero if the TYPE contains a mutable member. */
12203 :
12204 : bool
12205 970961601 : cp_has_mutable_p (const_tree type)
12206 : {
12207 : /* This CONST_CAST is okay because strip_array_types returns its
12208 : argument unmodified and we assign it to a const_tree. */
12209 970961601 : type = strip_array_types (const_cast<tree> (type));
12210 :
12211 970961601 : return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
12212 : }
12213 :
12214 : /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
12215 : TYPE_QUALS. For a VAR_DECL, this may be an optimistic
12216 : approximation. In particular, consider:
12217 :
12218 : int f();
12219 : struct S { int i; };
12220 : const S s = { f(); }
12221 :
12222 : Here, we will make "s" as TREE_READONLY (because it is declared
12223 : "const") -- only to reverse ourselves upon seeing that the
12224 : initializer is non-constant. */
12225 :
12226 : void
12227 1047408841 : cp_apply_type_quals_to_decl (int type_quals, tree decl)
12228 : {
12229 1047408841 : tree type = TREE_TYPE (decl);
12230 :
12231 1047408841 : if (type == error_mark_node)
12232 : return;
12233 :
12234 1047408069 : if (TREE_CODE (decl) == TYPE_DECL)
12235 : return;
12236 :
12237 923663754 : gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
12238 : && type_quals != TYPE_UNQUALIFIED));
12239 :
12240 : /* Avoid setting TREE_READONLY incorrectly. */
12241 : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
12242 : constructor can produce constant init, so rely on cp_finish_decl to
12243 : clear TREE_READONLY if the variable has non-constant init. */
12244 :
12245 : /* If the type has (or might have) a mutable component, that component
12246 : might be modified. */
12247 923663754 : if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
12248 85219164 : type_quals &= ~TYPE_QUAL_CONST;
12249 :
12250 923663754 : c_apply_type_quals_to_decl (type_quals, decl);
12251 : }
12252 :
12253 : /* Subroutine of casts_away_constness. Make T1 and T2 point at
12254 : exemplar types such that casting T1 to T2 is casting away constness
12255 : if and only if there is no implicit conversion from T1 to T2. */
12256 :
12257 : static void
12258 1328469 : casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
12259 : {
12260 1328469 : int quals1;
12261 1328469 : int quals2;
12262 :
12263 : /* [expr.const.cast]
12264 :
12265 : For multi-level pointer to members and multi-level mixed pointers
12266 : and pointers to members (conv.qual), the "member" aspect of a
12267 : pointer to member level is ignored when determining if a const
12268 : cv-qualifier has been cast away. */
12269 : /* [expr.const.cast]
12270 :
12271 : For two pointer types:
12272 :
12273 : X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
12274 : X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
12275 : K is min(N,M)
12276 :
12277 : casting from X1 to X2 casts away constness if, for a non-pointer
12278 : type T there does not exist an implicit conversion (clause
12279 : _conv_) from:
12280 :
12281 : Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
12282 :
12283 : to
12284 :
12285 : Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
12286 1328469 : if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
12287 664585 : || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
12288 : {
12289 664062 : *t1 = cp_build_qualified_type (void_type_node,
12290 : cp_type_quals (*t1));
12291 664062 : *t2 = cp_build_qualified_type (void_type_node,
12292 : cp_type_quals (*t2));
12293 664062 : return;
12294 : }
12295 :
12296 664407 : quals1 = cp_type_quals (*t1);
12297 664407 : quals2 = cp_type_quals (*t2);
12298 :
12299 664407 : if (TYPE_PTRDATAMEM_P (*t1))
12300 0 : *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
12301 : else
12302 664407 : *t1 = TREE_TYPE (*t1);
12303 664407 : if (TYPE_PTRDATAMEM_P (*t2))
12304 0 : *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
12305 : else
12306 664407 : *t2 = TREE_TYPE (*t2);
12307 :
12308 664407 : casts_away_constness_r (t1, t2, complain);
12309 664407 : *t1 = build_pointer_type (*t1);
12310 664407 : *t2 = build_pointer_type (*t2);
12311 664407 : *t1 = cp_build_qualified_type (*t1, quals1);
12312 664407 : *t2 = cp_build_qualified_type (*t2, quals2);
12313 : }
12314 :
12315 : /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
12316 : constness.
12317 :
12318 : ??? This function returns non-zero if casting away qualifiers not
12319 : just const. We would like to return to the caller exactly which
12320 : qualifiers are casted away to give more accurate diagnostics.
12321 : */
12322 :
12323 : static bool
12324 664134 : casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
12325 : {
12326 664134 : if (TYPE_REF_P (t2))
12327 : {
12328 : /* [expr.const.cast]
12329 :
12330 : Casting from an lvalue of type T1 to an lvalue of type T2
12331 : using a reference cast casts away constness if a cast from an
12332 : rvalue of type "pointer to T1" to the type "pointer to T2"
12333 : casts away constness. */
12334 0 : t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
12335 0 : return casts_away_constness (build_pointer_type (t1),
12336 0 : build_pointer_type (TREE_TYPE (t2)),
12337 0 : complain);
12338 : }
12339 :
12340 664134 : if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
12341 : /* [expr.const.cast]
12342 :
12343 : Casting from an rvalue of type "pointer to data member of X
12344 : of type T1" to the type "pointer to data member of Y of type
12345 : T2" casts away constness if a cast from an rvalue of type
12346 : "pointer to T1" to the type "pointer to T2" casts away
12347 : constness. */
12348 48 : return casts_away_constness
12349 48 : (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
12350 48 : build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
12351 48 : complain);
12352 :
12353 : /* Casting away constness is only something that makes sense for
12354 : pointer or reference types. */
12355 664086 : if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
12356 : return false;
12357 :
12358 : /* Top-level qualifiers don't matter. */
12359 664062 : t1 = TYPE_MAIN_VARIANT (t1);
12360 664062 : t2 = TYPE_MAIN_VARIANT (t2);
12361 664062 : casts_away_constness_r (&t1, &t2, complain);
12362 664062 : if (!can_convert (t2, t1, complain))
12363 : return true;
12364 :
12365 : return false;
12366 : }
12367 :
12368 : /* If T is a REFERENCE_TYPE return the type to which T refers.
12369 : Otherwise, return T itself. */
12370 :
12371 : tree
12372 4478714366 : non_reference (tree t)
12373 : {
12374 4478714366 : if (t && TYPE_REF_P (t))
12375 403642036 : t = TREE_TYPE (t);
12376 4478714366 : return t;
12377 : }
12378 :
12379 :
12380 : /* Return nonzero if REF is an lvalue valid for this language;
12381 : otherwise, print an error message and return zero. USE says
12382 : how the lvalue is being used and so selects the error message. */
12383 :
12384 : int
12385 43384123 : lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
12386 : {
12387 43384123 : cp_lvalue_kind kind = lvalue_kind (ref);
12388 :
12389 43384123 : if (kind == clk_none)
12390 : {
12391 690 : if (complain & tf_error)
12392 126 : lvalue_error (cp_expr_loc_or_input_loc (ref), use);
12393 690 : return 0;
12394 : }
12395 43383433 : else if (kind & (clk_rvalueref|clk_class))
12396 : {
12397 220 : if (!(complain & tf_error))
12398 : return 0;
12399 : /* Make this a permerror because we used to accept it. */
12400 18 : permerror (cp_expr_loc_or_input_loc (ref),
12401 : "using rvalue as lvalue");
12402 : }
12403 : return 1;
12404 : }
12405 :
12406 : /* Return true if a user-defined literal operator is a raw operator. */
12407 :
12408 : bool
12409 116074 : check_raw_literal_operator (const_tree decl)
12410 : {
12411 116074 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12412 116074 : tree argtype;
12413 116074 : int arity;
12414 116074 : bool maybe_raw_p = false;
12415 :
12416 : /* Count the number and type of arguments and check for ellipsis. */
12417 116074 : for (argtype = argtypes, arity = 0;
12418 174128 : argtype && argtype != void_list_node;
12419 58054 : ++arity, argtype = TREE_CHAIN (argtype))
12420 : {
12421 58054 : tree t = TREE_VALUE (argtype);
12422 :
12423 58054 : if (same_type_p (t, const_string_type_node))
12424 9 : maybe_raw_p = true;
12425 : }
12426 116074 : if (!argtype)
12427 : return false; /* Found ellipsis. */
12428 :
12429 116074 : if (!maybe_raw_p || arity != 1)
12430 116068 : return false;
12431 :
12432 : return true;
12433 : }
12434 :
12435 :
12436 : /* Return true if a user-defined literal operator has one of the allowed
12437 : argument types. */
12438 :
12439 : bool
12440 317802 : check_literal_operator_args (const_tree decl,
12441 : bool *long_long_unsigned_p, bool *long_double_p)
12442 : {
12443 317802 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12444 :
12445 317802 : *long_long_unsigned_p = false;
12446 317802 : *long_double_p = false;
12447 317802 : if (processing_template_decl || processing_specialization)
12448 58179 : return argtypes == void_list_node;
12449 : else
12450 : {
12451 : tree argtype;
12452 : int arity;
12453 : int max_arity = 2;
12454 :
12455 : /* Count the number and type of arguments and check for ellipsis. */
12456 259495 : for (argtype = argtypes, arity = 0;
12457 519118 : argtype && argtype != void_list_node;
12458 259495 : argtype = TREE_CHAIN (argtype))
12459 : {
12460 259629 : tree t = TREE_VALUE (argtype);
12461 259629 : ++arity;
12462 :
12463 259629 : if (TYPE_PTR_P (t))
12464 : {
12465 126111 : bool maybe_raw_p = false;
12466 126111 : t = TREE_TYPE (t);
12467 126111 : if (cp_type_quals (t) != TYPE_QUAL_CONST)
12468 : return false;
12469 126108 : t = TYPE_MAIN_VARIANT (t);
12470 126108 : if ((maybe_raw_p = same_type_p (t, char_type_node))
12471 100696 : || same_type_p (t, wchar_type_node)
12472 75470 : || same_type_p (t, char8_type_node)
12473 50464 : || same_type_p (t, char16_type_node)
12474 151340 : || same_type_p (t, char32_type_node))
12475 : {
12476 126105 : argtype = TREE_CHAIN (argtype);
12477 126105 : if (!argtype)
12478 : return false;
12479 126105 : t = TREE_VALUE (argtype);
12480 126105 : if (maybe_raw_p && argtype == void_list_node)
12481 : return true;
12482 125998 : else if (same_type_p (t, size_type_node))
12483 : {
12484 125992 : ++arity;
12485 125992 : continue;
12486 : }
12487 : else
12488 : return false;
12489 : }
12490 : }
12491 133518 : else if (same_type_p (t, long_long_unsigned_type_node))
12492 : {
12493 47022 : max_arity = 1;
12494 47022 : *long_long_unsigned_p = true;
12495 : }
12496 86496 : else if (same_type_p (t, long_double_type_node))
12497 : {
12498 86396 : max_arity = 1;
12499 86396 : *long_double_p = true;
12500 : }
12501 100 : else if (same_type_p (t, char_type_node))
12502 : max_arity = 1;
12503 56 : else if (same_type_p (t, wchar_type_node))
12504 : max_arity = 1;
12505 45 : else if (same_type_p (t, char8_type_node))
12506 : max_arity = 1;
12507 40 : else if (same_type_p (t, char16_type_node))
12508 : max_arity = 1;
12509 29 : else if (same_type_p (t, char32_type_node))
12510 : max_arity = 1;
12511 : else
12512 : return false;
12513 : }
12514 259489 : if (!argtype)
12515 : return false; /* Found ellipsis. */
12516 :
12517 259486 : if (arity != max_arity)
12518 : return false;
12519 :
12520 : return true;
12521 : }
12522 : }
12523 :
12524 : /* Always returns false since unlike C90, C++ has no concept of implicit
12525 : function declarations. */
12526 :
12527 : bool
12528 395 : c_decl_implicit (const_tree)
12529 : {
12530 395 : return false;
12531 : }
|