1 | /* Build expressions with type checking for C++ compiler. |
2 | Copyright (C) 1987-2023 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 "gcc-rich-location.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 | require_complete_type (tree value, |
75 | tsubst_flags_t complain /* = tf_warning_or_error */) |
76 | { |
77 | tree type; |
78 | |
79 | if (processing_template_decl || value == error_mark_node) |
80 | return value; |
81 | |
82 | if (TREE_CODE (value) == OVERLOAD) |
83 | type = unknown_type_node; |
84 | else |
85 | type = TREE_TYPE (value); |
86 | |
87 | if (type == error_mark_node) |
88 | return error_mark_node; |
89 | |
90 | /* First, detect a valid value with a complete type. */ |
91 | if (COMPLETE_TYPE_P (type)) |
92 | return value; |
93 | |
94 | if (complete_type_or_maybe_complain (type, value, complain)) |
95 | return value; |
96 | else |
97 | 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 | complete_type (tree type) |
107 | { |
108 | if (type == NULL_TREE) |
109 | /* Rather than crash, we return something sure to cause an error |
110 | at some point. */ |
111 | return error_mark_node; |
112 | |
113 | if (type == error_mark_node || COMPLETE_TYPE_P (type)) |
114 | ; |
115 | else if (TREE_CODE (type) == ARRAY_TYPE) |
116 | { |
117 | tree t = complete_type (TREE_TYPE (type)); |
118 | unsigned int needs_constructing, has_nontrivial_dtor; |
119 | if (COMPLETE_TYPE_P (t) && !dependent_type_p (type)) |
120 | layout_type (type); |
121 | needs_constructing |
122 | = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t)); |
123 | has_nontrivial_dtor |
124 | = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t)); |
125 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
126 | { |
127 | TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing; |
128 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor; |
129 | } |
130 | } |
131 | else if (CLASS_TYPE_P (type)) |
132 | { |
133 | if (modules_p ()) |
134 | /* TYPE could be a class member we've not loaded the definition of. */ |
135 | lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type))); |
136 | |
137 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) |
138 | 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 | complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain) |
150 | { |
151 | type = complete_type (type); |
152 | if (type == error_mark_node) |
153 | /* We already issued an error. */ |
154 | return NULL_TREE; |
155 | else if (!COMPLETE_TYPE_P (type)) |
156 | { |
157 | if (complain & tf_error) |
158 | cxx_incomplete_type_diagnostic (value, type, diag_kind: DK_ERROR); |
159 | note_failed_type_completion_for_satisfaction (type); |
160 | return NULL_TREE; |
161 | } |
162 | else |
163 | return type; |
164 | } |
165 | |
166 | tree |
167 | complete_type_or_else (tree type, tree value) |
168 | { |
169 | return complete_type_or_maybe_complain (type, value, complain: 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 | commonparms (tree p1, tree p2) |
182 | { |
183 | tree oldargs = p1, newargs, n; |
184 | int i, len; |
185 | int any_change = 0; |
186 | |
187 | len = list_length (p1); |
188 | newargs = tree_last (p1); |
189 | |
190 | if (newargs == void_list_node) |
191 | i = 1; |
192 | else |
193 | { |
194 | i = 0; |
195 | newargs = 0; |
196 | } |
197 | |
198 | for (; i < len; i++) |
199 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
200 | |
201 | n = newargs; |
202 | |
203 | for (i = 0; p1; |
204 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++) |
205 | { |
206 | if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2)) |
207 | { |
208 | TREE_PURPOSE (n) = TREE_PURPOSE (p1); |
209 | any_change = 1; |
210 | } |
211 | else if (! TREE_PURPOSE (p1)) |
212 | { |
213 | if (TREE_PURPOSE (p2)) |
214 | { |
215 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); |
216 | any_change = 1; |
217 | } |
218 | } |
219 | else |
220 | { |
221 | if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1) |
222 | any_change = 1; |
223 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); |
224 | } |
225 | if (TREE_VALUE (p1) != TREE_VALUE (p2)) |
226 | { |
227 | any_change = 1; |
228 | TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2)); |
229 | } |
230 | else |
231 | TREE_VALUE (n) = TREE_VALUE (p1); |
232 | } |
233 | if (! any_change) |
234 | 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 | original_type (tree t) |
243 | { |
244 | int quals = cp_type_quals (t); |
245 | while (t != error_mark_node |
246 | && TYPE_NAME (t) != NULL_TREE) |
247 | { |
248 | tree x = TYPE_NAME (t); |
249 | if (TREE_CODE (x) != TYPE_DECL) |
250 | break; |
251 | x = DECL_ORIGINAL_TYPE (x); |
252 | if (x == NULL_TREE) |
253 | break; |
254 | t = x; |
255 | } |
256 | 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 | merge_type_attributes_from (tree type, tree other_type) |
264 | { |
265 | tree attrs = targetm.merge_type_attributes (type, other_type); |
266 | attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type)); |
267 | 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 | cp_compare_floating_point_conversion_ranks (tree t1, tree t2) |
280 | { |
281 | tree mv1 = TYPE_MAIN_VARIANT (t1); |
282 | tree mv2 = TYPE_MAIN_VARIANT (t2); |
283 | int extended1 = 0; |
284 | int extended2 = 0; |
285 | |
286 | if (mv1 == mv2) |
287 | return 0; |
288 | |
289 | for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i) |
290 | { |
291 | if (mv1 == FLOATN_NX_TYPE_NODE (i)) |
292 | extended1 = i + 1; |
293 | if (mv2 == FLOATN_NX_TYPE_NODE (i)) |
294 | extended2 = i + 1; |
295 | } |
296 | if (mv1 == bfloat16_type_node) |
297 | extended1 = true; |
298 | if (mv2 == bfloat16_type_node) |
299 | extended2 = true; |
300 | if (extended2 && !extended1) |
301 | { |
302 | int ret = cp_compare_floating_point_conversion_ranks (t1: t2, t2: t1); |
303 | return ret == 3 ? 3 : -ret; |
304 | } |
305 | |
306 | const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1)); |
307 | const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2)); |
308 | 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 | int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1)) |
315 | ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p); |
316 | int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2)) |
317 | ? 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 | if ((p1 > p2 && fmt1->emax >= fmt2->emax) |
322 | || (p1 == p2 && fmt1->emax > fmt2->emax)) |
323 | return 2; |
324 | if ((p1 < p2 && fmt1->emax <= fmt2->emax) |
325 | || (p1 == p2 && fmt1->emax < fmt2->emax)) |
326 | return -2; |
327 | if ((p1 > p2 && fmt1->emax < fmt2->emax) |
328 | || (p1 < p2 && fmt1->emax > fmt2->emax)) |
329 | return 3; |
330 | 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 | if (t1 == long_double_type_node) |
335 | return 2; |
336 | else if (t2 == long_double_type_node) |
337 | return -2; |
338 | if (t1 == double_type_node) |
339 | return 2; |
340 | else if (t2 == double_type_node) |
341 | return -2; |
342 | if (t1 == float_type_node) |
343 | return 2; |
344 | 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 | if (extended1 && extended2) |
351 | { |
352 | if ((extended1 <= NUM_FLOATN_TYPES) == (extended2 <= NUM_FLOATN_TYPES)) |
353 | { |
354 | /* Prefer higher extendedN value. */ |
355 | if (extended1 > extended2) |
356 | return 1; |
357 | else if (extended1 < extended2) |
358 | return -1; |
359 | else |
360 | return 0; |
361 | } |
362 | else if (extended1 <= NUM_FLOATN_TYPES) |
363 | /* Prefer _FloatN type over _FloatMx type. */ |
364 | return 1; |
365 | 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 | for (p = &float_type_node; p <= &long_double_type_node; ++p) |
375 | { |
376 | const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p)); |
377 | gcc_assert (fmt3->b == 2); |
378 | int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p)) |
379 | ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p); |
380 | if (p1 == p3 && fmt1->emax == fmt3->emax) |
381 | ++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 | if (cnt > 1 && mv2 == long_double_type_node) |
395 | return -2; |
396 | /* Otherwise, they have equal rank, but extended types |
397 | (other than std::bfloat16_t) have higher subrank. |
398 | std::bfloat16_t shouldn't have equal rank to any standard |
399 | floating point type. */ |
400 | return 1; |
401 | } |
402 | |
403 | /* Return the common type for two arithmetic types T1 and T2 under the |
404 | usual arithmetic conversions. The default conversions have already |
405 | been applied, and enumerated types converted to their compatible |
406 | integer types. */ |
407 | |
408 | static tree |
409 | cp_common_type (tree t1, tree t2) |
410 | { |
411 | enum tree_code code1 = TREE_CODE (t1); |
412 | enum tree_code code2 = TREE_CODE (t2); |
413 | tree attributes; |
414 | int i; |
415 | |
416 | |
417 | /* In what follows, we slightly generalize the rules given in [expr] so |
418 | as to deal with `long long' and `complex'. First, merge the |
419 | attributes. */ |
420 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
421 | |
422 | if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2)) |
423 | { |
424 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) |
425 | return build_type_attribute_variant (t1, attributes); |
426 | else |
427 | return NULL_TREE; |
428 | } |
429 | |
430 | /* FIXME: Attributes. */ |
431 | gcc_assert (ARITHMETIC_TYPE_P (t1) |
432 | || VECTOR_TYPE_P (t1) |
433 | || UNSCOPED_ENUM_P (t1)); |
434 | gcc_assert (ARITHMETIC_TYPE_P (t2) |
435 | || VECTOR_TYPE_P (t2) |
436 | || UNSCOPED_ENUM_P (t2)); |
437 | |
438 | /* If one type is complex, form the common type of the non-complex |
439 | components, then make that complex. Use T1 or T2 if it is the |
440 | required type. */ |
441 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) |
442 | { |
443 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; |
444 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; |
445 | tree subtype |
446 | = type_after_usual_arithmetic_conversions (subtype1, subtype2); |
447 | |
448 | if (subtype == error_mark_node) |
449 | return subtype; |
450 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) |
451 | return build_type_attribute_variant (t1, attributes); |
452 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) |
453 | return build_type_attribute_variant (t2, attributes); |
454 | else |
455 | return build_type_attribute_variant (build_complex_type (subtype), |
456 | attributes); |
457 | } |
458 | |
459 | if (code1 == VECTOR_TYPE) |
460 | { |
461 | /* When we get here we should have two vectors of the same size. |
462 | Just prefer the unsigned one if present. */ |
463 | if (TYPE_UNSIGNED (t1)) |
464 | return merge_type_attributes_from (type: t1, other_type: t2); |
465 | else |
466 | return merge_type_attributes_from (type: t2, other_type: t1); |
467 | } |
468 | |
469 | /* If only one is real, use it as the result. */ |
470 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) |
471 | return build_type_attribute_variant (t1, attributes); |
472 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) |
473 | return build_type_attribute_variant (t2, attributes); |
474 | |
475 | if (code1 == REAL_TYPE |
476 | && (extended_float_type_p (type: t1) || extended_float_type_p (type: t2))) |
477 | { |
478 | tree mv1 = TYPE_MAIN_VARIANT (t1); |
479 | tree mv2 = TYPE_MAIN_VARIANT (t2); |
480 | if (mv1 == mv2) |
481 | return build_type_attribute_variant (t1, attributes); |
482 | |
483 | int cmpret = cp_compare_floating_point_conversion_ranks (t1: mv1, t2: mv2); |
484 | if (cmpret == 3) |
485 | return error_mark_node; |
486 | else if (cmpret >= 0) |
487 | return build_type_attribute_variant (t1, attributes); |
488 | else |
489 | return build_type_attribute_variant (t2, attributes); |
490 | } |
491 | |
492 | /* Both real or both integers; use the one with greater precision. */ |
493 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) |
494 | return build_type_attribute_variant (t1, attributes); |
495 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) |
496 | return build_type_attribute_variant (t2, attributes); |
497 | |
498 | /* The types are the same; no need to do anything fancy. */ |
499 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) |
500 | return build_type_attribute_variant (t1, attributes); |
501 | |
502 | if (code1 != REAL_TYPE) |
503 | { |
504 | /* If one is unsigned long long, then convert the other to unsigned |
505 | long long. */ |
506 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node) |
507 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node)) |
508 | return build_type_attribute_variant (long_long_unsigned_type_node, |
509 | attributes); |
510 | /* If one is a long long, and the other is an unsigned long, and |
511 | long long can represent all the values of an unsigned long, then |
512 | convert to a long long. Otherwise, convert to an unsigned long |
513 | long. Otherwise, if either operand is long long, convert the |
514 | other to long long. |
515 | |
516 | Since we're here, we know the TYPE_PRECISION is the same; |
517 | therefore converting to long long cannot represent all the values |
518 | of an unsigned long, so we choose unsigned long long in that |
519 | case. */ |
520 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node) |
521 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node)) |
522 | { |
523 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
524 | ? long_long_unsigned_type_node |
525 | : long_long_integer_type_node); |
526 | return build_type_attribute_variant (t, attributes); |
527 | } |
528 | |
529 | /* Go through the same procedure, but for longs. */ |
530 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node) |
531 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node)) |
532 | return build_type_attribute_variant (long_unsigned_type_node, |
533 | attributes); |
534 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node) |
535 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node)) |
536 | { |
537 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
538 | ? long_unsigned_type_node : long_integer_type_node); |
539 | return build_type_attribute_variant (t, attributes); |
540 | } |
541 | |
542 | /* For __intN types, either the type is __int128 (and is lower |
543 | priority than the types checked above, but higher than other |
544 | 128-bit types) or it's known to not be the same size as other |
545 | types (enforced in toplev.cc). Prefer the unsigned type. */ |
546 | for (i = 0; i < NUM_INT_N_ENTS; i ++) |
547 | { |
548 | if (int_n_enabled_p [i] |
549 | && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type) |
550 | || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type) |
551 | || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type) |
552 | || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type))) |
553 | { |
554 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
555 | ? int_n_trees[i].unsigned_type |
556 | : int_n_trees[i].signed_type); |
557 | return build_type_attribute_variant (t, attributes); |
558 | } |
559 | } |
560 | |
561 | /* Otherwise prefer the unsigned one. */ |
562 | if (TYPE_UNSIGNED (t1)) |
563 | return build_type_attribute_variant (t1, attributes); |
564 | else |
565 | return build_type_attribute_variant (t2, attributes); |
566 | } |
567 | else |
568 | { |
569 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node) |
570 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node)) |
571 | return build_type_attribute_variant (long_double_type_node, |
572 | attributes); |
573 | if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node) |
574 | || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node)) |
575 | return build_type_attribute_variant (double_type_node, |
576 | attributes); |
577 | if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node) |
578 | || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node)) |
579 | return build_type_attribute_variant (float_type_node, |
580 | attributes); |
581 | |
582 | /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of |
583 | the standard C++ floating-point types. Logic earlier in this |
584 | function has already eliminated the possibility that |
585 | TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no |
586 | compelling reason to choose one or the other. */ |
587 | return build_type_attribute_variant (t1, attributes); |
588 | } |
589 | } |
590 | |
591 | /* T1 and T2 are arithmetic or enumeration types. Return the type |
592 | that will result from the "usual arithmetic conversions" on T1 and |
593 | T2 as described in [expr]. */ |
594 | |
595 | tree |
596 | type_after_usual_arithmetic_conversions (tree t1, tree t2) |
597 | { |
598 | gcc_assert (ARITHMETIC_TYPE_P (t1) |
599 | || VECTOR_TYPE_P (t1) |
600 | || UNSCOPED_ENUM_P (t1)); |
601 | gcc_assert (ARITHMETIC_TYPE_P (t2) |
602 | || VECTOR_TYPE_P (t2) |
603 | || UNSCOPED_ENUM_P (t2)); |
604 | |
605 | /* Perform the integral promotions. We do not promote real types here. */ |
606 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1) |
607 | && INTEGRAL_OR_ENUMERATION_TYPE_P (t2)) |
608 | { |
609 | t1 = type_promotes_to (t1); |
610 | t2 = type_promotes_to (t2); |
611 | } |
612 | |
613 | return cp_common_type (t1, t2); |
614 | } |
615 | |
616 | static void |
617 | composite_pointer_error (const op_location_t &location, |
618 | diagnostic_t kind, tree t1, tree t2, |
619 | composite_pointer_operation operation) |
620 | { |
621 | switch (operation) |
622 | { |
623 | case CPO_COMPARISON: |
624 | emit_diagnostic (kind, location, 0, |
625 | "comparison between " |
626 | "distinct pointer types %qT and %qT lacks a cast" , |
627 | t1, t2); |
628 | break; |
629 | case CPO_CONVERSION: |
630 | emit_diagnostic (kind, location, 0, |
631 | "conversion between " |
632 | "distinct pointer types %qT and %qT lacks a cast" , |
633 | t1, t2); |
634 | break; |
635 | case CPO_CONDITIONAL_EXPR: |
636 | emit_diagnostic (kind, location, 0, |
637 | "conditional expression between " |
638 | "distinct pointer types %qT and %qT lacks a cast" , |
639 | t1, t2); |
640 | break; |
641 | default: |
642 | gcc_unreachable (); |
643 | } |
644 | } |
645 | |
646 | /* Subroutine of composite_pointer_type to implement the recursive |
647 | case. See that function for documentation of the parameters. And ADD_CONST |
648 | is used to track adding "const" where needed. */ |
649 | |
650 | static tree |
651 | composite_pointer_type_r (const op_location_t &location, |
652 | tree t1, tree t2, bool *add_const, |
653 | composite_pointer_operation operation, |
654 | tsubst_flags_t complain) |
655 | { |
656 | tree pointee1; |
657 | tree pointee2; |
658 | tree result_type; |
659 | tree attributes; |
660 | |
661 | /* Determine the types pointed to by T1 and T2. */ |
662 | if (TYPE_PTR_P (t1)) |
663 | { |
664 | pointee1 = TREE_TYPE (t1); |
665 | pointee2 = TREE_TYPE (t2); |
666 | } |
667 | else |
668 | { |
669 | pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1); |
670 | pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2); |
671 | } |
672 | |
673 | /* [expr.type] |
674 | |
675 | If T1 and T2 are similar types, the result is the cv-combined type of |
676 | T1 and T2. */ |
677 | if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2)) |
678 | result_type = pointee1; |
679 | else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2)) |
680 | || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2))) |
681 | { |
682 | result_type = composite_pointer_type_r (location, t1: pointee1, t2: pointee2, |
683 | add_const, operation, complain); |
684 | if (result_type == error_mark_node) |
685 | return error_mark_node; |
686 | } |
687 | else |
688 | { |
689 | if (complain & tf_error) |
690 | composite_pointer_error (location, kind: DK_PERMERROR, |
691 | t1, t2, operation); |
692 | else |
693 | return error_mark_node; |
694 | result_type = void_type_node; |
695 | } |
696 | const int q1 = cp_type_quals (pointee1); |
697 | const int q2 = cp_type_quals (pointee2); |
698 | const int quals = q1 | q2; |
699 | result_type = cp_build_qualified_type (result_type, |
700 | (quals | (*add_const |
701 | ? TYPE_QUAL_CONST |
702 | : TYPE_UNQUALIFIED))); |
703 | /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for |
704 | the TLQ). The reason is that both T1 and T2 can then be converted to the |
705 | cv-combined type of T1 and T2. */ |
706 | if (quals != q1 || quals != q2) |
707 | *add_const = true; |
708 | /* If the original types were pointers to members, so is the |
709 | result. */ |
710 | if (TYPE_PTRMEM_P (t1)) |
711 | { |
712 | if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), |
713 | TYPE_PTRMEM_CLASS_TYPE (t2))) |
714 | { |
715 | if (complain & tf_error) |
716 | composite_pointer_error (location, kind: DK_PERMERROR, |
717 | t1, t2, operation); |
718 | else |
719 | return error_mark_node; |
720 | } |
721 | result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), |
722 | result_type); |
723 | } |
724 | else |
725 | result_type = build_pointer_type (result_type); |
726 | |
727 | /* Merge the attributes. */ |
728 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
729 | return build_type_attribute_variant (result_type, attributes); |
730 | } |
731 | |
732 | /* Return the composite pointer type (see [expr.type]) for T1 and T2. |
733 | ARG1 and ARG2 are the values with those types. The OPERATION is to |
734 | describe the operation between the pointer types, |
735 | in case an error occurs. |
736 | |
737 | This routine also implements the computation of a common type for |
738 | pointers-to-members as per [expr.eq]. */ |
739 | |
740 | tree |
741 | composite_pointer_type (const op_location_t &location, |
742 | tree t1, tree t2, tree arg1, tree arg2, |
743 | composite_pointer_operation operation, |
744 | tsubst_flags_t complain) |
745 | { |
746 | tree class1; |
747 | tree class2; |
748 | |
749 | /* [expr.type] |
750 | |
751 | If one operand is a null pointer constant, the composite pointer |
752 | type is the type of the other operand. */ |
753 | if (null_ptr_cst_p (arg1)) |
754 | return t2; |
755 | if (null_ptr_cst_p (arg2)) |
756 | return t1; |
757 | |
758 | /* We have: |
759 | |
760 | [expr.type] |
761 | |
762 | If one of the operands has type "pointer to cv1 void", then |
763 | the other has type "pointer to cv2 T", and the composite pointer |
764 | type is "pointer to cv12 void", where cv12 is the union of cv1 |
765 | and cv2. |
766 | |
767 | If either type is a pointer to void, make sure it is T1. */ |
768 | if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2))) |
769 | std::swap (a&: t1, b&: t2); |
770 | |
771 | /* Now, if T1 is a pointer to void, merge the qualifiers. */ |
772 | if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1))) |
773 | { |
774 | tree attributes; |
775 | tree result_type; |
776 | |
777 | if (TYPE_PTRFN_P (t2)) |
778 | { |
779 | if (complain & tf_error) |
780 | { |
781 | switch (operation) |
782 | { |
783 | case CPO_COMPARISON: |
784 | pedwarn (location, OPT_Wpedantic, |
785 | "ISO C++ forbids comparison between pointer " |
786 | "of type %<void *%> and pointer-to-function" ); |
787 | break; |
788 | case CPO_CONVERSION: |
789 | pedwarn (location, OPT_Wpedantic, |
790 | "ISO C++ forbids conversion between pointer " |
791 | "of type %<void *%> and pointer-to-function" ); |
792 | break; |
793 | case CPO_CONDITIONAL_EXPR: |
794 | pedwarn (location, OPT_Wpedantic, |
795 | "ISO C++ forbids conditional expression between " |
796 | "pointer of type %<void *%> and " |
797 | "pointer-to-function" ); |
798 | break; |
799 | default: |
800 | gcc_unreachable (); |
801 | } |
802 | } |
803 | else |
804 | return error_mark_node; |
805 | } |
806 | result_type |
807 | = cp_build_qualified_type (void_type_node, |
808 | (cp_type_quals (TREE_TYPE (t1)) |
809 | | cp_type_quals (TREE_TYPE (t2)))); |
810 | result_type = build_pointer_type (result_type); |
811 | /* Merge the attributes. */ |
812 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
813 | return build_type_attribute_variant (result_type, attributes); |
814 | } |
815 | |
816 | if (c_dialect_objc () && TYPE_PTR_P (t1) |
817 | && TYPE_PTR_P (t2)) |
818 | { |
819 | if (objc_have_common_type (t1, t2, -3, NULL_TREE)) |
820 | return objc_common_type (t1, t2); |
821 | } |
822 | |
823 | /* if T1 or T2 is "pointer to noexcept function" and the other type is |
824 | "pointer to function", where the function types are otherwise the same, |
825 | "pointer to function" */ |
826 | if (fnptr_conv_p (t1, t2)) |
827 | return t1; |
828 | if (fnptr_conv_p (t2, t1)) |
829 | return t2; |
830 | |
831 | /* [expr.eq] permits the application of a pointer conversion to |
832 | bring the pointers to a common type. */ |
833 | if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2) |
834 | && CLASS_TYPE_P (TREE_TYPE (t1)) |
835 | && CLASS_TYPE_P (TREE_TYPE (t2)) |
836 | && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1), |
837 | TREE_TYPE (t2))) |
838 | { |
839 | class1 = TREE_TYPE (t1); |
840 | class2 = TREE_TYPE (t2); |
841 | |
842 | if (DERIVED_FROM_P (class1, class2)) |
843 | t2 = (build_pointer_type |
844 | (cp_build_qualified_type (class1, cp_type_quals (class2)))); |
845 | else if (DERIVED_FROM_P (class2, class1)) |
846 | t1 = (build_pointer_type |
847 | (cp_build_qualified_type (class2, cp_type_quals (class1)))); |
848 | else |
849 | { |
850 | if (complain & tf_error) |
851 | composite_pointer_error (location, kind: DK_ERROR, t1, t2, operation); |
852 | return error_mark_node; |
853 | } |
854 | } |
855 | /* [expr.eq] permits the application of a pointer-to-member |
856 | conversion to change the class type of one of the types. */ |
857 | else if (TYPE_PTRMEM_P (t1) |
858 | && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), |
859 | TYPE_PTRMEM_CLASS_TYPE (t2))) |
860 | { |
861 | class1 = TYPE_PTRMEM_CLASS_TYPE (t1); |
862 | class2 = TYPE_PTRMEM_CLASS_TYPE (t2); |
863 | |
864 | if (DERIVED_FROM_P (class1, class2)) |
865 | t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1)); |
866 | else if (DERIVED_FROM_P (class2, class1)) |
867 | t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2)); |
868 | else |
869 | { |
870 | if (complain & tf_error) |
871 | switch (operation) |
872 | { |
873 | case CPO_COMPARISON: |
874 | error_at (location, "comparison between distinct " |
875 | "pointer-to-member types %qT and %qT lacks a cast" , |
876 | t1, t2); |
877 | break; |
878 | case CPO_CONVERSION: |
879 | error_at (location, "conversion between distinct " |
880 | "pointer-to-member types %qT and %qT lacks a cast" , |
881 | t1, t2); |
882 | break; |
883 | case CPO_CONDITIONAL_EXPR: |
884 | error_at (location, "conditional expression between distinct " |
885 | "pointer-to-member types %qT and %qT lacks a cast" , |
886 | t1, t2); |
887 | break; |
888 | default: |
889 | gcc_unreachable (); |
890 | } |
891 | return error_mark_node; |
892 | } |
893 | } |
894 | |
895 | bool add_const = false; |
896 | return composite_pointer_type_r (location, t1, t2, add_const: &add_const, operation, |
897 | complain); |
898 | } |
899 | |
900 | /* Return the merged type of two types. |
901 | We assume that comptypes has already been done and returned 1; |
902 | if that isn't so, this may crash. |
903 | |
904 | This just combines attributes and default arguments; any other |
905 | differences would cause the two types to compare unalike. */ |
906 | |
907 | tree |
908 | merge_types (tree t1, tree t2) |
909 | { |
910 | enum tree_code code1; |
911 | enum tree_code code2; |
912 | tree attributes; |
913 | |
914 | /* Save time if the two types are the same. */ |
915 | if (t1 == t2) |
916 | return t1; |
917 | if (original_type (t: t1) == original_type (t: t2)) |
918 | return t1; |
919 | |
920 | /* If one type is nonsense, use the other. */ |
921 | if (t1 == error_mark_node) |
922 | return t2; |
923 | if (t2 == error_mark_node) |
924 | return t1; |
925 | |
926 | /* Handle merging an auto redeclaration with a previous deduced |
927 | return type. */ |
928 | if (is_auto (t1)) |
929 | return t2; |
930 | |
931 | /* Merge the attributes. */ |
932 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
933 | |
934 | if (TYPE_PTRMEMFUNC_P (t1)) |
935 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); |
936 | if (TYPE_PTRMEMFUNC_P (t2)) |
937 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); |
938 | |
939 | code1 = TREE_CODE (t1); |
940 | code2 = TREE_CODE (t2); |
941 | if (code1 != code2) |
942 | { |
943 | gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE); |
944 | if (code1 == TYPENAME_TYPE) |
945 | { |
946 | t1 = resolve_typename_type (t1, /*only_current_p=*/true); |
947 | code1 = TREE_CODE (t1); |
948 | } |
949 | else |
950 | { |
951 | t2 = resolve_typename_type (t2, /*only_current_p=*/true); |
952 | code2 = TREE_CODE (t2); |
953 | } |
954 | } |
955 | |
956 | switch (code1) |
957 | { |
958 | case POINTER_TYPE: |
959 | case REFERENCE_TYPE: |
960 | /* For two pointers, do this recursively on the target type. */ |
961 | { |
962 | tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
963 | int quals = cp_type_quals (t1); |
964 | |
965 | if (code1 == POINTER_TYPE) |
966 | { |
967 | t1 = build_pointer_type (target); |
968 | if (TREE_CODE (target) == METHOD_TYPE) |
969 | t1 = build_ptrmemfunc_type (t1); |
970 | } |
971 | else |
972 | t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1)); |
973 | t1 = build_type_attribute_variant (t1, attributes); |
974 | t1 = cp_build_qualified_type (t1, quals); |
975 | |
976 | return t1; |
977 | } |
978 | |
979 | case OFFSET_TYPE: |
980 | { |
981 | int quals; |
982 | tree pointee; |
983 | quals = cp_type_quals (t1); |
984 | pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1), |
985 | TYPE_PTRMEM_POINTED_TO_TYPE (t2)); |
986 | t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), |
987 | pointee); |
988 | t1 = cp_build_qualified_type (t1, quals); |
989 | break; |
990 | } |
991 | |
992 | case ARRAY_TYPE: |
993 | { |
994 | tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
995 | /* Save space: see if the result is identical to one of the args. */ |
996 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) |
997 | return build_type_attribute_variant (t1, attributes); |
998 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) |
999 | return build_type_attribute_variant (t2, attributes); |
1000 | /* Merge the element types, and have a size if either arg has one. */ |
1001 | t1 = build_cplus_array_type |
1002 | (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); |
1003 | break; |
1004 | } |
1005 | |
1006 | case FUNCTION_TYPE: |
1007 | /* Function types: prefer the one that specified arg types. |
1008 | If both do, merge the arg types. Also merge the return types. */ |
1009 | { |
1010 | tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
1011 | tree p1 = TYPE_ARG_TYPES (t1); |
1012 | tree p2 = TYPE_ARG_TYPES (t2); |
1013 | tree parms; |
1014 | |
1015 | /* Save space: see if the result is identical to one of the args. */ |
1016 | if (valtype == TREE_TYPE (t1) && ! p2) |
1017 | return cp_build_type_attribute_variant (t1, attributes); |
1018 | if (valtype == TREE_TYPE (t2) && ! p1) |
1019 | return cp_build_type_attribute_variant (t2, attributes); |
1020 | |
1021 | /* Simple way if one arg fails to specify argument types. */ |
1022 | if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node) |
1023 | parms = p2; |
1024 | else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node) |
1025 | parms = p1; |
1026 | else |
1027 | parms = commonparms (p1, p2); |
1028 | |
1029 | cp_cv_quals quals = type_memfn_quals (t1); |
1030 | cp_ref_qualifier rqual = type_memfn_rqual (t1); |
1031 | gcc_assert (quals == type_memfn_quals (t2)); |
1032 | gcc_assert (rqual == type_memfn_rqual (t2)); |
1033 | |
1034 | tree rval = build_function_type (valtype, parms); |
1035 | rval = apply_memfn_quals (rval, quals); |
1036 | tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), |
1037 | TYPE_RAISES_EXCEPTIONS (t2)); |
1038 | bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1); |
1039 | t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p); |
1040 | break; |
1041 | } |
1042 | |
1043 | case METHOD_TYPE: |
1044 | { |
1045 | /* Get this value the long way, since TYPE_METHOD_BASETYPE |
1046 | is just the main variant of this. */ |
1047 | tree basetype = class_of_this_parm (fntype: t2); |
1048 | tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), |
1049 | TYPE_RAISES_EXCEPTIONS (t2)); |
1050 | cp_ref_qualifier rqual = type_memfn_rqual (t1); |
1051 | tree t3; |
1052 | bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1); |
1053 | |
1054 | /* If this was a member function type, get back to the |
1055 | original type of type member function (i.e., without |
1056 | the class instance variable up front. */ |
1057 | t1 = build_function_type (TREE_TYPE (t1), |
1058 | TREE_CHAIN (TYPE_ARG_TYPES (t1))); |
1059 | t2 = build_function_type (TREE_TYPE (t2), |
1060 | TREE_CHAIN (TYPE_ARG_TYPES (t2))); |
1061 | t3 = merge_types (t1, t2); |
1062 | t3 = build_method_type_directly (basetype, TREE_TYPE (t3), |
1063 | TYPE_ARG_TYPES (t3)); |
1064 | t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p); |
1065 | break; |
1066 | } |
1067 | |
1068 | case TYPENAME_TYPE: |
1069 | /* There is no need to merge attributes into a TYPENAME_TYPE. |
1070 | When the type is instantiated it will have whatever |
1071 | attributes result from the instantiation. */ |
1072 | return t1; |
1073 | |
1074 | default:; |
1075 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) |
1076 | return t1; |
1077 | else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) |
1078 | return t2; |
1079 | break; |
1080 | } |
1081 | |
1082 | return cp_build_type_attribute_variant (t1, attributes); |
1083 | } |
1084 | |
1085 | /* Return the ARRAY_TYPE type without its domain. */ |
1086 | |
1087 | tree |
1088 | strip_array_domain (tree type) |
1089 | { |
1090 | tree t2; |
1091 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
1092 | if (TYPE_DOMAIN (type) == NULL_TREE) |
1093 | return type; |
1094 | t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); |
1095 | return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type)); |
1096 | } |
1097 | |
1098 | /* Wrapper around cp_common_type that is used by c-common.cc and other |
1099 | front end optimizations that remove promotions. |
1100 | |
1101 | Return the common type for two arithmetic types T1 and T2 under the |
1102 | usual arithmetic conversions. The default conversions have already |
1103 | been applied, and enumerated types converted to their compatible |
1104 | integer types. */ |
1105 | |
1106 | tree |
1107 | common_type (tree t1, tree t2) |
1108 | { |
1109 | /* If one type is nonsense, use the other */ |
1110 | if (t1 == error_mark_node) |
1111 | return t2; |
1112 | if (t2 == error_mark_node) |
1113 | return t1; |
1114 | |
1115 | return cp_common_type (t1, t2); |
1116 | } |
1117 | |
1118 | /* Return the common type of two pointer types T1 and T2. This is the |
1119 | type for the result of most arithmetic operations if the operands |
1120 | have the given two types. |
1121 | |
1122 | We assume that comp_target_types has already been done and returned |
1123 | nonzero; if that isn't so, this may crash. */ |
1124 | |
1125 | tree |
1126 | common_pointer_type (tree t1, tree t2) |
1127 | { |
1128 | gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2)) |
1129 | || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2)) |
1130 | || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2))); |
1131 | |
1132 | return composite_pointer_type (location: input_location, t1, t2, |
1133 | error_mark_node, error_mark_node, |
1134 | operation: CPO_CONVERSION, complain: tf_warning_or_error); |
1135 | } |
1136 | |
1137 | /* Compare two exception specifier types for exactness or subsetness, if |
1138 | allowed. Returns false for mismatch, true for match (same, or |
1139 | derived and !exact). |
1140 | |
1141 | [except.spec] "If a class X ... objects of class X or any class publicly |
1142 | and unambiguously derived from X. Similarly, if a pointer type Y * ... |
1143 | exceptions of type Y * or that are pointers to any type publicly and |
1144 | unambiguously derived from Y. Otherwise a function only allows exceptions |
1145 | that have the same type ..." |
1146 | This does not mention cv qualifiers and is different to what throw |
1147 | [except.throw] and catch [except.catch] will do. They will ignore the |
1148 | top level cv qualifiers, and allow qualifiers in the pointer to class |
1149 | example. |
1150 | |
1151 | We implement the letter of the standard. */ |
1152 | |
1153 | static bool |
1154 | comp_except_types (tree a, tree b, bool exact) |
1155 | { |
1156 | if (same_type_p (a, b)) |
1157 | return true; |
1158 | else if (!exact) |
1159 | { |
1160 | if (cp_type_quals (a) || cp_type_quals (b)) |
1161 | return false; |
1162 | |
1163 | if (TYPE_PTR_P (a) && TYPE_PTR_P (b)) |
1164 | { |
1165 | a = TREE_TYPE (a); |
1166 | b = TREE_TYPE (b); |
1167 | if (cp_type_quals (a) || cp_type_quals (b)) |
1168 | return false; |
1169 | } |
1170 | |
1171 | if (TREE_CODE (a) != RECORD_TYPE |
1172 | || TREE_CODE (b) != RECORD_TYPE) |
1173 | return false; |
1174 | |
1175 | if (publicly_uniquely_derived_p (a, b)) |
1176 | return true; |
1177 | } |
1178 | return false; |
1179 | } |
1180 | |
1181 | /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers. |
1182 | If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5). |
1183 | If EXACT is ce_type, the C++17 type compatibility rules apply. |
1184 | If EXACT is ce_normal, the compatibility rules in 15.4/3 apply. |
1185 | If EXACT is ce_exact, the specs must be exactly the same. Exception lists |
1186 | are unordered, but we've already filtered out duplicates. Most lists will |
1187 | be in order, we should try to make use of that. */ |
1188 | |
1189 | bool |
1190 | comp_except_specs (const_tree t1, const_tree t2, int exact) |
1191 | { |
1192 | const_tree probe; |
1193 | const_tree base; |
1194 | int length = 0; |
1195 | |
1196 | if (t1 == t2) |
1197 | return true; |
1198 | |
1199 | /* First handle noexcept. */ |
1200 | if (exact < ce_exact) |
1201 | { |
1202 | if (exact == ce_type |
1203 | && (canonical_eh_spec (CONST_CAST_TREE (t1)) |
1204 | == canonical_eh_spec (CONST_CAST_TREE (t2)))) |
1205 | return true; |
1206 | |
1207 | /* noexcept(false) is compatible with no exception-specification, |
1208 | and less strict than any spec. */ |
1209 | if (t1 == noexcept_false_spec) |
1210 | return t2 == NULL_TREE || exact == ce_derived; |
1211 | /* Even a derived noexcept(false) is compatible with no |
1212 | exception-specification. */ |
1213 | if (t2 == noexcept_false_spec) |
1214 | return t1 == NULL_TREE; |
1215 | |
1216 | /* Otherwise, if we aren't looking for an exact match, noexcept is |
1217 | equivalent to throw(). */ |
1218 | if (t1 == noexcept_true_spec) |
1219 | t1 = empty_except_spec; |
1220 | if (t2 == noexcept_true_spec) |
1221 | t2 = empty_except_spec; |
1222 | } |
1223 | |
1224 | /* If any noexcept is left, it is only comparable to itself; |
1225 | either we're looking for an exact match or we're redeclaring a |
1226 | template with dependent noexcept. */ |
1227 | if ((t1 && TREE_PURPOSE (t1)) |
1228 | || (t2 && TREE_PURPOSE (t2))) |
1229 | return (t1 && t2 |
1230 | && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))); |
1231 | |
1232 | if (t1 == NULL_TREE) /* T1 is ... */ |
1233 | return t2 == NULL_TREE || exact == ce_derived; |
1234 | if (!TREE_VALUE (t1)) /* t1 is EMPTY */ |
1235 | return t2 != NULL_TREE && !TREE_VALUE (t2); |
1236 | if (t2 == NULL_TREE) /* T2 is ... */ |
1237 | return false; |
1238 | if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */ |
1239 | return exact == ce_derived; |
1240 | |
1241 | /* Neither set is ... or EMPTY, make sure each part of T2 is in T1. |
1242 | Count how many we find, to determine exactness. For exact matching and |
1243 | ordered T1, T2, this is an O(n) operation, otherwise its worst case is |
1244 | O(nm). */ |
1245 | for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2)) |
1246 | { |
1247 | for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe)) |
1248 | { |
1249 | tree a = TREE_VALUE (probe); |
1250 | tree b = TREE_VALUE (t2); |
1251 | |
1252 | if (comp_except_types (a, b, exact)) |
1253 | { |
1254 | if (probe == base && exact > ce_derived) |
1255 | base = TREE_CHAIN (probe); |
1256 | length++; |
1257 | break; |
1258 | } |
1259 | } |
1260 | if (probe == NULL_TREE) |
1261 | return false; |
1262 | } |
1263 | return exact == ce_derived || base == NULL_TREE || length == list_length (t1); |
1264 | } |
1265 | |
1266 | /* Compare the array types T1 and T2. CB says how we should behave when |
1267 | comparing array bounds: bounds_none doesn't allow dimensionless arrays, |
1268 | bounds_either says than any array can be [], bounds_first means that |
1269 | onlt T1 can be an array with unknown bounds. STRICT is true if |
1270 | qualifiers must match when comparing the types of the array elements. */ |
1271 | |
1272 | static bool |
1273 | comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb, |
1274 | bool strict) |
1275 | { |
1276 | tree d1; |
1277 | tree d2; |
1278 | tree max1, max2; |
1279 | |
1280 | if (t1 == t2) |
1281 | return true; |
1282 | |
1283 | /* The type of the array elements must be the same. */ |
1284 | if (strict |
1285 | ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)) |
1286 | : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1287 | return false; |
1288 | |
1289 | d1 = TYPE_DOMAIN (t1); |
1290 | d2 = TYPE_DOMAIN (t2); |
1291 | |
1292 | if (d1 == d2) |
1293 | return true; |
1294 | |
1295 | /* If one of the arrays is dimensionless, and the other has a |
1296 | dimension, they are of different types. However, it is valid to |
1297 | write: |
1298 | |
1299 | extern int a[]; |
1300 | int a[3]; |
1301 | |
1302 | by [basic.link]: |
1303 | |
1304 | declarations for an array object can specify |
1305 | array types that differ by the presence or absence of a major |
1306 | array bound (_dcl.array_). */ |
1307 | if (!d1 && d2) |
1308 | return cb >= bounds_either; |
1309 | else if (d1 && !d2) |
1310 | return cb == bounds_either; |
1311 | |
1312 | /* Check that the dimensions are the same. */ |
1313 | |
1314 | if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))) |
1315 | return false; |
1316 | max1 = TYPE_MAX_VALUE (d1); |
1317 | max2 = TYPE_MAX_VALUE (d2); |
1318 | |
1319 | if (!cp_tree_equal (max1, max2)) |
1320 | return false; |
1321 | |
1322 | return true; |
1323 | } |
1324 | |
1325 | /* Compare the relative position of T1 and T2 into their respective |
1326 | template parameter list. |
1327 | T1 and T2 must be template parameter types. |
1328 | Return TRUE if T1 and T2 have the same position, FALSE otherwise. */ |
1329 | |
1330 | static bool |
1331 | comp_template_parms_position (tree t1, tree t2) |
1332 | { |
1333 | tree index1, index2; |
1334 | gcc_assert (t1 && t2 |
1335 | && TREE_CODE (t1) == TREE_CODE (t2) |
1336 | && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM |
1337 | || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM |
1338 | || TREE_CODE (t1) == TEMPLATE_TYPE_PARM)); |
1339 | |
1340 | index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1)); |
1341 | index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2)); |
1342 | |
1343 | /* Then compare their relative position. */ |
1344 | if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2) |
1345 | || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2) |
1346 | || (TEMPLATE_PARM_PARAMETER_PACK (index1) |
1347 | != TEMPLATE_PARM_PARAMETER_PACK (index2))) |
1348 | return false; |
1349 | |
1350 | /* In C++14 we can end up comparing 'auto' to a normal template |
1351 | parameter. Don't confuse them. */ |
1352 | if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2))) |
1353 | return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2); |
1354 | |
1355 | return true; |
1356 | } |
1357 | |
1358 | /* Heuristic check if two parameter types can be considered ABI-equivalent. */ |
1359 | |
1360 | static bool |
1361 | cxx_safe_arg_type_equiv_p (tree t1, tree t2) |
1362 | { |
1363 | t1 = TYPE_MAIN_VARIANT (t1); |
1364 | t2 = TYPE_MAIN_VARIANT (t2); |
1365 | |
1366 | if (TYPE_PTR_P (t1) |
1367 | && TYPE_PTR_P (t2)) |
1368 | return true; |
1369 | |
1370 | /* The signedness of the parameter matters only when an integral |
1371 | type smaller than int is promoted to int, otherwise only the |
1372 | precision of the parameter matters. |
1373 | This check should make sure that the callee does not see |
1374 | undefined values in argument registers. */ |
1375 | if (INTEGRAL_TYPE_P (t1) |
1376 | && INTEGRAL_TYPE_P (t2) |
1377 | && TYPE_PRECISION (t1) == TYPE_PRECISION (t2) |
1378 | && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2) |
1379 | || !targetm.calls.promote_prototypes (NULL_TREE) |
1380 | || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node))) |
1381 | return true; |
1382 | |
1383 | 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 | cxx_safe_function_type_cast_p (tree t1, tree t2) |
1390 | { |
1391 | if (TREE_TYPE (t1) == void_type_node && |
1392 | TYPE_ARG_TYPES (t1) == void_list_node) |
1393 | return true; |
1394 | |
1395 | if (TREE_TYPE (t2) == void_type_node && |
1396 | TYPE_ARG_TYPES (t2) == void_list_node) |
1397 | return true; |
1398 | |
1399 | if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1400 | return false; |
1401 | |
1402 | for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2); |
1403 | t1 && t2; |
1404 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) |
1405 | 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 | structural_comptypes (tree t1, tree t2, int strict) |
1415 | { |
1416 | /* Both should be types that are not obviously the same. */ |
1417 | 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 | if (!comparing_specializations) |
1422 | { |
1423 | /* TYPENAME_TYPEs should be resolved if the qualifying scope is the |
1424 | current instantiation. */ |
1425 | if (TREE_CODE (t1) == TYPENAME_TYPE) |
1426 | t1 = resolve_typename_type (t1, /*only_current_p=*/true); |
1427 | |
1428 | if (TREE_CODE (t2) == TYPENAME_TYPE) |
1429 | t2 = resolve_typename_type (t2, /*only_current_p=*/true); |
1430 | } |
1431 | |
1432 | if (TYPE_PTRMEMFUNC_P (t1)) |
1433 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); |
1434 | if (TYPE_PTRMEMFUNC_P (t2)) |
1435 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); |
1436 | |
1437 | /* Different classes of types can't be compatible. */ |
1438 | 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 | if (TREE_CODE (t1) != ARRAY_TYPE |
1444 | && cp_type_quals (t1) != cp_type_quals (t2)) |
1445 | return false; |
1446 | if (TREE_CODE (t1) == FUNCTION_TYPE |
1447 | && 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 | if (FUNC_OR_METHOD_TYPE_P (t1)) |
1452 | { |
1453 | if (type_memfn_rqual (t1) != type_memfn_rqual (t2)) |
1454 | return false; |
1455 | if (flag_noexcept_type |
1456 | && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1), |
1457 | TYPE_RAISES_EXCEPTIONS (t2), |
1458 | exact: 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 | if (TREE_CODE (t1) != ARRAY_TYPE |
1466 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) |
1467 | 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 | 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 | case OPAQUE_TYPE: |
1480 | case INTEGER_TYPE: |
1481 | case FIXED_POINT_TYPE: |
1482 | 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 | if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2)) |
1498 | return false; |
1499 | |
1500 | /* We don't need or want the attribute comparison. */ |
1501 | goto check_alias; |
1502 | |
1503 | case TEMPLATE_TEMPLATE_PARM: |
1504 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
1505 | if (!comp_template_parms_position (t1, t2)) |
1506 | return false; |
1507 | if (!comp_template_parms |
1508 | (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)), |
1509 | DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2)))) |
1510 | return false; |
1511 | if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM) |
1512 | break; |
1513 | /* Don't check inheritance. */ |
1514 | strict = COMPARE_STRICT; |
1515 | /* Fall through. */ |
1516 | |
1517 | case RECORD_TYPE: |
1518 | case UNION_TYPE: |
1519 | if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2) |
1520 | && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2) |
1521 | || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM) |
1522 | && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2))) |
1523 | break; |
1524 | |
1525 | if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2)) |
1526 | break; |
1527 | else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1)) |
1528 | break; |
1529 | |
1530 | return false; |
1531 | |
1532 | case OFFSET_TYPE: |
1533 | if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2), |
1534 | strict & ~COMPARE_REDECLARATION)) |
1535 | return false; |
1536 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1537 | return false; |
1538 | break; |
1539 | |
1540 | case REFERENCE_TYPE: |
1541 | if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2)) |
1542 | return false; |
1543 | /* fall through to checks for pointer types */ |
1544 | gcc_fallthrough (); |
1545 | |
1546 | case POINTER_TYPE: |
1547 | if (TYPE_MODE (t1) != TYPE_MODE (t2) |
1548 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1549 | return false; |
1550 | break; |
1551 | |
1552 | case METHOD_TYPE: |
1553 | case FUNCTION_TYPE: |
1554 | /* Exception specs and memfn_rquals were checked above. */ |
1555 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1556 | return false; |
1557 | if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))) |
1558 | return false; |
1559 | break; |
1560 | |
1561 | case ARRAY_TYPE: |
1562 | /* Target types must match incl. qualifiers. */ |
1563 | if (!comp_array_types (t1, t2, cb: ((strict & COMPARE_REDECLARATION) |
1564 | ? bounds_either : bounds_none), |
1565 | /*strict=*/true)) |
1566 | return false; |
1567 | break; |
1568 | |
1569 | 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 | 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 | if (CLASS_PLACEHOLDER_TEMPLATE (t1) |
1577 | != CLASS_PLACEHOLDER_TEMPLATE (t2)) |
1578 | return false; |
1579 | /* Constrained 'auto's are distinct from parms that don't have the same |
1580 | constraints. */ |
1581 | if (!equivalent_placeholder_constraints (t1, t2)) |
1582 | return false; |
1583 | break; |
1584 | |
1585 | case TYPENAME_TYPE: |
1586 | if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1), |
1587 | TYPENAME_TYPE_FULLNAME (t2))) |
1588 | return false; |
1589 | /* Qualifiers don't matter on scopes. */ |
1590 | if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1), |
1591 | TYPE_CONTEXT (t2))) |
1592 | return false; |
1593 | break; |
1594 | |
1595 | case UNBOUND_CLASS_TEMPLATE: |
1596 | if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2))) |
1597 | return false; |
1598 | if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))) |
1599 | return false; |
1600 | break; |
1601 | |
1602 | case COMPLEX_TYPE: |
1603 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1604 | return false; |
1605 | break; |
1606 | |
1607 | case VECTOR_TYPE: |
1608 | if (gnu_vector_type_p (type: t1) != gnu_vector_type_p (type: t2) |
1609 | || maybe_ne (a: TYPE_VECTOR_SUBPARTS (node: t1), b: TYPE_VECTOR_SUBPARTS (node: t2)) |
1610 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1611 | return false; |
1612 | break; |
1613 | |
1614 | case TYPE_PACK_EXPANSION: |
1615 | return (same_type_p (PACK_EXPANSION_PATTERN (t1), |
1616 | PACK_EXPANSION_PATTERN (t2)) |
1617 | && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1), |
1618 | PACK_EXPANSION_EXTRA_ARGS (t2))); |
1619 | |
1620 | case DECLTYPE_TYPE: |
1621 | if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1) |
1622 | != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)) |
1623 | return false; |
1624 | if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2)) |
1625 | return false; |
1626 | if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2)) |
1627 | return false; |
1628 | if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2))) |
1629 | return false; |
1630 | break; |
1631 | |
1632 | case TRAIT_TYPE: |
1633 | if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2)) |
1634 | return false; |
1635 | if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2)) |
1636 | || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2))) |
1637 | return false; |
1638 | break; |
1639 | |
1640 | case TYPEOF_TYPE: |
1641 | if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2))) |
1642 | return false; |
1643 | break; |
1644 | |
1645 | default: |
1646 | return false; |
1647 | } |
1648 | |
1649 | /* If we get here, we know that from a target independent POV the |
1650 | types are the same. Make sure the target attributes are also |
1651 | the same. */ |
1652 | if (!comp_type_attributes (t1, t2)) |
1653 | return false; |
1654 | |
1655 | check_alias: |
1656 | if (comparing_dependent_aliases) |
1657 | { |
1658 | /* Don't treat an alias template specialization with dependent |
1659 | arguments as equivalent to its underlying type when used as a |
1660 | template argument; we need them to be distinct so that we |
1661 | substitute into the specialization arguments at instantiation |
1662 | time. And aliases can't be equivalent without being ==, so |
1663 | we don't need to look any deeper. */ |
1664 | ++processing_template_decl; |
1665 | tree dep1 = dependent_alias_template_spec_p (t1, nt_transparent); |
1666 | tree dep2 = dependent_alias_template_spec_p (t2, nt_transparent); |
1667 | --processing_template_decl; |
1668 | if ((dep1 || dep2) && dep1 != dep2) |
1669 | return false; |
1670 | } |
1671 | |
1672 | return true; |
1673 | } |
1674 | |
1675 | /* Return true if T1 and T2 are related as allowed by STRICT. STRICT |
1676 | is a bitwise-or of the COMPARE_* flags. */ |
1677 | |
1678 | bool |
1679 | comptypes (tree t1, tree t2, int strict) |
1680 | { |
1681 | gcc_checking_assert (t1 && t2); |
1682 | |
1683 | /* TYPE_ARGUMENT_PACKS are not really types. */ |
1684 | gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK |
1685 | && TREE_CODE (t2) != TYPE_ARGUMENT_PACK); |
1686 | |
1687 | if (t1 == t2) |
1688 | return true; |
1689 | |
1690 | /* Suppress errors caused by previously reported errors. */ |
1691 | if (t1 == error_mark_node || t2 == error_mark_node) |
1692 | return false; |
1693 | |
1694 | if (strict == COMPARE_STRICT) |
1695 | { |
1696 | if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2)) |
1697 | /* At least one of the types requires structural equality, so |
1698 | perform a deep check. */ |
1699 | return structural_comptypes (t1, t2, strict); |
1700 | |
1701 | if (flag_checking && param_use_canonical_types) |
1702 | { |
1703 | bool result = structural_comptypes (t1, t2, strict); |
1704 | |
1705 | if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2)) |
1706 | /* The two types are structurally equivalent, but their |
1707 | canonical types were different. This is a failure of the |
1708 | canonical type propagation code.*/ |
1709 | internal_error |
1710 | ("canonical types differ for identical types %qT and %qT" , |
1711 | t1, t2); |
1712 | else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)) |
1713 | /* Two types are structurally different, but the canonical |
1714 | types are the same. This means we were over-eager in |
1715 | assigning canonical types. */ |
1716 | internal_error |
1717 | ("same canonical type node for different types %qT and %qT" , |
1718 | t1, t2); |
1719 | |
1720 | return result; |
1721 | } |
1722 | if (!flag_checking && param_use_canonical_types) |
1723 | return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); |
1724 | else |
1725 | return structural_comptypes (t1, t2, strict); |
1726 | } |
1727 | else if (strict == COMPARE_STRUCTURAL) |
1728 | return structural_comptypes (t1, t2, COMPARE_STRICT); |
1729 | else |
1730 | return structural_comptypes (t1, t2, strict); |
1731 | } |
1732 | |
1733 | /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring |
1734 | top-level qualifiers. */ |
1735 | |
1736 | bool |
1737 | same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2) |
1738 | { |
1739 | if (type1 == error_mark_node || type2 == error_mark_node) |
1740 | return false; |
1741 | if (type1 == type2) |
1742 | return true; |
1743 | |
1744 | type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED); |
1745 | type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED); |
1746 | return same_type_p (type1, type2); |
1747 | } |
1748 | |
1749 | /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */ |
1750 | |
1751 | bool |
1752 | similar_type_p (tree type1, tree type2) |
1753 | { |
1754 | if (type1 == error_mark_node || type2 == error_mark_node) |
1755 | return false; |
1756 | |
1757 | /* Informally, two types are similar if, ignoring top-level cv-qualification: |
1758 | * they are the same type; or |
1759 | * they are both pointers, and the pointed-to types are similar; or |
1760 | * they are both pointers to member of the same class, and the types of |
1761 | the pointed-to members are similar; or |
1762 | * they are both arrays of the same size or both arrays of unknown bound, |
1763 | and the array element types are similar. */ |
1764 | |
1765 | if (same_type_ignoring_top_level_qualifiers_p (type1, type2)) |
1766 | return true; |
1767 | |
1768 | if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) |
1769 | || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)) |
1770 | || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE)) |
1771 | return comp_ptr_ttypes_const (type1, type2, bounds_either); |
1772 | |
1773 | return false; |
1774 | } |
1775 | |
1776 | /* Helper function for layout_compatible_type_p and |
1777 | is_corresponding_member_aggr. Advance to next members (NULL if |
1778 | no further ones) and return true if those members are still part of |
1779 | the common initial sequence. */ |
1780 | |
1781 | bool |
1782 | next_common_initial_sequence (tree &memb1, tree &memb2) |
1783 | { |
1784 | while (memb1) |
1785 | { |
1786 | if (TREE_CODE (memb1) != FIELD_DECL |
1787 | || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1))) |
1788 | { |
1789 | memb1 = DECL_CHAIN (memb1); |
1790 | continue; |
1791 | } |
1792 | if (DECL_FIELD_IS_BASE (memb1)) |
1793 | { |
1794 | memb1 = TYPE_FIELDS (TREE_TYPE (memb1)); |
1795 | continue; |
1796 | } |
1797 | break; |
1798 | } |
1799 | while (memb2) |
1800 | { |
1801 | if (TREE_CODE (memb2) != FIELD_DECL |
1802 | || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2))) |
1803 | { |
1804 | memb2 = DECL_CHAIN (memb2); |
1805 | continue; |
1806 | } |
1807 | if (DECL_FIELD_IS_BASE (memb2)) |
1808 | { |
1809 | memb2 = TYPE_FIELDS (TREE_TYPE (memb2)); |
1810 | continue; |
1811 | } |
1812 | break; |
1813 | } |
1814 | if (memb1 == NULL_TREE && memb2 == NULL_TREE) |
1815 | return true; |
1816 | if (memb1 == NULL_TREE || memb2 == NULL_TREE) |
1817 | return false; |
1818 | if (DECL_BIT_FIELD_TYPE (memb1)) |
1819 | { |
1820 | if (!DECL_BIT_FIELD_TYPE (memb2)) |
1821 | return false; |
1822 | if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1), |
1823 | DECL_BIT_FIELD_TYPE (memb2))) |
1824 | return false; |
1825 | if (TYPE_PRECISION (TREE_TYPE (memb1)) |
1826 | != TYPE_PRECISION (TREE_TYPE (memb2))) |
1827 | return false; |
1828 | } |
1829 | else if (DECL_BIT_FIELD_TYPE (memb2)) |
1830 | return false; |
1831 | else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2))) |
1832 | return false; |
1833 | if ((!lookup_attribute (attr_name: "no_unique_address" , DECL_ATTRIBUTES (memb1))) |
1834 | != !lookup_attribute (attr_name: "no_unique_address" , DECL_ATTRIBUTES (memb2))) |
1835 | return false; |
1836 | if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2)) |
1837 | return false; |
1838 | if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2))) |
1839 | return false; |
1840 | return true; |
1841 | } |
1842 | |
1843 | /* Return true if TYPE1 and TYPE2 are layout-compatible types. */ |
1844 | |
1845 | bool |
1846 | layout_compatible_type_p (tree type1, tree type2) |
1847 | { |
1848 | if (type1 == error_mark_node || type2 == error_mark_node) |
1849 | return false; |
1850 | if (type1 == type2) |
1851 | return true; |
1852 | if (TREE_CODE (type1) != TREE_CODE (type2)) |
1853 | return false; |
1854 | |
1855 | type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED); |
1856 | type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED); |
1857 | |
1858 | if (TREE_CODE (type1) == ENUMERAL_TYPE) |
1859 | return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)) |
1860 | && same_type_p (finish_underlying_type (type1), |
1861 | finish_underlying_type (type2))); |
1862 | |
1863 | if (CLASS_TYPE_P (type1) |
1864 | && std_layout_type_p (type1) |
1865 | && std_layout_type_p (type2) |
1866 | && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))) |
1867 | { |
1868 | tree field1 = TYPE_FIELDS (type1); |
1869 | tree field2 = TYPE_FIELDS (type2); |
1870 | if (TREE_CODE (type1) == RECORD_TYPE) |
1871 | { |
1872 | while (1) |
1873 | { |
1874 | if (!next_common_initial_sequence (memb1&: field1, memb2&: field2)) |
1875 | return false; |
1876 | if (field1 == NULL_TREE) |
1877 | return true; |
1878 | field1 = DECL_CHAIN (field1); |
1879 | field2 = DECL_CHAIN (field2); |
1880 | } |
1881 | } |
1882 | /* Otherwise both types must be union types. |
1883 | The standard says: |
1884 | "Two standard-layout unions are layout-compatible if they have |
1885 | the same number of non-static data members and corresponding |
1886 | non-static data members (in any order) have layout-compatible |
1887 | types." |
1888 | but the code anticipates that bitfield vs. non-bitfield, |
1889 | different bitfield widths or presence/absence of |
1890 | [[no_unique_address]] should be checked as well. */ |
1891 | auto_vec<tree, 16> vec; |
1892 | unsigned int count = 0; |
1893 | for (; field1; field1 = DECL_CHAIN (field1)) |
1894 | if (TREE_CODE (field1) == FIELD_DECL) |
1895 | count++; |
1896 | for (; field2; field2 = DECL_CHAIN (field2)) |
1897 | if (TREE_CODE (field2) == FIELD_DECL) |
1898 | vec.safe_push (obj: field2); |
1899 | /* Discussions on core lean towards treating multiple union fields |
1900 | of the same type as the same field, so this might need changing |
1901 | in the future. */ |
1902 | if (count != vec.length ()) |
1903 | return false; |
1904 | for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1)) |
1905 | { |
1906 | if (TREE_CODE (field1) != FIELD_DECL) |
1907 | continue; |
1908 | unsigned int j; |
1909 | tree t1 = DECL_BIT_FIELD_TYPE (field1); |
1910 | if (t1 == NULL_TREE) |
1911 | t1 = TREE_TYPE (field1); |
1912 | FOR_EACH_VEC_ELT (vec, j, field2) |
1913 | { |
1914 | tree t2 = DECL_BIT_FIELD_TYPE (field2); |
1915 | if (t2 == NULL_TREE) |
1916 | t2 = TREE_TYPE (field2); |
1917 | if (DECL_BIT_FIELD_TYPE (field1)) |
1918 | { |
1919 | if (!DECL_BIT_FIELD_TYPE (field2)) |
1920 | continue; |
1921 | if (TYPE_PRECISION (TREE_TYPE (field1)) |
1922 | != TYPE_PRECISION (TREE_TYPE (field2))) |
1923 | continue; |
1924 | } |
1925 | else if (DECL_BIT_FIELD_TYPE (field2)) |
1926 | continue; |
1927 | if (!layout_compatible_type_p (type1: t1, type2: t2)) |
1928 | continue; |
1929 | if ((!lookup_attribute (attr_name: "no_unique_address" , |
1930 | DECL_ATTRIBUTES (field1))) |
1931 | != !lookup_attribute (attr_name: "no_unique_address" , |
1932 | DECL_ATTRIBUTES (field2))) |
1933 | continue; |
1934 | break; |
1935 | } |
1936 | if (j == vec.length ()) |
1937 | return false; |
1938 | vec.unordered_remove (ix: j); |
1939 | } |
1940 | return true; |
1941 | } |
1942 | |
1943 | return same_type_p (type1, type2); |
1944 | } |
1945 | |
1946 | /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */ |
1947 | |
1948 | bool |
1949 | at_least_as_qualified_p (const_tree type1, const_tree type2) |
1950 | { |
1951 | int q1 = cp_type_quals (type1); |
1952 | int q2 = cp_type_quals (type2); |
1953 | |
1954 | /* All qualifiers for TYPE2 must also appear in TYPE1. */ |
1955 | return (q1 & q2) == q2; |
1956 | } |
1957 | |
1958 | /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is |
1959 | more cv-qualified that TYPE1, and 0 otherwise. */ |
1960 | |
1961 | int |
1962 | comp_cv_qualification (int q1, int q2) |
1963 | { |
1964 | if (q1 == q2) |
1965 | return 0; |
1966 | |
1967 | if ((q1 & q2) == q2) |
1968 | return 1; |
1969 | else if ((q1 & q2) == q1) |
1970 | return -1; |
1971 | |
1972 | return 0; |
1973 | } |
1974 | |
1975 | int |
1976 | comp_cv_qualification (const_tree type1, const_tree type2) |
1977 | { |
1978 | int q1 = cp_type_quals (type1); |
1979 | int q2 = cp_type_quals (type2); |
1980 | return comp_cv_qualification (q1, q2); |
1981 | } |
1982 | |
1983 | /* Returns 1 if the cv-qualification signature of TYPE1 is a proper |
1984 | subset of the cv-qualification signature of TYPE2, and the types |
1985 | are similar. Returns -1 if the other way 'round, and 0 otherwise. */ |
1986 | |
1987 | int |
1988 | comp_cv_qual_signature (tree type1, tree type2) |
1989 | { |
1990 | if (comp_ptr_ttypes_real (type2, type1, -1)) |
1991 | return 1; |
1992 | else if (comp_ptr_ttypes_real (type1, type2, -1)) |
1993 | return -1; |
1994 | else |
1995 | return 0; |
1996 | } |
1997 | |
1998 | /* Subroutines of `comptypes'. */ |
1999 | |
2000 | /* Return true if two parameter type lists PARMS1 and PARMS2 are |
2001 | equivalent in the sense that functions with those parameter types |
2002 | can have equivalent types. The two lists must be equivalent, |
2003 | element by element. */ |
2004 | |
2005 | bool |
2006 | compparms (const_tree parms1, const_tree parms2) |
2007 | { |
2008 | const_tree t1, t2; |
2009 | |
2010 | /* An unspecified parmlist matches any specified parmlist |
2011 | whose argument types don't need default promotions. */ |
2012 | |
2013 | for (t1 = parms1, t2 = parms2; |
2014 | t1 || t2; |
2015 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) |
2016 | { |
2017 | /* If one parmlist is shorter than the other, |
2018 | they fail to match. */ |
2019 | if (!t1 || !t2) |
2020 | return false; |
2021 | if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) |
2022 | return false; |
2023 | } |
2024 | return true; |
2025 | } |
2026 | |
2027 | |
2028 | /* Process a sizeof or alignof expression where the operand is a type. |
2029 | STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment) |
2030 | or GNU (preferred alignment) semantics; it is ignored if OP is |
2031 | SIZEOF_EXPR. */ |
2032 | |
2033 | tree |
2034 | cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op, |
2035 | bool std_alignof, bool complain) |
2036 | { |
2037 | gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR); |
2038 | if (type == error_mark_node) |
2039 | return error_mark_node; |
2040 | |
2041 | type = non_reference (type); |
2042 | if (TREE_CODE (type) == METHOD_TYPE) |
2043 | { |
2044 | if (complain) |
2045 | { |
2046 | pedwarn (loc, OPT_Wpointer_arith, |
2047 | "invalid application of %qs to a member function" , |
2048 | OVL_OP_INFO (false, op)->name); |
2049 | return size_one_node; |
2050 | } |
2051 | else |
2052 | return error_mark_node; |
2053 | } |
2054 | else if (VOID_TYPE_P (type) && std_alignof) |
2055 | { |
2056 | if (complain) |
2057 | error_at (loc, "invalid application of %qs to a void type" , |
2058 | OVL_OP_INFO (false, op)->name); |
2059 | return error_mark_node; |
2060 | } |
2061 | |
2062 | bool dependent_p = dependent_type_p (type); |
2063 | if (!dependent_p) |
2064 | complete_type (type); |
2065 | if (dependent_p |
2066 | /* VLA types will have a non-constant size. In the body of an |
2067 | uninstantiated template, we don't need to try to compute the |
2068 | value, because the sizeof expression is not an integral |
2069 | constant expression in that case. And, if we do try to |
2070 | compute the value, we'll likely end up with SAVE_EXPRs, which |
2071 | the template substitution machinery does not expect to see. */ |
2072 | || (processing_template_decl |
2073 | && COMPLETE_TYPE_P (type) |
2074 | && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)) |
2075 | { |
2076 | tree value = build_min (op, size_type_node, type); |
2077 | TREE_READONLY (value) = 1; |
2078 | if (op == ALIGNOF_EXPR && std_alignof) |
2079 | ALIGNOF_EXPR_STD_P (value) = true; |
2080 | SET_EXPR_LOCATION (value, loc); |
2081 | return value; |
2082 | } |
2083 | |
2084 | return c_sizeof_or_alignof_type (loc, complete_type (type), |
2085 | op == SIZEOF_EXPR, std_alignof, |
2086 | complain); |
2087 | } |
2088 | |
2089 | /* Return the size of the type, without producing any warnings for |
2090 | types whose size cannot be taken. This routine should be used only |
2091 | in some other routine that has already produced a diagnostic about |
2092 | using the size of such a type. */ |
2093 | tree |
2094 | cxx_sizeof_nowarn (tree type) |
2095 | { |
2096 | if (TREE_CODE (type) == FUNCTION_TYPE |
2097 | || VOID_TYPE_P (type) |
2098 | || TREE_CODE (type) == ERROR_MARK) |
2099 | return size_one_node; |
2100 | else if (!COMPLETE_TYPE_P (type)) |
2101 | return size_zero_node; |
2102 | else |
2103 | return cxx_sizeof_or_alignof_type (loc: input_location, type, |
2104 | op: SIZEOF_EXPR, std_alignof: false, complain: false); |
2105 | } |
2106 | |
2107 | /* Process a sizeof expression where the operand is an expression. */ |
2108 | |
2109 | static tree |
2110 | cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain) |
2111 | { |
2112 | if (e == error_mark_node) |
2113 | return error_mark_node; |
2114 | |
2115 | if (instantiation_dependent_uneval_expression_p (e)) |
2116 | { |
2117 | e = build_min (SIZEOF_EXPR, size_type_node, e); |
2118 | TREE_SIDE_EFFECTS (e) = 0; |
2119 | TREE_READONLY (e) = 1; |
2120 | SET_EXPR_LOCATION (e, loc); |
2121 | |
2122 | return e; |
2123 | } |
2124 | |
2125 | location_t e_loc = cp_expr_loc_or_loc (t: e, or_loc: loc); |
2126 | STRIP_ANY_LOCATION_WRAPPER (e); |
2127 | |
2128 | /* To get the size of a static data member declared as an array of |
2129 | unknown bound, we need to instantiate it. */ |
2130 | if (VAR_P (e) |
2131 | && VAR_HAD_UNKNOWN_BOUND (e) |
2132 | && DECL_TEMPLATE_INSTANTIATION (e)) |
2133 | instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false); |
2134 | |
2135 | if (TREE_CODE (e) == PARM_DECL |
2136 | && DECL_ARRAY_PARAMETER_P (e) |
2137 | && (complain & tf_warning)) |
2138 | { |
2139 | auto_diagnostic_group d; |
2140 | if (warning_at (e_loc, OPT_Wsizeof_array_argument, |
2141 | "%<sizeof%> on array function parameter %qE " |
2142 | "will return size of %qT" , e, TREE_TYPE (e))) |
2143 | inform (DECL_SOURCE_LOCATION (e), "declared here" ); |
2144 | } |
2145 | |
2146 | e = mark_type_use (e); |
2147 | |
2148 | if (bitfield_p (e)) |
2149 | { |
2150 | if (complain & tf_error) |
2151 | error_at (e_loc, |
2152 | "invalid application of %<sizeof%> to a bit-field" ); |
2153 | else |
2154 | return error_mark_node; |
2155 | e = char_type_node; |
2156 | } |
2157 | else if (is_overloaded_fn (e)) |
2158 | { |
2159 | if (complain & tf_error) |
2160 | permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to " |
2161 | "an expression of function type" ); |
2162 | else |
2163 | return error_mark_node; |
2164 | e = char_type_node; |
2165 | } |
2166 | else if (type_unknown_p (expr: e)) |
2167 | { |
2168 | if (complain & tf_error) |
2169 | cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e)); |
2170 | else |
2171 | return error_mark_node; |
2172 | e = char_type_node; |
2173 | } |
2174 | else |
2175 | e = TREE_TYPE (e); |
2176 | |
2177 | return cxx_sizeof_or_alignof_type (loc, type: e, op: SIZEOF_EXPR, std_alignof: false, |
2178 | complain: complain & tf_error); |
2179 | } |
2180 | |
2181 | /* Implement the __alignof keyword: Return the minimum required |
2182 | alignment of E, measured in bytes. For VAR_DECL's and |
2183 | FIELD_DECL's return DECL_ALIGN (which can be set from an |
2184 | "aligned" __attribute__ specification). STD_ALIGNOF acts |
2185 | like in cxx_sizeof_or_alignof_type. */ |
2186 | |
2187 | static tree |
2188 | cxx_alignof_expr (location_t loc, tree e, bool std_alignof, |
2189 | tsubst_flags_t complain) |
2190 | { |
2191 | tree t; |
2192 | |
2193 | if (e == error_mark_node) |
2194 | return error_mark_node; |
2195 | |
2196 | if (processing_template_decl) |
2197 | { |
2198 | e = build_min (ALIGNOF_EXPR, size_type_node, e); |
2199 | TREE_SIDE_EFFECTS (e) = 0; |
2200 | TREE_READONLY (e) = 1; |
2201 | SET_EXPR_LOCATION (e, loc); |
2202 | ALIGNOF_EXPR_STD_P (e) = std_alignof; |
2203 | |
2204 | return e; |
2205 | } |
2206 | |
2207 | location_t e_loc = cp_expr_loc_or_loc (t: e, or_loc: loc); |
2208 | STRIP_ANY_LOCATION_WRAPPER (e); |
2209 | |
2210 | e = mark_type_use (e); |
2211 | |
2212 | if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e), |
2213 | !(complain & tf_error))) |
2214 | { |
2215 | if (!(complain & tf_error)) |
2216 | return error_mark_node; |
2217 | t = size_one_node; |
2218 | } |
2219 | else if (VAR_P (e)) |
2220 | t = size_int (DECL_ALIGN_UNIT (e)); |
2221 | else if (bitfield_p (e)) |
2222 | { |
2223 | if (complain & tf_error) |
2224 | error_at (e_loc, |
2225 | "invalid application of %<__alignof%> to a bit-field" ); |
2226 | else |
2227 | return error_mark_node; |
2228 | t = size_one_node; |
2229 | } |
2230 | else if (TREE_CODE (e) == COMPONENT_REF |
2231 | && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL) |
2232 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1))); |
2233 | else if (is_overloaded_fn (e)) |
2234 | { |
2235 | if (complain & tf_error) |
2236 | permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to " |
2237 | "an expression of function type" ); |
2238 | else |
2239 | return error_mark_node; |
2240 | if (TREE_CODE (e) == FUNCTION_DECL) |
2241 | t = size_int (DECL_ALIGN_UNIT (e)); |
2242 | else |
2243 | t = size_one_node; |
2244 | } |
2245 | else if (type_unknown_p (expr: e)) |
2246 | { |
2247 | if (complain & tf_error) |
2248 | cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e)); |
2249 | else |
2250 | return error_mark_node; |
2251 | t = size_one_node; |
2252 | } |
2253 | else |
2254 | return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e), |
2255 | op: ALIGNOF_EXPR, std_alignof, |
2256 | complain: complain & tf_error); |
2257 | |
2258 | return fold_convert_loc (loc, size_type_node, t); |
2259 | } |
2260 | |
2261 | /* Process a sizeof or alignof expression E with code OP where the operand |
2262 | is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */ |
2263 | |
2264 | tree |
2265 | cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op, |
2266 | bool std_alignof, bool complain) |
2267 | { |
2268 | gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR); |
2269 | if (op == SIZEOF_EXPR) |
2270 | return cxx_sizeof_expr (loc, e, complain: complain? tf_warning_or_error : tf_none); |
2271 | else |
2272 | return cxx_alignof_expr (loc, e, std_alignof, |
2273 | complain: complain? tf_warning_or_error : tf_none); |
2274 | } |
2275 | |
2276 | /* Build a representation of an expression 'alignas(E).' Return the |
2277 | folded integer value of E if it is an integral constant expression |
2278 | that resolves to a valid alignment. If E depends on a template |
2279 | parameter, return a syntactic representation tree of kind |
2280 | ALIGNOF_EXPR. Otherwise, return an error_mark_node if the |
2281 | expression is ill formed, or NULL_TREE if E is NULL_TREE. */ |
2282 | |
2283 | tree |
2284 | cxx_alignas_expr (tree e) |
2285 | { |
2286 | if (e == NULL_TREE || e == error_mark_node |
2287 | || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e))) |
2288 | return e; |
2289 | |
2290 | if (TYPE_P (e)) |
2291 | /* [dcl.align]/3: |
2292 | |
2293 | When the alignment-specifier is of the form |
2294 | alignas(type-id), it shall have the same effect as |
2295 | alignas(alignof(type-id)). */ |
2296 | |
2297 | return cxx_sizeof_or_alignof_type (loc: input_location, |
2298 | type: e, op: ALIGNOF_EXPR, |
2299 | /*std_alignof=*/true, |
2300 | /*complain=*/true); |
2301 | |
2302 | /* If we reach this point, it means the alignas expression if of |
2303 | the form "alignas(assignment-expression)", so we should follow |
2304 | what is stated by [dcl.align]/2. */ |
2305 | |
2306 | if (value_dependent_expression_p (e)) |
2307 | /* Leave value-dependent expression alone for now. */ |
2308 | return e; |
2309 | |
2310 | e = instantiate_non_dependent_expr (e); |
2311 | e = mark_rvalue_use (e); |
2312 | |
2313 | /* [dcl.align]/2 says: |
2314 | |
2315 | the assignment-expression shall be an integral constant |
2316 | expression. */ |
2317 | |
2318 | if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e))) |
2319 | { |
2320 | error ("%<alignas%> argument has non-integral type %qT" , TREE_TYPE (e)); |
2321 | return error_mark_node; |
2322 | } |
2323 | |
2324 | return cxx_constant_value (e); |
2325 | } |
2326 | |
2327 | |
2328 | /* EXPR is being used in a context that is not a function call. |
2329 | Enforce: |
2330 | |
2331 | [expr.ref] |
2332 | |
2333 | The expression can be used only as the left-hand operand of a |
2334 | member function call. |
2335 | |
2336 | [expr.mptr.operator] |
2337 | |
2338 | If the result of .* or ->* is a function, then that result can be |
2339 | used only as the operand for the function call operator (). |
2340 | |
2341 | by issuing an error message if appropriate. Returns true iff EXPR |
2342 | violates these rules. */ |
2343 | |
2344 | bool |
2345 | invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain) |
2346 | { |
2347 | if (expr == NULL_TREE) |
2348 | return false; |
2349 | /* Don't enforce this in MS mode. */ |
2350 | if (flag_ms_extensions) |
2351 | return false; |
2352 | if (is_overloaded_fn (expr) && !really_overloaded_fn (expr)) |
2353 | expr = get_first_fn (expr); |
2354 | if (TREE_TYPE (expr) |
2355 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr)) |
2356 | { |
2357 | if (complain & tf_error) |
2358 | { |
2359 | if (DECL_P (expr)) |
2360 | { |
2361 | error_at (loc, "invalid use of non-static member function %qD" , |
2362 | expr); |
2363 | inform (DECL_SOURCE_LOCATION (expr), "declared here" ); |
2364 | } |
2365 | else |
2366 | error_at (loc, "invalid use of non-static member function of " |
2367 | "type %qT" , TREE_TYPE (expr)); |
2368 | } |
2369 | return true; |
2370 | } |
2371 | return false; |
2372 | } |
2373 | |
2374 | /* If EXP is a reference to a bit-field, and the type of EXP does not |
2375 | match the declared type of the bit-field, return the declared type |
2376 | of the bit-field. Otherwise, return NULL_TREE. */ |
2377 | |
2378 | tree |
2379 | is_bitfield_expr_with_lowered_type (const_tree exp) |
2380 | { |
2381 | switch (TREE_CODE (exp)) |
2382 | { |
2383 | case COND_EXPR: |
2384 | if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1) |
2385 | ? TREE_OPERAND (exp, 1) |
2386 | : TREE_OPERAND (exp, 0))) |
2387 | return NULL_TREE; |
2388 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2)); |
2389 | |
2390 | case COMPOUND_EXPR: |
2391 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)); |
2392 | |
2393 | case MODIFY_EXPR: |
2394 | case SAVE_EXPR: |
2395 | case UNARY_PLUS_EXPR: |
2396 | case PREDECREMENT_EXPR: |
2397 | case PREINCREMENT_EXPR: |
2398 | case POSTDECREMENT_EXPR: |
2399 | case POSTINCREMENT_EXPR: |
2400 | case NEGATE_EXPR: |
2401 | case NON_LVALUE_EXPR: |
2402 | case BIT_NOT_EXPR: |
2403 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); |
2404 | |
2405 | case COMPONENT_REF: |
2406 | { |
2407 | tree field; |
2408 | |
2409 | field = TREE_OPERAND (exp, 1); |
2410 | if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field)) |
2411 | return NULL_TREE; |
2412 | if (same_type_ignoring_top_level_qualifiers_p |
2413 | (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field))) |
2414 | return NULL_TREE; |
2415 | return DECL_BIT_FIELD_TYPE (field); |
2416 | } |
2417 | |
2418 | case VAR_DECL: |
2419 | if (DECL_HAS_VALUE_EXPR_P (exp)) |
2420 | return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR |
2421 | (CONST_CAST_TREE (exp))); |
2422 | return NULL_TREE; |
2423 | |
2424 | case VIEW_CONVERT_EXPR: |
2425 | if (location_wrapper_p (exp)) |
2426 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); |
2427 | else |
2428 | return NULL_TREE; |
2429 | |
2430 | default: |
2431 | return NULL_TREE; |
2432 | } |
2433 | } |
2434 | |
2435 | /* Like is_bitfield_with_lowered_type, except that if EXP is not a |
2436 | bitfield with a lowered type, the type of EXP is returned, rather |
2437 | than NULL_TREE. */ |
2438 | |
2439 | tree |
2440 | unlowered_expr_type (const_tree exp) |
2441 | { |
2442 | tree type; |
2443 | tree etype = TREE_TYPE (exp); |
2444 | |
2445 | type = is_bitfield_expr_with_lowered_type (exp); |
2446 | if (type) |
2447 | type = cp_build_qualified_type (type, cp_type_quals (etype)); |
2448 | else |
2449 | type = etype; |
2450 | |
2451 | return type; |
2452 | } |
2453 | |
2454 | /* Perform the conversions in [expr] that apply when an lvalue appears |
2455 | in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and |
2456 | function-to-pointer conversions. In addition, bitfield references are |
2457 | converted to their declared types. Note that this function does not perform |
2458 | the lvalue-to-rvalue conversion for class types. If you need that conversion |
2459 | for class types, then you probably need to use force_rvalue. |
2460 | |
2461 | Although the returned value is being used as an rvalue, this |
2462 | function does not wrap the returned expression in a |
2463 | NON_LVALUE_EXPR; the caller is expected to be mindful of the fact |
2464 | that the return value is no longer an lvalue. */ |
2465 | |
2466 | tree |
2467 | decay_conversion (tree exp, |
2468 | tsubst_flags_t complain, |
2469 | bool reject_builtin /* = true */) |
2470 | { |
2471 | tree type; |
2472 | enum tree_code code; |
2473 | location_t loc = cp_expr_loc_or_input_loc (t: exp); |
2474 | |
2475 | type = TREE_TYPE (exp); |
2476 | if (type == error_mark_node) |
2477 | return error_mark_node; |
2478 | |
2479 | exp = resolve_nondeduced_context_or_error (exp, complain); |
2480 | |
2481 | code = TREE_CODE (type); |
2482 | |
2483 | if (error_operand_p (t: exp)) |
2484 | return error_mark_node; |
2485 | |
2486 | if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp)) |
2487 | { |
2488 | mark_rvalue_use (exp, loc, reject_builtin); |
2489 | return nullptr_node; |
2490 | } |
2491 | |
2492 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
2493 | Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */ |
2494 | if (code == VOID_TYPE) |
2495 | { |
2496 | if (complain & tf_error) |
2497 | error_at (loc, "void value not ignored as it ought to be" ); |
2498 | return error_mark_node; |
2499 | } |
2500 | if (invalid_nonstatic_memfn_p (loc, expr: exp, complain)) |
2501 | return error_mark_node; |
2502 | if (code == FUNCTION_TYPE || is_overloaded_fn (exp)) |
2503 | { |
2504 | exp = mark_lvalue_use (exp); |
2505 | if (reject_builtin && reject_gcc_builtin (exp, loc)) |
2506 | return error_mark_node; |
2507 | return cp_build_addr_expr (exp, complain); |
2508 | } |
2509 | if (code == ARRAY_TYPE) |
2510 | { |
2511 | tree adr; |
2512 | tree ptrtype; |
2513 | |
2514 | exp = mark_lvalue_use (exp); |
2515 | |
2516 | if (INDIRECT_REF_P (exp)) |
2517 | return build_nop (build_pointer_type (TREE_TYPE (type)), |
2518 | TREE_OPERAND (exp, 0)); |
2519 | |
2520 | if (TREE_CODE (exp) == COMPOUND_EXPR) |
2521 | { |
2522 | tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain); |
2523 | if (op1 == error_mark_node) |
2524 | return error_mark_node; |
2525 | return build2 (COMPOUND_EXPR, TREE_TYPE (op1), |
2526 | TREE_OPERAND (exp, 0), op1); |
2527 | } |
2528 | |
2529 | if (!obvalue_p (exp) |
2530 | && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) |
2531 | { |
2532 | if (complain & tf_error) |
2533 | error_at (loc, "invalid use of non-lvalue array" ); |
2534 | return error_mark_node; |
2535 | } |
2536 | |
2537 | /* Don't let an array compound literal decay to a pointer. It can |
2538 | still be used to initialize an array or bind to a reference. */ |
2539 | if (TREE_CODE (exp) == TARGET_EXPR) |
2540 | { |
2541 | if (complain & tf_error) |
2542 | error_at (loc, "taking address of temporary array" ); |
2543 | return error_mark_node; |
2544 | } |
2545 | |
2546 | ptrtype = build_pointer_type (TREE_TYPE (type)); |
2547 | |
2548 | if (VAR_P (exp)) |
2549 | { |
2550 | if (!cxx_mark_addressable (exp)) |
2551 | return error_mark_node; |
2552 | adr = build_nop (ptrtype, build_address (exp)); |
2553 | return adr; |
2554 | } |
2555 | /* This way is better for a COMPONENT_REF since it can |
2556 | simplify the offset for a component. */ |
2557 | adr = cp_build_addr_expr (exp, complain); |
2558 | return cp_convert (ptrtype, adr, complain); |
2559 | } |
2560 | |
2561 | /* Otherwise, it's the lvalue-to-rvalue conversion. */ |
2562 | exp = mark_rvalue_use (exp, loc, reject_builtin); |
2563 | |
2564 | /* If a bitfield is used in a context where integral promotion |
2565 | applies, then the caller is expected to have used |
2566 | default_conversion. That function promotes bitfields correctly |
2567 | before calling this function. At this point, if we have a |
2568 | bitfield referenced, we may assume that is not subject to |
2569 | promotion, and that, therefore, the type of the resulting rvalue |
2570 | is the declared type of the bitfield. */ |
2571 | exp = convert_bitfield_to_declared_type (exp); |
2572 | |
2573 | /* We do not call rvalue() here because we do not want to wrap EXP |
2574 | in a NON_LVALUE_EXPR. */ |
2575 | |
2576 | /* [basic.lval] |
2577 | |
2578 | Non-class rvalues always have cv-unqualified types. */ |
2579 | type = TREE_TYPE (exp); |
2580 | if (!CLASS_TYPE_P (type) && cv_qualified_p (type)) |
2581 | exp = build_nop (cv_unqualified (type), exp); |
2582 | |
2583 | if (!complete_type_or_maybe_complain (type, value: exp, complain)) |
2584 | return error_mark_node; |
2585 | |
2586 | return exp; |
2587 | } |
2588 | |
2589 | /* Perform preparatory conversions, as part of the "usual arithmetic |
2590 | conversions". In particular, as per [expr]: |
2591 | |
2592 | Whenever an lvalue expression appears as an operand of an |
2593 | operator that expects the rvalue for that operand, the |
2594 | lvalue-to-rvalue, array-to-pointer, or function-to-pointer |
2595 | standard conversions are applied to convert the expression to an |
2596 | rvalue. |
2597 | |
2598 | In addition, we perform integral promotions here, as those are |
2599 | applied to both operands to a binary operator before determining |
2600 | what additional conversions should apply. */ |
2601 | |
2602 | static tree |
2603 | cp_default_conversion (tree exp, tsubst_flags_t complain) |
2604 | { |
2605 | /* Check for target-specific promotions. */ |
2606 | tree promoted_type = targetm.promoted_type (TREE_TYPE (exp)); |
2607 | if (promoted_type) |
2608 | exp = cp_convert (promoted_type, exp, complain); |
2609 | /* Perform the integral promotions first so that bitfield |
2610 | expressions (which may promote to "int", even if the bitfield is |
2611 | declared "unsigned") are promoted correctly. */ |
2612 | else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp))) |
2613 | exp = cp_perform_integral_promotions (exp, complain); |
2614 | /* Perform the other conversions. */ |
2615 | exp = decay_conversion (exp, complain); |
2616 | |
2617 | return exp; |
2618 | } |
2619 | |
2620 | /* C version. */ |
2621 | |
2622 | tree |
2623 | default_conversion (tree exp) |
2624 | { |
2625 | return cp_default_conversion (exp, complain: tf_warning_or_error); |
2626 | } |
2627 | |
2628 | /* EXPR is an expression with an integral or enumeration type. |
2629 | Perform the integral promotions in [conv.prom], and return the |
2630 | converted value. */ |
2631 | |
2632 | tree |
2633 | cp_perform_integral_promotions (tree expr, tsubst_flags_t complain) |
2634 | { |
2635 | tree type; |
2636 | tree promoted_type; |
2637 | |
2638 | expr = mark_rvalue_use (expr); |
2639 | if (error_operand_p (t: expr)) |
2640 | return error_mark_node; |
2641 | |
2642 | type = TREE_TYPE (expr); |
2643 | |
2644 | /* [conv.prom] |
2645 | |
2646 | A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue |
2647 | of type int if int can represent all the values of the bit-field; |
2648 | otherwise, it can be converted to unsigned int if unsigned int can |
2649 | represent all the values of the bit-field. If the bit-field is larger yet, |
2650 | no integral promotion applies to it. If the bit-field has an enumerated |
2651 | type, it is treated as any other value of that type for promotion |
2652 | purposes. */ |
2653 | tree bitfield_type = is_bitfield_expr_with_lowered_type (exp: expr); |
2654 | if (bitfield_type |
2655 | && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE |
2656 | || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node))) |
2657 | type = bitfield_type; |
2658 | |
2659 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); |
2660 | /* Scoped enums don't promote. */ |
2661 | if (SCOPED_ENUM_P (type)) |
2662 | return expr; |
2663 | promoted_type = type_promotes_to (type); |
2664 | if (type != promoted_type) |
2665 | expr = cp_convert (promoted_type, expr, complain); |
2666 | else if (bitfield_type && bitfield_type != type) |
2667 | /* Prevent decay_conversion from converting to bitfield_type. */ |
2668 | expr = build_nop (type, expr); |
2669 | return expr; |
2670 | } |
2671 | |
2672 | /* C version. */ |
2673 | |
2674 | tree |
2675 | perform_integral_promotions (tree expr) |
2676 | { |
2677 | return cp_perform_integral_promotions (expr, complain: tf_warning_or_error); |
2678 | } |
2679 | |
2680 | /* Returns nonzero iff exp is a STRING_CST or the result of applying |
2681 | decay_conversion to one. */ |
2682 | |
2683 | int |
2684 | string_conv_p (const_tree totype, const_tree exp, int warn) |
2685 | { |
2686 | tree t; |
2687 | |
2688 | if (!TYPE_PTR_P (totype)) |
2689 | return 0; |
2690 | |
2691 | t = TREE_TYPE (totype); |
2692 | if (!same_type_p (t, char_type_node) |
2693 | && !same_type_p (t, char8_type_node) |
2694 | && !same_type_p (t, char16_type_node) |
2695 | && !same_type_p (t, char32_type_node) |
2696 | && !same_type_p (t, wchar_type_node)) |
2697 | return 0; |
2698 | |
2699 | location_t loc = EXPR_LOC_OR_LOC (exp, input_location); |
2700 | |
2701 | STRIP_ANY_LOCATION_WRAPPER (exp); |
2702 | |
2703 | if (TREE_CODE (exp) == STRING_CST) |
2704 | { |
2705 | /* Make sure that we don't try to convert between char and wide chars. */ |
2706 | if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t)) |
2707 | return 0; |
2708 | } |
2709 | else |
2710 | { |
2711 | /* Is this a string constant which has decayed to 'const char *'? */ |
2712 | t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST)); |
2713 | if (!same_type_p (TREE_TYPE (exp), t)) |
2714 | return 0; |
2715 | STRIP_NOPS (exp); |
2716 | if (TREE_CODE (exp) != ADDR_EXPR |
2717 | || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST) |
2718 | return 0; |
2719 | } |
2720 | if (warn) |
2721 | { |
2722 | if (cxx_dialect >= cxx11) |
2723 | pedwarn (loc, OPT_Wwrite_strings, |
2724 | "ISO C++ forbids converting a string constant to %qT" , |
2725 | totype); |
2726 | else |
2727 | warning_at (loc, OPT_Wwrite_strings, |
2728 | "deprecated conversion from string constant to %qT" , |
2729 | totype); |
2730 | } |
2731 | |
2732 | return 1; |
2733 | } |
2734 | |
2735 | /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we |
2736 | can, for example, use as an lvalue. This code used to be in |
2737 | unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c' |
2738 | expressions, where we're dealing with aggregates. But now it's again only |
2739 | called from unary_complex_lvalue. The case (in particular) that led to |
2740 | this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd |
2741 | get it there. */ |
2742 | |
2743 | static tree |
2744 | rationalize_conditional_expr (enum tree_code code, tree t, |
2745 | tsubst_flags_t complain) |
2746 | { |
2747 | location_t loc = cp_expr_loc_or_input_loc (t); |
2748 | |
2749 | /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that |
2750 | the first operand is always the one to be used if both operands |
2751 | are equal, so we know what conditional expression this used to be. */ |
2752 | if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR) |
2753 | { |
2754 | tree op0 = TREE_OPERAND (t, 0); |
2755 | tree op1 = TREE_OPERAND (t, 1); |
2756 | |
2757 | /* The following code is incorrect if either operand side-effects. */ |
2758 | gcc_assert (!TREE_SIDE_EFFECTS (op0) |
2759 | && !TREE_SIDE_EFFECTS (op1)); |
2760 | return |
2761 | build_conditional_expr (loc, |
2762 | build_x_binary_op (loc, |
2763 | (TREE_CODE (t) == MIN_EXPR |
2764 | ? LE_EXPR : GE_EXPR), |
2765 | op0, TREE_CODE (op0), |
2766 | op1, TREE_CODE (op1), |
2767 | NULL_TREE, |
2768 | /*overload=*/NULL, |
2769 | complain), |
2770 | cp_build_unary_op (code, op0, false, complain), |
2771 | cp_build_unary_op (code, op1, false, complain), |
2772 | complain); |
2773 | } |
2774 | |
2775 | tree op1 = TREE_OPERAND (t, 1); |
2776 | if (TREE_CODE (op1) != THROW_EXPR) |
2777 | op1 = cp_build_unary_op (code, op1, false, complain); |
2778 | tree op2 = TREE_OPERAND (t, 2); |
2779 | if (TREE_CODE (op2) != THROW_EXPR) |
2780 | op2 = cp_build_unary_op (code, op2, false, complain); |
2781 | |
2782 | return |
2783 | build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain); |
2784 | } |
2785 | |
2786 | /* Given the TYPE of an anonymous union field inside T, return the |
2787 | FIELD_DECL for the field. If not found return NULL_TREE. Because |
2788 | anonymous unions can nest, we must also search all anonymous unions |
2789 | that are directly reachable. */ |
2790 | |
2791 | tree |
2792 | lookup_anon_field (tree, tree type) |
2793 | { |
2794 | tree field; |
2795 | |
2796 | type = TYPE_MAIN_VARIANT (type); |
2797 | field = ANON_AGGR_TYPE_FIELD (type); |
2798 | gcc_assert (field); |
2799 | return field; |
2800 | } |
2801 | |
2802 | /* Build an expression representing OBJECT.MEMBER. OBJECT is an |
2803 | expression; MEMBER is a DECL or baselink. If ACCESS_PATH is |
2804 | non-NULL, it indicates the path to the base used to name MEMBER. |
2805 | If PRESERVE_REFERENCE is true, the expression returned will have |
2806 | REFERENCE_TYPE if the MEMBER does. Otherwise, the expression |
2807 | returned will have the type referred to by the reference. |
2808 | |
2809 | This function does not perform access control; that is either done |
2810 | earlier by the parser when the name of MEMBER is resolved to MEMBER |
2811 | itself, or later when overload resolution selects one of the |
2812 | functions indicated by MEMBER. */ |
2813 | |
2814 | tree |
2815 | build_class_member_access_expr (cp_expr object, tree member, |
2816 | tree access_path, bool preserve_reference, |
2817 | tsubst_flags_t complain) |
2818 | { |
2819 | tree object_type; |
2820 | tree member_scope; |
2821 | tree result = NULL_TREE; |
2822 | tree using_decl = NULL_TREE; |
2823 | |
2824 | if (error_operand_p (t: object) || error_operand_p (t: member)) |
2825 | return error_mark_node; |
2826 | |
2827 | gcc_assert (DECL_P (member) || BASELINK_P (member)); |
2828 | |
2829 | /* [expr.ref] |
2830 | |
2831 | The type of the first expression shall be "class object" (of a |
2832 | complete type). */ |
2833 | object_type = TREE_TYPE (object); |
2834 | if (!currently_open_class (object_type) |
2835 | && !complete_type_or_maybe_complain (type: object_type, value: object, complain)) |
2836 | return error_mark_node; |
2837 | if (!CLASS_TYPE_P (object_type)) |
2838 | { |
2839 | if (complain & tf_error) |
2840 | { |
2841 | if (INDIRECT_TYPE_P (object_type) |
2842 | && CLASS_TYPE_P (TREE_TYPE (object_type))) |
2843 | error ("request for member %qD in %qE, which is of pointer " |
2844 | "type %qT (maybe you meant to use %<->%> ?)" , |
2845 | member, object.get_value (), object_type); |
2846 | else |
2847 | error ("request for member %qD in %qE, which is of non-class " |
2848 | "type %qT" , member, object.get_value (), object_type); |
2849 | } |
2850 | return error_mark_node; |
2851 | } |
2852 | |
2853 | /* The standard does not seem to actually say that MEMBER must be a |
2854 | member of OBJECT_TYPE. However, that is clearly what is |
2855 | intended. */ |
2856 | if (DECL_P (member)) |
2857 | { |
2858 | member_scope = DECL_CLASS_CONTEXT (member); |
2859 | if (!mark_used (member, complain) && !(complain & tf_error)) |
2860 | return error_mark_node; |
2861 | |
2862 | if (TREE_UNAVAILABLE (member)) |
2863 | error_unavailable_use (member, NULL_TREE); |
2864 | else if (TREE_DEPRECATED (member)) |
2865 | warn_deprecated_use (member, NULL_TREE); |
2866 | } |
2867 | else |
2868 | member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member)); |
2869 | /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will |
2870 | presently be the anonymous union. Go outwards until we find a |
2871 | type related to OBJECT_TYPE. */ |
2872 | while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope)) |
2873 | && !same_type_ignoring_top_level_qualifiers_p (type1: member_scope, |
2874 | type2: object_type)) |
2875 | member_scope = TYPE_CONTEXT (member_scope); |
2876 | if (!member_scope || !DERIVED_FROM_P (member_scope, object_type)) |
2877 | { |
2878 | if (complain & tf_error) |
2879 | { |
2880 | if (TREE_CODE (member) == FIELD_DECL) |
2881 | error ("invalid use of non-static data member %qE" , member); |
2882 | else |
2883 | error ("%qD is not a member of %qT" , member, object_type); |
2884 | } |
2885 | return error_mark_node; |
2886 | } |
2887 | |
2888 | /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into |
2889 | `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue |
2890 | in the front end; only _DECLs and _REFs are lvalues in the back end. */ |
2891 | if (tree temp = unary_complex_lvalue (ADDR_EXPR, object)) |
2892 | { |
2893 | temp = cp_build_fold_indirect_ref (temp); |
2894 | if (!lvalue_p (object) && lvalue_p (temp)) |
2895 | /* Preserve rvalueness. */ |
2896 | temp = move (temp); |
2897 | object = temp; |
2898 | } |
2899 | |
2900 | /* In [expr.ref], there is an explicit list of the valid choices for |
2901 | MEMBER. We check for each of those cases here. */ |
2902 | if (VAR_P (member)) |
2903 | { |
2904 | /* A static data member. */ |
2905 | result = member; |
2906 | mark_exp_read (object); |
2907 | |
2908 | if (tree wrap = maybe_get_tls_wrapper_call (result)) |
2909 | /* Replace an evaluated use of the thread_local variable with |
2910 | a call to its wrapper. */ |
2911 | result = wrap; |
2912 | |
2913 | /* If OBJECT has side-effects, they are supposed to occur. */ |
2914 | if (TREE_SIDE_EFFECTS (object)) |
2915 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result); |
2916 | } |
2917 | else if (TREE_CODE (member) == FIELD_DECL) |
2918 | { |
2919 | /* A non-static data member. */ |
2920 | bool null_object_p; |
2921 | int type_quals; |
2922 | tree member_type; |
2923 | |
2924 | if (INDIRECT_REF_P (object)) |
2925 | null_object_p = |
2926 | integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0))); |
2927 | else |
2928 | null_object_p = false; |
2929 | |
2930 | /* Convert OBJECT to the type of MEMBER. */ |
2931 | if (!same_type_p (TYPE_MAIN_VARIANT (object_type), |
2932 | TYPE_MAIN_VARIANT (member_scope))) |
2933 | { |
2934 | tree binfo; |
2935 | base_kind kind; |
2936 | |
2937 | /* We didn't complain above about a currently open class, but now we |
2938 | must: we don't know how to refer to a base member before layout is |
2939 | complete. But still don't complain in a template. */ |
2940 | if (!cp_unevaluated_operand |
2941 | && !dependent_type_p (object_type) |
2942 | && !complete_type_or_maybe_complain (type: object_type, value: object, |
2943 | complain)) |
2944 | return error_mark_node; |
2945 | |
2946 | binfo = lookup_base (access_path ? access_path : object_type, |
2947 | member_scope, ba_unique, &kind, complain); |
2948 | if (binfo == error_mark_node) |
2949 | return error_mark_node; |
2950 | |
2951 | /* It is invalid to try to get to a virtual base of a |
2952 | NULL object. The most common cause is invalid use of |
2953 | offsetof macro. */ |
2954 | if (null_object_p && kind == bk_via_virtual) |
2955 | { |
2956 | if (complain & tf_error) |
2957 | { |
2958 | error ("invalid access to non-static data member %qD in " |
2959 | "virtual base of NULL object" , member); |
2960 | } |
2961 | return error_mark_node; |
2962 | } |
2963 | |
2964 | /* Convert to the base. */ |
2965 | object = build_base_path (PLUS_EXPR, object, binfo, |
2966 | /*nonnull=*/1, complain); |
2967 | /* If we found the base successfully then we should be able |
2968 | to convert to it successfully. */ |
2969 | gcc_assert (object != error_mark_node); |
2970 | } |
2971 | |
2972 | /* If MEMBER is from an anonymous aggregate, we have converted |
2973 | OBJECT so that it refers to the class containing the |
2974 | anonymous union. Generate a reference to the anonymous union |
2975 | itself, and recur to find MEMBER. */ |
2976 | if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member)) |
2977 | /* When this code is called from build_field_call, the |
2978 | object already has the type of the anonymous union. |
2979 | That is because the COMPONENT_REF was already |
2980 | constructed, and was then disassembled before calling |
2981 | build_field_call. After the function-call code is |
2982 | cleaned up, this waste can be eliminated. */ |
2983 | && (!same_type_ignoring_top_level_qualifiers_p |
2984 | (TREE_TYPE (object), DECL_CONTEXT (member)))) |
2985 | { |
2986 | tree anonymous_union; |
2987 | |
2988 | anonymous_union = lookup_anon_field (TREE_TYPE (object), |
2989 | DECL_CONTEXT (member)); |
2990 | object = build_class_member_access_expr (object, |
2991 | member: anonymous_union, |
2992 | /*access_path=*/NULL_TREE, |
2993 | preserve_reference, |
2994 | complain); |
2995 | } |
2996 | |
2997 | /* Compute the type of the field, as described in [expr.ref]. */ |
2998 | type_quals = TYPE_UNQUALIFIED; |
2999 | member_type = TREE_TYPE (member); |
3000 | if (!TYPE_REF_P (member_type)) |
3001 | { |
3002 | type_quals = (cp_type_quals (member_type) |
3003 | | cp_type_quals (object_type)); |
3004 | |
3005 | /* A field is const (volatile) if the enclosing object, or the |
3006 | field itself, is const (volatile). But, a mutable field is |
3007 | not const, even within a const object. */ |
3008 | if (DECL_MUTABLE_P (member)) |
3009 | type_quals &= ~TYPE_QUAL_CONST; |
3010 | member_type = cp_build_qualified_type (member_type, type_quals); |
3011 | } |
3012 | |
3013 | result = build3_loc (loc: input_location, code: COMPONENT_REF, type: member_type, |
3014 | arg0: object, arg1: member, NULL_TREE); |
3015 | |
3016 | /* Mark the expression const or volatile, as appropriate. Even |
3017 | though we've dealt with the type above, we still have to mark the |
3018 | expression itself. */ |
3019 | if (type_quals & TYPE_QUAL_CONST) |
3020 | TREE_READONLY (result) = 1; |
3021 | if (type_quals & TYPE_QUAL_VOLATILE) |
3022 | TREE_THIS_VOLATILE (result) = 1; |
3023 | } |
3024 | else if (BASELINK_P (member)) |
3025 | { |
3026 | /* The member is a (possibly overloaded) member function. */ |
3027 | tree functions; |
3028 | tree type; |
3029 | |
3030 | /* If the MEMBER is exactly one static member function, then we |
3031 | know the type of the expression. Otherwise, we must wait |
3032 | until overload resolution has been performed. */ |
3033 | functions = BASELINK_FUNCTIONS (member); |
3034 | if (TREE_CODE (functions) == FUNCTION_DECL |
3035 | && DECL_STATIC_FUNCTION_P (functions)) |
3036 | type = TREE_TYPE (functions); |
3037 | else |
3038 | type = unknown_type_node; |
3039 | /* Note that we do not convert OBJECT to the BASELINK_BINFO |
3040 | base. That will happen when the function is called. */ |
3041 | result = build3_loc (loc: input_location, code: COMPONENT_REF, type, arg0: object, arg1: member, |
3042 | NULL_TREE); |
3043 | } |
3044 | else if (TREE_CODE (member) == CONST_DECL) |
3045 | { |
3046 | /* The member is an enumerator. */ |
3047 | result = member; |
3048 | /* If OBJECT has side-effects, they are supposed to occur. */ |
3049 | if (TREE_SIDE_EFFECTS (object)) |
3050 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
3051 | object, result); |
3052 | } |
3053 | else if ((using_decl = strip_using_decl (member)) != member) |
3054 | result = build_class_member_access_expr (object, |
3055 | member: using_decl, |
3056 | access_path, preserve_reference, |
3057 | complain); |
3058 | else |
3059 | { |
3060 | if (complain & tf_error) |
3061 | error ("invalid use of %qD" , member); |
3062 | return error_mark_node; |
3063 | } |
3064 | |
3065 | if (!preserve_reference) |
3066 | /* [expr.ref] |
3067 | |
3068 | If E2 is declared to have type "reference to T", then ... the |
3069 | type of E1.E2 is T. */ |
3070 | result = convert_from_reference (result); |
3071 | |
3072 | return result; |
3073 | } |
3074 | |
3075 | /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if |
3076 | SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */ |
3077 | |
3078 | tree |
3079 | lookup_destructor (tree object, tree scope, tree dtor_name, |
3080 | tsubst_flags_t complain) |
3081 | { |
3082 | tree object_type = TREE_TYPE (object); |
3083 | tree dtor_type = TREE_OPERAND (dtor_name, 0); |
3084 | tree expr; |
3085 | |
3086 | /* We've already complained about this destructor. */ |
3087 | if (dtor_type == error_mark_node) |
3088 | return error_mark_node; |
3089 | |
3090 | if (scope && !check_dtor_name (scope, dtor_type)) |
3091 | { |
3092 | if (complain & tf_error) |
3093 | error ("qualified type %qT does not match destructor name ~%qT" , |
3094 | scope, dtor_type); |
3095 | return error_mark_node; |
3096 | } |
3097 | if (is_auto (dtor_type)) |
3098 | dtor_type = object_type; |
3099 | else if (identifier_p (t: dtor_type)) |
3100 | { |
3101 | /* In a template, names we can't find a match for are still accepted |
3102 | destructor names, and we check them here. */ |
3103 | if (check_dtor_name (object_type, dtor_type)) |
3104 | dtor_type = object_type; |
3105 | else |
3106 | { |
3107 | if (complain & tf_error) |
3108 | error ("object type %qT does not match destructor name ~%qT" , |
3109 | object_type, dtor_type); |
3110 | return error_mark_node; |
3111 | } |
3112 | |
3113 | } |
3114 | else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type))) |
3115 | { |
3116 | if (complain & tf_error) |
3117 | error ("the type being destroyed is %qT, but the destructor " |
3118 | "refers to %qT" , TYPE_MAIN_VARIANT (object_type), dtor_type); |
3119 | return error_mark_node; |
3120 | } |
3121 | expr = lookup_member (dtor_type, complete_dtor_identifier, |
3122 | /*protect=*/1, /*want_type=*/false, |
3123 | tf_warning_or_error); |
3124 | if (!expr) |
3125 | { |
3126 | if (complain & tf_error) |
3127 | cxx_incomplete_type_error (value: dtor_name, type: dtor_type); |
3128 | return error_mark_node; |
3129 | } |
3130 | expr = (adjust_result_of_qualified_name_lookup |
3131 | (expr, dtor_type, object_type)); |
3132 | if (scope == NULL_TREE) |
3133 | /* We need to call adjust_result_of_qualified_name_lookup in case the |
3134 | destructor names a base class, but we unset BASELINK_QUALIFIED_P so |
3135 | that we still get virtual function binding. */ |
3136 | BASELINK_QUALIFIED_P (expr) = false; |
3137 | return expr; |
3138 | } |
3139 | |
3140 | /* An expression of the form "A::template B" has been resolved to |
3141 | DECL. Issue a diagnostic if B is not a template or template |
3142 | specialization. */ |
3143 | |
3144 | void |
3145 | check_template_keyword (tree decl) |
3146 | { |
3147 | /* The standard says: |
3148 | |
3149 | [temp.names] |
3150 | |
3151 | If a name prefixed by the keyword template is not a member |
3152 | template, the program is ill-formed. |
3153 | |
3154 | DR 228 removed the restriction that the template be a member |
3155 | template. |
3156 | |
3157 | DR 96, if accepted would add the further restriction that explicit |
3158 | template arguments must be provided if the template keyword is |
3159 | used, but, as of 2005-10-16, that DR is still in "drafting". If |
3160 | this DR is accepted, then the semantic checks here can be |
3161 | simplified, as the entity named must in fact be a template |
3162 | specialization, rather than, as at present, a set of overloaded |
3163 | functions containing at least one template function. */ |
3164 | if (TREE_CODE (decl) != TEMPLATE_DECL |
3165 | && TREE_CODE (decl) != TEMPLATE_ID_EXPR) |
3166 | { |
3167 | if (VAR_P (decl)) |
3168 | { |
3169 | if (DECL_USE_TEMPLATE (decl) |
3170 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) |
3171 | ; |
3172 | else |
3173 | permerror (input_location, "%qD is not a template" , decl); |
3174 | } |
3175 | else if (!is_overloaded_fn (decl)) |
3176 | permerror (input_location, "%qD is not a template" , decl); |
3177 | else |
3178 | { |
3179 | bool found = false; |
3180 | |
3181 | for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl)); |
3182 | !found && iter; ++iter) |
3183 | { |
3184 | tree fn = *iter; |
3185 | if (TREE_CODE (fn) == TEMPLATE_DECL |
3186 | || TREE_CODE (fn) == TEMPLATE_ID_EXPR |
3187 | || (TREE_CODE (fn) == FUNCTION_DECL |
3188 | && DECL_USE_TEMPLATE (fn) |
3189 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))) |
3190 | found = true; |
3191 | } |
3192 | if (!found) |
3193 | permerror (input_location, "%qD is not a template" , decl); |
3194 | } |
3195 | } |
3196 | } |
3197 | |
3198 | /* Record that an access failure occurred on BASETYPE_PATH attempting |
3199 | to access DECL, where DIAG_DECL should be used for diagnostics. */ |
3200 | |
3201 | void |
3202 | access_failure_info::record_access_failure (tree basetype_path, |
3203 | tree decl, tree diag_decl) |
3204 | { |
3205 | m_was_inaccessible = true; |
3206 | m_basetype_path = basetype_path; |
3207 | m_decl = decl; |
3208 | m_diag_decl = diag_decl; |
3209 | } |
3210 | |
3211 | /* If an access failure was recorded, then attempt to locate an |
3212 | accessor function for the pertinent field. |
3213 | Otherwise, return NULL_TREE. */ |
3214 | |
3215 | tree |
3216 | access_failure_info::get_any_accessor (bool const_p) const |
3217 | { |
3218 | if (!was_inaccessible_p ()) |
3219 | return NULL_TREE; |
3220 | |
3221 | tree accessor |
3222 | = locate_field_accessor (m_basetype_path, m_diag_decl, const_p); |
3223 | if (!accessor) |
3224 | return NULL_TREE; |
3225 | |
3226 | /* The accessor must itself be accessible for it to be a reasonable |
3227 | suggestion. */ |
3228 | if (!accessible_p (m_basetype_path, accessor, true)) |
3229 | return NULL_TREE; |
3230 | |
3231 | return accessor; |
3232 | } |
3233 | |
3234 | /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by |
3235 | replacing the primary location in RICHLOC with "accessor()". */ |
3236 | |
3237 | void |
3238 | access_failure_info::add_fixit_hint (rich_location *richloc, |
3239 | tree accessor_decl) |
3240 | { |
3241 | pretty_printer pp; |
3242 | pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl))); |
3243 | pp_string (&pp, "()" ); |
3244 | richloc->add_fixit_replace (new_content: pp_formatted_text (&pp)); |
3245 | } |
3246 | |
3247 | /* If an access failure was recorded, then attempt to locate an |
3248 | accessor function for the pertinent field, and if one is |
3249 | available, add a note and fix-it hint suggesting using it. */ |
3250 | |
3251 | void |
3252 | access_failure_info::maybe_suggest_accessor (bool const_p) const |
3253 | { |
3254 | tree accessor = get_any_accessor (const_p); |
3255 | if (accessor == NULL_TREE) |
3256 | return; |
3257 | rich_location richloc (line_table, input_location); |
3258 | add_fixit_hint (richloc: &richloc, accessor_decl: accessor); |
3259 | inform (&richloc, "field %q#D can be accessed via %q#D" , |
3260 | m_diag_decl, accessor); |
3261 | } |
3262 | |
3263 | /* Subroutine of finish_class_member_access_expr. |
3264 | Issue an error about NAME not being a member of ACCESS_PATH (or |
3265 | OBJECT_TYPE), potentially providing a fix-it hint for misspelled |
3266 | names. */ |
3267 | |
3268 | static void |
3269 | complain_about_unrecognized_member (tree access_path, tree name, |
3270 | tree object_type) |
3271 | { |
3272 | /* Attempt to provide a hint about misspelled names. */ |
3273 | tree guessed_id = lookup_member_fuzzy (access_path, name, |
3274 | /*want_type=*/false); |
3275 | if (guessed_id == NULL_TREE) |
3276 | { |
3277 | /* No hint. */ |
3278 | error ("%q#T has no member named %qE" , |
3279 | TREE_CODE (access_path) == TREE_BINFO |
3280 | ? TREE_TYPE (access_path) : object_type, name); |
3281 | return; |
3282 | } |
3283 | |
3284 | location_t bogus_component_loc = input_location; |
3285 | gcc_rich_location rich_loc (bogus_component_loc); |
3286 | |
3287 | /* Check that the guessed name is accessible along access_path. */ |
3288 | access_failure_info afi; |
3289 | lookup_member (access_path, guessed_id, /*protect=*/1, |
3290 | /*want_type=*/false, /*complain=*/false, |
3291 | afi: &afi); |
3292 | if (afi.was_inaccessible_p ()) |
3293 | { |
3294 | tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type)); |
3295 | if (accessor) |
3296 | { |
3297 | /* The guessed name isn't directly accessible, but can be accessed |
3298 | via an accessor member function. */ |
3299 | afi.add_fixit_hint (richloc: &rich_loc, accessor_decl: accessor); |
3300 | error_at (&rich_loc, |
3301 | "%q#T has no member named %qE;" |
3302 | " did you mean %q#D? (accessible via %q#D)" , |
3303 | TREE_CODE (access_path) == TREE_BINFO |
3304 | ? TREE_TYPE (access_path) : object_type, |
3305 | name, afi.get_diag_decl (), accessor); |
3306 | } |
3307 | else |
3308 | { |
3309 | /* The guessed name isn't directly accessible, and no accessor |
3310 | member function could be found. */ |
3311 | error_at (&rich_loc, |
3312 | "%q#T has no member named %qE;" |
3313 | " did you mean %q#D? (not accessible from this context)" , |
3314 | TREE_CODE (access_path) == TREE_BINFO |
3315 | ? TREE_TYPE (access_path) : object_type, |
3316 | name, afi.get_diag_decl ()); |
3317 | complain_about_access (afi.get_decl (), afi.get_diag_decl (), |
3318 | afi.get_diag_decl (), false, ak_none); |
3319 | } |
3320 | } |
3321 | else |
3322 | { |
3323 | /* The guessed name is directly accessible; suggest it. */ |
3324 | rich_loc.add_fixit_misspelled_id (misspelled_token_loc: bogus_component_loc, |
3325 | hint_id: guessed_id); |
3326 | error_at (&rich_loc, |
3327 | "%q#T has no member named %qE;" |
3328 | " did you mean %qE?" , |
3329 | TREE_CODE (access_path) == TREE_BINFO |
3330 | ? TREE_TYPE (access_path) : object_type, |
3331 | name, guessed_id); |
3332 | } |
3333 | } |
3334 | |
3335 | /* This function is called by the parser to process a class member |
3336 | access expression of the form OBJECT.NAME. NAME is a node used by |
3337 | the parser to represent a name; it is not yet a DECL. It may, |
3338 | however, be a BASELINK where the BASELINK_FUNCTIONS is a |
3339 | TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and |
3340 | there is no reason to do the lookup twice, so the parser keeps the |
3341 | BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to |
3342 | be a template via the use of the "A::template B" syntax. */ |
3343 | |
3344 | tree |
3345 | finish_class_member_access_expr (cp_expr object, tree name, bool template_p, |
3346 | tsubst_flags_t complain) |
3347 | { |
3348 | tree expr; |
3349 | tree object_type; |
3350 | tree member; |
3351 | tree access_path = NULL_TREE; |
3352 | tree orig_object = object; |
3353 | tree orig_name = name; |
3354 | |
3355 | if (object == error_mark_node || name == error_mark_node) |
3356 | return error_mark_node; |
3357 | |
3358 | /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */ |
3359 | if (!objc_is_public (object, name)) |
3360 | return error_mark_node; |
3361 | |
3362 | object_type = TREE_TYPE (object); |
3363 | |
3364 | if (processing_template_decl) |
3365 | { |
3366 | if (/* If OBJECT is dependent, so is OBJECT.NAME. */ |
3367 | type_dependent_object_expression_p (object) |
3368 | /* If NAME is "f<args>", where either 'f' or 'args' is |
3369 | dependent, then the expression is dependent. */ |
3370 | || (TREE_CODE (name) == TEMPLATE_ID_EXPR |
3371 | && dependent_template_id_p (TREE_OPERAND (name, 0), |
3372 | TREE_OPERAND (name, 1))) |
3373 | /* If NAME is "T::X" where "T" is dependent, then the |
3374 | expression is dependent. */ |
3375 | || (TREE_CODE (name) == SCOPE_REF |
3376 | && TYPE_P (TREE_OPERAND (name, 0)) |
3377 | && dependent_scope_p (TREE_OPERAND (name, 0))) |
3378 | /* If NAME is operator T where "T" is dependent, we can't |
3379 | lookup until we instantiate the T. */ |
3380 | || (TREE_CODE (name) == IDENTIFIER_NODE |
3381 | && IDENTIFIER_CONV_OP_P (name) |
3382 | && dependent_type_p (TREE_TYPE (name)))) |
3383 | { |
3384 | dependent: |
3385 | return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, |
3386 | orig_object, orig_name, NULL_TREE); |
3387 | } |
3388 | } |
3389 | else if (c_dialect_objc () |
3390 | && identifier_p (t: name) |
3391 | && (expr = objc_maybe_build_component_ref (object, name))) |
3392 | return expr; |
3393 | |
3394 | /* [expr.ref] |
3395 | |
3396 | The type of the first expression shall be "class object" (of a |
3397 | complete type). */ |
3398 | if (!currently_open_class (object_type) |
3399 | && !complete_type_or_maybe_complain (type: object_type, value: object, complain)) |
3400 | return error_mark_node; |
3401 | if (!CLASS_TYPE_P (object_type)) |
3402 | { |
3403 | if (complain & tf_error) |
3404 | { |
3405 | if (INDIRECT_TYPE_P (object_type) |
3406 | && CLASS_TYPE_P (TREE_TYPE (object_type))) |
3407 | error ("request for member %qD in %qE, which is of pointer " |
3408 | "type %qT (maybe you meant to use %<->%> ?)" , |
3409 | name, object.get_value (), object_type); |
3410 | else |
3411 | error ("request for member %qD in %qE, which is of non-class " |
3412 | "type %qT" , name, object.get_value (), object_type); |
3413 | } |
3414 | return error_mark_node; |
3415 | } |
3416 | |
3417 | if (BASELINK_P (name)) |
3418 | /* A member function that has already been looked up. */ |
3419 | member = name; |
3420 | else |
3421 | { |
3422 | bool is_template_id = false; |
3423 | tree template_args = NULL_TREE; |
3424 | tree scope = NULL_TREE; |
3425 | |
3426 | access_path = object_type; |
3427 | |
3428 | if (TREE_CODE (name) == SCOPE_REF) |
3429 | { |
3430 | /* A qualified name. The qualifying class or namespace `S' |
3431 | has already been looked up; it is either a TYPE or a |
3432 | NAMESPACE_DECL. */ |
3433 | scope = TREE_OPERAND (name, 0); |
3434 | name = TREE_OPERAND (name, 1); |
3435 | |
3436 | /* If SCOPE is a namespace, then the qualified name does not |
3437 | name a member of OBJECT_TYPE. */ |
3438 | if (TREE_CODE (scope) == NAMESPACE_DECL) |
3439 | { |
3440 | if (complain & tf_error) |
3441 | error ("%<%D::%D%> is not a member of %qT" , |
3442 | scope, name, object_type); |
3443 | return error_mark_node; |
3444 | } |
3445 | } |
3446 | |
3447 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
3448 | { |
3449 | is_template_id = true; |
3450 | template_args = TREE_OPERAND (name, 1); |
3451 | name = TREE_OPERAND (name, 0); |
3452 | |
3453 | if (!identifier_p (t: name)) |
3454 | name = OVL_NAME (name); |
3455 | } |
3456 | |
3457 | if (scope) |
3458 | { |
3459 | if (TREE_CODE (scope) == ENUMERAL_TYPE) |
3460 | { |
3461 | gcc_assert (!is_template_id); |
3462 | /* Looking up a member enumerator (c++/56793). */ |
3463 | if (!TYPE_CLASS_SCOPE_P (scope) |
3464 | || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type)) |
3465 | { |
3466 | if (complain & tf_error) |
3467 | error ("%<%D::%D%> is not a member of %qT" , |
3468 | scope, name, object_type); |
3469 | return error_mark_node; |
3470 | } |
3471 | tree val = lookup_enumerator (scope, name); |
3472 | if (!val) |
3473 | { |
3474 | if (complain & tf_error) |
3475 | error ("%qD is not a member of %qD" , |
3476 | name, scope); |
3477 | return error_mark_node; |
3478 | } |
3479 | |
3480 | if (TREE_SIDE_EFFECTS (object)) |
3481 | val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val); |
3482 | return val; |
3483 | } |
3484 | |
3485 | gcc_assert (CLASS_TYPE_P (scope)); |
3486 | gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR); |
3487 | |
3488 | if (constructor_name_p (name, scope)) |
3489 | { |
3490 | if (complain & tf_error) |
3491 | error ("cannot call constructor %<%T::%D%> directly" , |
3492 | scope, name); |
3493 | return error_mark_node; |
3494 | } |
3495 | |
3496 | /* Find the base of OBJECT_TYPE corresponding to SCOPE. */ |
3497 | access_path = lookup_base (object_type, scope, ba_check, |
3498 | NULL, complain); |
3499 | if (access_path == error_mark_node) |
3500 | return error_mark_node; |
3501 | if (!access_path) |
3502 | { |
3503 | if (any_dependent_bases_p (object_type)) |
3504 | goto dependent; |
3505 | if (complain & tf_error) |
3506 | error ("%qT is not a base of %qT" , scope, object_type); |
3507 | return error_mark_node; |
3508 | } |
3509 | } |
3510 | |
3511 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
3512 | { |
3513 | if (dependent_type_p (object_type)) |
3514 | /* The destructor isn't declared yet. */ |
3515 | goto dependent; |
3516 | member = lookup_destructor (object, scope, dtor_name: name, complain); |
3517 | } |
3518 | else |
3519 | { |
3520 | /* Look up the member. */ |
3521 | access_failure_info afi; |
3522 | if (processing_template_decl) |
3523 | /* Even though this class member access expression is at this |
3524 | point not dependent, the member itself may be dependent, and |
3525 | we must not potentially push a access check for a dependent |
3526 | member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access |
3527 | ahead of time here; we're going to redo this member lookup at |
3528 | instantiation time anyway. */ |
3529 | push_deferring_access_checks (dk_no_check); |
3530 | member = lookup_member (access_path, name, /*protect=*/1, |
3531 | /*want_type=*/false, complain, |
3532 | afi: &afi); |
3533 | if (processing_template_decl) |
3534 | pop_deferring_access_checks (); |
3535 | afi.maybe_suggest_accessor (TYPE_READONLY (object_type)); |
3536 | if (member == NULL_TREE) |
3537 | { |
3538 | if (dependent_type_p (object_type)) |
3539 | /* Try again at instantiation time. */ |
3540 | goto dependent; |
3541 | if (complain & tf_error) |
3542 | complain_about_unrecognized_member (access_path, name, |
3543 | object_type); |
3544 | return error_mark_node; |
3545 | } |
3546 | if (member == error_mark_node) |
3547 | return error_mark_node; |
3548 | if (DECL_P (member) |
3549 | && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member))) |
3550 | /* Dependent type attributes on the decl mean that the TREE_TYPE is |
3551 | wrong, so don't use it. */ |
3552 | goto dependent; |
3553 | if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member)) |
3554 | goto dependent; |
3555 | } |
3556 | |
3557 | if (is_template_id) |
3558 | { |
3559 | tree templ = member; |
3560 | |
3561 | if (BASELINK_P (templ)) |
3562 | member = lookup_template_function (templ, template_args); |
3563 | else if (variable_template_p (t: templ)) |
3564 | member = (lookup_and_finish_template_variable |
3565 | (templ, template_args, complain)); |
3566 | else |
3567 | { |
3568 | if (complain & tf_error) |
3569 | error ("%qD is not a member template function" , name); |
3570 | return error_mark_node; |
3571 | } |
3572 | } |
3573 | } |
3574 | |
3575 | if (TREE_UNAVAILABLE (member)) |
3576 | error_unavailable_use (member, NULL_TREE); |
3577 | else if (TREE_DEPRECATED (member)) |
3578 | warn_deprecated_use (member, NULL_TREE); |
3579 | |
3580 | if (template_p) |
3581 | check_template_keyword (decl: member); |
3582 | |
3583 | expr = build_class_member_access_expr (object, member, access_path, |
3584 | /*preserve_reference=*/false, |
3585 | complain); |
3586 | if (processing_template_decl && expr != error_mark_node) |
3587 | { |
3588 | if (BASELINK_P (member)) |
3589 | { |
3590 | if (TREE_CODE (orig_name) == SCOPE_REF) |
3591 | BASELINK_QUALIFIED_P (member) = 1; |
3592 | orig_name = member; |
3593 | } |
3594 | return build_min_non_dep (COMPONENT_REF, expr, |
3595 | orig_object, orig_name, |
3596 | NULL_TREE); |
3597 | } |
3598 | |
3599 | return expr; |
3600 | } |
3601 | |
3602 | /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate |
3603 | type. */ |
3604 | |
3605 | tree |
3606 | build_simple_component_ref (tree object, tree member) |
3607 | { |
3608 | tree type = cp_build_qualified_type (TREE_TYPE (member), |
3609 | cp_type_quals (TREE_TYPE (object))); |
3610 | return build3_loc (loc: input_location, |
3611 | code: COMPONENT_REF, type, |
3612 | arg0: object, arg1: member, NULL_TREE); |
3613 | } |
3614 | |
3615 | /* Return an expression for the MEMBER_NAME field in the internal |
3616 | representation of PTRMEM, a pointer-to-member function. (Each |
3617 | pointer-to-member function type gets its own RECORD_TYPE so it is |
3618 | more convenient to access the fields by name than by FIELD_DECL.) |
3619 | This routine converts the NAME to a FIELD_DECL and then creates the |
3620 | node for the complete expression. */ |
3621 | |
3622 | tree |
3623 | build_ptrmemfunc_access_expr (tree ptrmem, tree member_name) |
3624 | { |
3625 | tree ptrmem_type; |
3626 | tree member; |
3627 | |
3628 | if (TREE_CODE (ptrmem) == CONSTRUCTOR) |
3629 | { |
3630 | for (auto &e: CONSTRUCTOR_ELTS (ptrmem)) |
3631 | if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name) |
3632 | return e.value; |
3633 | gcc_unreachable (); |
3634 | } |
3635 | |
3636 | /* This code is a stripped down version of |
3637 | build_class_member_access_expr. It does not work to use that |
3638 | routine directly because it expects the object to be of class |
3639 | type. */ |
3640 | ptrmem_type = TREE_TYPE (ptrmem); |
3641 | gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type)); |
3642 | for (member = TYPE_FIELDS (ptrmem_type); member; |
3643 | member = DECL_CHAIN (member)) |
3644 | if (DECL_NAME (member) == member_name) |
3645 | break; |
3646 | return build_simple_component_ref (object: ptrmem, member); |
3647 | } |
3648 | |
3649 | /* Return a TREE_LIST of namespace-scope overloads for the given operator, |
3650 | and for any other relevant operator. */ |
3651 | |
3652 | static tree |
3653 | op_unqualified_lookup (tree_code code, bool is_assign) |
3654 | { |
3655 | tree lookups = NULL_TREE; |
3656 | |
3657 | if (cxx_dialect >= cxx20 && !is_assign) |
3658 | { |
3659 | if (code == NE_EXPR) |
3660 | { |
3661 | /* != can get rewritten in terms of ==. */ |
3662 | tree fnname = ovl_op_identifier (isass: false, code: EQ_EXPR); |
3663 | if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE)) |
3664 | lookups = tree_cons (fnname, fns, lookups); |
3665 | } |
3666 | else if (code == GT_EXPR || code == LE_EXPR |
3667 | || code == LT_EXPR || code == GE_EXPR) |
3668 | { |
3669 | /* These can get rewritten in terms of <=>. */ |
3670 | tree fnname = ovl_op_identifier (isass: false, code: SPACESHIP_EXPR); |
3671 | if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE)) |
3672 | lookups = tree_cons (fnname, fns, lookups); |
3673 | } |
3674 | } |
3675 | |
3676 | tree fnname = ovl_op_identifier (isass: is_assign, code); |
3677 | if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE)) |
3678 | lookups = tree_cons (fnname, fns, lookups); |
3679 | |
3680 | if (lookups) |
3681 | return lookups; |
3682 | else |
3683 | return build_tree_list (NULL_TREE, NULL_TREE); |
3684 | } |
3685 | |
3686 | /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of |
3687 | the given operator. LOOKUPS, if non-NULL, is the result of phase 1 |
3688 | name lookup for the given operator. */ |
3689 | |
3690 | tree |
3691 | build_dependent_operator_type (tree lookups, tree_code code, bool is_assign) |
3692 | { |
3693 | if (lookups) |
3694 | /* We're partially instantiating a dependent operator expression, and |
3695 | LOOKUPS is the result of phase 1 name lookup that we performed |
3696 | earlier at template definition time, so just reuse the corresponding |
3697 | DEPENDENT_OPERATOR_TYPE. */ |
3698 | return TREE_TYPE (lookups); |
3699 | |
3700 | /* Otherwise we're processing a dependent operator expression at template |
3701 | definition time, so perform phase 1 name lookup now. */ |
3702 | lookups = op_unqualified_lookup (code, is_assign); |
3703 | |
3704 | tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE); |
3705 | DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups; |
3706 | TREE_TYPE (lookups) = type; |
3707 | return type; |
3708 | } |
3709 | |
3710 | /* Given an expression PTR for a pointer, return an expression |
3711 | for the value pointed to. |
3712 | ERRORSTRING is the name of the operator to appear in error messages. |
3713 | |
3714 | This function may need to overload OPERATOR_FNNAME. |
3715 | Must also handle REFERENCE_TYPEs for C++. */ |
3716 | |
3717 | tree |
3718 | build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring, |
3719 | tree lookups, tsubst_flags_t complain) |
3720 | { |
3721 | tree orig_expr = expr; |
3722 | tree rval; |
3723 | tree overload = NULL_TREE; |
3724 | |
3725 | if (processing_template_decl) |
3726 | { |
3727 | /* Retain the type if we know the operand is a pointer. */ |
3728 | if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr))) |
3729 | { |
3730 | if (expr == current_class_ptr |
3731 | || (TREE_CODE (expr) == NOP_EXPR |
3732 | && TREE_OPERAND (expr, 0) == current_class_ptr |
3733 | && (same_type_ignoring_top_level_qualifiers_p |
3734 | (TREE_TYPE (expr), TREE_TYPE (current_class_ptr))))) |
3735 | return current_class_ref; |
3736 | return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr); |
3737 | } |
3738 | if (type_dependent_expression_p (expr)) |
3739 | { |
3740 | expr = build_min_nt_loc (loc, INDIRECT_REF, expr); |
3741 | TREE_TYPE (expr) |
3742 | = build_dependent_operator_type (lookups, code: INDIRECT_REF, is_assign: false); |
3743 | return expr; |
3744 | } |
3745 | } |
3746 | |
3747 | rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr, |
3748 | NULL_TREE, NULL_TREE, lookups, |
3749 | &overload, complain); |
3750 | if (!rval) |
3751 | rval = cp_build_indirect_ref (loc, expr, errorstring, complain); |
3752 | |
3753 | if (processing_template_decl && rval != error_mark_node) |
3754 | { |
3755 | if (overload != NULL_TREE) |
3756 | return (build_min_non_dep_op_overload |
3757 | (INDIRECT_REF, rval, overload, orig_expr)); |
3758 | |
3759 | return build_min_non_dep (INDIRECT_REF, rval, orig_expr); |
3760 | } |
3761 | else |
3762 | return rval; |
3763 | } |
3764 | |
3765 | /* Like c-family strict_aliasing_warning, but don't warn for dependent |
3766 | types or expressions. */ |
3767 | |
3768 | static bool |
3769 | cp_strict_aliasing_warning (location_t loc, tree type, tree expr) |
3770 | { |
3771 | if (processing_template_decl) |
3772 | { |
3773 | tree e = expr; |
3774 | STRIP_NOPS (e); |
3775 | if (dependent_type_p (type) || type_dependent_expression_p (e)) |
3776 | return false; |
3777 | } |
3778 | return strict_aliasing_warning (loc, type, expr); |
3779 | } |
3780 | |
3781 | /* The implementation of the above, and of indirection implied by other |
3782 | constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */ |
3783 | |
3784 | static tree |
3785 | cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring, |
3786 | tsubst_flags_t complain, bool do_fold) |
3787 | { |
3788 | tree pointer, type; |
3789 | |
3790 | /* RO_NULL should only be used with the folding entry points below, not |
3791 | cp_build_indirect_ref. */ |
3792 | gcc_checking_assert (errorstring != RO_NULL || do_fold); |
3793 | |
3794 | if (ptr == current_class_ptr |
3795 | || (TREE_CODE (ptr) == NOP_EXPR |
3796 | && TREE_OPERAND (ptr, 0) == current_class_ptr |
3797 | && (same_type_ignoring_top_level_qualifiers_p |
3798 | (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr))))) |
3799 | return current_class_ref; |
3800 | |
3801 | pointer = (TYPE_REF_P (TREE_TYPE (ptr)) |
3802 | ? ptr : decay_conversion (exp: ptr, complain)); |
3803 | if (pointer == error_mark_node) |
3804 | return error_mark_node; |
3805 | |
3806 | type = TREE_TYPE (pointer); |
3807 | |
3808 | if (INDIRECT_TYPE_P (type)) |
3809 | { |
3810 | /* [expr.unary.op] |
3811 | |
3812 | If the type of the expression is "pointer to T," the type |
3813 | of the result is "T." */ |
3814 | tree t = TREE_TYPE (type); |
3815 | |
3816 | if ((CONVERT_EXPR_P (ptr) |
3817 | || TREE_CODE (ptr) == VIEW_CONVERT_EXPR) |
3818 | && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t))) |
3819 | { |
3820 | /* If a warning is issued, mark it to avoid duplicates from |
3821 | the backend. This only needs to be done at |
3822 | warn_strict_aliasing > 2. */ |
3823 | if (warn_strict_aliasing > 2 |
3824 | && cp_strict_aliasing_warning (EXPR_LOCATION (ptr), |
3825 | type, TREE_OPERAND (ptr, 0))) |
3826 | suppress_warning (ptr, OPT_Wstrict_aliasing); |
3827 | } |
3828 | |
3829 | if (VOID_TYPE_P (t)) |
3830 | { |
3831 | /* A pointer to incomplete type (other than cv void) can be |
3832 | dereferenced [expr.unary.op]/1 */ |
3833 | if (complain & tf_error) |
3834 | error_at (loc, "%qT is not a pointer-to-object type" , type); |
3835 | return error_mark_node; |
3836 | } |
3837 | else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR |
3838 | && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0)))) |
3839 | /* The POINTER was something like `&x'. We simplify `*&x' to |
3840 | `x'. */ |
3841 | return TREE_OPERAND (pointer, 0); |
3842 | else |
3843 | { |
3844 | tree ref = build1 (INDIRECT_REF, t, pointer); |
3845 | |
3846 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, |
3847 | so that we get the proper error message if the result is used |
3848 | to assign to. Also, &* is supposed to be a no-op. */ |
3849 | TREE_READONLY (ref) = CP_TYPE_CONST_P (t); |
3850 | TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t); |
3851 | TREE_SIDE_EFFECTS (ref) |
3852 | = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)); |
3853 | return ref; |
3854 | } |
3855 | } |
3856 | else if (!(complain & tf_error)) |
3857 | /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */ |
3858 | ; |
3859 | /* `pointer' won't be an error_mark_node if we were given a |
3860 | pointer to member, so it's cool to check for this here. */ |
3861 | else if (TYPE_PTRMEM_P (type)) |
3862 | switch (errorstring) |
3863 | { |
3864 | case RO_ARRAY_INDEXING: |
3865 | error_at (loc, |
3866 | "invalid use of array indexing on pointer to member" ); |
3867 | break; |
3868 | case RO_UNARY_STAR: |
3869 | error_at (loc, "invalid use of unary %<*%> on pointer to member" ); |
3870 | break; |
3871 | case RO_IMPLICIT_CONVERSION: |
3872 | error_at (loc, "invalid use of implicit conversion on pointer " |
3873 | "to member" ); |
3874 | break; |
3875 | case RO_ARROW_STAR: |
3876 | error_at (loc, "left hand operand of %<->*%> must be a pointer to " |
3877 | "class, but is a pointer to member of type %qT" , type); |
3878 | break; |
3879 | default: |
3880 | gcc_unreachable (); |
3881 | } |
3882 | else if (pointer != error_mark_node) |
3883 | invalid_indirection_error (loc, type, errorstring); |
3884 | |
3885 | return error_mark_node; |
3886 | } |
3887 | |
3888 | /* Entry point used by c-common, which expects folding. */ |
3889 | |
3890 | tree |
3891 | build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring) |
3892 | { |
3893 | return cp_build_indirect_ref_1 (loc, ptr, errorstring, |
3894 | complain: tf_warning_or_error, do_fold: true); |
3895 | } |
3896 | |
3897 | /* Entry point used by internal indirection needs that don't correspond to any |
3898 | syntactic construct. */ |
3899 | |
3900 | tree |
3901 | cp_build_fold_indirect_ref (tree pointer) |
3902 | { |
3903 | return cp_build_indirect_ref_1 (loc: input_location, ptr: pointer, errorstring: RO_NULL, |
3904 | complain: tf_warning_or_error, do_fold: true); |
3905 | } |
3906 | |
3907 | /* Entry point used by indirection needs that correspond to some syntactic |
3908 | construct. */ |
3909 | |
3910 | tree |
3911 | cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring, |
3912 | tsubst_flags_t complain) |
3913 | { |
3914 | return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, do_fold: false); |
3915 | } |
3916 | |
3917 | /* This handles expressions of the form "a[i]", which denotes |
3918 | an array reference. |
3919 | |
3920 | This is logically equivalent in C to *(a+i), but we may do it differently. |
3921 | If A is a variable or a member, we generate a primitive ARRAY_REF. |
3922 | This avoids forcing the array out of registers, and can work on |
3923 | arrays that are not lvalues (for example, members of structures returned |
3924 | by functions). |
3925 | |
3926 | If INDEX is of some user-defined type, it must be converted to |
3927 | integer type. Otherwise, to make a compatible PLUS_EXPR, it |
3928 | will inherit the type of the array, which will be some pointer type. |
3929 | |
3930 | LOC is the location to use in building the array reference. */ |
3931 | |
3932 | tree |
3933 | cp_build_array_ref (location_t loc, tree array, tree idx, |
3934 | tsubst_flags_t complain) |
3935 | { |
3936 | tree ret; |
3937 | |
3938 | if (idx == 0) |
3939 | { |
3940 | if (complain & tf_error) |
3941 | error_at (loc, "subscript missing in array reference" ); |
3942 | return error_mark_node; |
3943 | } |
3944 | |
3945 | if (TREE_TYPE (array) == error_mark_node |
3946 | || TREE_TYPE (idx) == error_mark_node) |
3947 | return error_mark_node; |
3948 | |
3949 | /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference |
3950 | inside it. */ |
3951 | switch (TREE_CODE (array)) |
3952 | { |
3953 | case COMPOUND_EXPR: |
3954 | { |
3955 | tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx, |
3956 | complain); |
3957 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (value), |
3958 | TREE_OPERAND (array, 0), value); |
3959 | SET_EXPR_LOCATION (ret, loc); |
3960 | return ret; |
3961 | } |
3962 | |
3963 | case COND_EXPR: |
3964 | ret = build_conditional_expr |
3965 | (loc, TREE_OPERAND (array, 0), |
3966 | cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx, |
3967 | complain), |
3968 | cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx, |
3969 | complain), |
3970 | complain); |
3971 | protected_set_expr_location (ret, loc); |
3972 | return ret; |
3973 | |
3974 | default: |
3975 | break; |
3976 | } |
3977 | |
3978 | bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx); |
3979 | |
3980 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) |
3981 | { |
3982 | tree rval, type; |
3983 | |
3984 | warn_array_subscript_with_type_char (loc, idx); |
3985 | |
3986 | if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx))) |
3987 | { |
3988 | if (complain & tf_error) |
3989 | error_at (loc, "array subscript is not an integer" ); |
3990 | return error_mark_node; |
3991 | } |
3992 | |
3993 | /* Apply integral promotions *after* noticing character types. |
3994 | (It is unclear why we do these promotions -- the standard |
3995 | does not say that we should. In fact, the natural thing would |
3996 | seem to be to convert IDX to ptrdiff_t; we're performing |
3997 | pointer arithmetic.) */ |
3998 | idx = cp_perform_integral_promotions (expr: idx, complain); |
3999 | |
4000 | idx = maybe_fold_non_dependent_expr (idx, complain); |
4001 | |
4002 | /* An array that is indexed by a non-constant |
4003 | cannot be stored in a register; we must be able to do |
4004 | address arithmetic on its address. |
4005 | Likewise an array of elements of variable size. */ |
4006 | if (TREE_CODE (idx) != INTEGER_CST |
4007 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
4008 | && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) |
4009 | != INTEGER_CST))) |
4010 | { |
4011 | if (!cxx_mark_addressable (array, true)) |
4012 | return error_mark_node; |
4013 | } |
4014 | |
4015 | /* An array that is indexed by a constant value which is not within |
4016 | the array bounds cannot be stored in a register either; because we |
4017 | would get a crash in store_bit_field/extract_bit_field when trying |
4018 | to access a non-existent part of the register. */ |
4019 | if (TREE_CODE (idx) == INTEGER_CST |
4020 | && TYPE_DOMAIN (TREE_TYPE (array)) |
4021 | && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array)))) |
4022 | { |
4023 | if (!cxx_mark_addressable (array)) |
4024 | return error_mark_node; |
4025 | } |
4026 | |
4027 | /* Note in C++ it is valid to subscript a `register' array, since |
4028 | it is valid to take the address of something with that |
4029 | storage specification. */ |
4030 | if (extra_warnings) |
4031 | { |
4032 | tree foo = array; |
4033 | while (TREE_CODE (foo) == COMPONENT_REF) |
4034 | foo = TREE_OPERAND (foo, 0); |
4035 | if (VAR_P (foo) && DECL_REGISTER (foo) |
4036 | && (complain & tf_warning)) |
4037 | warning_at (loc, OPT_Wextra, |
4038 | "subscripting array declared %<register%>" ); |
4039 | } |
4040 | |
4041 | type = TREE_TYPE (TREE_TYPE (array)); |
4042 | rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE); |
4043 | /* Array ref is const/volatile if the array elements are |
4044 | or if the array is.. */ |
4045 | TREE_READONLY (rval) |
4046 | |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array)); |
4047 | TREE_SIDE_EFFECTS (rval) |
4048 | |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array)); |
4049 | TREE_THIS_VOLATILE (rval) |
4050 | |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array)); |
4051 | ret = require_complete_type (value: rval, complain); |
4052 | protected_set_expr_location (ret, loc); |
4053 | if (non_lvalue) |
4054 | ret = non_lvalue_loc (loc, ret); |
4055 | return ret; |
4056 | } |
4057 | |
4058 | { |
4059 | tree ar = cp_default_conversion (exp: array, complain); |
4060 | tree ind = cp_default_conversion (exp: idx, complain); |
4061 | tree first = NULL_TREE; |
4062 | |
4063 | if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind)) |
4064 | ar = first = save_expr (ar); |
4065 | |
4066 | /* Put the integer in IND to simplify error checking. */ |
4067 | if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) |
4068 | std::swap (a&: ar, b&: ind); |
4069 | |
4070 | if (ar == error_mark_node || ind == error_mark_node) |
4071 | return error_mark_node; |
4072 | |
4073 | if (!TYPE_PTR_P (TREE_TYPE (ar))) |
4074 | { |
4075 | if (complain & tf_error) |
4076 | error_at (loc, "subscripted value is neither array nor pointer" ); |
4077 | return error_mark_node; |
4078 | } |
4079 | if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) |
4080 | { |
4081 | if (complain & tf_error) |
4082 | error_at (loc, "array subscript is not an integer" ); |
4083 | return error_mark_node; |
4084 | } |
4085 | |
4086 | warn_array_subscript_with_type_char (loc, idx); |
4087 | |
4088 | ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain); |
4089 | if (first) |
4090 | ret = build2_loc (loc, code: COMPOUND_EXPR, TREE_TYPE (ret), arg0: first, arg1: ret); |
4091 | ret = cp_build_indirect_ref (loc, ptr: ret, errorstring: RO_ARRAY_INDEXING, complain); |
4092 | protected_set_expr_location (ret, loc); |
4093 | if (non_lvalue) |
4094 | ret = non_lvalue_loc (loc, ret); |
4095 | return ret; |
4096 | } |
4097 | } |
4098 | |
4099 | /* Entry point for Obj-C++. */ |
4100 | |
4101 | tree |
4102 | build_array_ref (location_t loc, tree array, tree idx) |
4103 | { |
4104 | return cp_build_array_ref (loc, array, idx, complain: tf_warning_or_error); |
4105 | } |
4106 | |
4107 | /* Resolve a pointer to member function. INSTANCE is the object |
4108 | instance to use, if the member points to a virtual member. |
4109 | |
4110 | This used to avoid checking for virtual functions if basetype |
4111 | has no virtual functions, according to an earlier ANSI draft. |
4112 | With the final ISO C++ rules, such an optimization is |
4113 | incorrect: A pointer to a derived member can be static_cast |
4114 | to pointer-to-base-member, as long as the dynamic object |
4115 | later has the right member. So now we only do this optimization |
4116 | when we know the dynamic type of the object. */ |
4117 | |
4118 | tree |
4119 | get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function, |
4120 | tsubst_flags_t complain) |
4121 | { |
4122 | if (TREE_CODE (function) == OFFSET_REF) |
4123 | function = TREE_OPERAND (function, 1); |
4124 | |
4125 | if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function))) |
4126 | { |
4127 | tree idx, delta, e1, e2, e3, vtbl; |
4128 | bool nonvirtual; |
4129 | tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)); |
4130 | tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)); |
4131 | |
4132 | tree instance_ptr = *instance_ptrptr; |
4133 | tree instance_save_expr = 0; |
4134 | if (instance_ptr == error_mark_node) |
4135 | { |
4136 | if (TREE_CODE (function) == PTRMEM_CST) |
4137 | { |
4138 | /* Extracting the function address from a pmf is only |
4139 | allowed with -Wno-pmf-conversions. It only works for |
4140 | pmf constants. */ |
4141 | e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain); |
4142 | e1 = convert (fntype, e1); |
4143 | return e1; |
4144 | } |
4145 | else |
4146 | { |
4147 | if (complain & tf_error) |
4148 | error ("object missing in use of %qE" , function); |
4149 | return error_mark_node; |
4150 | } |
4151 | } |
4152 | |
4153 | /* True if we know that the dynamic type of the object doesn't have |
4154 | virtual functions, so we can assume the PFN field is a pointer. */ |
4155 | nonvirtual = (COMPLETE_TYPE_P (basetype) |
4156 | && !TYPE_POLYMORPHIC_P (basetype) |
4157 | && resolves_to_fixed_type_p (instance_ptr, 0)); |
4158 | |
4159 | /* If we don't really have an object (i.e. in an ill-formed |
4160 | conversion from PMF to pointer), we can't resolve virtual |
4161 | functions anyway. */ |
4162 | if (!nonvirtual && is_dummy_object (instance_ptr)) |
4163 | nonvirtual = true; |
4164 | |
4165 | if (TREE_SIDE_EFFECTS (instance_ptr)) |
4166 | instance_ptr = instance_save_expr = save_expr (instance_ptr); |
4167 | |
4168 | if (TREE_SIDE_EFFECTS (function)) |
4169 | function = save_expr (function); |
4170 | |
4171 | /* Start by extracting all the information from the PMF itself. */ |
4172 | e3 = pfn_from_ptrmemfunc (function); |
4173 | delta = delta_from_ptrmemfunc (function); |
4174 | idx = build1 (NOP_EXPR, vtable_index_type, e3); |
4175 | switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) |
4176 | { |
4177 | int flag_sanitize_save; |
4178 | case ptrmemfunc_vbit_in_pfn: |
4179 | e1 = cp_build_binary_op (input_location, |
4180 | BIT_AND_EXPR, idx, integer_one_node, |
4181 | complain); |
4182 | idx = cp_build_binary_op (input_location, |
4183 | MINUS_EXPR, idx, integer_one_node, |
4184 | complain); |
4185 | if (idx == error_mark_node) |
4186 | return error_mark_node; |
4187 | break; |
4188 | |
4189 | case ptrmemfunc_vbit_in_delta: |
4190 | e1 = cp_build_binary_op (input_location, |
4191 | BIT_AND_EXPR, delta, integer_one_node, |
4192 | complain); |
4193 | /* Don't instrument the RSHIFT_EXPR we're about to create because |
4194 | we're going to use DELTA number of times, and that wouldn't play |
4195 | well with SAVE_EXPRs therein. */ |
4196 | flag_sanitize_save = flag_sanitize; |
4197 | flag_sanitize = 0; |
4198 | delta = cp_build_binary_op (input_location, |
4199 | RSHIFT_EXPR, delta, integer_one_node, |
4200 | complain); |
4201 | flag_sanitize = flag_sanitize_save; |
4202 | if (delta == error_mark_node) |
4203 | return error_mark_node; |
4204 | break; |
4205 | |
4206 | default: |
4207 | gcc_unreachable (); |
4208 | } |
4209 | |
4210 | if (e1 == error_mark_node) |
4211 | return error_mark_node; |
4212 | |
4213 | /* Convert down to the right base before using the instance. A |
4214 | special case is that in a pointer to member of class C, C may |
4215 | be incomplete. In that case, the function will of course be |
4216 | a member of C, and no conversion is required. In fact, |
4217 | lookup_base will fail in that case, because incomplete |
4218 | classes do not have BINFOs. */ |
4219 | if (!same_type_ignoring_top_level_qualifiers_p |
4220 | (type1: basetype, TREE_TYPE (TREE_TYPE (instance_ptr)))) |
4221 | { |
4222 | basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)), |
4223 | basetype, ba_check, NULL, complain); |
4224 | instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, |
4225 | 1, complain); |
4226 | if (instance_ptr == error_mark_node) |
4227 | return error_mark_node; |
4228 | } |
4229 | /* ...and then the delta in the PMF. */ |
4230 | instance_ptr = fold_build_pointer_plus (instance_ptr, delta); |
4231 | |
4232 | /* Hand back the adjusted 'this' argument to our caller. */ |
4233 | *instance_ptrptr = instance_ptr; |
4234 | |
4235 | if (nonvirtual) |
4236 | /* Now just return the pointer. */ |
4237 | return e3; |
4238 | |
4239 | /* Next extract the vtable pointer from the object. */ |
4240 | vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node), |
4241 | instance_ptr); |
4242 | vtbl = cp_build_fold_indirect_ref (pointer: vtbl); |
4243 | if (vtbl == error_mark_node) |
4244 | return error_mark_node; |
4245 | |
4246 | /* Finally, extract the function pointer from the vtable. */ |
4247 | e2 = fold_build_pointer_plus_loc (loc: input_location, ptr: vtbl, off: idx); |
4248 | e2 = cp_build_fold_indirect_ref (pointer: e2); |
4249 | if (e2 == error_mark_node) |
4250 | return error_mark_node; |
4251 | TREE_CONSTANT (e2) = 1; |
4252 | |
4253 | /* When using function descriptors, the address of the |
4254 | vtable entry is treated as a function pointer. */ |
4255 | if (TARGET_VTABLE_USES_DESCRIPTORS) |
4256 | e2 = build1 (NOP_EXPR, TREE_TYPE (e2), |
4257 | cp_build_addr_expr (e2, complain)); |
4258 | |
4259 | e2 = fold_convert (TREE_TYPE (e3), e2); |
4260 | e1 = build_conditional_expr (input_location, e1, e2, e3, complain); |
4261 | if (e1 == error_mark_node) |
4262 | return error_mark_node; |
4263 | |
4264 | /* Make sure this doesn't get evaluated first inside one of the |
4265 | branches of the COND_EXPR. */ |
4266 | if (instance_save_expr) |
4267 | e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1), |
4268 | instance_save_expr, e1); |
4269 | |
4270 | function = e1; |
4271 | } |
4272 | return function; |
4273 | } |
4274 | |
4275 | /* Used by the C-common bits. */ |
4276 | tree |
4277 | build_function_call (location_t /*loc*/, |
4278 | tree function, tree params) |
4279 | { |
4280 | return cp_build_function_call (function, params, tf_warning_or_error); |
4281 | } |
4282 | |
4283 | /* Used by the C-common bits. */ |
4284 | tree |
4285 | build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/, |
4286 | tree function, vec<tree, va_gc> *params, |
4287 | vec<tree, va_gc> * /*origtypes*/, tree orig_function) |
4288 | { |
4289 | vec<tree, va_gc> *orig_params = params; |
4290 | tree ret = cp_build_function_call_vec (function, ¶ms, |
4291 | tf_warning_or_error, orig_function); |
4292 | |
4293 | /* cp_build_function_call_vec can reallocate PARAMS by adding |
4294 | default arguments. That should never happen here. Verify |
4295 | that. */ |
4296 | gcc_assert (params == orig_params); |
4297 | |
4298 | return ret; |
4299 | } |
4300 | |
4301 | /* Build a function call using a tree list of arguments. */ |
4302 | |
4303 | static tree |
4304 | cp_build_function_call (tree function, tree params, tsubst_flags_t complain) |
4305 | { |
4306 | tree ret; |
4307 | |
4308 | releasing_vec vec; |
4309 | for (; params != NULL_TREE; params = TREE_CHAIN (params)) |
4310 | vec_safe_push (r&: vec, TREE_VALUE (params)); |
4311 | ret = cp_build_function_call_vec (function, &vec, complain); |
4312 | return ret; |
4313 | } |
4314 | |
4315 | /* Build a function call using varargs. */ |
4316 | |
4317 | tree |
4318 | cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...) |
4319 | { |
4320 | va_list args; |
4321 | tree ret, t; |
4322 | |
4323 | releasing_vec vec; |
4324 | va_start (args, complain); |
4325 | for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree)) |
4326 | vec_safe_push (r&: vec, t); |
4327 | va_end (args); |
4328 | ret = cp_build_function_call_vec (function, &vec, complain); |
4329 | return ret; |
4330 | } |
4331 | |
4332 | /* Build a function call using a vector of arguments. |
4333 | If FUNCTION is the result of resolving an overloaded target built-in, |
4334 | ORIG_FNDECL is the original function decl, otherwise it is null. |
4335 | PARAMS may be NULL if there are no parameters. This changes the |
4336 | contents of PARAMS. */ |
4337 | |
4338 | tree |
4339 | cp_build_function_call_vec (tree function, vec<tree, va_gc> **params, |
4340 | tsubst_flags_t complain, tree orig_fndecl) |
4341 | { |
4342 | tree fntype, fndecl; |
4343 | int is_method; |
4344 | tree original = function; |
4345 | int nargs; |
4346 | tree *argarray; |
4347 | tree parm_types; |
4348 | vec<tree, va_gc> *allocated = NULL; |
4349 | tree ret; |
4350 | |
4351 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
4352 | expressions, like those used for ObjC messenger dispatches. */ |
4353 | if (params != NULL && !vec_safe_is_empty (v: *params)) |
4354 | function = objc_rewrite_function_call (function, (**params)[0]); |
4355 | |
4356 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
4357 | Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */ |
4358 | if (TREE_CODE (function) == NOP_EXPR |
4359 | && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0))) |
4360 | function = TREE_OPERAND (function, 0); |
4361 | |
4362 | if (TREE_CODE (function) == FUNCTION_DECL) |
4363 | { |
4364 | if (!mark_used (function, complain)) |
4365 | return error_mark_node; |
4366 | fndecl = function; |
4367 | |
4368 | /* Convert anything with function type to a pointer-to-function. */ |
4369 | if (DECL_MAIN_P (function)) |
4370 | { |
4371 | if (complain & tf_error) |
4372 | pedwarn (input_location, OPT_Wpedantic, |
4373 | "ISO C++ forbids calling %<::main%> from within program" ); |
4374 | else |
4375 | return error_mark_node; |
4376 | } |
4377 | function = build_addr_func (function, complain); |
4378 | } |
4379 | else |
4380 | { |
4381 | fndecl = NULL_TREE; |
4382 | |
4383 | function = build_addr_func (function, complain); |
4384 | } |
4385 | |
4386 | if (function == error_mark_node) |
4387 | return error_mark_node; |
4388 | |
4389 | fntype = TREE_TYPE (function); |
4390 | |
4391 | if (TYPE_PTRMEMFUNC_P (fntype)) |
4392 | { |
4393 | if (complain & tf_error) |
4394 | error ("must use %<.*%> or %<->*%> to call pointer-to-member " |
4395 | "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>" , |
4396 | original, original); |
4397 | return error_mark_node; |
4398 | } |
4399 | |
4400 | is_method = (TYPE_PTR_P (fntype) |
4401 | && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE); |
4402 | |
4403 | if (!(TYPE_PTRFN_P (fntype) |
4404 | || is_method |
4405 | || TREE_CODE (function) == TEMPLATE_ID_EXPR)) |
4406 | { |
4407 | if (complain & tf_error) |
4408 | { |
4409 | if (!flag_diagnostics_show_caret) |
4410 | error_at (input_location, |
4411 | "%qE cannot be used as a function" , original); |
4412 | else if (DECL_P (original)) |
4413 | error_at (input_location, |
4414 | "%qD cannot be used as a function" , original); |
4415 | else |
4416 | error_at (input_location, |
4417 | "expression cannot be used as a function" ); |
4418 | } |
4419 | |
4420 | return error_mark_node; |
4421 | } |
4422 | |
4423 | /* fntype now gets the type of function pointed to. */ |
4424 | fntype = TREE_TYPE (fntype); |
4425 | parm_types = TYPE_ARG_TYPES (fntype); |
4426 | |
4427 | if (params == NULL) |
4428 | { |
4429 | allocated = make_tree_vector (); |
4430 | params = &allocated; |
4431 | } |
4432 | |
4433 | nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL, |
4434 | complain); |
4435 | if (nargs < 0) |
4436 | return error_mark_node; |
4437 | |
4438 | argarray = (*params)->address (); |
4439 | |
4440 | /* Check for errors in format strings and inappropriately |
4441 | null parameters. */ |
4442 | bool warned_p = check_function_arguments (loc: input_location, fndecl, fntype, |
4443 | nargs, argarray, NULL); |
4444 | |
4445 | ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl); |
4446 | |
4447 | if (warned_p) |
4448 | { |
4449 | tree c = extract_call_expr (ret); |
4450 | if (TREE_CODE (c) == CALL_EXPR) |
4451 | suppress_warning (c, OPT_Wnonnull); |
4452 | } |
4453 | |
4454 | if (allocated != NULL) |
4455 | release_tree_vector (allocated); |
4456 | |
4457 | return ret; |
4458 | } |
4459 | |
4460 | /* Subroutine of convert_arguments. |
4461 | Print an error message about a wrong number of arguments. */ |
4462 | |
4463 | static void |
4464 | error_args_num (location_t loc, tree fndecl, bool too_many_p) |
4465 | { |
4466 | if (fndecl) |
4467 | { |
4468 | if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE) |
4469 | { |
4470 | if (DECL_NAME (fndecl) == NULL_TREE |
4471 | || (DECL_NAME (fndecl) |
4472 | == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl))))) |
4473 | error_at (loc, |
4474 | too_many_p |
4475 | ? G_("too many arguments to constructor %q#D" ) |
4476 | : G_("too few arguments to constructor %q#D" ), |
4477 | fndecl); |
4478 | else |
4479 | error_at (loc, |
4480 | too_many_p |
4481 | ? G_("too many arguments to member function %q#D" ) |
4482 | : G_("too few arguments to member function %q#D" ), |
4483 | fndecl); |
4484 | } |
4485 | else |
4486 | error_at (loc, |
4487 | too_many_p |
4488 | ? G_("too many arguments to function %q#D" ) |
4489 | : G_("too few arguments to function %q#D" ), |
4490 | fndecl); |
4491 | if (!DECL_IS_UNDECLARED_BUILTIN (fndecl)) |
4492 | inform (DECL_SOURCE_LOCATION (fndecl), "declared here" ); |
4493 | } |
4494 | else |
4495 | { |
4496 | if (c_dialect_objc () && objc_message_selector ()) |
4497 | error_at (loc, |
4498 | too_many_p |
4499 | ? G_("too many arguments to method %q#D" ) |
4500 | : G_("too few arguments to method %q#D" ), |
4501 | objc_message_selector ()); |
4502 | else |
4503 | error_at (loc, too_many_p ? G_("too many arguments to function" ) |
4504 | : G_("too few arguments to function" )); |
4505 | } |
4506 | } |
4507 | |
4508 | /* Convert the actual parameter expressions in the list VALUES to the |
4509 | types in the list TYPELIST. The converted expressions are stored |
4510 | back in the VALUES vector. |
4511 | If parmdecls is exhausted, or when an element has NULL as its type, |
4512 | perform the default conversions. |
4513 | |
4514 | NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. |
4515 | |
4516 | This is also where warnings about wrong number of args are generated. |
4517 | |
4518 | Returns the actual number of arguments processed (which might be less |
4519 | than the length of the vector), or -1 on error. |
4520 | |
4521 | In C++, unspecified trailing parameters can be filled in with their |
4522 | default arguments, if such were specified. Do so here. */ |
4523 | |
4524 | static int |
4525 | convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl, |
4526 | int flags, tsubst_flags_t complain) |
4527 | { |
4528 | tree typetail; |
4529 | unsigned int i; |
4530 | |
4531 | /* Argument passing is always copy-initialization. */ |
4532 | flags |= LOOKUP_ONLYCONVERTING; |
4533 | |
4534 | for (i = 0, typetail = typelist; |
4535 | i < vec_safe_length (v: *values); |
4536 | i++) |
4537 | { |
4538 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
4539 | tree val = (**values)[i]; |
4540 | |
4541 | if (val == error_mark_node || type == error_mark_node) |
4542 | return -1; |
4543 | |
4544 | if (type == void_type_node) |
4545 | { |
4546 | if (complain & tf_error) |
4547 | { |
4548 | error_args_num (loc: input_location, fndecl, /*too_many_p=*/true); |
4549 | return i; |
4550 | } |
4551 | else |
4552 | return -1; |
4553 | } |
4554 | |
4555 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
4556 | Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */ |
4557 | if (TREE_CODE (val) == NOP_EXPR |
4558 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)) |
4559 | && (type == 0 || !TYPE_REF_P (type))) |
4560 | val = TREE_OPERAND (val, 0); |
4561 | |
4562 | if (type == 0 || !TYPE_REF_P (type)) |
4563 | { |
4564 | if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE |
4565 | || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val))) |
4566 | val = decay_conversion (exp: val, complain); |
4567 | } |
4568 | |
4569 | if (val == error_mark_node) |
4570 | return -1; |
4571 | |
4572 | if (type != 0) |
4573 | { |
4574 | /* Formal parm type is specified by a function prototype. */ |
4575 | tree parmval; |
4576 | |
4577 | if (!COMPLETE_TYPE_P (complete_type (type))) |
4578 | { |
4579 | if (complain & tf_error) |
4580 | { |
4581 | location_t loc = EXPR_LOC_OR_LOC (val, input_location); |
4582 | if (fndecl) |
4583 | { |
4584 | auto_diagnostic_group d; |
4585 | error_at (loc, |
4586 | "parameter %P of %qD has incomplete type %qT" , |
4587 | i, fndecl, type); |
4588 | inform (get_fndecl_argument_location (fndecl, i), |
4589 | " declared here" ); |
4590 | } |
4591 | else |
4592 | error_at (loc, "parameter %P has incomplete type %qT" , i, |
4593 | type); |
4594 | } |
4595 | parmval = error_mark_node; |
4596 | } |
4597 | else |
4598 | { |
4599 | parmval = convert_for_initialization |
4600 | (NULL_TREE, type, val, flags, |
4601 | ICR_ARGPASS, fndecl, i, complain); |
4602 | parmval = convert_for_arg_passing (type, parmval, complain); |
4603 | } |
4604 | |
4605 | if (parmval == error_mark_node) |
4606 | return -1; |
4607 | |
4608 | (**values)[i] = parmval; |
4609 | } |
4610 | else |
4611 | { |
4612 | int magic = fndecl ? magic_varargs_p (fndecl) : 0; |
4613 | if (magic) |
4614 | { |
4615 | /* Don't truncate excess precision to the semantic type. */ |
4616 | if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR) |
4617 | val = TREE_OPERAND (val, 0); |
4618 | /* Don't do ellipsis conversion for __built_in_constant_p |
4619 | as this will result in spurious errors for non-trivial |
4620 | types. */ |
4621 | val = require_complete_type (value: val, complain); |
4622 | } |
4623 | else |
4624 | val = convert_arg_to_ellipsis (val, complain); |
4625 | |
4626 | (**values)[i] = val; |
4627 | } |
4628 | |
4629 | if (typetail) |
4630 | typetail = TREE_CHAIN (typetail); |
4631 | } |
4632 | |
4633 | if (typetail != 0 && typetail != void_list_node) |
4634 | { |
4635 | /* See if there are default arguments that can be used. Because |
4636 | we hold default arguments in the FUNCTION_TYPE (which is so |
4637 | wrong), we can see default parameters here from deduced |
4638 | contexts (and via typeof) for indirect function calls. |
4639 | Fortunately we know whether we have a function decl to |
4640 | provide default arguments in a language conformant |
4641 | manner. */ |
4642 | if (fndecl && TREE_PURPOSE (typetail) |
4643 | && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE) |
4644 | { |
4645 | for (; typetail != void_list_node; ++i) |
4646 | { |
4647 | /* After DR777, with explicit template args we can end up with a |
4648 | default argument followed by no default argument. */ |
4649 | if (!TREE_PURPOSE (typetail)) |
4650 | break; |
4651 | tree parmval |
4652 | = convert_default_arg (TREE_VALUE (typetail), |
4653 | TREE_PURPOSE (typetail), |
4654 | fndecl, i, complain); |
4655 | |
4656 | if (parmval == error_mark_node) |
4657 | return -1; |
4658 | |
4659 | vec_safe_push (v&: *values, obj: parmval); |
4660 | typetail = TREE_CHAIN (typetail); |
4661 | /* ends with `...'. */ |
4662 | if (typetail == NULL_TREE) |
4663 | break; |
4664 | } |
4665 | } |
4666 | |
4667 | if (typetail && typetail != void_list_node) |
4668 | { |
4669 | if (complain & tf_error) |
4670 | error_args_num (loc: input_location, fndecl, /*too_many_p=*/false); |
4671 | return -1; |
4672 | } |
4673 | } |
4674 | |
4675 | return (int) i; |
4676 | } |
4677 | |
4678 | /* Build a binary-operation expression, after performing default |
4679 | conversions on the operands. CODE is the kind of expression to |
4680 | build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE |
4681 | are the tree codes which correspond to ARG1 and ARG2 when issuing |
4682 | warnings about possibly misplaced parentheses. They may differ |
4683 | from the TREE_CODE of ARG1 and ARG2 if the parser has done constant |
4684 | folding (e.g., if the parser sees "a | 1 + 1", it may call this |
4685 | routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR). |
4686 | To avoid issuing any parentheses warnings, pass ARG1_CODE and/or |
4687 | ARG2_CODE as ERROR_MARK. */ |
4688 | |
4689 | tree |
4690 | build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1, |
4691 | enum tree_code arg1_code, tree arg2, |
4692 | enum tree_code arg2_code, tree lookups, |
4693 | tree *overload_p, tsubst_flags_t complain) |
4694 | { |
4695 | tree orig_arg1; |
4696 | tree orig_arg2; |
4697 | tree expr; |
4698 | tree overload = NULL_TREE; |
4699 | |
4700 | orig_arg1 = arg1; |
4701 | orig_arg2 = arg2; |
4702 | |
4703 | if (processing_template_decl) |
4704 | { |
4705 | if (type_dependent_expression_p (arg1) |
4706 | || type_dependent_expression_p (arg2)) |
4707 | { |
4708 | expr = build_min_nt_loc (loc, code, arg1, arg2); |
4709 | TREE_TYPE (expr) |
4710 | = build_dependent_operator_type (lookups, code, is_assign: false); |
4711 | return expr; |
4712 | } |
4713 | } |
4714 | |
4715 | if (code == DOTSTAR_EXPR) |
4716 | expr = build_m_component_ref (arg1, arg2, complain); |
4717 | else |
4718 | expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE, |
4719 | lookups, &overload, complain); |
4720 | |
4721 | if (overload_p != NULL) |
4722 | *overload_p = overload; |
4723 | |
4724 | /* Check for cases such as x+y<<z which users are likely to |
4725 | misinterpret. But don't warn about obj << x + y, since that is a |
4726 | common idiom for I/O. */ |
4727 | if (warn_parentheses |
4728 | && (complain & tf_warning) |
4729 | && !processing_template_decl |
4730 | && !error_operand_p (t: arg1) |
4731 | && !error_operand_p (t: arg2) |
4732 | && (code != LSHIFT_EXPR |
4733 | || !CLASS_TYPE_P (TREE_TYPE (arg1)))) |
4734 | warn_about_parentheses (loc, code, arg1_code, orig_arg1, |
4735 | arg2_code, orig_arg2); |
4736 | |
4737 | if (processing_template_decl && expr != error_mark_node) |
4738 | { |
4739 | if (overload != NULL_TREE) |
4740 | return (build_min_non_dep_op_overload |
4741 | (code, expr, overload, orig_arg1, orig_arg2)); |
4742 | |
4743 | return build_min_non_dep (code, expr, orig_arg1, orig_arg2); |
4744 | } |
4745 | |
4746 | return expr; |
4747 | } |
4748 | |
4749 | /* Build and return an ARRAY_REF expression. */ |
4750 | |
4751 | tree |
4752 | build_x_array_ref (location_t loc, tree arg1, tree arg2, |
4753 | tsubst_flags_t complain) |
4754 | { |
4755 | tree orig_arg1 = arg1; |
4756 | tree orig_arg2 = arg2; |
4757 | tree expr; |
4758 | tree overload = NULL_TREE; |
4759 | |
4760 | if (processing_template_decl) |
4761 | { |
4762 | if (type_dependent_expression_p (arg1) |
4763 | || type_dependent_expression_p (arg2)) |
4764 | return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2, |
4765 | NULL_TREE, NULL_TREE); |
4766 | } |
4767 | |
4768 | expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, |
4769 | NULL_TREE, NULL_TREE, &overload, complain); |
4770 | |
4771 | if (processing_template_decl && expr != error_mark_node) |
4772 | { |
4773 | if (overload != NULL_TREE) |
4774 | return (build_min_non_dep_op_overload |
4775 | (ARRAY_REF, expr, overload, orig_arg1, orig_arg2)); |
4776 | |
4777 | return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2, |
4778 | NULL_TREE, NULL_TREE); |
4779 | } |
4780 | return expr; |
4781 | } |
4782 | |
4783 | /* Return whether OP is an expression of enum type cast to integer |
4784 | type. In C++ even unsigned enum types are cast to signed integer |
4785 | types. We do not want to issue warnings about comparisons between |
4786 | signed and unsigned types when one of the types is an enum type. |
4787 | Those warnings are always false positives in practice. */ |
4788 | |
4789 | static bool |
4790 | enum_cast_to_int (tree op) |
4791 | { |
4792 | if (CONVERT_EXPR_P (op) |
4793 | && TREE_TYPE (op) == integer_type_node |
4794 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE |
4795 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0)))) |
4796 | return true; |
4797 | |
4798 | /* The cast may have been pushed into a COND_EXPR. */ |
4799 | if (TREE_CODE (op) == COND_EXPR) |
4800 | return (enum_cast_to_int (TREE_OPERAND (op, 1)) |
4801 | || enum_cast_to_int (TREE_OPERAND (op, 2))); |
4802 | |
4803 | return false; |
4804 | } |
4805 | |
4806 | /* For the c-common bits. */ |
4807 | tree |
4808 | build_binary_op (location_t location, enum tree_code code, tree op0, tree op1, |
4809 | bool /*convert_p*/) |
4810 | { |
4811 | return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error); |
4812 | } |
4813 | |
4814 | /* Build a vector comparison of ARG0 and ARG1 using CODE opcode |
4815 | into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */ |
4816 | |
4817 | static tree |
4818 | build_vec_cmp (tree_code code, tree type, |
4819 | tree arg0, tree arg1) |
4820 | { |
4821 | tree zero_vec = build_zero_cst (type); |
4822 | tree minus_one_vec = build_minus_one_cst (type); |
4823 | tree cmp_type = truth_type_for (TREE_TYPE (arg0)); |
4824 | tree cmp = build2 (code, cmp_type, arg0, arg1); |
4825 | return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec); |
4826 | } |
4827 | |
4828 | /* Possibly warn about an address never being NULL. */ |
4829 | |
4830 | static void |
4831 | warn_for_null_address (location_t location, tree op, tsubst_flags_t complain) |
4832 | { |
4833 | /* Prevent warnings issued for macro expansion. */ |
4834 | if (!warn_address |
4835 | || (complain & tf_warning) == 0 |
4836 | || c_inhibit_evaluation_warnings != 0 |
4837 | || from_macro_expansion_at (loc: location) |
4838 | || warning_suppressed_p (op, OPT_Waddress)) |
4839 | return; |
4840 | |
4841 | tree cop = fold_for_warn (op); |
4842 | |
4843 | if (TREE_CODE (cop) == NON_LVALUE_EXPR) |
4844 | /* Unwrap the expression for C++ 98. */ |
4845 | cop = TREE_OPERAND (cop, 0); |
4846 | |
4847 | if (TREE_CODE (cop) == PTRMEM_CST) |
4848 | { |
4849 | /* The address of a nonstatic data member is never null. */ |
4850 | warning_at (location, OPT_Waddress, |
4851 | "the address %qE will never be NULL" , |
4852 | cop); |
4853 | return; |
4854 | } |
4855 | |
4856 | if (TREE_CODE (cop) == NOP_EXPR) |
4857 | { |
4858 | /* Allow casts to intptr_t to suppress the warning. */ |
4859 | tree type = TREE_TYPE (cop); |
4860 | if (TREE_CODE (type) == INTEGER_TYPE) |
4861 | return; |
4862 | |
4863 | STRIP_NOPS (cop); |
4864 | } |
4865 | |
4866 | bool warned = false; |
4867 | if (TREE_CODE (cop) == ADDR_EXPR) |
4868 | { |
4869 | cop = TREE_OPERAND (cop, 0); |
4870 | |
4871 | /* Set to true in the loop below if OP dereferences its operand. |
4872 | In such a case the ultimate target need not be a decl for |
4873 | the null [in]equality test to be necessarily constant. */ |
4874 | bool deref = false; |
4875 | |
4876 | /* Get the outermost array or object, or member. */ |
4877 | while (handled_component_p (t: cop)) |
4878 | { |
4879 | if (TREE_CODE (cop) == COMPONENT_REF) |
4880 | { |
4881 | /* Get the member (its address is never null). */ |
4882 | cop = TREE_OPERAND (cop, 1); |
4883 | break; |
4884 | } |
4885 | |
4886 | /* Get the outer array/object to refer to in the warning. */ |
4887 | cop = TREE_OPERAND (cop, 0); |
4888 | deref = true; |
4889 | } |
4890 | |
4891 | if ((!deref && !decl_with_nonnull_addr_p (cop)) |
4892 | || from_macro_expansion_at (loc: location) |
4893 | || warning_suppressed_p (cop, OPT_Waddress)) |
4894 | return; |
4895 | |
4896 | warned = warning_at (location, OPT_Waddress, |
4897 | "the address of %qD will never be NULL" , cop); |
4898 | op = cop; |
4899 | } |
4900 | else if (TREE_CODE (cop) == POINTER_PLUS_EXPR) |
4901 | { |
4902 | /* Adding zero to the null pointer is well-defined in C++. When |
4903 | the offset is unknown (i.e., not a constant) warn anyway since |
4904 | it's less likely that the pointer operand is null than not. */ |
4905 | tree off = TREE_OPERAND (cop, 1); |
4906 | if (!integer_zerop (off) |
4907 | && !warning_suppressed_p (cop, OPT_Waddress)) |
4908 | { |
4909 | tree base = TREE_OPERAND (cop, 0); |
4910 | STRIP_NOPS (base); |
4911 | if (TYPE_REF_P (TREE_TYPE (base))) |
4912 | warning_at (location, OPT_Waddress, "the compiler can assume that " |
4913 | "the address of %qE will never be NULL" , base); |
4914 | else |
4915 | warning_at (location, OPT_Waddress, "comparing the result of " |
4916 | "pointer addition %qE and NULL" , cop); |
4917 | } |
4918 | return; |
4919 | } |
4920 | else if (CONVERT_EXPR_P (op) |
4921 | && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0)))) |
4922 | { |
4923 | STRIP_NOPS (op); |
4924 | |
4925 | if (TREE_CODE (op) == COMPONENT_REF) |
4926 | op = TREE_OPERAND (op, 1); |
4927 | |
4928 | if (DECL_P (op)) |
4929 | warned = warning_at (location, OPT_Waddress, |
4930 | "the compiler can assume that the address of " |
4931 | "%qD will never be NULL" , op); |
4932 | } |
4933 | |
4934 | if (warned && DECL_P (op)) |
4935 | inform (DECL_SOURCE_LOCATION (op), "%qD declared here" , op); |
4936 | } |
4937 | |
4938 | /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and |
4939 | the other operand is of a different enumeration type or a floating-point |
4940 | type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the |
4941 | code of the binary operation, TYPE0 and TYPE1 are the types of the operands, |
4942 | and LOC is the location for the whole binary expression. |
4943 | TODO: Consider combining this with -Wenum-compare in build_new_op_1. */ |
4944 | |
4945 | static void |
4946 | do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0, |
4947 | tree type1) |
4948 | { |
4949 | if (TREE_CODE (type0) == ENUMERAL_TYPE |
4950 | && TREE_CODE (type1) == ENUMERAL_TYPE |
4951 | && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1)) |
4952 | { |
4953 | /* In C++20, -Wdeprecated-enum-enum-conversion is on by default. |
4954 | Otherwise, warn if -Wenum-conversion is on. */ |
4955 | enum opt_code opt; |
4956 | if (warn_deprecated_enum_enum_conv) |
4957 | opt = OPT_Wdeprecated_enum_enum_conversion; |
4958 | else if (warn_enum_conversion) |
4959 | opt = OPT_Wenum_conversion; |
4960 | else |
4961 | return; |
4962 | |
4963 | switch (code) |
4964 | { |
4965 | case GT_EXPR: |
4966 | case LT_EXPR: |
4967 | case GE_EXPR: |
4968 | case LE_EXPR: |
4969 | case EQ_EXPR: |
4970 | case NE_EXPR: |
4971 | /* Comparisons are handled by -Wenum-compare. */ |
4972 | return; |
4973 | case SPACESHIP_EXPR: |
4974 | /* This is invalid, don't warn. */ |
4975 | return; |
4976 | case BIT_AND_EXPR: |
4977 | case BIT_IOR_EXPR: |
4978 | case BIT_XOR_EXPR: |
4979 | warning_at (loc, opt, "bitwise operation between different " |
4980 | "enumeration types %qT and %qT is deprecated" , |
4981 | type0, type1); |
4982 | return; |
4983 | default: |
4984 | warning_at (loc, opt, "arithmetic between different enumeration " |
4985 | "types %qT and %qT is deprecated" , type0, type1); |
4986 | return; |
4987 | } |
4988 | } |
4989 | else if ((TREE_CODE (type0) == ENUMERAL_TYPE |
4990 | && SCALAR_FLOAT_TYPE_P (type1)) |
4991 | || (SCALAR_FLOAT_TYPE_P (type0) |
4992 | && TREE_CODE (type1) == ENUMERAL_TYPE)) |
4993 | { |
4994 | const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE; |
4995 | /* In C++20, -Wdeprecated-enum-float-conversion is on by default. |
4996 | Otherwise, warn if -Wenum-conversion is on. */ |
4997 | enum opt_code opt; |
4998 | if (warn_deprecated_enum_float_conv) |
4999 | opt = OPT_Wdeprecated_enum_float_conversion; |
5000 | else if (warn_enum_conversion) |
5001 | opt = OPT_Wenum_conversion; |
5002 | else |
5003 | return; |
5004 | |
5005 | switch (code) |
5006 | { |
5007 | case GT_EXPR: |
5008 | case LT_EXPR: |
5009 | case GE_EXPR: |
5010 | case LE_EXPR: |
5011 | case EQ_EXPR: |
5012 | case NE_EXPR: |
5013 | if (enum_first_p) |
5014 | warning_at (loc, opt, "comparison of enumeration type %qT with " |
5015 | "floating-point type %qT is deprecated" , |
5016 | type0, type1); |
5017 | else |
5018 | warning_at (loc, opt, "comparison of floating-point type %qT " |
5019 | "with enumeration type %qT is deprecated" , |
5020 | type0, type1); |
5021 | return; |
5022 | case SPACESHIP_EXPR: |
5023 | /* This is invalid, don't warn. */ |
5024 | return; |
5025 | default: |
5026 | if (enum_first_p) |
5027 | warning_at (loc, opt, "arithmetic between enumeration type %qT " |
5028 | "and floating-point type %qT is deprecated" , |
5029 | type0, type1); |
5030 | else |
5031 | warning_at (loc, opt, "arithmetic between floating-point type %qT " |
5032 | "and enumeration type %qT is deprecated" , |
5033 | type0, type1); |
5034 | return; |
5035 | } |
5036 | } |
5037 | } |
5038 | |
5039 | /* Build a binary-operation expression without default conversions. |
5040 | CODE is the kind of expression to build. |
5041 | LOCATION is the location_t of the operator in the source code. |
5042 | This function differs from `build' in several ways: |
5043 | the data type of the result is computed and recorded in it, |
5044 | warnings are generated if arg data types are invalid, |
5045 | special handling for addition and subtraction of pointers is known, |
5046 | and some optimization is done (operations on narrow ints |
5047 | are done in the narrower type when that gives the same result). |
5048 | Constant folding is also done before the result is returned. |
5049 | |
5050 | Note that the operands will never have enumeral types |
5051 | because either they have just had the default conversions performed |
5052 | or they have both just been converted to some other type in which |
5053 | the arithmetic is to be done. |
5054 | |
5055 | C++: must do special pointer arithmetic when implementing |
5056 | multiple inheritance, and deal with pointer to member functions. */ |
5057 | |
5058 | tree |
5059 | cp_build_binary_op (const op_location_t &location, |
5060 | enum tree_code code, tree orig_op0, tree orig_op1, |
5061 | tsubst_flags_t complain) |
5062 | { |
5063 | tree op0, op1; |
5064 | enum tree_code code0, code1; |
5065 | tree type0, type1, orig_type0, orig_type1; |
5066 | const char *invalid_op_diag; |
5067 | |
5068 | /* Expression code to give to the expression when it is built. |
5069 | Normally this is CODE, which is what the caller asked for, |
5070 | but in some special cases we change it. */ |
5071 | enum tree_code resultcode = code; |
5072 | |
5073 | /* Data type in which the computation is to be performed. |
5074 | In the simplest cases this is the common type of the arguments. */ |
5075 | tree result_type = NULL_TREE; |
5076 | |
5077 | /* When the computation is in excess precision, the type of the |
5078 | final EXCESS_PRECISION_EXPR. */ |
5079 | tree semantic_result_type = NULL; |
5080 | |
5081 | /* Nonzero means operands have already been type-converted |
5082 | in whatever way is necessary. |
5083 | Zero means they need to be converted to RESULT_TYPE. */ |
5084 | int converted = 0; |
5085 | |
5086 | /* Nonzero means create the expression with this type, rather than |
5087 | RESULT_TYPE. */ |
5088 | tree build_type = 0; |
5089 | |
5090 | /* Nonzero means after finally constructing the expression |
5091 | convert it to this type. */ |
5092 | tree final_type = 0; |
5093 | |
5094 | tree result; |
5095 | |
5096 | /* Nonzero if this is an operation like MIN or MAX which can |
5097 | safely be computed in short if both args are promoted shorts. |
5098 | Also implies COMMON. |
5099 | -1 indicates a bitwise operation; this makes a difference |
5100 | in the exact conditions for when it is safe to do the operation |
5101 | in a narrower mode. */ |
5102 | int shorten = 0; |
5103 | |
5104 | /* Nonzero if this is a comparison operation; |
5105 | if both args are promoted shorts, compare the original shorts. |
5106 | Also implies COMMON. */ |
5107 | int short_compare = 0; |
5108 | |
5109 | /* Nonzero if this is a right-shift operation, which can be computed on the |
5110 | original short and then promoted if the operand is a promoted short. */ |
5111 | int short_shift = 0; |
5112 | |
5113 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ |
5114 | int common = 0; |
5115 | |
5116 | /* True if both operands have arithmetic type. */ |
5117 | bool arithmetic_types_p; |
5118 | |
5119 | /* Remember whether we're doing / or %. */ |
5120 | bool doing_div_or_mod = false; |
5121 | |
5122 | /* Remember whether we're doing << or >>. */ |
5123 | bool doing_shift = false; |
5124 | |
5125 | /* Tree holding instrumentation expression. */ |
5126 | tree instrument_expr = NULL_TREE; |
5127 | |
5128 | /* True means this is an arithmetic operation that may need excess |
5129 | precision. */ |
5130 | bool may_need_excess_precision; |
5131 | |
5132 | /* Apply default conversions. */ |
5133 | op0 = resolve_nondeduced_context (orig_op0, complain); |
5134 | op1 = resolve_nondeduced_context (orig_op1, complain); |
5135 | |
5136 | if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR |
5137 | || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR |
5138 | || code == TRUTH_XOR_EXPR) |
5139 | { |
5140 | if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0))) |
5141 | op0 = decay_conversion (exp: op0, complain); |
5142 | if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1))) |
5143 | op1 = decay_conversion (exp: op1, complain); |
5144 | } |
5145 | else |
5146 | { |
5147 | if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0))) |
5148 | op0 = cp_default_conversion (exp: op0, complain); |
5149 | if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1))) |
5150 | op1 = cp_default_conversion (exp: op1, complain); |
5151 | } |
5152 | |
5153 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
5154 | STRIP_TYPE_NOPS (op0); |
5155 | STRIP_TYPE_NOPS (op1); |
5156 | |
5157 | /* DTRT if one side is an overloaded function, but complain about it. */ |
5158 | if (type_unknown_p (expr: op0)) |
5159 | { |
5160 | tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none); |
5161 | if (t != error_mark_node) |
5162 | { |
5163 | if (complain & tf_error) |
5164 | permerror (location, |
5165 | "assuming cast to type %qT from overloaded function" , |
5166 | TREE_TYPE (t)); |
5167 | op0 = t; |
5168 | } |
5169 | } |
5170 | if (type_unknown_p (expr: op1)) |
5171 | { |
5172 | tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none); |
5173 | if (t != error_mark_node) |
5174 | { |
5175 | if (complain & tf_error) |
5176 | permerror (location, |
5177 | "assuming cast to type %qT from overloaded function" , |
5178 | TREE_TYPE (t)); |
5179 | op1 = t; |
5180 | } |
5181 | } |
5182 | |
5183 | orig_type0 = type0 = TREE_TYPE (op0); |
5184 | orig_type1 = type1 = TREE_TYPE (op1); |
5185 | tree non_ep_op0 = op0; |
5186 | tree non_ep_op1 = op1; |
5187 | |
5188 | /* The expression codes of the data types of the arguments tell us |
5189 | whether the arguments are integers, floating, pointers, etc. */ |
5190 | code0 = TREE_CODE (type0); |
5191 | code1 = TREE_CODE (type1); |
5192 | |
5193 | /* If an error was already reported for one of the arguments, |
5194 | avoid reporting another error. */ |
5195 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
5196 | return error_mark_node; |
5197 | |
5198 | if ((invalid_op_diag |
5199 | = targetm.invalid_binary_op (code, type0, type1))) |
5200 | { |
5201 | if (complain & tf_error) |
5202 | { |
5203 | if (code0 == REAL_TYPE |
5204 | && code1 == REAL_TYPE |
5205 | && (extended_float_type_p (type: type0) |
5206 | || extended_float_type_p (type: type1)) |
5207 | && cp_compare_floating_point_conversion_ranks (t1: type0, |
5208 | t2: type1) == 3) |
5209 | { |
5210 | rich_location richloc (line_table, location); |
5211 | binary_op_error (&richloc, code, type0, type1); |
5212 | } |
5213 | else |
5214 | error (invalid_op_diag); |
5215 | } |
5216 | return error_mark_node; |
5217 | } |
5218 | |
5219 | switch (code) |
5220 | { |
5221 | case PLUS_EXPR: |
5222 | case MINUS_EXPR: |
5223 | case MULT_EXPR: |
5224 | case TRUNC_DIV_EXPR: |
5225 | case CEIL_DIV_EXPR: |
5226 | case FLOOR_DIV_EXPR: |
5227 | case ROUND_DIV_EXPR: |
5228 | case EXACT_DIV_EXPR: |
5229 | may_need_excess_precision = true; |
5230 | break; |
5231 | case EQ_EXPR: |
5232 | case NE_EXPR: |
5233 | case LE_EXPR: |
5234 | case GE_EXPR: |
5235 | case LT_EXPR: |
5236 | case GT_EXPR: |
5237 | case SPACESHIP_EXPR: |
5238 | /* Excess precision for implicit conversions of integers to |
5239 | floating point. */ |
5240 | may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0) |
5241 | || ANY_INTEGRAL_TYPE_P (type1)); |
5242 | break; |
5243 | default: |
5244 | may_need_excess_precision = false; |
5245 | break; |
5246 | } |
5247 | if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) |
5248 | { |
5249 | op0 = TREE_OPERAND (op0, 0); |
5250 | type0 = TREE_TYPE (op0); |
5251 | } |
5252 | else if (may_need_excess_precision |
5253 | && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE)) |
5254 | if (tree eptype = excess_precision_type (type0)) |
5255 | { |
5256 | type0 = eptype; |
5257 | op0 = convert (eptype, op0); |
5258 | } |
5259 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) |
5260 | { |
5261 | op1 = TREE_OPERAND (op1, 0); |
5262 | type1 = TREE_TYPE (op1); |
5263 | } |
5264 | else if (may_need_excess_precision |
5265 | && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE)) |
5266 | if (tree eptype = excess_precision_type (type1)) |
5267 | { |
5268 | type1 = eptype; |
5269 | op1 = convert (eptype, op1); |
5270 | } |
5271 | |
5272 | /* Issue warnings about peculiar, but valid, uses of NULL. */ |
5273 | if ((null_node_p (expr: orig_op0) || null_node_p (expr: orig_op1)) |
5274 | /* It's reasonable to use pointer values as operands of && |
5275 | and ||, so NULL is no exception. */ |
5276 | && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR |
5277 | && ( /* Both are NULL (or 0) and the operation was not a |
5278 | comparison or a pointer subtraction. */ |
5279 | (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1) |
5280 | && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR) |
5281 | /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */ |
5282 | || (!null_ptr_cst_p (orig_op0) |
5283 | && !TYPE_PTR_OR_PTRMEM_P (type0)) |
5284 | || (!null_ptr_cst_p (orig_op1) |
5285 | && !TYPE_PTR_OR_PTRMEM_P (type1))) |
5286 | && (complain & tf_warning)) |
5287 | { |
5288 | location_t loc = |
5289 | expansion_point_location_if_in_system_header (input_location); |
5290 | |
5291 | warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic" ); |
5292 | } |
5293 | |
5294 | /* In case when one of the operands of the binary operation is |
5295 | a vector and another is a scalar -- convert scalar to vector. */ |
5296 | if ((gnu_vector_type_p (type: type0) && code1 != VECTOR_TYPE) |
5297 | || (gnu_vector_type_p (type: type1) && code0 != VECTOR_TYPE)) |
5298 | { |
5299 | enum stv_conv convert_flag |
5300 | = scalar_to_vector (loc: location, code, op0: non_ep_op0, op1: non_ep_op1, |
5301 | complain & tf_error); |
5302 | |
5303 | switch (convert_flag) |
5304 | { |
5305 | case stv_error: |
5306 | return error_mark_node; |
5307 | case stv_firstarg: |
5308 | { |
5309 | op0 = convert (TREE_TYPE (type1), op0); |
5310 | op0 = save_expr (op0); |
5311 | op0 = build_vector_from_val (type1, op0); |
5312 | orig_type0 = type0 = TREE_TYPE (op0); |
5313 | code0 = TREE_CODE (type0); |
5314 | converted = 1; |
5315 | break; |
5316 | } |
5317 | case stv_secondarg: |
5318 | { |
5319 | op1 = convert (TREE_TYPE (type0), op1); |
5320 | op1 = save_expr (op1); |
5321 | op1 = build_vector_from_val (type0, op1); |
5322 | orig_type1 = type1 = TREE_TYPE (op1); |
5323 | code1 = TREE_CODE (type1); |
5324 | converted = 1; |
5325 | break; |
5326 | } |
5327 | default: |
5328 | break; |
5329 | } |
5330 | } |
5331 | |
5332 | switch (code) |
5333 | { |
5334 | case MINUS_EXPR: |
5335 | /* Subtraction of two similar pointers. |
5336 | We must subtract them as integers, then divide by object size. */ |
5337 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE |
5338 | && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0), |
5339 | TREE_TYPE (type1))) |
5340 | { |
5341 | result = pointer_diff (location, op0, op1, |
5342 | common_pointer_type (t1: type0, t2: type1), complain, |
5343 | &instrument_expr); |
5344 | if (instrument_expr != NULL) |
5345 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
5346 | instrument_expr, result); |
5347 | |
5348 | return result; |
5349 | } |
5350 | /* In all other cases except pointer - int, the usual arithmetic |
5351 | rules apply. */ |
5352 | else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE)) |
5353 | { |
5354 | common = 1; |
5355 | break; |
5356 | } |
5357 | /* The pointer - int case is just like pointer + int; fall |
5358 | through. */ |
5359 | gcc_fallthrough (); |
5360 | case PLUS_EXPR: |
5361 | if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE) |
5362 | && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE)) |
5363 | { |
5364 | tree ptr_operand; |
5365 | tree int_operand; |
5366 | ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1); |
5367 | int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1); |
5368 | if (processing_template_decl) |
5369 | { |
5370 | result_type = TREE_TYPE (ptr_operand); |
5371 | break; |
5372 | } |
5373 | return cp_pointer_int_sum (location, code, |
5374 | ptr_operand, |
5375 | int_operand, |
5376 | complain); |
5377 | } |
5378 | common = 1; |
5379 | break; |
5380 | |
5381 | case MULT_EXPR: |
5382 | common = 1; |
5383 | break; |
5384 | |
5385 | case TRUNC_DIV_EXPR: |
5386 | case CEIL_DIV_EXPR: |
5387 | case FLOOR_DIV_EXPR: |
5388 | case ROUND_DIV_EXPR: |
5389 | case EXACT_DIV_EXPR: |
5390 | if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR) |
5391 | { |
5392 | tree type0 = TREE_OPERAND (op0, 0); |
5393 | tree type1 = TREE_OPERAND (op1, 0); |
5394 | tree first_arg = tree_strip_any_location_wrapper (exp: type0); |
5395 | if (!TYPE_P (type0)) |
5396 | type0 = TREE_TYPE (type0); |
5397 | if (!TYPE_P (type1)) |
5398 | type1 = TREE_TYPE (type1); |
5399 | if (type0 |
5400 | && INDIRECT_TYPE_P (type0) |
5401 | && same_type_p (TREE_TYPE (type0), type1)) |
5402 | { |
5403 | if (!(TREE_CODE (first_arg) == PARM_DECL |
5404 | && DECL_ARRAY_PARAMETER_P (first_arg) |
5405 | && warn_sizeof_array_argument) |
5406 | && (complain & tf_warning)) |
5407 | { |
5408 | auto_diagnostic_group d; |
5409 | if (warning_at (location, OPT_Wsizeof_pointer_div, |
5410 | "division %<sizeof (%T) / sizeof (%T)%> does " |
5411 | "not compute the number of array elements" , |
5412 | type0, type1)) |
5413 | if (DECL_P (first_arg)) |
5414 | inform (DECL_SOURCE_LOCATION (first_arg), |
5415 | "first %<sizeof%> operand was declared here" ); |
5416 | } |
5417 | } |
5418 | else if (!dependent_type_p (type0) |
5419 | && !dependent_type_p (type1) |
5420 | && TREE_CODE (type0) == ARRAY_TYPE |
5421 | && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0))) |
5422 | /* Set by finish_parenthesized_expr. */ |
5423 | && !warning_suppressed_p (op1, OPT_Wsizeof_array_div) |
5424 | && (complain & tf_warning)) |
5425 | maybe_warn_sizeof_array_div (location, first_arg, type0, |
5426 | op1, non_reference (type1)); |
5427 | } |
5428 | |
5429 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
5430 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
5431 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
5432 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
5433 | { |
5434 | enum tree_code tcode0 = code0, tcode1 = code1; |
5435 | doing_div_or_mod = true; |
5436 | warn_for_div_by_zero (location, divisor: fold_for_warn (op1)); |
5437 | |
5438 | if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE) |
5439 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); |
5440 | if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE) |
5441 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); |
5442 | |
5443 | if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)) |
5444 | resultcode = RDIV_EXPR; |
5445 | else |
5446 | { |
5447 | /* When dividing two signed integers, we have to promote to int. |
5448 | unless we divide by a constant != -1. Note that default |
5449 | conversion will have been performed on the operands at this |
5450 | point, so we have to dig out the original type to find out if |
5451 | it was unsigned. */ |
5452 | tree stripped_op1 = tree_strip_any_location_wrapper (exp: op1); |
5453 | shorten = may_shorten_divmod (op0, op1: stripped_op1); |
5454 | } |
5455 | |
5456 | common = 1; |
5457 | } |
5458 | break; |
5459 | |
5460 | case BIT_AND_EXPR: |
5461 | case BIT_IOR_EXPR: |
5462 | case BIT_XOR_EXPR: |
5463 | if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
5464 | || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
5465 | && !VECTOR_FLOAT_TYPE_P (type0) |
5466 | && !VECTOR_FLOAT_TYPE_P (type1))) |
5467 | shorten = -1; |
5468 | break; |
5469 | |
5470 | case TRUNC_MOD_EXPR: |
5471 | case FLOOR_MOD_EXPR: |
5472 | doing_div_or_mod = true; |
5473 | warn_for_div_by_zero (location, divisor: fold_for_warn (op1)); |
5474 | |
5475 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
5476 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
5477 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) |
5478 | common = 1; |
5479 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
5480 | { |
5481 | /* Although it would be tempting to shorten always here, that loses |
5482 | on some targets, since the modulo instruction is undefined if the |
5483 | quotient can't be represented in the computation mode. We shorten |
5484 | only if unsigned or if dividing by something we know != -1. */ |
5485 | tree stripped_op1 = tree_strip_any_location_wrapper (exp: op1); |
5486 | shorten = may_shorten_divmod (op0, op1: stripped_op1); |
5487 | common = 1; |
5488 | } |
5489 | break; |
5490 | |
5491 | case TRUTH_ANDIF_EXPR: |
5492 | case TRUTH_ORIF_EXPR: |
5493 | case TRUTH_AND_EXPR: |
5494 | case TRUTH_OR_EXPR: |
5495 | if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type: type1)) |
5496 | { |
5497 | if (!COMPARISON_CLASS_P (op1)) |
5498 | op1 = cp_build_binary_op (EXPR_LOCATION (op1), code: NE_EXPR, orig_op0: op1, |
5499 | orig_op1: build_zero_cst (type1), complain); |
5500 | if (code == TRUTH_ANDIF_EXPR) |
5501 | { |
5502 | tree z = build_zero_cst (TREE_TYPE (op1)); |
5503 | return build_conditional_expr (location, op0, op1, z, complain); |
5504 | } |
5505 | else if (code == TRUTH_ORIF_EXPR) |
5506 | { |
5507 | tree m1 = build_all_ones_cst (TREE_TYPE (op1)); |
5508 | return build_conditional_expr (location, op0, m1, op1, complain); |
5509 | } |
5510 | else |
5511 | gcc_unreachable (); |
5512 | } |
5513 | if (gnu_vector_type_p (type: type0) |
5514 | && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type: type1))) |
5515 | { |
5516 | if (!COMPARISON_CLASS_P (op0)) |
5517 | op0 = cp_build_binary_op (EXPR_LOCATION (op0), code: NE_EXPR, orig_op0: op0, |
5518 | orig_op1: build_zero_cst (type0), complain); |
5519 | if (!VECTOR_TYPE_P (type1)) |
5520 | { |
5521 | tree m1 = build_all_ones_cst (TREE_TYPE (op0)); |
5522 | tree z = build_zero_cst (TREE_TYPE (op0)); |
5523 | op1 = build_conditional_expr (location, op1, m1, z, complain); |
5524 | } |
5525 | else if (!COMPARISON_CLASS_P (op1)) |
5526 | op1 = cp_build_binary_op (EXPR_LOCATION (op1), code: NE_EXPR, orig_op0: op1, |
5527 | orig_op1: build_zero_cst (type1), complain); |
5528 | |
5529 | if (code == TRUTH_ANDIF_EXPR) |
5530 | code = BIT_AND_EXPR; |
5531 | else if (code == TRUTH_ORIF_EXPR) |
5532 | code = BIT_IOR_EXPR; |
5533 | else |
5534 | gcc_unreachable (); |
5535 | |
5536 | return cp_build_binary_op (location, code, orig_op0: op0, orig_op1: op1, complain); |
5537 | } |
5538 | |
5539 | result_type = boolean_type_node; |
5540 | break; |
5541 | |
5542 | /* Shift operations: result has same type as first operand; |
5543 | always convert second operand to int. |
5544 | Also set SHORT_SHIFT if shifting rightward. */ |
5545 | |
5546 | case RSHIFT_EXPR: |
5547 | if (gnu_vector_type_p (type: type0) |
5548 | && code1 == INTEGER_TYPE |
5549 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) |
5550 | { |
5551 | result_type = type0; |
5552 | converted = 1; |
5553 | } |
5554 | else if (gnu_vector_type_p (type: type0) |
5555 | && gnu_vector_type_p (type: type1) |
5556 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
5557 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE |
5558 | && known_eq (TYPE_VECTOR_SUBPARTS (type0), |
5559 | TYPE_VECTOR_SUBPARTS (type1))) |
5560 | { |
5561 | result_type = type0; |
5562 | converted = 1; |
5563 | } |
5564 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
5565 | { |
5566 | tree const_op1 = fold_for_warn (op1); |
5567 | if (TREE_CODE (const_op1) != INTEGER_CST) |
5568 | const_op1 = op1; |
5569 | result_type = type0; |
5570 | doing_shift = true; |
5571 | if (TREE_CODE (const_op1) == INTEGER_CST) |
5572 | { |
5573 | if (tree_int_cst_lt (t1: const_op1, integer_zero_node)) |
5574 | { |
5575 | if ((complain & tf_warning) |
5576 | && c_inhibit_evaluation_warnings == 0) |
5577 | warning_at (location, OPT_Wshift_count_negative, |
5578 | "right shift count is negative" ); |
5579 | } |
5580 | else |
5581 | { |
5582 | if (!integer_zerop (const_op1)) |
5583 | short_shift = 1; |
5584 | |
5585 | if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0 |
5586 | && (complain & tf_warning) |
5587 | && c_inhibit_evaluation_warnings == 0) |
5588 | warning_at (location, OPT_Wshift_count_overflow, |
5589 | "right shift count >= width of type" ); |
5590 | } |
5591 | } |
5592 | /* Avoid converting op1 to result_type later. */ |
5593 | converted = 1; |
5594 | } |
5595 | break; |
5596 | |
5597 | case LSHIFT_EXPR: |
5598 | if (gnu_vector_type_p (type: type0) |
5599 | && code1 == INTEGER_TYPE |
5600 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) |
5601 | { |
5602 | result_type = type0; |
5603 | converted = 1; |
5604 | } |
5605 | else if (gnu_vector_type_p (type: type0) |
5606 | && gnu_vector_type_p (type: type1) |
5607 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
5608 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE |
5609 | && known_eq (TYPE_VECTOR_SUBPARTS (type0), |
5610 | TYPE_VECTOR_SUBPARTS (type1))) |
5611 | { |
5612 | result_type = type0; |
5613 | converted = 1; |
5614 | } |
5615 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
5616 | { |
5617 | tree const_op0 = fold_for_warn (op0); |
5618 | if (TREE_CODE (const_op0) != INTEGER_CST) |
5619 | const_op0 = op0; |
5620 | tree const_op1 = fold_for_warn (op1); |
5621 | if (TREE_CODE (const_op1) != INTEGER_CST) |
5622 | const_op1 = op1; |
5623 | result_type = type0; |
5624 | doing_shift = true; |
5625 | if (TREE_CODE (const_op0) == INTEGER_CST |
5626 | && tree_int_cst_sgn (const_op0) < 0 |
5627 | && !TYPE_OVERFLOW_WRAPS (type0) |
5628 | && (complain & tf_warning) |
5629 | && c_inhibit_evaluation_warnings == 0) |
5630 | warning_at (location, OPT_Wshift_negative_value, |
5631 | "left shift of negative value" ); |
5632 | if (TREE_CODE (const_op1) == INTEGER_CST) |
5633 | { |
5634 | if (tree_int_cst_lt (t1: const_op1, integer_zero_node)) |
5635 | { |
5636 | if ((complain & tf_warning) |
5637 | && c_inhibit_evaluation_warnings == 0) |
5638 | warning_at (location, OPT_Wshift_count_negative, |
5639 | "left shift count is negative" ); |
5640 | } |
5641 | else if (compare_tree_int (const_op1, |
5642 | TYPE_PRECISION (type0)) >= 0) |
5643 | { |
5644 | if ((complain & tf_warning) |
5645 | && c_inhibit_evaluation_warnings == 0) |
5646 | warning_at (location, OPT_Wshift_count_overflow, |
5647 | "left shift count >= width of type" ); |
5648 | } |
5649 | else if (TREE_CODE (const_op0) == INTEGER_CST |
5650 | && (complain & tf_warning)) |
5651 | maybe_warn_shift_overflow (location, const_op0, const_op1); |
5652 | } |
5653 | /* Avoid converting op1 to result_type later. */ |
5654 | converted = 1; |
5655 | } |
5656 | break; |
5657 | |
5658 | case EQ_EXPR: |
5659 | case NE_EXPR: |
5660 | if (gnu_vector_type_p (type: type0) && gnu_vector_type_p (type: type1)) |
5661 | goto vector_compare; |
5662 | if ((complain & tf_warning) |
5663 | && c_inhibit_evaluation_warnings == 0 |
5664 | && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))) |
5665 | warning_at (location, OPT_Wfloat_equal, |
5666 | "comparing floating-point with %<==%> " |
5667 | "or %<!=%> is unsafe" ); |
5668 | if (complain & tf_warning) |
5669 | { |
5670 | tree stripped_orig_op0 = tree_strip_any_location_wrapper (exp: orig_op0); |
5671 | tree stripped_orig_op1 = tree_strip_any_location_wrapper (exp: orig_op1); |
5672 | if ((TREE_CODE (stripped_orig_op0) == STRING_CST |
5673 | && !integer_zerop (cp_fully_fold (op1))) |
5674 | || (TREE_CODE (stripped_orig_op1) == STRING_CST |
5675 | && !integer_zerop (cp_fully_fold (op0)))) |
5676 | warning_at (location, OPT_Waddress, |
5677 | "comparison with string literal results in " |
5678 | "unspecified behavior" ); |
5679 | else if (warn_array_compare |
5680 | && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE |
5681 | && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE) |
5682 | do_warn_array_compare (location, code, stripped_orig_op0, |
5683 | stripped_orig_op1); |
5684 | } |
5685 | |
5686 | build_type = boolean_type_node; |
5687 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
5688 | || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE) |
5689 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
5690 | || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)) |
5691 | short_compare = 1; |
5692 | else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0)) |
5693 | && null_ptr_cst_p (orig_op1)) |
5694 | /* Handle, eg, (void*)0 (c++/43906), and more. */ |
5695 | || (code0 == POINTER_TYPE |
5696 | && TYPE_PTR_P (type1) && integer_zerop (op1))) |
5697 | { |
5698 | if (TYPE_PTR_P (type1)) |
5699 | result_type = composite_pointer_type (location, |
5700 | t1: type0, t2: type1, arg1: op0, arg2: op1, |
5701 | operation: CPO_COMPARISON, complain); |
5702 | else |
5703 | result_type = type0; |
5704 | |
5705 | if (char_type_p (TREE_TYPE (orig_op1))) |
5706 | { |
5707 | auto_diagnostic_group d; |
5708 | if (warning_at (location, OPT_Wpointer_compare, |
5709 | "comparison between pointer and zero character " |
5710 | "constant" )) |
5711 | inform (location, |
5712 | "did you mean to dereference the pointer?" ); |
5713 | } |
5714 | warn_for_null_address (location, op: op0, complain); |
5715 | } |
5716 | else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1)) |
5717 | && null_ptr_cst_p (orig_op0)) |
5718 | /* Handle, eg, (void*)0 (c++/43906), and more. */ |
5719 | || (code1 == POINTER_TYPE |
5720 | && TYPE_PTR_P (type0) && integer_zerop (op0))) |
5721 | { |
5722 | if (TYPE_PTR_P (type0)) |
5723 | result_type = composite_pointer_type (location, |
5724 | t1: type0, t2: type1, arg1: op0, arg2: op1, |
5725 | operation: CPO_COMPARISON, complain); |
5726 | else |
5727 | result_type = type1; |
5728 | |
5729 | if (char_type_p (TREE_TYPE (orig_op0))) |
5730 | { |
5731 | auto_diagnostic_group d; |
5732 | if (warning_at (location, OPT_Wpointer_compare, |
5733 | "comparison between pointer and zero character " |
5734 | "constant" )) |
5735 | inform (location, |
5736 | "did you mean to dereference the pointer?" ); |
5737 | } |
5738 | warn_for_null_address (location, op: op1, complain); |
5739 | } |
5740 | else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
5741 | || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1))) |
5742 | result_type = composite_pointer_type (location, |
5743 | t1: type0, t2: type1, arg1: op0, arg2: op1, |
5744 | operation: CPO_COMPARISON, complain); |
5745 | else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)) |
5746 | /* One of the operands must be of nullptr_t type. */ |
5747 | result_type = TREE_TYPE (nullptr_node); |
5748 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
5749 | { |
5750 | result_type = type0; |
5751 | if (complain & tf_error) |
5752 | permerror (location, "ISO C++ forbids comparison between " |
5753 | "pointer and integer" ); |
5754 | else |
5755 | return error_mark_node; |
5756 | } |
5757 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) |
5758 | { |
5759 | result_type = type1; |
5760 | if (complain & tf_error) |
5761 | permerror (location, "ISO C++ forbids comparison between " |
5762 | "pointer and integer" ); |
5763 | else |
5764 | return error_mark_node; |
5765 | } |
5766 | else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1)) |
5767 | { |
5768 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION |
5769 | == ptrmemfunc_vbit_in_delta) |
5770 | { |
5771 | tree pfn0, delta0, e1, e2; |
5772 | |
5773 | if (TREE_SIDE_EFFECTS (op0)) |
5774 | op0 = cp_save_expr (op0); |
5775 | |
5776 | pfn0 = pfn_from_ptrmemfunc (op0); |
5777 | delta0 = delta_from_ptrmemfunc (op0); |
5778 | { |
5779 | /* If we will warn below about a null-address compare |
5780 | involving the orig_op0 ptrmemfunc, we'd likely also |
5781 | warn about the pfn0's null-address compare, and |
5782 | that would be redundant, so suppress it. */ |
5783 | warning_sentinel ws (warn_address); |
5784 | e1 = cp_build_binary_op (location, |
5785 | code: EQ_EXPR, |
5786 | orig_op0: pfn0, |
5787 | orig_op1: build_zero_cst (TREE_TYPE (pfn0)), |
5788 | complain); |
5789 | } |
5790 | e2 = cp_build_binary_op (location, |
5791 | code: BIT_AND_EXPR, |
5792 | orig_op0: delta0, |
5793 | integer_one_node, |
5794 | complain); |
5795 | |
5796 | if (complain & tf_warning) |
5797 | maybe_warn_zero_as_null_pointer_constant (op1, input_location); |
5798 | |
5799 | e2 = cp_build_binary_op (location, |
5800 | code: EQ_EXPR, orig_op0: e2, integer_zero_node, |
5801 | complain); |
5802 | op0 = cp_build_binary_op (location, |
5803 | code: TRUTH_ANDIF_EXPR, orig_op0: e1, orig_op1: e2, |
5804 | complain); |
5805 | op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain); |
5806 | } |
5807 | else |
5808 | { |
5809 | op0 = build_ptrmemfunc_access_expr (ptrmem: op0, pfn_identifier); |
5810 | op1 = cp_convert (TREE_TYPE (op0), op1, complain); |
5811 | } |
5812 | result_type = TREE_TYPE (op0); |
5813 | |
5814 | warn_for_null_address (location, op: orig_op0, complain); |
5815 | } |
5816 | else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0)) |
5817 | return cp_build_binary_op (location, code, orig_op0: op1, orig_op1: op0, complain); |
5818 | else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)) |
5819 | { |
5820 | tree type; |
5821 | /* E will be the final comparison. */ |
5822 | tree e; |
5823 | /* E1 and E2 are for scratch. */ |
5824 | tree e1; |
5825 | tree e2; |
5826 | tree pfn0; |
5827 | tree pfn1; |
5828 | tree delta0; |
5829 | tree delta1; |
5830 | |
5831 | type = composite_pointer_type (location, t1: type0, t2: type1, arg1: op0, arg2: op1, |
5832 | operation: CPO_COMPARISON, complain); |
5833 | |
5834 | if (!same_type_p (TREE_TYPE (op0), type)) |
5835 | op0 = cp_convert_and_check (type, op0, complain); |
5836 | if (!same_type_p (TREE_TYPE (op1), type)) |
5837 | op1 = cp_convert_and_check (type, op1, complain); |
5838 | |
5839 | if (op0 == error_mark_node || op1 == error_mark_node) |
5840 | return error_mark_node; |
5841 | |
5842 | if (TREE_SIDE_EFFECTS (op0)) |
5843 | op0 = save_expr (op0); |
5844 | if (TREE_SIDE_EFFECTS (op1)) |
5845 | op1 = save_expr (op1); |
5846 | |
5847 | pfn0 = pfn_from_ptrmemfunc (op0); |
5848 | pfn0 = cp_fully_fold (pfn0); |
5849 | /* Avoid -Waddress warnings (c++/64877). */ |
5850 | if (TREE_CODE (pfn0) == ADDR_EXPR) |
5851 | suppress_warning (pfn0, OPT_Waddress); |
5852 | pfn1 = pfn_from_ptrmemfunc (op1); |
5853 | pfn1 = cp_fully_fold (pfn1); |
5854 | delta0 = delta_from_ptrmemfunc (op0); |
5855 | delta1 = delta_from_ptrmemfunc (op1); |
5856 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION |
5857 | == ptrmemfunc_vbit_in_delta) |
5858 | { |
5859 | /* We generate: |
5860 | |
5861 | (op0.pfn == op1.pfn |
5862 | && ((op0.delta == op1.delta) |
5863 | || (!op0.pfn && op0.delta & 1 == 0 |
5864 | && op1.delta & 1 == 0)) |
5865 | |
5866 | The reason for the `!op0.pfn' bit is that a NULL |
5867 | pointer-to-member is any member with a zero PFN and |
5868 | LSB of the DELTA field is 0. */ |
5869 | |
5870 | e1 = cp_build_binary_op (location, code: BIT_AND_EXPR, |
5871 | orig_op0: delta0, |
5872 | integer_one_node, |
5873 | complain); |
5874 | e1 = cp_build_binary_op (location, |
5875 | code: EQ_EXPR, orig_op0: e1, integer_zero_node, |
5876 | complain); |
5877 | e2 = cp_build_binary_op (location, code: BIT_AND_EXPR, |
5878 | orig_op0: delta1, |
5879 | integer_one_node, |
5880 | complain); |
5881 | e2 = cp_build_binary_op (location, |
5882 | code: EQ_EXPR, orig_op0: e2, integer_zero_node, |
5883 | complain); |
5884 | e1 = cp_build_binary_op (location, |
5885 | code: TRUTH_ANDIF_EXPR, orig_op0: e2, orig_op1: e1, |
5886 | complain); |
5887 | e2 = cp_build_binary_op (location, code: EQ_EXPR, |
5888 | orig_op0: pfn0, |
5889 | orig_op1: build_zero_cst (TREE_TYPE (pfn0)), |
5890 | complain); |
5891 | e2 = cp_build_binary_op (location, |
5892 | code: TRUTH_ANDIF_EXPR, orig_op0: e2, orig_op1: e1, complain); |
5893 | e1 = cp_build_binary_op (location, |
5894 | code: EQ_EXPR, orig_op0: delta0, orig_op1: delta1, complain); |
5895 | e1 = cp_build_binary_op (location, |
5896 | code: TRUTH_ORIF_EXPR, orig_op0: e1, orig_op1: e2, complain); |
5897 | } |
5898 | else |
5899 | { |
5900 | /* We generate: |
5901 | |
5902 | (op0.pfn == op1.pfn |
5903 | && (!op0.pfn || op0.delta == op1.delta)) |
5904 | |
5905 | The reason for the `!op0.pfn' bit is that a NULL |
5906 | pointer-to-member is any member with a zero PFN; the |
5907 | DELTA field is unspecified. */ |
5908 | |
5909 | e1 = cp_build_binary_op (location, |
5910 | code: EQ_EXPR, orig_op0: delta0, orig_op1: delta1, complain); |
5911 | e2 = cp_build_binary_op (location, |
5912 | code: EQ_EXPR, |
5913 | orig_op0: pfn0, |
5914 | orig_op1: build_zero_cst (TREE_TYPE (pfn0)), |
5915 | complain); |
5916 | e1 = cp_build_binary_op (location, |
5917 | code: TRUTH_ORIF_EXPR, orig_op0: e1, orig_op1: e2, complain); |
5918 | } |
5919 | e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1); |
5920 | e = cp_build_binary_op (location, |
5921 | code: TRUTH_ANDIF_EXPR, orig_op0: e2, orig_op1: e1, complain); |
5922 | if (code == EQ_EXPR) |
5923 | return e; |
5924 | return cp_build_binary_op (location, |
5925 | code: EQ_EXPR, orig_op0: e, integer_zero_node, complain); |
5926 | } |
5927 | else |
5928 | { |
5929 | gcc_assert (!TYPE_PTRMEMFUNC_P (type0) |
5930 | || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), |
5931 | type1)); |
5932 | gcc_assert (!TYPE_PTRMEMFUNC_P (type1) |
5933 | || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), |
5934 | type0)); |
5935 | } |
5936 | |
5937 | break; |
5938 | |
5939 | case MAX_EXPR: |
5940 | case MIN_EXPR: |
5941 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) |
5942 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) |
5943 | shorten = 1; |
5944 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
5945 | result_type = composite_pointer_type (location, |
5946 | t1: type0, t2: type1, arg1: op0, arg2: op1, |
5947 | operation: CPO_COMPARISON, complain); |
5948 | break; |
5949 | |
5950 | case LE_EXPR: |
5951 | case GE_EXPR: |
5952 | case LT_EXPR: |
5953 | case GT_EXPR: |
5954 | case SPACESHIP_EXPR: |
5955 | if (TREE_CODE (orig_op0) == STRING_CST |
5956 | || TREE_CODE (orig_op1) == STRING_CST) |
5957 | { |
5958 | if (complain & tf_warning) |
5959 | warning_at (location, OPT_Waddress, |
5960 | "comparison with string literal results " |
5961 | "in unspecified behavior" ); |
5962 | } |
5963 | else if (warn_array_compare |
5964 | && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE |
5965 | && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE |
5966 | && code != SPACESHIP_EXPR |
5967 | && (complain & tf_warning)) |
5968 | do_warn_array_compare (location, code, |
5969 | tree_strip_any_location_wrapper (exp: orig_op0), |
5970 | tree_strip_any_location_wrapper (exp: orig_op1)); |
5971 | |
5972 | if (gnu_vector_type_p (type: type0) && gnu_vector_type_p (type: type1)) |
5973 | { |
5974 | vector_compare: |
5975 | tree intt; |
5976 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0), |
5977 | TREE_TYPE (type1)) |
5978 | && !vector_types_compatible_elements_p (type0, type1)) |
5979 | { |
5980 | if (complain & tf_error) |
5981 | { |
5982 | error_at (location, "comparing vectors with different " |
5983 | "element types" ); |
5984 | inform (location, "operand types are %qT and %qT" , |
5985 | type0, type1); |
5986 | } |
5987 | return error_mark_node; |
5988 | } |
5989 | |
5990 | if (maybe_ne (a: TYPE_VECTOR_SUBPARTS (node: type0), |
5991 | b: TYPE_VECTOR_SUBPARTS (node: type1))) |
5992 | { |
5993 | if (complain & tf_error) |
5994 | { |
5995 | error_at (location, "comparing vectors with different " |
5996 | "number of elements" ); |
5997 | inform (location, "operand types are %qT and %qT" , |
5998 | type0, type1); |
5999 | } |
6000 | return error_mark_node; |
6001 | } |
6002 | |
6003 | /* It's not precisely specified how the usual arithmetic |
6004 | conversions apply to the vector types. Here, we use |
6005 | the unsigned type if one of the operands is signed and |
6006 | the other one is unsigned. */ |
6007 | if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)) |
6008 | { |
6009 | if (!TYPE_UNSIGNED (type0)) |
6010 | op0 = build1 (VIEW_CONVERT_EXPR, type1, op0); |
6011 | else |
6012 | op1 = build1 (VIEW_CONVERT_EXPR, type0, op1); |
6013 | warning_at (location, OPT_Wsign_compare, "comparison between " |
6014 | "types %qT and %qT" , type0, type1); |
6015 | } |
6016 | |
6017 | if (resultcode == SPACESHIP_EXPR) |
6018 | { |
6019 | if (complain & tf_error) |
6020 | sorry_at (location, "three-way comparison of vectors" ); |
6021 | return error_mark_node; |
6022 | } |
6023 | |
6024 | /* Always construct signed integer vector type. */ |
6025 | intt = c_common_type_for_size |
6026 | (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0); |
6027 | if (!intt) |
6028 | { |
6029 | if (complain & tf_error) |
6030 | error_at (location, "could not find an integer type " |
6031 | "of the same size as %qT" , TREE_TYPE (type0)); |
6032 | return error_mark_node; |
6033 | } |
6034 | result_type = build_opaque_vector_type (intt, |
6035 | TYPE_VECTOR_SUBPARTS (node: type0)); |
6036 | return build_vec_cmp (code: resultcode, type: result_type, arg0: op0, arg1: op1); |
6037 | } |
6038 | build_type = boolean_type_node; |
6039 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
6040 | || code0 == ENUMERAL_TYPE) |
6041 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
6042 | || code1 == ENUMERAL_TYPE)) |
6043 | short_compare = 1; |
6044 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
6045 | result_type = composite_pointer_type (location, |
6046 | t1: type0, t2: type1, arg1: op0, arg2: op1, |
6047 | operation: CPO_COMPARISON, complain); |
6048 | else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1)) |
6049 | || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0)) |
6050 | || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))) |
6051 | { |
6052 | /* Core Issue 1512 made this ill-formed. */ |
6053 | if (complain & tf_error) |
6054 | error_at (location, "ordered comparison of pointer with " |
6055 | "integer zero (%qT and %qT)" , type0, type1); |
6056 | return error_mark_node; |
6057 | } |
6058 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
6059 | { |
6060 | result_type = type0; |
6061 | if (complain & tf_error) |
6062 | permerror (location, "ISO C++ forbids comparison between " |
6063 | "pointer and integer" ); |
6064 | else |
6065 | return error_mark_node; |
6066 | } |
6067 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) |
6068 | { |
6069 | result_type = type1; |
6070 | if (complain & tf_error) |
6071 | permerror (location, "ISO C++ forbids comparison between " |
6072 | "pointer and integer" ); |
6073 | else |
6074 | return error_mark_node; |
6075 | } |
6076 | |
6077 | if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE) |
6078 | && !processing_template_decl |
6079 | && sanitize_flags_p (flag: SANITIZE_POINTER_COMPARE)) |
6080 | { |
6081 | op0 = save_expr (op0); |
6082 | op1 = save_expr (op1); |
6083 | |
6084 | tree tt = builtin_decl_explicit (fncode: BUILT_IN_ASAN_POINTER_COMPARE); |
6085 | instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1); |
6086 | } |
6087 | |
6088 | break; |
6089 | |
6090 | case UNORDERED_EXPR: |
6091 | case ORDERED_EXPR: |
6092 | case UNLT_EXPR: |
6093 | case UNLE_EXPR: |
6094 | case UNGT_EXPR: |
6095 | case UNGE_EXPR: |
6096 | case UNEQ_EXPR: |
6097 | build_type = integer_type_node; |
6098 | if (code0 != REAL_TYPE || code1 != REAL_TYPE) |
6099 | { |
6100 | if (complain & tf_error) |
6101 | error ("unordered comparison on non-floating-point argument" ); |
6102 | return error_mark_node; |
6103 | } |
6104 | common = 1; |
6105 | break; |
6106 | |
6107 | default: |
6108 | break; |
6109 | } |
6110 | |
6111 | if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
6112 | || code0 == ENUMERAL_TYPE) |
6113 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
6114 | || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))) |
6115 | arithmetic_types_p = 1; |
6116 | else |
6117 | { |
6118 | arithmetic_types_p = 0; |
6119 | /* Vector arithmetic is only allowed when both sides are vectors. */ |
6120 | if (gnu_vector_type_p (type: type0) && gnu_vector_type_p (type: type1)) |
6121 | { |
6122 | if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) |
6123 | || !vector_types_compatible_elements_p (type0, type1)) |
6124 | { |
6125 | if (complain & tf_error) |
6126 | { |
6127 | /* "location" already embeds the locations of the |
6128 | operands, so we don't need to add them separately |
6129 | to richloc. */ |
6130 | rich_location richloc (line_table, location); |
6131 | binary_op_error (&richloc, code, type0, type1); |
6132 | } |
6133 | return error_mark_node; |
6134 | } |
6135 | arithmetic_types_p = 1; |
6136 | } |
6137 | } |
6138 | /* Determine the RESULT_TYPE, if it is not already known. */ |
6139 | if (!result_type |
6140 | && arithmetic_types_p |
6141 | && (shorten || common || short_compare)) |
6142 | { |
6143 | result_type = cp_common_type (t1: type0, t2: type1); |
6144 | if (result_type == error_mark_node) |
6145 | { |
6146 | tree t1 = type0; |
6147 | tree t2 = type1; |
6148 | if (TREE_CODE (t1) == COMPLEX_TYPE) |
6149 | t1 = TREE_TYPE (t1); |
6150 | if (TREE_CODE (t2) == COMPLEX_TYPE) |
6151 | t2 = TREE_TYPE (t2); |
6152 | gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE |
6153 | && TREE_CODE (t2) == REAL_TYPE |
6154 | && (extended_float_type_p (t1) |
6155 | || extended_float_type_p (t2)) |
6156 | && cp_compare_floating_point_conversion_ranks |
6157 | (t1, t2) == 3); |
6158 | if (complain & tf_error) |
6159 | { |
6160 | rich_location richloc (line_table, location); |
6161 | binary_op_error (&richloc, code, type0, type1); |
6162 | } |
6163 | return error_mark_node; |
6164 | } |
6165 | if (complain & tf_warning) |
6166 | { |
6167 | do_warn_double_promotion (result_type, type0, type1, |
6168 | "implicit conversion from %qH to %qI " |
6169 | "to match other operand of binary " |
6170 | "expression" , |
6171 | location); |
6172 | do_warn_enum_conversions (loc: location, code, TREE_TYPE (orig_op0), |
6173 | TREE_TYPE (orig_op1)); |
6174 | } |
6175 | } |
6176 | if (may_need_excess_precision |
6177 | && (orig_type0 != type0 || orig_type1 != type1) |
6178 | && build_type == NULL_TREE |
6179 | && result_type) |
6180 | { |
6181 | gcc_assert (common); |
6182 | semantic_result_type = cp_common_type (t1: orig_type0, t2: orig_type1); |
6183 | if (semantic_result_type == error_mark_node) |
6184 | { |
6185 | tree t1 = orig_type0; |
6186 | tree t2 = orig_type1; |
6187 | if (TREE_CODE (t1) == COMPLEX_TYPE) |
6188 | t1 = TREE_TYPE (t1); |
6189 | if (TREE_CODE (t2) == COMPLEX_TYPE) |
6190 | t2 = TREE_TYPE (t2); |
6191 | gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE |
6192 | && TREE_CODE (t2) == REAL_TYPE |
6193 | && (extended_float_type_p (t1) |
6194 | || extended_float_type_p (t2)) |
6195 | && cp_compare_floating_point_conversion_ranks |
6196 | (t1, t2) == 3); |
6197 | if (complain & tf_error) |
6198 | { |
6199 | rich_location richloc (line_table, location); |
6200 | binary_op_error (&richloc, code, type0, type1); |
6201 | } |
6202 | return error_mark_node; |
6203 | } |
6204 | } |
6205 | |
6206 | if (code == SPACESHIP_EXPR) |
6207 | { |
6208 | iloc_sentinel s (location); |
6209 | |
6210 | tree orig_type0 = TREE_TYPE (orig_op0); |
6211 | tree_code orig_code0 = TREE_CODE (orig_type0); |
6212 | tree orig_type1 = TREE_TYPE (orig_op1); |
6213 | tree_code orig_code1 = TREE_CODE (orig_type1); |
6214 | if (!result_type || result_type == error_mark_node) |
6215 | /* Nope. */ |
6216 | result_type = NULL_TREE; |
6217 | else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE)) |
6218 | /* "If one of the operands is of type bool and the other is not, the |
6219 | program is ill-formed." */ |
6220 | result_type = NULL_TREE; |
6221 | else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE |
6222 | && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE) |
6223 | /* We only do array/function-to-pointer conversion if "at least one of |
6224 | the operands is of pointer type". */ |
6225 | result_type = NULL_TREE; |
6226 | else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type)) |
6227 | /* <=> no longer supports equality relations. */ |
6228 | result_type = NULL_TREE; |
6229 | else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE |
6230 | && !(same_type_ignoring_top_level_qualifiers_p |
6231 | (type1: orig_type0, type2: orig_type1))) |
6232 | /* "If both operands have arithmetic types, or one operand has integral |
6233 | type and the other operand has unscoped enumeration type, the usual |
6234 | arithmetic conversions are applied to the operands." So we don't do |
6235 | arithmetic conversions if the operands both have enumeral type. */ |
6236 | result_type = NULL_TREE; |
6237 | else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE) |
6238 | || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE)) |
6239 | /* [depr.arith.conv.enum]: Three-way comparisons between such operands |
6240 | [where one is of enumeration type and the other is of a different |
6241 | enumeration type or a floating-point type] are ill-formed. */ |
6242 | result_type = NULL_TREE; |
6243 | |
6244 | if (result_type) |
6245 | { |
6246 | build_type = spaceship_type (result_type, complain); |
6247 | if (build_type == error_mark_node) |
6248 | return error_mark_node; |
6249 | } |
6250 | |
6251 | if (result_type && arithmetic_types_p) |
6252 | { |
6253 | /* If a narrowing conversion is required, other than from an integral |
6254 | type to a floating point type, the program is ill-formed. */ |
6255 | bool ok = true; |
6256 | if (TREE_CODE (result_type) == REAL_TYPE |
6257 | && CP_INTEGRAL_TYPE_P (orig_type0)) |
6258 | /* OK */; |
6259 | else if (!check_narrowing (result_type, orig_op0, complain)) |
6260 | ok = false; |
6261 | if (TREE_CODE (result_type) == REAL_TYPE |
6262 | && CP_INTEGRAL_TYPE_P (orig_type1)) |
6263 | /* OK */; |
6264 | else if (!check_narrowing (result_type, orig_op1, complain)) |
6265 | ok = false; |
6266 | if (!ok && !(complain & tf_error)) |
6267 | return error_mark_node; |
6268 | } |
6269 | } |
6270 | |
6271 | if (!result_type) |
6272 | { |
6273 | if (complain & tf_error) |
6274 | { |
6275 | binary_op_rich_location richloc (location, |
6276 | orig_op0, orig_op1, true); |
6277 | error_at (&richloc, |
6278 | "invalid operands of types %qT and %qT to binary %qO" , |
6279 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code); |
6280 | } |
6281 | return error_mark_node; |
6282 | } |
6283 | |
6284 | /* If we're in a template, the only thing we need to know is the |
6285 | RESULT_TYPE. */ |
6286 | if (processing_template_decl) |
6287 | { |
6288 | /* Since the middle-end checks the type when doing a build2, we |
6289 | need to build the tree in pieces. This built tree will never |
6290 | get out of the front-end as we replace it when instantiating |
6291 | the template. */ |
6292 | tree tmp = build2 (resultcode, |
6293 | build_type ? build_type : result_type, |
6294 | NULL_TREE, op1); |
6295 | TREE_OPERAND (tmp, 0) = op0; |
6296 | if (semantic_result_type) |
6297 | tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp); |
6298 | return tmp; |
6299 | } |
6300 | |
6301 | /* Remember the original type; RESULT_TYPE might be changed later on |
6302 | by shorten_binary_op. */ |
6303 | tree orig_type = result_type; |
6304 | |
6305 | if (arithmetic_types_p) |
6306 | { |
6307 | bool first_complex = (code0 == COMPLEX_TYPE); |
6308 | bool second_complex = (code1 == COMPLEX_TYPE); |
6309 | int none_complex = (!first_complex && !second_complex); |
6310 | |
6311 | /* Adapted from patch for c/24581. */ |
6312 | if (first_complex != second_complex |
6313 | && (code == PLUS_EXPR |
6314 | || code == MINUS_EXPR |
6315 | || code == MULT_EXPR |
6316 | || (code == TRUNC_DIV_EXPR && first_complex)) |
6317 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE |
6318 | && flag_signed_zeros) |
6319 | { |
6320 | /* An operation on mixed real/complex operands must be |
6321 | handled specially, but the language-independent code can |
6322 | more easily optimize the plain complex arithmetic if |
6323 | -fno-signed-zeros. */ |
6324 | tree real_type = TREE_TYPE (result_type); |
6325 | tree real, imag; |
6326 | if (first_complex) |
6327 | { |
6328 | if (TREE_TYPE (op0) != result_type) |
6329 | op0 = cp_convert_and_check (result_type, op0, complain); |
6330 | if (TREE_TYPE (op1) != real_type) |
6331 | op1 = cp_convert_and_check (real_type, op1, complain); |
6332 | } |
6333 | else |
6334 | { |
6335 | if (TREE_TYPE (op0) != real_type) |
6336 | op0 = cp_convert_and_check (real_type, op0, complain); |
6337 | if (TREE_TYPE (op1) != result_type) |
6338 | op1 = cp_convert_and_check (result_type, op1, complain); |
6339 | } |
6340 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) |
6341 | return error_mark_node; |
6342 | if (first_complex) |
6343 | { |
6344 | op0 = save_expr (op0); |
6345 | real = cp_build_unary_op (REALPART_EXPR, op0, true, complain); |
6346 | imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain); |
6347 | switch (code) |
6348 | { |
6349 | case MULT_EXPR: |
6350 | case TRUNC_DIV_EXPR: |
6351 | op1 = save_expr (op1); |
6352 | imag = build2 (resultcode, real_type, imag, op1); |
6353 | /* Fall through. */ |
6354 | case PLUS_EXPR: |
6355 | case MINUS_EXPR: |
6356 | real = build2 (resultcode, real_type, real, op1); |
6357 | break; |
6358 | default: |
6359 | gcc_unreachable(); |
6360 | } |
6361 | } |
6362 | else |
6363 | { |
6364 | op1 = save_expr (op1); |
6365 | real = cp_build_unary_op (REALPART_EXPR, op1, true, complain); |
6366 | imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain); |
6367 | switch (code) |
6368 | { |
6369 | case MULT_EXPR: |
6370 | op0 = save_expr (op0); |
6371 | imag = build2 (resultcode, real_type, op0, imag); |
6372 | /* Fall through. */ |
6373 | case PLUS_EXPR: |
6374 | real = build2 (resultcode, real_type, op0, real); |
6375 | break; |
6376 | case MINUS_EXPR: |
6377 | real = build2 (resultcode, real_type, op0, real); |
6378 | imag = build1 (NEGATE_EXPR, real_type, imag); |
6379 | break; |
6380 | default: |
6381 | gcc_unreachable(); |
6382 | } |
6383 | } |
6384 | result = build2 (COMPLEX_EXPR, result_type, real, imag); |
6385 | if (semantic_result_type) |
6386 | result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, |
6387 | result); |
6388 | return result; |
6389 | } |
6390 | |
6391 | /* For certain operations (which identify themselves by shorten != 0) |
6392 | if both args were extended from the same smaller type, |
6393 | do the arithmetic in that type and then extend. |
6394 | |
6395 | shorten !=0 and !=1 indicates a bitwise operation. |
6396 | For them, this optimization is safe only if |
6397 | both args are zero-extended or both are sign-extended. |
6398 | Otherwise, we might change the result. |
6399 | E.g., (short)-1 | (unsigned short)-1 is (int)-1 |
6400 | but calculated in (unsigned short) it would be (unsigned short)-1. */ |
6401 | |
6402 | if (shorten && none_complex) |
6403 | { |
6404 | final_type = result_type; |
6405 | result_type = shorten_binary_op (result_type, op0, op1, |
6406 | bitwise: shorten == -1); |
6407 | } |
6408 | |
6409 | /* Shifts can be shortened if shifting right. */ |
6410 | |
6411 | if (short_shift) |
6412 | { |
6413 | int unsigned_arg; |
6414 | tree arg0 = get_narrower (op0, &unsigned_arg); |
6415 | /* We're not really warning here but when we set short_shift we |
6416 | used fold_for_warn to fold the operand. */ |
6417 | tree const_op1 = fold_for_warn (op1); |
6418 | |
6419 | final_type = result_type; |
6420 | |
6421 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) |
6422 | unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); |
6423 | |
6424 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) |
6425 | && tree_int_cst_sgn (const_op1) > 0 |
6426 | /* We can shorten only if the shift count is less than the |
6427 | number of bits in the smaller type size. */ |
6428 | && compare_tree_int (const_op1, |
6429 | TYPE_PRECISION (TREE_TYPE (arg0))) < 0 |
6430 | /* We cannot drop an unsigned shift after sign-extension. */ |
6431 | && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) |
6432 | { |
6433 | /* Do an unsigned shift if the operand was zero-extended. */ |
6434 | result_type |
6435 | = c_common_signed_or_unsigned_type (unsigned_arg, |
6436 | TREE_TYPE (arg0)); |
6437 | /* Convert value-to-be-shifted to that type. */ |
6438 | if (TREE_TYPE (op0) != result_type) |
6439 | op0 = convert (result_type, op0); |
6440 | converted = 1; |
6441 | } |
6442 | } |
6443 | |
6444 | /* Comparison operations are shortened too but differently. |
6445 | They identify themselves by setting short_compare = 1. */ |
6446 | |
6447 | if (short_compare) |
6448 | { |
6449 | /* We call shorten_compare only for diagnostics. */ |
6450 | tree xop0 = fold_simple (op0); |
6451 | tree xop1 = fold_simple (op1); |
6452 | tree xresult_type = result_type; |
6453 | enum tree_code xresultcode = resultcode; |
6454 | shorten_compare (location, &xop0, &xop1, &xresult_type, |
6455 | &xresultcode); |
6456 | } |
6457 | |
6458 | if ((short_compare || code == MIN_EXPR || code == MAX_EXPR) |
6459 | && warn_sign_compare |
6460 | /* Do not warn until the template is instantiated; we cannot |
6461 | bound the ranges of the arguments until that point. */ |
6462 | && !processing_template_decl |
6463 | && (complain & tf_warning) |
6464 | && c_inhibit_evaluation_warnings == 0 |
6465 | /* Even unsigned enum types promote to signed int. We don't |
6466 | want to issue -Wsign-compare warnings for this case. */ |
6467 | && !enum_cast_to_int (op: orig_op0) |
6468 | && !enum_cast_to_int (op: orig_op1)) |
6469 | { |
6470 | warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1, |
6471 | result_type, resultcode); |
6472 | } |
6473 | } |
6474 | |
6475 | /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE. |
6476 | Then the expression will be built. |
6477 | It will be given type FINAL_TYPE if that is nonzero; |
6478 | otherwise, it will be given type RESULT_TYPE. */ |
6479 | if (! converted) |
6480 | { |
6481 | warning_sentinel w (warn_sign_conversion, short_compare); |
6482 | if (!same_type_p (TREE_TYPE (op0), result_type)) |
6483 | op0 = cp_convert_and_check (result_type, op0, complain); |
6484 | if (!same_type_p (TREE_TYPE (op1), result_type)) |
6485 | op1 = cp_convert_and_check (result_type, op1, complain); |
6486 | |
6487 | if (op0 == error_mark_node || op1 == error_mark_node) |
6488 | return error_mark_node; |
6489 | } |
6490 | |
6491 | if (build_type == NULL_TREE) |
6492 | build_type = result_type; |
6493 | |
6494 | if (doing_shift |
6495 | && flag_strong_eval_order == 2 |
6496 | && TREE_SIDE_EFFECTS (op1) |
6497 | && !processing_template_decl) |
6498 | { |
6499 | /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before |
6500 | op1, so if op1 has side-effects, use SAVE_EXPR around op0. */ |
6501 | op0 = cp_save_expr (op0); |
6502 | instrument_expr = op0; |
6503 | } |
6504 | |
6505 | if (sanitize_flags_p (flag: (SANITIZE_SHIFT |
6506 | | SANITIZE_DIVIDE |
6507 | | SANITIZE_FLOAT_DIVIDE |
6508 | | SANITIZE_SI_OVERFLOW)) |
6509 | && current_function_decl != NULL_TREE |
6510 | && !processing_template_decl |
6511 | && (doing_div_or_mod || doing_shift)) |
6512 | { |
6513 | /* OP0 and/or OP1 might have side-effects. */ |
6514 | op0 = cp_save_expr (op0); |
6515 | op1 = cp_save_expr (op1); |
6516 | op0 = fold_non_dependent_expr (op0, complain); |
6517 | op1 = fold_non_dependent_expr (op1, complain); |
6518 | tree instrument_expr1 = NULL_TREE; |
6519 | if (doing_div_or_mod |
6520 | && sanitize_flags_p (flag: SANITIZE_DIVIDE |
6521 | | SANITIZE_FLOAT_DIVIDE |
6522 | | SANITIZE_SI_OVERFLOW)) |
6523 | { |
6524 | /* For diagnostics we want to use the promoted types without |
6525 | shorten_binary_op. So convert the arguments to the |
6526 | original result_type. */ |
6527 | tree cop0 = op0; |
6528 | tree cop1 = op1; |
6529 | if (TREE_TYPE (cop0) != orig_type) |
6530 | cop0 = cp_convert (orig_type, op0, complain); |
6531 | if (TREE_TYPE (cop1) != orig_type) |
6532 | cop1 = cp_convert (orig_type, op1, complain); |
6533 | instrument_expr1 = ubsan_instrument_division (location, cop0, cop1); |
6534 | } |
6535 | else if (doing_shift && sanitize_flags_p (flag: SANITIZE_SHIFT)) |
6536 | instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1); |
6537 | if (instrument_expr != NULL) |
6538 | instrument_expr = add_stmt_to_compound (instrument_expr, |
6539 | instrument_expr1); |
6540 | else |
6541 | instrument_expr = instrument_expr1; |
6542 | } |
6543 | |
6544 | result = build2_loc (loc: location, code: resultcode, type: build_type, arg0: op0, arg1: op1); |
6545 | if (final_type != 0) |
6546 | result = cp_convert (final_type, result, complain); |
6547 | |
6548 | if (instrument_expr != NULL) |
6549 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
6550 | instrument_expr, result); |
6551 | |
6552 | if (resultcode == SPACESHIP_EXPR && !processing_template_decl) |
6553 | result = get_target_expr (result, complain); |
6554 | |
6555 | if (semantic_result_type) |
6556 | result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result); |
6557 | |
6558 | if (!c_inhibit_evaluation_warnings) |
6559 | { |
6560 | if (!processing_template_decl) |
6561 | { |
6562 | op0 = cp_fully_fold (op0); |
6563 | /* Only consider the second argument if the first isn't overflowed. */ |
6564 | if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0)) |
6565 | return result; |
6566 | op1 = cp_fully_fold (op1); |
6567 | if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1)) |
6568 | return result; |
6569 | } |
6570 | else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1) |
6571 | || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1)) |
6572 | return result; |
6573 | |
6574 | tree result_ovl = fold_build2 (resultcode, build_type, op0, op1); |
6575 | if (TREE_OVERFLOW_P (result_ovl)) |
6576 | overflow_warning (location, result_ovl); |
6577 | } |
6578 | |
6579 | return result; |
6580 | } |
6581 | |
6582 | /* Build a VEC_PERM_EXPR. |
6583 | This is a simple wrapper for c_build_vec_perm_expr. */ |
6584 | tree |
6585 | build_x_vec_perm_expr (location_t loc, |
6586 | tree arg0, tree arg1, tree arg2, |
6587 | tsubst_flags_t complain) |
6588 | { |
6589 | tree orig_arg0 = arg0; |
6590 | tree orig_arg1 = arg1; |
6591 | tree orig_arg2 = arg2; |
6592 | if (processing_template_decl) |
6593 | { |
6594 | if (type_dependent_expression_p (arg0) |
6595 | || type_dependent_expression_p (arg1) |
6596 | || type_dependent_expression_p (arg2)) |
6597 | return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2); |
6598 | } |
6599 | tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error); |
6600 | if (processing_template_decl && exp != error_mark_node) |
6601 | return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0, |
6602 | orig_arg1, orig_arg2); |
6603 | return exp; |
6604 | } |
6605 | |
6606 | /* Build a VEC_PERM_EXPR. |
6607 | This is a simple wrapper for c_build_shufflevector. */ |
6608 | tree |
6609 | build_x_shufflevector (location_t loc, vec<tree, va_gc> *args, |
6610 | tsubst_flags_t complain) |
6611 | { |
6612 | tree arg0 = (*args)[0]; |
6613 | tree arg1 = (*args)[1]; |
6614 | if (processing_template_decl) |
6615 | { |
6616 | for (unsigned i = 0; i < args->length (); ++i) |
6617 | if (i <= 1 |
6618 | ? type_dependent_expression_p ((*args)[i]) |
6619 | : instantiation_dependent_expression_p ((*args)[i])) |
6620 | { |
6621 | tree exp = build_min_nt_call_vec (NULL, args); |
6622 | CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR; |
6623 | return exp; |
6624 | } |
6625 | } |
6626 | auto_vec<tree, 16> mask; |
6627 | for (unsigned i = 2; i < args->length (); ++i) |
6628 | { |
6629 | tree idx = fold_non_dependent_expr ((*args)[i], complain); |
6630 | mask.safe_push (obj: idx); |
6631 | } |
6632 | tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error); |
6633 | if (processing_template_decl && exp != error_mark_node) |
6634 | { |
6635 | exp = build_min_non_dep_call_vec (exp, NULL, args); |
6636 | CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR; |
6637 | } |
6638 | return exp; |
6639 | } |
6640 | |
6641 | /* Return a tree for the sum or difference (RESULTCODE says which) |
6642 | of pointer PTROP and integer INTOP. */ |
6643 | |
6644 | static tree |
6645 | cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop, |
6646 | tree intop, tsubst_flags_t complain) |
6647 | { |
6648 | tree res_type = TREE_TYPE (ptrop); |
6649 | |
6650 | /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type) |
6651 | in certain circumstance (when it's valid to do so). So we need |
6652 | to make sure it's complete. We don't need to check here, if we |
6653 | can actually complete it at all, as those checks will be done in |
6654 | pointer_int_sum() anyway. */ |
6655 | complete_type (TREE_TYPE (res_type)); |
6656 | |
6657 | return pointer_int_sum (loc, resultcode, ptrop, |
6658 | intop, complain & tf_warning_or_error); |
6659 | } |
6660 | |
6661 | /* Return a tree for the difference of pointers OP0 and OP1. |
6662 | The resulting tree has type int. If POINTER_SUBTRACT sanitization is |
6663 | enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */ |
6664 | |
6665 | static tree |
6666 | pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype, |
6667 | tsubst_flags_t complain, tree *instrument_expr) |
6668 | { |
6669 | tree result, inttype; |
6670 | tree restype = ptrdiff_type_node; |
6671 | tree target_type = TREE_TYPE (ptrtype); |
6672 | |
6673 | if (!complete_type_or_maybe_complain (type: target_type, NULL_TREE, complain)) |
6674 | return error_mark_node; |
6675 | |
6676 | if (VOID_TYPE_P (target_type)) |
6677 | { |
6678 | if (complain & tf_error) |
6679 | permerror (loc, "ISO C++ forbids using pointer of " |
6680 | "type %<void *%> in subtraction" ); |
6681 | else |
6682 | return error_mark_node; |
6683 | } |
6684 | if (TREE_CODE (target_type) == FUNCTION_TYPE) |
6685 | { |
6686 | if (complain & tf_error) |
6687 | permerror (loc, "ISO C++ forbids using pointer to " |
6688 | "a function in subtraction" ); |
6689 | else |
6690 | return error_mark_node; |
6691 | } |
6692 | if (TREE_CODE (target_type) == METHOD_TYPE) |
6693 | { |
6694 | if (complain & tf_error) |
6695 | permerror (loc, "ISO C++ forbids using pointer to " |
6696 | "a method in subtraction" ); |
6697 | else |
6698 | return error_mark_node; |
6699 | } |
6700 | else if (!verify_type_context (loc, TCTX_POINTER_ARITH, |
6701 | TREE_TYPE (TREE_TYPE (op0)), |
6702 | !(complain & tf_error)) |
6703 | || !verify_type_context (loc, TCTX_POINTER_ARITH, |
6704 | TREE_TYPE (TREE_TYPE (op1)), |
6705 | !(complain & tf_error))) |
6706 | return error_mark_node; |
6707 | |
6708 | /* Determine integer type result of the subtraction. This will usually |
6709 | be the same as the result type (ptrdiff_t), but may need to be a wider |
6710 | type if pointers for the address space are wider than ptrdiff_t. */ |
6711 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) |
6712 | inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); |
6713 | else |
6714 | inttype = restype; |
6715 | |
6716 | if (!processing_template_decl |
6717 | && sanitize_flags_p (flag: SANITIZE_POINTER_SUBTRACT)) |
6718 | { |
6719 | op0 = save_expr (op0); |
6720 | op1 = save_expr (op1); |
6721 | |
6722 | tree tt = builtin_decl_explicit (fncode: BUILT_IN_ASAN_POINTER_SUBTRACT); |
6723 | *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1); |
6724 | } |
6725 | |
6726 | /* First do the subtraction, then build the divide operator |
6727 | and only convert at the very end. |
6728 | Do not do default conversions in case restype is a short type. */ |
6729 | |
6730 | /* POINTER_DIFF_EXPR requires a signed integer type of the same size as |
6731 | pointers. If some platform cannot provide that, or has a larger |
6732 | ptrdiff_type to support differences larger than half the address |
6733 | space, cast the pointers to some larger integer type and do the |
6734 | computations in that type. */ |
6735 | if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0))) |
6736 | op0 = cp_build_binary_op (location: loc, |
6737 | code: MINUS_EXPR, |
6738 | orig_op0: cp_convert (inttype, op0, complain), |
6739 | orig_op1: cp_convert (inttype, op1, complain), |
6740 | complain); |
6741 | else |
6742 | op0 = build2_loc (loc, code: POINTER_DIFF_EXPR, type: inttype, arg0: op0, arg1: op1); |
6743 | |
6744 | /* This generates an error if op1 is a pointer to an incomplete type. */ |
6745 | if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1)))) |
6746 | { |
6747 | if (complain & tf_error) |
6748 | error_at (loc, "invalid use of a pointer to an incomplete type in " |
6749 | "pointer arithmetic" ); |
6750 | else |
6751 | return error_mark_node; |
6752 | } |
6753 | |
6754 | if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1))) |
6755 | { |
6756 | if (complain & tf_error) |
6757 | error_at (loc, "arithmetic on pointer to an empty aggregate" ); |
6758 | else |
6759 | return error_mark_node; |
6760 | } |
6761 | |
6762 | op1 = (TYPE_PTROB_P (ptrtype) |
6763 | ? size_in_bytes_loc (loc, target_type) |
6764 | : integer_one_node); |
6765 | |
6766 | /* Do the division. */ |
6767 | |
6768 | result = build2_loc (loc, code: EXACT_DIV_EXPR, type: inttype, arg0: op0, |
6769 | arg1: cp_convert (inttype, op1, complain)); |
6770 | return cp_convert (restype, result, complain); |
6771 | } |
6772 | |
6773 | /* Construct and perhaps optimize a tree representation |
6774 | for a unary operation. CODE, a tree_code, specifies the operation |
6775 | and XARG is the operand. */ |
6776 | |
6777 | tree |
6778 | build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg, |
6779 | tree lookups, tsubst_flags_t complain) |
6780 | { |
6781 | tree orig_expr = xarg; |
6782 | tree exp; |
6783 | int ptrmem = 0; |
6784 | tree overload = NULL_TREE; |
6785 | |
6786 | if (processing_template_decl) |
6787 | { |
6788 | if (type_dependent_expression_p (xarg)) |
6789 | { |
6790 | tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE); |
6791 | TREE_TYPE (e) = build_dependent_operator_type (lookups, code, is_assign: false); |
6792 | return e; |
6793 | } |
6794 | } |
6795 | |
6796 | exp = NULL_TREE; |
6797 | |
6798 | /* [expr.unary.op] says: |
6799 | |
6800 | The address of an object of incomplete type can be taken. |
6801 | |
6802 | (And is just the ordinary address operator, not an overloaded |
6803 | "operator &".) However, if the type is a template |
6804 | specialization, we must complete the type at this point so that |
6805 | an overloaded "operator &" will be available if required. */ |
6806 | if (code == ADDR_EXPR |
6807 | && TREE_CODE (xarg) != TEMPLATE_ID_EXPR |
6808 | && ((CLASS_TYPE_P (TREE_TYPE (xarg)) |
6809 | && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg)))) |
6810 | || (TREE_CODE (xarg) == OFFSET_REF))) |
6811 | /* Don't look for a function. */; |
6812 | else |
6813 | exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE, |
6814 | NULL_TREE, lookups, &overload, complain); |
6815 | |
6816 | if (!exp && code == ADDR_EXPR) |
6817 | { |
6818 | if (is_overloaded_fn (xarg)) |
6819 | { |
6820 | tree fn = get_first_fn (xarg); |
6821 | if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn)) |
6822 | { |
6823 | if (complain & tf_error) |
6824 | error_at (loc, DECL_CONSTRUCTOR_P (fn) |
6825 | ? G_("taking address of constructor %qD" ) |
6826 | : G_("taking address of destructor %qD" ), |
6827 | fn); |
6828 | return error_mark_node; |
6829 | } |
6830 | } |
6831 | |
6832 | /* A pointer to member-function can be formed only by saying |
6833 | &X::mf. */ |
6834 | if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE |
6835 | && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg))) |
6836 | { |
6837 | if (TREE_CODE (xarg) != OFFSET_REF |
6838 | || !TYPE_P (TREE_OPERAND (xarg, 0))) |
6839 | { |
6840 | if (complain & tf_error) |
6841 | { |
6842 | error_at (loc, "invalid use of %qE to form a " |
6843 | "pointer-to-member-function" , xarg.get_value ()); |
6844 | if (TREE_CODE (xarg) != OFFSET_REF) |
6845 | inform (loc, " a qualified-id is required" ); |
6846 | } |
6847 | return error_mark_node; |
6848 | } |
6849 | else |
6850 | { |
6851 | if (complain & tf_error) |
6852 | error_at (loc, "parentheses around %qE cannot be used to " |
6853 | "form a pointer-to-member-function" , |
6854 | xarg.get_value ()); |
6855 | else |
6856 | return error_mark_node; |
6857 | PTRMEM_OK_P (xarg) = 1; |
6858 | } |
6859 | } |
6860 | |
6861 | if (TREE_CODE (xarg) == OFFSET_REF) |
6862 | { |
6863 | ptrmem = PTRMEM_OK_P (xarg); |
6864 | |
6865 | if (!ptrmem && !flag_ms_extensions |
6866 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE) |
6867 | { |
6868 | /* A single non-static member, make sure we don't allow a |
6869 | pointer-to-member. */ |
6870 | xarg = build2 (OFFSET_REF, TREE_TYPE (xarg), |
6871 | TREE_OPERAND (xarg, 0), |
6872 | ovl_make (TREE_OPERAND (xarg, 1))); |
6873 | PTRMEM_OK_P (xarg) = ptrmem; |
6874 | } |
6875 | } |
6876 | |
6877 | exp = cp_build_addr_expr_strict (xarg, complain); |
6878 | |
6879 | if (TREE_CODE (exp) == PTRMEM_CST) |
6880 | PTRMEM_CST_LOCATION (exp) = loc; |
6881 | else |
6882 | protected_set_expr_location (exp, loc); |
6883 | } |
6884 | |
6885 | if (processing_template_decl && exp != error_mark_node) |
6886 | { |
6887 | if (overload != NULL_TREE) |
6888 | return (build_min_non_dep_op_overload |
6889 | (code, exp, overload, orig_expr, integer_zero_node)); |
6890 | |
6891 | exp = build_min_non_dep (code, exp, orig_expr, |
6892 | /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE); |
6893 | } |
6894 | if (TREE_CODE (exp) == ADDR_EXPR) |
6895 | PTRMEM_OK_P (exp) = ptrmem; |
6896 | return exp; |
6897 | } |
6898 | |
6899 | /* Construct and perhaps optimize a tree representation |
6900 | for __builtin_addressof operation. ARG specifies the operand. */ |
6901 | |
6902 | tree |
6903 | cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain) |
6904 | { |
6905 | tree orig_expr = arg; |
6906 | |
6907 | if (processing_template_decl) |
6908 | { |
6909 | if (type_dependent_expression_p (arg)) |
6910 | return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE); |
6911 | } |
6912 | |
6913 | tree exp = cp_build_addr_expr_strict (arg, complain); |
6914 | |
6915 | if (processing_template_decl && exp != error_mark_node) |
6916 | exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE); |
6917 | return exp; |
6918 | } |
6919 | |
6920 | /* Like c_common_truthvalue_conversion, but handle pointer-to-member |
6921 | constants, where a null value is represented by an INTEGER_CST of |
6922 | -1. */ |
6923 | |
6924 | tree |
6925 | cp_truthvalue_conversion (tree expr, tsubst_flags_t complain) |
6926 | { |
6927 | tree type = TREE_TYPE (expr); |
6928 | location_t loc = cp_expr_loc_or_input_loc (t: expr); |
6929 | if (TYPE_PTR_OR_PTRMEM_P (type) |
6930 | /* Avoid ICE on invalid use of non-static member function. */ |
6931 | || TREE_CODE (expr) == FUNCTION_DECL) |
6932 | return cp_build_binary_op (location: loc, code: NE_EXPR, orig_op0: expr, nullptr_node, complain); |
6933 | else |
6934 | return c_common_truthvalue_conversion (loc, expr); |
6935 | } |
6936 | |
6937 | /* Returns EXPR contextually converted to bool. */ |
6938 | |
6939 | tree |
6940 | contextual_conv_bool (tree expr, tsubst_flags_t complain) |
6941 | { |
6942 | return perform_implicit_conversion_flags (boolean_type_node, expr, |
6943 | complain, LOOKUP_NORMAL); |
6944 | } |
6945 | |
6946 | /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This |
6947 | is a low-level function; most callers should use maybe_convert_cond. */ |
6948 | |
6949 | tree |
6950 | condition_conversion (tree expr) |
6951 | { |
6952 | tree t = contextual_conv_bool (expr, complain: tf_warning_or_error); |
6953 | if (!processing_template_decl) |
6954 | t = fold_build_cleanup_point_expr (boolean_type_node, expr: t); |
6955 | return t; |
6956 | } |
6957 | |
6958 | /* Returns the address of T. This function will fold away |
6959 | ADDR_EXPR of INDIRECT_REF. This is only for low-level usage; |
6960 | most places should use cp_build_addr_expr instead. */ |
6961 | |
6962 | tree |
6963 | build_address (tree t) |
6964 | { |
6965 | if (error_operand_p (t) || !cxx_mark_addressable (t)) |
6966 | return error_mark_node; |
6967 | gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR |
6968 | || processing_template_decl); |
6969 | t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t); |
6970 | if (TREE_CODE (t) != ADDR_EXPR) |
6971 | t = rvalue (t); |
6972 | return t; |
6973 | } |
6974 | |
6975 | /* Return a NOP_EXPR converting EXPR to TYPE. */ |
6976 | |
6977 | tree |
6978 | build_nop (tree type, tree expr) |
6979 | { |
6980 | if (type == error_mark_node || error_operand_p (t: expr)) |
6981 | return expr; |
6982 | return build1_loc (EXPR_LOCATION (expr), code: NOP_EXPR, type, arg1: expr); |
6983 | } |
6984 | |
6985 | /* Take the address of ARG, whatever that means under C++ semantics. |
6986 | If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues |
6987 | and class rvalues as well. |
6988 | |
6989 | Nothing should call this function directly; instead, callers should use |
6990 | cp_build_addr_expr or cp_build_addr_expr_strict. */ |
6991 | |
6992 | static tree |
6993 | cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain) |
6994 | { |
6995 | tree argtype; |
6996 | tree val; |
6997 | |
6998 | if (!arg || error_operand_p (t: arg)) |
6999 | return error_mark_node; |
7000 | |
7001 | arg = mark_lvalue_use (arg); |
7002 | if (error_operand_p (t: arg)) |
7003 | return error_mark_node; |
7004 | |
7005 | argtype = lvalue_type (arg); |
7006 | location_t loc = cp_expr_loc_or_input_loc (t: arg); |
7007 | |
7008 | gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg))); |
7009 | |
7010 | if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (expr: arg) |
7011 | && !really_overloaded_fn (arg)) |
7012 | { |
7013 | /* They're trying to take the address of a unique non-static |
7014 | member function. This is ill-formed (except in MS-land), |
7015 | but let's try to DTRT. |
7016 | Note: We only handle unique functions here because we don't |
7017 | want to complain if there's a static overload; non-unique |
7018 | cases will be handled by instantiate_type. But we need to |
7019 | handle this case here to allow casts on the resulting PMF. |
7020 | We could defer this in non-MS mode, but it's easier to give |
7021 | a useful error here. */ |
7022 | |
7023 | /* Inside constant member functions, the `this' pointer |
7024 | contains an extra const qualifier. TYPE_MAIN_VARIANT |
7025 | is used here to remove this const from the diagnostics |
7026 | and the created OFFSET_REF. */ |
7027 | tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0))); |
7028 | tree fn = get_first_fn (TREE_OPERAND (arg, 1)); |
7029 | if (!mark_used (fn, complain) && !(complain & tf_error)) |
7030 | return error_mark_node; |
7031 | |
7032 | if (! flag_ms_extensions) |
7033 | { |
7034 | tree name = DECL_NAME (fn); |
7035 | if (!(complain & tf_error)) |
7036 | return error_mark_node; |
7037 | else if (current_class_type |
7038 | && TREE_OPERAND (arg, 0) == current_class_ref) |
7039 | /* An expression like &memfn. */ |
7040 | permerror (loc, |
7041 | "ISO C++ forbids taking the address of an unqualified" |
7042 | " or parenthesized non-static member function to form" |
7043 | " a pointer to member function. Say %<&%T::%D%>" , |
7044 | base, name); |
7045 | else |
7046 | permerror (loc, |
7047 | "ISO C++ forbids taking the address of a bound member" |
7048 | " function to form a pointer to member function." |
7049 | " Say %<&%T::%D%>" , |
7050 | base, name); |
7051 | } |
7052 | arg = build_offset_ref (base, fn, /*address_p=*/true, complain); |
7053 | } |
7054 | |
7055 | /* Uninstantiated types are all functions. Taking the |
7056 | address of a function is a no-op, so just return the |
7057 | argument. */ |
7058 | if (type_unknown_p (expr: arg)) |
7059 | return build1 (ADDR_EXPR, unknown_type_node, arg); |
7060 | |
7061 | if (TREE_CODE (arg) == OFFSET_REF) |
7062 | /* We want a pointer to member; bypass all the code for actually taking |
7063 | the address of something. */ |
7064 | goto offset_ref; |
7065 | |
7066 | /* Anything not already handled and not a true memory reference |
7067 | is an error. */ |
7068 | if (!FUNC_OR_METHOD_TYPE_P (argtype)) |
7069 | { |
7070 | cp_lvalue_kind kind = lvalue_kind (arg); |
7071 | if (kind == clk_none) |
7072 | { |
7073 | if (complain & tf_error) |
7074 | lvalue_error (loc, lv_addressof); |
7075 | return error_mark_node; |
7076 | } |
7077 | if (strict_lvalue && (kind & (clk_rvalueref|clk_class))) |
7078 | { |
7079 | if (!(complain & tf_error)) |
7080 | return error_mark_node; |
7081 | /* Make this a permerror because we used to accept it. */ |
7082 | permerror (loc, "taking address of rvalue" ); |
7083 | } |
7084 | } |
7085 | |
7086 | if (TYPE_REF_P (argtype)) |
7087 | { |
7088 | tree type = build_pointer_type (TREE_TYPE (argtype)); |
7089 | arg = build1 (CONVERT_EXPR, type, arg); |
7090 | return arg; |
7091 | } |
7092 | else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg))) |
7093 | { |
7094 | /* ARM $3.4 */ |
7095 | /* Apparently a lot of autoconf scripts for C++ packages do this, |
7096 | so only complain if -Wpedantic. */ |
7097 | if (complain & (flag_pedantic_errors ? tf_error : tf_warning)) |
7098 | pedwarn (loc, OPT_Wpedantic, |
7099 | "ISO C++ forbids taking address of function %<::main%>" ); |
7100 | else if (flag_pedantic_errors) |
7101 | return error_mark_node; |
7102 | } |
7103 | |
7104 | /* Let &* cancel out to simplify resulting code. */ |
7105 | if (INDIRECT_REF_P (arg)) |
7106 | { |
7107 | arg = TREE_OPERAND (arg, 0); |
7108 | if (TYPE_REF_P (TREE_TYPE (arg))) |
7109 | { |
7110 | tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg))); |
7111 | arg = build1 (CONVERT_EXPR, type, arg); |
7112 | } |
7113 | else |
7114 | /* Don't let this be an lvalue. */ |
7115 | arg = rvalue (arg); |
7116 | return arg; |
7117 | } |
7118 | |
7119 | /* Handle complex lvalues (when permitted) |
7120 | by reduction to simpler cases. */ |
7121 | val = unary_complex_lvalue (ADDR_EXPR, arg); |
7122 | if (val != 0) |
7123 | return val; |
7124 | |
7125 | switch (TREE_CODE (arg)) |
7126 | { |
7127 | CASE_CONVERT: |
7128 | case FLOAT_EXPR: |
7129 | case FIX_TRUNC_EXPR: |
7130 | /* We should have handled this above in the lvalue_kind check. */ |
7131 | gcc_unreachable (); |
7132 | break; |
7133 | |
7134 | case BASELINK: |
7135 | arg = BASELINK_FUNCTIONS (arg); |
7136 | /* Fall through. */ |
7137 | |
7138 | case OVERLOAD: |
7139 | arg = OVL_FIRST (arg); |
7140 | break; |
7141 | |
7142 | case OFFSET_REF: |
7143 | offset_ref: |
7144 | /* Turn a reference to a non-static data member into a |
7145 | pointer-to-member. */ |
7146 | { |
7147 | tree type; |
7148 | tree t; |
7149 | |
7150 | gcc_assert (PTRMEM_OK_P (arg)); |
7151 | |
7152 | t = TREE_OPERAND (arg, 1); |
7153 | if (TYPE_REF_P (TREE_TYPE (t))) |
7154 | { |
7155 | if (complain & tf_error) |
7156 | error_at (loc, |
7157 | "cannot create pointer to reference member %qD" , t); |
7158 | return error_mark_node; |
7159 | } |
7160 | |
7161 | /* Forming a pointer-to-member is a use of non-pure-virtual fns. */ |
7162 | if (TREE_CODE (t) == FUNCTION_DECL |
7163 | && !DECL_PURE_VIRTUAL_P (t) |
7164 | && !mark_used (t, complain) && !(complain & tf_error)) |
7165 | return error_mark_node; |
7166 | |
7167 | type = build_ptrmem_type (context_for_name_lookup (t), |
7168 | TREE_TYPE (t)); |
7169 | t = make_ptrmem_cst (type, t); |
7170 | return t; |
7171 | } |
7172 | |
7173 | default: |
7174 | break; |
7175 | } |
7176 | |
7177 | if (argtype != error_mark_node) |
7178 | argtype = build_pointer_type (argtype); |
7179 | |
7180 | if (bitfield_p (arg)) |
7181 | { |
7182 | if (complain & tf_error) |
7183 | error_at (loc, "attempt to take address of bit-field" ); |
7184 | return error_mark_node; |
7185 | } |
7186 | |
7187 | /* In a template, we are processing a non-dependent expression |
7188 | so we can just form an ADDR_EXPR with the correct type. */ |
7189 | if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF) |
7190 | { |
7191 | if (!mark_single_function (arg, complain)) |
7192 | return error_mark_node; |
7193 | val = build_address (t: arg); |
7194 | if (TREE_CODE (arg) == OFFSET_REF) |
7195 | PTRMEM_OK_P (val) = PTRMEM_OK_P (arg); |
7196 | } |
7197 | else if (BASELINK_P (TREE_OPERAND (arg, 1))) |
7198 | { |
7199 | tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1)); |
7200 | |
7201 | /* We can only get here with a single static member |
7202 | function. */ |
7203 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL |
7204 | && DECL_STATIC_FUNCTION_P (fn)); |
7205 | if (!mark_used (fn, complain) && !(complain & tf_error)) |
7206 | return error_mark_node; |
7207 | val = build_address (t: fn); |
7208 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0))) |
7209 | /* Do not lose object's side effects. */ |
7210 | val = build2 (COMPOUND_EXPR, TREE_TYPE (val), |
7211 | TREE_OPERAND (arg, 0), val); |
7212 | } |
7213 | else |
7214 | { |
7215 | tree object = TREE_OPERAND (arg, 0); |
7216 | tree field = TREE_OPERAND (arg, 1); |
7217 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
7218 | (TREE_TYPE (object), decl_type_context (field))); |
7219 | val = build_address (t: arg); |
7220 | } |
7221 | |
7222 | if (TYPE_PTR_P (argtype) |
7223 | && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE) |
7224 | { |
7225 | build_ptrmemfunc_type (argtype); |
7226 | val = build_ptrmemfunc (argtype, val, 0, |
7227 | /*c_cast_p=*/false, |
7228 | complain); |
7229 | } |
7230 | |
7231 | /* For addresses of immediate functions ensure we have EXPR_LOCATION |
7232 | set for possible later diagnostics. */ |
7233 | if (TREE_CODE (val) == ADDR_EXPR |
7234 | && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL |
7235 | && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (val, 0))) |
7236 | SET_EXPR_LOCATION (val, input_location); |
7237 | |
7238 | return val; |
7239 | } |
7240 | |
7241 | /* Take the address of ARG if it has one, even if it's an rvalue. */ |
7242 | |
7243 | tree |
7244 | cp_build_addr_expr (tree arg, tsubst_flags_t complain) |
7245 | { |
7246 | return cp_build_addr_expr_1 (arg, strict_lvalue: 0, complain); |
7247 | } |
7248 | |
7249 | /* Take the address of ARG, but only if it's an lvalue. */ |
7250 | |
7251 | static tree |
7252 | cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain) |
7253 | { |
7254 | return cp_build_addr_expr_1 (arg, strict_lvalue: 1, complain); |
7255 | } |
7256 | |
7257 | /* C++: Must handle pointers to members. |
7258 | |
7259 | Perhaps type instantiation should be extended to handle conversion |
7260 | from aggregates to types we don't yet know we want? (Or are those |
7261 | cases typically errors which should be reported?) |
7262 | |
7263 | NOCONVERT suppresses the default promotions (such as from short to int). */ |
7264 | |
7265 | tree |
7266 | cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert, |
7267 | tsubst_flags_t complain) |
7268 | { |
7269 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ |
7270 | tree arg = xarg; |
7271 | location_t location = cp_expr_loc_or_input_loc (t: arg); |
7272 | tree argtype = 0; |
7273 | tree eptype = NULL_TREE; |
7274 | const char *errstring = NULL; |
7275 | tree val; |
7276 | const char *invalid_op_diag; |
7277 | |
7278 | if (!arg || error_operand_p (t: arg)) |
7279 | return error_mark_node; |
7280 | |
7281 | arg = resolve_nondeduced_context (arg, complain); |
7282 | |
7283 | if ((invalid_op_diag |
7284 | = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR |
7285 | ? CONVERT_EXPR |
7286 | : code), |
7287 | TREE_TYPE (arg)))) |
7288 | { |
7289 | if (complain & tf_error) |
7290 | error (invalid_op_diag); |
7291 | return error_mark_node; |
7292 | } |
7293 | |
7294 | if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) |
7295 | { |
7296 | eptype = TREE_TYPE (arg); |
7297 | arg = TREE_OPERAND (arg, 0); |
7298 | } |
7299 | |
7300 | switch (code) |
7301 | { |
7302 | case UNARY_PLUS_EXPR: |
7303 | case NEGATE_EXPR: |
7304 | { |
7305 | int flags = WANT_ARITH | WANT_ENUM; |
7306 | /* Unary plus (but not unary minus) is allowed on pointers. */ |
7307 | if (code == UNARY_PLUS_EXPR) |
7308 | flags |= WANT_POINTER; |
7309 | arg = build_expr_type_conversion (flags, arg, true); |
7310 | if (!arg) |
7311 | errstring = (code == NEGATE_EXPR |
7312 | ? _("wrong type argument to unary minus" ) |
7313 | : _("wrong type argument to unary plus" )); |
7314 | else |
7315 | { |
7316 | if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg))) |
7317 | arg = cp_perform_integral_promotions (expr: arg, complain); |
7318 | |
7319 | /* Make sure the result is not an lvalue: a unary plus or minus |
7320 | expression is always a rvalue. */ |
7321 | arg = rvalue (arg); |
7322 | } |
7323 | } |
7324 | break; |
7325 | |
7326 | case BIT_NOT_EXPR: |
7327 | if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) |
7328 | { |
7329 | code = CONJ_EXPR; |
7330 | if (!noconvert) |
7331 | { |
7332 | arg = cp_default_conversion (exp: arg, complain); |
7333 | if (arg == error_mark_node) |
7334 | return error_mark_node; |
7335 | } |
7336 | } |
7337 | else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM |
7338 | | WANT_VECTOR_OR_COMPLEX, |
7339 | arg, true))) |
7340 | errstring = _("wrong type argument to bit-complement" ); |
7341 | else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg))) |
7342 | { |
7343 | /* Warn if the expression has boolean value. */ |
7344 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE |
7345 | && (complain & tf_warning) |
7346 | && warning_at (location, OPT_Wbool_operation, |
7347 | "%<~%> on an expression of type %<bool%>" )) |
7348 | inform (location, "did you mean to use logical not (%<!%>)?" ); |
7349 | arg = cp_perform_integral_promotions (expr: arg, complain); |
7350 | } |
7351 | else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg))) |
7352 | arg = mark_rvalue_use (arg); |
7353 | break; |
7354 | |
7355 | case ABS_EXPR: |
7356 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) |
7357 | errstring = _("wrong type argument to abs" ); |
7358 | else if (!noconvert) |
7359 | { |
7360 | arg = cp_default_conversion (exp: arg, complain); |
7361 | if (arg == error_mark_node) |
7362 | return error_mark_node; |
7363 | } |
7364 | break; |
7365 | |
7366 | case CONJ_EXPR: |
7367 | /* Conjugating a real value is a no-op, but allow it anyway. */ |
7368 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) |
7369 | errstring = _("wrong type argument to conjugation" ); |
7370 | else if (!noconvert) |
7371 | { |
7372 | arg = cp_default_conversion (exp: arg, complain); |
7373 | if (arg == error_mark_node) |
7374 | return error_mark_node; |
7375 | } |
7376 | break; |
7377 | |
7378 | case TRUTH_NOT_EXPR: |
7379 | if (gnu_vector_type_p (TREE_TYPE (arg))) |
7380 | return cp_build_binary_op (location: input_location, code: EQ_EXPR, orig_op0: arg, |
7381 | orig_op1: build_zero_cst (TREE_TYPE (arg)), complain); |
7382 | arg = perform_implicit_conversion (boolean_type_node, arg, |
7383 | complain); |
7384 | if (arg != error_mark_node) |
7385 | { |
7386 | if (processing_template_decl) |
7387 | return build1_loc (loc: location, code: TRUTH_NOT_EXPR, boolean_type_node, arg1: arg); |
7388 | val = invert_truthvalue_loc (location, arg); |
7389 | if (obvalue_p (val)) |
7390 | val = non_lvalue_loc (location, val); |
7391 | return val; |
7392 | } |
7393 | errstring = _("in argument to unary !" ); |
7394 | break; |
7395 | |
7396 | case NOP_EXPR: |
7397 | break; |
7398 | |
7399 | case REALPART_EXPR: |
7400 | case IMAGPART_EXPR: |
7401 | val = build_real_imag_expr (input_location, code, arg); |
7402 | if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR) |
7403 | val = build1_loc (loc: input_location, code: EXCESS_PRECISION_EXPR, |
7404 | TREE_TYPE (eptype), arg1: val); |
7405 | return val; |
7406 | |
7407 | case PREINCREMENT_EXPR: |
7408 | case POSTINCREMENT_EXPR: |
7409 | case PREDECREMENT_EXPR: |
7410 | case POSTDECREMENT_EXPR: |
7411 | /* Handle complex lvalues (when permitted) |
7412 | by reduction to simpler cases. */ |
7413 | |
7414 | val = unary_complex_lvalue (code, arg); |
7415 | if (val != 0) |
7416 | goto return_build_unary_op; |
7417 | |
7418 | arg = mark_lvalue_use (arg); |
7419 | |
7420 | /* Increment or decrement the real part of the value, |
7421 | and don't change the imaginary part. */ |
7422 | if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) |
7423 | { |
7424 | tree real, imag; |
7425 | |
7426 | arg = cp_stabilize_reference (arg); |
7427 | real = cp_build_unary_op (code: REALPART_EXPR, xarg: arg, noconvert: true, complain); |
7428 | imag = cp_build_unary_op (code: IMAGPART_EXPR, xarg: arg, noconvert: true, complain); |
7429 | real = cp_build_unary_op (code, xarg: real, noconvert: true, complain); |
7430 | if (real == error_mark_node || imag == error_mark_node) |
7431 | return error_mark_node; |
7432 | val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag); |
7433 | goto return_build_unary_op; |
7434 | } |
7435 | |
7436 | /* Report invalid types. */ |
7437 | |
7438 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER, |
7439 | arg, true))) |
7440 | { |
7441 | if (code == PREINCREMENT_EXPR) |
7442 | errstring = _("no pre-increment operator for type" ); |
7443 | else if (code == POSTINCREMENT_EXPR) |
7444 | errstring = _("no post-increment operator for type" ); |
7445 | else if (code == PREDECREMENT_EXPR) |
7446 | errstring = _("no pre-decrement operator for type" ); |
7447 | else |
7448 | errstring = _("no post-decrement operator for type" ); |
7449 | break; |
7450 | } |
7451 | else if (arg == error_mark_node) |
7452 | return error_mark_node; |
7453 | |
7454 | /* Report something read-only. */ |
7455 | |
7456 | if (CP_TYPE_CONST_P (TREE_TYPE (arg)) |
7457 | || TREE_READONLY (arg)) |
7458 | { |
7459 | if (complain & tf_error) |
7460 | cxx_readonly_error (location, arg, |
7461 | ((code == PREINCREMENT_EXPR |
7462 | || code == POSTINCREMENT_EXPR) |
7463 | ? lv_increment : lv_decrement)); |
7464 | else |
7465 | return error_mark_node; |
7466 | } |
7467 | |
7468 | { |
7469 | tree inc; |
7470 | tree declared_type = unlowered_expr_type (exp: arg); |
7471 | |
7472 | argtype = TREE_TYPE (arg); |
7473 | |
7474 | /* ARM $5.2.5 last annotation says this should be forbidden. */ |
7475 | if (TREE_CODE (argtype) == ENUMERAL_TYPE) |
7476 | { |
7477 | if (complain & tf_error) |
7478 | permerror (location, (code == PREINCREMENT_EXPR |
7479 | || code == POSTINCREMENT_EXPR) |
7480 | ? G_("ISO C++ forbids incrementing an enum" ) |
7481 | : G_("ISO C++ forbids decrementing an enum" )); |
7482 | else |
7483 | return error_mark_node; |
7484 | } |
7485 | |
7486 | /* Compute the increment. */ |
7487 | |
7488 | if (TYPE_PTR_P (argtype)) |
7489 | { |
7490 | tree type = complete_type (TREE_TYPE (argtype)); |
7491 | |
7492 | if (!COMPLETE_OR_VOID_TYPE_P (type)) |
7493 | { |
7494 | if (complain & tf_error) |
7495 | error_at (location, ((code == PREINCREMENT_EXPR |
7496 | || code == POSTINCREMENT_EXPR)) |
7497 | ? G_("cannot increment a pointer to incomplete " |
7498 | "type %qT" ) |
7499 | : G_("cannot decrement a pointer to incomplete " |
7500 | "type %qT" ), |
7501 | TREE_TYPE (argtype)); |
7502 | else |
7503 | return error_mark_node; |
7504 | } |
7505 | else if (!TYPE_PTROB_P (argtype)) |
7506 | { |
7507 | if (complain & tf_error) |
7508 | pedwarn (location, OPT_Wpointer_arith, |
7509 | (code == PREINCREMENT_EXPR |
7510 | || code == POSTINCREMENT_EXPR) |
7511 | ? G_("ISO C++ forbids incrementing a pointer " |
7512 | "of type %qT" ) |
7513 | : G_("ISO C++ forbids decrementing a pointer " |
7514 | "of type %qT" ), |
7515 | argtype); |
7516 | else |
7517 | return error_mark_node; |
7518 | } |
7519 | else if (!verify_type_context (location, TCTX_POINTER_ARITH, |
7520 | TREE_TYPE (argtype), |
7521 | !(complain & tf_error))) |
7522 | return error_mark_node; |
7523 | |
7524 | inc = cxx_sizeof_nowarn (TREE_TYPE (argtype)); |
7525 | } |
7526 | else |
7527 | inc = VECTOR_TYPE_P (argtype) |
7528 | ? build_one_cst (argtype) |
7529 | : integer_one_node; |
7530 | |
7531 | inc = cp_convert (argtype, inc, complain); |
7532 | |
7533 | /* If 'arg' is an Objective-C PROPERTY_REF expression, then we |
7534 | need to ask Objective-C to build the increment or decrement |
7535 | expression for it. */ |
7536 | if (objc_is_property_ref (arg)) |
7537 | return objc_build_incr_expr_for_property_ref (input_location, code, |
7538 | arg, inc); |
7539 | |
7540 | /* Complain about anything else that is not a true lvalue. */ |
7541 | if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR |
7542 | || code == POSTINCREMENT_EXPR) |
7543 | ? lv_increment : lv_decrement), |
7544 | complain)) |
7545 | return error_mark_node; |
7546 | |
7547 | /* [depr.volatile.type] "Postfix ++ and -- expressions and |
7548 | prefix ++ and -- expressions of volatile-qualified arithmetic |
7549 | and pointer types are deprecated." */ |
7550 | if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg))) |
7551 | && (complain & tf_warning)) |
7552 | warning_at (location, OPT_Wvolatile, |
7553 | "%qs expression of %<volatile%>-qualified type is " |
7554 | "deprecated" , |
7555 | ((code == PREINCREMENT_EXPR |
7556 | || code == POSTINCREMENT_EXPR) |
7557 | ? "++" : "--" )); |
7558 | |
7559 | /* Forbid using -- or ++ in C++17 on `bool'. */ |
7560 | if (TREE_CODE (declared_type) == BOOLEAN_TYPE) |
7561 | { |
7562 | if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR) |
7563 | { |
7564 | if (complain & tf_error) |
7565 | error_at (location, |
7566 | "use of an operand of type %qT in %<operator--%> " |
7567 | "is forbidden" , boolean_type_node); |
7568 | return error_mark_node; |
7569 | } |
7570 | else |
7571 | { |
7572 | if (cxx_dialect >= cxx17) |
7573 | { |
7574 | if (complain & tf_error) |
7575 | error_at (location, |
7576 | "use of an operand of type %qT in " |
7577 | "%<operator++%> is forbidden in C++17" , |
7578 | boolean_type_node); |
7579 | return error_mark_node; |
7580 | } |
7581 | /* Otherwise, [depr.incr.bool] says this is deprecated. */ |
7582 | else if (complain & tf_warning) |
7583 | warning_at (location, OPT_Wdeprecated, |
7584 | "use of an operand of type %qT " |
7585 | "in %<operator++%> is deprecated" , |
7586 | boolean_type_node); |
7587 | } |
7588 | val = boolean_increment (code, arg); |
7589 | } |
7590 | else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR) |
7591 | /* An rvalue has no cv-qualifiers. */ |
7592 | val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc); |
7593 | else |
7594 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
7595 | |
7596 | TREE_SIDE_EFFECTS (val) = 1; |
7597 | goto return_build_unary_op; |
7598 | } |
7599 | |
7600 | case ADDR_EXPR: |
7601 | /* Note that this operation never does default_conversion |
7602 | regardless of NOCONVERT. */ |
7603 | return cp_build_addr_expr (arg, complain); |
7604 | |
7605 | default: |
7606 | break; |
7607 | } |
7608 | |
7609 | if (!errstring) |
7610 | { |
7611 | if (argtype == 0) |
7612 | argtype = TREE_TYPE (arg); |
7613 | val = build1 (code, argtype, arg); |
7614 | return_build_unary_op: |
7615 | if (eptype) |
7616 | val = build1 (EXCESS_PRECISION_EXPR, eptype, val); |
7617 | return val; |
7618 | } |
7619 | |
7620 | if (complain & tf_error) |
7621 | error_at (location, "%s" , errstring); |
7622 | return error_mark_node; |
7623 | } |
7624 | |
7625 | /* Hook for the c-common bits that build a unary op. */ |
7626 | tree |
7627 | build_unary_op (location_t /*location*/, |
7628 | enum tree_code code, tree xarg, bool noconvert) |
7629 | { |
7630 | return cp_build_unary_op (code, xarg, noconvert, complain: tf_warning_or_error); |
7631 | } |
7632 | |
7633 | /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR, |
7634 | so that it is a valid lvalue even for GENERIC by replacing |
7635 | (lhs = rhs) with ((lhs = rhs), lhs) |
7636 | (--lhs) with ((--lhs), lhs) |
7637 | (++lhs) with ((++lhs), lhs) |
7638 | and if lhs has side-effects, calling cp_stabilize_reference on it, so |
7639 | that it can be evaluated multiple times. */ |
7640 | |
7641 | tree |
7642 | genericize_compound_lvalue (tree lvalue) |
7643 | { |
7644 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0))) |
7645 | lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue), |
7646 | cp_stabilize_reference (TREE_OPERAND (lvalue, 0)), |
7647 | TREE_OPERAND (lvalue, 1)); |
7648 | return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)), |
7649 | lvalue, TREE_OPERAND (lvalue, 0)); |
7650 | } |
7651 | |
7652 | /* Apply unary lvalue-demanding operator CODE to the expression ARG |
7653 | for certain kinds of expressions which are not really lvalues |
7654 | but which we can accept as lvalues. |
7655 | |
7656 | If ARG is not a kind of expression we can handle, return |
7657 | NULL_TREE. */ |
7658 | |
7659 | tree |
7660 | unary_complex_lvalue (enum tree_code code, tree arg) |
7661 | { |
7662 | /* Inside a template, making these kinds of adjustments is |
7663 | pointless; we are only concerned with the type of the |
7664 | expression. */ |
7665 | if (processing_template_decl) |
7666 | return NULL_TREE; |
7667 | |
7668 | /* Handle (a, b) used as an "lvalue". */ |
7669 | if (TREE_CODE (arg) == COMPOUND_EXPR) |
7670 | { |
7671 | tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), noconvert: false, |
7672 | complain: tf_warning_or_error); |
7673 | return build2 (COMPOUND_EXPR, TREE_TYPE (real_result), |
7674 | TREE_OPERAND (arg, 0), real_result); |
7675 | } |
7676 | |
7677 | /* Handle (a ? b : c) used as an "lvalue". */ |
7678 | if (TREE_CODE (arg) == COND_EXPR |
7679 | || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR) |
7680 | return rationalize_conditional_expr (code, t: arg, complain: tf_warning_or_error); |
7681 | |
7682 | /* Handle (a = b), (++a), and (--a) used as an "lvalue". */ |
7683 | if (TREE_CODE (arg) == MODIFY_EXPR |
7684 | || TREE_CODE (arg) == PREINCREMENT_EXPR |
7685 | || TREE_CODE (arg) == PREDECREMENT_EXPR) |
7686 | return unary_complex_lvalue (code, arg: genericize_compound_lvalue (lvalue: arg)); |
7687 | |
7688 | if (code != ADDR_EXPR) |
7689 | return NULL_TREE; |
7690 | |
7691 | /* Handle (a = b) used as an "lvalue" for `&'. */ |
7692 | if (TREE_CODE (arg) == MODIFY_EXPR |
7693 | || TREE_CODE (arg) == INIT_EXPR) |
7694 | { |
7695 | tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), noconvert: false, |
7696 | complain: tf_warning_or_error); |
7697 | arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result), |
7698 | arg, real_result); |
7699 | suppress_warning (arg /* What warning? */); |
7700 | return arg; |
7701 | } |
7702 | |
7703 | if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg)) |
7704 | || TREE_CODE (arg) == OFFSET_REF) |
7705 | return NULL_TREE; |
7706 | |
7707 | /* We permit compiler to make function calls returning |
7708 | objects of aggregate type look like lvalues. */ |
7709 | { |
7710 | tree targ = arg; |
7711 | |
7712 | if (TREE_CODE (targ) == SAVE_EXPR) |
7713 | targ = TREE_OPERAND (targ, 0); |
7714 | |
7715 | if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ))) |
7716 | { |
7717 | if (TREE_CODE (arg) == SAVE_EXPR) |
7718 | targ = arg; |
7719 | else |
7720 | targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error); |
7721 | return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ); |
7722 | } |
7723 | |
7724 | if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ)) |
7725 | return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)), |
7726 | TREE_OPERAND (targ, 0), current_function_decl, NULL); |
7727 | } |
7728 | |
7729 | /* Don't let anything else be handled specially. */ |
7730 | return NULL_TREE; |
7731 | } |
7732 | |
7733 | /* Mark EXP saying that we need to be able to take the |
7734 | address of it; it should not be allocated in a register. |
7735 | Value is true if successful. ARRAY_REF_P is true if this |
7736 | is for ARRAY_REF construction - in that case we don't want |
7737 | to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE, |
7738 | it is fine to use ARRAY_REFs for vector subscripts on vector |
7739 | register variables. |
7740 | |
7741 | C++: we do not allow `current_class_ptr' to be addressable. */ |
7742 | |
7743 | bool |
7744 | cxx_mark_addressable (tree exp, bool array_ref_p) |
7745 | { |
7746 | tree x = exp; |
7747 | |
7748 | while (1) |
7749 | switch (TREE_CODE (x)) |
7750 | { |
7751 | case VIEW_CONVERT_EXPR: |
7752 | if (array_ref_p |
7753 | && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE |
7754 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0)))) |
7755 | return true; |
7756 | x = TREE_OPERAND (x, 0); |
7757 | break; |
7758 | |
7759 | case COMPONENT_REF: |
7760 | if (bitfield_p (x)) |
7761 | error ("attempt to take address of bit-field" ); |
7762 | /* FALLTHRU */ |
7763 | case ADDR_EXPR: |
7764 | case ARRAY_REF: |
7765 | case REALPART_EXPR: |
7766 | case IMAGPART_EXPR: |
7767 | x = TREE_OPERAND (x, 0); |
7768 | break; |
7769 | |
7770 | case PARM_DECL: |
7771 | if (x == current_class_ptr) |
7772 | { |
7773 | error ("cannot take the address of %<this%>, which is an rvalue expression" ); |
7774 | TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */ |
7775 | return true; |
7776 | } |
7777 | /* Fall through. */ |
7778 | |
7779 | case VAR_DECL: |
7780 | /* Caller should not be trying to mark initialized |
7781 | constant fields addressable. */ |
7782 | gcc_assert (DECL_LANG_SPECIFIC (x) == 0 |
7783 | || DECL_IN_AGGR_P (x) == 0 |
7784 | || TREE_STATIC (x) |
7785 | || DECL_EXTERNAL (x)); |
7786 | /* Fall through. */ |
7787 | |
7788 | case RESULT_DECL: |
7789 | if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) |
7790 | && !DECL_ARTIFICIAL (x)) |
7791 | { |
7792 | if (VAR_P (x) && DECL_HARD_REGISTER (x)) |
7793 | { |
7794 | error |
7795 | ("address of explicit register variable %qD requested" , x); |
7796 | return false; |
7797 | } |
7798 | else if (extra_warnings) |
7799 | warning |
7800 | (OPT_Wextra, "address requested for %qD, which is declared %<register%>" , x); |
7801 | } |
7802 | TREE_ADDRESSABLE (x) = 1; |
7803 | return true; |
7804 | |
7805 | case CONST_DECL: |
7806 | case FUNCTION_DECL: |
7807 | TREE_ADDRESSABLE (x) = 1; |
7808 | return true; |
7809 | |
7810 | case CONSTRUCTOR: |
7811 | TREE_ADDRESSABLE (x) = 1; |
7812 | return true; |
7813 | |
7814 | case TARGET_EXPR: |
7815 | TREE_ADDRESSABLE (x) = 1; |
7816 | cxx_mark_addressable (TREE_OPERAND (x, 0)); |
7817 | return true; |
7818 | |
7819 | default: |
7820 | return true; |
7821 | } |
7822 | } |
7823 | |
7824 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. */ |
7825 | |
7826 | tree |
7827 | build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2, |
7828 | tsubst_flags_t complain) |
7829 | { |
7830 | tree orig_ifexp = ifexp; |
7831 | tree orig_op1 = op1; |
7832 | tree orig_op2 = op2; |
7833 | tree expr; |
7834 | |
7835 | if (processing_template_decl) |
7836 | { |
7837 | /* The standard says that the expression is type-dependent if |
7838 | IFEXP is type-dependent, even though the eventual type of the |
7839 | expression doesn't dependent on IFEXP. */ |
7840 | if (type_dependent_expression_p (ifexp) |
7841 | /* As a GNU extension, the middle operand may be omitted. */ |
7842 | || (op1 && type_dependent_expression_p (op1)) |
7843 | || type_dependent_expression_p (op2)) |
7844 | return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2); |
7845 | } |
7846 | |
7847 | expr = build_conditional_expr (loc, ifexp, op1, op2, complain); |
7848 | if (processing_template_decl && expr != error_mark_node) |
7849 | { |
7850 | tree min = build_min_non_dep (COND_EXPR, expr, |
7851 | orig_ifexp, orig_op1, orig_op2); |
7852 | expr = convert_from_reference (min); |
7853 | } |
7854 | return expr; |
7855 | } |
7856 | |
7857 | /* Given a list of expressions, return a compound expression |
7858 | that performs them all and returns the value of the last of them. */ |
7859 | |
7860 | tree |
7861 | build_x_compound_expr_from_list (tree list, expr_list_kind exp, |
7862 | tsubst_flags_t complain) |
7863 | { |
7864 | tree expr = TREE_VALUE (list); |
7865 | |
7866 | if (BRACE_ENCLOSED_INITIALIZER_P (expr) |
7867 | && !CONSTRUCTOR_IS_DIRECT_INIT (expr)) |
7868 | { |
7869 | if (complain & tf_error) |
7870 | pedwarn (cp_expr_loc_or_input_loc (t: expr), 0, |
7871 | "list-initializer for non-class type must not " |
7872 | "be parenthesized" ); |
7873 | else |
7874 | return error_mark_node; |
7875 | } |
7876 | |
7877 | if (TREE_CHAIN (list)) |
7878 | { |
7879 | if (complain & tf_error) |
7880 | switch (exp) |
7881 | { |
7882 | case ELK_INIT: |
7883 | permerror (input_location, "expression list treated as compound " |
7884 | "expression in initializer" ); |
7885 | break; |
7886 | case ELK_MEM_INIT: |
7887 | permerror (input_location, "expression list treated as compound " |
7888 | "expression in mem-initializer" ); |
7889 | break; |
7890 | case ELK_FUNC_CAST: |
7891 | permerror (input_location, "expression list treated as compound " |
7892 | "expression in functional cast" ); |
7893 | break; |
7894 | default: |
7895 | gcc_unreachable (); |
7896 | } |
7897 | else |
7898 | return error_mark_node; |
7899 | |
7900 | for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list)) |
7901 | expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)), |
7902 | expr, TREE_VALUE (list), NULL_TREE, |
7903 | complain); |
7904 | } |
7905 | |
7906 | return expr; |
7907 | } |
7908 | |
7909 | /* Like build_x_compound_expr_from_list, but using a VEC. */ |
7910 | |
7911 | tree |
7912 | build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg, |
7913 | tsubst_flags_t complain) |
7914 | { |
7915 | if (vec_safe_is_empty (v: vec)) |
7916 | return NULL_TREE; |
7917 | else if (vec->length () == 1) |
7918 | return (*vec)[0]; |
7919 | else |
7920 | { |
7921 | tree expr; |
7922 | unsigned int ix; |
7923 | tree t; |
7924 | |
7925 | if (msg != NULL) |
7926 | { |
7927 | if (complain & tf_error) |
7928 | permerror (input_location, |
7929 | "%s expression list treated as compound expression" , |
7930 | msg); |
7931 | else |
7932 | return error_mark_node; |
7933 | } |
7934 | |
7935 | expr = (*vec)[0]; |
7936 | for (ix = 1; vec->iterate (ix, ptr: &t); ++ix) |
7937 | expr = build_x_compound_expr (EXPR_LOCATION (t), expr, |
7938 | t, NULL_TREE, complain); |
7939 | |
7940 | return expr; |
7941 | } |
7942 | } |
7943 | |
7944 | /* Handle overloading of the ',' operator when needed. */ |
7945 | |
7946 | tree |
7947 | build_x_compound_expr (location_t loc, tree op1, tree op2, |
7948 | tree lookups, tsubst_flags_t complain) |
7949 | { |
7950 | tree result; |
7951 | tree orig_op1 = op1; |
7952 | tree orig_op2 = op2; |
7953 | tree overload = NULL_TREE; |
7954 | |
7955 | if (processing_template_decl) |
7956 | { |
7957 | if (type_dependent_expression_p (op1) |
7958 | || type_dependent_expression_p (op2)) |
7959 | { |
7960 | result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2); |
7961 | TREE_TYPE (result) |
7962 | = build_dependent_operator_type (lookups, code: COMPOUND_EXPR, is_assign: false); |
7963 | return result; |
7964 | } |
7965 | } |
7966 | |
7967 | result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, |
7968 | NULL_TREE, lookups, &overload, complain); |
7969 | if (!result) |
7970 | result = cp_build_compound_expr (op1, op2, complain); |
7971 | |
7972 | if (processing_template_decl && result != error_mark_node) |
7973 | { |
7974 | if (overload != NULL_TREE) |
7975 | return (build_min_non_dep_op_overload |
7976 | (COMPOUND_EXPR, result, overload, orig_op1, orig_op2)); |
7977 | |
7978 | return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2); |
7979 | } |
7980 | |
7981 | return result; |
7982 | } |
7983 | |
7984 | /* Like cp_build_compound_expr, but for the c-common bits. */ |
7985 | |
7986 | tree |
7987 | build_compound_expr (location_t /*loc*/, tree lhs, tree rhs) |
7988 | { |
7989 | return cp_build_compound_expr (lhs, rhs, tf_warning_or_error); |
7990 | } |
7991 | |
7992 | /* Build a compound expression. */ |
7993 | |
7994 | tree |
7995 | cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain) |
7996 | { |
7997 | lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain); |
7998 | |
7999 | if (lhs == error_mark_node || rhs == error_mark_node) |
8000 | return error_mark_node; |
8001 | |
8002 | if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR) |
8003 | lhs = TREE_OPERAND (lhs, 0); |
8004 | tree eptype = NULL_TREE; |
8005 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
8006 | { |
8007 | eptype = TREE_TYPE (rhs); |
8008 | rhs = TREE_OPERAND (rhs, 0); |
8009 | } |
8010 | |
8011 | if (TREE_CODE (rhs) == TARGET_EXPR) |
8012 | { |
8013 | /* If the rhs is a TARGET_EXPR, then build the compound |
8014 | expression inside the target_expr's initializer. This |
8015 | helps the compiler to eliminate unnecessary temporaries. */ |
8016 | tree init = TREE_OPERAND (rhs, 1); |
8017 | |
8018 | init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init); |
8019 | TREE_OPERAND (rhs, 1) = init; |
8020 | |
8021 | if (eptype) |
8022 | rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs); |
8023 | return rhs; |
8024 | } |
8025 | |
8026 | if (type_unknown_p (expr: rhs)) |
8027 | { |
8028 | if (complain & tf_error) |
8029 | error_at (cp_expr_loc_or_input_loc (t: rhs), |
8030 | "no context to resolve type of %qE" , rhs); |
8031 | return error_mark_node; |
8032 | } |
8033 | |
8034 | tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs); |
8035 | if (eptype) |
8036 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); |
8037 | return ret; |
8038 | } |
8039 | |
8040 | /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE |
8041 | casts away constness. CAST gives the type of cast. Returns true |
8042 | if the cast is ill-formed, false if it is well-formed. |
8043 | |
8044 | ??? This function warns for casting away any qualifier not just |
8045 | const. We would like to specify exactly what qualifiers are casted |
8046 | away. |
8047 | */ |
8048 | |
8049 | static bool |
8050 | check_for_casting_away_constness (location_t loc, tree src_type, |
8051 | tree dest_type, enum tree_code cast, |
8052 | tsubst_flags_t complain) |
8053 | { |
8054 | /* C-style casts are allowed to cast away constness. With |
8055 | WARN_CAST_QUAL, we still want to issue a warning. */ |
8056 | if (cast == CAST_EXPR && !warn_cast_qual) |
8057 | return false; |
8058 | |
8059 | if (!casts_away_constness (src_type, dest_type, complain)) |
8060 | return false; |
8061 | |
8062 | switch (cast) |
8063 | { |
8064 | case CAST_EXPR: |
8065 | if (complain & tf_warning) |
8066 | warning_at (loc, OPT_Wcast_qual, |
8067 | "cast from type %qT to type %qT casts away qualifiers" , |
8068 | src_type, dest_type); |
8069 | return false; |
8070 | |
8071 | case STATIC_CAST_EXPR: |
8072 | if (complain & tf_error) |
8073 | error_at (loc, "%<static_cast%> from type %qT to type %qT casts " |
8074 | "away qualifiers" , |
8075 | src_type, dest_type); |
8076 | return true; |
8077 | |
8078 | case REINTERPRET_CAST_EXPR: |
8079 | if (complain & tf_error) |
8080 | error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT " |
8081 | "casts away qualifiers" , |
8082 | src_type, dest_type); |
8083 | return true; |
8084 | |
8085 | default: |
8086 | gcc_unreachable(); |
8087 | } |
8088 | } |
8089 | |
8090 | /* Warns if the cast from expression EXPR to type TYPE is useless. */ |
8091 | void |
8092 | maybe_warn_about_useless_cast (location_t loc, tree type, tree expr, |
8093 | tsubst_flags_t complain) |
8094 | { |
8095 | if (warn_useless_cast |
8096 | && complain & tf_warning) |
8097 | { |
8098 | if (TYPE_REF_P (type) |
8099 | ? ((TYPE_REF_IS_RVALUE (type) |
8100 | ? xvalue_p (expr) : lvalue_p (expr)) |
8101 | && same_type_p (TREE_TYPE (expr), TREE_TYPE (type))) |
8102 | /* Don't warn when converting a class object to a non-reference type, |
8103 | because that's a common way to create a temporary. */ |
8104 | : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type))) |
8105 | warning_at (loc, OPT_Wuseless_cast, |
8106 | "useless cast to type %q#T" , type); |
8107 | } |
8108 | } |
8109 | |
8110 | /* Warns if the cast ignores cv-qualifiers on TYPE. */ |
8111 | static void |
8112 | maybe_warn_about_cast_ignoring_quals (location_t loc, tree type, |
8113 | tsubst_flags_t complain) |
8114 | { |
8115 | if (warn_ignored_qualifiers |
8116 | && complain & tf_warning |
8117 | && !CLASS_TYPE_P (type) |
8118 | && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))) |
8119 | warning_at (loc, OPT_Wignored_qualifiers, |
8120 | "type qualifiers ignored on cast result type" ); |
8121 | } |
8122 | |
8123 | /* Convert EXPR (an expression with pointer-to-member type) to TYPE |
8124 | (another pointer-to-member type in the same hierarchy) and return |
8125 | the converted expression. If ALLOW_INVERSE_P is permitted, a |
8126 | pointer-to-derived may be converted to pointer-to-base; otherwise, |
8127 | only the other direction is permitted. If C_CAST_P is true, this |
8128 | conversion is taking place as part of a C-style cast. */ |
8129 | |
8130 | tree |
8131 | convert_ptrmem (tree type, tree expr, bool allow_inverse_p, |
8132 | bool c_cast_p, tsubst_flags_t complain) |
8133 | { |
8134 | if (same_type_p (type, TREE_TYPE (expr))) |
8135 | return expr; |
8136 | |
8137 | if (TYPE_PTRDATAMEM_P (type)) |
8138 | { |
8139 | tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)); |
8140 | tree nbase = TYPE_PTRMEM_CLASS_TYPE (type); |
8141 | tree delta = (get_delta_difference |
8142 | (obase, nbase, |
8143 | allow_inverse_p, c_cast_p, complain)); |
8144 | |
8145 | if (delta == error_mark_node) |
8146 | return error_mark_node; |
8147 | |
8148 | if (!same_type_p (obase, nbase)) |
8149 | { |
8150 | if (TREE_CODE (expr) == PTRMEM_CST) |
8151 | expr = cplus_expand_constant (expr); |
8152 | |
8153 | tree cond = cp_build_binary_op (location: input_location, code: EQ_EXPR, orig_op0: expr, |
8154 | orig_op1: build_int_cst (TREE_TYPE (expr), -1), |
8155 | complain); |
8156 | tree op1 = build_nop (ptrdiff_type_node, expr); |
8157 | tree op2 = cp_build_binary_op (location: input_location, code: PLUS_EXPR, orig_op0: op1, orig_op1: delta, |
8158 | complain); |
8159 | |
8160 | expr = fold_build3_loc (input_location, |
8161 | COND_EXPR, ptrdiff_type_node, cond, op1, op2); |
8162 | } |
8163 | |
8164 | return build_nop (type, expr); |
8165 | } |
8166 | else |
8167 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, |
8168 | allow_inverse_p, c_cast_p, complain); |
8169 | } |
8170 | |
8171 | /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true, |
8172 | this static_cast is being attempted as one of the possible casts |
8173 | allowed by a C-style cast. (In that case, accessibility of base |
8174 | classes is not considered, and it is OK to cast away |
8175 | constness.) Return the result of the cast. *VALID_P is set to |
8176 | indicate whether or not the cast was valid. */ |
8177 | |
8178 | static tree |
8179 | build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p, |
8180 | bool *valid_p, tsubst_flags_t complain) |
8181 | { |
8182 | tree intype; |
8183 | tree result; |
8184 | cp_lvalue_kind clk; |
8185 | |
8186 | /* Assume the cast is valid. */ |
8187 | *valid_p = true; |
8188 | |
8189 | intype = unlowered_expr_type (exp: expr); |
8190 | |
8191 | /* Save casted types in the function's used types hash table. */ |
8192 | used_types_insert (type); |
8193 | |
8194 | /* A prvalue of non-class type is cv-unqualified. */ |
8195 | if (!CLASS_TYPE_P (type)) |
8196 | type = cv_unqualified (type); |
8197 | |
8198 | /* [expr.static.cast] |
8199 | |
8200 | An lvalue of type "cv1 B", where B is a class type, can be cast |
8201 | to type "reference to cv2 D", where D is a class derived (clause |
8202 | _class.derived_) from B, if a valid standard conversion from |
8203 | "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the |
8204 | same cv-qualification as, or greater cv-qualification than, cv1, |
8205 | and B is not a virtual base class of D. */ |
8206 | /* We check this case before checking the validity of "TYPE t = |
8207 | EXPR;" below because for this case: |
8208 | |
8209 | struct B {}; |
8210 | struct D : public B { D(const B&); }; |
8211 | extern B& b; |
8212 | void f() { static_cast<const D&>(b); } |
8213 | |
8214 | we want to avoid constructing a new D. The standard is not |
8215 | completely clear about this issue, but our interpretation is |
8216 | consistent with other compilers. */ |
8217 | if (TYPE_REF_P (type) |
8218 | && CLASS_TYPE_P (TREE_TYPE (type)) |
8219 | && CLASS_TYPE_P (intype) |
8220 | && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr)) |
8221 | && DERIVED_FROM_P (intype, TREE_TYPE (type)) |
8222 | && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)), |
8223 | build_pointer_type (TYPE_MAIN_VARIANT |
8224 | (TREE_TYPE (type))), |
8225 | complain) |
8226 | && (c_cast_p |
8227 | || at_least_as_qualified_p (TREE_TYPE (type), type2: intype))) |
8228 | { |
8229 | tree base; |
8230 | |
8231 | if (processing_template_decl) |
8232 | return expr; |
8233 | |
8234 | /* There is a standard conversion from "D*" to "B*" even if "B" |
8235 | is ambiguous or inaccessible. If this is really a |
8236 | static_cast, then we check both for inaccessibility and |
8237 | ambiguity. However, if this is a static_cast being performed |
8238 | because the user wrote a C-style cast, then accessibility is |
8239 | not considered. */ |
8240 | base = lookup_base (TREE_TYPE (type), intype, |
8241 | c_cast_p ? ba_unique : ba_check, |
8242 | NULL, complain); |
8243 | expr = cp_build_addr_expr (arg: expr, complain); |
8244 | |
8245 | if (sanitize_flags_p (flag: SANITIZE_VPTR)) |
8246 | { |
8247 | tree ubsan_check |
8248 | = cp_ubsan_maybe_instrument_downcast (loc, type, |
8249 | intype, expr); |
8250 | if (ubsan_check) |
8251 | expr = ubsan_check; |
8252 | } |
8253 | |
8254 | /* Convert from "B*" to "D*". This function will check that "B" |
8255 | is not a virtual base of "D". Even if we don't have a guarantee |
8256 | that expr is NULL, if the static_cast is to a reference type, |
8257 | it is UB if it would be NULL, so omit the non-NULL check. */ |
8258 | expr = build_base_path (MINUS_EXPR, expr, base, |
8259 | /*nonnull=*/flag_delete_null_pointer_checks, |
8260 | complain); |
8261 | |
8262 | /* Convert the pointer to a reference -- but then remember that |
8263 | there are no expressions with reference type in C++. |
8264 | |
8265 | We call rvalue so that there's an actual tree code |
8266 | (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand |
8267 | is a variable with the same type, the conversion would get folded |
8268 | away, leaving just the variable and causing lvalue_kind to give |
8269 | the wrong answer. */ |
8270 | expr = cp_fold_convert (type, expr); |
8271 | |
8272 | /* When -fsanitize=null, make sure to diagnose reference binding to |
8273 | NULL even when the reference is converted to pointer later on. */ |
8274 | if (sanitize_flags_p (flag: SANITIZE_NULL) |
8275 | && TREE_CODE (expr) == COND_EXPR |
8276 | && TREE_OPERAND (expr, 2) |
8277 | && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST |
8278 | && TREE_TYPE (TREE_OPERAND (expr, 2)) == type) |
8279 | ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2)); |
8280 | |
8281 | return convert_from_reference (rvalue (expr)); |
8282 | } |
8283 | |
8284 | /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to |
8285 | cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */ |
8286 | if (TYPE_REF_P (type) |
8287 | && TYPE_REF_IS_RVALUE (type) |
8288 | && (clk = real_lvalue_p (expr)) |
8289 | && reference_compatible_p (TREE_TYPE (type), intype) |
8290 | && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), type2: intype))) |
8291 | { |
8292 | if (processing_template_decl) |
8293 | return expr; |
8294 | if (clk == clk_ordinary) |
8295 | { |
8296 | /* Handle the (non-bit-field) lvalue case here by casting to |
8297 | lvalue reference and then changing it to an rvalue reference. |
8298 | Casting an xvalue to rvalue reference will be handled by the |
8299 | main code path. */ |
8300 | tree lref = cp_build_reference_type (TREE_TYPE (type), false); |
8301 | result = (perform_direct_initialization_if_possible |
8302 | (lref, expr, c_cast_p, complain)); |
8303 | result = build1 (NON_LVALUE_EXPR, type, result); |
8304 | return convert_from_reference (result); |
8305 | } |
8306 | else |
8307 | /* For a bit-field or packed field, bind to a temporary. */ |
8308 | expr = rvalue (expr); |
8309 | } |
8310 | |
8311 | /* Resolve overloaded address here rather than once in |
8312 | implicit_conversion and again in the inverse code below. */ |
8313 | if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr)) |
8314 | { |
8315 | expr = instantiate_type (type, expr, complain); |
8316 | intype = TREE_TYPE (expr); |
8317 | } |
8318 | |
8319 | /* [expr.static.cast] |
8320 | |
8321 | Any expression can be explicitly converted to type cv void. */ |
8322 | if (VOID_TYPE_P (type)) |
8323 | { |
8324 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) |
8325 | expr = TREE_OPERAND (expr, 0); |
8326 | return convert_to_void (expr, ICV_CAST, complain); |
8327 | } |
8328 | |
8329 | /* [class.abstract] |
8330 | An abstract class shall not be used ... as the type of an explicit |
8331 | conversion. */ |
8332 | if (abstract_virtuals_error (ACU_CAST, type, complain)) |
8333 | return error_mark_node; |
8334 | |
8335 | /* [expr.static.cast] |
8336 | |
8337 | An expression e can be explicitly converted to a type T using a |
8338 | static_cast of the form static_cast<T>(e) if the declaration T |
8339 | t(e);" is well-formed, for some invented temporary variable |
8340 | t. */ |
8341 | result = perform_direct_initialization_if_possible (type, expr, |
8342 | c_cast_p, complain); |
8343 | /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42), |
8344 | which initialize the first element of the aggregate. We need to handle |
8345 | the array case specifically. */ |
8346 | if (result == NULL_TREE |
8347 | && cxx_dialect >= cxx20 |
8348 | && TREE_CODE (type) == ARRAY_TYPE) |
8349 | { |
8350 | /* Create { EXPR } and perform direct-initialization from it. */ |
8351 | tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr); |
8352 | CONSTRUCTOR_IS_DIRECT_INIT (e) = true; |
8353 | CONSTRUCTOR_IS_PAREN_INIT (e) = true; |
8354 | result = perform_direct_initialization_if_possible (type, e, c_cast_p, |
8355 | complain); |
8356 | } |
8357 | if (result) |
8358 | { |
8359 | if (processing_template_decl) |
8360 | return expr; |
8361 | |
8362 | result = convert_from_reference (result); |
8363 | |
8364 | /* [expr.static.cast] |
8365 | |
8366 | If T is a reference type, the result is an lvalue; otherwise, |
8367 | the result is an rvalue. */ |
8368 | if (!TYPE_REF_P (type)) |
8369 | { |
8370 | result = rvalue (result); |
8371 | |
8372 | if (result == expr && SCALAR_TYPE_P (type)) |
8373 | /* Leave some record of the cast. */ |
8374 | result = build_nop (type, expr); |
8375 | } |
8376 | return result; |
8377 | } |
8378 | |
8379 | /* [expr.static.cast] |
8380 | |
8381 | The inverse of any standard conversion sequence (clause _conv_), |
8382 | other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer |
8383 | (_conv.array_), function-to-pointer (_conv.func_), and boolean |
8384 | (_conv.bool_) conversions, can be performed explicitly using |
8385 | static_cast subject to the restriction that the explicit |
8386 | conversion does not cast away constness (_expr.const.cast_), and |
8387 | the following additional rules for specific cases: */ |
8388 | /* For reference, the conversions not excluded are: integral |
8389 | promotions, floating-point promotion, integral conversions, |
8390 | floating-point conversions, floating-integral conversions, |
8391 | pointer conversions, and pointer to member conversions. */ |
8392 | /* DR 128 |
8393 | |
8394 | A value of integral _or enumeration_ type can be explicitly |
8395 | converted to an enumeration type. */ |
8396 | /* The effect of all that is that any conversion between any two |
8397 | types which are integral, floating, or enumeration types can be |
8398 | performed. */ |
8399 | if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
8400 | || SCALAR_FLOAT_TYPE_P (type)) |
8401 | && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype) |
8402 | || SCALAR_FLOAT_TYPE_P (intype))) |
8403 | { |
8404 | if (processing_template_decl) |
8405 | return expr; |
8406 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) |
8407 | expr = TREE_OPERAND (expr, 0); |
8408 | /* [expr.static.cast]: "If the value is not a bit-field, the result |
8409 | refers to the object or the specified base class subobject thereof; |
8410 | otherwise, the lvalue-to-rvalue conversion is applied to the |
8411 | bit-field and the resulting prvalue is used as the operand of the |
8412 | static_cast." There are no prvalue bit-fields; the l-to-r conversion |
8413 | will give us an object of the underlying type of the bit-field. */ |
8414 | expr = decay_conversion (exp: expr, complain); |
8415 | return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain); |
8416 | } |
8417 | |
8418 | if (TYPE_PTR_P (type) && TYPE_PTR_P (intype) |
8419 | && CLASS_TYPE_P (TREE_TYPE (type)) |
8420 | && CLASS_TYPE_P (TREE_TYPE (intype)) |
8421 | && can_convert (build_pointer_type (TYPE_MAIN_VARIANT |
8422 | (TREE_TYPE (intype))), |
8423 | build_pointer_type (TYPE_MAIN_VARIANT |
8424 | (TREE_TYPE (type))), |
8425 | complain)) |
8426 | { |
8427 | tree base; |
8428 | |
8429 | if (processing_template_decl) |
8430 | return expr; |
8431 | |
8432 | if (!c_cast_p |
8433 | && check_for_casting_away_constness (loc, src_type: intype, dest_type: type, |
8434 | cast: STATIC_CAST_EXPR, |
8435 | complain)) |
8436 | return error_mark_node; |
8437 | base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype), |
8438 | c_cast_p ? ba_unique : ba_check, |
8439 | NULL, complain); |
8440 | expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false, |
8441 | complain); |
8442 | |
8443 | if (sanitize_flags_p (flag: SANITIZE_VPTR)) |
8444 | { |
8445 | tree ubsan_check |
8446 | = cp_ubsan_maybe_instrument_downcast (loc, type, |
8447 | intype, expr); |
8448 | if (ubsan_check) |
8449 | expr = ubsan_check; |
8450 | } |
8451 | |
8452 | return cp_fold_convert (type, expr); |
8453 | } |
8454 | |
8455 | if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype)) |
8456 | || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) |
8457 | { |
8458 | tree c1; |
8459 | tree c2; |
8460 | tree t1; |
8461 | tree t2; |
8462 | |
8463 | c1 = TYPE_PTRMEM_CLASS_TYPE (intype); |
8464 | c2 = TYPE_PTRMEM_CLASS_TYPE (type); |
8465 | |
8466 | if (TYPE_PTRDATAMEM_P (type)) |
8467 | { |
8468 | t1 = (build_ptrmem_type |
8469 | (c1, |
8470 | TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype)))); |
8471 | t2 = (build_ptrmem_type |
8472 | (c2, |
8473 | TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type)))); |
8474 | } |
8475 | else |
8476 | { |
8477 | t1 = intype; |
8478 | t2 = type; |
8479 | } |
8480 | if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain)) |
8481 | { |
8482 | if (!c_cast_p |
8483 | && check_for_casting_away_constness (loc, src_type: intype, dest_type: type, |
8484 | cast: STATIC_CAST_EXPR, |
8485 | complain)) |
8486 | return error_mark_node; |
8487 | if (processing_template_decl) |
8488 | return expr; |
8489 | return convert_ptrmem (type, expr, /*allow_inverse_p=*/1, |
8490 | c_cast_p, complain); |
8491 | } |
8492 | } |
8493 | |
8494 | /* [expr.static.cast] |
8495 | |
8496 | An rvalue of type "pointer to cv void" can be explicitly |
8497 | converted to a pointer to object type. A value of type pointer |
8498 | to object converted to "pointer to cv void" and back to the |
8499 | original pointer type will have its original value. */ |
8500 | if (TYPE_PTR_P (intype) |
8501 | && VOID_TYPE_P (TREE_TYPE (intype)) |
8502 | && TYPE_PTROB_P (type)) |
8503 | { |
8504 | if (!c_cast_p |
8505 | && check_for_casting_away_constness (loc, src_type: intype, dest_type: type, |
8506 | cast: STATIC_CAST_EXPR, |
8507 | complain)) |
8508 | return error_mark_node; |
8509 | if (processing_template_decl) |
8510 | return expr; |
8511 | return build_nop (type, expr); |
8512 | } |
8513 | |
8514 | *valid_p = false; |
8515 | return error_mark_node; |
8516 | } |
8517 | |
8518 | /* Return an expression representing static_cast<TYPE>(EXPR). */ |
8519 | |
8520 | tree |
8521 | build_static_cast (location_t loc, tree type, tree oexpr, |
8522 | tsubst_flags_t complain) |
8523 | { |
8524 | tree expr = oexpr; |
8525 | tree result; |
8526 | bool valid_p; |
8527 | |
8528 | if (type == error_mark_node || expr == error_mark_node) |
8529 | return error_mark_node; |
8530 | |
8531 | bool dependent = (dependent_type_p (type) |
8532 | || type_dependent_expression_p (expr)); |
8533 | if (dependent) |
8534 | { |
8535 | tmpl: |
8536 | expr = build_min (STATIC_CAST_EXPR, type, oexpr); |
8537 | /* We don't know if it will or will not have side effects. */ |
8538 | TREE_SIDE_EFFECTS (expr) = 1; |
8539 | result = convert_from_reference (expr); |
8540 | protected_set_expr_location (result, loc); |
8541 | return result; |
8542 | } |
8543 | |
8544 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
8545 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ |
8546 | if (!TYPE_REF_P (type) |
8547 | && TREE_CODE (expr) == NOP_EXPR |
8548 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) |
8549 | expr = TREE_OPERAND (expr, 0); |
8550 | |
8551 | result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false, |
8552 | valid_p: &valid_p, complain); |
8553 | if (valid_p) |
8554 | { |
8555 | if (result != error_mark_node) |
8556 | { |
8557 | maybe_warn_about_useless_cast (loc, type, expr, complain); |
8558 | maybe_warn_about_cast_ignoring_quals (loc, type, complain); |
8559 | } |
8560 | if (processing_template_decl) |
8561 | goto tmpl; |
8562 | protected_set_expr_location (result, loc); |
8563 | return result; |
8564 | } |
8565 | |
8566 | if (complain & tf_error) |
8567 | { |
8568 | error_at (loc, "invalid %<static_cast%> from type %qT to type %qT" , |
8569 | TREE_TYPE (expr), type); |
8570 | if ((TYPE_PTR_P (type) || TYPE_REF_P (type)) |
8571 | && CLASS_TYPE_P (TREE_TYPE (type)) |
8572 | && !COMPLETE_TYPE_P (TREE_TYPE (type))) |
8573 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))), |
8574 | "class type %qT is incomplete" , TREE_TYPE (type)); |
8575 | tree expr_type = TREE_TYPE (expr); |
8576 | if (TYPE_PTR_P (expr_type)) |
8577 | expr_type = TREE_TYPE (expr_type); |
8578 | if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type)) |
8579 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)), |
8580 | "class type %qT is incomplete" , expr_type); |
8581 | } |
8582 | return error_mark_node; |
8583 | } |
8584 | |
8585 | /* EXPR is an expression with member function or pointer-to-member |
8586 | function type. TYPE is a pointer type. Converting EXPR to TYPE is |
8587 | not permitted by ISO C++, but we accept it in some modes. If we |
8588 | are not in one of those modes, issue a diagnostic. Return the |
8589 | converted expression. */ |
8590 | |
8591 | tree |
8592 | convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain) |
8593 | { |
8594 | tree intype; |
8595 | tree decl; |
8596 | |
8597 | intype = TREE_TYPE (expr); |
8598 | gcc_assert (TYPE_PTRMEMFUNC_P (intype) |
8599 | || TREE_CODE (intype) == METHOD_TYPE); |
8600 | |
8601 | if (!(complain & tf_warning_or_error)) |
8602 | return error_mark_node; |
8603 | |
8604 | location_t loc = cp_expr_loc_or_input_loc (t: expr); |
8605 | |
8606 | if (pedantic || warn_pmf2ptr) |
8607 | pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions, |
8608 | "converting from %qH to %qI" , intype, type); |
8609 | |
8610 | STRIP_ANY_LOCATION_WRAPPER (expr); |
8611 | |
8612 | if (TREE_CODE (intype) == METHOD_TYPE) |
8613 | expr = build_addr_func (expr, complain); |
8614 | else if (TREE_CODE (expr) == PTRMEM_CST) |
8615 | expr = build_address (PTRMEM_CST_MEMBER (expr)); |
8616 | else |
8617 | { |
8618 | decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0); |
8619 | decl = build_address (t: decl); |
8620 | expr = get_member_function_from_ptrfunc (instance_ptrptr: &decl, function: expr, complain); |
8621 | } |
8622 | |
8623 | if (expr == error_mark_node) |
8624 | return error_mark_node; |
8625 | |
8626 | expr = build_nop (type, expr); |
8627 | SET_EXPR_LOCATION (expr, loc); |
8628 | return expr; |
8629 | } |
8630 | |
8631 | /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that |
8632 | constexpr evaluation knows to reject it. */ |
8633 | |
8634 | static tree |
8635 | build_nop_reinterpret (tree type, tree expr) |
8636 | { |
8637 | tree ret = build_nop (type, expr); |
8638 | if (ret != expr) |
8639 | REINTERPRET_CAST_P (ret) = true; |
8640 | return ret; |
8641 | } |
8642 | |
8643 | /* Return a representation for a reinterpret_cast from EXPR to TYPE. |
8644 | If C_CAST_P is true, this reinterpret cast is being done as part of |
8645 | a C-style cast. If VALID_P is non-NULL, *VALID_P is set to |
8646 | indicate whether or not reinterpret_cast was valid. */ |
8647 | |
8648 | static tree |
8649 | build_reinterpret_cast_1 (location_t loc, tree type, tree expr, |
8650 | bool c_cast_p, bool *valid_p, |
8651 | tsubst_flags_t complain) |
8652 | { |
8653 | tree intype; |
8654 | |
8655 | /* Assume the cast is invalid. */ |
8656 | if (valid_p) |
8657 | *valid_p = true; |
8658 | |
8659 | if (type == error_mark_node || error_operand_p (t: expr)) |
8660 | return error_mark_node; |
8661 | |
8662 | intype = TREE_TYPE (expr); |
8663 | |
8664 | /* Save casted types in the function's used types hash table. */ |
8665 | used_types_insert (type); |
8666 | |
8667 | /* A prvalue of non-class type is cv-unqualified. */ |
8668 | if (!CLASS_TYPE_P (type)) |
8669 | type = cv_unqualified (type); |
8670 | |
8671 | /* [expr.reinterpret.cast] |
8672 | A glvalue of type T1, designating an object x, can be cast to the type |
8673 | "reference to T2" if an expression of type "pointer to T1" can be |
8674 | explicitly converted to the type "pointer to T2" using a reinterpret_cast. |
8675 | The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x |
8676 | of type "pointer to T1". No temporary is created, no copy is made, and no |
8677 | constructors (11.4.4) or conversion functions (11.4.7) are called. */ |
8678 | if (TYPE_REF_P (type)) |
8679 | { |
8680 | if (!glvalue_p (expr)) |
8681 | { |
8682 | if (complain & tf_error) |
8683 | error_at (loc, "invalid cast of a prvalue expression of type " |
8684 | "%qT to type %qT" , |
8685 | intype, type); |
8686 | return error_mark_node; |
8687 | } |
8688 | |
8689 | /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and |
8690 | "B" are related class types; the reinterpret_cast does not |
8691 | adjust the pointer. */ |
8692 | if (TYPE_PTR_P (intype) |
8693 | && (complain & tf_warning) |
8694 | && (comptypes (TREE_TYPE (intype), TREE_TYPE (type), |
8695 | COMPARE_BASE | COMPARE_DERIVED))) |
8696 | warning_at (loc, 0, "casting %qT to %qT does not dereference pointer" , |
8697 | intype, type); |
8698 | |
8699 | expr = cp_build_addr_expr (arg: expr, complain); |
8700 | |
8701 | if (warn_strict_aliasing > 2) |
8702 | cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr); |
8703 | |
8704 | if (expr != error_mark_node) |
8705 | expr = build_reinterpret_cast_1 |
8706 | (loc, type: build_pointer_type (TREE_TYPE (type)), expr, c_cast_p, |
8707 | valid_p, complain); |
8708 | if (expr != error_mark_node) |
8709 | /* cp_build_indirect_ref isn't right for rvalue refs. */ |
8710 | expr = convert_from_reference (fold_convert (type, expr)); |
8711 | return expr; |
8712 | } |
8713 | |
8714 | /* As a G++ extension, we consider conversions from member |
8715 | functions, and pointers to member functions to |
8716 | pointer-to-function and pointer-to-void types. If |
8717 | -Wno-pmf-conversions has not been specified, |
8718 | convert_member_func_to_ptr will issue an error message. */ |
8719 | if ((TYPE_PTRMEMFUNC_P (intype) |
8720 | || TREE_CODE (intype) == METHOD_TYPE) |
8721 | && TYPE_PTR_P (type) |
8722 | && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE |
8723 | || VOID_TYPE_P (TREE_TYPE (type)))) |
8724 | return convert_member_func_to_ptr (type, expr, complain); |
8725 | |
8726 | /* If the cast is not to a reference type, the lvalue-to-rvalue, |
8727 | array-to-pointer, and function-to-pointer conversions are |
8728 | performed. */ |
8729 | expr = decay_conversion (exp: expr, complain); |
8730 | |
8731 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
8732 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ |
8733 | if (TREE_CODE (expr) == NOP_EXPR |
8734 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) |
8735 | expr = TREE_OPERAND (expr, 0); |
8736 | |
8737 | if (error_operand_p (t: expr)) |
8738 | return error_mark_node; |
8739 | |
8740 | intype = TREE_TYPE (expr); |
8741 | |
8742 | /* [expr.reinterpret.cast] |
8743 | A pointer can be converted to any integral type large enough to |
8744 | hold it. ... A value of type std::nullptr_t can be converted to |
8745 | an integral type; the conversion has the same meaning and |
8746 | validity as a conversion of (void*)0 to the integral type. */ |
8747 | if (CP_INTEGRAL_TYPE_P (type) |
8748 | && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype))) |
8749 | { |
8750 | if (TYPE_PRECISION (type) < TYPE_PRECISION (intype)) |
8751 | { |
8752 | if (complain & tf_error) |
8753 | permerror (loc, "cast from %qH to %qI loses precision" , |
8754 | intype, type); |
8755 | else |
8756 | return error_mark_node; |
8757 | } |
8758 | if (NULLPTR_TYPE_P (intype)) |
8759 | return build_int_cst (type, 0); |
8760 | } |
8761 | /* [expr.reinterpret.cast] |
8762 | A value of integral or enumeration type can be explicitly |
8763 | converted to a pointer. */ |
8764 | else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype)) |
8765 | /* OK */ |
8766 | ; |
8767 | else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
8768 | || TYPE_PTR_OR_PTRMEM_P (type)) |
8769 | && same_type_p (type, intype)) |
8770 | /* DR 799 */ |
8771 | return rvalue (expr); |
8772 | else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype)) |
8773 | { |
8774 | if ((complain & tf_warning) |
8775 | && !cxx_safe_function_type_cast_p (TREE_TYPE (type), |
8776 | TREE_TYPE (intype))) |
8777 | warning_at (loc, OPT_Wcast_function_type, |
8778 | "cast between incompatible function types" |
8779 | " from %qH to %qI" , intype, type); |
8780 | return build_nop_reinterpret (type, expr); |
8781 | } |
8782 | else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)) |
8783 | { |
8784 | if ((complain & tf_warning) |
8785 | && !cxx_safe_function_type_cast_p |
8786 | (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)), |
8787 | TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype)))) |
8788 | warning_at (loc, OPT_Wcast_function_type, |
8789 | "cast between incompatible pointer to member types" |
8790 | " from %qH to %qI" , intype, type); |
8791 | return build_nop_reinterpret (type, expr); |
8792 | } |
8793 | else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype)) |
8794 | || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype))) |
8795 | { |
8796 | if (!c_cast_p |
8797 | && check_for_casting_away_constness (loc, src_type: intype, dest_type: type, |
8798 | cast: REINTERPRET_CAST_EXPR, |
8799 | complain)) |
8800 | return error_mark_node; |
8801 | /* Warn about possible alignment problems. */ |
8802 | if ((STRICT_ALIGNMENT || warn_cast_align == 2) |
8803 | && (complain & tf_warning) |
8804 | && !VOID_TYPE_P (type) |
8805 | && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE |
8806 | && COMPLETE_TYPE_P (TREE_TYPE (type)) |
8807 | && COMPLETE_TYPE_P (TREE_TYPE (intype)) |
8808 | && min_align_of_type (TREE_TYPE (type)) |
8809 | > min_align_of_type (TREE_TYPE (intype))) |
8810 | warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI " |
8811 | "increases required alignment of target type" , |
8812 | intype, type); |
8813 | |
8814 | if (warn_strict_aliasing <= 2) |
8815 | /* strict_aliasing_warning STRIP_NOPs its expr. */ |
8816 | cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr); |
8817 | |
8818 | return build_nop_reinterpret (type, expr); |
8819 | } |
8820 | else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype)) |
8821 | || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type))) |
8822 | { |
8823 | if (complain & tf_warning) |
8824 | /* C++11 5.2.10 p8 says that "Converting a function pointer to an |
8825 | object pointer type or vice versa is conditionally-supported." */ |
8826 | warning_at (loc, OPT_Wconditionally_supported, |
8827 | "casting between pointer-to-function and " |
8828 | "pointer-to-object is conditionally-supported" ); |
8829 | return build_nop_reinterpret (type, expr); |
8830 | } |
8831 | else if (gnu_vector_type_p (type) && scalarish_type_p (intype)) |
8832 | return convert_to_vector (type, rvalue (expr)); |
8833 | else if (gnu_vector_type_p (type: intype) |
8834 | && INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
8835 | return convert_to_integer_nofold (t: type, x: expr); |
8836 | else |
8837 | { |
8838 | if (valid_p) |
8839 | *valid_p = false; |
8840 | if (complain & tf_error) |
8841 | error_at (loc, "invalid cast from type %qT to type %qT" , |
8842 | intype, type); |
8843 | return error_mark_node; |
8844 | } |
8845 | |
8846 | expr = cp_convert (type, expr, complain); |
8847 | if (TREE_CODE (expr) == NOP_EXPR) |
8848 | /* Mark any nop_expr that created as a reintepret_cast. */ |
8849 | REINTERPRET_CAST_P (expr) = true; |
8850 | return expr; |
8851 | } |
8852 | |
8853 | tree |
8854 | build_reinterpret_cast (location_t loc, tree type, tree expr, |
8855 | tsubst_flags_t complain) |
8856 | { |
8857 | tree r; |
8858 | |
8859 | if (type == error_mark_node || expr == error_mark_node) |
8860 | return error_mark_node; |
8861 | |
8862 | if (processing_template_decl) |
8863 | { |
8864 | tree t = build_min (REINTERPRET_CAST_EXPR, type, expr); |
8865 | |
8866 | if (!TREE_SIDE_EFFECTS (t) |
8867 | && type_dependent_expression_p (expr)) |
8868 | /* There might turn out to be side effects inside expr. */ |
8869 | TREE_SIDE_EFFECTS (t) = 1; |
8870 | r = convert_from_reference (t); |
8871 | protected_set_expr_location (r, loc); |
8872 | return r; |
8873 | } |
8874 | |
8875 | r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false, |
8876 | /*valid_p=*/NULL, complain); |
8877 | if (r != error_mark_node) |
8878 | { |
8879 | maybe_warn_about_useless_cast (loc, type, expr, complain); |
8880 | maybe_warn_about_cast_ignoring_quals (loc, type, complain); |
8881 | } |
8882 | protected_set_expr_location (r, loc); |
8883 | return r; |
8884 | } |
8885 | |
8886 | /* Perform a const_cast from EXPR to TYPE. If the cast is valid, |
8887 | return an appropriate expression. Otherwise, return |
8888 | error_mark_node. If the cast is not valid, and COMPLAIN is true, |
8889 | then a diagnostic will be issued. If VALID_P is non-NULL, we are |
8890 | performing a C-style cast, its value upon return will indicate |
8891 | whether or not the conversion succeeded. */ |
8892 | |
8893 | static tree |
8894 | build_const_cast_1 (location_t loc, tree dst_type, tree expr, |
8895 | tsubst_flags_t complain, bool *valid_p) |
8896 | { |
8897 | tree src_type; |
8898 | tree reference_type; |
8899 | |
8900 | /* Callers are responsible for handling error_mark_node as a |
8901 | destination type. */ |
8902 | gcc_assert (dst_type != error_mark_node); |
8903 | /* In a template, callers should be building syntactic |
8904 | representations of casts, not using this machinery. */ |
8905 | gcc_assert (!processing_template_decl); |
8906 | |
8907 | /* Assume the conversion is invalid. */ |
8908 | if (valid_p) |
8909 | *valid_p = false; |
8910 | |
8911 | if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type)) |
8912 | { |
8913 | if (complain & tf_error) |
8914 | error_at (loc, "invalid use of %<const_cast%> with type %qT, " |
8915 | "which is not a pointer, reference, " |
8916 | "nor a pointer-to-data-member type" , dst_type); |
8917 | return error_mark_node; |
8918 | } |
8919 | |
8920 | if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE) |
8921 | { |
8922 | if (complain & tf_error) |
8923 | error_at (loc, "invalid use of %<const_cast%> with type %qT, " |
8924 | "which is a pointer or reference to a function type" , |
8925 | dst_type); |
8926 | return error_mark_node; |
8927 | } |
8928 | |
8929 | /* A prvalue of non-class type is cv-unqualified. */ |
8930 | dst_type = cv_unqualified (dst_type); |
8931 | |
8932 | /* Save casted types in the function's used types hash table. */ |
8933 | used_types_insert (dst_type); |
8934 | |
8935 | src_type = TREE_TYPE (expr); |
8936 | /* Expressions do not really have reference types. */ |
8937 | if (TYPE_REF_P (src_type)) |
8938 | src_type = TREE_TYPE (src_type); |
8939 | |
8940 | /* [expr.const.cast] |
8941 | |
8942 | For two object types T1 and T2, if a pointer to T1 can be explicitly |
8943 | converted to the type "pointer to T2" using a const_cast, then the |
8944 | following conversions can also be made: |
8945 | |
8946 | -- an lvalue of type T1 can be explicitly converted to an lvalue of |
8947 | type T2 using the cast const_cast<T2&>; |
8948 | |
8949 | -- a glvalue of type T1 can be explicitly converted to an xvalue of |
8950 | type T2 using the cast const_cast<T2&&>; and |
8951 | |
8952 | -- if T1 is a class type, a prvalue of type T1 can be explicitly |
8953 | converted to an xvalue of type T2 using the cast const_cast<T2&&>. */ |
8954 | |
8955 | if (TYPE_REF_P (dst_type)) |
8956 | { |
8957 | reference_type = dst_type; |
8958 | if (!TYPE_REF_IS_RVALUE (dst_type) |
8959 | ? lvalue_p (expr) |
8960 | : obvalue_p (expr)) |
8961 | /* OK. */; |
8962 | else |
8963 | { |
8964 | if (complain & tf_error) |
8965 | error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT " |
8966 | "to type %qT" , |
8967 | src_type, dst_type); |
8968 | return error_mark_node; |
8969 | } |
8970 | dst_type = build_pointer_type (TREE_TYPE (dst_type)); |
8971 | src_type = build_pointer_type (src_type); |
8972 | } |
8973 | else |
8974 | { |
8975 | reference_type = NULL_TREE; |
8976 | /* If the destination type is not a reference type, the |
8977 | lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
8978 | conversions are performed. */ |
8979 | src_type = type_decays_to (src_type); |
8980 | if (src_type == error_mark_node) |
8981 | return error_mark_node; |
8982 | } |
8983 | |
8984 | if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type)) |
8985 | { |
8986 | if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none)) |
8987 | { |
8988 | if (valid_p) |
8989 | { |
8990 | *valid_p = true; |
8991 | /* This cast is actually a C-style cast. Issue a warning if |
8992 | the user is making a potentially unsafe cast. */ |
8993 | check_for_casting_away_constness (loc, src_type, dest_type: dst_type, |
8994 | cast: CAST_EXPR, complain); |
8995 | /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */ |
8996 | if ((STRICT_ALIGNMENT || warn_cast_align == 2) |
8997 | && (complain & tf_warning) |
8998 | && min_align_of_type (TREE_TYPE (dst_type)) |
8999 | > min_align_of_type (TREE_TYPE (src_type))) |
9000 | warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI " |
9001 | "increases required alignment of target type" , |
9002 | src_type, dst_type); |
9003 | } |
9004 | if (reference_type) |
9005 | { |
9006 | expr = cp_build_addr_expr (arg: expr, complain); |
9007 | if (expr == error_mark_node) |
9008 | return error_mark_node; |
9009 | expr = build_nop (type: reference_type, expr); |
9010 | return convert_from_reference (expr); |
9011 | } |
9012 | else |
9013 | { |
9014 | expr = decay_conversion (exp: expr, complain); |
9015 | if (expr == error_mark_node) |
9016 | return error_mark_node; |
9017 | |
9018 | /* build_c_cast puts on a NOP_EXPR to make the result not an |
9019 | lvalue. Strip such NOP_EXPRs if VALUE is being used in |
9020 | non-lvalue context. */ |
9021 | if (TREE_CODE (expr) == NOP_EXPR |
9022 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) |
9023 | expr = TREE_OPERAND (expr, 0); |
9024 | return build_nop (type: dst_type, expr); |
9025 | } |
9026 | } |
9027 | else if (valid_p |
9028 | && !at_least_as_qualified_p (TREE_TYPE (dst_type), |
9029 | TREE_TYPE (src_type))) |
9030 | check_for_casting_away_constness (loc, src_type, dest_type: dst_type, |
9031 | cast: CAST_EXPR, complain); |
9032 | } |
9033 | |
9034 | if (complain & tf_error) |
9035 | error_at (loc, "invalid %<const_cast%> from type %qT to type %qT" , |
9036 | src_type, dst_type); |
9037 | return error_mark_node; |
9038 | } |
9039 | |
9040 | tree |
9041 | build_const_cast (location_t loc, tree type, tree expr, |
9042 | tsubst_flags_t complain) |
9043 | { |
9044 | tree r; |
9045 | |
9046 | if (type == error_mark_node || error_operand_p (t: expr)) |
9047 | return error_mark_node; |
9048 | |
9049 | if (processing_template_decl) |
9050 | { |
9051 | tree t = build_min (CONST_CAST_EXPR, type, expr); |
9052 | |
9053 | if (!TREE_SIDE_EFFECTS (t) |
9054 | && type_dependent_expression_p (expr)) |
9055 | /* There might turn out to be side effects inside expr. */ |
9056 | TREE_SIDE_EFFECTS (t) = 1; |
9057 | r = convert_from_reference (t); |
9058 | protected_set_expr_location (r, loc); |
9059 | return r; |
9060 | } |
9061 | |
9062 | r = build_const_cast_1 (loc, dst_type: type, expr, complain, /*valid_p=*/NULL); |
9063 | if (r != error_mark_node) |
9064 | { |
9065 | maybe_warn_about_useless_cast (loc, type, expr, complain); |
9066 | maybe_warn_about_cast_ignoring_quals (loc, type, complain); |
9067 | } |
9068 | protected_set_expr_location (r, loc); |
9069 | return r; |
9070 | } |
9071 | |
9072 | /* Like cp_build_c_cast, but for the c-common bits. */ |
9073 | |
9074 | tree |
9075 | build_c_cast (location_t loc, tree type, tree expr) |
9076 | { |
9077 | return cp_build_c_cast (loc, type, expr, tf_warning_or_error); |
9078 | } |
9079 | |
9080 | /* Like the "build_c_cast" used for c-common, but using cp_expr to |
9081 | preserve location information even for tree nodes that don't |
9082 | support it. */ |
9083 | |
9084 | cp_expr |
9085 | build_c_cast (location_t loc, tree type, cp_expr expr) |
9086 | { |
9087 | cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error); |
9088 | result.set_location (loc); |
9089 | return result; |
9090 | } |
9091 | |
9092 | /* Build an expression representing an explicit C-style cast to type |
9093 | TYPE of expression EXPR. */ |
9094 | |
9095 | tree |
9096 | cp_build_c_cast (location_t loc, tree type, tree expr, |
9097 | tsubst_flags_t complain) |
9098 | { |
9099 | tree value = expr; |
9100 | tree result; |
9101 | bool valid_p; |
9102 | |
9103 | if (type == error_mark_node || error_operand_p (t: expr)) |
9104 | return error_mark_node; |
9105 | |
9106 | if (processing_template_decl) |
9107 | { |
9108 | tree t = build_min (CAST_EXPR, type, |
9109 | tree_cons (NULL_TREE, value, NULL_TREE)); |
9110 | /* We don't know if it will or will not have side effects. */ |
9111 | TREE_SIDE_EFFECTS (t) = 1; |
9112 | return convert_from_reference (t); |
9113 | } |
9114 | |
9115 | /* Casts to a (pointer to a) specific ObjC class (or 'id' or |
9116 | 'Class') should always be retained, because this information aids |
9117 | in method lookup. */ |
9118 | if (objc_is_object_ptr (type) |
9119 | && objc_is_object_ptr (TREE_TYPE (expr))) |
9120 | return build_nop (type, expr); |
9121 | |
9122 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
9123 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ |
9124 | if (!TYPE_REF_P (type) |
9125 | && TREE_CODE (value) == NOP_EXPR |
9126 | && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0))) |
9127 | value = TREE_OPERAND (value, 0); |
9128 | |
9129 | if (TREE_CODE (type) == ARRAY_TYPE) |
9130 | { |
9131 | /* Allow casting from T1* to T2[] because Cfront allows it. |
9132 | NIHCL uses it. It is not valid ISO C++ however. */ |
9133 | if (TYPE_PTR_P (TREE_TYPE (expr))) |
9134 | { |
9135 | if (complain & tf_error) |
9136 | permerror (loc, "ISO C++ forbids casting to an array type %qT" , |
9137 | type); |
9138 | else |
9139 | return error_mark_node; |
9140 | type = build_pointer_type (TREE_TYPE (type)); |
9141 | } |
9142 | else |
9143 | { |
9144 | if (complain & tf_error) |
9145 | error_at (loc, "ISO C++ forbids casting to an array type %qT" , |
9146 | type); |
9147 | return error_mark_node; |
9148 | } |
9149 | } |
9150 | |
9151 | if (FUNC_OR_METHOD_TYPE_P (type)) |
9152 | { |
9153 | if (complain & tf_error) |
9154 | error_at (loc, "invalid cast to function type %qT" , type); |
9155 | return error_mark_node; |
9156 | } |
9157 | |
9158 | if (TYPE_PTR_P (type) |
9159 | && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE |
9160 | /* Casting to an integer of smaller size is an error detected elsewhere. */ |
9161 | && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value)) |
9162 | /* Don't warn about converting any constant. */ |
9163 | && !TREE_CONSTANT (value)) |
9164 | warning_at (loc, OPT_Wint_to_pointer_cast, |
9165 | "cast to pointer from integer of different size" ); |
9166 | |
9167 | /* A C-style cast can be a const_cast. */ |
9168 | result = build_const_cast_1 (loc, dst_type: type, expr: value, complain: complain & tf_warning, |
9169 | valid_p: &valid_p); |
9170 | if (valid_p) |
9171 | { |
9172 | if (result != error_mark_node) |
9173 | { |
9174 | maybe_warn_about_useless_cast (loc, type, expr: value, complain); |
9175 | maybe_warn_about_cast_ignoring_quals (loc, type, complain); |
9176 | } |
9177 | return result; |
9178 | } |
9179 | |
9180 | /* Or a static cast. */ |
9181 | result = build_static_cast_1 (loc, type, expr: value, /*c_cast_p=*/true, |
9182 | valid_p: &valid_p, complain); |
9183 | /* Or a reinterpret_cast. */ |
9184 | if (!valid_p) |
9185 | result = build_reinterpret_cast_1 (loc, type, expr: value, /*c_cast_p=*/true, |
9186 | valid_p: &valid_p, complain); |
9187 | /* The static_cast or reinterpret_cast may be followed by a |
9188 | const_cast. */ |
9189 | if (valid_p |
9190 | /* A valid cast may result in errors if, for example, a |
9191 | conversion to an ambiguous base class is required. */ |
9192 | && !error_operand_p (t: result)) |
9193 | { |
9194 | tree result_type; |
9195 | |
9196 | maybe_warn_about_useless_cast (loc, type, expr: value, complain); |
9197 | maybe_warn_about_cast_ignoring_quals (loc, type, complain); |
9198 | |
9199 | /* Non-class rvalues always have cv-unqualified type. */ |
9200 | if (!CLASS_TYPE_P (type)) |
9201 | type = TYPE_MAIN_VARIANT (type); |
9202 | result_type = TREE_TYPE (result); |
9203 | if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type)) |
9204 | result_type = TYPE_MAIN_VARIANT (result_type); |
9205 | /* If the type of RESULT does not match TYPE, perform a |
9206 | const_cast to make it match. If the static_cast or |
9207 | reinterpret_cast succeeded, we will differ by at most |
9208 | cv-qualification, so the follow-on const_cast is guaranteed |
9209 | to succeed. */ |
9210 | if (!same_type_p (non_reference (type), non_reference (result_type))) |
9211 | { |
9212 | result = build_const_cast_1 (loc, dst_type: type, expr: result, complain: false, valid_p: &valid_p); |
9213 | gcc_assert (valid_p); |
9214 | } |
9215 | return result; |
9216 | } |
9217 | |
9218 | return error_mark_node; |
9219 | } |
9220 | |
9221 | /* Warn when a value is moved to itself with std::move. LHS is the target, |
9222 | RHS may be the std::move call, and LOC is the location of the whole |
9223 | assignment. */ |
9224 | |
9225 | static void |
9226 | maybe_warn_self_move (location_t loc, tree lhs, tree rhs) |
9227 | { |
9228 | if (!warn_self_move) |
9229 | return; |
9230 | |
9231 | /* C++98 doesn't know move. */ |
9232 | if (cxx_dialect < cxx11) |
9233 | return; |
9234 | |
9235 | if (processing_template_decl) |
9236 | return; |
9237 | |
9238 | if (!REFERENCE_REF_P (rhs) |
9239 | || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR) |
9240 | return; |
9241 | tree fn = TREE_OPERAND (rhs, 0); |
9242 | if (!is_std_move_p (fn)) |
9243 | return; |
9244 | |
9245 | /* Just a little helper to strip * and various NOPs. */ |
9246 | auto = [] (tree &op) { |
9247 | STRIP_NOPS (op); |
9248 | while (INDIRECT_REF_P (op)) |
9249 | op = TREE_OPERAND (op, 0); |
9250 | op = maybe_undo_parenthesized_ref (op); |
9251 | STRIP_ANY_LOCATION_WRAPPER (op); |
9252 | }; |
9253 | |
9254 | tree arg = CALL_EXPR_ARG (fn, 0); |
9255 | extract_op (arg); |
9256 | if (TREE_CODE (arg) == ADDR_EXPR) |
9257 | arg = TREE_OPERAND (arg, 0); |
9258 | tree type = TREE_TYPE (lhs); |
9259 | tree orig_lhs = lhs; |
9260 | extract_op (lhs); |
9261 | if (cp_tree_equal (lhs, arg)) |
9262 | { |
9263 | auto_diagnostic_group d; |
9264 | if (warning_at (loc, OPT_Wself_move, |
9265 | "moving %qE of type %qT to itself" , orig_lhs, type)) |
9266 | inform (loc, "remove %<std::move%> call" ); |
9267 | } |
9268 | } |
9269 | |
9270 | /* For use from the C common bits. */ |
9271 | tree |
9272 | build_modify_expr (location_t location, |
9273 | tree lhs, tree /*lhs_origtype*/, |
9274 | enum tree_code modifycode, |
9275 | location_t /*rhs_location*/, tree rhs, |
9276 | tree /*rhs_origtype*/) |
9277 | { |
9278 | return cp_build_modify_expr (location, lhs, modifycode, rhs, |
9279 | tf_warning_or_error); |
9280 | } |
9281 | |
9282 | /* Build an assignment expression of lvalue LHS from value RHS. |
9283 | MODIFYCODE is the code for a binary operator that we use |
9284 | to combine the old value of LHS with RHS to get the new value. |
9285 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. |
9286 | |
9287 | C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */ |
9288 | |
9289 | tree |
9290 | cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, |
9291 | tree rhs, tsubst_flags_t complain) |
9292 | { |
9293 | lhs = mark_lvalue_use_nonread (lhs); |
9294 | |
9295 | tree result = NULL_TREE; |
9296 | tree newrhs = rhs; |
9297 | tree lhstype = TREE_TYPE (lhs); |
9298 | tree olhs = lhs; |
9299 | tree olhstype = lhstype; |
9300 | bool plain_assign = (modifycode == NOP_EXPR); |
9301 | bool compound_side_effects_p = false; |
9302 | tree preeval = NULL_TREE; |
9303 | |
9304 | /* Avoid duplicate error messages from operands that had errors. */ |
9305 | if (error_operand_p (t: lhs) || error_operand_p (t: rhs)) |
9306 | return error_mark_node; |
9307 | |
9308 | while (TREE_CODE (lhs) == COMPOUND_EXPR) |
9309 | { |
9310 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))) |
9311 | compound_side_effects_p = true; |
9312 | lhs = TREE_OPERAND (lhs, 1); |
9313 | } |
9314 | |
9315 | /* Handle control structure constructs used as "lvalues". Note that we |
9316 | leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */ |
9317 | switch (TREE_CODE (lhs)) |
9318 | { |
9319 | /* Handle --foo = 5; as these are valid constructs in C++. */ |
9320 | case PREDECREMENT_EXPR: |
9321 | case PREINCREMENT_EXPR: |
9322 | if (compound_side_effects_p) |
9323 | newrhs = rhs = stabilize_expr (rhs, &preeval); |
9324 | lhs = genericize_compound_lvalue (lvalue: lhs); |
9325 | maybe_add_compound: |
9326 | /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5; |
9327 | and looked through the COMPOUND_EXPRs, readd them now around |
9328 | the resulting lhs. */ |
9329 | if (TREE_CODE (olhs) == COMPOUND_EXPR) |
9330 | { |
9331 | lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs); |
9332 | tree *ptr = &TREE_OPERAND (lhs, 1); |
9333 | for (olhs = TREE_OPERAND (olhs, 1); |
9334 | TREE_CODE (olhs) == COMPOUND_EXPR; |
9335 | olhs = TREE_OPERAND (olhs, 1)) |
9336 | { |
9337 | *ptr = build2 (COMPOUND_EXPR, lhstype, |
9338 | TREE_OPERAND (olhs, 0), *ptr); |
9339 | ptr = &TREE_OPERAND (*ptr, 1); |
9340 | } |
9341 | } |
9342 | break; |
9343 | |
9344 | case MODIFY_EXPR: |
9345 | if (compound_side_effects_p) |
9346 | newrhs = rhs = stabilize_expr (rhs, &preeval); |
9347 | lhs = genericize_compound_lvalue (lvalue: lhs); |
9348 | goto maybe_add_compound; |
9349 | |
9350 | case MIN_EXPR: |
9351 | case MAX_EXPR: |
9352 | /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues, |
9353 | when neither operand has side-effects. */ |
9354 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
9355 | return error_mark_node; |
9356 | |
9357 | gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)) |
9358 | && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1))); |
9359 | |
9360 | lhs = build3 (COND_EXPR, TREE_TYPE (lhs), |
9361 | build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR, |
9362 | boolean_type_node, |
9363 | TREE_OPERAND (lhs, 0), |
9364 | TREE_OPERAND (lhs, 1)), |
9365 | TREE_OPERAND (lhs, 0), |
9366 | TREE_OPERAND (lhs, 1)); |
9367 | gcc_fallthrough (); |
9368 | |
9369 | /* Handle (a ? b : c) used as an "lvalue". */ |
9370 | case COND_EXPR: |
9371 | { |
9372 | /* Produce (a ? (b = rhs) : (c = rhs)) |
9373 | except that the RHS goes through a save-expr |
9374 | so the code to compute it is only emitted once. */ |
9375 | if (VOID_TYPE_P (TREE_TYPE (rhs))) |
9376 | { |
9377 | if (complain & tf_error) |
9378 | error_at (cp_expr_loc_or_loc (t: rhs, or_loc: loc), |
9379 | "void value not ignored as it ought to be" ); |
9380 | return error_mark_node; |
9381 | } |
9382 | |
9383 | rhs = stabilize_expr (rhs, &preeval); |
9384 | |
9385 | /* Check this here to avoid odd errors when trying to convert |
9386 | a throw to the type of the COND_EXPR. */ |
9387 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
9388 | return error_mark_node; |
9389 | |
9390 | tree op1 = TREE_OPERAND (lhs, 1); |
9391 | if (TREE_CODE (op1) != THROW_EXPR) |
9392 | op1 = cp_build_modify_expr (loc, lhs: op1, modifycode, rhs, complain); |
9393 | /* When sanitizing undefined behavior, even when rhs doesn't need |
9394 | stabilization at this point, the sanitization might add extra |
9395 | SAVE_EXPRs in there and so make sure there is no tree sharing |
9396 | in the rhs, otherwise those SAVE_EXPRs will have initialization |
9397 | only in one of the two branches. */ |
9398 | if (sanitize_flags_p (flag: SANITIZE_UNDEFINED |
9399 | | SANITIZE_UNDEFINED_NONDEFAULT)) |
9400 | rhs = unshare_expr (rhs); |
9401 | tree op2 = TREE_OPERAND (lhs, 2); |
9402 | if (TREE_CODE (op2) != THROW_EXPR) |
9403 | op2 = cp_build_modify_expr (loc, lhs: op2, modifycode, rhs, complain); |
9404 | tree cond = build_conditional_expr (input_location, |
9405 | TREE_OPERAND (lhs, 0), op1, op2, |
9406 | complain); |
9407 | |
9408 | if (cond == error_mark_node) |
9409 | return cond; |
9410 | /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d; |
9411 | and looked through the COMPOUND_EXPRs, readd them now around |
9412 | the resulting cond before adding the preevaluated rhs. */ |
9413 | if (TREE_CODE (olhs) == COMPOUND_EXPR) |
9414 | { |
9415 | cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond), |
9416 | TREE_OPERAND (olhs, 0), cond); |
9417 | tree *ptr = &TREE_OPERAND (cond, 1); |
9418 | for (olhs = TREE_OPERAND (olhs, 1); |
9419 | TREE_CODE (olhs) == COMPOUND_EXPR; |
9420 | olhs = TREE_OPERAND (olhs, 1)) |
9421 | { |
9422 | *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond), |
9423 | TREE_OPERAND (olhs, 0), *ptr); |
9424 | ptr = &TREE_OPERAND (*ptr, 1); |
9425 | } |
9426 | } |
9427 | /* Make sure the code to compute the rhs comes out |
9428 | before the split. */ |
9429 | result = cond; |
9430 | goto ret; |
9431 | } |
9432 | |
9433 | default: |
9434 | lhs = olhs; |
9435 | break; |
9436 | } |
9437 | |
9438 | if (modifycode == INIT_EXPR) |
9439 | { |
9440 | if (BRACE_ENCLOSED_INITIALIZER_P (rhs)) |
9441 | /* Do the default thing. */; |
9442 | else if (TREE_CODE (rhs) == CONSTRUCTOR) |
9443 | { |
9444 | /* Compound literal. */ |
9445 | if (! same_type_p (TREE_TYPE (rhs), lhstype)) |
9446 | /* Call convert to generate an error; see PR 11063. */ |
9447 | rhs = convert (lhstype, rhs); |
9448 | result = cp_build_init_expr (t: lhs, i: rhs); |
9449 | TREE_SIDE_EFFECTS (result) = 1; |
9450 | goto ret; |
9451 | } |
9452 | else if (! MAYBE_CLASS_TYPE_P (lhstype)) |
9453 | /* Do the default thing. */; |
9454 | else |
9455 | { |
9456 | releasing_vec rhs_vec = make_tree_vector_single (rhs); |
9457 | result = build_special_member_call (lhs, complete_ctor_identifier, |
9458 | &rhs_vec, lhstype, LOOKUP_NORMAL, |
9459 | complain); |
9460 | if (result == NULL_TREE) |
9461 | return error_mark_node; |
9462 | goto ret; |
9463 | } |
9464 | } |
9465 | else |
9466 | { |
9467 | lhs = require_complete_type (value: lhs, complain); |
9468 | if (lhs == error_mark_node) |
9469 | return error_mark_node; |
9470 | |
9471 | if (modifycode == NOP_EXPR) |
9472 | { |
9473 | maybe_warn_self_move (loc, lhs, rhs); |
9474 | |
9475 | if (c_dialect_objc ()) |
9476 | { |
9477 | result = objc_maybe_build_modify_expr (lhs, rhs); |
9478 | if (result) |
9479 | goto ret; |
9480 | } |
9481 | |
9482 | /* `operator=' is not an inheritable operator. */ |
9483 | if (! MAYBE_CLASS_TYPE_P (lhstype)) |
9484 | /* Do the default thing. */; |
9485 | else |
9486 | { |
9487 | result = build_new_op (input_location, MODIFY_EXPR, |
9488 | LOOKUP_NORMAL, lhs, rhs, |
9489 | make_node (NOP_EXPR), NULL_TREE, |
9490 | /*overload=*/NULL, complain); |
9491 | if (result == NULL_TREE) |
9492 | return error_mark_node; |
9493 | goto ret; |
9494 | } |
9495 | lhstype = olhstype; |
9496 | } |
9497 | else |
9498 | { |
9499 | tree init = NULL_TREE; |
9500 | |
9501 | /* A binary op has been requested. Combine the old LHS |
9502 | value with the RHS producing the value we should actually |
9503 | store into the LHS. */ |
9504 | gcc_assert (!((TYPE_REF_P (lhstype) |
9505 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype))) |
9506 | || MAYBE_CLASS_TYPE_P (lhstype))); |
9507 | |
9508 | /* Preevaluate the RHS to make sure its evaluation is complete |
9509 | before the lvalue-to-rvalue conversion of the LHS: |
9510 | |
9511 | [expr.ass] With respect to an indeterminately-sequenced |
9512 | function call, the operation of a compound assignment is a |
9513 | single evaluation. [ Note: Therefore, a function call shall |
9514 | not intervene between the lvalue-to-rvalue conversion and the |
9515 | side effect associated with any single compound assignment |
9516 | operator. -- end note ] */ |
9517 | lhs = cp_stabilize_reference (lhs); |
9518 | rhs = decay_conversion (exp: rhs, complain); |
9519 | if (rhs == error_mark_node) |
9520 | return error_mark_node; |
9521 | rhs = stabilize_expr (rhs, &init); |
9522 | newrhs = cp_build_binary_op (location: loc, code: modifycode, orig_op0: lhs, orig_op1: rhs, complain); |
9523 | if (newrhs == error_mark_node) |
9524 | { |
9525 | if (complain & tf_error) |
9526 | inform (loc, " in evaluation of %<%Q(%#T, %#T)%>" , |
9527 | modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs)); |
9528 | return error_mark_node; |
9529 | } |
9530 | |
9531 | if (init) |
9532 | newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs); |
9533 | |
9534 | /* Now it looks like a plain assignment. */ |
9535 | modifycode = NOP_EXPR; |
9536 | if (c_dialect_objc ()) |
9537 | { |
9538 | result = objc_maybe_build_modify_expr (lhs, newrhs); |
9539 | if (result) |
9540 | goto ret; |
9541 | } |
9542 | } |
9543 | gcc_assert (!TYPE_REF_P (lhstype)); |
9544 | gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs))); |
9545 | } |
9546 | |
9547 | /* The left-hand side must be an lvalue. */ |
9548 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
9549 | return error_mark_node; |
9550 | |
9551 | /* Warn about modifying something that is `const'. Don't warn if |
9552 | this is initialization. */ |
9553 | if (modifycode != INIT_EXPR |
9554 | && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype) |
9555 | /* Functions are not modifiable, even though they are |
9556 | lvalues. */ |
9557 | || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs)) |
9558 | /* If it's an aggregate and any field is const, then it is |
9559 | effectively const. */ |
9560 | || (CLASS_TYPE_P (lhstype) |
9561 | && C_TYPE_FIELDS_READONLY (lhstype)))) |
9562 | { |
9563 | if (complain & tf_error) |
9564 | cxx_readonly_error (loc, lhs, lv_assign); |
9565 | return error_mark_node; |
9566 | } |
9567 | |
9568 | /* If storing into a structure or union member, it may have been given a |
9569 | lowered bitfield type. We need to convert to the declared type first, |
9570 | so retrieve it now. */ |
9571 | |
9572 | olhstype = unlowered_expr_type (exp: lhs); |
9573 | |
9574 | /* Convert new value to destination type. */ |
9575 | |
9576 | if (TREE_CODE (lhstype) == ARRAY_TYPE) |
9577 | { |
9578 | int from_array; |
9579 | |
9580 | if (BRACE_ENCLOSED_INITIALIZER_P (newrhs)) |
9581 | { |
9582 | if (modifycode != INIT_EXPR) |
9583 | { |
9584 | if (complain & tf_error) |
9585 | error_at (loc, |
9586 | "assigning to an array from an initializer list" ); |
9587 | return error_mark_node; |
9588 | } |
9589 | if (check_array_initializer (lhs, lhstype, newrhs)) |
9590 | return error_mark_node; |
9591 | newrhs = digest_init (lhstype, newrhs, complain); |
9592 | if (newrhs == error_mark_node) |
9593 | return error_mark_node; |
9594 | } |
9595 | |
9596 | /* C++11 8.5/17: "If the destination type is an array of characters, |
9597 | an array of char16_t, an array of char32_t, or an array of wchar_t, |
9598 | and the initializer is a string literal...". */ |
9599 | else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs)) |
9600 | == STRING_CST) |
9601 | && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype))) |
9602 | && modifycode == INIT_EXPR) |
9603 | { |
9604 | newrhs = digest_init (lhstype, newrhs, complain); |
9605 | if (newrhs == error_mark_node) |
9606 | return error_mark_node; |
9607 | } |
9608 | |
9609 | else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype), |
9610 | TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))) |
9611 | { |
9612 | if (complain & tf_error) |
9613 | error_at (loc, "incompatible types in assignment of %qT to %qT" , |
9614 | TREE_TYPE (rhs), lhstype); |
9615 | return error_mark_node; |
9616 | } |
9617 | |
9618 | /* Allow array assignment in compiler-generated code. */ |
9619 | else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs)) |
9620 | /* OK, used by coroutines (co-await-initlist1.C). */; |
9621 | else if (!current_function_decl |
9622 | || !DECL_DEFAULTED_FN (current_function_decl)) |
9623 | { |
9624 | /* This routine is used for both initialization and assignment. |
9625 | Make sure the diagnostic message differentiates the context. */ |
9626 | if (complain & tf_error) |
9627 | { |
9628 | if (modifycode == INIT_EXPR) |
9629 | error_at (loc, "array used as initializer" ); |
9630 | else |
9631 | error_at (loc, "invalid array assignment" ); |
9632 | } |
9633 | return error_mark_node; |
9634 | } |
9635 | |
9636 | from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE |
9637 | ? 1 + (modifycode != INIT_EXPR): 0; |
9638 | result = build_vec_init (lhs, NULL_TREE, newrhs, |
9639 | /*explicit_value_init_p=*/false, |
9640 | from_array, complain); |
9641 | goto ret; |
9642 | } |
9643 | |
9644 | if (modifycode == INIT_EXPR) |
9645 | /* Calls with INIT_EXPR are all direct-initialization, so don't set |
9646 | LOOKUP_ONLYCONVERTING. */ |
9647 | newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL, |
9648 | ICR_INIT, NULL_TREE, 0, |
9649 | complain | tf_no_cleanup); |
9650 | else |
9651 | newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN, |
9652 | NULL_TREE, 0, complain, LOOKUP_IMPLICIT); |
9653 | |
9654 | if (!same_type_p (lhstype, olhstype)) |
9655 | newrhs = cp_convert_and_check (lhstype, newrhs, complain); |
9656 | |
9657 | if (modifycode != INIT_EXPR) |
9658 | { |
9659 | if (TREE_CODE (newrhs) == CALL_EXPR |
9660 | && TYPE_NEEDS_CONSTRUCTING (lhstype)) |
9661 | newrhs = build_cplus_new (lhstype, newrhs, complain); |
9662 | |
9663 | /* Can't initialize directly from a TARGET_EXPR, since that would |
9664 | cause the lhs to be constructed twice, and possibly result in |
9665 | accidental self-initialization. So we force the TARGET_EXPR to be |
9666 | expanded without a target. */ |
9667 | if (TREE_CODE (newrhs) == TARGET_EXPR) |
9668 | newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs, |
9669 | TREE_OPERAND (newrhs, 0)); |
9670 | } |
9671 | |
9672 | if (newrhs == error_mark_node) |
9673 | return error_mark_node; |
9674 | |
9675 | if (c_dialect_objc () && flag_objc_gc) |
9676 | { |
9677 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); |
9678 | |
9679 | if (result) |
9680 | goto ret; |
9681 | } |
9682 | |
9683 | result = build2_loc (loc, code: modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR, |
9684 | type: lhstype, arg0: lhs, arg1: newrhs); |
9685 | if (modifycode == INIT_EXPR) |
9686 | set_target_expr_eliding (newrhs); |
9687 | |
9688 | TREE_SIDE_EFFECTS (result) = 1; |
9689 | if (!plain_assign) |
9690 | suppress_warning (result, OPT_Wparentheses); |
9691 | |
9692 | ret: |
9693 | if (preeval) |
9694 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result); |
9695 | return result; |
9696 | } |
9697 | |
9698 | cp_expr |
9699 | build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, |
9700 | tree rhs, tree lookups, tsubst_flags_t complain) |
9701 | { |
9702 | tree orig_lhs = lhs; |
9703 | tree orig_rhs = rhs; |
9704 | tree overload = NULL_TREE; |
9705 | |
9706 | if (lhs == error_mark_node || rhs == error_mark_node) |
9707 | return cp_expr (error_mark_node, loc); |
9708 | |
9709 | tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE); |
9710 | |
9711 | if (processing_template_decl) |
9712 | { |
9713 | if (type_dependent_expression_p (lhs) |
9714 | || type_dependent_expression_p (rhs)) |
9715 | { |
9716 | tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs); |
9717 | if (modifycode != NOP_EXPR) |
9718 | TREE_TYPE (rval) |
9719 | = build_dependent_operator_type (lookups, code: modifycode, is_assign: true); |
9720 | return rval; |
9721 | } |
9722 | } |
9723 | |
9724 | tree rval; |
9725 | if (modifycode == NOP_EXPR) |
9726 | rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain); |
9727 | else |
9728 | rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, |
9729 | lhs, rhs, op, lookups, &overload, complain); |
9730 | if (rval == error_mark_node) |
9731 | return error_mark_node; |
9732 | if (processing_template_decl) |
9733 | { |
9734 | if (overload != NULL_TREE) |
9735 | return (build_min_non_dep_op_overload |
9736 | (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs)); |
9737 | |
9738 | return (build_min_non_dep |
9739 | (MODOP_EXPR, rval, orig_lhs, op, orig_rhs)); |
9740 | } |
9741 | return rval; |
9742 | } |
9743 | |
9744 | /* Helper function for get_delta_difference which assumes FROM is a base |
9745 | class of TO. Returns a delta for the conversion of pointer-to-member |
9746 | of FROM to pointer-to-member of TO. If the conversion is invalid and |
9747 | tf_error is not set in COMPLAIN returns error_mark_node, otherwise |
9748 | returns zero. If FROM is not a base class of TO, returns NULL_TREE. |
9749 | If C_CAST_P is true, this conversion is taking place as part of a |
9750 | C-style cast. */ |
9751 | |
9752 | static tree |
9753 | get_delta_difference_1 (tree from, tree to, bool c_cast_p, |
9754 | tsubst_flags_t complain) |
9755 | { |
9756 | tree binfo; |
9757 | base_kind kind; |
9758 | |
9759 | binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check, |
9760 | &kind, complain); |
9761 | |
9762 | if (binfo == error_mark_node) |
9763 | { |
9764 | if (!(complain & tf_error)) |
9765 | return error_mark_node; |
9766 | |
9767 | inform (input_location, " in pointer to member function conversion" ); |
9768 | return size_zero_node; |
9769 | } |
9770 | else if (binfo) |
9771 | { |
9772 | if (kind != bk_via_virtual) |
9773 | return BINFO_OFFSET (binfo); |
9774 | else |
9775 | /* FROM is a virtual base class of TO. Issue an error or warning |
9776 | depending on whether or not this is a reinterpret cast. */ |
9777 | { |
9778 | if (!(complain & tf_error)) |
9779 | return error_mark_node; |
9780 | |
9781 | error ("pointer to member conversion via virtual base %qT" , |
9782 | BINFO_TYPE (binfo_from_vbase (binfo))); |
9783 | |
9784 | return size_zero_node; |
9785 | } |
9786 | } |
9787 | else |
9788 | return NULL_TREE; |
9789 | } |
9790 | |
9791 | /* Get difference in deltas for different pointer to member function |
9792 | types. If the conversion is invalid and tf_error is not set in |
9793 | COMPLAIN, returns error_mark_node, otherwise returns an integer |
9794 | constant of type PTRDIFF_TYPE_NODE and its value is zero if the |
9795 | conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse |
9796 | conversions as well. If C_CAST_P is true this conversion is taking |
9797 | place as part of a C-style cast. |
9798 | |
9799 | Note that the naming of FROM and TO is kind of backwards; the return |
9800 | value is what we add to a TO in order to get a FROM. They are named |
9801 | this way because we call this function to find out how to convert from |
9802 | a pointer to member of FROM to a pointer to member of TO. */ |
9803 | |
9804 | static tree |
9805 | get_delta_difference (tree from, tree to, |
9806 | bool allow_inverse_p, |
9807 | bool c_cast_p, tsubst_flags_t complain) |
9808 | { |
9809 | tree result; |
9810 | |
9811 | if (same_type_ignoring_top_level_qualifiers_p (type1: from, type2: to)) |
9812 | /* Pointer to member of incomplete class is permitted*/ |
9813 | result = size_zero_node; |
9814 | else |
9815 | result = get_delta_difference_1 (from, to, c_cast_p, complain); |
9816 | |
9817 | if (result == error_mark_node) |
9818 | return error_mark_node; |
9819 | |
9820 | if (!result) |
9821 | { |
9822 | if (!allow_inverse_p) |
9823 | { |
9824 | if (!(complain & tf_error)) |
9825 | return error_mark_node; |
9826 | |
9827 | error_not_base_type (from, to); |
9828 | inform (input_location, " in pointer to member conversion" ); |
9829 | result = size_zero_node; |
9830 | } |
9831 | else |
9832 | { |
9833 | result = get_delta_difference_1 (from: to, to: from, c_cast_p, complain); |
9834 | |
9835 | if (result == error_mark_node) |
9836 | return error_mark_node; |
9837 | |
9838 | if (result) |
9839 | result = size_diffop_loc (input_location, |
9840 | size_zero_node, result); |
9841 | else |
9842 | { |
9843 | if (!(complain & tf_error)) |
9844 | return error_mark_node; |
9845 | |
9846 | error_not_base_type (from, to); |
9847 | inform (input_location, " in pointer to member conversion" ); |
9848 | result = size_zero_node; |
9849 | } |
9850 | } |
9851 | } |
9852 | |
9853 | return convert_to_integer (ptrdiff_type_node, result); |
9854 | } |
9855 | |
9856 | /* Return a constructor for the pointer-to-member-function TYPE using |
9857 | the other components as specified. */ |
9858 | |
9859 | tree |
9860 | build_ptrmemfunc1 (tree type, tree delta, tree pfn) |
9861 | { |
9862 | tree u = NULL_TREE; |
9863 | tree delta_field; |
9864 | tree pfn_field; |
9865 | vec<constructor_elt, va_gc> *v; |
9866 | |
9867 | /* Pull the FIELD_DECLs out of the type. */ |
9868 | pfn_field = TYPE_FIELDS (type); |
9869 | delta_field = DECL_CHAIN (pfn_field); |
9870 | |
9871 | /* Make sure DELTA has the type we want. */ |
9872 | delta = convert_and_check (input_location, delta_type_node, delta); |
9873 | |
9874 | /* Convert to the correct target type if necessary. */ |
9875 | pfn = fold_convert (TREE_TYPE (pfn_field), pfn); |
9876 | |
9877 | /* Finish creating the initializer. */ |
9878 | vec_alloc (v, nelems: 2); |
9879 | CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn); |
9880 | CONSTRUCTOR_APPEND_ELT(v, delta_field, delta); |
9881 | u = build_constructor (type, v); |
9882 | TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta); |
9883 | TREE_STATIC (u) = (TREE_CONSTANT (u) |
9884 | && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn)) |
9885 | != NULL_TREE) |
9886 | && (initializer_constant_valid_p (delta, TREE_TYPE (delta)) |
9887 | != NULL_TREE)); |
9888 | return u; |
9889 | } |
9890 | |
9891 | /* Build a constructor for a pointer to member function. It can be |
9892 | used to initialize global variables, local variable, or used |
9893 | as a value in expressions. TYPE is the POINTER to METHOD_TYPE we |
9894 | want to be. |
9895 | |
9896 | If FORCE is nonzero, then force this conversion, even if |
9897 | we would rather not do it. Usually set when using an explicit |
9898 | cast. A C-style cast is being processed iff C_CAST_P is true. |
9899 | |
9900 | Return error_mark_node, if something goes wrong. */ |
9901 | |
9902 | tree |
9903 | build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p, |
9904 | tsubst_flags_t complain) |
9905 | { |
9906 | tree fn; |
9907 | tree pfn_type; |
9908 | tree to_type; |
9909 | |
9910 | if (error_operand_p (t: pfn)) |
9911 | return error_mark_node; |
9912 | |
9913 | pfn_type = TREE_TYPE (pfn); |
9914 | to_type = build_ptrmemfunc_type (type); |
9915 | |
9916 | /* Handle multiple conversions of pointer to member functions. */ |
9917 | if (TYPE_PTRMEMFUNC_P (pfn_type)) |
9918 | { |
9919 | tree delta = NULL_TREE; |
9920 | tree npfn = NULL_TREE; |
9921 | tree n; |
9922 | |
9923 | if (!force |
9924 | && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, |
9925 | LOOKUP_NORMAL, complain)) |
9926 | { |
9927 | if (complain & tf_error) |
9928 | error ("invalid conversion to type %qT from type %qT" , |
9929 | to_type, pfn_type); |
9930 | else |
9931 | return error_mark_node; |
9932 | } |
9933 | |
9934 | n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type), |
9935 | TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type), |
9936 | allow_inverse_p: force, |
9937 | c_cast_p, complain); |
9938 | if (n == error_mark_node) |
9939 | return error_mark_node; |
9940 | |
9941 | STRIP_ANY_LOCATION_WRAPPER (pfn); |
9942 | |
9943 | /* We don't have to do any conversion to convert a |
9944 | pointer-to-member to its own type. But, we don't want to |
9945 | just return a PTRMEM_CST if there's an explicit cast; that |
9946 | cast should make the expression an invalid template argument. */ |
9947 | if (TREE_CODE (pfn) != PTRMEM_CST |
9948 | && same_type_p (to_type, pfn_type)) |
9949 | return pfn; |
9950 | |
9951 | if (TREE_SIDE_EFFECTS (pfn)) |
9952 | pfn = save_expr (pfn); |
9953 | |
9954 | /* Obtain the function pointer and the current DELTA. */ |
9955 | if (TREE_CODE (pfn) == PTRMEM_CST) |
9956 | expand_ptrmemfunc_cst (pfn, &delta, &npfn); |
9957 | else |
9958 | { |
9959 | npfn = build_ptrmemfunc_access_expr (ptrmem: pfn, pfn_identifier); |
9960 | delta = build_ptrmemfunc_access_expr (ptrmem: pfn, delta_identifier); |
9961 | } |
9962 | |
9963 | /* Just adjust the DELTA field. */ |
9964 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
9965 | (TREE_TYPE (delta), ptrdiff_type_node)); |
9966 | if (!integer_zerop (n)) |
9967 | { |
9968 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta) |
9969 | n = cp_build_binary_op (location: input_location, |
9970 | code: LSHIFT_EXPR, orig_op0: n, integer_one_node, |
9971 | complain); |
9972 | delta = cp_build_binary_op (location: input_location, |
9973 | code: PLUS_EXPR, orig_op0: delta, orig_op1: n, complain); |
9974 | } |
9975 | return build_ptrmemfunc1 (type: to_type, delta, pfn: npfn); |
9976 | } |
9977 | |
9978 | /* Handle null pointer to member function conversions. */ |
9979 | if (null_ptr_cst_p (pfn)) |
9980 | { |
9981 | pfn = cp_build_c_cast (loc: input_location, |
9982 | TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type), |
9983 | expr: pfn, complain); |
9984 | return build_ptrmemfunc1 (type: to_type, |
9985 | integer_zero_node, |
9986 | pfn); |
9987 | } |
9988 | |
9989 | if (type_unknown_p (expr: pfn)) |
9990 | return instantiate_type (type, pfn, complain); |
9991 | |
9992 | fn = TREE_OPERAND (pfn, 0); |
9993 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL |
9994 | /* In a template, we will have preserved the |
9995 | OFFSET_REF. */ |
9996 | || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF)); |
9997 | return make_ptrmem_cst (to_type, fn); |
9998 | } |
9999 | |
10000 | /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST |
10001 | given by CST. |
10002 | |
10003 | ??? There is no consistency as to the types returned for the above |
10004 | values. Some code acts as if it were a sizetype and some as if it were |
10005 | integer_type_node. */ |
10006 | |
10007 | void |
10008 | expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn) |
10009 | { |
10010 | tree type = TREE_TYPE (cst); |
10011 | tree fn = PTRMEM_CST_MEMBER (cst); |
10012 | tree ptr_class, fn_class; |
10013 | |
10014 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); |
10015 | |
10016 | /* The class that the function belongs to. */ |
10017 | fn_class = DECL_CONTEXT (fn); |
10018 | |
10019 | /* The class that we're creating a pointer to member of. */ |
10020 | ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type); |
10021 | |
10022 | /* First, calculate the adjustment to the function's class. */ |
10023 | *delta = get_delta_difference (from: fn_class, to: ptr_class, /*force=*/allow_inverse_p: 0, |
10024 | /*c_cast_p=*/0, complain: tf_warning_or_error); |
10025 | |
10026 | if (!DECL_VIRTUAL_P (fn)) |
10027 | { |
10028 | tree t = build_addr_func (fn, tf_warning_or_error); |
10029 | if (TREE_CODE (t) == ADDR_EXPR) |
10030 | SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst)); |
10031 | *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t); |
10032 | } |
10033 | else |
10034 | { |
10035 | /* If we're dealing with a virtual function, we have to adjust 'this' |
10036 | again, to point to the base which provides the vtable entry for |
10037 | fn; the call will do the opposite adjustment. */ |
10038 | tree orig_class = DECL_CONTEXT (fn); |
10039 | tree binfo = binfo_or_else (orig_class, fn_class); |
10040 | *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta), |
10041 | *delta, BINFO_OFFSET (binfo)); |
10042 | |
10043 | /* We set PFN to the vtable offset at which the function can be |
10044 | found, plus one (unless ptrmemfunc_vbit_in_delta, in which |
10045 | case delta is shifted left, and then incremented). */ |
10046 | *pfn = DECL_VINDEX (fn); |
10047 | *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn, |
10048 | TYPE_SIZE_UNIT (vtable_entry_type)); |
10049 | |
10050 | switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) |
10051 | { |
10052 | case ptrmemfunc_vbit_in_pfn: |
10053 | *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn, |
10054 | integer_one_node); |
10055 | break; |
10056 | |
10057 | case ptrmemfunc_vbit_in_delta: |
10058 | *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta), |
10059 | *delta, integer_one_node); |
10060 | *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta), |
10061 | *delta, integer_one_node); |
10062 | break; |
10063 | |
10064 | default: |
10065 | gcc_unreachable (); |
10066 | } |
10067 | |
10068 | *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn); |
10069 | } |
10070 | } |
10071 | |
10072 | /* Return an expression for PFN from the pointer-to-member function |
10073 | given by T. */ |
10074 | |
10075 | static tree |
10076 | pfn_from_ptrmemfunc (tree t) |
10077 | { |
10078 | if (TREE_CODE (t) == PTRMEM_CST) |
10079 | { |
10080 | tree delta; |
10081 | tree pfn; |
10082 | |
10083 | expand_ptrmemfunc_cst (cst: t, delta: &delta, pfn: &pfn); |
10084 | if (pfn) |
10085 | return pfn; |
10086 | } |
10087 | |
10088 | return build_ptrmemfunc_access_expr (ptrmem: t, pfn_identifier); |
10089 | } |
10090 | |
10091 | /* Return an expression for DELTA from the pointer-to-member function |
10092 | given by T. */ |
10093 | |
10094 | static tree |
10095 | delta_from_ptrmemfunc (tree t) |
10096 | { |
10097 | if (TREE_CODE (t) == PTRMEM_CST) |
10098 | { |
10099 | tree delta; |
10100 | tree pfn; |
10101 | |
10102 | expand_ptrmemfunc_cst (cst: t, delta: &delta, pfn: &pfn); |
10103 | if (delta) |
10104 | return delta; |
10105 | } |
10106 | |
10107 | return build_ptrmemfunc_access_expr (ptrmem: t, delta_identifier); |
10108 | } |
10109 | |
10110 | /* Convert value RHS to type TYPE as preparation for an assignment to |
10111 | an lvalue of type TYPE. ERRTYPE indicates what kind of error the |
10112 | implicit conversion is. If FNDECL is non-NULL, we are doing the |
10113 | conversion in order to pass the PARMNUMth argument of FNDECL. |
10114 | If FNDECL is NULL, we are doing the conversion in function pointer |
10115 | argument passing, conversion in initialization, etc. */ |
10116 | |
10117 | static tree |
10118 | convert_for_assignment (tree type, tree rhs, |
10119 | impl_conv_rhs errtype, tree fndecl, int parmnum, |
10120 | tsubst_flags_t complain, int flags) |
10121 | { |
10122 | tree rhstype; |
10123 | enum tree_code coder; |
10124 | |
10125 | location_t rhs_loc = cp_expr_loc_or_input_loc (t: rhs); |
10126 | bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION; |
10127 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue, |
10128 | but preserve location wrappers. */ |
10129 | if (TREE_CODE (rhs) == NON_LVALUE_EXPR |
10130 | && !location_wrapper_p (exp: rhs)) |
10131 | rhs = TREE_OPERAND (rhs, 0); |
10132 | |
10133 | /* Handle [dcl.init.list] direct-list-initialization from |
10134 | single element of enumeration with a fixed underlying type. */ |
10135 | if (is_direct_enum_init (type, rhs)) |
10136 | { |
10137 | tree elt = CONSTRUCTOR_ELT (rhs, 0)->value; |
10138 | if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain)) |
10139 | { |
10140 | warning_sentinel w (warn_useless_cast); |
10141 | warning_sentinel w2 (warn_ignored_qualifiers); |
10142 | rhs = cp_build_c_cast (loc: rhs_loc, type, expr: elt, complain); |
10143 | } |
10144 | else |
10145 | rhs = error_mark_node; |
10146 | } |
10147 | |
10148 | rhstype = TREE_TYPE (rhs); |
10149 | coder = TREE_CODE (rhstype); |
10150 | |
10151 | if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE |
10152 | && vector_types_convertible_p (t1: type, t2: rhstype, emit_lax_note: true)) |
10153 | { |
10154 | rhs = mark_rvalue_use (rhs); |
10155 | return convert (type, rhs); |
10156 | } |
10157 | |
10158 | if (rhs == error_mark_node || rhstype == error_mark_node) |
10159 | return error_mark_node; |
10160 | if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node) |
10161 | return error_mark_node; |
10162 | |
10163 | /* The RHS of an assignment cannot have void type. */ |
10164 | if (coder == VOID_TYPE) |
10165 | { |
10166 | if (complain & tf_error) |
10167 | error_at (rhs_loc, "void value not ignored as it ought to be" ); |
10168 | return error_mark_node; |
10169 | } |
10170 | |
10171 | if (c_dialect_objc ()) |
10172 | { |
10173 | int parmno; |
10174 | tree selector; |
10175 | tree rname = fndecl; |
10176 | |
10177 | switch (errtype) |
10178 | { |
10179 | case ICR_ASSIGN: |
10180 | parmno = -1; |
10181 | break; |
10182 | case ICR_INIT: |
10183 | parmno = -2; |
10184 | break; |
10185 | default: |
10186 | selector = objc_message_selector (); |
10187 | parmno = parmnum; |
10188 | if (selector && parmno > 1) |
10189 | { |
10190 | rname = selector; |
10191 | parmno -= 1; |
10192 | } |
10193 | } |
10194 | |
10195 | if (objc_compare_types (type, rhstype, parmno, rname)) |
10196 | { |
10197 | rhs = mark_rvalue_use (rhs); |
10198 | return convert (type, rhs); |
10199 | } |
10200 | } |
10201 | |
10202 | /* [expr.ass] |
10203 | |
10204 | The expression is implicitly converted (clause _conv_) to the |
10205 | cv-unqualified type of the left operand. |
10206 | |
10207 | We allow bad conversions here because by the time we get to this point |
10208 | we are committed to doing the conversion. If we end up doing a bad |
10209 | conversion, convert_like will complain. */ |
10210 | if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain)) |
10211 | { |
10212 | /* When -Wno-pmf-conversions is use, we just silently allow |
10213 | conversions from pointers-to-members to plain pointers. If |
10214 | the conversion doesn't work, cp_convert will complain. */ |
10215 | if (!warn_pmf2ptr |
10216 | && TYPE_PTR_P (type) |
10217 | && TYPE_PTRMEMFUNC_P (rhstype)) |
10218 | rhs = cp_convert (strip_top_quals (type), rhs, complain); |
10219 | else |
10220 | { |
10221 | if (complain & tf_error) |
10222 | { |
10223 | /* If the right-hand side has unknown type, then it is an |
10224 | overloaded function. Call instantiate_type to get error |
10225 | messages. */ |
10226 | if (rhstype == unknown_type_node) |
10227 | { |
10228 | tree r = instantiate_type (type, rhs, tf_warning_or_error); |
10229 | /* -fpermissive might allow this; recurse. */ |
10230 | if (!seen_error ()) |
10231 | return convert_for_assignment (type, rhs: r, errtype, fndecl, |
10232 | parmnum, complain, flags); |
10233 | } |
10234 | else if (fndecl) |
10235 | complain_about_bad_argument (arg_loc: rhs_loc, |
10236 | from_type: rhstype, to_type: type, |
10237 | fndecl, parmnum); |
10238 | else |
10239 | { |
10240 | range_label_for_type_mismatch label (rhstype, type); |
10241 | gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL); |
10242 | auto_diagnostic_group d; |
10243 | |
10244 | switch (errtype) |
10245 | { |
10246 | case ICR_DEFAULT_ARGUMENT: |
10247 | error_at (&richloc, |
10248 | "cannot convert %qH to %qI in default argument" , |
10249 | rhstype, type); |
10250 | break; |
10251 | case ICR_ARGPASS: |
10252 | error_at (&richloc, |
10253 | "cannot convert %qH to %qI in argument passing" , |
10254 | rhstype, type); |
10255 | break; |
10256 | case ICR_CONVERTING: |
10257 | error_at (&richloc, "cannot convert %qH to %qI" , |
10258 | rhstype, type); |
10259 | break; |
10260 | case ICR_INIT: |
10261 | error_at (&richloc, |
10262 | "cannot convert %qH to %qI in initialization" , |
10263 | rhstype, type); |
10264 | break; |
10265 | case ICR_RETURN: |
10266 | error_at (&richloc, "cannot convert %qH to %qI in return" , |
10267 | rhstype, type); |
10268 | break; |
10269 | case ICR_ASSIGN: |
10270 | error_at (&richloc, |
10271 | "cannot convert %qH to %qI in assignment" , |
10272 | rhstype, type); |
10273 | break; |
10274 | default: |
10275 | gcc_unreachable(); |
10276 | } |
10277 | } |
10278 | |
10279 | /* See if we can be more helpful. */ |
10280 | maybe_show_nonconverting_candidate (type, rhstype, rhs, flags); |
10281 | |
10282 | if (TYPE_PTR_P (rhstype) |
10283 | && TYPE_PTR_P (type) |
10284 | && CLASS_TYPE_P (TREE_TYPE (rhstype)) |
10285 | && CLASS_TYPE_P (TREE_TYPE (type)) |
10286 | && !COMPLETE_TYPE_P (TREE_TYPE (rhstype))) |
10287 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL |
10288 | (TREE_TYPE (rhstype))), |
10289 | "class type %qT is incomplete" , TREE_TYPE (rhstype)); |
10290 | } |
10291 | return error_mark_node; |
10292 | } |
10293 | } |
10294 | if (warn_suggest_attribute_format) |
10295 | { |
10296 | const enum tree_code codel = TREE_CODE (type); |
10297 | if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) |
10298 | && coder == codel |
10299 | && check_missing_format_attribute (type, rhstype) |
10300 | && (complain & tf_warning)) |
10301 | switch (errtype) |
10302 | { |
10303 | case ICR_ARGPASS: |
10304 | case ICR_DEFAULT_ARGUMENT: |
10305 | if (fndecl) |
10306 | warning (OPT_Wsuggest_attribute_format, |
10307 | "parameter %qP of %qD might be a candidate " |
10308 | "for a format attribute" , parmnum, fndecl); |
10309 | else |
10310 | warning (OPT_Wsuggest_attribute_format, |
10311 | "parameter might be a candidate " |
10312 | "for a format attribute" ); |
10313 | break; |
10314 | case ICR_CONVERTING: |
10315 | warning (OPT_Wsuggest_attribute_format, |
10316 | "target of conversion might be a candidate " |
10317 | "for a format attribute" ); |
10318 | break; |
10319 | case ICR_INIT: |
10320 | warning (OPT_Wsuggest_attribute_format, |
10321 | "target of initialization might be a candidate " |
10322 | "for a format attribute" ); |
10323 | break; |
10324 | case ICR_RETURN: |
10325 | warning (OPT_Wsuggest_attribute_format, |
10326 | "return type might be a candidate " |
10327 | "for a format attribute" ); |
10328 | break; |
10329 | case ICR_ASSIGN: |
10330 | warning (OPT_Wsuggest_attribute_format, |
10331 | "left-hand side of assignment might be a candidate " |
10332 | "for a format attribute" ); |
10333 | break; |
10334 | default: |
10335 | gcc_unreachable(); |
10336 | } |
10337 | } |
10338 | |
10339 | /* If -Wparentheses, warn about a = b = c when a has type bool and b |
10340 | does not. */ |
10341 | if (TREE_CODE (type) == BOOLEAN_TYPE |
10342 | && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE) |
10343 | maybe_warn_unparenthesized_assignment (rhs, complain); |
10344 | |
10345 | if (complain & tf_warning) |
10346 | warn_for_address_or_pointer_of_packed_member (type, rhs); |
10347 | |
10348 | return perform_implicit_conversion_flags (strip_top_quals (type), rhs, |
10349 | complain, flags); |
10350 | } |
10351 | |
10352 | /* Convert RHS to be of type TYPE. |
10353 | If EXP is nonzero, it is the target of the initialization. |
10354 | ERRTYPE indicates what kind of error the implicit conversion is. |
10355 | |
10356 | Two major differences between the behavior of |
10357 | `convert_for_assignment' and `convert_for_initialization' |
10358 | are that references are bashed in the former, while |
10359 | copied in the latter, and aggregates are assigned in |
10360 | the former (operator=) while initialized in the |
10361 | latter (X(X&)). |
10362 | |
10363 | If using constructor make sure no conversion operator exists, if one does |
10364 | exist, an ambiguity exists. */ |
10365 | |
10366 | tree |
10367 | convert_for_initialization (tree exp, tree type, tree rhs, int flags, |
10368 | impl_conv_rhs errtype, tree fndecl, int parmnum, |
10369 | tsubst_flags_t complain) |
10370 | { |
10371 | enum tree_code codel = TREE_CODE (type); |
10372 | tree rhstype; |
10373 | enum tree_code coder; |
10374 | |
10375 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
10376 | Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */ |
10377 | if (TREE_CODE (rhs) == NOP_EXPR |
10378 | && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0)) |
10379 | && codel != REFERENCE_TYPE) |
10380 | rhs = TREE_OPERAND (rhs, 0); |
10381 | |
10382 | if (type == error_mark_node |
10383 | || rhs == error_mark_node |
10384 | || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)) |
10385 | return error_mark_node; |
10386 | |
10387 | if (MAYBE_CLASS_TYPE_P (non_reference (type))) |
10388 | ; |
10389 | else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE |
10390 | && TREE_CODE (type) != ARRAY_TYPE |
10391 | && (!TYPE_REF_P (type) |
10392 | || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)) |
10393 | || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE |
10394 | && !TYPE_REFFN_P (type)) |
10395 | || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE) |
10396 | rhs = decay_conversion (exp: rhs, complain); |
10397 | |
10398 | rhstype = TREE_TYPE (rhs); |
10399 | coder = TREE_CODE (rhstype); |
10400 | |
10401 | if (coder == ERROR_MARK) |
10402 | return error_mark_node; |
10403 | |
10404 | /* We accept references to incomplete types, so we can |
10405 | return here before checking if RHS is of complete type. */ |
10406 | |
10407 | if (codel == REFERENCE_TYPE) |
10408 | { |
10409 | auto_diagnostic_group d; |
10410 | /* This should eventually happen in convert_arguments. */ |
10411 | int savew = 0, savee = 0; |
10412 | |
10413 | if (fndecl) |
10414 | savew = warningcount + werrorcount, savee = errorcount; |
10415 | rhs = initialize_reference (type, rhs, flags, complain); |
10416 | |
10417 | if (fndecl |
10418 | && (warningcount + werrorcount > savew || errorcount > savee)) |
10419 | inform (get_fndecl_argument_location (fndecl, parmnum), |
10420 | "in passing argument %P of %qD" , parmnum, fndecl); |
10421 | return rhs; |
10422 | } |
10423 | |
10424 | if (exp != 0) |
10425 | exp = require_complete_type (value: exp, complain); |
10426 | if (exp == error_mark_node) |
10427 | return error_mark_node; |
10428 | |
10429 | type = complete_type (type); |
10430 | |
10431 | if (DIRECT_INIT_EXPR_P (type, rhs)) |
10432 | /* Don't try to do copy-initialization if we already have |
10433 | direct-initialization. */ |
10434 | return rhs; |
10435 | |
10436 | if (MAYBE_CLASS_TYPE_P (type)) |
10437 | return perform_implicit_conversion_flags (type, rhs, complain, flags); |
10438 | |
10439 | return convert_for_assignment (type, rhs, errtype, fndecl, parmnum, |
10440 | complain, flags); |
10441 | } |
10442 | |
10443 | /* If RETVAL is the address of, or a reference to, a local variable or |
10444 | temporary give an appropriate warning and return true. */ |
10445 | |
10446 | static bool |
10447 | maybe_warn_about_returning_address_of_local (tree retval, location_t loc) |
10448 | { |
10449 | tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl)); |
10450 | tree whats_returned = fold_for_warn (retval); |
10451 | if (!loc) |
10452 | loc = cp_expr_loc_or_input_loc (t: retval); |
10453 | |
10454 | for (;;) |
10455 | { |
10456 | if (TREE_CODE (whats_returned) == COMPOUND_EXPR) |
10457 | whats_returned = TREE_OPERAND (whats_returned, 1); |
10458 | else if (CONVERT_EXPR_P (whats_returned) |
10459 | || TREE_CODE (whats_returned) == NON_LVALUE_EXPR) |
10460 | whats_returned = TREE_OPERAND (whats_returned, 0); |
10461 | else |
10462 | break; |
10463 | } |
10464 | |
10465 | if (TREE_CODE (whats_returned) == TARGET_EXPR |
10466 | && is_std_init_list (TREE_TYPE (whats_returned))) |
10467 | { |
10468 | tree init = TARGET_EXPR_INITIAL (whats_returned); |
10469 | if (TREE_CODE (init) == CONSTRUCTOR) |
10470 | /* Pull out the array address. */ |
10471 | whats_returned = CONSTRUCTOR_ELT (init, 0)->value; |
10472 | else if (TREE_CODE (init) == INDIRECT_REF) |
10473 | /* The source of a trivial copy looks like *(T*)&var. */ |
10474 | whats_returned = TREE_OPERAND (init, 0); |
10475 | else |
10476 | return false; |
10477 | STRIP_NOPS (whats_returned); |
10478 | } |
10479 | |
10480 | /* As a special case, we handle a call to std::move or std::forward. */ |
10481 | if (TREE_CODE (whats_returned) == CALL_EXPR |
10482 | && (is_std_move_p (whats_returned) |
10483 | || is_std_forward_p (whats_returned))) |
10484 | { |
10485 | tree arg = CALL_EXPR_ARG (whats_returned, 0); |
10486 | return maybe_warn_about_returning_address_of_local (retval: arg, loc); |
10487 | } |
10488 | |
10489 | if (TREE_CODE (whats_returned) != ADDR_EXPR) |
10490 | return false; |
10491 | whats_returned = TREE_OPERAND (whats_returned, 0); |
10492 | |
10493 | while (TREE_CODE (whats_returned) == COMPONENT_REF |
10494 | || TREE_CODE (whats_returned) == ARRAY_REF) |
10495 | whats_returned = TREE_OPERAND (whats_returned, 0); |
10496 | |
10497 | if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR |
10498 | || TREE_CODE (whats_returned) == TARGET_EXPR) |
10499 | { |
10500 | if (TYPE_REF_P (valtype)) |
10501 | warning_at (loc, OPT_Wreturn_local_addr, |
10502 | "returning reference to temporary" ); |
10503 | else if (is_std_init_list (valtype)) |
10504 | warning_at (loc, OPT_Winit_list_lifetime, |
10505 | "returning temporary %<initializer_list%> does not extend " |
10506 | "the lifetime of the underlying array" ); |
10507 | return true; |
10508 | } |
10509 | |
10510 | STRIP_ANY_LOCATION_WRAPPER (whats_returned); |
10511 | |
10512 | if (DECL_P (whats_returned) |
10513 | && DECL_NAME (whats_returned) |
10514 | && DECL_FUNCTION_SCOPE_P (whats_returned) |
10515 | && !is_capture_proxy (whats_returned) |
10516 | && !(TREE_STATIC (whats_returned) |
10517 | || TREE_PUBLIC (whats_returned))) |
10518 | { |
10519 | if (VAR_P (whats_returned) |
10520 | && DECL_DECOMPOSITION_P (whats_returned) |
10521 | && DECL_DECOMP_BASE (whats_returned) |
10522 | && DECL_HAS_VALUE_EXPR_P (whats_returned)) |
10523 | { |
10524 | /* When returning address of a structured binding, if the structured |
10525 | binding is not a reference, continue normally, if it is a |
10526 | reference, recurse on the initializer of the structured |
10527 | binding. */ |
10528 | tree base = DECL_DECOMP_BASE (whats_returned); |
10529 | if (TYPE_REF_P (TREE_TYPE (base))) |
10530 | { |
10531 | if (tree init = DECL_INITIAL (base)) |
10532 | return maybe_warn_about_returning_address_of_local (retval: init, loc); |
10533 | else |
10534 | return false; |
10535 | } |
10536 | } |
10537 | bool w = false; |
10538 | auto_diagnostic_group d; |
10539 | if (TYPE_REF_P (valtype)) |
10540 | w = warning_at (loc, OPT_Wreturn_local_addr, |
10541 | "reference to local variable %qD returned" , |
10542 | whats_returned); |
10543 | else if (is_std_init_list (valtype)) |
10544 | w = warning_at (loc, OPT_Winit_list_lifetime, |
10545 | "returning local %<initializer_list%> variable %qD " |
10546 | "does not extend the lifetime of the underlying array" , |
10547 | whats_returned); |
10548 | else if (POINTER_TYPE_P (valtype) |
10549 | && TREE_CODE (whats_returned) == LABEL_DECL) |
10550 | w = warning_at (loc, OPT_Wreturn_local_addr, |
10551 | "address of label %qD returned" , |
10552 | whats_returned); |
10553 | else if (POINTER_TYPE_P (valtype)) |
10554 | w = warning_at (loc, OPT_Wreturn_local_addr, |
10555 | "address of local variable %qD returned" , |
10556 | whats_returned); |
10557 | if (w) |
10558 | inform (DECL_SOURCE_LOCATION (whats_returned), |
10559 | "declared here" ); |
10560 | return true; |
10561 | } |
10562 | |
10563 | return false; |
10564 | } |
10565 | |
10566 | /* Returns true if DECL is in the std namespace. */ |
10567 | |
10568 | bool |
10569 | decl_in_std_namespace_p (tree decl) |
10570 | { |
10571 | while (decl) |
10572 | { |
10573 | decl = decl_namespace_context (decl); |
10574 | if (DECL_NAMESPACE_STD_P (decl)) |
10575 | return true; |
10576 | /* Allow inline namespaces inside of std namespace, e.g. with |
10577 | --enable-symvers=gnu-versioned-namespace std::forward would be |
10578 | actually std::_8::forward. */ |
10579 | if (!DECL_NAMESPACE_INLINE_P (decl)) |
10580 | return false; |
10581 | decl = CP_DECL_CONTEXT (decl); |
10582 | } |
10583 | return false; |
10584 | } |
10585 | |
10586 | /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */ |
10587 | |
10588 | static bool |
10589 | is_std_forward_p (tree fn) |
10590 | { |
10591 | /* std::forward only takes one argument. */ |
10592 | if (call_expr_nargs (fn) != 1) |
10593 | return false; |
10594 | |
10595 | tree fndecl = cp_get_callee_fndecl_nofold (fn); |
10596 | if (!decl_in_std_namespace_p (decl: fndecl)) |
10597 | return false; |
10598 | |
10599 | tree name = DECL_NAME (fndecl); |
10600 | return name && id_equal (id: name, str: "forward" ); |
10601 | } |
10602 | |
10603 | /* Returns true if FN, a CALL_EXPR, is a call to std::move. */ |
10604 | |
10605 | static bool |
10606 | is_std_move_p (tree fn) |
10607 | { |
10608 | /* std::move only takes one argument. */ |
10609 | if (call_expr_nargs (fn) != 1) |
10610 | return false; |
10611 | |
10612 | tree fndecl = cp_get_callee_fndecl_nofold (fn); |
10613 | if (!decl_in_std_namespace_p (decl: fndecl)) |
10614 | return false; |
10615 | |
10616 | tree name = DECL_NAME (fndecl); |
10617 | return name && id_equal (id: name, str: "move" ); |
10618 | } |
10619 | |
10620 | /* Returns true if RETVAL is a good candidate for the NRVO as per |
10621 | [class.copy.elision]. FUNCTYPE is the type the function is declared |
10622 | to return. */ |
10623 | |
10624 | static bool |
10625 | can_do_nrvo_p (tree retval, tree functype) |
10626 | { |
10627 | if (functype == error_mark_node) |
10628 | return false; |
10629 | if (retval) |
10630 | STRIP_ANY_LOCATION_WRAPPER (retval); |
10631 | tree result = DECL_RESULT (current_function_decl); |
10632 | return (retval != NULL_TREE |
10633 | && !processing_template_decl |
10634 | /* Must be a local, automatic variable. */ |
10635 | && VAR_P (retval) |
10636 | && DECL_CONTEXT (retval) == current_function_decl |
10637 | && !TREE_STATIC (retval) |
10638 | /* And not a lambda or anonymous union proxy. */ |
10639 | && !DECL_HAS_VALUE_EXPR_P (retval) |
10640 | && (DECL_ALIGN (retval) <= DECL_ALIGN (result)) |
10641 | /* The cv-unqualified type of the returned value must be the |
10642 | same as the cv-unqualified return type of the |
10643 | function. */ |
10644 | && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)), |
10645 | TYPE_MAIN_VARIANT (functype)) |
10646 | /* And the returned value must be non-volatile. */ |
10647 | && !TYPE_VOLATILE (TREE_TYPE (retval))); |
10648 | } |
10649 | |
10650 | /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we |
10651 | would otherwise return in memory. */ |
10652 | |
10653 | static bool |
10654 | want_nrvo_p (tree retval, tree functype) |
10655 | { |
10656 | return (can_do_nrvo_p (retval, functype) |
10657 | && aggregate_value_p (functype, current_function_decl)); |
10658 | } |
10659 | |
10660 | /* Like can_do_nrvo_p, but we check if we're trying to move a class |
10661 | prvalue. */ |
10662 | |
10663 | static bool |
10664 | can_elide_copy_prvalue_p (tree retval, tree functype) |
10665 | { |
10666 | if (functype == error_mark_node) |
10667 | return false; |
10668 | if (retval) |
10669 | STRIP_ANY_LOCATION_WRAPPER (retval); |
10670 | return (retval != NULL_TREE |
10671 | && !glvalue_p (retval) |
10672 | && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)), |
10673 | TYPE_MAIN_VARIANT (functype)) |
10674 | && !TYPE_VOLATILE (TREE_TYPE (retval))); |
10675 | } |
10676 | |
10677 | /* If we should treat RETVAL, an expression being returned, as if it were |
10678 | designated by an rvalue, returns it adjusted accordingly; otherwise, returns |
10679 | NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return |
10680 | context (rather than throw). */ |
10681 | |
10682 | tree |
10683 | treat_lvalue_as_rvalue_p (tree expr, bool return_p) |
10684 | { |
10685 | if (cxx_dialect == cxx98) |
10686 | return NULL_TREE; |
10687 | |
10688 | tree retval = expr; |
10689 | STRIP_ANY_LOCATION_WRAPPER (retval); |
10690 | if (REFERENCE_REF_P (retval)) |
10691 | retval = TREE_OPERAND (retval, 0); |
10692 | |
10693 | /* An implicitly movable entity is a variable of automatic storage duration |
10694 | that is either a non-volatile object or (C++20) an rvalue reference to a |
10695 | non-volatile object type. */ |
10696 | if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval)) |
10697 | || TREE_CODE (retval) == PARM_DECL) |
10698 | && !TREE_STATIC (retval) |
10699 | && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval))) |
10700 | && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE |
10701 | || (cxx_dialect >= cxx20 |
10702 | && TYPE_REF_IS_RVALUE (TREE_TYPE (retval)))))) |
10703 | return NULL_TREE; |
10704 | |
10705 | /* If the expression in a return or co_return statement is a (possibly |
10706 | parenthesized) id-expression that names an implicitly movable entity |
10707 | declared in the body or parameter-declaration-clause of the innermost |
10708 | enclosing function or lambda-expression, */ |
10709 | if (DECL_CONTEXT (retval) != current_function_decl) |
10710 | return NULL_TREE; |
10711 | if (return_p) |
10712 | { |
10713 | expr = move (expr); |
10714 | if (expr == error_mark_node) |
10715 | return NULL_TREE; |
10716 | return set_implicit_rvalue_p (expr); |
10717 | } |
10718 | |
10719 | /* if the operand of a throw-expression is a (possibly parenthesized) |
10720 | id-expression that names an implicitly movable entity whose scope does not |
10721 | extend beyond the compound-statement of the innermost try-block or |
10722 | function-try-block (if any) whose compound-statement or ctor-initializer |
10723 | encloses the throw-expression, */ |
10724 | |
10725 | /* C++20 added move on throw of parms. */ |
10726 | if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20) |
10727 | return NULL_TREE; |
10728 | |
10729 | for (cp_binding_level *b = current_binding_level; |
10730 | ; b = b->level_chain) |
10731 | { |
10732 | for (tree decl = b->names; decl; decl = TREE_CHAIN (decl)) |
10733 | if (decl == retval) |
10734 | return set_implicit_rvalue_p (move (expr)); |
10735 | if (b->kind == sk_function_parms || b->kind == sk_try) |
10736 | return NULL_TREE; |
10737 | } |
10738 | } |
10739 | |
10740 | /* Warn about dubious usage of std::move (in a return statement, if RETURN_P |
10741 | is true). EXPR is the std::move expression; TYPE is the type of the object |
10742 | being initialized. */ |
10743 | |
10744 | void |
10745 | maybe_warn_pessimizing_move (tree expr, tree type, bool return_p) |
10746 | { |
10747 | if (!(warn_pessimizing_move || warn_redundant_move)) |
10748 | return; |
10749 | |
10750 | const location_t loc = cp_expr_loc_or_input_loc (t: expr); |
10751 | |
10752 | /* C++98 doesn't know move. */ |
10753 | if (cxx_dialect < cxx11) |
10754 | return; |
10755 | |
10756 | /* Wait until instantiation time, since we can't gauge if we should do |
10757 | the NRVO until then. */ |
10758 | if (processing_template_decl) |
10759 | return; |
10760 | |
10761 | /* This is only interesting for class types. */ |
10762 | if (!CLASS_TYPE_P (type)) |
10763 | return; |
10764 | |
10765 | bool wrapped_p = false; |
10766 | /* A a = std::move (A()); */ |
10767 | if (TREE_CODE (expr) == TREE_LIST) |
10768 | { |
10769 | if (list_length (expr) == 1) |
10770 | { |
10771 | expr = TREE_VALUE (expr); |
10772 | wrapped_p = true; |
10773 | } |
10774 | else |
10775 | return; |
10776 | } |
10777 | /* A a = {std::move (A())}; |
10778 | A a{std::move (A())}; */ |
10779 | else if (TREE_CODE (expr) == CONSTRUCTOR) |
10780 | { |
10781 | if (CONSTRUCTOR_NELTS (expr) == 1) |
10782 | { |
10783 | expr = CONSTRUCTOR_ELT (expr, 0)->value; |
10784 | wrapped_p = true; |
10785 | } |
10786 | else |
10787 | return; |
10788 | } |
10789 | |
10790 | /* First, check if this is a call to std::move. */ |
10791 | if (!REFERENCE_REF_P (expr) |
10792 | || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR) |
10793 | return; |
10794 | tree fn = TREE_OPERAND (expr, 0); |
10795 | if (!is_std_move_p (fn)) |
10796 | return; |
10797 | tree arg = CALL_EXPR_ARG (fn, 0); |
10798 | if (TREE_CODE (arg) != NOP_EXPR) |
10799 | return; |
10800 | /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO |
10801 | and implicitly-movable warnings. */ |
10802 | if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR) |
10803 | { |
10804 | arg = TREE_OPERAND (arg, 0); |
10805 | arg = TREE_OPERAND (arg, 0); |
10806 | arg = convert_from_reference (arg); |
10807 | if (can_elide_copy_prvalue_p (retval: arg, functype: type)) |
10808 | { |
10809 | auto_diagnostic_group d; |
10810 | if (warning_at (loc, OPT_Wpessimizing_move, |
10811 | "moving a temporary object prevents copy elision" )) |
10812 | inform (loc, "remove %<std::move%> call" ); |
10813 | } |
10814 | /* The rest of the warnings is only relevant for when we are returning |
10815 | from a function. */ |
10816 | if (!return_p) |
10817 | return; |
10818 | |
10819 | tree moved; |
10820 | /* Warn if we could do copy elision were it not for the move. */ |
10821 | if (can_do_nrvo_p (retval: arg, functype: type)) |
10822 | { |
10823 | auto_diagnostic_group d; |
10824 | if (!warning_suppressed_p (expr, OPT_Wpessimizing_move) |
10825 | && warning_at (loc, OPT_Wpessimizing_move, |
10826 | "moving a local object in a return statement " |
10827 | "prevents copy elision" )) |
10828 | inform (loc, "remove %<std::move%> call" ); |
10829 | } |
10830 | /* Warn if the move is redundant. It is redundant when we would |
10831 | do maybe-rvalue overload resolution even without std::move. */ |
10832 | else if (warn_redundant_move |
10833 | /* This doesn't apply for return {std::move (t)};. */ |
10834 | && !wrapped_p |
10835 | && !warning_suppressed_p (expr, OPT_Wredundant_move) |
10836 | && (moved = treat_lvalue_as_rvalue_p (expr: arg, /*return*/return_p: true))) |
10837 | { |
10838 | /* Make sure that overload resolution would actually succeed |
10839 | if we removed the std::move call. */ |
10840 | tree t = convert_for_initialization (NULL_TREE, type, |
10841 | rhs: moved, |
10842 | flags: (LOOKUP_NORMAL |
10843 | | LOOKUP_ONLYCONVERTING), |
10844 | errtype: ICR_RETURN, NULL_TREE, parmnum: 0, |
10845 | complain: tf_none); |
10846 | /* If this worked, implicit rvalue would work, so the call to |
10847 | std::move is redundant. */ |
10848 | if (t != error_mark_node) |
10849 | { |
10850 | auto_diagnostic_group d; |
10851 | if (warning_at (loc, OPT_Wredundant_move, |
10852 | "redundant move in return statement" )) |
10853 | inform (loc, "remove %<std::move%> call" ); |
10854 | } |
10855 | } |
10856 | } |
10857 | /* Also try to warn about redundant std::move in code such as |
10858 | T f (const T& t) |
10859 | { |
10860 | return std::move(t); |
10861 | } |
10862 | for which EXPR will be something like |
10863 | *std::move<const T&> ((const struct T &) (const struct T *) t) |
10864 | and where the std::move does nothing if T does not have a T(const T&&) |
10865 | constructor, because the argument is const. It will not use T(T&&) |
10866 | because that would mean losing the const. */ |
10867 | else if (warn_redundant_move |
10868 | && !warning_suppressed_p (expr, OPT_Wredundant_move) |
10869 | && TYPE_REF_P (TREE_TYPE (arg)) |
10870 | && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg)))) |
10871 | { |
10872 | tree rtype = TREE_TYPE (TREE_TYPE (arg)); |
10873 | if (!same_type_ignoring_top_level_qualifiers_p (type1: rtype, type2: type)) |
10874 | return; |
10875 | /* Check for the unlikely case there's T(const T&&) (we don't care if |
10876 | it's deleted). */ |
10877 | for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype))) |
10878 | if (move_fn_p (fn)) |
10879 | { |
10880 | tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn)); |
10881 | if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t)))) |
10882 | return; |
10883 | } |
10884 | auto_diagnostic_group d; |
10885 | if (return_p |
10886 | ? warning_at (loc, OPT_Wredundant_move, |
10887 | "redundant move in return statement" ) |
10888 | : warning_at (loc, OPT_Wredundant_move, |
10889 | "redundant move in initialization" )) |
10890 | inform (loc, "remove %<std::move%> call" ); |
10891 | } |
10892 | } |
10893 | |
10894 | /* Check that returning RETVAL from the current function is valid. |
10895 | Return an expression explicitly showing all conversions required to |
10896 | change RETVAL into the function return type, and to assign it to |
10897 | the DECL_RESULT for the function. Set *NO_WARNING to true if |
10898 | code reaches end of non-void function warning shouldn't be issued |
10899 | on this RETURN_EXPR. Set *DANGLING to true if code returns the |
10900 | address of a local variable. */ |
10901 | |
10902 | tree |
10903 | check_return_expr (tree retval, bool *no_warning, bool *dangling) |
10904 | { |
10905 | tree result; |
10906 | /* The type actually returned by the function. */ |
10907 | tree valtype; |
10908 | /* The type the function is declared to return, or void if |
10909 | the declared type is incomplete. */ |
10910 | tree functype; |
10911 | int fn_returns_value_p; |
10912 | location_t loc = cp_expr_loc_or_input_loc (t: retval); |
10913 | |
10914 | *no_warning = false; |
10915 | *dangling = false; |
10916 | |
10917 | /* A `volatile' function is one that isn't supposed to return, ever. |
10918 | (This is a G++ extension, used to get better code for functions |
10919 | that call the `volatile' function.) */ |
10920 | if (TREE_THIS_VOLATILE (current_function_decl)) |
10921 | warning (0, "function declared %<noreturn%> has a %<return%> statement" ); |
10922 | |
10923 | /* Check for various simple errors. */ |
10924 | if (DECL_DESTRUCTOR_P (current_function_decl)) |
10925 | { |
10926 | if (retval) |
10927 | error_at (loc, "returning a value from a destructor" ); |
10928 | |
10929 | if (targetm.cxx.cdtor_returns_this () && !processing_template_decl) |
10930 | retval = current_class_ptr; |
10931 | else |
10932 | return NULL_TREE; |
10933 | } |
10934 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) |
10935 | { |
10936 | if (in_function_try_handler) |
10937 | /* If a return statement appears in a handler of the |
10938 | function-try-block of a constructor, the program is ill-formed. */ |
10939 | error ("cannot return from a handler of a function-try-block of a constructor" ); |
10940 | else if (retval) |
10941 | /* You can't return a value from a constructor. */ |
10942 | error_at (loc, "returning a value from a constructor" ); |
10943 | |
10944 | if (targetm.cxx.cdtor_returns_this () && !processing_template_decl) |
10945 | retval = current_class_ptr; |
10946 | else |
10947 | return NULL_TREE; |
10948 | } |
10949 | |
10950 | const tree saved_retval = retval; |
10951 | |
10952 | if (processing_template_decl) |
10953 | { |
10954 | current_function_returns_value = 1; |
10955 | |
10956 | if (check_for_bare_parameter_packs (retval)) |
10957 | return error_mark_node; |
10958 | |
10959 | /* If one of the types might be void, we can't tell whether we're |
10960 | returning a value. */ |
10961 | if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))) |
10962 | && !FNDECL_USED_AUTO (current_function_decl)) |
10963 | || (retval != NULL_TREE |
10964 | && (TREE_TYPE (retval) == NULL_TREE |
10965 | || WILDCARD_TYPE_P (TREE_TYPE (retval))))) |
10966 | goto dependent; |
10967 | } |
10968 | |
10969 | functype = TREE_TYPE (TREE_TYPE (current_function_decl)); |
10970 | |
10971 | /* Deduce auto return type from a return statement. */ |
10972 | if (FNDECL_USED_AUTO (current_function_decl)) |
10973 | { |
10974 | tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl); |
10975 | tree auto_node; |
10976 | tree type; |
10977 | |
10978 | if (!retval && !is_auto (pattern)) |
10979 | { |
10980 | /* Give a helpful error message. */ |
10981 | error ("return-statement with no value, in function returning %qT" , |
10982 | pattern); |
10983 | inform (input_location, "only plain %<auto%> return type can be " |
10984 | "deduced to %<void%>" ); |
10985 | type = error_mark_node; |
10986 | } |
10987 | else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval)) |
10988 | { |
10989 | error ("returning initializer list" ); |
10990 | type = error_mark_node; |
10991 | } |
10992 | else |
10993 | { |
10994 | if (!retval) |
10995 | retval = void_node; |
10996 | auto_node = type_uses_auto (pattern); |
10997 | type = do_auto_deduction (pattern, retval, auto_node, |
10998 | tf_warning_or_error, adc_return_type); |
10999 | } |
11000 | |
11001 | if (type == error_mark_node) |
11002 | /* Leave it. */; |
11003 | else if (functype == pattern) |
11004 | apply_deduced_return_type (current_function_decl, type); |
11005 | else if (!same_type_p (type, functype)) |
11006 | { |
11007 | if (LAMBDA_FUNCTION_P (current_function_decl)) |
11008 | error_at (loc, "inconsistent types %qT and %qT deduced for " |
11009 | "lambda return type" , functype, type); |
11010 | else |
11011 | error_at (loc, "inconsistent deduction for auto return type: " |
11012 | "%qT and then %qT" , functype, type); |
11013 | } |
11014 | functype = type; |
11015 | } |
11016 | |
11017 | result = DECL_RESULT (current_function_decl); |
11018 | valtype = TREE_TYPE (result); |
11019 | gcc_assert (valtype != NULL_TREE); |
11020 | fn_returns_value_p = !VOID_TYPE_P (valtype); |
11021 | |
11022 | /* Check for a return statement with no return value in a function |
11023 | that's supposed to return a value. */ |
11024 | if (!retval && fn_returns_value_p) |
11025 | { |
11026 | if (functype != error_mark_node) |
11027 | permerror (input_location, "return-statement with no value, in " |
11028 | "function returning %qT" , valtype); |
11029 | /* Remember that this function did return. */ |
11030 | current_function_returns_value = 1; |
11031 | /* And signal caller that TREE_NO_WARNING should be set on the |
11032 | RETURN_EXPR to avoid control reaches end of non-void function |
11033 | warnings in tree-cfg.cc. */ |
11034 | *no_warning = true; |
11035 | } |
11036 | /* Check for a return statement with a value in a function that |
11037 | isn't supposed to return a value. */ |
11038 | else if (retval && !fn_returns_value_p) |
11039 | { |
11040 | if (VOID_TYPE_P (TREE_TYPE (retval))) |
11041 | /* You can return a `void' value from a function of `void' |
11042 | type. In that case, we have to evaluate the expression for |
11043 | its side-effects. */ |
11044 | finish_expr_stmt (retval); |
11045 | else if (retval != error_mark_node) |
11046 | permerror (loc, "return-statement with a value, in function " |
11047 | "returning %qT" , valtype); |
11048 | current_function_returns_null = 1; |
11049 | |
11050 | /* There's really no value to return, after all. */ |
11051 | return NULL_TREE; |
11052 | } |
11053 | else if (!retval) |
11054 | /* Remember that this function can sometimes return without a |
11055 | value. */ |
11056 | current_function_returns_null = 1; |
11057 | else |
11058 | /* Remember that this function did return a value. */ |
11059 | current_function_returns_value = 1; |
11060 | |
11061 | /* Check for erroneous operands -- but after giving ourselves a |
11062 | chance to provide an error about returning a value from a void |
11063 | function. */ |
11064 | if (error_operand_p (t: retval)) |
11065 | { |
11066 | current_function_return_value = error_mark_node; |
11067 | return error_mark_node; |
11068 | } |
11069 | |
11070 | /* Only operator new(...) throw(), can return NULL [expr.new/13]. */ |
11071 | if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl)) |
11072 | && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl)) |
11073 | && ! flag_check_new |
11074 | && retval && null_ptr_cst_p (retval)) |
11075 | warning (0, "%<operator new%> must not return NULL unless it is " |
11076 | "declared %<throw()%> (or %<-fcheck-new%> is in effect)" ); |
11077 | |
11078 | /* Effective C++ rule 15. See also start_function. */ |
11079 | if (warn_ecpp |
11080 | && DECL_NAME (current_function_decl) == assign_op_identifier |
11081 | && !type_dependent_expression_p (retval)) |
11082 | { |
11083 | bool warn = true; |
11084 | |
11085 | /* The function return type must be a reference to the current |
11086 | class. */ |
11087 | if (TYPE_REF_P (valtype) |
11088 | && same_type_ignoring_top_level_qualifiers_p |
11089 | (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))) |
11090 | { |
11091 | /* Returning '*this' is obviously OK. */ |
11092 | if (retval == current_class_ref) |
11093 | warn = false; |
11094 | /* If we are calling a function whose return type is the same of |
11095 | the current class reference, it is ok. */ |
11096 | else if (INDIRECT_REF_P (retval) |
11097 | && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR) |
11098 | warn = false; |
11099 | } |
11100 | |
11101 | if (warn) |
11102 | warning_at (loc, OPT_Weffc__, |
11103 | "%<operator=%> should return a reference to %<*this%>" ); |
11104 | } |
11105 | |
11106 | if (dependent_type_p (functype) |
11107 | || type_dependent_expression_p (retval)) |
11108 | { |
11109 | dependent: |
11110 | /* We should not have changed the return value. */ |
11111 | gcc_assert (retval == saved_retval); |
11112 | /* We don't know if this is an lvalue or rvalue use, but |
11113 | either way we can mark it as read. */ |
11114 | mark_exp_read (retval); |
11115 | return retval; |
11116 | } |
11117 | |
11118 | /* The fabled Named Return Value optimization, as per [class.copy]/15: |
11119 | |
11120 | [...] For a function with a class return type, if the expression |
11121 | in the return statement is the name of a local object, and the cv- |
11122 | unqualified type of the local object is the same as the function |
11123 | return type, an implementation is permitted to omit creating the tem- |
11124 | porary object to hold the function return value [...] |
11125 | |
11126 | So, if this is a value-returning function that always returns the same |
11127 | local variable, remember it. |
11128 | |
11129 | We choose the first suitable variable even if the function sometimes |
11130 | returns something else, but only if the variable is out of scope at the |
11131 | other return sites, or else we run the risk of clobbering the variable we |
11132 | chose if the other returned expression uses the chosen variable somehow. |
11133 | |
11134 | We don't currently do this if the first return is a non-variable, as it |
11135 | would be complicated to determine whether an NRV selected later was in |
11136 | scope at the point of the earlier return. We also don't currently support |
11137 | multiple variables with non-overlapping scopes (53637). |
11138 | |
11139 | See finish_function and finalize_nrv for the rest of this optimization. */ |
11140 | tree bare_retval = NULL_TREE; |
11141 | if (retval) |
11142 | { |
11143 | retval = maybe_undo_parenthesized_ref (retval); |
11144 | bare_retval = tree_strip_any_location_wrapper (exp: retval); |
11145 | } |
11146 | |
11147 | bool named_return_value_okay_p = want_nrvo_p (retval: bare_retval, functype); |
11148 | if (fn_returns_value_p && flag_elide_constructors |
11149 | && current_function_return_value != bare_retval) |
11150 | { |
11151 | if (named_return_value_okay_p |
11152 | && current_function_return_value == NULL_TREE) |
11153 | current_function_return_value = bare_retval; |
11154 | else if (current_function_return_value |
11155 | && VAR_P (current_function_return_value) |
11156 | && DECL_NAME (current_function_return_value) |
11157 | && !decl_in_scope_p (current_function_return_value)) |
11158 | { |
11159 | /* The earlier NRV is out of scope at this point, so it's safe to |
11160 | leave it alone; the current return can't refer to it. */; |
11161 | if (named_return_value_okay_p |
11162 | && !warning_suppressed_p (current_function_decl, OPT_Wnrvo)) |
11163 | { |
11164 | warning (OPT_Wnrvo, "not eliding copy on return from %qD" , |
11165 | bare_retval); |
11166 | suppress_warning (current_function_decl, OPT_Wnrvo); |
11167 | } |
11168 | } |
11169 | else |
11170 | { |
11171 | if ((named_return_value_okay_p |
11172 | || (current_function_return_value |
11173 | && current_function_return_value != error_mark_node)) |
11174 | && !warning_suppressed_p (current_function_decl, OPT_Wnrvo)) |
11175 | { |
11176 | warning (OPT_Wnrvo, "not eliding copy on return in %qD" , |
11177 | current_function_decl); |
11178 | suppress_warning (current_function_decl, OPT_Wnrvo); |
11179 | } |
11180 | current_function_return_value = error_mark_node; |
11181 | } |
11182 | } |
11183 | |
11184 | /* We don't need to do any conversions when there's nothing being |
11185 | returned. */ |
11186 | if (!retval) |
11187 | return NULL_TREE; |
11188 | |
11189 | if (!named_return_value_okay_p) |
11190 | maybe_warn_pessimizing_move (expr: retval, type: functype, /*return_p*/true); |
11191 | |
11192 | /* Do any required conversions. */ |
11193 | if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl)) |
11194 | /* No conversions are required. */ |
11195 | ; |
11196 | else |
11197 | { |
11198 | int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING; |
11199 | |
11200 | /* The functype's return type will have been set to void, if it |
11201 | was an incomplete type. Just treat this as 'return;' */ |
11202 | if (VOID_TYPE_P (functype)) |
11203 | return error_mark_node; |
11204 | |
11205 | /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes |
11206 | treated as an rvalue for the purposes of overload resolution to |
11207 | favor move constructors over copy constructors. |
11208 | |
11209 | Note that these conditions are similar to, but not as strict as, |
11210 | the conditions for the named return value optimization. */ |
11211 | bool converted = false; |
11212 | tree moved; |
11213 | /* Until C++23, this was only interesting for class type, but in C++23, |
11214 | we should do the below when we're converting rom/to a class/reference |
11215 | (a non-scalar type). */ |
11216 | if ((cxx_dialect < cxx23 |
11217 | ? CLASS_TYPE_P (functype) |
11218 | : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval))) |
11219 | && (moved = treat_lvalue_as_rvalue_p (expr: retval, /*return*/return_p: true))) |
11220 | /* In C++20 and earlier we treat the return value as an rvalue |
11221 | that can bind to lvalue refs. In C++23, such an expression is just |
11222 | an xvalue (see reference_binding). */ |
11223 | retval = moved; |
11224 | |
11225 | /* The call in a (lambda) thunk needs no conversions. */ |
11226 | if (TREE_CODE (retval) == CALL_EXPR |
11227 | && call_from_lambda_thunk_p (retval)) |
11228 | converted = true; |
11229 | |
11230 | /* First convert the value to the function's return type, then |
11231 | to the type of return value's location to handle the |
11232 | case that functype is smaller than the valtype. */ |
11233 | if (!converted) |
11234 | retval = convert_for_initialization |
11235 | (NULL_TREE, type: functype, rhs: retval, flags, errtype: ICR_RETURN, NULL_TREE, parmnum: 0, |
11236 | complain: tf_warning_or_error); |
11237 | retval = convert (valtype, retval); |
11238 | |
11239 | /* If the conversion failed, treat this just like `return;'. */ |
11240 | if (retval == error_mark_node) |
11241 | return retval; |
11242 | /* We can't initialize a register from a AGGR_INIT_EXPR. */ |
11243 | else if (! cfun->returns_struct |
11244 | && TREE_CODE (retval) == TARGET_EXPR |
11245 | && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR) |
11246 | retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, |
11247 | TREE_OPERAND (retval, 0)); |
11248 | else if (!processing_template_decl |
11249 | && maybe_warn_about_returning_address_of_local (retval, loc) |
11250 | && INDIRECT_TYPE_P (valtype)) |
11251 | *dangling = true; |
11252 | } |
11253 | |
11254 | /* A naive attempt to reduce the number of -Wdangling-reference false |
11255 | positives: if we know that this function can return a variable with |
11256 | static storage duration rather than one of its parameters, suppress |
11257 | the warning. */ |
11258 | if (warn_dangling_reference |
11259 | && TYPE_REF_P (functype) |
11260 | && bare_retval |
11261 | && VAR_P (bare_retval) |
11262 | && TREE_STATIC (bare_retval)) |
11263 | suppress_warning (current_function_decl, OPT_Wdangling_reference); |
11264 | |
11265 | if (processing_template_decl) |
11266 | return saved_retval; |
11267 | |
11268 | /* Actually copy the value returned into the appropriate location. */ |
11269 | if (retval && retval != result) |
11270 | { |
11271 | /* If there's a postcondition for a scalar return value, wrap |
11272 | retval in a call to the postcondition function. */ |
11273 | if (tree post = apply_postcondition_to_return (retval)) |
11274 | retval = post; |
11275 | retval = cp_build_init_expr (t: result, i: retval); |
11276 | } |
11277 | |
11278 | if (current_function_return_value == bare_retval) |
11279 | INIT_EXPR_NRV_P (retval) = true; |
11280 | |
11281 | if (tree set = maybe_set_retval_sentinel ()) |
11282 | retval = build2 (COMPOUND_EXPR, void_type_node, retval, set); |
11283 | |
11284 | /* If there's a postcondition for an aggregate return value, call the |
11285 | postcondition function after the return object is initialized. */ |
11286 | if (tree post = apply_postcondition_to_return (result)) |
11287 | retval = build2 (COMPOUND_EXPR, void_type_node, retval, post); |
11288 | |
11289 | return retval; |
11290 | } |
11291 | |
11292 | |
11293 | /* Returns nonzero if the pointer-type FROM can be converted to the |
11294 | pointer-type TO via a qualification conversion. If CONSTP is -1, |
11295 | then we return nonzero if the pointers are similar, and the |
11296 | cv-qualification signature of FROM is a proper subset of that of TO. |
11297 | |
11298 | If CONSTP is positive, then all outer pointers have been |
11299 | const-qualified. */ |
11300 | |
11301 | static bool |
11302 | comp_ptr_ttypes_real (tree to, tree from, int constp) |
11303 | { |
11304 | bool to_more_cv_qualified = false; |
11305 | bool is_opaque_pointer = false; |
11306 | |
11307 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) |
11308 | { |
11309 | if (TREE_CODE (to) != TREE_CODE (from)) |
11310 | return false; |
11311 | |
11312 | if (TREE_CODE (from) == OFFSET_TYPE |
11313 | && !same_type_p (TYPE_OFFSET_BASETYPE (from), |
11314 | TYPE_OFFSET_BASETYPE (to))) |
11315 | return false; |
11316 | |
11317 | /* Const and volatile mean something different for function and |
11318 | array types, so the usual checks are not appropriate. We'll |
11319 | check the array type elements in further iterations. */ |
11320 | if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE) |
11321 | { |
11322 | if (!at_least_as_qualified_p (type1: to, type2: from)) |
11323 | return false; |
11324 | |
11325 | if (!at_least_as_qualified_p (type1: from, type2: to)) |
11326 | { |
11327 | if (constp == 0) |
11328 | return false; |
11329 | to_more_cv_qualified = true; |
11330 | } |
11331 | |
11332 | if (constp > 0) |
11333 | constp &= TYPE_READONLY (to); |
11334 | } |
11335 | |
11336 | if (VECTOR_TYPE_P (to)) |
11337 | is_opaque_pointer = vector_targets_convertible_p (t1: to, t2: from); |
11338 | |
11339 | /* P0388R4 allows a conversion from int[N] to int[] but not the |
11340 | other way round. When both arrays have bounds but they do |
11341 | not match, then no conversion is possible. */ |
11342 | if (TREE_CODE (to) == ARRAY_TYPE |
11343 | && !comp_array_types (t1: to, t2: from, cb: bounds_first, /*strict=*/false)) |
11344 | return false; |
11345 | |
11346 | if (!TYPE_PTR_P (to) |
11347 | && !TYPE_PTRDATAMEM_P (to) |
11348 | /* CWG 330 says we need to look through arrays. */ |
11349 | && TREE_CODE (to) != ARRAY_TYPE) |
11350 | return ((constp >= 0 || to_more_cv_qualified) |
11351 | && (is_opaque_pointer |
11352 | || same_type_ignoring_top_level_qualifiers_p (type1: to, type2: from))); |
11353 | } |
11354 | } |
11355 | |
11356 | /* When comparing, say, char ** to char const **, this function takes |
11357 | the 'char *' and 'char const *'. Do not pass non-pointer/reference |
11358 | types to this function. */ |
11359 | |
11360 | int |
11361 | comp_ptr_ttypes (tree to, tree from) |
11362 | { |
11363 | return comp_ptr_ttypes_real (to, from, constp: 1); |
11364 | } |
11365 | |
11366 | /* Returns true iff FNTYPE is a non-class type that involves |
11367 | error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node |
11368 | if a parameter type is ill-formed. */ |
11369 | |
11370 | bool |
11371 | error_type_p (const_tree type) |
11372 | { |
11373 | tree t; |
11374 | |
11375 | switch (TREE_CODE (type)) |
11376 | { |
11377 | case ERROR_MARK: |
11378 | return true; |
11379 | |
11380 | case POINTER_TYPE: |
11381 | case REFERENCE_TYPE: |
11382 | case OFFSET_TYPE: |
11383 | return error_type_p (TREE_TYPE (type)); |
11384 | |
11385 | case FUNCTION_TYPE: |
11386 | case METHOD_TYPE: |
11387 | if (error_type_p (TREE_TYPE (type))) |
11388 | return true; |
11389 | for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t)) |
11390 | if (error_type_p (TREE_VALUE (t))) |
11391 | return true; |
11392 | return false; |
11393 | |
11394 | case RECORD_TYPE: |
11395 | if (TYPE_PTRMEMFUNC_P (type)) |
11396 | return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type)); |
11397 | return false; |
11398 | |
11399 | default: |
11400 | return false; |
11401 | } |
11402 | } |
11403 | |
11404 | /* Returns true if to and from are (possibly multi-level) pointers to the same |
11405 | type or inheritance-related types, regardless of cv-quals. */ |
11406 | |
11407 | bool |
11408 | ptr_reasonably_similar (const_tree to, const_tree from) |
11409 | { |
11410 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) |
11411 | { |
11412 | /* Any target type is similar enough to void. */ |
11413 | if (VOID_TYPE_P (to)) |
11414 | return !error_type_p (type: from); |
11415 | if (VOID_TYPE_P (from)) |
11416 | return !error_type_p (type: to); |
11417 | |
11418 | if (TREE_CODE (to) != TREE_CODE (from)) |
11419 | return false; |
11420 | |
11421 | if (TREE_CODE (from) == OFFSET_TYPE |
11422 | && comptypes (TYPE_OFFSET_BASETYPE (to), |
11423 | TYPE_OFFSET_BASETYPE (from), |
11424 | COMPARE_BASE | COMPARE_DERIVED)) |
11425 | continue; |
11426 | |
11427 | if (VECTOR_TYPE_P (to) |
11428 | && vector_types_convertible_p (t1: to, t2: from, emit_lax_note: false)) |
11429 | return true; |
11430 | |
11431 | if (TREE_CODE (to) == INTEGER_TYPE |
11432 | && TYPE_PRECISION (to) == TYPE_PRECISION (from)) |
11433 | return true; |
11434 | |
11435 | if (TREE_CODE (to) == FUNCTION_TYPE) |
11436 | return !error_type_p (type: to) && !error_type_p (type: from); |
11437 | |
11438 | if (!TYPE_PTR_P (to)) |
11439 | { |
11440 | /* When either type is incomplete avoid DERIVED_FROM_P, |
11441 | which may call complete_type (c++/57942). */ |
11442 | bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from); |
11443 | return comptypes |
11444 | (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), |
11445 | strict: b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED); |
11446 | } |
11447 | } |
11448 | } |
11449 | |
11450 | /* Return true if TO and FROM (both of which are POINTER_TYPEs or |
11451 | pointer-to-member types) are the same, ignoring cv-qualification at |
11452 | all levels. CB says how we should behave when comparing array bounds. */ |
11453 | |
11454 | bool |
11455 | comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb) |
11456 | { |
11457 | bool is_opaque_pointer = false; |
11458 | |
11459 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) |
11460 | { |
11461 | if (TREE_CODE (to) != TREE_CODE (from)) |
11462 | return false; |
11463 | |
11464 | if (TREE_CODE (from) == OFFSET_TYPE |
11465 | && same_type_p (TYPE_OFFSET_BASETYPE (from), |
11466 | TYPE_OFFSET_BASETYPE (to))) |
11467 | continue; |
11468 | |
11469 | if (VECTOR_TYPE_P (to)) |
11470 | is_opaque_pointer = vector_targets_convertible_p (t1: to, t2: from); |
11471 | |
11472 | if (TREE_CODE (to) == ARRAY_TYPE |
11473 | /* Ignore cv-qualification, but if we see e.g. int[3] and int[4], |
11474 | we must fail. */ |
11475 | && !comp_array_types (t1: to, t2: from, cb, /*strict=*/false)) |
11476 | return false; |
11477 | |
11478 | /* CWG 330 says we need to look through arrays. */ |
11479 | if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE) |
11480 | return (is_opaque_pointer |
11481 | || same_type_ignoring_top_level_qualifiers_p (type1: to, type2: from)); |
11482 | } |
11483 | } |
11484 | |
11485 | /* Returns the type qualifiers for this type, including the qualifiers on the |
11486 | elements for an array type. */ |
11487 | |
11488 | int |
11489 | cp_type_quals (const_tree type) |
11490 | { |
11491 | int quals; |
11492 | /* This CONST_CAST is okay because strip_array_types returns its |
11493 | argument unmodified and we assign it to a const_tree. */ |
11494 | type = strip_array_types (CONST_CAST_TREE (type)); |
11495 | if (type == error_mark_node |
11496 | /* Quals on a FUNCTION_TYPE are memfn quals. */ |
11497 | || TREE_CODE (type) == FUNCTION_TYPE) |
11498 | return TYPE_UNQUALIFIED; |
11499 | quals = TYPE_QUALS (type); |
11500 | /* METHOD and REFERENCE_TYPEs should never have quals. */ |
11501 | gcc_assert ((TREE_CODE (type) != METHOD_TYPE |
11502 | && !TYPE_REF_P (type)) |
11503 | || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) |
11504 | == TYPE_UNQUALIFIED)); |
11505 | return quals; |
11506 | } |
11507 | |
11508 | /* Returns the function-ref-qualifier for TYPE */ |
11509 | |
11510 | cp_ref_qualifier |
11511 | type_memfn_rqual (const_tree type) |
11512 | { |
11513 | gcc_assert (FUNC_OR_METHOD_TYPE_P (type)); |
11514 | |
11515 | if (!FUNCTION_REF_QUALIFIED (type)) |
11516 | return REF_QUAL_NONE; |
11517 | else if (FUNCTION_RVALUE_QUALIFIED (type)) |
11518 | return REF_QUAL_RVALUE; |
11519 | else |
11520 | return REF_QUAL_LVALUE; |
11521 | } |
11522 | |
11523 | /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or |
11524 | METHOD_TYPE. */ |
11525 | |
11526 | int |
11527 | type_memfn_quals (const_tree type) |
11528 | { |
11529 | if (TREE_CODE (type) == FUNCTION_TYPE) |
11530 | return TYPE_QUALS (type); |
11531 | else if (TREE_CODE (type) == METHOD_TYPE) |
11532 | return cp_type_quals (type: class_of_this_parm (fntype: type)); |
11533 | else |
11534 | gcc_unreachable (); |
11535 | } |
11536 | |
11537 | /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to |
11538 | MEMFN_QUALS and its ref-qualifier to RQUAL. */ |
11539 | |
11540 | tree |
11541 | apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual) |
11542 | { |
11543 | /* Could handle METHOD_TYPE here if necessary. */ |
11544 | gcc_assert (TREE_CODE (type) == FUNCTION_TYPE); |
11545 | if (TYPE_QUALS (type) == memfn_quals |
11546 | && type_memfn_rqual (type) == rqual) |
11547 | return type; |
11548 | |
11549 | /* This should really have a different TYPE_MAIN_VARIANT, but that gets |
11550 | complex. */ |
11551 | tree result = build_qualified_type (type, memfn_quals); |
11552 | return build_ref_qualified_type (result, rqual); |
11553 | } |
11554 | |
11555 | /* Returns nonzero if TYPE is const or volatile. */ |
11556 | |
11557 | bool |
11558 | cv_qualified_p (const_tree type) |
11559 | { |
11560 | int quals = cp_type_quals (type); |
11561 | return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0; |
11562 | } |
11563 | |
11564 | /* Returns nonzero if the TYPE contains a mutable member. */ |
11565 | |
11566 | bool |
11567 | cp_has_mutable_p (const_tree type) |
11568 | { |
11569 | /* This CONST_CAST is okay because strip_array_types returns its |
11570 | argument unmodified and we assign it to a const_tree. */ |
11571 | type = strip_array_types (CONST_CAST_TREE(type)); |
11572 | |
11573 | return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type); |
11574 | } |
11575 | |
11576 | /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the |
11577 | TYPE_QUALS. For a VAR_DECL, this may be an optimistic |
11578 | approximation. In particular, consider: |
11579 | |
11580 | int f(); |
11581 | struct S { int i; }; |
11582 | const S s = { f(); } |
11583 | |
11584 | Here, we will make "s" as TREE_READONLY (because it is declared |
11585 | "const") -- only to reverse ourselves upon seeing that the |
11586 | initializer is non-constant. */ |
11587 | |
11588 | void |
11589 | cp_apply_type_quals_to_decl (int type_quals, tree decl) |
11590 | { |
11591 | tree type = TREE_TYPE (decl); |
11592 | |
11593 | if (type == error_mark_node) |
11594 | return; |
11595 | |
11596 | if (TREE_CODE (decl) == TYPE_DECL) |
11597 | return; |
11598 | |
11599 | gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE |
11600 | && type_quals != TYPE_UNQUALIFIED)); |
11601 | |
11602 | /* Avoid setting TREE_READONLY incorrectly. */ |
11603 | /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr |
11604 | constructor can produce constant init, so rely on cp_finish_decl to |
11605 | clear TREE_READONLY if the variable has non-constant init. */ |
11606 | |
11607 | /* If the type has (or might have) a mutable component, that component |
11608 | might be modified. */ |
11609 | if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type)) |
11610 | type_quals &= ~TYPE_QUAL_CONST; |
11611 | |
11612 | c_apply_type_quals_to_decl (type_quals, decl); |
11613 | } |
11614 | |
11615 | /* Subroutine of casts_away_constness. Make T1 and T2 point at |
11616 | exemplar types such that casting T1 to T2 is casting away constness |
11617 | if and only if there is no implicit conversion from T1 to T2. */ |
11618 | |
11619 | static void |
11620 | casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain) |
11621 | { |
11622 | int quals1; |
11623 | int quals2; |
11624 | |
11625 | /* [expr.const.cast] |
11626 | |
11627 | For multi-level pointer to members and multi-level mixed pointers |
11628 | and pointers to members (conv.qual), the "member" aspect of a |
11629 | pointer to member level is ignored when determining if a const |
11630 | cv-qualifier has been cast away. */ |
11631 | /* [expr.const.cast] |
11632 | |
11633 | For two pointer types: |
11634 | |
11635 | X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type |
11636 | X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type |
11637 | K is min(N,M) |
11638 | |
11639 | casting from X1 to X2 casts away constness if, for a non-pointer |
11640 | type T there does not exist an implicit conversion (clause |
11641 | _conv_) from: |
11642 | |
11643 | Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N * |
11644 | |
11645 | to |
11646 | |
11647 | Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */ |
11648 | if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1)) |
11649 | || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2))) |
11650 | { |
11651 | *t1 = cp_build_qualified_type (void_type_node, |
11652 | cp_type_quals (type: *t1)); |
11653 | *t2 = cp_build_qualified_type (void_type_node, |
11654 | cp_type_quals (type: *t2)); |
11655 | return; |
11656 | } |
11657 | |
11658 | quals1 = cp_type_quals (type: *t1); |
11659 | quals2 = cp_type_quals (type: *t2); |
11660 | |
11661 | if (TYPE_PTRDATAMEM_P (*t1)) |
11662 | *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1); |
11663 | else |
11664 | *t1 = TREE_TYPE (*t1); |
11665 | if (TYPE_PTRDATAMEM_P (*t2)) |
11666 | *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2); |
11667 | else |
11668 | *t2 = TREE_TYPE (*t2); |
11669 | |
11670 | casts_away_constness_r (t1, t2, complain); |
11671 | *t1 = build_pointer_type (*t1); |
11672 | *t2 = build_pointer_type (*t2); |
11673 | *t1 = cp_build_qualified_type (*t1, quals1); |
11674 | *t2 = cp_build_qualified_type (*t2, quals2); |
11675 | } |
11676 | |
11677 | /* Returns nonzero if casting from TYPE1 to TYPE2 casts away |
11678 | constness. |
11679 | |
11680 | ??? This function returns non-zero if casting away qualifiers not |
11681 | just const. We would like to return to the caller exactly which |
11682 | qualifiers are casted away to give more accurate diagnostics. |
11683 | */ |
11684 | |
11685 | static bool |
11686 | casts_away_constness (tree t1, tree t2, tsubst_flags_t complain) |
11687 | { |
11688 | if (TYPE_REF_P (t2)) |
11689 | { |
11690 | /* [expr.const.cast] |
11691 | |
11692 | Casting from an lvalue of type T1 to an lvalue of type T2 |
11693 | using a reference cast casts away constness if a cast from an |
11694 | rvalue of type "pointer to T1" to the type "pointer to T2" |
11695 | casts away constness. */ |
11696 | t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1); |
11697 | return casts_away_constness (t1: build_pointer_type (t1), |
11698 | t2: build_pointer_type (TREE_TYPE (t2)), |
11699 | complain); |
11700 | } |
11701 | |
11702 | if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2)) |
11703 | /* [expr.const.cast] |
11704 | |
11705 | Casting from an rvalue of type "pointer to data member of X |
11706 | of type T1" to the type "pointer to data member of Y of type |
11707 | T2" casts away constness if a cast from an rvalue of type |
11708 | "pointer to T1" to the type "pointer to T2" casts away |
11709 | constness. */ |
11710 | return casts_away_constness |
11711 | (t1: build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)), |
11712 | t2: build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)), |
11713 | complain); |
11714 | |
11715 | /* Casting away constness is only something that makes sense for |
11716 | pointer or reference types. */ |
11717 | if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2)) |
11718 | return false; |
11719 | |
11720 | /* Top-level qualifiers don't matter. */ |
11721 | t1 = TYPE_MAIN_VARIANT (t1); |
11722 | t2 = TYPE_MAIN_VARIANT (t2); |
11723 | casts_away_constness_r (t1: &t1, t2: &t2, complain); |
11724 | if (!can_convert (t2, t1, complain)) |
11725 | return true; |
11726 | |
11727 | return false; |
11728 | } |
11729 | |
11730 | /* If T is a REFERENCE_TYPE return the type to which T refers. |
11731 | Otherwise, return T itself. */ |
11732 | |
11733 | tree |
11734 | non_reference (tree t) |
11735 | { |
11736 | if (t && TYPE_REF_P (t)) |
11737 | t = TREE_TYPE (t); |
11738 | return t; |
11739 | } |
11740 | |
11741 | |
11742 | /* Return nonzero if REF is an lvalue valid for this language; |
11743 | otherwise, print an error message and return zero. USE says |
11744 | how the lvalue is being used and so selects the error message. */ |
11745 | |
11746 | int |
11747 | lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain) |
11748 | { |
11749 | cp_lvalue_kind kind = lvalue_kind (ref); |
11750 | |
11751 | if (kind == clk_none) |
11752 | { |
11753 | if (complain & tf_error) |
11754 | lvalue_error (cp_expr_loc_or_input_loc (t: ref), use); |
11755 | return 0; |
11756 | } |
11757 | else if (kind & (clk_rvalueref|clk_class)) |
11758 | { |
11759 | if (!(complain & tf_error)) |
11760 | return 0; |
11761 | /* Make this a permerror because we used to accept it. */ |
11762 | permerror (cp_expr_loc_or_input_loc (t: ref), |
11763 | "using rvalue as lvalue" ); |
11764 | } |
11765 | return 1; |
11766 | } |
11767 | |
11768 | /* Return true if a user-defined literal operator is a raw operator. */ |
11769 | |
11770 | bool |
11771 | check_raw_literal_operator (const_tree decl) |
11772 | { |
11773 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
11774 | tree argtype; |
11775 | int arity; |
11776 | bool maybe_raw_p = false; |
11777 | |
11778 | /* Count the number and type of arguments and check for ellipsis. */ |
11779 | for (argtype = argtypes, arity = 0; |
11780 | argtype && argtype != void_list_node; |
11781 | ++arity, argtype = TREE_CHAIN (argtype)) |
11782 | { |
11783 | tree t = TREE_VALUE (argtype); |
11784 | |
11785 | if (same_type_p (t, const_string_type_node)) |
11786 | maybe_raw_p = true; |
11787 | } |
11788 | if (!argtype) |
11789 | return false; /* Found ellipsis. */ |
11790 | |
11791 | if (!maybe_raw_p || arity != 1) |
11792 | return false; |
11793 | |
11794 | return true; |
11795 | } |
11796 | |
11797 | |
11798 | /* Return true if a user-defined literal operator has one of the allowed |
11799 | argument types. */ |
11800 | |
11801 | bool |
11802 | check_literal_operator_args (const_tree decl, |
11803 | bool *long_long_unsigned_p, bool *long_double_p) |
11804 | { |
11805 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
11806 | |
11807 | *long_long_unsigned_p = false; |
11808 | *long_double_p = false; |
11809 | if (processing_template_decl || processing_specialization) |
11810 | return argtypes == void_list_node; |
11811 | else |
11812 | { |
11813 | tree argtype; |
11814 | int arity; |
11815 | int max_arity = 2; |
11816 | |
11817 | /* Count the number and type of arguments and check for ellipsis. */ |
11818 | for (argtype = argtypes, arity = 0; |
11819 | argtype && argtype != void_list_node; |
11820 | argtype = TREE_CHAIN (argtype)) |
11821 | { |
11822 | tree t = TREE_VALUE (argtype); |
11823 | ++arity; |
11824 | |
11825 | if (TYPE_PTR_P (t)) |
11826 | { |
11827 | bool maybe_raw_p = false; |
11828 | t = TREE_TYPE (t); |
11829 | if (cp_type_quals (type: t) != TYPE_QUAL_CONST) |
11830 | return false; |
11831 | t = TYPE_MAIN_VARIANT (t); |
11832 | if ((maybe_raw_p = same_type_p (t, char_type_node)) |
11833 | || same_type_p (t, wchar_type_node) |
11834 | || same_type_p (t, char8_type_node) |
11835 | || same_type_p (t, char16_type_node) |
11836 | || same_type_p (t, char32_type_node)) |
11837 | { |
11838 | argtype = TREE_CHAIN (argtype); |
11839 | if (!argtype) |
11840 | return false; |
11841 | t = TREE_VALUE (argtype); |
11842 | if (maybe_raw_p && argtype == void_list_node) |
11843 | return true; |
11844 | else if (same_type_p (t, size_type_node)) |
11845 | { |
11846 | ++arity; |
11847 | continue; |
11848 | } |
11849 | else |
11850 | return false; |
11851 | } |
11852 | } |
11853 | else if (same_type_p (t, long_long_unsigned_type_node)) |
11854 | { |
11855 | max_arity = 1; |
11856 | *long_long_unsigned_p = true; |
11857 | } |
11858 | else if (same_type_p (t, long_double_type_node)) |
11859 | { |
11860 | max_arity = 1; |
11861 | *long_double_p = true; |
11862 | } |
11863 | else if (same_type_p (t, char_type_node)) |
11864 | max_arity = 1; |
11865 | else if (same_type_p (t, wchar_type_node)) |
11866 | max_arity = 1; |
11867 | else if (same_type_p (t, char8_type_node)) |
11868 | max_arity = 1; |
11869 | else if (same_type_p (t, char16_type_node)) |
11870 | max_arity = 1; |
11871 | else if (same_type_p (t, char32_type_node)) |
11872 | max_arity = 1; |
11873 | else |
11874 | return false; |
11875 | } |
11876 | if (!argtype) |
11877 | return false; /* Found ellipsis. */ |
11878 | |
11879 | if (arity != max_arity) |
11880 | return false; |
11881 | |
11882 | return true; |
11883 | } |
11884 | } |
11885 | |
11886 | /* Always returns false since unlike C90, C++ has no concept of implicit |
11887 | function declarations. */ |
11888 | |
11889 | bool |
11890 | c_decl_implicit (const_tree) |
11891 | { |
11892 | return false; |
11893 | } |
11894 | |