Branch data Line data Source code
1 : : /* Build expressions with type checking for C++ compiler.
2 : : Copyright (C) 1987-2025 Free Software Foundation, Inc.
3 : : Hacked by Michael Tiemann (tiemann@cygnus.com)
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify
8 : : it under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful,
13 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : : GNU General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : :
22 : : /* This file is part of the C++ front end.
23 : : It contains routines to build C++ expressions given their operands,
24 : : including computing the types of the result, C and C++ specific error
25 : : checks, and some optimization. */
26 : :
27 : : #include "config.h"
28 : : #include "system.h"
29 : : #include "coretypes.h"
30 : : #include "target.h"
31 : : #include "cp-tree.h"
32 : : #include "stor-layout.h"
33 : : #include "varasm.h"
34 : : #include "intl.h"
35 : : #include "convert.h"
36 : : #include "c-family/c-objc.h"
37 : : #include "c-family/c-ubsan.h"
38 : : #include "c-family/c-type-mismatch.h"
39 : : #include "stringpool.h"
40 : : #include "attribs.h"
41 : : #include "asan.h"
42 : : #include "gimplify.h"
43 : :
44 : : static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
45 : : static tree cp_build_function_call (tree, tree, tsubst_flags_t);
46 : : static tree pfn_from_ptrmemfunc (tree);
47 : : static tree delta_from_ptrmemfunc (tree);
48 : : static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 : : tsubst_flags_t, int);
50 : : static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
51 : : tsubst_flags_t);
52 : : static tree rationalize_conditional_expr (enum tree_code, tree,
53 : : tsubst_flags_t);
54 : : static bool comp_ptr_ttypes_real (tree, tree, int);
55 : : static bool comp_except_types (tree, tree, bool);
56 : : static bool comp_array_types (const_tree, const_tree, compare_bounds_t, bool);
57 : : static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
58 : : static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59 : : static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60 : : static bool casts_away_constness (tree, tree, tsubst_flags_t);
61 : : static bool maybe_warn_about_returning_address_of_local (tree, location_t = UNKNOWN_LOCATION);
62 : : static void error_args_num (location_t, tree, bool);
63 : : static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
64 : : tsubst_flags_t);
65 : : static bool is_std_move_p (tree);
66 : : static bool is_std_forward_p (tree);
67 : :
68 : : /* Do `exp = require_complete_type (exp);' to make sure exp
69 : : does not have an incomplete type. (That includes void types.)
70 : : Returns error_mark_node if the VALUE does not have
71 : : complete type when this function returns. */
72 : :
73 : : tree
74 : 115325710 : require_complete_type (tree value,
75 : : tsubst_flags_t complain /* = tf_warning_or_error */)
76 : : {
77 : 115325710 : tree type;
78 : :
79 : 115325710 : if (processing_template_decl || value == error_mark_node)
80 : : return value;
81 : :
82 : 107153688 : if (TREE_CODE (value) == OVERLOAD)
83 : 0 : type = unknown_type_node;
84 : : else
85 : 107153688 : type = TREE_TYPE (value);
86 : :
87 : 107153688 : if (type == error_mark_node)
88 : : return error_mark_node;
89 : :
90 : : /* First, detect a valid value with a complete type. */
91 : 107153676 : if (COMPLETE_TYPE_P (type))
92 : : return value;
93 : :
94 : 112425 : if (complete_type_or_maybe_complain (type, value, complain))
95 : : return value;
96 : : else
97 : 88 : return error_mark_node;
98 : : }
99 : :
100 : : /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
101 : : a template instantiation, do the instantiation. Returns TYPE,
102 : : whether or not it could be completed, unless something goes
103 : : horribly wrong, in which case the error_mark_node is returned. */
104 : :
105 : : tree
106 : 12317443388 : complete_type (tree type)
107 : : {
108 : 12317443388 : 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 : 12317443388 : if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 : : ;
115 : 4114192713 : else if (TREE_CODE (type) == ARRAY_TYPE)
116 : : {
117 : 631623 : tree t = complete_type (TREE_TYPE (type));
118 : 631623 : unsigned int needs_constructing, has_nontrivial_dtor;
119 : 631623 : if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 : 626853 : layout_type (type);
121 : 631623 : needs_constructing
122 : 631623 : = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 : 631623 : has_nontrivial_dtor
124 : 631623 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 : 2084937 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 : : {
127 : 1453314 : TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 : 1453314 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 : : }
130 : : }
131 : 4113561090 : else if (CLASS_TYPE_P (type))
132 : : {
133 : 4032798066 : if (modules_p ())
134 : : /* TYPE could be a class member we've not loaded the definition of. */
135 : 12736075 : lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
136 : :
137 : 4032798066 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 : 772188784 : 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 : 1298606785 : complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 : : {
151 : 1298606785 : type = complete_type (type);
152 : 1298606782 : if (type == error_mark_node)
153 : : /* We already issued an error. */
154 : : return NULL_TREE;
155 : 1298606629 : else if (!COMPLETE_TYPE_P (type))
156 : : {
157 : 3796 : if (complain & tf_error)
158 : 472 : cxx_incomplete_type_diagnostic (value, type, diagnostics::kind::error);
159 : 3796 : note_failed_type_completion (type, complain);
160 : 3796 : return NULL_TREE;
161 : : }
162 : : else
163 : : return type;
164 : : }
165 : :
166 : : tree
167 : 142288690 : complete_type_or_else (tree type, tree value)
168 : : {
169 : 142288690 : 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 : 4126925 : commonparms (tree p1, tree p2)
182 : : {
183 : 4126925 : tree oldargs = p1, newargs, n;
184 : 4126925 : int i, len;
185 : 4126925 : int any_change = 0;
186 : :
187 : 4126925 : len = list_length (p1);
188 : 4126925 : newargs = tree_last (p1);
189 : :
190 : 4126925 : if (newargs == void_list_node)
191 : : i = 1;
192 : : else
193 : : {
194 : 39738 : i = 0;
195 : 39738 : newargs = 0;
196 : : }
197 : :
198 : 13908485 : for (; i < len; i++)
199 : 9781560 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200 : :
201 : : n = newargs;
202 : :
203 : 17995672 : for (i = 0; p1;
204 : 13868747 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 : : {
206 : 13868933 : if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 : : {
208 : 159 : TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 : 159 : any_change = 1;
210 : : }
211 : 13868588 : else if (! TREE_PURPOSE (p1))
212 : : {
213 : 13868561 : if (TREE_PURPOSE (p2))
214 : : {
215 : 425479 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 : 425479 : any_change = 1;
217 : : }
218 : : }
219 : : else
220 : : {
221 : 27 : if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
222 : 27 : any_change = 1;
223 : 27 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 : : }
225 : 13868747 : if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 : : {
227 : 3853061 : any_change = 1;
228 : 3853061 : TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 : : }
230 : : else
231 : 10015686 : TREE_VALUE (n) = TREE_VALUE (p1);
232 : : }
233 : 4126925 : if (! any_change)
234 : 1400443 : 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 : 32994760 : original_type (tree t)
243 : : {
244 : 32994760 : int quals = cp_type_quals (t);
245 : 32994760 : while (t != error_mark_node
246 : 34131663 : && TYPE_NAME (t) != NULL_TREE)
247 : : {
248 : 11539581 : tree x = TYPE_NAME (t);
249 : 11539581 : if (TREE_CODE (x) != TYPE_DECL)
250 : : break;
251 : 11539581 : x = DECL_ORIGINAL_TYPE (x);
252 : 11539581 : if (x == NULL_TREE)
253 : : break;
254 : : t = x;
255 : : }
256 : 32994760 : 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 : 50279 : merge_type_attributes_from (tree type, tree other_type)
264 : : {
265 : 50279 : tree attrs = targetm.merge_type_attributes (type, other_type);
266 : 50279 : attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
267 : 50279 : 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 : 10698137 : cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
280 : : {
281 : 10698137 : tree mv1 = TYPE_MAIN_VARIANT (t1);
282 : 10698137 : tree mv2 = TYPE_MAIN_VARIANT (t2);
283 : 10698137 : int extended1 = 0;
284 : 10698137 : int extended2 = 0;
285 : :
286 : 10698137 : if (mv1 == mv2)
287 : : return 0;
288 : :
289 : 85573776 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
290 : : {
291 : 74877054 : if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 : 4757676 : extended1 = i + 1;
293 : 74877054 : if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 : 4059451 : extended2 = i + 1;
295 : : }
296 : 10696722 : if (mv1 == bfloat16_type_node)
297 : 1098105 : extended1 = true;
298 : 10696722 : if (mv2 == bfloat16_type_node)
299 : 889760 : extended2 = true;
300 : 10696722 : if (extended2 && !extended1)
301 : : {
302 : 4752841 : int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 : 4752841 : return ret == 3 ? 3 : -ret;
304 : : }
305 : :
306 : 5943881 : const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 : 5943881 : const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 : 5943881 : gcc_assert (fmt1->b == 2 && fmt2->b == 2);
309 : : /* For {ibm,mips}_extended_format formats, the type has variable
310 : : precision up to ~2150 bits when the first double is around maximum
311 : : representable double and second double is subnormal minimum.
312 : : So, e.g. for __ibm128 vs. std::float128_t, they have unordered
313 : : ranks. */
314 : 47551048 : int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
315 : 11887762 : ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
316 : 47551048 : int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
317 : 11887762 : ? fmt2->emax - fmt2->emin + fmt2->p - 1 : fmt2->p);
318 : : /* The rank of a floating point type T is greater than the rank of
319 : : any floating-point type whose set of values is a proper subset
320 : : of the set of values of T. */
321 : 5943881 : if ((p1 > p2 && fmt1->emax >= fmt2->emax)
322 : 4577938 : || (p1 == p2 && fmt1->emax > fmt2->emax))
323 : : return 2;
324 : 4577938 : if ((p1 < p2 && fmt1->emax <= fmt2->emax)
325 : 1516093 : || (p1 == p2 && fmt1->emax < fmt2->emax))
326 : : return -2;
327 : 1516093 : if ((p1 > p2 && fmt1->emax < fmt2->emax)
328 : 1507741 : || (p1 < p2 && fmt1->emax > fmt2->emax))
329 : : return 3;
330 : 1499389 : if (!extended1 && !extended2)
331 : : {
332 : : /* The rank of long double is greater than the rank of double, which
333 : : is greater than the rank of float. */
334 : 0 : if (t1 == long_double_type_node)
335 : : return 2;
336 : 0 : else if (t2 == long_double_type_node)
337 : : return -2;
338 : 0 : if (t1 == double_type_node)
339 : : return 2;
340 : 0 : else if (t2 == double_type_node)
341 : : return -2;
342 : 0 : if (t1 == float_type_node)
343 : : return 2;
344 : 0 : else if (t2 == float_type_node)
345 : : return -2;
346 : : return 0;
347 : : }
348 : : /* Two extended floating-point types with the same set of values have equal
349 : : ranks. */
350 : 1499389 : if (extended1 && extended2)
351 : : {
352 : 4 : if ((extended1 <= NUM_FLOATN_TYPES) == (extended2 <= NUM_FLOATN_TYPES))
353 : : {
354 : : /* Prefer higher extendedN value. */
355 : 0 : if (extended1 > extended2)
356 : : return 1;
357 : 0 : else if (extended1 < extended2)
358 : : return -1;
359 : : else
360 : : return 0;
361 : : }
362 : 4 : else if (extended1 <= NUM_FLOATN_TYPES)
363 : : /* Prefer _FloatN type over _FloatMx type. */
364 : : return 1;
365 : 2 : else if (extended2 <= NUM_FLOATN_TYPES)
366 : : return -1;
367 : : else
368 : : return 0;
369 : : }
370 : :
371 : : /* gcc_assert (extended1 && !extended2); */
372 : : tree *p;
373 : : int cnt = 0;
374 : 5997540 : for (p = &float_type_node; p <= &long_double_type_node; ++p)
375 : : {
376 : 4498155 : const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
377 : 4498155 : gcc_assert (fmt3->b == 2);
378 : 35985240 : int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
379 : 8996310 : ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
380 : 4498155 : if (p1 == p3 && fmt1->emax == fmt3->emax)
381 : 1307805 : ++cnt;
382 : : }
383 : : /* An extended floating-point type with the same set of values
384 : : as exactly one cv-unqualified standard floating-point type
385 : : has a rank equal to the rank of that standard floating-point
386 : : type.
387 : :
388 : : An extended floating-point type with the same set of values
389 : : as more than one cv-unqualified standard floating-point type
390 : : has a rank equal to the rank of double.
391 : :
392 : : Thus, if the latter is true and t2 is long double, t2
393 : : has higher rank. */
394 : 1499385 : if (cnt > 1 && mv2 == long_double_type_node)
395 : : return -2;
396 : : /* And similarly if t2 is float, t2 has lower rank. */
397 : 0 : if (cnt > 1 && mv2 == float_type_node)
398 : : return 2;
399 : : /* Otherwise, they have equal rank, but extended types
400 : : (other than std::bfloat16_t) have higher subrank.
401 : : std::bfloat16_t shouldn't have equal rank to any standard
402 : : floating point type. */
403 : : return 1;
404 : : }
405 : :
406 : : /* Return the common type for two arithmetic types T1 and T2 under the
407 : : usual arithmetic conversions. The default conversions have already
408 : : been applied, and enumerated types converted to their compatible
409 : : integer types. */
410 : :
411 : : static tree
412 : 95991496 : cp_common_type (tree t1, tree t2)
413 : : {
414 : 95991496 : enum tree_code code1 = TREE_CODE (t1);
415 : 95991496 : enum tree_code code2 = TREE_CODE (t2);
416 : 95991496 : tree attributes;
417 : 95991496 : int i;
418 : :
419 : :
420 : : /* In what follows, we slightly generalize the rules given in [expr] so
421 : : as to deal with `long long' and `complex'. First, merge the
422 : : attributes. */
423 : 95991496 : attributes = (*targetm.merge_type_attributes) (t1, t2);
424 : :
425 : 95991496 : if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
426 : : {
427 : 1425788 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
428 : 1425788 : return build_type_attribute_variant (t1, attributes);
429 : : else
430 : : return NULL_TREE;
431 : : }
432 : :
433 : : /* FIXME: Attributes. */
434 : 94565708 : gcc_assert (ARITHMETIC_TYPE_P (t1)
435 : : || VECTOR_TYPE_P (t1)
436 : : || UNSCOPED_ENUM_P (t1));
437 : 94565708 : gcc_assert (ARITHMETIC_TYPE_P (t2)
438 : : || VECTOR_TYPE_P (t2)
439 : : || UNSCOPED_ENUM_P (t2));
440 : :
441 : : /* If one type is complex, form the common type of the non-complex
442 : : components, then make that complex. Use T1 or T2 if it is the
443 : : required type. */
444 : 94565708 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
445 : : {
446 : 229563 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
447 : 229563 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
448 : 229563 : tree subtype
449 : 229563 : = type_after_usual_arithmetic_conversions (subtype1, subtype2);
450 : :
451 : 229563 : if (subtype == error_mark_node)
452 : : return subtype;
453 : 229563 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
454 : 196219 : return build_type_attribute_variant (t1, attributes);
455 : 33344 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
456 : 33284 : return build_type_attribute_variant (t2, attributes);
457 : : else
458 : 60 : return build_type_attribute_variant (build_complex_type (subtype),
459 : 60 : attributes);
460 : : }
461 : :
462 : 94336145 : if (code1 == VECTOR_TYPE)
463 : : {
464 : : /* When we get here we should have two vectors of the same size.
465 : : Just prefer the unsigned one if present. */
466 : 50279 : if (TYPE_UNSIGNED (t1))
467 : 11031 : return merge_type_attributes_from (t1, t2);
468 : : else
469 : 39248 : return merge_type_attributes_from (t2, t1);
470 : : }
471 : :
472 : : /* If only one is real, use it as the result. */
473 : 94285866 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
474 : 1114082 : return build_type_attribute_variant (t1, attributes);
475 : 93171784 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
476 : 1042520 : return build_type_attribute_variant (t2, attributes);
477 : :
478 : 92129264 : if (code1 == REAL_TYPE
479 : 92129264 : && (extended_float_type_p (t1) || extended_float_type_p (t2)))
480 : : {
481 : 195256 : tree mv1 = TYPE_MAIN_VARIANT (t1);
482 : 195256 : tree mv2 = TYPE_MAIN_VARIANT (t2);
483 : 195256 : if (mv1 == mv2)
484 : 191594 : return build_type_attribute_variant (t1, attributes);
485 : :
486 : 3662 : int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
487 : 3662 : if (cmpret == 3)
488 : 0 : return error_mark_node;
489 : 3662 : else if (cmpret >= 0)
490 : 3400 : return build_type_attribute_variant (t1, attributes);
491 : : else
492 : 262 : return build_type_attribute_variant (t2, attributes);
493 : : }
494 : :
495 : : /* Both real or both integers; use the one with greater precision. */
496 : 91934008 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
497 : 13327837 : return build_type_attribute_variant (t1, attributes);
498 : 78606171 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
499 : 1544279 : return build_type_attribute_variant (t2, attributes);
500 : :
501 : : /* The types are the same; no need to do anything fancy. */
502 : 77061892 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
503 : 70968396 : return build_type_attribute_variant (t1, attributes);
504 : :
505 : 6093496 : if (code1 != REAL_TYPE)
506 : : {
507 : : /* If one is unsigned long long, then convert the other to unsigned
508 : : long long. */
509 : 6093493 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
510 : 6093493 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
511 : 127319 : return build_type_attribute_variant (long_long_unsigned_type_node,
512 : 127319 : attributes);
513 : : /* If one is a long long, and the other is an unsigned long, and
514 : : long long can represent all the values of an unsigned long, then
515 : : convert to a long long. Otherwise, convert to an unsigned long
516 : : long. Otherwise, if either operand is long long, convert the
517 : : other to long long.
518 : :
519 : : Since we're here, we know the TYPE_PRECISION is the same;
520 : : therefore converting to long long cannot represent all the values
521 : : of an unsigned long, so we choose unsigned long long in that
522 : : case. */
523 : 5966174 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
524 : 5966174 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
525 : : {
526 : 597 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
527 : 304 : ? long_long_unsigned_type_node
528 : 304 : : long_long_integer_type_node);
529 : 304 : return build_type_attribute_variant (t, attributes);
530 : : }
531 : :
532 : : /* Go through the same procedure, but for longs. */
533 : 5965870 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
534 : 5965870 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
535 : 736722 : return build_type_attribute_variant (long_unsigned_type_node,
536 : 736722 : attributes);
537 : 5229148 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
538 : 5229148 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
539 : : {
540 : 71408 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
541 : 71408 : ? long_unsigned_type_node : long_integer_type_node);
542 : 36056 : return build_type_attribute_variant (t, attributes);
543 : : }
544 : :
545 : : /* For __intN types, either the type is __int128 (and is lower
546 : : priority than the types checked above, but higher than other
547 : : 128-bit types) or it's known to not be the same size as other
548 : : types (enforced in toplev.cc). Prefer the unsigned type. */
549 : 10386152 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
550 : : {
551 : 5193092 : if (int_n_enabled_p [i]
552 : 5193092 : && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
553 : 5114804 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
554 : 5114800 : || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
555 : 5114800 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
556 : : {
557 : 60 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
558 : 32 : ? int_n_trees[i].unsigned_type
559 : 32 : : int_n_trees[i].signed_type);
560 : 32 : return build_type_attribute_variant (t, attributes);
561 : : }
562 : : }
563 : :
564 : : /* Otherwise prefer the unsigned one. */
565 : 5193060 : if (TYPE_UNSIGNED (t1))
566 : 4153333 : return build_type_attribute_variant (t1, attributes);
567 : : else
568 : 1039727 : return build_type_attribute_variant (t2, attributes);
569 : : }
570 : : else
571 : : {
572 : 3 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
573 : 3 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
574 : 0 : return build_type_attribute_variant (long_double_type_node,
575 : 0 : attributes);
576 : 3 : if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
577 : 3 : || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
578 : 3 : return build_type_attribute_variant (double_type_node,
579 : 3 : attributes);
580 : 0 : if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
581 : 0 : || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
582 : 0 : return build_type_attribute_variant (float_type_node,
583 : 0 : attributes);
584 : :
585 : : /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
586 : : the standard C++ floating-point types. Logic earlier in this
587 : : function has already eliminated the possibility that
588 : : TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
589 : : compelling reason to choose one or the other. */
590 : 0 : return build_type_attribute_variant (t1, attributes);
591 : : }
592 : : }
593 : :
594 : : /* T1 and T2 are arithmetic or enumeration types. Return the type
595 : : that will result from the "usual arithmetic conversions" on T1 and
596 : : T2 as described in [expr]. */
597 : :
598 : : tree
599 : 959846 : type_after_usual_arithmetic_conversions (tree t1, tree t2)
600 : : {
601 : 959846 : gcc_assert (ARITHMETIC_TYPE_P (t1)
602 : : || VECTOR_TYPE_P (t1)
603 : : || UNSCOPED_ENUM_P (t1));
604 : 959846 : gcc_assert (ARITHMETIC_TYPE_P (t2)
605 : : || VECTOR_TYPE_P (t2)
606 : : || UNSCOPED_ENUM_P (t2));
607 : :
608 : : /* Perform the integral promotions. We do not promote real types here. */
609 : 959846 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
610 : 729743 : && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
611 : : {
612 : 729385 : t1 = type_promotes_to (t1);
613 : 729385 : t2 = type_promotes_to (t2);
614 : : }
615 : :
616 : 959846 : return cp_common_type (t1, t2);
617 : : }
618 : :
619 : : static void
620 : 42 : composite_pointer_error (const op_location_t &location,
621 : : enum diagnostics::kind kind, tree t1, tree t2,
622 : : composite_pointer_operation operation)
623 : : {
624 : 42 : switch (operation)
625 : : {
626 : 33 : case CPO_COMPARISON:
627 : 33 : emit_diagnostic (kind, location, 0,
628 : : "comparison between "
629 : : "distinct pointer types %qT and %qT lacks a cast",
630 : : t1, t2);
631 : 33 : break;
632 : 0 : case CPO_CONVERSION:
633 : 0 : emit_diagnostic (kind, location, 0,
634 : : "conversion between "
635 : : "distinct pointer types %qT and %qT lacks a cast",
636 : : t1, t2);
637 : 0 : break;
638 : 9 : case CPO_CONDITIONAL_EXPR:
639 : 9 : emit_diagnostic (kind, location, 0,
640 : : "conditional expression between "
641 : : "distinct pointer types %qT and %qT lacks a cast",
642 : : t1, t2);
643 : 9 : break;
644 : 0 : default:
645 : 0 : gcc_unreachable ();
646 : : }
647 : 42 : }
648 : :
649 : : /* Subroutine of composite_pointer_type to implement the recursive
650 : : case. See that function for documentation of the parameters. And ADD_CONST
651 : : is used to track adding "const" where needed. */
652 : :
653 : : static tree
654 : 3743341 : composite_pointer_type_r (const op_location_t &location,
655 : : tree t1, tree t2, bool *add_const,
656 : : composite_pointer_operation operation,
657 : : tsubst_flags_t complain)
658 : : {
659 : 3743341 : tree pointee1;
660 : 3743341 : tree pointee2;
661 : 3743341 : tree result_type;
662 : 3743341 : tree attributes;
663 : :
664 : : /* Determine the types pointed to by T1 and T2. */
665 : 3743341 : if (TYPE_PTR_P (t1))
666 : : {
667 : 3742845 : pointee1 = TREE_TYPE (t1);
668 : 3742845 : pointee2 = TREE_TYPE (t2);
669 : : }
670 : : else
671 : : {
672 : 496 : pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
673 : 496 : pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
674 : : }
675 : :
676 : : /* [expr.type]
677 : :
678 : : If T1 and T2 are similar types, the result is the cv-combined type of
679 : : T1 and T2. */
680 : 3743341 : if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
681 : : result_type = pointee1;
682 : 33 : else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
683 : 148 : || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
684 : : {
685 : 33 : result_type = composite_pointer_type_r (location, pointee1, pointee2,
686 : : add_const, operation, complain);
687 : 33 : if (result_type == error_mark_node)
688 : : return error_mark_node;
689 : : }
690 : : else
691 : : {
692 : 115 : if (complain & tf_error)
693 : 30 : composite_pointer_error (location, diagnostics::kind::permerror,
694 : : t1, t2, operation);
695 : : else
696 : 85 : return error_mark_node;
697 : 30 : result_type = void_type_node;
698 : : }
699 : 3743250 : const int q1 = cp_type_quals (pointee1);
700 : 3743250 : const int q2 = cp_type_quals (pointee2);
701 : 3743250 : const int quals = q1 | q2;
702 : 3743250 : result_type = cp_build_qualified_type (result_type,
703 : 3743250 : (quals | (*add_const
704 : 3743250 : ? TYPE_QUAL_CONST
705 : : : TYPE_UNQUALIFIED)));
706 : : /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
707 : : the TLQ). The reason is that both T1 and T2 can then be converted to the
708 : : cv-combined type of T1 and T2. */
709 : 3743250 : if (quals != q1 || quals != q2)
710 : 566010 : *add_const = true;
711 : : /* If the original types were pointers to members, so is the
712 : : result. */
713 : 3743250 : if (TYPE_PTRMEM_P (t1))
714 : : {
715 : 486 : if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
716 : : TYPE_PTRMEM_CLASS_TYPE (t2)))
717 : : {
718 : 0 : if (complain & tf_error)
719 : 0 : composite_pointer_error (location, diagnostics::kind::permerror,
720 : : t1, t2, operation);
721 : : else
722 : 0 : return error_mark_node;
723 : : }
724 : 486 : result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
725 : : result_type);
726 : : }
727 : : else
728 : 3742764 : result_type = build_pointer_type (result_type);
729 : :
730 : : /* Merge the attributes. */
731 : 3743250 : attributes = (*targetm.merge_type_attributes) (t1, t2);
732 : 3743250 : return build_type_attribute_variant (result_type, attributes);
733 : : }
734 : :
735 : : /* Return the composite pointer type (see [expr.type]) for T1 and T2.
736 : : ARG1 and ARG2 are the values with those types. The OPERATION is to
737 : : describe the operation between the pointer types,
738 : : in case an error occurs.
739 : :
740 : : This routine also implements the computation of a common type for
741 : : pointers-to-members as per [expr.eq]. */
742 : :
743 : : tree
744 : 3947990 : composite_pointer_type (const op_location_t &location,
745 : : tree t1, tree t2, tree arg1, tree arg2,
746 : : composite_pointer_operation operation,
747 : : tsubst_flags_t complain)
748 : : {
749 : 3947990 : tree class1;
750 : 3947990 : tree class2;
751 : :
752 : : /* [expr.type]
753 : :
754 : : If one operand is a null pointer constant, the composite pointer
755 : : type is the type of the other operand. */
756 : 3947990 : if (null_ptr_cst_p (arg1))
757 : : return t2;
758 : 3947811 : if (null_ptr_cst_p (arg2))
759 : : return t1;
760 : :
761 : : /* We have:
762 : :
763 : : [expr.type]
764 : :
765 : : If one of the operands has type "pointer to cv1 void", then
766 : : the other has type "pointer to cv2 T", and the composite pointer
767 : : type is "pointer to cv12 void", where cv12 is the union of cv1
768 : : and cv2.
769 : :
770 : : If either type is a pointer to void, make sure it is T1. */
771 : 3923035 : if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
772 : : std::swap (t1, t2);
773 : :
774 : : /* Now, if T1 is a pointer to void, merge the qualifiers. */
775 : 3923035 : if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
776 : : {
777 : 179556 : tree attributes;
778 : 179556 : tree result_type;
779 : :
780 : 179556 : if (TYPE_PTRFN_P (t2))
781 : : {
782 : 10 : if (complain & tf_error)
783 : : {
784 : 9 : switch (operation)
785 : : {
786 : 9 : case CPO_COMPARISON:
787 : 9 : pedwarn (location, OPT_Wpedantic,
788 : : "ISO C++ forbids comparison between pointer "
789 : : "of type %<void *%> and pointer-to-function");
790 : 9 : break;
791 : 0 : case CPO_CONVERSION:
792 : 0 : pedwarn (location, OPT_Wpedantic,
793 : : "ISO C++ forbids conversion between pointer "
794 : : "of type %<void *%> and pointer-to-function");
795 : 0 : break;
796 : 0 : case CPO_CONDITIONAL_EXPR:
797 : 0 : pedwarn (location, OPT_Wpedantic,
798 : : "ISO C++ forbids conditional expression between "
799 : : "pointer of type %<void *%> and "
800 : : "pointer-to-function");
801 : 0 : break;
802 : 0 : default:
803 : 0 : gcc_unreachable ();
804 : : }
805 : : }
806 : : else
807 : 1 : return error_mark_node;
808 : : }
809 : 179555 : result_type
810 : 179555 : = cp_build_qualified_type (void_type_node,
811 : 179555 : (cp_type_quals (TREE_TYPE (t1))
812 : 179555 : | cp_type_quals (TREE_TYPE (t2))));
813 : 179555 : result_type = build_pointer_type (result_type);
814 : : /* Merge the attributes. */
815 : 179555 : attributes = (*targetm.merge_type_attributes) (t1, t2);
816 : 179555 : return build_type_attribute_variant (result_type, attributes);
817 : : }
818 : :
819 : 3743479 : if (c_dialect_objc () && TYPE_PTR_P (t1)
820 : 0 : && TYPE_PTR_P (t2))
821 : : {
822 : 0 : if (objc_have_common_type (t1, t2, -3, NULL_TREE))
823 : 0 : return objc_common_type (t1, t2);
824 : : }
825 : :
826 : : /* if T1 or T2 is "pointer to noexcept function" and the other type is
827 : : "pointer to function", where the function types are otherwise the same,
828 : : "pointer to function" */
829 : 3743479 : if (fnptr_conv_p (t1, t2))
830 : : return t1;
831 : 3743411 : if (fnptr_conv_p (t2, t1))
832 : : return t2;
833 : :
834 : : /* [expr.eq] permits the application of a pointer conversion to
835 : : bring the pointers to a common type. */
836 : 3742830 : if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
837 : 3742830 : && CLASS_TYPE_P (TREE_TYPE (t1))
838 : 594233 : && CLASS_TYPE_P (TREE_TYPE (t2))
839 : 4337577 : && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
840 : 594230 : TREE_TYPE (t2)))
841 : : {
842 : 31593 : class1 = TREE_TYPE (t1);
843 : 31593 : class2 = TREE_TYPE (t2);
844 : :
845 : 31593 : if (DERIVED_FROM_P (class1, class2))
846 : 3238 : t2 = (build_pointer_type
847 : 1619 : (cp_build_qualified_type (class1, cp_type_quals (class2))));
848 : 29974 : else if (DERIVED_FROM_P (class2, class1))
849 : 59912 : t1 = (build_pointer_type
850 : 29956 : (cp_build_qualified_type (class2, cp_type_quals (class1))));
851 : : else
852 : : {
853 : 18 : if (complain & tf_error)
854 : 12 : composite_pointer_error (location, diagnostics::kind::error,
855 : : t1, t2, operation);
856 : 18 : return error_mark_node;
857 : : }
858 : : }
859 : : /* [expr.eq] permits the application of a pointer-to-member
860 : : conversion to change the class type of one of the types. */
861 : 3711357 : else if (TYPE_PTRMEM_P (t1)
862 : 3711874 : && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
863 : : TYPE_PTRMEM_CLASS_TYPE (t2)))
864 : : {
865 : 52 : class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
866 : 52 : class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
867 : :
868 : 52 : if (DERIVED_FROM_P (class1, class2))
869 : 21 : t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
870 : 31 : else if (DERIVED_FROM_P (class2, class1))
871 : 10 : t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
872 : : else
873 : : {
874 : 21 : if (complain & tf_error)
875 : 3 : switch (operation)
876 : : {
877 : 3 : case CPO_COMPARISON:
878 : 3 : error_at (location, "comparison between distinct "
879 : : "pointer-to-member types %qT and %qT lacks a cast",
880 : : t1, t2);
881 : 3 : break;
882 : 0 : case CPO_CONVERSION:
883 : 0 : error_at (location, "conversion between distinct "
884 : : "pointer-to-member types %qT and %qT lacks a cast",
885 : : t1, t2);
886 : 0 : break;
887 : 0 : case CPO_CONDITIONAL_EXPR:
888 : 0 : error_at (location, "conditional expression between distinct "
889 : : "pointer-to-member types %qT and %qT lacks a cast",
890 : : t1, t2);
891 : 0 : break;
892 : 0 : default:
893 : 0 : gcc_unreachable ();
894 : : }
895 : 21 : return error_mark_node;
896 : : }
897 : : }
898 : :
899 : 3743308 : bool add_const = false;
900 : 3743308 : return composite_pointer_type_r (location, t1, t2, &add_const, operation,
901 : 3743308 : complain);
902 : : }
903 : :
904 : : /* Return the merged type of two types.
905 : : We assume that comptypes has already been done and returned 1;
906 : : if that isn't so, this may crash.
907 : :
908 : : This just combines attributes and default arguments; any other
909 : : differences would cause the two types to compare unalike. */
910 : :
911 : : tree
912 : 29250185 : merge_types (tree t1, tree t2)
913 : : {
914 : 29250185 : enum tree_code code1;
915 : 29250185 : enum tree_code code2;
916 : 29250185 : tree attributes;
917 : :
918 : : /* Save time if the two types are the same. */
919 : 29250185 : if (t1 == t2)
920 : : return t1;
921 : 16497380 : if (original_type (t1) == original_type (t2))
922 : : return t1;
923 : :
924 : : /* If one type is nonsense, use the other. */
925 : 16434833 : if (t1 == error_mark_node)
926 : : return t2;
927 : 16434833 : if (t2 == error_mark_node)
928 : : return t1;
929 : :
930 : : /* Handle merging an auto redeclaration with a previous deduced
931 : : return type. */
932 : 16434833 : if (is_auto (t1))
933 : : return t2;
934 : :
935 : : /* Merge the attributes. */
936 : 16425025 : attributes = (*targetm.merge_type_attributes) (t1, t2);
937 : :
938 : 16425025 : if (TYPE_PTRMEMFUNC_P (t1))
939 : 15 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
940 : 16425025 : if (TYPE_PTRMEMFUNC_P (t2))
941 : 15 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
942 : :
943 : 16425025 : code1 = TREE_CODE (t1);
944 : 16425025 : code2 = TREE_CODE (t2);
945 : 16425025 : if (code1 != code2)
946 : : {
947 : 0 : gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
948 : 0 : if (code1 == TYPENAME_TYPE)
949 : : {
950 : 0 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
951 : 0 : code1 = TREE_CODE (t1);
952 : : }
953 : : else
954 : : {
955 : 0 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
956 : 0 : code2 = TREE_CODE (t2);
957 : : }
958 : : }
959 : :
960 : 16425025 : switch (code1)
961 : : {
962 : 2786615 : case POINTER_TYPE:
963 : 2786615 : case REFERENCE_TYPE:
964 : : /* For two pointers, do this recursively on the target type. */
965 : 2786615 : {
966 : 2786615 : tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
967 : 2786615 : int quals = cp_type_quals (t1);
968 : :
969 : 2786615 : if (code1 == POINTER_TYPE)
970 : : {
971 : 748249 : t1 = build_pointer_type (target);
972 : 748249 : if (TREE_CODE (target) == METHOD_TYPE)
973 : 15 : t1 = build_ptrmemfunc_type (t1);
974 : : }
975 : : else
976 : 2038366 : t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
977 : 2786615 : t1 = build_type_attribute_variant (t1, attributes);
978 : 2786615 : t1 = cp_build_qualified_type (t1, quals);
979 : :
980 : 2786615 : return t1;
981 : : }
982 : :
983 : 12 : case OFFSET_TYPE:
984 : 12 : {
985 : 12 : int quals;
986 : 12 : tree pointee;
987 : 12 : quals = cp_type_quals (t1);
988 : 12 : pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
989 : 12 : TYPE_PTRMEM_POINTED_TO_TYPE (t2));
990 : 12 : t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
991 : : pointee);
992 : 12 : t1 = cp_build_qualified_type (t1, quals);
993 : 12 : break;
994 : : }
995 : :
996 : 10984 : case ARRAY_TYPE:
997 : 10984 : {
998 : 10984 : tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
999 : : /* Save space: see if the result is identical to one of the args. */
1000 : 21968 : if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
1001 : 10962 : return build_type_attribute_variant (t1, attributes);
1002 : 38 : if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
1003 : 16 : return build_type_attribute_variant (t2, attributes);
1004 : : /* Merge the element types, and have a size if either arg has one. */
1005 : 6 : t1 = build_cplus_array_type
1006 : 6 : (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
1007 : 6 : break;
1008 : : }
1009 : :
1010 : 4273991 : case FUNCTION_TYPE:
1011 : : /* Function types: prefer the one that specified arg types.
1012 : : If both do, merge the arg types. Also merge the return types. */
1013 : 4273991 : {
1014 : 4273991 : tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1015 : 4273991 : tree p1 = TYPE_ARG_TYPES (t1);
1016 : 4273991 : tree p2 = TYPE_ARG_TYPES (t2);
1017 : 4273991 : tree parms;
1018 : :
1019 : : /* Save space: see if the result is identical to one of the args. */
1020 : 4273991 : if (valtype == TREE_TYPE (t1) && ! p2)
1021 : 0 : return cp_build_type_attribute_variant (t1, attributes);
1022 : 4273991 : if (valtype == TREE_TYPE (t2) && ! p1)
1023 : 0 : return cp_build_type_attribute_variant (t2, attributes);
1024 : :
1025 : : /* Simple way if one arg fails to specify argument types. */
1026 : 8547982 : if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1027 : : parms = p2;
1028 : 8253850 : else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1029 : : parms = p1;
1030 : : else
1031 : 4126925 : parms = commonparms (p1, p2);
1032 : :
1033 : 4273991 : cp_cv_quals quals = type_memfn_quals (t1);
1034 : 4273991 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1035 : 4273991 : gcc_assert (quals == type_memfn_quals (t2));
1036 : 4273991 : gcc_assert (rqual == type_memfn_rqual (t2));
1037 : :
1038 : 4273991 : tree rval = build_function_type (valtype, parms);
1039 : 4273991 : rval = apply_memfn_quals (rval, quals);
1040 : 4273991 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1041 : 4273991 : TYPE_RAISES_EXCEPTIONS (t2));
1042 : 4273991 : bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1043 : 4273991 : t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1044 : 4273991 : break;
1045 : : }
1046 : :
1047 : 4033364 : case METHOD_TYPE:
1048 : 4033364 : {
1049 : : /* Get this value the long way, since TYPE_METHOD_BASETYPE
1050 : : is just the main variant of this. */
1051 : 4033364 : tree basetype = class_of_this_parm (t2);
1052 : 4033364 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1053 : 4033364 : TYPE_RAISES_EXCEPTIONS (t2));
1054 : 4033364 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1055 : 4033364 : tree t3;
1056 : 4033364 : bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1057 : :
1058 : : /* If this was a member function type, get back to the
1059 : : original type of type member function (i.e., without
1060 : : the class instance variable up front. */
1061 : 4033364 : t1 = build_function_type (TREE_TYPE (t1),
1062 : 4033364 : TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1063 : 4033364 : t2 = build_function_type (TREE_TYPE (t2),
1064 : 4033364 : TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1065 : 4033364 : t3 = merge_types (t1, t2);
1066 : 4033364 : t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1067 : 4033364 : TYPE_ARG_TYPES (t3));
1068 : 4033364 : t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1069 : 4033364 : break;
1070 : : }
1071 : :
1072 : : case TYPENAME_TYPE:
1073 : : /* There is no need to merge attributes into a TYPENAME_TYPE.
1074 : : When the type is instantiated it will have whatever
1075 : : attributes result from the instantiation. */
1076 : : return t1;
1077 : :
1078 : 5310295 : default:;
1079 : 5310295 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1080 : : return t1;
1081 : 12 : else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1082 : : return t2;
1083 : : break;
1084 : : }
1085 : :
1086 : 8307373 : return cp_build_type_attribute_variant (t1, attributes);
1087 : : }
1088 : :
1089 : : /* Return the ARRAY_TYPE type without its domain. */
1090 : :
1091 : : tree
1092 : 468 : strip_array_domain (tree type)
1093 : : {
1094 : 468 : tree t2;
1095 : 468 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1096 : 468 : if (TYPE_DOMAIN (type) == NULL_TREE)
1097 : : return type;
1098 : 209 : t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
1099 : 209 : return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
1100 : : }
1101 : :
1102 : : /* Wrapper around cp_common_type that is used by c-common.cc and other
1103 : : front end optimizations that remove promotions.
1104 : :
1105 : : Return the common type for two arithmetic types T1 and T2 under the
1106 : : usual arithmetic conversions. The default conversions have already
1107 : : been applied, and enumerated types converted to their compatible
1108 : : integer types. */
1109 : :
1110 : : tree
1111 : 463722 : common_type (tree t1, tree t2)
1112 : : {
1113 : : /* If one type is nonsense, use the other */
1114 : 463722 : if (t1 == error_mark_node)
1115 : : return t2;
1116 : 463722 : if (t2 == error_mark_node)
1117 : : return t1;
1118 : :
1119 : 463722 : return cp_common_type (t1, t2);
1120 : : }
1121 : :
1122 : : /* Return the common type of two pointer types T1 and T2. This is the
1123 : : type for the result of most arithmetic operations if the operands
1124 : : have the given two types.
1125 : :
1126 : : We assume that comp_target_types has already been done and returned
1127 : : nonzero; if that isn't so, this may crash. */
1128 : :
1129 : : tree
1130 : 1260249 : common_pointer_type (tree t1, tree t2)
1131 : : {
1132 : 1260249 : gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
1133 : : || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
1134 : : || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
1135 : :
1136 : 1260249 : return composite_pointer_type (input_location, t1, t2,
1137 : : error_mark_node, error_mark_node,
1138 : 1260249 : CPO_CONVERSION, tf_warning_or_error);
1139 : : }
1140 : :
1141 : : /* Compare two exception specifier types for exactness or subsetness, if
1142 : : allowed. Returns false for mismatch, true for match (same, or
1143 : : derived and !exact).
1144 : :
1145 : : [except.spec] "If a class X ... objects of class X or any class publicly
1146 : : and unambiguously derived from X. Similarly, if a pointer type Y * ...
1147 : : exceptions of type Y * or that are pointers to any type publicly and
1148 : : unambiguously derived from Y. Otherwise a function only allows exceptions
1149 : : that have the same type ..."
1150 : : This does not mention cv qualifiers and is different to what throw
1151 : : [except.throw] and catch [except.catch] will do. They will ignore the
1152 : : top level cv qualifiers, and allow qualifiers in the pointer to class
1153 : : example.
1154 : :
1155 : : We implement the letter of the standard. */
1156 : :
1157 : : static bool
1158 : 1782 : comp_except_types (tree a, tree b, bool exact)
1159 : : {
1160 : 1782 : if (same_type_p (a, b))
1161 : : return true;
1162 : 508 : else if (!exact)
1163 : : {
1164 : 12 : if (cp_type_quals (a) || cp_type_quals (b))
1165 : 0 : return false;
1166 : :
1167 : 12 : if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1168 : : {
1169 : 7 : a = TREE_TYPE (a);
1170 : 7 : b = TREE_TYPE (b);
1171 : 7 : if (cp_type_quals (a) || cp_type_quals (b))
1172 : 1 : return false;
1173 : : }
1174 : :
1175 : 11 : if (TREE_CODE (a) != RECORD_TYPE
1176 : 11 : || TREE_CODE (b) != RECORD_TYPE)
1177 : : return false;
1178 : :
1179 : 10 : if (publicly_uniquely_derived_p (a, b))
1180 : : return true;
1181 : : }
1182 : : return false;
1183 : : }
1184 : :
1185 : : /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1186 : : If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1187 : : If EXACT is ce_type, the C++17 type compatibility rules apply.
1188 : : If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1189 : : If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1190 : : are unordered, but we've already filtered out duplicates. Most lists will
1191 : : be in order, we should try to make use of that. */
1192 : :
1193 : : bool
1194 : 1423420307 : comp_except_specs (const_tree t1, const_tree t2, int exact)
1195 : : {
1196 : 1423420307 : const_tree probe;
1197 : 1423420307 : const_tree base;
1198 : 1423420307 : int length = 0;
1199 : :
1200 : 1423420307 : if (t1 == t2)
1201 : : return true;
1202 : :
1203 : : /* First handle noexcept. */
1204 : 558512871 : if (exact < ce_exact)
1205 : : {
1206 : 7778605 : if (exact == ce_type
1207 : 15491423 : && (canonical_eh_spec (CONST_CAST_TREE (t1))
1208 : 7712818 : == canonical_eh_spec (CONST_CAST_TREE (t2))))
1209 : : return true;
1210 : :
1211 : : /* noexcept(false) is compatible with no exception-specification,
1212 : : and less strict than any spec. */
1213 : 7760079 : if (t1 == noexcept_false_spec)
1214 : 153 : return t2 == NULL_TREE || exact == ce_derived;
1215 : : /* Even a derived noexcept(false) is compatible with no
1216 : : exception-specification. */
1217 : 7759926 : if (t2 == noexcept_false_spec)
1218 : 1645 : return t1 == NULL_TREE;
1219 : :
1220 : : /* Otherwise, if we aren't looking for an exact match, noexcept is
1221 : : equivalent to throw(). */
1222 : 7758281 : if (t1 == noexcept_true_spec)
1223 : 709714 : t1 = empty_except_spec;
1224 : 7758281 : if (t2 == noexcept_true_spec)
1225 : 6765201 : t2 = empty_except_spec;
1226 : : }
1227 : :
1228 : : /* If any noexcept is left, it is only comparable to itself;
1229 : : either we're looking for an exact match or we're redeclaring a
1230 : : template with dependent noexcept. */
1231 : 535425244 : if ((t1 && TREE_PURPOSE (t1))
1232 : 584448803 : || (t2 && TREE_PURPOSE (t2)))
1233 : 548668833 : return (t1 && t2
1234 : 548668833 : && (exact == ce_exact
1235 : 63047467 : ? TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
1236 : 280456 : : cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))));
1237 : :
1238 : 9823714 : if (t1 == NULL_TREE) /* T1 is ... */
1239 : 6997109 : return t2 == NULL_TREE || exact == ce_derived;
1240 : 2826605 : if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1241 : 2793005 : return t2 != NULL_TREE && !TREE_VALUE (t2);
1242 : 34890 : if (t2 == NULL_TREE) /* T2 is ... */
1243 : : return false;
1244 : 1660 : if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1245 : 76 : return exact == ce_derived;
1246 : :
1247 : : /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1248 : : Count how many we find, to determine exactness. For exact matching and
1249 : : ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1250 : : O(nm). */
1251 : 2861 : for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1252 : : {
1253 : 2180 : for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1254 : : {
1255 : 1782 : tree a = TREE_VALUE (probe);
1256 : 1782 : tree b = TREE_VALUE (t2);
1257 : :
1258 : 1782 : if (comp_except_types (a, b, exact))
1259 : : {
1260 : 1277 : if (probe == base && exact > ce_derived)
1261 : 1242 : base = TREE_CHAIN (probe);
1262 : 1277 : length++;
1263 : 1277 : break;
1264 : : }
1265 : : }
1266 : 1277 : if (probe == NULL_TREE)
1267 : : return false;
1268 : : }
1269 : 1186 : return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1270 : : }
1271 : :
1272 : : /* Compare the array types T1 and T2. CB says how we should behave when
1273 : : comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1274 : : bounds_either says than any array can be [], bounds_first means that
1275 : : onlt T1 can be an array with unknown bounds. STRICT is true if
1276 : : qualifiers must match when comparing the types of the array elements. */
1277 : :
1278 : : static bool
1279 : 1390881 : comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1280 : : bool strict)
1281 : : {
1282 : 1390881 : tree d1;
1283 : 1390881 : tree d2;
1284 : 1390881 : tree max1, max2;
1285 : :
1286 : 1390881 : if (t1 == t2)
1287 : : return true;
1288 : :
1289 : : /* The type of the array elements must be the same. */
1290 : 1390789 : if (strict
1291 : 1390789 : ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1292 : 2829 : : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1293 : : return false;
1294 : :
1295 : 931684 : d1 = TYPE_DOMAIN (t1);
1296 : 931684 : d2 = TYPE_DOMAIN (t2);
1297 : :
1298 : 931684 : if (d1 == d2)
1299 : : return true;
1300 : :
1301 : : /* If one of the arrays is dimensionless, and the other has a
1302 : : dimension, they are of different types. However, it is valid to
1303 : : write:
1304 : :
1305 : : extern int a[];
1306 : : int a[3];
1307 : :
1308 : : by [basic.link]:
1309 : :
1310 : : declarations for an array object can specify
1311 : : array types that differ by the presence or absence of a major
1312 : : array bound (_dcl.array_). */
1313 : 610461 : if (!d1 && d2)
1314 : 2342 : return cb >= bounds_either;
1315 : 608119 : else if (d1 && !d2)
1316 : 6568 : return cb == bounds_either;
1317 : :
1318 : : /* Check that the dimensions are the same. */
1319 : :
1320 : 601551 : if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1321 : : return false;
1322 : 601530 : max1 = TYPE_MAX_VALUE (d1);
1323 : 601530 : max2 = TYPE_MAX_VALUE (d2);
1324 : :
1325 : 601530 : if (!cp_tree_equal (max1, max2))
1326 : : return false;
1327 : :
1328 : : return true;
1329 : : }
1330 : :
1331 : : /* Compare the relative position of T1 and T2 into their respective
1332 : : template parameter list.
1333 : : T1 and T2 must be template parameter types.
1334 : : Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1335 : :
1336 : : static bool
1337 : 1573763984 : comp_template_parms_position (tree t1, tree t2)
1338 : : {
1339 : 1573763984 : tree index1, index2;
1340 : 1573763984 : gcc_assert (t1 && t2
1341 : : && TREE_CODE (t1) == TREE_CODE (t2)
1342 : : && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1343 : : || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1344 : : || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1345 : :
1346 : 1573763984 : index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1347 : 1573763984 : index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1348 : :
1349 : : /* Then compare their relative position. */
1350 : 1573763984 : if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1351 : 989688614 : || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1352 : 2235351722 : || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1353 : 661587738 : != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1354 : : return false;
1355 : :
1356 : : /* In C++14 we can end up comparing 'auto' to a normal template
1357 : : parameter. Don't confuse them. */
1358 : 588546287 : if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1359 : 113563724 : return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1360 : :
1361 : : return true;
1362 : : }
1363 : :
1364 : : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1365 : :
1366 : : static bool
1367 : 511 : cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1368 : : {
1369 : 511 : t1 = TYPE_MAIN_VARIANT (t1);
1370 : 511 : t2 = TYPE_MAIN_VARIANT (t2);
1371 : :
1372 : 511 : if (TYPE_PTR_P (t1)
1373 : 141 : && TYPE_PTR_P (t2))
1374 : : return true;
1375 : :
1376 : : /* Only the precision of the parameter matters. This check should
1377 : : make sure that the callee does not see undefined values in argument
1378 : : registers. */
1379 : 376 : if (INTEGRAL_TYPE_P (t1)
1380 : 67 : && INTEGRAL_TYPE_P (t2)
1381 : 422 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1382 : : return true;
1383 : :
1384 : 333 : return same_type_p (t1, t2);
1385 : : }
1386 : :
1387 : : /* Check if a type cast between two function types can be considered safe. */
1388 : :
1389 : : static bool
1390 : 6737 : cxx_safe_function_type_cast_p (tree t1, tree t2)
1391 : : {
1392 : 6737 : if (TREE_TYPE (t1) == void_type_node &&
1393 : 6665 : TYPE_ARG_TYPES (t1) == void_list_node)
1394 : : return true;
1395 : :
1396 : 5328 : if (TREE_TYPE (t2) == void_type_node &&
1397 : 5123 : TYPE_ARG_TYPES (t2) == void_list_node)
1398 : : return true;
1399 : :
1400 : 272 : if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1401 : : return false;
1402 : :
1403 : 111 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1404 : 314 : t1 && t2;
1405 : 203 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1406 : 239 : if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1407 : : return false;
1408 : :
1409 : : return true;
1410 : : }
1411 : :
1412 : : /* Subroutine in comptypes. */
1413 : :
1414 : : static bool
1415 : 12552201812 : structural_comptypes (tree t1, tree t2, int strict)
1416 : : {
1417 : : /* Both should be types that are not obviously the same. */
1418 : 12552201812 : gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1419 : :
1420 : : /* Suppress typename resolution under spec_hasher::equal in place of calling
1421 : : push_to_top_level there. */
1422 : 12552201812 : if (!comparing_specializations)
1423 : : {
1424 : : /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1425 : : current instantiation. */
1426 : 10393794899 : if (TREE_CODE (t1) == TYPENAME_TYPE)
1427 : 91021670 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1428 : :
1429 : 10393794899 : if (TREE_CODE (t2) == TYPENAME_TYPE)
1430 : 111533573 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1431 : : }
1432 : :
1433 : 12552201812 : if (TYPE_PTRMEMFUNC_P (t1))
1434 : 31827909 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1435 : 12552201812 : if (TYPE_PTRMEMFUNC_P (t2))
1436 : 35159133 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1437 : :
1438 : : /* Different classes of types can't be compatible. */
1439 : 12552201812 : if (TREE_CODE (t1) != TREE_CODE (t2))
1440 : : return false;
1441 : :
1442 : : /* Qualifiers must match. For array types, we will check when we
1443 : : recur on the array element types. */
1444 : 8090868565 : if (TREE_CODE (t1) != ARRAY_TYPE
1445 : 8090868565 : && cp_type_quals (t1) != cp_type_quals (t2))
1446 : : return false;
1447 : 7601851719 : if (TREE_CODE (t1) == FUNCTION_TYPE
1448 : 7601851719 : && type_memfn_quals (t1) != type_memfn_quals (t2))
1449 : : return false;
1450 : : /* Need to check this before TYPE_MAIN_VARIANT.
1451 : : FIXME function qualifiers should really change the main variant. */
1452 : 7601745507 : if (FUNC_OR_METHOD_TYPE_P (t1))
1453 : : {
1454 : 38536422 : if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1455 : : return false;
1456 : 19483046 : if (flag_noexcept_type
1457 : 38858733 : && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1458 : 19375687 : TYPE_RAISES_EXCEPTIONS (t2),
1459 : : ce_type))
1460 : : return false;
1461 : : }
1462 : :
1463 : : /* Allow for two different type nodes which have essentially the same
1464 : : definition. Note that we already checked for equality of the type
1465 : : qualifiers (just above). */
1466 : 7575178995 : if (TREE_CODE (t1) != ARRAY_TYPE
1467 : 7575178995 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1468 : 330706106 : goto check_alias;
1469 : :
1470 : : /* Compare the types. Return false on known not-same. Break on not
1471 : : known. Never return true from this switch -- you'll break
1472 : : specialization comparison. */
1473 : 7244472889 : switch (TREE_CODE (t1))
1474 : : {
1475 : : case VOID_TYPE:
1476 : : case BOOLEAN_TYPE:
1477 : : /* All void and bool types are the same. */
1478 : : break;
1479 : :
1480 : 513492579 : case OPAQUE_TYPE:
1481 : 513492579 : case INTEGER_TYPE:
1482 : 513492579 : case FIXED_POINT_TYPE:
1483 : 513492579 : case REAL_TYPE:
1484 : : /* With these nodes, we can't determine type equivalence by
1485 : : looking at what is stored in the nodes themselves, because
1486 : : two nodes might have different TYPE_MAIN_VARIANTs but still
1487 : : represent the same type. For example, wchar_t and int could
1488 : : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1489 : : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1490 : : and are distinct types. On the other hand, int and the
1491 : : following typedef
1492 : :
1493 : : typedef int INT __attribute((may_alias));
1494 : :
1495 : : have identical properties, different TYPE_MAIN_VARIANTs, but
1496 : : represent the same type. The canonical type system keeps
1497 : : track of equivalence in this case, so we fall back on it. */
1498 : 513492579 : if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1499 : : return false;
1500 : :
1501 : : /* We don't need or want the attribute comparison. */
1502 : 8399 : goto check_alias;
1503 : :
1504 : 956011 : case TEMPLATE_TEMPLATE_PARM:
1505 : 956011 : case BOUND_TEMPLATE_TEMPLATE_PARM:
1506 : 956011 : if (!comp_template_parms_position (t1, t2))
1507 : : return false;
1508 : 837956 : if (!comp_template_parms
1509 : 1675912 : (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1510 : 2355049 : DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1511 : : return false;
1512 : 633936 : if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1513 : : break;
1514 : : /* Don't check inheritance. */
1515 : : strict = COMPARE_STRICT;
1516 : : /* Fall through. */
1517 : :
1518 : 3616619456 : case RECORD_TYPE:
1519 : 3616619456 : case UNION_TYPE:
1520 : 6367020092 : if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1521 : 2233498434 : && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1522 : 1724959700 : || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1523 : 4125200085 : && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1524 : : break;
1525 : :
1526 : 3577883494 : if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1527 : : break;
1528 : 3577883461 : else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1529 : : break;
1530 : :
1531 : : return false;
1532 : :
1533 : 44513 : case OFFSET_TYPE:
1534 : 44513 : if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1535 : : strict & ~COMPARE_REDECLARATION))
1536 : : return false;
1537 : 42522 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1538 : : return false;
1539 : : break;
1540 : :
1541 : 795664788 : case REFERENCE_TYPE:
1542 : 795664788 : if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1543 : : return false;
1544 : : /* fall through to checks for pointer types */
1545 : 1120566967 : gcc_fallthrough ();
1546 : :
1547 : 1120566967 : case POINTER_TYPE:
1548 : 1120566967 : if (TYPE_MODE (t1) != TYPE_MODE (t2)
1549 : 1120566967 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1550 : 1010191960 : return false;
1551 : : break;
1552 : :
1553 : 11648873 : case METHOD_TYPE:
1554 : 11648873 : case FUNCTION_TYPE:
1555 : : /* Exception specs and memfn_rquals were checked above. */
1556 : 11648873 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1557 : : return false;
1558 : 11022922 : if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1559 : : return false;
1560 : : break;
1561 : :
1562 : 1387960 : case ARRAY_TYPE:
1563 : : /* Target types must match incl. qualifiers. */
1564 : 1387960 : if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1565 : 1387960 : ? bounds_either : bounds_none),
1566 : : /*strict=*/true))
1567 : : return false;
1568 : : break;
1569 : :
1570 : 1572807973 : case TEMPLATE_TYPE_PARM:
1571 : : /* If T1 and T2 don't have the same relative position in their
1572 : : template parameters set, they can't be equal. */
1573 : 1572807973 : if (!comp_template_parms_position (t1, t2))
1574 : : return false;
1575 : : /* If T1 and T2 don't represent the same class template deduction,
1576 : : they aren't equal. */
1577 : 1027197670 : if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1578 : 513598835 : CLASS_PLACEHOLDER_TEMPLATE (t2)))
1579 : : return false;
1580 : : /* Constrained 'auto's are distinct from parms that don't have the same
1581 : : constraints. */
1582 : 497826298 : if (!equivalent_placeholder_constraints (t1, t2))
1583 : : return false;
1584 : : break;
1585 : :
1586 : 137346156 : case TYPENAME_TYPE:
1587 : 274692312 : if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1588 : 137346156 : TYPENAME_TYPE_FULLNAME (t2)))
1589 : : return false;
1590 : : /* Qualifiers don't matter on scopes. */
1591 : 120257564 : if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1592 : 120257564 : TYPE_CONTEXT (t2)))
1593 : : return false;
1594 : : break;
1595 : :
1596 : 7797 : case UNBOUND_CLASS_TEMPLATE:
1597 : 7797 : if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1598 : : return false;
1599 : 7797 : if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1600 : : return false;
1601 : : break;
1602 : :
1603 : 3235902 : case COMPLEX_TYPE:
1604 : 3235902 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1605 : : return false;
1606 : : break;
1607 : :
1608 : 11104390 : case VECTOR_TYPE:
1609 : 11104390 : if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1610 : 20772671 : || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1611 : 20820290 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1612 : 1436109 : return false;
1613 : : break;
1614 : :
1615 : 6693281 : case TYPE_PACK_EXPANSION:
1616 : 6693281 : return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1617 : : PACK_EXPANSION_PATTERN (t2))
1618 : 13231240 : && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1619 : 6537959 : PACK_EXPANSION_EXTRA_ARGS (t2)));
1620 : :
1621 : 14 : case PACK_INDEX_TYPE:
1622 : 14 : return (cp_tree_equal (PACK_INDEX_PACK (t1),
1623 : 14 : PACK_INDEX_PACK (t2))
1624 : 16 : && cp_tree_equal (PACK_INDEX_INDEX (t1),
1625 : 2 : PACK_INDEX_INDEX (t2)));
1626 : :
1627 : 14584395 : case DECLTYPE_TYPE:
1628 : 29168790 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1629 : 14584395 : != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1630 : : return false;
1631 : 14539205 : if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1632 : : return false;
1633 : 14539205 : if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1634 : : return false;
1635 : 14539205 : if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1636 : : return false;
1637 : : break;
1638 : :
1639 : 727070 : case TRAIT_TYPE:
1640 : 727070 : if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1641 : : return false;
1642 : 1325 : if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1643 : 2650 : || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1644 : 0 : return false;
1645 : : break;
1646 : :
1647 : 3 : case TYPEOF_TYPE:
1648 : 3 : if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1649 : : return false;
1650 : : break;
1651 : :
1652 : : default:
1653 : : return false;
1654 : : }
1655 : :
1656 : : /* If we get here, we know that from a target independent POV the
1657 : : types are the same. Make sure the target attributes are also
1658 : : the same. */
1659 : 668507193 : if (!comp_type_attributes (t1, t2))
1660 : : return false;
1661 : :
1662 : 668506049 : check_alias:
1663 : 999220554 : if (comparing_dependent_aliases
1664 : 999220554 : && (typedef_variant_p (t1) || typedef_variant_p (t2)))
1665 : : {
1666 : 2773521 : tree dep1 = dependent_opaque_alias_p (t1) ? t1 : NULL_TREE;
1667 : 2773521 : tree dep2 = dependent_opaque_alias_p (t2) ? t2 : NULL_TREE;
1668 : 2773521 : if ((dep1 || dep2)
1669 : 2773521 : && (!(dep1 && dep2)
1670 : 0 : || !comp_type_attributes (DECL_ATTRIBUTES (TYPE_NAME (dep1)),
1671 : 0 : DECL_ATTRIBUTES (TYPE_NAME (dep2)))))
1672 : 12 : return false;
1673 : :
1674 : : /* Don't treat an alias template specialization with dependent
1675 : : arguments as equivalent to its underlying type when used as a
1676 : : template argument; we need them to be distinct so that we
1677 : : substitute into the specialization arguments at instantiation
1678 : : time. And aliases can't be equivalent without being ==, so
1679 : : we don't need to look any deeper. */
1680 : 2773509 : ++processing_template_decl;
1681 : 2773509 : dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1682 : 2773509 : dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1683 : 2773509 : --processing_template_decl;
1684 : 2773509 : if ((dep1 || dep2) && dep1 != dep2)
1685 : : return false;
1686 : : }
1687 : :
1688 : : return true;
1689 : : }
1690 : :
1691 : : /* C++ implementation of compatible_types_for_indirection_note_p. */
1692 : :
1693 : : bool
1694 : 1675 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1695 : : {
1696 : 1675 : return same_type_p (type1, type2);
1697 : : }
1698 : :
1699 : : /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1700 : : is a bitwise-or of the COMPARE_* flags. */
1701 : :
1702 : : bool
1703 : 19411731863 : comptypes (tree t1, tree t2, int strict)
1704 : : {
1705 : 19411731863 : gcc_checking_assert (t1 && t2);
1706 : :
1707 : : /* TYPE_ARGUMENT_PACKS are not really types. */
1708 : 19411731863 : gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1709 : : && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1710 : :
1711 : 19411731863 : if (t1 == t2)
1712 : : return true;
1713 : :
1714 : : /* Suppress errors caused by previously reported errors. */
1715 : 12552203011 : if (t1 == error_mark_node || t2 == error_mark_node)
1716 : : return false;
1717 : :
1718 : 12552202815 : if (strict == COMPARE_STRICT)
1719 : : {
1720 : 11296813589 : if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1721 : : /* At least one of the types requires structural equality, so
1722 : : perform a deep check. */
1723 : 961611517 : return structural_comptypes (t1, t2, strict);
1724 : :
1725 : 10335202072 : if (flag_checking && param_use_canonical_types)
1726 : : {
1727 : 10335201069 : bool result = structural_comptypes (t1, t2, strict);
1728 : :
1729 : 10335201069 : if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1730 : : /* The two types are structurally equivalent, but their
1731 : : canonical types were different. This is a failure of the
1732 : : canonical type propagation code.*/
1733 : 0 : internal_error
1734 : 0 : ("canonical types differ for identical types %qT and %qT",
1735 : : t1, t2);
1736 : 10335201069 : else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1737 : : /* Two types are structurally different, but the canonical
1738 : : types are the same. This means we were over-eager in
1739 : : assigning canonical types. */
1740 : 0 : internal_error
1741 : 0 : ("same canonical type node for different types %qT and %qT",
1742 : : t1, t2);
1743 : :
1744 : : return result;
1745 : : }
1746 : 1003 : if (!flag_checking && param_use_canonical_types)
1747 : 1003 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1748 : : else
1749 : 0 : return structural_comptypes (t1, t2, strict);
1750 : : }
1751 : 1255389226 : else if (strict == COMPARE_STRUCTURAL)
1752 : 1218183281 : return structural_comptypes (t1, t2, COMPARE_STRICT);
1753 : : else
1754 : 37205945 : return structural_comptypes (t1, t2, strict);
1755 : : }
1756 : :
1757 : : /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1758 : : top-level qualifiers. */
1759 : :
1760 : : bool
1761 : 2797309369 : same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1762 : : {
1763 : 2797309369 : if (type1 == error_mark_node || type2 == error_mark_node)
1764 : : return false;
1765 : 2797309359 : if (type1 == type2)
1766 : : return true;
1767 : :
1768 : 1543090474 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1769 : 1543090474 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1770 : 1543090474 : return same_type_p (type1, type2);
1771 : : }
1772 : :
1773 : : /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1774 : :
1775 : : bool
1776 : 219096498 : similar_type_p (tree type1, tree type2)
1777 : : {
1778 : 219096498 : if (type1 == error_mark_node || type2 == error_mark_node)
1779 : : return false;
1780 : :
1781 : : /* Informally, two types are similar if, ignoring top-level cv-qualification:
1782 : : * they are the same type; or
1783 : : * they are both pointers, and the pointed-to types are similar; or
1784 : : * they are both pointers to member of the same class, and the types of
1785 : : the pointed-to members are similar; or
1786 : : * they are both arrays of the same size or both arrays of unknown bound,
1787 : : and the array element types are similar. */
1788 : :
1789 : 219096498 : if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1790 : : return true;
1791 : :
1792 : 98731480 : if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1793 : 98618372 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1794 : 98618372 : || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1795 : 113574 : return comp_ptr_ttypes_const (type1, type2, bounds_either);
1796 : :
1797 : : return false;
1798 : : }
1799 : :
1800 : : /* Helper function for layout_compatible_type_p and
1801 : : is_corresponding_member_aggr. Advance to next members (NULL if
1802 : : no further ones) and return true if those members are still part of
1803 : : the common initial sequence. */
1804 : :
1805 : : bool
1806 : 1996 : next_common_initial_sequence (tree &memb1, tree &memb2)
1807 : : {
1808 : 2456 : while (memb1)
1809 : : {
1810 : 2492 : if (TREE_CODE (memb1) != FIELD_DECL
1811 : 2090 : || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1812 : : {
1813 : 402 : memb1 = DECL_CHAIN (memb1);
1814 : 402 : continue;
1815 : : }
1816 : 1688 : if (DECL_FIELD_IS_BASE (memb1))
1817 : : {
1818 : 58 : memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1819 : 58 : continue;
1820 : : }
1821 : : break;
1822 : : }
1823 : 2426 : while (memb2)
1824 : : {
1825 : 2462 : if (TREE_CODE (memb2) != FIELD_DECL
1826 : 2060 : || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1827 : : {
1828 : 402 : memb2 = DECL_CHAIN (memb2);
1829 : 402 : continue;
1830 : : }
1831 : 1658 : if (DECL_FIELD_IS_BASE (memb2))
1832 : : {
1833 : 28 : memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1834 : 28 : continue;
1835 : : }
1836 : : break;
1837 : : }
1838 : 1996 : if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1839 : : return true;
1840 : 1630 : if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1841 : : return false;
1842 : 1630 : if (DECL_BIT_FIELD_TYPE (memb1))
1843 : : {
1844 : 138 : if (!DECL_BIT_FIELD_TYPE (memb2))
1845 : : return false;
1846 : 39 : if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1847 : 39 : DECL_BIT_FIELD_TYPE (memb2)))
1848 : : return false;
1849 : 39 : if (TYPE_PRECISION (TREE_TYPE (memb1))
1850 : 39 : != TYPE_PRECISION (TREE_TYPE (memb2)))
1851 : : return false;
1852 : : }
1853 : 1492 : else if (DECL_BIT_FIELD_TYPE (memb2))
1854 : : return false;
1855 : 1465 : else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1856 : : return false;
1857 : 1441 : if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1858 : 1441 : != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1859 : : return false;
1860 : 1414 : if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1861 : : return false;
1862 : 1383 : if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1863 : : return false;
1864 : : return true;
1865 : : }
1866 : :
1867 : : /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1868 : :
1869 : : bool
1870 : 2503 : layout_compatible_type_p (tree type1, tree type2)
1871 : : {
1872 : 2503 : if (type1 == error_mark_node || type2 == error_mark_node)
1873 : : return false;
1874 : 2503 : if (type1 == type2)
1875 : : return true;
1876 : 1222 : if (TREE_CODE (type1) != TREE_CODE (type2))
1877 : : return false;
1878 : :
1879 : 1184 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1880 : 1184 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1881 : :
1882 : 1184 : if (TREE_CODE (type1) == ENUMERAL_TYPE)
1883 : 32 : return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1884 : 32 : && same_type_p (finish_underlying_type (type1),
1885 : : finish_underlying_type (type2)));
1886 : :
1887 : 367 : if (CLASS_TYPE_P (type1)
1888 : 367 : && std_layout_type_p (type1)
1889 : 357 : && std_layout_type_p (type2)
1890 : 1509 : && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1891 : : {
1892 : 315 : tree field1 = TYPE_FIELDS (type1);
1893 : 315 : tree field2 = TYPE_FIELDS (type2);
1894 : 315 : if (TREE_CODE (type1) == RECORD_TYPE)
1895 : : {
1896 : 966 : while (1)
1897 : : {
1898 : 606 : if (!next_common_initial_sequence (field1, field2))
1899 : : return false;
1900 : 600 : if (field1 == NULL_TREE)
1901 : : return true;
1902 : 360 : field1 = DECL_CHAIN (field1);
1903 : 360 : field2 = DECL_CHAIN (field2);
1904 : : }
1905 : : }
1906 : : /* Otherwise both types must be union types.
1907 : : The standard says:
1908 : : "Two standard-layout unions are layout-compatible if they have
1909 : : the same number of non-static data members and corresponding
1910 : : non-static data members (in any order) have layout-compatible
1911 : : types."
1912 : : but the code anticipates that bitfield vs. non-bitfield,
1913 : : different bitfield widths or presence/absence of
1914 : : [[no_unique_address]] should be checked as well. */
1915 : 69 : auto_vec<tree, 16> vec;
1916 : 69 : unsigned int count = 0;
1917 : 315 : for (; field1; field1 = DECL_CHAIN (field1))
1918 : 246 : if (TREE_CODE (field1) == FIELD_DECL)
1919 : 162 : count++;
1920 : 315 : for (; field2; field2 = DECL_CHAIN (field2))
1921 : 246 : if (TREE_CODE (field2) == FIELD_DECL)
1922 : 162 : vec.safe_push (field2);
1923 : : /* Discussions on core lean towards treating multiple union fields
1924 : : of the same type as the same field, so this might need changing
1925 : : in the future. */
1926 : 138 : if (count != vec.length ())
1927 : : return false;
1928 : 297 : for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1929 : : {
1930 : 234 : if (TREE_CODE (field1) != FIELD_DECL)
1931 : 78 : continue;
1932 : 156 : unsigned int j;
1933 : 156 : tree t1 = DECL_BIT_FIELD_TYPE (field1);
1934 : 156 : if (t1 == NULL_TREE)
1935 : 153 : t1 = TREE_TYPE (field1);
1936 : 258 : FOR_EACH_VEC_ELT (vec, j, field2)
1937 : : {
1938 : 252 : tree t2 = DECL_BIT_FIELD_TYPE (field2);
1939 : 252 : if (t2 == NULL_TREE)
1940 : 240 : t2 = TREE_TYPE (field2);
1941 : 252 : if (DECL_BIT_FIELD_TYPE (field1))
1942 : : {
1943 : 6 : if (!DECL_BIT_FIELD_TYPE (field2))
1944 : 0 : continue;
1945 : 6 : if (TYPE_PRECISION (TREE_TYPE (field1))
1946 : 6 : != TYPE_PRECISION (TREE_TYPE (field2)))
1947 : 6 : continue;
1948 : : }
1949 : 246 : else if (DECL_BIT_FIELD_TYPE (field2))
1950 : 6 : continue;
1951 : 240 : if (!layout_compatible_type_p (t1, t2))
1952 : 90 : continue;
1953 : 150 : if ((!lookup_attribute ("no_unique_address",
1954 : 150 : DECL_ATTRIBUTES (field1)))
1955 : 150 : != !lookup_attribute ("no_unique_address",
1956 : 150 : DECL_ATTRIBUTES (field2)))
1957 : 0 : continue;
1958 : : break;
1959 : : }
1960 : 312 : if (j == vec.length ())
1961 : : return false;
1962 : 150 : vec.unordered_remove (j);
1963 : : }
1964 : : return true;
1965 : 69 : }
1966 : :
1967 : 837 : return same_type_p (type1, type2);
1968 : : }
1969 : :
1970 : : /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1971 : :
1972 : : bool
1973 : 158876038 : at_least_as_qualified_p (const_tree type1, const_tree type2)
1974 : : {
1975 : 158876038 : int q1 = cp_type_quals (type1);
1976 : 158876038 : int q2 = cp_type_quals (type2);
1977 : :
1978 : : /* All qualifiers for TYPE2 must also appear in TYPE1. */
1979 : 158876038 : return (q1 & q2) == q2;
1980 : : }
1981 : :
1982 : : /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1983 : : more cv-qualified that TYPE1, and 0 otherwise. */
1984 : :
1985 : : int
1986 : 10215964 : comp_cv_qualification (int q1, int q2)
1987 : : {
1988 : 10215964 : if (q1 == q2)
1989 : : return 0;
1990 : :
1991 : 3064645 : if ((q1 & q2) == q2)
1992 : : return 1;
1993 : 247046 : else if ((q1 & q2) == q1)
1994 : 246457 : return -1;
1995 : :
1996 : : return 0;
1997 : : }
1998 : :
1999 : : int
2000 : 0 : comp_cv_qualification (const_tree type1, const_tree type2)
2001 : : {
2002 : 0 : int q1 = cp_type_quals (type1);
2003 : 0 : int q2 = cp_type_quals (type2);
2004 : 0 : return comp_cv_qualification (q1, q2);
2005 : : }
2006 : :
2007 : : /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
2008 : : subset of the cv-qualification signature of TYPE2, and the types
2009 : : are similar. Returns -1 if the other way 'round, and 0 otherwise. */
2010 : :
2011 : : int
2012 : 377 : comp_cv_qual_signature (tree type1, tree type2)
2013 : : {
2014 : 377 : if (comp_ptr_ttypes_real (type2, type1, -1))
2015 : : return 1;
2016 : 291 : else if (comp_ptr_ttypes_real (type1, type2, -1))
2017 : : return -1;
2018 : : else
2019 : 279 : return 0;
2020 : : }
2021 : :
2022 : : /* Subroutines of `comptypes'. */
2023 : :
2024 : : /* Return true if two parameter type lists PARMS1 and PARMS2 are
2025 : : equivalent in the sense that functions with those parameter types
2026 : : can have equivalent types. The two lists must be equivalent,
2027 : : element by element. */
2028 : :
2029 : : bool
2030 : 852980517 : compparms (const_tree parms1, const_tree parms2)
2031 : : {
2032 : 852980517 : const_tree t1, t2;
2033 : :
2034 : : /* An unspecified parmlist matches any specified parmlist
2035 : : whose argument types don't need default promotions. */
2036 : :
2037 : 852980517 : for (t1 = parms1, t2 = parms2;
2038 : 1381493701 : t1 || t2;
2039 : 528513184 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2040 : : {
2041 : : /* If one parmlist is shorter than the other,
2042 : : they fail to match. */
2043 : 1301033201 : if (!t1 || !t2)
2044 : : return false;
2045 : 1300311347 : if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2046 : : return false;
2047 : : }
2048 : : return true;
2049 : : }
2050 : :
2051 : :
2052 : : /* Process a sizeof or alignof expression where the operand is a type.
2053 : : STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2054 : : or GNU (preferred alignment) semantics; it is ignored if OP is
2055 : : SIZEOF_EXPR. */
2056 : :
2057 : : tree
2058 : 22407781 : cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2059 : : bool std_alignof, bool complain)
2060 : : {
2061 : 22407781 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2062 : 22407781 : if (type == error_mark_node)
2063 : : return error_mark_node;
2064 : :
2065 : 22407779 : type = non_reference (type);
2066 : 22407779 : if (TREE_CODE (type) == METHOD_TYPE)
2067 : : {
2068 : 0 : if (complain)
2069 : : {
2070 : 0 : pedwarn (loc, OPT_Wpointer_arith,
2071 : : "invalid application of %qs to a member function",
2072 : 0 : OVL_OP_INFO (false, op)->name);
2073 : 0 : return size_one_node;
2074 : : }
2075 : : else
2076 : 0 : return error_mark_node;
2077 : : }
2078 : 22407779 : else if (VOID_TYPE_P (type) && std_alignof)
2079 : : {
2080 : 14 : if (complain)
2081 : 14 : error_at (loc, "invalid application of %qs to a void type",
2082 : 14 : OVL_OP_INFO (false, op)->name);
2083 : 14 : return error_mark_node;
2084 : : }
2085 : :
2086 : 22407765 : bool dependent_p = dependent_type_p (type);
2087 : 22407765 : if (!dependent_p)
2088 : : {
2089 : 18533999 : complete_type (type);
2090 : 18533999 : if (!COMPLETE_TYPE_P (type))
2091 : : /* Call this here because the incompleteness diagnostic comes from
2092 : : c_sizeof_or_alignof_type instead of
2093 : : complete_type_or_maybe_complain. */
2094 : 190 : note_failed_type_completion (type, complain);
2095 : : }
2096 : 18533999 : if (dependent_p
2097 : : /* VLA types will have a non-constant size. In the body of an
2098 : : uninstantiated template, we don't need to try to compute the
2099 : : value, because the sizeof expression is not an integral
2100 : : constant expression in that case. And, if we do try to
2101 : : compute the value, we'll likely end up with SAVE_EXPRs, which
2102 : : the template substitution machinery does not expect to see. */
2103 : 18533999 : || (processing_template_decl
2104 : 1720376 : && COMPLETE_TYPE_P (type)
2105 : 1720370 : && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2106 : : {
2107 : 3873766 : tree value = build_min (op, size_type_node, type);
2108 : 3873766 : TREE_READONLY (value) = 1;
2109 : 3873766 : if (op == ALIGNOF_EXPR && std_alignof)
2110 : 460679 : ALIGNOF_EXPR_STD_P (value) = true;
2111 : 3873766 : SET_EXPR_LOCATION (value, loc);
2112 : 3873766 : return value;
2113 : : }
2114 : :
2115 : 18533999 : return c_sizeof_or_alignof_type (loc, complete_type (type),
2116 : : op == SIZEOF_EXPR, std_alignof,
2117 : 18533999 : complain & (tf_warning_or_error));
2118 : : }
2119 : :
2120 : : /* Return the size of the type, without producing any warnings for
2121 : : types whose size cannot be taken. This routine should be used only
2122 : : in some other routine that has already produced a diagnostic about
2123 : : using the size of such a type. */
2124 : : tree
2125 : 2168904 : cxx_sizeof_nowarn (tree type)
2126 : : {
2127 : 2168904 : if (TREE_CODE (type) == FUNCTION_TYPE
2128 : 2168904 : || VOID_TYPE_P (type)
2129 : 2168822 : || TREE_CODE (type) == ERROR_MARK)
2130 : 82 : return size_one_node;
2131 : 2168822 : else if (!COMPLETE_TYPE_P (type))
2132 : 19 : return size_zero_node;
2133 : : else
2134 : 2168803 : return cxx_sizeof_or_alignof_type (input_location, type,
2135 : 2168803 : SIZEOF_EXPR, false, false);
2136 : : }
2137 : :
2138 : : /* Process a sizeof expression where the operand is an expression. */
2139 : :
2140 : : static tree
2141 : 1392578 : cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2142 : : {
2143 : 1392578 : if (e == error_mark_node)
2144 : : return error_mark_node;
2145 : :
2146 : 1391839 : if (instantiation_dependent_uneval_expression_p (e))
2147 : : {
2148 : 316881 : e = build_min (SIZEOF_EXPR, size_type_node, e);
2149 : 316881 : TREE_SIDE_EFFECTS (e) = 0;
2150 : 316881 : TREE_READONLY (e) = 1;
2151 : 316881 : SET_EXPR_LOCATION (e, loc);
2152 : :
2153 : 316881 : return e;
2154 : : }
2155 : :
2156 : 1074958 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2157 : 1074958 : STRIP_ANY_LOCATION_WRAPPER (e);
2158 : :
2159 : 1074958 : if (TREE_CODE (e) == PARM_DECL
2160 : 45028 : && DECL_ARRAY_PARAMETER_P (e)
2161 : 1075335 : && (complain & tf_warning))
2162 : : {
2163 : 120 : auto_diagnostic_group d;
2164 : 120 : if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2165 : : "%<sizeof%> on array function parameter %qE "
2166 : 120 : "will return size of %qT", e, TREE_TYPE (e)))
2167 : 24 : inform (DECL_SOURCE_LOCATION (e), "declared here");
2168 : 120 : }
2169 : :
2170 : 1074958 : e = mark_type_use (e);
2171 : :
2172 : 1074958 : if (bitfield_p (e))
2173 : : {
2174 : 12 : if (complain & tf_error)
2175 : 6 : error_at (e_loc,
2176 : : "invalid application of %<sizeof%> to a bit-field");
2177 : : else
2178 : 6 : return error_mark_node;
2179 : 6 : e = char_type_node;
2180 : : }
2181 : 1074946 : else if (is_overloaded_fn (e))
2182 : : {
2183 : 33 : if (complain & tf_error)
2184 : 12 : permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2185 : : "an expression of function type");
2186 : : else
2187 : 21 : return error_mark_node;
2188 : 12 : e = char_type_node;
2189 : : }
2190 : 1074913 : else if (type_unknown_p (e))
2191 : : {
2192 : 0 : if (complain & tf_error)
2193 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2194 : : else
2195 : 0 : return error_mark_node;
2196 : 0 : e = char_type_node;
2197 : : }
2198 : : else
2199 : 1074913 : e = TREE_TYPE (e);
2200 : :
2201 : 1074931 : return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2202 : 1074931 : complain & tf_error);
2203 : : }
2204 : :
2205 : : /* Implement the __alignof keyword: Return the minimum required
2206 : : alignment of E, measured in bytes. For VAR_DECL's and
2207 : : FIELD_DECL's return DECL_ALIGN (which can be set from an
2208 : : "aligned" __attribute__ specification). STD_ALIGNOF acts
2209 : : like in cxx_sizeof_or_alignof_type. */
2210 : :
2211 : : static tree
2212 : 101689 : cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2213 : : tsubst_flags_t complain)
2214 : : {
2215 : 101689 : tree t;
2216 : :
2217 : 101689 : if (e == error_mark_node)
2218 : : return error_mark_node;
2219 : :
2220 : 101681 : if (processing_template_decl)
2221 : : {
2222 : 19978 : e = build_min (ALIGNOF_EXPR, size_type_node, e);
2223 : 19978 : TREE_SIDE_EFFECTS (e) = 0;
2224 : 19978 : TREE_READONLY (e) = 1;
2225 : 19978 : SET_EXPR_LOCATION (e, loc);
2226 : 19978 : ALIGNOF_EXPR_STD_P (e) = std_alignof;
2227 : :
2228 : 19978 : return e;
2229 : : }
2230 : :
2231 : 81703 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2232 : 81703 : STRIP_ANY_LOCATION_WRAPPER (e);
2233 : :
2234 : 81703 : e = mark_type_use (e);
2235 : :
2236 : 81703 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2237 : : !(complain & tf_error)))
2238 : : {
2239 : 0 : if (!(complain & tf_error))
2240 : 0 : return error_mark_node;
2241 : 0 : t = size_one_node;
2242 : : }
2243 : 81703 : else if (VAR_P (e))
2244 : 12270 : t = size_int (DECL_ALIGN_UNIT (e));
2245 : 69433 : else if (bitfield_p (e))
2246 : : {
2247 : 6 : if (complain & tf_error)
2248 : 6 : error_at (e_loc,
2249 : : "invalid application of %<__alignof%> to a bit-field");
2250 : : else
2251 : 0 : return error_mark_node;
2252 : 6 : t = size_one_node;
2253 : : }
2254 : 69427 : else if (TREE_CODE (e) == COMPONENT_REF
2255 : 69427 : && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2256 : 37792 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2257 : 31635 : else if (is_overloaded_fn (e))
2258 : : {
2259 : 6 : if (complain & tf_error)
2260 : 6 : permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2261 : : "an expression of function type");
2262 : : else
2263 : 0 : return error_mark_node;
2264 : 6 : if (TREE_CODE (e) == FUNCTION_DECL)
2265 : 3 : t = size_int (DECL_ALIGN_UNIT (e));
2266 : : else
2267 : 3 : t = size_one_node;
2268 : : }
2269 : 31629 : else if (type_unknown_p (e))
2270 : : {
2271 : 0 : if (complain & tf_error)
2272 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2273 : : else
2274 : 0 : return error_mark_node;
2275 : 0 : t = size_one_node;
2276 : : }
2277 : : else
2278 : 31629 : return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2279 : : ALIGNOF_EXPR, std_alignof,
2280 : 31629 : complain & tf_error);
2281 : :
2282 : 50074 : return fold_convert_loc (loc, size_type_node, t);
2283 : : }
2284 : :
2285 : : /* Process a sizeof or alignof expression E with code OP where the operand
2286 : : is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2287 : :
2288 : : tree
2289 : 1494267 : cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2290 : : bool std_alignof, bool complain)
2291 : : {
2292 : 1494267 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2293 : 1494267 : if (op == SIZEOF_EXPR)
2294 : 2106037 : return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2295 : : else
2296 : 101716 : return cxx_alignof_expr (loc, e, std_alignof,
2297 : 101689 : complain? tf_warning_or_error : tf_none);
2298 : : }
2299 : :
2300 : : /* Build a representation of an expression 'alignas(E).' Return the
2301 : : folded integer value of E if it is an integral constant expression
2302 : : that resolves to a valid alignment. If E depends on a template
2303 : : parameter, return a syntactic representation tree of kind
2304 : : ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2305 : : expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2306 : :
2307 : : tree
2308 : 197277 : cxx_alignas_expr (tree e)
2309 : : {
2310 : 197277 : if (e == NULL_TREE || e == error_mark_node
2311 : 394554 : || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2312 : 0 : return e;
2313 : :
2314 : 197277 : if (TYPE_P (e))
2315 : : /* [dcl.align]/3:
2316 : :
2317 : : When the alignment-specifier is of the form
2318 : : alignas(type-id), it shall have the same effect as
2319 : : alignas(alignof(type-id)). */
2320 : :
2321 : 122541 : return cxx_sizeof_or_alignof_type (input_location,
2322 : : e, ALIGNOF_EXPR,
2323 : : /*std_alignof=*/true,
2324 : 122541 : /*complain=*/true);
2325 : :
2326 : : /* If we reach this point, it means the alignas expression if of
2327 : : the form "alignas(assignment-expression)", so we should follow
2328 : : what is stated by [dcl.align]/2. */
2329 : :
2330 : 74736 : if (value_dependent_expression_p (e))
2331 : : /* Leave value-dependent expression alone for now. */
2332 : : return e;
2333 : :
2334 : 16890 : e = instantiate_non_dependent_expr (e);
2335 : 16890 : e = mark_rvalue_use (e);
2336 : :
2337 : : /* [dcl.align]/2 says:
2338 : :
2339 : : the assignment-expression shall be an integral constant
2340 : : expression. */
2341 : :
2342 : 16890 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2343 : : {
2344 : 18 : error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2345 : 18 : return error_mark_node;
2346 : : }
2347 : :
2348 : 16872 : return cxx_constant_value (e);
2349 : : }
2350 : :
2351 : :
2352 : : /* EXPR is being used in a context that is not a function call.
2353 : : Enforce:
2354 : :
2355 : : [expr.ref]
2356 : :
2357 : : The expression can be used only as the left-hand operand of a
2358 : : member function call.
2359 : :
2360 : : [expr.mptr.operator]
2361 : :
2362 : : If the result of .* or ->* is a function, then that result can be
2363 : : used only as the operand for the function call operator ().
2364 : :
2365 : : by issuing an error message if appropriate. Returns true iff EXPR
2366 : : violates these rules. */
2367 : :
2368 : : bool
2369 : 1208872905 : invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2370 : : {
2371 : 1208872905 : if (expr == NULL_TREE)
2372 : : return false;
2373 : : /* Don't enforce this in MS mode. */
2374 : 1208871471 : if (flag_ms_extensions)
2375 : : return false;
2376 : 1208863410 : if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2377 : 99180334 : expr = get_first_fn (expr);
2378 : 1208863410 : if (TREE_TYPE (expr)
2379 : 1208863410 : && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2380 : : {
2381 : 91 : if (complain & tf_error)
2382 : : {
2383 : 87 : if (DECL_P (expr))
2384 : : {
2385 : 69 : auto_diagnostic_group d;
2386 : 69 : error_at (loc, "invalid use of non-static member function %qD",
2387 : : expr);
2388 : 69 : inform (DECL_SOURCE_LOCATION (expr), "declared here");
2389 : 69 : }
2390 : : else
2391 : 18 : error_at (loc, "invalid use of non-static member function of "
2392 : 18 : "type %qT", TREE_TYPE (expr));
2393 : : }
2394 : 91 : return true;
2395 : : }
2396 : : return false;
2397 : : }
2398 : :
2399 : : /* If EXP is a reference to a bit-field, and the type of EXP does not
2400 : : match the declared type of the bit-field, return the declared type
2401 : : of the bit-field. Otherwise, return NULL_TREE. */
2402 : :
2403 : : tree
2404 : 2801306587 : is_bitfield_expr_with_lowered_type (const_tree exp)
2405 : : {
2406 : 3870286715 : switch (TREE_CODE (exp))
2407 : : {
2408 : 5526061 : case COND_EXPR:
2409 : 11052122 : if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2410 : 5526035 : ? TREE_OPERAND (exp, 1)
2411 : 26 : : TREE_OPERAND (exp, 0)))
2412 : : return NULL_TREE;
2413 : 135 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2414 : :
2415 : 882850 : case COMPOUND_EXPR:
2416 : 882850 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2417 : :
2418 : 349235438 : case MODIFY_EXPR:
2419 : 349235438 : case SAVE_EXPR:
2420 : 349235438 : case UNARY_PLUS_EXPR:
2421 : 349235438 : case PREDECREMENT_EXPR:
2422 : 349235438 : case PREINCREMENT_EXPR:
2423 : 349235438 : case POSTDECREMENT_EXPR:
2424 : 349235438 : case POSTINCREMENT_EXPR:
2425 : 349235438 : case NEGATE_EXPR:
2426 : 349235438 : case NON_LVALUE_EXPR:
2427 : 349235438 : case BIT_NOT_EXPR:
2428 : 349235438 : case CLEANUP_POINT_EXPR:
2429 : 349235438 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2430 : :
2431 : 239887421 : case COMPONENT_REF:
2432 : 239887421 : {
2433 : 239887421 : tree field;
2434 : :
2435 : 239887421 : field = TREE_OPERAND (exp, 1);
2436 : 239887421 : if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2437 : : return NULL_TREE;
2438 : 39794747 : if (same_type_ignoring_top_level_qualifiers_p
2439 : 39794747 : (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2440 : : return NULL_TREE;
2441 : 39643838 : return DECL_BIT_FIELD_TYPE (field);
2442 : : }
2443 : :
2444 : 526740466 : case VAR_DECL:
2445 : 526740466 : if (DECL_HAS_VALUE_EXPR_P (exp))
2446 : 1913415 : return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2447 : 1913415 : (CONST_CAST_TREE (exp)));
2448 : : return NULL_TREE;
2449 : :
2450 : 724158140 : case VIEW_CONVERT_EXPR:
2451 : 724158140 : if (location_wrapper_p (exp))
2452 : 716948290 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2453 : : else
2454 : : return NULL_TREE;
2455 : :
2456 : : default:
2457 : : return NULL_TREE;
2458 : : }
2459 : : }
2460 : :
2461 : : /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2462 : : bitfield with a lowered type, the type of EXP is returned, rather
2463 : : than NULL_TREE. */
2464 : :
2465 : : tree
2466 : 1305484042 : unlowered_expr_type (const_tree exp)
2467 : : {
2468 : 1305484042 : tree type;
2469 : 1305484042 : tree etype = TREE_TYPE (exp);
2470 : :
2471 : 1305484042 : type = is_bitfield_expr_with_lowered_type (exp);
2472 : 1305484042 : if (type)
2473 : 33554063 : type = cp_build_qualified_type (type, cp_type_quals (etype));
2474 : : else
2475 : : type = etype;
2476 : :
2477 : 1305484042 : return type;
2478 : : }
2479 : :
2480 : : /* Perform the conversions in [expr] that apply when an lvalue appears
2481 : : in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2482 : : function-to-pointer conversions. In addition, bitfield references are
2483 : : converted to their declared types. Note that this function does not perform
2484 : : the lvalue-to-rvalue conversion for class types. If you need that conversion
2485 : : for class types, then you probably need to use force_rvalue.
2486 : :
2487 : : Although the returned value is being used as an rvalue, this
2488 : : function does not wrap the returned expression in a
2489 : : NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2490 : : that the return value is no longer an lvalue. */
2491 : :
2492 : : tree
2493 : 965947369 : decay_conversion (tree exp,
2494 : : tsubst_flags_t complain,
2495 : : bool reject_builtin /* = true */)
2496 : : {
2497 : 965947369 : tree type;
2498 : 965947369 : enum tree_code code;
2499 : 965947369 : location_t loc = cp_expr_loc_or_input_loc (exp);
2500 : :
2501 : 965947369 : type = TREE_TYPE (exp);
2502 : 965947369 : if (type == error_mark_node)
2503 : : return error_mark_node;
2504 : :
2505 : 965947159 : exp = resolve_nondeduced_context_or_error (exp, complain);
2506 : :
2507 : 965947159 : code = TREE_CODE (type);
2508 : :
2509 : 965947159 : if (error_operand_p (exp))
2510 : 90 : return error_mark_node;
2511 : :
2512 : 965947069 : if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2513 : : {
2514 : 1506225 : mark_rvalue_use (exp, loc, reject_builtin);
2515 : 1506225 : return nullptr_node;
2516 : : }
2517 : :
2518 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2519 : : Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2520 : 964440844 : if (code == VOID_TYPE)
2521 : : {
2522 : 5785 : if (complain & tf_error)
2523 : 3 : error_at (loc, "void value not ignored as it ought to be");
2524 : 5785 : return error_mark_node;
2525 : : }
2526 : 964435059 : if (invalid_nonstatic_memfn_p (loc, exp, complain))
2527 : 6 : return error_mark_node;
2528 : 964435053 : if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2529 : : {
2530 : 100575838 : exp = mark_lvalue_use (exp);
2531 : 100575838 : if (reject_builtin && reject_gcc_builtin (exp, loc))
2532 : 110 : return error_mark_node;
2533 : 100575728 : return cp_build_addr_expr (exp, complain);
2534 : : }
2535 : 863859215 : if (code == ARRAY_TYPE)
2536 : : {
2537 : 5375329 : tree adr;
2538 : 5375329 : tree ptrtype;
2539 : :
2540 : 5375329 : exp = mark_lvalue_use (exp);
2541 : :
2542 : 5375329 : if (INDIRECT_REF_P (exp))
2543 : 99087 : return build_nop (build_pointer_type (TREE_TYPE (type)),
2544 : 198174 : TREE_OPERAND (exp, 0));
2545 : :
2546 : 5276242 : if (TREE_CODE (exp) == COMPOUND_EXPR)
2547 : : {
2548 : 3 : tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2549 : 3 : if (op1 == error_mark_node)
2550 : : return error_mark_node;
2551 : 3 : return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2552 : 6 : TREE_OPERAND (exp, 0), op1);
2553 : : }
2554 : :
2555 : 5276239 : if (!obvalue_p (exp)
2556 : 5276239 : && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2557 : : {
2558 : 0 : if (complain & tf_error)
2559 : 0 : error_at (loc, "invalid use of non-lvalue array");
2560 : 0 : return error_mark_node;
2561 : : }
2562 : :
2563 : 5276239 : ptrtype = build_pointer_type (TREE_TYPE (type));
2564 : :
2565 : 5276239 : if (VAR_P (exp))
2566 : : {
2567 : 527328 : if (!cxx_mark_addressable (exp))
2568 : 0 : return error_mark_node;
2569 : 527328 : adr = build_nop (ptrtype, build_address (exp));
2570 : 527328 : return adr;
2571 : : }
2572 : : /* This way is better for a COMPONENT_REF since it can
2573 : : simplify the offset for a component. */
2574 : 4748911 : adr = cp_build_addr_expr (exp, complain);
2575 : 4748911 : return cp_convert (ptrtype, adr, complain);
2576 : : }
2577 : :
2578 : : /* Otherwise, it's the lvalue-to-rvalue conversion. */
2579 : 858483886 : exp = mark_rvalue_use (exp, loc, reject_builtin);
2580 : :
2581 : : /* If a bitfield is used in a context where integral promotion
2582 : : applies, then the caller is expected to have used
2583 : : default_conversion. That function promotes bitfields correctly
2584 : : before calling this function. At this point, if we have a
2585 : : bitfield referenced, we may assume that is not subject to
2586 : : promotion, and that, therefore, the type of the resulting rvalue
2587 : : is the declared type of the bitfield. */
2588 : 858483886 : exp = convert_bitfield_to_declared_type (exp);
2589 : :
2590 : : /* We do not call rvalue() here because we do not want to wrap EXP
2591 : : in a NON_LVALUE_EXPR. */
2592 : :
2593 : : /* [basic.lval]
2594 : :
2595 : : Non-class rvalues always have cv-unqualified types. */
2596 : 858483886 : type = TREE_TYPE (exp);
2597 : 858483886 : if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2598 : 263244795 : exp = build_nop (cv_unqualified (type), exp);
2599 : :
2600 : 858483886 : if (!complete_type_or_maybe_complain (type, exp, complain))
2601 : 42 : return error_mark_node;
2602 : :
2603 : : return exp;
2604 : : }
2605 : :
2606 : : /* Perform preparatory conversions, as part of the "usual arithmetic
2607 : : conversions". In particular, as per [expr]:
2608 : :
2609 : : Whenever an lvalue expression appears as an operand of an
2610 : : operator that expects the rvalue for that operand, the
2611 : : lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2612 : : standard conversions are applied to convert the expression to an
2613 : : rvalue.
2614 : :
2615 : : In addition, we perform integral promotions here, as those are
2616 : : applied to both operands to a binary operator before determining
2617 : : what additional conversions should apply. */
2618 : :
2619 : : static tree
2620 : 268023398 : cp_default_conversion (tree exp, tsubst_flags_t complain)
2621 : : {
2622 : : /* Check for target-specific promotions. */
2623 : 268023398 : tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2624 : 268023398 : if (promoted_type)
2625 : 0 : exp = cp_convert (promoted_type, exp, complain);
2626 : : /* Perform the integral promotions first so that bitfield
2627 : : expressions (which may promote to "int", even if the bitfield is
2628 : : declared "unsigned") are promoted correctly. */
2629 : 268023398 : else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2630 : 178421956 : exp = cp_perform_integral_promotions (exp, complain);
2631 : : /* Perform the other conversions. */
2632 : 268023398 : exp = decay_conversion (exp, complain);
2633 : :
2634 : 268023398 : return exp;
2635 : : }
2636 : :
2637 : : /* C version. */
2638 : :
2639 : : tree
2640 : 40445575 : default_conversion (tree exp)
2641 : : {
2642 : 40445575 : return cp_default_conversion (exp, tf_warning_or_error);
2643 : : }
2644 : :
2645 : : /* EXPR is an expression with an integral or enumeration type.
2646 : : Perform the integral promotions in [conv.prom], and return the
2647 : : converted value. */
2648 : :
2649 : : tree
2650 : 184638785 : cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2651 : : {
2652 : 184638785 : tree type;
2653 : 184638785 : tree promoted_type;
2654 : :
2655 : 184638785 : expr = mark_rvalue_use (expr);
2656 : 184638785 : if (error_operand_p (expr))
2657 : 6 : return error_mark_node;
2658 : :
2659 : 184638779 : type = TREE_TYPE (expr);
2660 : :
2661 : : /* [conv.prom]
2662 : :
2663 : : A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2664 : : of type int if int can represent all the values of the bit-field;
2665 : : otherwise, it can be converted to unsigned int if unsigned int can
2666 : : represent all the values of the bit-field. If the bit-field is larger yet,
2667 : : no integral promotion applies to it. If the bit-field has an enumerated
2668 : : type, it is treated as any other value of that type for promotion
2669 : : purposes. */
2670 : 184638779 : tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2671 : 184638779 : if (bitfield_type
2672 : 184638779 : && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2673 : 317338 : || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2674 : : type = bitfield_type;
2675 : :
2676 : 184638779 : gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2677 : : /* Scoped enums don't promote. */
2678 : 184638779 : if (SCOPED_ENUM_P (type))
2679 : : return expr;
2680 : 183553420 : promoted_type = type_promotes_to (type);
2681 : 183553420 : if (type != promoted_type)
2682 : 50227307 : expr = cp_convert (promoted_type, expr, complain);
2683 : 133326113 : else if (bitfield_type && bitfield_type != type)
2684 : : /* Prevent decay_conversion from converting to bitfield_type. */
2685 : 159 : expr = build_nop (type, expr);
2686 : : return expr;
2687 : : }
2688 : :
2689 : : /* C version. */
2690 : :
2691 : : tree
2692 : 2030852 : perform_integral_promotions (tree expr)
2693 : : {
2694 : 2030852 : return cp_perform_integral_promotions (expr, tf_warning_or_error);
2695 : : }
2696 : :
2697 : : /* Returns nonzero iff exp is a STRING_CST or the result of applying
2698 : : decay_conversion to one. */
2699 : :
2700 : : int
2701 : 3196012 : string_conv_p (const_tree totype, const_tree exp, int warn)
2702 : : {
2703 : 3196012 : tree t;
2704 : :
2705 : 3196012 : if (!TYPE_PTR_P (totype))
2706 : : return 0;
2707 : :
2708 : 3195961 : t = TREE_TYPE (totype);
2709 : 3195961 : if (!same_type_p (t, char_type_node)
2710 : 3059757 : && !same_type_p (t, char8_type_node)
2711 : 3043005 : && !same_type_p (t, char16_type_node)
2712 : 2992565 : && !same_type_p (t, char32_type_node)
2713 : 6138087 : && !same_type_p (t, wchar_type_node))
2714 : : return 0;
2715 : :
2716 : 283726 : location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2717 : :
2718 : 283726 : STRIP_ANY_LOCATION_WRAPPER (exp);
2719 : :
2720 : 283726 : if (TREE_CODE (exp) == STRING_CST)
2721 : : {
2722 : : /* Make sure that we don't try to convert between char and wide chars. */
2723 : 108 : if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2724 : : return 0;
2725 : : }
2726 : : else
2727 : : {
2728 : : /* Is this a string constant which has decayed to 'const char *'? */
2729 : 283618 : t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2730 : 283618 : if (!same_type_p (TREE_TYPE (exp), t))
2731 : : return 0;
2732 : 14034 : STRIP_NOPS (exp);
2733 : 14034 : if (TREE_CODE (exp) != ADDR_EXPR
2734 : 14034 : || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2735 : : return 0;
2736 : : }
2737 : 306 : if (warn)
2738 : : {
2739 : 102 : if (cxx_dialect >= cxx11)
2740 : 87 : pedwarn (loc, OPT_Wwrite_strings,
2741 : : "ISO C++ forbids converting a string constant to %qT",
2742 : : totype);
2743 : : else
2744 : 15 : warning_at (loc, OPT_Wwrite_strings,
2745 : : "deprecated conversion from string constant to %qT",
2746 : : totype);
2747 : : }
2748 : :
2749 : : return 1;
2750 : : }
2751 : :
2752 : : /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2753 : : can, for example, use as an lvalue. This code used to be in
2754 : : unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2755 : : expressions, where we're dealing with aggregates. But now it's again only
2756 : : called from unary_complex_lvalue. The case (in particular) that led to
2757 : : this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2758 : : get it there. */
2759 : :
2760 : : static tree
2761 : 101288 : rationalize_conditional_expr (enum tree_code code, tree t,
2762 : : tsubst_flags_t complain)
2763 : : {
2764 : 101288 : location_t loc = cp_expr_loc_or_input_loc (t);
2765 : :
2766 : : /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2767 : : the first operand is always the one to be used if both operands
2768 : : are equal, so we know what conditional expression this used to be. */
2769 : 101288 : if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2770 : : {
2771 : 0 : tree op0 = TREE_OPERAND (t, 0);
2772 : 0 : tree op1 = TREE_OPERAND (t, 1);
2773 : :
2774 : : /* The following code is incorrect if either operand side-effects. */
2775 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (op0)
2776 : : && !TREE_SIDE_EFFECTS (op1));
2777 : 0 : return
2778 : 0 : build_conditional_expr (loc,
2779 : 0 : build_x_binary_op (loc,
2780 : 0 : (TREE_CODE (t) == MIN_EXPR
2781 : : ? LE_EXPR : GE_EXPR),
2782 : 0 : op0, TREE_CODE (op0),
2783 : 0 : op1, TREE_CODE (op1),
2784 : : NULL_TREE,
2785 : : /*overload=*/NULL,
2786 : : complain),
2787 : : cp_build_unary_op (code, op0, false, complain),
2788 : : cp_build_unary_op (code, op1, false, complain),
2789 : : complain);
2790 : : }
2791 : :
2792 : 101288 : tree op1 = TREE_OPERAND (t, 1);
2793 : 101288 : if (TREE_CODE (op1) != THROW_EXPR)
2794 : 101285 : op1 = cp_build_unary_op (code, op1, false, complain);
2795 : 101288 : tree op2 = TREE_OPERAND (t, 2);
2796 : 101288 : if (TREE_CODE (op2) != THROW_EXPR)
2797 : 101282 : op2 = cp_build_unary_op (code, op2, false, complain);
2798 : :
2799 : 101288 : return
2800 : 101288 : build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2801 : : }
2802 : :
2803 : : /* Given the TYPE of an anonymous union field inside T, return the
2804 : : FIELD_DECL for the field. If not found return NULL_TREE. Because
2805 : : anonymous unions can nest, we must also search all anonymous unions
2806 : : that are directly reachable. */
2807 : :
2808 : : tree
2809 : 1134114 : lookup_anon_field (tree, tree type)
2810 : : {
2811 : 1134114 : tree field;
2812 : :
2813 : 1134114 : type = TYPE_MAIN_VARIANT (type);
2814 : 1134114 : field = ANON_AGGR_TYPE_FIELD (type);
2815 : 1134114 : gcc_assert (field);
2816 : 1134114 : return field;
2817 : : }
2818 : :
2819 : : /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2820 : : expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2821 : : non-NULL, it indicates the path to the base used to name MEMBER.
2822 : : If PRESERVE_REFERENCE is true, the expression returned will have
2823 : : REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2824 : : returned will have the type referred to by the reference.
2825 : :
2826 : : This function does not perform access control; that is either done
2827 : : earlier by the parser when the name of MEMBER is resolved to MEMBER
2828 : : itself, or later when overload resolution selects one of the
2829 : : functions indicated by MEMBER. */
2830 : :
2831 : : tree
2832 : 105704931 : build_class_member_access_expr (cp_expr object, tree member,
2833 : : tree access_path, bool preserve_reference,
2834 : : tsubst_flags_t complain)
2835 : : {
2836 : 105704931 : tree object_type;
2837 : 105704931 : tree member_scope;
2838 : 105704931 : tree result = NULL_TREE;
2839 : 105704931 : tree using_decl = NULL_TREE;
2840 : :
2841 : 105704931 : if (error_operand_p (object) || error_operand_p (member))
2842 : 48 : return error_mark_node;
2843 : :
2844 : 105704883 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2845 : :
2846 : : /* [expr.ref]
2847 : :
2848 : : The type of the first expression shall be "class object" (of a
2849 : : complete type). */
2850 : 105704883 : object_type = TREE_TYPE (object);
2851 : 105704883 : if (!currently_open_class (object_type)
2852 : 105704883 : && !complete_type_or_maybe_complain (object_type, object, complain))
2853 : 0 : return error_mark_node;
2854 : 105704883 : if (!CLASS_TYPE_P (object_type))
2855 : : {
2856 : 0 : if (complain & tf_error)
2857 : : {
2858 : 0 : if (INDIRECT_TYPE_P (object_type)
2859 : 0 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
2860 : 0 : error ("request for member %qD in %qE, which is of pointer "
2861 : : "type %qT (maybe you meant to use %<->%> ?)",
2862 : : member, object.get_value (), object_type);
2863 : : else
2864 : 0 : error ("request for member %qD in %qE, which is of non-class "
2865 : : "type %qT", member, object.get_value (), object_type);
2866 : : }
2867 : 0 : return error_mark_node;
2868 : : }
2869 : :
2870 : : /* The standard does not seem to actually say that MEMBER must be a
2871 : : member of OBJECT_TYPE. However, that is clearly what is
2872 : : intended. */
2873 : 105704883 : if (DECL_P (member))
2874 : : {
2875 : 44729934 : member_scope = DECL_CLASS_CONTEXT (member);
2876 : 44729934 : if (!mark_used (member, complain) && !(complain & tf_error))
2877 : 0 : return error_mark_node;
2878 : :
2879 : 44729934 : if (TREE_UNAVAILABLE (member))
2880 : 30 : error_unavailable_use (member, NULL_TREE);
2881 : 44729904 : else if (TREE_DEPRECATED (member))
2882 : 30 : warn_deprecated_use (member, NULL_TREE);
2883 : : }
2884 : : else
2885 : 60974949 : member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2886 : : /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2887 : : presently be the anonymous union. Go outwards until we find a
2888 : : type related to OBJECT_TYPE. */
2889 : 106842835 : while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2890 : 107981195 : && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2891 : : object_type))
2892 : 1137952 : member_scope = TYPE_CONTEXT (member_scope);
2893 : 105704883 : if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2894 : : {
2895 : 3 : if (complain & tf_error)
2896 : : {
2897 : 3 : if (TREE_CODE (member) == FIELD_DECL)
2898 : 0 : error ("invalid use of non-static data member %qE", member);
2899 : : else
2900 : 3 : error ("%qD is not a member of %qT", member, object_type);
2901 : : }
2902 : 3 : return error_mark_node;
2903 : : }
2904 : :
2905 : : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2906 : : `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2907 : : in the front end; only _DECLs and _REFs are lvalues in the back end. */
2908 : 105704880 : if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2909 : : {
2910 : 50 : temp = cp_build_fold_indirect_ref (temp);
2911 : 50 : if (!lvalue_p (object) && lvalue_p (temp))
2912 : : /* Preserve rvalueness. */
2913 : 6 : temp = move (temp);
2914 : 50 : object = temp;
2915 : : }
2916 : :
2917 : : /* In [expr.ref], there is an explicit list of the valid choices for
2918 : : MEMBER. We check for each of those cases here. */
2919 : 105704880 : if (VAR_P (member))
2920 : : {
2921 : : /* A static data member. */
2922 : 95768 : result = member;
2923 : 95768 : mark_exp_read (object);
2924 : :
2925 : 95768 : if (tree wrap = maybe_get_tls_wrapper_call (result))
2926 : : /* Replace an evaluated use of the thread_local variable with
2927 : : a call to its wrapper. */
2928 : 102 : result = wrap;
2929 : :
2930 : : /* If OBJECT has side-effects, they are supposed to occur. */
2931 : 95768 : if (TREE_SIDE_EFFECTS (object))
2932 : 57 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2933 : : }
2934 : 105609112 : else if (TREE_CODE (member) == FIELD_DECL)
2935 : : {
2936 : : /* A non-static data member. */
2937 : 44633967 : bool null_object_p;
2938 : 44633967 : int type_quals;
2939 : 44633967 : tree member_type;
2940 : :
2941 : 44633967 : if (INDIRECT_REF_P (object))
2942 : 33898275 : null_object_p =
2943 : 33898275 : integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2944 : : else
2945 : : null_object_p = false;
2946 : :
2947 : : /* Convert OBJECT to the type of MEMBER. */
2948 : 44633967 : if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2949 : : TYPE_MAIN_VARIANT (member_scope)))
2950 : : {
2951 : 1863440 : tree binfo;
2952 : 1863440 : base_kind kind;
2953 : :
2954 : : /* We didn't complain above about a currently open class, but now we
2955 : : must: we don't know how to refer to a base member before layout is
2956 : : complete. But still don't complain in a template. */
2957 : 1863440 : if (!cp_unevaluated_operand
2958 : 1862925 : && !dependent_type_p (object_type)
2959 : 3420161 : && !complete_type_or_maybe_complain (object_type, object,
2960 : : complain))
2961 : 9 : return error_mark_node;
2962 : :
2963 : 2324397 : binfo = lookup_base (access_path ? access_path : object_type,
2964 : : member_scope, ba_unique, &kind, complain);
2965 : 1863437 : if (binfo == error_mark_node)
2966 : : return error_mark_node;
2967 : :
2968 : : /* It is invalid to try to get to a virtual base of a
2969 : : NULL object. The most common cause is invalid use of
2970 : : offsetof macro. */
2971 : 1863437 : if (null_object_p && kind == bk_via_virtual)
2972 : : {
2973 : 6 : if (complain & tf_error)
2974 : : {
2975 : 6 : error ("invalid access to non-static data member %qD in "
2976 : : "virtual base of NULL object", member);
2977 : : }
2978 : 6 : return error_mark_node;
2979 : : }
2980 : :
2981 : : /* Convert to the base. */
2982 : 1863431 : object = build_base_path (PLUS_EXPR, object, binfo,
2983 : : /*nonnull=*/1, complain);
2984 : : /* If we found the base successfully then we should be able
2985 : : to convert to it successfully. */
2986 : 1863431 : gcc_assert (object != error_mark_node || seen_error ());
2987 : : }
2988 : :
2989 : : /* If MEMBER is from an anonymous aggregate, we have converted
2990 : : OBJECT so that it refers to the class containing the
2991 : : anonymous union. Generate a reference to the anonymous union
2992 : : itself, and recur to find MEMBER. */
2993 : 89267916 : if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2994 : : /* When this code is called from build_field_call, the
2995 : : object already has the type of the anonymous union.
2996 : : That is because the COMPONENT_REF was already
2997 : : constructed, and was then disassembled before calling
2998 : : build_field_call. After the function-call code is
2999 : : cleaned up, this waste can be eliminated. */
3000 : 45768348 : && (!same_type_ignoring_top_level_qualifiers_p
3001 : 1134390 : (TREE_TYPE (object), DECL_CONTEXT (member))))
3002 : : {
3003 : 1133988 : tree anonymous_union;
3004 : :
3005 : 1133988 : anonymous_union = lookup_anon_field (TREE_TYPE (object),
3006 : 1133988 : DECL_CONTEXT (member));
3007 : 1133988 : object = build_class_member_access_expr (object,
3008 : : anonymous_union,
3009 : : /*access_path=*/NULL_TREE,
3010 : : preserve_reference,
3011 : : complain);
3012 : : }
3013 : :
3014 : : /* Compute the type of the field, as described in [expr.ref]. */
3015 : 44633958 : type_quals = TYPE_UNQUALIFIED;
3016 : 44633958 : member_type = TREE_TYPE (member);
3017 : 44633958 : if (!TYPE_REF_P (member_type))
3018 : : {
3019 : 43703539 : type_quals = (cp_type_quals (member_type)
3020 : 43703539 : | cp_type_quals (object_type));
3021 : :
3022 : : /* A field is const (volatile) if the enclosing object, or the
3023 : : field itself, is const (volatile). But, a mutable field is
3024 : : not const, even within a const object. */
3025 : 43703539 : if (DECL_MUTABLE_P (member))
3026 : 336917 : type_quals &= ~TYPE_QUAL_CONST;
3027 : 43703539 : member_type = cp_build_qualified_type (member_type, type_quals);
3028 : : }
3029 : :
3030 : 44633958 : result = build3_loc (input_location, COMPONENT_REF, member_type,
3031 : : object, member, NULL_TREE);
3032 : :
3033 : : /* Mark the expression const or volatile, as appropriate. Even
3034 : : though we've dealt with the type above, we still have to mark the
3035 : : expression itself. */
3036 : 44633958 : if (type_quals & TYPE_QUAL_CONST)
3037 : 14211780 : TREE_READONLY (result) = 1;
3038 : 44633958 : if (type_quals & TYPE_QUAL_VOLATILE)
3039 : 186631 : TREE_THIS_VOLATILE (result) = 1;
3040 : : }
3041 : 60975145 : else if (BASELINK_P (member))
3042 : : {
3043 : : /* The member is a (possibly overloaded) member function. */
3044 : 60974946 : tree functions;
3045 : 60974946 : tree type;
3046 : :
3047 : : /* If the MEMBER is exactly one static member function, then we
3048 : : know the type of the expression. Otherwise, we must wait
3049 : : until overload resolution has been performed. */
3050 : 60974946 : functions = BASELINK_FUNCTIONS (member);
3051 : 60974946 : if (TREE_CODE (functions) == OVERLOAD && OVL_SINGLE_P (functions))
3052 : 60974946 : functions = OVL_FIRST (functions);
3053 : 60974946 : if (TREE_CODE (functions) == FUNCTION_DECL
3054 : 60974946 : && DECL_STATIC_FUNCTION_P (functions))
3055 : 476891 : type = TREE_TYPE (functions);
3056 : : else
3057 : 60498055 : type = unknown_type_node;
3058 : : /* Note that we do not convert OBJECT to the BASELINK_BINFO
3059 : : base. That will happen when the function is called. */
3060 : 60974946 : result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3061 : : NULL_TREE);
3062 : : }
3063 : 199 : else if (TREE_CODE (member) == CONST_DECL)
3064 : : {
3065 : : /* The member is an enumerator. */
3066 : 187 : result = member;
3067 : : /* If OBJECT has side-effects, they are supposed to occur. */
3068 : 187 : if (TREE_SIDE_EFFECTS (object))
3069 : 6 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3070 : : object, result);
3071 : : }
3072 : 12 : else if ((using_decl = strip_using_decl (member)) != member)
3073 : 0 : result = build_class_member_access_expr (object,
3074 : : using_decl,
3075 : : access_path, preserve_reference,
3076 : : complain);
3077 : : else
3078 : : {
3079 : 12 : if (complain & tf_error)
3080 : 12 : error ("invalid use of %qD", member);
3081 : 12 : return error_mark_node;
3082 : : }
3083 : :
3084 : 105704859 : if (!preserve_reference)
3085 : : /* [expr.ref]
3086 : :
3087 : : If E2 is declared to have type "reference to T", then ... the
3088 : : type of E1.E2 is T. */
3089 : 100082385 : result = convert_from_reference (result);
3090 : :
3091 : : return result;
3092 : : }
3093 : :
3094 : : /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3095 : : SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3096 : :
3097 : : tree
3098 : 158939 : lookup_destructor (tree object, tree scope, tree dtor_name,
3099 : : tsubst_flags_t complain)
3100 : : {
3101 : 158939 : tree object_type = TREE_TYPE (object);
3102 : 158939 : tree dtor_type = TREE_OPERAND (dtor_name, 0);
3103 : 158939 : tree expr;
3104 : :
3105 : : /* We've already complained about this destructor. */
3106 : 158939 : if (dtor_type == error_mark_node)
3107 : : return error_mark_node;
3108 : :
3109 : 158933 : if (scope && !check_dtor_name (scope, dtor_type))
3110 : : {
3111 : 3 : if (complain & tf_error)
3112 : 3 : error ("qualified type %qT does not match destructor name ~%qT",
3113 : : scope, dtor_type);
3114 : 3 : return error_mark_node;
3115 : : }
3116 : 158930 : if (is_auto (dtor_type))
3117 : : dtor_type = object_type;
3118 : 158927 : else if (identifier_p (dtor_type))
3119 : : {
3120 : : /* In a template, names we can't find a match for are still accepted
3121 : : destructor names, and we check them here. */
3122 : 23 : if (check_dtor_name (object_type, dtor_type))
3123 : : dtor_type = object_type;
3124 : : else
3125 : : {
3126 : 3 : if (complain & tf_error)
3127 : 3 : error ("object type %qT does not match destructor name ~%qT",
3128 : : object_type, dtor_type);
3129 : 3 : return error_mark_node;
3130 : : }
3131 : :
3132 : : }
3133 : 158904 : else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3134 : : {
3135 : 6 : if (complain & tf_error)
3136 : 6 : error ("the type being destroyed is %qT, but the destructor "
3137 : 6 : "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3138 : 6 : return error_mark_node;
3139 : : }
3140 : 158921 : expr = lookup_member (dtor_type, complete_dtor_identifier,
3141 : : /*protect=*/1, /*want_type=*/false,
3142 : : tf_warning_or_error);
3143 : 158921 : if (!expr)
3144 : : {
3145 : 6 : if (complain & tf_error)
3146 : 6 : cxx_incomplete_type_error (dtor_name, dtor_type);
3147 : 6 : return error_mark_node;
3148 : : }
3149 : 158915 : expr = (adjust_result_of_qualified_name_lookup
3150 : 158915 : (expr, dtor_type, object_type));
3151 : 158915 : if (scope == NULL_TREE)
3152 : : /* We need to call adjust_result_of_qualified_name_lookup in case the
3153 : : destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3154 : : that we still get virtual function binding. */
3155 : 158772 : BASELINK_QUALIFIED_P (expr) = false;
3156 : : return expr;
3157 : : }
3158 : :
3159 : : /* An expression of the form "A::template B" has been resolved to
3160 : : DECL. Issue a diagnostic if B is not a template or template
3161 : : specialization. */
3162 : :
3163 : : void
3164 : 3152508 : check_template_keyword (tree decl)
3165 : : {
3166 : : /* The standard says:
3167 : :
3168 : : [temp.names]
3169 : :
3170 : : If a name prefixed by the keyword template is not a member
3171 : : template, the program is ill-formed.
3172 : :
3173 : : DR 228 removed the restriction that the template be a member
3174 : : template.
3175 : :
3176 : : DR 96, if accepted would add the further restriction that explicit
3177 : : template arguments must be provided if the template keyword is
3178 : : used, but, as of 2005-10-16, that DR is still in "drafting". If
3179 : : this DR is accepted, then the semantic checks here can be
3180 : : simplified, as the entity named must in fact be a template
3181 : : specialization, rather than, as at present, a set of overloaded
3182 : : functions containing at least one template function. */
3183 : 3152508 : if (TREE_CODE (decl) != TEMPLATE_DECL
3184 : 3152508 : && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3185 : : {
3186 : 2825541 : if (VAR_P (decl))
3187 : : {
3188 : 9781 : if (DECL_USE_TEMPLATE (decl)
3189 : 9781 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3190 : : ;
3191 : : else
3192 : 0 : permerror (input_location, "%qD is not a template", decl);
3193 : : }
3194 : 2815760 : else if (!is_overloaded_fn (decl))
3195 : 0 : permerror (input_location, "%qD is not a template", decl);
3196 : : else
3197 : : {
3198 : 2815760 : bool found = false;
3199 : :
3200 : 5631520 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3201 : 5631522 : !found && iter; ++iter)
3202 : : {
3203 : 2815762 : tree fn = *iter;
3204 : 2815762 : if (TREE_CODE (fn) == TEMPLATE_DECL
3205 : 2815366 : || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3206 : 2815776 : || (TREE_CODE (fn) == FUNCTION_DECL
3207 : 14 : && DECL_USE_TEMPLATE (fn)
3208 : 6 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3209 : : found = true;
3210 : : }
3211 : 2815760 : if (!found)
3212 : 12 : permerror (input_location, "%qD is not a template", decl);
3213 : : }
3214 : : }
3215 : 3152508 : }
3216 : :
3217 : : /* Record that an access failure occurred on BASETYPE_PATH attempting
3218 : : to access DECL, where DIAG_DECL should be used for diagnostics. */
3219 : :
3220 : : void
3221 : 406 : access_failure_info::record_access_failure (tree basetype_path,
3222 : : tree decl, tree diag_decl)
3223 : : {
3224 : 406 : m_was_inaccessible = true;
3225 : 406 : m_basetype_path = basetype_path;
3226 : 406 : m_decl = decl;
3227 : 406 : m_diag_decl = diag_decl;
3228 : 406 : }
3229 : :
3230 : : /* If an access failure was recorded, then attempt to locate an
3231 : : accessor function for the pertinent field.
3232 : : Otherwise, return NULL_TREE. */
3233 : :
3234 : : tree
3235 : 88085047 : access_failure_info::get_any_accessor (bool const_p) const
3236 : : {
3237 : 88085047 : if (!was_inaccessible_p ())
3238 : : return NULL_TREE;
3239 : :
3240 : 406 : tree accessor
3241 : 406 : = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3242 : 406 : if (!accessor)
3243 : : return NULL_TREE;
3244 : :
3245 : : /* The accessor must itself be accessible for it to be a reasonable
3246 : : suggestion. */
3247 : 161 : if (!accessible_p (m_basetype_path, accessor, true))
3248 : : return NULL_TREE;
3249 : :
3250 : : return accessor;
3251 : : }
3252 : :
3253 : : /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3254 : : replacing the primary location in RICHLOC with "accessor()". */
3255 : :
3256 : : void
3257 : 140 : access_failure_info::add_fixit_hint (rich_location *richloc,
3258 : : tree accessor_decl)
3259 : : {
3260 : 140 : pretty_printer pp;
3261 : 140 : pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3262 : 140 : pp_string (&pp, "()");
3263 : 140 : richloc->add_fixit_replace (pp_formatted_text (&pp));
3264 : 140 : }
3265 : :
3266 : : /* If an access failure was recorded, then attempt to locate an
3267 : : accessor function for the pertinent field, and if one is
3268 : : available, add a note and fix-it hint suggesting using it. */
3269 : :
3270 : : void
3271 : 88084977 : access_failure_info::maybe_suggest_accessor (bool const_p) const
3272 : : {
3273 : 88084977 : tree accessor = get_any_accessor (const_p);
3274 : 88084977 : if (accessor == NULL_TREE)
3275 : 88084879 : return;
3276 : 98 : rich_location richloc (line_table, input_location);
3277 : 98 : add_fixit_hint (&richloc, accessor);
3278 : 98 : inform (&richloc, "field %q#D can be accessed via %q#D",
3279 : 98 : m_diag_decl, accessor);
3280 : 98 : }
3281 : :
3282 : : /* Subroutine of finish_class_member_access_expr.
3283 : : Issue an error about NAME not being a member of ACCESS_PATH (or
3284 : : OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3285 : : names. */
3286 : :
3287 : : static void
3288 : 308 : complain_about_unrecognized_member (tree access_path, tree name,
3289 : : tree object_type)
3290 : : {
3291 : : /* Attempt to provide a hint about misspelled names. */
3292 : 308 : tree guessed_id = lookup_member_fuzzy (access_path, name,
3293 : : /*want_type=*/false);
3294 : 308 : if (guessed_id == NULL_TREE)
3295 : : {
3296 : : /* No hint. */
3297 : 201 : error ("%q#T has no member named %qE",
3298 : 201 : TREE_CODE (access_path) == TREE_BINFO
3299 : 6 : ? TREE_TYPE (access_path) : object_type, name);
3300 : 201 : return;
3301 : : }
3302 : :
3303 : 107 : location_t bogus_component_loc = input_location;
3304 : 107 : gcc_rich_location rich_loc (bogus_component_loc);
3305 : :
3306 : : /* Check that the guessed name is accessible along access_path. */
3307 : 107 : access_failure_info afi;
3308 : 107 : lookup_member (access_path, guessed_id, /*protect=*/1,
3309 : : /*want_type=*/false, /*complain=*/false,
3310 : : &afi);
3311 : 107 : if (afi.was_inaccessible_p ())
3312 : : {
3313 : 70 : tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3314 : 70 : if (accessor)
3315 : : {
3316 : : /* The guessed name isn't directly accessible, but can be accessed
3317 : : via an accessor member function. */
3318 : 42 : afi.add_fixit_hint (&rich_loc, accessor);
3319 : 42 : error_at (&rich_loc,
3320 : : "%q#T has no member named %qE;"
3321 : : " did you mean %q#D? (accessible via %q#D)",
3322 : 42 : TREE_CODE (access_path) == TREE_BINFO
3323 : 0 : ? TREE_TYPE (access_path) : object_type,
3324 : : name, afi.get_diag_decl (), accessor);
3325 : : }
3326 : : else
3327 : : {
3328 : : /* The guessed name isn't directly accessible, and no accessor
3329 : : member function could be found. */
3330 : 28 : auto_diagnostic_group d;
3331 : 28 : error_at (&rich_loc,
3332 : : "%q#T has no member named %qE;"
3333 : : " did you mean %q#D? (not accessible from this context)",
3334 : 28 : TREE_CODE (access_path) == TREE_BINFO
3335 : 0 : ? TREE_TYPE (access_path) : object_type,
3336 : : name, afi.get_diag_decl ());
3337 : 28 : complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3338 : : afi.get_diag_decl (), false, ak_none);
3339 : 28 : }
3340 : : }
3341 : : else
3342 : : {
3343 : : /* The guessed name is directly accessible; suggest it. */
3344 : 37 : rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3345 : : guessed_id);
3346 : 37 : error_at (&rich_loc,
3347 : : "%q#T has no member named %qE;"
3348 : : " did you mean %qE?",
3349 : 37 : TREE_CODE (access_path) == TREE_BINFO
3350 : 0 : ? TREE_TYPE (access_path) : object_type,
3351 : : name, guessed_id);
3352 : : }
3353 : 107 : }
3354 : :
3355 : : /* This function is called by the parser to process a class member
3356 : : access expression of the form OBJECT.NAME. NAME is a node used by
3357 : : the parser to represent a name; it is not yet a DECL. It may,
3358 : : however, be a BASELINK where the BASELINK_FUNCTIONS is a
3359 : : TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3360 : : there is no reason to do the lookup twice, so the parser keeps the
3361 : : BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3362 : : be a template via the use of the "A::template B" syntax. */
3363 : :
3364 : : tree
3365 : 172718547 : finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3366 : : tsubst_flags_t complain)
3367 : : {
3368 : 172718547 : tree expr;
3369 : 172718547 : tree object_type;
3370 : 172718547 : tree member;
3371 : 172718547 : tree access_path = NULL_TREE;
3372 : 172718547 : tree orig_object = object;
3373 : 172718547 : tree orig_name = name;
3374 : :
3375 : 172718547 : if (object == error_mark_node || name == error_mark_node)
3376 : : return error_mark_node;
3377 : :
3378 : : /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3379 : 172718117 : if (!objc_is_public (object, name))
3380 : 0 : return error_mark_node;
3381 : :
3382 : 172718117 : object_type = TREE_TYPE (object);
3383 : :
3384 : 172718117 : if (processing_template_decl)
3385 : : {
3386 : 144078081 : if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3387 : 144078081 : type_dependent_object_expression_p (object)
3388 : : /* If NAME is "f<args>", where either 'f' or 'args' is
3389 : : dependent, then the expression is dependent. */
3390 : 69608099 : || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3391 : 632678 : && dependent_template_id_p (TREE_OPERAND (name, 0),
3392 : 632678 : TREE_OPERAND (name, 1)))
3393 : : /* If NAME is "T::X" where "T" is dependent, then the
3394 : : expression is dependent. */
3395 : 69289183 : || (TREE_CODE (name) == SCOPE_REF
3396 : 111434 : && TYPE_P (TREE_OPERAND (name, 0))
3397 : 111434 : && dependent_scope_p (TREE_OPERAND (name, 0)))
3398 : : /* If NAME is operator T where "T" is dependent, we can't
3399 : : lookup until we instantiate the T. */
3400 : 213256044 : || (TREE_CODE (name) == IDENTIFIER_NODE
3401 : 68505006 : && IDENTIFIER_CONV_OP_P (name)
3402 : 41 : && dependent_type_p (TREE_TYPE (name))))
3403 : : {
3404 : 88802389 : dependent:
3405 : 88802389 : return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3406 : 88802389 : orig_object, orig_name, NULL_TREE);
3407 : : }
3408 : : }
3409 : 28640036 : else if (c_dialect_objc ()
3410 : 97817961 : && identifier_p (name)
3411 : 28640036 : && (expr = objc_maybe_build_component_ref (object, name)))
3412 : : return expr;
3413 : :
3414 : : /* [expr.ref]
3415 : :
3416 : : The type of the first expression shall be "class object" (of a
3417 : : complete type). */
3418 : 97817961 : if (!currently_open_class (object_type)
3419 : 97817961 : && !complete_type_or_maybe_complain (object_type, object, complain))
3420 : 86 : return error_mark_node;
3421 : 97817875 : if (!CLASS_TYPE_P (object_type))
3422 : : {
3423 : 98769 : if (complain & tf_error)
3424 : : {
3425 : 117 : if (INDIRECT_TYPE_P (object_type)
3426 : 117 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
3427 : 21 : error ("request for member %qD in %qE, which is of pointer "
3428 : : "type %qT (maybe you meant to use %<->%> ?)",
3429 : : name, object.get_value (), object_type);
3430 : : else
3431 : 96 : error ("request for member %qD in %qE, which is of non-class "
3432 : : "type %qT", name, object.get_value (), object_type);
3433 : : }
3434 : 98769 : return error_mark_node;
3435 : : }
3436 : :
3437 : 97719106 : if (BASELINK_P (name))
3438 : : /* A member function that has already been looked up. */
3439 : : member = name;
3440 : : else
3441 : : {
3442 : 88278518 : bool is_template_id = false;
3443 : 88278518 : tree template_args = NULL_TREE;
3444 : 88278518 : tree scope = NULL_TREE;
3445 : :
3446 : 88278518 : access_path = object_type;
3447 : :
3448 : 88278518 : if (TREE_CODE (name) == SCOPE_REF)
3449 : : {
3450 : : /* A qualified name. The qualifying class or namespace `S'
3451 : : has already been looked up; it is either a TYPE or a
3452 : : NAMESPACE_DECL. */
3453 : 1762 : scope = TREE_OPERAND (name, 0);
3454 : 1762 : name = TREE_OPERAND (name, 1);
3455 : :
3456 : : /* If SCOPE is a namespace, then the qualified name does not
3457 : : name a member of OBJECT_TYPE. */
3458 : 1762 : if (TREE_CODE (scope) == NAMESPACE_DECL)
3459 : : {
3460 : 0 : if (complain & tf_error)
3461 : 0 : error ("%<%D::%D%> is not a member of %qT",
3462 : : scope, name, object_type);
3463 : 0 : return error_mark_node;
3464 : : }
3465 : : }
3466 : :
3467 : 88278518 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3468 : : {
3469 : 476091 : is_template_id = true;
3470 : 476091 : template_args = TREE_OPERAND (name, 1);
3471 : 476091 : name = TREE_OPERAND (name, 0);
3472 : :
3473 : 907652 : if (!identifier_p (name))
3474 : 431579 : name = OVL_NAME (name);
3475 : : }
3476 : :
3477 : 88278518 : if (scope)
3478 : : {
3479 : 1762 : if (TREE_CODE (scope) == ENUMERAL_TYPE)
3480 : : {
3481 : 12 : gcc_assert (!is_template_id);
3482 : : /* Looking up a member enumerator (c++/56793). */
3483 : 24 : if (!TYPE_CLASS_SCOPE_P (scope)
3484 : 21 : || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3485 : : {
3486 : 3 : if (complain & tf_error)
3487 : 3 : error ("%<%D::%D%> is not a member of %qT",
3488 : : scope, name, object_type);
3489 : 3 : return error_mark_node;
3490 : : }
3491 : 9 : tree val = lookup_enumerator (scope, name);
3492 : 9 : if (!val)
3493 : : {
3494 : 6 : if (complain & tf_error)
3495 : 6 : error ("%qD is not a member of %qD",
3496 : : name, scope);
3497 : 6 : return error_mark_node;
3498 : : }
3499 : :
3500 : 3 : if (TREE_SIDE_EFFECTS (object))
3501 : 0 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3502 : 3 : return val;
3503 : : }
3504 : :
3505 : 1750 : gcc_assert (CLASS_TYPE_P (scope));
3506 : 1750 : gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3507 : :
3508 : 1750 : if (constructor_name_p (name, scope))
3509 : : {
3510 : 3 : if (complain & tf_error)
3511 : 3 : error ("cannot call constructor %<%T::%D%> directly",
3512 : : scope, name);
3513 : 3 : return error_mark_node;
3514 : : }
3515 : :
3516 : : /* NAME may refer to a static data member, in which case there is
3517 : : one copy of the data member that is shared by all the objects of
3518 : : the class. So NAME can be unambiguously referred to even if
3519 : : there are multiple indirect base classes containing NAME. */
3520 : 5241 : const base_access ba = [scope, name] ()
3521 : : {
3522 : 1747 : if (identifier_p (name))
3523 : : {
3524 : 1610 : tree m = lookup_member (scope, name, /*protect=*/0,
3525 : : /*want_type=*/false, tf_none);
3526 : 1610 : if (!m || shared_member_p (m))
3527 : 32 : return ba_any;
3528 : : }
3529 : : return ba_check;
3530 : 1747 : } ();
3531 : :
3532 : : /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3533 : 1747 : access_path = lookup_base (object_type, scope, ba, NULL, complain);
3534 : 1747 : if (access_path == error_mark_node)
3535 : : return error_mark_node;
3536 : 1735 : if (!access_path)
3537 : : {
3538 : 12 : if (any_dependent_bases_p (object_type))
3539 : 6 : goto dependent;
3540 : 6 : if (complain & tf_error)
3541 : 6 : error ("%qT is not a base of %qT", scope, object_type);
3542 : 6 : return error_mark_node;
3543 : : }
3544 : : }
3545 : :
3546 : 88278479 : if (TREE_CODE (name) == BIT_NOT_EXPR)
3547 : : {
3548 : 193502 : if (dependent_type_p (object_type))
3549 : : /* The destructor isn't declared yet. */
3550 : 34578 : goto dependent;
3551 : 158924 : member = lookup_destructor (object, scope, name, complain);
3552 : : }
3553 : : else
3554 : : {
3555 : : /* Look up the member. */
3556 : 88084977 : {
3557 : 88084977 : auto_diagnostic_group d;
3558 : 88084977 : access_failure_info afi;
3559 : 88084977 : if (processing_template_decl)
3560 : : /* Even though this class member access expression is at this
3561 : : point not dependent, the member itself may be dependent, and
3562 : : we must not potentially push a access check for a dependent
3563 : : member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3564 : : ahead of time here; we're going to redo this member lookup at
3565 : : instantiation time anyway. */
3566 : 68818920 : push_deferring_access_checks (dk_no_check);
3567 : 88084977 : member = lookup_member (access_path, name, /*protect=*/1,
3568 : : /*want_type=*/false, complain,
3569 : : &afi);
3570 : 88084977 : if (processing_template_decl)
3571 : 68818920 : pop_deferring_access_checks ();
3572 : 88084977 : afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3573 : 88084977 : }
3574 : 88084977 : if (member == NULL_TREE)
3575 : : {
3576 : 8417727 : if (dependentish_scope_p (object_type))
3577 : : /* Try again at instantiation time. */
3578 : 8398393 : goto dependent;
3579 : 19334 : if (complain & tf_error)
3580 : 308 : complain_about_unrecognized_member (access_path, name,
3581 : : object_type);
3582 : 19334 : return error_mark_node;
3583 : : }
3584 : 79667250 : if (member == error_mark_node)
3585 : : return error_mark_node;
3586 : 79667206 : if (DECL_P (member)
3587 : 79667206 : && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3588 : : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3589 : : wrong, so don't use it. */
3590 : 6 : goto dependent;
3591 : 79667200 : if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3592 : 5469250 : goto dependent;
3593 : : }
3594 : :
3595 : 74356874 : if (is_template_id)
3596 : : {
3597 : 476081 : tree templ = member;
3598 : :
3599 : 476081 : if (BASELINK_P (templ))
3600 : 476057 : member = lookup_template_function (templ, template_args);
3601 : 24 : else if (variable_template_p (templ))
3602 : 24 : member = (lookup_and_finish_template_variable
3603 : 24 : (templ, template_args, complain));
3604 : : else
3605 : : {
3606 : 0 : if (complain & tf_error)
3607 : 0 : error ("%qD is not a member template function", name);
3608 : 0 : return error_mark_node;
3609 : : }
3610 : : }
3611 : : }
3612 : :
3613 : 83797462 : if (TREE_UNAVAILABLE (member))
3614 : 30 : error_unavailable_use (member, NULL_TREE);
3615 : 83797432 : else if (TREE_DEPRECATED (member))
3616 : 30 : warn_deprecated_use (member, NULL_TREE);
3617 : :
3618 : 83797462 : if (template_p)
3619 : 9940 : check_template_keyword (member);
3620 : :
3621 : 83797462 : expr = build_class_member_access_expr (object, member, access_path,
3622 : : /*preserve_reference=*/false,
3623 : : complain);
3624 : 83797462 : if (processing_template_decl && expr != error_mark_node)
3625 : : {
3626 : 55275616 : if (BASELINK_P (member))
3627 : : {
3628 : 41226217 : if (TREE_CODE (orig_name) == SCOPE_REF)
3629 : 169 : BASELINK_QUALIFIED_P (member) = 1;
3630 : : orig_name = member;
3631 : : }
3632 : 55275616 : return build_min_non_dep (COMPONENT_REF, expr,
3633 : : orig_object, orig_name,
3634 : 55275616 : NULL_TREE);
3635 : : }
3636 : :
3637 : : return expr;
3638 : : }
3639 : :
3640 : : /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3641 : : type. */
3642 : :
3643 : : tree
3644 : 180750 : build_simple_component_ref (tree object, tree member)
3645 : : {
3646 : 180750 : tree type = cp_build_qualified_type (TREE_TYPE (member),
3647 : 180750 : cp_type_quals (TREE_TYPE (object)));
3648 : 180750 : return build3_loc (input_location,
3649 : : COMPONENT_REF, type,
3650 : 180750 : object, member, NULL_TREE);
3651 : : }
3652 : :
3653 : : /* Return an expression for the MEMBER_NAME field in the internal
3654 : : representation of PTRMEM, a pointer-to-member function. (Each
3655 : : pointer-to-member function type gets its own RECORD_TYPE so it is
3656 : : more convenient to access the fields by name than by FIELD_DECL.)
3657 : : This routine converts the NAME to a FIELD_DECL and then creates the
3658 : : node for the complete expression. */
3659 : :
3660 : : tree
3661 : 180714 : build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3662 : : {
3663 : 180714 : tree ptrmem_type;
3664 : 180714 : tree member;
3665 : :
3666 : 180714 : if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3667 : : {
3668 : 180 : for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3669 : 72 : if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3670 : 54 : return e.value;
3671 : 0 : gcc_unreachable ();
3672 : : }
3673 : :
3674 : : /* This code is a stripped down version of
3675 : : build_class_member_access_expr. It does not work to use that
3676 : : routine directly because it expects the object to be of class
3677 : : type. */
3678 : 180660 : ptrmem_type = TREE_TYPE (ptrmem);
3679 : 180660 : gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3680 : 270893 : for (member = TYPE_FIELDS (ptrmem_type); member;
3681 : 90233 : member = DECL_CHAIN (member))
3682 : 270893 : if (DECL_NAME (member) == member_name)
3683 : : break;
3684 : 180660 : return build_simple_component_ref (ptrmem, member);
3685 : : }
3686 : :
3687 : : /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3688 : : and for any other relevant operator. */
3689 : :
3690 : : static tree
3691 : 120006808 : op_unqualified_lookup (tree_code code, bool is_assign)
3692 : : {
3693 : 120006808 : tree lookups = NULL_TREE;
3694 : :
3695 : 120006808 : if (cxx_dialect >= cxx20 && !is_assign)
3696 : : {
3697 : 34972539 : if (code == NE_EXPR)
3698 : : {
3699 : : /* != can get rewritten in terms of ==. */
3700 : 2303116 : tree fnname = ovl_op_identifier (false, EQ_EXPR);
3701 : 2303116 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3702 : 2287567 : lookups = tree_cons (fnname, fns, lookups);
3703 : : }
3704 : 32669423 : else if (code == GT_EXPR || code == LE_EXPR
3705 : 32669423 : || code == LT_EXPR || code == GE_EXPR)
3706 : : {
3707 : : /* These can get rewritten in terms of <=>. */
3708 : 3068444 : tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3709 : 3068444 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3710 : 2892188 : lookups = tree_cons (fnname, fns, lookups);
3711 : : }
3712 : : }
3713 : :
3714 : 120006808 : tree fnname = ovl_op_identifier (is_assign, code);
3715 : 120006808 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3716 : 66943369 : lookups = tree_cons (fnname, fns, lookups);
3717 : :
3718 : 120006808 : if (lookups)
3719 : : return lookups;
3720 : : else
3721 : 53004330 : return build_tree_list (NULL_TREE, NULL_TREE);
3722 : : }
3723 : :
3724 : : /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3725 : : the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3726 : : name lookup for the given operator. */
3727 : :
3728 : : tree
3729 : 129502304 : build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3730 : : {
3731 : 129502304 : if (lookups)
3732 : : /* We're partially instantiating a dependent operator expression, and
3733 : : LOOKUPS is the result of phase 1 name lookup that we performed
3734 : : earlier at template definition time, so just reuse the corresponding
3735 : : DEPENDENT_OPERATOR_TYPE. */
3736 : 9495496 : return TREE_TYPE (lookups);
3737 : :
3738 : : /* Otherwise we're processing a dependent operator expression at template
3739 : : definition time, so perform phase 1 name lookup now. */
3740 : 120006808 : lookups = op_unqualified_lookup (code, is_assign);
3741 : :
3742 : 120006808 : tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3743 : 120006808 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3744 : 120006808 : TREE_TYPE (lookups) = type;
3745 : 120006808 : return type;
3746 : : }
3747 : :
3748 : : /* Given an expression PTR for a pointer, return an expression
3749 : : for the value pointed to.
3750 : : ERRORSTRING is the name of the operator to appear in error messages.
3751 : :
3752 : : This function may need to overload OPERATOR_FNNAME.
3753 : : Must also handle REFERENCE_TYPEs for C++. */
3754 : :
3755 : : tree
3756 : 40366366 : build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3757 : : tree lookups, tsubst_flags_t complain)
3758 : : {
3759 : 40366366 : tree orig_expr = expr;
3760 : 40366366 : tree rval;
3761 : 40366366 : tree overload = NULL_TREE;
3762 : :
3763 : 40366366 : if (processing_template_decl)
3764 : : {
3765 : : /* Retain the type if we know the operand is a pointer. */
3766 : 22996555 : if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3767 : : {
3768 : 12566836 : if (expr == current_class_ptr
3769 : 12566836 : || (TREE_CODE (expr) == NOP_EXPR
3770 : 7894121 : && TREE_OPERAND (expr, 0) == current_class_ptr
3771 : 7882839 : && (same_type_ignoring_top_level_qualifiers_p
3772 : 7882839 : (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3773 : 7882838 : return current_class_ref;
3774 : 4683998 : return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3775 : : }
3776 : 10429719 : if (type_dependent_expression_p (expr))
3777 : : {
3778 : 10283403 : expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3779 : 10283403 : TREE_TYPE (expr)
3780 : 10283403 : = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3781 : 10283403 : return expr;
3782 : : }
3783 : : }
3784 : :
3785 : 17516127 : rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3786 : : NULL_TREE, NULL_TREE, lookups,
3787 : : &overload, complain);
3788 : 17516127 : if (!rval)
3789 : 0 : rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3790 : :
3791 : 17516127 : if (processing_template_decl && rval != error_mark_node)
3792 : : {
3793 : 144483 : if (overload != NULL_TREE)
3794 : 144452 : return (build_min_non_dep_op_overload
3795 : 144452 : (INDIRECT_REF, rval, overload, orig_expr));
3796 : :
3797 : 31 : return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3798 : : }
3799 : : else
3800 : : return rval;
3801 : : }
3802 : :
3803 : : /* Like c-family strict_aliasing_warning, but don't warn for dependent
3804 : : types or expressions. */
3805 : :
3806 : : static bool
3807 : 534861 : cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3808 : : {
3809 : 534861 : if (processing_template_decl)
3810 : : {
3811 : 38899 : tree e = expr;
3812 : 38899 : STRIP_NOPS (e);
3813 : 38899 : if (dependent_type_p (type) || type_dependent_expression_p (e))
3814 : 6705 : return false;
3815 : : }
3816 : 528156 : return strict_aliasing_warning (loc, type, expr);
3817 : : }
3818 : :
3819 : : /* The implementation of the above, and of indirection implied by other
3820 : : constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3821 : :
3822 : : static tree
3823 : 302798672 : cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3824 : : tsubst_flags_t complain, bool do_fold)
3825 : : {
3826 : 302798672 : tree pointer, type;
3827 : :
3828 : : /* RO_NULL should only be used with the folding entry points below, not
3829 : : cp_build_indirect_ref. */
3830 : 302798672 : gcc_checking_assert (errorstring != RO_NULL || do_fold);
3831 : :
3832 : 302798672 : if (ptr == current_class_ptr
3833 : 302798672 : || (TREE_CODE (ptr) == NOP_EXPR
3834 : 30442355 : && TREE_OPERAND (ptr, 0) == current_class_ptr
3835 : 17233547 : && (same_type_ignoring_top_level_qualifiers_p
3836 : 17233547 : (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3837 : 18660101 : return current_class_ref;
3838 : :
3839 : 284138571 : pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3840 : 284138571 : ? ptr : decay_conversion (ptr, complain));
3841 : 284138571 : if (pointer == error_mark_node)
3842 : : return error_mark_node;
3843 : :
3844 : 284132725 : type = TREE_TYPE (pointer);
3845 : :
3846 : 284132725 : if (INDIRECT_TYPE_P (type))
3847 : : {
3848 : : /* [expr.unary.op]
3849 : :
3850 : : If the type of the expression is "pointer to T," the type
3851 : : of the result is "T." */
3852 : 284132392 : tree t = TREE_TYPE (type);
3853 : :
3854 : 269753726 : if ((CONVERT_EXPR_P (ptr)
3855 : 204045299 : || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3856 : 351754778 : && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3857 : : {
3858 : : /* If a warning is issued, mark it to avoid duplicates from
3859 : : the backend. This only needs to be done at
3860 : : warn_strict_aliasing > 2. */
3861 : 57650547 : if (warn_strict_aliasing > 2
3862 : 58106361 : && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3863 : 455814 : type, TREE_OPERAND (ptr, 0)))
3864 : 6 : suppress_warning (ptr, OPT_Wstrict_aliasing);
3865 : : }
3866 : :
3867 : 284132392 : if (VOID_TYPE_P (t))
3868 : : {
3869 : : /* A pointer to incomplete type (other than cv void) can be
3870 : : dereferenced [expr.unary.op]/1 */
3871 : 70 : if (complain & tf_error)
3872 : 19 : error_at (loc, "%qT is not a pointer-to-object type", type);
3873 : 70 : return error_mark_node;
3874 : : }
3875 : 275037045 : else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3876 : 289761483 : && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3877 : : /* The POINTER was something like `&x'. We simplify `*&x' to
3878 : : `x'. This can change the value category: '*&TARGET_EXPR'
3879 : : is an lvalue and folding it into 'TARGET_EXPR' turns it into
3880 : : a prvalue of class type. */
3881 : 5629132 : return TREE_OPERAND (pointer, 0);
3882 : : else
3883 : : {
3884 : 278503190 : tree ref = build1 (INDIRECT_REF, t, pointer);
3885 : :
3886 : : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3887 : : so that we get the proper error message if the result is used
3888 : : to assign to. Also, &* is supposed to be a no-op. */
3889 : 278503190 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3890 : 278503190 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3891 : 278503190 : TREE_SIDE_EFFECTS (ref)
3892 : 278503190 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3893 : 278503190 : return ref;
3894 : : }
3895 : : }
3896 : 333 : else if (!(complain & tf_error))
3897 : : /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3898 : : ;
3899 : : /* `pointer' won't be an error_mark_node if we were given a
3900 : : pointer to member, so it's cool to check for this here. */
3901 : 33 : else if (TYPE_PTRMEM_P (type))
3902 : 15 : switch (errorstring)
3903 : : {
3904 : 0 : case RO_ARRAY_INDEXING:
3905 : 0 : error_at (loc,
3906 : : "invalid use of array indexing on pointer to member");
3907 : 0 : break;
3908 : 12 : case RO_UNARY_STAR:
3909 : 12 : error_at (loc, "invalid use of unary %<*%> on pointer to member");
3910 : 12 : break;
3911 : 0 : case RO_IMPLICIT_CONVERSION:
3912 : 0 : error_at (loc, "invalid use of implicit conversion on pointer "
3913 : : "to member");
3914 : 0 : break;
3915 : 3 : case RO_ARROW_STAR:
3916 : 3 : error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3917 : : "class, but is a pointer to member of type %qT", type);
3918 : 3 : break;
3919 : 0 : default:
3920 : 0 : gcc_unreachable ();
3921 : : }
3922 : 18 : else if (pointer != error_mark_node)
3923 : 18 : invalid_indirection_error (loc, type, errorstring);
3924 : :
3925 : 333 : return error_mark_node;
3926 : : }
3927 : :
3928 : : /* Entry point used by c-common, which expects folding. */
3929 : :
3930 : : tree
3931 : 20750 : build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3932 : : {
3933 : 20750 : return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3934 : 20750 : tf_warning_or_error, true);
3935 : : }
3936 : :
3937 : : /* Entry point used by internal indirection needs that don't correspond to any
3938 : : syntactic construct. */
3939 : :
3940 : : tree
3941 : 278389864 : cp_build_fold_indirect_ref (tree pointer)
3942 : : {
3943 : 278389864 : return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3944 : 278389864 : tf_warning_or_error, true);
3945 : : }
3946 : :
3947 : : /* Entry point used by indirection needs that correspond to some syntactic
3948 : : construct. */
3949 : :
3950 : : tree
3951 : 24388058 : cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3952 : : tsubst_flags_t complain)
3953 : : {
3954 : 24388058 : return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3955 : : }
3956 : :
3957 : : /* This handles expressions of the form "a[i]", which denotes
3958 : : an array reference.
3959 : :
3960 : : This is logically equivalent in C to *(a+i), but we may do it differently.
3961 : : If A is a variable or a member, we generate a primitive ARRAY_REF.
3962 : : This avoids forcing the array out of registers, and can work on
3963 : : arrays that are not lvalues (for example, members of structures returned
3964 : : by functions).
3965 : :
3966 : : If INDEX is of some user-defined type, it must be converted to
3967 : : integer type. Otherwise, to make a compatible PLUS_EXPR, it
3968 : : will inherit the type of the array, which will be some pointer type.
3969 : :
3970 : : LOC is the location to use in building the array reference. */
3971 : :
3972 : : tree
3973 : 5351364 : cp_build_array_ref (location_t loc, tree array, tree idx,
3974 : : tsubst_flags_t complain)
3975 : : {
3976 : 5351364 : tree first = NULL_TREE;
3977 : 5351364 : tree ret;
3978 : :
3979 : 5351364 : if (idx == 0)
3980 : : {
3981 : 0 : if (complain & tf_error)
3982 : 0 : error_at (loc, "subscript missing in array reference");
3983 : 0 : return error_mark_node;
3984 : : }
3985 : :
3986 : 5351364 : if (TREE_TYPE (array) == error_mark_node
3987 : 5351364 : || TREE_TYPE (idx) == error_mark_node)
3988 : : return error_mark_node;
3989 : :
3990 : : /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3991 : : inside it. */
3992 : 5351364 : switch (TREE_CODE (array))
3993 : : {
3994 : 28 : case COMPOUND_EXPR:
3995 : 28 : {
3996 : 28 : tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3997 : : complain);
3998 : 28 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3999 : 28 : TREE_OPERAND (array, 0), value);
4000 : 28 : SET_EXPR_LOCATION (ret, loc);
4001 : 28 : return ret;
4002 : : }
4003 : :
4004 : 58 : case COND_EXPR:
4005 : 58 : tree op0, op1, op2;
4006 : 58 : op0 = TREE_OPERAND (array, 0);
4007 : 58 : op1 = TREE_OPERAND (array, 1);
4008 : 58 : op2 = TREE_OPERAND (array, 2);
4009 : 58 : if (TREE_SIDE_EFFECTS (idx) || !tree_invariant_p (idx))
4010 : : {
4011 : : /* If idx could possibly have some SAVE_EXPRs, turning
4012 : : (op0 ? op1 : op2)[idx] into
4013 : : op0 ? op1[idx] : op2[idx] can lead into temporaries
4014 : : initialized in one conditional path and uninitialized
4015 : : uses of them in the other path.
4016 : : And if idx is a really large expression, evaluating it
4017 : : twice is also not optimal.
4018 : : On the other side, op0 must be sequenced before evaluation
4019 : : of op1 and op2 and for C++17 op0, op1 and op2 must be
4020 : : sequenced before idx.
4021 : : If idx is INTEGER_CST, we can just do the optimization
4022 : : without any SAVE_EXPRs, if op1 and op2 are both ARRAY_TYPE
4023 : : VAR_DECLs or COMPONENT_REFs thereof (so their address
4024 : : is constant or relative to frame), optimize into
4025 : : (SAVE_EXPR <op0>, SAVE_EXPR <idx>, SAVE_EXPR <op0>)
4026 : : ? op1[SAVE_EXPR <idx>] : op2[SAVE_EXPR <idx>]
4027 : : Otherwise avoid this optimization. */
4028 : 43 : if (flag_strong_eval_order == 2)
4029 : : {
4030 : 27 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4031 : : {
4032 : 18 : if (!address_invariant_p (op1) || !address_invariant_p (op2))
4033 : : {
4034 : : /* Force default conversion on array if
4035 : : we can't optimize this and array is ARRAY_TYPE
4036 : : COND_EXPR, we can't leave COND_EXPRs with
4037 : : ARRAY_TYPE in the IL. */
4038 : 8 : array = cp_default_conversion (array, complain);
4039 : 8 : if (error_operand_p (array))
4040 : 0 : return error_mark_node;
4041 : : break;
4042 : : }
4043 : : }
4044 : 16 : else if (!POINTER_TYPE_P (TREE_TYPE (array))
4045 : 2 : || !tree_invariant_p (op1)
4046 : 11 : || !tree_invariant_p (op2))
4047 : : break;
4048 : : }
4049 : 28 : if (TREE_SIDE_EFFECTS (idx))
4050 : : {
4051 : 9 : idx = save_expr (idx);
4052 : 9 : op0 = save_expr (op0);
4053 : 9 : tree tem = build_compound_expr (loc, op0, idx);
4054 : 9 : op0 = build_compound_expr (loc, tem, op0);
4055 : : }
4056 : : }
4057 : 43 : op1 = cp_build_array_ref (loc, op1, idx, complain);
4058 : 43 : op2 = cp_build_array_ref (loc, op2, idx, complain);
4059 : 43 : ret = build_conditional_expr (loc, op0, op1, op2, complain);
4060 : 43 : protected_set_expr_location (ret, loc);
4061 : 43 : return ret;
4062 : :
4063 : : default:
4064 : : break;
4065 : : }
4066 : :
4067 : 5351293 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
4068 : :
4069 : : /* 0[array] */
4070 : 5351293 : if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE)
4071 : : {
4072 : 7 : std::swap (array, idx);
4073 : 7 : if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array))
4074 : 6 : idx = first = save_expr (idx);
4075 : : }
4076 : :
4077 : 5351293 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4078 : : {
4079 : 1685791 : tree rval, type;
4080 : :
4081 : 1685791 : warn_array_subscript_with_type_char (loc, idx);
4082 : :
4083 : 1685791 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
4084 : : {
4085 : 3 : if (complain & tf_error)
4086 : 3 : error_at (loc, "array subscript is not an integer");
4087 : 3 : return error_mark_node;
4088 : : }
4089 : :
4090 : : /* Apply integral promotions *after* noticing character types.
4091 : : (It is unclear why we do these promotions -- the standard
4092 : : does not say that we should. In fact, the natural thing would
4093 : : seem to be to convert IDX to ptrdiff_t; we're performing
4094 : : pointer arithmetic.) */
4095 : 1685788 : idx = cp_perform_integral_promotions (idx, complain);
4096 : :
4097 : 1685788 : idx = maybe_fold_non_dependent_expr (idx, complain);
4098 : :
4099 : : /* An array that is indexed by a non-constant
4100 : : cannot be stored in a register; we must be able to do
4101 : : address arithmetic on its address.
4102 : : Likewise an array of elements of variable size. */
4103 : 1685788 : if (TREE_CODE (idx) != INTEGER_CST
4104 : 1685788 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4105 : 835133 : && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4106 : : != INTEGER_CST)))
4107 : : {
4108 : 851906 : if (!cxx_mark_addressable (array, true))
4109 : 0 : return error_mark_node;
4110 : : }
4111 : :
4112 : : /* An array that is indexed by a constant value which is not within
4113 : : the array bounds cannot be stored in a register either; because we
4114 : : would get a crash in store_bit_field/extract_bit_field when trying
4115 : : to access a non-existent part of the register. */
4116 : 1685788 : if (TREE_CODE (idx) == INTEGER_CST
4117 : 835136 : && TYPE_DOMAIN (TREE_TYPE (array))
4118 : 2519529 : && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4119 : : {
4120 : 2833 : if (!cxx_mark_addressable (array))
4121 : 0 : return error_mark_node;
4122 : : }
4123 : :
4124 : : /* Note in C++ it is valid to subscript a `register' array, since
4125 : : it is valid to take the address of something with that
4126 : : storage specification. */
4127 : 1685788 : if (extra_warnings)
4128 : : {
4129 : 37439 : tree foo = array;
4130 : 57436 : while (TREE_CODE (foo) == COMPONENT_REF)
4131 : 19997 : foo = TREE_OPERAND (foo, 0);
4132 : 8 : if (VAR_P (foo) && DECL_REGISTER (foo)
4133 : 37439 : && (complain & tf_warning))
4134 : 0 : warning_at (loc, OPT_Wextra,
4135 : : "subscripting array declared %<register%>");
4136 : : }
4137 : :
4138 : 1685788 : type = TREE_TYPE (TREE_TYPE (array));
4139 : 1685788 : rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4140 : : /* Array ref is const/volatile if the array elements are
4141 : : or if the array is.. */
4142 : 5057364 : TREE_READONLY (rval)
4143 : 1685788 : |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4144 : 5057364 : TREE_SIDE_EFFECTS (rval)
4145 : 1685788 : |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4146 : 3371576 : TREE_THIS_VOLATILE (rval)
4147 : 1685788 : |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4148 : 1685788 : ret = require_complete_type (rval, complain);
4149 : 1685788 : protected_set_expr_location (ret, loc);
4150 : 1685788 : if (non_lvalue)
4151 : 6070 : ret = non_lvalue_loc (loc, ret);
4152 : 1685788 : if (first)
4153 : 6 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4154 : 1685788 : return ret;
4155 : : }
4156 : :
4157 : 3665502 : {
4158 : 3665502 : tree ar = cp_default_conversion (array, complain);
4159 : 3665502 : tree ind = cp_default_conversion (idx, complain);
4160 : :
4161 : 3665502 : if (!processing_template_decl
4162 : 3665502 : && !first && flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
4163 : 99454 : ar = first = save_expr (ar);
4164 : :
4165 : : /* Put the integer in IND to simplify error checking. */
4166 : 3665502 : if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4167 : 57 : std::swap (ar, ind);
4168 : :
4169 : 3665502 : if (ar == error_mark_node || ind == error_mark_node)
4170 : : return error_mark_node;
4171 : :
4172 : 3665502 : if (!TYPE_PTR_P (TREE_TYPE (ar)))
4173 : : {
4174 : 22 : if (complain & tf_error)
4175 : 3 : error_at (loc, "subscripted value is neither array nor pointer");
4176 : 22 : return error_mark_node;
4177 : : }
4178 : 3665480 : if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4179 : : {
4180 : 0 : if (complain & tf_error)
4181 : 0 : error_at (loc, "array subscript is not an integer");
4182 : 0 : return error_mark_node;
4183 : : }
4184 : :
4185 : 3665480 : warn_array_subscript_with_type_char (loc, idx);
4186 : :
4187 : 3665480 : ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4188 : 3665480 : if (first)
4189 : 99454 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4190 : 3665480 : ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4191 : 3665480 : protected_set_expr_location (ret, loc);
4192 : 3665480 : if (non_lvalue)
4193 : 0 : ret = non_lvalue_loc (loc, ret);
4194 : : return ret;
4195 : : }
4196 : : }
4197 : :
4198 : : /* Entry point for Obj-C++. */
4199 : :
4200 : : tree
4201 : 3623916 : build_array_ref (location_t loc, tree array, tree idx)
4202 : : {
4203 : 3623916 : return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4204 : : }
4205 : :
4206 : : /* Resolve a pointer to member function. INSTANCE is the object
4207 : : instance to use, if the member points to a virtual member.
4208 : :
4209 : : This used to avoid checking for virtual functions if basetype
4210 : : has no virtual functions, according to an earlier ANSI draft.
4211 : : With the final ISO C++ rules, such an optimization is
4212 : : incorrect: A pointer to a derived member can be static_cast
4213 : : to pointer-to-base-member, as long as the dynamic object
4214 : : later has the right member. So now we only do this optimization
4215 : : when we know the dynamic type of the object. */
4216 : :
4217 : : tree
4218 : 90129 : get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4219 : : tsubst_flags_t complain)
4220 : : {
4221 : 90129 : if (TREE_CODE (function) == OFFSET_REF)
4222 : 0 : function = TREE_OPERAND (function, 1);
4223 : :
4224 : 90129 : if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4225 : : {
4226 : 90129 : tree idx, delta, e1, e2, e3, vtbl;
4227 : 90129 : bool nonvirtual;
4228 : 90129 : tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4229 : 90129 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4230 : :
4231 : 90129 : tree instance_ptr = *instance_ptrptr;
4232 : 90129 : tree instance_save_expr = 0;
4233 : 90129 : if (instance_ptr == error_mark_node)
4234 : : {
4235 : 0 : if (TREE_CODE (function) == PTRMEM_CST)
4236 : : {
4237 : : /* Extracting the function address from a pmf is only
4238 : : allowed with -Wno-pmf-conversions. It only works for
4239 : : pmf constants. */
4240 : 0 : e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4241 : 0 : e1 = convert (fntype, e1);
4242 : 0 : return e1;
4243 : : }
4244 : : else
4245 : : {
4246 : 0 : if (complain & tf_error)
4247 : 0 : error ("object missing in use of %qE", function);
4248 : 0 : return error_mark_node;
4249 : : }
4250 : : }
4251 : :
4252 : : /* True if we know that the dynamic type of the object doesn't have
4253 : : virtual functions, so we can assume the PFN field is a pointer. */
4254 : 90129 : nonvirtual = (COMPLETE_TYPE_P (basetype)
4255 : 90081 : && !TYPE_POLYMORPHIC_P (basetype)
4256 : 121040 : && resolves_to_fixed_type_p (instance_ptr, 0));
4257 : :
4258 : : /* If we don't really have an object (i.e. in an ill-formed
4259 : : conversion from PMF to pointer), we can't resolve virtual
4260 : : functions anyway. */
4261 : 89867 : if (!nonvirtual && is_dummy_object (instance_ptr))
4262 : : nonvirtual = true;
4263 : :
4264 : : /* Use force_target_expr even when instance_ptr doesn't have
4265 : : side-effects, unless it is a simple decl or constant, so
4266 : : that we don't ubsan instrument the expression multiple times.
4267 : : Don't use save_expr, as save_expr can avoid building a SAVE_EXPR
4268 : : and building a SAVE_EXPR manually can be optimized away during
4269 : : cp_fold. See PR116449 and PR117259. */
4270 : 90129 : if (TREE_SIDE_EFFECTS (instance_ptr)
4271 : 90129 : || (!nonvirtual
4272 : 50275 : && !DECL_P (instance_ptr)
4273 : 50275 : && !TREE_CONSTANT (instance_ptr)))
4274 : 89631 : instance_ptr = instance_save_expr
4275 : 89631 : = get_internal_target_expr (instance_ptr);
4276 : :
4277 : : /* See above comment. */
4278 : 90129 : if (TREE_SIDE_EFFECTS (function)
4279 : 90129 : || (!nonvirtual
4280 : 69900 : && !DECL_P (function)
4281 : 69888 : && !TREE_CONSTANT (function)))
4282 : 89652 : function = get_internal_target_expr (function);
4283 : :
4284 : : /* Start by extracting all the information from the PMF itself. */
4285 : 90129 : e3 = pfn_from_ptrmemfunc (function);
4286 : 90129 : delta = delta_from_ptrmemfunc (function);
4287 : 90129 : idx = build1 (NOP_EXPR, vtable_index_type, e3);
4288 : 90129 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4289 : : {
4290 : 90129 : int flag_sanitize_save;
4291 : 90129 : case ptrmemfunc_vbit_in_pfn:
4292 : 90129 : e1 = cp_build_binary_op (input_location,
4293 : : BIT_AND_EXPR, idx, integer_one_node,
4294 : : complain);
4295 : 90129 : idx = cp_build_binary_op (input_location,
4296 : : MINUS_EXPR, idx, integer_one_node,
4297 : : complain);
4298 : 90129 : if (idx == error_mark_node)
4299 : : return error_mark_node;
4300 : 90129 : break;
4301 : :
4302 : : case ptrmemfunc_vbit_in_delta:
4303 : : e1 = cp_build_binary_op (input_location,
4304 : : BIT_AND_EXPR, delta, integer_one_node,
4305 : : complain);
4306 : : /* Don't instrument the RSHIFT_EXPR we're about to create because
4307 : : we're going to use DELTA number of times, and that wouldn't play
4308 : : well with SAVE_EXPRs therein. */
4309 : : flag_sanitize_save = flag_sanitize;
4310 : : flag_sanitize = 0;
4311 : : delta = cp_build_binary_op (input_location,
4312 : : RSHIFT_EXPR, delta, integer_one_node,
4313 : : complain);
4314 : : flag_sanitize = flag_sanitize_save;
4315 : : if (delta == error_mark_node)
4316 : : return error_mark_node;
4317 : : break;
4318 : :
4319 : : default:
4320 : : gcc_unreachable ();
4321 : : }
4322 : :
4323 : 90129 : if (e1 == error_mark_node)
4324 : : return error_mark_node;
4325 : :
4326 : : /* Convert down to the right base before using the instance. A
4327 : : special case is that in a pointer to member of class C, C may
4328 : : be incomplete. In that case, the function will of course be
4329 : : a member of C, and no conversion is required. In fact,
4330 : : lookup_base will fail in that case, because incomplete
4331 : : classes do not have BINFOs. */
4332 : 90129 : if (!same_type_ignoring_top_level_qualifiers_p
4333 : 90129 : (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4334 : : {
4335 : 81 : basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4336 : : basetype, ba_check, NULL, complain);
4337 : 81 : instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4338 : : 1, complain);
4339 : 81 : if (instance_ptr == error_mark_node)
4340 : : return error_mark_node;
4341 : : }
4342 : : /* ...and then the delta in the PMF. */
4343 : 90129 : instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4344 : :
4345 : : /* Hand back the adjusted 'this' argument to our caller. */
4346 : 90129 : *instance_ptrptr = instance_ptr;
4347 : :
4348 : 90129 : if (nonvirtual)
4349 : : /* Now just return the pointer. */
4350 : : return e3;
4351 : :
4352 : : /* Next extract the vtable pointer from the object. */
4353 : 89849 : vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4354 : : instance_ptr);
4355 : 89849 : vtbl = cp_build_fold_indirect_ref (vtbl);
4356 : 89849 : if (vtbl == error_mark_node)
4357 : : return error_mark_node;
4358 : :
4359 : : /* Finally, extract the function pointer from the vtable. */
4360 : 89849 : e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4361 : 89849 : e2 = cp_build_fold_indirect_ref (e2);
4362 : 89849 : if (e2 == error_mark_node)
4363 : : return error_mark_node;
4364 : 89849 : TREE_CONSTANT (e2) = 1;
4365 : :
4366 : : /* When using function descriptors, the address of the
4367 : : vtable entry is treated as a function pointer. */
4368 : 89849 : if (TARGET_VTABLE_USES_DESCRIPTORS)
4369 : : e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4370 : : cp_build_addr_expr (e2, complain));
4371 : :
4372 : 89849 : e2 = fold_convert (TREE_TYPE (e3), e2);
4373 : 89849 : e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4374 : 89849 : if (e1 == error_mark_node)
4375 : : return error_mark_node;
4376 : :
4377 : : /* Make sure this doesn't get evaluated first inside one of the
4378 : : branches of the COND_EXPR. */
4379 : 89849 : if (instance_save_expr)
4380 : 89583 : e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4381 : : instance_save_expr, e1);
4382 : :
4383 : : function = e1;
4384 : : }
4385 : : return function;
4386 : : }
4387 : :
4388 : : /* Used by the C-common bits. */
4389 : : tree
4390 : 0 : build_function_call (location_t /*loc*/,
4391 : : tree function, tree params)
4392 : : {
4393 : 0 : return cp_build_function_call (function, params, tf_warning_or_error);
4394 : : }
4395 : :
4396 : : /* Used by the C-common bits. */
4397 : : tree
4398 : 236553 : build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4399 : : tree function, vec<tree, va_gc> *params,
4400 : : vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4401 : : {
4402 : 236553 : vec<tree, va_gc> *orig_params = params;
4403 : 236553 : tree ret = cp_build_function_call_vec (function, ¶ms,
4404 : : tf_warning_or_error, orig_function);
4405 : :
4406 : : /* cp_build_function_call_vec can reallocate PARAMS by adding
4407 : : default arguments. That should never happen here. Verify
4408 : : that. */
4409 : 236553 : gcc_assert (params == orig_params);
4410 : :
4411 : 236553 : return ret;
4412 : : }
4413 : :
4414 : : /* Build a function call using a tree list of arguments. */
4415 : :
4416 : : static tree
4417 : 0 : cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4418 : : {
4419 : 0 : tree ret;
4420 : :
4421 : 0 : releasing_vec vec;
4422 : 0 : for (; params != NULL_TREE; params = TREE_CHAIN (params))
4423 : 0 : vec_safe_push (vec, TREE_VALUE (params));
4424 : 0 : ret = cp_build_function_call_vec (function, &vec, complain);
4425 : 0 : return ret;
4426 : 0 : }
4427 : :
4428 : : /* Build a function call using varargs. */
4429 : :
4430 : : tree
4431 : 524755 : cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4432 : : {
4433 : 524755 : va_list args;
4434 : 524755 : tree ret, t;
4435 : :
4436 : 524755 : releasing_vec vec;
4437 : 524755 : va_start (args, complain);
4438 : 1295688 : for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4439 : 770933 : vec_safe_push (vec, t);
4440 : 524755 : va_end (args);
4441 : 524755 : ret = cp_build_function_call_vec (function, &vec, complain);
4442 : 524755 : return ret;
4443 : 524755 : }
4444 : :
4445 : : /* C++ implementation of callback for use when checking param types. */
4446 : :
4447 : : bool
4448 : 18 : cp_comp_parm_types (tree wanted_type, tree actual_type)
4449 : : {
4450 : 18 : if (TREE_CODE (wanted_type) == POINTER_TYPE
4451 : 18 : && TREE_CODE (actual_type) == POINTER_TYPE)
4452 : 15 : return same_or_base_type_p (TREE_TYPE (wanted_type),
4453 : : TREE_TYPE (actual_type));
4454 : : return false;
4455 : : }
4456 : :
4457 : : /* Build a function call using a vector of arguments.
4458 : : If FUNCTION is the result of resolving an overloaded target built-in,
4459 : : ORIG_FNDECL is the original function decl, otherwise it is null.
4460 : : PARAMS may be NULL if there are no parameters. This changes the
4461 : : contents of PARAMS. */
4462 : :
4463 : : tree
4464 : 3274475 : cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4465 : : tsubst_flags_t complain, tree orig_fndecl)
4466 : : {
4467 : 3274475 : tree fntype, fndecl;
4468 : 3274475 : int is_method;
4469 : 3274475 : tree original = function;
4470 : 3274475 : int nargs;
4471 : 3274475 : tree *argarray;
4472 : 3274475 : tree parm_types;
4473 : 3274475 : vec<tree, va_gc> *allocated = NULL;
4474 : 3274475 : tree ret;
4475 : :
4476 : : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4477 : : expressions, like those used for ObjC messenger dispatches. */
4478 : 3274475 : if (params != NULL && !vec_safe_is_empty (*params))
4479 : 1761219 : function = objc_rewrite_function_call (function, (**params)[0]);
4480 : :
4481 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4482 : : Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4483 : 3274475 : if (TREE_CODE (function) == NOP_EXPR
4484 : 3274475 : && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4485 : 15 : function = TREE_OPERAND (function, 0);
4486 : :
4487 : 3274475 : if (TREE_CODE (function) == FUNCTION_DECL)
4488 : : {
4489 : 1375802 : if (!mark_used (function, complain))
4490 : 24 : return error_mark_node;
4491 : 1375778 : fndecl = function;
4492 : :
4493 : : /* Convert anything with function type to a pointer-to-function. */
4494 : 1375778 : if (DECL_MAIN_P (function))
4495 : : {
4496 : 0 : if (complain & tf_error)
4497 : 0 : pedwarn (input_location, OPT_Wpedantic,
4498 : : "ISO C++ forbids calling %<::main%> from within program");
4499 : : else
4500 : 0 : return error_mark_node;
4501 : : }
4502 : 1375778 : function = build_addr_func (function, complain);
4503 : : }
4504 : : else
4505 : : {
4506 : 1898673 : fndecl = NULL_TREE;
4507 : :
4508 : 1898673 : function = build_addr_func (function, complain);
4509 : : }
4510 : :
4511 : 3274451 : if (function == error_mark_node)
4512 : : return error_mark_node;
4513 : :
4514 : 3274349 : fntype = TREE_TYPE (function);
4515 : :
4516 : 3274349 : if (TYPE_PTRMEMFUNC_P (fntype))
4517 : : {
4518 : 15 : if (complain & tf_error)
4519 : 15 : error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4520 : : "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4521 : : original, original);
4522 : 15 : return error_mark_node;
4523 : : }
4524 : :
4525 : 6548668 : is_method = (TYPE_PTR_P (fntype)
4526 : 3274334 : && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4527 : :
4528 : 3274334 : if (!(TYPE_PTRFN_P (fntype)
4529 : 91455 : || is_method
4530 : 1399 : || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4531 : : {
4532 : 1399 : if (complain & tf_error)
4533 : : {
4534 : 174 : if (is_stub_object (original))
4535 : 3 : error_at (input_location,
4536 : : "%qT cannot be used as a function",
4537 : 3 : TREE_TYPE (original));
4538 : 171 : else if (!flag_diagnostics_show_caret)
4539 : 171 : error_at (input_location,
4540 : : "%qE cannot be used as a function", original);
4541 : 0 : else if (DECL_P (original))
4542 : 0 : error_at (input_location,
4543 : : "%qD cannot be used as a function", original);
4544 : : else
4545 : 0 : error_at (input_location,
4546 : : "expression cannot be used as a function");
4547 : : }
4548 : :
4549 : 1399 : return error_mark_node;
4550 : : }
4551 : :
4552 : : /* fntype now gets the type of function pointed to. */
4553 : 3272935 : fntype = TREE_TYPE (fntype);
4554 : 3272935 : parm_types = TYPE_ARG_TYPES (fntype);
4555 : :
4556 : 3272935 : if (params == NULL)
4557 : : {
4558 : 162685 : allocated = make_tree_vector ();
4559 : 162685 : params = &allocated;
4560 : : }
4561 : :
4562 : 3272935 : nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4563 : : complain);
4564 : 3272935 : if (nargs < 0)
4565 : 1638 : return error_mark_node;
4566 : :
4567 : 3271297 : argarray = (*params)->address ();
4568 : :
4569 : : /* Check for errors in format strings and inappropriately
4570 : : null parameters. */
4571 : 3271297 : bool warned_p
4572 : 3271297 : = ((complain & tf_warning)
4573 : 3271297 : && check_function_arguments (input_location, fndecl, fntype,
4574 : : nargs, argarray, NULL,
4575 : 3271297 : cp_comp_parm_types));
4576 : :
4577 : 3271297 : ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4578 : :
4579 : 3271297 : if (warned_p)
4580 : : {
4581 : 27 : tree c = extract_call_expr (ret);
4582 : 27 : if (TREE_CODE (c) == CALL_EXPR)
4583 : 27 : suppress_warning (c, OPT_Wnonnull);
4584 : : }
4585 : :
4586 : 3271297 : if (allocated != NULL)
4587 : 162685 : release_tree_vector (allocated);
4588 : :
4589 : : return ret;
4590 : : }
4591 : :
4592 : : /* Subroutine of convert_arguments.
4593 : : Print an error message about a wrong number of arguments. */
4594 : :
4595 : : static void
4596 : 156 : error_args_num (location_t loc, tree fndecl, bool too_many_p)
4597 : : {
4598 : 156 : if (fndecl)
4599 : : {
4600 : 120 : auto_diagnostic_group d;
4601 : 120 : if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4602 : : {
4603 : 0 : if (DECL_NAME (fndecl) == NULL_TREE
4604 : 0 : || (DECL_NAME (fndecl)
4605 : 0 : == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4606 : 0 : error_at (loc,
4607 : : too_many_p
4608 : : ? G_("too many arguments to constructor %q#D")
4609 : : : G_("too few arguments to constructor %q#D"),
4610 : : fndecl);
4611 : : else
4612 : 0 : error_at (loc,
4613 : : too_many_p
4614 : : ? G_("too many arguments to member function %q#D")
4615 : : : G_("too few arguments to member function %q#D"),
4616 : : fndecl);
4617 : : }
4618 : : else
4619 : 183 : error_at (loc,
4620 : : too_many_p
4621 : : ? G_("too many arguments to function %q#D")
4622 : : : G_("too few arguments to function %q#D"),
4623 : : fndecl);
4624 : 120 : if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4625 : 81 : inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4626 : 120 : }
4627 : : else
4628 : : {
4629 : 36 : if (c_dialect_objc () && objc_message_selector ())
4630 : 0 : error_at (loc,
4631 : : too_many_p
4632 : : ? G_("too many arguments to method %q#D")
4633 : : : G_("too few arguments to method %q#D"),
4634 : : objc_message_selector ());
4635 : : else
4636 : 57 : error_at (loc, too_many_p ? G_("too many arguments to function")
4637 : : : G_("too few arguments to function"));
4638 : : }
4639 : 156 : }
4640 : :
4641 : : /* Convert the actual parameter expressions in the list VALUES to the
4642 : : types in the list TYPELIST. The converted expressions are stored
4643 : : back in the VALUES vector.
4644 : : If parmdecls is exhausted, or when an element has NULL as its type,
4645 : : perform the default conversions.
4646 : :
4647 : : NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4648 : :
4649 : : This is also where warnings about wrong number of args are generated.
4650 : :
4651 : : Returns the actual number of arguments processed (which might be less
4652 : : than the length of the vector), or -1 on error.
4653 : :
4654 : : In C++, unspecified trailing parameters can be filled in with their
4655 : : default arguments, if such were specified. Do so here. */
4656 : :
4657 : : static int
4658 : 3272935 : convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4659 : : int flags, tsubst_flags_t complain)
4660 : : {
4661 : 3272935 : tree typetail;
4662 : 3272935 : unsigned int i;
4663 : :
4664 : : /* Argument passing is always copy-initialization. */
4665 : 3272935 : flags |= LOOKUP_ONLYCONVERTING;
4666 : :
4667 : 7236366 : for (i = 0, typetail = typelist;
4668 : 7236366 : i < vec_safe_length (*values);
4669 : : i++)
4670 : : {
4671 : 7929375 : tree type = typetail ? TREE_VALUE (typetail) : 0;
4672 : 3964912 : tree val = (**values)[i];
4673 : :
4674 : 3964912 : if (val == error_mark_node || type == error_mark_node)
4675 : : return -1;
4676 : :
4677 : 3964906 : if (type == void_type_node)
4678 : : {
4679 : 200 : if (complain & tf_error)
4680 : 72 : error_args_num (input_location, fndecl, /*too_many_p=*/true);
4681 : 200 : return -1;
4682 : : }
4683 : :
4684 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4685 : : Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4686 : 3964706 : if (TREE_CODE (val) == NOP_EXPR
4687 : 952631 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4688 : 3964724 : && (type == 0 || !TYPE_REF_P (type)))
4689 : 18 : val = TREE_OPERAND (val, 0);
4690 : :
4691 : 3964706 : if (type == 0 || !TYPE_REF_P (type))
4692 : : {
4693 : 3743628 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4694 : 3743628 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4695 : 11220 : val = decay_conversion (val, complain);
4696 : : }
4697 : :
4698 : 3964706 : if (val == error_mark_node)
4699 : : return -1;
4700 : :
4701 : 3964706 : if (type != 0)
4702 : : {
4703 : : /* Formal parm type is specified by a function prototype. */
4704 : 3964257 : tree parmval;
4705 : :
4706 : 3964257 : if (!COMPLETE_TYPE_P (complete_type (type)))
4707 : : {
4708 : 18 : if (complain & tf_error)
4709 : : {
4710 : 12 : location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4711 : 12 : if (fndecl)
4712 : : {
4713 : 12 : auto_diagnostic_group d;
4714 : 12 : error_at (loc,
4715 : : "parameter %P of %qD has incomplete type %qT",
4716 : : i, fndecl, type);
4717 : 12 : inform (get_fndecl_argument_location (fndecl, i),
4718 : : " declared here");
4719 : 12 : }
4720 : : else
4721 : 0 : error_at (loc, "parameter %P has incomplete type %qT", i,
4722 : : type);
4723 : : }
4724 : 18 : parmval = error_mark_node;
4725 : : }
4726 : : else
4727 : : {
4728 : 3964239 : parmval = convert_for_initialization
4729 : 3964239 : (NULL_TREE, type, val, flags,
4730 : : ICR_ARGPASS, fndecl, i, complain);
4731 : 3964239 : parmval = convert_for_arg_passing (type, parmval, complain);
4732 : : }
4733 : :
4734 : 3964257 : if (parmval == error_mark_node)
4735 : : return -1;
4736 : :
4737 : 3962982 : (**values)[i] = parmval;
4738 : : }
4739 : : else
4740 : : {
4741 : 449 : int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4742 : 21 : if (magic)
4743 : : {
4744 : : /* Don't truncate excess precision to the semantic type. */
4745 : 0 : if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4746 : 0 : val = TREE_OPERAND (val, 0);
4747 : : /* Don't do ellipsis conversion for __built_in_constant_p
4748 : : as this will result in spurious errors for non-trivial
4749 : : types. */
4750 : 0 : val = require_complete_type (val, complain);
4751 : : }
4752 : : else
4753 : 449 : val = convert_arg_to_ellipsis (val, complain);
4754 : :
4755 : 449 : (**values)[i] = val;
4756 : : }
4757 : :
4758 : 3963431 : if (typetail)
4759 : 3962982 : typetail = TREE_CHAIN (typetail);
4760 : : }
4761 : :
4762 : 3271454 : if (typetail != 0 && typetail != void_list_node)
4763 : : {
4764 : : /* See if there are default arguments that can be used. Because
4765 : : we hold default arguments in the FUNCTION_TYPE (which is so
4766 : : wrong), we can see default parameters here from deduced
4767 : : contexts (and via typeof) for indirect function calls.
4768 : : Fortunately we know whether we have a function decl to
4769 : : provide default arguments in a language conformant
4770 : : manner. */
4771 : 63 : if (fndecl && TREE_PURPOSE (typetail)
4772 : 160 : && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4773 : : {
4774 : 6 : for (; typetail != void_list_node; ++i)
4775 : : {
4776 : : /* After DR777, with explicit template args we can end up with a
4777 : : default argument followed by no default argument. */
4778 : 6 : if (!TREE_PURPOSE (typetail))
4779 : : break;
4780 : 3 : tree parmval
4781 : 3 : = convert_default_arg (TREE_VALUE (typetail),
4782 : 3 : TREE_PURPOSE (typetail),
4783 : 3 : fndecl, i, complain);
4784 : :
4785 : 3 : if (parmval == error_mark_node)
4786 : 0 : return -1;
4787 : :
4788 : 3 : vec_safe_push (*values, parmval);
4789 : 3 : typetail = TREE_CHAIN (typetail);
4790 : : /* ends with `...'. */
4791 : 3 : if (typetail == NULL_TREE)
4792 : : break;
4793 : : }
4794 : : }
4795 : :
4796 : 157 : if (typetail && typetail != void_list_node)
4797 : : {
4798 : 157 : if (complain & tf_error)
4799 : 84 : error_args_num (input_location, fndecl, /*too_many_p=*/false);
4800 : 157 : return -1;
4801 : : }
4802 : : }
4803 : :
4804 : 3271297 : return (int) i;
4805 : : }
4806 : :
4807 : : /* Build a binary-operation expression, after performing default
4808 : : conversions on the operands. CODE is the kind of expression to
4809 : : build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4810 : : are the tree codes which correspond to ARG1 and ARG2 when issuing
4811 : : warnings about possibly misplaced parentheses. They may differ
4812 : : from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4813 : : folding (e.g., if the parser sees "a | 1 + 1", it may call this
4814 : : routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4815 : : To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4816 : : ARG2_CODE as ERROR_MARK. */
4817 : :
4818 : : tree
4819 : 198009446 : build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4820 : : enum tree_code arg1_code, tree arg2,
4821 : : enum tree_code arg2_code, tree lookups,
4822 : : tree *overload_p, tsubst_flags_t complain)
4823 : : {
4824 : 198009446 : tree orig_arg1;
4825 : 198009446 : tree orig_arg2;
4826 : 198009446 : tree expr;
4827 : 198009446 : tree overload = NULL_TREE;
4828 : :
4829 : 198009446 : orig_arg1 = arg1;
4830 : 198009446 : orig_arg2 = arg2;
4831 : :
4832 : 198009446 : if (processing_template_decl)
4833 : : {
4834 : 117667985 : if (type_dependent_expression_p (arg1)
4835 : 117667985 : || type_dependent_expression_p (arg2))
4836 : : {
4837 : 84145502 : expr = build_min_nt_loc (loc, code, arg1, arg2);
4838 : 84145502 : TREE_TYPE (expr)
4839 : 84145502 : = build_dependent_operator_type (lookups, code, false);
4840 : 84145502 : return expr;
4841 : : }
4842 : : }
4843 : :
4844 : 113863944 : if (code == DOTSTAR_EXPR)
4845 : 90424 : expr = build_m_component_ref (arg1, arg2, complain);
4846 : : else
4847 : 113773520 : expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4848 : : lookups, &overload, complain);
4849 : :
4850 : 113863911 : if (overload_p != NULL)
4851 : 47813694 : *overload_p = overload;
4852 : :
4853 : : /* Check for cases such as x+y<<z which users are likely to
4854 : : misinterpret. But don't warn about obj << x + y, since that is a
4855 : : common idiom for I/O. */
4856 : 113863911 : if (warn_parentheses
4857 : 1107010 : && (complain & tf_warning)
4858 : 1068829 : && !processing_template_decl
4859 : 741139 : && !error_operand_p (arg1)
4860 : 741133 : && !error_operand_p (arg2)
4861 : 114605032 : && (code != LSHIFT_EXPR
4862 : 67607 : || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4863 : 740136 : warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4864 : : arg2_code, orig_arg2);
4865 : :
4866 : 113863911 : if (processing_template_decl && expr != error_mark_node)
4867 : : {
4868 : 33522300 : if (overload != NULL_TREE)
4869 : 2320027 : return (build_min_non_dep_op_overload
4870 : 2320027 : (code, expr, overload, orig_arg1, orig_arg2));
4871 : :
4872 : 31202273 : return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4873 : : }
4874 : :
4875 : : return expr;
4876 : : }
4877 : :
4878 : : /* Build and return an ARRAY_REF expression. */
4879 : :
4880 : : tree
4881 : 1964068 : build_x_array_ref (location_t loc, tree arg1, tree arg2,
4882 : : tsubst_flags_t complain)
4883 : : {
4884 : 1964068 : tree orig_arg1 = arg1;
4885 : 1964068 : tree orig_arg2 = arg2;
4886 : 1964068 : tree expr;
4887 : 1964068 : tree overload = NULL_TREE;
4888 : :
4889 : 1964068 : if (processing_template_decl)
4890 : : {
4891 : 665 : if (type_dependent_expression_p (arg1)
4892 : 665 : || type_dependent_expression_p (arg2))
4893 : 639 : return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4894 : 639 : NULL_TREE, NULL_TREE);
4895 : : }
4896 : :
4897 : 1963429 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4898 : : NULL_TREE, NULL_TREE, &overload, complain);
4899 : :
4900 : 1963429 : if (processing_template_decl && expr != error_mark_node)
4901 : : {
4902 : 23 : if (overload != NULL_TREE)
4903 : 9 : return (build_min_non_dep_op_overload
4904 : 9 : (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4905 : :
4906 : 14 : return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4907 : 14 : NULL_TREE, NULL_TREE);
4908 : : }
4909 : : return expr;
4910 : : }
4911 : :
4912 : : /* Build an OpenMP array section reference, creating an exact type for the
4913 : : resulting expression based on the element type and bounds if possible. If
4914 : : we have variable bounds, create an incomplete array type for the result
4915 : : instead. */
4916 : :
4917 : : tree
4918 : 10692 : build_omp_array_section (location_t loc, tree array_expr, tree index,
4919 : : tree length)
4920 : : {
4921 : 10692 : if (TREE_CODE (array_expr) == TYPE_DECL
4922 : 10692 : || type_dependent_expression_p (array_expr))
4923 : 274 : return build3_loc (loc, OMP_ARRAY_SECTION, NULL_TREE, array_expr, index,
4924 : 274 : length);
4925 : :
4926 : 10418 : tree type = TREE_TYPE (array_expr);
4927 : 10418 : gcc_assert (type);
4928 : 10418 : type = non_reference (type);
4929 : :
4930 : 10418 : tree sectype, eltype = TREE_TYPE (type);
4931 : :
4932 : : /* It's not an array or pointer type. Just reuse the type of the
4933 : : original expression as the type of the array section (an error will be
4934 : : raised anyway, later). */
4935 : 10418 : if (eltype == NULL_TREE)
4936 : 42 : sectype = TREE_TYPE (array_expr);
4937 : : else
4938 : : {
4939 : 10376 : tree idxtype = NULL_TREE;
4940 : :
4941 : : /* If we know the integer bounds, create an index type with exact
4942 : : low/high (or zero/length) bounds. Otherwise, create an incomplete
4943 : : array type. (This mostly only affects diagnostics.) */
4944 : 10376 : if (index != NULL_TREE
4945 : 10376 : && length != NULL_TREE
4946 : 7482 : && TREE_CODE (index) == INTEGER_CST
4947 : 6686 : && TREE_CODE (length) == INTEGER_CST)
4948 : : {
4949 : 5191 : tree low = fold_convert (sizetype, index);
4950 : 5191 : tree high = fold_convert (sizetype, length);
4951 : 5191 : high = size_binop (PLUS_EXPR, low, high);
4952 : 5191 : high = size_binop (MINUS_EXPR, high, size_one_node);
4953 : 5191 : idxtype = build_range_type (sizetype, low, high);
4954 : 5191 : }
4955 : 2948 : else if ((index == NULL_TREE || integer_zerop (index))
4956 : 3555 : && length != NULL_TREE
4957 : 7290 : && TREE_CODE (length) == INTEGER_CST)
4958 : 1384 : idxtype = build_index_type (length);
4959 : :
4960 : 10376 : sectype = build_array_type (eltype, idxtype);
4961 : : }
4962 : :
4963 : 10418 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array_expr, index,
4964 : 10418 : length);
4965 : : }
4966 : :
4967 : : /* Return whether OP is an expression of enum type cast to integer
4968 : : type. In C++ even unsigned enum types are cast to signed integer
4969 : : types. We do not want to issue warnings about comparisons between
4970 : : signed and unsigned types when one of the types is an enum type.
4971 : : Those warnings are always false positives in practice. */
4972 : :
4973 : : static bool
4974 : 578202 : enum_cast_to_int (tree op)
4975 : : {
4976 : 566445 : if (CONVERT_EXPR_P (op)
4977 : 12863 : && TREE_TYPE (op) == integer_type_node
4978 : 800 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4979 : 578224 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4980 : : return true;
4981 : :
4982 : : /* The cast may have been pushed into a COND_EXPR. */
4983 : 578188 : if (TREE_CODE (op) == COND_EXPR)
4984 : 761 : return (enum_cast_to_int (TREE_OPERAND (op, 1))
4985 : 761 : || enum_cast_to_int (TREE_OPERAND (op, 2)));
4986 : :
4987 : : return false;
4988 : : }
4989 : :
4990 : : /* For the c-common bits. */
4991 : : tree
4992 : 4017877 : build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4993 : : bool /*convert_p*/)
4994 : : {
4995 : 4017877 : return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4996 : : }
4997 : :
4998 : : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4999 : : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
5000 : :
5001 : : static tree
5002 : 6593 : build_vec_cmp (tree_code code, tree type,
5003 : : tree arg0, tree arg1)
5004 : : {
5005 : 6593 : tree zero_vec = build_zero_cst (type);
5006 : 6593 : tree minus_one_vec = build_minus_one_cst (type);
5007 : 6593 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
5008 : 6593 : tree cmp = build2 (code, cmp_type, arg0, arg1);
5009 : 6593 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
5010 : : }
5011 : :
5012 : : /* Possibly warn about an address never being NULL. */
5013 : :
5014 : : static void
5015 : 1597095 : warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
5016 : : {
5017 : : /* Prevent warnings issued for macro expansion. */
5018 : 1597095 : if (!warn_address
5019 : 40334 : || (complain & tf_warning) == 0
5020 : 24088 : || c_inhibit_evaluation_warnings != 0
5021 : 24048 : || from_macro_expansion_at (location)
5022 : 1614531 : || warning_suppressed_p (op, OPT_Waddress))
5023 : 1579982 : return;
5024 : :
5025 : 17403 : tree cop = fold_for_warn (op);
5026 : :
5027 : 17403 : if (TREE_CODE (cop) == NON_LVALUE_EXPR)
5028 : : /* Unwrap the expression for C++ 98. */
5029 : 1 : cop = TREE_OPERAND (cop, 0);
5030 : :
5031 : 17403 : if (TREE_CODE (cop) == PTRMEM_CST)
5032 : : {
5033 : : /* The address of a nonstatic data member is never null. */
5034 : 30 : warning_at (location, OPT_Waddress,
5035 : : "the address %qE will never be NULL",
5036 : : cop);
5037 : 30 : return;
5038 : : }
5039 : :
5040 : 17373 : if (TREE_CODE (cop) == NOP_EXPR)
5041 : : {
5042 : : /* Allow casts to intptr_t to suppress the warning. */
5043 : 1098 : tree type = TREE_TYPE (cop);
5044 : 1098 : if (TREE_CODE (type) == INTEGER_TYPE)
5045 : : return;
5046 : :
5047 : 1098 : STRIP_NOPS (cop);
5048 : : }
5049 : :
5050 : 17373 : auto_diagnostic_group d;
5051 : 17373 : bool warned = false;
5052 : 17373 : if (TREE_CODE (cop) == ADDR_EXPR)
5053 : : {
5054 : 789 : cop = TREE_OPERAND (cop, 0);
5055 : :
5056 : : /* Set to true in the loop below if OP dereferences its operand.
5057 : : In such a case the ultimate target need not be a decl for
5058 : : the null [in]equality test to be necessarily constant. */
5059 : 789 : bool deref = false;
5060 : :
5061 : : /* Get the outermost array or object, or member. */
5062 : 960 : while (handled_component_p (cop))
5063 : : {
5064 : 270 : if (TREE_CODE (cop) == COMPONENT_REF)
5065 : : {
5066 : : /* Get the member (its address is never null). */
5067 : 99 : cop = TREE_OPERAND (cop, 1);
5068 : 99 : break;
5069 : : }
5070 : :
5071 : : /* Get the outer array/object to refer to in the warning. */
5072 : 171 : cop = TREE_OPERAND (cop, 0);
5073 : 171 : deref = true;
5074 : : }
5075 : :
5076 : 639 : if ((!deref && !decl_with_nonnull_addr_p (cop))
5077 : 631 : || from_macro_expansion_at (location)
5078 : 1420 : || warning_suppressed_p (cop, OPT_Waddress))
5079 : 158 : return;
5080 : :
5081 : 631 : warned = warning_at (location, OPT_Waddress,
5082 : : "the address of %qD will never be NULL", cop);
5083 : 631 : op = cop;
5084 : : }
5085 : 16584 : else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
5086 : : {
5087 : : /* Adding zero to the null pointer is well-defined in C++. When
5088 : : the offset is unknown (i.e., not a constant) warn anyway since
5089 : : it's less likely that the pointer operand is null than not. */
5090 : 102 : tree off = TREE_OPERAND (cop, 1);
5091 : 102 : if (!integer_zerop (off)
5092 : 102 : && !warning_suppressed_p (cop, OPT_Waddress))
5093 : : {
5094 : 102 : tree base = TREE_OPERAND (cop, 0);
5095 : 102 : STRIP_NOPS (base);
5096 : 102 : if (TYPE_REF_P (TREE_TYPE (base)))
5097 : 12 : warning_at (location, OPT_Waddress, "the compiler can assume that "
5098 : : "the address of %qE will never be NULL", base);
5099 : : else
5100 : 90 : warning_at (location, OPT_Waddress, "comparing the result of "
5101 : : "pointer addition %qE and NULL", cop);
5102 : : }
5103 : 102 : return;
5104 : : }
5105 : 15665 : else if (CONVERT_EXPR_P (op)
5106 : 16544 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
5107 : : {
5108 : 62 : STRIP_NOPS (op);
5109 : :
5110 : 62 : if (TREE_CODE (op) == COMPONENT_REF)
5111 : 24 : op = TREE_OPERAND (op, 1);
5112 : :
5113 : 62 : if (DECL_P (op))
5114 : 62 : warned = warning_at (location, OPT_Waddress,
5115 : : "the compiler can assume that the address of "
5116 : : "%qD will never be NULL", op);
5117 : : }
5118 : :
5119 : 693 : if (warned && DECL_P (op))
5120 : 657 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
5121 : 17373 : }
5122 : :
5123 : : /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5124 : : the other operand is of a different enumeration type or a floating-point
5125 : : type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5126 : : code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5127 : : and LOC is the location for the whole binary expression.
5128 : : For C++26 this is ill-formed rather than deprecated.
5129 : : Return true for SFINAE errors.
5130 : : TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5131 : :
5132 : : static bool
5133 : 94506232 : do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5134 : : tree type1, tsubst_flags_t complain)
5135 : : {
5136 : 94506232 : if (TREE_CODE (type0) == ENUMERAL_TYPE
5137 : 2794924 : && TREE_CODE (type1) == ENUMERAL_TYPE
5138 : 96777191 : && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
5139 : : {
5140 : 198 : if (cxx_dialect >= cxx26)
5141 : : {
5142 : 60 : if ((complain & tf_warning_or_error) == 0)
5143 : : return true;
5144 : : }
5145 : 138 : else if ((complain & tf_warning) == 0)
5146 : : return false;
5147 : : /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5148 : : Otherwise, warn if -Wenum-conversion is on. */
5149 : 190 : enum opt_code opt;
5150 : 190 : if (warn_deprecated_enum_enum_conv)
5151 : : opt = OPT_Wdeprecated_enum_enum_conversion;
5152 : 97 : else if (warn_enum_conversion)
5153 : : opt = OPT_Wenum_conversion;
5154 : : else
5155 : : return false;
5156 : :
5157 : 115 : switch (code)
5158 : : {
5159 : : case GT_EXPR:
5160 : : case LT_EXPR:
5161 : : case GE_EXPR:
5162 : : case LE_EXPR:
5163 : : case EQ_EXPR:
5164 : : case NE_EXPR:
5165 : : /* Comparisons are handled by -Wenum-compare. */
5166 : : return false;
5167 : : case SPACESHIP_EXPR:
5168 : : /* This is invalid, don't warn. */
5169 : : return false;
5170 : 17 : case BIT_AND_EXPR:
5171 : 17 : case BIT_IOR_EXPR:
5172 : 17 : case BIT_XOR_EXPR:
5173 : 17 : if (cxx_dialect >= cxx26)
5174 : 5 : pedwarn (loc, opt, "bitwise operation between different "
5175 : : "enumeration types %qT and %qT", type0, type1);
5176 : : else
5177 : 12 : warning_at (loc, opt, "bitwise operation between different "
5178 : : "enumeration types %qT and %qT is deprecated",
5179 : : type0, type1);
5180 : 17 : return false;
5181 : 41 : default:
5182 : 41 : if (cxx_dialect >= cxx26)
5183 : 12 : pedwarn (loc, opt, "arithmetic between different enumeration "
5184 : : "types %qT and %qT", type0, type1);
5185 : : else
5186 : 29 : warning_at (loc, opt, "arithmetic between different enumeration "
5187 : : "types %qT and %qT is deprecated", type0, type1);
5188 : 41 : return false;
5189 : : }
5190 : : }
5191 : 94506034 : else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5192 : 2794726 : && SCALAR_FLOAT_TYPE_P (type1))
5193 : 94505957 : || (SCALAR_FLOAT_TYPE_P (type0)
5194 : 32852581 : && TREE_CODE (type1) == ENUMERAL_TYPE))
5195 : : {
5196 : 154 : if (cxx_dialect >= cxx26)
5197 : : {
5198 : 46 : if ((complain & tf_warning_or_error) == 0)
5199 : : return true;
5200 : : }
5201 : 108 : else if ((complain & tf_warning) == 0)
5202 : : return false;
5203 : 142 : const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5204 : : /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5205 : : Otherwise, warn if -Wenum-conversion is on. */
5206 : 142 : enum opt_code opt;
5207 : 142 : if (warn_deprecated_enum_float_conv)
5208 : : opt = OPT_Wdeprecated_enum_float_conversion;
5209 : 75 : else if (warn_enum_conversion)
5210 : : opt = OPT_Wenum_conversion;
5211 : : else
5212 : : return false;
5213 : :
5214 : 84 : switch (code)
5215 : : {
5216 : 34 : case GT_EXPR:
5217 : 34 : case LT_EXPR:
5218 : 34 : case GE_EXPR:
5219 : 34 : case LE_EXPR:
5220 : 34 : case EQ_EXPR:
5221 : 34 : case NE_EXPR:
5222 : 34 : if (enum_first_p && cxx_dialect >= cxx26)
5223 : 5 : pedwarn (loc, opt, "comparison of enumeration type %qT with "
5224 : : "floating-point type %qT", type0, type1);
5225 : 29 : else if (cxx_dialect >= cxx26)
5226 : 5 : pedwarn (loc, opt, "comparison of floating-point type %qT "
5227 : : "with enumeration type %qT", type0, type1);
5228 : 24 : else if (enum_first_p)
5229 : 12 : warning_at (loc, opt, "comparison of enumeration type %qT with "
5230 : : "floating-point type %qT is deprecated",
5231 : : type0, type1);
5232 : : else
5233 : 12 : warning_at (loc, opt, "comparison of floating-point type %qT "
5234 : : "with enumeration type %qT is deprecated",
5235 : : type0, type1);
5236 : 34 : return false;
5237 : : case SPACESHIP_EXPR:
5238 : : /* This is invalid, don't warn. */
5239 : : return false;
5240 : 38 : default:
5241 : 38 : if (enum_first_p && cxx_dialect >= cxx26)
5242 : 5 : pedwarn (loc, opt, "arithmetic between enumeration type %qT "
5243 : : "and floating-point type %qT", type0, type1);
5244 : 33 : else if (cxx_dialect >= cxx26)
5245 : 6 : pedwarn (loc, opt, "arithmetic between floating-point type %qT "
5246 : : "and enumeration type %qT", type0, type1);
5247 : 27 : else if (enum_first_p)
5248 : 12 : warning_at (loc, opt, "arithmetic between enumeration type %qT "
5249 : : "and floating-point type %qT is deprecated",
5250 : : type0, type1);
5251 : : else
5252 : 15 : warning_at (loc, opt, "arithmetic between floating-point type %qT "
5253 : : "and enumeration type %qT is deprecated",
5254 : : type0, type1);
5255 : 38 : return false;
5256 : : }
5257 : : }
5258 : : return false;
5259 : : }
5260 : :
5261 : : /* Build a binary-operation expression without default conversions.
5262 : : CODE is the kind of expression to build.
5263 : : LOCATION is the location_t of the operator in the source code.
5264 : : This function differs from `build' in several ways:
5265 : : the data type of the result is computed and recorded in it,
5266 : : warnings are generated if arg data types are invalid,
5267 : : special handling for addition and subtraction of pointers is known,
5268 : : and some optimization is done (operations on narrow ints
5269 : : are done in the narrower type when that gives the same result).
5270 : : Constant folding is also done before the result is returned.
5271 : :
5272 : : Note that the operands will never have enumeral types
5273 : : because either they have just had the default conversions performed
5274 : : or they have both just been converted to some other type in which
5275 : : the arithmetic is to be done.
5276 : :
5277 : : C++: must do special pointer arithmetic when implementing
5278 : : multiple inheritance, and deal with pointer to member functions. */
5279 : :
5280 : : tree
5281 : 124131473 : cp_build_binary_op (const op_location_t &location,
5282 : : enum tree_code code, tree orig_op0, tree orig_op1,
5283 : : tsubst_flags_t complain)
5284 : : {
5285 : 124131473 : tree op0, op1;
5286 : 124131473 : enum tree_code code0, code1;
5287 : 124131473 : tree type0, type1, orig_type0, orig_type1;
5288 : 124131473 : const char *invalid_op_diag;
5289 : :
5290 : : /* Expression code to give to the expression when it is built.
5291 : : Normally this is CODE, which is what the caller asked for,
5292 : : but in some special cases we change it. */
5293 : 124131473 : enum tree_code resultcode = code;
5294 : :
5295 : : /* Data type in which the computation is to be performed.
5296 : : In the simplest cases this is the common type of the arguments. */
5297 : 124131473 : tree result_type = NULL_TREE;
5298 : :
5299 : : /* When the computation is in excess precision, the type of the
5300 : : final EXCESS_PRECISION_EXPR. */
5301 : 124131473 : tree semantic_result_type = NULL;
5302 : :
5303 : : /* Nonzero means operands have already been type-converted
5304 : : in whatever way is necessary.
5305 : : Zero means they need to be converted to RESULT_TYPE. */
5306 : 124131473 : int converted = 0;
5307 : :
5308 : : /* Nonzero means create the expression with this type, rather than
5309 : : RESULT_TYPE. */
5310 : 124131473 : tree build_type = 0;
5311 : :
5312 : : /* Nonzero means after finally constructing the expression
5313 : : convert it to this type. */
5314 : 124131473 : tree final_type = 0;
5315 : :
5316 : 124131473 : tree result;
5317 : :
5318 : : /* Nonzero if this is an operation like MIN or MAX which can
5319 : : safely be computed in short if both args are promoted shorts.
5320 : : Also implies COMMON.
5321 : : -1 indicates a bitwise operation; this makes a difference
5322 : : in the exact conditions for when it is safe to do the operation
5323 : : in a narrower mode. */
5324 : 124131473 : int shorten = 0;
5325 : :
5326 : : /* Nonzero if this is a comparison operation;
5327 : : if both args are promoted shorts, compare the original shorts.
5328 : : Also implies COMMON. */
5329 : 124131473 : int short_compare = 0;
5330 : :
5331 : : /* Nonzero if this is a right-shift operation, which can be computed on the
5332 : : original short and then promoted if the operand is a promoted short. */
5333 : 124131473 : int short_shift = 0;
5334 : :
5335 : : /* Nonzero means set RESULT_TYPE to the common type of the args. */
5336 : 124131473 : int common = 0;
5337 : :
5338 : : /* True if both operands have arithmetic type. */
5339 : 124131473 : bool arithmetic_types_p;
5340 : :
5341 : : /* Remember whether we're doing / or %. */
5342 : 124131473 : bool doing_div_or_mod = false;
5343 : :
5344 : : /* Remember whether we're doing << or >>. */
5345 : 124131473 : bool doing_shift = false;
5346 : :
5347 : : /* Tree holding instrumentation expression. */
5348 : 124131473 : tree instrument_expr = NULL_TREE;
5349 : :
5350 : : /* True means this is an arithmetic operation that may need excess
5351 : : precision. */
5352 : 124131473 : bool may_need_excess_precision;
5353 : :
5354 : : /* Apply default conversions. */
5355 : 124131473 : op0 = resolve_nondeduced_context (orig_op0, complain);
5356 : 124131473 : op1 = resolve_nondeduced_context (orig_op1, complain);
5357 : :
5358 : 124131473 : if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5359 : 112868770 : || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5360 : 110123407 : || code == TRUTH_XOR_EXPR)
5361 : : {
5362 : 14008066 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5363 : 14008048 : op0 = decay_conversion (op0, complain);
5364 : 14008066 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5365 : 14008066 : op1 = decay_conversion (op1, complain);
5366 : : }
5367 : : else
5368 : : {
5369 : 110123407 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5370 : 110123392 : op0 = cp_default_conversion (op0, complain);
5371 : 110123407 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5372 : 110123404 : op1 = cp_default_conversion (op1, complain);
5373 : : }
5374 : :
5375 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5376 : 131597067 : STRIP_TYPE_NOPS (op0);
5377 : 152132841 : STRIP_TYPE_NOPS (op1);
5378 : :
5379 : : /* DTRT if one side is an overloaded function, but complain about it. */
5380 : 124131473 : if (type_unknown_p (op0))
5381 : : {
5382 : 9 : tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5383 : 9 : if (t != error_mark_node)
5384 : : {
5385 : 0 : if (complain & tf_error)
5386 : 0 : permerror (location,
5387 : : "assuming cast to type %qT from overloaded function",
5388 : 0 : TREE_TYPE (t));
5389 : : op0 = t;
5390 : : }
5391 : : }
5392 : 124131473 : if (type_unknown_p (op1))
5393 : : {
5394 : 3 : tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5395 : 3 : if (t != error_mark_node)
5396 : : {
5397 : 3 : if (complain & tf_error)
5398 : 3 : permerror (location,
5399 : : "assuming cast to type %qT from overloaded function",
5400 : 3 : TREE_TYPE (t));
5401 : : op1 = t;
5402 : : }
5403 : : }
5404 : :
5405 : 124131473 : orig_type0 = type0 = TREE_TYPE (op0);
5406 : 124131473 : orig_type1 = type1 = TREE_TYPE (op1);
5407 : 124131473 : tree non_ep_op0 = op0;
5408 : 124131473 : tree non_ep_op1 = op1;
5409 : :
5410 : : /* The expression codes of the data types of the arguments tell us
5411 : : whether the arguments are integers, floating, pointers, etc. */
5412 : 124131473 : code0 = TREE_CODE (type0);
5413 : 124131473 : code1 = TREE_CODE (type1);
5414 : :
5415 : : /* If an error was already reported for one of the arguments,
5416 : : avoid reporting another error. */
5417 : 124131473 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5418 : 100 : return error_mark_node;
5419 : :
5420 : 248262746 : if ((invalid_op_diag
5421 : 124131373 : = targetm.invalid_binary_op (code, type0, type1)))
5422 : : {
5423 : 0 : if (complain & tf_error)
5424 : : {
5425 : 0 : if (code0 == REAL_TYPE
5426 : 0 : && code1 == REAL_TYPE
5427 : 0 : && (extended_float_type_p (type0)
5428 : 0 : || extended_float_type_p (type1))
5429 : 0 : && cp_compare_floating_point_conversion_ranks (type0,
5430 : : type1) == 3)
5431 : : {
5432 : 0 : rich_location richloc (line_table, location);
5433 : 0 : binary_op_error (&richloc, code, type0, type1);
5434 : 0 : }
5435 : : else
5436 : 0 : error (invalid_op_diag);
5437 : : }
5438 : 0 : return error_mark_node;
5439 : : }
5440 : :
5441 : 124131373 : switch (code)
5442 : : {
5443 : : case PLUS_EXPR:
5444 : : case MINUS_EXPR:
5445 : : case MULT_EXPR:
5446 : : case TRUNC_DIV_EXPR:
5447 : : case CEIL_DIV_EXPR:
5448 : : case FLOOR_DIV_EXPR:
5449 : : case ROUND_DIV_EXPR:
5450 : : case EXACT_DIV_EXPR:
5451 : : may_need_excess_precision = true;
5452 : : break;
5453 : 41352523 : case EQ_EXPR:
5454 : 41352523 : case NE_EXPR:
5455 : 41352523 : case LE_EXPR:
5456 : 41352523 : case GE_EXPR:
5457 : 41352523 : case LT_EXPR:
5458 : 41352523 : case GT_EXPR:
5459 : 41352523 : case SPACESHIP_EXPR:
5460 : : /* Excess precision for implicit conversions of integers to
5461 : : floating point. */
5462 : 8866693 : may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5463 : 50213193 : || ANY_INTEGRAL_TYPE_P (type1));
5464 : : break;
5465 : : default:
5466 : 124131373 : may_need_excess_precision = false;
5467 : : break;
5468 : : }
5469 : 124131373 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5470 : : {
5471 : 21456 : op0 = TREE_OPERAND (op0, 0);
5472 : 21456 : type0 = TREE_TYPE (op0);
5473 : : }
5474 : 124109917 : else if (may_need_excess_precision
5475 : 94553563 : && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5476 : 28686474 : if (tree eptype = excess_precision_type (type0))
5477 : : {
5478 : 57541 : type0 = eptype;
5479 : 57541 : op0 = convert (eptype, op0);
5480 : : }
5481 : 124131373 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5482 : : {
5483 : 27174 : op1 = TREE_OPERAND (op1, 0);
5484 : 27174 : type1 = TREE_TYPE (op1);
5485 : : }
5486 : 124104199 : else if (may_need_excess_precision
5487 : 94550926 : && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5488 : 28609865 : if (tree eptype = excess_precision_type (type1))
5489 : : {
5490 : 37677 : type1 = eptype;
5491 : 37677 : op1 = convert (eptype, op1);
5492 : : }
5493 : :
5494 : : /* Issue warnings about peculiar, but valid, uses of NULL. */
5495 : 248262661 : if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5496 : : /* It's reasonable to use pointer values as operands of &&
5497 : : and ||, so NULL is no exception. */
5498 : 12715 : && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5499 : 12700 : && ( /* Both are NULL (or 0) and the operation was not a
5500 : : comparison or a pointer subtraction. */
5501 : 12773 : (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5502 : 27 : && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5503 : : /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5504 : 12688 : || (!null_ptr_cst_p (orig_op0)
5505 : 12627 : && !TYPE_PTR_OR_PTRMEM_P (type0))
5506 : 12676 : || (!null_ptr_cst_p (orig_op1)
5507 : 46 : && !TYPE_PTR_OR_PTRMEM_P (type1)))
5508 : 124131412 : && (complain & tf_warning))
5509 : : {
5510 : 39 : location_t loc =
5511 : 39 : expansion_point_location_if_in_system_header (input_location);
5512 : :
5513 : 39 : warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5514 : : }
5515 : :
5516 : : /* In case when one of the operands of the binary operation is
5517 : : a vector and another is a scalar -- convert scalar to vector. */
5518 : 124188603 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5519 : 124187779 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5520 : : {
5521 : 909 : enum stv_conv convert_flag
5522 : 909 : = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5523 : : complain & tf_error);
5524 : :
5525 : 909 : switch (convert_flag)
5526 : : {
5527 : 21 : case stv_error:
5528 : 21 : return error_mark_node;
5529 : 55 : case stv_firstarg:
5530 : 55 : {
5531 : 55 : op0 = convert (TREE_TYPE (type1), op0);
5532 : 55 : op0 = cp_save_expr (op0);
5533 : 55 : op0 = build_vector_from_val (type1, op0);
5534 : 55 : orig_type0 = type0 = TREE_TYPE (op0);
5535 : 55 : code0 = TREE_CODE (type0);
5536 : 55 : converted = 1;
5537 : 55 : break;
5538 : : }
5539 : 728 : case stv_secondarg:
5540 : 728 : {
5541 : 728 : op1 = convert (TREE_TYPE (type0), op1);
5542 : 728 : op1 = cp_save_expr (op1);
5543 : 728 : op1 = build_vector_from_val (type0, op1);
5544 : 728 : orig_type1 = type1 = TREE_TYPE (op1);
5545 : 728 : code1 = TREE_CODE (type1);
5546 : 728 : converted = 1;
5547 : 728 : break;
5548 : : }
5549 : : default:
5550 : : break;
5551 : : }
5552 : : }
5553 : :
5554 : 124131352 : switch (code)
5555 : : {
5556 : 14468502 : case MINUS_EXPR:
5557 : : /* Subtraction of two similar pointers.
5558 : : We must subtract them as integers, then divide by object size. */
5559 : 14468502 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5560 : 15728753 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5561 : 1260251 : TREE_TYPE (type1)))
5562 : : {
5563 : 1260249 : result = pointer_diff (location, op0, op1,
5564 : : common_pointer_type (type0, type1), complain,
5565 : : &instrument_expr);
5566 : 1260249 : if (instrument_expr != NULL)
5567 : 84 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5568 : : instrument_expr, result);
5569 : :
5570 : 1260249 : return result;
5571 : : }
5572 : : /* In all other cases except pointer - int, the usual arithmetic
5573 : : rules apply. */
5574 : 13208253 : else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5575 : : {
5576 : : common = 1;
5577 : : break;
5578 : : }
5579 : : /* The pointer - int case is just like pointer + int; fall
5580 : : through. */
5581 : 19118757 : gcc_fallthrough ();
5582 : 19118757 : case PLUS_EXPR:
5583 : 19118757 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5584 : 6282221 : && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5585 : : {
5586 : 6282212 : tree ptr_operand;
5587 : 6282212 : tree int_operand;
5588 : 6282212 : ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5589 : 19867 : int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5590 : 6282212 : if (processing_template_decl)
5591 : : {
5592 : 2047731 : result_type = TREE_TYPE (ptr_operand);
5593 : 2047731 : break;
5594 : : }
5595 : 4234481 : return cp_pointer_int_sum (location, code,
5596 : : ptr_operand,
5597 : : int_operand,
5598 : 4234481 : complain);
5599 : : }
5600 : : common = 1;
5601 : : break;
5602 : :
5603 : : case MULT_EXPR:
5604 : 118603647 : common = 1;
5605 : : break;
5606 : :
5607 : 9611206 : case TRUNC_DIV_EXPR:
5608 : 9611206 : case CEIL_DIV_EXPR:
5609 : 9611206 : case FLOOR_DIV_EXPR:
5610 : 9611206 : case ROUND_DIV_EXPR:
5611 : 9611206 : case EXACT_DIV_EXPR:
5612 : 9611206 : if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5613 : : {
5614 : 102553 : tree type0 = TREE_OPERAND (op0, 0);
5615 : 102553 : tree type1 = TREE_OPERAND (op1, 0);
5616 : 102553 : tree first_arg = tree_strip_any_location_wrapper (type0);
5617 : 102553 : if (!TYPE_P (type0))
5618 : 82452 : type0 = TREE_TYPE (type0);
5619 : 102553 : if (!TYPE_P (type1))
5620 : 46951 : type1 = TREE_TYPE (type1);
5621 : 102553 : if (type0
5622 : 102553 : && type1
5623 : 82042 : && INDIRECT_TYPE_P (type0)
5624 : 102628 : && same_type_p (TREE_TYPE (type0), type1))
5625 : : {
5626 : 27 : if (!(TREE_CODE (first_arg) == PARM_DECL
5627 : 21 : && DECL_ARRAY_PARAMETER_P (first_arg)
5628 : 3 : && warn_sizeof_array_argument)
5629 : 42 : && (complain & tf_warning))
5630 : : {
5631 : 21 : auto_diagnostic_group d;
5632 : 21 : if (warning_at (location, OPT_Wsizeof_pointer_div,
5633 : : "division %<sizeof (%T) / sizeof (%T)%> does "
5634 : : "not compute the number of array elements",
5635 : : type0, type1))
5636 : 18 : if (DECL_P (first_arg))
5637 : 18 : inform (DECL_SOURCE_LOCATION (first_arg),
5638 : : "first %<sizeof%> operand was declared here");
5639 : 21 : }
5640 : : }
5641 : 102529 : else if (!dependent_type_p (type0)
5642 : 26540 : && !dependent_type_p (type1)
5643 : 26442 : && TREE_CODE (type0) == ARRAY_TYPE
5644 : 22444 : && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5645 : : /* Set by finish_parenthesized_expr. */
5646 : 21797 : && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5647 : 124308 : && (complain & tf_warning))
5648 : 21779 : maybe_warn_sizeof_array_div (location, first_arg, type0,
5649 : : op1, non_reference (type1));
5650 : : }
5651 : :
5652 : 9611206 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5653 : : || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5654 : 9611188 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5655 : : || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5656 : : {
5657 : 9611179 : enum tree_code tcode0 = code0, tcode1 = code1;
5658 : 9611179 : doing_div_or_mod = true;
5659 : 9611179 : warn_for_div_by_zero (location, fold_for_warn (op1));
5660 : :
5661 : 9611179 : if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5662 : 60994 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5663 : 9611179 : if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5664 : 31221 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5665 : :
5666 : 9611179 : if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5667 : : resultcode = RDIV_EXPR;
5668 : : else
5669 : : {
5670 : : /* When dividing two signed integers, we have to promote to int.
5671 : : unless we divide by a constant != -1. Note that default
5672 : : conversion will have been performed on the operands at this
5673 : : point, so we have to dig out the original type to find out if
5674 : : it was unsigned. */
5675 : 3830922 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5676 : 3830922 : shorten = may_shorten_divmod (op0, stripped_op1);
5677 : : }
5678 : :
5679 : : common = 1;
5680 : : }
5681 : : break;
5682 : :
5683 : 2087380 : case BIT_AND_EXPR:
5684 : 2087380 : case BIT_IOR_EXPR:
5685 : 2087380 : case BIT_XOR_EXPR:
5686 : 2087380 : if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5687 : 2087380 : || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5688 : 17510 : && !VECTOR_FLOAT_TYPE_P (type0)
5689 : 17492 : && !VECTOR_FLOAT_TYPE_P (type1)))
5690 : : shorten = -1;
5691 : : break;
5692 : :
5693 : 1181693 : case TRUNC_MOD_EXPR:
5694 : 1181693 : case FLOOR_MOD_EXPR:
5695 : 1181693 : doing_div_or_mod = true;
5696 : 1181693 : warn_for_div_by_zero (location, fold_for_warn (op1));
5697 : :
5698 : 1181693 : if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5699 : 20 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5700 : 1181713 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5701 : : common = 1;
5702 : 1181673 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5703 : : {
5704 : : /* Although it would be tempting to shorten always here, that loses
5705 : : on some targets, since the modulo instruction is undefined if the
5706 : : quotient can't be represented in the computation mode. We shorten
5707 : : only if unsigned or if dividing by something we know != -1. */
5708 : 1181649 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5709 : 1181649 : shorten = may_shorten_divmod (op0, stripped_op1);
5710 : 1181649 : common = 1;
5711 : : }
5712 : : break;
5713 : :
5714 : 14008051 : case TRUTH_ANDIF_EXPR:
5715 : 14008051 : case TRUTH_ORIF_EXPR:
5716 : 14008051 : case TRUTH_AND_EXPR:
5717 : 14008051 : case TRUTH_OR_EXPR:
5718 : 14008051 : if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5719 : : {
5720 : 3 : if (!COMPARISON_CLASS_P (op1))
5721 : 3 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5722 : : build_zero_cst (type1), complain);
5723 : 3 : if (code == TRUTH_ANDIF_EXPR)
5724 : : {
5725 : 0 : tree z = build_zero_cst (TREE_TYPE (op1));
5726 : 0 : return build_conditional_expr (location, op0, op1, z, complain);
5727 : : }
5728 : 3 : else if (code == TRUTH_ORIF_EXPR)
5729 : : {
5730 : 3 : tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5731 : 3 : return build_conditional_expr (location, op0, m1, op1, complain);
5732 : : }
5733 : : else
5734 : 0 : gcc_unreachable ();
5735 : : }
5736 : 14008048 : if (gnu_vector_type_p (type0)
5737 : 14008048 : && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5738 : : {
5739 : 24 : if (!COMPARISON_CLASS_P (op0))
5740 : 24 : op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5741 : : build_zero_cst (type0), complain);
5742 : 24 : if (!VECTOR_TYPE_P (type1))
5743 : : {
5744 : 9 : tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5745 : 9 : tree z = build_zero_cst (TREE_TYPE (op0));
5746 : 9 : op1 = build_conditional_expr (location, op1, m1, z, complain);
5747 : : }
5748 : 15 : else if (!COMPARISON_CLASS_P (op1))
5749 : 15 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5750 : : build_zero_cst (type1), complain);
5751 : :
5752 : 24 : if (code == TRUTH_ANDIF_EXPR)
5753 : : code = BIT_AND_EXPR;
5754 : 9 : else if (code == TRUTH_ORIF_EXPR)
5755 : : code = BIT_IOR_EXPR;
5756 : : else
5757 : 0 : gcc_unreachable ();
5758 : :
5759 : 24 : return cp_build_binary_op (location, code, op0, op1, complain);
5760 : : }
5761 : :
5762 : 14008024 : result_type = boolean_type_node;
5763 : 14008024 : break;
5764 : :
5765 : : /* Shift operations: result has same type as first operand;
5766 : : always convert second operand to int.
5767 : : Also set SHORT_SHIFT if shifting rightward. */
5768 : :
5769 : 932742 : case RSHIFT_EXPR:
5770 : 932742 : if (gnu_vector_type_p (type0)
5771 : 170 : && code1 == INTEGER_TYPE
5772 : 932789 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5773 : : {
5774 : : result_type = type0;
5775 : : converted = 1;
5776 : : }
5777 : 932695 : else if (gnu_vector_type_p (type0)
5778 : 123 : && gnu_vector_type_p (type1)
5779 : 123 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5780 : 120 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5781 : 932815 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5782 : : TYPE_VECTOR_SUBPARTS (type1)))
5783 : : {
5784 : : result_type = type0;
5785 : : converted = 1;
5786 : : }
5787 : 932575 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5788 : : {
5789 : 932548 : tree const_op1 = fold_for_warn (op1);
5790 : 932548 : if (TREE_CODE (const_op1) != INTEGER_CST)
5791 : 74633 : const_op1 = op1;
5792 : 932548 : result_type = type0;
5793 : 932548 : doing_shift = true;
5794 : 932548 : if (TREE_CODE (const_op1) == INTEGER_CST)
5795 : : {
5796 : 857915 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5797 : : {
5798 : 48 : if ((complain & tf_warning)
5799 : 48 : && c_inhibit_evaluation_warnings == 0)
5800 : 36 : warning_at (location, OPT_Wshift_count_negative,
5801 : : "right shift count is negative");
5802 : : }
5803 : : else
5804 : : {
5805 : 857867 : if (!integer_zerop (const_op1))
5806 : 857781 : short_shift = 1;
5807 : :
5808 : 857867 : if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5809 : 46 : && (complain & tf_warning)
5810 : 857913 : && c_inhibit_evaluation_warnings == 0)
5811 : 34 : warning_at (location, OPT_Wshift_count_overflow,
5812 : : "right shift count >= width of type");
5813 : : }
5814 : : }
5815 : : /* Avoid converting op1 to result_type later. */
5816 : : converted = 1;
5817 : : }
5818 : : break;
5819 : :
5820 : 2870082 : case LSHIFT_EXPR:
5821 : 2870082 : if (gnu_vector_type_p (type0)
5822 : 183 : && code1 == INTEGER_TYPE
5823 : 2870119 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5824 : : {
5825 : : result_type = type0;
5826 : : converted = 1;
5827 : : }
5828 : 2870045 : else if (gnu_vector_type_p (type0)
5829 : 146 : && gnu_vector_type_p (type1)
5830 : 146 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5831 : 143 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5832 : 2870185 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5833 : : TYPE_VECTOR_SUBPARTS (type1)))
5834 : : {
5835 : : result_type = type0;
5836 : : converted = 1;
5837 : : }
5838 : 2869905 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5839 : : {
5840 : 2869874 : tree const_op0 = fold_for_warn (op0);
5841 : 2869874 : if (TREE_CODE (const_op0) != INTEGER_CST)
5842 : 141395 : const_op0 = op0;
5843 : 2869874 : tree const_op1 = fold_for_warn (op1);
5844 : 2869874 : if (TREE_CODE (const_op1) != INTEGER_CST)
5845 : 207576 : const_op1 = op1;
5846 : 2869874 : result_type = type0;
5847 : 2869874 : doing_shift = true;
5848 : 2869874 : if (TREE_CODE (const_op0) == INTEGER_CST
5849 : 2728479 : && tree_int_cst_sgn (const_op0) < 0
5850 : 280 : && !TYPE_OVERFLOW_WRAPS (type0)
5851 : 196 : && (complain & tf_warning)
5852 : 2870070 : && c_inhibit_evaluation_warnings == 0)
5853 : 196 : warning_at (location, OPT_Wshift_negative_value,
5854 : : "left shift of negative value");
5855 : 2869874 : if (TREE_CODE (const_op1) == INTEGER_CST)
5856 : : {
5857 : 2662298 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5858 : : {
5859 : 57 : if ((complain & tf_warning)
5860 : 57 : && c_inhibit_evaluation_warnings == 0)
5861 : 39 : warning_at (location, OPT_Wshift_count_negative,
5862 : : "left shift count is negative");
5863 : : }
5864 : 2662241 : else if (compare_tree_int (const_op1,
5865 : 2662241 : TYPE_PRECISION (type0)) >= 0)
5866 : : {
5867 : 83 : if ((complain & tf_warning)
5868 : 61 : && c_inhibit_evaluation_warnings == 0)
5869 : 45 : warning_at (location, OPT_Wshift_count_overflow,
5870 : : "left shift count >= width of type");
5871 : : }
5872 : 2662158 : else if (TREE_CODE (const_op0) == INTEGER_CST
5873 : 2542948 : && (complain & tf_warning))
5874 : 2427976 : maybe_warn_shift_overflow (location, const_op0, const_op1);
5875 : : }
5876 : : /* Avoid converting op1 to result_type later. */
5877 : : converted = 1;
5878 : : }
5879 : : break;
5880 : :
5881 : 20519216 : case EQ_EXPR:
5882 : 20519216 : case NE_EXPR:
5883 : 20519216 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5884 : 2415 : goto vector_compare;
5885 : 20516801 : if ((complain & tf_warning)
5886 : 19252637 : && c_inhibit_evaluation_warnings == 0
5887 : 39122298 : && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5888 : 828592 : warning_at (location, OPT_Wfloat_equal,
5889 : : "comparing floating-point with %<==%> "
5890 : : "or %<!=%> is unsafe");
5891 : 20516801 : {
5892 : 20516801 : tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5893 : 20516801 : tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5894 : 20516801 : if ((complain & tf_warning_or_error)
5895 : 20516801 : && ((TREE_CODE (stripped_orig_op0) == STRING_CST
5896 : 39 : && !integer_zerop (cp_fully_fold (op1)))
5897 : 19509413 : || (TREE_CODE (stripped_orig_op1) == STRING_CST
5898 : 68 : && !integer_zerop (cp_fully_fold (op0)))))
5899 : 47 : warning_at (location, OPT_Waddress,
5900 : : "comparison with string literal results in "
5901 : : "unspecified behavior");
5902 : 20516754 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5903 : 20516754 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5904 : : {
5905 : : /* P2865R5 made array comparisons ill-formed in C++26. */
5906 : 76 : if (complain & tf_warning_or_error)
5907 : 45 : do_warn_array_compare (location, code, stripped_orig_op0,
5908 : : stripped_orig_op1);
5909 : 31 : else if (cxx_dialect >= cxx26)
5910 : 13 : return error_mark_node;
5911 : : }
5912 : : }
5913 : :
5914 : 20516788 : build_type = boolean_type_node;
5915 : 20516788 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5916 : : || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5917 : 17346970 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5918 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5919 : : short_compare = 1;
5920 : 28225 : else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5921 : 3169355 : && null_ptr_cst_p (orig_op1))
5922 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5923 : 4817564 : || (code0 == POINTER_TYPE
5924 : 1593037 : && TYPE_PTR_P (type1) && integer_zerop (op1)))
5925 : : {
5926 : 1595930 : if (TYPE_PTR_P (type1))
5927 : 19984 : result_type = composite_pointer_type (location,
5928 : : type0, type1, op0, op1,
5929 : : CPO_COMPARISON, complain);
5930 : : else
5931 : : result_type = type0;
5932 : :
5933 : 1595930 : if (char_type_p (TREE_TYPE (orig_op1)))
5934 : : {
5935 : 10 : auto_diagnostic_group d;
5936 : 10 : if (warning_at (location, OPT_Wpointer_compare,
5937 : : "comparison between pointer and zero character "
5938 : : "constant"))
5939 : 10 : inform (location,
5940 : : "did you mean to dereference the pointer?");
5941 : 10 : }
5942 : 1595930 : warn_for_null_address (location, op0, complain);
5943 : : }
5944 : 900 : else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5945 : 1600321 : && null_ptr_cst_p (orig_op0))
5946 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5947 : 3200753 : || (code1 == POINTER_TYPE
5948 : 1599052 : && TYPE_PTR_P (type0) && integer_zerop (op0)))
5949 : : {
5950 : 965 : if (TYPE_PTR_P (type0))
5951 : 68 : result_type = composite_pointer_type (location,
5952 : : type0, type1, op0, op1,
5953 : : CPO_COMPARISON, complain);
5954 : : else
5955 : : result_type = type1;
5956 : :
5957 : 965 : if (char_type_p (TREE_TYPE (orig_op0)))
5958 : : {
5959 : 10 : auto_diagnostic_group d;
5960 : 10 : if (warning_at (location, OPT_Wpointer_compare,
5961 : : "comparison between pointer and zero character "
5962 : : "constant"))
5963 : 10 : inform (location,
5964 : : "did you mean to dereference the pointer?");
5965 : 10 : }
5966 : 965 : warn_for_null_address (location, op1, complain);
5967 : : }
5968 : 1599860 : else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5969 : 26933 : || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5970 : 1573299 : result_type = composite_pointer_type (location,
5971 : : type0, type1, op0, op1,
5972 : : CPO_COMPARISON, complain);
5973 : 26561 : else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5974 : : /* One of the operands must be of nullptr_t type. */
5975 : 74 : result_type = TREE_TYPE (nullptr_node);
5976 : 26487 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5977 : : {
5978 : 58 : result_type = type0;
5979 : 58 : if (complain & tf_error)
5980 : 56 : permerror (location, "ISO C++ forbids comparison between "
5981 : : "pointer and integer");
5982 : : else
5983 : 2 : return error_mark_node;
5984 : : }
5985 : 26429 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5986 : : {
5987 : 26057 : result_type = type1;
5988 : 26057 : if (complain & tf_error)
5989 : 69 : permerror (location, "ISO C++ forbids comparison between "
5990 : : "pointer and integer");
5991 : : else
5992 : 25988 : return error_mark_node;
5993 : : }
5994 : 372 : else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5995 : : {
5996 : 200 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5997 : : == ptrmemfunc_vbit_in_delta)
5998 : : {
5999 : : tree pfn0, delta0, e1, e2;
6000 : :
6001 : : if (TREE_SIDE_EFFECTS (op0))
6002 : : op0 = cp_save_expr (op0);
6003 : :
6004 : : pfn0 = pfn_from_ptrmemfunc (op0);
6005 : : delta0 = delta_from_ptrmemfunc (op0);
6006 : : {
6007 : : /* If we will warn below about a null-address compare
6008 : : involving the orig_op0 ptrmemfunc, we'd likely also
6009 : : warn about the pfn0's null-address compare, and
6010 : : that would be redundant, so suppress it. */
6011 : : warning_sentinel ws (warn_address);
6012 : : e1 = cp_build_binary_op (location,
6013 : : EQ_EXPR,
6014 : : pfn0,
6015 : : build_zero_cst (TREE_TYPE (pfn0)),
6016 : : complain);
6017 : : }
6018 : : e2 = cp_build_binary_op (location,
6019 : : BIT_AND_EXPR,
6020 : : delta0,
6021 : : integer_one_node,
6022 : : complain);
6023 : :
6024 : : if (complain & tf_warning)
6025 : : maybe_warn_zero_as_null_pointer_constant (op1, input_location);
6026 : :
6027 : : e2 = cp_build_binary_op (location,
6028 : : EQ_EXPR, e2, integer_zero_node,
6029 : : complain);
6030 : : op0 = cp_build_binary_op (location,
6031 : : TRUTH_ANDIF_EXPR, e1, e2,
6032 : : complain);
6033 : : op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
6034 : : }
6035 : : else
6036 : : {
6037 : 200 : op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
6038 : 200 : op1 = cp_convert (TREE_TYPE (op0), op1, complain);
6039 : : }
6040 : 200 : result_type = TREE_TYPE (op0);
6041 : :
6042 : 200 : warn_for_null_address (location, orig_op0, complain);
6043 : : }
6044 : 172 : else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
6045 : 36 : return cp_build_binary_op (location, code, op1, op0, complain);
6046 : 136 : else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
6047 : : {
6048 : 115 : tree type;
6049 : : /* E will be the final comparison. */
6050 : 115 : tree e;
6051 : : /* E1 and E2 are for scratch. */
6052 : 115 : tree e1;
6053 : 115 : tree e2;
6054 : 115 : tree pfn0;
6055 : 115 : tree pfn1;
6056 : 115 : tree delta0;
6057 : 115 : tree delta1;
6058 : :
6059 : 115 : type = composite_pointer_type (location, type0, type1, op0, op1,
6060 : : CPO_COMPARISON, complain);
6061 : :
6062 : 115 : if (!same_type_p (TREE_TYPE (op0), type))
6063 : 6 : op0 = cp_convert_and_check (type, op0, complain);
6064 : 115 : if (!same_type_p (TREE_TYPE (op1), type))
6065 : 9 : op1 = cp_convert_and_check (type, op1, complain);
6066 : :
6067 : 115 : if (op0 == error_mark_node || op1 == error_mark_node)
6068 : : return error_mark_node;
6069 : :
6070 : 112 : if (TREE_SIDE_EFFECTS (op0))
6071 : 53 : op0 = cp_save_expr (op0);
6072 : 112 : if (TREE_SIDE_EFFECTS (op1))
6073 : 3 : op1 = cp_save_expr (op1);
6074 : :
6075 : 112 : pfn0 = pfn_from_ptrmemfunc (op0);
6076 : 112 : pfn0 = cp_fully_fold (pfn0);
6077 : : /* Avoid -Waddress warnings (c++/64877). */
6078 : 112 : if (TREE_CODE (pfn0) == ADDR_EXPR)
6079 : 12 : suppress_warning (pfn0, OPT_Waddress);
6080 : 112 : pfn1 = pfn_from_ptrmemfunc (op1);
6081 : 112 : pfn1 = cp_fully_fold (pfn1);
6082 : 112 : delta0 = delta_from_ptrmemfunc (op0);
6083 : 112 : delta1 = delta_from_ptrmemfunc (op1);
6084 : 112 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6085 : : == ptrmemfunc_vbit_in_delta)
6086 : : {
6087 : : /* We generate:
6088 : :
6089 : : (op0.pfn == op1.pfn
6090 : : && ((op0.delta == op1.delta)
6091 : : || (!op0.pfn && op0.delta & 1 == 0
6092 : : && op1.delta & 1 == 0))
6093 : :
6094 : : The reason for the `!op0.pfn' bit is that a NULL
6095 : : pointer-to-member is any member with a zero PFN and
6096 : : LSB of the DELTA field is 0. */
6097 : :
6098 : : e1 = cp_build_binary_op (location, BIT_AND_EXPR,
6099 : : delta0,
6100 : : integer_one_node,
6101 : : complain);
6102 : : e1 = cp_build_binary_op (location,
6103 : : EQ_EXPR, e1, integer_zero_node,
6104 : : complain);
6105 : : e2 = cp_build_binary_op (location, BIT_AND_EXPR,
6106 : : delta1,
6107 : : integer_one_node,
6108 : : complain);
6109 : : e2 = cp_build_binary_op (location,
6110 : : EQ_EXPR, e2, integer_zero_node,
6111 : : complain);
6112 : : e1 = cp_build_binary_op (location,
6113 : : TRUTH_ANDIF_EXPR, e2, e1,
6114 : : complain);
6115 : : e2 = cp_build_binary_op (location, EQ_EXPR,
6116 : : pfn0,
6117 : : build_zero_cst (TREE_TYPE (pfn0)),
6118 : : complain);
6119 : : e2 = cp_build_binary_op (location,
6120 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6121 : : e1 = cp_build_binary_op (location,
6122 : : EQ_EXPR, delta0, delta1, complain);
6123 : : e1 = cp_build_binary_op (location,
6124 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6125 : : }
6126 : : else
6127 : : {
6128 : : /* We generate:
6129 : :
6130 : : (op0.pfn == op1.pfn
6131 : : && (!op0.pfn || op0.delta == op1.delta))
6132 : :
6133 : : The reason for the `!op0.pfn' bit is that a NULL
6134 : : pointer-to-member is any member with a zero PFN; the
6135 : : DELTA field is unspecified. */
6136 : :
6137 : 112 : e1 = cp_build_binary_op (location,
6138 : : EQ_EXPR, delta0, delta1, complain);
6139 : 112 : e2 = cp_build_binary_op (location,
6140 : : EQ_EXPR,
6141 : : pfn0,
6142 : 112 : build_zero_cst (TREE_TYPE (pfn0)),
6143 : : complain);
6144 : 112 : e1 = cp_build_binary_op (location,
6145 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6146 : : }
6147 : 112 : e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
6148 : 112 : e = cp_build_binary_op (location,
6149 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6150 : 112 : if (code == EQ_EXPR)
6151 : : return e;
6152 : 30 : return cp_build_binary_op (location,
6153 : 30 : EQ_EXPR, e, integer_zero_node, complain);
6154 : : }
6155 : : else
6156 : : {
6157 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
6158 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
6159 : : type1));
6160 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
6161 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
6162 : : type0));
6163 : : }
6164 : :
6165 : : break;
6166 : :
6167 : 241 : case MAX_EXPR:
6168 : 241 : case MIN_EXPR:
6169 : 241 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6170 : 241 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6171 : : shorten = 1;
6172 : 0 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6173 : 0 : result_type = composite_pointer_type (location,
6174 : : type0, type1, op0, op1,
6175 : : CPO_COMPARISON, complain);
6176 : : break;
6177 : :
6178 : 20833307 : case LE_EXPR:
6179 : 20833307 : case GE_EXPR:
6180 : 20833307 : case LT_EXPR:
6181 : 20833307 : case GT_EXPR:
6182 : 20833307 : case SPACESHIP_EXPR:
6183 : 20833307 : if (TREE_CODE (orig_op0) == STRING_CST
6184 : 20833307 : || TREE_CODE (orig_op1) == STRING_CST)
6185 : : {
6186 : 0 : if (complain & tf_warning)
6187 : 0 : warning_at (location, OPT_Waddress,
6188 : : "comparison with string literal results "
6189 : : "in unspecified behavior");
6190 : : }
6191 : 20833307 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6192 : 153 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6193 : 20833411 : && code != SPACESHIP_EXPR)
6194 : : {
6195 : 88 : if (complain & tf_warning_or_error)
6196 : 51 : do_warn_array_compare (location, code,
6197 : : tree_strip_any_location_wrapper (orig_op0),
6198 : : tree_strip_any_location_wrapper (orig_op1));
6199 : 37 : else if (cxx_dialect >= cxx26)
6200 : 1 : return error_mark_node;
6201 : : }
6202 : :
6203 : 20833306 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6204 : : {
6205 : 6602 : vector_compare:
6206 : 6602 : tree intt;
6207 : 6602 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6208 : 6602 : TREE_TYPE (type1))
6209 : 6602 : && !vector_types_compatible_elements_p (type0, type1))
6210 : : {
6211 : 3 : if (complain & tf_error)
6212 : : {
6213 : 3 : auto_diagnostic_group d;
6214 : 3 : error_at (location, "comparing vectors with different "
6215 : : "element types");
6216 : 3 : inform (location, "operand types are %qT and %qT",
6217 : : type0, type1);
6218 : 3 : }
6219 : 3 : return error_mark_node;
6220 : : }
6221 : :
6222 : 6599 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
6223 : 13198 : TYPE_VECTOR_SUBPARTS (type1)))
6224 : : {
6225 : 3 : if (complain & tf_error)
6226 : : {
6227 : 3 : auto_diagnostic_group d;
6228 : 3 : error_at (location, "comparing vectors with different "
6229 : : "number of elements");
6230 : 3 : inform (location, "operand types are %qT and %qT",
6231 : : type0, type1);
6232 : 3 : }
6233 : 3 : return error_mark_node;
6234 : : }
6235 : :
6236 : : /* It's not precisely specified how the usual arithmetic
6237 : : conversions apply to the vector types. Here, we use
6238 : : the unsigned type if one of the operands is signed and
6239 : : the other one is unsigned. */
6240 : 6596 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6241 : : {
6242 : 36 : if (!TYPE_UNSIGNED (type0))
6243 : 36 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6244 : : else
6245 : 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6246 : 36 : warning_at (location, OPT_Wsign_compare, "comparison between "
6247 : : "types %qT and %qT", type0, type1);
6248 : : }
6249 : :
6250 : 6596 : if (resultcode == SPACESHIP_EXPR)
6251 : : {
6252 : 3 : if (complain & tf_error)
6253 : 3 : sorry_at (location, "three-way comparison of vectors");
6254 : 3 : return error_mark_node;
6255 : : }
6256 : :
6257 : : /* Always construct signed integer vector type. */
6258 : 6593 : intt = c_common_type_for_size
6259 : 13186 : (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
6260 : 6593 : if (!intt)
6261 : : {
6262 : 0 : if (complain & tf_error)
6263 : 0 : error_at (location, "could not find an integer type "
6264 : 0 : "of the same size as %qT", TREE_TYPE (type0));
6265 : 0 : return error_mark_node;
6266 : : }
6267 : 6593 : result_type = build_opaque_vector_type (intt,
6268 : 6593 : TYPE_VECTOR_SUBPARTS (type0));
6269 : 6593 : return build_vec_cmp (resultcode, result_type, op0, op1);
6270 : : }
6271 : 20829119 : build_type = boolean_type_node;
6272 : 20829119 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6273 : : || code0 == ENUMERAL_TYPE)
6274 : 19761128 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6275 : : || code1 == ENUMERAL_TYPE))
6276 : : short_compare = 1;
6277 : 1068078 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6278 : 1067846 : result_type = composite_pointer_type (location,
6279 : : type0, type1, op0, op1,
6280 : : CPO_COMPARISON, complain);
6281 : 74 : else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6282 : 160 : || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6283 : 338 : || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6284 : : {
6285 : : /* Core Issue 1512 made this ill-formed. */
6286 : 191 : if (complain & tf_error)
6287 : 188 : error_at (location, "ordered comparison of pointer with "
6288 : : "integer zero (%qT and %qT)", type0, type1);
6289 : 191 : return error_mark_node;
6290 : : }
6291 : 41 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6292 : : {
6293 : 0 : result_type = type0;
6294 : 0 : if (complain & tf_error)
6295 : 0 : permerror (location, "ISO C++ forbids comparison between "
6296 : : "pointer and integer");
6297 : : else
6298 : 0 : return error_mark_node;
6299 : : }
6300 : 41 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6301 : : {
6302 : 24 : result_type = type1;
6303 : 24 : if (complain & tf_error)
6304 : 24 : permerror (location, "ISO C++ forbids comparison between "
6305 : : "pointer and integer");
6306 : : else
6307 : 0 : return error_mark_node;
6308 : : }
6309 : :
6310 : 20828928 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6311 : 1067872 : && !processing_template_decl
6312 : 21678387 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6313 : : {
6314 : 49 : op0 = cp_save_expr (op0);
6315 : 49 : op1 = cp_save_expr (op1);
6316 : :
6317 : 49 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6318 : 49 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6319 : : }
6320 : :
6321 : : break;
6322 : :
6323 : 0 : case UNORDERED_EXPR:
6324 : 0 : case ORDERED_EXPR:
6325 : 0 : case UNLT_EXPR:
6326 : 0 : case UNLE_EXPR:
6327 : 0 : case UNGT_EXPR:
6328 : 0 : case UNGE_EXPR:
6329 : 0 : case UNEQ_EXPR:
6330 : 0 : build_type = integer_type_node;
6331 : 0 : if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6332 : : {
6333 : 0 : if (complain & tf_error)
6334 : 0 : error ("unordered comparison on non-floating-point argument");
6335 : 0 : return error_mark_node;
6336 : : }
6337 : : common = 1;
6338 : : break;
6339 : :
6340 : : default:
6341 : : break;
6342 : : }
6343 : :
6344 : 118603647 : if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6345 : : || code0 == ENUMERAL_TYPE)
6346 : 98672174 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6347 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6348 : : arithmetic_types_p = 1;
6349 : : else
6350 : : {
6351 : 20163286 : arithmetic_types_p = 0;
6352 : : /* Vector arithmetic is only allowed when both sides are vectors. */
6353 : 20163286 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6354 : : {
6355 : 50572 : if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6356 : 50572 : || !vector_types_compatible_elements_p (type0, type1))
6357 : : {
6358 : 18 : if (complain & tf_error)
6359 : : {
6360 : : /* "location" already embeds the locations of the
6361 : : operands, so we don't need to add them separately
6362 : : to richloc. */
6363 : 18 : rich_location richloc (line_table, location);
6364 : 18 : binary_op_error (&richloc, code, type0, type1);
6365 : 18 : }
6366 : 18 : return error_mark_node;
6367 : : }
6368 : : arithmetic_types_p = 1;
6369 : : }
6370 : : }
6371 : : /* Determine the RESULT_TYPE, if it is not already known. */
6372 : 118603629 : if (!result_type
6373 : 118603629 : && arithmetic_types_p
6374 : 94506309 : && (shorten || common || short_compare))
6375 : : {
6376 : 94506232 : result_type = cp_common_type (type0, type1);
6377 : 94506232 : if (result_type == error_mark_node)
6378 : : {
6379 : 0 : tree t1 = type0;
6380 : 0 : tree t2 = type1;
6381 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6382 : 0 : t1 = TREE_TYPE (t1);
6383 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6384 : 0 : t2 = TREE_TYPE (t2);
6385 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6386 : : && TREE_CODE (t2) == REAL_TYPE
6387 : : && (extended_float_type_p (t1)
6388 : : || extended_float_type_p (t2))
6389 : : && cp_compare_floating_point_conversion_ranks
6390 : : (t1, t2) == 3);
6391 : 0 : if (complain & tf_error)
6392 : : {
6393 : 0 : rich_location richloc (line_table, location);
6394 : 0 : binary_op_error (&richloc, code, type0, type1);
6395 : 0 : }
6396 : 0 : return error_mark_node;
6397 : : }
6398 : 94506232 : if (complain & tf_warning)
6399 : 89668946 : do_warn_double_promotion (result_type, type0, type1,
6400 : : "implicit conversion from %qH to %qI "
6401 : : "to match other operand of binary "
6402 : : "expression", location);
6403 : 94506232 : if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6404 : 94506232 : TREE_TYPE (orig_op1), complain))
6405 : 6 : return error_mark_node;
6406 : : }
6407 : 118603623 : if (may_need_excess_precision
6408 : 89047890 : && (orig_type0 != type0 || orig_type1 != type1)
6409 : 81895 : && build_type == NULL_TREE
6410 : 81895 : && result_type)
6411 : : {
6412 : 61696 : gcc_assert (common);
6413 : 61696 : semantic_result_type = cp_common_type (orig_type0, orig_type1);
6414 : 61696 : if (semantic_result_type == error_mark_node)
6415 : : {
6416 : 0 : tree t1 = orig_type0;
6417 : 0 : tree t2 = orig_type1;
6418 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6419 : 0 : t1 = TREE_TYPE (t1);
6420 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6421 : 0 : t2 = TREE_TYPE (t2);
6422 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6423 : : && TREE_CODE (t2) == REAL_TYPE
6424 : : && (extended_float_type_p (t1)
6425 : : || extended_float_type_p (t2))
6426 : : && cp_compare_floating_point_conversion_ranks
6427 : : (t1, t2) == 3);
6428 : 0 : if (complain & tf_error)
6429 : : {
6430 : 0 : rich_location richloc (line_table, location);
6431 : 0 : binary_op_error (&richloc, code, type0, type1);
6432 : 0 : }
6433 : 0 : return error_mark_node;
6434 : : }
6435 : : }
6436 : :
6437 : 118603623 : if (code == SPACESHIP_EXPR)
6438 : : {
6439 : 184286 : iloc_sentinel s (location);
6440 : :
6441 : 184286 : tree orig_type0 = TREE_TYPE (orig_op0);
6442 : 184286 : tree_code orig_code0 = TREE_CODE (orig_type0);
6443 : 184286 : tree orig_type1 = TREE_TYPE (orig_op1);
6444 : 184286 : tree_code orig_code1 = TREE_CODE (orig_type1);
6445 : 184286 : if (!result_type || result_type == error_mark_node)
6446 : : /* Nope. */
6447 : : result_type = NULL_TREE;
6448 : 184265 : else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6449 : : /* "If one of the operands is of type bool and the other is not, the
6450 : : program is ill-formed." */
6451 : : result_type = NULL_TREE;
6452 : 184262 : else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6453 : 16 : && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6454 : : /* We only do array/function-to-pointer conversion if "at least one of
6455 : : the operands is of pointer type". */
6456 : : result_type = NULL_TREE;
6457 : 184246 : else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6458 : : /* <=> no longer supports equality relations. */
6459 : : result_type = NULL_TREE;
6460 : 184243 : else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6461 : 184279 : && !(same_type_ignoring_top_level_qualifiers_p
6462 : 36 : (orig_type0, orig_type1)))
6463 : : /* "If both operands have arithmetic types, or one operand has integral
6464 : : type and the other operand has unscoped enumeration type, the usual
6465 : : arithmetic conversions are applied to the operands." So we don't do
6466 : : arithmetic conversions if the operands both have enumeral type. */
6467 : : result_type = NULL_TREE;
6468 : 184228 : else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6469 : 184222 : || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6470 : : /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6471 : : [where one is of enumeration type and the other is of a different
6472 : : enumeration type or a floating-point type] are ill-formed. */
6473 : : result_type = NULL_TREE;
6474 : :
6475 : 184216 : if (result_type)
6476 : : {
6477 : 184216 : build_type = spaceship_type (result_type, complain);
6478 : 184216 : if (build_type == error_mark_node)
6479 : : return error_mark_node;
6480 : : }
6481 : :
6482 : 184271 : if (result_type && arithmetic_types_p)
6483 : : {
6484 : : /* If a narrowing conversion is required, other than from an integral
6485 : : type to a floating point type, the program is ill-formed. */
6486 : 75323 : bool ok = true;
6487 : 75323 : if (TREE_CODE (result_type) == REAL_TYPE
6488 : 670 : && CP_INTEGRAL_TYPE_P (orig_type0))
6489 : : /* OK */;
6490 : 75312 : else if (!check_narrowing (result_type, orig_op0, complain))
6491 : 75323 : ok = false;
6492 : 75323 : if (TREE_CODE (result_type) == REAL_TYPE
6493 : 670 : && CP_INTEGRAL_TYPE_P (orig_type1))
6494 : : /* OK */;
6495 : 75312 : else if (!check_narrowing (result_type, orig_op1, complain))
6496 : : ok = false;
6497 : 75323 : if (!ok && !(complain & tf_error))
6498 : 4 : return error_mark_node;
6499 : : }
6500 : 184286 : }
6501 : :
6502 : 118603604 : if (!result_type)
6503 : : {
6504 : 477 : if (complain & tf_error)
6505 : : {
6506 : 206 : binary_op_rich_location richloc (location,
6507 : 206 : orig_op0, orig_op1, true);
6508 : 206 : error_at (&richloc,
6509 : : "invalid operands of types %qT and %qT to binary %qO",
6510 : 206 : TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6511 : 206 : }
6512 : 477 : return error_mark_node;
6513 : : }
6514 : :
6515 : : /* If we're in a template, the only thing we need to know is the
6516 : : RESULT_TYPE. */
6517 : 118603127 : if (processing_template_decl)
6518 : : {
6519 : : /* Since the middle-end checks the type when doing a build2, we
6520 : : need to build the tree in pieces. This built tree will never
6521 : : get out of the front-end as we replace it when instantiating
6522 : : the template. */
6523 : 52661237 : tree tmp = build2 (resultcode,
6524 : : build_type ? build_type : result_type,
6525 : : NULL_TREE, op1);
6526 : 33051367 : TREE_OPERAND (tmp, 0) = op0;
6527 : 33051367 : if (semantic_result_type)
6528 : 0 : tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6529 : 33051367 : return tmp;
6530 : : }
6531 : :
6532 : : /* Remember the original type; RESULT_TYPE might be changed later on
6533 : : by shorten_binary_op. */
6534 : 85551760 : tree orig_type = result_type;
6535 : :
6536 : 85551760 : if (arithmetic_types_p)
6537 : : {
6538 : 74566399 : bool first_complex = (code0 == COMPLEX_TYPE);
6539 : 74566399 : bool second_complex = (code1 == COMPLEX_TYPE);
6540 : 74566399 : int none_complex = (!first_complex && !second_complex);
6541 : :
6542 : : /* Adapted from patch for c/24581. */
6543 : 74566399 : if (first_complex != second_complex
6544 : 165830 : && (code == PLUS_EXPR
6545 : : || code == MINUS_EXPR
6546 : 165830 : || code == MULT_EXPR
6547 : 43286 : || (code == TRUNC_DIV_EXPR && first_complex))
6548 : 152323 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6549 : 74718561 : && flag_signed_zeros)
6550 : : {
6551 : : /* An operation on mixed real/complex operands must be
6552 : : handled specially, but the language-independent code can
6553 : : more easily optimize the plain complex arithmetic if
6554 : : -fno-signed-zeros. */
6555 : 152162 : tree real_type = TREE_TYPE (result_type);
6556 : 152162 : tree real, imag;
6557 : 152162 : if (first_complex)
6558 : : {
6559 : 119149 : if (TREE_TYPE (op0) != result_type)
6560 : 6 : op0 = cp_convert_and_check (result_type, op0, complain);
6561 : 119149 : if (TREE_TYPE (op1) != real_type)
6562 : 26 : op1 = cp_convert_and_check (real_type, op1, complain);
6563 : : }
6564 : : else
6565 : : {
6566 : 33013 : if (TREE_TYPE (op0) != real_type)
6567 : 31 : op0 = cp_convert_and_check (real_type, op0, complain);
6568 : 33013 : if (TREE_TYPE (op1) != result_type)
6569 : 32 : op1 = cp_convert_and_check (result_type, op1, complain);
6570 : : }
6571 : 152162 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6572 : 0 : return error_mark_node;
6573 : 152162 : if (first_complex)
6574 : : {
6575 : 119149 : op0 = cp_save_expr (op0);
6576 : 119149 : real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6577 : 119149 : imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6578 : 119149 : switch (code)
6579 : : {
6580 : 59551 : case MULT_EXPR:
6581 : 59551 : case TRUNC_DIV_EXPR:
6582 : 59551 : op1 = cp_save_expr (op1);
6583 : 59551 : imag = build2 (resultcode, real_type, imag, op1);
6584 : : /* Fall through. */
6585 : 119149 : case PLUS_EXPR:
6586 : 119149 : case MINUS_EXPR:
6587 : 119149 : real = build2 (resultcode, real_type, real, op1);
6588 : 119149 : break;
6589 : 0 : default:
6590 : 0 : gcc_unreachable();
6591 : : }
6592 : : }
6593 : : else
6594 : : {
6595 : 33013 : op1 = cp_save_expr (op1);
6596 : 33013 : real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6597 : 33013 : imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6598 : 33013 : switch (code)
6599 : : {
6600 : 638 : case MULT_EXPR:
6601 : 638 : op0 = cp_save_expr (op0);
6602 : 638 : imag = build2 (resultcode, real_type, op0, imag);
6603 : : /* Fall through. */
6604 : 17266 : case PLUS_EXPR:
6605 : 17266 : real = build2 (resultcode, real_type, op0, real);
6606 : 17266 : break;
6607 : 15747 : case MINUS_EXPR:
6608 : 15747 : real = build2 (resultcode, real_type, op0, real);
6609 : 15747 : imag = build1 (NEGATE_EXPR, real_type, imag);
6610 : 15747 : break;
6611 : 0 : default:
6612 : 0 : gcc_unreachable();
6613 : : }
6614 : : }
6615 : 152162 : result = build2 (COMPLEX_EXPR, result_type, real, imag);
6616 : 152162 : if (semantic_result_type)
6617 : 24 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6618 : : result);
6619 : 152162 : return result;
6620 : : }
6621 : :
6622 : : /* For certain operations (which identify themselves by shorten != 0)
6623 : : if both args were extended from the same smaller type,
6624 : : do the arithmetic in that type and then extend.
6625 : :
6626 : : shorten !=0 and !=1 indicates a bitwise operation.
6627 : : For them, this optimization is safe only if
6628 : : both args are zero-extended or both are sign-extended.
6629 : : Otherwise, we might change the result.
6630 : : E.g., (short)-1 | (unsigned short)-1 is (int)-1
6631 : : but calculated in (unsigned short) it would be (unsigned short)-1. */
6632 : :
6633 : 74414237 : if (shorten && none_complex)
6634 : : {
6635 : 3980192 : final_type = result_type;
6636 : 3980192 : result_type = shorten_binary_op (result_type, op0, op1,
6637 : : shorten == -1);
6638 : : }
6639 : :
6640 : : /* Shifts can be shortened if shifting right. */
6641 : :
6642 : 74414237 : if (short_shift)
6643 : : {
6644 : 763497 : int unsigned_arg;
6645 : 763497 : tree arg0 = get_narrower (op0, &unsigned_arg);
6646 : : /* We're not really warning here but when we set short_shift we
6647 : : used fold_for_warn to fold the operand. */
6648 : 763497 : tree const_op1 = fold_for_warn (op1);
6649 : :
6650 : 763497 : final_type = result_type;
6651 : :
6652 : 763497 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
6653 : 705787 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6654 : :
6655 : 763497 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6656 : 3666 : && tree_int_cst_sgn (const_op1) > 0
6657 : : /* We can shorten only if the shift count is less than the
6658 : : number of bits in the smaller type size. */
6659 : 3666 : && compare_tree_int (const_op1,
6660 : 3666 : TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6661 : : /* We cannot drop an unsigned shift after sign-extension. */
6662 : 767153 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6663 : : {
6664 : : /* Do an unsigned shift if the operand was zero-extended. */
6665 : 3649 : result_type
6666 : 3649 : = c_common_signed_or_unsigned_type (unsigned_arg,
6667 : 3649 : TREE_TYPE (arg0));
6668 : : /* Convert value-to-be-shifted to that type. */
6669 : 3649 : if (TREE_TYPE (op0) != result_type)
6670 : 3649 : op0 = convert (result_type, op0);
6671 : : converted = 1;
6672 : : }
6673 : : }
6674 : :
6675 : : /* Comparison operations are shortened too but differently.
6676 : : They identify themselves by setting short_compare = 1. */
6677 : :
6678 : 74414237 : if (short_compare)
6679 : : {
6680 : : /* We call shorten_compare only for diagnostics. */
6681 : 24104432 : tree xop0 = fold_simple (op0);
6682 : 24104432 : tree xop1 = fold_simple (op1);
6683 : 24104432 : tree xresult_type = result_type;
6684 : 24104432 : enum tree_code xresultcode = resultcode;
6685 : 24104432 : shorten_compare (location, &xop0, &xop1, &xresult_type,
6686 : : &xresultcode);
6687 : : }
6688 : :
6689 : 50309718 : if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6690 : 24104621 : && warn_sign_compare
6691 : : /* Do not warn until the template is instantiated; we cannot
6692 : : bound the ranges of the arguments until that point. */
6693 : 309585 : && !processing_template_decl
6694 : 309585 : && (complain & tf_warning)
6695 : 303643 : && c_inhibit_evaluation_warnings == 0
6696 : : /* Even unsigned enum types promote to signed int. We don't
6697 : : want to issue -Wsign-compare warnings for this case. */
6698 : 288340 : && !enum_cast_to_int (orig_op0)
6699 : 74702577 : && !enum_cast_to_int (orig_op1))
6700 : : {
6701 : 288326 : warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6702 : : result_type, resultcode);
6703 : : }
6704 : : }
6705 : :
6706 : : /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6707 : : Then the expression will be built.
6708 : : It will be given type FINAL_TYPE if that is nonzero;
6709 : : otherwise, it will be given type RESULT_TYPE. */
6710 : 85399598 : if (! converted)
6711 : : {
6712 : 81956185 : warning_sentinel w (warn_sign_conversion, short_compare);
6713 : 81956185 : if (!same_type_p (TREE_TYPE (op0), result_type))
6714 : 3482877 : op0 = cp_convert_and_check (result_type, op0, complain);
6715 : 81956185 : if (!same_type_p (TREE_TYPE (op1), result_type))
6716 : 14809706 : op1 = cp_convert_and_check (result_type, op1, complain);
6717 : :
6718 : 81956185 : if (op0 == error_mark_node || op1 == error_mark_node)
6719 : 65 : return error_mark_node;
6720 : 81956185 : }
6721 : :
6722 : 85399533 : if (build_type == NULL_TREE)
6723 : 57521619 : build_type = result_type;
6724 : :
6725 : 85399533 : if (doing_shift
6726 : 3442847 : && flag_strong_eval_order == 2
6727 : 3345142 : && TREE_SIDE_EFFECTS (op1)
6728 : 85425599 : && !processing_template_decl)
6729 : : {
6730 : : /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6731 : : op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6732 : 26066 : op0 = cp_save_expr (op0);
6733 : 26066 : instrument_expr = op0;
6734 : : }
6735 : :
6736 : 85399533 : if (sanitize_flags_p ((SANITIZE_SHIFT
6737 : : | SANITIZE_DIVIDE
6738 : : | SANITIZE_FLOAT_DIVIDE
6739 : : | SANITIZE_SI_OVERFLOW))
6740 : 12494 : && current_function_decl != NULL_TREE
6741 : 10325 : && !processing_template_decl
6742 : 85409858 : && (doing_div_or_mod || doing_shift))
6743 : : {
6744 : : /* OP0 and/or OP1 might have side-effects. */
6745 : 881 : op0 = cp_save_expr (op0);
6746 : 881 : op1 = cp_save_expr (op1);
6747 : 881 : op0 = fold_non_dependent_expr (op0, complain);
6748 : 881 : op1 = fold_non_dependent_expr (op1, complain);
6749 : 881 : tree instrument_expr1 = NULL_TREE;
6750 : 881 : if (doing_div_or_mod
6751 : 881 : && sanitize_flags_p (SANITIZE_DIVIDE
6752 : : | SANITIZE_FLOAT_DIVIDE
6753 : : | SANITIZE_SI_OVERFLOW))
6754 : : {
6755 : : /* For diagnostics we want to use the promoted types without
6756 : : shorten_binary_op. So convert the arguments to the
6757 : : original result_type. */
6758 : 453 : tree cop0 = op0;
6759 : 453 : tree cop1 = op1;
6760 : 453 : if (TREE_TYPE (cop0) != orig_type)
6761 : 6 : cop0 = cp_convert (orig_type, op0, complain);
6762 : 453 : if (TREE_TYPE (cop1) != orig_type)
6763 : 32 : cop1 = cp_convert (orig_type, op1, complain);
6764 : 453 : instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6765 : : }
6766 : 428 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6767 : 347 : instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6768 : 881 : if (instrument_expr != NULL)
6769 : 18 : instrument_expr = add_stmt_to_compound (instrument_expr,
6770 : : instrument_expr1);
6771 : : else
6772 : 863 : instrument_expr = instrument_expr1;
6773 : : }
6774 : :
6775 : 85399533 : result = build2_loc (location, resultcode, build_type, op0, op1);
6776 : 85399533 : if (final_type != 0)
6777 : 4743689 : result = cp_convert (final_type, result, complain);
6778 : :
6779 : 85399533 : if (instrument_expr != NULL)
6780 : 26568 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6781 : : instrument_expr, result);
6782 : :
6783 : 85399533 : if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6784 : 157064 : result = get_target_expr (result, complain);
6785 : :
6786 : 85399533 : if (semantic_result_type)
6787 : 61672 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
6788 : :
6789 : 85399533 : if (!c_inhibit_evaluation_warnings)
6790 : : {
6791 : 77135365 : if (!processing_template_decl)
6792 : : {
6793 : 77135365 : op0 = cp_fully_fold (op0);
6794 : : /* Only consider the second argument if the first isn't overflowed. */
6795 : 77135365 : if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6796 : : return result;
6797 : 20267281 : op1 = cp_fully_fold (op1);
6798 : 20267281 : if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6799 : : return result;
6800 : : }
6801 : 0 : else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6802 : 0 : || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6803 : : return result;
6804 : :
6805 : 14892507 : tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6806 : 14892507 : if (TREE_OVERFLOW_P (result_ovl))
6807 : 190 : overflow_warning (location, result_ovl);
6808 : : }
6809 : :
6810 : : return result;
6811 : : }
6812 : :
6813 : : /* Build a VEC_PERM_EXPR.
6814 : : This is a simple wrapper for c_build_vec_perm_expr. */
6815 : : tree
6816 : 14734 : build_x_vec_perm_expr (location_t loc,
6817 : : tree arg0, tree arg1, tree arg2,
6818 : : tsubst_flags_t complain)
6819 : : {
6820 : 14734 : tree orig_arg0 = arg0;
6821 : 14734 : tree orig_arg1 = arg1;
6822 : 14734 : tree orig_arg2 = arg2;
6823 : 14734 : if (processing_template_decl)
6824 : : {
6825 : 19 : if (type_dependent_expression_p (arg0)
6826 : 13 : || type_dependent_expression_p (arg1)
6827 : 32 : || type_dependent_expression_p (arg2))
6828 : 6 : return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6829 : : }
6830 : 14728 : tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6831 : 14728 : if (processing_template_decl && exp != error_mark_node)
6832 : 13 : return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6833 : 13 : orig_arg1, orig_arg2);
6834 : : return exp;
6835 : : }
6836 : :
6837 : : /* Build a VEC_PERM_EXPR.
6838 : : This is a simple wrapper for c_build_shufflevector. */
6839 : : tree
6840 : 34455 : build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6841 : : tsubst_flags_t complain)
6842 : : {
6843 : 34455 : tree arg0 = (*args)[0];
6844 : 34455 : tree arg1 = (*args)[1];
6845 : 34455 : if (processing_template_decl)
6846 : : {
6847 : 43 : for (unsigned i = 0; i < args->length (); ++i)
6848 : 40 : if (i <= 1
6849 : 40 : ? type_dependent_expression_p ((*args)[i])
6850 : 9 : : instantiation_dependent_expression_p ((*args)[i]))
6851 : : {
6852 : 22 : tree exp = build_min_nt_call_vec (NULL, args);
6853 : 22 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6854 : 22 : return exp;
6855 : : }
6856 : : }
6857 : 34433 : auto_vec<tree, 16> mask;
6858 : 467509 : for (unsigned i = 2; i < args->length (); ++i)
6859 : : {
6860 : 433076 : tree idx = fold_non_dependent_expr ((*args)[i], complain);
6861 : 433076 : mask.safe_push (idx);
6862 : : }
6863 : 34433 : tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6864 : 34433 : if (processing_template_decl && exp != error_mark_node)
6865 : : {
6866 : 3 : exp = build_min_non_dep_call_vec (exp, NULL, args);
6867 : 3 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6868 : : }
6869 : 34433 : return exp;
6870 : 34433 : }
6871 : :
6872 : : /* Return a tree for the sum or difference (RESULTCODE says which)
6873 : : of pointer PTROP and integer INTOP. */
6874 : :
6875 : : static tree
6876 : 4234481 : cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6877 : : tree intop, tsubst_flags_t complain)
6878 : : {
6879 : 4234481 : tree res_type = TREE_TYPE (ptrop);
6880 : :
6881 : : /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6882 : : in certain circumstance (when it's valid to do so). So we need
6883 : : to make sure it's complete. We don't need to check here, if we
6884 : : can actually complete it at all, as those checks will be done in
6885 : : pointer_int_sum() anyway. */
6886 : 4234481 : complete_type (TREE_TYPE (res_type));
6887 : :
6888 : 4234481 : return pointer_int_sum (loc, resultcode, ptrop,
6889 : 4234481 : intop, complain & tf_warning_or_error);
6890 : : }
6891 : :
6892 : : /* Return a tree for the difference of pointers OP0 and OP1.
6893 : : The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6894 : : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6895 : :
6896 : : static tree
6897 : 1260249 : pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6898 : : tsubst_flags_t complain, tree *instrument_expr)
6899 : : {
6900 : 1260249 : tree result, inttype;
6901 : 1260249 : tree restype = ptrdiff_type_node;
6902 : 1260249 : tree target_type = TREE_TYPE (ptrtype);
6903 : :
6904 : 1260249 : if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6905 : 12 : return error_mark_node;
6906 : :
6907 : 1260237 : if (VOID_TYPE_P (target_type))
6908 : : {
6909 : 0 : if (complain & tf_error)
6910 : 0 : permerror (loc, "ISO C++ forbids using pointer of "
6911 : : "type %<void *%> in subtraction");
6912 : : else
6913 : 0 : return error_mark_node;
6914 : : }
6915 : 1260237 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
6916 : : {
6917 : 15 : if (complain & tf_error)
6918 : 3 : permerror (loc, "ISO C++ forbids using pointer to "
6919 : : "a function in subtraction");
6920 : : else
6921 : 12 : return error_mark_node;
6922 : : }
6923 : 1260225 : if (TREE_CODE (target_type) == METHOD_TYPE)
6924 : : {
6925 : 0 : if (complain & tf_error)
6926 : 0 : permerror (loc, "ISO C++ forbids using pointer to "
6927 : : "a method in subtraction");
6928 : : else
6929 : 0 : return error_mark_node;
6930 : : }
6931 : 2520450 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6932 : 1260225 : TREE_TYPE (TREE_TYPE (op0)),
6933 : : !(complain & tf_error))
6934 : 2520450 : || !verify_type_context (loc, TCTX_POINTER_ARITH,
6935 : 1260225 : TREE_TYPE (TREE_TYPE (op1)),
6936 : : !(complain & tf_error)))
6937 : 0 : return error_mark_node;
6938 : :
6939 : : /* Determine integer type result of the subtraction. This will usually
6940 : : be the same as the result type (ptrdiff_t), but may need to be a wider
6941 : : type if pointers for the address space are wider than ptrdiff_t. */
6942 : 1260225 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6943 : 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6944 : : else
6945 : : inttype = restype;
6946 : :
6947 : 1260225 : if (!processing_template_decl
6948 : 1260225 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
6949 : : {
6950 : 84 : op0 = save_expr (op0);
6951 : 84 : op1 = save_expr (op1);
6952 : :
6953 : 84 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
6954 : 84 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6955 : : }
6956 : :
6957 : : /* First do the subtraction, then build the divide operator
6958 : : and only convert at the very end.
6959 : : Do not do default conversions in case restype is a short type. */
6960 : :
6961 : : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6962 : : pointers. If some platform cannot provide that, or has a larger
6963 : : ptrdiff_type to support differences larger than half the address
6964 : : space, cast the pointers to some larger integer type and do the
6965 : : computations in that type. */
6966 : 1260225 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6967 : 0 : op0 = cp_build_binary_op (loc,
6968 : : MINUS_EXPR,
6969 : : cp_convert (inttype, op0, complain),
6970 : : cp_convert (inttype, op1, complain),
6971 : : complain);
6972 : : else
6973 : 1260225 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
6974 : :
6975 : : /* This generates an error if op1 is a pointer to an incomplete type. */
6976 : 1260225 : if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
6977 : : {
6978 : 0 : if (complain & tf_error)
6979 : 0 : error_at (loc, "invalid use of a pointer to an incomplete type in "
6980 : : "pointer arithmetic");
6981 : : else
6982 : 0 : return error_mark_node;
6983 : : }
6984 : :
6985 : 1260225 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
6986 : : {
6987 : 15 : if (complain & tf_error)
6988 : 15 : error_at (loc, "arithmetic on pointer to an empty aggregate");
6989 : : else
6990 : 0 : return error_mark_node;
6991 : : }
6992 : :
6993 : 2520447 : op1 = (TYPE_PTROB_P (ptrtype)
6994 : 2520447 : ? size_in_bytes_loc (loc, target_type)
6995 : : : integer_one_node);
6996 : :
6997 : : /* Do the division. */
6998 : :
6999 : 1260225 : result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
7000 : : cp_convert (inttype, op1, complain));
7001 : 1260225 : return cp_convert (restype, result, complain);
7002 : : }
7003 : :
7004 : : /* Construct and perhaps optimize a tree representation
7005 : : for a unary operation. CODE, a tree_code, specifies the operation
7006 : : and XARG is the operand. */
7007 : :
7008 : : tree
7009 : 56455409 : build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
7010 : : tree lookups, tsubst_flags_t complain)
7011 : : {
7012 : 56455409 : tree orig_expr = xarg;
7013 : 56455409 : tree exp;
7014 : 56455409 : int ptrmem = 0;
7015 : 56455409 : tree overload = NULL_TREE;
7016 : :
7017 : 56455409 : if (processing_template_decl)
7018 : : {
7019 : 39728292 : if (type_dependent_expression_p (xarg))
7020 : : {
7021 : 26399832 : tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
7022 : 26399832 : TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
7023 : 26399832 : return e;
7024 : : }
7025 : : }
7026 : :
7027 : 30055577 : exp = NULL_TREE;
7028 : :
7029 : : /* [expr.unary.op] says:
7030 : :
7031 : : The address of an object of incomplete type can be taken.
7032 : :
7033 : : (And is just the ordinary address operator, not an overloaded
7034 : : "operator &".) However, if the type is a template
7035 : : specialization, we must complete the type at this point so that
7036 : : an overloaded "operator &" will be available if required. */
7037 : 30055577 : if (code == ADDR_EXPR
7038 : 3410037 : && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
7039 : 33465212 : && ((CLASS_TYPE_P (TREE_TYPE (xarg))
7040 : 1619453 : && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
7041 : 3407262 : || (TREE_CODE (xarg) == OFFSET_REF)))
7042 : : /* Don't look for a function. */;
7043 : : else
7044 : 29986808 : exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
7045 : : NULL_TREE, lookups, &overload, complain);
7046 : :
7047 : 30055577 : if (!exp && code == ADDR_EXPR)
7048 : : {
7049 : 3409771 : if (is_overloaded_fn (xarg))
7050 : : {
7051 : 376848 : tree fn = get_first_fn (xarg);
7052 : 753696 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
7053 : : {
7054 : 12 : if (complain & tf_error)
7055 : 21 : error_at (loc, DECL_CONSTRUCTOR_P (fn)
7056 : : ? G_("taking address of constructor %qD")
7057 : : : G_("taking address of destructor %qD"),
7058 : : fn);
7059 : 12 : return error_mark_node;
7060 : : }
7061 : : }
7062 : :
7063 : : /* A pointer to member-function can be formed only by saying
7064 : : &X::mf. */
7065 : 3409753 : if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
7066 : 3472366 : && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
7067 : : {
7068 : 9 : if (TREE_CODE (xarg) != OFFSET_REF
7069 : 9 : || !TYPE_P (TREE_OPERAND (xarg, 0)))
7070 : : {
7071 : 9 : if (complain & tf_error)
7072 : : {
7073 : 9 : auto_diagnostic_group d;
7074 : 9 : error_at (loc, "invalid use of %qE to form a "
7075 : : "pointer-to-member-function", xarg.get_value ());
7076 : 9 : if (TREE_CODE (xarg) != OFFSET_REF)
7077 : 6 : inform (loc, " a qualified-id is required");
7078 : 9 : }
7079 : 9 : return error_mark_node;
7080 : : }
7081 : : else
7082 : : {
7083 : 0 : if (complain & tf_error)
7084 : 0 : error_at (loc, "parentheses around %qE cannot be used to "
7085 : : "form a pointer-to-member-function",
7086 : : xarg.get_value ());
7087 : : else
7088 : 0 : return error_mark_node;
7089 : 0 : PTRMEM_OK_P (xarg) = 1;
7090 : : }
7091 : : }
7092 : :
7093 : 3409750 : if (TREE_CODE (xarg) == OFFSET_REF)
7094 : : {
7095 : 66381 : ptrmem = PTRMEM_OK_P (xarg);
7096 : :
7097 : 0 : if (!ptrmem && !flag_ms_extensions
7098 : 66381 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
7099 : : {
7100 : : /* A single non-static member, make sure we don't allow a
7101 : : pointer-to-member. */
7102 : 0 : xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
7103 : 0 : TREE_OPERAND (xarg, 0),
7104 : 0 : ovl_make (TREE_OPERAND (xarg, 1)));
7105 : 0 : PTRMEM_OK_P (xarg) = ptrmem;
7106 : : }
7107 : : }
7108 : :
7109 : 6819500 : exp = cp_build_addr_expr_strict (xarg, complain);
7110 : :
7111 : 3409750 : if (TREE_CODE (exp) == PTRMEM_CST)
7112 : 65406 : PTRMEM_CST_LOCATION (exp) = loc;
7113 : : else
7114 : 3344344 : protected_set_expr_location (exp, loc);
7115 : : }
7116 : :
7117 : 30055556 : if (processing_template_decl && exp != error_mark_node)
7118 : : {
7119 : 13328348 : if (overload != NULL_TREE)
7120 : 165977 : return (build_min_non_dep_op_overload
7121 : 165977 : (code, exp, overload, orig_expr, integer_zero_node));
7122 : :
7123 : 13162371 : exp = build_min_non_dep (code, exp, orig_expr,
7124 : : /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
7125 : : }
7126 : 29889579 : if (TREE_CODE (exp) == ADDR_EXPR)
7127 : 2614655 : PTRMEM_OK_P (exp) = ptrmem;
7128 : : return exp;
7129 : : }
7130 : :
7131 : : /* Construct and perhaps optimize a tree representation
7132 : : for __builtin_addressof operation. ARG specifies the operand. */
7133 : :
7134 : : tree
7135 : 477032 : cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7136 : : {
7137 : 477032 : tree orig_expr = arg;
7138 : :
7139 : 477032 : if (processing_template_decl)
7140 : : {
7141 : 183105 : if (type_dependent_expression_p (arg))
7142 : 183105 : return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7143 : : }
7144 : :
7145 : 587854 : tree exp = cp_build_addr_expr_strict (arg, complain);
7146 : :
7147 : 293927 : if (processing_template_decl && exp != error_mark_node)
7148 : 0 : exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
7149 : : return exp;
7150 : : }
7151 : :
7152 : : /* Like c_common_truthvalue_conversion, but handle pointer-to-member
7153 : : constants, where a null value is represented by an INTEGER_CST of
7154 : : -1. */
7155 : :
7156 : : tree
7157 : 7265436 : cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7158 : : {
7159 : 7265436 : tree type = TREE_TYPE (expr);
7160 : 7265436 : location_t loc = cp_expr_loc_or_input_loc (expr);
7161 : 6143637 : if (TYPE_PTR_OR_PTRMEM_P (type)
7162 : : /* Avoid ICE on invalid use of non-static member function. */
7163 : 13408787 : || TREE_CODE (expr) == FUNCTION_DECL)
7164 : 1122085 : return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
7165 : : else
7166 : 6143351 : return c_common_truthvalue_conversion (loc, expr);
7167 : : }
7168 : :
7169 : : /* Returns EXPR contextually converted to bool. */
7170 : :
7171 : : tree
7172 : 46390641 : contextual_conv_bool (tree expr, tsubst_flags_t complain)
7173 : : {
7174 : 46390641 : return perform_implicit_conversion_flags (boolean_type_node, expr,
7175 : 46390641 : complain, LOOKUP_NORMAL);
7176 : : }
7177 : :
7178 : : /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7179 : : is a low-level function; most callers should use maybe_convert_cond. */
7180 : :
7181 : : tree
7182 : 41381589 : condition_conversion (tree expr)
7183 : : {
7184 : 41381589 : tree t = contextual_conv_bool (expr, tf_warning_or_error);
7185 : 41381589 : if (!processing_template_decl)
7186 : 20981361 : t = fold_build_cleanup_point_expr (boolean_type_node, t);
7187 : 41381589 : return t;
7188 : : }
7189 : :
7190 : : /* Returns the address of T. This function will fold away
7191 : : ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7192 : : most places should use cp_build_addr_expr instead. */
7193 : :
7194 : : tree
7195 : 276468070 : build_address (tree t)
7196 : : {
7197 : 276468070 : if (error_operand_p (t) || !cxx_mark_addressable (t))
7198 : 21 : return error_mark_node;
7199 : 276468049 : gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7200 : : || processing_template_decl);
7201 : 276468049 : t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7202 : 276468049 : if (TREE_CODE (t) != ADDR_EXPR)
7203 : 48711590 : t = rvalue (t);
7204 : : return t;
7205 : : }
7206 : :
7207 : : /* Return a NOP_EXPR converting EXPR to TYPE. */
7208 : :
7209 : : tree
7210 : 465101173 : build_nop (tree type, tree expr MEM_STAT_DECL)
7211 : : {
7212 : 465101173 : if (type == error_mark_node || error_operand_p (expr))
7213 : : return expr;
7214 : 465101095 : return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr PASS_MEM_STAT);
7215 : : }
7216 : :
7217 : : /* Take the address of ARG, whatever that means under C++ semantics.
7218 : : If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7219 : : and class rvalues as well.
7220 : :
7221 : : Nothing should call this function directly; instead, callers should use
7222 : : cp_build_addr_expr or cp_build_addr_expr_strict. */
7223 : :
7224 : : static tree
7225 : 211818879 : cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7226 : : {
7227 : 211818879 : tree argtype;
7228 : 211818879 : tree val;
7229 : :
7230 : 211818879 : if (!arg || error_operand_p (arg))
7231 : 12 : return error_mark_node;
7232 : :
7233 : 211818867 : arg = mark_lvalue_use (arg);
7234 : 211818867 : if (error_operand_p (arg))
7235 : 3 : return error_mark_node;
7236 : :
7237 : 211818864 : argtype = lvalue_type (arg);
7238 : 211818864 : location_t loc = cp_expr_loc_or_input_loc (arg);
7239 : :
7240 : 211818864 : gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7241 : :
7242 : 12950894 : if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7243 : 211819058 : && !really_overloaded_fn (arg))
7244 : : {
7245 : : /* They're trying to take the address of a unique non-static
7246 : : member function. This is ill-formed (except in MS-land),
7247 : : but let's try to DTRT.
7248 : : Note: We only handle unique functions here because we don't
7249 : : want to complain if there's a static overload; non-unique
7250 : : cases will be handled by instantiate_type. But we need to
7251 : : handle this case here to allow casts on the resulting PMF.
7252 : : We could defer this in non-MS mode, but it's easier to give
7253 : : a useful error here. */
7254 : :
7255 : : /* Inside constant member functions, the `this' pointer
7256 : : contains an extra const qualifier. TYPE_MAIN_VARIANT
7257 : : is used here to remove this const from the diagnostics
7258 : : and the created OFFSET_REF. */
7259 : 70 : tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7260 : 70 : tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7261 : 70 : if (!mark_used (fn, complain) && !(complain & tf_error))
7262 : 0 : return error_mark_node;
7263 : : /* Until microsoft headers are known to incorrectly take the address of
7264 : : unqualified xobj member functions we should not support this
7265 : : extension.
7266 : : See comment in class.cc:resolve_address_of_overloaded_function for
7267 : : the extended reasoning. */
7268 : 70 : if (!flag_ms_extensions || DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7269 : : {
7270 : 64 : auto_diagnostic_group d;
7271 : 64 : tree name = DECL_NAME (fn);
7272 : 64 : if (!(complain & tf_error))
7273 : 0 : return error_mark_node;
7274 : 64 : else if (current_class_type
7275 : 64 : && TREE_OPERAND (arg, 0) == current_class_ref)
7276 : : /* An expression like &memfn. */
7277 : 31 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7278 : 21 : permerror (loc,
7279 : : "ISO C++ forbids taking the address of an unqualified"
7280 : : " or parenthesized non-static member function to form"
7281 : : " a pointer to member function. Say %<&%T::%D%>",
7282 : : base, name);
7283 : : else
7284 : 10 : error_at (loc,
7285 : : "ISO C++ forbids taking the address of an unqualified"
7286 : : " or parenthesized non-static member function to form"
7287 : : " a pointer to explicit object member function");
7288 : : else
7289 : 33 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7290 : 9 : permerror (loc,
7291 : : "ISO C++ forbids taking the address of a bound member"
7292 : : " function to form a pointer to member function."
7293 : : " Say %<&%T::%D%>",
7294 : : base, name);
7295 : : else
7296 : 24 : error_at (loc,
7297 : : "ISO C++ forbids taking the address of a bound member"
7298 : : " function to form a pointer to explicit object member"
7299 : : " function");
7300 : 64 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7301 : 34 : inform (loc,
7302 : : "a pointer to explicit object member function can only be "
7303 : : "formed with %<&%T::%D%>", base, name);
7304 : 64 : }
7305 : 70 : arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7306 : : }
7307 : :
7308 : : /* Uninstantiated types are all functions. Taking the
7309 : : address of a function is a no-op, so just return the
7310 : : argument. */
7311 : 211818864 : if (type_unknown_p (arg))
7312 : 1970 : return build1 (ADDR_EXPR, unknown_type_node, arg);
7313 : :
7314 : 211816894 : if (TREE_CODE (arg) == OFFSET_REF)
7315 : : /* We want a pointer to member; bypass all the code for actually taking
7316 : : the address of something. */
7317 : 65531 : goto offset_ref;
7318 : :
7319 : : /* Anything not already handled and not a true memory reference
7320 : : is an error. */
7321 : 211751363 : if (!FUNC_OR_METHOD_TYPE_P (argtype))
7322 : : {
7323 : 110808209 : cp_lvalue_kind kind = lvalue_kind (arg);
7324 : 110808209 : if (kind == clk_none)
7325 : : {
7326 : 23 : if (complain & tf_error)
7327 : 23 : lvalue_error (loc, lv_addressof);
7328 : 23 : return error_mark_node;
7329 : : }
7330 : 110808186 : if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7331 : : {
7332 : 36 : if (!(complain & tf_error))
7333 : 9 : return error_mark_node;
7334 : : /* Make this a permerror because we used to accept it. */
7335 : 27 : permerror (loc, "taking address of rvalue");
7336 : : }
7337 : : }
7338 : :
7339 : 211751331 : if (TYPE_REF_P (argtype))
7340 : : {
7341 : 233 : tree type = build_pointer_type (TREE_TYPE (argtype));
7342 : 233 : arg = build1 (CONVERT_EXPR, type, arg);
7343 : 233 : return arg;
7344 : : }
7345 : 211751098 : else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7346 : : {
7347 : : /* ARM $3.4 */
7348 : : /* Apparently a lot of autoconf scripts for C++ packages do this,
7349 : : so only complain if -Wpedantic. */
7350 : 9 : if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7351 : 9 : pedwarn (loc, OPT_Wpedantic,
7352 : : "ISO C++ forbids taking address of function %<::main%>");
7353 : 0 : else if (flag_pedantic_errors)
7354 : 0 : return error_mark_node;
7355 : : }
7356 : :
7357 : : /* Let &* cancel out to simplify resulting code. */
7358 : 211751098 : if (INDIRECT_REF_P (arg))
7359 : : {
7360 : 70230853 : arg = TREE_OPERAND (arg, 0);
7361 : 70230853 : if (TYPE_REF_P (TREE_TYPE (arg)))
7362 : : {
7363 : 41147112 : tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7364 : 41147112 : arg = build1 (CONVERT_EXPR, type, arg);
7365 : : }
7366 : : else
7367 : : /* Don't let this be an lvalue. */
7368 : 29083741 : arg = rvalue (arg);
7369 : 70230853 : return arg;
7370 : : }
7371 : :
7372 : : /* Handle complex lvalues (when permitted)
7373 : : by reduction to simpler cases. */
7374 : 141520245 : val = unary_complex_lvalue (ADDR_EXPR, arg);
7375 : 141520245 : if (val != 0)
7376 : : return val;
7377 : :
7378 : 141029430 : switch (TREE_CODE (arg))
7379 : : {
7380 : 0 : CASE_CONVERT:
7381 : 0 : case FLOAT_EXPR:
7382 : 0 : case FIX_TRUNC_EXPR:
7383 : : /* We should have handled this above in the lvalue_kind check. */
7384 : 0 : gcc_unreachable ();
7385 : 64813 : break;
7386 : :
7387 : 64813 : case BASELINK:
7388 : 64813 : arg = BASELINK_FUNCTIONS (arg);
7389 : : /* Fall through. */
7390 : :
7391 : 64813 : case OVERLOAD:
7392 : 64813 : arg = OVL_FIRST (arg);
7393 : : break;
7394 : :
7395 : 65531 : case OFFSET_REF:
7396 : 65531 : offset_ref:
7397 : : /* Turn a reference to a non-static data member into a
7398 : : pointer-to-member. */
7399 : 65531 : {
7400 : 65531 : tree type;
7401 : 65531 : tree t;
7402 : :
7403 : 65531 : gcc_assert (PTRMEM_OK_P (arg));
7404 : :
7405 : 65531 : t = TREE_OPERAND (arg, 1);
7406 : 65531 : if (TYPE_REF_P (TREE_TYPE (t)))
7407 : : {
7408 : 15 : if (complain & tf_error)
7409 : 15 : error_at (loc,
7410 : : "cannot create pointer to reference member %qD", t);
7411 : 15 : return error_mark_node;
7412 : : }
7413 : :
7414 : : /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7415 : 65516 : if (TREE_CODE (t) == FUNCTION_DECL
7416 : 62741 : && !DECL_PURE_VIRTUAL_P (t)
7417 : 128236 : && !mark_used (t, complain) && !(complain & tf_error))
7418 : 3 : return error_mark_node;
7419 : :
7420 : : /* Pull out the function_decl for a single xobj member function, and
7421 : : let the rest of this function handle it. This is similar to how
7422 : : static member functions are handled in the BASELINK case above. */
7423 : 65513 : if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7424 : : {
7425 : : arg = t;
7426 : : break;
7427 : : }
7428 : :
7429 : 65457 : type = build_ptrmem_type (context_for_name_lookup (t),
7430 : 65457 : TREE_TYPE (t));
7431 : 65457 : t = make_ptrmem_cst (type, t);
7432 : 65457 : return t;
7433 : : }
7434 : :
7435 : : default:
7436 : : break;
7437 : : }
7438 : :
7439 : 141029486 : if (argtype != error_mark_node)
7440 : 141029486 : argtype = build_pointer_type (argtype);
7441 : :
7442 : 141029486 : if (bitfield_p (arg))
7443 : : {
7444 : 33 : if (complain & tf_error)
7445 : 33 : error_at (loc, "attempt to take address of bit-field");
7446 : 33 : return error_mark_node;
7447 : : }
7448 : :
7449 : : /* In a template, we are processing a non-dependent expression
7450 : : so we can just form an ADDR_EXPR with the correct type. */
7451 : 141029453 : if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7452 : : {
7453 : 128378321 : if (!mark_single_function (arg, complain))
7454 : 3 : return error_mark_node;
7455 : 128378318 : val = build_address (arg);
7456 : 128378318 : if (TREE_CODE (arg) == OFFSET_REF)
7457 : 0 : PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7458 : : }
7459 : 12651132 : else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7460 : : {
7461 : 27 : tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7462 : :
7463 : 27 : if (TREE_CODE (fn) == OVERLOAD && OVL_SINGLE_P (fn))
7464 : 27 : fn = OVL_FIRST (fn);
7465 : :
7466 : : /* We can only get here with a single static member
7467 : : function. */
7468 : 27 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7469 : : && DECL_STATIC_FUNCTION_P (fn));
7470 : 27 : if (!mark_used (fn, complain) && !(complain & tf_error))
7471 : 0 : return error_mark_node;
7472 : 27 : val = build_address (fn);
7473 : 27 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7474 : : /* Do not lose object's side effects. */
7475 : 12 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7476 : 12 : TREE_OPERAND (arg, 0), val);
7477 : : }
7478 : : else
7479 : : {
7480 : 12651105 : tree object = TREE_OPERAND (arg, 0);
7481 : 12651105 : tree field = TREE_OPERAND (arg, 1);
7482 : 12651105 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7483 : : (TREE_TYPE (object), decl_type_context (field)));
7484 : 12651105 : val = build_address (arg);
7485 : : }
7486 : :
7487 : 141029450 : if (TYPE_PTR_P (argtype)
7488 : 141029450 : && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7489 : : {
7490 : 1063 : build_ptrmemfunc_type (argtype);
7491 : 1063 : val = build_ptrmemfunc (argtype, val, 0,
7492 : : /*c_cast_p=*/false,
7493 : : complain);
7494 : : }
7495 : :
7496 : : /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7497 : 141029450 : if (TREE_CODE (val) == ADDR_EXPR
7498 : 141029450 : && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7499 : 99524440 : SET_EXPR_LOCATION (val, input_location);
7500 : :
7501 : : return val;
7502 : : }
7503 : :
7504 : : /* Take the address of ARG if it has one, even if it's an rvalue. */
7505 : :
7506 : : tree
7507 : 208115202 : cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7508 : : {
7509 : 208115202 : return cp_build_addr_expr_1 (arg, 0, complain);
7510 : : }
7511 : :
7512 : : /* Take the address of ARG, but only if it's an lvalue. */
7513 : :
7514 : : static tree
7515 : 3703677 : cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7516 : : {
7517 : 3703677 : return cp_build_addr_expr_1 (arg, 1, complain);
7518 : : }
7519 : :
7520 : : /* C++: Must handle pointers to members.
7521 : :
7522 : : Perhaps type instantiation should be extended to handle conversion
7523 : : from aggregates to types we don't yet know we want? (Or are those
7524 : : cases typically errors which should be reported?)
7525 : :
7526 : : NOCONVERT suppresses the default promotions (such as from short to int). */
7527 : :
7528 : : tree
7529 : 26809733 : cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7530 : : tsubst_flags_t complain)
7531 : : {
7532 : : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7533 : 26809733 : tree arg = xarg;
7534 : 26809733 : location_t location = cp_expr_loc_or_input_loc (arg);
7535 : 26809733 : tree argtype = 0;
7536 : 26809733 : tree eptype = NULL_TREE;
7537 : 26809733 : const char *errstring = NULL;
7538 : 26809733 : tree val;
7539 : 26809733 : const char *invalid_op_diag;
7540 : :
7541 : 26809733 : if (!arg || error_operand_p (arg))
7542 : 0 : return error_mark_node;
7543 : :
7544 : 26809733 : arg = resolve_nondeduced_context (arg, complain);
7545 : :
7546 : 53619466 : if ((invalid_op_diag
7547 : 26809733 : = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7548 : : ? CONVERT_EXPR
7549 : : : code),
7550 : 26809733 : TREE_TYPE (arg))))
7551 : : {
7552 : 0 : if (complain & tf_error)
7553 : 0 : error (invalid_op_diag);
7554 : 0 : return error_mark_node;
7555 : : }
7556 : :
7557 : 26809733 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7558 : : {
7559 : 10424 : eptype = TREE_TYPE (arg);
7560 : 10424 : arg = TREE_OPERAND (arg, 0);
7561 : : }
7562 : :
7563 : 26809733 : switch (code)
7564 : : {
7565 : 2427179 : case UNARY_PLUS_EXPR:
7566 : 2427179 : case NEGATE_EXPR:
7567 : 2427179 : {
7568 : 2427179 : int flags = WANT_ARITH | WANT_ENUM;
7569 : : /* Unary plus (but not unary minus) is allowed on pointers. */
7570 : 2427179 : if (code == UNARY_PLUS_EXPR)
7571 : 167599 : flags |= WANT_POINTER;
7572 : 2427179 : arg = build_expr_type_conversion (flags, arg, true);
7573 : 2427179 : if (!arg)
7574 : 30 : errstring = (code == NEGATE_EXPR
7575 : 24 : ? _("wrong type argument to unary minus")
7576 : 6 : : _("wrong type argument to unary plus"));
7577 : : else
7578 : : {
7579 : 2427149 : if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7580 : 616473 : arg = cp_perform_integral_promotions (arg, complain);
7581 : :
7582 : : /* Make sure the result is not an lvalue: a unary plus or minus
7583 : : expression is always a rvalue. */
7584 : 2427149 : arg = rvalue (arg);
7585 : : }
7586 : : }
7587 : : break;
7588 : :
7589 : 876560 : case BIT_NOT_EXPR:
7590 : 876560 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7591 : : {
7592 : 15 : code = CONJ_EXPR;
7593 : 15 : if (!noconvert)
7594 : : {
7595 : 15 : arg = cp_default_conversion (arg, complain);
7596 : 15 : if (arg == error_mark_node)
7597 : : return error_mark_node;
7598 : : }
7599 : : }
7600 : 876545 : else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7601 : : | WANT_VECTOR_OR_COMPLEX,
7602 : : arg, true)))
7603 : 13 : errstring = _("wrong type argument to bit-complement");
7604 : 876532 : else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7605 : : {
7606 : : /* Warn if the expression has boolean value. */
7607 : 876297 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7608 : 876297 : && (complain & tf_warning))
7609 : : {
7610 : 61 : auto_diagnostic_group d;
7611 : 61 : if (warning_at (location, OPT_Wbool_operation,
7612 : : "%<~%> on an expression of type %<bool%>"))
7613 : 42 : inform (location, "did you mean to use logical not (%<!%>)?");
7614 : 61 : }
7615 : 876297 : arg = cp_perform_integral_promotions (arg, complain);
7616 : : }
7617 : 235 : else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7618 : 235 : arg = mark_rvalue_use (arg);
7619 : : break;
7620 : :
7621 : 0 : case ABS_EXPR:
7622 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7623 : 0 : errstring = _("wrong type argument to abs");
7624 : 0 : else if (!noconvert)
7625 : : {
7626 : 0 : arg = cp_default_conversion (arg, complain);
7627 : 0 : if (arg == error_mark_node)
7628 : : return error_mark_node;
7629 : : }
7630 : : break;
7631 : :
7632 : 0 : case CONJ_EXPR:
7633 : : /* Conjugating a real value is a no-op, but allow it anyway. */
7634 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7635 : 0 : errstring = _("wrong type argument to conjugation");
7636 : 0 : else if (!noconvert)
7637 : : {
7638 : 0 : arg = cp_default_conversion (arg, complain);
7639 : 0 : if (arg == error_mark_node)
7640 : : return error_mark_node;
7641 : : }
7642 : : break;
7643 : :
7644 : 14787404 : case TRUTH_NOT_EXPR:
7645 : 14787404 : if (gnu_vector_type_p (TREE_TYPE (arg)))
7646 : 60 : return cp_build_binary_op (input_location, EQ_EXPR, arg,
7647 : 60 : build_zero_cst (TREE_TYPE (arg)), complain);
7648 : 14787344 : arg = perform_implicit_conversion (boolean_type_node, arg,
7649 : : complain);
7650 : 14787344 : if (arg != error_mark_node)
7651 : : {
7652 : 14787333 : if (processing_template_decl)
7653 : 7266874 : return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
7654 : 7520459 : val = invert_truthvalue_loc (location, arg);
7655 : 7520459 : if (obvalue_p (val))
7656 : 12703 : val = non_lvalue_loc (location, val);
7657 : 7520459 : return val;
7658 : : }
7659 : 11 : errstring = _("in argument to unary !");
7660 : 11 : break;
7661 : :
7662 : : case NOP_EXPR:
7663 : : break;
7664 : :
7665 : 469509 : case REALPART_EXPR:
7666 : 469509 : case IMAGPART_EXPR:
7667 : 469509 : val = build_real_imag_expr (input_location, code, arg);
7668 : 469509 : if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
7669 : 0 : val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
7670 : 0 : TREE_TYPE (eptype), val);
7671 : : return val;
7672 : :
7673 : 7653139 : case PREINCREMENT_EXPR:
7674 : 7653139 : case POSTINCREMENT_EXPR:
7675 : 7653139 : case PREDECREMENT_EXPR:
7676 : 7653139 : case POSTDECREMENT_EXPR:
7677 : : /* Handle complex lvalues (when permitted)
7678 : : by reduction to simpler cases. */
7679 : :
7680 : 7653139 : val = unary_complex_lvalue (code, arg);
7681 : 7653139 : if (val != 0)
7682 : 30 : goto return_build_unary_op;
7683 : :
7684 : 7653109 : tree stripped_arg;
7685 : 7653109 : stripped_arg = tree_strip_any_location_wrapper (arg);
7686 : 1371624 : if ((VAR_P (stripped_arg) || TREE_CODE (stripped_arg) == PARM_DECL)
7687 : 7110893 : && !DECL_READ_P (stripped_arg)
7688 : 8746620 : && (VAR_P (stripped_arg) ? warn_unused_but_set_variable
7689 : : : warn_unused_but_set_parameter) > 1)
7690 : : {
7691 : 9515 : arg = mark_lvalue_use (arg);
7692 : 9515 : DECL_READ_P (stripped_arg) = 0;
7693 : : }
7694 : : else
7695 : 7643594 : arg = mark_lvalue_use (arg);
7696 : :
7697 : : /* Increment or decrement the real part of the value,
7698 : : and don't change the imaginary part. */
7699 : 7653109 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7700 : : {
7701 : 18 : tree real, imag;
7702 : :
7703 : 18 : arg = cp_stabilize_reference (arg);
7704 : 18 : real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7705 : 18 : imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7706 : 18 : real = cp_build_unary_op (code, real, true, complain);
7707 : 18 : if (real == error_mark_node || imag == error_mark_node)
7708 : : return error_mark_node;
7709 : 15 : val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
7710 : 15 : goto return_build_unary_op;
7711 : : }
7712 : :
7713 : : /* Report invalid types. */
7714 : :
7715 : 7653091 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7716 : : arg, true)))
7717 : : {
7718 : 36 : if (code == PREINCREMENT_EXPR)
7719 : 9 : errstring = _("no pre-increment operator for type");
7720 : 27 : else if (code == POSTINCREMENT_EXPR)
7721 : 9 : errstring = _("no post-increment operator for type");
7722 : 18 : else if (code == PREDECREMENT_EXPR)
7723 : 9 : errstring = _("no pre-decrement operator for type");
7724 : : else
7725 : 9 : errstring = _("no post-decrement operator for type");
7726 : : break;
7727 : : }
7728 : 7653055 : else if (arg == error_mark_node)
7729 : : return error_mark_node;
7730 : :
7731 : : /* Report something read-only. */
7732 : :
7733 : 7653055 : if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7734 : 7653055 : || TREE_READONLY (arg))
7735 : : {
7736 : 61 : if (complain & tf_error)
7737 : 61 : cxx_readonly_error (location, arg,
7738 : 61 : ((code == PREINCREMENT_EXPR
7739 : 61 : || code == POSTINCREMENT_EXPR)
7740 : : ? lv_increment : lv_decrement));
7741 : : else
7742 : 0 : return error_mark_node;
7743 : : }
7744 : :
7745 : 7653055 : {
7746 : 7653055 : tree inc;
7747 : 7653055 : tree declared_type = unlowered_expr_type (arg);
7748 : :
7749 : 7653055 : argtype = TREE_TYPE (arg);
7750 : :
7751 : : /* ARM $5.2.5 last annotation says this should be forbidden. */
7752 : 7653055 : if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7753 : : {
7754 : 0 : if (complain & tf_error)
7755 : 0 : permerror (location, (code == PREINCREMENT_EXPR
7756 : 0 : || code == POSTINCREMENT_EXPR)
7757 : : ? G_("ISO C++ forbids incrementing an enum")
7758 : : : G_("ISO C++ forbids decrementing an enum"));
7759 : : else
7760 : 0 : return error_mark_node;
7761 : : }
7762 : :
7763 : : /* Compute the increment. */
7764 : :
7765 : 7653055 : if (TYPE_PTR_P (argtype))
7766 : : {
7767 : 2121985 : tree type = complete_type (TREE_TYPE (argtype));
7768 : :
7769 : 2121985 : if (!COMPLETE_OR_VOID_TYPE_P (type))
7770 : : {
7771 : 11 : if (complain & tf_error)
7772 : 9 : error_at (location, ((code == PREINCREMENT_EXPR
7773 : 9 : || code == POSTINCREMENT_EXPR))
7774 : : ? G_("cannot increment a pointer to incomplete "
7775 : : "type %qT")
7776 : : : G_("cannot decrement a pointer to incomplete "
7777 : : "type %qT"),
7778 : 9 : TREE_TYPE (argtype));
7779 : : else
7780 : 2 : return error_mark_node;
7781 : : }
7782 : 2121974 : else if (!TYPE_PTROB_P (argtype))
7783 : : {
7784 : 75 : if (complain & tf_error)
7785 : 63 : pedwarn (location, OPT_Wpointer_arith,
7786 : 63 : (code == PREINCREMENT_EXPR
7787 : 63 : || code == POSTINCREMENT_EXPR)
7788 : : ? G_("ISO C++ forbids incrementing a pointer "
7789 : : "of type %qT")
7790 : : : G_("ISO C++ forbids decrementing a pointer "
7791 : : "of type %qT"),
7792 : : argtype);
7793 : : else
7794 : 12 : return error_mark_node;
7795 : : }
7796 : 2121899 : else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7797 : 2121899 : TREE_TYPE (argtype),
7798 : : !(complain & tf_error)))
7799 : 0 : return error_mark_node;
7800 : :
7801 : 2121971 : inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7802 : : }
7803 : : else
7804 : 5530927 : inc = VECTOR_TYPE_P (argtype)
7805 : 5531070 : ? build_one_cst (argtype)
7806 : : : integer_one_node;
7807 : :
7808 : 7653041 : inc = cp_convert (argtype, inc, complain);
7809 : :
7810 : : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7811 : : need to ask Objective-C to build the increment or decrement
7812 : : expression for it. */
7813 : 7653041 : if (objc_is_property_ref (arg))
7814 : 0 : return objc_build_incr_expr_for_property_ref (input_location, code,
7815 : 0 : arg, inc);
7816 : :
7817 : : /* Complain about anything else that is not a true lvalue. */
7818 : 7653041 : if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7819 : 7653041 : || code == POSTINCREMENT_EXPR)
7820 : : ? lv_increment : lv_decrement),
7821 : : complain))
7822 : 33 : return error_mark_node;
7823 : :
7824 : : /* [depr.volatile.type] "Postfix ++ and -- expressions and
7825 : : prefix ++ and -- expressions of volatile-qualified arithmetic
7826 : : and pointer types are deprecated." */
7827 : 7652659 : if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7828 : 7653008 : && (complain & tf_warning))
7829 : 462 : warning_at (location, OPT_Wvolatile,
7830 : : "%qs expression of %<volatile%>-qualified type is "
7831 : : "deprecated",
7832 : : ((code == PREINCREMENT_EXPR
7833 : : || code == POSTINCREMENT_EXPR)
7834 : : ? "++" : "--"));
7835 : :
7836 : : /* Forbid using -- or ++ in C++17 on `bool'. */
7837 : 7653008 : if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7838 : : {
7839 : 95 : if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7840 : : {
7841 : 33 : if (complain & tf_error)
7842 : 33 : error_at (location,
7843 : : "use of an operand of type %qT in %<operator--%> "
7844 : : "is forbidden", boolean_type_node);
7845 : 33 : return error_mark_node;
7846 : : }
7847 : : else
7848 : : {
7849 : 62 : if (cxx_dialect >= cxx17)
7850 : : {
7851 : 33 : if (complain & tf_error)
7852 : 32 : error_at (location,
7853 : : "use of an operand of type %qT in "
7854 : : "%<operator++%> is forbidden in C++17",
7855 : : boolean_type_node);
7856 : 33 : return error_mark_node;
7857 : : }
7858 : : /* Otherwise, [depr.incr.bool] says this is deprecated. */
7859 : 29 : else if (complain & tf_warning)
7860 : 25 : warning_at (location, OPT_Wdeprecated,
7861 : : "use of an operand of type %qT "
7862 : : "in %<operator++%> is deprecated",
7863 : : boolean_type_node);
7864 : : }
7865 : 29 : val = boolean_increment (code, arg);
7866 : : }
7867 : 7652913 : else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7868 : : /* An rvalue has no cv-qualifiers. */
7869 : 1065463 : val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7870 : : else
7871 : 6587450 : val = build2 (code, TREE_TYPE (arg), arg, inc);
7872 : :
7873 : 7652942 : TREE_SIDE_EFFECTS (val) = 1;
7874 : 7652942 : goto return_build_unary_op;
7875 : : }
7876 : :
7877 : 595942 : case ADDR_EXPR:
7878 : : /* Note that this operation never does default_conversion
7879 : : regardless of NOCONVERT. */
7880 : 595942 : return cp_build_addr_expr (arg, complain);
7881 : :
7882 : : default:
7883 : : break;
7884 : : }
7885 : :
7886 : 3303771 : if (!errstring)
7887 : : {
7888 : 3303696 : if (argtype == 0)
7889 : 3303696 : argtype = TREE_TYPE (arg);
7890 : 3303696 : val = build1 (code, argtype, arg);
7891 : 10956683 : return_build_unary_op:
7892 : 10956683 : if (eptype)
7893 : 10415 : val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
7894 : 10956683 : return val;
7895 : : }
7896 : :
7897 : 90 : if (complain & tf_error)
7898 : 48 : error_at (location, "%s", errstring);
7899 : 90 : return error_mark_node;
7900 : : }
7901 : :
7902 : : /* Hook for the c-common bits that build a unary op. */
7903 : : tree
7904 : 4141 : build_unary_op (location_t /*location*/,
7905 : : enum tree_code code, tree xarg, bool noconvert)
7906 : : {
7907 : 4141 : return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
7908 : : }
7909 : :
7910 : : /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7911 : : so that it is a valid lvalue even for GENERIC by replacing
7912 : : (lhs = rhs) with ((lhs = rhs), lhs)
7913 : : (--lhs) with ((--lhs), lhs)
7914 : : (++lhs) with ((++lhs), lhs)
7915 : : and if lhs has side-effects, calling cp_stabilize_reference on it, so
7916 : : that it can be evaluated multiple times. */
7917 : :
7918 : : tree
7919 : 389341 : genericize_compound_lvalue (tree lvalue)
7920 : : {
7921 : 389341 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7922 : 196 : lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7923 : 196 : cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7924 : 196 : TREE_OPERAND (lvalue, 1));
7925 : 389341 : return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7926 : 389341 : lvalue, TREE_OPERAND (lvalue, 0));
7927 : : }
7928 : :
7929 : : /* Apply unary lvalue-demanding operator CODE to the expression ARG
7930 : : for certain kinds of expressions which are not really lvalues
7931 : : but which we can accept as lvalues.
7932 : :
7933 : : If ARG is not a kind of expression we can handle, return
7934 : : NULL_TREE. */
7935 : :
7936 : : tree
7937 : 262670138 : unary_complex_lvalue (enum tree_code code, tree arg)
7938 : : {
7939 : : /* Inside a template, making these kinds of adjustments is
7940 : : pointless; we are only concerned with the type of the
7941 : : expression. */
7942 : 263059273 : if (processing_template_decl)
7943 : : return NULL_TREE;
7944 : :
7945 : : /* Handle (a, b) used as an "lvalue". */
7946 : 182048943 : if (TREE_CODE (arg) == COMPOUND_EXPR)
7947 : : {
7948 : 389578 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7949 : : tf_warning_or_error);
7950 : 389578 : return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7951 : 779156 : TREE_OPERAND (arg, 0), real_result);
7952 : : }
7953 : :
7954 : : /* Handle (a ? b : c) used as an "lvalue". */
7955 : 181659365 : if (TREE_CODE (arg) == COND_EXPR
7956 : 181558077 : || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7957 : 101288 : return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7958 : :
7959 : : /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7960 : 181558077 : if (TREE_CODE (arg) == MODIFY_EXPR
7961 : 181169125 : || TREE_CODE (arg) == PREINCREMENT_EXPR
7962 : 181169041 : || TREE_CODE (arg) == PREDECREMENT_EXPR)
7963 : 389135 : return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7964 : :
7965 : 181168942 : if (code != ADDR_EXPR)
7966 : : return NULL_TREE;
7967 : :
7968 : : /* Handle (a = b) used as an "lvalue" for `&'. */
7969 : 178088932 : if (TREE_CODE (arg) == MODIFY_EXPR
7970 : 178088932 : || TREE_CODE (arg) == INIT_EXPR)
7971 : : {
7972 : 0 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
7973 : : tf_warning_or_error);
7974 : 0 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7975 : : arg, real_result);
7976 : 0 : suppress_warning (arg /* What warning? */);
7977 : 0 : return arg;
7978 : : }
7979 : :
7980 : 276517491 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7981 : 276516449 : || TREE_CODE (arg) == OFFSET_REF)
7982 : : return NULL_TREE;
7983 : :
7984 : : /* We permit compiler to make function calls returning
7985 : : objects of aggregate type look like lvalues. */
7986 : 98427517 : {
7987 : 98427517 : tree targ = arg;
7988 : :
7989 : 98427517 : if (TREE_CODE (targ) == SAVE_EXPR)
7990 : 0 : targ = TREE_OPERAND (targ, 0);
7991 : :
7992 : 98427517 : if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
7993 : : {
7994 : 29 : if (TREE_CODE (arg) == SAVE_EXPR)
7995 : : targ = arg;
7996 : : else
7997 : 29 : targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
7998 : 29 : return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
7999 : : }
8000 : :
8001 : 98427488 : if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
8002 : 0 : return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
8003 : 0 : TREE_OPERAND (targ, 0), current_function_decl, NULL);
8004 : : }
8005 : :
8006 : : /* Don't let anything else be handled specially. */
8007 : : return NULL_TREE;
8008 : : }
8009 : :
8010 : : /* Mark EXP saying that we need to be able to take the
8011 : : address of it; it should not be allocated in a register.
8012 : : Value is true if successful. ARRAY_REF_P is true if this
8013 : : is for ARRAY_REF construction - in that case we don't want
8014 : : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
8015 : : it is fine to use ARRAY_REFs for vector subscripts on vector
8016 : : register variables.
8017 : :
8018 : : C++: we do not allow `current_class_ptr' to be addressable. */
8019 : :
8020 : : bool
8021 : 291171121 : cxx_mark_addressable (tree exp, bool array_ref_p)
8022 : : {
8023 : 291171121 : tree x = exp;
8024 : :
8025 : 344765190 : while (1)
8026 : 344765190 : switch (TREE_CODE (x))
8027 : : {
8028 : 26915036 : case VIEW_CONVERT_EXPR:
8029 : 26915036 : if (array_ref_p
8030 : 731215 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8031 : 27478261 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
8032 : : return true;
8033 : 26912139 : x = TREE_OPERAND (x, 0);
8034 : 26912139 : break;
8035 : :
8036 : 26439095 : case COMPONENT_REF:
8037 : 26439095 : if (bitfield_p (x))
8038 : 9 : error ("attempt to take address of bit-field");
8039 : : /* FALLTHRU */
8040 : 26681930 : case ADDR_EXPR:
8041 : 26681930 : case ARRAY_REF:
8042 : 26681930 : case REALPART_EXPR:
8043 : 26681930 : case IMAGPART_EXPR:
8044 : 26681930 : x = TREE_OPERAND (x, 0);
8045 : 26681930 : break;
8046 : :
8047 : 5697394 : case PARM_DECL:
8048 : 5697394 : if (x == current_class_ptr)
8049 : : {
8050 : 18 : error ("cannot take the address of %<this%>, which is an rvalue expression");
8051 : 18 : TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
8052 : 18 : return true;
8053 : : }
8054 : : /* Fall through. */
8055 : :
8056 : 56376891 : case VAR_DECL:
8057 : : /* Caller should not be trying to mark initialized
8058 : : constant fields addressable. */
8059 : 56376891 : gcc_assert (DECL_LANG_SPECIFIC (x) == 0
8060 : : || DECL_IN_AGGR_P (x) == 0
8061 : : || TREE_STATIC (x)
8062 : : || DECL_EXTERNAL (x));
8063 : : /* Fall through. */
8064 : :
8065 : 56443588 : case RESULT_DECL:
8066 : 56443667 : if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
8067 : 56443661 : && !DECL_ARTIFICIAL (x))
8068 : : {
8069 : 22 : if (VAR_P (x) && DECL_HARD_REGISTER (x))
8070 : : {
8071 : 12 : error
8072 : 12 : ("address of explicit register variable %qD requested", x);
8073 : 12 : return false;
8074 : : }
8075 : 10 : else if (extra_warnings)
8076 : 3 : warning
8077 : 3 : (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
8078 : : }
8079 : 56443576 : TREE_ADDRESSABLE (x) = 1;
8080 : 56443576 : return true;
8081 : :
8082 : 143822811 : case CONST_DECL:
8083 : 143822811 : case FUNCTION_DECL:
8084 : 143822811 : TREE_ADDRESSABLE (x) = 1;
8085 : 143822811 : return true;
8086 : :
8087 : 53014 : case CONSTRUCTOR:
8088 : 53014 : TREE_ADDRESSABLE (x) = 1;
8089 : 53014 : return true;
8090 : :
8091 : 12539093 : case TARGET_EXPR:
8092 : 12539093 : TREE_ADDRESSABLE (x) = 1;
8093 : 12539093 : cxx_mark_addressable (TARGET_EXPR_SLOT (x));
8094 : 12539093 : return true;
8095 : :
8096 : : default:
8097 : : return true;
8098 : : }
8099 : : }
8100 : :
8101 : : /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
8102 : :
8103 : : tree
8104 : 6450278 : build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
8105 : : tsubst_flags_t complain)
8106 : : {
8107 : 6450278 : tree orig_ifexp = ifexp;
8108 : 6450278 : tree orig_op1 = op1;
8109 : 6450278 : tree orig_op2 = op2;
8110 : 6450278 : tree expr;
8111 : :
8112 : 6450278 : if (processing_template_decl)
8113 : : {
8114 : : /* The standard says that the expression is type-dependent if
8115 : : IFEXP is type-dependent, even though the eventual type of the
8116 : : expression doesn't dependent on IFEXP. */
8117 : 2712415 : if (type_dependent_expression_p (ifexp)
8118 : : /* As a GNU extension, the middle operand may be omitted. */
8119 : 1337623 : || (op1 && type_dependent_expression_p (op1))
8120 : 3597600 : || type_dependent_expression_p (op2))
8121 : 1855838 : return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
8122 : : }
8123 : :
8124 : 4594440 : expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
8125 : 4594440 : if (processing_template_decl && expr != error_mark_node)
8126 : : {
8127 : 856571 : tree min = build_min_non_dep (COND_EXPR, expr,
8128 : : orig_ifexp, orig_op1, orig_op2);
8129 : 856571 : expr = convert_from_reference (min);
8130 : : }
8131 : : return expr;
8132 : : }
8133 : :
8134 : : /* Given a list of expressions, return a compound expression
8135 : : that performs them all and returns the value of the last of them. */
8136 : :
8137 : : tree
8138 : 30915252 : build_x_compound_expr_from_list (tree list, expr_list_kind exp,
8139 : : tsubst_flags_t complain)
8140 : : {
8141 : 30915252 : tree expr = TREE_VALUE (list);
8142 : :
8143 : 254500 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8144 : 31169746 : && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
8145 : : {
8146 : 19 : if (complain & tf_error)
8147 : 38 : pedwarn (cp_expr_loc_or_input_loc (expr), 0,
8148 : : "list-initializer for non-class type must not "
8149 : : "be parenthesized");
8150 : : else
8151 : 0 : return error_mark_node;
8152 : : }
8153 : :
8154 : 30915252 : if (TREE_CHAIN (list))
8155 : : {
8156 : 24 : if (complain & tf_error)
8157 : 15 : switch (exp)
8158 : : {
8159 : 12 : case ELK_INIT:
8160 : 12 : permerror (input_location, "expression list treated as compound "
8161 : : "expression in initializer");
8162 : 12 : break;
8163 : 2 : case ELK_MEM_INIT:
8164 : 2 : permerror (input_location, "expression list treated as compound "
8165 : : "expression in mem-initializer");
8166 : 2 : break;
8167 : 1 : case ELK_FUNC_CAST:
8168 : 1 : permerror (input_location, "expression list treated as compound "
8169 : : "expression in functional cast");
8170 : 1 : break;
8171 : 0 : default:
8172 : 0 : gcc_unreachable ();
8173 : : }
8174 : : else
8175 : 9 : return error_mark_node;
8176 : :
8177 : 30 : for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
8178 : 30 : expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
8179 : 15 : expr, TREE_VALUE (list), NULL_TREE,
8180 : : complain);
8181 : : }
8182 : :
8183 : : return expr;
8184 : : }
8185 : :
8186 : : /* Like build_x_compound_expr_from_list, but using a VEC. */
8187 : :
8188 : : tree
8189 : 131219 : build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8190 : : tsubst_flags_t complain)
8191 : : {
8192 : 131219 : if (vec_safe_is_empty (vec))
8193 : : return NULL_TREE;
8194 : 131219 : else if (vec->length () == 1)
8195 : 131154 : return (*vec)[0];
8196 : : else
8197 : : {
8198 : 65 : tree expr;
8199 : 65 : unsigned int ix;
8200 : 65 : tree t;
8201 : :
8202 : 65 : if (msg != NULL)
8203 : : {
8204 : 5 : if (complain & tf_error)
8205 : 0 : permerror (input_location,
8206 : : "%s expression list treated as compound expression",
8207 : : msg);
8208 : : else
8209 : 5 : return error_mark_node;
8210 : : }
8211 : :
8212 : 60 : expr = (*vec)[0];
8213 : 134 : for (ix = 1; vec->iterate (ix, &t); ++ix)
8214 : 74 : expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
8215 : : t, NULL_TREE, complain);
8216 : :
8217 : : return expr;
8218 : : }
8219 : : }
8220 : :
8221 : : /* Handle overloading of the ',' operator when needed. */
8222 : :
8223 : : tree
8224 : 2761149 : build_x_compound_expr (location_t loc, tree op1, tree op2,
8225 : : tree lookups, tsubst_flags_t complain)
8226 : : {
8227 : 2761149 : tree result;
8228 : 2761149 : tree orig_op1 = op1;
8229 : 2761149 : tree orig_op2 = op2;
8230 : 2761149 : tree overload = NULL_TREE;
8231 : :
8232 : 2761149 : if (processing_template_decl)
8233 : : {
8234 : 2533387 : if (type_dependent_expression_p (op1)
8235 : 2533387 : || type_dependent_expression_p (op2))
8236 : : {
8237 : 2251413 : result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8238 : 2251413 : TREE_TYPE (result)
8239 : 2251413 : = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
8240 : 2251413 : return result;
8241 : : }
8242 : : }
8243 : :
8244 : 509736 : result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8245 : : NULL_TREE, lookups, &overload, complain);
8246 : 509736 : if (!result)
8247 : 502638 : result = cp_build_compound_expr (op1, op2, complain);
8248 : :
8249 : 509736 : if (processing_template_decl && result != error_mark_node)
8250 : : {
8251 : 280153 : if (overload != NULL_TREE)
8252 : 52 : return (build_min_non_dep_op_overload
8253 : 52 : (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
8254 : :
8255 : 280101 : return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
8256 : : }
8257 : :
8258 : : return result;
8259 : : }
8260 : :
8261 : : /* Like cp_build_compound_expr, but for the c-common bits. */
8262 : :
8263 : : tree
8264 : 10932 : build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8265 : : {
8266 : 10932 : return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8267 : : }
8268 : :
8269 : : /* Build a compound expression. */
8270 : :
8271 : : tree
8272 : 618149 : cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8273 : : {
8274 : 618149 : lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8275 : :
8276 : 618149 : if (lhs == error_mark_node || rhs == error_mark_node)
8277 : : return error_mark_node;
8278 : :
8279 : 618146 : if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8280 : 0 : lhs = TREE_OPERAND (lhs, 0);
8281 : 618146 : tree eptype = NULL_TREE;
8282 : 618146 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8283 : : {
8284 : 3 : eptype = TREE_TYPE (rhs);
8285 : 3 : rhs = TREE_OPERAND (rhs, 0);
8286 : : }
8287 : :
8288 : 618146 : if (TREE_CODE (rhs) == TARGET_EXPR)
8289 : : {
8290 : : /* If the rhs is a TARGET_EXPR, then build the compound
8291 : : expression inside the target_expr's initializer. This
8292 : : helps the compiler to eliminate unnecessary temporaries. */
8293 : 2918 : tree init = TARGET_EXPR_INITIAL (rhs);
8294 : :
8295 : 2918 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8296 : 2918 : TARGET_EXPR_INITIAL (rhs) = init;
8297 : :
8298 : 2918 : if (eptype)
8299 : 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8300 : 2918 : return rhs;
8301 : : }
8302 : :
8303 : 615228 : rhs = resolve_nondeduced_context (rhs, complain);
8304 : :
8305 : 615228 : if (type_unknown_p (rhs))
8306 : : {
8307 : 33 : if (complain & tf_error)
8308 : 51 : error_at (cp_expr_loc_or_input_loc (rhs),
8309 : : "no context to resolve type of %qE", rhs);
8310 : 33 : return error_mark_node;
8311 : : }
8312 : :
8313 : 615195 : tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8314 : 615195 : if (eptype)
8315 : 3 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8316 : : return ret;
8317 : : }
8318 : :
8319 : : /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8320 : : casts away constness. CAST gives the type of cast. Returns true
8321 : : if the cast is ill-formed, false if it is well-formed.
8322 : :
8323 : : ??? This function warns for casting away any qualifier not just
8324 : : const. We would like to specify exactly what qualifiers are casted
8325 : : away.
8326 : : */
8327 : :
8328 : : static bool
8329 : 696924 : check_for_casting_away_constness (location_t loc, tree src_type,
8330 : : tree dest_type, enum tree_code cast,
8331 : : tsubst_flags_t complain)
8332 : : {
8333 : : /* C-style casts are allowed to cast away constness. With
8334 : : WARN_CAST_QUAL, we still want to issue a warning. */
8335 : 696924 : if (cast == CAST_EXPR && !warn_cast_qual)
8336 : : return false;
8337 : :
8338 : 566679 : if (!casts_away_constness (src_type, dest_type, complain))
8339 : : return false;
8340 : :
8341 : 386 : switch (cast)
8342 : : {
8343 : 329 : case CAST_EXPR:
8344 : 329 : if (complain & tf_warning)
8345 : 329 : warning_at (loc, OPT_Wcast_qual,
8346 : : "cast from type %qT to type %qT casts away qualifiers",
8347 : : src_type, dest_type);
8348 : : return false;
8349 : :
8350 : 36 : case STATIC_CAST_EXPR:
8351 : 36 : if (complain & tf_error)
8352 : 24 : error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8353 : : "away qualifiers",
8354 : : src_type, dest_type);
8355 : : return true;
8356 : :
8357 : 21 : case REINTERPRET_CAST_EXPR:
8358 : 21 : if (complain & tf_error)
8359 : 15 : error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8360 : : "casts away qualifiers",
8361 : : src_type, dest_type);
8362 : : return true;
8363 : :
8364 : 0 : default:
8365 : 0 : gcc_unreachable();
8366 : : }
8367 : : }
8368 : :
8369 : : /* Warns if the cast from expression EXPR to type TYPE is useless. */
8370 : : void
8371 : 44655502 : maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8372 : : tsubst_flags_t complain)
8373 : : {
8374 : 44655502 : if (warn_useless_cast
8375 : 152 : && complain & tf_warning)
8376 : : {
8377 : 152 : if (TYPE_REF_P (type)
8378 : 152 : ? ((TYPE_REF_IS_RVALUE (type)
8379 : 86 : ? xvalue_p (expr) : lvalue_p (expr))
8380 : 172 : && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8381 : : /* Don't warn when converting a class object to a non-reference type,
8382 : : because that's a common way to create a temporary. */
8383 : 66 : : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8384 : 96 : warning_at (loc, OPT_Wuseless_cast,
8385 : : "useless cast to type %q#T", type);
8386 : : }
8387 : 44655502 : }
8388 : :
8389 : : /* Warns if the cast ignores cv-qualifiers on TYPE. */
8390 : : static void
8391 : 44645981 : maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8392 : : tsubst_flags_t complain)
8393 : : {
8394 : 44645981 : if (warn_ignored_qualifiers
8395 : 258997 : && complain & tf_warning
8396 : 258628 : && !CLASS_TYPE_P (type)
8397 : 44900165 : && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8398 : 36 : warning_at (loc, OPT_Wignored_qualifiers,
8399 : : "type qualifiers ignored on cast result type");
8400 : 44645981 : }
8401 : :
8402 : : /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8403 : : (another pointer-to-member type in the same hierarchy) and return
8404 : : the converted expression. If ALLOW_INVERSE_P is permitted, a
8405 : : pointer-to-derived may be converted to pointer-to-base; otherwise,
8406 : : only the other direction is permitted. If C_CAST_P is true, this
8407 : : conversion is taking place as part of a C-style cast. */
8408 : :
8409 : : tree
8410 : 30129 : convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8411 : : bool c_cast_p, tsubst_flags_t complain)
8412 : : {
8413 : 30129 : if (same_type_p (type, TREE_TYPE (expr)))
8414 : : return expr;
8415 : :
8416 : 30129 : if (TYPE_PTRDATAMEM_P (type))
8417 : : {
8418 : 364 : tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8419 : 364 : tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8420 : 364 : tree delta = (get_delta_difference
8421 : 364 : (obase, nbase,
8422 : : allow_inverse_p, c_cast_p, complain));
8423 : :
8424 : 364 : if (delta == error_mark_node)
8425 : : return error_mark_node;
8426 : :
8427 : 358 : if (!same_type_p (obase, nbase))
8428 : : {
8429 : 287 : if (TREE_CODE (expr) == PTRMEM_CST)
8430 : 159 : expr = cplus_expand_constant (expr);
8431 : :
8432 : 287 : tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8433 : 287 : build_int_cst (TREE_TYPE (expr), -1),
8434 : : complain);
8435 : 287 : tree op1 = build_nop (ptrdiff_type_node, expr);
8436 : 287 : tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8437 : : complain);
8438 : :
8439 : 287 : expr = fold_build3_loc (input_location,
8440 : : COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8441 : : }
8442 : :
8443 : 358 : return build_nop (type, expr);
8444 : : }
8445 : : else
8446 : 29765 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8447 : 29765 : allow_inverse_p, c_cast_p, complain);
8448 : : }
8449 : :
8450 : : /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8451 : : this static_cast is being attempted as one of the possible casts
8452 : : allowed by a C-style cast. (In that case, accessibility of base
8453 : : classes is not considered, and it is OK to cast away
8454 : : constness.) Return the result of the cast. *VALID_P is set to
8455 : : indicate whether or not the cast was valid. */
8456 : :
8457 : : static tree
8458 : 44286777 : build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8459 : : bool *valid_p, tsubst_flags_t complain)
8460 : : {
8461 : 44286777 : tree intype;
8462 : 44286777 : tree result;
8463 : 44286777 : cp_lvalue_kind clk;
8464 : :
8465 : : /* Assume the cast is valid. */
8466 : 44286777 : *valid_p = true;
8467 : :
8468 : 44286777 : intype = unlowered_expr_type (expr);
8469 : :
8470 : : /* Save casted types in the function's used types hash table. */
8471 : 44286777 : used_types_insert (type);
8472 : :
8473 : : /* A prvalue of non-class type is cv-unqualified. */
8474 : 44286777 : if (!CLASS_TYPE_P (type))
8475 : 42608884 : type = cv_unqualified (type);
8476 : :
8477 : : /* [expr.static.cast]
8478 : :
8479 : : An lvalue of type "cv1 B", where B is a class type, can be cast
8480 : : to type "reference to cv2 D", where D is a class derived (clause
8481 : : _class.derived_) from B, if a valid standard conversion from
8482 : : "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8483 : : same cv-qualification as, or greater cv-qualification than, cv1,
8484 : : and B is not a virtual base class of D. */
8485 : : /* We check this case before checking the validity of "TYPE t =
8486 : : EXPR;" below because for this case:
8487 : :
8488 : : struct B {};
8489 : : struct D : public B { D(const B&); };
8490 : : extern B& b;
8491 : : void f() { static_cast<const D&>(b); }
8492 : :
8493 : : we want to avoid constructing a new D. The standard is not
8494 : : completely clear about this issue, but our interpretation is
8495 : : consistent with other compilers. */
8496 : 44286777 : if (TYPE_REF_P (type)
8497 : 3355106 : && CLASS_TYPE_P (TREE_TYPE (type))
8498 : 2702165 : && CLASS_TYPE_P (intype)
8499 : 2702150 : && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8500 : 2700738 : && DERIVED_FROM_P (intype, TREE_TYPE (type))
8501 : 2669397 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8502 : 2669397 : build_pointer_type (TYPE_MAIN_VARIANT
8503 : : (TREE_TYPE (type))),
8504 : : complain)
8505 : 46956174 : && (c_cast_p
8506 : 2669373 : || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8507 : : {
8508 : 2669397 : tree base;
8509 : :
8510 : 2669397 : if (processing_template_decl)
8511 : : return expr;
8512 : :
8513 : : /* There is a standard conversion from "D*" to "B*" even if "B"
8514 : : is ambiguous or inaccessible. If this is really a
8515 : : static_cast, then we check both for inaccessibility and
8516 : : ambiguity. However, if this is a static_cast being performed
8517 : : because the user wrote a C-style cast, then accessibility is
8518 : : not considered. */
8519 : 4985368 : base = lookup_base (TREE_TYPE (type), intype,
8520 : : c_cast_p ? ba_unique : ba_check,
8521 : : NULL, complain);
8522 : 2492696 : expr = cp_build_addr_expr (expr, complain);
8523 : :
8524 : 2492696 : if (sanitize_flags_p (SANITIZE_VPTR))
8525 : : {
8526 : 438 : tree ubsan_check
8527 : 438 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8528 : : intype, expr);
8529 : 438 : if (ubsan_check)
8530 : 2492696 : expr = ubsan_check;
8531 : : }
8532 : :
8533 : : /* Convert from "B*" to "D*". This function will check that "B"
8534 : : is not a virtual base of "D". Even if we don't have a guarantee
8535 : : that expr is NULL, if the static_cast is to a reference type,
8536 : : it is UB if it would be NULL, so omit the non-NULL check. */
8537 : 2492696 : expr = build_base_path (MINUS_EXPR, expr, base,
8538 : : /*nonnull=*/flag_delete_null_pointer_checks,
8539 : : complain);
8540 : :
8541 : : /* Convert the pointer to a reference -- but then remember that
8542 : : there are no expressions with reference type in C++.
8543 : :
8544 : : We call rvalue so that there's an actual tree code
8545 : : (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8546 : : is a variable with the same type, the conversion would get folded
8547 : : away, leaving just the variable and causing lvalue_kind to give
8548 : : the wrong answer. */
8549 : 2492696 : expr = cp_fold_convert (type, expr);
8550 : :
8551 : : /* When -fsanitize=null, make sure to diagnose reference binding to
8552 : : NULL even when the reference is converted to pointer later on. */
8553 : 2492696 : if (sanitize_flags_p (SANITIZE_NULL)
8554 : 444 : && TREE_CODE (expr) == COND_EXPR
8555 : 6 : && TREE_OPERAND (expr, 2)
8556 : 6 : && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8557 : 2492702 : && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8558 : 6 : ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8559 : :
8560 : 2492696 : return convert_from_reference (rvalue (expr));
8561 : : }
8562 : :
8563 : : /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8564 : : cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8565 : 41617380 : if (TYPE_REF_P (type)
8566 : 685709 : && TYPE_REF_IS_RVALUE (type)
8567 : 421521 : && (clk = real_lvalue_p (expr))
8568 : 418272 : && reference_compatible_p (TREE_TYPE (type), intype)
8569 : 42035649 : && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8570 : : {
8571 : 418269 : if (processing_template_decl)
8572 : : return expr;
8573 : 418054 : if (clk == clk_ordinary)
8574 : : {
8575 : : /* Handle the (non-bit-field) lvalue case here by casting to
8576 : : lvalue reference and then changing it to an rvalue reference.
8577 : : Casting an xvalue to rvalue reference will be handled by the
8578 : : main code path. */
8579 : 418051 : tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8580 : 418051 : result = (perform_direct_initialization_if_possible
8581 : 418051 : (lref, expr, c_cast_p, complain));
8582 : 418051 : result = build1 (NON_LVALUE_EXPR, type, result);
8583 : 418051 : return convert_from_reference (result);
8584 : : }
8585 : : else
8586 : : /* For a bit-field or packed field, bind to a temporary. */
8587 : 3 : expr = rvalue (expr);
8588 : : }
8589 : :
8590 : : /* Resolve overloaded address here rather than once in
8591 : : implicit_conversion and again in the inverse code below. */
8592 : 41199114 : if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8593 : : {
8594 : 18 : expr = instantiate_type (type, expr, complain);
8595 : 18 : intype = TREE_TYPE (expr);
8596 : : }
8597 : :
8598 : : /* [expr.static.cast]
8599 : :
8600 : : Any expression can be explicitly converted to type cv void. */
8601 : 41199114 : if (VOID_TYPE_P (type))
8602 : : {
8603 : 377541 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8604 : 0 : expr = TREE_OPERAND (expr, 0);
8605 : 377541 : return convert_to_void (expr, ICV_CAST, complain);
8606 : : }
8607 : :
8608 : : /* [class.abstract]
8609 : : An abstract class shall not be used ... as the type of an explicit
8610 : : conversion. */
8611 : 40821573 : if (abstract_virtuals_error (ACU_CAST, type, complain))
8612 : 6 : return error_mark_node;
8613 : :
8614 : : /* [expr.static.cast]
8615 : :
8616 : : An expression e can be explicitly converted to a type T using a
8617 : : static_cast of the form static_cast<T>(e) if the declaration T
8618 : : t(e);" is well-formed, for some invented temporary variable
8619 : : t. */
8620 : 40821567 : result = perform_direct_initialization_if_possible (type, expr,
8621 : : c_cast_p, complain);
8622 : : /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8623 : : which initialize the first element of the aggregate. We need to handle
8624 : : the array case specifically. */
8625 : 40821567 : if (result == NULL_TREE
8626 : 4026057 : && cxx_dialect >= cxx20
8627 : 1075440 : && TREE_CODE (type) == ARRAY_TYPE)
8628 : : {
8629 : : /* Create { EXPR } and perform direct-initialization from it. */
8630 : 15 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8631 : 15 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8632 : 15 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8633 : 15 : result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8634 : : complain);
8635 : : }
8636 : 4026057 : if (result)
8637 : : {
8638 : 36795525 : if (processing_template_decl)
8639 : : return expr;
8640 : :
8641 : 36119942 : result = convert_from_reference (result);
8642 : :
8643 : : /* [expr.static.cast]
8644 : :
8645 : : If T is a reference type, the result is an lvalue; otherwise,
8646 : : the result is an rvalue. */
8647 : 36119942 : if (!TYPE_REF_P (type))
8648 : : {
8649 : 35852596 : result = rvalue (result);
8650 : :
8651 : 35852596 : if (result == expr && SCALAR_TYPE_P (type))
8652 : : /* Leave some record of the cast. */
8653 : 2035927 : result = build_nop (type, expr);
8654 : : }
8655 : 36119942 : return result;
8656 : : }
8657 : :
8658 : : /* [expr.static.cast]
8659 : :
8660 : : The inverse of any standard conversion sequence (clause _conv_),
8661 : : other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8662 : : (_conv.array_), function-to-pointer (_conv.func_), and boolean
8663 : : (_conv.bool_) conversions, can be performed explicitly using
8664 : : static_cast subject to the restriction that the explicit
8665 : : conversion does not cast away constness (_expr.const.cast_), and
8666 : : the following additional rules for specific cases: */
8667 : : /* For reference, the conversions not excluded are: integral
8668 : : promotions, floating-point promotion, integral conversions,
8669 : : floating-point conversions, floating-integral conversions,
8670 : : pointer conversions, and pointer to member conversions. */
8671 : : /* DR 128
8672 : :
8673 : : A value of integral _or enumeration_ type can be explicitly
8674 : : converted to an enumeration type. */
8675 : : /* The effect of all that is that any conversion between any two
8676 : : types which are integral, floating, or enumeration types can be
8677 : : performed. */
8678 : 4026042 : if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8679 : 2291087 : || SCALAR_FLOAT_TYPE_P (type))
8680 : 1899304 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8681 : 270210 : || SCALAR_FLOAT_TYPE_P (intype)))
8682 : : {
8683 : 1793431 : if (processing_template_decl)
8684 : : return expr;
8685 : 1718664 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8686 : 0 : expr = TREE_OPERAND (expr, 0);
8687 : : /* [expr.static.cast]: "If the value is not a bit-field, the result
8688 : : refers to the object or the specified base class subobject thereof;
8689 : : otherwise, the lvalue-to-rvalue conversion is applied to the
8690 : : bit-field and the resulting prvalue is used as the operand of the
8691 : : static_cast." There are no prvalue bit-fields; the l-to-r conversion
8692 : : will give us an object of the underlying type of the bit-field. */
8693 : 1718664 : expr = decay_conversion (expr, complain);
8694 : 1718664 : return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8695 : : }
8696 : :
8697 : 726641 : if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8698 : 722305 : && CLASS_TYPE_P (TREE_TYPE (type))
8699 : 203921 : && CLASS_TYPE_P (TREE_TYPE (intype))
8700 : 2271806 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8701 : : (TREE_TYPE (intype))),
8702 : 39195 : build_pointer_type (TYPE_MAIN_VARIANT
8703 : : (TREE_TYPE (type))),
8704 : : complain))
8705 : : {
8706 : 38856 : tree base;
8707 : :
8708 : 38856 : if (processing_template_decl)
8709 : : return expr;
8710 : :
8711 : 38838 : if (!c_cast_p
8712 : 38838 : && check_for_casting_away_constness (loc, intype, type,
8713 : : STATIC_CAST_EXPR,
8714 : : complain))
8715 : 6 : return error_mark_node;
8716 : 77125 : base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8717 : : c_cast_p ? ba_unique : ba_check,
8718 : : NULL, complain);
8719 : 38832 : expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8720 : : complain);
8721 : :
8722 : 38832 : if (sanitize_flags_p (SANITIZE_VPTR))
8723 : : {
8724 : 43 : tree ubsan_check
8725 : 43 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8726 : : intype, expr);
8727 : 43 : if (ubsan_check)
8728 : 38832 : expr = ubsan_check;
8729 : : }
8730 : :
8731 : 38832 : return cp_fold_convert (type, expr);
8732 : : }
8733 : :
8734 : 89 : if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8735 : 2193755 : || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8736 : : {
8737 : 232 : tree c1;
8738 : 232 : tree c2;
8739 : 232 : tree t1;
8740 : 232 : tree t2;
8741 : :
8742 : 232 : c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8743 : 232 : c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8744 : :
8745 : 232 : if (TYPE_PTRDATAMEM_P (type))
8746 : : {
8747 : 89 : t1 = (build_ptrmem_type
8748 : 89 : (c1,
8749 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8750 : 89 : t2 = (build_ptrmem_type
8751 : 89 : (c2,
8752 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8753 : : }
8754 : : else
8755 : : {
8756 : : t1 = intype;
8757 : : t2 = type;
8758 : : }
8759 : 232 : if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8760 : : {
8761 : 154 : if (!c_cast_p
8762 : 154 : && check_for_casting_away_constness (loc, intype, type,
8763 : : STATIC_CAST_EXPR,
8764 : : complain))
8765 : 9 : return error_mark_node;
8766 : 145 : if (processing_template_decl)
8767 : : return expr;
8768 : 145 : return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8769 : 145 : c_cast_p, complain);
8770 : : }
8771 : : }
8772 : :
8773 : : /* [expr.static.cast]
8774 : :
8775 : : An rvalue of type "pointer to cv void" can be explicitly
8776 : : converted to a pointer to object type. A value of type pointer
8777 : : to object converted to "pointer to cv void" and back to the
8778 : : original pointer type will have its original value. */
8779 : 2193601 : if (TYPE_PTR_P (intype)
8780 : 787340 : && VOID_TYPE_P (TREE_TYPE (intype))
8781 : 2848655 : && TYPE_PTROB_P (type))
8782 : : {
8783 : 623460 : if (!c_cast_p
8784 : 623460 : && check_for_casting_away_constness (loc, intype, type,
8785 : : STATIC_CAST_EXPR,
8786 : : complain))
8787 : 21 : return error_mark_node;
8788 : 623439 : if (processing_template_decl)
8789 : : return expr;
8790 : 533523 : return build_nop (type, expr);
8791 : : }
8792 : :
8793 : 1570141 : *valid_p = false;
8794 : 1570141 : return error_mark_node;
8795 : : }
8796 : :
8797 : : /* Return an expression representing static_cast<TYPE>(EXPR). */
8798 : :
8799 : : tree
8800 : 12599172 : build_static_cast (location_t loc, tree type, tree oexpr,
8801 : : tsubst_flags_t complain)
8802 : : {
8803 : 12599172 : tree expr = oexpr;
8804 : 12599172 : tree result;
8805 : 12599172 : bool valid_p;
8806 : :
8807 : 12599172 : if (type == error_mark_node || expr == error_mark_node)
8808 : : return error_mark_node;
8809 : :
8810 : 12599118 : bool dependent = (dependent_type_p (type)
8811 : 12599118 : || type_dependent_expression_p (expr));
8812 : 8400258 : if (dependent)
8813 : : {
8814 : 5216097 : tmpl:
8815 : 5216097 : expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8816 : : /* We don't know if it will or will not have side effects. */
8817 : 5216097 : TREE_SIDE_EFFECTS (expr) = 1;
8818 : 5216097 : result = convert_from_reference (expr);
8819 : 5216097 : protected_set_expr_location (result, loc);
8820 : 5216097 : return result;
8821 : : }
8822 : :
8823 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8824 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8825 : 8400258 : if (!TYPE_REF_P (type)
8826 : 5045619 : && TREE_CODE (expr) == NOP_EXPR
8827 : 8501243 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8828 : 48 : expr = TREE_OPERAND (expr, 0);
8829 : :
8830 : 8400258 : result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8831 : : &valid_p, complain);
8832 : 8400258 : if (valid_p)
8833 : : {
8834 : 8400062 : if (result != error_mark_node)
8835 : : {
8836 : 8399957 : maybe_warn_about_useless_cast (loc, type, expr, complain);
8837 : 8399957 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8838 : : }
8839 : 8400062 : if (processing_template_decl)
8840 : 1017237 : goto tmpl;
8841 : 7382825 : protected_set_expr_location (result, loc);
8842 : 7382825 : return result;
8843 : : }
8844 : :
8845 : 196 : if (complain & tf_error)
8846 : : {
8847 : 163 : auto_diagnostic_group d;
8848 : 163 : error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8849 : 163 : TREE_TYPE (expr), type);
8850 : 163 : if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8851 : 121 : && CLASS_TYPE_P (TREE_TYPE (type))
8852 : 192 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8853 : 17 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8854 : 17 : "class type %qT is incomplete", TREE_TYPE (type));
8855 : 163 : tree expr_type = TREE_TYPE (expr);
8856 : 163 : if (TYPE_PTR_P (expr_type))
8857 : 36 : expr_type = TREE_TYPE (expr_type);
8858 : 163 : if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8859 : 12 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8860 : : "class type %qT is incomplete", expr_type);
8861 : 163 : }
8862 : 196 : return error_mark_node;
8863 : : }
8864 : :
8865 : : /* EXPR is an expression with member function or pointer-to-member
8866 : : function type. TYPE is a pointer type. Converting EXPR to TYPE is
8867 : : not permitted by ISO C++, but we accept it in some modes. If we
8868 : : are not in one of those modes, issue a diagnostic. Return the
8869 : : converted expression. */
8870 : :
8871 : : tree
8872 : 197 : convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8873 : : {
8874 : 197 : tree intype;
8875 : 197 : tree decl;
8876 : :
8877 : 197 : intype = TREE_TYPE (expr);
8878 : 197 : gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8879 : : || TREE_CODE (intype) == METHOD_TYPE);
8880 : :
8881 : 197 : if (!(complain & tf_warning_or_error))
8882 : 0 : return error_mark_node;
8883 : :
8884 : 197 : location_t loc = cp_expr_loc_or_input_loc (expr);
8885 : :
8886 : 197 : if (pedantic || warn_pmf2ptr)
8887 : 271 : pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8888 : : "converting from %qH to %qI", intype, type);
8889 : :
8890 : 197 : STRIP_ANY_LOCATION_WRAPPER (expr);
8891 : :
8892 : 197 : if (TREE_CODE (intype) == METHOD_TYPE)
8893 : 88 : expr = build_addr_func (expr, complain);
8894 : 109 : else if (TREE_CODE (expr) == PTRMEM_CST)
8895 : 91 : expr = build_address (PTRMEM_CST_MEMBER (expr));
8896 : : else
8897 : : {
8898 : 18 : decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8899 : 18 : decl = build_address (decl);
8900 : 18 : expr = get_member_function_from_ptrfunc (&decl, expr, complain);
8901 : : }
8902 : :
8903 : 197 : if (expr == error_mark_node)
8904 : : return error_mark_node;
8905 : :
8906 : 197 : expr = build_nop (type, expr);
8907 : 197 : SET_EXPR_LOCATION (expr, loc);
8908 : 197 : return expr;
8909 : : }
8910 : :
8911 : : /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8912 : : constexpr evaluation knows to reject it. */
8913 : :
8914 : : static tree
8915 : 97512 : build_nop_reinterpret (tree type, tree expr)
8916 : : {
8917 : 97512 : tree ret = build_nop (type, expr);
8918 : 97512 : if (ret != expr)
8919 : 97512 : REINTERPRET_CAST_P (ret) = true;
8920 : 97512 : return ret;
8921 : : }
8922 : :
8923 : : /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8924 : : If C_CAST_P is true, this reinterpret cast is being done as part of
8925 : : a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8926 : : indicate whether or not reinterpret_cast was valid. */
8927 : :
8928 : : static tree
8929 : 1691580 : build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8930 : : bool c_cast_p, bool *valid_p,
8931 : : tsubst_flags_t complain)
8932 : : {
8933 : 1691580 : tree intype;
8934 : :
8935 : : /* Assume the cast is invalid. */
8936 : 1691580 : if (valid_p)
8937 : 1570001 : *valid_p = true;
8938 : :
8939 : 1691580 : if (type == error_mark_node || error_operand_p (expr))
8940 : : return error_mark_node;
8941 : :
8942 : 1691580 : intype = TREE_TYPE (expr);
8943 : :
8944 : : /* Save casted types in the function's used types hash table. */
8945 : 1691580 : used_types_insert (type);
8946 : :
8947 : : /* A prvalue of non-class type is cv-unqualified. */
8948 : 1691580 : if (!CLASS_TYPE_P (type))
8949 : 1691571 : type = cv_unqualified (type);
8950 : :
8951 : : /* [expr.reinterpret.cast]
8952 : : A glvalue of type T1, designating an object x, can be cast to the type
8953 : : "reference to T2" if an expression of type "pointer to T1" can be
8954 : : explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8955 : : The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8956 : : of type "pointer to T1". No temporary is created, no copy is made, and no
8957 : : constructors (11.4.4) or conversion functions (11.4.7) are called. */
8958 : 1691580 : if (TYPE_REF_P (type))
8959 : : {
8960 : 10192 : if (!glvalue_p (expr))
8961 : : {
8962 : 9 : if (complain & tf_error)
8963 : 9 : error_at (loc, "invalid cast of a prvalue expression of type "
8964 : : "%qT to type %qT",
8965 : : intype, type);
8966 : 9 : return error_mark_node;
8967 : : }
8968 : :
8969 : : /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8970 : : "B" are related class types; the reinterpret_cast does not
8971 : : adjust the pointer. */
8972 : 10183 : if (TYPE_PTR_P (intype)
8973 : 3 : && (complain & tf_warning)
8974 : 10186 : && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8975 : : COMPARE_BASE | COMPARE_DERIVED)))
8976 : 3 : warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
8977 : : intype, type);
8978 : :
8979 : 10183 : expr = cp_build_addr_expr (expr, complain);
8980 : :
8981 : 10183 : if (warn_strict_aliasing > 2)
8982 : 69 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8983 : :
8984 : 10183 : if (expr != error_mark_node)
8985 : 10183 : expr = build_reinterpret_cast_1
8986 : 10183 : (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
8987 : : valid_p, complain);
8988 : 10183 : if (expr != error_mark_node)
8989 : : /* cp_build_indirect_ref isn't right for rvalue refs. */
8990 : 10180 : expr = convert_from_reference (fold_convert (type, expr));
8991 : 10183 : return expr;
8992 : : }
8993 : :
8994 : : /* As a G++ extension, we consider conversions from member
8995 : : functions, and pointers to member functions to
8996 : : pointer-to-function and pointer-to-void types. If
8997 : : -Wno-pmf-conversions has not been specified,
8998 : : convert_member_func_to_ptr will issue an error message. */
8999 : 265 : if ((TYPE_PTRMEMFUNC_P (intype)
9000 : 1681200 : || TREE_CODE (intype) == METHOD_TYPE)
9001 : 276 : && TYPE_PTR_P (type)
9002 : 1681585 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
9003 : 155 : || VOID_TYPE_P (TREE_TYPE (type))))
9004 : 188 : return convert_member_func_to_ptr (type, expr, complain);
9005 : :
9006 : : /* If the cast is not to a reference type, the lvalue-to-rvalue,
9007 : : array-to-pointer, and function-to-pointer conversions are
9008 : : performed. */
9009 : 1681200 : expr = decay_conversion (expr, complain);
9010 : :
9011 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9012 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9013 : 1681200 : if (TREE_CODE (expr) == NOP_EXPR
9014 : 1681200 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9015 : 31 : expr = TREE_OPERAND (expr, 0);
9016 : :
9017 : 1681200 : if (error_operand_p (expr))
9018 : 30 : return error_mark_node;
9019 : :
9020 : 1681170 : intype = TREE_TYPE (expr);
9021 : :
9022 : : /* [expr.reinterpret.cast]
9023 : : A pointer can be converted to any integral type large enough to
9024 : : hold it. ... A value of type std::nullptr_t can be converted to
9025 : : an integral type; the conversion has the same meaning and
9026 : : validity as a conversion of (void*)0 to the integral type. */
9027 : 1681170 : if (CP_INTEGRAL_TYPE_P (type)
9028 : 146592 : && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
9029 : : {
9030 : 144860 : if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
9031 : : {
9032 : 21 : if (complain & tf_error)
9033 : 15 : permerror (loc, "cast from %qH to %qI loses precision",
9034 : : intype, type);
9035 : : else
9036 : 6 : return error_mark_node;
9037 : : }
9038 : 144854 : if (NULLPTR_TYPE_P (intype))
9039 : 98 : return build_int_cst (type, 0);
9040 : : }
9041 : : /* [expr.reinterpret.cast]
9042 : : A value of integral or enumeration type can be explicitly
9043 : : converted to a pointer. */
9044 : 1536310 : else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
9045 : : /* OK */
9046 : : ;
9047 : 1500015 : else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
9048 : 1498268 : || TYPE_PTR_OR_PTRMEM_P (type))
9049 : 1598093 : && same_type_p (type, intype))
9050 : : /* DR 799 */
9051 : 534 : return rvalue (expr);
9052 : 1499481 : else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
9053 : : {
9054 : 6658 : if ((complain & tf_warning)
9055 : 13316 : && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
9056 : 6658 : TREE_TYPE (intype)))
9057 : 145 : warning_at (loc, OPT_Wcast_function_type,
9058 : : "cast between incompatible function types"
9059 : : " from %qH to %qI", intype, type);
9060 : 6658 : return build_nop_reinterpret (type, expr);
9061 : : }
9062 : 1492823 : else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
9063 : : {
9064 : 79 : if ((complain & tf_warning)
9065 : 158 : && !cxx_safe_function_type_cast_p
9066 : 79 : (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
9067 : 79 : TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
9068 : 52 : warning_at (loc, OPT_Wcast_function_type,
9069 : : "cast between incompatible pointer to member types"
9070 : : " from %qH to %qI", intype, type);
9071 : 79 : return build_nop_reinterpret (type, expr);
9072 : : }
9073 : 21 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
9074 : 1492744 : || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
9075 : : {
9076 : 90520 : if (!c_cast_p
9077 : 90520 : && check_for_casting_away_constness (loc, intype, type,
9078 : : REINTERPRET_CAST_EXPR,
9079 : : complain))
9080 : 21 : return error_mark_node;
9081 : : /* Warn about possible alignment problems. */
9082 : 90499 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9083 : 24 : && (complain & tf_warning)
9084 : 24 : && !VOID_TYPE_P (type)
9085 : 24 : && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
9086 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (type))
9087 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (intype))
9088 : 90547 : && min_align_of_type (TREE_TYPE (type))
9089 : 24 : > min_align_of_type (TREE_TYPE (intype)))
9090 : 18 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9091 : : "increases required alignment of target type",
9092 : : intype, type);
9093 : :
9094 : 90499 : if (warn_strict_aliasing <= 2)
9095 : : /* strict_aliasing_warning STRIP_NOPs its expr. */
9096 : 78978 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9097 : :
9098 : 90499 : return build_nop_reinterpret (type, expr);
9099 : : }
9100 : 297 : else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
9101 : 1402384 : || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
9102 : : {
9103 : 276 : if (complain & tf_warning)
9104 : : /* C++11 5.2.10 p8 says that "Converting a function pointer to an
9105 : : object pointer type or vice versa is conditionally-supported." */
9106 : 276 : warning_at (loc, OPT_Wconditionally_supported,
9107 : : "casting between pointer-to-function and "
9108 : : "pointer-to-object is conditionally-supported");
9109 : 276 : return build_nop_reinterpret (type, expr);
9110 : : }
9111 : 1401948 : else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
9112 : 1400166 : return convert_to_vector (type, rvalue (expr));
9113 : 1782 : else if (gnu_vector_type_p (intype)
9114 : 1782 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9115 : 1681 : return convert_to_integer_nofold (type, expr);
9116 : : else
9117 : : {
9118 : 101 : if (valid_p)
9119 : 83 : *valid_p = false;
9120 : 101 : if (complain & tf_error)
9121 : 92 : error_at (loc, "invalid cast from type %qT to type %qT",
9122 : : intype, type);
9123 : 101 : return error_mark_node;
9124 : : }
9125 : :
9126 : 181051 : expr = cp_convert (type, expr, complain);
9127 : 181051 : if (TREE_CODE (expr) == NOP_EXPR)
9128 : : /* Mark any nop_expr that created as a reintepret_cast. */
9129 : 0 : REINTERPRET_CAST_P (expr) = true;
9130 : : return expr;
9131 : : }
9132 : :
9133 : : tree
9134 : 552240 : build_reinterpret_cast (location_t loc, tree type, tree expr,
9135 : : tsubst_flags_t complain)
9136 : : {
9137 : 552240 : tree r;
9138 : :
9139 : 552240 : if (type == error_mark_node || expr == error_mark_node)
9140 : : return error_mark_node;
9141 : :
9142 : 552233 : if (processing_template_decl)
9143 : : {
9144 : 440781 : tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
9145 : :
9146 : 440781 : if (!TREE_SIDE_EFFECTS (t)
9147 : 440781 : && type_dependent_expression_p (expr))
9148 : : /* There might turn out to be side effects inside expr. */
9149 : 198553 : TREE_SIDE_EFFECTS (t) = 1;
9150 : 440781 : r = convert_from_reference (t);
9151 : 440781 : protected_set_expr_location (r, loc);
9152 : 440781 : return r;
9153 : : }
9154 : :
9155 : 111452 : r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9156 : : /*valid_p=*/NULL, complain);
9157 : 111452 : if (r != error_mark_node)
9158 : : {
9159 : 111389 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9160 : 111389 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9161 : : }
9162 : 111452 : protected_set_expr_location (r, loc);
9163 : 111452 : return r;
9164 : : }
9165 : :
9166 : : /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9167 : : return an appropriate expression. Otherwise, return
9168 : : error_mark_node. If the cast is not valid, and COMPLAIN is true,
9169 : : then a diagnostic will be issued. If VALID_P is non-NULL, we are
9170 : : performing a C-style cast, its value upon return will indicate
9171 : : whether or not the conversion succeeded. */
9172 : :
9173 : : static tree
9174 : 36135102 : build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9175 : : tsubst_flags_t complain, bool *valid_p)
9176 : : {
9177 : 36135102 : tree src_type;
9178 : 36135102 : tree reference_type;
9179 : :
9180 : : /* Callers are responsible for handling error_mark_node as a
9181 : : destination type. */
9182 : 36135102 : gcc_assert (dst_type != error_mark_node);
9183 : : /* In a template, callers should be building syntactic
9184 : : representations of casts, not using this machinery. */
9185 : 36135102 : gcc_assert (!processing_template_decl);
9186 : :
9187 : : /* Assume the conversion is invalid. */
9188 : 36135102 : if (valid_p)
9189 : 36004482 : *valid_p = false;
9190 : :
9191 : 36135102 : if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9192 : : {
9193 : 35494657 : if (complain & tf_error)
9194 : 9 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9195 : : "which is not a pointer, reference, "
9196 : : "nor a pointer-to-data-member type", dst_type);
9197 : 35494657 : return error_mark_node;
9198 : : }
9199 : :
9200 : 640445 : if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
9201 : : {
9202 : 3946 : if (complain & tf_error)
9203 : 6 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9204 : : "which is a pointer or reference to a function type",
9205 : : dst_type);
9206 : 3946 : return error_mark_node;
9207 : : }
9208 : :
9209 : : /* A prvalue of non-class type is cv-unqualified. */
9210 : 636499 : dst_type = cv_unqualified (dst_type);
9211 : :
9212 : : /* Save casted types in the function's used types hash table. */
9213 : 636499 : used_types_insert (dst_type);
9214 : :
9215 : 636499 : src_type = TREE_TYPE (expr);
9216 : : /* Expressions do not really have reference types. */
9217 : 636499 : if (TYPE_REF_P (src_type))
9218 : 0 : src_type = TREE_TYPE (src_type);
9219 : :
9220 : : /* [expr.const.cast]
9221 : :
9222 : : For two object types T1 and T2, if a pointer to T1 can be explicitly
9223 : : converted to the type "pointer to T2" using a const_cast, then the
9224 : : following conversions can also be made:
9225 : :
9226 : : -- an lvalue of type T1 can be explicitly converted to an lvalue of
9227 : : type T2 using the cast const_cast<T2&>;
9228 : :
9229 : : -- a glvalue of type T1 can be explicitly converted to an xvalue of
9230 : : type T2 using the cast const_cast<T2&&>; and
9231 : :
9232 : : -- if T1 is a class type, a prvalue of type T1 can be explicitly
9233 : : converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9234 : :
9235 : 636499 : if (TYPE_REF_P (dst_type))
9236 : : {
9237 : 62600 : reference_type = dst_type;
9238 : 62600 : if (!TYPE_REF_IS_RVALUE (dst_type)
9239 : 62600 : ? lvalue_p (expr)
9240 : 672 : : obvalue_p (expr))
9241 : : /* OK. */;
9242 : : else
9243 : : {
9244 : 35 : if (complain & tf_error)
9245 : 9 : error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
9246 : : "to type %qT",
9247 : : src_type, dst_type);
9248 : 35 : return error_mark_node;
9249 : : }
9250 : 62565 : dst_type = build_pointer_type (TREE_TYPE (dst_type));
9251 : 62565 : src_type = build_pointer_type (src_type);
9252 : : }
9253 : : else
9254 : : {
9255 : 573899 : reference_type = NULL_TREE;
9256 : : /* If the destination type is not a reference type, the
9257 : : lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9258 : : conversions are performed. */
9259 : 573899 : src_type = type_decays_to (src_type);
9260 : 573899 : if (src_type == error_mark_node)
9261 : : return error_mark_node;
9262 : : }
9263 : :
9264 : 636464 : if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9265 : : {
9266 : 486132 : if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9267 : : {
9268 : 248505 : if (valid_p)
9269 : : {
9270 : 117963 : *valid_p = true;
9271 : : /* This cast is actually a C-style cast. Issue a warning if
9272 : : the user is making a potentially unsafe cast. */
9273 : 117963 : check_for_casting_away_constness (loc, src_type, dst_type,
9274 : : CAST_EXPR, complain);
9275 : : /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9276 : 117963 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9277 : 3 : && (complain & tf_warning)
9278 : 117969 : && min_align_of_type (TREE_TYPE (dst_type))
9279 : 3 : > min_align_of_type (TREE_TYPE (src_type)))
9280 : 3 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9281 : : "increases required alignment of target type",
9282 : : src_type, dst_type);
9283 : : }
9284 : 248505 : if (reference_type)
9285 : : {
9286 : 62126 : expr = cp_build_addr_expr (expr, complain);
9287 : 62126 : if (expr == error_mark_node)
9288 : : return error_mark_node;
9289 : 62108 : expr = build_nop (reference_type, expr);
9290 : 62108 : return convert_from_reference (expr);
9291 : : }
9292 : : else
9293 : : {
9294 : 186379 : expr = decay_conversion (expr, complain);
9295 : 186379 : if (expr == error_mark_node)
9296 : : return error_mark_node;
9297 : :
9298 : : /* build_c_cast puts on a NOP_EXPR to make the result not an
9299 : : lvalue. Strip such NOP_EXPRs if VALUE is being used in
9300 : : non-lvalue context. */
9301 : 186379 : if (TREE_CODE (expr) == NOP_EXPR
9302 : 186379 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9303 : 3 : expr = TREE_OPERAND (expr, 0);
9304 : 186379 : return build_nop (dst_type, expr);
9305 : : }
9306 : : }
9307 : 237627 : else if (valid_p
9308 : 475206 : && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9309 : 237579 : TREE_TYPE (src_type)))
9310 : 12971 : check_for_casting_away_constness (loc, src_type, dst_type,
9311 : : CAST_EXPR, complain);
9312 : : }
9313 : :
9314 : 387959 : if (complain & tf_error)
9315 : 30 : error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9316 : : src_type, dst_type);
9317 : 387959 : return error_mark_node;
9318 : : }
9319 : :
9320 : : tree
9321 : 551025 : build_const_cast (location_t loc, tree type, tree expr,
9322 : : tsubst_flags_t complain)
9323 : : {
9324 : 551025 : tree r;
9325 : :
9326 : 551025 : if (type == error_mark_node || error_operand_p (expr))
9327 : : return error_mark_node;
9328 : :
9329 : 551017 : if (processing_template_decl)
9330 : : {
9331 : 420397 : tree t = build_min (CONST_CAST_EXPR, type, expr);
9332 : :
9333 : 420397 : if (!TREE_SIDE_EFFECTS (t)
9334 : 420397 : && type_dependent_expression_p (expr))
9335 : : /* There might turn out to be side effects inside expr. */
9336 : 329114 : TREE_SIDE_EFFECTS (t) = 1;
9337 : 420397 : r = convert_from_reference (t);
9338 : 420397 : protected_set_expr_location (r, loc);
9339 : 420397 : return r;
9340 : : }
9341 : :
9342 : 130620 : r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9343 : 130620 : if (r != error_mark_node)
9344 : : {
9345 : 130542 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9346 : 130542 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9347 : : }
9348 : 130620 : protected_set_expr_location (r, loc);
9349 : 130620 : return r;
9350 : : }
9351 : :
9352 : : /* Like cp_build_c_cast, but for the c-common bits. */
9353 : :
9354 : : tree
9355 : 0 : build_c_cast (location_t loc, tree type, tree expr)
9356 : : {
9357 : 0 : return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9358 : : }
9359 : :
9360 : : /* Like the "build_c_cast" used for c-common, but using cp_expr to
9361 : : preserve location information even for tree nodes that don't
9362 : : support it. */
9363 : :
9364 : : cp_expr
9365 : 8712102 : build_c_cast (location_t loc, tree type, cp_expr expr)
9366 : : {
9367 : 8712102 : cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9368 : 8712102 : result.set_location (loc);
9369 : 8712102 : return result;
9370 : : }
9371 : :
9372 : : /* Build an expression representing an explicit C-style cast to type
9373 : : TYPE of expression EXPR. */
9374 : :
9375 : : tree
9376 : 37888454 : cp_build_c_cast (location_t loc, tree type, tree expr,
9377 : : tsubst_flags_t complain)
9378 : : {
9379 : 37888454 : tree value = expr;
9380 : 37888454 : tree result;
9381 : 37888454 : bool valid_p;
9382 : :
9383 : 37888454 : if (type == error_mark_node || error_operand_p (expr))
9384 : : return error_mark_node;
9385 : :
9386 : 37888281 : if (processing_template_decl)
9387 : : {
9388 : 1883790 : tree t = build_min (CAST_EXPR, type,
9389 : : tree_cons (NULL_TREE, value, NULL_TREE));
9390 : : /* We don't know if it will or will not have side effects. */
9391 : 1883790 : TREE_SIDE_EFFECTS (t) = 1;
9392 : 1883790 : return convert_from_reference (t);
9393 : : }
9394 : :
9395 : : /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9396 : : 'Class') should always be retained, because this information aids
9397 : : in method lookup. */
9398 : 36004491 : if (objc_is_object_ptr (type)
9399 : 36004491 : && objc_is_object_ptr (TREE_TYPE (expr)))
9400 : 0 : return build_nop (type, expr);
9401 : :
9402 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9403 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9404 : 36004491 : if (!TYPE_REF_P (type)
9405 : 36003678 : && TREE_CODE (value) == NOP_EXPR
9406 : 36231877 : && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9407 : 84274 : value = TREE_OPERAND (value, 0);
9408 : :
9409 : 36004491 : if (TREE_CODE (type) == ARRAY_TYPE)
9410 : : {
9411 : : /* Allow casting from T1* to T2[] because Cfront allows it.
9412 : : NIHCL uses it. It is not valid ISO C++ however. */
9413 : 3 : if (TYPE_PTR_P (TREE_TYPE (expr)))
9414 : : {
9415 : 0 : if (complain & tf_error)
9416 : 0 : permerror (loc, "ISO C++ forbids casting to an array type %qT",
9417 : : type);
9418 : : else
9419 : 0 : return error_mark_node;
9420 : 0 : type = build_pointer_type (TREE_TYPE (type));
9421 : : }
9422 : : else
9423 : : {
9424 : 3 : if (complain & tf_error)
9425 : 3 : error_at (loc, "ISO C++ forbids casting to an array type %qT",
9426 : : type);
9427 : 3 : return error_mark_node;
9428 : : }
9429 : : }
9430 : :
9431 : 36004488 : if (FUNC_OR_METHOD_TYPE_P (type))
9432 : : {
9433 : 15 : if (complain & tf_error)
9434 : 15 : error_at (loc, "invalid cast to function type %qT", type);
9435 : 15 : return error_mark_node;
9436 : : }
9437 : :
9438 : 36004473 : if (TYPE_PTR_P (type)
9439 : 508877 : && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9440 : : /* Casting to an integer of smaller size is an error detected elsewhere. */
9441 : 148799 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9442 : : /* Don't warn about converting any constant. */
9443 : 36096121 : && !TREE_CONSTANT (value))
9444 : 47 : warning_at (loc, OPT_Wint_to_pointer_cast,
9445 : : "cast to pointer from integer of different size");
9446 : :
9447 : : /* A C-style cast can be a const_cast. */
9448 : 36004473 : result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9449 : : &valid_p);
9450 : 36004473 : if (valid_p)
9451 : : {
9452 : 117954 : if (result != error_mark_node)
9453 : : {
9454 : 117945 : maybe_warn_about_useless_cast (loc, type, value, complain);
9455 : 117945 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9456 : : }
9457 : 9 : else if (complain & tf_error)
9458 : 9 : build_const_cast_1 (loc, type, value, tf_error, &valid_p);
9459 : 117954 : return result;
9460 : : }
9461 : :
9462 : : /* Or a static cast. */
9463 : 35886519 : result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9464 : : &valid_p, complain);
9465 : : /* Or a reinterpret_cast. */
9466 : 35886519 : if (!valid_p)
9467 : 1569945 : result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9468 : : &valid_p, complain);
9469 : : /* The static_cast or reinterpret_cast may be followed by a
9470 : : const_cast. */
9471 : 35886519 : if (valid_p
9472 : : /* A valid cast may result in errors if, for example, a
9473 : : conversion to an ambiguous base class is required. */
9474 : 35886519 : && !error_operand_p (result))
9475 : : {
9476 : 35886148 : tree result_type;
9477 : :
9478 : 35886148 : maybe_warn_about_useless_cast (loc, type, value, complain);
9479 : 35886148 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9480 : :
9481 : : /* Non-class rvalues always have cv-unqualified type. */
9482 : 35886148 : if (!CLASS_TYPE_P (type))
9483 : 34471904 : type = TYPE_MAIN_VARIANT (type);
9484 : 35886148 : result_type = TREE_TYPE (result);
9485 : 35886148 : if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9486 : 34471443 : result_type = TYPE_MAIN_VARIANT (result_type);
9487 : : /* If the type of RESULT does not match TYPE, perform a
9488 : : const_cast to make it match. If the static_cast or
9489 : : reinterpret_cast succeeded, we will differ by at most
9490 : : cv-qualification, so the follow-on const_cast is guaranteed
9491 : : to succeed. */
9492 : 35886148 : if (!same_type_p (non_reference (type), non_reference (result_type)))
9493 : : {
9494 : 0 : result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
9495 : 0 : gcc_assert (valid_p);
9496 : : }
9497 : 35886148 : return result;
9498 : : }
9499 : :
9500 : 371 : return error_mark_node;
9501 : : }
9502 : :
9503 : : /* Warn when a value is moved to itself with std::move. LHS is the target,
9504 : : RHS may be the std::move call, and LOC is the location of the whole
9505 : : assignment. Return true if we warned. */
9506 : :
9507 : : bool
9508 : 24858175 : maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9509 : : {
9510 : 24858175 : if (!warn_self_move)
9511 : : return false;
9512 : :
9513 : : /* C++98 doesn't know move. */
9514 : 335697 : if (cxx_dialect < cxx11)
9515 : : return false;
9516 : :
9517 : 316131 : if (processing_template_decl)
9518 : : return false;
9519 : :
9520 : 20219 : if (!REFERENCE_REF_P (rhs)
9521 : 271517 : || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9522 : : return false;
9523 : 4262 : tree fn = TREE_OPERAND (rhs, 0);
9524 : 4262 : if (!is_std_move_p (fn))
9525 : : return false;
9526 : :
9527 : : /* Just a little helper to strip * and various NOPs. */
9528 : 5880 : auto extract_op = [] (tree &op) {
9529 : 3920 : STRIP_NOPS (op);
9530 : 5090 : while (INDIRECT_REF_P (op))
9531 : 1170 : op = TREE_OPERAND (op, 0);
9532 : 3920 : op = maybe_undo_parenthesized_ref (op);
9533 : 3920 : STRIP_ANY_LOCATION_WRAPPER (op);
9534 : 3920 : };
9535 : :
9536 : 1960 : tree arg = CALL_EXPR_ARG (fn, 0);
9537 : 1960 : extract_op (arg);
9538 : 1960 : if (TREE_CODE (arg) == ADDR_EXPR)
9539 : 1312 : arg = TREE_OPERAND (arg, 0);
9540 : 1960 : tree type = TREE_TYPE (lhs);
9541 : 1960 : tree orig_lhs = lhs;
9542 : 1960 : extract_op (lhs);
9543 : 1960 : if (cp_tree_equal (lhs, arg)
9544 : : /* Also warn in a member-initializer-list, as in : i(std::move(i)). */
9545 : 1960 : || (TREE_CODE (lhs) == FIELD_DECL
9546 : 515 : && TREE_CODE (arg) == COMPONENT_REF
9547 : 298 : && cp_tree_equal (TREE_OPERAND (arg, 0), current_class_ref)
9548 : 6 : && TREE_OPERAND (arg, 1) == lhs))
9549 : 117 : if (warning_at (loc, OPT_Wself_move,
9550 : : "moving %qE of type %qT to itself", orig_lhs, type))
9551 : : return true;
9552 : :
9553 : : return false;
9554 : : }
9555 : :
9556 : : /* For use from the C common bits. */
9557 : : tree
9558 : 4771 : build_modify_expr (location_t location,
9559 : : tree lhs, tree /*lhs_origtype*/,
9560 : : enum tree_code modifycode,
9561 : : location_t /*rhs_location*/, tree rhs,
9562 : : tree /*rhs_origtype*/)
9563 : : {
9564 : 4771 : return cp_build_modify_expr (location, lhs, modifycode, rhs,
9565 : 4771 : tf_warning_or_error);
9566 : : }
9567 : :
9568 : : /* Build an assignment expression of lvalue LHS from value RHS.
9569 : : MODIFYCODE is the code for a binary operator that we use
9570 : : to combine the old value of LHS with RHS to get the new value.
9571 : : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9572 : :
9573 : : C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9574 : :
9575 : : tree
9576 : 32287876 : cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9577 : : tree rhs, tsubst_flags_t complain)
9578 : : {
9579 : 32287876 : lhs = mark_lvalue_use_nonread (lhs);
9580 : :
9581 : 32287876 : tree result = NULL_TREE;
9582 : 32287876 : tree newrhs = rhs;
9583 : 32287876 : tree lhstype = TREE_TYPE (lhs);
9584 : 32287876 : tree olhs = lhs;
9585 : 32287876 : tree olhstype = lhstype;
9586 : 32287876 : bool plain_assign = (modifycode == NOP_EXPR);
9587 : 32287876 : bool compound_side_effects_p = false;
9588 : 32287876 : tree preeval = NULL_TREE;
9589 : :
9590 : : /* Avoid duplicate error messages from operands that had errors. */
9591 : 32287876 : if (error_operand_p (lhs) || error_operand_p (rhs))
9592 : 20 : return error_mark_node;
9593 : :
9594 : 32287935 : while (TREE_CODE (lhs) == COMPOUND_EXPR)
9595 : : {
9596 : 79 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9597 : 67 : compound_side_effects_p = true;
9598 : 79 : lhs = TREE_OPERAND (lhs, 1);
9599 : : }
9600 : :
9601 : : /* Handle control structure constructs used as "lvalues". Note that we
9602 : : leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9603 : 32287856 : switch (TREE_CODE (lhs))
9604 : : {
9605 : : /* Handle --foo = 5; as these are valid constructs in C++. */
9606 : 21 : case PREDECREMENT_EXPR:
9607 : 21 : case PREINCREMENT_EXPR:
9608 : 21 : if (compound_side_effects_p)
9609 : 9 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9610 : 21 : lhs = genericize_compound_lvalue (lhs);
9611 : 149 : maybe_add_compound:
9612 : : /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9613 : : and looked through the COMPOUND_EXPRs, readd them now around
9614 : : the resulting lhs. */
9615 : 149 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9616 : : {
9617 : 9 : lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9618 : 9 : tree *ptr = &TREE_OPERAND (lhs, 1);
9619 : 9 : for (olhs = TREE_OPERAND (olhs, 1);
9620 : 12 : TREE_CODE (olhs) == COMPOUND_EXPR;
9621 : 3 : olhs = TREE_OPERAND (olhs, 1))
9622 : : {
9623 : 3 : *ptr = build2 (COMPOUND_EXPR, lhstype,
9624 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9625 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9626 : : }
9627 : : }
9628 : : break;
9629 : :
9630 : 128 : case MODIFY_EXPR:
9631 : 128 : if (compound_side_effects_p)
9632 : 0 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9633 : 128 : lhs = genericize_compound_lvalue (lhs);
9634 : 128 : goto maybe_add_compound;
9635 : :
9636 : 0 : case MIN_EXPR:
9637 : 0 : case MAX_EXPR:
9638 : : /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9639 : : when neither operand has side-effects. */
9640 : 0 : if (!lvalue_or_else (lhs, lv_assign, complain))
9641 : 0 : return error_mark_node;
9642 : :
9643 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9644 : : && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9645 : :
9646 : 0 : lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9647 : 0 : build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9648 : : boolean_type_node,
9649 : 0 : TREE_OPERAND (lhs, 0),
9650 : 0 : TREE_OPERAND (lhs, 1)),
9651 : 0 : TREE_OPERAND (lhs, 0),
9652 : 0 : TREE_OPERAND (lhs, 1));
9653 : 200 : gcc_fallthrough ();
9654 : :
9655 : : /* Handle (a ? b : c) used as an "lvalue". */
9656 : 200 : case COND_EXPR:
9657 : 200 : {
9658 : : /* Produce (a ? (b = rhs) : (c = rhs))
9659 : : except that the RHS goes through a save-expr
9660 : : so the code to compute it is only emitted once. */
9661 : 200 : if (VOID_TYPE_P (TREE_TYPE (rhs)))
9662 : : {
9663 : 18 : if (complain & tf_error)
9664 : 24 : error_at (cp_expr_loc_or_loc (rhs, loc),
9665 : : "void value not ignored as it ought to be");
9666 : 18 : return error_mark_node;
9667 : : }
9668 : :
9669 : 182 : rhs = stabilize_expr (rhs, &preeval);
9670 : :
9671 : : /* Check this here to avoid odd errors when trying to convert
9672 : : a throw to the type of the COND_EXPR. */
9673 : 182 : if (!lvalue_or_else (lhs, lv_assign, complain))
9674 : 6 : return error_mark_node;
9675 : :
9676 : 176 : tree op1 = TREE_OPERAND (lhs, 1);
9677 : 176 : if (TREE_CODE (op1) != THROW_EXPR)
9678 : 170 : op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
9679 : : /* When sanitizing undefined behavior, even when rhs doesn't need
9680 : : stabilization at this point, the sanitization might add extra
9681 : : SAVE_EXPRs in there and so make sure there is no tree sharing
9682 : : in the rhs, otherwise those SAVE_EXPRs will have initialization
9683 : : only in one of the two branches. */
9684 : 176 : if (sanitize_flags_p (SANITIZE_UNDEFINED
9685 : : | SANITIZE_UNDEFINED_NONDEFAULT))
9686 : 3 : rhs = unshare_expr (rhs);
9687 : 176 : tree op2 = TREE_OPERAND (lhs, 2);
9688 : 176 : if (TREE_CODE (op2) != THROW_EXPR)
9689 : 170 : op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9690 : 176 : tree cond = build_conditional_expr (input_location,
9691 : 176 : TREE_OPERAND (lhs, 0), op1, op2,
9692 : : complain);
9693 : :
9694 : 176 : if (cond == error_mark_node)
9695 : : return cond;
9696 : : /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9697 : : and looked through the COMPOUND_EXPRs, readd them now around
9698 : : the resulting cond before adding the preevaluated rhs. */
9699 : 173 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9700 : : {
9701 : 15 : cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9702 : 15 : TREE_OPERAND (olhs, 0), cond);
9703 : 15 : tree *ptr = &TREE_OPERAND (cond, 1);
9704 : 15 : for (olhs = TREE_OPERAND (olhs, 1);
9705 : 18 : TREE_CODE (olhs) == COMPOUND_EXPR;
9706 : 3 : olhs = TREE_OPERAND (olhs, 1))
9707 : : {
9708 : 3 : *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9709 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9710 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9711 : : }
9712 : : }
9713 : : /* Make sure the code to compute the rhs comes out
9714 : : before the split. */
9715 : 173 : result = cond;
9716 : 173 : goto ret;
9717 : : }
9718 : :
9719 : : default:
9720 : : lhs = olhs;
9721 : : break;
9722 : : }
9723 : :
9724 : 32287656 : if (modifycode == INIT_EXPR)
9725 : : {
9726 : 3360487 : if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9727 : : /* Do the default thing. */;
9728 : 3180260 : else if (TREE_CODE (rhs) == CONSTRUCTOR)
9729 : : {
9730 : : /* Compound literal. */
9731 : 169 : if (! same_type_p (TREE_TYPE (rhs), lhstype))
9732 : : /* Call convert to generate an error; see PR 11063. */
9733 : 0 : rhs = convert (lhstype, rhs);
9734 : 169 : result = cp_build_init_expr (lhs, rhs);
9735 : 169 : TREE_SIDE_EFFECTS (result) = 1;
9736 : 169 : goto ret;
9737 : : }
9738 : 3180091 : else if (! MAYBE_CLASS_TYPE_P (lhstype))
9739 : : /* Do the default thing. */;
9740 : : else
9741 : : {
9742 : 43155 : releasing_vec rhs_vec = make_tree_vector_single (rhs);
9743 : 43155 : result = build_special_member_call (lhs, complete_ctor_identifier,
9744 : : &rhs_vec, lhstype, LOOKUP_NORMAL,
9745 : : complain);
9746 : 43155 : if (result == NULL_TREE)
9747 : 0 : return error_mark_node;
9748 : 43155 : goto ret;
9749 : 43155 : }
9750 : : }
9751 : : else
9752 : : {
9753 : 28927169 : lhs = require_complete_type (lhs, complain);
9754 : 28927169 : if (lhs == error_mark_node)
9755 : : return error_mark_node;
9756 : :
9757 : 28927126 : if (modifycode == NOP_EXPR)
9758 : : {
9759 : 24812378 : maybe_warn_self_move (loc, lhs, rhs);
9760 : :
9761 : 24812378 : if (c_dialect_objc ())
9762 : : {
9763 : 0 : result = objc_maybe_build_modify_expr (lhs, rhs);
9764 : 0 : if (result)
9765 : 0 : goto ret;
9766 : : }
9767 : :
9768 : : /* `operator=' is not an inheritable operator. */
9769 : 24812378 : if (! MAYBE_CLASS_TYPE_P (lhstype))
9770 : : /* Do the default thing. */;
9771 : : else
9772 : : {
9773 : 2366173 : result = build_new_op (input_location, MODIFY_EXPR,
9774 : : LOOKUP_NORMAL, lhs, rhs,
9775 : : make_node (NOP_EXPR), NULL_TREE,
9776 : : /*overload=*/NULL, complain);
9777 : 2366173 : if (result == NULL_TREE)
9778 : 0 : return error_mark_node;
9779 : 2366173 : goto ret;
9780 : : }
9781 : : lhstype = olhstype;
9782 : : }
9783 : : else
9784 : : {
9785 : 4114748 : tree init = NULL_TREE;
9786 : :
9787 : : /* A binary op has been requested. Combine the old LHS
9788 : : value with the RHS producing the value we should actually
9789 : : store into the LHS. */
9790 : 4114748 : gcc_assert (!((TYPE_REF_P (lhstype)
9791 : : && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9792 : : || MAYBE_CLASS_TYPE_P (lhstype)));
9793 : :
9794 : : /* Preevaluate the RHS to make sure its evaluation is complete
9795 : : before the lvalue-to-rvalue conversion of the LHS:
9796 : :
9797 : : [expr.ass] With respect to an indeterminately-sequenced
9798 : : function call, the operation of a compound assignment is a
9799 : : single evaluation. [ Note: Therefore, a function call shall
9800 : : not intervene between the lvalue-to-rvalue conversion and the
9801 : : side effect associated with any single compound assignment
9802 : : operator. -- end note ] */
9803 : 4114748 : lhs = cp_stabilize_reference (lhs);
9804 : 4114748 : rhs = decay_conversion (rhs, complain);
9805 : 4114748 : if (rhs == error_mark_node)
9806 : 123 : return error_mark_node;
9807 : :
9808 : 4114745 : {
9809 : 4114745 : auto_diagnostic_group d;
9810 : 4114745 : rhs = stabilize_expr (rhs, &init);
9811 : 4114745 : bool clear_decl_read = false;
9812 : 4114745 : tree stripped_lhs = tree_strip_any_location_wrapper (lhs);
9813 : 1023775 : if ((VAR_P (stripped_lhs) || TREE_CODE (stripped_lhs) == PARM_DECL)
9814 : 3511093 : && !DECL_READ_P (stripped_lhs)
9815 : 925487 : && (VAR_P (stripped_lhs) ? warn_unused_but_set_variable
9816 : : : warn_unused_but_set_parameter) > 2
9817 : 6293 : && !CLASS_TYPE_P (TREE_TYPE (lhs))
9818 : 4121038 : && !CLASS_TYPE_P (TREE_TYPE (rhs)))
9819 : : {
9820 : 6293 : mark_exp_read (rhs);
9821 : 6293 : if (!DECL_READ_P (stripped_lhs))
9822 : 4114745 : clear_decl_read = true;
9823 : : }
9824 : 4114745 : newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9825 : 4114745 : if (clear_decl_read)
9826 : 6293 : DECL_READ_P (stripped_lhs) = 0;
9827 : 4114745 : if (newrhs == error_mark_node)
9828 : : {
9829 : 120 : if (complain & tf_error)
9830 : 24 : inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9831 : 24 : modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9832 : 120 : return error_mark_node;
9833 : : }
9834 : 4114745 : }
9835 : :
9836 : 4114625 : if (init)
9837 : 281197 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9838 : :
9839 : : /* Now it looks like a plain assignment. */
9840 : 4114625 : modifycode = NOP_EXPR;
9841 : 4114625 : if (c_dialect_objc ())
9842 : : {
9843 : 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
9844 : 0 : if (result)
9845 : 0 : goto ret;
9846 : : }
9847 : : }
9848 : 26560830 : gcc_assert (!TYPE_REF_P (lhstype));
9849 : 26560830 : gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9850 : : }
9851 : :
9852 : : /* The left-hand side must be an lvalue. */
9853 : 29877993 : if (!lvalue_or_else (lhs, lv_assign, complain))
9854 : 166 : return error_mark_node;
9855 : :
9856 : : /* Warn about modifying something that is `const'. Don't warn if
9857 : : this is initialization. */
9858 : 29877827 : if (modifycode != INIT_EXPR
9859 : 29877827 : && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9860 : : /* Functions are not modifiable, even though they are
9861 : : lvalues. */
9862 : 26508993 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9863 : : /* If it's an aggregate and any field is const, then it is
9864 : : effectively const. */
9865 : 26508963 : || (CLASS_TYPE_P (lhstype)
9866 : 0 : && C_TYPE_FIELDS_READONLY (lhstype))))
9867 : : {
9868 : 51701 : if (complain & tf_error)
9869 : 84 : cxx_readonly_error (loc, lhs, lv_assign);
9870 : 51701 : return error_mark_node;
9871 : : }
9872 : :
9873 : : /* If storing into a structure or union member, it may have been given a
9874 : : lowered bitfield type. We need to convert to the declared type first,
9875 : : so retrieve it now. */
9876 : :
9877 : 29826126 : olhstype = unlowered_expr_type (lhs);
9878 : :
9879 : : /* Convert new value to destination type. */
9880 : :
9881 : 29826126 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
9882 : : {
9883 : 185 : int from_array;
9884 : :
9885 : 185 : if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9886 : : {
9887 : 12 : if (modifycode != INIT_EXPR)
9888 : : {
9889 : 12 : if (complain & tf_error)
9890 : 12 : error_at (loc,
9891 : : "assigning to an array from an initializer list");
9892 : 12 : return error_mark_node;
9893 : : }
9894 : 0 : if (check_array_initializer (lhs, lhstype, newrhs))
9895 : 0 : return error_mark_node;
9896 : 0 : newrhs = digest_init (lhstype, newrhs, complain);
9897 : 0 : if (newrhs == error_mark_node)
9898 : : return error_mark_node;
9899 : : }
9900 : :
9901 : : /* C++11 8.5/17: "If the destination type is an array of characters,
9902 : : an array of char16_t, an array of char32_t, or an array of wchar_t,
9903 : : and the initializer is a string literal...". */
9904 : 173 : else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9905 : : == STRING_CST)
9906 : 43 : && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9907 : 216 : && modifycode == INIT_EXPR)
9908 : : {
9909 : 28 : newrhs = digest_init (lhstype, newrhs, complain);
9910 : 28 : if (newrhs == error_mark_node)
9911 : : return error_mark_node;
9912 : : }
9913 : :
9914 : 145 : else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9915 : : TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9916 : : {
9917 : 20 : if (complain & tf_error)
9918 : 15 : error_at (loc, "incompatible types in assignment of %qT to %qT",
9919 : 15 : TREE_TYPE (rhs), lhstype);
9920 : 20 : return error_mark_node;
9921 : : }
9922 : :
9923 : : /* Allow array assignment in compiler-generated code. */
9924 : 125 : else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
9925 : : /* OK, used by coroutines (co-await-initlist1.C). */;
9926 : 119 : else if (!current_function_decl
9927 : 119 : || !DECL_DEFAULTED_FN (current_function_decl))
9928 : : {
9929 : : /* This routine is used for both initialization and assignment.
9930 : : Make sure the diagnostic message differentiates the context. */
9931 : 72 : if (complain & tf_error)
9932 : : {
9933 : 30 : if (modifycode == INIT_EXPR)
9934 : 9 : error_at (loc, "array used as initializer");
9935 : : else
9936 : 21 : error_at (loc, "invalid array assignment");
9937 : : }
9938 : 72 : return error_mark_node;
9939 : : }
9940 : :
9941 : 128 : from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9942 : 81 : ? 1 + (modifycode != INIT_EXPR) : 0;
9943 : 81 : result = build_vec_init (lhs, NULL_TREE, newrhs,
9944 : : /*explicit_value_init_p=*/false,
9945 : : from_array, complain);
9946 : 81 : goto ret;
9947 : : }
9948 : :
9949 : 29825941 : if (modifycode == INIT_EXPR)
9950 : : /* Calls with INIT_EXPR are all direct-initialization, so don't set
9951 : : LOOKUP_ONLYCONVERTING. */
9952 : 3317101 : newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9953 : : ICR_INIT, NULL_TREE, 0,
9954 : : complain | tf_no_cleanup);
9955 : : else
9956 : 26508840 : newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9957 : : NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9958 : :
9959 : 29825941 : if (!same_type_p (lhstype, olhstype))
9960 : 245933 : newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9961 : :
9962 : 29825941 : if (modifycode != INIT_EXPR)
9963 : : {
9964 : 26508840 : if (TREE_CODE (newrhs) == CALL_EXPR
9965 : 26508840 : && TYPE_NEEDS_CONSTRUCTING (lhstype))
9966 : 0 : newrhs = build_cplus_new (lhstype, newrhs, complain);
9967 : :
9968 : : /* Can't initialize directly from a TARGET_EXPR, since that would
9969 : : cause the lhs to be constructed twice, and possibly result in
9970 : : accidental self-initialization. So we force the TARGET_EXPR to be
9971 : : expanded without a target. */
9972 : 26508840 : if (TREE_CODE (newrhs) == TARGET_EXPR)
9973 : 84 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9974 : 84 : TARGET_EXPR_SLOT (newrhs));
9975 : : }
9976 : :
9977 : 29825941 : if (newrhs == error_mark_node)
9978 : : return error_mark_node;
9979 : :
9980 : 29825373 : if (c_dialect_objc () && flag_objc_gc)
9981 : : {
9982 : 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
9983 : :
9984 : 0 : if (result)
9985 : 0 : goto ret;
9986 : : }
9987 : :
9988 : 33142425 : result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
9989 : : lhstype, lhs, newrhs);
9990 : 29825373 : if (modifycode == INIT_EXPR)
9991 : 3317052 : set_target_expr_eliding (newrhs);
9992 : :
9993 : 29825373 : TREE_SIDE_EFFECTS (result) = 1;
9994 : 29825373 : if (!plain_assign)
9995 : 7431647 : suppress_warning (result, OPT_Wparentheses);
9996 : :
9997 : 22393726 : ret:
9998 : 32235124 : if (preeval)
9999 : 33 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
10000 : : return result;
10001 : : }
10002 : :
10003 : : cp_expr
10004 : 59443857 : build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
10005 : : tree rhs, tree lookups, tsubst_flags_t complain)
10006 : : {
10007 : 59443857 : tree orig_lhs = lhs;
10008 : 59443857 : tree orig_rhs = rhs;
10009 : 59443857 : tree overload = NULL_TREE;
10010 : :
10011 : 59443857 : if (lhs == error_mark_node || rhs == error_mark_node)
10012 : 2069 : return cp_expr (error_mark_node, loc);
10013 : :
10014 : 59441788 : tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
10015 : :
10016 : 59441788 : if (processing_template_decl)
10017 : : {
10018 : 40159562 : if (type_dependent_expression_p (lhs)
10019 : 40159562 : || type_dependent_expression_p (rhs))
10020 : : {
10021 : 30891970 : tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
10022 : 30891970 : if (modifycode != NOP_EXPR)
10023 : 5777467 : TREE_TYPE (rval)
10024 : 11554934 : = build_dependent_operator_type (lookups, modifycode, true);
10025 : 30891970 : return rval;
10026 : : }
10027 : : }
10028 : :
10029 : 28549818 : tree rval;
10030 : 28549818 : if (modifycode == NOP_EXPR)
10031 : 22743626 : rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
10032 : : else
10033 : 5806192 : rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
10034 : : lhs, rhs, op, lookups, &overload, complain);
10035 : 28549818 : if (rval == error_mark_node)
10036 : 1841 : return error_mark_node;
10037 : 28547977 : if (processing_template_decl)
10038 : : {
10039 : 9267571 : if (overload != NULL_TREE)
10040 : 1252082 : return (build_min_non_dep_op_overload
10041 : 1252082 : (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
10042 : :
10043 : 8015489 : return (build_min_non_dep
10044 : 8015489 : (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
10045 : : }
10046 : 19280406 : return rval;
10047 : : }
10048 : :
10049 : : /* Helper function for get_delta_difference which assumes FROM is a base
10050 : : class of TO. Returns a delta for the conversion of pointer-to-member
10051 : : of FROM to pointer-to-member of TO. If the conversion is invalid and
10052 : : tf_error is not set in COMPLAIN returns error_mark_node, otherwise
10053 : : returns zero. If FROM is not a base class of TO, returns NULL_TREE.
10054 : : If C_CAST_P is true, this conversion is taking place as part of a
10055 : : C-style cast. */
10056 : :
10057 : : static tree
10058 : 30206 : get_delta_difference_1 (tree from, tree to, bool c_cast_p,
10059 : : tsubst_flags_t complain)
10060 : : {
10061 : 30206 : tree binfo;
10062 : 30206 : base_kind kind;
10063 : :
10064 : 60162 : binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
10065 : : &kind, complain);
10066 : :
10067 : 30206 : if (binfo == error_mark_node)
10068 : : {
10069 : 51 : if (!(complain & tf_error))
10070 : : return error_mark_node;
10071 : :
10072 : 51 : inform (input_location, " in pointer to member function conversion");
10073 : 51 : return size_zero_node;
10074 : : }
10075 : 30155 : else if (binfo)
10076 : : {
10077 : 30007 : if (kind != bk_via_virtual)
10078 : 29914 : return BINFO_OFFSET (binfo);
10079 : : else
10080 : : /* FROM is a virtual base class of TO. Issue an error or warning
10081 : : depending on whether or not this is a reinterpret cast. */
10082 : : {
10083 : 93 : if (!(complain & tf_error))
10084 : : return error_mark_node;
10085 : :
10086 : 78 : error ("pointer to member conversion via virtual base %qT",
10087 : 78 : BINFO_TYPE (binfo_from_vbase (binfo)));
10088 : :
10089 : 78 : return size_zero_node;
10090 : : }
10091 : : }
10092 : : else
10093 : : return NULL_TREE;
10094 : : }
10095 : :
10096 : : /* Get difference in deltas for different pointer to member function
10097 : : types. If the conversion is invalid and tf_error is not set in
10098 : : COMPLAIN, returns error_mark_node, otherwise returns an integer
10099 : : constant of type PTRDIFF_TYPE_NODE and its value is zero if the
10100 : : conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
10101 : : conversions as well. If C_CAST_P is true this conversion is taking
10102 : : place as part of a C-style cast.
10103 : :
10104 : : Note that the naming of FROM and TO is kind of backwards; the return
10105 : : value is what we add to a TO in order to get a FROM. They are named
10106 : : this way because we call this function to find out how to convert from
10107 : : a pointer to member of FROM to a pointer to member of TO. */
10108 : :
10109 : : static tree
10110 : 93827 : get_delta_difference (tree from, tree to,
10111 : : bool allow_inverse_p,
10112 : : bool c_cast_p, tsubst_flags_t complain)
10113 : : {
10114 : 93827 : auto_diagnostic_group d;
10115 : 93827 : tree result;
10116 : :
10117 : 93827 : if (same_type_ignoring_top_level_qualifiers_p (from, to))
10118 : : /* Pointer to member of incomplete class is permitted*/
10119 : 63769 : result = size_zero_node;
10120 : : else
10121 : 30058 : result = get_delta_difference_1 (from, to, c_cast_p, complain);
10122 : :
10123 : 93827 : if (result == error_mark_node)
10124 : : return error_mark_node;
10125 : :
10126 : 93812 : if (!result)
10127 : : {
10128 : 148 : if (!allow_inverse_p)
10129 : : {
10130 : 0 : if (!(complain & tf_error))
10131 : : return error_mark_node;
10132 : :
10133 : 0 : error_not_base_type (from, to);
10134 : 0 : inform (input_location, " in pointer to member conversion");
10135 : 0 : result = size_zero_node;
10136 : : }
10137 : : else
10138 : : {
10139 : 148 : result = get_delta_difference_1 (to, from, c_cast_p, complain);
10140 : :
10141 : 148 : if (result == error_mark_node)
10142 : : return error_mark_node;
10143 : :
10144 : 148 : if (result)
10145 : 148 : result = size_diffop_loc (input_location,
10146 : : size_zero_node, result);
10147 : : else
10148 : : {
10149 : 0 : if (!(complain & tf_error))
10150 : : return error_mark_node;
10151 : :
10152 : 0 : error_not_base_type (from, to);
10153 : 0 : inform (input_location, " in pointer to member conversion");
10154 : 0 : result = size_zero_node;
10155 : : }
10156 : : }
10157 : : }
10158 : :
10159 : 93812 : return convert_to_integer (ptrdiff_type_node, result);
10160 : 93827 : }
10161 : :
10162 : : /* Return a constructor for the pointer-to-member-function TYPE using
10163 : : the other components as specified. */
10164 : :
10165 : : tree
10166 : 64155 : build_ptrmemfunc1 (tree type, tree delta, tree pfn)
10167 : : {
10168 : 64155 : tree u = NULL_TREE;
10169 : 64155 : tree delta_field;
10170 : 64155 : tree pfn_field;
10171 : 64155 : vec<constructor_elt, va_gc> *v;
10172 : :
10173 : : /* Pull the FIELD_DECLs out of the type. */
10174 : 64155 : pfn_field = TYPE_FIELDS (type);
10175 : 64155 : delta_field = DECL_CHAIN (pfn_field);
10176 : :
10177 : : /* Make sure DELTA has the type we want. */
10178 : 64155 : delta = convert_and_check (input_location, delta_type_node, delta);
10179 : :
10180 : : /* Convert to the correct target type if necessary. */
10181 : 64155 : pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10182 : :
10183 : : /* Finish creating the initializer. */
10184 : 64155 : vec_alloc (v, 2);
10185 : 64155 : CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10186 : 64155 : CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10187 : 64155 : u = build_constructor (type, v);
10188 : 64155 : TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10189 : 64155 : TREE_STATIC (u) = (TREE_CONSTANT (u)
10190 : 64053 : && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10191 : : != NULL_TREE)
10192 : 128208 : && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10193 : : != NULL_TREE));
10194 : 64155 : return u;
10195 : : }
10196 : :
10197 : : /* Build a constructor for a pointer to member function. It can be
10198 : : used to initialize global variables, local variable, or used
10199 : : as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10200 : : want to be.
10201 : :
10202 : : If FORCE is nonzero, then force this conversion, even if
10203 : : we would rather not do it. Usually set when using an explicit
10204 : : cast. A C-style cast is being processed iff C_CAST_P is true.
10205 : :
10206 : : Return error_mark_node, if something goes wrong. */
10207 : :
10208 : : tree
10209 : 31577 : build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10210 : : tsubst_flags_t complain)
10211 : : {
10212 : 31577 : tree fn;
10213 : 31577 : tree pfn_type;
10214 : 31577 : tree to_type;
10215 : :
10216 : 31577 : if (error_operand_p (pfn))
10217 : 0 : return error_mark_node;
10218 : :
10219 : 31577 : pfn_type = TREE_TYPE (pfn);
10220 : 31577 : to_type = build_ptrmemfunc_type (type);
10221 : :
10222 : : /* Handle multiple conversions of pointer to member functions. */
10223 : 31577 : if (TYPE_PTRMEMFUNC_P (pfn_type))
10224 : : {
10225 : 29765 : tree delta = NULL_TREE;
10226 : 29765 : tree npfn = NULL_TREE;
10227 : 29765 : tree n;
10228 : :
10229 : 29765 : if (!force
10230 : 29765 : && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
10231 : : LOOKUP_NORMAL, complain))
10232 : : {
10233 : 0 : if (complain & tf_error)
10234 : 0 : error ("invalid conversion to type %qT from type %qT",
10235 : : to_type, pfn_type);
10236 : : else
10237 : 0 : return error_mark_node;
10238 : : }
10239 : :
10240 : 29765 : n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10241 : 29765 : TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10242 : : force,
10243 : : c_cast_p, complain);
10244 : 29765 : if (n == error_mark_node)
10245 : : return error_mark_node;
10246 : :
10247 : 29756 : STRIP_ANY_LOCATION_WRAPPER (pfn);
10248 : :
10249 : : /* We don't have to do any conversion to convert a
10250 : : pointer-to-member to its own type. But, we don't want to
10251 : : just return a PTRMEM_CST if there's an explicit cast; that
10252 : : cast should make the expression an invalid template argument. */
10253 : 29756 : if (TREE_CODE (pfn) != PTRMEM_CST
10254 : 29756 : && same_type_p (to_type, pfn_type))
10255 : : return pfn;
10256 : :
10257 : 29756 : if (TREE_SIDE_EFFECTS (pfn))
10258 : 8 : pfn = save_expr (pfn);
10259 : :
10260 : : /* Obtain the function pointer and the current DELTA. */
10261 : 29756 : if (TREE_CODE (pfn) == PTRMEM_CST)
10262 : 29668 : expand_ptrmemfunc_cst (pfn, &delta, &npfn);
10263 : : else
10264 : : {
10265 : 88 : npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
10266 : 88 : delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
10267 : : }
10268 : :
10269 : : /* Just adjust the DELTA field. */
10270 : 29756 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10271 : : (TREE_TYPE (delta), ptrdiff_type_node));
10272 : 29756 : if (!integer_zerop (n))
10273 : : {
10274 : 50 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
10275 : : n = cp_build_binary_op (input_location,
10276 : : LSHIFT_EXPR, n, integer_one_node,
10277 : : complain);
10278 : 50 : delta = cp_build_binary_op (input_location,
10279 : : PLUS_EXPR, delta, n, complain);
10280 : : }
10281 : 29756 : return build_ptrmemfunc1 (to_type, delta, npfn);
10282 : : }
10283 : :
10284 : : /* Handle null pointer to member function conversions. */
10285 : 1812 : if (null_ptr_cst_p (pfn))
10286 : : {
10287 : 749 : pfn = cp_build_c_cast (input_location,
10288 : 749 : TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10289 : : pfn, complain);
10290 : 749 : return build_ptrmemfunc1 (to_type,
10291 : : integer_zero_node,
10292 : 749 : pfn);
10293 : : }
10294 : :
10295 : 1063 : if (type_unknown_p (pfn))
10296 : 0 : return instantiate_type (type, pfn, complain);
10297 : :
10298 : 1063 : fn = TREE_OPERAND (pfn, 0);
10299 : 1063 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10300 : : /* In a template, we will have preserved the
10301 : : OFFSET_REF. */
10302 : : || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10303 : 1063 : return make_ptrmem_cst (to_type, fn);
10304 : : }
10305 : :
10306 : : /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10307 : : given by CST.
10308 : :
10309 : : ??? There is no consistency as to the types returned for the above
10310 : : values. Some code acts as if it were a sizetype and some as if it were
10311 : : integer_type_node. */
10312 : :
10313 : : void
10314 : 63698 : expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10315 : : {
10316 : 63698 : tree type = TREE_TYPE (cst);
10317 : 63698 : tree fn = PTRMEM_CST_MEMBER (cst);
10318 : 63698 : tree ptr_class, fn_class;
10319 : :
10320 : 63698 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10321 : :
10322 : : /* The class that the function belongs to. */
10323 : 63698 : fn_class = DECL_CONTEXT (fn);
10324 : :
10325 : : /* The class that we're creating a pointer to member of. */
10326 : 63698 : ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10327 : :
10328 : : /* First, calculate the adjustment to the function's class. */
10329 : 63698 : *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10330 : : /*c_cast_p=*/0, tf_warning_or_error);
10331 : :
10332 : 63698 : if (!DECL_VIRTUAL_P (fn))
10333 : : {
10334 : 62470 : tree t = build_addr_func (fn, tf_warning_or_error);
10335 : 62470 : if (TREE_CODE (t) == ADDR_EXPR)
10336 : 62470 : SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10337 : 62470 : *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10338 : : }
10339 : : else
10340 : : {
10341 : : /* If we're dealing with a virtual function, we have to adjust 'this'
10342 : : again, to point to the base which provides the vtable entry for
10343 : : fn; the call will do the opposite adjustment. */
10344 : 1228 : tree orig_class = DECL_CONTEXT (fn);
10345 : 1228 : tree binfo = binfo_or_else (orig_class, fn_class);
10346 : 1228 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10347 : : *delta, BINFO_OFFSET (binfo));
10348 : :
10349 : : /* We set PFN to the vtable offset at which the function can be
10350 : : found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10351 : : case delta is shifted left, and then incremented). */
10352 : 1228 : *pfn = DECL_VINDEX (fn);
10353 : 1228 : *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10354 : : TYPE_SIZE_UNIT (vtable_entry_type));
10355 : :
10356 : 1228 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10357 : : {
10358 : 1228 : case ptrmemfunc_vbit_in_pfn:
10359 : 1228 : *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10360 : : integer_one_node);
10361 : 1228 : break;
10362 : :
10363 : : case ptrmemfunc_vbit_in_delta:
10364 : : *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10365 : : *delta, integer_one_node);
10366 : : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10367 : : *delta, integer_one_node);
10368 : : break;
10369 : :
10370 : : default:
10371 : : gcc_unreachable ();
10372 : : }
10373 : :
10374 : 1228 : *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10375 : : }
10376 : 63698 : }
10377 : :
10378 : : /* Return an expression for PFN from the pointer-to-member function
10379 : : given by T. */
10380 : :
10381 : : static tree
10382 : 90353 : pfn_from_ptrmemfunc (tree t)
10383 : : {
10384 : 90353 : if (TREE_CODE (t) == PTRMEM_CST)
10385 : : {
10386 : 190 : tree delta;
10387 : 190 : tree pfn;
10388 : :
10389 : 190 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10390 : 190 : if (pfn)
10391 : 190 : return pfn;
10392 : : }
10393 : :
10394 : 90163 : return build_ptrmemfunc_access_expr (t, pfn_identifier);
10395 : : }
10396 : :
10397 : : /* Return an expression for DELTA from the pointer-to-member function
10398 : : given by T. */
10399 : :
10400 : : static tree
10401 : 90353 : delta_from_ptrmemfunc (tree t)
10402 : : {
10403 : 90353 : if (TREE_CODE (t) == PTRMEM_CST)
10404 : : {
10405 : 190 : tree delta;
10406 : 190 : tree pfn;
10407 : :
10408 : 190 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10409 : 190 : if (delta)
10410 : 190 : return delta;
10411 : : }
10412 : :
10413 : 90163 : return build_ptrmemfunc_access_expr (t, delta_identifier);
10414 : : }
10415 : :
10416 : : /* Convert value RHS to type TYPE as preparation for an assignment to
10417 : : an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10418 : : implicit conversion is. If FNDECL is non-NULL, we are doing the
10419 : : conversion in order to pass the PARMNUMth argument of FNDECL.
10420 : : If FNDECL is NULL, we are doing the conversion in function pointer
10421 : : argument passing, conversion in initialization, etc. */
10422 : :
10423 : : static tree
10424 : 121200754 : convert_for_assignment (tree type, tree rhs,
10425 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10426 : : tsubst_flags_t complain, int flags)
10427 : : {
10428 : 121200754 : tree rhstype;
10429 : 121200754 : enum tree_code coder;
10430 : :
10431 : 121200754 : location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10432 : 121200754 : bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10433 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10434 : : but preserve location wrappers. */
10435 : 121200754 : if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10436 : 121200754 : && !location_wrapper_p (rhs))
10437 : 7110 : rhs = TREE_OPERAND (rhs, 0);
10438 : :
10439 : : /* Handle [dcl.init.list] direct-list-initialization from
10440 : : single element of enumeration with a fixed underlying type. */
10441 : 121200754 : if (is_direct_enum_init (type, rhs))
10442 : : {
10443 : 20 : tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10444 : 20 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10445 : : {
10446 : 11 : warning_sentinel w (warn_useless_cast);
10447 : 11 : warning_sentinel w2 (warn_ignored_qualifiers);
10448 : 11 : rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10449 : 11 : }
10450 : : else
10451 : 9 : rhs = error_mark_node;
10452 : : }
10453 : :
10454 : 121200754 : rhstype = TREE_TYPE (rhs);
10455 : 121200754 : coder = TREE_CODE (rhstype);
10456 : :
10457 : 1168804 : if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10458 : 122369452 : && vector_types_convertible_p (type, rhstype, true))
10459 : : {
10460 : 1168689 : rhs = mark_rvalue_use (rhs);
10461 : 1168689 : return convert (type, rhs);
10462 : : }
10463 : :
10464 : 120032065 : if (rhs == error_mark_node || rhstype == error_mark_node)
10465 : : return error_mark_node;
10466 : 120032056 : if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10467 : : return error_mark_node;
10468 : :
10469 : : /* The RHS of an assignment cannot have void type. */
10470 : 120032056 : if (coder == VOID_TYPE)
10471 : : {
10472 : 48 : if (complain & tf_error)
10473 : 30 : error_at (rhs_loc, "void value not ignored as it ought to be");
10474 : 48 : return error_mark_node;
10475 : : }
10476 : :
10477 : 120032008 : if (c_dialect_objc ())
10478 : : {
10479 : 0 : int parmno;
10480 : 0 : tree selector;
10481 : 0 : tree rname = fndecl;
10482 : :
10483 : 0 : switch (errtype)
10484 : : {
10485 : : case ICR_ASSIGN:
10486 : : parmno = -1;
10487 : : break;
10488 : 0 : case ICR_INIT:
10489 : 0 : parmno = -2;
10490 : 0 : break;
10491 : 0 : default:
10492 : 0 : selector = objc_message_selector ();
10493 : 0 : parmno = parmnum;
10494 : 0 : if (selector && parmno > 1)
10495 : : {
10496 : 0 : rname = selector;
10497 : 0 : parmno -= 1;
10498 : : }
10499 : : }
10500 : :
10501 : 0 : if (objc_compare_types (type, rhstype, parmno, rname))
10502 : : {
10503 : 0 : rhs = mark_rvalue_use (rhs);
10504 : 0 : return convert (type, rhs);
10505 : : }
10506 : : }
10507 : :
10508 : : /* [expr.ass]
10509 : :
10510 : : The expression is implicitly converted (clause _conv_) to the
10511 : : cv-unqualified type of the left operand.
10512 : :
10513 : : We allow bad conversions here because by the time we get to this point
10514 : : we are committed to doing the conversion. If we end up doing a bad
10515 : : conversion, convert_like will complain. */
10516 : 120032008 : if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10517 : : {
10518 : : /* When -Wno-pmf-conversions is use, we just silently allow
10519 : : conversions from pointers-to-members to plain pointers. If
10520 : : the conversion doesn't work, cp_convert will complain. */
10521 : 1467 : if (!warn_pmf2ptr
10522 : 9 : && TYPE_PTR_P (type)
10523 : 1476 : && TYPE_PTRMEMFUNC_P (rhstype))
10524 : 9 : rhs = cp_convert (strip_top_quals (type), rhs, complain);
10525 : : else
10526 : : {
10527 : 1458 : if (complain & tf_error)
10528 : : {
10529 : 1271 : auto_diagnostic_group d;
10530 : :
10531 : : /* If the right-hand side has unknown type, then it is an
10532 : : overloaded function. Call instantiate_type to get error
10533 : : messages. */
10534 : 1271 : if (rhstype == unknown_type_node)
10535 : : {
10536 : 197 : tree r = instantiate_type (type, rhs, tf_warning_or_error);
10537 : : /* -fpermissive might allow this; recurse. */
10538 : 197 : if (!seen_error ())
10539 : 15 : return convert_for_assignment (type, r, errtype, fndecl,
10540 : : parmnum, complain, flags);
10541 : : }
10542 : 1074 : else if (fndecl)
10543 : 92 : complain_about_bad_argument (rhs_loc,
10544 : : rhstype, type,
10545 : : fndecl, parmnum);
10546 : : else
10547 : : {
10548 : 982 : range_label_for_type_mismatch label (rhstype, type);
10549 : 982 : gcc_rich_location richloc
10550 : : (rhs_loc,
10551 : : has_loc ? &label : NULL,
10552 : 982 : has_loc ? highlight_colors::percent_h : NULL);
10553 : :
10554 : 982 : switch (errtype)
10555 : : {
10556 : 0 : case ICR_DEFAULT_ARGUMENT:
10557 : 0 : error_at (&richloc,
10558 : : "cannot convert %qH to %qI in default argument",
10559 : : rhstype, type);
10560 : 0 : break;
10561 : 6 : case ICR_ARGPASS:
10562 : 6 : error_at (&richloc,
10563 : : "cannot convert %qH to %qI in argument passing",
10564 : : rhstype, type);
10565 : 6 : break;
10566 : 0 : case ICR_CONVERTING:
10567 : 0 : error_at (&richloc, "cannot convert %qH to %qI",
10568 : : rhstype, type);
10569 : 0 : break;
10570 : 544 : case ICR_INIT:
10571 : 544 : error_at (&richloc,
10572 : : "cannot convert %qH to %qI in initialization",
10573 : : rhstype, type);
10574 : 544 : break;
10575 : 18 : case ICR_RETURN:
10576 : 18 : error_at (&richloc, "cannot convert %qH to %qI in return",
10577 : : rhstype, type);
10578 : 18 : break;
10579 : 414 : case ICR_ASSIGN:
10580 : 414 : error_at (&richloc,
10581 : : "cannot convert %qH to %qI in assignment",
10582 : : rhstype, type);
10583 : 414 : break;
10584 : 0 : default:
10585 : 0 : gcc_unreachable();
10586 : : }
10587 : 982 : }
10588 : :
10589 : : /* See if we can be more helpful. */
10590 : 1256 : maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
10591 : :
10592 : 1256 : if (TYPE_PTR_P (rhstype)
10593 : 411 : && TYPE_PTR_P (type)
10594 : 396 : && CLASS_TYPE_P (TREE_TYPE (rhstype))
10595 : 278 : && CLASS_TYPE_P (TREE_TYPE (type))
10596 : 1318 : && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10597 : 3 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10598 : : (TREE_TYPE (rhstype))),
10599 : 3 : "class type %qT is incomplete", TREE_TYPE (rhstype));
10600 : 1271 : }
10601 : 1443 : return error_mark_node;
10602 : : }
10603 : : }
10604 : 120030550 : if (warn_suggest_attribute_format)
10605 : : {
10606 : 2363 : const enum tree_code codel = TREE_CODE (type);
10607 : 2363 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10608 : 592 : && coder == codel
10609 : 504 : && check_missing_format_attribute (type, rhstype)
10610 : 2381 : && (complain & tf_warning))
10611 : 18 : switch (errtype)
10612 : : {
10613 : 0 : case ICR_ARGPASS:
10614 : 0 : case ICR_DEFAULT_ARGUMENT:
10615 : 0 : if (fndecl)
10616 : 0 : warning (OPT_Wsuggest_attribute_format,
10617 : : "parameter %qP of %qD might be a candidate "
10618 : : "for a format attribute", parmnum, fndecl);
10619 : : else
10620 : 0 : warning (OPT_Wsuggest_attribute_format,
10621 : : "parameter might be a candidate "
10622 : : "for a format attribute");
10623 : : break;
10624 : 0 : case ICR_CONVERTING:
10625 : 0 : warning (OPT_Wsuggest_attribute_format,
10626 : : "target of conversion might be a candidate "
10627 : : "for a format attribute");
10628 : 0 : break;
10629 : 6 : case ICR_INIT:
10630 : 6 : warning (OPT_Wsuggest_attribute_format,
10631 : : "target of initialization might be a candidate "
10632 : : "for a format attribute");
10633 : 6 : break;
10634 : 6 : case ICR_RETURN:
10635 : 6 : warning (OPT_Wsuggest_attribute_format,
10636 : : "return type might be a candidate "
10637 : : "for a format attribute");
10638 : 6 : break;
10639 : 6 : case ICR_ASSIGN:
10640 : 6 : warning (OPT_Wsuggest_attribute_format,
10641 : : "left-hand side of assignment might be a candidate "
10642 : : "for a format attribute");
10643 : 6 : break;
10644 : 0 : default:
10645 : 0 : gcc_unreachable();
10646 : : }
10647 : : }
10648 : :
10649 : 120030550 : if (TREE_CODE (type) == BOOLEAN_TYPE)
10650 : 23255288 : maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
10651 : :
10652 : 120030550 : if (complain & tf_warning)
10653 : 118912177 : warn_for_address_of_packed_member (type, rhs);
10654 : :
10655 : 120030550 : return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10656 : 120030550 : complain, flags);
10657 : : }
10658 : :
10659 : : /* Convert RHS to be of type TYPE.
10660 : : If EXP is nonzero, it is the target of the initialization.
10661 : : ERRTYPE indicates what kind of error the implicit conversion is.
10662 : :
10663 : : Two major differences between the behavior of
10664 : : `convert_for_assignment' and `convert_for_initialization'
10665 : : are that references are bashed in the former, while
10666 : : copied in the latter, and aggregates are assigned in
10667 : : the former (operator=) while initialized in the
10668 : : latter (X(X&)).
10669 : :
10670 : : If using constructor make sure no conversion operator exists, if one does
10671 : : exist, an ambiguity exists. */
10672 : :
10673 : : tree
10674 : 107031272 : convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10675 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10676 : : tsubst_flags_t complain)
10677 : : {
10678 : 107031272 : enum tree_code codel = TREE_CODE (type);
10679 : 107031272 : tree rhstype;
10680 : 107031272 : enum tree_code coder;
10681 : :
10682 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10683 : : Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10684 : 107031272 : if (TREE_CODE (rhs) == NOP_EXPR
10685 : 4719316 : && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10686 : 107389118 : && codel != REFERENCE_TYPE)
10687 : 357846 : rhs = TREE_OPERAND (rhs, 0);
10688 : :
10689 : 107031272 : if (type == error_mark_node
10690 : 107031234 : || rhs == error_mark_node
10691 : 214062445 : || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10692 : : return error_mark_node;
10693 : :
10694 : 107031173 : if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10695 : : ;
10696 : 96446238 : else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10697 : 1528192 : && TREE_CODE (type) != ARRAY_TYPE
10698 : 1528192 : && (!TYPE_REF_P (type)
10699 : 8540 : || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10700 : 94926583 : || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10701 : 13214 : && !TYPE_REFFN_P (type))
10702 : 191361230 : || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10703 : 1531252 : rhs = decay_conversion (rhs, complain);
10704 : :
10705 : 107031173 : rhstype = TREE_TYPE (rhs);
10706 : 107031173 : coder = TREE_CODE (rhstype);
10707 : :
10708 : 107031173 : if (coder == ERROR_MARK)
10709 : 9 : return error_mark_node;
10710 : :
10711 : : /* We accept references to incomplete types, so we can
10712 : : return here before checking if RHS is of complete type. */
10713 : :
10714 : 107031164 : if (codel == REFERENCE_TYPE)
10715 : : {
10716 : 5097371 : auto_diagnostic_group d;
10717 : : /* This should eventually happen in convert_arguments. */
10718 : 5097371 : int savew = 0, savee = 0;
10719 : :
10720 : 5097371 : if (fndecl)
10721 : 257175 : savew = warningcount + werrorcount, savee = errorcount;
10722 : 5097371 : rhs = initialize_reference (type, rhs, flags, complain);
10723 : :
10724 : 5097371 : if (fndecl
10725 : 5097371 : && (warningcount + werrorcount > savew || errorcount > savee))
10726 : 36 : inform (get_fndecl_argument_location (fndecl, parmnum),
10727 : : "in passing argument %P of %qD", parmnum, fndecl);
10728 : 5097371 : return rhs;
10729 : 5097371 : }
10730 : :
10731 : 101933793 : if (exp != 0)
10732 : 3317101 : exp = require_complete_type (exp, complain);
10733 : 101933793 : if (exp == error_mark_node)
10734 : : return error_mark_node;
10735 : :
10736 : 101933793 : type = complete_type (type);
10737 : :
10738 : 101933793 : if (DIRECT_INIT_EXPR_P (type, rhs))
10739 : : /* Don't try to do copy-initialization if we already have
10740 : : direct-initialization. */
10741 : : return rhs;
10742 : :
10743 : 101932890 : if (MAYBE_CLASS_TYPE_P (type))
10744 : 7240991 : return perform_implicit_conversion_flags (type, rhs, complain, flags);
10745 : :
10746 : 94691899 : return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10747 : 94691899 : complain, flags);
10748 : : }
10749 : :
10750 : : /* If RETVAL is the address of, or a reference to, a local variable or
10751 : : temporary give an appropriate warning and return true. */
10752 : :
10753 : : static bool
10754 : 35828804 : maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10755 : : {
10756 : 35829489 : tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10757 : 35829489 : tree whats_returned = fold_for_warn (retval);
10758 : 35829489 : if (!loc)
10759 : 35829489 : loc = cp_expr_loc_or_input_loc (retval);
10760 : :
10761 : 43409251 : for (;;)
10762 : : {
10763 : 43409251 : if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10764 : 463202 : whats_returned = TREE_OPERAND (whats_returned, 1);
10765 : 42946049 : else if (CONVERT_EXPR_P (whats_returned)
10766 : 35849781 : || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10767 : 7116560 : whats_returned = TREE_OPERAND (whats_returned, 0);
10768 : : else
10769 : : break;
10770 : : }
10771 : :
10772 : 35829489 : if (TREE_CODE (whats_returned) == TARGET_EXPR
10773 : 35829489 : && is_std_init_list (TREE_TYPE (whats_returned)))
10774 : : {
10775 : 14 : tree init = TARGET_EXPR_INITIAL (whats_returned);
10776 : 14 : if (TREE_CODE (init) == CONSTRUCTOR)
10777 : : /* Pull out the array address. */
10778 : 6 : whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10779 : 8 : else if (TREE_CODE (init) == INDIRECT_REF)
10780 : : /* The source of a trivial copy looks like *(T*)&var. */
10781 : 5 : whats_returned = TREE_OPERAND (init, 0);
10782 : : else
10783 : : return false;
10784 : 11 : STRIP_NOPS (whats_returned);
10785 : : }
10786 : :
10787 : : /* As a special case, we handle a call to std::move or std::forward. */
10788 : 35829486 : if (TREE_CODE (whats_returned) == CALL_EXPR
10789 : 35829486 : && (is_std_move_p (whats_returned)
10790 : 9760144 : || is_std_forward_p (whats_returned)))
10791 : : {
10792 : 673 : tree arg = CALL_EXPR_ARG (whats_returned, 0);
10793 : 673 : return maybe_warn_about_returning_address_of_local (arg, loc);
10794 : : }
10795 : :
10796 : 35828813 : if (TREE_CODE (whats_returned) != ADDR_EXPR)
10797 : : return false;
10798 : 783572 : whats_returned = TREE_OPERAND (whats_returned, 0);
10799 : :
10800 : 783572 : while (TREE_CODE (whats_returned) == COMPONENT_REF
10801 : 1522847 : || TREE_CODE (whats_returned) == ARRAY_REF)
10802 : 739275 : whats_returned = TREE_OPERAND (whats_returned, 0);
10803 : :
10804 : 783572 : if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10805 : 783572 : || TREE_CODE (whats_returned) == TARGET_EXPR)
10806 : : {
10807 : 50 : if (TYPE_REF_P (valtype))
10808 : : /* P2748 made this an error in C++26. */
10809 : 72 : emit_diagnostic ((cxx_dialect >= cxx26
10810 : : ? diagnostics::kind::permerror
10811 : : : diagnostics::kind::warning),
10812 : 41 : loc, OPT_Wreturn_local_addr,
10813 : : "returning reference to temporary");
10814 : 9 : else if (TYPE_PTR_P (valtype))
10815 : 3 : warning_at (loc, OPT_Wreturn_local_addr,
10816 : : "returning pointer to temporary");
10817 : 6 : else if (is_std_init_list (valtype))
10818 : 6 : warning_at (loc, OPT_Winit_list_lifetime,
10819 : : "returning temporary %<initializer_list%> does not extend "
10820 : : "the lifetime of the underlying array");
10821 : 50 : return true;
10822 : : }
10823 : :
10824 : 783522 : STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10825 : :
10826 : 783522 : if (DECL_P (whats_returned)
10827 : 73943 : && DECL_NAME (whats_returned)
10828 : 73943 : && DECL_FUNCTION_SCOPE_P (whats_returned)
10829 : 12689 : && !is_capture_proxy (whats_returned)
10830 : 796208 : && !(TREE_STATIC (whats_returned)
10831 : 146 : || TREE_PUBLIC (whats_returned)))
10832 : : {
10833 : 106 : if (DECL_DECOMPOSITION_P (whats_returned)
10834 : 39 : && !DECL_DECOMP_IS_BASE (whats_returned)
10835 : 185 : && DECL_HAS_VALUE_EXPR_P (whats_returned))
10836 : : {
10837 : : /* When returning address of a structured binding, if the structured
10838 : : binding is not a reference, continue normally, if it is a
10839 : : reference, recurse on the initializer of the structured
10840 : : binding. */
10841 : 39 : tree base = DECL_DECOMP_BASE (whats_returned);
10842 : 39 : if (TYPE_REF_P (TREE_TYPE (base)))
10843 : : {
10844 : 15 : if (tree init = DECL_INITIAL (base))
10845 : : return maybe_warn_about_returning_address_of_local (init, loc);
10846 : : else
10847 : : return false;
10848 : : }
10849 : : }
10850 : 131 : bool w = false;
10851 : 131 : auto_diagnostic_group d;
10852 : 131 : if (TYPE_REF_P (valtype))
10853 : 81 : w = warning_at (loc, OPT_Wreturn_local_addr,
10854 : : "reference to local variable %qD returned",
10855 : : whats_returned);
10856 : 50 : else if (is_std_init_list (valtype))
10857 : 3 : w = warning_at (loc, OPT_Winit_list_lifetime,
10858 : : "returning local %<initializer_list%> variable %qD "
10859 : : "does not extend the lifetime of the underlying array",
10860 : : whats_returned);
10861 : 47 : else if (POINTER_TYPE_P (valtype)
10862 : 41 : && TREE_CODE (whats_returned) == LABEL_DECL)
10863 : 6 : w = warning_at (loc, OPT_Wreturn_local_addr,
10864 : : "address of label %qD returned",
10865 : : whats_returned);
10866 : 41 : else if (POINTER_TYPE_P (valtype))
10867 : 35 : w = warning_at (loc, OPT_Wreturn_local_addr,
10868 : : "address of local variable %qD returned",
10869 : : whats_returned);
10870 : 125 : if (w)
10871 : 103 : inform (DECL_SOURCE_LOCATION (whats_returned),
10872 : : "declared here");
10873 : 131 : return true;
10874 : 131 : }
10875 : :
10876 : : return false;
10877 : : }
10878 : :
10879 : : /* Returns true if DECL is in the std namespace. */
10880 : :
10881 : : bool
10882 : 74525464 : decl_in_std_namespace_p (tree decl)
10883 : : {
10884 : 86820423 : while (decl)
10885 : : {
10886 : 86361097 : decl = decl_namespace_context (decl);
10887 : 86361097 : if (DECL_NAMESPACE_STD_P (decl))
10888 : : return true;
10889 : : /* Allow inline namespaces inside of std namespace, e.g. with
10890 : : --enable-symvers=gnu-versioned-namespace std::forward would be
10891 : : actually std::_8::forward. */
10892 : 41444035 : if (!DECL_NAMESPACE_INLINE_P (decl))
10893 : : return false;
10894 : 12294959 : decl = CP_DECL_CONTEXT (decl);
10895 : : }
10896 : : return false;
10897 : : }
10898 : :
10899 : : /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10900 : :
10901 : : static bool
10902 : 9760144 : is_std_forward_p (tree fn)
10903 : : {
10904 : : /* std::forward only takes one argument. */
10905 : 9760144 : if (call_expr_nargs (fn) != 1)
10906 : : return false;
10907 : :
10908 : 4674537 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10909 : 4674537 : if (!decl_in_std_namespace_p (fndecl))
10910 : : return false;
10911 : :
10912 : 1647686 : tree name = DECL_NAME (fndecl);
10913 : 1647686 : return name && id_equal (name, "forward");
10914 : : }
10915 : :
10916 : : /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10917 : :
10918 : : static bool
10919 : 9767026 : is_std_move_p (tree fn)
10920 : : {
10921 : : /* std::move only takes one argument. */
10922 : 9767026 : if (call_expr_nargs (fn) != 1)
10923 : : return false;
10924 : :
10925 : 4680707 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10926 : 4680707 : if (!decl_in_std_namespace_p (fndecl))
10927 : : return false;
10928 : :
10929 : 1653398 : tree name = DECL_NAME (fndecl);
10930 : 1653398 : return name && id_equal (name, "move");
10931 : : }
10932 : :
10933 : : /* Returns true if RETVAL is a good candidate for the NRVO as per
10934 : : [class.copy.elision]. FUNCTYPE is the type the function is declared
10935 : : to return. */
10936 : :
10937 : : static bool
10938 : 45307586 : can_do_nrvo_p (tree retval, tree functype)
10939 : : {
10940 : 45307586 : if (functype == error_mark_node)
10941 : : return false;
10942 : 45307545 : if (retval)
10943 : 43786708 : STRIP_ANY_LOCATION_WRAPPER (retval);
10944 : 45307545 : tree result = DECL_RESULT (current_function_decl);
10945 : 45307545 : return (retval != NULL_TREE
10946 : 43786708 : && !processing_template_decl
10947 : : /* Must be a local, automatic variable. */
10948 : 36613908 : && VAR_P (retval)
10949 : 3298531 : && DECL_CONTEXT (retval) == current_function_decl
10950 : 2587980 : && !TREE_STATIC (retval)
10951 : : /* And not a lambda or anonymous union proxy. */
10952 : 2585154 : && !DECL_HAS_VALUE_EXPR_P (retval)
10953 : 2584943 : && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10954 : : /* The cv-unqualified type of the returned value must be the
10955 : : same as the cv-unqualified return type of the
10956 : : function. */
10957 : 2584547 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10958 : : TYPE_MAIN_VARIANT (functype))
10959 : : /* And the returned value must be non-volatile. */
10960 : 47881213 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
10961 : : }
10962 : :
10963 : : /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
10964 : : would otherwise return in memory. */
10965 : :
10966 : : static bool
10967 : 45307245 : want_nrvo_p (tree retval, tree functype)
10968 : : {
10969 : 45307245 : return (can_do_nrvo_p (retval, functype)
10970 : 45307245 : && aggregate_value_p (functype, current_function_decl));
10971 : : }
10972 : :
10973 : : /* Like can_do_nrvo_p, but we check if we're trying to move a class
10974 : : prvalue. */
10975 : :
10976 : : static bool
10977 : 1498 : can_elide_copy_prvalue_p (tree retval, tree functype)
10978 : : {
10979 : 1498 : if (functype == error_mark_node)
10980 : : return false;
10981 : 1498 : if (retval)
10982 : 1498 : STRIP_ANY_LOCATION_WRAPPER (retval);
10983 : 1498 : return (retval != NULL_TREE
10984 : 1498 : && !glvalue_p (retval)
10985 : 135 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10986 : : TYPE_MAIN_VARIANT (functype))
10987 : 1606 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
10988 : : }
10989 : :
10990 : : /* If we should treat RETVAL, an expression being returned, as if it were
10991 : : designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10992 : : NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10993 : : context (rather than throw). */
10994 : :
10995 : : tree
10996 : 8701943 : treat_lvalue_as_rvalue_p (tree expr, bool return_p)
10997 : : {
10998 : 8701943 : if (cxx_dialect == cxx98)
10999 : : return NULL_TREE;
11000 : :
11001 : 8689943 : tree retval = expr;
11002 : 8689943 : STRIP_ANY_LOCATION_WRAPPER (retval);
11003 : 8689943 : if (REFERENCE_REF_P (retval))
11004 : 597642 : retval = TREE_OPERAND (retval, 0);
11005 : :
11006 : : /* An implicitly movable entity is a variable of automatic storage duration
11007 : : that is either a non-volatile object or (C++20) an rvalue reference to a
11008 : : non-volatile object type. */
11009 : 8689943 : if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
11010 : 7800480 : || TREE_CODE (retval) == PARM_DECL)
11011 : 1203277 : && !TREE_STATIC (retval)
11012 : 1168979 : && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
11013 : 1168974 : && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
11014 : 104890 : || (cxx_dialect >= cxx20
11015 : 86042 : && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
11016 : 7625287 : return NULL_TREE;
11017 : :
11018 : : /* If the expression in a return or co_return statement is a (possibly
11019 : : parenthesized) id-expression that names an implicitly movable entity
11020 : : declared in the body or parameter-declaration-clause of the innermost
11021 : : enclosing function or lambda-expression, */
11022 : 1064656 : if (return_p)
11023 : : {
11024 : 1054803 : if (DECL_CONTEXT (retval) != current_function_decl)
11025 : : return NULL_TREE;
11026 : 1054737 : expr = move (expr);
11027 : 1054737 : if (expr == error_mark_node)
11028 : : return NULL_TREE;
11029 : 1054735 : return set_implicit_rvalue_p (expr);
11030 : : }
11031 : :
11032 : : /* if the id-expression (possibly parenthesized) is the operand of
11033 : : a throw-expression, and names an implicitly movable entity that belongs
11034 : : to a scope that does not contain the compound-statement of the innermost
11035 : : lambda-expression, try-block, or function-try-block (if any) whose
11036 : : compound-statement or ctor-initializer contains the throw-expression. */
11037 : :
11038 : : /* C++20 added move on throw of parms. */
11039 : 9853 : if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
11040 : : return NULL_TREE;
11041 : :
11042 : : /* We don't check for lambda-expression here, because we should not get past
11043 : : the DECL_HAS_VALUE_EXPR_P check above. */
11044 : 2427 : for (cp_binding_level *b = current_binding_level;
11045 : 2518 : b->kind != sk_namespace; b = b->level_chain)
11046 : : {
11047 : 2520 : for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
11048 : 100 : if (decl == retval)
11049 : 88 : return set_implicit_rvalue_p (move (expr));
11050 : 2420 : if (b->kind == sk_try)
11051 : : return NULL_TREE;
11052 : : }
11053 : :
11054 : 10 : return set_implicit_rvalue_p (move (expr));
11055 : : }
11056 : :
11057 : : /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
11058 : : is true). EXPR is the std::move expression; TYPE is the type of the object
11059 : : being initialized. */
11060 : :
11061 : : void
11062 : 128186005 : maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
11063 : : {
11064 : 128186005 : if (!(warn_pessimizing_move || warn_redundant_move))
11065 : : return;
11066 : :
11067 : 4387737 : const location_t loc = cp_expr_loc_or_input_loc (expr);
11068 : :
11069 : : /* C++98 doesn't know move. */
11070 : 4387737 : if (cxx_dialect < cxx11)
11071 : : return;
11072 : :
11073 : : /* Wait until instantiation time, since we can't gauge if we should do
11074 : : the NRVO until then. */
11075 : 4323319 : if (processing_template_decl)
11076 : : return;
11077 : :
11078 : : /* This is only interesting for class types. */
11079 : 4242620 : if (!CLASS_TYPE_P (type))
11080 : : return;
11081 : :
11082 : 91021 : bool wrapped_p = false;
11083 : : /* A a = std::move (A()); */
11084 : 91021 : if (TREE_CODE (expr) == TREE_LIST)
11085 : : {
11086 : 7520 : if (list_length (expr) == 1)
11087 : : {
11088 : 4952 : expr = TREE_VALUE (expr);
11089 : 4952 : wrapped_p = true;
11090 : : }
11091 : : else
11092 : : return;
11093 : : }
11094 : : /* A a = {std::move (A())};
11095 : : A a{std::move (A())}; */
11096 : 83501 : else if (TREE_CODE (expr) == CONSTRUCTOR)
11097 : : {
11098 : 6124 : if (CONSTRUCTOR_NELTS (expr) == 1)
11099 : : {
11100 : 738 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
11101 : 738 : wrapped_p = true;
11102 : : }
11103 : : else
11104 : : return;
11105 : : }
11106 : :
11107 : : /* First, check if this is a call to std::move. */
11108 : 5787 : if (!REFERENCE_REF_P (expr)
11109 : 87476 : || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
11110 : : return;
11111 : 2596 : tree fn = TREE_OPERAND (expr, 0);
11112 : 2596 : if (!is_std_move_p (fn))
11113 : : return;
11114 : 2040 : tree arg = CALL_EXPR_ARG (fn, 0);
11115 : 2040 : if (TREE_CODE (arg) != NOP_EXPR)
11116 : : return;
11117 : : /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
11118 : : and implicitly-movable warnings. */
11119 : 2040 : if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
11120 : : {
11121 : 1498 : arg = TREE_OPERAND (arg, 0);
11122 : 1498 : arg = TREE_OPERAND (arg, 0);
11123 : 1498 : arg = convert_from_reference (arg);
11124 : 1498 : if (can_elide_copy_prvalue_p (arg, type))
11125 : : {
11126 : 108 : auto_diagnostic_group d;
11127 : 108 : if (warning_at (loc, OPT_Wpessimizing_move,
11128 : : "moving a temporary object prevents copy elision"))
11129 : 108 : inform (loc, "remove %<std::move%> call");
11130 : 108 : }
11131 : : /* The rest of the warnings is only relevant for when we are returning
11132 : : from a function. */
11133 : 1498 : if (!return_p)
11134 : : return;
11135 : :
11136 : 341 : tree moved;
11137 : : /* Warn if we could do copy elision were it not for the move. */
11138 : 341 : if (can_do_nrvo_p (arg, type))
11139 : : {
11140 : 48 : auto_diagnostic_group d;
11141 : 48 : if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
11142 : 48 : && warning_at (loc, OPT_Wpessimizing_move,
11143 : : "moving a local object in a return statement "
11144 : : "prevents copy elision"))
11145 : 42 : inform (loc, "remove %<std::move%> call");
11146 : 48 : }
11147 : : /* Warn if the move is redundant. It is redundant when we would
11148 : : do maybe-rvalue overload resolution even without std::move. */
11149 : 293 : else if (warn_redundant_move
11150 : : /* This doesn't apply for return {std::move (t)};. */
11151 : 176 : && !wrapped_p
11152 : 173 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11153 : 389 : && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
11154 : : {
11155 : : /* Make sure that overload resolution would actually succeed
11156 : : if we removed the std::move call. */
11157 : 54 : tree t = convert_for_initialization (NULL_TREE, type,
11158 : : moved,
11159 : : (LOOKUP_NORMAL
11160 : : | LOOKUP_ONLYCONVERTING),
11161 : : ICR_RETURN, NULL_TREE, 0,
11162 : : tf_none);
11163 : : /* If this worked, implicit rvalue would work, so the call to
11164 : : std::move is redundant. */
11165 : 54 : if (t != error_mark_node)
11166 : : {
11167 : 54 : auto_diagnostic_group d;
11168 : 54 : if (warning_at (loc, OPT_Wredundant_move,
11169 : : "redundant move in return statement"))
11170 : 54 : inform (loc, "remove %<std::move%> call");
11171 : 54 : }
11172 : : }
11173 : : }
11174 : : /* Also try to warn about redundant std::move in code such as
11175 : : T f (const T& t)
11176 : : {
11177 : : return std::move(t);
11178 : : }
11179 : : for which EXPR will be something like
11180 : : *std::move<const T&> ((const struct T &) (const struct T *) t)
11181 : : and where the std::move does nothing if T does not have a T(const T&&)
11182 : : constructor, because the argument is const. It will not use T(T&&)
11183 : : because that would mean losing the const. */
11184 : 542 : else if (warn_redundant_move
11185 : 411 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11186 : 64 : && TYPE_REF_P (TREE_TYPE (arg))
11187 : 606 : && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
11188 : : {
11189 : 18 : tree rtype = TREE_TYPE (TREE_TYPE (arg));
11190 : 18 : if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
11191 : 9 : return;
11192 : : /* Check for the unlikely case there's T(const T&&) (we don't care if
11193 : : it's deleted). */
11194 : 54 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
11195 : 30 : if (move_fn_p (fn))
11196 : : {
11197 : 15 : tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11198 : 15 : if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
11199 : 6 : return;
11200 : : }
11201 : 9 : auto_diagnostic_group d;
11202 : 18 : if (return_p
11203 : 9 : ? warning_at (loc, OPT_Wredundant_move,
11204 : : "redundant move in return statement")
11205 : 9 : : warning_at (loc, OPT_Wredundant_move,
11206 : : "redundant move in initialization"))
11207 : 9 : inform (loc, "remove %<std::move%> call");
11208 : 9 : }
11209 : : }
11210 : :
11211 : : /* Check that returning RETVAL from the current function is valid.
11212 : : Return an expression explicitly showing all conversions required to
11213 : : change RETVAL into the function return type, and to assign it to
11214 : : the DECL_RESULT for the function. Set *NO_WARNING to true if
11215 : : code reaches end of non-void function warning shouldn't be issued
11216 : : on this RETURN_EXPR. Set *DANGLING to true if code returns the
11217 : : address of a local variable. */
11218 : :
11219 : : tree
11220 : 105715488 : check_return_expr (tree retval, bool *no_warning, bool *dangling)
11221 : : {
11222 : 105715488 : tree result;
11223 : : /* The type actually returned by the function. */
11224 : 105715488 : tree valtype;
11225 : : /* The type the function is declared to return, or void if
11226 : : the declared type is incomplete. */
11227 : 105715488 : tree functype;
11228 : 105715488 : int fn_returns_value_p;
11229 : 105715488 : location_t loc = cp_expr_loc_or_input_loc (retval);
11230 : :
11231 : 105715488 : *no_warning = false;
11232 : 105715488 : *dangling = false;
11233 : :
11234 : : /* A `volatile' function is one that isn't supposed to return, ever.
11235 : : (This is a G++ extension, used to get better code for functions
11236 : : that call the `volatile' function.) */
11237 : 105715488 : if (TREE_THIS_VOLATILE (current_function_decl))
11238 : 12 : warning (0, "function declared %<noreturn%> has a %<return%> statement");
11239 : :
11240 : : /* Check for various simple errors. */
11241 : 211430976 : if (DECL_DESTRUCTOR_P (current_function_decl))
11242 : : {
11243 : 3272 : if (retval)
11244 : 3 : error_at (loc, "returning a value from a destructor");
11245 : :
11246 : 3272 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11247 : 0 : retval = current_class_ptr;
11248 : : else
11249 : : return NULL_TREE;
11250 : : }
11251 : 105712216 : else if (DECL_CONSTRUCTOR_P (current_function_decl))
11252 : : {
11253 : 54889 : if (in_function_try_handler)
11254 : : /* If a return statement appears in a handler of the
11255 : : function-try-block of a constructor, the program is ill-formed. */
11256 : 0 : error ("cannot return from a handler of a function-try-block of a constructor");
11257 : 54889 : else if (retval)
11258 : : /* You can't return a value from a constructor. */
11259 : 3 : error_at (loc, "returning a value from a constructor");
11260 : :
11261 : 54889 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11262 : 0 : retval = current_class_ptr;
11263 : : else
11264 : : return NULL_TREE;
11265 : : }
11266 : :
11267 : 105657327 : const tree saved_retval = retval;
11268 : :
11269 : 105657327 : if (processing_template_decl)
11270 : : {
11271 : 68366421 : current_function_returns_value = 1;
11272 : :
11273 : 68366421 : if (check_for_bare_parameter_packs (retval))
11274 : 13 : return error_mark_node;
11275 : :
11276 : : /* If one of the types might be void, we can't tell whether we're
11277 : : returning a value. */
11278 : 68366408 : if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11279 : 26637365 : && !FNDECL_USED_AUTO (current_function_decl))
11280 : 41729043 : || (retval != NULL_TREE
11281 : 40636106 : && (TREE_TYPE (retval) == NULL_TREE
11282 : 28479784 : || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11283 : 46275035 : goto dependent;
11284 : : }
11285 : :
11286 : 59382279 : functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11287 : :
11288 : : /* Deduce auto return type from a return statement. */
11289 : 59382279 : if (FNDECL_USED_AUTO (current_function_decl))
11290 : : {
11291 : 921069 : tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11292 : 921069 : tree auto_node;
11293 : 921069 : tree type;
11294 : :
11295 : 921069 : if (!retval && !is_auto (pattern))
11296 : : {
11297 : : /* Give a helpful error message. */
11298 : 3 : auto_diagnostic_group d;
11299 : 3 : error ("return-statement with no value, in function returning %qT",
11300 : : pattern);
11301 : 3 : inform (input_location, "only plain %<auto%> return type can be "
11302 : : "deduced to %<void%>");
11303 : 3 : type = error_mark_node;
11304 : 3 : }
11305 : 921066 : else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11306 : : {
11307 : 6 : error ("returning initializer list");
11308 : 6 : type = error_mark_node;
11309 : : }
11310 : : else
11311 : : {
11312 : 921060 : if (!retval)
11313 : 5217 : retval = void_node;
11314 : 921060 : auto_node = type_uses_auto (pattern);
11315 : 921060 : type = do_auto_deduction (pattern, retval, auto_node,
11316 : : tf_warning_or_error, adc_return_type);
11317 : : }
11318 : :
11319 : 921069 : if (type == error_mark_node)
11320 : : /* Leave it. */;
11321 : 920873 : else if (functype == pattern)
11322 : 577012 : apply_deduced_return_type (current_function_decl, type);
11323 : 343861 : else if (!same_type_p (type, functype))
11324 : : {
11325 : 21 : if (LAMBDA_FUNCTION_P (current_function_decl))
11326 : 6 : error_at (loc, "inconsistent types %qT and %qT deduced for "
11327 : : "lambda return type", functype, type);
11328 : : else
11329 : 9 : error_at (loc, "inconsistent deduction for auto return type: "
11330 : : "%qT and then %qT", functype, type);
11331 : : }
11332 : : functype = type;
11333 : : }
11334 : :
11335 : 59382279 : result = DECL_RESULT (current_function_decl);
11336 : 59382279 : valtype = TREE_TYPE (result);
11337 : 59382279 : gcc_assert (valtype != NULL_TREE);
11338 : 59382279 : fn_returns_value_p = !VOID_TYPE_P (valtype);
11339 : :
11340 : : /* Check for a return statement with no return value in a function
11341 : : that's supposed to return a value. */
11342 : 59382279 : if (!retval && fn_returns_value_p)
11343 : : {
11344 : 35 : if (functype != error_mark_node)
11345 : 32 : permerror (input_location, "return-statement with no value, in "
11346 : : "function returning %qT", valtype);
11347 : : /* Remember that this function did return. */
11348 : 35 : current_function_returns_value = 1;
11349 : : /* But suppress NRV .*/
11350 : 35 : current_function_return_value = error_mark_node;
11351 : : /* And signal caller that TREE_NO_WARNING should be set on the
11352 : : RETURN_EXPR to avoid control reaches end of non-void function
11353 : : warnings in tree-cfg.cc. */
11354 : 35 : *no_warning = true;
11355 : : }
11356 : : /* Check for a return statement with a value in a function that
11357 : : isn't supposed to return a value. */
11358 : 59382244 : else if (retval && !fn_returns_value_p)
11359 : : {
11360 : 258597 : if (VOID_TYPE_P (TREE_TYPE (retval)))
11361 : : /* You can return a `void' value from a function of `void'
11362 : : type. In that case, we have to evaluate the expression for
11363 : : its side-effects. */
11364 : 258564 : finish_expr_stmt (retval);
11365 : 33 : else if (retval != error_mark_node)
11366 : 30 : permerror (loc, "return-statement with a value, in function "
11367 : : "returning %qT", valtype);
11368 : 258597 : current_function_returns_null = 1;
11369 : :
11370 : : /* There's really no value to return, after all. */
11371 : 258597 : return NULL_TREE;
11372 : : }
11373 : 59123647 : else if (!retval)
11374 : : /* Remember that this function can sometimes return without a
11375 : : value. */
11376 : 1520820 : current_function_returns_null = 1;
11377 : : else
11378 : : /* Remember that this function did return a value. */
11379 : 57602827 : current_function_returns_value = 1;
11380 : :
11381 : : /* Check for erroneous operands -- but after giving ourselves a
11382 : : chance to provide an error about returning a value from a void
11383 : : function. */
11384 : 59123682 : if (error_operand_p (retval))
11385 : : {
11386 : 957 : current_function_return_value = error_mark_node;
11387 : 957 : return error_mark_node;
11388 : : }
11389 : :
11390 : : /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11391 : 118245450 : if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11392 : 31221 : && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11393 : 3061 : && ! flag_check_new
11394 : 59125777 : && retval && null_ptr_cst_p (retval))
11395 : 24 : warning (0, "%<operator new%> must not return NULL unless it is "
11396 : : "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11397 : :
11398 : : /* Effective C++ rule 15. See also start_function. */
11399 : 59122725 : if (warn_ecpp
11400 : 42 : && DECL_NAME (current_function_decl) == assign_op_identifier
11401 : 59122755 : && !type_dependent_expression_p (retval))
11402 : : {
11403 : 27 : bool warn = true;
11404 : :
11405 : : /* The function return type must be a reference to the current
11406 : : class. */
11407 : 27 : if (TYPE_REF_P (valtype)
11408 : 45 : && same_type_ignoring_top_level_qualifiers_p
11409 : 18 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11410 : : {
11411 : : /* Returning '*this' is obviously OK. */
11412 : 18 : if (retval == current_class_ref)
11413 : : warn = false;
11414 : : /* If we are calling a function whose return type is the same of
11415 : : the current class reference, it is ok. */
11416 : 6 : else if (INDIRECT_REF_P (retval)
11417 : 6 : && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11418 : : warn = false;
11419 : : }
11420 : :
11421 : : if (warn)
11422 : 9 : warning_at (loc, OPT_Weffc__,
11423 : : "%<operator=%> should return a reference to %<*this%>");
11424 : : }
11425 : :
11426 : 59122725 : if (dependent_type_p (functype)
11427 : 59122725 : || type_dependent_expression_p (retval))
11428 : : {
11429 : 60090515 : dependent:
11430 : : /* We should not have changed the return value. */
11431 : 60090515 : gcc_assert (retval == saved_retval);
11432 : : /* We don't know if this is an lvalue or rvalue use, but
11433 : : either way we can mark it as read. */
11434 : 60090515 : mark_exp_read (retval);
11435 : 60090515 : return retval;
11436 : : }
11437 : :
11438 : : /* The fabled Named Return Value optimization, as per [class.copy]/15:
11439 : :
11440 : : [...] For a function with a class return type, if the expression
11441 : : in the return statement is the name of a local object, and the cv-
11442 : : unqualified type of the local object is the same as the function
11443 : : return type, an implementation is permitted to omit creating the tem-
11444 : : porary object to hold the function return value [...]
11445 : :
11446 : : So, if this is a value-returning function that always returns the same
11447 : : local variable, remember it.
11448 : :
11449 : : We choose the first suitable variable even if the function sometimes
11450 : : returns something else, but only if the variable is out of scope at the
11451 : : other return sites, or else we run the risk of clobbering the variable we
11452 : : chose if the other returned expression uses the chosen variable somehow.
11453 : :
11454 : : We don't currently do this if the first return is a non-variable, as it
11455 : : would be complicated to determine whether an NRV selected later was in
11456 : : scope at the point of the earlier return. We also don't currently support
11457 : : multiple variables with non-overlapping scopes (53637).
11458 : :
11459 : : See finish_function and finalize_nrv for the rest of this optimization. */
11460 : 45307245 : tree bare_retval = NULL_TREE;
11461 : 45307245 : if (retval)
11462 : : {
11463 : 43786405 : retval = maybe_undo_parenthesized_ref (retval);
11464 : 43786405 : bare_retval = tree_strip_any_location_wrapper (retval);
11465 : : }
11466 : :
11467 : 45307245 : bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11468 : 45307245 : if (fn_returns_value_p && flag_elide_constructors
11469 : 43786384 : && current_function_return_value != bare_retval)
11470 : : {
11471 : 43775096 : if (named_return_value_okay_p
11472 : 223614 : && current_function_return_value == NULL_TREE)
11473 : 182200 : current_function_return_value = bare_retval;
11474 : 43592896 : else if (current_function_return_value
11475 : 8088390 : && VAR_P (current_function_return_value)
11476 : 159 : && DECL_NAME (current_function_return_value)
11477 : 43593055 : && !decl_in_scope_p (current_function_return_value))
11478 : : {
11479 : : /* The earlier NRV is out of scope at this point, so it's safe to
11480 : 92 : leave it alone; the current return can't refer to it. */;
11481 : 92 : if (named_return_value_okay_p
11482 : 92 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11483 : : {
11484 : 8 : warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11485 : : bare_retval);
11486 : 8 : suppress_warning (current_function_decl, OPT_Wnrvo);
11487 : : }
11488 : : }
11489 : : else
11490 : : {
11491 : 43592804 : if ((named_return_value_okay_p
11492 : 43551400 : || (current_function_return_value
11493 : 8046894 : && current_function_return_value != error_mark_node))
11494 : 43592849 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11495 : : {
11496 : 41386 : warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11497 : : current_function_decl);
11498 : 41386 : suppress_warning (current_function_decl, OPT_Wnrvo);
11499 : : }
11500 : 43592804 : current_function_return_value = error_mark_node;
11501 : : }
11502 : : }
11503 : :
11504 : : /* We don't need to do any conversions when there's nothing being
11505 : : returned. */
11506 : 45307245 : if (!retval)
11507 : : return NULL_TREE;
11508 : :
11509 : 43786405 : if (!named_return_value_okay_p)
11510 : 43551503 : maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11511 : :
11512 : : /* Do any required conversions. */
11513 : 87572810 : if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11514 : : /* No conversions are required. */
11515 : : ;
11516 : : else
11517 : : {
11518 : 43786405 : int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11519 : :
11520 : : /* The functype's return type will have been set to void, if it
11521 : : was an incomplete type. Just treat this as 'return;' */
11522 : 43786405 : if (VOID_TYPE_P (functype))
11523 : 6 : return error_mark_node;
11524 : :
11525 : : /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11526 : : treated as an rvalue for the purposes of overload resolution to
11527 : : favor move constructors over copy constructors.
11528 : :
11529 : : Note that these conditions are similar to, but not as strict as,
11530 : : the conditions for the named return value optimization. */
11531 : 43786399 : bool converted = false;
11532 : 43786399 : tree moved;
11533 : : /* Until C++23, this was only interesting for class type, but in C++23,
11534 : : we should do the below when we're converting from/to a class/reference
11535 : : (a non-scalar type). */
11536 : 43786399 : if ((cxx_dialect < cxx23
11537 : 4699403 : ? CLASS_TYPE_P (functype)
11538 : 10909373 : : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11539 : 51677054 : && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11540 : : /* In C++20 and earlier we treat the return value as an rvalue
11541 : : that can bind to lvalue refs. In C++23, such an expression is just
11542 : : an xvalue (see reference_binding). */
11543 : : retval = moved;
11544 : :
11545 : : /* The call in a (lambda) thunk needs no conversions. */
11546 : 43786399 : if (TREE_CODE (retval) == CALL_EXPR
11547 : 43786399 : && call_from_lambda_thunk_p (retval))
11548 : : converted = true;
11549 : :
11550 : : /* Don't check copy-initialization for NRV in a coroutine ramp; we
11551 : : implement this case as NRV, but it's specified as directly
11552 : : initializing the return value from get_return_object(). */
11553 : 43786399 : if (DECL_RAMP_P (current_function_decl) && named_return_value_okay_p)
11554 : : converted = true;
11555 : :
11556 : : /* First convert the value to the function's return type, then
11557 : : to the type of return value's location to handle the
11558 : : case that functype is smaller than the valtype. */
11559 : 43786150 : if (!converted)
11560 : 43767252 : retval = convert_for_initialization
11561 : 43767252 : (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11562 : : tf_warning_or_error);
11563 : 43786399 : retval = convert (valtype, retval);
11564 : :
11565 : : /* If the conversion failed, treat this just like `return;'. */
11566 : 43786399 : if (retval == error_mark_node)
11567 : : {
11568 : : /* And suppress NRV. */
11569 : 192 : current_function_return_value = error_mark_node;
11570 : 192 : return retval;
11571 : : }
11572 : : /* We can't initialize a register from a AGGR_INIT_EXPR. */
11573 : 43786207 : else if (! cfun->returns_struct
11574 : 42207481 : && TREE_CODE (retval) == TARGET_EXPR
11575 : 48448070 : && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == AGGR_INIT_EXPR)
11576 : 784615 : retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11577 : 784615 : TARGET_EXPR_SLOT (retval));
11578 : 43001592 : else if (!processing_template_decl
11579 : 35828804 : && maybe_warn_about_returning_address_of_local (retval, loc)
11580 : 43001773 : && INDIRECT_TYPE_P (valtype))
11581 : 166 : *dangling = true;
11582 : : }
11583 : :
11584 : : /* A naive attempt to reduce the number of -Wdangling-reference false
11585 : : positives: if we know that this function can return a variable with
11586 : : static storage duration rather than one of its parameters, suppress
11587 : : the warning. */
11588 : 43786207 : if (warn_dangling_reference
11589 : 369862 : && TYPE_REF_P (functype)
11590 : 23941 : && bare_retval
11591 : 23941 : && VAR_P (bare_retval)
11592 : 50 : && TREE_STATIC (bare_retval))
11593 : 50 : suppress_warning (current_function_decl, OPT_Wdangling_reference);
11594 : :
11595 : 43786207 : if (processing_template_decl)
11596 : : return saved_retval;
11597 : :
11598 : : /* Actually copy the value returned into the appropriate location. */
11599 : 36613419 : if (retval && retval != result)
11600 : 36613419 : retval = cp_build_init_expr (result, retval);
11601 : :
11602 : 36613419 : if (current_function_return_value == bare_retval)
11603 : 193482 : INIT_EXPR_NRV_P (retval) = true;
11604 : :
11605 : 36613419 : if (tree set = maybe_set_retval_sentinel ())
11606 : 140147 : retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11607 : :
11608 : : return retval;
11609 : : }
11610 : :
11611 : :
11612 : : /* Returns nonzero if the pointer-type FROM can be converted to the
11613 : : pointer-type TO via a qualification conversion. If CONSTP is -1,
11614 : : then we return nonzero if the pointers are similar, and the
11615 : : cv-qualification signature of FROM is a proper subset of that of TO.
11616 : :
11617 : : If CONSTP is positive, then all outer pointers have been
11618 : : const-qualified. */
11619 : :
11620 : : static bool
11621 : 127124623 : comp_ptr_ttypes_real (tree to, tree from, int constp)
11622 : : {
11623 : 127124623 : bool to_more_cv_qualified = false;
11624 : 127124623 : bool is_opaque_pointer = false;
11625 : :
11626 : 569629 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11627 : : {
11628 : 127694252 : if (TREE_CODE (to) != TREE_CODE (from))
11629 : : return false;
11630 : :
11631 : 81161209 : if (TREE_CODE (from) == OFFSET_TYPE
11632 : 81161209 : && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11633 : : TYPE_OFFSET_BASETYPE (to)))
11634 : : return false;
11635 : :
11636 : : /* Const and volatile mean something different for function and
11637 : : array types, so the usual checks are not appropriate. We'll
11638 : : check the array type elements in further iterations. */
11639 : 81161209 : if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11640 : : {
11641 : 80923734 : if (!at_least_as_qualified_p (to, from))
11642 : : return false;
11643 : :
11644 : 71540026 : if (!at_least_as_qualified_p (from, to))
11645 : : {
11646 : 50184058 : if (constp == 0)
11647 : : return false;
11648 : : to_more_cv_qualified = true;
11649 : : }
11650 : :
11651 : 71539764 : if (constp > 0)
11652 : 71534956 : constp &= TYPE_READONLY (to);
11653 : : }
11654 : :
11655 : 71777239 : if (VECTOR_TYPE_P (to))
11656 : 6010 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11657 : :
11658 : : /* P0388R4 allows a conversion from int[N] to int[] but not the
11659 : : other way round. When both arrays have bounds but they do
11660 : : not match, then no conversion is possible. */
11661 : 71777239 : if (TREE_CODE (to) == ARRAY_TYPE
11662 : 71777239 : && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11663 : : return false;
11664 : :
11665 : 71776943 : if (!TYPE_PTR_P (to)
11666 : : && !TYPE_PTRDATAMEM_P (to)
11667 : : /* CWG 330 says we need to look through arrays. */
11668 : : && TREE_CODE (to) != ARRAY_TYPE)
11669 : 71207314 : return ((constp >= 0 || to_more_cv_qualified)
11670 : 71207314 : && (is_opaque_pointer
11671 : 71206756 : || same_type_ignoring_top_level_qualifiers_p (to, from)));
11672 : : }
11673 : : }
11674 : :
11675 : : /* When comparing, say, char ** to char const **, this function takes
11676 : : the 'char *' and 'char const *'. Do not pass non-pointer/reference
11677 : : types to this function. */
11678 : :
11679 : : int
11680 : 127123955 : comp_ptr_ttypes (tree to, tree from)
11681 : : {
11682 : 127123955 : return comp_ptr_ttypes_real (to, from, 1);
11683 : : }
11684 : :
11685 : : /* Returns true iff FNTYPE is a non-class type that involves
11686 : : error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11687 : : if a parameter type is ill-formed. */
11688 : :
11689 : : bool
11690 : 641601 : error_type_p (const_tree type)
11691 : : {
11692 : 686134 : tree t;
11693 : :
11694 : 686134 : switch (TREE_CODE (type))
11695 : : {
11696 : : case ERROR_MARK:
11697 : : return true;
11698 : :
11699 : 44444 : case POINTER_TYPE:
11700 : 44444 : case REFERENCE_TYPE:
11701 : 44444 : case OFFSET_TYPE:
11702 : 44444 : return error_type_p (TREE_TYPE (type));
11703 : :
11704 : 3759 : case FUNCTION_TYPE:
11705 : 3759 : case METHOD_TYPE:
11706 : 3759 : if (error_type_p (TREE_TYPE (type)))
11707 : : return true;
11708 : 10397 : for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11709 : 6644 : if (error_type_p (TREE_VALUE (t)))
11710 : : return true;
11711 : : return false;
11712 : :
11713 : 203785 : case RECORD_TYPE:
11714 : 203785 : if (TYPE_PTRMEMFUNC_P (type))
11715 : 89 : return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11716 : : return false;
11717 : :
11718 : : default:
11719 : : return false;
11720 : : }
11721 : : }
11722 : :
11723 : : /* Returns true if to and from are (possibly multi-level) pointers to the same
11724 : : type or inheritance-related types, regardless of cv-quals. */
11725 : :
11726 : : bool
11727 : 89364608 : ptr_reasonably_similar (const_tree to, const_tree from)
11728 : : {
11729 : 1020 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11730 : : {
11731 : : /* Any target type is similar enough to void. */
11732 : 89365628 : if (VOID_TYPE_P (to))
11733 : 998 : return !error_type_p (from);
11734 : 89364630 : if (VOID_TYPE_P (from))
11735 : 627214 : return !error_type_p (to);
11736 : :
11737 : 88737416 : if (TREE_CODE (to) != TREE_CODE (from))
11738 : : return false;
11739 : :
11740 : 42692252 : if (TREE_CODE (from) == OFFSET_TYPE
11741 : 42692252 : && comptypes (TYPE_OFFSET_BASETYPE (to),
11742 : 3 : TYPE_OFFSET_BASETYPE (from),
11743 : : COMPARE_BASE | COMPARE_DERIVED))
11744 : 3 : continue;
11745 : :
11746 : 42692246 : if (VECTOR_TYPE_P (to)
11747 : 42692246 : && vector_types_convertible_p (to, from, false))
11748 : : return true;
11749 : :
11750 : 42692162 : if (TREE_CODE (to) == INTEGER_TYPE
11751 : 42692162 : && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11752 : : return true;
11753 : :
11754 : 42565371 : if (TREE_CODE (to) == FUNCTION_TYPE)
11755 : 1493 : return !error_type_p (to) && !error_type_p (from);
11756 : :
11757 : 42563878 : if (!TYPE_PTR_P (to))
11758 : : {
11759 : : /* When either type is incomplete avoid DERIVED_FROM_P,
11760 : : which may call complete_type (c++/57942). */
11761 : 42562861 : bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11762 : : return comptypes
11763 : 42562861 : (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11764 : 42562861 : b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11765 : : }
11766 : 1020 : }
11767 : : }
11768 : :
11769 : : /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11770 : : pointer-to-member types) are the same, ignoring cv-qualification at
11771 : : all levels. CB says how we should behave when comparing array bounds. */
11772 : :
11773 : : bool
11774 : 787610 : comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11775 : : {
11776 : 787610 : bool is_opaque_pointer = false;
11777 : :
11778 : 788689 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11779 : : {
11780 : 1576299 : if (TREE_CODE (to) != TREE_CODE (from))
11781 : : return false;
11782 : :
11783 : 1221766 : if (TREE_CODE (from) == OFFSET_TYPE
11784 : 1221730 : && same_type_p (TYPE_OFFSET_BASETYPE (from),
11785 : : TYPE_OFFSET_BASETYPE (to)))
11786 : 36 : continue;
11787 : :
11788 : 1221694 : if (VECTOR_TYPE_P (to))
11789 : 7559 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11790 : :
11791 : 1221694 : if (TREE_CODE (to) == ARRAY_TYPE
11792 : : /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11793 : : we must fail. */
11794 : 1221694 : && !comp_array_types (to, from, cb, /*strict=*/false))
11795 : : return false;
11796 : :
11797 : : /* CWG 330 says we need to look through arrays. */
11798 : 1221296 : if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11799 : 432643 : return (is_opaque_pointer
11800 : 432643 : || same_type_ignoring_top_level_qualifiers_p (to, from));
11801 : : }
11802 : : }
11803 : :
11804 : : /* Returns the type qualifiers for this type, including the qualifiers on the
11805 : : elements for an array type. */
11806 : :
11807 : : int
11808 : 48766219415 : cp_type_quals (const_tree type)
11809 : : {
11810 : 48766219415 : int quals;
11811 : : /* This CONST_CAST is okay because strip_array_types returns its
11812 : : argument unmodified and we assign it to a const_tree. */
11813 : 48766219415 : type = strip_array_types (CONST_CAST_TREE (type));
11814 : 48766219415 : if (type == error_mark_node
11815 : : /* Quals on a FUNCTION_TYPE are memfn quals. */
11816 : 48736187508 : || TREE_CODE (type) == FUNCTION_TYPE)
11817 : : return TYPE_UNQUALIFIED;
11818 : 48693513609 : quals = TYPE_QUALS (type);
11819 : : /* METHOD and REFERENCE_TYPEs should never have quals. */
11820 : 48693513609 : gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11821 : : && !TYPE_REF_P (type))
11822 : : || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11823 : : == TYPE_UNQUALIFIED));
11824 : : return quals;
11825 : : }
11826 : :
11827 : : /* Returns the function-ref-qualifier for TYPE */
11828 : :
11829 : : cp_ref_qualifier
11830 : 2731274667 : type_memfn_rqual (const_tree type)
11831 : : {
11832 : 2731274667 : gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11833 : :
11834 : 2731274667 : if (!FUNCTION_REF_QUALIFIED (type))
11835 : : return REF_QUAL_NONE;
11836 : 38845843 : else if (FUNCTION_RVALUE_QUALIFIED (type))
11837 : : return REF_QUAL_RVALUE;
11838 : : else
11839 : 19473261 : return REF_QUAL_LVALUE;
11840 : : }
11841 : :
11842 : : /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11843 : : METHOD_TYPE. */
11844 : :
11845 : : int
11846 : 999925237 : type_memfn_quals (const_tree type)
11847 : : {
11848 : 999925237 : if (TREE_CODE (type) == FUNCTION_TYPE)
11849 : 76072076 : return TYPE_QUALS (type);
11850 : 923853161 : else if (TREE_CODE (type) == METHOD_TYPE)
11851 : 923853161 : return cp_type_quals (class_of_this_parm (type));
11852 : : else
11853 : 0 : gcc_unreachable ();
11854 : : }
11855 : :
11856 : : /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11857 : : MEMFN_QUALS and its ref-qualifier to RQUAL. */
11858 : :
11859 : : tree
11860 : 53328925 : apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11861 : : {
11862 : : /* Could handle METHOD_TYPE here if necessary. */
11863 : 53328925 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11864 : 53328925 : if (TYPE_QUALS (type) == memfn_quals
11865 : 53328925 : && type_memfn_rqual (type) == rqual)
11866 : : return type;
11867 : :
11868 : : /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11869 : : complex. */
11870 : 661589 : tree result = build_qualified_type (type, memfn_quals);
11871 : 661589 : return build_ref_qualified_type (result, rqual);
11872 : : }
11873 : :
11874 : : /* Returns nonzero if TYPE is const or volatile. */
11875 : :
11876 : : bool
11877 : 1003891004 : cv_qualified_p (const_tree type)
11878 : : {
11879 : 1003891004 : int quals = cp_type_quals (type);
11880 : 1003891004 : return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11881 : : }
11882 : :
11883 : : /* Returns nonzero if the TYPE contains a mutable member. */
11884 : :
11885 : : bool
11886 : 813280336 : cp_has_mutable_p (const_tree type)
11887 : : {
11888 : : /* This CONST_CAST is okay because strip_array_types returns its
11889 : : argument unmodified and we assign it to a const_tree. */
11890 : 813280336 : type = strip_array_types (CONST_CAST_TREE(type));
11891 : :
11892 : 813280336 : return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11893 : : }
11894 : :
11895 : : /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11896 : : TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11897 : : approximation. In particular, consider:
11898 : :
11899 : : int f();
11900 : : struct S { int i; };
11901 : : const S s = { f(); }
11902 : :
11903 : : Here, we will make "s" as TREE_READONLY (because it is declared
11904 : : "const") -- only to reverse ourselves upon seeing that the
11905 : : initializer is non-constant. */
11906 : :
11907 : : void
11908 : 865837521 : cp_apply_type_quals_to_decl (int type_quals, tree decl)
11909 : : {
11910 : 865837521 : tree type = TREE_TYPE (decl);
11911 : :
11912 : 865837521 : if (type == error_mark_node)
11913 : : return;
11914 : :
11915 : 865836862 : if (TREE_CODE (decl) == TYPE_DECL)
11916 : : return;
11917 : :
11918 : 775006387 : gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11919 : : && type_quals != TYPE_UNQUALIFIED));
11920 : :
11921 : : /* Avoid setting TREE_READONLY incorrectly. */
11922 : : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11923 : : constructor can produce constant init, so rely on cp_finish_decl to
11924 : : clear TREE_READONLY if the variable has non-constant init. */
11925 : :
11926 : : /* If the type has (or might have) a mutable component, that component
11927 : : might be modified. */
11928 : 775006387 : if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11929 : 70118279 : type_quals &= ~TYPE_QUAL_CONST;
11930 : :
11931 : 775006387 : c_apply_type_quals_to_decl (type_quals, decl);
11932 : : }
11933 : :
11934 : : /* Subroutine of casts_away_constness. Make T1 and T2 point at
11935 : : exemplar types such that casting T1 to T2 is casting away constness
11936 : : if and only if there is no implicit conversion from T1 to T2. */
11937 : :
11938 : : static void
11939 : 1133655 : casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11940 : : {
11941 : 1133655 : int quals1;
11942 : 1133655 : int quals2;
11943 : :
11944 : : /* [expr.const.cast]
11945 : :
11946 : : For multi-level pointer to members and multi-level mixed pointers
11947 : : and pointers to members (conv.qual), the "member" aspect of a
11948 : : pointer to member level is ignored when determining if a const
11949 : : cv-qualifier has been cast away. */
11950 : : /* [expr.const.cast]
11951 : :
11952 : : For two pointer types:
11953 : :
11954 : : X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11955 : : X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11956 : : K is min(N,M)
11957 : :
11958 : : casting from X1 to X2 casts away constness if, for a non-pointer
11959 : : type T there does not exist an implicit conversion (clause
11960 : : _conv_) from:
11961 : :
11962 : : Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11963 : :
11964 : : to
11965 : :
11966 : : Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11967 : 1133655 : if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11968 : 567172 : || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11969 : : {
11970 : 566655 : *t1 = cp_build_qualified_type (void_type_node,
11971 : : cp_type_quals (*t1));
11972 : 566655 : *t2 = cp_build_qualified_type (void_type_node,
11973 : : cp_type_quals (*t2));
11974 : 566655 : return;
11975 : : }
11976 : :
11977 : 567000 : quals1 = cp_type_quals (*t1);
11978 : 567000 : quals2 = cp_type_quals (*t2);
11979 : :
11980 : 567000 : if (TYPE_PTRDATAMEM_P (*t1))
11981 : 0 : *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
11982 : : else
11983 : 567000 : *t1 = TREE_TYPE (*t1);
11984 : 567000 : if (TYPE_PTRDATAMEM_P (*t2))
11985 : 0 : *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
11986 : : else
11987 : 567000 : *t2 = TREE_TYPE (*t2);
11988 : :
11989 : 567000 : casts_away_constness_r (t1, t2, complain);
11990 : 567000 : *t1 = build_pointer_type (*t1);
11991 : 567000 : *t2 = build_pointer_type (*t2);
11992 : 567000 : *t1 = cp_build_qualified_type (*t1, quals1);
11993 : 567000 : *t2 = cp_build_qualified_type (*t2, quals2);
11994 : : }
11995 : :
11996 : : /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11997 : : constness.
11998 : :
11999 : : ??? This function returns non-zero if casting away qualifiers not
12000 : : just const. We would like to return to the caller exactly which
12001 : : qualifiers are casted away to give more accurate diagnostics.
12002 : : */
12003 : :
12004 : : static bool
12005 : 566727 : casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
12006 : : {
12007 : 566727 : if (TYPE_REF_P (t2))
12008 : : {
12009 : : /* [expr.const.cast]
12010 : :
12011 : : Casting from an lvalue of type T1 to an lvalue of type T2
12012 : : using a reference cast casts away constness if a cast from an
12013 : : rvalue of type "pointer to T1" to the type "pointer to T2"
12014 : : casts away constness. */
12015 : 0 : t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
12016 : 0 : return casts_away_constness (build_pointer_type (t1),
12017 : 0 : build_pointer_type (TREE_TYPE (t2)),
12018 : 0 : complain);
12019 : : }
12020 : :
12021 : 566727 : if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
12022 : : /* [expr.const.cast]
12023 : :
12024 : : Casting from an rvalue of type "pointer to data member of X
12025 : : of type T1" to the type "pointer to data member of Y of type
12026 : : T2" casts away constness if a cast from an rvalue of type
12027 : : "pointer to T1" to the type "pointer to T2" casts away
12028 : : constness. */
12029 : 48 : return casts_away_constness
12030 : 48 : (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
12031 : 48 : build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
12032 : 48 : complain);
12033 : :
12034 : : /* Casting away constness is only something that makes sense for
12035 : : pointer or reference types. */
12036 : 566679 : if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
12037 : : return false;
12038 : :
12039 : : /* Top-level qualifiers don't matter. */
12040 : 566655 : t1 = TYPE_MAIN_VARIANT (t1);
12041 : 566655 : t2 = TYPE_MAIN_VARIANT (t2);
12042 : 566655 : casts_away_constness_r (&t1, &t2, complain);
12043 : 566655 : if (!can_convert (t2, t1, complain))
12044 : : return true;
12045 : :
12046 : : return false;
12047 : : }
12048 : :
12049 : : /* If T is a REFERENCE_TYPE return the type to which T refers.
12050 : : Otherwise, return T itself. */
12051 : :
12052 : : tree
12053 : 2981498874 : non_reference (tree t)
12054 : : {
12055 : 2981498874 : if (t && TYPE_REF_P (t))
12056 : 281013711 : t = TREE_TYPE (t);
12057 : 2981498874 : return t;
12058 : : }
12059 : :
12060 : :
12061 : : /* Return nonzero if REF is an lvalue valid for this language;
12062 : : otherwise, print an error message and return zero. USE says
12063 : : how the lvalue is being used and so selects the error message. */
12064 : :
12065 : : int
12066 : 37548010 : lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
12067 : : {
12068 : 37548010 : cp_lvalue_kind kind = lvalue_kind (ref);
12069 : :
12070 : 37548010 : if (kind == clk_none)
12071 : : {
12072 : 128 : if (complain & tf_error)
12073 : 115 : lvalue_error (cp_expr_loc_or_input_loc (ref), use);
12074 : 128 : return 0;
12075 : : }
12076 : 37547882 : else if (kind & (clk_rvalueref|clk_class))
12077 : : {
12078 : 104 : if (!(complain & tf_error))
12079 : : return 0;
12080 : : /* Make this a permerror because we used to accept it. */
12081 : 18 : permerror (cp_expr_loc_or_input_loc (ref),
12082 : : "using rvalue as lvalue");
12083 : : }
12084 : : return 1;
12085 : : }
12086 : :
12087 : : /* Return true if a user-defined literal operator is a raw operator. */
12088 : :
12089 : : bool
12090 : 120766 : check_raw_literal_operator (const_tree decl)
12091 : : {
12092 : 120766 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12093 : 120766 : tree argtype;
12094 : 120766 : int arity;
12095 : 120766 : bool maybe_raw_p = false;
12096 : :
12097 : : /* Count the number and type of arguments and check for ellipsis. */
12098 : 120766 : for (argtype = argtypes, arity = 0;
12099 : 181166 : argtype && argtype != void_list_node;
12100 : 60400 : ++arity, argtype = TREE_CHAIN (argtype))
12101 : : {
12102 : 60400 : tree t = TREE_VALUE (argtype);
12103 : :
12104 : 60400 : if (same_type_p (t, const_string_type_node))
12105 : 9 : maybe_raw_p = true;
12106 : : }
12107 : 120766 : if (!argtype)
12108 : : return false; /* Found ellipsis. */
12109 : :
12110 : 120766 : if (!maybe_raw_p || arity != 1)
12111 : 120760 : return false;
12112 : :
12113 : : return true;
12114 : : }
12115 : :
12116 : :
12117 : : /* Return true if a user-defined literal operator has one of the allowed
12118 : : argument types. */
12119 : :
12120 : : bool
12121 : 285772 : check_literal_operator_args (const_tree decl,
12122 : : bool *long_long_unsigned_p, bool *long_double_p)
12123 : : {
12124 : 285772 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12125 : :
12126 : 285772 : *long_long_unsigned_p = false;
12127 : 285772 : *long_double_p = false;
12128 : 285772 : if (processing_template_decl || processing_specialization)
12129 : 60504 : return argtypes == void_list_node;
12130 : : else
12131 : : {
12132 : : tree argtype;
12133 : : int arity;
12134 : : int max_arity = 2;
12135 : :
12136 : : /* Count the number and type of arguments and check for ellipsis. */
12137 : 225174 : for (argtype = argtypes, arity = 0;
12138 : 450442 : argtype && argtype != void_list_node;
12139 : 225174 : argtype = TREE_CHAIN (argtype))
12140 : : {
12141 : 225274 : tree t = TREE_VALUE (argtype);
12142 : 225274 : ++arity;
12143 : :
12144 : 225274 : if (TYPE_PTR_P (t))
12145 : : {
12146 : 101106 : bool maybe_raw_p = false;
12147 : 101106 : t = TREE_TYPE (t);
12148 : 101106 : if (cp_type_quals (t) != TYPE_QUAL_CONST)
12149 : : return false;
12150 : 101103 : t = TYPE_MAIN_VARIANT (t);
12151 : 101103 : if ((maybe_raw_p = same_type_p (t, char_type_node))
12152 : 77117 : || same_type_p (t, wchar_type_node)
12153 : 53283 : || same_type_p (t, char8_type_node)
12154 : 47680 : || same_type_p (t, char16_type_node)
12155 : 124943 : || same_type_p (t, char32_type_node))
12156 : : {
12157 : 101100 : argtype = TREE_CHAIN (argtype);
12158 : 101100 : if (!argtype)
12159 : : return false;
12160 : 101100 : t = TREE_VALUE (argtype);
12161 : 101100 : if (maybe_raw_p && argtype == void_list_node)
12162 : : return true;
12163 : 101027 : else if (same_type_p (t, size_type_node))
12164 : : {
12165 : 101021 : ++arity;
12166 : 101021 : continue;
12167 : : }
12168 : : else
12169 : : return false;
12170 : : }
12171 : : }
12172 : 124168 : else if (same_type_p (t, long_long_unsigned_type_node))
12173 : : {
12174 : 34155 : max_arity = 1;
12175 : 34155 : *long_long_unsigned_p = true;
12176 : : }
12177 : 90013 : else if (same_type_p (t, long_double_type_node))
12178 : : {
12179 : 89913 : max_arity = 1;
12180 : 89913 : *long_double_p = true;
12181 : : }
12182 : 100 : else if (same_type_p (t, char_type_node))
12183 : : max_arity = 1;
12184 : 56 : else if (same_type_p (t, wchar_type_node))
12185 : : max_arity = 1;
12186 : 45 : else if (same_type_p (t, char8_type_node))
12187 : : max_arity = 1;
12188 : 40 : else if (same_type_p (t, char16_type_node))
12189 : : max_arity = 1;
12190 : 29 : else if (same_type_p (t, char32_type_node))
12191 : : max_arity = 1;
12192 : : else
12193 : : return false;
12194 : : }
12195 : 225168 : if (!argtype)
12196 : : return false; /* Found ellipsis. */
12197 : :
12198 : 225165 : if (arity != max_arity)
12199 : : return false;
12200 : :
12201 : : return true;
12202 : : }
12203 : : }
12204 : :
12205 : : /* Always returns false since unlike C90, C++ has no concept of implicit
12206 : : function declarations. */
12207 : :
12208 : : bool
12209 : 359 : c_decl_implicit (const_tree)
12210 : : {
12211 : 359 : return false;
12212 : : }
|