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 150939086 : require_complete_type (tree value,
75 : tsubst_flags_t complain /* = tf_warning_or_error */)
76 : {
77 150939086 : tree type;
78 :
79 150939086 : if (processing_template_decl || value == error_mark_node)
80 : return value;
81 :
82 140279286 : if (TREE_CODE (value) == OVERLOAD)
83 0 : type = unknown_type_node;
84 : else
85 140279286 : type = TREE_TYPE (value);
86 :
87 140279286 : if (type == error_mark_node)
88 : return error_mark_node;
89 :
90 : /* First, detect a valid value with a complete type. */
91 140279274 : if (COMPLETE_TYPE_P (type))
92 : return value;
93 :
94 135312 : 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 15884125112 : complete_type (tree type)
107 : {
108 15884125112 : 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 15884125112 : if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 : ;
115 5172308398 : else if (TREE_CODE (type) == ARRAY_TYPE)
116 : {
117 799989 : tree t = complete_type (TREE_TYPE (type));
118 799989 : unsigned int needs_constructing, has_nontrivial_dtor;
119 799989 : if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 781079 : layout_type (type);
121 799989 : needs_constructing
122 799989 : = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 799989 : has_nontrivial_dtor
124 799989 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 2659530 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 : {
127 1859541 : TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 1859541 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 : }
130 : }
131 5171508409 : else if (CLASS_TYPE_P (type))
132 : {
133 5075833405 : if (modules_p ())
134 : /* TYPE could be a class member we've not loaded the definition of. */
135 20643741 : lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
136 :
137 5075833405 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 967037937 : 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 1715316866 : complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 : {
151 1715316866 : type = complete_type (type);
152 1715316863 : if (type == error_mark_node)
153 : /* We already issued an error. */
154 : return NULL_TREE;
155 1715316705 : else if (!COMPLETE_TYPE_P (type))
156 : {
157 3968 : if (complain & tf_error)
158 478 : cxx_incomplete_type_diagnostic (value, type, diagnostics::kind::error);
159 3968 : note_failed_type_completion (type, complain);
160 3968 : return NULL_TREE;
161 : }
162 : else
163 : return type;
164 : }
165 :
166 : tree
167 171233094 : complete_type_or_else (tree type, tree value)
168 : {
169 171233094 : 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 4301143 : commonparms (tree p1, tree p2)
182 : {
183 4301143 : tree oldargs = p1, newargs, n;
184 4301143 : int i, len;
185 4301143 : int any_change = 0;
186 :
187 4301143 : len = list_length (p1);
188 4301143 : newargs = tree_last (p1);
189 :
190 4301143 : if (newargs == void_list_node)
191 4301143 : i = 1;
192 : else
193 : {
194 40770 : i = 0;
195 40770 : newargs = 0;
196 : }
197 :
198 14207454 : for (; i < len; i++)
199 9906311 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200 :
201 : n = newargs;
202 :
203 18467827 : for (i = 0; p1;
204 14166684 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 : {
206 14166893 : if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 : {
208 174 : TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 174 : any_change = 1;
210 : }
211 14166510 : else if (! TREE_PURPOSE (p1))
212 : {
213 14166475 : if (TREE_PURPOSE (p2))
214 : {
215 428912 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 428912 : any_change = 1;
217 : }
218 : }
219 : else
220 : {
221 35 : if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
222 35 : any_change = 1;
223 35 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 : }
225 14166684 : if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 : {
227 3965386 : any_change = 1;
228 3965386 : TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 : }
230 : else
231 10201298 : TREE_VALUE (n) = TREE_VALUE (p1);
232 : }
233 4301143 : if (! any_change)
234 1430948 : 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 33715038 : original_type (tree t)
243 : {
244 33715038 : auto quals = cp_type_quals (t);
245 33715038 : while (t != error_mark_node
246 34887743 : && TYPE_NAME (t) != NULL_TREE)
247 : {
248 11755879 : tree x = TYPE_NAME (t);
249 11755879 : if (TREE_CODE (x) != TYPE_DECL)
250 : break;
251 11755879 : x = DECL_ORIGINAL_TYPE (x);
252 11755879 : if (x == NULL_TREE)
253 : break;
254 : t = x;
255 : }
256 33715038 : 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 7493313 : cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
280 : {
281 7493313 : tree mv1 = TYPE_MAIN_VARIANT (t1);
282 7493313 : tree mv2 = TYPE_MAIN_VARIANT (t2);
283 7493313 : int extended1 = 0;
284 7493313 : int extended2 = 0;
285 :
286 7493313 : if (mv1 == mv2)
287 : return 0;
288 :
289 59927176 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
290 : {
291 52436279 : if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 3369203 : extended1 = i + 1;
293 52436279 : if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 2848265 : extended2 = i + 1;
295 : }
296 7490897 : if (mv1 == bfloat16_type_node)
297 818472 : extended1 = true;
298 7490897 : if (mv2 == bfloat16_type_node)
299 626222 : extended2 = true;
300 7490897 : if (extended2 && !extended1)
301 : {
302 3261483 : int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 3261483 : return ret == 3 ? 3 : -ret;
304 : }
305 :
306 4229414 : const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 4229414 : 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 4229414 : gcc_assert (fmt1->b == 2);
313 4229414 : 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 18 : return 3;
330 : }
331 4229381 : 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 33835048 : int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
338 8458762 : ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
339 33835048 : int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
340 8458762 : ? 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 4229381 : if ((p1 > p2 && fmt1->emax >= fmt2->emax)
345 3315551 : || (p1 == p2 && fmt1->emax > fmt2->emax))
346 : return 2;
347 3315551 : if ((p1 < p2 && fmt1->emax <= fmt2->emax)
348 1088547 : || (p1 == p2 && fmt1->emax < fmt2->emax))
349 : return -2;
350 1088547 : if ((p1 > p2 && fmt1->emax < fmt2->emax)
351 1075194 : || (p1 < p2 && fmt1->emax > fmt2->emax))
352 : return 3;
353 1061943 : 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 0 : return 0;
370 : }
371 : /* Two extended floating-point types with the same set of values have equal
372 : ranks. */
373 1061943 : 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 0 : 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 0 : return 0;
392 : }
393 :
394 : /* gcc_assert (extended1 && !extended2); */
395 : tree *p;
396 : int cnt = 0;
397 4247748 : for (p = &float_type_node; p <= &long_double_type_node; ++p)
398 : {
399 3185811 : const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
400 3185811 : gcc_assert (fmt3->b == 2);
401 25486488 : int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
402 6371622 : ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
403 3185811 : if (p1 == p3 && fmt1->emax == fmt3->emax)
404 944711 : ++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 1061937 : 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 0 : 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 115942939 : cp_common_type (tree t1, tree t2)
436 : {
437 115942939 : enum tree_code code1 = TREE_CODE (t1);
438 115942939 : enum tree_code code2 = TREE_CODE (t2);
439 115942939 : tree attributes;
440 115942939 : 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 115942939 : attributes = (*targetm.merge_type_attributes) (t1, t2);
447 :
448 115942939 : if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
449 : {
450 2308323 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
451 2308323 : return build_type_attribute_variant (t1, attributes);
452 : else
453 : return NULL_TREE;
454 : }
455 :
456 : /* FIXME: Attributes. */
457 113634616 : gcc_assert (ARITHMETIC_TYPE_P (t1)
458 : || VECTOR_TYPE_P (t1)
459 : || UNSCOPED_ENUM_P (t1));
460 113634616 : 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 113634616 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
468 : {
469 222242 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
470 222242 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
471 222242 : tree subtype
472 222242 : = type_after_usual_arithmetic_conversions (subtype1, subtype2);
473 :
474 222242 : if (subtype == error_mark_node)
475 : return subtype;
476 222242 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
477 188834 : 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 113412374 : 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 113352513 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
497 1161338 : return build_type_attribute_variant (t1, attributes);
498 112191175 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
499 1039310 : return build_type_attribute_variant (t2, attributes);
500 :
501 111151865 : if (code1 == REAL_TYPE
502 111151865 : && (extended_float_type_p (t1) || extended_float_type_p (t2)))
503 : {
504 140275 : tree mv1 = TYPE_MAIN_VARIANT (t1);
505 140275 : tree mv2 = TYPE_MAIN_VARIANT (t2);
506 140275 : if (mv1 == mv2)
507 137246 : return build_type_attribute_variant (t1, attributes);
508 :
509 3029 : int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
510 3029 : if (cmpret == 3)
511 6 : return error_mark_node;
512 3023 : else if (cmpret >= 0)
513 2648 : 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 111011590 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
520 17603574 : return build_type_attribute_variant (t1, attributes);
521 93408016 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
522 2493949 : return build_type_attribute_variant (t2, attributes);
523 :
524 : /* The types are the same; no need to do anything fancy. */
525 90914067 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
526 83429228 : return build_type_attribute_variant (t1, attributes);
527 :
528 7484839 : if (code1 != REAL_TYPE)
529 : {
530 : /* If one is unsigned long long, then convert the other to unsigned
531 : long long. */
532 7484836 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
533 7484836 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
534 156987 : return build_type_attribute_variant (long_long_unsigned_type_node,
535 156987 : 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 7327849 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
547 7327849 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
548 : {
549 1360 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
550 686 : ? long_long_unsigned_type_node
551 686 : : long_long_integer_type_node);
552 686 : return build_type_attribute_variant (t, attributes);
553 : }
554 :
555 : /* Go through the same procedure, but for longs. */
556 7327163 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
557 7327163 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
558 924834 : return build_type_attribute_variant (long_unsigned_type_node,
559 924834 : attributes);
560 6402329 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
561 6402329 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
562 : {
563 75943 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
564 75943 : ? long_unsigned_type_node : long_integer_type_node);
565 38446 : 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 12727734 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
573 : {
574 6363883 : if (int_n_enabled_p [i]
575 6363883 : && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
576 6273935 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
577 6273931 : || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
578 6273931 : || 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 6363851 : if (TYPE_UNSIGNED (t1))
589 5137312 : return build_type_attribute_variant (t1, attributes);
590 : else
591 1226539 : 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 1244412 : type_after_usual_arithmetic_conversions (tree t1, tree t2)
623 : {
624 1244412 : gcc_assert (ARITHMETIC_TYPE_P (t1)
625 : || VECTOR_TYPE_P (t1)
626 : || UNSCOPED_ENUM_P (t1));
627 1244412 : 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 1244412 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
633 1021634 : && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
634 : {
635 1021315 : t1 = type_promotes_to (t1);
636 1021315 : t2 = type_promotes_to (t2);
637 : }
638 :
639 1244412 : 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 6269184 : 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 6269184 : tree pointee1;
683 6269184 : tree pointee2;
684 6269184 : tree result_type;
685 6269184 : tree attributes;
686 :
687 : /* Determine the types pointed to by T1 and T2. */
688 6269184 : if (TYPE_PTR_P (t1))
689 : {
690 6268593 : pointee1 = TREE_TYPE (t1);
691 6268593 : pointee2 = TREE_TYPE (t2);
692 : }
693 : else
694 : {
695 591 : pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
696 591 : 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 6269184 : 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 6269107 : const auto q1 = cp_type_quals (pointee1);
723 6269107 : const auto q2 = cp_type_quals (pointee2);
724 6269107 : const auto quals = q1 | q2;
725 6269107 : result_type = cp_build_qualified_type (result_type,
726 6269107 : (quals | (*add_const
727 : ? 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 6269107 : if (quals != q1 || quals != q2)
733 745297 : *add_const = true;
734 : /* If the original types were pointers to members, so is the
735 : result. */
736 6269107 : if (TYPE_PTRMEM_P (t1))
737 : {
738 577 : 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 577 : result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
748 : result_type);
749 : }
750 : else
751 6268530 : result_type = build_pointer_type (result_type);
752 :
753 : /* Merge the attributes. */
754 6269107 : attributes = (*targetm.merge_type_attributes) (t1, t2);
755 6269107 : 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 6739510 : 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 6739510 : tree class1;
773 6739510 : 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 6739510 : if (null_ptr_cst_p (arg1))
780 : return t2;
781 6739286 : 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 6717824 : 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 6717824 : if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
799 : {
800 448568 : tree attributes;
801 448568 : tree result_type;
802 :
803 448568 : 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 448566 : result_type
833 448566 : = cp_build_qualified_type (void_type_node,
834 448566 : (cp_type_quals (TREE_TYPE (t1))
835 448566 : | cp_type_quals (TREE_TYPE (t2))));
836 448566 : result_type = build_pointer_type (result_type);
837 : /* Merge the attributes. */
838 448566 : attributes = (*targetm.merge_type_attributes) (t1, t2);
839 448566 : return build_type_attribute_variant (result_type, attributes);
840 : }
841 :
842 6269256 : 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 6269256 : if (fnptr_conv_p (t1, t2))
853 : return t1;
854 6269221 : 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 6268578 : if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
860 6268578 : && CLASS_TYPE_P (TREE_TYPE (t1))
861 957736 : && CLASS_TYPE_P (TREE_TYPE (t2))
862 7226923 : && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
863 957733 : TREE_TYPE (t2)))
864 : {
865 30192 : class1 = TREE_TYPE (t1);
866 30192 : class2 = TREE_TYPE (t2);
867 :
868 30192 : if (DERIVED_FROM_P (class1, class2))
869 3256 : t2 = (build_pointer_type
870 1628 : (cp_build_qualified_type (class1, cp_type_quals (class2))));
871 28564 : else if (DERIVED_FROM_P (class2, class1))
872 57092 : t1 = (build_pointer_type
873 28546 : (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 6238549 : else if (TYPE_PTRMEM_P (t1)
885 6239161 : && !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 6269151 : bool add_const = false;
923 6269151 : return composite_pointer_type_r (location, t1, t2, &add_const, operation,
924 6269151 : 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 30904139 : merge_types (tree t1, tree t2)
936 : {
937 30904139 : enum tree_code code1;
938 30904139 : enum tree_code code2;
939 30904139 : tree attributes;
940 :
941 : /* Save time if the two types are the same. */
942 30904139 : if (t1 == t2)
943 : return t1;
944 16857519 : if (original_type (t1) == original_type (t2))
945 : return t1;
946 :
947 : /* If one type is nonsense, use the other. */
948 16720536 : if (t1 == error_mark_node)
949 : return t2;
950 16720536 : if (t2 == error_mark_node)
951 : return t1;
952 :
953 : /* Handle merging an auto redeclaration with a previous deduced
954 : return type. */
955 16720536 : if (is_auto (t1))
956 : return t2;
957 :
958 : /* Merge the attributes. */
959 16705675 : attributes = (*targetm.merge_type_attributes) (t1, t2);
960 :
961 16705675 : if (TYPE_PTRMEMFUNC_P (t1))
962 15 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
963 16705675 : if (TYPE_PTRMEMFUNC_P (t2))
964 15 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
965 :
966 16705675 : code1 = TREE_CODE (t1);
967 16705675 : code2 = TREE_CODE (t2);
968 16705675 : 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 16705675 : switch (code1)
984 : {
985 2843788 : case POINTER_TYPE:
986 2843788 : case REFERENCE_TYPE:
987 : /* For two pointers, do this recursively on the target type. */
988 2843788 : {
989 2843788 : tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
990 2843788 : auto quals = cp_type_quals (t1);
991 :
992 2843788 : if (code1 == POINTER_TYPE)
993 : {
994 757921 : t1 = build_pointer_type (target);
995 757921 : if (TREE_CODE (target) == METHOD_TYPE)
996 15 : t1 = build_ptrmemfunc_type (t1);
997 : }
998 : else
999 2085867 : t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
1000 2843788 : t1 = build_type_attribute_variant (t1, attributes);
1001 2843788 : t1 = cp_build_qualified_type (t1, quals);
1002 :
1003 2843788 : return t1;
1004 : }
1005 :
1006 12 : case OFFSET_TYPE:
1007 12 : {
1008 12 : tree pointee;
1009 12 : auto quals = cp_type_quals (t1);
1010 12 : pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
1011 12 : TYPE_PTRMEM_POINTED_TO_TYPE (t2));
1012 12 : t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
1013 : pointee);
1014 12 : t1 = cp_build_qualified_type (t1, quals);
1015 12 : break;
1016 : }
1017 :
1018 10492 : case ARRAY_TYPE:
1019 10492 : {
1020 10492 : tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1021 : /* Save space: see if the result is identical to one of the args. */
1022 20984 : if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
1023 10461 : return build_type_attribute_variant (t1, attributes);
1024 53 : if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
1025 22 : return build_type_attribute_variant (t2, attributes);
1026 : /* Merge the element types, and have a size if either arg has one. */
1027 9 : t1 = build_cplus_array_type
1028 9 : (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
1029 9 : break;
1030 : }
1031 :
1032 4468124 : case FUNCTION_TYPE:
1033 : /* Function types: prefer the one that specified arg types.
1034 : If both do, merge the arg types. Also merge the return types. */
1035 4468124 : {
1036 4468124 : tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1037 4468124 : tree p1 = TYPE_ARG_TYPES (t1);
1038 4468124 : tree p2 = TYPE_ARG_TYPES (t2);
1039 4468124 : tree parms;
1040 :
1041 : /* Save space: see if the result is identical to one of the args. */
1042 4468124 : if (valtype == TREE_TYPE (t1) && ! p2)
1043 0 : return cp_build_type_attribute_variant (t1, attributes);
1044 4468124 : if (valtype == TREE_TYPE (t2) && ! p1)
1045 0 : return cp_build_type_attribute_variant (t2, attributes);
1046 :
1047 : /* Simple way if one arg fails to specify argument types. */
1048 8936248 : if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1049 : parms = p2;
1050 8602286 : else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1051 : parms = p1;
1052 : else
1053 4301143 : parms = commonparms (p1, p2);
1054 :
1055 4468124 : cp_cv_quals quals = type_memfn_quals (t1);
1056 4468124 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1057 4468124 : gcc_assert (quals == type_memfn_quals (t2));
1058 4468124 : gcc_assert (rqual == type_memfn_rqual (t2));
1059 :
1060 4468124 : tree rval = cp_build_function_type (valtype, parms);
1061 4468124 : rval = apply_memfn_quals (rval, quals);
1062 4468124 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1063 4468124 : TYPE_RAISES_EXCEPTIONS (t2));
1064 4468124 : bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1065 4468124 : t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1066 4468124 : break;
1067 : }
1068 :
1069 4056692 : case METHOD_TYPE:
1070 4056692 : {
1071 : /* Get this value the long way, since TYPE_METHOD_BASETYPE
1072 : is just the main variant of this. */
1073 4056692 : tree basetype = class_of_this_parm (t2);
1074 4056692 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1075 4056692 : TYPE_RAISES_EXCEPTIONS (t2));
1076 4056692 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1077 4056692 : tree t3;
1078 4056692 : bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1079 :
1080 : /* If this was a member function type, get back to the
1081 : original type of type member function (i.e., without
1082 : the class instance variable up front. */
1083 4056692 : t1 = cp_build_function_type (TREE_TYPE (t1),
1084 4056692 : TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1085 4056692 : t2 = cp_build_function_type (TREE_TYPE (t2),
1086 4056692 : TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1087 4056692 : t3 = merge_types (t1, t2);
1088 4056692 : t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1089 4056692 : TYPE_ARG_TYPES (t3));
1090 4056692 : t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1091 4056692 : break;
1092 : }
1093 :
1094 : case TYPENAME_TYPE:
1095 : /* There is no need to merge attributes into a TYPENAME_TYPE.
1096 : When the type is instantiated it will have whatever
1097 : attributes result from the instantiation. */
1098 : return t1;
1099 :
1100 5316295 : default:;
1101 5316295 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1102 : return t1;
1103 12 : else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1104 : return t2;
1105 : break;
1106 : }
1107 :
1108 8524837 : return cp_build_type_attribute_variant (t1, attributes);
1109 : }
1110 :
1111 : /* Return the ARRAY_TYPE type without its domain. */
1112 :
1113 : tree
1114 391 : strip_array_domain (tree type)
1115 : {
1116 391 : tree t2;
1117 391 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1118 391 : if (TYPE_DOMAIN (type) == NULL_TREE)
1119 : return type;
1120 226 : t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
1121 226 : return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
1122 : }
1123 :
1124 : /* Wrapper around cp_common_type that is used by c-common.cc and other
1125 : front end optimizations that remove promotions.
1126 :
1127 : Return the common type for two arithmetic types T1 and T2 under the
1128 : usual arithmetic conversions. The default conversions have already
1129 : been applied, and enumerated types converted to their compatible
1130 : integer types. */
1131 :
1132 : tree
1133 648885 : common_type (tree t1, tree t2)
1134 : {
1135 : /* If one type is nonsense, use the other */
1136 648885 : if (t1 == error_mark_node)
1137 : return t2;
1138 648885 : if (t2 == error_mark_node)
1139 : return t1;
1140 :
1141 648885 : return cp_common_type (t1, t2);
1142 : }
1143 :
1144 : /* Return the common type of two pointer types T1 and T2. This is the
1145 : type for the result of most arithmetic operations if the operands
1146 : have the given two types.
1147 :
1148 : We assume that comp_target_types has already been done and returned
1149 : nonzero; if that isn't so, this may crash. */
1150 :
1151 : tree
1152 1554839 : common_pointer_type (tree t1, tree t2)
1153 : {
1154 1554839 : gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
1155 : || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
1156 : || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
1157 :
1158 1554839 : return composite_pointer_type (input_location, t1, t2,
1159 : error_mark_node, error_mark_node,
1160 1554839 : CPO_CONVERSION, tf_warning_or_error);
1161 : }
1162 :
1163 : /* Compare two exception specifier types for exactness or subsetness, if
1164 : allowed. Returns false for mismatch, true for match (same, or
1165 : derived and !exact).
1166 :
1167 : [except.spec] "If a class X ... objects of class X or any class publicly
1168 : and unambiguously derived from X. Similarly, if a pointer type Y * ...
1169 : exceptions of type Y * or that are pointers to any type publicly and
1170 : unambiguously derived from Y. Otherwise a function only allows exceptions
1171 : that have the same type ..."
1172 : This does not mention cv qualifiers and is different to what throw
1173 : [except.throw] and catch [except.catch] will do. They will ignore the
1174 : top level cv qualifiers, and allow qualifiers in the pointer to class
1175 : example.
1176 :
1177 : We implement the letter of the standard. */
1178 :
1179 : static bool
1180 1690 : comp_except_types (tree a, tree b, bool exact)
1181 : {
1182 1690 : if (same_type_p (a, b))
1183 : return true;
1184 506 : else if (!exact)
1185 : {
1186 11 : if (cp_type_quals (a) || cp_type_quals (b))
1187 : return false;
1188 :
1189 11 : if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1190 : {
1191 7 : a = TREE_TYPE (a);
1192 7 : b = TREE_TYPE (b);
1193 7 : if (cp_type_quals (a) || cp_type_quals (b))
1194 : return false;
1195 : }
1196 :
1197 10 : if (TREE_CODE (a) != RECORD_TYPE
1198 10 : || TREE_CODE (b) != RECORD_TYPE)
1199 : return false;
1200 :
1201 9 : if (publicly_uniquely_derived_p (a, b))
1202 : return true;
1203 : }
1204 : return false;
1205 : }
1206 :
1207 : /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1208 : If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1209 : If EXACT is ce_type, the C++17 type compatibility rules apply.
1210 : If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1211 : If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1212 : are unordered, but we've already filtered out duplicates. Most lists will
1213 : be in order, we should try to make use of that. */
1214 :
1215 : bool
1216 1670393542 : comp_except_specs (const_tree t1, const_tree t2, int exact)
1217 : {
1218 1670393542 : const_tree probe;
1219 1670393542 : const_tree base;
1220 1670393542 : int length = 0;
1221 :
1222 1670393542 : if (t1 == t2)
1223 : return true;
1224 :
1225 : /* First handle noexcept. */
1226 679743959 : if (exact < ce_exact)
1227 : {
1228 4996345 : if (exact == ce_type
1229 9948286 : && (canonical_eh_spec (const_cast<tree> (t1))
1230 4951941 : == canonical_eh_spec (const_cast<tree> (t2))))
1231 : return true;
1232 :
1233 : /* noexcept(false) is compatible with no exception-specification,
1234 : and less strict than any spec. */
1235 4965652 : if (t1 == noexcept_false_spec)
1236 236 : return t2 == NULL_TREE || exact == ce_derived;
1237 : /* Even a derived noexcept(false) is compatible with no
1238 : exception-specification. */
1239 4965416 : if (t2 == noexcept_false_spec)
1240 1387 : return t1 == NULL_TREE;
1241 :
1242 : /* Otherwise, if we aren't looking for an exact match, noexcept is
1243 : equivalent to throw(). */
1244 4964029 : if (t1 == noexcept_true_spec)
1245 723664 : t1 = empty_except_spec;
1246 4964029 : if (t2 == noexcept_true_spec)
1247 4047229 : t2 = empty_except_spec;
1248 : }
1249 :
1250 : /* If any noexcept is left, it is only comparable to itself;
1251 : either we're looking for an exact match or we're redeclaring a
1252 : template with dependent noexcept. */
1253 659011137 : if ((t1 && TREE_PURPOSE (t1))
1254 703269940 : || (t2 && TREE_PURPOSE (t2)))
1255 672650856 : return (t1 && t2
1256 672650856 : && (exact == ce_exact
1257 84048427 : ? TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
1258 192841 : : cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))));
1259 :
1260 7060787 : if (t1 == NULL_TREE) /* T1 is ... */
1261 4267344 : return t2 == NULL_TREE || exact == ce_derived;
1262 2793443 : if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1263 2759801 : return t2 != NULL_TREE && !TREE_VALUE (t2);
1264 35060 : if (t2 == NULL_TREE) /* T2 is ... */
1265 : return false;
1266 1568 : if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1267 76 : return exact == ce_derived;
1268 :
1269 : /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1270 : Count how many we find, to determine exactness. For exact matching and
1271 : ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1272 : O(nm). */
1273 2679 : for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1274 : {
1275 2086 : for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1276 : {
1277 1690 : tree a = TREE_VALUE (probe);
1278 1690 : tree b = TREE_VALUE (t2);
1279 :
1280 1690 : if (comp_except_types (a, b, exact))
1281 : {
1282 1187 : if (probe == base && exact > ce_derived)
1283 1152 : base = TREE_CHAIN (probe);
1284 1187 : length++;
1285 1187 : break;
1286 : }
1287 : }
1288 1187 : if (probe == NULL_TREE)
1289 : return false;
1290 : }
1291 1096 : return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1292 : }
1293 :
1294 : /* Compare the array types T1 and T2. CB says how we should behave when
1295 : comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1296 : bounds_either says than any array can be [], bounds_first means that
1297 : onlt T1 can be an array with unknown bounds. STRICT is true if
1298 : qualifiers must match when comparing the types of the array elements. */
1299 :
1300 : static bool
1301 1924891 : comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1302 : bool strict)
1303 : {
1304 1924891 : tree d1;
1305 1924891 : tree d2;
1306 1924891 : tree max1, max2;
1307 :
1308 1924891 : if (t1 == t2)
1309 : return true;
1310 :
1311 : /* The type of the array elements must be the same. */
1312 1924003 : if (strict
1313 1924003 : ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1314 14318 : : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1315 : return false;
1316 :
1317 1237828 : d1 = TYPE_DOMAIN (t1);
1318 1237828 : d2 = TYPE_DOMAIN (t2);
1319 :
1320 1237828 : if (d1 == d2)
1321 : return true;
1322 :
1323 : /* If one of the arrays is dimensionless, and the other has a
1324 : dimension, they are of different types. However, it is valid to
1325 : write:
1326 :
1327 : extern int a[];
1328 : int a[3];
1329 :
1330 : by [basic.link]:
1331 :
1332 : declarations for an array object can specify
1333 : array types that differ by the presence or absence of a major
1334 : array bound (_dcl.array_). */
1335 679059 : if (!d1 && d2)
1336 7920 : return cb >= bounds_either;
1337 671139 : else if (d1 && !d2)
1338 15307 : return cb == bounds_either;
1339 :
1340 : /* Check that the dimensions are the same. */
1341 :
1342 655832 : if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1343 : return false;
1344 655811 : max1 = TYPE_MAX_VALUE (d1);
1345 655811 : max2 = TYPE_MAX_VALUE (d2);
1346 :
1347 655811 : if (!cp_tree_equal (max1, max2))
1348 : return false;
1349 :
1350 : return true;
1351 : }
1352 :
1353 : /* Compare the relative position of T1 and T2 into their respective
1354 : template parameter list.
1355 : T1 and T2 must be template parameter types.
1356 : Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1357 :
1358 : static bool
1359 1957539419 : comp_template_parms_position (tree t1, tree t2)
1360 : {
1361 1957539419 : tree index1, index2;
1362 1957539419 : gcc_assert (t1 && t2
1363 : && TREE_CODE (t1) == TREE_CODE (t2)
1364 : && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1365 : || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1366 : || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1367 :
1368 1957539419 : index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1369 1957539419 : index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1370 :
1371 : /* Then compare their relative position. */
1372 1957539419 : if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1373 1310734088 : || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1374 2885289250 : || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1375 927749831 : != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1376 : return false;
1377 :
1378 : /* In C++14 we can end up comparing 'auto' to a normal template
1379 : parameter. Don't confuse them. */
1380 834136177 : if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1381 252666886 : return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1382 :
1383 : return true;
1384 : }
1385 :
1386 : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1387 :
1388 : static bool
1389 512 : cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1390 : {
1391 512 : t1 = TYPE_MAIN_VARIANT (t1);
1392 512 : t2 = TYPE_MAIN_VARIANT (t2);
1393 :
1394 512 : if (TYPE_PTR_P (t1)
1395 141 : && TYPE_PTR_P (t2))
1396 : return true;
1397 :
1398 : /* Only the precision of the parameter matters. This check should
1399 : make sure that the callee does not see undefined values in argument
1400 : registers. */
1401 377 : if (INTEGRAL_TYPE_P (t1)
1402 68 : && INTEGRAL_TYPE_P (t2)
1403 424 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1404 : return true;
1405 :
1406 333 : return same_type_p (t1, t2);
1407 : }
1408 :
1409 : /* Check if a type cast between two function types can be considered safe. */
1410 :
1411 : static bool
1412 361 : cxx_safe_function_type_cast_p (tree t1, tree t2)
1413 : {
1414 361 : if (TREE_TYPE (t1) == void_type_node &&
1415 280 : TYPE_ARG_TYPES (t1) == void_list_node)
1416 : return true;
1417 :
1418 309 : if (TREE_TYPE (t2) == void_type_node &&
1419 103 : TYPE_ARG_TYPES (t2) == void_list_node)
1420 : return true;
1421 :
1422 273 : if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1423 : return false;
1424 :
1425 112 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1426 315 : t1 && t2;
1427 203 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1428 239 : if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1429 : return false;
1430 :
1431 : return true;
1432 : }
1433 :
1434 : /* Subroutine in comptypes. */
1435 :
1436 : static bool
1437 17037965542 : structural_comptypes (tree t1, tree t2, int strict)
1438 : {
1439 : /* Both should be types that are not obviously the same. */
1440 17037965542 : gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1441 :
1442 : /* Suppress typename resolution under spec_hasher::equal in place of calling
1443 : push_to_top_level there. */
1444 17037965542 : if (!comparing_specializations)
1445 : {
1446 : /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1447 : current instantiation. */
1448 13587439629 : if (TREE_CODE (t1) == TYPENAME_TYPE)
1449 107037902 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1450 :
1451 13587439629 : if (TREE_CODE (t2) == TYPENAME_TYPE)
1452 139209149 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1453 : }
1454 :
1455 17037965542 : if (TYPE_PTRMEMFUNC_P (t1))
1456 26202028 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1457 17037965542 : if (TYPE_PTRMEMFUNC_P (t2))
1458 29455935 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1459 :
1460 : /* Different classes of types can't be compatible. */
1461 17037965542 : if (TREE_CODE (t1) != TREE_CODE (t2))
1462 : return false;
1463 :
1464 : /* Qualifiers must match. For array types, we will check when we
1465 : recur on the array element types. */
1466 11327308962 : if (TREE_CODE (t1) != ARRAY_TYPE
1467 11327308962 : && cp_type_quals (t1) != cp_type_quals (t2))
1468 : return false;
1469 10623256091 : if (TREE_CODE (t1) == FUNCTION_TYPE
1470 10623256091 : && type_memfn_quals (t1) != type_memfn_quals (t2))
1471 : return false;
1472 : /* Need to check this before TYPE_MAIN_VARIANT.
1473 : FIXME function qualifiers should really change the main variant. */
1474 10623173018 : if (FUNC_OR_METHOD_TYPE_P (t1))
1475 : {
1476 29816570 : if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1477 : return false;
1478 14459509 : if (flag_noexcept_type
1479 28847722 : && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1480 14388213 : TYPE_RAISES_EXCEPTIONS (t2),
1481 : ce_type))
1482 : return false;
1483 : }
1484 :
1485 : /* Allow for two different type nodes which have essentially the same
1486 : definition. Note that we already checked for equality of the type
1487 : qualifiers (just above). */
1488 10603047620 : if (TREE_CODE (t1) != ARRAY_TYPE
1489 10603047620 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1490 532995867 : goto check_alias;
1491 :
1492 : /* Compare the types. Return false on known not-same. Break on not
1493 : known. Never return true from this switch -- you'll break
1494 : specialization comparison. */
1495 10070051753 : switch (TREE_CODE (t1))
1496 : {
1497 : case VOID_TYPE:
1498 : case BOOLEAN_TYPE:
1499 : /* All void and bool types are the same. */
1500 : break;
1501 :
1502 821764808 : case OPAQUE_TYPE:
1503 821764808 : case INTEGER_TYPE:
1504 821764808 : case FIXED_POINT_TYPE:
1505 821764808 : case REAL_TYPE:
1506 : /* With these nodes, we can't determine type equivalence by
1507 : looking at what is stored in the nodes themselves, because
1508 : two nodes might have different TYPE_MAIN_VARIANTs but still
1509 : represent the same type. For example, wchar_t and int could
1510 : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1511 : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1512 : and are distinct types. On the other hand, int and the
1513 : following typedef
1514 :
1515 : typedef int INT __attribute((may_alias));
1516 :
1517 : have identical properties, different TYPE_MAIN_VARIANTs, but
1518 : represent the same type. The canonical type system keeps
1519 : track of equivalence in this case, so we fall back on it. */
1520 821764808 : if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1521 : return false;
1522 :
1523 : /* We don't need or want the attribute comparison. */
1524 9637 : goto check_alias;
1525 :
1526 1762996 : case TEMPLATE_TEMPLATE_PARM:
1527 1762996 : case BOUND_TEMPLATE_TEMPLATE_PARM:
1528 1762996 : if (!comp_template_parms_position (t1, t2))
1529 : return false;
1530 1341766 : if (!comp_template_parms
1531 2683532 : (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1532 3803550 : DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1533 : return false;
1534 929781 : if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1535 : break;
1536 : /* Don't check inheritance. */
1537 : strict = COMPARE_STRICT;
1538 : /* Fall through. */
1539 :
1540 4659112442 : case RECORD_TYPE:
1541 4659112442 : case UNION_TYPE:
1542 8206767838 : if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1543 2956946275 : && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1544 2086604391 : || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1545 5529519568 : && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1546 : break;
1547 :
1548 4594222920 : if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1549 : break;
1550 4594222886 : else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1551 : break;
1552 :
1553 : return false;
1554 :
1555 60684 : case OFFSET_TYPE:
1556 60684 : if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1557 : strict & ~COMPARE_REDECLARATION))
1558 : return false;
1559 58353 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1560 : return false;
1561 : break;
1562 :
1563 1476895785 : case REFERENCE_TYPE:
1564 1476895785 : if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1565 : return false;
1566 : /* fall through to checks for pointer types */
1567 1840051557 : gcc_fallthrough ();
1568 :
1569 1840051557 : case POINTER_TYPE:
1570 1840051557 : if (TYPE_MODE (t1) != TYPE_MODE (t2)
1571 1840051557 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1572 : return false;
1573 : break;
1574 :
1575 1965222 : case FUNCTION_TYPE:
1576 1965222 : if (TYPE_NO_NAMED_ARGS_STDARG_P (t1) != TYPE_NO_NAMED_ARGS_STDARG_P (t2))
1577 : return false;
1578 : /* FALLTHRU */
1579 9381674 : case METHOD_TYPE:
1580 : /* Exception specs and memfn_rquals were checked above. */
1581 9381674 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1582 : return false;
1583 8707345 : if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1584 : return false;
1585 : break;
1586 :
1587 1909685 : case ARRAY_TYPE:
1588 : /* Target types must match incl. qualifiers. */
1589 1909685 : if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1590 1909685 : ? bounds_either : bounds_none),
1591 : /*strict=*/true))
1592 : return false;
1593 : break;
1594 :
1595 1955776423 : case TEMPLATE_TYPE_PARM:
1596 : /* If T1 and T2 don't have the same relative position in their
1597 : template parameters set, they can't be equal. */
1598 1955776423 : if (!comp_template_parms_position (t1, t2))
1599 : return false;
1600 : /* If T1 and T2 don't represent the same class template deduction,
1601 : they aren't equal. */
1602 1270404538 : if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1603 635202269 : CLASS_PLACEHOLDER_TEMPLATE (t2)))
1604 : return false;
1605 : /* Constrained 'auto's are distinct from parms that don't have the same
1606 : constraints. */
1607 618047228 : if (!equivalent_placeholder_constraints (t1, t2))
1608 : return false;
1609 : break;
1610 :
1611 305194549 : case TYPENAME_TYPE:
1612 610389098 : if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1613 305194549 : TYPENAME_TYPE_FULLNAME (t2)))
1614 : return false;
1615 : /* Qualifiers don't matter on scopes. */
1616 267776035 : if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1617 267776035 : TYPE_CONTEXT (t2)))
1618 : return false;
1619 : break;
1620 :
1621 28401 : case UNBOUND_CLASS_TEMPLATE:
1622 28401 : if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1623 : return false;
1624 28205 : if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1625 : return false;
1626 : break;
1627 :
1628 2560880 : case COMPLEX_TYPE:
1629 2560880 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1630 : return false;
1631 : break;
1632 :
1633 12847540 : case VECTOR_TYPE:
1634 12847540 : if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1635 23993182 : || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1636 23993182 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1637 : return false;
1638 : break;
1639 :
1640 7834012 : case TYPE_PACK_EXPANSION:
1641 7834012 : return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1642 : PACK_EXPANSION_PATTERN (t2))
1643 15491752 : && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1644 7657740 : PACK_EXPANSION_EXTRA_ARGS (t2)));
1645 :
1646 34 : case PACK_INDEX_TYPE:
1647 34 : return (cp_tree_equal (PACK_INDEX_PACK (t1),
1648 34 : PACK_INDEX_PACK (t2))
1649 40 : && cp_tree_equal (PACK_INDEX_INDEX (t1),
1650 6 : PACK_INDEX_INDEX (t2)));
1651 :
1652 32536432 : case DECLTYPE_TYPE:
1653 65072864 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1654 32536432 : != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1655 : return false;
1656 32507022 : if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1657 : return false;
1658 32507022 : if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1659 : return false;
1660 32507022 : if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1661 : return false;
1662 : break;
1663 :
1664 749751 : case TRAIT_TYPE:
1665 749751 : if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1666 : return false;
1667 1378 : if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1668 2756 : || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1669 : return false;
1670 : break;
1671 :
1672 3 : case TYPEOF_TYPE:
1673 3 : if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1674 : return false;
1675 : break;
1676 :
1677 : default:
1678 : return false;
1679 : }
1680 :
1681 : /* If we get here, we know that from a target independent POV the
1682 : types are the same. Make sure the target attributes are also
1683 : the same. */
1684 869181091 : if (!comp_type_attributes (t1, t2))
1685 : return false;
1686 :
1687 869179962 : check_alias:
1688 1402185466 : if (comparing_dependent_aliases
1689 1402185466 : && (typedef_variant_p (t1) || typedef_variant_p (t2)))
1690 : {
1691 5211736 : tree dep1 = dependent_opaque_alias_p (t1) ? t1 : NULL_TREE;
1692 5211736 : tree dep2 = dependent_opaque_alias_p (t2) ? t2 : NULL_TREE;
1693 5211736 : if ((dep1 || dep2)
1694 5211736 : && (!(dep1 && dep2)
1695 0 : || !comp_type_attributes (DECL_ATTRIBUTES (TYPE_NAME (dep1)),
1696 0 : DECL_ATTRIBUTES (TYPE_NAME (dep2)))))
1697 : return false;
1698 :
1699 : /* Don't treat an alias template specialization with dependent
1700 : arguments as equivalent to its underlying type when used as a
1701 : template argument; we need them to be distinct so that we
1702 : substitute into the specialization arguments at instantiation
1703 : time. And aliases can't be equivalent without being ==, so
1704 : we don't need to look any deeper. */
1705 5211656 : ++processing_template_decl;
1706 5211656 : dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1707 5211656 : dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1708 5211656 : --processing_template_decl;
1709 5211656 : if ((dep1 || dep2) && dep1 != dep2)
1710 : return false;
1711 : }
1712 :
1713 : return true;
1714 : }
1715 :
1716 : /* C++ implementation of compatible_types_for_indirection_note_p. */
1717 :
1718 : bool
1719 1611 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1720 : {
1721 1611 : return same_type_p (type1, type2);
1722 : }
1723 :
1724 : /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1725 : is a bitwise-or of the COMPARE_* flags. */
1726 :
1727 : bool
1728 25699049756 : comptypes (tree t1, tree t2, int strict)
1729 : {
1730 25699049756 : gcc_checking_assert (t1 && t2);
1731 :
1732 : /* TYPE_ARGUMENT_PACKS are not really types. */
1733 25699049756 : gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1734 : && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1735 :
1736 25699049756 : if (t1 == t2)
1737 : return true;
1738 :
1739 : /* Suppress errors caused by previously reported errors. */
1740 17037966710 : if (t1 == error_mark_node || t2 == error_mark_node)
1741 : return false;
1742 :
1743 17037966545 : if (strict == COMPARE_STRICT)
1744 : {
1745 15424626628 : if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1746 : /* At least one of the types requires structural equality, so
1747 : perform a deep check. */
1748 1790458762 : return structural_comptypes (t1, t2, strict);
1749 :
1750 13634167866 : if (flag_checking && param_use_canonical_types)
1751 : {
1752 13634166863 : bool result = structural_comptypes (t1, t2, strict);
1753 :
1754 13634166863 : if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1755 : /* The two types are structurally equivalent, but their
1756 : canonical types were different. This is a failure of the
1757 : canonical type propagation code.*/
1758 0 : internal_error
1759 0 : ("canonical types differ for identical types %qT and %qT",
1760 : t1, t2);
1761 13634166863 : else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1762 : /* Two types are structurally different, but the canonical
1763 : types are the same. This means we were over-eager in
1764 : assigning canonical types. */
1765 0 : internal_error
1766 0 : ("same canonical type node for different types %qT and %qT",
1767 : t1, t2);
1768 :
1769 : return result;
1770 : }
1771 1003 : if (!flag_checking && param_use_canonical_types)
1772 1003 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1773 : else
1774 0 : return structural_comptypes (t1, t2, strict);
1775 : }
1776 1613339917 : else if (strict == COMPARE_STRUCTURAL)
1777 1546615632 : return structural_comptypes (t1, t2, COMPARE_STRICT);
1778 : else
1779 66724285 : return structural_comptypes (t1, t2, strict);
1780 : }
1781 :
1782 : /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1783 : top-level qualifiers. */
1784 :
1785 : bool
1786 4557692547 : same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1787 : {
1788 4557692547 : if (type1 == error_mark_node || type2 == error_mark_node)
1789 : return false;
1790 4557692536 : if (type1 == type2)
1791 : return true;
1792 :
1793 2529316416 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1794 2529316416 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1795 2529316416 : return same_type_p (type1, type2);
1796 : }
1797 :
1798 : /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1799 :
1800 : bool
1801 391142782 : similar_type_p (tree type1, tree type2)
1802 : {
1803 391142782 : if (type1 == error_mark_node || type2 == error_mark_node)
1804 : return false;
1805 :
1806 : /* Informally, two types are similar if, ignoring top-level cv-qualification:
1807 : * they are the same type; or
1808 : * they are both pointers, and the pointed-to types are similar; or
1809 : * they are both pointers to member of the same class, and the types of
1810 : the pointed-to members are similar; or
1811 : * they are both arrays of the same size or both arrays of unknown bound,
1812 : and the array element types are similar. */
1813 :
1814 391142782 : if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1815 : return true;
1816 :
1817 161533111 : if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1818 161264062 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1819 161262196 : || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1820 273845 : return comp_ptr_ttypes_const (type1, type2, bounds_either);
1821 :
1822 : return false;
1823 : }
1824 :
1825 : /* Helper function for layout_compatible_type_p and
1826 : is_corresponding_member_aggr. Advance to next members (NULL if
1827 : no further ones) and return true if those members are still part of
1828 : the common initial sequence. */
1829 :
1830 : bool
1831 2210 : next_common_initial_sequence (tree &memb1, tree &memb2)
1832 : {
1833 2733 : while (memb1)
1834 : {
1835 2772 : if (TREE_CODE (memb1) != FIELD_DECL
1836 2313 : || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1837 : {
1838 459 : memb1 = DECL_CHAIN (memb1);
1839 459 : continue;
1840 : }
1841 1854 : if (DECL_FIELD_IS_BASE (memb1))
1842 : {
1843 64 : memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1844 64 : continue;
1845 : }
1846 : break;
1847 : }
1848 2694 : while (memb2)
1849 : {
1850 2730 : if (TREE_CODE (memb2) != FIELD_DECL
1851 2274 : || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1852 : {
1853 456 : memb2 = DECL_CHAIN (memb2);
1854 456 : continue;
1855 : }
1856 1818 : if (DECL_FIELD_IS_BASE (memb2))
1857 : {
1858 28 : memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1859 28 : continue;
1860 : }
1861 : break;
1862 : }
1863 2210 : if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1864 : return true;
1865 1790 : if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1866 : return false;
1867 1790 : if (DECL_BIT_FIELD_TYPE (memb1))
1868 : {
1869 165 : if (!DECL_BIT_FIELD_TYPE (memb2))
1870 : return false;
1871 66 : if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1872 66 : DECL_BIT_FIELD_TYPE (memb2)))
1873 : return false;
1874 66 : if (TYPE_PRECISION (TREE_TYPE (memb1))
1875 66 : != TYPE_PRECISION (TREE_TYPE (memb2)))
1876 : return false;
1877 : }
1878 1625 : else if (DECL_BIT_FIELD_TYPE (memb2))
1879 : return false;
1880 1598 : else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1881 : return false;
1882 1598 : if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1883 1598 : != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1884 : return false;
1885 1571 : if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1886 : return false;
1887 1512 : if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1888 : return false;
1889 : return true;
1890 : }
1891 :
1892 : /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1893 :
1894 : bool
1895 2773 : layout_compatible_type_p (tree type1, tree type2, bool explain/*=false*/)
1896 : {
1897 2773 : if (type1 == error_mark_node || type2 == error_mark_node)
1898 : return false;
1899 2773 : if (type1 == type2)
1900 : return true;
1901 :
1902 1359 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1903 1359 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1904 1359 : if (same_type_p (type1, type2))
1905 : return true;
1906 :
1907 654 : if (TREE_CODE (type1) != TREE_CODE (type2)
1908 602 : || (TREE_CODE (type1) != ENUMERAL_TYPE
1909 564 : && !CLASS_TYPE_P (type1))
1910 1107 : || (TREE_CODE (type2) != ENUMERAL_TYPE
1911 415 : && !CLASS_TYPE_P (type2)))
1912 : {
1913 201 : if (explain)
1914 6 : inform (input_location, "%q#T and %q#T are not both the same type, "
1915 : "layout-compatible enumerations, or "
1916 : "layout-compatible standard-layout class types",
1917 : type1, type2);
1918 : return false;
1919 : }
1920 :
1921 453 : if (!std_layout_type_p (type1))
1922 : {
1923 16 : if (explain)
1924 3 : inform (location_of (type1),
1925 : "%qT is not a standard-layout type", type1);
1926 : return false;
1927 : }
1928 437 : if (!std_layout_type_p (type2))
1929 : {
1930 0 : if (explain)
1931 0 : inform (location_of (type2),
1932 : "%qT is not a standard-layout type", type2);
1933 : return false;
1934 : }
1935 :
1936 437 : if (TREE_CODE (type1) == ENUMERAL_TYPE)
1937 : {
1938 38 : tree underlying1 = finish_underlying_type (type1);
1939 38 : tree underlying2 = finish_underlying_type (type2);
1940 38 : if (!same_type_p (underlying1, underlying2))
1941 : {
1942 25 : if (explain)
1943 : {
1944 3 : inform (location_of (type1),
1945 : "the underlying type of %qT is %qT, but",
1946 : type1, underlying1);
1947 3 : inform (location_of (type2),
1948 : "the underlying type of %qT is %qT",
1949 : type2, underlying2);
1950 : }
1951 : return false;
1952 : }
1953 : }
1954 399 : else if (TREE_CODE (type1) == RECORD_TYPE)
1955 : {
1956 315 : tree field1 = TYPE_FIELDS (type1);
1957 315 : tree field2 = TYPE_FIELDS (type2);
1958 1179 : while (1)
1959 : {
1960 747 : if (!next_common_initial_sequence (field1, field2))
1961 : {
1962 27 : if (explain)
1963 : {
1964 6 : if (field1 && field2)
1965 : {
1966 6 : inform (DECL_SOURCE_LOCATION (field1),
1967 : "%qD and %qD do not correspond",
1968 : field1, field2);
1969 6 : inform (DECL_SOURCE_LOCATION (field2),
1970 : "%qD declared here", field2);
1971 : }
1972 0 : else if (field1)
1973 0 : inform (DECL_SOURCE_LOCATION (field1),
1974 : "%qT has no member corresponding to %qD",
1975 : type2, field1);
1976 0 : else if (field2)
1977 0 : inform (DECL_SOURCE_LOCATION (field2),
1978 : "%qT has no member corresponding to %qD",
1979 : type1, field2);
1980 : }
1981 27 : return false;
1982 : }
1983 720 : if (field1 == NULL_TREE)
1984 : break;
1985 432 : field1 = DECL_CHAIN (field1);
1986 432 : field2 = DECL_CHAIN (field2);
1987 : }
1988 : }
1989 84 : else if (TREE_CODE (type1) == UNION_TYPE)
1990 : {
1991 : /* The standard says:
1992 : "Two standard-layout unions are layout-compatible if they have
1993 : the same number of non-static data members and corresponding
1994 : non-static data members (in any order) have layout-compatible
1995 : types."
1996 : but the code anticipates that bitfield vs. non-bitfield,
1997 : different bitfield widths or presence/absence of
1998 : [[no_unique_address]] should be checked as well. */
1999 84 : tree field1 = TYPE_FIELDS (type1);
2000 84 : tree field2 = TYPE_FIELDS (type2);
2001 84 : auto_vec<tree, 16> vec;
2002 84 : unsigned int count = 0;
2003 378 : for (; field1; field1 = DECL_CHAIN (field1))
2004 294 : if (TREE_CODE (field1) == FIELD_DECL)
2005 195 : count++;
2006 372 : for (; field2; field2 = DECL_CHAIN (field2))
2007 288 : if (TREE_CODE (field2) == FIELD_DECL)
2008 189 : vec.safe_push (field2);
2009 :
2010 : /* Discussions on core lean towards treating multiple union fields
2011 : of the same type as the same field, so this might need changing
2012 : in the future. */
2013 168 : if (count != vec.length ())
2014 : {
2015 6 : if (explain)
2016 : {
2017 3 : inform_n (location_of (type1), count,
2018 : "%qT has %u field, but",
2019 : "%qT has %u fields, but",
2020 : type1, count);
2021 6 : inform_n (location_of (type2), vec.length (),
2022 : "%qT has %u field",
2023 : "%qT has %u fields",
2024 : type2, vec.length ());
2025 : }
2026 : return false;
2027 : }
2028 :
2029 324 : for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
2030 : {
2031 258 : if (TREE_CODE (field1) != FIELD_DECL)
2032 81 : continue;
2033 177 : unsigned int j;
2034 177 : tree t1 = DECL_BIT_FIELD_TYPE (field1);
2035 177 : if (t1 == NULL_TREE)
2036 174 : t1 = TREE_TYPE (field1);
2037 474 : FOR_EACH_VEC_ELT (vec, j, field2)
2038 : {
2039 285 : tree t2 = DECL_BIT_FIELD_TYPE (field2);
2040 285 : if (t2 == NULL_TREE)
2041 273 : t2 = TREE_TYPE (field2);
2042 285 : if (DECL_BIT_FIELD_TYPE (field1))
2043 : {
2044 6 : if (!DECL_BIT_FIELD_TYPE (field2))
2045 0 : continue;
2046 6 : if (TYPE_PRECISION (TREE_TYPE (field1))
2047 6 : != TYPE_PRECISION (TREE_TYPE (field2)))
2048 6 : continue;
2049 : }
2050 279 : else if (DECL_BIT_FIELD_TYPE (field2))
2051 6 : continue;
2052 273 : if (!layout_compatible_type_p (t1, t2))
2053 108 : continue;
2054 165 : if ((!lookup_attribute ("no_unique_address",
2055 165 : DECL_ATTRIBUTES (field1)))
2056 165 : != !lookup_attribute ("no_unique_address",
2057 165 : DECL_ATTRIBUTES (field2)))
2058 0 : continue;
2059 : break;
2060 : }
2061 354 : if (j == vec.length ())
2062 : {
2063 12 : if (explain)
2064 : {
2065 3 : inform (DECL_SOURCE_LOCATION (field1),
2066 : "%qT has no member corresponding to %qD",
2067 : type2, field1);
2068 3 : inform (location_of (type2), "%qT declared here", type2);
2069 : }
2070 : return false;
2071 : }
2072 165 : vec.unordered_remove (j);
2073 : }
2074 84 : }
2075 : else
2076 0 : gcc_unreachable ();
2077 :
2078 : return true;
2079 : }
2080 :
2081 : /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
2082 :
2083 : bool
2084 253077274 : at_least_as_qualified_p (const_tree type1, const_tree type2)
2085 : {
2086 253077274 : int q1 = cp_type_quals (type1);
2087 253077274 : int q2 = cp_type_quals (type2);
2088 :
2089 : /* All qualifiers for TYPE2 must also appear in TYPE1. */
2090 253077274 : return (q1 & q2) == q2;
2091 : }
2092 :
2093 : /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
2094 : more cv-qualified that TYPE1, and 0 otherwise. */
2095 :
2096 : int
2097 15247552 : comp_cv_qualification (int q1, int q2)
2098 : {
2099 15247552 : if (q1 == q2)
2100 : return 0;
2101 :
2102 3431567 : if ((q1 & q2) == q2)
2103 : return 1;
2104 365062 : else if ((q1 & q2) == q1)
2105 364477 : return -1;
2106 :
2107 : return 0;
2108 : }
2109 :
2110 : int
2111 0 : comp_cv_qualification (const_tree type1, const_tree type2)
2112 : {
2113 0 : int q1 = cp_type_quals (type1);
2114 0 : int q2 = cp_type_quals (type2);
2115 0 : return comp_cv_qualification (q1, q2);
2116 : }
2117 :
2118 : /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
2119 : subset of the cv-qualification signature of TYPE2, and the types
2120 : are similar. Returns -1 if the other way 'round, and 0 otherwise. */
2121 :
2122 : int
2123 385 : comp_cv_qual_signature (tree type1, tree type2)
2124 : {
2125 385 : if (comp_ptr_ttypes_real (type2, type1, -1))
2126 : return 1;
2127 311 : else if (comp_ptr_ttypes_real (type1, type2, -1))
2128 : return -1;
2129 : else
2130 299 : return 0;
2131 : }
2132 :
2133 : /* Subroutines of `comptypes'. */
2134 :
2135 : /* Return true if two parameter type lists PARMS1 and PARMS2 are
2136 : equivalent in the sense that functions with those parameter types
2137 : can have equivalent types. The two lists must be equivalent,
2138 : element by element. */
2139 :
2140 : bool
2141 1127211236 : compparms (const_tree parms1, const_tree parms2)
2142 : {
2143 1127211236 : const_tree t1, t2;
2144 :
2145 : /* An unspecified parmlist matches any specified parmlist
2146 : whose argument types don't need default promotions. */
2147 :
2148 1127211236 : for (t1 = parms1, t2 = parms2;
2149 1787429227 : t1 || t2;
2150 660217991 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2151 : {
2152 : /* If one parmlist is shorter than the other,
2153 : they fail to match. */
2154 1671417432 : if (!t1 || !t2)
2155 : return false;
2156 1670823042 : if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2157 : return false;
2158 : }
2159 : return true;
2160 : }
2161 :
2162 :
2163 : /* Process a sizeof or alignof expression where the operand is a type.
2164 : STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2165 : or GNU (preferred alignment) semantics; it is ignored if OP is
2166 : SIZEOF_EXPR. */
2167 :
2168 : tree
2169 30424053 : cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2170 : bool std_alignof, bool complain)
2171 : {
2172 30424053 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2173 30424053 : if (type == error_mark_node)
2174 : return error_mark_node;
2175 :
2176 30424051 : type = non_reference (type);
2177 30424051 : if (TREE_CODE (type) == METHOD_TYPE)
2178 : {
2179 0 : if (complain)
2180 : {
2181 0 : pedwarn (loc, OPT_Wpointer_arith,
2182 : "invalid application of %qs to a member function",
2183 0 : OVL_OP_INFO (false, op)->name);
2184 0 : return size_one_node;
2185 : }
2186 : else
2187 0 : return error_mark_node;
2188 : }
2189 30424051 : else if (VOID_TYPE_P (type) && std_alignof)
2190 : {
2191 12 : if (complain)
2192 12 : error_at (loc, "invalid application of %qs to a void type",
2193 12 : OVL_OP_INFO (false, op)->name);
2194 12 : return error_mark_node;
2195 : }
2196 :
2197 30424039 : bool dependent_p = dependent_type_p (type);
2198 30424039 : if (!dependent_p)
2199 : {
2200 25309740 : complete_type (type);
2201 25309740 : if (!COMPLETE_TYPE_P (type))
2202 : /* Call this here because the incompleteness diagnostic comes from
2203 : c_sizeof_or_alignof_type instead of
2204 : complete_type_or_maybe_complain. */
2205 288 : note_failed_type_completion (type, complain);
2206 : }
2207 25309740 : if (dependent_p
2208 : /* VLA types will have a non-constant size. In the body of an
2209 : uninstantiated template, we don't need to try to compute the
2210 : value, because the sizeof expression is not an integral
2211 : constant expression in that case. And, if we do try to
2212 : compute the value, we'll likely end up with SAVE_EXPRs, which
2213 : the template substitution machinery does not expect to see. */
2214 25309740 : || (processing_template_decl
2215 2330399 : && COMPLETE_TYPE_P (type)
2216 2330393 : && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2217 : {
2218 5114299 : tree value = build_min (op, size_type_node, type);
2219 5114299 : TREE_READONLY (value) = 1;
2220 5114299 : if (op == ALIGNOF_EXPR && std_alignof)
2221 583370 : ALIGNOF_EXPR_STD_P (value) = true;
2222 5114299 : SET_EXPR_LOCATION (value, loc);
2223 5114299 : return value;
2224 : }
2225 :
2226 25309740 : return c_sizeof_or_alignof_type (loc, complete_type (type),
2227 : op == SIZEOF_EXPR, std_alignof,
2228 25309740 : complain & (tf_warning_or_error));
2229 : }
2230 :
2231 : /* Return the size of the type, without producing any warnings for
2232 : types whose size cannot be taken. This routine should be used only
2233 : in some other routine that has already produced a diagnostic about
2234 : using the size of such a type. */
2235 : tree
2236 2871729 : cxx_sizeof_nowarn (tree type)
2237 : {
2238 2871729 : if (TREE_CODE (type) == FUNCTION_TYPE
2239 2871729 : || VOID_TYPE_P (type)
2240 2871647 : || TREE_CODE (type) == ERROR_MARK)
2241 82 : return size_one_node;
2242 2871647 : else if (!COMPLETE_TYPE_P (type))
2243 25 : return size_zero_node;
2244 : else
2245 2871622 : return cxx_sizeof_or_alignof_type (input_location, type,
2246 2871622 : SIZEOF_EXPR, false, false);
2247 : }
2248 :
2249 : /* Process a sizeof expression where the operand is an expression. */
2250 :
2251 : static tree
2252 1333946 : cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2253 : {
2254 1333946 : if (e == error_mark_node)
2255 : return error_mark_node;
2256 :
2257 1333211 : if (instantiation_dependent_uneval_expression_p (e))
2258 : {
2259 366232 : e = build_min (SIZEOF_EXPR, size_type_node, e);
2260 366232 : TREE_SIDE_EFFECTS (e) = 0;
2261 366232 : TREE_READONLY (e) = 1;
2262 366232 : SET_EXPR_LOCATION (e, loc);
2263 :
2264 366232 : return e;
2265 : }
2266 :
2267 966979 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2268 966979 : STRIP_ANY_LOCATION_WRAPPER (e);
2269 :
2270 966979 : if (TREE_CODE (e) == PARM_DECL
2271 36393 : && DECL_ARRAY_PARAMETER_P (e)
2272 967356 : && (complain & tf_warning))
2273 : {
2274 120 : auto_diagnostic_group d;
2275 120 : if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2276 : "%<sizeof%> on array function parameter %qE "
2277 120 : "will return size of %qT", e, TREE_TYPE (e)))
2278 24 : inform (DECL_SOURCE_LOCATION (e), "declared here");
2279 120 : }
2280 :
2281 966979 : e = mark_type_use (e);
2282 :
2283 966979 : if (bitfield_p (e))
2284 : {
2285 15 : if (complain & tf_error)
2286 6 : error_at (e_loc,
2287 : "invalid application of %<sizeof%> to a bit-field");
2288 : else
2289 9 : return error_mark_node;
2290 6 : e = char_type_node;
2291 : }
2292 966964 : else if (is_overloaded_fn (e))
2293 : {
2294 36 : if (complain & tf_error)
2295 12 : permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2296 : "an expression of function type");
2297 : else
2298 24 : return error_mark_node;
2299 12 : e = char_type_node;
2300 : }
2301 966928 : else if (type_unknown_p (e))
2302 : {
2303 0 : if (complain & tf_error)
2304 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2305 : else
2306 0 : return error_mark_node;
2307 0 : e = char_type_node;
2308 : }
2309 : else
2310 966928 : e = TREE_TYPE (e);
2311 :
2312 966946 : return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2313 966946 : complain & tf_error);
2314 : }
2315 :
2316 : /* Implement the __alignof keyword: Return the minimum required
2317 : alignment of E, measured in bytes. For VAR_DECL's and
2318 : FIELD_DECL's return DECL_ALIGN (which can be set from an
2319 : "aligned" __attribute__ specification). STD_ALIGNOF acts
2320 : like in cxx_sizeof_or_alignof_type. */
2321 :
2322 : static tree
2323 101113 : cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2324 : tsubst_flags_t complain)
2325 : {
2326 101113 : tree t;
2327 :
2328 101113 : if (e == error_mark_node)
2329 : return error_mark_node;
2330 :
2331 101105 : if (processing_template_decl)
2332 : {
2333 19278 : e = build_min (ALIGNOF_EXPR, size_type_node, e);
2334 19278 : TREE_SIDE_EFFECTS (e) = 0;
2335 19278 : TREE_READONLY (e) = 1;
2336 19278 : SET_EXPR_LOCATION (e, loc);
2337 19278 : ALIGNOF_EXPR_STD_P (e) = std_alignof;
2338 :
2339 19278 : return e;
2340 : }
2341 :
2342 81827 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2343 81827 : STRIP_ANY_LOCATION_WRAPPER (e);
2344 :
2345 81827 : e = mark_type_use (e);
2346 :
2347 81827 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2348 81827 : !(complain & tf_error)))
2349 : {
2350 0 : if (!(complain & tf_error))
2351 0 : return error_mark_node;
2352 0 : t = size_one_node;
2353 : }
2354 81827 : else if (VAR_P (e))
2355 12390 : t = size_int (DECL_ALIGN_UNIT (e));
2356 69437 : else if (bitfield_p (e))
2357 : {
2358 6 : if (complain & tf_error)
2359 6 : error_at (e_loc,
2360 : "invalid application of %<__alignof%> to a bit-field");
2361 : else
2362 0 : return error_mark_node;
2363 6 : t = size_one_node;
2364 : }
2365 69431 : else if (TREE_CODE (e) == COMPONENT_REF
2366 69431 : && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2367 37792 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2368 31639 : else if (is_overloaded_fn (e))
2369 : {
2370 6 : if (complain & tf_error)
2371 6 : permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2372 : "an expression of function type");
2373 : else
2374 0 : return error_mark_node;
2375 6 : if (TREE_CODE (e) == FUNCTION_DECL)
2376 3 : t = size_int (DECL_ALIGN_UNIT (e));
2377 : else
2378 3 : t = size_one_node;
2379 : }
2380 31633 : else if (type_unknown_p (e))
2381 : {
2382 0 : if (complain & tf_error)
2383 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2384 : else
2385 0 : return error_mark_node;
2386 0 : t = size_one_node;
2387 : }
2388 : else
2389 31633 : return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2390 : ALIGNOF_EXPR, std_alignof,
2391 31633 : complain & tf_error);
2392 :
2393 50194 : return fold_convert_loc (loc, size_type_node, t);
2394 : }
2395 :
2396 : /* Process a sizeof or alignof expression E with code OP where the operand
2397 : is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2398 :
2399 : tree
2400 1435059 : cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2401 : bool std_alignof, bool complain)
2402 : {
2403 1435059 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2404 1435059 : if (op == SIZEOF_EXPR)
2405 1953145 : return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2406 : else
2407 101140 : return cxx_alignof_expr (loc, e, std_alignof,
2408 101113 : complain? tf_warning_or_error : tf_none);
2409 : }
2410 :
2411 : /* Build a representation of an expression 'alignas(E).' Return the
2412 : folded integer value of E if it is an integral constant expression
2413 : that resolves to a valid alignment. If E depends on a template
2414 : parameter, return a syntactic representation tree of kind
2415 : ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2416 : expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2417 :
2418 : tree
2419 277481 : cxx_alignas_expr (tree e)
2420 : {
2421 277481 : if (e == NULL_TREE || e == error_mark_node
2422 554962 : || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2423 : return e;
2424 :
2425 277481 : if (TYPE_P (e))
2426 : /* [dcl.align]/3:
2427 :
2428 : When the alignment-specifier is of the form
2429 : alignas(type-id), it shall have the same effect as
2430 : alignas(alignof(type-id)). */
2431 :
2432 145818 : return cxx_sizeof_or_alignof_type (input_location,
2433 : e, ALIGNOF_EXPR,
2434 : /*std_alignof=*/true,
2435 145818 : /*complain=*/true);
2436 :
2437 : /* If we reach this point, it means the alignas expression if of
2438 : the form "alignas(assignment-expression)", so we should follow
2439 : what is stated by [dcl.align]/2. */
2440 :
2441 131663 : if (value_dependent_expression_p (e))
2442 : /* Leave value-dependent expression alone for now. */
2443 : return e;
2444 :
2445 67940 : e = instantiate_non_dependent_expr (e);
2446 67940 : e = mark_rvalue_use (e);
2447 :
2448 : /* [dcl.align]/2 says:
2449 :
2450 : the assignment-expression shall be an integral constant
2451 : expression. */
2452 :
2453 67940 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2454 : {
2455 18 : error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2456 18 : return error_mark_node;
2457 : }
2458 :
2459 67922 : return cxx_constant_value (e);
2460 : }
2461 :
2462 :
2463 : /* EXPR is being used in a context that is not a function call.
2464 : Enforce:
2465 :
2466 : [expr.ref]
2467 :
2468 : The expression can be used only as the left-hand operand of a
2469 : member function call.
2470 :
2471 : [expr.mptr.operator]
2472 :
2473 : If the result of .* or ->* is a function, then that result can be
2474 : used only as the operand for the function call operator ().
2475 :
2476 : by issuing an error message if appropriate. Returns true iff EXPR
2477 : violates these rules. */
2478 :
2479 : bool
2480 1548839295 : invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2481 : {
2482 1548839295 : if (expr == NULL_TREE)
2483 : return false;
2484 : /* Don't enforce this in MS mode. */
2485 1548837845 : if (flag_ms_extensions)
2486 : return false;
2487 1548829784 : if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2488 129287112 : expr = get_first_fn (expr);
2489 1548829784 : if (TREE_TYPE (expr)
2490 1548829784 : && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2491 : {
2492 96 : if (complain & tf_error)
2493 : {
2494 88 : if (DECL_P (expr))
2495 : {
2496 70 : auto_diagnostic_group d;
2497 70 : error_at (loc, "invalid use of non-static member function %qD",
2498 : expr);
2499 70 : inform (DECL_SOURCE_LOCATION (expr), "declared here");
2500 70 : }
2501 : else
2502 18 : error_at (loc, "invalid use of non-static member function of "
2503 18 : "type %qT", TREE_TYPE (expr));
2504 : }
2505 : return true;
2506 : }
2507 : return false;
2508 : }
2509 :
2510 : /* If EXP is a reference to a bit-field, and the type of EXP does not
2511 : match the declared type of the bit-field, return the declared type
2512 : of the bit-field. Otherwise, return NULL_TREE. */
2513 :
2514 : tree
2515 3712923177 : is_bitfield_expr_with_lowered_type (const_tree exp)
2516 : {
2517 5067446898 : switch (TREE_CODE (exp))
2518 : {
2519 8837658 : case COND_EXPR:
2520 17675316 : if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2521 8837632 : ? TREE_OPERAND (exp, 1)
2522 26 : : TREE_OPERAND (exp, 0)))
2523 : return NULL_TREE;
2524 141 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2525 :
2526 1075423 : case COMPOUND_EXPR:
2527 1075423 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2528 :
2529 444428681 : case MODIFY_EXPR:
2530 444428681 : case SAVE_EXPR:
2531 444428681 : case UNARY_PLUS_EXPR:
2532 444428681 : case PREDECREMENT_EXPR:
2533 444428681 : case PREINCREMENT_EXPR:
2534 444428681 : case POSTDECREMENT_EXPR:
2535 444428681 : case POSTINCREMENT_EXPR:
2536 444428681 : case NEGATE_EXPR:
2537 444428681 : case NON_LVALUE_EXPR:
2538 444428681 : case BIT_NOT_EXPR:
2539 444428681 : case CLEANUP_POINT_EXPR:
2540 444428681 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2541 :
2542 269900961 : case COMPONENT_REF:
2543 269900961 : {
2544 269900961 : tree field;
2545 :
2546 269900961 : field = TREE_OPERAND (exp, 1);
2547 269900961 : if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2548 : return NULL_TREE;
2549 22561041 : if (same_type_ignoring_top_level_qualifiers_p
2550 22561041 : (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2551 : return NULL_TREE;
2552 22393105 : return DECL_BIT_FIELD_TYPE (field);
2553 : }
2554 :
2555 688136281 : case VAR_DECL:
2556 688136281 : if (DECL_HAS_VALUE_EXPR_P (exp))
2557 1384250 : return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2558 1384250 : (const_cast<tree> (exp)));
2559 : return NULL_TREE;
2560 :
2561 914736834 : case VIEW_CONVERT_EXPR:
2562 914736834 : if (location_wrapper_p (exp))
2563 907635226 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2564 : else
2565 : return NULL_TREE;
2566 :
2567 : default:
2568 : return NULL_TREE;
2569 : }
2570 : }
2571 :
2572 : /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2573 : bitfield with a lowered type, the type of EXP is returned, rather
2574 : than NULL_TREE. */
2575 :
2576 : tree
2577 1780385305 : unlowered_expr_type (const_tree exp)
2578 : {
2579 1780385305 : tree type;
2580 1780385305 : tree etype = TREE_TYPE (exp);
2581 :
2582 1780385305 : type = is_bitfield_expr_with_lowered_type (exp);
2583 1780385305 : if (type)
2584 17982888 : type = cp_build_qualified_type (type, cp_type_quals (etype));
2585 : else
2586 : type = etype;
2587 :
2588 1780385305 : return type;
2589 : }
2590 :
2591 : /* Perform the conversions in [expr] that apply when an lvalue appears
2592 : in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2593 : function-to-pointer conversions. In addition, bitfield references are
2594 : converted to their declared types. Note that this function does not perform
2595 : the lvalue-to-rvalue conversion for class types. If you need that conversion
2596 : for class types, then you probably need to use force_rvalue.
2597 :
2598 : Although the returned value is being used as an rvalue, this
2599 : function does not wrap the returned expression in a
2600 : NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2601 : that the return value is no longer an lvalue. */
2602 :
2603 : tree
2604 1226760820 : decay_conversion (tree exp,
2605 : tsubst_flags_t complain,
2606 : bool reject_builtin /* = true */)
2607 : {
2608 1226760820 : tree type;
2609 1226760820 : enum tree_code code;
2610 1226760820 : location_t loc = cp_expr_loc_or_input_loc (exp);
2611 :
2612 1226760820 : type = TREE_TYPE (exp);
2613 1226760820 : if (type == error_mark_node)
2614 : return error_mark_node;
2615 :
2616 1226760606 : exp = resolve_nondeduced_context_or_error (exp, complain);
2617 :
2618 1226760606 : code = TREE_CODE (type);
2619 :
2620 1226760606 : if (error_operand_p (exp))
2621 90 : return error_mark_node;
2622 :
2623 1226760516 : if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2624 : {
2625 1774399 : mark_rvalue_use (exp, loc, reject_builtin);
2626 1774399 : return nullptr_node;
2627 : }
2628 :
2629 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2630 : Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2631 1224986117 : if (code == VOID_TYPE)
2632 : {
2633 5800 : if (complain & tf_error)
2634 3 : error_at (loc, "void value not ignored as it ought to be");
2635 5800 : return error_mark_node;
2636 : }
2637 1224980317 : if (invalid_nonstatic_memfn_p (loc, exp, complain))
2638 6 : return error_mark_node;
2639 1224980311 : if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2640 : {
2641 134902860 : exp = mark_lvalue_use (exp);
2642 134902860 : if (reject_builtin && reject_gcc_builtin (exp, loc))
2643 109 : return error_mark_node;
2644 134902751 : return cp_build_addr_expr (exp, complain);
2645 : }
2646 1090077451 : if (code == ARRAY_TYPE)
2647 : {
2648 6857248 : tree adr;
2649 6857248 : tree ptrtype;
2650 :
2651 6857248 : exp = mark_lvalue_use (exp);
2652 :
2653 6857248 : if (INDIRECT_REF_P (exp))
2654 165848 : return build_nop (build_pointer_type (TREE_TYPE (type)),
2655 331696 : TREE_OPERAND (exp, 0));
2656 :
2657 6691400 : if (TREE_CODE (exp) == COMPOUND_EXPR)
2658 : {
2659 3 : tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2660 3 : if (op1 == error_mark_node)
2661 : return error_mark_node;
2662 3 : return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2663 6 : TREE_OPERAND (exp, 0), op1);
2664 : }
2665 :
2666 6691397 : if (!obvalue_p (exp)
2667 6691397 : && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2668 : {
2669 0 : if (complain & tf_error)
2670 0 : error_at (loc, "invalid use of non-lvalue array");
2671 0 : return error_mark_node;
2672 : }
2673 :
2674 6691397 : ptrtype = build_pointer_type (TREE_TYPE (type));
2675 :
2676 6691397 : if (VAR_P (exp))
2677 : {
2678 578086 : if (!cxx_mark_addressable (exp))
2679 0 : return error_mark_node;
2680 578086 : adr = build_nop (ptrtype, build_address (exp));
2681 578086 : return adr;
2682 : }
2683 : /* This way is better for a COMPONENT_REF since it can
2684 : simplify the offset for a component. */
2685 6113311 : adr = cp_build_addr_expr (exp, complain);
2686 6113311 : return cp_convert (ptrtype, adr, complain);
2687 : }
2688 :
2689 : /* Otherwise, it's the lvalue-to-rvalue conversion. */
2690 1083220203 : exp = mark_rvalue_use (exp, loc, reject_builtin);
2691 :
2692 : /* If a bitfield is used in a context where integral promotion
2693 : applies, then the caller is expected to have used
2694 : default_conversion. That function promotes bitfields correctly
2695 : before calling this function. At this point, if we have a
2696 : bitfield referenced, we may assume that is not subject to
2697 : promotion, and that, therefore, the type of the resulting rvalue
2698 : is the declared type of the bitfield. */
2699 1083220203 : exp = convert_bitfield_to_declared_type (exp);
2700 :
2701 : /* We do not call rvalue() here because we do not want to wrap EXP
2702 : in a NON_LVALUE_EXPR. */
2703 :
2704 : /* [basic.lval]
2705 :
2706 : Non-class rvalues always have cv-unqualified types. */
2707 1083220203 : type = TREE_TYPE (exp);
2708 1083220203 : if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2709 332948754 : exp = build_nop (cv_unqualified (type), exp);
2710 :
2711 1083220203 : if (!complete_type_or_maybe_complain (type, exp, complain))
2712 42 : return error_mark_node;
2713 :
2714 : return exp;
2715 : }
2716 :
2717 : /* Perform preparatory conversions, as part of the "usual arithmetic
2718 : conversions". In particular, as per [expr]:
2719 :
2720 : Whenever an lvalue expression appears as an operand of an
2721 : operator that expects the rvalue for that operand, the
2722 : lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2723 : standard conversions are applied to convert the expression to an
2724 : rvalue.
2725 :
2726 : In addition, we perform integral promotions here, as those are
2727 : applied to both operands to a binary operator before determining
2728 : what additional conversions should apply. */
2729 :
2730 : static tree
2731 320583685 : cp_default_conversion (tree exp, tsubst_flags_t complain)
2732 : {
2733 : /* Check for target-specific promotions. */
2734 320583685 : tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2735 320583685 : if (promoted_type)
2736 0 : exp = cp_convert (promoted_type, exp, complain);
2737 : /* Perform the integral promotions first so that bitfield
2738 : expressions (which may promote to "int", even if the bitfield is
2739 : declared "unsigned") are promoted correctly. */
2740 320583685 : else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2741 223151532 : exp = cp_perform_integral_promotions (exp, complain);
2742 : /* Perform the other conversions. */
2743 320583685 : exp = decay_conversion (exp, complain);
2744 :
2745 320583685 : return exp;
2746 : }
2747 :
2748 : /* C version. */
2749 :
2750 : tree
2751 41878718 : default_conversion (tree exp)
2752 : {
2753 41878718 : return cp_default_conversion (exp, tf_warning_or_error);
2754 : }
2755 :
2756 : /* EXPR is an expression with an integral or enumeration type.
2757 : Perform the integral promotions in [conv.prom], and return the
2758 : converted value. */
2759 :
2760 : tree
2761 229520937 : cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2762 : {
2763 229520937 : tree type;
2764 229520937 : tree promoted_type;
2765 :
2766 229520937 : expr = mark_rvalue_use (expr);
2767 229520937 : if (error_operand_p (expr))
2768 6 : return error_mark_node;
2769 :
2770 229520931 : type = TREE_TYPE (expr);
2771 :
2772 : /* [conv.prom]
2773 :
2774 : A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2775 : of type int if int can represent all the values of the bit-field;
2776 : otherwise, it can be converted to unsigned int if unsigned int can
2777 : represent all the values of the bit-field. If the bit-field is larger yet,
2778 : no integral promotion applies to it. If the bit-field has an enumerated
2779 : type, it is treated as any other value of that type for promotion
2780 : purposes. */
2781 229520931 : tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2782 229520931 : if (bitfield_type
2783 229520931 : && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2784 553594 : || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2785 : type = bitfield_type;
2786 :
2787 229520931 : gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2788 : /* Scoped enums don't promote. */
2789 229520931 : if (SCOPED_ENUM_P (type))
2790 : return expr;
2791 228878599 : promoted_type = type_promotes_to (type);
2792 228878599 : if (type != promoted_type)
2793 69223506 : expr = cp_convert (promoted_type, expr, complain);
2794 159655093 : else if (bitfield_type && bitfield_type != type)
2795 : /* Prevent decay_conversion from converting to bitfield_type. */
2796 159 : expr = build_nop (type, expr);
2797 : return expr;
2798 : }
2799 :
2800 : /* C version. */
2801 :
2802 : tree
2803 1868616 : perform_integral_promotions (tree expr)
2804 : {
2805 1868616 : return cp_perform_integral_promotions (expr, tf_warning_or_error);
2806 : }
2807 :
2808 : /* Returns nonzero iff exp is a STRING_CST or the result of applying
2809 : decay_conversion to one. */
2810 :
2811 : int
2812 3887479 : string_conv_p (const_tree totype, const_tree exp, int warn)
2813 : {
2814 3887479 : tree t;
2815 :
2816 3887479 : if (!TYPE_PTR_P (totype))
2817 : return 0;
2818 :
2819 3886588 : t = TREE_TYPE (totype);
2820 3886588 : if (!same_type_p (t, char_type_node)
2821 3737188 : && !same_type_p (t, char8_type_node)
2822 3673843 : && !same_type_p (t, char16_type_node)
2823 3622098 : && !same_type_p (t, char32_type_node)
2824 7456942 : && !same_type_p (t, wchar_type_node))
2825 : return 0;
2826 :
2827 357469 : location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2828 :
2829 357469 : STRIP_ANY_LOCATION_WRAPPER (exp);
2830 :
2831 357469 : if (TREE_CODE (exp) == STRING_CST)
2832 : {
2833 : /* Make sure that we don't try to convert between char and wide chars. */
2834 108 : if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2835 : return 0;
2836 : }
2837 : else
2838 : {
2839 : /* Is this a string constant which has decayed to 'const char *'? */
2840 357361 : t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2841 357361 : if (!same_type_p (TREE_TYPE (exp), t))
2842 : return 0;
2843 14455 : STRIP_NOPS (exp);
2844 14455 : if (TREE_CODE (exp) != ADDR_EXPR
2845 14455 : || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2846 : return 0;
2847 : }
2848 306 : if (warn)
2849 : {
2850 102 : if (cxx_dialect >= cxx11)
2851 87 : pedwarn (loc, OPT_Wwrite_strings,
2852 : "ISO C++ forbids converting a string constant to %qT",
2853 : totype);
2854 : else
2855 15 : warning_at (loc, OPT_Wwrite_strings,
2856 : "deprecated conversion from string constant to %qT",
2857 : totype);
2858 : }
2859 :
2860 : return 1;
2861 : }
2862 :
2863 : /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2864 : can, for example, use as an lvalue. This code used to be in
2865 : unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2866 : expressions, where we're dealing with aggregates. But now it's again only
2867 : called from unary_complex_lvalue. The case (in particular) that led to
2868 : this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2869 : get it there. */
2870 :
2871 : static tree
2872 87065 : rationalize_conditional_expr (enum tree_code code, tree t,
2873 : tsubst_flags_t complain)
2874 : {
2875 87065 : location_t loc = cp_expr_loc_or_input_loc (t);
2876 :
2877 : /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2878 : the first operand is always the one to be used if both operands
2879 : are equal, so we know what conditional expression this used to be. */
2880 87065 : if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2881 : {
2882 0 : tree op0 = TREE_OPERAND (t, 0);
2883 0 : tree op1 = TREE_OPERAND (t, 1);
2884 :
2885 : /* The following code is incorrect if either operand side-effects. */
2886 0 : gcc_assert (!TREE_SIDE_EFFECTS (op0)
2887 : && !TREE_SIDE_EFFECTS (op1));
2888 0 : return
2889 0 : build_conditional_expr (loc,
2890 0 : build_x_binary_op (loc,
2891 0 : (TREE_CODE (t) == MIN_EXPR
2892 : ? LE_EXPR : GE_EXPR),
2893 0 : op0, TREE_CODE (op0),
2894 0 : op1, TREE_CODE (op1),
2895 : NULL_TREE,
2896 : /*overload=*/NULL,
2897 : complain),
2898 : cp_build_unary_op (code, op0, false, complain),
2899 : cp_build_unary_op (code, op1, false, complain),
2900 : complain);
2901 : }
2902 :
2903 87065 : tree op1 = TREE_OPERAND (t, 1);
2904 87065 : if (TREE_CODE (op1) != THROW_EXPR)
2905 87062 : op1 = cp_build_unary_op (code, op1, false, complain);
2906 87065 : tree op2 = TREE_OPERAND (t, 2);
2907 87065 : if (TREE_CODE (op2) != THROW_EXPR)
2908 87059 : op2 = cp_build_unary_op (code, op2, false, complain);
2909 :
2910 87065 : return
2911 87065 : build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2912 : }
2913 :
2914 : /* Given the TYPE of an anonymous union field inside T, return the
2915 : FIELD_DECL for the field. If not found return NULL_TREE. Because
2916 : anonymous unions can nest, we must also search all anonymous unions
2917 : that are directly reachable. */
2918 :
2919 : tree
2920 1524326 : lookup_anon_field (tree, tree type)
2921 : {
2922 1524326 : tree field;
2923 :
2924 1524326 : type = TYPE_MAIN_VARIANT (type);
2925 1524326 : field = ANON_AGGR_TYPE_FIELD (type);
2926 1524326 : gcc_assert (field);
2927 1524326 : return field;
2928 : }
2929 :
2930 : /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2931 : expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2932 : non-NULL, it indicates the path to the base used to name MEMBER.
2933 : If PRESERVE_REFERENCE is true, the expression returned will have
2934 : REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2935 : returned will have the type referred to by the reference.
2936 :
2937 : This function does not perform access control; that is either done
2938 : earlier by the parser when the name of MEMBER is resolved to MEMBER
2939 : itself, or later when overload resolution selects one of the
2940 : functions indicated by MEMBER. */
2941 :
2942 : tree
2943 124304963 : build_class_member_access_expr (cp_expr object, tree member,
2944 : tree access_path, bool preserve_reference,
2945 : tsubst_flags_t complain)
2946 : {
2947 124304963 : tree object_type;
2948 124304963 : tree member_scope;
2949 124304963 : tree result = NULL_TREE;
2950 124304963 : tree using_decl = NULL_TREE;
2951 :
2952 124304963 : if (error_operand_p (object) || error_operand_p (member))
2953 59 : return error_mark_node;
2954 :
2955 124304904 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2956 :
2957 : /* [expr.ref]
2958 :
2959 : The type of the first expression shall be "class object" (of a
2960 : complete type). */
2961 124304904 : object_type = TREE_TYPE (object);
2962 124304904 : if (!currently_open_class (object_type)
2963 124304904 : && !complete_type_or_maybe_complain (object_type, object, complain))
2964 0 : return error_mark_node;
2965 124304904 : if (!CLASS_TYPE_P (object_type))
2966 : {
2967 0 : if (complain & tf_error)
2968 : {
2969 0 : if (INDIRECT_TYPE_P (object_type)
2970 0 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
2971 0 : error ("request for member %qD in %qE, which is of pointer "
2972 : "type %qT (maybe you meant to use %<->%> ?)",
2973 : member, object.get_value (), object_type);
2974 : else
2975 0 : error ("request for member %qD in %qE, which is of non-class "
2976 : "type %qT", member, object.get_value (), object_type);
2977 : }
2978 0 : return error_mark_node;
2979 : }
2980 :
2981 : /* The standard does not seem to actually say that MEMBER must be a
2982 : member of OBJECT_TYPE. However, that is clearly what is
2983 : intended. */
2984 124304904 : if (DECL_P (member))
2985 : {
2986 54233465 : member_scope = DECL_CLASS_CONTEXT (member);
2987 54233465 : if (!mark_used (member, complain) && !(complain & tf_error))
2988 0 : return error_mark_node;
2989 :
2990 54233465 : if (TREE_UNAVAILABLE (member))
2991 30 : error_unavailable_use (member, NULL_TREE);
2992 54233435 : else if (TREE_DEPRECATED (member))
2993 30 : warn_deprecated_use (member, NULL_TREE);
2994 : }
2995 : else
2996 70071439 : member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2997 : /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2998 : presently be the anonymous union. Go outwards until we find a
2999 : type related to OBJECT_TYPE. */
3000 125833521 : while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
3001 127362714 : && !same_type_ignoring_top_level_qualifiers_p (member_scope,
3002 : object_type))
3003 1528617 : member_scope = TYPE_CONTEXT (member_scope);
3004 124304904 : if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
3005 : {
3006 15 : if (complain & tf_error)
3007 : {
3008 15 : if (TREE_CODE (member) == FIELD_DECL)
3009 0 : error ("invalid use of non-static data member %qE", member);
3010 : else
3011 15 : error ("%qD is not a member of %qT", member, object_type);
3012 : }
3013 15 : return error_mark_node;
3014 : }
3015 :
3016 : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
3017 : `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
3018 : in the front end; only _DECLs and _REFs are lvalues in the back end. */
3019 124304889 : if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
3020 : {
3021 44 : temp = cp_build_fold_indirect_ref (temp);
3022 44 : if (!lvalue_p (object) && lvalue_p (temp))
3023 : /* Preserve rvalueness. */
3024 6 : temp = move (temp);
3025 44 : object = temp;
3026 : }
3027 :
3028 : /* In [expr.ref], there is an explicit list of the valid choices for
3029 : MEMBER. We check for each of those cases here. */
3030 124304889 : if (VAR_P (member))
3031 : {
3032 : /* A static data member. */
3033 117532 : result = member;
3034 117532 : mark_exp_read (object);
3035 :
3036 117532 : if (tree wrap = maybe_get_tls_wrapper_call (result))
3037 : /* Replace an evaluated use of the thread_local variable with
3038 : a call to its wrapper. */
3039 102 : result = wrap;
3040 :
3041 : /* If OBJECT has side-effects, they are supposed to occur. */
3042 117532 : if (TREE_SIDE_EFFECTS (object))
3043 4827 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
3044 : }
3045 124187357 : else if (TREE_CODE (member) == FIELD_DECL)
3046 : {
3047 : /* A non-static data member. */
3048 54115706 : bool null_object_p;
3049 54115706 : cv_qualifier type_quals;
3050 54115706 : tree member_type;
3051 :
3052 54115706 : if (INDIRECT_REF_P (object))
3053 40679914 : null_object_p =
3054 40679914 : integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
3055 : else
3056 : null_object_p = false;
3057 :
3058 : /* Convert OBJECT to the type of MEMBER. */
3059 54115706 : if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
3060 : TYPE_MAIN_VARIANT (member_scope)))
3061 : {
3062 2053312 : tree binfo;
3063 2053312 : base_kind kind;
3064 :
3065 : /* We didn't complain above about a currently open class, but now we
3066 : must: we don't know how to refer to a base member before layout is
3067 : complete. But still don't complain in a template. */
3068 2053312 : if (!cp_unevaluated_operand
3069 2052787 : && !dependent_type_p (object_type)
3070 3816564 : && !complete_type_or_maybe_complain (object_type, object,
3071 : complain))
3072 9 : return error_mark_node;
3073 :
3074 2779037 : binfo = lookup_base (access_path ? access_path : object_type,
3075 : member_scope, ba_unique, &kind, complain);
3076 2053309 : if (binfo == error_mark_node)
3077 : return error_mark_node;
3078 :
3079 : /* It is invalid to try to get to a virtual base of a
3080 : NULL object. The most common cause is invalid use of
3081 : offsetof macro. */
3082 2053309 : if (null_object_p && kind == bk_via_virtual)
3083 : {
3084 6 : if (complain & tf_error)
3085 : {
3086 6 : error ("invalid access to non-static data member %qD in "
3087 : "virtual base of NULL object", member);
3088 : }
3089 6 : return error_mark_node;
3090 : }
3091 :
3092 : /* Convert to the base. */
3093 2053303 : object = build_base_path (PLUS_EXPR, object, binfo,
3094 : /*nonnull=*/1, complain);
3095 : /* If we found the base successfully then we should be able
3096 : to convert to it successfully. */
3097 2053303 : gcc_assert (object != error_mark_node || seen_error ());
3098 : }
3099 :
3100 : /* If MEMBER is from an anonymous aggregate, we have converted
3101 : OBJECT so that it refers to the class containing the
3102 : anonymous union. Generate a reference to the anonymous union
3103 : itself, and recur to find MEMBER. */
3104 108231394 : if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
3105 : /* When this code is called from build_field_call, the
3106 : object already has the type of the anonymous union.
3107 : That is because the COMPONENT_REF was already
3108 : constructed, and was then disassembled before calling
3109 : build_field_call. After the function-call code is
3110 : cleaned up, this waste can be eliminated. */
3111 55640453 : && (!same_type_ignoring_top_level_qualifiers_p
3112 1524756 : (TREE_TYPE (object), DECL_CONTEXT (member))))
3113 : {
3114 1524186 : tree anonymous_union;
3115 :
3116 1524186 : anonymous_union = lookup_anon_field (TREE_TYPE (object),
3117 1524186 : DECL_CONTEXT (member));
3118 1524186 : object = build_class_member_access_expr (object,
3119 : anonymous_union,
3120 : /*access_path=*/NULL_TREE,
3121 : preserve_reference,
3122 : complain);
3123 : }
3124 :
3125 : /* Compute the type of the field, as described in [expr.ref]. */
3126 54115697 : type_quals = TYPE_UNQUALIFIED;
3127 54115697 : member_type = TREE_TYPE (member);
3128 54115697 : if (!TYPE_REF_P (member_type))
3129 : {
3130 106166966 : type_quals = (cp_type_quals (member_type)
3131 53083483 : | cp_type_quals (object_type));
3132 :
3133 : /* A field is const (volatile) if the enclosing object, or the
3134 : field itself, is const (volatile). But, a mutable field is
3135 : not const, even within a const object. */
3136 53083483 : if (DECL_MUTABLE_P (member))
3137 322098 : type_quals &= ~TYPE_QUAL_CONST;
3138 53083483 : member_type = cp_build_qualified_type (member_type, type_quals);
3139 : }
3140 :
3141 54115697 : result = build3_loc (input_location, COMPONENT_REF, member_type,
3142 : object, member, NULL_TREE);
3143 :
3144 : /* Mark the expression const or volatile, as appropriate. Even
3145 : though we've dealt with the type above, we still have to mark the
3146 : expression itself. */
3147 54115697 : if (type_quals & TYPE_QUAL_CONST)
3148 16834822 : TREE_READONLY (result) = 1;
3149 54115697 : if (type_quals & TYPE_QUAL_VOLATILE)
3150 186428 : TREE_THIS_VOLATILE (result) = 1;
3151 : }
3152 70071651 : else if (BASELINK_P (member))
3153 : {
3154 : /* The member is a (possibly overloaded) member function. */
3155 70071424 : tree functions;
3156 70071424 : tree type;
3157 :
3158 : /* If the MEMBER is exactly one static member function, then we
3159 : know the type of the expression. Otherwise, we must wait
3160 : until overload resolution has been performed. */
3161 70071424 : functions = BASELINK_FUNCTIONS (member);
3162 70071424 : if (TREE_CODE (functions) == OVERLOAD && OVL_SINGLE_P (functions))
3163 70071424 : functions = OVL_FIRST (functions);
3164 70071424 : if (TREE_CODE (functions) == FUNCTION_DECL
3165 70071424 : && DECL_STATIC_FUNCTION_P (functions))
3166 625464 : type = TREE_TYPE (functions);
3167 : else
3168 69445960 : type = unknown_type_node;
3169 : /* Note that we do not convert OBJECT to the BASELINK_BINFO
3170 : base. That will happen when the function is called. */
3171 70071424 : result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3172 : NULL_TREE);
3173 : }
3174 227 : else if (TREE_CODE (member) == CONST_DECL)
3175 : {
3176 : /* The member is an enumerator. */
3177 215 : result = member;
3178 : /* If OBJECT has side-effects, they are supposed to occur. */
3179 215 : if (TREE_SIDE_EFFECTS (object))
3180 6 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3181 : object, result);
3182 : }
3183 12 : else if ((using_decl = strip_using_decl (member)) != member)
3184 0 : result = build_class_member_access_expr (object,
3185 : using_decl,
3186 : access_path, preserve_reference,
3187 : complain);
3188 : else
3189 : {
3190 12 : if (complain & tf_error)
3191 12 : error ("invalid use of %qD", member);
3192 12 : return error_mark_node;
3193 : }
3194 :
3195 124304868 : if (!preserve_reference)
3196 : /* [expr.ref]
3197 :
3198 : If E2 is declared to have type "reference to T", then ... the
3199 : type of E1.E2 is T. */
3200 117396417 : result = convert_from_reference (result);
3201 :
3202 : return result;
3203 : }
3204 :
3205 : /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3206 : SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3207 :
3208 : tree
3209 221544 : lookup_destructor (tree object, tree scope, tree dtor_name,
3210 : tsubst_flags_t complain)
3211 : {
3212 221544 : tree object_type = TREE_TYPE (object);
3213 221544 : tree dtor_type = TREE_OPERAND (dtor_name, 0);
3214 221544 : tree expr;
3215 :
3216 : /* We've already complained about this destructor. */
3217 221544 : if (dtor_type == error_mark_node)
3218 : return error_mark_node;
3219 :
3220 221538 : if (scope && !check_dtor_name (scope, dtor_type))
3221 : {
3222 3 : if (complain & tf_error)
3223 3 : error ("qualified type %qT does not match destructor name ~%qT",
3224 : scope, dtor_type);
3225 3 : return error_mark_node;
3226 : }
3227 221535 : if (is_auto (dtor_type))
3228 : dtor_type = object_type;
3229 221532 : else if (identifier_p (dtor_type))
3230 : {
3231 : /* In a template, names we can't find a match for are still accepted
3232 : destructor names, and we check them here. */
3233 23 : if (check_dtor_name (object_type, dtor_type))
3234 : dtor_type = object_type;
3235 : else
3236 : {
3237 3 : if (complain & tf_error)
3238 3 : error ("object type %qT does not match destructor name ~%qT",
3239 : object_type, dtor_type);
3240 3 : return error_mark_node;
3241 : }
3242 :
3243 : }
3244 221509 : else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3245 : {
3246 8 : if (complain & tf_error)
3247 8 : error ("the type being destroyed is %qT, but the destructor "
3248 8 : "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3249 8 : return error_mark_node;
3250 : }
3251 221524 : expr = lookup_member (dtor_type, complete_dtor_identifier,
3252 : /*protect=*/1, /*want_type=*/false,
3253 : complain);
3254 221524 : if (expr == error_mark_node)
3255 : return error_mark_node;
3256 221524 : if (!expr)
3257 : {
3258 6 : if (complain & tf_error)
3259 6 : cxx_incomplete_type_error (dtor_name, dtor_type);
3260 6 : return error_mark_node;
3261 : }
3262 221518 : expr = (adjust_result_of_qualified_name_lookup
3263 221518 : (expr, dtor_type, object_type));
3264 221518 : if (scope == NULL_TREE)
3265 : /* We need to call adjust_result_of_qualified_name_lookup in case the
3266 : destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3267 : that we still get virtual function binding. */
3268 221375 : BASELINK_QUALIFIED_P (expr) = false;
3269 : return expr;
3270 : }
3271 :
3272 : /* An expression of the form "A::template B" has been resolved to
3273 : DECL. Issue a diagnostic if B is not a template or template
3274 : specialization. */
3275 :
3276 : void
3277 272946 : check_template_keyword (tree decl)
3278 : {
3279 : /* The standard says:
3280 :
3281 : [temp.names]
3282 :
3283 : If a name prefixed by the keyword template is not a member
3284 : template, the program is ill-formed.
3285 :
3286 : DR 228 removed the restriction that the template be a member
3287 : template.
3288 :
3289 : DR 96, if accepted would add the further restriction that explicit
3290 : template arguments must be provided if the template keyword is
3291 : used, but, as of 2005-10-16, that DR is still in "drafting". If
3292 : this DR is accepted, then the semantic checks here can be
3293 : simplified, as the entity named must in fact be a template
3294 : specialization, rather than, as at present, a set of overloaded
3295 : functions containing at least one template function. */
3296 272946 : if (TREE_CODE (decl) != TEMPLATE_DECL
3297 272946 : && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3298 : {
3299 60790 : if (VAR_P (decl))
3300 : {
3301 9729 : if (DECL_USE_TEMPLATE (decl)
3302 9729 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3303 : ;
3304 : else
3305 0 : permerror (input_location, "%qD is not a template", decl);
3306 : }
3307 51061 : else if (!is_overloaded_fn (decl))
3308 0 : permerror (input_location, "%qD is not a template", decl);
3309 : else
3310 : {
3311 51061 : bool found = false;
3312 :
3313 102122 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3314 102124 : !found && iter; ++iter)
3315 : {
3316 51063 : tree fn = *iter;
3317 51063 : if (TREE_CODE (fn) == TEMPLATE_DECL
3318 50667 : || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3319 51077 : || (TREE_CODE (fn) == FUNCTION_DECL
3320 14 : && DECL_USE_TEMPLATE (fn)
3321 6 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3322 : found = true;
3323 : }
3324 51061 : if (!found)
3325 12 : permerror (input_location, "%qD is not a template", decl);
3326 : }
3327 : }
3328 272946 : }
3329 :
3330 : /* Record that an access failure occurred on BASETYPE_PATH attempting
3331 : to access DECL, where DIAG_DECL should be used for diagnostics. */
3332 :
3333 : void
3334 412 : access_failure_info::record_access_failure (tree basetype_path,
3335 : tree decl, tree diag_decl)
3336 : {
3337 412 : m_was_inaccessible = true;
3338 412 : m_basetype_path = basetype_path;
3339 412 : m_decl = decl;
3340 412 : m_diag_decl = diag_decl;
3341 412 : }
3342 :
3343 : /* If an access failure was recorded, then attempt to locate an
3344 : accessor function for the pertinent field.
3345 : Otherwise, return NULL_TREE. */
3346 :
3347 : tree
3348 99475274 : access_failure_info::get_any_accessor (bool const_p) const
3349 : {
3350 99475274 : if (!was_inaccessible_p ())
3351 : return NULL_TREE;
3352 :
3353 412 : tree accessor
3354 412 : = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3355 412 : if (!accessor)
3356 : return NULL_TREE;
3357 :
3358 : /* The accessor must itself be accessible for it to be a reasonable
3359 : suggestion. */
3360 161 : if (!accessible_p (m_basetype_path, accessor, true))
3361 21 : return NULL_TREE;
3362 :
3363 : return accessor;
3364 : }
3365 :
3366 : /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3367 : replacing the primary location in RICHLOC with "accessor()". */
3368 :
3369 : void
3370 140 : access_failure_info::add_fixit_hint (rich_location *richloc,
3371 : tree accessor_decl)
3372 : {
3373 140 : pretty_printer pp;
3374 140 : pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3375 140 : pp_string (&pp, "()");
3376 140 : richloc->add_fixit_replace (pp_formatted_text (&pp));
3377 140 : }
3378 :
3379 : /* If an access failure was recorded, then attempt to locate an
3380 : accessor function for the pertinent field, and if one is
3381 : available, add a note and fix-it hint suggesting using it. */
3382 :
3383 : void
3384 99475204 : access_failure_info::maybe_suggest_accessor (bool const_p) const
3385 : {
3386 99475204 : tree accessor = get_any_accessor (const_p);
3387 99475204 : if (accessor == NULL_TREE)
3388 : return;
3389 98 : rich_location richloc (line_table, input_location);
3390 98 : add_fixit_hint (&richloc, accessor);
3391 98 : inform (&richloc, "field %q#D can be accessed via %q#D",
3392 98 : m_diag_decl, accessor);
3393 98 : }
3394 :
3395 : /* Subroutine of finish_class_member_access_expr.
3396 : Issue an error about NAME not being a member of ACCESS_PATH (or
3397 : OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3398 : names. */
3399 :
3400 : static void
3401 339 : complain_about_unrecognized_member (tree access_path, tree name,
3402 : tree object_type)
3403 : {
3404 : /* Attempt to provide a hint about misspelled names. */
3405 339 : tree guessed_id = lookup_member_fuzzy (access_path, name,
3406 : /*want_type=*/false);
3407 339 : if (guessed_id == NULL_TREE)
3408 : {
3409 : /* No hint. */
3410 223 : error ("%q#T has no member named %qE",
3411 223 : TREE_CODE (access_path) == TREE_BINFO
3412 6 : ? TREE_TYPE (access_path) : object_type, name);
3413 223 : return;
3414 : }
3415 :
3416 116 : location_t bogus_component_loc = input_location;
3417 116 : gcc_rich_location rich_loc (bogus_component_loc);
3418 :
3419 : /* Check that the guessed name is accessible along access_path. */
3420 116 : access_failure_info afi;
3421 116 : lookup_member (access_path, guessed_id, /*protect=*/1,
3422 : /*want_type=*/false, /*complain=*/false,
3423 : &afi);
3424 116 : if (afi.was_inaccessible_p ())
3425 : {
3426 70 : tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3427 70 : if (accessor)
3428 : {
3429 : /* The guessed name isn't directly accessible, but can be accessed
3430 : via an accessor member function. */
3431 42 : afi.add_fixit_hint (&rich_loc, accessor);
3432 42 : error_at (&rich_loc,
3433 : "%q#T has no member named %qE;"
3434 : " did you mean %q#D? (accessible via %q#D)",
3435 42 : TREE_CODE (access_path) == TREE_BINFO
3436 0 : ? TREE_TYPE (access_path) : object_type,
3437 : name, afi.get_diag_decl (), accessor);
3438 : }
3439 : else
3440 : {
3441 : /* The guessed name isn't directly accessible, and no accessor
3442 : member function could be found. */
3443 28 : auto_diagnostic_group d;
3444 28 : error_at (&rich_loc,
3445 : "%q#T has no member named %qE;"
3446 : " did you mean %q#D? (not accessible from this context)",
3447 28 : TREE_CODE (access_path) == TREE_BINFO
3448 0 : ? TREE_TYPE (access_path) : object_type,
3449 : name, afi.get_diag_decl ());
3450 28 : complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3451 : afi.get_diag_decl (), false, ak_none);
3452 28 : }
3453 : }
3454 : else
3455 : {
3456 : /* The guessed name is directly accessible; suggest it. */
3457 46 : rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3458 : guessed_id);
3459 46 : error_at (&rich_loc,
3460 : "%q#T has no member named %qE;"
3461 : " did you mean %qE?",
3462 46 : TREE_CODE (access_path) == TREE_BINFO
3463 0 : ? TREE_TYPE (access_path) : object_type,
3464 : name, guessed_id);
3465 : }
3466 116 : }
3467 :
3468 : /* This function is called by the parser to process a class member
3469 : access expression of the form OBJECT.NAME. NAME is a node used by
3470 : the parser to represent a name; it is not yet a DECL, except when
3471 : SPLICE_P. It may, however, be a BASELINK where the BASELINK_FUNCTIONS
3472 : is a TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3473 : there is no reason to do the lookup twice, so the parser keeps the
3474 : BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3475 : be a template via the use of the "A::template B" syntax. SPLICE_P
3476 : is true if NAME was designated by a splice-expression. */
3477 :
3478 : tree
3479 198879886 : finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3480 : tsubst_flags_t complain,
3481 : bool splice_p/*=false*/)
3482 : {
3483 198879886 : tree expr;
3484 198879886 : tree object_type;
3485 198879886 : tree member;
3486 198879886 : tree access_path = NULL_TREE;
3487 198879886 : tree orig_object = object;
3488 198879886 : tree orig_name = name;
3489 :
3490 198879886 : if (object == error_mark_node || name == error_mark_node)
3491 : return error_mark_node;
3492 :
3493 : /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3494 198879339 : if (!objc_is_public (object, name))
3495 0 : return error_mark_node;
3496 :
3497 198879339 : object_type = TREE_TYPE (object);
3498 :
3499 198879339 : if (processing_template_decl)
3500 : {
3501 164107454 : if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3502 164107454 : type_dependent_object_expression_p (object)
3503 : /* If NAME is "f<args>", where either 'f' or 'args' is
3504 : dependent, then the expression is dependent. */
3505 77151822 : || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3506 695852 : && dependent_template_id_p (TREE_OPERAND (name, 0),
3507 695852 : TREE_OPERAND (name, 1)))
3508 : /* If NAME is "T::X" where "T" is dependent, then the
3509 : expression is dependent. */
3510 76776691 : || (TREE_CODE (name) == SCOPE_REF
3511 107092 : && TYPE_P (TREE_OPERAND (name, 0))
3512 107092 : && dependent_scope_p (TREE_OPERAND (name, 0)))
3513 : /* If NAME is operator T where "T" is dependent, we can't
3514 : lookup until we instantiate the T. */
3515 76669862 : || (TREE_CODE (name) == IDENTIFIER_NODE
3516 76011416 : && IDENTIFIER_CONV_OP_P (name)
3517 100 : && dependent_type_p (TREE_TYPE (name)))
3518 : /* This is OBJECT.[:R:], which is dependent. */
3519 76669768 : || dependent_splice_p (name)
3520 : /* This is OBJECT.[:T::R:], which is dependent. */
3521 240776798 : || (TREE_CODE (name) == SCOPE_REF
3522 263 : && dependent_splice_p (TREE_OPERAND (name, 1))))
3523 : {
3524 102332497 : dependent:
3525 102332497 : expr = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3526 : orig_object, orig_name, NULL_TREE);
3527 102332497 : COMPONENT_REF_SPLICE_P (expr) = splice_p;
3528 102332497 : return expr;
3529 : }
3530 : }
3531 34771885 : else if (c_dialect_objc ()
3532 111441229 : && identifier_p (name)
3533 34771885 : && (expr = objc_maybe_build_component_ref (object, name)))
3534 : return expr;
3535 :
3536 : /* [expr.ref]
3537 :
3538 : The type of the first expression shall be "class object" (of a
3539 : complete type). */
3540 111441229 : if (!currently_open_class (object_type)
3541 111441229 : && !complete_type_or_maybe_complain (object_type, object, complain))
3542 76 : return error_mark_node;
3543 111441153 : if (!CLASS_TYPE_P (object_type))
3544 : {
3545 287867 : if (complain & tf_error)
3546 : {
3547 135 : if (INDIRECT_TYPE_P (object_type)
3548 135 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
3549 23 : error ("request for member %qD in %qE, which is of pointer "
3550 : "type %qT (maybe you meant to use %<->%> ?)",
3551 : name, object.get_value (), object_type);
3552 : else
3553 112 : error ("request for member %qD in %qE, which is of non-class "
3554 : "type %qT", name, object.get_value (), object_type);
3555 : }
3556 287867 : return error_mark_node;
3557 : }
3558 :
3559 111153286 : if (BASELINK_P (name))
3560 : /* A member function that has already been looked up. */
3561 : member = name;
3562 99745333 : else if (TREE_CODE (name) == TREE_BINFO)
3563 : {
3564 : /* Splicing a base class subobject. We can only get here via
3565 : e1.[:e2:]. */
3566 94 : expr = build_base_path (PLUS_EXPR, object, name, /*nonnull=*/true,
3567 : complain);
3568 94 : if (expr == error_mark_node && (complain & tf_error))
3569 4 : error_not_base_type (BINFO_TYPE (name), object_type);
3570 : return expr;
3571 : }
3572 : else
3573 : {
3574 99745239 : bool is_template_id = false;
3575 99745239 : tree template_args = NULL_TREE;
3576 99745239 : tree scope = NULL_TREE;
3577 :
3578 99745239 : access_path = object_type;
3579 :
3580 99745239 : if (TREE_CODE (name) == SCOPE_REF)
3581 : {
3582 : /* A qualified name. The qualifying class or namespace `S'
3583 : has already been looked up; it is either a TYPE or a
3584 : NAMESPACE_DECL. */
3585 1854 : scope = TREE_OPERAND (name, 0);
3586 1854 : name = TREE_OPERAND (name, 1);
3587 :
3588 : /* If SCOPE is a namespace, then the qualified name does not
3589 : name a member of OBJECT_TYPE. */
3590 1854 : if (TREE_CODE (scope) == NAMESPACE_DECL)
3591 : {
3592 0 : if (complain & tf_error)
3593 0 : error ("%<%D::%D%> is not a member of %qT",
3594 : scope, name, object_type);
3595 0 : return error_mark_node;
3596 : }
3597 : }
3598 99743385 : else if (splice_p && valid_splice_for_member_access_p (name))
3599 : {
3600 486 : scope = context_for_name_lookup (OVL_FIRST (name));
3601 486 : if (!CLASS_TYPE_P (scope))
3602 : {
3603 8 : if (complain & tf_error)
3604 : {
3605 8 : auto_diagnostic_group d;
3606 8 : error ("%q#D is not a member of %qT", name, object_type);
3607 16 : inform (DECL_SOURCE_LOCATION (OVL_FIRST (name)), "declared here");
3608 8 : }
3609 8 : return error_mark_node;
3610 : }
3611 : }
3612 :
3613 99745231 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3614 : {
3615 486037 : is_template_id = true;
3616 486037 : template_args = TREE_OPERAND (name, 1);
3617 486037 : name = TREE_OPERAND (name, 0);
3618 :
3619 486037 : if (splice_p
3620 486523 : && (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (name))
3621 162 : || variable_template_p (name)))
3622 162 : scope = DECL_CONTEXT (OVL_FIRST (name));
3623 942498 : else if (!identifier_p (name))
3624 456693 : name = OVL_NAME (name);
3625 : }
3626 :
3627 99745231 : if (scope)
3628 : {
3629 2494 : if (TREE_CODE (scope) == ENUMERAL_TYPE)
3630 : {
3631 12 : gcc_assert (!is_template_id);
3632 : /* Looking up a member enumerator (c++/56793). */
3633 24 : if (!TYPE_CLASS_SCOPE_P (scope)
3634 21 : || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3635 : {
3636 3 : if (complain & tf_error)
3637 3 : error ("%<%D::%D%> is not a member of %qT",
3638 : scope, name, object_type);
3639 3 : return error_mark_node;
3640 : }
3641 9 : tree val;
3642 9 : if (splice_p && TREE_CODE (name) == CONST_DECL)
3643 : val = name;
3644 : else
3645 9 : val = lookup_enumerator (scope, name);
3646 9 : if (!val)
3647 : {
3648 6 : if (complain & tf_error)
3649 6 : error ("%qD is not a member of %qD",
3650 : name, scope);
3651 6 : return error_mark_node;
3652 : }
3653 :
3654 3 : if (TREE_SIDE_EFFECTS (object))
3655 0 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3656 : return val;
3657 : }
3658 :
3659 2482 : gcc_assert (CLASS_TYPE_P (scope));
3660 2482 : gcc_assert (identifier_p (name)
3661 : || TREE_CODE (name) == BIT_NOT_EXPR
3662 : || (splice_p && valid_splice_for_member_access_p (name)));
3663 :
3664 2482 : if (constructor_name_p (name, scope))
3665 : {
3666 3 : if (complain & tf_error)
3667 3 : error ("cannot call constructor %<%T::%D%> directly",
3668 : scope, name);
3669 3 : return error_mark_node;
3670 : }
3671 :
3672 : /* NAME may refer to a static data member, in which case there is
3673 : one copy of the data member that is shared by all the objects of
3674 : the class. So NAME can be unambiguously referred to even if
3675 : there are multiple indirect base classes containing NAME. */
3676 7437 : const base_access ba = [scope, name, splice_p] ()
3677 : {
3678 : /* [class.access.base]/5: A member m is accessible at the
3679 : point R when designated in class N if
3680 : -- m is designated by a splice-expression */
3681 2479 : if (splice_p)
3682 : return ba_unique;
3683 1839 : if (identifier_p (name))
3684 : {
3685 1702 : tree m = lookup_member (scope, name, /*protect=*/0,
3686 : /*want_type=*/false, tf_none);
3687 1702 : if (!m || shared_member_p (m))
3688 38 : return ba_any;
3689 : }
3690 : return ba_check;
3691 2479 : } ();
3692 :
3693 : /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3694 2479 : access_path = lookup_base (object_type, scope, ba, NULL, complain);
3695 2479 : if (access_path == error_mark_node)
3696 : return error_mark_node;
3697 2441 : if (!access_path)
3698 : {
3699 40 : if (any_dependent_bases_p (object_type))
3700 6 : goto dependent;
3701 34 : if (complain & tf_error)
3702 34 : error ("%qT is not a base of %qT", scope, object_type);
3703 34 : return error_mark_node;
3704 : }
3705 : }
3706 :
3707 99745138 : if (TREE_CODE (name) == BIT_NOT_EXPR)
3708 : {
3709 269348 : if (dependent_type_p (object_type))
3710 : /* The destructor isn't declared yet. */
3711 47819 : goto dependent;
3712 221529 : member = lookup_destructor (object, scope, name, complain);
3713 : }
3714 99475790 : else if (splice_p && valid_splice_for_member_access_p (name))
3715 : {
3716 586 : member = name;
3717 586 : name = OVL_FIRST (name);
3718 586 : if (any_dependent_type_attributes_p (DECL_ATTRIBUTES (name)))
3719 : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3720 : wrong, so don't use it. */
3721 0 : goto dependent;
3722 586 : if (is_overloaded_fn (name))
3723 0 : member
3724 0 : = build_baselink (access_path, TYPE_BINFO (object_type), member,
3725 0 : IDENTIFIER_CONV_OP_P (DECL_NAME (name))
3726 0 : ? TREE_TYPE (DECL_NAME (name)) : NULL_TREE);
3727 : }
3728 : else
3729 : {
3730 : /* Look up the member. */
3731 99475204 : {
3732 99475204 : auto_diagnostic_group d;
3733 99475204 : access_failure_info afi;
3734 99475204 : if (processing_template_decl)
3735 : /* Even though this class member access expression is at this
3736 : point not dependent, the member itself may be dependent, and
3737 : we must not potentially push a access check for a dependent
3738 : member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3739 : ahead of time here; we're going to redo this member lookup at
3740 : instantiation time anyway. */
3741 76332124 : push_deferring_access_checks (dk_no_check);
3742 99475204 : member = lookup_member (access_path, name, /*protect=*/1,
3743 : /*want_type=*/false, complain,
3744 : &afi);
3745 99475204 : if (processing_template_decl)
3746 76332124 : pop_deferring_access_checks ();
3747 99475204 : afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3748 99475204 : }
3749 99475204 : if (member == NULL_TREE)
3750 : {
3751 9531823 : if (dependentish_scope_p (object_type))
3752 : /* Try again at instantiation time. */
3753 9507725 : goto dependent;
3754 24098 : if (complain & tf_error)
3755 339 : complain_about_unrecognized_member (access_path, name,
3756 : object_type);
3757 24098 : return error_mark_node;
3758 : }
3759 89943381 : if (member == error_mark_node)
3760 : return error_mark_node;
3761 89943335 : if (DECL_P (member)
3762 89943335 : && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3763 : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3764 : wrong, so don't use it. */
3765 6 : goto dependent;
3766 89943329 : if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3767 5338831 : goto dependent;
3768 : }
3769 :
3770 84826613 : if (is_template_id)
3771 : {
3772 485747 : tree templ = member;
3773 :
3774 485747 : if (BASELINK_P (templ))
3775 485493 : member = lookup_template_function (templ, template_args);
3776 254 : else if (variable_template_p (templ))
3777 254 : member = (lookup_and_finish_template_variable
3778 254 : (templ, template_args, complain));
3779 : else
3780 : {
3781 0 : if (complain & tf_error)
3782 0 : error ("%qD is not a member template function", name);
3783 0 : return error_mark_node;
3784 : }
3785 : }
3786 : }
3787 :
3788 96234566 : if (TREE_UNAVAILABLE (member))
3789 30 : error_unavailable_use (member, NULL_TREE);
3790 96234536 : else if (TREE_DEPRECATED (member))
3791 30 : warn_deprecated_use (member, NULL_TREE);
3792 :
3793 96234566 : if (template_p)
3794 9713 : check_template_keyword (member);
3795 :
3796 96234566 : expr = build_class_member_access_expr (object, member, access_path,
3797 : /*preserve_reference=*/false,
3798 : complain);
3799 96234566 : if (processing_template_decl && expr != error_mark_node)
3800 : {
3801 61774860 : if (BASELINK_P (member))
3802 : {
3803 45812405 : if (TREE_CODE (orig_name) == SCOPE_REF)
3804 215 : BASELINK_QUALIFIED_P (member) = 1;
3805 : orig_name = member;
3806 : }
3807 61774860 : expr = build_min_non_dep (COMPONENT_REF, expr,
3808 : orig_object, orig_name,
3809 : NULL_TREE);
3810 123549720 : COMPONENT_REF_SPLICE_P (STRIP_REFERENCE_REF (expr)) = splice_p;
3811 : }
3812 :
3813 : return expr;
3814 : }
3815 :
3816 : /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3817 : type. */
3818 :
3819 : tree
3820 173892 : build_simple_component_ref (tree object, tree member)
3821 : {
3822 173892 : tree type = cp_build_qualified_type (TREE_TYPE (member),
3823 173892 : cp_type_quals (TREE_TYPE (object)));
3824 173892 : return build3_loc (input_location,
3825 : COMPONENT_REF, type,
3826 173892 : object, member, NULL_TREE);
3827 : }
3828 :
3829 : /* Return an expression for the MEMBER_NAME field in the internal
3830 : representation of PTRMEM, a pointer-to-member function. (Each
3831 : pointer-to-member function type gets its own RECORD_TYPE so it is
3832 : more convenient to access the fields by name than by FIELD_DECL.)
3833 : This routine converts the NAME to a FIELD_DECL and then creates the
3834 : node for the complete expression. */
3835 :
3836 : tree
3837 173808 : build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3838 : {
3839 173808 : tree ptrmem_type;
3840 173808 : tree member;
3841 :
3842 173808 : if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3843 : {
3844 234 : for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3845 90 : if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3846 72 : return e.value;
3847 0 : gcc_unreachable ();
3848 : }
3849 :
3850 : /* This code is a stripped down version of
3851 : build_class_member_access_expr. It does not work to use that
3852 : routine directly because it expects the object to be of class
3853 : type. */
3854 173736 : ptrmem_type = TREE_TYPE (ptrmem);
3855 173736 : gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3856 260487 : for (member = TYPE_FIELDS (ptrmem_type); member;
3857 86751 : member = DECL_CHAIN (member))
3858 260487 : if (DECL_NAME (member) == member_name)
3859 : break;
3860 173736 : return build_simple_component_ref (ptrmem, member);
3861 : }
3862 :
3863 : /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3864 : and for any other relevant operator. */
3865 :
3866 : static tree
3867 143770582 : op_unqualified_lookup (tree_code code, bool is_assign)
3868 : {
3869 143770582 : tree lookups = NULL_TREE;
3870 :
3871 143770582 : if (cxx_dialect >= cxx20 && !is_assign)
3872 : {
3873 135905781 : if (code == NE_EXPR)
3874 : {
3875 : /* != can get rewritten in terms of ==. */
3876 8763888 : tree fnname = ovl_op_identifier (false, EQ_EXPR);
3877 8763888 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3878 8656196 : lookups = tree_cons (fnname, fns, lookups);
3879 : }
3880 127141893 : else if (code == GT_EXPR || code == LE_EXPR
3881 127141893 : || code == LT_EXPR || code == GE_EXPR)
3882 : {
3883 : /* These can get rewritten in terms of <=>. */
3884 12011129 : tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3885 12011129 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3886 11374913 : lookups = tree_cons (fnname, fns, lookups);
3887 : }
3888 : }
3889 :
3890 143770582 : tree fnname = ovl_op_identifier (is_assign, code);
3891 143770582 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3892 78004573 : lookups = tree_cons (fnname, fns, lookups);
3893 :
3894 143770582 : if (lookups)
3895 : return lookups;
3896 : else
3897 65534698 : return build_tree_list (NULL_TREE, NULL_TREE);
3898 : }
3899 :
3900 : /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3901 : the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3902 : name lookup for the given operator. */
3903 :
3904 : tree
3905 150162538 : build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3906 : {
3907 150162538 : if (lookups)
3908 : /* We're partially instantiating a dependent operator expression, and
3909 : LOOKUPS is the result of phase 1 name lookup that we performed
3910 : earlier at template definition time, so just reuse the corresponding
3911 : DEPENDENT_OPERATOR_TYPE. */
3912 6391956 : return TREE_TYPE (lookups);
3913 :
3914 : /* Otherwise we're processing a dependent operator expression at template
3915 : definition time, so perform phase 1 name lookup now. */
3916 143770582 : lookups = op_unqualified_lookup (code, is_assign);
3917 :
3918 143770582 : tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3919 143770582 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3920 143770582 : TREE_TYPE (lookups) = type;
3921 143770582 : return type;
3922 : }
3923 :
3924 : /* Given an expression PTR for a pointer, return an expression
3925 : for the value pointed to.
3926 : ERRORSTRING is the name of the operator to appear in error messages.
3927 :
3928 : This function may need to overload OPERATOR_FNNAME.
3929 : Must also handle REFERENCE_TYPEs for C++. */
3930 :
3931 : tree
3932 52001096 : build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3933 : tree lookups, tsubst_flags_t complain)
3934 : {
3935 52001096 : tree orig_expr = expr;
3936 52001096 : tree rval;
3937 52001096 : tree overload = NULL_TREE;
3938 :
3939 52001096 : if (processing_template_decl)
3940 : {
3941 : /* Retain the type if we know the operand is a pointer. */
3942 30142563 : if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3943 : {
3944 14849149 : if (expr == current_class_ptr
3945 14849149 : || (TREE_CODE (expr) == NOP_EXPR
3946 9862062 : && TREE_OPERAND (expr, 0) == current_class_ptr
3947 9748233 : && (same_type_ignoring_top_level_qualifiers_p
3948 9748233 : (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3949 9748232 : return current_class_ref;
3950 5100917 : return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3951 : }
3952 15293414 : if (type_dependent_expression_p (expr))
3953 : {
3954 15151473 : expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3955 15151473 : TREE_TYPE (expr)
3956 15151473 : = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3957 15151473 : return expr;
3958 : }
3959 : }
3960 :
3961 22000474 : rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3962 : NULL_TREE, NULL_TREE, lookups,
3963 : &overload, complain);
3964 22000474 : if (!rval)
3965 0 : rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3966 :
3967 22000474 : if (processing_template_decl && rval != error_mark_node)
3968 : {
3969 140108 : if (overload != NULL_TREE)
3970 140077 : return (build_min_non_dep_op_overload
3971 140077 : (INDIRECT_REF, rval, overload, orig_expr));
3972 :
3973 31 : return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3974 : }
3975 : else
3976 : return rval;
3977 : }
3978 :
3979 : /* Like c-family strict_aliasing_warning, but don't warn for dependent
3980 : types or expressions. */
3981 :
3982 : static bool
3983 658177 : cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3984 : {
3985 658177 : if (processing_template_decl)
3986 : {
3987 45714 : tree e = expr;
3988 45714 : STRIP_NOPS (e);
3989 45714 : if (dependent_type_p (type) || type_dependent_expression_p (e))
3990 : return false;
3991 : }
3992 650741 : return strict_aliasing_warning (loc, type, expr);
3993 : }
3994 :
3995 : /* The implementation of the above, and of indirection implied by other
3996 : constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3997 :
3998 : static tree
3999 368761847 : cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
4000 : tsubst_flags_t complain, bool do_fold)
4001 : {
4002 368761847 : tree pointer, type;
4003 :
4004 : /* RO_NULL should only be used with the folding entry points below, not
4005 : cp_build_indirect_ref. */
4006 368761847 : gcc_checking_assert (errorstring != RO_NULL || do_fold);
4007 :
4008 368761847 : if (ptr == current_class_ptr
4009 368761847 : || (TREE_CODE (ptr) == NOP_EXPR
4010 39627948 : && TREE_OPERAND (ptr, 0) == current_class_ptr
4011 21051943 : && (same_type_ignoring_top_level_qualifiers_p
4012 21051943 : (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
4013 22587941 : return current_class_ref;
4014 :
4015 346173906 : pointer = (TYPE_REF_P (TREE_TYPE (ptr))
4016 346173906 : ? ptr : decay_conversion (ptr, complain));
4017 346173906 : if (pointer == error_mark_node)
4018 : return error_mark_node;
4019 :
4020 346168045 : type = TREE_TYPE (pointer);
4021 :
4022 346168045 : if (INDIRECT_TYPE_P (type))
4023 : {
4024 : /* [expr.unary.op]
4025 :
4026 : If the type of the expression is "pointer to T," the type
4027 : of the result is "T." */
4028 346167445 : tree t = TREE_TYPE (type);
4029 :
4030 326264323 : if ((CONVERT_EXPR_P (ptr)
4031 237106277 : || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
4032 437757165 : && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
4033 : {
4034 : /* If a warning is issued, mark it to avoid duplicates from
4035 : the backend. This only needs to be done at
4036 : warn_strict_aliasing > 2. */
4037 87417304 : if (warn_strict_aliasing > 2
4038 87973168 : && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
4039 555864 : type, TREE_OPERAND (ptr, 0)))
4040 9 : suppress_warning (ptr, OPT_Wstrict_aliasing);
4041 : }
4042 :
4043 346167445 : if (VOID_TYPE_P (t))
4044 : {
4045 : /* A pointer to incomplete type (other than cv void) can be
4046 : dereferenced [expr.unary.op]/1 */
4047 48 : if (complain & tf_error)
4048 19 : error_at (loc, "%qT is not a pointer-to-object type", type);
4049 48 : return error_mark_node;
4050 : }
4051 335082361 : else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
4052 353791836 : && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
4053 : /* The POINTER was something like `&x'. We simplify `*&x' to
4054 : `x'. This can change the value category: '*&TARGET_EXPR'
4055 : is an lvalue and folding it into 'TARGET_EXPR' turns it into
4056 : a prvalue of class type. */
4057 7624361 : return TREE_OPERAND (pointer, 0);
4058 : else
4059 : {
4060 338543036 : tree ref = build1 (INDIRECT_REF, t, pointer);
4061 :
4062 : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
4063 : so that we get the proper error message if the result is used
4064 : to assign to. Also, &* is supposed to be a no-op. */
4065 338543036 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
4066 338543036 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
4067 338543036 : TREE_SIDE_EFFECTS (ref)
4068 338543036 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
4069 338543036 : return ref;
4070 : }
4071 : }
4072 600 : else if (!(complain & tf_error))
4073 : /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
4074 : ;
4075 : /* `pointer' won't be an error_mark_node if we were given a
4076 : pointer to member, so it's cool to check for this here. */
4077 41 : else if (TYPE_PTRMEM_P (type))
4078 15 : switch (errorstring)
4079 : {
4080 0 : case RO_ARRAY_INDEXING:
4081 0 : error_at (loc,
4082 : "invalid use of array indexing on pointer to member");
4083 0 : break;
4084 12 : case RO_UNARY_STAR:
4085 12 : error_at (loc, "invalid use of unary %<*%> on pointer to member");
4086 12 : break;
4087 0 : case RO_IMPLICIT_CONVERSION:
4088 0 : error_at (loc, "invalid use of implicit conversion on pointer "
4089 : "to member");
4090 0 : break;
4091 3 : case RO_ARROW_STAR:
4092 3 : error_at (loc, "left hand operand of %<->*%> must be a pointer to "
4093 : "class, but is a pointer to member of type %qT", type);
4094 3 : break;
4095 0 : default:
4096 0 : gcc_unreachable ();
4097 : }
4098 26 : else if (pointer != error_mark_node)
4099 26 : invalid_indirection_error (loc, type, errorstring);
4100 :
4101 600 : return error_mark_node;
4102 : }
4103 :
4104 : /* Entry point used by c-common, which expects folding. */
4105 :
4106 : tree
4107 69931 : build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
4108 : {
4109 69931 : return cp_build_indirect_ref_1 (loc, ptr, errorstring,
4110 69931 : tf_warning_or_error, true);
4111 : }
4112 :
4113 : /* Entry point used by internal indirection needs that don't correspond to any
4114 : syntactic construct. */
4115 :
4116 : tree
4117 338896788 : cp_build_fold_indirect_ref (tree pointer)
4118 : {
4119 338896788 : return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
4120 338896788 : tf_warning_or_error, true);
4121 : }
4122 :
4123 : /* Entry point used by indirection needs that correspond to some syntactic
4124 : construct. */
4125 :
4126 : tree
4127 29795128 : cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
4128 : tsubst_flags_t complain)
4129 : {
4130 29795128 : return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
4131 : }
4132 :
4133 : /* This handles expressions of the form "a[i]", which denotes
4134 : an array reference.
4135 :
4136 : This is logically equivalent in C to *(a+i), but we may do it differently.
4137 : If A is a variable or a member, we generate a primitive ARRAY_REF.
4138 : This avoids forcing the array out of registers, and can work on
4139 : arrays that are not lvalues (for example, members of structures returned
4140 : by functions).
4141 :
4142 : If INDEX is of some user-defined type, it must be converted to
4143 : integer type. Otherwise, to make a compatible PLUS_EXPR, it
4144 : will inherit the type of the array, which will be some pointer type.
4145 :
4146 : LOC is the location to use in building the array reference. */
4147 :
4148 : tree
4149 6201279 : cp_build_array_ref (location_t loc, tree array, tree idx,
4150 : tsubst_flags_t complain)
4151 : {
4152 6201279 : tree ret;
4153 :
4154 6201279 : if (idx == 0)
4155 : {
4156 0 : if (complain & tf_error)
4157 0 : error_at (loc, "subscript missing in array reference");
4158 0 : return error_mark_node;
4159 : }
4160 :
4161 6201279 : if (TREE_TYPE (array) == error_mark_node
4162 6201279 : || TREE_TYPE (idx) == error_mark_node)
4163 : return error_mark_node;
4164 :
4165 : /* 0[array] */
4166 6201279 : if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE
4167 6201279 : && TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
4168 : {
4169 10 : std::swap (array, idx);
4170 :
4171 10 : tree first = NULL_TREE;
4172 10 : if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array))
4173 9 : idx = first = save_expr (idx);
4174 10 : ret = cp_build_array_ref (loc, array, idx, complain);
4175 :
4176 10 : if (first)
4177 9 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4178 : return ret;
4179 : }
4180 :
4181 : /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
4182 : inside it. */
4183 6201269 : switch (TREE_CODE (array))
4184 : {
4185 28 : case COMPOUND_EXPR:
4186 28 : {
4187 28 : tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
4188 : complain);
4189 28 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
4190 28 : TREE_OPERAND (array, 0), value);
4191 28 : SET_EXPR_LOCATION (ret, loc);
4192 28 : return ret;
4193 : }
4194 :
4195 73 : case COND_EXPR:
4196 73 : tree op0, op1, op2;
4197 73 : op0 = TREE_OPERAND (array, 0);
4198 73 : op1 = TREE_OPERAND (array, 1);
4199 73 : op2 = TREE_OPERAND (array, 2);
4200 73 : if (TREE_SIDE_EFFECTS (idx) || !tree_invariant_p (idx))
4201 : {
4202 : /* If idx could possibly have some SAVE_EXPRs, turning
4203 : (op0 ? op1 : op2)[idx] into
4204 : op0 ? op1[idx] : op2[idx] can lead into temporaries
4205 : initialized in one conditional path and uninitialized
4206 : uses of them in the other path.
4207 : And if idx is a really large expression, evaluating it
4208 : twice is also not optimal.
4209 : On the other side, op0 must be sequenced before evaluation
4210 : of op1 and op2 and for C++17 op0, op1 and op2 must be
4211 : sequenced before idx.
4212 : If idx is INTEGER_CST, we can just do the optimization
4213 : without any SAVE_EXPRs, if op1 and op2 are both ARRAY_TYPE
4214 : VAR_DECLs or COMPONENT_REFs thereof (so their address
4215 : is constant or relative to frame), optimize into
4216 : (SAVE_EXPR <op0>, SAVE_EXPR <idx>, SAVE_EXPR <op0>)
4217 : ? op1[SAVE_EXPR <idx>] : op2[SAVE_EXPR <idx>]
4218 : Otherwise avoid this optimization. */
4219 58 : if (flag_strong_eval_order == 2)
4220 : {
4221 39 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4222 : {
4223 30 : if (!address_invariant_p (op1) || !address_invariant_p (op2))
4224 : {
4225 : /* Force default conversion on array if
4226 : we can't optimize this and array is ARRAY_TYPE
4227 : COND_EXPR, we can't leave COND_EXPRs with
4228 : ARRAY_TYPE in the IL. */
4229 8 : array = cp_default_conversion (array, complain);
4230 8 : if (error_operand_p (array))
4231 0 : return error_mark_node;
4232 : break;
4233 : }
4234 : }
4235 16 : else if (!POINTER_TYPE_P (TREE_TYPE (array))
4236 2 : || !tree_invariant_p (op1)
4237 11 : || !tree_invariant_p (op2))
4238 : break;
4239 : }
4240 43 : if (TREE_SIDE_EFFECTS (idx))
4241 : {
4242 18 : idx = save_expr (idx);
4243 18 : op0 = save_expr (op0);
4244 18 : warning_sentinel w (warn_unused_value);
4245 18 : tree tem = build_compound_expr (loc, op0, idx);
4246 18 : op0 = build_compound_expr (loc, tem, op0);
4247 18 : }
4248 : }
4249 58 : op1 = cp_build_array_ref (loc, op1, idx, complain);
4250 58 : op2 = cp_build_array_ref (loc, op2, idx, complain);
4251 58 : ret = build_conditional_expr (loc, op0, op1, op2, complain);
4252 58 : protected_set_expr_location (ret, loc);
4253 58 : return ret;
4254 :
4255 : default:
4256 : break;
4257 : }
4258 :
4259 6201183 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
4260 :
4261 6201183 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4262 : {
4263 1921273 : tree rval, type;
4264 :
4265 1921273 : warn_array_subscript_with_type_char (loc, idx);
4266 :
4267 1921273 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
4268 : {
4269 6 : if (complain & tf_error)
4270 6 : error_at (loc, "array subscript is not an integer");
4271 6 : return error_mark_node;
4272 : }
4273 :
4274 : /* Apply integral promotions *after* noticing character types.
4275 : (It is unclear why we do these promotions -- the standard
4276 : does not say that we should. In fact, the natural thing would
4277 : seem to be to convert IDX to ptrdiff_t; we're performing
4278 : pointer arithmetic.) */
4279 1921267 : idx = cp_perform_integral_promotions (idx, complain);
4280 :
4281 1921267 : idx = maybe_fold_non_dependent_expr (idx, complain);
4282 :
4283 : /* An array that is indexed by a non-constant
4284 : cannot be stored in a register; we must be able to do
4285 : address arithmetic on its address.
4286 : Likewise an array of elements of variable size. */
4287 1921267 : if (TREE_CODE (idx) != INTEGER_CST
4288 1921267 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4289 932402 : && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4290 : != INTEGER_CST)))
4291 : {
4292 990125 : if (!cxx_mark_addressable (array, true))
4293 0 : return error_mark_node;
4294 : }
4295 :
4296 : /* An array that is indexed by a constant value which is not within
4297 : the array bounds cannot be stored in a register either; because we
4298 : would get a crash in store_bit_field/extract_bit_field when trying
4299 : to access a non-existent part of the register. */
4300 1921267 : if (TREE_CODE (idx) == INTEGER_CST
4301 932405 : && TYPE_DOMAIN (TREE_TYPE (array))
4302 2852274 : && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4303 : {
4304 3124 : if (!cxx_mark_addressable (array))
4305 0 : return error_mark_node;
4306 : }
4307 :
4308 : /* Note in C++ it is valid to subscript a `register' array, since
4309 : it is valid to take the address of something with that
4310 : storage specification. */
4311 1921267 : if (extra_warnings)
4312 : {
4313 38852 : tree foo = array;
4314 59542 : while (TREE_CODE (foo) == COMPONENT_REF)
4315 20690 : foo = TREE_OPERAND (foo, 0);
4316 8 : if (VAR_P (foo) && DECL_REGISTER (foo)
4317 38852 : && (complain & tf_warning))
4318 0 : warning_at (loc, OPT_Wextra,
4319 : "subscripting array declared %<register%>");
4320 : }
4321 :
4322 1921267 : type = TREE_TYPE (TREE_TYPE (array));
4323 1921267 : rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4324 : /* Array ref is const/volatile if the array elements are
4325 : or if the array is.. */
4326 5763801 : TREE_READONLY (rval)
4327 1921267 : |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4328 5763801 : TREE_SIDE_EFFECTS (rval)
4329 1921267 : |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4330 3842534 : TREE_THIS_VOLATILE (rval)
4331 1921267 : |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4332 1921267 : ret = require_complete_type (rval, complain);
4333 1921267 : protected_set_expr_location (ret, loc);
4334 1921267 : if (non_lvalue)
4335 38431 : ret = non_lvalue_loc (loc, ret);
4336 : return ret;
4337 : }
4338 :
4339 4279910 : {
4340 4279910 : tree ar = cp_default_conversion (array, complain);
4341 4279910 : tree ind = cp_default_conversion (idx, complain);
4342 4279910 : tree first = NULL_TREE;
4343 :
4344 2775588 : if (!processing_template_decl && flag_strong_eval_order == 2
4345 7015007 : && TREE_SIDE_EFFECTS (ind))
4346 93973 : ar = first = save_expr (ar);
4347 :
4348 : /* Put the integer in IND to simplify error checking. */
4349 4279910 : if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4350 57 : std::swap (ar, ind);
4351 :
4352 4279910 : if (ar == error_mark_node || ind == error_mark_node)
4353 : return error_mark_node;
4354 :
4355 4279910 : if (!TYPE_PTR_P (TREE_TYPE (ar)))
4356 : {
4357 74 : if (complain & tf_error)
4358 3 : error_at (loc, "subscripted value is neither array nor pointer");
4359 74 : return error_mark_node;
4360 : }
4361 4279836 : if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4362 : {
4363 0 : if (complain & tf_error)
4364 0 : error_at (loc, "array subscript is not an integer");
4365 0 : return error_mark_node;
4366 : }
4367 :
4368 4279836 : warn_array_subscript_with_type_char (loc, idx);
4369 :
4370 4279836 : ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4371 4279836 : if (first)
4372 93973 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4373 4279836 : ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4374 4279836 : protected_set_expr_location (ret, loc);
4375 4279836 : if (non_lvalue)
4376 0 : ret = non_lvalue_loc (loc, ret);
4377 : return ret;
4378 : }
4379 : }
4380 :
4381 : /* Entry point for Obj-C++. */
4382 :
4383 : tree
4384 3734995 : build_array_ref (location_t loc, tree array, tree idx)
4385 : {
4386 3734995 : return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4387 : }
4388 :
4389 : /* Resolve a pointer to member function. INSTANCE is the object
4390 : instance to use, if the member points to a virtual member.
4391 :
4392 : This used to avoid checking for virtual functions if basetype
4393 : has no virtual functions, according to an earlier ANSI draft.
4394 : With the final ISO C++ rules, such an optimization is
4395 : incorrect: A pointer to a derived member can be static_cast
4396 : to pointer-to-base-member, as long as the dynamic object
4397 : later has the right member. So now we only do this optimization
4398 : when we know the dynamic type of the object. */
4399 :
4400 : tree
4401 86574 : get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4402 : tsubst_flags_t complain)
4403 : {
4404 86574 : if (TREE_CODE (function) == OFFSET_REF)
4405 0 : function = TREE_OPERAND (function, 1);
4406 :
4407 86574 : if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4408 : {
4409 86574 : tree idx, delta, e1, e2, e3, vtbl;
4410 86574 : bool nonvirtual;
4411 86574 : tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4412 86574 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4413 :
4414 86574 : tree instance_ptr = *instance_ptrptr;
4415 86574 : tree instance_save_expr = 0;
4416 86574 : if (instance_ptr == error_mark_node)
4417 : {
4418 0 : if (TREE_CODE (function) == PTRMEM_CST)
4419 : {
4420 : /* Extracting the function address from a pmf is only
4421 : allowed with -Wno-pmf-conversions. It only works for
4422 : pmf constants. */
4423 0 : e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4424 0 : e1 = convert (fntype, e1);
4425 0 : return e1;
4426 : }
4427 : else
4428 : {
4429 0 : if (complain & tf_error)
4430 0 : error ("object missing in use of %qE", function);
4431 0 : return error_mark_node;
4432 : }
4433 : }
4434 :
4435 : /* True if we know that the dynamic type of the object doesn't have
4436 : virtual functions, so we can assume the PFN field is a pointer. */
4437 86574 : nonvirtual = (COMPLETE_TYPE_P (basetype)
4438 86538 : && !TYPE_POLYMORPHIC_P (basetype)
4439 116414 : && resolves_to_fixed_type_p (instance_ptr, 0));
4440 :
4441 : /* If we don't really have an object (i.e. in an ill-formed
4442 : conversion from PMF to pointer), we can't resolve virtual
4443 : functions anyway. */
4444 86314 : if (!nonvirtual && is_dummy_object (instance_ptr))
4445 : nonvirtual = true;
4446 :
4447 : /* Use force_target_expr even when instance_ptr doesn't have
4448 : side-effects, unless it is a simple decl or constant, so
4449 : that we don't ubsan instrument the expression multiple times.
4450 : Don't use save_expr, as save_expr can avoid building a SAVE_EXPR
4451 : and building a SAVE_EXPR manually can be optimized away during
4452 : cp_fold. See PR116449 and PR117259. */
4453 86574 : if (TREE_SIDE_EFFECTS (instance_ptr)
4454 86574 : || (!nonvirtual
4455 48288 : && !DECL_P (instance_ptr)
4456 48288 : && !TREE_CONSTANT (instance_ptr)))
4457 85995 : instance_ptr = instance_save_expr
4458 85995 : = get_internal_target_expr (instance_ptr);
4459 :
4460 : /* See above comment. */
4461 86574 : if (TREE_SIDE_EFFECTS (function)
4462 86574 : || (!nonvirtual
4463 67122 : && !DECL_P (function)
4464 67110 : && !TREE_CONSTANT (function)))
4465 86112 : function = get_internal_target_expr (function);
4466 :
4467 : /* Start by extracting all the information from the PMF itself. */
4468 86574 : e3 = pfn_from_ptrmemfunc (function);
4469 86574 : delta = delta_from_ptrmemfunc (function);
4470 86574 : idx = build1 (NOP_EXPR, vtable_index_type, e3);
4471 86574 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4472 : {
4473 86574 : sanitize_code_type flag_sanitize_save;
4474 86574 : case ptrmemfunc_vbit_in_pfn:
4475 86574 : e1 = cp_build_binary_op (input_location,
4476 : BIT_AND_EXPR, idx, integer_one_node,
4477 : complain);
4478 86574 : idx = cp_build_binary_op (input_location,
4479 : MINUS_EXPR, idx, integer_one_node,
4480 : complain);
4481 86574 : if (idx == error_mark_node)
4482 : return error_mark_node;
4483 86574 : break;
4484 :
4485 : case ptrmemfunc_vbit_in_delta:
4486 : e1 = cp_build_binary_op (input_location,
4487 : BIT_AND_EXPR, delta, integer_one_node,
4488 : complain);
4489 : /* Don't instrument the RSHIFT_EXPR we're about to create because
4490 : we're going to use DELTA number of times, and that wouldn't play
4491 : well with SAVE_EXPRs therein. */
4492 : flag_sanitize_save = flag_sanitize;
4493 : flag_sanitize = 0;
4494 : delta = cp_build_binary_op (input_location,
4495 : RSHIFT_EXPR, delta, integer_one_node,
4496 : complain);
4497 : flag_sanitize = flag_sanitize_save;
4498 : if (delta == error_mark_node)
4499 : return error_mark_node;
4500 : break;
4501 :
4502 : default:
4503 : gcc_unreachable ();
4504 : }
4505 :
4506 86574 : if (e1 == error_mark_node)
4507 : return error_mark_node;
4508 :
4509 : /* Convert down to the right base before using the instance. A
4510 : special case is that in a pointer to member of class C, C may
4511 : be incomplete. In that case, the function will of course be
4512 : a member of C, and no conversion is required. In fact,
4513 : lookup_base will fail in that case, because incomplete
4514 : classes do not have BINFOs. */
4515 86574 : if (!same_type_ignoring_top_level_qualifiers_p
4516 86574 : (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4517 : {
4518 81 : basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4519 : basetype, ba_check, NULL, complain);
4520 81 : instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4521 : 1, complain);
4522 81 : if (instance_ptr == error_mark_node)
4523 : return error_mark_node;
4524 : }
4525 : /* ...and then the delta in the PMF. */
4526 86574 : instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4527 :
4528 : /* Hand back the adjusted 'this' argument to our caller. */
4529 86574 : *instance_ptrptr = instance_ptr;
4530 :
4531 86574 : if (nonvirtual)
4532 : /* Now just return the pointer. */
4533 : return e3;
4534 :
4535 : /* Next extract the vtable pointer from the object. */
4536 86296 : vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4537 : instance_ptr);
4538 86296 : vtbl = cp_build_fold_indirect_ref (vtbl);
4539 86296 : if (vtbl == error_mark_node)
4540 : return error_mark_node;
4541 :
4542 : /* Finally, extract the function pointer from the vtable. */
4543 86296 : e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4544 86296 : e2 = cp_build_fold_indirect_ref (e2);
4545 86296 : if (e2 == error_mark_node)
4546 : return error_mark_node;
4547 86296 : TREE_CONSTANT (e2) = 1;
4548 :
4549 : /* When using function descriptors, the address of the
4550 : vtable entry is treated as a function pointer. */
4551 86296 : if (TARGET_VTABLE_USES_DESCRIPTORS)
4552 : e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4553 : cp_build_addr_expr (e2, complain));
4554 :
4555 86296 : e2 = fold_convert (TREE_TYPE (e3), e2);
4556 86296 : e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4557 86296 : if (e1 == error_mark_node)
4558 : return error_mark_node;
4559 :
4560 : /* Make sure this doesn't get evaluated first inside one of the
4561 : branches of the COND_EXPR. */
4562 86296 : if (instance_save_expr)
4563 85947 : e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4564 : instance_save_expr, e1);
4565 :
4566 : function = e1;
4567 : }
4568 : return function;
4569 : }
4570 :
4571 : /* Used by the C-common bits. */
4572 : tree
4573 0 : build_function_call (location_t /*loc*/,
4574 : tree function, tree params)
4575 : {
4576 0 : return cp_build_function_call (function, params, tf_warning_or_error);
4577 : }
4578 :
4579 : /* Used by the C-common bits. */
4580 : tree
4581 340774 : build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4582 : tree function, vec<tree, va_gc> *params,
4583 : vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4584 : {
4585 340774 : vec<tree, va_gc> *orig_params = params;
4586 340774 : tree ret = cp_build_function_call_vec (function, ¶ms,
4587 : tf_warning_or_error, orig_function);
4588 :
4589 : /* cp_build_function_call_vec can reallocate PARAMS by adding
4590 : default arguments. That should never happen here. Verify
4591 : that. */
4592 340774 : gcc_assert (params == orig_params);
4593 :
4594 340774 : return ret;
4595 : }
4596 :
4597 : /* Build a function call using a tree list of arguments. */
4598 :
4599 : static tree
4600 0 : cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4601 : {
4602 0 : tree ret;
4603 :
4604 0 : releasing_vec vec;
4605 0 : for (; params != NULL_TREE; params = TREE_CHAIN (params))
4606 0 : vec_safe_push (vec, TREE_VALUE (params));
4607 0 : ret = cp_build_function_call_vec (function, &vec, complain);
4608 0 : return ret;
4609 0 : }
4610 :
4611 : /* Build a function call using varargs. */
4612 :
4613 : tree
4614 615380 : cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4615 : {
4616 615380 : va_list args;
4617 615380 : tree ret, t;
4618 :
4619 615380 : releasing_vec vec;
4620 615380 : va_start (args, complain);
4621 1538210 : for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4622 922830 : vec_safe_push (vec, t);
4623 615380 : va_end (args);
4624 615380 : ret = cp_build_function_call_vec (function, &vec, complain);
4625 615380 : return ret;
4626 615380 : }
4627 :
4628 : /* C++ implementation of callback for use when checking param types. */
4629 :
4630 : bool
4631 18 : cp_comp_parm_types (tree wanted_type, tree actual_type)
4632 : {
4633 18 : if (TREE_CODE (wanted_type) == POINTER_TYPE
4634 18 : && TREE_CODE (actual_type) == POINTER_TYPE)
4635 15 : return same_or_base_type_p (TREE_TYPE (wanted_type),
4636 : TREE_TYPE (actual_type));
4637 : return false;
4638 : }
4639 :
4640 : /* Build a function call using a vector of arguments.
4641 : If FUNCTION is the result of resolving an overloaded target built-in,
4642 : ORIG_FNDECL is the original function decl, otherwise it is null.
4643 : PARAMS may be NULL if there are no parameters. This changes the
4644 : contents of PARAMS. */
4645 :
4646 : tree
4647 7807877 : cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4648 : tsubst_flags_t complain, tree orig_fndecl)
4649 : {
4650 7807877 : tree fntype, fndecl;
4651 7807877 : int is_method;
4652 7807877 : tree original = function;
4653 7807877 : int nargs;
4654 7807877 : tree *argarray;
4655 7807877 : tree parm_types;
4656 7807877 : vec<tree, va_gc> *allocated = NULL;
4657 7807877 : tree ret;
4658 :
4659 : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4660 : expressions, like those used for ObjC messenger dispatches. */
4661 7807877 : if (params != NULL && !vec_safe_is_empty (*params))
4662 2229195 : function = objc_rewrite_function_call (function, (**params)[0]);
4663 :
4664 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4665 : Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4666 7807877 : if (TREE_CODE (function) == NOP_EXPR
4667 7807877 : && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4668 19 : function = TREE_OPERAND (function, 0);
4669 :
4670 7807877 : if (TREE_CODE (function) == FUNCTION_DECL)
4671 : {
4672 1688011 : if (!mark_used (function, complain))
4673 24 : return error_mark_node;
4674 1687987 : fndecl = function;
4675 :
4676 : /* For target_version semantics, the function set cannot be called
4677 : if there is no default version in scope. */
4678 1687987 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
4679 : && !is_function_default_version (fndecl))
4680 : {
4681 : if (complain & tf_error)
4682 : error ("no default version in scope");
4683 : return error_mark_node;
4684 : }
4685 :
4686 : /* Convert anything with function type to a pointer-to-function. */
4687 1687987 : if (DECL_MAIN_P (function))
4688 : {
4689 0 : if (complain & tf_error)
4690 0 : pedwarn (input_location, OPT_Wpedantic,
4691 : "ISO C++ forbids calling %<::main%> from within program");
4692 : else
4693 0 : return error_mark_node;
4694 : }
4695 1687987 : function = build_addr_func (function, complain);
4696 : }
4697 : else
4698 : {
4699 6119866 : fndecl = NULL_TREE;
4700 :
4701 6119866 : function = build_addr_func (function, complain);
4702 : }
4703 :
4704 7807853 : if (function == error_mark_node)
4705 : return error_mark_node;
4706 :
4707 7807751 : fntype = TREE_TYPE (function);
4708 :
4709 7807751 : if (TYPE_PTRMEMFUNC_P (fntype))
4710 : {
4711 15 : if (complain & tf_error)
4712 15 : error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4713 : "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4714 : original, original);
4715 15 : return error_mark_node;
4716 : }
4717 :
4718 15615472 : is_method = (TYPE_PTR_P (fntype)
4719 7807736 : && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4720 :
4721 7807736 : if (!(TYPE_PTRFN_P (fntype)
4722 88784 : || is_method
4723 2269 : || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4724 : {
4725 2269 : if (complain & tf_error)
4726 : {
4727 178 : if (is_stub_object (original))
4728 3 : error_at (input_location,
4729 : "%qT cannot be used as a function",
4730 3 : TREE_TYPE (original));
4731 175 : else if (!flag_diagnostics_show_caret)
4732 175 : error_at (input_location,
4733 : "%qE cannot be used as a function", original);
4734 0 : else if (DECL_P (original))
4735 0 : error_at (input_location,
4736 : "%qD cannot be used as a function", original);
4737 : else
4738 0 : error_at (input_location,
4739 : "expression cannot be used as a function");
4740 : }
4741 :
4742 2269 : return error_mark_node;
4743 : }
4744 :
4745 : /* fntype now gets the type of function pointed to. */
4746 7805467 : fntype = TREE_TYPE (fntype);
4747 7805467 : parm_types = TYPE_ARG_TYPES (fntype);
4748 :
4749 7805467 : if (params == NULL)
4750 : {
4751 160525 : allocated = make_tree_vector ();
4752 160525 : params = &allocated;
4753 : }
4754 :
4755 7805467 : nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4756 : complain);
4757 7805467 : if (nargs < 0)
4758 2022 : return error_mark_node;
4759 :
4760 7803445 : argarray = (*params)->address ();
4761 :
4762 : /* Check for errors in format strings and inappropriately
4763 : null parameters. */
4764 7803445 : bool warned_p
4765 7803445 : = ((complain & tf_warning)
4766 7803445 : && check_function_arguments (input_location, fndecl, fntype,
4767 : nargs, argarray, NULL,
4768 7803445 : cp_comp_parm_types));
4769 :
4770 7803445 : ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4771 :
4772 7803445 : if (warned_p)
4773 : {
4774 27 : tree c = extract_call_expr (ret);
4775 27 : if (TREE_CODE (c) == CALL_EXPR)
4776 27 : suppress_warning (c, OPT_Wnonnull);
4777 : }
4778 :
4779 7803445 : if (allocated != NULL)
4780 160525 : release_tree_vector (allocated);
4781 :
4782 : return ret;
4783 : }
4784 :
4785 : /* Subroutine of convert_arguments.
4786 : Print an error message about a wrong number of arguments. */
4787 :
4788 : static void
4789 162 : error_args_num (location_t loc, tree fndecl, bool too_many_p)
4790 : {
4791 162 : if (fndecl)
4792 : {
4793 126 : auto_diagnostic_group d;
4794 126 : if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4795 : {
4796 0 : if (DECL_NAME (fndecl) == NULL_TREE
4797 0 : || (DECL_NAME (fndecl)
4798 0 : == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4799 0 : error_at (loc,
4800 : too_many_p
4801 : ? G_("too many arguments to constructor %q#D")
4802 : : G_("too few arguments to constructor %q#D"),
4803 : fndecl);
4804 : else
4805 0 : error_at (loc,
4806 : too_many_p
4807 : ? G_("too many arguments to member function %q#D")
4808 : : G_("too few arguments to member function %q#D"),
4809 : fndecl);
4810 : }
4811 : else
4812 189 : error_at (loc,
4813 : too_many_p
4814 : ? G_("too many arguments to function %q#D")
4815 : : G_("too few arguments to function %q#D"),
4816 : fndecl);
4817 126 : if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4818 87 : inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4819 126 : }
4820 : else
4821 : {
4822 36 : if (c_dialect_objc () && objc_message_selector ())
4823 0 : error_at (loc,
4824 : too_many_p
4825 : ? G_("too many arguments to method %q#D")
4826 : : G_("too few arguments to method %q#D"),
4827 : objc_message_selector ());
4828 : else
4829 57 : error_at (loc, too_many_p ? G_("too many arguments to function")
4830 : : G_("too few arguments to function"));
4831 : }
4832 162 : }
4833 :
4834 : /* Convert the actual parameter expressions in the list VALUES to the
4835 : types in the list TYPELIST. The converted expressions are stored
4836 : back in the VALUES vector.
4837 : If parmdecls is exhausted, or when an element has NULL as its type,
4838 : perform the default conversions.
4839 :
4840 : NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4841 :
4842 : This is also where warnings about wrong number of args are generated.
4843 :
4844 : Returns the actual number of arguments processed (which might be less
4845 : than the length of the vector), or -1 on error.
4846 :
4847 : In C++, unspecified trailing parameters can be filled in with their
4848 : default arguments, if such were specified. Do so here. */
4849 :
4850 : static int
4851 7805467 : convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4852 : int flags, tsubst_flags_t complain)
4853 : {
4854 7805467 : tree typetail;
4855 7805467 : unsigned int i;
4856 :
4857 : /* Argument passing is always copy-initialization. */
4858 7805467 : flags |= LOOKUP_ONLYCONVERTING;
4859 :
4860 12641338 : for (i = 0, typetail = typelist;
4861 12641338 : i < vec_safe_length (*values);
4862 : i++)
4863 : {
4864 9675000 : tree type = typetail ? TREE_VALUE (typetail) : 0;
4865 4837723 : tree val = (**values)[i];
4866 :
4867 4837723 : if (val == error_mark_node || type == error_mark_node)
4868 : return -1;
4869 :
4870 4837717 : if (type == void_type_node)
4871 : {
4872 212 : if (complain & tf_error)
4873 78 : error_args_num (input_location, fndecl, /*too_many_p=*/true);
4874 : return -1;
4875 : }
4876 :
4877 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4878 : Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4879 4837505 : if (TREE_CODE (val) == NOP_EXPR
4880 1264920 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4881 4837523 : && (type == 0 || !TYPE_REF_P (type)))
4882 18 : val = TREE_OPERAND (val, 0);
4883 :
4884 4837505 : if (type == 0 || !TYPE_REF_P (type))
4885 : {
4886 4631879 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4887 4631879 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4888 11090 : val = decay_conversion (val, complain);
4889 : }
4890 :
4891 4837505 : if (val == error_mark_node)
4892 : return -1;
4893 :
4894 4837505 : if (type != 0)
4895 : {
4896 : /* Formal parm type is specified by a function prototype. */
4897 4837059 : tree parmval;
4898 :
4899 4837059 : if (!COMPLETE_TYPE_P (complete_type (type)))
4900 : {
4901 18 : if (complain & tf_error)
4902 : {
4903 12 : location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4904 12 : if (fndecl)
4905 : {
4906 12 : auto_diagnostic_group d;
4907 12 : error_at (loc,
4908 : "parameter %P of %qD has incomplete type %qT",
4909 : i, fndecl, type);
4910 12 : inform (get_fndecl_argument_location (fndecl, i),
4911 : " declared here");
4912 12 : }
4913 : else
4914 0 : error_at (loc, "parameter %P has incomplete type %qT", i,
4915 : type);
4916 : }
4917 18 : parmval = error_mark_node;
4918 : }
4919 : else
4920 : {
4921 4837041 : parmval = convert_for_initialization
4922 4837041 : (NULL_TREE, type, val, flags,
4923 : ICR_ARGPASS, fndecl, i, complain);
4924 4837041 : parmval = convert_for_arg_passing (type, parmval, complain);
4925 : }
4926 :
4927 4837059 : if (parmval == error_mark_node)
4928 : return -1;
4929 :
4930 4835425 : (**values)[i] = parmval;
4931 : }
4932 : else
4933 : {
4934 446 : int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4935 21 : if (magic)
4936 : {
4937 : /* Don't truncate excess precision to the semantic type. */
4938 0 : if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4939 0 : val = TREE_OPERAND (val, 0);
4940 : /* Don't do ellipsis conversion for __built_in_constant_p
4941 : as this will result in spurious errors for non-trivial
4942 : types. */
4943 0 : val = require_complete_type (val, complain);
4944 : }
4945 : else
4946 446 : val = convert_arg_to_ellipsis (val, complain);
4947 :
4948 446 : (**values)[i] = val;
4949 : }
4950 :
4951 4835871 : if (typetail)
4952 4835425 : typetail = TREE_CHAIN (typetail);
4953 : }
4954 :
4955 7803615 : if (typetail != 0 && typetail != void_list_node)
4956 : {
4957 : /* See if there are default arguments that can be used. Because
4958 : we hold default arguments in the FUNCTION_TYPE (which is so
4959 : wrong), we can see default parameters here from deduced
4960 : contexts (and via typeof) for indirect function calls.
4961 : Fortunately we know whether we have a function decl to
4962 : provide default arguments in a language conformant
4963 : manner. */
4964 63 : if (fndecl && TREE_PURPOSE (typetail)
4965 173 : && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4966 : {
4967 6 : for (; typetail != void_list_node; ++i)
4968 : {
4969 : /* After DR777, with explicit template args we can end up with a
4970 : default argument followed by no default argument. */
4971 6 : if (!TREE_PURPOSE (typetail))
4972 : break;
4973 3 : tree parmval
4974 3 : = convert_default_arg (TREE_VALUE (typetail),
4975 3 : TREE_PURPOSE (typetail),
4976 3 : fndecl, i, complain);
4977 :
4978 3 : if (parmval == error_mark_node)
4979 0 : return -1;
4980 :
4981 3 : vec_safe_push (*values, parmval);
4982 3 : typetail = TREE_CHAIN (typetail);
4983 : /* ends with `...'. */
4984 3 : if (typetail == NULL_TREE)
4985 : break;
4986 : }
4987 : }
4988 :
4989 170 : if (typetail && typetail != void_list_node)
4990 : {
4991 170 : if (complain & tf_error)
4992 84 : error_args_num (input_location, fndecl, /*too_many_p=*/false);
4993 : return -1;
4994 : }
4995 : }
4996 :
4997 7803445 : return (int) i;
4998 : }
4999 :
5000 : /* Build a binary-operation expression, after performing default
5001 : conversions on the operands. CODE is the kind of expression to
5002 : build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
5003 : are the tree codes which correspond to ARG1 and ARG2 when issuing
5004 : warnings about possibly misplaced parentheses. They may differ
5005 : from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
5006 : folding (e.g., if the parser sees "a | 1 + 1", it may call this
5007 : routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
5008 : To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
5009 : ARG2_CODE as ERROR_MARK. */
5010 :
5011 : tree
5012 239112578 : build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
5013 : enum tree_code arg1_code, tree arg2,
5014 : enum tree_code arg2_code, tree lookups,
5015 : tree *overload_p, tsubst_flags_t complain)
5016 : {
5017 239112578 : tree orig_arg1;
5018 239112578 : tree orig_arg2;
5019 239112578 : tree expr;
5020 239112578 : tree overload = NULL_TREE;
5021 :
5022 239112578 : orig_arg1 = arg1;
5023 239112578 : orig_arg2 = arg2;
5024 :
5025 239112578 : if (processing_template_decl)
5026 : {
5027 140378756 : if (type_dependent_expression_p (arg1)
5028 140378756 : || type_dependent_expression_p (arg2))
5029 : {
5030 95676827 : expr = build_min_nt_loc (loc, code, arg1, arg2);
5031 95676827 : TREE_TYPE (expr)
5032 95676827 : = build_dependent_operator_type (lookups, code, false);
5033 95676827 : return expr;
5034 : }
5035 : }
5036 :
5037 143435751 : if (code == DOTSTAR_EXPR)
5038 86780 : expr = build_m_component_ref (arg1, arg2, complain);
5039 : else
5040 143348971 : expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
5041 : lookups, &overload, complain);
5042 :
5043 143435718 : if (overload_p != NULL)
5044 63351376 : *overload_p = overload;
5045 :
5046 : /* Check for cases such as x+y<<z which users are likely to
5047 : misinterpret. But don't warn about obj << x + y, since that is a
5048 : common idiom for I/O. */
5049 143435718 : if (warn_parentheses
5050 1262301 : && (complain & tf_warning)
5051 1204184 : && !processing_template_decl
5052 821774 : && !error_operand_p (arg1)
5053 821768 : && !error_operand_p (arg2)
5054 144257474 : && (code != LSHIFT_EXPR
5055 71195 : || !CLASS_TYPE_P (TREE_TYPE (arg1))))
5056 820709 : warn_about_parentheses (loc, code, arg1_code, orig_arg1,
5057 : arg2_code, orig_arg2);
5058 :
5059 143435718 : if (processing_template_decl && expr != error_mark_node)
5060 : {
5061 44701698 : if (overload != NULL_TREE)
5062 3924955 : return (build_min_non_dep_op_overload
5063 3924955 : (code, expr, overload, orig_arg1, orig_arg2));
5064 :
5065 40776743 : return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
5066 : }
5067 :
5068 : return expr;
5069 : }
5070 :
5071 : /* Build and return an ARRAY_REF expression. */
5072 :
5073 : tree
5074 2623734 : build_x_array_ref (location_t loc, tree arg1, tree arg2,
5075 : tsubst_flags_t complain)
5076 : {
5077 2623734 : tree orig_arg1 = arg1;
5078 2623734 : tree orig_arg2 = arg2;
5079 2623734 : tree expr;
5080 2623734 : tree overload = NULL_TREE;
5081 :
5082 2623734 : if (processing_template_decl)
5083 : {
5084 9499 : if (type_dependent_expression_p (arg1)
5085 9499 : || type_dependent_expression_p (arg2))
5086 4551 : return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
5087 4551 : NULL_TREE, NULL_TREE);
5088 : }
5089 :
5090 2619183 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
5091 : NULL_TREE, NULL_TREE, &overload, complain);
5092 :
5093 2619183 : if (processing_template_decl && expr != error_mark_node)
5094 : {
5095 4945 : if (overload != NULL_TREE)
5096 4936 : return (build_min_non_dep_op_overload
5097 4936 : (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
5098 :
5099 9 : return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
5100 9 : NULL_TREE, NULL_TREE);
5101 : }
5102 : return expr;
5103 : }
5104 :
5105 : /* Build an OpenMP array section reference, creating an exact type for the
5106 : resulting expression based on the element type and bounds if possible. If
5107 : we have variable bounds, create an incomplete array type for the result
5108 : instead. */
5109 :
5110 : tree
5111 11088 : build_omp_array_section (location_t loc, tree array_expr, tree index,
5112 : tree length)
5113 : {
5114 11088 : if (TREE_CODE (array_expr) == TYPE_DECL
5115 11088 : || type_dependent_expression_p (array_expr))
5116 274 : return build3_loc (loc, OMP_ARRAY_SECTION, NULL_TREE, array_expr, index,
5117 274 : length);
5118 :
5119 10814 : tree type = TREE_TYPE (array_expr);
5120 10814 : gcc_assert (type);
5121 10814 : type = non_reference (type);
5122 :
5123 10814 : tree sectype, eltype = TREE_TYPE (type);
5124 :
5125 : /* It's not an array or pointer type. Just reuse the type of the
5126 : original expression as the type of the array section (an error will be
5127 : raised anyway, later). */
5128 10814 : if (eltype == NULL_TREE)
5129 42 : sectype = TREE_TYPE (array_expr);
5130 : else
5131 : {
5132 10772 : tree idxtype = NULL_TREE;
5133 :
5134 : /* If we know the integer bounds, create an index type with exact
5135 : low/high (or zero/length) bounds. Otherwise, create an incomplete
5136 : array type. (This mostly only affects diagnostics.) */
5137 10772 : if (index != NULL_TREE
5138 10772 : && length != NULL_TREE
5139 7695 : && TREE_CODE (index) == INTEGER_CST
5140 6878 : && TREE_CODE (length) == INTEGER_CST)
5141 : {
5142 5375 : tree low = fold_convert (sizetype, index);
5143 5375 : tree high = fold_convert (sizetype, length);
5144 5375 : high = size_binop (PLUS_EXPR, low, high);
5145 5375 : high = size_binop (MINUS_EXPR, high, size_one_node);
5146 5375 : idxtype = build_range_type (sizetype, low, high);
5147 5375 : }
5148 3007 : else if ((index == NULL_TREE || integer_zerop (index))
5149 3714 : && length != NULL_TREE
5150 7607 : && TREE_CODE (length) == INTEGER_CST)
5151 1496 : idxtype = build_index_type (length);
5152 :
5153 10772 : sectype = build_array_type (eltype, idxtype);
5154 : }
5155 :
5156 10814 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array_expr, index,
5157 10814 : length);
5158 : }
5159 :
5160 : /* Return whether OP is an expression of enum type cast to integer
5161 : type. In C++ even unsigned enum types are cast to signed integer
5162 : types. We do not want to issue warnings about comparisons between
5163 : signed and unsigned types when one of the types is an enum type.
5164 : Those warnings are always false positives in practice. */
5165 :
5166 : static bool
5167 645134 : enum_cast_to_int (tree op)
5168 : {
5169 632111 : if (CONVERT_EXPR_P (op)
5170 14050 : && TREE_TYPE (op) == integer_type_node
5171 910 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
5172 645149 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
5173 : return true;
5174 :
5175 : /* The cast may have been pushed into a COND_EXPR. */
5176 645127 : if (TREE_CODE (op) == COND_EXPR)
5177 765 : return (enum_cast_to_int (TREE_OPERAND (op, 1))
5178 765 : || enum_cast_to_int (TREE_OPERAND (op, 2)));
5179 :
5180 : return false;
5181 : }
5182 :
5183 : /* For the c-common bits. */
5184 : tree
5185 5103014 : build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
5186 : bool /*convert_p*/)
5187 : {
5188 5103014 : return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
5189 : }
5190 :
5191 : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
5192 : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
5193 :
5194 : static tree
5195 8229 : build_vec_cmp (tree_code code, tree type,
5196 : tree arg0, tree arg1)
5197 : {
5198 8229 : tree zero_vec = build_zero_cst (type);
5199 8229 : tree minus_one_vec = build_minus_one_cst (type);
5200 8229 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
5201 8229 : tree cmp = build2 (code, cmp_type, arg0, arg1);
5202 8229 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
5203 : }
5204 :
5205 : /* Possibly warn about an address never being NULL. */
5206 :
5207 : static void
5208 1828683 : warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
5209 : {
5210 : /* Prevent warnings issued for macro expansion. */
5211 1828683 : if (!warn_address
5212 41700 : || (complain & tf_warning) == 0
5213 24943 : || c_inhibit_evaluation_warnings != 0
5214 24926 : || from_macro_expansion_at (location)
5215 1846876 : || warning_suppressed_p (op, OPT_Waddress))
5216 1810813 : return;
5217 :
5218 18160 : tree cop = fold_for_warn (op);
5219 :
5220 18160 : if (TREE_CODE (cop) == NON_LVALUE_EXPR)
5221 : /* Unwrap the expression for C++ 98. */
5222 1 : cop = TREE_OPERAND (cop, 0);
5223 :
5224 18160 : if (TREE_CODE (cop) == PTRMEM_CST)
5225 : {
5226 : /* The address of a nonstatic data member is never null. */
5227 30 : warning_at (location, OPT_Waddress,
5228 : "the address %qE will never be NULL",
5229 : cop);
5230 30 : return;
5231 : }
5232 :
5233 18130 : if (TREE_CODE (cop) == NOP_EXPR)
5234 : {
5235 : /* Allow casts to intptr_t to suppress the warning. */
5236 1248 : tree type = TREE_TYPE (cop);
5237 1248 : if (TREE_CODE (type) == INTEGER_TYPE)
5238 : return;
5239 :
5240 1248 : STRIP_NOPS (cop);
5241 : }
5242 :
5243 18130 : auto_diagnostic_group d;
5244 18130 : bool warned = false;
5245 18130 : if (TREE_CODE (cop) == ADDR_EXPR)
5246 : {
5247 789 : cop = TREE_OPERAND (cop, 0);
5248 :
5249 : /* Set to true in the loop below if OP dereferences its operand.
5250 : In such a case the ultimate target need not be a decl for
5251 : the null [in]equality test to be necessarily constant. */
5252 789 : bool deref = false;
5253 :
5254 : /* Get the outermost array or object, or member. */
5255 960 : while (handled_component_p (cop))
5256 : {
5257 270 : if (TREE_CODE (cop) == COMPONENT_REF)
5258 : {
5259 : /* Get the member (its address is never null). */
5260 99 : cop = TREE_OPERAND (cop, 1);
5261 99 : break;
5262 : }
5263 :
5264 : /* Get the outer array/object to refer to in the warning. */
5265 171 : cop = TREE_OPERAND (cop, 0);
5266 171 : deref = true;
5267 : }
5268 :
5269 639 : if ((!deref && !decl_with_nonnull_addr_p (cop))
5270 631 : || from_macro_expansion_at (location)
5271 1420 : || warning_suppressed_p (cop, OPT_Waddress))
5272 : return;
5273 :
5274 631 : warned = warning_at (location, OPT_Waddress,
5275 : "the address of %qD will never be NULL", cop);
5276 631 : op = cop;
5277 : }
5278 17341 : else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
5279 : {
5280 : /* Adding zero to the null pointer is well-defined in C++. When
5281 : the offset is unknown (i.e., not a constant) warn anyway since
5282 : it's less likely that the pointer operand is null than not. */
5283 102 : tree off = TREE_OPERAND (cop, 1);
5284 102 : if (!integer_zerop (off)
5285 102 : && !warning_suppressed_p (cop, OPT_Waddress))
5286 : {
5287 102 : tree base = TREE_OPERAND (cop, 0);
5288 102 : STRIP_NOPS (base);
5289 102 : if (TYPE_REF_P (TREE_TYPE (base)))
5290 12 : warning_at (location, OPT_Waddress, "the compiler can assume that "
5291 : "the address of %qE will never be NULL", base);
5292 : else
5293 90 : warning_at (location, OPT_Waddress, "comparing the result of "
5294 : "pointer addition %qE and NULL", cop);
5295 : }
5296 : return;
5297 : }
5298 16272 : else if (CONVERT_EXPR_P (op)
5299 17301 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
5300 : {
5301 62 : STRIP_NOPS (op);
5302 :
5303 62 : if (TREE_CODE (op) == COMPONENT_REF)
5304 24 : op = TREE_OPERAND (op, 1);
5305 :
5306 62 : if (DECL_P (op))
5307 62 : warned = warning_at (location, OPT_Waddress,
5308 : "the compiler can assume that the address of "
5309 : "%qD will never be NULL", op);
5310 : }
5311 :
5312 693 : if (warned && DECL_P (op))
5313 657 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
5314 18130 : }
5315 :
5316 : /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5317 : the other operand is of a different enumeration type or a floating-point
5318 : type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5319 : code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5320 : and LOC is the location for the whole binary expression.
5321 : For C++26 this is ill-formed rather than deprecated.
5322 : Return true for SFINAE errors.
5323 : TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5324 :
5325 : static bool
5326 114005604 : do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5327 : tree type1, tsubst_flags_t complain)
5328 : {
5329 114005604 : if (TREE_CODE (type0) == ENUMERAL_TYPE
5330 3488920 : && TREE_CODE (type1) == ENUMERAL_TYPE
5331 116964200 : && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
5332 : {
5333 200 : if (cxx_dialect >= cxx26)
5334 : {
5335 65 : if ((complain & tf_warning_or_error) == 0)
5336 : return true;
5337 : }
5338 135 : else if ((complain & tf_warning) == 0)
5339 : return false;
5340 : /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5341 : Otherwise, warn if -Wenum-conversion is on. */
5342 193 : enum opt_code opt;
5343 193 : if (warn_deprecated_enum_enum_conv)
5344 : opt = OPT_Wdeprecated_enum_enum_conversion;
5345 93 : else if (warn_enum_conversion)
5346 : opt = OPT_Wenum_conversion;
5347 : else
5348 : return false;
5349 :
5350 122 : switch (code)
5351 : {
5352 : case GT_EXPR:
5353 : case LT_EXPR:
5354 : case GE_EXPR:
5355 : case LE_EXPR:
5356 : case EQ_EXPR:
5357 : case NE_EXPR:
5358 : /* Comparisons are handled by -Wenum-compare. */
5359 : return false;
5360 : case SPACESHIP_EXPR:
5361 : /* This is invalid, don't warn. */
5362 : return false;
5363 18 : case BIT_AND_EXPR:
5364 18 : case BIT_IOR_EXPR:
5365 18 : case BIT_XOR_EXPR:
5366 18 : if (cxx_dialect >= cxx26)
5367 6 : pedwarn (loc, opt, "bitwise operation between different "
5368 : "enumeration types %qT and %qT", type0, type1);
5369 : else
5370 12 : warning_at (loc, opt, "bitwise operation between different "
5371 : "enumeration types %qT and %qT is deprecated",
5372 : type0, type1);
5373 : return false;
5374 42 : default:
5375 42 : if (cxx_dialect >= cxx26)
5376 13 : pedwarn (loc, opt, "arithmetic between different enumeration "
5377 : "types %qT and %qT", type0, type1);
5378 : else
5379 29 : warning_at (loc, opt, "arithmetic between different enumeration "
5380 : "types %qT and %qT is deprecated", type0, type1);
5381 : return false;
5382 : }
5383 : }
5384 114005404 : else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5385 3488720 : && SCALAR_FLOAT_TYPE_P (type1))
5386 114005332 : || (SCALAR_FLOAT_TYPE_P (type0)
5387 31618848 : && TREE_CODE (type1) == ENUMERAL_TYPE))
5388 : {
5389 151 : if (cxx_dialect >= cxx26)
5390 : {
5391 54 : if ((complain & tf_warning_or_error) == 0)
5392 : return true;
5393 : }
5394 97 : else if ((complain & tf_warning) == 0)
5395 : return false;
5396 139 : const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5397 : /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5398 : Otherwise, warn if -Wenum-conversion is on. */
5399 139 : enum opt_code opt;
5400 139 : if (warn_deprecated_enum_float_conv)
5401 : opt = OPT_Wdeprecated_enum_float_conversion;
5402 68 : else if (warn_enum_conversion)
5403 : opt = OPT_Wenum_conversion;
5404 : else
5405 : return false;
5406 :
5407 88 : switch (code)
5408 : {
5409 36 : case GT_EXPR:
5410 36 : case LT_EXPR:
5411 36 : case GE_EXPR:
5412 36 : case LE_EXPR:
5413 36 : case EQ_EXPR:
5414 36 : case NE_EXPR:
5415 36 : if (enum_first_p && cxx_dialect >= cxx26)
5416 6 : pedwarn (loc, opt, "comparison of enumeration type %qT with "
5417 : "floating-point type %qT", type0, type1);
5418 30 : else if (cxx_dialect >= cxx26)
5419 6 : pedwarn (loc, opt, "comparison of floating-point type %qT "
5420 : "with enumeration type %qT", type0, type1);
5421 24 : else if (enum_first_p)
5422 12 : warning_at (loc, opt, "comparison of enumeration type %qT with "
5423 : "floating-point type %qT is deprecated",
5424 : type0, type1);
5425 : else
5426 12 : warning_at (loc, opt, "comparison of floating-point type %qT "
5427 : "with enumeration type %qT is deprecated",
5428 : type0, type1);
5429 : return false;
5430 : case SPACESHIP_EXPR:
5431 : /* This is invalid, don't warn. */
5432 : return false;
5433 40 : default:
5434 40 : if (enum_first_p && cxx_dialect >= cxx26)
5435 6 : pedwarn (loc, opt, "arithmetic between enumeration type %qT "
5436 : "and floating-point type %qT", type0, type1);
5437 34 : else if (cxx_dialect >= cxx26)
5438 7 : pedwarn (loc, opt, "arithmetic between floating-point type %qT "
5439 : "and enumeration type %qT", type0, type1);
5440 27 : else if (enum_first_p)
5441 12 : warning_at (loc, opt, "arithmetic between enumeration type %qT "
5442 : "and floating-point type %qT is deprecated",
5443 : type0, type1);
5444 : else
5445 15 : warning_at (loc, opt, "arithmetic between floating-point type %qT "
5446 : "and enumeration type %qT is deprecated",
5447 : type0, type1);
5448 : return false;
5449 : }
5450 : }
5451 : return false;
5452 : }
5453 :
5454 : /* Build a binary-operation expression without default conversions.
5455 : CODE is the kind of expression to build.
5456 : LOCATION is the location_t of the operator in the source code.
5457 : This function differs from `build' in several ways:
5458 : the data type of the result is computed and recorded in it,
5459 : warnings are generated if arg data types are invalid,
5460 : special handling for addition and subtraction of pointers is known,
5461 : and some optimization is done (operations on narrow ints
5462 : are done in the narrower type when that gives the same result).
5463 : Constant folding is also done before the result is returned.
5464 :
5465 : Note that the operands will never have enumeral types
5466 : because either they have just had the default conversions performed
5467 : or they have both just been converted to some other type in which
5468 : the arithmetic is to be done.
5469 :
5470 : C++: must do special pointer arithmetic when implementing
5471 : multiple inheritance, and deal with pointer to member functions. */
5472 :
5473 : tree
5474 153355109 : cp_build_binary_op (const op_location_t &location,
5475 : enum tree_code code, tree orig_op0, tree orig_op1,
5476 : tsubst_flags_t complain)
5477 : {
5478 153355109 : tree op0, op1;
5479 153355109 : enum tree_code code0, code1;
5480 153355109 : tree type0, type1, orig_type0, orig_type1;
5481 153355109 : const char *invalid_op_diag;
5482 :
5483 : /* Expression code to give to the expression when it is built.
5484 : Normally this is CODE, which is what the caller asked for,
5485 : but in some special cases we change it. */
5486 153355109 : enum tree_code resultcode = code;
5487 :
5488 : /* Data type in which the computation is to be performed.
5489 : In the simplest cases this is the common type of the arguments. */
5490 153355109 : tree result_type = NULL_TREE;
5491 :
5492 : /* When the computation is in excess precision, the type of the
5493 : final EXCESS_PRECISION_EXPR. */
5494 153355109 : tree semantic_result_type = NULL;
5495 :
5496 : /* Nonzero means operands have already been type-converted
5497 : in whatever way is necessary.
5498 : Zero means they need to be converted to RESULT_TYPE. */
5499 153355109 : int converted = 0;
5500 :
5501 : /* Nonzero means create the expression with this type, rather than
5502 : RESULT_TYPE. */
5503 153355109 : tree build_type = 0;
5504 :
5505 : /* Nonzero means after finally constructing the expression
5506 : convert it to this type. */
5507 153355109 : tree final_type = 0;
5508 :
5509 153355109 : tree result;
5510 :
5511 : /* Nonzero if this is an operation like MIN or MAX which can
5512 : safely be computed in short if both args are promoted shorts.
5513 : Also implies COMMON.
5514 : -1 indicates a bitwise operation; this makes a difference
5515 : in the exact conditions for when it is safe to do the operation
5516 : in a narrower mode. */
5517 153355109 : int shorten = 0;
5518 :
5519 : /* Nonzero if this is a comparison operation;
5520 : if both args are promoted shorts, compare the original shorts.
5521 : Also implies COMMON. */
5522 153355109 : int short_compare = 0;
5523 :
5524 : /* Nonzero if this is a right-shift operation, which can be computed on the
5525 : original short and then promoted if the operand is a promoted short. */
5526 153355109 : int short_shift = 0;
5527 :
5528 : /* Nonzero means set RESULT_TYPE to the common type of the args. */
5529 153355109 : int common = 0;
5530 :
5531 : /* True if both operands have arithmetic type. */
5532 153355109 : bool arithmetic_types_p;
5533 :
5534 : /* Remember whether we're doing / or %. */
5535 153355109 : bool doing_div_or_mod = false;
5536 :
5537 : /* Remember whether we're doing << or >>. */
5538 153355109 : bool doing_shift = false;
5539 :
5540 : /* Tree holding instrumentation expression. */
5541 153355109 : tree instrument_expr = NULL_TREE;
5542 :
5543 : /* True means this is an arithmetic operation that may need excess
5544 : precision. */
5545 153355109 : bool may_need_excess_precision;
5546 :
5547 : /* Apply default conversions. */
5548 153355109 : op0 = resolve_nondeduced_context (orig_op0, complain);
5549 153355109 : op1 = resolve_nondeduced_context (orig_op1, complain);
5550 :
5551 153355109 : if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5552 138713848 : || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5553 135072596 : || code == TRUTH_XOR_EXPR)
5554 : {
5555 18282513 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5556 18282495 : op0 = decay_conversion (op0, complain);
5557 18282513 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5558 18282513 : op1 = decay_conversion (op1, complain);
5559 : }
5560 : else
5561 : {
5562 135072596 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5563 135072547 : op0 = cp_default_conversion (op0, complain);
5564 135072596 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5565 135072577 : op1 = cp_default_conversion (op1, complain);
5566 : }
5567 :
5568 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5569 162026275 : STRIP_TYPE_NOPS (op0);
5570 188154581 : STRIP_TYPE_NOPS (op1);
5571 :
5572 : /* DTRT if one side is an overloaded function, but complain about it. */
5573 153355109 : if (type_unknown_p (op0))
5574 : {
5575 9 : tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5576 9 : if (t != error_mark_node)
5577 : {
5578 0 : if (complain & tf_error)
5579 0 : permerror (location,
5580 : "assuming cast to type %qT from overloaded function",
5581 0 : TREE_TYPE (t));
5582 : op0 = t;
5583 : }
5584 : }
5585 153355109 : if (type_unknown_p (op1))
5586 : {
5587 3 : tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5588 3 : if (t != error_mark_node)
5589 : {
5590 3 : if (complain & tf_error)
5591 3 : permerror (location,
5592 : "assuming cast to type %qT from overloaded function",
5593 3 : TREE_TYPE (t));
5594 : op1 = t;
5595 : }
5596 : }
5597 :
5598 153355109 : orig_type0 = type0 = TREE_TYPE (op0);
5599 153355109 : orig_type1 = type1 = TREE_TYPE (op1);
5600 153355109 : tree non_ep_op0 = op0;
5601 153355109 : tree non_ep_op1 = op1;
5602 :
5603 : /* The expression codes of the data types of the arguments tell us
5604 : whether the arguments are integers, floating, pointers, etc. */
5605 153355109 : code0 = TREE_CODE (type0);
5606 153355109 : code1 = TREE_CODE (type1);
5607 :
5608 : /* If an error was already reported for one of the arguments,
5609 : avoid reporting another error. */
5610 153355109 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5611 101 : return error_mark_node;
5612 :
5613 306710016 : if ((invalid_op_diag
5614 153355008 : = targetm.invalid_binary_op (code, type0, type1)))
5615 : {
5616 0 : if (complain & tf_error)
5617 : {
5618 0 : if (code0 == REAL_TYPE
5619 0 : && code1 == REAL_TYPE
5620 0 : && (extended_float_type_p (type0)
5621 0 : || extended_float_type_p (type1))
5622 0 : && cp_compare_floating_point_conversion_ranks (type0,
5623 : type1) == 3)
5624 : {
5625 0 : rich_location richloc (line_table, location);
5626 0 : binary_op_error (&richloc, code, type0, type1);
5627 0 : }
5628 : else
5629 0 : error (invalid_op_diag);
5630 : }
5631 0 : return error_mark_node;
5632 : }
5633 :
5634 153355008 : switch (code)
5635 : {
5636 : case PLUS_EXPR:
5637 : case MINUS_EXPR:
5638 : case MULT_EXPR:
5639 : case TRUNC_DIV_EXPR:
5640 : case CEIL_DIV_EXPR:
5641 : case FLOOR_DIV_EXPR:
5642 : case ROUND_DIV_EXPR:
5643 : case EXACT_DIV_EXPR:
5644 : may_need_excess_precision = true;
5645 : break;
5646 54485542 : case EQ_EXPR:
5647 54485542 : case NE_EXPR:
5648 54485542 : case LE_EXPR:
5649 54485542 : case GE_EXPR:
5650 54485542 : case LT_EXPR:
5651 54485542 : case GT_EXPR:
5652 54485542 : case SPACESHIP_EXPR:
5653 : /* Excess precision for implicit conversions of integers to
5654 : floating point. */
5655 11571200 : may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5656 66049191 : || ANY_INTEGRAL_TYPE_P (type1));
5657 : break;
5658 : default:
5659 153355008 : may_need_excess_precision = false;
5660 : break;
5661 : }
5662 153355008 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5663 : {
5664 15185 : op0 = TREE_OPERAND (op0, 0);
5665 15185 : type0 = TREE_TYPE (op0);
5666 : }
5667 153339823 : else if (may_need_excess_precision
5668 115019104 : && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5669 27567044 : if (tree eptype = excess_precision_type (type0))
5670 : {
5671 40680 : type0 = eptype;
5672 40680 : op0 = convert (eptype, op0);
5673 : }
5674 153355008 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5675 : {
5676 19193 : op1 = TREE_OPERAND (op1, 0);
5677 19193 : type1 = TREE_TYPE (op1);
5678 : }
5679 153335815 : else if (may_need_excess_precision
5680 115017338 : && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5681 27441804 : if (tree eptype = excess_precision_type (type1))
5682 : {
5683 27070 : type1 = eptype;
5684 27070 : op1 = convert (eptype, op1);
5685 : }
5686 :
5687 : /* Issue warnings about peculiar, but valid, uses of NULL. */
5688 306709931 : if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5689 : /* It's reasonable to use pointer values as operands of &&
5690 : and ||, so NULL is no exception. */
5691 12228 : && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5692 12213 : && ( /* Both are NULL (or 0) and the operation was not a
5693 : comparison or a pointer subtraction. */
5694 12286 : (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5695 27 : && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5696 : /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5697 12201 : || (!null_ptr_cst_p (orig_op0)
5698 12140 : && !TYPE_PTR_OR_PTRMEM_P (type0))
5699 12189 : || (!null_ptr_cst_p (orig_op1)
5700 46 : && !TYPE_PTR_OR_PTRMEM_P (type1)))
5701 153355047 : && (complain & tf_warning))
5702 : {
5703 39 : location_t loc =
5704 39 : expansion_point_location_if_in_system_header (input_location);
5705 :
5706 39 : warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5707 : }
5708 :
5709 : /* In case when one of the operands of the binary operation is
5710 : a vector and another is a scalar -- convert scalar to vector. */
5711 153423576 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5712 153422441 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5713 : {
5714 1220 : enum stv_conv convert_flag
5715 1220 : = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5716 : complain & tf_error);
5717 :
5718 1220 : switch (convert_flag)
5719 : {
5720 21 : case stv_error:
5721 21 : return error_mark_node;
5722 55 : case stv_firstarg:
5723 55 : {
5724 55 : op0 = convert (TREE_TYPE (type1), op0);
5725 55 : op0 = cp_save_expr (op0);
5726 55 : op0 = build_vector_from_val (type1, op0);
5727 55 : orig_type0 = type0 = TREE_TYPE (op0);
5728 55 : code0 = TREE_CODE (type0);
5729 55 : converted = 1;
5730 55 : break;
5731 : }
5732 1007 : case stv_secondarg:
5733 1007 : {
5734 1007 : op1 = convert (TREE_TYPE (type0), op1);
5735 1007 : op1 = cp_save_expr (op1);
5736 1007 : op1 = build_vector_from_val (type0, op1);
5737 1007 : orig_type1 = type1 = TREE_TYPE (op1);
5738 1007 : code1 = TREE_CODE (type1);
5739 1007 : converted = 1;
5740 1007 : break;
5741 : }
5742 : default:
5743 : break;
5744 : }
5745 : }
5746 :
5747 153354987 : switch (code)
5748 : {
5749 16705374 : case MINUS_EXPR:
5750 : /* Subtraction of two similar pointers.
5751 : We must subtract them as integers, then divide by object size. */
5752 16705374 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5753 18260216 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5754 1554842 : TREE_TYPE (type1)))
5755 : {
5756 1554839 : result = pointer_diff (location, op0, op1,
5757 : common_pointer_type (type0, type1), complain,
5758 : &instrument_expr);
5759 1554839 : if (instrument_expr != NULL)
5760 84 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5761 : instrument_expr, result);
5762 :
5763 : return result;
5764 : }
5765 : /* In all other cases except pointer - int, the usual arithmetic
5766 : rules apply. */
5767 15150535 : else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5768 : {
5769 : common = 1;
5770 : break;
5771 : }
5772 : /* The pointer - int case is just like pointer + int; fall
5773 : through. */
5774 22380194 : gcc_fallthrough ();
5775 22380194 : case PLUS_EXPR:
5776 22380194 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5777 8066785 : && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5778 : {
5779 8066776 : tree ptr_operand;
5780 8066776 : tree int_operand;
5781 8066776 : ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5782 128767 : int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5783 8066776 : if (processing_template_decl)
5784 : {
5785 2242346 : result_type = TREE_TYPE (ptr_operand);
5786 2242346 : break;
5787 : }
5788 5824430 : return cp_pointer_int_sum (location, code,
5789 : ptr_operand,
5790 : int_operand,
5791 5824430 : complain);
5792 : }
5793 : common = 1;
5794 : break;
5795 :
5796 : case MULT_EXPR:
5797 145928358 : common = 1;
5798 : break;
5799 :
5800 12385616 : case TRUNC_DIV_EXPR:
5801 12385616 : case CEIL_DIV_EXPR:
5802 12385616 : case FLOOR_DIV_EXPR:
5803 12385616 : case ROUND_DIV_EXPR:
5804 12385616 : case EXACT_DIV_EXPR:
5805 12385616 : if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5806 : {
5807 128424 : tree type0 = TREE_OPERAND (op0, 0);
5808 128424 : tree type1 = TREE_OPERAND (op1, 0);
5809 128424 : tree first_arg = tree_strip_any_location_wrapper (type0);
5810 128424 : if (!TYPE_P (type0))
5811 86056 : type0 = TREE_TYPE (type0);
5812 128424 : if (!TYPE_P (type1))
5813 50217 : type1 = TREE_TYPE (type1);
5814 128424 : if (type0
5815 128424 : && type1
5816 108829 : && INDIRECT_TYPE_P (type0)
5817 128499 : && same_type_p (TREE_TYPE (type0), type1))
5818 : {
5819 27 : if (!(TREE_CODE (first_arg) == PARM_DECL
5820 21 : && DECL_ARRAY_PARAMETER_P (first_arg)
5821 3 : && warn_sizeof_array_argument)
5822 42 : && (complain & tf_warning))
5823 : {
5824 21 : auto_diagnostic_group d;
5825 21 : if (warning_at (location, OPT_Wsizeof_pointer_div,
5826 : "division %<sizeof (%T) / sizeof (%T)%> does "
5827 : "not compute the number of array elements",
5828 : type0, type1))
5829 18 : if (DECL_P (first_arg))
5830 18 : inform (DECL_SOURCE_LOCATION (first_arg),
5831 : "first %<sizeof%> operand was declared here");
5832 21 : }
5833 : }
5834 128400 : else if (!dependent_type_p (type0)
5835 30725 : && !dependent_type_p (type1)
5836 30625 : && TREE_CODE (type0) == ARRAY_TYPE
5837 21605 : && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5838 : /* Set by finish_parenthesized_expr. */
5839 20954 : && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5840 149336 : && (complain & tf_warning))
5841 20936 : maybe_warn_sizeof_array_div (location, first_arg, type0,
5842 : op1, non_reference (type1));
5843 : }
5844 :
5845 12385616 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5846 : || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5847 12385598 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5848 : || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5849 : {
5850 12385589 : enum tree_code tcode0 = code0, tcode1 = code1;
5851 12385589 : doing_div_or_mod = true;
5852 12385589 : warn_for_div_by_zero (location, fold_for_warn (op1));
5853 :
5854 12385589 : if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5855 58810 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5856 12385589 : if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5857 30264 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5858 :
5859 12385592 : if (!((tcode0 == INTEGER_TYPE
5860 5417178 : || (tcode0 == ENUMERAL_TYPE && code0 == VECTOR_TYPE))
5861 : && (tcode1 == INTEGER_TYPE
5862 97018 : || (tcode1 == ENUMERAL_TYPE && code1 == VECTOR_TYPE))))
5863 : resultcode = RDIV_EXPR;
5864 : else
5865 : {
5866 : /* When dividing two signed integers, we have to promote to int.
5867 : unless we divide by a constant != -1. Note that default
5868 : conversion will have been performed on the operands at this
5869 : point, so we have to dig out the original type to find out if
5870 : it was unsigned. */
5871 6871399 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5872 6871399 : shorten = may_shorten_divmod (op0, stripped_op1);
5873 : }
5874 :
5875 : common = 1;
5876 : }
5877 : break;
5878 :
5879 2952193 : case BIT_AND_EXPR:
5880 2952193 : case BIT_IOR_EXPR:
5881 2952193 : case BIT_XOR_EXPR:
5882 2952193 : if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5883 2952193 : || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5884 21143 : && !VECTOR_FLOAT_TYPE_P (type0)
5885 21125 : && !VECTOR_FLOAT_TYPE_P (type1)))
5886 : shorten = -1;
5887 : break;
5888 :
5889 1576498 : case TRUNC_MOD_EXPR:
5890 1576498 : case FLOOR_MOD_EXPR:
5891 1576498 : doing_div_or_mod = true;
5892 1576498 : warn_for_div_by_zero (location, fold_for_warn (op1));
5893 :
5894 1576498 : if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5895 36 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5896 1576534 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5897 : common = 1;
5898 1576462 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5899 : {
5900 : /* Although it would be tempting to shorten always here, that loses
5901 : on some targets, since the modulo instruction is undefined if the
5902 : quotient can't be represented in the computation mode. We shorten
5903 : only if unsigned or if dividing by something we know != -1. */
5904 1576438 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5905 1576438 : shorten = may_shorten_divmod (op0, stripped_op1);
5906 1576438 : common = 1;
5907 : }
5908 : break;
5909 :
5910 18282498 : case TRUTH_ANDIF_EXPR:
5911 18282498 : case TRUTH_ORIF_EXPR:
5912 18282498 : case TRUTH_AND_EXPR:
5913 18282498 : case TRUTH_OR_EXPR:
5914 18282498 : if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5915 : {
5916 3 : if (!COMPARISON_CLASS_P (op1))
5917 3 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5918 : build_zero_cst (type1), complain);
5919 3 : if (code == TRUTH_ANDIF_EXPR)
5920 : {
5921 0 : tree z = build_zero_cst (TREE_TYPE (op1));
5922 0 : return build_conditional_expr (location, op0, op1, z, complain);
5923 : }
5924 3 : else if (code == TRUTH_ORIF_EXPR)
5925 : {
5926 3 : tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5927 3 : return build_conditional_expr (location, op0, m1, op1, complain);
5928 : }
5929 : else
5930 0 : gcc_unreachable ();
5931 : }
5932 18282495 : if (gnu_vector_type_p (type0)
5933 18282495 : && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5934 : {
5935 24 : if (!COMPARISON_CLASS_P (op0))
5936 24 : op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5937 : build_zero_cst (type0), complain);
5938 24 : if (!VECTOR_TYPE_P (type1))
5939 : {
5940 9 : tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5941 9 : tree z = build_zero_cst (TREE_TYPE (op0));
5942 9 : op1 = build_conditional_expr (location, op1, m1, z, complain);
5943 : }
5944 15 : else if (!COMPARISON_CLASS_P (op1))
5945 15 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5946 : build_zero_cst (type1), complain);
5947 :
5948 24 : if (code == TRUTH_ANDIF_EXPR)
5949 : code = BIT_AND_EXPR;
5950 9 : else if (code == TRUTH_ORIF_EXPR)
5951 : code = BIT_IOR_EXPR;
5952 : else
5953 0 : gcc_unreachable ();
5954 :
5955 24 : return cp_build_binary_op (location, code, op0, op1, complain);
5956 : }
5957 :
5958 18282471 : if (warn_constant_logical_operand
5959 135698 : && (complain & tf_warning)
5960 133232 : && (code0 == INTEGER_TYPE || code0 == ENUMERAL_TYPE)
5961 11363 : && (code1 == INTEGER_TYPE || code1 == ENUMERAL_TYPE))
5962 : {
5963 5030 : tree cop0 = fold_for_warn (op0), cop1 = fold_for_warn (op1);
5964 5030 : const char *name
5965 : = ((code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
5966 : ? "&&" : "||");
5967 5115 : auto enum_other_than_0_1 = [] (tree type) {
5968 85 : if (TREE_CODE (type) != ENUMERAL_TYPE)
5969 : return false;
5970 154 : for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l))
5971 : {
5972 125 : tree v = DECL_INITIAL (TREE_VALUE (l));
5973 125 : if (!integer_zerop (v) && !integer_onep (v))
5974 : return true;
5975 : }
5976 : return false;
5977 : };
5978 15042 : auto diagnose_constant_logical_operand = [=] (tree val, tree type) {
5979 10012 : if (TREE_CODE (val) != INTEGER_CST || integer_zerop (val))
5980 : return false;
5981 157 : if (integer_onep (val) && !enum_other_than_0_1 (type))
5982 : return false;
5983 96 : gcc_rich_location richloc (location);
5984 96 : richloc.add_fixit_replace (name + 1);
5985 96 : auto_diagnostic_group d;
5986 96 : if (warning_at (location, OPT_Wconstant_logical_operand,
5987 : "use of logical %qs with constant operand %qE",
5988 : name, val))
5989 96 : inform (&richloc, "use %qs for bitwise operation", name + 1);
5990 96 : return true;
5991 96 : };
5992 5030 : if (!diagnose_constant_logical_operand (cop1, orig_type1))
5993 4982 : diagnose_constant_logical_operand (cop0, orig_type0);
5994 : }
5995 :
5996 18282471 : result_type = boolean_type_node;
5997 18282471 : break;
5998 :
5999 : /* Shift operations: result has same type as first operand;
6000 : always convert second operand to int.
6001 : Also set SHORT_SHIFT if shifting rightward. */
6002 :
6003 1177936 : case RSHIFT_EXPR:
6004 1177936 : if (gnu_vector_type_p (type0)
6005 202 : && code1 == INTEGER_TYPE
6006 1178001 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
6007 : {
6008 : result_type = type0;
6009 : converted = 1;
6010 : }
6011 1177871 : else if (gnu_vector_type_p (type0)
6012 137 : && gnu_vector_type_p (type1)
6013 137 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
6014 134 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
6015 1178005 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
6016 : TYPE_VECTOR_SUBPARTS (type1)))
6017 : {
6018 : result_type = type0;
6019 : converted = 1;
6020 : }
6021 1177737 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6022 : {
6023 1177710 : tree const_op1 = fold_for_warn (op1);
6024 1177710 : if (TREE_CODE (const_op1) != INTEGER_CST)
6025 152057 : const_op1 = op1;
6026 1177710 : result_type = type0;
6027 1177710 : doing_shift = true;
6028 1177710 : if (TREE_CODE (const_op1) == INTEGER_CST)
6029 : {
6030 1025653 : if (tree_int_cst_lt (const_op1, integer_zero_node))
6031 : {
6032 48 : if ((complain & tf_warning)
6033 48 : && c_inhibit_evaluation_warnings == 0)
6034 36 : warning_at (location, OPT_Wshift_count_negative,
6035 : "right shift count is negative");
6036 : }
6037 : else
6038 : {
6039 1025605 : if (!integer_zerop (const_op1))
6040 1025219 : short_shift = 1;
6041 :
6042 1025605 : if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
6043 70 : && (complain & tf_warning)
6044 1025675 : && c_inhibit_evaluation_warnings == 0)
6045 34 : warning_at (location, OPT_Wshift_count_overflow,
6046 : "right shift count >= width of type");
6047 : }
6048 : }
6049 : /* Avoid converting op1 to result_type later. */
6050 : converted = 1;
6051 : }
6052 : break;
6053 :
6054 3243486 : case LSHIFT_EXPR:
6055 3243486 : if (gnu_vector_type_p (type0)
6056 271 : && code1 == INTEGER_TYPE
6057 3243537 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
6058 : {
6059 : result_type = type0;
6060 : converted = 1;
6061 : }
6062 3243435 : else if (gnu_vector_type_p (type0)
6063 220 : && gnu_vector_type_p (type1)
6064 220 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
6065 217 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
6066 3243649 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
6067 : TYPE_VECTOR_SUBPARTS (type1)))
6068 : {
6069 : result_type = type0;
6070 : converted = 1;
6071 : }
6072 3243221 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6073 : {
6074 3243121 : tree const_op0 = fold_for_warn (op0);
6075 3243121 : if (TREE_CODE (const_op0) != INTEGER_CST)
6076 313409 : const_op0 = op0;
6077 3243121 : tree const_op1 = fold_for_warn (op1);
6078 3243121 : if (TREE_CODE (const_op1) != INTEGER_CST)
6079 277187 : const_op1 = op1;
6080 3243121 : result_type = type0;
6081 3243121 : doing_shift = true;
6082 3243121 : if (TREE_CODE (const_op0) == INTEGER_CST
6083 2929712 : && tree_int_cst_sgn (const_op0) < 0
6084 352 : && !TYPE_OVERFLOW_WRAPS (type0)
6085 268 : && (complain & tf_warning)
6086 3243389 : && c_inhibit_evaluation_warnings == 0)
6087 196 : warning_at (location, OPT_Wshift_negative_value,
6088 : "left shift of negative value");
6089 3243121 : if (TREE_CODE (const_op1) == INTEGER_CST)
6090 : {
6091 2965934 : if (tree_int_cst_lt (const_op1, integer_zero_node))
6092 : {
6093 57 : if ((complain & tf_warning)
6094 57 : && c_inhibit_evaluation_warnings == 0)
6095 39 : warning_at (location, OPT_Wshift_count_negative,
6096 : "left shift count is negative");
6097 : }
6098 2965877 : else if (compare_tree_int (const_op1,
6099 2965877 : TYPE_PRECISION (type0)) >= 0)
6100 : {
6101 178 : if ((complain & tf_warning)
6102 156 : && c_inhibit_evaluation_warnings == 0)
6103 45 : warning_at (location, OPT_Wshift_count_overflow,
6104 : "left shift count >= width of type");
6105 : }
6106 2965699 : else if (TREE_CODE (const_op0) == INTEGER_CST
6107 2720071 : && (complain & tf_warning))
6108 2609606 : maybe_warn_shift_overflow (location, const_op0, const_op1);
6109 : }
6110 : /* Avoid converting op1 to result_type later. */
6111 : converted = 1;
6112 : }
6113 : break;
6114 :
6115 27378399 : case EQ_EXPR:
6116 27378399 : case NE_EXPR:
6117 27378399 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6118 3336 : goto vector_compare;
6119 27375063 : if ((complain & tf_warning)
6120 24344194 : && c_inhibit_evaluation_warnings == 0
6121 50558292 : && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
6122 800375 : warning_at (location, OPT_Wfloat_equal,
6123 : "comparing floating-point with %<==%> "
6124 : "or %<!=%> is unsafe");
6125 27375063 : {
6126 27375063 : tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
6127 27375063 : tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
6128 27375063 : if ((complain & tf_warning_or_error)
6129 27375063 : && ((TREE_CODE (stripped_orig_op0) == STRING_CST
6130 39 : && !integer_zerop (cp_fully_fold (op1)))
6131 24847417 : || (TREE_CODE (stripped_orig_op1) == STRING_CST
6132 68 : && !integer_zerop (cp_fully_fold (op0)))))
6133 47 : warning_at (location, OPT_Waddress,
6134 : "comparison with string literal results in "
6135 : "unspecified behavior");
6136 27375016 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6137 27375016 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
6138 : {
6139 : /* P2865R5 made array comparisons ill-formed in C++26. */
6140 68 : if (complain & tf_warning_or_error)
6141 45 : do_warn_array_compare (location, code, stripped_orig_op0,
6142 : stripped_orig_op1);
6143 23 : else if (cxx_dialect >= cxx26)
6144 1 : return error_mark_node;
6145 : }
6146 : }
6147 :
6148 27375062 : build_type = boolean_type_node;
6149 27375062 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6150 : || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
6151 23113588 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6152 : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
6153 : short_compare = 1;
6154 45789 : else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
6155 4256260 : && null_ptr_cst_p (orig_op1))
6156 : /* Handle, eg, (void*)0 (c++/43906), and more. */
6157 6794040 : || (code0 == POINTER_TYPE
6158 2447604 : && TYPE_PTR_P (type1) && integer_zerop (op1)))
6159 : {
6160 1827473 : if (TYPE_PTR_P (type1))
6161 19225 : result_type = composite_pointer_type (location,
6162 : type0, type1, op0, op1,
6163 : CPO_COMPARISON, complain);
6164 : else
6165 : result_type = type0;
6166 :
6167 1827473 : if (char_type_p (TREE_TYPE (orig_op1)))
6168 : {
6169 10 : auto_diagnostic_group d;
6170 10 : if (warning_at (location, OPT_Wpointer_compare,
6171 : "comparison between pointer and zero character "
6172 : "constant"))
6173 10 : inform (location,
6174 : "did you mean to dereference the pointer?");
6175 10 : }
6176 1827473 : warn_for_null_address (location, op0, complain);
6177 : }
6178 5692 : else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
6179 2468411 : && null_ptr_cst_p (orig_op0))
6180 : /* Handle, eg, (void*)0 (c++/43906), and more. */
6181 4946449 : || (code1 == POINTER_TYPE
6182 2467110 : && TYPE_PTR_P (type0) && integer_zerop (op0)))
6183 : {
6184 961 : if (TYPE_PTR_P (type0))
6185 68 : result_type = composite_pointer_type (location,
6186 : type0, type1, op0, op1,
6187 : CPO_COMPARISON, complain);
6188 : else
6189 : result_type = type1;
6190 :
6191 961 : if (char_type_p (TREE_TYPE (orig_op0)))
6192 : {
6193 10 : auto_diagnostic_group d;
6194 10 : if (warning_at (location, OPT_Wpointer_compare,
6195 : "comparison between pointer and zero character "
6196 : "constant"))
6197 10 : inform (location,
6198 : "did you mean to dereference the pointer?");
6199 10 : }
6200 961 : warn_for_null_address (location, op1, complain);
6201 : }
6202 2472710 : else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6203 44457 : || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
6204 2428661 : result_type = composite_pointer_type (location,
6205 : type0, type1, op0, op1,
6206 : CPO_COMPARISON, complain);
6207 44049 : else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
6208 : /* One of the operands must be of nullptr_t type. */
6209 69 : result_type = TREE_TYPE (nullptr_node);
6210 43980 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6211 : {
6212 58 : result_type = type0;
6213 58 : if (complain & tf_error)
6214 56 : permerror (location, "ISO C++ forbids comparison between "
6215 : "pointer and integer");
6216 : else
6217 2 : return error_mark_node;
6218 : }
6219 43922 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6220 : {
6221 38789 : result_type = type1;
6222 38789 : if (complain & tf_error)
6223 69 : permerror (location, "ISO C++ forbids comparison between "
6224 : "pointer and integer");
6225 : else
6226 38720 : return error_mark_node;
6227 : }
6228 5133 : else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
6229 : {
6230 249 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6231 : == ptrmemfunc_vbit_in_delta)
6232 : {
6233 : tree pfn0, delta0, e1, e2;
6234 :
6235 : if (TREE_SIDE_EFFECTS (op0))
6236 : op0 = cp_save_expr (op0);
6237 :
6238 : pfn0 = pfn_from_ptrmemfunc (op0);
6239 : delta0 = delta_from_ptrmemfunc (op0);
6240 : {
6241 : /* If we will warn below about a null-address compare
6242 : involving the orig_op0 ptrmemfunc, we'd likely also
6243 : warn about the pfn0's null-address compare, and
6244 : that would be redundant, so suppress it. */
6245 : warning_sentinel ws (warn_address);
6246 : e1 = cp_build_binary_op (location,
6247 : EQ_EXPR,
6248 : pfn0,
6249 : build_zero_cst (TREE_TYPE (pfn0)),
6250 : complain);
6251 : }
6252 : e2 = cp_build_binary_op (location,
6253 : BIT_AND_EXPR,
6254 : delta0,
6255 : integer_one_node,
6256 : complain);
6257 :
6258 : if (complain & tf_warning)
6259 : maybe_warn_zero_as_null_pointer_constant (op1, input_location);
6260 :
6261 : e2 = cp_build_binary_op (location,
6262 : EQ_EXPR, e2, integer_zero_node,
6263 : complain);
6264 : op0 = cp_build_binary_op (location,
6265 : TRUTH_ANDIF_EXPR, e1, e2,
6266 : complain);
6267 : op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
6268 : }
6269 : else
6270 : {
6271 249 : op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
6272 249 : op1 = cp_convert (TREE_TYPE (op0), op1, complain);
6273 : }
6274 249 : result_type = TREE_TYPE (op0);
6275 :
6276 249 : warn_for_null_address (location, orig_op0, complain);
6277 : }
6278 4884 : else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
6279 36 : return cp_build_binary_op (location, code, op1, op0, complain);
6280 4848 : else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
6281 : {
6282 145 : tree type;
6283 : /* E will be the final comparison. */
6284 145 : tree e;
6285 : /* E1 and E2 are for scratch. */
6286 145 : tree e1;
6287 145 : tree e2;
6288 145 : tree pfn0;
6289 145 : tree pfn1;
6290 145 : tree delta0;
6291 145 : tree delta1;
6292 :
6293 145 : type = composite_pointer_type (location, type0, type1, op0, op1,
6294 : CPO_COMPARISON, complain);
6295 :
6296 145 : if (!same_type_p (TREE_TYPE (op0), type))
6297 6 : op0 = cp_convert_and_check (type, op0, complain);
6298 145 : if (!same_type_p (TREE_TYPE (op1), type))
6299 9 : op1 = cp_convert_and_check (type, op1, complain);
6300 :
6301 145 : if (op0 == error_mark_node || op1 == error_mark_node)
6302 : return error_mark_node;
6303 :
6304 142 : if (TREE_SIDE_EFFECTS (op0))
6305 79 : op0 = cp_save_expr (op0);
6306 142 : if (TREE_SIDE_EFFECTS (op1))
6307 3 : op1 = cp_save_expr (op1);
6308 :
6309 142 : pfn0 = pfn_from_ptrmemfunc (op0);
6310 142 : pfn0 = cp_fully_fold (pfn0);
6311 : /* Avoid -Waddress warnings (c++/64877). */
6312 142 : if (TREE_CODE (pfn0) == ADDR_EXPR)
6313 20 : suppress_warning (pfn0, OPT_Waddress);
6314 142 : pfn1 = pfn_from_ptrmemfunc (op1);
6315 142 : pfn1 = cp_fully_fold (pfn1);
6316 142 : delta0 = delta_from_ptrmemfunc (op0);
6317 142 : delta1 = delta_from_ptrmemfunc (op1);
6318 142 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6319 : == ptrmemfunc_vbit_in_delta)
6320 : {
6321 : /* We generate:
6322 :
6323 : (op0.pfn == op1.pfn
6324 : && ((op0.delta == op1.delta)
6325 : || (!op0.pfn && op0.delta & 1 == 0
6326 : && op1.delta & 1 == 0))
6327 :
6328 : The reason for the `!op0.pfn' bit is that a NULL
6329 : pointer-to-member is any member with a zero PFN and
6330 : LSB of the DELTA field is 0. */
6331 :
6332 : e1 = cp_build_binary_op (location, BIT_AND_EXPR,
6333 : delta0,
6334 : integer_one_node,
6335 : complain);
6336 : e1 = cp_build_binary_op (location,
6337 : EQ_EXPR, e1, integer_zero_node,
6338 : complain);
6339 : e2 = cp_build_binary_op (location, BIT_AND_EXPR,
6340 : delta1,
6341 : integer_one_node,
6342 : complain);
6343 : e2 = cp_build_binary_op (location,
6344 : EQ_EXPR, e2, integer_zero_node,
6345 : complain);
6346 : e1 = cp_build_binary_op (location,
6347 : TRUTH_ANDIF_EXPR, e2, e1,
6348 : complain);
6349 : e2 = cp_build_binary_op (location, EQ_EXPR,
6350 : pfn0,
6351 : build_zero_cst (TREE_TYPE (pfn0)),
6352 : complain);
6353 : e2 = cp_build_binary_op (location,
6354 : TRUTH_ANDIF_EXPR, e2, e1, complain);
6355 : e1 = cp_build_binary_op (location,
6356 : EQ_EXPR, delta0, delta1, complain);
6357 : e1 = cp_build_binary_op (location,
6358 : TRUTH_ORIF_EXPR, e1, e2, complain);
6359 : }
6360 : else
6361 : {
6362 : /* We generate:
6363 :
6364 : (op0.pfn == op1.pfn
6365 : && (!op0.pfn || op0.delta == op1.delta))
6366 :
6367 : The reason for the `!op0.pfn' bit is that a NULL
6368 : pointer-to-member is any member with a zero PFN; the
6369 : DELTA field is unspecified. */
6370 :
6371 142 : e1 = cp_build_binary_op (location,
6372 : EQ_EXPR, delta0, delta1, complain);
6373 142 : e2 = cp_build_binary_op (location,
6374 : EQ_EXPR,
6375 : pfn0,
6376 142 : build_zero_cst (TREE_TYPE (pfn0)),
6377 : complain);
6378 142 : e1 = cp_build_binary_op (location,
6379 : TRUTH_ORIF_EXPR, e1, e2, complain);
6380 : }
6381 142 : e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
6382 142 : e = cp_build_binary_op (location,
6383 : TRUTH_ANDIF_EXPR, e2, e1, complain);
6384 142 : if (code == EQ_EXPR)
6385 : return e;
6386 34 : return cp_build_binary_op (location,
6387 34 : EQ_EXPR, e, integer_zero_node, complain);
6388 : }
6389 : /* [expr.eq]: "If both operands are of type std::meta::info,
6390 : comparison is defined as follows..." */
6391 4703 : else if (REFLECTION_TYPE_P (type0) && REFLECTION_TYPE_P (type1))
6392 : result_type = type0;
6393 : else
6394 : {
6395 28 : gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
6396 : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
6397 : type1));
6398 28 : gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
6399 : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
6400 : type0));
6401 : }
6402 :
6403 : break;
6404 :
6405 241 : case MAX_EXPR:
6406 241 : case MIN_EXPR:
6407 241 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6408 241 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6409 : shorten = 1;
6410 0 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6411 0 : result_type = composite_pointer_type (location,
6412 : type0, type1, op0, op1,
6413 : CPO_COMPARISON, complain);
6414 : break;
6415 :
6416 27107143 : case LE_EXPR:
6417 27107143 : case GE_EXPR:
6418 27107143 : case LT_EXPR:
6419 27107143 : case GT_EXPR:
6420 27107143 : case SPACESHIP_EXPR:
6421 27107143 : if (TREE_CODE (orig_op0) == STRING_CST
6422 27107143 : || TREE_CODE (orig_op1) == STRING_CST)
6423 : {
6424 0 : if (complain & tf_warning)
6425 0 : warning_at (location, OPT_Waddress,
6426 : "comparison with string literal results "
6427 : "in unspecified behavior");
6428 : }
6429 27107143 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6430 224 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6431 27107258 : && code != SPACESHIP_EXPR)
6432 : {
6433 96 : if (complain & tf_warning_or_error)
6434 51 : do_warn_array_compare (location, code,
6435 : tree_strip_any_location_wrapper (orig_op0),
6436 : tree_strip_any_location_wrapper (orig_op1));
6437 45 : else if (cxx_dialect >= cxx26)
6438 1 : return error_mark_node;
6439 : }
6440 :
6441 27107142 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6442 : {
6443 8238 : vector_compare:
6444 8238 : tree intt;
6445 8238 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6446 8238 : TREE_TYPE (type1))
6447 8238 : && !vector_types_compatible_elements_p (type0, type1))
6448 : {
6449 3 : if (complain & tf_error)
6450 : {
6451 3 : auto_diagnostic_group d;
6452 3 : error_at (location, "comparing vectors with different "
6453 : "element types");
6454 3 : inform (location, "operand types are %qT and %qT",
6455 : type0, type1);
6456 3 : }
6457 3 : return error_mark_node;
6458 : }
6459 :
6460 8235 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
6461 16470 : TYPE_VECTOR_SUBPARTS (type1)))
6462 : {
6463 3 : if (complain & tf_error)
6464 : {
6465 3 : auto_diagnostic_group d;
6466 3 : error_at (location, "comparing vectors with different "
6467 : "number of elements");
6468 3 : inform (location, "operand types are %qT and %qT",
6469 : type0, type1);
6470 3 : }
6471 3 : return error_mark_node;
6472 : }
6473 :
6474 : /* It's not precisely specified how the usual arithmetic
6475 : conversions apply to the vector types. Here, we use
6476 : the unsigned type if one of the operands is signed and
6477 : the other one is unsigned. */
6478 8232 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6479 : {
6480 36 : if (!TYPE_UNSIGNED (type0))
6481 36 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6482 : else
6483 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6484 36 : warning_at (location, OPT_Wsign_compare, "comparison between "
6485 : "types %qT and %qT", type0, type1);
6486 : }
6487 :
6488 8232 : if (resultcode == SPACESHIP_EXPR)
6489 : {
6490 3 : if (complain & tf_error)
6491 3 : sorry_at (location, "three-way comparison of vectors");
6492 3 : return error_mark_node;
6493 : }
6494 :
6495 8229 : if (VECTOR_BOOLEAN_TYPE_P (type0) && VECTOR_BOOLEAN_TYPE_P (type1))
6496 : result_type = type0;
6497 : else
6498 : {
6499 : /* Always construct signed integer vector type. */
6500 8229 : auto intmode = SCALAR_TYPE_MODE (TREE_TYPE (type0));
6501 8229 : auto nelts = TYPE_VECTOR_SUBPARTS (type0);
6502 :
6503 16458 : intt = c_common_type_for_size (GET_MODE_BITSIZE (intmode), 0);
6504 8229 : if (!intt)
6505 : {
6506 0 : if (complain & tf_error)
6507 0 : error_at (location, "could not find an integer type "
6508 0 : "of the same size as %qT", TREE_TYPE (type0));
6509 0 : return error_mark_node;
6510 : }
6511 8229 : result_type = build_opaque_vector_type (intt, nelts);
6512 : }
6513 8229 : return build_vec_cmp (resultcode, result_type, op0, op1);
6514 : }
6515 27102240 : build_type = boolean_type_node;
6516 27102240 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6517 : || code0 == ENUMERAL_TYPE)
6518 24388221 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6519 : || code1 == ENUMERAL_TYPE))
6520 : short_compare = 1;
6521 2714122 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6522 2713842 : result_type = composite_pointer_type (location,
6523 : type0, type1, op0, op1,
6524 : CPO_COMPARISON, complain);
6525 73 : else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6526 208 : || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6527 434 : || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6528 : {
6529 : /* Core Issue 1512 made this ill-formed. */
6530 190 : if (complain & tf_error)
6531 186 : error_at (location, "ordered comparison of pointer with "
6532 : "integer zero (%qT and %qT)", type0, type1);
6533 190 : return error_mark_node;
6534 : }
6535 90 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6536 : {
6537 0 : result_type = type0;
6538 0 : if (complain & tf_error)
6539 0 : permerror (location, "ISO C++ forbids comparison between "
6540 : "pointer and integer");
6541 : else
6542 0 : return error_mark_node;
6543 : }
6544 90 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6545 : {
6546 24 : result_type = type1;
6547 24 : if (complain & tf_error)
6548 24 : permerror (location, "ISO C++ forbids comparison between "
6549 : "pointer and integer");
6550 : else
6551 0 : return error_mark_node;
6552 : }
6553 :
6554 27102050 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6555 2713867 : && !processing_template_decl
6556 29595586 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6557 : {
6558 49 : op0 = cp_save_expr (op0);
6559 49 : op1 = cp_save_expr (op1);
6560 :
6561 49 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6562 49 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6563 : }
6564 :
6565 : break;
6566 :
6567 0 : case UNORDERED_EXPR:
6568 0 : case ORDERED_EXPR:
6569 0 : case UNLT_EXPR:
6570 0 : case UNLE_EXPR:
6571 0 : case UNGT_EXPR:
6572 0 : case UNGE_EXPR:
6573 0 : case UNEQ_EXPR:
6574 0 : build_type = integer_type_node;
6575 0 : if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6576 : {
6577 0 : if (complain & tf_error)
6578 0 : error ("unordered comparison on non-floating-point argument");
6579 0 : return error_mark_node;
6580 : }
6581 : common = 1;
6582 : break;
6583 :
6584 : default:
6585 : break;
6586 : }
6587 :
6588 145928358 : if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6589 : || code0 == ENUMERAL_TYPE)
6590 118926707 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6591 : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6592 : arithmetic_types_p = 1;
6593 : else
6594 : {
6595 27398093 : arithmetic_types_p = 0;
6596 : /* Vector arithmetic is only allowed when both sides are vectors. */
6597 27398093 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6598 : {
6599 60242 : if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6600 60242 : || !vector_types_compatible_elements_p (type0, type1))
6601 : {
6602 18 : if (complain & tf_error)
6603 : {
6604 : /* "location" already embeds the locations of the
6605 : operands, so we don't need to add them separately
6606 : to richloc. */
6607 18 : rich_location richloc (line_table, location);
6608 18 : binary_op_error (&richloc, code, type0, type1);
6609 18 : }
6610 18 : return error_mark_node;
6611 : }
6612 : arithmetic_types_p = 1;
6613 : }
6614 : }
6615 : /* Determine the RESULT_TYPE, if it is not already known. */
6616 145928340 : if (!result_type
6617 145928340 : && arithmetic_types_p
6618 114005757 : && (shorten || common || short_compare))
6619 : {
6620 114005610 : result_type = cp_common_type (type0, type1);
6621 114005610 : if (result_type == error_mark_node)
6622 : {
6623 6 : tree t1 = type0;
6624 6 : tree t2 = type1;
6625 6 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6626 0 : t1 = TREE_TYPE (t1);
6627 6 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6628 0 : t2 = TREE_TYPE (t2);
6629 6 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6630 : && TREE_CODE (t2) == REAL_TYPE
6631 : && (extended_float_type_p (t1)
6632 : || extended_float_type_p (t2))
6633 : && cp_compare_floating_point_conversion_ranks
6634 : (t1, t2) == 3);
6635 6 : if (complain & tf_error)
6636 : {
6637 6 : rich_location richloc (line_table, location);
6638 6 : binary_op_error (&richloc, code, type0, type1);
6639 6 : }
6640 6 : return error_mark_node;
6641 : }
6642 114005604 : if (complain & tf_warning)
6643 103922658 : do_warn_double_promotion (result_type, type0, type1,
6644 : "implicit conversion from %qH to %qI "
6645 : "to match other operand of binary "
6646 : "expression", location);
6647 114005604 : if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6648 114005604 : TREE_TYPE (orig_op1), complain))
6649 12 : return error_mark_node;
6650 : }
6651 145928322 : if (may_need_excess_precision
6652 107608350 : && (orig_type0 != type0 || orig_type1 != type1)
6653 57967 : && build_type == NULL_TREE
6654 57967 : && result_type)
6655 : {
6656 44032 : gcc_assert (common);
6657 44032 : semantic_result_type = cp_common_type (orig_type0, orig_type1);
6658 44032 : if (semantic_result_type == error_mark_node)
6659 : {
6660 0 : tree t1 = orig_type0;
6661 0 : tree t2 = orig_type1;
6662 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6663 0 : t1 = TREE_TYPE (t1);
6664 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6665 0 : t2 = TREE_TYPE (t2);
6666 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6667 : && TREE_CODE (t2) == REAL_TYPE
6668 : && (extended_float_type_p (t1)
6669 : || extended_float_type_p (t2))
6670 : && cp_compare_floating_point_conversion_ranks
6671 : (t1, t2) == 3);
6672 0 : if (complain & tf_error)
6673 : {
6674 0 : rich_location richloc (line_table, location);
6675 0 : binary_op_error (&richloc, code, type0, type1);
6676 0 : }
6677 0 : return error_mark_node;
6678 : }
6679 : }
6680 :
6681 145928322 : if (code == SPACESHIP_EXPR)
6682 : {
6683 623479 : iloc_sentinel s (location);
6684 :
6685 623479 : tree orig_type0 = TREE_TYPE (orig_op0);
6686 623479 : tree_code orig_code0 = TREE_CODE (orig_type0);
6687 623479 : tree orig_type1 = TREE_TYPE (orig_op1);
6688 623479 : tree_code orig_code1 = TREE_CODE (orig_type1);
6689 623479 : if (!result_type || result_type == error_mark_node)
6690 : /* Nope. */
6691 : result_type = NULL_TREE;
6692 623458 : else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6693 : /* "If one of the operands is of type bool and the other is not, the
6694 : program is ill-formed." */
6695 : result_type = NULL_TREE;
6696 623455 : else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6697 19 : && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6698 : /* We only do array/function-to-pointer conversion if "at least one of
6699 : the operands is of pointer type". */
6700 : result_type = NULL_TREE;
6701 623436 : else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6702 : /* <=> no longer supports equality relations. */
6703 : result_type = NULL_TREE;
6704 623433 : else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6705 623463 : && !(same_type_ignoring_top_level_qualifiers_p
6706 30 : (orig_type0, orig_type1)))
6707 : /* "If both operands have arithmetic types, or one operand has integral
6708 : type and the other operand has unscoped enumeration type, the usual
6709 : arithmetic conversions are applied to the operands." So we don't do
6710 : arithmetic conversions if the operands both have enumeral type. */
6711 : result_type = NULL_TREE;
6712 623418 : else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6713 623412 : || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6714 : /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6715 : [where one is of enumeration type and the other is of a different
6716 : enumeration type or a floating-point type] are ill-formed. */
6717 : result_type = NULL_TREE;
6718 :
6719 623406 : if (result_type)
6720 : {
6721 623406 : build_type = spaceship_type (result_type, complain);
6722 623406 : if (build_type == error_mark_node)
6723 : return error_mark_node;
6724 : }
6725 :
6726 623464 : if (result_type && arithmetic_types_p)
6727 : {
6728 : /* If a narrowing conversion is required, other than from an integral
6729 : type to a floating point type, the program is ill-formed. */
6730 288543 : bool ok = true;
6731 288543 : if (TREE_CODE (result_type) == REAL_TYPE
6732 614 : && CP_INTEGRAL_TYPE_P (orig_type0))
6733 : /* OK */;
6734 288536 : else if (!check_narrowing (result_type, orig_op0, complain))
6735 288543 : ok = false;
6736 288543 : if (TREE_CODE (result_type) == REAL_TYPE
6737 614 : && CP_INTEGRAL_TYPE_P (orig_type1))
6738 : /* OK */;
6739 288536 : else if (!check_narrowing (result_type, orig_op1, complain))
6740 : ok = false;
6741 288543 : if (!ok && !(complain & tf_error))
6742 2 : return error_mark_node;
6743 : }
6744 623479 : }
6745 :
6746 145928305 : if (!result_type)
6747 : {
6748 606 : if (complain & tf_error)
6749 : {
6750 221 : binary_op_rich_location richloc (location,
6751 221 : orig_op0, orig_op1, true);
6752 221 : error_at (&richloc,
6753 : "invalid operands of types %qT and %qT to binary %qO",
6754 221 : TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6755 221 : }
6756 606 : return error_mark_node;
6757 : }
6758 :
6759 : /* If we're in a template, the only thing we need to know is the
6760 : RESULT_TYPE. */
6761 145927699 : if (processing_template_decl)
6762 : {
6763 : /* Since the middle-end checks the type when doing a build2, we
6764 : need to build the tree in pieces. This built tree will never
6765 : get out of the front-end as we replace it when instantiating
6766 : the template. */
6767 68633305 : tree tmp = build2 (resultcode,
6768 : build_type ? build_type : result_type,
6769 : NULL_TREE, op1);
6770 42931577 : TREE_OPERAND (tmp, 0) = op0;
6771 42931577 : if (semantic_result_type)
6772 72 : tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6773 : return tmp;
6774 : }
6775 :
6776 : /* Remember the original type; RESULT_TYPE might be changed later on
6777 : by shorten_binary_op. */
6778 102996122 : tree orig_type = result_type;
6779 :
6780 102996122 : if (arithmetic_types_p)
6781 : {
6782 88270575 : bool first_complex = (code0 == COMPLEX_TYPE);
6783 88270575 : bool second_complex = (code1 == COMPLEX_TYPE);
6784 88270575 : int none_complex = (!first_complex && !second_complex);
6785 :
6786 : /* Adapted from patch for c/24581. */
6787 88270575 : if (first_complex != second_complex
6788 160959 : && (code == PLUS_EXPR
6789 : || code == MINUS_EXPR
6790 160959 : || code == MULT_EXPR
6791 42059 : || (code == TRUNC_DIV_EXPR && first_complex))
6792 147452 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6793 88417866 : && flag_signed_zeros)
6794 : {
6795 : /* An operation on mixed real/complex operands must be
6796 : handled specially, but the language-independent code can
6797 : more easily optimize the plain complex arithmetic if
6798 : -fno-signed-zeros. */
6799 147291 : tree real_type = TREE_TYPE (result_type);
6800 147291 : tree real, imag;
6801 147291 : if (first_complex)
6802 : {
6803 114243 : if (TREE_TYPE (op0) != result_type)
6804 6 : op0 = cp_convert_and_check (result_type, op0, complain);
6805 114243 : if (TREE_TYPE (op1) != real_type)
6806 26 : op1 = cp_convert_and_check (real_type, op1, complain);
6807 : }
6808 : else
6809 : {
6810 33048 : if (TREE_TYPE (op0) != real_type)
6811 31 : op0 = cp_convert_and_check (real_type, op0, complain);
6812 33048 : if (TREE_TYPE (op1) != result_type)
6813 32 : op1 = cp_convert_and_check (result_type, op1, complain);
6814 : }
6815 147291 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6816 0 : return error_mark_node;
6817 147291 : if (first_complex)
6818 : {
6819 114243 : op0 = cp_save_expr (op0);
6820 114243 : real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6821 114243 : imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6822 114243 : switch (code)
6823 : {
6824 57099 : case MULT_EXPR:
6825 57099 : case TRUNC_DIV_EXPR:
6826 57099 : op1 = cp_save_expr (op1);
6827 57099 : imag = build2 (resultcode, real_type, imag, op1);
6828 : /* Fall through. */
6829 114243 : case PLUS_EXPR:
6830 114243 : case MINUS_EXPR:
6831 114243 : real = build2 (resultcode, real_type, real, op1);
6832 114243 : break;
6833 : default:
6834 : gcc_unreachable();
6835 : }
6836 : }
6837 : else
6838 : {
6839 33048 : op1 = cp_save_expr (op1);
6840 33048 : real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6841 33048 : imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6842 33048 : switch (code)
6843 : {
6844 638 : case MULT_EXPR:
6845 638 : op0 = cp_save_expr (op0);
6846 638 : imag = build2 (resultcode, real_type, op0, imag);
6847 : /* Fall through. */
6848 17301 : case PLUS_EXPR:
6849 17301 : real = build2 (resultcode, real_type, op0, real);
6850 17301 : break;
6851 15747 : case MINUS_EXPR:
6852 15747 : real = build2 (resultcode, real_type, op0, real);
6853 15747 : imag = build1 (NEGATE_EXPR, real_type, imag);
6854 15747 : break;
6855 0 : default:
6856 0 : gcc_unreachable();
6857 : }
6858 : }
6859 147291 : result = build2 (COMPLEX_EXPR, result_type, real, imag);
6860 147291 : if (semantic_result_type)
6861 26 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6862 : result);
6863 : return result;
6864 : }
6865 :
6866 : /* For certain operations (which identify themselves by shorten != 0)
6867 : if both args were extended from the same smaller type,
6868 : do the arithmetic in that type and then extend.
6869 :
6870 : shorten !=0 and !=1 indicates a bitwise operation.
6871 : For them, this optimization is safe only if
6872 : both args are zero-extended or both are sign-extended.
6873 : Otherwise, we might change the result.
6874 : E.g., (short)-1 | (unsigned short)-1 is (int)-1
6875 : but calculated in (unsigned short) it would be (unsigned short)-1. */
6876 :
6877 88123284 : if (shorten && none_complex)
6878 : {
6879 6944453 : final_type = result_type;
6880 6944453 : result_type = shorten_binary_op (result_type, op0, op1,
6881 : shorten == -1);
6882 : }
6883 :
6884 : /* Shifts can be shortened if shifting right. */
6885 :
6886 88123284 : if (short_shift)
6887 : {
6888 849478 : int unsigned_arg;
6889 849478 : tree arg0 = get_narrower (op0, &unsigned_arg);
6890 : /* We're not really warning here but when we set short_shift we
6891 : used fold_for_warn to fold the operand. */
6892 849478 : tree const_op1 = fold_for_warn (op1);
6893 :
6894 849478 : final_type = result_type;
6895 :
6896 849478 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
6897 771139 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6898 :
6899 849478 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6900 1126 : && tree_int_cst_sgn (const_op1) > 0
6901 : /* We can shorten only if the shift count is less than the
6902 : number of bits in the smaller type size. */
6903 1126 : && compare_tree_int (const_op1,
6904 1126 : TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6905 : /* We cannot drop an unsigned shift after sign-extension. */
6906 850564 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6907 : {
6908 : /* Do an unsigned shift if the operand was zero-extended. */
6909 1079 : result_type
6910 1079 : = c_common_signed_or_unsigned_type (unsigned_arg,
6911 1079 : TREE_TYPE (arg0));
6912 : /* Convert value-to-be-shifted to that type. */
6913 1079 : if (TREE_TYPE (op0) != result_type)
6914 1079 : op0 = convert (result_type, op0);
6915 : converted = 1;
6916 : }
6917 : }
6918 :
6919 : /* Comparison operations are shortened too but differently.
6920 : They identify themselves by setting short_compare = 1. */
6921 :
6922 88123284 : if (short_compare)
6923 : {
6924 : /* We call shorten_compare only for diagnostics. */
6925 30675543 : tree xop0 = fold_simple (op0);
6926 30675543 : tree xop1 = fold_simple (op1);
6927 30675543 : tree xresult_type = result_type;
6928 30675543 : enum tree_code xresultcode = resultcode;
6929 30675543 : shorten_compare (location, &xop0, &xop1, &xresult_type,
6930 : &xresultcode);
6931 : }
6932 :
6933 57447654 : if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6934 30675732 : && warn_sign_compare
6935 : /* Do not warn until the template is instantiated; we cannot
6936 : bound the ranges of the arguments until that point. */
6937 346104 : && !processing_template_decl
6938 346104 : && (complain & tf_warning)
6939 338222 : && c_inhibit_evaluation_warnings == 0
6940 : /* Even unsigned enum types promote to signed int. We don't
6941 : want to issue -Wsign-compare warnings for this case. */
6942 321802 : && !enum_cast_to_int (orig_op0)
6943 88445086 : && !enum_cast_to_int (orig_op1))
6944 : {
6945 321795 : warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6946 : result_type, resultcode);
6947 : }
6948 : }
6949 :
6950 : /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6951 : Then the expression will be built.
6952 : It will be given type FINAL_TYPE if that is nonzero;
6953 : otherwise, it will be given type RESULT_TYPE. */
6954 102848831 : if (! converted)
6955 : {
6956 99107327 : warning_sentinel w (warn_sign_conversion, short_compare);
6957 99107327 : if (!same_type_p (TREE_TYPE (op0), result_type))
6958 4413752 : op0 = cp_convert_and_check (result_type, op0, complain);
6959 99107327 : if (!same_type_p (TREE_TYPE (op1), result_type))
6960 19246935 : op1 = cp_convert_and_check (result_type, op1, complain);
6961 :
6962 99107327 : if (op0 == error_mark_node || op1 == error_mark_node)
6963 58 : return error_mark_node;
6964 99107327 : }
6965 :
6966 102848773 : if (build_type == NULL_TREE)
6967 65640629 : build_type = result_type;
6968 :
6969 102848773 : if (doing_shift
6970 3740767 : && flag_strong_eval_order == 2
6971 3651589 : && TREE_SIDE_EFFECTS (op1)
6972 102871622 : && !processing_template_decl)
6973 : {
6974 : /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6975 : op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6976 22849 : op0 = cp_save_expr (op0);
6977 22849 : instrument_expr = op0;
6978 : }
6979 :
6980 102848773 : if (sanitize_flags_p ((SANITIZE_SHIFT
6981 : | SANITIZE_DIVIDE
6982 : | SANITIZE_FLOAT_DIVIDE
6983 : | SANITIZE_SI_OVERFLOW))
6984 13814 : && current_function_decl != NULL_TREE
6985 11430 : && !processing_template_decl
6986 102860203 : && (doing_div_or_mod || doing_shift))
6987 : {
6988 : /* OP0 and/or OP1 might have side-effects. */
6989 939 : op0 = cp_save_expr (op0);
6990 939 : op1 = cp_save_expr (op1);
6991 939 : op0 = fold_non_dependent_expr (op0, complain);
6992 939 : op1 = fold_non_dependent_expr (op1, complain);
6993 939 : tree instrument_expr1 = NULL_TREE;
6994 939 : if (doing_div_or_mod
6995 939 : && sanitize_flags_p (SANITIZE_DIVIDE
6996 : | SANITIZE_FLOAT_DIVIDE
6997 : | SANITIZE_SI_OVERFLOW))
6998 : {
6999 : /* For diagnostics we want to use the promoted types without
7000 : shorten_binary_op. So convert the arguments to the
7001 : original result_type. */
7002 475 : tree cop0 = op0;
7003 475 : tree cop1 = op1;
7004 475 : if (TREE_TYPE (cop0) != orig_type)
7005 6 : cop0 = cp_convert (orig_type, op0, complain);
7006 475 : if (TREE_TYPE (cop1) != orig_type)
7007 39 : cop1 = cp_convert (orig_type, op1, complain);
7008 475 : instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
7009 : }
7010 464 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
7011 383 : instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
7012 939 : if (instrument_expr != NULL)
7013 18 : instrument_expr = add_stmt_to_compound (instrument_expr,
7014 : instrument_expr1);
7015 : else
7016 921 : instrument_expr = instrument_expr1;
7017 : }
7018 :
7019 102848773 : result = build2_loc (location, resultcode, build_type, op0, op1);
7020 102848773 : if (final_type != 0)
7021 7793931 : result = cp_convert (final_type, result, complain);
7022 :
7023 102848773 : if (instrument_expr != NULL)
7024 23347 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
7025 : instrument_expr, result);
7026 :
7027 102848773 : if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
7028 525305 : result = get_target_expr (result, complain);
7029 :
7030 102848773 : if (semantic_result_type)
7031 43934 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
7032 :
7033 102848773 : if (!c_inhibit_evaluation_warnings)
7034 : {
7035 87397730 : if (!processing_template_decl)
7036 : {
7037 87397730 : op0 = cp_fully_fold (op0);
7038 : /* Only consider the second argument if the first isn't overflowed. */
7039 87397730 : if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
7040 : return result;
7041 24615681 : op1 = cp_fully_fold (op1);
7042 24615681 : if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
7043 : return result;
7044 : }
7045 0 : else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
7046 0 : || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
7047 : return result;
7048 :
7049 19080529 : tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
7050 19080529 : if (TREE_OVERFLOW_P (result_ovl))
7051 190 : overflow_warning (location, result_ovl);
7052 : }
7053 :
7054 : return result;
7055 : }
7056 :
7057 : /* Build a VEC_PERM_EXPR.
7058 : This is a simple wrapper for c_build_vec_perm_expr. */
7059 : tree
7060 17402 : build_x_vec_perm_expr (location_t loc,
7061 : tree arg0, tree arg1, tree arg2,
7062 : tsubst_flags_t complain)
7063 : {
7064 17402 : tree orig_arg0 = arg0;
7065 17402 : tree orig_arg1 = arg1;
7066 17402 : tree orig_arg2 = arg2;
7067 17402 : if (processing_template_decl)
7068 : {
7069 19 : if (type_dependent_expression_p (arg0)
7070 13 : || type_dependent_expression_p (arg1)
7071 32 : || type_dependent_expression_p (arg2))
7072 6 : return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
7073 : }
7074 17396 : tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
7075 17396 : if (processing_template_decl && exp != error_mark_node)
7076 13 : return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
7077 13 : orig_arg1, orig_arg2);
7078 : return exp;
7079 : }
7080 :
7081 : /* Build a VEC_PERM_EXPR.
7082 : This is a simple wrapper for c_build_shufflevector. */
7083 : tree
7084 42181 : build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
7085 : tsubst_flags_t complain)
7086 : {
7087 42181 : tree arg0 = (*args)[0];
7088 42181 : tree arg1 = (*args)[1];
7089 42181 : if (processing_template_decl)
7090 : {
7091 743 : for (unsigned i = 0; i < args->length (); ++i)
7092 712 : if (i <= 1
7093 712 : ? type_dependent_expression_p ((*args)[i])
7094 121 : : instantiation_dependent_expression_p ((*args)[i]))
7095 : {
7096 526 : tree exp = build_min_nt_call_vec (NULL, args);
7097 526 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
7098 526 : return exp;
7099 : }
7100 : }
7101 41655 : auto_vec<tree, 16> mask;
7102 561159 : for (unsigned i = 2; i < args->length (); ++i)
7103 : {
7104 519504 : tree idx = fold_non_dependent_expr ((*args)[i], complain);
7105 519504 : mask.safe_push (idx);
7106 : }
7107 41655 : tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
7108 41655 : if (processing_template_decl && exp != error_mark_node)
7109 : {
7110 31 : exp = build_min_non_dep_call_vec (exp, NULL, args);
7111 31 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
7112 : }
7113 41655 : return exp;
7114 41655 : }
7115 :
7116 : /* Build __builtin_bswapg (ARG) or __builtin_bitreverseg (ARG). */
7117 : tree
7118 1536 : build_x_bswapg_bitreverseg (location_t loc, internal_fn ifn,
7119 : vec<tree, va_gc> *args, tsubst_flags_t complain)
7120 : {
7121 1446 : const char *name
7122 1536 : = ifn == IFN_BSWAP ? "__builtin_bswapg" : "__builtin_bitreverseg";
7123 1536 : if (args->length () != 1)
7124 : {
7125 18 : if (complain & tf_error)
7126 18 : error_at (loc, "wrong number of arguments to %qs", name);
7127 18 : return error_mark_node;
7128 : }
7129 1518 : tree arg = (*args)[0];
7130 1518 : if (error_operand_p (arg))
7131 6 : return error_mark_node;
7132 1512 : if (type_dependent_expression_p (arg))
7133 : {
7134 1362 : tree exp = build_min_nt_call_vec (NULL, args);
7135 1362 : CALL_EXPR_IFN (exp) = ifn;
7136 1362 : return exp;
7137 : }
7138 150 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (arg));
7139 150 : if (!INTEGRAL_TYPE_P (type))
7140 : {
7141 24 : if (complain & tf_error)
7142 24 : error_at (loc, "%qs operand not an integral type", name);
7143 24 : return error_mark_node;
7144 : }
7145 126 : if (TREE_CODE (type) == ENUMERAL_TYPE)
7146 : {
7147 30 : if (complain & tf_error)
7148 30 : error_at (loc, "argument %u in call to function "
7149 : "%qs has enumerated type", 1, name);
7150 30 : return error_mark_node;
7151 : }
7152 96 : if (TREE_CODE (type) == BOOLEAN_TYPE)
7153 : {
7154 6 : if (complain & tf_error)
7155 6 : error_at (loc, "argument %u in call to function "
7156 : "%qs has boolean type", 1, name);
7157 6 : return error_mark_node;
7158 : }
7159 90 : if (!TYPE_UNSIGNED (type))
7160 : {
7161 12 : if (complain & tf_error)
7162 12 : error_at (loc, "argument 1 in call to function "
7163 : "%qs has signed type", name);
7164 12 : return error_mark_node;
7165 : }
7166 78 : if (ifn == IFN_BSWAP && (TYPE_PRECISION (type) % 8) != 0)
7167 : {
7168 0 : if (complain & tf_error)
7169 0 : error_at (loc, "precision %d of argument 1 to function "
7170 : "%qs is not a multiple of 8",
7171 0 : TYPE_PRECISION (type), name);
7172 0 : return error_mark_node;
7173 : }
7174 78 : tree exp = build_call_expr_internal_loc (loc, ifn, type, 1, arg);
7175 78 : if (processing_template_decl)
7176 : {
7177 0 : exp = build_min_non_dep_call_vec (exp, NULL, args);
7178 0 : CALL_EXPR_IFN (exp) = ifn;
7179 : }
7180 : return exp;
7181 : }
7182 :
7183 : /* Build __builtin_clmulg (ARG1, ARG2) or
7184 : __builtin_clmulg (ARG1, ARG2, ARG3). */
7185 : tree
7186 1290 : build_x_clmulg (location_t loc, vec<tree, va_gc> *args,
7187 : tsubst_flags_t complain)
7188 : {
7189 1290 : const char *name = "__builtin_clmulg";
7190 1290 : if (args->length () < 2 || args->length () > 3)
7191 : {
7192 9 : if (complain & tf_error)
7193 9 : error_at (loc, "wrong number of arguments to %qs", name);
7194 9 : return error_mark_node;
7195 : }
7196 1281 : tree arg1 = (*args)[0];
7197 1281 : tree arg2 = (*args)[1];
7198 1281 : tree arg3 = args->length () == 3 ? (*args)[2] : NULL_TREE;
7199 1281 : if (error_operand_p (arg1)
7200 1281 : || error_operand_p (arg2)
7201 2562 : || error_operand_p (arg3))
7202 0 : return error_mark_node;
7203 1281 : if (type_dependent_expression_p (arg1)
7204 303 : || type_dependent_expression_p (arg2)
7205 1584 : || type_dependent_expression_p (arg3))
7206 : {
7207 978 : tree exp = build_min_nt_call_vec (NULL, args);
7208 978 : CALL_EXPR_IFN (exp) = IFN_CLMUL;
7209 978 : return exp;
7210 : }
7211 303 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (arg1));
7212 303 : if (!INTEGRAL_TYPE_P (type))
7213 : {
7214 12 : if (complain & tf_error)
7215 12 : error_at (loc, "%qs operand not an integral type", name);
7216 12 : return error_mark_node;
7217 : }
7218 291 : if (TREE_CODE (type) == ENUMERAL_TYPE)
7219 : {
7220 15 : if (complain & tf_error)
7221 15 : error_at (loc, "argument %u in call to function "
7222 : "%qs has enumerated type", 1, name);
7223 15 : return error_mark_node;
7224 : }
7225 276 : if (TREE_CODE (type) == BOOLEAN_TYPE)
7226 : {
7227 6 : if (complain & tf_error)
7228 6 : error_at (loc, "argument %u in call to function "
7229 : "%qs has boolean type", 1, name);
7230 6 : return error_mark_node;
7231 : }
7232 270 : if (!TYPE_UNSIGNED (type))
7233 : {
7234 12 : if (complain & tf_error)
7235 12 : error_at (loc, "argument 1 in call to function "
7236 : "%qs has signed type", name);
7237 12 : return error_mark_node;
7238 : }
7239 258 : if (!same_type_p (type, TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
7240 : {
7241 24 : if (complain & tf_error)
7242 24 : error_at (loc, "arguments 1 and 2 to function %qs "
7243 : "have different types %qT and %qT",
7244 24 : name, type, TYPE_MAIN_VARIANT (TREE_TYPE (arg2)));
7245 24 : return error_mark_node;
7246 : }
7247 234 : if (arg3)
7248 : {
7249 84 : if (!POINTER_TYPE_P (TREE_TYPE (arg3)))
7250 : {
7251 3 : if (complain & tf_error)
7252 3 : error_at (loc, "argument 3 to function %qs does not have "
7253 : "pointer type", name);
7254 3 : return error_mark_node;
7255 : }
7256 81 : tree htype = TREE_TYPE (TREE_TYPE (arg3));
7257 81 : if (!same_type_p (type, TYPE_MAIN_VARIANT (htype)))
7258 : {
7259 3 : if (complain & tf_error)
7260 3 : error_at (loc, "arguments 1 and 3 to function %qs "
7261 : "have incompatible types %qT and %qT",
7262 3 : name, type, TREE_TYPE (arg3));
7263 3 : return error_mark_node;
7264 : }
7265 78 : if (TYPE_READONLY (htype))
7266 : {
7267 3 : if (complain & tf_error)
7268 3 : error_at (loc, "argument 3 to function %qs "
7269 : "has pointer to %qs type %qT",
7270 3 : name, "const", TREE_TYPE (arg3));
7271 3 : return error_mark_node;
7272 : }
7273 : }
7274 225 : tree exp = build_call_expr_internal_loc (loc, IFN_CLMUL, type,
7275 : arg3 ? 3 : 2, arg1, arg2, arg3);
7276 225 : if (processing_template_decl)
7277 : {
7278 0 : exp = build_min_non_dep_call_vec (exp, NULL, args);
7279 0 : CALL_EXPR_IFN (exp) = IFN_CLMUL;
7280 : }
7281 : return exp;
7282 : }
7283 :
7284 : /* Return a tree for the sum or difference (RESULTCODE says which)
7285 : of pointer PTROP and integer INTOP. */
7286 :
7287 : static tree
7288 5824430 : cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
7289 : tree intop, tsubst_flags_t complain)
7290 : {
7291 5824430 : tree res_type = TREE_TYPE (ptrop);
7292 :
7293 : /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
7294 : in certain circumstance (when it's valid to do so). So we need
7295 : to make sure it's complete. We don't need to check here, if we
7296 : can actually complete it at all, as those checks will be done in
7297 : pointer_int_sum() anyway. */
7298 5824430 : complete_type (TREE_TYPE (res_type));
7299 :
7300 5824430 : return pointer_int_sum (loc, resultcode, ptrop,
7301 5824430 : intop, complain & tf_warning_or_error);
7302 : }
7303 :
7304 : /* Return a tree for the difference of pointers OP0 and OP1.
7305 : The resulting tree has type int. If POINTER_SUBTRACT sanitization is
7306 : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
7307 :
7308 : static tree
7309 1554839 : pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
7310 : tsubst_flags_t complain, tree *instrument_expr)
7311 : {
7312 1554839 : tree result, inttype;
7313 1554839 : tree restype = ptrdiff_type_node;
7314 1554839 : tree target_type = TREE_TYPE (ptrtype);
7315 :
7316 1554839 : if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
7317 9 : return error_mark_node;
7318 :
7319 1554830 : if (VOID_TYPE_P (target_type))
7320 : {
7321 0 : if (complain & tf_error)
7322 0 : permerror (loc, "ISO C++ forbids using pointer of "
7323 : "type %<void *%> in subtraction");
7324 : else
7325 0 : return error_mark_node;
7326 : }
7327 1554830 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
7328 : {
7329 9 : if (complain & tf_error)
7330 3 : permerror (loc, "ISO C++ forbids using pointer to "
7331 : "a function in subtraction");
7332 : else
7333 6 : return error_mark_node;
7334 : }
7335 1554824 : if (TREE_CODE (target_type) == METHOD_TYPE)
7336 : {
7337 0 : if (complain & tf_error)
7338 0 : permerror (loc, "ISO C++ forbids using pointer to "
7339 : "a method in subtraction");
7340 : else
7341 0 : return error_mark_node;
7342 : }
7343 3109648 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
7344 1554824 : TREE_TYPE (TREE_TYPE (op0)),
7345 1554824 : !(complain & tf_error))
7346 3109648 : || !verify_type_context (loc, TCTX_POINTER_ARITH,
7347 1554824 : TREE_TYPE (TREE_TYPE (op1)),
7348 : !(complain & tf_error)))
7349 0 : return error_mark_node;
7350 :
7351 : /* Determine integer type result of the subtraction. This will usually
7352 : be the same as the result type (ptrdiff_t), but may need to be a wider
7353 : type if pointers for the address space are wider than ptrdiff_t. */
7354 1554824 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
7355 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
7356 : else
7357 : inttype = restype;
7358 :
7359 1554824 : if (!processing_template_decl
7360 1554824 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
7361 : {
7362 84 : op0 = save_expr (op0);
7363 84 : op1 = save_expr (op1);
7364 :
7365 84 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
7366 84 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
7367 : }
7368 :
7369 : /* First do the subtraction, then build the divide operator
7370 : and only convert at the very end.
7371 : Do not do default conversions in case restype is a short type. */
7372 :
7373 : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
7374 : pointers. If some platform cannot provide that, or has a larger
7375 : ptrdiff_type to support differences larger than half the address
7376 : space, cast the pointers to some larger integer type and do the
7377 : computations in that type. */
7378 1554824 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
7379 0 : op0 = cp_build_binary_op (loc,
7380 : MINUS_EXPR,
7381 : cp_convert (inttype, op0, complain),
7382 : cp_convert (inttype, op1, complain),
7383 : complain);
7384 : else
7385 1554824 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
7386 :
7387 : /* This generates an error if op1 is a pointer to an incomplete type. */
7388 1554824 : if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
7389 : {
7390 0 : if (complain & tf_error)
7391 0 : error_at (loc, "invalid use of a pointer to an incomplete type in "
7392 : "pointer arithmetic");
7393 : else
7394 0 : return error_mark_node;
7395 : }
7396 :
7397 1554824 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
7398 : {
7399 15 : if (complain & tf_error)
7400 15 : error_at (loc, "arithmetic on pointer to an empty aggregate");
7401 : else
7402 0 : return error_mark_node;
7403 : }
7404 :
7405 3109645 : op1 = (TYPE_PTROB_P (ptrtype)
7406 3109645 : ? size_in_bytes_loc (loc, target_type)
7407 : : integer_one_node);
7408 :
7409 : /* Do the division. */
7410 :
7411 1554824 : result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
7412 : cp_convert (inttype, op1, complain));
7413 1554824 : return cp_convert (restype, result, complain);
7414 : }
7415 :
7416 : /* Construct and perhaps optimize a tree representation
7417 : for a unary operation. CODE, a tree_code, specifies the operation
7418 : and XARG is the operand. */
7419 :
7420 : tree
7421 65842138 : build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
7422 : tree lookups, tsubst_flags_t complain)
7423 : {
7424 65842138 : tree orig_expr = xarg;
7425 65842138 : tree exp;
7426 65842138 : int ptrmem = 0;
7427 65842138 : tree overload = NULL_TREE;
7428 :
7429 65842138 : if (processing_template_decl)
7430 : {
7431 44884327 : if (type_dependent_expression_p (xarg))
7432 : {
7433 29273527 : tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
7434 29273527 : TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
7435 29273527 : return e;
7436 : }
7437 : }
7438 :
7439 36568611 : exp = NULL_TREE;
7440 :
7441 : /* [expr.unary.op] says:
7442 :
7443 : The address of an object of incomplete type can be taken.
7444 :
7445 : (And is just the ordinary address operator, not an overloaded
7446 : "operator &".) However, if the type is a template
7447 : specialization, we must complete the type at this point so that
7448 : an overloaded "operator &" will be available if required. */
7449 36568611 : if (code == ADDR_EXPR
7450 4212154 : && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
7451 40780342 : && ((CLASS_TYPE_P (TREE_TYPE (xarg))
7452 1848433 : && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
7453 4202355 : || (TREE_CODE (xarg) == OFFSET_REF)))
7454 : /* Don't look for a function. */;
7455 : else
7456 36495346 : exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
7457 : NULL_TREE, lookups, &overload, complain);
7458 :
7459 36568611 : if (!exp && code == ADDR_EXPR)
7460 : {
7461 4211739 : if (is_overloaded_fn (xarg))
7462 : {
7463 385626 : tree fn = get_first_fn (xarg);
7464 771252 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
7465 : {
7466 12 : if (complain & tf_error)
7467 21 : error_at (loc, DECL_CONSTRUCTOR_P (fn)
7468 : ? G_("taking address of constructor %qD")
7469 : : G_("taking address of destructor %qD"),
7470 : fn);
7471 12 : return error_mark_node;
7472 : }
7473 : }
7474 :
7475 : /* A pointer to member-function can be formed only by saying
7476 : &X::mf. */
7477 4211721 : if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
7478 4271840 : && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
7479 : {
7480 9 : if (TREE_CODE (xarg) != OFFSET_REF
7481 9 : || !TYPE_P (TREE_OPERAND (xarg, 0)))
7482 : {
7483 9 : if (complain & tf_error)
7484 : {
7485 9 : auto_diagnostic_group d;
7486 9 : error_at (loc, "invalid use of %qE to form a "
7487 : "pointer-to-member-function", xarg.get_value ());
7488 9 : if (TREE_CODE (xarg) != OFFSET_REF)
7489 6 : inform (loc, " a qualified-id is required");
7490 9 : }
7491 9 : return error_mark_node;
7492 : }
7493 : else
7494 : {
7495 0 : if (complain & tf_error)
7496 0 : error_at (loc, "parentheses around %qE cannot be used to "
7497 : "form a pointer-to-member-function",
7498 : xarg.get_value ());
7499 : else
7500 0 : return error_mark_node;
7501 0 : PTRMEM_OK_P (xarg) = 1;
7502 : }
7503 : }
7504 :
7505 4211718 : if (TREE_CODE (xarg) == OFFSET_REF)
7506 : {
7507 63874 : ptrmem = PTRMEM_OK_P (xarg);
7508 :
7509 0 : if (!ptrmem && !flag_ms_extensions
7510 63874 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
7511 : {
7512 : /* A single non-static member, make sure we don't allow a
7513 : pointer-to-member. */
7514 0 : xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
7515 0 : TREE_OPERAND (xarg, 0),
7516 0 : ovl_make (TREE_OPERAND (xarg, 1)));
7517 0 : PTRMEM_OK_P (xarg) = ptrmem;
7518 : }
7519 : }
7520 :
7521 8423436 : exp = cp_build_addr_expr_strict (xarg, complain);
7522 :
7523 4211718 : if (TREE_CODE (exp) == PTRMEM_CST)
7524 62797 : PTRMEM_CST_LOCATION (exp) = loc;
7525 : else
7526 4148921 : protected_set_expr_location (exp, loc);
7527 : }
7528 :
7529 36568590 : if (processing_template_decl && exp != error_mark_node)
7530 : {
7531 15610639 : if (overload != NULL_TREE)
7532 193028 : return (build_min_non_dep_op_overload
7533 193028 : (code, exp, overload, orig_expr, integer_zero_node));
7534 :
7535 15417611 : exp = build_min_non_dep (code, exp, orig_expr,
7536 : /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
7537 : }
7538 36375562 : if (TREE_CODE (exp) == ADDR_EXPR)
7539 3387829 : PTRMEM_OK_P (exp) = ptrmem;
7540 : return exp;
7541 : }
7542 :
7543 : /* Construct and perhaps optimize a tree representation
7544 : for __builtin_addressof operation. ARG specifies the operand. */
7545 :
7546 : tree
7547 698148 : cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7548 : {
7549 698148 : tree orig_expr = arg;
7550 :
7551 698148 : if (processing_template_decl)
7552 : {
7553 187654 : if (type_dependent_expression_p (arg))
7554 187654 : return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7555 : }
7556 :
7557 1020988 : tree exp = cp_build_addr_expr_strict (arg, complain);
7558 :
7559 510494 : if (processing_template_decl && exp != error_mark_node)
7560 0 : exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
7561 : return exp;
7562 : }
7563 :
7564 : /* Like c_common_truthvalue_conversion, but handle pointer-to-member
7565 : constants, where a null value is represented by an INTEGER_CST of
7566 : -1. */
7567 :
7568 : tree
7569 8581033 : cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7570 : {
7571 8581033 : tree type = TREE_TYPE (expr);
7572 8581033 : location_t loc = cp_expr_loc_or_input_loc (expr);
7573 7291269 : if (TYPE_PTR_OR_PTRMEM_P (type)
7574 : /* Avoid ICE on invalid use of non-static member function. */
7575 15871996 : || TREE_CODE (expr) == FUNCTION_DECL)
7576 1290070 : return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
7577 : else
7578 7290963 : return c_common_truthvalue_conversion (loc, expr);
7579 : }
7580 :
7581 : /* Returns EXPR contextually converted to bool. */
7582 :
7583 : tree
7584 80170566 : contextual_conv_bool (tree expr, tsubst_flags_t complain)
7585 : {
7586 80170566 : return perform_implicit_conversion_flags (boolean_type_node, expr,
7587 80170566 : complain, LOOKUP_NORMAL);
7588 : }
7589 :
7590 : /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7591 : is a low-level function; most callers should use maybe_convert_cond. */
7592 :
7593 : tree
7594 54753931 : condition_conversion (tree expr)
7595 : {
7596 54753931 : tree t = contextual_conv_bool (expr, tf_warning_or_error);
7597 54753931 : if (!processing_template_decl)
7598 27241066 : t = fold_build_cleanup_point_expr (boolean_type_node, t);
7599 54753931 : return t;
7600 : }
7601 :
7602 : /* Returns the address of T. This function will fold away
7603 : ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7604 : most places should use cp_build_addr_expr instead. */
7605 :
7606 : tree
7607 361750525 : build_address (tree t)
7608 : {
7609 361750525 : if (error_operand_p (t) || !cxx_mark_addressable (t))
7610 24 : return error_mark_node;
7611 361750501 : gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7612 : || processing_template_decl);
7613 361750501 : t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7614 361750501 : if (TREE_CODE (t) != ADDR_EXPR)
7615 62034911 : t = rvalue (t);
7616 : return t;
7617 : }
7618 :
7619 : /* Return a NOP_EXPR converting EXPR to TYPE. */
7620 :
7621 : tree
7622 592618801 : build_nop (tree type, tree expr MEM_STAT_DECL)
7623 : {
7624 592618801 : if (type == error_mark_node || error_operand_p (expr))
7625 : return expr;
7626 592618727 : return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr PASS_MEM_STAT);
7627 : }
7628 :
7629 : /* Take the address of ARG, whatever that means under C++ semantics.
7630 : If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7631 : and class rvalues as well.
7632 :
7633 : Nothing should call this function directly; instead, callers should use
7634 : cp_build_addr_expr or cp_build_addr_expr_strict. */
7635 :
7636 : static tree
7637 285620346 : cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7638 : {
7639 285620346 : tree argtype;
7640 285620346 : tree val;
7641 :
7642 285620346 : if (!arg || error_operand_p (arg))
7643 12 : return error_mark_node;
7644 :
7645 285620334 : arg = mark_lvalue_use (arg);
7646 285620334 : if (error_operand_p (arg))
7647 3 : return error_mark_node;
7648 :
7649 285620331 : argtype = lvalue_type (arg);
7650 285620331 : location_t loc = cp_expr_loc_or_input_loc (arg);
7651 :
7652 285620331 : gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7653 :
7654 16200257 : if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7655 285620559 : && !really_overloaded_fn (arg))
7656 : {
7657 : /* They're trying to take the address of a unique non-static
7658 : member function. This is ill-formed (except in MS-land),
7659 : but let's try to DTRT.
7660 : Note: We only handle unique functions here because we don't
7661 : want to complain if there's a static overload; non-unique
7662 : cases will be handled by instantiate_type. But we need to
7663 : handle this case here to allow casts on the resulting PMF.
7664 : We could defer this in non-MS mode, but it's easier to give
7665 : a useful error here. */
7666 :
7667 : /* Inside constant member functions, the `this' pointer
7668 : contains an extra const qualifier. TYPE_MAIN_VARIANT
7669 : is used here to remove this const from the diagnostics
7670 : and the created OFFSET_REF. */
7671 87 : tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7672 87 : tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7673 87 : if (!mark_used (fn, complain) && !(complain & tf_error))
7674 0 : return error_mark_node;
7675 : /* Until microsoft headers are known to incorrectly take the address of
7676 : unqualified xobj member functions we should not support this
7677 : extension.
7678 : See comment in class.cc:resolve_address_of_overloaded_function for
7679 : the extended reasoning. */
7680 87 : if (!flag_ms_extensions || DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7681 : {
7682 81 : auto_diagnostic_group d;
7683 81 : tree name = DECL_NAME (fn);
7684 81 : if (!(complain & tf_error))
7685 0 : return error_mark_node;
7686 81 : else if (current_class_type
7687 81 : && TREE_OPERAND (arg, 0) == current_class_ref)
7688 : /* An expression like &memfn. */
7689 36 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7690 21 : permerror (loc,
7691 : "ISO C++ forbids taking the address of an unqualified"
7692 : " or parenthesized non-static member function to form"
7693 : " a pointer to member function. Say %<&%T::%D%>",
7694 : base, name);
7695 : else
7696 15 : error_at (loc,
7697 : "ISO C++ forbids taking the address of an unqualified"
7698 : " or parenthesized non-static member function to form"
7699 : " a pointer to explicit object member function");
7700 : else
7701 45 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7702 9 : permerror (loc,
7703 : "ISO C++ forbids taking the address of a bound member"
7704 : " function to form a pointer to member function."
7705 : " Say %<&%T::%D%>",
7706 : base, name);
7707 : else
7708 36 : error_at (loc,
7709 : "ISO C++ forbids taking the address of a bound member"
7710 : " function to form a pointer to explicit object member"
7711 : " function");
7712 81 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7713 51 : inform (loc,
7714 : "a pointer to explicit object member function can only be "
7715 : "formed with %<&%T::%D%>", base, name);
7716 81 : }
7717 87 : arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7718 : }
7719 :
7720 : /* Uninstantiated types are all functions. Taking the
7721 : address of a function is a no-op, so just return the
7722 : argument. */
7723 285620331 : if (type_unknown_p (arg))
7724 3265 : return build1 (ADDR_EXPR, unknown_type_node, arg);
7725 :
7726 285617066 : if (TREE_CODE (arg) == OFFSET_REF)
7727 : /* We want a pointer to member; bypass all the code for actually taking
7728 : the address of something. */
7729 63014 : goto offset_ref;
7730 :
7731 : /* Anything not already handled and not a true memory reference
7732 : is an error. */
7733 285554052 : if (!FUNC_OR_METHOD_TYPE_P (argtype))
7734 : {
7735 150244360 : cp_lvalue_kind kind = lvalue_kind (arg);
7736 150244360 : if (kind == clk_none)
7737 : {
7738 25 : if (complain & tf_error)
7739 25 : lvalue_error (loc, lv_addressof);
7740 25 : return error_mark_node;
7741 : }
7742 150244335 : if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7743 : {
7744 39 : if (!(complain & tf_error))
7745 12 : return error_mark_node;
7746 : /* Make this a permerror because we used to accept it. */
7747 27 : permerror (loc, "taking address of rvalue");
7748 : }
7749 : }
7750 :
7751 285554015 : if (TYPE_REF_P (argtype))
7752 : {
7753 244 : tree type = build_pointer_type (TREE_TYPE (argtype));
7754 244 : arg = build1 (CONVERT_EXPR, type, arg);
7755 244 : return arg;
7756 : }
7757 285553771 : else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7758 : {
7759 : /* ARM $3.4 */
7760 : /* Apparently a lot of autoconf scripts for C++ packages do this,
7761 : so only complain if -Wpedantic. */
7762 9 : if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7763 9 : pedwarn (loc, OPT_Wpedantic,
7764 : "ISO C++ forbids taking address of function %<::main%>");
7765 0 : else if (flag_pedantic_errors)
7766 0 : return error_mark_node;
7767 : }
7768 :
7769 : /* Let &* cancel out to simplify resulting code. */
7770 285553771 : if (INDIRECT_REF_P (arg))
7771 : {
7772 102516105 : arg = TREE_OPERAND (arg, 0);
7773 102516105 : if (TYPE_REF_P (TREE_TYPE (arg)))
7774 : {
7775 63091768 : tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7776 63091768 : arg = build1 (CONVERT_EXPR, type, arg);
7777 : }
7778 : else
7779 : /* Don't let this be an lvalue. */
7780 39424337 : arg = rvalue (arg);
7781 : return arg;
7782 : }
7783 :
7784 : /* Handle complex lvalues (when permitted)
7785 : by reduction to simpler cases. */
7786 183037666 : val = unary_complex_lvalue (ADDR_EXPR, arg);
7787 183037666 : if (val != 0)
7788 : return val;
7789 :
7790 182541458 : switch (TREE_CODE (arg))
7791 : {
7792 0 : CASE_CONVERT:
7793 0 : case FLOAT_EXPR:
7794 0 : case FIX_TRUNC_EXPR:
7795 : /* We should have handled this above in the lvalue_kind check. */
7796 0 : gcc_unreachable ();
7797 81730 : break;
7798 :
7799 81730 : case BASELINK:
7800 81730 : arg = BASELINK_FUNCTIONS (arg);
7801 : /* Fall through. */
7802 :
7803 81730 : case OVERLOAD:
7804 81730 : arg = OVL_FIRST (arg);
7805 : break;
7806 :
7807 63014 : case OFFSET_REF:
7808 63014 : offset_ref:
7809 : /* Turn a reference to a non-static data member into a
7810 : pointer-to-member. */
7811 63014 : {
7812 63014 : tree type;
7813 63014 : tree t;
7814 :
7815 63014 : gcc_assert (PTRMEM_OK_P (arg));
7816 :
7817 63014 : t = TREE_OPERAND (arg, 1);
7818 63014 : if (TYPE_REF_P (TREE_TYPE (t)))
7819 : {
7820 15 : if (complain & tf_error)
7821 15 : error_at (loc,
7822 : "cannot create pointer to reference member %qD", t);
7823 15 : return error_mark_node;
7824 : }
7825 :
7826 : /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7827 62999 : if (TREE_CODE (t) == FUNCTION_DECL
7828 60321 : && !DECL_PURE_VIRTUAL_P (t)
7829 123297 : && !mark_used (t, complain) && !(complain & tf_error))
7830 3 : return error_mark_node;
7831 :
7832 : /* Pull out the function_decl for a single xobj member function, and
7833 : let the rest of this function handle it. This is similar to how
7834 : static member functions are handled in the BASELINK case above. */
7835 62996 : if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7836 : {
7837 : arg = t;
7838 : break;
7839 : }
7840 :
7841 62900 : type = build_ptrmem_type (context_for_name_lookup (t),
7842 62900 : TREE_TYPE (t));
7843 62900 : t = make_ptrmem_cst (type, t);
7844 62900 : return t;
7845 : }
7846 :
7847 : default:
7848 : break;
7849 : }
7850 :
7851 182541554 : if (argtype != error_mark_node)
7852 182541554 : argtype = build_pointer_type (argtype);
7853 :
7854 182541554 : if (bitfield_p (arg))
7855 : {
7856 33 : if (complain & tf_error)
7857 33 : error_at (loc, "attempt to take address of bit-field");
7858 33 : return error_mark_node;
7859 : }
7860 :
7861 : /* In a template, we are processing a non-dependent expression
7862 : so we can just form an ADDR_EXPR with the correct type. */
7863 182541521 : if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7864 : {
7865 166824443 : if (!mark_single_function (arg, complain))
7866 3 : return error_mark_node;
7867 166824440 : val = build_address (arg);
7868 166824440 : if (TREE_CODE (arg) == OFFSET_REF)
7869 0 : PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7870 : }
7871 15717078 : else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7872 : {
7873 27 : tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7874 :
7875 27 : if (TREE_CODE (fn) == OVERLOAD && OVL_SINGLE_P (fn))
7876 27 : fn = OVL_FIRST (fn);
7877 :
7878 : /* We can only get here with a single static member
7879 : function. */
7880 27 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7881 : && DECL_STATIC_FUNCTION_P (fn));
7882 27 : if (!mark_used (fn, complain) && !(complain & tf_error))
7883 0 : return error_mark_node;
7884 27 : val = build_address (fn);
7885 27 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7886 : /* Do not lose object's side effects. */
7887 12 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7888 12 : TREE_OPERAND (arg, 0), val);
7889 : }
7890 : else
7891 : {
7892 15717051 : tree object = TREE_OPERAND (arg, 0);
7893 15717051 : tree field = TREE_OPERAND (arg, 1);
7894 15717051 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7895 : (TREE_TYPE (object), decl_type_context (field)));
7896 15717051 : val = build_address (arg);
7897 : }
7898 :
7899 182541518 : if (TYPE_PTR_P (argtype)
7900 182541518 : && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7901 : {
7902 1132 : build_ptrmemfunc_type (argtype);
7903 1132 : val = build_ptrmemfunc (argtype, val, 0,
7904 : /*c_cast_p=*/false,
7905 : complain);
7906 : }
7907 :
7908 : /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7909 182541518 : if (TREE_CODE (val) == ADDR_EXPR
7910 182541518 : && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7911 129671433 : SET_EXPR_LOCATION (val, input_location);
7912 :
7913 : return val;
7914 : }
7915 :
7916 : /* Take the address of ARG if it has one, even if it's an rvalue. */
7917 :
7918 : tree
7919 280898134 : cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7920 : {
7921 280898134 : return cp_build_addr_expr_1 (arg, 0, complain);
7922 : }
7923 :
7924 : /* Take the address of ARG, but only if it's an lvalue. */
7925 :
7926 : static tree
7927 4722212 : cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7928 : {
7929 4722212 : return cp_build_addr_expr_1 (arg, 1, complain);
7930 : }
7931 :
7932 : /* C++: Must handle pointers to members.
7933 :
7934 : Perhaps type instantiation should be extended to handle conversion
7935 : from aggregates to types we don't yet know we want? (Or are those
7936 : cases typically errors which should be reported?)
7937 :
7938 : NOCONVERT suppresses the default promotions (such as from short to int). */
7939 :
7940 : tree
7941 32018156 : cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7942 : tsubst_flags_t complain)
7943 : {
7944 : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7945 32018156 : tree arg = xarg;
7946 32018156 : location_t location = cp_expr_loc_or_input_loc (arg);
7947 32018156 : tree argtype = 0;
7948 32018156 : tree eptype = NULL_TREE;
7949 32018156 : const char *errstring = NULL;
7950 32018156 : tree val;
7951 32018156 : const char *invalid_op_diag;
7952 :
7953 32018156 : if (!arg || error_operand_p (arg))
7954 0 : return error_mark_node;
7955 :
7956 32018156 : arg = resolve_nondeduced_context (arg, complain);
7957 :
7958 64036312 : if ((invalid_op_diag
7959 32018156 : = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7960 : ? CONVERT_EXPR
7961 : : code),
7962 32018156 : TREE_TYPE (arg))))
7963 : {
7964 0 : if (complain & tf_error)
7965 0 : error (invalid_op_diag);
7966 0 : return error_mark_node;
7967 : }
7968 :
7969 32018156 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7970 : {
7971 9654 : eptype = TREE_TYPE (arg);
7972 9654 : arg = TREE_OPERAND (arg, 0);
7973 : }
7974 :
7975 32018156 : switch (code)
7976 : {
7977 2429899 : case UNARY_PLUS_EXPR:
7978 2429899 : case NEGATE_EXPR:
7979 2429899 : {
7980 2429899 : int flags = WANT_ARITH | WANT_ENUM;
7981 : /* Unary plus (but not unary minus) is allowed on pointers. */
7982 2429899 : if (code == UNARY_PLUS_EXPR)
7983 163344 : flags |= WANT_POINTER;
7984 2429899 : arg = build_expr_type_conversion (flags, arg, true);
7985 2429899 : if (!arg)
7986 35 : errstring = (code == NEGATE_EXPR
7987 27 : ? _("wrong type argument to unary minus")
7988 8 : : _("wrong type argument to unary plus"));
7989 : else
7990 : {
7991 2429864 : if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7992 685488 : arg = cp_perform_integral_promotions (arg, complain);
7993 :
7994 : /* Make sure the result is not an lvalue: a unary plus or minus
7995 : expression is always a rvalue. */
7996 2429864 : arg = rvalue (arg);
7997 : }
7998 : }
7999 : break;
8000 :
8001 963715 : case BIT_NOT_EXPR:
8002 963715 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
8003 : {
8004 15 : code = CONJ_EXPR;
8005 15 : if (!noconvert)
8006 : {
8007 15 : arg = cp_default_conversion (arg, complain);
8008 15 : if (arg == error_mark_node)
8009 : return error_mark_node;
8010 : }
8011 : }
8012 963700 : else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
8013 : | WANT_VECTOR_OR_COMPLEX,
8014 : arg, true)))
8015 20 : errstring = _("wrong type argument to bit-complement");
8016 963680 : else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
8017 : {
8018 : /* Warn if the expression has boolean value. */
8019 963179 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
8020 963179 : && (complain & tf_warning))
8021 : {
8022 61 : auto_diagnostic_group d;
8023 61 : if (warning_at (location, OPT_Wbool_operation,
8024 : "%<~%> on an expression of type %<bool%>"))
8025 42 : inform (location, "did you mean to use logical not (%<!%>)?");
8026 61 : }
8027 963179 : arg = cp_perform_integral_promotions (arg, complain);
8028 : }
8029 501 : else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
8030 501 : arg = mark_rvalue_use (arg);
8031 : break;
8032 :
8033 0 : case ABS_EXPR:
8034 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
8035 0 : errstring = _("wrong type argument to abs");
8036 0 : else if (!noconvert)
8037 : {
8038 0 : arg = cp_default_conversion (arg, complain);
8039 0 : if (arg == error_mark_node)
8040 : return error_mark_node;
8041 : }
8042 : break;
8043 :
8044 0 : case CONJ_EXPR:
8045 : /* Conjugating a real value is a no-op, but allow it anyway. */
8046 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
8047 0 : errstring = _("wrong type argument to conjugation");
8048 0 : else if (!noconvert)
8049 : {
8050 0 : arg = cp_default_conversion (arg, complain);
8051 0 : if (arg == error_mark_node)
8052 : return error_mark_node;
8053 : }
8054 : break;
8055 :
8056 18805979 : case TRUTH_NOT_EXPR:
8057 18805979 : if (gnu_vector_type_p (TREE_TYPE (arg)))
8058 60 : return cp_build_binary_op (input_location, EQ_EXPR, arg,
8059 60 : build_zero_cst (TREE_TYPE (arg)), complain);
8060 18805919 : arg = contextual_conv_bool (arg, complain);
8061 18805919 : if (arg != error_mark_node)
8062 : {
8063 18805909 : if (processing_template_decl)
8064 8837529 : return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
8065 9968380 : val = invert_truthvalue_loc (location, arg);
8066 9968380 : if (obvalue_p (val))
8067 12286 : val = non_lvalue_loc (location, val);
8068 : return val;
8069 : }
8070 10 : errstring = _("in argument to unary !");
8071 10 : break;
8072 :
8073 : case NOP_EXPR:
8074 : break;
8075 :
8076 454195 : case REALPART_EXPR:
8077 454195 : case IMAGPART_EXPR:
8078 454195 : val = build_real_imag_expr (input_location, code, arg);
8079 454195 : if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
8080 0 : val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
8081 0 : TREE_TYPE (eptype), val);
8082 : return val;
8083 :
8084 8777257 : case PREINCREMENT_EXPR:
8085 8777257 : case POSTINCREMENT_EXPR:
8086 8777257 : case PREDECREMENT_EXPR:
8087 8777257 : case POSTDECREMENT_EXPR:
8088 : /* Handle complex lvalues (when permitted)
8089 : by reduction to simpler cases. */
8090 :
8091 8777257 : val = unary_complex_lvalue (code, arg);
8092 8777257 : if (val != 0)
8093 42 : goto return_build_unary_op;
8094 :
8095 8777215 : tree stripped_arg;
8096 8777215 : stripped_arg = tree_strip_any_location_wrapper (arg);
8097 2186431 : if ((VAR_P (stripped_arg) || TREE_CODE (stripped_arg) == PARM_DECL)
8098 8132935 : && !DECL_READ_P (stripped_arg)
8099 9937629 : && (VAR_P (stripped_arg) ? warn_unused_but_set_variable
8100 : : warn_unused_but_set_parameter) > 1)
8101 : {
8102 9868 : arg = mark_lvalue_use (arg);
8103 9868 : DECL_READ_P (stripped_arg) = 0;
8104 : }
8105 : else
8106 8767347 : arg = mark_lvalue_use (arg);
8107 :
8108 : /* Increment or decrement the real part of the value,
8109 : and don't change the imaginary part. */
8110 8777215 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
8111 : {
8112 18 : tree real, imag;
8113 :
8114 18 : arg = cp_stabilize_reference (arg);
8115 18 : real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
8116 18 : imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
8117 18 : real = cp_build_unary_op (code, real, true, complain);
8118 18 : if (real == error_mark_node || imag == error_mark_node)
8119 : return error_mark_node;
8120 15 : val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
8121 15 : goto return_build_unary_op;
8122 : }
8123 :
8124 : /* Report invalid types. */
8125 :
8126 8777197 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
8127 : arg, true)))
8128 : {
8129 36 : if (code == PREINCREMENT_EXPR)
8130 9 : errstring = _("no pre-increment operator for type");
8131 27 : else if (code == POSTINCREMENT_EXPR)
8132 9 : errstring = _("no post-increment operator for type");
8133 18 : else if (code == PREDECREMENT_EXPR)
8134 9 : errstring = _("no pre-decrement operator for type");
8135 : else
8136 9 : errstring = _("no post-decrement operator for type");
8137 : break;
8138 : }
8139 8777161 : else if (arg == error_mark_node)
8140 : return error_mark_node;
8141 :
8142 : /* Report something read-only. */
8143 :
8144 8777158 : if (CP_TYPE_CONST_P (TREE_TYPE (arg))
8145 8777158 : || TREE_READONLY (arg))
8146 : {
8147 176 : if (complain & tf_error)
8148 176 : cxx_readonly_error (location, arg,
8149 176 : ((code == PREINCREMENT_EXPR
8150 176 : || code == POSTINCREMENT_EXPR)
8151 : ? lv_increment : lv_decrement));
8152 : else
8153 0 : return error_mark_node;
8154 : }
8155 :
8156 8777158 : {
8157 8777158 : tree inc;
8158 8777158 : tree declared_type = unlowered_expr_type (arg);
8159 :
8160 8777158 : argtype = TREE_TYPE (arg);
8161 :
8162 : /* ARM $5.2.5 last annotation says this should be forbidden. */
8163 8777158 : if (TREE_CODE (argtype) == ENUMERAL_TYPE)
8164 : {
8165 0 : if (complain & tf_error)
8166 0 : permerror (location, (code == PREINCREMENT_EXPR
8167 0 : || code == POSTINCREMENT_EXPR)
8168 : ? G_("ISO C++ forbids incrementing an enum")
8169 : : G_("ISO C++ forbids decrementing an enum"));
8170 : else
8171 0 : return error_mark_node;
8172 : }
8173 :
8174 : /* Compute the increment. */
8175 :
8176 8777158 : if (TYPE_PTR_P (argtype))
8177 : {
8178 2813453 : tree type = complete_type (TREE_TYPE (argtype));
8179 :
8180 2813453 : if (!COMPLETE_OR_VOID_TYPE_P (type))
8181 : {
8182 10 : if (complain & tf_error)
8183 9 : error_at (location, ((code == PREINCREMENT_EXPR
8184 9 : || code == POSTINCREMENT_EXPR))
8185 : ? G_("cannot increment a pointer to incomplete "
8186 : "type %qT")
8187 : : G_("cannot decrement a pointer to incomplete "
8188 : "type %qT"),
8189 9 : TREE_TYPE (argtype));
8190 : else
8191 1 : return error_mark_node;
8192 : }
8193 2813443 : else if (!TYPE_PTROB_P (argtype))
8194 : {
8195 69 : if (complain & tf_error)
8196 63 : pedwarn (location, OPT_Wpointer_arith,
8197 63 : (code == PREINCREMENT_EXPR
8198 63 : || code == POSTINCREMENT_EXPR)
8199 : ? G_("ISO C++ forbids incrementing a pointer "
8200 : "of type %qT")
8201 : : G_("ISO C++ forbids decrementing a pointer "
8202 : "of type %qT"),
8203 : argtype);
8204 : else
8205 6 : return error_mark_node;
8206 : }
8207 2813374 : else if (!verify_type_context (location, TCTX_POINTER_ARITH,
8208 2813374 : TREE_TYPE (argtype),
8209 2813374 : !(complain & tf_error)))
8210 0 : return error_mark_node;
8211 :
8212 2813446 : inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
8213 : }
8214 : else
8215 5963562 : inc = VECTOR_TYPE_P (argtype)
8216 5963705 : ? build_one_cst (argtype)
8217 : : integer_one_node;
8218 :
8219 8777151 : inc = cp_convert (argtype, inc, complain);
8220 :
8221 : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
8222 : need to ask Objective-C to build the increment or decrement
8223 : expression for it. */
8224 8777151 : if (objc_is_property_ref (arg))
8225 0 : return objc_build_incr_expr_for_property_ref (input_location, code,
8226 0 : arg, inc);
8227 :
8228 : /* Complain about anything else that is not a true lvalue. */
8229 8777151 : if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
8230 8777151 : || code == POSTINCREMENT_EXPR)
8231 : ? lv_increment : lv_decrement),
8232 : complain))
8233 38 : return error_mark_node;
8234 :
8235 : /* [depr.volatile.type] "Postfix ++ and -- expressions and
8236 : prefix ++ and -- expressions of volatile-qualified arithmetic
8237 : and pointer types are deprecated." */
8238 8776828 : if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
8239 8777113 : && (complain & tf_warning))
8240 398 : warning_at (location, OPT_Wvolatile,
8241 : "%qs expression of %<volatile%>-qualified type is "
8242 : "deprecated",
8243 : ((code == PREINCREMENT_EXPR
8244 : || code == POSTINCREMENT_EXPR)
8245 : ? "++" : "--"));
8246 :
8247 : /* Forbid using -- or ++ in C++17 on `bool'. */
8248 8777113 : if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
8249 : {
8250 95 : if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
8251 : {
8252 33 : if (complain & tf_error)
8253 33 : error_at (location,
8254 : "use of an operand of type %qT in %<operator--%> "
8255 : "is forbidden", boolean_type_node);
8256 33 : return error_mark_node;
8257 : }
8258 : else
8259 : {
8260 62 : if (cxx_dialect >= cxx17)
8261 : {
8262 33 : if (complain & tf_error)
8263 32 : error_at (location,
8264 : "use of an operand of type %qT in "
8265 : "%<operator++%> is forbidden in C++17",
8266 : boolean_type_node);
8267 33 : return error_mark_node;
8268 : }
8269 : /* Otherwise, [depr.incr.bool] says this is deprecated. */
8270 29 : else if (complain & tf_warning)
8271 25 : warning_at (location, OPT_Wdeprecated,
8272 : "use of an operand of type %qT "
8273 : "in %<operator++%> is deprecated",
8274 : boolean_type_node);
8275 : }
8276 29 : val = boolean_increment (code, arg);
8277 : }
8278 8777018 : else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
8279 : /* An rvalue has no cv-qualifiers. */
8280 1420246 : val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
8281 : else
8282 7356772 : val = build2 (code, TREE_TYPE (arg), arg, inc);
8283 :
8284 8777047 : TREE_SIDE_EFFECTS (val) = 1;
8285 8777047 : goto return_build_unary_op;
8286 : }
8287 :
8288 587111 : case ADDR_EXPR:
8289 : /* Note that this operation never does default_conversion
8290 : regardless of NOCONVERT. */
8291 587111 : return cp_build_addr_expr (arg, complain);
8292 :
8293 : default:
8294 : break;
8295 : }
8296 :
8297 3393645 : if (!errstring)
8298 : {
8299 3393559 : if (argtype == 0)
8300 3393559 : argtype = TREE_TYPE (arg);
8301 3393559 : val = build1 (code, argtype, arg);
8302 12170663 : return_build_unary_op:
8303 12170663 : if (eptype)
8304 9645 : val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
8305 : return val;
8306 : }
8307 :
8308 101 : if (complain & tf_error)
8309 54 : error_at (location, "%s", errstring);
8310 101 : return error_mark_node;
8311 : }
8312 :
8313 : /* Hook for the c-common bits that build a unary op. */
8314 : tree
8315 4147 : build_unary_op (location_t /*location*/,
8316 : enum tree_code code, tree xarg, bool noconvert)
8317 : {
8318 4147 : return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
8319 : }
8320 :
8321 : /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
8322 : so that it is a valid lvalue even for GENERIC by replacing
8323 : (lhs = rhs) with ((lhs = rhs), lhs)
8324 : (--lhs) with ((--lhs), lhs)
8325 : (++lhs) with ((++lhs), lhs)
8326 : and if lhs has side-effects, calling cp_stabilize_reference on it, so
8327 : that it can be evaluated multiple times. */
8328 :
8329 : tree
8330 407733 : genericize_compound_lvalue (tree lvalue)
8331 : {
8332 407733 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
8333 203 : lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
8334 203 : cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
8335 203 : TREE_OPERAND (lvalue, 1));
8336 407733 : return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
8337 407733 : lvalue, TREE_OPERAND (lvalue, 0));
8338 : }
8339 :
8340 : /* Apply unary lvalue-demanding operator CODE to the expression ARG
8341 : for certain kinds of expressions which are not really lvalues
8342 : but which we can accept as lvalues.
8343 :
8344 : If ARG is not a kind of expression we can handle, return
8345 : NULL_TREE. */
8346 :
8347 : tree
8348 324799272 : unary_complex_lvalue (enum tree_code code, tree arg)
8349 : {
8350 : /* Inside a template, making these kinds of adjustments is
8351 : pointless; we are only concerned with the type of the
8352 : expression. */
8353 325206775 : if (processing_template_decl)
8354 : return NULL_TREE;
8355 :
8356 : /* Handle (a, b) used as an "lvalue". */
8357 234521383 : if (TREE_CODE (arg) == COMPOUND_EXPR)
8358 : {
8359 409203 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
8360 : tf_warning_or_error);
8361 409203 : return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
8362 818406 : TREE_OPERAND (arg, 0), real_result);
8363 : }
8364 :
8365 : /* Handle (a ? b : c) used as an "lvalue". */
8366 234112180 : if (TREE_CODE (arg) == COND_EXPR
8367 234025115 : || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
8368 87065 : return rationalize_conditional_expr (code, arg, tf_warning_or_error);
8369 :
8370 : /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
8371 234025115 : if (TREE_CODE (arg) == MODIFY_EXPR
8372 233617737 : || TREE_CODE (arg) == PREINCREMENT_EXPR
8373 233617673 : || TREE_CODE (arg) == PREDECREMENT_EXPR)
8374 407503 : return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
8375 :
8376 233617612 : if (code != ADDR_EXPR)
8377 : return NULL_TREE;
8378 :
8379 : /* Handle (a = b) used as an "lvalue" for `&'. */
8380 229641360 : if (TREE_CODE (arg) == MODIFY_EXPR
8381 229641360 : || TREE_CODE (arg) == INIT_EXPR)
8382 : {
8383 0 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
8384 : tf_warning_or_error);
8385 0 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
8386 : arg, real_result);
8387 0 : suppress_warning (arg /* What warning? */);
8388 0 : return arg;
8389 : }
8390 :
8391 351831015 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
8392 351829904 : || TREE_CODE (arg) == OFFSET_REF)
8393 : return NULL_TREE;
8394 :
8395 : /* We permit compiler to make function calls returning
8396 : objects of aggregate type look like lvalues. */
8397 122188544 : {
8398 122188544 : tree targ = arg;
8399 :
8400 122188544 : if (TREE_CODE (targ) == SAVE_EXPR)
8401 0 : targ = TREE_OPERAND (targ, 0);
8402 :
8403 122188544 : if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
8404 : {
8405 26 : if (TREE_CODE (arg) == SAVE_EXPR)
8406 : targ = arg;
8407 : else
8408 26 : targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
8409 26 : return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
8410 : }
8411 :
8412 122188518 : if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
8413 0 : return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
8414 0 : TREE_OPERAND (targ, 0), current_function_decl, NULL);
8415 : }
8416 :
8417 : /* Don't let anything else be handled specially. */
8418 : return NULL_TREE;
8419 : }
8420 :
8421 : /* Mark EXP saying that we need to be able to take the
8422 : address of it; it should not be allocated in a register.
8423 : Value is true if successful. ARRAY_REF_P is true if this
8424 : is for ARRAY_REF construction - in that case we don't want
8425 : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
8426 : it is fine to use ARRAY_REFs for vector subscripts on vector
8427 : register variables.
8428 :
8429 : C++: we do not allow `current_class_ptr' to be addressable. */
8430 :
8431 : bool
8432 381368588 : cxx_mark_addressable (tree exp, bool array_ref_p)
8433 : {
8434 381368588 : tree x = exp;
8435 :
8436 445963467 : while (1)
8437 445963467 : switch (TREE_CODE (x))
8438 : {
8439 32692616 : case VIEW_CONVERT_EXPR:
8440 32692616 : if (array_ref_p
8441 808406 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8442 33305483 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
8443 : return true;
8444 32689349 : x = TREE_OPERAND (x, 0);
8445 32689349 : break;
8446 :
8447 31430801 : case COMPONENT_REF:
8448 31430801 : if (bitfield_p (x))
8449 9 : error ("attempt to take address of bit-field");
8450 : /* FALLTHRU */
8451 31905530 : case ADDR_EXPR:
8452 31905530 : case ARRAY_REF:
8453 31905530 : case REALPART_EXPR:
8454 31905530 : case IMAGPART_EXPR:
8455 31905530 : x = TREE_OPERAND (x, 0);
8456 31905530 : break;
8457 :
8458 8449691 : case PARM_DECL:
8459 8449691 : if (x == current_class_ptr)
8460 : {
8461 18 : error ("cannot take the address of %<this%>, which is an rvalue expression");
8462 18 : TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
8463 18 : return true;
8464 : }
8465 : /* Fall through. */
8466 :
8467 78228725 : case VAR_DECL:
8468 : /* Caller should not be trying to mark initialized
8469 : constant fields addressable. */
8470 78228725 : gcc_assert (DECL_LANG_SPECIFIC (x) == 0
8471 : || DECL_IN_AGGR_P (x) == 0
8472 : || TREE_STATIC (x)
8473 : || DECL_EXTERNAL (x));
8474 78228725 : if (VAR_P (x)
8475 69779052 : && DECL_ANON_UNION_VAR_P (x)
8476 78228874 : && !TREE_ADDRESSABLE (x))
8477 51 : cxx_mark_addressable (DECL_VALUE_EXPR (x));
8478 : /* Fall through. */
8479 :
8480 79099230 : case RESULT_DECL:
8481 79099360 : if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
8482 79099351 : && !DECL_ARTIFICIAL (x))
8483 : {
8484 31 : if (VAR_P (x) && DECL_HARD_REGISTER (x))
8485 : {
8486 15 : error
8487 15 : ("address of explicit register variable %qD requested", x);
8488 15 : return false;
8489 : }
8490 16 : else if (extra_warnings)
8491 3 : warning
8492 3 : (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
8493 : }
8494 79099215 : TREE_ADDRESSABLE (x) = 1;
8495 79099215 : return true;
8496 :
8497 186504993 : case CONST_DECL:
8498 186504993 : case FUNCTION_DECL:
8499 186504993 : TREE_ADDRESSABLE (x) = 1;
8500 186504993 : return true;
8501 :
8502 55727 : case CONSTRUCTOR:
8503 55727 : TREE_ADDRESSABLE (x) = 1;
8504 55727 : return true;
8505 :
8506 17517939 : case TARGET_EXPR:
8507 17517939 : TREE_ADDRESSABLE (x) = 1;
8508 17517939 : cxx_mark_addressable (TARGET_EXPR_SLOT (x));
8509 17517939 : return true;
8510 :
8511 : default:
8512 : return true;
8513 : }
8514 : }
8515 :
8516 : /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
8517 :
8518 : tree
8519 9853929 : build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
8520 : tsubst_flags_t complain)
8521 : {
8522 9853929 : tree orig_ifexp = ifexp;
8523 9853929 : tree orig_op1 = op1;
8524 9853929 : tree orig_op2 = op2;
8525 9853929 : tree expr;
8526 :
8527 9853929 : if (processing_template_decl)
8528 : {
8529 : /* The standard says that the expression is type-dependent if
8530 : IFEXP is type-dependent, even though the eventual type of the
8531 : expression doesn't dependent on IFEXP. */
8532 3511725 : if (type_dependent_expression_p (ifexp)
8533 : /* As a GNU extension, the middle operand may be omitted. */
8534 1911098 : || (op1 && type_dependent_expression_p (op1))
8535 4934092 : || type_dependent_expression_p (op2))
8536 2116290 : return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
8537 : }
8538 :
8539 7737639 : expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
8540 7737639 : if (processing_template_decl && expr != error_mark_node)
8541 : {
8542 1395429 : tree min = build_min_non_dep (COND_EXPR, expr,
8543 : orig_ifexp, orig_op1, orig_op2);
8544 1395429 : expr = convert_from_reference (min);
8545 : }
8546 : return expr;
8547 : }
8548 :
8549 : /* Given a list of expressions, return a compound expression
8550 : that performs them all and returns the value of the last of them. */
8551 :
8552 : tree
8553 32613530 : build_x_compound_expr_from_list (tree list, expr_list_kind exp,
8554 : tsubst_flags_t complain)
8555 : {
8556 32613530 : tree expr = TREE_VALUE (list);
8557 :
8558 488044 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8559 33101562 : && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
8560 : {
8561 14 : if (complain & tf_error)
8562 28 : pedwarn (cp_expr_loc_or_input_loc (expr), 0,
8563 : "list-initializer for non-class type must not "
8564 : "be parenthesized");
8565 : else
8566 0 : return error_mark_node;
8567 : }
8568 :
8569 32613530 : if (TREE_CHAIN (list))
8570 : {
8571 23 : if (complain & tf_error)
8572 14 : switch (exp)
8573 : {
8574 12 : case ELK_INIT:
8575 12 : permerror (input_location, "expression list treated as compound "
8576 : "expression in initializer");
8577 12 : break;
8578 1 : case ELK_MEM_INIT:
8579 1 : permerror (input_location, "expression list treated as compound "
8580 : "expression in mem-initializer");
8581 1 : break;
8582 1 : case ELK_FUNC_CAST:
8583 1 : permerror (input_location, "expression list treated as compound "
8584 : "expression in functional cast");
8585 1 : break;
8586 0 : default:
8587 0 : gcc_unreachable ();
8588 : }
8589 : else
8590 9 : return error_mark_node;
8591 :
8592 28 : for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
8593 28 : expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
8594 14 : expr, TREE_VALUE (list), NULL_TREE,
8595 : complain);
8596 : }
8597 :
8598 : return expr;
8599 : }
8600 :
8601 : /* Like build_x_compound_expr_from_list, but using a VEC. */
8602 :
8603 : tree
8604 410667 : build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8605 : tsubst_flags_t complain)
8606 : {
8607 410667 : if (vec_safe_is_empty (vec))
8608 : return NULL_TREE;
8609 410667 : else if (vec->length () == 1)
8610 410603 : return (*vec)[0];
8611 : else
8612 : {
8613 64 : tree expr;
8614 64 : unsigned int ix;
8615 64 : tree t;
8616 :
8617 64 : if (msg != NULL)
8618 : {
8619 4 : if (complain & tf_error)
8620 0 : permerror (input_location,
8621 : "%s expression list treated as compound expression",
8622 : msg);
8623 : else
8624 4 : return error_mark_node;
8625 : }
8626 :
8627 60 : expr = (*vec)[0];
8628 134 : for (ix = 1; vec->iterate (ix, &t); ++ix)
8629 74 : expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
8630 : t, NULL_TREE, complain);
8631 :
8632 : return expr;
8633 : }
8634 : }
8635 :
8636 : /* Handle overloading of the ',' operator when needed. */
8637 :
8638 : tree
8639 3189610 : build_x_compound_expr (location_t loc, tree op1, tree op2,
8640 : tree lookups, tsubst_flags_t complain)
8641 : {
8642 3189610 : tree result;
8643 3189610 : tree orig_op1 = op1;
8644 3189610 : tree orig_op2 = op2;
8645 3189610 : tree overload = NULL_TREE;
8646 :
8647 3189610 : if (processing_template_decl)
8648 : {
8649 2921757 : if (type_dependent_expression_p (op1)
8650 2921757 : || type_dependent_expression_p (op2))
8651 : {
8652 2649393 : result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8653 2649393 : TREE_TYPE (result)
8654 2649393 : = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
8655 2649393 : return result;
8656 : }
8657 : }
8658 :
8659 540217 : result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8660 : NULL_TREE, lookups, &overload, complain);
8661 540217 : if (!result)
8662 531596 : result = cp_build_compound_expr (op1, op2, complain);
8663 :
8664 540217 : if (processing_template_decl && result != error_mark_node)
8665 : {
8666 270543 : if (overload != NULL_TREE)
8667 72 : return (build_min_non_dep_op_overload
8668 72 : (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
8669 :
8670 270471 : return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
8671 : }
8672 :
8673 : return result;
8674 : }
8675 :
8676 : /* Like cp_build_compound_expr, but for the c-common bits. */
8677 :
8678 : tree
8679 22080 : build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8680 : {
8681 22080 : return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8682 : }
8683 :
8684 : /* Build a compound expression. */
8685 :
8686 : tree
8687 696082 : cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8688 : {
8689 696082 : lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8690 :
8691 696082 : if (lhs == error_mark_node || rhs == error_mark_node)
8692 : return error_mark_node;
8693 :
8694 696079 : if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8695 0 : lhs = TREE_OPERAND (lhs, 0);
8696 696079 : tree eptype = NULL_TREE;
8697 696079 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8698 : {
8699 3 : eptype = TREE_TYPE (rhs);
8700 3 : rhs = TREE_OPERAND (rhs, 0);
8701 : }
8702 :
8703 696079 : if (TREE_CODE (rhs) == TARGET_EXPR)
8704 : {
8705 : /* If the rhs is a TARGET_EXPR, then build the compound
8706 : expression inside the target_expr's initializer. This
8707 : helps the compiler to eliminate unnecessary temporaries. */
8708 31477 : tree init = TARGET_EXPR_INITIAL (rhs);
8709 :
8710 31477 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8711 31477 : TARGET_EXPR_INITIAL (rhs) = init;
8712 :
8713 31477 : if (eptype)
8714 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8715 : return rhs;
8716 : }
8717 :
8718 664602 : rhs = resolve_nondeduced_context (rhs, complain);
8719 :
8720 664602 : if (type_unknown_p (rhs))
8721 : {
8722 33 : if (complain & tf_error)
8723 51 : error_at (cp_expr_loc_or_input_loc (rhs),
8724 : "no context to resolve type of %qE", rhs);
8725 33 : return error_mark_node;
8726 : }
8727 :
8728 664569 : tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8729 664569 : if (eptype)
8730 3 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8731 : return ret;
8732 : }
8733 :
8734 : /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8735 : casts away constness. CAST gives the type of cast. Returns true
8736 : if the cast is ill-formed, false if it is well-formed.
8737 :
8738 : ??? This function warns for casting away any qualifier not just
8739 : const. We would like to specify exactly what qualifiers are casted
8740 : away.
8741 : */
8742 :
8743 : static bool
8744 869806 : check_for_casting_away_constness (location_t loc, tree src_type,
8745 : tree dest_type, enum tree_code cast,
8746 : tsubst_flags_t complain)
8747 : {
8748 : /* C-style casts are allowed to cast away constness. With
8749 : WARN_CAST_QUAL, we still want to issue a warning. */
8750 869806 : if (cast == CAST_EXPR && !warn_cast_qual)
8751 : return false;
8752 :
8753 655014 : if (!casts_away_constness (src_type, dest_type, complain))
8754 : return false;
8755 :
8756 402 : switch (cast)
8757 : {
8758 345 : case CAST_EXPR:
8759 345 : if (complain & tf_warning)
8760 345 : warning_at (loc, OPT_Wcast_qual,
8761 : "cast from type %qT to type %qT casts away qualifiers",
8762 : src_type, dest_type);
8763 : return false;
8764 :
8765 36 : case STATIC_CAST_EXPR:
8766 36 : if (complain & tf_error)
8767 24 : error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8768 : "away qualifiers",
8769 : src_type, dest_type);
8770 : return true;
8771 :
8772 21 : case REINTERPRET_CAST_EXPR:
8773 21 : if (complain & tf_error)
8774 15 : error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8775 : "casts away qualifiers",
8776 : src_type, dest_type);
8777 : return true;
8778 :
8779 0 : default:
8780 0 : gcc_unreachable();
8781 : }
8782 : }
8783 :
8784 : /* Warns if the cast from expression EXPR to type TYPE is useless. */
8785 : void
8786 52025430 : maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8787 : tsubst_flags_t complain)
8788 : {
8789 52025430 : if (warn_useless_cast
8790 152 : && complain & tf_warning)
8791 : {
8792 152 : if (TYPE_REF_P (type)
8793 152 : ? ((TYPE_REF_IS_RVALUE (type)
8794 86 : ? xvalue_p (expr) : lvalue_p (expr))
8795 172 : && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8796 : /* Don't warn when converting a class object to a non-reference type,
8797 : because that's a common way to create a temporary. */
8798 66 : : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8799 96 : warning_at (loc, OPT_Wuseless_cast,
8800 : "useless cast to type %q#T", type);
8801 : }
8802 52025430 : }
8803 :
8804 : /* Warns if the cast ignores cv-qualifiers on TYPE. */
8805 : static void
8806 52018345 : maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8807 : tsubst_flags_t complain)
8808 : {
8809 52018345 : if (warn_ignored_qualifiers
8810 276781 : && complain & tf_warning
8811 276274 : && !CLASS_TYPE_P (type)
8812 52289265 : && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8813 63 : warning_at (loc, OPT_Wignored_qualifiers,
8814 : "type qualifiers ignored on cast result type");
8815 52018345 : }
8816 :
8817 : /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8818 : (another pointer-to-member type in the same hierarchy) and return
8819 : the converted expression. If ALLOW_INVERSE_P is permitted, a
8820 : pointer-to-derived may be converted to pointer-to-base; otherwise,
8821 : only the other direction is permitted. If C_CAST_P is true, this
8822 : conversion is taking place as part of a C-style cast. */
8823 :
8824 : tree
8825 28974 : convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8826 : bool c_cast_p, tsubst_flags_t complain)
8827 : {
8828 28974 : if (same_type_p (type, TREE_TYPE (expr)))
8829 : return expr;
8830 :
8831 28974 : if (TYPE_PTRDATAMEM_P (type))
8832 : {
8833 418 : tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8834 418 : tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8835 418 : tree delta = (get_delta_difference
8836 418 : (obase, nbase,
8837 : allow_inverse_p, c_cast_p, complain));
8838 :
8839 418 : if (delta == error_mark_node)
8840 : return error_mark_node;
8841 :
8842 412 : if (!same_type_p (obase, nbase))
8843 : {
8844 299 : if (TREE_CODE (expr) == PTRMEM_CST)
8845 157 : expr = cplus_expand_constant (expr);
8846 :
8847 299 : tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8848 299 : build_int_cst (TREE_TYPE (expr), -1),
8849 : complain);
8850 299 : tree op1 = build_nop (ptrdiff_type_node, expr);
8851 299 : tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8852 : complain);
8853 :
8854 299 : expr = fold_build3_loc (input_location,
8855 : COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8856 : }
8857 :
8858 412 : return build_nop (type, expr);
8859 : }
8860 : else
8861 28556 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8862 28556 : allow_inverse_p, c_cast_p, complain);
8863 : }
8864 :
8865 : /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8866 : this static_cast is being attempted as one of the possible casts
8867 : allowed by a C-style cast. (In that case, accessibility of base
8868 : classes is not considered, and it is OK to cast away
8869 : constness.) Return the result of the cast. *VALID_P is set to
8870 : indicate whether or not the cast was valid. */
8871 :
8872 : static tree
8873 51475900 : build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8874 : bool *valid_p, tsubst_flags_t complain)
8875 : {
8876 51475900 : tree intype;
8877 51475900 : tree result;
8878 51475900 : cp_lvalue_kind clk;
8879 :
8880 : /* Assume the cast is valid. */
8881 51475900 : *valid_p = true;
8882 :
8883 51475900 : intype = unlowered_expr_type (expr);
8884 :
8885 : /* Save casted types in the function's used types hash table. */
8886 51475900 : used_types_insert (type);
8887 :
8888 : /* A prvalue of non-class type is cv-unqualified. */
8889 51475900 : if (!CLASS_TYPE_P (type))
8890 48251608 : type = cv_unqualified (type);
8891 :
8892 : /* [expr.static.cast]
8893 :
8894 : An lvalue of type "cv1 B", where B is a class type, can be cast
8895 : to type "reference to cv2 D", where D is a class derived (clause
8896 : _class.derived_) from B, if a valid standard conversion from
8897 : "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8898 : same cv-qualification as, or greater cv-qualification than, cv1,
8899 : and B is not a virtual base class of D. */
8900 : /* We check this case before checking the validity of "TYPE t =
8901 : EXPR;" below because for this case:
8902 :
8903 : struct B {};
8904 : struct D : public B { D(const B&); };
8905 : extern B& b;
8906 : void f() { static_cast<const D&>(b); }
8907 :
8908 : we want to avoid constructing a new D. The standard is not
8909 : completely clear about this issue, but our interpretation is
8910 : consistent with other compilers. */
8911 51475900 : if (TYPE_REF_P (type)
8912 5799502 : && CLASS_TYPE_P (TREE_TYPE (type))
8913 3804276 : && CLASS_TYPE_P (intype)
8914 3804259 : && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8915 3791116 : && DERIVED_FROM_P (intype, TREE_TYPE (type))
8916 3768667 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8917 3768667 : build_pointer_type (TYPE_MAIN_VARIANT
8918 : (TREE_TYPE (type))),
8919 : complain)
8920 55244567 : && (c_cast_p
8921 3768643 : || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8922 : {
8923 3768667 : tree base;
8924 :
8925 3768667 : if (processing_template_decl)
8926 : return expr;
8927 :
8928 : /* There is a standard conversion from "D*" to "B*" even if "B"
8929 : is ambiguous or inaccessible. If this is really a
8930 : static_cast, then we check both for inaccessibility and
8931 : ambiguity. However, if this is a static_cast being performed
8932 : because the user wrote a C-style cast, then accessibility is
8933 : not considered. */
8934 7059630 : base = lookup_base (TREE_TYPE (type), intype,
8935 : c_cast_p ? ba_unique : ba_check,
8936 : NULL, complain);
8937 3529827 : expr = cp_build_addr_expr (expr, complain);
8938 :
8939 3529827 : if (sanitize_flags_p (SANITIZE_VPTR))
8940 : {
8941 511 : tree ubsan_check
8942 511 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8943 : intype, expr);
8944 511 : if (ubsan_check)
8945 3529827 : expr = ubsan_check;
8946 : }
8947 :
8948 : /* Convert from "B*" to "D*". This function will check that "B"
8949 : is not a virtual base of "D". Even if we don't have a guarantee
8950 : that expr is NULL, if the static_cast is to a reference type,
8951 : it is UB if it would be NULL, so omit the non-NULL check. */
8952 3529827 : expr = build_base_path (MINUS_EXPR, expr, base,
8953 : /*nonnull=*/flag_delete_null_pointer_checks,
8954 : complain);
8955 :
8956 : /* Convert the pointer to a reference -- but then remember that
8957 : there are no expressions with reference type in C++.
8958 :
8959 : We call rvalue so that there's an actual tree code
8960 : (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8961 : is a variable with the same type, the conversion would get folded
8962 : away, leaving just the variable and causing lvalue_kind to give
8963 : the wrong answer. */
8964 3529827 : expr = cp_fold_convert (type, expr);
8965 :
8966 : /* When -fsanitize=null, make sure to diagnose reference binding to
8967 : NULL even when the reference is converted to pointer later on. */
8968 3529827 : if (sanitize_flags_p (SANITIZE_NULL)
8969 517 : && TREE_CODE (expr) == COND_EXPR
8970 6 : && TREE_OPERAND (expr, 2)
8971 6 : && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8972 3529833 : && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8973 6 : ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8974 :
8975 3529827 : return convert_from_reference (rvalue (expr));
8976 : }
8977 :
8978 : /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8979 : cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8980 47707233 : if (TYPE_REF_P (type)
8981 2030835 : && TYPE_REF_IS_RVALUE (type)
8982 858035 : && (clk = real_lvalue_p (expr))
8983 846075 : && reference_compatible_p (TREE_TYPE (type), intype)
8984 48553301 : && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8985 : {
8986 846068 : if (processing_template_decl)
8987 : return expr;
8988 845303 : if (clk == clk_ordinary)
8989 : {
8990 : /* Handle the (non-bit-field) lvalue case here by casting to
8991 : lvalue reference and then changing it to an rvalue reference.
8992 : Casting an xvalue to rvalue reference will be handled by the
8993 : main code path. */
8994 845300 : tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8995 845300 : result = (perform_direct_initialization_if_possible
8996 845300 : (lref, expr, c_cast_p, complain));
8997 845300 : result = build1 (NON_LVALUE_EXPR, type, result);
8998 845300 : return convert_from_reference (result);
8999 : }
9000 : else
9001 : /* For a bit-field or packed field, bind to a temporary. */
9002 3 : expr = rvalue (expr);
9003 : }
9004 :
9005 : /* Resolve overloaded address here rather than once in
9006 : implicit_conversion and again in the inverse code below. */
9007 46861168 : if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
9008 : {
9009 21 : expr = instantiate_type (type, expr, complain);
9010 21 : intype = TREE_TYPE (expr);
9011 : }
9012 :
9013 : /* [expr.static.cast]
9014 :
9015 : Any expression can be explicitly converted to type cv void. */
9016 46861168 : if (VOID_TYPE_P (type))
9017 : {
9018 609021 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9019 0 : expr = TREE_OPERAND (expr, 0);
9020 609021 : return convert_to_void (expr, ICV_CAST, complain);
9021 : }
9022 :
9023 : /* [class.abstract]
9024 : An abstract class shall not be used ... as the type of an explicit
9025 : conversion. */
9026 46252147 : if (abstract_virtuals_error (ACU_CAST, type, complain))
9027 6 : return error_mark_node;
9028 :
9029 : /* [expr.static.cast]
9030 :
9031 : An expression e can be explicitly converted to a type T using a
9032 : static_cast of the form static_cast<T>(e) if the declaration T
9033 : t(e);" is well-formed, for some invented temporary variable
9034 : t. */
9035 46252141 : result = perform_direct_initialization_if_possible (type, expr,
9036 : c_cast_p, complain);
9037 : /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
9038 : which initialize the first element of the aggregate. We need to handle
9039 : the array case specifically. */
9040 46252141 : if (result == NULL_TREE
9041 4753600 : && cxx_dialect >= cxx20
9042 3377776 : && TREE_CODE (type) == ARRAY_TYPE)
9043 : {
9044 : /* Create { EXPR } and perform direct-initialization from it. */
9045 15 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
9046 15 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
9047 15 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
9048 15 : result = perform_direct_initialization_if_possible (type, e, c_cast_p,
9049 : complain);
9050 : }
9051 4753600 : if (result)
9052 : {
9053 41498556 : if (processing_template_decl)
9054 : return expr;
9055 :
9056 40791391 : result = convert_from_reference (result);
9057 :
9058 : /* [expr.static.cast]
9059 :
9060 : If T is a reference type, the result is an lvalue; otherwise,
9061 : the result is an rvalue. */
9062 40791391 : if (!TYPE_REF_P (type))
9063 : {
9064 39606718 : result = rvalue (result);
9065 :
9066 39606718 : if (result == expr && SCALAR_TYPE_P (type))
9067 : /* Leave some record of the cast. */
9068 2454997 : result = build_nop (type, expr);
9069 : }
9070 : return result;
9071 : }
9072 :
9073 : /* [expr.static.cast]
9074 :
9075 : The inverse of any standard conversion sequence (clause _conv_),
9076 : other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
9077 : (_conv.array_), function-to-pointer (_conv.func_), and boolean
9078 : (_conv.bool_) conversions, can be performed explicitly using
9079 : static_cast subject to the restriction that the explicit
9080 : conversion does not cast away constness (_expr.const.cast_), and
9081 : the following additional rules for specific cases: */
9082 : /* For reference, the conversions not excluded are: integral
9083 : promotions, floating-point promotion, integral conversions,
9084 : floating-point conversions, floating-integral conversions,
9085 : pointer conversions, and pointer to member conversions. */
9086 : /* DR 128
9087 :
9088 : A value of integral _or enumeration_ type can be explicitly
9089 : converted to an enumeration type. */
9090 : /* The effect of all that is that any conversion between any two
9091 : types which are integral, floating, or enumeration types can be
9092 : performed. */
9093 4753585 : if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
9094 2634240 : || SCALAR_FLOAT_TYPE_P (type))
9095 2240048 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
9096 221721 : || SCALAR_FLOAT_TYPE_P (intype)))
9097 : {
9098 2138937 : if (processing_template_decl)
9099 : return expr;
9100 2046640 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9101 0 : expr = TREE_OPERAND (expr, 0);
9102 : /* [expr.static.cast]: "If the value is not a bit-field, the result
9103 : refers to the object or the specified base class subobject thereof;
9104 : otherwise, the lvalue-to-rvalue conversion is applied to the
9105 : bit-field and the resulting prvalue is used as the operand of the
9106 : static_cast." There are no prvalue bit-fields; the l-to-r conversion
9107 : will give us an object of the underlying type of the bit-field. */
9108 2046640 : expr = decay_conversion (expr, complain);
9109 2046640 : return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
9110 : }
9111 :
9112 809283 : if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
9113 804645 : && CLASS_TYPE_P (TREE_TYPE (type))
9114 189711 : && CLASS_TYPE_P (TREE_TYPE (intype))
9115 2649931 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
9116 : (TREE_TYPE (intype))),
9117 35283 : build_pointer_type (TYPE_MAIN_VARIANT
9118 : (TREE_TYPE (type))),
9119 : complain))
9120 : {
9121 34943 : tree base;
9122 :
9123 34943 : if (processing_template_decl)
9124 : return expr;
9125 :
9126 34925 : if (!c_cast_p
9127 34925 : && check_for_casting_away_constness (loc, intype, type,
9128 : STATIC_CAST_EXPR,
9129 : complain))
9130 6 : return error_mark_node;
9131 69296 : base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
9132 : c_cast_p ? ba_unique : ba_check,
9133 : NULL, complain);
9134 34919 : expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
9135 : complain);
9136 :
9137 34919 : if (sanitize_flags_p (SANITIZE_VPTR))
9138 : {
9139 43 : tree ubsan_check
9140 43 : = cp_ubsan_maybe_instrument_downcast (loc, type,
9141 : intype, expr);
9142 43 : if (ubsan_check)
9143 34919 : expr = ubsan_check;
9144 : }
9145 :
9146 34919 : return cp_fold_convert (type, expr);
9147 : }
9148 :
9149 89 : if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
9150 2579705 : || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
9151 : {
9152 232 : tree c1;
9153 232 : tree c2;
9154 232 : tree t1;
9155 232 : tree t2;
9156 :
9157 232 : c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
9158 232 : c2 = TYPE_PTRMEM_CLASS_TYPE (type);
9159 :
9160 232 : if (TYPE_PTRDATAMEM_P (type))
9161 : {
9162 89 : t1 = (build_ptrmem_type
9163 89 : (c1,
9164 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
9165 89 : t2 = (build_ptrmem_type
9166 89 : (c2,
9167 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
9168 : }
9169 : else
9170 : {
9171 : t1 = intype;
9172 : t2 = type;
9173 : }
9174 232 : if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
9175 : {
9176 154 : if (!c_cast_p
9177 154 : && check_for_casting_away_constness (loc, intype, type,
9178 : STATIC_CAST_EXPR,
9179 : complain))
9180 9 : return error_mark_node;
9181 145 : if (processing_template_decl)
9182 : return expr;
9183 145 : return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
9184 145 : c_cast_p, complain);
9185 : }
9186 : }
9187 :
9188 : /* [expr.static.cast]
9189 :
9190 : An rvalue of type "pointer to cv void" can be explicitly
9191 : converted to a pointer to object type. A value of type pointer
9192 : to object converted to "pointer to cv void" and back to the
9193 : original pointer type will have its original value. */
9194 2579551 : if (TYPE_PTR_P (intype)
9195 868494 : && VOID_TYPE_P (TREE_TYPE (intype))
9196 3312495 : && TYPE_PTROB_P (type))
9197 : {
9198 700658 : if (!c_cast_p
9199 700658 : && check_for_casting_away_constness (loc, intype, type,
9200 : STATIC_CAST_EXPR,
9201 : complain))
9202 21 : return error_mark_node;
9203 700637 : if (processing_template_decl)
9204 : return expr;
9205 615772 : return build_nop (type, expr);
9206 : }
9207 :
9208 1878893 : *valid_p = false;
9209 1878893 : return error_mark_node;
9210 : }
9211 :
9212 : /* Return an expression representing static_cast<TYPE>(EXPR). */
9213 :
9214 : tree
9215 17645993 : build_static_cast (location_t loc, tree type, tree oexpr,
9216 : tsubst_flags_t complain)
9217 : {
9218 17645993 : tree expr = oexpr;
9219 17645993 : tree result;
9220 17645993 : bool valid_p;
9221 :
9222 17645993 : if (type == error_mark_node || expr == error_mark_node)
9223 : return error_mark_node;
9224 :
9225 17645741 : bool dependent = (dependent_type_p (type)
9226 17645741 : || type_dependent_expression_p (expr));
9227 12293989 : if (dependent)
9228 : {
9229 6475739 : tmpl:
9230 6475739 : expr = build_min (STATIC_CAST_EXPR, type, oexpr);
9231 : /* We don't know if it will or will not have side effects. */
9232 6475739 : TREE_SIDE_EFFECTS (expr) = 1;
9233 6475739 : result = convert_from_reference (expr);
9234 6475739 : protected_set_expr_location (result, loc);
9235 6475739 : return result;
9236 : }
9237 :
9238 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9239 : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9240 12293989 : if (!TYPE_REF_P (type)
9241 6495949 : && TREE_CODE (expr) == NOP_EXPR
9242 12405290 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9243 92 : expr = TREE_OPERAND (expr, 0);
9244 :
9245 12293989 : result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9246 : &valid_p, complain);
9247 12293989 : if (valid_p)
9248 : {
9249 12293688 : if (result != error_mark_node)
9250 : {
9251 12293571 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9252 12293571 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9253 : }
9254 12293688 : if (processing_template_decl)
9255 1123987 : goto tmpl;
9256 11169701 : protected_set_expr_location (result, loc);
9257 11169701 : return result;
9258 : }
9259 :
9260 301 : if (complain & tf_error)
9261 : {
9262 172 : auto_diagnostic_group d;
9263 172 : error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
9264 172 : TREE_TYPE (expr), type);
9265 172 : if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
9266 130 : && CLASS_TYPE_P (TREE_TYPE (type))
9267 201 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
9268 17 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
9269 17 : "class type %qT is incomplete", TREE_TYPE (type));
9270 172 : tree expr_type = TREE_TYPE (expr);
9271 172 : if (TYPE_PTR_P (expr_type))
9272 44 : expr_type = TREE_TYPE (expr_type);
9273 172 : if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
9274 12 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
9275 : "class type %qT is incomplete", expr_type);
9276 172 : }
9277 301 : return error_mark_node;
9278 : }
9279 :
9280 : /* EXPR is an expression with member function or pointer-to-member
9281 : function type. TYPE is a pointer type. Converting EXPR to TYPE is
9282 : not permitted by ISO C++, but we accept it in some modes. If we
9283 : are not in one of those modes, issue a diagnostic. Return the
9284 : converted expression. */
9285 :
9286 : tree
9287 169 : convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
9288 : {
9289 169 : tree intype;
9290 169 : tree decl;
9291 :
9292 169 : intype = TREE_TYPE (expr);
9293 169 : gcc_assert (TYPE_PTRMEMFUNC_P (intype)
9294 : || TREE_CODE (intype) == METHOD_TYPE);
9295 :
9296 169 : if (!(complain & tf_warning_or_error))
9297 0 : return error_mark_node;
9298 :
9299 169 : location_t loc = cp_expr_loc_or_input_loc (expr);
9300 :
9301 169 : if (pedantic || warn_pmf2ptr)
9302 215 : pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
9303 : "converting from %qH to %qI", intype, type);
9304 :
9305 169 : STRIP_ANY_LOCATION_WRAPPER (expr);
9306 :
9307 169 : if (TREE_CODE (intype) == METHOD_TYPE)
9308 74 : expr = build_addr_func (expr, complain);
9309 95 : else if (TREE_CODE (expr) == PTRMEM_CST)
9310 77 : expr = build_address (PTRMEM_CST_MEMBER (expr));
9311 : else
9312 : {
9313 18 : decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
9314 18 : decl = build_address (decl);
9315 18 : expr = get_member_function_from_ptrfunc (&decl, expr, complain);
9316 : }
9317 :
9318 169 : if (expr == error_mark_node)
9319 : return error_mark_node;
9320 :
9321 169 : expr = build_nop (type, expr);
9322 169 : SET_EXPR_LOCATION (expr, loc);
9323 169 : return expr;
9324 : }
9325 :
9326 : /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
9327 : constexpr evaluation knows to reject it. */
9328 :
9329 : static tree
9330 114716 : build_nop_reinterpret (tree type, tree expr)
9331 : {
9332 114716 : tree ret = build_nop (type, expr);
9333 114716 : if (ret != expr)
9334 114716 : REINTERPRET_CAST_P (ret) = true;
9335 114716 : return ret;
9336 : }
9337 :
9338 : /* Return a representation for a reinterpret_cast from EXPR to TYPE.
9339 : If C_CAST_P is true, this reinterpret cast is being done as part of
9340 : a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
9341 : indicate whether or not reinterpret_cast was valid. */
9342 :
9343 : static tree
9344 2033048 : build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
9345 : bool c_cast_p, bool *valid_p,
9346 : tsubst_flags_t complain)
9347 : {
9348 2033048 : tree intype;
9349 :
9350 : /* Assume the cast is invalid. */
9351 2033048 : if (valid_p)
9352 1878648 : *valid_p = true;
9353 :
9354 2033048 : if (type == error_mark_node || error_operand_p (expr))
9355 : return error_mark_node;
9356 :
9357 2033048 : intype = TREE_TYPE (expr);
9358 :
9359 : /* Save casted types in the function's used types hash table. */
9360 2033048 : used_types_insert (type);
9361 :
9362 : /* A prvalue of non-class type is cv-unqualified. */
9363 2033048 : if (!CLASS_TYPE_P (type))
9364 2033039 : type = cv_unqualified (type);
9365 :
9366 : /* [expr.reinterpret.cast]
9367 : A glvalue of type T1, designating an object x, can be cast to the type
9368 : "reference to T2" if an expression of type "pointer to T1" can be
9369 : explicitly converted to the type "pointer to T2" using a reinterpret_cast.
9370 : The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
9371 : of type "pointer to T1". No temporary is created, no copy is made, and no
9372 : constructors (11.4.4) or conversion functions (11.4.7) are called. */
9373 2033048 : if (TYPE_REF_P (type))
9374 : {
9375 9823 : if (!glvalue_p (expr))
9376 : {
9377 9 : if (complain & tf_error)
9378 9 : error_at (loc, "invalid cast of a prvalue expression of type "
9379 : "%qT to type %qT",
9380 : intype, type);
9381 9 : return error_mark_node;
9382 : }
9383 :
9384 : /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
9385 : "B" are related class types; the reinterpret_cast does not
9386 : adjust the pointer. */
9387 9814 : if (TYPE_PTR_P (intype)
9388 3 : && (complain & tf_warning)
9389 9817 : && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
9390 : COMPARE_BASE | COMPARE_DERIVED)))
9391 3 : warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
9392 : intype, type);
9393 :
9394 9814 : expr = cp_build_addr_expr (expr, complain);
9395 :
9396 9814 : if (warn_strict_aliasing > 2)
9397 72 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9398 :
9399 9814 : if (expr != error_mark_node)
9400 9814 : expr = build_reinterpret_cast_1
9401 9814 : (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
9402 : valid_p, complain);
9403 9814 : if (expr != error_mark_node)
9404 : /* cp_build_indirect_ref isn't right for rvalue refs. */
9405 9811 : expr = convert_from_reference (fold_convert (type, expr));
9406 : return expr;
9407 : }
9408 :
9409 : /* As a G++ extension, we consider conversions from member
9410 : functions, and pointers to member functions to
9411 : pointer-to-function and pointer-to-void types. If
9412 : -Wno-pmf-conversions has not been specified,
9413 : convert_member_func_to_ptr will issue an error message. */
9414 252 : if ((TYPE_PTRMEMFUNC_P (intype)
9415 2023051 : || TREE_CODE (intype) == METHOD_TYPE)
9416 248 : && TYPE_PTR_P (type)
9417 2023394 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
9418 127 : || VOID_TYPE_P (TREE_TYPE (type))))
9419 160 : return convert_member_func_to_ptr (type, expr, complain);
9420 :
9421 : /* If the cast is not to a reference type, the lvalue-to-rvalue,
9422 : array-to-pointer, and function-to-pointer conversions are
9423 : performed. */
9424 2023065 : expr = decay_conversion (expr, complain);
9425 :
9426 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9427 : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9428 2023065 : if (TREE_CODE (expr) == NOP_EXPR
9429 2023065 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9430 388 : expr = TREE_OPERAND (expr, 0);
9431 :
9432 2023065 : if (error_operand_p (expr))
9433 30 : return error_mark_node;
9434 :
9435 2023035 : intype = TREE_TYPE (expr);
9436 :
9437 : /* [expr.reinterpret.cast]
9438 : A pointer can be converted to any integral type large enough to
9439 : hold it. ... A value of type std::nullptr_t can be converted to
9440 : an integral type; the conversion has the same meaning and
9441 : validity as a conversion of (void*)0 to the integral type. */
9442 2023035 : if (CP_INTEGRAL_TYPE_P (type)
9443 167970 : && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
9444 : {
9445 166063 : if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
9446 : {
9447 21 : if (complain & tf_error)
9448 15 : permerror (loc, "cast from %qH to %qI loses precision",
9449 : intype, type);
9450 : else
9451 6 : return error_mark_node;
9452 : }
9453 166057 : if (NULLPTR_TYPE_P (intype))
9454 98 : return build_int_cst (type, 0);
9455 : }
9456 : /* [expr.reinterpret.cast]
9457 : A value of integral or enumeration type can be explicitly
9458 : converted to a pointer. */
9459 1856972 : else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
9460 : /* OK */
9461 : ;
9462 1821941 : else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
9463 1820019 : || TYPE_PTR_OR_PTRMEM_P (type))
9464 1937163 : && same_type_p (type, intype))
9465 : /* DR 799 */
9466 474 : return rvalue (expr);
9467 1821467 : else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
9468 : {
9469 282 : if ((complain & tf_warning)
9470 564 : && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
9471 282 : TREE_TYPE (intype)))
9472 145 : warning_at (loc, OPT_Wcast_function_type,
9473 : "cast between incompatible function types"
9474 : " from %qH to %qI", intype, type);
9475 282 : return build_nop_reinterpret (type, expr);
9476 : }
9477 1821185 : else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
9478 : {
9479 79 : if ((complain & tf_warning)
9480 158 : && !cxx_safe_function_type_cast_p
9481 79 : (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
9482 79 : TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
9483 52 : warning_at (loc, OPT_Wcast_function_type,
9484 : "cast between incompatible pointer to member types"
9485 : " from %qH to %qI", intype, type);
9486 79 : return build_nop_reinterpret (type, expr);
9487 : }
9488 21 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
9489 1821106 : || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
9490 : {
9491 114100 : if (!c_cast_p
9492 114100 : && check_for_casting_away_constness (loc, intype, type,
9493 : REINTERPRET_CAST_EXPR,
9494 : complain))
9495 21 : return error_mark_node;
9496 : /* Warn about possible alignment problems. */
9497 114079 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9498 24 : && (complain & tf_warning)
9499 24 : && !VOID_TYPE_P (type)
9500 24 : && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
9501 24 : && COMPLETE_TYPE_P (TREE_TYPE (type))
9502 24 : && COMPLETE_TYPE_P (TREE_TYPE (intype))
9503 114127 : && min_align_of_type (TREE_TYPE (type))
9504 24 : > min_align_of_type (TREE_TYPE (intype)))
9505 18 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9506 : "increases required alignment of target type",
9507 : intype, type);
9508 :
9509 114079 : if (warn_strict_aliasing <= 2)
9510 : /* strict_aliasing_warning STRIP_NOPs its expr. */
9511 102241 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9512 :
9513 114079 : return build_nop_reinterpret (type, expr);
9514 : }
9515 297 : else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
9516 1707166 : || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
9517 : {
9518 276 : if (complain & tf_warning)
9519 : /* C++11 5.2.10 p8 says that "Converting a function pointer to an
9520 : object pointer type or vice versa is conditionally-supported." */
9521 276 : warning_at (loc, OPT_Wconditionally_supported,
9522 : "casting between pointer-to-function and "
9523 : "pointer-to-object is conditionally-supported");
9524 276 : return build_nop_reinterpret (type, expr);
9525 : }
9526 1706730 : else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
9527 1704773 : return convert_to_vector (type, rvalue (expr));
9528 1957 : else if (gnu_vector_type_p (intype)
9529 1957 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9530 1855 : return convert_to_integer_nofold (type, expr);
9531 : else
9532 : {
9533 102 : if (valid_p)
9534 84 : *valid_p = false;
9535 102 : if (complain & tf_error)
9536 93 : error_at (loc, "invalid cast from type %qT to type %qT",
9537 : intype, type);
9538 102 : return error_mark_node;
9539 : }
9540 :
9541 200990 : expr = cp_convert (type, expr, complain);
9542 200990 : if (TREE_CODE (expr) == NOP_EXPR)
9543 : /* Mark any nop_expr that created as a reintepret_cast. */
9544 0 : REINTERPRET_CAST_P (expr) = true;
9545 : return expr;
9546 : }
9547 :
9548 : tree
9549 769604 : build_reinterpret_cast (location_t loc, tree type, tree expr,
9550 : tsubst_flags_t complain)
9551 : {
9552 769604 : tree r;
9553 :
9554 769604 : if (type == error_mark_node || expr == error_mark_node)
9555 : return error_mark_node;
9556 :
9557 769597 : if (processing_template_decl)
9558 : {
9559 624955 : tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
9560 :
9561 624955 : if (!TREE_SIDE_EFFECTS (t)
9562 624955 : && type_dependent_expression_p (expr))
9563 : /* There might turn out to be side effects inside expr. */
9564 293465 : TREE_SIDE_EFFECTS (t) = 1;
9565 624955 : r = convert_from_reference (t);
9566 624955 : protected_set_expr_location (r, loc);
9567 624955 : return r;
9568 : }
9569 :
9570 144642 : r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9571 : /*valid_p=*/NULL, complain);
9572 144642 : if (r != error_mark_node)
9573 : {
9574 144579 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9575 144579 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9576 : }
9577 144642 : protected_set_expr_location (r, loc);
9578 144642 : return r;
9579 : }
9580 :
9581 : /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9582 : return an appropriate expression. Otherwise, return
9583 : error_mark_node. If the cast is not valid, and COMPLAIN is true,
9584 : then a diagnostic will be issued. If VALID_P is non-NULL, we are
9585 : performing a C-style cast, its value upon return will indicate
9586 : whether or not the conversion succeeded. */
9587 :
9588 : static tree
9589 39580775 : build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9590 : tsubst_flags_t complain, bool *valid_p)
9591 : {
9592 39580775 : tree src_type;
9593 39580775 : tree reference_type;
9594 :
9595 : /* Callers are responsible for handling error_mark_node as a
9596 : destination type. */
9597 39580775 : gcc_assert (dst_type != error_mark_node);
9598 : /* In a template, callers should be building syntactic
9599 : representations of casts, not using this machinery. */
9600 39580775 : gcc_assert (!processing_template_decl);
9601 :
9602 : /* Assume the conversion is invalid. */
9603 39580775 : if (valid_p)
9604 39382725 : *valid_p = false;
9605 :
9606 39580775 : if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9607 : {
9608 38502255 : if (complain & tf_error)
9609 9 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9610 : "which is not a pointer, reference, "
9611 : "nor a pointer-to-data-member type", dst_type);
9612 38502255 : return error_mark_node;
9613 : }
9614 :
9615 1078520 : if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
9616 : {
9617 4176 : if (complain & tf_error)
9618 6 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9619 : "which is a pointer or reference to a function type",
9620 : dst_type);
9621 4176 : return error_mark_node;
9622 : }
9623 :
9624 : /* A prvalue of non-class type is cv-unqualified. */
9625 1074344 : dst_type = cv_unqualified (dst_type);
9626 :
9627 : /* Save casted types in the function's used types hash table. */
9628 1074344 : used_types_insert (dst_type);
9629 :
9630 1074344 : src_type = TREE_TYPE (expr);
9631 : /* Expressions do not really have reference types. */
9632 1074344 : if (TYPE_REF_P (src_type))
9633 0 : src_type = TREE_TYPE (src_type);
9634 :
9635 : /* [expr.const.cast]
9636 :
9637 : For two object types T1 and T2, if a pointer to T1 can be explicitly
9638 : converted to the type "pointer to T2" using a const_cast, then the
9639 : following conversions can also be made:
9640 :
9641 : -- an lvalue of type T1 can be explicitly converted to an lvalue of
9642 : type T2 using the cast const_cast<T2&>;
9643 :
9644 : -- a glvalue of type T1 can be explicitly converted to an xvalue of
9645 : type T2 using the cast const_cast<T2&&>; and
9646 :
9647 : -- if T1 is a class type, a prvalue of type T1 can be explicitly
9648 : converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9649 :
9650 1074344 : if (TYPE_REF_P (dst_type))
9651 : {
9652 125124 : reference_type = dst_type;
9653 125124 : if (!TYPE_REF_IS_RVALUE (dst_type)
9654 125124 : ? lvalue_p (expr)
9655 1560 : : obvalue_p (expr))
9656 : /* OK. */;
9657 : else
9658 : {
9659 35 : if (complain & tf_error)
9660 9 : error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
9661 : "to type %qT",
9662 : src_type, dst_type);
9663 35 : return error_mark_node;
9664 : }
9665 125089 : dst_type = build_pointer_type (TREE_TYPE (dst_type));
9666 125089 : src_type = build_pointer_type (src_type);
9667 : }
9668 : else
9669 : {
9670 949220 : reference_type = NULL_TREE;
9671 : /* If the destination type is not a reference type, the
9672 : lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9673 : conversions are performed. */
9674 949220 : src_type = type_decays_to (src_type);
9675 949220 : if (src_type == error_mark_node)
9676 : return error_mark_node;
9677 : }
9678 :
9679 1074309 : if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9680 : {
9681 640815 : if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9682 : {
9683 398786 : if (valid_p)
9684 : {
9685 200814 : *valid_p = true;
9686 : /* This cast is actually a C-style cast. Issue a warning if
9687 : the user is making a potentially unsafe cast. */
9688 200814 : check_for_casting_away_constness (loc, src_type, dst_type,
9689 : CAST_EXPR, complain);
9690 : /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9691 200814 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9692 3 : && (complain & tf_warning)
9693 200820 : && min_align_of_type (TREE_TYPE (dst_type))
9694 3 : > min_align_of_type (TREE_TYPE (src_type)))
9695 3 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9696 : "increases required alignment of target type",
9697 : src_type, dst_type);
9698 : }
9699 398786 : if (reference_type)
9700 : {
9701 123655 : expr = cp_build_addr_expr (expr, complain);
9702 123655 : if (expr == error_mark_node)
9703 : return error_mark_node;
9704 123637 : expr = build_nop (reference_type, expr);
9705 123637 : return convert_from_reference (expr);
9706 : }
9707 : else
9708 : {
9709 275131 : expr = decay_conversion (expr, complain);
9710 275131 : if (expr == error_mark_node)
9711 : return error_mark_node;
9712 :
9713 : /* build_c_cast puts on a NOP_EXPR to make the result not an
9714 : lvalue. Strip such NOP_EXPRs if VALUE is being used in
9715 : non-lvalue context. */
9716 275131 : if (TREE_CODE (expr) == NOP_EXPR
9717 275131 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9718 3 : expr = TREE_OPERAND (expr, 0);
9719 275131 : return build_nop (dst_type, expr);
9720 : }
9721 : }
9722 242029 : else if (valid_p
9723 484010 : && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9724 241981 : TREE_TYPE (src_type)))
9725 14992 : check_for_casting_away_constness (loc, src_type, dst_type,
9726 : CAST_EXPR, complain);
9727 : }
9728 :
9729 675523 : if (complain & tf_error)
9730 30 : error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9731 : src_type, dst_type);
9732 675523 : return error_mark_node;
9733 : }
9734 :
9735 : tree
9736 694688 : build_const_cast (location_t loc, tree type, tree expr,
9737 : tsubst_flags_t complain)
9738 : {
9739 694688 : tree r;
9740 :
9741 694688 : if (type == error_mark_node || error_operand_p (expr))
9742 : return error_mark_node;
9743 :
9744 694680 : if (processing_template_decl)
9745 : {
9746 496630 : tree t = build_min (CONST_CAST_EXPR, type, expr);
9747 :
9748 496630 : if (!TREE_SIDE_EFFECTS (t)
9749 496630 : && type_dependent_expression_p (expr))
9750 : /* There might turn out to be side effects inside expr. */
9751 380200 : TREE_SIDE_EFFECTS (t) = 1;
9752 496630 : r = convert_from_reference (t);
9753 496630 : protected_set_expr_location (r, loc);
9754 496630 : return r;
9755 : }
9756 :
9757 198050 : r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9758 198050 : if (r != error_mark_node)
9759 : {
9760 197972 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9761 197972 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9762 : }
9763 198050 : protected_set_expr_location (r, loc);
9764 198050 : return r;
9765 : }
9766 :
9767 : /* Like cp_build_c_cast, but for the c-common bits. */
9768 :
9769 : tree
9770 0 : build_c_cast (location_t loc, tree type, tree expr)
9771 : {
9772 0 : return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9773 : }
9774 :
9775 : /* Like the "build_c_cast" used for c-common, but using cp_expr to
9776 : preserve location information even for tree nodes that don't
9777 : support it. */
9778 :
9779 : cp_expr
9780 10682615 : build_c_cast (location_t loc, tree type, cp_expr expr)
9781 : {
9782 10682615 : cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9783 10682615 : result.set_location (loc);
9784 10682615 : return result;
9785 : }
9786 :
9787 : /* Build an expression representing an explicit C-style cast to type
9788 : TYPE of expression EXPR. */
9789 :
9790 : tree
9791 42179443 : cp_build_c_cast (location_t loc, tree type, tree expr,
9792 : tsubst_flags_t complain)
9793 : {
9794 42179443 : tree value = expr;
9795 42179443 : tree result;
9796 42179443 : bool valid_p;
9797 :
9798 42179443 : if (type == error_mark_node || error_operand_p (expr))
9799 : return error_mark_node;
9800 :
9801 42179266 : if (processing_template_decl)
9802 : {
9803 2796532 : tree t = build_min (CAST_EXPR, type,
9804 : tree_cons (NULL_TREE, value, NULL_TREE));
9805 : /* We don't know if it will or will not have side effects. */
9806 2796532 : TREE_SIDE_EFFECTS (t) = 1;
9807 2796532 : return convert_from_reference (t);
9808 : }
9809 :
9810 : /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9811 : 'Class') should always be retained, because this information aids
9812 : in method lookup. */
9813 39382734 : if (objc_is_object_ptr (type)
9814 39382734 : && objc_is_object_ptr (TREE_TYPE (expr)))
9815 0 : return build_nop (type, expr);
9816 :
9817 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9818 : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9819 39382734 : if (!TYPE_REF_P (type)
9820 39379787 : && TREE_CODE (value) == NOP_EXPR
9821 39857077 : && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9822 241435 : value = TREE_OPERAND (value, 0);
9823 :
9824 39382734 : if (TREE_CODE (type) == ARRAY_TYPE)
9825 : {
9826 : /* Allow casting from T1* to T2[] because Cfront allows it.
9827 : NIHCL uses it. It is not valid ISO C++ however. */
9828 3 : if (TYPE_PTR_P (TREE_TYPE (expr)))
9829 : {
9830 0 : if (complain & tf_error)
9831 0 : permerror (loc, "ISO C++ forbids casting to an array type %qT",
9832 : type);
9833 : else
9834 0 : return error_mark_node;
9835 0 : type = build_pointer_type (TREE_TYPE (type));
9836 : }
9837 : else
9838 : {
9839 3 : if (complain & tf_error)
9840 3 : error_at (loc, "ISO C++ forbids casting to an array type %qT",
9841 : type);
9842 3 : return error_mark_node;
9843 : }
9844 : }
9845 :
9846 39382731 : if (FUNC_OR_METHOD_TYPE_P (type))
9847 : {
9848 15 : if (complain & tf_error)
9849 15 : error_at (loc, "invalid cast to function type %qT", type);
9850 15 : return error_mark_node;
9851 : }
9852 :
9853 39382716 : if (TYPE_PTR_P (type)
9854 877370 : && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9855 : /* Casting to an integer of smaller size is an error detected elsewhere. */
9856 432032 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9857 : /* Don't warn about converting any constant. */
9858 39759253 : && !TREE_CONSTANT (value))
9859 47 : warning_at (loc, OPT_Wint_to_pointer_cast,
9860 : "cast to pointer from integer of different size");
9861 :
9862 : /* A C-style cast can be a const_cast. */
9863 39382716 : result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9864 : &valid_p);
9865 39382716 : if (valid_p)
9866 : {
9867 200805 : if (result != error_mark_node)
9868 : {
9869 200796 : maybe_warn_about_useless_cast (loc, type, value, complain);
9870 200796 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9871 : }
9872 9 : else if (complain & tf_error)
9873 9 : build_const_cast_1 (loc, type, value, tf_error, &valid_p);
9874 : return result;
9875 : }
9876 :
9877 : /* Or a static cast. */
9878 39181911 : result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9879 : &valid_p, complain);
9880 : /* Or a reinterpret_cast. */
9881 39181911 : if (!valid_p)
9882 1878592 : result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9883 : &valid_p, complain);
9884 : /* The static_cast or reinterpret_cast may be followed by a
9885 : const_cast. */
9886 39181911 : if (valid_p
9887 : /* A valid cast may result in errors if, for example, a
9888 : conversion to an ambiguous base class is required. */
9889 39181911 : && !error_operand_p (result))
9890 : {
9891 39181427 : tree result_type;
9892 :
9893 39181427 : maybe_warn_about_useless_cast (loc, type, value, complain);
9894 39181427 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9895 :
9896 : /* Non-class rvalues always have cv-unqualified type. */
9897 39181427 : if (!CLASS_TYPE_P (type))
9898 36358804 : type = TYPE_MAIN_VARIANT (type);
9899 39181427 : result_type = TREE_TYPE (result);
9900 39181427 : if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9901 36357348 : result_type = TYPE_MAIN_VARIANT (result_type);
9902 : /* If the type of RESULT does not match TYPE, perform a
9903 : const_cast to make it match. If the static_cast or
9904 : reinterpret_cast succeeded, we will differ by at most
9905 : cv-qualification, so the follow-on const_cast is guaranteed
9906 : to succeed. */
9907 39181427 : if (!same_type_p (non_reference (type), non_reference (result_type)))
9908 : {
9909 0 : result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
9910 0 : gcc_assert (valid_p);
9911 : }
9912 : return result;
9913 : }
9914 :
9915 484 : return error_mark_node;
9916 : }
9917 :
9918 : /* Warn when a value is moved to itself with std::move. LHS is the target,
9919 : RHS may be the std::move call, and LOC is the location of the whole
9920 : assignment. Return true if we warned. */
9921 :
9922 : bool
9923 29247838 : maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9924 : {
9925 29247838 : if (!warn_self_move)
9926 : return false;
9927 :
9928 : /* C++98 doesn't know move. */
9929 368812 : if (cxx_dialect < cxx11)
9930 : return false;
9931 :
9932 348985 : if (processing_template_decl)
9933 : return false;
9934 :
9935 26841 : if (!REFERENCE_REF_P (rhs)
9936 305213 : || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9937 : return false;
9938 4910 : tree fn = TREE_OPERAND (rhs, 0);
9939 4910 : if (!is_std_move_p (fn))
9940 : return false;
9941 :
9942 : /* Just a little helper to strip * and various NOPs. */
9943 6693 : auto extract_op = [] (tree &op) {
9944 4462 : STRIP_NOPS (op);
9945 5683 : while (INDIRECT_REF_P (op))
9946 1221 : op = TREE_OPERAND (op, 0);
9947 4462 : op = maybe_undo_parenthesized_ref (op);
9948 4462 : STRIP_ANY_LOCATION_WRAPPER (op);
9949 4462 : };
9950 :
9951 2231 : tree arg = CALL_EXPR_ARG (fn, 0);
9952 2231 : extract_op (arg);
9953 2231 : if (TREE_CODE (arg) == ADDR_EXPR)
9954 1555 : arg = TREE_OPERAND (arg, 0);
9955 2231 : tree type = TREE_TYPE (lhs);
9956 2231 : tree orig_lhs = lhs;
9957 2231 : extract_op (lhs);
9958 2231 : if (cp_tree_equal (lhs, arg)
9959 : /* Also warn in a member-initializer-list, as in : i(std::move(i)). */
9960 2231 : || (TREE_CODE (lhs) == FIELD_DECL
9961 642 : && TREE_CODE (arg) == COMPONENT_REF
9962 305 : && cp_tree_equal (TREE_OPERAND (arg, 0), current_class_ref)
9963 6 : && TREE_OPERAND (arg, 1) == lhs))
9964 117 : if (warning_at (loc, OPT_Wself_move,
9965 : "moving %qE of type %qT to itself", orig_lhs, type))
9966 : return true;
9967 :
9968 : return false;
9969 : }
9970 :
9971 : /* For use from the C common bits. */
9972 : tree
9973 4778 : build_modify_expr (location_t location,
9974 : tree lhs, tree /*lhs_origtype*/,
9975 : enum tree_code modifycode,
9976 : location_t /*rhs_location*/, tree rhs,
9977 : tree /*rhs_origtype*/)
9978 : {
9979 4778 : return cp_build_modify_expr (location, lhs, modifycode, rhs,
9980 4778 : tf_warning_or_error);
9981 : }
9982 :
9983 : /* Build an assignment expression of lvalue LHS from value RHS.
9984 : MODIFYCODE is the code for a binary operator that we use
9985 : to combine the old value of LHS with RHS to get the new value.
9986 : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9987 :
9988 : C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9989 :
9990 : tree
9991 38292665 : cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9992 : tree rhs, tsubst_flags_t complain)
9993 : {
9994 38292665 : lhs = mark_lvalue_use_nonread (lhs);
9995 :
9996 38292665 : tree result = NULL_TREE;
9997 38292665 : tree newrhs = rhs;
9998 38292665 : tree lhstype = TREE_TYPE (lhs);
9999 38292665 : tree olhs = lhs;
10000 38292665 : tree olhstype = lhstype;
10001 38292665 : bool plain_assign = (modifycode == NOP_EXPR);
10002 38292665 : bool compound_side_effects_p = false;
10003 38292665 : tree preeval = NULL_TREE;
10004 :
10005 : /* Avoid duplicate error messages from operands that had errors. */
10006 38292665 : if (error_operand_p (lhs) || error_operand_p (rhs))
10007 19 : return error_mark_node;
10008 :
10009 38292728 : while (TREE_CODE (lhs) == COMPOUND_EXPR)
10010 : {
10011 82 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
10012 70 : compound_side_effects_p = true;
10013 82 : lhs = TREE_OPERAND (lhs, 1);
10014 : }
10015 :
10016 : /* Handle control structure constructs used as "lvalues". Note that we
10017 : leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
10018 38292646 : switch (TREE_CODE (lhs))
10019 : {
10020 : /* Handle --foo = 5; as these are valid constructs in C++. */
10021 27 : case PREDECREMENT_EXPR:
10022 27 : case PREINCREMENT_EXPR:
10023 27 : if (compound_side_effects_p)
10024 9 : newrhs = rhs = stabilize_expr (rhs, &preeval);
10025 27 : lhs = genericize_compound_lvalue (lhs);
10026 173 : maybe_add_compound:
10027 : /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
10028 : and looked through the COMPOUND_EXPRs, readd them now around
10029 : the resulting lhs. */
10030 173 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
10031 : {
10032 9 : lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
10033 9 : tree *ptr = &TREE_OPERAND (lhs, 1);
10034 9 : for (olhs = TREE_OPERAND (olhs, 1);
10035 12 : TREE_CODE (olhs) == COMPOUND_EXPR;
10036 3 : olhs = TREE_OPERAND (olhs, 1))
10037 : {
10038 3 : *ptr = build2 (COMPOUND_EXPR, lhstype,
10039 3 : TREE_OPERAND (olhs, 0), *ptr);
10040 3 : ptr = &TREE_OPERAND (*ptr, 1);
10041 : }
10042 : }
10043 : break;
10044 :
10045 146 : case MODIFY_EXPR:
10046 146 : if (compound_side_effects_p)
10047 0 : newrhs = rhs = stabilize_expr (rhs, &preeval);
10048 146 : lhs = genericize_compound_lvalue (lhs);
10049 146 : goto maybe_add_compound;
10050 :
10051 0 : case MIN_EXPR:
10052 0 : case MAX_EXPR:
10053 : /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
10054 : when neither operand has side-effects. */
10055 0 : if (!lvalue_or_else (lhs, lv_assign, complain))
10056 0 : return error_mark_node;
10057 :
10058 0 : gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
10059 : && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
10060 :
10061 0 : lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
10062 0 : build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
10063 : boolean_type_node,
10064 0 : TREE_OPERAND (lhs, 0),
10065 0 : TREE_OPERAND (lhs, 1)),
10066 0 : TREE_OPERAND (lhs, 0),
10067 0 : TREE_OPERAND (lhs, 1));
10068 212 : gcc_fallthrough ();
10069 :
10070 : /* Handle (a ? b : c) used as an "lvalue". */
10071 212 : case COND_EXPR:
10072 212 : {
10073 : /* Produce (a ? (b = rhs) : (c = rhs))
10074 : except that the RHS goes through a save-expr
10075 : so the code to compute it is only emitted once. */
10076 212 : if (VOID_TYPE_P (TREE_TYPE (rhs)))
10077 : {
10078 18 : if (complain & tf_error)
10079 24 : error_at (cp_expr_loc_or_loc (rhs, loc),
10080 : "void value not ignored as it ought to be");
10081 18 : return error_mark_node;
10082 : }
10083 :
10084 194 : rhs = stabilize_expr (rhs, &preeval);
10085 :
10086 : /* Check this here to avoid odd errors when trying to convert
10087 : a throw to the type of the COND_EXPR. */
10088 194 : if (!lvalue_or_else (lhs, lv_assign, complain))
10089 6 : return error_mark_node;
10090 :
10091 188 : tree op1 = TREE_OPERAND (lhs, 1);
10092 188 : if (TREE_CODE (op1) != THROW_EXPR)
10093 182 : op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
10094 : /* When sanitizing undefined behavior, even when rhs doesn't need
10095 : stabilization at this point, the sanitization might add extra
10096 : SAVE_EXPRs in there and so make sure there is no tree sharing
10097 : in the rhs, otherwise those SAVE_EXPRs will have initialization
10098 : only in one of the two branches. */
10099 188 : if (sanitize_flags_p (SANITIZE_UNDEFINED
10100 : | SANITIZE_UNDEFINED_NONDEFAULT))
10101 3 : rhs = unshare_expr (rhs);
10102 188 : tree op2 = TREE_OPERAND (lhs, 2);
10103 188 : if (TREE_CODE (op2) != THROW_EXPR)
10104 182 : op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
10105 188 : tree cond = build_conditional_expr (input_location,
10106 188 : TREE_OPERAND (lhs, 0), op1, op2,
10107 : complain);
10108 :
10109 188 : if (cond == error_mark_node)
10110 : return cond;
10111 : /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
10112 : and looked through the COMPOUND_EXPRs, readd them now around
10113 : the resulting cond before adding the preevaluated rhs. */
10114 185 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
10115 : {
10116 18 : cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
10117 18 : TREE_OPERAND (olhs, 0), cond);
10118 18 : tree *ptr = &TREE_OPERAND (cond, 1);
10119 18 : for (olhs = TREE_OPERAND (olhs, 1);
10120 21 : TREE_CODE (olhs) == COMPOUND_EXPR;
10121 3 : olhs = TREE_OPERAND (olhs, 1))
10122 : {
10123 3 : *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
10124 3 : TREE_OPERAND (olhs, 0), *ptr);
10125 3 : ptr = &TREE_OPERAND (*ptr, 1);
10126 : }
10127 : }
10128 : /* Make sure the code to compute the rhs comes out
10129 : before the split. */
10130 185 : result = cond;
10131 185 : goto ret;
10132 : }
10133 :
10134 : default:
10135 : lhs = olhs;
10136 : break;
10137 : }
10138 :
10139 38292434 : if (modifycode == INIT_EXPR)
10140 : {
10141 4302613 : if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10142 : /* Do the default thing. */;
10143 4097551 : else if (TREE_CODE (rhs) == CONSTRUCTOR)
10144 : {
10145 : /* Compound literal. */
10146 98 : if (! same_type_p (TREE_TYPE (rhs), lhstype))
10147 : /* Call convert to generate an error; see PR 11063. */
10148 0 : rhs = convert (lhstype, rhs);
10149 98 : result = cp_build_init_expr (lhs, rhs);
10150 98 : TREE_SIDE_EFFECTS (result) = 1;
10151 98 : goto ret;
10152 : }
10153 4097453 : else if (! MAYBE_CLASS_TYPE_P (lhstype))
10154 : /* Do the default thing. */;
10155 : else
10156 : {
10157 41511 : releasing_vec rhs_vec = make_tree_vector_single (rhs);
10158 41511 : result = build_special_member_call (lhs, complete_ctor_identifier,
10159 : &rhs_vec, lhstype, LOOKUP_NORMAL,
10160 : complain);
10161 41511 : if (result == NULL_TREE)
10162 0 : return error_mark_node;
10163 41511 : goto ret;
10164 41511 : }
10165 : }
10166 : else
10167 : {
10168 33989821 : lhs = require_complete_type (lhs, complain);
10169 33989821 : if (lhs == error_mark_node)
10170 : return error_mark_node;
10171 :
10172 33989655 : if (modifycode == NOP_EXPR)
10173 : {
10174 29198638 : maybe_warn_self_move (loc, lhs, rhs);
10175 :
10176 29198638 : if (c_dialect_objc ())
10177 : {
10178 0 : result = objc_maybe_build_modify_expr (lhs, rhs);
10179 0 : if (result)
10180 0 : goto ret;
10181 : }
10182 :
10183 : /* `operator=' is not an inheritable operator. */
10184 29198638 : if (! MAYBE_CLASS_TYPE_P (lhstype))
10185 : /* Do the default thing. */;
10186 : else
10187 : {
10188 3573424 : result = build_new_op (input_location, MODIFY_EXPR,
10189 : LOOKUP_NORMAL, lhs, rhs,
10190 : make_node (NOP_EXPR), NULL_TREE,
10191 : /*overload=*/NULL, complain);
10192 3573424 : if (result == NULL_TREE)
10193 0 : return error_mark_node;
10194 3573424 : goto ret;
10195 : }
10196 : lhstype = olhstype;
10197 : }
10198 : else
10199 : {
10200 4791017 : tree init = NULL_TREE;
10201 :
10202 : /* A binary op has been requested. Combine the old LHS
10203 : value with the RHS producing the value we should actually
10204 : store into the LHS. */
10205 4791017 : gcc_assert (!((TYPE_REF_P (lhstype)
10206 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
10207 : || MAYBE_CLASS_TYPE_P (lhstype)));
10208 :
10209 : /* Preevaluate the RHS to make sure its evaluation is complete
10210 : before the lvalue-to-rvalue conversion of the LHS:
10211 :
10212 : [expr.ass] With respect to an indeterminately-sequenced
10213 : function call, the operation of a compound assignment is a
10214 : single evaluation. [ Note: Therefore, a function call shall
10215 : not intervene between the lvalue-to-rvalue conversion and the
10216 : side effect associated with any single compound assignment
10217 : operator. -- end note ] */
10218 4791017 : lhs = cp_stabilize_reference (lhs);
10219 4791017 : rhs = decay_conversion (rhs, complain);
10220 4791017 : if (rhs == error_mark_node)
10221 123 : return error_mark_node;
10222 :
10223 4791014 : {
10224 4791014 : auto_diagnostic_group d;
10225 4791014 : rhs = stabilize_expr (rhs, &init);
10226 4791014 : bool clear_decl_read = false;
10227 4791014 : tree stripped_lhs = tree_strip_any_location_wrapper (lhs);
10228 1435133 : if ((VAR_P (stripped_lhs) || TREE_CODE (stripped_lhs) == PARM_DECL)
10229 4044932 : && !DECL_READ_P (stripped_lhs)
10230 1054017 : && (VAR_P (stripped_lhs) ? warn_unused_but_set_variable
10231 : : warn_unused_but_set_parameter) > 2
10232 7161 : && !CLASS_TYPE_P (TREE_TYPE (lhs))
10233 4798175 : && !CLASS_TYPE_P (TREE_TYPE (rhs)))
10234 : {
10235 7161 : mark_exp_read (rhs);
10236 7161 : if (!DECL_READ_P (stripped_lhs))
10237 4791014 : clear_decl_read = true;
10238 : }
10239 4791014 : newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
10240 4791014 : if (clear_decl_read)
10241 7161 : DECL_READ_P (stripped_lhs) = 0;
10242 4791014 : if (newrhs == error_mark_node)
10243 : {
10244 120 : if (complain & tf_error)
10245 24 : inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
10246 24 : modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
10247 120 : return error_mark_node;
10248 : }
10249 4791014 : }
10250 :
10251 4790894 : if (init)
10252 294169 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
10253 :
10254 : /* Now it looks like a plain assignment. */
10255 4790894 : modifycode = NOP_EXPR;
10256 4790894 : if (c_dialect_objc ())
10257 : {
10258 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
10259 0 : if (result)
10260 0 : goto ret;
10261 : }
10262 : }
10263 30416108 : gcc_assert (!TYPE_REF_P (lhstype));
10264 30416108 : gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
10265 : }
10266 :
10267 : /* The left-hand side must be an lvalue. */
10268 34677112 : if (!lvalue_or_else (lhs, lv_assign, complain))
10269 839 : return error_mark_node;
10270 :
10271 : /* Warn about modifying something that is `const'. Don't warn if
10272 : this is initialization. */
10273 34676273 : if (modifycode != INIT_EXPR
10274 34676273 : && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
10275 : /* Functions are not modifiable, even though they are
10276 : lvalues. */
10277 30372971 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
10278 : /* If it's an aggregate and any field is const, then it is
10279 : effectively const. */
10280 30372893 : || (CLASS_TYPE_P (lhstype)
10281 0 : && C_TYPE_FIELDS_READONLY (lhstype))))
10282 : {
10283 42376 : if (complain & tf_error)
10284 89 : cxx_readonly_error (loc, lhs, lv_assign);
10285 42376 : return error_mark_node;
10286 : }
10287 :
10288 : /* If storing into a structure or union member, it may have been given a
10289 : lowered bitfield type. We need to convert to the declared type first,
10290 : so retrieve it now. */
10291 :
10292 34633897 : olhstype = unlowered_expr_type (lhs);
10293 :
10294 : /* Convert new value to destination type. */
10295 :
10296 34633897 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
10297 : {
10298 222 : int from_array;
10299 :
10300 222 : if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
10301 : {
10302 12 : if (modifycode != INIT_EXPR)
10303 : {
10304 12 : if (complain & tf_error)
10305 12 : error_at (loc,
10306 : "assigning to an array from an initializer list");
10307 12 : return error_mark_node;
10308 : }
10309 0 : if (check_array_initializer (lhs, lhstype, newrhs))
10310 0 : return error_mark_node;
10311 0 : newrhs = digest_init (lhstype, newrhs, complain);
10312 0 : if (newrhs == error_mark_node)
10313 : return error_mark_node;
10314 : }
10315 :
10316 : /* C++11 8.5/17: "If the destination type is an array of characters,
10317 : an array of char16_t, an array of char32_t, or an array of wchar_t,
10318 : and the initializer is a string literal...". */
10319 210 : else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
10320 : == STRING_CST)
10321 43 : && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
10322 253 : && modifycode == INIT_EXPR)
10323 : {
10324 28 : newrhs = digest_init (lhstype, newrhs, complain);
10325 28 : if (newrhs == error_mark_node)
10326 : return error_mark_node;
10327 : }
10328 :
10329 182 : else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
10330 : TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
10331 : {
10332 27 : if (complain & tf_error)
10333 12 : error_at (loc, "incompatible types in assignment of %qT to %qT",
10334 12 : TREE_TYPE (rhs), lhstype);
10335 27 : return error_mark_node;
10336 : }
10337 :
10338 : /* Allow array assignment in compiler-generated code. */
10339 155 : else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
10340 : /* OK, used by coroutines (co-await-initlist1.C). */;
10341 149 : else if (!current_function_decl
10342 149 : || !DECL_DEFAULTED_FN (current_function_decl))
10343 : {
10344 : /* This routine is used for both initialization and assignment.
10345 : Make sure the diagnostic message differentiates the context. */
10346 99 : if (complain & tf_error)
10347 : {
10348 30 : if (modifycode == INIT_EXPR)
10349 9 : error_at (loc, "array used as initializer");
10350 : else
10351 21 : error_at (loc, "invalid array assignment");
10352 : }
10353 99 : return error_mark_node;
10354 : }
10355 :
10356 131 : from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
10357 84 : ? 1 + (modifycode != INIT_EXPR) : 0;
10358 84 : result = build_vec_init (lhs, NULL_TREE, newrhs,
10359 : /*explicit_value_init_p=*/false,
10360 : from_array, complain);
10361 84 : goto ret;
10362 : }
10363 :
10364 34633675 : if (modifycode == INIT_EXPR)
10365 : /* Calls with INIT_EXPR are all direct-initialization, so don't set
10366 : LOOKUP_ONLYCONVERTING. */
10367 4260945 : newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
10368 : ICR_INIT, NULL_TREE, 0,
10369 : complain | tf_no_cleanup);
10370 : else
10371 30372730 : newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
10372 : NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
10373 :
10374 34633675 : if (!same_type_p (lhstype, olhstype))
10375 404455 : newrhs = cp_convert_and_check (lhstype, newrhs, complain);
10376 :
10377 34633675 : if (modifycode != INIT_EXPR)
10378 : {
10379 30372730 : if (TREE_CODE (newrhs) == CALL_EXPR
10380 30372730 : && TYPE_NEEDS_CONSTRUCTING (lhstype))
10381 0 : newrhs = build_cplus_new (lhstype, newrhs, complain);
10382 :
10383 : /* Can't initialize directly from a TARGET_EXPR, since that would
10384 : cause the lhs to be constructed twice, and possibly result in
10385 : accidental self-initialization. So we force the TARGET_EXPR to be
10386 : expanded without a target. */
10387 30372730 : if (TREE_CODE (newrhs) == TARGET_EXPR)
10388 118 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
10389 118 : TARGET_EXPR_SLOT (newrhs));
10390 : }
10391 :
10392 34633675 : if (newrhs == error_mark_node)
10393 : return error_mark_node;
10394 :
10395 34632973 : if (c_dialect_objc () && flag_objc_gc)
10396 : {
10397 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
10398 :
10399 0 : if (result)
10400 0 : goto ret;
10401 : }
10402 :
10403 38893869 : result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
10404 : lhstype, lhs, newrhs);
10405 34632973 : if (modifycode == INIT_EXPR)
10406 4260896 : set_target_expr_eliding (newrhs);
10407 :
10408 34632973 : TREE_SIDE_EFFECTS (result) = 1;
10409 34632973 : if (!plain_assign)
10410 9051760 : suppress_warning (result, OPT_Wparentheses);
10411 :
10412 25581213 : ret:
10413 38248275 : if (preeval)
10414 39 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
10415 : return result;
10416 : }
10417 :
10418 : cp_expr
10419 71683427 : build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
10420 : tree rhs, tree lookups, tsubst_flags_t complain)
10421 : {
10422 71683427 : tree orig_lhs = lhs;
10423 71683427 : tree orig_rhs = rhs;
10424 71683427 : tree overload = NULL_TREE;
10425 :
10426 71683427 : if (lhs == error_mark_node || rhs == error_mark_node)
10427 2173 : return cp_expr (error_mark_node, loc);
10428 :
10429 71681254 : tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
10430 :
10431 71681254 : if (processing_template_decl)
10432 : {
10433 50017703 : if (type_dependent_expression_p (lhs)
10434 50017703 : || type_dependent_expression_p (rhs))
10435 : {
10436 37551309 : tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
10437 37551309 : if (modifycode != NOP_EXPR)
10438 6370537 : TREE_TYPE (rval)
10439 12741074 : = build_dependent_operator_type (lookups, modifycode, true);
10440 37551309 : return rval;
10441 : }
10442 : }
10443 :
10444 34129945 : tree rval;
10445 34129945 : if (modifycode == NOP_EXPR)
10446 26433454 : rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
10447 : else
10448 7696491 : rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
10449 : lhs, rhs, op, lookups, &overload, complain);
10450 34129945 : if (rval == error_mark_node)
10451 7260 : return error_mark_node;
10452 34122685 : if (processing_template_decl)
10453 : {
10454 12466373 : if (overload != NULL_TREE)
10455 1954436 : return (build_min_non_dep_op_overload
10456 1954436 : (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
10457 :
10458 10511937 : return (build_min_non_dep
10459 10511937 : (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
10460 : }
10461 21656312 : return rval;
10462 : }
10463 :
10464 : /* Helper function for get_delta_difference which assumes FROM is a base
10465 : class of TO. Returns a delta for the conversion of pointer-to-member
10466 : of FROM to pointer-to-member of TO. If the conversion is invalid and
10467 : tf_error is not set in COMPLAIN returns error_mark_node, otherwise
10468 : returns zero. If FROM is not a base class of TO, returns NULL_TREE.
10469 : If C_CAST_P is true, this conversion is taking place as part of a
10470 : C-style cast. */
10471 :
10472 : static tree
10473 28989 : get_delta_difference_1 (tree from, tree to, bool c_cast_p,
10474 : tsubst_flags_t complain)
10475 : {
10476 28989 : tree binfo;
10477 28989 : base_kind kind;
10478 :
10479 57728 : binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
10480 : &kind, complain);
10481 :
10482 28989 : if (binfo == error_mark_node)
10483 : {
10484 51 : if (!(complain & tf_error))
10485 : return error_mark_node;
10486 :
10487 51 : inform (input_location, " in pointer to member function conversion");
10488 51 : return size_zero_node;
10489 : }
10490 28938 : else if (binfo)
10491 : {
10492 28790 : if (kind != bk_via_virtual)
10493 28697 : return BINFO_OFFSET (binfo);
10494 : else
10495 : /* FROM is a virtual base class of TO. Issue an error or warning
10496 : depending on whether or not this is a reinterpret cast. */
10497 : {
10498 93 : if (!(complain & tf_error))
10499 : return error_mark_node;
10500 :
10501 78 : error ("pointer to member conversion via virtual base %qT",
10502 78 : BINFO_TYPE (binfo_from_vbase (binfo)));
10503 :
10504 78 : return size_zero_node;
10505 : }
10506 : }
10507 : else
10508 : return NULL_TREE;
10509 : }
10510 :
10511 : /* Get difference in deltas for different pointer to member function
10512 : types. If the conversion is invalid and tf_error is not set in
10513 : COMPLAIN, returns error_mark_node, otherwise returns an integer
10514 : constant of type PTRDIFF_TYPE_NODE and its value is zero if the
10515 : conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
10516 : conversions as well. If C_CAST_P is true this conversion is taking
10517 : place as part of a C-style cast.
10518 :
10519 : Note that the naming of FROM and TO is kind of backwards; the return
10520 : value is what we add to a TO in order to get a FROM. They are named
10521 : this way because we call this function to find out how to convert from
10522 : a pointer to member of FROM to a pointer to member of TO. */
10523 :
10524 : static tree
10525 90609 : get_delta_difference (tree from, tree to,
10526 : bool allow_inverse_p,
10527 : bool c_cast_p, tsubst_flags_t complain)
10528 : {
10529 90609 : auto_diagnostic_group d;
10530 90609 : tree result;
10531 :
10532 90609 : if (same_type_ignoring_top_level_qualifiers_p (from, to))
10533 : /* Pointer to member of incomplete class is permitted*/
10534 61768 : result = size_zero_node;
10535 : else
10536 28841 : result = get_delta_difference_1 (from, to, c_cast_p, complain);
10537 :
10538 90609 : if (result == error_mark_node)
10539 : return error_mark_node;
10540 :
10541 90594 : if (!result)
10542 : {
10543 148 : if (!allow_inverse_p)
10544 : {
10545 0 : if (!(complain & tf_error))
10546 : return error_mark_node;
10547 :
10548 0 : error_not_base_type (from, to);
10549 0 : inform (input_location, " in pointer to member conversion");
10550 0 : result = size_zero_node;
10551 : }
10552 : else
10553 : {
10554 148 : result = get_delta_difference_1 (to, from, c_cast_p, complain);
10555 :
10556 148 : if (result == error_mark_node)
10557 : return error_mark_node;
10558 :
10559 148 : if (result)
10560 148 : result = size_diffop_loc (input_location,
10561 : size_zero_node, result);
10562 : else
10563 : {
10564 0 : if (!(complain & tf_error))
10565 : return error_mark_node;
10566 :
10567 0 : error_not_base_type (from, to);
10568 0 : inform (input_location, " in pointer to member conversion");
10569 0 : result = size_zero_node;
10570 : }
10571 : }
10572 : }
10573 :
10574 90594 : return convert_to_integer (ptrdiff_type_node, result);
10575 90609 : }
10576 :
10577 : /* Return a constructor for the pointer-to-member-function TYPE using
10578 : the other components as specified. */
10579 :
10580 : tree
10581 62017 : build_ptrmemfunc1 (tree type, tree delta, tree pfn)
10582 : {
10583 62017 : tree u = NULL_TREE;
10584 62017 : tree delta_field;
10585 62017 : tree pfn_field;
10586 62017 : vec<constructor_elt, va_gc> *v;
10587 :
10588 : /* Pull the FIELD_DECLs out of the type. */
10589 62017 : pfn_field = TYPE_FIELDS (type);
10590 62017 : delta_field = DECL_CHAIN (pfn_field);
10591 :
10592 : /* Make sure DELTA has the type we want. */
10593 62017 : delta = convert_and_check (input_location, delta_type_node, delta);
10594 :
10595 : /* Convert to the correct target type if necessary. */
10596 62017 : pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10597 :
10598 : /* Finish creating the initializer. */
10599 62017 : vec_alloc (v, 2);
10600 62017 : CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10601 62017 : CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10602 62017 : u = build_constructor (type, v);
10603 62017 : TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10604 62017 : TREE_STATIC (u) = (TREE_CONSTANT (u)
10605 61879 : && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10606 : != NULL_TREE)
10607 123896 : && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10608 : != NULL_TREE));
10609 62017 : return u;
10610 : }
10611 :
10612 : /* Build a constructor for a pointer to member function. It can be
10613 : used to initialize global variables, local variable, or used
10614 : as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10615 : want to be.
10616 :
10617 : If FORCE is nonzero, then force this conversion, even if
10618 : we would rather not do it. Usually set when using an explicit
10619 : cast. A C-style cast is being processed iff C_CAST_P is true.
10620 :
10621 : Return error_mark_node, if something goes wrong. */
10622 :
10623 : tree
10624 30362 : build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10625 : tsubst_flags_t complain)
10626 : {
10627 30362 : tree fn;
10628 30362 : tree pfn_type;
10629 30362 : tree to_type;
10630 :
10631 30362 : if (error_operand_p (pfn))
10632 0 : return error_mark_node;
10633 :
10634 30362 : pfn_type = TREE_TYPE (pfn);
10635 30362 : to_type = build_ptrmemfunc_type (type);
10636 :
10637 : /* Handle multiple conversions of pointer to member functions. */
10638 30362 : if (TYPE_PTRMEMFUNC_P (pfn_type))
10639 : {
10640 28556 : tree delta = NULL_TREE;
10641 28556 : tree npfn = NULL_TREE;
10642 28556 : tree n;
10643 :
10644 28556 : if (!force
10645 28556 : && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
10646 : LOOKUP_NORMAL, complain))
10647 : {
10648 0 : if (complain & tf_error)
10649 0 : error ("invalid conversion to type %qT from type %qT",
10650 : to_type, pfn_type);
10651 : else
10652 0 : return error_mark_node;
10653 : }
10654 :
10655 28556 : n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10656 28556 : TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10657 : force,
10658 : c_cast_p, complain);
10659 28556 : if (n == error_mark_node)
10660 : return error_mark_node;
10661 :
10662 28547 : STRIP_ANY_LOCATION_WRAPPER (pfn);
10663 :
10664 : /* We don't have to do any conversion to convert a
10665 : pointer-to-member to its own type. But, we don't want to
10666 : just return a PTRMEM_CST if there's an explicit cast; that
10667 : cast should make the expression an invalid template argument. */
10668 28547 : if (TREE_CODE (pfn) != PTRMEM_CST
10669 28547 : && same_type_p (to_type, pfn_type))
10670 : return pfn;
10671 :
10672 28547 : if (TREE_SIDE_EFFECTS (pfn))
10673 18 : pfn = save_expr (pfn);
10674 :
10675 : /* Obtain the function pointer and the current DELTA. */
10676 28547 : if (TREE_CODE (pfn) == PTRMEM_CST)
10677 28433 : expand_ptrmemfunc_cst (pfn, &delta, &npfn);
10678 : else
10679 : {
10680 114 : npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
10681 114 : delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
10682 : }
10683 :
10684 : /* Just adjust the DELTA field. */
10685 28547 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10686 : (TREE_TYPE (delta), ptrdiff_type_node));
10687 28547 : if (!integer_zerop (n))
10688 : {
10689 56 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
10690 : n = cp_build_binary_op (input_location,
10691 : LSHIFT_EXPR, n, integer_one_node,
10692 : complain);
10693 56 : delta = cp_build_binary_op (input_location,
10694 : PLUS_EXPR, delta, n, complain);
10695 : }
10696 28547 : return build_ptrmemfunc1 (to_type, delta, npfn);
10697 : }
10698 :
10699 : /* Handle null pointer to member function conversions. */
10700 1806 : if (null_ptr_cst_p (pfn))
10701 : {
10702 674 : pfn = cp_build_c_cast (input_location,
10703 674 : TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10704 : pfn, complain);
10705 674 : return build_ptrmemfunc1 (to_type,
10706 : integer_zero_node,
10707 674 : pfn);
10708 : }
10709 :
10710 1132 : if (type_unknown_p (pfn))
10711 0 : return instantiate_type (type, pfn, complain);
10712 :
10713 1132 : fn = TREE_OPERAND (pfn, 0);
10714 1132 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10715 : /* In a template, we will have preserved the
10716 : OFFSET_REF. */
10717 : || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10718 1132 : return make_ptrmem_cst (to_type, fn);
10719 : }
10720 :
10721 : /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10722 : given by CST.
10723 :
10724 : ??? There is no consistency as to the types returned for the above
10725 : values. Some code acts as if it were a sizetype and some as if it were
10726 : integer_type_node. */
10727 :
10728 : void
10729 61635 : expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10730 : {
10731 61635 : tree type = TREE_TYPE (cst);
10732 61635 : tree fn = PTRMEM_CST_MEMBER (cst);
10733 61635 : tree ptr_class, fn_class;
10734 :
10735 61635 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10736 :
10737 : /* The class that the function belongs to. */
10738 61635 : fn_class = DECL_CONTEXT (fn);
10739 :
10740 : /* The class that we're creating a pointer to member of. */
10741 61635 : ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10742 :
10743 : /* First, calculate the adjustment to the function's class. */
10744 61635 : *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10745 : /*c_cast_p=*/0, tf_warning_or_error);
10746 :
10747 61635 : if (!DECL_VIRTUAL_P (fn))
10748 : {
10749 60518 : tree t = build_addr_func (fn, tf_warning_or_error);
10750 60518 : if (TREE_CODE (t) == ADDR_EXPR)
10751 60518 : SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10752 60518 : *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10753 : }
10754 : else
10755 : {
10756 : /* If we're dealing with a virtual function, we have to adjust 'this'
10757 : again, to point to the base which provides the vtable entry for
10758 : fn; the call will do the opposite adjustment. */
10759 1117 : tree orig_class = DECL_CONTEXT (fn);
10760 1117 : tree binfo = binfo_or_else (orig_class, fn_class);
10761 1117 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10762 : *delta, BINFO_OFFSET (binfo));
10763 :
10764 : /* We set PFN to the vtable offset at which the function can be
10765 : found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10766 : case delta is shifted left, and then incremented). */
10767 1117 : *pfn = DECL_VINDEX (fn);
10768 1117 : *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10769 : TYPE_SIZE_UNIT (vtable_entry_type));
10770 :
10771 1117 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10772 : {
10773 1117 : case ptrmemfunc_vbit_in_pfn:
10774 1117 : *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10775 : integer_one_node);
10776 1117 : break;
10777 :
10778 : case ptrmemfunc_vbit_in_delta:
10779 : *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10780 : *delta, integer_one_node);
10781 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10782 : *delta, integer_one_node);
10783 : break;
10784 :
10785 : default:
10786 : gcc_unreachable ();
10787 : }
10788 :
10789 1117 : *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10790 : }
10791 61635 : }
10792 :
10793 : /* Return an expression for PFN from the pointer-to-member function
10794 : given by T. */
10795 :
10796 : static tree
10797 86858 : pfn_from_ptrmemfunc (tree t)
10798 : {
10799 86858 : if (TREE_CODE (t) == PTRMEM_CST)
10800 : {
10801 203 : tree delta;
10802 203 : tree pfn;
10803 :
10804 203 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10805 203 : if (pfn)
10806 203 : return pfn;
10807 : }
10808 :
10809 86655 : return build_ptrmemfunc_access_expr (t, pfn_identifier);
10810 : }
10811 :
10812 : /* Return an expression for DELTA from the pointer-to-member function
10813 : given by T. */
10814 :
10815 : static tree
10816 86858 : delta_from_ptrmemfunc (tree t)
10817 : {
10818 86858 : if (TREE_CODE (t) == PTRMEM_CST)
10819 : {
10820 203 : tree delta;
10821 203 : tree pfn;
10822 :
10823 203 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10824 203 : if (delta)
10825 203 : return delta;
10826 : }
10827 :
10828 86655 : return build_ptrmemfunc_access_expr (t, delta_identifier);
10829 : }
10830 :
10831 : /* Convert value RHS to type TYPE as preparation for an assignment to
10832 : an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10833 : implicit conversion is. If FNDECL is non-NULL, we are doing the
10834 : conversion in order to pass the PARMNUMth argument of FNDECL.
10835 : If FNDECL is NULL, we are doing the conversion in function pointer
10836 : argument passing, conversion in initialization, etc. */
10837 :
10838 : static tree
10839 184366918 : convert_for_assignment (tree type, tree rhs,
10840 : impl_conv_rhs errtype, tree fndecl, int parmnum,
10841 : tsubst_flags_t complain, int flags)
10842 : {
10843 184366918 : tree rhstype;
10844 184366918 : enum tree_code coder;
10845 :
10846 184366918 : location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10847 184366918 : bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10848 : /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10849 : but preserve location wrappers. */
10850 184366918 : if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10851 184366918 : && !location_wrapper_p (rhs))
10852 8530 : rhs = TREE_OPERAND (rhs, 0);
10853 :
10854 : /* Handle [dcl.init.list] direct-list-initialization from
10855 : single element of enumeration with a fixed underlying type. */
10856 184366918 : if (is_direct_enum_init (type, rhs))
10857 : {
10858 20 : tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10859 20 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10860 : {
10861 11 : warning_sentinel w (warn_useless_cast);
10862 11 : warning_sentinel w2 (warn_ignored_qualifiers);
10863 11 : rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10864 11 : }
10865 : else
10866 9 : rhs = error_mark_node;
10867 : }
10868 :
10869 184366918 : rhstype = TREE_TYPE (rhs);
10870 184366918 : coder = TREE_CODE (rhstype);
10871 :
10872 1382050 : if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10873 185748814 : && vector_types_convertible_p (type, rhstype, true))
10874 : {
10875 1381887 : rhs = mark_rvalue_use (rhs);
10876 1381887 : return convert (type, rhs);
10877 : }
10878 :
10879 182985031 : if (rhs == error_mark_node || rhstype == error_mark_node)
10880 : return error_mark_node;
10881 182985022 : if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10882 : return error_mark_node;
10883 :
10884 : /* The RHS of an assignment cannot have void type. */
10885 182985022 : if (coder == VOID_TYPE)
10886 : {
10887 50 : if (complain & tf_error)
10888 30 : error_at (rhs_loc, "void value not ignored as it ought to be");
10889 50 : return error_mark_node;
10890 : }
10891 :
10892 182984972 : if (c_dialect_objc ())
10893 : {
10894 0 : int parmno;
10895 0 : tree selector;
10896 0 : tree rname = fndecl;
10897 :
10898 0 : switch (errtype)
10899 : {
10900 : case ICR_ASSIGN:
10901 : parmno = -1;
10902 : break;
10903 0 : case ICR_INIT:
10904 0 : parmno = -2;
10905 0 : break;
10906 0 : default:
10907 0 : selector = objc_message_selector ();
10908 0 : parmno = parmnum;
10909 0 : if (selector && parmno > 1)
10910 : {
10911 0 : rname = selector;
10912 0 : parmno -= 1;
10913 : }
10914 : }
10915 :
10916 0 : if (objc_compare_types (type, rhstype, parmno, rname))
10917 : {
10918 0 : rhs = mark_rvalue_use (rhs);
10919 0 : return convert (type, rhs);
10920 : }
10921 : }
10922 :
10923 : /* [expr.ass]
10924 :
10925 : The expression is implicitly converted (clause _conv_) to the
10926 : cv-unqualified type of the left operand.
10927 :
10928 : We allow bad conversions here because by the time we get to this point
10929 : we are committed to doing the conversion. If we end up doing a bad
10930 : conversion, convert_like will complain. */
10931 182984972 : if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10932 : {
10933 : /* When -Wno-pmf-conversions is use, we just silently allow
10934 : conversions from pointers-to-members to plain pointers. If
10935 : the conversion doesn't work, cp_convert will complain. */
10936 1688 : if (!warn_pmf2ptr
10937 9 : && TYPE_PTR_P (type)
10938 1697 : && TYPE_PTRMEMFUNC_P (rhstype))
10939 9 : rhs = cp_convert (strip_top_quals (type), rhs, complain);
10940 : else
10941 : {
10942 1679 : if (complain & tf_error)
10943 : {
10944 1364 : auto_diagnostic_group d;
10945 :
10946 : /* If the right-hand side has unknown type, then it is an
10947 : overloaded function. Call instantiate_type to get error
10948 : messages. */
10949 1364 : if (rhstype == unknown_type_node)
10950 : {
10951 225 : tree r = instantiate_type (type, rhs, tf_warning_or_error);
10952 : /* -fpermissive might allow this; recurse. */
10953 225 : if (!seen_error ())
10954 15 : return convert_for_assignment (type, r, errtype, fndecl,
10955 : parmnum, complain, flags);
10956 : }
10957 1139 : else if (fndecl)
10958 140 : complain_about_bad_argument (rhs_loc,
10959 : rhstype, type,
10960 : fndecl, parmnum);
10961 : else
10962 : {
10963 999 : range_label_for_type_mismatch label (rhstype, type);
10964 999 : gcc_rich_location richloc
10965 : (rhs_loc,
10966 : has_loc ? &label : NULL,
10967 999 : has_loc ? highlight_colors::percent_h : NULL);
10968 :
10969 999 : switch (errtype)
10970 : {
10971 0 : case ICR_DEFAULT_ARGUMENT:
10972 0 : error_at (&richloc,
10973 : "cannot convert %qH to %qI in default argument",
10974 : rhstype, type);
10975 0 : break;
10976 6 : case ICR_ARGPASS:
10977 6 : error_at (&richloc,
10978 : "cannot convert %qH to %qI in argument passing",
10979 : rhstype, type);
10980 6 : break;
10981 0 : case ICR_CONVERTING:
10982 0 : error_at (&richloc, "cannot convert %qH to %qI",
10983 : rhstype, type);
10984 0 : break;
10985 561 : case ICR_INIT:
10986 561 : error_at (&richloc,
10987 : "cannot convert %qH to %qI in initialization",
10988 : rhstype, type);
10989 561 : break;
10990 18 : case ICR_RETURN:
10991 18 : error_at (&richloc, "cannot convert %qH to %qI in return",
10992 : rhstype, type);
10993 18 : break;
10994 414 : case ICR_ASSIGN:
10995 414 : error_at (&richloc,
10996 : "cannot convert %qH to %qI in assignment",
10997 : rhstype, type);
10998 414 : break;
10999 0 : default:
11000 0 : gcc_unreachable();
11001 : }
11002 999 : }
11003 :
11004 : /* See if we can be more helpful. */
11005 1349 : maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
11006 :
11007 1349 : if (TYPE_PTR_P (rhstype)
11008 413 : && TYPE_PTR_P (type)
11009 398 : && CLASS_TYPE_P (TREE_TYPE (rhstype))
11010 280 : && CLASS_TYPE_P (TREE_TYPE (type))
11011 1413 : && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
11012 3 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
11013 : (TREE_TYPE (rhstype))),
11014 3 : "class type %qT is incomplete", TREE_TYPE (rhstype));
11015 1364 : }
11016 1664 : return error_mark_node;
11017 : }
11018 : }
11019 182983293 : if (warn_suggest_attribute_format)
11020 : {
11021 2383 : const enum tree_code codel = TREE_CODE (type);
11022 2383 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
11023 596 : && coder == codel
11024 508 : && check_missing_format_attribute (type, rhstype)
11025 2401 : && (complain & tf_warning))
11026 18 : switch (errtype)
11027 : {
11028 0 : case ICR_ARGPASS:
11029 0 : case ICR_DEFAULT_ARGUMENT:
11030 0 : if (fndecl)
11031 0 : warning (OPT_Wsuggest_attribute_format,
11032 : "parameter %qP of %qD might be a candidate "
11033 : "for a format attribute", parmnum, fndecl);
11034 : else
11035 0 : warning (OPT_Wsuggest_attribute_format,
11036 : "parameter might be a candidate "
11037 : "for a format attribute");
11038 : break;
11039 0 : case ICR_CONVERTING:
11040 0 : warning (OPT_Wsuggest_attribute_format,
11041 : "target of conversion might be a candidate "
11042 : "for a format attribute");
11043 0 : break;
11044 6 : case ICR_INIT:
11045 6 : warning (OPT_Wsuggest_attribute_format,
11046 : "target of initialization might be a candidate "
11047 : "for a format attribute");
11048 6 : break;
11049 6 : case ICR_RETURN:
11050 6 : warning (OPT_Wsuggest_attribute_format,
11051 : "return type might be a candidate "
11052 : "for a format attribute");
11053 6 : break;
11054 6 : case ICR_ASSIGN:
11055 6 : warning (OPT_Wsuggest_attribute_format,
11056 : "left-hand side of assignment might be a candidate "
11057 : "for a format attribute");
11058 6 : break;
11059 0 : default:
11060 0 : gcc_unreachable();
11061 : }
11062 : }
11063 :
11064 182983293 : if (TREE_CODE (type) == BOOLEAN_TYPE)
11065 37693559 : maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
11066 :
11067 182983293 : if (complain & tf_warning)
11068 180409908 : warn_for_address_of_packed_member (type, rhs);
11069 :
11070 182983293 : return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
11071 182983293 : complain, flags);
11072 : }
11073 :
11074 : /* Convert RHS to be of type TYPE.
11075 : If EXP is nonzero, it is the target of the initialization.
11076 : ERRTYPE indicates what kind of error the implicit conversion is.
11077 :
11078 : Two major differences between the behavior of
11079 : `convert_for_assignment' and `convert_for_initialization'
11080 : are that references are bashed in the former, while
11081 : copied in the latter, and aggregates are assigned in
11082 : the former (operator=) while initialized in the
11083 : latter (X(X&)).
11084 :
11085 : If using constructor make sure no conversion operator exists, if one does
11086 : exist, an ambiguity exists. */
11087 :
11088 : tree
11089 170532592 : convert_for_initialization (tree exp, tree type, tree rhs, int flags,
11090 : impl_conv_rhs errtype, tree fndecl, int parmnum,
11091 : tsubst_flags_t complain)
11092 : {
11093 170532592 : enum tree_code codel = TREE_CODE (type);
11094 170532592 : tree rhstype;
11095 170532592 : enum tree_code coder;
11096 :
11097 : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
11098 : Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
11099 170532592 : if (TREE_CODE (rhs) == NOP_EXPR
11100 5942022 : && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
11101 171023482 : && codel != REFERENCE_TYPE)
11102 490890 : rhs = TREE_OPERAND (rhs, 0);
11103 :
11104 170532592 : if (type == error_mark_node
11105 170532549 : || rhs == error_mark_node
11106 341065072 : || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
11107 : return error_mark_node;
11108 :
11109 170532480 : if (MAYBE_CLASS_TYPE_P (non_reference (type)))
11110 : ;
11111 155966514 : else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
11112 2390466 : && TREE_CODE (type) != ARRAY_TYPE
11113 2390466 : && (!TYPE_REF_P (type)
11114 1168 : || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
11115 153577213 : || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
11116 26252 : && !TYPE_REFFN_P (type))
11117 309518706 : || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
11118 2414328 : rhs = decay_conversion (rhs, complain);
11119 :
11120 170532480 : rhstype = TREE_TYPE (rhs);
11121 170532480 : coder = TREE_CODE (rhstype);
11122 :
11123 170532480 : if (coder == ERROR_MARK)
11124 9 : return error_mark_node;
11125 :
11126 : /* We accept references to incomplete types, so we can
11127 : return here before checking if RHS is of complete type. */
11128 :
11129 170532471 : if (codel == REFERENCE_TYPE)
11130 : {
11131 6053743 : auto_diagnostic_group d;
11132 : /* This should eventually happen in convert_arguments. */
11133 6053743 : int savew = 0, savee = 0;
11134 :
11135 6053743 : if (fndecl)
11136 297275 : savew = warningcount + werrorcount, savee = errorcount;
11137 6053743 : rhs = initialize_reference (type, rhs, flags, complain);
11138 :
11139 6053743 : if (fndecl
11140 6053743 : && (warningcount + werrorcount > savew || errorcount > savee))
11141 36 : inform (get_fndecl_argument_location (fndecl, parmnum),
11142 : "in passing argument %P of %qD", parmnum, fndecl);
11143 6053743 : return rhs;
11144 6053743 : }
11145 :
11146 164478728 : if (exp != 0)
11147 4260945 : exp = require_complete_type (exp, complain);
11148 164478728 : if (exp == error_mark_node)
11149 : return error_mark_node;
11150 :
11151 164478728 : type = complete_type (type);
11152 :
11153 164478728 : if (DIRECT_INIT_EXPR_P (type, rhs))
11154 : /* Don't try to do copy-initialization if we already have
11155 : direct-initialization. */
11156 : return rhs;
11157 :
11158 164477295 : if (MAYBE_CLASS_TYPE_P (type))
11159 10483122 : return perform_implicit_conversion_flags (type, rhs, complain, flags);
11160 :
11161 153994173 : return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
11162 153994173 : complain, flags);
11163 : }
11164 :
11165 : /* If RETVAL is the address of, or a reference to, a local variable or
11166 : temporary give an appropriate warning and return true. */
11167 :
11168 : static bool
11169 43828992 : maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
11170 : {
11171 43830023 : tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
11172 43830023 : tree whats_returned = fold_for_warn (retval);
11173 43830023 : if (!loc)
11174 43830023 : loc = cp_expr_loc_or_input_loc (retval);
11175 :
11176 53365260 : for (;;)
11177 : {
11178 53365260 : if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
11179 776401 : whats_returned = TREE_OPERAND (whats_returned, 1);
11180 52588859 : else if (CONVERT_EXPR_P (whats_returned)
11181 43847656 : || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
11182 8758836 : whats_returned = TREE_OPERAND (whats_returned, 0);
11183 : else
11184 : break;
11185 : }
11186 :
11187 43830023 : if (TREE_CODE (whats_returned) == TARGET_EXPR
11188 43830023 : && is_std_init_list (TREE_TYPE (whats_returned)))
11189 : {
11190 16 : tree init = TARGET_EXPR_INITIAL (whats_returned);
11191 16 : if (TREE_CODE (init) == CONSTRUCTOR)
11192 : /* Pull out the array address. */
11193 7 : whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
11194 9 : else if (TREE_CODE (init) == INDIRECT_REF)
11195 : /* The source of a trivial copy looks like *(T*)&var. */
11196 6 : whats_returned = TREE_OPERAND (init, 0);
11197 : else
11198 : return false;
11199 13 : STRIP_NOPS (whats_returned);
11200 : }
11201 :
11202 : /* As a special case, we handle a call to std::move or std::forward. */
11203 43830020 : if (TREE_CODE (whats_returned) == CALL_EXPR
11204 43830020 : && (is_std_move_p (whats_returned)
11205 10419200 : || is_std_forward_p (whats_returned)))
11206 : {
11207 1019 : tree arg = CALL_EXPR_ARG (whats_returned, 0);
11208 1019 : return maybe_warn_about_returning_address_of_local (arg, loc);
11209 : }
11210 :
11211 43829001 : if (TREE_CODE (whats_returned) != ADDR_EXPR)
11212 : return false;
11213 921491 : whats_returned = TREE_OPERAND (whats_returned, 0);
11214 :
11215 921491 : while (TREE_CODE (whats_returned) == COMPONENT_REF
11216 1789318 : || TREE_CODE (whats_returned) == ARRAY_REF)
11217 867827 : whats_returned = TREE_OPERAND (whats_returned, 0);
11218 :
11219 921491 : if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
11220 921491 : || TREE_CODE (whats_returned) == TARGET_EXPR)
11221 : {
11222 50 : if (TYPE_REF_P (valtype))
11223 : /* P2748 made this an error in C++26. */
11224 72 : emit_diagnostic ((cxx_dialect >= cxx26
11225 : ? diagnostics::kind::permerror
11226 : : diagnostics::kind::warning),
11227 41 : loc, OPT_Wreturn_local_addr,
11228 : "returning reference to temporary");
11229 9 : else if (TYPE_PTR_P (valtype))
11230 3 : warning_at (loc, OPT_Wreturn_local_addr,
11231 : "returning pointer to temporary");
11232 6 : else if (is_std_init_list (valtype))
11233 6 : warning_at (loc, OPT_Winit_list_lifetime,
11234 : "returning temporary %<initializer_list%> does not extend "
11235 : "the lifetime of the underlying array");
11236 : return true;
11237 : }
11238 :
11239 921441 : STRIP_ANY_LOCATION_WRAPPER (whats_returned);
11240 :
11241 921441 : if (DECL_P (whats_returned)
11242 120015 : && DECL_NAME (whats_returned)
11243 120015 : && DECL_FUNCTION_SCOPE_P (whats_returned)
11244 12405 : && !is_capture_proxy (whats_returned)
11245 933816 : && !(TREE_STATIC (whats_returned)
11246 161 : || TREE_PUBLIC (whats_returned)))
11247 : {
11248 114 : if (DECL_DECOMPOSITION_P (whats_returned)
11249 39 : && !DECL_DECOMP_IS_BASE (whats_returned)
11250 200 : && DECL_HAS_VALUE_EXPR_P (whats_returned))
11251 : {
11252 : /* When returning address of a structured binding, if the structured
11253 : binding is not a reference, continue normally, if it is a
11254 : reference, recurse on the initializer of the structured
11255 : binding. */
11256 39 : tree base = DECL_DECOMP_BASE (whats_returned);
11257 39 : if (TYPE_REF_P (TREE_TYPE (base)))
11258 : {
11259 15 : if (tree init = DECL_INITIAL (base))
11260 : return maybe_warn_about_returning_address_of_local (init, loc);
11261 : else
11262 : return false;
11263 : }
11264 : }
11265 146 : bool w = false;
11266 146 : auto_diagnostic_group d;
11267 146 : if (TYPE_REF_P (valtype))
11268 89 : w = warning_at (loc, OPT_Wreturn_local_addr,
11269 : "reference to local variable %qD returned",
11270 : whats_returned);
11271 57 : else if (is_std_init_list (valtype))
11272 3 : w = warning_at (loc, OPT_Winit_list_lifetime,
11273 : "returning local %<initializer_list%> variable %qD "
11274 : "does not extend the lifetime of the underlying array",
11275 : whats_returned);
11276 54 : else if (POINTER_TYPE_P (valtype)
11277 48 : && TREE_CODE (whats_returned) == LABEL_DECL)
11278 6 : w = warning_at (loc, OPT_Wreturn_local_addr,
11279 : "address of label %qD returned",
11280 : whats_returned);
11281 48 : else if (POINTER_TYPE_P (valtype))
11282 42 : w = warning_at (loc, OPT_Wreturn_local_addr,
11283 : "address of local variable %qD returned",
11284 : whats_returned);
11285 140 : if (w)
11286 117 : inform (DECL_SOURCE_LOCATION (whats_returned),
11287 : "declared here");
11288 146 : return true;
11289 146 : }
11290 :
11291 : return false;
11292 : }
11293 :
11294 : /* Returns true if DECL is in the std namespace. */
11295 :
11296 : bool
11297 88472456 : decl_in_std_namespace_p (tree decl)
11298 : {
11299 102658382 : while (decl)
11300 : {
11301 102239019 : decl = decl_namespace_context (decl);
11302 102239019 : if (DECL_NAMESPACE_STD_P (decl))
11303 : return true;
11304 : /* Allow inline namespaces inside of std namespace, e.g. with
11305 : --enable-symvers=gnu-versioned-namespace std::forward would be
11306 : actually std::_8::forward. */
11307 48658690 : if (!DECL_NAMESPACE_INLINE_P (decl))
11308 : return false;
11309 14185926 : decl = CP_DECL_CONTEXT (decl);
11310 : }
11311 : return false;
11312 : }
11313 :
11314 : /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
11315 :
11316 : static bool
11317 10419200 : is_std_forward_p (tree fn)
11318 : {
11319 : /* std::forward only takes one argument. */
11320 10419200 : if (call_expr_nargs (fn) != 1)
11321 : return false;
11322 :
11323 4843928 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
11324 4843928 : if (!decl_in_std_namespace_p (fndecl))
11325 : return false;
11326 :
11327 1821288 : tree name = DECL_NAME (fndecl);
11328 1821288 : return name && id_equal (name, "forward");
11329 : }
11330 :
11331 : /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
11332 :
11333 : static bool
11334 10426861 : is_std_move_p (tree fn)
11335 : {
11336 : /* std::move only takes one argument. */
11337 10426861 : if (call_expr_nargs (fn) != 1)
11338 : return false;
11339 :
11340 4850810 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
11341 4850810 : if (!decl_in_std_namespace_p (fndecl))
11342 : return false;
11343 :
11344 1827686 : tree name = DECL_NAME (fndecl);
11345 1827686 : return name && id_equal (name, "move");
11346 : }
11347 :
11348 : /* Returns true if RETVAL is a good candidate for the NRVO as per
11349 : [class.copy.elision]. FUNCTYPE is the type the function is declared
11350 : to return. */
11351 :
11352 : static bool
11353 56923788 : can_do_nrvo_p (tree retval, tree functype)
11354 : {
11355 56923788 : if (functype == error_mark_node)
11356 : return false;
11357 56923742 : if (retval)
11358 55246789 : STRIP_ANY_LOCATION_WRAPPER (retval);
11359 56923742 : tree result = DECL_RESULT (current_function_decl);
11360 56923742 : return (retval != NULL_TREE
11361 55246789 : && !processing_template_decl
11362 : /* Must be a local, automatic variable. */
11363 45328625 : && VAR_P (retval)
11364 3975983 : && DECL_CONTEXT (retval) == current_function_decl
11365 2902653 : && !TREE_STATIC (retval)
11366 : /* And not a lambda or anonymous union proxy. */
11367 2899804 : && !DECL_HAS_VALUE_EXPR_P (retval)
11368 2899522 : && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
11369 : /* The cv-unqualified type of the returned value must be the
11370 : same as the cv-unqualified return type of the
11371 : function. */
11372 2899393 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
11373 : TYPE_MAIN_VARIANT (functype))
11374 : /* And the returned value must be non-volatile. */
11375 59799463 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
11376 : }
11377 :
11378 : /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
11379 : would otherwise return in memory. */
11380 :
11381 : static bool
11382 56923418 : want_nrvo_p (tree retval, tree functype)
11383 : {
11384 56923418 : return (can_do_nrvo_p (retval, functype)
11385 56923418 : && aggregate_value_p (functype, current_function_decl));
11386 : }
11387 :
11388 : /* Like can_do_nrvo_p, but we check if we're trying to move a class
11389 : prvalue. */
11390 :
11391 : static bool
11392 1441 : can_elide_copy_prvalue_p (tree retval, tree functype)
11393 : {
11394 1441 : if (functype == error_mark_node)
11395 : return false;
11396 1441 : if (retval)
11397 1441 : STRIP_ANY_LOCATION_WRAPPER (retval);
11398 1441 : return (retval != NULL_TREE
11399 1441 : && !glvalue_p (retval)
11400 135 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
11401 : TYPE_MAIN_VARIANT (functype))
11402 1549 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
11403 : }
11404 :
11405 : /* If we should treat RETVAL, an expression being returned, as if it were
11406 : designated by an rvalue, returns it adjusted accordingly; otherwise, returns
11407 : NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
11408 : context (rather than throw). */
11409 :
11410 : tree
11411 11736752 : treat_lvalue_as_rvalue_p (tree expr, bool return_p)
11412 : {
11413 11736752 : if (cxx_dialect == cxx98)
11414 : return NULL_TREE;
11415 :
11416 11728797 : tree retval = expr;
11417 11728797 : STRIP_ANY_LOCATION_WRAPPER (retval);
11418 11728797 : if (REFERENCE_REF_P (retval))
11419 586923 : retval = TREE_OPERAND (retval, 0);
11420 :
11421 : /* An implicitly movable entity is a variable of automatic storage duration
11422 : that is either a non-volatile object or (C++20) an rvalue reference to a
11423 : non-volatile object type. */
11424 11728797 : if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
11425 10453286 : || TREE_CODE (retval) == PARM_DECL)
11426 1840539 : && !TREE_STATIC (retval)
11427 1746002 : && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
11428 1745997 : && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
11429 128836 : || (cxx_dialect >= cxx20
11430 128284 : && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
11431 : return NULL_TREE;
11432 :
11433 : /* If the expression in a return or co_return statement is a (possibly
11434 : parenthesized) id-expression that names an implicitly movable entity
11435 : declared in the body or parameter-declaration-clause of the innermost
11436 : enclosing function or lambda-expression, */
11437 1618008 : if (return_p)
11438 : {
11439 1608561 : if (DECL_CONTEXT (retval) != current_function_decl)
11440 : return NULL_TREE;
11441 1608494 : expr = move (expr);
11442 1608494 : if (expr == error_mark_node)
11443 : return NULL_TREE;
11444 1608492 : return set_implicit_rvalue_p (expr);
11445 : }
11446 :
11447 : /* if the id-expression (possibly parenthesized) is the operand of
11448 : a throw-expression, and names an implicitly movable entity that belongs
11449 : to a scope that does not contain the compound-statement of the innermost
11450 : lambda-expression, try-block, or function-try-block (if any) whose
11451 : compound-statement or ctor-initializer contains the throw-expression. */
11452 :
11453 : /* C++20 added move on throw of parms. */
11454 9447 : if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
11455 : return NULL_TREE;
11456 :
11457 : /* We don't check for lambda-expression here, because we should not get past
11458 : the DECL_HAS_VALUE_EXPR_P check above. */
11459 9431 : for (cp_binding_level *b = current_binding_level;
11460 9527 : b->kind != sk_namespace; b = b->level_chain)
11461 : {
11462 9525 : for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
11463 103 : if (decl == retval)
11464 91 : return set_implicit_rvalue_p (move (expr));
11465 9422 : if (b->kind == sk_try)
11466 : return NULL_TREE;
11467 : }
11468 :
11469 14 : return set_implicit_rvalue_p (move (expr));
11470 : }
11471 :
11472 : /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
11473 : is true). EXPR is the std::move expression; TYPE is the type of the object
11474 : being initialized. */
11475 :
11476 : void
11477 156579574 : maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
11478 : {
11479 156579574 : if (!(warn_pessimizing_move || warn_redundant_move))
11480 : return;
11481 :
11482 4630465 : const location_t loc = cp_expr_loc_or_input_loc (expr);
11483 :
11484 : /* C++98 doesn't know move. */
11485 4630465 : if (cxx_dialect < cxx11)
11486 : return;
11487 :
11488 : /* Wait until instantiation time, since we can't gauge if we should do
11489 : the NRVO until then. */
11490 4567772 : if (processing_template_decl)
11491 : return;
11492 :
11493 : /* This is only interesting for class types. */
11494 4478332 : if (!CLASS_TYPE_P (type))
11495 : return;
11496 :
11497 113124 : bool wrapped_p = false;
11498 : /* A a = std::move (A()); */
11499 113124 : if (TREE_CODE (expr) == TREE_LIST)
11500 : {
11501 8982 : if (list_length (expr) == 1)
11502 : {
11503 6001 : expr = TREE_VALUE (expr);
11504 6001 : wrapped_p = true;
11505 : }
11506 : else
11507 : return;
11508 : }
11509 : /* A a = {std::move (A())};
11510 : A a{std::move (A())}; */
11511 104142 : else if (TREE_CODE (expr) == CONSTRUCTOR)
11512 : {
11513 7969 : if (CONSTRUCTOR_NELTS (expr) == 1)
11514 : {
11515 890 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
11516 890 : wrapped_p = true;
11517 : }
11518 : else
11519 : return;
11520 : }
11521 :
11522 : /* First, check if this is a call to std::move. */
11523 6836 : if (!REFERENCE_REF_P (expr)
11524 107869 : || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
11525 : return;
11526 2710 : tree fn = TREE_OPERAND (expr, 0);
11527 2710 : if (!is_std_move_p (fn))
11528 : return;
11529 2084 : tree arg = CALL_EXPR_ARG (fn, 0);
11530 2084 : if (TREE_CODE (arg) != NOP_EXPR)
11531 : return;
11532 : /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
11533 : and implicitly-movable warnings. */
11534 2084 : if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
11535 : {
11536 1441 : arg = TREE_OPERAND (arg, 0);
11537 1441 : arg = TREE_OPERAND (arg, 0);
11538 1441 : arg = convert_from_reference (arg);
11539 1441 : if (can_elide_copy_prvalue_p (arg, type))
11540 : {
11541 108 : auto_diagnostic_group d;
11542 108 : if (warning_at (loc, OPT_Wpessimizing_move,
11543 : "moving a temporary object prevents copy elision"))
11544 108 : inform (loc, "remove %<std::move%> call");
11545 108 : }
11546 : /* The rest of the warnings is only relevant for when we are returning
11547 : from a function. */
11548 1441 : if (!return_p)
11549 : return;
11550 :
11551 370 : tree moved;
11552 : /* Warn if we could do copy elision were it not for the move. */
11553 370 : if (can_do_nrvo_p (arg, type))
11554 : {
11555 48 : auto_diagnostic_group d;
11556 48 : if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
11557 48 : && warning_at (loc, OPT_Wpessimizing_move,
11558 : "moving a local object in a return statement "
11559 : "prevents copy elision"))
11560 42 : inform (loc, "remove %<std::move%> call");
11561 48 : }
11562 : /* Warn if the move is redundant. It is redundant when we would
11563 : do maybe-rvalue overload resolution even without std::move. */
11564 322 : else if (warn_redundant_move
11565 : /* This doesn't apply for return {std::move (t)};. */
11566 224 : && !wrapped_p
11567 221 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11568 418 : && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
11569 : {
11570 : /* Make sure that overload resolution would actually succeed
11571 : if we removed the std::move call. */
11572 54 : tree t = convert_for_initialization (NULL_TREE, type,
11573 : moved,
11574 : (LOOKUP_NORMAL
11575 : | LOOKUP_ONLYCONVERTING),
11576 : ICR_RETURN, NULL_TREE, 0,
11577 : tf_none);
11578 : /* If this worked, implicit rvalue would work, so the call to
11579 : std::move is redundant. */
11580 54 : if (t != error_mark_node)
11581 : {
11582 54 : auto_diagnostic_group d;
11583 54 : if (warning_at (loc, OPT_Wredundant_move,
11584 : "redundant move in return statement"))
11585 54 : inform (loc, "remove %<std::move%> call");
11586 54 : }
11587 : }
11588 : }
11589 : /* Also try to warn about redundant std::move in code such as
11590 : T f (const T& t)
11591 : {
11592 : return std::move(t);
11593 : }
11594 : for which EXPR will be something like
11595 : *std::move<const T&> ((const struct T &) (const struct T *) t)
11596 : and where the std::move does nothing if T does not have a T(const T&&)
11597 : constructor, because the argument is const. It will not use T(T&&)
11598 : because that would mean losing the const. */
11599 643 : else if (warn_redundant_move
11600 521 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11601 66 : && TYPE_REF_P (TREE_TYPE (arg))
11602 709 : && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
11603 : {
11604 18 : tree rtype = TREE_TYPE (TREE_TYPE (arg));
11605 18 : if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
11606 9 : return;
11607 : /* Check for the unlikely case there's T(const T&&) (we don't care if
11608 : it's deleted). */
11609 54 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
11610 30 : if (move_fn_p (fn))
11611 : {
11612 15 : tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11613 15 : if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
11614 6 : return;
11615 : }
11616 9 : auto_diagnostic_group d;
11617 18 : if (return_p
11618 9 : ? warning_at (loc, OPT_Wredundant_move,
11619 : "redundant move in return statement")
11620 9 : : warning_at (loc, OPT_Wredundant_move,
11621 : "redundant move in initialization"))
11622 9 : inform (loc, "remove %<std::move%> call");
11623 9 : }
11624 : }
11625 :
11626 : /* Check that returning RETVAL from the current function is valid.
11627 : Return an expression explicitly showing all conversions required to
11628 : change RETVAL into the function return type, and to assign it to
11629 : the DECL_RESULT for the function. Set *NO_WARNING to true if
11630 : code reaches end of non-void function warning shouldn't be issued
11631 : on this RETURN_EXPR. Set *DANGLING to true if code returns the
11632 : address of a local variable. */
11633 :
11634 : tree
11635 131793375 : check_return_expr (tree retval, bool *no_warning, bool *dangling)
11636 : {
11637 131793375 : tree result;
11638 : /* The type actually returned by the function. */
11639 131793375 : tree valtype;
11640 : /* The type the function is declared to return, or void if
11641 : the declared type is incomplete. */
11642 131793375 : tree functype;
11643 131793375 : int fn_returns_value_p;
11644 131793375 : location_t loc = cp_expr_loc_or_input_loc (retval);
11645 :
11646 131793375 : *no_warning = false;
11647 131793375 : *dangling = false;
11648 :
11649 : /* A `volatile' function is one that isn't supposed to return, ever.
11650 : (This is a G++ extension, used to get better code for functions
11651 : that call the `volatile' function.) */
11652 131793375 : if (TREE_THIS_VOLATILE (current_function_decl))
11653 12 : warning (0, "function declared %<noreturn%> has a %<return%> statement");
11654 :
11655 : /* Check for various simple errors. */
11656 263586750 : if (DECL_DESTRUCTOR_P (current_function_decl))
11657 : {
11658 3554 : if (retval)
11659 3 : error_at (loc, "returning a value from a destructor");
11660 :
11661 3554 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11662 0 : retval = current_class_ptr;
11663 : else
11664 : return NULL_TREE;
11665 : }
11666 131789821 : else if (DECL_CONSTRUCTOR_P (current_function_decl))
11667 : {
11668 162798 : if (in_function_try_handler)
11669 : /* If a return statement appears in a handler of the
11670 : function-try-block of a constructor, the program is ill-formed. */
11671 0 : error ("cannot return from a handler of a function-try-block of a constructor");
11672 162798 : else if (retval)
11673 : /* You can't return a value from a constructor. */
11674 3 : error_at (loc, "returning a value from a constructor");
11675 :
11676 162798 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11677 0 : retval = current_class_ptr;
11678 : else
11679 : return NULL_TREE;
11680 : }
11681 :
11682 131627023 : const tree saved_retval = retval;
11683 :
11684 131627023 : if (processing_template_decl)
11685 : {
11686 : /* If in expansion statement body, we don't know if the body
11687 : will be instantiated at all. */
11688 85600839 : if (in_expansion_stmt)
11689 46 : goto dependent;
11690 :
11691 85600793 : current_function_returns_value = 1;
11692 :
11693 85600793 : if (check_for_bare_parameter_packs (retval))
11694 14 : return error_mark_node;
11695 :
11696 : /* If one of the types might be void, we can't tell whether we're
11697 : returning a value. */
11698 85600779 : if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11699 35930683 : && !FNDECL_USED_AUTO (current_function_decl))
11700 49670096 : || (retval != NULL_TREE
11701 48522228 : && (TREE_TYPE (retval) == NULL_TREE
11702 33533468 : || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11703 58174243 : goto dependent;
11704 : }
11705 :
11706 73452720 : functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11707 :
11708 : /* Deduce auto return type from a return statement. */
11709 73452720 : if (FNDECL_USED_AUTO (current_function_decl))
11710 : {
11711 1288719 : tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11712 1288719 : tree auto_node;
11713 1288719 : tree type;
11714 :
11715 1288719 : if (!retval && !is_auto (pattern))
11716 : {
11717 : /* Give a helpful error message. */
11718 3 : auto_diagnostic_group d;
11719 3 : error ("return-statement with no value, in function returning %qT",
11720 : pattern);
11721 3 : inform (input_location, "only plain %<auto%> return type can be "
11722 : "deduced to %<void%>");
11723 3 : type = error_mark_node;
11724 3 : }
11725 1288716 : else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11726 : {
11727 6 : error ("returning initializer list");
11728 6 : type = error_mark_node;
11729 : }
11730 : else
11731 : {
11732 1288710 : if (!retval)
11733 2588 : retval = void_node;
11734 1288710 : auto_node = type_uses_auto (pattern);
11735 1288710 : type = do_auto_deduction (pattern, retval, auto_node,
11736 : tf_warning_or_error, adc_return_type);
11737 : }
11738 :
11739 1288719 : if (type == error_mark_node)
11740 : /* Leave it. */;
11741 1288471 : else if (functype == pattern)
11742 1050896 : apply_deduced_return_type (current_function_decl, type);
11743 237575 : else if (!same_type_p (type, functype))
11744 : {
11745 24 : if (LAMBDA_FUNCTION_P (current_function_decl))
11746 6 : error_at (loc, "inconsistent types %qT and %qT deduced for "
11747 : "lambda return type", functype, type);
11748 : else
11749 12 : error_at (loc, "inconsistent deduction for auto return type: "
11750 : "%qT and then %qT", functype, type);
11751 : }
11752 : functype = type;
11753 : }
11754 :
11755 73452720 : result = DECL_RESULT (current_function_decl);
11756 73452720 : valtype = TREE_TYPE (result);
11757 73452720 : gcc_assert (valtype != NULL_TREE);
11758 73452720 : fn_returns_value_p = !VOID_TYPE_P (valtype);
11759 :
11760 : /* Check for a return statement with no return value in a function
11761 : that's supposed to return a value. */
11762 73452720 : if (!retval && fn_returns_value_p)
11763 : {
11764 33 : if (functype != error_mark_node)
11765 30 : permerror (input_location, "return-statement with no value, in "
11766 : "function returning %qT", valtype);
11767 : /* Remember that this function did return. */
11768 33 : current_function_returns_value = 1;
11769 : /* But suppress NRV .*/
11770 33 : current_function_return_value = error_mark_node;
11771 : /* And signal caller that TREE_NO_WARNING should be set on the
11772 : RETURN_EXPR to avoid control reaches end of non-void function
11773 : warnings in tree-cfg.cc. */
11774 33 : *no_warning = true;
11775 : }
11776 : /* Check for a return statement with a value in a function that
11777 : isn't supposed to return a value. */
11778 73452687 : else if (retval && !fn_returns_value_p)
11779 : {
11780 177387 : if (VOID_TYPE_P (TREE_TYPE (retval)))
11781 : /* You can return a `void' value from a function of `void'
11782 : type. In that case, we have to evaluate the expression for
11783 : its side-effects. */
11784 177348 : finish_expr_stmt (retval);
11785 39 : else if (retval != error_mark_node)
11786 36 : permerror (loc, "return-statement with a value, in function "
11787 : "returning %qT", valtype);
11788 177387 : current_function_returns_null = 1;
11789 :
11790 : /* There's really no value to return, after all. */
11791 177387 : return NULL_TREE;
11792 : }
11793 73275300 : else if (!retval)
11794 : /* Remember that this function can sometimes return without a
11795 : value. */
11796 1676938 : current_function_returns_null = 1;
11797 : else
11798 : /* Remember that this function did return a value. */
11799 71598362 : current_function_returns_value = 1;
11800 :
11801 : /* Check for erroneous operands -- but after giving ourselves a
11802 : chance to provide an error about returning a value from a void
11803 : function. */
11804 73275333 : if (error_operand_p (retval))
11805 : {
11806 1101 : current_function_return_value = error_mark_node;
11807 1101 : return error_mark_node;
11808 : }
11809 :
11810 : /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11811 146548464 : if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11812 32058 : && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11813 2675 : && ! flag_check_new
11814 73276898 : && retval && null_ptr_cst_p (retval))
11815 24 : warning (0, "%<operator new%> must not return NULL unless it is "
11816 : "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11817 :
11818 : /* Effective C++ rule 15. See also start_function. */
11819 73274232 : if (warn_ecpp
11820 42 : && DECL_NAME (current_function_decl) == assign_op_identifier
11821 73274262 : && !type_dependent_expression_p (retval))
11822 : {
11823 27 : bool warn = true;
11824 :
11825 : /* The function return type must be a reference to the current
11826 : class. */
11827 27 : if (TYPE_REF_P (valtype)
11828 45 : && same_type_ignoring_top_level_qualifiers_p
11829 18 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11830 : {
11831 : /* Returning '*this' is obviously OK. */
11832 18 : if (retval == current_class_ref)
11833 : warn = false;
11834 : /* If we are calling a function whose return type is the same of
11835 : the current class reference, it is ok. */
11836 6 : else if (INDIRECT_REF_P (retval)
11837 6 : && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11838 : warn = false;
11839 : }
11840 :
11841 : if (warn)
11842 9 : warning_at (loc, OPT_Weffc__,
11843 : "%<operator=%> should return a reference to %<*this%>");
11844 : }
11845 :
11846 73274232 : if (dependent_type_p (functype)
11847 73274232 : || type_dependent_expression_p (retval))
11848 : {
11849 74525103 : dependent:
11850 : /* We should not have changed the return value. */
11851 74525103 : gcc_assert (retval == saved_retval);
11852 : /* We don't know if this is an lvalue or rvalue use, but
11853 : either way we can mark it as read. */
11854 74525103 : mark_exp_read (retval);
11855 74525103 : return retval;
11856 : }
11857 :
11858 : /* The fabled Named Return Value optimization, as per [class.copy]/15:
11859 :
11860 : [...] For a function with a class return type, if the expression
11861 : in the return statement is the name of a local object, and the cv-
11862 : unqualified type of the local object is the same as the function
11863 : return type, an implementation is permitted to omit creating the tem-
11864 : porary object to hold the function return value [...]
11865 :
11866 : So, if this is a value-returning function that always returns the same
11867 : local variable, remember it.
11868 :
11869 : We choose the first suitable variable even if the function sometimes
11870 : returns something else, but only if the variable is out of scope at the
11871 : other return sites, or else we run the risk of clobbering the variable we
11872 : chose if the other returned expression uses the chosen variable somehow.
11873 :
11874 : We don't currently do this if the first return is a non-variable, as it
11875 : would be complicated to determine whether an NRV selected later was in
11876 : scope at the point of the earlier return. We also don't currently support
11877 : multiple variables with non-overlapping scopes (53637).
11878 :
11879 : See finish_function and finalize_nrv for the rest of this optimization. */
11880 56923418 : tree bare_retval = NULL_TREE;
11881 56923418 : if (retval)
11882 : {
11883 55246462 : retval = maybe_undo_parenthesized_ref (retval);
11884 55246462 : bare_retval = tree_strip_any_location_wrapper (retval);
11885 : }
11886 :
11887 56923418 : bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11888 56923418 : if (fn_returns_value_p && flag_elide_constructors
11889 55246439 : && current_function_return_value != bare_retval)
11890 : {
11891 55240384 : if (named_return_value_okay_p
11892 270473 : && current_function_return_value == NULL_TREE)
11893 229834 : current_function_return_value = bare_retval;
11894 55010550 : else if (current_function_return_value
11895 11188321 : && VAR_P (current_function_return_value)
11896 209 : && DECL_NAME (current_function_return_value)
11897 55010759 : && !decl_in_scope_p (current_function_return_value))
11898 : {
11899 : /* The earlier NRV is out of scope at this point, so it's safe to
11900 145 : leave it alone; the current return can't refer to it. */;
11901 145 : if (named_return_value_okay_p
11902 145 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11903 : {
11904 12 : warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11905 : bare_retval);
11906 12 : suppress_warning (current_function_decl, OPT_Wnrvo);
11907 : }
11908 : }
11909 : else
11910 : {
11911 55010405 : if ((named_return_value_okay_p
11912 54969780 : || (current_function_return_value
11913 11147551 : && current_function_return_value != error_mark_node))
11914 55010447 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11915 : {
11916 40590 : warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11917 : current_function_decl);
11918 40590 : suppress_warning (current_function_decl, OPT_Wnrvo);
11919 : }
11920 55010405 : current_function_return_value = error_mark_node;
11921 : }
11922 : }
11923 :
11924 : /* We don't need to do any conversions when there's nothing being
11925 : returned. */
11926 56923418 : if (!retval)
11927 : return NULL_TREE;
11928 :
11929 55246462 : if (!named_return_value_okay_p)
11930 54969934 : maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11931 :
11932 : /* Do any required conversions. */
11933 110492924 : if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11934 : /* No conversions are required. */
11935 : ;
11936 : else
11937 : {
11938 55246462 : int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11939 :
11940 : /* The functype's return type will have been set to void, if it
11941 : was an incomplete type. Just treat this as 'return;' */
11942 55246462 : if (VOID_TYPE_P (functype))
11943 6 : return error_mark_node;
11944 :
11945 : /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11946 : treated as an rvalue for the purposes of overload resolution to
11947 : favor move constructors over copy constructors.
11948 :
11949 : Note that these conditions are similar to, but not as strict as,
11950 : the conditions for the named return value optimization. */
11951 55246456 : bool converted = false;
11952 55246456 : tree moved;
11953 : /* Until C++23, this was only interesting for class type, but in C++23,
11954 : we should do the below when we're converting from/to a class/reference
11955 : (a non-scalar type). */
11956 55246456 : if ((cxx_dialect < cxx23
11957 7808789 : ? CLASS_TYPE_P (functype)
11958 10058321 : : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11959 66141148 : && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11960 : /* In C++20 and earlier we treat the return value as an rvalue
11961 : that can bind to lvalue refs. In C++23, such an expression is just
11962 : an xvalue (see reference_binding). */
11963 : retval = moved;
11964 :
11965 : /* The call in a (lambda) thunk needs no conversions. */
11966 55246456 : if (TREE_CODE (retval) == CALL_EXPR
11967 55246456 : && call_from_lambda_thunk_p (retval))
11968 : converted = true;
11969 :
11970 : /* Don't check copy-initialization for NRV in a coroutine ramp; we
11971 : implement this case as NRV, but it's specified as directly
11972 : initializing the return value from get_return_object(). */
11973 55246456 : if (DECL_RAMP_P (current_function_decl) && named_return_value_okay_p)
11974 : converted = true;
11975 :
11976 : /* First convert the value to the function's return type, then
11977 : to the type of return value's location to handle the
11978 : case that functype is smaller than the valtype. */
11979 55246194 : if (!converted)
11980 55189869 : retval = convert_for_initialization
11981 55189869 : (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11982 : tf_warning_or_error);
11983 55246456 : retval = convert (valtype, retval);
11984 :
11985 : /* If the conversion failed, treat this just like `return;'. */
11986 55246456 : if (retval == error_mark_node)
11987 : {
11988 : /* And suppress NRV. */
11989 213 : current_function_return_value = error_mark_node;
11990 213 : return retval;
11991 : }
11992 : /* We can't initialize a register from a AGGR_INIT_EXPR. */
11993 55246243 : else if (! cfun->returns_struct
11994 53099645 : && TREE_CODE (retval) == TARGET_EXPR
11995 62083571 : && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == AGGR_INIT_EXPR)
11996 1499099 : retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11997 1499099 : TARGET_EXPR_SLOT (retval));
11998 53747144 : else if (!processing_template_decl
11999 43828992 : && maybe_warn_about_returning_address_of_local (retval, loc)
12000 53747340 : && INDIRECT_TYPE_P (valtype))
12001 181 : *dangling = true;
12002 : }
12003 :
12004 : /* A naive attempt to reduce the number of -Wdangling-reference false
12005 : positives: if we know that this function can return a variable with
12006 : static storage duration rather than one of its parameters, suppress
12007 : the warning. */
12008 55246243 : if (warn_dangling_reference
12009 415787 : && TYPE_REF_P (functype)
12010 27671 : && bare_retval
12011 27671 : && VAR_P (bare_retval)
12012 50 : && TREE_STATIC (bare_retval))
12013 50 : suppress_warning (current_function_decl, OPT_Wdangling_reference);
12014 :
12015 55246243 : if (processing_template_decl)
12016 : return saved_retval;
12017 :
12018 : /* Actually copy the value returned into the appropriate location. */
12019 45328091 : if (retval && retval != result)
12020 45328091 : retval = cp_build_init_expr (result, retval);
12021 :
12022 45328091 : if (current_function_return_value == bare_retval)
12023 235883 : INIT_EXPR_NRV_P (retval) = true;
12024 :
12025 45328091 : if (tree set = maybe_set_retval_sentinel ())
12026 133355 : retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
12027 :
12028 : return retval;
12029 : }
12030 :
12031 :
12032 : /* Returns nonzero if the pointer-type FROM can be converted to the
12033 : pointer-type TO via a qualification conversion. If CONSTP is -1,
12034 : then we return nonzero if the pointers are similar, and the
12035 : cv-qualification signature of FROM is a proper subset of that of TO.
12036 :
12037 : If CONSTP is positive, then all outer pointers have been
12038 : const-qualified. */
12039 :
12040 : static bool
12041 195369449 : comp_ptr_ttypes_real (tree to, tree from, int constp)
12042 : {
12043 195369449 : bool to_more_cv_qualified = false;
12044 195369449 : bool is_opaque_pointer = false;
12045 :
12046 668545 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
12047 : {
12048 196037994 : if (TREE_CODE (to) != TREE_CODE (from))
12049 : return false;
12050 :
12051 129570980 : if (TREE_CODE (from) == OFFSET_TYPE
12052 129570980 : && !same_type_p (TYPE_OFFSET_BASETYPE (from),
12053 : TYPE_OFFSET_BASETYPE (to)))
12054 : return false;
12055 :
12056 : /* Const and volatile mean something different for function and
12057 : array types, so the usual checks are not appropriate. We'll
12058 : check the array type elements in further iterations. */
12059 129570980 : if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
12060 : {
12061 129320062 : if (!at_least_as_qualified_p (to, from))
12062 : return false;
12063 :
12064 115130966 : if (!at_least_as_qualified_p (from, to))
12065 : {
12066 78069606 : if (constp == 0)
12067 : return false;
12068 : to_more_cv_qualified = true;
12069 : }
12070 :
12071 115130697 : if (constp > 0)
12072 115126359 : constp &= TYPE_READONLY (to);
12073 : }
12074 :
12075 115381615 : if (VECTOR_TYPE_P (to))
12076 10121 : is_opaque_pointer = vector_targets_convertible_p (to, from);
12077 :
12078 : /* P0388R4 allows a conversion from int[N] to int[] but not the
12079 : other way round. When both arrays have bounds but they do
12080 : not match, then no conversion is possible. */
12081 115381615 : if (TREE_CODE (to) == ARRAY_TYPE
12082 115381615 : && !comp_array_types (to, from, bounds_first, /*strict=*/false))
12083 : return false;
12084 :
12085 115380777 : if (!TYPE_PTR_P (to)
12086 : && !TYPE_PTRDATAMEM_P (to)
12087 : /* CWG 330 says we need to look through arrays. */
12088 : && TREE_CODE (to) != ARRAY_TYPE)
12089 114712232 : return ((constp >= 0 || to_more_cv_qualified)
12090 114712232 : && (is_opaque_pointer
12091 114711634 : || same_type_ignoring_top_level_qualifiers_p (to, from)));
12092 : }
12093 : }
12094 :
12095 : /* When comparing, say, char ** to char const **, this function takes
12096 : the 'char *' and 'char const *'. Do not pass non-pointer/reference
12097 : types to this function. */
12098 :
12099 : int
12100 195368753 : comp_ptr_ttypes (tree to, tree from)
12101 : {
12102 195368753 : return comp_ptr_ttypes_real (to, from, 1);
12103 : }
12104 :
12105 : /* Returns true iff FNTYPE is a non-class type that involves
12106 : error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
12107 : if a parameter type is ill-formed. */
12108 :
12109 : bool
12110 716413 : error_type_p (const_tree type)
12111 : {
12112 757179 : tree t;
12113 :
12114 757179 : switch (TREE_CODE (type))
12115 : {
12116 : case ERROR_MARK:
12117 : return true;
12118 :
12119 40677 : case POINTER_TYPE:
12120 40677 : case REFERENCE_TYPE:
12121 40677 : case OFFSET_TYPE:
12122 40677 : return error_type_p (TREE_TYPE (type));
12123 :
12124 3263 : case FUNCTION_TYPE:
12125 3263 : case METHOD_TYPE:
12126 3263 : if (error_type_p (TREE_TYPE (type)))
12127 : return true;
12128 8816 : for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
12129 5559 : if (error_type_p (TREE_VALUE (t)))
12130 : return true;
12131 : return false;
12132 :
12133 190733 : case RECORD_TYPE:
12134 190733 : if (TYPE_PTRMEMFUNC_P (type))
12135 89 : return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
12136 : return false;
12137 :
12138 : default:
12139 : return false;
12140 : }
12141 : }
12142 :
12143 : /* Returns true if to and from are (possibly multi-level) pointers to the same
12144 : type or inheritance-related types, regardless of cv-quals. */
12145 :
12146 : bool
12147 140774953 : ptr_reasonably_similar (const_tree to, const_tree from)
12148 : {
12149 1548 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
12150 : {
12151 : /* Any target type is similar enough to void. */
12152 140776501 : if (VOID_TYPE_P (to))
12153 2100 : return !error_type_p (from);
12154 140774401 : if (VOID_TYPE_P (from))
12155 703201 : return !error_type_p (to);
12156 :
12157 140071200 : if (TREE_CODE (to) != TREE_CODE (from))
12158 : return false;
12159 :
12160 74167146 : if (TREE_CODE (from) == OFFSET_TYPE
12161 74167146 : && comptypes (TYPE_OFFSET_BASETYPE (to),
12162 3 : TYPE_OFFSET_BASETYPE (from),
12163 : COMPARE_BASE | COMPARE_DERIVED))
12164 3 : continue;
12165 :
12166 74167140 : if (VECTOR_TYPE_P (to)
12167 74167140 : && vector_types_convertible_p (to, from, false))
12168 : return true;
12169 :
12170 74167056 : if (TREE_CODE (to) == INTEGER_TYPE
12171 74167056 : && TYPE_PRECISION (to) == TYPE_PRECISION (from))
12172 : return true;
12173 :
12174 73964618 : if (TREE_CODE (to) == FUNCTION_TYPE)
12175 1145 : return !error_type_p (to) && !error_type_p (from);
12176 :
12177 73963473 : if (!TYPE_PTR_P (to))
12178 : {
12179 : /* When either type is incomplete avoid DERIVED_FROM_P,
12180 : which may call complete_type (c++/57942). */
12181 73961928 : bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
12182 : return comptypes
12183 73961928 : (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
12184 73961928 : b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
12185 : }
12186 1548 : }
12187 : }
12188 :
12189 : /* Return true if TO and FROM (both of which are POINTER_TYPEs or
12190 : pointer-to-member types) are the same, ignoring cv-qualification at
12191 : all levels. CB says how we should behave when comparing array bounds. */
12192 :
12193 : bool
12194 1111434 : comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
12195 : {
12196 1111434 : bool is_opaque_pointer = false;
12197 :
12198 1113931 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
12199 : {
12200 2225365 : if (TREE_CODE (to) != TREE_CODE (from))
12201 : return false;
12202 :
12203 1892730 : if (TREE_CODE (from) == OFFSET_TYPE
12204 1890758 : && same_type_p (TYPE_OFFSET_BASETYPE (from),
12205 : TYPE_OFFSET_BASETYPE (to)))
12206 1972 : continue;
12207 :
12208 1888786 : if (VECTOR_TYPE_P (to))
12209 8632 : is_opaque_pointer = vector_targets_convertible_p (to, from);
12210 :
12211 1888786 : if (TREE_CODE (to) == ARRAY_TYPE
12212 : /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
12213 : we must fail. */
12214 1888786 : && !comp_array_types (to, from, cb, /*strict=*/false))
12215 : return false;
12216 :
12217 : /* CWG 330 says we need to look through arrays. */
12218 1885936 : if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
12219 773977 : return (is_opaque_pointer
12220 773977 : || same_type_ignoring_top_level_qualifiers_p (to, from));
12221 : }
12222 : }
12223 :
12224 : /* Returns the type qualifiers for this type, including the qualifiers on the
12225 : elements for an array type. */
12226 :
12227 : cv_qualifier
12228 65258505357 : cp_type_quals (const_tree type)
12229 : {
12230 65258505357 : cv_qualifier quals;
12231 65258505357 : addr_space_t as;
12232 : /* This CONST_CAST is okay because strip_array_types returns its
12233 : argument unmodified and we assign it to a const_tree. */
12234 65258505357 : type = strip_array_types (const_cast<tree> (type));
12235 65258505357 : if (type == error_mark_node
12236 : /* Quals on a FUNCTION_TYPE are memfn quals. */
12237 65221112574 : || TREE_CODE (type) == FUNCTION_TYPE)
12238 : return TYPE_UNQUALIFIED;
12239 :
12240 65167303067 : std::tie (quals, as) = TYPE_QUALS (type).split ();
12241 : /* No address space support yet. */
12242 65167303067 : gcc_assert (ADDR_SPACE_GENERIC_P (as));
12243 : /* METHOD and REFERENCE_TYPEs should never have quals. */
12244 65167303067 : gcc_assert ((TREE_CODE (type) != METHOD_TYPE
12245 : && !TYPE_REF_P (type))
12246 : || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
12247 : == TYPE_UNQUALIFIED));
12248 : return quals;
12249 : }
12250 :
12251 : /* Returns the function-ref-qualifier for TYPE */
12252 :
12253 : cp_ref_qualifier
12254 3486629836 : type_memfn_rqual (const_tree type)
12255 : {
12256 3486629836 : gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
12257 :
12258 3486629836 : if (!FUNCTION_REF_QUALIFIED (type))
12259 : return REF_QUAL_NONE;
12260 32346922 : else if (FUNCTION_RVALUE_QUALIFIED (type))
12261 : return REF_QUAL_RVALUE;
12262 : else
12263 16064835 : return REF_QUAL_LVALUE;
12264 : }
12265 :
12266 : /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
12267 : METHOD_TYPE. */
12268 :
12269 : cv_qualifier
12270 1124169683 : type_memfn_quals (const_tree type)
12271 : {
12272 1124169683 : if (TREE_CODE (type) == FUNCTION_TYPE)
12273 : {
12274 96594406 : cv_qualifier quals;
12275 96594406 : addr_space_t as;
12276 96594406 : std::tie (quals, as) = TYPE_QUALS (type).split ();
12277 : /* No address space support yet. */
12278 96594406 : gcc_assert (ADDR_SPACE_GENERIC_P (as));
12279 : return quals;
12280 : }
12281 1027575277 : else if (TREE_CODE (type) == METHOD_TYPE)
12282 1027575277 : return cp_type_quals (class_of_this_parm (type));
12283 : else
12284 0 : gcc_unreachable ();
12285 : }
12286 :
12287 : /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
12288 : MEMFN_QUALS and its ref-qualifier to RQUAL. */
12289 :
12290 : tree
12291 75900506 : apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
12292 : {
12293 : /* Could handle METHOD_TYPE here if necessary. */
12294 75900506 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
12295 75900506 : if (TYPE_QUALS (type) == memfn_quals
12296 75196267 : && type_memfn_rqual (type) == rqual)
12297 : return type;
12298 :
12299 : /* This should really have a different TYPE_MAIN_VARIANT, but that gets
12300 : complex. */
12301 708250 : tree result = build_qualified_type (type, memfn_quals);
12302 708250 : return build_ref_qualified_type (result, rqual);
12303 : }
12304 :
12305 : /* Returns nonzero if TYPE is const or volatile. */
12306 :
12307 : bool
12308 1258809896 : cv_qualified_p (const_tree type)
12309 : {
12310 1258809896 : int quals = cp_type_quals (type);
12311 1258809896 : return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
12312 : }
12313 :
12314 : /* Returns nonzero if the TYPE contains a mutable member. */
12315 :
12316 : bool
12317 965402692 : cp_has_mutable_p (const_tree type)
12318 : {
12319 : /* This CONST_CAST is okay because strip_array_types returns its
12320 : argument unmodified and we assign it to a const_tree. */
12321 965402692 : type = strip_array_types (const_cast<tree> (type));
12322 :
12323 965402692 : return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
12324 : }
12325 :
12326 : /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
12327 : TYPE_QUALS. For a VAR_DECL, this may be an optimistic
12328 : approximation. In particular, consider:
12329 :
12330 : int f();
12331 : struct S { int i; };
12332 : const S s = { f(); }
12333 :
12334 : Here, we will make "s" as TREE_READONLY (because it is declared
12335 : "const") -- only to reverse ourselves upon seeing that the
12336 : initializer is non-constant. */
12337 :
12338 : void
12339 1041499611 : cp_apply_type_quals_to_decl (cv_qualifier type_quals, tree decl)
12340 : {
12341 1041499611 : tree type = TREE_TYPE (decl);
12342 :
12343 1041499611 : if (type == error_mark_node)
12344 : return;
12345 :
12346 1041498887 : if (TREE_CODE (decl) == TYPE_DECL)
12347 : return;
12348 :
12349 918590310 : gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
12350 : && type_quals != TYPE_UNQUALIFIED));
12351 :
12352 : /* Avoid setting TREE_READONLY incorrectly. */
12353 : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
12354 : constructor can produce constant init, so rely on cp_finish_decl to
12355 : clear TREE_READONLY if the variable has non-constant init. */
12356 :
12357 : /* If the type has (or might have) a mutable component, that component
12358 : might be modified. */
12359 918590310 : if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
12360 85095615 : type_quals &= ~TYPE_QUAL_CONST;
12361 :
12362 918590310 : c_apply_type_quals_to_decl (type_quals, decl);
12363 : }
12364 :
12365 : /* Subroutine of casts_away_constness. Make T1 and T2 point at
12366 : exemplar types such that casting T1 to T2 is casting away constness
12367 : if and only if there is no implicit conversion from T1 to T2. */
12368 :
12369 : static void
12370 1310325 : casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
12371 : {
12372 1310325 : cv_qualifier quals1;
12373 1310325 : cv_qualifier quals2;
12374 :
12375 : /* [expr.const.cast]
12376 :
12377 : For multi-level pointer to members and multi-level mixed pointers
12378 : and pointers to members (conv.qual), the "member" aspect of a
12379 : pointer to member level is ignored when determining if a const
12380 : cv-qualifier has been cast away. */
12381 : /* [expr.const.cast]
12382 :
12383 : For two pointer types:
12384 :
12385 : X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
12386 : X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
12387 : K is min(N,M)
12388 :
12389 : casting from X1 to X2 casts away constness if, for a non-pointer
12390 : type T there does not exist an implicit conversion (clause
12391 : _conv_) from:
12392 :
12393 : Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
12394 :
12395 : to
12396 :
12397 : Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
12398 1310325 : if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
12399 655515 : || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
12400 : {
12401 654990 : *t1 = cp_build_qualified_type (void_type_node,
12402 : cp_type_quals (*t1));
12403 654990 : *t2 = cp_build_qualified_type (void_type_node,
12404 : cp_type_quals (*t2));
12405 654990 : return;
12406 : }
12407 :
12408 655335 : quals1 = cp_type_quals (*t1);
12409 655335 : quals2 = cp_type_quals (*t2);
12410 :
12411 655335 : if (TYPE_PTRDATAMEM_P (*t1))
12412 0 : *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
12413 : else
12414 655335 : *t1 = TREE_TYPE (*t1);
12415 655335 : if (TYPE_PTRDATAMEM_P (*t2))
12416 0 : *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
12417 : else
12418 655335 : *t2 = TREE_TYPE (*t2);
12419 :
12420 655335 : casts_away_constness_r (t1, t2, complain);
12421 655335 : *t1 = build_pointer_type (*t1);
12422 655335 : *t2 = build_pointer_type (*t2);
12423 655335 : *t1 = cp_build_qualified_type (*t1, quals1);
12424 655335 : *t2 = cp_build_qualified_type (*t2, quals2);
12425 : }
12426 :
12427 : /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
12428 : constness.
12429 :
12430 : ??? This function returns non-zero if casting away qualifiers not
12431 : just const. We would like to return to the caller exactly which
12432 : qualifiers are casted away to give more accurate diagnostics.
12433 : */
12434 :
12435 : static bool
12436 655062 : casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
12437 : {
12438 655062 : if (TYPE_REF_P (t2))
12439 : {
12440 : /* [expr.const.cast]
12441 :
12442 : Casting from an lvalue of type T1 to an lvalue of type T2
12443 : using a reference cast casts away constness if a cast from an
12444 : rvalue of type "pointer to T1" to the type "pointer to T2"
12445 : casts away constness. */
12446 0 : t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
12447 0 : return casts_away_constness (build_pointer_type (t1),
12448 0 : build_pointer_type (TREE_TYPE (t2)),
12449 0 : complain);
12450 : }
12451 :
12452 655062 : if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
12453 : /* [expr.const.cast]
12454 :
12455 : Casting from an rvalue of type "pointer to data member of X
12456 : of type T1" to the type "pointer to data member of Y of type
12457 : T2" casts away constness if a cast from an rvalue of type
12458 : "pointer to T1" to the type "pointer to T2" casts away
12459 : constness. */
12460 48 : return casts_away_constness
12461 48 : (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
12462 48 : build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
12463 48 : complain);
12464 :
12465 : /* Casting away constness is only something that makes sense for
12466 : pointer or reference types. */
12467 655014 : if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
12468 : return false;
12469 :
12470 : /* Top-level qualifiers don't matter. */
12471 654990 : t1 = TYPE_MAIN_VARIANT (t1);
12472 654990 : t2 = TYPE_MAIN_VARIANT (t2);
12473 654990 : casts_away_constness_r (&t1, &t2, complain);
12474 654990 : if (!can_convert (t2, t1, complain))
12475 402 : return true;
12476 :
12477 : return false;
12478 : }
12479 :
12480 : /* If T is a REFERENCE_TYPE return the type to which T refers.
12481 : Otherwise, return T itself. */
12482 :
12483 : tree
12484 4502239600 : non_reference (tree t)
12485 : {
12486 4502239600 : if (t && TYPE_REF_P (t))
12487 405330924 : t = TREE_TYPE (t);
12488 4502239600 : return t;
12489 : }
12490 :
12491 :
12492 : /* Return nonzero if REF is an lvalue valid for this language;
12493 : otherwise, print an error message and return zero. USE says
12494 : how the lvalue is being used and so selects the error message. */
12495 :
12496 : int
12497 43473058 : lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
12498 : {
12499 43473058 : cp_lvalue_kind kind = lvalue_kind (ref);
12500 :
12501 43473058 : if (kind == clk_none)
12502 : {
12503 690 : if (complain & tf_error)
12504 126 : lvalue_error (cp_expr_loc_or_input_loc (ref), use);
12505 : return 0;
12506 : }
12507 43472368 : else if (kind & (clk_rvalueref|clk_class))
12508 : {
12509 220 : if (!(complain & tf_error))
12510 : return 0;
12511 : /* Make this a permerror because we used to accept it. */
12512 18 : permerror (cp_expr_loc_or_input_loc (ref),
12513 : "using rvalue as lvalue");
12514 : }
12515 : return 1;
12516 : }
12517 :
12518 : /* Return true if a user-defined literal operator is a raw operator. */
12519 :
12520 : bool
12521 115210 : check_raw_literal_operator (const_tree decl)
12522 : {
12523 115210 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12524 115210 : tree argtype;
12525 115210 : int arity;
12526 115210 : bool maybe_raw_p = false;
12527 :
12528 : /* Count the number and type of arguments and check for ellipsis. */
12529 115210 : for (argtype = argtypes, arity = 0;
12530 172832 : argtype && argtype != void_list_node;
12531 57622 : ++arity, argtype = TREE_CHAIN (argtype))
12532 : {
12533 57622 : tree t = TREE_VALUE (argtype);
12534 :
12535 57622 : if (same_type_p (t, const_string_type_node))
12536 9 : maybe_raw_p = true;
12537 : }
12538 115210 : if (!argtype)
12539 : return false; /* Found ellipsis. */
12540 :
12541 115210 : if (!maybe_raw_p || arity != 1)
12542 115204 : return false;
12543 :
12544 : return true;
12545 : }
12546 :
12547 :
12548 : /* Return true if a user-defined literal operator has one of the allowed
12549 : argument types. */
12550 :
12551 : bool
12552 313936 : check_literal_operator_args (const_tree decl,
12553 : bool *long_long_unsigned_p, bool *long_double_p)
12554 : {
12555 313936 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12556 :
12557 313936 : *long_long_unsigned_p = false;
12558 313936 : *long_double_p = false;
12559 313936 : if (processing_template_decl || processing_specialization)
12560 57744 : return argtypes == void_list_node;
12561 : else
12562 : {
12563 : tree argtype;
12564 : int arity;
12565 : int max_arity = 2;
12566 :
12567 : /* Count the number and type of arguments and check for ellipsis. */
12568 256064 : for (argtype = argtypes, arity = 0;
12569 512256 : argtype && argtype != void_list_node;
12570 256064 : argtype = TREE_CHAIN (argtype))
12571 : {
12572 256198 : tree t = TREE_VALUE (argtype);
12573 256198 : ++arity;
12574 :
12575 256198 : if (TYPE_PTR_P (t))
12576 : {
12577 123270 : bool maybe_raw_p = false;
12578 123270 : t = TREE_TYPE (t);
12579 123270 : if (cp_type_quals (t) != TYPE_QUAL_CONST)
12580 : return false;
12581 123267 : t = TYPE_MAIN_VARIANT (t);
12582 123267 : if ((maybe_raw_p = same_type_p (t, char_type_node))
12583 98423 : || same_type_p (t, wchar_type_node)
12584 73765 : || same_type_p (t, char8_type_node)
12585 49328 : || same_type_p (t, char16_type_node)
12586 147931 : || same_type_p (t, char32_type_node))
12587 : {
12588 123264 : argtype = TREE_CHAIN (argtype);
12589 123264 : if (!argtype)
12590 : return false;
12591 123264 : t = TREE_VALUE (argtype);
12592 123264 : if (maybe_raw_p && argtype == void_list_node)
12593 : return true;
12594 123157 : else if (same_type_p (t, size_type_node))
12595 : {
12596 123151 : ++arity;
12597 123151 : continue;
12598 : }
12599 : else
12600 : return false;
12601 : }
12602 : }
12603 132928 : else if (same_type_p (t, long_long_unsigned_type_node))
12604 : {
12605 46927 : max_arity = 1;
12606 46927 : *long_long_unsigned_p = true;
12607 : }
12608 86001 : else if (same_type_p (t, long_double_type_node))
12609 : {
12610 85901 : max_arity = 1;
12611 85901 : *long_double_p = true;
12612 : }
12613 100 : else if (same_type_p (t, char_type_node))
12614 : max_arity = 1;
12615 56 : else if (same_type_p (t, wchar_type_node))
12616 : max_arity = 1;
12617 45 : else if (same_type_p (t, char8_type_node))
12618 : max_arity = 1;
12619 40 : else if (same_type_p (t, char16_type_node))
12620 : max_arity = 1;
12621 29 : else if (same_type_p (t, char32_type_node))
12622 : max_arity = 1;
12623 : else
12624 : return false;
12625 : }
12626 256058 : if (!argtype)
12627 : return false; /* Found ellipsis. */
12628 :
12629 256055 : if (arity != max_arity)
12630 : return false;
12631 :
12632 256049 : return true;
12633 : }
12634 : }
12635 :
12636 : /* Always returns false since unlike C90, C++ has no concept of implicit
12637 : function declarations. */
12638 :
12639 : bool
12640 395 : c_decl_implicit (const_tree)
12641 : {
12642 395 : return false;
12643 : }
|