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 : 102123224 : require_complete_type (tree value,
75 : : tsubst_flags_t complain /* = tf_warning_or_error */)
76 : : {
77 : 102123224 : tree type;
78 : :
79 : 102123224 : if (processing_template_decl || value == error_mark_node)
80 : : return value;
81 : :
82 : 94416225 : if (TREE_CODE (value) == OVERLOAD)
83 : 0 : type = unknown_type_node;
84 : : else
85 : 94416225 : type = TREE_TYPE (value);
86 : :
87 : 94416225 : if (type == error_mark_node)
88 : : return error_mark_node;
89 : :
90 : : /* First, detect a valid value with a complete type. */
91 : 94416213 : if (COMPLETE_TYPE_P (type))
92 : : return value;
93 : :
94 : 101140 : 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 : 11359311330 : complete_type (tree type)
107 : : {
108 : 11359311330 : 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 : 11359311330 : if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 : : ;
115 : 3823135708 : else if (TREE_CODE (type) == ARRAY_TYPE)
116 : : {
117 : 571996 : tree t = complete_type (TREE_TYPE (type));
118 : 571996 : unsigned int needs_constructing, has_nontrivial_dtor;
119 : 571996 : if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 : 571862 : layout_type (type);
121 : 571996 : needs_constructing
122 : 571996 : = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 : 571996 : has_nontrivial_dtor
124 : 571996 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 : 1814141 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 : : {
127 : 1242145 : TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 : 1242145 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 : : }
130 : : }
131 : 3822563712 : else if (CLASS_TYPE_P (type))
132 : : {
133 : 3747112027 : if (modules_p ())
134 : : /* TYPE could be a class member we've not loaded the definition of. */
135 : 11634148 : lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
136 : :
137 : 3747112027 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 : 717590941 : 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 : 1175693571 : complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 : : {
151 : 1175693571 : type = complete_type (type);
152 : 1175693568 : if (type == error_mark_node)
153 : : /* We already issued an error. */
154 : : return NULL_TREE;
155 : 1175693421 : else if (!COMPLETE_TYPE_P (type))
156 : : {
157 : 3797 : if (complain & tf_error)
158 : 493 : cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
159 : 3797 : note_failed_type_completion_for_satisfaction (type);
160 : 3797 : return NULL_TREE;
161 : : }
162 : : else
163 : : return type;
164 : : }
165 : :
166 : : tree
167 : 132797779 : complete_type_or_else (tree type, tree value)
168 : : {
169 : 132797779 : 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 : 3904431 : commonparms (tree p1, tree p2)
182 : : {
183 : 3904431 : tree oldargs = p1, newargs, n;
184 : 3904431 : int i, len;
185 : 3904431 : int any_change = 0;
186 : :
187 : 3904431 : len = list_length (p1);
188 : 3904431 : newargs = tree_last (p1);
189 : :
190 : 3904431 : if (newargs == void_list_node)
191 : : i = 1;
192 : : else
193 : : {
194 : 37404 : i = 0;
195 : 37404 : newargs = 0;
196 : : }
197 : :
198 : 13149307 : for (; i < len; i++)
199 : 9244876 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200 : :
201 : : n = newargs;
202 : :
203 : 17016334 : for (i = 0; p1;
204 : 13111903 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 : : {
206 : 13112089 : if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 : : {
208 : 159 : TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 : 159 : any_change = 1;
210 : : }
211 : 13111744 : else if (! TREE_PURPOSE (p1))
212 : : {
213 : 13111717 : if (TREE_PURPOSE (p2))
214 : : {
215 : 405626 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 : 405626 : any_change = 1;
217 : : }
218 : : }
219 : : else
220 : : {
221 : 27 : if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
222 : 27 : any_change = 1;
223 : 27 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 : : }
225 : 13111903 : if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 : : {
227 : 3629153 : any_change = 1;
228 : 3629153 : TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 : : }
230 : : else
231 : 9482750 : TREE_VALUE (n) = TREE_VALUE (p1);
232 : : }
233 : 3904431 : if (! any_change)
234 : 1337646 : 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 : 31240150 : original_type (tree t)
243 : : {
244 : 31240150 : int quals = cp_type_quals (t);
245 : 31240150 : while (t != error_mark_node
246 : 32309390 : && TYPE_NAME (t) != NULL_TREE)
247 : : {
248 : 10905306 : tree x = TYPE_NAME (t);
249 : 10905306 : if (TREE_CODE (x) != TYPE_DECL)
250 : : break;
251 : 10905306 : x = DECL_ORIGINAL_TYPE (x);
252 : 10905306 : if (x == NULL_TREE)
253 : : break;
254 : : t = x;
255 : : }
256 : 31240150 : 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 : 49666 : merge_type_attributes_from (tree type, tree other_type)
264 : : {
265 : 49666 : tree attrs = targetm.merge_type_attributes (type, other_type);
266 : 49666 : attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
267 : 49666 : 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 : 8995995 : cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
280 : : {
281 : 8995995 : tree mv1 = TYPE_MAIN_VARIANT (t1);
282 : 8995995 : tree mv2 = TYPE_MAIN_VARIANT (t2);
283 : 8995995 : int extended1 = 0;
284 : 8995995 : int extended2 = 0;
285 : :
286 : 8995995 : if (mv1 == mv2)
287 : : return 0;
288 : :
289 : 71956640 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
290 : : {
291 : 62962060 : if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 : 4035288 : extended1 = i + 1;
293 : 62962060 : if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 : 3396798 : extended2 = i + 1;
295 : : }
296 : 8994580 : if (mv1 == bfloat16_type_node)
297 : 936877 : extended1 = true;
298 : 8994580 : if (mv2 == bfloat16_type_node)
299 : 772480 : extended2 = true;
300 : 8994580 : if (extended2 && !extended1)
301 : : {
302 : 3990365 : int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 : 3990365 : return ret == 3 ? 3 : -ret;
304 : : }
305 : :
306 : 5004215 : const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 : 5004215 : const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 : 5004215 : 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 : 40033720 : int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
315 : 10008430 : ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
316 : 40033720 : int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
317 : 10008430 : ? 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 : 5004215 : if ((p1 > p2 && fmt1->emax >= fmt2->emax)
322 : 3775532 : || (p1 == p2 && fmt1->emax > fmt2->emax))
323 : : return 2;
324 : 3775532 : if ((p1 < p2 && fmt1->emax <= fmt2->emax)
325 : 1185708 : || (p1 == p2 && fmt1->emax < fmt2->emax))
326 : : return -2;
327 : 1185708 : if ((p1 > p2 && fmt1->emax < fmt2->emax)
328 : 1179780 : || (p1 < p2 && fmt1->emax > fmt2->emax))
329 : : return 3;
330 : 1173852 : 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 : 1173852 : 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 : 4695392 : for (p = &float_type_node; p <= &long_double_type_node; ++p)
375 : : {
376 : 3521544 : const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
377 : 3521544 : gcc_assert (fmt3->b == 2);
378 : 28172352 : int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
379 : 7043088 : ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
380 : 3521544 : if (p1 == p3 && fmt1->emax == fmt3->emax)
381 : 1133002 : ++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 : 1173848 : 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 : 90697253 : cp_common_type (tree t1, tree t2)
413 : : {
414 : 90697253 : enum tree_code code1 = TREE_CODE (t1);
415 : 90697253 : enum tree_code code2 = TREE_CODE (t2);
416 : 90697253 : tree attributes;
417 : 90697253 : 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 : 90697253 : attributes = (*targetm.merge_type_attributes) (t1, t2);
424 : :
425 : 90697253 : if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
426 : : {
427 : 861662 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
428 : 861662 : return build_type_attribute_variant (t1, attributes);
429 : : else
430 : : return NULL_TREE;
431 : : }
432 : :
433 : : /* FIXME: Attributes. */
434 : 89835591 : gcc_assert (ARITHMETIC_TYPE_P (t1)
435 : : || VECTOR_TYPE_P (t1)
436 : : || UNSCOPED_ENUM_P (t1));
437 : 89835591 : 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 : 89835591 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
445 : : {
446 : 223777 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
447 : 223777 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
448 : 223777 : tree subtype
449 : 223777 : = type_after_usual_arithmetic_conversions (subtype1, subtype2);
450 : :
451 : 223777 : if (subtype == error_mark_node)
452 : : return subtype;
453 : 223777 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
454 : 190433 : return build_type_attribute_variant (t1, attributes);
455 : 33344 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
456 : 33284 : return build_type_attribute_variant (t2, attributes);
457 : : else
458 : 60 : return build_type_attribute_variant (build_complex_type (subtype),
459 : 60 : attributes);
460 : : }
461 : :
462 : 89611814 : 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 : 49666 : if (TYPE_UNSIGNED (t1))
467 : 10913 : return merge_type_attributes_from (t1, t2);
468 : : else
469 : 38753 : return merge_type_attributes_from (t2, t1);
470 : : }
471 : :
472 : : /* If only one is real, use it as the result. */
473 : 89562148 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
474 : 1071016 : return build_type_attribute_variant (t1, attributes);
475 : 88491132 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
476 : 1006307 : return build_type_attribute_variant (t2, attributes);
477 : :
478 : 87484825 : if (code1 == REAL_TYPE
479 : 87484825 : && (extended_float_type_p (t1) || extended_float_type_p (t2)))
480 : : {
481 : 171761 : tree mv1 = TYPE_MAIN_VARIANT (t1);
482 : 171761 : tree mv2 = TYPE_MAIN_VARIANT (t2);
483 : 171761 : if (mv1 == mv2)
484 : 168511 : return build_type_attribute_variant (t1, attributes);
485 : :
486 : 3250 : int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
487 : 3250 : if (cmpret == 3)
488 : 0 : return error_mark_node;
489 : 3250 : else if (cmpret >= 0)
490 : 2988 : 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 : 87313064 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
497 : 12330992 : return build_type_attribute_variant (t1, attributes);
498 : 74982072 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
499 : 1456727 : return build_type_attribute_variant (t2, attributes);
500 : :
501 : : /* The types are the same; no need to do anything fancy. */
502 : 73525345 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
503 : 67734486 : return build_type_attribute_variant (t1, attributes);
504 : :
505 : 5790859 : if (code1 != REAL_TYPE)
506 : : {
507 : : /* If one is unsigned long long, then convert the other to unsigned
508 : : long long. */
509 : 5790856 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
510 : 5790856 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
511 : 128861 : return build_type_attribute_variant (long_long_unsigned_type_node,
512 : 128861 : 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 : 5661995 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
524 : 5661995 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
525 : : {
526 : 603 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
527 : 603 : ? long_long_unsigned_type_node
528 : 305 : : long_long_integer_type_node);
529 : 305 : return build_type_attribute_variant (t, attributes);
530 : : }
531 : :
532 : : /* Go through the same procedure, but for longs. */
533 : 5661690 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
534 : 5661690 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
535 : 731177 : return build_type_attribute_variant (long_unsigned_type_node,
536 : 731177 : attributes);
537 : 4930513 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
538 : 4930513 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
539 : : {
540 : 71237 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
541 : 71237 : ? long_unsigned_type_node : long_integer_type_node);
542 : 35948 : 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 : 9789088 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
550 : : {
551 : 4894565 : if (int_n_enabled_p [i]
552 : 4894565 : && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
553 : 4816910 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
554 : 4816898 : || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
555 : 4816898 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
556 : : {
557 : 72 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
558 : 72 : ? int_n_trees[i].unsigned_type
559 : 42 : : int_n_trees[i].signed_type);
560 : 42 : return build_type_attribute_variant (t, attributes);
561 : : }
562 : : }
563 : :
564 : : /* Otherwise prefer the unsigned one. */
565 : 4894523 : if (TYPE_UNSIGNED (t1))
566 : 3902724 : return build_type_attribute_variant (t1, attributes);
567 : : else
568 : 991799 : 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 : 907609 : type_after_usual_arithmetic_conversions (tree t1, tree t2)
600 : : {
601 : 907609 : gcc_assert (ARITHMETIC_TYPE_P (t1)
602 : : || VECTOR_TYPE_P (t1)
603 : : || UNSCOPED_ENUM_P (t1));
604 : 907609 : 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 : 907609 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
610 : 683304 : && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
611 : : {
612 : 682990 : t1 = type_promotes_to (t1);
613 : 682990 : t2 = type_promotes_to (t2);
614 : : }
615 : :
616 : 907609 : return cp_common_type (t1, t2);
617 : : }
618 : :
619 : : static void
620 : 42 : composite_pointer_error (const op_location_t &location,
621 : : diagnostic_t 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 : 3373305 : 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 : 3373305 : tree pointee1;
660 : 3373305 : tree pointee2;
661 : 3373305 : tree result_type;
662 : 3373305 : tree attributes;
663 : :
664 : : /* Determine the types pointed to by T1 and T2. */
665 : 3373305 : if (TYPE_PTR_P (t1))
666 : : {
667 : 3372836 : pointee1 = TREE_TYPE (t1);
668 : 3372836 : pointee2 = TREE_TYPE (t2);
669 : : }
670 : : else
671 : : {
672 : 469 : pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
673 : 469 : 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 : 3373305 : 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, DK_PERMERROR,
694 : : t1, t2, operation);
695 : : else
696 : 85 : return error_mark_node;
697 : 30 : result_type = void_type_node;
698 : : }
699 : 3373214 : const int q1 = cp_type_quals (pointee1);
700 : 3373214 : const int q2 = cp_type_quals (pointee2);
701 : 3373214 : const int quals = q1 | q2;
702 : 3373214 : result_type = cp_build_qualified_type (result_type,
703 : 3373214 : (quals | (*add_const
704 : 3373214 : ? 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 : 3373214 : if (quals != q1 || quals != q2)
710 : 524914 : *add_const = true;
711 : : /* If the original types were pointers to members, so is the
712 : : result. */
713 : 3373214 : if (TYPE_PTRMEM_P (t1))
714 : : {
715 : 459 : 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, DK_PERMERROR,
720 : : t1, t2, operation);
721 : : else
722 : 0 : return error_mark_node;
723 : : }
724 : 459 : result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
725 : : result_type);
726 : : }
727 : : else
728 : 3372755 : result_type = build_pointer_type (result_type);
729 : :
730 : : /* Merge the attributes. */
731 : 3373214 : attributes = (*targetm.merge_type_attributes) (t1, t2);
732 : 3373214 : 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 : 3561803 : 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 : 3561803 : tree class1;
750 : 3561803 : 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 : 3561803 : if (null_ptr_cst_p (arg1))
757 : : return t2;
758 : 3561624 : 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 : 3538078 : 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 : 3538078 : if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
776 : : {
777 : 164641 : tree attributes;
778 : 164641 : tree result_type;
779 : :
780 : 164641 : 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 : 164640 : result_type
810 : 164640 : = cp_build_qualified_type (void_type_node,
811 : 164640 : (cp_type_quals (TREE_TYPE (t1))
812 : 164640 : | cp_type_quals (TREE_TYPE (t2))));
813 : 164640 : result_type = build_pointer_type (result_type);
814 : : /* Merge the attributes. */
815 : 164640 : attributes = (*targetm.merge_type_attributes) (t1, t2);
816 : 164640 : return build_type_attribute_variant (result_type, attributes);
817 : : }
818 : :
819 : 3373437 : 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 : 3373437 : if (fnptr_conv_p (t1, t2))
830 : : return t1;
831 : 3373369 : 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 : 3372821 : if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
837 : 3372821 : && CLASS_TYPE_P (TREE_TYPE (t1))
838 : 557724 : && CLASS_TYPE_P (TREE_TYPE (t2))
839 : 3931026 : && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
840 : 557721 : TREE_TYPE (t2)))
841 : : {
842 : 30368 : class1 = TREE_TYPE (t1);
843 : 30368 : class2 = TREE_TYPE (t2);
844 : :
845 : 30368 : if (DERIVED_FROM_P (class1, class2))
846 : 3052 : t2 = (build_pointer_type
847 : 1526 : (cp_build_qualified_type (class1, cp_type_quals (class2))));
848 : 28842 : else if (DERIVED_FROM_P (class2, class1))
849 : 57648 : t1 = (build_pointer_type
850 : 28824 : (cp_build_qualified_type (class2, cp_type_quals (class1))));
851 : : else
852 : : {
853 : 18 : if (complain & tf_error)
854 : 12 : composite_pointer_error (location, DK_ERROR, t1, t2, operation);
855 : 18 : return error_mark_node;
856 : : }
857 : : }
858 : : /* [expr.eq] permits the application of a pointer-to-member
859 : : conversion to change the class type of one of the types. */
860 : 3342551 : else if (TYPE_PTRMEM_P (t1)
861 : 3343035 : && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
862 : : TYPE_PTRMEM_CLASS_TYPE (t2)))
863 : : {
864 : 46 : class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
865 : 46 : class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
866 : :
867 : 46 : if (DERIVED_FROM_P (class1, class2))
868 : 21 : t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
869 : 25 : else if (DERIVED_FROM_P (class2, class1))
870 : 10 : t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
871 : : else
872 : : {
873 : 15 : if (complain & tf_error)
874 : 3 : switch (operation)
875 : : {
876 : 3 : case CPO_COMPARISON:
877 : 3 : error_at (location, "comparison between distinct "
878 : : "pointer-to-member types %qT and %qT lacks a cast",
879 : : t1, t2);
880 : 3 : break;
881 : 0 : case CPO_CONVERSION:
882 : 0 : error_at (location, "conversion between distinct "
883 : : "pointer-to-member types %qT and %qT lacks a cast",
884 : : t1, t2);
885 : 0 : break;
886 : 0 : case CPO_CONDITIONAL_EXPR:
887 : 0 : error_at (location, "conditional expression between distinct "
888 : : "pointer-to-member types %qT and %qT lacks a cast",
889 : : t1, t2);
890 : 0 : break;
891 : 0 : default:
892 : 0 : gcc_unreachable ();
893 : : }
894 : 15 : return error_mark_node;
895 : : }
896 : : }
897 : :
898 : 3373272 : bool add_const = false;
899 : 3373272 : return composite_pointer_type_r (location, t1, t2, &add_const, operation,
900 : 3373272 : complain);
901 : : }
902 : :
903 : : /* Return the merged type of two types.
904 : : We assume that comptypes has already been done and returned 1;
905 : : if that isn't so, this may crash.
906 : :
907 : : This just combines attributes and default arguments; any other
908 : : differences would cause the two types to compare unalike. */
909 : :
910 : : tree
911 : 27598600 : merge_types (tree t1, tree t2)
912 : : {
913 : 27598600 : enum tree_code code1;
914 : 27598600 : enum tree_code code2;
915 : 27598600 : tree attributes;
916 : :
917 : : /* Save time if the two types are the same. */
918 : 27598600 : if (t1 == t2)
919 : : return t1;
920 : 15620075 : if (original_type (t1) == original_type (t2))
921 : : return t1;
922 : :
923 : : /* If one type is nonsense, use the other. */
924 : 15560821 : if (t1 == error_mark_node)
925 : : return t2;
926 : 15560821 : if (t2 == error_mark_node)
927 : : return t1;
928 : :
929 : : /* Handle merging an auto redeclaration with a previous deduced
930 : : return type. */
931 : 15560821 : if (is_auto (t1))
932 : : return t2;
933 : :
934 : : /* Merge the attributes. */
935 : 15551339 : attributes = (*targetm.merge_type_attributes) (t1, t2);
936 : :
937 : 15551339 : if (TYPE_PTRMEMFUNC_P (t1))
938 : 15 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
939 : 15551339 : if (TYPE_PTRMEMFUNC_P (t2))
940 : 15 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
941 : :
942 : 15551339 : code1 = TREE_CODE (t1);
943 : 15551339 : code2 = TREE_CODE (t2);
944 : 15551339 : if (code1 != code2)
945 : : {
946 : 0 : gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
947 : 0 : if (code1 == TYPENAME_TYPE)
948 : : {
949 : 0 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
950 : 0 : code1 = TREE_CODE (t1);
951 : : }
952 : : else
953 : : {
954 : 0 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
955 : 0 : code2 = TREE_CODE (t2);
956 : : }
957 : : }
958 : :
959 : 15551339 : switch (code1)
960 : : {
961 : 2643271 : case POINTER_TYPE:
962 : 2643271 : case REFERENCE_TYPE:
963 : : /* For two pointers, do this recursively on the target type. */
964 : 2643271 : {
965 : 2643271 : tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
966 : 2643271 : int quals = cp_type_quals (t1);
967 : :
968 : 2643271 : if (code1 == POINTER_TYPE)
969 : : {
970 : 697208 : t1 = build_pointer_type (target);
971 : 697208 : if (TREE_CODE (target) == METHOD_TYPE)
972 : 15 : t1 = build_ptrmemfunc_type (t1);
973 : : }
974 : : else
975 : 1946063 : t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
976 : 2643271 : t1 = build_type_attribute_variant (t1, attributes);
977 : 2643271 : t1 = cp_build_qualified_type (t1, quals);
978 : :
979 : 2643271 : return t1;
980 : : }
981 : :
982 : 12 : case OFFSET_TYPE:
983 : 12 : {
984 : 12 : int quals;
985 : 12 : tree pointee;
986 : 12 : quals = cp_type_quals (t1);
987 : 12 : pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
988 : 12 : TYPE_PTRMEM_POINTED_TO_TYPE (t2));
989 : 12 : t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
990 : : pointee);
991 : 12 : t1 = cp_build_qualified_type (t1, quals);
992 : 12 : break;
993 : : }
994 : :
995 : 10477 : case ARRAY_TYPE:
996 : 10477 : {
997 : 10477 : tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
998 : : /* Save space: see if the result is identical to one of the args. */
999 : 20954 : if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
1000 : 10455 : return build_type_attribute_variant (t1, attributes);
1001 : 38 : if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
1002 : 16 : return build_type_attribute_variant (t2, attributes);
1003 : : /* Merge the element types, and have a size if either arg has one. */
1004 : 6 : t1 = build_cplus_array_type
1005 : 6 : (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
1006 : 6 : break;
1007 : : }
1008 : :
1009 : 4045781 : case FUNCTION_TYPE:
1010 : : /* Function types: prefer the one that specified arg types.
1011 : : If both do, merge the arg types. Also merge the return types. */
1012 : 4045781 : {
1013 : 4045781 : tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1014 : 4045781 : tree p1 = TYPE_ARG_TYPES (t1);
1015 : 4045781 : tree p2 = TYPE_ARG_TYPES (t2);
1016 : 4045781 : tree parms;
1017 : :
1018 : : /* Save space: see if the result is identical to one of the args. */
1019 : 4045781 : if (valtype == TREE_TYPE (t1) && ! p2)
1020 : 0 : return cp_build_type_attribute_variant (t1, attributes);
1021 : 4045781 : if (valtype == TREE_TYPE (t2) && ! p1)
1022 : 0 : return cp_build_type_attribute_variant (t2, attributes);
1023 : :
1024 : : /* Simple way if one arg fails to specify argument types. */
1025 : 8091562 : if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1026 : : parms = p2;
1027 : 7808862 : else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1028 : : parms = p1;
1029 : : else
1030 : 3904431 : parms = commonparms (p1, p2);
1031 : :
1032 : 4045781 : cp_cv_quals quals = type_memfn_quals (t1);
1033 : 4045781 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1034 : 4045781 : gcc_assert (quals == type_memfn_quals (t2));
1035 : 4045781 : gcc_assert (rqual == type_memfn_rqual (t2));
1036 : :
1037 : 4045781 : tree rval = build_function_type (valtype, parms);
1038 : 4045781 : rval = apply_memfn_quals (rval, quals);
1039 : 4045781 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1040 : 4045781 : TYPE_RAISES_EXCEPTIONS (t2));
1041 : 4045781 : bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1042 : 4045781 : t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1043 : 4045781 : break;
1044 : : }
1045 : :
1046 : 3818657 : case METHOD_TYPE:
1047 : 3818657 : {
1048 : : /* Get this value the long way, since TYPE_METHOD_BASETYPE
1049 : : is just the main variant of this. */
1050 : 3818657 : tree basetype = class_of_this_parm (t2);
1051 : 3818657 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1052 : 3818657 : TYPE_RAISES_EXCEPTIONS (t2));
1053 : 3818657 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1054 : 3818657 : tree t3;
1055 : 3818657 : bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1056 : :
1057 : : /* If this was a member function type, get back to the
1058 : : original type of type member function (i.e., without
1059 : : the class instance variable up front. */
1060 : 3818657 : t1 = build_function_type (TREE_TYPE (t1),
1061 : 3818657 : TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1062 : 3818657 : t2 = build_function_type (TREE_TYPE (t2),
1063 : 3818657 : TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1064 : 3818657 : t3 = merge_types (t1, t2);
1065 : 3818657 : t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1066 : 3818657 : TYPE_ARG_TYPES (t3));
1067 : 3818657 : t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1068 : 3818657 : break;
1069 : : }
1070 : :
1071 : : case TYPENAME_TYPE:
1072 : : /* There is no need to merge attributes into a TYPENAME_TYPE.
1073 : : When the type is instantiated it will have whatever
1074 : : attributes result from the instantiation. */
1075 : : return t1;
1076 : :
1077 : 5023698 : default:;
1078 : 5023698 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1079 : : return t1;
1080 : 12 : else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1081 : : return t2;
1082 : : break;
1083 : : }
1084 : :
1085 : 7864456 : return cp_build_type_attribute_variant (t1, attributes);
1086 : : }
1087 : :
1088 : : /* Return the ARRAY_TYPE type without its domain. */
1089 : :
1090 : : tree
1091 : 236 : strip_array_domain (tree type)
1092 : : {
1093 : 236 : tree t2;
1094 : 236 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1095 : 236 : if (TYPE_DOMAIN (type) == NULL_TREE)
1096 : : return type;
1097 : 209 : t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
1098 : 209 : return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
1099 : : }
1100 : :
1101 : : /* Wrapper around cp_common_type that is used by c-common.cc and other
1102 : : front end optimizations that remove promotions.
1103 : :
1104 : : Return the common type for two arithmetic types T1 and T2 under the
1105 : : usual arithmetic conversions. The default conversions have already
1106 : : been applied, and enumerated types converted to their compatible
1107 : : integer types. */
1108 : :
1109 : : tree
1110 : 391977 : common_type (tree t1, tree t2)
1111 : : {
1112 : : /* If one type is nonsense, use the other */
1113 : 391977 : if (t1 == error_mark_node)
1114 : : return t2;
1115 : 391977 : if (t2 == error_mark_node)
1116 : : return t1;
1117 : :
1118 : 391977 : return cp_common_type (t1, t2);
1119 : : }
1120 : :
1121 : : /* Return the common type of two pointer types T1 and T2. This is the
1122 : : type for the result of most arithmetic operations if the operands
1123 : : have the given two types.
1124 : :
1125 : : We assume that comp_target_types has already been done and returned
1126 : : nonzero; if that isn't so, this may crash. */
1127 : :
1128 : : tree
1129 : 1194710 : common_pointer_type (tree t1, tree t2)
1130 : : {
1131 : 1194710 : gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
1132 : : || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
1133 : : || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
1134 : :
1135 : 1194710 : return composite_pointer_type (input_location, t1, t2,
1136 : : error_mark_node, error_mark_node,
1137 : 1194710 : CPO_CONVERSION, tf_warning_or_error);
1138 : : }
1139 : :
1140 : : /* Compare two exception specifier types for exactness or subsetness, if
1141 : : allowed. Returns false for mismatch, true for match (same, or
1142 : : derived and !exact).
1143 : :
1144 : : [except.spec] "If a class X ... objects of class X or any class publicly
1145 : : and unambiguously derived from X. Similarly, if a pointer type Y * ...
1146 : : exceptions of type Y * or that are pointers to any type publicly and
1147 : : unambiguously derived from Y. Otherwise a function only allows exceptions
1148 : : that have the same type ..."
1149 : : This does not mention cv qualifiers and is different to what throw
1150 : : [except.throw] and catch [except.catch] will do. They will ignore the
1151 : : top level cv qualifiers, and allow qualifiers in the pointer to class
1152 : : example.
1153 : :
1154 : : We implement the letter of the standard. */
1155 : :
1156 : : static bool
1157 : 1658 : comp_except_types (tree a, tree b, bool exact)
1158 : : {
1159 : 1658 : if (same_type_p (a, b))
1160 : : return true;
1161 : 508 : else if (!exact)
1162 : : {
1163 : 12 : if (cp_type_quals (a) || cp_type_quals (b))
1164 : 0 : return false;
1165 : :
1166 : 12 : if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1167 : : {
1168 : 7 : a = TREE_TYPE (a);
1169 : 7 : b = TREE_TYPE (b);
1170 : 7 : if (cp_type_quals (a) || cp_type_quals (b))
1171 : 1 : return false;
1172 : : }
1173 : :
1174 : 11 : if (TREE_CODE (a) != RECORD_TYPE
1175 : 11 : || TREE_CODE (b) != RECORD_TYPE)
1176 : : return false;
1177 : :
1178 : 10 : if (publicly_uniquely_derived_p (a, b))
1179 : : return true;
1180 : : }
1181 : : return false;
1182 : : }
1183 : :
1184 : : /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1185 : : If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1186 : : If EXACT is ce_type, the C++17 type compatibility rules apply.
1187 : : If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1188 : : If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1189 : : are unordered, but we've already filtered out duplicates. Most lists will
1190 : : be in order, we should try to make use of that. */
1191 : :
1192 : : bool
1193 : 1327894850 : comp_except_specs (const_tree t1, const_tree t2, int exact)
1194 : : {
1195 : 1327894850 : const_tree probe;
1196 : 1327894850 : const_tree base;
1197 : 1327894850 : int length = 0;
1198 : :
1199 : 1327894850 : if (t1 == t2)
1200 : : return true;
1201 : :
1202 : : /* First handle noexcept. */
1203 : 521443282 : if (exact < ce_exact)
1204 : : {
1205 : 7215834 : if (exact == ce_type
1206 : 14362698 : && (canonical_eh_spec (CONST_CAST_TREE (t1))
1207 : 7146864 : == canonical_eh_spec (CONST_CAST_TREE (t2))))
1208 : : return true;
1209 : :
1210 : : /* noexcept(false) is compatible with no exception-specification,
1211 : : and less strict than any spec. */
1212 : 7209480 : if (t1 == noexcept_false_spec)
1213 : 104 : return t2 == NULL_TREE || exact == ce_derived;
1214 : : /* Even a derived noexcept(false) is compatible with no
1215 : : exception-specification. */
1216 : 7209376 : if (t2 == noexcept_false_spec)
1217 : 1313 : return t1 == NULL_TREE;
1218 : :
1219 : : /* Otherwise, if we aren't looking for an exact match, noexcept is
1220 : : equivalent to throw(). */
1221 : 7208063 : if (t1 == noexcept_true_spec)
1222 : 638701 : t1 = empty_except_spec;
1223 : 7208063 : if (t2 == noexcept_true_spec)
1224 : 6303331 : t2 = empty_except_spec;
1225 : : }
1226 : :
1227 : : /* If any noexcept is left, it is only comparable to itself;
1228 : : either we're looking for an exact match or we're redeclaring a
1229 : : template with dependent noexcept. */
1230 : 500154084 : if ((t1 && TREE_PURPOSE (t1))
1231 : 545371620 : || (t2 && TREE_PURPOSE (t2)))
1232 : 512322088 : return (t1 && t2
1233 : 512322088 : && (exact == ce_exact
1234 : 57584240 : ? TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
1235 : 263132 : : cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))));
1236 : :
1237 : 9113423 : if (t1 == NULL_TREE) /* T1 is ... */
1238 : 6518691 : return t2 == NULL_TREE || exact == ce_derived;
1239 : 2594732 : if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1240 : 2562140 : return t2 != NULL_TREE && !TREE_VALUE (t2);
1241 : 33892 : if (t2 == NULL_TREE) /* T2 is ... */
1242 : : return false;
1243 : 1536 : if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1244 : 76 : return exact == ce_derived;
1245 : :
1246 : : /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1247 : : Count how many we find, to determine exactness. For exact matching and
1248 : : ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1249 : : O(nm). */
1250 : 2613 : for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1251 : : {
1252 : 2056 : for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1253 : : {
1254 : 1658 : tree a = TREE_VALUE (probe);
1255 : 1658 : tree b = TREE_VALUE (t2);
1256 : :
1257 : 1658 : if (comp_except_types (a, b, exact))
1258 : : {
1259 : 1153 : if (probe == base && exact > ce_derived)
1260 : 1118 : base = TREE_CHAIN (probe);
1261 : 1153 : length++;
1262 : 1153 : break;
1263 : : }
1264 : : }
1265 : 1153 : if (probe == NULL_TREE)
1266 : : return false;
1267 : : }
1268 : 1062 : return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1269 : : }
1270 : :
1271 : : /* Compare the array types T1 and T2. CB says how we should behave when
1272 : : comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1273 : : bounds_either says than any array can be [], bounds_first means that
1274 : : onlt T1 can be an array with unknown bounds. STRICT is true if
1275 : : qualifiers must match when comparing the types of the array elements. */
1276 : :
1277 : : static bool
1278 : 1164262 : comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1279 : : bool strict)
1280 : : {
1281 : 1164262 : tree d1;
1282 : 1164262 : tree d2;
1283 : 1164262 : tree max1, max2;
1284 : :
1285 : 1164262 : if (t1 == t2)
1286 : : return true;
1287 : :
1288 : : /* The type of the array elements must be the same. */
1289 : 1164194 : if (strict
1290 : 1166482 : ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1291 : 2288 : : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1292 : : return false;
1293 : :
1294 : 769129 : d1 = TYPE_DOMAIN (t1);
1295 : 769129 : d2 = TYPE_DOMAIN (t2);
1296 : :
1297 : 769129 : if (d1 == d2)
1298 : : return true;
1299 : :
1300 : : /* If one of the arrays is dimensionless, and the other has a
1301 : : dimension, they are of different types. However, it is valid to
1302 : : write:
1303 : :
1304 : : extern int a[];
1305 : : int a[3];
1306 : :
1307 : : by [basic.link]:
1308 : :
1309 : : declarations for an array object can specify
1310 : : array types that differ by the presence or absence of a major
1311 : : array bound (_dcl.array_). */
1312 : 567978 : if (!d1 && d2)
1313 : 1932 : return cb >= bounds_either;
1314 : 566046 : else if (d1 && !d2)
1315 : 4481 : return cb == bounds_either;
1316 : :
1317 : : /* Check that the dimensions are the same. */
1318 : :
1319 : 561565 : if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1320 : : return false;
1321 : 561544 : max1 = TYPE_MAX_VALUE (d1);
1322 : 561544 : max2 = TYPE_MAX_VALUE (d2);
1323 : :
1324 : 561544 : if (!cp_tree_equal (max1, max2))
1325 : : return false;
1326 : :
1327 : : return true;
1328 : : }
1329 : :
1330 : : /* Compare the relative position of T1 and T2 into their respective
1331 : : template parameter list.
1332 : : T1 and T2 must be template parameter types.
1333 : : Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1334 : :
1335 : : static bool
1336 : 1323180081 : comp_template_parms_position (tree t1, tree t2)
1337 : : {
1338 : 1323180081 : tree index1, index2;
1339 : 1323180081 : gcc_assert (t1 && t2
1340 : : && TREE_CODE (t1) == TREE_CODE (t2)
1341 : : && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1342 : : || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1343 : : || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1344 : :
1345 : 1323180081 : index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1346 : 1323180081 : index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1347 : :
1348 : : /* Then compare their relative position. */
1349 : 1323180081 : if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1350 : 848738388 : || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1351 : 1920507702 : || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1352 : 597327621 : != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1353 : : return false;
1354 : :
1355 : : /* In C++14 we can end up comparing 'auto' to a normal template
1356 : : parameter. Don't confuse them. */
1357 : 525893761 : if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1358 : 95211569 : return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1359 : :
1360 : : return true;
1361 : : }
1362 : :
1363 : : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1364 : :
1365 : : static bool
1366 : 511 : cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1367 : : {
1368 : 511 : t1 = TYPE_MAIN_VARIANT (t1);
1369 : 511 : t2 = TYPE_MAIN_VARIANT (t2);
1370 : :
1371 : 511 : if (TYPE_PTR_P (t1)
1372 : 141 : && TYPE_PTR_P (t2))
1373 : : return true;
1374 : :
1375 : : /* The signedness of the parameter matters only when an integral
1376 : : type smaller than int is promoted to int, otherwise only the
1377 : : precision of the parameter matters.
1378 : : This check should make sure that the callee does not see
1379 : : undefined values in argument registers. */
1380 : 376 : if (INTEGRAL_TYPE_P (t1)
1381 : 67 : && INTEGRAL_TYPE_P (t2)
1382 : 46 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1383 : 419 : && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1384 : 0 : || !targetm.calls.promote_prototypes (NULL_TREE)
1385 : 0 : || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1386 : 43 : return true;
1387 : :
1388 : 333 : return same_type_p (t1, t2);
1389 : : }
1390 : :
1391 : : /* Check if a type cast between two function types can be considered safe. */
1392 : :
1393 : : static bool
1394 : 5668 : cxx_safe_function_type_cast_p (tree t1, tree t2)
1395 : : {
1396 : 5668 : if (TREE_TYPE (t1) == void_type_node &&
1397 : 5606 : TYPE_ARG_TYPES (t1) == void_list_node)
1398 : : return true;
1399 : :
1400 : 4858 : if (TREE_TYPE (t2) == void_type_node &&
1401 : 4653 : TYPE_ARG_TYPES (t2) == void_list_node)
1402 : : return true;
1403 : :
1404 : 272 : if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1405 : : return false;
1406 : :
1407 : 111 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1408 : 314 : t1 && t2;
1409 : 203 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1410 : 239 : if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1411 : : return false;
1412 : :
1413 : : return true;
1414 : : }
1415 : :
1416 : : /* Subroutine in comptypes. */
1417 : :
1418 : : static bool
1419 : 10289064048 : structural_comptypes (tree t1, tree t2, int strict)
1420 : : {
1421 : : /* Both should be types that are not obviously the same. */
1422 : 10289064048 : gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1423 : :
1424 : : /* Suppress typename resolution under spec_hasher::equal in place of calling
1425 : : push_to_top_level there. */
1426 : 10289064048 : if (!comparing_specializations)
1427 : : {
1428 : : /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1429 : : current instantiation. */
1430 : 8404906267 : if (TREE_CODE (t1) == TYPENAME_TYPE)
1431 : 75614730 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1432 : :
1433 : 8404906267 : if (TREE_CODE (t2) == TYPENAME_TYPE)
1434 : 93246890 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1435 : : }
1436 : :
1437 : 10289064048 : if (TYPE_PTRMEMFUNC_P (t1))
1438 : 30042725 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1439 : 10289064048 : if (TYPE_PTRMEMFUNC_P (t2))
1440 : 33294440 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1441 : :
1442 : : /* Different classes of types can't be compatible. */
1443 : 10289064048 : if (TREE_CODE (t1) != TREE_CODE (t2))
1444 : : return false;
1445 : :
1446 : : /* Qualifiers must match. For array types, we will check when we
1447 : : recur on the array element types. */
1448 : 6629496879 : if (TREE_CODE (t1) != ARRAY_TYPE
1449 : 6629496879 : && cp_type_quals (t1) != cp_type_quals (t2))
1450 : : return false;
1451 : 6235625554 : if (TREE_CODE (t1) == FUNCTION_TYPE
1452 : 6235625554 : && type_memfn_quals (t1) != type_memfn_quals (t2))
1453 : : return false;
1454 : : /* Need to check this before TYPE_MAIN_VARIANT.
1455 : : FIXME function qualifiers should really change the main variant. */
1456 : 6235555270 : if (FUNC_OR_METHOD_TYPE_P (t1))
1457 : : {
1458 : 36099119 : if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1459 : : return false;
1460 : 18238285 : if (flag_noexcept_type
1461 : 36377181 : && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1462 : 18138896 : TYPE_RAISES_EXCEPTIONS (t2),
1463 : : ce_type))
1464 : : return false;
1465 : : }
1466 : :
1467 : : /* Allow for two different type nodes which have essentially the same
1468 : : definition. Note that we already checked for equality of the type
1469 : : qualifiers (just above). */
1470 : 6210716351 : if (TREE_CODE (t1) != ARRAY_TYPE
1471 : 6210716351 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1472 : 300712035 : goto check_alias;
1473 : :
1474 : : /* Compare the types. Return false on known not-same. Break on not
1475 : : known. Never return true from this switch -- you'll break
1476 : : specialization comparison. */
1477 : 5910004316 : switch (TREE_CODE (t1))
1478 : : {
1479 : : case VOID_TYPE:
1480 : : case BOOLEAN_TYPE:
1481 : : /* All void and bool types are the same. */
1482 : : break;
1483 : :
1484 : 444900942 : case OPAQUE_TYPE:
1485 : 444900942 : case INTEGER_TYPE:
1486 : 444900942 : case FIXED_POINT_TYPE:
1487 : 444900942 : case REAL_TYPE:
1488 : : /* With these nodes, we can't determine type equivalence by
1489 : : looking at what is stored in the nodes themselves, because
1490 : : two nodes might have different TYPE_MAIN_VARIANTs but still
1491 : : represent the same type. For example, wchar_t and int could
1492 : : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1493 : : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1494 : : and are distinct types. On the other hand, int and the
1495 : : following typedef
1496 : :
1497 : : typedef int INT __attribute((may_alias));
1498 : :
1499 : : have identical properties, different TYPE_MAIN_VARIANTs, but
1500 : : represent the same type. The canonical type system keeps
1501 : : track of equivalence in this case, so we fall back on it. */
1502 : 444900942 : if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1503 : : return false;
1504 : :
1505 : : /* We don't need or want the attribute comparison. */
1506 : 8330 : goto check_alias;
1507 : :
1508 : 1076694 : case TEMPLATE_TEMPLATE_PARM:
1509 : 1076694 : case BOUND_TEMPLATE_TEMPLATE_PARM:
1510 : 1076694 : if (!comp_template_parms_position (t1, t2))
1511 : : return false;
1512 : 814642 : if (!comp_template_parms
1513 : 1629284 : (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1514 : 2296389 : DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1515 : : return false;
1516 : 579884 : if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1517 : : break;
1518 : : /* Don't check inheritance. */
1519 : : strict = COMPARE_STRICT;
1520 : : /* Fall through. */
1521 : :
1522 : 2849562745 : case RECORD_TYPE:
1523 : 2849562745 : case UNION_TYPE:
1524 : 5014844915 : if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1525 : 1765313554 : && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1526 : 1313275790 : || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1527 : 3301642164 : && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1528 : : break;
1529 : :
1530 : 2820825657 : if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1531 : : break;
1532 : 2820825624 : else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1533 : : break;
1534 : :
1535 : : return false;
1536 : :
1537 : 45528 : case OFFSET_TYPE:
1538 : 45528 : if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1539 : : strict & ~COMPARE_REDECLARATION))
1540 : : return false;
1541 : 41205 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1542 : : return false;
1543 : : break;
1544 : :
1545 : 621657715 : case REFERENCE_TYPE:
1546 : 621657715 : if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1547 : : return false;
1548 : : /* fall through to checks for pointer types */
1549 : 919154668 : gcc_fallthrough ();
1550 : :
1551 : 919154668 : case POINTER_TYPE:
1552 : 919154668 : if (TYPE_MODE (t1) != TYPE_MODE (t2)
1553 : 919154668 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1554 : 823250866 : return false;
1555 : : break;
1556 : :
1557 : 10962526 : case METHOD_TYPE:
1558 : 10962526 : case FUNCTION_TYPE:
1559 : : /* Exception specs and memfn_rquals were checked above. */
1560 : 10962526 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1561 : : return false;
1562 : 10351100 : if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1563 : : return false;
1564 : : break;
1565 : :
1566 : 1161906 : case ARRAY_TYPE:
1567 : : /* Target types must match incl. qualifiers. */
1568 : 1161906 : if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1569 : 1161906 : ? bounds_either : bounds_none),
1570 : : /*strict=*/true))
1571 : : return false;
1572 : : break;
1573 : :
1574 : 1322103387 : case TEMPLATE_TYPE_PARM:
1575 : : /* If T1 and T2 don't have the same relative position in their
1576 : : template parameters set, they can't be equal. */
1577 : 1322103387 : if (!comp_template_parms_position (t1, t2))
1578 : : return false;
1579 : : /* If T1 and T2 don't represent the same class template deduction,
1580 : : they aren't equal. */
1581 : 924789548 : if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1582 : 462394774 : CLASS_PLACEHOLDER_TEMPLATE (t2)))
1583 : : return false;
1584 : : /* Constrained 'auto's are distinct from parms that don't have the same
1585 : : constraints. */
1586 : 451085929 : if (!equivalent_placeholder_constraints (t1, t2))
1587 : : return false;
1588 : : break;
1589 : :
1590 : 112490987 : case TYPENAME_TYPE:
1591 : 224981974 : if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1592 : 112490987 : TYPENAME_TYPE_FULLNAME (t2)))
1593 : : return false;
1594 : : /* Qualifiers don't matter on scopes. */
1595 : 97613424 : if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1596 : 97613424 : TYPE_CONTEXT (t2)))
1597 : : return false;
1598 : : break;
1599 : :
1600 : 6903 : case UNBOUND_CLASS_TEMPLATE:
1601 : 6903 : if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1602 : : return false;
1603 : 6903 : if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1604 : : return false;
1605 : : break;
1606 : :
1607 : 2942235 : case COMPLEX_TYPE:
1608 : 2942235 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1609 : : return false;
1610 : : break;
1611 : :
1612 : 11899590 : case VECTOR_TYPE:
1613 : 11899590 : if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1614 : 22355637 : || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1615 : 22402721 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1616 : 1443543 : return false;
1617 : : break;
1618 : :
1619 : 6251216 : case TYPE_PACK_EXPANSION:
1620 : 6251216 : return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1621 : : PACK_EXPANSION_PATTERN (t2))
1622 : 12348417 : && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1623 : 6097201 : PACK_EXPANSION_EXTRA_ARGS (t2)));
1624 : :
1625 : 14 : case PACK_INDEX_TYPE:
1626 : 14 : return (cp_tree_equal (PACK_INDEX_PACK (t1),
1627 : 14 : PACK_INDEX_PACK (t2))
1628 : 16 : && cp_tree_equal (PACK_INDEX_INDEX (t1),
1629 : 2 : PACK_INDEX_INDEX (t2)));
1630 : :
1631 : 12087603 : case DECLTYPE_TYPE:
1632 : 24175206 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1633 : 12087603 : != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1634 : : return false;
1635 : 12083797 : if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1636 : : return false;
1637 : 12083797 : if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1638 : : return false;
1639 : 12083797 : if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1640 : : return false;
1641 : : break;
1642 : :
1643 : 509567 : case TRAIT_TYPE:
1644 : 509567 : if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1645 : : return false;
1646 : 1330 : if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1647 : 2660 : || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1648 : 0 : return false;
1649 : : break;
1650 : :
1651 : 3 : case TYPEOF_TYPE:
1652 : 3 : if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1653 : : return false;
1654 : : break;
1655 : :
1656 : : default:
1657 : : return false;
1658 : : }
1659 : :
1660 : : /* If we get here, we know that from a target independent POV the
1661 : : types are the same. Make sure the target attributes are also
1662 : : the same. */
1663 : 596071450 : if (!comp_type_attributes (t1, t2))
1664 : : return false;
1665 : :
1666 : 596070306 : check_alias:
1667 : 896790671 : if (comparing_dependent_aliases
1668 : 896790671 : && (typedef_variant_p (t1) || typedef_variant_p (t2)))
1669 : : {
1670 : 2596358 : tree dep1 = dependent_opaque_alias_p (t1) ? t1 : NULL_TREE;
1671 : 2596358 : tree dep2 = dependent_opaque_alias_p (t2) ? t2 : NULL_TREE;
1672 : 2596358 : if ((dep1 || dep2)
1673 : 2596358 : && (!(dep1 && dep2)
1674 : 0 : || !comp_type_attributes (DECL_ATTRIBUTES (TYPE_NAME (dep1)),
1675 : 0 : DECL_ATTRIBUTES (TYPE_NAME (dep2)))))
1676 : 22 : return false;
1677 : :
1678 : : /* Don't treat an alias template specialization with dependent
1679 : : arguments as equivalent to its underlying type when used as a
1680 : : template argument; we need them to be distinct so that we
1681 : : substitute into the specialization arguments at instantiation
1682 : : time. And aliases can't be equivalent without being ==, so
1683 : : we don't need to look any deeper. */
1684 : 2596336 : ++processing_template_decl;
1685 : 2596336 : dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1686 : 2596336 : dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1687 : 2596336 : --processing_template_decl;
1688 : 2596336 : if ((dep1 || dep2) && dep1 != dep2)
1689 : : return false;
1690 : : }
1691 : :
1692 : : return true;
1693 : : }
1694 : :
1695 : : /* C++ implementation of compatible_types_for_indirection_note_p. */
1696 : :
1697 : : bool
1698 : 1679 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1699 : : {
1700 : 1679 : return same_type_p (type1, type2);
1701 : : }
1702 : :
1703 : : /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1704 : : is a bitwise-or of the COMPARE_* flags. */
1705 : :
1706 : : bool
1707 : 16554517473 : comptypes (tree t1, tree t2, int strict)
1708 : : {
1709 : 16554517473 : gcc_checking_assert (t1 && t2);
1710 : :
1711 : : /* TYPE_ARGUMENT_PACKS are not really types. */
1712 : 16554517473 : gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1713 : : && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1714 : :
1715 : 16554517473 : if (t1 == t2)
1716 : : return true;
1717 : :
1718 : : /* Suppress errors caused by previously reported errors. */
1719 : 10289065217 : if (t1 == error_mark_node || t2 == error_mark_node)
1720 : : return false;
1721 : :
1722 : 10289065060 : if (strict == COMPARE_STRICT)
1723 : : {
1724 : 9173139610 : if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1725 : : /* At least one of the types requires structural equality, so
1726 : : perform a deep check. */
1727 : 805722961 : return structural_comptypes (t1, t2, strict);
1728 : :
1729 : 8367416649 : if (flag_checking && param_use_canonical_types)
1730 : : {
1731 : 8367415637 : bool result = structural_comptypes (t1, t2, strict);
1732 : :
1733 : 8367415637 : if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1734 : : /* The two types are structurally equivalent, but their
1735 : : canonical types were different. This is a failure of the
1736 : : canonical type propagation code.*/
1737 : 0 : internal_error
1738 : 0 : ("canonical types differ for identical types %qT and %qT",
1739 : : t1, t2);
1740 : 8367415637 : else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1741 : : /* Two types are structurally different, but the canonical
1742 : : types are the same. This means we were over-eager in
1743 : : assigning canonical types. */
1744 : 0 : internal_error
1745 : 0 : ("same canonical type node for different types %qT and %qT",
1746 : : t1, t2);
1747 : :
1748 : : return result;
1749 : : }
1750 : 1012 : if (!flag_checking && param_use_canonical_types)
1751 : 1012 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1752 : : else
1753 : 0 : return structural_comptypes (t1, t2, strict);
1754 : : }
1755 : 1115925450 : else if (strict == COMPARE_STRUCTURAL)
1756 : 1082147149 : return structural_comptypes (t1, t2, COMPARE_STRICT);
1757 : : else
1758 : 33778301 : return structural_comptypes (t1, t2, strict);
1759 : : }
1760 : :
1761 : : /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1762 : : top-level qualifiers. */
1763 : :
1764 : : bool
1765 : 2594357591 : same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1766 : : {
1767 : 2594357591 : if (type1 == error_mark_node || type2 == error_mark_node)
1768 : : return false;
1769 : 2594357581 : if (type1 == type2)
1770 : : return true;
1771 : :
1772 : 1440621852 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1773 : 1440621852 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1774 : 1440621852 : return same_type_p (type1, type2);
1775 : : }
1776 : :
1777 : : /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1778 : :
1779 : : bool
1780 : 201232320 : similar_type_p (tree type1, tree type2)
1781 : : {
1782 : 201232320 : if (type1 == error_mark_node || type2 == error_mark_node)
1783 : : return false;
1784 : :
1785 : : /* Informally, two types are similar if, ignoring top-level cv-qualification:
1786 : : * they are the same type; or
1787 : : * they are both pointers, and the pointed-to types are similar; or
1788 : : * they are both pointers to member of the same class, and the types of
1789 : : the pointed-to members are similar; or
1790 : : * they are both arrays of the same size or both arrays of unknown bound,
1791 : : and the array element types are similar. */
1792 : :
1793 : 201232320 : if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1794 : : return true;
1795 : :
1796 : 95457390 : if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1797 : 95404322 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1798 : 95404322 : || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1799 : 53560 : return comp_ptr_ttypes_const (type1, type2, bounds_either);
1800 : :
1801 : : return false;
1802 : : }
1803 : :
1804 : : /* Helper function for layout_compatible_type_p and
1805 : : is_corresponding_member_aggr. Advance to next members (NULL if
1806 : : no further ones) and return true if those members are still part of
1807 : : the common initial sequence. */
1808 : :
1809 : : bool
1810 : 2112 : next_common_initial_sequence (tree &memb1, tree &memb2)
1811 : : {
1812 : 2587 : while (memb1)
1813 : : {
1814 : 2623 : if (TREE_CODE (memb1) != FIELD_DECL
1815 : 2206 : || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1816 : : {
1817 : 417 : memb1 = DECL_CHAIN (memb1);
1818 : 417 : continue;
1819 : : }
1820 : 1789 : if (DECL_FIELD_IS_BASE (memb1))
1821 : : {
1822 : 58 : memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1823 : 58 : continue;
1824 : : }
1825 : : break;
1826 : : }
1827 : 2557 : while (memb2)
1828 : : {
1829 : 2593 : if (TREE_CODE (memb2) != FIELD_DECL
1830 : 2176 : || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1831 : : {
1832 : 417 : memb2 = DECL_CHAIN (memb2);
1833 : 417 : continue;
1834 : : }
1835 : 1759 : if (DECL_FIELD_IS_BASE (memb2))
1836 : : {
1837 : 28 : memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1838 : 28 : continue;
1839 : : }
1840 : : break;
1841 : : }
1842 : 2112 : if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1843 : : return true;
1844 : 1731 : if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1845 : : return false;
1846 : 1731 : if (DECL_BIT_FIELD_TYPE (memb1))
1847 : : {
1848 : 150 : if (!DECL_BIT_FIELD_TYPE (memb2))
1849 : : return false;
1850 : 42 : if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1851 : 42 : DECL_BIT_FIELD_TYPE (memb2)))
1852 : : return false;
1853 : 42 : if (TYPE_PRECISION (TREE_TYPE (memb1))
1854 : 42 : != TYPE_PRECISION (TREE_TYPE (memb2)))
1855 : : return false;
1856 : : }
1857 : 1581 : else if (DECL_BIT_FIELD_TYPE (memb2))
1858 : : return false;
1859 : 1551 : else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1860 : : return false;
1861 : 1524 : if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1862 : 1524 : != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1863 : : return false;
1864 : 1494 : if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1865 : : return false;
1866 : 1461 : if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1867 : : return false;
1868 : : return true;
1869 : : }
1870 : :
1871 : : /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1872 : :
1873 : : bool
1874 : 2709 : layout_compatible_type_p (tree type1, tree type2)
1875 : : {
1876 : 2709 : if (type1 == error_mark_node || type2 == error_mark_node)
1877 : : return false;
1878 : 2709 : if (type1 == type2)
1879 : : return true;
1880 : 1318 : if (TREE_CODE (type1) != TREE_CODE (type2))
1881 : : return false;
1882 : :
1883 : 1280 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1884 : 1280 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1885 : :
1886 : 1280 : if (TREE_CODE (type1) == ENUMERAL_TYPE)
1887 : 32 : return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1888 : 32 : && same_type_p (finish_underlying_type (type1),
1889 : : finish_underlying_type (type2)));
1890 : :
1891 : 391 : if (CLASS_TYPE_P (type1)
1892 : 391 : && std_layout_type_p (type1)
1893 : 381 : && std_layout_type_p (type2)
1894 : 1629 : && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1895 : : {
1896 : 336 : tree field1 = TYPE_FIELDS (type1);
1897 : 336 : tree field2 = TYPE_FIELDS (type2);
1898 : 336 : if (TREE_CODE (type1) == RECORD_TYPE)
1899 : : {
1900 : 1011 : while (1)
1901 : : {
1902 : 636 : if (!next_common_initial_sequence (field1, field2))
1903 : : return false;
1904 : 630 : if (field1 == NULL_TREE)
1905 : : return true;
1906 : 375 : field1 = DECL_CHAIN (field1);
1907 : 375 : field2 = DECL_CHAIN (field2);
1908 : : }
1909 : : }
1910 : : /* Otherwise both types must be union types.
1911 : : The standard says:
1912 : : "Two standard-layout unions are layout-compatible if they have
1913 : : the same number of non-static data members and corresponding
1914 : : non-static data members (in any order) have layout-compatible
1915 : : types."
1916 : : but the code anticipates that bitfield vs. non-bitfield,
1917 : : different bitfield widths or presence/absence of
1918 : : [[no_unique_address]] should be checked as well. */
1919 : 75 : auto_vec<tree, 16> vec;
1920 : 75 : unsigned int count = 0;
1921 : 339 : for (; field1; field1 = DECL_CHAIN (field1))
1922 : 264 : if (TREE_CODE (field1) == FIELD_DECL)
1923 : 174 : count++;
1924 : 339 : for (; field2; field2 = DECL_CHAIN (field2))
1925 : 264 : if (TREE_CODE (field2) == FIELD_DECL)
1926 : 174 : vec.safe_push (field2);
1927 : : /* Discussions on core lean towards treating multiple union fields
1928 : : of the same type as the same field, so this might need changing
1929 : : in the future. */
1930 : 150 : if (count != vec.length ())
1931 : : return false;
1932 : 321 : for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1933 : : {
1934 : 252 : if (TREE_CODE (field1) != FIELD_DECL)
1935 : 84 : continue;
1936 : 168 : unsigned int j;
1937 : 168 : tree t1 = DECL_BIT_FIELD_TYPE (field1);
1938 : 168 : if (t1 == NULL_TREE)
1939 : 165 : t1 = TREE_TYPE (field1);
1940 : 276 : FOR_EACH_VEC_ELT (vec, j, field2)
1941 : : {
1942 : 270 : tree t2 = DECL_BIT_FIELD_TYPE (field2);
1943 : 270 : if (t2 == NULL_TREE)
1944 : 258 : t2 = TREE_TYPE (field2);
1945 : 270 : if (DECL_BIT_FIELD_TYPE (field1))
1946 : : {
1947 : 6 : if (!DECL_BIT_FIELD_TYPE (field2))
1948 : 0 : continue;
1949 : 6 : if (TYPE_PRECISION (TREE_TYPE (field1))
1950 : 6 : != TYPE_PRECISION (TREE_TYPE (field2)))
1951 : 6 : continue;
1952 : : }
1953 : 264 : else if (DECL_BIT_FIELD_TYPE (field2))
1954 : 6 : continue;
1955 : 258 : if (!layout_compatible_type_p (t1, t2))
1956 : 96 : continue;
1957 : 162 : if ((!lookup_attribute ("no_unique_address",
1958 : 162 : DECL_ATTRIBUTES (field1)))
1959 : 162 : != !lookup_attribute ("no_unique_address",
1960 : 162 : DECL_ATTRIBUTES (field2)))
1961 : 0 : continue;
1962 : : break;
1963 : : }
1964 : 336 : if (j == vec.length ())
1965 : : return false;
1966 : 162 : vec.unordered_remove (j);
1967 : : }
1968 : : return true;
1969 : 75 : }
1970 : :
1971 : 912 : return same_type_p (type1, type2);
1972 : : }
1973 : :
1974 : : /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1975 : :
1976 : : bool
1977 : 141765675 : at_least_as_qualified_p (const_tree type1, const_tree type2)
1978 : : {
1979 : 141765675 : int q1 = cp_type_quals (type1);
1980 : 141765675 : int q2 = cp_type_quals (type2);
1981 : :
1982 : : /* All qualifiers for TYPE2 must also appear in TYPE1. */
1983 : 141765675 : return (q1 & q2) == q2;
1984 : : }
1985 : :
1986 : : /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1987 : : more cv-qualified that TYPE1, and 0 otherwise. */
1988 : :
1989 : : int
1990 : 9277244 : comp_cv_qualification (int q1, int q2)
1991 : : {
1992 : 9277244 : if (q1 == q2)
1993 : : return 0;
1994 : :
1995 : 2794905 : if ((q1 & q2) == q2)
1996 : : return 1;
1997 : 226026 : else if ((q1 & q2) == q1)
1998 : 225437 : return -1;
1999 : :
2000 : : return 0;
2001 : : }
2002 : :
2003 : : int
2004 : 0 : comp_cv_qualification (const_tree type1, const_tree type2)
2005 : : {
2006 : 0 : int q1 = cp_type_quals (type1);
2007 : 0 : int q2 = cp_type_quals (type2);
2008 : 0 : return comp_cv_qualification (q1, q2);
2009 : : }
2010 : :
2011 : : /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
2012 : : subset of the cv-qualification signature of TYPE2, and the types
2013 : : are similar. Returns -1 if the other way 'round, and 0 otherwise. */
2014 : :
2015 : : int
2016 : 311 : comp_cv_qual_signature (tree type1, tree type2)
2017 : : {
2018 : 311 : if (comp_ptr_ttypes_real (type2, type1, -1))
2019 : : return 1;
2020 : 261 : else if (comp_ptr_ttypes_real (type1, type2, -1))
2021 : : return -1;
2022 : : else
2023 : 249 : return 0;
2024 : : }
2025 : :
2026 : : /* Subroutines of `comptypes'. */
2027 : :
2028 : : /* Return true if two parameter type lists PARMS1 and PARMS2 are
2029 : : equivalent in the sense that functions with those parameter types
2030 : : can have equivalent types. The two lists must be equivalent,
2031 : : element by element. */
2032 : :
2033 : : bool
2034 : 688935609 : compparms (const_tree parms1, const_tree parms2)
2035 : : {
2036 : 688935609 : const_tree t1, t2;
2037 : :
2038 : : /* An unspecified parmlist matches any specified parmlist
2039 : : whose argument types don't need default promotions. */
2040 : :
2041 : 688935609 : for (t1 = parms1, t2 = parms2;
2042 : 1144138930 : t1 || t2;
2043 : 455203321 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2044 : : {
2045 : : /* If one parmlist is shorter than the other,
2046 : : they fail to match. */
2047 : 1081600743 : if (!t1 || !t2)
2048 : : return false;
2049 : 1080921920 : if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2050 : : return false;
2051 : : }
2052 : : return true;
2053 : : }
2054 : :
2055 : :
2056 : : /* Process a sizeof or alignof expression where the operand is a type.
2057 : : STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2058 : : or GNU (preferred alignment) semantics; it is ignored if OP is
2059 : : SIZEOF_EXPR. */
2060 : :
2061 : : tree
2062 : 22943968 : cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2063 : : bool std_alignof, bool complain)
2064 : : {
2065 : 22943968 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2066 : 22943968 : if (type == error_mark_node)
2067 : : return error_mark_node;
2068 : :
2069 : 22943966 : type = non_reference (type);
2070 : 22943966 : if (TREE_CODE (type) == METHOD_TYPE)
2071 : : {
2072 : 0 : if (complain)
2073 : : {
2074 : 0 : pedwarn (loc, OPT_Wpointer_arith,
2075 : : "invalid application of %qs to a member function",
2076 : 0 : OVL_OP_INFO (false, op)->name);
2077 : 0 : return size_one_node;
2078 : : }
2079 : : else
2080 : 0 : return error_mark_node;
2081 : : }
2082 : 22943966 : else if (VOID_TYPE_P (type) && std_alignof)
2083 : : {
2084 : 14 : if (complain)
2085 : 14 : error_at (loc, "invalid application of %qs to a void type",
2086 : 14 : OVL_OP_INFO (false, op)->name);
2087 : 14 : return error_mark_node;
2088 : : }
2089 : :
2090 : 22943952 : bool dependent_p = dependent_type_p (type);
2091 : 22943952 : if (!dependent_p)
2092 : 19337198 : complete_type (type);
2093 : 19337198 : if (dependent_p
2094 : : /* VLA types will have a non-constant size. In the body of an
2095 : : uninstantiated template, we don't need to try to compute the
2096 : : value, because the sizeof expression is not an integral
2097 : : constant expression in that case. And, if we do try to
2098 : : compute the value, we'll likely end up with SAVE_EXPRs, which
2099 : : the template substitution machinery does not expect to see. */
2100 : 19337198 : || (processing_template_decl
2101 : 1645222 : && COMPLETE_TYPE_P (type)
2102 : 1645216 : && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2103 : : {
2104 : 3606754 : tree value = build_min (op, size_type_node, type);
2105 : 3606754 : TREE_READONLY (value) = 1;
2106 : 3606754 : if (op == ALIGNOF_EXPR && std_alignof)
2107 : 432662 : ALIGNOF_EXPR_STD_P (value) = true;
2108 : 3606754 : SET_EXPR_LOCATION (value, loc);
2109 : 3606754 : return value;
2110 : : }
2111 : :
2112 : 19337198 : return c_sizeof_or_alignof_type (loc, complete_type (type),
2113 : : op == SIZEOF_EXPR, std_alignof,
2114 : 19337198 : complain);
2115 : : }
2116 : :
2117 : : /* Return the size of the type, without producing any warnings for
2118 : : types whose size cannot be taken. This routine should be used only
2119 : : in some other routine that has already produced a diagnostic about
2120 : : using the size of such a type. */
2121 : : tree
2122 : 2036515 : cxx_sizeof_nowarn (tree type)
2123 : : {
2124 : 2036515 : if (TREE_CODE (type) == FUNCTION_TYPE
2125 : 2036515 : || VOID_TYPE_P (type)
2126 : 2036431 : || TREE_CODE (type) == ERROR_MARK)
2127 : 84 : return size_one_node;
2128 : 2036431 : else if (!COMPLETE_TYPE_P (type))
2129 : 23 : return size_zero_node;
2130 : : else
2131 : 2036408 : return cxx_sizeof_or_alignof_type (input_location, type,
2132 : 2036408 : SIZEOF_EXPR, false, false);
2133 : : }
2134 : :
2135 : : /* Process a sizeof expression where the operand is an expression. */
2136 : :
2137 : : static tree
2138 : 1324611 : cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2139 : : {
2140 : 1324611 : if (e == error_mark_node)
2141 : : return error_mark_node;
2142 : :
2143 : 1323884 : if (instantiation_dependent_uneval_expression_p (e))
2144 : : {
2145 : 289323 : e = build_min (SIZEOF_EXPR, size_type_node, e);
2146 : 289323 : TREE_SIDE_EFFECTS (e) = 0;
2147 : 289323 : TREE_READONLY (e) = 1;
2148 : 289323 : SET_EXPR_LOCATION (e, loc);
2149 : :
2150 : 289323 : return e;
2151 : : }
2152 : :
2153 : 1034561 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2154 : 1034561 : STRIP_ANY_LOCATION_WRAPPER (e);
2155 : :
2156 : 1034561 : if (TREE_CODE (e) == PARM_DECL
2157 : 41689 : && DECL_ARRAY_PARAMETER_P (e)
2158 : 1034942 : && (complain & tf_warning))
2159 : : {
2160 : 120 : auto_diagnostic_group d;
2161 : 120 : if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2162 : : "%<sizeof%> on array function parameter %qE "
2163 : 120 : "will return size of %qT", e, TREE_TYPE (e)))
2164 : 24 : inform (DECL_SOURCE_LOCATION (e), "declared here");
2165 : 120 : }
2166 : :
2167 : 1034561 : e = mark_type_use (e);
2168 : :
2169 : 1034561 : if (bitfield_p (e))
2170 : : {
2171 : 12 : if (complain & tf_error)
2172 : 6 : error_at (e_loc,
2173 : : "invalid application of %<sizeof%> to a bit-field");
2174 : : else
2175 : 6 : return error_mark_node;
2176 : 6 : e = char_type_node;
2177 : : }
2178 : 1034549 : else if (is_overloaded_fn (e))
2179 : : {
2180 : 33 : if (complain & tf_error)
2181 : 12 : permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2182 : : "an expression of function type");
2183 : : else
2184 : 21 : return error_mark_node;
2185 : 12 : e = char_type_node;
2186 : : }
2187 : 1034516 : else if (type_unknown_p (e))
2188 : : {
2189 : 0 : if (complain & tf_error)
2190 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2191 : : else
2192 : 0 : return error_mark_node;
2193 : 0 : e = char_type_node;
2194 : : }
2195 : : else
2196 : 1034516 : e = TREE_TYPE (e);
2197 : :
2198 : 1034534 : return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2199 : 1034534 : complain & tf_error);
2200 : : }
2201 : :
2202 : : /* Implement the __alignof keyword: Return the minimum required
2203 : : alignment of E, measured in bytes. For VAR_DECL's and
2204 : : FIELD_DECL's return DECL_ALIGN (which can be set from an
2205 : : "aligned" __attribute__ specification). STD_ALIGNOF acts
2206 : : like in cxx_sizeof_or_alignof_type. */
2207 : :
2208 : : static tree
2209 : 101015 : cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2210 : : tsubst_flags_t complain)
2211 : : {
2212 : 101015 : tree t;
2213 : :
2214 : 101015 : if (e == error_mark_node)
2215 : : return error_mark_node;
2216 : :
2217 : 101007 : if (processing_template_decl)
2218 : : {
2219 : 19304 : e = build_min (ALIGNOF_EXPR, size_type_node, e);
2220 : 19304 : TREE_SIDE_EFFECTS (e) = 0;
2221 : 19304 : TREE_READONLY (e) = 1;
2222 : 19304 : SET_EXPR_LOCATION (e, loc);
2223 : 19304 : ALIGNOF_EXPR_STD_P (e) = std_alignof;
2224 : :
2225 : 19304 : return e;
2226 : : }
2227 : :
2228 : 81703 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2229 : 81703 : STRIP_ANY_LOCATION_WRAPPER (e);
2230 : :
2231 : 81703 : e = mark_type_use (e);
2232 : :
2233 : 81703 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2234 : : !(complain & tf_error)))
2235 : : {
2236 : 0 : if (!(complain & tf_error))
2237 : 0 : return error_mark_node;
2238 : 0 : t = size_one_node;
2239 : : }
2240 : 81703 : else if (VAR_P (e))
2241 : 12270 : t = size_int (DECL_ALIGN_UNIT (e));
2242 : 69433 : else if (bitfield_p (e))
2243 : : {
2244 : 6 : if (complain & tf_error)
2245 : 6 : error_at (e_loc,
2246 : : "invalid application of %<__alignof%> to a bit-field");
2247 : : else
2248 : 0 : return error_mark_node;
2249 : 6 : t = size_one_node;
2250 : : }
2251 : 69427 : else if (TREE_CODE (e) == COMPONENT_REF
2252 : 69427 : && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2253 : 37792 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2254 : 31635 : else if (is_overloaded_fn (e))
2255 : : {
2256 : 6 : if (complain & tf_error)
2257 : 6 : permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2258 : : "an expression of function type");
2259 : : else
2260 : 0 : return error_mark_node;
2261 : 6 : if (TREE_CODE (e) == FUNCTION_DECL)
2262 : 3 : t = size_int (DECL_ALIGN_UNIT (e));
2263 : : else
2264 : 3 : t = size_one_node;
2265 : : }
2266 : 31629 : else if (type_unknown_p (e))
2267 : : {
2268 : 0 : if (complain & tf_error)
2269 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2270 : : else
2271 : 0 : return error_mark_node;
2272 : 0 : t = size_one_node;
2273 : : }
2274 : : else
2275 : 31629 : return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2276 : : ALIGNOF_EXPR, std_alignof,
2277 : 31629 : complain & tf_error);
2278 : :
2279 : 50074 : return fold_convert_loc (loc, size_type_node, t);
2280 : : }
2281 : :
2282 : : /* Process a sizeof or alignof expression E with code OP where the operand
2283 : : is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2284 : :
2285 : : tree
2286 : 1425626 : cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2287 : : bool std_alignof, bool complain)
2288 : : {
2289 : 1425626 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2290 : 1425626 : if (op == SIZEOF_EXPR)
2291 : 2012230 : return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2292 : : else
2293 : 101042 : return cxx_alignof_expr (loc, e, std_alignof,
2294 : 101015 : complain? tf_warning_or_error : tf_none);
2295 : : }
2296 : :
2297 : : /* Build a representation of an expression 'alignas(E).' Return the
2298 : : folded integer value of E if it is an integral constant expression
2299 : : that resolves to a valid alignment. If E depends on a template
2300 : : parameter, return a syntactic representation tree of kind
2301 : : ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2302 : : expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2303 : :
2304 : : tree
2305 : 190446 : cxx_alignas_expr (tree e)
2306 : : {
2307 : 190446 : if (e == NULL_TREE || e == error_mark_node
2308 : 380892 : || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2309 : 0 : return e;
2310 : :
2311 : 190446 : if (TYPE_P (e))
2312 : : /* [dcl.align]/3:
2313 : :
2314 : : When the alignment-specifier is of the form
2315 : : alignas(type-id), it shall have the same effect as
2316 : : alignas(alignof(type-id)). */
2317 : :
2318 : 117542 : return cxx_sizeof_or_alignof_type (input_location,
2319 : : e, ALIGNOF_EXPR,
2320 : : /*std_alignof=*/true,
2321 : 117542 : /*complain=*/true);
2322 : :
2323 : : /* If we reach this point, it means the alignas expression if of
2324 : : the form "alignas(assignment-expression)", so we should follow
2325 : : what is stated by [dcl.align]/2. */
2326 : :
2327 : 72904 : if (value_dependent_expression_p (e))
2328 : : /* Leave value-dependent expression alone for now. */
2329 : : return e;
2330 : :
2331 : 17810 : e = instantiate_non_dependent_expr (e);
2332 : 17810 : e = mark_rvalue_use (e);
2333 : :
2334 : : /* [dcl.align]/2 says:
2335 : :
2336 : : the assignment-expression shall be an integral constant
2337 : : expression. */
2338 : :
2339 : 17810 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2340 : : {
2341 : 18 : error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2342 : 18 : return error_mark_node;
2343 : : }
2344 : :
2345 : 17792 : return cxx_constant_value (e);
2346 : : }
2347 : :
2348 : :
2349 : : /* EXPR is being used in a context that is not a function call.
2350 : : Enforce:
2351 : :
2352 : : [expr.ref]
2353 : :
2354 : : The expression can be used only as the left-hand operand of a
2355 : : member function call.
2356 : :
2357 : : [expr.mptr.operator]
2358 : :
2359 : : If the result of .* or ->* is a function, then that result can be
2360 : : used only as the operand for the function call operator ().
2361 : :
2362 : : by issuing an error message if appropriate. Returns true iff EXPR
2363 : : violates these rules. */
2364 : :
2365 : : bool
2366 : 1093437242 : invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2367 : : {
2368 : 1093437242 : if (expr == NULL_TREE)
2369 : : return false;
2370 : : /* Don't enforce this in MS mode. */
2371 : 1093435820 : if (flag_ms_extensions)
2372 : : return false;
2373 : 1093428095 : if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2374 : 85519316 : expr = get_first_fn (expr);
2375 : 1093428095 : if (TREE_TYPE (expr)
2376 : 1093428095 : && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2377 : : {
2378 : 91 : if (complain & tf_error)
2379 : : {
2380 : 87 : if (DECL_P (expr))
2381 : : {
2382 : 69 : auto_diagnostic_group d;
2383 : 69 : error_at (loc, "invalid use of non-static member function %qD",
2384 : : expr);
2385 : 69 : inform (DECL_SOURCE_LOCATION (expr), "declared here");
2386 : 69 : }
2387 : : else
2388 : 18 : error_at (loc, "invalid use of non-static member function of "
2389 : 18 : "type %qT", TREE_TYPE (expr));
2390 : : }
2391 : 91 : return true;
2392 : : }
2393 : : return false;
2394 : : }
2395 : :
2396 : : /* If EXP is a reference to a bit-field, and the type of EXP does not
2397 : : match the declared type of the bit-field, return the declared type
2398 : : of the bit-field. Otherwise, return NULL_TREE. */
2399 : :
2400 : : tree
2401 : 2600688653 : is_bitfield_expr_with_lowered_type (const_tree exp)
2402 : : {
2403 : 3610912799 : switch (TREE_CODE (exp))
2404 : : {
2405 : 4701986 : case COND_EXPR:
2406 : 9403972 : if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2407 : 4701960 : ? TREE_OPERAND (exp, 1)
2408 : 26 : : TREE_OPERAND (exp, 0)))
2409 : : return NULL_TREE;
2410 : 135 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2411 : :
2412 : 602696 : case COMPOUND_EXPR:
2413 : 602696 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2414 : :
2415 : 336544464 : case MODIFY_EXPR:
2416 : 336544464 : case SAVE_EXPR:
2417 : 336544464 : case UNARY_PLUS_EXPR:
2418 : 336544464 : case PREDECREMENT_EXPR:
2419 : 336544464 : case PREINCREMENT_EXPR:
2420 : 336544464 : case POSTDECREMENT_EXPR:
2421 : 336544464 : case POSTINCREMENT_EXPR:
2422 : 336544464 : case NEGATE_EXPR:
2423 : 336544464 : case NON_LVALUE_EXPR:
2424 : 336544464 : case BIT_NOT_EXPR:
2425 : 336544464 : case CLEANUP_POINT_EXPR:
2426 : 336544464 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2427 : :
2428 : 229113301 : case COMPONENT_REF:
2429 : 229113301 : {
2430 : 229113301 : tree field;
2431 : :
2432 : 229113301 : field = TREE_OPERAND (exp, 1);
2433 : 229113301 : if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2434 : : return NULL_TREE;
2435 : 40510688 : if (same_type_ignoring_top_level_qualifiers_p
2436 : 40510688 : (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2437 : : return NULL_TREE;
2438 : 40359779 : return DECL_BIT_FIELD_TYPE (field);
2439 : : }
2440 : :
2441 : 489699832 : case VAR_DECL:
2442 : 489699832 : if (DECL_HAS_VALUE_EXPR_P (exp))
2443 : 1276463 : return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2444 : 1276463 : (CONST_CAST_TREE (exp)));
2445 : : return NULL_TREE;
2446 : :
2447 : 678742651 : case VIEW_CONVERT_EXPR:
2448 : 678742651 : if (location_wrapper_p (exp))
2449 : 671800388 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2450 : : else
2451 : : return NULL_TREE;
2452 : :
2453 : : default:
2454 : : return NULL_TREE;
2455 : : }
2456 : : }
2457 : :
2458 : : /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2459 : : bitfield with a lowered type, the type of EXP is returned, rather
2460 : : than NULL_TREE. */
2461 : :
2462 : : tree
2463 : 1230914825 : unlowered_expr_type (const_tree exp)
2464 : : {
2465 : 1230914825 : tree type;
2466 : 1230914825 : tree etype = TREE_TYPE (exp);
2467 : :
2468 : 1230914825 : type = is_bitfield_expr_with_lowered_type (exp);
2469 : 1230914825 : if (type)
2470 : 35022509 : type = cp_build_qualified_type (type, cp_type_quals (etype));
2471 : : else
2472 : : type = etype;
2473 : :
2474 : 1230914825 : return type;
2475 : : }
2476 : :
2477 : : /* Perform the conversions in [expr] that apply when an lvalue appears
2478 : : in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2479 : : function-to-pointer conversions. In addition, bitfield references are
2480 : : converted to their declared types. Note that this function does not perform
2481 : : the lvalue-to-rvalue conversion for class types. If you need that conversion
2482 : : for class types, then you probably need to use force_rvalue.
2483 : :
2484 : : Although the returned value is being used as an rvalue, this
2485 : : function does not wrap the returned expression in a
2486 : : NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2487 : : that the return value is no longer an lvalue. */
2488 : :
2489 : : tree
2490 : 879800218 : decay_conversion (tree exp,
2491 : : tsubst_flags_t complain,
2492 : : bool reject_builtin /* = true */)
2493 : : {
2494 : 879800218 : tree type;
2495 : 879800218 : enum tree_code code;
2496 : 879800218 : location_t loc = cp_expr_loc_or_input_loc (exp);
2497 : :
2498 : 879800218 : type = TREE_TYPE (exp);
2499 : 879800218 : if (type == error_mark_node)
2500 : : return error_mark_node;
2501 : :
2502 : 879799999 : exp = resolve_nondeduced_context_or_error (exp, complain);
2503 : :
2504 : 879799999 : code = TREE_CODE (type);
2505 : :
2506 : 879799999 : if (error_operand_p (exp))
2507 : 90 : return error_mark_node;
2508 : :
2509 : 879799909 : if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2510 : : {
2511 : 1415458 : mark_rvalue_use (exp, loc, reject_builtin);
2512 : 1415458 : return nullptr_node;
2513 : : }
2514 : :
2515 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2516 : : Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2517 : 878384451 : if (code == VOID_TYPE)
2518 : : {
2519 : 5778 : if (complain & tf_error)
2520 : 3 : error_at (loc, "void value not ignored as it ought to be");
2521 : 5778 : return error_mark_node;
2522 : : }
2523 : 878378673 : if (invalid_nonstatic_memfn_p (loc, exp, complain))
2524 : 6 : return error_mark_node;
2525 : 878378667 : if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2526 : : {
2527 : 86566602 : exp = mark_lvalue_use (exp);
2528 : 86566602 : if (reject_builtin && reject_gcc_builtin (exp, loc))
2529 : 110 : return error_mark_node;
2530 : 86566492 : return cp_build_addr_expr (exp, complain);
2531 : : }
2532 : 791812065 : if (code == ARRAY_TYPE)
2533 : : {
2534 : 4761677 : tree adr;
2535 : 4761677 : tree ptrtype;
2536 : :
2537 : 4761677 : exp = mark_lvalue_use (exp);
2538 : :
2539 : 4761677 : if (INDIRECT_REF_P (exp))
2540 : 83288 : return build_nop (build_pointer_type (TREE_TYPE (type)),
2541 : 166576 : TREE_OPERAND (exp, 0));
2542 : :
2543 : 4678389 : if (TREE_CODE (exp) == COMPOUND_EXPR)
2544 : : {
2545 : 3 : tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2546 : 3 : if (op1 == error_mark_node)
2547 : : return error_mark_node;
2548 : 3 : return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2549 : 6 : TREE_OPERAND (exp, 0), op1);
2550 : : }
2551 : :
2552 : 4678386 : if (!obvalue_p (exp)
2553 : 4678386 : && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2554 : : {
2555 : 0 : if (complain & tf_error)
2556 : 0 : error_at (loc, "invalid use of non-lvalue array");
2557 : 0 : return error_mark_node;
2558 : : }
2559 : :
2560 : 4678386 : ptrtype = build_pointer_type (TREE_TYPE (type));
2561 : :
2562 : 4678386 : if (VAR_P (exp))
2563 : : {
2564 : 484603 : if (!cxx_mark_addressable (exp))
2565 : 0 : return error_mark_node;
2566 : 484603 : adr = build_nop (ptrtype, build_address (exp));
2567 : 484603 : return adr;
2568 : : }
2569 : : /* This way is better for a COMPONENT_REF since it can
2570 : : simplify the offset for a component. */
2571 : 4193783 : adr = cp_build_addr_expr (exp, complain);
2572 : 4193783 : return cp_convert (ptrtype, adr, complain);
2573 : : }
2574 : :
2575 : : /* Otherwise, it's the lvalue-to-rvalue conversion. */
2576 : 787050388 : exp = mark_rvalue_use (exp, loc, reject_builtin);
2577 : :
2578 : : /* If a bitfield is used in a context where integral promotion
2579 : : applies, then the caller is expected to have used
2580 : : default_conversion. That function promotes bitfields correctly
2581 : : before calling this function. At this point, if we have a
2582 : : bitfield referenced, we may assume that is not subject to
2583 : : promotion, and that, therefore, the type of the resulting rvalue
2584 : : is the declared type of the bitfield. */
2585 : 787050388 : exp = convert_bitfield_to_declared_type (exp);
2586 : :
2587 : : /* We do not call rvalue() here because we do not want to wrap EXP
2588 : : in a NON_LVALUE_EXPR. */
2589 : :
2590 : : /* [basic.lval]
2591 : :
2592 : : Non-class rvalues always have cv-unqualified types. */
2593 : 787050388 : type = TREE_TYPE (exp);
2594 : 787050388 : if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2595 : 242648241 : exp = build_nop (cv_unqualified (type), exp);
2596 : :
2597 : 787050388 : if (!complete_type_or_maybe_complain (type, exp, complain))
2598 : 42 : return error_mark_node;
2599 : :
2600 : : return exp;
2601 : : }
2602 : :
2603 : : /* Perform preparatory conversions, as part of the "usual arithmetic
2604 : : conversions". In particular, as per [expr]:
2605 : :
2606 : : Whenever an lvalue expression appears as an operand of an
2607 : : operator that expects the rvalue for that operand, the
2608 : : lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2609 : : standard conversions are applied to convert the expression to an
2610 : : rvalue.
2611 : :
2612 : : In addition, we perform integral promotions here, as those are
2613 : : applied to both operands to a binary operator before determining
2614 : : what additional conversions should apply. */
2615 : :
2616 : : static tree
2617 : 252126790 : cp_default_conversion (tree exp, tsubst_flags_t complain)
2618 : : {
2619 : : /* Check for target-specific promotions. */
2620 : 252126790 : tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2621 : 252126790 : if (promoted_type)
2622 : 0 : exp = cp_convert (promoted_type, exp, complain);
2623 : : /* Perform the integral promotions first so that bitfield
2624 : : expressions (which may promote to "int", even if the bitfield is
2625 : : declared "unsigned") are promoted correctly. */
2626 : 252126790 : else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2627 : 167209766 : exp = cp_perform_integral_promotions (exp, complain);
2628 : : /* Perform the other conversions. */
2629 : 252126790 : exp = decay_conversion (exp, complain);
2630 : :
2631 : 252126790 : return exp;
2632 : : }
2633 : :
2634 : : /* C version. */
2635 : :
2636 : : tree
2637 : 37669315 : default_conversion (tree exp)
2638 : : {
2639 : 37669315 : return cp_default_conversion (exp, tf_warning_or_error);
2640 : : }
2641 : :
2642 : : /* EXPR is an expression with an integral or enumeration type.
2643 : : Perform the integral promotions in [conv.prom], and return the
2644 : : converted value. */
2645 : :
2646 : : tree
2647 : 175683891 : cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2648 : : {
2649 : 175683891 : tree type;
2650 : 175683891 : tree promoted_type;
2651 : :
2652 : 175683891 : expr = mark_rvalue_use (expr);
2653 : 175683891 : if (error_operand_p (expr))
2654 : 6 : return error_mark_node;
2655 : :
2656 : 175683885 : type = TREE_TYPE (expr);
2657 : :
2658 : : /* [conv.prom]
2659 : :
2660 : : A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2661 : : of type int if int can represent all the values of the bit-field;
2662 : : otherwise, it can be converted to unsigned int if unsigned int can
2663 : : represent all the values of the bit-field. If the bit-field is larger yet,
2664 : : no integral promotion applies to it. If the bit-field has an enumerated
2665 : : type, it is treated as any other value of that type for promotion
2666 : : purposes. */
2667 : 175683885 : tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2668 : 175683885 : if (bitfield_type
2669 : 175683885 : && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2670 : 255665 : || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2671 : : type = bitfield_type;
2672 : :
2673 : 175683885 : gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2674 : : /* Scoped enums don't promote. */
2675 : 175683885 : if (SCOPED_ENUM_P (type))
2676 : : return expr;
2677 : 175255221 : promoted_type = type_promotes_to (type);
2678 : 175255221 : if (type != promoted_type)
2679 : 50354421 : expr = cp_convert (promoted_type, expr, complain);
2680 : 124900800 : else if (bitfield_type && bitfield_type != type)
2681 : : /* Prevent decay_conversion from converting to bitfield_type. */
2682 : 159 : expr = build_nop (type, expr);
2683 : : return expr;
2684 : : }
2685 : :
2686 : : /* C version. */
2687 : :
2688 : : tree
2689 : 1753965 : perform_integral_promotions (tree expr)
2690 : : {
2691 : 1753965 : return cp_perform_integral_promotions (expr, tf_warning_or_error);
2692 : : }
2693 : :
2694 : : /* Returns nonzero iff exp is a STRING_CST or the result of applying
2695 : : decay_conversion to one. */
2696 : :
2697 : : int
2698 : 3031444 : string_conv_p (const_tree totype, const_tree exp, int warn)
2699 : : {
2700 : 3031444 : tree t;
2701 : :
2702 : 3031444 : if (!TYPE_PTR_P (totype))
2703 : : return 0;
2704 : :
2705 : 3031393 : t = TREE_TYPE (totype);
2706 : 3031393 : if (!same_type_p (t, char_type_node)
2707 : 2891404 : && !same_type_p (t, char8_type_node)
2708 : 2871558 : && !same_type_p (t, char16_type_node)
2709 : 2813190 : && !same_type_p (t, char32_type_node)
2710 : 5786216 : && !same_type_p (t, wchar_type_node))
2711 : : return 0;
2712 : :
2713 : 326567 : location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2714 : :
2715 : 326567 : STRIP_ANY_LOCATION_WRAPPER (exp);
2716 : :
2717 : 326567 : if (TREE_CODE (exp) == STRING_CST)
2718 : : {
2719 : : /* Make sure that we don't try to convert between char and wide chars. */
2720 : 108 : if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2721 : : return 0;
2722 : : }
2723 : : else
2724 : : {
2725 : : /* Is this a string constant which has decayed to 'const char *'? */
2726 : 326459 : t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2727 : 326459 : if (!same_type_p (TREE_TYPE (exp), t))
2728 : : return 0;
2729 : 60053 : STRIP_NOPS (exp);
2730 : 60053 : if (TREE_CODE (exp) != ADDR_EXPR
2731 : 60053 : || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2732 : : return 0;
2733 : : }
2734 : 306 : if (warn)
2735 : : {
2736 : 102 : if (cxx_dialect >= cxx11)
2737 : 87 : pedwarn (loc, OPT_Wwrite_strings,
2738 : : "ISO C++ forbids converting a string constant to %qT",
2739 : : totype);
2740 : : else
2741 : 15 : warning_at (loc, OPT_Wwrite_strings,
2742 : : "deprecated conversion from string constant to %qT",
2743 : : totype);
2744 : : }
2745 : :
2746 : : return 1;
2747 : : }
2748 : :
2749 : : /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2750 : : can, for example, use as an lvalue. This code used to be in
2751 : : unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2752 : : expressions, where we're dealing with aggregates. But now it's again only
2753 : : called from unary_complex_lvalue. The case (in particular) that led to
2754 : : this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2755 : : get it there. */
2756 : :
2757 : : static tree
2758 : 92952 : rationalize_conditional_expr (enum tree_code code, tree t,
2759 : : tsubst_flags_t complain)
2760 : : {
2761 : 92952 : location_t loc = cp_expr_loc_or_input_loc (t);
2762 : :
2763 : : /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2764 : : the first operand is always the one to be used if both operands
2765 : : are equal, so we know what conditional expression this used to be. */
2766 : 92952 : if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2767 : : {
2768 : 0 : tree op0 = TREE_OPERAND (t, 0);
2769 : 0 : tree op1 = TREE_OPERAND (t, 1);
2770 : :
2771 : : /* The following code is incorrect if either operand side-effects. */
2772 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (op0)
2773 : : && !TREE_SIDE_EFFECTS (op1));
2774 : 0 : return
2775 : 0 : build_conditional_expr (loc,
2776 : 0 : build_x_binary_op (loc,
2777 : 0 : (TREE_CODE (t) == MIN_EXPR
2778 : : ? LE_EXPR : GE_EXPR),
2779 : 0 : op0, TREE_CODE (op0),
2780 : 0 : op1, TREE_CODE (op1),
2781 : : NULL_TREE,
2782 : : /*overload=*/NULL,
2783 : : complain),
2784 : : cp_build_unary_op (code, op0, false, complain),
2785 : : cp_build_unary_op (code, op1, false, complain),
2786 : : complain);
2787 : : }
2788 : :
2789 : 92952 : tree op1 = TREE_OPERAND (t, 1);
2790 : 92952 : if (TREE_CODE (op1) != THROW_EXPR)
2791 : 92949 : op1 = cp_build_unary_op (code, op1, false, complain);
2792 : 92952 : tree op2 = TREE_OPERAND (t, 2);
2793 : 92952 : if (TREE_CODE (op2) != THROW_EXPR)
2794 : 92946 : op2 = cp_build_unary_op (code, op2, false, complain);
2795 : :
2796 : 92952 : return
2797 : 92952 : build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2798 : : }
2799 : :
2800 : : /* Given the TYPE of an anonymous union field inside T, return the
2801 : : FIELD_DECL for the field. If not found return NULL_TREE. Because
2802 : : anonymous unions can nest, we must also search all anonymous unions
2803 : : that are directly reachable. */
2804 : :
2805 : : tree
2806 : 1000844 : lookup_anon_field (tree, tree type)
2807 : : {
2808 : 1000844 : tree field;
2809 : :
2810 : 1000844 : type = TYPE_MAIN_VARIANT (type);
2811 : 1000844 : field = ANON_AGGR_TYPE_FIELD (type);
2812 : 1000844 : gcc_assert (field);
2813 : 1000844 : return field;
2814 : : }
2815 : :
2816 : : /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2817 : : expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2818 : : non-NULL, it indicates the path to the base used to name MEMBER.
2819 : : If PRESERVE_REFERENCE is true, the expression returned will have
2820 : : REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2821 : : returned will have the type referred to by the reference.
2822 : :
2823 : : This function does not perform access control; that is either done
2824 : : earlier by the parser when the name of MEMBER is resolved to MEMBER
2825 : : itself, or later when overload resolution selects one of the
2826 : : functions indicated by MEMBER. */
2827 : :
2828 : : tree
2829 : 96429343 : build_class_member_access_expr (cp_expr object, tree member,
2830 : : tree access_path, bool preserve_reference,
2831 : : tsubst_flags_t complain)
2832 : : {
2833 : 96429343 : tree object_type;
2834 : 96429343 : tree member_scope;
2835 : 96429343 : tree result = NULL_TREE;
2836 : 96429343 : tree using_decl = NULL_TREE;
2837 : :
2838 : 96429343 : if (error_operand_p (object) || error_operand_p (member))
2839 : 48 : return error_mark_node;
2840 : :
2841 : 96429295 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2842 : :
2843 : : /* [expr.ref]
2844 : :
2845 : : The type of the first expression shall be "class object" (of a
2846 : : complete type). */
2847 : 96429295 : object_type = TREE_TYPE (object);
2848 : 96429295 : if (!currently_open_class (object_type)
2849 : 96429295 : && !complete_type_or_maybe_complain (object_type, object, complain))
2850 : 0 : return error_mark_node;
2851 : 96429295 : if (!CLASS_TYPE_P (object_type))
2852 : : {
2853 : 0 : if (complain & tf_error)
2854 : : {
2855 : 0 : if (INDIRECT_TYPE_P (object_type)
2856 : 0 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
2857 : 0 : error ("request for member %qD in %qE, which is of pointer "
2858 : : "type %qT (maybe you meant to use %<->%> ?)",
2859 : : member, object.get_value (), object_type);
2860 : : else
2861 : 0 : error ("request for member %qD in %qE, which is of non-class "
2862 : : "type %qT", member, object.get_value (), object_type);
2863 : : }
2864 : 0 : return error_mark_node;
2865 : : }
2866 : :
2867 : : /* The standard does not seem to actually say that MEMBER must be a
2868 : : member of OBJECT_TYPE. However, that is clearly what is
2869 : : intended. */
2870 : 96429295 : if (DECL_P (member))
2871 : : {
2872 : 41179866 : member_scope = DECL_CLASS_CONTEXT (member);
2873 : 41179866 : if (!mark_used (member, complain) && !(complain & tf_error))
2874 : 0 : return error_mark_node;
2875 : :
2876 : 41179866 : if (TREE_UNAVAILABLE (member))
2877 : 30 : error_unavailable_use (member, NULL_TREE);
2878 : 41179836 : else if (TREE_DEPRECATED (member))
2879 : 30 : warn_deprecated_use (member, NULL_TREE);
2880 : : }
2881 : : else
2882 : 55249429 : member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2883 : : /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2884 : : presently be the anonymous union. Go outwards until we find a
2885 : : type related to OBJECT_TYPE. */
2886 : 97433853 : while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2887 : 98438818 : && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2888 : : object_type))
2889 : 1004558 : member_scope = TYPE_CONTEXT (member_scope);
2890 : 96429295 : if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2891 : : {
2892 : 3 : if (complain & tf_error)
2893 : : {
2894 : 3 : if (TREE_CODE (member) == FIELD_DECL)
2895 : 0 : error ("invalid use of non-static data member %qE", member);
2896 : : else
2897 : 3 : error ("%qD is not a member of %qT", member, object_type);
2898 : : }
2899 : 3 : return error_mark_node;
2900 : : }
2901 : :
2902 : : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2903 : : `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2904 : : in the front end; only _DECLs and _REFs are lvalues in the back end. */
2905 : 96429292 : if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2906 : : {
2907 : 38 : temp = cp_build_fold_indirect_ref (temp);
2908 : 38 : if (!lvalue_p (object) && lvalue_p (temp))
2909 : : /* Preserve rvalueness. */
2910 : 17 : temp = move (temp);
2911 : 38 : object = temp;
2912 : : }
2913 : :
2914 : : /* In [expr.ref], there is an explicit list of the valid choices for
2915 : : MEMBER. We check for each of those cases here. */
2916 : 96429292 : if (VAR_P (member))
2917 : : {
2918 : : /* A static data member. */
2919 : 87951 : result = member;
2920 : 87951 : mark_exp_read (object);
2921 : :
2922 : 87951 : if (tree wrap = maybe_get_tls_wrapper_call (result))
2923 : : /* Replace an evaluated use of the thread_local variable with
2924 : : a call to its wrapper. */
2925 : 102 : result = wrap;
2926 : :
2927 : : /* If OBJECT has side-effects, they are supposed to occur. */
2928 : 87951 : if (TREE_SIDE_EFFECTS (object))
2929 : 51 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2930 : : }
2931 : 96341341 : else if (TREE_CODE (member) == FIELD_DECL)
2932 : : {
2933 : : /* A non-static data member. */
2934 : 41091789 : bool null_object_p;
2935 : 41091789 : int type_quals;
2936 : 41091789 : tree member_type;
2937 : :
2938 : 41091789 : if (INDIRECT_REF_P (object))
2939 : 31094814 : null_object_p =
2940 : 31094814 : integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2941 : : else
2942 : : null_object_p = false;
2943 : :
2944 : : /* Convert OBJECT to the type of MEMBER. */
2945 : 41091789 : if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2946 : : TYPE_MAIN_VARIANT (member_scope)))
2947 : : {
2948 : 1668609 : tree binfo;
2949 : 1668609 : base_kind kind;
2950 : :
2951 : : /* We didn't complain above about a currently open class, but now we
2952 : : must: we don't know how to refer to a base member before layout is
2953 : : complete. But still don't complain in a template. */
2954 : 1668609 : if (!cp_unevaluated_operand
2955 : 1668252 : && !dependent_type_p (object_type)
2956 : 3057401 : && !complete_type_or_maybe_complain (object_type, object,
2957 : : complain))
2958 : 9 : return error_mark_node;
2959 : :
2960 : 2078313 : binfo = lookup_base (access_path ? access_path : object_type,
2961 : : member_scope, ba_unique, &kind, complain);
2962 : 1668606 : if (binfo == error_mark_node)
2963 : : return error_mark_node;
2964 : :
2965 : : /* It is invalid to try to get to a virtual base of a
2966 : : NULL object. The most common cause is invalid use of
2967 : : offsetof macro. */
2968 : 1668606 : if (null_object_p && kind == bk_via_virtual)
2969 : : {
2970 : 6 : if (complain & tf_error)
2971 : : {
2972 : 6 : error ("invalid access to non-static data member %qD in "
2973 : : "virtual base of NULL object", member);
2974 : : }
2975 : 6 : return error_mark_node;
2976 : : }
2977 : :
2978 : : /* Convert to the base. */
2979 : 3337200 : object = build_base_path (PLUS_EXPR, object, binfo,
2980 : 1668600 : /*nonnull=*/1, complain);
2981 : : /* If we found the base successfully then we should be able
2982 : : to convert to it successfully. */
2983 : 1668600 : gcc_assert (object != error_mark_node || seen_error ());
2984 : : }
2985 : :
2986 : : /* If MEMBER is from an anonymous aggregate, we have converted
2987 : : OBJECT so that it refers to the class containing the
2988 : : anonymous union. Generate a reference to the anonymous union
2989 : : itself, and recur to find MEMBER. */
2990 : 82183560 : if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2991 : : /* When this code is called from build_field_call, the
2992 : : object already has the type of the anonymous union.
2993 : : That is because the COMPONENT_REF was already
2994 : : constructed, and was then disassembled before calling
2995 : : build_field_call. After the function-call code is
2996 : : cleaned up, this waste can be eliminated. */
2997 : 42092869 : && (!same_type_ignoring_top_level_qualifiers_p
2998 : 1001089 : (TREE_TYPE (object), DECL_CONTEXT (member))))
2999 : : {
3000 : 1000688 : tree anonymous_union;
3001 : :
3002 : 1000688 : anonymous_union = lookup_anon_field (TREE_TYPE (object),
3003 : 1000688 : DECL_CONTEXT (member));
3004 : 1000688 : object = build_class_member_access_expr (object,
3005 : : anonymous_union,
3006 : : /*access_path=*/NULL_TREE,
3007 : : preserve_reference,
3008 : 1000688 : complain);
3009 : : }
3010 : :
3011 : : /* Compute the type of the field, as described in [expr.ref]. */
3012 : 41091780 : type_quals = TYPE_UNQUALIFIED;
3013 : 41091780 : member_type = TREE_TYPE (member);
3014 : 41091780 : if (!TYPE_REF_P (member_type))
3015 : : {
3016 : 40290221 : type_quals = (cp_type_quals (member_type)
3017 : 40290221 : | cp_type_quals (object_type));
3018 : :
3019 : : /* A field is const (volatile) if the enclosing object, or the
3020 : : field itself, is const (volatile). But, a mutable field is
3021 : : not const, even within a const object. */
3022 : 40290221 : if (DECL_MUTABLE_P (member))
3023 : 320335 : type_quals &= ~TYPE_QUAL_CONST;
3024 : 40290221 : member_type = cp_build_qualified_type (member_type, type_quals);
3025 : : }
3026 : :
3027 : 41091780 : result = build3_loc (input_location, COMPONENT_REF, member_type,
3028 : : object, member, NULL_TREE);
3029 : :
3030 : : /* Mark the expression const or volatile, as appropriate. Even
3031 : : though we've dealt with the type above, we still have to mark the
3032 : : expression itself. */
3033 : 41091780 : if (type_quals & TYPE_QUAL_CONST)
3034 : 13144691 : TREE_READONLY (result) = 1;
3035 : 41091780 : if (type_quals & TYPE_QUAL_VOLATILE)
3036 : 180597 : TREE_THIS_VOLATILE (result) = 1;
3037 : : }
3038 : 55249552 : else if (BASELINK_P (member))
3039 : : {
3040 : : /* The member is a (possibly overloaded) member function. */
3041 : 55249426 : tree functions;
3042 : 55249426 : tree type;
3043 : :
3044 : : /* If the MEMBER is exactly one static member function, then we
3045 : : know the type of the expression. Otherwise, we must wait
3046 : : until overload resolution has been performed. */
3047 : 55249426 : functions = BASELINK_FUNCTIONS (member);
3048 : 55249426 : if (TREE_CODE (functions) == OVERLOAD && OVL_SINGLE_P (functions))
3049 : 55249426 : functions = OVL_FIRST (functions);
3050 : 55249426 : if (TREE_CODE (functions) == FUNCTION_DECL
3051 : 55249426 : && DECL_STATIC_FUNCTION_P (functions))
3052 : 425878 : type = TREE_TYPE (functions);
3053 : : else
3054 : 54823548 : type = unknown_type_node;
3055 : : /* Note that we do not convert OBJECT to the BASELINK_BINFO
3056 : : base. That will happen when the function is called. */
3057 : 55249426 : result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3058 : : NULL_TREE);
3059 : : }
3060 : 126 : else if (TREE_CODE (member) == CONST_DECL)
3061 : : {
3062 : : /* The member is an enumerator. */
3063 : 110 : result = member;
3064 : : /* If OBJECT has side-effects, they are supposed to occur. */
3065 : 110 : if (TREE_SIDE_EFFECTS (object))
3066 : 6 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3067 : : object, result);
3068 : : }
3069 : 16 : else if ((using_decl = strip_using_decl (member)) != member)
3070 : 0 : result = build_class_member_access_expr (object,
3071 : : using_decl,
3072 : : access_path, preserve_reference,
3073 : : complain);
3074 : : else
3075 : : {
3076 : 16 : if (complain & tf_error)
3077 : 12 : error ("invalid use of %qD", member);
3078 : 16 : return error_mark_node;
3079 : : }
3080 : :
3081 : 96429267 : if (!preserve_reference)
3082 : : /* [expr.ref]
3083 : :
3084 : : If E2 is declared to have type "reference to T", then ... the
3085 : : type of E1.E2 is T. */
3086 : 91234794 : result = convert_from_reference (result);
3087 : :
3088 : : return result;
3089 : : }
3090 : :
3091 : : /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3092 : : SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3093 : :
3094 : : tree
3095 : 150885 : lookup_destructor (tree object, tree scope, tree dtor_name,
3096 : : tsubst_flags_t complain)
3097 : : {
3098 : 150885 : tree object_type = TREE_TYPE (object);
3099 : 150885 : tree dtor_type = TREE_OPERAND (dtor_name, 0);
3100 : 150885 : tree expr;
3101 : :
3102 : : /* We've already complained about this destructor. */
3103 : 150885 : if (dtor_type == error_mark_node)
3104 : : return error_mark_node;
3105 : :
3106 : 150879 : if (scope && !check_dtor_name (scope, dtor_type))
3107 : : {
3108 : 3 : if (complain & tf_error)
3109 : 3 : error ("qualified type %qT does not match destructor name ~%qT",
3110 : : scope, dtor_type);
3111 : 3 : return error_mark_node;
3112 : : }
3113 : 150876 : if (is_auto (dtor_type))
3114 : : dtor_type = object_type;
3115 : 150873 : else if (identifier_p (dtor_type))
3116 : : {
3117 : : /* In a template, names we can't find a match for are still accepted
3118 : : destructor names, and we check them here. */
3119 : 23 : if (check_dtor_name (object_type, dtor_type))
3120 : : dtor_type = object_type;
3121 : : else
3122 : : {
3123 : 3 : if (complain & tf_error)
3124 : 3 : error ("object type %qT does not match destructor name ~%qT",
3125 : : object_type, dtor_type);
3126 : 3 : return error_mark_node;
3127 : : }
3128 : :
3129 : : }
3130 : 150850 : else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3131 : : {
3132 : 6 : if (complain & tf_error)
3133 : 6 : error ("the type being destroyed is %qT, but the destructor "
3134 : 6 : "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3135 : 6 : return error_mark_node;
3136 : : }
3137 : 150867 : expr = lookup_member (dtor_type, complete_dtor_identifier,
3138 : : /*protect=*/1, /*want_type=*/false,
3139 : : tf_warning_or_error);
3140 : 150867 : if (!expr)
3141 : : {
3142 : 6 : if (complain & tf_error)
3143 : 6 : cxx_incomplete_type_error (dtor_name, dtor_type);
3144 : 6 : return error_mark_node;
3145 : : }
3146 : 150861 : expr = (adjust_result_of_qualified_name_lookup
3147 : 150861 : (expr, dtor_type, object_type));
3148 : 150861 : if (scope == NULL_TREE)
3149 : : /* We need to call adjust_result_of_qualified_name_lookup in case the
3150 : : destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3151 : : that we still get virtual function binding. */
3152 : 150718 : BASELINK_QUALIFIED_P (expr) = false;
3153 : : return expr;
3154 : : }
3155 : :
3156 : : /* An expression of the form "A::template B" has been resolved to
3157 : : DECL. Issue a diagnostic if B is not a template or template
3158 : : specialization. */
3159 : :
3160 : : void
3161 : 2998300 : check_template_keyword (tree decl)
3162 : : {
3163 : : /* The standard says:
3164 : :
3165 : : [temp.names]
3166 : :
3167 : : If a name prefixed by the keyword template is not a member
3168 : : template, the program is ill-formed.
3169 : :
3170 : : DR 228 removed the restriction that the template be a member
3171 : : template.
3172 : :
3173 : : DR 96, if accepted would add the further restriction that explicit
3174 : : template arguments must be provided if the template keyword is
3175 : : used, but, as of 2005-10-16, that DR is still in "drafting". If
3176 : : this DR is accepted, then the semantic checks here can be
3177 : : simplified, as the entity named must in fact be a template
3178 : : specialization, rather than, as at present, a set of overloaded
3179 : : functions containing at least one template function. */
3180 : 2998300 : if (TREE_CODE (decl) != TEMPLATE_DECL
3181 : 2998300 : && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3182 : : {
3183 : 2766798 : if (VAR_P (decl))
3184 : : {
3185 : 9769 : if (DECL_USE_TEMPLATE (decl)
3186 : 9769 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3187 : : ;
3188 : : else
3189 : 0 : permerror (input_location, "%qD is not a template", decl);
3190 : : }
3191 : 2757029 : else if (!is_overloaded_fn (decl))
3192 : 0 : permerror (input_location, "%qD is not a template", decl);
3193 : : else
3194 : : {
3195 : 2757029 : bool found = false;
3196 : :
3197 : 5514058 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3198 : 5514060 : !found && iter; ++iter)
3199 : : {
3200 : 2757031 : tree fn = *iter;
3201 : 2757031 : if (TREE_CODE (fn) == TEMPLATE_DECL
3202 : 2756635 : || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3203 : 2757045 : || (TREE_CODE (fn) == FUNCTION_DECL
3204 : 14 : && DECL_USE_TEMPLATE (fn)
3205 : 6 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3206 : : found = true;
3207 : : }
3208 : 2757029 : if (!found)
3209 : 12 : permerror (input_location, "%qD is not a template", decl);
3210 : : }
3211 : : }
3212 : 2998300 : }
3213 : :
3214 : : /* Record that an access failure occurred on BASETYPE_PATH attempting
3215 : : to access DECL, where DIAG_DECL should be used for diagnostics. */
3216 : :
3217 : : void
3218 : 406 : access_failure_info::record_access_failure (tree basetype_path,
3219 : : tree decl, tree diag_decl)
3220 : : {
3221 : 406 : m_was_inaccessible = true;
3222 : 406 : m_basetype_path = basetype_path;
3223 : 406 : m_decl = decl;
3224 : 406 : m_diag_decl = diag_decl;
3225 : 406 : }
3226 : :
3227 : : /* If an access failure was recorded, then attempt to locate an
3228 : : accessor function for the pertinent field.
3229 : : Otherwise, return NULL_TREE. */
3230 : :
3231 : : tree
3232 : 81297083 : access_failure_info::get_any_accessor (bool const_p) const
3233 : : {
3234 : 81297083 : if (!was_inaccessible_p ())
3235 : : return NULL_TREE;
3236 : :
3237 : 406 : tree accessor
3238 : 406 : = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3239 : 406 : if (!accessor)
3240 : : return NULL_TREE;
3241 : :
3242 : : /* The accessor must itself be accessible for it to be a reasonable
3243 : : suggestion. */
3244 : 161 : if (!accessible_p (m_basetype_path, accessor, true))
3245 : : return NULL_TREE;
3246 : :
3247 : : return accessor;
3248 : : }
3249 : :
3250 : : /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3251 : : replacing the primary location in RICHLOC with "accessor()". */
3252 : :
3253 : : void
3254 : 140 : access_failure_info::add_fixit_hint (rich_location *richloc,
3255 : : tree accessor_decl)
3256 : : {
3257 : 140 : pretty_printer pp;
3258 : 140 : pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3259 : 140 : pp_string (&pp, "()");
3260 : 140 : richloc->add_fixit_replace (pp_formatted_text (&pp));
3261 : 140 : }
3262 : :
3263 : : /* If an access failure was recorded, then attempt to locate an
3264 : : accessor function for the pertinent field, and if one is
3265 : : available, add a note and fix-it hint suggesting using it. */
3266 : :
3267 : : void
3268 : 81297013 : access_failure_info::maybe_suggest_accessor (bool const_p) const
3269 : : {
3270 : 81297013 : tree accessor = get_any_accessor (const_p);
3271 : 81297013 : if (accessor == NULL_TREE)
3272 : 81296915 : return;
3273 : 98 : rich_location richloc (line_table, input_location);
3274 : 98 : add_fixit_hint (&richloc, accessor);
3275 : 98 : inform (&richloc, "field %q#D can be accessed via %q#D",
3276 : 98 : m_diag_decl, accessor);
3277 : 98 : }
3278 : :
3279 : : /* Subroutine of finish_class_member_access_expr.
3280 : : Issue an error about NAME not being a member of ACCESS_PATH (or
3281 : : OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3282 : : names. */
3283 : :
3284 : : static void
3285 : 298 : complain_about_unrecognized_member (tree access_path, tree name,
3286 : : tree object_type)
3287 : : {
3288 : : /* Attempt to provide a hint about misspelled names. */
3289 : 298 : tree guessed_id = lookup_member_fuzzy (access_path, name,
3290 : : /*want_type=*/false);
3291 : 298 : if (guessed_id == NULL_TREE)
3292 : : {
3293 : : /* No hint. */
3294 : 188 : error ("%q#T has no member named %qE",
3295 : 188 : TREE_CODE (access_path) == TREE_BINFO
3296 : 6 : ? TREE_TYPE (access_path) : object_type, name);
3297 : 188 : return;
3298 : : }
3299 : :
3300 : 110 : location_t bogus_component_loc = input_location;
3301 : 110 : gcc_rich_location rich_loc (bogus_component_loc);
3302 : :
3303 : : /* Check that the guessed name is accessible along access_path. */
3304 : 110 : access_failure_info afi;
3305 : 110 : lookup_member (access_path, guessed_id, /*protect=*/1,
3306 : : /*want_type=*/false, /*complain=*/false,
3307 : : &afi);
3308 : 110 : if (afi.was_inaccessible_p ())
3309 : : {
3310 : 70 : tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3311 : 70 : if (accessor)
3312 : : {
3313 : : /* The guessed name isn't directly accessible, but can be accessed
3314 : : via an accessor member function. */
3315 : 42 : afi.add_fixit_hint (&rich_loc, accessor);
3316 : 42 : error_at (&rich_loc,
3317 : : "%q#T has no member named %qE;"
3318 : : " did you mean %q#D? (accessible via %q#D)",
3319 : 42 : TREE_CODE (access_path) == TREE_BINFO
3320 : 0 : ? TREE_TYPE (access_path) : object_type,
3321 : : name, afi.get_diag_decl (), accessor);
3322 : : }
3323 : : else
3324 : : {
3325 : : /* The guessed name isn't directly accessible, and no accessor
3326 : : member function could be found. */
3327 : 28 : auto_diagnostic_group d;
3328 : 28 : error_at (&rich_loc,
3329 : : "%q#T has no member named %qE;"
3330 : : " did you mean %q#D? (not accessible from this context)",
3331 : 28 : TREE_CODE (access_path) == TREE_BINFO
3332 : 0 : ? TREE_TYPE (access_path) : object_type,
3333 : : name, afi.get_diag_decl ());
3334 : 28 : complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3335 : : afi.get_diag_decl (), false, ak_none);
3336 : 28 : }
3337 : : }
3338 : : else
3339 : : {
3340 : : /* The guessed name is directly accessible; suggest it. */
3341 : 40 : rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3342 : : guessed_id);
3343 : 40 : error_at (&rich_loc,
3344 : : "%q#T has no member named %qE;"
3345 : : " did you mean %qE?",
3346 : 40 : TREE_CODE (access_path) == TREE_BINFO
3347 : 0 : ? TREE_TYPE (access_path) : object_type,
3348 : : name, guessed_id);
3349 : : }
3350 : 110 : }
3351 : :
3352 : : /* This function is called by the parser to process a class member
3353 : : access expression of the form OBJECT.NAME. NAME is a node used by
3354 : : the parser to represent a name; it is not yet a DECL. It may,
3355 : : however, be a BASELINK where the BASELINK_FUNCTIONS is a
3356 : : TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3357 : : there is no reason to do the lookup twice, so the parser keeps the
3358 : : BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3359 : : be a template via the use of the "A::template B" syntax. */
3360 : :
3361 : : tree
3362 : 160658396 : finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3363 : : tsubst_flags_t complain)
3364 : : {
3365 : 160658396 : tree expr;
3366 : 160658396 : tree object_type;
3367 : 160658396 : tree member;
3368 : 160658396 : tree access_path = NULL_TREE;
3369 : 160658396 : tree orig_object = object;
3370 : 160658396 : tree orig_name = name;
3371 : :
3372 : 160658396 : if (object == error_mark_node || name == error_mark_node)
3373 : : return error_mark_node;
3374 : :
3375 : : /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3376 : 160657967 : if (!objc_is_public (object, name))
3377 : 0 : return error_mark_node;
3378 : :
3379 : 160657967 : object_type = TREE_TYPE (object);
3380 : :
3381 : 160657967 : if (processing_template_decl)
3382 : : {
3383 : 135156821 : if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3384 : 135156821 : type_dependent_object_expression_p (object)
3385 : : /* If NAME is "f<args>", where either 'f' or 'args' is
3386 : : dependent, then the expression is dependent. */
3387 : 64654965 : || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3388 : 553898 : && dependent_template_id_p (TREE_OPERAND (name, 0),
3389 : 553898 : TREE_OPERAND (name, 1)))
3390 : : /* If NAME is "T::X" where "T" is dependent, then the
3391 : : expression is dependent. */
3392 : 64391313 : || (TREE_CODE (name) == SCOPE_REF
3393 : 107511 : && TYPE_P (TREE_OPERAND (name, 0))
3394 : 107511 : && dependent_scope_p (TREE_OPERAND (name, 0)))
3395 : : /* If NAME is operator T where "T" is dependent, we can't
3396 : : lookup until we instantiate the T. */
3397 : 199440837 : || (TREE_CODE (name) == IDENTIFIER_NODE
3398 : 63728430 : && IDENTIFIER_CONV_OP_P (name)
3399 : 41 : && dependent_type_p (TREE_TYPE (name))))
3400 : : {
3401 : 84078434 : dependent:
3402 : 84078434 : return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3403 : 84078434 : orig_object, orig_name, NULL_TREE);
3404 : : }
3405 : : }
3406 : 25501146 : else if (c_dialect_objc ()
3407 : 89785124 : && identifier_p (name)
3408 : 25501146 : && (expr = objc_maybe_build_component_ref (object, name)))
3409 : : return expr;
3410 : :
3411 : : /* [expr.ref]
3412 : :
3413 : : The type of the first expression shall be "class object" (of a
3414 : : complete type). */
3415 : 89785124 : if (!currently_open_class (object_type)
3416 : 89785124 : && !complete_type_or_maybe_complain (object_type, object, complain))
3417 : 76 : return error_mark_node;
3418 : 89785048 : if (!CLASS_TYPE_P (object_type))
3419 : : {
3420 : 54988 : if (complain & tf_error)
3421 : : {
3422 : 106 : if (INDIRECT_TYPE_P (object_type)
3423 : 106 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
3424 : 21 : error ("request for member %qD in %qE, which is of pointer "
3425 : : "type %qT (maybe you meant to use %<->%> ?)",
3426 : : name, object.get_value (), object_type);
3427 : : else
3428 : 85 : error ("request for member %qD in %qE, which is of non-class "
3429 : : "type %qT", name, object.get_value (), object_type);
3430 : : }
3431 : 54988 : return error_mark_node;
3432 : : }
3433 : :
3434 : 89730060 : if (BASELINK_P (name))
3435 : : /* A member function that has already been looked up. */
3436 : : member = name;
3437 : : else
3438 : : {
3439 : 81481082 : bool is_template_id = false;
3440 : 81481082 : tree template_args = NULL_TREE;
3441 : 81481082 : tree scope = NULL_TREE;
3442 : :
3443 : 81481082 : access_path = object_type;
3444 : :
3445 : 81481082 : if (TREE_CODE (name) == SCOPE_REF)
3446 : : {
3447 : : /* A qualified name. The qualifying class or namespace `S'
3448 : : has already been looked up; it is either a TYPE or a
3449 : : NAMESPACE_DECL. */
3450 : 1757 : scope = TREE_OPERAND (name, 0);
3451 : 1757 : name = TREE_OPERAND (name, 1);
3452 : :
3453 : : /* If SCOPE is a namespace, then the qualified name does not
3454 : : name a member of OBJECT_TYPE. */
3455 : 1757 : if (TREE_CODE (scope) == NAMESPACE_DECL)
3456 : : {
3457 : 0 : if (complain & tf_error)
3458 : 0 : error ("%<%D::%D%> is not a member of %qT",
3459 : : scope, name, object_type);
3460 : 0 : return error_mark_node;
3461 : : }
3462 : : }
3463 : :
3464 : 81481082 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3465 : : {
3466 : 439440 : is_template_id = true;
3467 : 439440 : template_args = TREE_OPERAND (name, 1);
3468 : 439440 : name = TREE_OPERAND (name, 0);
3469 : :
3470 : 838505 : if (!identifier_p (name))
3471 : 399083 : name = OVL_NAME (name);
3472 : : }
3473 : :
3474 : 81481082 : if (scope)
3475 : : {
3476 : 1757 : if (TREE_CODE (scope) == ENUMERAL_TYPE)
3477 : : {
3478 : 12 : gcc_assert (!is_template_id);
3479 : : /* Looking up a member enumerator (c++/56793). */
3480 : 24 : if (!TYPE_CLASS_SCOPE_P (scope)
3481 : 21 : || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3482 : : {
3483 : 3 : if (complain & tf_error)
3484 : 3 : error ("%<%D::%D%> is not a member of %qT",
3485 : : scope, name, object_type);
3486 : 3 : return error_mark_node;
3487 : : }
3488 : 9 : tree val = lookup_enumerator (scope, name);
3489 : 9 : if (!val)
3490 : : {
3491 : 6 : if (complain & tf_error)
3492 : 6 : error ("%qD is not a member of %qD",
3493 : : name, scope);
3494 : 6 : return error_mark_node;
3495 : : }
3496 : :
3497 : 3 : if (TREE_SIDE_EFFECTS (object))
3498 : 0 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3499 : 3 : return val;
3500 : : }
3501 : :
3502 : 1745 : gcc_assert (CLASS_TYPE_P (scope));
3503 : 1745 : gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3504 : :
3505 : 1745 : if (constructor_name_p (name, scope))
3506 : : {
3507 : 3 : if (complain & tf_error)
3508 : 3 : error ("cannot call constructor %<%T::%D%> directly",
3509 : : scope, name);
3510 : 3 : return error_mark_node;
3511 : : }
3512 : :
3513 : : /* NAME may refer to a static data member, in which case there is
3514 : : one copy of the data member that is shared by all the objects of
3515 : : the class. So NAME can be unambiguously referred to even if
3516 : : there are multiple indirect base classes containing NAME. */
3517 : 5226 : const base_access ba = [scope, name] ()
3518 : : {
3519 : 1742 : if (identifier_p (name))
3520 : : {
3521 : 1605 : tree m = lookup_member (scope, name, /*protect=*/0,
3522 : : /*want_type=*/false, tf_none);
3523 : 1605 : if (!m || shared_member_p (m))
3524 : 32 : return ba_any;
3525 : : }
3526 : : return ba_check;
3527 : 1742 : } ();
3528 : :
3529 : : /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3530 : 1742 : access_path = lookup_base (object_type, scope, ba, NULL, complain);
3531 : 1742 : if (access_path == error_mark_node)
3532 : : return error_mark_node;
3533 : 1730 : if (!access_path)
3534 : : {
3535 : 12 : if (any_dependent_bases_p (object_type))
3536 : 6 : goto dependent;
3537 : 6 : if (complain & tf_error)
3538 : 6 : error ("%qT is not a base of %qT", scope, object_type);
3539 : 6 : return error_mark_node;
3540 : : }
3541 : : }
3542 : :
3543 : 81481043 : if (TREE_CODE (name) == BIT_NOT_EXPR)
3544 : : {
3545 : 184030 : if (dependent_type_p (object_type))
3546 : : /* The destructor isn't declared yet. */
3547 : 33160 : goto dependent;
3548 : 150870 : member = lookup_destructor (object, scope, name, complain);
3549 : : }
3550 : : else
3551 : : {
3552 : : /* Look up the member. */
3553 : 81297013 : {
3554 : 81297013 : auto_diagnostic_group d;
3555 : 81297013 : access_failure_info afi;
3556 : 81297013 : if (processing_template_decl)
3557 : : /* Even though this class member access expression is at this
3558 : : point not dependent, the member itself may be dependent, and
3559 : : we must not potentially push a access check for a dependent
3560 : : member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3561 : : ahead of time here; we're going to redo this member lookup at
3562 : : instantiation time anyway. */
3563 : 64018828 : push_deferring_access_checks (dk_no_check);
3564 : 81297013 : member = lookup_member (access_path, name, /*protect=*/1,
3565 : : /*want_type=*/false, complain,
3566 : : &afi);
3567 : 81297013 : if (processing_template_decl)
3568 : 64018828 : pop_deferring_access_checks ();
3569 : 81297013 : afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3570 : 81297013 : }
3571 : 81297013 : if (member == NULL_TREE)
3572 : : {
3573 : 7958320 : if (dependentish_scope_p (object_type))
3574 : : /* Try again at instantiation time. */
3575 : 7945666 : goto dependent;
3576 : 12654 : if (complain & tf_error)
3577 : 298 : complain_about_unrecognized_member (access_path, name,
3578 : : object_type);
3579 : 12654 : return error_mark_node;
3580 : : }
3581 : 73338693 : if (member == error_mark_node)
3582 : : return error_mark_node;
3583 : 73338649 : if (DECL_P (member)
3584 : 73338649 : && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3585 : : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3586 : : wrong, so don't use it. */
3587 : 6 : goto dependent;
3588 : 73338643 : if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3589 : 5226753 : goto dependent;
3590 : : }
3591 : :
3592 : 68262760 : if (is_template_id)
3593 : : {
3594 : 439430 : tree templ = member;
3595 : :
3596 : 439430 : if (BASELINK_P (templ))
3597 : 439406 : member = lookup_template_function (templ, template_args);
3598 : 24 : else if (variable_template_p (templ))
3599 : 24 : member = (lookup_and_finish_template_variable
3600 : 24 : (templ, template_args, complain));
3601 : : else
3602 : : {
3603 : 0 : if (complain & tf_error)
3604 : 0 : error ("%qD is not a member template function", name);
3605 : 0 : return error_mark_node;
3606 : : }
3607 : : }
3608 : : }
3609 : :
3610 : 76511738 : if (TREE_UNAVAILABLE (member))
3611 : 30 : error_unavailable_use (member, NULL_TREE);
3612 : 76511708 : else if (TREE_DEPRECATED (member))
3613 : 30 : warn_deprecated_use (member, NULL_TREE);
3614 : :
3615 : 76511738 : if (template_p)
3616 : 9602 : check_template_keyword (member);
3617 : :
3618 : 76511738 : expr = build_class_member_access_expr (object, member, access_path,
3619 : : /*preserve_reference=*/false,
3620 : : complain);
3621 : 76511738 : if (processing_template_decl && expr != error_mark_node)
3622 : : {
3623 : 51078315 : if (BASELINK_P (member))
3624 : : {
3625 : 37823370 : if (TREE_CODE (orig_name) == SCOPE_REF)
3626 : 169 : BASELINK_QUALIFIED_P (member) = 1;
3627 : : orig_name = member;
3628 : : }
3629 : 51078315 : return build_min_non_dep (COMPONENT_REF, expr,
3630 : : orig_object, orig_name,
3631 : 51078315 : NULL_TREE);
3632 : : }
3633 : :
3634 : : return expr;
3635 : : }
3636 : :
3637 : : /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3638 : : type. */
3639 : :
3640 : : tree
3641 : 174682 : build_simple_component_ref (tree object, tree member)
3642 : : {
3643 : 174682 : tree type = cp_build_qualified_type (TREE_TYPE (member),
3644 : 174682 : cp_type_quals (TREE_TYPE (object)));
3645 : 174682 : return build3_loc (input_location,
3646 : : COMPONENT_REF, type,
3647 : 174682 : object, member, NULL_TREE);
3648 : : }
3649 : :
3650 : : /* Return an expression for the MEMBER_NAME field in the internal
3651 : : representation of PTRMEM, a pointer-to-member function. (Each
3652 : : pointer-to-member function type gets its own RECORD_TYPE so it is
3653 : : more convenient to access the fields by name than by FIELD_DECL.)
3654 : : This routine converts the NAME to a FIELD_DECL and then creates the
3655 : : node for the complete expression. */
3656 : :
3657 : : tree
3658 : 174640 : build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3659 : : {
3660 : 174640 : tree ptrmem_type;
3661 : 174640 : tree member;
3662 : :
3663 : 174640 : if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3664 : : {
3665 : 162 : for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3666 : 66 : if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3667 : 48 : return e.value;
3668 : 0 : gcc_unreachable ();
3669 : : }
3670 : :
3671 : : /* This code is a stripped down version of
3672 : : build_class_member_access_expr. It does not work to use that
3673 : : routine directly because it expects the object to be of class
3674 : : type. */
3675 : 174592 : ptrmem_type = TREE_TYPE (ptrmem);
3676 : 174592 : gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3677 : 261794 : for (member = TYPE_FIELDS (ptrmem_type); member;
3678 : 87202 : member = DECL_CHAIN (member))
3679 : 261794 : if (DECL_NAME (member) == member_name)
3680 : : break;
3681 : 174592 : return build_simple_component_ref (ptrmem, member);
3682 : : }
3683 : :
3684 : : /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3685 : : and for any other relevant operator. */
3686 : :
3687 : : static tree
3688 : 112663875 : op_unqualified_lookup (tree_code code, bool is_assign)
3689 : : {
3690 : 112663875 : tree lookups = NULL_TREE;
3691 : :
3692 : 112663875 : if (cxx_dialect >= cxx20 && !is_assign)
3693 : : {
3694 : 29964542 : if (code == NE_EXPR)
3695 : : {
3696 : : /* != can get rewritten in terms of ==. */
3697 : 1935160 : tree fnname = ovl_op_identifier (false, EQ_EXPR);
3698 : 1935160 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3699 : 1921652 : lookups = tree_cons (fnname, fns, lookups);
3700 : : }
3701 : 28029382 : else if (code == GT_EXPR || code == LE_EXPR
3702 : 28029382 : || code == LT_EXPR || code == GE_EXPR)
3703 : : {
3704 : : /* These can get rewritten in terms of <=>. */
3705 : 2593336 : tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3706 : 2593336 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3707 : 2471284 : lookups = tree_cons (fnname, fns, lookups);
3708 : : }
3709 : : }
3710 : :
3711 : 112663875 : tree fnname = ovl_op_identifier (is_assign, code);
3712 : 112663875 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3713 : 63611069 : lookups = tree_cons (fnname, fns, lookups);
3714 : :
3715 : 112663875 : if (lookups)
3716 : : return lookups;
3717 : : else
3718 : 48976373 : return build_tree_list (NULL_TREE, NULL_TREE);
3719 : : }
3720 : :
3721 : : /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3722 : : the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3723 : : name lookup for the given operator. */
3724 : :
3725 : : tree
3726 : 121614588 : build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3727 : : {
3728 : 121614588 : if (lookups)
3729 : : /* We're partially instantiating a dependent operator expression, and
3730 : : LOOKUPS is the result of phase 1 name lookup that we performed
3731 : : earlier at template definition time, so just reuse the corresponding
3732 : : DEPENDENT_OPERATOR_TYPE. */
3733 : 8950713 : return TREE_TYPE (lookups);
3734 : :
3735 : : /* Otherwise we're processing a dependent operator expression at template
3736 : : definition time, so perform phase 1 name lookup now. */
3737 : 112663875 : lookups = op_unqualified_lookup (code, is_assign);
3738 : :
3739 : 112663875 : tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3740 : 112663875 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3741 : 112663875 : TREE_TYPE (lookups) = type;
3742 : 112663875 : return type;
3743 : : }
3744 : :
3745 : : /* Given an expression PTR for a pointer, return an expression
3746 : : for the value pointed to.
3747 : : ERRORSTRING is the name of the operator to appear in error messages.
3748 : :
3749 : : This function may need to overload OPERATOR_FNNAME.
3750 : : Must also handle REFERENCE_TYPEs for C++. */
3751 : :
3752 : : tree
3753 : 36658439 : build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3754 : : tree lookups, tsubst_flags_t complain)
3755 : : {
3756 : 36658439 : tree orig_expr = expr;
3757 : 36658439 : tree rval;
3758 : 36658439 : tree overload = NULL_TREE;
3759 : :
3760 : 36658439 : if (processing_template_decl)
3761 : : {
3762 : : /* Retain the type if we know the operand is a pointer. */
3763 : 21525997 : if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3764 : : {
3765 : 11898207 : if (expr == current_class_ptr
3766 : 11898207 : || (TREE_CODE (expr) == NOP_EXPR
3767 : 7397295 : && TREE_OPERAND (expr, 0) == current_class_ptr
3768 : 7385555 : && (same_type_ignoring_top_level_qualifiers_p
3769 : 7385555 : (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3770 : 7385554 : return current_class_ref;
3771 : 4512653 : return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3772 : : }
3773 : 9627790 : if (type_dependent_expression_p (expr))
3774 : : {
3775 : 9486809 : expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3776 : 9486809 : TREE_TYPE (expr)
3777 : 9486809 : = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3778 : 9486809 : return expr;
3779 : : }
3780 : : }
3781 : :
3782 : 15273423 : rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3783 : : NULL_TREE, NULL_TREE, lookups,
3784 : : &overload, complain);
3785 : 15273423 : if (!rval)
3786 : 0 : rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3787 : :
3788 : 15273423 : if (processing_template_decl && rval != error_mark_node)
3789 : : {
3790 : 139140 : if (overload != NULL_TREE)
3791 : 139109 : return (build_min_non_dep_op_overload
3792 : 139109 : (INDIRECT_REF, rval, overload, orig_expr));
3793 : :
3794 : 31 : return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3795 : : }
3796 : : else
3797 : : return rval;
3798 : : }
3799 : :
3800 : : /* Like c-family strict_aliasing_warning, but don't warn for dependent
3801 : : types or expressions. */
3802 : :
3803 : : static bool
3804 : 518466 : cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3805 : : {
3806 : 518466 : if (processing_template_decl)
3807 : : {
3808 : 38534 : tree e = expr;
3809 : 38534 : STRIP_NOPS (e);
3810 : 38534 : if (dependent_type_p (type) || type_dependent_expression_p (e))
3811 : 6490 : return false;
3812 : : }
3813 : 511976 : return strict_aliasing_warning (loc, type, expr);
3814 : : }
3815 : :
3816 : : /* The implementation of the above, and of indirection implied by other
3817 : : constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3818 : :
3819 : : static tree
3820 : 280568442 : cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3821 : : tsubst_flags_t complain, bool do_fold)
3822 : : {
3823 : 280568442 : tree pointer, type;
3824 : :
3825 : : /* RO_NULL should only be used with the folding entry points below, not
3826 : : cp_build_indirect_ref. */
3827 : 280568442 : gcc_checking_assert (errorstring != RO_NULL || do_fold);
3828 : :
3829 : 280568442 : if (ptr == current_class_ptr
3830 : 280568442 : || (TREE_CODE (ptr) == NOP_EXPR
3831 : 27215649 : && TREE_OPERAND (ptr, 0) == current_class_ptr
3832 : 15073351 : && (same_type_ignoring_top_level_qualifiers_p
3833 : 15073351 : (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3834 : 16353211 : return current_class_ref;
3835 : :
3836 : 264215231 : pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3837 : 264215231 : ? ptr : decay_conversion (ptr, complain));
3838 : 264215231 : if (pointer == error_mark_node)
3839 : : return error_mark_node;
3840 : :
3841 : 264209383 : type = TREE_TYPE (pointer);
3842 : :
3843 : 264209383 : if (INDIRECT_TYPE_P (type))
3844 : : {
3845 : : /* [expr.unary.op]
3846 : :
3847 : : If the type of the expression is "pointer to T," the type
3848 : : of the result is "T." */
3849 : 264209076 : tree t = TREE_TYPE (type);
3850 : :
3851 : 250980625 : if ((CONVERT_EXPR_P (ptr)
3852 : 190891597 : || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3853 : 326081335 : && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3854 : : {
3855 : : /* If a warning is issued, mark it to avoid duplicates from
3856 : : the backend. This only needs to be done at
3857 : : warn_strict_aliasing > 2. */
3858 : 52095526 : if (warn_strict_aliasing > 2
3859 : 52538570 : && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3860 : 443044 : type, TREE_OPERAND (ptr, 0)))
3861 : 6 : suppress_warning (ptr, OPT_Wstrict_aliasing);
3862 : : }
3863 : :
3864 : 264209076 : if (VOID_TYPE_P (t))
3865 : : {
3866 : : /* A pointer to incomplete type (other than cv void) can be
3867 : : dereferenced [expr.unary.op]/1 */
3868 : 70 : if (complain & tf_error)
3869 : 19 : error_at (loc, "%qT is not a pointer-to-object type", type);
3870 : 70 : return error_mark_node;
3871 : : }
3872 : 255604296 : else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3873 : 5287736 : && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0)))
3874 : : /* Don't let this change the value category. '*&TARGET_EXPR'
3875 : : is an lvalue, but folding it into 'TARGET_EXPR' would turn
3876 : : it into a prvalue of class type. */
3877 : 269496742 : && lvalue_p (TREE_OPERAND (pointer, 0)))
3878 : : /* The POINTER was something like `&x'. We simplify `*&x' to
3879 : : `x'. */
3880 : 5258028 : return TREE_OPERAND (pointer, 0);
3881 : : else
3882 : : {
3883 : 258950978 : tree ref = build1 (INDIRECT_REF, t, pointer);
3884 : :
3885 : : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3886 : : so that we get the proper error message if the result is used
3887 : : to assign to. Also, &* is supposed to be a no-op. */
3888 : 258950978 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3889 : 258950978 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3890 : 258950978 : TREE_SIDE_EFFECTS (ref)
3891 : 258950978 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3892 : 258950978 : return ref;
3893 : : }
3894 : : }
3895 : 307 : else if (!(complain & tf_error))
3896 : : /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3897 : : ;
3898 : : /* `pointer' won't be an error_mark_node if we were given a
3899 : : pointer to member, so it's cool to check for this here. */
3900 : 33 : else if (TYPE_PTRMEM_P (type))
3901 : 15 : switch (errorstring)
3902 : : {
3903 : 0 : case RO_ARRAY_INDEXING:
3904 : 0 : error_at (loc,
3905 : : "invalid use of array indexing on pointer to member");
3906 : 0 : break;
3907 : 12 : case RO_UNARY_STAR:
3908 : 12 : error_at (loc, "invalid use of unary %<*%> on pointer to member");
3909 : 12 : break;
3910 : 0 : case RO_IMPLICIT_CONVERSION:
3911 : 0 : error_at (loc, "invalid use of implicit conversion on pointer "
3912 : : "to member");
3913 : 0 : break;
3914 : 3 : case RO_ARROW_STAR:
3915 : 3 : error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3916 : : "class, but is a pointer to member of type %qT", type);
3917 : 3 : break;
3918 : 0 : default:
3919 : 0 : gcc_unreachable ();
3920 : : }
3921 : 18 : else if (pointer != error_mark_node)
3922 : 18 : invalid_indirection_error (loc, type, errorstring);
3923 : :
3924 : 307 : return error_mark_node;
3925 : : }
3926 : :
3927 : : /* Entry point used by c-common, which expects folding. */
3928 : :
3929 : : tree
3930 : 28256 : build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3931 : : {
3932 : 28256 : return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3933 : 28256 : tf_warning_or_error, true);
3934 : : }
3935 : :
3936 : : /* Entry point used by internal indirection needs that don't correspond to any
3937 : : syntactic construct. */
3938 : :
3939 : : tree
3940 : 258637167 : cp_build_fold_indirect_ref (tree pointer)
3941 : : {
3942 : 258637167 : return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3943 : 258637167 : tf_warning_or_error, true);
3944 : : }
3945 : :
3946 : : /* Entry point used by indirection needs that correspond to some syntactic
3947 : : construct. */
3948 : :
3949 : : tree
3950 : 21903019 : cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3951 : : tsubst_flags_t complain)
3952 : : {
3953 : 21903019 : return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3954 : : }
3955 : :
3956 : : /* This handles expressions of the form "a[i]", which denotes
3957 : : an array reference.
3958 : :
3959 : : This is logically equivalent in C to *(a+i), but we may do it differently.
3960 : : If A is a variable or a member, we generate a primitive ARRAY_REF.
3961 : : This avoids forcing the array out of registers, and can work on
3962 : : arrays that are not lvalues (for example, members of structures returned
3963 : : by functions).
3964 : :
3965 : : If INDEX is of some user-defined type, it must be converted to
3966 : : integer type. Otherwise, to make a compatible PLUS_EXPR, it
3967 : : will inherit the type of the array, which will be some pointer type.
3968 : :
3969 : : LOC is the location to use in building the array reference. */
3970 : :
3971 : : tree
3972 : 5061488 : cp_build_array_ref (location_t loc, tree array, tree idx,
3973 : : tsubst_flags_t complain)
3974 : : {
3975 : 5061488 : tree first = NULL_TREE;
3976 : 5061488 : tree ret;
3977 : :
3978 : 5061488 : if (idx == 0)
3979 : : {
3980 : 0 : if (complain & tf_error)
3981 : 0 : error_at (loc, "subscript missing in array reference");
3982 : 0 : return error_mark_node;
3983 : : }
3984 : :
3985 : 5061488 : if (TREE_TYPE (array) == error_mark_node
3986 : 5061488 : || TREE_TYPE (idx) == error_mark_node)
3987 : : return error_mark_node;
3988 : :
3989 : : /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3990 : : inside it. */
3991 : 5061488 : switch (TREE_CODE (array))
3992 : : {
3993 : 26 : case COMPOUND_EXPR:
3994 : 26 : {
3995 : 26 : tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3996 : : complain);
3997 : 26 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3998 : 26 : TREE_OPERAND (array, 0), value);
3999 : 26 : SET_EXPR_LOCATION (ret, loc);
4000 : 26 : return ret;
4001 : : }
4002 : :
4003 : 13 : case COND_EXPR:
4004 : 13 : ret = build_conditional_expr
4005 : 26 : (loc, TREE_OPERAND (array, 0),
4006 : 13 : cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
4007 : : complain),
4008 : 13 : cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
4009 : : complain),
4010 : : complain);
4011 : 13 : protected_set_expr_location (ret, loc);
4012 : 13 : return ret;
4013 : :
4014 : 5061449 : default:
4015 : 5061449 : break;
4016 : : }
4017 : :
4018 : 5061449 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
4019 : :
4020 : : /* 0[array] */
4021 : 5061449 : if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE)
4022 : : {
4023 : 7 : std::swap (array, idx);
4024 : 7 : if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array))
4025 : 6 : idx = first = save_expr (idx);
4026 : : }
4027 : :
4028 : 5061449 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4029 : : {
4030 : 1617423 : tree rval, type;
4031 : :
4032 : 1617423 : warn_array_subscript_with_type_char (loc, idx);
4033 : :
4034 : 1617423 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
4035 : : {
4036 : 3 : if (complain & tf_error)
4037 : 3 : error_at (loc, "array subscript is not an integer");
4038 : 3 : return error_mark_node;
4039 : : }
4040 : :
4041 : : /* Apply integral promotions *after* noticing character types.
4042 : : (It is unclear why we do these promotions -- the standard
4043 : : does not say that we should. In fact, the natural thing would
4044 : : seem to be to convert IDX to ptrdiff_t; we're performing
4045 : : pointer arithmetic.) */
4046 : 1617420 : idx = cp_perform_integral_promotions (idx, complain);
4047 : :
4048 : 1617420 : idx = maybe_fold_non_dependent_expr (idx, complain);
4049 : :
4050 : : /* An array that is indexed by a non-constant
4051 : : cannot be stored in a register; we must be able to do
4052 : : address arithmetic on its address.
4053 : : Likewise an array of elements of variable size. */
4054 : 1617420 : if (TREE_CODE (idx) != INTEGER_CST
4055 : 1617420 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4056 : 807330 : && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4057 : : != INTEGER_CST)))
4058 : : {
4059 : 811341 : if (!cxx_mark_addressable (array, true))
4060 : 0 : return error_mark_node;
4061 : : }
4062 : :
4063 : : /* An array that is indexed by a constant value which is not within
4064 : : the array bounds cannot be stored in a register either; because we
4065 : : would get a crash in store_bit_field/extract_bit_field when trying
4066 : : to access a non-existent part of the register. */
4067 : 1617420 : if (TREE_CODE (idx) == INTEGER_CST
4068 : 807333 : && TYPE_DOMAIN (TREE_TYPE (array))
4069 : 2423382 : && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4070 : : {
4071 : 2836 : if (!cxx_mark_addressable (array))
4072 : 0 : return error_mark_node;
4073 : : }
4074 : :
4075 : : /* Note in C++ it is valid to subscript a `register' array, since
4076 : : it is valid to take the address of something with that
4077 : : storage specification. */
4078 : 1617420 : if (extra_warnings)
4079 : : {
4080 : 37510 : tree foo = array;
4081 : 57472 : while (TREE_CODE (foo) == COMPONENT_REF)
4082 : 19962 : foo = TREE_OPERAND (foo, 0);
4083 : 8 : if (VAR_P (foo) && DECL_REGISTER (foo)
4084 : 37510 : && (complain & tf_warning))
4085 : 0 : warning_at (loc, OPT_Wextra,
4086 : : "subscripting array declared %<register%>");
4087 : : }
4088 : :
4089 : 1617420 : type = TREE_TYPE (TREE_TYPE (array));
4090 : 1617420 : rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4091 : : /* Array ref is const/volatile if the array elements are
4092 : : or if the array is.. */
4093 : 4852260 : TREE_READONLY (rval)
4094 : 1617420 : |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4095 : 4852260 : TREE_SIDE_EFFECTS (rval)
4096 : 1617420 : |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4097 : 3234840 : TREE_THIS_VOLATILE (rval)
4098 : 1617420 : |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4099 : 1617420 : ret = require_complete_type (rval, complain);
4100 : 1617420 : protected_set_expr_location (ret, loc);
4101 : 1617420 : if (non_lvalue)
4102 : 6032 : ret = non_lvalue_loc (loc, ret);
4103 : 1617420 : if (first)
4104 : 6 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4105 : 1617420 : return ret;
4106 : : }
4107 : :
4108 : 3444026 : {
4109 : 3444026 : tree ar = cp_default_conversion (array, complain);
4110 : 3444026 : tree ind = cp_default_conversion (idx, complain);
4111 : :
4112 : 3444026 : if (!processing_template_decl
4113 : 3444026 : && !first && flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
4114 : 91683 : ar = first = save_expr (ar);
4115 : :
4116 : : /* Put the integer in IND to simplify error checking. */
4117 : 3444026 : if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4118 : 57 : std::swap (ar, ind);
4119 : :
4120 : 3444026 : if (ar == error_mark_node || ind == error_mark_node)
4121 : : return error_mark_node;
4122 : :
4123 : 3444026 : if (!TYPE_PTR_P (TREE_TYPE (ar)))
4124 : : {
4125 : 22 : if (complain & tf_error)
4126 : 3 : error_at (loc, "subscripted value is neither array nor pointer");
4127 : 22 : return error_mark_node;
4128 : : }
4129 : 3444004 : if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4130 : : {
4131 : 0 : if (complain & tf_error)
4132 : 0 : error_at (loc, "array subscript is not an integer");
4133 : 0 : return error_mark_node;
4134 : : }
4135 : :
4136 : 3444004 : warn_array_subscript_with_type_char (loc, idx);
4137 : :
4138 : 3444004 : ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4139 : 3444004 : if (first)
4140 : 91683 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4141 : 3444004 : ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4142 : 3444004 : protected_set_expr_location (ret, loc);
4143 : 3444004 : if (non_lvalue)
4144 : 0 : ret = non_lvalue_loc (loc, ret);
4145 : : return ret;
4146 : : }
4147 : : }
4148 : :
4149 : : /* Entry point for Obj-C++. */
4150 : :
4151 : : tree
4152 : 3446227 : build_array_ref (location_t loc, tree array, tree idx)
4153 : : {
4154 : 3446227 : return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4155 : : }
4156 : :
4157 : : /* Resolve a pointer to member function. INSTANCE is the object
4158 : : instance to use, if the member points to a virtual member.
4159 : :
4160 : : This used to avoid checking for virtual functions if basetype
4161 : : has no virtual functions, according to an earlier ANSI draft.
4162 : : With the final ISO C++ rules, such an optimization is
4163 : : incorrect: A pointer to a derived member can be static_cast
4164 : : to pointer-to-base-member, as long as the dynamic object
4165 : : later has the right member. So now we only do this optimization
4166 : : when we know the dynamic type of the object. */
4167 : :
4168 : : tree
4169 : 87126 : get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4170 : : tsubst_flags_t complain)
4171 : : {
4172 : 87126 : if (TREE_CODE (function) == OFFSET_REF)
4173 : 0 : function = TREE_OPERAND (function, 1);
4174 : :
4175 : 87126 : if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4176 : : {
4177 : 87126 : tree idx, delta, e1, e2, e3, vtbl;
4178 : 87126 : bool nonvirtual;
4179 : 87126 : tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4180 : 87126 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4181 : :
4182 : 87126 : tree instance_ptr = *instance_ptrptr;
4183 : 87126 : tree instance_save_expr = 0;
4184 : 87126 : if (instance_ptr == error_mark_node)
4185 : : {
4186 : 0 : if (TREE_CODE (function) == PTRMEM_CST)
4187 : : {
4188 : : /* Extracting the function address from a pmf is only
4189 : : allowed with -Wno-pmf-conversions. It only works for
4190 : : pmf constants. */
4191 : 0 : e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4192 : 0 : e1 = convert (fntype, e1);
4193 : 0 : return e1;
4194 : : }
4195 : : else
4196 : : {
4197 : 0 : if (complain & tf_error)
4198 : 0 : error ("object missing in use of %qE", function);
4199 : 0 : return error_mark_node;
4200 : : }
4201 : : }
4202 : :
4203 : : /* True if we know that the dynamic type of the object doesn't have
4204 : : virtual functions, so we can assume the PFN field is a pointer. */
4205 : 87126 : nonvirtual = (COMPLETE_TYPE_P (basetype)
4206 : 87084 : && !TYPE_POLYMORPHIC_P (basetype)
4207 : 117023 : && resolves_to_fixed_type_p (instance_ptr, 0));
4208 : :
4209 : : /* If we don't really have an object (i.e. in an ill-formed
4210 : : conversion from PMF to pointer), we can't resolve virtual
4211 : : functions anyway. */
4212 : 86864 : if (!nonvirtual && is_dummy_object (instance_ptr))
4213 : : nonvirtual = true;
4214 : :
4215 : : /* Use force_target_expr even when instance_ptr doesn't have
4216 : : side-effects, unless it is a simple decl or constant, so
4217 : : that we don't ubsan instrument the expression multiple times.
4218 : : Don't use save_expr, as save_expr can avoid building a SAVE_EXPR
4219 : : and building a SAVE_EXPR manually can be optimized away during
4220 : : cp_fold. See PR116449 and PR117259. */
4221 : 87126 : if (TREE_SIDE_EFFECTS (instance_ptr)
4222 : 87126 : || (!nonvirtual
4223 : 29583 : && !DECL_P (instance_ptr)
4224 : 29583 : && !TREE_CONSTANT (instance_ptr)))
4225 : 86748 : instance_ptr = instance_save_expr
4226 : 86748 : = get_internal_target_expr (instance_ptr);
4227 : :
4228 : : /* See above comment. */
4229 : 87126 : if (TREE_SIDE_EFFECTS (function)
4230 : 87126 : || (!nonvirtual
4231 : 48562 : && !DECL_P (function)
4232 : 48550 : && !TREE_CONSTANT (function)))
4233 : 86649 : function = get_internal_target_expr (function);
4234 : :
4235 : : /* Start by extracting all the information from the PMF itself. */
4236 : 87126 : e3 = pfn_from_ptrmemfunc (function);
4237 : 87126 : delta = delta_from_ptrmemfunc (function);
4238 : 87126 : idx = build1 (NOP_EXPR, vtable_index_type, e3);
4239 : 87126 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4240 : : {
4241 : 87126 : int flag_sanitize_save;
4242 : 87126 : case ptrmemfunc_vbit_in_pfn:
4243 : 87126 : e1 = cp_build_binary_op (input_location,
4244 : : BIT_AND_EXPR, idx, integer_one_node,
4245 : : complain);
4246 : 87126 : idx = cp_build_binary_op (input_location,
4247 : : MINUS_EXPR, idx, integer_one_node,
4248 : : complain);
4249 : 87126 : if (idx == error_mark_node)
4250 : : return error_mark_node;
4251 : 87126 : break;
4252 : :
4253 : : case ptrmemfunc_vbit_in_delta:
4254 : : e1 = cp_build_binary_op (input_location,
4255 : : BIT_AND_EXPR, delta, integer_one_node,
4256 : : complain);
4257 : : /* Don't instrument the RSHIFT_EXPR we're about to create because
4258 : : we're going to use DELTA number of times, and that wouldn't play
4259 : : well with SAVE_EXPRs therein. */
4260 : : flag_sanitize_save = flag_sanitize;
4261 : : flag_sanitize = 0;
4262 : : delta = cp_build_binary_op (input_location,
4263 : : RSHIFT_EXPR, delta, integer_one_node,
4264 : : complain);
4265 : : flag_sanitize = flag_sanitize_save;
4266 : : if (delta == error_mark_node)
4267 : : return error_mark_node;
4268 : : break;
4269 : :
4270 : : default:
4271 : : gcc_unreachable ();
4272 : : }
4273 : :
4274 : 87126 : if (e1 == error_mark_node)
4275 : : return error_mark_node;
4276 : :
4277 : : /* Convert down to the right base before using the instance. A
4278 : : special case is that in a pointer to member of class C, C may
4279 : : be incomplete. In that case, the function will of course be
4280 : : a member of C, and no conversion is required. In fact,
4281 : : lookup_base will fail in that case, because incomplete
4282 : : classes do not have BINFOs. */
4283 : 87126 : if (!same_type_ignoring_top_level_qualifiers_p
4284 : 87126 : (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4285 : : {
4286 : 81 : basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4287 : : basetype, ba_check, NULL, complain);
4288 : 81 : instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4289 : : 1, complain);
4290 : 81 : if (instance_ptr == error_mark_node)
4291 : : return error_mark_node;
4292 : : }
4293 : : /* ...and then the delta in the PMF. */
4294 : 87126 : instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4295 : :
4296 : : /* Hand back the adjusted 'this' argument to our caller. */
4297 : 87126 : *instance_ptrptr = instance_ptr;
4298 : :
4299 : 87126 : if (nonvirtual)
4300 : : /* Now just return the pointer. */
4301 : : return e3;
4302 : :
4303 : : /* Next extract the vtable pointer from the object. */
4304 : 86846 : vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4305 : : instance_ptr);
4306 : 86846 : vtbl = cp_build_fold_indirect_ref (vtbl);
4307 : 86846 : if (vtbl == error_mark_node)
4308 : : return error_mark_node;
4309 : :
4310 : : /* Finally, extract the function pointer from the vtable. */
4311 : 86846 : e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4312 : 86846 : e2 = cp_build_fold_indirect_ref (e2);
4313 : 86846 : if (e2 == error_mark_node)
4314 : : return error_mark_node;
4315 : 86846 : TREE_CONSTANT (e2) = 1;
4316 : :
4317 : : /* When using function descriptors, the address of the
4318 : : vtable entry is treated as a function pointer. */
4319 : 86846 : if (TARGET_VTABLE_USES_DESCRIPTORS)
4320 : : e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4321 : : cp_build_addr_expr (e2, complain));
4322 : :
4323 : 86846 : e2 = fold_convert (TREE_TYPE (e3), e2);
4324 : 86846 : e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4325 : 86846 : if (e1 == error_mark_node)
4326 : : return error_mark_node;
4327 : :
4328 : : /* Make sure this doesn't get evaluated first inside one of the
4329 : : branches of the COND_EXPR. */
4330 : 86846 : if (instance_save_expr)
4331 : 86700 : e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4332 : : instance_save_expr, e1);
4333 : :
4334 : : function = e1;
4335 : : }
4336 : : return function;
4337 : : }
4338 : :
4339 : : /* Used by the C-common bits. */
4340 : : tree
4341 : 0 : build_function_call (location_t /*loc*/,
4342 : : tree function, tree params)
4343 : : {
4344 : 0 : return cp_build_function_call (function, params, tf_warning_or_error);
4345 : : }
4346 : :
4347 : : /* Used by the C-common bits. */
4348 : : tree
4349 : 235617 : build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4350 : : tree function, vec<tree, va_gc> *params,
4351 : : vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4352 : : {
4353 : 235617 : vec<tree, va_gc> *orig_params = params;
4354 : 235617 : tree ret = cp_build_function_call_vec (function, ¶ms,
4355 : : tf_warning_or_error, orig_function);
4356 : :
4357 : : /* cp_build_function_call_vec can reallocate PARAMS by adding
4358 : : default arguments. That should never happen here. Verify
4359 : : that. */
4360 : 235617 : gcc_assert (params == orig_params);
4361 : :
4362 : 235617 : return ret;
4363 : : }
4364 : :
4365 : : /* Build a function call using a tree list of arguments. */
4366 : :
4367 : : static tree
4368 : 0 : cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4369 : : {
4370 : 0 : tree ret;
4371 : :
4372 : 0 : releasing_vec vec;
4373 : 0 : for (; params != NULL_TREE; params = TREE_CHAIN (params))
4374 : 0 : vec_safe_push (vec, TREE_VALUE (params));
4375 : 0 : ret = cp_build_function_call_vec (function, &vec, complain);
4376 : 0 : return ret;
4377 : 0 : }
4378 : :
4379 : : /* Build a function call using varargs. */
4380 : :
4381 : : tree
4382 : 466712 : cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4383 : : {
4384 : 466712 : va_list args;
4385 : 466712 : tree ret, t;
4386 : :
4387 : 466712 : releasing_vec vec;
4388 : 466712 : va_start (args, complain);
4389 : 1150989 : for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4390 : 684277 : vec_safe_push (vec, t);
4391 : 466712 : va_end (args);
4392 : 466712 : ret = cp_build_function_call_vec (function, &vec, complain);
4393 : 466712 : return ret;
4394 : 466712 : }
4395 : :
4396 : : /* C++ implementation of callback for use when checking param types. */
4397 : :
4398 : : bool
4399 : 18 : cp_comp_parm_types (tree wanted_type, tree actual_type)
4400 : : {
4401 : 18 : if (TREE_CODE (wanted_type) == POINTER_TYPE
4402 : 18 : && TREE_CODE (actual_type) == POINTER_TYPE)
4403 : 15 : return same_or_base_type_p (TREE_TYPE (wanted_type),
4404 : : TREE_TYPE (actual_type));
4405 : : return false;
4406 : : }
4407 : :
4408 : : /* Build a function call using a vector of arguments.
4409 : : If FUNCTION is the result of resolving an overloaded target built-in,
4410 : : ORIG_FNDECL is the original function decl, otherwise it is null.
4411 : : PARAMS may be NULL if there are no parameters. This changes the
4412 : : contents of PARAMS. */
4413 : :
4414 : : tree
4415 : 2785038 : cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4416 : : tsubst_flags_t complain, tree orig_fndecl)
4417 : : {
4418 : 2785038 : tree fntype, fndecl;
4419 : 2785038 : int is_method;
4420 : 2785038 : tree original = function;
4421 : 2785038 : int nargs;
4422 : 2785038 : tree *argarray;
4423 : 2785038 : tree parm_types;
4424 : 2785038 : vec<tree, va_gc> *allocated = NULL;
4425 : 2785038 : tree ret;
4426 : :
4427 : : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4428 : : expressions, like those used for ObjC messenger dispatches. */
4429 : 2785038 : if (params != NULL && !vec_safe_is_empty (*params))
4430 : 1625864 : function = objc_rewrite_function_call (function, (**params)[0]);
4431 : :
4432 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4433 : : Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4434 : 2785038 : if (TREE_CODE (function) == NOP_EXPR
4435 : 2785038 : && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4436 : 15 : function = TREE_OPERAND (function, 0);
4437 : :
4438 : 2785038 : if (TREE_CODE (function) == FUNCTION_DECL)
4439 : : {
4440 : 1255007 : if (!mark_used (function, complain))
4441 : 24 : return error_mark_node;
4442 : 1254983 : fndecl = function;
4443 : :
4444 : : /* Convert anything with function type to a pointer-to-function. */
4445 : 1254983 : if (DECL_MAIN_P (function))
4446 : : {
4447 : 0 : if (complain & tf_error)
4448 : 0 : pedwarn (input_location, OPT_Wpedantic,
4449 : : "ISO C++ forbids calling %<::main%> from within program");
4450 : : else
4451 : 0 : return error_mark_node;
4452 : : }
4453 : 1254983 : function = build_addr_func (function, complain);
4454 : : }
4455 : : else
4456 : : {
4457 : 1530031 : fndecl = NULL_TREE;
4458 : :
4459 : 1530031 : function = build_addr_func (function, complain);
4460 : : }
4461 : :
4462 : 2785014 : if (function == error_mark_node)
4463 : : return error_mark_node;
4464 : :
4465 : 2784912 : fntype = TREE_TYPE (function);
4466 : :
4467 : 2784912 : if (TYPE_PTRMEMFUNC_P (fntype))
4468 : : {
4469 : 15 : if (complain & tf_error)
4470 : 15 : error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4471 : : "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4472 : : original, original);
4473 : 15 : return error_mark_node;
4474 : : }
4475 : :
4476 : 5569794 : is_method = (TYPE_PTR_P (fntype)
4477 : 2784897 : && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4478 : :
4479 : 2784897 : if (!(TYPE_PTRFN_P (fntype)
4480 : 88438 : || is_method
4481 : 1385 : || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4482 : : {
4483 : 1385 : if (complain & tf_error)
4484 : : {
4485 : 165 : if (!flag_diagnostics_show_caret)
4486 : 165 : error_at (input_location,
4487 : : "%qE cannot be used as a function", original);
4488 : 0 : else if (DECL_P (original))
4489 : 0 : error_at (input_location,
4490 : : "%qD cannot be used as a function", original);
4491 : : else
4492 : 0 : error_at (input_location,
4493 : : "expression cannot be used as a function");
4494 : : }
4495 : :
4496 : 1385 : return error_mark_node;
4497 : : }
4498 : :
4499 : : /* fntype now gets the type of function pointed to. */
4500 : 2783512 : fntype = TREE_TYPE (fntype);
4501 : 2783512 : parm_types = TYPE_ARG_TYPES (fntype);
4502 : :
4503 : 2783512 : if (params == NULL)
4504 : : {
4505 : 146994 : allocated = make_tree_vector ();
4506 : 146994 : params = &allocated;
4507 : : }
4508 : :
4509 : 2783512 : nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4510 : : complain);
4511 : 2783512 : if (nargs < 0)
4512 : 1433 : return error_mark_node;
4513 : :
4514 : 2782079 : argarray = (*params)->address ();
4515 : :
4516 : : /* Check for errors in format strings and inappropriately
4517 : : null parameters. */
4518 : 2782079 : bool warned_p
4519 : 2782079 : = ((complain & tf_warning)
4520 : 2782079 : && check_function_arguments (input_location, fndecl, fntype,
4521 : : nargs, argarray, NULL,
4522 : 2782079 : cp_comp_parm_types));
4523 : :
4524 : 2782079 : ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4525 : :
4526 : 2782079 : if (warned_p)
4527 : : {
4528 : 27 : tree c = extract_call_expr (ret);
4529 : 27 : if (TREE_CODE (c) == CALL_EXPR)
4530 : 27 : suppress_warning (c, OPT_Wnonnull);
4531 : : }
4532 : :
4533 : 2782079 : if (allocated != NULL)
4534 : 146994 : release_tree_vector (allocated);
4535 : :
4536 : : return ret;
4537 : : }
4538 : :
4539 : : /* Subroutine of convert_arguments.
4540 : : Print an error message about a wrong number of arguments. */
4541 : :
4542 : : static void
4543 : 153 : error_args_num (location_t loc, tree fndecl, bool too_many_p)
4544 : : {
4545 : 153 : if (fndecl)
4546 : : {
4547 : 120 : auto_diagnostic_group d;
4548 : 120 : if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4549 : : {
4550 : 0 : if (DECL_NAME (fndecl) == NULL_TREE
4551 : 0 : || (DECL_NAME (fndecl)
4552 : 0 : == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4553 : 0 : error_at (loc,
4554 : : too_many_p
4555 : : ? G_("too many arguments to constructor %q#D")
4556 : : : G_("too few arguments to constructor %q#D"),
4557 : : fndecl);
4558 : : else
4559 : 0 : error_at (loc,
4560 : : too_many_p
4561 : : ? G_("too many arguments to member function %q#D")
4562 : : : G_("too few arguments to member function %q#D"),
4563 : : fndecl);
4564 : : }
4565 : : else
4566 : 183 : error_at (loc,
4567 : : too_many_p
4568 : : ? G_("too many arguments to function %q#D")
4569 : : : G_("too few arguments to function %q#D"),
4570 : : fndecl);
4571 : 120 : if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4572 : 81 : inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4573 : 120 : }
4574 : : else
4575 : : {
4576 : 33 : if (c_dialect_objc () && objc_message_selector ())
4577 : 0 : error_at (loc,
4578 : : too_many_p
4579 : : ? G_("too many arguments to method %q#D")
4580 : : : G_("too few arguments to method %q#D"),
4581 : : objc_message_selector ());
4582 : : else
4583 : 54 : error_at (loc, too_many_p ? G_("too many arguments to function")
4584 : : : G_("too few arguments to function"));
4585 : : }
4586 : 153 : }
4587 : :
4588 : : /* Convert the actual parameter expressions in the list VALUES to the
4589 : : types in the list TYPELIST. The converted expressions are stored
4590 : : back in the VALUES vector.
4591 : : If parmdecls is exhausted, or when an element has NULL as its type,
4592 : : perform the default conversions.
4593 : :
4594 : : NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4595 : :
4596 : : This is also where warnings about wrong number of args are generated.
4597 : :
4598 : : Returns the actual number of arguments processed (which might be less
4599 : : than the length of the vector), or -1 on error.
4600 : :
4601 : : In C++, unspecified trailing parameters can be filled in with their
4602 : : default arguments, if such were specified. Do so here. */
4603 : :
4604 : : static int
4605 : 2783512 : convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4606 : : int flags, tsubst_flags_t complain)
4607 : : {
4608 : 2783512 : tree typetail;
4609 : 2783512 : unsigned int i;
4610 : :
4611 : : /* Argument passing is always copy-initialization. */
4612 : 2783512 : flags |= LOOKUP_ONLYCONVERTING;
4613 : :
4614 : 6460892 : for (i = 0, typetail = typelist;
4615 : 6460892 : i < vec_safe_length (*values);
4616 : : i++)
4617 : : {
4618 : 7357018 : tree type = typetail ? TREE_VALUE (typetail) : 0;
4619 : 3678732 : tree val = (**values)[i];
4620 : :
4621 : 3678732 : if (val == error_mark_node || type == error_mark_node)
4622 : : return -1;
4623 : :
4624 : 3678726 : if (type == void_type_node)
4625 : : {
4626 : 190 : if (complain & tf_error)
4627 : : {
4628 : 69 : error_args_num (input_location, fndecl, /*too_many_p=*/true);
4629 : 69 : return i;
4630 : : }
4631 : : else
4632 : : return -1;
4633 : : }
4634 : :
4635 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4636 : : Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4637 : 3678536 : if (TREE_CODE (val) == NOP_EXPR
4638 : 854281 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4639 : 3678554 : && (type == 0 || !TYPE_REF_P (type)))
4640 : 18 : val = TREE_OPERAND (val, 0);
4641 : :
4642 : 3678536 : if (type == 0 || !TYPE_REF_P (type))
4643 : : {
4644 : 3463917 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4645 : 3463917 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4646 : 10680 : val = decay_conversion (val, complain);
4647 : : }
4648 : :
4649 : 3678536 : if (val == error_mark_node)
4650 : : return -1;
4651 : :
4652 : 3678536 : if (type != 0)
4653 : : {
4654 : : /* Formal parm type is specified by a function prototype. */
4655 : 3678090 : tree parmval;
4656 : :
4657 : 3678090 : if (!COMPLETE_TYPE_P (complete_type (type)))
4658 : : {
4659 : 18 : if (complain & tf_error)
4660 : : {
4661 : 12 : location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4662 : 12 : if (fndecl)
4663 : : {
4664 : 12 : auto_diagnostic_group d;
4665 : 12 : error_at (loc,
4666 : : "parameter %P of %qD has incomplete type %qT",
4667 : : i, fndecl, type);
4668 : 12 : inform (get_fndecl_argument_location (fndecl, i),
4669 : : " declared here");
4670 : 12 : }
4671 : : else
4672 : 0 : error_at (loc, "parameter %P has incomplete type %qT", i,
4673 : : type);
4674 : : }
4675 : 18 : parmval = error_mark_node;
4676 : : }
4677 : : else
4678 : : {
4679 : 3678072 : parmval = convert_for_initialization
4680 : 3678072 : (NULL_TREE, type, val, flags,
4681 : : ICR_ARGPASS, fndecl, i, complain);
4682 : 3678072 : parmval = convert_for_arg_passing (type, parmval, complain);
4683 : : }
4684 : :
4685 : 3678090 : if (parmval == error_mark_node)
4686 : : return -1;
4687 : :
4688 : 3676934 : (**values)[i] = parmval;
4689 : : }
4690 : : else
4691 : : {
4692 : 446 : int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4693 : 21 : if (magic)
4694 : : {
4695 : : /* Don't truncate excess precision to the semantic type. */
4696 : 0 : if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4697 : 0 : val = TREE_OPERAND (val, 0);
4698 : : /* Don't do ellipsis conversion for __built_in_constant_p
4699 : : as this will result in spurious errors for non-trivial
4700 : : types. */
4701 : 0 : val = require_complete_type (val, complain);
4702 : : }
4703 : : else
4704 : 446 : val = convert_arg_to_ellipsis (val, complain);
4705 : :
4706 : 446 : (**values)[i] = val;
4707 : : }
4708 : :
4709 : 3677380 : if (typetail)
4710 : 3676934 : typetail = TREE_CHAIN (typetail);
4711 : : }
4712 : :
4713 : 2782160 : if (typetail != 0 && typetail != void_list_node)
4714 : : {
4715 : : /* See if there are default arguments that can be used. Because
4716 : : we hold default arguments in the FUNCTION_TYPE (which is so
4717 : : wrong), we can see default parameters here from deduced
4718 : : contexts (and via typeof) for indirect function calls.
4719 : : Fortunately we know whether we have a function decl to
4720 : : provide default arguments in a language conformant
4721 : : manner. */
4722 : 63 : if (fndecl && TREE_PURPOSE (typetail)
4723 : 153 : && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4724 : : {
4725 : 6 : for (; typetail != void_list_node; ++i)
4726 : : {
4727 : : /* After DR777, with explicit template args we can end up with a
4728 : : default argument followed by no default argument. */
4729 : 6 : if (!TREE_PURPOSE (typetail))
4730 : : break;
4731 : 3 : tree parmval
4732 : 3 : = convert_default_arg (TREE_VALUE (typetail),
4733 : 3 : TREE_PURPOSE (typetail),
4734 : 3 : fndecl, i, complain);
4735 : :
4736 : 3 : if (parmval == error_mark_node)
4737 : 0 : return -1;
4738 : :
4739 : 3 : vec_safe_push (*values, parmval);
4740 : 3 : typetail = TREE_CHAIN (typetail);
4741 : : /* ends with `...'. */
4742 : 3 : if (typetail == NULL_TREE)
4743 : : break;
4744 : : }
4745 : : }
4746 : :
4747 : 150 : if (typetail && typetail != void_list_node)
4748 : : {
4749 : 150 : if (complain & tf_error)
4750 : 84 : error_args_num (input_location, fndecl, /*too_many_p=*/false);
4751 : 150 : return -1;
4752 : : }
4753 : : }
4754 : :
4755 : 2782010 : return (int) i;
4756 : : }
4757 : :
4758 : : /* Build a binary-operation expression, after performing default
4759 : : conversions on the operands. CODE is the kind of expression to
4760 : : build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4761 : : are the tree codes which correspond to ARG1 and ARG2 when issuing
4762 : : warnings about possibly misplaced parentheses. They may differ
4763 : : from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4764 : : folding (e.g., if the parser sees "a | 1 + 1", it may call this
4765 : : routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4766 : : To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4767 : : ARG2_CODE as ERROR_MARK. */
4768 : :
4769 : : tree
4770 : 185969344 : build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4771 : : enum tree_code arg1_code, tree arg2,
4772 : : enum tree_code arg2_code, tree lookups,
4773 : : tree *overload_p, tsubst_flags_t complain)
4774 : : {
4775 : 185969344 : tree orig_arg1;
4776 : 185969344 : tree orig_arg2;
4777 : 185969344 : tree expr;
4778 : 185969344 : tree overload = NULL_TREE;
4779 : :
4780 : 185969344 : orig_arg1 = arg1;
4781 : 185969344 : orig_arg2 = arg2;
4782 : :
4783 : 185969344 : if (processing_template_decl)
4784 : : {
4785 : 110500345 : if (type_dependent_expression_p (arg1)
4786 : 110500345 : || type_dependent_expression_p (arg2))
4787 : : {
4788 : 79114149 : expr = build_min_nt_loc (loc, code, arg1, arg2);
4789 : 79114149 : TREE_TYPE (expr)
4790 : 79114149 : = build_dependent_operator_type (lookups, code, false);
4791 : 79114149 : return expr;
4792 : : }
4793 : : }
4794 : :
4795 : 106855195 : if (code == DOTSTAR_EXPR)
4796 : 106712 : expr = build_m_component_ref (arg1, arg2, complain);
4797 : : else
4798 : 106748483 : expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4799 : : lookups, &overload, complain);
4800 : :
4801 : 106855162 : if (overload_p != NULL)
4802 : 44767767 : *overload_p = overload;
4803 : :
4804 : : /* Check for cases such as x+y<<z which users are likely to
4805 : : misinterpret. But don't warn about obj << x + y, since that is a
4806 : : common idiom for I/O. */
4807 : 106855162 : if (warn_parentheses
4808 : 1090087 : && (complain & tf_warning)
4809 : 1054911 : && !processing_template_decl
4810 : 730508 : && !error_operand_p (arg1)
4811 : 730502 : && !error_operand_p (arg2)
4812 : 107585652 : && (code != LSHIFT_EXPR
4813 : 67654 : || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4814 : 729505 : warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4815 : : arg2_code, orig_arg2);
4816 : :
4817 : 106855162 : if (processing_template_decl && expr != error_mark_node)
4818 : : {
4819 : 31386005 : if (overload != NULL_TREE)
4820 : 1967811 : return (build_min_non_dep_op_overload
4821 : 1967811 : (code, expr, overload, orig_arg1, orig_arg2));
4822 : :
4823 : 29418194 : return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4824 : : }
4825 : :
4826 : : return expr;
4827 : : }
4828 : :
4829 : : /* Build and return an ARRAY_REF expression. */
4830 : :
4831 : : tree
4832 : 1739935 : build_x_array_ref (location_t loc, tree arg1, tree arg2,
4833 : : tsubst_flags_t complain)
4834 : : {
4835 : 1739935 : tree orig_arg1 = arg1;
4836 : 1739935 : tree orig_arg2 = arg2;
4837 : 1739935 : tree expr;
4838 : 1739935 : tree overload = NULL_TREE;
4839 : :
4840 : 1739935 : if (processing_template_decl)
4841 : : {
4842 : 650 : if (type_dependent_expression_p (arg1)
4843 : 650 : || type_dependent_expression_p (arg2))
4844 : 632 : return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4845 : 632 : NULL_TREE, NULL_TREE);
4846 : : }
4847 : :
4848 : 1739303 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4849 : : NULL_TREE, NULL_TREE, &overload, complain);
4850 : :
4851 : 1739303 : if (processing_template_decl && expr != error_mark_node)
4852 : : {
4853 : 15 : if (overload != NULL_TREE)
4854 : 1 : return (build_min_non_dep_op_overload
4855 : 1 : (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4856 : :
4857 : 14 : return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4858 : 14 : NULL_TREE, NULL_TREE);
4859 : : }
4860 : : return expr;
4861 : : }
4862 : :
4863 : : /* Build an OpenMP array section reference, creating an exact type for the
4864 : : resulting expression based on the element type and bounds if possible. If
4865 : : we have variable bounds, create an incomplete array type for the result
4866 : : instead. */
4867 : :
4868 : : tree
4869 : 10419 : build_omp_array_section (location_t loc, tree array_expr, tree index,
4870 : : tree length)
4871 : : {
4872 : 10419 : if (TREE_CODE (array_expr) == TYPE_DECL
4873 : 10419 : || type_dependent_expression_p (array_expr))
4874 : 274 : return build3_loc (loc, OMP_ARRAY_SECTION, NULL_TREE, array_expr, index,
4875 : 274 : length);
4876 : :
4877 : 10145 : tree type = TREE_TYPE (array_expr);
4878 : 10145 : gcc_assert (type);
4879 : 10145 : type = non_reference (type);
4880 : :
4881 : 10145 : tree sectype, eltype = TREE_TYPE (type);
4882 : :
4883 : : /* It's not an array or pointer type. Just reuse the type of the
4884 : : original expression as the type of the array section (an error will be
4885 : : raised anyway, later). */
4886 : 10145 : if (eltype == NULL_TREE)
4887 : 42 : sectype = TREE_TYPE (array_expr);
4888 : : else
4889 : : {
4890 : 10103 : tree idxtype = NULL_TREE;
4891 : :
4892 : : /* If we know the integer bounds, create an index type with exact
4893 : : low/high (or zero/length) bounds. Otherwise, create an incomplete
4894 : : array type. (This mostly only affects diagnostics.) */
4895 : 10103 : if (index != NULL_TREE
4896 : 10103 : && length != NULL_TREE
4897 : 7305 : && TREE_CODE (index) == INTEGER_CST
4898 : 6509 : && TREE_CODE (length) == INTEGER_CST)
4899 : : {
4900 : 5116 : tree low = fold_convert (sizetype, index);
4901 : 5116 : tree high = fold_convert (sizetype, length);
4902 : 5116 : high = size_binop (PLUS_EXPR, low, high);
4903 : 5116 : high = size_binop (MINUS_EXPR, high, size_one_node);
4904 : 5116 : idxtype = build_range_type (sizetype, low, high);
4905 : 5116 : }
4906 : 2846 : else if ((index == NULL_TREE || integer_zerop (index))
4907 : 3357 : && length != NULL_TREE
4908 : 6894 : && TREE_CODE (length) == INTEGER_CST)
4909 : 1334 : idxtype = build_index_type (length);
4910 : :
4911 : 10103 : sectype = build_array_type (eltype, idxtype);
4912 : : }
4913 : :
4914 : 10145 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array_expr, index,
4915 : 10145 : length);
4916 : : }
4917 : :
4918 : : /* Return whether OP is an expression of enum type cast to integer
4919 : : type. In C++ even unsigned enum types are cast to signed integer
4920 : : types. We do not want to issue warnings about comparisons between
4921 : : signed and unsigned types when one of the types is an enum type.
4922 : : Those warnings are always false positives in practice. */
4923 : :
4924 : : static bool
4925 : 570270 : enum_cast_to_int (tree op)
4926 : : {
4927 : 558634 : if (CONVERT_EXPR_P (op)
4928 : 12724 : && TREE_TYPE (op) == integer_type_node
4929 : 757 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4930 : 570308 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4931 : : return true;
4932 : :
4933 : : /* The cast may have been pushed into a COND_EXPR. */
4934 : 570240 : if (TREE_CODE (op) == COND_EXPR)
4935 : 757 : return (enum_cast_to_int (TREE_OPERAND (op, 1))
4936 : 757 : || enum_cast_to_int (TREE_OPERAND (op, 2)));
4937 : :
4938 : : return false;
4939 : : }
4940 : :
4941 : : /* For the c-common bits. */
4942 : : tree
4943 : 3739755 : build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4944 : : bool /*convert_p*/)
4945 : : {
4946 : 3739755 : return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4947 : : }
4948 : :
4949 : : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4950 : : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4951 : :
4952 : : static tree
4953 : 6551 : build_vec_cmp (tree_code code, tree type,
4954 : : tree arg0, tree arg1)
4955 : : {
4956 : 6551 : tree zero_vec = build_zero_cst (type);
4957 : 6551 : tree minus_one_vec = build_minus_one_cst (type);
4958 : 6551 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
4959 : 6551 : tree cmp = build2 (code, cmp_type, arg0, arg1);
4960 : 6551 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4961 : : }
4962 : :
4963 : : /* Possibly warn about an address never being NULL. */
4964 : :
4965 : : static void
4966 : 1502111 : warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4967 : : {
4968 : : /* Prevent warnings issued for macro expansion. */
4969 : 1502111 : if (!warn_address
4970 : 40279 : || (complain & tf_warning) == 0
4971 : 24035 : || c_inhibit_evaluation_warnings != 0
4972 : 23995 : || from_macro_expansion_at (location)
4973 : 1519494 : || warning_suppressed_p (op, OPT_Waddress))
4974 : 1485051 : return;
4975 : :
4976 : 17350 : tree cop = fold_for_warn (op);
4977 : :
4978 : 17350 : if (TREE_CODE (cop) == NON_LVALUE_EXPR)
4979 : : /* Unwrap the expression for C++ 98. */
4980 : 1 : cop = TREE_OPERAND (cop, 0);
4981 : :
4982 : 17350 : if (TREE_CODE (cop) == PTRMEM_CST)
4983 : : {
4984 : : /* The address of a nonstatic data member is never null. */
4985 : 30 : warning_at (location, OPT_Waddress,
4986 : : "the address %qE will never be NULL",
4987 : : cop);
4988 : 30 : return;
4989 : : }
4990 : :
4991 : 17320 : if (TREE_CODE (cop) == NOP_EXPR)
4992 : : {
4993 : : /* Allow casts to intptr_t to suppress the warning. */
4994 : 1081 : tree type = TREE_TYPE (cop);
4995 : 1081 : if (TREE_CODE (type) == INTEGER_TYPE)
4996 : : return;
4997 : :
4998 : 1081 : STRIP_NOPS (cop);
4999 : : }
5000 : :
5001 : 17320 : auto_diagnostic_group d;
5002 : 17320 : bool warned = false;
5003 : 17320 : if (TREE_CODE (cop) == ADDR_EXPR)
5004 : : {
5005 : 789 : cop = TREE_OPERAND (cop, 0);
5006 : :
5007 : : /* Set to true in the loop below if OP dereferences its operand.
5008 : : In such a case the ultimate target need not be a decl for
5009 : : the null [in]equality test to be necessarily constant. */
5010 : 789 : bool deref = false;
5011 : :
5012 : : /* Get the outermost array or object, or member. */
5013 : 960 : while (handled_component_p (cop))
5014 : : {
5015 : 270 : if (TREE_CODE (cop) == COMPONENT_REF)
5016 : : {
5017 : : /* Get the member (its address is never null). */
5018 : 99 : cop = TREE_OPERAND (cop, 1);
5019 : 99 : break;
5020 : : }
5021 : :
5022 : : /* Get the outer array/object to refer to in the warning. */
5023 : 171 : cop = TREE_OPERAND (cop, 0);
5024 : 171 : deref = true;
5025 : : }
5026 : :
5027 : 639 : if ((!deref && !decl_with_nonnull_addr_p (cop))
5028 : 631 : || from_macro_expansion_at (location)
5029 : 1420 : || warning_suppressed_p (cop, OPT_Waddress))
5030 : 158 : return;
5031 : :
5032 : 631 : warned = warning_at (location, OPT_Waddress,
5033 : : "the address of %qD will never be NULL", cop);
5034 : 631 : op = cop;
5035 : : }
5036 : 16531 : else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
5037 : : {
5038 : : /* Adding zero to the null pointer is well-defined in C++. When
5039 : : the offset is unknown (i.e., not a constant) warn anyway since
5040 : : it's less likely that the pointer operand is null than not. */
5041 : 102 : tree off = TREE_OPERAND (cop, 1);
5042 : 102 : if (!integer_zerop (off)
5043 : 102 : && !warning_suppressed_p (cop, OPT_Waddress))
5044 : : {
5045 : 102 : tree base = TREE_OPERAND (cop, 0);
5046 : 102 : STRIP_NOPS (base);
5047 : 102 : if (TYPE_REF_P (TREE_TYPE (base)))
5048 : 12 : warning_at (location, OPT_Waddress, "the compiler can assume that "
5049 : : "the address of %qE will never be NULL", base);
5050 : : else
5051 : 90 : warning_at (location, OPT_Waddress, "comparing the result of "
5052 : : "pointer addition %qE and NULL", cop);
5053 : : }
5054 : 102 : return;
5055 : : }
5056 : 15629 : else if (CONVERT_EXPR_P (op)
5057 : 16491 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
5058 : : {
5059 : 62 : STRIP_NOPS (op);
5060 : :
5061 : 62 : if (TREE_CODE (op) == COMPONENT_REF)
5062 : 24 : op = TREE_OPERAND (op, 1);
5063 : :
5064 : 62 : if (DECL_P (op))
5065 : 62 : warned = warning_at (location, OPT_Waddress,
5066 : : "the compiler can assume that the address of "
5067 : : "%qD will never be NULL", op);
5068 : : }
5069 : :
5070 : 693 : if (warned && DECL_P (op))
5071 : 657 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
5072 : 17320 : }
5073 : :
5074 : : /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5075 : : the other operand is of a different enumeration type or a floating-point
5076 : : type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5077 : : code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5078 : : and LOC is the location for the whole binary expression.
5079 : : For C++26 this is ill-formed rather than deprecated.
5080 : : Return true for SFINAE errors.
5081 : : TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5082 : :
5083 : : static bool
5084 : 89343171 : do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5085 : : tree type1, tsubst_flags_t complain)
5086 : : {
5087 : 89343171 : if (TREE_CODE (type0) == ENUMERAL_TYPE
5088 : 2581090 : && TREE_CODE (type1) == ENUMERAL_TYPE
5089 : 91406716 : && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
5090 : : {
5091 : 198 : if (cxx_dialect >= cxx26)
5092 : : {
5093 : 60 : if ((complain & tf_warning_or_error) == 0)
5094 : : return true;
5095 : : }
5096 : 138 : else if ((complain & tf_warning) == 0)
5097 : : return false;
5098 : : /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5099 : : Otherwise, warn if -Wenum-conversion is on. */
5100 : 190 : enum opt_code opt;
5101 : 190 : if (warn_deprecated_enum_enum_conv)
5102 : : opt = OPT_Wdeprecated_enum_enum_conversion;
5103 : 97 : else if (warn_enum_conversion)
5104 : : opt = OPT_Wenum_conversion;
5105 : : else
5106 : : return false;
5107 : :
5108 : 115 : switch (code)
5109 : : {
5110 : : case GT_EXPR:
5111 : : case LT_EXPR:
5112 : : case GE_EXPR:
5113 : : case LE_EXPR:
5114 : : case EQ_EXPR:
5115 : : case NE_EXPR:
5116 : : /* Comparisons are handled by -Wenum-compare. */
5117 : : return false;
5118 : : case SPACESHIP_EXPR:
5119 : : /* This is invalid, don't warn. */
5120 : : return false;
5121 : 17 : case BIT_AND_EXPR:
5122 : 17 : case BIT_IOR_EXPR:
5123 : 17 : case BIT_XOR_EXPR:
5124 : 17 : if (cxx_dialect >= cxx26)
5125 : 5 : pedwarn (loc, opt, "bitwise operation between different "
5126 : : "enumeration types %qT and %qT", type0, type1);
5127 : : else
5128 : 12 : warning_at (loc, opt, "bitwise operation between different "
5129 : : "enumeration types %qT and %qT is deprecated",
5130 : : type0, type1);
5131 : 17 : return false;
5132 : 41 : default:
5133 : 41 : if (cxx_dialect >= cxx26)
5134 : 12 : pedwarn (loc, opt, "arithmetic between different enumeration "
5135 : : "types %qT and %qT", type0, type1);
5136 : : else
5137 : 29 : warning_at (loc, opt, "arithmetic between different enumeration "
5138 : : "types %qT and %qT is deprecated", type0, type1);
5139 : 41 : return false;
5140 : : }
5141 : : }
5142 : 89342973 : else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5143 : 2580892 : && SCALAR_FLOAT_TYPE_P (type1))
5144 : 89342896 : || (SCALAR_FLOAT_TYPE_P (type0)
5145 : 31743025 : && TREE_CODE (type1) == ENUMERAL_TYPE))
5146 : : {
5147 : 154 : if (cxx_dialect >= cxx26)
5148 : : {
5149 : 46 : if ((complain & tf_warning_or_error) == 0)
5150 : : return true;
5151 : : }
5152 : 108 : else if ((complain & tf_warning) == 0)
5153 : : return false;
5154 : 142 : const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5155 : : /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5156 : : Otherwise, warn if -Wenum-conversion is on. */
5157 : 142 : enum opt_code opt;
5158 : 142 : if (warn_deprecated_enum_float_conv)
5159 : : opt = OPT_Wdeprecated_enum_float_conversion;
5160 : 75 : else if (warn_enum_conversion)
5161 : : opt = OPT_Wenum_conversion;
5162 : : else
5163 : : return false;
5164 : :
5165 : 84 : switch (code)
5166 : : {
5167 : 34 : case GT_EXPR:
5168 : 34 : case LT_EXPR:
5169 : 34 : case GE_EXPR:
5170 : 34 : case LE_EXPR:
5171 : 34 : case EQ_EXPR:
5172 : 34 : case NE_EXPR:
5173 : 34 : if (enum_first_p && cxx_dialect >= cxx26)
5174 : 5 : pedwarn (loc, opt, "comparison of enumeration type %qT with "
5175 : : "floating-point type %qT", type0, type1);
5176 : 29 : else if (cxx_dialect >= cxx26)
5177 : 5 : pedwarn (loc, opt, "comparison of floating-point type %qT "
5178 : : "with enumeration type %qT", type0, type1);
5179 : 24 : else if (enum_first_p)
5180 : 12 : warning_at (loc, opt, "comparison of enumeration type %qT with "
5181 : : "floating-point type %qT is deprecated",
5182 : : type0, type1);
5183 : : else
5184 : 12 : warning_at (loc, opt, "comparison of floating-point type %qT "
5185 : : "with enumeration type %qT is deprecated",
5186 : : type0, type1);
5187 : 34 : return false;
5188 : : case SPACESHIP_EXPR:
5189 : : /* This is invalid, don't warn. */
5190 : : return false;
5191 : 38 : default:
5192 : 38 : if (enum_first_p && cxx_dialect >= cxx26)
5193 : 5 : pedwarn (loc, opt, "arithmetic between enumeration type %qT "
5194 : : "and floating-point type %qT", type0, type1);
5195 : 33 : else if (cxx_dialect >= cxx26)
5196 : 6 : pedwarn (loc, opt, "arithmetic between floating-point type %qT "
5197 : : "and enumeration type %qT", type0, type1);
5198 : 27 : else if (enum_first_p)
5199 : 12 : warning_at (loc, opt, "arithmetic between enumeration type %qT "
5200 : : "and floating-point type %qT is deprecated",
5201 : : type0, type1);
5202 : : else
5203 : 15 : warning_at (loc, opt, "arithmetic between floating-point type %qT "
5204 : : "and enumeration type %qT is deprecated",
5205 : : type0, type1);
5206 : 38 : return false;
5207 : : }
5208 : : }
5209 : : return false;
5210 : : }
5211 : :
5212 : : /* Build a binary-operation expression without default conversions.
5213 : : CODE is the kind of expression to build.
5214 : : LOCATION is the location_t of the operator in the source code.
5215 : : This function differs from `build' in several ways:
5216 : : the data type of the result is computed and recorded in it,
5217 : : warnings are generated if arg data types are invalid,
5218 : : special handling for addition and subtraction of pointers is known,
5219 : : and some optimization is done (operations on narrow ints
5220 : : are done in the narrower type when that gives the same result).
5221 : : Constant folding is also done before the result is returned.
5222 : :
5223 : : Note that the operands will never have enumeral types
5224 : : because either they have just had the default conversions performed
5225 : : or they have both just been converted to some other type in which
5226 : : the arithmetic is to be done.
5227 : :
5228 : : C++: must do special pointer arithmetic when implementing
5229 : : multiple inheritance, and deal with pointer to member functions. */
5230 : :
5231 : : tree
5232 : 116769478 : cp_build_binary_op (const op_location_t &location,
5233 : : enum tree_code code, tree orig_op0, tree orig_op1,
5234 : : tsubst_flags_t complain)
5235 : : {
5236 : 116769478 : tree op0, op1;
5237 : 116769478 : enum tree_code code0, code1;
5238 : 116769478 : tree type0, type1, orig_type0, orig_type1;
5239 : 116769478 : const char *invalid_op_diag;
5240 : :
5241 : : /* Expression code to give to the expression when it is built.
5242 : : Normally this is CODE, which is what the caller asked for,
5243 : : but in some special cases we change it. */
5244 : 116769478 : enum tree_code resultcode = code;
5245 : :
5246 : : /* Data type in which the computation is to be performed.
5247 : : In the simplest cases this is the common type of the arguments. */
5248 : 116769478 : tree result_type = NULL_TREE;
5249 : :
5250 : : /* When the computation is in excess precision, the type of the
5251 : : final EXCESS_PRECISION_EXPR. */
5252 : 116769478 : tree semantic_result_type = NULL;
5253 : :
5254 : : /* Nonzero means operands have already been type-converted
5255 : : in whatever way is necessary.
5256 : : Zero means they need to be converted to RESULT_TYPE. */
5257 : 116769478 : int converted = 0;
5258 : :
5259 : : /* Nonzero means create the expression with this type, rather than
5260 : : RESULT_TYPE. */
5261 : 116769478 : tree build_type = 0;
5262 : :
5263 : : /* Nonzero means after finally constructing the expression
5264 : : convert it to this type. */
5265 : 116769478 : tree final_type = 0;
5266 : :
5267 : 116769478 : tree result;
5268 : :
5269 : : /* Nonzero if this is an operation like MIN or MAX which can
5270 : : safely be computed in short if both args are promoted shorts.
5271 : : Also implies COMMON.
5272 : : -1 indicates a bitwise operation; this makes a difference
5273 : : in the exact conditions for when it is safe to do the operation
5274 : : in a narrower mode. */
5275 : 116769478 : int shorten = 0;
5276 : :
5277 : : /* Nonzero if this is a comparison operation;
5278 : : if both args are promoted shorts, compare the original shorts.
5279 : : Also implies COMMON. */
5280 : 116769478 : int short_compare = 0;
5281 : :
5282 : : /* Nonzero if this is a right-shift operation, which can be computed on the
5283 : : original short and then promoted if the operand is a promoted short. */
5284 : 116769478 : int short_shift = 0;
5285 : :
5286 : : /* Nonzero means set RESULT_TYPE to the common type of the args. */
5287 : 116769478 : int common = 0;
5288 : :
5289 : : /* True if both operands have arithmetic type. */
5290 : 116769478 : bool arithmetic_types_p;
5291 : :
5292 : : /* Remember whether we're doing / or %. */
5293 : 116769478 : bool doing_div_or_mod = false;
5294 : :
5295 : : /* Remember whether we're doing << or >>. */
5296 : 116769478 : bool doing_shift = false;
5297 : :
5298 : : /* Tree holding instrumentation expression. */
5299 : 116769478 : tree instrument_expr = NULL_TREE;
5300 : :
5301 : : /* True means this is an arithmetic operation that may need excess
5302 : : precision. */
5303 : 116769478 : bool may_need_excess_precision;
5304 : :
5305 : : /* Apply default conversions. */
5306 : 116769478 : op0 = resolve_nondeduced_context (orig_op0, complain);
5307 : 116769478 : op1 = resolve_nondeduced_context (orig_op1, complain);
5308 : :
5309 : 116769478 : if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5310 : 106293417 : || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5311 : 103784713 : || code == TRUTH_XOR_EXPR)
5312 : : {
5313 : 12984765 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5314 : 12984747 : op0 = decay_conversion (op0, complain);
5315 : 12984765 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5316 : 12984765 : op1 = decay_conversion (op1, complain);
5317 : : }
5318 : : else
5319 : : {
5320 : 103784713 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5321 : 103784698 : op0 = cp_default_conversion (op0, complain);
5322 : 103784713 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5323 : 103784710 : op1 = cp_default_conversion (op1, complain);
5324 : : }
5325 : :
5326 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5327 : 123780452 : STRIP_TYPE_NOPS (op0);
5328 : 142451553 : STRIP_TYPE_NOPS (op1);
5329 : :
5330 : : /* DTRT if one side is an overloaded function, but complain about it. */
5331 : 116769478 : if (type_unknown_p (op0))
5332 : : {
5333 : 9 : tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5334 : 9 : if (t != error_mark_node)
5335 : : {
5336 : 0 : if (complain & tf_error)
5337 : 0 : permerror (location,
5338 : : "assuming cast to type %qT from overloaded function",
5339 : 0 : TREE_TYPE (t));
5340 : : op0 = t;
5341 : : }
5342 : : }
5343 : 116769478 : if (type_unknown_p (op1))
5344 : : {
5345 : 3 : tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5346 : 3 : if (t != error_mark_node)
5347 : : {
5348 : 3 : if (complain & tf_error)
5349 : 3 : permerror (location,
5350 : : "assuming cast to type %qT from overloaded function",
5351 : 3 : TREE_TYPE (t));
5352 : : op1 = t;
5353 : : }
5354 : : }
5355 : :
5356 : 116769478 : orig_type0 = type0 = TREE_TYPE (op0);
5357 : 116769478 : orig_type1 = type1 = TREE_TYPE (op1);
5358 : 116769478 : tree non_ep_op0 = op0;
5359 : 116769478 : tree non_ep_op1 = op1;
5360 : :
5361 : : /* The expression codes of the data types of the arguments tell us
5362 : : whether the arguments are integers, floating, pointers, etc. */
5363 : 116769478 : code0 = TREE_CODE (type0);
5364 : 116769478 : code1 = TREE_CODE (type1);
5365 : :
5366 : : /* If an error was already reported for one of the arguments,
5367 : : avoid reporting another error. */
5368 : 116769478 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5369 : 100 : return error_mark_node;
5370 : :
5371 : 233538756 : if ((invalid_op_diag
5372 : 116769378 : = targetm.invalid_binary_op (code, type0, type1)))
5373 : : {
5374 : 0 : if (complain & tf_error)
5375 : : {
5376 : 0 : if (code0 == REAL_TYPE
5377 : 0 : && code1 == REAL_TYPE
5378 : 0 : && (extended_float_type_p (type0)
5379 : 0 : || extended_float_type_p (type1))
5380 : 0 : && cp_compare_floating_point_conversion_ranks (type0,
5381 : : type1) == 3)
5382 : : {
5383 : 0 : rich_location richloc (line_table, location);
5384 : 0 : binary_op_error (&richloc, code, type0, type1);
5385 : 0 : }
5386 : : else
5387 : 0 : error (invalid_op_diag);
5388 : : }
5389 : 0 : return error_mark_node;
5390 : : }
5391 : :
5392 : 116769378 : switch (code)
5393 : : {
5394 : : case PLUS_EXPR:
5395 : : case MINUS_EXPR:
5396 : : case MULT_EXPR:
5397 : : case TRUNC_DIV_EXPR:
5398 : : case CEIL_DIV_EXPR:
5399 : : case FLOOR_DIV_EXPR:
5400 : : case ROUND_DIV_EXPR:
5401 : : case EXACT_DIV_EXPR:
5402 : : may_need_excess_precision = true;
5403 : : break;
5404 : 38250973 : case EQ_EXPR:
5405 : 38250973 : case NE_EXPR:
5406 : 38250973 : case LE_EXPR:
5407 : 38250973 : case GE_EXPR:
5408 : 38250973 : case LT_EXPR:
5409 : 38250973 : case GT_EXPR:
5410 : 38250973 : case SPACESHIP_EXPR:
5411 : : /* Excess precision for implicit conversions of integers to
5412 : : floating point. */
5413 : 8283782 : may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5414 : 46528774 : || ANY_INTEGRAL_TYPE_P (type1));
5415 : : break;
5416 : : default:
5417 : 116769378 : may_need_excess_precision = false;
5418 : : break;
5419 : : }
5420 : 116769378 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5421 : : {
5422 : 18936 : op0 = TREE_OPERAND (op0, 0);
5423 : 18936 : type0 = TREE_TYPE (op0);
5424 : : }
5425 : 116750442 : else if (may_need_excess_precision
5426 : 89240646 : && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5427 : 27727086 : if (tree eptype = excess_precision_type (type0))
5428 : : {
5429 : 50701 : type0 = eptype;
5430 : 50701 : op0 = convert (eptype, op0);
5431 : : }
5432 : 116769378 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5433 : : {
5434 : 23884 : op1 = TREE_OPERAND (op1, 0);
5435 : 23884 : type1 = TREE_TYPE (op1);
5436 : : }
5437 : 116745494 : else if (may_need_excess_precision
5438 : 89238369 : && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5439 : 27658081 : if (tree eptype = excess_precision_type (type1))
5440 : : {
5441 : 33357 : type1 = eptype;
5442 : 33357 : op1 = convert (eptype, op1);
5443 : : }
5444 : :
5445 : : /* Issue warnings about peculiar, but valid, uses of NULL. */
5446 : 233538671 : if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5447 : : /* It's reasonable to use pointer values as operands of &&
5448 : : and ||, so NULL is no exception. */
5449 : 11971 : && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5450 : 11956 : && ( /* Both are NULL (or 0) and the operation was not a
5451 : : comparison or a pointer subtraction. */
5452 : 12029 : (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5453 : 27 : && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5454 : : /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5455 : 11944 : || (!null_ptr_cst_p (orig_op0)
5456 : 11883 : && !TYPE_PTR_OR_PTRMEM_P (type0))
5457 : 11932 : || (!null_ptr_cst_p (orig_op1)
5458 : 46 : && !TYPE_PTR_OR_PTRMEM_P (type1)))
5459 : 116769417 : && (complain & tf_warning))
5460 : : {
5461 : 39 : location_t loc =
5462 : 39 : expansion_point_location_if_in_system_header (input_location);
5463 : :
5464 : 39 : warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5465 : : }
5466 : :
5467 : : /* In case when one of the operands of the binary operation is
5468 : : a vector and another is a scalar -- convert scalar to vector. */
5469 : 116825954 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5470 : 116825136 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5471 : : {
5472 : 902 : enum stv_conv convert_flag
5473 : 902 : = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5474 : : complain & tf_error);
5475 : :
5476 : 902 : switch (convert_flag)
5477 : : {
5478 : 21 : case stv_error:
5479 : 21 : return error_mark_node;
5480 : 54 : case stv_firstarg:
5481 : 54 : {
5482 : 54 : op0 = convert (TREE_TYPE (type1), op0);
5483 : 54 : op0 = cp_save_expr (op0);
5484 : 54 : op0 = build_vector_from_val (type1, op0);
5485 : 54 : orig_type0 = type0 = TREE_TYPE (op0);
5486 : 54 : code0 = TREE_CODE (type0);
5487 : 54 : converted = 1;
5488 : 54 : break;
5489 : : }
5490 : 722 : case stv_secondarg:
5491 : 722 : {
5492 : 722 : op1 = convert (TREE_TYPE (type0), op1);
5493 : 722 : op1 = cp_save_expr (op1);
5494 : 722 : op1 = build_vector_from_val (type0, op1);
5495 : 722 : orig_type1 = type1 = TREE_TYPE (op1);
5496 : 722 : code1 = TREE_CODE (type1);
5497 : 722 : converted = 1;
5498 : 722 : break;
5499 : : }
5500 : : default:
5501 : : break;
5502 : : }
5503 : : }
5504 : :
5505 : 116769357 : switch (code)
5506 : : {
5507 : 13720800 : case MINUS_EXPR:
5508 : : /* Subtraction of two similar pointers.
5509 : : We must subtract them as integers, then divide by object size. */
5510 : 13720800 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5511 : 14915512 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5512 : 1194712 : TREE_TYPE (type1)))
5513 : : {
5514 : 1194710 : result = pointer_diff (location, op0, op1,
5515 : : common_pointer_type (type0, type1), complain,
5516 : : &instrument_expr);
5517 : 1194710 : if (instrument_expr != NULL)
5518 : 84 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5519 : : instrument_expr, result);
5520 : :
5521 : 1194710 : return result;
5522 : : }
5523 : : /* In all other cases except pointer - int, the usual arithmetic
5524 : : rules apply. */
5525 : 12526090 : else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5526 : : {
5527 : : common = 1;
5528 : : break;
5529 : : }
5530 : : /* The pointer - int case is just like pointer + int; fall
5531 : : through. */
5532 : 18112936 : gcc_fallthrough ();
5533 : 18112936 : case PLUS_EXPR:
5534 : 18112936 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5535 : 5875908 : && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5536 : : {
5537 : 5875899 : tree ptr_operand;
5538 : 5875899 : tree int_operand;
5539 : 5875899 : ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5540 : 16718 : int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5541 : 5875899 : if (processing_template_decl)
5542 : : {
5543 : 1942824 : result_type = TREE_TYPE (ptr_operand);
5544 : 1942824 : break;
5545 : : }
5546 : 3933075 : return cp_pointer_int_sum (location, code,
5547 : : ptr_operand,
5548 : : int_operand,
5549 : 3933075 : complain);
5550 : : }
5551 : : common = 1;
5552 : : break;
5553 : :
5554 : : case MULT_EXPR:
5555 : 111616366 : common = 1;
5556 : : break;
5557 : :
5558 : 9261359 : case TRUNC_DIV_EXPR:
5559 : 9261359 : case CEIL_DIV_EXPR:
5560 : 9261359 : case FLOOR_DIV_EXPR:
5561 : 9261359 : case ROUND_DIV_EXPR:
5562 : 9261359 : case EXACT_DIV_EXPR:
5563 : 9261359 : if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5564 : : {
5565 : 95371 : tree type0 = TREE_OPERAND (op0, 0);
5566 : 95371 : tree type1 = TREE_OPERAND (op1, 0);
5567 : 95371 : tree first_arg = tree_strip_any_location_wrapper (type0);
5568 : 95371 : if (!TYPE_P (type0))
5569 : 76720 : type0 = TREE_TYPE (type0);
5570 : 95371 : if (!TYPE_P (type1))
5571 : 43107 : type1 = TREE_TYPE (type1);
5572 : 95371 : if (type0
5573 : 95371 : && type1
5574 : 75618 : && INDIRECT_TYPE_P (type0)
5575 : 95446 : && same_type_p (TREE_TYPE (type0), type1))
5576 : : {
5577 : 27 : if (!(TREE_CODE (first_arg) == PARM_DECL
5578 : 21 : && DECL_ARRAY_PARAMETER_P (first_arg)
5579 : 3 : && warn_sizeof_array_argument)
5580 : 42 : && (complain & tf_warning))
5581 : : {
5582 : 21 : auto_diagnostic_group d;
5583 : 21 : if (warning_at (location, OPT_Wsizeof_pointer_div,
5584 : : "division %<sizeof (%T) / sizeof (%T)%> does "
5585 : : "not compute the number of array elements",
5586 : : type0, type1))
5587 : 18 : if (DECL_P (first_arg))
5588 : 18 : inform (DECL_SOURCE_LOCATION (first_arg),
5589 : : "first %<sizeof%> operand was declared here");
5590 : 21 : }
5591 : : }
5592 : 95347 : else if (!dependent_type_p (type0)
5593 : 23454 : && !dependent_type_p (type1)
5594 : 23356 : && TREE_CODE (type0) == ARRAY_TYPE
5595 : 21748 : && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5596 : : /* Set by finish_parenthesized_expr. */
5597 : 21130 : && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5598 : 116459 : && (complain & tf_warning))
5599 : 21112 : maybe_warn_sizeof_array_div (location, first_arg, type0,
5600 : : op1, non_reference (type1));
5601 : : }
5602 : :
5603 : 9261359 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5604 : : || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5605 : 9261341 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5606 : : || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5607 : : {
5608 : 9261332 : enum tree_code tcode0 = code0, tcode1 = code1;
5609 : 9261332 : doing_div_or_mod = true;
5610 : 9261332 : warn_for_div_by_zero (location, fold_for_warn (op1));
5611 : :
5612 : 9261332 : if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5613 : 59048 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5614 : 9261332 : if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5615 : 30238 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5616 : :
5617 : 9261332 : if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5618 : : resultcode = RDIV_EXPR;
5619 : : else
5620 : : {
5621 : : /* When dividing two signed integers, we have to promote to int.
5622 : : unless we divide by a constant != -1. Note that default
5623 : : conversion will have been performed on the operands at this
5624 : : point, so we have to dig out the original type to find out if
5625 : : it was unsigned. */
5626 : 3675176 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5627 : 3675176 : shorten = may_shorten_divmod (op0, stripped_op1);
5628 : : }
5629 : :
5630 : : common = 1;
5631 : : }
5632 : : break;
5633 : :
5634 : 1969482 : case BIT_AND_EXPR:
5635 : 1969482 : case BIT_IOR_EXPR:
5636 : 1969482 : case BIT_XOR_EXPR:
5637 : 1969482 : if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5638 : 1969482 : || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5639 : 17292 : && !VECTOR_FLOAT_TYPE_P (type0)
5640 : 17274 : && !VECTOR_FLOAT_TYPE_P (type1)))
5641 : : shorten = -1;
5642 : : break;
5643 : :
5644 : 1119861 : case TRUNC_MOD_EXPR:
5645 : 1119861 : case FLOOR_MOD_EXPR:
5646 : 1119861 : doing_div_or_mod = true;
5647 : 1119861 : warn_for_div_by_zero (location, fold_for_warn (op1));
5648 : :
5649 : 1119861 : if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5650 : 20 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5651 : 1119881 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5652 : : common = 1;
5653 : 1119841 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5654 : : {
5655 : : /* Although it would be tempting to shorten always here, that loses
5656 : : on some targets, since the modulo instruction is undefined if the
5657 : : quotient can't be represented in the computation mode. We shorten
5658 : : only if unsigned or if dividing by something we know != -1. */
5659 : 1119817 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5660 : 1119817 : shorten = may_shorten_divmod (op0, stripped_op1);
5661 : 1119817 : common = 1;
5662 : : }
5663 : : break;
5664 : :
5665 : 12984750 : case TRUTH_ANDIF_EXPR:
5666 : 12984750 : case TRUTH_ORIF_EXPR:
5667 : 12984750 : case TRUTH_AND_EXPR:
5668 : 12984750 : case TRUTH_OR_EXPR:
5669 : 12984750 : if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5670 : : {
5671 : 3 : if (!COMPARISON_CLASS_P (op1))
5672 : 3 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5673 : : build_zero_cst (type1), complain);
5674 : 3 : if (code == TRUTH_ANDIF_EXPR)
5675 : : {
5676 : 0 : tree z = build_zero_cst (TREE_TYPE (op1));
5677 : 0 : return build_conditional_expr (location, op0, op1, z, complain);
5678 : : }
5679 : 3 : else if (code == TRUTH_ORIF_EXPR)
5680 : : {
5681 : 3 : tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5682 : 3 : return build_conditional_expr (location, op0, m1, op1, complain);
5683 : : }
5684 : : else
5685 : 0 : gcc_unreachable ();
5686 : : }
5687 : 12984747 : if (gnu_vector_type_p (type0)
5688 : 12984747 : && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5689 : : {
5690 : 24 : if (!COMPARISON_CLASS_P (op0))
5691 : 24 : op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5692 : : build_zero_cst (type0), complain);
5693 : 24 : if (!VECTOR_TYPE_P (type1))
5694 : : {
5695 : 9 : tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5696 : 9 : tree z = build_zero_cst (TREE_TYPE (op0));
5697 : 9 : op1 = build_conditional_expr (location, op1, m1, z, complain);
5698 : : }
5699 : 15 : else if (!COMPARISON_CLASS_P (op1))
5700 : 15 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5701 : : build_zero_cst (type1), complain);
5702 : :
5703 : 24 : if (code == TRUTH_ANDIF_EXPR)
5704 : : code = BIT_AND_EXPR;
5705 : 9 : else if (code == TRUTH_ORIF_EXPR)
5706 : : code = BIT_IOR_EXPR;
5707 : : else
5708 : 0 : gcc_unreachable ();
5709 : :
5710 : 24 : return cp_build_binary_op (location, code, op0, op1, complain);
5711 : : }
5712 : :
5713 : 12984723 : result_type = boolean_type_node;
5714 : 12984723 : break;
5715 : :
5716 : : /* Shift operations: result has same type as first operand;
5717 : : always convert second operand to int.
5718 : : Also set SHORT_SHIFT if shifting rightward. */
5719 : :
5720 : 880901 : case RSHIFT_EXPR:
5721 : 880901 : if (gnu_vector_type_p (type0)
5722 : 170 : && code1 == INTEGER_TYPE
5723 : 880948 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5724 : : {
5725 : : result_type = type0;
5726 : : converted = 1;
5727 : : }
5728 : 880854 : else if (gnu_vector_type_p (type0)
5729 : 123 : && gnu_vector_type_p (type1)
5730 : 123 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5731 : 120 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5732 : 880974 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5733 : : TYPE_VECTOR_SUBPARTS (type1)))
5734 : : {
5735 : : result_type = type0;
5736 : : converted = 1;
5737 : : }
5738 : 880734 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5739 : : {
5740 : 880707 : tree const_op1 = fold_for_warn (op1);
5741 : 880707 : if (TREE_CODE (const_op1) != INTEGER_CST)
5742 : 69296 : const_op1 = op1;
5743 : 880707 : result_type = type0;
5744 : 880707 : doing_shift = true;
5745 : 880707 : if (TREE_CODE (const_op1) == INTEGER_CST)
5746 : : {
5747 : 811411 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5748 : : {
5749 : 48 : if ((complain & tf_warning)
5750 : 48 : && c_inhibit_evaluation_warnings == 0)
5751 : 36 : warning_at (location, OPT_Wshift_count_negative,
5752 : : "right shift count is negative");
5753 : : }
5754 : : else
5755 : : {
5756 : 811363 : if (!integer_zerop (const_op1))
5757 : 811277 : short_shift = 1;
5758 : :
5759 : 811363 : if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5760 : 46 : && (complain & tf_warning)
5761 : 811409 : && c_inhibit_evaluation_warnings == 0)
5762 : 34 : warning_at (location, OPT_Wshift_count_overflow,
5763 : : "right shift count >= width of type");
5764 : : }
5765 : : }
5766 : : /* Avoid converting op1 to result_type later. */
5767 : : converted = 1;
5768 : : }
5769 : : break;
5770 : :
5771 : 2639513 : case LSHIFT_EXPR:
5772 : 2639513 : if (gnu_vector_type_p (type0)
5773 : 183 : && code1 == INTEGER_TYPE
5774 : 2639550 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5775 : : {
5776 : : result_type = type0;
5777 : : converted = 1;
5778 : : }
5779 : 2639476 : else if (gnu_vector_type_p (type0)
5780 : 146 : && gnu_vector_type_p (type1)
5781 : 146 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5782 : 143 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5783 : 2639616 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5784 : : TYPE_VECTOR_SUBPARTS (type1)))
5785 : : {
5786 : : result_type = type0;
5787 : : converted = 1;
5788 : : }
5789 : 2639336 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5790 : : {
5791 : 2639305 : tree const_op0 = fold_for_warn (op0);
5792 : 2639305 : if (TREE_CODE (const_op0) != INTEGER_CST)
5793 : 113072 : const_op0 = op0;
5794 : 2639305 : tree const_op1 = fold_for_warn (op1);
5795 : 2639305 : if (TREE_CODE (const_op1) != INTEGER_CST)
5796 : 188135 : const_op1 = op1;
5797 : 2639305 : result_type = type0;
5798 : 2639305 : doing_shift = true;
5799 : 2639305 : if (TREE_CODE (const_op0) == INTEGER_CST
5800 : 2526233 : && tree_int_cst_sgn (const_op0) < 0
5801 : 280 : && !TYPE_OVERFLOW_WRAPS (type0)
5802 : 196 : && (complain & tf_warning)
5803 : 2639501 : && c_inhibit_evaluation_warnings == 0)
5804 : 196 : warning_at (location, OPT_Wshift_negative_value,
5805 : : "left shift of negative value");
5806 : 2639305 : if (TREE_CODE (const_op1) == INTEGER_CST)
5807 : : {
5808 : 2451170 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5809 : : {
5810 : 57 : if ((complain & tf_warning)
5811 : 57 : && c_inhibit_evaluation_warnings == 0)
5812 : 39 : warning_at (location, OPT_Wshift_count_negative,
5813 : : "left shift count is negative");
5814 : : }
5815 : 2451113 : else if (compare_tree_int (const_op1,
5816 : 2451113 : TYPE_PRECISION (type0)) >= 0)
5817 : : {
5818 : 83 : if ((complain & tf_warning)
5819 : 61 : && c_inhibit_evaluation_warnings == 0)
5820 : 45 : warning_at (location, OPT_Wshift_count_overflow,
5821 : : "left shift count >= width of type");
5822 : : }
5823 : 2451030 : else if (TREE_CODE (const_op0) == INTEGER_CST
5824 : 2359300 : && (complain & tf_warning))
5825 : 2251354 : maybe_warn_shift_overflow (location, const_op0, const_op1);
5826 : : }
5827 : : /* Avoid converting op1 to result_type later. */
5828 : : converted = 1;
5829 : : }
5830 : : break;
5831 : :
5832 : 18856635 : case EQ_EXPR:
5833 : 18856635 : case NE_EXPR:
5834 : 18856635 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5835 : 2395 : goto vector_compare;
5836 : 18854240 : if ((complain & tf_warning)
5837 : 17810125 : && c_inhibit_evaluation_warnings == 0
5838 : 36075831 : && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5839 : 801413 : warning_at (location, OPT_Wfloat_equal,
5840 : : "comparing floating-point with %<==%> "
5841 : : "or %<!=%> is unsafe");
5842 : 18854240 : {
5843 : 18854240 : tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5844 : 18854240 : tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5845 : 18854240 : if ((complain & tf_warning_or_error)
5846 : 18854240 : && ((TREE_CODE (stripped_orig_op0) == STRING_CST
5847 : 39 : && !integer_zerop (cp_fully_fold (op1)))
5848 : 17960610 : || (TREE_CODE (stripped_orig_op1) == STRING_CST
5849 : 62 : && !integer_zerop (cp_fully_fold (op0)))))
5850 : 41 : warning_at (location, OPT_Waddress,
5851 : : "comparison with string literal results in "
5852 : : "unspecified behavior");
5853 : 18854199 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5854 : 18854199 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5855 : : {
5856 : : /* P2865R5 made array comparisons ill-formed in C++26. */
5857 : 76 : if (complain & tf_warning_or_error)
5858 : 45 : do_warn_array_compare (location, code, stripped_orig_op0,
5859 : : stripped_orig_op1);
5860 : 31 : else if (cxx_dialect >= cxx26)
5861 : 13 : return error_mark_node;
5862 : : }
5863 : : }
5864 : :
5865 : 18854227 : build_type = boolean_type_node;
5866 : 18854227 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5867 : : || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5868 : 15937466 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5869 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5870 : : short_compare = 1;
5871 : 20463 : else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5872 : 2916329 : && null_ptr_cst_p (orig_op1))
5873 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5874 : 4390336 : || (code0 == POINTER_TYPE
5875 : 1434320 : && TYPE_PTR_P (type1) && integer_zerop (op1)))
5876 : : {
5877 : 1500959 : if (TYPE_PTR_P (type1))
5878 : 19317 : result_type = composite_pointer_type (location,
5879 : : type0, type1, op0, op1,
5880 : : CPO_COMPARISON, complain);
5881 : : else
5882 : : result_type = type0;
5883 : :
5884 : 1500959 : if (char_type_p (TREE_TYPE (orig_op1)))
5885 : : {
5886 : 10 : auto_diagnostic_group d;
5887 : 10 : if (warning_at (location, OPT_Wpointer_compare,
5888 : : "comparison between pointer and zero character "
5889 : : "constant"))
5890 : 10 : inform (location,
5891 : : "did you mean to dereference the pointer?");
5892 : 10 : }
5893 : 1500959 : warn_for_null_address (location, op0, complain);
5894 : : }
5895 : 864 : else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5896 : 1434557 : && null_ptr_cst_p (orig_op0))
5897 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5898 : 2869167 : || (code1 == POINTER_TYPE
5899 : 1433297 : && TYPE_PTR_P (type0) && integer_zerop (op0)))
5900 : : {
5901 : 961 : if (TYPE_PTR_P (type0))
5902 : 68 : result_type = composite_pointer_type (location,
5903 : : type0, type1, op0, op1,
5904 : : CPO_COMPARISON, complain);
5905 : : else
5906 : : result_type = type1;
5907 : :
5908 : 961 : if (char_type_p (TREE_TYPE (orig_op0)))
5909 : : {
5910 : 10 : auto_diagnostic_group d;
5911 : 10 : if (warning_at (location, OPT_Wpointer_compare,
5912 : : "comparison between pointer and zero character "
5913 : : "constant"))
5914 : 10 : inform (location,
5915 : : "did you mean to dereference the pointer?");
5916 : 10 : }
5917 : 961 : warn_for_null_address (location, op1, complain);
5918 : : }
5919 : 1434069 : else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5920 : 19192 : || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5921 : 1415244 : result_type = composite_pointer_type (location,
5922 : : type0, type1, op0, op1,
5923 : : CPO_COMPARISON, complain);
5924 : 18825 : else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5925 : : /* One of the operands must be of nullptr_t type. */
5926 : 74 : result_type = TREE_TYPE (nullptr_node);
5927 : 18751 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5928 : : {
5929 : 58 : result_type = type0;
5930 : 58 : if (complain & tf_error)
5931 : 56 : permerror (location, "ISO C++ forbids comparison between "
5932 : : "pointer and integer");
5933 : : else
5934 : 2 : return error_mark_node;
5935 : : }
5936 : 18693 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5937 : : {
5938 : 18352 : result_type = type1;
5939 : 18352 : if (complain & tf_error)
5940 : 69 : permerror (location, "ISO C++ forbids comparison between "
5941 : : "pointer and integer");
5942 : : else
5943 : 18283 : return error_mark_node;
5944 : : }
5945 : 341 : else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5946 : : {
5947 : 191 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5948 : : == ptrmemfunc_vbit_in_delta)
5949 : : {
5950 : : tree pfn0, delta0, e1, e2;
5951 : :
5952 : : if (TREE_SIDE_EFFECTS (op0))
5953 : : op0 = cp_save_expr (op0);
5954 : :
5955 : : pfn0 = pfn_from_ptrmemfunc (op0);
5956 : : delta0 = delta_from_ptrmemfunc (op0);
5957 : : {
5958 : : /* If we will warn below about a null-address compare
5959 : : involving the orig_op0 ptrmemfunc, we'd likely also
5960 : : warn about the pfn0's null-address compare, and
5961 : : that would be redundant, so suppress it. */
5962 : : warning_sentinel ws (warn_address);
5963 : : e1 = cp_build_binary_op (location,
5964 : : EQ_EXPR,
5965 : : pfn0,
5966 : : build_zero_cst (TREE_TYPE (pfn0)),
5967 : : complain);
5968 : : }
5969 : : e2 = cp_build_binary_op (location,
5970 : : BIT_AND_EXPR,
5971 : : delta0,
5972 : : integer_one_node,
5973 : : complain);
5974 : :
5975 : : if (complain & tf_warning)
5976 : : maybe_warn_zero_as_null_pointer_constant (op1, input_location);
5977 : :
5978 : : e2 = cp_build_binary_op (location,
5979 : : EQ_EXPR, e2, integer_zero_node,
5980 : : complain);
5981 : : op0 = cp_build_binary_op (location,
5982 : : TRUTH_ANDIF_EXPR, e1, e2,
5983 : : complain);
5984 : : op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
5985 : : }
5986 : : else
5987 : : {
5988 : 191 : op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
5989 : 191 : op1 = cp_convert (TREE_TYPE (op0), op1, complain);
5990 : : }
5991 : 191 : result_type = TREE_TYPE (op0);
5992 : :
5993 : 191 : warn_for_null_address (location, orig_op0, complain);
5994 : : }
5995 : 150 : else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
5996 : 36 : return cp_build_binary_op (location, code, op1, op0, complain);
5997 : 114 : else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
5998 : : {
5999 : 93 : tree type;
6000 : : /* E will be the final comparison. */
6001 : 93 : tree e;
6002 : : /* E1 and E2 are for scratch. */
6003 : 93 : tree e1;
6004 : 93 : tree e2;
6005 : 93 : tree pfn0;
6006 : 93 : tree pfn1;
6007 : 93 : tree delta0;
6008 : 93 : tree delta1;
6009 : :
6010 : 93 : type = composite_pointer_type (location, type0, type1, op0, op1,
6011 : : CPO_COMPARISON, complain);
6012 : :
6013 : 93 : if (!same_type_p (TREE_TYPE (op0), type))
6014 : 6 : op0 = cp_convert_and_check (type, op0, complain);
6015 : 93 : if (!same_type_p (TREE_TYPE (op1), type))
6016 : 9 : op1 = cp_convert_and_check (type, op1, complain);
6017 : :
6018 : 93 : if (op0 == error_mark_node || op1 == error_mark_node)
6019 : : return error_mark_node;
6020 : :
6021 : 90 : if (TREE_SIDE_EFFECTS (op0))
6022 : 31 : op0 = cp_save_expr (op0);
6023 : 90 : if (TREE_SIDE_EFFECTS (op1))
6024 : 3 : op1 = cp_save_expr (op1);
6025 : :
6026 : 90 : pfn0 = pfn_from_ptrmemfunc (op0);
6027 : 90 : pfn0 = cp_fully_fold (pfn0);
6028 : : /* Avoid -Waddress warnings (c++/64877). */
6029 : 90 : if (TREE_CODE (pfn0) == ADDR_EXPR)
6030 : 12 : suppress_warning (pfn0, OPT_Waddress);
6031 : 90 : pfn1 = pfn_from_ptrmemfunc (op1);
6032 : 90 : pfn1 = cp_fully_fold (pfn1);
6033 : 90 : delta0 = delta_from_ptrmemfunc (op0);
6034 : 90 : delta1 = delta_from_ptrmemfunc (op1);
6035 : 90 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6036 : : == ptrmemfunc_vbit_in_delta)
6037 : : {
6038 : : /* We generate:
6039 : :
6040 : : (op0.pfn == op1.pfn
6041 : : && ((op0.delta == op1.delta)
6042 : : || (!op0.pfn && op0.delta & 1 == 0
6043 : : && op1.delta & 1 == 0))
6044 : :
6045 : : The reason for the `!op0.pfn' bit is that a NULL
6046 : : pointer-to-member is any member with a zero PFN and
6047 : : LSB of the DELTA field is 0. */
6048 : :
6049 : : e1 = cp_build_binary_op (location, BIT_AND_EXPR,
6050 : : delta0,
6051 : : integer_one_node,
6052 : : complain);
6053 : : e1 = cp_build_binary_op (location,
6054 : : EQ_EXPR, e1, integer_zero_node,
6055 : : complain);
6056 : : e2 = cp_build_binary_op (location, BIT_AND_EXPR,
6057 : : delta1,
6058 : : integer_one_node,
6059 : : complain);
6060 : : e2 = cp_build_binary_op (location,
6061 : : EQ_EXPR, e2, integer_zero_node,
6062 : : complain);
6063 : : e1 = cp_build_binary_op (location,
6064 : : TRUTH_ANDIF_EXPR, e2, e1,
6065 : : complain);
6066 : : e2 = cp_build_binary_op (location, EQ_EXPR,
6067 : : pfn0,
6068 : : build_zero_cst (TREE_TYPE (pfn0)),
6069 : : complain);
6070 : : e2 = cp_build_binary_op (location,
6071 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6072 : : e1 = cp_build_binary_op (location,
6073 : : EQ_EXPR, delta0, delta1, complain);
6074 : : e1 = cp_build_binary_op (location,
6075 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6076 : : }
6077 : : else
6078 : : {
6079 : : /* We generate:
6080 : :
6081 : : (op0.pfn == op1.pfn
6082 : : && (!op0.pfn || op0.delta == op1.delta))
6083 : :
6084 : : The reason for the `!op0.pfn' bit is that a NULL
6085 : : pointer-to-member is any member with a zero PFN; the
6086 : : DELTA field is unspecified. */
6087 : :
6088 : 90 : e1 = cp_build_binary_op (location,
6089 : : EQ_EXPR, delta0, delta1, complain);
6090 : 90 : e2 = cp_build_binary_op (location,
6091 : : EQ_EXPR,
6092 : : pfn0,
6093 : 90 : build_zero_cst (TREE_TYPE (pfn0)),
6094 : : complain);
6095 : 90 : e1 = cp_build_binary_op (location,
6096 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6097 : : }
6098 : 90 : e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
6099 : 90 : e = cp_build_binary_op (location,
6100 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6101 : 90 : if (code == EQ_EXPR)
6102 : : return e;
6103 : 30 : return cp_build_binary_op (location,
6104 : 30 : EQ_EXPR, e, integer_zero_node, complain);
6105 : : }
6106 : : else
6107 : : {
6108 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
6109 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
6110 : : type1));
6111 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
6112 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
6113 : : type0));
6114 : : }
6115 : :
6116 : : break;
6117 : :
6118 : 241 : case MAX_EXPR:
6119 : 241 : case MIN_EXPR:
6120 : 241 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6121 : 241 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6122 : : shorten = 1;
6123 : 0 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6124 : 0 : result_type = composite_pointer_type (location,
6125 : : type0, type1, op0, op1,
6126 : : CPO_COMPARISON, complain);
6127 : : break;
6128 : :
6129 : 19394338 : case LE_EXPR:
6130 : 19394338 : case GE_EXPR:
6131 : 19394338 : case LT_EXPR:
6132 : 19394338 : case GT_EXPR:
6133 : 19394338 : case SPACESHIP_EXPR:
6134 : 19394338 : if (TREE_CODE (orig_op0) == STRING_CST
6135 : 19394338 : || TREE_CODE (orig_op1) == STRING_CST)
6136 : : {
6137 : 0 : if (complain & tf_warning)
6138 : 0 : warning_at (location, OPT_Waddress,
6139 : : "comparison with string literal results "
6140 : : "in unspecified behavior");
6141 : : }
6142 : 19394338 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6143 : 153 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6144 : 19394442 : && code != SPACESHIP_EXPR)
6145 : : {
6146 : 88 : if (complain & tf_warning_or_error)
6147 : 51 : do_warn_array_compare (location, code,
6148 : : tree_strip_any_location_wrapper (orig_op0),
6149 : : tree_strip_any_location_wrapper (orig_op1));
6150 : 37 : else if (cxx_dialect >= cxx26)
6151 : 1 : return error_mark_node;
6152 : : }
6153 : :
6154 : 19394337 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6155 : : {
6156 : 6560 : vector_compare:
6157 : 6560 : tree intt;
6158 : 6560 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6159 : 6560 : TREE_TYPE (type1))
6160 : 6560 : && !vector_types_compatible_elements_p (type0, type1))
6161 : : {
6162 : 3 : if (complain & tf_error)
6163 : : {
6164 : 3 : auto_diagnostic_group d;
6165 : 3 : error_at (location, "comparing vectors with different "
6166 : : "element types");
6167 : 3 : inform (location, "operand types are %qT and %qT",
6168 : : type0, type1);
6169 : 3 : }
6170 : 3 : return error_mark_node;
6171 : : }
6172 : :
6173 : 6557 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
6174 : 13114 : TYPE_VECTOR_SUBPARTS (type1)))
6175 : : {
6176 : 3 : if (complain & tf_error)
6177 : : {
6178 : 3 : auto_diagnostic_group d;
6179 : 3 : error_at (location, "comparing vectors with different "
6180 : : "number of elements");
6181 : 3 : inform (location, "operand types are %qT and %qT",
6182 : : type0, type1);
6183 : 3 : }
6184 : 3 : return error_mark_node;
6185 : : }
6186 : :
6187 : : /* It's not precisely specified how the usual arithmetic
6188 : : conversions apply to the vector types. Here, we use
6189 : : the unsigned type if one of the operands is signed and
6190 : : the other one is unsigned. */
6191 : 6554 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6192 : : {
6193 : 36 : if (!TYPE_UNSIGNED (type0))
6194 : 36 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6195 : : else
6196 : 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6197 : 36 : warning_at (location, OPT_Wsign_compare, "comparison between "
6198 : : "types %qT and %qT", type0, type1);
6199 : : }
6200 : :
6201 : 6554 : if (resultcode == SPACESHIP_EXPR)
6202 : : {
6203 : 3 : if (complain & tf_error)
6204 : 3 : sorry_at (location, "three-way comparison of vectors");
6205 : 3 : return error_mark_node;
6206 : : }
6207 : :
6208 : : /* Always construct signed integer vector type. */
6209 : 6551 : intt = c_common_type_for_size
6210 : 13102 : (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
6211 : 6551 : if (!intt)
6212 : : {
6213 : 0 : if (complain & tf_error)
6214 : 0 : error_at (location, "could not find an integer type "
6215 : 0 : "of the same size as %qT", TREE_TYPE (type0));
6216 : 0 : return error_mark_node;
6217 : : }
6218 : 6551 : result_type = build_opaque_vector_type (intt,
6219 : 6551 : TYPE_VECTOR_SUBPARTS (type0));
6220 : 6551 : return build_vec_cmp (resultcode, result_type, op0, op1);
6221 : : }
6222 : 19390172 : build_type = boolean_type_node;
6223 : 19390172 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6224 : : || code0 == ENUMERAL_TYPE)
6225 : 18482744 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6226 : : || code1 == ENUMERAL_TYPE))
6227 : : short_compare = 1;
6228 : 907515 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6229 : 907283 : result_type = composite_pointer_type (location,
6230 : : type0, type1, op0, op1,
6231 : : CPO_COMPARISON, complain);
6232 : 74 : else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6233 : 160 : || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6234 : 338 : || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6235 : : {
6236 : : /* Core Issue 1512 made this ill-formed. */
6237 : 191 : if (complain & tf_error)
6238 : 188 : error_at (location, "ordered comparison of pointer with "
6239 : : "integer zero (%qT and %qT)", type0, type1);
6240 : 191 : return error_mark_node;
6241 : : }
6242 : 41 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6243 : : {
6244 : 0 : result_type = type0;
6245 : 0 : if (complain & tf_error)
6246 : 0 : permerror (location, "ISO C++ forbids comparison between "
6247 : : "pointer and integer");
6248 : : else
6249 : 0 : return error_mark_node;
6250 : : }
6251 : 41 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6252 : : {
6253 : 24 : result_type = type1;
6254 : 24 : if (complain & tf_error)
6255 : 24 : permerror (location, "ISO C++ forbids comparison between "
6256 : : "pointer and integer");
6257 : : else
6258 : 0 : return error_mark_node;
6259 : : }
6260 : :
6261 : 19389981 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6262 : 907309 : && !processing_template_decl
6263 : 20086537 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6264 : : {
6265 : 49 : op0 = cp_save_expr (op0);
6266 : 49 : op1 = cp_save_expr (op1);
6267 : :
6268 : 49 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6269 : 49 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6270 : : }
6271 : :
6272 : : break;
6273 : :
6274 : 0 : case UNORDERED_EXPR:
6275 : 0 : case ORDERED_EXPR:
6276 : 0 : case UNLT_EXPR:
6277 : 0 : case UNLE_EXPR:
6278 : 0 : case UNGT_EXPR:
6279 : 0 : case UNGE_EXPR:
6280 : 0 : case UNEQ_EXPR:
6281 : 0 : build_type = integer_type_node;
6282 : 0 : if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6283 : : {
6284 : 0 : if (complain & tf_error)
6285 : 0 : error ("unordered comparison on non-floating-point argument");
6286 : 0 : return error_mark_node;
6287 : : }
6288 : : common = 1;
6289 : : break;
6290 : :
6291 : : default:
6292 : : break;
6293 : : }
6294 : :
6295 : 111616366 : if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6296 : : || code0 == ENUMERAL_TYPE)
6297 : 93207109 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6298 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6299 : : arithmetic_types_p = 1;
6300 : : else
6301 : : {
6302 : 18619481 : arithmetic_types_p = 0;
6303 : : /* Vector arithmetic is only allowed when both sides are vectors. */
6304 : 18619481 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6305 : : {
6306 : 49959 : if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6307 : 49959 : || !vector_types_compatible_elements_p (type0, type1))
6308 : : {
6309 : 18 : if (complain & tf_error)
6310 : : {
6311 : : /* "location" already embeds the locations of the
6312 : : operands, so we don't need to add them separately
6313 : : to richloc. */
6314 : 18 : rich_location richloc (line_table, location);
6315 : 18 : binary_op_error (&richloc, code, type0, type1);
6316 : 18 : }
6317 : 18 : return error_mark_node;
6318 : : }
6319 : : arithmetic_types_p = 1;
6320 : : }
6321 : : }
6322 : : /* Determine the RESULT_TYPE, if it is not already known. */
6323 : 111616348 : if (!result_type
6324 : 111616348 : && arithmetic_types_p
6325 : 89343248 : && (shorten || common || short_compare))
6326 : : {
6327 : 89343171 : result_type = cp_common_type (type0, type1);
6328 : 89343171 : if (result_type == error_mark_node)
6329 : : {
6330 : 0 : tree t1 = type0;
6331 : 0 : tree t2 = type1;
6332 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6333 : 0 : t1 = TREE_TYPE (t1);
6334 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6335 : 0 : t2 = TREE_TYPE (t2);
6336 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6337 : : && TREE_CODE (t2) == REAL_TYPE
6338 : : && (extended_float_type_p (t1)
6339 : : || extended_float_type_p (t2))
6340 : : && cp_compare_floating_point_conversion_ranks
6341 : : (t1, t2) == 3);
6342 : 0 : if (complain & tf_error)
6343 : : {
6344 : 0 : rich_location richloc (line_table, location);
6345 : 0 : binary_op_error (&richloc, code, type0, type1);
6346 : 0 : }
6347 : 0 : return error_mark_node;
6348 : : }
6349 : 89343171 : if (complain & tf_warning)
6350 : 85046184 : do_warn_double_promotion (result_type, type0, type1,
6351 : : "implicit conversion from %qH to %qI "
6352 : : "to match other operand of binary "
6353 : : "expression", location);
6354 : 89343171 : if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6355 : 89343171 : TREE_TYPE (orig_op1), complain))
6356 : 6 : return error_mark_node;
6357 : : }
6358 : 111616342 : if (may_need_excess_precision
6359 : 84107145 : && (orig_type0 != type0 || orig_type1 != type1)
6360 : 72175 : && build_type == NULL_TREE
6361 : 72175 : && result_type)
6362 : : {
6363 : 54496 : gcc_assert (common);
6364 : 54496 : semantic_result_type = cp_common_type (orig_type0, orig_type1);
6365 : 54496 : if (semantic_result_type == error_mark_node)
6366 : : {
6367 : 0 : tree t1 = orig_type0;
6368 : 0 : tree t2 = orig_type1;
6369 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6370 : 0 : t1 = TREE_TYPE (t1);
6371 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6372 : 0 : t2 = TREE_TYPE (t2);
6373 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6374 : : && TREE_CODE (t2) == REAL_TYPE
6375 : : && (extended_float_type_p (t1)
6376 : : || extended_float_type_p (t2))
6377 : : && cp_compare_floating_point_conversion_ranks
6378 : : (t1, t2) == 3);
6379 : 0 : if (complain & tf_error)
6380 : : {
6381 : 0 : rich_location richloc (line_table, location);
6382 : 0 : binary_op_error (&richloc, code, type0, type1);
6383 : 0 : }
6384 : 0 : return error_mark_node;
6385 : : }
6386 : : }
6387 : :
6388 : 111616342 : if (code == SPACESHIP_EXPR)
6389 : : {
6390 : 152571 : iloc_sentinel s (location);
6391 : :
6392 : 152571 : tree orig_type0 = TREE_TYPE (orig_op0);
6393 : 152571 : tree_code orig_code0 = TREE_CODE (orig_type0);
6394 : 152571 : tree orig_type1 = TREE_TYPE (orig_op1);
6395 : 152571 : tree_code orig_code1 = TREE_CODE (orig_type1);
6396 : 152571 : if (!result_type || result_type == error_mark_node)
6397 : : /* Nope. */
6398 : : result_type = NULL_TREE;
6399 : 152550 : else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6400 : : /* "If one of the operands is of type bool and the other is not, the
6401 : : program is ill-formed." */
6402 : : result_type = NULL_TREE;
6403 : 152547 : else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6404 : 16 : && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6405 : : /* We only do array/function-to-pointer conversion if "at least one of
6406 : : the operands is of pointer type". */
6407 : : result_type = NULL_TREE;
6408 : 152531 : else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6409 : : /* <=> no longer supports equality relations. */
6410 : : result_type = NULL_TREE;
6411 : 152528 : else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6412 : 152564 : && !(same_type_ignoring_top_level_qualifiers_p
6413 : 36 : (orig_type0, orig_type1)))
6414 : : /* "If both operands have arithmetic types, or one operand has integral
6415 : : type and the other operand has unscoped enumeration type, the usual
6416 : : arithmetic conversions are applied to the operands." So we don't do
6417 : : arithmetic conversions if the operands both have enumeral type. */
6418 : : result_type = NULL_TREE;
6419 : 152513 : else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6420 : 152507 : || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6421 : : /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6422 : : [where one is of enumeration type and the other is of a different
6423 : : enumeration type or a floating-point type] are ill-formed. */
6424 : : result_type = NULL_TREE;
6425 : :
6426 : 152501 : if (result_type)
6427 : : {
6428 : 152501 : build_type = spaceship_type (result_type, complain);
6429 : 152501 : if (build_type == error_mark_node)
6430 : : return error_mark_node;
6431 : : }
6432 : :
6433 : 152556 : if (result_type && arithmetic_types_p)
6434 : : {
6435 : : /* If a narrowing conversion is required, other than from an integral
6436 : : type to a floating point type, the program is ill-formed. */
6437 : 73289 : bool ok = true;
6438 : 73289 : if (TREE_CODE (result_type) == REAL_TYPE
6439 : 641 : && CP_INTEGRAL_TYPE_P (orig_type0))
6440 : : /* OK */;
6441 : 73278 : else if (!check_narrowing (result_type, orig_op0, complain))
6442 : 73289 : ok = false;
6443 : 73289 : if (TREE_CODE (result_type) == REAL_TYPE
6444 : 641 : && CP_INTEGRAL_TYPE_P (orig_type1))
6445 : : /* OK */;
6446 : 73278 : else if (!check_narrowing (result_type, orig_op1, complain))
6447 : : ok = false;
6448 : 73289 : if (!ok && !(complain & tf_error))
6449 : 4 : return error_mark_node;
6450 : : }
6451 : 152571 : }
6452 : :
6453 : 111616323 : if (!result_type)
6454 : : {
6455 : 477 : if (complain & tf_error)
6456 : : {
6457 : 206 : binary_op_rich_location richloc (location,
6458 : 206 : orig_op0, orig_op1, true);
6459 : 206 : error_at (&richloc,
6460 : : "invalid operands of types %qT and %qT to binary %qO",
6461 : 206 : TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6462 : 206 : }
6463 : 477 : return error_mark_node;
6464 : : }
6465 : :
6466 : : /* If we're in a template, the only thing we need to know is the
6467 : : RESULT_TYPE. */
6468 : 111615846 : if (processing_template_decl)
6469 : : {
6470 : : /* Since the middle-end checks the type when doing a build2, we
6471 : : need to build the tree in pieces. This built tree will never
6472 : : get out of the front-end as we replace it when instantiating
6473 : : the template. */
6474 : 49667666 : tree tmp = build2 (resultcode,
6475 : : build_type ? build_type : result_type,
6476 : : NULL_TREE, op1);
6477 : 31101540 : TREE_OPERAND (tmp, 0) = op0;
6478 : 31101540 : if (semantic_result_type)
6479 : 0 : tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6480 : 31101540 : return tmp;
6481 : : }
6482 : :
6483 : : /* Remember the original type; RESULT_TYPE might be changed later on
6484 : : by shorten_binary_op. */
6485 : 80514306 : tree orig_type = result_type;
6486 : :
6487 : 80514306 : if (arithmetic_types_p)
6488 : : {
6489 : 70477842 : bool first_complex = (code0 == COMPLEX_TYPE);
6490 : 70477842 : bool second_complex = (code1 == COMPLEX_TYPE);
6491 : 70477842 : int none_complex = (!first_complex && !second_complex);
6492 : :
6493 : : /* Adapted from patch for c/24581. */
6494 : 70477842 : if (first_complex != second_complex
6495 : 161978 : && (code == PLUS_EXPR
6496 : : || code == MINUS_EXPR
6497 : 161978 : || code == MULT_EXPR
6498 : 42323 : || (code == TRUNC_DIV_EXPR && first_complex))
6499 : 148471 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6500 : 70626152 : && flag_signed_zeros)
6501 : : {
6502 : : /* An operation on mixed real/complex operands must be
6503 : : handled specially, but the language-independent code can
6504 : : more easily optimize the plain complex arithmetic if
6505 : : -fno-signed-zeros. */
6506 : 148310 : tree real_type = TREE_TYPE (result_type);
6507 : 148310 : tree real, imag;
6508 : 148310 : if (first_complex)
6509 : : {
6510 : 115297 : if (TREE_TYPE (op0) != result_type)
6511 : 6 : op0 = cp_convert_and_check (result_type, op0, complain);
6512 : 115297 : if (TREE_TYPE (op1) != real_type)
6513 : 26 : op1 = cp_convert_and_check (real_type, op1, complain);
6514 : : }
6515 : : else
6516 : : {
6517 : 33013 : if (TREE_TYPE (op0) != real_type)
6518 : 31 : op0 = cp_convert_and_check (real_type, op0, complain);
6519 : 33013 : if (TREE_TYPE (op1) != result_type)
6520 : 32 : op1 = cp_convert_and_check (result_type, op1, complain);
6521 : : }
6522 : 148310 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6523 : 0 : return error_mark_node;
6524 : 148310 : if (first_complex)
6525 : : {
6526 : 115297 : op0 = cp_save_expr (op0);
6527 : 115297 : real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6528 : 115297 : imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6529 : 115297 : switch (code)
6530 : : {
6531 : 57625 : case MULT_EXPR:
6532 : 57625 : case TRUNC_DIV_EXPR:
6533 : 57625 : op1 = cp_save_expr (op1);
6534 : 57625 : imag = build2 (resultcode, real_type, imag, op1);
6535 : : /* Fall through. */
6536 : 115297 : case PLUS_EXPR:
6537 : 115297 : case MINUS_EXPR:
6538 : 115297 : real = build2 (resultcode, real_type, real, op1);
6539 : 115297 : break;
6540 : 0 : default:
6541 : 0 : gcc_unreachable();
6542 : : }
6543 : : }
6544 : : else
6545 : : {
6546 : 33013 : op1 = cp_save_expr (op1);
6547 : 33013 : real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6548 : 33013 : imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6549 : 33013 : switch (code)
6550 : : {
6551 : 638 : case MULT_EXPR:
6552 : 638 : op0 = cp_save_expr (op0);
6553 : 638 : imag = build2 (resultcode, real_type, op0, imag);
6554 : : /* Fall through. */
6555 : 17266 : case PLUS_EXPR:
6556 : 17266 : real = build2 (resultcode, real_type, op0, real);
6557 : 17266 : break;
6558 : 15747 : case MINUS_EXPR:
6559 : 15747 : real = build2 (resultcode, real_type, op0, real);
6560 : 15747 : imag = build1 (NEGATE_EXPR, real_type, imag);
6561 : 15747 : break;
6562 : 0 : default:
6563 : 0 : gcc_unreachable();
6564 : : }
6565 : : }
6566 : 148310 : result = build2 (COMPLEX_EXPR, result_type, real, imag);
6567 : 148310 : if (semantic_result_type)
6568 : 24 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6569 : : result);
6570 : 148310 : return result;
6571 : : }
6572 : :
6573 : : /* For certain operations (which identify themselves by shorten != 0)
6574 : : if both args were extended from the same smaller type,
6575 : : do the arithmetic in that type and then extend.
6576 : :
6577 : : shorten !=0 and !=1 indicates a bitwise operation.
6578 : : For them, this optimization is safe only if
6579 : : both args are zero-extended or both are sign-extended.
6580 : : Otherwise, we might change the result.
6581 : : E.g., (short)-1 | (unsigned short)-1 is (int)-1
6582 : : but calculated in (unsigned short) it would be (unsigned short)-1. */
6583 : :
6584 : 70329532 : if (shorten && none_complex)
6585 : : {
6586 : 3749818 : final_type = result_type;
6587 : 3749818 : result_type = shorten_binary_op (result_type, op0, op1,
6588 : : shorten == -1);
6589 : : }
6590 : :
6591 : : /* Shifts can be shortened if shifting right. */
6592 : :
6593 : 70329532 : if (short_shift)
6594 : : {
6595 : 716081 : int unsigned_arg;
6596 : 716081 : tree arg0 = get_narrower (op0, &unsigned_arg);
6597 : : /* We're not really warning here but when we set short_shift we
6598 : : used fold_for_warn to fold the operand. */
6599 : 716081 : tree const_op1 = fold_for_warn (op1);
6600 : :
6601 : 716081 : final_type = result_type;
6602 : :
6603 : 716081 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
6604 : 658094 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6605 : :
6606 : 716081 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6607 : 1064 : && tree_int_cst_sgn (const_op1) > 0
6608 : : /* We can shorten only if the shift count is less than the
6609 : : number of bits in the smaller type size. */
6610 : 1064 : && compare_tree_int (const_op1,
6611 : 1064 : TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6612 : : /* We cannot drop an unsigned shift after sign-extension. */
6613 : 717135 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6614 : : {
6615 : : /* Do an unsigned shift if the operand was zero-extended. */
6616 : 1047 : result_type
6617 : 1047 : = c_common_signed_or_unsigned_type (unsigned_arg,
6618 : 1047 : TREE_TYPE (arg0));
6619 : : /* Convert value-to-be-shifted to that type. */
6620 : 1047 : if (TREE_TYPE (op0) != result_type)
6621 : 1047 : op0 = convert (result_type, op0);
6622 : : converted = 1;
6623 : : }
6624 : : }
6625 : :
6626 : : /* Comparison operations are shortened too but differently.
6627 : : They identify themselves by setting short_compare = 1. */
6628 : :
6629 : 70329532 : if (short_compare)
6630 : : {
6631 : : /* We call shorten_compare only for diagnostics. */
6632 : 22312656 : tree xop0 = fold_simple (op0);
6633 : 22312656 : tree xop1 = fold_simple (op1);
6634 : 22312656 : tree xresult_type = result_type;
6635 : 22312656 : enum tree_code xresultcode = resultcode;
6636 : 22312656 : shorten_compare (location, &xop0, &xop1, &xresult_type,
6637 : : &xresultcode);
6638 : : }
6639 : :
6640 : 48016789 : if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6641 : 22312845 : && warn_sign_compare
6642 : : /* Do not warn until the template is instantiated; we cannot
6643 : : bound the ranges of the arguments until that point. */
6644 : 304622 : && !processing_template_decl
6645 : 304622 : && (complain & tf_warning)
6646 : 299287 : && c_inhibit_evaluation_warnings == 0
6647 : : /* Even unsigned enum types promote to signed int. We don't
6648 : : want to issue -Wsign-compare warnings for this case. */
6649 : 284378 : && !enum_cast_to_int (orig_op0)
6650 : 70613910 : && !enum_cast_to_int (orig_op1))
6651 : : {
6652 : 284348 : warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6653 : : result_type, resultcode);
6654 : : }
6655 : : }
6656 : :
6657 : : /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6658 : : Then the expression will be built.
6659 : : It will be given type FINAL_TYPE if that is nonzero;
6660 : : otherwise, it will be given type RESULT_TYPE. */
6661 : 80365996 : if (! converted)
6662 : : {
6663 : 77188675 : warning_sentinel w (warn_sign_conversion, short_compare);
6664 : 77188675 : if (!same_type_p (TREE_TYPE (op0), result_type))
6665 : 3285191 : op0 = cp_convert_and_check (result_type, op0, complain);
6666 : 77188675 : if (!same_type_p (TREE_TYPE (op1), result_type))
6667 : 13729127 : op1 = cp_convert_and_check (result_type, op1, complain);
6668 : :
6669 : 77188675 : if (op0 == error_mark_node || op1 == error_mark_node)
6670 : 65 : return error_mark_node;
6671 : 77188675 : }
6672 : :
6673 : 80365931 : if (build_type == NULL_TREE)
6674 : 54675715 : build_type = result_type;
6675 : :
6676 : 80365931 : if (doing_shift
6677 : 3176758 : && flag_strong_eval_order == 2
6678 : 3088018 : && TREE_SIDE_EFFECTS (op1)
6679 : 80385371 : && !processing_template_decl)
6680 : : {
6681 : : /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6682 : : op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6683 : 19440 : op0 = cp_save_expr (op0);
6684 : 19440 : instrument_expr = op0;
6685 : : }
6686 : :
6687 : 80365931 : if (sanitize_flags_p ((SANITIZE_SHIFT
6688 : : | SANITIZE_DIVIDE
6689 : : | SANITIZE_FLOAT_DIVIDE
6690 : : | SANITIZE_SI_OVERFLOW))
6691 : 12103 : && current_function_decl != NULL_TREE
6692 : 9952 : && !processing_template_decl
6693 : 80375883 : && (doing_div_or_mod || doing_shift))
6694 : : {
6695 : : /* OP0 and/or OP1 might have side-effects. */
6696 : 874 : op0 = cp_save_expr (op0);
6697 : 874 : op1 = cp_save_expr (op1);
6698 : 874 : op0 = fold_non_dependent_expr (op0, complain);
6699 : 874 : op1 = fold_non_dependent_expr (op1, complain);
6700 : 874 : tree instrument_expr1 = NULL_TREE;
6701 : 874 : if (doing_div_or_mod
6702 : 874 : && sanitize_flags_p (SANITIZE_DIVIDE
6703 : : | SANITIZE_FLOAT_DIVIDE
6704 : : | SANITIZE_SI_OVERFLOW))
6705 : : {
6706 : : /* For diagnostics we want to use the promoted types without
6707 : : shorten_binary_op. So convert the arguments to the
6708 : : original result_type. */
6709 : 447 : tree cop0 = op0;
6710 : 447 : tree cop1 = op1;
6711 : 447 : if (TREE_TYPE (cop0) != orig_type)
6712 : 6 : cop0 = cp_convert (orig_type, op0, complain);
6713 : 447 : if (TREE_TYPE (cop1) != orig_type)
6714 : 32 : cop1 = cp_convert (orig_type, op1, complain);
6715 : 447 : instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6716 : : }
6717 : 427 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6718 : 346 : instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6719 : 874 : if (instrument_expr != NULL)
6720 : 18 : instrument_expr = add_stmt_to_compound (instrument_expr,
6721 : : instrument_expr1);
6722 : : else
6723 : 856 : instrument_expr = instrument_expr1;
6724 : : }
6725 : :
6726 : 80365931 : result = build2_loc (location, resultcode, build_type, op0, op1);
6727 : 80365931 : if (final_type != 0)
6728 : 4465899 : result = cp_convert (final_type, result, complain);
6729 : :
6730 : 80365931 : if (instrument_expr != NULL)
6731 : 19936 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6732 : : instrument_expr, result);
6733 : :
6734 : 80365931 : if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6735 : 129370 : result = get_target_expr (result, complain);
6736 : :
6737 : 80365931 : if (semantic_result_type)
6738 : 54472 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
6739 : :
6740 : 80365931 : if (!c_inhibit_evaluation_warnings)
6741 : : {
6742 : 72685235 : if (!processing_template_decl)
6743 : : {
6744 : 72685235 : op0 = cp_fully_fold (op0);
6745 : : /* Only consider the second argument if the first isn't overflowed. */
6746 : 72685235 : if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6747 : : return result;
6748 : 18999180 : op1 = cp_fully_fold (op1);
6749 : 18999180 : if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6750 : : return result;
6751 : : }
6752 : 0 : else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6753 : 0 : || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6754 : : return result;
6755 : :
6756 : 13867035 : tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6757 : 13867035 : if (TREE_OVERFLOW_P (result_ovl))
6758 : 188 : overflow_warning (location, result_ovl);
6759 : : }
6760 : :
6761 : : return result;
6762 : : }
6763 : :
6764 : : /* Build a VEC_PERM_EXPR.
6765 : : This is a simple wrapper for c_build_vec_perm_expr. */
6766 : : tree
6767 : 14554 : build_x_vec_perm_expr (location_t loc,
6768 : : tree arg0, tree arg1, tree arg2,
6769 : : tsubst_flags_t complain)
6770 : : {
6771 : 14554 : tree orig_arg0 = arg0;
6772 : 14554 : tree orig_arg1 = arg1;
6773 : 14554 : tree orig_arg2 = arg2;
6774 : 14554 : if (processing_template_decl)
6775 : : {
6776 : 19 : if (type_dependent_expression_p (arg0)
6777 : 13 : || type_dependent_expression_p (arg1)
6778 : 32 : || type_dependent_expression_p (arg2))
6779 : 6 : return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6780 : : }
6781 : 14548 : tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6782 : 14548 : if (processing_template_decl && exp != error_mark_node)
6783 : 13 : return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6784 : 13 : orig_arg1, orig_arg2);
6785 : : return exp;
6786 : : }
6787 : :
6788 : : /* Build a VEC_PERM_EXPR.
6789 : : This is a simple wrapper for c_build_shufflevector. */
6790 : : tree
6791 : 34007 : build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6792 : : tsubst_flags_t complain)
6793 : : {
6794 : 34007 : tree arg0 = (*args)[0];
6795 : 34007 : tree arg1 = (*args)[1];
6796 : 34007 : if (processing_template_decl)
6797 : : {
6798 : 43 : for (unsigned i = 0; i < args->length (); ++i)
6799 : 40 : if (i <= 1
6800 : 40 : ? type_dependent_expression_p ((*args)[i])
6801 : 9 : : instantiation_dependent_expression_p ((*args)[i]))
6802 : : {
6803 : 22 : tree exp = build_min_nt_call_vec (NULL, args);
6804 : 22 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6805 : 22 : return exp;
6806 : : }
6807 : : }
6808 : 33985 : auto_vec<tree, 16> mask;
6809 : 461429 : for (unsigned i = 2; i < args->length (); ++i)
6810 : : {
6811 : 427444 : tree idx = fold_non_dependent_expr ((*args)[i], complain);
6812 : 427444 : mask.safe_push (idx);
6813 : : }
6814 : 33985 : tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6815 : 33985 : if (processing_template_decl && exp != error_mark_node)
6816 : : {
6817 : 3 : exp = build_min_non_dep_call_vec (exp, NULL, args);
6818 : 3 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6819 : : }
6820 : 33985 : return exp;
6821 : 33985 : }
6822 : :
6823 : : /* Return a tree for the sum or difference (RESULTCODE says which)
6824 : : of pointer PTROP and integer INTOP. */
6825 : :
6826 : : static tree
6827 : 3933075 : cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6828 : : tree intop, tsubst_flags_t complain)
6829 : : {
6830 : 3933075 : tree res_type = TREE_TYPE (ptrop);
6831 : :
6832 : : /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6833 : : in certain circumstance (when it's valid to do so). So we need
6834 : : to make sure it's complete. We don't need to check here, if we
6835 : : can actually complete it at all, as those checks will be done in
6836 : : pointer_int_sum() anyway. */
6837 : 3933075 : complete_type (TREE_TYPE (res_type));
6838 : :
6839 : 3933075 : return pointer_int_sum (loc, resultcode, ptrop,
6840 : 3933075 : intop, complain & tf_warning_or_error);
6841 : : }
6842 : :
6843 : : /* Return a tree for the difference of pointers OP0 and OP1.
6844 : : The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6845 : : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6846 : :
6847 : : static tree
6848 : 1194710 : pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6849 : : tsubst_flags_t complain, tree *instrument_expr)
6850 : : {
6851 : 1194710 : tree result, inttype;
6852 : 1194710 : tree restype = ptrdiff_type_node;
6853 : 1194710 : tree target_type = TREE_TYPE (ptrtype);
6854 : :
6855 : 1194710 : if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6856 : 12 : return error_mark_node;
6857 : :
6858 : 1194698 : if (VOID_TYPE_P (target_type))
6859 : : {
6860 : 0 : if (complain & tf_error)
6861 : 0 : permerror (loc, "ISO C++ forbids using pointer of "
6862 : : "type %<void *%> in subtraction");
6863 : : else
6864 : 0 : return error_mark_node;
6865 : : }
6866 : 1194698 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
6867 : : {
6868 : 15 : if (complain & tf_error)
6869 : 3 : permerror (loc, "ISO C++ forbids using pointer to "
6870 : : "a function in subtraction");
6871 : : else
6872 : 12 : return error_mark_node;
6873 : : }
6874 : 1194686 : if (TREE_CODE (target_type) == METHOD_TYPE)
6875 : : {
6876 : 0 : if (complain & tf_error)
6877 : 0 : permerror (loc, "ISO C++ forbids using pointer to "
6878 : : "a method in subtraction");
6879 : : else
6880 : 0 : return error_mark_node;
6881 : : }
6882 : 2389372 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6883 : 1194686 : TREE_TYPE (TREE_TYPE (op0)),
6884 : : !(complain & tf_error))
6885 : 2389372 : || !verify_type_context (loc, TCTX_POINTER_ARITH,
6886 : 1194686 : TREE_TYPE (TREE_TYPE (op1)),
6887 : : !(complain & tf_error)))
6888 : 0 : return error_mark_node;
6889 : :
6890 : : /* Determine integer type result of the subtraction. This will usually
6891 : : be the same as the result type (ptrdiff_t), but may need to be a wider
6892 : : type if pointers for the address space are wider than ptrdiff_t. */
6893 : 1194686 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6894 : 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6895 : : else
6896 : : inttype = restype;
6897 : :
6898 : 1194686 : if (!processing_template_decl
6899 : 1194686 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
6900 : : {
6901 : 84 : op0 = save_expr (op0);
6902 : 84 : op1 = save_expr (op1);
6903 : :
6904 : 84 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
6905 : 84 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6906 : : }
6907 : :
6908 : : /* First do the subtraction, then build the divide operator
6909 : : and only convert at the very end.
6910 : : Do not do default conversions in case restype is a short type. */
6911 : :
6912 : : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6913 : : pointers. If some platform cannot provide that, or has a larger
6914 : : ptrdiff_type to support differences larger than half the address
6915 : : space, cast the pointers to some larger integer type and do the
6916 : : computations in that type. */
6917 : 1194686 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6918 : 0 : op0 = cp_build_binary_op (loc,
6919 : : MINUS_EXPR,
6920 : : cp_convert (inttype, op0, complain),
6921 : : cp_convert (inttype, op1, complain),
6922 : : complain);
6923 : : else
6924 : 1194686 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
6925 : :
6926 : : /* This generates an error if op1 is a pointer to an incomplete type. */
6927 : 1194686 : if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
6928 : : {
6929 : 0 : if (complain & tf_error)
6930 : 0 : error_at (loc, "invalid use of a pointer to an incomplete type in "
6931 : : "pointer arithmetic");
6932 : : else
6933 : 0 : return error_mark_node;
6934 : : }
6935 : :
6936 : 1194686 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
6937 : : {
6938 : 15 : if (complain & tf_error)
6939 : 15 : error_at (loc, "arithmetic on pointer to an empty aggregate");
6940 : : else
6941 : 0 : return error_mark_node;
6942 : : }
6943 : :
6944 : 1194686 : op1 = (TYPE_PTROB_P (ptrtype)
6945 : 2389372 : ? size_in_bytes_loc (loc, target_type)
6946 : : : integer_one_node);
6947 : :
6948 : : /* Do the division. */
6949 : :
6950 : 1194686 : result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
6951 : : cp_convert (inttype, op1, complain));
6952 : 1194686 : return cp_convert (restype, result, complain);
6953 : : }
6954 : :
6955 : : /* Construct and perhaps optimize a tree representation
6956 : : for a unary operation. CODE, a tree_code, specifies the operation
6957 : : and XARG is the operand. */
6958 : :
6959 : : tree
6960 : 53807715 : build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
6961 : : tree lookups, tsubst_flags_t complain)
6962 : : {
6963 : 53807715 : tree orig_expr = xarg;
6964 : 53807715 : tree exp;
6965 : 53807715 : int ptrmem = 0;
6966 : 53807715 : tree overload = NULL_TREE;
6967 : :
6968 : 53807715 : if (processing_template_decl)
6969 : : {
6970 : 37447687 : if (type_dependent_expression_p (xarg))
6971 : : {
6972 : 24821897 : tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
6973 : 24821897 : TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
6974 : 24821897 : return e;
6975 : : }
6976 : : }
6977 : :
6978 : 28985818 : exp = NULL_TREE;
6979 : :
6980 : : /* [expr.unary.op] says:
6981 : :
6982 : : The address of an object of incomplete type can be taken.
6983 : :
6984 : : (And is just the ordinary address operator, not an overloaded
6985 : : "operator &".) However, if the type is a template
6986 : : specialization, we must complete the type at this point so that
6987 : : an overloaded "operator &" will be available if required. */
6988 : 28985818 : if (code == ADDR_EXPR
6989 : 3297208 : && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
6990 : 32282624 : && ((CLASS_TYPE_P (TREE_TYPE (xarg))
6991 : 1591833 : && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
6992 : 3294634 : || (TREE_CODE (xarg) == OFFSET_REF)))
6993 : : /* Don't look for a function. */;
6994 : : else
6995 : 28919460 : exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
6996 : : NULL_TREE, lookups, &overload, complain);
6997 : :
6998 : 28985818 : if (!exp && code == ADDR_EXPR)
6999 : : {
7000 : 3296945 : if (is_overloaded_fn (xarg))
7001 : : {
7002 : 356063 : tree fn = get_first_fn (xarg);
7003 : 712126 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
7004 : : {
7005 : 12 : if (complain & tf_error)
7006 : 21 : error_at (loc, DECL_CONSTRUCTOR_P (fn)
7007 : : ? G_("taking address of constructor %qD")
7008 : : : G_("taking address of destructor %qD"),
7009 : : fn);
7010 : 12 : return error_mark_node;
7011 : : }
7012 : : }
7013 : :
7014 : : /* A pointer to member-function can be formed only by saying
7015 : : &X::mf. */
7016 : 3296927 : if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
7017 : 3357561 : && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
7018 : : {
7019 : 9 : if (TREE_CODE (xarg) != OFFSET_REF
7020 : 9 : || !TYPE_P (TREE_OPERAND (xarg, 0)))
7021 : : {
7022 : 9 : if (complain & tf_error)
7023 : : {
7024 : 9 : auto_diagnostic_group d;
7025 : 9 : error_at (loc, "invalid use of %qE to form a "
7026 : : "pointer-to-member-function", xarg.get_value ());
7027 : 9 : if (TREE_CODE (xarg) != OFFSET_REF)
7028 : 6 : inform (loc, " a qualified-id is required");
7029 : 9 : }
7030 : 9 : return error_mark_node;
7031 : : }
7032 : : else
7033 : : {
7034 : 0 : if (complain & tf_error)
7035 : 0 : error_at (loc, "parentheses around %qE cannot be used to "
7036 : : "form a pointer-to-member-function",
7037 : : xarg.get_value ());
7038 : : else
7039 : 0 : return error_mark_node;
7040 : 0 : PTRMEM_OK_P (xarg) = 1;
7041 : : }
7042 : : }
7043 : :
7044 : 3296924 : if (TREE_CODE (xarg) == OFFSET_REF)
7045 : : {
7046 : 64171 : ptrmem = PTRMEM_OK_P (xarg);
7047 : :
7048 : 0 : if (!ptrmem && !flag_ms_extensions
7049 : 64171 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
7050 : : {
7051 : : /* A single non-static member, make sure we don't allow a
7052 : : pointer-to-member. */
7053 : 0 : xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
7054 : 0 : TREE_OPERAND (xarg, 0),
7055 : 0 : ovl_make (TREE_OPERAND (xarg, 1)));
7056 : 0 : PTRMEM_OK_P (xarg) = ptrmem;
7057 : : }
7058 : : }
7059 : :
7060 : 6593848 : exp = cp_build_addr_expr_strict (xarg, complain);
7061 : :
7062 : 3296924 : if (TREE_CODE (exp) == PTRMEM_CST)
7063 : 63294 : PTRMEM_CST_LOCATION (exp) = loc;
7064 : : else
7065 : 3233630 : protected_set_expr_location (exp, loc);
7066 : : }
7067 : :
7068 : 28985797 : if (processing_template_decl && exp != error_mark_node)
7069 : : {
7070 : 12625678 : if (overload != NULL_TREE)
7071 : 157742 : return (build_min_non_dep_op_overload
7072 : 157742 : (code, exp, overload, orig_expr, integer_zero_node));
7073 : :
7074 : 12467936 : exp = build_min_non_dep (code, exp, orig_expr,
7075 : : /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
7076 : : }
7077 : 28828055 : if (TREE_CODE (exp) == ADDR_EXPR)
7078 : 2527926 : PTRMEM_OK_P (exp) = ptrmem;
7079 : : return exp;
7080 : : }
7081 : :
7082 : : /* Construct and perhaps optimize a tree representation
7083 : : for __builtin_addressof operation. ARG specifies the operand. */
7084 : :
7085 : : tree
7086 : 411279 : cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7087 : : {
7088 : 411279 : tree orig_expr = arg;
7089 : :
7090 : 411279 : if (processing_template_decl)
7091 : : {
7092 : 144687 : if (type_dependent_expression_p (arg))
7093 : 144687 : return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7094 : : }
7095 : :
7096 : 533184 : tree exp = cp_build_addr_expr_strict (arg, complain);
7097 : :
7098 : 266592 : if (processing_template_decl && exp != error_mark_node)
7099 : 0 : exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
7100 : : return exp;
7101 : : }
7102 : :
7103 : : /* Like c_common_truthvalue_conversion, but handle pointer-to-member
7104 : : constants, where a null value is represented by an INTEGER_CST of
7105 : : -1. */
7106 : :
7107 : : tree
7108 : 6774908 : cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7109 : : {
7110 : 6774908 : tree type = TREE_TYPE (expr);
7111 : 6774908 : location_t loc = cp_expr_loc_or_input_loc (expr);
7112 : 5724295 : if (TYPE_PTR_OR_PTRMEM_P (type)
7113 : : /* Avoid ICE on invalid use of non-static member function. */
7114 : 12498925 : || TREE_CODE (expr) == FUNCTION_DECL)
7115 : 1050891 : return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
7116 : : else
7117 : 5724017 : return c_common_truthvalue_conversion (loc, expr);
7118 : : }
7119 : :
7120 : : /* Returns EXPR contextually converted to bool. */
7121 : :
7122 : : tree
7123 : 42698100 : contextual_conv_bool (tree expr, tsubst_flags_t complain)
7124 : : {
7125 : 42698100 : return perform_implicit_conversion_flags (boolean_type_node, expr,
7126 : 42698100 : complain, LOOKUP_NORMAL);
7127 : : }
7128 : :
7129 : : /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7130 : : is a low-level function; most callers should use maybe_convert_cond. */
7131 : :
7132 : : tree
7133 : 38114475 : condition_conversion (tree expr)
7134 : : {
7135 : 38114475 : tree t = contextual_conv_bool (expr, tf_warning_or_error);
7136 : 38114475 : if (!processing_template_decl)
7137 : 19092590 : t = fold_build_cleanup_point_expr (boolean_type_node, t);
7138 : 38114475 : return t;
7139 : : }
7140 : :
7141 : : /* Returns the address of T. This function will fold away
7142 : : ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7143 : : most places should use cp_build_addr_expr instead. */
7144 : :
7145 : : tree
7146 : 244709145 : build_address (tree t)
7147 : : {
7148 : 244709145 : if (error_operand_p (t) || !cxx_mark_addressable (t))
7149 : 21 : return error_mark_node;
7150 : 244709124 : gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7151 : : || processing_template_decl);
7152 : 244709124 : t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7153 : 244709124 : if (TREE_CODE (t) != ADDR_EXPR)
7154 : 43898494 : t = rvalue (t);
7155 : : return t;
7156 : : }
7157 : :
7158 : : /* Return a NOP_EXPR converting EXPR to TYPE. */
7159 : :
7160 : : tree
7161 : 425080000 : build_nop (tree type, tree expr MEM_STAT_DECL)
7162 : : {
7163 : 425080000 : if (type == error_mark_node || error_operand_p (expr))
7164 : : return expr;
7165 : 425079924 : return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr PASS_MEM_STAT);
7166 : : }
7167 : :
7168 : : /* Take the address of ARG, whatever that means under C++ semantics.
7169 : : If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7170 : : and class rvalues as well.
7171 : :
7172 : : Nothing should call this function directly; instead, callers should use
7173 : : cp_build_addr_expr or cp_build_addr_expr_strict. */
7174 : :
7175 : : static tree
7176 : 182681678 : cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7177 : : {
7178 : 182681678 : tree argtype;
7179 : 182681678 : tree val;
7180 : :
7181 : 182681678 : if (!arg || error_operand_p (arg))
7182 : 12 : return error_mark_node;
7183 : :
7184 : 182681666 : arg = mark_lvalue_use (arg);
7185 : 182681666 : if (error_operand_p (arg))
7186 : 6 : return error_mark_node;
7187 : :
7188 : 182681660 : argtype = lvalue_type (arg);
7189 : 182681660 : location_t loc = cp_expr_loc_or_input_loc (arg);
7190 : :
7191 : 182681660 : gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7192 : :
7193 : 11853977 : if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7194 : 182681854 : && !really_overloaded_fn (arg))
7195 : : {
7196 : : /* They're trying to take the address of a unique non-static
7197 : : member function. This is ill-formed (except in MS-land),
7198 : : but let's try to DTRT.
7199 : : Note: We only handle unique functions here because we don't
7200 : : want to complain if there's a static overload; non-unique
7201 : : cases will be handled by instantiate_type. But we need to
7202 : : handle this case here to allow casts on the resulting PMF.
7203 : : We could defer this in non-MS mode, but it's easier to give
7204 : : a useful error here. */
7205 : :
7206 : : /* Inside constant member functions, the `this' pointer
7207 : : contains an extra const qualifier. TYPE_MAIN_VARIANT
7208 : : is used here to remove this const from the diagnostics
7209 : : and the created OFFSET_REF. */
7210 : 70 : tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7211 : 70 : tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7212 : 70 : if (!mark_used (fn, complain) && !(complain & tf_error))
7213 : 0 : return error_mark_node;
7214 : : /* Until microsoft headers are known to incorrectly take the address of
7215 : : unqualified xobj member functions we should not support this
7216 : : extension.
7217 : : See comment in class.cc:resolve_address_of_overloaded_function for
7218 : : the extended reasoning. */
7219 : 70 : if (!flag_ms_extensions || DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7220 : : {
7221 : 64 : auto_diagnostic_group d;
7222 : 64 : tree name = DECL_NAME (fn);
7223 : 64 : if (!(complain & tf_error))
7224 : 0 : return error_mark_node;
7225 : 64 : else if (current_class_type
7226 : 64 : && TREE_OPERAND (arg, 0) == current_class_ref)
7227 : : /* An expression like &memfn. */
7228 : 31 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7229 : 21 : permerror (loc,
7230 : : "ISO C++ forbids taking the address of an unqualified"
7231 : : " or parenthesized non-static member function to form"
7232 : : " a pointer to member function. Say %<&%T::%D%>",
7233 : : base, name);
7234 : : else
7235 : 10 : error_at (loc,
7236 : : "ISO C++ forbids taking the address of an unqualified"
7237 : : " or parenthesized non-static member function to form"
7238 : : " a pointer to explicit object member function");
7239 : : else
7240 : 33 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7241 : 9 : permerror (loc,
7242 : : "ISO C++ forbids taking the address of a bound member"
7243 : : " function to form a pointer to member function."
7244 : : " Say %<&%T::%D%>",
7245 : : base, name);
7246 : : else
7247 : 24 : error_at (loc,
7248 : : "ISO C++ forbids taking the address of a bound member"
7249 : : " function to form a pointer to explicit object member"
7250 : : " function");
7251 : 64 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7252 : 34 : inform (loc,
7253 : : "a pointer to explicit object member function can only be "
7254 : : "formed with %<&%T::%D%>", base, name);
7255 : 64 : }
7256 : 70 : arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7257 : : }
7258 : :
7259 : : /* Uninstantiated types are all functions. Taking the
7260 : : address of a function is a no-op, so just return the
7261 : : argument. */
7262 : 182681660 : if (type_unknown_p (arg))
7263 : 1745 : return build1 (ADDR_EXPR, unknown_type_node, arg);
7264 : :
7265 : 182679915 : if (TREE_CODE (arg) == OFFSET_REF)
7266 : : /* We want a pointer to member; bypass all the code for actually taking
7267 : : the address of something. */
7268 : 63413 : goto offset_ref;
7269 : :
7270 : : /* Anything not already handled and not a true memory reference
7271 : : is an error. */
7272 : 182616502 : if (!FUNC_OR_METHOD_TYPE_P (argtype))
7273 : : {
7274 : 95708176 : cp_lvalue_kind kind = lvalue_kind (arg);
7275 : 95708176 : if (kind == clk_none)
7276 : : {
7277 : 23 : if (complain & tf_error)
7278 : 23 : lvalue_error (loc, lv_addressof);
7279 : 23 : return error_mark_node;
7280 : : }
7281 : 95708153 : if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7282 : : {
7283 : 36 : if (!(complain & tf_error))
7284 : 9 : return error_mark_node;
7285 : : /* Make this a permerror because we used to accept it. */
7286 : 27 : permerror (loc, "taking address of rvalue");
7287 : : }
7288 : : }
7289 : :
7290 : 182616470 : if (TYPE_REF_P (argtype))
7291 : : {
7292 : 225 : tree type = build_pointer_type (TREE_TYPE (argtype));
7293 : 225 : arg = build1 (CONVERT_EXPR, type, arg);
7294 : 225 : return arg;
7295 : : }
7296 : 182616245 : else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7297 : : {
7298 : : /* ARM $3.4 */
7299 : : /* Apparently a lot of autoconf scripts for C++ packages do this,
7300 : : so only complain if -Wpedantic. */
7301 : 9 : if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7302 : 9 : pedwarn (loc, OPT_Wpedantic,
7303 : : "ISO C++ forbids taking address of function %<::main%>");
7304 : 0 : else if (flag_pedantic_errors)
7305 : 0 : return error_mark_node;
7306 : : }
7307 : :
7308 : : /* Let &* cancel out to simplify resulting code. */
7309 : 182616245 : if (INDIRECT_REF_P (arg))
7310 : : {
7311 : 59229777 : arg = TREE_OPERAND (arg, 0);
7312 : 59229777 : if (TYPE_REF_P (TREE_TYPE (arg)))
7313 : : {
7314 : 32957792 : tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7315 : 32957792 : arg = build1 (CONVERT_EXPR, type, arg);
7316 : : }
7317 : : else
7318 : : /* Don't let this be an lvalue. */
7319 : 26271985 : arg = rvalue (arg);
7320 : 59229777 : return arg;
7321 : : }
7322 : :
7323 : : /* Handle complex lvalues (when permitted)
7324 : : by reduction to simpler cases. */
7325 : 123386468 : val = unary_complex_lvalue (ADDR_EXPR, arg);
7326 : 123386468 : if (val != 0)
7327 : : return val;
7328 : :
7329 : 122927867 : switch (TREE_CODE (arg))
7330 : : {
7331 : 0 : CASE_CONVERT:
7332 : 0 : case FLOAT_EXPR:
7333 : 0 : case FIX_TRUNC_EXPR:
7334 : : /* We should have handled this above in the lvalue_kind check. */
7335 : 0 : gcc_unreachable ();
7336 : 59853 : break;
7337 : :
7338 : 59853 : case BASELINK:
7339 : 59853 : arg = BASELINK_FUNCTIONS (arg);
7340 : : /* Fall through. */
7341 : :
7342 : 59853 : case OVERLOAD:
7343 : 59853 : arg = OVL_FIRST (arg);
7344 : : break;
7345 : :
7346 : 63413 : case OFFSET_REF:
7347 : 63413 : offset_ref:
7348 : : /* Turn a reference to a non-static data member into a
7349 : : pointer-to-member. */
7350 : 63413 : {
7351 : 63413 : tree type;
7352 : 63413 : tree t;
7353 : :
7354 : 63413 : gcc_assert (PTRMEM_OK_P (arg));
7355 : :
7356 : 63413 : t = TREE_OPERAND (arg, 1);
7357 : 63413 : if (TYPE_REF_P (TREE_TYPE (t)))
7358 : : {
7359 : 15 : if (complain & tf_error)
7360 : 15 : error_at (loc,
7361 : : "cannot create pointer to reference member %qD", t);
7362 : 15 : return error_mark_node;
7363 : : }
7364 : :
7365 : : /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7366 : 63398 : if (TREE_CODE (t) == FUNCTION_DECL
7367 : 60756 : && !DECL_PURE_VIRTUAL_P (t)
7368 : 124133 : && !mark_used (t, complain) && !(complain & tf_error))
7369 : 3 : return error_mark_node;
7370 : :
7371 : : /* Pull out the function_decl for a single xobj member function, and
7372 : : let the rest of this function handle it. This is similar to how
7373 : : static member functions are handled in the BASELINK case above. */
7374 : 63395 : if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7375 : : {
7376 : : arg = t;
7377 : : break;
7378 : : }
7379 : :
7380 : 63339 : type = build_ptrmem_type (context_for_name_lookup (t),
7381 : 63339 : TREE_TYPE (t));
7382 : 63339 : t = make_ptrmem_cst (type, t);
7383 : 63339 : return t;
7384 : : }
7385 : :
7386 : : default:
7387 : : break;
7388 : : }
7389 : :
7390 : 122927923 : if (argtype != error_mark_node)
7391 : 122927923 : argtype = build_pointer_type (argtype);
7392 : :
7393 : 122927923 : if (bitfield_p (arg))
7394 : : {
7395 : 33 : if (complain & tf_error)
7396 : 33 : error_at (loc, "attempt to take address of bit-field");
7397 : 33 : return error_mark_node;
7398 : : }
7399 : :
7400 : : /* In a template, we are processing a non-dependent expression
7401 : : so we can just form an ADDR_EXPR with the correct type. */
7402 : 122927890 : if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7403 : : {
7404 : 111350881 : if (!mark_single_function (arg, complain))
7405 : 3 : return error_mark_node;
7406 : 111350878 : val = build_address (arg);
7407 : 111350878 : if (TREE_CODE (arg) == OFFSET_REF)
7408 : 0 : PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7409 : : }
7410 : 11577009 : else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7411 : : {
7412 : 27 : tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7413 : :
7414 : 27 : if (TREE_CODE (fn) == OVERLOAD && OVL_SINGLE_P (fn))
7415 : 27 : fn = OVL_FIRST (fn);
7416 : :
7417 : : /* We can only get here with a single static member
7418 : : function. */
7419 : 27 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7420 : : && DECL_STATIC_FUNCTION_P (fn));
7421 : 27 : if (!mark_used (fn, complain) && !(complain & tf_error))
7422 : 0 : return error_mark_node;
7423 : 27 : val = build_address (fn);
7424 : 27 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7425 : : /* Do not lose object's side effects. */
7426 : 12 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7427 : 12 : TREE_OPERAND (arg, 0), val);
7428 : : }
7429 : : else
7430 : : {
7431 : 11576982 : tree object = TREE_OPERAND (arg, 0);
7432 : 11576982 : tree field = TREE_OPERAND (arg, 1);
7433 : 11576982 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7434 : : (TREE_TYPE (object), decl_type_context (field)));
7435 : 11576982 : val = build_address (arg);
7436 : : }
7437 : :
7438 : 122927887 : if (TYPE_PTR_P (argtype)
7439 : 122927887 : && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7440 : : {
7441 : 1063 : build_ptrmemfunc_type (argtype);
7442 : 1063 : val = build_ptrmemfunc (argtype, val, 0,
7443 : : /*c_cast_p=*/false,
7444 : : complain);
7445 : : }
7446 : :
7447 : : /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7448 : 122927887 : if (TREE_CODE (val) == ADDR_EXPR
7449 : 122927887 : && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7450 : 85837997 : SET_EXPR_LOCATION (val, input_location);
7451 : :
7452 : : return val;
7453 : : }
7454 : :
7455 : : /* Take the address of ARG if it has one, even if it's an rvalue. */
7456 : :
7457 : : tree
7458 : 179118162 : cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7459 : : {
7460 : 179118162 : return cp_build_addr_expr_1 (arg, 0, complain);
7461 : : }
7462 : :
7463 : : /* Take the address of ARG, but only if it's an lvalue. */
7464 : :
7465 : : static tree
7466 : 3563516 : cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7467 : : {
7468 : 3563516 : return cp_build_addr_expr_1 (arg, 1, complain);
7469 : : }
7470 : :
7471 : : /* C++: Must handle pointers to members.
7472 : :
7473 : : Perhaps type instantiation should be extended to handle conversion
7474 : : from aggregates to types we don't yet know we want? (Or are those
7475 : : cases typically errors which should be reported?)
7476 : :
7477 : : NOCONVERT suppresses the default promotions (such as from short to int). */
7478 : :
7479 : : tree
7480 : 25972165 : cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7481 : : tsubst_flags_t complain)
7482 : : {
7483 : : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7484 : 25972165 : tree arg = xarg;
7485 : 25972165 : location_t location = cp_expr_loc_or_input_loc (arg);
7486 : 25972165 : tree argtype = 0;
7487 : 25972165 : tree eptype = NULL_TREE;
7488 : 25972165 : const char *errstring = NULL;
7489 : 25972165 : tree val;
7490 : 25972165 : const char *invalid_op_diag;
7491 : :
7492 : 25972165 : if (!arg || error_operand_p (arg))
7493 : 0 : return error_mark_node;
7494 : :
7495 : 25972165 : arg = resolve_nondeduced_context (arg, complain);
7496 : :
7497 : 51944330 : if ((invalid_op_diag
7498 : 25972165 : = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7499 : : ? CONVERT_EXPR
7500 : : : code),
7501 : 25972165 : TREE_TYPE (arg))))
7502 : : {
7503 : 0 : if (complain & tf_error)
7504 : 0 : error (invalid_op_diag);
7505 : 0 : return error_mark_node;
7506 : : }
7507 : :
7508 : 25972165 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7509 : : {
7510 : 8532 : eptype = TREE_TYPE (arg);
7511 : 8532 : arg = TREE_OPERAND (arg, 0);
7512 : : }
7513 : :
7514 : 25972165 : switch (code)
7515 : : {
7516 : 2328572 : case UNARY_PLUS_EXPR:
7517 : 2328572 : case NEGATE_EXPR:
7518 : 2328572 : {
7519 : 2328572 : int flags = WANT_ARITH | WANT_ENUM;
7520 : : /* Unary plus (but not unary minus) is allowed on pointers. */
7521 : 2328572 : if (code == UNARY_PLUS_EXPR)
7522 : 161980 : flags |= WANT_POINTER;
7523 : 2328572 : arg = build_expr_type_conversion (flags, arg, true);
7524 : 2328572 : if (!arg)
7525 : 30 : errstring = (code == NEGATE_EXPR
7526 : 24 : ? _("wrong type argument to unary minus")
7527 : 6 : : _("wrong type argument to unary plus"));
7528 : : else
7529 : : {
7530 : 2328542 : if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7531 : 578733 : arg = cp_perform_integral_promotions (arg, complain);
7532 : :
7533 : : /* Make sure the result is not an lvalue: a unary plus or minus
7534 : : expression is always a rvalue. */
7535 : 2328542 : arg = rvalue (arg);
7536 : : }
7537 : : }
7538 : : break;
7539 : :
7540 : 813099 : case BIT_NOT_EXPR:
7541 : 813099 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7542 : : {
7543 : 15 : code = CONJ_EXPR;
7544 : 15 : if (!noconvert)
7545 : : {
7546 : 15 : arg = cp_default_conversion (arg, complain);
7547 : 15 : if (arg == error_mark_node)
7548 : : return error_mark_node;
7549 : : }
7550 : : }
7551 : 813084 : else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7552 : : | WANT_VECTOR_OR_COMPLEX,
7553 : : arg, true)))
7554 : 13 : errstring = _("wrong type argument to bit-complement");
7555 : 813071 : else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7556 : : {
7557 : : /* Warn if the expression has boolean value. */
7558 : 812836 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7559 : 812836 : && (complain & tf_warning))
7560 : : {
7561 : 61 : auto_diagnostic_group d;
7562 : 61 : if (warning_at (location, OPT_Wbool_operation,
7563 : : "%<~%> on an expression of type %<bool%>"))
7564 : 42 : inform (location, "did you mean to use logical not (%<!%>)?");
7565 : 61 : }
7566 : 812836 : arg = cp_perform_integral_promotions (arg, complain);
7567 : : }
7568 : 235 : else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7569 : 235 : arg = mark_rvalue_use (arg);
7570 : : break;
7571 : :
7572 : 0 : case ABS_EXPR:
7573 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7574 : 0 : errstring = _("wrong type argument to abs");
7575 : 0 : else if (!noconvert)
7576 : : {
7577 : 0 : arg = cp_default_conversion (arg, complain);
7578 : 0 : if (arg == error_mark_node)
7579 : : return error_mark_node;
7580 : : }
7581 : : break;
7582 : :
7583 : 0 : case CONJ_EXPR:
7584 : : /* Conjugating a real value is a no-op, but allow it anyway. */
7585 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7586 : 0 : errstring = _("wrong type argument to conjugation");
7587 : 0 : else if (!noconvert)
7588 : : {
7589 : 0 : arg = cp_default_conversion (arg, complain);
7590 : 0 : if (arg == error_mark_node)
7591 : : return error_mark_node;
7592 : : }
7593 : : break;
7594 : :
7595 : 14548256 : case TRUTH_NOT_EXPR:
7596 : 14548256 : if (gnu_vector_type_p (TREE_TYPE (arg)))
7597 : 60 : return cp_build_binary_op (input_location, EQ_EXPR, arg,
7598 : 60 : build_zero_cst (TREE_TYPE (arg)), complain);
7599 : 14548196 : arg = perform_implicit_conversion (boolean_type_node, arg,
7600 : : complain);
7601 : 14548196 : if (arg != error_mark_node)
7602 : : {
7603 : 14548185 : if (processing_template_decl)
7604 : 6831403 : return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
7605 : 7716782 : val = invert_truthvalue_loc (location, arg);
7606 : 7716782 : if (obvalue_p (val))
7607 : 12875 : val = non_lvalue_loc (location, val);
7608 : 7716782 : return val;
7609 : : }
7610 : 11 : errstring = _("in argument to unary !");
7611 : 11 : break;
7612 : :
7613 : : case NOP_EXPR:
7614 : : break;
7615 : :
7616 : 457311 : case REALPART_EXPR:
7617 : 457311 : case IMAGPART_EXPR:
7618 : 457311 : val = build_real_imag_expr (input_location, code, arg);
7619 : 457311 : if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
7620 : 0 : val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
7621 : 0 : TREE_TYPE (eptype), val);
7622 : : return val;
7623 : :
7624 : 7269568 : case PREINCREMENT_EXPR:
7625 : 7269568 : case POSTINCREMENT_EXPR:
7626 : 7269568 : case PREDECREMENT_EXPR:
7627 : 7269568 : case POSTDECREMENT_EXPR:
7628 : : /* Handle complex lvalues (when permitted)
7629 : : by reduction to simpler cases. */
7630 : :
7631 : 7269568 : val = unary_complex_lvalue (code, arg);
7632 : 7269568 : if (val != 0)
7633 : 30 : goto return_build_unary_op;
7634 : :
7635 : 7269538 : arg = mark_lvalue_use (arg);
7636 : :
7637 : : /* Increment or decrement the real part of the value,
7638 : : and don't change the imaginary part. */
7639 : 7269538 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7640 : : {
7641 : 18 : tree real, imag;
7642 : :
7643 : 18 : arg = cp_stabilize_reference (arg);
7644 : 18 : real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7645 : 18 : imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7646 : 18 : real = cp_build_unary_op (code, real, true, complain);
7647 : 18 : if (real == error_mark_node || imag == error_mark_node)
7648 : : return error_mark_node;
7649 : 15 : val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
7650 : 15 : goto return_build_unary_op;
7651 : : }
7652 : :
7653 : : /* Report invalid types. */
7654 : :
7655 : 7269520 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7656 : : arg, true)))
7657 : : {
7658 : 36 : if (code == PREINCREMENT_EXPR)
7659 : 9 : errstring = _("no pre-increment operator for type");
7660 : 27 : else if (code == POSTINCREMENT_EXPR)
7661 : 9 : errstring = _("no post-increment operator for type");
7662 : 18 : else if (code == PREDECREMENT_EXPR)
7663 : 9 : errstring = _("no pre-decrement operator for type");
7664 : : else
7665 : 9 : errstring = _("no post-decrement operator for type");
7666 : : break;
7667 : : }
7668 : 7269484 : else if (arg == error_mark_node)
7669 : : return error_mark_node;
7670 : :
7671 : : /* Report something read-only. */
7672 : :
7673 : 7269484 : if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7674 : 7269484 : || TREE_READONLY (arg))
7675 : : {
7676 : 61 : if (complain & tf_error)
7677 : 61 : cxx_readonly_error (location, arg,
7678 : 61 : ((code == PREINCREMENT_EXPR
7679 : 61 : || code == POSTINCREMENT_EXPR)
7680 : : ? lv_increment : lv_decrement));
7681 : : else
7682 : 0 : return error_mark_node;
7683 : : }
7684 : :
7685 : 7269484 : {
7686 : 7269484 : tree inc;
7687 : 7269484 : tree declared_type = unlowered_expr_type (arg);
7688 : :
7689 : 7269484 : argtype = TREE_TYPE (arg);
7690 : :
7691 : : /* ARM $5.2.5 last annotation says this should be forbidden. */
7692 : 7269484 : if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7693 : : {
7694 : 0 : if (complain & tf_error)
7695 : 0 : permerror (location, (code == PREINCREMENT_EXPR
7696 : 0 : || code == POSTINCREMENT_EXPR)
7697 : : ? G_("ISO C++ forbids incrementing an enum")
7698 : : : G_("ISO C++ forbids decrementing an enum"));
7699 : : else
7700 : 0 : return error_mark_node;
7701 : : }
7702 : :
7703 : : /* Compute the increment. */
7704 : :
7705 : 7269484 : if (TYPE_PTR_P (argtype))
7706 : : {
7707 : 1991698 : tree type = complete_type (TREE_TYPE (argtype));
7708 : :
7709 : 1991698 : if (!COMPLETE_OR_VOID_TYPE_P (type))
7710 : : {
7711 : 11 : if (complain & tf_error)
7712 : 9 : error_at (location, ((code == PREINCREMENT_EXPR
7713 : 9 : || code == POSTINCREMENT_EXPR))
7714 : : ? G_("cannot increment a pointer to incomplete "
7715 : : "type %qT")
7716 : : : G_("cannot decrement a pointer to incomplete "
7717 : : "type %qT"),
7718 : 9 : TREE_TYPE (argtype));
7719 : : else
7720 : 2 : return error_mark_node;
7721 : : }
7722 : 1991687 : else if (!TYPE_PTROB_P (argtype))
7723 : : {
7724 : 75 : if (complain & tf_error)
7725 : 63 : pedwarn (location, OPT_Wpointer_arith,
7726 : 63 : (code == PREINCREMENT_EXPR
7727 : 63 : || code == POSTINCREMENT_EXPR)
7728 : : ? G_("ISO C++ forbids incrementing a pointer "
7729 : : "of type %qT")
7730 : : : G_("ISO C++ forbids decrementing a pointer "
7731 : : "of type %qT"),
7732 : : argtype);
7733 : : else
7734 : 12 : return error_mark_node;
7735 : : }
7736 : 1991612 : else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7737 : 1991612 : TREE_TYPE (argtype),
7738 : : !(complain & tf_error)))
7739 : 0 : return error_mark_node;
7740 : :
7741 : 1991684 : inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7742 : : }
7743 : : else
7744 : 5277786 : inc = VECTOR_TYPE_P (argtype)
7745 : 5277786 : ? build_one_cst (argtype)
7746 : : : integer_one_node;
7747 : :
7748 : 7269470 : inc = cp_convert (argtype, inc, complain);
7749 : :
7750 : : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7751 : : need to ask Objective-C to build the increment or decrement
7752 : : expression for it. */
7753 : 7269470 : if (objc_is_property_ref (arg))
7754 : 0 : return objc_build_incr_expr_for_property_ref (input_location, code,
7755 : 0 : arg, inc);
7756 : :
7757 : : /* Complain about anything else that is not a true lvalue. */
7758 : 7269470 : if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7759 : 7269470 : || code == POSTINCREMENT_EXPR)
7760 : : ? lv_increment : lv_decrement),
7761 : : complain))
7762 : 33 : return error_mark_node;
7763 : :
7764 : : /* [depr.volatile.type] "Postfix ++ and -- expressions and
7765 : : prefix ++ and -- expressions of volatile-qualified arithmetic
7766 : : and pointer types are deprecated." */
7767 : 7269088 : if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7768 : 7269437 : && (complain & tf_warning))
7769 : 462 : warning_at (location, OPT_Wvolatile,
7770 : : "%qs expression of %<volatile%>-qualified type is "
7771 : : "deprecated",
7772 : : ((code == PREINCREMENT_EXPR
7773 : : || code == POSTINCREMENT_EXPR)
7774 : : ? "++" : "--"));
7775 : :
7776 : : /* Forbid using -- or ++ in C++17 on `bool'. */
7777 : 7269437 : if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7778 : : {
7779 : 95 : if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7780 : : {
7781 : 33 : if (complain & tf_error)
7782 : 33 : error_at (location,
7783 : : "use of an operand of type %qT in %<operator--%> "
7784 : : "is forbidden", boolean_type_node);
7785 : 33 : return error_mark_node;
7786 : : }
7787 : : else
7788 : : {
7789 : 62 : if (cxx_dialect >= cxx17)
7790 : : {
7791 : 33 : if (complain & tf_error)
7792 : 32 : error_at (location,
7793 : : "use of an operand of type %qT in "
7794 : : "%<operator++%> is forbidden in C++17",
7795 : : boolean_type_node);
7796 : 33 : return error_mark_node;
7797 : : }
7798 : : /* Otherwise, [depr.incr.bool] says this is deprecated. */
7799 : 29 : else if (complain & tf_warning)
7800 : 25 : warning_at (location, OPT_Wdeprecated,
7801 : : "use of an operand of type %qT "
7802 : : "in %<operator++%> is deprecated",
7803 : : boolean_type_node);
7804 : : }
7805 : 29 : val = boolean_increment (code, arg);
7806 : : }
7807 : 7269342 : else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7808 : : /* An rvalue has no cv-qualifiers. */
7809 : 990183 : val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7810 : : else
7811 : 6279159 : val = build2 (code, TREE_TYPE (arg), arg, inc);
7812 : :
7813 : 7269371 : TREE_SIDE_EFFECTS (val) = 1;
7814 : 7269371 : goto return_build_unary_op;
7815 : : }
7816 : :
7817 : 555359 : case ADDR_EXPR:
7818 : : /* Note that this operation never does default_conversion
7819 : : regardless of NOCONVERT. */
7820 : 555359 : return cp_build_addr_expr (arg, complain);
7821 : :
7822 : : default:
7823 : : break;
7824 : : }
7825 : :
7826 : 3141703 : if (!errstring)
7827 : : {
7828 : 3141628 : if (argtype == 0)
7829 : 3141628 : argtype = TREE_TYPE (arg);
7830 : 3141628 : val = build1 (code, argtype, arg);
7831 : 10411044 : return_build_unary_op:
7832 : 10411044 : if (eptype)
7833 : 8523 : val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
7834 : 10411044 : return val;
7835 : : }
7836 : :
7837 : 90 : if (complain & tf_error)
7838 : 48 : error_at (location, "%s", errstring);
7839 : 90 : return error_mark_node;
7840 : : }
7841 : :
7842 : : /* Hook for the c-common bits that build a unary op. */
7843 : : tree
7844 : 4139 : build_unary_op (location_t /*location*/,
7845 : : enum tree_code code, tree xarg, bool noconvert)
7846 : : {
7847 : 4139 : return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
7848 : : }
7849 : :
7850 : : /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7851 : : so that it is a valid lvalue even for GENERIC by replacing
7852 : : (lhs = rhs) with ((lhs = rhs), lhs)
7853 : : (--lhs) with ((--lhs), lhs)
7854 : : (++lhs) with ((++lhs), lhs)
7855 : : and if lhs has side-effects, calling cp_stabilize_reference on it, so
7856 : : that it can be evaluated multiple times. */
7857 : :
7858 : : tree
7859 : 365437 : genericize_compound_lvalue (tree lvalue)
7860 : : {
7861 : 365437 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7862 : 188 : lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7863 : 188 : cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7864 : 188 : TREE_OPERAND (lvalue, 1));
7865 : 365437 : return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7866 : 365437 : lvalue, TREE_OPERAND (lvalue, 0));
7867 : : }
7868 : :
7869 : : /* Apply unary lvalue-demanding operator CODE to the expression ARG
7870 : : for certain kinds of expressions which are not really lvalues
7871 : : but which we can accept as lvalues.
7872 : :
7873 : : If ARG is not a kind of expression we can handle, return
7874 : : NULL_TREE. */
7875 : :
7876 : : tree
7877 : 234179487 : unary_complex_lvalue (enum tree_code code, tree arg)
7878 : : {
7879 : : /* Inside a template, making these kinds of adjustments is
7880 : : pointless; we are only concerned with the type of the
7881 : : expression. */
7882 : 234544718 : if (processing_template_decl)
7883 : : return NULL_TREE;
7884 : :
7885 : : /* Handle (a, b) used as an "lvalue". */
7886 : 159332529 : if (TREE_CODE (arg) == COMPOUND_EXPR)
7887 : : {
7888 : 365669 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7889 : : tf_warning_or_error);
7890 : 365669 : return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7891 : 731338 : TREE_OPERAND (arg, 0), real_result);
7892 : : }
7893 : :
7894 : : /* Handle (a ? b : c) used as an "lvalue". */
7895 : 158966860 : if (TREE_CODE (arg) == COND_EXPR
7896 : 158873908 : || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7897 : 92952 : return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7898 : :
7899 : : /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7900 : 158873908 : if (TREE_CODE (arg) == MODIFY_EXPR
7901 : 158508854 : || TREE_CODE (arg) == PREINCREMENT_EXPR
7902 : 158508770 : || TREE_CODE (arg) == PREDECREMENT_EXPR)
7903 : 365231 : return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7904 : :
7905 : 158508677 : if (code != ADDR_EXPR)
7906 : : return NULL_TREE;
7907 : :
7908 : : /* Handle (a = b) used as an "lvalue" for `&'. */
7909 : 155608670 : if (TREE_CODE (arg) == MODIFY_EXPR
7910 : 155608670 : || TREE_CODE (arg) == INIT_EXPR)
7911 : : {
7912 : 0 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
7913 : : tf_warning_or_error);
7914 : 0 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7915 : : arg, real_result);
7916 : 0 : suppress_warning (arg /* What warning? */);
7917 : 0 : return arg;
7918 : : }
7919 : :
7920 : 243936035 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7921 : 243934993 : || TREE_CODE (arg) == OFFSET_REF)
7922 : : return NULL_TREE;
7923 : :
7924 : : /* We permit compiler to make function calls returning
7925 : : objects of aggregate type look like lvalues. */
7926 : 88326323 : {
7927 : 88326323 : tree targ = arg;
7928 : :
7929 : 88326323 : if (TREE_CODE (targ) == SAVE_EXPR)
7930 : 0 : targ = TREE_OPERAND (targ, 0);
7931 : :
7932 : 88326323 : if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
7933 : : {
7934 : 48 : if (TREE_CODE (arg) == SAVE_EXPR)
7935 : : targ = arg;
7936 : : else
7937 : 48 : targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
7938 : 48 : return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
7939 : : }
7940 : :
7941 : 88326275 : if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
7942 : 0 : return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
7943 : 0 : TREE_OPERAND (targ, 0), current_function_decl, NULL);
7944 : : }
7945 : :
7946 : : /* Don't let anything else be handled specially. */
7947 : : return NULL_TREE;
7948 : : }
7949 : :
7950 : : /* Mark EXP saying that we need to be able to take the
7951 : : address of it; it should not be allocated in a register.
7952 : : Value is true if successful. ARRAY_REF_P is true if this
7953 : : is for ARRAY_REF construction - in that case we don't want
7954 : : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7955 : : it is fine to use ARRAY_REFs for vector subscripts on vector
7956 : : register variables.
7957 : :
7958 : : C++: we do not allow `current_class_ptr' to be addressable. */
7959 : :
7960 : : bool
7961 : 257975442 : cxx_mark_addressable (tree exp, bool array_ref_p)
7962 : : {
7963 : 257975442 : tree x = exp;
7964 : :
7965 : 305307512 : while (1)
7966 : 305307512 : switch (TREE_CODE (x))
7967 : : {
7968 : 23208827 : case VIEW_CONVERT_EXPR:
7969 : 23208827 : if (array_ref_p
7970 : 707767 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7971 : 23750615 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
7972 : : return true;
7973 : 23205923 : x = TREE_OPERAND (x, 0);
7974 : 23205923 : break;
7975 : :
7976 : 23892520 : case COMPONENT_REF:
7977 : 23892520 : if (bitfield_p (x))
7978 : 9 : error ("attempt to take address of bit-field");
7979 : : /* FALLTHRU */
7980 : 24126147 : case ADDR_EXPR:
7981 : 24126147 : case ARRAY_REF:
7982 : 24126147 : case REALPART_EXPR:
7983 : 24126147 : case IMAGPART_EXPR:
7984 : 24126147 : x = TREE_OPERAND (x, 0);
7985 : 24126147 : break;
7986 : :
7987 : 4457530 : case PARM_DECL:
7988 : 4457530 : if (x == current_class_ptr)
7989 : : {
7990 : 18 : error ("cannot take the address of %<this%>, which is an rvalue expression");
7991 : 18 : TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
7992 : 18 : return true;
7993 : : }
7994 : : /* Fall through. */
7995 : :
7996 : 50637615 : case VAR_DECL:
7997 : : /* Caller should not be trying to mark initialized
7998 : : constant fields addressable. */
7999 : 50637615 : gcc_assert (DECL_LANG_SPECIFIC (x) == 0
8000 : : || DECL_IN_AGGR_P (x) == 0
8001 : : || TREE_STATIC (x)
8002 : : || DECL_EXTERNAL (x));
8003 : : /* Fall through. */
8004 : :
8005 : 50800564 : case RESULT_DECL:
8006 : 50800604 : if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
8007 : 50800598 : && !DECL_ARTIFICIAL (x))
8008 : : {
8009 : 22 : if (VAR_P (x) && DECL_HARD_REGISTER (x))
8010 : : {
8011 : 12 : error
8012 : 12 : ("address of explicit register variable %qD requested", x);
8013 : 12 : return false;
8014 : : }
8015 : 10 : else if (extra_warnings)
8016 : 3 : warning
8017 : 3 : (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
8018 : : }
8019 : 50800552 : TREE_ADDRESSABLE (x) = 1;
8020 : 50800552 : return true;
8021 : :
8022 : 124884307 : case CONST_DECL:
8023 : 124884307 : case FUNCTION_DECL:
8024 : 124884307 : TREE_ADDRESSABLE (x) = 1;
8025 : 124884307 : return true;
8026 : :
8027 : 49712 : case CONSTRUCTOR:
8028 : 49712 : TREE_ADDRESSABLE (x) = 1;
8029 : 49712 : return true;
8030 : :
8031 : 11278474 : case TARGET_EXPR:
8032 : 11278474 : TREE_ADDRESSABLE (x) = 1;
8033 : 11278474 : cxx_mark_addressable (TARGET_EXPR_SLOT (x));
8034 : 11278474 : return true;
8035 : :
8036 : : default:
8037 : : return true;
8038 : : }
8039 : : }
8040 : :
8041 : : /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
8042 : :
8043 : : tree
8044 : 5657536 : build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
8045 : : tsubst_flags_t complain)
8046 : : {
8047 : 5657536 : tree orig_ifexp = ifexp;
8048 : 5657536 : tree orig_op1 = op1;
8049 : 5657536 : tree orig_op2 = op2;
8050 : 5657536 : tree expr;
8051 : :
8052 : 5657536 : if (processing_template_decl)
8053 : : {
8054 : : /* The standard says that the expression is type-dependent if
8055 : : IFEXP is type-dependent, even though the eventual type of the
8056 : : expression doesn't dependent on IFEXP. */
8057 : 2504217 : if (type_dependent_expression_p (ifexp)
8058 : : /* As a GNU extension, the middle operand may be omitted. */
8059 : 1233697 : || (op1 && type_dependent_expression_p (op1))
8060 : 3310859 : || type_dependent_expression_p (op2))
8061 : 1722405 : return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
8062 : : }
8063 : :
8064 : 3935131 : expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
8065 : 3935131 : if (processing_template_decl && expr != error_mark_node)
8066 : : {
8067 : 781806 : tree min = build_min_non_dep (COND_EXPR, expr,
8068 : : orig_ifexp, orig_op1, orig_op2);
8069 : 781806 : expr = convert_from_reference (min);
8070 : : }
8071 : : return expr;
8072 : : }
8073 : :
8074 : : /* Given a list of expressions, return a compound expression
8075 : : that performs them all and returns the value of the last of them. */
8076 : :
8077 : : tree
8078 : 28782707 : build_x_compound_expr_from_list (tree list, expr_list_kind exp,
8079 : : tsubst_flags_t complain)
8080 : : {
8081 : 28782707 : tree expr = TREE_VALUE (list);
8082 : :
8083 : 232240 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8084 : 29014941 : && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
8085 : : {
8086 : 19 : if (complain & tf_error)
8087 : 38 : pedwarn (cp_expr_loc_or_input_loc (expr), 0,
8088 : : "list-initializer for non-class type must not "
8089 : : "be parenthesized");
8090 : : else
8091 : 0 : return error_mark_node;
8092 : : }
8093 : :
8094 : 28782707 : if (TREE_CHAIN (list))
8095 : : {
8096 : 23 : if (complain & tf_error)
8097 : 14 : switch (exp)
8098 : : {
8099 : 12 : case ELK_INIT:
8100 : 12 : permerror (input_location, "expression list treated as compound "
8101 : : "expression in initializer");
8102 : 12 : break;
8103 : 2 : case ELK_MEM_INIT:
8104 : 2 : permerror (input_location, "expression list treated as compound "
8105 : : "expression in mem-initializer");
8106 : 2 : break;
8107 : 0 : case ELK_FUNC_CAST:
8108 : 0 : permerror (input_location, "expression list treated as compound "
8109 : : "expression in functional cast");
8110 : 0 : break;
8111 : 0 : default:
8112 : 0 : gcc_unreachable ();
8113 : : }
8114 : : else
8115 : 9 : return error_mark_node;
8116 : :
8117 : 28 : for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
8118 : 28 : expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
8119 : 14 : expr, TREE_VALUE (list), NULL_TREE,
8120 : : complain);
8121 : : }
8122 : :
8123 : : return expr;
8124 : : }
8125 : :
8126 : : /* Like build_x_compound_expr_from_list, but using a VEC. */
8127 : :
8128 : : tree
8129 : 120096 : build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8130 : : tsubst_flags_t complain)
8131 : : {
8132 : 120096 : if (vec_safe_is_empty (vec))
8133 : : return NULL_TREE;
8134 : 120096 : else if (vec->length () == 1)
8135 : 120031 : return (*vec)[0];
8136 : : else
8137 : : {
8138 : 65 : tree expr;
8139 : 65 : unsigned int ix;
8140 : 65 : tree t;
8141 : :
8142 : 65 : if (msg != NULL)
8143 : : {
8144 : 5 : if (complain & tf_error)
8145 : 0 : permerror (input_location,
8146 : : "%s expression list treated as compound expression",
8147 : : msg);
8148 : : else
8149 : 5 : return error_mark_node;
8150 : : }
8151 : :
8152 : 60 : expr = (*vec)[0];
8153 : 134 : for (ix = 1; vec->iterate (ix, &t); ++ix)
8154 : 74 : expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
8155 : : t, NULL_TREE, complain);
8156 : :
8157 : : return expr;
8158 : : }
8159 : : }
8160 : :
8161 : : /* Handle overloading of the ',' operator when needed. */
8162 : :
8163 : : tree
8164 : 2507313 : build_x_compound_expr (location_t loc, tree op1, tree op2,
8165 : : tree lookups, tsubst_flags_t complain)
8166 : : {
8167 : 2507313 : tree result;
8168 : 2507313 : tree orig_op1 = op1;
8169 : 2507313 : tree orig_op2 = op2;
8170 : 2507313 : tree overload = NULL_TREE;
8171 : :
8172 : 2507313 : if (processing_template_decl)
8173 : : {
8174 : 2394083 : if (type_dependent_expression_p (op1)
8175 : 2394083 : || type_dependent_expression_p (op2))
8176 : : {
8177 : 2124212 : result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8178 : 2124212 : TREE_TYPE (result)
8179 : 2124212 : = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
8180 : 2124212 : return result;
8181 : : }
8182 : : }
8183 : :
8184 : 383101 : result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8185 : : NULL_TREE, lookups, &overload, complain);
8186 : 383101 : if (!result)
8187 : 376212 : result = cp_build_compound_expr (op1, op2, complain);
8188 : :
8189 : 383101 : if (processing_template_decl && result != error_mark_node)
8190 : : {
8191 : 268050 : if (overload != NULL_TREE)
8192 : 52 : return (build_min_non_dep_op_overload
8193 : 52 : (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
8194 : :
8195 : 267998 : return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
8196 : : }
8197 : :
8198 : : return result;
8199 : : }
8200 : :
8201 : : /* Like cp_build_compound_expr, but for the c-common bits. */
8202 : :
8203 : : tree
8204 : 10452 : build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8205 : : {
8206 : 10452 : return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8207 : : }
8208 : :
8209 : : /* Build a compound expression. */
8210 : :
8211 : : tree
8212 : 484822 : cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8213 : : {
8214 : 484822 : lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8215 : :
8216 : 484822 : if (lhs == error_mark_node || rhs == error_mark_node)
8217 : : return error_mark_node;
8218 : :
8219 : 484819 : if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8220 : 0 : lhs = TREE_OPERAND (lhs, 0);
8221 : 484819 : tree eptype = NULL_TREE;
8222 : 484819 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8223 : : {
8224 : 3 : eptype = TREE_TYPE (rhs);
8225 : 3 : rhs = TREE_OPERAND (rhs, 0);
8226 : : }
8227 : :
8228 : 484819 : if (TREE_CODE (rhs) == TARGET_EXPR)
8229 : : {
8230 : : /* If the rhs is a TARGET_EXPR, then build the compound
8231 : : expression inside the target_expr's initializer. This
8232 : : helps the compiler to eliminate unnecessary temporaries. */
8233 : 2662 : tree init = TARGET_EXPR_INITIAL (rhs);
8234 : :
8235 : 2662 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8236 : 2662 : TARGET_EXPR_INITIAL (rhs) = init;
8237 : :
8238 : 2662 : if (eptype)
8239 : 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8240 : 2662 : return rhs;
8241 : : }
8242 : :
8243 : 482157 : rhs = resolve_nondeduced_context (rhs, complain);
8244 : :
8245 : 482157 : if (type_unknown_p (rhs))
8246 : : {
8247 : 33 : if (complain & tf_error)
8248 : 51 : error_at (cp_expr_loc_or_input_loc (rhs),
8249 : : "no context to resolve type of %qE", rhs);
8250 : 33 : return error_mark_node;
8251 : : }
8252 : :
8253 : 482124 : tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8254 : 482124 : if (eptype)
8255 : 3 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8256 : : return ret;
8257 : : }
8258 : :
8259 : : /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8260 : : casts away constness. CAST gives the type of cast. Returns true
8261 : : if the cast is ill-formed, false if it is well-formed.
8262 : :
8263 : : ??? This function warns for casting away any qualifier not just
8264 : : const. We would like to specify exactly what qualifiers are casted
8265 : : away.
8266 : : */
8267 : :
8268 : : static bool
8269 : 583356 : check_for_casting_away_constness (location_t loc, tree src_type,
8270 : : tree dest_type, enum tree_code cast,
8271 : : tsubst_flags_t complain)
8272 : : {
8273 : : /* C-style casts are allowed to cast away constness. With
8274 : : WARN_CAST_QUAL, we still want to issue a warning. */
8275 : 583356 : if (cast == CAST_EXPR && !warn_cast_qual)
8276 : : return false;
8277 : :
8278 : 536272 : if (!casts_away_constness (src_type, dest_type, complain))
8279 : : return false;
8280 : :
8281 : 386 : switch (cast)
8282 : : {
8283 : 329 : case CAST_EXPR:
8284 : 329 : if (complain & tf_warning)
8285 : 329 : warning_at (loc, OPT_Wcast_qual,
8286 : : "cast from type %qT to type %qT casts away qualifiers",
8287 : : src_type, dest_type);
8288 : : return false;
8289 : :
8290 : 36 : case STATIC_CAST_EXPR:
8291 : 36 : if (complain & tf_error)
8292 : 24 : error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8293 : : "away qualifiers",
8294 : : src_type, dest_type);
8295 : : return true;
8296 : :
8297 : 21 : case REINTERPRET_CAST_EXPR:
8298 : 21 : if (complain & tf_error)
8299 : 15 : error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8300 : : "casts away qualifiers",
8301 : : src_type, dest_type);
8302 : : return true;
8303 : :
8304 : 0 : default:
8305 : 0 : gcc_unreachable();
8306 : : }
8307 : : }
8308 : :
8309 : : /* Warns if the cast from expression EXPR to type TYPE is useless. */
8310 : : void
8311 : 41643128 : maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8312 : : tsubst_flags_t complain)
8313 : : {
8314 : 41643128 : if (warn_useless_cast
8315 : 152 : && complain & tf_warning)
8316 : : {
8317 : 152 : if (TYPE_REF_P (type)
8318 : 152 : ? ((TYPE_REF_IS_RVALUE (type)
8319 : 86 : ? xvalue_p (expr) : lvalue_p (expr))
8320 : 172 : && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8321 : : /* Don't warn when converting a class object to a non-reference type,
8322 : : because that's a common way to create a temporary. */
8323 : 66 : : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8324 : 96 : warning_at (loc, OPT_Wuseless_cast,
8325 : : "useless cast to type %q#T", type);
8326 : : }
8327 : 41643128 : }
8328 : :
8329 : : /* Warns if the cast ignores cv-qualifiers on TYPE. */
8330 : : static void
8331 : 41634353 : maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8332 : : tsubst_flags_t complain)
8333 : : {
8334 : 41634353 : if (warn_ignored_qualifiers
8335 : 263105 : && complain & tf_warning
8336 : 262757 : && !CLASS_TYPE_P (type)
8337 : 41892714 : && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8338 : 36 : warning_at (loc, OPT_Wignored_qualifiers,
8339 : : "type qualifiers ignored on cast result type");
8340 : 41634353 : }
8341 : :
8342 : : /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8343 : : (another pointer-to-member type in the same hierarchy) and return
8344 : : the converted expression. If ALLOW_INVERSE_P is permitted, a
8345 : : pointer-to-derived may be converted to pointer-to-base; otherwise,
8346 : : only the other direction is permitted. If C_CAST_P is true, this
8347 : : conversion is taking place as part of a C-style cast. */
8348 : :
8349 : : tree
8350 : 29166 : convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8351 : : bool c_cast_p, tsubst_flags_t complain)
8352 : : {
8353 : 29166 : if (same_type_p (type, TREE_TYPE (expr)))
8354 : : return expr;
8355 : :
8356 : 29166 : if (TYPE_PTRDATAMEM_P (type))
8357 : : {
8358 : 364 : tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8359 : 364 : tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8360 : 364 : tree delta = (get_delta_difference
8361 : 364 : (obase, nbase,
8362 : : allow_inverse_p, c_cast_p, complain));
8363 : :
8364 : 364 : if (delta == error_mark_node)
8365 : : return error_mark_node;
8366 : :
8367 : 358 : if (!same_type_p (obase, nbase))
8368 : : {
8369 : 287 : if (TREE_CODE (expr) == PTRMEM_CST)
8370 : 159 : expr = cplus_expand_constant (expr);
8371 : :
8372 : 287 : tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8373 : 287 : build_int_cst (TREE_TYPE (expr), -1),
8374 : : complain);
8375 : 287 : tree op1 = build_nop (ptrdiff_type_node, expr);
8376 : 287 : tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8377 : : complain);
8378 : :
8379 : 287 : expr = fold_build3_loc (input_location,
8380 : : COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8381 : : }
8382 : :
8383 : 358 : return build_nop (type, expr);
8384 : : }
8385 : : else
8386 : 28802 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8387 : 28802 : allow_inverse_p, c_cast_p, complain);
8388 : : }
8389 : :
8390 : : /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8391 : : this static_cast is being attempted as one of the possible casts
8392 : : allowed by a C-style cast. (In that case, accessibility of base
8393 : : classes is not considered, and it is OK to cast away
8394 : : constness.) Return the result of the cast. *VALID_P is set to
8395 : : indicate whether or not the cast was valid. */
8396 : :
8397 : : static tree
8398 : 41372306 : build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8399 : : bool *valid_p, tsubst_flags_t complain)
8400 : : {
8401 : 41372306 : tree intype;
8402 : 41372306 : tree result;
8403 : 41372306 : cp_lvalue_kind clk;
8404 : :
8405 : : /* Assume the cast is valid. */
8406 : 41372306 : *valid_p = true;
8407 : :
8408 : 41372306 : intype = unlowered_expr_type (expr);
8409 : :
8410 : : /* Save casted types in the function's used types hash table. */
8411 : 41372306 : used_types_insert (type);
8412 : :
8413 : : /* A prvalue of non-class type is cv-unqualified. */
8414 : 41372306 : if (!CLASS_TYPE_P (type))
8415 : 39943232 : type = cv_unqualified (type);
8416 : :
8417 : : /* [expr.static.cast]
8418 : :
8419 : : An lvalue of type "cv1 B", where B is a class type, can be cast
8420 : : to type "reference to cv2 D", where D is a class derived (clause
8421 : : _class.derived_) from B, if a valid standard conversion from
8422 : : "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8423 : : same cv-qualification as, or greater cv-qualification than, cv1,
8424 : : and B is not a virtual base class of D. */
8425 : : /* We check this case before checking the validity of "TYPE t =
8426 : : EXPR;" below because for this case:
8427 : :
8428 : : struct B {};
8429 : : struct D : public B { D(const B&); };
8430 : : extern B& b;
8431 : : void f() { static_cast<const D&>(b); }
8432 : :
8433 : : we want to avoid constructing a new D. The standard is not
8434 : : completely clear about this issue, but our interpretation is
8435 : : consistent with other compilers. */
8436 : 41372306 : if (TYPE_REF_P (type)
8437 : 2920911 : && CLASS_TYPE_P (TREE_TYPE (type))
8438 : 2328765 : && CLASS_TYPE_P (intype)
8439 : 2328750 : && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8440 : 2327628 : && DERIVED_FROM_P (intype, TREE_TYPE (type))
8441 : 2297402 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8442 : 2297402 : build_pointer_type (TYPE_MAIN_VARIANT
8443 : : (TREE_TYPE (type))),
8444 : : complain)
8445 : 43669708 : && (c_cast_p
8446 : 2297378 : || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8447 : : {
8448 : 2297402 : tree base;
8449 : :
8450 : 2297402 : if (processing_template_decl)
8451 : : return expr;
8452 : :
8453 : : /* There is a standard conversion from "D*" to "B*" even if "B"
8454 : : is ambiguous or inaccessible. If this is really a
8455 : : static_cast, then we check both for inaccessibility and
8456 : : ambiguity. However, if this is a static_cast being performed
8457 : : because the user wrote a C-style cast, then accessibility is
8458 : : not considered. */
8459 : 4256904 : base = lookup_base (TREE_TYPE (type), intype,
8460 : : c_cast_p ? ba_unique : ba_check,
8461 : : NULL, complain);
8462 : 2128464 : expr = cp_build_addr_expr (expr, complain);
8463 : :
8464 : 2128464 : if (sanitize_flags_p (SANITIZE_VPTR))
8465 : : {
8466 : 334 : tree ubsan_check
8467 : 334 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8468 : : intype, expr);
8469 : 334 : if (ubsan_check)
8470 : 2128464 : expr = ubsan_check;
8471 : : }
8472 : :
8473 : : /* Convert from "B*" to "D*". This function will check that "B"
8474 : : is not a virtual base of "D". Even if we don't have a guarantee
8475 : : that expr is NULL, if the static_cast is to a reference type,
8476 : : it is UB if it would be NULL, so omit the non-NULL check. */
8477 : 2128464 : expr = build_base_path (MINUS_EXPR, expr, base,
8478 : : /*nonnull=*/flag_delete_null_pointer_checks,
8479 : : complain);
8480 : :
8481 : : /* Convert the pointer to a reference -- but then remember that
8482 : : there are no expressions with reference type in C++.
8483 : :
8484 : : We call rvalue so that there's an actual tree code
8485 : : (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8486 : : is a variable with the same type, the conversion would get folded
8487 : : away, leaving just the variable and causing lvalue_kind to give
8488 : : the wrong answer. */
8489 : 2128464 : expr = cp_fold_convert (type, expr);
8490 : :
8491 : : /* When -fsanitize=null, make sure to diagnose reference binding to
8492 : : NULL even when the reference is converted to pointer later on. */
8493 : 2128464 : if (sanitize_flags_p (SANITIZE_NULL)
8494 : 340 : && TREE_CODE (expr) == COND_EXPR
8495 : 6 : && TREE_OPERAND (expr, 2)
8496 : 6 : && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8497 : 2128470 : && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8498 : 6 : ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8499 : :
8500 : 2128464 : return convert_from_reference (rvalue (expr));
8501 : : }
8502 : :
8503 : : /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8504 : : cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8505 : 39074904 : if (TYPE_REF_P (type)
8506 : 623509 : && TYPE_REF_IS_RVALUE (type)
8507 : 386113 : && (clk = real_lvalue_p (expr))
8508 : 383203 : && reference_compatible_p (TREE_TYPE (type), intype)
8509 : 39458104 : && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8510 : : {
8511 : 383200 : if (processing_template_decl)
8512 : : return expr;
8513 : 382976 : if (clk == clk_ordinary)
8514 : : {
8515 : : /* Handle the (non-bit-field) lvalue case here by casting to
8516 : : lvalue reference and then changing it to an rvalue reference.
8517 : : Casting an xvalue to rvalue reference will be handled by the
8518 : : main code path. */
8519 : 382973 : tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8520 : 382973 : result = (perform_direct_initialization_if_possible
8521 : 382973 : (lref, expr, c_cast_p, complain));
8522 : 382973 : result = build1 (NON_LVALUE_EXPR, type, result);
8523 : 382973 : return convert_from_reference (result);
8524 : : }
8525 : : else
8526 : : /* For a bit-field or packed field, bind to a temporary. */
8527 : 3 : expr = rvalue (expr);
8528 : : }
8529 : :
8530 : : /* Resolve overloaded address here rather than once in
8531 : : implicit_conversion and again in the inverse code below. */
8532 : 38691707 : if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8533 : : {
8534 : 18 : expr = instantiate_type (type, expr, complain);
8535 : 18 : intype = TREE_TYPE (expr);
8536 : : }
8537 : :
8538 : : /* [expr.static.cast]
8539 : :
8540 : : Any expression can be explicitly converted to type cv void. */
8541 : 38691707 : if (VOID_TYPE_P (type))
8542 : : {
8543 : 138971 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8544 : 0 : expr = TREE_OPERAND (expr, 0);
8545 : 138971 : return convert_to_void (expr, ICV_CAST, complain);
8546 : : }
8547 : :
8548 : : /* [class.abstract]
8549 : : An abstract class shall not be used ... as the type of an explicit
8550 : : conversion. */
8551 : 38552736 : if (abstract_virtuals_error (ACU_CAST, type, complain))
8552 : 6 : return error_mark_node;
8553 : :
8554 : : /* [expr.static.cast]
8555 : :
8556 : : An expression e can be explicitly converted to a type T using a
8557 : : static_cast of the form static_cast<T>(e) if the declaration T
8558 : : t(e);" is well-formed, for some invented temporary variable
8559 : : t. */
8560 : 38552730 : result = perform_direct_initialization_if_possible (type, expr,
8561 : : c_cast_p, complain);
8562 : : /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8563 : : which initialize the first element of the aggregate. We need to handle
8564 : : the array case specifically. */
8565 : 38552730 : if (result == NULL_TREE
8566 : 3859479 : && cxx_dialect >= cxx20
8567 : 951066 : && TREE_CODE (type) == ARRAY_TYPE)
8568 : : {
8569 : : /* Create { EXPR } and perform direct-initialization from it. */
8570 : 15 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8571 : 15 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8572 : 15 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8573 : 15 : result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8574 : : complain);
8575 : : }
8576 : 3859479 : if (result)
8577 : : {
8578 : 34693266 : if (processing_template_decl)
8579 : : return expr;
8580 : :
8581 : 34074141 : result = convert_from_reference (result);
8582 : :
8583 : : /* [expr.static.cast]
8584 : :
8585 : : If T is a reference type, the result is an lvalue; otherwise,
8586 : : the result is an rvalue. */
8587 : 34074141 : if (!TYPE_REF_P (type))
8588 : : {
8589 : 33833926 : result = rvalue (result);
8590 : :
8591 : 33833926 : if (result == expr && SCALAR_TYPE_P (type))
8592 : : /* Leave some record of the cast. */
8593 : 1766610 : result = build_nop (type, expr);
8594 : : }
8595 : 34074141 : return result;
8596 : : }
8597 : :
8598 : : /* [expr.static.cast]
8599 : :
8600 : : The inverse of any standard conversion sequence (clause _conv_),
8601 : : other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8602 : : (_conv.array_), function-to-pointer (_conv.func_), and boolean
8603 : : (_conv.bool_) conversions, can be performed explicitly using
8604 : : static_cast subject to the restriction that the explicit
8605 : : conversion does not cast away constness (_expr.const.cast_), and
8606 : : the following additional rules for specific cases: */
8607 : : /* For reference, the conversions not excluded are: integral
8608 : : promotions, floating-point promotion, integral conversions,
8609 : : floating-point conversions, floating-integral conversions,
8610 : : pointer conversions, and pointer to member conversions. */
8611 : : /* DR 128
8612 : :
8613 : : A value of integral _or enumeration_ type can be explicitly
8614 : : converted to an enumeration type. */
8615 : : /* The effect of all that is that any conversion between any two
8616 : : types which are integral, floating, or enumeration types can be
8617 : : performed. */
8618 : 3859464 : if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8619 : 2241918 : || SCALAR_FLOAT_TYPE_P (type))
8620 : 1760683 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8621 : 246349 : || SCALAR_FLOAT_TYPE_P (intype)))
8622 : : {
8623 : 1657459 : if (processing_template_decl)
8624 : : return expr;
8625 : 1569670 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8626 : 0 : expr = TREE_OPERAND (expr, 0);
8627 : : /* [expr.static.cast]: "If the value is not a bit-field, the result
8628 : : refers to the object or the specified base class subobject thereof;
8629 : : otherwise, the lvalue-to-rvalue conversion is applied to the
8630 : : bit-field and the resulting prvalue is used as the operand of the
8631 : : static_cast." There are no prvalue bit-fields; the l-to-r conversion
8632 : : will give us an object of the underlying type of the bit-field. */
8633 : 1569670 : expr = decay_conversion (expr, complain);
8634 : 1569670 : return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8635 : : }
8636 : :
8637 : 690870 : if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8638 : 686543 : && CLASS_TYPE_P (TREE_TYPE (type))
8639 : 194847 : && CLASS_TYPE_P (TREE_TYPE (intype))
8640 : 2239039 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8641 : : (TREE_TYPE (intype))),
8642 : 37034 : build_pointer_type (TYPE_MAIN_VARIANT
8643 : : (TREE_TYPE (type))),
8644 : : complain))
8645 : : {
8646 : 36695 : tree base;
8647 : :
8648 : 36695 : if (processing_template_decl)
8649 : : return expr;
8650 : :
8651 : 36677 : if (!c_cast_p
8652 : 36677 : && check_for_casting_away_constness (loc, intype, type,
8653 : : STATIC_CAST_EXPR,
8654 : : complain))
8655 : 6 : return error_mark_node;
8656 : 72800 : base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8657 : : c_cast_p ? ba_unique : ba_check,
8658 : : NULL, complain);
8659 : 36671 : expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8660 : : complain);
8661 : :
8662 : 36671 : if (sanitize_flags_p (SANITIZE_VPTR))
8663 : : {
8664 : 43 : tree ubsan_check
8665 : 43 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8666 : : intype, expr);
8667 : 43 : if (ubsan_check)
8668 : 36671 : expr = ubsan_check;
8669 : : }
8670 : :
8671 : 36671 : return cp_fold_convert (type, expr);
8672 : : }
8673 : :
8674 : 89 : if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8675 : 2165310 : || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8676 : : {
8677 : 232 : tree c1;
8678 : 232 : tree c2;
8679 : 232 : tree t1;
8680 : 232 : tree t2;
8681 : :
8682 : 232 : c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8683 : 232 : c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8684 : :
8685 : 232 : if (TYPE_PTRDATAMEM_P (type))
8686 : : {
8687 : 89 : t1 = (build_ptrmem_type
8688 : 89 : (c1,
8689 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8690 : 89 : t2 = (build_ptrmem_type
8691 : 89 : (c2,
8692 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8693 : : }
8694 : : else
8695 : : {
8696 : : t1 = intype;
8697 : : t2 = type;
8698 : : }
8699 : 232 : if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8700 : : {
8701 : 154 : if (!c_cast_p
8702 : 154 : && check_for_casting_away_constness (loc, intype, type,
8703 : : STATIC_CAST_EXPR,
8704 : : complain))
8705 : 9 : return error_mark_node;
8706 : 145 : if (processing_template_decl)
8707 : : return expr;
8708 : 145 : return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8709 : 145 : c_cast_p, complain);
8710 : : }
8711 : : }
8712 : :
8713 : : /* [expr.static.cast]
8714 : :
8715 : : An rvalue of type "pointer to cv void" can be explicitly
8716 : : converted to a pointer to object type. A value of type pointer
8717 : : to object converted to "pointer to cv void" and back to the
8718 : : original pointer type will have its original value. */
8719 : 2165156 : if (TYPE_PTR_P (intype)
8720 : 751103 : && VOID_TYPE_P (TREE_TYPE (intype))
8721 : 2791204 : && TYPE_PTROB_P (type))
8722 : : {
8723 : 592391 : if (!c_cast_p
8724 : 592391 : && check_for_casting_away_constness (loc, intype, type,
8725 : : STATIC_CAST_EXPR,
8726 : : complain))
8727 : 21 : return error_mark_node;
8728 : 592370 : if (processing_template_decl)
8729 : : return expr;
8730 : 507117 : return build_nop (type, expr);
8731 : : }
8732 : :
8733 : 1572765 : *valid_p = false;
8734 : 1572765 : return error_mark_node;
8735 : : }
8736 : :
8737 : : /* Return an expression representing static_cast<TYPE>(EXPR). */
8738 : :
8739 : : tree
8740 : 11475803 : build_static_cast (location_t loc, tree type, tree oexpr,
8741 : : tsubst_flags_t complain)
8742 : : {
8743 : 11475803 : tree expr = oexpr;
8744 : 11475803 : tree result;
8745 : 11475803 : bool valid_p;
8746 : :
8747 : 11475803 : if (type == error_mark_node || expr == error_mark_node)
8748 : : return error_mark_node;
8749 : :
8750 : 11475749 : bool dependent = (dependent_type_p (type)
8751 : 11475749 : || type_dependent_expression_p (expr));
8752 : 7575101 : if (dependent)
8753 : : {
8754 : 4862032 : tmpl:
8755 : 4862032 : expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8756 : : /* We don't know if it will or will not have side effects. */
8757 : 4862032 : TREE_SIDE_EFFECTS (expr) = 1;
8758 : 4862032 : result = convert_from_reference (expr);
8759 : 4862032 : protected_set_expr_location (result, loc);
8760 : 4862032 : return result;
8761 : : }
8762 : :
8763 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8764 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8765 : 7575101 : if (!TYPE_REF_P (type)
8766 : 4654655 : && TREE_CODE (expr) == NOP_EXPR
8767 : 7667369 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8768 : 48 : expr = TREE_OPERAND (expr, 0);
8769 : :
8770 : 7575101 : result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8771 : : &valid_p, complain);
8772 : 7575101 : if (valid_p)
8773 : : {
8774 : 7574905 : if (result != error_mark_node)
8775 : : {
8776 : 7574800 : maybe_warn_about_useless_cast (loc, type, expr, complain);
8777 : 7574800 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8778 : : }
8779 : 7574905 : if (processing_template_decl)
8780 : 961384 : goto tmpl;
8781 : 6613521 : protected_set_expr_location (result, loc);
8782 : 6613521 : return result;
8783 : : }
8784 : :
8785 : 196 : if (complain & tf_error)
8786 : : {
8787 : 163 : auto_diagnostic_group d;
8788 : 163 : error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8789 : 163 : TREE_TYPE (expr), type);
8790 : 163 : if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8791 : 121 : && CLASS_TYPE_P (TREE_TYPE (type))
8792 : 192 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8793 : 17 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8794 : 17 : "class type %qT is incomplete", TREE_TYPE (type));
8795 : 163 : tree expr_type = TREE_TYPE (expr);
8796 : 163 : if (TYPE_PTR_P (expr_type))
8797 : 36 : expr_type = TREE_TYPE (expr_type);
8798 : 163 : if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8799 : 12 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8800 : : "class type %qT is incomplete", expr_type);
8801 : 163 : }
8802 : 196 : return error_mark_node;
8803 : : }
8804 : :
8805 : : /* EXPR is an expression with member function or pointer-to-member
8806 : : function type. TYPE is a pointer type. Converting EXPR to TYPE is
8807 : : not permitted by ISO C++, but we accept it in some modes. If we
8808 : : are not in one of those modes, issue a diagnostic. Return the
8809 : : converted expression. */
8810 : :
8811 : : tree
8812 : 197 : convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8813 : : {
8814 : 197 : tree intype;
8815 : 197 : tree decl;
8816 : :
8817 : 197 : intype = TREE_TYPE (expr);
8818 : 197 : gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8819 : : || TREE_CODE (intype) == METHOD_TYPE);
8820 : :
8821 : 197 : if (!(complain & tf_warning_or_error))
8822 : 0 : return error_mark_node;
8823 : :
8824 : 197 : location_t loc = cp_expr_loc_or_input_loc (expr);
8825 : :
8826 : 197 : if (pedantic || warn_pmf2ptr)
8827 : 271 : pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8828 : : "converting from %qH to %qI", intype, type);
8829 : :
8830 : 197 : STRIP_ANY_LOCATION_WRAPPER (expr);
8831 : :
8832 : 197 : if (TREE_CODE (intype) == METHOD_TYPE)
8833 : 88 : expr = build_addr_func (expr, complain);
8834 : 109 : else if (TREE_CODE (expr) == PTRMEM_CST)
8835 : 91 : expr = build_address (PTRMEM_CST_MEMBER (expr));
8836 : : else
8837 : : {
8838 : 18 : decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8839 : 18 : decl = build_address (decl);
8840 : 18 : expr = get_member_function_from_ptrfunc (&decl, expr, complain);
8841 : : }
8842 : :
8843 : 197 : if (expr == error_mark_node)
8844 : : return error_mark_node;
8845 : :
8846 : 197 : expr = build_nop (type, expr);
8847 : 197 : SET_EXPR_LOCATION (expr, loc);
8848 : 197 : return expr;
8849 : : }
8850 : :
8851 : : /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8852 : : constexpr evaluation knows to reject it. */
8853 : :
8854 : : static tree
8855 : 92816 : build_nop_reinterpret (tree type, tree expr)
8856 : : {
8857 : 92816 : tree ret = build_nop (type, expr);
8858 : 92816 : if (ret != expr)
8859 : 92816 : REINTERPRET_CAST_P (ret) = true;
8860 : 92816 : return ret;
8861 : : }
8862 : :
8863 : : /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8864 : : If C_CAST_P is true, this reinterpret cast is being done as part of
8865 : : a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8866 : : indicate whether or not reinterpret_cast was valid. */
8867 : :
8868 : : static tree
8869 : 1691908 : build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8870 : : bool c_cast_p, bool *valid_p,
8871 : : tsubst_flags_t complain)
8872 : : {
8873 : 1691908 : tree intype;
8874 : :
8875 : : /* Assume the cast is invalid. */
8876 : 1691908 : if (valid_p)
8877 : 1572625 : *valid_p = true;
8878 : :
8879 : 1691908 : if (type == error_mark_node || error_operand_p (expr))
8880 : : return error_mark_node;
8881 : :
8882 : 1691908 : intype = TREE_TYPE (expr);
8883 : :
8884 : : /* Save casted types in the function's used types hash table. */
8885 : 1691908 : used_types_insert (type);
8886 : :
8887 : : /* A prvalue of non-class type is cv-unqualified. */
8888 : 1691908 : if (!CLASS_TYPE_P (type))
8889 : 1691899 : type = cv_unqualified (type);
8890 : :
8891 : : /* [expr.reinterpret.cast]
8892 : : A glvalue of type T1, designating an object x, can be cast to the type
8893 : : "reference to T2" if an expression of type "pointer to T1" can be
8894 : : explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8895 : : The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8896 : : of type "pointer to T1". No temporary is created, no copy is made, and no
8897 : : constructors (11.4.4) or conversion functions (11.4.7) are called. */
8898 : 1691908 : if (TYPE_REF_P (type))
8899 : : {
8900 : 12055 : if (!glvalue_p (expr))
8901 : : {
8902 : 9 : if (complain & tf_error)
8903 : 9 : error_at (loc, "invalid cast of a prvalue expression of type "
8904 : : "%qT to type %qT",
8905 : : intype, type);
8906 : 9 : return error_mark_node;
8907 : : }
8908 : :
8909 : : /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8910 : : "B" are related class types; the reinterpret_cast does not
8911 : : adjust the pointer. */
8912 : 12046 : if (TYPE_PTR_P (intype)
8913 : 3 : && (complain & tf_warning)
8914 : 12049 : && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8915 : : COMPARE_BASE | COMPARE_DERIVED)))
8916 : 3 : warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
8917 : : intype, type);
8918 : :
8919 : 12046 : expr = cp_build_addr_expr (expr, complain);
8920 : :
8921 : 12046 : if (warn_strict_aliasing > 2)
8922 : 82 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8923 : :
8924 : 12046 : if (expr != error_mark_node)
8925 : 12046 : expr = build_reinterpret_cast_1
8926 : 12046 : (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
8927 : : valid_p, complain);
8928 : 12046 : if (expr != error_mark_node)
8929 : : /* cp_build_indirect_ref isn't right for rvalue refs. */
8930 : 12043 : expr = convert_from_reference (fold_convert (type, expr));
8931 : 12046 : return expr;
8932 : : }
8933 : :
8934 : : /* As a G++ extension, we consider conversions from member
8935 : : functions, and pointers to member functions to
8936 : : pointer-to-function and pointer-to-void types. If
8937 : : -Wno-pmf-conversions has not been specified,
8938 : : convert_member_func_to_ptr will issue an error message. */
8939 : 264 : if ((TYPE_PTRMEMFUNC_P (intype)
8940 : 1679665 : || TREE_CODE (intype) == METHOD_TYPE)
8941 : 276 : && TYPE_PTR_P (type)
8942 : 1680050 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
8943 : 155 : || VOID_TYPE_P (TREE_TYPE (type))))
8944 : 188 : return convert_member_func_to_ptr (type, expr, complain);
8945 : :
8946 : : /* If the cast is not to a reference type, the lvalue-to-rvalue,
8947 : : array-to-pointer, and function-to-pointer conversions are
8948 : : performed. */
8949 : 1679665 : expr = decay_conversion (expr, complain);
8950 : :
8951 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8952 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8953 : 1679665 : if (TREE_CODE (expr) == NOP_EXPR
8954 : 1679665 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8955 : 31 : expr = TREE_OPERAND (expr, 0);
8956 : :
8957 : 1679665 : if (error_operand_p (expr))
8958 : 30 : return error_mark_node;
8959 : :
8960 : 1679635 : intype = TREE_TYPE (expr);
8961 : :
8962 : : /* [expr.reinterpret.cast]
8963 : : A pointer can be converted to any integral type large enough to
8964 : : hold it. ... A value of type std::nullptr_t can be converted to
8965 : : an integral type; the conversion has the same meaning and
8966 : : validity as a conversion of (void*)0 to the integral type. */
8967 : 1679635 : if (CP_INTEGRAL_TYPE_P (type)
8968 : 141097 : && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
8969 : : {
8970 : 139378 : if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
8971 : : {
8972 : 21 : if (complain & tf_error)
8973 : 15 : permerror (loc, "cast from %qH to %qI loses precision",
8974 : : intype, type);
8975 : : else
8976 : 6 : return error_mark_node;
8977 : : }
8978 : 139372 : if (NULLPTR_TYPE_P (intype))
8979 : 98 : return build_int_cst (type, 0);
8980 : : }
8981 : : /* [expr.reinterpret.cast]
8982 : : A value of integral or enumeration type can be explicitly
8983 : : converted to a pointer. */
8984 : 1540257 : else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
8985 : : /* OK */
8986 : : ;
8987 : 1505182 : else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8988 : 1503448 : || TYPE_PTR_OR_PTRMEM_P (type))
8989 : 1600626 : && same_type_p (type, intype))
8990 : : /* DR 799 */
8991 : 2596 : return rvalue (expr);
8992 : 1502586 : else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
8993 : : {
8994 : 5589 : if ((complain & tf_warning)
8995 : 11178 : && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
8996 : 5589 : TREE_TYPE (intype)))
8997 : 145 : warning_at (loc, OPT_Wcast_function_type,
8998 : : "cast between incompatible function types"
8999 : : " from %qH to %qI", intype, type);
9000 : 5589 : return build_nop_reinterpret (type, expr);
9001 : : }
9002 : 1496997 : else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
9003 : : {
9004 : 79 : if ((complain & tf_warning)
9005 : 158 : && !cxx_safe_function_type_cast_p
9006 : 79 : (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
9007 : 79 : TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
9008 : 52 : warning_at (loc, OPT_Wcast_function_type,
9009 : : "cast between incompatible pointer to member types"
9010 : : " from %qH to %qI", intype, type);
9011 : 79 : return build_nop_reinterpret (type, expr);
9012 : : }
9013 : 21 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
9014 : 1496918 : || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
9015 : : {
9016 : 86893 : if (!c_cast_p
9017 : 86893 : && check_for_casting_away_constness (loc, intype, type,
9018 : : REINTERPRET_CAST_EXPR,
9019 : : complain))
9020 : 21 : return error_mark_node;
9021 : : /* Warn about possible alignment problems. */
9022 : 86872 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9023 : 24 : && (complain & tf_warning)
9024 : 24 : && !VOID_TYPE_P (type)
9025 : 24 : && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
9026 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (type))
9027 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (intype))
9028 : 86920 : && min_align_of_type (TREE_TYPE (type))
9029 : 24 : > min_align_of_type (TREE_TYPE (intype)))
9030 : 18 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9031 : : "increases required alignment of target type",
9032 : : intype, type);
9033 : :
9034 : 86872 : if (warn_strict_aliasing <= 2)
9035 : : /* strict_aliasing_warning STRIP_NOPs its expr. */
9036 : 75340 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9037 : :
9038 : 86872 : return build_nop_reinterpret (type, expr);
9039 : : }
9040 : 297 : else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
9041 : 1410185 : || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
9042 : : {
9043 : 276 : if (complain & tf_warning)
9044 : : /* C++11 5.2.10 p8 says that "Converting a function pointer to an
9045 : : object pointer type or vice versa is conditionally-supported." */
9046 : 276 : warning_at (loc, OPT_Wconditionally_supported,
9047 : : "casting between pointer-to-function and "
9048 : : "pointer-to-object is conditionally-supported");
9049 : 276 : return build_nop_reinterpret (type, expr);
9050 : : }
9051 : 1409749 : else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
9052 : 1407980 : return convert_to_vector (type, rvalue (expr));
9053 : 1769 : else if (gnu_vector_type_p (intype)
9054 : 1769 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9055 : 1669 : return convert_to_integer_nofold (type, expr);
9056 : : else
9057 : : {
9058 : 100 : if (valid_p)
9059 : 82 : *valid_p = false;
9060 : 100 : if (complain & tf_error)
9061 : 91 : error_at (loc, "invalid cast from type %qT to type %qT",
9062 : : intype, type);
9063 : 100 : return error_mark_node;
9064 : : }
9065 : :
9066 : 174349 : expr = cp_convert (type, expr, complain);
9067 : 174349 : if (TREE_CODE (expr) == NOP_EXPR)
9068 : : /* Mark any nop_expr that created as a reintepret_cast. */
9069 : 0 : REINTERPRET_CAST_P (expr) = true;
9070 : : return expr;
9071 : : }
9072 : :
9073 : : tree
9074 : 522246 : build_reinterpret_cast (location_t loc, tree type, tree expr,
9075 : : tsubst_flags_t complain)
9076 : : {
9077 : 522246 : tree r;
9078 : :
9079 : 522246 : if (type == error_mark_node || expr == error_mark_node)
9080 : : return error_mark_node;
9081 : :
9082 : 522239 : if (processing_template_decl)
9083 : : {
9084 : 414946 : tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
9085 : :
9086 : 414946 : if (!TREE_SIDE_EFFECTS (t)
9087 : 414946 : && type_dependent_expression_p (expr))
9088 : : /* There might turn out to be side effects inside expr. */
9089 : 184171 : TREE_SIDE_EFFECTS (t) = 1;
9090 : 414946 : r = convert_from_reference (t);
9091 : 414946 : protected_set_expr_location (r, loc);
9092 : 414946 : return r;
9093 : : }
9094 : :
9095 : 107293 : r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9096 : : /*valid_p=*/NULL, complain);
9097 : 107293 : if (r != error_mark_node)
9098 : : {
9099 : 107230 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9100 : 107230 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9101 : : }
9102 : 107293 : protected_set_expr_location (r, loc);
9103 : 107293 : return r;
9104 : : }
9105 : :
9106 : : /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9107 : : return an appropriate expression. Otherwise, return
9108 : : error_mark_node. If the cast is not valid, and COMPLAIN is true,
9109 : : then a diagnostic will be issued. If VALID_P is non-NULL, we are
9110 : : performing a C-style cast, its value upon return will indicate
9111 : : whether or not the conversion succeeded. */
9112 : :
9113 : : static tree
9114 : 33952709 : build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9115 : : tsubst_flags_t complain, bool *valid_p)
9116 : : {
9117 : 33952709 : tree src_type;
9118 : 33952709 : tree reference_type;
9119 : :
9120 : : /* Callers are responsible for handling error_mark_node as a
9121 : : destination type. */
9122 : 33952709 : gcc_assert (dst_type != error_mark_node);
9123 : : /* In a template, callers should be building syntactic
9124 : : representations of casts, not using this machinery. */
9125 : 33952709 : gcc_assert (!processing_template_decl);
9126 : :
9127 : : /* Assume the conversion is invalid. */
9128 : 33952709 : if (valid_p)
9129 : 33831905 : *valid_p = false;
9130 : :
9131 : 33952709 : if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9132 : : {
9133 : 33425984 : if (complain & tf_error)
9134 : 9 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9135 : : "which is not a pointer, reference, "
9136 : : "nor a pointer-to-data-member type", dst_type);
9137 : 33425984 : return error_mark_node;
9138 : : }
9139 : :
9140 : 526725 : if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
9141 : : {
9142 : 3946 : if (complain & tf_error)
9143 : 6 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9144 : : "which is a pointer or reference to a function type",
9145 : : dst_type);
9146 : 3946 : return error_mark_node;
9147 : : }
9148 : :
9149 : : /* A prvalue of non-class type is cv-unqualified. */
9150 : 522779 : dst_type = cv_unqualified (dst_type);
9151 : :
9152 : : /* Save casted types in the function's used types hash table. */
9153 : 522779 : used_types_insert (dst_type);
9154 : :
9155 : 522779 : src_type = TREE_TYPE (expr);
9156 : : /* Expressions do not really have reference types. */
9157 : 522779 : if (TYPE_REF_P (src_type))
9158 : 0 : src_type = TREE_TYPE (src_type);
9159 : :
9160 : : /* [expr.const.cast]
9161 : :
9162 : : For two object types T1 and T2, if a pointer to T1 can be explicitly
9163 : : converted to the type "pointer to T2" using a const_cast, then the
9164 : : following conversions can also be made:
9165 : :
9166 : : -- an lvalue of type T1 can be explicitly converted to an lvalue of
9167 : : type T2 using the cast const_cast<T2&>;
9168 : :
9169 : : -- a glvalue of type T1 can be explicitly converted to an xvalue of
9170 : : type T2 using the cast const_cast<T2&&>; and
9171 : :
9172 : : -- if T1 is a class type, a prvalue of type T1 can be explicitly
9173 : : converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9174 : :
9175 : 522779 : if (TYPE_REF_P (dst_type))
9176 : : {
9177 : 53658 : reference_type = dst_type;
9178 : 53658 : if (!TYPE_REF_IS_RVALUE (dst_type)
9179 : 53658 : ? lvalue_p (expr)
9180 : 607 : : obvalue_p (expr))
9181 : : /* OK. */;
9182 : : else
9183 : : {
9184 : 35 : if (complain & tf_error)
9185 : 9 : error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
9186 : : "to type %qT",
9187 : : src_type, dst_type);
9188 : 35 : return error_mark_node;
9189 : : }
9190 : 53623 : dst_type = build_pointer_type (TREE_TYPE (dst_type));
9191 : 53623 : src_type = build_pointer_type (src_type);
9192 : : }
9193 : : else
9194 : : {
9195 : 469121 : reference_type = NULL_TREE;
9196 : : /* If the destination type is not a reference type, the
9197 : : lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9198 : : conversions are performed. */
9199 : 469121 : src_type = type_decays_to (src_type);
9200 : 469121 : if (src_type == error_mark_node)
9201 : : return error_mark_node;
9202 : : }
9203 : :
9204 : 522744 : if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9205 : : {
9206 : 384112 : if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9207 : : {
9208 : 155426 : if (valid_p)
9209 : : {
9210 : 34700 : *valid_p = true;
9211 : : /* This cast is actually a C-style cast. Issue a warning if
9212 : : the user is making a potentially unsafe cast. */
9213 : 34700 : check_for_casting_away_constness (loc, src_type, dst_type,
9214 : : CAST_EXPR, complain);
9215 : : /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9216 : 34700 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9217 : 3 : && (complain & tf_warning)
9218 : 34706 : && min_align_of_type (TREE_TYPE (dst_type))
9219 : 3 : > min_align_of_type (TREE_TYPE (src_type)))
9220 : 3 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9221 : : "increases required alignment of target type",
9222 : : src_type, dst_type);
9223 : : }
9224 : 155426 : if (reference_type)
9225 : : {
9226 : 53186 : expr = cp_build_addr_expr (expr, complain);
9227 : 53186 : if (expr == error_mark_node)
9228 : : return error_mark_node;
9229 : 53168 : expr = build_nop (reference_type, expr);
9230 : 53168 : return convert_from_reference (expr);
9231 : : }
9232 : : else
9233 : : {
9234 : 102240 : expr = decay_conversion (expr, complain);
9235 : 102240 : if (expr == error_mark_node)
9236 : : return error_mark_node;
9237 : :
9238 : : /* build_c_cast puts on a NOP_EXPR to make the result not an
9239 : : lvalue. Strip such NOP_EXPRs if VALUE is being used in
9240 : : non-lvalue context. */
9241 : 102240 : if (TREE_CODE (expr) == NOP_EXPR
9242 : 102240 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9243 : 3 : expr = TREE_OPERAND (expr, 0);
9244 : 102240 : return build_nop (dst_type, expr);
9245 : : }
9246 : : }
9247 : 228686 : else if (valid_p
9248 : 457324 : && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9249 : 228638 : TREE_TYPE (src_type)))
9250 : 12822 : check_for_casting_away_constness (loc, src_type, dst_type,
9251 : : CAST_EXPR, complain);
9252 : : }
9253 : :
9254 : 367318 : if (complain & tf_error)
9255 : 30 : error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9256 : : src_type, dst_type);
9257 : 367318 : return error_mark_node;
9258 : : }
9259 : :
9260 : : tree
9261 : 518782 : build_const_cast (location_t loc, tree type, tree expr,
9262 : : tsubst_flags_t complain)
9263 : : {
9264 : 518782 : tree r;
9265 : :
9266 : 518782 : if (type == error_mark_node || error_operand_p (expr))
9267 : : return error_mark_node;
9268 : :
9269 : 518774 : if (processing_template_decl)
9270 : : {
9271 : 397970 : tree t = build_min (CONST_CAST_EXPR, type, expr);
9272 : :
9273 : 397970 : if (!TREE_SIDE_EFFECTS (t)
9274 : 397970 : && type_dependent_expression_p (expr))
9275 : : /* There might turn out to be side effects inside expr. */
9276 : 313622 : TREE_SIDE_EFFECTS (t) = 1;
9277 : 397970 : r = convert_from_reference (t);
9278 : 397970 : protected_set_expr_location (r, loc);
9279 : 397970 : return r;
9280 : : }
9281 : :
9282 : 120804 : r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9283 : 120804 : if (r != error_mark_node)
9284 : : {
9285 : 120726 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9286 : 120726 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9287 : : }
9288 : 120804 : protected_set_expr_location (r, loc);
9289 : 120804 : return r;
9290 : : }
9291 : :
9292 : : /* Like cp_build_c_cast, but for the c-common bits. */
9293 : :
9294 : : tree
9295 : 0 : build_c_cast (location_t loc, tree type, tree expr)
9296 : : {
9297 : 0 : return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9298 : : }
9299 : :
9300 : : /* Like the "build_c_cast" used for c-common, but using cp_expr to
9301 : : preserve location information even for tree nodes that don't
9302 : : support it. */
9303 : :
9304 : : cp_expr
9305 : 8433530 : build_c_cast (location_t loc, tree type, cp_expr expr)
9306 : : {
9307 : 8433530 : cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9308 : 8433530 : result.set_location (loc);
9309 : 8433530 : return result;
9310 : : }
9311 : :
9312 : : /* Build an expression representing an explicit C-style cast to type
9313 : : TYPE of expression EXPR. */
9314 : :
9315 : : tree
9316 : 35544010 : cp_build_c_cast (location_t loc, tree type, tree expr,
9317 : : tsubst_flags_t complain)
9318 : : {
9319 : 35544010 : tree value = expr;
9320 : 35544010 : tree result;
9321 : 35544010 : bool valid_p;
9322 : :
9323 : 35544010 : if (type == error_mark_node || error_operand_p (expr))
9324 : : return error_mark_node;
9325 : :
9326 : 35543854 : if (processing_template_decl)
9327 : : {
9328 : 1711940 : tree t = build_min (CAST_EXPR, type,
9329 : : tree_cons (NULL_TREE, value, NULL_TREE));
9330 : : /* We don't know if it will or will not have side effects. */
9331 : 1711940 : TREE_SIDE_EFFECTS (t) = 1;
9332 : 1711940 : return convert_from_reference (t);
9333 : : }
9334 : :
9335 : : /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9336 : : 'Class') should always be retained, because this information aids
9337 : : in method lookup. */
9338 : 33831914 : if (objc_is_object_ptr (type)
9339 : 33831914 : && objc_is_object_ptr (TREE_TYPE (expr)))
9340 : 0 : return build_nop (type, expr);
9341 : :
9342 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9343 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9344 : 33831914 : if (!TYPE_REF_P (type)
9345 : 33831118 : && TREE_CODE (value) == NOP_EXPR
9346 : 34035405 : && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9347 : 64056 : value = TREE_OPERAND (value, 0);
9348 : :
9349 : 33831914 : if (TREE_CODE (type) == ARRAY_TYPE)
9350 : : {
9351 : : /* Allow casting from T1* to T2[] because Cfront allows it.
9352 : : NIHCL uses it. It is not valid ISO C++ however. */
9353 : 3 : if (TYPE_PTR_P (TREE_TYPE (expr)))
9354 : : {
9355 : 0 : if (complain & tf_error)
9356 : 0 : permerror (loc, "ISO C++ forbids casting to an array type %qT",
9357 : : type);
9358 : : else
9359 : 0 : return error_mark_node;
9360 : 0 : type = build_pointer_type (TREE_TYPE (type));
9361 : : }
9362 : : else
9363 : : {
9364 : 3 : if (complain & tf_error)
9365 : 3 : error_at (loc, "ISO C++ forbids casting to an array type %qT",
9366 : : type);
9367 : 3 : return error_mark_node;
9368 : : }
9369 : : }
9370 : :
9371 : 33831911 : if (FUNC_OR_METHOD_TYPE_P (type))
9372 : : {
9373 : 15 : if (complain & tf_error)
9374 : 15 : error_at (loc, "invalid cast to function type %qT", type);
9375 : 15 : return error_mark_node;
9376 : : }
9377 : :
9378 : 33831896 : if (TYPE_PTR_P (type)
9379 : 404990 : && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9380 : : /* Casting to an integer of smaller size is an error detected elsewhere. */
9381 : 137107 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9382 : : /* Don't warn about converting any constant. */
9383 : 33914896 : && !TREE_CONSTANT (value))
9384 : 47 : warning_at (loc, OPT_Wint_to_pointer_cast,
9385 : : "cast to pointer from integer of different size");
9386 : :
9387 : : /* A C-style cast can be a const_cast. */
9388 : 33831896 : result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9389 : : &valid_p);
9390 : 33831896 : if (valid_p)
9391 : : {
9392 : 34691 : if (result != error_mark_node)
9393 : : {
9394 : 34682 : maybe_warn_about_useless_cast (loc, type, value, complain);
9395 : 34682 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9396 : : }
9397 : 9 : else if (complain & tf_error)
9398 : 9 : build_const_cast_1 (loc, type, value, tf_error, &valid_p);
9399 : 34691 : return result;
9400 : : }
9401 : :
9402 : : /* Or a static cast. */
9403 : 33797205 : result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9404 : : &valid_p, complain);
9405 : : /* Or a reinterpret_cast. */
9406 : 33797205 : if (!valid_p)
9407 : 1572569 : result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9408 : : &valid_p, complain);
9409 : : /* The static_cast or reinterpret_cast may be followed by a
9410 : : const_cast. */
9411 : 33797205 : if (valid_p
9412 : : /* A valid cast may result in errors if, for example, a
9413 : : conversion to an ambiguous base class is required. */
9414 : 33797205 : && !error_operand_p (result))
9415 : : {
9416 : 33796915 : tree result_type;
9417 : :
9418 : 33796915 : maybe_warn_about_useless_cast (loc, type, value, complain);
9419 : 33796915 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9420 : :
9421 : : /* Non-class rvalues always have cv-unqualified type. */
9422 : 33796915 : if (!CLASS_TYPE_P (type))
9423 : 32530663 : type = TYPE_MAIN_VARIANT (type);
9424 : 33796915 : result_type = TREE_TYPE (result);
9425 : 33796915 : if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9426 : 32530204 : result_type = TYPE_MAIN_VARIANT (result_type);
9427 : : /* If the type of RESULT does not match TYPE, perform a
9428 : : const_cast to make it match. If the static_cast or
9429 : : reinterpret_cast succeeded, we will differ by at most
9430 : : cv-qualification, so the follow-on const_cast is guaranteed
9431 : : to succeed. */
9432 : 33796915 : if (!same_type_p (non_reference (type), non_reference (result_type)))
9433 : : {
9434 : 0 : result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
9435 : 0 : gcc_assert (valid_p);
9436 : : }
9437 : 33796915 : return result;
9438 : : }
9439 : :
9440 : 290 : return error_mark_node;
9441 : : }
9442 : :
9443 : : /* Warn when a value is moved to itself with std::move. LHS is the target,
9444 : : RHS may be the std::move call, and LOC is the location of the whole
9445 : : assignment. Return true if we warned. */
9446 : :
9447 : : bool
9448 : 23012783 : maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9449 : : {
9450 : 23012783 : if (!warn_self_move)
9451 : : return false;
9452 : :
9453 : : /* C++98 doesn't know move. */
9454 : 331912 : if (cxx_dialect < cxx11)
9455 : : return false;
9456 : :
9457 : 312326 : if (processing_template_decl)
9458 : : return false;
9459 : :
9460 : 18741 : if (!REFERENCE_REF_P (rhs)
9461 : 266697 : || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9462 : : return false;
9463 : 3844 : tree fn = TREE_OPERAND (rhs, 0);
9464 : 3844 : if (!is_std_move_p (fn))
9465 : : return false;
9466 : :
9467 : : /* Just a little helper to strip * and various NOPs. */
9468 : 5679 : auto extract_op = [] (tree &op) {
9469 : 3786 : STRIP_NOPS (op);
9470 : 5012 : while (INDIRECT_REF_P (op))
9471 : 1226 : op = TREE_OPERAND (op, 0);
9472 : 3786 : op = maybe_undo_parenthesized_ref (op);
9473 : 3786 : STRIP_ANY_LOCATION_WRAPPER (op);
9474 : 3786 : };
9475 : :
9476 : 1893 : tree arg = CALL_EXPR_ARG (fn, 0);
9477 : 1893 : extract_op (arg);
9478 : 1893 : if (TREE_CODE (arg) == ADDR_EXPR)
9479 : 1175 : arg = TREE_OPERAND (arg, 0);
9480 : 1893 : tree type = TREE_TYPE (lhs);
9481 : 1893 : tree orig_lhs = lhs;
9482 : 1893 : extract_op (lhs);
9483 : 1893 : if (cp_tree_equal (lhs, arg)
9484 : : /* Also warn in a member-initializer-list, as in : i(std::move(i)). */
9485 : 1893 : || (TREE_CODE (lhs) == FIELD_DECL
9486 : 508 : && TREE_CODE (arg) == COMPONENT_REF
9487 : 314 : && cp_tree_equal (TREE_OPERAND (arg, 0), current_class_ref)
9488 : 6 : && TREE_OPERAND (arg, 1) == lhs))
9489 : 117 : if (warning_at (loc, OPT_Wself_move,
9490 : : "moving %qE of type %qT to itself", orig_lhs, type))
9491 : : return true;
9492 : :
9493 : : return false;
9494 : : }
9495 : :
9496 : : /* For use from the C common bits. */
9497 : : tree
9498 : 4849 : build_modify_expr (location_t location,
9499 : : tree lhs, tree /*lhs_origtype*/,
9500 : : enum tree_code modifycode,
9501 : : location_t /*rhs_location*/, tree rhs,
9502 : : tree /*rhs_origtype*/)
9503 : : {
9504 : 4849 : return cp_build_modify_expr (location, lhs, modifycode, rhs,
9505 : 4849 : tf_warning_or_error);
9506 : : }
9507 : :
9508 : : /* Build an assignment expression of lvalue LHS from value RHS.
9509 : : MODIFYCODE is the code for a binary operator that we use
9510 : : to combine the old value of LHS with RHS to get the new value.
9511 : : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9512 : :
9513 : : C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9514 : :
9515 : : tree
9516 : 30007748 : cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9517 : : tree rhs, tsubst_flags_t complain)
9518 : : {
9519 : 30007748 : lhs = mark_lvalue_use_nonread (lhs);
9520 : :
9521 : 30007748 : tree result = NULL_TREE;
9522 : 30007748 : tree newrhs = rhs;
9523 : 30007748 : tree lhstype = TREE_TYPE (lhs);
9524 : 30007748 : tree olhs = lhs;
9525 : 30007748 : tree olhstype = lhstype;
9526 : 30007748 : bool plain_assign = (modifycode == NOP_EXPR);
9527 : 30007748 : bool compound_side_effects_p = false;
9528 : 30007748 : tree preeval = NULL_TREE;
9529 : :
9530 : : /* Avoid duplicate error messages from operands that had errors. */
9531 : 30007748 : if (error_operand_p (lhs) || error_operand_p (rhs))
9532 : 20 : return error_mark_node;
9533 : :
9534 : 30007807 : while (TREE_CODE (lhs) == COMPOUND_EXPR)
9535 : : {
9536 : 79 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9537 : 67 : compound_side_effects_p = true;
9538 : 79 : lhs = TREE_OPERAND (lhs, 1);
9539 : : }
9540 : :
9541 : : /* Handle control structure constructs used as "lvalues". Note that we
9542 : : leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9543 : 30007728 : switch (TREE_CODE (lhs))
9544 : : {
9545 : : /* Handle --foo = 5; as these are valid constructs in C++. */
9546 : 21 : case PREDECREMENT_EXPR:
9547 : 21 : case PREINCREMENT_EXPR:
9548 : 21 : if (compound_side_effects_p)
9549 : 9 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9550 : 21 : lhs = genericize_compound_lvalue (lhs);
9551 : 149 : maybe_add_compound:
9552 : : /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9553 : : and looked through the COMPOUND_EXPRs, readd them now around
9554 : : the resulting lhs. */
9555 : 149 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9556 : : {
9557 : 9 : lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9558 : 9 : tree *ptr = &TREE_OPERAND (lhs, 1);
9559 : 9 : for (olhs = TREE_OPERAND (olhs, 1);
9560 : 12 : TREE_CODE (olhs) == COMPOUND_EXPR;
9561 : 3 : olhs = TREE_OPERAND (olhs, 1))
9562 : : {
9563 : 3 : *ptr = build2 (COMPOUND_EXPR, lhstype,
9564 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9565 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9566 : : }
9567 : : }
9568 : : break;
9569 : :
9570 : 128 : case MODIFY_EXPR:
9571 : 128 : if (compound_side_effects_p)
9572 : 0 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9573 : 128 : lhs = genericize_compound_lvalue (lhs);
9574 : 128 : goto maybe_add_compound;
9575 : :
9576 : 0 : case MIN_EXPR:
9577 : 0 : case MAX_EXPR:
9578 : : /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9579 : : when neither operand has side-effects. */
9580 : 0 : if (!lvalue_or_else (lhs, lv_assign, complain))
9581 : 0 : return error_mark_node;
9582 : :
9583 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9584 : : && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9585 : :
9586 : 0 : lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9587 : 0 : build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9588 : : boolean_type_node,
9589 : 0 : TREE_OPERAND (lhs, 0),
9590 : 0 : TREE_OPERAND (lhs, 1)),
9591 : 0 : TREE_OPERAND (lhs, 0),
9592 : 0 : TREE_OPERAND (lhs, 1));
9593 : 200 : gcc_fallthrough ();
9594 : :
9595 : : /* Handle (a ? b : c) used as an "lvalue". */
9596 : 200 : case COND_EXPR:
9597 : 200 : {
9598 : : /* Produce (a ? (b = rhs) : (c = rhs))
9599 : : except that the RHS goes through a save-expr
9600 : : so the code to compute it is only emitted once. */
9601 : 200 : if (VOID_TYPE_P (TREE_TYPE (rhs)))
9602 : : {
9603 : 18 : if (complain & tf_error)
9604 : 24 : error_at (cp_expr_loc_or_loc (rhs, loc),
9605 : : "void value not ignored as it ought to be");
9606 : 18 : return error_mark_node;
9607 : : }
9608 : :
9609 : 182 : rhs = stabilize_expr (rhs, &preeval);
9610 : :
9611 : : /* Check this here to avoid odd errors when trying to convert
9612 : : a throw to the type of the COND_EXPR. */
9613 : 182 : if (!lvalue_or_else (lhs, lv_assign, complain))
9614 : 6 : return error_mark_node;
9615 : :
9616 : 176 : tree op1 = TREE_OPERAND (lhs, 1);
9617 : 176 : if (TREE_CODE (op1) != THROW_EXPR)
9618 : 170 : op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
9619 : : /* When sanitizing undefined behavior, even when rhs doesn't need
9620 : : stabilization at this point, the sanitization might add extra
9621 : : SAVE_EXPRs in there and so make sure there is no tree sharing
9622 : : in the rhs, otherwise those SAVE_EXPRs will have initialization
9623 : : only in one of the two branches. */
9624 : 176 : if (sanitize_flags_p (SANITIZE_UNDEFINED
9625 : : | SANITIZE_UNDEFINED_NONDEFAULT))
9626 : 3 : rhs = unshare_expr (rhs);
9627 : 176 : tree op2 = TREE_OPERAND (lhs, 2);
9628 : 176 : if (TREE_CODE (op2) != THROW_EXPR)
9629 : 170 : op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9630 : 176 : tree cond = build_conditional_expr (input_location,
9631 : 176 : TREE_OPERAND (lhs, 0), op1, op2,
9632 : : complain);
9633 : :
9634 : 176 : if (cond == error_mark_node)
9635 : : return cond;
9636 : : /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9637 : : and looked through the COMPOUND_EXPRs, readd them now around
9638 : : the resulting cond before adding the preevaluated rhs. */
9639 : 173 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9640 : : {
9641 : 15 : cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9642 : 15 : TREE_OPERAND (olhs, 0), cond);
9643 : 15 : tree *ptr = &TREE_OPERAND (cond, 1);
9644 : 15 : for (olhs = TREE_OPERAND (olhs, 1);
9645 : 18 : TREE_CODE (olhs) == COMPOUND_EXPR;
9646 : 3 : olhs = TREE_OPERAND (olhs, 1))
9647 : : {
9648 : 3 : *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9649 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9650 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9651 : : }
9652 : : }
9653 : : /* Make sure the code to compute the rhs comes out
9654 : : before the split. */
9655 : 173 : result = cond;
9656 : 173 : goto ret;
9657 : : }
9658 : :
9659 : : default:
9660 : : lhs = olhs;
9661 : : break;
9662 : : }
9663 : :
9664 : 30007528 : if (modifycode == INIT_EXPR)
9665 : : {
9666 : 3123256 : if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9667 : : /* Do the default thing. */;
9668 : 2953002 : else if (TREE_CODE (rhs) == CONSTRUCTOR)
9669 : : {
9670 : : /* Compound literal. */
9671 : 169 : if (! same_type_p (TREE_TYPE (rhs), lhstype))
9672 : : /* Call convert to generate an error; see PR 11063. */
9673 : 0 : rhs = convert (lhstype, rhs);
9674 : 169 : result = cp_build_init_expr (lhs, rhs);
9675 : 169 : TREE_SIDE_EFFECTS (result) = 1;
9676 : 169 : goto ret;
9677 : : }
9678 : 2952833 : else if (! MAYBE_CLASS_TYPE_P (lhstype))
9679 : : /* Do the default thing. */;
9680 : : else
9681 : : {
9682 : 40028 : releasing_vec rhs_vec = make_tree_vector_single (rhs);
9683 : 40028 : result = build_special_member_call (lhs, complete_ctor_identifier,
9684 : : &rhs_vec, lhstype, LOOKUP_NORMAL,
9685 : : complain);
9686 : 40028 : if (result == NULL_TREE)
9687 : 0 : return error_mark_node;
9688 : 40028 : goto ret;
9689 : 40028 : }
9690 : : }
9691 : : else
9692 : : {
9693 : 26884272 : lhs = require_complete_type (lhs, complain);
9694 : 26884272 : if (lhs == error_mark_node)
9695 : : return error_mark_node;
9696 : :
9697 : 26884229 : if (modifycode == NOP_EXPR)
9698 : : {
9699 : 22967495 : maybe_warn_self_move (loc, lhs, rhs);
9700 : :
9701 : 22967495 : if (c_dialect_objc ())
9702 : : {
9703 : 0 : result = objc_maybe_build_modify_expr (lhs, rhs);
9704 : 0 : if (result)
9705 : 0 : goto ret;
9706 : : }
9707 : :
9708 : : /* `operator=' is not an inheritable operator. */
9709 : 22967495 : if (! MAYBE_CLASS_TYPE_P (lhstype))
9710 : : /* Do the default thing. */;
9711 : : else
9712 : : {
9713 : 1899788 : result = build_new_op (input_location, MODIFY_EXPR,
9714 : : LOOKUP_NORMAL, lhs, rhs,
9715 : : make_node (NOP_EXPR), NULL_TREE,
9716 : : /*overload=*/NULL, complain);
9717 : 1899788 : if (result == NULL_TREE)
9718 : 0 : return error_mark_node;
9719 : 1899788 : goto ret;
9720 : : }
9721 : : lhstype = olhstype;
9722 : : }
9723 : : else
9724 : : {
9725 : 3916734 : tree init = NULL_TREE;
9726 : :
9727 : : /* A binary op has been requested. Combine the old LHS
9728 : : value with the RHS producing the value we should actually
9729 : : store into the LHS. */
9730 : 3916734 : gcc_assert (!((TYPE_REF_P (lhstype)
9731 : : && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9732 : : || MAYBE_CLASS_TYPE_P (lhstype)));
9733 : :
9734 : : /* Preevaluate the RHS to make sure its evaluation is complete
9735 : : before the lvalue-to-rvalue conversion of the LHS:
9736 : :
9737 : : [expr.ass] With respect to an indeterminately-sequenced
9738 : : function call, the operation of a compound assignment is a
9739 : : single evaluation. [ Note: Therefore, a function call shall
9740 : : not intervene between the lvalue-to-rvalue conversion and the
9741 : : side effect associated with any single compound assignment
9742 : : operator. -- end note ] */
9743 : 3916734 : lhs = cp_stabilize_reference (lhs);
9744 : 3916734 : rhs = decay_conversion (rhs, complain);
9745 : 3916734 : if (rhs == error_mark_node)
9746 : 123 : return error_mark_node;
9747 : :
9748 : 3916731 : {
9749 : 3916731 : auto_diagnostic_group d;
9750 : 3916731 : rhs = stabilize_expr (rhs, &init);
9751 : 3916731 : newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9752 : 3916731 : if (newrhs == error_mark_node)
9753 : : {
9754 : 120 : if (complain & tf_error)
9755 : 24 : inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9756 : 24 : modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9757 : 120 : return error_mark_node;
9758 : : }
9759 : 3916731 : }
9760 : :
9761 : 3916611 : if (init)
9762 : 262091 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9763 : :
9764 : : /* Now it looks like a plain assignment. */
9765 : 3916611 : modifycode = NOP_EXPR;
9766 : 3916611 : if (c_dialect_objc ())
9767 : : {
9768 : 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
9769 : 0 : if (result)
9770 : 0 : goto ret;
9771 : : }
9772 : : }
9773 : 24984318 : gcc_assert (!TYPE_REF_P (lhstype));
9774 : 24984318 : gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9775 : : }
9776 : :
9777 : : /* The left-hand side must be an lvalue. */
9778 : 28067377 : if (!lvalue_or_else (lhs, lv_assign, complain))
9779 : 166 : return error_mark_node;
9780 : :
9781 : : /* Warn about modifying something that is `const'. Don't warn if
9782 : : this is initialization. */
9783 : 28067211 : if (modifycode != INIT_EXPR
9784 : 28067211 : && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9785 : : /* Functions are not modifiable, even though they are
9786 : : lvalues. */
9787 : 24939591 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9788 : : /* If it's an aggregate and any field is const, then it is
9789 : : effectively const. */
9790 : 24939561 : || (CLASS_TYPE_P (lhstype)
9791 : 0 : && C_TYPE_FIELDS_READONLY (lhstype))))
9792 : : {
9793 : 44591 : if (complain & tf_error)
9794 : 77 : cxx_readonly_error (loc, lhs, lv_assign);
9795 : 44591 : return error_mark_node;
9796 : : }
9797 : :
9798 : : /* If storing into a structure or union member, it may have been given a
9799 : : lowered bitfield type. We need to convert to the declared type first,
9800 : : so retrieve it now. */
9801 : :
9802 : 28022620 : olhstype = unlowered_expr_type (lhs);
9803 : :
9804 : : /* Convert new value to destination type. */
9805 : :
9806 : 28022620 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
9807 : : {
9808 : 180 : int from_array;
9809 : :
9810 : 180 : if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9811 : : {
9812 : 12 : if (modifycode != INIT_EXPR)
9813 : : {
9814 : 12 : if (complain & tf_error)
9815 : 12 : error_at (loc,
9816 : : "assigning to an array from an initializer list");
9817 : 12 : return error_mark_node;
9818 : : }
9819 : 0 : if (check_array_initializer (lhs, lhstype, newrhs))
9820 : 0 : return error_mark_node;
9821 : 0 : newrhs = digest_init (lhstype, newrhs, complain);
9822 : 0 : if (newrhs == error_mark_node)
9823 : : return error_mark_node;
9824 : : }
9825 : :
9826 : : /* C++11 8.5/17: "If the destination type is an array of characters,
9827 : : an array of char16_t, an array of char32_t, or an array of wchar_t,
9828 : : and the initializer is a string literal...". */
9829 : 168 : else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9830 : : == STRING_CST)
9831 : 43 : && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9832 : 211 : && modifycode == INIT_EXPR)
9833 : : {
9834 : 28 : newrhs = digest_init (lhstype, newrhs, complain);
9835 : 28 : if (newrhs == error_mark_node)
9836 : : return error_mark_node;
9837 : : }
9838 : :
9839 : 140 : else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9840 : : TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9841 : : {
9842 : 20 : if (complain & tf_error)
9843 : 15 : error_at (loc, "incompatible types in assignment of %qT to %qT",
9844 : 15 : TREE_TYPE (rhs), lhstype);
9845 : 20 : return error_mark_node;
9846 : : }
9847 : :
9848 : : /* Allow array assignment in compiler-generated code. */
9849 : 120 : else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
9850 : : /* OK, used by coroutines (co-await-initlist1.C). */;
9851 : 118 : else if (!current_function_decl
9852 : 118 : || !DECL_DEFAULTED_FN (current_function_decl))
9853 : : {
9854 : : /* This routine is used for both initialization and assignment.
9855 : : Make sure the diagnostic message differentiates the context. */
9856 : 71 : if (complain & tf_error)
9857 : : {
9858 : 30 : if (modifycode == INIT_EXPR)
9859 : 9 : error_at (loc, "array used as initializer");
9860 : : else
9861 : 21 : error_at (loc, "invalid array assignment");
9862 : : }
9863 : 71 : return error_mark_node;
9864 : : }
9865 : :
9866 : 77 : from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9867 : 77 : ? 1 + (modifycode != INIT_EXPR) : 0;
9868 : 77 : result = build_vec_init (lhs, NULL_TREE, newrhs,
9869 : : /*explicit_value_init_p=*/false,
9870 : : from_array, complain);
9871 : 77 : goto ret;
9872 : : }
9873 : :
9874 : 28022440 : if (modifycode == INIT_EXPR)
9875 : : /* Calls with INIT_EXPR are all direct-initialization, so don't set
9876 : : LOOKUP_ONLYCONVERTING. */
9877 : 3083001 : newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9878 : : ICR_INIT, NULL_TREE, 0,
9879 : : complain | tf_no_cleanup);
9880 : : else
9881 : 24939439 : newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9882 : : NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9883 : :
9884 : 28022440 : if (!same_type_p (lhstype, olhstype))
9885 : 188484 : newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9886 : :
9887 : 28022440 : if (modifycode != INIT_EXPR)
9888 : : {
9889 : 24939439 : if (TREE_CODE (newrhs) == CALL_EXPR
9890 : 24939439 : && TYPE_NEEDS_CONSTRUCTING (lhstype))
9891 : 0 : newrhs = build_cplus_new (lhstype, newrhs, complain);
9892 : :
9893 : : /* Can't initialize directly from a TARGET_EXPR, since that would
9894 : : cause the lhs to be constructed twice, and possibly result in
9895 : : accidental self-initialization. So we force the TARGET_EXPR to be
9896 : : expanded without a target. */
9897 : 24939439 : if (TREE_CODE (newrhs) == TARGET_EXPR)
9898 : 84 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9899 : 84 : TARGET_EXPR_SLOT (newrhs));
9900 : : }
9901 : :
9902 : 28022440 : if (newrhs == error_mark_node)
9903 : : return error_mark_node;
9904 : :
9905 : 28021872 : if (c_dialect_objc () && flag_objc_gc)
9906 : : {
9907 : 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
9908 : :
9909 : 0 : if (result)
9910 : 0 : goto ret;
9911 : : }
9912 : :
9913 : 31104824 : result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
9914 : : lhstype, lhs, newrhs);
9915 : 28021872 : if (modifycode == INIT_EXPR)
9916 : 3082952 : set_target_expr_eliding (newrhs);
9917 : :
9918 : 28021872 : TREE_SIDE_EFFECTS (result) = 1;
9919 : 28021872 : if (!plain_assign)
9920 : 6999533 : suppress_warning (result, OPT_Wparentheses);
9921 : :
9922 : 21022339 : ret:
9923 : 29962107 : if (preeval)
9924 : 33 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
9925 : : return result;
9926 : : }
9927 : :
9928 : : cp_expr
9929 : 55309827 : build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9930 : : tree rhs, tree lookups, tsubst_flags_t complain)
9931 : : {
9932 : 55309827 : tree orig_lhs = lhs;
9933 : 55309827 : tree orig_rhs = rhs;
9934 : 55309827 : tree overload = NULL_TREE;
9935 : :
9936 : 55309827 : if (lhs == error_mark_node || rhs == error_mark_node)
9937 : 2063 : return cp_expr (error_mark_node, loc);
9938 : :
9939 : 55307764 : tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
9940 : :
9941 : 55307764 : if (processing_template_decl)
9942 : : {
9943 : 37356552 : if (type_dependent_expression_p (lhs)
9944 : 37356552 : || type_dependent_expression_p (rhs))
9945 : : {
9946 : 28654852 : tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
9947 : 28654852 : if (modifycode != NOP_EXPR)
9948 : 5507864 : TREE_TYPE (rval)
9949 : 11015728 : = build_dependent_operator_type (lookups, modifycode, true);
9950 : 28654852 : return rval;
9951 : : }
9952 : : }
9953 : :
9954 : 26652912 : tree rval;
9955 : 26652912 : if (modifycode == NOP_EXPR)
9956 : 21188024 : rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
9957 : : else
9958 : 5464888 : rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
9959 : : lhs, rhs, op, lookups, &overload, complain);
9960 : 26652912 : if (rval == error_mark_node)
9961 : 1808 : return error_mark_node;
9962 : 26651104 : if (processing_template_decl)
9963 : : {
9964 : 8701679 : if (overload != NULL_TREE)
9965 : 1148758 : return (build_min_non_dep_op_overload
9966 : 1148758 : (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
9967 : :
9968 : 7552921 : return (build_min_non_dep
9969 : 7552921 : (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
9970 : : }
9971 : 17949425 : return rval;
9972 : : }
9973 : :
9974 : : /* Helper function for get_delta_difference which assumes FROM is a base
9975 : : class of TO. Returns a delta for the conversion of pointer-to-member
9976 : : of FROM to pointer-to-member of TO. If the conversion is invalid and
9977 : : tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9978 : : returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9979 : : If C_CAST_P is true, this conversion is taking place as part of a
9980 : : C-style cast. */
9981 : :
9982 : : static tree
9983 : 29243 : get_delta_difference_1 (tree from, tree to, bool c_cast_p,
9984 : : tsubst_flags_t complain)
9985 : : {
9986 : 29243 : tree binfo;
9987 : 29243 : base_kind kind;
9988 : :
9989 : 58236 : binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
9990 : : &kind, complain);
9991 : :
9992 : 29243 : if (binfo == error_mark_node)
9993 : : {
9994 : 51 : if (!(complain & tf_error))
9995 : : return error_mark_node;
9996 : :
9997 : 51 : inform (input_location, " in pointer to member function conversion");
9998 : 51 : return size_zero_node;
9999 : : }
10000 : 29192 : else if (binfo)
10001 : : {
10002 : 29044 : if (kind != bk_via_virtual)
10003 : 28951 : return BINFO_OFFSET (binfo);
10004 : : else
10005 : : /* FROM is a virtual base class of TO. Issue an error or warning
10006 : : depending on whether or not this is a reinterpret cast. */
10007 : : {
10008 : 93 : if (!(complain & tf_error))
10009 : : return error_mark_node;
10010 : :
10011 : 78 : error ("pointer to member conversion via virtual base %qT",
10012 : 78 : BINFO_TYPE (binfo_from_vbase (binfo)));
10013 : :
10014 : 78 : return size_zero_node;
10015 : : }
10016 : : }
10017 : : else
10018 : : return NULL_TREE;
10019 : : }
10020 : :
10021 : : /* Get difference in deltas for different pointer to member function
10022 : : types. If the conversion is invalid and tf_error is not set in
10023 : : COMPLAIN, returns error_mark_node, otherwise returns an integer
10024 : : constant of type PTRDIFF_TYPE_NODE and its value is zero if the
10025 : : conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
10026 : : conversions as well. If C_CAST_P is true this conversion is taking
10027 : : place as part of a C-style cast.
10028 : :
10029 : : Note that the naming of FROM and TO is kind of backwards; the return
10030 : : value is what we add to a TO in order to get a FROM. They are named
10031 : : this way because we call this function to find out how to convert from
10032 : : a pointer to member of FROM to a pointer to member of TO. */
10033 : :
10034 : : static tree
10035 : 90948 : get_delta_difference (tree from, tree to,
10036 : : bool allow_inverse_p,
10037 : : bool c_cast_p, tsubst_flags_t complain)
10038 : : {
10039 : 90948 : auto_diagnostic_group d;
10040 : 90948 : tree result;
10041 : :
10042 : 90948 : if (same_type_ignoring_top_level_qualifiers_p (from, to))
10043 : : /* Pointer to member of incomplete class is permitted*/
10044 : 61853 : result = size_zero_node;
10045 : : else
10046 : 29095 : result = get_delta_difference_1 (from, to, c_cast_p, complain);
10047 : :
10048 : 90948 : if (result == error_mark_node)
10049 : : return error_mark_node;
10050 : :
10051 : 90933 : if (!result)
10052 : : {
10053 : 148 : if (!allow_inverse_p)
10054 : : {
10055 : 0 : if (!(complain & tf_error))
10056 : : return error_mark_node;
10057 : :
10058 : 0 : error_not_base_type (from, to);
10059 : 0 : inform (input_location, " in pointer to member conversion");
10060 : 0 : result = size_zero_node;
10061 : : }
10062 : : else
10063 : : {
10064 : 148 : result = get_delta_difference_1 (to, from, c_cast_p, complain);
10065 : :
10066 : 148 : if (result == error_mark_node)
10067 : : return error_mark_node;
10068 : :
10069 : 148 : if (result)
10070 : 148 : result = size_diffop_loc (input_location,
10071 : : size_zero_node, result);
10072 : : else
10073 : : {
10074 : 0 : if (!(complain & tf_error))
10075 : : return error_mark_node;
10076 : :
10077 : 0 : error_not_base_type (from, to);
10078 : 0 : inform (input_location, " in pointer to member conversion");
10079 : 0 : result = size_zero_node;
10080 : : }
10081 : : }
10082 : : }
10083 : :
10084 : 90933 : return convert_to_integer (ptrdiff_type_node, result);
10085 : 90948 : }
10086 : :
10087 : : /* Return a constructor for the pointer-to-member-function TYPE using
10088 : : the other components as specified. */
10089 : :
10090 : : tree
10091 : 62278 : build_ptrmemfunc1 (tree type, tree delta, tree pfn)
10092 : : {
10093 : 62278 : tree u = NULL_TREE;
10094 : 62278 : tree delta_field;
10095 : 62278 : tree pfn_field;
10096 : 62278 : vec<constructor_elt, va_gc> *v;
10097 : :
10098 : : /* Pull the FIELD_DECLs out of the type. */
10099 : 62278 : pfn_field = TYPE_FIELDS (type);
10100 : 62278 : delta_field = DECL_CHAIN (pfn_field);
10101 : :
10102 : : /* Make sure DELTA has the type we want. */
10103 : 62278 : delta = convert_and_check (input_location, delta_type_node, delta);
10104 : :
10105 : : /* Convert to the correct target type if necessary. */
10106 : 62278 : pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10107 : :
10108 : : /* Finish creating the initializer. */
10109 : 62278 : vec_alloc (v, 2);
10110 : 62278 : CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10111 : 62278 : CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10112 : 62278 : u = build_constructor (type, v);
10113 : 62278 : TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10114 : 62278 : TREE_STATIC (u) = (TREE_CONSTANT (u)
10115 : 62176 : && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10116 : : != NULL_TREE)
10117 : 124454 : && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10118 : : != NULL_TREE));
10119 : 62278 : return u;
10120 : : }
10121 : :
10122 : : /* Build a constructor for a pointer to member function. It can be
10123 : : used to initialize global variables, local variable, or used
10124 : : as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10125 : : want to be.
10126 : :
10127 : : If FORCE is nonzero, then force this conversion, even if
10128 : : we would rather not do it. Usually set when using an explicit
10129 : : cast. A C-style cast is being processed iff C_CAST_P is true.
10130 : :
10131 : : Return error_mark_node, if something goes wrong. */
10132 : :
10133 : : tree
10134 : 30621 : build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10135 : : tsubst_flags_t complain)
10136 : : {
10137 : 30621 : tree fn;
10138 : 30621 : tree pfn_type;
10139 : 30621 : tree to_type;
10140 : :
10141 : 30621 : if (error_operand_p (pfn))
10142 : 0 : return error_mark_node;
10143 : :
10144 : 30621 : pfn_type = TREE_TYPE (pfn);
10145 : 30621 : to_type = build_ptrmemfunc_type (type);
10146 : :
10147 : : /* Handle multiple conversions of pointer to member functions. */
10148 : 30621 : if (TYPE_PTRMEMFUNC_P (pfn_type))
10149 : : {
10150 : 28802 : tree delta = NULL_TREE;
10151 : 28802 : tree npfn = NULL_TREE;
10152 : 28802 : tree n;
10153 : :
10154 : 28802 : if (!force
10155 : 28802 : && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
10156 : : LOOKUP_NORMAL, complain))
10157 : : {
10158 : 0 : if (complain & tf_error)
10159 : 0 : error ("invalid conversion to type %qT from type %qT",
10160 : : to_type, pfn_type);
10161 : : else
10162 : 0 : return error_mark_node;
10163 : : }
10164 : :
10165 : 28802 : n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10166 : 28802 : TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10167 : : force,
10168 : : c_cast_p, complain);
10169 : 28802 : if (n == error_mark_node)
10170 : : return error_mark_node;
10171 : :
10172 : 28793 : STRIP_ANY_LOCATION_WRAPPER (pfn);
10173 : :
10174 : : /* We don't have to do any conversion to convert a
10175 : : pointer-to-member to its own type. But, we don't want to
10176 : : just return a PTRMEM_CST if there's an explicit cast; that
10177 : : cast should make the expression an invalid template argument. */
10178 : 28793 : if (TREE_CODE (pfn) != PTRMEM_CST
10179 : 28793 : && same_type_p (to_type, pfn_type))
10180 : : return pfn;
10181 : :
10182 : 28793 : if (TREE_SIDE_EFFECTS (pfn))
10183 : 8 : pfn = save_expr (pfn);
10184 : :
10185 : : /* Obtain the function pointer and the current DELTA. */
10186 : 28793 : if (TREE_CODE (pfn) == PTRMEM_CST)
10187 : 28705 : expand_ptrmemfunc_cst (pfn, &delta, &npfn);
10188 : : else
10189 : : {
10190 : 88 : npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
10191 : 88 : delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
10192 : : }
10193 : :
10194 : : /* Just adjust the DELTA field. */
10195 : 28793 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10196 : : (TREE_TYPE (delta), ptrdiff_type_node));
10197 : 28793 : if (!integer_zerop (n))
10198 : : {
10199 : 50 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
10200 : : n = cp_build_binary_op (input_location,
10201 : : LSHIFT_EXPR, n, integer_one_node,
10202 : : complain);
10203 : 50 : delta = cp_build_binary_op (input_location,
10204 : : PLUS_EXPR, delta, n, complain);
10205 : : }
10206 : 28793 : return build_ptrmemfunc1 (to_type, delta, npfn);
10207 : : }
10208 : :
10209 : : /* Handle null pointer to member function conversions. */
10210 : 1819 : if (null_ptr_cst_p (pfn))
10211 : : {
10212 : 756 : pfn = cp_build_c_cast (input_location,
10213 : 756 : TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10214 : : pfn, complain);
10215 : 756 : return build_ptrmemfunc1 (to_type,
10216 : : integer_zero_node,
10217 : 756 : pfn);
10218 : : }
10219 : :
10220 : 1063 : if (type_unknown_p (pfn))
10221 : 0 : return instantiate_type (type, pfn, complain);
10222 : :
10223 : 1063 : fn = TREE_OPERAND (pfn, 0);
10224 : 1063 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10225 : : /* In a template, we will have preserved the
10226 : : OFFSET_REF. */
10227 : : || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10228 : 1063 : return make_ptrmem_cst (to_type, fn);
10229 : : }
10230 : :
10231 : : /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10232 : : given by CST.
10233 : :
10234 : : ??? There is no consistency as to the types returned for the above
10235 : : values. Some code acts as if it were a sizetype and some as if it were
10236 : : integer_type_node. */
10237 : :
10238 : : void
10239 : 61782 : expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10240 : : {
10241 : 61782 : tree type = TREE_TYPE (cst);
10242 : 61782 : tree fn = PTRMEM_CST_MEMBER (cst);
10243 : 61782 : tree ptr_class, fn_class;
10244 : :
10245 : 61782 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10246 : :
10247 : : /* The class that the function belongs to. */
10248 : 61782 : fn_class = DECL_CONTEXT (fn);
10249 : :
10250 : : /* The class that we're creating a pointer to member of. */
10251 : 61782 : ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10252 : :
10253 : : /* First, calculate the adjustment to the function's class. */
10254 : 61782 : *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10255 : : /*c_cast_p=*/0, tf_warning_or_error);
10256 : :
10257 : 61782 : if (!DECL_VIRTUAL_P (fn))
10258 : : {
10259 : 60499 : tree t = build_addr_func (fn, tf_warning_or_error);
10260 : 60499 : if (TREE_CODE (t) == ADDR_EXPR)
10261 : 60499 : SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10262 : 60499 : *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10263 : : }
10264 : : else
10265 : : {
10266 : : /* If we're dealing with a virtual function, we have to adjust 'this'
10267 : : again, to point to the base which provides the vtable entry for
10268 : : fn; the call will do the opposite adjustment. */
10269 : 1283 : tree orig_class = DECL_CONTEXT (fn);
10270 : 1283 : tree binfo = binfo_or_else (orig_class, fn_class);
10271 : 1283 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10272 : : *delta, BINFO_OFFSET (binfo));
10273 : :
10274 : : /* We set PFN to the vtable offset at which the function can be
10275 : : found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10276 : : case delta is shifted left, and then incremented). */
10277 : 1283 : *pfn = DECL_VINDEX (fn);
10278 : 1283 : *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10279 : : TYPE_SIZE_UNIT (vtable_entry_type));
10280 : :
10281 : 1283 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10282 : : {
10283 : 1283 : case ptrmemfunc_vbit_in_pfn:
10284 : 1283 : *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10285 : : integer_one_node);
10286 : 1283 : break;
10287 : :
10288 : : case ptrmemfunc_vbit_in_delta:
10289 : : *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10290 : : *delta, integer_one_node);
10291 : : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10292 : : *delta, integer_one_node);
10293 : : break;
10294 : :
10295 : : default:
10296 : : gcc_unreachable ();
10297 : : }
10298 : :
10299 : 1283 : *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10300 : : }
10301 : 61782 : }
10302 : :
10303 : : /* Return an expression for PFN from the pointer-to-member function
10304 : : given by T. */
10305 : :
10306 : : static tree
10307 : 87306 : pfn_from_ptrmemfunc (tree t)
10308 : : {
10309 : 87306 : if (TREE_CODE (t) == PTRMEM_CST)
10310 : : {
10311 : 174 : tree delta;
10312 : 174 : tree pfn;
10313 : :
10314 : 174 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10315 : 174 : if (pfn)
10316 : 174 : return pfn;
10317 : : }
10318 : :
10319 : 87132 : return build_ptrmemfunc_access_expr (t, pfn_identifier);
10320 : : }
10321 : :
10322 : : /* Return an expression for DELTA from the pointer-to-member function
10323 : : given by T. */
10324 : :
10325 : : static tree
10326 : 87306 : delta_from_ptrmemfunc (tree t)
10327 : : {
10328 : 87306 : if (TREE_CODE (t) == PTRMEM_CST)
10329 : : {
10330 : 174 : tree delta;
10331 : 174 : tree pfn;
10332 : :
10333 : 174 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10334 : 174 : if (delta)
10335 : 174 : return delta;
10336 : : }
10337 : :
10338 : 87132 : return build_ptrmemfunc_access_expr (t, delta_identifier);
10339 : : }
10340 : :
10341 : : /* Convert value RHS to type TYPE as preparation for an assignment to
10342 : : an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10343 : : implicit conversion is. If FNDECL is non-NULL, we are doing the
10344 : : conversion in order to pass the PARMNUMth argument of FNDECL.
10345 : : If FNDECL is NULL, we are doing the conversion in function pointer
10346 : : argument passing, conversion in initialization, etc. */
10347 : :
10348 : : static tree
10349 : 108032892 : convert_for_assignment (tree type, tree rhs,
10350 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10351 : : tsubst_flags_t complain, int flags)
10352 : : {
10353 : 108032892 : tree rhstype;
10354 : 108032892 : enum tree_code coder;
10355 : :
10356 : 108032892 : location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10357 : 108032892 : bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10358 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10359 : : but preserve location wrappers. */
10360 : 108032892 : if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10361 : 108032892 : && !location_wrapper_p (rhs))
10362 : 7025 : rhs = TREE_OPERAND (rhs, 0);
10363 : :
10364 : : /* Handle [dcl.init.list] direct-list-initialization from
10365 : : single element of enumeration with a fixed underlying type. */
10366 : 108032892 : if (is_direct_enum_init (type, rhs))
10367 : : {
10368 : 20 : tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10369 : 20 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10370 : : {
10371 : 11 : warning_sentinel w (warn_useless_cast);
10372 : 11 : warning_sentinel w2 (warn_ignored_qualifiers);
10373 : 11 : rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10374 : 11 : }
10375 : : else
10376 : 9 : rhs = error_mark_node;
10377 : : }
10378 : :
10379 : 108032892 : rhstype = TREE_TYPE (rhs);
10380 : 108032892 : coder = TREE_CODE (rhstype);
10381 : :
10382 : 1209490 : if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10383 : 109242276 : && vector_types_convertible_p (type, rhstype, true))
10384 : : {
10385 : 1209375 : rhs = mark_rvalue_use (rhs);
10386 : 1209375 : return convert (type, rhs);
10387 : : }
10388 : :
10389 : 106823517 : if (rhs == error_mark_node || rhstype == error_mark_node)
10390 : : return error_mark_node;
10391 : 106823508 : if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10392 : : return error_mark_node;
10393 : :
10394 : : /* The RHS of an assignment cannot have void type. */
10395 : 106823508 : if (coder == VOID_TYPE)
10396 : : {
10397 : 48 : if (complain & tf_error)
10398 : 30 : error_at (rhs_loc, "void value not ignored as it ought to be");
10399 : 48 : return error_mark_node;
10400 : : }
10401 : :
10402 : 106823460 : if (c_dialect_objc ())
10403 : : {
10404 : 0 : int parmno;
10405 : 0 : tree selector;
10406 : 0 : tree rname = fndecl;
10407 : :
10408 : 0 : switch (errtype)
10409 : : {
10410 : : case ICR_ASSIGN:
10411 : : parmno = -1;
10412 : : break;
10413 : 0 : case ICR_INIT:
10414 : 0 : parmno = -2;
10415 : 0 : break;
10416 : 0 : default:
10417 : 0 : selector = objc_message_selector ();
10418 : 0 : parmno = parmnum;
10419 : 0 : if (selector && parmno > 1)
10420 : : {
10421 : 0 : rname = selector;
10422 : 0 : parmno -= 1;
10423 : : }
10424 : : }
10425 : :
10426 : 0 : if (objc_compare_types (type, rhstype, parmno, rname))
10427 : : {
10428 : 0 : rhs = mark_rvalue_use (rhs);
10429 : 0 : return convert (type, rhs);
10430 : : }
10431 : : }
10432 : :
10433 : : /* [expr.ass]
10434 : :
10435 : : The expression is implicitly converted (clause _conv_) to the
10436 : : cv-unqualified type of the left operand.
10437 : :
10438 : : We allow bad conversions here because by the time we get to this point
10439 : : we are committed to doing the conversion. If we end up doing a bad
10440 : : conversion, convert_like will complain. */
10441 : 106823460 : if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10442 : : {
10443 : : /* When -Wno-pmf-conversions is use, we just silently allow
10444 : : conversions from pointers-to-members to plain pointers. If
10445 : : the conversion doesn't work, cp_convert will complain. */
10446 : 1463 : if (!warn_pmf2ptr
10447 : 9 : && TYPE_PTR_P (type)
10448 : 1472 : && TYPE_PTRMEMFUNC_P (rhstype))
10449 : 9 : rhs = cp_convert (strip_top_quals (type), rhs, complain);
10450 : : else
10451 : : {
10452 : 1454 : if (complain & tf_error)
10453 : : {
10454 : 1267 : auto_diagnostic_group d;
10455 : :
10456 : : /* If the right-hand side has unknown type, then it is an
10457 : : overloaded function. Call instantiate_type to get error
10458 : : messages. */
10459 : 1267 : if (rhstype == unknown_type_node)
10460 : : {
10461 : 197 : tree r = instantiate_type (type, rhs, tf_warning_or_error);
10462 : : /* -fpermissive might allow this; recurse. */
10463 : 197 : if (!seen_error ())
10464 : 15 : return convert_for_assignment (type, r, errtype, fndecl,
10465 : : parmnum, complain, flags);
10466 : : }
10467 : 1070 : else if (fndecl)
10468 : 92 : complain_about_bad_argument (rhs_loc,
10469 : : rhstype, type,
10470 : : fndecl, parmnum);
10471 : : else
10472 : : {
10473 : 978 : range_label_for_type_mismatch label (rhstype, type);
10474 : 978 : gcc_rich_location richloc
10475 : : (rhs_loc,
10476 : : has_loc ? &label : NULL,
10477 : 978 : has_loc ? highlight_colors::percent_h : NULL);
10478 : :
10479 : 978 : switch (errtype)
10480 : : {
10481 : 0 : case ICR_DEFAULT_ARGUMENT:
10482 : 0 : error_at (&richloc,
10483 : : "cannot convert %qH to %qI in default argument",
10484 : : rhstype, type);
10485 : 0 : break;
10486 : 6 : case ICR_ARGPASS:
10487 : 6 : error_at (&richloc,
10488 : : "cannot convert %qH to %qI in argument passing",
10489 : : rhstype, type);
10490 : 6 : break;
10491 : 0 : case ICR_CONVERTING:
10492 : 0 : error_at (&richloc, "cannot convert %qH to %qI",
10493 : : rhstype, type);
10494 : 0 : break;
10495 : 542 : case ICR_INIT:
10496 : 542 : error_at (&richloc,
10497 : : "cannot convert %qH to %qI in initialization",
10498 : : rhstype, type);
10499 : 542 : break;
10500 : 16 : case ICR_RETURN:
10501 : 16 : error_at (&richloc, "cannot convert %qH to %qI in return",
10502 : : rhstype, type);
10503 : 16 : break;
10504 : 414 : case ICR_ASSIGN:
10505 : 414 : error_at (&richloc,
10506 : : "cannot convert %qH to %qI in assignment",
10507 : : rhstype, type);
10508 : 414 : break;
10509 : 0 : default:
10510 : 0 : gcc_unreachable();
10511 : : }
10512 : 978 : }
10513 : :
10514 : : /* See if we can be more helpful. */
10515 : 1252 : maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
10516 : :
10517 : 1252 : if (TYPE_PTR_P (rhstype)
10518 : 411 : && TYPE_PTR_P (type)
10519 : 396 : && CLASS_TYPE_P (TREE_TYPE (rhstype))
10520 : 278 : && CLASS_TYPE_P (TREE_TYPE (type))
10521 : 1314 : && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10522 : 3 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10523 : : (TREE_TYPE (rhstype))),
10524 : 3 : "class type %qT is incomplete", TREE_TYPE (rhstype));
10525 : 1267 : }
10526 : 1439 : return error_mark_node;
10527 : : }
10528 : : }
10529 : 106822006 : if (warn_suggest_attribute_format)
10530 : : {
10531 : 2363 : const enum tree_code codel = TREE_CODE (type);
10532 : 2363 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10533 : 592 : && coder == codel
10534 : 504 : && check_missing_format_attribute (type, rhstype)
10535 : 2381 : && (complain & tf_warning))
10536 : 18 : switch (errtype)
10537 : : {
10538 : 0 : case ICR_ARGPASS:
10539 : 0 : case ICR_DEFAULT_ARGUMENT:
10540 : 0 : if (fndecl)
10541 : 0 : warning (OPT_Wsuggest_attribute_format,
10542 : : "parameter %qP of %qD might be a candidate "
10543 : : "for a format attribute", parmnum, fndecl);
10544 : : else
10545 : 0 : warning (OPT_Wsuggest_attribute_format,
10546 : : "parameter might be a candidate "
10547 : : "for a format attribute");
10548 : : break;
10549 : 0 : case ICR_CONVERTING:
10550 : 0 : warning (OPT_Wsuggest_attribute_format,
10551 : : "target of conversion might be a candidate "
10552 : : "for a format attribute");
10553 : 0 : break;
10554 : 6 : case ICR_INIT:
10555 : 6 : warning (OPT_Wsuggest_attribute_format,
10556 : : "target of initialization might be a candidate "
10557 : : "for a format attribute");
10558 : 6 : break;
10559 : 6 : case ICR_RETURN:
10560 : 6 : warning (OPT_Wsuggest_attribute_format,
10561 : : "return type might be a candidate "
10562 : : "for a format attribute");
10563 : 6 : break;
10564 : 6 : case ICR_ASSIGN:
10565 : 6 : warning (OPT_Wsuggest_attribute_format,
10566 : : "left-hand side of assignment might be a candidate "
10567 : : "for a format attribute");
10568 : 6 : break;
10569 : 0 : default:
10570 : 0 : gcc_unreachable();
10571 : : }
10572 : : }
10573 : :
10574 : 106822006 : if (TREE_CODE (type) == BOOLEAN_TYPE)
10575 : 20395373 : maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
10576 : :
10577 : 106822006 : if (complain & tf_warning)
10578 : 105886922 : warn_for_address_of_packed_member (type, rhs);
10579 : :
10580 : 106822006 : return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10581 : 106822006 : complain, flags);
10582 : : }
10583 : :
10584 : : /* Convert RHS to be of type TYPE.
10585 : : If EXP is nonzero, it is the target of the initialization.
10586 : : ERRTYPE indicates what kind of error the implicit conversion is.
10587 : :
10588 : : Two major differences between the behavior of
10589 : : `convert_for_assignment' and `convert_for_initialization'
10590 : : are that references are bashed in the former, while
10591 : : copied in the latter, and aggregates are assigned in
10592 : : the former (operator=) while initialized in the
10593 : : latter (X(X&)).
10594 : :
10595 : : If using constructor make sure no conversion operator exists, if one does
10596 : : exist, an ambiguity exists. */
10597 : :
10598 : : tree
10599 : 94286118 : convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10600 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10601 : : tsubst_flags_t complain)
10602 : : {
10603 : 94286118 : enum tree_code codel = TREE_CODE (type);
10604 : 94286118 : tree rhstype;
10605 : 94286118 : enum tree_code coder;
10606 : :
10607 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10608 : : Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10609 : 94286118 : if (TREE_CODE (rhs) == NOP_EXPR
10610 : 4370959 : && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10611 : 94625088 : && codel != REFERENCE_TYPE)
10612 : 338970 : rhs = TREE_OPERAND (rhs, 0);
10613 : :
10614 : 94286118 : if (type == error_mark_node
10615 : 94286080 : || rhs == error_mark_node
10616 : 188572143 : || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10617 : : return error_mark_node;
10618 : :
10619 : 94286025 : if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10620 : : ;
10621 : 84696033 : else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10622 : 1335697 : && TREE_CODE (type) != ARRAY_TYPE
10623 : 1335697 : && (!TYPE_REF_P (type)
10624 : 5534 : || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10625 : 83365867 : || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10626 : 11768 : && !TYPE_REFFN_P (type))
10627 : 168051563 : || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10628 : 1340509 : rhs = decay_conversion (rhs, complain);
10629 : :
10630 : 94286025 : rhstype = TREE_TYPE (rhs);
10631 : 94286025 : coder = TREE_CODE (rhstype);
10632 : :
10633 : 94286025 : if (coder == ERROR_MARK)
10634 : 9 : return error_mark_node;
10635 : :
10636 : : /* We accept references to incomplete types, so we can
10637 : : return here before checking if RHS is of complete type. */
10638 : :
10639 : 94286016 : if (codel == REFERENCE_TYPE)
10640 : : {
10641 : 4745985 : auto_diagnostic_group d;
10642 : : /* This should eventually happen in convert_arguments. */
10643 : 4745985 : int savew = 0, savee = 0;
10644 : :
10645 : 4745985 : if (fndecl)
10646 : 238508 : savew = warningcount + werrorcount, savee = errorcount;
10647 : 4745985 : rhs = initialize_reference (type, rhs, flags, complain);
10648 : :
10649 : 4745985 : if (fndecl
10650 : 4745985 : && (warningcount + werrorcount > savew || errorcount > savee))
10651 : 36 : inform (get_fndecl_argument_location (fndecl, parmnum),
10652 : : "in passing argument %P of %qD", parmnum, fndecl);
10653 : 4745985 : return rhs;
10654 : 4745985 : }
10655 : :
10656 : 89540031 : if (exp != 0)
10657 : 3083001 : exp = require_complete_type (exp, complain);
10658 : 89540031 : if (exp == error_mark_node)
10659 : : return error_mark_node;
10660 : :
10661 : 89540031 : type = complete_type (type);
10662 : :
10663 : 89540031 : if (DIRECT_INIT_EXPR_P (type, rhs))
10664 : : /* Don't try to do copy-initialization if we already have
10665 : : direct-initialization. */
10666 : : return rhs;
10667 : :
10668 : 89534757 : if (MAYBE_CLASS_TYPE_P (type))
10669 : 6441319 : return perform_implicit_conversion_flags (type, rhs, complain, flags);
10670 : :
10671 : 83093438 : return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10672 : 83093438 : complain, flags);
10673 : : }
10674 : :
10675 : : /* If RETVAL is the address of, or a reference to, a local variable or
10676 : : temporary give an appropriate warning and return true. */
10677 : :
10678 : : static bool
10679 : 32925205 : maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10680 : : {
10681 : 32925803 : tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10682 : 32925803 : tree whats_returned = fold_for_warn (retval);
10683 : 32925803 : if (!loc)
10684 : 32925803 : loc = cp_expr_loc_or_input_loc (retval);
10685 : :
10686 : 39990383 : for (;;)
10687 : : {
10688 : 39990383 : if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10689 : 432148 : whats_returned = TREE_OPERAND (whats_returned, 1);
10690 : 39558235 : else if (CONVERT_EXPR_P (whats_returned)
10691 : 32944891 : || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10692 : 6632432 : whats_returned = TREE_OPERAND (whats_returned, 0);
10693 : : else
10694 : : break;
10695 : : }
10696 : :
10697 : 32925803 : if (TREE_CODE (whats_returned) == TARGET_EXPR
10698 : 32925803 : && is_std_init_list (TREE_TYPE (whats_returned)))
10699 : : {
10700 : 14 : tree init = TARGET_EXPR_INITIAL (whats_returned);
10701 : 14 : if (TREE_CODE (init) == CONSTRUCTOR)
10702 : : /* Pull out the array address. */
10703 : 6 : whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10704 : 8 : else if (TREE_CODE (init) == INDIRECT_REF)
10705 : : /* The source of a trivial copy looks like *(T*)&var. */
10706 : 5 : whats_returned = TREE_OPERAND (init, 0);
10707 : : else
10708 : : return false;
10709 : 11 : STRIP_NOPS (whats_returned);
10710 : : }
10711 : :
10712 : : /* As a special case, we handle a call to std::move or std::forward. */
10713 : 32925800 : if (TREE_CODE (whats_returned) == CALL_EXPR
10714 : 32925800 : && (is_std_move_p (whats_returned)
10715 : 8998260 : || is_std_forward_p (whats_returned)))
10716 : : {
10717 : 586 : tree arg = CALL_EXPR_ARG (whats_returned, 0);
10718 : 586 : return maybe_warn_about_returning_address_of_local (arg, loc);
10719 : : }
10720 : :
10721 : 32925214 : if (TREE_CODE (whats_returned) != ADDR_EXPR)
10722 : : return false;
10723 : 703133 : whats_returned = TREE_OPERAND (whats_returned, 0);
10724 : :
10725 : 703133 : while (TREE_CODE (whats_returned) == COMPONENT_REF
10726 : 1379873 : || TREE_CODE (whats_returned) == ARRAY_REF)
10727 : 676740 : whats_returned = TREE_OPERAND (whats_returned, 0);
10728 : :
10729 : 703133 : if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10730 : 703133 : || TREE_CODE (whats_returned) == TARGET_EXPR)
10731 : : {
10732 : 50 : if (TYPE_REF_P (valtype))
10733 : : /* P2748 made this an error in C++26. */
10734 : 72 : emit_diagnostic (cxx_dialect >= cxx26 ? DK_PERMERROR : DK_WARNING,
10735 : : loc, OPT_Wreturn_local_addr,
10736 : : "returning reference to temporary");
10737 : 9 : else if (TYPE_PTR_P (valtype))
10738 : 3 : warning_at (loc, OPT_Wreturn_local_addr,
10739 : : "returning pointer to temporary");
10740 : 6 : else if (is_std_init_list (valtype))
10741 : 6 : warning_at (loc, OPT_Winit_list_lifetime,
10742 : : "returning temporary %<initializer_list%> does not extend "
10743 : : "the lifetime of the underlying array");
10744 : 50 : return true;
10745 : : }
10746 : :
10747 : 703083 : STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10748 : :
10749 : 703083 : if (DECL_P (whats_returned)
10750 : 62340 : && DECL_NAME (whats_returned)
10751 : 62340 : && DECL_FUNCTION_SCOPE_P (whats_returned)
10752 : 14393 : && !is_capture_proxy (whats_returned)
10753 : 717473 : && !(TREE_STATIC (whats_returned)
10754 : : || TREE_PUBLIC (whats_returned)))
10755 : : {
10756 : 106 : if (DECL_DECOMPOSITION_P (whats_returned)
10757 : 39 : && !DECL_DECOMP_IS_BASE (whats_returned)
10758 : 185 : && DECL_HAS_VALUE_EXPR_P (whats_returned))
10759 : : {
10760 : : /* When returning address of a structured binding, if the structured
10761 : : binding is not a reference, continue normally, if it is a
10762 : : reference, recurse on the initializer of the structured
10763 : : binding. */
10764 : 39 : tree base = DECL_DECOMP_BASE (whats_returned);
10765 : 39 : if (TYPE_REF_P (TREE_TYPE (base)))
10766 : : {
10767 : 15 : if (tree init = DECL_INITIAL (base))
10768 : : return maybe_warn_about_returning_address_of_local (init, loc);
10769 : : else
10770 : : return false;
10771 : : }
10772 : : }
10773 : 131 : bool w = false;
10774 : 131 : auto_diagnostic_group d;
10775 : 131 : if (TYPE_REF_P (valtype))
10776 : 81 : w = warning_at (loc, OPT_Wreturn_local_addr,
10777 : : "reference to local variable %qD returned",
10778 : : whats_returned);
10779 : 50 : else if (is_std_init_list (valtype))
10780 : 3 : w = warning_at (loc, OPT_Winit_list_lifetime,
10781 : : "returning local %<initializer_list%> variable %qD "
10782 : : "does not extend the lifetime of the underlying array",
10783 : : whats_returned);
10784 : 47 : else if (POINTER_TYPE_P (valtype)
10785 : 41 : && TREE_CODE (whats_returned) == LABEL_DECL)
10786 : 6 : w = warning_at (loc, OPT_Wreturn_local_addr,
10787 : : "address of label %qD returned",
10788 : : whats_returned);
10789 : 41 : else if (POINTER_TYPE_P (valtype))
10790 : 35 : w = warning_at (loc, OPT_Wreturn_local_addr,
10791 : : "address of local variable %qD returned",
10792 : : whats_returned);
10793 : 125 : if (w)
10794 : 103 : inform (DECL_SOURCE_LOCATION (whats_returned),
10795 : : "declared here");
10796 : 131 : return true;
10797 : 131 : }
10798 : :
10799 : : return false;
10800 : : }
10801 : :
10802 : : /* Returns true if DECL is in the std namespace. */
10803 : :
10804 : : bool
10805 : 64176231 : decl_in_std_namespace_p (tree decl)
10806 : : {
10807 : 75254161 : while (decl)
10808 : : {
10809 : 74834482 : decl = decl_namespace_context (decl);
10810 : 74834482 : if (DECL_NAMESPACE_STD_P (decl))
10811 : : return true;
10812 : : /* Allow inline namespaces inside of std namespace, e.g. with
10813 : : --enable-symvers=gnu-versioned-namespace std::forward would be
10814 : : actually std::_8::forward. */
10815 : 35166853 : if (!DECL_NAMESPACE_INLINE_P (decl))
10816 : : return false;
10817 : 11077930 : decl = CP_DECL_CONTEXT (decl);
10818 : : }
10819 : : return false;
10820 : : }
10821 : :
10822 : : /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10823 : :
10824 : : static bool
10825 : 8998260 : is_std_forward_p (tree fn)
10826 : : {
10827 : : /* std::forward only takes one argument. */
10828 : 8998260 : if (call_expr_nargs (fn) != 1)
10829 : : return false;
10830 : :
10831 : 4168011 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10832 : 4168011 : if (!decl_in_std_namespace_p (fndecl))
10833 : : return false;
10834 : :
10835 : 1416932 : tree name = DECL_NAME (fndecl);
10836 : 1416932 : return name && id_equal (name, "forward");
10837 : : }
10838 : :
10839 : : /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10840 : :
10841 : : static bool
10842 : 9004414 : is_std_move_p (tree fn)
10843 : : {
10844 : : /* std::move only takes one argument. */
10845 : 9004414 : if (call_expr_nargs (fn) != 1)
10846 : : return false;
10847 : :
10848 : 4173751 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10849 : 4173751 : if (!decl_in_std_namespace_p (fndecl))
10850 : : return false;
10851 : :
10852 : 1422249 : tree name = DECL_NAME (fndecl);
10853 : 1422249 : return name && id_equal (name, "move");
10854 : : }
10855 : :
10856 : : /* Returns true if RETVAL is a good candidate for the NRVO as per
10857 : : [class.copy.elision]. FUNCTYPE is the type the function is declared
10858 : : to return. */
10859 : :
10860 : : static bool
10861 : 41646695 : can_do_nrvo_p (tree retval, tree functype)
10862 : : {
10863 : 41646695 : if (functype == error_mark_node)
10864 : : return false;
10865 : 41646654 : if (retval)
10866 : 40234180 : STRIP_ANY_LOCATION_WRAPPER (retval);
10867 : 41646654 : tree result = DECL_RESULT (current_function_decl);
10868 : 41646654 : return (retval != NULL_TREE
10869 : 40234180 : && !processing_template_decl
10870 : : /* Must be a local, automatic variable. */
10871 : 33650692 : && VAR_P (retval)
10872 : 3065582 : && DECL_CONTEXT (retval) == current_function_decl
10873 : 2404708 : && !TREE_STATIC (retval)
10874 : : /* And not a lambda or anonymous union proxy. */
10875 : 2402088 : && !DECL_HAS_VALUE_EXPR_P (retval)
10876 : 2401879 : && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10877 : : /* The cv-unqualified type of the returned value must be the
10878 : : same as the cv-unqualified return type of the
10879 : : function. */
10880 : 2401786 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10881 : : TYPE_MAIN_VARIANT (functype))
10882 : : /* And the returned value must be non-volatile. */
10883 : 44041594 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
10884 : : }
10885 : :
10886 : : /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
10887 : : would otherwise return in memory. */
10888 : :
10889 : : static bool
10890 : 41646380 : want_nrvo_p (tree retval, tree functype)
10891 : : {
10892 : 41646380 : return (can_do_nrvo_p (retval, functype)
10893 : 41646380 : && aggregate_value_p (functype, current_function_decl));
10894 : : }
10895 : :
10896 : : /* Like can_do_nrvo_p, but we check if we're trying to move a class
10897 : : prvalue. */
10898 : :
10899 : : static bool
10900 : 1249 : can_elide_copy_prvalue_p (tree retval, tree functype)
10901 : : {
10902 : 1249 : if (functype == error_mark_node)
10903 : : return false;
10904 : 1249 : if (retval)
10905 : 1249 : STRIP_ANY_LOCATION_WRAPPER (retval);
10906 : 1249 : return (retval != NULL_TREE
10907 : 1249 : && !glvalue_p (retval)
10908 : 135 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10909 : : TYPE_MAIN_VARIANT (functype))
10910 : 1357 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
10911 : : }
10912 : :
10913 : : /* If we should treat RETVAL, an expression being returned, as if it were
10914 : : designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10915 : : NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10916 : : context (rather than throw). */
10917 : :
10918 : : tree
10919 : 7572254 : treat_lvalue_as_rvalue_p (tree expr, bool return_p)
10920 : : {
10921 : 7572254 : if (cxx_dialect == cxx98)
10922 : : return NULL_TREE;
10923 : :
10924 : 7561959 : tree retval = expr;
10925 : 7561959 : STRIP_ANY_LOCATION_WRAPPER (retval);
10926 : 7561959 : if (REFERENCE_REF_P (retval))
10927 : 499095 : retval = TREE_OPERAND (retval, 0);
10928 : :
10929 : : /* An implicitly movable entity is a variable of automatic storage duration
10930 : : that is either a non-volatile object or (C++20) an rvalue reference to a
10931 : : non-volatile object type. */
10932 : 7561959 : if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
10933 : 6740482 : || TREE_CODE (retval) == PARM_DECL)
10934 : 1081645 : && !TREE_STATIC (retval)
10935 : 1054753 : && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
10936 : 1054748 : && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
10937 : 92135 : || (cxx_dialect >= cxx20
10938 : 74067 : && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
10939 : 6598780 : return NULL_TREE;
10940 : :
10941 : : /* If the expression in a return or co_return statement is a (possibly
10942 : : parenthesized) id-expression that names an implicitly movable entity
10943 : : declared in the body or parameter-declaration-clause of the innermost
10944 : : enclosing function or lambda-expression, */
10945 : 963179 : if (return_p)
10946 : : {
10947 : 963067 : if (DECL_CONTEXT (retval) != current_function_decl)
10948 : : return NULL_TREE;
10949 : 963010 : expr = move (expr);
10950 : 963010 : if (expr == error_mark_node)
10951 : : return NULL_TREE;
10952 : 963008 : return set_implicit_rvalue_p (expr);
10953 : : }
10954 : :
10955 : : /* if the id-expression (possibly parenthesized) is the operand of
10956 : : a throw-expression, and names an implicitly movable entity that belongs
10957 : : to a scope that does not contain the compound-statement of the innermost
10958 : : lambda-expression, try-block, or function-try-block (if any) whose
10959 : : compound-statement or ctor-initializer contains the throw-expression. */
10960 : :
10961 : : /* C++20 added move on throw of parms. */
10962 : 112 : if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
10963 : : return NULL_TREE;
10964 : :
10965 : : /* We don't check for lambda-expression here, because we should not get past
10966 : : the DECL_HAS_VALUE_EXPR_P check above. */
10967 : 99 : for (cp_binding_level *b = current_binding_level;
10968 : 190 : b->kind != sk_namespace; b = b->level_chain)
10969 : : {
10970 : 192 : for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
10971 : 78 : if (decl == retval)
10972 : 66 : return set_implicit_rvalue_p (move (expr));
10973 : 114 : if (b->kind == sk_try)
10974 : : return NULL_TREE;
10975 : : }
10976 : :
10977 : 10 : return set_implicit_rvalue_p (move (expr));
10978 : : }
10979 : :
10980 : : /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
10981 : : is true). EXPR is the std::move expression; TYPE is the type of the object
10982 : : being initialized. */
10983 : :
10984 : : void
10985 : 117107227 : maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
10986 : : {
10987 : 117107227 : if (!(warn_pessimizing_move || warn_redundant_move))
10988 : : return;
10989 : :
10990 : 4556594 : const location_t loc = cp_expr_loc_or_input_loc (expr);
10991 : :
10992 : : /* C++98 doesn't know move. */
10993 : 4556594 : if (cxx_dialect < cxx11)
10994 : : return;
10995 : :
10996 : : /* Wait until instantiation time, since we can't gauge if we should do
10997 : : the NRVO until then. */
10998 : 4492046 : if (processing_template_decl)
10999 : : return;
11000 : :
11001 : : /* This is only interesting for class types. */
11002 : 4412862 : if (!CLASS_TYPE_P (type))
11003 : : return;
11004 : :
11005 : 84731 : bool wrapped_p = false;
11006 : : /* A a = std::move (A()); */
11007 : 84731 : if (TREE_CODE (expr) == TREE_LIST)
11008 : : {
11009 : 7237 : if (list_length (expr) == 1)
11010 : : {
11011 : 4786 : expr = TREE_VALUE (expr);
11012 : 4786 : wrapped_p = true;
11013 : : }
11014 : : else
11015 : : return;
11016 : : }
11017 : : /* A a = {std::move (A())};
11018 : : A a{std::move (A())}; */
11019 : 77494 : else if (TREE_CODE (expr) == CONSTRUCTOR)
11020 : : {
11021 : 5477 : if (CONSTRUCTOR_NELTS (expr) == 1)
11022 : : {
11023 : 734 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
11024 : 734 : wrapped_p = true;
11025 : : }
11026 : : else
11027 : : return;
11028 : : }
11029 : :
11030 : : /* First, check if this is a call to std::move. */
11031 : 5348 : if (!REFERENCE_REF_P (expr)
11032 : 81532 : || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
11033 : : return;
11034 : 2295 : tree fn = TREE_OPERAND (expr, 0);
11035 : 2295 : if (!is_std_move_p (fn))
11036 : : return;
11037 : 1837 : tree arg = CALL_EXPR_ARG (fn, 0);
11038 : 1837 : if (TREE_CODE (arg) != NOP_EXPR)
11039 : : return;
11040 : : /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
11041 : : and implicitly-movable warnings. */
11042 : 1837 : if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
11043 : : {
11044 : 1249 : arg = TREE_OPERAND (arg, 0);
11045 : 1249 : arg = TREE_OPERAND (arg, 0);
11046 : 1249 : arg = convert_from_reference (arg);
11047 : 1249 : if (can_elide_copy_prvalue_p (arg, type))
11048 : : {
11049 : 108 : auto_diagnostic_group d;
11050 : 108 : if (warning_at (loc, OPT_Wpessimizing_move,
11051 : : "moving a temporary object prevents copy elision"))
11052 : 108 : inform (loc, "remove %<std::move%> call");
11053 : 108 : }
11054 : : /* The rest of the warnings is only relevant for when we are returning
11055 : : from a function. */
11056 : 1249 : if (!return_p)
11057 : : return;
11058 : :
11059 : 315 : tree moved;
11060 : : /* Warn if we could do copy elision were it not for the move. */
11061 : 315 : if (can_do_nrvo_p (arg, type))
11062 : : {
11063 : 48 : auto_diagnostic_group d;
11064 : 48 : if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
11065 : 48 : && warning_at (loc, OPT_Wpessimizing_move,
11066 : : "moving a local object in a return statement "
11067 : : "prevents copy elision"))
11068 : 42 : inform (loc, "remove %<std::move%> call");
11069 : 48 : }
11070 : : /* Warn if the move is redundant. It is redundant when we would
11071 : : do maybe-rvalue overload resolution even without std::move. */
11072 : 267 : else if (warn_redundant_move
11073 : : /* This doesn't apply for return {std::move (t)};. */
11074 : 160 : && !wrapped_p
11075 : 157 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11076 : 363 : && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
11077 : : {
11078 : : /* Make sure that overload resolution would actually succeed
11079 : : if we removed the std::move call. */
11080 : 54 : tree t = convert_for_initialization (NULL_TREE, type,
11081 : : moved,
11082 : : (LOOKUP_NORMAL
11083 : : | LOOKUP_ONLYCONVERTING),
11084 : : ICR_RETURN, NULL_TREE, 0,
11085 : : tf_none);
11086 : : /* If this worked, implicit rvalue would work, so the call to
11087 : : std::move is redundant. */
11088 : 54 : if (t != error_mark_node)
11089 : : {
11090 : 54 : auto_diagnostic_group d;
11091 : 54 : if (warning_at (loc, OPT_Wredundant_move,
11092 : : "redundant move in return statement"))
11093 : 54 : inform (loc, "remove %<std::move%> call");
11094 : 54 : }
11095 : : }
11096 : : }
11097 : : /* Also try to warn about redundant std::move in code such as
11098 : : T f (const T& t)
11099 : : {
11100 : : return std::move(t);
11101 : : }
11102 : : for which EXPR will be something like
11103 : : *std::move<const T&> ((const struct T &) (const struct T *) t)
11104 : : and where the std::move does nothing if T does not have a T(const T&&)
11105 : : constructor, because the argument is const. It will not use T(T&&)
11106 : : because that would mean losing the const. */
11107 : 588 : else if (warn_redundant_move
11108 : 457 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11109 : 64 : && TYPE_REF_P (TREE_TYPE (arg))
11110 : 652 : && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
11111 : : {
11112 : 18 : tree rtype = TREE_TYPE (TREE_TYPE (arg));
11113 : 18 : if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
11114 : 9 : return;
11115 : : /* Check for the unlikely case there's T(const T&&) (we don't care if
11116 : : it's deleted). */
11117 : 54 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
11118 : 30 : if (move_fn_p (fn))
11119 : : {
11120 : 15 : tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11121 : 15 : if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
11122 : 6 : return;
11123 : : }
11124 : 9 : auto_diagnostic_group d;
11125 : 18 : if (return_p
11126 : 9 : ? warning_at (loc, OPT_Wredundant_move,
11127 : : "redundant move in return statement")
11128 : 9 : : warning_at (loc, OPT_Wredundant_move,
11129 : : "redundant move in initialization"))
11130 : 9 : inform (loc, "remove %<std::move%> call");
11131 : 9 : }
11132 : : }
11133 : :
11134 : : /* Check that returning RETVAL from the current function is valid.
11135 : : Return an expression explicitly showing all conversions required to
11136 : : change RETVAL into the function return type, and to assign it to
11137 : : the DECL_RESULT for the function. Set *NO_WARNING to true if
11138 : : code reaches end of non-void function warning shouldn't be issued
11139 : : on this RETURN_EXPR. Set *DANGLING to true if code returns the
11140 : : address of a local variable. */
11141 : :
11142 : : tree
11143 : 98214501 : check_return_expr (tree retval, bool *no_warning, bool *dangling)
11144 : : {
11145 : 98214501 : tree result;
11146 : : /* The type actually returned by the function. */
11147 : 98214501 : tree valtype;
11148 : : /* The type the function is declared to return, or void if
11149 : : the declared type is incomplete. */
11150 : 98214501 : tree functype;
11151 : 98214501 : int fn_returns_value_p;
11152 : 98214501 : location_t loc = cp_expr_loc_or_input_loc (retval);
11153 : :
11154 : 98214501 : *no_warning = false;
11155 : 98214501 : *dangling = false;
11156 : :
11157 : : /* A `volatile' function is one that isn't supposed to return, ever.
11158 : : (This is a G++ extension, used to get better code for functions
11159 : : that call the `volatile' function.) */
11160 : 98214501 : if (TREE_THIS_VOLATILE (current_function_decl))
11161 : 9 : warning (0, "function declared %<noreturn%> has a %<return%> statement");
11162 : :
11163 : : /* Check for various simple errors. */
11164 : 196429002 : if (DECL_DESTRUCTOR_P (current_function_decl))
11165 : : {
11166 : 1582 : if (retval)
11167 : 3 : error_at (loc, "returning a value from a destructor");
11168 : :
11169 : 1582 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11170 : 0 : retval = current_class_ptr;
11171 : : else
11172 : : return NULL_TREE;
11173 : : }
11174 : 98212919 : else if (DECL_CONSTRUCTOR_P (current_function_decl))
11175 : : {
11176 : 39257 : if (in_function_try_handler)
11177 : : /* If a return statement appears in a handler of the
11178 : : function-try-block of a constructor, the program is ill-formed. */
11179 : 0 : error ("cannot return from a handler of a function-try-block of a constructor");
11180 : 39257 : else if (retval)
11181 : : /* You can't return a value from a constructor. */
11182 : 3 : error_at (loc, "returning a value from a constructor");
11183 : :
11184 : 39257 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11185 : 0 : retval = current_class_ptr;
11186 : : else
11187 : : return NULL_TREE;
11188 : : }
11189 : :
11190 : 98173662 : const tree saved_retval = retval;
11191 : :
11192 : 98173662 : if (processing_template_decl)
11193 : : {
11194 : 63908251 : current_function_returns_value = 1;
11195 : :
11196 : 63908251 : if (check_for_bare_parameter_packs (retval))
11197 : 10 : return error_mark_node;
11198 : :
11199 : : /* If one of the types might be void, we can't tell whether we're
11200 : : returning a value. */
11201 : 63908241 : if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11202 : 24857740 : && !FNDECL_USED_AUTO (current_function_decl))
11203 : 39050501 : || (retval != NULL_TREE
11204 : 38044850 : && (TREE_TYPE (retval) == NULL_TREE
11205 : 26693833 : || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11206 : 43290787 : goto dependent;
11207 : : }
11208 : :
11209 : 54882865 : functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11210 : :
11211 : : /* Deduce auto return type from a return statement. */
11212 : 54882865 : if (FNDECL_USED_AUTO (current_function_decl))
11213 : : {
11214 : 705901 : tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11215 : 705901 : tree auto_node;
11216 : 705901 : tree type;
11217 : :
11218 : 705901 : if (!retval && !is_auto (pattern))
11219 : : {
11220 : : /* Give a helpful error message. */
11221 : 3 : auto_diagnostic_group d;
11222 : 3 : error ("return-statement with no value, in function returning %qT",
11223 : : pattern);
11224 : 3 : inform (input_location, "only plain %<auto%> return type can be "
11225 : : "deduced to %<void%>");
11226 : 3 : type = error_mark_node;
11227 : 3 : }
11228 : 705898 : else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11229 : : {
11230 : 6 : error ("returning initializer list");
11231 : 6 : type = error_mark_node;
11232 : : }
11233 : : else
11234 : : {
11235 : 705892 : if (!retval)
11236 : 4704 : retval = void_node;
11237 : 705892 : auto_node = type_uses_auto (pattern);
11238 : 705892 : type = do_auto_deduction (pattern, retval, auto_node,
11239 : : tf_warning_or_error, adc_return_type);
11240 : : }
11241 : :
11242 : 705901 : if (type == error_mark_node)
11243 : : /* Leave it. */;
11244 : 705706 : else if (functype == pattern)
11245 : 445805 : apply_deduced_return_type (current_function_decl, type);
11246 : 259901 : else if (!same_type_p (type, functype))
11247 : : {
11248 : 21 : if (LAMBDA_FUNCTION_P (current_function_decl))
11249 : 6 : error_at (loc, "inconsistent types %qT and %qT deduced for "
11250 : : "lambda return type", functype, type);
11251 : : else
11252 : 9 : error_at (loc, "inconsistent deduction for auto return type: "
11253 : : "%qT and then %qT", functype, type);
11254 : : }
11255 : : functype = type;
11256 : : }
11257 : :
11258 : 54882865 : result = DECL_RESULT (current_function_decl);
11259 : 54882865 : valtype = TREE_TYPE (result);
11260 : 54882865 : gcc_assert (valtype != NULL_TREE);
11261 : 54882865 : fn_returns_value_p = !VOID_TYPE_P (valtype);
11262 : :
11263 : : /* Check for a return statement with no return value in a function
11264 : : that's supposed to return a value. */
11265 : 54882865 : if (!retval && fn_returns_value_p)
11266 : : {
11267 : 35 : if (functype != error_mark_node)
11268 : 32 : permerror (input_location, "return-statement with no value, in "
11269 : : "function returning %qT", valtype);
11270 : : /* Remember that this function did return. */
11271 : 35 : current_function_returns_value = 1;
11272 : : /* But suppress NRV .*/
11273 : 35 : current_function_return_value = error_mark_node;
11274 : : /* And signal caller that TREE_NO_WARNING should be set on the
11275 : : RETURN_EXPR to avoid control reaches end of non-void function
11276 : : warnings in tree-cfg.cc. */
11277 : 35 : *no_warning = true;
11278 : : }
11279 : : /* Check for a return statement with a value in a function that
11280 : : isn't supposed to return a value. */
11281 : 54882830 : else if (retval && !fn_returns_value_p)
11282 : : {
11283 : 216980 : if (VOID_TYPE_P (TREE_TYPE (retval)))
11284 : : /* You can return a `void' value from a function of `void'
11285 : : type. In that case, we have to evaluate the expression for
11286 : : its side-effects. */
11287 : 216955 : finish_expr_stmt (retval);
11288 : 25 : else if (retval != error_mark_node)
11289 : 20 : permerror (loc, "return-statement with a value, in function "
11290 : : "returning %qT", valtype);
11291 : 216980 : current_function_returns_null = 1;
11292 : :
11293 : : /* There's really no value to return, after all. */
11294 : 216980 : return NULL_TREE;
11295 : : }
11296 : 54665850 : else if (!retval)
11297 : : /* Remember that this function can sometimes return without a
11298 : : value. */
11299 : 1412457 : current_function_returns_null = 1;
11300 : : else
11301 : : /* Remember that this function did return a value. */
11302 : 53253393 : current_function_returns_value = 1;
11303 : :
11304 : : /* Check for erroneous operands -- but after giving ourselves a
11305 : : chance to provide an error about returning a value from a void
11306 : : function. */
11307 : 54665885 : if (error_operand_p (retval))
11308 : : {
11309 : 958 : current_function_return_value = error_mark_node;
11310 : 958 : return error_mark_node;
11311 : : }
11312 : :
11313 : : /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11314 : 109329854 : if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11315 : 29105 : && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11316 : 2847 : && ! flag_check_new
11317 : 54667765 : && retval && null_ptr_cst_p (retval))
11318 : 24 : warning (0, "%<operator new%> must not return NULL unless it is "
11319 : : "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11320 : :
11321 : : /* Effective C++ rule 15. See also start_function. */
11322 : 54664927 : if (warn_ecpp
11323 : 42 : && DECL_NAME (current_function_decl) == assign_op_identifier
11324 : 54664957 : && !type_dependent_expression_p (retval))
11325 : : {
11326 : 27 : bool warn = true;
11327 : :
11328 : : /* The function return type must be a reference to the current
11329 : : class. */
11330 : 27 : if (TYPE_REF_P (valtype)
11331 : 45 : && same_type_ignoring_top_level_qualifiers_p
11332 : 18 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11333 : : {
11334 : : /* Returning '*this' is obviously OK. */
11335 : 18 : if (retval == current_class_ref)
11336 : : warn = false;
11337 : : /* If we are calling a function whose return type is the same of
11338 : : the current class reference, it is ok. */
11339 : 6 : else if (INDIRECT_REF_P (retval)
11340 : 6 : && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11341 : : warn = false;
11342 : : }
11343 : :
11344 : : if (warn)
11345 : 9 : warning_at (loc, OPT_Weffc__,
11346 : : "%<operator=%> should return a reference to %<*this%>");
11347 : : }
11348 : :
11349 : 54664927 : if (dependent_type_p (functype)
11350 : 54664927 : || type_dependent_expression_p (retval))
11351 : : {
11352 : 56309334 : dependent:
11353 : : /* We should not have changed the return value. */
11354 : 56309334 : gcc_assert (retval == saved_retval);
11355 : : /* We don't know if this is an lvalue or rvalue use, but
11356 : : either way we can mark it as read. */
11357 : 56309334 : mark_exp_read (retval);
11358 : 56309334 : return retval;
11359 : : }
11360 : :
11361 : : /* The fabled Named Return Value optimization, as per [class.copy]/15:
11362 : :
11363 : : [...] For a function with a class return type, if the expression
11364 : : in the return statement is the name of a local object, and the cv-
11365 : : unqualified type of the local object is the same as the function
11366 : : return type, an implementation is permitted to omit creating the tem-
11367 : : porary object to hold the function return value [...]
11368 : :
11369 : : So, if this is a value-returning function that always returns the same
11370 : : local variable, remember it.
11371 : :
11372 : : We choose the first suitable variable even if the function sometimes
11373 : : returns something else, but only if the variable is out of scope at the
11374 : : other return sites, or else we run the risk of clobbering the variable we
11375 : : chose if the other returned expression uses the chosen variable somehow.
11376 : :
11377 : : We don't currently do this if the first return is a non-variable, as it
11378 : : would be complicated to determine whether an NRV selected later was in
11379 : : scope at the point of the earlier return. We also don't currently support
11380 : : multiple variables with non-overlapping scopes (53637).
11381 : :
11382 : : See finish_function and finalize_nrv for the rest of this optimization. */
11383 : 41646380 : tree bare_retval = NULL_TREE;
11384 : 41646380 : if (retval)
11385 : : {
11386 : 40233903 : retval = maybe_undo_parenthesized_ref (retval);
11387 : 40233903 : bare_retval = tree_strip_any_location_wrapper (retval);
11388 : : }
11389 : :
11390 : 41646380 : bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11391 : 41646380 : if (fn_returns_value_p && flag_elide_constructors
11392 : 40233882 : && current_function_return_value != bare_retval)
11393 : : {
11394 : 40223604 : if (named_return_value_okay_p
11395 : 207752 : && current_function_return_value == NULL_TREE)
11396 : 168026 : current_function_return_value = bare_retval;
11397 : 40055578 : else if (current_function_return_value
11398 : 7326789 : && VAR_P (current_function_return_value)
11399 : 134 : && DECL_NAME (current_function_return_value)
11400 : 40055712 : && !decl_in_scope_p (current_function_return_value))
11401 : : {
11402 : : /* The earlier NRV is out of scope at this point, so it's safe to
11403 : 67 : leave it alone; the current return can't refer to it. */;
11404 : 67 : if (named_return_value_okay_p
11405 : 67 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11406 : : {
11407 : 8 : warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11408 : : bare_retval);
11409 : 8 : suppress_warning (current_function_decl, OPT_Wnrvo);
11410 : : }
11411 : : }
11412 : : else
11413 : : {
11414 : 40055511 : if ((named_return_value_okay_p
11415 : 40015795 : || (current_function_return_value
11416 : 7287006 : && current_function_return_value != error_mark_node))
11417 : 40055556 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11418 : : {
11419 : 39695 : warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11420 : : current_function_decl);
11421 : 39695 : suppress_warning (current_function_decl, OPT_Wnrvo);
11422 : : }
11423 : 40055511 : current_function_return_value = error_mark_node;
11424 : : }
11425 : : }
11426 : :
11427 : : /* We don't need to do any conversions when there's nothing being
11428 : : returned. */
11429 : 41646380 : if (!retval)
11430 : : return NULL_TREE;
11431 : :
11432 : 40233903 : if (!named_return_value_okay_p)
11433 : 40015873 : maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11434 : :
11435 : : /* Do any required conversions. */
11436 : 80467806 : if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11437 : : /* No conversions are required. */
11438 : : ;
11439 : : else
11440 : : {
11441 : 40233903 : int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11442 : :
11443 : : /* The functype's return type will have been set to void, if it
11444 : : was an incomplete type. Just treat this as 'return;' */
11445 : 40233903 : if (VOID_TYPE_P (functype))
11446 : 6 : return error_mark_node;
11447 : :
11448 : : /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11449 : : treated as an rvalue for the purposes of overload resolution to
11450 : : favor move constructors over copy constructors.
11451 : :
11452 : : Note that these conditions are similar to, but not as strict as,
11453 : : the conditions for the named return value optimization. */
11454 : 40233897 : bool converted = false;
11455 : 40233897 : tree moved;
11456 : : /* Until C++23, this was only interesting for class type, but in C++23,
11457 : : we should do the below when we're converting from/to a class/reference
11458 : : (a non-scalar type). */
11459 : 40233897 : if ((cxx_dialect < cxx23
11460 : 4440426 : ? CLASS_TYPE_P (functype)
11461 : 8527119 : : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11462 : 47207239 : && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11463 : : /* In C++20 and earlier we treat the return value as an rvalue
11464 : : that can bind to lvalue refs. In C++23, such an expression is just
11465 : : an xvalue (see reference_binding). */
11466 : : retval = moved;
11467 : :
11468 : : /* The call in a (lambda) thunk needs no conversions. */
11469 : 40233897 : if (TREE_CODE (retval) == CALL_EXPR
11470 : 40233897 : && call_from_lambda_thunk_p (retval))
11471 : : converted = true;
11472 : :
11473 : : /* First convert the value to the function's return type, then
11474 : : to the type of return value's location to handle the
11475 : : case that functype is smaller than the valtype. */
11476 : 40224415 : if (!converted)
11477 : 40224415 : retval = convert_for_initialization
11478 : 40224415 : (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11479 : : tf_warning_or_error);
11480 : 40233897 : retval = convert (valtype, retval);
11481 : :
11482 : : /* If the conversion failed, treat this just like `return;'. */
11483 : 40233897 : if (retval == error_mark_node)
11484 : : {
11485 : : /* And suppress NRV. */
11486 : 190 : current_function_return_value = error_mark_node;
11487 : 190 : return retval;
11488 : : }
11489 : : /* We can't initialize a register from a AGGR_INIT_EXPR. */
11490 : 40233707 : else if (! cfun->returns_struct
11491 : 38843648 : && TREE_CODE (retval) == TARGET_EXPR
11492 : 44398993 : && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == AGGR_INIT_EXPR)
11493 : 725026 : retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11494 : 725026 : TARGET_EXPR_SLOT (retval));
11495 : 39508681 : else if (!processing_template_decl
11496 : 32925205 : && maybe_warn_about_returning_address_of_local (retval, loc)
11497 : 39508862 : && INDIRECT_TYPE_P (valtype))
11498 : 166 : *dangling = true;
11499 : : }
11500 : :
11501 : : /* A naive attempt to reduce the number of -Wdangling-reference false
11502 : : positives: if we know that this function can return a variable with
11503 : : static storage duration rather than one of its parameters, suppress
11504 : : the warning. */
11505 : 40233707 : if (warn_dangling_reference
11506 : 361678 : && TYPE_REF_P (functype)
11507 : 23778 : && bare_retval
11508 : : && VAR_P (bare_retval)
11509 : 23778 : && TREE_STATIC (bare_retval))
11510 : 50 : suppress_warning (current_function_decl, OPT_Wdangling_reference);
11511 : :
11512 : 40233707 : if (processing_template_decl)
11513 : : return saved_retval;
11514 : :
11515 : : /* Actually copy the value returned into the appropriate location. */
11516 : 33650231 : if (retval && retval != result)
11517 : 33650231 : retval = cp_build_init_expr (result, retval);
11518 : :
11519 : 33650231 : if (current_function_return_value == bare_retval)
11520 : 178298 : INIT_EXPR_NRV_P (retval) = true;
11521 : :
11522 : 33650231 : if (tree set = maybe_set_retval_sentinel ())
11523 : 131517 : retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11524 : :
11525 : : return retval;
11526 : : }
11527 : :
11528 : :
11529 : : /* Returns nonzero if the pointer-type FROM can be converted to the
11530 : : pointer-type TO via a qualification conversion. If CONSTP is -1,
11531 : : then we return nonzero if the pointers are similar, and the
11532 : : cv-qualification signature of FROM is a proper subset of that of TO.
11533 : :
11534 : : If CONSTP is positive, then all outer pointers have been
11535 : : const-qualified. */
11536 : :
11537 : : static bool
11538 : 115447900 : comp_ptr_ttypes_real (tree to, tree from, int constp)
11539 : : {
11540 : 115447900 : bool to_more_cv_qualified = false;
11541 : 115447900 : bool is_opaque_pointer = false;
11542 : :
11543 : 508160 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11544 : : {
11545 : 115956060 : if (TREE_CODE (to) != TREE_CODE (from))
11546 : : return false;
11547 : :
11548 : 72373994 : if (TREE_CODE (from) == OFFSET_TYPE
11549 : 72373994 : && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11550 : : TYPE_OFFSET_BASETYPE (to)))
11551 : : return false;
11552 : :
11553 : : /* Const and volatile mean something different for function and
11554 : : array types, so the usual checks are not appropriate. We'll
11555 : : check the array type elements in further iterations. */
11556 : 72373994 : if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11557 : : {
11558 : 72159088 : if (!at_least_as_qualified_p (to, from))
11559 : : return false;
11560 : :
11561 : 63687818 : if (!at_least_as_qualified_p (from, to))
11562 : : {
11563 : 44090349 : if (constp == 0)
11564 : : return false;
11565 : : to_more_cv_qualified = true;
11566 : : }
11567 : :
11568 : 63687559 : if (constp > 0)
11569 : 63683189 : constp &= TYPE_READONLY (to);
11570 : : }
11571 : :
11572 : 63902465 : if (VECTOR_TYPE_P (to))
11573 : 5936 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11574 : :
11575 : : /* P0388R4 allows a conversion from int[N] to int[] but not the
11576 : : other way round. When both arrays have bounds but they do
11577 : : not match, then no conversion is possible. */
11578 : 63902465 : if (TREE_CODE (to) == ARRAY_TYPE
11579 : 63902465 : && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11580 : : return false;
11581 : :
11582 : 63902210 : if (!TYPE_PTR_P (to)
11583 : : && !TYPE_PTRDATAMEM_P (to)
11584 : : /* CWG 330 says we need to look through arrays. */
11585 : : && TREE_CODE (to) != ARRAY_TYPE)
11586 : 63394050 : return ((constp >= 0 || to_more_cv_qualified)
11587 : 63394050 : && (is_opaque_pointer
11588 : 63393552 : || same_type_ignoring_top_level_qualifiers_p (to, from)));
11589 : : }
11590 : : }
11591 : :
11592 : : /* When comparing, say, char ** to char const **, this function takes
11593 : : the 'char *' and 'char const *'. Do not pass non-pointer/reference
11594 : : types to this function. */
11595 : :
11596 : : int
11597 : 115447328 : comp_ptr_ttypes (tree to, tree from)
11598 : : {
11599 : 115447328 : return comp_ptr_ttypes_real (to, from, 1);
11600 : : }
11601 : :
11602 : : /* Returns true iff FNTYPE is a non-class type that involves
11603 : : error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11604 : : if a parameter type is ill-formed. */
11605 : :
11606 : : bool
11607 : 609604 : error_type_p (const_tree type)
11608 : : {
11609 : 652384 : tree t;
11610 : :
11611 : 652384 : switch (TREE_CODE (type))
11612 : : {
11613 : : case ERROR_MARK:
11614 : : return true;
11615 : :
11616 : 42691 : case POINTER_TYPE:
11617 : 42691 : case REFERENCE_TYPE:
11618 : 42691 : case OFFSET_TYPE:
11619 : 42691 : return error_type_p (TREE_TYPE (type));
11620 : :
11621 : 3639 : case FUNCTION_TYPE:
11622 : 3639 : case METHOD_TYPE:
11623 : 3639 : if (error_type_p (TREE_TYPE (type)))
11624 : : return true;
11625 : 10041 : for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11626 : 6408 : if (error_type_p (TREE_VALUE (t)))
11627 : : return true;
11628 : : return false;
11629 : :
11630 : 195524 : case RECORD_TYPE:
11631 : 195524 : if (TYPE_PTRMEMFUNC_P (type))
11632 : 89 : return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11633 : : return false;
11634 : :
11635 : : default:
11636 : : return false;
11637 : : }
11638 : : }
11639 : :
11640 : : /* Returns true if to and from are (possibly multi-level) pointers to the same
11641 : : type or inheritance-related types, regardless of cv-quals. */
11642 : :
11643 : : bool
11644 : 82363984 : ptr_reasonably_similar (const_tree to, const_tree from)
11645 : : {
11646 : 974 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11647 : : {
11648 : : /* Any target type is similar enough to void. */
11649 : 82364958 : if (VOID_TYPE_P (to))
11650 : 991 : return !error_type_p (from);
11651 : 82363967 : if (VOID_TYPE_P (from))
11652 : 595640 : return !error_type_p (to);
11653 : :
11654 : 81768327 : if (TREE_CODE (to) != TREE_CODE (from))
11655 : : return false;
11656 : :
11657 : 38646823 : if (TREE_CODE (from) == OFFSET_TYPE
11658 : 38646823 : && comptypes (TYPE_OFFSET_BASETYPE (to),
11659 : 3 : TYPE_OFFSET_BASETYPE (from),
11660 : : COMPARE_BASE | COMPARE_DERIVED))
11661 : 3 : continue;
11662 : :
11663 : 38646817 : if (VECTOR_TYPE_P (to)
11664 : 38646817 : && vector_types_convertible_p (to, from, false))
11665 : : return true;
11666 : :
11667 : 38646733 : if (TREE_CODE (to) == INTEGER_TYPE
11668 : 38646733 : && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11669 : : return true;
11670 : :
11671 : 38497940 : if (TREE_CODE (to) == FUNCTION_TYPE)
11672 : 1463 : return !error_type_p (to) && !error_type_p (from);
11673 : :
11674 : 38496477 : if (!TYPE_PTR_P (to))
11675 : : {
11676 : : /* When either type is incomplete avoid DERIVED_FROM_P,
11677 : : which may call complete_type (c++/57942). */
11678 : 38495506 : bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11679 : : return comptypes
11680 : 38495506 : (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11681 : 38495506 : b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11682 : : }
11683 : 974 : }
11684 : : }
11685 : :
11686 : : /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11687 : : pointer-to-member types) are the same, ignoring cv-qualification at
11688 : : all levels. CB says how we should behave when comparing array bounds. */
11689 : :
11690 : : bool
11691 : 618867 : comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11692 : : {
11693 : 618867 : bool is_opaque_pointer = false;
11694 : :
11695 : 619858 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11696 : : {
11697 : 1238725 : if (TREE_CODE (to) != TREE_CODE (from))
11698 : : return false;
11699 : :
11700 : 921144 : if (TREE_CODE (from) == OFFSET_TYPE
11701 : 921108 : && same_type_p (TYPE_OFFSET_BASETYPE (from),
11702 : : TYPE_OFFSET_BASETYPE (to)))
11703 : 36 : continue;
11704 : :
11705 : 921072 : if (VECTOR_TYPE_P (to))
11706 : 7485 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11707 : :
11708 : 921072 : if (TREE_CODE (to) == ARRAY_TYPE
11709 : : /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11710 : : we must fail. */
11711 : 921072 : && !comp_array_types (to, from, cb, /*strict=*/false))
11712 : : return false;
11713 : :
11714 : : /* CWG 330 says we need to look through arrays. */
11715 : 920648 : if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11716 : 300826 : return (is_opaque_pointer
11717 : 300826 : || same_type_ignoring_top_level_qualifiers_p (to, from));
11718 : : }
11719 : : }
11720 : :
11721 : : /* Returns the type qualifiers for this type, including the qualifiers on the
11722 : : elements for an array type. */
11723 : :
11724 : : int
11725 : 42912690991 : cp_type_quals (const_tree type)
11726 : : {
11727 : 42912690991 : int quals;
11728 : : /* This CONST_CAST is okay because strip_array_types returns its
11729 : : argument unmodified and we assign it to a const_tree. */
11730 : 42912690991 : type = strip_array_types (CONST_CAST_TREE (type));
11731 : 42912690991 : if (type == error_mark_node
11732 : : /* Quals on a FUNCTION_TYPE are memfn quals. */
11733 : 42884996122 : || TREE_CODE (type) == FUNCTION_TYPE)
11734 : : return TYPE_UNQUALIFIED;
11735 : 42846369578 : quals = TYPE_QUALS (type);
11736 : : /* METHOD and REFERENCE_TYPEs should never have quals. */
11737 : 42846369578 : gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11738 : : && !TYPE_REF_P (type))
11739 : : || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11740 : : == TYPE_UNQUALIFIED));
11741 : : return quals;
11742 : : }
11743 : :
11744 : : /* Returns the function-ref-qualifier for TYPE */
11745 : :
11746 : : cp_ref_qualifier
11747 : 2318624120 : type_memfn_rqual (const_tree type)
11748 : : {
11749 : 2318624120 : gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11750 : :
11751 : 2318624120 : if (!FUNCTION_REF_QUALIFIED (type))
11752 : : return REF_QUAL_NONE;
11753 : 36394001 : else if (FUNCTION_RVALUE_QUALIFIED (type))
11754 : : return REF_QUAL_RVALUE;
11755 : : else
11756 : 18239433 : return REF_QUAL_LVALUE;
11757 : : }
11758 : :
11759 : : /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11760 : : METHOD_TYPE. */
11761 : :
11762 : : int
11763 : 922206020 : type_memfn_quals (const_tree type)
11764 : : {
11765 : 922206020 : if (TREE_CODE (type) == FUNCTION_TYPE)
11766 : 69572782 : return TYPE_QUALS (type);
11767 : 852633238 : else if (TREE_CODE (type) == METHOD_TYPE)
11768 : 852633238 : return cp_type_quals (class_of_this_parm (type));
11769 : : else
11770 : 0 : gcc_unreachable ();
11771 : : }
11772 : :
11773 : : /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11774 : : MEMFN_QUALS and its ref-qualifier to RQUAL. */
11775 : :
11776 : : tree
11777 : 48543562 : apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11778 : : {
11779 : : /* Could handle METHOD_TYPE here if necessary. */
11780 : 48543562 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11781 : 48543562 : if (TYPE_QUALS (type) == memfn_quals
11782 : 48543562 : && type_memfn_rqual (type) == rqual)
11783 : : return type;
11784 : :
11785 : : /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11786 : : complex. */
11787 : 576240 : tree result = build_qualified_type (type, memfn_quals);
11788 : 576240 : return build_ref_qualified_type (result, rqual);
11789 : : }
11790 : :
11791 : : /* Returns nonzero if TYPE is const or volatile. */
11792 : :
11793 : : bool
11794 : 921671013 : cv_qualified_p (const_tree type)
11795 : : {
11796 : 921671013 : int quals = cp_type_quals (type);
11797 : 921671013 : return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11798 : : }
11799 : :
11800 : : /* Returns nonzero if the TYPE contains a mutable member. */
11801 : :
11802 : : bool
11803 : 759244014 : cp_has_mutable_p (const_tree type)
11804 : : {
11805 : : /* This CONST_CAST is okay because strip_array_types returns its
11806 : : argument unmodified and we assign it to a const_tree. */
11807 : 759244014 : type = strip_array_types (CONST_CAST_TREE(type));
11808 : :
11809 : 759244014 : return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11810 : : }
11811 : :
11812 : : /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11813 : : TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11814 : : approximation. In particular, consider:
11815 : :
11816 : : int f();
11817 : : struct S { int i; };
11818 : : const S s = { f(); }
11819 : :
11820 : : Here, we will make "s" as TREE_READONLY (because it is declared
11821 : : "const") -- only to reverse ourselves upon seeing that the
11822 : : initializer is non-constant. */
11823 : :
11824 : : void
11825 : 807596037 : cp_apply_type_quals_to_decl (int type_quals, tree decl)
11826 : : {
11827 : 807596037 : tree type = TREE_TYPE (decl);
11828 : :
11829 : 807596037 : if (type == error_mark_node)
11830 : : return;
11831 : :
11832 : 807595399 : if (TREE_CODE (decl) == TYPE_DECL)
11833 : : return;
11834 : :
11835 : 723647377 : gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11836 : : && type_quals != TYPE_UNQUALIFIED));
11837 : :
11838 : : /* Avoid setting TREE_READONLY incorrectly. */
11839 : : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11840 : : constructor can produce constant init, so rely on cp_finish_decl to
11841 : : clear TREE_READONLY if the variable has non-constant init. */
11842 : :
11843 : : /* If the type has (or might have) a mutable component, that component
11844 : : might be modified. */
11845 : 723647377 : if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11846 : 65019417 : type_quals &= ~TYPE_QUAL_CONST;
11847 : :
11848 : 723647377 : c_apply_type_quals_to_decl (type_quals, decl);
11849 : : }
11850 : :
11851 : : /* Subroutine of casts_away_constness. Make T1 and T2 point at
11852 : : exemplar types such that casting T1 to T2 is casting away constness
11853 : : if and only if there is no implicit conversion from T1 to T2. */
11854 : :
11855 : : static void
11856 : 1072841 : casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11857 : : {
11858 : 1072841 : int quals1;
11859 : 1072841 : int quals2;
11860 : :
11861 : : /* [expr.const.cast]
11862 : :
11863 : : For multi-level pointer to members and multi-level mixed pointers
11864 : : and pointers to members (conv.qual), the "member" aspect of a
11865 : : pointer to member level is ignored when determining if a const
11866 : : cv-qualifier has been cast away. */
11867 : : /* [expr.const.cast]
11868 : :
11869 : : For two pointer types:
11870 : :
11871 : : X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11872 : : X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11873 : : K is min(N,M)
11874 : :
11875 : : casting from X1 to X2 casts away constness if, for a non-pointer
11876 : : type T there does not exist an implicit conversion (clause
11877 : : _conv_) from:
11878 : :
11879 : : Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11880 : :
11881 : : to
11882 : :
11883 : : Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11884 : 1072841 : if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11885 : 536761 : || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11886 : : {
11887 : 536248 : *t1 = cp_build_qualified_type (void_type_node,
11888 : : cp_type_quals (*t1));
11889 : 536248 : *t2 = cp_build_qualified_type (void_type_node,
11890 : : cp_type_quals (*t2));
11891 : 536248 : return;
11892 : : }
11893 : :
11894 : 536593 : quals1 = cp_type_quals (*t1);
11895 : 536593 : quals2 = cp_type_quals (*t2);
11896 : :
11897 : 536593 : if (TYPE_PTRDATAMEM_P (*t1))
11898 : 0 : *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
11899 : : else
11900 : 536593 : *t1 = TREE_TYPE (*t1);
11901 : 536593 : if (TYPE_PTRDATAMEM_P (*t2))
11902 : 0 : *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
11903 : : else
11904 : 536593 : *t2 = TREE_TYPE (*t2);
11905 : :
11906 : 536593 : casts_away_constness_r (t1, t2, complain);
11907 : 536593 : *t1 = build_pointer_type (*t1);
11908 : 536593 : *t2 = build_pointer_type (*t2);
11909 : 536593 : *t1 = cp_build_qualified_type (*t1, quals1);
11910 : 536593 : *t2 = cp_build_qualified_type (*t2, quals2);
11911 : : }
11912 : :
11913 : : /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11914 : : constness.
11915 : :
11916 : : ??? This function returns non-zero if casting away qualifiers not
11917 : : just const. We would like to return to the caller exactly which
11918 : : qualifiers are casted away to give more accurate diagnostics.
11919 : : */
11920 : :
11921 : : static bool
11922 : 536320 : casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
11923 : : {
11924 : 536320 : if (TYPE_REF_P (t2))
11925 : : {
11926 : : /* [expr.const.cast]
11927 : :
11928 : : Casting from an lvalue of type T1 to an lvalue of type T2
11929 : : using a reference cast casts away constness if a cast from an
11930 : : rvalue of type "pointer to T1" to the type "pointer to T2"
11931 : : casts away constness. */
11932 : 0 : t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
11933 : 0 : return casts_away_constness (build_pointer_type (t1),
11934 : 0 : build_pointer_type (TREE_TYPE (t2)),
11935 : 0 : complain);
11936 : : }
11937 : :
11938 : 536320 : if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
11939 : : /* [expr.const.cast]
11940 : :
11941 : : Casting from an rvalue of type "pointer to data member of X
11942 : : of type T1" to the type "pointer to data member of Y of type
11943 : : T2" casts away constness if a cast from an rvalue of type
11944 : : "pointer to T1" to the type "pointer to T2" casts away
11945 : : constness. */
11946 : 48 : return casts_away_constness
11947 : 48 : (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
11948 : 48 : build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
11949 : 48 : complain);
11950 : :
11951 : : /* Casting away constness is only something that makes sense for
11952 : : pointer or reference types. */
11953 : 536272 : if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
11954 : : return false;
11955 : :
11956 : : /* Top-level qualifiers don't matter. */
11957 : 536248 : t1 = TYPE_MAIN_VARIANT (t1);
11958 : 536248 : t2 = TYPE_MAIN_VARIANT (t2);
11959 : 536248 : casts_away_constness_r (&t1, &t2, complain);
11960 : 536248 : if (!can_convert (t2, t1, complain))
11961 : : return true;
11962 : :
11963 : : return false;
11964 : : }
11965 : :
11966 : : /* If T is a REFERENCE_TYPE return the type to which T refers.
11967 : : Otherwise, return T itself. */
11968 : :
11969 : : tree
11970 : 2691136008 : non_reference (tree t)
11971 : : {
11972 : 2691136008 : if (t && TYPE_REF_P (t))
11973 : 258324163 : t = TREE_TYPE (t);
11974 : 2691136008 : return t;
11975 : : }
11976 : :
11977 : :
11978 : : /* Return nonzero if REF is an lvalue valid for this language;
11979 : : otherwise, print an error message and return zero. USE says
11980 : : how the lvalue is being used and so selects the error message. */
11981 : :
11982 : : int
11983 : 35353169 : lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
11984 : : {
11985 : 35353169 : cp_lvalue_kind kind = lvalue_kind (ref);
11986 : :
11987 : 35353169 : if (kind == clk_none)
11988 : : {
11989 : 128 : if (complain & tf_error)
11990 : 115 : lvalue_error (cp_expr_loc_or_input_loc (ref), use);
11991 : 128 : return 0;
11992 : : }
11993 : 35353041 : else if (kind & (clk_rvalueref|clk_class))
11994 : : {
11995 : 104 : if (!(complain & tf_error))
11996 : : return 0;
11997 : : /* Make this a permerror because we used to accept it. */
11998 : 18 : permerror (cp_expr_loc_or_input_loc (ref),
11999 : : "using rvalue as lvalue");
12000 : : }
12001 : : return 1;
12002 : : }
12003 : :
12004 : : /* Return true if a user-defined literal operator is a raw operator. */
12005 : :
12006 : : bool
12007 : 116182 : check_raw_literal_operator (const_tree decl)
12008 : : {
12009 : 116182 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12010 : 116182 : tree argtype;
12011 : 116182 : int arity;
12012 : 116182 : bool maybe_raw_p = false;
12013 : :
12014 : : /* Count the number and type of arguments and check for ellipsis. */
12015 : 116182 : for (argtype = argtypes, arity = 0;
12016 : 174290 : argtype && argtype != void_list_node;
12017 : 58108 : ++arity, argtype = TREE_CHAIN (argtype))
12018 : : {
12019 : 58108 : tree t = TREE_VALUE (argtype);
12020 : :
12021 : 58108 : if (same_type_p (t, const_string_type_node))
12022 : 9 : maybe_raw_p = true;
12023 : : }
12024 : 116182 : if (!argtype)
12025 : : return false; /* Found ellipsis. */
12026 : :
12027 : 116182 : if (!maybe_raw_p || arity != 1)
12028 : 116176 : return false;
12029 : :
12030 : : return true;
12031 : : }
12032 : :
12033 : :
12034 : : /* Return true if a user-defined literal operator has one of the allowed
12035 : : argument types. */
12036 : :
12037 : : bool
12038 : 272567 : check_literal_operator_args (const_tree decl,
12039 : : bool *long_long_unsigned_p, bool *long_double_p)
12040 : : {
12041 : 272567 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12042 : :
12043 : 272567 : *long_long_unsigned_p = false;
12044 : 272567 : *long_double_p = false;
12045 : 272567 : if (processing_template_decl || processing_specialization)
12046 : 58209 : return argtypes == void_list_node;
12047 : : else
12048 : : {
12049 : : tree argtype;
12050 : : int arity;
12051 : : int max_arity = 2;
12052 : :
12053 : : /* Count the number and type of arguments and check for ellipsis. */
12054 : 214264 : for (argtype = argtypes, arity = 0;
12055 : 428622 : argtype && argtype != void_list_node;
12056 : 214264 : argtype = TREE_CHAIN (argtype))
12057 : : {
12058 : 214364 : tree t = TREE_VALUE (argtype);
12059 : 214364 : ++arity;
12060 : :
12061 : 214364 : if (TYPE_PTR_P (t))
12062 : : {
12063 : 94816 : bool maybe_raw_p = false;
12064 : 94816 : t = TREE_TYPE (t);
12065 : 94816 : if (cp_type_quals (t) != TYPE_QUAL_CONST)
12066 : : return false;
12067 : 94813 : t = TYPE_MAIN_VARIANT (t);
12068 : 94813 : if ((maybe_raw_p = same_type_p (t, char_type_node))
12069 : 72273 : || same_type_p (t, wchar_type_node)
12070 : 49883 : || same_type_p (t, char8_type_node)
12071 : 44792 : || same_type_p (t, char16_type_node)
12072 : 117209 : || same_type_p (t, char32_type_node))
12073 : : {
12074 : 94810 : argtype = TREE_CHAIN (argtype);
12075 : 94810 : if (!argtype)
12076 : : return false;
12077 : 94810 : t = TREE_VALUE (argtype);
12078 : 94810 : if (maybe_raw_p && argtype == void_list_node)
12079 : : return true;
12080 : 94737 : else if (same_type_p (t, size_type_node))
12081 : : {
12082 : 94731 : ++arity;
12083 : 94731 : continue;
12084 : : }
12085 : : else
12086 : : return false;
12087 : : }
12088 : : }
12089 : 119548 : else if (same_type_p (t, long_long_unsigned_type_node))
12090 : : {
12091 : 32790 : max_arity = 1;
12092 : 32790 : *long_long_unsigned_p = true;
12093 : : }
12094 : 86758 : else if (same_type_p (t, long_double_type_node))
12095 : : {
12096 : 86658 : max_arity = 1;
12097 : 86658 : *long_double_p = true;
12098 : : }
12099 : 100 : else if (same_type_p (t, char_type_node))
12100 : : max_arity = 1;
12101 : 56 : else if (same_type_p (t, wchar_type_node))
12102 : : max_arity = 1;
12103 : 45 : else if (same_type_p (t, char8_type_node))
12104 : : max_arity = 1;
12105 : 40 : else if (same_type_p (t, char16_type_node))
12106 : : max_arity = 1;
12107 : 29 : else if (same_type_p (t, char32_type_node))
12108 : : max_arity = 1;
12109 : : else
12110 : : return false;
12111 : : }
12112 : 214258 : if (!argtype)
12113 : : return false; /* Found ellipsis. */
12114 : :
12115 : 214255 : if (arity != max_arity)
12116 : : return false;
12117 : :
12118 : : return true;
12119 : : }
12120 : : }
12121 : :
12122 : : /* Always returns false since unlike C90, C++ has no concept of implicit
12123 : : function declarations. */
12124 : :
12125 : : bool
12126 : 361 : c_decl_implicit (const_tree)
12127 : : {
12128 : 361 : return false;
12129 : : }
|