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 : 117360454 : require_complete_type (tree value,
75 : : tsubst_flags_t complain /* = tf_warning_or_error */)
76 : : {
77 : 117360454 : tree type;
78 : :
79 : 117360454 : if (processing_template_decl || value == error_mark_node)
80 : : return value;
81 : :
82 : 109233218 : if (TREE_CODE (value) == OVERLOAD)
83 : 0 : type = unknown_type_node;
84 : : else
85 : 109233218 : type = TREE_TYPE (value);
86 : :
87 : 109233218 : if (type == error_mark_node)
88 : : return error_mark_node;
89 : :
90 : : /* First, detect a valid value with a complete type. */
91 : 109233206 : if (COMPLETE_TYPE_P (type))
92 : : return value;
93 : :
94 : 112164 : 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 : 12340110933 : complete_type (tree type)
107 : : {
108 : 12340110933 : 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 : 12340110933 : if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 : : ;
115 : 4099084354 : else if (TREE_CODE (type) == ARRAY_TYPE)
116 : : {
117 : 632068 : tree t = complete_type (TREE_TYPE (type));
118 : 632068 : unsigned int needs_constructing, has_nontrivial_dtor;
119 : 632068 : if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 : 627194 : layout_type (type);
121 : 632068 : needs_constructing
122 : 632068 : = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 : 632068 : has_nontrivial_dtor
124 : 632068 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 : 2091308 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 : : {
127 : 1459240 : TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 : 1459240 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 : : }
130 : : }
131 : 4098452286 : else if (CLASS_TYPE_P (type))
132 : : {
133 : 4018175054 : if (modules_p ())
134 : : /* TYPE could be a class member we've not loaded the definition of. */
135 : 13009513 : lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
136 : :
137 : 4018175054 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 : 776432259 : 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 : 1315734564 : complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 : : {
151 : 1315734564 : type = complete_type (type);
152 : 1315734561 : if (type == error_mark_node)
153 : : /* We already issued an error. */
154 : : return NULL_TREE;
155 : 1315734408 : else if (!COMPLETE_TYPE_P (type))
156 : : {
157 : 3803 : if (complain & tf_error)
158 : 472 : cxx_incomplete_type_diagnostic (value, type, diagnostics::kind::error);
159 : 3803 : note_failed_type_completion (type, complain);
160 : 3803 : return NULL_TREE;
161 : : }
162 : : else
163 : : return type;
164 : : }
165 : :
166 : : tree
167 : 141546910 : complete_type_or_else (tree type, tree value)
168 : : {
169 : 141546910 : 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 : 4169886 : commonparms (tree p1, tree p2)
182 : : {
183 : 4169886 : tree oldargs = p1, newargs, n;
184 : 4169886 : int i, len;
185 : 4169886 : int any_change = 0;
186 : :
187 : 4169886 : len = list_length (p1);
188 : 4169886 : newargs = tree_last (p1);
189 : :
190 : 4169886 : if (newargs == void_list_node)
191 : : i = 1;
192 : : else
193 : : {
194 : 38628 : i = 0;
195 : 38628 : newargs = 0;
196 : : }
197 : :
198 : 13889674 : for (; i < len; i++)
199 : 9719788 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200 : :
201 : : n = newargs;
202 : :
203 : 18020932 : for (i = 0; p1;
204 : 13851046 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 : : {
206 : 13851247 : if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 : : {
208 : 168 : TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 : 168 : any_change = 1;
210 : : }
211 : 13850878 : else if (! TREE_PURPOSE (p1))
212 : : {
213 : 13850845 : if (TREE_PURPOSE (p2))
214 : : {
215 : 417504 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 : 417504 : any_change = 1;
217 : : }
218 : : }
219 : : else
220 : : {
221 : 33 : if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
222 : 33 : any_change = 1;
223 : 33 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 : : }
225 : 13851046 : if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 : : {
227 : 3818705 : any_change = 1;
228 : 3818705 : TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 : : }
230 : : else
231 : 10032341 : TREE_VALUE (n) = TREE_VALUE (p1);
232 : : }
233 : 4169886 : if (! any_change)
234 : 1466128 : 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 : 32777242 : original_type (tree t)
243 : : {
244 : 32777242 : int quals = cp_type_quals (t);
245 : 32777242 : while (t != error_mark_node
246 : 33898863 : && TYPE_NAME (t) != NULL_TREE)
247 : : {
248 : 11416423 : tree x = TYPE_NAME (t);
249 : 11416423 : if (TREE_CODE (x) != TYPE_DECL)
250 : : break;
251 : 11416423 : x = DECL_ORIGINAL_TYPE (x);
252 : 11416423 : if (x == NULL_TREE)
253 : : break;
254 : : t = x;
255 : : }
256 : 32777242 : 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 : 50303 : merge_type_attributes_from (tree type, tree other_type)
264 : : {
265 : 50303 : tree attrs = targetm.merge_type_attributes (type, other_type);
266 : 50303 : attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
267 : 50303 : 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 : 11000340 : cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
280 : : {
281 : 11000340 : tree mv1 = TYPE_MAIN_VARIANT (t1);
282 : 11000340 : tree mv2 = TYPE_MAIN_VARIANT (t2);
283 : 11000340 : int extended1 = 0;
284 : 11000340 : int extended2 = 0;
285 : :
286 : 11000340 : if (mv1 == mv2)
287 : : return 0;
288 : :
289 : 87991400 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
290 : : {
291 : 76992475 : if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 : 4888862 : extended1 = i + 1;
293 : 76992475 : if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 : 4175605 : extended2 = i + 1;
295 : : }
296 : 10998925 : if (mv1 == bfloat16_type_node)
297 : 1130030 : extended1 = true;
298 : 10998925 : if (mv2 == bfloat16_type_node)
299 : 915849 : extended2 = true;
300 : 10998925 : if (extended2 && !extended1)
301 : : {
302 : 4890000 : int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 : 4890000 : return ret == 3 ? 3 : -ret;
304 : : }
305 : :
306 : 6108925 : const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 : 6108925 : const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 : 6108925 : 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 : 48871400 : int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
315 : 12217850 : ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
316 : 48871400 : int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
317 : 12217850 : ? 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 : 6108925 : if ((p1 > p2 && fmt1->emax >= fmt2->emax)
322 : 4704229 : || (p1 == p2 && fmt1->emax > fmt2->emax))
323 : : return 2;
324 : 4704229 : if ((p1 < p2 && fmt1->emax <= fmt2->emax)
325 : 1553695 : || (p1 == p2 && fmt1->emax < fmt2->emax))
326 : : return -2;
327 : 1553695 : if ((p1 > p2 && fmt1->emax < fmt2->emax)
328 : 1545117 : || (p1 < p2 && fmt1->emax > fmt2->emax))
329 : : return 3;
330 : 1536539 : 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 : 1536539 : 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 : 6146140 : for (p = &float_type_node; p <= &long_double_type_node; ++p)
375 : : {
376 : 4609605 : const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
377 : 4609605 : gcc_assert (fmt3->b == 2);
378 : 36876840 : int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
379 : 9219210 : ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
380 : 4609605 : if (p1 == p3 && fmt1->emax == fmt3->emax)
381 : 1343138 : ++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 : 1536535 : 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 : 95715561 : cp_common_type (tree t1, tree t2)
413 : : {
414 : 95715561 : enum tree_code code1 = TREE_CODE (t1);
415 : 95715561 : enum tree_code code2 = TREE_CODE (t2);
416 : 95715561 : tree attributes;
417 : 95715561 : 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 : 95715561 : attributes = (*targetm.merge_type_attributes) (t1, t2);
424 : :
425 : 95715561 : if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
426 : : {
427 : 1446018 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
428 : 1446018 : return build_type_attribute_variant (t1, attributes);
429 : : else
430 : : return NULL_TREE;
431 : : }
432 : :
433 : : /* FIXME: Attributes. */
434 : 94269543 : gcc_assert (ARITHMETIC_TYPE_P (t1)
435 : : || VECTOR_TYPE_P (t1)
436 : : || UNSCOPED_ENUM_P (t1));
437 : 94269543 : 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 : 94269543 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
445 : : {
446 : 228715 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
447 : 228715 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
448 : 228715 : tree subtype
449 : 228715 : = type_after_usual_arithmetic_conversions (subtype1, subtype2);
450 : :
451 : 228715 : if (subtype == error_mark_node)
452 : : return subtype;
453 : 228715 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
454 : 195301 : return build_type_attribute_variant (t1, attributes);
455 : 33414 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
456 : 33354 : return build_type_attribute_variant (t2, attributes);
457 : : else
458 : 60 : return build_type_attribute_variant (build_complex_type (subtype),
459 : 60 : attributes);
460 : : }
461 : :
462 : 94040828 : 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 : 50303 : if (TYPE_UNSIGNED (t1))
467 : 11031 : return merge_type_attributes_from (t1, t2);
468 : : else
469 : 39272 : return merge_type_attributes_from (t2, t1);
470 : : }
471 : :
472 : : /* If only one is real, use it as the result. */
473 : 93990525 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
474 : 1110315 : return build_type_attribute_variant (t1, attributes);
475 : 92880210 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
476 : 1036857 : return build_type_attribute_variant (t2, attributes);
477 : :
478 : 91843353 : if (code1 == REAL_TYPE
479 : 91843353 : && (extended_float_type_p (t1) || extended_float_type_p (t2)))
480 : : {
481 : 200558 : tree mv1 = TYPE_MAIN_VARIANT (t1);
482 : 200558 : tree mv2 = TYPE_MAIN_VARIANT (t2);
483 : 200558 : if (mv1 == mv2)
484 : 196806 : return build_type_attribute_variant (t1, attributes);
485 : :
486 : 3752 : int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
487 : 3752 : if (cmpret == 3)
488 : 0 : return error_mark_node;
489 : 3752 : else if (cmpret >= 0)
490 : 3490 : 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 : 91642795 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
497 : 13285395 : return build_type_attribute_variant (t1, attributes);
498 : 78357400 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
499 : 1494977 : return build_type_attribute_variant (t2, attributes);
500 : :
501 : : /* The types are the same; no need to do anything fancy. */
502 : 76862423 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
503 : 70751588 : return build_type_attribute_variant (t1, attributes);
504 : :
505 : 6110835 : if (code1 != REAL_TYPE)
506 : : {
507 : : /* If one is unsigned long long, then convert the other to unsigned
508 : : long long. */
509 : 6110832 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
510 : 6110832 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
511 : 122499 : return build_type_attribute_variant (long_long_unsigned_type_node,
512 : 122499 : 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 : 5988333 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
524 : 5988333 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
525 : : {
526 : 1358 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
527 : 685 : ? long_long_unsigned_type_node
528 : 685 : : long_long_integer_type_node);
529 : 685 : return build_type_attribute_variant (t, attributes);
530 : : }
531 : :
532 : : /* Go through the same procedure, but for longs. */
533 : 5987648 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
534 : 5987648 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
535 : 745058 : return build_type_attribute_variant (long_unsigned_type_node,
536 : 745058 : attributes);
537 : 5242590 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
538 : 5242590 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
539 : : {
540 : 72185 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
541 : 72185 : ? long_unsigned_type_node : long_integer_type_node);
542 : 36467 : 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 : 10412214 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
550 : : {
551 : 5206123 : if (int_n_enabled_p [i]
552 : 5206123 : && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
553 : 5125302 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
554 : 5125298 : || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
555 : 5125298 : || 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 : 5206091 : if (TYPE_UNSIGNED (t1))
566 : 4169885 : return build_type_attribute_variant (t1, attributes);
567 : : else
568 : 1036206 : 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 : 905319 : type_after_usual_arithmetic_conversions (tree t1, tree t2)
600 : : {
601 : 905319 : gcc_assert (ARITHMETIC_TYPE_P (t1)
602 : : || VECTOR_TYPE_P (t1)
603 : : || UNSCOPED_ENUM_P (t1));
604 : 905319 : 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 : 905319 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
610 : 676054 : && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
611 : : {
612 : 675704 : t1 = type_promotes_to (t1);
613 : 675704 : t2 = type_promotes_to (t2);
614 : : }
615 : :
616 : 905319 : 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 : 4117721 : 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 : 4117721 : tree pointee1;
660 : 4117721 : tree pointee2;
661 : 4117721 : tree result_type;
662 : 4117721 : tree attributes;
663 : :
664 : : /* Determine the types pointed to by T1 and T2. */
665 : 4117721 : if (TYPE_PTR_P (t1))
666 : : {
667 : 4117225 : pointee1 = TREE_TYPE (t1);
668 : 4117225 : 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 : 4117721 : 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 : 4117630 : const int q1 = cp_type_quals (pointee1);
700 : 4117630 : const int q2 = cp_type_quals (pointee2);
701 : 4117630 : const int quals = q1 | q2;
702 : 4117630 : result_type = cp_build_qualified_type (result_type,
703 : 4117630 : (quals | (*add_const
704 : 4117630 : ? 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 : 4117630 : if (quals != q1 || quals != q2)
710 : 552309 : *add_const = true;
711 : : /* If the original types were pointers to members, so is the
712 : : result. */
713 : 4117630 : 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 : 4117144 : result_type = build_pointer_type (result_type);
729 : :
730 : : /* Merge the attributes. */
731 : 4117630 : attributes = (*targetm.merge_type_attributes) (t1, t2);
732 : 4117630 : 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 : 4320277 : 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 : 4320277 : tree class1;
750 : 4320277 : 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 : 4320277 : if (null_ptr_cst_p (arg1))
757 : : return t2;
758 : 4320098 : 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 : 4298073 : 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 : 4298073 : if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
776 : : {
777 : 180214 : tree attributes;
778 : 180214 : tree result_type;
779 : :
780 : 180214 : 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 : 180213 : result_type
810 : 180213 : = cp_build_qualified_type (void_type_node,
811 : 180213 : (cp_type_quals (TREE_TYPE (t1))
812 : 180213 : | cp_type_quals (TREE_TYPE (t2))));
813 : 180213 : result_type = build_pointer_type (result_type);
814 : : /* Merge the attributes. */
815 : 180213 : attributes = (*targetm.merge_type_attributes) (t1, t2);
816 : 180213 : return build_type_attribute_variant (result_type, attributes);
817 : : }
818 : :
819 : 4117859 : 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 : 4117859 : if (fnptr_conv_p (t1, t2))
830 : : return t1;
831 : 4117791 : 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 : 4117210 : if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
837 : 4117210 : && CLASS_TYPE_P (TREE_TYPE (t1))
838 : 724349 : && CLASS_TYPE_P (TREE_TYPE (t2))
839 : 4842073 : && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
840 : 724346 : TREE_TYPE (t2)))
841 : : {
842 : 31277 : class1 = TREE_TYPE (t1);
843 : 31277 : class2 = TREE_TYPE (t2);
844 : :
845 : 31277 : if (DERIVED_FROM_P (class1, class2))
846 : 3260 : t2 = (build_pointer_type
847 : 1630 : (cp_build_qualified_type (class1, cp_type_quals (class2))));
848 : 29647 : else if (DERIVED_FROM_P (class2, class1))
849 : 59258 : t1 = (build_pointer_type
850 : 29629 : (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 : 4086053 : else if (TYPE_PTRMEM_P (t1)
862 : 4086570 : && !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 : 4117688 : bool add_const = false;
900 : 4117688 : return composite_pointer_type_r (location, t1, t2, &add_const, operation,
901 : 4117688 : 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 : 29704077 : merge_types (tree t1, tree t2)
913 : : {
914 : 29704077 : enum tree_code code1;
915 : 29704077 : enum tree_code code2;
916 : 29704077 : tree attributes;
917 : :
918 : : /* Save time if the two types are the same. */
919 : 29704077 : if (t1 == t2)
920 : : return t1;
921 : 16388621 : if (original_type (t1) == original_type (t2))
922 : : return t1;
923 : :
924 : : /* If one type is nonsense, use the other. */
925 : 16315468 : if (t1 == error_mark_node)
926 : : return t2;
927 : 16315468 : if (t2 == error_mark_node)
928 : : return t1;
929 : :
930 : : /* Handle merging an auto redeclaration with a previous deduced
931 : : return type. */
932 : 16315468 : if (is_auto (t1))
933 : : return t2;
934 : :
935 : : /* Merge the attributes. */
936 : 16305711 : attributes = (*targetm.merge_type_attributes) (t1, t2);
937 : :
938 : 16305711 : if (TYPE_PTRMEMFUNC_P (t1))
939 : 15 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
940 : 16305711 : if (TYPE_PTRMEMFUNC_P (t2))
941 : 15 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
942 : :
943 : 16305711 : code1 = TREE_CODE (t1);
944 : 16305711 : code2 = TREE_CODE (t2);
945 : 16305711 : 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 : 16305711 : switch (code1)
961 : : {
962 : 2747951 : case POINTER_TYPE:
963 : 2747951 : case REFERENCE_TYPE:
964 : : /* For two pointers, do this recursively on the target type. */
965 : 2747951 : {
966 : 2747951 : tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
967 : 2747951 : int quals = cp_type_quals (t1);
968 : :
969 : 2747951 : if (code1 == POINTER_TYPE)
970 : : {
971 : 729159 : t1 = build_pointer_type (target);
972 : 729159 : if (TREE_CODE (target) == METHOD_TYPE)
973 : 15 : t1 = build_ptrmemfunc_type (t1);
974 : : }
975 : : else
976 : 2018792 : t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
977 : 2747951 : t1 = build_type_attribute_variant (t1, attributes);
978 : 2747951 : t1 = cp_build_qualified_type (t1, quals);
979 : :
980 : 2747951 : 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 : 10771 : case ARRAY_TYPE:
997 : 10771 : {
998 : 10771 : 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 : 21542 : if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
1001 : 10749 : 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 : 4315617 : 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 : 4315617 : {
1014 : 4315617 : tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1015 : 4315617 : tree p1 = TYPE_ARG_TYPES (t1);
1016 : 4315617 : tree p2 = TYPE_ARG_TYPES (t2);
1017 : 4315617 : tree parms;
1018 : :
1019 : : /* Save space: see if the result is identical to one of the args. */
1020 : 4315617 : if (valtype == TREE_TYPE (t1) && ! p2)
1021 : 0 : return cp_build_type_attribute_variant (t1, attributes);
1022 : 4315617 : 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 : 8631234 : if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1027 : : parms = p2;
1028 : 8339772 : else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1029 : : parms = p1;
1030 : : else
1031 : 4169886 : parms = commonparms (p1, p2);
1032 : :
1033 : 4315617 : cp_cv_quals quals = type_memfn_quals (t1);
1034 : 4315617 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1035 : 4315617 : gcc_assert (quals == type_memfn_quals (t2));
1036 : 4315617 : gcc_assert (rqual == type_memfn_rqual (t2));
1037 : :
1038 : 4315617 : tree rval = cp_build_function_type (valtype, parms);
1039 : 4315617 : rval = apply_memfn_quals (rval, quals);
1040 : 4315617 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1041 : 4315617 : TYPE_RAISES_EXCEPTIONS (t2));
1042 : 4315617 : bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1043 : 4315617 : t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1044 : 4315617 : break;
1045 : : }
1046 : :
1047 : 3977591 : case METHOD_TYPE:
1048 : 3977591 : {
1049 : : /* Get this value the long way, since TYPE_METHOD_BASETYPE
1050 : : is just the main variant of this. */
1051 : 3977591 : tree basetype = class_of_this_parm (t2);
1052 : 3977591 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1053 : 3977591 : TYPE_RAISES_EXCEPTIONS (t2));
1054 : 3977591 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1055 : 3977591 : tree t3;
1056 : 3977591 : 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 : 3977591 : t1 = cp_build_function_type (TREE_TYPE (t1),
1062 : 3977591 : TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1063 : 3977591 : t2 = cp_build_function_type (TREE_TYPE (t2),
1064 : 3977591 : TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1065 : 3977591 : t3 = merge_types (t1, t2);
1066 : 3977591 : t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1067 : 3977591 : TYPE_ARG_TYPES (t3));
1068 : 3977591 : t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1069 : 3977591 : 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 : 5244056 : default:;
1079 : 5244056 : 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 : 8293226 : return cp_build_type_attribute_variant (t1, attributes);
1087 : : }
1088 : :
1089 : : /* Return the ARRAY_TYPE type without its domain. */
1090 : :
1091 : : tree
1092 : 480 : strip_array_domain (tree type)
1093 : : {
1094 : 480 : tree t2;
1095 : 480 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1096 : 480 : 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 : 453905 : common_type (tree t1, tree t2)
1112 : : {
1113 : : /* If one type is nonsense, use the other */
1114 : 453905 : if (t1 == error_mark_node)
1115 : : return t2;
1116 : 453905 : if (t2 == error_mark_node)
1117 : : return t1;
1118 : :
1119 : 453905 : 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 : 1296319 : common_pointer_type (tree t1, tree t2)
1131 : : {
1132 : 1296319 : 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 : 1296319 : return composite_pointer_type (input_location, t1, t2,
1137 : : error_mark_node, error_mark_node,
1138 : 1296319 : 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 : 1668 : comp_except_types (tree a, tree b, bool exact)
1159 : : {
1160 : 1668 : 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 : 1425076216 : comp_except_specs (const_tree t1, const_tree t2, int exact)
1195 : : {
1196 : 1425076216 : const_tree probe;
1197 : 1425076216 : const_tree base;
1198 : 1425076216 : int length = 0;
1199 : :
1200 : 1425076216 : if (t1 == t2)
1201 : : return true;
1202 : :
1203 : : /* First handle noexcept. */
1204 : 557868334 : if (exact < ce_exact)
1205 : : {
1206 : 6928906 : if (exact == ce_type
1207 : 13793283 : && (canonical_eh_spec (CONST_CAST_TREE (t1))
1208 : 6864377 : == 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 : 6909975 : 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 : 6909822 : if (t2 == noexcept_false_spec)
1218 : 1688 : return t1 == NULL_TREE;
1219 : :
1220 : : /* Otherwise, if we aren't looking for an exact match, noexcept is
1221 : : equivalent to throw(). */
1222 : 6908134 : if (t1 == noexcept_true_spec)
1223 : 688381 : t1 = empty_except_spec;
1224 : 6908134 : if (t2 == noexcept_true_spec)
1225 : 5952316 : 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 : 535317876 : if ((t1 && TREE_PURPOSE (t1))
1232 : 583116384 : || (t2 && TREE_PURPOSE (t2)))
1233 : 549000369 : return (t1 && t2
1234 : 549000369 : && (exact == ce_exact
1235 : 63174576 : ? TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
1236 : 264504 : : cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))));
1237 : :
1238 : 8847193 : if (t1 == NULL_TREE) /* T1 is ... */
1239 : 6170011 : return t2 == NULL_TREE || exact == ce_derived;
1240 : 2677182 : if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1241 : 2644198 : return t2 != NULL_TREE && !TREE_VALUE (t2);
1242 : 34290 : if (t2 == NULL_TREE) /* T2 is ... */
1243 : : return false;
1244 : 1546 : 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 : 2633 : for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1252 : : {
1253 : 2066 : for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1254 : : {
1255 : 1668 : tree a = TREE_VALUE (probe);
1256 : 1668 : tree b = TREE_VALUE (t2);
1257 : :
1258 : 1668 : if (comp_except_types (a, b, exact))
1259 : : {
1260 : 1163 : if (probe == base && exact > ce_derived)
1261 : 1128 : base = TREE_CHAIN (probe);
1262 : 1163 : length++;
1263 : 1163 : break;
1264 : : }
1265 : : }
1266 : 1163 : if (probe == NULL_TREE)
1267 : : return false;
1268 : : }
1269 : 1072 : 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 : 1387397 : comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1280 : : bool strict)
1281 : : {
1282 : 1387397 : tree d1;
1283 : 1387397 : tree d2;
1284 : 1387397 : tree max1, max2;
1285 : :
1286 : 1387397 : if (t1 == t2)
1287 : : return true;
1288 : :
1289 : : /* The type of the array elements must be the same. */
1290 : 1387305 : if (strict
1291 : 1387305 : ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1292 : 3142 : : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1293 : : return false;
1294 : :
1295 : 931977 : d1 = TYPE_DOMAIN (t1);
1296 : 931977 : d2 = TYPE_DOMAIN (t2);
1297 : :
1298 : 931977 : 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 : 601328 : if (!d1 && d2)
1314 : 2334 : return cb >= bounds_either;
1315 : 598994 : else if (d1 && !d2)
1316 : 4494 : return cb == bounds_either;
1317 : :
1318 : : /* Check that the dimensions are the same. */
1319 : :
1320 : 594500 : if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1321 : : return false;
1322 : 594479 : max1 = TYPE_MAX_VALUE (d1);
1323 : 594479 : max2 = TYPE_MAX_VALUE (d2);
1324 : :
1325 : 594479 : 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 : 1483391539 : comp_template_parms_position (tree t1, tree t2)
1338 : : {
1339 : 1483391539 : tree index1, index2;
1340 : 1483391539 : 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 : 1483391539 : index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1347 : 1483391539 : index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1348 : :
1349 : : /* Then compare their relative position. */
1350 : 1483391539 : if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1351 : 882762001 : || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1352 : 2142806627 : || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1353 : 659415088 : != 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 : 581512074 : if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1359 : 110857418 : return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1360 : :
1361 : : return true;
1362 : : }
1363 : :
1364 : : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1365 : :
1366 : : static bool
1367 : 512 : cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1368 : : {
1369 : 512 : t1 = TYPE_MAIN_VARIANT (t1);
1370 : 512 : t2 = TYPE_MAIN_VARIANT (t2);
1371 : :
1372 : 512 : if (TYPE_PTR_P (t1)
1373 : 141 : && TYPE_PTR_P (t2))
1374 : : return true;
1375 : :
1376 : : /* Only the precision of the parameter matters. This check should
1377 : : make sure that the callee does not see undefined values in argument
1378 : : registers. */
1379 : 377 : if (INTEGRAL_TYPE_P (t1)
1380 : 68 : && INTEGRAL_TYPE_P (t2)
1381 : 424 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1382 : : return true;
1383 : :
1384 : 333 : return same_type_p (t1, t2);
1385 : : }
1386 : :
1387 : : /* Check if a type cast between two function types can be considered safe. */
1388 : :
1389 : : static bool
1390 : 6916 : cxx_safe_function_type_cast_p (tree t1, tree t2)
1391 : : {
1392 : 6916 : if (TREE_TYPE (t1) == void_type_node &&
1393 : 6843 : TYPE_ARG_TYPES (t1) == void_list_node)
1394 : : return true;
1395 : :
1396 : 5435 : if (TREE_TYPE (t2) == void_type_node &&
1397 : 5229 : TYPE_ARG_TYPES (t2) == void_list_node)
1398 : : return true;
1399 : :
1400 : 273 : if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1401 : : return false;
1402 : :
1403 : 112 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1404 : 315 : t1 && t2;
1405 : 203 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1406 : 239 : if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1407 : : return false;
1408 : :
1409 : : return true;
1410 : : }
1411 : :
1412 : : /* Subroutine in comptypes. */
1413 : :
1414 : : static bool
1415 : 12489001098 : structural_comptypes (tree t1, tree t2, int strict)
1416 : : {
1417 : : /* Both should be types that are not obviously the same. */
1418 : 12489001098 : 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 : 12489001098 : if (!comparing_specializations)
1423 : : {
1424 : : /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1425 : : current instantiation. */
1426 : 10378849065 : if (TREE_CODE (t1) == TYPENAME_TYPE)
1427 : 90908902 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1428 : :
1429 : 10378849065 : if (TREE_CODE (t2) == TYPENAME_TYPE)
1430 : 111980029 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1431 : : }
1432 : :
1433 : 12489001098 : if (TYPE_PTRMEMFUNC_P (t1))
1434 : 26803733 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1435 : 12489001098 : if (TYPE_PTRMEMFUNC_P (t2))
1436 : 30162294 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1437 : :
1438 : : /* Different classes of types can't be compatible. */
1439 : 12489001098 : 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 : 8015766896 : if (TREE_CODE (t1) != ARRAY_TYPE
1445 : 8015766896 : && cp_type_quals (t1) != cp_type_quals (t2))
1446 : : return false;
1447 : 7526788766 : if (TREE_CODE (t1) == FUNCTION_TYPE
1448 : 7526788766 : && 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 : 7526681180 : if (FUNC_OR_METHOD_TYPE_P (t1))
1453 : : {
1454 : 33586216 : if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1455 : : return false;
1456 : 17855286 : if (flag_noexcept_type
1457 : 35639912 : && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1458 : 17784626 : 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 : 7504278403 : if (TREE_CODE (t1) != ARRAY_TYPE
1467 : 7504278403 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1468 : 334969760 : 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 : 7169308643 : 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 : 518327237 : case OPAQUE_TYPE:
1481 : 518327237 : case INTEGER_TYPE:
1482 : 518327237 : case FIXED_POINT_TYPE:
1483 : 518327237 : 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 : 518327237 : 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 : 1066939 : case TEMPLATE_TEMPLATE_PARM:
1505 : 1066939 : case BOUND_TEMPLATE_TEMPLATE_PARM:
1506 : 1066939 : if (!comp_template_parms_position (t1, t2))
1507 : : return false;
1508 : 896842 : if (!comp_template_parms
1509 : 1793684 : (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1510 : 2486394 : DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1511 : : return false;
1512 : 607473 : if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1513 : : break;
1514 : : /* Don't check inheritance. */
1515 : : strict = COMPARE_STRICT;
1516 : : /* Fall through. */
1517 : :
1518 : 3612599911 : case RECORD_TYPE:
1519 : 3612599911 : case UNION_TYPE:
1520 : 6360079055 : if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1521 : 2231897392 : && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1522 : 1719302202 : || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1523 : 4125234097 : && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1524 : : break;
1525 : :
1526 : 3573206572 : if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1527 : : break;
1528 : 3573206539 : else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1529 : : break;
1530 : :
1531 : : return false;
1532 : :
1533 : 44728 : case OFFSET_TYPE:
1534 : 44728 : if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1535 : : strict & ~COMPARE_REDECLARATION))
1536 : : return false;
1537 : 42320 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1538 : : return false;
1539 : : break;
1540 : :
1541 : 810752228 : case REFERENCE_TYPE:
1542 : 810752228 : if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1543 : : return false;
1544 : : /* fall through to checks for pointer types */
1545 : 1130230115 : gcc_fallthrough ();
1546 : :
1547 : 1130230115 : case POINTER_TYPE:
1548 : 1130230115 : if (TYPE_MODE (t1) != TYPE_MODE (t2)
1549 : 1130230115 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1550 : 1019206821 : return false;
1551 : : break;
1552 : :
1553 : 3469382 : case FUNCTION_TYPE:
1554 : 3469382 : if (TYPE_NO_NAMED_ARGS_STDARG_P (t1) != TYPE_NO_NAMED_ARGS_STDARG_P (t2))
1555 : : return false;
1556 : : /* FALLTHRU */
1557 : 10865290 : case METHOD_TYPE:
1558 : : /* Exception specs and memfn_rquals were checked above. */
1559 : 10865290 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1560 : : return false;
1561 : 10223309 : if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1562 : : return false;
1563 : : break;
1564 : :
1565 : 1384163 : case ARRAY_TYPE:
1566 : : /* Target types must match incl. qualifiers. */
1567 : 1384163 : if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1568 : 1384163 : ? bounds_either : bounds_none),
1569 : : /*strict=*/true))
1570 : : return false;
1571 : : break;
1572 : :
1573 : 1482324600 : case TEMPLATE_TYPE_PARM:
1574 : : /* If T1 and T2 don't have the same relative position in their
1575 : : template parameters set, they can't be equal. */
1576 : 1482324600 : if (!comp_template_parms_position (t1, t2))
1577 : : return false;
1578 : : /* If T1 and T2 don't represent the same class template deduction,
1579 : : they aren't equal. */
1580 : 1008731640 : if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1581 : 504365820 : CLASS_PLACEHOLDER_TEMPLATE (t2)))
1582 : : return false;
1583 : : /* Constrained 'auto's are distinct from parms that don't have the same
1584 : : constraints. */
1585 : 493414936 : if (!equivalent_placeholder_constraints (t1, t2))
1586 : : return false;
1587 : : break;
1588 : :
1589 : 140641124 : case TYPENAME_TYPE:
1590 : 281282248 : if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1591 : 140641124 : TYPENAME_TYPE_FULLNAME (t2)))
1592 : : return false;
1593 : : /* Qualifiers don't matter on scopes. */
1594 : 122957989 : if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1595 : 122957989 : TYPE_CONTEXT (t2)))
1596 : : return false;
1597 : : break;
1598 : :
1599 : 7933 : case UNBOUND_CLASS_TEMPLATE:
1600 : 7933 : if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1601 : : return false;
1602 : 7933 : if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1603 : : return false;
1604 : : break;
1605 : :
1606 : 3289942 : case COMPLEX_TYPE:
1607 : 3289942 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1608 : : return false;
1609 : : break;
1610 : :
1611 : 11104176 : case VECTOR_TYPE:
1612 : 11104176 : if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1613 : 20772457 : || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1614 : 20820001 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1615 : 1435895 : return false;
1616 : : break;
1617 : :
1618 : 6514141 : case TYPE_PACK_EXPANSION:
1619 : 6514141 : return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1620 : : PACK_EXPANSION_PATTERN (t2))
1621 : 12871614 : && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1622 : 6357473 : PACK_EXPANSION_EXTRA_ARGS (t2)));
1623 : :
1624 : 14 : case PACK_INDEX_TYPE:
1625 : 14 : return (cp_tree_equal (PACK_INDEX_PACK (t1),
1626 : 14 : PACK_INDEX_PACK (t2))
1627 : 16 : && cp_tree_equal (PACK_INDEX_INDEX (t1),
1628 : 2 : PACK_INDEX_INDEX (t2)));
1629 : :
1630 : 14730319 : case DECLTYPE_TYPE:
1631 : 29460638 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1632 : 14730319 : != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1633 : : return false;
1634 : 14693181 : if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1635 : : return false;
1636 : 14693181 : if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1637 : : return false;
1638 : 14693181 : if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1639 : : return false;
1640 : : break;
1641 : :
1642 : 709085 : case TRAIT_TYPE:
1643 : 709085 : if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1644 : : return false;
1645 : 1326 : if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1646 : 2652 : || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1647 : 0 : return false;
1648 : : break;
1649 : :
1650 : 3 : case TYPEOF_TYPE:
1651 : 3 : if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1652 : : return false;
1653 : : break;
1654 : :
1655 : : default:
1656 : : return false;
1657 : : }
1658 : :
1659 : : /* If we get here, we know that from a target independent POV the
1660 : : types are the same. Make sure the target attributes are also
1661 : : the same. */
1662 : 666141802 : if (!comp_type_attributes (t1, t2))
1663 : : return false;
1664 : :
1665 : 666140658 : check_alias:
1666 : 1001118817 : if (comparing_dependent_aliases
1667 : 1001118817 : && (typedef_variant_p (t1) || typedef_variant_p (t2)))
1668 : : {
1669 : 3120533 : tree dep1 = dependent_opaque_alias_p (t1) ? t1 : NULL_TREE;
1670 : 3120533 : tree dep2 = dependent_opaque_alias_p (t2) ? t2 : NULL_TREE;
1671 : 3120533 : if ((dep1 || dep2)
1672 : 3120533 : && (!(dep1 && dep2)
1673 : 0 : || !comp_type_attributes (DECL_ATTRIBUTES (TYPE_NAME (dep1)),
1674 : 0 : DECL_ATTRIBUTES (TYPE_NAME (dep2)))))
1675 : 12 : return false;
1676 : :
1677 : : /* Don't treat an alias template specialization with dependent
1678 : : arguments as equivalent to its underlying type when used as a
1679 : : template argument; we need them to be distinct so that we
1680 : : substitute into the specialization arguments at instantiation
1681 : : time. And aliases can't be equivalent without being ==, so
1682 : : we don't need to look any deeper. */
1683 : 3120521 : ++processing_template_decl;
1684 : 3120521 : dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1685 : 3120521 : dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1686 : 3120521 : --processing_template_decl;
1687 : 3120521 : if ((dep1 || dep2) && dep1 != dep2)
1688 : : return false;
1689 : : }
1690 : :
1691 : : return true;
1692 : : }
1693 : :
1694 : : /* C++ implementation of compatible_types_for_indirection_note_p. */
1695 : :
1696 : : bool
1697 : 1675 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1698 : : {
1699 : 1675 : return same_type_p (type1, type2);
1700 : : }
1701 : :
1702 : : /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1703 : : is a bitwise-or of the COMPARE_* flags. */
1704 : :
1705 : : bool
1706 : 19318161370 : comptypes (tree t1, tree t2, int strict)
1707 : : {
1708 : 19318161370 : gcc_checking_assert (t1 && t2);
1709 : :
1710 : : /* TYPE_ARGUMENT_PACKS are not really types. */
1711 : 19318161370 : gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1712 : : && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1713 : :
1714 : 19318161370 : if (t1 == t2)
1715 : : return true;
1716 : :
1717 : : /* Suppress errors caused by previously reported errors. */
1718 : 12489002297 : if (t1 == error_mark_node || t2 == error_mark_node)
1719 : : return false;
1720 : :
1721 : 12489002101 : if (strict == COMPARE_STRICT)
1722 : : {
1723 : 11285434337 : if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1724 : : /* At least one of the types requires structural equality, so
1725 : : perform a deep check. */
1726 : 968259728 : return structural_comptypes (t1, t2, strict);
1727 : :
1728 : 10317174609 : if (flag_checking && param_use_canonical_types)
1729 : : {
1730 : 10317173606 : bool result = structural_comptypes (t1, t2, strict);
1731 : :
1732 : 10317173606 : if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1733 : : /* The two types are structurally equivalent, but their
1734 : : canonical types were different. This is a failure of the
1735 : : canonical type propagation code.*/
1736 : 0 : internal_error
1737 : 0 : ("canonical types differ for identical types %qT and %qT",
1738 : : t1, t2);
1739 : 10317173606 : else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1740 : : /* Two types are structurally different, but the canonical
1741 : : types are the same. This means we were over-eager in
1742 : : assigning canonical types. */
1743 : 0 : internal_error
1744 : 0 : ("same canonical type node for different types %qT and %qT",
1745 : : t1, t2);
1746 : :
1747 : : return result;
1748 : : }
1749 : 1003 : if (!flag_checking && param_use_canonical_types)
1750 : 1003 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1751 : : else
1752 : 0 : return structural_comptypes (t1, t2, strict);
1753 : : }
1754 : 1203567764 : else if (strict == COMPARE_STRUCTURAL)
1755 : 1165238922 : return structural_comptypes (t1, t2, COMPARE_STRICT);
1756 : : else
1757 : 38328842 : return structural_comptypes (t1, t2, strict);
1758 : : }
1759 : :
1760 : : /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1761 : : top-level qualifiers. */
1762 : :
1763 : : bool
1764 : 2865525515 : same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1765 : : {
1766 : 2865525515 : if (type1 == error_mark_node || type2 == error_mark_node)
1767 : : return false;
1768 : 2865525505 : if (type1 == type2)
1769 : : return true;
1770 : :
1771 : 1564434622 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1772 : 1564434622 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1773 : 1564434622 : return same_type_p (type1, type2);
1774 : : }
1775 : :
1776 : : /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1777 : :
1778 : : bool
1779 : 223307167 : similar_type_p (tree type1, tree type2)
1780 : : {
1781 : 223307167 : if (type1 == error_mark_node || type2 == error_mark_node)
1782 : : return false;
1783 : :
1784 : : /* Informally, two types are similar if, ignoring top-level cv-qualification:
1785 : : * they are the same type; or
1786 : : * they are both pointers, and the pointed-to types are similar; or
1787 : : * they are both pointers to member of the same class, and the types of
1788 : : the pointed-to members are similar; or
1789 : : * they are both arrays of the same size or both arrays of unknown bound,
1790 : : and the array element types are similar. */
1791 : :
1792 : 223307167 : if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1793 : : return true;
1794 : :
1795 : 100328188 : if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1796 : 100213798 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1797 : 100213798 : || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1798 : 115161 : return comp_ptr_ttypes_const (type1, type2, bounds_either);
1799 : :
1800 : : return false;
1801 : : }
1802 : :
1803 : : /* Helper function for layout_compatible_type_p and
1804 : : is_corresponding_member_aggr. Advance to next members (NULL if
1805 : : no further ones) and return true if those members are still part of
1806 : : the common initial sequence. */
1807 : :
1808 : : bool
1809 : 1996 : next_common_initial_sequence (tree &memb1, tree &memb2)
1810 : : {
1811 : 2456 : while (memb1)
1812 : : {
1813 : 2492 : if (TREE_CODE (memb1) != FIELD_DECL
1814 : 2090 : || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1815 : : {
1816 : 402 : memb1 = DECL_CHAIN (memb1);
1817 : 402 : continue;
1818 : : }
1819 : 1688 : if (DECL_FIELD_IS_BASE (memb1))
1820 : : {
1821 : 58 : memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1822 : 58 : continue;
1823 : : }
1824 : : break;
1825 : : }
1826 : 2426 : while (memb2)
1827 : : {
1828 : 2462 : if (TREE_CODE (memb2) != FIELD_DECL
1829 : 2060 : || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1830 : : {
1831 : 402 : memb2 = DECL_CHAIN (memb2);
1832 : 402 : continue;
1833 : : }
1834 : 1658 : if (DECL_FIELD_IS_BASE (memb2))
1835 : : {
1836 : 28 : memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1837 : 28 : continue;
1838 : : }
1839 : : break;
1840 : : }
1841 : 1996 : if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1842 : : return true;
1843 : 1630 : if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1844 : : return false;
1845 : 1630 : if (DECL_BIT_FIELD_TYPE (memb1))
1846 : : {
1847 : 138 : if (!DECL_BIT_FIELD_TYPE (memb2))
1848 : : return false;
1849 : 39 : if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1850 : 39 : DECL_BIT_FIELD_TYPE (memb2)))
1851 : : return false;
1852 : 39 : if (TYPE_PRECISION (TREE_TYPE (memb1))
1853 : 39 : != TYPE_PRECISION (TREE_TYPE (memb2)))
1854 : : return false;
1855 : : }
1856 : 1492 : else if (DECL_BIT_FIELD_TYPE (memb2))
1857 : : return false;
1858 : 1465 : else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1859 : : return false;
1860 : 1441 : if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1861 : 1441 : != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1862 : : return false;
1863 : 1414 : if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1864 : : return false;
1865 : 1383 : if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1866 : : return false;
1867 : : return true;
1868 : : }
1869 : :
1870 : : /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1871 : :
1872 : : bool
1873 : 2503 : layout_compatible_type_p (tree type1, tree type2)
1874 : : {
1875 : 2503 : if (type1 == error_mark_node || type2 == error_mark_node)
1876 : : return false;
1877 : 2503 : if (type1 == type2)
1878 : : return true;
1879 : 1222 : if (TREE_CODE (type1) != TREE_CODE (type2))
1880 : : return false;
1881 : :
1882 : 1184 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1883 : 1184 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1884 : :
1885 : 1184 : if (TREE_CODE (type1) == ENUMERAL_TYPE)
1886 : 32 : return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1887 : 32 : && same_type_p (finish_underlying_type (type1),
1888 : : finish_underlying_type (type2)));
1889 : :
1890 : 367 : if (CLASS_TYPE_P (type1)
1891 : 367 : && std_layout_type_p (type1)
1892 : 357 : && std_layout_type_p (type2)
1893 : 1509 : && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1894 : : {
1895 : 315 : tree field1 = TYPE_FIELDS (type1);
1896 : 315 : tree field2 = TYPE_FIELDS (type2);
1897 : 315 : if (TREE_CODE (type1) == RECORD_TYPE)
1898 : : {
1899 : 966 : while (1)
1900 : : {
1901 : 606 : if (!next_common_initial_sequence (field1, field2))
1902 : : return false;
1903 : 600 : if (field1 == NULL_TREE)
1904 : : return true;
1905 : 360 : field1 = DECL_CHAIN (field1);
1906 : 360 : field2 = DECL_CHAIN (field2);
1907 : : }
1908 : : }
1909 : : /* Otherwise both types must be union types.
1910 : : The standard says:
1911 : : "Two standard-layout unions are layout-compatible if they have
1912 : : the same number of non-static data members and corresponding
1913 : : non-static data members (in any order) have layout-compatible
1914 : : types."
1915 : : but the code anticipates that bitfield vs. non-bitfield,
1916 : : different bitfield widths or presence/absence of
1917 : : [[no_unique_address]] should be checked as well. */
1918 : 69 : auto_vec<tree, 16> vec;
1919 : 69 : unsigned int count = 0;
1920 : 315 : for (; field1; field1 = DECL_CHAIN (field1))
1921 : 246 : if (TREE_CODE (field1) == FIELD_DECL)
1922 : 162 : count++;
1923 : 315 : for (; field2; field2 = DECL_CHAIN (field2))
1924 : 246 : if (TREE_CODE (field2) == FIELD_DECL)
1925 : 162 : vec.safe_push (field2);
1926 : : /* Discussions on core lean towards treating multiple union fields
1927 : : of the same type as the same field, so this might need changing
1928 : : in the future. */
1929 : 138 : if (count != vec.length ())
1930 : : return false;
1931 : 297 : for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1932 : : {
1933 : 234 : if (TREE_CODE (field1) != FIELD_DECL)
1934 : 78 : continue;
1935 : 156 : unsigned int j;
1936 : 156 : tree t1 = DECL_BIT_FIELD_TYPE (field1);
1937 : 156 : if (t1 == NULL_TREE)
1938 : 153 : t1 = TREE_TYPE (field1);
1939 : 258 : FOR_EACH_VEC_ELT (vec, j, field2)
1940 : : {
1941 : 252 : tree t2 = DECL_BIT_FIELD_TYPE (field2);
1942 : 252 : if (t2 == NULL_TREE)
1943 : 240 : t2 = TREE_TYPE (field2);
1944 : 252 : if (DECL_BIT_FIELD_TYPE (field1))
1945 : : {
1946 : 6 : if (!DECL_BIT_FIELD_TYPE (field2))
1947 : 0 : continue;
1948 : 6 : if (TYPE_PRECISION (TREE_TYPE (field1))
1949 : 6 : != TYPE_PRECISION (TREE_TYPE (field2)))
1950 : 6 : continue;
1951 : : }
1952 : 246 : else if (DECL_BIT_FIELD_TYPE (field2))
1953 : 6 : continue;
1954 : 240 : if (!layout_compatible_type_p (t1, t2))
1955 : 90 : continue;
1956 : 150 : if ((!lookup_attribute ("no_unique_address",
1957 : 150 : DECL_ATTRIBUTES (field1)))
1958 : 150 : != !lookup_attribute ("no_unique_address",
1959 : 150 : DECL_ATTRIBUTES (field2)))
1960 : 0 : continue;
1961 : : break;
1962 : : }
1963 : 312 : if (j == vec.length ())
1964 : : return false;
1965 : 150 : vec.unordered_remove (j);
1966 : : }
1967 : : return true;
1968 : 69 : }
1969 : :
1970 : 837 : return same_type_p (type1, type2);
1971 : : }
1972 : :
1973 : : /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1974 : :
1975 : : bool
1976 : 161919122 : at_least_as_qualified_p (const_tree type1, const_tree type2)
1977 : : {
1978 : 161919122 : int q1 = cp_type_quals (type1);
1979 : 161919122 : int q2 = cp_type_quals (type2);
1980 : :
1981 : : /* All qualifiers for TYPE2 must also appear in TYPE1. */
1982 : 161919122 : return (q1 & q2) == q2;
1983 : : }
1984 : :
1985 : : /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1986 : : more cv-qualified that TYPE1, and 0 otherwise. */
1987 : :
1988 : : int
1989 : 10438137 : comp_cv_qualification (int q1, int q2)
1990 : : {
1991 : 10438137 : if (q1 == q2)
1992 : : return 0;
1993 : :
1994 : 3033620 : if ((q1 & q2) == q2)
1995 : : return 1;
1996 : 246054 : else if ((q1 & q2) == q1)
1997 : 245465 : return -1;
1998 : :
1999 : : return 0;
2000 : : }
2001 : :
2002 : : int
2003 : 0 : comp_cv_qualification (const_tree type1, const_tree type2)
2004 : : {
2005 : 0 : int q1 = cp_type_quals (type1);
2006 : 0 : int q2 = cp_type_quals (type2);
2007 : 0 : return comp_cv_qualification (q1, q2);
2008 : : }
2009 : :
2010 : : /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
2011 : : subset of the cv-qualification signature of TYPE2, and the types
2012 : : are similar. Returns -1 if the other way 'round, and 0 otherwise. */
2013 : :
2014 : : int
2015 : 381 : comp_cv_qual_signature (tree type1, tree type2)
2016 : : {
2017 : 381 : if (comp_ptr_ttypes_real (type2, type1, -1))
2018 : : return 1;
2019 : 295 : else if (comp_ptr_ttypes_real (type1, type2, -1))
2020 : : return -1;
2021 : : else
2022 : 283 : return 0;
2023 : : }
2024 : :
2025 : : /* Subroutines of `comptypes'. */
2026 : :
2027 : : /* Return true if two parameter type lists PARMS1 and PARMS2 are
2028 : : equivalent in the sense that functions with those parameter types
2029 : : can have equivalent types. The two lists must be equivalent,
2030 : : element by element. */
2031 : :
2032 : : bool
2033 : 853116708 : compparms (const_tree parms1, const_tree parms2)
2034 : : {
2035 : 853116708 : const_tree t1, t2;
2036 : :
2037 : : /* An unspecified parmlist matches any specified parmlist
2038 : : whose argument types don't need default promotions. */
2039 : :
2040 : 853116708 : for (t1 = parms1, t2 = parms2;
2041 : 1381848681 : t1 || t2;
2042 : 528731973 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2043 : : {
2044 : : /* If one parmlist is shorter than the other,
2045 : : they fail to match. */
2046 : 1299448557 : if (!t1 || !t2)
2047 : : return false;
2048 : 1298843142 : if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2049 : : return false;
2050 : : }
2051 : : return true;
2052 : : }
2053 : :
2054 : :
2055 : : /* Process a sizeof or alignof expression where the operand is a type.
2056 : : STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2057 : : or GNU (preferred alignment) semantics; it is ignored if OP is
2058 : : SIZEOF_EXPR. */
2059 : :
2060 : : tree
2061 : 22524787 : cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2062 : : bool std_alignof, bool complain)
2063 : : {
2064 : 22524787 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2065 : 22524787 : if (type == error_mark_node)
2066 : : return error_mark_node;
2067 : :
2068 : 22524785 : type = non_reference (type);
2069 : 22524785 : if (TREE_CODE (type) == METHOD_TYPE)
2070 : : {
2071 : 0 : if (complain)
2072 : : {
2073 : 0 : pedwarn (loc, OPT_Wpointer_arith,
2074 : : "invalid application of %qs to a member function",
2075 : 0 : OVL_OP_INFO (false, op)->name);
2076 : 0 : return size_one_node;
2077 : : }
2078 : : else
2079 : 0 : return error_mark_node;
2080 : : }
2081 : 22524785 : else if (VOID_TYPE_P (type) && std_alignof)
2082 : : {
2083 : 14 : if (complain)
2084 : 14 : error_at (loc, "invalid application of %qs to a void type",
2085 : 14 : OVL_OP_INFO (false, op)->name);
2086 : 14 : return error_mark_node;
2087 : : }
2088 : :
2089 : 22524771 : bool dependent_p = dependent_type_p (type);
2090 : 22524771 : if (!dependent_p)
2091 : : {
2092 : 18707978 : complete_type (type);
2093 : 18707978 : if (!COMPLETE_TYPE_P (type))
2094 : : /* Call this here because the incompleteness diagnostic comes from
2095 : : c_sizeof_or_alignof_type instead of
2096 : : complete_type_or_maybe_complain. */
2097 : 190 : note_failed_type_completion (type, complain);
2098 : : }
2099 : 18707978 : if (dependent_p
2100 : : /* VLA types will have a non-constant size. In the body of an
2101 : : uninstantiated template, we don't need to try to compute the
2102 : : value, because the sizeof expression is not an integral
2103 : : constant expression in that case. And, if we do try to
2104 : : compute the value, we'll likely end up with SAVE_EXPRs, which
2105 : : the template substitution machinery does not expect to see. */
2106 : 18707978 : || (processing_template_decl
2107 : 1730389 : && COMPLETE_TYPE_P (type)
2108 : 1730383 : && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2109 : : {
2110 : 3816793 : tree value = build_min (op, size_type_node, type);
2111 : 3816793 : TREE_READONLY (value) = 1;
2112 : 3816793 : if (op == ALIGNOF_EXPR && std_alignof)
2113 : 455191 : ALIGNOF_EXPR_STD_P (value) = true;
2114 : 3816793 : SET_EXPR_LOCATION (value, loc);
2115 : 3816793 : return value;
2116 : : }
2117 : :
2118 : 18707978 : return c_sizeof_or_alignof_type (loc, complete_type (type),
2119 : : op == SIZEOF_EXPR, std_alignof,
2120 : 18707978 : complain & (tf_warning_or_error));
2121 : : }
2122 : :
2123 : : /* Return the size of the type, without producing any warnings for
2124 : : types whose size cannot be taken. This routine should be used only
2125 : : in some other routine that has already produced a diagnostic about
2126 : : using the size of such a type. */
2127 : : tree
2128 : 2268667 : cxx_sizeof_nowarn (tree type)
2129 : : {
2130 : 2268667 : if (TREE_CODE (type) == FUNCTION_TYPE
2131 : 2268667 : || VOID_TYPE_P (type)
2132 : 2268585 : || TREE_CODE (type) == ERROR_MARK)
2133 : 82 : return size_one_node;
2134 : 2268585 : else if (!COMPLETE_TYPE_P (type))
2135 : 19 : return size_zero_node;
2136 : : else
2137 : 2268566 : return cxx_sizeof_or_alignof_type (input_location, type,
2138 : 2268566 : SIZEOF_EXPR, false, false);
2139 : : }
2140 : :
2141 : : /* Process a sizeof expression where the operand is an expression. */
2142 : :
2143 : : static tree
2144 : 1398536 : cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2145 : : {
2146 : 1398536 : if (e == error_mark_node)
2147 : : return error_mark_node;
2148 : :
2149 : 1397797 : if (instantiation_dependent_uneval_expression_p (e))
2150 : : {
2151 : 319985 : e = build_min (SIZEOF_EXPR, size_type_node, e);
2152 : 319985 : TREE_SIDE_EFFECTS (e) = 0;
2153 : 319985 : TREE_READONLY (e) = 1;
2154 : 319985 : SET_EXPR_LOCATION (e, loc);
2155 : :
2156 : 319985 : return e;
2157 : : }
2158 : :
2159 : 1077812 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2160 : 1077812 : STRIP_ANY_LOCATION_WRAPPER (e);
2161 : :
2162 : 1077812 : if (TREE_CODE (e) == PARM_DECL
2163 : 46114 : && DECL_ARRAY_PARAMETER_P (e)
2164 : 1078189 : && (complain & tf_warning))
2165 : : {
2166 : 120 : auto_diagnostic_group d;
2167 : 120 : if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2168 : : "%<sizeof%> on array function parameter %qE "
2169 : 120 : "will return size of %qT", e, TREE_TYPE (e)))
2170 : 24 : inform (DECL_SOURCE_LOCATION (e), "declared here");
2171 : 120 : }
2172 : :
2173 : 1077812 : e = mark_type_use (e);
2174 : :
2175 : 1077812 : if (bitfield_p (e))
2176 : : {
2177 : 12 : if (complain & tf_error)
2178 : 6 : error_at (e_loc,
2179 : : "invalid application of %<sizeof%> to a bit-field");
2180 : : else
2181 : 6 : return error_mark_node;
2182 : 6 : e = char_type_node;
2183 : : }
2184 : 1077800 : else if (is_overloaded_fn (e))
2185 : : {
2186 : 33 : if (complain & tf_error)
2187 : 12 : permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2188 : : "an expression of function type");
2189 : : else
2190 : 21 : return error_mark_node;
2191 : 12 : e = char_type_node;
2192 : : }
2193 : 1077767 : else if (type_unknown_p (e))
2194 : : {
2195 : 0 : if (complain & tf_error)
2196 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2197 : : else
2198 : 0 : return error_mark_node;
2199 : 0 : e = char_type_node;
2200 : : }
2201 : : else
2202 : 1077767 : e = TREE_TYPE (e);
2203 : :
2204 : 1077785 : return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2205 : 1077785 : complain & tf_error);
2206 : : }
2207 : :
2208 : : /* Implement the __alignof keyword: Return the minimum required
2209 : : alignment of E, measured in bytes. For VAR_DECL's and
2210 : : FIELD_DECL's return DECL_ALIGN (which can be set from an
2211 : : "aligned" __attribute__ specification). STD_ALIGNOF acts
2212 : : like in cxx_sizeof_or_alignof_type. */
2213 : :
2214 : : static tree
2215 : 101591 : cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2216 : : tsubst_flags_t complain)
2217 : : {
2218 : 101591 : tree t;
2219 : :
2220 : 101591 : if (e == error_mark_node)
2221 : : return error_mark_node;
2222 : :
2223 : 101583 : if (processing_template_decl)
2224 : : {
2225 : 19880 : e = build_min (ALIGNOF_EXPR, size_type_node, e);
2226 : 19880 : TREE_SIDE_EFFECTS (e) = 0;
2227 : 19880 : TREE_READONLY (e) = 1;
2228 : 19880 : SET_EXPR_LOCATION (e, loc);
2229 : 19880 : ALIGNOF_EXPR_STD_P (e) = std_alignof;
2230 : :
2231 : 19880 : return e;
2232 : : }
2233 : :
2234 : 81703 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2235 : 81703 : STRIP_ANY_LOCATION_WRAPPER (e);
2236 : :
2237 : 81703 : e = mark_type_use (e);
2238 : :
2239 : 81703 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2240 : : !(complain & tf_error)))
2241 : : {
2242 : 0 : if (!(complain & tf_error))
2243 : 0 : return error_mark_node;
2244 : 0 : t = size_one_node;
2245 : : }
2246 : 81703 : else if (VAR_P (e))
2247 : 12270 : t = size_int (DECL_ALIGN_UNIT (e));
2248 : 69433 : else if (bitfield_p (e))
2249 : : {
2250 : 6 : if (complain & tf_error)
2251 : 6 : error_at (e_loc,
2252 : : "invalid application of %<__alignof%> to a bit-field");
2253 : : else
2254 : 0 : return error_mark_node;
2255 : 6 : t = size_one_node;
2256 : : }
2257 : 69427 : else if (TREE_CODE (e) == COMPONENT_REF
2258 : 69427 : && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2259 : 37792 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2260 : 31635 : else if (is_overloaded_fn (e))
2261 : : {
2262 : 6 : if (complain & tf_error)
2263 : 6 : permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2264 : : "an expression of function type");
2265 : : else
2266 : 0 : return error_mark_node;
2267 : 6 : if (TREE_CODE (e) == FUNCTION_DECL)
2268 : 3 : t = size_int (DECL_ALIGN_UNIT (e));
2269 : : else
2270 : 3 : t = size_one_node;
2271 : : }
2272 : 31629 : else if (type_unknown_p (e))
2273 : : {
2274 : 0 : if (complain & tf_error)
2275 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2276 : : else
2277 : 0 : return error_mark_node;
2278 : 0 : t = size_one_node;
2279 : : }
2280 : : else
2281 : 31629 : return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2282 : : ALIGNOF_EXPR, std_alignof,
2283 : 31629 : complain & tf_error);
2284 : :
2285 : 50074 : return fold_convert_loc (loc, size_type_node, t);
2286 : : }
2287 : :
2288 : : /* Process a sizeof or alignof expression E with code OP where the operand
2289 : : is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2290 : :
2291 : : tree
2292 : 1500127 : cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2293 : : bool std_alignof, bool complain)
2294 : : {
2295 : 1500127 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2296 : 1500127 : if (op == SIZEOF_EXPR)
2297 : 2113964 : return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2298 : : else
2299 : 101618 : return cxx_alignof_expr (loc, e, std_alignof,
2300 : 101591 : complain? tf_warning_or_error : tf_none);
2301 : : }
2302 : :
2303 : : /* Build a representation of an expression 'alignas(E).' Return the
2304 : : folded integer value of E if it is an integral constant expression
2305 : : that resolves to a valid alignment. If E depends on a template
2306 : : parameter, return a syntactic representation tree of kind
2307 : : ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2308 : : expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2309 : :
2310 : : tree
2311 : 198933 : cxx_alignas_expr (tree e)
2312 : : {
2313 : 198933 : if (e == NULL_TREE || e == error_mark_node
2314 : 397866 : || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2315 : 0 : return e;
2316 : :
2317 : 198933 : if (TYPE_P (e))
2318 : : /* [dcl.align]/3:
2319 : :
2320 : : When the alignment-specifier is of the form
2321 : : alignas(type-id), it shall have the same effect as
2322 : : alignas(alignof(type-id)). */
2323 : :
2324 : 122128 : return cxx_sizeof_or_alignof_type (input_location,
2325 : : e, ALIGNOF_EXPR,
2326 : : /*std_alignof=*/true,
2327 : 122128 : /*complain=*/true);
2328 : :
2329 : : /* If we reach this point, it means the alignas expression if of
2330 : : the form "alignas(assignment-expression)", so we should follow
2331 : : what is stated by [dcl.align]/2. */
2332 : :
2333 : 76805 : if (value_dependent_expression_p (e))
2334 : : /* Leave value-dependent expression alone for now. */
2335 : : return e;
2336 : :
2337 : 19652 : e = instantiate_non_dependent_expr (e);
2338 : 19652 : e = mark_rvalue_use (e);
2339 : :
2340 : : /* [dcl.align]/2 says:
2341 : :
2342 : : the assignment-expression shall be an integral constant
2343 : : expression. */
2344 : :
2345 : 19652 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2346 : : {
2347 : 18 : error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2348 : 18 : return error_mark_node;
2349 : : }
2350 : :
2351 : 19634 : return cxx_constant_value (e);
2352 : : }
2353 : :
2354 : :
2355 : : /* EXPR is being used in a context that is not a function call.
2356 : : Enforce:
2357 : :
2358 : : [expr.ref]
2359 : :
2360 : : The expression can be used only as the left-hand operand of a
2361 : : member function call.
2362 : :
2363 : : [expr.mptr.operator]
2364 : :
2365 : : If the result of .* or ->* is a function, then that result can be
2366 : : used only as the operand for the function call operator ().
2367 : :
2368 : : by issuing an error message if appropriate. Returns true iff EXPR
2369 : : violates these rules. */
2370 : :
2371 : : bool
2372 : 1214832004 : invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2373 : : {
2374 : 1214832004 : if (expr == NULL_TREE)
2375 : : return false;
2376 : : /* Don't enforce this in MS mode. */
2377 : 1214830561 : if (flag_ms_extensions)
2378 : : return false;
2379 : 1214822500 : if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2380 : 100263999 : expr = get_first_fn (expr);
2381 : 1214822500 : if (TREE_TYPE (expr)
2382 : 1214822500 : && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2383 : : {
2384 : 91 : if (complain & tf_error)
2385 : : {
2386 : 87 : if (DECL_P (expr))
2387 : : {
2388 : 69 : auto_diagnostic_group d;
2389 : 69 : error_at (loc, "invalid use of non-static member function %qD",
2390 : : expr);
2391 : 69 : inform (DECL_SOURCE_LOCATION (expr), "declared here");
2392 : 69 : }
2393 : : else
2394 : 18 : error_at (loc, "invalid use of non-static member function of "
2395 : 18 : "type %qT", TREE_TYPE (expr));
2396 : : }
2397 : 91 : return true;
2398 : : }
2399 : : return false;
2400 : : }
2401 : :
2402 : : /* If EXP is a reference to a bit-field, and the type of EXP does not
2403 : : match the declared type of the bit-field, return the declared type
2404 : : of the bit-field. Otherwise, return NULL_TREE. */
2405 : :
2406 : : tree
2407 : 2824218363 : is_bitfield_expr_with_lowered_type (const_tree exp)
2408 : : {
2409 : 3897100508 : switch (TREE_CODE (exp))
2410 : : {
2411 : 5690985 : case COND_EXPR:
2412 : 11381970 : if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2413 : 5690959 : ? TREE_OPERAND (exp, 1)
2414 : 26 : : TREE_OPERAND (exp, 0)))
2415 : : return NULL_TREE;
2416 : 135 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2417 : :
2418 : 896804 : case COMPOUND_EXPR:
2419 : 896804 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2420 : :
2421 : 350310051 : case MODIFY_EXPR:
2422 : 350310051 : case SAVE_EXPR:
2423 : 350310051 : case UNARY_PLUS_EXPR:
2424 : 350310051 : case PREDECREMENT_EXPR:
2425 : 350310051 : case PREINCREMENT_EXPR:
2426 : 350310051 : case POSTDECREMENT_EXPR:
2427 : 350310051 : case POSTINCREMENT_EXPR:
2428 : 350310051 : case NEGATE_EXPR:
2429 : 350310051 : case NON_LVALUE_EXPR:
2430 : 350310051 : case BIT_NOT_EXPR:
2431 : 350310051 : case CLEANUP_POINT_EXPR:
2432 : 350310051 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2433 : :
2434 : 239787148 : case COMPONENT_REF:
2435 : 239787148 : {
2436 : 239787148 : tree field;
2437 : :
2438 : 239787148 : field = TREE_OPERAND (exp, 1);
2439 : 239787148 : if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2440 : : return NULL_TREE;
2441 : 40570498 : if (same_type_ignoring_top_level_qualifiers_p
2442 : 40570498 : (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2443 : : return NULL_TREE;
2444 : 40419589 : return DECL_BIT_FIELD_TYPE (field);
2445 : : }
2446 : :
2447 : 532424098 : case VAR_DECL:
2448 : 532424098 : if (DECL_HAS_VALUE_EXPR_P (exp))
2449 : 1970351 : return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2450 : 1970351 : (CONST_CAST_TREE (exp)));
2451 : : return NULL_TREE;
2452 : :
2453 : 726779831 : case VIEW_CONVERT_EXPR:
2454 : 726779831 : if (location_wrapper_p (exp))
2455 : 719704804 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2456 : : else
2457 : : return NULL_TREE;
2458 : :
2459 : : default:
2460 : : return NULL_TREE;
2461 : : }
2462 : : }
2463 : :
2464 : : /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2465 : : bitfield with a lowered type, the type of EXP is returned, rather
2466 : : than NULL_TREE. */
2467 : :
2468 : : tree
2469 : 1318908823 : unlowered_expr_type (const_tree exp)
2470 : : {
2471 : 1318908823 : tree type;
2472 : 1318908823 : tree etype = TREE_TYPE (exp);
2473 : :
2474 : 1318908823 : type = is_bitfield_expr_with_lowered_type (exp);
2475 : 1318908823 : if (type)
2476 : 34204634 : type = cp_build_qualified_type (type, cp_type_quals (etype));
2477 : : else
2478 : : type = etype;
2479 : :
2480 : 1318908823 : return type;
2481 : : }
2482 : :
2483 : : /* Perform the conversions in [expr] that apply when an lvalue appears
2484 : : in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2485 : : function-to-pointer conversions. In addition, bitfield references are
2486 : : converted to their declared types. Note that this function does not perform
2487 : : the lvalue-to-rvalue conversion for class types. If you need that conversion
2488 : : for class types, then you probably need to use force_rvalue.
2489 : :
2490 : : Although the returned value is being used as an rvalue, this
2491 : : function does not wrap the returned expression in a
2492 : : NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2493 : : that the return value is no longer an lvalue. */
2494 : :
2495 : : tree
2496 : 972143226 : decay_conversion (tree exp,
2497 : : tsubst_flags_t complain,
2498 : : bool reject_builtin /* = true */)
2499 : : {
2500 : 972143226 : tree type;
2501 : 972143226 : enum tree_code code;
2502 : 972143226 : location_t loc = cp_expr_loc_or_input_loc (exp);
2503 : :
2504 : 972143226 : type = TREE_TYPE (exp);
2505 : 972143226 : if (type == error_mark_node)
2506 : : return error_mark_node;
2507 : :
2508 : 972143016 : exp = resolve_nondeduced_context_or_error (exp, complain);
2509 : :
2510 : 972143016 : code = TREE_CODE (type);
2511 : :
2512 : 972143016 : if (error_operand_p (exp))
2513 : 90 : return error_mark_node;
2514 : :
2515 : 972142926 : if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2516 : : {
2517 : 1485282 : mark_rvalue_use (exp, loc, reject_builtin);
2518 : 1485282 : return nullptr_node;
2519 : : }
2520 : :
2521 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2522 : : Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2523 : 970657644 : if (code == VOID_TYPE)
2524 : : {
2525 : 5787 : if (complain & tf_error)
2526 : 3 : error_at (loc, "void value not ignored as it ought to be");
2527 : 5787 : return error_mark_node;
2528 : : }
2529 : 970651857 : if (invalid_nonstatic_memfn_p (loc, exp, complain))
2530 : 6 : return error_mark_node;
2531 : 970651851 : if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2532 : : {
2533 : 102229402 : exp = mark_lvalue_use (exp);
2534 : 102229402 : if (reject_builtin && reject_gcc_builtin (exp, loc))
2535 : 110 : return error_mark_node;
2536 : 102229292 : return cp_build_addr_expr (exp, complain);
2537 : : }
2538 : 868422449 : if (code == ARRAY_TYPE)
2539 : : {
2540 : 5412849 : tree adr;
2541 : 5412849 : tree ptrtype;
2542 : :
2543 : 5412849 : exp = mark_lvalue_use (exp);
2544 : :
2545 : 5412849 : if (INDIRECT_REF_P (exp))
2546 : 99595 : return build_nop (build_pointer_type (TREE_TYPE (type)),
2547 : 199190 : TREE_OPERAND (exp, 0));
2548 : :
2549 : 5313254 : if (TREE_CODE (exp) == COMPOUND_EXPR)
2550 : : {
2551 : 3 : tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2552 : 3 : if (op1 == error_mark_node)
2553 : : return error_mark_node;
2554 : 3 : return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2555 : 6 : TREE_OPERAND (exp, 0), op1);
2556 : : }
2557 : :
2558 : 5313251 : if (!obvalue_p (exp)
2559 : 5313251 : && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2560 : : {
2561 : 0 : if (complain & tf_error)
2562 : 0 : error_at (loc, "invalid use of non-lvalue array");
2563 : 0 : return error_mark_node;
2564 : : }
2565 : :
2566 : 5313251 : ptrtype = build_pointer_type (TREE_TYPE (type));
2567 : :
2568 : 5313251 : if (VAR_P (exp))
2569 : : {
2570 : 527150 : if (!cxx_mark_addressable (exp))
2571 : 0 : return error_mark_node;
2572 : 527150 : adr = build_nop (ptrtype, build_address (exp));
2573 : 527150 : return adr;
2574 : : }
2575 : : /* This way is better for a COMPONENT_REF since it can
2576 : : simplify the offset for a component. */
2577 : 4786101 : adr = cp_build_addr_expr (exp, complain);
2578 : 4786101 : return cp_convert (ptrtype, adr, complain);
2579 : : }
2580 : :
2581 : : /* Otherwise, it's the lvalue-to-rvalue conversion. */
2582 : 863009600 : exp = mark_rvalue_use (exp, loc, reject_builtin);
2583 : :
2584 : : /* If a bitfield is used in a context where integral promotion
2585 : : applies, then the caller is expected to have used
2586 : : default_conversion. That function promotes bitfields correctly
2587 : : before calling this function. At this point, if we have a
2588 : : bitfield referenced, we may assume that is not subject to
2589 : : promotion, and that, therefore, the type of the resulting rvalue
2590 : : is the declared type of the bitfield. */
2591 : 863009600 : exp = convert_bitfield_to_declared_type (exp);
2592 : :
2593 : : /* We do not call rvalue() here because we do not want to wrap EXP
2594 : : in a NON_LVALUE_EXPR. */
2595 : :
2596 : : /* [basic.lval]
2597 : :
2598 : : Non-class rvalues always have cv-unqualified types. */
2599 : 863009600 : type = TREE_TYPE (exp);
2600 : 863009600 : if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2601 : 264693526 : exp = build_nop (cv_unqualified (type), exp);
2602 : :
2603 : 863009600 : if (!complete_type_or_maybe_complain (type, exp, complain))
2604 : 42 : return error_mark_node;
2605 : :
2606 : : return exp;
2607 : : }
2608 : :
2609 : : /* Perform preparatory conversions, as part of the "usual arithmetic
2610 : : conversions". In particular, as per [expr]:
2611 : :
2612 : : Whenever an lvalue expression appears as an operand of an
2613 : : operator that expects the rvalue for that operand, the
2614 : : lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2615 : : standard conversions are applied to convert the expression to an
2616 : : rvalue.
2617 : :
2618 : : In addition, we perform integral promotions here, as those are
2619 : : applied to both operands to a binary operator before determining
2620 : : what additional conversions should apply. */
2621 : :
2622 : : static tree
2623 : 267962349 : cp_default_conversion (tree exp, tsubst_flags_t complain)
2624 : : {
2625 : : /* Check for target-specific promotions. */
2626 : 267962349 : tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2627 : 267962349 : if (promoted_type)
2628 : 0 : exp = cp_convert (promoted_type, exp, complain);
2629 : : /* Perform the integral promotions first so that bitfield
2630 : : expressions (which may promote to "int", even if the bitfield is
2631 : : declared "unsigned") are promoted correctly. */
2632 : 267962349 : else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2633 : 177966580 : exp = cp_perform_integral_promotions (exp, complain);
2634 : : /* Perform the other conversions. */
2635 : 267962349 : exp = decay_conversion (exp, complain);
2636 : :
2637 : 267962349 : return exp;
2638 : : }
2639 : :
2640 : : /* C version. */
2641 : :
2642 : : tree
2643 : 40180558 : default_conversion (tree exp)
2644 : : {
2645 : 40180558 : return cp_default_conversion (exp, tf_warning_or_error);
2646 : : }
2647 : :
2648 : : /* EXPR is an expression with an integral or enumeration type.
2649 : : Perform the integral promotions in [conv.prom], and return the
2650 : : converted value. */
2651 : :
2652 : : tree
2653 : 184177803 : cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2654 : : {
2655 : 184177803 : tree type;
2656 : 184177803 : tree promoted_type;
2657 : :
2658 : 184177803 : expr = mark_rvalue_use (expr);
2659 : 184177803 : if (error_operand_p (expr))
2660 : 6 : return error_mark_node;
2661 : :
2662 : 184177797 : type = TREE_TYPE (expr);
2663 : :
2664 : : /* [conv.prom]
2665 : :
2666 : : A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2667 : : of type int if int can represent all the values of the bit-field;
2668 : : otherwise, it can be converted to unsigned int if unsigned int can
2669 : : represent all the values of the bit-field. If the bit-field is larger yet,
2670 : : no integral promotion applies to it. If the bit-field has an enumerated
2671 : : type, it is treated as any other value of that type for promotion
2672 : : purposes. */
2673 : 184177797 : tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2674 : 184177797 : if (bitfield_type
2675 : 184177797 : && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2676 : 323122 : || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2677 : : type = bitfield_type;
2678 : :
2679 : 184177797 : gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2680 : : /* Scoped enums don't promote. */
2681 : 184177797 : if (SCOPED_ENUM_P (type))
2682 : : return expr;
2683 : 183069358 : promoted_type = type_promotes_to (type);
2684 : 183069358 : if (type != promoted_type)
2685 : 50524165 : expr = cp_convert (promoted_type, expr, complain);
2686 : 132545193 : else if (bitfield_type && bitfield_type != type)
2687 : : /* Prevent decay_conversion from converting to bitfield_type. */
2688 : 159 : expr = build_nop (type, expr);
2689 : : return expr;
2690 : : }
2691 : :
2692 : : /* C version. */
2693 : :
2694 : : tree
2695 : 2048989 : perform_integral_promotions (tree expr)
2696 : : {
2697 : 2048989 : return cp_perform_integral_promotions (expr, tf_warning_or_error);
2698 : : }
2699 : :
2700 : : /* Returns nonzero iff exp is a STRING_CST or the result of applying
2701 : : decay_conversion to one. */
2702 : :
2703 : : int
2704 : 3185623 : string_conv_p (const_tree totype, const_tree exp, int warn)
2705 : : {
2706 : 3185623 : tree t;
2707 : :
2708 : 3185623 : if (!TYPE_PTR_P (totype))
2709 : : return 0;
2710 : :
2711 : 3185572 : t = TREE_TYPE (totype);
2712 : 3185572 : if (!same_type_p (t, char_type_node)
2713 : 3052592 : && !same_type_p (t, char8_type_node)
2714 : 3035507 : && !same_type_p (t, char16_type_node)
2715 : 2986550 : && !same_type_p (t, char32_type_node)
2716 : 6123166 : && !same_type_p (t, wchar_type_node))
2717 : : return 0;
2718 : :
2719 : 277755 : location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2720 : :
2721 : 277755 : STRIP_ANY_LOCATION_WRAPPER (exp);
2722 : :
2723 : 277755 : if (TREE_CODE (exp) == STRING_CST)
2724 : : {
2725 : : /* Make sure that we don't try to convert between char and wide chars. */
2726 : 108 : if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2727 : : return 0;
2728 : : }
2729 : : else
2730 : : {
2731 : : /* Is this a string constant which has decayed to 'const char *'? */
2732 : 277647 : t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2733 : 277647 : if (!same_type_p (TREE_TYPE (exp), t))
2734 : : return 0;
2735 : 13534 : STRIP_NOPS (exp);
2736 : 13534 : if (TREE_CODE (exp) != ADDR_EXPR
2737 : 13534 : || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2738 : : return 0;
2739 : : }
2740 : 306 : if (warn)
2741 : : {
2742 : 102 : if (cxx_dialect >= cxx11)
2743 : 87 : pedwarn (loc, OPT_Wwrite_strings,
2744 : : "ISO C++ forbids converting a string constant to %qT",
2745 : : totype);
2746 : : else
2747 : 15 : warning_at (loc, OPT_Wwrite_strings,
2748 : : "deprecated conversion from string constant to %qT",
2749 : : totype);
2750 : : }
2751 : :
2752 : : return 1;
2753 : : }
2754 : :
2755 : : /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2756 : : can, for example, use as an lvalue. This code used to be in
2757 : : unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2758 : : expressions, where we're dealing with aggregates. But now it's again only
2759 : : called from unary_complex_lvalue. The case (in particular) that led to
2760 : : this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2761 : : get it there. */
2762 : :
2763 : : static tree
2764 : 102090 : rationalize_conditional_expr (enum tree_code code, tree t,
2765 : : tsubst_flags_t complain)
2766 : : {
2767 : 102090 : location_t loc = cp_expr_loc_or_input_loc (t);
2768 : :
2769 : : /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2770 : : the first operand is always the one to be used if both operands
2771 : : are equal, so we know what conditional expression this used to be. */
2772 : 102090 : if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2773 : : {
2774 : 0 : tree op0 = TREE_OPERAND (t, 0);
2775 : 0 : tree op1 = TREE_OPERAND (t, 1);
2776 : :
2777 : : /* The following code is incorrect if either operand side-effects. */
2778 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (op0)
2779 : : && !TREE_SIDE_EFFECTS (op1));
2780 : 0 : return
2781 : 0 : build_conditional_expr (loc,
2782 : 0 : build_x_binary_op (loc,
2783 : 0 : (TREE_CODE (t) == MIN_EXPR
2784 : : ? LE_EXPR : GE_EXPR),
2785 : 0 : op0, TREE_CODE (op0),
2786 : 0 : op1, TREE_CODE (op1),
2787 : : NULL_TREE,
2788 : : /*overload=*/NULL,
2789 : : complain),
2790 : : cp_build_unary_op (code, op0, false, complain),
2791 : : cp_build_unary_op (code, op1, false, complain),
2792 : : complain);
2793 : : }
2794 : :
2795 : 102090 : tree op1 = TREE_OPERAND (t, 1);
2796 : 102090 : if (TREE_CODE (op1) != THROW_EXPR)
2797 : 102087 : op1 = cp_build_unary_op (code, op1, false, complain);
2798 : 102090 : tree op2 = TREE_OPERAND (t, 2);
2799 : 102090 : if (TREE_CODE (op2) != THROW_EXPR)
2800 : 102084 : op2 = cp_build_unary_op (code, op2, false, complain);
2801 : :
2802 : 102090 : return
2803 : 102090 : build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2804 : : }
2805 : :
2806 : : /* Given the TYPE of an anonymous union field inside T, return the
2807 : : FIELD_DECL for the field. If not found return NULL_TREE. Because
2808 : : anonymous unions can nest, we must also search all anonymous unions
2809 : : that are directly reachable. */
2810 : :
2811 : : tree
2812 : 1129759 : lookup_anon_field (tree, tree type)
2813 : : {
2814 : 1129759 : tree field;
2815 : :
2816 : 1129759 : type = TYPE_MAIN_VARIANT (type);
2817 : 1129759 : field = ANON_AGGR_TYPE_FIELD (type);
2818 : 1129759 : gcc_assert (field);
2819 : 1129759 : return field;
2820 : : }
2821 : :
2822 : : /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2823 : : expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2824 : : non-NULL, it indicates the path to the base used to name MEMBER.
2825 : : If PRESERVE_REFERENCE is true, the expression returned will have
2826 : : REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2827 : : returned will have the type referred to by the reference.
2828 : :
2829 : : This function does not perform access control; that is either done
2830 : : earlier by the parser when the name of MEMBER is resolved to MEMBER
2831 : : itself, or later when overload resolution selects one of the
2832 : : functions indicated by MEMBER. */
2833 : :
2834 : : tree
2835 : 105592458 : build_class_member_access_expr (cp_expr object, tree member,
2836 : : tree access_path, bool preserve_reference,
2837 : : tsubst_flags_t complain)
2838 : : {
2839 : 105592458 : tree object_type;
2840 : 105592458 : tree member_scope;
2841 : 105592458 : tree result = NULL_TREE;
2842 : 105592458 : tree using_decl = NULL_TREE;
2843 : :
2844 : 105592458 : if (error_operand_p (object) || error_operand_p (member))
2845 : 57 : return error_mark_node;
2846 : :
2847 : 105592401 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2848 : :
2849 : : /* [expr.ref]
2850 : :
2851 : : The type of the first expression shall be "class object" (of a
2852 : : complete type). */
2853 : 105592401 : object_type = TREE_TYPE (object);
2854 : 105592401 : if (!currently_open_class (object_type)
2855 : 105592401 : && !complete_type_or_maybe_complain (object_type, object, complain))
2856 : 0 : return error_mark_node;
2857 : 105592401 : if (!CLASS_TYPE_P (object_type))
2858 : : {
2859 : 0 : if (complain & tf_error)
2860 : : {
2861 : 0 : if (INDIRECT_TYPE_P (object_type)
2862 : 0 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
2863 : 0 : error ("request for member %qD in %qE, which is of pointer "
2864 : : "type %qT (maybe you meant to use %<->%> ?)",
2865 : : member, object.get_value (), object_type);
2866 : : else
2867 : 0 : error ("request for member %qD in %qE, which is of non-class "
2868 : : "type %qT", member, object.get_value (), object_type);
2869 : : }
2870 : 0 : return error_mark_node;
2871 : : }
2872 : :
2873 : : /* The standard does not seem to actually say that MEMBER must be a
2874 : : member of OBJECT_TYPE. However, that is clearly what is
2875 : : intended. */
2876 : 105592401 : if (DECL_P (member))
2877 : : {
2878 : 44981732 : member_scope = DECL_CLASS_CONTEXT (member);
2879 : 44981732 : if (!mark_used (member, complain) && !(complain & tf_error))
2880 : 0 : return error_mark_node;
2881 : :
2882 : 44981732 : if (TREE_UNAVAILABLE (member))
2883 : 30 : error_unavailable_use (member, NULL_TREE);
2884 : 44981702 : else if (TREE_DEPRECATED (member))
2885 : 30 : warn_deprecated_use (member, NULL_TREE);
2886 : : }
2887 : : else
2888 : 60610669 : member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2889 : : /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2890 : : presently be the anonymous union. Go outwards until we find a
2891 : : type related to OBJECT_TYPE. */
2892 : 106725998 : while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2893 : 107860006 : && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2894 : : object_type))
2895 : 1133597 : member_scope = TYPE_CONTEXT (member_scope);
2896 : 105592401 : if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2897 : : {
2898 : 3 : if (complain & tf_error)
2899 : : {
2900 : 3 : if (TREE_CODE (member) == FIELD_DECL)
2901 : 0 : error ("invalid use of non-static data member %qE", member);
2902 : : else
2903 : 3 : error ("%qD is not a member of %qT", member, object_type);
2904 : : }
2905 : 3 : return error_mark_node;
2906 : : }
2907 : :
2908 : : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2909 : : `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2910 : : in the front end; only _DECLs and _REFs are lvalues in the back end. */
2911 : 105592398 : if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2912 : : {
2913 : 50 : temp = cp_build_fold_indirect_ref (temp);
2914 : 50 : if (!lvalue_p (object) && lvalue_p (temp))
2915 : : /* Preserve rvalueness. */
2916 : 6 : temp = move (temp);
2917 : 50 : object = temp;
2918 : : }
2919 : :
2920 : : /* In [expr.ref], there is an explicit list of the valid choices for
2921 : : MEMBER. We check for each of those cases here. */
2922 : 105592398 : if (VAR_P (member))
2923 : : {
2924 : : /* A static data member. */
2925 : 97880 : result = member;
2926 : 97880 : mark_exp_read (object);
2927 : :
2928 : 97880 : if (tree wrap = maybe_get_tls_wrapper_call (result))
2929 : : /* Replace an evaluated use of the thread_local variable with
2930 : : a call to its wrapper. */
2931 : 102 : result = wrap;
2932 : :
2933 : : /* If OBJECT has side-effects, they are supposed to occur. */
2934 : 97880 : if (TREE_SIDE_EFFECTS (object))
2935 : 57 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2936 : : }
2937 : 105494518 : else if (TREE_CODE (member) == FIELD_DECL)
2938 : : {
2939 : : /* A non-static data member. */
2940 : 44883653 : bool null_object_p;
2941 : 44883653 : int type_quals;
2942 : 44883653 : tree member_type;
2943 : :
2944 : 44883653 : if (INDIRECT_REF_P (object))
2945 : 34233616 : null_object_p =
2946 : 34233616 : integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2947 : : else
2948 : : null_object_p = false;
2949 : :
2950 : : /* Convert OBJECT to the type of MEMBER. */
2951 : 44883653 : if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2952 : : TYPE_MAIN_VARIANT (member_scope)))
2953 : : {
2954 : 1859247 : tree binfo;
2955 : 1859247 : base_kind kind;
2956 : :
2957 : : /* We didn't complain above about a currently open class, but now we
2958 : : must: we don't know how to refer to a base member before layout is
2959 : : complete. But still don't complain in a template. */
2960 : 1859247 : if (!cp_unevaluated_operand
2961 : 1858724 : && !dependent_type_p (object_type)
2962 : 3414294 : && !complete_type_or_maybe_complain (object_type, object,
2963 : : complain))
2964 : 9 : return error_mark_node;
2965 : :
2966 : 2318997 : binfo = lookup_base (access_path ? access_path : object_type,
2967 : : member_scope, ba_unique, &kind, complain);
2968 : 1859244 : if (binfo == error_mark_node)
2969 : : return error_mark_node;
2970 : :
2971 : : /* It is invalid to try to get to a virtual base of a
2972 : : NULL object. The most common cause is invalid use of
2973 : : offsetof macro. */
2974 : 1859244 : if (null_object_p && kind == bk_via_virtual)
2975 : : {
2976 : 6 : if (complain & tf_error)
2977 : : {
2978 : 6 : error ("invalid access to non-static data member %qD in "
2979 : : "virtual base of NULL object", member);
2980 : : }
2981 : 6 : return error_mark_node;
2982 : : }
2983 : :
2984 : : /* Convert to the base. */
2985 : 1859238 : object = build_base_path (PLUS_EXPR, object, binfo,
2986 : : /*nonnull=*/1, complain);
2987 : : /* If we found the base successfully then we should be able
2988 : : to convert to it successfully. */
2989 : 1859238 : gcc_assert (object != error_mark_node || seen_error ());
2990 : : }
2991 : :
2992 : : /* If MEMBER is from an anonymous aggregate, we have converted
2993 : : OBJECT so that it refers to the class containing the
2994 : : anonymous union. Generate a reference to the anonymous union
2995 : : itself, and recur to find MEMBER. */
2996 : 89767288 : if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2997 : : /* When this code is called from build_field_call, the
2998 : : object already has the type of the anonymous union.
2999 : : That is because the COMPONENT_REF was already
3000 : : constructed, and was then disassembled before calling
3001 : : build_field_call. After the function-call code is
3002 : : cleaned up, this waste can be eliminated. */
3003 : 46013682 : && (!same_type_ignoring_top_level_qualifiers_p
3004 : 1130038 : (TREE_TYPE (object), DECL_CONTEXT (member))))
3005 : : {
3006 : 1129633 : tree anonymous_union;
3007 : :
3008 : 1129633 : anonymous_union = lookup_anon_field (TREE_TYPE (object),
3009 : 1129633 : DECL_CONTEXT (member));
3010 : 1129633 : object = build_class_member_access_expr (object,
3011 : : anonymous_union,
3012 : : /*access_path=*/NULL_TREE,
3013 : : preserve_reference,
3014 : : complain);
3015 : : }
3016 : :
3017 : : /* Compute the type of the field, as described in [expr.ref]. */
3018 : 44883644 : type_quals = TYPE_UNQUALIFIED;
3019 : 44883644 : member_type = TREE_TYPE (member);
3020 : 44883644 : if (!TYPE_REF_P (member_type))
3021 : : {
3022 : 43611966 : type_quals = (cp_type_quals (member_type)
3023 : 43611966 : | cp_type_quals (object_type));
3024 : :
3025 : : /* A field is const (volatile) if the enclosing object, or the
3026 : : field itself, is const (volatile). But, a mutable field is
3027 : : not const, even within a const object. */
3028 : 43611966 : if (DECL_MUTABLE_P (member))
3029 : 329876 : type_quals &= ~TYPE_QUAL_CONST;
3030 : 43611966 : member_type = cp_build_qualified_type (member_type, type_quals);
3031 : : }
3032 : :
3033 : 44883644 : result = build3_loc (input_location, COMPONENT_REF, member_type,
3034 : : object, member, NULL_TREE);
3035 : :
3036 : : /* Mark the expression const or volatile, as appropriate. Even
3037 : : though we've dealt with the type above, we still have to mark the
3038 : : expression itself. */
3039 : 44883644 : if (type_quals & TYPE_QUAL_CONST)
3040 : 14197560 : TREE_READONLY (result) = 1;
3041 : 44883644 : if (type_quals & TYPE_QUAL_VOLATILE)
3042 : 185831 : TREE_THIS_VOLATILE (result) = 1;
3043 : : }
3044 : 60610865 : else if (BASELINK_P (member))
3045 : : {
3046 : : /* The member is a (possibly overloaded) member function. */
3047 : 60610666 : tree functions;
3048 : 60610666 : tree type;
3049 : :
3050 : : /* If the MEMBER is exactly one static member function, then we
3051 : : know the type of the expression. Otherwise, we must wait
3052 : : until overload resolution has been performed. */
3053 : 60610666 : functions = BASELINK_FUNCTIONS (member);
3054 : 60610666 : if (TREE_CODE (functions) == OVERLOAD && OVL_SINGLE_P (functions))
3055 : 60610666 : functions = OVL_FIRST (functions);
3056 : 60610666 : if (TREE_CODE (functions) == FUNCTION_DECL
3057 : 60610666 : && DECL_STATIC_FUNCTION_P (functions))
3058 : 467939 : type = TREE_TYPE (functions);
3059 : : else
3060 : 60142727 : type = unknown_type_node;
3061 : : /* Note that we do not convert OBJECT to the BASELINK_BINFO
3062 : : base. That will happen when the function is called. */
3063 : 60610666 : result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3064 : : NULL_TREE);
3065 : : }
3066 : 199 : else if (TREE_CODE (member) == CONST_DECL)
3067 : : {
3068 : : /* The member is an enumerator. */
3069 : 187 : result = member;
3070 : : /* If OBJECT has side-effects, they are supposed to occur. */
3071 : 187 : if (TREE_SIDE_EFFECTS (object))
3072 : 6 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3073 : : object, result);
3074 : : }
3075 : 12 : else if ((using_decl = strip_using_decl (member)) != member)
3076 : 0 : result = build_class_member_access_expr (object,
3077 : : using_decl,
3078 : : access_path, preserve_reference,
3079 : : complain);
3080 : : else
3081 : : {
3082 : 12 : if (complain & tf_error)
3083 : 12 : error ("invalid use of %qD", member);
3084 : 12 : return error_mark_node;
3085 : : }
3086 : :
3087 : 105592377 : if (!preserve_reference)
3088 : : /* [expr.ref]
3089 : :
3090 : : If E2 is declared to have type "reference to T", then ... the
3091 : : type of E1.E2 is T. */
3092 : 99997270 : result = convert_from_reference (result);
3093 : :
3094 : : return result;
3095 : : }
3096 : :
3097 : : /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3098 : : SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3099 : :
3100 : : tree
3101 : 162229 : lookup_destructor (tree object, tree scope, tree dtor_name,
3102 : : tsubst_flags_t complain)
3103 : : {
3104 : 162229 : tree object_type = TREE_TYPE (object);
3105 : 162229 : tree dtor_type = TREE_OPERAND (dtor_name, 0);
3106 : 162229 : tree expr;
3107 : :
3108 : : /* We've already complained about this destructor. */
3109 : 162229 : if (dtor_type == error_mark_node)
3110 : : return error_mark_node;
3111 : :
3112 : 162223 : if (scope && !check_dtor_name (scope, dtor_type))
3113 : : {
3114 : 3 : if (complain & tf_error)
3115 : 3 : error ("qualified type %qT does not match destructor name ~%qT",
3116 : : scope, dtor_type);
3117 : 3 : return error_mark_node;
3118 : : }
3119 : 162220 : if (is_auto (dtor_type))
3120 : : dtor_type = object_type;
3121 : 162217 : else if (identifier_p (dtor_type))
3122 : : {
3123 : : /* In a template, names we can't find a match for are still accepted
3124 : : destructor names, and we check them here. */
3125 : 23 : if (check_dtor_name (object_type, dtor_type))
3126 : : dtor_type = object_type;
3127 : : else
3128 : : {
3129 : 3 : if (complain & tf_error)
3130 : 3 : error ("object type %qT does not match destructor name ~%qT",
3131 : : object_type, dtor_type);
3132 : 3 : return error_mark_node;
3133 : : }
3134 : :
3135 : : }
3136 : 162194 : else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3137 : : {
3138 : 6 : if (complain & tf_error)
3139 : 6 : error ("the type being destroyed is %qT, but the destructor "
3140 : 6 : "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3141 : 6 : return error_mark_node;
3142 : : }
3143 : 162211 : expr = lookup_member (dtor_type, complete_dtor_identifier,
3144 : : /*protect=*/1, /*want_type=*/false,
3145 : : tf_warning_or_error);
3146 : 162211 : if (!expr)
3147 : : {
3148 : 6 : if (complain & tf_error)
3149 : 6 : cxx_incomplete_type_error (dtor_name, dtor_type);
3150 : 6 : return error_mark_node;
3151 : : }
3152 : 162205 : expr = (adjust_result_of_qualified_name_lookup
3153 : 162205 : (expr, dtor_type, object_type));
3154 : 162205 : if (scope == NULL_TREE)
3155 : : /* We need to call adjust_result_of_qualified_name_lookup in case the
3156 : : destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3157 : : that we still get virtual function binding. */
3158 : 162062 : BASELINK_QUALIFIED_P (expr) = false;
3159 : : return expr;
3160 : : }
3161 : :
3162 : : /* An expression of the form "A::template B" has been resolved to
3163 : : DECL. Issue a diagnostic if B is not a template or template
3164 : : specialization. */
3165 : :
3166 : : void
3167 : 3107537 : check_template_keyword (tree decl)
3168 : : {
3169 : : /* The standard says:
3170 : :
3171 : : [temp.names]
3172 : :
3173 : : If a name prefixed by the keyword template is not a member
3174 : : template, the program is ill-formed.
3175 : :
3176 : : DR 228 removed the restriction that the template be a member
3177 : : template.
3178 : :
3179 : : DR 96, if accepted would add the further restriction that explicit
3180 : : template arguments must be provided if the template keyword is
3181 : : used, but, as of 2005-10-16, that DR is still in "drafting". If
3182 : : this DR is accepted, then the semantic checks here can be
3183 : : simplified, as the entity named must in fact be a template
3184 : : specialization, rather than, as at present, a set of overloaded
3185 : : functions containing at least one template function. */
3186 : 3107537 : if (TREE_CODE (decl) != TEMPLATE_DECL
3187 : 3107537 : && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3188 : : {
3189 : 2784449 : if (VAR_P (decl))
3190 : : {
3191 : 9781 : if (DECL_USE_TEMPLATE (decl)
3192 : 9781 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3193 : : ;
3194 : : else
3195 : 0 : permerror (input_location, "%qD is not a template", decl);
3196 : : }
3197 : 2774668 : else if (!is_overloaded_fn (decl))
3198 : 0 : permerror (input_location, "%qD is not a template", decl);
3199 : : else
3200 : : {
3201 : 2774668 : bool found = false;
3202 : :
3203 : 5549336 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3204 : 5549338 : !found && iter; ++iter)
3205 : : {
3206 : 2774670 : tree fn = *iter;
3207 : 2774670 : if (TREE_CODE (fn) == TEMPLATE_DECL
3208 : 2774274 : || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3209 : 2774684 : || (TREE_CODE (fn) == FUNCTION_DECL
3210 : 14 : && DECL_USE_TEMPLATE (fn)
3211 : 6 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3212 : : found = true;
3213 : : }
3214 : 2774668 : if (!found)
3215 : 12 : permerror (input_location, "%qD is not a template", decl);
3216 : : }
3217 : : }
3218 : 3107537 : }
3219 : :
3220 : : /* Record that an access failure occurred on BASETYPE_PATH attempting
3221 : : to access DECL, where DIAG_DECL should be used for diagnostics. */
3222 : :
3223 : : void
3224 : 412 : access_failure_info::record_access_failure (tree basetype_path,
3225 : : tree decl, tree diag_decl)
3226 : : {
3227 : 412 : m_was_inaccessible = true;
3228 : 412 : m_basetype_path = basetype_path;
3229 : 412 : m_decl = decl;
3230 : 412 : m_diag_decl = diag_decl;
3231 : 412 : }
3232 : :
3233 : : /* If an access failure was recorded, then attempt to locate an
3234 : : accessor function for the pertinent field.
3235 : : Otherwise, return NULL_TREE. */
3236 : :
3237 : : tree
3238 : 87738042 : access_failure_info::get_any_accessor (bool const_p) const
3239 : : {
3240 : 87738042 : if (!was_inaccessible_p ())
3241 : : return NULL_TREE;
3242 : :
3243 : 412 : tree accessor
3244 : 412 : = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3245 : 412 : if (!accessor)
3246 : : return NULL_TREE;
3247 : :
3248 : : /* The accessor must itself be accessible for it to be a reasonable
3249 : : suggestion. */
3250 : 161 : if (!accessible_p (m_basetype_path, accessor, true))
3251 : : return NULL_TREE;
3252 : :
3253 : : return accessor;
3254 : : }
3255 : :
3256 : : /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3257 : : replacing the primary location in RICHLOC with "accessor()". */
3258 : :
3259 : : void
3260 : 140 : access_failure_info::add_fixit_hint (rich_location *richloc,
3261 : : tree accessor_decl)
3262 : : {
3263 : 140 : pretty_printer pp;
3264 : 140 : pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3265 : 140 : pp_string (&pp, "()");
3266 : 140 : richloc->add_fixit_replace (pp_formatted_text (&pp));
3267 : 140 : }
3268 : :
3269 : : /* If an access failure was recorded, then attempt to locate an
3270 : : accessor function for the pertinent field, and if one is
3271 : : available, add a note and fix-it hint suggesting using it. */
3272 : :
3273 : : void
3274 : 87737972 : access_failure_info::maybe_suggest_accessor (bool const_p) const
3275 : : {
3276 : 87737972 : tree accessor = get_any_accessor (const_p);
3277 : 87737972 : if (accessor == NULL_TREE)
3278 : 87737874 : return;
3279 : 98 : rich_location richloc (line_table, input_location);
3280 : 98 : add_fixit_hint (&richloc, accessor);
3281 : 98 : inform (&richloc, "field %q#D can be accessed via %q#D",
3282 : 98 : m_diag_decl, accessor);
3283 : 98 : }
3284 : :
3285 : : /* Subroutine of finish_class_member_access_expr.
3286 : : Issue an error about NAME not being a member of ACCESS_PATH (or
3287 : : OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3288 : : names. */
3289 : :
3290 : : static void
3291 : 308 : complain_about_unrecognized_member (tree access_path, tree name,
3292 : : tree object_type)
3293 : : {
3294 : : /* Attempt to provide a hint about misspelled names. */
3295 : 308 : tree guessed_id = lookup_member_fuzzy (access_path, name,
3296 : : /*want_type=*/false);
3297 : 308 : if (guessed_id == NULL_TREE)
3298 : : {
3299 : : /* No hint. */
3300 : 201 : error ("%q#T has no member named %qE",
3301 : 201 : TREE_CODE (access_path) == TREE_BINFO
3302 : 6 : ? TREE_TYPE (access_path) : object_type, name);
3303 : 201 : return;
3304 : : }
3305 : :
3306 : 107 : location_t bogus_component_loc = input_location;
3307 : 107 : gcc_rich_location rich_loc (bogus_component_loc);
3308 : :
3309 : : /* Check that the guessed name is accessible along access_path. */
3310 : 107 : access_failure_info afi;
3311 : 107 : lookup_member (access_path, guessed_id, /*protect=*/1,
3312 : : /*want_type=*/false, /*complain=*/false,
3313 : : &afi);
3314 : 107 : if (afi.was_inaccessible_p ())
3315 : : {
3316 : 70 : tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3317 : 70 : if (accessor)
3318 : : {
3319 : : /* The guessed name isn't directly accessible, but can be accessed
3320 : : via an accessor member function. */
3321 : 42 : afi.add_fixit_hint (&rich_loc, accessor);
3322 : 42 : error_at (&rich_loc,
3323 : : "%q#T has no member named %qE;"
3324 : : " did you mean %q#D? (accessible via %q#D)",
3325 : 42 : TREE_CODE (access_path) == TREE_BINFO
3326 : 0 : ? TREE_TYPE (access_path) : object_type,
3327 : : name, afi.get_diag_decl (), accessor);
3328 : : }
3329 : : else
3330 : : {
3331 : : /* The guessed name isn't directly accessible, and no accessor
3332 : : member function could be found. */
3333 : 28 : auto_diagnostic_group d;
3334 : 28 : error_at (&rich_loc,
3335 : : "%q#T has no member named %qE;"
3336 : : " did you mean %q#D? (not accessible from this context)",
3337 : 28 : TREE_CODE (access_path) == TREE_BINFO
3338 : 0 : ? TREE_TYPE (access_path) : object_type,
3339 : : name, afi.get_diag_decl ());
3340 : 28 : complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3341 : : afi.get_diag_decl (), false, ak_none);
3342 : 28 : }
3343 : : }
3344 : : else
3345 : : {
3346 : : /* The guessed name is directly accessible; suggest it. */
3347 : 37 : rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3348 : : guessed_id);
3349 : 37 : error_at (&rich_loc,
3350 : : "%q#T has no member named %qE;"
3351 : : " did you mean %qE?",
3352 : 37 : TREE_CODE (access_path) == TREE_BINFO
3353 : 0 : ? TREE_TYPE (access_path) : object_type,
3354 : : name, guessed_id);
3355 : : }
3356 : 107 : }
3357 : :
3358 : : /* This function is called by the parser to process a class member
3359 : : access expression of the form OBJECT.NAME. NAME is a node used by
3360 : : the parser to represent a name; it is not yet a DECL. It may,
3361 : : however, be a BASELINK where the BASELINK_FUNCTIONS is a
3362 : : TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3363 : : there is no reason to do the lookup twice, so the parser keeps the
3364 : : BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3365 : : be a template via the use of the "A::template B" syntax. */
3366 : :
3367 : : tree
3368 : 171746724 : finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3369 : : tsubst_flags_t complain)
3370 : : {
3371 : 171746724 : tree expr;
3372 : 171746724 : tree object_type;
3373 : 171746724 : tree member;
3374 : 171746724 : tree access_path = NULL_TREE;
3375 : 171746724 : tree orig_object = object;
3376 : 171746724 : tree orig_name = name;
3377 : :
3378 : 171746724 : if (object == error_mark_node || name == error_mark_node)
3379 : : return error_mark_node;
3380 : :
3381 : : /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3382 : 171746263 : if (!objc_is_public (object, name))
3383 : 0 : return error_mark_node;
3384 : :
3385 : 171746263 : object_type = TREE_TYPE (object);
3386 : :
3387 : 171746263 : if (processing_template_decl)
3388 : : {
3389 : 143019099 : if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3390 : 143019099 : type_dependent_object_expression_p (object)
3391 : : /* If NAME is "f<args>", where either 'f' or 'args' is
3392 : : dependent, then the expression is dependent. */
3393 : 69178371 : || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3394 : 632451 : && dependent_template_id_p (TREE_OPERAND (name, 0),
3395 : 632451 : TREE_OPERAND (name, 1)))
3396 : : /* If NAME is "T::X" where "T" is dependent, then the
3397 : : expression is dependent. */
3398 : 68856455 : || (TREE_CODE (name) == SCOPE_REF
3399 : 110661 : && TYPE_P (TREE_OPERAND (name, 0))
3400 : 110661 : && dependent_scope_p (TREE_OPERAND (name, 0)))
3401 : : /* If NAME is operator T where "T" is dependent, we can't
3402 : : lookup until we instantiate the T. */
3403 : 211765107 : || (TREE_CODE (name) == IDENTIFIER_NODE
3404 : 68078046 : && IDENTIFIER_CONV_OP_P (name)
3405 : 41 : && dependent_type_p (TREE_TYPE (name))))
3406 : : {
3407 : 88248880 : dependent:
3408 : 88248880 : return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3409 : 88248880 : orig_object, orig_name, NULL_TREE);
3410 : : }
3411 : : }
3412 : 28727164 : else if (c_dialect_objc ()
3413 : 97473134 : && identifier_p (name)
3414 : 28727164 : && (expr = objc_maybe_build_component_ref (object, name)))
3415 : : return expr;
3416 : :
3417 : : /* [expr.ref]
3418 : :
3419 : : The type of the first expression shall be "class object" (of a
3420 : : complete type). */
3421 : 97473134 : if (!currently_open_class (object_type)
3422 : 97473134 : && !complete_type_or_maybe_complain (object_type, object, complain))
3423 : 86 : return error_mark_node;
3424 : 97473048 : if (!CLASS_TYPE_P (object_type))
3425 : : {
3426 : 109458 : if (complain & tf_error)
3427 : : {
3428 : 117 : if (INDIRECT_TYPE_P (object_type)
3429 : 117 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
3430 : 21 : error ("request for member %qD in %qE, which is of pointer "
3431 : : "type %qT (maybe you meant to use %<->%> ?)",
3432 : : name, object.get_value (), object_type);
3433 : : else
3434 : 96 : error ("request for member %qD in %qE, which is of non-class "
3435 : : "type %qT", name, object.get_value (), object_type);
3436 : : }
3437 : 109458 : return error_mark_node;
3438 : : }
3439 : :
3440 : 97363590 : if (BASELINK_P (name))
3441 : : /* A member function that has already been looked up. */
3442 : : member = name;
3443 : : else
3444 : : {
3445 : 87934758 : bool is_template_id = false;
3446 : 87934758 : tree template_args = NULL_TREE;
3447 : 87934758 : tree scope = NULL_TREE;
3448 : :
3449 : 87934758 : access_path = object_type;
3450 : :
3451 : 87934758 : if (TREE_CODE (name) == SCOPE_REF)
3452 : : {
3453 : : /* A qualified name. The qualifying class or namespace `S'
3454 : : has already been looked up; it is either a TYPE or a
3455 : : NAMESPACE_DECL. */
3456 : 1774 : scope = TREE_OPERAND (name, 0);
3457 : 1774 : name = TREE_OPERAND (name, 1);
3458 : :
3459 : : /* If SCOPE is a namespace, then the qualified name does not
3460 : : name a member of OBJECT_TYPE. */
3461 : 1774 : if (TREE_CODE (scope) == NAMESPACE_DECL)
3462 : : {
3463 : 0 : if (complain & tf_error)
3464 : 0 : error ("%<%D::%D%> is not a member of %qT",
3465 : : scope, name, object_type);
3466 : 0 : return error_mark_node;
3467 : : }
3468 : : }
3469 : :
3470 : 87934758 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3471 : : {
3472 : 474424 : is_template_id = true;
3473 : 474424 : template_args = TREE_OPERAND (name, 1);
3474 : 474424 : name = TREE_OPERAND (name, 0);
3475 : :
3476 : 903446 : if (!identifier_p (name))
3477 : 429040 : name = OVL_NAME (name);
3478 : : }
3479 : :
3480 : 87934758 : if (scope)
3481 : : {
3482 : 1774 : if (TREE_CODE (scope) == ENUMERAL_TYPE)
3483 : : {
3484 : 12 : gcc_assert (!is_template_id);
3485 : : /* Looking up a member enumerator (c++/56793). */
3486 : 24 : if (!TYPE_CLASS_SCOPE_P (scope)
3487 : 21 : || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3488 : : {
3489 : 3 : if (complain & tf_error)
3490 : 3 : error ("%<%D::%D%> is not a member of %qT",
3491 : : scope, name, object_type);
3492 : 3 : return error_mark_node;
3493 : : }
3494 : 9 : tree val = lookup_enumerator (scope, name);
3495 : 9 : if (!val)
3496 : : {
3497 : 6 : if (complain & tf_error)
3498 : 6 : error ("%qD is not a member of %qD",
3499 : : name, scope);
3500 : 6 : return error_mark_node;
3501 : : }
3502 : :
3503 : 3 : if (TREE_SIDE_EFFECTS (object))
3504 : 0 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3505 : 3 : return val;
3506 : : }
3507 : :
3508 : 1762 : gcc_assert (CLASS_TYPE_P (scope));
3509 : 1762 : gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3510 : :
3511 : 1762 : if (constructor_name_p (name, scope))
3512 : : {
3513 : 3 : if (complain & tf_error)
3514 : 3 : error ("cannot call constructor %<%T::%D%> directly",
3515 : : scope, name);
3516 : 3 : return error_mark_node;
3517 : : }
3518 : :
3519 : : /* NAME may refer to a static data member, in which case there is
3520 : : one copy of the data member that is shared by all the objects of
3521 : : the class. So NAME can be unambiguously referred to even if
3522 : : there are multiple indirect base classes containing NAME. */
3523 : 5277 : const base_access ba = [scope, name] ()
3524 : : {
3525 : 1759 : if (identifier_p (name))
3526 : : {
3527 : 1622 : tree m = lookup_member (scope, name, /*protect=*/0,
3528 : : /*want_type=*/false, tf_none);
3529 : 1622 : if (!m || shared_member_p (m))
3530 : 32 : return ba_any;
3531 : : }
3532 : : return ba_check;
3533 : 1759 : } ();
3534 : :
3535 : : /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3536 : 1759 : access_path = lookup_base (object_type, scope, ba, NULL, complain);
3537 : 1759 : if (access_path == error_mark_node)
3538 : : return error_mark_node;
3539 : 1747 : if (!access_path)
3540 : : {
3541 : 12 : if (any_dependent_bases_p (object_type))
3542 : 6 : goto dependent;
3543 : 6 : if (complain & tf_error)
3544 : 6 : error ("%qT is not a base of %qT", scope, object_type);
3545 : 6 : return error_mark_node;
3546 : : }
3547 : : }
3548 : :
3549 : 87934719 : if (TREE_CODE (name) == BIT_NOT_EXPR)
3550 : : {
3551 : 196747 : if (dependent_type_p (object_type))
3552 : : /* The destructor isn't declared yet. */
3553 : 34533 : goto dependent;
3554 : 162214 : member = lookup_destructor (object, scope, name, complain);
3555 : : }
3556 : : else
3557 : : {
3558 : : /* Look up the member. */
3559 : 87737972 : {
3560 : 87737972 : auto_diagnostic_group d;
3561 : 87737972 : access_failure_info afi;
3562 : 87737972 : if (processing_template_decl)
3563 : : /* Even though this class member access expression is at this
3564 : : point not dependent, the member itself may be dependent, and
3565 : : we must not potentially push a access check for a dependent
3566 : : member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3567 : : ahead of time here; we're going to redo this member lookup at
3568 : : instantiation time anyway. */
3569 : 68388733 : push_deferring_access_checks (dk_no_check);
3570 : 87737972 : member = lookup_member (access_path, name, /*protect=*/1,
3571 : : /*want_type=*/false, complain,
3572 : : &afi);
3573 : 87737972 : if (processing_template_decl)
3574 : 68388733 : pop_deferring_access_checks ();
3575 : 87737972 : afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3576 : 87737972 : }
3577 : 87737972 : if (member == NULL_TREE)
3578 : : {
3579 : 8397562 : if (dependentish_scope_p (object_type))
3580 : : /* Try again at instantiation time. */
3581 : 8377179 : goto dependent;
3582 : 20383 : if (complain & tf_error)
3583 : 308 : complain_about_unrecognized_member (access_path, name,
3584 : : object_type);
3585 : 20383 : return error_mark_node;
3586 : : }
3587 : 79340410 : if (member == error_mark_node)
3588 : : return error_mark_node;
3589 : 79340366 : if (DECL_P (member)
3590 : 79340366 : && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3591 : : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3592 : : wrong, so don't use it. */
3593 : 6 : goto dependent;
3594 : 79340360 : if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3595 : 5564027 : goto dependent;
3596 : : }
3597 : :
3598 : 73938547 : if (is_template_id)
3599 : : {
3600 : 474414 : tree templ = member;
3601 : :
3602 : 474414 : if (BASELINK_P (templ))
3603 : 474390 : member = lookup_template_function (templ, template_args);
3604 : 24 : else if (variable_template_p (templ))
3605 : 24 : member = (lookup_and_finish_template_variable
3606 : 24 : (templ, template_args, complain));
3607 : : else
3608 : : {
3609 : 0 : if (complain & tf_error)
3610 : 0 : error ("%qD is not a member template function", name);
3611 : 0 : return error_mark_node;
3612 : : }
3613 : : }
3614 : : }
3615 : :
3616 : 83367379 : if (TREE_UNAVAILABLE (member))
3617 : 30 : error_unavailable_use (member, NULL_TREE);
3618 : 83367349 : else if (TREE_DEPRECATED (member))
3619 : 30 : warn_deprecated_use (member, NULL_TREE);
3620 : :
3621 : 83367379 : if (template_p)
3622 : 9891 : check_template_keyword (member);
3623 : :
3624 : 83367379 : expr = build_class_member_access_expr (object, member, access_path,
3625 : : /*preserve_reference=*/false,
3626 : : complain);
3627 : 83367379 : if (processing_template_decl && expr != error_mark_node)
3628 : : {
3629 : 54770143 : if (BASELINK_P (member))
3630 : : {
3631 : 40777287 : if (TREE_CODE (orig_name) == SCOPE_REF)
3632 : 169 : BASELINK_QUALIFIED_P (member) = 1;
3633 : : orig_name = member;
3634 : : }
3635 : 54770143 : return build_min_non_dep (COMPONENT_REF, expr,
3636 : : orig_object, orig_name,
3637 : 54770143 : NULL_TREE);
3638 : : }
3639 : :
3640 : : return expr;
3641 : : }
3642 : :
3643 : : /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3644 : : type. */
3645 : :
3646 : : tree
3647 : 179947 : build_simple_component_ref (tree object, tree member)
3648 : : {
3649 : 179947 : tree type = cp_build_qualified_type (TREE_TYPE (member),
3650 : 179947 : cp_type_quals (TREE_TYPE (object)));
3651 : 179947 : return build3_loc (input_location,
3652 : : COMPONENT_REF, type,
3653 : 179947 : object, member, NULL_TREE);
3654 : : }
3655 : :
3656 : : /* Return an expression for the MEMBER_NAME field in the internal
3657 : : representation of PTRMEM, a pointer-to-member function. (Each
3658 : : pointer-to-member function type gets its own RECORD_TYPE so it is
3659 : : more convenient to access the fields by name than by FIELD_DECL.)
3660 : : This routine converts the NAME to a FIELD_DECL and then creates the
3661 : : node for the complete expression. */
3662 : :
3663 : : tree
3664 : 179851 : build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3665 : : {
3666 : 179851 : tree ptrmem_type;
3667 : 179851 : tree member;
3668 : :
3669 : 179851 : if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3670 : : {
3671 : 198 : for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3672 : 78 : if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3673 : 60 : return e.value;
3674 : 0 : gcc_unreachable ();
3675 : : }
3676 : :
3677 : : /* This code is a stripped down version of
3678 : : build_class_member_access_expr. It does not work to use that
3679 : : routine directly because it expects the object to be of class
3680 : : type. */
3681 : 179791 : ptrmem_type = TREE_TYPE (ptrmem);
3682 : 179791 : gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3683 : 269588 : for (member = TYPE_FIELDS (ptrmem_type); member;
3684 : 89797 : member = DECL_CHAIN (member))
3685 : 269588 : if (DECL_NAME (member) == member_name)
3686 : : break;
3687 : 179791 : return build_simple_component_ref (ptrmem, member);
3688 : : }
3689 : :
3690 : : /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3691 : : and for any other relevant operator. */
3692 : :
3693 : : static tree
3694 : 120160315 : op_unqualified_lookup (tree_code code, bool is_assign)
3695 : : {
3696 : 120160315 : tree lookups = NULL_TREE;
3697 : :
3698 : 120160315 : if (cxx_dialect >= cxx20 && !is_assign)
3699 : : {
3700 : 36229108 : if (code == NE_EXPR)
3701 : : {
3702 : : /* != can get rewritten in terms of ==. */
3703 : 2372161 : tree fnname = ovl_op_identifier (false, EQ_EXPR);
3704 : 2372161 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3705 : 2350543 : lookups = tree_cons (fnname, fns, lookups);
3706 : : }
3707 : 33856947 : else if (code == GT_EXPR || code == LE_EXPR
3708 : 33856947 : || code == LT_EXPR || code == GE_EXPR)
3709 : : {
3710 : : /* These can get rewritten in terms of <=>. */
3711 : 3161928 : tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3712 : 3161928 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3713 : 2984001 : lookups = tree_cons (fnname, fns, lookups);
3714 : : }
3715 : : }
3716 : :
3717 : 120160315 : tree fnname = ovl_op_identifier (is_assign, code);
3718 : 120160315 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3719 : 66410066 : lookups = tree_cons (fnname, fns, lookups);
3720 : :
3721 : 120160315 : if (lookups)
3722 : : return lookups;
3723 : : else
3724 : 53689773 : return build_tree_list (NULL_TREE, NULL_TREE);
3725 : : }
3726 : :
3727 : : /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3728 : : the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3729 : : name lookup for the given operator. */
3730 : :
3731 : : tree
3732 : 129561790 : build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3733 : : {
3734 : 129561790 : if (lookups)
3735 : : /* We're partially instantiating a dependent operator expression, and
3736 : : LOOKUPS is the result of phase 1 name lookup that we performed
3737 : : earlier at template definition time, so just reuse the corresponding
3738 : : DEPENDENT_OPERATOR_TYPE. */
3739 : 9401475 : return TREE_TYPE (lookups);
3740 : :
3741 : : /* Otherwise we're processing a dependent operator expression at template
3742 : : definition time, so perform phase 1 name lookup now. */
3743 : 120160315 : lookups = op_unqualified_lookup (code, is_assign);
3744 : :
3745 : 120160315 : tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3746 : 120160315 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3747 : 120160315 : TREE_TYPE (lookups) = type;
3748 : 120160315 : return type;
3749 : : }
3750 : :
3751 : : /* Given an expression PTR for a pointer, return an expression
3752 : : for the value pointed to.
3753 : : ERRORSTRING is the name of the operator to appear in error messages.
3754 : :
3755 : : This function may need to overload OPERATOR_FNNAME.
3756 : : Must also handle REFERENCE_TYPEs for C++. */
3757 : :
3758 : : tree
3759 : 41418644 : build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3760 : : tree lookups, tsubst_flags_t complain)
3761 : : {
3762 : 41418644 : tree orig_expr = expr;
3763 : 41418644 : tree rval;
3764 : 41418644 : tree overload = NULL_TREE;
3765 : :
3766 : 41418644 : if (processing_template_decl)
3767 : : {
3768 : : /* Retain the type if we know the operand is a pointer. */
3769 : 23888682 : if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3770 : : {
3771 : 12483349 : if (expr == current_class_ptr
3772 : 12483349 : || (TREE_CODE (expr) == NOP_EXPR
3773 : 7844949 : && TREE_OPERAND (expr, 0) == current_class_ptr
3774 : 7833675 : && (same_type_ignoring_top_level_qualifiers_p
3775 : 7833675 : (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3776 : 7833674 : return current_class_ref;
3777 : 4649675 : return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3778 : : }
3779 : 11405333 : if (type_dependent_expression_p (expr))
3780 : : {
3781 : 11255626 : expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3782 : 11255626 : TREE_TYPE (expr)
3783 : 11255626 : = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3784 : 11255626 : return expr;
3785 : : }
3786 : : }
3787 : :
3788 : 17679669 : rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3789 : : NULL_TREE, NULL_TREE, lookups,
3790 : : &overload, complain);
3791 : 17679669 : if (!rval)
3792 : 0 : rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3793 : :
3794 : 17679669 : if (processing_template_decl && rval != error_mark_node)
3795 : : {
3796 : 147874 : if (overload != NULL_TREE)
3797 : 147843 : return (build_min_non_dep_op_overload
3798 : 147843 : (INDIRECT_REF, rval, overload, orig_expr));
3799 : :
3800 : 31 : return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3801 : : }
3802 : : else
3803 : : return rval;
3804 : : }
3805 : :
3806 : : /* Like c-family strict_aliasing_warning, but don't warn for dependent
3807 : : types or expressions. */
3808 : :
3809 : : static bool
3810 : 571236 : cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3811 : : {
3812 : 571236 : if (processing_template_decl)
3813 : : {
3814 : 42459 : tree e = expr;
3815 : 42459 : STRIP_NOPS (e);
3816 : 42459 : if (dependent_type_p (type) || type_dependent_expression_p (e))
3817 : 6872 : return false;
3818 : : }
3819 : 564364 : return strict_aliasing_warning (loc, type, expr);
3820 : : }
3821 : :
3822 : : /* The implementation of the above, and of indirection implied by other
3823 : : constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3824 : :
3825 : : static tree
3826 : 301792290 : cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3827 : : tsubst_flags_t complain, bool do_fold)
3828 : : {
3829 : 301792290 : tree pointer, type;
3830 : :
3831 : : /* RO_NULL should only be used with the folding entry points below, not
3832 : : cp_build_indirect_ref. */
3833 : 301792290 : gcc_checking_assert (errorstring != RO_NULL || do_fold);
3834 : :
3835 : 301792290 : if (ptr == current_class_ptr
3836 : 301792290 : || (TREE_CODE (ptr) == NOP_EXPR
3837 : 30854546 : && TREE_OPERAND (ptr, 0) == current_class_ptr
3838 : 17208798 : && (same_type_ignoring_top_level_qualifiers_p
3839 : 17208798 : (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3840 : 18637354 : return current_class_ref;
3841 : :
3842 : 283154936 : pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3843 : 283154936 : ? ptr : decay_conversion (ptr, complain));
3844 : 283154936 : if (pointer == error_mark_node)
3845 : : return error_mark_node;
3846 : :
3847 : 283149088 : type = TREE_TYPE (pointer);
3848 : :
3849 : 283149088 : if (INDIRECT_TYPE_P (type))
3850 : : {
3851 : : /* [expr.unary.op]
3852 : :
3853 : : If the type of the expression is "pointer to T," the type
3854 : : of the result is "T." */
3855 : 283148747 : tree t = TREE_TYPE (type);
3856 : :
3857 : 268343317 : if ((CONVERT_EXPR_P (ptr)
3858 : 201993608 : || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3859 : 351431858 : && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3860 : : {
3861 : : /* If a warning is issued, mark it to avoid duplicates from
3862 : : the backend. This only needs to be done at
3863 : : warn_strict_aliasing > 2. */
3864 : 58896031 : if (warn_strict_aliasing > 2
3865 : 59389160 : && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3866 : 493129 : type, TREE_OPERAND (ptr, 0)))
3867 : 6 : suppress_warning (ptr, OPT_Wstrict_aliasing);
3868 : : }
3869 : :
3870 : 283148747 : if (VOID_TYPE_P (t))
3871 : : {
3872 : : /* A pointer to incomplete type (other than cv void) can be
3873 : : dereferenced [expr.unary.op]/1 */
3874 : 70 : if (complain & tf_error)
3875 : 19 : error_at (loc, "%qT is not a pointer-to-object type", type);
3876 : 70 : return error_mark_node;
3877 : : }
3878 : 273971039 : else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3879 : 288795776 : && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3880 : : /* The POINTER was something like `&x'. We simplify `*&x' to
3881 : : `x'. This can change the value category: '*&TARGET_EXPR'
3882 : : is an lvalue and folding it into 'TARGET_EXPR' turns it into
3883 : : a prvalue of class type. */
3884 : 5647070 : return TREE_OPERAND (pointer, 0);
3885 : : else
3886 : : {
3887 : 277501607 : tree ref = build1 (INDIRECT_REF, t, pointer);
3888 : :
3889 : : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3890 : : so that we get the proper error message if the result is used
3891 : : to assign to. Also, &* is supposed to be a no-op. */
3892 : 277501607 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3893 : 277501607 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3894 : 277501607 : TREE_SIDE_EFFECTS (ref)
3895 : 277501607 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3896 : 277501607 : return ref;
3897 : : }
3898 : : }
3899 : 341 : else if (!(complain & tf_error))
3900 : : /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3901 : : ;
3902 : : /* `pointer' won't be an error_mark_node if we were given a
3903 : : pointer to member, so it's cool to check for this here. */
3904 : 39 : else if (TYPE_PTRMEM_P (type))
3905 : 15 : switch (errorstring)
3906 : : {
3907 : 0 : case RO_ARRAY_INDEXING:
3908 : 0 : error_at (loc,
3909 : : "invalid use of array indexing on pointer to member");
3910 : 0 : break;
3911 : 12 : case RO_UNARY_STAR:
3912 : 12 : error_at (loc, "invalid use of unary %<*%> on pointer to member");
3913 : 12 : break;
3914 : 0 : case RO_IMPLICIT_CONVERSION:
3915 : 0 : error_at (loc, "invalid use of implicit conversion on pointer "
3916 : : "to member");
3917 : 0 : break;
3918 : 3 : case RO_ARROW_STAR:
3919 : 3 : error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3920 : : "class, but is a pointer to member of type %qT", type);
3921 : 3 : break;
3922 : 0 : default:
3923 : 0 : gcc_unreachable ();
3924 : : }
3925 : 24 : else if (pointer != error_mark_node)
3926 : 24 : invalid_indirection_error (loc, type, errorstring);
3927 : :
3928 : 341 : return error_mark_node;
3929 : : }
3930 : :
3931 : : /* Entry point used by c-common, which expects folding. */
3932 : :
3933 : : tree
3934 : 21225 : build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3935 : : {
3936 : 21225 : return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3937 : 21225 : tf_warning_or_error, true);
3938 : : }
3939 : :
3940 : : /* Entry point used by internal indirection needs that don't correspond to any
3941 : : syntactic construct. */
3942 : :
3943 : : tree
3944 : 277302450 : cp_build_fold_indirect_ref (tree pointer)
3945 : : {
3946 : 277302450 : return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3947 : 277302450 : tf_warning_or_error, true);
3948 : : }
3949 : :
3950 : : /* Entry point used by indirection needs that correspond to some syntactic
3951 : : construct. */
3952 : :
3953 : : tree
3954 : 24468615 : cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3955 : : tsubst_flags_t complain)
3956 : : {
3957 : 24468615 : return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3958 : : }
3959 : :
3960 : : /* This handles expressions of the form "a[i]", which denotes
3961 : : an array reference.
3962 : :
3963 : : This is logically equivalent in C to *(a+i), but we may do it differently.
3964 : : If A is a variable or a member, we generate a primitive ARRAY_REF.
3965 : : This avoids forcing the array out of registers, and can work on
3966 : : arrays that are not lvalues (for example, members of structures returned
3967 : : by functions).
3968 : :
3969 : : If INDEX is of some user-defined type, it must be converted to
3970 : : integer type. Otherwise, to make a compatible PLUS_EXPR, it
3971 : : will inherit the type of the array, which will be some pointer type.
3972 : :
3973 : : LOC is the location to use in building the array reference. */
3974 : :
3975 : : tree
3976 : 5281569 : cp_build_array_ref (location_t loc, tree array, tree idx,
3977 : : tsubst_flags_t complain)
3978 : : {
3979 : 5281569 : tree ret;
3980 : :
3981 : 5281569 : if (idx == 0)
3982 : : {
3983 : 0 : if (complain & tf_error)
3984 : 0 : error_at (loc, "subscript missing in array reference");
3985 : 0 : return error_mark_node;
3986 : : }
3987 : :
3988 : 5281569 : if (TREE_TYPE (array) == error_mark_node
3989 : 5281569 : || TREE_TYPE (idx) == error_mark_node)
3990 : : return error_mark_node;
3991 : :
3992 : : /* 0[array] */
3993 : 5281569 : if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE)
3994 : : {
3995 : 10 : std::swap (array, idx);
3996 : :
3997 : 10 : tree first = NULL_TREE;
3998 : 10 : if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array))
3999 : 9 : idx = first = save_expr (idx);
4000 : 10 : ret = cp_build_array_ref (loc, array, idx, complain);
4001 : :
4002 : 10 : if (first)
4003 : 9 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4004 : 10 : return ret;
4005 : : }
4006 : :
4007 : : /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
4008 : : inside it. */
4009 : 5281559 : switch (TREE_CODE (array))
4010 : : {
4011 : 28 : case COMPOUND_EXPR:
4012 : 28 : {
4013 : 28 : tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
4014 : : complain);
4015 : 28 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
4016 : 28 : TREE_OPERAND (array, 0), value);
4017 : 28 : SET_EXPR_LOCATION (ret, loc);
4018 : 28 : return ret;
4019 : : }
4020 : :
4021 : 64 : case COND_EXPR:
4022 : 64 : tree op0, op1, op2;
4023 : 64 : op0 = TREE_OPERAND (array, 0);
4024 : 64 : op1 = TREE_OPERAND (array, 1);
4025 : 64 : op2 = TREE_OPERAND (array, 2);
4026 : 64 : if (TREE_SIDE_EFFECTS (idx) || !tree_invariant_p (idx))
4027 : : {
4028 : : /* If idx could possibly have some SAVE_EXPRs, turning
4029 : : (op0 ? op1 : op2)[idx] into
4030 : : op0 ? op1[idx] : op2[idx] can lead into temporaries
4031 : : initialized in one conditional path and uninitialized
4032 : : uses of them in the other path.
4033 : : And if idx is a really large expression, evaluating it
4034 : : twice is also not optimal.
4035 : : On the other side, op0 must be sequenced before evaluation
4036 : : of op1 and op2 and for C++17 op0, op1 and op2 must be
4037 : : sequenced before idx.
4038 : : If idx is INTEGER_CST, we can just do the optimization
4039 : : without any SAVE_EXPRs, if op1 and op2 are both ARRAY_TYPE
4040 : : VAR_DECLs or COMPONENT_REFs thereof (so their address
4041 : : is constant or relative to frame), optimize into
4042 : : (SAVE_EXPR <op0>, SAVE_EXPR <idx>, SAVE_EXPR <op0>)
4043 : : ? op1[SAVE_EXPR <idx>] : op2[SAVE_EXPR <idx>]
4044 : : Otherwise avoid this optimization. */
4045 : 49 : if (flag_strong_eval_order == 2)
4046 : : {
4047 : 33 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4048 : : {
4049 : 24 : if (!address_invariant_p (op1) || !address_invariant_p (op2))
4050 : : {
4051 : : /* Force default conversion on array if
4052 : : we can't optimize this and array is ARRAY_TYPE
4053 : : COND_EXPR, we can't leave COND_EXPRs with
4054 : : ARRAY_TYPE in the IL. */
4055 : 8 : array = cp_default_conversion (array, complain);
4056 : 8 : if (error_operand_p (array))
4057 : 0 : return error_mark_node;
4058 : : break;
4059 : : }
4060 : : }
4061 : 16 : else if (!POINTER_TYPE_P (TREE_TYPE (array))
4062 : 2 : || !tree_invariant_p (op1)
4063 : 11 : || !tree_invariant_p (op2))
4064 : : break;
4065 : : }
4066 : 34 : if (TREE_SIDE_EFFECTS (idx))
4067 : : {
4068 : 12 : idx = save_expr (idx);
4069 : 12 : op0 = save_expr (op0);
4070 : 12 : tree tem = build_compound_expr (loc, op0, idx);
4071 : 12 : op0 = build_compound_expr (loc, tem, op0);
4072 : : }
4073 : : }
4074 : 49 : op1 = cp_build_array_ref (loc, op1, idx, complain);
4075 : 49 : op2 = cp_build_array_ref (loc, op2, idx, complain);
4076 : 49 : ret = build_conditional_expr (loc, op0, op1, op2, complain);
4077 : 49 : protected_set_expr_location (ret, loc);
4078 : 49 : return ret;
4079 : :
4080 : : default:
4081 : : break;
4082 : : }
4083 : :
4084 : 5281482 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
4085 : :
4086 : 5281482 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4087 : : {
4088 : 1664977 : tree rval, type;
4089 : :
4090 : 1664977 : warn_array_subscript_with_type_char (loc, idx);
4091 : :
4092 : 1664977 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
4093 : : {
4094 : 3 : if (complain & tf_error)
4095 : 3 : error_at (loc, "array subscript is not an integer");
4096 : 3 : return error_mark_node;
4097 : : }
4098 : :
4099 : : /* Apply integral promotions *after* noticing character types.
4100 : : (It is unclear why we do these promotions -- the standard
4101 : : does not say that we should. In fact, the natural thing would
4102 : : seem to be to convert IDX to ptrdiff_t; we're performing
4103 : : pointer arithmetic.) */
4104 : 1664974 : idx = cp_perform_integral_promotions (idx, complain);
4105 : :
4106 : 1664974 : idx = maybe_fold_non_dependent_expr (idx, complain);
4107 : :
4108 : : /* An array that is indexed by a non-constant
4109 : : cannot be stored in a register; we must be able to do
4110 : : address arithmetic on its address.
4111 : : Likewise an array of elements of variable size. */
4112 : 1664974 : if (TREE_CODE (idx) != INTEGER_CST
4113 : 1664974 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4114 : 826481 : && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4115 : : != INTEGER_CST)))
4116 : : {
4117 : 839744 : if (!cxx_mark_addressable (array, true))
4118 : 0 : return error_mark_node;
4119 : : }
4120 : :
4121 : : /* An array that is indexed by a constant value which is not within
4122 : : the array bounds cannot be stored in a register either; because we
4123 : : would get a crash in store_bit_field/extract_bit_field when trying
4124 : : to access a non-existent part of the register. */
4125 : 1664974 : if (TREE_CODE (idx) == INTEGER_CST
4126 : 826484 : && TYPE_DOMAIN (TREE_TYPE (array))
4127 : 2490063 : && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4128 : : {
4129 : 2836 : if (!cxx_mark_addressable (array))
4130 : 0 : return error_mark_node;
4131 : : }
4132 : :
4133 : : /* Note in C++ it is valid to subscript a `register' array, since
4134 : : it is valid to take the address of something with that
4135 : : storage specification. */
4136 : 1664974 : if (extra_warnings)
4137 : : {
4138 : 37949 : tree foo = array;
4139 : 58110 : while (TREE_CODE (foo) == COMPONENT_REF)
4140 : 20161 : foo = TREE_OPERAND (foo, 0);
4141 : 8 : if (VAR_P (foo) && DECL_REGISTER (foo)
4142 : 37949 : && (complain & tf_warning))
4143 : 0 : warning_at (loc, OPT_Wextra,
4144 : : "subscripting array declared %<register%>");
4145 : : }
4146 : :
4147 : 1664974 : type = TREE_TYPE (TREE_TYPE (array));
4148 : 1664974 : rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4149 : : /* Array ref is const/volatile if the array elements are
4150 : : or if the array is.. */
4151 : 4994922 : TREE_READONLY (rval)
4152 : 1664974 : |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4153 : 4994922 : TREE_SIDE_EFFECTS (rval)
4154 : 1664974 : |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4155 : 3329948 : TREE_THIS_VOLATILE (rval)
4156 : 1664974 : |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4157 : 1664974 : ret = require_complete_type (rval, complain);
4158 : 1664974 : protected_set_expr_location (ret, loc);
4159 : 1664974 : if (non_lvalue)
4160 : 6070 : ret = non_lvalue_loc (loc, ret);
4161 : 1664974 : return ret;
4162 : : }
4163 : :
4164 : 3616505 : {
4165 : 3616505 : tree ar = cp_default_conversion (array, complain);
4166 : 3616505 : tree ind = cp_default_conversion (idx, complain);
4167 : 3616505 : tree first = NULL_TREE;
4168 : :
4169 : 2217552 : if (!processing_template_decl && flag_strong_eval_order == 2
4170 : 5794969 : && TREE_SIDE_EFFECTS (ind))
4171 : 99263 : ar = first = save_expr (ar);
4172 : :
4173 : : /* Put the integer in IND to simplify error checking. */
4174 : 3616505 : if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4175 : 57 : std::swap (ar, ind);
4176 : :
4177 : 3616505 : if (ar == error_mark_node || ind == error_mark_node)
4178 : : return error_mark_node;
4179 : :
4180 : 3616505 : if (!TYPE_PTR_P (TREE_TYPE (ar)))
4181 : : {
4182 : 22 : if (complain & tf_error)
4183 : 3 : error_at (loc, "subscripted value is neither array nor pointer");
4184 : 22 : return error_mark_node;
4185 : : }
4186 : 3616483 : if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4187 : : {
4188 : 0 : if (complain & tf_error)
4189 : 0 : error_at (loc, "array subscript is not an integer");
4190 : 0 : return error_mark_node;
4191 : : }
4192 : :
4193 : 3616483 : warn_array_subscript_with_type_char (loc, idx);
4194 : :
4195 : 3616483 : ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4196 : 3616483 : if (first)
4197 : 99263 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4198 : 3616483 : ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4199 : 3616483 : protected_set_expr_location (ret, loc);
4200 : 3616483 : if (non_lvalue)
4201 : 0 : ret = non_lvalue_loc (loc, ret);
4202 : : return ret;
4203 : : }
4204 : : }
4205 : :
4206 : : /* Entry point for Obj-C++. */
4207 : :
4208 : : tree
4209 : 3542882 : build_array_ref (location_t loc, tree array, tree idx)
4210 : : {
4211 : 3542882 : return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4212 : : }
4213 : :
4214 : : /* Resolve a pointer to member function. INSTANCE is the object
4215 : : instance to use, if the member points to a virtual member.
4216 : :
4217 : : This used to avoid checking for virtual functions if basetype
4218 : : has no virtual functions, according to an earlier ANSI draft.
4219 : : With the final ISO C++ rules, such an optimization is
4220 : : incorrect: A pointer to a derived member can be static_cast
4221 : : to pointer-to-base-member, as long as the dynamic object
4222 : : later has the right member. So now we only do this optimization
4223 : : when we know the dynamic type of the object. */
4224 : :
4225 : : tree
4226 : 89694 : get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4227 : : tsubst_flags_t complain)
4228 : : {
4229 : 89694 : if (TREE_CODE (function) == OFFSET_REF)
4230 : 0 : function = TREE_OPERAND (function, 1);
4231 : :
4232 : 89694 : if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4233 : : {
4234 : 89694 : tree idx, delta, e1, e2, e3, vtbl;
4235 : 89694 : bool nonvirtual;
4236 : 89694 : tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4237 : 89694 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4238 : :
4239 : 89694 : tree instance_ptr = *instance_ptrptr;
4240 : 89694 : tree instance_save_expr = 0;
4241 : 89694 : if (instance_ptr == error_mark_node)
4242 : : {
4243 : 0 : if (TREE_CODE (function) == PTRMEM_CST)
4244 : : {
4245 : : /* Extracting the function address from a pmf is only
4246 : : allowed with -Wno-pmf-conversions. It only works for
4247 : : pmf constants. */
4248 : 0 : e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4249 : 0 : e1 = convert (fntype, e1);
4250 : 0 : return e1;
4251 : : }
4252 : : else
4253 : : {
4254 : 0 : if (complain & tf_error)
4255 : 0 : error ("object missing in use of %qE", function);
4256 : 0 : return error_mark_node;
4257 : : }
4258 : : }
4259 : :
4260 : : /* True if we know that the dynamic type of the object doesn't have
4261 : : virtual functions, so we can assume the PFN field is a pointer. */
4262 : 89694 : nonvirtual = (COMPLETE_TYPE_P (basetype)
4263 : 89646 : && !TYPE_POLYMORPHIC_P (basetype)
4264 : 120458 : && resolves_to_fixed_type_p (instance_ptr, 0));
4265 : :
4266 : : /* If we don't really have an object (i.e. in an ill-formed
4267 : : conversion from PMF to pointer), we can't resolve virtual
4268 : : functions anyway. */
4269 : 89442 : if (!nonvirtual && is_dummy_object (instance_ptr))
4270 : : nonvirtual = true;
4271 : :
4272 : : /* Use force_target_expr even when instance_ptr doesn't have
4273 : : side-effects, unless it is a simple decl or constant, so
4274 : : that we don't ubsan instrument the expression multiple times.
4275 : : Don't use save_expr, as save_expr can avoid building a SAVE_EXPR
4276 : : and building a SAVE_EXPR manually can be optimized away during
4277 : : cp_fold. See PR116449 and PR117259. */
4278 : 89694 : if (TREE_SIDE_EFFECTS (instance_ptr)
4279 : 89694 : || (!nonvirtual
4280 : 50037 : && !DECL_P (instance_ptr)
4281 : 50037 : && !TREE_CONSTANT (instance_ptr)))
4282 : 89196 : instance_ptr = instance_save_expr
4283 : 89196 : = get_internal_target_expr (instance_ptr);
4284 : :
4285 : : /* See above comment. */
4286 : 89694 : if (TREE_SIDE_EFFECTS (function)
4287 : 89694 : || (!nonvirtual
4288 : 69568 : && !DECL_P (function)
4289 : 69556 : && !TREE_CONSTANT (function)))
4290 : 89228 : function = get_internal_target_expr (function);
4291 : :
4292 : : /* Start by extracting all the information from the PMF itself. */
4293 : 89694 : e3 = pfn_from_ptrmemfunc (function);
4294 : 89694 : delta = delta_from_ptrmemfunc (function);
4295 : 89694 : idx = build1 (NOP_EXPR, vtable_index_type, e3);
4296 : 89694 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4297 : : {
4298 : 89694 : sanitize_code_type flag_sanitize_save;
4299 : 89694 : case ptrmemfunc_vbit_in_pfn:
4300 : 89694 : e1 = cp_build_binary_op (input_location,
4301 : : BIT_AND_EXPR, idx, integer_one_node,
4302 : : complain);
4303 : 89694 : idx = cp_build_binary_op (input_location,
4304 : : MINUS_EXPR, idx, integer_one_node,
4305 : : complain);
4306 : 89694 : if (idx == error_mark_node)
4307 : : return error_mark_node;
4308 : 89694 : break;
4309 : :
4310 : : case ptrmemfunc_vbit_in_delta:
4311 : : e1 = cp_build_binary_op (input_location,
4312 : : BIT_AND_EXPR, delta, integer_one_node,
4313 : : complain);
4314 : : /* Don't instrument the RSHIFT_EXPR we're about to create because
4315 : : we're going to use DELTA number of times, and that wouldn't play
4316 : : well with SAVE_EXPRs therein. */
4317 : : flag_sanitize_save = flag_sanitize;
4318 : : flag_sanitize = 0;
4319 : : delta = cp_build_binary_op (input_location,
4320 : : RSHIFT_EXPR, delta, integer_one_node,
4321 : : complain);
4322 : : flag_sanitize = flag_sanitize_save;
4323 : : if (delta == error_mark_node)
4324 : : return error_mark_node;
4325 : : break;
4326 : :
4327 : : default:
4328 : : gcc_unreachable ();
4329 : : }
4330 : :
4331 : 89694 : if (e1 == error_mark_node)
4332 : : return error_mark_node;
4333 : :
4334 : : /* Convert down to the right base before using the instance. A
4335 : : special case is that in a pointer to member of class C, C may
4336 : : be incomplete. In that case, the function will of course be
4337 : : a member of C, and no conversion is required. In fact,
4338 : : lookup_base will fail in that case, because incomplete
4339 : : classes do not have BINFOs. */
4340 : 89694 : if (!same_type_ignoring_top_level_qualifiers_p
4341 : 89694 : (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4342 : : {
4343 : 81 : basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4344 : : basetype, ba_check, NULL, complain);
4345 : 81 : instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4346 : : 1, complain);
4347 : 81 : if (instance_ptr == error_mark_node)
4348 : : return error_mark_node;
4349 : : }
4350 : : /* ...and then the delta in the PMF. */
4351 : 89694 : instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4352 : :
4353 : : /* Hand back the adjusted 'this' argument to our caller. */
4354 : 89694 : *instance_ptrptr = instance_ptr;
4355 : :
4356 : 89694 : if (nonvirtual)
4357 : : /* Now just return the pointer. */
4358 : : return e3;
4359 : :
4360 : : /* Next extract the vtable pointer from the object. */
4361 : 89424 : vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4362 : : instance_ptr);
4363 : 89424 : vtbl = cp_build_fold_indirect_ref (vtbl);
4364 : 89424 : if (vtbl == error_mark_node)
4365 : : return error_mark_node;
4366 : :
4367 : : /* Finally, extract the function pointer from the vtable. */
4368 : 89424 : e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4369 : 89424 : e2 = cp_build_fold_indirect_ref (e2);
4370 : 89424 : if (e2 == error_mark_node)
4371 : : return error_mark_node;
4372 : 89424 : TREE_CONSTANT (e2) = 1;
4373 : :
4374 : : /* When using function descriptors, the address of the
4375 : : vtable entry is treated as a function pointer. */
4376 : 89424 : if (TARGET_VTABLE_USES_DESCRIPTORS)
4377 : : e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4378 : : cp_build_addr_expr (e2, complain));
4379 : :
4380 : 89424 : e2 = fold_convert (TREE_TYPE (e3), e2);
4381 : 89424 : e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4382 : 89424 : if (e1 == error_mark_node)
4383 : : return error_mark_node;
4384 : :
4385 : : /* Make sure this doesn't get evaluated first inside one of the
4386 : : branches of the COND_EXPR. */
4387 : 89424 : if (instance_save_expr)
4388 : 89148 : e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4389 : : instance_save_expr, e1);
4390 : :
4391 : : function = e1;
4392 : : }
4393 : : return function;
4394 : : }
4395 : :
4396 : : /* Used by the C-common bits. */
4397 : : tree
4398 : 0 : build_function_call (location_t /*loc*/,
4399 : : tree function, tree params)
4400 : : {
4401 : 0 : return cp_build_function_call (function, params, tf_warning_or_error);
4402 : : }
4403 : :
4404 : : /* Used by the C-common bits. */
4405 : : tree
4406 : 235836 : build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4407 : : tree function, vec<tree, va_gc> *params,
4408 : : vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4409 : : {
4410 : 235836 : vec<tree, va_gc> *orig_params = params;
4411 : 235836 : tree ret = cp_build_function_call_vec (function, ¶ms,
4412 : : tf_warning_or_error, orig_function);
4413 : :
4414 : : /* cp_build_function_call_vec can reallocate PARAMS by adding
4415 : : default arguments. That should never happen here. Verify
4416 : : that. */
4417 : 235836 : gcc_assert (params == orig_params);
4418 : :
4419 : 235836 : return ret;
4420 : : }
4421 : :
4422 : : /* Build a function call using a tree list of arguments. */
4423 : :
4424 : : static tree
4425 : 0 : cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4426 : : {
4427 : 0 : tree ret;
4428 : :
4429 : 0 : releasing_vec vec;
4430 : 0 : for (; params != NULL_TREE; params = TREE_CHAIN (params))
4431 : 0 : vec_safe_push (vec, TREE_VALUE (params));
4432 : 0 : ret = cp_build_function_call_vec (function, &vec, complain);
4433 : 0 : return ret;
4434 : 0 : }
4435 : :
4436 : : /* Build a function call using varargs. */
4437 : :
4438 : : tree
4439 : 521228 : cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4440 : : {
4441 : 521228 : va_list args;
4442 : 521228 : tree ret, t;
4443 : :
4444 : 521228 : releasing_vec vec;
4445 : 521228 : va_start (args, complain);
4446 : 1287006 : for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4447 : 765778 : vec_safe_push (vec, t);
4448 : 521228 : va_end (args);
4449 : 521228 : ret = cp_build_function_call_vec (function, &vec, complain);
4450 : 521228 : return ret;
4451 : 521228 : }
4452 : :
4453 : : /* C++ implementation of callback for use when checking param types. */
4454 : :
4455 : : bool
4456 : 18 : cp_comp_parm_types (tree wanted_type, tree actual_type)
4457 : : {
4458 : 18 : if (TREE_CODE (wanted_type) == POINTER_TYPE
4459 : 18 : && TREE_CODE (actual_type) == POINTER_TYPE)
4460 : 15 : return same_or_base_type_p (TREE_TYPE (wanted_type),
4461 : : TREE_TYPE (actual_type));
4462 : : return false;
4463 : : }
4464 : :
4465 : : /* Build a function call using a vector of arguments.
4466 : : If FUNCTION is the result of resolving an overloaded target built-in,
4467 : : ORIG_FNDECL is the original function decl, otherwise it is null.
4468 : : PARAMS may be NULL if there are no parameters. This changes the
4469 : : contents of PARAMS. */
4470 : :
4471 : : tree
4472 : 3830417 : cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4473 : : tsubst_flags_t complain, tree orig_fndecl)
4474 : : {
4475 : 3830417 : tree fntype, fndecl;
4476 : 3830417 : int is_method;
4477 : 3830417 : tree original = function;
4478 : 3830417 : int nargs;
4479 : 3830417 : tree *argarray;
4480 : 3830417 : tree parm_types;
4481 : 3830417 : vec<tree, va_gc> *allocated = NULL;
4482 : 3830417 : tree ret;
4483 : :
4484 : : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4485 : : expressions, like those used for ObjC messenger dispatches. */
4486 : 3830417 : if (params != NULL && !vec_safe_is_empty (*params))
4487 : 1749218 : function = objc_rewrite_function_call (function, (**params)[0]);
4488 : :
4489 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4490 : : Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4491 : 3830417 : if (TREE_CODE (function) == NOP_EXPR
4492 : 3830417 : && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4493 : 15 : function = TREE_OPERAND (function, 0);
4494 : :
4495 : 3830417 : if (TREE_CODE (function) == FUNCTION_DECL)
4496 : : {
4497 : 1369869 : if (!mark_used (function, complain))
4498 : 24 : return error_mark_node;
4499 : 1369845 : fndecl = function;
4500 : :
4501 : : /* For target_version semantics, the function set cannot be called
4502 : : if there is no default version in scope. */
4503 : 1369845 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
4504 : : && !is_function_default_version (fndecl))
4505 : : {
4506 : : if (complain & tf_error)
4507 : : error ("no default version in scope");
4508 : : return error_mark_node;
4509 : : }
4510 : :
4511 : : /* Convert anything with function type to a pointer-to-function. */
4512 : 1369845 : if (DECL_MAIN_P (function))
4513 : : {
4514 : 0 : if (complain & tf_error)
4515 : 0 : pedwarn (input_location, OPT_Wpedantic,
4516 : : "ISO C++ forbids calling %<::main%> from within program");
4517 : : else
4518 : 0 : return error_mark_node;
4519 : : }
4520 : 1369845 : function = build_addr_func (function, complain);
4521 : : }
4522 : : else
4523 : : {
4524 : 2460548 : fndecl = NULL_TREE;
4525 : :
4526 : 2460548 : function = build_addr_func (function, complain);
4527 : : }
4528 : :
4529 : 3830393 : if (function == error_mark_node)
4530 : : return error_mark_node;
4531 : :
4532 : 3830291 : fntype = TREE_TYPE (function);
4533 : :
4534 : 3830291 : if (TYPE_PTRMEMFUNC_P (fntype))
4535 : : {
4536 : 15 : if (complain & tf_error)
4537 : 15 : error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4538 : : "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4539 : : original, original);
4540 : 15 : return error_mark_node;
4541 : : }
4542 : :
4543 : 7660552 : is_method = (TYPE_PTR_P (fntype)
4544 : 3830276 : && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4545 : :
4546 : 3830276 : if (!(TYPE_PTRFN_P (fntype)
4547 : 91028 : || is_method
4548 : 1407 : || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4549 : : {
4550 : 1407 : if (complain & tf_error)
4551 : : {
4552 : 174 : if (is_stub_object (original))
4553 : 3 : error_at (input_location,
4554 : : "%qT cannot be used as a function",
4555 : 3 : TREE_TYPE (original));
4556 : 171 : else if (!flag_diagnostics_show_caret)
4557 : 171 : error_at (input_location,
4558 : : "%qE cannot be used as a function", original);
4559 : 0 : else if (DECL_P (original))
4560 : 0 : error_at (input_location,
4561 : : "%qD cannot be used as a function", original);
4562 : : else
4563 : 0 : error_at (input_location,
4564 : : "expression cannot be used as a function");
4565 : : }
4566 : :
4567 : 1407 : return error_mark_node;
4568 : : }
4569 : :
4570 : : /* fntype now gets the type of function pointed to. */
4571 : 3828869 : fntype = TREE_TYPE (fntype);
4572 : 3828869 : parm_types = TYPE_ARG_TYPES (fntype);
4573 : :
4574 : 3828869 : if (params == NULL)
4575 : : {
4576 : 161592 : allocated = make_tree_vector ();
4577 : 161592 : params = &allocated;
4578 : : }
4579 : :
4580 : 3828869 : nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4581 : : complain);
4582 : 3828869 : if (nargs < 0)
4583 : 1654 : return error_mark_node;
4584 : :
4585 : 3827215 : argarray = (*params)->address ();
4586 : :
4587 : : /* Check for errors in format strings and inappropriately
4588 : : null parameters. */
4589 : 3827215 : bool warned_p
4590 : 3827215 : = ((complain & tf_warning)
4591 : 3827215 : && check_function_arguments (input_location, fndecl, fntype,
4592 : : nargs, argarray, NULL,
4593 : 3827215 : cp_comp_parm_types));
4594 : :
4595 : 3827215 : ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4596 : :
4597 : 3827215 : if (warned_p)
4598 : : {
4599 : 27 : tree c = extract_call_expr (ret);
4600 : 27 : if (TREE_CODE (c) == CALL_EXPR)
4601 : 27 : suppress_warning (c, OPT_Wnonnull);
4602 : : }
4603 : :
4604 : 3827215 : if (allocated != NULL)
4605 : 161592 : release_tree_vector (allocated);
4606 : :
4607 : : return ret;
4608 : : }
4609 : :
4610 : : /* Subroutine of convert_arguments.
4611 : : Print an error message about a wrong number of arguments. */
4612 : :
4613 : : static void
4614 : 162 : error_args_num (location_t loc, tree fndecl, bool too_many_p)
4615 : : {
4616 : 162 : if (fndecl)
4617 : : {
4618 : 126 : auto_diagnostic_group d;
4619 : 126 : if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4620 : : {
4621 : 0 : if (DECL_NAME (fndecl) == NULL_TREE
4622 : 0 : || (DECL_NAME (fndecl)
4623 : 0 : == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4624 : 0 : error_at (loc,
4625 : : too_many_p
4626 : : ? G_("too many arguments to constructor %q#D")
4627 : : : G_("too few arguments to constructor %q#D"),
4628 : : fndecl);
4629 : : else
4630 : 0 : error_at (loc,
4631 : : too_many_p
4632 : : ? G_("too many arguments to member function %q#D")
4633 : : : G_("too few arguments to member function %q#D"),
4634 : : fndecl);
4635 : : }
4636 : : else
4637 : 189 : error_at (loc,
4638 : : too_many_p
4639 : : ? G_("too many arguments to function %q#D")
4640 : : : G_("too few arguments to function %q#D"),
4641 : : fndecl);
4642 : 126 : if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4643 : 87 : inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4644 : 126 : }
4645 : : else
4646 : : {
4647 : 36 : if (c_dialect_objc () && objc_message_selector ())
4648 : 0 : error_at (loc,
4649 : : too_many_p
4650 : : ? G_("too many arguments to method %q#D")
4651 : : : G_("too few arguments to method %q#D"),
4652 : : objc_message_selector ());
4653 : : else
4654 : 57 : error_at (loc, too_many_p ? G_("too many arguments to function")
4655 : : : G_("too few arguments to function"));
4656 : : }
4657 : 162 : }
4658 : :
4659 : : /* Convert the actual parameter expressions in the list VALUES to the
4660 : : types in the list TYPELIST. The converted expressions are stored
4661 : : back in the VALUES vector.
4662 : : If parmdecls is exhausted, or when an element has NULL as its type,
4663 : : perform the default conversions.
4664 : :
4665 : : NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4666 : :
4667 : : This is also where warnings about wrong number of args are generated.
4668 : :
4669 : : Returns the actual number of arguments processed (which might be less
4670 : : than the length of the vector), or -1 on error.
4671 : :
4672 : : In C++, unspecified trailing parameters can be filled in with their
4673 : : default arguments, if such were specified. Do so here. */
4674 : :
4675 : : static int
4676 : 3828869 : convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4677 : : int flags, tsubst_flags_t complain)
4678 : : {
4679 : 3828869 : tree typetail;
4680 : 3828869 : unsigned int i;
4681 : :
4682 : : /* Argument passing is always copy-initialization. */
4683 : 3828869 : flags |= LOOKUP_ONLYCONVERTING;
4684 : :
4685 : 7763788 : for (i = 0, typetail = typelist;
4686 : 7763788 : i < vec_safe_length (*values);
4687 : : i++)
4688 : : {
4689 : 7872383 : tree type = typetail ? TREE_VALUE (typetail) : 0;
4690 : 3936416 : tree val = (**values)[i];
4691 : :
4692 : 3936416 : if (val == error_mark_node || type == error_mark_node)
4693 : : return -1;
4694 : :
4695 : 3936410 : if (type == void_type_node)
4696 : : {
4697 : 206 : if (complain & tf_error)
4698 : 78 : error_args_num (input_location, fndecl, /*too_many_p=*/true);
4699 : 206 : return -1;
4700 : : }
4701 : :
4702 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4703 : : Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4704 : 3936204 : if (TREE_CODE (val) == NOP_EXPR
4705 : 950155 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4706 : 3936222 : && (type == 0 || !TYPE_REF_P (type)))
4707 : 18 : val = TREE_OPERAND (val, 0);
4708 : :
4709 : 3936204 : if (type == 0 || !TYPE_REF_P (type))
4710 : : {
4711 : 3715790 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4712 : 3715790 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4713 : 10793 : val = decay_conversion (val, complain);
4714 : : }
4715 : :
4716 : 3936204 : if (val == error_mark_node)
4717 : : return -1;
4718 : :
4719 : 3936204 : if (type != 0)
4720 : : {
4721 : : /* Formal parm type is specified by a function prototype. */
4722 : 3935755 : tree parmval;
4723 : :
4724 : 3935755 : if (!COMPLETE_TYPE_P (complete_type (type)))
4725 : : {
4726 : 18 : if (complain & tf_error)
4727 : : {
4728 : 12 : location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4729 : 12 : if (fndecl)
4730 : : {
4731 : 12 : auto_diagnostic_group d;
4732 : 12 : error_at (loc,
4733 : : "parameter %P of %qD has incomplete type %qT",
4734 : : i, fndecl, type);
4735 : 12 : inform (get_fndecl_argument_location (fndecl, i),
4736 : : " declared here");
4737 : 12 : }
4738 : : else
4739 : 0 : error_at (loc, "parameter %P has incomplete type %qT", i,
4740 : : type);
4741 : : }
4742 : 18 : parmval = error_mark_node;
4743 : : }
4744 : : else
4745 : : {
4746 : 3935737 : parmval = convert_for_initialization
4747 : 3935737 : (NULL_TREE, type, val, flags,
4748 : : ICR_ARGPASS, fndecl, i, complain);
4749 : 3935737 : parmval = convert_for_arg_passing (type, parmval, complain);
4750 : : }
4751 : :
4752 : 3935755 : if (parmval == error_mark_node)
4753 : : return -1;
4754 : :
4755 : 3934470 : (**values)[i] = parmval;
4756 : : }
4757 : : else
4758 : : {
4759 : 449 : int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4760 : 21 : if (magic)
4761 : : {
4762 : : /* Don't truncate excess precision to the semantic type. */
4763 : 0 : if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4764 : 0 : val = TREE_OPERAND (val, 0);
4765 : : /* Don't do ellipsis conversion for __built_in_constant_p
4766 : : as this will result in spurious errors for non-trivial
4767 : : types. */
4768 : 0 : val = require_complete_type (val, complain);
4769 : : }
4770 : : else
4771 : 449 : val = convert_arg_to_ellipsis (val, complain);
4772 : :
4773 : 449 : (**values)[i] = val;
4774 : : }
4775 : :
4776 : 3934919 : if (typetail)
4777 : 3934470 : typetail = TREE_CHAIN (typetail);
4778 : : }
4779 : :
4780 : 3827372 : if (typetail != 0 && typetail != void_list_node)
4781 : : {
4782 : : /* See if there are default arguments that can be used. Because
4783 : : we hold default arguments in the FUNCTION_TYPE (which is so
4784 : : wrong), we can see default parameters here from deduced
4785 : : contexts (and via typeof) for indirect function calls.
4786 : : Fortunately we know whether we have a function decl to
4787 : : provide default arguments in a language conformant
4788 : : manner. */
4789 : 63 : if (fndecl && TREE_PURPOSE (typetail)
4790 : 160 : && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4791 : : {
4792 : 6 : for (; typetail != void_list_node; ++i)
4793 : : {
4794 : : /* After DR777, with explicit template args we can end up with a
4795 : : default argument followed by no default argument. */
4796 : 6 : if (!TREE_PURPOSE (typetail))
4797 : : break;
4798 : 3 : tree parmval
4799 : 3 : = convert_default_arg (TREE_VALUE (typetail),
4800 : 3 : TREE_PURPOSE (typetail),
4801 : 3 : fndecl, i, complain);
4802 : :
4803 : 3 : if (parmval == error_mark_node)
4804 : 0 : return -1;
4805 : :
4806 : 3 : vec_safe_push (*values, parmval);
4807 : 3 : typetail = TREE_CHAIN (typetail);
4808 : : /* ends with `...'. */
4809 : 3 : if (typetail == NULL_TREE)
4810 : : break;
4811 : : }
4812 : : }
4813 : :
4814 : 157 : if (typetail && typetail != void_list_node)
4815 : : {
4816 : 157 : if (complain & tf_error)
4817 : 84 : error_args_num (input_location, fndecl, /*too_many_p=*/false);
4818 : 157 : return -1;
4819 : : }
4820 : : }
4821 : :
4822 : 3827215 : return (int) i;
4823 : : }
4824 : :
4825 : : /* Build a binary-operation expression, after performing default
4826 : : conversions on the operands. CODE is the kind of expression to
4827 : : build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4828 : : are the tree codes which correspond to ARG1 and ARG2 when issuing
4829 : : warnings about possibly misplaced parentheses. They may differ
4830 : : from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4831 : : folding (e.g., if the parser sees "a | 1 + 1", it may call this
4832 : : routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4833 : : To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4834 : : ARG2_CODE as ERROR_MARK. */
4835 : :
4836 : : tree
4837 : 197580657 : build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4838 : : enum tree_code arg1_code, tree arg2,
4839 : : enum tree_code arg2_code, tree lookups,
4840 : : tree *overload_p, tsubst_flags_t complain)
4841 : : {
4842 : 197580657 : tree orig_arg1;
4843 : 197580657 : tree orig_arg2;
4844 : 197580657 : tree expr;
4845 : 197580657 : tree overload = NULL_TREE;
4846 : :
4847 : 197580657 : orig_arg1 = arg1;
4848 : 197580657 : orig_arg2 = arg2;
4849 : :
4850 : 197580657 : if (processing_template_decl)
4851 : : {
4852 : 116959078 : if (type_dependent_expression_p (arg1)
4853 : 116959078 : || type_dependent_expression_p (arg2))
4854 : : {
4855 : 83362746 : expr = build_min_nt_loc (loc, code, arg1, arg2);
4856 : 83362746 : TREE_TYPE (expr)
4857 : 83362746 : = build_dependent_operator_type (lookups, code, false);
4858 : 83362746 : return expr;
4859 : : }
4860 : : }
4861 : :
4862 : 114217911 : if (code == DOTSTAR_EXPR)
4863 : 89983 : expr = build_m_component_ref (arg1, arg2, complain);
4864 : : else
4865 : 114127928 : expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4866 : : lookups, &overload, complain);
4867 : :
4868 : 114217878 : if (overload_p != NULL)
4869 : 47673929 : *overload_p = overload;
4870 : :
4871 : : /* Check for cases such as x+y<<z which users are likely to
4872 : : misinterpret. But don't warn about obj << x + y, since that is a
4873 : : common idiom for I/O. */
4874 : 114217878 : if (warn_parentheses
4875 : 1147691 : && (complain & tf_warning)
4876 : 1100650 : && !processing_template_decl
4877 : 763209 : && !error_operand_p (arg1)
4878 : 763203 : && !error_operand_p (arg2)
4879 : 114981069 : && (code != LSHIFT_EXPR
4880 : 68879 : || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4881 : 762188 : warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4882 : : arg2_code, orig_arg2);
4883 : :
4884 : 114217878 : if (processing_template_decl && expr != error_mark_node)
4885 : : {
4886 : 33596141 : if (overload != NULL_TREE)
4887 : 2340103 : return (build_min_non_dep_op_overload
4888 : 2340103 : (code, expr, overload, orig_arg1, orig_arg2));
4889 : :
4890 : 31256038 : return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4891 : : }
4892 : :
4893 : : return expr;
4894 : : }
4895 : :
4896 : : /* Build and return an ARRAY_REF expression. */
4897 : :
4898 : : tree
4899 : 1983329 : build_x_array_ref (location_t loc, tree arg1, tree arg2,
4900 : : tsubst_flags_t complain)
4901 : : {
4902 : 1983329 : tree orig_arg1 = arg1;
4903 : 1983329 : tree orig_arg2 = arg2;
4904 : 1983329 : tree expr;
4905 : 1983329 : tree overload = NULL_TREE;
4906 : :
4907 : 1983329 : if (processing_template_decl)
4908 : : {
4909 : 688 : if (type_dependent_expression_p (arg1)
4910 : 688 : || type_dependent_expression_p (arg2))
4911 : 655 : return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4912 : 655 : NULL_TREE, NULL_TREE);
4913 : : }
4914 : :
4915 : 1982674 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4916 : : NULL_TREE, NULL_TREE, &overload, complain);
4917 : :
4918 : 1982674 : if (processing_template_decl && expr != error_mark_node)
4919 : : {
4920 : 30 : if (overload != NULL_TREE)
4921 : 16 : return (build_min_non_dep_op_overload
4922 : 16 : (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4923 : :
4924 : 14 : return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4925 : 14 : NULL_TREE, NULL_TREE);
4926 : : }
4927 : : return expr;
4928 : : }
4929 : :
4930 : : /* Build an OpenMP array section reference, creating an exact type for the
4931 : : resulting expression based on the element type and bounds if possible. If
4932 : : we have variable bounds, create an incomplete array type for the result
4933 : : instead. */
4934 : :
4935 : : tree
4936 : 10852 : build_omp_array_section (location_t loc, tree array_expr, tree index,
4937 : : tree length)
4938 : : {
4939 : 10852 : if (TREE_CODE (array_expr) == TYPE_DECL
4940 : 10852 : || type_dependent_expression_p (array_expr))
4941 : 274 : return build3_loc (loc, OMP_ARRAY_SECTION, NULL_TREE, array_expr, index,
4942 : 274 : length);
4943 : :
4944 : 10578 : tree type = TREE_TYPE (array_expr);
4945 : 10578 : gcc_assert (type);
4946 : 10578 : type = non_reference (type);
4947 : :
4948 : 10578 : tree sectype, eltype = TREE_TYPE (type);
4949 : :
4950 : : /* It's not an array or pointer type. Just reuse the type of the
4951 : : original expression as the type of the array section (an error will be
4952 : : raised anyway, later). */
4953 : 10578 : if (eltype == NULL_TREE)
4954 : 42 : sectype = TREE_TYPE (array_expr);
4955 : : else
4956 : : {
4957 : 10536 : tree idxtype = NULL_TREE;
4958 : :
4959 : : /* If we know the integer bounds, create an index type with exact
4960 : : low/high (or zero/length) bounds. Otherwise, create an incomplete
4961 : : array type. (This mostly only affects diagnostics.) */
4962 : 10536 : if (index != NULL_TREE
4963 : 10536 : && length != NULL_TREE
4964 : 7555 : && TREE_CODE (index) == INTEGER_CST
4965 : 6757 : && TREE_CODE (length) == INTEGER_CST)
4966 : : {
4967 : 5262 : tree low = fold_convert (sizetype, index);
4968 : 5262 : tree high = fold_convert (sizetype, length);
4969 : 5262 : high = size_binop (PLUS_EXPR, low, high);
4970 : 5262 : high = size_binop (MINUS_EXPR, high, size_one_node);
4971 : 5262 : idxtype = build_range_type (sizetype, low, high);
4972 : 5262 : }
4973 : 2950 : else if ((index == NULL_TREE || integer_zerop (index))
4974 : 3642 : && length != NULL_TREE
4975 : 7464 : && TREE_CODE (length) == INTEGER_CST)
4976 : 1471 : idxtype = build_index_type (length);
4977 : :
4978 : 10536 : sectype = build_array_type (eltype, idxtype);
4979 : : }
4980 : :
4981 : 10578 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array_expr, index,
4982 : 10578 : length);
4983 : : }
4984 : :
4985 : : /* Return whether OP is an expression of enum type cast to integer
4986 : : type. In C++ even unsigned enum types are cast to signed integer
4987 : : types. We do not want to issue warnings about comparisons between
4988 : : signed and unsigned types when one of the types is an enum type.
4989 : : Those warnings are always false positives in practice. */
4990 : :
4991 : : static bool
4992 : 595382 : enum_cast_to_int (tree op)
4993 : : {
4994 : 583213 : if (CONVERT_EXPR_P (op)
4995 : 13207 : && TREE_TYPE (op) == integer_type_node
4996 : 823 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4997 : 595397 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4998 : : return true;
4999 : :
5000 : : /* The cast may have been pushed into a COND_EXPR. */
5001 : 595375 : if (TREE_CODE (op) == COND_EXPR)
5002 : 761 : return (enum_cast_to_int (TREE_OPERAND (op, 1))
5003 : 761 : || enum_cast_to_int (TREE_OPERAND (op, 2)));
5004 : :
5005 : : return false;
5006 : : }
5007 : :
5008 : : /* For the c-common bits. */
5009 : : tree
5010 : 4017132 : build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
5011 : : bool /*convert_p*/)
5012 : : {
5013 : 4017132 : return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
5014 : : }
5015 : :
5016 : : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
5017 : : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
5018 : :
5019 : : static tree
5020 : 6593 : build_vec_cmp (tree_code code, tree type,
5021 : : tree arg0, tree arg1)
5022 : : {
5023 : 6593 : tree zero_vec = build_zero_cst (type);
5024 : 6593 : tree minus_one_vec = build_minus_one_cst (type);
5025 : 6593 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
5026 : 6593 : tree cmp = build2 (code, cmp_type, arg0, arg1);
5027 : 6593 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
5028 : : }
5029 : :
5030 : : /* Possibly warn about an address never being NULL. */
5031 : :
5032 : : static void
5033 : 1575492 : warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
5034 : : {
5035 : : /* Prevent warnings issued for macro expansion. */
5036 : 1575492 : if (!warn_address
5037 : 40738 : || (complain & tf_warning) == 0
5038 : 24382 : || c_inhibit_evaluation_warnings != 0
5039 : 24342 : || from_macro_expansion_at (location)
5040 : 1593220 : || warning_suppressed_p (op, OPT_Waddress))
5041 : 1558087 : return;
5042 : :
5043 : 17695 : tree cop = fold_for_warn (op);
5044 : :
5045 : 17695 : if (TREE_CODE (cop) == NON_LVALUE_EXPR)
5046 : : /* Unwrap the expression for C++ 98. */
5047 : 1 : cop = TREE_OPERAND (cop, 0);
5048 : :
5049 : 17695 : if (TREE_CODE (cop) == PTRMEM_CST)
5050 : : {
5051 : : /* The address of a nonstatic data member is never null. */
5052 : 30 : warning_at (location, OPT_Waddress,
5053 : : "the address %qE will never be NULL",
5054 : : cop);
5055 : 30 : return;
5056 : : }
5057 : :
5058 : 17665 : if (TREE_CODE (cop) == NOP_EXPR)
5059 : : {
5060 : : /* Allow casts to intptr_t to suppress the warning. */
5061 : 1212 : tree type = TREE_TYPE (cop);
5062 : 1212 : if (TREE_CODE (type) == INTEGER_TYPE)
5063 : : return;
5064 : :
5065 : 1212 : STRIP_NOPS (cop);
5066 : : }
5067 : :
5068 : 17665 : auto_diagnostic_group d;
5069 : 17665 : bool warned = false;
5070 : 17665 : if (TREE_CODE (cop) == ADDR_EXPR)
5071 : : {
5072 : 789 : cop = TREE_OPERAND (cop, 0);
5073 : :
5074 : : /* Set to true in the loop below if OP dereferences its operand.
5075 : : In such a case the ultimate target need not be a decl for
5076 : : the null [in]equality test to be necessarily constant. */
5077 : 789 : bool deref = false;
5078 : :
5079 : : /* Get the outermost array or object, or member. */
5080 : 960 : while (handled_component_p (cop))
5081 : : {
5082 : 270 : if (TREE_CODE (cop) == COMPONENT_REF)
5083 : : {
5084 : : /* Get the member (its address is never null). */
5085 : 99 : cop = TREE_OPERAND (cop, 1);
5086 : 99 : break;
5087 : : }
5088 : :
5089 : : /* Get the outer array/object to refer to in the warning. */
5090 : 171 : cop = TREE_OPERAND (cop, 0);
5091 : 171 : deref = true;
5092 : : }
5093 : :
5094 : 639 : if ((!deref && !decl_with_nonnull_addr_p (cop))
5095 : 631 : || from_macro_expansion_at (location)
5096 : 1420 : || warning_suppressed_p (cop, OPT_Waddress))
5097 : 158 : return;
5098 : :
5099 : 631 : warned = warning_at (location, OPT_Waddress,
5100 : : "the address of %qD will never be NULL", cop);
5101 : 631 : op = cop;
5102 : : }
5103 : 16876 : else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
5104 : : {
5105 : : /* Adding zero to the null pointer is well-defined in C++. When
5106 : : the offset is unknown (i.e., not a constant) warn anyway since
5107 : : it's less likely that the pointer operand is null than not. */
5108 : 102 : tree off = TREE_OPERAND (cop, 1);
5109 : 102 : if (!integer_zerop (off)
5110 : 102 : && !warning_suppressed_p (cop, OPT_Waddress))
5111 : : {
5112 : 102 : tree base = TREE_OPERAND (cop, 0);
5113 : 102 : STRIP_NOPS (base);
5114 : 102 : if (TYPE_REF_P (TREE_TYPE (base)))
5115 : 12 : warning_at (location, OPT_Waddress, "the compiler can assume that "
5116 : : "the address of %qE will never be NULL", base);
5117 : : else
5118 : 90 : warning_at (location, OPT_Waddress, "comparing the result of "
5119 : : "pointer addition %qE and NULL", cop);
5120 : : }
5121 : 102 : return;
5122 : : }
5123 : 15843 : else if (CONVERT_EXPR_P (op)
5124 : 16836 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
5125 : : {
5126 : 62 : STRIP_NOPS (op);
5127 : :
5128 : 62 : if (TREE_CODE (op) == COMPONENT_REF)
5129 : 24 : op = TREE_OPERAND (op, 1);
5130 : :
5131 : 62 : if (DECL_P (op))
5132 : 62 : warned = warning_at (location, OPT_Waddress,
5133 : : "the compiler can assume that the address of "
5134 : : "%qD will never be NULL", op);
5135 : : }
5136 : :
5137 : 693 : if (warned && DECL_P (op))
5138 : 657 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
5139 : 17665 : }
5140 : :
5141 : : /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5142 : : the other operand is of a different enumeration type or a floating-point
5143 : : type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5144 : : code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5145 : : and LOC is the location for the whole binary expression.
5146 : : For C++26 this is ill-formed rather than deprecated.
5147 : : Return true for SFINAE errors.
5148 : : TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5149 : :
5150 : : static bool
5151 : 94292959 : do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5152 : : tree type1, tsubst_flags_t complain)
5153 : : {
5154 : 94292959 : if (TREE_CODE (type0) == ENUMERAL_TYPE
5155 : 2784703 : && TREE_CODE (type1) == ENUMERAL_TYPE
5156 : 96562318 : && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
5157 : : {
5158 : 198 : if (cxx_dialect >= cxx26)
5159 : : {
5160 : 60 : if ((complain & tf_warning_or_error) == 0)
5161 : : return true;
5162 : : }
5163 : 138 : else if ((complain & tf_warning) == 0)
5164 : : return false;
5165 : : /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5166 : : Otherwise, warn if -Wenum-conversion is on. */
5167 : 190 : enum opt_code opt;
5168 : 190 : if (warn_deprecated_enum_enum_conv)
5169 : : opt = OPT_Wdeprecated_enum_enum_conversion;
5170 : 97 : else if (warn_enum_conversion)
5171 : : opt = OPT_Wenum_conversion;
5172 : : else
5173 : : return false;
5174 : :
5175 : 115 : switch (code)
5176 : : {
5177 : : case GT_EXPR:
5178 : : case LT_EXPR:
5179 : : case GE_EXPR:
5180 : : case LE_EXPR:
5181 : : case EQ_EXPR:
5182 : : case NE_EXPR:
5183 : : /* Comparisons are handled by -Wenum-compare. */
5184 : : return false;
5185 : : case SPACESHIP_EXPR:
5186 : : /* This is invalid, don't warn. */
5187 : : return false;
5188 : 17 : case BIT_AND_EXPR:
5189 : 17 : case BIT_IOR_EXPR:
5190 : 17 : case BIT_XOR_EXPR:
5191 : 17 : if (cxx_dialect >= cxx26)
5192 : 5 : pedwarn (loc, opt, "bitwise operation between different "
5193 : : "enumeration types %qT and %qT", type0, type1);
5194 : : else
5195 : 12 : warning_at (loc, opt, "bitwise operation between different "
5196 : : "enumeration types %qT and %qT is deprecated",
5197 : : type0, type1);
5198 : 17 : return false;
5199 : 41 : default:
5200 : 41 : if (cxx_dialect >= cxx26)
5201 : 12 : pedwarn (loc, opt, "arithmetic between different enumeration "
5202 : : "types %qT and %qT", type0, type1);
5203 : : else
5204 : 29 : warning_at (loc, opt, "arithmetic between different enumeration "
5205 : : "types %qT and %qT is deprecated", type0, type1);
5206 : 41 : return false;
5207 : : }
5208 : : }
5209 : 94292761 : else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5210 : 2784505 : && SCALAR_FLOAT_TYPE_P (type1))
5211 : 94292684 : || (SCALAR_FLOAT_TYPE_P (type0)
5212 : 32668598 : && TREE_CODE (type1) == ENUMERAL_TYPE))
5213 : : {
5214 : 154 : if (cxx_dialect >= cxx26)
5215 : : {
5216 : 46 : if ((complain & tf_warning_or_error) == 0)
5217 : : return true;
5218 : : }
5219 : 108 : else if ((complain & tf_warning) == 0)
5220 : : return false;
5221 : 142 : const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5222 : : /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5223 : : Otherwise, warn if -Wenum-conversion is on. */
5224 : 142 : enum opt_code opt;
5225 : 142 : if (warn_deprecated_enum_float_conv)
5226 : : opt = OPT_Wdeprecated_enum_float_conversion;
5227 : 75 : else if (warn_enum_conversion)
5228 : : opt = OPT_Wenum_conversion;
5229 : : else
5230 : : return false;
5231 : :
5232 : 84 : switch (code)
5233 : : {
5234 : 34 : case GT_EXPR:
5235 : 34 : case LT_EXPR:
5236 : 34 : case GE_EXPR:
5237 : 34 : case LE_EXPR:
5238 : 34 : case EQ_EXPR:
5239 : 34 : case NE_EXPR:
5240 : 34 : if (enum_first_p && cxx_dialect >= cxx26)
5241 : 5 : pedwarn (loc, opt, "comparison of enumeration type %qT with "
5242 : : "floating-point type %qT", type0, type1);
5243 : 29 : else if (cxx_dialect >= cxx26)
5244 : 5 : pedwarn (loc, opt, "comparison of floating-point type %qT "
5245 : : "with enumeration type %qT", type0, type1);
5246 : 24 : else if (enum_first_p)
5247 : 12 : warning_at (loc, opt, "comparison of enumeration type %qT with "
5248 : : "floating-point type %qT is deprecated",
5249 : : type0, type1);
5250 : : else
5251 : 12 : warning_at (loc, opt, "comparison of floating-point type %qT "
5252 : : "with enumeration type %qT is deprecated",
5253 : : type0, type1);
5254 : 34 : return false;
5255 : : case SPACESHIP_EXPR:
5256 : : /* This is invalid, don't warn. */
5257 : : return false;
5258 : 38 : default:
5259 : 38 : if (enum_first_p && cxx_dialect >= cxx26)
5260 : 5 : pedwarn (loc, opt, "arithmetic between enumeration type %qT "
5261 : : "and floating-point type %qT", type0, type1);
5262 : 33 : else if (cxx_dialect >= cxx26)
5263 : 6 : pedwarn (loc, opt, "arithmetic between floating-point type %qT "
5264 : : "and enumeration type %qT", type0, type1);
5265 : 27 : else if (enum_first_p)
5266 : 12 : warning_at (loc, opt, "arithmetic between enumeration type %qT "
5267 : : "and floating-point type %qT is deprecated",
5268 : : type0, type1);
5269 : : else
5270 : 15 : warning_at (loc, opt, "arithmetic between floating-point type %qT "
5271 : : "and enumeration type %qT is deprecated",
5272 : : type0, type1);
5273 : 38 : return false;
5274 : : }
5275 : : }
5276 : : return false;
5277 : : }
5278 : :
5279 : : /* Build a binary-operation expression without default conversions.
5280 : : CODE is the kind of expression to build.
5281 : : LOCATION is the location_t of the operator in the source code.
5282 : : This function differs from `build' in several ways:
5283 : : the data type of the result is computed and recorded in it,
5284 : : warnings are generated if arg data types are invalid,
5285 : : special handling for addition and subtraction of pointers is known,
5286 : : and some optimization is done (operations on narrow ints
5287 : : are done in the narrower type when that gives the same result).
5288 : : Constant folding is also done before the result is returned.
5289 : :
5290 : : Note that the operands will never have enumeral types
5291 : : because either they have just had the default conversions performed
5292 : : or they have both just been converted to some other type in which
5293 : : the arithmetic is to be done.
5294 : :
5295 : : C++: must do special pointer arithmetic when implementing
5296 : : multiple inheritance, and deal with pointer to member functions. */
5297 : :
5298 : : tree
5299 : 124338373 : cp_build_binary_op (const op_location_t &location,
5300 : : enum tree_code code, tree orig_op0, tree orig_op1,
5301 : : tsubst_flags_t complain)
5302 : : {
5303 : 124338373 : tree op0, op1;
5304 : 124338373 : enum tree_code code0, code1;
5305 : 124338373 : tree type0, type1, orig_type0, orig_type1;
5306 : 124338373 : const char *invalid_op_diag;
5307 : :
5308 : : /* Expression code to give to the expression when it is built.
5309 : : Normally this is CODE, which is what the caller asked for,
5310 : : but in some special cases we change it. */
5311 : 124338373 : enum tree_code resultcode = code;
5312 : :
5313 : : /* Data type in which the computation is to be performed.
5314 : : In the simplest cases this is the common type of the arguments. */
5315 : 124338373 : tree result_type = NULL_TREE;
5316 : :
5317 : : /* When the computation is in excess precision, the type of the
5318 : : final EXCESS_PRECISION_EXPR. */
5319 : 124338373 : tree semantic_result_type = NULL;
5320 : :
5321 : : /* Nonzero means operands have already been type-converted
5322 : : in whatever way is necessary.
5323 : : Zero means they need to be converted to RESULT_TYPE. */
5324 : 124338373 : int converted = 0;
5325 : :
5326 : : /* Nonzero means create the expression with this type, rather than
5327 : : RESULT_TYPE. */
5328 : 124338373 : tree build_type = 0;
5329 : :
5330 : : /* Nonzero means after finally constructing the expression
5331 : : convert it to this type. */
5332 : 124338373 : tree final_type = 0;
5333 : :
5334 : 124338373 : tree result;
5335 : :
5336 : : /* Nonzero if this is an operation like MIN or MAX which can
5337 : : safely be computed in short if both args are promoted shorts.
5338 : : Also implies COMMON.
5339 : : -1 indicates a bitwise operation; this makes a difference
5340 : : in the exact conditions for when it is safe to do the operation
5341 : : in a narrower mode. */
5342 : 124338373 : int shorten = 0;
5343 : :
5344 : : /* Nonzero if this is a comparison operation;
5345 : : if both args are promoted shorts, compare the original shorts.
5346 : : Also implies COMMON. */
5347 : 124338373 : int short_compare = 0;
5348 : :
5349 : : /* Nonzero if this is a right-shift operation, which can be computed on the
5350 : : original short and then promoted if the operand is a promoted short. */
5351 : 124338373 : int short_shift = 0;
5352 : :
5353 : : /* Nonzero means set RESULT_TYPE to the common type of the args. */
5354 : 124338373 : int common = 0;
5355 : :
5356 : : /* True if both operands have arithmetic type. */
5357 : 124338373 : bool arithmetic_types_p;
5358 : :
5359 : : /* Remember whether we're doing / or %. */
5360 : 124338373 : bool doing_div_or_mod = false;
5361 : :
5362 : : /* Remember whether we're doing << or >>. */
5363 : 124338373 : bool doing_shift = false;
5364 : :
5365 : : /* Tree holding instrumentation expression. */
5366 : 124338373 : tree instrument_expr = NULL_TREE;
5367 : :
5368 : : /* True means this is an arithmetic operation that may need excess
5369 : : precision. */
5370 : 124338373 : bool may_need_excess_precision;
5371 : :
5372 : : /* Apply default conversions. */
5373 : 124338373 : op0 = resolve_nondeduced_context (orig_op0, complain);
5374 : 124338373 : op1 = resolve_nondeduced_context (orig_op1, complain);
5375 : :
5376 : 124338373 : if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5377 : 113037994 : || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5378 : 110274388 : || code == TRUTH_XOR_EXPR)
5379 : : {
5380 : 14063985 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5381 : 14063967 : op0 = decay_conversion (op0, complain);
5382 : 14063985 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5383 : 14063985 : op1 = decay_conversion (op1, complain);
5384 : : }
5385 : : else
5386 : : {
5387 : 110274388 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5388 : 110274373 : op0 = cp_default_conversion (op0, complain);
5389 : 110274388 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5390 : 110274385 : op1 = cp_default_conversion (op1, complain);
5391 : : }
5392 : :
5393 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5394 : 131733187 : STRIP_TYPE_NOPS (op0);
5395 : 152289203 : STRIP_TYPE_NOPS (op1);
5396 : :
5397 : : /* DTRT if one side is an overloaded function, but complain about it. */
5398 : 124338373 : if (type_unknown_p (op0))
5399 : : {
5400 : 9 : tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5401 : 9 : if (t != error_mark_node)
5402 : : {
5403 : 0 : if (complain & tf_error)
5404 : 0 : permerror (location,
5405 : : "assuming cast to type %qT from overloaded function",
5406 : 0 : TREE_TYPE (t));
5407 : : op0 = t;
5408 : : }
5409 : : }
5410 : 124338373 : if (type_unknown_p (op1))
5411 : : {
5412 : 3 : tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5413 : 3 : if (t != error_mark_node)
5414 : : {
5415 : 3 : if (complain & tf_error)
5416 : 3 : permerror (location,
5417 : : "assuming cast to type %qT from overloaded function",
5418 : 3 : TREE_TYPE (t));
5419 : : op1 = t;
5420 : : }
5421 : : }
5422 : :
5423 : 124338373 : orig_type0 = type0 = TREE_TYPE (op0);
5424 : 124338373 : orig_type1 = type1 = TREE_TYPE (op1);
5425 : 124338373 : tree non_ep_op0 = op0;
5426 : 124338373 : tree non_ep_op1 = op1;
5427 : :
5428 : : /* The expression codes of the data types of the arguments tell us
5429 : : whether the arguments are integers, floating, pointers, etc. */
5430 : 124338373 : code0 = TREE_CODE (type0);
5431 : 124338373 : code1 = TREE_CODE (type1);
5432 : :
5433 : : /* If an error was already reported for one of the arguments,
5434 : : avoid reporting another error. */
5435 : 124338373 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5436 : 100 : return error_mark_node;
5437 : :
5438 : 248676546 : if ((invalid_op_diag
5439 : 124338273 : = targetm.invalid_binary_op (code, type0, type1)))
5440 : : {
5441 : 0 : if (complain & tf_error)
5442 : : {
5443 : 0 : if (code0 == REAL_TYPE
5444 : 0 : && code1 == REAL_TYPE
5445 : 0 : && (extended_float_type_p (type0)
5446 : 0 : || extended_float_type_p (type1))
5447 : 0 : && cp_compare_floating_point_conversion_ranks (type0,
5448 : : type1) == 3)
5449 : : {
5450 : 0 : rich_location richloc (line_table, location);
5451 : 0 : binary_op_error (&richloc, code, type0, type1);
5452 : 0 : }
5453 : : else
5454 : 0 : error (invalid_op_diag);
5455 : : }
5456 : 0 : return error_mark_node;
5457 : : }
5458 : :
5459 : 124338273 : switch (code)
5460 : : {
5461 : : case PLUS_EXPR:
5462 : : case MINUS_EXPR:
5463 : : case MULT_EXPR:
5464 : : case TRUNC_DIV_EXPR:
5465 : : case CEIL_DIV_EXPR:
5466 : : case FLOOR_DIV_EXPR:
5467 : : case ROUND_DIV_EXPR:
5468 : : case EXACT_DIV_EXPR:
5469 : : may_need_excess_precision = true;
5470 : : break;
5471 : 41761659 : case EQ_EXPR:
5472 : 41761659 : case NE_EXPR:
5473 : 41761659 : case LE_EXPR:
5474 : 41761659 : case GE_EXPR:
5475 : 41761659 : case LT_EXPR:
5476 : 41761659 : case GT_EXPR:
5477 : 41761659 : case SPACESHIP_EXPR:
5478 : : /* Excess precision for implicit conversions of integers to
5479 : : floating point. */
5480 : 9158817 : may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5481 : 50914453 : || ANY_INTEGRAL_TYPE_P (type1));
5482 : : break;
5483 : : default:
5484 : 124338273 : may_need_excess_precision = false;
5485 : : break;
5486 : : }
5487 : 124338273 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5488 : : {
5489 : 22044 : op0 = TREE_OPERAND (op0, 0);
5490 : 22044 : type0 = TREE_TYPE (op0);
5491 : : }
5492 : 124316229 : else if (may_need_excess_precision
5493 : 94478743 : && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5494 : 28527436 : if (tree eptype = excess_precision_type (type0))
5495 : : {
5496 : 59139 : type0 = eptype;
5497 : 59139 : op0 = convert (eptype, op0);
5498 : : }
5499 : 124338273 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5500 : : {
5501 : 27936 : op1 = TREE_OPERAND (op1, 0);
5502 : 27936 : type1 = TREE_TYPE (op1);
5503 : : }
5504 : 124310337 : else if (may_need_excess_precision
5505 : 94476022 : && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5506 : 28448773 : if (tree eptype = excess_precision_type (type1))
5507 : : {
5508 : 38687 : type1 = eptype;
5509 : 38687 : op1 = convert (eptype, op1);
5510 : : }
5511 : :
5512 : : /* Issue warnings about peculiar, but valid, uses of NULL. */
5513 : 248676461 : if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5514 : : /* It's reasonable to use pointer values as operands of &&
5515 : : and ||, so NULL is no exception. */
5516 : 12274 : && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5517 : 12259 : && ( /* Both are NULL (or 0) and the operation was not a
5518 : : comparison or a pointer subtraction. */
5519 : 12332 : (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5520 : 27 : && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5521 : : /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5522 : 12247 : || (!null_ptr_cst_p (orig_op0)
5523 : 12186 : && !TYPE_PTR_OR_PTRMEM_P (type0))
5524 : 12235 : || (!null_ptr_cst_p (orig_op1)
5525 : 46 : && !TYPE_PTR_OR_PTRMEM_P (type1)))
5526 : 124338312 : && (complain & tf_warning))
5527 : : {
5528 : 39 : location_t loc =
5529 : 39 : expansion_point_location_if_in_system_header (input_location);
5530 : :
5531 : 39 : warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5532 : : }
5533 : :
5534 : : /* In case when one of the operands of the binary operation is
5535 : : a vector and another is a scalar -- convert scalar to vector. */
5536 : 124395527 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5537 : 124394679 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5538 : : {
5539 : 933 : enum stv_conv convert_flag
5540 : 933 : = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5541 : : complain & tf_error);
5542 : :
5543 : 933 : switch (convert_flag)
5544 : : {
5545 : 21 : case stv_error:
5546 : 21 : return error_mark_node;
5547 : 55 : case stv_firstarg:
5548 : 55 : {
5549 : 55 : op0 = convert (TREE_TYPE (type1), op0);
5550 : 55 : op0 = cp_save_expr (op0);
5551 : 55 : op0 = build_vector_from_val (type1, op0);
5552 : 55 : orig_type0 = type0 = TREE_TYPE (op0);
5553 : 55 : code0 = TREE_CODE (type0);
5554 : 55 : converted = 1;
5555 : 55 : break;
5556 : : }
5557 : 752 : case stv_secondarg:
5558 : 752 : {
5559 : 752 : op1 = convert (TREE_TYPE (type0), op1);
5560 : 752 : op1 = cp_save_expr (op1);
5561 : 752 : op1 = build_vector_from_val (type0, op1);
5562 : 752 : orig_type1 = type1 = TREE_TYPE (op1);
5563 : 752 : code1 = TREE_CODE (type1);
5564 : 752 : converted = 1;
5565 : 752 : break;
5566 : : }
5567 : : default:
5568 : : break;
5569 : : }
5570 : : }
5571 : :
5572 : 124338252 : switch (code)
5573 : : {
5574 : 14527324 : case MINUS_EXPR:
5575 : : /* Subtraction of two similar pointers.
5576 : : We must subtract them as integers, then divide by object size. */
5577 : 14527324 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5578 : 15823645 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5579 : 1296321 : TREE_TYPE (type1)))
5580 : : {
5581 : 1296319 : result = pointer_diff (location, op0, op1,
5582 : : common_pointer_type (type0, type1), complain,
5583 : : &instrument_expr);
5584 : 1296319 : if (instrument_expr != NULL)
5585 : 84 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5586 : : instrument_expr, result);
5587 : :
5588 : 1296319 : return result;
5589 : : }
5590 : : /* In all other cases except pointer - int, the usual arithmetic
5591 : : rules apply. */
5592 : 13231005 : else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5593 : : {
5594 : : common = 1;
5595 : : break;
5596 : : }
5597 : : /* The pointer - int case is just like pointer + int; fall
5598 : : through. */
5599 : 19087946 : gcc_fallthrough ();
5600 : 19087946 : case PLUS_EXPR:
5601 : 19087946 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5602 : 6352014 : && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5603 : : {
5604 : 6352005 : tree ptr_operand;
5605 : 6352005 : tree int_operand;
5606 : 6352005 : ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5607 : 43268 : int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5608 : 6352005 : if (processing_template_decl)
5609 : : {
5610 : 2010601 : result_type = TREE_TYPE (ptr_operand);
5611 : 2010601 : break;
5612 : : }
5613 : 4341404 : return cp_pointer_int_sum (location, code,
5614 : : ptr_operand,
5615 : : int_operand,
5616 : 4341404 : complain);
5617 : : }
5618 : : common = 1;
5619 : : break;
5620 : :
5621 : : case MULT_EXPR:
5622 : 118666845 : common = 1;
5623 : : break;
5624 : :
5625 : 9554123 : case TRUNC_DIV_EXPR:
5626 : 9554123 : case CEIL_DIV_EXPR:
5627 : 9554123 : case FLOOR_DIV_EXPR:
5628 : 9554123 : case ROUND_DIV_EXPR:
5629 : 9554123 : case EXACT_DIV_EXPR:
5630 : 9554123 : if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5631 : : {
5632 : 101122 : tree type0 = TREE_OPERAND (op0, 0);
5633 : 101122 : tree type1 = TREE_OPERAND (op1, 0);
5634 : 101122 : tree first_arg = tree_strip_any_location_wrapper (type0);
5635 : 101122 : if (!TYPE_P (type0))
5636 : 81250 : type0 = TREE_TYPE (type0);
5637 : 101122 : if (!TYPE_P (type1))
5638 : 46691 : type1 = TREE_TYPE (type1);
5639 : 101122 : if (type0
5640 : 101122 : && type1
5641 : 80829 : && INDIRECT_TYPE_P (type0)
5642 : 101197 : && same_type_p (TREE_TYPE (type0), type1))
5643 : : {
5644 : 27 : if (!(TREE_CODE (first_arg) == PARM_DECL
5645 : 21 : && DECL_ARRAY_PARAMETER_P (first_arg)
5646 : 3 : && warn_sizeof_array_argument)
5647 : 42 : && (complain & tf_warning))
5648 : : {
5649 : 21 : auto_diagnostic_group d;
5650 : 21 : if (warning_at (location, OPT_Wsizeof_pointer_div,
5651 : : "division %<sizeof (%T) / sizeof (%T)%> does "
5652 : : "not compute the number of array elements",
5653 : : type0, type1))
5654 : 18 : if (DECL_P (first_arg))
5655 : 18 : inform (DECL_SOURCE_LOCATION (first_arg),
5656 : : "first %<sizeof%> operand was declared here");
5657 : 21 : }
5658 : : }
5659 : 101098 : else if (!dependent_type_p (type0)
5660 : 26498 : && !dependent_type_p (type1)
5661 : 26400 : && TREE_CODE (type0) == ARRAY_TYPE
5662 : 22326 : && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5663 : : /* Set by finish_parenthesized_expr. */
5664 : 21679 : && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5665 : 122759 : && (complain & tf_warning))
5666 : 21661 : maybe_warn_sizeof_array_div (location, first_arg, type0,
5667 : : op1, non_reference (type1));
5668 : : }
5669 : :
5670 : 9554123 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5671 : : || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5672 : 9554105 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5673 : : || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5674 : : {
5675 : 9554096 : enum tree_code tcode0 = code0, tcode1 = code1;
5676 : 9554096 : doing_div_or_mod = true;
5677 : 9554096 : warn_for_div_by_zero (location, fold_for_warn (op1));
5678 : :
5679 : 9554096 : if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5680 : 60688 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5681 : 9554096 : if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5682 : 31068 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5683 : :
5684 : 9554096 : if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5685 : : resultcode = RDIV_EXPR;
5686 : : else
5687 : : {
5688 : : /* When dividing two signed integers, we have to promote to int.
5689 : : unless we divide by a constant != -1. Note that default
5690 : : conversion will have been performed on the operands at this
5691 : : point, so we have to dig out the original type to find out if
5692 : : it was unsigned. */
5693 : 3806297 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5694 : 3806297 : shorten = may_shorten_divmod (op0, stripped_op1);
5695 : : }
5696 : :
5697 : : common = 1;
5698 : : }
5699 : : break;
5700 : :
5701 : 2073295 : case BIT_AND_EXPR:
5702 : 2073295 : case BIT_IOR_EXPR:
5703 : 2073295 : case BIT_XOR_EXPR:
5704 : 2073295 : if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5705 : 2073295 : || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5706 : 17510 : && !VECTOR_FLOAT_TYPE_P (type0)
5707 : 17492 : && !VECTOR_FLOAT_TYPE_P (type1)))
5708 : : shorten = -1;
5709 : : break;
5710 : :
5711 : 1187168 : case TRUNC_MOD_EXPR:
5712 : 1187168 : case FLOOR_MOD_EXPR:
5713 : 1187168 : doing_div_or_mod = true;
5714 : 1187168 : warn_for_div_by_zero (location, fold_for_warn (op1));
5715 : :
5716 : 1187168 : if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5717 : 20 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5718 : 1187188 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5719 : : common = 1;
5720 : 1187148 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5721 : : {
5722 : : /* Although it would be tempting to shorten always here, that loses
5723 : : on some targets, since the modulo instruction is undefined if the
5724 : : quotient can't be represented in the computation mode. We shorten
5725 : : only if unsigned or if dividing by something we know != -1. */
5726 : 1187124 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5727 : 1187124 : shorten = may_shorten_divmod (op0, stripped_op1);
5728 : 1187124 : common = 1;
5729 : : }
5730 : : break;
5731 : :
5732 : 14063970 : case TRUTH_ANDIF_EXPR:
5733 : 14063970 : case TRUTH_ORIF_EXPR:
5734 : 14063970 : case TRUTH_AND_EXPR:
5735 : 14063970 : case TRUTH_OR_EXPR:
5736 : 14063970 : if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5737 : : {
5738 : 3 : if (!COMPARISON_CLASS_P (op1))
5739 : 3 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5740 : : build_zero_cst (type1), complain);
5741 : 3 : if (code == TRUTH_ANDIF_EXPR)
5742 : : {
5743 : 0 : tree z = build_zero_cst (TREE_TYPE (op1));
5744 : 0 : return build_conditional_expr (location, op0, op1, z, complain);
5745 : : }
5746 : 3 : else if (code == TRUTH_ORIF_EXPR)
5747 : : {
5748 : 3 : tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5749 : 3 : return build_conditional_expr (location, op0, m1, op1, complain);
5750 : : }
5751 : : else
5752 : 0 : gcc_unreachable ();
5753 : : }
5754 : 14063967 : if (gnu_vector_type_p (type0)
5755 : 14063967 : && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5756 : : {
5757 : 24 : if (!COMPARISON_CLASS_P (op0))
5758 : 24 : op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5759 : : build_zero_cst (type0), complain);
5760 : 24 : if (!VECTOR_TYPE_P (type1))
5761 : : {
5762 : 9 : tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5763 : 9 : tree z = build_zero_cst (TREE_TYPE (op0));
5764 : 9 : op1 = build_conditional_expr (location, op1, m1, z, complain);
5765 : : }
5766 : 15 : else if (!COMPARISON_CLASS_P (op1))
5767 : 15 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5768 : : build_zero_cst (type1), complain);
5769 : :
5770 : 24 : if (code == TRUTH_ANDIF_EXPR)
5771 : : code = BIT_AND_EXPR;
5772 : 9 : else if (code == TRUTH_ORIF_EXPR)
5773 : : code = BIT_IOR_EXPR;
5774 : : else
5775 : 0 : gcc_unreachable ();
5776 : :
5777 : 24 : return cp_build_binary_op (location, code, op0, op1, complain);
5778 : : }
5779 : :
5780 : 14063943 : result_type = boolean_type_node;
5781 : 14063943 : break;
5782 : :
5783 : : /* Shift operations: result has same type as first operand;
5784 : : always convert second operand to int.
5785 : : Also set SHORT_SHIFT if shifting rightward. */
5786 : :
5787 : 926389 : case RSHIFT_EXPR:
5788 : 926389 : if (gnu_vector_type_p (type0)
5789 : 170 : && code1 == INTEGER_TYPE
5790 : 926436 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5791 : : {
5792 : : result_type = type0;
5793 : : converted = 1;
5794 : : }
5795 : 926342 : else if (gnu_vector_type_p (type0)
5796 : 123 : && gnu_vector_type_p (type1)
5797 : 123 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5798 : 120 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5799 : 926462 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5800 : : TYPE_VECTOR_SUBPARTS (type1)))
5801 : : {
5802 : : result_type = type0;
5803 : : converted = 1;
5804 : : }
5805 : 926222 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5806 : : {
5807 : 926195 : tree const_op1 = fold_for_warn (op1);
5808 : 926195 : if (TREE_CODE (const_op1) != INTEGER_CST)
5809 : 79592 : const_op1 = op1;
5810 : 926195 : result_type = type0;
5811 : 926195 : doing_shift = true;
5812 : 926195 : if (TREE_CODE (const_op1) == INTEGER_CST)
5813 : : {
5814 : 846603 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5815 : : {
5816 : 48 : if ((complain & tf_warning)
5817 : 48 : && c_inhibit_evaluation_warnings == 0)
5818 : 36 : warning_at (location, OPT_Wshift_count_negative,
5819 : : "right shift count is negative");
5820 : : }
5821 : : else
5822 : : {
5823 : 846555 : if (!integer_zerop (const_op1))
5824 : 846469 : short_shift = 1;
5825 : :
5826 : 846555 : if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5827 : 46 : && (complain & tf_warning)
5828 : 846601 : && c_inhibit_evaluation_warnings == 0)
5829 : 34 : warning_at (location, OPT_Wshift_count_overflow,
5830 : : "right shift count >= width of type");
5831 : : }
5832 : : }
5833 : : /* Avoid converting op1 to result_type later. */
5834 : : converted = 1;
5835 : : }
5836 : : break;
5837 : :
5838 : 2816731 : case LSHIFT_EXPR:
5839 : 2816731 : if (gnu_vector_type_p (type0)
5840 : 183 : && code1 == INTEGER_TYPE
5841 : 2816768 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5842 : : {
5843 : : result_type = type0;
5844 : : converted = 1;
5845 : : }
5846 : 2816694 : else if (gnu_vector_type_p (type0)
5847 : 146 : && gnu_vector_type_p (type1)
5848 : 146 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5849 : 143 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5850 : 2816834 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5851 : : TYPE_VECTOR_SUBPARTS (type1)))
5852 : : {
5853 : : result_type = type0;
5854 : : converted = 1;
5855 : : }
5856 : 2816554 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5857 : : {
5858 : 2816523 : tree const_op0 = fold_for_warn (op0);
5859 : 2816523 : if (TREE_CODE (const_op0) != INTEGER_CST)
5860 : 145970 : const_op0 = op0;
5861 : 2816523 : tree const_op1 = fold_for_warn (op1);
5862 : 2816523 : if (TREE_CODE (const_op1) != INTEGER_CST)
5863 : 216326 : const_op1 = op1;
5864 : 2816523 : result_type = type0;
5865 : 2816523 : doing_shift = true;
5866 : 2816523 : if (TREE_CODE (const_op0) == INTEGER_CST
5867 : 2670553 : && tree_int_cst_sgn (const_op0) < 0
5868 : 280 : && !TYPE_OVERFLOW_WRAPS (type0)
5869 : 196 : && (complain & tf_warning)
5870 : 2816719 : && c_inhibit_evaluation_warnings == 0)
5871 : 196 : warning_at (location, OPT_Wshift_negative_value,
5872 : : "left shift of negative value");
5873 : 2816523 : if (TREE_CODE (const_op1) == INTEGER_CST)
5874 : : {
5875 : 2600197 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5876 : : {
5877 : 57 : if ((complain & tf_warning)
5878 : 57 : && c_inhibit_evaluation_warnings == 0)
5879 : 39 : warning_at (location, OPT_Wshift_count_negative,
5880 : : "left shift count is negative");
5881 : : }
5882 : 2600140 : else if (compare_tree_int (const_op1,
5883 : 2600140 : TYPE_PRECISION (type0)) >= 0)
5884 : : {
5885 : 83 : if ((complain & tf_warning)
5886 : 61 : && c_inhibit_evaluation_warnings == 0)
5887 : 45 : warning_at (location, OPT_Wshift_count_overflow,
5888 : : "left shift count >= width of type");
5889 : : }
5890 : 2600057 : else if (TREE_CODE (const_op0) == INTEGER_CST
5891 : 2480073 : && (complain & tf_warning))
5892 : 2367940 : maybe_warn_shift_overflow (location, const_op0, const_op1);
5893 : : }
5894 : : /* Avoid converting op1 to result_type later. */
5895 : : converted = 1;
5896 : : }
5897 : : break;
5898 : :
5899 : 20696146 : case EQ_EXPR:
5900 : 20696146 : case NE_EXPR:
5901 : 20696146 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5902 : 2415 : goto vector_compare;
5903 : 20693731 : if ((complain & tf_warning)
5904 : 19305255 : && c_inhibit_evaluation_warnings == 0
5905 : 39353055 : && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5906 : 824729 : warning_at (location, OPT_Wfloat_equal,
5907 : : "comparing floating-point with %<==%> "
5908 : : "or %<!=%> is unsafe");
5909 : 20693731 : {
5910 : 20693731 : tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5911 : 20693731 : tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5912 : 20693731 : if ((complain & tf_warning_or_error)
5913 : 20693731 : && ((TREE_CODE (stripped_orig_op0) == STRING_CST
5914 : 39 : && !integer_zerop (cp_fully_fold (op1)))
5915 : 19567066 : || (TREE_CODE (stripped_orig_op1) == STRING_CST
5916 : 68 : && !integer_zerop (cp_fully_fold (op0)))))
5917 : 47 : warning_at (location, OPT_Waddress,
5918 : : "comparison with string literal results in "
5919 : : "unspecified behavior");
5920 : 20693684 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5921 : 20693684 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5922 : : {
5923 : : /* P2865R5 made array comparisons ill-formed in C++26. */
5924 : 76 : if (complain & tf_warning_or_error)
5925 : 45 : do_warn_array_compare (location, code, stripped_orig_op0,
5926 : : stripped_orig_op1);
5927 : 31 : else if (cxx_dialect >= cxx26)
5928 : 13 : return error_mark_node;
5929 : : }
5930 : : }
5931 : :
5932 : 20693718 : build_type = boolean_type_node;
5933 : 20693718 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5934 : : || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5935 : 17442307 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5936 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5937 : : short_compare = 1;
5938 : 28940 : else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5939 : 3250942 : && null_ptr_cst_p (orig_op1))
5940 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5941 : 5003676 : || (code0 == POINTER_TYPE
5942 : 1696132 : && TYPE_PTR_P (type1) && integer_zerop (op1)))
5943 : : {
5944 : 1574321 : if (TYPE_PTR_P (type1))
5945 : 19883 : result_type = composite_pointer_type (location,
5946 : : type0, type1, op0, op1,
5947 : : CPO_COMPARISON, complain);
5948 : : else
5949 : : result_type = type0;
5950 : :
5951 : 1574321 : if (char_type_p (TREE_TYPE (orig_op1)))
5952 : : {
5953 : 10 : auto_diagnostic_group d;
5954 : 10 : if (warning_at (location, OPT_Wpointer_compare,
5955 : : "comparison between pointer and zero character "
5956 : : "constant"))
5957 : 10 : inform (location,
5958 : : "did you mean to dereference the pointer?");
5959 : 10 : }
5960 : 1574321 : warn_for_null_address (location, op0, complain);
5961 : : }
5962 : 906 : else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5963 : 1704226 : && null_ptr_cst_p (orig_op0))
5964 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5965 : 3408575 : || (code1 == POINTER_TYPE
5966 : 1702957 : && TYPE_PTR_P (type0) && integer_zerop (op0)))
5967 : : {
5968 : 965 : if (TYPE_PTR_P (type0))
5969 : 68 : result_type = composite_pointer_type (location,
5970 : : type0, type1, op0, op1,
5971 : : CPO_COMPARISON, complain);
5972 : : else
5973 : : result_type = type1;
5974 : :
5975 : 965 : if (char_type_p (TREE_TYPE (orig_op0)))
5976 : : {
5977 : 10 : auto_diagnostic_group d;
5978 : 10 : if (warning_at (location, OPT_Wpointer_compare,
5979 : : "comparison between pointer and zero character "
5980 : : "constant"))
5981 : 10 : inform (location,
5982 : : "did you mean to dereference the pointer?");
5983 : 10 : }
5984 : 965 : warn_for_null_address (location, op1, complain);
5985 : : }
5986 : 1703771 : else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5987 : 27648 : || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5988 : 1676495 : result_type = composite_pointer_type (location,
5989 : : type0, type1, op0, op1,
5990 : : CPO_COMPARISON, complain);
5991 : 27276 : else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5992 : : /* One of the operands must be of nullptr_t type. */
5993 : 74 : result_type = TREE_TYPE (nullptr_node);
5994 : 27202 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5995 : : {
5996 : 58 : result_type = type0;
5997 : 58 : if (complain & tf_error)
5998 : 56 : permerror (location, "ISO C++ forbids comparison between "
5999 : : "pointer and integer");
6000 : : else
6001 : 2 : return error_mark_node;
6002 : : }
6003 : 27144 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6004 : : {
6005 : 26766 : result_type = type1;
6006 : 26766 : if (complain & tf_error)
6007 : 69 : permerror (location, "ISO C++ forbids comparison between "
6008 : : "pointer and integer");
6009 : : else
6010 : 26697 : return error_mark_node;
6011 : : }
6012 : 378 : else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
6013 : : {
6014 : 206 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6015 : : == ptrmemfunc_vbit_in_delta)
6016 : : {
6017 : : tree pfn0, delta0, e1, e2;
6018 : :
6019 : : if (TREE_SIDE_EFFECTS (op0))
6020 : : op0 = cp_save_expr (op0);
6021 : :
6022 : : pfn0 = pfn_from_ptrmemfunc (op0);
6023 : : delta0 = delta_from_ptrmemfunc (op0);
6024 : : {
6025 : : /* If we will warn below about a null-address compare
6026 : : involving the orig_op0 ptrmemfunc, we'd likely also
6027 : : warn about the pfn0's null-address compare, and
6028 : : that would be redundant, so suppress it. */
6029 : : warning_sentinel ws (warn_address);
6030 : : e1 = cp_build_binary_op (location,
6031 : : EQ_EXPR,
6032 : : pfn0,
6033 : : build_zero_cst (TREE_TYPE (pfn0)),
6034 : : complain);
6035 : : }
6036 : : e2 = cp_build_binary_op (location,
6037 : : BIT_AND_EXPR,
6038 : : delta0,
6039 : : integer_one_node,
6040 : : complain);
6041 : :
6042 : : if (complain & tf_warning)
6043 : : maybe_warn_zero_as_null_pointer_constant (op1, input_location);
6044 : :
6045 : : e2 = cp_build_binary_op (location,
6046 : : EQ_EXPR, e2, integer_zero_node,
6047 : : complain);
6048 : : op0 = cp_build_binary_op (location,
6049 : : TRUTH_ANDIF_EXPR, e1, e2,
6050 : : complain);
6051 : : op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
6052 : : }
6053 : : else
6054 : : {
6055 : 206 : op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
6056 : 206 : op1 = cp_convert (TREE_TYPE (op0), op1, complain);
6057 : : }
6058 : 206 : result_type = TREE_TYPE (op0);
6059 : :
6060 : 206 : warn_for_null_address (location, orig_op0, complain);
6061 : : }
6062 : 172 : else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
6063 : 36 : return cp_build_binary_op (location, code, op1, op0, complain);
6064 : 136 : else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
6065 : : {
6066 : 115 : tree type;
6067 : : /* E will be the final comparison. */
6068 : 115 : tree e;
6069 : : /* E1 and E2 are for scratch. */
6070 : 115 : tree e1;
6071 : 115 : tree e2;
6072 : 115 : tree pfn0;
6073 : 115 : tree pfn1;
6074 : 115 : tree delta0;
6075 : 115 : tree delta1;
6076 : :
6077 : 115 : type = composite_pointer_type (location, type0, type1, op0, op1,
6078 : : CPO_COMPARISON, complain);
6079 : :
6080 : 115 : if (!same_type_p (TREE_TYPE (op0), type))
6081 : 6 : op0 = cp_convert_and_check (type, op0, complain);
6082 : 115 : if (!same_type_p (TREE_TYPE (op1), type))
6083 : 9 : op1 = cp_convert_and_check (type, op1, complain);
6084 : :
6085 : 115 : if (op0 == error_mark_node || op1 == error_mark_node)
6086 : : return error_mark_node;
6087 : :
6088 : 112 : if (TREE_SIDE_EFFECTS (op0))
6089 : 53 : op0 = cp_save_expr (op0);
6090 : 112 : if (TREE_SIDE_EFFECTS (op1))
6091 : 3 : op1 = cp_save_expr (op1);
6092 : :
6093 : 112 : pfn0 = pfn_from_ptrmemfunc (op0);
6094 : 112 : pfn0 = cp_fully_fold (pfn0);
6095 : : /* Avoid -Waddress warnings (c++/64877). */
6096 : 112 : if (TREE_CODE (pfn0) == ADDR_EXPR)
6097 : 12 : suppress_warning (pfn0, OPT_Waddress);
6098 : 112 : pfn1 = pfn_from_ptrmemfunc (op1);
6099 : 112 : pfn1 = cp_fully_fold (pfn1);
6100 : 112 : delta0 = delta_from_ptrmemfunc (op0);
6101 : 112 : delta1 = delta_from_ptrmemfunc (op1);
6102 : 112 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6103 : : == ptrmemfunc_vbit_in_delta)
6104 : : {
6105 : : /* We generate:
6106 : :
6107 : : (op0.pfn == op1.pfn
6108 : : && ((op0.delta == op1.delta)
6109 : : || (!op0.pfn && op0.delta & 1 == 0
6110 : : && op1.delta & 1 == 0))
6111 : :
6112 : : The reason for the `!op0.pfn' bit is that a NULL
6113 : : pointer-to-member is any member with a zero PFN and
6114 : : LSB of the DELTA field is 0. */
6115 : :
6116 : : e1 = cp_build_binary_op (location, BIT_AND_EXPR,
6117 : : delta0,
6118 : : integer_one_node,
6119 : : complain);
6120 : : e1 = cp_build_binary_op (location,
6121 : : EQ_EXPR, e1, integer_zero_node,
6122 : : complain);
6123 : : e2 = cp_build_binary_op (location, BIT_AND_EXPR,
6124 : : delta1,
6125 : : integer_one_node,
6126 : : complain);
6127 : : e2 = cp_build_binary_op (location,
6128 : : EQ_EXPR, e2, integer_zero_node,
6129 : : complain);
6130 : : e1 = cp_build_binary_op (location,
6131 : : TRUTH_ANDIF_EXPR, e2, e1,
6132 : : complain);
6133 : : e2 = cp_build_binary_op (location, EQ_EXPR,
6134 : : pfn0,
6135 : : build_zero_cst (TREE_TYPE (pfn0)),
6136 : : complain);
6137 : : e2 = cp_build_binary_op (location,
6138 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6139 : : e1 = cp_build_binary_op (location,
6140 : : EQ_EXPR, delta0, delta1, complain);
6141 : : e1 = cp_build_binary_op (location,
6142 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6143 : : }
6144 : : else
6145 : : {
6146 : : /* We generate:
6147 : :
6148 : : (op0.pfn == op1.pfn
6149 : : && (!op0.pfn || op0.delta == op1.delta))
6150 : :
6151 : : The reason for the `!op0.pfn' bit is that a NULL
6152 : : pointer-to-member is any member with a zero PFN; the
6153 : : DELTA field is unspecified. */
6154 : :
6155 : 112 : e1 = cp_build_binary_op (location,
6156 : : EQ_EXPR, delta0, delta1, complain);
6157 : 112 : e2 = cp_build_binary_op (location,
6158 : : EQ_EXPR,
6159 : : pfn0,
6160 : 112 : build_zero_cst (TREE_TYPE (pfn0)),
6161 : : complain);
6162 : 112 : e1 = cp_build_binary_op (location,
6163 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6164 : : }
6165 : 112 : e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
6166 : 112 : e = cp_build_binary_op (location,
6167 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6168 : 112 : if (code == EQ_EXPR)
6169 : : return e;
6170 : 30 : return cp_build_binary_op (location,
6171 : 30 : EQ_EXPR, e, integer_zero_node, complain);
6172 : : }
6173 : : else
6174 : : {
6175 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
6176 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
6177 : : type1));
6178 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
6179 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
6180 : : type0));
6181 : : }
6182 : :
6183 : : break;
6184 : :
6185 : 241 : case MAX_EXPR:
6186 : 241 : case MIN_EXPR:
6187 : 241 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6188 : 241 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6189 : : shorten = 1;
6190 : 0 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6191 : 0 : result_type = composite_pointer_type (location,
6192 : : type0, type1, op0, op1,
6193 : : CPO_COMPARISON, complain);
6194 : : break;
6195 : :
6196 : 21065513 : case LE_EXPR:
6197 : 21065513 : case GE_EXPR:
6198 : 21065513 : case LT_EXPR:
6199 : 21065513 : case GT_EXPR:
6200 : 21065513 : case SPACESHIP_EXPR:
6201 : 21065513 : if (TREE_CODE (orig_op0) == STRING_CST
6202 : 21065513 : || TREE_CODE (orig_op1) == STRING_CST)
6203 : : {
6204 : 0 : if (complain & tf_warning)
6205 : 0 : warning_at (location, OPT_Waddress,
6206 : : "comparison with string literal results "
6207 : : "in unspecified behavior");
6208 : : }
6209 : 21065513 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6210 : 153 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6211 : 21065617 : && code != SPACESHIP_EXPR)
6212 : : {
6213 : 88 : if (complain & tf_warning_or_error)
6214 : 51 : do_warn_array_compare (location, code,
6215 : : tree_strip_any_location_wrapper (orig_op0),
6216 : : tree_strip_any_location_wrapper (orig_op1));
6217 : 37 : else if (cxx_dialect >= cxx26)
6218 : 1 : return error_mark_node;
6219 : : }
6220 : :
6221 : 21065512 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6222 : : {
6223 : 6602 : vector_compare:
6224 : 6602 : tree intt;
6225 : 6602 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6226 : 6602 : TREE_TYPE (type1))
6227 : 6602 : && !vector_types_compatible_elements_p (type0, type1))
6228 : : {
6229 : 3 : if (complain & tf_error)
6230 : : {
6231 : 3 : auto_diagnostic_group d;
6232 : 3 : error_at (location, "comparing vectors with different "
6233 : : "element types");
6234 : 3 : inform (location, "operand types are %qT and %qT",
6235 : : type0, type1);
6236 : 3 : }
6237 : 3 : return error_mark_node;
6238 : : }
6239 : :
6240 : 6599 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
6241 : 13198 : TYPE_VECTOR_SUBPARTS (type1)))
6242 : : {
6243 : 3 : if (complain & tf_error)
6244 : : {
6245 : 3 : auto_diagnostic_group d;
6246 : 3 : error_at (location, "comparing vectors with different "
6247 : : "number of elements");
6248 : 3 : inform (location, "operand types are %qT and %qT",
6249 : : type0, type1);
6250 : 3 : }
6251 : 3 : return error_mark_node;
6252 : : }
6253 : :
6254 : : /* It's not precisely specified how the usual arithmetic
6255 : : conversions apply to the vector types. Here, we use
6256 : : the unsigned type if one of the operands is signed and
6257 : : the other one is unsigned. */
6258 : 6596 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6259 : : {
6260 : 36 : if (!TYPE_UNSIGNED (type0))
6261 : 36 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6262 : : else
6263 : 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6264 : 36 : warning_at (location, OPT_Wsign_compare, "comparison between "
6265 : : "types %qT and %qT", type0, type1);
6266 : : }
6267 : :
6268 : 6596 : if (resultcode == SPACESHIP_EXPR)
6269 : : {
6270 : 3 : if (complain & tf_error)
6271 : 3 : sorry_at (location, "three-way comparison of vectors");
6272 : 3 : return error_mark_node;
6273 : : }
6274 : :
6275 : : /* Always construct signed integer vector type. */
6276 : 6593 : intt = c_common_type_for_size
6277 : 13186 : (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
6278 : 6593 : if (!intt)
6279 : : {
6280 : 0 : if (complain & tf_error)
6281 : 0 : error_at (location, "could not find an integer type "
6282 : 0 : "of the same size as %qT", TREE_TYPE (type0));
6283 : 0 : return error_mark_node;
6284 : : }
6285 : 6593 : result_type = build_opaque_vector_type (intt,
6286 : 6593 : TYPE_VECTOR_SUBPARTS (type0));
6287 : 6593 : return build_vec_cmp (resultcode, result_type, op0, op1);
6288 : : }
6289 : 21061325 : build_type = boolean_type_node;
6290 : 21061325 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6291 : : || code0 == ENUMERAL_TYPE)
6292 : 19757541 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6293 : : || code1 == ENUMERAL_TYPE))
6294 : : short_compare = 1;
6295 : 1303871 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6296 : 1303639 : result_type = composite_pointer_type (location,
6297 : : type0, type1, op0, op1,
6298 : : CPO_COMPARISON, complain);
6299 : 74 : else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6300 : 160 : || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6301 : 338 : || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6302 : : {
6303 : : /* Core Issue 1512 made this ill-formed. */
6304 : 191 : if (complain & tf_error)
6305 : 188 : error_at (location, "ordered comparison of pointer with "
6306 : : "integer zero (%qT and %qT)", type0, type1);
6307 : 191 : return error_mark_node;
6308 : : }
6309 : 41 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6310 : : {
6311 : 0 : result_type = type0;
6312 : 0 : if (complain & tf_error)
6313 : 0 : permerror (location, "ISO C++ forbids comparison between "
6314 : : "pointer and integer");
6315 : : else
6316 : 0 : return error_mark_node;
6317 : : }
6318 : 41 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6319 : : {
6320 : 24 : result_type = type1;
6321 : 24 : if (complain & tf_error)
6322 : 24 : permerror (location, "ISO C++ forbids comparison between "
6323 : : "pointer and integer");
6324 : : else
6325 : 0 : return error_mark_node;
6326 : : }
6327 : :
6328 : 21061134 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6329 : 1303665 : && !processing_template_decl
6330 : 22147495 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6331 : : {
6332 : 49 : op0 = cp_save_expr (op0);
6333 : 49 : op1 = cp_save_expr (op1);
6334 : :
6335 : 49 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6336 : 49 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6337 : : }
6338 : :
6339 : : break;
6340 : :
6341 : 0 : case UNORDERED_EXPR:
6342 : 0 : case ORDERED_EXPR:
6343 : 0 : case UNLT_EXPR:
6344 : 0 : case UNLE_EXPR:
6345 : 0 : case UNGT_EXPR:
6346 : 0 : case UNGE_EXPR:
6347 : 0 : case UNEQ_EXPR:
6348 : 0 : build_type = integer_type_node;
6349 : 0 : if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6350 : : {
6351 : 0 : if (complain & tf_error)
6352 : 0 : error ("unordered comparison on non-floating-point argument");
6353 : 0 : return error_mark_node;
6354 : : }
6355 : : common = 1;
6356 : : break;
6357 : :
6358 : : default:
6359 : : break;
6360 : : }
6361 : :
6362 : 118666845 : if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6363 : : || code0 == ENUMERAL_TYPE)
6364 : 98402957 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6365 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6366 : : arithmetic_types_p = 1;
6367 : : else
6368 : : {
6369 : 20498418 : arithmetic_types_p = 0;
6370 : : /* Vector arithmetic is only allowed when both sides are vectors. */
6371 : 20498418 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6372 : : {
6373 : 50596 : if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6374 : 50596 : || !vector_types_compatible_elements_p (type0, type1))
6375 : : {
6376 : 18 : if (complain & tf_error)
6377 : : {
6378 : : /* "location" already embeds the locations of the
6379 : : operands, so we don't need to add them separately
6380 : : to richloc. */
6381 : 18 : rich_location richloc (line_table, location);
6382 : 18 : binary_op_error (&richloc, code, type0, type1);
6383 : 18 : }
6384 : 18 : return error_mark_node;
6385 : : }
6386 : : arithmetic_types_p = 1;
6387 : : }
6388 : : }
6389 : : /* Determine the RESULT_TYPE, if it is not already known. */
6390 : 118666827 : if (!result_type
6391 : 118666827 : && arithmetic_types_p
6392 : 94293036 : && (shorten || common || short_compare))
6393 : : {
6394 : 94292959 : result_type = cp_common_type (type0, type1);
6395 : 94292959 : if (result_type == error_mark_node)
6396 : : {
6397 : 0 : tree t1 = type0;
6398 : 0 : tree t2 = type1;
6399 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6400 : 0 : t1 = TREE_TYPE (t1);
6401 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6402 : 0 : t2 = TREE_TYPE (t2);
6403 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6404 : : && TREE_CODE (t2) == REAL_TYPE
6405 : : && (extended_float_type_p (t1)
6406 : : || extended_float_type_p (t2))
6407 : : && cp_compare_floating_point_conversion_ranks
6408 : : (t1, t2) == 3);
6409 : 0 : if (complain & tf_error)
6410 : : {
6411 : 0 : rich_location richloc (line_table, location);
6412 : 0 : binary_op_error (&richloc, code, type0, type1);
6413 : 0 : }
6414 : 0 : return error_mark_node;
6415 : : }
6416 : 94292959 : if (complain & tf_warning)
6417 : 89356802 : do_warn_double_promotion (result_type, type0, type1,
6418 : : "implicit conversion from %qH to %qI "
6419 : : "to match other operand of binary "
6420 : : "expression", location);
6421 : 94292959 : if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6422 : 94292959 : TREE_TYPE (orig_op1), complain))
6423 : 6 : return error_mark_node;
6424 : : }
6425 : 118666821 : if (may_need_excess_precision
6426 : 88829956 : && (orig_type0 != type0 || orig_type1 != type1)
6427 : 84165 : && build_type == NULL_TREE
6428 : 84165 : && result_type)
6429 : : {
6430 : 63378 : gcc_assert (common);
6431 : 63378 : semantic_result_type = cp_common_type (orig_type0, orig_type1);
6432 : 63378 : if (semantic_result_type == error_mark_node)
6433 : : {
6434 : 0 : tree t1 = orig_type0;
6435 : 0 : tree t2 = orig_type1;
6436 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6437 : 0 : t1 = TREE_TYPE (t1);
6438 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6439 : 0 : t2 = TREE_TYPE (t2);
6440 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6441 : : && TREE_CODE (t2) == REAL_TYPE
6442 : : && (extended_float_type_p (t1)
6443 : : || extended_float_type_p (t2))
6444 : : && cp_compare_floating_point_conversion_ranks
6445 : : (t1, t2) == 3);
6446 : 0 : if (complain & tf_error)
6447 : : {
6448 : 0 : rich_location richloc (line_table, location);
6449 : 0 : binary_op_error (&richloc, code, type0, type1);
6450 : 0 : }
6451 : 0 : return error_mark_node;
6452 : : }
6453 : : }
6454 : :
6455 : 118666821 : if (code == SPACESHIP_EXPR)
6456 : : {
6457 : 208619 : iloc_sentinel s (location);
6458 : :
6459 : 208619 : tree orig_type0 = TREE_TYPE (orig_op0);
6460 : 208619 : tree_code orig_code0 = TREE_CODE (orig_type0);
6461 : 208619 : tree orig_type1 = TREE_TYPE (orig_op1);
6462 : 208619 : tree_code orig_code1 = TREE_CODE (orig_type1);
6463 : 208619 : if (!result_type || result_type == error_mark_node)
6464 : : /* Nope. */
6465 : : result_type = NULL_TREE;
6466 : 208598 : else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6467 : : /* "If one of the operands is of type bool and the other is not, the
6468 : : program is ill-formed." */
6469 : : result_type = NULL_TREE;
6470 : 208595 : else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6471 : 16 : && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6472 : : /* We only do array/function-to-pointer conversion if "at least one of
6473 : : the operands is of pointer type". */
6474 : : result_type = NULL_TREE;
6475 : 208579 : else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6476 : : /* <=> no longer supports equality relations. */
6477 : : result_type = NULL_TREE;
6478 : 208576 : else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6479 : 208612 : && !(same_type_ignoring_top_level_qualifiers_p
6480 : 36 : (orig_type0, orig_type1)))
6481 : : /* "If both operands have arithmetic types, or one operand has integral
6482 : : type and the other operand has unscoped enumeration type, the usual
6483 : : arithmetic conversions are applied to the operands." So we don't do
6484 : : arithmetic conversions if the operands both have enumeral type. */
6485 : : result_type = NULL_TREE;
6486 : 208561 : else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6487 : 208555 : || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6488 : : /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6489 : : [where one is of enumeration type and the other is of a different
6490 : : enumeration type or a floating-point type] are ill-formed. */
6491 : : result_type = NULL_TREE;
6492 : :
6493 : 208549 : if (result_type)
6494 : : {
6495 : 208549 : build_type = spaceship_type (result_type, complain);
6496 : 208549 : if (build_type == error_mark_node)
6497 : : return error_mark_node;
6498 : : }
6499 : :
6500 : 208604 : if (result_type && arithmetic_types_p)
6501 : : {
6502 : : /* If a narrowing conversion is required, other than from an integral
6503 : : type to a floating point type, the program is ill-formed. */
6504 : 82834 : bool ok = true;
6505 : 82834 : if (TREE_CODE (result_type) == REAL_TYPE
6506 : 673 : && CP_INTEGRAL_TYPE_P (orig_type0))
6507 : : /* OK */;
6508 : 82823 : else if (!check_narrowing (result_type, orig_op0, complain))
6509 : 82834 : ok = false;
6510 : 82834 : if (TREE_CODE (result_type) == REAL_TYPE
6511 : 673 : && CP_INTEGRAL_TYPE_P (orig_type1))
6512 : : /* OK */;
6513 : 82823 : else if (!check_narrowing (result_type, orig_op1, complain))
6514 : : ok = false;
6515 : 82834 : if (!ok && !(complain & tf_error))
6516 : 4 : return error_mark_node;
6517 : : }
6518 : 208619 : }
6519 : :
6520 : 118666802 : if (!result_type)
6521 : : {
6522 : 477 : if (complain & tf_error)
6523 : : {
6524 : 206 : binary_op_rich_location richloc (location,
6525 : 206 : orig_op0, orig_op1, true);
6526 : 206 : error_at (&richloc,
6527 : : "invalid operands of types %qT and %qT to binary %qO",
6528 : 206 : TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6529 : 206 : }
6530 : 477 : return error_mark_node;
6531 : : }
6532 : :
6533 : : /* If we're in a template, the only thing we need to know is the
6534 : : RESULT_TYPE. */
6535 : 118666325 : if (processing_template_decl)
6536 : : {
6537 : : /* Since the middle-end checks the type when doing a build2, we
6538 : : need to build the tree in pieces. This built tree will never
6539 : : get out of the front-end as we replace it when instantiating
6540 : : the template. */
6541 : 52658677 : tree tmp = build2 (resultcode,
6542 : : build_type ? build_type : result_type,
6543 : : NULL_TREE, op1);
6544 : 33072233 : TREE_OPERAND (tmp, 0) = op0;
6545 : 33072233 : if (semantic_result_type)
6546 : 0 : tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6547 : 33072233 : return tmp;
6548 : : }
6549 : :
6550 : : /* Remember the original type; RESULT_TYPE might be changed later on
6551 : : by shorten_binary_op. */
6552 : 85594092 : tree orig_type = result_type;
6553 : :
6554 : 85594092 : if (arithmetic_types_p)
6555 : : {
6556 : 74238714 : bool first_complex = (code0 == COMPLEX_TYPE);
6557 : 74238714 : bool second_complex = (code1 == COMPLEX_TYPE);
6558 : 74238714 : int none_complex = (!first_complex && !second_complex);
6559 : :
6560 : : /* Adapted from patch for c/24581. */
6561 : 74238714 : if (first_complex != second_complex
6562 : 165253 : && (code == PLUS_EXPR
6563 : : || code == MINUS_EXPR
6564 : 165253 : || code == MULT_EXPR
6565 : 43133 : || (code == TRUNC_DIV_EXPR && first_complex))
6566 : 151746 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6567 : 74390299 : && flag_signed_zeros)
6568 : : {
6569 : : /* An operation on mixed real/complex operands must be
6570 : : handled specially, but the language-independent code can
6571 : : more easily optimize the plain complex arithmetic if
6572 : : -fno-signed-zeros. */
6573 : 151585 : tree real_type = TREE_TYPE (result_type);
6574 : 151585 : tree real, imag;
6575 : 151585 : if (first_complex)
6576 : : {
6577 : 118537 : if (TREE_TYPE (op0) != result_type)
6578 : 6 : op0 = cp_convert_and_check (result_type, op0, complain);
6579 : 118537 : if (TREE_TYPE (op1) != real_type)
6580 : 26 : op1 = cp_convert_and_check (real_type, op1, complain);
6581 : : }
6582 : : else
6583 : : {
6584 : 33048 : if (TREE_TYPE (op0) != real_type)
6585 : 31 : op0 = cp_convert_and_check (real_type, op0, complain);
6586 : 33048 : if (TREE_TYPE (op1) != result_type)
6587 : 32 : op1 = cp_convert_and_check (result_type, op1, complain);
6588 : : }
6589 : 151585 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6590 : 0 : return error_mark_node;
6591 : 151585 : if (first_complex)
6592 : : {
6593 : 118537 : op0 = cp_save_expr (op0);
6594 : 118537 : real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6595 : 118537 : imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6596 : 118537 : switch (code)
6597 : : {
6598 : 59245 : case MULT_EXPR:
6599 : 59245 : case TRUNC_DIV_EXPR:
6600 : 59245 : op1 = cp_save_expr (op1);
6601 : 59245 : imag = build2 (resultcode, real_type, imag, op1);
6602 : : /* Fall through. */
6603 : 118537 : case PLUS_EXPR:
6604 : 118537 : case MINUS_EXPR:
6605 : 118537 : real = build2 (resultcode, real_type, real, op1);
6606 : 118537 : break;
6607 : 0 : default:
6608 : 0 : gcc_unreachable();
6609 : : }
6610 : : }
6611 : : else
6612 : : {
6613 : 33048 : op1 = cp_save_expr (op1);
6614 : 33048 : real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6615 : 33048 : imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6616 : 33048 : switch (code)
6617 : : {
6618 : 638 : case MULT_EXPR:
6619 : 638 : op0 = cp_save_expr (op0);
6620 : 638 : imag = build2 (resultcode, real_type, op0, imag);
6621 : : /* Fall through. */
6622 : 17301 : case PLUS_EXPR:
6623 : 17301 : real = build2 (resultcode, real_type, op0, real);
6624 : 17301 : break;
6625 : 15747 : case MINUS_EXPR:
6626 : 15747 : real = build2 (resultcode, real_type, op0, real);
6627 : 15747 : imag = build1 (NEGATE_EXPR, real_type, imag);
6628 : 15747 : break;
6629 : 0 : default:
6630 : 0 : gcc_unreachable();
6631 : : }
6632 : : }
6633 : 151585 : result = build2 (COMPLEX_EXPR, result_type, real, imag);
6634 : 151585 : if (semantic_result_type)
6635 : 24 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6636 : : result);
6637 : 151585 : return result;
6638 : : }
6639 : :
6640 : : /* For certain operations (which identify themselves by shorten != 0)
6641 : : if both args were extended from the same smaller type,
6642 : : do the arithmetic in that type and then extend.
6643 : :
6644 : : shorten !=0 and !=1 indicates a bitwise operation.
6645 : : For them, this optimization is safe only if
6646 : : both args are zero-extended or both are sign-extended.
6647 : : Otherwise, we might change the result.
6648 : : E.g., (short)-1 | (unsigned short)-1 is (int)-1
6649 : : but calculated in (unsigned short) it would be (unsigned short)-1. */
6650 : :
6651 : 74087129 : if (shorten && none_complex)
6652 : : {
6653 : 3903045 : final_type = result_type;
6654 : 3903045 : result_type = shorten_binary_op (result_type, op0, op1,
6655 : : shorten == -1);
6656 : : }
6657 : :
6658 : : /* Shifts can be shortened if shifting right. */
6659 : :
6660 : 74087129 : if (short_shift)
6661 : : {
6662 : 752261 : int unsigned_arg;
6663 : 752261 : tree arg0 = get_narrower (op0, &unsigned_arg);
6664 : : /* We're not really warning here but when we set short_shift we
6665 : : used fold_for_warn to fold the operand. */
6666 : 752261 : tree const_op1 = fold_for_warn (op1);
6667 : :
6668 : 752261 : final_type = result_type;
6669 : :
6670 : 752261 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
6671 : 690091 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6672 : :
6673 : 752261 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6674 : 3510 : && tree_int_cst_sgn (const_op1) > 0
6675 : : /* We can shorten only if the shift count is less than the
6676 : : number of bits in the smaller type size. */
6677 : 3510 : && compare_tree_int (const_op1,
6678 : 3510 : TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6679 : : /* We cannot drop an unsigned shift after sign-extension. */
6680 : 755761 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6681 : : {
6682 : : /* Do an unsigned shift if the operand was zero-extended. */
6683 : 3493 : result_type
6684 : 3493 : = c_common_signed_or_unsigned_type (unsigned_arg,
6685 : 3493 : TREE_TYPE (arg0));
6686 : : /* Convert value-to-be-shifted to that type. */
6687 : 3493 : if (TREE_TYPE (op0) != result_type)
6688 : 3493 : op0 = convert (result_type, op0);
6689 : : converted = 1;
6690 : : }
6691 : : }
6692 : :
6693 : : /* Comparison operations are shortened too but differently.
6694 : : They identify themselves by setting short_compare = 1. */
6695 : :
6696 : 74087129 : if (short_compare)
6697 : : {
6698 : : /* We call shorten_compare only for diagnostics. */
6699 : 24147431 : tree xop0 = fold_simple (op0);
6700 : 24147431 : tree xop1 = fold_simple (op1);
6701 : 24147431 : tree xresult_type = result_type;
6702 : 24147431 : enum tree_code xresultcode = resultcode;
6703 : 24147431 : shorten_compare (location, &xop0, &xop1, &xresult_type,
6704 : : &xresultcode);
6705 : : }
6706 : :
6707 : 49939611 : if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6708 : 24147620 : && warn_sign_compare
6709 : : /* Do not warn until the template is instantiated; we cannot
6710 : : bound the ranges of the arguments until that point. */
6711 : 319831 : && !processing_template_decl
6712 : 319831 : && (complain & tf_warning)
6713 : 313002 : && c_inhibit_evaluation_warnings == 0
6714 : : /* Even unsigned enum types promote to signed int. We don't
6715 : : want to issue -Wsign-compare warnings for this case. */
6716 : 296930 : && !enum_cast_to_int (orig_op0)
6717 : 74384059 : && !enum_cast_to_int (orig_op1))
6718 : : {
6719 : 296923 : warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6720 : : result_type, resultcode);
6721 : : }
6722 : : }
6723 : :
6724 : : /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6725 : : Then the expression will be built.
6726 : : It will be given type FINAL_TYPE if that is nonzero;
6727 : : otherwise, it will be given type RESULT_TYPE. */
6728 : 85442507 : if (! converted)
6729 : : {
6730 : 82072658 : warning_sentinel w (warn_sign_conversion, short_compare);
6731 : 82072658 : if (!same_type_p (TREE_TYPE (op0), result_type))
6732 : 3466882 : op0 = cp_convert_and_check (result_type, op0, complain);
6733 : 82072658 : if (!same_type_p (TREE_TYPE (op1), result_type))
6734 : 14673386 : op1 = cp_convert_and_check (result_type, op1, complain);
6735 : :
6736 : 82072658 : if (op0 == error_mark_node || op1 == error_mark_node)
6737 : 65 : return error_mark_node;
6738 : 82072658 : }
6739 : :
6740 : 85442442 : if (build_type == NULL_TREE)
6741 : 57200393 : build_type = result_type;
6742 : :
6743 : 85442442 : if (doing_shift
6744 : 3369267 : && flag_strong_eval_order == 2
6745 : 3282254 : && TREE_SIDE_EFFECTS (op1)
6746 : 85468596 : && !processing_template_decl)
6747 : : {
6748 : : /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6749 : : op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6750 : 26154 : op0 = cp_save_expr (op0);
6751 : 26154 : instrument_expr = op0;
6752 : : }
6753 : :
6754 : 85442442 : if (sanitize_flags_p ((SANITIZE_SHIFT
6755 : : | SANITIZE_DIVIDE
6756 : : | SANITIZE_FLOAT_DIVIDE
6757 : : | SANITIZE_SI_OVERFLOW))
6758 : 12594 : && current_function_decl != NULL_TREE
6759 : 10419 : && !processing_template_decl
6760 : 85452861 : && (doing_div_or_mod || doing_shift))
6761 : : {
6762 : : /* OP0 and/or OP1 might have side-effects. */
6763 : 881 : op0 = cp_save_expr (op0);
6764 : 881 : op1 = cp_save_expr (op1);
6765 : 881 : op0 = fold_non_dependent_expr (op0, complain);
6766 : 881 : op1 = fold_non_dependent_expr (op1, complain);
6767 : 881 : tree instrument_expr1 = NULL_TREE;
6768 : 881 : if (doing_div_or_mod
6769 : 881 : && sanitize_flags_p (SANITIZE_DIVIDE
6770 : : | SANITIZE_FLOAT_DIVIDE
6771 : : | SANITIZE_SI_OVERFLOW))
6772 : : {
6773 : : /* For diagnostics we want to use the promoted types without
6774 : : shorten_binary_op. So convert the arguments to the
6775 : : original result_type. */
6776 : 453 : tree cop0 = op0;
6777 : 453 : tree cop1 = op1;
6778 : 453 : if (TREE_TYPE (cop0) != orig_type)
6779 : 6 : cop0 = cp_convert (orig_type, op0, complain);
6780 : 453 : if (TREE_TYPE (cop1) != orig_type)
6781 : 32 : cop1 = cp_convert (orig_type, op1, complain);
6782 : 453 : instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6783 : : }
6784 : 428 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6785 : 347 : instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6786 : 881 : if (instrument_expr != NULL)
6787 : 18 : instrument_expr = add_stmt_to_compound (instrument_expr,
6788 : : instrument_expr1);
6789 : : else
6790 : 863 : instrument_expr = instrument_expr1;
6791 : : }
6792 : :
6793 : 85442442 : result = build2_loc (location, resultcode, build_type, op0, op1);
6794 : 85442442 : if (final_type != 0)
6795 : 4655306 : result = cp_convert (final_type, result, complain);
6796 : :
6797 : 85442442 : if (instrument_expr != NULL)
6798 : 26656 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6799 : : instrument_expr, result);
6800 : :
6801 : 85442442 : if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6802 : 180846 : result = get_target_expr (result, complain);
6803 : :
6804 : 85442442 : if (semantic_result_type)
6805 : 63354 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
6806 : :
6807 : 85442442 : if (!c_inhibit_evaluation_warnings)
6808 : : {
6809 : 76766302 : if (!processing_template_decl)
6810 : : {
6811 : 76766302 : op0 = cp_fully_fold (op0);
6812 : : /* Only consider the second argument if the first isn't overflowed. */
6813 : 76766302 : if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6814 : : return result;
6815 : 20120118 : op1 = cp_fully_fold (op1);
6816 : 20120118 : if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6817 : : return result;
6818 : : }
6819 : 0 : else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6820 : 0 : || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6821 : : return result;
6822 : :
6823 : 14767565 : tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6824 : 14767565 : if (TREE_OVERFLOW_P (result_ovl))
6825 : 190 : overflow_warning (location, result_ovl);
6826 : : }
6827 : :
6828 : : return result;
6829 : : }
6830 : :
6831 : : /* Build a VEC_PERM_EXPR.
6832 : : This is a simple wrapper for c_build_vec_perm_expr. */
6833 : : tree
6834 : 14734 : build_x_vec_perm_expr (location_t loc,
6835 : : tree arg0, tree arg1, tree arg2,
6836 : : tsubst_flags_t complain)
6837 : : {
6838 : 14734 : tree orig_arg0 = arg0;
6839 : 14734 : tree orig_arg1 = arg1;
6840 : 14734 : tree orig_arg2 = arg2;
6841 : 14734 : if (processing_template_decl)
6842 : : {
6843 : 19 : if (type_dependent_expression_p (arg0)
6844 : 13 : || type_dependent_expression_p (arg1)
6845 : 32 : || type_dependent_expression_p (arg2))
6846 : 6 : return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6847 : : }
6848 : 14728 : tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6849 : 14728 : if (processing_template_decl && exp != error_mark_node)
6850 : 13 : return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6851 : 13 : orig_arg1, orig_arg2);
6852 : : return exp;
6853 : : }
6854 : :
6855 : : /* Build a VEC_PERM_EXPR.
6856 : : This is a simple wrapper for c_build_shufflevector. */
6857 : : tree
6858 : 34455 : build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6859 : : tsubst_flags_t complain)
6860 : : {
6861 : 34455 : tree arg0 = (*args)[0];
6862 : 34455 : tree arg1 = (*args)[1];
6863 : 34455 : if (processing_template_decl)
6864 : : {
6865 : 43 : for (unsigned i = 0; i < args->length (); ++i)
6866 : 40 : if (i <= 1
6867 : 40 : ? type_dependent_expression_p ((*args)[i])
6868 : 9 : : instantiation_dependent_expression_p ((*args)[i]))
6869 : : {
6870 : 22 : tree exp = build_min_nt_call_vec (NULL, args);
6871 : 22 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6872 : 22 : return exp;
6873 : : }
6874 : : }
6875 : 34433 : auto_vec<tree, 16> mask;
6876 : 467509 : for (unsigned i = 2; i < args->length (); ++i)
6877 : : {
6878 : 433076 : tree idx = fold_non_dependent_expr ((*args)[i], complain);
6879 : 433076 : mask.safe_push (idx);
6880 : : }
6881 : 34433 : tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6882 : 34433 : if (processing_template_decl && exp != error_mark_node)
6883 : : {
6884 : 3 : exp = build_min_non_dep_call_vec (exp, NULL, args);
6885 : 3 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6886 : : }
6887 : 34433 : return exp;
6888 : 34433 : }
6889 : :
6890 : : /* Return a tree for the sum or difference (RESULTCODE says which)
6891 : : of pointer PTROP and integer INTOP. */
6892 : :
6893 : : static tree
6894 : 4341404 : cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6895 : : tree intop, tsubst_flags_t complain)
6896 : : {
6897 : 4341404 : tree res_type = TREE_TYPE (ptrop);
6898 : :
6899 : : /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6900 : : in certain circumstance (when it's valid to do so). So we need
6901 : : to make sure it's complete. We don't need to check here, if we
6902 : : can actually complete it at all, as those checks will be done in
6903 : : pointer_int_sum() anyway. */
6904 : 4341404 : complete_type (TREE_TYPE (res_type));
6905 : :
6906 : 4341404 : return pointer_int_sum (loc, resultcode, ptrop,
6907 : 4341404 : intop, complain & tf_warning_or_error);
6908 : : }
6909 : :
6910 : : /* Return a tree for the difference of pointers OP0 and OP1.
6911 : : The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6912 : : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6913 : :
6914 : : static tree
6915 : 1296319 : pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6916 : : tsubst_flags_t complain, tree *instrument_expr)
6917 : : {
6918 : 1296319 : tree result, inttype;
6919 : 1296319 : tree restype = ptrdiff_type_node;
6920 : 1296319 : tree target_type = TREE_TYPE (ptrtype);
6921 : :
6922 : 1296319 : if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6923 : 12 : return error_mark_node;
6924 : :
6925 : 1296307 : if (VOID_TYPE_P (target_type))
6926 : : {
6927 : 0 : if (complain & tf_error)
6928 : 0 : permerror (loc, "ISO C++ forbids using pointer of "
6929 : : "type %<void *%> in subtraction");
6930 : : else
6931 : 0 : return error_mark_node;
6932 : : }
6933 : 1296307 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
6934 : : {
6935 : 15 : if (complain & tf_error)
6936 : 3 : permerror (loc, "ISO C++ forbids using pointer to "
6937 : : "a function in subtraction");
6938 : : else
6939 : 12 : return error_mark_node;
6940 : : }
6941 : 1296295 : if (TREE_CODE (target_type) == METHOD_TYPE)
6942 : : {
6943 : 0 : if (complain & tf_error)
6944 : 0 : permerror (loc, "ISO C++ forbids using pointer to "
6945 : : "a method in subtraction");
6946 : : else
6947 : 0 : return error_mark_node;
6948 : : }
6949 : 2592590 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6950 : 1296295 : TREE_TYPE (TREE_TYPE (op0)),
6951 : : !(complain & tf_error))
6952 : 2592590 : || !verify_type_context (loc, TCTX_POINTER_ARITH,
6953 : 1296295 : TREE_TYPE (TREE_TYPE (op1)),
6954 : : !(complain & tf_error)))
6955 : 0 : return error_mark_node;
6956 : :
6957 : : /* Determine integer type result of the subtraction. This will usually
6958 : : be the same as the result type (ptrdiff_t), but may need to be a wider
6959 : : type if pointers for the address space are wider than ptrdiff_t. */
6960 : 1296295 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6961 : 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6962 : : else
6963 : : inttype = restype;
6964 : :
6965 : 1296295 : if (!processing_template_decl
6966 : 1296295 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
6967 : : {
6968 : 84 : op0 = save_expr (op0);
6969 : 84 : op1 = save_expr (op1);
6970 : :
6971 : 84 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
6972 : 84 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6973 : : }
6974 : :
6975 : : /* First do the subtraction, then build the divide operator
6976 : : and only convert at the very end.
6977 : : Do not do default conversions in case restype is a short type. */
6978 : :
6979 : : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6980 : : pointers. If some platform cannot provide that, or has a larger
6981 : : ptrdiff_type to support differences larger than half the address
6982 : : space, cast the pointers to some larger integer type and do the
6983 : : computations in that type. */
6984 : 1296295 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6985 : 0 : op0 = cp_build_binary_op (loc,
6986 : : MINUS_EXPR,
6987 : : cp_convert (inttype, op0, complain),
6988 : : cp_convert (inttype, op1, complain),
6989 : : complain);
6990 : : else
6991 : 1296295 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
6992 : :
6993 : : /* This generates an error if op1 is a pointer to an incomplete type. */
6994 : 1296295 : if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
6995 : : {
6996 : 0 : if (complain & tf_error)
6997 : 0 : error_at (loc, "invalid use of a pointer to an incomplete type in "
6998 : : "pointer arithmetic");
6999 : : else
7000 : 0 : return error_mark_node;
7001 : : }
7002 : :
7003 : 1296295 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
7004 : : {
7005 : 15 : if (complain & tf_error)
7006 : 15 : error_at (loc, "arithmetic on pointer to an empty aggregate");
7007 : : else
7008 : 0 : return error_mark_node;
7009 : : }
7010 : :
7011 : 2592587 : op1 = (TYPE_PTROB_P (ptrtype)
7012 : 2592587 : ? size_in_bytes_loc (loc, target_type)
7013 : : : integer_one_node);
7014 : :
7015 : : /* Do the division. */
7016 : :
7017 : 1296295 : result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
7018 : : cp_convert (inttype, op1, complain));
7019 : 1296295 : return cp_convert (restype, result, complain);
7020 : : }
7021 : :
7022 : : /* Construct and perhaps optimize a tree representation
7023 : : for a unary operation. CODE, a tree_code, specifies the operation
7024 : : and XARG is the operand. */
7025 : :
7026 : : tree
7027 : 56522186 : build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
7028 : : tree lookups, tsubst_flags_t complain)
7029 : : {
7030 : 56522186 : tree orig_expr = xarg;
7031 : 56522186 : tree exp;
7032 : 56522186 : int ptrmem = 0;
7033 : 56522186 : tree overload = NULL_TREE;
7034 : :
7035 : 56522186 : if (processing_template_decl)
7036 : : {
7037 : 39529663 : if (type_dependent_expression_p (xarg))
7038 : : {
7039 : 26255984 : tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
7040 : 26255984 : TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
7041 : 26255984 : return e;
7042 : : }
7043 : : }
7044 : :
7045 : 30266202 : exp = NULL_TREE;
7046 : :
7047 : : /* [expr.unary.op] says:
7048 : :
7049 : : The address of an object of incomplete type can be taken.
7050 : :
7051 : : (And is just the ordinary address operator, not an overloaded
7052 : : "operator &".) However, if the type is a template
7053 : : specialization, we must complete the type at this point so that
7054 : : an overloaded "operator &" will be available if required. */
7055 : 30266202 : if (code == ADDR_EXPR
7056 : 3412666 : && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
7057 : 33678466 : && ((CLASS_TYPE_P (TREE_TYPE (xarg))
7058 : 1608181 : && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
7059 : 3409836 : || (TREE_CODE (xarg) == OFFSET_REF)))
7060 : : /* Don't look for a function. */;
7061 : : else
7062 : 30197733 : exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
7063 : : NULL_TREE, lookups, &overload, complain);
7064 : :
7065 : 30266202 : if (!exp && code == ADDR_EXPR)
7066 : : {
7067 : 3412391 : if (is_overloaded_fn (xarg))
7068 : : {
7069 : 368900 : tree fn = get_first_fn (xarg);
7070 : 737800 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
7071 : : {
7072 : 12 : if (complain & tf_error)
7073 : 21 : error_at (loc, DECL_CONSTRUCTOR_P (fn)
7074 : : ? G_("taking address of constructor %qD")
7075 : : : G_("taking address of destructor %qD"),
7076 : : fn);
7077 : 12 : return error_mark_node;
7078 : : }
7079 : : }
7080 : :
7081 : : /* A pointer to member-function can be formed only by saying
7082 : : &X::mf. */
7083 : 3412373 : if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
7084 : 3474686 : && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
7085 : : {
7086 : 9 : if (TREE_CODE (xarg) != OFFSET_REF
7087 : 9 : || !TYPE_P (TREE_OPERAND (xarg, 0)))
7088 : : {
7089 : 9 : if (complain & tf_error)
7090 : : {
7091 : 9 : auto_diagnostic_group d;
7092 : 9 : error_at (loc, "invalid use of %qE to form a "
7093 : : "pointer-to-member-function", xarg.get_value ());
7094 : 9 : if (TREE_CODE (xarg) != OFFSET_REF)
7095 : 6 : inform (loc, " a qualified-id is required");
7096 : 9 : }
7097 : 9 : return error_mark_node;
7098 : : }
7099 : : else
7100 : : {
7101 : 0 : if (complain & tf_error)
7102 : 0 : error_at (loc, "parentheses around %qE cannot be used to "
7103 : : "form a pointer-to-member-function",
7104 : : xarg.get_value ());
7105 : : else
7106 : 0 : return error_mark_node;
7107 : 0 : PTRMEM_OK_P (xarg) = 1;
7108 : : }
7109 : : }
7110 : :
7111 : 3412370 : if (TREE_CODE (xarg) == OFFSET_REF)
7112 : : {
7113 : 66026 : ptrmem = PTRMEM_OK_P (xarg);
7114 : :
7115 : 0 : if (!ptrmem && !flag_ms_extensions
7116 : 66026 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
7117 : : {
7118 : : /* A single non-static member, make sure we don't allow a
7119 : : pointer-to-member. */
7120 : 0 : xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
7121 : 0 : TREE_OPERAND (xarg, 0),
7122 : 0 : ovl_make (TREE_OPERAND (xarg, 1)));
7123 : 0 : PTRMEM_OK_P (xarg) = ptrmem;
7124 : : }
7125 : : }
7126 : :
7127 : 6824740 : exp = cp_build_addr_expr_strict (xarg, complain);
7128 : :
7129 : 3412370 : if (TREE_CODE (exp) == PTRMEM_CST)
7130 : 65109 : PTRMEM_CST_LOCATION (exp) = loc;
7131 : : else
7132 : 3347261 : protected_set_expr_location (exp, loc);
7133 : : }
7134 : :
7135 : 30266181 : if (processing_template_decl && exp != error_mark_node)
7136 : : {
7137 : 13273531 : if (overload != NULL_TREE)
7138 : 163727 : return (build_min_non_dep_op_overload
7139 : 163727 : (code, exp, overload, orig_expr, integer_zero_node));
7140 : :
7141 : 13109804 : exp = build_min_non_dep (code, exp, orig_expr,
7142 : : /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
7143 : : }
7144 : 30102454 : if (TREE_CODE (exp) == ADDR_EXPR)
7145 : 2626115 : PTRMEM_OK_P (exp) = ptrmem;
7146 : : return exp;
7147 : : }
7148 : :
7149 : : /* Construct and perhaps optimize a tree representation
7150 : : for __builtin_addressof operation. ARG specifies the operand. */
7151 : :
7152 : : tree
7153 : 476078 : cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7154 : : {
7155 : 476078 : tree orig_expr = arg;
7156 : :
7157 : 476078 : if (processing_template_decl)
7158 : : {
7159 : 182722 : if (type_dependent_expression_p (arg))
7160 : 182722 : return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7161 : : }
7162 : :
7163 : 586712 : tree exp = cp_build_addr_expr_strict (arg, complain);
7164 : :
7165 : 293356 : if (processing_template_decl && exp != error_mark_node)
7166 : 0 : exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
7167 : : return exp;
7168 : : }
7169 : :
7170 : : /* Like c_common_truthvalue_conversion, but handle pointer-to-member
7171 : : constants, where a null value is represented by an INTEGER_CST of
7172 : : -1. */
7173 : :
7174 : : tree
7175 : 7234224 : cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7176 : : {
7177 : 7234224 : tree type = TREE_TYPE (expr);
7178 : 7234224 : location_t loc = cp_expr_loc_or_input_loc (expr);
7179 : 6129814 : if (TYPE_PTR_OR_PTRMEM_P (type)
7180 : : /* Avoid ICE on invalid use of non-static member function. */
7181 : 13363752 : || TREE_CODE (expr) == FUNCTION_DECL)
7182 : 1104696 : return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
7183 : : else
7184 : 6129528 : return c_common_truthvalue_conversion (loc, expr);
7185 : : }
7186 : :
7187 : : /* Returns EXPR contextually converted to bool. */
7188 : :
7189 : : tree
7190 : 46722762 : contextual_conv_bool (tree expr, tsubst_flags_t complain)
7191 : : {
7192 : 46722762 : return perform_implicit_conversion_flags (boolean_type_node, expr,
7193 : 46722762 : complain, LOOKUP_NORMAL);
7194 : : }
7195 : :
7196 : : /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7197 : : is a low-level function; most callers should use maybe_convert_cond. */
7198 : :
7199 : : tree
7200 : 41729036 : condition_conversion (tree expr)
7201 : : {
7202 : 41729036 : tree t = contextual_conv_bool (expr, tf_warning_or_error);
7203 : 41729036 : if (!processing_template_decl)
7204 : 21209291 : t = fold_build_cleanup_point_expr (boolean_type_node, t);
7205 : 41729036 : return t;
7206 : : }
7207 : :
7208 : : /* Returns the address of T. This function will fold away
7209 : : ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7210 : : most places should use cp_build_addr_expr instead. */
7211 : :
7212 : : tree
7213 : 278464875 : build_address (tree t)
7214 : : {
7215 : 278464875 : if (error_operand_p (t) || !cxx_mark_addressable (t))
7216 : 21 : return error_mark_node;
7217 : 278464854 : gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7218 : : || processing_template_decl);
7219 : 278464854 : t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7220 : 278464854 : if (TREE_CODE (t) != ADDR_EXPR)
7221 : 48687277 : t = rvalue (t);
7222 : : return t;
7223 : : }
7224 : :
7225 : : /* Return a NOP_EXPR converting EXPR to TYPE. */
7226 : :
7227 : : tree
7228 : 467511726 : build_nop (tree type, tree expr MEM_STAT_DECL)
7229 : : {
7230 : 467511726 : if (type == error_mark_node || error_operand_p (expr))
7231 : : return expr;
7232 : 467511645 : return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr PASS_MEM_STAT);
7233 : : }
7234 : :
7235 : : /* Take the address of ARG, whatever that means under C++ semantics.
7236 : : If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7237 : : and class rvalues as well.
7238 : :
7239 : : Nothing should call this function directly; instead, callers should use
7240 : : cp_build_addr_expr or cp_build_addr_expr_strict. */
7241 : :
7242 : : static tree
7243 : 214725343 : cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7244 : : {
7245 : 214725343 : tree argtype;
7246 : 214725343 : tree val;
7247 : :
7248 : 214725343 : if (!arg || error_operand_p (arg))
7249 : 12 : return error_mark_node;
7250 : :
7251 : 214725331 : arg = mark_lvalue_use (arg);
7252 : 214725331 : if (error_operand_p (arg))
7253 : 3 : return error_mark_node;
7254 : :
7255 : 214725328 : argtype = lvalue_type (arg);
7256 : 214725328 : location_t loc = cp_expr_loc_or_input_loc (arg);
7257 : :
7258 : 214725328 : gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7259 : :
7260 : 12973488 : if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7261 : 214725522 : && !really_overloaded_fn (arg))
7262 : : {
7263 : : /* They're trying to take the address of a unique non-static
7264 : : member function. This is ill-formed (except in MS-land),
7265 : : but let's try to DTRT.
7266 : : Note: We only handle unique functions here because we don't
7267 : : want to complain if there's a static overload; non-unique
7268 : : cases will be handled by instantiate_type. But we need to
7269 : : handle this case here to allow casts on the resulting PMF.
7270 : : We could defer this in non-MS mode, but it's easier to give
7271 : : a useful error here. */
7272 : :
7273 : : /* Inside constant member functions, the `this' pointer
7274 : : contains an extra const qualifier. TYPE_MAIN_VARIANT
7275 : : is used here to remove this const from the diagnostics
7276 : : and the created OFFSET_REF. */
7277 : 70 : tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7278 : 70 : tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7279 : 70 : if (!mark_used (fn, complain) && !(complain & tf_error))
7280 : 0 : return error_mark_node;
7281 : : /* Until microsoft headers are known to incorrectly take the address of
7282 : : unqualified xobj member functions we should not support this
7283 : : extension.
7284 : : See comment in class.cc:resolve_address_of_overloaded_function for
7285 : : the extended reasoning. */
7286 : 70 : if (!flag_ms_extensions || DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7287 : : {
7288 : 64 : auto_diagnostic_group d;
7289 : 64 : tree name = DECL_NAME (fn);
7290 : 64 : if (!(complain & tf_error))
7291 : 0 : return error_mark_node;
7292 : 64 : else if (current_class_type
7293 : 64 : && TREE_OPERAND (arg, 0) == current_class_ref)
7294 : : /* An expression like &memfn. */
7295 : 31 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7296 : 21 : permerror (loc,
7297 : : "ISO C++ forbids taking the address of an unqualified"
7298 : : " or parenthesized non-static member function to form"
7299 : : " a pointer to member function. Say %<&%T::%D%>",
7300 : : base, name);
7301 : : else
7302 : 10 : error_at (loc,
7303 : : "ISO C++ forbids taking the address of an unqualified"
7304 : : " or parenthesized non-static member function to form"
7305 : : " a pointer to explicit object member function");
7306 : : else
7307 : 33 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7308 : 9 : permerror (loc,
7309 : : "ISO C++ forbids taking the address of a bound member"
7310 : : " function to form a pointer to member function."
7311 : : " Say %<&%T::%D%>",
7312 : : base, name);
7313 : : else
7314 : 24 : error_at (loc,
7315 : : "ISO C++ forbids taking the address of a bound member"
7316 : : " function to form a pointer to explicit object member"
7317 : : " function");
7318 : 64 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7319 : 34 : inform (loc,
7320 : : "a pointer to explicit object member function can only be "
7321 : : "formed with %<&%T::%D%>", base, name);
7322 : 64 : }
7323 : 70 : arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7324 : : }
7325 : :
7326 : : /* Uninstantiated types are all functions. Taking the
7327 : : address of a function is a no-op, so just return the
7328 : : argument. */
7329 : 214725328 : if (type_unknown_p (arg))
7330 : 1915 : return build1 (ADDR_EXPR, unknown_type_node, arg);
7331 : :
7332 : 214723413 : if (TREE_CODE (arg) == OFFSET_REF)
7333 : : /* We want a pointer to member; bypass all the code for actually taking
7334 : : the address of something. */
7335 : 65234 : goto offset_ref;
7336 : :
7337 : : /* Anything not already handled and not a true memory reference
7338 : : is an error. */
7339 : 214658179 : if (!FUNC_OR_METHOD_TYPE_P (argtype))
7340 : : {
7341 : 112073587 : cp_lvalue_kind kind = lvalue_kind (arg);
7342 : 112073587 : if (kind == clk_none)
7343 : : {
7344 : 23 : if (complain & tf_error)
7345 : 23 : lvalue_error (loc, lv_addressof);
7346 : 23 : return error_mark_node;
7347 : : }
7348 : 112073564 : if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7349 : : {
7350 : 36 : if (!(complain & tf_error))
7351 : 9 : return error_mark_node;
7352 : : /* Make this a permerror because we used to accept it. */
7353 : 27 : permerror (loc, "taking address of rvalue");
7354 : : }
7355 : : }
7356 : :
7357 : 214658147 : if (TYPE_REF_P (argtype))
7358 : : {
7359 : 236 : tree type = build_pointer_type (TREE_TYPE (argtype));
7360 : 236 : arg = build1 (CONVERT_EXPR, type, arg);
7361 : 236 : return arg;
7362 : : }
7363 : 214657911 : else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7364 : : {
7365 : : /* ARM $3.4 */
7366 : : /* Apparently a lot of autoconf scripts for C++ packages do this,
7367 : : so only complain if -Wpedantic. */
7368 : 9 : if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7369 : 9 : pedwarn (loc, OPT_Wpedantic,
7370 : : "ISO C++ forbids taking address of function %<::main%>");
7371 : 0 : else if (flag_pedantic_errors)
7372 : 0 : return error_mark_node;
7373 : : }
7374 : :
7375 : : /* Let &* cancel out to simplify resulting code. */
7376 : 214657911 : if (INDIRECT_REF_P (arg))
7377 : : {
7378 : 71800884 : arg = TREE_OPERAND (arg, 0);
7379 : 71800884 : if (TYPE_REF_P (TREE_TYPE (arg)))
7380 : : {
7381 : 42725420 : tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7382 : 42725420 : arg = build1 (CONVERT_EXPR, type, arg);
7383 : : }
7384 : : else
7385 : : /* Don't let this be an lvalue. */
7386 : 29075464 : arg = rvalue (arg);
7387 : 71800884 : return arg;
7388 : : }
7389 : :
7390 : : /* Handle complex lvalues (when permitted)
7391 : : by reduction to simpler cases. */
7392 : 142857027 : val = unary_complex_lvalue (ADDR_EXPR, arg);
7393 : 142857027 : if (val != 0)
7394 : : return val;
7395 : :
7396 : 142370929 : switch (TREE_CODE (arg))
7397 : : {
7398 : 0 : CASE_CONVERT:
7399 : 0 : case FLOAT_EXPR:
7400 : 0 : case FIX_TRUNC_EXPR:
7401 : : /* We should have handled this above in the lvalue_kind check. */
7402 : 0 : gcc_unreachable ();
7403 : 65005 : break;
7404 : :
7405 : 65005 : case BASELINK:
7406 : 65005 : arg = BASELINK_FUNCTIONS (arg);
7407 : : /* Fall through. */
7408 : :
7409 : 65005 : case OVERLOAD:
7410 : 65005 : arg = OVL_FIRST (arg);
7411 : : break;
7412 : :
7413 : 65234 : case OFFSET_REF:
7414 : 65234 : offset_ref:
7415 : : /* Turn a reference to a non-static data member into a
7416 : : pointer-to-member. */
7417 : 65234 : {
7418 : 65234 : tree type;
7419 : 65234 : tree t;
7420 : :
7421 : 65234 : gcc_assert (PTRMEM_OK_P (arg));
7422 : :
7423 : 65234 : t = TREE_OPERAND (arg, 1);
7424 : 65234 : if (TYPE_REF_P (TREE_TYPE (t)))
7425 : : {
7426 : 15 : if (complain & tf_error)
7427 : 15 : error_at (loc,
7428 : : "cannot create pointer to reference member %qD", t);
7429 : 15 : return error_mark_node;
7430 : : }
7431 : :
7432 : : /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7433 : 65219 : if (TREE_CODE (t) == FUNCTION_DECL
7434 : 62441 : && !DECL_PURE_VIRTUAL_P (t)
7435 : 127639 : && !mark_used (t, complain) && !(complain & tf_error))
7436 : 3 : return error_mark_node;
7437 : :
7438 : : /* Pull out the function_decl for a single xobj member function, and
7439 : : let the rest of this function handle it. This is similar to how
7440 : : static member functions are handled in the BASELINK case above. */
7441 : 65216 : if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7442 : : {
7443 : : arg = t;
7444 : : break;
7445 : : }
7446 : :
7447 : 65160 : type = build_ptrmem_type (context_for_name_lookup (t),
7448 : 65160 : TREE_TYPE (t));
7449 : 65160 : t = make_ptrmem_cst (type, t);
7450 : 65160 : return t;
7451 : : }
7452 : :
7453 : : default:
7454 : : break;
7455 : : }
7456 : :
7457 : 142370985 : if (argtype != error_mark_node)
7458 : 142370985 : argtype = build_pointer_type (argtype);
7459 : :
7460 : 142370985 : if (bitfield_p (arg))
7461 : : {
7462 : 33 : if (complain & tf_error)
7463 : 33 : error_at (loc, "attempt to take address of bit-field");
7464 : 33 : return error_mark_node;
7465 : : }
7466 : :
7467 : : /* In a template, we are processing a non-dependent expression
7468 : : so we can just form an ADDR_EXPR with the correct type. */
7469 : 142370952 : if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7470 : : {
7471 : 129698011 : if (!mark_single_function (arg, complain))
7472 : 3 : return error_mark_node;
7473 : 129698008 : val = build_address (arg);
7474 : 129698008 : if (TREE_CODE (arg) == OFFSET_REF)
7475 : 0 : PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7476 : : }
7477 : 12672941 : else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7478 : : {
7479 : 27 : tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7480 : :
7481 : 27 : if (TREE_CODE (fn) == OVERLOAD && OVL_SINGLE_P (fn))
7482 : 27 : fn = OVL_FIRST (fn);
7483 : :
7484 : : /* We can only get here with a single static member
7485 : : function. */
7486 : 27 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7487 : : && DECL_STATIC_FUNCTION_P (fn));
7488 : 27 : if (!mark_used (fn, complain) && !(complain & tf_error))
7489 : 0 : return error_mark_node;
7490 : 27 : val = build_address (fn);
7491 : 27 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7492 : : /* Do not lose object's side effects. */
7493 : 12 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7494 : 12 : TREE_OPERAND (arg, 0), val);
7495 : : }
7496 : : else
7497 : : {
7498 : 12672914 : tree object = TREE_OPERAND (arg, 0);
7499 : 12672914 : tree field = TREE_OPERAND (arg, 1);
7500 : 12672914 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7501 : : (TREE_TYPE (object), decl_type_context (field)));
7502 : 12672914 : val = build_address (arg);
7503 : : }
7504 : :
7505 : 142370949 : if (TYPE_PTR_P (argtype)
7506 : 142370949 : && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7507 : : {
7508 : 1063 : build_ptrmemfunc_type (argtype);
7509 : 1063 : val = build_ptrmemfunc (argtype, val, 0,
7510 : : /*c_cast_p=*/false,
7511 : : complain);
7512 : : }
7513 : :
7514 : : /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7515 : 142370949 : if (TREE_CODE (val) == ADDR_EXPR
7516 : 142370949 : && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7517 : 100595964 : SET_EXPR_LOCATION (val, input_location);
7518 : :
7519 : : return val;
7520 : : }
7521 : :
7522 : : /* Take the address of ARG if it has one, even if it's an rvalue. */
7523 : :
7524 : : tree
7525 : 211019617 : cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7526 : : {
7527 : 211019617 : return cp_build_addr_expr_1 (arg, 0, complain);
7528 : : }
7529 : :
7530 : : /* Take the address of ARG, but only if it's an lvalue. */
7531 : :
7532 : : static tree
7533 : 3705726 : cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7534 : : {
7535 : 3705726 : return cp_build_addr_expr_1 (arg, 1, complain);
7536 : : }
7537 : :
7538 : : /* C++: Must handle pointers to members.
7539 : :
7540 : : Perhaps type instantiation should be extended to handle conversion
7541 : : from aggregates to types we don't yet know we want? (Or are those
7542 : : cases typically errors which should be reported?)
7543 : :
7544 : : NOCONVERT suppresses the default promotions (such as from short to int). */
7545 : :
7546 : : tree
7547 : 26993654 : cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7548 : : tsubst_flags_t complain)
7549 : : {
7550 : : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7551 : 26993654 : tree arg = xarg;
7552 : 26993654 : location_t location = cp_expr_loc_or_input_loc (arg);
7553 : 26993654 : tree argtype = 0;
7554 : 26993654 : tree eptype = NULL_TREE;
7555 : 26993654 : const char *errstring = NULL;
7556 : 26993654 : tree val;
7557 : 26993654 : const char *invalid_op_diag;
7558 : :
7559 : 26993654 : if (!arg || error_operand_p (arg))
7560 : 0 : return error_mark_node;
7561 : :
7562 : 26993654 : arg = resolve_nondeduced_context (arg, complain);
7563 : :
7564 : 53987308 : if ((invalid_op_diag
7565 : 26993654 : = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7566 : : ? CONVERT_EXPR
7567 : : : code),
7568 : 26993654 : TREE_TYPE (arg))))
7569 : : {
7570 : 0 : if (complain & tf_error)
7571 : 0 : error (invalid_op_diag);
7572 : 0 : return error_mark_node;
7573 : : }
7574 : :
7575 : 26993654 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7576 : : {
7577 : 10708 : eptype = TREE_TYPE (arg);
7578 : 10708 : arg = TREE_OPERAND (arg, 0);
7579 : : }
7580 : :
7581 : 26993654 : switch (code)
7582 : : {
7583 : 2411834 : case UNARY_PLUS_EXPR:
7584 : 2411834 : case NEGATE_EXPR:
7585 : 2411834 : {
7586 : 2411834 : int flags = WANT_ARITH | WANT_ENUM;
7587 : : /* Unary plus (but not unary minus) is allowed on pointers. */
7588 : 2411834 : if (code == UNARY_PLUS_EXPR)
7589 : 166071 : flags |= WANT_POINTER;
7590 : 2411834 : arg = build_expr_type_conversion (flags, arg, true);
7591 : 2411834 : if (!arg)
7592 : 30 : errstring = (code == NEGATE_EXPR
7593 : 24 : ? _("wrong type argument to unary minus")
7594 : 6 : : _("wrong type argument to unary plus"));
7595 : : else
7596 : : {
7597 : 2411804 : if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7598 : 611074 : arg = cp_perform_integral_promotions (arg, complain);
7599 : :
7600 : : /* Make sure the result is not an lvalue: a unary plus or minus
7601 : : expression is always a rvalue. */
7602 : 2411804 : arg = rvalue (arg);
7603 : : }
7604 : : }
7605 : : break;
7606 : :
7607 : 868119 : case BIT_NOT_EXPR:
7608 : 868119 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7609 : : {
7610 : 15 : code = CONJ_EXPR;
7611 : 15 : if (!noconvert)
7612 : : {
7613 : 15 : arg = cp_default_conversion (arg, complain);
7614 : 15 : if (arg == error_mark_node)
7615 : : return error_mark_node;
7616 : : }
7617 : : }
7618 : 868104 : else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7619 : : | WANT_VECTOR_OR_COMPLEX,
7620 : : arg, true)))
7621 : 13 : errstring = _("wrong type argument to bit-complement");
7622 : 868091 : else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7623 : : {
7624 : : /* Warn if the expression has boolean value. */
7625 : 867856 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7626 : 867856 : && (complain & tf_warning))
7627 : : {
7628 : 61 : auto_diagnostic_group d;
7629 : 61 : if (warning_at (location, OPT_Wbool_operation,
7630 : : "%<~%> on an expression of type %<bool%>"))
7631 : 42 : inform (location, "did you mean to use logical not (%<!%>)?");
7632 : 61 : }
7633 : 867856 : arg = cp_perform_integral_promotions (arg, complain);
7634 : : }
7635 : 235 : else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7636 : 235 : arg = mark_rvalue_use (arg);
7637 : : break;
7638 : :
7639 : 0 : case ABS_EXPR:
7640 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7641 : 0 : errstring = _("wrong type argument to abs");
7642 : 0 : else if (!noconvert)
7643 : : {
7644 : 0 : arg = cp_default_conversion (arg, complain);
7645 : 0 : if (arg == error_mark_node)
7646 : : return error_mark_node;
7647 : : }
7648 : : break;
7649 : :
7650 : 0 : case CONJ_EXPR:
7651 : : /* Conjugating a real value is a no-op, but allow it anyway. */
7652 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7653 : 0 : errstring = _("wrong type argument to conjugation");
7654 : 0 : else if (!noconvert)
7655 : : {
7656 : 0 : arg = cp_default_conversion (arg, complain);
7657 : 0 : if (arg == error_mark_node)
7658 : : return error_mark_node;
7659 : : }
7660 : : break;
7661 : :
7662 : 14987586 : case TRUTH_NOT_EXPR:
7663 : 14987586 : if (gnu_vector_type_p (TREE_TYPE (arg)))
7664 : 60 : return cp_build_binary_op (input_location, EQ_EXPR, arg,
7665 : 60 : build_zero_cst (TREE_TYPE (arg)), complain);
7666 : 14987526 : arg = perform_implicit_conversion (boolean_type_node, arg,
7667 : : complain);
7668 : 14987526 : if (arg != error_mark_node)
7669 : : {
7670 : 14987515 : if (processing_template_decl)
7671 : 7243403 : return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
7672 : 7744112 : val = invert_truthvalue_loc (location, arg);
7673 : 7744112 : if (obvalue_p (val))
7674 : 12652 : val = non_lvalue_loc (location, val);
7675 : 7744112 : return val;
7676 : : }
7677 : 11 : errstring = _("in argument to unary !");
7678 : 11 : break;
7679 : :
7680 : : case NOP_EXPR:
7681 : : break;
7682 : :
7683 : 467647 : case REALPART_EXPR:
7684 : 467647 : case IMAGPART_EXPR:
7685 : 467647 : val = build_real_imag_expr (input_location, code, arg);
7686 : 467647 : if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
7687 : 0 : val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
7688 : 0 : TREE_TYPE (eptype), val);
7689 : : return val;
7690 : :
7691 : 7666446 : case PREINCREMENT_EXPR:
7692 : 7666446 : case POSTINCREMENT_EXPR:
7693 : 7666446 : case PREDECREMENT_EXPR:
7694 : 7666446 : case POSTDECREMENT_EXPR:
7695 : : /* Handle complex lvalues (when permitted)
7696 : : by reduction to simpler cases. */
7697 : :
7698 : 7666446 : val = unary_complex_lvalue (code, arg);
7699 : 7666446 : if (val != 0)
7700 : 30 : goto return_build_unary_op;
7701 : :
7702 : 7666416 : tree stripped_arg;
7703 : 7666416 : stripped_arg = tree_strip_any_location_wrapper (arg);
7704 : 1409403 : if ((VAR_P (stripped_arg) || TREE_CODE (stripped_arg) == PARM_DECL)
7705 : 7185310 : && !DECL_READ_P (stripped_arg)
7706 : 8757404 : && (VAR_P (stripped_arg) ? warn_unused_but_set_variable
7707 : : : warn_unused_but_set_parameter) > 1)
7708 : : {
7709 : 9667 : arg = mark_lvalue_use (arg);
7710 : 9667 : DECL_READ_P (stripped_arg) = 0;
7711 : : }
7712 : : else
7713 : 7656749 : arg = mark_lvalue_use (arg);
7714 : :
7715 : : /* Increment or decrement the real part of the value,
7716 : : and don't change the imaginary part. */
7717 : 7666416 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7718 : : {
7719 : 18 : tree real, imag;
7720 : :
7721 : 18 : arg = cp_stabilize_reference (arg);
7722 : 18 : real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7723 : 18 : imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7724 : 18 : real = cp_build_unary_op (code, real, true, complain);
7725 : 18 : if (real == error_mark_node || imag == error_mark_node)
7726 : : return error_mark_node;
7727 : 15 : val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
7728 : 15 : goto return_build_unary_op;
7729 : : }
7730 : :
7731 : : /* Report invalid types. */
7732 : :
7733 : 7666398 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7734 : : arg, true)))
7735 : : {
7736 : 36 : if (code == PREINCREMENT_EXPR)
7737 : 9 : errstring = _("no pre-increment operator for type");
7738 : 27 : else if (code == POSTINCREMENT_EXPR)
7739 : 9 : errstring = _("no post-increment operator for type");
7740 : 18 : else if (code == PREDECREMENT_EXPR)
7741 : 9 : errstring = _("no pre-decrement operator for type");
7742 : : else
7743 : 9 : errstring = _("no post-decrement operator for type");
7744 : : break;
7745 : : }
7746 : 7666362 : else if (arg == error_mark_node)
7747 : : return error_mark_node;
7748 : :
7749 : : /* Report something read-only. */
7750 : :
7751 : 7666362 : if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7752 : 7666362 : || TREE_READONLY (arg))
7753 : : {
7754 : 61 : if (complain & tf_error)
7755 : 61 : cxx_readonly_error (location, arg,
7756 : 61 : ((code == PREINCREMENT_EXPR
7757 : 61 : || code == POSTINCREMENT_EXPR)
7758 : : ? lv_increment : lv_decrement));
7759 : : else
7760 : 0 : return error_mark_node;
7761 : : }
7762 : :
7763 : 7666362 : {
7764 : 7666362 : tree inc;
7765 : 7666362 : tree declared_type = unlowered_expr_type (arg);
7766 : :
7767 : 7666362 : argtype = TREE_TYPE (arg);
7768 : :
7769 : : /* ARM $5.2.5 last annotation says this should be forbidden. */
7770 : 7666362 : if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7771 : : {
7772 : 0 : if (complain & tf_error)
7773 : 0 : permerror (location, (code == PREINCREMENT_EXPR
7774 : 0 : || code == POSTINCREMENT_EXPR)
7775 : : ? G_("ISO C++ forbids incrementing an enum")
7776 : : : G_("ISO C++ forbids decrementing an enum"));
7777 : : else
7778 : 0 : return error_mark_node;
7779 : : }
7780 : :
7781 : : /* Compute the increment. */
7782 : :
7783 : 7666362 : if (TYPE_PTR_P (argtype))
7784 : : {
7785 : 2222336 : tree type = complete_type (TREE_TYPE (argtype));
7786 : :
7787 : 2222336 : if (!COMPLETE_OR_VOID_TYPE_P (type))
7788 : : {
7789 : 11 : if (complain & tf_error)
7790 : 9 : error_at (location, ((code == PREINCREMENT_EXPR
7791 : 9 : || code == POSTINCREMENT_EXPR))
7792 : : ? G_("cannot increment a pointer to incomplete "
7793 : : "type %qT")
7794 : : : G_("cannot decrement a pointer to incomplete "
7795 : : "type %qT"),
7796 : 9 : TREE_TYPE (argtype));
7797 : : else
7798 : 2 : return error_mark_node;
7799 : : }
7800 : 2222325 : else if (!TYPE_PTROB_P (argtype))
7801 : : {
7802 : 75 : if (complain & tf_error)
7803 : 63 : pedwarn (location, OPT_Wpointer_arith,
7804 : 63 : (code == PREINCREMENT_EXPR
7805 : 63 : || code == POSTINCREMENT_EXPR)
7806 : : ? G_("ISO C++ forbids incrementing a pointer "
7807 : : "of type %qT")
7808 : : : G_("ISO C++ forbids decrementing a pointer "
7809 : : "of type %qT"),
7810 : : argtype);
7811 : : else
7812 : 12 : return error_mark_node;
7813 : : }
7814 : 2222250 : else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7815 : 2222250 : TREE_TYPE (argtype),
7816 : : !(complain & tf_error)))
7817 : 0 : return error_mark_node;
7818 : :
7819 : 2222322 : inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7820 : : }
7821 : : else
7822 : 5443883 : inc = VECTOR_TYPE_P (argtype)
7823 : 5444026 : ? build_one_cst (argtype)
7824 : : : integer_one_node;
7825 : :
7826 : 7666348 : inc = cp_convert (argtype, inc, complain);
7827 : :
7828 : : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7829 : : need to ask Objective-C to build the increment or decrement
7830 : : expression for it. */
7831 : 7666348 : if (objc_is_property_ref (arg))
7832 : 0 : return objc_build_incr_expr_for_property_ref (input_location, code,
7833 : 0 : arg, inc);
7834 : :
7835 : : /* Complain about anything else that is not a true lvalue. */
7836 : 7666348 : if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7837 : 7666348 : || code == POSTINCREMENT_EXPR)
7838 : : ? lv_increment : lv_decrement),
7839 : : complain))
7840 : 33 : return error_mark_node;
7841 : :
7842 : : /* [depr.volatile.type] "Postfix ++ and -- expressions and
7843 : : prefix ++ and -- expressions of volatile-qualified arithmetic
7844 : : and pointer types are deprecated." */
7845 : 7665966 : if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7846 : 7666315 : && (complain & tf_warning))
7847 : 462 : warning_at (location, OPT_Wvolatile,
7848 : : "%qs expression of %<volatile%>-qualified type is "
7849 : : "deprecated",
7850 : : ((code == PREINCREMENT_EXPR
7851 : : || code == POSTINCREMENT_EXPR)
7852 : : ? "++" : "--"));
7853 : :
7854 : : /* Forbid using -- or ++ in C++17 on `bool'. */
7855 : 7666315 : if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7856 : : {
7857 : 95 : if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7858 : : {
7859 : 33 : if (complain & tf_error)
7860 : 33 : error_at (location,
7861 : : "use of an operand of type %qT in %<operator--%> "
7862 : : "is forbidden", boolean_type_node);
7863 : 33 : return error_mark_node;
7864 : : }
7865 : : else
7866 : : {
7867 : 62 : if (cxx_dialect >= cxx17)
7868 : : {
7869 : 33 : if (complain & tf_error)
7870 : 32 : error_at (location,
7871 : : "use of an operand of type %qT in "
7872 : : "%<operator++%> is forbidden in C++17",
7873 : : boolean_type_node);
7874 : 33 : return error_mark_node;
7875 : : }
7876 : : /* Otherwise, [depr.incr.bool] says this is deprecated. */
7877 : 29 : else if (complain & tf_warning)
7878 : 25 : warning_at (location, OPT_Wdeprecated,
7879 : : "use of an operand of type %qT "
7880 : : "in %<operator++%> is deprecated",
7881 : : boolean_type_node);
7882 : : }
7883 : 29 : val = boolean_increment (code, arg);
7884 : : }
7885 : 7666220 : else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7886 : : /* An rvalue has no cv-qualifiers. */
7887 : 1121598 : val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7888 : : else
7889 : 6544622 : val = build2 (code, TREE_TYPE (arg), arg, inc);
7890 : :
7891 : 7666249 : TREE_SIDE_EFFECTS (val) = 1;
7892 : 7666249 : goto return_build_unary_op;
7893 : : }
7894 : :
7895 : 592022 : case ADDR_EXPR:
7896 : : /* Note that this operation never does default_conversion
7897 : : regardless of NOCONVERT. */
7898 : 592022 : return cp_build_addr_expr (arg, complain);
7899 : :
7900 : : default:
7901 : : break;
7902 : : }
7903 : :
7904 : 3279985 : if (!errstring)
7905 : : {
7906 : 3279910 : if (argtype == 0)
7907 : 3279910 : argtype = TREE_TYPE (arg);
7908 : 3279910 : val = build1 (code, argtype, arg);
7909 : 10946204 : return_build_unary_op:
7910 : 10946204 : if (eptype)
7911 : 10699 : val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
7912 : 10946204 : return val;
7913 : : }
7914 : :
7915 : 90 : if (complain & tf_error)
7916 : 48 : error_at (location, "%s", errstring);
7917 : 90 : return error_mark_node;
7918 : : }
7919 : :
7920 : : /* Hook for the c-common bits that build a unary op. */
7921 : : tree
7922 : 4136 : build_unary_op (location_t /*location*/,
7923 : : enum tree_code code, tree xarg, bool noconvert)
7924 : : {
7925 : 4136 : return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
7926 : : }
7927 : :
7928 : : /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7929 : : so that it is a valid lvalue even for GENERIC by replacing
7930 : : (lhs = rhs) with ((lhs = rhs), lhs)
7931 : : (--lhs) with ((--lhs), lhs)
7932 : : (++lhs) with ((++lhs), lhs)
7933 : : and if lhs has side-effects, calling cp_stabilize_reference on it, so
7934 : : that it can be evaluated multiple times. */
7935 : :
7936 : : tree
7937 : 383822 : genericize_compound_lvalue (tree lvalue)
7938 : : {
7939 : 383822 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7940 : 196 : lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7941 : 196 : cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7942 : 196 : TREE_OPERAND (lvalue, 1));
7943 : 383822 : return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7944 : 383822 : lvalue, TREE_OPERAND (lvalue, 0));
7945 : : }
7946 : :
7947 : : /* Apply unary lvalue-demanding operator CODE to the expression ARG
7948 : : for certain kinds of expressions which are not really lvalues
7949 : : but which we can accept as lvalues.
7950 : :
7951 : : If ARG is not a kind of expression we can handle, return
7952 : : NULL_TREE. */
7953 : :
7954 : : tree
7955 : 263895255 : unary_complex_lvalue (enum tree_code code, tree arg)
7956 : : {
7957 : : /* Inside a template, making these kinds of adjustments is
7958 : : pointless; we are only concerned with the type of the
7959 : : expression. */
7960 : 264278871 : if (processing_template_decl)
7961 : : return NULL_TREE;
7962 : :
7963 : : /* Handle (a, b) used as an "lvalue". */
7964 : 184192828 : if (TREE_CODE (arg) == COMPOUND_EXPR)
7965 : : {
7966 : 384059 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7967 : : tf_warning_or_error);
7968 : 384059 : return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7969 : 768118 : TREE_OPERAND (arg, 0), real_result);
7970 : : }
7971 : :
7972 : : /* Handle (a ? b : c) used as an "lvalue". */
7973 : 183808769 : if (TREE_CODE (arg) == COND_EXPR
7974 : 183706679 : || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7975 : 102090 : return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7976 : :
7977 : : /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7978 : 183706679 : if (TREE_CODE (arg) == MODIFY_EXPR
7979 : 183323246 : || TREE_CODE (arg) == PREINCREMENT_EXPR
7980 : 183323162 : || TREE_CODE (arg) == PREDECREMENT_EXPR)
7981 : 383616 : return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7982 : :
7983 : 183323063 : if (code != ADDR_EXPR)
7984 : : return NULL_TREE;
7985 : :
7986 : : /* Handle (a = b) used as an "lvalue" for `&'. */
7987 : 180205649 : if (TREE_CODE (arg) == MODIFY_EXPR
7988 : 180205649 : || TREE_CODE (arg) == INIT_EXPR)
7989 : : {
7990 : 0 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
7991 : : tf_warning_or_error);
7992 : 0 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7993 : : arg, real_result);
7994 : 0 : suppress_warning (arg /* What warning? */);
7995 : 0 : return arg;
7996 : : }
7997 : :
7998 : 279290290 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7999 : 279289248 : || TREE_CODE (arg) == OFFSET_REF)
8000 : : return NULL_TREE;
8001 : :
8002 : : /* We permit compiler to make function calls returning
8003 : : objects of aggregate type look like lvalues. */
8004 : 99083599 : {
8005 : 99083599 : tree targ = arg;
8006 : :
8007 : 99083599 : if (TREE_CODE (targ) == SAVE_EXPR)
8008 : 0 : targ = TREE_OPERAND (targ, 0);
8009 : :
8010 : 99083599 : if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
8011 : : {
8012 : 29 : if (TREE_CODE (arg) == SAVE_EXPR)
8013 : : targ = arg;
8014 : : else
8015 : 29 : targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
8016 : 29 : return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
8017 : : }
8018 : :
8019 : 99083570 : if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
8020 : 0 : return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
8021 : 0 : TREE_OPERAND (targ, 0), current_function_decl, NULL);
8022 : : }
8023 : :
8024 : : /* Don't let anything else be handled specially. */
8025 : : return NULL_TREE;
8026 : : }
8027 : :
8028 : : /* Mark EXP saying that we need to be able to take the
8029 : : address of it; it should not be allocated in a register.
8030 : : Value is true if successful. ARRAY_REF_P is true if this
8031 : : is for ARRAY_REF construction - in that case we don't want
8032 : : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
8033 : : it is fine to use ARRAY_REFs for vector subscripts on vector
8034 : : register variables.
8035 : :
8036 : : C++: we do not allow `current_class_ptr' to be addressable. */
8037 : :
8038 : : bool
8039 : 293159085 : cxx_mark_addressable (tree exp, bool array_ref_p)
8040 : : {
8041 : 293159085 : tree x = exp;
8042 : :
8043 : 347085701 : while (1)
8044 : 347085701 : switch (TREE_CODE (x))
8045 : : {
8046 : 27214888 : case VIEW_CONVERT_EXPR:
8047 : 27214888 : if (array_ref_p
8048 : 719801 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8049 : 27766907 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
8050 : : return true;
8051 : 27211991 : x = TREE_OPERAND (x, 0);
8052 : 27211991 : break;
8053 : :
8054 : 26471349 : case COMPONENT_REF:
8055 : 26471349 : if (bitfield_p (x))
8056 : 9 : error ("attempt to take address of bit-field");
8057 : : /* FALLTHRU */
8058 : 26714625 : case ADDR_EXPR:
8059 : 26714625 : case ARRAY_REF:
8060 : 26714625 : case REALPART_EXPR:
8061 : 26714625 : case IMAGPART_EXPR:
8062 : 26714625 : x = TREE_OPERAND (x, 0);
8063 : 26714625 : break;
8064 : :
8065 : 5909198 : case PARM_DECL:
8066 : 5909198 : if (x == current_class_ptr)
8067 : : {
8068 : 18 : error ("cannot take the address of %<this%>, which is an rvalue expression");
8069 : 18 : TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
8070 : 18 : return true;
8071 : : }
8072 : : /* Fall through. */
8073 : :
8074 : 56880831 : case VAR_DECL:
8075 : : /* Caller should not be trying to mark initialized
8076 : : constant fields addressable. */
8077 : 56880831 : gcc_assert (DECL_LANG_SPECIFIC (x) == 0
8078 : : || DECL_IN_AGGR_P (x) == 0
8079 : : || TREE_STATIC (x)
8080 : : || DECL_EXTERNAL (x));
8081 : : /* Fall through. */
8082 : :
8083 : 56950707 : case RESULT_DECL:
8084 : 56950786 : if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
8085 : 56950780 : && !DECL_ARTIFICIAL (x))
8086 : : {
8087 : 22 : if (VAR_P (x) && DECL_HARD_REGISTER (x))
8088 : : {
8089 : 12 : error
8090 : 12 : ("address of explicit register variable %qD requested", x);
8091 : 12 : return false;
8092 : : }
8093 : 10 : else if (extra_warnings)
8094 : 3 : warning
8095 : 3 : (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
8096 : : }
8097 : 56950695 : TREE_ADDRESSABLE (x) = 1;
8098 : 56950695 : return true;
8099 : :
8100 : 145272003 : case CONST_DECL:
8101 : 145272003 : case FUNCTION_DECL:
8102 : 145272003 : TREE_ADDRESSABLE (x) = 1;
8103 : 145272003 : return true;
8104 : :
8105 : 53194 : case CONSTRUCTOR:
8106 : 53194 : TREE_ADDRESSABLE (x) = 1;
8107 : 53194 : return true;
8108 : :
8109 : 12555030 : case TARGET_EXPR:
8110 : 12555030 : TREE_ADDRESSABLE (x) = 1;
8111 : 12555030 : cxx_mark_addressable (TARGET_EXPR_SLOT (x));
8112 : 12555030 : return true;
8113 : :
8114 : : default:
8115 : : return true;
8116 : : }
8117 : : }
8118 : :
8119 : : /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
8120 : :
8121 : : tree
8122 : 6586925 : build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
8123 : : tsubst_flags_t complain)
8124 : : {
8125 : 6586925 : tree orig_ifexp = ifexp;
8126 : 6586925 : tree orig_op1 = op1;
8127 : 6586925 : tree orig_op2 = op2;
8128 : 6586925 : tree expr;
8129 : :
8130 : 6586925 : if (processing_template_decl)
8131 : : {
8132 : : /* The standard says that the expression is type-dependent if
8133 : : IFEXP is type-dependent, even though the eventual type of the
8134 : : expression doesn't dependent on IFEXP. */
8135 : 2687626 : if (type_dependent_expression_p (ifexp)
8136 : : /* As a GNU extension, the middle operand may be omitted. */
8137 : 1332793 : || (op1 && type_dependent_expression_p (op1))
8138 : 3572684 : || type_dependent_expression_p (op2))
8139 : 1832026 : return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
8140 : : }
8141 : :
8142 : 4754899 : expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
8143 : 4754899 : if (processing_template_decl && expr != error_mark_node)
8144 : : {
8145 : 855594 : tree min = build_min_non_dep (COND_EXPR, expr,
8146 : : orig_ifexp, orig_op1, orig_op2);
8147 : 855594 : expr = convert_from_reference (min);
8148 : : }
8149 : : return expr;
8150 : : }
8151 : :
8152 : : /* Given a list of expressions, return a compound expression
8153 : : that performs them all and returns the value of the last of them. */
8154 : :
8155 : : tree
8156 : 30891496 : build_x_compound_expr_from_list (tree list, expr_list_kind exp,
8157 : : tsubst_flags_t complain)
8158 : : {
8159 : 30891496 : tree expr = TREE_VALUE (list);
8160 : :
8161 : 253233 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8162 : 31144717 : && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
8163 : : {
8164 : 19 : if (complain & tf_error)
8165 : 38 : pedwarn (cp_expr_loc_or_input_loc (expr), 0,
8166 : : "list-initializer for non-class type must not "
8167 : : "be parenthesized");
8168 : : else
8169 : 0 : return error_mark_node;
8170 : : }
8171 : :
8172 : 30891496 : if (TREE_CHAIN (list))
8173 : : {
8174 : 24 : if (complain & tf_error)
8175 : 15 : switch (exp)
8176 : : {
8177 : 12 : case ELK_INIT:
8178 : 12 : permerror (input_location, "expression list treated as compound "
8179 : : "expression in initializer");
8180 : 12 : break;
8181 : 2 : case ELK_MEM_INIT:
8182 : 2 : permerror (input_location, "expression list treated as compound "
8183 : : "expression in mem-initializer");
8184 : 2 : break;
8185 : 1 : case ELK_FUNC_CAST:
8186 : 1 : permerror (input_location, "expression list treated as compound "
8187 : : "expression in functional cast");
8188 : 1 : break;
8189 : 0 : default:
8190 : 0 : gcc_unreachable ();
8191 : : }
8192 : : else
8193 : 9 : return error_mark_node;
8194 : :
8195 : 30 : for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
8196 : 30 : expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
8197 : 15 : expr, TREE_VALUE (list), NULL_TREE,
8198 : : complain);
8199 : : }
8200 : :
8201 : : return expr;
8202 : : }
8203 : :
8204 : : /* Like build_x_compound_expr_from_list, but using a VEC. */
8205 : :
8206 : : tree
8207 : 132713 : build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8208 : : tsubst_flags_t complain)
8209 : : {
8210 : 132713 : if (vec_safe_is_empty (vec))
8211 : : return NULL_TREE;
8212 : 132713 : else if (vec->length () == 1)
8213 : 132648 : return (*vec)[0];
8214 : : else
8215 : : {
8216 : 65 : tree expr;
8217 : 65 : unsigned int ix;
8218 : 65 : tree t;
8219 : :
8220 : 65 : if (msg != NULL)
8221 : : {
8222 : 5 : if (complain & tf_error)
8223 : 0 : permerror (input_location,
8224 : : "%s expression list treated as compound expression",
8225 : : msg);
8226 : : else
8227 : 5 : return error_mark_node;
8228 : : }
8229 : :
8230 : 60 : expr = (*vec)[0];
8231 : 134 : for (ix = 1; vec->iterate (ix, &t); ++ix)
8232 : 74 : expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
8233 : : t, NULL_TREE, complain);
8234 : :
8235 : : return expr;
8236 : : }
8237 : : }
8238 : :
8239 : : /* Handle overloading of the ',' operator when needed. */
8240 : :
8241 : : tree
8242 : 2746573 : build_x_compound_expr (location_t loc, tree op1, tree op2,
8243 : : tree lookups, tsubst_flags_t complain)
8244 : : {
8245 : 2746573 : tree result;
8246 : 2746573 : tree orig_op1 = op1;
8247 : 2746573 : tree orig_op2 = op2;
8248 : 2746573 : tree overload = NULL_TREE;
8249 : :
8250 : 2746573 : if (processing_template_decl)
8251 : : {
8252 : 2515979 : if (type_dependent_expression_p (op1)
8253 : 2515979 : || type_dependent_expression_p (op2))
8254 : : {
8255 : 2235433 : result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8256 : 2235433 : TREE_TYPE (result)
8257 : 2235433 : = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
8258 : 2235433 : return result;
8259 : : }
8260 : : }
8261 : :
8262 : 511140 : result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8263 : : NULL_TREE, lookups, &overload, complain);
8264 : 511140 : if (!result)
8265 : 504040 : result = cp_build_compound_expr (op1, op2, complain);
8266 : :
8267 : 511140 : if (processing_template_decl && result != error_mark_node)
8268 : : {
8269 : 278725 : if (overload != NULL_TREE)
8270 : 52 : return (build_min_non_dep_op_overload
8271 : 52 : (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
8272 : :
8273 : 278673 : return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
8274 : : }
8275 : :
8276 : : return result;
8277 : : }
8278 : :
8279 : : /* Like cp_build_compound_expr, but for the c-common bits. */
8280 : :
8281 : : tree
8282 : 11252 : build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8283 : : {
8284 : 11252 : return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8285 : : }
8286 : :
8287 : : /* Build a compound expression. */
8288 : :
8289 : : tree
8290 : 616647 : cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8291 : : {
8292 : 616647 : lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8293 : :
8294 : 616647 : if (lhs == error_mark_node || rhs == error_mark_node)
8295 : : return error_mark_node;
8296 : :
8297 : 616644 : if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8298 : 0 : lhs = TREE_OPERAND (lhs, 0);
8299 : 616644 : tree eptype = NULL_TREE;
8300 : 616644 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8301 : : {
8302 : 3 : eptype = TREE_TYPE (rhs);
8303 : 3 : rhs = TREE_OPERAND (rhs, 0);
8304 : : }
8305 : :
8306 : 616644 : if (TREE_CODE (rhs) == TARGET_EXPR)
8307 : : {
8308 : : /* If the rhs is a TARGET_EXPR, then build the compound
8309 : : expression inside the target_expr's initializer. This
8310 : : helps the compiler to eliminate unnecessary temporaries. */
8311 : 3052 : tree init = TARGET_EXPR_INITIAL (rhs);
8312 : :
8313 : 3052 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8314 : 3052 : TARGET_EXPR_INITIAL (rhs) = init;
8315 : :
8316 : 3052 : if (eptype)
8317 : 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8318 : 3052 : return rhs;
8319 : : }
8320 : :
8321 : 613592 : rhs = resolve_nondeduced_context (rhs, complain);
8322 : :
8323 : 613592 : if (type_unknown_p (rhs))
8324 : : {
8325 : 33 : if (complain & tf_error)
8326 : 51 : error_at (cp_expr_loc_or_input_loc (rhs),
8327 : : "no context to resolve type of %qE", rhs);
8328 : 33 : return error_mark_node;
8329 : : }
8330 : :
8331 : 613559 : tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8332 : 613559 : if (eptype)
8333 : 3 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8334 : : return ret;
8335 : : }
8336 : :
8337 : : /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8338 : : casts away constness. CAST gives the type of cast. Returns true
8339 : : if the cast is ill-formed, false if it is well-formed.
8340 : :
8341 : : ??? This function warns for casting away any qualifier not just
8342 : : const. We would like to specify exactly what qualifiers are casted
8343 : : away.
8344 : : */
8345 : :
8346 : : static bool
8347 : 690211 : check_for_casting_away_constness (location_t loc, tree src_type,
8348 : : tree dest_type, enum tree_code cast,
8349 : : tsubst_flags_t complain)
8350 : : {
8351 : : /* C-style casts are allowed to cast away constness. With
8352 : : WARN_CAST_QUAL, we still want to issue a warning. */
8353 : 690211 : if (cast == CAST_EXPR && !warn_cast_qual)
8354 : : return false;
8355 : :
8356 : 558697 : if (!casts_away_constness (src_type, dest_type, complain))
8357 : : return false;
8358 : :
8359 : 392 : switch (cast)
8360 : : {
8361 : 335 : case CAST_EXPR:
8362 : 335 : if (complain & tf_warning)
8363 : 335 : warning_at (loc, OPT_Wcast_qual,
8364 : : "cast from type %qT to type %qT casts away qualifiers",
8365 : : src_type, dest_type);
8366 : : return false;
8367 : :
8368 : 36 : case STATIC_CAST_EXPR:
8369 : 36 : if (complain & tf_error)
8370 : 24 : error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8371 : : "away qualifiers",
8372 : : src_type, dest_type);
8373 : : return true;
8374 : :
8375 : 21 : case REINTERPRET_CAST_EXPR:
8376 : 21 : if (complain & tf_error)
8377 : 15 : error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8378 : : "casts away qualifiers",
8379 : : src_type, dest_type);
8380 : : return true;
8381 : :
8382 : 0 : default:
8383 : 0 : gcc_unreachable();
8384 : : }
8385 : : }
8386 : :
8387 : : /* Warns if the cast from expression EXPR to type TYPE is useless. */
8388 : : void
8389 : 45012978 : maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8390 : : tsubst_flags_t complain)
8391 : : {
8392 : 45012978 : if (warn_useless_cast
8393 : 152 : && complain & tf_warning)
8394 : : {
8395 : 152 : if (TYPE_REF_P (type)
8396 : 152 : ? ((TYPE_REF_IS_RVALUE (type)
8397 : 86 : ? xvalue_p (expr) : lvalue_p (expr))
8398 : 172 : && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8399 : : /* Don't warn when converting a class object to a non-reference type,
8400 : : because that's a common way to create a temporary. */
8401 : 66 : : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8402 : 96 : warning_at (loc, OPT_Wuseless_cast,
8403 : : "useless cast to type %q#T", type);
8404 : : }
8405 : 45012978 : }
8406 : :
8407 : : /* Warns if the cast ignores cv-qualifiers on TYPE. */
8408 : : static void
8409 : 45003382 : maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8410 : : tsubst_flags_t complain)
8411 : : {
8412 : 45003382 : if (warn_ignored_qualifiers
8413 : 263558 : && complain & tf_warning
8414 : 263138 : && !CLASS_TYPE_P (type)
8415 : 45261737 : && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8416 : 36 : warning_at (loc, OPT_Wignored_qualifiers,
8417 : : "type qualifiers ignored on cast result type");
8418 : 45003382 : }
8419 : :
8420 : : /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8421 : : (another pointer-to-member type in the same hierarchy) and return
8422 : : the converted expression. If ALLOW_INVERSE_P is permitted, a
8423 : : pointer-to-derived may be converted to pointer-to-base; otherwise,
8424 : : only the other direction is permitted. If C_CAST_P is true, this
8425 : : conversion is taking place as part of a C-style cast. */
8426 : :
8427 : : tree
8428 : 29973 : convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8429 : : bool c_cast_p, tsubst_flags_t complain)
8430 : : {
8431 : 29973 : if (same_type_p (type, TREE_TYPE (expr)))
8432 : : return expr;
8433 : :
8434 : 29973 : if (TYPE_PTRDATAMEM_P (type))
8435 : : {
8436 : 364 : tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8437 : 364 : tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8438 : 364 : tree delta = (get_delta_difference
8439 : 364 : (obase, nbase,
8440 : : allow_inverse_p, c_cast_p, complain));
8441 : :
8442 : 364 : if (delta == error_mark_node)
8443 : : return error_mark_node;
8444 : :
8445 : 358 : if (!same_type_p (obase, nbase))
8446 : : {
8447 : 287 : if (TREE_CODE (expr) == PTRMEM_CST)
8448 : 159 : expr = cplus_expand_constant (expr);
8449 : :
8450 : 287 : tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8451 : 287 : build_int_cst (TREE_TYPE (expr), -1),
8452 : : complain);
8453 : 287 : tree op1 = build_nop (ptrdiff_type_node, expr);
8454 : 287 : tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8455 : : complain);
8456 : :
8457 : 287 : expr = fold_build3_loc (input_location,
8458 : : COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8459 : : }
8460 : :
8461 : 358 : return build_nop (type, expr);
8462 : : }
8463 : : else
8464 : 29609 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8465 : 29609 : allow_inverse_p, c_cast_p, complain);
8466 : : }
8467 : :
8468 : : /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8469 : : this static_cast is being attempted as one of the possible casts
8470 : : allowed by a C-style cast. (In that case, accessibility of base
8471 : : classes is not considered, and it is OK to cast away
8472 : : constness.) Return the result of the cast. *VALID_P is set to
8473 : : indicate whether or not the cast was valid. */
8474 : :
8475 : : static tree
8476 : 44641251 : build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8477 : : bool *valid_p, tsubst_flags_t complain)
8478 : : {
8479 : 44641251 : tree intype;
8480 : 44641251 : tree result;
8481 : 44641251 : cp_lvalue_kind clk;
8482 : :
8483 : : /* Assume the cast is valid. */
8484 : 44641251 : *valid_p = true;
8485 : :
8486 : 44641251 : intype = unlowered_expr_type (expr);
8487 : :
8488 : : /* Save casted types in the function's used types hash table. */
8489 : 44641251 : used_types_insert (type);
8490 : :
8491 : : /* A prvalue of non-class type is cv-unqualified. */
8492 : 44641251 : if (!CLASS_TYPE_P (type))
8493 : 42926380 : type = cv_unqualified (type);
8494 : :
8495 : : /* [expr.static.cast]
8496 : :
8497 : : An lvalue of type "cv1 B", where B is a class type, can be cast
8498 : : to type "reference to cv2 D", where D is a class derived (clause
8499 : : _class.derived_) from B, if a valid standard conversion from
8500 : : "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8501 : : same cv-qualification as, or greater cv-qualification than, cv1,
8502 : : and B is not a virtual base class of D. */
8503 : : /* We check this case before checking the validity of "TYPE t =
8504 : : EXPR;" below because for this case:
8505 : :
8506 : : struct B {};
8507 : : struct D : public B { D(const B&); };
8508 : : extern B& b;
8509 : : void f() { static_cast<const D&>(b); }
8510 : :
8511 : : we want to avoid constructing a new D. The standard is not
8512 : : completely clear about this issue, but our interpretation is
8513 : : consistent with other compilers. */
8514 : 44641251 : if (TYPE_REF_P (type)
8515 : 3616153 : && CLASS_TYPE_P (TREE_TYPE (type))
8516 : 2804589 : && CLASS_TYPE_P (intype)
8517 : 2804574 : && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8518 : 2796902 : && DERIVED_FROM_P (intype, TREE_TYPE (type))
8519 : 2764795 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8520 : 2764795 : build_pointer_type (TYPE_MAIN_VARIANT
8521 : : (TREE_TYPE (type))),
8522 : : complain)
8523 : 47406046 : && (c_cast_p
8524 : 2764771 : || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8525 : : {
8526 : 2764795 : tree base;
8527 : :
8528 : 2764795 : if (processing_template_decl)
8529 : : return expr;
8530 : :
8531 : : /* There is a standard conversion from "D*" to "B*" even if "B"
8532 : : is ambiguous or inaccessible. If this is really a
8533 : : static_cast, then we check both for inaccessibility and
8534 : : ambiguity. However, if this is a static_cast being performed
8535 : : because the user wrote a C-style cast, then accessibility is
8536 : : not considered. */
8537 : 5118470 : base = lookup_base (TREE_TYPE (type), intype,
8538 : : c_cast_p ? ba_unique : ba_check,
8539 : : NULL, complain);
8540 : 2559247 : expr = cp_build_addr_expr (expr, complain);
8541 : :
8542 : 2559247 : if (sanitize_flags_p (SANITIZE_VPTR))
8543 : : {
8544 : 456 : tree ubsan_check
8545 : 456 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8546 : : intype, expr);
8547 : 456 : if (ubsan_check)
8548 : 2559247 : expr = ubsan_check;
8549 : : }
8550 : :
8551 : : /* Convert from "B*" to "D*". This function will check that "B"
8552 : : is not a virtual base of "D". Even if we don't have a guarantee
8553 : : that expr is NULL, if the static_cast is to a reference type,
8554 : : it is UB if it would be NULL, so omit the non-NULL check. */
8555 : 2559247 : expr = build_base_path (MINUS_EXPR, expr, base,
8556 : : /*nonnull=*/flag_delete_null_pointer_checks,
8557 : : complain);
8558 : :
8559 : : /* Convert the pointer to a reference -- but then remember that
8560 : : there are no expressions with reference type in C++.
8561 : :
8562 : : We call rvalue so that there's an actual tree code
8563 : : (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8564 : : is a variable with the same type, the conversion would get folded
8565 : : away, leaving just the variable and causing lvalue_kind to give
8566 : : the wrong answer. */
8567 : 2559247 : expr = cp_fold_convert (type, expr);
8568 : :
8569 : : /* When -fsanitize=null, make sure to diagnose reference binding to
8570 : : NULL even when the reference is converted to pointer later on. */
8571 : 2559247 : if (sanitize_flags_p (SANITIZE_NULL)
8572 : 462 : && TREE_CODE (expr) == COND_EXPR
8573 : 6 : && TREE_OPERAND (expr, 2)
8574 : 6 : && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8575 : 2559253 : && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8576 : 6 : ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8577 : :
8578 : 2559247 : return convert_from_reference (rvalue (expr));
8579 : : }
8580 : :
8581 : : /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8582 : : cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8583 : 41876456 : if (TYPE_REF_P (type)
8584 : 851358 : && TYPE_REF_IS_RVALUE (type)
8585 : 465436 : && (clk = real_lvalue_p (expr))
8586 : 462123 : && reference_compatible_p (TREE_TYPE (type), intype)
8587 : 42338576 : && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8588 : : {
8589 : 462120 : if (processing_template_decl)
8590 : : return expr;
8591 : 461903 : if (clk == clk_ordinary)
8592 : : {
8593 : : /* Handle the (non-bit-field) lvalue case here by casting to
8594 : : lvalue reference and then changing it to an rvalue reference.
8595 : : Casting an xvalue to rvalue reference will be handled by the
8596 : : main code path. */
8597 : 461900 : tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8598 : 461900 : result = (perform_direct_initialization_if_possible
8599 : 461900 : (lref, expr, c_cast_p, complain));
8600 : 461900 : result = build1 (NON_LVALUE_EXPR, type, result);
8601 : 461900 : return convert_from_reference (result);
8602 : : }
8603 : : else
8604 : : /* For a bit-field or packed field, bind to a temporary. */
8605 : 3 : expr = rvalue (expr);
8606 : : }
8607 : :
8608 : : /* Resolve overloaded address here rather than once in
8609 : : implicit_conversion and again in the inverse code below. */
8610 : 41414339 : if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8611 : : {
8612 : 18 : expr = instantiate_type (type, expr, complain);
8613 : 18 : intype = TREE_TYPE (expr);
8614 : : }
8615 : :
8616 : : /* [expr.static.cast]
8617 : :
8618 : : Any expression can be explicitly converted to type cv void. */
8619 : 41414339 : if (VOID_TYPE_P (type))
8620 : : {
8621 : 409121 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8622 : 0 : expr = TREE_OPERAND (expr, 0);
8623 : 409121 : return convert_to_void (expr, ICV_CAST, complain);
8624 : : }
8625 : :
8626 : : /* [class.abstract]
8627 : : An abstract class shall not be used ... as the type of an explicit
8628 : : conversion. */
8629 : 41005218 : if (abstract_virtuals_error (ACU_CAST, type, complain))
8630 : 6 : return error_mark_node;
8631 : :
8632 : : /* [expr.static.cast]
8633 : :
8634 : : An expression e can be explicitly converted to a type T using a
8635 : : static_cast of the form static_cast<T>(e) if the declaration T
8636 : : t(e);" is well-formed, for some invented temporary variable
8637 : : t. */
8638 : 41005212 : result = perform_direct_initialization_if_possible (type, expr,
8639 : : c_cast_p, complain);
8640 : : /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8641 : : which initialize the first element of the aggregate. We need to handle
8642 : : the array case specifically. */
8643 : 41005212 : if (result == NULL_TREE
8644 : 4001898 : && cxx_dialect >= cxx20
8645 : 1092860 : && TREE_CODE (type) == ARRAY_TYPE)
8646 : : {
8647 : : /* Create { EXPR } and perform direct-initialization from it. */
8648 : 15 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8649 : 15 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8650 : 15 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8651 : 15 : result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8652 : : complain);
8653 : : }
8654 : 4001898 : if (result)
8655 : : {
8656 : 37003329 : if (processing_template_decl)
8657 : : return expr;
8658 : :
8659 : 36326965 : result = convert_from_reference (result);
8660 : :
8661 : : /* [expr.static.cast]
8662 : :
8663 : : If T is a reference type, the result is an lvalue; otherwise,
8664 : : the result is an rvalue. */
8665 : 36326965 : if (!TYPE_REF_P (type))
8666 : : {
8667 : 35937821 : result = rvalue (result);
8668 : :
8669 : 35937821 : if (result == expr && SCALAR_TYPE_P (type))
8670 : : /* Leave some record of the cast. */
8671 : 2060712 : result = build_nop (type, expr);
8672 : : }
8673 : 36326965 : return result;
8674 : : }
8675 : :
8676 : : /* [expr.static.cast]
8677 : :
8678 : : The inverse of any standard conversion sequence (clause _conv_),
8679 : : other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8680 : : (_conv.array_), function-to-pointer (_conv.func_), and boolean
8681 : : (_conv.bool_) conversions, can be performed explicitly using
8682 : : static_cast subject to the restriction that the explicit
8683 : : conversion does not cast away constness (_expr.const.cast_), and
8684 : : the following additional rules for specific cases: */
8685 : : /* For reference, the conversions not excluded are: integral
8686 : : promotions, floating-point promotion, integral conversions,
8687 : : floating-point conversions, floating-integral conversions,
8688 : : pointer conversions, and pointer to member conversions. */
8689 : : /* DR 128
8690 : :
8691 : : A value of integral _or enumeration_ type can be explicitly
8692 : : converted to an enumeration type. */
8693 : : /* The effect of all that is that any conversion between any two
8694 : : types which are integral, floating, or enumeration types can be
8695 : : performed. */
8696 : 4001883 : if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8697 : 2285819 : || SCALAR_FLOAT_TYPE_P (type))
8698 : 1885212 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8699 : 274471 : || SCALAR_FLOAT_TYPE_P (intype)))
8700 : : {
8701 : 1779877 : if (processing_template_decl)
8702 : : return expr;
8703 : 1706144 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8704 : 0 : expr = TREE_OPERAND (expr, 0);
8705 : : /* [expr.static.cast]: "If the value is not a bit-field, the result
8706 : : refers to the object or the specified base class subobject thereof;
8707 : : otherwise, the lvalue-to-rvalue conversion is applied to the
8708 : : bit-field and the resulting prvalue is used as the operand of the
8709 : : static_cast." There are no prvalue bit-fields; the l-to-r conversion
8710 : : will give us an object of the underlying type of the bit-field. */
8711 : 1706144 : expr = decay_conversion (expr, complain);
8712 : 1706144 : return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8713 : : }
8714 : :
8715 : 716574 : if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8716 : 712229 : && CLASS_TYPE_P (TREE_TYPE (type))
8717 : 202465 : && CLASS_TYPE_P (TREE_TYPE (intype))
8718 : 2260848 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8719 : : (TREE_TYPE (intype))),
8720 : 38842 : build_pointer_type (TYPE_MAIN_VARIANT
8721 : : (TREE_TYPE (type))),
8722 : : complain))
8723 : : {
8724 : 38503 : tree base;
8725 : :
8726 : 38503 : if (processing_template_decl)
8727 : : return expr;
8728 : :
8729 : 38485 : if (!c_cast_p
8730 : 38485 : && check_for_casting_away_constness (loc, intype, type,
8731 : : STATIC_CAST_EXPR,
8732 : : complain))
8733 : 6 : return error_mark_node;
8734 : 76419 : base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8735 : : c_cast_p ? ba_unique : ba_check,
8736 : : NULL, complain);
8737 : 38479 : expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8738 : : complain);
8739 : :
8740 : 38479 : if (sanitize_flags_p (SANITIZE_VPTR))
8741 : : {
8742 : 43 : tree ubsan_check
8743 : 43 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8744 : : intype, expr);
8745 : 43 : if (ubsan_check)
8746 : 38479 : expr = ubsan_check;
8747 : : }
8748 : :
8749 : 38479 : return cp_fold_convert (type, expr);
8750 : : }
8751 : :
8752 : 89 : if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8753 : 2183503 : || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8754 : : {
8755 : 232 : tree c1;
8756 : 232 : tree c2;
8757 : 232 : tree t1;
8758 : 232 : tree t2;
8759 : :
8760 : 232 : c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8761 : 232 : c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8762 : :
8763 : 232 : if (TYPE_PTRDATAMEM_P (type))
8764 : : {
8765 : 89 : t1 = (build_ptrmem_type
8766 : 89 : (c1,
8767 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8768 : 89 : t2 = (build_ptrmem_type
8769 : 89 : (c2,
8770 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8771 : : }
8772 : : else
8773 : : {
8774 : : t1 = intype;
8775 : : t2 = type;
8776 : : }
8777 : 232 : if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8778 : : {
8779 : 154 : if (!c_cast_p
8780 : 154 : && check_for_casting_away_constness (loc, intype, type,
8781 : : STATIC_CAST_EXPR,
8782 : : complain))
8783 : 9 : return error_mark_node;
8784 : 145 : if (processing_template_decl)
8785 : : return expr;
8786 : 145 : return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8787 : 145 : c_cast_p, complain);
8788 : : }
8789 : : }
8790 : :
8791 : : /* [expr.static.cast]
8792 : :
8793 : : An rvalue of type "pointer to cv void" can be explicitly
8794 : : converted to a pointer to object type. A value of type pointer
8795 : : to object converted to "pointer to cv void" and back to the
8796 : : original pointer type will have its original value. */
8797 : 2183349 : if (TYPE_PTR_P (intype)
8798 : 777079 : && VOID_TYPE_P (TREE_TYPE (intype))
8799 : 2829736 : && TYPE_PTROB_P (type))
8800 : : {
8801 : 614793 : if (!c_cast_p
8802 : 614793 : && check_for_casting_away_constness (loc, intype, type,
8803 : : STATIC_CAST_EXPR,
8804 : : complain))
8805 : 21 : return error_mark_node;
8806 : 614772 : if (processing_template_decl)
8807 : : return expr;
8808 : 526710 : return build_nop (type, expr);
8809 : : }
8810 : :
8811 : 1568556 : *valid_p = false;
8812 : 1568556 : return error_mark_node;
8813 : : }
8814 : :
8815 : : /* Return an expression representing static_cast<TYPE>(EXPR). */
8816 : :
8817 : : tree
8818 : 12836775 : build_static_cast (location_t loc, tree type, tree oexpr,
8819 : : tsubst_flags_t complain)
8820 : : {
8821 : 12836775 : tree expr = oexpr;
8822 : 12836775 : tree result;
8823 : 12836775 : bool valid_p;
8824 : :
8825 : 12836775 : if (type == error_mark_node || expr == error_mark_node)
8826 : : return error_mark_node;
8827 : :
8828 : 12836721 : bool dependent = (dependent_type_p (type)
8829 : 12836721 : || type_dependent_expression_p (expr));
8830 : 8784851 : if (dependent)
8831 : : {
8832 : 5095849 : tmpl:
8833 : 5095849 : expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8834 : : /* We don't know if it will or will not have side effects. */
8835 : 5095849 : TREE_SIDE_EFFECTS (expr) = 1;
8836 : 5095849 : result = convert_from_reference (expr);
8837 : 5095849 : protected_set_expr_location (result, loc);
8838 : 5095849 : return result;
8839 : : }
8840 : :
8841 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8842 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8843 : 8784851 : if (!TYPE_REF_P (type)
8844 : 5169685 : && TREE_CODE (expr) == NOP_EXPR
8845 : 8885060 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8846 : 48 : expr = TREE_OPERAND (expr, 0);
8847 : :
8848 : 8784851 : result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8849 : : &valid_p, complain);
8850 : 8784851 : if (valid_p)
8851 : : {
8852 : 8784655 : if (result != error_mark_node)
8853 : : {
8854 : 8784550 : maybe_warn_about_useless_cast (loc, type, expr, complain);
8855 : 8784550 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8856 : : }
8857 : 8784655 : if (processing_template_decl)
8858 : 1043979 : goto tmpl;
8859 : 7740676 : protected_set_expr_location (result, loc);
8860 : 7740676 : return result;
8861 : : }
8862 : :
8863 : 196 : if (complain & tf_error)
8864 : : {
8865 : 163 : auto_diagnostic_group d;
8866 : 163 : error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8867 : 163 : TREE_TYPE (expr), type);
8868 : 163 : if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8869 : 121 : && CLASS_TYPE_P (TREE_TYPE (type))
8870 : 192 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8871 : 17 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8872 : 17 : "class type %qT is incomplete", TREE_TYPE (type));
8873 : 163 : tree expr_type = TREE_TYPE (expr);
8874 : 163 : if (TYPE_PTR_P (expr_type))
8875 : 36 : expr_type = TREE_TYPE (expr_type);
8876 : 163 : if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8877 : 12 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8878 : : "class type %qT is incomplete", expr_type);
8879 : 163 : }
8880 : 196 : return error_mark_node;
8881 : : }
8882 : :
8883 : : /* EXPR is an expression with member function or pointer-to-member
8884 : : function type. TYPE is a pointer type. Converting EXPR to TYPE is
8885 : : not permitted by ISO C++, but we accept it in some modes. If we
8886 : : are not in one of those modes, issue a diagnostic. Return the
8887 : : converted expression. */
8888 : :
8889 : : tree
8890 : 197 : convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8891 : : {
8892 : 197 : tree intype;
8893 : 197 : tree decl;
8894 : :
8895 : 197 : intype = TREE_TYPE (expr);
8896 : 197 : gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8897 : : || TREE_CODE (intype) == METHOD_TYPE);
8898 : :
8899 : 197 : if (!(complain & tf_warning_or_error))
8900 : 0 : return error_mark_node;
8901 : :
8902 : 197 : location_t loc = cp_expr_loc_or_input_loc (expr);
8903 : :
8904 : 197 : if (pedantic || warn_pmf2ptr)
8905 : 271 : pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8906 : : "converting from %qH to %qI", intype, type);
8907 : :
8908 : 197 : STRIP_ANY_LOCATION_WRAPPER (expr);
8909 : :
8910 : 197 : if (TREE_CODE (intype) == METHOD_TYPE)
8911 : 88 : expr = build_addr_func (expr, complain);
8912 : 109 : else if (TREE_CODE (expr) == PTRMEM_CST)
8913 : 91 : expr = build_address (PTRMEM_CST_MEMBER (expr));
8914 : : else
8915 : : {
8916 : 18 : decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8917 : 18 : decl = build_address (decl);
8918 : 18 : expr = get_member_function_from_ptrfunc (&decl, expr, complain);
8919 : : }
8920 : :
8921 : 197 : if (expr == error_mark_node)
8922 : : return error_mark_node;
8923 : :
8924 : 197 : expr = build_nop (type, expr);
8925 : 197 : SET_EXPR_LOCATION (expr, loc);
8926 : 197 : return expr;
8927 : : }
8928 : :
8929 : : /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8930 : : constexpr evaluation knows to reject it. */
8931 : :
8932 : : static tree
8933 : 96781 : build_nop_reinterpret (tree type, tree expr)
8934 : : {
8935 : 96781 : tree ret = build_nop (type, expr);
8936 : 96781 : if (ret != expr)
8937 : 96781 : REINTERPRET_CAST_P (ret) = true;
8938 : 96781 : return ret;
8939 : : }
8940 : :
8941 : : /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8942 : : If C_CAST_P is true, this reinterpret cast is being done as part of
8943 : : a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8944 : : indicate whether or not reinterpret_cast was valid. */
8945 : :
8946 : : static tree
8947 : 1690338 : build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8948 : : bool c_cast_p, bool *valid_p,
8949 : : tsubst_flags_t complain)
8950 : : {
8951 : 1690338 : tree intype;
8952 : :
8953 : : /* Assume the cast is invalid. */
8954 : 1690338 : if (valid_p)
8955 : 1568416 : *valid_p = true;
8956 : :
8957 : 1690338 : if (type == error_mark_node || error_operand_p (expr))
8958 : : return error_mark_node;
8959 : :
8960 : 1690338 : intype = TREE_TYPE (expr);
8961 : :
8962 : : /* Save casted types in the function's used types hash table. */
8963 : 1690338 : used_types_insert (type);
8964 : :
8965 : : /* A prvalue of non-class type is cv-unqualified. */
8966 : 1690338 : if (!CLASS_TYPE_P (type))
8967 : 1690329 : type = cv_unqualified (type);
8968 : :
8969 : : /* [expr.reinterpret.cast]
8970 : : A glvalue of type T1, designating an object x, can be cast to the type
8971 : : "reference to T2" if an expression of type "pointer to T1" can be
8972 : : explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8973 : : The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8974 : : of type "pointer to T1". No temporary is created, no copy is made, and no
8975 : : constructors (11.4.4) or conversion functions (11.4.7) are called. */
8976 : 1690338 : if (TYPE_REF_P (type))
8977 : : {
8978 : 10143 : if (!glvalue_p (expr))
8979 : : {
8980 : 9 : if (complain & tf_error)
8981 : 9 : error_at (loc, "invalid cast of a prvalue expression of type "
8982 : : "%qT to type %qT",
8983 : : intype, type);
8984 : 9 : return error_mark_node;
8985 : : }
8986 : :
8987 : : /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8988 : : "B" are related class types; the reinterpret_cast does not
8989 : : adjust the pointer. */
8990 : 10134 : if (TYPE_PTR_P (intype)
8991 : 3 : && (complain & tf_warning)
8992 : 10137 : && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8993 : : COMPARE_BASE | COMPARE_DERIVED)))
8994 : 3 : warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
8995 : : intype, type);
8996 : :
8997 : 10134 : expr = cp_build_addr_expr (expr, complain);
8998 : :
8999 : 10134 : if (warn_strict_aliasing > 2)
9000 : 71 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9001 : :
9002 : 10134 : if (expr != error_mark_node)
9003 : 10134 : expr = build_reinterpret_cast_1
9004 : 10134 : (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
9005 : : valid_p, complain);
9006 : 10134 : if (expr != error_mark_node)
9007 : : /* cp_build_indirect_ref isn't right for rvalue refs. */
9008 : 10131 : expr = convert_from_reference (fold_convert (type, expr));
9009 : 10134 : return expr;
9010 : : }
9011 : :
9012 : : /* As a G++ extension, we consider conversions from member
9013 : : functions, and pointers to member functions to
9014 : : pointer-to-function and pointer-to-void types. If
9015 : : -Wno-pmf-conversions has not been specified,
9016 : : convert_member_func_to_ptr will issue an error message. */
9017 : 265 : if ((TYPE_PTRMEMFUNC_P (intype)
9018 : 1680007 : || TREE_CODE (intype) == METHOD_TYPE)
9019 : 276 : && TYPE_PTR_P (type)
9020 : 1680392 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
9021 : 155 : || VOID_TYPE_P (TREE_TYPE (type))))
9022 : 188 : return convert_member_func_to_ptr (type, expr, complain);
9023 : :
9024 : : /* If the cast is not to a reference type, the lvalue-to-rvalue,
9025 : : array-to-pointer, and function-to-pointer conversions are
9026 : : performed. */
9027 : 1680007 : expr = decay_conversion (expr, complain);
9028 : :
9029 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9030 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9031 : 1680007 : if (TREE_CODE (expr) == NOP_EXPR
9032 : 1680007 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9033 : 388 : expr = TREE_OPERAND (expr, 0);
9034 : :
9035 : 1680007 : if (error_operand_p (expr))
9036 : 30 : return error_mark_node;
9037 : :
9038 : 1679977 : intype = TREE_TYPE (expr);
9039 : :
9040 : : /* [expr.reinterpret.cast]
9041 : : A pointer can be converted to any integral type large enough to
9042 : : hold it. ... A value of type std::nullptr_t can be converted to
9043 : : an integral type; the conversion has the same meaning and
9044 : : validity as a conversion of (void*)0 to the integral type. */
9045 : 1679977 : if (CP_INTEGRAL_TYPE_P (type)
9046 : 146324 : && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
9047 : : {
9048 : 144592 : if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
9049 : : {
9050 : 21 : if (complain & tf_error)
9051 : 15 : permerror (loc, "cast from %qH to %qI loses precision",
9052 : : intype, type);
9053 : : else
9054 : 6 : return error_mark_node;
9055 : : }
9056 : 144586 : if (NULLPTR_TYPE_P (intype))
9057 : 98 : return build_int_cst (type, 0);
9058 : : }
9059 : : /* [expr.reinterpret.cast]
9060 : : A value of integral or enumeration type can be explicitly
9061 : : converted to a pointer. */
9062 : 1535385 : else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
9063 : : /* OK */
9064 : : ;
9065 : 1499226 : else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
9066 : 1497479 : || TYPE_PTR_OR_PTRMEM_P (type))
9067 : 1596515 : && same_type_p (type, intype))
9068 : : /* DR 799 */
9069 : 476 : return rvalue (expr);
9070 : 1498750 : else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
9071 : : {
9072 : 6837 : if ((complain & tf_warning)
9073 : 13674 : && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
9074 : 6837 : TREE_TYPE (intype)))
9075 : 145 : warning_at (loc, OPT_Wcast_function_type,
9076 : : "cast between incompatible function types"
9077 : : " from %qH to %qI", intype, type);
9078 : 6837 : return build_nop_reinterpret (type, expr);
9079 : : }
9080 : 1491913 : else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
9081 : : {
9082 : 79 : if ((complain & tf_warning)
9083 : 158 : && !cxx_safe_function_type_cast_p
9084 : 79 : (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
9085 : 79 : TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
9086 : 52 : warning_at (loc, OPT_Wcast_function_type,
9087 : : "cast between incompatible pointer to member types"
9088 : : " from %qH to %qI", intype, type);
9089 : 79 : return build_nop_reinterpret (type, expr);
9090 : : }
9091 : 21 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
9092 : 1491834 : || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
9093 : : {
9094 : 89610 : if (!c_cast_p
9095 : 89610 : && check_for_casting_away_constness (loc, intype, type,
9096 : : REINTERPRET_CAST_EXPR,
9097 : : complain))
9098 : 21 : return error_mark_node;
9099 : : /* Warn about possible alignment problems. */
9100 : 89589 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9101 : 24 : && (complain & tf_warning)
9102 : 24 : && !VOID_TYPE_P (type)
9103 : 24 : && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
9104 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (type))
9105 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (intype))
9106 : 89637 : && min_align_of_type (TREE_TYPE (type))
9107 : 24 : > min_align_of_type (TREE_TYPE (intype)))
9108 : 18 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9109 : : "increases required alignment of target type",
9110 : : intype, type);
9111 : :
9112 : 89589 : if (warn_strict_aliasing <= 2)
9113 : : /* strict_aliasing_warning STRIP_NOPs its expr. */
9114 : 78036 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9115 : :
9116 : 89589 : return build_nop_reinterpret (type, expr);
9117 : : }
9118 : 297 : else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
9119 : 1402384 : || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
9120 : : {
9121 : 276 : if (complain & tf_warning)
9122 : : /* C++11 5.2.10 p8 says that "Converting a function pointer to an
9123 : : object pointer type or vice versa is conditionally-supported." */
9124 : 276 : warning_at (loc, OPT_Wconditionally_supported,
9125 : : "casting between pointer-to-function and "
9126 : : "pointer-to-object is conditionally-supported");
9127 : 276 : return build_nop_reinterpret (type, expr);
9128 : : }
9129 : 1401948 : else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
9130 : 1400166 : return convert_to_vector (type, rvalue (expr));
9131 : 1782 : else if (gnu_vector_type_p (intype)
9132 : 1782 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9133 : 1681 : return convert_to_integer_nofold (type, expr);
9134 : : else
9135 : : {
9136 : 101 : if (valid_p)
9137 : 83 : *valid_p = false;
9138 : 101 : if (complain & tf_error)
9139 : 92 : error_at (loc, "invalid cast from type %qT to type %qT",
9140 : : intype, type);
9141 : 101 : return error_mark_node;
9142 : : }
9143 : :
9144 : 180647 : expr = cp_convert (type, expr, complain);
9145 : 180647 : if (TREE_CODE (expr) == NOP_EXPR)
9146 : : /* Mark any nop_expr that created as a reintepret_cast. */
9147 : 0 : REINTERPRET_CAST_P (expr) = true;
9148 : : return expr;
9149 : : }
9150 : :
9151 : : tree
9152 : 588763 : build_reinterpret_cast (location_t loc, tree type, tree expr,
9153 : : tsubst_flags_t complain)
9154 : : {
9155 : 588763 : tree r;
9156 : :
9157 : 588763 : if (type == error_mark_node || expr == error_mark_node)
9158 : : return error_mark_node;
9159 : :
9160 : 588756 : if (processing_template_decl)
9161 : : {
9162 : 476912 : tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
9163 : :
9164 : 476912 : if (!TREE_SIDE_EFFECTS (t)
9165 : 476912 : && type_dependent_expression_p (expr))
9166 : : /* There might turn out to be side effects inside expr. */
9167 : 200844 : TREE_SIDE_EFFECTS (t) = 1;
9168 : 476912 : r = convert_from_reference (t);
9169 : 476912 : protected_set_expr_location (r, loc);
9170 : 476912 : return r;
9171 : : }
9172 : :
9173 : 111844 : r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9174 : : /*valid_p=*/NULL, complain);
9175 : 111844 : if (r != error_mark_node)
9176 : : {
9177 : 111781 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9178 : 111781 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9179 : : }
9180 : 111844 : protected_set_expr_location (r, loc);
9181 : 111844 : return r;
9182 : : }
9183 : :
9184 : : /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9185 : : return an appropriate expression. Otherwise, return
9186 : : error_mark_node. If the cast is not valid, and COMPLAIN is true,
9187 : : then a diagnostic will be issued. If VALID_P is non-NULL, we are
9188 : : performing a C-style cast, its value upon return will indicate
9189 : : whether or not the conversion succeeded. */
9190 : :
9191 : : static tree
9192 : 36107548 : build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9193 : : tsubst_flags_t complain, bool *valid_p)
9194 : : {
9195 : 36107548 : tree src_type;
9196 : 36107548 : tree reference_type;
9197 : :
9198 : : /* Callers are responsible for handling error_mark_node as a
9199 : : destination type. */
9200 : 36107548 : gcc_assert (dst_type != error_mark_node);
9201 : : /* In a template, callers should be building syntactic
9202 : : representations of casts, not using this machinery. */
9203 : 36107548 : gcc_assert (!processing_template_decl);
9204 : :
9205 : : /* Assume the conversion is invalid. */
9206 : 36107548 : if (valid_p)
9207 : 35975761 : *valid_p = false;
9208 : :
9209 : 36107548 : if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9210 : : {
9211 : 35466448 : if (complain & tf_error)
9212 : 9 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9213 : : "which is not a pointer, reference, "
9214 : : "nor a pointer-to-data-member type", dst_type);
9215 : 35466448 : return error_mark_node;
9216 : : }
9217 : :
9218 : 641100 : if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
9219 : : {
9220 : 3949 : if (complain & tf_error)
9221 : 6 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9222 : : "which is a pointer or reference to a function type",
9223 : : dst_type);
9224 : 3949 : return error_mark_node;
9225 : : }
9226 : :
9227 : : /* A prvalue of non-class type is cv-unqualified. */
9228 : 637151 : dst_type = cv_unqualified (dst_type);
9229 : :
9230 : : /* Save casted types in the function's used types hash table. */
9231 : 637151 : used_types_insert (dst_type);
9232 : :
9233 : 637151 : src_type = TREE_TYPE (expr);
9234 : : /* Expressions do not really have reference types. */
9235 : 637151 : if (TYPE_REF_P (src_type))
9236 : 0 : src_type = TREE_TYPE (src_type);
9237 : :
9238 : : /* [expr.const.cast]
9239 : :
9240 : : For two object types T1 and T2, if a pointer to T1 can be explicitly
9241 : : converted to the type "pointer to T2" using a const_cast, then the
9242 : : following conversions can also be made:
9243 : :
9244 : : -- an lvalue of type T1 can be explicitly converted to an lvalue of
9245 : : type T2 using the cast const_cast<T2&>;
9246 : :
9247 : : -- a glvalue of type T1 can be explicitly converted to an xvalue of
9248 : : type T2 using the cast const_cast<T2&&>; and
9249 : :
9250 : : -- if T1 is a class type, a prvalue of type T1 can be explicitly
9251 : : converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9252 : :
9253 : 637151 : if (TYPE_REF_P (dst_type))
9254 : : {
9255 : 62391 : reference_type = dst_type;
9256 : 62391 : if (!TYPE_REF_IS_RVALUE (dst_type)
9257 : 62391 : ? lvalue_p (expr)
9258 : 990 : : obvalue_p (expr))
9259 : : /* OK. */;
9260 : : else
9261 : : {
9262 : 35 : if (complain & tf_error)
9263 : 9 : error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
9264 : : "to type %qT",
9265 : : src_type, dst_type);
9266 : 35 : return error_mark_node;
9267 : : }
9268 : 62356 : dst_type = build_pointer_type (TREE_TYPE (dst_type));
9269 : 62356 : src_type = build_pointer_type (src_type);
9270 : : }
9271 : : else
9272 : : {
9273 : 574760 : reference_type = NULL_TREE;
9274 : : /* If the destination type is not a reference type, the
9275 : : lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9276 : : conversions are performed. */
9277 : 574760 : src_type = type_decays_to (src_type);
9278 : 574760 : if (src_type == error_mark_node)
9279 : : return error_mark_node;
9280 : : }
9281 : :
9282 : 637116 : if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9283 : : {
9284 : 486563 : if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9285 : : {
9286 : 251070 : if (valid_p)
9287 : : {
9288 : 119361 : *valid_p = true;
9289 : : /* This cast is actually a C-style cast. Issue a warning if
9290 : : the user is making a potentially unsafe cast. */
9291 : 119361 : check_for_casting_away_constness (loc, src_type, dst_type,
9292 : : CAST_EXPR, complain);
9293 : : /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9294 : 119361 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9295 : 3 : && (complain & tf_warning)
9296 : 119367 : && min_align_of_type (TREE_TYPE (dst_type))
9297 : 3 : > min_align_of_type (TREE_TYPE (src_type)))
9298 : 3 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9299 : : "increases required alignment of target type",
9300 : : src_type, dst_type);
9301 : : }
9302 : 251070 : if (reference_type)
9303 : : {
9304 : 61397 : expr = cp_build_addr_expr (expr, complain);
9305 : 61397 : if (expr == error_mark_node)
9306 : : return error_mark_node;
9307 : 61379 : expr = build_nop (reference_type, expr);
9308 : 61379 : return convert_from_reference (expr);
9309 : : }
9310 : : else
9311 : : {
9312 : 189673 : expr = decay_conversion (expr, complain);
9313 : 189673 : if (expr == error_mark_node)
9314 : : return error_mark_node;
9315 : :
9316 : : /* build_c_cast puts on a NOP_EXPR to make the result not an
9317 : : lvalue. Strip such NOP_EXPRs if VALUE is being used in
9318 : : non-lvalue context. */
9319 : 189673 : if (TREE_CODE (expr) == NOP_EXPR
9320 : 189673 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9321 : 3 : expr = TREE_OPERAND (expr, 0);
9322 : 189673 : return build_nop (dst_type, expr);
9323 : : }
9324 : : }
9325 : 235493 : else if (valid_p
9326 : 470938 : && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9327 : 235445 : TREE_TYPE (src_type)))
9328 : 12971 : check_for_casting_away_constness (loc, src_type, dst_type,
9329 : : CAST_EXPR, complain);
9330 : : }
9331 : :
9332 : 386046 : if (complain & tf_error)
9333 : 30 : error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9334 : : src_type, dst_type);
9335 : 386046 : return error_mark_node;
9336 : : }
9337 : :
9338 : : tree
9339 : 563925 : build_const_cast (location_t loc, tree type, tree expr,
9340 : : tsubst_flags_t complain)
9341 : : {
9342 : 563925 : tree r;
9343 : :
9344 : 563925 : if (type == error_mark_node || error_operand_p (expr))
9345 : : return error_mark_node;
9346 : :
9347 : 563917 : if (processing_template_decl)
9348 : : {
9349 : 432130 : tree t = build_min (CONST_CAST_EXPR, type, expr);
9350 : :
9351 : 432130 : if (!TREE_SIDE_EFFECTS (t)
9352 : 432130 : && type_dependent_expression_p (expr))
9353 : : /* There might turn out to be side effects inside expr. */
9354 : 329213 : TREE_SIDE_EFFECTS (t) = 1;
9355 : 432130 : r = convert_from_reference (t);
9356 : 432130 : protected_set_expr_location (r, loc);
9357 : 432130 : return r;
9358 : : }
9359 : :
9360 : 131787 : r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9361 : 131787 : if (r != error_mark_node)
9362 : : {
9363 : 131709 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9364 : 131709 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9365 : : }
9366 : 131787 : protected_set_expr_location (r, loc);
9367 : 131787 : return r;
9368 : : }
9369 : :
9370 : : /* Like cp_build_c_cast, but for the c-common bits. */
9371 : :
9372 : : tree
9373 : 0 : build_c_cast (location_t loc, tree type, tree expr)
9374 : : {
9375 : 0 : return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9376 : : }
9377 : :
9378 : : /* Like the "build_c_cast" used for c-common, but using cp_expr to
9379 : : preserve location information even for tree nodes that don't
9380 : : support it. */
9381 : :
9382 : : cp_expr
9383 : 8686867 : build_c_cast (location_t loc, tree type, cp_expr expr)
9384 : : {
9385 : 8686867 : cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9386 : 8686867 : result.set_location (loc);
9387 : 8686867 : return result;
9388 : : }
9389 : :
9390 : : /* Build an expression representing an explicit C-style cast to type
9391 : : TYPE of expression EXPR. */
9392 : :
9393 : : tree
9394 : 37881826 : cp_build_c_cast (location_t loc, tree type, tree expr,
9395 : : tsubst_flags_t complain)
9396 : : {
9397 : 37881826 : tree value = expr;
9398 : 37881826 : tree result;
9399 : 37881826 : bool valid_p;
9400 : :
9401 : 37881826 : if (type == error_mark_node || error_operand_p (expr))
9402 : : return error_mark_node;
9403 : :
9404 : 37881653 : if (processing_template_decl)
9405 : : {
9406 : 1905883 : tree t = build_min (CAST_EXPR, type,
9407 : : tree_cons (NULL_TREE, value, NULL_TREE));
9408 : : /* We don't know if it will or will not have side effects. */
9409 : 1905883 : TREE_SIDE_EFFECTS (t) = 1;
9410 : 1905883 : return convert_from_reference (t);
9411 : : }
9412 : :
9413 : : /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9414 : : 'Class') should always be retained, because this information aids
9415 : : in method lookup. */
9416 : 35975770 : if (objc_is_object_ptr (type)
9417 : 35975770 : && objc_is_object_ptr (TREE_TYPE (expr)))
9418 : 0 : return build_nop (type, expr);
9419 : :
9420 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9421 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9422 : 35975770 : if (!TYPE_REF_P (type)
9423 : 35974192 : && TREE_CODE (value) == NOP_EXPR
9424 : 36213351 : && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9425 : 109184 : value = TREE_OPERAND (value, 0);
9426 : :
9427 : 35975770 : if (TREE_CODE (type) == ARRAY_TYPE)
9428 : : {
9429 : : /* Allow casting from T1* to T2[] because Cfront allows it.
9430 : : NIHCL uses it. It is not valid ISO C++ however. */
9431 : 3 : if (TYPE_PTR_P (TREE_TYPE (expr)))
9432 : : {
9433 : 0 : if (complain & tf_error)
9434 : 0 : permerror (loc, "ISO C++ forbids casting to an array type %qT",
9435 : : type);
9436 : : else
9437 : 0 : return error_mark_node;
9438 : 0 : type = build_pointer_type (TREE_TYPE (type));
9439 : : }
9440 : : else
9441 : : {
9442 : 3 : if (complain & tf_error)
9443 : 3 : error_at (loc, "ISO C++ forbids casting to an array type %qT",
9444 : : type);
9445 : 3 : return error_mark_node;
9446 : : }
9447 : : }
9448 : :
9449 : 35975767 : if (FUNC_OR_METHOD_TYPE_P (type))
9450 : : {
9451 : 15 : if (complain & tf_error)
9452 : 15 : error_at (loc, "invalid cast to function type %qT", type);
9453 : 15 : return error_mark_node;
9454 : : }
9455 : :
9456 : 35975752 : if (TYPE_PTR_P (type)
9457 : 507598 : && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9458 : : /* Casting to an integer of smaller size is an error detected elsewhere. */
9459 : 148969 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9460 : : /* Don't warn about converting any constant. */
9461 : 36069044 : && !TREE_CONSTANT (value))
9462 : 47 : warning_at (loc, OPT_Wint_to_pointer_cast,
9463 : : "cast to pointer from integer of different size");
9464 : :
9465 : : /* A C-style cast can be a const_cast. */
9466 : 35975752 : result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9467 : : &valid_p);
9468 : 35975752 : if (valid_p)
9469 : : {
9470 : 119352 : if (result != error_mark_node)
9471 : : {
9472 : 119343 : maybe_warn_about_useless_cast (loc, type, value, complain);
9473 : 119343 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9474 : : }
9475 : 9 : else if (complain & tf_error)
9476 : 9 : build_const_cast_1 (loc, type, value, tf_error, &valid_p);
9477 : 119352 : return result;
9478 : : }
9479 : :
9480 : : /* Or a static cast. */
9481 : 35856400 : result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9482 : : &valid_p, complain);
9483 : : /* Or a reinterpret_cast. */
9484 : 35856400 : if (!valid_p)
9485 : 1568360 : result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9486 : : &valid_p, complain);
9487 : : /* The static_cast or reinterpret_cast may be followed by a
9488 : : const_cast. */
9489 : 35856400 : if (valid_p
9490 : : /* A valid cast may result in errors if, for example, a
9491 : : conversion to an ambiguous base class is required. */
9492 : 35856400 : && !error_operand_p (result))
9493 : : {
9494 : 35855999 : tree result_type;
9495 : :
9496 : 35855999 : maybe_warn_about_useless_cast (loc, type, value, complain);
9497 : 35855999 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9498 : :
9499 : : /* Non-class rvalues always have cv-unqualified type. */
9500 : 35855999 : if (!CLASS_TYPE_P (type))
9501 : 34420351 : type = TYPE_MAIN_VARIANT (type);
9502 : 35855999 : result_type = TREE_TYPE (result);
9503 : 35855999 : if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9504 : 34419370 : result_type = TYPE_MAIN_VARIANT (result_type);
9505 : : /* If the type of RESULT does not match TYPE, perform a
9506 : : const_cast to make it match. If the static_cast or
9507 : : reinterpret_cast succeeded, we will differ by at most
9508 : : cv-qualification, so the follow-on const_cast is guaranteed
9509 : : to succeed. */
9510 : 35855999 : if (!same_type_p (non_reference (type), non_reference (result_type)))
9511 : : {
9512 : 0 : result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
9513 : 0 : gcc_assert (valid_p);
9514 : : }
9515 : 35855999 : return result;
9516 : : }
9517 : :
9518 : 401 : return error_mark_node;
9519 : : }
9520 : :
9521 : : /* Warn when a value is moved to itself with std::move. LHS is the target,
9522 : : RHS may be the std::move call, and LOC is the location of the whole
9523 : : assignment. Return true if we warned. */
9524 : :
9525 : : bool
9526 : 24944060 : maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9527 : : {
9528 : 24944060 : if (!warn_self_move)
9529 : : return false;
9530 : :
9531 : : /* C++98 doesn't know move. */
9532 : 349647 : if (cxx_dialect < cxx11)
9533 : : return false;
9534 : :
9535 : 329824 : if (processing_template_decl)
9536 : : return false;
9537 : :
9538 : 23027 : if (!REFERENCE_REF_P (rhs)
9539 : 284823 : || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9540 : : return false;
9541 : 4666 : tree fn = TREE_OPERAND (rhs, 0);
9542 : 4666 : if (!is_std_move_p (fn))
9543 : : return false;
9544 : :
9545 : : /* Just a little helper to strip * and various NOPs. */
9546 : 6210 : auto extract_op = [] (tree &op) {
9547 : 4140 : STRIP_NOPS (op);
9548 : 5342 : while (INDIRECT_REF_P (op))
9549 : 1202 : op = TREE_OPERAND (op, 0);
9550 : 4140 : op = maybe_undo_parenthesized_ref (op);
9551 : 4140 : STRIP_ANY_LOCATION_WRAPPER (op);
9552 : 4140 : };
9553 : :
9554 : 2070 : tree arg = CALL_EXPR_ARG (fn, 0);
9555 : 2070 : extract_op (arg);
9556 : 2070 : if (TREE_CODE (arg) == ADDR_EXPR)
9557 : 1406 : arg = TREE_OPERAND (arg, 0);
9558 : 2070 : tree type = TREE_TYPE (lhs);
9559 : 2070 : tree orig_lhs = lhs;
9560 : 2070 : extract_op (lhs);
9561 : 2070 : if (cp_tree_equal (lhs, arg)
9562 : : /* Also warn in a member-initializer-list, as in : i(std::move(i)). */
9563 : 2070 : || (TREE_CODE (lhs) == FIELD_DECL
9564 : 549 : && TREE_CODE (arg) == COMPONENT_REF
9565 : 294 : && cp_tree_equal (TREE_OPERAND (arg, 0), current_class_ref)
9566 : 6 : && TREE_OPERAND (arg, 1) == lhs))
9567 : 117 : if (warning_at (loc, OPT_Wself_move,
9568 : : "moving %qE of type %qT to itself", orig_lhs, type))
9569 : : return true;
9570 : :
9571 : : return false;
9572 : : }
9573 : :
9574 : : /* For use from the C common bits. */
9575 : : tree
9576 : 4766 : build_modify_expr (location_t location,
9577 : : tree lhs, tree /*lhs_origtype*/,
9578 : : enum tree_code modifycode,
9579 : : location_t /*rhs_location*/, tree rhs,
9580 : : tree /*rhs_origtype*/)
9581 : : {
9582 : 4766 : return cp_build_modify_expr (location, lhs, modifycode, rhs,
9583 : 4766 : tf_warning_or_error);
9584 : : }
9585 : :
9586 : : /* Build an assignment expression of lvalue LHS from value RHS.
9587 : : MODIFYCODE is the code for a binary operator that we use
9588 : : to combine the old value of LHS with RHS to get the new value.
9589 : : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9590 : :
9591 : : C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9592 : :
9593 : : tree
9594 : 32377178 : cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9595 : : tree rhs, tsubst_flags_t complain)
9596 : : {
9597 : 32377178 : lhs = mark_lvalue_use_nonread (lhs);
9598 : :
9599 : 32377178 : tree result = NULL_TREE;
9600 : 32377178 : tree newrhs = rhs;
9601 : 32377178 : tree lhstype = TREE_TYPE (lhs);
9602 : 32377178 : tree olhs = lhs;
9603 : 32377178 : tree olhstype = lhstype;
9604 : 32377178 : bool plain_assign = (modifycode == NOP_EXPR);
9605 : 32377178 : bool compound_side_effects_p = false;
9606 : 32377178 : tree preeval = NULL_TREE;
9607 : :
9608 : : /* Avoid duplicate error messages from operands that had errors. */
9609 : 32377178 : if (error_operand_p (lhs) || error_operand_p (rhs))
9610 : 20 : return error_mark_node;
9611 : :
9612 : 32377240 : while (TREE_CODE (lhs) == COMPOUND_EXPR)
9613 : : {
9614 : 82 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9615 : 70 : compound_side_effects_p = true;
9616 : 82 : lhs = TREE_OPERAND (lhs, 1);
9617 : : }
9618 : :
9619 : : /* Handle control structure constructs used as "lvalues". Note that we
9620 : : leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9621 : 32377158 : switch (TREE_CODE (lhs))
9622 : : {
9623 : : /* Handle --foo = 5; as these are valid constructs in C++. */
9624 : 21 : case PREDECREMENT_EXPR:
9625 : 21 : case PREINCREMENT_EXPR:
9626 : 21 : if (compound_side_effects_p)
9627 : 9 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9628 : 21 : lhs = genericize_compound_lvalue (lhs);
9629 : 149 : maybe_add_compound:
9630 : : /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9631 : : and looked through the COMPOUND_EXPRs, readd them now around
9632 : : the resulting lhs. */
9633 : 149 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9634 : : {
9635 : 9 : lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9636 : 9 : tree *ptr = &TREE_OPERAND (lhs, 1);
9637 : 9 : for (olhs = TREE_OPERAND (olhs, 1);
9638 : 12 : TREE_CODE (olhs) == COMPOUND_EXPR;
9639 : 3 : olhs = TREE_OPERAND (olhs, 1))
9640 : : {
9641 : 3 : *ptr = build2 (COMPOUND_EXPR, lhstype,
9642 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9643 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9644 : : }
9645 : : }
9646 : : break;
9647 : :
9648 : 128 : case MODIFY_EXPR:
9649 : 128 : if (compound_side_effects_p)
9650 : 0 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9651 : 128 : lhs = genericize_compound_lvalue (lhs);
9652 : 128 : goto maybe_add_compound;
9653 : :
9654 : 0 : case MIN_EXPR:
9655 : 0 : case MAX_EXPR:
9656 : : /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9657 : : when neither operand has side-effects. */
9658 : 0 : if (!lvalue_or_else (lhs, lv_assign, complain))
9659 : 0 : return error_mark_node;
9660 : :
9661 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9662 : : && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9663 : :
9664 : 0 : lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9665 : 0 : build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9666 : : boolean_type_node,
9667 : 0 : TREE_OPERAND (lhs, 0),
9668 : 0 : TREE_OPERAND (lhs, 1)),
9669 : 0 : TREE_OPERAND (lhs, 0),
9670 : 0 : TREE_OPERAND (lhs, 1));
9671 : 206 : gcc_fallthrough ();
9672 : :
9673 : : /* Handle (a ? b : c) used as an "lvalue". */
9674 : 206 : case COND_EXPR:
9675 : 206 : {
9676 : : /* Produce (a ? (b = rhs) : (c = rhs))
9677 : : except that the RHS goes through a save-expr
9678 : : so the code to compute it is only emitted once. */
9679 : 206 : if (VOID_TYPE_P (TREE_TYPE (rhs)))
9680 : : {
9681 : 18 : if (complain & tf_error)
9682 : 24 : error_at (cp_expr_loc_or_loc (rhs, loc),
9683 : : "void value not ignored as it ought to be");
9684 : 18 : return error_mark_node;
9685 : : }
9686 : :
9687 : 188 : rhs = stabilize_expr (rhs, &preeval);
9688 : :
9689 : : /* Check this here to avoid odd errors when trying to convert
9690 : : a throw to the type of the COND_EXPR. */
9691 : 188 : if (!lvalue_or_else (lhs, lv_assign, complain))
9692 : 6 : return error_mark_node;
9693 : :
9694 : 182 : tree op1 = TREE_OPERAND (lhs, 1);
9695 : 182 : if (TREE_CODE (op1) != THROW_EXPR)
9696 : 176 : op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
9697 : : /* When sanitizing undefined behavior, even when rhs doesn't need
9698 : : stabilization at this point, the sanitization might add extra
9699 : : SAVE_EXPRs in there and so make sure there is no tree sharing
9700 : : in the rhs, otherwise those SAVE_EXPRs will have initialization
9701 : : only in one of the two branches. */
9702 : 182 : if (sanitize_flags_p (SANITIZE_UNDEFINED
9703 : : | SANITIZE_UNDEFINED_NONDEFAULT))
9704 : 3 : rhs = unshare_expr (rhs);
9705 : 182 : tree op2 = TREE_OPERAND (lhs, 2);
9706 : 182 : if (TREE_CODE (op2) != THROW_EXPR)
9707 : 176 : op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9708 : 182 : tree cond = build_conditional_expr (input_location,
9709 : 182 : TREE_OPERAND (lhs, 0), op1, op2,
9710 : : complain);
9711 : :
9712 : 182 : if (cond == error_mark_node)
9713 : : return cond;
9714 : : /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9715 : : and looked through the COMPOUND_EXPRs, readd them now around
9716 : : the resulting cond before adding the preevaluated rhs. */
9717 : 179 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9718 : : {
9719 : 18 : cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9720 : 18 : TREE_OPERAND (olhs, 0), cond);
9721 : 18 : tree *ptr = &TREE_OPERAND (cond, 1);
9722 : 18 : for (olhs = TREE_OPERAND (olhs, 1);
9723 : 21 : TREE_CODE (olhs) == COMPOUND_EXPR;
9724 : 3 : olhs = TREE_OPERAND (olhs, 1))
9725 : : {
9726 : 3 : *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9727 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9728 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9729 : : }
9730 : : }
9731 : : /* Make sure the code to compute the rhs comes out
9732 : : before the split. */
9733 : 179 : result = cond;
9734 : 179 : goto ret;
9735 : : }
9736 : :
9737 : : default:
9738 : : lhs = olhs;
9739 : : break;
9740 : : }
9741 : :
9742 : 32376952 : if (modifycode == INIT_EXPR)
9743 : : {
9744 : 3337917 : if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9745 : : /* Do the default thing. */;
9746 : 3160631 : else if (TREE_CODE (rhs) == CONSTRUCTOR)
9747 : : {
9748 : : /* Compound literal. */
9749 : 169 : if (! same_type_p (TREE_TYPE (rhs), lhstype))
9750 : : /* Call convert to generate an error; see PR 11063. */
9751 : 0 : rhs = convert (lhstype, rhs);
9752 : 169 : result = cp_build_init_expr (lhs, rhs);
9753 : 169 : TREE_SIDE_EFFECTS (result) = 1;
9754 : 169 : goto ret;
9755 : : }
9756 : 3160462 : else if (! MAYBE_CLASS_TYPE_P (lhstype))
9757 : : /* Do the default thing. */;
9758 : : else
9759 : : {
9760 : 43031 : releasing_vec rhs_vec = make_tree_vector_single (rhs);
9761 : 43031 : result = build_special_member_call (lhs, complete_ctor_identifier,
9762 : : &rhs_vec, lhstype, LOOKUP_NORMAL,
9763 : : complain);
9764 : 43031 : if (result == NULL_TREE)
9765 : 0 : return error_mark_node;
9766 : 43031 : goto ret;
9767 : 43031 : }
9768 : : }
9769 : : else
9770 : : {
9771 : 29039035 : lhs = require_complete_type (lhs, complain);
9772 : 29039035 : if (lhs == error_mark_node)
9773 : : return error_mark_node;
9774 : :
9775 : 29038992 : if (modifycode == NOP_EXPR)
9776 : : {
9777 : 24896950 : maybe_warn_self_move (loc, lhs, rhs);
9778 : :
9779 : 24896950 : if (c_dialect_objc ())
9780 : : {
9781 : 0 : result = objc_maybe_build_modify_expr (lhs, rhs);
9782 : 0 : if (result)
9783 : 0 : goto ret;
9784 : : }
9785 : :
9786 : : /* `operator=' is not an inheritable operator. */
9787 : 24896950 : if (! MAYBE_CLASS_TYPE_P (lhstype))
9788 : : /* Do the default thing. */;
9789 : : else
9790 : : {
9791 : 2428832 : result = build_new_op (input_location, MODIFY_EXPR,
9792 : : LOOKUP_NORMAL, lhs, rhs,
9793 : : make_node (NOP_EXPR), NULL_TREE,
9794 : : /*overload=*/NULL, complain);
9795 : 2428832 : if (result == NULL_TREE)
9796 : 0 : return error_mark_node;
9797 : 2428832 : goto ret;
9798 : : }
9799 : : lhstype = olhstype;
9800 : : }
9801 : : else
9802 : : {
9803 : 4142042 : tree init = NULL_TREE;
9804 : :
9805 : : /* A binary op has been requested. Combine the old LHS
9806 : : value with the RHS producing the value we should actually
9807 : : store into the LHS. */
9808 : 4142042 : gcc_assert (!((TYPE_REF_P (lhstype)
9809 : : && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9810 : : || MAYBE_CLASS_TYPE_P (lhstype)));
9811 : :
9812 : : /* Preevaluate the RHS to make sure its evaluation is complete
9813 : : before the lvalue-to-rvalue conversion of the LHS:
9814 : :
9815 : : [expr.ass] With respect to an indeterminately-sequenced
9816 : : function call, the operation of a compound assignment is a
9817 : : single evaluation. [ Note: Therefore, a function call shall
9818 : : not intervene between the lvalue-to-rvalue conversion and the
9819 : : side effect associated with any single compound assignment
9820 : : operator. -- end note ] */
9821 : 4142042 : lhs = cp_stabilize_reference (lhs);
9822 : 4142042 : rhs = decay_conversion (rhs, complain);
9823 : 4142042 : if (rhs == error_mark_node)
9824 : 123 : return error_mark_node;
9825 : :
9826 : 4142039 : {
9827 : 4142039 : auto_diagnostic_group d;
9828 : 4142039 : rhs = stabilize_expr (rhs, &init);
9829 : 4142039 : bool clear_decl_read = false;
9830 : 4142039 : tree stripped_lhs = tree_strip_any_location_wrapper (lhs);
9831 : 1041872 : if ((VAR_P (stripped_lhs) || TREE_CODE (stripped_lhs) == PARM_DECL)
9832 : 3563120 : && !DECL_READ_P (stripped_lhs)
9833 : 950304 : && (VAR_P (stripped_lhs) ? warn_unused_but_set_variable
9834 : : : warn_unused_but_set_parameter) > 2
9835 : 6869 : && !CLASS_TYPE_P (TREE_TYPE (lhs))
9836 : 4148908 : && !CLASS_TYPE_P (TREE_TYPE (rhs)))
9837 : : {
9838 : 6869 : mark_exp_read (rhs);
9839 : 6869 : if (!DECL_READ_P (stripped_lhs))
9840 : 4142039 : clear_decl_read = true;
9841 : : }
9842 : 4142039 : newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9843 : 4142039 : if (clear_decl_read)
9844 : 6869 : DECL_READ_P (stripped_lhs) = 0;
9845 : 4142039 : if (newrhs == error_mark_node)
9846 : : {
9847 : 120 : if (complain & tf_error)
9848 : 24 : inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9849 : 24 : modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9850 : 120 : return error_mark_node;
9851 : : }
9852 : 4142039 : }
9853 : :
9854 : 4141919 : if (init)
9855 : 280851 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9856 : :
9857 : : /* Now it looks like a plain assignment. */
9858 : 4141919 : modifycode = NOP_EXPR;
9859 : 4141919 : if (c_dialect_objc ())
9860 : : {
9861 : 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
9862 : 0 : if (result)
9863 : 0 : goto ret;
9864 : : }
9865 : : }
9866 : 26610037 : gcc_assert (!TYPE_REF_P (lhstype));
9867 : 26610037 : gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9868 : : }
9869 : :
9870 : : /* The left-hand side must be an lvalue. */
9871 : 29904754 : if (!lvalue_or_else (lhs, lv_assign, complain))
9872 : 166 : return error_mark_node;
9873 : :
9874 : : /* Warn about modifying something that is `const'. Don't warn if
9875 : : this is initialization. */
9876 : 29904588 : if (modifycode != INIT_EXPR
9877 : 29904588 : && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9878 : : /* Functions are not modifiable, even though they are
9879 : : lvalues. */
9880 : 26557558 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9881 : : /* If it's an aggregate and any field is const, then it is
9882 : : effectively const. */
9883 : 26557528 : || (CLASS_TYPE_P (lhstype)
9884 : 0 : && C_TYPE_FIELDS_READONLY (lhstype))))
9885 : : {
9886 : 52343 : if (complain & tf_error)
9887 : 88 : cxx_readonly_error (loc, lhs, lv_assign);
9888 : 52343 : return error_mark_node;
9889 : : }
9890 : :
9891 : : /* If storing into a structure or union member, it may have been given a
9892 : : lowered bitfield type. We need to convert to the declared type first,
9893 : : so retrieve it now. */
9894 : :
9895 : 29852245 : olhstype = unlowered_expr_type (lhs);
9896 : :
9897 : : /* Convert new value to destination type. */
9898 : :
9899 : 29852245 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
9900 : : {
9901 : 185 : int from_array;
9902 : :
9903 : 185 : if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9904 : : {
9905 : 12 : if (modifycode != INIT_EXPR)
9906 : : {
9907 : 12 : if (complain & tf_error)
9908 : 12 : error_at (loc,
9909 : : "assigning to an array from an initializer list");
9910 : 12 : return error_mark_node;
9911 : : }
9912 : 0 : if (check_array_initializer (lhs, lhstype, newrhs))
9913 : 0 : return error_mark_node;
9914 : 0 : newrhs = digest_init (lhstype, newrhs, complain);
9915 : 0 : if (newrhs == error_mark_node)
9916 : : return error_mark_node;
9917 : : }
9918 : :
9919 : : /* C++11 8.5/17: "If the destination type is an array of characters,
9920 : : an array of char16_t, an array of char32_t, or an array of wchar_t,
9921 : : and the initializer is a string literal...". */
9922 : 173 : else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9923 : : == STRING_CST)
9924 : 43 : && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9925 : 216 : && modifycode == INIT_EXPR)
9926 : : {
9927 : 28 : newrhs = digest_init (lhstype, newrhs, complain);
9928 : 28 : if (newrhs == error_mark_node)
9929 : : return error_mark_node;
9930 : : }
9931 : :
9932 : 145 : else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9933 : : TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9934 : : {
9935 : 20 : if (complain & tf_error)
9936 : 15 : error_at (loc, "incompatible types in assignment of %qT to %qT",
9937 : 15 : TREE_TYPE (rhs), lhstype);
9938 : 20 : return error_mark_node;
9939 : : }
9940 : :
9941 : : /* Allow array assignment in compiler-generated code. */
9942 : 125 : else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
9943 : : /* OK, used by coroutines (co-await-initlist1.C). */;
9944 : 119 : else if (!current_function_decl
9945 : 119 : || !DECL_DEFAULTED_FN (current_function_decl))
9946 : : {
9947 : : /* This routine is used for both initialization and assignment.
9948 : : Make sure the diagnostic message differentiates the context. */
9949 : 72 : if (complain & tf_error)
9950 : : {
9951 : 30 : if (modifycode == INIT_EXPR)
9952 : 9 : error_at (loc, "array used as initializer");
9953 : : else
9954 : 21 : error_at (loc, "invalid array assignment");
9955 : : }
9956 : 72 : return error_mark_node;
9957 : : }
9958 : :
9959 : 128 : from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9960 : 81 : ? 1 + (modifycode != INIT_EXPR) : 0;
9961 : 81 : result = build_vec_init (lhs, NULL_TREE, newrhs,
9962 : : /*explicit_value_init_p=*/false,
9963 : : from_array, complain);
9964 : 81 : goto ret;
9965 : : }
9966 : :
9967 : 29852060 : if (modifycode == INIT_EXPR)
9968 : : /* Calls with INIT_EXPR are all direct-initialization, so don't set
9969 : : LOOKUP_ONLYCONVERTING. */
9970 : 3294655 : newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9971 : : ICR_INIT, NULL_TREE, 0,
9972 : : complain | tf_no_cleanup);
9973 : : else
9974 : 26557405 : newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9975 : : NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9976 : :
9977 : 29852060 : if (!same_type_p (lhstype, olhstype))
9978 : 250435 : newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9979 : :
9980 : 29852060 : if (modifycode != INIT_EXPR)
9981 : : {
9982 : 26557405 : if (TREE_CODE (newrhs) == CALL_EXPR
9983 : 26557405 : && TYPE_NEEDS_CONSTRUCTING (lhstype))
9984 : 0 : newrhs = build_cplus_new (lhstype, newrhs, complain);
9985 : :
9986 : : /* Can't initialize directly from a TARGET_EXPR, since that would
9987 : : cause the lhs to be constructed twice, and possibly result in
9988 : : accidental self-initialization. So we force the TARGET_EXPR to be
9989 : : expanded without a target. */
9990 : 26557405 : if (TREE_CODE (newrhs) == TARGET_EXPR)
9991 : 84 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9992 : 84 : TARGET_EXPR_SLOT (newrhs));
9993 : : }
9994 : :
9995 : 29852060 : if (newrhs == error_mark_node)
9996 : : return error_mark_node;
9997 : :
9998 : 29851492 : if (c_dialect_objc () && flag_objc_gc)
9999 : : {
10000 : 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
10001 : :
10002 : 0 : if (result)
10003 : 0 : goto ret;
10004 : : }
10005 : :
10006 : 33146098 : result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
10007 : : lhstype, lhs, newrhs);
10008 : 29851492 : if (modifycode == INIT_EXPR)
10009 : 3294606 : set_target_expr_eliding (newrhs);
10010 : :
10011 : 29851492 : TREE_SIDE_EFFECTS (result) = 1;
10012 : 29851492 : if (!plain_assign)
10013 : 7436495 : suppress_warning (result, OPT_Wparentheses);
10014 : :
10015 : 22414997 : ret:
10016 : 32323784 : if (preeval)
10017 : 33 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
10018 : : return result;
10019 : : }
10020 : :
10021 : : cp_expr
10022 : 59466303 : build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
10023 : : tree rhs, tree lookups, tsubst_flags_t complain)
10024 : : {
10025 : 59466303 : tree orig_lhs = lhs;
10026 : 59466303 : tree orig_rhs = rhs;
10027 : 59466303 : tree overload = NULL_TREE;
10028 : :
10029 : 59466303 : if (lhs == error_mark_node || rhs == error_mark_node)
10030 : 2093 : return cp_expr (error_mark_node, loc);
10031 : :
10032 : 59464210 : tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
10033 : :
10034 : 59464210 : if (processing_template_decl)
10035 : : {
10036 : 40033135 : if (type_dependent_expression_p (lhs)
10037 : 40033135 : || type_dependent_expression_p (rhs))
10038 : : {
10039 : 30821996 : tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
10040 : 30821996 : if (modifycode != NOP_EXPR)
10041 : 5783225 : TREE_TYPE (rval)
10042 : 11566450 : = build_dependent_operator_type (lookups, modifycode, true);
10043 : 30821996 : return rval;
10044 : : }
10045 : : }
10046 : :
10047 : 28642214 : tree rval;
10048 : 28642214 : if (modifycode == NOP_EXPR)
10049 : 22819794 : rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
10050 : : else
10051 : 5822420 : rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
10052 : : lhs, rhs, op, lookups, &overload, complain);
10053 : 28642214 : if (rval == error_mark_node)
10054 : 2005 : return error_mark_node;
10055 : 28640209 : if (processing_template_decl)
10056 : : {
10057 : 9211118 : if (overload != NULL_TREE)
10058 : 1234430 : return (build_min_non_dep_op_overload
10059 : 1234430 : (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
10060 : :
10061 : 7976688 : return (build_min_non_dep
10062 : 7976688 : (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
10063 : : }
10064 : 19429091 : return rval;
10065 : : }
10066 : :
10067 : : /* Helper function for get_delta_difference which assumes FROM is a base
10068 : : class of TO. Returns a delta for the conversion of pointer-to-member
10069 : : of FROM to pointer-to-member of TO. If the conversion is invalid and
10070 : : tf_error is not set in COMPLAIN returns error_mark_node, otherwise
10071 : : returns zero. If FROM is not a base class of TO, returns NULL_TREE.
10072 : : If C_CAST_P is true, this conversion is taking place as part of a
10073 : : C-style cast. */
10074 : :
10075 : : static tree
10076 : 30050 : get_delta_difference_1 (tree from, tree to, bool c_cast_p,
10077 : : tsubst_flags_t complain)
10078 : : {
10079 : 30050 : tree binfo;
10080 : 30050 : base_kind kind;
10081 : :
10082 : 59850 : binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
10083 : : &kind, complain);
10084 : :
10085 : 30050 : if (binfo == error_mark_node)
10086 : : {
10087 : 51 : if (!(complain & tf_error))
10088 : : return error_mark_node;
10089 : :
10090 : 51 : inform (input_location, " in pointer to member function conversion");
10091 : 51 : return size_zero_node;
10092 : : }
10093 : 29999 : else if (binfo)
10094 : : {
10095 : 29851 : if (kind != bk_via_virtual)
10096 : 29758 : return BINFO_OFFSET (binfo);
10097 : : else
10098 : : /* FROM is a virtual base class of TO. Issue an error or warning
10099 : : depending on whether or not this is a reinterpret cast. */
10100 : : {
10101 : 93 : if (!(complain & tf_error))
10102 : : return error_mark_node;
10103 : :
10104 : 78 : error ("pointer to member conversion via virtual base %qT",
10105 : 78 : BINFO_TYPE (binfo_from_vbase (binfo)));
10106 : :
10107 : 78 : return size_zero_node;
10108 : : }
10109 : : }
10110 : : else
10111 : : return NULL_TREE;
10112 : : }
10113 : :
10114 : : /* Get difference in deltas for different pointer to member function
10115 : : types. If the conversion is invalid and tf_error is not set in
10116 : : COMPLAIN, returns error_mark_node, otherwise returns an integer
10117 : : constant of type PTRDIFF_TYPE_NODE and its value is zero if the
10118 : : conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
10119 : : conversions as well. If C_CAST_P is true this conversion is taking
10120 : : place as part of a C-style cast.
10121 : :
10122 : : Note that the naming of FROM and TO is kind of backwards; the return
10123 : : value is what we add to a TO in order to get a FROM. They are named
10124 : : this way because we call this function to find out how to convert from
10125 : : a pointer to member of FROM to a pointer to member of TO. */
10126 : :
10127 : : static tree
10128 : 93393 : get_delta_difference (tree from, tree to,
10129 : : bool allow_inverse_p,
10130 : : bool c_cast_p, tsubst_flags_t complain)
10131 : : {
10132 : 93393 : auto_diagnostic_group d;
10133 : 93393 : tree result;
10134 : :
10135 : 93393 : if (same_type_ignoring_top_level_qualifiers_p (from, to))
10136 : : /* Pointer to member of incomplete class is permitted*/
10137 : 63491 : result = size_zero_node;
10138 : : else
10139 : 29902 : result = get_delta_difference_1 (from, to, c_cast_p, complain);
10140 : :
10141 : 93393 : if (result == error_mark_node)
10142 : : return error_mark_node;
10143 : :
10144 : 93378 : if (!result)
10145 : : {
10146 : 148 : if (!allow_inverse_p)
10147 : : {
10148 : 0 : if (!(complain & tf_error))
10149 : : return error_mark_node;
10150 : :
10151 : 0 : error_not_base_type (from, to);
10152 : 0 : inform (input_location, " in pointer to member conversion");
10153 : 0 : result = size_zero_node;
10154 : : }
10155 : : else
10156 : : {
10157 : 148 : result = get_delta_difference_1 (to, from, c_cast_p, complain);
10158 : :
10159 : 148 : if (result == error_mark_node)
10160 : : return error_mark_node;
10161 : :
10162 : 148 : if (result)
10163 : 148 : result = size_diffop_loc (input_location,
10164 : : size_zero_node, result);
10165 : : else
10166 : : {
10167 : 0 : if (!(complain & tf_error))
10168 : : return error_mark_node;
10169 : :
10170 : 0 : error_not_base_type (from, to);
10171 : 0 : inform (input_location, " in pointer to member conversion");
10172 : 0 : result = size_zero_node;
10173 : : }
10174 : : }
10175 : : }
10176 : :
10177 : 93378 : return convert_to_integer (ptrdiff_type_node, result);
10178 : 93393 : }
10179 : :
10180 : : /* Return a constructor for the pointer-to-member-function TYPE using
10181 : : the other components as specified. */
10182 : :
10183 : : tree
10184 : 63914 : build_ptrmemfunc1 (tree type, tree delta, tree pfn)
10185 : : {
10186 : 63914 : tree u = NULL_TREE;
10187 : 63914 : tree delta_field;
10188 : 63914 : tree pfn_field;
10189 : 63914 : vec<constructor_elt, va_gc> *v;
10190 : :
10191 : : /* Pull the FIELD_DECLs out of the type. */
10192 : 63914 : pfn_field = TYPE_FIELDS (type);
10193 : 63914 : delta_field = DECL_CHAIN (pfn_field);
10194 : :
10195 : : /* Make sure DELTA has the type we want. */
10196 : 63914 : delta = convert_and_check (input_location, delta_type_node, delta);
10197 : :
10198 : : /* Convert to the correct target type if necessary. */
10199 : 63914 : pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10200 : :
10201 : : /* Finish creating the initializer. */
10202 : 63914 : vec_alloc (v, 2);
10203 : 63914 : CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10204 : 63914 : CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10205 : 63914 : u = build_constructor (type, v);
10206 : 63914 : TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10207 : 63914 : TREE_STATIC (u) = (TREE_CONSTANT (u)
10208 : 63812 : && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10209 : : != NULL_TREE)
10210 : 127726 : && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10211 : : != NULL_TREE));
10212 : 63914 : return u;
10213 : : }
10214 : :
10215 : : /* Build a constructor for a pointer to member function. It can be
10216 : : used to initialize global variables, local variable, or used
10217 : : as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10218 : : want to be.
10219 : :
10220 : : If FORCE is nonzero, then force this conversion, even if
10221 : : we would rather not do it. Usually set when using an explicit
10222 : : cast. A C-style cast is being processed iff C_CAST_P is true.
10223 : :
10224 : : Return error_mark_node, if something goes wrong. */
10225 : :
10226 : : tree
10227 : 31460 : build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10228 : : tsubst_flags_t complain)
10229 : : {
10230 : 31460 : tree fn;
10231 : 31460 : tree pfn_type;
10232 : 31460 : tree to_type;
10233 : :
10234 : 31460 : if (error_operand_p (pfn))
10235 : 0 : return error_mark_node;
10236 : :
10237 : 31460 : pfn_type = TREE_TYPE (pfn);
10238 : 31460 : to_type = build_ptrmemfunc_type (type);
10239 : :
10240 : : /* Handle multiple conversions of pointer to member functions. */
10241 : 31460 : if (TYPE_PTRMEMFUNC_P (pfn_type))
10242 : : {
10243 : 29609 : tree delta = NULL_TREE;
10244 : 29609 : tree npfn = NULL_TREE;
10245 : 29609 : tree n;
10246 : :
10247 : 29609 : if (!force
10248 : 29609 : && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
10249 : : LOOKUP_NORMAL, complain))
10250 : : {
10251 : 0 : if (complain & tf_error)
10252 : 0 : error ("invalid conversion to type %qT from type %qT",
10253 : : to_type, pfn_type);
10254 : : else
10255 : 0 : return error_mark_node;
10256 : : }
10257 : :
10258 : 29609 : n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10259 : 29609 : TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10260 : : force,
10261 : : c_cast_p, complain);
10262 : 29609 : if (n == error_mark_node)
10263 : : return error_mark_node;
10264 : :
10265 : 29600 : STRIP_ANY_LOCATION_WRAPPER (pfn);
10266 : :
10267 : : /* We don't have to do any conversion to convert a
10268 : : pointer-to-member to its own type. But, we don't want to
10269 : : just return a PTRMEM_CST if there's an explicit cast; that
10270 : : cast should make the expression an invalid template argument. */
10271 : 29600 : if (TREE_CODE (pfn) != PTRMEM_CST
10272 : 29600 : && same_type_p (to_type, pfn_type))
10273 : : return pfn;
10274 : :
10275 : 29600 : if (TREE_SIDE_EFFECTS (pfn))
10276 : 8 : pfn = save_expr (pfn);
10277 : :
10278 : : /* Obtain the function pointer and the current DELTA. */
10279 : 29600 : if (TREE_CODE (pfn) == PTRMEM_CST)
10280 : 29512 : expand_ptrmemfunc_cst (pfn, &delta, &npfn);
10281 : : else
10282 : : {
10283 : 88 : npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
10284 : 88 : delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
10285 : : }
10286 : :
10287 : : /* Just adjust the DELTA field. */
10288 : 29600 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10289 : : (TREE_TYPE (delta), ptrdiff_type_node));
10290 : 29600 : if (!integer_zerop (n))
10291 : : {
10292 : 50 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
10293 : : n = cp_build_binary_op (input_location,
10294 : : LSHIFT_EXPR, n, integer_one_node,
10295 : : complain);
10296 : 50 : delta = cp_build_binary_op (input_location,
10297 : : PLUS_EXPR, delta, n, complain);
10298 : : }
10299 : 29600 : return build_ptrmemfunc1 (to_type, delta, npfn);
10300 : : }
10301 : :
10302 : : /* Handle null pointer to member function conversions. */
10303 : 1851 : if (null_ptr_cst_p (pfn))
10304 : : {
10305 : 788 : pfn = cp_build_c_cast (input_location,
10306 : 788 : TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10307 : : pfn, complain);
10308 : 788 : return build_ptrmemfunc1 (to_type,
10309 : : integer_zero_node,
10310 : 788 : pfn);
10311 : : }
10312 : :
10313 : 1063 : if (type_unknown_p (pfn))
10314 : 0 : return instantiate_type (type, pfn, complain);
10315 : :
10316 : 1063 : fn = TREE_OPERAND (pfn, 0);
10317 : 1063 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10318 : : /* In a template, we will have preserved the
10319 : : OFFSET_REF. */
10320 : : || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10321 : 1063 : return make_ptrmem_cst (to_type, fn);
10322 : : }
10323 : :
10324 : : /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10325 : : given by CST.
10326 : :
10327 : : ??? There is no consistency as to the types returned for the above
10328 : : values. Some code acts as if it were a sizetype and some as if it were
10329 : : integer_type_node. */
10330 : :
10331 : : void
10332 : 63420 : expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10333 : : {
10334 : 63420 : tree type = TREE_TYPE (cst);
10335 : 63420 : tree fn = PTRMEM_CST_MEMBER (cst);
10336 : 63420 : tree ptr_class, fn_class;
10337 : :
10338 : 63420 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10339 : :
10340 : : /* The class that the function belongs to. */
10341 : 63420 : fn_class = DECL_CONTEXT (fn);
10342 : :
10343 : : /* The class that we're creating a pointer to member of. */
10344 : 63420 : ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10345 : :
10346 : : /* First, calculate the adjustment to the function's class. */
10347 : 63420 : *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10348 : : /*c_cast_p=*/0, tf_warning_or_error);
10349 : :
10350 : 63420 : if (!DECL_VIRTUAL_P (fn))
10351 : : {
10352 : 62192 : tree t = build_addr_func (fn, tf_warning_or_error);
10353 : 62192 : if (TREE_CODE (t) == ADDR_EXPR)
10354 : 62192 : SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10355 : 62192 : *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10356 : : }
10357 : : else
10358 : : {
10359 : : /* If we're dealing with a virtual function, we have to adjust 'this'
10360 : : again, to point to the base which provides the vtable entry for
10361 : : fn; the call will do the opposite adjustment. */
10362 : 1228 : tree orig_class = DECL_CONTEXT (fn);
10363 : 1228 : tree binfo = binfo_or_else (orig_class, fn_class);
10364 : 1228 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10365 : : *delta, BINFO_OFFSET (binfo));
10366 : :
10367 : : /* We set PFN to the vtable offset at which the function can be
10368 : : found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10369 : : case delta is shifted left, and then incremented). */
10370 : 1228 : *pfn = DECL_VINDEX (fn);
10371 : 1228 : *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10372 : : TYPE_SIZE_UNIT (vtable_entry_type));
10373 : :
10374 : 1228 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10375 : : {
10376 : 1228 : case ptrmemfunc_vbit_in_pfn:
10377 : 1228 : *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10378 : : integer_one_node);
10379 : 1228 : break;
10380 : :
10381 : : case ptrmemfunc_vbit_in_delta:
10382 : : *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10383 : : *delta, integer_one_node);
10384 : : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10385 : : *delta, integer_one_node);
10386 : : break;
10387 : :
10388 : : default:
10389 : : gcc_unreachable ();
10390 : : }
10391 : :
10392 : 1228 : *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10393 : : }
10394 : 63420 : }
10395 : :
10396 : : /* Return an expression for PFN from the pointer-to-member function
10397 : : given by T. */
10398 : :
10399 : : static tree
10400 : 89918 : pfn_from_ptrmemfunc (tree t)
10401 : : {
10402 : 89918 : if (TREE_CODE (t) == PTRMEM_CST)
10403 : : {
10404 : 191 : tree delta;
10405 : 191 : tree pfn;
10406 : :
10407 : 191 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10408 : 191 : if (pfn)
10409 : 191 : return pfn;
10410 : : }
10411 : :
10412 : 89727 : return build_ptrmemfunc_access_expr (t, pfn_identifier);
10413 : : }
10414 : :
10415 : : /* Return an expression for DELTA from the pointer-to-member function
10416 : : given by T. */
10417 : :
10418 : : static tree
10419 : 89918 : delta_from_ptrmemfunc (tree t)
10420 : : {
10421 : 89918 : if (TREE_CODE (t) == PTRMEM_CST)
10422 : : {
10423 : 191 : tree delta;
10424 : 191 : tree pfn;
10425 : :
10426 : 191 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10427 : 191 : if (delta)
10428 : 191 : return delta;
10429 : : }
10430 : :
10431 : 89727 : return build_ptrmemfunc_access_expr (t, delta_identifier);
10432 : : }
10433 : :
10434 : : /* Convert value RHS to type TYPE as preparation for an assignment to
10435 : : an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10436 : : implicit conversion is. If FNDECL is non-NULL, we are doing the
10437 : : conversion in order to pass the PARMNUMth argument of FNDECL.
10438 : : If FNDECL is NULL, we are doing the conversion in function pointer
10439 : : argument passing, conversion in initialization, etc. */
10440 : :
10441 : : static tree
10442 : 122840493 : convert_for_assignment (tree type, tree rhs,
10443 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10444 : : tsubst_flags_t complain, int flags)
10445 : : {
10446 : 122840493 : tree rhstype;
10447 : 122840493 : enum tree_code coder;
10448 : :
10449 : 122840493 : location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10450 : 122840493 : bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10451 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10452 : : but preserve location wrappers. */
10453 : 122840493 : if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10454 : 122840493 : && !location_wrapper_p (rhs))
10455 : 7422 : rhs = TREE_OPERAND (rhs, 0);
10456 : :
10457 : : /* Handle [dcl.init.list] direct-list-initialization from
10458 : : single element of enumeration with a fixed underlying type. */
10459 : 122840493 : if (is_direct_enum_init (type, rhs))
10460 : : {
10461 : 20 : tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10462 : 20 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10463 : : {
10464 : 11 : warning_sentinel w (warn_useless_cast);
10465 : 11 : warning_sentinel w2 (warn_ignored_qualifiers);
10466 : 11 : rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10467 : 11 : }
10468 : : else
10469 : 9 : rhs = error_mark_node;
10470 : : }
10471 : :
10472 : 122840493 : rhstype = TREE_TYPE (rhs);
10473 : 122840493 : coder = TREE_CODE (rhstype);
10474 : :
10475 : 1168834 : if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10476 : 124009221 : && vector_types_convertible_p (type, rhstype, true))
10477 : : {
10478 : 1168719 : rhs = mark_rvalue_use (rhs);
10479 : 1168719 : return convert (type, rhs);
10480 : : }
10481 : :
10482 : 121671774 : if (rhs == error_mark_node || rhstype == error_mark_node)
10483 : : return error_mark_node;
10484 : 121671765 : if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10485 : : return error_mark_node;
10486 : :
10487 : : /* The RHS of an assignment cannot have void type. */
10488 : 121671765 : if (coder == VOID_TYPE)
10489 : : {
10490 : 48 : if (complain & tf_error)
10491 : 30 : error_at (rhs_loc, "void value not ignored as it ought to be");
10492 : 48 : return error_mark_node;
10493 : : }
10494 : :
10495 : 121671717 : if (c_dialect_objc ())
10496 : : {
10497 : 0 : int parmno;
10498 : 0 : tree selector;
10499 : 0 : tree rname = fndecl;
10500 : :
10501 : 0 : switch (errtype)
10502 : : {
10503 : : case ICR_ASSIGN:
10504 : : parmno = -1;
10505 : : break;
10506 : 0 : case ICR_INIT:
10507 : 0 : parmno = -2;
10508 : 0 : break;
10509 : 0 : default:
10510 : 0 : selector = objc_message_selector ();
10511 : 0 : parmno = parmnum;
10512 : 0 : if (selector && parmno > 1)
10513 : : {
10514 : 0 : rname = selector;
10515 : 0 : parmno -= 1;
10516 : : }
10517 : : }
10518 : :
10519 : 0 : if (objc_compare_types (type, rhstype, parmno, rname))
10520 : : {
10521 : 0 : rhs = mark_rvalue_use (rhs);
10522 : 0 : return convert (type, rhs);
10523 : : }
10524 : : }
10525 : :
10526 : : /* [expr.ass]
10527 : :
10528 : : The expression is implicitly converted (clause _conv_) to the
10529 : : cv-unqualified type of the left operand.
10530 : :
10531 : : We allow bad conversions here because by the time we get to this point
10532 : : we are committed to doing the conversion. If we end up doing a bad
10533 : : conversion, convert_like will complain. */
10534 : 121671717 : if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10535 : : {
10536 : : /* When -Wno-pmf-conversions is use, we just silently allow
10537 : : conversions from pointers-to-members to plain pointers. If
10538 : : the conversion doesn't work, cp_convert will complain. */
10539 : 1471 : if (!warn_pmf2ptr
10540 : 9 : && TYPE_PTR_P (type)
10541 : 1480 : && TYPE_PTRMEMFUNC_P (rhstype))
10542 : 9 : rhs = cp_convert (strip_top_quals (type), rhs, complain);
10543 : : else
10544 : : {
10545 : 1462 : if (complain & tf_error)
10546 : : {
10547 : 1271 : auto_diagnostic_group d;
10548 : :
10549 : : /* If the right-hand side has unknown type, then it is an
10550 : : overloaded function. Call instantiate_type to get error
10551 : : messages. */
10552 : 1271 : if (rhstype == unknown_type_node)
10553 : : {
10554 : 197 : tree r = instantiate_type (type, rhs, tf_warning_or_error);
10555 : : /* -fpermissive might allow this; recurse. */
10556 : 197 : if (!seen_error ())
10557 : 15 : return convert_for_assignment (type, r, errtype, fndecl,
10558 : : parmnum, complain, flags);
10559 : : }
10560 : 1074 : else if (fndecl)
10561 : 92 : complain_about_bad_argument (rhs_loc,
10562 : : rhstype, type,
10563 : : fndecl, parmnum);
10564 : : else
10565 : : {
10566 : 982 : range_label_for_type_mismatch label (rhstype, type);
10567 : 982 : gcc_rich_location richloc
10568 : : (rhs_loc,
10569 : : has_loc ? &label : NULL,
10570 : 982 : has_loc ? highlight_colors::percent_h : NULL);
10571 : :
10572 : 982 : switch (errtype)
10573 : : {
10574 : 0 : case ICR_DEFAULT_ARGUMENT:
10575 : 0 : error_at (&richloc,
10576 : : "cannot convert %qH to %qI in default argument",
10577 : : rhstype, type);
10578 : 0 : break;
10579 : 6 : case ICR_ARGPASS:
10580 : 6 : error_at (&richloc,
10581 : : "cannot convert %qH to %qI in argument passing",
10582 : : rhstype, type);
10583 : 6 : break;
10584 : 0 : case ICR_CONVERTING:
10585 : 0 : error_at (&richloc, "cannot convert %qH to %qI",
10586 : : rhstype, type);
10587 : 0 : break;
10588 : 544 : case ICR_INIT:
10589 : 544 : error_at (&richloc,
10590 : : "cannot convert %qH to %qI in initialization",
10591 : : rhstype, type);
10592 : 544 : break;
10593 : 18 : case ICR_RETURN:
10594 : 18 : error_at (&richloc, "cannot convert %qH to %qI in return",
10595 : : rhstype, type);
10596 : 18 : break;
10597 : 414 : case ICR_ASSIGN:
10598 : 414 : error_at (&richloc,
10599 : : "cannot convert %qH to %qI in assignment",
10600 : : rhstype, type);
10601 : 414 : break;
10602 : 0 : default:
10603 : 0 : gcc_unreachable();
10604 : : }
10605 : 982 : }
10606 : :
10607 : : /* See if we can be more helpful. */
10608 : 1256 : maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
10609 : :
10610 : 1256 : if (TYPE_PTR_P (rhstype)
10611 : 411 : && TYPE_PTR_P (type)
10612 : 396 : && CLASS_TYPE_P (TREE_TYPE (rhstype))
10613 : 278 : && CLASS_TYPE_P (TREE_TYPE (type))
10614 : 1318 : && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10615 : 3 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10616 : : (TREE_TYPE (rhstype))),
10617 : 3 : "class type %qT is incomplete", TREE_TYPE (rhstype));
10618 : 1271 : }
10619 : 1447 : return error_mark_node;
10620 : : }
10621 : : }
10622 : 121670255 : if (warn_suggest_attribute_format)
10623 : : {
10624 : 2363 : const enum tree_code codel = TREE_CODE (type);
10625 : 2363 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10626 : 592 : && coder == codel
10627 : 504 : && check_missing_format_attribute (type, rhstype)
10628 : 2381 : && (complain & tf_warning))
10629 : 18 : switch (errtype)
10630 : : {
10631 : 0 : case ICR_ARGPASS:
10632 : 0 : case ICR_DEFAULT_ARGUMENT:
10633 : 0 : if (fndecl)
10634 : 0 : warning (OPT_Wsuggest_attribute_format,
10635 : : "parameter %qP of %qD might be a candidate "
10636 : : "for a format attribute", parmnum, fndecl);
10637 : : else
10638 : 0 : warning (OPT_Wsuggest_attribute_format,
10639 : : "parameter might be a candidate "
10640 : : "for a format attribute");
10641 : : break;
10642 : 0 : case ICR_CONVERTING:
10643 : 0 : warning (OPT_Wsuggest_attribute_format,
10644 : : "target of conversion might be a candidate "
10645 : : "for a format attribute");
10646 : 0 : break;
10647 : 6 : case ICR_INIT:
10648 : 6 : warning (OPT_Wsuggest_attribute_format,
10649 : : "target of initialization might be a candidate "
10650 : : "for a format attribute");
10651 : 6 : break;
10652 : 6 : case ICR_RETURN:
10653 : 6 : warning (OPT_Wsuggest_attribute_format,
10654 : : "return type might be a candidate "
10655 : : "for a format attribute");
10656 : 6 : break;
10657 : 6 : case ICR_ASSIGN:
10658 : 6 : warning (OPT_Wsuggest_attribute_format,
10659 : : "left-hand side of assignment might be a candidate "
10660 : : "for a format attribute");
10661 : 6 : break;
10662 : 0 : default:
10663 : 0 : gcc_unreachable();
10664 : : }
10665 : : }
10666 : :
10667 : 121670255 : if (TREE_CODE (type) == BOOLEAN_TYPE)
10668 : 23916235 : maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
10669 : :
10670 : 121670255 : if (complain & tf_warning)
10671 : 120380262 : warn_for_address_of_packed_member (type, rhs);
10672 : :
10673 : 121670255 : return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10674 : 121670255 : complain, flags);
10675 : : }
10676 : :
10677 : : /* Convert RHS to be of type TYPE.
10678 : : If EXP is nonzero, it is the target of the initialization.
10679 : : ERRTYPE indicates what kind of error the implicit conversion is.
10680 : :
10681 : : Two major differences between the behavior of
10682 : : `convert_for_assignment' and `convert_for_initialization'
10683 : : are that references are bashed in the former, while
10684 : : copied in the latter, and aggregates are assigned in
10685 : : the former (operator=) while initialized in the
10686 : : latter (X(X&)).
10687 : :
10688 : : If using constructor make sure no conversion operator exists, if one does
10689 : : exist, an ambiguity exists. */
10690 : :
10691 : : tree
10692 : 108534286 : convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10693 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10694 : : tsubst_flags_t complain)
10695 : : {
10696 : 108534286 : enum tree_code codel = TREE_CODE (type);
10697 : 108534286 : tree rhstype;
10698 : 108534286 : enum tree_code coder;
10699 : :
10700 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10701 : : Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10702 : 108534286 : if (TREE_CODE (rhs) == NOP_EXPR
10703 : 4730729 : && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10704 : 108890011 : && codel != REFERENCE_TYPE)
10705 : 355725 : rhs = TREE_OPERAND (rhs, 0);
10706 : :
10707 : 108534286 : if (type == error_mark_node
10708 : 108534248 : || rhs == error_mark_node
10709 : 217068473 : || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10710 : : return error_mark_node;
10711 : :
10712 : 108534187 : if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10713 : : ;
10714 : 98043580 : else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10715 : 1565384 : && TREE_CODE (type) != ARRAY_TYPE
10716 : 1565384 : && (!TYPE_REF_P (type)
10717 : 8705 : || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10718 : 96486898 : || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10719 : 13460 : && !TYPE_REFFN_P (type))
10720 : 194518690 : || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10721 : 1568476 : rhs = decay_conversion (rhs, complain);
10722 : :
10723 : 108534187 : rhstype = TREE_TYPE (rhs);
10724 : 108534187 : coder = TREE_CODE (rhstype);
10725 : :
10726 : 108534187 : if (coder == ERROR_MARK)
10727 : 9 : return error_mark_node;
10728 : :
10729 : : /* We accept references to incomplete types, so we can
10730 : : return here before checking if RHS is of complete type. */
10731 : :
10732 : 108534178 : if (codel == REFERENCE_TYPE)
10733 : : {
10734 : 5086855 : auto_diagnostic_group d;
10735 : : /* This should eventually happen in convert_arguments. */
10736 : 5086855 : int savew = 0, savee = 0;
10737 : :
10738 : 5086855 : if (fndecl)
10739 : 249513 : savew = warningcount + werrorcount, savee = errorcount;
10740 : 5086855 : rhs = initialize_reference (type, rhs, flags, complain);
10741 : :
10742 : 5086855 : if (fndecl
10743 : 5086855 : && (warningcount + werrorcount > savew || errorcount > savee))
10744 : 36 : inform (get_fndecl_argument_location (fndecl, parmnum),
10745 : : "in passing argument %P of %qD", parmnum, fndecl);
10746 : 5086855 : return rhs;
10747 : 5086855 : }
10748 : :
10749 : 103447323 : if (exp != 0)
10750 : 3294655 : exp = require_complete_type (exp, complain);
10751 : 103447323 : if (exp == error_mark_node)
10752 : : return error_mark_node;
10753 : :
10754 : 103447323 : type = complete_type (type);
10755 : :
10756 : 103447323 : if (DIRECT_INIT_EXPR_P (type, rhs))
10757 : : /* Don't try to do copy-initialization if we already have
10758 : : direct-initialization. */
10759 : : return rhs;
10760 : :
10761 : 103446368 : if (MAYBE_CLASS_TYPE_P (type))
10762 : 7163295 : return perform_implicit_conversion_flags (type, rhs, complain, flags);
10763 : :
10764 : 96283073 : return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10765 : 96283073 : complain, flags);
10766 : : }
10767 : :
10768 : : /* If RETVAL is the address of, or a reference to, a local variable or
10769 : : temporary give an appropriate warning and return true. */
10770 : :
10771 : : static bool
10772 : 35592945 : maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10773 : : {
10774 : 35593632 : tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10775 : 35593632 : tree whats_returned = fold_for_warn (retval);
10776 : 35593632 : if (!loc)
10777 : 35593632 : loc = cp_expr_loc_or_input_loc (retval);
10778 : :
10779 : 43177457 : for (;;)
10780 : : {
10781 : 43177457 : if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10782 : 473544 : whats_returned = TREE_OPERAND (whats_returned, 1);
10783 : 42703913 : else if (CONVERT_EXPR_P (whats_returned)
10784 : 35613788 : || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10785 : 7110281 : whats_returned = TREE_OPERAND (whats_returned, 0);
10786 : : else
10787 : : break;
10788 : : }
10789 : :
10790 : 35593632 : if (TREE_CODE (whats_returned) == TARGET_EXPR
10791 : 35593632 : && is_std_init_list (TREE_TYPE (whats_returned)))
10792 : : {
10793 : 16 : tree init = TARGET_EXPR_INITIAL (whats_returned);
10794 : 16 : if (TREE_CODE (init) == CONSTRUCTOR)
10795 : : /* Pull out the array address. */
10796 : 6 : whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10797 : 10 : else if (TREE_CODE (init) == INDIRECT_REF)
10798 : : /* The source of a trivial copy looks like *(T*)&var. */
10799 : 7 : whats_returned = TREE_OPERAND (init, 0);
10800 : : else
10801 : : return false;
10802 : 13 : STRIP_NOPS (whats_returned);
10803 : : }
10804 : :
10805 : : /* As a special case, we handle a call to std::move or std::forward. */
10806 : 35593629 : if (TREE_CODE (whats_returned) == CALL_EXPR
10807 : 35593629 : && (is_std_move_p (whats_returned)
10808 : 9693997 : || is_std_forward_p (whats_returned)))
10809 : : {
10810 : 675 : tree arg = CALL_EXPR_ARG (whats_returned, 0);
10811 : 675 : return maybe_warn_about_returning_address_of_local (arg, loc);
10812 : : }
10813 : :
10814 : 35592954 : if (TREE_CODE (whats_returned) != ADDR_EXPR)
10815 : : return false;
10816 : 782008 : whats_returned = TREE_OPERAND (whats_returned, 0);
10817 : :
10818 : 782008 : while (TREE_CODE (whats_returned) == COMPONENT_REF
10819 : 1518422 : || TREE_CODE (whats_returned) == ARRAY_REF)
10820 : 736414 : whats_returned = TREE_OPERAND (whats_returned, 0);
10821 : :
10822 : 782008 : if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10823 : 782008 : || TREE_CODE (whats_returned) == TARGET_EXPR)
10824 : : {
10825 : 50 : if (TYPE_REF_P (valtype))
10826 : : /* P2748 made this an error in C++26. */
10827 : 72 : emit_diagnostic ((cxx_dialect >= cxx26
10828 : : ? diagnostics::kind::permerror
10829 : : : diagnostics::kind::warning),
10830 : 41 : loc, OPT_Wreturn_local_addr,
10831 : : "returning reference to temporary");
10832 : 9 : else if (TYPE_PTR_P (valtype))
10833 : 3 : warning_at (loc, OPT_Wreturn_local_addr,
10834 : : "returning pointer to temporary");
10835 : 6 : else if (is_std_init_list (valtype))
10836 : 6 : warning_at (loc, OPT_Winit_list_lifetime,
10837 : : "returning temporary %<initializer_list%> does not extend "
10838 : : "the lifetime of the underlying array");
10839 : 50 : return true;
10840 : : }
10841 : :
10842 : 781958 : STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10843 : :
10844 : 781958 : if (DECL_P (whats_returned)
10845 : 76461 : && DECL_NAME (whats_returned)
10846 : 76461 : && DECL_FUNCTION_SCOPE_P (whats_returned)
10847 : 12547 : && !is_capture_proxy (whats_returned)
10848 : 794484 : && !(TREE_STATIC (whats_returned)
10849 : 146 : || TREE_PUBLIC (whats_returned)))
10850 : : {
10851 : 106 : if (DECL_DECOMPOSITION_P (whats_returned)
10852 : 39 : && !DECL_DECOMP_IS_BASE (whats_returned)
10853 : 185 : && DECL_HAS_VALUE_EXPR_P (whats_returned))
10854 : : {
10855 : : /* When returning address of a structured binding, if the structured
10856 : : binding is not a reference, continue normally, if it is a
10857 : : reference, recurse on the initializer of the structured
10858 : : binding. */
10859 : 39 : tree base = DECL_DECOMP_BASE (whats_returned);
10860 : 39 : if (TYPE_REF_P (TREE_TYPE (base)))
10861 : : {
10862 : 15 : if (tree init = DECL_INITIAL (base))
10863 : : return maybe_warn_about_returning_address_of_local (init, loc);
10864 : : else
10865 : : return false;
10866 : : }
10867 : : }
10868 : 131 : bool w = false;
10869 : 131 : auto_diagnostic_group d;
10870 : 131 : if (TYPE_REF_P (valtype))
10871 : 81 : w = warning_at (loc, OPT_Wreturn_local_addr,
10872 : : "reference to local variable %qD returned",
10873 : : whats_returned);
10874 : 50 : else if (is_std_init_list (valtype))
10875 : 3 : w = warning_at (loc, OPT_Winit_list_lifetime,
10876 : : "returning local %<initializer_list%> variable %qD "
10877 : : "does not extend the lifetime of the underlying array",
10878 : : whats_returned);
10879 : 47 : else if (POINTER_TYPE_P (valtype)
10880 : 41 : && TREE_CODE (whats_returned) == LABEL_DECL)
10881 : 6 : w = warning_at (loc, OPT_Wreturn_local_addr,
10882 : : "address of label %qD returned",
10883 : : whats_returned);
10884 : 41 : else if (POINTER_TYPE_P (valtype))
10885 : 35 : w = warning_at (loc, OPT_Wreturn_local_addr,
10886 : : "address of local variable %qD returned",
10887 : : whats_returned);
10888 : 125 : if (w)
10889 : 103 : inform (DECL_SOURCE_LOCATION (whats_returned),
10890 : : "declared here");
10891 : 131 : return true;
10892 : 131 : }
10893 : :
10894 : : return false;
10895 : : }
10896 : :
10897 : : /* Returns true if DECL is in the std namespace. */
10898 : :
10899 : : bool
10900 : 74740377 : decl_in_std_namespace_p (tree decl)
10901 : : {
10902 : 86927714 : while (decl)
10903 : : {
10904 : 86480368 : decl = decl_namespace_context (decl);
10905 : 86480368 : if (DECL_NAMESPACE_STD_P (decl))
10906 : : return true;
10907 : : /* Allow inline namespaces inside of std namespace, e.g. with
10908 : : --enable-symvers=gnu-versioned-namespace std::forward would be
10909 : : actually std::_8::forward. */
10910 : 41565918 : if (!DECL_NAMESPACE_INLINE_P (decl))
10911 : : return false;
10912 : 12187337 : decl = CP_DECL_CONTEXT (decl);
10913 : : }
10914 : : return false;
10915 : : }
10916 : :
10917 : : /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10918 : :
10919 : : static bool
10920 : 9693997 : is_std_forward_p (tree fn)
10921 : : {
10922 : : /* std::forward only takes one argument. */
10923 : 9693997 : if (call_expr_nargs (fn) != 1)
10924 : : return false;
10925 : :
10926 : 4661230 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10927 : 4661230 : if (!decl_in_std_namespace_p (fndecl))
10928 : : return false;
10929 : :
10930 : 1639662 : tree name = DECL_NAME (fndecl);
10931 : 1639662 : return name && id_equal (name, "forward");
10932 : : }
10933 : :
10934 : : /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10935 : :
10936 : : static bool
10937 : 9701583 : is_std_move_p (tree fn)
10938 : : {
10939 : : /* std::move only takes one argument. */
10940 : 9701583 : if (call_expr_nargs (fn) != 1)
10941 : : return false;
10942 : :
10943 : 4668024 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10944 : 4668024 : if (!decl_in_std_namespace_p (fndecl))
10945 : : return false;
10946 : :
10947 : 1645966 : tree name = DECL_NAME (fndecl);
10948 : 1645966 : return name && id_equal (name, "move");
10949 : : }
10950 : :
10951 : : /* Returns true if RETVAL is a good candidate for the NRVO as per
10952 : : [class.copy.elision]. FUNCTYPE is the type the function is declared
10953 : : to return. */
10954 : :
10955 : : static bool
10956 : 44954918 : can_do_nrvo_p (tree retval, tree functype)
10957 : : {
10958 : 44954918 : if (functype == error_mark_node)
10959 : : return false;
10960 : 44954877 : if (retval)
10961 : 43445674 : STRIP_ANY_LOCATION_WRAPPER (retval);
10962 : 44954877 : tree result = DECL_RESULT (current_function_decl);
10963 : 44954877 : return (retval != NULL_TREE
10964 : 43445674 : && !processing_template_decl
10965 : : /* Must be a local, automatic variable. */
10966 : 36374424 : && VAR_P (retval)
10967 : 3284515 : && DECL_CONTEXT (retval) == current_function_decl
10968 : 2572462 : && !TREE_STATIC (retval)
10969 : : /* And not a lambda or anonymous union proxy. */
10970 : 2569793 : && !DECL_HAS_VALUE_EXPR_P (retval)
10971 : 2569548 : && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10972 : : /* The cv-unqualified type of the returned value must be the
10973 : : same as the cv-unqualified return type of the
10974 : : function. */
10975 : 2569433 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10976 : : TYPE_MAIN_VARIANT (functype))
10977 : : /* And the returned value must be non-volatile. */
10978 : 47513253 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
10979 : : }
10980 : :
10981 : : /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
10982 : : would otherwise return in memory. */
10983 : :
10984 : : static bool
10985 : 44954553 : want_nrvo_p (tree retval, tree functype)
10986 : : {
10987 : 44954553 : return (can_do_nrvo_p (retval, functype)
10988 : 44954553 : && aggregate_value_p (functype, current_function_decl));
10989 : : }
10990 : :
10991 : : /* Like can_do_nrvo_p, but we check if we're trying to move a class
10992 : : prvalue. */
10993 : :
10994 : : static bool
10995 : 1696 : can_elide_copy_prvalue_p (tree retval, tree functype)
10996 : : {
10997 : 1696 : if (functype == error_mark_node)
10998 : : return false;
10999 : 1696 : if (retval)
11000 : 1696 : STRIP_ANY_LOCATION_WRAPPER (retval);
11001 : 1696 : return (retval != NULL_TREE
11002 : 1696 : && !glvalue_p (retval)
11003 : 135 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
11004 : : TYPE_MAIN_VARIANT (functype))
11005 : 1804 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
11006 : : }
11007 : :
11008 : : /* If we should treat RETVAL, an expression being returned, as if it were
11009 : : designated by an rvalue, returns it adjusted accordingly; otherwise, returns
11010 : : NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
11011 : : context (rather than throw). */
11012 : :
11013 : : tree
11014 : 8702650 : treat_lvalue_as_rvalue_p (tree expr, bool return_p)
11015 : : {
11016 : 8702650 : if (cxx_dialect == cxx98)
11017 : : return NULL_TREE;
11018 : :
11019 : 8695212 : tree retval = expr;
11020 : 8695212 : STRIP_ANY_LOCATION_WRAPPER (retval);
11021 : 8695212 : if (REFERENCE_REF_P (retval))
11022 : 622392 : retval = TREE_OPERAND (retval, 0);
11023 : :
11024 : : /* An implicitly movable entity is a variable of automatic storage duration
11025 : : that is either a non-volatile object or (C++20) an rvalue reference to a
11026 : : non-volatile object type. */
11027 : 8695212 : if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
11028 : 7776149 : || TREE_CODE (retval) == PARM_DECL)
11029 : 1248465 : && !TREE_STATIC (retval)
11030 : 1212226 : && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
11031 : 1212221 : && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
11032 : 106490 : || (cxx_dialect >= cxx20
11033 : 88288 : && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
11034 : 7588909 : return NULL_TREE;
11035 : :
11036 : : /* If the expression in a return or co_return statement is a (possibly
11037 : : parenthesized) id-expression that names an implicitly movable entity
11038 : : declared in the body or parameter-declaration-clause of the innermost
11039 : : enclosing function or lambda-expression, */
11040 : 1106303 : if (return_p)
11041 : : {
11042 : 1096502 : if (DECL_CONTEXT (retval) != current_function_decl)
11043 : : return NULL_TREE;
11044 : 1096436 : expr = move (expr);
11045 : 1096436 : if (expr == error_mark_node)
11046 : : return NULL_TREE;
11047 : 1096434 : return set_implicit_rvalue_p (expr);
11048 : : }
11049 : :
11050 : : /* if the id-expression (possibly parenthesized) is the operand of
11051 : : a throw-expression, and names an implicitly movable entity that belongs
11052 : : to a scope that does not contain the compound-statement of the innermost
11053 : : lambda-expression, try-block, or function-try-block (if any) whose
11054 : : compound-statement or ctor-initializer contains the throw-expression. */
11055 : :
11056 : : /* C++20 added move on throw of parms. */
11057 : 9801 : if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
11058 : : return NULL_TREE;
11059 : :
11060 : : /* We don't check for lambda-expression here, because we should not get past
11061 : : the DECL_HAS_VALUE_EXPR_P check above. */
11062 : 2476 : for (cp_binding_level *b = current_binding_level;
11063 : 2567 : b->kind != sk_namespace; b = b->level_chain)
11064 : : {
11065 : 2569 : for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
11066 : 100 : if (decl == retval)
11067 : 88 : return set_implicit_rvalue_p (move (expr));
11068 : 2469 : if (b->kind == sk_try)
11069 : : return NULL_TREE;
11070 : : }
11071 : :
11072 : 10 : return set_implicit_rvalue_p (move (expr));
11073 : : }
11074 : :
11075 : : /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
11076 : : is true). EXPR is the std::move expression; TYPE is the type of the object
11077 : : being initialized. */
11078 : :
11079 : : void
11080 : 127972565 : maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
11081 : : {
11082 : 127972565 : if (!(warn_pessimizing_move || warn_redundant_move))
11083 : : return;
11084 : :
11085 : 4435067 : const location_t loc = cp_expr_loc_or_input_loc (expr);
11086 : :
11087 : : /* C++98 doesn't know move. */
11088 : 4435067 : if (cxx_dialect < cxx11)
11089 : : return;
11090 : :
11091 : : /* Wait until instantiation time, since we can't gauge if we should do
11092 : : the NRVO until then. */
11093 : 4372687 : if (processing_template_decl)
11094 : : return;
11095 : :
11096 : : /* This is only interesting for class types. */
11097 : 4291071 : if (!CLASS_TYPE_P (type))
11098 : : return;
11099 : :
11100 : 96440 : bool wrapped_p = false;
11101 : : /* A a = std::move (A()); */
11102 : 96440 : if (TREE_CODE (expr) == TREE_LIST)
11103 : : {
11104 : 7864 : if (list_length (expr) == 1)
11105 : : {
11106 : 5166 : expr = TREE_VALUE (expr);
11107 : 5166 : wrapped_p = true;
11108 : : }
11109 : : else
11110 : : return;
11111 : : }
11112 : : /* A a = {std::move (A())};
11113 : : A a{std::move (A())}; */
11114 : 88576 : else if (TREE_CODE (expr) == CONSTRUCTOR)
11115 : : {
11116 : 6575 : if (CONSTRUCTOR_NELTS (expr) == 1)
11117 : : {
11118 : 812 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
11119 : 812 : wrapped_p = true;
11120 : : }
11121 : : else
11122 : : return;
11123 : : }
11124 : :
11125 : : /* First, check if this is a call to std::move. */
11126 : 6328 : if (!REFERENCE_REF_P (expr)
11127 : 92817 : || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
11128 : : return;
11129 : 2896 : tree fn = TREE_OPERAND (expr, 0);
11130 : 2896 : if (!is_std_move_p (fn))
11131 : : return;
11132 : 2306 : tree arg = CALL_EXPR_ARG (fn, 0);
11133 : 2306 : if (TREE_CODE (arg) != NOP_EXPR)
11134 : : return;
11135 : : /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
11136 : : and implicitly-movable warnings. */
11137 : 2306 : if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
11138 : : {
11139 : 1696 : arg = TREE_OPERAND (arg, 0);
11140 : 1696 : arg = TREE_OPERAND (arg, 0);
11141 : 1696 : arg = convert_from_reference (arg);
11142 : 1696 : if (can_elide_copy_prvalue_p (arg, type))
11143 : : {
11144 : 108 : auto_diagnostic_group d;
11145 : 108 : if (warning_at (loc, OPT_Wpessimizing_move,
11146 : : "moving a temporary object prevents copy elision"))
11147 : 108 : inform (loc, "remove %<std::move%> call");
11148 : 108 : }
11149 : : /* The rest of the warnings is only relevant for when we are returning
11150 : : from a function. */
11151 : 1696 : if (!return_p)
11152 : : return;
11153 : :
11154 : 365 : tree moved;
11155 : : /* Warn if we could do copy elision were it not for the move. */
11156 : 365 : if (can_do_nrvo_p (arg, type))
11157 : : {
11158 : 48 : auto_diagnostic_group d;
11159 : 48 : if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
11160 : 48 : && warning_at (loc, OPT_Wpessimizing_move,
11161 : : "moving a local object in a return statement "
11162 : : "prevents copy elision"))
11163 : 42 : inform (loc, "remove %<std::move%> call");
11164 : 48 : }
11165 : : /* Warn if the move is redundant. It is redundant when we would
11166 : : do maybe-rvalue overload resolution even without std::move. */
11167 : 317 : else if (warn_redundant_move
11168 : : /* This doesn't apply for return {std::move (t)};. */
11169 : 200 : && !wrapped_p
11170 : 197 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11171 : 413 : && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
11172 : : {
11173 : : /* Make sure that overload resolution would actually succeed
11174 : : if we removed the std::move call. */
11175 : 54 : tree t = convert_for_initialization (NULL_TREE, type,
11176 : : moved,
11177 : : (LOOKUP_NORMAL
11178 : : | LOOKUP_ONLYCONVERTING),
11179 : : ICR_RETURN, NULL_TREE, 0,
11180 : : tf_none);
11181 : : /* If this worked, implicit rvalue would work, so the call to
11182 : : std::move is redundant. */
11183 : 54 : if (t != error_mark_node)
11184 : : {
11185 : 54 : auto_diagnostic_group d;
11186 : 54 : if (warning_at (loc, OPT_Wredundant_move,
11187 : : "redundant move in return statement"))
11188 : 54 : inform (loc, "remove %<std::move%> call");
11189 : 54 : }
11190 : : }
11191 : : }
11192 : : /* Also try to warn about redundant std::move in code such as
11193 : : T f (const T& t)
11194 : : {
11195 : : return std::move(t);
11196 : : }
11197 : : for which EXPR will be something like
11198 : : *std::move<const T&> ((const struct T &) (const struct T *) t)
11199 : : and where the std::move does nothing if T does not have a T(const T&&)
11200 : : constructor, because the argument is const. It will not use T(T&&)
11201 : : because that would mean losing the const. */
11202 : 610 : else if (warn_redundant_move
11203 : 469 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11204 : 64 : && TYPE_REF_P (TREE_TYPE (arg))
11205 : 674 : && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
11206 : : {
11207 : 18 : tree rtype = TREE_TYPE (TREE_TYPE (arg));
11208 : 18 : if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
11209 : 9 : return;
11210 : : /* Check for the unlikely case there's T(const T&&) (we don't care if
11211 : : it's deleted). */
11212 : 54 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
11213 : 30 : if (move_fn_p (fn))
11214 : : {
11215 : 15 : tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11216 : 15 : if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
11217 : 6 : return;
11218 : : }
11219 : 9 : auto_diagnostic_group d;
11220 : 18 : if (return_p
11221 : 9 : ? warning_at (loc, OPT_Wredundant_move,
11222 : : "redundant move in return statement")
11223 : 9 : : warning_at (loc, OPT_Wredundant_move,
11224 : : "redundant move in initialization"))
11225 : 9 : inform (loc, "remove %<std::move%> call");
11226 : 9 : }
11227 : : }
11228 : :
11229 : : /* Check that returning RETVAL from the current function is valid.
11230 : : Return an expression explicitly showing all conversions required to
11231 : : change RETVAL into the function return type, and to assign it to
11232 : : the DECL_RESULT for the function. Set *NO_WARNING to true if
11233 : : code reaches end of non-void function warning shouldn't be issued
11234 : : on this RETURN_EXPR. Set *DANGLING to true if code returns the
11235 : : address of a local variable. */
11236 : :
11237 : : tree
11238 : 105024279 : check_return_expr (tree retval, bool *no_warning, bool *dangling)
11239 : : {
11240 : 105024279 : tree result;
11241 : : /* The type actually returned by the function. */
11242 : 105024279 : tree valtype;
11243 : : /* The type the function is declared to return, or void if
11244 : : the declared type is incomplete. */
11245 : 105024279 : tree functype;
11246 : 105024279 : int fn_returns_value_p;
11247 : 105024279 : location_t loc = cp_expr_loc_or_input_loc (retval);
11248 : :
11249 : 105024279 : *no_warning = false;
11250 : 105024279 : *dangling = false;
11251 : :
11252 : : /* A `volatile' function is one that isn't supposed to return, ever.
11253 : : (This is a G++ extension, used to get better code for functions
11254 : : that call the `volatile' function.) */
11255 : 105024279 : if (TREE_THIS_VOLATILE (current_function_decl))
11256 : 12 : warning (0, "function declared %<noreturn%> has a %<return%> statement");
11257 : :
11258 : : /* Check for various simple errors. */
11259 : 210048558 : if (DECL_DESTRUCTOR_P (current_function_decl))
11260 : : {
11261 : 3356 : if (retval)
11262 : 3 : error_at (loc, "returning a value from a destructor");
11263 : :
11264 : 3356 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11265 : 0 : retval = current_class_ptr;
11266 : : else
11267 : : return NULL_TREE;
11268 : : }
11269 : 105020923 : else if (DECL_CONSTRUCTOR_P (current_function_decl))
11270 : : {
11271 : 56369 : if (in_function_try_handler)
11272 : : /* If a return statement appears in a handler of the
11273 : : function-try-block of a constructor, the program is ill-formed. */
11274 : 0 : error ("cannot return from a handler of a function-try-block of a constructor");
11275 : 56369 : else if (retval)
11276 : : /* You can't return a value from a constructor. */
11277 : 3 : error_at (loc, "returning a value from a constructor");
11278 : :
11279 : 56369 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11280 : 0 : retval = current_class_ptr;
11281 : : else
11282 : : return NULL_TREE;
11283 : : }
11284 : :
11285 : 104964554 : const tree saved_retval = retval;
11286 : :
11287 : 104964554 : if (processing_template_decl)
11288 : : {
11289 : 67911997 : current_function_returns_value = 1;
11290 : :
11291 : 67911997 : if (check_for_bare_parameter_packs (retval))
11292 : 13 : return error_mark_node;
11293 : :
11294 : : /* If one of the types might be void, we can't tell whether we're
11295 : : returning a value. */
11296 : 67911984 : if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11297 : 26627386 : && !FNDECL_USED_AUTO (current_function_decl))
11298 : 41284607 : || (retval != NULL_TREE
11299 : 40201585 : && (TREE_TYPE (retval) == NULL_TREE
11300 : 28110196 : || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11301 : 46098498 : goto dependent;
11302 : : }
11303 : :
11304 : 58866043 : functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11305 : :
11306 : : /* Deduce auto return type from a return statement. */
11307 : 58866043 : if (FNDECL_USED_AUTO (current_function_decl))
11308 : : {
11309 : 958839 : tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11310 : 958839 : tree auto_node;
11311 : 958839 : tree type;
11312 : :
11313 : 958839 : if (!retval && !is_auto (pattern))
11314 : : {
11315 : : /* Give a helpful error message. */
11316 : 3 : auto_diagnostic_group d;
11317 : 3 : error ("return-statement with no value, in function returning %qT",
11318 : : pattern);
11319 : 3 : inform (input_location, "only plain %<auto%> return type can be "
11320 : : "deduced to %<void%>");
11321 : 3 : type = error_mark_node;
11322 : 3 : }
11323 : 958836 : else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11324 : : {
11325 : 6 : error ("returning initializer list");
11326 : 6 : type = error_mark_node;
11327 : : }
11328 : : else
11329 : : {
11330 : 958830 : if (!retval)
11331 : 5327 : retval = void_node;
11332 : 958830 : auto_node = type_uses_auto (pattern);
11333 : 958830 : type = do_auto_deduction (pattern, retval, auto_node,
11334 : : tf_warning_or_error, adc_return_type);
11335 : : }
11336 : :
11337 : 958839 : if (type == error_mark_node)
11338 : : /* Leave it. */;
11339 : 958639 : else if (functype == pattern)
11340 : 607457 : apply_deduced_return_type (current_function_decl, type);
11341 : 351182 : else if (!same_type_p (type, functype))
11342 : : {
11343 : 24 : if (LAMBDA_FUNCTION_P (current_function_decl))
11344 : 6 : error_at (loc, "inconsistent types %qT and %qT deduced for "
11345 : : "lambda return type", functype, type);
11346 : : else
11347 : 12 : error_at (loc, "inconsistent deduction for auto return type: "
11348 : : "%qT and then %qT", functype, type);
11349 : : }
11350 : : functype = type;
11351 : : }
11352 : :
11353 : 58866043 : result = DECL_RESULT (current_function_decl);
11354 : 58866043 : valtype = TREE_TYPE (result);
11355 : 58866043 : gcc_assert (valtype != NULL_TREE);
11356 : 58866043 : fn_returns_value_p = !VOID_TYPE_P (valtype);
11357 : :
11358 : : /* Check for a return statement with no return value in a function
11359 : : that's supposed to return a value. */
11360 : 58866043 : if (!retval && fn_returns_value_p)
11361 : : {
11362 : 35 : if (functype != error_mark_node)
11363 : 32 : permerror (input_location, "return-statement with no value, in "
11364 : : "function returning %qT", valtype);
11365 : : /* Remember that this function did return. */
11366 : 35 : current_function_returns_value = 1;
11367 : : /* But suppress NRV .*/
11368 : 35 : current_function_return_value = error_mark_node;
11369 : : /* And signal caller that TREE_NO_WARNING should be set on the
11370 : : RETURN_EXPR to avoid control reaches end of non-void function
11371 : : warnings in tree-cfg.cc. */
11372 : 35 : *no_warning = true;
11373 : : }
11374 : : /* Check for a return statement with a value in a function that
11375 : : isn't supposed to return a value. */
11376 : 58866008 : else if (retval && !fn_returns_value_p)
11377 : : {
11378 : 261359 : if (VOID_TYPE_P (TREE_TYPE (retval)))
11379 : : /* You can return a `void' value from a function of `void'
11380 : : type. In that case, we have to evaluate the expression for
11381 : : its side-effects. */
11382 : 261322 : finish_expr_stmt (retval);
11383 : 37 : else if (retval != error_mark_node)
11384 : 34 : permerror (loc, "return-statement with a value, in function "
11385 : : "returning %qT", valtype);
11386 : 261359 : current_function_returns_null = 1;
11387 : :
11388 : : /* There's really no value to return, after all. */
11389 : 261359 : return NULL_TREE;
11390 : : }
11391 : 58604649 : else if (!retval)
11392 : : /* Remember that this function can sometimes return without a
11393 : : value. */
11394 : 1509186 : current_function_returns_null = 1;
11395 : : else
11396 : : /* Remember that this function did return a value. */
11397 : 57095463 : current_function_returns_value = 1;
11398 : :
11399 : : /* Check for erroneous operands -- but after giving ourselves a
11400 : : chance to provide an error about returning a value from a void
11401 : : function. */
11402 : 58604684 : if (error_operand_p (retval))
11403 : : {
11404 : 964 : current_function_return_value = error_mark_node;
11405 : 964 : return error_mark_node;
11406 : : }
11407 : :
11408 : : /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11409 : 117207440 : if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11410 : 30420 : && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11411 : 3112 : && ! flag_check_new
11412 : 58606823 : && retval && null_ptr_cst_p (retval))
11413 : 24 : warning (0, "%<operator new%> must not return NULL unless it is "
11414 : : "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11415 : :
11416 : : /* Effective C++ rule 15. See also start_function. */
11417 : 58603720 : if (warn_ecpp
11418 : 42 : && DECL_NAME (current_function_decl) == assign_op_identifier
11419 : 58603750 : && !type_dependent_expression_p (retval))
11420 : : {
11421 : 27 : bool warn = true;
11422 : :
11423 : : /* The function return type must be a reference to the current
11424 : : class. */
11425 : 27 : if (TYPE_REF_P (valtype)
11426 : 45 : && same_type_ignoring_top_level_qualifiers_p
11427 : 18 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11428 : : {
11429 : : /* Returning '*this' is obviously OK. */
11430 : 18 : if (retval == current_class_ref)
11431 : : warn = false;
11432 : : /* If we are calling a function whose return type is the same of
11433 : : the current class reference, it is ok. */
11434 : 6 : else if (INDIRECT_REF_P (retval)
11435 : 6 : && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11436 : : warn = false;
11437 : : }
11438 : :
11439 : : if (warn)
11440 : 9 : warning_at (loc, OPT_Weffc__,
11441 : : "%<operator=%> should return a reference to %<*this%>");
11442 : : }
11443 : :
11444 : 58603720 : if (dependent_type_p (functype)
11445 : 58603720 : || type_dependent_expression_p (retval))
11446 : : {
11447 : 59747665 : dependent:
11448 : : /* We should not have changed the return value. */
11449 : 59747665 : gcc_assert (retval == saved_retval);
11450 : : /* We don't know if this is an lvalue or rvalue use, but
11451 : : either way we can mark it as read. */
11452 : 59747665 : mark_exp_read (retval);
11453 : 59747665 : return retval;
11454 : : }
11455 : :
11456 : : /* The fabled Named Return Value optimization, as per [class.copy]/15:
11457 : :
11458 : : [...] For a function with a class return type, if the expression
11459 : : in the return statement is the name of a local object, and the cv-
11460 : : unqualified type of the local object is the same as the function
11461 : : return type, an implementation is permitted to omit creating the tem-
11462 : : porary object to hold the function return value [...]
11463 : :
11464 : : So, if this is a value-returning function that always returns the same
11465 : : local variable, remember it.
11466 : :
11467 : : We choose the first suitable variable even if the function sometimes
11468 : : returns something else, but only if the variable is out of scope at the
11469 : : other return sites, or else we run the risk of clobbering the variable we
11470 : : chose if the other returned expression uses the chosen variable somehow.
11471 : :
11472 : : We don't currently do this if the first return is a non-variable, as it
11473 : : would be complicated to determine whether an NRV selected later was in
11474 : : scope at the point of the earlier return. We also don't currently support
11475 : : multiple variables with non-overlapping scopes (53637).
11476 : :
11477 : : See finish_function and finalize_nrv for the rest of this optimization. */
11478 : 44954553 : tree bare_retval = NULL_TREE;
11479 : 44954553 : if (retval)
11480 : : {
11481 : 43445347 : retval = maybe_undo_parenthesized_ref (retval);
11482 : 43445347 : bare_retval = tree_strip_any_location_wrapper (retval);
11483 : : }
11484 : :
11485 : 44954553 : bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11486 : 44954553 : if (fn_returns_value_p && flag_elide_constructors
11487 : 43445326 : && current_function_return_value != bare_retval)
11488 : : {
11489 : 43433855 : if (named_return_value_okay_p
11490 : 221099 : && current_function_return_value == NULL_TREE)
11491 : 180214 : current_function_return_value = bare_retval;
11492 : 43253641 : else if (current_function_return_value
11493 : 8091374 : && VAR_P (current_function_return_value)
11494 : 172 : && DECL_NAME (current_function_return_value)
11495 : 43253813 : && !decl_in_scope_p (current_function_return_value))
11496 : : {
11497 : : /* The earlier NRV is out of scope at this point, so it's safe to
11498 : 103 : leave it alone; the current return can't refer to it. */;
11499 : 103 : if (named_return_value_okay_p
11500 : 103 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11501 : : {
11502 : 8 : warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11503 : : bare_retval);
11504 : 8 : suppress_warning (current_function_decl, OPT_Wnrvo);
11505 : : }
11506 : : }
11507 : : else
11508 : : {
11509 : 43253538 : if ((named_return_value_okay_p
11510 : 43212663 : || (current_function_return_value
11511 : 8050396 : && current_function_return_value != error_mark_node))
11512 : 43253585 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11513 : : {
11514 : 40859 : warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11515 : : current_function_decl);
11516 : 40859 : suppress_warning (current_function_decl, OPT_Wnrvo);
11517 : : }
11518 : 43253538 : current_function_return_value = error_mark_node;
11519 : : }
11520 : : }
11521 : :
11522 : : /* We don't need to do any conversions when there's nothing being
11523 : : returned. */
11524 : 44954553 : if (!retval)
11525 : : return NULL_TREE;
11526 : :
11527 : 43445347 : if (!named_return_value_okay_p)
11528 : 43212777 : maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11529 : :
11530 : : /* Do any required conversions. */
11531 : 86890694 : if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11532 : : /* No conversions are required. */
11533 : : ;
11534 : : else
11535 : : {
11536 : 43445347 : int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11537 : :
11538 : : /* The functype's return type will have been set to void, if it
11539 : : was an incomplete type. Just treat this as 'return;' */
11540 : 43445347 : if (VOID_TYPE_P (functype))
11541 : 6 : return error_mark_node;
11542 : :
11543 : : /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11544 : : treated as an rvalue for the purposes of overload resolution to
11545 : : favor move constructors over copy constructors.
11546 : :
11547 : : Note that these conditions are similar to, but not as strict as,
11548 : : the conditions for the named return value optimization. */
11549 : 43445341 : bool converted = false;
11550 : 43445341 : tree moved;
11551 : : /* Until C++23, this was only interesting for class type, but in C++23,
11552 : : we should do the below when we're converting from/to a class/reference
11553 : : (a non-scalar type). */
11554 : 43445341 : if ((cxx_dialect < cxx23
11555 : 4583836 : ? CLASS_TYPE_P (functype)
11556 : 11277031 : : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11557 : 51303327 : && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11558 : : /* In C++20 and earlier we treat the return value as an rvalue
11559 : : that can bind to lvalue refs. In C++23, such an expression is just
11560 : : an xvalue (see reference_binding). */
11561 : : retval = moved;
11562 : :
11563 : : /* The call in a (lambda) thunk needs no conversions. */
11564 : 43445341 : if (TREE_CODE (retval) == CALL_EXPR
11565 : 43445341 : && call_from_lambda_thunk_p (retval))
11566 : : converted = true;
11567 : :
11568 : : /* Don't check copy-initialization for NRV in a coroutine ramp; we
11569 : : implement this case as NRV, but it's specified as directly
11570 : : initializing the return value from get_return_object(). */
11571 : 43445341 : if (DECL_RAMP_P (current_function_decl) && named_return_value_okay_p)
11572 : : converted = true;
11573 : :
11574 : : /* First convert the value to the function's return type, then
11575 : : to the type of return value's location to handle the
11576 : : case that functype is smaller than the valtype. */
11577 : 43445092 : if (!converted)
11578 : 43424208 : retval = convert_for_initialization
11579 : 43424208 : (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11580 : : tf_warning_or_error);
11581 : 43445341 : retval = convert (valtype, retval);
11582 : :
11583 : : /* If the conversion failed, treat this just like `return;'. */
11584 : 43445341 : if (retval == error_mark_node)
11585 : : {
11586 : : /* And suppress NRV. */
11587 : 189 : current_function_return_value = error_mark_node;
11588 : 189 : return retval;
11589 : : }
11590 : : /* We can't initialize a register from a AGGR_INIT_EXPR. */
11591 : 43445152 : else if (! cfun->returns_struct
11592 : 41874426 : && TREE_CODE (retval) == TARGET_EXPR
11593 : 48002481 : && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == AGGR_INIT_EXPR)
11594 : 780969 : retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11595 : 780969 : TARGET_EXPR_SLOT (retval));
11596 : 42664183 : else if (!processing_template_decl
11597 : 35592945 : && maybe_warn_about_returning_address_of_local (retval, loc)
11598 : 42664364 : && INDIRECT_TYPE_P (valtype))
11599 : 166 : *dangling = true;
11600 : : }
11601 : :
11602 : : /* A naive attempt to reduce the number of -Wdangling-reference false
11603 : : positives: if we know that this function can return a variable with
11604 : : static storage duration rather than one of its parameters, suppress
11605 : : the warning. */
11606 : 43445152 : if (warn_dangling_reference
11607 : 382570 : && TYPE_REF_P (functype)
11608 : 25412 : && bare_retval
11609 : 25412 : && VAR_P (bare_retval)
11610 : 50 : && TREE_STATIC (bare_retval))
11611 : 50 : suppress_warning (current_function_decl, OPT_Wdangling_reference);
11612 : :
11613 : 43445152 : if (processing_template_decl)
11614 : : return saved_retval;
11615 : :
11616 : : /* Actually copy the value returned into the appropriate location. */
11617 : 36373914 : if (retval && retval != result)
11618 : 36373914 : retval = cp_build_init_expr (result, retval);
11619 : :
11620 : 36373914 : if (current_function_return_value == bare_retval)
11621 : 191679 : INIT_EXPR_NRV_P (retval) = true;
11622 : :
11623 : 36373914 : if (tree set = maybe_set_retval_sentinel ())
11624 : 137863 : retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11625 : :
11626 : : return retval;
11627 : : }
11628 : :
11629 : :
11630 : : /* Returns nonzero if the pointer-type FROM can be converted to the
11631 : : pointer-type TO via a qualification conversion. If CONSTP is -1,
11632 : : then we return nonzero if the pointers are similar, and the
11633 : : cv-qualification signature of FROM is a proper subset of that of TO.
11634 : :
11635 : : If CONSTP is positive, then all outer pointers have been
11636 : : const-qualified. */
11637 : :
11638 : : static bool
11639 : 128875711 : comp_ptr_ttypes_real (tree to, tree from, int constp)
11640 : : {
11641 : 128875711 : bool to_more_cv_qualified = false;
11642 : 128875711 : bool is_opaque_pointer = false;
11643 : :
11644 : 530609 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11645 : : {
11646 : 129406320 : if (TREE_CODE (to) != TREE_CODE (from))
11647 : : return false;
11648 : :
11649 : 82687300 : if (TREE_CODE (from) == OFFSET_TYPE
11650 : 82687300 : && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11651 : : TYPE_OFFSET_BASETYPE (to)))
11652 : : return false;
11653 : :
11654 : : /* Const and volatile mean something different for function and
11655 : : array types, so the usual checks are not appropriate. We'll
11656 : : check the array type elements in further iterations. */
11657 : 82687300 : if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11658 : : {
11659 : 82456962 : if (!at_least_as_qualified_p (to, from))
11660 : : return false;
11661 : :
11662 : 72901929 : if (!at_least_as_qualified_p (from, to))
11663 : : {
11664 : 50983528 : if (constp == 0)
11665 : : return false;
11666 : : to_more_cv_qualified = true;
11667 : : }
11668 : :
11669 : 72901667 : if (constp > 0)
11670 : 72897030 : constp &= TYPE_READONLY (to);
11671 : : }
11672 : :
11673 : 73132005 : if (VECTOR_TYPE_P (to))
11674 : 6010 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11675 : :
11676 : : /* P0388R4 allows a conversion from int[N] to int[] but not the
11677 : : other way round. When both arrays have bounds but they do
11678 : : not match, then no conversion is possible. */
11679 : 73132005 : if (TREE_CODE (to) == ARRAY_TYPE
11680 : 73132005 : && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11681 : : return false;
11682 : :
11683 : 73131709 : if (!TYPE_PTR_P (to)
11684 : : && !TYPE_PTRDATAMEM_P (to)
11685 : : /* CWG 330 says we need to look through arrays. */
11686 : : && TREE_CODE (to) != ARRAY_TYPE)
11687 : 72601100 : return ((constp >= 0 || to_more_cv_qualified)
11688 : 72601100 : && (is_opaque_pointer
11689 : 72600534 : || same_type_ignoring_top_level_qualifiers_p (to, from)));
11690 : : }
11691 : : }
11692 : :
11693 : : /* When comparing, say, char ** to char const **, this function takes
11694 : : the 'char *' and 'char const *'. Do not pass non-pointer/reference
11695 : : types to this function. */
11696 : :
11697 : : int
11698 : 128875035 : comp_ptr_ttypes (tree to, tree from)
11699 : : {
11700 : 128875035 : return comp_ptr_ttypes_real (to, from, 1);
11701 : : }
11702 : :
11703 : : /* Returns true iff FNTYPE is a non-class type that involves
11704 : : error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11705 : : if a parameter type is ill-formed. */
11706 : :
11707 : : bool
11708 : 633075 : error_type_p (const_tree type)
11709 : : {
11710 : 677444 : tree t;
11711 : :
11712 : 677444 : switch (TREE_CODE (type))
11713 : : {
11714 : : case ERROR_MARK:
11715 : : return true;
11716 : :
11717 : 44280 : case POINTER_TYPE:
11718 : 44280 : case REFERENCE_TYPE:
11719 : 44280 : case OFFSET_TYPE:
11720 : 44280 : return error_type_p (TREE_TYPE (type));
11721 : :
11722 : 3761 : case FUNCTION_TYPE:
11723 : 3761 : case METHOD_TYPE:
11724 : 3761 : if (error_type_p (TREE_TYPE (type)))
11725 : : return true;
11726 : 10399 : for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11727 : 6644 : if (error_type_p (TREE_VALUE (t)))
11728 : : return true;
11729 : : return false;
11730 : :
11731 : 202432 : case RECORD_TYPE:
11732 : 202432 : if (TYPE_PTRMEMFUNC_P (type))
11733 : 89 : return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11734 : : return false;
11735 : :
11736 : : default:
11737 : : return false;
11738 : : }
11739 : : }
11740 : :
11741 : : /* Returns true if to and from are (possibly multi-level) pointers to the same
11742 : : type or inheritance-related types, regardless of cv-quals. */
11743 : :
11744 : : bool
11745 : 90692217 : ptr_reasonably_similar (const_tree to, const_tree from)
11746 : : {
11747 : 1020 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11748 : : {
11749 : : /* Any target type is similar enough to void. */
11750 : 90693237 : if (VOID_TYPE_P (to))
11751 : 1082 : return !error_type_p (from);
11752 : 90692155 : if (VOID_TYPE_P (from))
11753 : 618600 : return !error_type_p (to);
11754 : :
11755 : 90073555 : if (TREE_CODE (to) != TREE_CODE (from))
11756 : : return false;
11757 : :
11758 : 43834559 : if (TREE_CODE (from) == OFFSET_TYPE
11759 : 43834559 : && comptypes (TYPE_OFFSET_BASETYPE (to),
11760 : 3 : TYPE_OFFSET_BASETYPE (from),
11761 : : COMPARE_BASE | COMPARE_DERIVED))
11762 : 3 : continue;
11763 : :
11764 : 43834553 : if (VECTOR_TYPE_P (to)
11765 : 43834553 : && vector_types_convertible_p (to, from, false))
11766 : : return true;
11767 : :
11768 : 43834469 : if (TREE_CODE (to) == INTEGER_TYPE
11769 : 43834469 : && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11770 : : return true;
11771 : :
11772 : 43718994 : if (TREE_CODE (to) == FUNCTION_TYPE)
11773 : 1494 : return !error_type_p (to) && !error_type_p (from);
11774 : :
11775 : 43717500 : if (!TYPE_PTR_P (to))
11776 : : {
11777 : : /* When either type is incomplete avoid DERIVED_FROM_P,
11778 : : which may call complete_type (c++/57942). */
11779 : 43716483 : bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11780 : : return comptypes
11781 : 43716483 : (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11782 : 43716483 : b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11783 : : }
11784 : 1020 : }
11785 : : }
11786 : :
11787 : : /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11788 : : pointer-to-member types) are the same, ignoring cv-qualification at
11789 : : all levels. CB says how we should behave when comparing array bounds. */
11790 : :
11791 : : bool
11792 : 787810 : comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11793 : : {
11794 : 787810 : bool is_opaque_pointer = false;
11795 : :
11796 : 788604 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11797 : : {
11798 : 1576414 : if (TREE_CODE (to) != TREE_CODE (from))
11799 : : return false;
11800 : :
11801 : 1223669 : if (TREE_CODE (from) == OFFSET_TYPE
11802 : 1223631 : && same_type_p (TYPE_OFFSET_BASETYPE (from),
11803 : : TYPE_OFFSET_BASETYPE (to)))
11804 : 38 : continue;
11805 : :
11806 : 1223593 : if (VECTOR_TYPE_P (to))
11807 : 7559 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11808 : :
11809 : 1223593 : if (TREE_CODE (to) == ARRAY_TYPE
11810 : : /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11811 : : we must fail. */
11812 : 1223593 : && !comp_array_types (to, from, cb, /*strict=*/false))
11813 : : return false;
11814 : :
11815 : : /* CWG 330 says we need to look through arrays. */
11816 : 1222890 : if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11817 : 434324 : return (is_opaque_pointer
11818 : 434324 : || same_type_ignoring_top_level_qualifiers_p (to, from));
11819 : : }
11820 : : }
11821 : :
11822 : : /* Returns the type qualifiers for this type, including the qualifiers on the
11823 : : elements for an array type. */
11824 : :
11825 : : int
11826 : 48810704907 : cp_type_quals (const_tree type)
11827 : : {
11828 : 48810704907 : int quals;
11829 : : /* This CONST_CAST is okay because strip_array_types returns its
11830 : : argument unmodified and we assign it to a const_tree. */
11831 : 48810704907 : type = strip_array_types (CONST_CAST_TREE (type));
11832 : 48810704907 : if (type == error_mark_node
11833 : : /* Quals on a FUNCTION_TYPE are memfn quals. */
11834 : 48780331151 : || TREE_CODE (type) == FUNCTION_TYPE)
11835 : : return TYPE_UNQUALIFIED;
11836 : 48735388864 : quals = TYPE_QUALS (type);
11837 : : /* METHOD and REFERENCE_TYPEs should never have quals. */
11838 : 48735388864 : gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11839 : : && !TYPE_REF_P (type))
11840 : : || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11841 : : == TYPE_UNQUALIFIED));
11842 : : return quals;
11843 : : }
11844 : :
11845 : : /* Returns the function-ref-qualifier for TYPE */
11846 : :
11847 : : cp_ref_qualifier
11848 : 2733283808 : type_memfn_rqual (const_tree type)
11849 : : {
11850 : 2733283808 : gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11851 : :
11852 : 2733283808 : if (!FUNCTION_REF_QUALIFIED (type))
11853 : : return REF_QUAL_NONE;
11854 : 32357656 : else if (FUNCTION_RVALUE_QUALIFIED (type))
11855 : : return REF_QUAL_RVALUE;
11856 : : else
11857 : 16228662 : return REF_QUAL_LVALUE;
11858 : : }
11859 : :
11860 : : /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11861 : : METHOD_TYPE. */
11862 : :
11863 : : int
11864 : 993767566 : type_memfn_quals (const_tree type)
11865 : : {
11866 : 993767566 : if (TREE_CODE (type) == FUNCTION_TYPE)
11867 : 78368991 : return TYPE_QUALS (type);
11868 : 915398575 : else if (TREE_CODE (type) == METHOD_TYPE)
11869 : 915398575 : return cp_type_quals (class_of_this_parm (type));
11870 : : else
11871 : 0 : gcc_unreachable ();
11872 : : }
11873 : :
11874 : : /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11875 : : MEMFN_QUALS and its ref-qualifier to RQUAL. */
11876 : :
11877 : : tree
11878 : 55198557 : apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11879 : : {
11880 : : /* Could handle METHOD_TYPE here if necessary. */
11881 : 55198557 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11882 : 55198557 : if (TYPE_QUALS (type) == memfn_quals
11883 : 55198557 : && type_memfn_rqual (type) == rqual)
11884 : : return type;
11885 : :
11886 : : /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11887 : : complex. */
11888 : 661101 : tree result = build_qualified_type (type, memfn_quals);
11889 : 661101 : return build_ref_qualified_type (result, rqual);
11890 : : }
11891 : :
11892 : : /* Returns nonzero if TYPE is const or volatile. */
11893 : :
11894 : : bool
11895 : 1008294806 : cv_qualified_p (const_tree type)
11896 : : {
11897 : 1008294806 : int quals = cp_type_quals (type);
11898 : 1008294806 : return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11899 : : }
11900 : :
11901 : : /* Returns nonzero if the TYPE contains a mutable member. */
11902 : :
11903 : : bool
11904 : 811703127 : cp_has_mutable_p (const_tree type)
11905 : : {
11906 : : /* This CONST_CAST is okay because strip_array_types returns its
11907 : : argument unmodified and we assign it to a const_tree. */
11908 : 811703127 : type = strip_array_types (CONST_CAST_TREE(type));
11909 : :
11910 : 811703127 : return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11911 : : }
11912 : :
11913 : : /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11914 : : TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11915 : : approximation. In particular, consider:
11916 : :
11917 : : int f();
11918 : : struct S { int i; };
11919 : : const S s = { f(); }
11920 : :
11921 : : Here, we will make "s" as TREE_READONLY (because it is declared
11922 : : "const") -- only to reverse ourselves upon seeing that the
11923 : : initializer is non-constant. */
11924 : :
11925 : : void
11926 : 865439104 : cp_apply_type_quals_to_decl (int type_quals, tree decl)
11927 : : {
11928 : 865439104 : tree type = TREE_TYPE (decl);
11929 : :
11930 : 865439104 : if (type == error_mark_node)
11931 : : return;
11932 : :
11933 : 865438430 : if (TREE_CODE (decl) == TYPE_DECL)
11934 : : return;
11935 : :
11936 : 773204452 : gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11937 : : && type_quals != TYPE_UNQUALIFIED));
11938 : :
11939 : : /* Avoid setting TREE_READONLY incorrectly. */
11940 : : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11941 : : constructor can produce constant init, so rely on cp_finish_decl to
11942 : : clear TREE_READONLY if the variable has non-constant init. */
11943 : :
11944 : : /* If the type has (or might have) a mutable component, that component
11945 : : might be modified. */
11946 : 773204452 : if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11947 : 69467742 : type_quals &= ~TYPE_QUAL_CONST;
11948 : :
11949 : 773204452 : c_apply_type_quals_to_decl (type_quals, decl);
11950 : : }
11951 : :
11952 : : /* Subroutine of casts_away_constness. Make T1 and T2 point at
11953 : : exemplar types such that casting T1 to T2 is casting away constness
11954 : : if and only if there is no implicit conversion from T1 to T2. */
11955 : :
11956 : : static void
11957 : 1117691 : casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11958 : : {
11959 : 1117691 : int quals1;
11960 : 1117691 : int quals2;
11961 : :
11962 : : /* [expr.const.cast]
11963 : :
11964 : : For multi-level pointer to members and multi-level mixed pointers
11965 : : and pointers to members (conv.qual), the "member" aspect of a
11966 : : pointer to member level is ignored when determining if a const
11967 : : cv-qualifier has been cast away. */
11968 : : /* [expr.const.cast]
11969 : :
11970 : : For two pointer types:
11971 : :
11972 : : X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11973 : : X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11974 : : K is min(N,M)
11975 : :
11976 : : casting from X1 to X2 casts away constness if, for a non-pointer
11977 : : type T there does not exist an implicit conversion (clause
11978 : : _conv_) from:
11979 : :
11980 : : Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11981 : :
11982 : : to
11983 : :
11984 : : Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11985 : 1117691 : if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11986 : 559192 : || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11987 : : {
11988 : 558673 : *t1 = cp_build_qualified_type (void_type_node,
11989 : : cp_type_quals (*t1));
11990 : 558673 : *t2 = cp_build_qualified_type (void_type_node,
11991 : : cp_type_quals (*t2));
11992 : 558673 : return;
11993 : : }
11994 : :
11995 : 559018 : quals1 = cp_type_quals (*t1);
11996 : 559018 : quals2 = cp_type_quals (*t2);
11997 : :
11998 : 559018 : if (TYPE_PTRDATAMEM_P (*t1))
11999 : 0 : *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
12000 : : else
12001 : 559018 : *t1 = TREE_TYPE (*t1);
12002 : 559018 : if (TYPE_PTRDATAMEM_P (*t2))
12003 : 0 : *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
12004 : : else
12005 : 559018 : *t2 = TREE_TYPE (*t2);
12006 : :
12007 : 559018 : casts_away_constness_r (t1, t2, complain);
12008 : 559018 : *t1 = build_pointer_type (*t1);
12009 : 559018 : *t2 = build_pointer_type (*t2);
12010 : 559018 : *t1 = cp_build_qualified_type (*t1, quals1);
12011 : 559018 : *t2 = cp_build_qualified_type (*t2, quals2);
12012 : : }
12013 : :
12014 : : /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
12015 : : constness.
12016 : :
12017 : : ??? This function returns non-zero if casting away qualifiers not
12018 : : just const. We would like to return to the caller exactly which
12019 : : qualifiers are casted away to give more accurate diagnostics.
12020 : : */
12021 : :
12022 : : static bool
12023 : 558745 : casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
12024 : : {
12025 : 558745 : if (TYPE_REF_P (t2))
12026 : : {
12027 : : /* [expr.const.cast]
12028 : :
12029 : : Casting from an lvalue of type T1 to an lvalue of type T2
12030 : : using a reference cast casts away constness if a cast from an
12031 : : rvalue of type "pointer to T1" to the type "pointer to T2"
12032 : : casts away constness. */
12033 : 0 : t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
12034 : 0 : return casts_away_constness (build_pointer_type (t1),
12035 : 0 : build_pointer_type (TREE_TYPE (t2)),
12036 : 0 : complain);
12037 : : }
12038 : :
12039 : 558745 : if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
12040 : : /* [expr.const.cast]
12041 : :
12042 : : Casting from an rvalue of type "pointer to data member of X
12043 : : of type T1" to the type "pointer to data member of Y of type
12044 : : T2" casts away constness if a cast from an rvalue of type
12045 : : "pointer to T1" to the type "pointer to T2" casts away
12046 : : constness. */
12047 : 48 : return casts_away_constness
12048 : 48 : (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
12049 : 48 : build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
12050 : 48 : complain);
12051 : :
12052 : : /* Casting away constness is only something that makes sense for
12053 : : pointer or reference types. */
12054 : 558697 : if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
12055 : : return false;
12056 : :
12057 : : /* Top-level qualifiers don't matter. */
12058 : 558673 : t1 = TYPE_MAIN_VARIANT (t1);
12059 : 558673 : t2 = TYPE_MAIN_VARIANT (t2);
12060 : 558673 : casts_away_constness_r (&t1, &t2, complain);
12061 : 558673 : if (!can_convert (t2, t1, complain))
12062 : : return true;
12063 : :
12064 : : return false;
12065 : : }
12066 : :
12067 : : /* If T is a REFERENCE_TYPE return the type to which T refers.
12068 : : Otherwise, return T itself. */
12069 : :
12070 : : tree
12071 : 3017503383 : non_reference (tree t)
12072 : : {
12073 : 3017503383 : if (t && TYPE_REF_P (t))
12074 : 286815705 : t = TREE_TYPE (t);
12075 : 3017503383 : return t;
12076 : : }
12077 : :
12078 : :
12079 : : /* Return nonzero if REF is an lvalue valid for this language;
12080 : : otherwise, print an error message and return zero. USE says
12081 : : how the lvalue is being used and so selects the error message. */
12082 : :
12083 : : int
12084 : 37587678 : lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
12085 : : {
12086 : 37587678 : cp_lvalue_kind kind = lvalue_kind (ref);
12087 : :
12088 : 37587678 : if (kind == clk_none)
12089 : : {
12090 : 128 : if (complain & tf_error)
12091 : 115 : lvalue_error (cp_expr_loc_or_input_loc (ref), use);
12092 : 128 : return 0;
12093 : : }
12094 : 37587550 : else if (kind & (clk_rvalueref|clk_class))
12095 : : {
12096 : 104 : if (!(complain & tf_error))
12097 : : return 0;
12098 : : /* Make this a permerror because we used to accept it. */
12099 : 18 : permerror (cp_expr_loc_or_input_loc (ref),
12100 : : "using rvalue as lvalue");
12101 : : }
12102 : : return 1;
12103 : : }
12104 : :
12105 : : /* Return true if a user-defined literal operator is a raw operator. */
12106 : :
12107 : : bool
12108 : 119566 : check_raw_literal_operator (const_tree decl)
12109 : : {
12110 : 119566 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12111 : 119566 : tree argtype;
12112 : 119566 : int arity;
12113 : 119566 : bool maybe_raw_p = false;
12114 : :
12115 : : /* Count the number and type of arguments and check for ellipsis. */
12116 : 119566 : for (argtype = argtypes, arity = 0;
12117 : 179366 : argtype && argtype != void_list_node;
12118 : 59800 : ++arity, argtype = TREE_CHAIN (argtype))
12119 : : {
12120 : 59800 : tree t = TREE_VALUE (argtype);
12121 : :
12122 : 59800 : if (same_type_p (t, const_string_type_node))
12123 : 9 : maybe_raw_p = true;
12124 : : }
12125 : 119566 : if (!argtype)
12126 : : return false; /* Found ellipsis. */
12127 : :
12128 : 119566 : if (!maybe_raw_p || arity != 1)
12129 : 119560 : return false;
12130 : :
12131 : : return true;
12132 : : }
12133 : :
12134 : :
12135 : : /* Return true if a user-defined literal operator has one of the allowed
12136 : : argument types. */
12137 : :
12138 : : bool
12139 : 281413 : check_literal_operator_args (const_tree decl,
12140 : : bool *long_long_unsigned_p, bool *long_double_p)
12141 : : {
12142 : 281413 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12143 : :
12144 : 281413 : *long_long_unsigned_p = false;
12145 : 281413 : *long_double_p = false;
12146 : 281413 : if (processing_template_decl || processing_specialization)
12147 : 59907 : return argtypes == void_list_node;
12148 : : else
12149 : : {
12150 : : tree argtype;
12151 : : int arity;
12152 : : int max_arity = 2;
12153 : :
12154 : : /* Count the number and type of arguments and check for ellipsis. */
12155 : 221412 : for (argtype = argtypes, arity = 0;
12156 : 442918 : argtype && argtype != void_list_node;
12157 : 221412 : argtype = TREE_CHAIN (argtype))
12158 : : {
12159 : 221512 : tree t = TREE_VALUE (argtype);
12160 : 221512 : ++arity;
12161 : :
12162 : 221512 : if (TYPE_PTR_P (t))
12163 : : {
12164 : 98146 : bool maybe_raw_p = false;
12165 : 98146 : t = TREE_TYPE (t);
12166 : 98146 : if (cp_type_quals (t) != TYPE_QUAL_CONST)
12167 : : return false;
12168 : 98143 : t = TYPE_MAIN_VARIANT (t);
12169 : 98143 : if ((maybe_raw_p = same_type_p (t, char_type_node))
12170 : 74925 : || same_type_p (t, wchar_type_node)
12171 : 51859 : || same_type_p (t, char8_type_node)
12172 : 46144 : || same_type_p (t, char16_type_node)
12173 : 121215 : || same_type_p (t, char32_type_node))
12174 : : {
12175 : 98140 : argtype = TREE_CHAIN (argtype);
12176 : 98140 : if (!argtype)
12177 : : return false;
12178 : 98140 : t = TREE_VALUE (argtype);
12179 : 98140 : if (maybe_raw_p && argtype == void_list_node)
12180 : : return true;
12181 : 98067 : else if (same_type_p (t, size_type_node))
12182 : : {
12183 : 98061 : ++arity;
12184 : 98061 : continue;
12185 : : }
12186 : : else
12187 : : return false;
12188 : : }
12189 : : }
12190 : 123366 : else if (same_type_p (t, long_long_unsigned_type_node))
12191 : : {
12192 : 34106 : max_arity = 1;
12193 : 34106 : *long_long_unsigned_p = true;
12194 : : }
12195 : 89260 : else if (same_type_p (t, long_double_type_node))
12196 : : {
12197 : 89160 : max_arity = 1;
12198 : 89160 : *long_double_p = true;
12199 : : }
12200 : 100 : else if (same_type_p (t, char_type_node))
12201 : : max_arity = 1;
12202 : 56 : else if (same_type_p (t, wchar_type_node))
12203 : : max_arity = 1;
12204 : 45 : else if (same_type_p (t, char8_type_node))
12205 : : max_arity = 1;
12206 : 40 : else if (same_type_p (t, char16_type_node))
12207 : : max_arity = 1;
12208 : 29 : else if (same_type_p (t, char32_type_node))
12209 : : max_arity = 1;
12210 : : else
12211 : : return false;
12212 : : }
12213 : 221406 : if (!argtype)
12214 : : return false; /* Found ellipsis. */
12215 : :
12216 : 221403 : if (arity != max_arity)
12217 : : return false;
12218 : :
12219 : : return true;
12220 : : }
12221 : : }
12222 : :
12223 : : /* Always returns false since unlike C90, C++ has no concept of implicit
12224 : : function declarations. */
12225 : :
12226 : : bool
12227 : 359 : c_decl_implicit (const_tree)
12228 : : {
12229 : 359 : return false;
12230 : : }
|