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 : 111930784 : require_complete_type (tree value,
75 : : tsubst_flags_t complain /* = tf_warning_or_error */)
76 : : {
77 : 111930784 : tree type;
78 : :
79 : 111930784 : if (processing_template_decl || value == error_mark_node)
80 : : return value;
81 : :
82 : 104030413 : if (TREE_CODE (value) == OVERLOAD)
83 : 0 : type = unknown_type_node;
84 : : else
85 : 104030413 : type = TREE_TYPE (value);
86 : :
87 : 104030413 : if (type == error_mark_node)
88 : : return error_mark_node;
89 : :
90 : : /* First, detect a valid value with a complete type. */
91 : 104030401 : if (COMPLETE_TYPE_P (type))
92 : : return value;
93 : :
94 : 109447 : 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 : 11921633421 : complete_type (tree type)
107 : : {
108 : 11921633421 : 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 : 11921633421 : if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 : : ;
115 : 3968349192 : else if (TREE_CODE (type) == ARRAY_TYPE)
116 : : {
117 : 602111 : tree t = complete_type (TREE_TYPE (type));
118 : 602111 : unsigned int needs_constructing, has_nontrivial_dtor;
119 : 602111 : if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 : 601975 : layout_type (type);
121 : 602111 : needs_constructing
122 : 602111 : = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 : 602111 : has_nontrivial_dtor
124 : 602111 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 : 1985673 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 : : {
127 : 1383562 : TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 : 1383562 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 : : }
130 : : }
131 : 3967747081 : else if (CLASS_TYPE_P (type))
132 : : {
133 : 3889445871 : if (modules_p ())
134 : : /* TYPE could be a class member we've not loaded the definition of. */
135 : 12386503 : lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
136 : :
137 : 3889445871 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 : 741912988 : 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 : 1261582796 : complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 : : {
151 : 1261582796 : type = complete_type (type);
152 : 1261582793 : if (type == error_mark_node)
153 : : /* We already issued an error. */
154 : : return NULL_TREE;
155 : 1261582640 : else if (!COMPLETE_TYPE_P (type))
156 : : {
157 : 3790 : if (complain & tf_error)
158 : 472 : cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
159 : 3790 : note_failed_type_completion (type, complain);
160 : 3790 : return NULL_TREE;
161 : : }
162 : : else
163 : : return type;
164 : : }
165 : :
166 : : tree
167 : 137715807 : complete_type_or_else (tree type, tree value)
168 : : {
169 : 137715807 : 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 : 4005032 : commonparms (tree p1, tree p2)
182 : : {
183 : 4005032 : tree oldargs = p1, newargs, n;
184 : 4005032 : int i, len;
185 : 4005032 : int any_change = 0;
186 : :
187 : 4005032 : len = list_length (p1);
188 : 4005032 : newargs = tree_last (p1);
189 : :
190 : 4005032 : if (newargs == void_list_node)
191 : : i = 1;
192 : : else
193 : : {
194 : 38055 : i = 0;
195 : 38055 : newargs = 0;
196 : : }
197 : :
198 : 13476196 : for (; i < len; i++)
199 : 9471164 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200 : :
201 : : n = newargs;
202 : :
203 : 17443173 : for (i = 0; p1;
204 : 13438141 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 : : {
206 : 13438327 : if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 : : {
208 : 159 : TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 : 159 : any_change = 1;
210 : : }
211 : 13437982 : else if (! TREE_PURPOSE (p1))
212 : : {
213 : 13437955 : if (TREE_PURPOSE (p2))
214 : : {
215 : 411261 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 : 411261 : 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 : 13438141 : if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 : : {
227 : 3744959 : any_change = 1;
228 : 3744959 : TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 : : }
230 : : else
231 : 9693182 : TREE_VALUE (n) = TREE_VALUE (p1);
232 : : }
233 : 4005032 : if (! any_change)
234 : 1356477 : 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 : 32043070 : original_type (tree t)
243 : : {
244 : 32043070 : int quals = cp_type_quals (t);
245 : 32043070 : while (t != error_mark_node
246 : 33136959 : && TYPE_NAME (t) != NULL_TREE)
247 : : {
248 : 11197749 : tree x = TYPE_NAME (t);
249 : 11197749 : if (TREE_CODE (x) != TYPE_DECL)
250 : : break;
251 : 11197749 : x = DECL_ORIGINAL_TYPE (x);
252 : 11197749 : if (x == NULL_TREE)
253 : : break;
254 : : t = x;
255 : : }
256 : 32043070 : 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 : 49978 : merge_type_attributes_from (tree type, tree other_type)
264 : : {
265 : 49978 : tree attrs = targetm.merge_type_attributes (type, other_type);
266 : 49978 : attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
267 : 49978 : 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 : 10002173 : cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
280 : : {
281 : 10002173 : tree mv1 = TYPE_MAIN_VARIANT (t1);
282 : 10002173 : tree mv2 = TYPE_MAIN_VARIANT (t2);
283 : 10002173 : int extended1 = 0;
284 : 10002173 : int extended2 = 0;
285 : :
286 : 10002173 : if (mv1 == mv2)
287 : : return 0;
288 : :
289 : 80006064 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
290 : : {
291 : 70005306 : if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 : 4506022 : extended1 = i + 1;
293 : 70005306 : if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 : 3779930 : extended2 = i + 1;
295 : : }
296 : 10000758 : if (mv1 == bfloat16_type_node)
297 : 1044584 : extended1 = true;
298 : 10000758 : if (mv2 == bfloat16_type_node)
299 : 853887 : extended2 = true;
300 : 10000758 : if (extended2 && !extended1)
301 : : {
302 : 4408237 : int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 : 4408237 : return ret == 3 ? 3 : -ret;
304 : : }
305 : :
306 : 5592521 : const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 : 5592521 : const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 : 5592521 : 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 : 44740168 : int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
315 : 11185042 : ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
316 : 44740168 : int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
317 : 11185042 : ? 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 : 5592521 : if ((p1 > p2 && fmt1->emax >= fmt2->emax)
322 : 4241939 : || (p1 == p2 && fmt1->emax > fmt2->emax))
323 : : return 2;
324 : 4241939 : if ((p1 < p2 && fmt1->emax <= fmt2->emax)
325 : 1337979 : || (p1 == p2 && fmt1->emax < fmt2->emax))
326 : : return -2;
327 : 1337979 : if ((p1 > p2 && fmt1->emax < fmt2->emax)
328 : 1329975 : || (p1 < p2 && fmt1->emax > fmt2->emax))
329 : : return 3;
330 : 1321971 : 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 : 1321971 : 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 : 5287868 : for (p = &float_type_node; p <= &long_double_type_node; ++p)
375 : : {
376 : 3965901 : const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
377 : 3965901 : gcc_assert (fmt3->b == 2);
378 : 31727208 : int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
379 : 7931802 : ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
380 : 3965901 : if (p1 == p3 && fmt1->emax == fmt3->emax)
381 : 1258396 : ++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 : 1321967 : 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 : 93224383 : cp_common_type (tree t1, tree t2)
413 : : {
414 : 93224383 : enum tree_code code1 = TREE_CODE (t1);
415 : 93224383 : enum tree_code code2 = TREE_CODE (t2);
416 : 93224383 : tree attributes;
417 : 93224383 : 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 : 93224383 : attributes = (*targetm.merge_type_attributes) (t1, t2);
424 : :
425 : 93224383 : if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
426 : : {
427 : 1383209 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
428 : 1383209 : return build_type_attribute_variant (t1, attributes);
429 : : else
430 : : return NULL_TREE;
431 : : }
432 : :
433 : : /* FIXME: Attributes. */
434 : 91841174 : gcc_assert (ARITHMETIC_TYPE_P (t1)
435 : : || VECTOR_TYPE_P (t1)
436 : : || UNSCOPED_ENUM_P (t1));
437 : 91841174 : 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 : 91841174 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
445 : : {
446 : 226125 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
447 : 226125 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
448 : 226125 : tree subtype
449 : 226125 : = type_after_usual_arithmetic_conversions (subtype1, subtype2);
450 : :
451 : 226125 : if (subtype == error_mark_node)
452 : : return subtype;
453 : 226125 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
454 : 192781 : 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 : 91615049 : 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 : 49978 : if (TYPE_UNSIGNED (t1))
467 : 10972 : return merge_type_attributes_from (t1, t2);
468 : : else
469 : 39006 : return merge_type_attributes_from (t2, t1);
470 : : }
471 : :
472 : : /* If only one is real, use it as the result. */
473 : 91565071 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
474 : 1090929 : return build_type_attribute_variant (t1, attributes);
475 : 90474142 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
476 : 1021355 : return build_type_attribute_variant (t2, attributes);
477 : :
478 : 89452787 : if (code1 == REAL_TYPE
479 : 89452787 : && (extended_float_type_p (t1) || extended_float_type_p (t2)))
480 : : {
481 : 187323 : tree mv1 = TYPE_MAIN_VARIANT (t1);
482 : 187323 : tree mv2 = TYPE_MAIN_VARIANT (t2);
483 : 187323 : if (mv1 == mv2)
484 : 183777 : return build_type_attribute_variant (t1, attributes);
485 : :
486 : 3546 : int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
487 : 3546 : if (cmpret == 3)
488 : 0 : return error_mark_node;
489 : 3546 : else if (cmpret >= 0)
490 : 3284 : 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 : 89265464 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
497 : 12820666 : return build_type_attribute_variant (t1, attributes);
498 : 76444798 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
499 : 1492957 : return build_type_attribute_variant (t2, attributes);
500 : :
501 : : /* The types are the same; no need to do anything fancy. */
502 : 74951841 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
503 : 69020767 : return build_type_attribute_variant (t1, attributes);
504 : :
505 : 5931074 : if (code1 != REAL_TYPE)
506 : : {
507 : : /* If one is unsigned long long, then convert the other to unsigned
508 : : long long. */
509 : 5931071 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
510 : 5931071 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
511 : 122985 : return build_type_attribute_variant (long_long_unsigned_type_node,
512 : 122985 : 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 : 5808086 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
524 : 5808086 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
525 : : {
526 : 597 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
527 : 304 : ? long_long_unsigned_type_node
528 : 304 : : long_long_integer_type_node);
529 : 304 : return build_type_attribute_variant (t, attributes);
530 : : }
531 : :
532 : : /* Go through the same procedure, but for longs. */
533 : 5807782 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
534 : 5807782 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
535 : 706767 : return build_type_attribute_variant (long_unsigned_type_node,
536 : 706767 : attributes);
537 : 5101015 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
538 : 5101015 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
539 : : {
540 : 71286 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
541 : 71286 : ? long_unsigned_type_node : long_integer_type_node);
542 : 35980 : 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 : 10130028 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
550 : : {
551 : 5065035 : if (int_n_enabled_p [i]
552 : 5065035 : && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
553 : 4986973 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
554 : 4986961 : || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
555 : 4986961 : || 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 : 42 : ? 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 : 5064993 : if (TYPE_UNSIGNED (t1))
566 : 4049728 : return build_type_attribute_variant (t1, attributes);
567 : : else
568 : 1015265 : 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 : 925344 : type_after_usual_arithmetic_conversions (tree t1, tree t2)
600 : : {
601 : 925344 : gcc_assert (ARITHMETIC_TYPE_P (t1)
602 : : || VECTOR_TYPE_P (t1)
603 : : || UNSCOPED_ENUM_P (t1));
604 : 925344 : 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 : 925344 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
610 : 698679 : && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
611 : : {
612 : 698353 : t1 = type_promotes_to (t1);
613 : 698353 : t2 = type_promotes_to (t2);
614 : : }
615 : :
616 : 925344 : 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 : 3634116 : 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 : 3634116 : tree pointee1;
660 : 3634116 : tree pointee2;
661 : 3634116 : tree result_type;
662 : 3634116 : tree attributes;
663 : :
664 : : /* Determine the types pointed to by T1 and T2. */
665 : 3634116 : if (TYPE_PTR_P (t1))
666 : : {
667 : 3633647 : pointee1 = TREE_TYPE (t1);
668 : 3633647 : 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 : 3634116 : 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 : 3634025 : const int q1 = cp_type_quals (pointee1);
700 : 3634025 : const int q2 = cp_type_quals (pointee2);
701 : 3634025 : const int quals = q1 | q2;
702 : 3634025 : result_type = cp_build_qualified_type (result_type,
703 : 3634025 : (quals | (*add_const
704 : 3634025 : ? 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 : 3634025 : if (quals != q1 || quals != q2)
710 : 542019 : *add_const = true;
711 : : /* If the original types were pointers to members, so is the
712 : : result. */
713 : 3634025 : 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 : 3633566 : result_type = build_pointer_type (result_type);
729 : :
730 : : /* Merge the attributes. */
731 : 3634025 : attributes = (*targetm.merge_type_attributes) (t1, t2);
732 : 3634025 : 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 : 3834242 : 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 : 3834242 : tree class1;
750 : 3834242 : 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 : 3834242 : if (null_ptr_cst_p (arg1))
757 : : return t2;
758 : 3834063 : 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 : 3810087 : 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 : 3810087 : if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
776 : : {
777 : 175839 : tree attributes;
778 : 175839 : tree result_type;
779 : :
780 : 175839 : 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 : 175838 : result_type
810 : 175838 : = cp_build_qualified_type (void_type_node,
811 : 175838 : (cp_type_quals (TREE_TYPE (t1))
812 : 175838 : | cp_type_quals (TREE_TYPE (t2))));
813 : 175838 : result_type = build_pointer_type (result_type);
814 : : /* Merge the attributes. */
815 : 175838 : attributes = (*targetm.merge_type_attributes) (t1, t2);
816 : 175838 : return build_type_attribute_variant (result_type, attributes);
817 : : }
818 : :
819 : 3634248 : 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 : 3634248 : if (fnptr_conv_p (t1, t2))
830 : : return t1;
831 : 3634180 : 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 : 3633632 : if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
837 : 3633632 : && CLASS_TYPE_P (TREE_TYPE (t1))
838 : 571716 : && CLASS_TYPE_P (TREE_TYPE (t2))
839 : 4205829 : && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
840 : 571713 : TREE_TYPE (t2)))
841 : : {
842 : 30760 : class1 = TREE_TYPE (t1);
843 : 30760 : class2 = TREE_TYPE (t2);
844 : :
845 : 30760 : if (DERIVED_FROM_P (class1, class2))
846 : 3098 : t2 = (build_pointer_type
847 : 1549 : (cp_build_qualified_type (class1, cp_type_quals (class2))));
848 : 29211 : else if (DERIVED_FROM_P (class2, class1))
849 : 58386 : t1 = (build_pointer_type
850 : 29193 : (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 : 3602970 : else if (TYPE_PTRMEM_P (t1)
861 : 3603454 : && !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 : 3634083 : bool add_const = false;
899 : 3634083 : return composite_pointer_type_r (location, t1, t2, &add_const, operation,
900 : 3634083 : 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 : 28424477 : merge_types (tree t1, tree t2)
912 : : {
913 : 28424477 : enum tree_code code1;
914 : 28424477 : enum tree_code code2;
915 : 28424477 : tree attributes;
916 : :
917 : : /* Save time if the two types are the same. */
918 : 28424477 : if (t1 == t2)
919 : : return t1;
920 : 16021535 : if (original_type (t1) == original_type (t2))
921 : : return t1;
922 : :
923 : : /* If one type is nonsense, use the other. */
924 : 15960877 : if (t1 == error_mark_node)
925 : : return t2;
926 : 15960877 : if (t2 == error_mark_node)
927 : : return t1;
928 : :
929 : : /* Handle merging an auto redeclaration with a previous deduced
930 : : return type. */
931 : 15960877 : if (is_auto (t1))
932 : : return t2;
933 : :
934 : : /* Merge the attributes. */
935 : 15951260 : attributes = (*targetm.merge_type_attributes) (t1, t2);
936 : :
937 : 15951260 : if (TYPE_PTRMEMFUNC_P (t1))
938 : 15 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
939 : 15951260 : if (TYPE_PTRMEMFUNC_P (t2))
940 : 15 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
941 : :
942 : 15951260 : code1 = TREE_CODE (t1);
943 : 15951260 : code2 = TREE_CODE (t2);
944 : 15951260 : 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 : 15951260 : switch (code1)
960 : : {
961 : 2706174 : case POINTER_TYPE:
962 : 2706174 : case REFERENCE_TYPE:
963 : : /* For two pointers, do this recursively on the target type. */
964 : 2706174 : {
965 : 2706174 : tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
966 : 2706174 : int quals = cp_type_quals (t1);
967 : :
968 : 2706174 : if (code1 == POINTER_TYPE)
969 : : {
970 : 718427 : t1 = build_pointer_type (target);
971 : 718427 : if (TREE_CODE (target) == METHOD_TYPE)
972 : 15 : t1 = build_ptrmemfunc_type (t1);
973 : : }
974 : : else
975 : 1987747 : t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
976 : 2706174 : t1 = build_type_attribute_variant (t1, attributes);
977 : 2706174 : t1 = cp_build_qualified_type (t1, quals);
978 : :
979 : 2706174 : 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 : 10615 : case ARRAY_TYPE:
996 : 10615 : {
997 : 10615 : 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 : 21230 : if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
1000 : 10593 : 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 : 4148624 : 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 : 4148624 : {
1013 : 4148624 : tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1014 : 4148624 : tree p1 = TYPE_ARG_TYPES (t1);
1015 : 4148624 : tree p2 = TYPE_ARG_TYPES (t2);
1016 : 4148624 : tree parms;
1017 : :
1018 : : /* Save space: see if the result is identical to one of the args. */
1019 : 4148624 : if (valtype == TREE_TYPE (t1) && ! p2)
1020 : 0 : return cp_build_type_attribute_variant (t1, attributes);
1021 : 4148624 : 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 : 8297248 : if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1026 : : parms = p2;
1027 : 8010064 : else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1028 : : parms = p1;
1029 : : else
1030 : 4005032 : parms = commonparms (p1, p2);
1031 : :
1032 : 4148624 : cp_cv_quals quals = type_memfn_quals (t1);
1033 : 4148624 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1034 : 4148624 : gcc_assert (quals == type_memfn_quals (t2));
1035 : 4148624 : gcc_assert (rqual == type_memfn_rqual (t2));
1036 : :
1037 : 4148624 : tree rval = build_function_type (valtype, parms);
1038 : 4148624 : rval = apply_memfn_quals (rval, quals);
1039 : 4148624 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1040 : 4148624 : TYPE_RAISES_EXCEPTIONS (t2));
1041 : 4148624 : bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1042 : 4148624 : t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1043 : 4148624 : break;
1044 : : }
1045 : :
1046 : 3917648 : case METHOD_TYPE:
1047 : 3917648 : {
1048 : : /* Get this value the long way, since TYPE_METHOD_BASETYPE
1049 : : is just the main variant of this. */
1050 : 3917648 : tree basetype = class_of_this_parm (t2);
1051 : 3917648 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1052 : 3917648 : TYPE_RAISES_EXCEPTIONS (t2));
1053 : 3917648 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1054 : 3917648 : tree t3;
1055 : 3917648 : 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 : 3917648 : t1 = build_function_type (TREE_TYPE (t1),
1061 : 3917648 : TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1062 : 3917648 : t2 = build_function_type (TREE_TYPE (t2),
1063 : 3917648 : TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1064 : 3917648 : t3 = merge_types (t1, t2);
1065 : 3917648 : t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1066 : 3917648 : TYPE_ARG_TYPES (t3));
1067 : 3917648 : t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1068 : 3917648 : 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 : 5158614 : default:;
1078 : 5158614 : 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 : 8066290 : 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 : 412412 : common_type (tree t1, tree t2)
1111 : : {
1112 : : /* If one type is nonsense, use the other */
1113 : 412412 : if (t1 == error_mark_node)
1114 : : return t2;
1115 : 412412 : if (t2 == error_mark_node)
1116 : : return t1;
1117 : :
1118 : 412412 : 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 : 1234108 : common_pointer_type (tree t1, tree t2)
1130 : : {
1131 : 1234108 : 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 : 1234108 : return composite_pointer_type (input_location, t1, t2,
1136 : : error_mark_node, error_mark_node,
1137 : 1234108 : 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 : 1660 : comp_except_types (tree a, tree b, bool exact)
1158 : : {
1159 : 1660 : 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 : 1375328738 : comp_except_specs (const_tree t1, const_tree t2, int exact)
1194 : : {
1195 : 1375328738 : const_tree probe;
1196 : 1375328738 : const_tree base;
1197 : 1375328738 : int length = 0;
1198 : :
1199 : 1375328738 : if (t1 == t2)
1200 : : return true;
1201 : :
1202 : : /* First handle noexcept. */
1203 : 538890370 : if (exact < ce_exact)
1204 : : {
1205 : 7475345 : if (exact == ce_type
1206 : 14880422 : && (canonical_eh_spec (CONST_CAST_TREE (t1))
1207 : 7405077 : == 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 : 7457097 : if (t1 == noexcept_false_spec)
1213 : 153 : return t2 == NULL_TREE || exact == ce_derived;
1214 : : /* Even a derived noexcept(false) is compatible with no
1215 : : exception-specification. */
1216 : 7456944 : if (t2 == noexcept_false_spec)
1217 : 1588 : return t1 == NULL_TREE;
1218 : :
1219 : : /* Otherwise, if we aren't looking for an exact match, noexcept is
1220 : : equivalent to throw(). */
1221 : 7455356 : if (t1 == noexcept_true_spec)
1222 : 677292 : t1 = empty_except_spec;
1223 : 7455356 : if (t2 == noexcept_true_spec)
1224 : 6498082 : 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 : 516623359 : if ((t1 && TREE_PURPOSE (t1))
1231 : 563813656 : || (t2 && TREE_PURPOSE (t2)))
1232 : 529521218 : return (t1 && t2
1233 : 529521218 : && (exact == ce_exact
1234 : 61043146 : ? TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
1235 : 277083 : : cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))));
1236 : :
1237 : 9349163 : if (t1 == NULL_TREE) /* T1 is ... */
1238 : 6713726 : return t2 == NULL_TREE || exact == ce_derived;
1239 : 2635437 : if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1240 : 2602651 : return t2 != NULL_TREE && !TREE_VALUE (t2);
1241 : 34100 : if (t2 == NULL_TREE) /* T2 is ... */
1242 : : return false;
1243 : 1538 : 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 : 2617 : for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1251 : : {
1252 : 2058 : for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1253 : : {
1254 : 1660 : tree a = TREE_VALUE (probe);
1255 : 1660 : tree b = TREE_VALUE (t2);
1256 : :
1257 : 1660 : if (comp_except_types (a, b, exact))
1258 : : {
1259 : 1155 : if (probe == base && exact > ce_derived)
1260 : 1120 : base = TREE_CHAIN (probe);
1261 : 1155 : length++;
1262 : 1155 : break;
1263 : : }
1264 : : }
1265 : 1155 : if (probe == NULL_TREE)
1266 : : return false;
1267 : : }
1268 : 1064 : 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 : 1383566 : comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1279 : : bool strict)
1280 : : {
1281 : 1383566 : tree d1;
1282 : 1383566 : tree d2;
1283 : 1383566 : tree max1, max2;
1284 : :
1285 : 1383566 : if (t1 == t2)
1286 : : return true;
1287 : :
1288 : : /* The type of the array elements must be the same. */
1289 : 1383474 : if (strict
1290 : 1383474 : ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1291 : 2554 : : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1292 : : return false;
1293 : :
1294 : 940375 : d1 = TYPE_DOMAIN (t1);
1295 : 940375 : d2 = TYPE_DOMAIN (t2);
1296 : :
1297 : 940375 : 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 : 634094 : if (!d1 && d2)
1313 : 2377 : return cb >= bounds_either;
1314 : 631717 : else if (d1 && !d2)
1315 : 34473 : return cb == bounds_either;
1316 : :
1317 : : /* Check that the dimensions are the same. */
1318 : :
1319 : 597244 : if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1320 : : return false;
1321 : 597223 : max1 = TYPE_MAX_VALUE (d1);
1322 : 597223 : max2 = TYPE_MAX_VALUE (d2);
1323 : :
1324 : 597223 : 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 : 1379216196 : comp_template_parms_position (tree t1, tree t2)
1337 : : {
1338 : 1379216196 : tree index1, index2;
1339 : 1379216196 : 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 : 1379216196 : index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1346 : 1379216196 : index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1347 : :
1348 : : /* Then compare their relative position. */
1349 : 1379216196 : if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1350 : 879838939 : || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1351 : 2029052380 : || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1352 : 649836184 : != 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 : 575213797 : if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1358 : 110114785 : 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 : : /* Only the precision of the parameter matters. This check should
1376 : : make sure that the callee does not see undefined values in argument
1377 : : registers. */
1378 : 376 : if (INTEGRAL_TYPE_P (t1)
1379 : 67 : && INTEGRAL_TYPE_P (t2)
1380 : 422 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1381 : : return true;
1382 : :
1383 : 333 : return same_type_p (t1, t2);
1384 : : }
1385 : :
1386 : : /* Check if a type cast between two function types can be considered safe. */
1387 : :
1388 : : static bool
1389 : 6509 : cxx_safe_function_type_cast_p (tree t1, tree t2)
1390 : : {
1391 : 6509 : if (TREE_TYPE (t1) == void_type_node &&
1392 : 6437 : TYPE_ARG_TYPES (t1) == void_list_node)
1393 : : return true;
1394 : :
1395 : 5206 : if (TREE_TYPE (t2) == void_type_node &&
1396 : 5001 : TYPE_ARG_TYPES (t2) == void_list_node)
1397 : : return true;
1398 : :
1399 : 272 : if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1400 : : return false;
1401 : :
1402 : 111 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1403 : 314 : t1 && t2;
1404 : 203 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1405 : 239 : if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1406 : : return false;
1407 : :
1408 : : return true;
1409 : : }
1410 : :
1411 : : /* Subroutine in comptypes. */
1412 : :
1413 : : static bool
1414 : 12194787484 : structural_comptypes (tree t1, tree t2, int strict)
1415 : : {
1416 : : /* Both should be types that are not obviously the same. */
1417 : 12194787484 : gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1418 : :
1419 : : /* Suppress typename resolution under spec_hasher::equal in place of calling
1420 : : push_to_top_level there. */
1421 : 12194787484 : if (!comparing_specializations)
1422 : : {
1423 : : /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1424 : : current instantiation. */
1425 : 10128493441 : if (TREE_CODE (t1) == TYPENAME_TYPE)
1426 : 89450486 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1427 : :
1428 : 10128493441 : if (TREE_CODE (t2) == TYPENAME_TYPE)
1429 : 113529442 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1430 : : }
1431 : :
1432 : 12194787484 : if (TYPE_PTRMEMFUNC_P (t1))
1433 : 30450534 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1434 : 12194787484 : if (TYPE_PTRMEMFUNC_P (t2))
1435 : 33772045 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1436 : :
1437 : : /* Different classes of types can't be compatible. */
1438 : 12194787484 : if (TREE_CODE (t1) != TREE_CODE (t2))
1439 : : return false;
1440 : :
1441 : : /* Qualifiers must match. For array types, we will check when we
1442 : : recur on the array element types. */
1443 : 7731259043 : if (TREE_CODE (t1) != ARRAY_TYPE
1444 : 7731259043 : && cp_type_quals (t1) != cp_type_quals (t2))
1445 : : return false;
1446 : 7261939102 : if (TREE_CODE (t1) == FUNCTION_TYPE
1447 : 7261939102 : && type_memfn_quals (t1) != type_memfn_quals (t2))
1448 : : return false;
1449 : : /* Need to check this before TYPE_MAIN_VARIANT.
1450 : : FIXME function qualifiers should really change the main variant. */
1451 : 7261837087 : if (FUNC_OR_METHOD_TYPE_P (t1))
1452 : : {
1453 : 36971844 : if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1454 : : return false;
1455 : 18779829 : if (flag_noexcept_type
1456 : 37460692 : && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1457 : 18680863 : TYPE_RAISES_EXCEPTIONS (t2),
1458 : : ce_type))
1459 : : return false;
1460 : : }
1461 : :
1462 : : /* Allow for two different type nodes which have essentially the same
1463 : : definition. Note that we already checked for equality of the type
1464 : : qualifiers (just above). */
1465 : 7236433496 : if (TREE_CODE (t1) != ARRAY_TYPE
1466 : 7236433496 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1467 : 316554556 : goto check_alias;
1468 : :
1469 : : /* Compare the types. Return false on known not-same. Break on not
1470 : : known. Never return true from this switch -- you'll break
1471 : : specialization comparison. */
1472 : 6919878940 : switch (TREE_CODE (t1))
1473 : : {
1474 : : case VOID_TYPE:
1475 : : case BOOLEAN_TYPE:
1476 : : /* All void and bool types are the same. */
1477 : : break;
1478 : :
1479 : 493299741 : case OPAQUE_TYPE:
1480 : 493299741 : case INTEGER_TYPE:
1481 : 493299741 : case FIXED_POINT_TYPE:
1482 : 493299741 : case REAL_TYPE:
1483 : : /* With these nodes, we can't determine type equivalence by
1484 : : looking at what is stored in the nodes themselves, because
1485 : : two nodes might have different TYPE_MAIN_VARIANTs but still
1486 : : represent the same type. For example, wchar_t and int could
1487 : : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1488 : : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1489 : : and are distinct types. On the other hand, int and the
1490 : : following typedef
1491 : :
1492 : : typedef int INT __attribute((may_alias));
1493 : :
1494 : : have identical properties, different TYPE_MAIN_VARIANTs, but
1495 : : represent the same type. The canonical type system keeps
1496 : : track of equivalence in this case, so we fall back on it. */
1497 : 493299741 : if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1498 : : return false;
1499 : :
1500 : : /* We don't need or want the attribute comparison. */
1501 : 8375 : goto check_alias;
1502 : :
1503 : 994206 : case TEMPLATE_TEMPLATE_PARM:
1504 : 994206 : case BOUND_TEMPLATE_TEMPLATE_PARM:
1505 : 994206 : if (!comp_template_parms_position (t1, t2))
1506 : : return false;
1507 : 832699 : if (!comp_template_parms
1508 : 1665398 : (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1509 : 2314509 : DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1510 : : return false;
1511 : 617690 : if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1512 : : break;
1513 : : /* Don't check inheritance. */
1514 : : strict = COMPARE_STRICT;
1515 : : /* Fall through. */
1516 : :
1517 : 3528571041 : case RECORD_TYPE:
1518 : 3528571041 : case UNION_TYPE:
1519 : 6207383785 : if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1520 : 2173751278 : && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1521 : 1679975065 : || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1522 : 4022402463 : && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1523 : : break;
1524 : :
1525 : 3487976460 : if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1526 : : break;
1527 : 3487976427 : else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1528 : : break;
1529 : :
1530 : : return false;
1531 : :
1532 : 44136 : case OFFSET_TYPE:
1533 : 44136 : if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1534 : : strict & ~COMPARE_REDECLARATION))
1535 : : return false;
1536 : 41764 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1537 : : return false;
1538 : : break;
1539 : :
1540 : 801235703 : case REFERENCE_TYPE:
1541 : 801235703 : if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1542 : : return false;
1543 : : /* fall through to checks for pointer types */
1544 : 1108698417 : gcc_fallthrough ();
1545 : :
1546 : 1108698417 : case POINTER_TYPE:
1547 : 1108698417 : if (TYPE_MODE (t1) != TYPE_MODE (t2)
1548 : 1108698417 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1549 : 1001594554 : return false;
1550 : : break;
1551 : :
1552 : 11255963 : case METHOD_TYPE:
1553 : 11255963 : case FUNCTION_TYPE:
1554 : : /* Exception specs and memfn_rquals were checked above. */
1555 : 11255963 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1556 : : return false;
1557 : 10641332 : if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1558 : : return false;
1559 : : break;
1560 : :
1561 : 1380920 : case ARRAY_TYPE:
1562 : : /* Target types must match incl. qualifiers. */
1563 : 1380920 : if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1564 : 1380920 : ? bounds_either : bounds_none),
1565 : : /*strict=*/true))
1566 : : return false;
1567 : : break;
1568 : :
1569 : 1378221990 : case TEMPLATE_TYPE_PARM:
1570 : : /* If T1 and T2 don't have the same relative position in their
1571 : : template parameters set, they can't be equal. */
1572 : 1378221990 : if (!comp_template_parms_position (t1, t2))
1573 : : return false;
1574 : : /* If T1 and T2 don't represent the same class template deduction,
1575 : : they aren't equal. */
1576 : 1007409300 : if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1577 : 503704650 : CLASS_PLACEHOLDER_TEMPLATE (t2)))
1578 : : return false;
1579 : : /* Constrained 'auto's are distinct from parms that don't have the same
1580 : : constraints. */
1581 : 486859297 : if (!equivalent_placeholder_constraints (t1, t2))
1582 : : return false;
1583 : : break;
1584 : :
1585 : 135645614 : case TYPENAME_TYPE:
1586 : 271291228 : if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1587 : 135645614 : TYPENAME_TYPE_FULLNAME (t2)))
1588 : : return false;
1589 : : /* Qualifiers don't matter on scopes. */
1590 : 118555018 : if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1591 : 118555018 : TYPE_CONTEXT (t2)))
1592 : : return false;
1593 : : break;
1594 : :
1595 : 7669 : case UNBOUND_CLASS_TEMPLATE:
1596 : 7669 : if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1597 : : return false;
1598 : 7669 : if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1599 : : return false;
1600 : : break;
1601 : :
1602 : 3131698 : case COMPLEX_TYPE:
1603 : 3131698 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1604 : : return false;
1605 : : break;
1606 : :
1607 : 11033688 : case VECTOR_TYPE:
1608 : 11033688 : if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1609 : 20639479 : || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1610 : 20687409 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1611 : 1427897 : return false;
1612 : : break;
1613 : :
1614 : 6473673 : case TYPE_PACK_EXPANSION:
1615 : 6473673 : return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1616 : : PACK_EXPANSION_PATTERN (t2))
1617 : 12800211 : && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1618 : 6326538 : PACK_EXPANSION_EXTRA_ARGS (t2)));
1619 : :
1620 : 14 : case PACK_INDEX_TYPE:
1621 : 14 : return (cp_tree_equal (PACK_INDEX_PACK (t1),
1622 : 14 : PACK_INDEX_PACK (t2))
1623 : 16 : && cp_tree_equal (PACK_INDEX_INDEX (t1),
1624 : 2 : PACK_INDEX_INDEX (t2)));
1625 : :
1626 : 13843050 : case DECLTYPE_TYPE:
1627 : 27686100 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1628 : 13843050 : != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1629 : : return false;
1630 : 13835753 : if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1631 : : return false;
1632 : 13835753 : if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1633 : : return false;
1634 : 13835753 : if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1635 : : return false;
1636 : : break;
1637 : :
1638 : 699124 : case TRAIT_TYPE:
1639 : 699124 : if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1640 : : return false;
1641 : 1327 : if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1642 : 2654 : || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1643 : 0 : return false;
1644 : : break;
1645 : :
1646 : 3 : case TYPEOF_TYPE:
1647 : 3 : if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1648 : : return false;
1649 : : break;
1650 : :
1651 : : default:
1652 : : return false;
1653 : : }
1654 : :
1655 : : /* If we get here, we know that from a target independent POV the
1656 : : types are the same. Make sure the target attributes are also
1657 : : the same. */
1658 : 656647525 : if (!comp_type_attributes (t1, t2))
1659 : : return false;
1660 : :
1661 : 656646381 : check_alias:
1662 : 973209312 : if (comparing_dependent_aliases
1663 : 973209312 : && (typedef_variant_p (t1) || typedef_variant_p (t2)))
1664 : : {
1665 : 2673395 : tree dep1 = dependent_opaque_alias_p (t1) ? t1 : NULL_TREE;
1666 : 2673395 : tree dep2 = dependent_opaque_alias_p (t2) ? t2 : NULL_TREE;
1667 : 2673395 : if ((dep1 || dep2)
1668 : 2673395 : && (!(dep1 && dep2)
1669 : 0 : || !comp_type_attributes (DECL_ATTRIBUTES (TYPE_NAME (dep1)),
1670 : 0 : DECL_ATTRIBUTES (TYPE_NAME (dep2)))))
1671 : 15 : return false;
1672 : :
1673 : : /* Don't treat an alias template specialization with dependent
1674 : : arguments as equivalent to its underlying type when used as a
1675 : : template argument; we need them to be distinct so that we
1676 : : substitute into the specialization arguments at instantiation
1677 : : time. And aliases can't be equivalent without being ==, so
1678 : : we don't need to look any deeper. */
1679 : 2673380 : ++processing_template_decl;
1680 : 2673380 : dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1681 : 2673380 : dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1682 : 2673380 : --processing_template_decl;
1683 : 2673380 : if ((dep1 || dep2) && dep1 != dep2)
1684 : : return false;
1685 : : }
1686 : :
1687 : : return true;
1688 : : }
1689 : :
1690 : : /* C++ implementation of compatible_types_for_indirection_note_p. */
1691 : :
1692 : : bool
1693 : 1675 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1694 : : {
1695 : 1675 : return same_type_p (type1, type2);
1696 : : }
1697 : :
1698 : : /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1699 : : is a bitwise-or of the COMPARE_* flags. */
1700 : :
1701 : : bool
1702 : 18828872000 : comptypes (tree t1, tree t2, int strict)
1703 : : {
1704 : 18828872000 : gcc_checking_assert (t1 && t2);
1705 : :
1706 : : /* TYPE_ARGUMENT_PACKS are not really types. */
1707 : 18828872000 : gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1708 : : && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1709 : :
1710 : 18828872000 : if (t1 == t2)
1711 : : return true;
1712 : :
1713 : : /* Suppress errors caused by previously reported errors. */
1714 : 12194788656 : if (t1 == error_mark_node || t2 == error_mark_node)
1715 : : return false;
1716 : :
1717 : 12194788487 : if (strict == COMPARE_STRICT)
1718 : : {
1719 : 11025636320 : if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1720 : : /* At least one of the types requires structural equality, so
1721 : : perform a deep check. */
1722 : 960352178 : return structural_comptypes (t1, t2, strict);
1723 : :
1724 : 10065284142 : if (flag_checking && param_use_canonical_types)
1725 : : {
1726 : 10065283139 : bool result = structural_comptypes (t1, t2, strict);
1727 : :
1728 : 10065283139 : if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1729 : : /* The two types are structurally equivalent, but their
1730 : : canonical types were different. This is a failure of the
1731 : : canonical type propagation code.*/
1732 : 0 : internal_error
1733 : 0 : ("canonical types differ for identical types %qT and %qT",
1734 : : t1, t2);
1735 : 10065283139 : else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1736 : : /* Two types are structurally different, but the canonical
1737 : : types are the same. This means we were over-eager in
1738 : : assigning canonical types. */
1739 : 0 : internal_error
1740 : 0 : ("same canonical type node for different types %qT and %qT",
1741 : : t1, t2);
1742 : :
1743 : : return result;
1744 : : }
1745 : 1003 : if (!flag_checking && param_use_canonical_types)
1746 : 1003 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1747 : : else
1748 : 0 : return structural_comptypes (t1, t2, strict);
1749 : : }
1750 : 1169152167 : else if (strict == COMPARE_STRUCTURAL)
1751 : 1132862602 : return structural_comptypes (t1, t2, COMPARE_STRICT);
1752 : : else
1753 : 36289565 : return structural_comptypes (t1, t2, strict);
1754 : : }
1755 : :
1756 : : /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1757 : : top-level qualifiers. */
1758 : :
1759 : : bool
1760 : 2712204608 : same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1761 : : {
1762 : 2712204608 : if (type1 == error_mark_node || type2 == error_mark_node)
1763 : : return false;
1764 : 2712204598 : if (type1 == type2)
1765 : : return true;
1766 : :
1767 : 1514951214 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1768 : 1514951214 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1769 : 1514951214 : return same_type_p (type1, type2);
1770 : : }
1771 : :
1772 : : /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1773 : :
1774 : : bool
1775 : 211081547 : similar_type_p (tree type1, tree type2)
1776 : : {
1777 : 211081547 : if (type1 == error_mark_node || type2 == error_mark_node)
1778 : : return false;
1779 : :
1780 : : /* Informally, two types are similar if, ignoring top-level cv-qualification:
1781 : : * they are the same type; or
1782 : : * they are both pointers, and the pointed-to types are similar; or
1783 : : * they are both pointers to member of the same class, and the types of
1784 : : the pointed-to members are similar; or
1785 : : * they are both arrays of the same size or both arrays of unknown bound,
1786 : : and the array element types are similar. */
1787 : :
1788 : 211081547 : if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1789 : : return true;
1790 : :
1791 : 97529971 : if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1792 : 97424115 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1793 : 97424115 : || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1794 : 106322 : return comp_ptr_ttypes_const (type1, type2, bounds_either);
1795 : :
1796 : : return false;
1797 : : }
1798 : :
1799 : : /* Helper function for layout_compatible_type_p and
1800 : : is_corresponding_member_aggr. Advance to next members (NULL if
1801 : : no further ones) and return true if those members are still part of
1802 : : the common initial sequence. */
1803 : :
1804 : : bool
1805 : 1996 : next_common_initial_sequence (tree &memb1, tree &memb2)
1806 : : {
1807 : 2456 : while (memb1)
1808 : : {
1809 : 2492 : if (TREE_CODE (memb1) != FIELD_DECL
1810 : 2090 : || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1811 : : {
1812 : 402 : memb1 = DECL_CHAIN (memb1);
1813 : 402 : continue;
1814 : : }
1815 : 1688 : if (DECL_FIELD_IS_BASE (memb1))
1816 : : {
1817 : 58 : memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1818 : 58 : continue;
1819 : : }
1820 : : break;
1821 : : }
1822 : 2426 : while (memb2)
1823 : : {
1824 : 2462 : if (TREE_CODE (memb2) != FIELD_DECL
1825 : 2060 : || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1826 : : {
1827 : 402 : memb2 = DECL_CHAIN (memb2);
1828 : 402 : continue;
1829 : : }
1830 : 1658 : if (DECL_FIELD_IS_BASE (memb2))
1831 : : {
1832 : 28 : memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1833 : 28 : continue;
1834 : : }
1835 : : break;
1836 : : }
1837 : 1996 : if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1838 : : return true;
1839 : 1630 : if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1840 : : return false;
1841 : 1630 : if (DECL_BIT_FIELD_TYPE (memb1))
1842 : : {
1843 : 138 : if (!DECL_BIT_FIELD_TYPE (memb2))
1844 : : return false;
1845 : 39 : if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1846 : 39 : DECL_BIT_FIELD_TYPE (memb2)))
1847 : : return false;
1848 : 39 : if (TYPE_PRECISION (TREE_TYPE (memb1))
1849 : 39 : != TYPE_PRECISION (TREE_TYPE (memb2)))
1850 : : return false;
1851 : : }
1852 : 1492 : else if (DECL_BIT_FIELD_TYPE (memb2))
1853 : : return false;
1854 : 1465 : else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1855 : : return false;
1856 : 1441 : if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1857 : 1441 : != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1858 : : return false;
1859 : 1414 : if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1860 : : return false;
1861 : 1383 : if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1862 : : return false;
1863 : : return true;
1864 : : }
1865 : :
1866 : : /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1867 : :
1868 : : bool
1869 : 2503 : layout_compatible_type_p (tree type1, tree type2)
1870 : : {
1871 : 2503 : if (type1 == error_mark_node || type2 == error_mark_node)
1872 : : return false;
1873 : 2503 : if (type1 == type2)
1874 : : return true;
1875 : 1222 : if (TREE_CODE (type1) != TREE_CODE (type2))
1876 : : return false;
1877 : :
1878 : 1184 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1879 : 1184 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1880 : :
1881 : 1184 : if (TREE_CODE (type1) == ENUMERAL_TYPE)
1882 : 32 : return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1883 : 32 : && same_type_p (finish_underlying_type (type1),
1884 : : finish_underlying_type (type2)));
1885 : :
1886 : 367 : if (CLASS_TYPE_P (type1)
1887 : 367 : && std_layout_type_p (type1)
1888 : 357 : && std_layout_type_p (type2)
1889 : 1509 : && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1890 : : {
1891 : 315 : tree field1 = TYPE_FIELDS (type1);
1892 : 315 : tree field2 = TYPE_FIELDS (type2);
1893 : 315 : if (TREE_CODE (type1) == RECORD_TYPE)
1894 : : {
1895 : 966 : while (1)
1896 : : {
1897 : 606 : if (!next_common_initial_sequence (field1, field2))
1898 : : return false;
1899 : 600 : if (field1 == NULL_TREE)
1900 : : return true;
1901 : 360 : field1 = DECL_CHAIN (field1);
1902 : 360 : field2 = DECL_CHAIN (field2);
1903 : : }
1904 : : }
1905 : : /* Otherwise both types must be union types.
1906 : : The standard says:
1907 : : "Two standard-layout unions are layout-compatible if they have
1908 : : the same number of non-static data members and corresponding
1909 : : non-static data members (in any order) have layout-compatible
1910 : : types."
1911 : : but the code anticipates that bitfield vs. non-bitfield,
1912 : : different bitfield widths or presence/absence of
1913 : : [[no_unique_address]] should be checked as well. */
1914 : 69 : auto_vec<tree, 16> vec;
1915 : 69 : unsigned int count = 0;
1916 : 315 : for (; field1; field1 = DECL_CHAIN (field1))
1917 : 246 : if (TREE_CODE (field1) == FIELD_DECL)
1918 : 162 : count++;
1919 : 315 : for (; field2; field2 = DECL_CHAIN (field2))
1920 : 246 : if (TREE_CODE (field2) == FIELD_DECL)
1921 : 162 : vec.safe_push (field2);
1922 : : /* Discussions on core lean towards treating multiple union fields
1923 : : of the same type as the same field, so this might need changing
1924 : : in the future. */
1925 : 138 : if (count != vec.length ())
1926 : : return false;
1927 : 297 : for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1928 : : {
1929 : 234 : if (TREE_CODE (field1) != FIELD_DECL)
1930 : 78 : continue;
1931 : 156 : unsigned int j;
1932 : 156 : tree t1 = DECL_BIT_FIELD_TYPE (field1);
1933 : 156 : if (t1 == NULL_TREE)
1934 : 153 : t1 = TREE_TYPE (field1);
1935 : 258 : FOR_EACH_VEC_ELT (vec, j, field2)
1936 : : {
1937 : 252 : tree t2 = DECL_BIT_FIELD_TYPE (field2);
1938 : 252 : if (t2 == NULL_TREE)
1939 : 240 : t2 = TREE_TYPE (field2);
1940 : 252 : if (DECL_BIT_FIELD_TYPE (field1))
1941 : : {
1942 : 6 : if (!DECL_BIT_FIELD_TYPE (field2))
1943 : 0 : continue;
1944 : 6 : if (TYPE_PRECISION (TREE_TYPE (field1))
1945 : 6 : != TYPE_PRECISION (TREE_TYPE (field2)))
1946 : 6 : continue;
1947 : : }
1948 : 246 : else if (DECL_BIT_FIELD_TYPE (field2))
1949 : 6 : continue;
1950 : 240 : if (!layout_compatible_type_p (t1, t2))
1951 : 90 : continue;
1952 : 150 : if ((!lookup_attribute ("no_unique_address",
1953 : 150 : DECL_ATTRIBUTES (field1)))
1954 : 150 : != !lookup_attribute ("no_unique_address",
1955 : 150 : DECL_ATTRIBUTES (field2)))
1956 : 0 : continue;
1957 : : break;
1958 : : }
1959 : 312 : if (j == vec.length ())
1960 : : return false;
1961 : 150 : vec.unordered_remove (j);
1962 : : }
1963 : : return true;
1964 : 69 : }
1965 : :
1966 : 837 : return same_type_p (type1, type2);
1967 : : }
1968 : :
1969 : : /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1970 : :
1971 : : bool
1972 : 153841022 : at_least_as_qualified_p (const_tree type1, const_tree type2)
1973 : : {
1974 : 153841022 : int q1 = cp_type_quals (type1);
1975 : 153841022 : int q2 = cp_type_quals (type2);
1976 : :
1977 : : /* All qualifiers for TYPE2 must also appear in TYPE1. */
1978 : 153841022 : return (q1 & q2) == q2;
1979 : : }
1980 : :
1981 : : /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1982 : : more cv-qualified that TYPE1, and 0 otherwise. */
1983 : :
1984 : : int
1985 : 10392090 : comp_cv_qualification (int q1, int q2)
1986 : : {
1987 : 10392090 : if (q1 == q2)
1988 : : return 0;
1989 : :
1990 : 2978915 : if ((q1 & q2) == q2)
1991 : : return 1;
1992 : 239362 : else if ((q1 & q2) == q1)
1993 : 238773 : return -1;
1994 : :
1995 : : return 0;
1996 : : }
1997 : :
1998 : : int
1999 : 0 : comp_cv_qualification (const_tree type1, const_tree type2)
2000 : : {
2001 : 0 : int q1 = cp_type_quals (type1);
2002 : 0 : int q2 = cp_type_quals (type2);
2003 : 0 : return comp_cv_qualification (q1, q2);
2004 : : }
2005 : :
2006 : : /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
2007 : : subset of the cv-qualification signature of TYPE2, and the types
2008 : : are similar. Returns -1 if the other way 'round, and 0 otherwise. */
2009 : :
2010 : : int
2011 : 365 : comp_cv_qual_signature (tree type1, tree type2)
2012 : : {
2013 : 365 : if (comp_ptr_ttypes_real (type2, type1, -1))
2014 : : return 1;
2015 : 289 : else if (comp_ptr_ttypes_real (type1, type2, -1))
2016 : : return -1;
2017 : : else
2018 : 277 : return 0;
2019 : : }
2020 : :
2021 : : /* Subroutines of `comptypes'. */
2022 : :
2023 : : /* Return true if two parameter type lists PARMS1 and PARMS2 are
2024 : : equivalent in the sense that functions with those parameter types
2025 : : can have equivalent types. The two lists must be equivalent,
2026 : : element by element. */
2027 : :
2028 : : bool
2029 : 845616824 : compparms (const_tree parms1, const_tree parms2)
2030 : : {
2031 : 845616824 : const_tree t1, t2;
2032 : :
2033 : : /* An unspecified parmlist matches any specified parmlist
2034 : : whose argument types don't need default promotions. */
2035 : :
2036 : 845616824 : for (t1 = parms1, t2 = parms2;
2037 : 1364751123 : t1 || t2;
2038 : 519134299 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2039 : : {
2040 : : /* If one parmlist is shorter than the other,
2041 : : they fail to match. */
2042 : 1284283778 : if (!t1 || !t2)
2043 : : return false;
2044 : 1283593273 : if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2045 : : return false;
2046 : : }
2047 : : return true;
2048 : : }
2049 : :
2050 : :
2051 : : /* Process a sizeof or alignof expression where the operand is a type.
2052 : : STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2053 : : or GNU (preferred alignment) semantics; it is ignored if OP is
2054 : : SIZEOF_EXPR. */
2055 : :
2056 : : tree
2057 : 22024473 : cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2058 : : bool std_alignof, bool complain)
2059 : : {
2060 : 22024473 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2061 : 22024473 : if (type == error_mark_node)
2062 : : return error_mark_node;
2063 : :
2064 : 22024471 : type = non_reference (type);
2065 : 22024471 : if (TREE_CODE (type) == METHOD_TYPE)
2066 : : {
2067 : 0 : if (complain)
2068 : : {
2069 : 0 : pedwarn (loc, OPT_Wpointer_arith,
2070 : : "invalid application of %qs to a member function",
2071 : 0 : OVL_OP_INFO (false, op)->name);
2072 : 0 : return size_one_node;
2073 : : }
2074 : : else
2075 : 0 : return error_mark_node;
2076 : : }
2077 : 22024471 : else if (VOID_TYPE_P (type) && std_alignof)
2078 : : {
2079 : 14 : if (complain)
2080 : 14 : error_at (loc, "invalid application of %qs to a void type",
2081 : 14 : OVL_OP_INFO (false, op)->name);
2082 : 14 : return error_mark_node;
2083 : : }
2084 : :
2085 : 22024457 : bool dependent_p = dependent_type_p (type);
2086 : 22024457 : if (!dependent_p)
2087 : : {
2088 : 18290292 : complete_type (type);
2089 : 18290292 : if (!COMPLETE_TYPE_P (type))
2090 : : /* Call this here because the incompleteness diagnostic comes from
2091 : : c_sizeof_or_alignof_type instead of
2092 : : complete_type_or_maybe_complain. */
2093 : 2218 : note_failed_type_completion (type, complain);
2094 : : }
2095 : 18290292 : if (dependent_p
2096 : : /* VLA types will have a non-constant size. In the body of an
2097 : : uninstantiated template, we don't need to try to compute the
2098 : : value, because the sizeof expression is not an integral
2099 : : constant expression in that case. And, if we do try to
2100 : : compute the value, we'll likely end up with SAVE_EXPRs, which
2101 : : the template substitution machinery does not expect to see. */
2102 : 18290292 : || (processing_template_decl
2103 : 1671837 : && COMPLETE_TYPE_P (type)
2104 : 1671831 : && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2105 : : {
2106 : 3734165 : tree value = build_min (op, size_type_node, type);
2107 : 3734165 : TREE_READONLY (value) = 1;
2108 : 3734165 : if (op == ALIGNOF_EXPR && std_alignof)
2109 : 443088 : ALIGNOF_EXPR_STD_P (value) = true;
2110 : 3734165 : SET_EXPR_LOCATION (value, loc);
2111 : 3734165 : return value;
2112 : : }
2113 : :
2114 : 18290292 : return c_sizeof_or_alignof_type (loc, complete_type (type),
2115 : : op == SIZEOF_EXPR, std_alignof,
2116 : 18290292 : complain & (tf_warning_or_error));
2117 : : }
2118 : :
2119 : : /* Return the size of the type, without producing any warnings for
2120 : : types whose size cannot be taken. This routine should be used only
2121 : : in some other routine that has already produced a diagnostic about
2122 : : using the size of such a type. */
2123 : : tree
2124 : 2120949 : cxx_sizeof_nowarn (tree type)
2125 : : {
2126 : 2120949 : if (TREE_CODE (type) == FUNCTION_TYPE
2127 : 2120949 : || VOID_TYPE_P (type)
2128 : 2120867 : || TREE_CODE (type) == ERROR_MARK)
2129 : 82 : return size_one_node;
2130 : 2120867 : else if (!COMPLETE_TYPE_P (type))
2131 : 19 : return size_zero_node;
2132 : : else
2133 : 2120848 : return cxx_sizeof_or_alignof_type (input_location, type,
2134 : 2120848 : SIZEOF_EXPR, false, false);
2135 : : }
2136 : :
2137 : : /* Process a sizeof expression where the operand is an expression. */
2138 : :
2139 : : static tree
2140 : 1360032 : cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2141 : : {
2142 : 1360032 : if (e == error_mark_node)
2143 : : return error_mark_node;
2144 : :
2145 : 1359293 : if (instantiation_dependent_uneval_expression_p (e))
2146 : : {
2147 : 303969 : e = build_min (SIZEOF_EXPR, size_type_node, e);
2148 : 303969 : TREE_SIDE_EFFECTS (e) = 0;
2149 : 303969 : TREE_READONLY (e) = 1;
2150 : 303969 : SET_EXPR_LOCATION (e, loc);
2151 : :
2152 : 303969 : return e;
2153 : : }
2154 : :
2155 : 1055324 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2156 : 1055324 : STRIP_ANY_LOCATION_WRAPPER (e);
2157 : :
2158 : 1055324 : if (TREE_CODE (e) == PARM_DECL
2159 : 43590 : && DECL_ARRAY_PARAMETER_P (e)
2160 : 1055701 : && (complain & tf_warning))
2161 : : {
2162 : 120 : auto_diagnostic_group d;
2163 : 120 : if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2164 : : "%<sizeof%> on array function parameter %qE "
2165 : 120 : "will return size of %qT", e, TREE_TYPE (e)))
2166 : 24 : inform (DECL_SOURCE_LOCATION (e), "declared here");
2167 : 120 : }
2168 : :
2169 : 1055324 : e = mark_type_use (e);
2170 : :
2171 : 1055324 : if (bitfield_p (e))
2172 : : {
2173 : 12 : if (complain & tf_error)
2174 : 6 : error_at (e_loc,
2175 : : "invalid application of %<sizeof%> to a bit-field");
2176 : : else
2177 : 6 : return error_mark_node;
2178 : 6 : e = char_type_node;
2179 : : }
2180 : 1055312 : else if (is_overloaded_fn (e))
2181 : : {
2182 : 33 : if (complain & tf_error)
2183 : 12 : permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2184 : : "an expression of function type");
2185 : : else
2186 : 21 : return error_mark_node;
2187 : 12 : e = char_type_node;
2188 : : }
2189 : 1055279 : else if (type_unknown_p (e))
2190 : : {
2191 : 0 : if (complain & tf_error)
2192 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2193 : : else
2194 : 0 : return error_mark_node;
2195 : 0 : e = char_type_node;
2196 : : }
2197 : : else
2198 : 1055279 : e = TREE_TYPE (e);
2199 : :
2200 : 1055297 : return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2201 : 1055297 : complain & tf_error);
2202 : : }
2203 : :
2204 : : /* Implement the __alignof keyword: Return the minimum required
2205 : : alignment of E, measured in bytes. For VAR_DECL's and
2206 : : FIELD_DECL's return DECL_ALIGN (which can be set from an
2207 : : "aligned" __attribute__ specification). STD_ALIGNOF acts
2208 : : like in cxx_sizeof_or_alignof_type. */
2209 : :
2210 : : static tree
2211 : 101307 : cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2212 : : tsubst_flags_t complain)
2213 : : {
2214 : 101307 : tree t;
2215 : :
2216 : 101307 : if (e == error_mark_node)
2217 : : return error_mark_node;
2218 : :
2219 : 101299 : if (processing_template_decl)
2220 : : {
2221 : 19596 : e = build_min (ALIGNOF_EXPR, size_type_node, e);
2222 : 19596 : TREE_SIDE_EFFECTS (e) = 0;
2223 : 19596 : TREE_READONLY (e) = 1;
2224 : 19596 : SET_EXPR_LOCATION (e, loc);
2225 : 19596 : ALIGNOF_EXPR_STD_P (e) = std_alignof;
2226 : :
2227 : 19596 : return e;
2228 : : }
2229 : :
2230 : 81703 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2231 : 81703 : STRIP_ANY_LOCATION_WRAPPER (e);
2232 : :
2233 : 81703 : e = mark_type_use (e);
2234 : :
2235 : 81703 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2236 : : !(complain & tf_error)))
2237 : : {
2238 : 0 : if (!(complain & tf_error))
2239 : 0 : return error_mark_node;
2240 : 0 : t = size_one_node;
2241 : : }
2242 : 81703 : else if (VAR_P (e))
2243 : 12270 : t = size_int (DECL_ALIGN_UNIT (e));
2244 : 69433 : else if (bitfield_p (e))
2245 : : {
2246 : 6 : if (complain & tf_error)
2247 : 6 : error_at (e_loc,
2248 : : "invalid application of %<__alignof%> to a bit-field");
2249 : : else
2250 : 0 : return error_mark_node;
2251 : 6 : t = size_one_node;
2252 : : }
2253 : 69427 : else if (TREE_CODE (e) == COMPONENT_REF
2254 : 69427 : && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2255 : 37792 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2256 : 31635 : else if (is_overloaded_fn (e))
2257 : : {
2258 : 6 : if (complain & tf_error)
2259 : 6 : permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2260 : : "an expression of function type");
2261 : : else
2262 : 0 : return error_mark_node;
2263 : 6 : if (TREE_CODE (e) == FUNCTION_DECL)
2264 : 3 : t = size_int (DECL_ALIGN_UNIT (e));
2265 : : else
2266 : 3 : t = size_one_node;
2267 : : }
2268 : 31629 : else if (type_unknown_p (e))
2269 : : {
2270 : 0 : if (complain & tf_error)
2271 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2272 : : else
2273 : 0 : return error_mark_node;
2274 : 0 : t = size_one_node;
2275 : : }
2276 : : else
2277 : 31629 : return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2278 : : ALIGNOF_EXPR, std_alignof,
2279 : 31629 : complain & tf_error);
2280 : :
2281 : 50074 : return fold_convert_loc (loc, size_type_node, t);
2282 : : }
2283 : :
2284 : : /* Process a sizeof or alignof expression E with code OP where the operand
2285 : : is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2286 : :
2287 : : tree
2288 : 1461339 : cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2289 : : bool std_alignof, bool complain)
2290 : : {
2291 : 1461339 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2292 : 1461339 : if (op == SIZEOF_EXPR)
2293 : 2060655 : return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2294 : : else
2295 : 101334 : return cxx_alignof_expr (loc, e, std_alignof,
2296 : 101307 : complain? tf_warning_or_error : tf_none);
2297 : : }
2298 : :
2299 : : /* Build a representation of an expression 'alignas(E).' Return the
2300 : : folded integer value of E if it is an integral constant expression
2301 : : that resolves to a valid alignment. If E depends on a template
2302 : : parameter, return a syntactic representation tree of kind
2303 : : ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2304 : : expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2305 : :
2306 : : tree
2307 : 192614 : cxx_alignas_expr (tree e)
2308 : : {
2309 : 192614 : if (e == NULL_TREE || e == error_mark_node
2310 : 385228 : || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2311 : 0 : return e;
2312 : :
2313 : 192614 : if (TYPE_P (e))
2314 : : /* [dcl.align]/3:
2315 : :
2316 : : When the alignment-specifier is of the form
2317 : : alignas(type-id), it shall have the same effect as
2318 : : alignas(alignof(type-id)). */
2319 : :
2320 : 119898 : return cxx_sizeof_or_alignof_type (input_location,
2321 : : e, ALIGNOF_EXPR,
2322 : : /*std_alignof=*/true,
2323 : 119898 : /*complain=*/true);
2324 : :
2325 : : /* If we reach this point, it means the alignas expression if of
2326 : : the form "alignas(assignment-expression)", so we should follow
2327 : : what is stated by [dcl.align]/2. */
2328 : :
2329 : 72716 : if (value_dependent_expression_p (e))
2330 : : /* Leave value-dependent expression alone for now. */
2331 : : return e;
2332 : :
2333 : 16467 : e = instantiate_non_dependent_expr (e);
2334 : 16467 : e = mark_rvalue_use (e);
2335 : :
2336 : : /* [dcl.align]/2 says:
2337 : :
2338 : : the assignment-expression shall be an integral constant
2339 : : expression. */
2340 : :
2341 : 16467 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2342 : : {
2343 : 18 : error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2344 : 18 : return error_mark_node;
2345 : : }
2346 : :
2347 : 16449 : return cxx_constant_value (e);
2348 : : }
2349 : :
2350 : :
2351 : : /* EXPR is being used in a context that is not a function call.
2352 : : Enforce:
2353 : :
2354 : : [expr.ref]
2355 : :
2356 : : The expression can be used only as the left-hand operand of a
2357 : : member function call.
2358 : :
2359 : : [expr.mptr.operator]
2360 : :
2361 : : If the result of .* or ->* is a function, then that result can be
2362 : : used only as the operand for the function call operator ().
2363 : :
2364 : : by issuing an error message if appropriate. Returns true iff EXPR
2365 : : violates these rules. */
2366 : :
2367 : : bool
2368 : 1169777814 : invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2369 : : {
2370 : 1169777814 : if (expr == NULL_TREE)
2371 : : return false;
2372 : : /* Don't enforce this in MS mode. */
2373 : 1169776389 : if (flag_ms_extensions)
2374 : : return false;
2375 : 1169768664 : if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2376 : 96051415 : expr = get_first_fn (expr);
2377 : 1169768664 : if (TREE_TYPE (expr)
2378 : 1169768664 : && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2379 : : {
2380 : 91 : if (complain & tf_error)
2381 : : {
2382 : 87 : if (DECL_P (expr))
2383 : : {
2384 : 69 : auto_diagnostic_group d;
2385 : 69 : error_at (loc, "invalid use of non-static member function %qD",
2386 : : expr);
2387 : 69 : inform (DECL_SOURCE_LOCATION (expr), "declared here");
2388 : 69 : }
2389 : : else
2390 : 18 : error_at (loc, "invalid use of non-static member function of "
2391 : 18 : "type %qT", TREE_TYPE (expr));
2392 : : }
2393 : 91 : return true;
2394 : : }
2395 : : return false;
2396 : : }
2397 : :
2398 : : /* If EXP is a reference to a bit-field, and the type of EXP does not
2399 : : match the declared type of the bit-field, return the declared type
2400 : : of the bit-field. Otherwise, return NULL_TREE. */
2401 : :
2402 : : tree
2403 : 2744823841 : is_bitfield_expr_with_lowered_type (const_tree exp)
2404 : : {
2405 : 3808731674 : switch (TREE_CODE (exp))
2406 : : {
2407 : 4931594 : case COND_EXPR:
2408 : 9863188 : if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2409 : 4931568 : ? TREE_OPERAND (exp, 1)
2410 : 26 : : TREE_OPERAND (exp, 0)))
2411 : : return NULL_TREE;
2412 : 135 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2413 : :
2414 : 618568 : case COMPOUND_EXPR:
2415 : 618568 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2416 : :
2417 : 348277217 : case MODIFY_EXPR:
2418 : 348277217 : case SAVE_EXPR:
2419 : 348277217 : case UNARY_PLUS_EXPR:
2420 : 348277217 : case PREDECREMENT_EXPR:
2421 : 348277217 : case PREINCREMENT_EXPR:
2422 : 348277217 : case POSTDECREMENT_EXPR:
2423 : 348277217 : case POSTINCREMENT_EXPR:
2424 : 348277217 : case NEGATE_EXPR:
2425 : 348277217 : case NON_LVALUE_EXPR:
2426 : 348277217 : case BIT_NOT_EXPR:
2427 : 348277217 : case CLEANUP_POINT_EXPR:
2428 : 348277217 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2429 : :
2430 : 234779453 : case COMPONENT_REF:
2431 : 234779453 : {
2432 : 234779453 : tree field;
2433 : :
2434 : 234779453 : field = TREE_OPERAND (exp, 1);
2435 : 234779453 : if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2436 : : return NULL_TREE;
2437 : 41049206 : if (same_type_ignoring_top_level_qualifiers_p
2438 : 41049206 : (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2439 : : return NULL_TREE;
2440 : 40898297 : return DECL_BIT_FIELD_TYPE (field);
2441 : : }
2442 : :
2443 : 515007638 : case VAR_DECL:
2444 : 515007638 : if (DECL_HAS_VALUE_EXPR_P (exp))
2445 : 1841611 : return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2446 : 1841611 : (CONST_CAST_TREE (exp)));
2447 : : return NULL_TREE;
2448 : :
2449 : 720147775 : case VIEW_CONVERT_EXPR:
2450 : 720147775 : if (location_wrapper_p (exp))
2451 : 713170302 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2452 : : else
2453 : : return NULL_TREE;
2454 : :
2455 : : default:
2456 : : return NULL_TREE;
2457 : : }
2458 : : }
2459 : :
2460 : : /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2461 : : bitfield with a lowered type, the type of EXP is returned, rather
2462 : : than NULL_TREE. */
2463 : :
2464 : : tree
2465 : 1298207153 : unlowered_expr_type (const_tree exp)
2466 : : {
2467 : 1298207153 : tree type;
2468 : 1298207153 : tree etype = TREE_TYPE (exp);
2469 : :
2470 : 1298207153 : type = is_bitfield_expr_with_lowered_type (exp);
2471 : 1298207153 : if (type)
2472 : 34940434 : type = cp_build_qualified_type (type, cp_type_quals (etype));
2473 : : else
2474 : : type = etype;
2475 : :
2476 : 1298207153 : return type;
2477 : : }
2478 : :
2479 : : /* Perform the conversions in [expr] that apply when an lvalue appears
2480 : : in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2481 : : function-to-pointer conversions. In addition, bitfield references are
2482 : : converted to their declared types. Note that this function does not perform
2483 : : the lvalue-to-rvalue conversion for class types. If you need that conversion
2484 : : for class types, then you probably need to use force_rvalue.
2485 : :
2486 : : Although the returned value is being used as an rvalue, this
2487 : : function does not wrap the returned expression in a
2488 : : NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2489 : : that the return value is no longer an lvalue. */
2490 : :
2491 : : tree
2492 : 934601407 : decay_conversion (tree exp,
2493 : : tsubst_flags_t complain,
2494 : : bool reject_builtin /* = true */)
2495 : : {
2496 : 934601407 : tree type;
2497 : 934601407 : enum tree_code code;
2498 : 934601407 : location_t loc = cp_expr_loc_or_input_loc (exp);
2499 : :
2500 : 934601407 : type = TREE_TYPE (exp);
2501 : 934601407 : if (type == error_mark_node)
2502 : : return error_mark_node;
2503 : :
2504 : 934601194 : exp = resolve_nondeduced_context_or_error (exp, complain);
2505 : :
2506 : 934601194 : code = TREE_CODE (type);
2507 : :
2508 : 934601194 : if (error_operand_p (exp))
2509 : 90 : return error_mark_node;
2510 : :
2511 : 934601104 : if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2512 : : {
2513 : 1452210 : mark_rvalue_use (exp, loc, reject_builtin);
2514 : 1452210 : return nullptr_node;
2515 : : }
2516 : :
2517 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2518 : : Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2519 : 933148894 : if (code == VOID_TYPE)
2520 : : {
2521 : 5782 : if (complain & tf_error)
2522 : 3 : error_at (loc, "void value not ignored as it ought to be");
2523 : 5782 : return error_mark_node;
2524 : : }
2525 : 933143112 : if (invalid_nonstatic_memfn_p (loc, exp, complain))
2526 : 6 : return error_mark_node;
2527 : 933143106 : if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2528 : : {
2529 : 97404991 : exp = mark_lvalue_use (exp);
2530 : 97404991 : if (reject_builtin && reject_gcc_builtin (exp, loc))
2531 : 110 : return error_mark_node;
2532 : 97404881 : return cp_build_addr_expr (exp, complain);
2533 : : }
2534 : 835738115 : if (code == ARRAY_TYPE)
2535 : : {
2536 : 5194885 : tree adr;
2537 : 5194885 : tree ptrtype;
2538 : :
2539 : 5194885 : exp = mark_lvalue_use (exp);
2540 : :
2541 : 5194885 : if (INDIRECT_REF_P (exp))
2542 : 95723 : return build_nop (build_pointer_type (TREE_TYPE (type)),
2543 : 191446 : TREE_OPERAND (exp, 0));
2544 : :
2545 : 5099162 : if (TREE_CODE (exp) == COMPOUND_EXPR)
2546 : : {
2547 : 3 : tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2548 : 3 : if (op1 == error_mark_node)
2549 : : return error_mark_node;
2550 : 3 : return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2551 : 6 : TREE_OPERAND (exp, 0), op1);
2552 : : }
2553 : :
2554 : 5099159 : if (!obvalue_p (exp)
2555 : 5099159 : && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2556 : : {
2557 : 0 : if (complain & tf_error)
2558 : 0 : error_at (loc, "invalid use of non-lvalue array");
2559 : 0 : return error_mark_node;
2560 : : }
2561 : :
2562 : 5099159 : ptrtype = build_pointer_type (TREE_TYPE (type));
2563 : :
2564 : 5099159 : if (VAR_P (exp))
2565 : : {
2566 : 504487 : if (!cxx_mark_addressable (exp))
2567 : 0 : return error_mark_node;
2568 : 504487 : adr = build_nop (ptrtype, build_address (exp));
2569 : 504487 : return adr;
2570 : : }
2571 : : /* This way is better for a COMPONENT_REF since it can
2572 : : simplify the offset for a component. */
2573 : 4594672 : adr = cp_build_addr_expr (exp, complain);
2574 : 4594672 : return cp_convert (ptrtype, adr, complain);
2575 : : }
2576 : :
2577 : : /* Otherwise, it's the lvalue-to-rvalue conversion. */
2578 : 830543230 : exp = mark_rvalue_use (exp, loc, reject_builtin);
2579 : :
2580 : : /* If a bitfield is used in a context where integral promotion
2581 : : applies, then the caller is expected to have used
2582 : : default_conversion. That function promotes bitfields correctly
2583 : : before calling this function. At this point, if we have a
2584 : : bitfield referenced, we may assume that is not subject to
2585 : : promotion, and that, therefore, the type of the resulting rvalue
2586 : : is the declared type of the bitfield. */
2587 : 830543230 : exp = convert_bitfield_to_declared_type (exp);
2588 : :
2589 : : /* We do not call rvalue() here because we do not want to wrap EXP
2590 : : in a NON_LVALUE_EXPR. */
2591 : :
2592 : : /* [basic.lval]
2593 : :
2594 : : Non-class rvalues always have cv-unqualified types. */
2595 : 830543230 : type = TREE_TYPE (exp);
2596 : 830543230 : if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2597 : 253480187 : exp = build_nop (cv_unqualified (type), exp);
2598 : :
2599 : 830543230 : if (!complete_type_or_maybe_complain (type, exp, complain))
2600 : 42 : return error_mark_node;
2601 : :
2602 : : return exp;
2603 : : }
2604 : :
2605 : : /* Perform preparatory conversions, as part of the "usual arithmetic
2606 : : conversions". In particular, as per [expr]:
2607 : :
2608 : : Whenever an lvalue expression appears as an operand of an
2609 : : operator that expects the rvalue for that operand, the
2610 : : lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2611 : : standard conversions are applied to convert the expression to an
2612 : : rvalue.
2613 : :
2614 : : In addition, we perform integral promotions here, as those are
2615 : : applied to both operands to a binary operator before determining
2616 : : what additional conversions should apply. */
2617 : :
2618 : : static tree
2619 : 259190803 : cp_default_conversion (tree exp, tsubst_flags_t complain)
2620 : : {
2621 : : /* Check for target-specific promotions. */
2622 : 259190803 : tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2623 : 259190803 : if (promoted_type)
2624 : 0 : exp = cp_convert (promoted_type, exp, complain);
2625 : : /* Perform the integral promotions first so that bitfield
2626 : : expressions (which may promote to "int", even if the bitfield is
2627 : : declared "unsigned") are promoted correctly. */
2628 : 259190803 : else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2629 : 171610535 : exp = cp_perform_integral_promotions (exp, complain);
2630 : : /* Perform the other conversions. */
2631 : 259190803 : exp = decay_conversion (exp, complain);
2632 : :
2633 : 259190803 : return exp;
2634 : : }
2635 : :
2636 : : /* C version. */
2637 : :
2638 : : tree
2639 : 38274868 : default_conversion (tree exp)
2640 : : {
2641 : 38274868 : return cp_default_conversion (exp, tf_warning_or_error);
2642 : : }
2643 : :
2644 : : /* EXPR is an expression with an integral or enumeration type.
2645 : : Perform the integral promotions in [conv.prom], and return the
2646 : : converted value. */
2647 : :
2648 : : tree
2649 : 177684697 : cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2650 : : {
2651 : 177684697 : tree type;
2652 : 177684697 : tree promoted_type;
2653 : :
2654 : 177684697 : expr = mark_rvalue_use (expr);
2655 : 177684697 : if (error_operand_p (expr))
2656 : 6 : return error_mark_node;
2657 : :
2658 : 177684691 : type = TREE_TYPE (expr);
2659 : :
2660 : : /* [conv.prom]
2661 : :
2662 : : A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2663 : : of type int if int can represent all the values of the bit-field;
2664 : : otherwise, it can be converted to unsigned int if unsigned int can
2665 : : represent all the values of the bit-field. If the bit-field is larger yet,
2666 : : no integral promotion applies to it. If the bit-field has an enumerated
2667 : : type, it is treated as any other value of that type for promotion
2668 : : purposes. */
2669 : 177684691 : tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2670 : 177684691 : if (bitfield_type
2671 : 177684691 : && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2672 : 316632 : || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2673 : : type = bitfield_type;
2674 : :
2675 : 177684691 : gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2676 : : /* Scoped enums don't promote. */
2677 : 177684691 : if (SCOPED_ENUM_P (type))
2678 : : return expr;
2679 : 176627761 : promoted_type = type_promotes_to (type);
2680 : 176627761 : if (type != promoted_type)
2681 : 48608247 : expr = cp_convert (promoted_type, expr, complain);
2682 : 128019514 : else if (bitfield_type && bitfield_type != type)
2683 : : /* Prevent decay_conversion from converting to bitfield_type. */
2684 : 159 : expr = build_nop (type, expr);
2685 : : return expr;
2686 : : }
2687 : :
2688 : : /* C version. */
2689 : :
2690 : : tree
2691 : 2005049 : perform_integral_promotions (tree expr)
2692 : : {
2693 : 2005049 : return cp_perform_integral_promotions (expr, tf_warning_or_error);
2694 : : }
2695 : :
2696 : : /* Returns nonzero iff exp is a STRING_CST or the result of applying
2697 : : decay_conversion to one. */
2698 : :
2699 : : int
2700 : 3085854 : string_conv_p (const_tree totype, const_tree exp, int warn)
2701 : : {
2702 : 3085854 : tree t;
2703 : :
2704 : 3085854 : if (!TYPE_PTR_P (totype))
2705 : : return 0;
2706 : :
2707 : 3085803 : t = TREE_TYPE (totype);
2708 : 3085803 : if (!same_type_p (t, char_type_node)
2709 : 2955000 : && !same_type_p (t, char8_type_node)
2710 : 2938615 : && !same_type_p (t, char16_type_node)
2711 : 2890462 : && !same_type_p (t, char32_type_node)
2712 : 5928113 : && !same_type_p (t, wchar_type_node))
2713 : : return 0;
2714 : :
2715 : 272377 : location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2716 : :
2717 : 272377 : STRIP_ANY_LOCATION_WRAPPER (exp);
2718 : :
2719 : 272377 : if (TREE_CODE (exp) == STRING_CST)
2720 : : {
2721 : : /* Make sure that we don't try to convert between char and wide chars. */
2722 : 108 : if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2723 : : return 0;
2724 : : }
2725 : : else
2726 : : {
2727 : : /* Is this a string constant which has decayed to 'const char *'? */
2728 : 272269 : t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2729 : 272269 : if (!same_type_p (TREE_TYPE (exp), t))
2730 : : return 0;
2731 : 13187 : STRIP_NOPS (exp);
2732 : 13187 : if (TREE_CODE (exp) != ADDR_EXPR
2733 : 13187 : || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2734 : : return 0;
2735 : : }
2736 : 306 : if (warn)
2737 : : {
2738 : 102 : if (cxx_dialect >= cxx11)
2739 : 87 : pedwarn (loc, OPT_Wwrite_strings,
2740 : : "ISO C++ forbids converting a string constant to %qT",
2741 : : totype);
2742 : : else
2743 : 15 : warning_at (loc, OPT_Wwrite_strings,
2744 : : "deprecated conversion from string constant to %qT",
2745 : : totype);
2746 : : }
2747 : :
2748 : : return 1;
2749 : : }
2750 : :
2751 : : /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2752 : : can, for example, use as an lvalue. This code used to be in
2753 : : unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2754 : : expressions, where we're dealing with aggregates. But now it's again only
2755 : : called from unary_complex_lvalue. The case (in particular) that led to
2756 : : this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2757 : : get it there. */
2758 : :
2759 : : static tree
2760 : 99399 : rationalize_conditional_expr (enum tree_code code, tree t,
2761 : : tsubst_flags_t complain)
2762 : : {
2763 : 99399 : location_t loc = cp_expr_loc_or_input_loc (t);
2764 : :
2765 : : /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2766 : : the first operand is always the one to be used if both operands
2767 : : are equal, so we know what conditional expression this used to be. */
2768 : 99399 : if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2769 : : {
2770 : 0 : tree op0 = TREE_OPERAND (t, 0);
2771 : 0 : tree op1 = TREE_OPERAND (t, 1);
2772 : :
2773 : : /* The following code is incorrect if either operand side-effects. */
2774 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (op0)
2775 : : && !TREE_SIDE_EFFECTS (op1));
2776 : 0 : return
2777 : 0 : build_conditional_expr (loc,
2778 : 0 : build_x_binary_op (loc,
2779 : 0 : (TREE_CODE (t) == MIN_EXPR
2780 : : ? LE_EXPR : GE_EXPR),
2781 : 0 : op0, TREE_CODE (op0),
2782 : 0 : op1, TREE_CODE (op1),
2783 : : NULL_TREE,
2784 : : /*overload=*/NULL,
2785 : : complain),
2786 : : cp_build_unary_op (code, op0, false, complain),
2787 : : cp_build_unary_op (code, op1, false, complain),
2788 : : complain);
2789 : : }
2790 : :
2791 : 99399 : tree op1 = TREE_OPERAND (t, 1);
2792 : 99399 : if (TREE_CODE (op1) != THROW_EXPR)
2793 : 99396 : op1 = cp_build_unary_op (code, op1, false, complain);
2794 : 99399 : tree op2 = TREE_OPERAND (t, 2);
2795 : 99399 : if (TREE_CODE (op2) != THROW_EXPR)
2796 : 99393 : op2 = cp_build_unary_op (code, op2, false, complain);
2797 : :
2798 : 99399 : return
2799 : 99399 : build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2800 : : }
2801 : :
2802 : : /* Given the TYPE of an anonymous union field inside T, return the
2803 : : FIELD_DECL for the field. If not found return NULL_TREE. Because
2804 : : anonymous unions can nest, we must also search all anonymous unions
2805 : : that are directly reachable. */
2806 : :
2807 : : tree
2808 : 1095091 : lookup_anon_field (tree, tree type)
2809 : : {
2810 : 1095091 : tree field;
2811 : :
2812 : 1095091 : type = TYPE_MAIN_VARIANT (type);
2813 : 1095091 : field = ANON_AGGR_TYPE_FIELD (type);
2814 : 1095091 : gcc_assert (field);
2815 : 1095091 : return field;
2816 : : }
2817 : :
2818 : : /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2819 : : expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2820 : : non-NULL, it indicates the path to the base used to name MEMBER.
2821 : : If PRESERVE_REFERENCE is true, the expression returned will have
2822 : : REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2823 : : returned will have the type referred to by the reference.
2824 : :
2825 : : This function does not perform access control; that is either done
2826 : : earlier by the parser when the name of MEMBER is resolved to MEMBER
2827 : : itself, or later when overload resolution selects one of the
2828 : : functions indicated by MEMBER. */
2829 : :
2830 : : tree
2831 : 102018913 : build_class_member_access_expr (cp_expr object, tree member,
2832 : : tree access_path, bool preserve_reference,
2833 : : tsubst_flags_t complain)
2834 : : {
2835 : 102018913 : tree object_type;
2836 : 102018913 : tree member_scope;
2837 : 102018913 : tree result = NULL_TREE;
2838 : 102018913 : tree using_decl = NULL_TREE;
2839 : :
2840 : 102018913 : if (error_operand_p (object) || error_operand_p (member))
2841 : 48 : return error_mark_node;
2842 : :
2843 : 102018865 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2844 : :
2845 : : /* [expr.ref]
2846 : :
2847 : : The type of the first expression shall be "class object" (of a
2848 : : complete type). */
2849 : 102018865 : object_type = TREE_TYPE (object);
2850 : 102018865 : if (!currently_open_class (object_type)
2851 : 102018865 : && !complete_type_or_maybe_complain (object_type, object, complain))
2852 : 0 : return error_mark_node;
2853 : 102018865 : if (!CLASS_TYPE_P (object_type))
2854 : : {
2855 : 0 : if (complain & tf_error)
2856 : : {
2857 : 0 : if (INDIRECT_TYPE_P (object_type)
2858 : 0 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
2859 : 0 : error ("request for member %qD in %qE, which is of pointer "
2860 : : "type %qT (maybe you meant to use %<->%> ?)",
2861 : : member, object.get_value (), object_type);
2862 : : else
2863 : 0 : error ("request for member %qD in %qE, which is of non-class "
2864 : : "type %qT", member, object.get_value (), object_type);
2865 : : }
2866 : 0 : return error_mark_node;
2867 : : }
2868 : :
2869 : : /* The standard does not seem to actually say that MEMBER must be a
2870 : : member of OBJECT_TYPE. However, that is clearly what is
2871 : : intended. */
2872 : 102018865 : if (DECL_P (member))
2873 : : {
2874 : 43530469 : member_scope = DECL_CLASS_CONTEXT (member);
2875 : 43530469 : if (!mark_used (member, complain) && !(complain & tf_error))
2876 : 0 : return error_mark_node;
2877 : :
2878 : 43530469 : if (TREE_UNAVAILABLE (member))
2879 : 30 : error_unavailable_use (member, NULL_TREE);
2880 : 43530439 : else if (TREE_DEPRECATED (member))
2881 : 30 : warn_deprecated_use (member, NULL_TREE);
2882 : : }
2883 : : else
2884 : 58488396 : member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2885 : : /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2886 : : presently be the anonymous union. Go outwards until we find a
2887 : : type related to OBJECT_TYPE. */
2888 : 103117717 : while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2889 : 104216976 : && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2890 : : object_type))
2891 : 1098852 : member_scope = TYPE_CONTEXT (member_scope);
2892 : 102018865 : if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2893 : : {
2894 : 3 : if (complain & tf_error)
2895 : : {
2896 : 3 : if (TREE_CODE (member) == FIELD_DECL)
2897 : 0 : error ("invalid use of non-static data member %qE", member);
2898 : : else
2899 : 3 : error ("%qD is not a member of %qT", member, object_type);
2900 : : }
2901 : 3 : return error_mark_node;
2902 : : }
2903 : :
2904 : : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2905 : : `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2906 : : in the front end; only _DECLs and _REFs are lvalues in the back end. */
2907 : 102018862 : if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2908 : : {
2909 : 38 : temp = cp_build_fold_indirect_ref (temp);
2910 : 38 : if (!lvalue_p (object) && lvalue_p (temp))
2911 : : /* Preserve rvalueness. */
2912 : 6 : temp = move (temp);
2913 : 38 : object = temp;
2914 : : }
2915 : :
2916 : : /* In [expr.ref], there is an explicit list of the valid choices for
2917 : : MEMBER. We check for each of those cases here. */
2918 : 102018862 : if (VAR_P (member))
2919 : : {
2920 : : /* A static data member. */
2921 : 94999 : result = member;
2922 : 94999 : mark_exp_read (object);
2923 : :
2924 : 94999 : if (tree wrap = maybe_get_tls_wrapper_call (result))
2925 : : /* Replace an evaluated use of the thread_local variable with
2926 : : a call to its wrapper. */
2927 : 102 : result = wrap;
2928 : :
2929 : : /* If OBJECT has side-effects, they are supposed to occur. */
2930 : 94999 : if (TREE_SIDE_EFFECTS (object))
2931 : 51 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2932 : : }
2933 : 101923863 : else if (TREE_CODE (member) == FIELD_DECL)
2934 : : {
2935 : : /* A non-static data member. */
2936 : 43435336 : bool null_object_p;
2937 : 43435336 : int type_quals;
2938 : 43435336 : tree member_type;
2939 : :
2940 : 43435336 : if (INDIRECT_REF_P (object))
2941 : 32946395 : null_object_p =
2942 : 32946395 : integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2943 : : else
2944 : : null_object_p = false;
2945 : :
2946 : : /* Convert OBJECT to the type of MEMBER. */
2947 : 43435336 : if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2948 : : TYPE_MAIN_VARIANT (member_scope)))
2949 : : {
2950 : 1811399 : tree binfo;
2951 : 1811399 : base_kind kind;
2952 : :
2953 : : /* We didn't complain above about a currently open class, but now we
2954 : : must: we don't know how to refer to a base member before layout is
2955 : : complete. But still don't complain in a template. */
2956 : 1811399 : if (!cp_unevaluated_operand
2957 : 1810906 : && !dependent_type_p (object_type)
2958 : 3323628 : && !complete_type_or_maybe_complain (object_type, object,
2959 : : complain))
2960 : 9 : return error_mark_node;
2961 : :
2962 : 2260150 : binfo = lookup_base (access_path ? access_path : object_type,
2963 : : member_scope, ba_unique, &kind, complain);
2964 : 1811396 : if (binfo == error_mark_node)
2965 : : return error_mark_node;
2966 : :
2967 : : /* It is invalid to try to get to a virtual base of a
2968 : : NULL object. The most common cause is invalid use of
2969 : : offsetof macro. */
2970 : 1811396 : if (null_object_p && kind == bk_via_virtual)
2971 : : {
2972 : 6 : if (complain & tf_error)
2973 : : {
2974 : 6 : error ("invalid access to non-static data member %qD in "
2975 : : "virtual base of NULL object", member);
2976 : : }
2977 : 6 : return error_mark_node;
2978 : : }
2979 : :
2980 : : /* Convert to the base. */
2981 : 1811390 : object = build_base_path (PLUS_EXPR, object, binfo,
2982 : : /*nonnull=*/1, complain);
2983 : : /* If we found the base successfully then we should be able
2984 : : to convert to it successfully. */
2985 : 1811390 : gcc_assert (object != error_mark_node || seen_error ());
2986 : : }
2987 : :
2988 : : /* If MEMBER is from an anonymous aggregate, we have converted
2989 : : OBJECT so that it refers to the class containing the
2990 : : anonymous union. Generate a reference to the anonymous union
2991 : : itself, and recur to find MEMBER. */
2992 : 86870654 : if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2993 : : /* When this code is called from build_field_call, the
2994 : : object already has the type of the anonymous union.
2995 : : That is because the COMPONENT_REF was already
2996 : : constructed, and was then disassembled before calling
2997 : : build_field_call. After the function-call code is
2998 : : cleaned up, this waste can be eliminated. */
2999 : 44530693 : && (!same_type_ignoring_top_level_qualifiers_p
3000 : 1095366 : (TREE_TYPE (object), DECL_CONTEXT (member))))
3001 : : {
3002 : 1094965 : tree anonymous_union;
3003 : :
3004 : 1094965 : anonymous_union = lookup_anon_field (TREE_TYPE (object),
3005 : 1094965 : DECL_CONTEXT (member));
3006 : 1094965 : object = build_class_member_access_expr (object,
3007 : : anonymous_union,
3008 : : /*access_path=*/NULL_TREE,
3009 : : preserve_reference,
3010 : : complain);
3011 : : }
3012 : :
3013 : : /* Compute the type of the field, as described in [expr.ref]. */
3014 : 43435327 : type_quals = TYPE_UNQUALIFIED;
3015 : 43435327 : member_type = TREE_TYPE (member);
3016 : 43435327 : if (!TYPE_REF_P (member_type))
3017 : : {
3018 : 42526031 : type_quals = (cp_type_quals (member_type)
3019 : 42526031 : | cp_type_quals (object_type));
3020 : :
3021 : : /* A field is const (volatile) if the enclosing object, or the
3022 : : field itself, is const (volatile). But, a mutable field is
3023 : : not const, even within a const object. */
3024 : 42526031 : if (DECL_MUTABLE_P (member))
3025 : 324807 : type_quals &= ~TYPE_QUAL_CONST;
3026 : 42526031 : member_type = cp_build_qualified_type (member_type, type_quals);
3027 : : }
3028 : :
3029 : 43435327 : result = build3_loc (input_location, COMPONENT_REF, member_type,
3030 : : object, member, NULL_TREE);
3031 : :
3032 : : /* Mark the expression const or volatile, as appropriate. Even
3033 : : though we've dealt with the type above, we still have to mark the
3034 : : expression itself. */
3035 : 43435327 : if (type_quals & TYPE_QUAL_CONST)
3036 : 13838788 : TREE_READONLY (result) = 1;
3037 : 43435327 : if (type_quals & TYPE_QUAL_VOLATILE)
3038 : 183132 : TREE_THIS_VOLATILE (result) = 1;
3039 : : }
3040 : 58488527 : else if (BASELINK_P (member))
3041 : : {
3042 : : /* The member is a (possibly overloaded) member function. */
3043 : 58488393 : tree functions;
3044 : 58488393 : tree type;
3045 : :
3046 : : /* If the MEMBER is exactly one static member function, then we
3047 : : know the type of the expression. Otherwise, we must wait
3048 : : until overload resolution has been performed. */
3049 : 58488393 : functions = BASELINK_FUNCTIONS (member);
3050 : 58488393 : if (TREE_CODE (functions) == OVERLOAD && OVL_SINGLE_P (functions))
3051 : 58488393 : functions = OVL_FIRST (functions);
3052 : 58488393 : if (TREE_CODE (functions) == FUNCTION_DECL
3053 : 58488393 : && DECL_STATIC_FUNCTION_P (functions))
3054 : 457873 : type = TREE_TYPE (functions);
3055 : : else
3056 : 58030520 : type = unknown_type_node;
3057 : : /* Note that we do not convert OBJECT to the BASELINK_BINFO
3058 : : base. That will happen when the function is called. */
3059 : 58488393 : result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3060 : : NULL_TREE);
3061 : : }
3062 : 134 : else if (TREE_CODE (member) == CONST_DECL)
3063 : : {
3064 : : /* The member is an enumerator. */
3065 : 110 : result = member;
3066 : : /* If OBJECT has side-effects, they are supposed to occur. */
3067 : 110 : if (TREE_SIDE_EFFECTS (object))
3068 : 6 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3069 : : object, result);
3070 : : }
3071 : 24 : else if ((using_decl = strip_using_decl (member)) != member)
3072 : 0 : result = build_class_member_access_expr (object,
3073 : : using_decl,
3074 : : access_path, preserve_reference,
3075 : : complain);
3076 : : else
3077 : : {
3078 : 24 : if (complain & tf_error)
3079 : 12 : error ("invalid use of %qD", member);
3080 : 24 : return error_mark_node;
3081 : : }
3082 : :
3083 : 102018829 : if (!preserve_reference)
3084 : : /* [expr.ref]
3085 : :
3086 : : If E2 is declared to have type "reference to T", then ... the
3087 : : type of E1.E2 is T. */
3088 : 96559991 : result = convert_from_reference (result);
3089 : :
3090 : : return result;
3091 : : }
3092 : :
3093 : : /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3094 : : SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3095 : :
3096 : : tree
3097 : 154054 : lookup_destructor (tree object, tree scope, tree dtor_name,
3098 : : tsubst_flags_t complain)
3099 : : {
3100 : 154054 : tree object_type = TREE_TYPE (object);
3101 : 154054 : tree dtor_type = TREE_OPERAND (dtor_name, 0);
3102 : 154054 : tree expr;
3103 : :
3104 : : /* We've already complained about this destructor. */
3105 : 154054 : if (dtor_type == error_mark_node)
3106 : : return error_mark_node;
3107 : :
3108 : 154048 : if (scope && !check_dtor_name (scope, dtor_type))
3109 : : {
3110 : 3 : if (complain & tf_error)
3111 : 3 : error ("qualified type %qT does not match destructor name ~%qT",
3112 : : scope, dtor_type);
3113 : 3 : return error_mark_node;
3114 : : }
3115 : 154045 : if (is_auto (dtor_type))
3116 : : dtor_type = object_type;
3117 : 154042 : else if (identifier_p (dtor_type))
3118 : : {
3119 : : /* In a template, names we can't find a match for are still accepted
3120 : : destructor names, and we check them here. */
3121 : 23 : if (check_dtor_name (object_type, dtor_type))
3122 : : dtor_type = object_type;
3123 : : else
3124 : : {
3125 : 3 : if (complain & tf_error)
3126 : 3 : error ("object type %qT does not match destructor name ~%qT",
3127 : : object_type, dtor_type);
3128 : 3 : return error_mark_node;
3129 : : }
3130 : :
3131 : : }
3132 : 154019 : else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3133 : : {
3134 : 6 : if (complain & tf_error)
3135 : 6 : error ("the type being destroyed is %qT, but the destructor "
3136 : 6 : "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3137 : 6 : return error_mark_node;
3138 : : }
3139 : 154036 : expr = lookup_member (dtor_type, complete_dtor_identifier,
3140 : : /*protect=*/1, /*want_type=*/false,
3141 : : tf_warning_or_error);
3142 : 154036 : if (!expr)
3143 : : {
3144 : 6 : if (complain & tf_error)
3145 : 6 : cxx_incomplete_type_error (dtor_name, dtor_type);
3146 : 6 : return error_mark_node;
3147 : : }
3148 : 154030 : expr = (adjust_result_of_qualified_name_lookup
3149 : 154030 : (expr, dtor_type, object_type));
3150 : 154030 : if (scope == NULL_TREE)
3151 : : /* We need to call adjust_result_of_qualified_name_lookup in case the
3152 : : destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3153 : : that we still get virtual function binding. */
3154 : 153887 : BASELINK_QUALIFIED_P (expr) = false;
3155 : : return expr;
3156 : : }
3157 : :
3158 : : /* An expression of the form "A::template B" has been resolved to
3159 : : DECL. Issue a diagnostic if B is not a template or template
3160 : : specialization. */
3161 : :
3162 : : void
3163 : 3063027 : check_template_keyword (tree decl)
3164 : : {
3165 : : /* The standard says:
3166 : :
3167 : : [temp.names]
3168 : :
3169 : : If a name prefixed by the keyword template is not a member
3170 : : template, the program is ill-formed.
3171 : :
3172 : : DR 228 removed the restriction that the template be a member
3173 : : template.
3174 : :
3175 : : DR 96, if accepted would add the further restriction that explicit
3176 : : template arguments must be provided if the template keyword is
3177 : : used, but, as of 2005-10-16, that DR is still in "drafting". If
3178 : : this DR is accepted, then the semantic checks here can be
3179 : : simplified, as the entity named must in fact be a template
3180 : : specialization, rather than, as at present, a set of overloaded
3181 : : functions containing at least one template function. */
3182 : 3063027 : if (TREE_CODE (decl) != TEMPLATE_DECL
3183 : 3063027 : && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3184 : : {
3185 : 2770107 : if (VAR_P (decl))
3186 : : {
3187 : 9777 : if (DECL_USE_TEMPLATE (decl)
3188 : 9777 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3189 : : ;
3190 : : else
3191 : 0 : permerror (input_location, "%qD is not a template", decl);
3192 : : }
3193 : 2760330 : else if (!is_overloaded_fn (decl))
3194 : 0 : permerror (input_location, "%qD is not a template", decl);
3195 : : else
3196 : : {
3197 : 2760330 : bool found = false;
3198 : :
3199 : 5520660 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3200 : 5520662 : !found && iter; ++iter)
3201 : : {
3202 : 2760332 : tree fn = *iter;
3203 : 2760332 : if (TREE_CODE (fn) == TEMPLATE_DECL
3204 : 2759936 : || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3205 : 2760346 : || (TREE_CODE (fn) == FUNCTION_DECL
3206 : 14 : && DECL_USE_TEMPLATE (fn)
3207 : 6 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3208 : : found = true;
3209 : : }
3210 : 2760330 : if (!found)
3211 : 12 : permerror (input_location, "%qD is not a template", decl);
3212 : : }
3213 : : }
3214 : 3063027 : }
3215 : :
3216 : : /* Record that an access failure occurred on BASETYPE_PATH attempting
3217 : : to access DECL, where DIAG_DECL should be used for diagnostics. */
3218 : :
3219 : : void
3220 : 406 : access_failure_info::record_access_failure (tree basetype_path,
3221 : : tree decl, tree diag_decl)
3222 : : {
3223 : 406 : m_was_inaccessible = true;
3224 : 406 : m_basetype_path = basetype_path;
3225 : 406 : m_decl = decl;
3226 : 406 : m_diag_decl = diag_decl;
3227 : 406 : }
3228 : :
3229 : : /* If an access failure was recorded, then attempt to locate an
3230 : : accessor function for the pertinent field.
3231 : : Otherwise, return NULL_TREE. */
3232 : :
3233 : : tree
3234 : 85156134 : access_failure_info::get_any_accessor (bool const_p) const
3235 : : {
3236 : 85156134 : if (!was_inaccessible_p ())
3237 : : return NULL_TREE;
3238 : :
3239 : 406 : tree accessor
3240 : 406 : = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3241 : 406 : if (!accessor)
3242 : : return NULL_TREE;
3243 : :
3244 : : /* The accessor must itself be accessible for it to be a reasonable
3245 : : suggestion. */
3246 : 161 : if (!accessible_p (m_basetype_path, accessor, true))
3247 : : return NULL_TREE;
3248 : :
3249 : : return accessor;
3250 : : }
3251 : :
3252 : : /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3253 : : replacing the primary location in RICHLOC with "accessor()". */
3254 : :
3255 : : void
3256 : 140 : access_failure_info::add_fixit_hint (rich_location *richloc,
3257 : : tree accessor_decl)
3258 : : {
3259 : 140 : pretty_printer pp;
3260 : 140 : pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3261 : 140 : pp_string (&pp, "()");
3262 : 140 : richloc->add_fixit_replace (pp_formatted_text (&pp));
3263 : 140 : }
3264 : :
3265 : : /* If an access failure was recorded, then attempt to locate an
3266 : : accessor function for the pertinent field, and if one is
3267 : : available, add a note and fix-it hint suggesting using it. */
3268 : :
3269 : : void
3270 : 85156064 : access_failure_info::maybe_suggest_accessor (bool const_p) const
3271 : : {
3272 : 85156064 : tree accessor = get_any_accessor (const_p);
3273 : 85156064 : if (accessor == NULL_TREE)
3274 : 85155966 : return;
3275 : 98 : rich_location richloc (line_table, input_location);
3276 : 98 : add_fixit_hint (&richloc, accessor);
3277 : 98 : inform (&richloc, "field %q#D can be accessed via %q#D",
3278 : 98 : m_diag_decl, accessor);
3279 : 98 : }
3280 : :
3281 : : /* Subroutine of finish_class_member_access_expr.
3282 : : Issue an error about NAME not being a member of ACCESS_PATH (or
3283 : : OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3284 : : names. */
3285 : :
3286 : : static void
3287 : 302 : complain_about_unrecognized_member (tree access_path, tree name,
3288 : : tree object_type)
3289 : : {
3290 : : /* Attempt to provide a hint about misspelled names. */
3291 : 302 : tree guessed_id = lookup_member_fuzzy (access_path, name,
3292 : : /*want_type=*/false);
3293 : 302 : if (guessed_id == NULL_TREE)
3294 : : {
3295 : : /* No hint. */
3296 : 192 : error ("%q#T has no member named %qE",
3297 : 192 : TREE_CODE (access_path) == TREE_BINFO
3298 : 6 : ? TREE_TYPE (access_path) : object_type, name);
3299 : 192 : return;
3300 : : }
3301 : :
3302 : 110 : location_t bogus_component_loc = input_location;
3303 : 110 : gcc_rich_location rich_loc (bogus_component_loc);
3304 : :
3305 : : /* Check that the guessed name is accessible along access_path. */
3306 : 110 : access_failure_info afi;
3307 : 110 : lookup_member (access_path, guessed_id, /*protect=*/1,
3308 : : /*want_type=*/false, /*complain=*/false,
3309 : : &afi);
3310 : 110 : if (afi.was_inaccessible_p ())
3311 : : {
3312 : 70 : tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3313 : 70 : if (accessor)
3314 : : {
3315 : : /* The guessed name isn't directly accessible, but can be accessed
3316 : : via an accessor member function. */
3317 : 42 : afi.add_fixit_hint (&rich_loc, accessor);
3318 : 42 : error_at (&rich_loc,
3319 : : "%q#T has no member named %qE;"
3320 : : " did you mean %q#D? (accessible via %q#D)",
3321 : 42 : TREE_CODE (access_path) == TREE_BINFO
3322 : 0 : ? TREE_TYPE (access_path) : object_type,
3323 : : name, afi.get_diag_decl (), accessor);
3324 : : }
3325 : : else
3326 : : {
3327 : : /* The guessed name isn't directly accessible, and no accessor
3328 : : member function could be found. */
3329 : 28 : auto_diagnostic_group d;
3330 : 28 : error_at (&rich_loc,
3331 : : "%q#T has no member named %qE;"
3332 : : " did you mean %q#D? (not accessible from this context)",
3333 : 28 : TREE_CODE (access_path) == TREE_BINFO
3334 : 0 : ? TREE_TYPE (access_path) : object_type,
3335 : : name, afi.get_diag_decl ());
3336 : 28 : complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3337 : : afi.get_diag_decl (), false, ak_none);
3338 : 28 : }
3339 : : }
3340 : : else
3341 : : {
3342 : : /* The guessed name is directly accessible; suggest it. */
3343 : 40 : rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3344 : : guessed_id);
3345 : 40 : error_at (&rich_loc,
3346 : : "%q#T has no member named %qE;"
3347 : : " did you mean %qE?",
3348 : 40 : TREE_CODE (access_path) == TREE_BINFO
3349 : 0 : ? TREE_TYPE (access_path) : object_type,
3350 : : name, guessed_id);
3351 : : }
3352 : 110 : }
3353 : :
3354 : : /* This function is called by the parser to process a class member
3355 : : access expression of the form OBJECT.NAME. NAME is a node used by
3356 : : the parser to represent a name; it is not yet a DECL. It may,
3357 : : however, be a BASELINK where the BASELINK_FUNCTIONS is a
3358 : : TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3359 : : there is no reason to do the lookup twice, so the parser keeps the
3360 : : BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3361 : : be a template via the use of the "A::template B" syntax. */
3362 : :
3363 : : tree
3364 : 166928188 : finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3365 : : tsubst_flags_t complain)
3366 : : {
3367 : 166928188 : tree expr;
3368 : 166928188 : tree object_type;
3369 : 166928188 : tree member;
3370 : 166928188 : tree access_path = NULL_TREE;
3371 : 166928188 : tree orig_object = object;
3372 : 166928188 : tree orig_name = name;
3373 : :
3374 : 166928188 : if (object == error_mark_node || name == error_mark_node)
3375 : : return error_mark_node;
3376 : :
3377 : : /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3378 : 166927759 : if (!objc_is_public (object, name))
3379 : 0 : return error_mark_node;
3380 : :
3381 : 166927759 : object_type = TREE_TYPE (object);
3382 : :
3383 : 166927759 : if (processing_template_decl)
3384 : : {
3385 : 139093221 : if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3386 : 139093221 : type_dependent_object_expression_p (object)
3387 : : /* If NAME is "f<args>", where either 'f' or 'args' is
3388 : : dependent, then the expression is dependent. */
3389 : 66928290 : || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3390 : 614425 : && dependent_template_id_p (TREE_OPERAND (name, 0),
3391 : 614425 : TREE_OPERAND (name, 1)))
3392 : : /* If NAME is "T::X" where "T" is dependent, then the
3393 : : expression is dependent. */
3394 : 66619811 : || (TREE_CODE (name) == SCOPE_REF
3395 : 109089 : && TYPE_P (TREE_OPERAND (name, 0))
3396 : 109089 : && dependent_scope_p (TREE_OPERAND (name, 0)))
3397 : : /* If NAME is operator T where "T" is dependent, we can't
3398 : : lookup until we instantiate the T. */
3399 : 205604157 : || (TREE_CODE (name) == IDENTIFIER_NODE
3400 : 65933512 : && IDENTIFIER_CONV_OP_P (name)
3401 : 41 : && dependent_type_p (TREE_TYPE (name))))
3402 : : {
3403 : 86081573 : dependent:
3404 : 86081573 : return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3405 : 86081573 : orig_object, orig_name, NULL_TREE);
3406 : : }
3407 : : }
3408 : 27834538 : else if (c_dialect_objc ()
3409 : 94345436 : && identifier_p (name)
3410 : 27834538 : && (expr = objc_maybe_build_component_ref (object, name)))
3411 : : return expr;
3412 : :
3413 : : /* [expr.ref]
3414 : :
3415 : : The type of the first expression shall be "class object" (of a
3416 : : complete type). */
3417 : 94345436 : if (!currently_open_class (object_type)
3418 : 94345436 : && !complete_type_or_maybe_complain (object_type, object, complain))
3419 : 86 : return error_mark_node;
3420 : 94345350 : if (!CLASS_TYPE_P (object_type))
3421 : : {
3422 : 96290 : if (complain & tf_error)
3423 : : {
3424 : 106 : if (INDIRECT_TYPE_P (object_type)
3425 : 106 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
3426 : 21 : error ("request for member %qD in %qE, which is of pointer "
3427 : : "type %qT (maybe you meant to use %<->%> ?)",
3428 : : name, object.get_value (), object_type);
3429 : : else
3430 : 85 : error ("request for member %qD in %qE, which is of non-class "
3431 : : "type %qT", name, object.get_value (), object_type);
3432 : : }
3433 : 96290 : return error_mark_node;
3434 : : }
3435 : :
3436 : 94249060 : if (BASELINK_P (name))
3437 : : /* A member function that has already been looked up. */
3438 : : member = name;
3439 : : else
3440 : : {
3441 : 85344025 : bool is_template_id = false;
3442 : 85344025 : tree template_args = NULL_TREE;
3443 : 85344025 : tree scope = NULL_TREE;
3444 : :
3445 : 85344025 : access_path = object_type;
3446 : :
3447 : 85344025 : if (TREE_CODE (name) == SCOPE_REF)
3448 : : {
3449 : : /* A qualified name. The qualifying class or namespace `S'
3450 : : has already been looked up; it is either a TYPE or a
3451 : : NAMESPACE_DECL. */
3452 : 1760 : scope = TREE_OPERAND (name, 0);
3453 : 1760 : name = TREE_OPERAND (name, 1);
3454 : :
3455 : : /* If SCOPE is a namespace, then the qualified name does not
3456 : : name a member of OBJECT_TYPE. */
3457 : 1760 : if (TREE_CODE (scope) == NAMESPACE_DECL)
3458 : : {
3459 : 0 : if (complain & tf_error)
3460 : 0 : error ("%<%D::%D%> is not a member of %qT",
3461 : : scope, name, object_type);
3462 : 0 : return error_mark_node;
3463 : : }
3464 : : }
3465 : :
3466 : 85344025 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3467 : : {
3468 : 464434 : is_template_id = true;
3469 : 464434 : template_args = TREE_OPERAND (name, 1);
3470 : 464434 : name = TREE_OPERAND (name, 0);
3471 : :
3472 : 885766 : if (!identifier_p (name))
3473 : 421350 : name = OVL_NAME (name);
3474 : : }
3475 : :
3476 : 85344025 : if (scope)
3477 : : {
3478 : 1760 : if (TREE_CODE (scope) == ENUMERAL_TYPE)
3479 : : {
3480 : 12 : gcc_assert (!is_template_id);
3481 : : /* Looking up a member enumerator (c++/56793). */
3482 : 24 : if (!TYPE_CLASS_SCOPE_P (scope)
3483 : 21 : || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3484 : : {
3485 : 3 : if (complain & tf_error)
3486 : 3 : error ("%<%D::%D%> is not a member of %qT",
3487 : : scope, name, object_type);
3488 : 3 : return error_mark_node;
3489 : : }
3490 : 9 : tree val = lookup_enumerator (scope, name);
3491 : 9 : if (!val)
3492 : : {
3493 : 6 : if (complain & tf_error)
3494 : 6 : error ("%qD is not a member of %qD",
3495 : : name, scope);
3496 : 6 : return error_mark_node;
3497 : : }
3498 : :
3499 : 3 : if (TREE_SIDE_EFFECTS (object))
3500 : 0 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3501 : 3 : return val;
3502 : : }
3503 : :
3504 : 1748 : gcc_assert (CLASS_TYPE_P (scope));
3505 : 1748 : gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3506 : :
3507 : 1748 : if (constructor_name_p (name, scope))
3508 : : {
3509 : 3 : if (complain & tf_error)
3510 : 3 : error ("cannot call constructor %<%T::%D%> directly",
3511 : : scope, name);
3512 : 3 : return error_mark_node;
3513 : : }
3514 : :
3515 : : /* NAME may refer to a static data member, in which case there is
3516 : : one copy of the data member that is shared by all the objects of
3517 : : the class. So NAME can be unambiguously referred to even if
3518 : : there are multiple indirect base classes containing NAME. */
3519 : 5235 : const base_access ba = [scope, name] ()
3520 : : {
3521 : 1745 : if (identifier_p (name))
3522 : : {
3523 : 1608 : tree m = lookup_member (scope, name, /*protect=*/0,
3524 : : /*want_type=*/false, tf_none);
3525 : 1608 : if (!m || shared_member_p (m))
3526 : 32 : return ba_any;
3527 : : }
3528 : : return ba_check;
3529 : 1745 : } ();
3530 : :
3531 : : /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3532 : 1745 : access_path = lookup_base (object_type, scope, ba, NULL, complain);
3533 : 1745 : if (access_path == error_mark_node)
3534 : : return error_mark_node;
3535 : 1733 : if (!access_path)
3536 : : {
3537 : 12 : if (any_dependent_bases_p (object_type))
3538 : 6 : goto dependent;
3539 : 6 : if (complain & tf_error)
3540 : 6 : error ("%qT is not a base of %qT", scope, object_type);
3541 : 6 : return error_mark_node;
3542 : : }
3543 : : }
3544 : :
3545 : 85343986 : if (TREE_CODE (name) == BIT_NOT_EXPR)
3546 : : {
3547 : 187922 : if (dependent_type_p (object_type))
3548 : : /* The destructor isn't declared yet. */
3549 : 33883 : goto dependent;
3550 : 154039 : member = lookup_destructor (object, scope, name, complain);
3551 : : }
3552 : : else
3553 : : {
3554 : : /* Look up the member. */
3555 : 85156064 : {
3556 : 85156064 : auto_diagnostic_group d;
3557 : 85156064 : access_failure_info afi;
3558 : 85156064 : if (processing_template_decl)
3559 : : /* Even though this class member access expression is at this
3560 : : point not dependent, the member itself may be dependent, and
3561 : : we must not potentially push a access check for a dependent
3562 : : member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3563 : : ahead of time here; we're going to redo this member lookup at
3564 : : instantiation time anyway. */
3565 : 66239610 : push_deferring_access_checks (dk_no_check);
3566 : 85156064 : member = lookup_member (access_path, name, /*protect=*/1,
3567 : : /*want_type=*/false, complain,
3568 : : &afi);
3569 : 85156064 : if (processing_template_decl)
3570 : 66239610 : pop_deferring_access_checks ();
3571 : 85156064 : afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3572 : 85156064 : }
3573 : 85156064 : if (member == NULL_TREE)
3574 : : {
3575 : 8151845 : if (dependentish_scope_p (object_type))
3576 : : /* Try again at instantiation time. */
3577 : 8129560 : goto dependent;
3578 : 22285 : if (complain & tf_error)
3579 : 302 : complain_about_unrecognized_member (access_path, name,
3580 : : object_type);
3581 : 22285 : return error_mark_node;
3582 : : }
3583 : 77004219 : if (member == error_mark_node)
3584 : : return error_mark_node;
3585 : 77004175 : if (DECL_P (member)
3586 : 77004175 : && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3587 : : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3588 : : wrong, so don't use it. */
3589 : 6 : goto dependent;
3590 : 77004169 : if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3591 : 5335795 : goto dependent;
3592 : : }
3593 : :
3594 : 71822413 : if (is_template_id)
3595 : : {
3596 : 464424 : tree templ = member;
3597 : :
3598 : 464424 : if (BASELINK_P (templ))
3599 : 464400 : member = lookup_template_function (templ, template_args);
3600 : 24 : else if (variable_template_p (templ))
3601 : 24 : member = (lookup_and_finish_template_variable
3602 : 24 : (templ, template_args, complain));
3603 : : else
3604 : : {
3605 : 0 : if (complain & tf_error)
3606 : 0 : error ("%qD is not a member template function", name);
3607 : 0 : return error_mark_node;
3608 : : }
3609 : : }
3610 : : }
3611 : :
3612 : 80727448 : if (TREE_UNAVAILABLE (member))
3613 : 30 : error_unavailable_use (member, NULL_TREE);
3614 : 80727418 : else if (TREE_DEPRECATED (member))
3615 : 30 : warn_deprecated_use (member, NULL_TREE);
3616 : :
3617 : 80727448 : if (template_p)
3618 : 9740 : check_template_keyword (member);
3619 : :
3620 : 80727448 : expr = build_class_member_access_expr (object, member, access_path,
3621 : : /*preserve_reference=*/false,
3622 : : complain);
3623 : 80727448 : if (processing_template_decl && expr != error_mark_node)
3624 : : {
3625 : 53011572 : if (BASELINK_P (member))
3626 : : {
3627 : 39313360 : if (TREE_CODE (orig_name) == SCOPE_REF)
3628 : 169 : BASELINK_QUALIFIED_P (member) = 1;
3629 : : orig_name = member;
3630 : : }
3631 : 53011572 : return build_min_non_dep (COMPONENT_REF, expr,
3632 : : orig_object, orig_name,
3633 : 53011572 : NULL_TREE);
3634 : : }
3635 : :
3636 : : return expr;
3637 : : }
3638 : :
3639 : : /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3640 : : type. */
3641 : :
3642 : : tree
3643 : 177244 : build_simple_component_ref (tree object, tree member)
3644 : : {
3645 : 177244 : tree type = cp_build_qualified_type (TREE_TYPE (member),
3646 : 177244 : cp_type_quals (TREE_TYPE (object)));
3647 : 177244 : return build3_loc (input_location,
3648 : : COMPONENT_REF, type,
3649 : 177244 : object, member, NULL_TREE);
3650 : : }
3651 : :
3652 : : /* Return an expression for the MEMBER_NAME field in the internal
3653 : : representation of PTRMEM, a pointer-to-member function. (Each
3654 : : pointer-to-member function type gets its own RECORD_TYPE so it is
3655 : : more convenient to access the fields by name than by FIELD_DECL.)
3656 : : This routine converts the NAME to a FIELD_DECL and then creates the
3657 : : node for the complete expression. */
3658 : :
3659 : : tree
3660 : 177208 : build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3661 : : {
3662 : 177208 : tree ptrmem_type;
3663 : 177208 : tree member;
3664 : :
3665 : 177208 : if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3666 : : {
3667 : 180 : for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3668 : 72 : if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3669 : 54 : return e.value;
3670 : 0 : gcc_unreachable ();
3671 : : }
3672 : :
3673 : : /* This code is a stripped down version of
3674 : : build_class_member_access_expr. It does not work to use that
3675 : : routine directly because it expects the object to be of class
3676 : : type. */
3677 : 177154 : ptrmem_type = TREE_TYPE (ptrmem);
3678 : 177154 : gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3679 : 265634 : for (member = TYPE_FIELDS (ptrmem_type); member;
3680 : 88480 : member = DECL_CHAIN (member))
3681 : 265634 : if (DECL_NAME (member) == member_name)
3682 : : break;
3683 : 177154 : return build_simple_component_ref (ptrmem, member);
3684 : : }
3685 : :
3686 : : /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3687 : : and for any other relevant operator. */
3688 : :
3689 : : static tree
3690 : 115624963 : op_unqualified_lookup (tree_code code, bool is_assign)
3691 : : {
3692 : 115624963 : tree lookups = NULL_TREE;
3693 : :
3694 : 115624963 : if (cxx_dialect >= cxx20 && !is_assign)
3695 : : {
3696 : 32951107 : if (code == NE_EXPR)
3697 : : {
3698 : : /* != can get rewritten in terms of ==. */
3699 : 2187756 : tree fnname = ovl_op_identifier (false, EQ_EXPR);
3700 : 2187756 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3701 : 2172738 : lookups = tree_cons (fnname, fns, lookups);
3702 : : }
3703 : 30763351 : else if (code == GT_EXPR || code == LE_EXPR
3704 : 30763351 : || code == LT_EXPR || code == GE_EXPR)
3705 : : {
3706 : : /* These can get rewritten in terms of <=>. */
3707 : 2850221 : tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3708 : 2850221 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3709 : 2715128 : lookups = tree_cons (fnname, fns, lookups);
3710 : : }
3711 : : }
3712 : :
3713 : 115624963 : tree fnname = ovl_op_identifier (is_assign, code);
3714 : 115624963 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3715 : 64980157 : lookups = tree_cons (fnname, fns, lookups);
3716 : :
3717 : 115624963 : if (lookups)
3718 : : return lookups;
3719 : : else
3720 : 50562073 : return build_tree_list (NULL_TREE, NULL_TREE);
3721 : : }
3722 : :
3723 : : /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3724 : : the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3725 : : name lookup for the given operator. */
3726 : :
3727 : : tree
3728 : 124771345 : build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3729 : : {
3730 : 124771345 : if (lookups)
3731 : : /* We're partially instantiating a dependent operator expression, and
3732 : : LOOKUPS is the result of phase 1 name lookup that we performed
3733 : : earlier at template definition time, so just reuse the corresponding
3734 : : DEPENDENT_OPERATOR_TYPE. */
3735 : 9146382 : return TREE_TYPE (lookups);
3736 : :
3737 : : /* Otherwise we're processing a dependent operator expression at template
3738 : : definition time, so perform phase 1 name lookup now. */
3739 : 115624963 : lookups = op_unqualified_lookup (code, is_assign);
3740 : :
3741 : 115624963 : tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3742 : 115624963 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3743 : 115624963 : TREE_TYPE (lookups) = type;
3744 : 115624963 : return type;
3745 : : }
3746 : :
3747 : : /* Given an expression PTR for a pointer, return an expression
3748 : : for the value pointed to.
3749 : : ERRORSTRING is the name of the operator to appear in error messages.
3750 : :
3751 : : This function may need to overload OPERATOR_FNNAME.
3752 : : Must also handle REFERENCE_TYPEs for C++. */
3753 : :
3754 : : tree
3755 : 39061851 : build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3756 : : tree lookups, tsubst_flags_t complain)
3757 : : {
3758 : 39061851 : tree orig_expr = expr;
3759 : 39061851 : tree rval;
3760 : 39061851 : tree overload = NULL_TREE;
3761 : :
3762 : 39061851 : if (processing_template_decl)
3763 : : {
3764 : : /* Retain the type if we know the operand is a pointer. */
3765 : 22173575 : if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3766 : : {
3767 : 12207013 : if (expr == current_class_ptr
3768 : 12207013 : || (TREE_CODE (expr) == NOP_EXPR
3769 : 7640421 : && TREE_OPERAND (expr, 0) == current_class_ptr
3770 : 7630734 : && (same_type_ignoring_top_level_qualifiers_p
3771 : 7630734 : (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3772 : 7630733 : return current_class_ref;
3773 : 4576280 : return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3774 : : }
3775 : 9966562 : if (type_dependent_expression_p (expr))
3776 : : {
3777 : 9823408 : expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3778 : 9823408 : TREE_TYPE (expr)
3779 : 9823408 : = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3780 : 9823408 : return expr;
3781 : : }
3782 : : }
3783 : :
3784 : 17031430 : rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3785 : : NULL_TREE, NULL_TREE, lookups,
3786 : : &overload, complain);
3787 : 17031430 : if (!rval)
3788 : 0 : rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3789 : :
3790 : 17031430 : if (processing_template_decl && rval != error_mark_node)
3791 : : {
3792 : 141321 : if (overload != NULL_TREE)
3793 : 141290 : return (build_min_non_dep_op_overload
3794 : 141290 : (INDIRECT_REF, rval, overload, orig_expr));
3795 : :
3796 : 31 : return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3797 : : }
3798 : : else
3799 : : return rval;
3800 : : }
3801 : :
3802 : : /* Like c-family strict_aliasing_warning, but don't warn for dependent
3803 : : types or expressions. */
3804 : :
3805 : : static bool
3806 : 533286 : cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3807 : : {
3808 : 533286 : if (processing_template_decl)
3809 : : {
3810 : 39144 : tree e = expr;
3811 : 39144 : STRIP_NOPS (e);
3812 : 39144 : if (dependent_type_p (type) || type_dependent_expression_p (e))
3813 : 6712 : return false;
3814 : : }
3815 : 526574 : return strict_aliasing_warning (loc, type, expr);
3816 : : }
3817 : :
3818 : : /* The implementation of the above, and of indirection implied by other
3819 : : constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3820 : :
3821 : : static tree
3822 : 294597192 : cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3823 : : tsubst_flags_t complain, bool do_fold)
3824 : : {
3825 : 294597192 : tree pointer, type;
3826 : :
3827 : : /* RO_NULL should only be used with the folding entry points below, not
3828 : : cp_build_indirect_ref. */
3829 : 294597192 : gcc_checking_assert (errorstring != RO_NULL || do_fold);
3830 : :
3831 : 294597192 : if (ptr == current_class_ptr
3832 : 294597192 : || (TREE_CODE (ptr) == NOP_EXPR
3833 : 29598942 : && TREE_OPERAND (ptr, 0) == current_class_ptr
3834 : 16771560 : && (same_type_ignoring_top_level_qualifiers_p
3835 : 16771560 : (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3836 : 18148879 : return current_class_ref;
3837 : :
3838 : 276448313 : pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3839 : 276448313 : ? ptr : decay_conversion (ptr, complain));
3840 : 276448313 : if (pointer == error_mark_node)
3841 : : return error_mark_node;
3842 : :
3843 : 276442467 : type = TREE_TYPE (pointer);
3844 : :
3845 : 276442467 : if (INDIRECT_TYPE_P (type))
3846 : : {
3847 : : /* [expr.unary.op]
3848 : :
3849 : : If the type of the expression is "pointer to T," the type
3850 : : of the result is "T." */
3851 : 276442157 : tree t = TREE_TYPE (type);
3852 : :
3853 : 262477492 : if ((CONVERT_EXPR_P (ptr)
3854 : 197695106 : || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3855 : 343077851 : && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3856 : : {
3857 : : /* If a warning is issued, mark it to avoid duplicates from
3858 : : the backend. This only needs to be done at
3859 : : warn_strict_aliasing > 2. */
3860 : 56894619 : if (warn_strict_aliasing > 2
3861 : 57351212 : && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3862 : 456593 : type, TREE_OPERAND (ptr, 0)))
3863 : 6 : suppress_warning (ptr, OPT_Wstrict_aliasing);
3864 : : }
3865 : :
3866 : 276442157 : if (VOID_TYPE_P (t))
3867 : : {
3868 : : /* A pointer to incomplete type (other than cv void) can be
3869 : : dereferenced [expr.unary.op]/1 */
3870 : 70 : if (complain & tf_error)
3871 : 19 : error_at (loc, "%qT is not a pointer-to-object type", type);
3872 : 70 : return error_mark_node;
3873 : : }
3874 : 267647102 : else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3875 : 281948195 : && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3876 : : /* The POINTER was something like `&x'. We simplify `*&x' to
3877 : : `x'. This can change the value category: '*&TARGET_EXPR'
3878 : : is an lvalue and folding it into 'TARGET_EXPR' turns it into
3879 : : a prvalue of class type. */
3880 : 5506108 : return TREE_OPERAND (pointer, 0);
3881 : : else
3882 : : {
3883 : 270935979 : 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 : 270935979 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3889 : 270935979 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3890 : 270935979 : TREE_SIDE_EFFECTS (ref)
3891 : 270935979 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3892 : 270935979 : return ref;
3893 : : }
3894 : : }
3895 : 310 : 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 : 310 : return error_mark_node;
3925 : : }
3926 : :
3927 : : /* Entry point used by c-common, which expects folding. */
3928 : :
3929 : : tree
3930 : 20322 : build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3931 : : {
3932 : 20322 : return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3933 : 20322 : 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 : 270895583 : cp_build_fold_indirect_ref (tree pointer)
3941 : : {
3942 : 270895583 : return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3943 : 270895583 : 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 : 23681287 : cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3951 : : tsubst_flags_t complain)
3952 : : {
3953 : 23681287 : 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 : 5198634 : cp_build_array_ref (location_t loc, tree array, tree idx,
3973 : : tsubst_flags_t complain)
3974 : : {
3975 : 5198634 : tree first = NULL_TREE;
3976 : 5198634 : tree ret;
3977 : :
3978 : 5198634 : 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 : 5198634 : if (TREE_TYPE (array) == error_mark_node
3986 : 5198634 : || 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 : 5198634 : switch (TREE_CODE (array))
3992 : : {
3993 : 28 : case COMPOUND_EXPR:
3994 : 28 : {
3995 : 28 : tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3996 : : complain);
3997 : 28 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3998 : 28 : TREE_OPERAND (array, 0), value);
3999 : 28 : SET_EXPR_LOCATION (ret, loc);
4000 : 28 : 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 : 5198593 : default:
4015 : 5198593 : break;
4016 : : }
4017 : :
4018 : 5198593 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
4019 : :
4020 : : /* 0[array] */
4021 : 5198593 : 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 : 5198593 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4029 : : {
4030 : 1653728 : tree rval, type;
4031 : :
4032 : 1653728 : warn_array_subscript_with_type_char (loc, idx);
4033 : :
4034 : 1653728 : 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 : 1653725 : idx = cp_perform_integral_promotions (idx, complain);
4047 : :
4048 : 1653725 : 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 : 1653725 : if (TREE_CODE (idx) != INTEGER_CST
4055 : 1653725 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4056 : 821732 : && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4057 : : != INTEGER_CST)))
4058 : : {
4059 : 833244 : 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 : 1653725 : if (TREE_CODE (idx) == INTEGER_CST
4068 : 821735 : && TYPE_DOMAIN (TREE_TYPE (array))
4069 : 2474083 : && ! 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 : 1653725 : if (extra_warnings)
4079 : : {
4080 : 37439 : tree foo = array;
4081 : 57436 : while (TREE_CODE (foo) == COMPONENT_REF)
4082 : 19997 : foo = TREE_OPERAND (foo, 0);
4083 : 8 : if (VAR_P (foo) && DECL_REGISTER (foo)
4084 : 37439 : && (complain & tf_warning))
4085 : 0 : warning_at (loc, OPT_Wextra,
4086 : : "subscripting array declared %<register%>");
4087 : : }
4088 : :
4089 : 1653725 : type = TREE_TYPE (TREE_TYPE (array));
4090 : 1653725 : 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 : 4961175 : TREE_READONLY (rval)
4094 : 1653725 : |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4095 : 4961175 : TREE_SIDE_EFFECTS (rval)
4096 : 1653725 : |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4097 : 3307450 : TREE_THIS_VOLATILE (rval)
4098 : 1653725 : |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4099 : 1653725 : ret = require_complete_type (rval, complain);
4100 : 1653725 : protected_set_expr_location (ret, loc);
4101 : 1653725 : if (non_lvalue)
4102 : 6051 : ret = non_lvalue_loc (loc, ret);
4103 : 1653725 : if (first)
4104 : 6 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4105 : 1653725 : return ret;
4106 : : }
4107 : :
4108 : 3544865 : {
4109 : 3544865 : tree ar = cp_default_conversion (array, complain);
4110 : 3544865 : tree ind = cp_default_conversion (idx, complain);
4111 : :
4112 : 3544865 : if (!processing_template_decl
4113 : 3544865 : && !first && flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
4114 : 96916 : ar = first = save_expr (ar);
4115 : :
4116 : : /* Put the integer in IND to simplify error checking. */
4117 : 3544865 : if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4118 : 57 : std::swap (ar, ind);
4119 : :
4120 : 3544865 : if (ar == error_mark_node || ind == error_mark_node)
4121 : : return error_mark_node;
4122 : :
4123 : 3544865 : 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 : 3544843 : 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 : 3544843 : warn_array_subscript_with_type_char (loc, idx);
4137 : :
4138 : 3544843 : ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4139 : 3544843 : if (first)
4140 : 96916 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4141 : 3544843 : ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4142 : 3544843 : protected_set_expr_location (ret, loc);
4143 : 3544843 : 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 : 3506122 : build_array_ref (location_t loc, tree array, tree idx)
4153 : : {
4154 : 3506122 : 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 : 88404 : get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4170 : : tsubst_flags_t complain)
4171 : : {
4172 : 88404 : if (TREE_CODE (function) == OFFSET_REF)
4173 : 0 : function = TREE_OPERAND (function, 1);
4174 : :
4175 : 88404 : if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4176 : : {
4177 : 88404 : tree idx, delta, e1, e2, e3, vtbl;
4178 : 88404 : bool nonvirtual;
4179 : 88404 : tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4180 : 88404 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4181 : :
4182 : 88404 : tree instance_ptr = *instance_ptrptr;
4183 : 88404 : tree instance_save_expr = 0;
4184 : 88404 : 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 : 88404 : nonvirtual = (COMPLETE_TYPE_P (basetype)
4206 : 88362 : && !TYPE_POLYMORPHIC_P (basetype)
4207 : 118742 : && 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 : 88142 : 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 : 88404 : if (TREE_SIDE_EFFECTS (instance_ptr)
4222 : 88404 : || (!nonvirtual
4223 : 49314 : && !DECL_P (instance_ptr)
4224 : 49314 : && !TREE_CONSTANT (instance_ptr)))
4225 : 87912 : instance_ptr = instance_save_expr
4226 : 87912 : = get_internal_target_expr (instance_ptr);
4227 : :
4228 : : /* See above comment. */
4229 : 88404 : if (TREE_SIDE_EFFECTS (function)
4230 : 88404 : || (!nonvirtual
4231 : 68557 : && !DECL_P (function)
4232 : 68545 : && !TREE_CONSTANT (function)))
4233 : 87927 : function = get_internal_target_expr (function);
4234 : :
4235 : : /* Start by extracting all the information from the PMF itself. */
4236 : 88404 : e3 = pfn_from_ptrmemfunc (function);
4237 : 88404 : delta = delta_from_ptrmemfunc (function);
4238 : 88404 : idx = build1 (NOP_EXPR, vtable_index_type, e3);
4239 : 88404 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4240 : : {
4241 : 88404 : int flag_sanitize_save;
4242 : 88404 : case ptrmemfunc_vbit_in_pfn:
4243 : 88404 : e1 = cp_build_binary_op (input_location,
4244 : : BIT_AND_EXPR, idx, integer_one_node,
4245 : : complain);
4246 : 88404 : idx = cp_build_binary_op (input_location,
4247 : : MINUS_EXPR, idx, integer_one_node,
4248 : : complain);
4249 : 88404 : if (idx == error_mark_node)
4250 : : return error_mark_node;
4251 : 88404 : 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 : 88404 : 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 : 88404 : if (!same_type_ignoring_top_level_qualifiers_p
4284 : 88404 : (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 : 88404 : instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4295 : :
4296 : : /* Hand back the adjusted 'this' argument to our caller. */
4297 : 88404 : *instance_ptrptr = instance_ptr;
4298 : :
4299 : 88404 : if (nonvirtual)
4300 : : /* Now just return the pointer. */
4301 : : return e3;
4302 : :
4303 : : /* Next extract the vtable pointer from the object. */
4304 : 88124 : vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4305 : : instance_ptr);
4306 : 88124 : vtbl = cp_build_fold_indirect_ref (vtbl);
4307 : 88124 : if (vtbl == error_mark_node)
4308 : : return error_mark_node;
4309 : :
4310 : : /* Finally, extract the function pointer from the vtable. */
4311 : 88124 : e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4312 : 88124 : e2 = cp_build_fold_indirect_ref (e2);
4313 : 88124 : if (e2 == error_mark_node)
4314 : : return error_mark_node;
4315 : 88124 : TREE_CONSTANT (e2) = 1;
4316 : :
4317 : : /* When using function descriptors, the address of the
4318 : : vtable entry is treated as a function pointer. */
4319 : 88124 : if (TARGET_VTABLE_USES_DESCRIPTORS)
4320 : : e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4321 : : cp_build_addr_expr (e2, complain));
4322 : :
4323 : 88124 : e2 = fold_convert (TREE_TYPE (e3), e2);
4324 : 88124 : e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4325 : 88124 : 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 : 88124 : if (instance_save_expr)
4331 : 87864 : 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 : 231233 : 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 : 231233 : vec<tree, va_gc> *orig_params = params;
4354 : 231233 : 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 : 231233 : gcc_assert (params == orig_params);
4361 : :
4362 : 231233 : 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 : 474152 : cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4383 : : {
4384 : 474152 : va_list args;
4385 : 474152 : tree ret, t;
4386 : :
4387 : 474152 : releasing_vec vec;
4388 : 474152 : va_start (args, complain);
4389 : 1169598 : for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4390 : 695446 : vec_safe_push (vec, t);
4391 : 474152 : va_end (args);
4392 : 474152 : ret = cp_build_function_call_vec (function, &vec, complain);
4393 : 474152 : return ret;
4394 : 474152 : }
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 : 3122257 : cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4416 : : tsubst_flags_t complain, tree orig_fndecl)
4417 : : {
4418 : 3122257 : tree fntype, fndecl;
4419 : 3122257 : int is_method;
4420 : 3122257 : tree original = function;
4421 : 3122257 : int nargs;
4422 : 3122257 : tree *argarray;
4423 : 3122257 : tree parm_types;
4424 : 3122257 : vec<tree, va_gc> *allocated = NULL;
4425 : 3122257 : 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 : 3122257 : if (params != NULL && !vec_safe_is_empty (*params))
4430 : 1663639 : 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 : 3122257 : if (TREE_CODE (function) == NOP_EXPR
4435 : 3122257 : && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4436 : 15 : function = TREE_OPERAND (function, 0);
4437 : :
4438 : 3122257 : if (TREE_CODE (function) == FUNCTION_DECL)
4439 : : {
4440 : 1281854 : if (!mark_used (function, complain))
4441 : 24 : return error_mark_node;
4442 : 1281830 : fndecl = function;
4443 : :
4444 : : /* Convert anything with function type to a pointer-to-function. */
4445 : 1281830 : 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 : 1281830 : function = build_addr_func (function, complain);
4454 : : }
4455 : : else
4456 : : {
4457 : 1840403 : fndecl = NULL_TREE;
4458 : :
4459 : 1840403 : function = build_addr_func (function, complain);
4460 : : }
4461 : :
4462 : 3122233 : if (function == error_mark_node)
4463 : : return error_mark_node;
4464 : :
4465 : 3122131 : fntype = TREE_TYPE (function);
4466 : :
4467 : 3122131 : 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 : 6244232 : is_method = (TYPE_PTR_P (fntype)
4477 : 3122116 : && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4478 : :
4479 : 3122116 : if (!(TYPE_PTRFN_P (fntype)
4480 : 89718 : || is_method
4481 : 1387 : || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4482 : : {
4483 : 1387 : 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 : 1387 : return error_mark_node;
4497 : : }
4498 : :
4499 : : /* fntype now gets the type of function pointed to. */
4500 : 3120729 : fntype = TREE_TYPE (fntype);
4501 : 3120729 : parm_types = TYPE_ARG_TYPES (fntype);
4502 : :
4503 : 3120729 : if (params == NULL)
4504 : : {
4505 : 148734 : allocated = make_tree_vector ();
4506 : 148734 : params = &allocated;
4507 : : }
4508 : :
4509 : 3120729 : nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4510 : : complain);
4511 : 3120729 : if (nargs < 0)
4512 : 1552 : return error_mark_node;
4513 : :
4514 : 3119177 : argarray = (*params)->address ();
4515 : :
4516 : : /* Check for errors in format strings and inappropriately
4517 : : null parameters. */
4518 : 3119177 : bool warned_p
4519 : 3119177 : = ((complain & tf_warning)
4520 : 3119177 : && check_function_arguments (input_location, fndecl, fntype,
4521 : : nargs, argarray, NULL,
4522 : 3119177 : cp_comp_parm_types));
4523 : :
4524 : 3119177 : ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4525 : :
4526 : 3119177 : 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 : 3119177 : if (allocated != NULL)
4534 : 148734 : 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 : 3120729 : convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4606 : : int flags, tsubst_flags_t complain)
4607 : : {
4608 : 3120729 : tree typetail;
4609 : 3120729 : unsigned int i;
4610 : :
4611 : : /* Argument passing is always copy-initialization. */
4612 : 3120729 : flags |= LOOKUP_ONLYCONVERTING;
4613 : :
4614 : 6897429 : for (i = 0, typetail = typelist;
4615 : 6897429 : i < vec_safe_length (*values);
4616 : : i++)
4617 : : {
4618 : 7555879 : tree type = typetail ? TREE_VALUE (typetail) : 0;
4619 : 3778164 : tree val = (**values)[i];
4620 : :
4621 : 3778164 : if (val == error_mark_node || type == error_mark_node)
4622 : : return -1;
4623 : :
4624 : 3778158 : if (type == void_type_node)
4625 : : {
4626 : 194 : 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 : 3777964 : if (TREE_CODE (val) == NOP_EXPR
4638 : 907075 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4639 : 3777982 : && (type == 0 || !TYPE_REF_P (type)))
4640 : 18 : val = TREE_OPERAND (val, 0);
4641 : :
4642 : 3777964 : if (type == 0 || !TYPE_REF_P (type))
4643 : : {
4644 : 3561622 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4645 : 3561622 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4646 : 10710 : val = decay_conversion (val, complain);
4647 : : }
4648 : :
4649 : 3777964 : if (val == error_mark_node)
4650 : : return -1;
4651 : :
4652 : 3777964 : if (type != 0)
4653 : : {
4654 : : /* Formal parm type is specified by a function prototype. */
4655 : 3777515 : tree parmval;
4656 : :
4657 : 3777515 : 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 : 3777497 : parmval = convert_for_initialization
4680 : 3777497 : (NULL_TREE, type, val, flags,
4681 : : ICR_ARGPASS, fndecl, i, complain);
4682 : 3777497 : parmval = convert_for_arg_passing (type, parmval, complain);
4683 : : }
4684 : :
4685 : 3777515 : if (parmval == error_mark_node)
4686 : : return -1;
4687 : :
4688 : 3776251 : (**values)[i] = parmval;
4689 : : }
4690 : : else
4691 : : {
4692 : 449 : 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 : 449 : val = convert_arg_to_ellipsis (val, complain);
4705 : :
4706 : 449 : (**values)[i] = val;
4707 : : }
4708 : :
4709 : 3776700 : if (typetail)
4710 : 3776251 : typetail = TREE_CHAIN (typetail);
4711 : : }
4712 : :
4713 : 3119265 : 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 : 160 : && 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 : 157 : if (typetail && typetail != void_list_node)
4748 : : {
4749 : 157 : if (complain & tf_error)
4750 : 84 : error_args_num (input_location, fndecl, /*too_many_p=*/false);
4751 : 157 : return -1;
4752 : : }
4753 : : }
4754 : :
4755 : 3119108 : 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 : 191489307 : 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 : 191489307 : tree orig_arg1;
4776 : 191489307 : tree orig_arg2;
4777 : 191489307 : tree expr;
4778 : 191489307 : tree overload = NULL_TREE;
4779 : :
4780 : 191489307 : orig_arg1 = arg1;
4781 : 191489307 : orig_arg2 = arg2;
4782 : :
4783 : 191489307 : if (processing_template_decl)
4784 : : {
4785 : 113460865 : if (type_dependent_expression_p (arg1)
4786 : 113460865 : || type_dependent_expression_p (arg2))
4787 : : {
4788 : 81056171 : expr = build_min_nt_loc (loc, code, arg1, arg2);
4789 : 81056171 : TREE_TYPE (expr)
4790 : 81056171 : = build_dependent_operator_type (lookups, code, false);
4791 : 81056171 : return expr;
4792 : : }
4793 : : }
4794 : :
4795 : 110433136 : if (code == DOTSTAR_EXPR)
4796 : 88705 : expr = build_m_component_ref (arg1, arg2, complain);
4797 : : else
4798 : 110344431 : expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4799 : : lookups, &overload, complain);
4800 : :
4801 : 110433103 : if (overload_p != NULL)
4802 : 45998577 : *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 : 110433103 : if (warn_parentheses
4808 : 1098560 : && (complain & tf_warning)
4809 : 1060801 : && !processing_template_decl
4810 : 733932 : && !error_operand_p (arg1)
4811 : 733926 : && !error_operand_p (arg2)
4812 : 111167017 : && (code != LSHIFT_EXPR
4813 : 66969 : || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4814 : 732929 : warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4815 : : arg2_code, orig_arg2);
4816 : :
4817 : 110433103 : if (processing_template_decl && expr != error_mark_node)
4818 : : {
4819 : 32404511 : if (overload != NULL_TREE)
4820 : 2034006 : return (build_min_non_dep_op_overload
4821 : 2034006 : (code, expr, overload, orig_arg1, orig_arg2));
4822 : :
4823 : 30370505 : 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 : 1923306 : build_x_array_ref (location_t loc, tree arg1, tree arg2,
4833 : : tsubst_flags_t complain)
4834 : : {
4835 : 1923306 : tree orig_arg1 = arg1;
4836 : 1923306 : tree orig_arg2 = arg2;
4837 : 1923306 : tree expr;
4838 : 1923306 : tree overload = NULL_TREE;
4839 : :
4840 : 1923306 : if (processing_template_decl)
4841 : : {
4842 : 655 : if (type_dependent_expression_p (arg1)
4843 : 655 : || type_dependent_expression_p (arg2))
4844 : 637 : return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4845 : 637 : NULL_TREE, NULL_TREE);
4846 : : }
4847 : :
4848 : 1922669 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4849 : : NULL_TREE, NULL_TREE, &overload, complain);
4850 : :
4851 : 1922669 : 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 : 10692 : build_omp_array_section (location_t loc, tree array_expr, tree index,
4870 : : tree length)
4871 : : {
4872 : 10692 : if (TREE_CODE (array_expr) == TYPE_DECL
4873 : 10692 : || 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 : 10418 : tree type = TREE_TYPE (array_expr);
4878 : 10418 : gcc_assert (type);
4879 : 10418 : type = non_reference (type);
4880 : :
4881 : 10418 : 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 : 10418 : if (eltype == NULL_TREE)
4887 : 42 : sectype = TREE_TYPE (array_expr);
4888 : : else
4889 : : {
4890 : 10376 : 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 : 10376 : if (index != NULL_TREE
4896 : 10376 : && length != NULL_TREE
4897 : 7482 : && TREE_CODE (index) == INTEGER_CST
4898 : 6686 : && TREE_CODE (length) == INTEGER_CST)
4899 : : {
4900 : 5191 : tree low = fold_convert (sizetype, index);
4901 : 5191 : tree high = fold_convert (sizetype, length);
4902 : 5191 : high = size_binop (PLUS_EXPR, low, high);
4903 : 5191 : high = size_binop (MINUS_EXPR, high, size_one_node);
4904 : 5191 : idxtype = build_range_type (sizetype, low, high);
4905 : 5191 : }
4906 : 2948 : else if ((index == NULL_TREE || integer_zerop (index))
4907 : 3555 : && length != NULL_TREE
4908 : 7290 : && TREE_CODE (length) == INTEGER_CST)
4909 : 1384 : idxtype = build_index_type (length);
4910 : :
4911 : 10376 : sectype = build_array_type (eltype, idxtype);
4912 : : }
4913 : :
4914 : 10418 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array_expr, index,
4915 : 10418 : 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 : 575382 : enum_cast_to_int (tree op)
4926 : : {
4927 : 563664 : if (CONVERT_EXPR_P (op)
4928 : 12824 : && TREE_TYPE (op) == integer_type_node
4929 : 774 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4930 : 575420 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4931 : : return true;
4932 : :
4933 : : /* The cast may have been pushed into a COND_EXPR. */
4934 : 575352 : if (TREE_CODE (op) == COND_EXPR)
4935 : 761 : return (enum_cast_to_int (TREE_OPERAND (op, 1))
4936 : 761 : || enum_cast_to_int (TREE_OPERAND (op, 2)));
4937 : :
4938 : : return false;
4939 : : }
4940 : :
4941 : : /* For the c-common bits. */
4942 : : tree
4943 : 3920796 : build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4944 : : bool /*convert_p*/)
4945 : : {
4946 : 3920796 : 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 : 6574 : build_vec_cmp (tree_code code, tree type,
4954 : : tree arg0, tree arg1)
4955 : : {
4956 : 6574 : tree zero_vec = build_zero_cst (type);
4957 : 6574 : tree minus_one_vec = build_minus_one_cst (type);
4958 : 6574 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
4959 : 6574 : tree cmp = build2 (code, cmp_type, arg0, arg1);
4960 : 6574 : 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 : 1539086 : warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4967 : : {
4968 : : /* Prevent warnings issued for macro expansion. */
4969 : 1539086 : if (!warn_address
4970 : 40314 : || (complain & tf_warning) == 0
4971 : 24068 : || c_inhibit_evaluation_warnings != 0
4972 : 24028 : || from_macro_expansion_at (location)
4973 : 1556502 : || warning_suppressed_p (op, OPT_Waddress))
4974 : 1521993 : return;
4975 : :
4976 : 17383 : tree cop = fold_for_warn (op);
4977 : :
4978 : 17383 : if (TREE_CODE (cop) == NON_LVALUE_EXPR)
4979 : : /* Unwrap the expression for C++ 98. */
4980 : 1 : cop = TREE_OPERAND (cop, 0);
4981 : :
4982 : 17383 : 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 : 17353 : if (TREE_CODE (cop) == NOP_EXPR)
4992 : : {
4993 : : /* Allow casts to intptr_t to suppress the warning. */
4994 : 1098 : tree type = TREE_TYPE (cop);
4995 : 1098 : if (TREE_CODE (type) == INTEGER_TYPE)
4996 : : return;
4997 : :
4998 : 1098 : STRIP_NOPS (cop);
4999 : : }
5000 : :
5001 : 17353 : auto_diagnostic_group d;
5002 : 17353 : bool warned = false;
5003 : 17353 : 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 : 16564 : 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 : 15645 : else if (CONVERT_EXPR_P (op)
5057 : 16524 : && 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 : 17353 : }
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 : 91827211 : do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5085 : : tree type1, tsubst_flags_t complain)
5086 : : {
5087 : 91827211 : if (TREE_CODE (type0) == ENUMERAL_TYPE
5088 : 2755393 : && TREE_CODE (type1) == ENUMERAL_TYPE
5089 : 94073123 : && 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 : 91827013 : else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5143 : 2755195 : && SCALAR_FLOAT_TYPE_P (type1))
5144 : 91826936 : || (SCALAR_FLOAT_TYPE_P (type0)
5145 : 32196611 : && 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 : 120563657 : 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 : 120563657 : tree op0, op1;
5237 : 120563657 : enum tree_code code0, code1;
5238 : 120563657 : tree type0, type1, orig_type0, orig_type1;
5239 : 120563657 : 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 : 120563657 : 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 : 120563657 : tree result_type = NULL_TREE;
5249 : :
5250 : : /* When the computation is in excess precision, the type of the
5251 : : final EXCESS_PRECISION_EXPR. */
5252 : 120563657 : 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 : 120563657 : int converted = 0;
5258 : :
5259 : : /* Nonzero means create the expression with this type, rather than
5260 : : RESULT_TYPE. */
5261 : 120563657 : tree build_type = 0;
5262 : :
5263 : : /* Nonzero means after finally constructing the expression
5264 : : convert it to this type. */
5265 : 120563657 : tree final_type = 0;
5266 : :
5267 : 120563657 : 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 : 120563657 : 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 : 120563657 : 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 : 120563657 : int short_shift = 0;
5285 : :
5286 : : /* Nonzero means set RESULT_TYPE to the common type of the args. */
5287 : 120563657 : int common = 0;
5288 : :
5289 : : /* True if both operands have arithmetic type. */
5290 : 120563657 : bool arithmetic_types_p;
5291 : :
5292 : : /* Remember whether we're doing / or %. */
5293 : 120563657 : bool doing_div_or_mod = false;
5294 : :
5295 : : /* Remember whether we're doing << or >>. */
5296 : 120563657 : bool doing_shift = false;
5297 : :
5298 : : /* Tree holding instrumentation expression. */
5299 : 120563657 : tree instrument_expr = NULL_TREE;
5300 : :
5301 : : /* True means this is an arithmetic operation that may need excess
5302 : : precision. */
5303 : 120563657 : bool may_need_excess_precision;
5304 : :
5305 : : /* Apply default conversions. */
5306 : 120563657 : op0 = resolve_nondeduced_context (orig_op0, complain);
5307 : 120563657 : op1 = resolve_nondeduced_context (orig_op1, complain);
5308 : :
5309 : 120563657 : if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5310 : 109570544 : || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5311 : 106913104 : || code == TRUTH_XOR_EXPR)
5312 : : {
5313 : 13650553 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5314 : 13650535 : op0 = decay_conversion (op0, complain);
5315 : 13650553 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5316 : 13650553 : op1 = decay_conversion (op1, complain);
5317 : : }
5318 : : else
5319 : : {
5320 : 106913104 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5321 : 106913089 : op0 = cp_default_conversion (op0, complain);
5322 : 106913104 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5323 : 106913101 : op1 = cp_default_conversion (op1, complain);
5324 : : }
5325 : :
5326 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5327 : 127734880 : STRIP_TYPE_NOPS (op0);
5328 : 147572038 : STRIP_TYPE_NOPS (op1);
5329 : :
5330 : : /* DTRT if one side is an overloaded function, but complain about it. */
5331 : 120563657 : 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 : 120563657 : 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 : 120563657 : orig_type0 = type0 = TREE_TYPE (op0);
5357 : 120563657 : orig_type1 = type1 = TREE_TYPE (op1);
5358 : 120563657 : tree non_ep_op0 = op0;
5359 : 120563657 : 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 : 120563657 : code0 = TREE_CODE (type0);
5364 : 120563657 : code1 = TREE_CODE (type1);
5365 : :
5366 : : /* If an error was already reported for one of the arguments,
5367 : : avoid reporting another error. */
5368 : 120563657 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5369 : 100 : return error_mark_node;
5370 : :
5371 : 241127114 : if ((invalid_op_diag
5372 : 120563557 : = 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 : 120563557 : 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 : 40057444 : case EQ_EXPR:
5405 : 40057444 : case NE_EXPR:
5406 : 40057444 : case LE_EXPR:
5407 : 40057444 : case GE_EXPR:
5408 : 40057444 : case LT_EXPR:
5409 : 40057444 : case GT_EXPR:
5410 : 40057444 : case SPACESHIP_EXPR:
5411 : : /* Excess precision for implicit conversions of integers to
5412 : : floating point. */
5413 : 8623655 : may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5414 : 48675095 : || ANY_INTEGRAL_TYPE_P (type1));
5415 : : break;
5416 : : default:
5417 : 120563557 : may_need_excess_precision = false;
5418 : : break;
5419 : : }
5420 : 120563557 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5421 : : {
5422 : 20658 : op0 = TREE_OPERAND (op0, 0);
5423 : 20658 : type0 = TREE_TYPE (op0);
5424 : : }
5425 : 120542899 : else if (may_need_excess_precision
5426 : 91875808 : && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5427 : 28119781 : if (tree eptype = excess_precision_type (type0))
5428 : : {
5429 : 55375 : type0 = eptype;
5430 : 55375 : op0 = convert (eptype, op0);
5431 : : }
5432 : 120563557 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5433 : : {
5434 : 26148 : op1 = TREE_OPERAND (op1, 0);
5435 : 26148 : type1 = TREE_TYPE (op1);
5436 : : }
5437 : 120537409 : else if (may_need_excess_precision
5438 : 91873285 : && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5439 : 28045420 : if (tree eptype = excess_precision_type (type1))
5440 : : {
5441 : 36309 : type1 = eptype;
5442 : 36309 : op1 = convert (eptype, op1);
5443 : : }
5444 : :
5445 : : /* Issue warnings about peculiar, but valid, uses of NULL. */
5446 : 241127029 : 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 : 12094 : && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5450 : 12079 : && ( /* Both are NULL (or 0) and the operation was not a
5451 : : comparison or a pointer subtraction. */
5452 : 12152 : (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 : 12067 : || (!null_ptr_cst_p (orig_op0)
5456 : 12006 : && !TYPE_PTR_OR_PTRMEM_P (type0))
5457 : 12055 : || (!null_ptr_cst_p (orig_op1)
5458 : 46 : && !TYPE_PTR_OR_PTRMEM_P (type1)))
5459 : 120563596 : && (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 : 120620467 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5470 : 120619643 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5471 : : {
5472 : 909 : enum stv_conv convert_flag
5473 : 909 : = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5474 : : complain & tf_error);
5475 : :
5476 : 909 : switch (convert_flag)
5477 : : {
5478 : 21 : case stv_error:
5479 : 21 : return error_mark_node;
5480 : 55 : case stv_firstarg:
5481 : 55 : {
5482 : 55 : op0 = convert (TREE_TYPE (type1), op0);
5483 : 55 : op0 = cp_save_expr (op0);
5484 : 55 : op0 = build_vector_from_val (type1, op0);
5485 : 55 : orig_type0 = type0 = TREE_TYPE (op0);
5486 : 55 : code0 = TREE_CODE (type0);
5487 : 55 : converted = 1;
5488 : 55 : break;
5489 : : }
5490 : 728 : case stv_secondarg:
5491 : 728 : {
5492 : 728 : op1 = convert (TREE_TYPE (type0), op1);
5493 : 728 : op1 = cp_save_expr (op1);
5494 : 728 : op1 = build_vector_from_val (type0, op1);
5495 : 728 : orig_type1 = type1 = TREE_TYPE (op1);
5496 : 728 : code1 = TREE_CODE (type1);
5497 : 728 : converted = 1;
5498 : 728 : break;
5499 : : }
5500 : : default:
5501 : : break;
5502 : : }
5503 : : }
5504 : :
5505 : 120563536 : switch (code)
5506 : : {
5507 : 14084273 : case MINUS_EXPR:
5508 : : /* Subtraction of two similar pointers.
5509 : : We must subtract them as integers, then divide by object size. */
5510 : 14084273 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5511 : 15318383 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5512 : 1234110 : TREE_TYPE (type1)))
5513 : : {
5514 : 1234108 : result = pointer_diff (location, op0, op1,
5515 : : common_pointer_type (type0, type1), complain,
5516 : : &instrument_expr);
5517 : 1234108 : if (instrument_expr != NULL)
5518 : 84 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5519 : : instrument_expr, result);
5520 : :
5521 : 1234108 : return result;
5522 : : }
5523 : : /* In all other cases except pointer - int, the usual arithmetic
5524 : : rules apply. */
5525 : 12850165 : 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 : 18632009 : gcc_fallthrough ();
5533 : 18632009 : case PLUS_EXPR:
5534 : 18632009 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5535 : 6114900 : && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5536 : : {
5537 : 6114891 : tree ptr_operand;
5538 : 6114891 : tree int_operand;
5539 : 6114891 : ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5540 : 19979 : int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5541 : 6114891 : if (processing_template_decl)
5542 : : {
5543 : 1978084 : result_type = TREE_TYPE (ptr_operand);
5544 : 1978084 : break;
5545 : : }
5546 : 4136807 : return cp_pointer_int_sum (location, code,
5547 : : ptr_operand,
5548 : : int_operand,
5549 : 4136807 : complain);
5550 : : }
5551 : : common = 1;
5552 : : break;
5553 : :
5554 : : case MULT_EXPR:
5555 : 115159918 : common = 1;
5556 : : break;
5557 : :
5558 : 9345858 : case TRUNC_DIV_EXPR:
5559 : 9345858 : case CEIL_DIV_EXPR:
5560 : 9345858 : case FLOOR_DIV_EXPR:
5561 : 9345858 : case ROUND_DIV_EXPR:
5562 : 9345858 : case EXACT_DIV_EXPR:
5563 : 9345858 : if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5564 : : {
5565 : 99326 : tree type0 = TREE_OPERAND (op0, 0);
5566 : 99326 : tree type1 = TREE_OPERAND (op1, 0);
5567 : 99326 : tree first_arg = tree_strip_any_location_wrapper (type0);
5568 : 99326 : if (!TYPE_P (type0))
5569 : 80001 : type0 = TREE_TYPE (type0);
5570 : 99326 : if (!TYPE_P (type1))
5571 : 45930 : type1 = TREE_TYPE (type1);
5572 : 99326 : if (type0
5573 : 99326 : && type1
5574 : 79317 : && INDIRECT_TYPE_P (type0)
5575 : 99401 : && 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 : 99302 : else if (!dependent_type_p (type0)
5593 : 26021 : && !dependent_type_p (type1)
5594 : 25923 : && TREE_CODE (type0) == ARRAY_TYPE
5595 : 22017 : && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5596 : : /* Set by finish_parenthesized_expr. */
5597 : 21399 : && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5598 : 120683 : && (complain & tf_warning))
5599 : 21381 : maybe_warn_sizeof_array_div (location, first_arg, type0,
5600 : : op1, non_reference (type1));
5601 : : }
5602 : :
5603 : 9345858 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5604 : : || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5605 : 9345840 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5606 : : || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5607 : : {
5608 : 9345831 : enum tree_code tcode0 = code0, tcode1 = code1;
5609 : 9345831 : doing_div_or_mod = true;
5610 : 9345831 : warn_for_div_by_zero (location, fold_for_warn (op1));
5611 : :
5612 : 9345831 : if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5613 : 59839 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5614 : 9345831 : if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5615 : 30639 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5616 : :
5617 : 9345831 : 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 : 3680176 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5627 : 3680176 : shorten = may_shorten_divmod (op0, stripped_op1);
5628 : : }
5629 : :
5630 : : common = 1;
5631 : : }
5632 : : break;
5633 : :
5634 : 2033733 : case BIT_AND_EXPR:
5635 : 2033733 : case BIT_IOR_EXPR:
5636 : 2033733 : case BIT_XOR_EXPR:
5637 : 2033733 : if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5638 : 2033733 : || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5639 : 17401 : && !VECTOR_FLOAT_TYPE_P (type0)
5640 : 17383 : && !VECTOR_FLOAT_TYPE_P (type1)))
5641 : : shorten = -1;
5642 : : break;
5643 : :
5644 : 1128795 : case TRUNC_MOD_EXPR:
5645 : 1128795 : case FLOOR_MOD_EXPR:
5646 : 1128795 : doing_div_or_mod = true;
5647 : 1128795 : warn_for_div_by_zero (location, fold_for_warn (op1));
5648 : :
5649 : 1128795 : if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5650 : 20 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5651 : 1128815 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5652 : : common = 1;
5653 : 1128775 : 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 : 1128751 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5660 : 1128751 : shorten = may_shorten_divmod (op0, stripped_op1);
5661 : 1128751 : common = 1;
5662 : : }
5663 : : break;
5664 : :
5665 : 13650538 : case TRUTH_ANDIF_EXPR:
5666 : 13650538 : case TRUTH_ORIF_EXPR:
5667 : 13650538 : case TRUTH_AND_EXPR:
5668 : 13650538 : case TRUTH_OR_EXPR:
5669 : 13650538 : 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 : 13650535 : if (gnu_vector_type_p (type0)
5688 : 13650535 : && (!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 : 13650511 : result_type = boolean_type_node;
5714 : 13650511 : 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 : 895746 : case RSHIFT_EXPR:
5721 : 895746 : if (gnu_vector_type_p (type0)
5722 : 170 : && code1 == INTEGER_TYPE
5723 : 895793 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5724 : : {
5725 : : result_type = type0;
5726 : : converted = 1;
5727 : : }
5728 : 895699 : 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 : 895819 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5733 : : TYPE_VECTOR_SUBPARTS (type1)))
5734 : : {
5735 : : result_type = type0;
5736 : : converted = 1;
5737 : : }
5738 : 895579 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5739 : : {
5740 : 895552 : tree const_op1 = fold_for_warn (op1);
5741 : 895552 : if (TREE_CODE (const_op1) != INTEGER_CST)
5742 : 71011 : const_op1 = op1;
5743 : 895552 : result_type = type0;
5744 : 895552 : doing_shift = true;
5745 : 895552 : if (TREE_CODE (const_op1) == INTEGER_CST)
5746 : : {
5747 : 824541 : 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 : 824493 : if (!integer_zerop (const_op1))
5757 : 824407 : short_shift = 1;
5758 : :
5759 : 824493 : if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5760 : 46 : && (complain & tf_warning)
5761 : 824539 : && 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 : 2713875 : case LSHIFT_EXPR:
5772 : 2713875 : if (gnu_vector_type_p (type0)
5773 : 183 : && code1 == INTEGER_TYPE
5774 : 2713912 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5775 : : {
5776 : : result_type = type0;
5777 : : converted = 1;
5778 : : }
5779 : 2713838 : 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 : 2713978 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5784 : : TYPE_VECTOR_SUBPARTS (type1)))
5785 : : {
5786 : : result_type = type0;
5787 : : converted = 1;
5788 : : }
5789 : 2713698 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5790 : : {
5791 : 2713667 : tree const_op0 = fold_for_warn (op0);
5792 : 2713667 : if (TREE_CODE (const_op0) != INTEGER_CST)
5793 : 132677 : const_op0 = op0;
5794 : 2713667 : tree const_op1 = fold_for_warn (op1);
5795 : 2713667 : if (TREE_CODE (const_op1) != INTEGER_CST)
5796 : 202643 : const_op1 = op1;
5797 : 2713667 : result_type = type0;
5798 : 2713667 : doing_shift = true;
5799 : 2713667 : if (TREE_CODE (const_op0) == INTEGER_CST
5800 : 2580990 : && tree_int_cst_sgn (const_op0) < 0
5801 : 280 : && !TYPE_OVERFLOW_WRAPS (type0)
5802 : 196 : && (complain & tf_warning)
5803 : 2713863 : && c_inhibit_evaluation_warnings == 0)
5804 : 196 : warning_at (location, OPT_Wshift_negative_value,
5805 : : "left shift of negative value");
5806 : 2713667 : if (TREE_CODE (const_op1) == INTEGER_CST)
5807 : : {
5808 : 2511024 : 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 : 2510967 : else if (compare_tree_int (const_op1,
5816 : 2510967 : 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 : 2510884 : else if (TREE_CODE (const_op0) == INTEGER_CST
5824 : 2400074 : && (complain & tf_warning))
5825 : 2289993 : 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 : 19889554 : case EQ_EXPR:
5833 : 19889554 : case NE_EXPR:
5834 : 19889554 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5835 : 2407 : goto vector_compare;
5836 : 19887147 : if ((complain & tf_warning)
5837 : 18681574 : && c_inhibit_evaluation_warnings == 0
5838 : 37987760 : && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5839 : 812981 : warning_at (location, OPT_Wfloat_equal,
5840 : : "comparing floating-point with %<==%> "
5841 : : "or %<!=%> is unsafe");
5842 : 19887147 : {
5843 : 19887147 : tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5844 : 19887147 : tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5845 : 19887147 : if ((complain & tf_warning_or_error)
5846 : 19887147 : && ((TREE_CODE (stripped_orig_op0) == STRING_CST
5847 : 39 : && !integer_zerop (cp_fully_fold (op1)))
5848 : 18904920 : || (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 : 19887106 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5854 : 19887106 : && 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 : 19887134 : build_type = boolean_type_node;
5866 : 19887134 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5867 : : || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5868 : 16824933 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5869 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5870 : : short_compare = 1;
5871 : 27955 : else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5872 : 3061760 : && null_ptr_cst_p (orig_op1))
5873 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5874 : 4659450 : || (code0 == POINTER_TYPE
5875 : 1543037 : && TYPE_PTR_P (type1) && integer_zerop (op1)))
5876 : : {
5877 : 1537925 : if (TYPE_PTR_P (type1))
5878 : 19569 : result_type = composite_pointer_type (location,
5879 : : type0, type1, op0, op1,
5880 : : CPO_COMPARISON, complain);
5881 : : else
5882 : : result_type = type0;
5883 : :
5884 : 1537925 : 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 : 1537925 : warn_for_null_address (location, op0, complain);
5894 : : }
5895 : 873 : else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5896 : 1550496 : && null_ptr_cst_p (orig_op0))
5897 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5898 : 3101063 : || (code1 == POINTER_TYPE
5899 : 1549236 : && 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 : 1550017 : else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5920 : 26675 : || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5921 : 1523709 : result_type = composite_pointer_type (location,
5922 : : type0, type1, op0, op1,
5923 : : CPO_COMPARISON, complain);
5924 : 26308 : 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 : 26234 : 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 : 26176 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5937 : : {
5938 : 25826 : result_type = type1;
5939 : 25826 : if (complain & tf_error)
5940 : 69 : permerror (location, "ISO C++ forbids comparison between "
5941 : : "pointer and integer");
5942 : : else
5943 : 25757 : return error_mark_node;
5944 : : }
5945 : 350 : else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5946 : : {
5947 : 200 : 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 : 200 : op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
5989 : 200 : op1 = cp_convert (TREE_TYPE (op0), op1, complain);
5990 : : }
5991 : 200 : result_type = TREE_TYPE (op0);
5992 : :
5993 : 200 : 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 : 20167890 : case LE_EXPR:
6130 : 20167890 : case GE_EXPR:
6131 : 20167890 : case LT_EXPR:
6132 : 20167890 : case GT_EXPR:
6133 : 20167890 : case SPACESHIP_EXPR:
6134 : 20167890 : if (TREE_CODE (orig_op0) == STRING_CST
6135 : 20167890 : || 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 : 20167890 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6143 : 153 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6144 : 20167994 : && 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 : 20167889 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6155 : : {
6156 : 6583 : vector_compare:
6157 : 6583 : tree intt;
6158 : 6583 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6159 : 6583 : TREE_TYPE (type1))
6160 : 6583 : && !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 : 6580 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
6174 : 13160 : 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 : 6577 : 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 : 6577 : 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 : 6574 : intt = c_common_type_for_size
6210 : 13148 : (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
6211 : 6574 : 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 : 6574 : result_type = build_opaque_vector_type (intt,
6219 : 6574 : TYPE_VECTOR_SUBPARTS (type0));
6220 : 6574 : return build_vec_cmp (resultcode, result_type, op0, op1);
6221 : : }
6222 : 20163713 : build_type = boolean_type_node;
6223 : 20163713 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6224 : : || code0 == ENUMERAL_TYPE)
6225 : 19132468 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6226 : : || code1 == ENUMERAL_TYPE))
6227 : : short_compare = 1;
6228 : 1031332 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6229 : 1031100 : 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 : 20163522 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6262 : 1031126 : && !processing_template_decl
6263 : 20980559 : && 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 : 115159918 : if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6296 : : || code0 == ENUMERAL_TYPE)
6297 : 95801207 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6298 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6299 : : arithmetic_types_p = 1;
6300 : : else
6301 : : {
6302 : 19592900 : arithmetic_types_p = 0;
6303 : : /* Vector arithmetic is only allowed when both sides are vectors. */
6304 : 19592900 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6305 : : {
6306 : 50271 : if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6307 : 50271 : || !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 : 115159900 : if (!result_type
6324 : 115159900 : && arithmetic_types_p
6325 : 91827288 : && (shorten || common || short_compare))
6326 : : {
6327 : 91827211 : result_type = cp_common_type (type0, type1);
6328 : 91827211 : 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 : 91827211 : if (complain & tf_warning)
6350 : 87257462 : 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 : 91827211 : if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6355 : 91827211 : TREE_TYPE (orig_op1), complain))
6356 : 6 : return error_mark_node;
6357 : : }
6358 : 115159894 : if (may_need_excess_precision
6359 : 86493402 : && (orig_type0 != type0 || orig_type1 != type1)
6360 : 78817 : && build_type == NULL_TREE
6361 : 78817 : && result_type)
6362 : : {
6363 : 59416 : gcc_assert (common);
6364 : 59416 : semantic_result_type = cp_common_type (orig_type0, orig_type1);
6365 : 59416 : 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 : 115159894 : if (code == SPACESHIP_EXPR)
6389 : : {
6390 : 179850 : iloc_sentinel s (location);
6391 : :
6392 : 179850 : tree orig_type0 = TREE_TYPE (orig_op0);
6393 : 179850 : tree_code orig_code0 = TREE_CODE (orig_type0);
6394 : 179850 : tree orig_type1 = TREE_TYPE (orig_op1);
6395 : 179850 : tree_code orig_code1 = TREE_CODE (orig_type1);
6396 : 179850 : if (!result_type || result_type == error_mark_node)
6397 : : /* Nope. */
6398 : : result_type = NULL_TREE;
6399 : 179829 : 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 : 179826 : 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 : 179810 : else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6409 : : /* <=> no longer supports equality relations. */
6410 : : result_type = NULL_TREE;
6411 : 179807 : else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6412 : 179843 : && !(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 : 179792 : else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6420 : 179786 : || (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 : 179780 : if (result_type)
6427 : : {
6428 : 179780 : build_type = spaceship_type (result_type, complain);
6429 : 179780 : if (build_type == error_mark_node)
6430 : : return error_mark_node;
6431 : : }
6432 : :
6433 : 179835 : 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 : 74010 : bool ok = true;
6438 : 74010 : if (TREE_CODE (result_type) == REAL_TYPE
6439 : 663 : && CP_INTEGRAL_TYPE_P (orig_type0))
6440 : : /* OK */;
6441 : 73999 : else if (!check_narrowing (result_type, orig_op0, complain))
6442 : 74010 : ok = false;
6443 : 74010 : if (TREE_CODE (result_type) == REAL_TYPE
6444 : 663 : && CP_INTEGRAL_TYPE_P (orig_type1))
6445 : : /* OK */;
6446 : 73999 : else if (!check_narrowing (result_type, orig_op1, complain))
6447 : : ok = false;
6448 : 74010 : if (!ok && !(complain & tf_error))
6449 : 4 : return error_mark_node;
6450 : : }
6451 : 179850 : }
6452 : :
6453 : 115159875 : 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 : 115159398 : 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 : 51145587 : tree tmp = build2 (resultcode,
6475 : : build_type ? build_type : result_type,
6476 : : NULL_TREE, op1);
6477 : 32073137 : TREE_OPERAND (tmp, 0) = op0;
6478 : 32073137 : if (semantic_result_type)
6479 : 0 : tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6480 : 32073137 : return tmp;
6481 : : }
6482 : :
6483 : : /* Remember the original type; RESULT_TYPE might be changed later on
6484 : : by shorten_binary_op. */
6485 : 83086261 : tree orig_type = result_type;
6486 : :
6487 : 83086261 : if (arithmetic_types_p)
6488 : : {
6489 : 72389014 : bool first_complex = (code0 == COMPLEX_TYPE);
6490 : 72389014 : bool second_complex = (code1 == COMPLEX_TYPE);
6491 : 72389014 : int none_complex = (!first_complex && !second_complex);
6492 : :
6493 : : /* Adapted from patch for c/24581. */
6494 : 72389014 : if (first_complex != second_complex
6495 : 163538 : && (code == PLUS_EXPR
6496 : : || code == MINUS_EXPR
6497 : 163538 : || code == MULT_EXPR
6498 : 42713 : || (code == TRUNC_DIV_EXPR && first_complex))
6499 : 150031 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6500 : 72538884 : && 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 : 149870 : tree real_type = TREE_TYPE (result_type);
6507 : 149870 : tree real, imag;
6508 : 149870 : if (first_complex)
6509 : : {
6510 : 116857 : if (TREE_TYPE (op0) != result_type)
6511 : 6 : op0 = cp_convert_and_check (result_type, op0, complain);
6512 : 116857 : 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 : 149870 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6523 : 0 : return error_mark_node;
6524 : 149870 : if (first_complex)
6525 : : {
6526 : 116857 : op0 = cp_save_expr (op0);
6527 : 116857 : real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6528 : 116857 : imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6529 : 116857 : switch (code)
6530 : : {
6531 : 58405 : case MULT_EXPR:
6532 : 58405 : case TRUNC_DIV_EXPR:
6533 : 58405 : op1 = cp_save_expr (op1);
6534 : 58405 : imag = build2 (resultcode, real_type, imag, op1);
6535 : : /* Fall through. */
6536 : 116857 : case PLUS_EXPR:
6537 : 116857 : case MINUS_EXPR:
6538 : 116857 : real = build2 (resultcode, real_type, real, op1);
6539 : 116857 : 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 : 149870 : result = build2 (COMPLEX_EXPR, result_type, real, imag);
6567 : 149870 : if (semantic_result_type)
6568 : 24 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6569 : : result);
6570 : 149870 : 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 : 72239144 : if (shorten && none_complex)
6585 : : {
6586 : 3802900 : final_type = result_type;
6587 : 3802900 : result_type = shorten_binary_op (result_type, op0, op1,
6588 : : shorten == -1);
6589 : : }
6590 : :
6591 : : /* Shifts can be shortened if shifting right. */
6592 : :
6593 : 72239144 : if (short_shift)
6594 : : {
6595 : 734873 : int unsigned_arg;
6596 : 734873 : 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 : 734873 : tree const_op1 = fold_for_warn (op1);
6600 : :
6601 : 734873 : final_type = result_type;
6602 : :
6603 : 734873 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
6604 : 679032 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6605 : :
6606 : 734873 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6607 : 3330 : && 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 : 3330 : && compare_tree_int (const_op1,
6611 : 3330 : TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6612 : : /* We cannot drop an unsigned shift after sign-extension. */
6613 : 738193 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6614 : : {
6615 : : /* Do an unsigned shift if the operand was zero-extended. */
6616 : 3313 : result_type
6617 : 3313 : = c_common_signed_or_unsigned_type (unsigned_arg,
6618 : 3313 : TREE_TYPE (arg0));
6619 : : /* Convert value-to-be-shifted to that type. */
6620 : 3313 : if (TREE_TYPE (op0) != result_type)
6621 : 3313 : 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 : 72239144 : if (short_compare)
6630 : : {
6631 : : /* We call shorten_compare only for diagnostics. */
6632 : 23384151 : tree xop0 = fold_simple (op0);
6633 : 23384151 : tree xop1 = fold_simple (op1);
6634 : 23384151 : tree xresult_type = result_type;
6635 : 23384151 : enum tree_code xresultcode = resultcode;
6636 : 23384151 : shorten_compare (location, &xop0, &xop1, &xresult_type,
6637 : : &xresultcode);
6638 : : }
6639 : :
6640 : 48854906 : if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6641 : 23384340 : && 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 : 307746 : && !processing_template_decl
6645 : 307746 : && (complain & tf_warning)
6646 : 301981 : && 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 : 286930 : && !enum_cast_to_int (orig_op0)
6650 : 72526074 : && !enum_cast_to_int (orig_op1))
6651 : : {
6652 : 286900 : 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 : 82936391 : if (! converted)
6662 : : {
6663 : 79670335 : warning_sentinel w (warn_sign_conversion, short_compare);
6664 : 79670335 : if (!same_type_p (TREE_TYPE (op0), result_type))
6665 : 3339063 : op0 = cp_convert_and_check (result_type, op0, complain);
6666 : 79670335 : if (!same_type_p (TREE_TYPE (op1), result_type))
6667 : 14246460 : op1 = cp_convert_and_check (result_type, op1, complain);
6668 : :
6669 : 79670335 : if (op0 == error_mark_node || op1 == error_mark_node)
6670 : 65 : return error_mark_node;
6671 : 79670335 : }
6672 : :
6673 : 82936326 : if (build_type == NULL_TREE)
6674 : 55912409 : build_type = result_type;
6675 : :
6676 : 82936326 : if (doing_shift
6677 : 3265490 : && flag_strong_eval_order == 2
6678 : 3179007 : && TREE_SIDE_EFFECTS (op1)
6679 : 82961778 : && !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 : 25452 : op0 = cp_save_expr (op0);
6684 : 25452 : instrument_expr = op0;
6685 : : }
6686 : :
6687 : 82936326 : if (sanitize_flags_p ((SANITIZE_SHIFT
6688 : : | SANITIZE_DIVIDE
6689 : : | SANITIZE_FLOAT_DIVIDE
6690 : : | SANITIZE_SI_OVERFLOW))
6691 : 12466 : && current_function_decl != NULL_TREE
6692 : 10287 : && !processing_template_decl
6693 : 82946613 : && (doing_div_or_mod || doing_shift))
6694 : : {
6695 : : /* OP0 and/or OP1 might have side-effects. */
6696 : 875 : op0 = cp_save_expr (op0);
6697 : 875 : op1 = cp_save_expr (op1);
6698 : 875 : op0 = fold_non_dependent_expr (op0, complain);
6699 : 875 : op1 = fold_non_dependent_expr (op1, complain);
6700 : 875 : tree instrument_expr1 = NULL_TREE;
6701 : 875 : if (doing_div_or_mod
6702 : 875 : && 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 : 428 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6718 : 347 : instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6719 : 875 : if (instrument_expr != NULL)
6720 : 18 : instrument_expr = add_stmt_to_compound (instrument_expr,
6721 : : instrument_expr1);
6722 : : else
6723 : 857 : instrument_expr = instrument_expr1;
6724 : : }
6725 : :
6726 : 82936326 : result = build2_loc (location, resultcode, build_type, op0, op1);
6727 : 82936326 : if (final_type != 0)
6728 : 4537773 : result = cp_convert (final_type, result, complain);
6729 : :
6730 : 82936326 : if (instrument_expr != NULL)
6731 : 25948 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6732 : : instrument_expr, result);
6733 : :
6734 : 82936326 : if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6735 : 153222 : result = get_target_expr (result, complain);
6736 : :
6737 : 82936326 : if (semantic_result_type)
6738 : 59392 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
6739 : :
6740 : 82936326 : if (!c_inhibit_evaluation_warnings)
6741 : : {
6742 : 75089061 : if (!processing_template_decl)
6743 : : {
6744 : 75089061 : op0 = cp_fully_fold (op0);
6745 : : /* Only consider the second argument if the first isn't overflowed. */
6746 : 75089061 : if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6747 : : return result;
6748 : 19507951 : op1 = cp_fully_fold (op1);
6749 : 19507951 : 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 : 14245298 : tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6757 : 14245298 : 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 : 14642 : build_x_vec_perm_expr (location_t loc,
6768 : : tree arg0, tree arg1, tree arg2,
6769 : : tsubst_flags_t complain)
6770 : : {
6771 : 14642 : tree orig_arg0 = arg0;
6772 : 14642 : tree orig_arg1 = arg1;
6773 : 14642 : tree orig_arg2 = arg2;
6774 : 14642 : 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 : 14636 : tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6782 : 14636 : 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 : 34231 : build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6792 : : tsubst_flags_t complain)
6793 : : {
6794 : 34231 : tree arg0 = (*args)[0];
6795 : 34231 : tree arg1 = (*args)[1];
6796 : 34231 : 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 : 34209 : auto_vec<tree, 16> mask;
6809 : 464469 : for (unsigned i = 2; i < args->length (); ++i)
6810 : : {
6811 : 430260 : tree idx = fold_non_dependent_expr ((*args)[i], complain);
6812 : 430260 : mask.safe_push (idx);
6813 : : }
6814 : 34209 : tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6815 : 34209 : 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 : 34209 : return exp;
6821 : 34209 : }
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 : 4136807 : cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6828 : : tree intop, tsubst_flags_t complain)
6829 : : {
6830 : 4136807 : 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 : 4136807 : complete_type (TREE_TYPE (res_type));
6838 : :
6839 : 4136807 : return pointer_int_sum (loc, resultcode, ptrop,
6840 : 4136807 : 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 : 1234108 : pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6849 : : tsubst_flags_t complain, tree *instrument_expr)
6850 : : {
6851 : 1234108 : tree result, inttype;
6852 : 1234108 : tree restype = ptrdiff_type_node;
6853 : 1234108 : tree target_type = TREE_TYPE (ptrtype);
6854 : :
6855 : 1234108 : if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6856 : 12 : return error_mark_node;
6857 : :
6858 : 1234096 : 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 : 1234096 : 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 : 1234084 : 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 : 2468168 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6883 : 1234084 : TREE_TYPE (TREE_TYPE (op0)),
6884 : : !(complain & tf_error))
6885 : 2468168 : || !verify_type_context (loc, TCTX_POINTER_ARITH,
6886 : 1234084 : 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 : 1234084 : 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 : 1234084 : if (!processing_template_decl
6899 : 1234084 : && 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 : 1234084 : 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 : 1234084 : 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 : 1234084 : 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 : 1234084 : 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 : 2468165 : op1 = (TYPE_PTROB_P (ptrtype)
6945 : 2468165 : ? size_in_bytes_loc (loc, target_type)
6946 : : : integer_one_node);
6947 : :
6948 : : /* Do the division. */
6949 : :
6950 : 1234084 : result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
6951 : : cp_convert (inttype, op1, complain));
6952 : 1234084 : 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 : 55771654 : build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
6961 : : tree lookups, tsubst_flags_t complain)
6962 : : {
6963 : 55771654 : tree orig_expr = xarg;
6964 : 55771654 : tree exp;
6965 : 55771654 : int ptrmem = 0;
6966 : 55771654 : tree overload = NULL_TREE;
6967 : :
6968 : 55771654 : if (processing_template_decl)
6969 : : {
6970 : 38439383 : if (type_dependent_expression_p (xarg))
6971 : : {
6972 : 25489500 : tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
6973 : 25489500 : TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
6974 : 25489500 : return e;
6975 : : }
6976 : : }
6977 : :
6978 : 30282154 : 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 : 30282154 : if (code == ADDR_EXPR
6989 : 3330672 : && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
6990 : 33612424 : && ((CLASS_TYPE_P (TREE_TYPE (xarg))
6991 : 1591815 : && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
6992 : 3327958 : || (TREE_CODE (xarg) == OFFSET_REF)))
6993 : : /* Don't look for a function. */;
6994 : : else
6995 : 30214789 : exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
6996 : : NULL_TREE, lookups, &overload, complain);
6997 : :
6998 : 30282154 : if (!exp && code == ADDR_EXPR)
6999 : : {
7000 : 3330406 : if (is_overloaded_fn (xarg))
7001 : : {
7002 : 363655 : tree fn = get_first_fn (xarg);
7003 : 727310 : 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 : 3330388 : if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
7017 : 3391835 : && (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 : 3330385 : if (TREE_CODE (xarg) == OFFSET_REF)
7045 : : {
7046 : 65038 : ptrmem = PTRMEM_OK_P (xarg);
7047 : :
7048 : 0 : if (!ptrmem && !flag_ms_extensions
7049 : 65038 : && 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 : 6660770 : exp = cp_build_addr_expr_strict (xarg, complain);
7061 : :
7062 : 3330385 : if (TREE_CODE (exp) == PTRMEM_CST)
7063 : 64129 : PTRMEM_CST_LOCATION (exp) = loc;
7064 : : else
7065 : 3266256 : protected_set_expr_location (exp, loc);
7066 : : }
7067 : :
7068 : 30282133 : if (processing_template_decl && exp != error_mark_node)
7069 : : {
7070 : 12949771 : if (overload != NULL_TREE)
7071 : 160834 : return (build_min_non_dep_op_overload
7072 : 160834 : (code, exp, overload, orig_expr, integer_zero_node));
7073 : :
7074 : 12788937 : exp = build_min_non_dep (code, exp, orig_expr,
7075 : : /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
7076 : : }
7077 : 30121299 : if (TREE_CODE (exp) == ADDR_EXPR)
7078 : 2556656 : 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 : 456210 : cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7087 : : {
7088 : 456210 : tree orig_expr = arg;
7089 : :
7090 : 456210 : if (processing_template_decl)
7091 : : {
7092 : 172217 : if (type_dependent_expression_p (arg))
7093 : 172217 : return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7094 : : }
7095 : :
7096 : 567986 : tree exp = cp_build_addr_expr_strict (arg, complain);
7097 : :
7098 : 283993 : 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 : 7046276 : cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7109 : : {
7110 : 7046276 : tree type = TREE_TYPE (expr);
7111 : 7046276 : location_t loc = cp_expr_loc_or_input_loc (expr);
7112 : 5969626 : if (TYPE_PTR_OR_PTRMEM_P (type)
7113 : : /* Avoid ICE on invalid use of non-static member function. */
7114 : 13015624 : || TREE_CODE (expr) == FUNCTION_DECL)
7115 : 1076928 : return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
7116 : : else
7117 : 5969348 : return c_common_truthvalue_conversion (loc, expr);
7118 : : }
7119 : :
7120 : : /* Returns EXPR contextually converted to bool. */
7121 : :
7122 : : tree
7123 : 45147283 : contextual_conv_bool (tree expr, tsubst_flags_t complain)
7124 : : {
7125 : 45147283 : return perform_implicit_conversion_flags (boolean_type_node, expr,
7126 : 45147283 : 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 : 40385568 : condition_conversion (tree expr)
7134 : : {
7135 : 40385568 : tree t = contextual_conv_bool (expr, tf_warning_or_error);
7136 : 40385568 : if (!processing_template_decl)
7137 : 20668079 : t = fold_build_cleanup_point_expr (boolean_type_node, t);
7138 : 40385568 : 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 : 267133227 : build_address (tree t)
7147 : : {
7148 : 267133227 : if (error_operand_p (t) || !cxx_mark_addressable (t))
7149 : 21 : return error_mark_node;
7150 : 267133206 : gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7151 : : || processing_template_decl);
7152 : 267133206 : t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7153 : 267133206 : if (TREE_CODE (t) != ADDR_EXPR)
7154 : 47004430 : t = rvalue (t);
7155 : : return t;
7156 : : }
7157 : :
7158 : : /* Return a NOP_EXPR converting EXPR to TYPE. */
7159 : :
7160 : : tree
7161 : 449001349 : build_nop (tree type, tree expr MEM_STAT_DECL)
7162 : : {
7163 : 449001349 : if (type == error_mark_node || error_operand_p (expr))
7164 : : return expr;
7165 : 449001273 : 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 : 205052580 : cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7177 : : {
7178 : 205052580 : tree argtype;
7179 : 205052580 : tree val;
7180 : :
7181 : 205052580 : if (!arg || error_operand_p (arg))
7182 : 12 : return error_mark_node;
7183 : :
7184 : 205052568 : arg = mark_lvalue_use (arg);
7185 : 205052568 : if (error_operand_p (arg))
7186 : 6 : return error_mark_node;
7187 : :
7188 : 205052562 : argtype = lvalue_type (arg);
7189 : 205052562 : location_t loc = cp_expr_loc_or_input_loc (arg);
7190 : :
7191 : 205052562 : gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7192 : :
7193 : 12594880 : if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7194 : 205052756 : && !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 : 205052562 : if (type_unknown_p (arg))
7263 : 1904 : return build1 (ADDR_EXPR, unknown_type_node, arg);
7264 : :
7265 : 205050658 : 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 : 64248 : goto offset_ref;
7269 : :
7270 : : /* Anything not already handled and not a true memory reference
7271 : : is an error. */
7272 : 204986410 : if (!FUNC_OR_METHOD_TYPE_P (argtype))
7273 : : {
7274 : 107232056 : cp_lvalue_kind kind = lvalue_kind (arg);
7275 : 107232056 : 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 : 107232033 : 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 : 204986378 : if (TYPE_REF_P (argtype))
7291 : : {
7292 : 231 : tree type = build_pointer_type (TREE_TYPE (argtype));
7293 : 231 : arg = build1 (CONVERT_EXPR, type, arg);
7294 : 231 : return arg;
7295 : : }
7296 : 204986147 : 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 : 204986147 : if (INDIRECT_REF_P (arg))
7310 : : {
7311 : 67886327 : arg = TREE_OPERAND (arg, 0);
7312 : 67886327 : if (TYPE_REF_P (TREE_TYPE (arg)))
7313 : : {
7314 : 39689522 : tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7315 : 39689522 : arg = build1 (CONVERT_EXPR, type, arg);
7316 : : }
7317 : : else
7318 : : /* Don't let this be an lvalue. */
7319 : 28196805 : arg = rvalue (arg);
7320 : 67886327 : return arg;
7321 : : }
7322 : :
7323 : : /* Handle complex lvalues (when permitted)
7324 : : by reduction to simpler cases. */
7325 : 137099820 : val = unary_complex_lvalue (ADDR_EXPR, arg);
7326 : 137099820 : if (val != 0)
7327 : : return val;
7328 : :
7329 : 136626861 : 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 : 63415 : break;
7337 : :
7338 : 63415 : case BASELINK:
7339 : 63415 : arg = BASELINK_FUNCTIONS (arg);
7340 : : /* Fall through. */
7341 : :
7342 : 63415 : case OVERLOAD:
7343 : 63415 : arg = OVL_FIRST (arg);
7344 : : break;
7345 : :
7346 : 64248 : case OFFSET_REF:
7347 : 64248 : offset_ref:
7348 : : /* Turn a reference to a non-static data member into a
7349 : : pointer-to-member. */
7350 : 64248 : {
7351 : 64248 : tree type;
7352 : 64248 : tree t;
7353 : :
7354 : 64248 : gcc_assert (PTRMEM_OK_P (arg));
7355 : :
7356 : 64248 : t = TREE_OPERAND (arg, 1);
7357 : 64248 : 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 : 64233 : if (TREE_CODE (t) == FUNCTION_DECL
7367 : 61569 : && !DECL_PURE_VIRTUAL_P (t)
7368 : 125781 : && !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 : 64230 : if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7375 : : {
7376 : : arg = t;
7377 : : break;
7378 : : }
7379 : :
7380 : 64174 : type = build_ptrmem_type (context_for_name_lookup (t),
7381 : 64174 : TREE_TYPE (t));
7382 : 64174 : t = make_ptrmem_cst (type, t);
7383 : 64174 : return t;
7384 : : }
7385 : :
7386 : : default:
7387 : : break;
7388 : : }
7389 : :
7390 : 136626917 : if (argtype != error_mark_node)
7391 : 136626917 : argtype = build_pointer_type (argtype);
7392 : :
7393 : 136626917 : 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 : 136626884 : if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7403 : : {
7404 : 124324943 : if (!mark_single_function (arg, complain))
7405 : 3 : return error_mark_node;
7406 : 124324940 : val = build_address (arg);
7407 : 124324940 : if (TREE_CODE (arg) == OFFSET_REF)
7408 : 0 : PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7409 : : }
7410 : 12301941 : 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 : 12301914 : tree object = TREE_OPERAND (arg, 0);
7432 : 12301914 : tree field = TREE_OPERAND (arg, 1);
7433 : 12301914 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7434 : : (TREE_TYPE (object), decl_type_context (field)));
7435 : 12301914 : val = build_address (arg);
7436 : : }
7437 : :
7438 : 136626881 : if (TYPE_PTR_P (argtype)
7439 : 136626881 : && 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 : 136626881 : if (TREE_CODE (val) == ADDR_EXPR
7449 : 136626881 : && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7450 : 96378246 : 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 : 201438202 : cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7459 : : {
7460 : 201438202 : 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 : 3614378 : cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7467 : : {
7468 : 3614378 : 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 : 27112233 : 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 : 27112233 : tree arg = xarg;
7485 : 27112233 : location_t location = cp_expr_loc_or_input_loc (arg);
7486 : 27112233 : tree argtype = 0;
7487 : 27112233 : tree eptype = NULL_TREE;
7488 : 27112233 : const char *errstring = NULL;
7489 : 27112233 : tree val;
7490 : 27112233 : const char *invalid_op_diag;
7491 : :
7492 : 27112233 : if (!arg || error_operand_p (arg))
7493 : 0 : return error_mark_node;
7494 : :
7495 : 27112233 : arg = resolve_nondeduced_context (arg, complain);
7496 : :
7497 : 54224466 : if ((invalid_op_diag
7498 : 27112233 : = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7499 : : ? CONVERT_EXPR
7500 : : : code),
7501 : 27112233 : 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 : 27112233 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7509 : : {
7510 : 9444 : eptype = TREE_TYPE (arg);
7511 : 9444 : arg = TREE_OPERAND (arg, 0);
7512 : : }
7513 : :
7514 : 27112233 : switch (code)
7515 : : {
7516 : 2367952 : case UNARY_PLUS_EXPR:
7517 : 2367952 : case NEGATE_EXPR:
7518 : 2367952 : {
7519 : 2367952 : int flags = WANT_ARITH | WANT_ENUM;
7520 : : /* Unary plus (but not unary minus) is allowed on pointers. */
7521 : 2367952 : if (code == UNARY_PLUS_EXPR)
7522 : 163920 : flags |= WANT_POINTER;
7523 : 2367952 : arg = build_expr_type_conversion (flags, arg, true);
7524 : 2367952 : 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 : 2367922 : if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7531 : 593947 : 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 : 2367922 : arg = rvalue (arg);
7536 : : }
7537 : : }
7538 : : break;
7539 : :
7540 : 844522 : case BIT_NOT_EXPR:
7541 : 844522 : 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 : 844507 : 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 : 844494 : else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7556 : : {
7557 : : /* Warn if the expression has boolean value. */
7558 : 844259 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7559 : 844259 : && (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 : 844259 : 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 : 15401437 : case TRUTH_NOT_EXPR:
7596 : 15401437 : 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 : 15401377 : arg = perform_implicit_conversion (boolean_type_node, arg,
7600 : : complain);
7601 : 15401377 : if (arg != error_mark_node)
7602 : : {
7603 : 15401366 : if (processing_template_decl)
7604 : 7044171 : return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
7605 : 8357195 : val = invert_truthvalue_loc (location, arg);
7606 : 8357195 : if (obvalue_p (val))
7607 : 13007 : val = non_lvalue_loc (location, val);
7608 : 8357195 : return val;
7609 : : }
7610 : 11 : errstring = _("in argument to unary !");
7611 : 11 : break;
7612 : :
7613 : : case NOP_EXPR:
7614 : : break;
7615 : :
7616 : 462251 : case REALPART_EXPR:
7617 : 462251 : case IMAGPART_EXPR:
7618 : 462251 : val = build_real_imag_expr (input_location, code, arg);
7619 : 462251 : 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 : 7459903 : case PREINCREMENT_EXPR:
7625 : 7459903 : case POSTINCREMENT_EXPR:
7626 : 7459903 : case PREDECREMENT_EXPR:
7627 : 7459903 : case POSTDECREMENT_EXPR:
7628 : : /* Handle complex lvalues (when permitted)
7629 : : by reduction to simpler cases. */
7630 : :
7631 : 7459903 : val = unary_complex_lvalue (code, arg);
7632 : 7459903 : if (val != 0)
7633 : 30 : goto return_build_unary_op;
7634 : :
7635 : 7459873 : 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 : 7459873 : 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 : 7459855 : 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 : 7459819 : else if (arg == error_mark_node)
7669 : : return error_mark_node;
7670 : :
7671 : : /* Report something read-only. */
7672 : :
7673 : 7459819 : if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7674 : 7459819 : || 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 : 7459819 : {
7686 : 7459819 : tree inc;
7687 : 7459819 : tree declared_type = unlowered_expr_type (arg);
7688 : :
7689 : 7459819 : argtype = TREE_TYPE (arg);
7690 : :
7691 : : /* ARM $5.2.5 last annotation says this should be forbidden. */
7692 : 7459819 : 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 : 7459819 : if (TYPE_PTR_P (argtype))
7706 : : {
7707 : 2075353 : tree type = complete_type (TREE_TYPE (argtype));
7708 : :
7709 : 2075353 : 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 : 2075342 : 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 : 2075267 : else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7737 : 2075267 : TREE_TYPE (argtype),
7738 : : !(complain & tf_error)))
7739 : 0 : return error_mark_node;
7740 : :
7741 : 2075339 : inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7742 : : }
7743 : : else
7744 : 5384323 : inc = VECTOR_TYPE_P (argtype)
7745 : 5384466 : ? build_one_cst (argtype)
7746 : : : integer_one_node;
7747 : :
7748 : 7459805 : 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 : 7459805 : 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 : 7459805 : if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7759 : 7459805 : || 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 : 7459423 : if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7768 : 7459772 : && (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 : 7459772 : 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 : 7459677 : else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7808 : : /* An rvalue has no cv-qualifiers. */
7809 : 1036131 : val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7810 : : else
7811 : 6423546 : val = build2 (code, TREE_TYPE (arg), arg, inc);
7812 : :
7813 : 7459706 : TREE_SIDE_EFFECTS (val) = 1;
7814 : 7459706 : goto return_build_unary_op;
7815 : : }
7816 : :
7817 : 576168 : case ADDR_EXPR:
7818 : : /* Note that this operation never does default_conversion
7819 : : regardless of NOCONVERT. */
7820 : 576168 : return cp_build_addr_expr (arg, complain);
7821 : :
7822 : : default:
7823 : : break;
7824 : : }
7825 : :
7826 : 3212506 : if (!errstring)
7827 : : {
7828 : 3212431 : if (argtype == 0)
7829 : 3212431 : argtype = TREE_TYPE (arg);
7830 : 3212431 : val = build1 (code, argtype, arg);
7831 : 10672182 : return_build_unary_op:
7832 : 10672182 : if (eptype)
7833 : 9435 : val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
7834 : 10672182 : 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 : 4141 : build_unary_op (location_t /*location*/,
7845 : : enum tree_code code, tree xarg, bool noconvert)
7846 : : {
7847 : 4141 : 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 : 373349 : genericize_compound_lvalue (tree lvalue)
7860 : : {
7861 : 373349 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7862 : 190 : lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7863 : 190 : cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7864 : 190 : TREE_OPERAND (lvalue, 1));
7865 : 373349 : return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7866 : 373349 : 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 : 254137504 : 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 : 254510647 : if (processing_template_decl)
7883 : : return NULL_TREE;
7884 : :
7885 : : /* Handle (a, b) used as an "lvalue". */
7886 : 176745981 : if (TREE_CODE (arg) == COMPOUND_EXPR)
7887 : : {
7888 : 373582 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7889 : : tf_warning_or_error);
7890 : 373582 : return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7891 : 747164 : TREE_OPERAND (arg, 0), real_result);
7892 : : }
7893 : :
7894 : : /* Handle (a ? b : c) used as an "lvalue". */
7895 : 176372399 : if (TREE_CODE (arg) == COND_EXPR
7896 : 176273000 : || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7897 : 99399 : return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7898 : :
7899 : : /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7900 : 176273000 : if (TREE_CODE (arg) == MODIFY_EXPR
7901 : 175900034 : || TREE_CODE (arg) == PREINCREMENT_EXPR
7902 : 175899950 : || TREE_CODE (arg) == PREDECREMENT_EXPR)
7903 : 373143 : return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7904 : :
7905 : 175899857 : if (code != ADDR_EXPR)
7906 : : return NULL_TREE;
7907 : :
7908 : : /* Handle (a = b) used as an "lvalue" for `&'. */
7909 : 172887705 : if (TREE_CODE (arg) == MODIFY_EXPR
7910 : 172887705 : || 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 : 268434470 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7921 : 268433428 : || 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 : 95545723 : {
7927 : 95545723 : tree targ = arg;
7928 : :
7929 : 95545723 : if (TREE_CODE (targ) == SAVE_EXPR)
7930 : 0 : targ = TREE_OPERAND (targ, 0);
7931 : :
7932 : 95545723 : if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
7933 : : {
7934 : 46 : if (TREE_CODE (arg) == SAVE_EXPR)
7935 : : targ = arg;
7936 : : else
7937 : 46 : targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
7938 : 46 : return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
7939 : : }
7940 : :
7941 : 95545677 : 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 : 281292016 : cxx_mark_addressable (tree exp, bool array_ref_p)
7962 : : {
7963 : 281292016 : tree x = exp;
7964 : :
7965 : 333256074 : while (1)
7966 : 333256074 : switch (TREE_CODE (x))
7967 : : {
7968 : 26044140 : case VIEW_CONVERT_EXPR:
7969 : 26044140 : if (array_ref_p
7970 : 716134 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7971 : 26593266 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
7972 : : return true;
7973 : 26041236 : x = TREE_OPERAND (x, 0);
7974 : 26041236 : break;
7975 : :
7976 : 25682551 : case COMPONENT_REF:
7977 : 25682551 : if (bitfield_p (x))
7978 : 9 : error ("attempt to take address of bit-field");
7979 : : /* FALLTHRU */
7980 : 25922822 : case ADDR_EXPR:
7981 : 25922822 : case ARRAY_REF:
7982 : 25922822 : case REALPART_EXPR:
7983 : 25922822 : case IMAGPART_EXPR:
7984 : 25922822 : x = TREE_OPERAND (x, 0);
7985 : 25922822 : break;
7986 : :
7987 : 5614070 : case PARM_DECL:
7988 : 5614070 : 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 : 54205604 : case VAR_DECL:
7997 : : /* Caller should not be trying to mark initialized
7998 : : constant fields addressable. */
7999 : 54205604 : 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 : 54255825 : case RESULT_DECL:
8006 : 54255865 : if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
8007 : 54255859 : && !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 : 54255813 : TREE_ADDRESSABLE (x) = 1;
8020 : 54255813 : return true;
8021 : :
8022 : 139163010 : case CONST_DECL:
8023 : 139163010 : case FUNCTION_DECL:
8024 : 139163010 : TREE_ADDRESSABLE (x) = 1;
8025 : 139163010 : return true;
8026 : :
8027 : 50726 : case CONSTRUCTOR:
8028 : 50726 : TREE_ADDRESSABLE (x) = 1;
8029 : 50726 : return true;
8030 : :
8031 : 12052546 : case TARGET_EXPR:
8032 : 12052546 : TREE_ADDRESSABLE (x) = 1;
8033 : 12052546 : cxx_mark_addressable (TARGET_EXPR_SLOT (x));
8034 : 12052546 : 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 : 5954063 : build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
8045 : : tsubst_flags_t complain)
8046 : : {
8047 : 5954063 : tree orig_ifexp = ifexp;
8048 : 5954063 : tree orig_op1 = op1;
8049 : 5954063 : tree orig_op2 = op2;
8050 : 5954063 : tree expr;
8051 : :
8052 : 5954063 : 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 : 2572876 : if (type_dependent_expression_p (ifexp)
8058 : : /* As a GNU extension, the middle operand may be omitted. */
8059 : 1263576 : || (op1 && type_dependent_expression_p (op1))
8060 : 3397879 : || type_dependent_expression_p (op2))
8061 : 1772994 : return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
8062 : : }
8063 : :
8064 : 4181069 : expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
8065 : 4181069 : if (processing_template_decl && expr != error_mark_node)
8066 : : {
8067 : 799876 : tree min = build_min_non_dep (COND_EXPR, expr,
8068 : : orig_ifexp, orig_op1, orig_op2);
8069 : 799876 : 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 : 29763351 : build_x_compound_expr_from_list (tree list, expr_list_kind exp,
8079 : : tsubst_flags_t complain)
8080 : : {
8081 : 29763351 : tree expr = TREE_VALUE (list);
8082 : :
8083 : 246010 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8084 : 30009355 : && !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 : 29763351 : 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 : 128431 : build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8130 : : tsubst_flags_t complain)
8131 : : {
8132 : 128431 : if (vec_safe_is_empty (vec))
8133 : : return NULL_TREE;
8134 : 128431 : else if (vec->length () == 1)
8135 : 128366 : 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 : 2561136 : build_x_compound_expr (location_t loc, tree op1, tree op2,
8165 : : tree lookups, tsubst_flags_t complain)
8166 : : {
8167 : 2561136 : tree result;
8168 : 2561136 : tree orig_op1 = op1;
8169 : 2561136 : tree orig_op2 = op2;
8170 : 2561136 : tree overload = NULL_TREE;
8171 : :
8172 : 2561136 : if (processing_template_decl)
8173 : : {
8174 : 2444993 : if (type_dependent_expression_p (op1)
8175 : 2444993 : || type_dependent_expression_p (op2))
8176 : : {
8177 : 2169971 : result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8178 : 2169971 : TREE_TYPE (result)
8179 : 2169971 : = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
8180 : 2169971 : return result;
8181 : : }
8182 : : }
8183 : :
8184 : 391165 : result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8185 : : NULL_TREE, lookups, &overload, complain);
8186 : 391165 : if (!result)
8187 : 384276 : result = cp_build_compound_expr (op1, op2, complain);
8188 : :
8189 : 391165 : if (processing_template_decl && result != error_mark_node)
8190 : : {
8191 : 273201 : 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 : 273149 : 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 : 10737 : build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8205 : : {
8206 : 10737 : return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8207 : : }
8208 : :
8209 : : /* Build a compound expression. */
8210 : :
8211 : : tree
8212 : 496664 : cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8213 : : {
8214 : 496664 : lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8215 : :
8216 : 496664 : if (lhs == error_mark_node || rhs == error_mark_node)
8217 : : return error_mark_node;
8218 : :
8219 : 496661 : if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8220 : 0 : lhs = TREE_OPERAND (lhs, 0);
8221 : 496661 : tree eptype = NULL_TREE;
8222 : 496661 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8223 : : {
8224 : 3 : eptype = TREE_TYPE (rhs);
8225 : 3 : rhs = TREE_OPERAND (rhs, 0);
8226 : : }
8227 : :
8228 : 496661 : 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 : 2857 : tree init = TARGET_EXPR_INITIAL (rhs);
8234 : :
8235 : 2857 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8236 : 2857 : TARGET_EXPR_INITIAL (rhs) = init;
8237 : :
8238 : 2857 : if (eptype)
8239 : 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8240 : 2857 : return rhs;
8241 : : }
8242 : :
8243 : 493804 : rhs = resolve_nondeduced_context (rhs, complain);
8244 : :
8245 : 493804 : 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 : 493771 : tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8254 : 493771 : 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 : 676333 : 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 : 676333 : if (cast == CAST_EXPR && !warn_cast_qual)
8276 : : return false;
8277 : :
8278 : 548598 : 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 : 43018653 : maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8312 : : tsubst_flags_t complain)
8313 : : {
8314 : 43018653 : 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 : 43018653 : }
8328 : :
8329 : : /* Warns if the cast ignores cv-qualifiers on TYPE. */
8330 : : static void
8331 : 43009436 : maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8332 : : tsubst_flags_t complain)
8333 : : {
8334 : 43009436 : if (warn_ignored_qualifiers
8335 : 258366 : && complain & tf_warning
8336 : 257994 : && !CLASS_TYPE_P (type)
8337 : 43263004 : && (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 : 43009436 : }
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 : 29556 : convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8351 : : bool c_cast_p, tsubst_flags_t complain)
8352 : : {
8353 : 29556 : if (same_type_p (type, TREE_TYPE (expr)))
8354 : : return expr;
8355 : :
8356 : 29556 : 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 : 29192 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8387 : 29192 : 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 : 42656964 : 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 : 42656964 : tree intype;
8402 : 42656964 : tree result;
8403 : 42656964 : cp_lvalue_kind clk;
8404 : :
8405 : : /* Assume the cast is valid. */
8406 : 42656964 : *valid_p = true;
8407 : :
8408 : 42656964 : intype = unlowered_expr_type (expr);
8409 : :
8410 : : /* Save casted types in the function's used types hash table. */
8411 : 42656964 : used_types_insert (type);
8412 : :
8413 : : /* A prvalue of non-class type is cv-unqualified. */
8414 : 42656964 : if (!CLASS_TYPE_P (type))
8415 : 41013722 : 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 : 42656964 : if (TYPE_REF_P (type)
8437 : 3271574 : && CLASS_TYPE_P (TREE_TYPE (type))
8438 : 2628130 : && CLASS_TYPE_P (intype)
8439 : 2628115 : && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8440 : 2626790 : && DERIVED_FROM_P (intype, TREE_TYPE (type))
8441 : 2596108 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8442 : 2596108 : build_pointer_type (TYPE_MAIN_VARIANT
8443 : : (TREE_TYPE (type))),
8444 : : complain)
8445 : 45253072 : && (c_cast_p
8446 : 2596084 : || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8447 : : {
8448 : 2596108 : tree base;
8449 : :
8450 : 2596108 : 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 : 4847066 : base = lookup_base (TREE_TYPE (type), intype,
8460 : : c_cast_p ? ba_unique : ba_check,
8461 : : NULL, complain);
8462 : 2423545 : expr = cp_build_addr_expr (expr, complain);
8463 : :
8464 : 2423545 : if (sanitize_flags_p (SANITIZE_VPTR))
8465 : : {
8466 : 431 : tree ubsan_check
8467 : 431 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8468 : : intype, expr);
8469 : 431 : if (ubsan_check)
8470 : 2423545 : 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 : 2423545 : 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 : 2423545 : 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 : 2423545 : if (sanitize_flags_p (SANITIZE_NULL)
8494 : 437 : && TREE_CODE (expr) == COND_EXPR
8495 : 6 : && TREE_OPERAND (expr, 2)
8496 : 6 : && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8497 : 2423551 : && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8498 : 6 : ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8499 : :
8500 : 2423545 : 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 : 40060856 : if (TYPE_REF_P (type)
8506 : 675466 : && TYPE_REF_IS_RVALUE (type)
8507 : 414510 : && (clk = real_lvalue_p (expr))
8508 : 411332 : && reference_compatible_p (TREE_TYPE (type), intype)
8509 : 40472185 : && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8510 : : {
8511 : 411329 : if (processing_template_decl)
8512 : : return expr;
8513 : 411108 : 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 : 411105 : tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8520 : 411105 : result = (perform_direct_initialization_if_possible
8521 : 411105 : (lref, expr, c_cast_p, complain));
8522 : 411105 : result = build1 (NON_LVALUE_EXPR, type, result);
8523 : 411105 : 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 : 39649530 : 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 : 39649530 : if (VOID_TYPE_P (type))
8542 : : {
8543 : 157081 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8544 : 0 : expr = TREE_OPERAND (expr, 0);
8545 : 157081 : 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 : 39492449 : 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 : 39492443 : 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 : 39492443 : if (result == NULL_TREE
8566 : 3921493 : && cxx_dialect >= cxx20
8567 : 1029308 : && 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 : 3921493 : if (result)
8577 : : {
8578 : 35570965 : if (processing_template_decl)
8579 : : return expr;
8580 : :
8581 : 34917427 : 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 : 34917427 : if (!TYPE_REF_P (type))
8588 : : {
8589 : 34653384 : result = rvalue (result);
8590 : :
8591 : 34653384 : if (result == expr && SCALAR_TYPE_P (type))
8592 : : /* Leave some record of the cast. */
8593 : 1885669 : result = build_nop (type, expr);
8594 : : }
8595 : 34917427 : 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 : 3921478 : if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8619 : 2253524 : || SCALAR_FLOAT_TYPE_P (type))
8620 : 1825314 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8621 : 261615 : || SCALAR_FLOAT_TYPE_P (intype)))
8622 : : {
8623 : 1721047 : if (processing_template_decl)
8624 : : return expr;
8625 : 1648791 : 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 : 1648791 : expr = decay_conversion (expr, complain);
8634 : 1648791 : return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8635 : : }
8636 : :
8637 : 705017 : if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8638 : 700684 : && CLASS_TYPE_P (TREE_TYPE (type))
8639 : 199194 : && CLASS_TYPE_P (TREE_TYPE (intype))
8640 : 2238476 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8641 : : (TREE_TYPE (intype))),
8642 : 38045 : build_pointer_type (TYPE_MAIN_VARIANT
8643 : : (TREE_TYPE (type))),
8644 : : complain))
8645 : : {
8646 : 37706 : tree base;
8647 : :
8648 : 37706 : if (processing_template_decl)
8649 : : return expr;
8650 : :
8651 : 37688 : if (!c_cast_p
8652 : 37688 : && check_for_casting_away_constness (loc, intype, type,
8653 : : STATIC_CAST_EXPR,
8654 : : complain))
8655 : 6 : return error_mark_node;
8656 : 74825 : base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8657 : : c_cast_p ? ba_unique : ba_check,
8658 : : NULL, complain);
8659 : 37682 : expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8660 : : complain);
8661 : :
8662 : 37682 : 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 : 37682 : expr = ubsan_check;
8669 : : }
8670 : :
8671 : 37682 : return cp_fold_convert (type, expr);
8672 : : }
8673 : :
8674 : 89 : if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8675 : 2162725 : || (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 : 2162571 : if (TYPE_PTR_P (intype)
8720 : 765270 : && VOID_TYPE_P (TREE_TYPE (intype))
8721 : 2798882 : && TYPE_PTROB_P (type))
8722 : : {
8723 : 604773 : if (!c_cast_p
8724 : 604773 : && check_for_casting_away_constness (loc, intype, type,
8725 : : STATIC_CAST_EXPR,
8726 : : complain))
8727 : 21 : return error_mark_node;
8728 : 604752 : if (processing_template_decl)
8729 : : return expr;
8730 : 518142 : return build_nop (type, expr);
8731 : : }
8732 : :
8733 : 1557798 : *valid_p = false;
8734 : 1557798 : return error_mark_node;
8735 : : }
8736 : :
8737 : : /* Return an expression representing static_cast<TYPE>(EXPR). */
8738 : :
8739 : : tree
8740 : 12207553 : build_static_cast (location_t loc, tree type, tree oexpr,
8741 : : tsubst_flags_t complain)
8742 : : {
8743 : 12207553 : tree expr = oexpr;
8744 : 12207553 : tree result;
8745 : 12207553 : bool valid_p;
8746 : :
8747 : 12207553 : if (type == error_mark_node || expr == error_mark_node)
8748 : : return error_mark_node;
8749 : :
8750 : 12207499 : bool dependent = (dependent_type_p (type)
8751 : 12207499 : || type_dependent_expression_p (expr));
8752 : 8140679 : if (dependent)
8753 : : {
8754 : 5052063 : tmpl:
8755 : 5052063 : expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8756 : : /* We don't know if it will or will not have side effects. */
8757 : 5052063 : TREE_SIDE_EFFECTS (expr) = 1;
8758 : 5052063 : result = convert_from_reference (expr);
8759 : 5052063 : protected_set_expr_location (result, loc);
8760 : 5052063 : 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 : 8140679 : if (!TYPE_REF_P (type)
8766 : 4869570 : && TREE_CODE (expr) == NOP_EXPR
8767 : 8234466 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8768 : 48 : expr = TREE_OPERAND (expr, 0);
8769 : :
8770 : 8140679 : result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8771 : : &valid_p, complain);
8772 : 8140679 : if (valid_p)
8773 : : {
8774 : 8140483 : if (result != error_mark_node)
8775 : : {
8776 : 8140378 : maybe_warn_about_useless_cast (loc, type, expr, complain);
8777 : 8140378 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8778 : : }
8779 : 8140483 : if (processing_template_decl)
8780 : 985243 : goto tmpl;
8781 : 7155240 : protected_set_expr_location (result, loc);
8782 : 7155240 : 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 : 94930 : build_nop_reinterpret (tree type, tree expr)
8856 : : {
8857 : 94930 : tree ret = build_nop (type, expr);
8858 : 94930 : if (ret != expr)
8859 : 94930 : REINTERPRET_CAST_P (ret) = true;
8860 : 94930 : 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 : 1676551 : 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 : 1676551 : tree intype;
8874 : :
8875 : : /* Assume the cast is invalid. */
8876 : 1676551 : if (valid_p)
8877 : 1557658 : *valid_p = true;
8878 : :
8879 : 1676551 : if (type == error_mark_node || error_operand_p (expr))
8880 : : return error_mark_node;
8881 : :
8882 : 1676551 : intype = TREE_TYPE (expr);
8883 : :
8884 : : /* Save casted types in the function's used types hash table. */
8885 : 1676551 : used_types_insert (type);
8886 : :
8887 : : /* A prvalue of non-class type is cv-unqualified. */
8888 : 1676551 : if (!CLASS_TYPE_P (type))
8889 : 1676542 : 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 : 1676551 : if (TYPE_REF_P (type))
8899 : : {
8900 : 10001 : 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 : 9992 : if (TYPE_PTR_P (intype)
8913 : 3 : && (complain & tf_warning)
8914 : 9995 : && (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 : 9992 : expr = cp_build_addr_expr (expr, complain);
8920 : :
8921 : 9992 : if (warn_strict_aliasing > 2)
8922 : 69 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8923 : :
8924 : 9992 : if (expr != error_mark_node)
8925 : 9992 : expr = build_reinterpret_cast_1
8926 : 9992 : (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
8927 : : valid_p, complain);
8928 : 9992 : if (expr != error_mark_node)
8929 : : /* cp_build_indirect_ref isn't right for rvalue refs. */
8930 : 9989 : expr = convert_from_reference (fold_convert (type, expr));
8931 : 9992 : 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 : 1666362 : || TREE_CODE (intype) == METHOD_TYPE)
8941 : 276 : && TYPE_PTR_P (type)
8942 : 1666747 : && (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 : 1666362 : 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 : 1666362 : if (TREE_CODE (expr) == NOP_EXPR
8954 : 1666362 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8955 : 31 : expr = TREE_OPERAND (expr, 0);
8956 : :
8957 : 1666362 : if (error_operand_p (expr))
8958 : 30 : return error_mark_node;
8959 : :
8960 : 1666332 : 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 : 1666332 : if (CP_INTEGRAL_TYPE_P (type)
8968 : 144037 : && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
8969 : : {
8970 : 142312 : 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 : 142306 : 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 : 1524020 : else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
8985 : : /* OK */
8986 : : ;
8987 : 1488416 : else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8988 : 1486676 : || TYPE_PTR_OR_PTRMEM_P (type))
8989 : 1583852 : && same_type_p (type, intype))
8990 : : /* DR 799 */
8991 : 474 : return rvalue (expr);
8992 : 1487942 : else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
8993 : : {
8994 : 6430 : if ((complain & tf_warning)
8995 : 12860 : && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
8996 : 6430 : 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 : 6430 : return build_nop_reinterpret (type, expr);
9001 : : }
9002 : 1481512 : 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 : 1481433 : || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
9015 : : {
9016 : 88166 : if (!c_cast_p
9017 : 88166 : && 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 : 88145 : 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 : 88193 : && 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 : 88145 : if (warn_strict_aliasing <= 2)
9035 : : /* strict_aliasing_warning STRIP_NOPs its expr. */
9036 : 76624 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9037 : :
9038 : 88145 : return build_nop_reinterpret (type, expr);
9039 : : }
9040 : 297 : else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
9041 : 1393427 : || (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 : 1392991 : else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
9052 : 1391216 : return convert_to_vector (type, rvalue (expr));
9053 : 1775 : else if (gnu_vector_type_p (intype)
9054 : 1775 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9055 : 1675 : 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 : 177812 : expr = cp_convert (type, expr, complain);
9067 : 177812 : 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 : 536661 : build_reinterpret_cast (location_t loc, tree type, tree expr,
9075 : : tsubst_flags_t complain)
9076 : : {
9077 : 536661 : tree r;
9078 : :
9079 : 536661 : if (type == error_mark_node || expr == error_mark_node)
9080 : : return error_mark_node;
9081 : :
9082 : 536654 : if (processing_template_decl)
9083 : : {
9084 : 427697 : tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
9085 : :
9086 : 427697 : if (!TREE_SIDE_EFFECTS (t)
9087 : 427697 : && type_dependent_expression_p (expr))
9088 : : /* There might turn out to be side effects inside expr. */
9089 : 192960 : TREE_SIDE_EFFECTS (t) = 1;
9090 : 427697 : r = convert_from_reference (t);
9091 : 427697 : protected_set_expr_location (r, loc);
9092 : 427697 : return r;
9093 : : }
9094 : :
9095 : 108957 : r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9096 : : /*valid_p=*/NULL, complain);
9097 : 108957 : if (r != error_mark_node)
9098 : : {
9099 : 108894 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9100 : 108894 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9101 : : }
9102 : 108957 : protected_set_expr_location (r, loc);
9103 : 108957 : 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 : 34760578 : build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9115 : : tsubst_flags_t complain, bool *valid_p)
9116 : : {
9117 : 34760578 : tree src_type;
9118 : 34760578 : tree reference_type;
9119 : :
9120 : : /* Callers are responsible for handling error_mark_node as a
9121 : : destination type. */
9122 : 34760578 : 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 : 34760578 : gcc_assert (!processing_template_decl);
9126 : :
9127 : : /* Assume the conversion is invalid. */
9128 : 34760578 : if (valid_p)
9129 : 34631826 : *valid_p = false;
9130 : :
9131 : 34760578 : if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9132 : : {
9133 : 34134480 : 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 : 34134480 : return error_mark_node;
9138 : : }
9139 : :
9140 : 626098 : 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 : 622152 : dst_type = cv_unqualified (dst_type);
9151 : :
9152 : : /* Save casted types in the function's used types hash table. */
9153 : 622152 : used_types_insert (dst_type);
9154 : :
9155 : 622152 : src_type = TREE_TYPE (expr);
9156 : : /* Expressions do not really have reference types. */
9157 : 622152 : 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 : 622152 : if (TYPE_REF_P (dst_type))
9176 : : {
9177 : 61486 : reference_type = dst_type;
9178 : 61486 : if (!TYPE_REF_IS_RVALUE (dst_type)
9179 : 61486 : ? lvalue_p (expr)
9180 : 646 : : 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 : 61451 : dst_type = build_pointer_type (TREE_TYPE (dst_type));
9191 : 61451 : src_type = build_pointer_type (src_type);
9192 : : }
9193 : : else
9194 : : {
9195 : 560666 : 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 : 560666 : src_type = type_decays_to (src_type);
9200 : 560666 : if (src_type == error_mark_node)
9201 : : return error_mark_node;
9202 : : }
9203 : :
9204 : 622117 : if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9205 : : {
9206 : 476034 : if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9207 : : {
9208 : 244215 : if (valid_p)
9209 : : {
9210 : 115541 : *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 : 115541 : check_for_casting_away_constness (loc, src_type, dst_type,
9214 : : CAST_EXPR, complain);
9215 : : /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9216 : 115541 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9217 : 3 : && (complain & tf_warning)
9218 : 115547 : && 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 : 244215 : if (reference_type)
9225 : : {
9226 : 61014 : expr = cp_build_addr_expr (expr, complain);
9227 : 61014 : if (expr == error_mark_node)
9228 : : return error_mark_node;
9229 : 60996 : expr = build_nop (reference_type, expr);
9230 : 60996 : return convert_from_reference (expr);
9231 : : }
9232 : : else
9233 : : {
9234 : 183201 : expr = decay_conversion (expr, complain);
9235 : 183201 : 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 : 183201 : if (TREE_CODE (expr) == NOP_EXPR
9242 : 183201 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9243 : 3 : expr = TREE_OPERAND (expr, 0);
9244 : 183201 : return build_nop (dst_type, expr);
9245 : : }
9246 : : }
9247 : 231819 : else if (valid_p
9248 : 463590 : && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9249 : 231771 : TREE_TYPE (src_type)))
9250 : 12891 : check_for_casting_away_constness (loc, src_type, dst_type,
9251 : : CAST_EXPR, complain);
9252 : : }
9253 : :
9254 : 377902 : if (complain & tf_error)
9255 : 30 : error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9256 : : src_type, dst_type);
9257 : 377902 : return error_mark_node;
9258 : : }
9259 : :
9260 : : tree
9261 : 542845 : build_const_cast (location_t loc, tree type, tree expr,
9262 : : tsubst_flags_t complain)
9263 : : {
9264 : 542845 : tree r;
9265 : :
9266 : 542845 : if (type == error_mark_node || error_operand_p (expr))
9267 : : return error_mark_node;
9268 : :
9269 : 542837 : if (processing_template_decl)
9270 : : {
9271 : 414085 : tree t = build_min (CONST_CAST_EXPR, type, expr);
9272 : :
9273 : 414085 : if (!TREE_SIDE_EFFECTS (t)
9274 : 414085 : && type_dependent_expression_p (expr))
9275 : : /* There might turn out to be side effects inside expr. */
9276 : 320828 : TREE_SIDE_EFFECTS (t) = 1;
9277 : 414085 : r = convert_from_reference (t);
9278 : 414085 : protected_set_expr_location (r, loc);
9279 : 414085 : return r;
9280 : : }
9281 : :
9282 : 128752 : r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9283 : 128752 : if (r != error_mark_node)
9284 : : {
9285 : 128674 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9286 : 128674 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9287 : : }
9288 : 128752 : protected_set_expr_location (r, loc);
9289 : 128752 : 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 : 8319396 : build_c_cast (location_t loc, tree type, cp_expr expr)
9306 : : {
9307 : 8319396 : cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9308 : 8319396 : result.set_location (loc);
9309 : 8319396 : 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 : 36413982 : cp_build_c_cast (location_t loc, tree type, tree expr,
9317 : : tsubst_flags_t complain)
9318 : : {
9319 : 36413982 : tree value = expr;
9320 : 36413982 : tree result;
9321 : 36413982 : bool valid_p;
9322 : :
9323 : 36413982 : if (type == error_mark_node || error_operand_p (expr))
9324 : : return error_mark_node;
9325 : :
9326 : 36413824 : if (processing_template_decl)
9327 : : {
9328 : 1781989 : 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 : 1781989 : TREE_SIDE_EFFECTS (t) = 1;
9332 : 1781989 : 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 : 34631835 : if (objc_is_object_ptr (type)
9339 : 34631835 : && 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 : 34631835 : if (!TYPE_REF_P (type)
9345 : 34631024 : && TREE_CODE (value) == NOP_EXPR
9346 : 34853955 : && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9347 : 82840 : value = TREE_OPERAND (value, 0);
9348 : :
9349 : 34631835 : 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 : 34631832 : 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 : 34631817 : if (TYPE_PTR_P (type)
9379 : 496400 : && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9380 : : /* Casting to an integer of smaller size is an error detected elsewhere. */
9381 : 144565 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9382 : : /* Don't warn about converting any constant. */
9383 : 34721476 : && !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 : 34631817 : result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9389 : : &valid_p);
9390 : 34631817 : if (valid_p)
9391 : : {
9392 : 115532 : if (result != error_mark_node)
9393 : : {
9394 : 115523 : maybe_warn_about_useless_cast (loc, type, value, complain);
9395 : 115523 : 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 : 115532 : return result;
9400 : : }
9401 : :
9402 : : /* Or a static cast. */
9403 : 34516285 : result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9404 : : &valid_p, complain);
9405 : : /* Or a reinterpret_cast. */
9406 : 34516285 : if (!valid_p)
9407 : 1557602 : 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 : 34516285 : 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 : 34516285 : && !error_operand_p (result))
9415 : : {
9416 : 34515967 : tree result_type;
9417 : :
9418 : 34515967 : maybe_warn_about_useless_cast (loc, type, value, complain);
9419 : 34515967 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9420 : :
9421 : : /* Non-class rvalues always have cv-unqualified type. */
9422 : 34515967 : if (!CLASS_TYPE_P (type))
9423 : 33128991 : type = TYPE_MAIN_VARIANT (type);
9424 : 34515967 : result_type = TREE_TYPE (result);
9425 : 34515967 : if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9426 : 33128532 : 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 : 34515967 : 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 : 34515967 : return result;
9438 : : }
9439 : :
9440 : 318 : 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 : 24101968 : maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9449 : : {
9450 : 24101968 : if (!warn_self_move)
9451 : : return false;
9452 : :
9453 : : /* C++98 doesn't know move. */
9454 : 334812 : if (cxx_dialect < cxx11)
9455 : : return false;
9456 : :
9457 : 315246 : if (processing_template_decl)
9458 : : return false;
9459 : :
9460 : 19758 : if (!REFERENCE_REF_P (rhs)
9461 : 270320 : || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9462 : : return false;
9463 : 4212 : tree fn = TREE_OPERAND (rhs, 0);
9464 : 4212 : if (!is_std_move_p (fn))
9465 : : return false;
9466 : :
9467 : : /* Just a little helper to strip * and various NOPs. */
9468 : 6162 : auto extract_op = [] (tree &op) {
9469 : 4108 : STRIP_NOPS (op);
9470 : 5360 : while (INDIRECT_REF_P (op))
9471 : 1252 : op = TREE_OPERAND (op, 0);
9472 : 4108 : op = maybe_undo_parenthesized_ref (op);
9473 : 4108 : STRIP_ANY_LOCATION_WRAPPER (op);
9474 : 4108 : };
9475 : :
9476 : 2054 : tree arg = CALL_EXPR_ARG (fn, 0);
9477 : 2054 : extract_op (arg);
9478 : 2054 : if (TREE_CODE (arg) == ADDR_EXPR)
9479 : 1336 : arg = TREE_OPERAND (arg, 0);
9480 : 2054 : tree type = TREE_TYPE (lhs);
9481 : 2054 : tree orig_lhs = lhs;
9482 : 2054 : extract_op (lhs);
9483 : 2054 : if (cp_tree_equal (lhs, arg)
9484 : : /* Also warn in a member-initializer-list, as in : i(std::move(i)). */
9485 : 2054 : || (TREE_CODE (lhs) == FIELD_DECL
9486 : 539 : && 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 : 4771 : 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 : 4771 : return cp_build_modify_expr (location, lhs, modifycode, rhs,
9505 : 4771 : 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 : 31330412 : cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9517 : : tree rhs, tsubst_flags_t complain)
9518 : : {
9519 : 31330412 : lhs = mark_lvalue_use_nonread (lhs);
9520 : :
9521 : 31330412 : tree result = NULL_TREE;
9522 : 31330412 : tree newrhs = rhs;
9523 : 31330412 : tree lhstype = TREE_TYPE (lhs);
9524 : 31330412 : tree olhs = lhs;
9525 : 31330412 : tree olhstype = lhstype;
9526 : 31330412 : bool plain_assign = (modifycode == NOP_EXPR);
9527 : 31330412 : bool compound_side_effects_p = false;
9528 : 31330412 : tree preeval = NULL_TREE;
9529 : :
9530 : : /* Avoid duplicate error messages from operands that had errors. */
9531 : 31330412 : if (error_operand_p (lhs) || error_operand_p (rhs))
9532 : 20 : return error_mark_node;
9533 : :
9534 : 31330471 : 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 : 31330392 : 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 : 31330192 : if (modifycode == INIT_EXPR)
9665 : : {
9666 : 3258515 : if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9667 : : /* Do the default thing. */;
9668 : 3084495 : 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 : 3084326 : else if (! MAYBE_CLASS_TYPE_P (lhstype))
9679 : : /* Do the default thing. */;
9680 : : else
9681 : : {
9682 : 42321 : releasing_vec rhs_vec = make_tree_vector_single (rhs);
9683 : 42321 : result = build_special_member_call (lhs, complete_ctor_identifier,
9684 : : &rhs_vec, lhstype, LOOKUP_NORMAL,
9685 : : complain);
9686 : 42321 : if (result == NULL_TREE)
9687 : 0 : return error_mark_node;
9688 : 42321 : goto ret;
9689 : 42321 : }
9690 : : }
9691 : : else
9692 : : {
9693 : 28071677 : lhs = require_complete_type (lhs, complain);
9694 : 28071677 : if (lhs == error_mark_node)
9695 : : return error_mark_node;
9696 : :
9697 : 28071634 : if (modifycode == NOP_EXPR)
9698 : : {
9699 : 24056181 : maybe_warn_self_move (loc, lhs, rhs);
9700 : :
9701 : 24056181 : 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 : 24056181 : if (! MAYBE_CLASS_TYPE_P (lhstype))
9710 : : /* Do the default thing. */;
9711 : : else
9712 : : {
9713 : 2246252 : 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 : 2246252 : if (result == NULL_TREE)
9718 : 0 : return error_mark_node;
9719 : 2246252 : goto ret;
9720 : : }
9721 : : lhstype = olhstype;
9722 : : }
9723 : : else
9724 : : {
9725 : 4015453 : 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 : 4015453 : 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 : 4015453 : lhs = cp_stabilize_reference (lhs);
9744 : 4015453 : rhs = decay_conversion (rhs, complain);
9745 : 4015453 : if (rhs == error_mark_node)
9746 : 123 : return error_mark_node;
9747 : :
9748 : 4015450 : {
9749 : 4015450 : auto_diagnostic_group d;
9750 : 4015450 : rhs = stabilize_expr (rhs, &init);
9751 : 4015450 : newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9752 : 4015450 : 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 : 4015450 : }
9760 : :
9761 : 4015330 : if (init)
9762 : 274848 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9763 : :
9764 : : /* Now it looks like a plain assignment. */
9765 : 4015330 : modifycode = NOP_EXPR;
9766 : 4015330 : 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 : 25825259 : gcc_assert (!TYPE_REF_P (lhstype));
9774 : 25825259 : gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9775 : : }
9776 : :
9777 : : /* The left-hand side must be an lvalue. */
9778 : 29041284 : 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 : 29041118 : if (modifycode != INIT_EXPR
9784 : 29041118 : && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9785 : : /* Functions are not modifiable, even though they are
9786 : : lvalues. */
9787 : 25775092 : || 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 : 25775062 : || (CLASS_TYPE_P (lhstype)
9791 : 0 : && C_TYPE_FIELDS_READONLY (lhstype))))
9792 : : {
9793 : 50031 : if (complain & tf_error)
9794 : 77 : cxx_readonly_error (loc, lhs, lv_assign);
9795 : 50031 : 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 : 28991087 : olhstype = unlowered_expr_type (lhs);
9803 : :
9804 : : /* Convert new value to destination type. */
9805 : :
9806 : 28991087 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
9807 : : {
9808 : 185 : int from_array;
9809 : :
9810 : 185 : 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 : 173 : 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 : 216 : && 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 : 145 : 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 : 125 : else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
9850 : : /* OK, used by coroutines (co-await-initlist1.C). */;
9851 : 119 : else if (!current_function_decl
9852 : 119 : || !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 : 72 : 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 : 72 : return error_mark_node;
9864 : : }
9865 : :
9866 : 128 : from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9867 : 81 : ? 1 + (modifycode != INIT_EXPR) : 0;
9868 : 81 : result = build_vec_init (lhs, NULL_TREE, newrhs,
9869 : : /*explicit_value_init_p=*/false,
9870 : : from_array, complain);
9871 : 81 : goto ret;
9872 : : }
9873 : :
9874 : 28990902 : if (modifycode == INIT_EXPR)
9875 : : /* Calls with INIT_EXPR are all direct-initialization, so don't set
9876 : : LOOKUP_ONLYCONVERTING. */
9877 : 3215963 : newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9878 : : ICR_INIT, NULL_TREE, 0,
9879 : : complain | tf_no_cleanup);
9880 : : else
9881 : 25774939 : newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9882 : : NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9883 : :
9884 : 28990902 : if (!same_type_p (lhstype, olhstype))
9885 : 238964 : newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9886 : :
9887 : 28990902 : if (modifycode != INIT_EXPR)
9888 : : {
9889 : 25774939 : if (TREE_CODE (newrhs) == CALL_EXPR
9890 : 25774939 : && 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 : 25774939 : 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 : 28990902 : if (newrhs == error_mark_node)
9903 : : return error_mark_node;
9904 : :
9905 : 28990334 : 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 : 32206248 : result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
9914 : : lhstype, lhs, newrhs);
9915 : 28990334 : if (modifycode == INIT_EXPR)
9916 : 3215914 : set_target_expr_eliding (newrhs);
9917 : :
9918 : 28990334 : TREE_SIDE_EFFECTS (result) = 1;
9919 : 28990334 : if (!plain_assign)
9920 : 7231214 : suppress_warning (result, OPT_Wparentheses);
9921 : :
9922 : 21759120 : ret:
9923 : 31279330 : if (preeval)
9924 : 33 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
9925 : : return result;
9926 : : }
9927 : :
9928 : : cp_expr
9929 : 57499986 : build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9930 : : tree rhs, tree lookups, tsubst_flags_t complain)
9931 : : {
9932 : 57499986 : tree orig_lhs = lhs;
9933 : 57499986 : tree orig_rhs = rhs;
9934 : 57499986 : tree overload = NULL_TREE;
9935 : :
9936 : 57499986 : if (lhs == error_mark_node || rhs == error_mark_node)
9937 : 2063 : return cp_expr (error_mark_node, loc);
9938 : :
9939 : 57497923 : tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
9940 : :
9941 : 57497923 : if (processing_template_decl)
9942 : : {
9943 : 38650855 : if (type_dependent_expression_p (lhs)
9944 : 38650855 : || type_dependent_expression_p (rhs))
9945 : : {
9946 : 29721299 : tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
9947 : 29721299 : if (modifycode != NOP_EXPR)
9948 : 5607584 : TREE_TYPE (rval)
9949 : 11215168 : = build_dependent_operator_type (lookups, modifycode, true);
9950 : 29721299 : return rval;
9951 : : }
9952 : : }
9953 : :
9954 : 27776624 : tree rval;
9955 : 27776624 : if (modifycode == NOP_EXPR)
9956 : 22151651 : rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
9957 : : else
9958 : 5624973 : rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
9959 : : lhs, rhs, op, lookups, &overload, complain);
9960 : 27776624 : if (rval == error_mark_node)
9961 : 1811 : return error_mark_node;
9962 : 27774813 : if (processing_template_decl)
9963 : : {
9964 : 8929535 : if (overload != NULL_TREE)
9965 : 1181678 : return (build_min_non_dep_op_overload
9966 : 1181678 : (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
9967 : :
9968 : 7747857 : return (build_min_non_dep
9969 : 7747857 : (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
9970 : : }
9971 : 18845278 : 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 : 29633 : get_delta_difference_1 (tree from, tree to, bool c_cast_p,
9984 : : tsubst_flags_t complain)
9985 : : {
9986 : 29633 : tree binfo;
9987 : 29633 : base_kind kind;
9988 : :
9989 : 59016 : binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
9990 : : &kind, complain);
9991 : :
9992 : 29633 : 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 : 29582 : else if (binfo)
10001 : : {
10002 : 29434 : if (kind != bk_via_virtual)
10003 : 29341 : 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 : 92078 : get_delta_difference (tree from, tree to,
10036 : : bool allow_inverse_p,
10037 : : bool c_cast_p, tsubst_flags_t complain)
10038 : : {
10039 : 92078 : auto_diagnostic_group d;
10040 : 92078 : tree result;
10041 : :
10042 : 92078 : if (same_type_ignoring_top_level_qualifiers_p (from, to))
10043 : : /* Pointer to member of incomplete class is permitted*/
10044 : 62593 : result = size_zero_node;
10045 : : else
10046 : 29485 : result = get_delta_difference_1 (from, to, c_cast_p, complain);
10047 : :
10048 : 92078 : if (result == error_mark_node)
10049 : : return error_mark_node;
10050 : :
10051 : 92063 : 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 : 92063 : return convert_to_integer (ptrdiff_type_node, result);
10085 : 92078 : }
10086 : :
10087 : : /* Return a constructor for the pointer-to-member-function TYPE using
10088 : : the other components as specified. */
10089 : :
10090 : : tree
10091 : 63011 : build_ptrmemfunc1 (tree type, tree delta, tree pfn)
10092 : : {
10093 : 63011 : tree u = NULL_TREE;
10094 : 63011 : tree delta_field;
10095 : 63011 : tree pfn_field;
10096 : 63011 : vec<constructor_elt, va_gc> *v;
10097 : :
10098 : : /* Pull the FIELD_DECLs out of the type. */
10099 : 63011 : pfn_field = TYPE_FIELDS (type);
10100 : 63011 : delta_field = DECL_CHAIN (pfn_field);
10101 : :
10102 : : /* Make sure DELTA has the type we want. */
10103 : 63011 : delta = convert_and_check (input_location, delta_type_node, delta);
10104 : :
10105 : : /* Convert to the correct target type if necessary. */
10106 : 63011 : pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10107 : :
10108 : : /* Finish creating the initializer. */
10109 : 63011 : vec_alloc (v, 2);
10110 : 63011 : CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10111 : 63011 : CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10112 : 63011 : u = build_constructor (type, v);
10113 : 63011 : TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10114 : 63011 : TREE_STATIC (u) = (TREE_CONSTANT (u)
10115 : 62909 : && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10116 : : != NULL_TREE)
10117 : 125920 : && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10118 : : != NULL_TREE));
10119 : 63011 : 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 : 31004 : build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10135 : : tsubst_flags_t complain)
10136 : : {
10137 : 31004 : tree fn;
10138 : 31004 : tree pfn_type;
10139 : 31004 : tree to_type;
10140 : :
10141 : 31004 : if (error_operand_p (pfn))
10142 : 0 : return error_mark_node;
10143 : :
10144 : 31004 : pfn_type = TREE_TYPE (pfn);
10145 : 31004 : to_type = build_ptrmemfunc_type (type);
10146 : :
10147 : : /* Handle multiple conversions of pointer to member functions. */
10148 : 31004 : if (TYPE_PTRMEMFUNC_P (pfn_type))
10149 : : {
10150 : 29192 : tree delta = NULL_TREE;
10151 : 29192 : tree npfn = NULL_TREE;
10152 : 29192 : tree n;
10153 : :
10154 : 29192 : if (!force
10155 : 29192 : && !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 : 29192 : n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10166 : 29192 : TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10167 : : force,
10168 : : c_cast_p, complain);
10169 : 29192 : if (n == error_mark_node)
10170 : : return error_mark_node;
10171 : :
10172 : 29183 : 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 : 29183 : if (TREE_CODE (pfn) != PTRMEM_CST
10179 : 29183 : && same_type_p (to_type, pfn_type))
10180 : : return pfn;
10181 : :
10182 : 29183 : if (TREE_SIDE_EFFECTS (pfn))
10183 : 8 : pfn = save_expr (pfn);
10184 : :
10185 : : /* Obtain the function pointer and the current DELTA. */
10186 : 29183 : if (TREE_CODE (pfn) == PTRMEM_CST)
10187 : 29095 : 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 : 29183 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10196 : : (TREE_TYPE (delta), ptrdiff_type_node));
10197 : 29183 : 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 : 29183 : return build_ptrmemfunc1 (to_type, delta, npfn);
10207 : : }
10208 : :
10209 : : /* Handle null pointer to member function conversions. */
10210 : 1812 : if (null_ptr_cst_p (pfn))
10211 : : {
10212 : 749 : pfn = cp_build_c_cast (input_location,
10213 : 749 : TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10214 : : pfn, complain);
10215 : 749 : return build_ptrmemfunc1 (to_type,
10216 : : integer_zero_node,
10217 : 749 : 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 : 62522 : expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10240 : : {
10241 : 62522 : tree type = TREE_TYPE (cst);
10242 : 62522 : tree fn = PTRMEM_CST_MEMBER (cst);
10243 : 62522 : tree ptr_class, fn_class;
10244 : :
10245 : 62522 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10246 : :
10247 : : /* The class that the function belongs to. */
10248 : 62522 : fn_class = DECL_CONTEXT (fn);
10249 : :
10250 : : /* The class that we're creating a pointer to member of. */
10251 : 62522 : ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10252 : :
10253 : : /* First, calculate the adjustment to the function's class. */
10254 : 62522 : *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10255 : : /*c_cast_p=*/0, tf_warning_or_error);
10256 : :
10257 : 62522 : if (!DECL_VIRTUAL_P (fn))
10258 : : {
10259 : 61292 : tree t = build_addr_func (fn, tf_warning_or_error);
10260 : 61292 : if (TREE_CODE (t) == ADDR_EXPR)
10261 : 61292 : SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10262 : 61292 : *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 : 1230 : tree orig_class = DECL_CONTEXT (fn);
10270 : 1230 : tree binfo = binfo_or_else (orig_class, fn_class);
10271 : 1230 : *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 : 1230 : *pfn = DECL_VINDEX (fn);
10278 : 1230 : *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10279 : : TYPE_SIZE_UNIT (vtable_entry_type));
10280 : :
10281 : 1230 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10282 : : {
10283 : 1230 : case ptrmemfunc_vbit_in_pfn:
10284 : 1230 : *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10285 : : integer_one_node);
10286 : 1230 : 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 : 1230 : *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10300 : : }
10301 : 62522 : }
10302 : :
10303 : : /* Return an expression for PFN from the pointer-to-member function
10304 : : given by T. */
10305 : :
10306 : : static tree
10307 : 88584 : pfn_from_ptrmemfunc (tree t)
10308 : : {
10309 : 88584 : 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 : 88410 : 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 : 88584 : delta_from_ptrmemfunc (tree t)
10327 : : {
10328 : 88584 : 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 : 88410 : 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 : 117110972 : 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 : 117110972 : tree rhstype;
10354 : 117110972 : enum tree_code coder;
10355 : :
10356 : 117110972 : location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10357 : 117110972 : 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 : 117110972 : if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10361 : 117110972 : && !location_wrapper_p (rhs))
10362 : 7056 : 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 : 117110972 : 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 : 117110972 : rhstype = TREE_TYPE (rhs);
10380 : 117110972 : coder = TREE_CODE (rhstype);
10381 : :
10382 : 1161461 : if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10383 : 118272327 : && vector_types_convertible_p (type, rhstype, true))
10384 : : {
10385 : 1161346 : rhs = mark_rvalue_use (rhs);
10386 : 1161346 : return convert (type, rhs);
10387 : : }
10388 : :
10389 : 115949626 : if (rhs == error_mark_node || rhstype == error_mark_node)
10390 : : return error_mark_node;
10391 : 115949617 : 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 : 115949617 : 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 : 115949569 : 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 : 115949569 : 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 : 1465 : if (!warn_pmf2ptr
10447 : 9 : && TYPE_PTR_P (type)
10448 : 1474 : && TYPE_PTRMEMFUNC_P (rhstype))
10449 : 9 : rhs = cp_convert (strip_top_quals (type), rhs, complain);
10450 : : else
10451 : : {
10452 : 1456 : if (complain & tf_error)
10453 : : {
10454 : 1269 : 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 : 1269 : 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 : 1072 : else if (fndecl)
10468 : 92 : complain_about_bad_argument (rhs_loc,
10469 : : rhstype, type,
10470 : : fndecl, parmnum);
10471 : : else
10472 : : {
10473 : 980 : range_label_for_type_mismatch label (rhstype, type);
10474 : 980 : gcc_rich_location richloc
10475 : : (rhs_loc,
10476 : : has_loc ? &label : NULL,
10477 : 980 : has_loc ? highlight_colors::percent_h : NULL);
10478 : :
10479 : 980 : 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 : 18 : case ICR_RETURN:
10501 : 18 : error_at (&richloc, "cannot convert %qH to %qI in return",
10502 : : rhstype, type);
10503 : 18 : 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 : 980 : }
10513 : :
10514 : : /* See if we can be more helpful. */
10515 : 1254 : maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
10516 : :
10517 : 1254 : 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 : 1316 : && !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 : 1269 : }
10526 : 1441 : return error_mark_node;
10527 : : }
10528 : : }
10529 : 115948113 : 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 : 115948113 : if (TREE_CODE (type) == BOOLEAN_TYPE)
10575 : 22272711 : maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
10576 : :
10577 : 115948113 : if (complain & tf_warning)
10578 : 114889485 : warn_for_address_of_packed_member (type, rhs);
10579 : :
10580 : 115948113 : return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10581 : 115948113 : 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 : 103272621 : 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 : 103272621 : enum tree_code codel = TREE_CODE (type);
10604 : 103272621 : tree rhstype;
10605 : 103272621 : 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 : 103272621 : if (TREE_CODE (rhs) == NOP_EXPR
10610 : 4533049 : && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10611 : 103621353 : && codel != REFERENCE_TYPE)
10612 : 348732 : rhs = TREE_OPERAND (rhs, 0);
10613 : :
10614 : 103272621 : if (type == error_mark_node
10615 : 103272583 : || rhs == error_mark_node
10616 : 206545143 : || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10617 : : return error_mark_node;
10618 : :
10619 : 103272522 : if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10620 : : ;
10621 : 93043389 : else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10622 : 1472491 : && TREE_CODE (type) != ARRAY_TYPE
10623 : 1472491 : && (!TYPE_REF_P (type)
10624 : 8341 : || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10625 : 91579236 : || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10626 : 12871 : && !TYPE_REFFN_P (type))
10627 : 184611319 : || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10628 : 1475465 : rhs = decay_conversion (rhs, complain);
10629 : :
10630 : 103272522 : rhstype = TREE_TYPE (rhs);
10631 : 103272522 : coder = TREE_CODE (rhstype);
10632 : :
10633 : 103272522 : 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 : 103272513 : if (codel == REFERENCE_TYPE)
10640 : : {
10641 : 4952739 : auto_diagnostic_group d;
10642 : : /* This should eventually happen in convert_arguments. */
10643 : 4952739 : int savew = 0, savee = 0;
10644 : :
10645 : 4952739 : if (fndecl)
10646 : 244659 : savew = warningcount + werrorcount, savee = errorcount;
10647 : 4952739 : rhs = initialize_reference (type, rhs, flags, complain);
10648 : :
10649 : 4952739 : if (fndecl
10650 : 4952739 : && (warningcount + werrorcount > savew || errorcount > savee))
10651 : 36 : inform (get_fndecl_argument_location (fndecl, parmnum),
10652 : : "in passing argument %P of %qD", parmnum, fndecl);
10653 : 4952739 : return rhs;
10654 : 4952739 : }
10655 : :
10656 : 98319774 : if (exp != 0)
10657 : 3215963 : exp = require_complete_type (exp, complain);
10658 : 98319774 : if (exp == error_mark_node)
10659 : : return error_mark_node;
10660 : :
10661 : 98319774 : type = complete_type (type);
10662 : :
10663 : 98319774 : 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 : 98318875 : if (MAYBE_CLASS_TYPE_P (type))
10669 : 6982857 : return perform_implicit_conversion_flags (type, rhs, complain, flags);
10670 : :
10671 : 91336018 : return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10672 : 91336018 : 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 : 34658031 : maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10680 : : {
10681 : 34658716 : tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10682 : 34658716 : tree whats_returned = fold_for_warn (retval);
10683 : 34658716 : if (!loc)
10684 : 34658716 : loc = cp_expr_loc_or_input_loc (retval);
10685 : :
10686 : 41992902 : for (;;)
10687 : : {
10688 : 41992902 : if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10689 : 445505 : whats_returned = TREE_OPERAND (whats_returned, 1);
10690 : 41547397 : else if (CONVERT_EXPR_P (whats_returned)
10691 : 34678348 : || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10692 : 6888681 : whats_returned = TREE_OPERAND (whats_returned, 0);
10693 : : else
10694 : : break;
10695 : : }
10696 : :
10697 : 34658716 : if (TREE_CODE (whats_returned) == TARGET_EXPR
10698 : 34658716 : && 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 : 34658713 : if (TREE_CODE (whats_returned) == CALL_EXPR
10714 : 34658713 : && (is_std_move_p (whats_returned)
10715 : 9505743 : || is_std_forward_p (whats_returned)))
10716 : : {
10717 : 673 : tree arg = CALL_EXPR_ARG (whats_returned, 0);
10718 : 673 : return maybe_warn_about_returning_address_of_local (arg, loc);
10719 : : }
10720 : :
10721 : 34658040 : if (TREE_CODE (whats_returned) != ADDR_EXPR)
10722 : : return false;
10723 : 751745 : whats_returned = TREE_OPERAND (whats_returned, 0);
10724 : :
10725 : 751745 : while (TREE_CODE (whats_returned) == COMPONENT_REF
10726 : 1469462 : || TREE_CODE (whats_returned) == ARRAY_REF)
10727 : 717717 : whats_returned = TREE_OPERAND (whats_returned, 0);
10728 : :
10729 : 751745 : if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10730 : 751745 : || 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 : 41 : 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 : 751695 : STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10748 : :
10749 : 751695 : if (DECL_P (whats_returned)
10750 : 72343 : && DECL_NAME (whats_returned)
10751 : 72343 : && DECL_FUNCTION_SCOPE_P (whats_returned)
10752 : 12376 : && !is_capture_proxy (whats_returned)
10753 : 764068 : && !(TREE_STATIC (whats_returned)
10754 : 146 : || 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 : 68076204 : decl_in_std_namespace_p (tree decl)
10806 : : {
10807 : 79564545 : while (decl)
10808 : : {
10809 : 79116519 : decl = decl_namespace_context (decl);
10810 : 79116519 : 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 : 37350863 : if (!DECL_NAMESPACE_INLINE_P (decl))
10816 : : return false;
10817 : 11488341 : 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 : 9505743 : is_std_forward_p (tree fn)
10826 : : {
10827 : : /* std::forward only takes one argument. */
10828 : 9505743 : if (call_expr_nargs (fn) != 1)
10829 : : return false;
10830 : :
10831 : 4551495 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10832 : 4551495 : if (!decl_in_std_namespace_p (fndecl))
10833 : : return false;
10834 : :
10835 : 1601268 : tree name = DECL_NAME (fndecl);
10836 : 1601268 : 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 : 9512539 : is_std_move_p (tree fn)
10843 : : {
10844 : : /* std::move only takes one argument. */
10845 : 9512539 : if (call_expr_nargs (fn) != 1)
10846 : : return false;
10847 : :
10848 : 4557719 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10849 : 4557719 : if (!decl_in_std_namespace_p (fndecl))
10850 : : return false;
10851 : :
10852 : 1607034 : tree name = DECL_NAME (fndecl);
10853 : 1607034 : 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 : 43775362 : can_do_nrvo_p (tree retval, tree functype)
10862 : : {
10863 : 43775362 : if (functype == error_mark_node)
10864 : : return false;
10865 : 43775321 : if (retval)
10866 : 42326636 : STRIP_ANY_LOCATION_WRAPPER (retval);
10867 : 43775321 : tree result = DECL_RESULT (current_function_decl);
10868 : 43775321 : return (retval != NULL_TREE
10869 : 42326636 : && !processing_template_decl
10870 : : /* Must be a local, automatic variable. */
10871 : 35419536 : && VAR_P (retval)
10872 : 3204652 : && DECL_CONTEXT (retval) == current_function_decl
10873 : 2513375 : && !TREE_STATIC (retval)
10874 : : /* And not a lambda or anonymous union proxy. */
10875 : 2510731 : && !DECL_HAS_VALUE_EXPR_P (retval)
10876 : 2510520 : && (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 : 2510136 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10881 : : TYPE_MAIN_VARIANT (functype))
10882 : : /* And the returned value must be non-volatile. */
10883 : 46274873 : && !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 : 43775021 : want_nrvo_p (tree retval, tree functype)
10891 : : {
10892 : 43775021 : return (can_do_nrvo_p (retval, functype)
10893 : 43775021 : && 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 : 1460 : can_elide_copy_prvalue_p (tree retval, tree functype)
10901 : : {
10902 : 1460 : if (functype == error_mark_node)
10903 : : return false;
10904 : 1460 : if (retval)
10905 : 1460 : STRIP_ANY_LOCATION_WRAPPER (retval);
10906 : 1460 : return (retval != NULL_TREE
10907 : 1460 : && !glvalue_p (retval)
10908 : 135 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10909 : : TYPE_MAIN_VARIANT (functype))
10910 : 1568 : && !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 : 8375758 : treat_lvalue_as_rvalue_p (tree expr, bool return_p)
10920 : : {
10921 : 8375758 : if (cxx_dialect == cxx98)
10922 : : return NULL_TREE;
10923 : :
10924 : 8365743 : tree retval = expr;
10925 : 8365743 : STRIP_ANY_LOCATION_WRAPPER (retval);
10926 : 8365743 : if (REFERENCE_REF_P (retval))
10927 : 580375 : 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 : 8365743 : if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
10933 : 7503021 : || TREE_CODE (retval) == PARM_DECL)
10934 : 1160129 : && !TREE_STATIC (retval)
10935 : 1130568 : && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
10936 : 1130563 : && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
10937 : 101703 : || (cxx_dialect >= cxx20
10938 : 83602 : && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
10939 : 7336311 : 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 : 1029432 : if (return_p)
10946 : : {
10947 : 1029298 : if (DECL_CONTEXT (retval) != current_function_decl)
10948 : : return NULL_TREE;
10949 : 1029232 : expr = move (expr);
10950 : 1029232 : if (expr == error_mark_node)
10951 : : return NULL_TREE;
10952 : 1029230 : 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 : 134 : 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 : 121 : for (cp_binding_level *b = current_binding_level;
10968 : 212 : b->kind != sk_namespace; b = b->level_chain)
10969 : : {
10970 : 214 : for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
10971 : 100 : if (decl == retval)
10972 : 88 : 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 : 124403140 : maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
10986 : : {
10987 : 124403140 : if (!(warn_pessimizing_move || warn_redundant_move))
10988 : : return;
10989 : :
10990 : 4378586 : const location_t loc = cp_expr_loc_or_input_loc (expr);
10991 : :
10992 : : /* C++98 doesn't know move. */
10993 : 4378586 : 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 : 4314173 : if (processing_template_decl)
10999 : : return;
11000 : :
11001 : : /* This is only interesting for class types. */
11002 : 4234069 : if (!CLASS_TYPE_P (type))
11003 : : return;
11004 : :
11005 : 90384 : bool wrapped_p = false;
11006 : : /* A a = std::move (A()); */
11007 : 90384 : if (TREE_CODE (expr) == TREE_LIST)
11008 : : {
11009 : 7528 : if (list_length (expr) == 1)
11010 : : {
11011 : 4960 : expr = TREE_VALUE (expr);
11012 : 4960 : wrapped_p = true;
11013 : : }
11014 : : else
11015 : : return;
11016 : : }
11017 : : /* A a = {std::move (A())};
11018 : : A a{std::move (A())}; */
11019 : 82856 : else if (TREE_CODE (expr) == CONSTRUCTOR)
11020 : : {
11021 : 6114 : if (CONSTRUCTOR_NELTS (expr) == 1)
11022 : : {
11023 : 738 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
11024 : 738 : wrapped_p = true;
11025 : : }
11026 : : else
11027 : : return;
11028 : : }
11029 : :
11030 : : /* First, check if this is a call to std::move. */
11031 : 5761 : if (!REFERENCE_REF_P (expr)
11032 : 86823 : || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
11033 : : return;
11034 : 2560 : tree fn = TREE_OPERAND (expr, 0);
11035 : 2560 : if (!is_std_move_p (fn))
11036 : : return;
11037 : 2048 : tree arg = CALL_EXPR_ARG (fn, 0);
11038 : 2048 : 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 : 2048 : if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
11043 : : {
11044 : 1460 : arg = TREE_OPERAND (arg, 0);
11045 : 1460 : arg = TREE_OPERAND (arg, 0);
11046 : 1460 : arg = convert_from_reference (arg);
11047 : 1460 : 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 : 1460 : if (!return_p)
11057 : : return;
11058 : :
11059 : 341 : tree moved;
11060 : : /* Warn if we could do copy elision were it not for the move. */
11061 : 341 : 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 : 293 : else if (warn_redundant_move
11073 : : /* This doesn't apply for return {std::move (t)};. */
11074 : 176 : && !wrapped_p
11075 : 173 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11076 : 389 : && (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 : 102269832 : check_return_expr (tree retval, bool *no_warning, bool *dangling)
11144 : : {
11145 : 102269832 : tree result;
11146 : : /* The type actually returned by the function. */
11147 : 102269832 : tree valtype;
11148 : : /* The type the function is declared to return, or void if
11149 : : the declared type is incomplete. */
11150 : 102269832 : tree functype;
11151 : 102269832 : int fn_returns_value_p;
11152 : 102269832 : location_t loc = cp_expr_loc_or_input_loc (retval);
11153 : :
11154 : 102269832 : *no_warning = false;
11155 : 102269832 : *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 : 102269832 : if (TREE_THIS_VOLATILE (current_function_decl))
11161 : 12 : warning (0, "function declared %<noreturn%> has a %<return%> statement");
11162 : :
11163 : : /* Check for various simple errors. */
11164 : 204539664 : if (DECL_DESTRUCTOR_P (current_function_decl))
11165 : : {
11166 : 1711 : if (retval)
11167 : 3 : error_at (loc, "returning a value from a destructor");
11168 : :
11169 : 1711 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11170 : 0 : retval = current_class_ptr;
11171 : : else
11172 : : return NULL_TREE;
11173 : : }
11174 : 102268121 : else if (DECL_CONSTRUCTOR_P (current_function_decl))
11175 : : {
11176 : 50193 : 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 : 50193 : 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 : 50193 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11185 : 0 : retval = current_class_ptr;
11186 : : else
11187 : : return NULL_TREE;
11188 : : }
11189 : :
11190 : 102217928 : const tree saved_retval = retval;
11191 : :
11192 : 102217928 : if (processing_template_decl)
11193 : : {
11194 : 66137399 : current_function_returns_value = 1;
11195 : :
11196 : 66137399 : if (check_for_bare_parameter_packs (retval))
11197 : 13 : 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 : 66137386 : if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11202 : 25850072 : && !FNDECL_USED_AUTO (current_function_decl))
11203 : 40287314 : || (retval != NULL_TREE
11204 : 39256476 : && (TREE_TYPE (retval) == NULL_TREE
11205 : 27524961 : || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11206 : 44832163 : goto dependent;
11207 : : }
11208 : :
11209 : 57385752 : functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11210 : :
11211 : : /* Deduce auto return type from a return statement. */
11212 : 57385752 : if (FNDECL_USED_AUTO (current_function_decl))
11213 : : {
11214 : 897342 : tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11215 : 897342 : tree auto_node;
11216 : 897342 : tree type;
11217 : :
11218 : 897342 : 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 : 897339 : 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 : 897333 : if (!retval)
11236 : 5098 : retval = void_node;
11237 : 897333 : auto_node = type_uses_auto (pattern);
11238 : 897333 : type = do_auto_deduction (pattern, retval, auto_node,
11239 : : tf_warning_or_error, adc_return_type);
11240 : : }
11241 : :
11242 : 897342 : if (type == error_mark_node)
11243 : : /* Leave it. */;
11244 : 897147 : else if (functype == pattern)
11245 : 561614 : apply_deduced_return_type (current_function_decl, type);
11246 : 335533 : 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 : 57385752 : result = DECL_RESULT (current_function_decl);
11259 : 57385752 : valtype = TREE_TYPE (result);
11260 : 57385752 : gcc_assert (valtype != NULL_TREE);
11261 : 57385752 : 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 : 57385752 : 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 : 57385717 : else if (retval && !fn_returns_value_p)
11282 : : {
11283 : 252394 : 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 : 252361 : finish_expr_stmt (retval);
11288 : 33 : else if (retval != error_mark_node)
11289 : 30 : permerror (loc, "return-statement with a value, in function "
11290 : : "returning %qT", valtype);
11291 : 252394 : current_function_returns_null = 1;
11292 : :
11293 : : /* There's really no value to return, after all. */
11294 : 252394 : return NULL_TREE;
11295 : : }
11296 : 57133323 : else if (!retval)
11297 : : /* Remember that this function can sometimes return without a
11298 : : value. */
11299 : 1448668 : current_function_returns_null = 1;
11300 : : else
11301 : : /* Remember that this function did return a value. */
11302 : 55684655 : 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 : 57133358 : if (error_operand_p (retval))
11308 : : {
11309 : 957 : current_function_return_value = error_mark_node;
11310 : 957 : return error_mark_node;
11311 : : }
11312 : :
11313 : : /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11314 : 114264802 : if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11315 : 29910 : && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11316 : 3002 : && ! flag_check_new
11317 : 57135394 : && 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 : 57132401 : if (warn_ecpp
11323 : 42 : && DECL_NAME (current_function_decl) == assign_op_identifier
11324 : 57132431 : && !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 : 57132401 : if (dependent_type_p (functype)
11350 : 57132401 : || type_dependent_expression_p (retval))
11351 : : {
11352 : 58189543 : dependent:
11353 : : /* We should not have changed the return value. */
11354 : 58189543 : 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 : 58189543 : mark_exp_read (retval);
11358 : 58189543 : 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 : 43775021 : tree bare_retval = NULL_TREE;
11384 : 43775021 : if (retval)
11385 : : {
11386 : 42326333 : retval = maybe_undo_parenthesized_ref (retval);
11387 : 42326333 : bare_retval = tree_strip_any_location_wrapper (retval);
11388 : : }
11389 : :
11390 : 43775021 : bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11391 : 43775021 : if (fn_returns_value_p && flag_elide_constructors
11392 : 42326312 : && current_function_return_value != bare_retval)
11393 : : {
11394 : 42315324 : if (named_return_value_okay_p
11395 : 216640 : && current_function_return_value == NULL_TREE)
11396 : 176345 : current_function_return_value = bare_retval;
11397 : 42138979 : else if (current_function_return_value
11398 : 7825433 : && VAR_P (current_function_return_value)
11399 : 145 : && DECL_NAME (current_function_return_value)
11400 : 42139124 : && !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 : 78 : leave it alone; the current return can't refer to it. */;
11404 : 78 : if (named_return_value_okay_p
11405 : 78 : && !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 : 42138901 : if ((named_return_value_okay_p
11415 : 42098616 : || (current_function_return_value
11416 : 7785070 : && current_function_return_value != error_mark_node))
11417 : 42138946 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11418 : : {
11419 : 40258 : warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11420 : : current_function_decl);
11421 : 40258 : suppress_warning (current_function_decl, OPT_Wnrvo);
11422 : : }
11423 : 42138901 : 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 : 43775021 : if (!retval)
11430 : : return NULL_TREE;
11431 : :
11432 : 42326333 : if (!named_return_value_okay_p)
11433 : 42098705 : maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11434 : :
11435 : : /* Do any required conversions. */
11436 : 84652666 : if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11437 : : /* No conversions are required. */
11438 : : ;
11439 : : else
11440 : : {
11441 : 42326333 : 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 : 42326333 : 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 : 42326327 : bool converted = false;
11455 : 42326327 : 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 : 42326327 : if ((cxx_dialect < cxx23
11460 : 4556637 : ? CLASS_TYPE_P (functype)
11461 : 10359885 : : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11462 : 49926547 : && (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 : 42326327 : if (TREE_CODE (retval) == CALL_EXPR
11470 : 42326327 : && call_from_lambda_thunk_p (retval))
11471 : : converted = true;
11472 : :
11473 : : /* Don't check copy-initialization for NRV in a coroutine ramp; we
11474 : : implement this case as NRV, but it's specified as directly
11475 : : initializing the return value from get_return_object(). */
11476 : 42326327 : if (DECL_RAMP_P (current_function_decl) && named_return_value_okay_p)
11477 : : converted = true;
11478 : :
11479 : : /* First convert the value to the function's return type, then
11480 : : to the type of return value's location to handle the
11481 : : case that functype is smaller than the valtype. */
11482 : 42326086 : if (!converted)
11483 : 42307778 : retval = convert_for_initialization
11484 : 42307778 : (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11485 : : tf_warning_or_error);
11486 : 42326327 : retval = convert (valtype, retval);
11487 : :
11488 : : /* If the conversion failed, treat this just like `return;'. */
11489 : 42326327 : if (retval == error_mark_node)
11490 : : {
11491 : : /* And suppress NRV. */
11492 : 192 : current_function_return_value = error_mark_node;
11493 : 192 : return retval;
11494 : : }
11495 : : /* We can't initialize a register from a AGGR_INIT_EXPR. */
11496 : 42326135 : else if (! cfun->returns_struct
11497 : 40847548 : && TREE_CODE (retval) == TARGET_EXPR
11498 : 46884964 : && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == AGGR_INIT_EXPR)
11499 : 761016 : retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11500 : 761016 : TARGET_EXPR_SLOT (retval));
11501 : 41565119 : else if (!processing_template_decl
11502 : 34658031 : && maybe_warn_about_returning_address_of_local (retval, loc)
11503 : 41565300 : && INDIRECT_TYPE_P (valtype))
11504 : 166 : *dangling = true;
11505 : : }
11506 : :
11507 : : /* A naive attempt to reduce the number of -Wdangling-reference false
11508 : : positives: if we know that this function can return a variable with
11509 : : static storage duration rather than one of its parameters, suppress
11510 : : the warning. */
11511 : 42326135 : if (warn_dangling_reference
11512 : 364046 : && TYPE_REF_P (functype)
11513 : 23899 : && bare_retval
11514 : 23899 : && VAR_P (bare_retval)
11515 : 50 : && TREE_STATIC (bare_retval))
11516 : 50 : suppress_warning (current_function_decl, OPT_Wdangling_reference);
11517 : :
11518 : 42326135 : if (processing_template_decl)
11519 : : return saved_retval;
11520 : :
11521 : : /* Actually copy the value returned into the appropriate location. */
11522 : 35419047 : if (retval && retval != result)
11523 : 35419047 : retval = cp_build_init_expr (result, retval);
11524 : :
11525 : 35419047 : if (current_function_return_value == bare_retval)
11526 : 187327 : INIT_EXPR_NRV_P (retval) = true;
11527 : :
11528 : 35419047 : if (tree set = maybe_set_retval_sentinel ())
11529 : 134983 : retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11530 : :
11531 : : return retval;
11532 : : }
11533 : :
11534 : :
11535 : : /* Returns nonzero if the pointer-type FROM can be converted to the
11536 : : pointer-type TO via a qualification conversion. If CONSTP is -1,
11537 : : then we return nonzero if the pointers are similar, and the
11538 : : cv-qualification signature of FROM is a proper subset of that of TO.
11539 : :
11540 : : If CONSTP is positive, then all outer pointers have been
11541 : : const-qualified. */
11542 : :
11543 : : static bool
11544 : 124993324 : comp_ptr_ttypes_real (tree to, tree from, int constp)
11545 : : {
11546 : 124993324 : bool to_more_cv_qualified = false;
11547 : 124993324 : bool is_opaque_pointer = false;
11548 : :
11549 : 543190 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11550 : : {
11551 : 125536514 : if (TREE_CODE (to) != TREE_CODE (from))
11552 : : return false;
11553 : :
11554 : 78634618 : if (TREE_CODE (from) == OFFSET_TYPE
11555 : 78634618 : && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11556 : : TYPE_OFFSET_BASETYPE (to)))
11557 : : return false;
11558 : :
11559 : : /* Const and volatile mean something different for function and
11560 : : array types, so the usual checks are not appropriate. We'll
11561 : : check the array type elements in further iterations. */
11562 : 78634618 : if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11563 : : {
11564 : 78408202 : if (!at_least_as_qualified_p (to, from))
11565 : : return false;
11566 : :
11567 : 69182339 : if (!at_least_as_qualified_p (from, to))
11568 : : {
11569 : 48279589 : if (constp == 0)
11570 : : return false;
11571 : : to_more_cv_qualified = true;
11572 : : }
11573 : :
11574 : 69182077 : if (constp > 0)
11575 : 69177361 : constp &= TYPE_READONLY (to);
11576 : : }
11577 : :
11578 : 69408493 : if (VECTOR_TYPE_P (to))
11579 : 5973 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11580 : :
11581 : : /* P0388R4 allows a conversion from int[N] to int[] but not the
11582 : : other way round. When both arrays have bounds but they do
11583 : : not match, then no conversion is possible. */
11584 : 69408493 : if (TREE_CODE (to) == ARRAY_TYPE
11585 : 69408493 : && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11586 : : return false;
11587 : :
11588 : 69408207 : if (!TYPE_PTR_P (to)
11589 : : && !TYPE_PTRDATAMEM_P (to)
11590 : : /* CWG 330 says we need to look through arrays. */
11591 : : && TREE_CODE (to) != ARRAY_TYPE)
11592 : 68865017 : return ((constp >= 0 || to_more_cv_qualified)
11593 : 68865017 : && (is_opaque_pointer
11594 : 68864463 : || same_type_ignoring_top_level_qualifiers_p (to, from)));
11595 : : }
11596 : : }
11597 : :
11598 : : /* When comparing, say, char ** to char const **, this function takes
11599 : : the 'char *' and 'char const *'. Do not pass non-pointer/reference
11600 : : types to this function. */
11601 : :
11602 : : int
11603 : 124992670 : comp_ptr_ttypes (tree to, tree from)
11604 : : {
11605 : 124992670 : return comp_ptr_ttypes_real (to, from, 1);
11606 : : }
11607 : :
11608 : : /* Returns true iff FNTYPE is a non-class type that involves
11609 : : error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11610 : : if a parameter type is ill-formed. */
11611 : :
11612 : : bool
11613 : 622771 : error_type_p (const_tree type)
11614 : : {
11615 : 666489 : tree t;
11616 : :
11617 : 666489 : switch (TREE_CODE (type))
11618 : : {
11619 : : case ERROR_MARK:
11620 : : return true;
11621 : :
11622 : 43629 : case POINTER_TYPE:
11623 : 43629 : case REFERENCE_TYPE:
11624 : 43629 : case OFFSET_TYPE:
11625 : 43629 : return error_type_p (TREE_TYPE (type));
11626 : :
11627 : 3743 : case FUNCTION_TYPE:
11628 : 3743 : case METHOD_TYPE:
11629 : 3743 : if (error_type_p (TREE_TYPE (type)))
11630 : : return true;
11631 : 10365 : for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11632 : 6628 : if (error_type_p (TREE_VALUE (t)))
11633 : : return true;
11634 : : return false;
11635 : :
11636 : 199501 : case RECORD_TYPE:
11637 : 199501 : if (TYPE_PTRMEMFUNC_P (type))
11638 : 89 : return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11639 : : return false;
11640 : :
11641 : : default:
11642 : : return false;
11643 : : }
11644 : : }
11645 : :
11646 : : /* Returns true if to and from are (possibly multi-level) pointers to the same
11647 : : type or inheritance-related types, regardless of cv-quals. */
11648 : :
11649 : : bool
11650 : 88655956 : ptr_reasonably_similar (const_tree to, const_tree from)
11651 : : {
11652 : 983 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11653 : : {
11654 : : /* Any target type is similar enough to void. */
11655 : 88656939 : if (VOID_TYPE_P (to))
11656 : 996 : return !error_type_p (from);
11657 : 88655943 : if (VOID_TYPE_P (from))
11658 : 608434 : return !error_type_p (to);
11659 : :
11660 : 88047509 : if (TREE_CODE (to) != TREE_CODE (from))
11661 : : return false;
11662 : :
11663 : 41617078 : if (TREE_CODE (from) == OFFSET_TYPE
11664 : 41617078 : && comptypes (TYPE_OFFSET_BASETYPE (to),
11665 : 3 : TYPE_OFFSET_BASETYPE (from),
11666 : : COMPARE_BASE | COMPARE_DERIVED))
11667 : 3 : continue;
11668 : :
11669 : 41617072 : if (VECTOR_TYPE_P (to)
11670 : 41617072 : && vector_types_convertible_p (to, from, false))
11671 : : return true;
11672 : :
11673 : 41616988 : if (TREE_CODE (to) == INTEGER_TYPE
11674 : 41616988 : && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11675 : : return true;
11676 : :
11677 : 41501279 : if (TREE_CODE (to) == FUNCTION_TYPE)
11678 : 1485 : return !error_type_p (to) && !error_type_p (from);
11679 : :
11680 : 41499794 : if (!TYPE_PTR_P (to))
11681 : : {
11682 : : /* When either type is incomplete avoid DERIVED_FROM_P,
11683 : : which may call complete_type (c++/57942). */
11684 : 41498814 : bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11685 : : return comptypes
11686 : 41498814 : (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11687 : 41498814 : b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11688 : : }
11689 : 983 : }
11690 : : }
11691 : :
11692 : : /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11693 : : pointer-to-member types) are the same, ignoring cv-qualification at
11694 : : all levels. CB says how we should behave when comparing array bounds. */
11695 : :
11696 : : bool
11697 : 766150 : comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11698 : : {
11699 : 766150 : bool is_opaque_pointer = false;
11700 : :
11701 : 767227 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11702 : : {
11703 : 1533377 : if (TREE_CODE (to) != TREE_CODE (from))
11704 : : return false;
11705 : :
11706 : 1185897 : if (TREE_CODE (from) == OFFSET_TYPE
11707 : 1185861 : && same_type_p (TYPE_OFFSET_BASETYPE (from),
11708 : : TYPE_OFFSET_BASETYPE (to)))
11709 : 36 : continue;
11710 : :
11711 : 1185825 : if (VECTOR_TYPE_P (to))
11712 : 7522 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11713 : :
11714 : 1185825 : if (TREE_CODE (to) == ARRAY_TYPE
11715 : : /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11716 : : we must fail. */
11717 : 1185825 : && !comp_array_types (to, from, cb, /*strict=*/false))
11718 : : return false;
11719 : :
11720 : : /* CWG 330 says we need to look through arrays. */
11721 : 1185427 : if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11722 : 418236 : return (is_opaque_pointer
11723 : 418236 : || same_type_ignoring_top_level_qualifiers_p (to, from));
11724 : : }
11725 : : }
11726 : :
11727 : : /* Returns the type qualifiers for this type, including the qualifiers on the
11728 : : elements for an array type. */
11729 : :
11730 : : int
11731 : 47124055074 : cp_type_quals (const_tree type)
11732 : : {
11733 : 47124055074 : int quals;
11734 : : /* This CONST_CAST is okay because strip_array_types returns its
11735 : : argument unmodified and we assign it to a const_tree. */
11736 : 47124055074 : type = strip_array_types (CONST_CAST_TREE (type));
11737 : 47124055074 : if (type == error_mark_node
11738 : : /* Quals on a FUNCTION_TYPE are memfn quals. */
11739 : 47093971959 : || TREE_CODE (type) == FUNCTION_TYPE)
11740 : : return TYPE_UNQUALIFIED;
11741 : 47052595090 : quals = TYPE_QUALS (type);
11742 : : /* METHOD and REFERENCE_TYPEs should never have quals. */
11743 : 47052595090 : gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11744 : : && !TYPE_REF_P (type))
11745 : : || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11746 : : == TYPE_UNQUALIFIED));
11747 : : return quals;
11748 : : }
11749 : :
11750 : : /* Returns the function-ref-qualifier for TYPE */
11751 : :
11752 : : cp_ref_qualifier
11753 : 2690009191 : type_memfn_rqual (const_tree type)
11754 : : {
11755 : 2690009191 : gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11756 : :
11757 : 2690009191 : if (!FUNCTION_REF_QUALIFIED (type))
11758 : : return REF_QUAL_NONE;
11759 : 37120719 : else if (FUNCTION_RVALUE_QUALIFIED (type))
11760 : : return REF_QUAL_RVALUE;
11761 : : else
11762 : 18609933 : return REF_QUAL_LVALUE;
11763 : : }
11764 : :
11765 : : /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11766 : : METHOD_TYPE. */
11767 : :
11768 : : int
11769 : 969857458 : type_memfn_quals (const_tree type)
11770 : : {
11771 : 969857458 : if (TREE_CODE (type) == FUNCTION_TYPE)
11772 : 72818947 : return TYPE_QUALS (type);
11773 : 897038511 : else if (TREE_CODE (type) == METHOD_TYPE)
11774 : 897038511 : return cp_type_quals (class_of_this_parm (type));
11775 : : else
11776 : 0 : gcc_unreachable ();
11777 : : }
11778 : :
11779 : : /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11780 : : MEMFN_QUALS and its ref-qualifier to RQUAL. */
11781 : :
11782 : : tree
11783 : 50750929 : apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11784 : : {
11785 : : /* Could handle METHOD_TYPE here if necessary. */
11786 : 50750929 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11787 : 50750929 : if (TYPE_QUALS (type) == memfn_quals
11788 : 50750929 : && type_memfn_rqual (type) == rqual)
11789 : : return type;
11790 : :
11791 : : /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11792 : : complex. */
11793 : 646420 : tree result = build_qualified_type (type, memfn_quals);
11794 : 646420 : return build_ref_qualified_type (result, rqual);
11795 : : }
11796 : :
11797 : : /* Returns nonzero if TYPE is const or volatile. */
11798 : :
11799 : : bool
11800 : 970946839 : cv_qualified_p (const_tree type)
11801 : : {
11802 : 970946839 : int quals = cp_type_quals (type);
11803 : 970946839 : return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11804 : : }
11805 : :
11806 : : /* Returns nonzero if the TYPE contains a mutable member. */
11807 : :
11808 : : bool
11809 : 785184599 : cp_has_mutable_p (const_tree type)
11810 : : {
11811 : : /* This CONST_CAST is okay because strip_array_types returns its
11812 : : argument unmodified and we assign it to a const_tree. */
11813 : 785184599 : type = strip_array_types (CONST_CAST_TREE(type));
11814 : :
11815 : 785184599 : return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11816 : : }
11817 : :
11818 : : /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11819 : : TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11820 : : approximation. In particular, consider:
11821 : :
11822 : : int f();
11823 : : struct S { int i; };
11824 : : const S s = { f(); }
11825 : :
11826 : : Here, we will make "s" as TREE_READONLY (because it is declared
11827 : : "const") -- only to reverse ourselves upon seeing that the
11828 : : initializer is non-constant. */
11829 : :
11830 : : void
11831 : 835869942 : cp_apply_type_quals_to_decl (int type_quals, tree decl)
11832 : : {
11833 : 835869942 : tree type = TREE_TYPE (decl);
11834 : :
11835 : 835869942 : if (type == error_mark_node)
11836 : : return;
11837 : :
11838 : 835869283 : if (TREE_CODE (decl) == TYPE_DECL)
11839 : : return;
11840 : :
11841 : 748467885 : gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11842 : : && type_quals != TYPE_UNQUALIFIED));
11843 : :
11844 : : /* Avoid setting TREE_READONLY incorrectly. */
11845 : : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11846 : : constructor can produce constant init, so rely on cp_finish_decl to
11847 : : clear TREE_READONLY if the variable has non-constant init. */
11848 : :
11849 : : /* If the type has (or might have) a mutable component, that component
11850 : : might be modified. */
11851 : 748467885 : if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11852 : 67894509 : type_quals &= ~TYPE_QUAL_CONST;
11853 : :
11854 : 748467885 : c_apply_type_quals_to_decl (type_quals, decl);
11855 : : }
11856 : :
11857 : : /* Subroutine of casts_away_constness. Make T1 and T2 point at
11858 : : exemplar types such that casting T1 to T2 is casting away constness
11859 : : if and only if there is no implicit conversion from T1 to T2. */
11860 : :
11861 : : static void
11862 : 1097493 : casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11863 : : {
11864 : 1097493 : int quals1;
11865 : 1097493 : int quals2;
11866 : :
11867 : : /* [expr.const.cast]
11868 : :
11869 : : For multi-level pointer to members and multi-level mixed pointers
11870 : : and pointers to members (conv.qual), the "member" aspect of a
11871 : : pointer to member level is ignored when determining if a const
11872 : : cv-qualifier has been cast away. */
11873 : : /* [expr.const.cast]
11874 : :
11875 : : For two pointer types:
11876 : :
11877 : : X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11878 : : X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11879 : : K is min(N,M)
11880 : :
11881 : : casting from X1 to X2 casts away constness if, for a non-pointer
11882 : : type T there does not exist an implicit conversion (clause
11883 : : _conv_) from:
11884 : :
11885 : : Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11886 : :
11887 : : to
11888 : :
11889 : : Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11890 : 1097493 : if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11891 : 549091 : || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11892 : : {
11893 : 548574 : *t1 = cp_build_qualified_type (void_type_node,
11894 : : cp_type_quals (*t1));
11895 : 548574 : *t2 = cp_build_qualified_type (void_type_node,
11896 : : cp_type_quals (*t2));
11897 : 548574 : return;
11898 : : }
11899 : :
11900 : 548919 : quals1 = cp_type_quals (*t1);
11901 : 548919 : quals2 = cp_type_quals (*t2);
11902 : :
11903 : 548919 : if (TYPE_PTRDATAMEM_P (*t1))
11904 : 0 : *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
11905 : : else
11906 : 548919 : *t1 = TREE_TYPE (*t1);
11907 : 548919 : if (TYPE_PTRDATAMEM_P (*t2))
11908 : 0 : *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
11909 : : else
11910 : 548919 : *t2 = TREE_TYPE (*t2);
11911 : :
11912 : 548919 : casts_away_constness_r (t1, t2, complain);
11913 : 548919 : *t1 = build_pointer_type (*t1);
11914 : 548919 : *t2 = build_pointer_type (*t2);
11915 : 548919 : *t1 = cp_build_qualified_type (*t1, quals1);
11916 : 548919 : *t2 = cp_build_qualified_type (*t2, quals2);
11917 : : }
11918 : :
11919 : : /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11920 : : constness.
11921 : :
11922 : : ??? This function returns non-zero if casting away qualifiers not
11923 : : just const. We would like to return to the caller exactly which
11924 : : qualifiers are casted away to give more accurate diagnostics.
11925 : : */
11926 : :
11927 : : static bool
11928 : 548646 : casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
11929 : : {
11930 : 548646 : if (TYPE_REF_P (t2))
11931 : : {
11932 : : /* [expr.const.cast]
11933 : :
11934 : : Casting from an lvalue of type T1 to an lvalue of type T2
11935 : : using a reference cast casts away constness if a cast from an
11936 : : rvalue of type "pointer to T1" to the type "pointer to T2"
11937 : : casts away constness. */
11938 : 0 : t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
11939 : 0 : return casts_away_constness (build_pointer_type (t1),
11940 : 0 : build_pointer_type (TREE_TYPE (t2)),
11941 : 0 : complain);
11942 : : }
11943 : :
11944 : 548646 : if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
11945 : : /* [expr.const.cast]
11946 : :
11947 : : Casting from an rvalue of type "pointer to data member of X
11948 : : of type T1" to the type "pointer to data member of Y of type
11949 : : T2" casts away constness if a cast from an rvalue of type
11950 : : "pointer to T1" to the type "pointer to T2" casts away
11951 : : constness. */
11952 : 48 : return casts_away_constness
11953 : 48 : (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
11954 : 48 : build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
11955 : 48 : complain);
11956 : :
11957 : : /* Casting away constness is only something that makes sense for
11958 : : pointer or reference types. */
11959 : 548598 : if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
11960 : : return false;
11961 : :
11962 : : /* Top-level qualifiers don't matter. */
11963 : 548574 : t1 = TYPE_MAIN_VARIANT (t1);
11964 : 548574 : t2 = TYPE_MAIN_VARIANT (t2);
11965 : 548574 : casts_away_constness_r (&t1, &t2, complain);
11966 : 548574 : if (!can_convert (t2, t1, complain))
11967 : : return true;
11968 : :
11969 : : return false;
11970 : : }
11971 : :
11972 : : /* If T is a REFERENCE_TYPE return the type to which T refers.
11973 : : Otherwise, return T itself. */
11974 : :
11975 : : tree
11976 : 2885653568 : non_reference (tree t)
11977 : : {
11978 : 2885653568 : if (t && TYPE_REF_P (t))
11979 : 274659581 : t = TREE_TYPE (t);
11980 : 2885653568 : return t;
11981 : : }
11982 : :
11983 : :
11984 : : /* Return nonzero if REF is an lvalue valid for this language;
11985 : : otherwise, print an error message and return zero. USE says
11986 : : how the lvalue is being used and so selects the error message. */
11987 : :
11988 : : int
11989 : 36517455 : lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
11990 : : {
11991 : 36517455 : cp_lvalue_kind kind = lvalue_kind (ref);
11992 : :
11993 : 36517455 : if (kind == clk_none)
11994 : : {
11995 : 128 : if (complain & tf_error)
11996 : 115 : lvalue_error (cp_expr_loc_or_input_loc (ref), use);
11997 : 128 : return 0;
11998 : : }
11999 : 36517327 : else if (kind & (clk_rvalueref|clk_class))
12000 : : {
12001 : 104 : if (!(complain & tf_error))
12002 : : return 0;
12003 : : /* Make this a permerror because we used to accept it. */
12004 : 18 : permerror (cp_expr_loc_or_input_loc (ref),
12005 : : "using rvalue as lvalue");
12006 : : }
12007 : : return 1;
12008 : : }
12009 : :
12010 : : /* Return true if a user-defined literal operator is a raw operator. */
12011 : :
12012 : : bool
12013 : 117754 : check_raw_literal_operator (const_tree decl)
12014 : : {
12015 : 117754 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12016 : 117754 : tree argtype;
12017 : 117754 : int arity;
12018 : 117754 : bool maybe_raw_p = false;
12019 : :
12020 : : /* Count the number and type of arguments and check for ellipsis. */
12021 : 117754 : for (argtype = argtypes, arity = 0;
12022 : 176648 : argtype && argtype != void_list_node;
12023 : 58894 : ++arity, argtype = TREE_CHAIN (argtype))
12024 : : {
12025 : 58894 : tree t = TREE_VALUE (argtype);
12026 : :
12027 : 58894 : if (same_type_p (t, const_string_type_node))
12028 : 9 : maybe_raw_p = true;
12029 : : }
12030 : 117754 : if (!argtype)
12031 : : return false; /* Found ellipsis. */
12032 : :
12033 : 117754 : if (!maybe_raw_p || arity != 1)
12034 : 117748 : return false;
12035 : :
12036 : : return true;
12037 : : }
12038 : :
12039 : :
12040 : : /* Return true if a user-defined literal operator has one of the allowed
12041 : : argument types. */
12042 : :
12043 : : bool
12044 : 276920 : check_literal_operator_args (const_tree decl,
12045 : : bool *long_long_unsigned_p, bool *long_double_p)
12046 : : {
12047 : 276920 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12048 : :
12049 : 276920 : *long_long_unsigned_p = false;
12050 : 276920 : *long_double_p = false;
12051 : 276920 : if (processing_template_decl || processing_specialization)
12052 : 58998 : return argtypes == void_list_node;
12053 : : else
12054 : : {
12055 : : tree argtype;
12056 : : int arity;
12057 : : int max_arity = 2;
12058 : :
12059 : : /* Count the number and type of arguments and check for ellipsis. */
12060 : 217828 : for (argtype = argtypes, arity = 0;
12061 : 435750 : argtype && argtype != void_list_node;
12062 : 217828 : argtype = TREE_CHAIN (argtype))
12063 : : {
12064 : 217928 : tree t = TREE_VALUE (argtype);
12065 : 217928 : ++arity;
12066 : :
12067 : 217928 : if (TYPE_PTR_P (t))
12068 : : {
12069 : 96534 : bool maybe_raw_p = false;
12070 : 96534 : t = TREE_TYPE (t);
12071 : 96534 : if (cp_type_quals (t) != TYPE_QUAL_CONST)
12072 : : return false;
12073 : 96531 : t = TYPE_MAIN_VARIANT (t);
12074 : 96531 : if ((maybe_raw_p = same_type_p (t, char_type_node))
12075 : 73659 : || same_type_p (t, wchar_type_node)
12076 : 50937 : || same_type_p (t, char8_type_node)
12077 : 45456 : || same_type_p (t, char16_type_node)
12078 : 119259 : || same_type_p (t, char32_type_node))
12079 : : {
12080 : 96528 : argtype = TREE_CHAIN (argtype);
12081 : 96528 : if (!argtype)
12082 : : return false;
12083 : 96528 : t = TREE_VALUE (argtype);
12084 : 96528 : if (maybe_raw_p && argtype == void_list_node)
12085 : : return true;
12086 : 96455 : else if (same_type_p (t, size_type_node))
12087 : : {
12088 : 96449 : ++arity;
12089 : 96449 : continue;
12090 : : }
12091 : : else
12092 : : return false;
12093 : : }
12094 : : }
12095 : 121394 : else if (same_type_p (t, long_long_unsigned_type_node))
12096 : : {
12097 : 33460 : max_arity = 1;
12098 : 33460 : *long_long_unsigned_p = true;
12099 : : }
12100 : 87934 : else if (same_type_p (t, long_double_type_node))
12101 : : {
12102 : 87834 : max_arity = 1;
12103 : 87834 : *long_double_p = true;
12104 : : }
12105 : 100 : else if (same_type_p (t, char_type_node))
12106 : : max_arity = 1;
12107 : 56 : else if (same_type_p (t, wchar_type_node))
12108 : : max_arity = 1;
12109 : 45 : else if (same_type_p (t, char8_type_node))
12110 : : max_arity = 1;
12111 : 40 : else if (same_type_p (t, char16_type_node))
12112 : : max_arity = 1;
12113 : 29 : else if (same_type_p (t, char32_type_node))
12114 : : max_arity = 1;
12115 : : else
12116 : : return false;
12117 : : }
12118 : 217822 : if (!argtype)
12119 : : return false; /* Found ellipsis. */
12120 : :
12121 : 217819 : if (arity != max_arity)
12122 : : return false;
12123 : :
12124 : : return true;
12125 : : }
12126 : : }
12127 : :
12128 : : /* Always returns false since unlike C90, C++ has no concept of implicit
12129 : : function declarations. */
12130 : :
12131 : : bool
12132 : 359 : c_decl_implicit (const_tree)
12133 : : {
12134 : 359 : return false;
12135 : : }
|