]>
Commit | Line | Data |
---|---|---|
471086d6 | 1 | /* Build expressions with type checking for C++ compiler. |
aad93da1 | 2 | Copyright (C) 1987-2017 Free Software Foundation, Inc. |
471086d6 | 3 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
4 | ||
6f0d25a6 | 5 | This file is part of GCC. |
471086d6 | 6 | |
6f0d25a6 | 7 | GCC is free software; you can redistribute it and/or modify |
471086d6 | 8 | it under the terms of the GNU General Public License as published by |
aa139c3f | 9 | the Free Software Foundation; either version 3, or (at your option) |
471086d6 | 10 | any later version. |
11 | ||
6f0d25a6 | 12 | GCC is distributed in the hope that it will be useful, |
471086d6 | 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 | |
aa139c3f | 18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
471086d6 | 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 | |
c857cd60 | 25 | checks, and some optimization. */ |
471086d6 | 26 | |
471086d6 | 27 | #include "config.h" |
b3ef7553 | 28 | #include "system.h" |
805e22b2 | 29 | #include "coretypes.h" |
4cba6f60 | 30 | #include "target.h" |
4cba6f60 | 31 | #include "cp-tree.h" |
9ed99284 | 32 | #include "stor-layout.h" |
33 | #include "varasm.h" | |
87080baa | 34 | #include "intl.h" |
ee1ab431 | 35 | #include "convert.h" |
6c536c4f | 36 | #include "c-family/c-objc.h" |
9e46467d | 37 | #include "c-family/c-ubsan.h" |
6753bca0 | 38 | #include "params.h" |
2a990300 | 39 | #include "gcc-rich-location.h" |
30a86690 | 40 | #include "stringpool.h" |
41 | #include "attribs.h" | |
9917317a | 42 | #include "asan.h" |
471086d6 | 43 | |
aafa8c0a | 44 | static tree cp_build_addr_expr_strict (tree, tsubst_flags_t); |
45 | static tree cp_build_function_call (tree, tree, tsubst_flags_t); | |
e75bce43 | 46 | static tree pfn_from_ptrmemfunc (tree); |
426ab4bb | 47 | static tree delta_from_ptrmemfunc (tree); |
183407ee | 48 | static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int, |
cf7aa2e5 | 49 | tsubst_flags_t, int); |
22a3f7bd | 50 | static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree, |
51 | tsubst_flags_t); | |
ebd21de4 | 52 | static tree rationalize_conditional_expr (enum tree_code, tree, |
53 | tsubst_flags_t); | |
f0dd33b5 | 54 | static int comp_ptr_ttypes_real (tree, tree, int); |
f0dd33b5 | 55 | static bool comp_except_types (tree, tree, bool); |
9f627b1a | 56 | static bool comp_array_types (const_tree, const_tree, bool); |
eabe2d94 | 57 | static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *); |
85132186 | 58 | static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t); |
66bbeb85 | 59 | static void casts_away_constness_r (tree *, tree *, tsubst_flags_t); |
60 | static bool casts_away_constness (tree, tree, tsubst_flags_t); | |
f22a2cb7 | 61 | static bool maybe_warn_about_returning_address_of_local (tree); |
19b271a9 | 62 | static tree lookup_destructor (tree, tree, tree, tsubst_flags_t); |
e14647a7 | 63 | static void error_args_num (location_t, tree, bool); |
f1f41a6c | 64 | static int convert_arguments (tree, vec<tree, va_gc> **, tree, int, |
ebd21de4 | 65 | tsubst_flags_t); |
471086d6 | 66 | |
471086d6 | 67 | /* Do `exp = require_complete_type (exp);' to make sure exp |
4bac4c68 | 68 | does not have an incomplete type. (That includes void types.) |
dba1ea2f | 69 | Returns error_mark_node if the VALUE does not have |
4bac4c68 | 70 | complete type when this function returns. */ |
471086d6 | 71 | |
72 | tree | |
dba1ea2f | 73 | require_complete_type_sfinae (tree value, tsubst_flags_t complain) |
471086d6 | 74 | { |
e857e9c7 | 75 | tree type; |
76 | ||
aeef2be5 | 77 | if (processing_template_decl || value == error_mark_node) |
e857e9c7 | 78 | return value; |
79 | ||
8417823c | 80 | if (TREE_CODE (value) == OVERLOAD) |
81 | type = unknown_type_node; | |
82 | else | |
83 | type = TREE_TYPE (value); | |
471086d6 | 84 | |
8db6f235 | 85 | if (type == error_mark_node) |
86 | return error_mark_node; | |
87 | ||
471086d6 | 88 | /* First, detect a valid value with a complete type. */ |
4b72716d | 89 | if (COMPLETE_TYPE_P (type)) |
471086d6 | 90 | return value; |
91 | ||
dba1ea2f | 92 | if (complete_type_or_maybe_complain (type, value, complain)) |
b3a558fd | 93 | return value; |
94 | else | |
4bac4c68 | 95 | return error_mark_node; |
471086d6 | 96 | } |
97 | ||
dba1ea2f | 98 | tree |
99 | require_complete_type (tree value) | |
100 | { | |
101 | return require_complete_type_sfinae (value, tf_warning_or_error); | |
102 | } | |
103 | ||
b3a558fd | 104 | /* Try to complete TYPE, if it is incomplete. For example, if TYPE is |
105 | a template instantiation, do the instantiation. Returns TYPE, | |
106 | whether or not it could be completed, unless something goes | |
107 | horribly wrong, in which case the error_mark_node is returned. */ | |
108 | ||
e857e9c7 | 109 | tree |
f0dd33b5 | 110 | complete_type (tree type) |
e857e9c7 | 111 | { |
2d45f35b | 112 | if (type == NULL_TREE) |
113 | /* Rather than crash, we return something sure to cause an error | |
114 | at some point. */ | |
115 | return error_mark_node; | |
116 | ||
4b72716d | 117 | if (type == error_mark_node || COMPLETE_TYPE_P (type)) |
e857e9c7 | 118 | ; |
752b5a4c | 119 | else if (TREE_CODE (type) == ARRAY_TYPE) |
e857e9c7 | 120 | { |
121 | tree t = complete_type (TREE_TYPE (type)); | |
361c8c6c | 122 | unsigned int needs_constructing, has_nontrivial_dtor; |
550bbfa8 | 123 | if (COMPLETE_TYPE_P (t) && !dependent_type_p (type)) |
596c0ae6 | 124 | layout_type (type); |
361c8c6c | 125 | needs_constructing |
ec10e4ad | 126 | = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t)); |
361c8c6c | 127 | has_nontrivial_dtor |
89e923d8 | 128 | = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t)); |
361c8c6c | 129 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
130 | { | |
131 | TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing; | |
132 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor; | |
133 | } | |
e857e9c7 | 134 | } |
e4e283ec | 135 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type)) |
e4ce2dc4 | 136 | instantiate_class_template (TYPE_MAIN_VARIANT (type)); |
e857e9c7 | 137 | |
138 | return type; | |
139 | } | |
140 | ||
6fee0737 | 141 | /* Like complete_type, but issue an error if the TYPE cannot be completed. |
a20e2be9 | 142 | VALUE is used for informative diagnostics. |
aeef2be5 | 143 | Returns NULL_TREE if the type cannot be made complete. */ |
b3a558fd | 144 | |
145 | tree | |
a5f2d620 | 146 | complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain) |
b3a558fd | 147 | { |
148 | type = complete_type (type); | |
4bac4c68 | 149 | if (type == error_mark_node) |
150 | /* We already issued an error. */ | |
151 | return NULL_TREE; | |
4b72716d | 152 | else if (!COMPLETE_TYPE_P (type)) |
b3a558fd | 153 | { |
a5f2d620 | 154 | if (complain & tf_error) |
155 | cxx_incomplete_type_diagnostic (value, type, DK_ERROR); | |
b3a558fd | 156 | return NULL_TREE; |
157 | } | |
158 | else | |
159 | return type; | |
160 | } | |
161 | ||
a5f2d620 | 162 | tree |
163 | complete_type_or_else (tree type, tree value) | |
164 | { | |
165 | return complete_type_or_maybe_complain (type, value, tf_warning_or_error); | |
166 | } | |
167 | ||
471086d6 | 168 | \f |
169 | /* Return the common type of two parameter lists. | |
a09db423 | 170 | We assume that comptypes has already been done and returned 1; |
471086d6 | 171 | if that isn't so, this may crash. |
172 | ||
173 | As an optimization, free the space we allocate if the parameter | |
174 | lists are already common. */ | |
175 | ||
e6c0970b | 176 | static tree |
f82b06e0 | 177 | commonparms (tree p1, tree p2) |
471086d6 | 178 | { |
179 | tree oldargs = p1, newargs, n; | |
180 | int i, len; | |
181 | int any_change = 0; | |
471086d6 | 182 | |
183 | len = list_length (p1); | |
184 | newargs = tree_last (p1); | |
185 | ||
186 | if (newargs == void_list_node) | |
187 | i = 1; | |
188 | else | |
189 | { | |
190 | i = 0; | |
191 | newargs = 0; | |
192 | } | |
193 | ||
194 | for (; i < len; i++) | |
195 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); | |
196 | ||
197 | n = newargs; | |
198 | ||
199 | for (i = 0; p1; | |
200 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++) | |
201 | { | |
202 | if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2)) | |
203 | { | |
471086d6 | 204 | TREE_PURPOSE (n) = TREE_PURPOSE (p1); |
205 | any_change = 1; | |
206 | } | |
207 | else if (! TREE_PURPOSE (p1)) | |
208 | { | |
209 | if (TREE_PURPOSE (p2)) | |
210 | { | |
211 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); | |
212 | any_change = 1; | |
213 | } | |
214 | } | |
215 | else | |
216 | { | |
d068be10 | 217 | if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2))) |
d81e00a4 | 218 | any_change = 1; |
471086d6 | 219 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); |
220 | } | |
221 | if (TREE_VALUE (p1) != TREE_VALUE (p2)) | |
222 | { | |
223 | any_change = 1; | |
be94aed9 | 224 | TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2)); |
471086d6 | 225 | } |
226 | else | |
227 | TREE_VALUE (n) = TREE_VALUE (p1); | |
228 | } | |
229 | if (! any_change) | |
33f7d3d2 | 230 | return oldargs; |
471086d6 | 231 | |
232 | return newargs; | |
233 | } | |
234 | ||
7ece5c25 | 235 | /* Given a type, perhaps copied for a typedef, |
236 | find the "original" version of it. */ | |
6cf0bc8c | 237 | static tree |
f82b06e0 | 238 | original_type (tree t) |
7ece5c25 | 239 | { |
b5de1aa4 | 240 | int quals = cp_type_quals (t); |
ac4d57eb | 241 | while (t != error_mark_node |
242 | && TYPE_NAME (t) != NULL_TREE) | |
7ece5c25 | 243 | { |
244 | tree x = TYPE_NAME (t); | |
245 | if (TREE_CODE (x) != TYPE_DECL) | |
246 | break; | |
247 | x = DECL_ORIGINAL_TYPE (x); | |
248 | if (x == NULL_TREE) | |
249 | break; | |
250 | t = x; | |
251 | } | |
b5de1aa4 | 252 | return cp_build_qualified_type (t, quals); |
7ece5c25 | 253 | } |
254 | ||
8021532e | 255 | /* Return the common type for two arithmetic types T1 and T2 under the |
256 | usual arithmetic conversions. The default conversions have already | |
257 | been applied, and enumerated types converted to their compatible | |
258 | integer types. */ | |
8c18e707 | 259 | |
8021532e | 260 | static tree |
261 | cp_common_type (tree t1, tree t2) | |
8c18e707 | 262 | { |
263 | enum tree_code code1 = TREE_CODE (t1); | |
264 | enum tree_code code2 = TREE_CODE (t2); | |
265 | tree attributes; | |
9f75f026 | 266 | int i; |
8c18e707 | 267 | |
5ba33bf4 | 268 | |
b3353c23 | 269 | /* In what follows, we slightly generalize the rules given in [expr] so |
270 | as to deal with `long long' and `complex'. First, merge the | |
271 | attributes. */ | |
272 | attributes = (*targetm.merge_type_attributes) (t1, t2); | |
273 | ||
274 | if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2)) | |
275 | { | |
276 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
277 | return build_type_attribute_variant (t1, attributes); | |
278 | else | |
279 | return NULL_TREE; | |
280 | } | |
281 | ||
8c18e707 | 282 | /* FIXME: Attributes. */ |
9031d10b | 283 | gcc_assert (ARITHMETIC_TYPE_P (t1) |
76249021 | 284 | || VECTOR_TYPE_P (t1) |
3f00a6c0 | 285 | || UNSCOPED_ENUM_P (t1)); |
9031d10b | 286 | gcc_assert (ARITHMETIC_TYPE_P (t2) |
76249021 | 287 | || VECTOR_TYPE_P (t2) |
3f00a6c0 | 288 | || UNSCOPED_ENUM_P (t2)); |
8c18e707 | 289 | |
be94aed9 | 290 | /* If one type is complex, form the common type of the non-complex |
291 | components, then make that complex. Use T1 or T2 if it is the | |
292 | required type. */ | |
293 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
294 | { | |
295 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
296 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
297 | tree subtype | |
298 | = type_after_usual_arithmetic_conversions (subtype1, subtype2); | |
299 | ||
300 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
301 | return build_type_attribute_variant (t1, attributes); | |
302 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
303 | return build_type_attribute_variant (t2, attributes); | |
304 | else | |
305 | return build_type_attribute_variant (build_complex_type (subtype), | |
306 | attributes); | |
307 | } | |
308 | ||
955f5493 | 309 | if (code1 == VECTOR_TYPE) |
310 | { | |
311 | /* When we get here we should have two vectors of the same size. | |
312 | Just prefer the unsigned one if present. */ | |
313 | if (TYPE_UNSIGNED (t1)) | |
314 | return build_type_attribute_variant (t1, attributes); | |
315 | else | |
316 | return build_type_attribute_variant (t2, attributes); | |
317 | } | |
318 | ||
8c18e707 | 319 | /* If only one is real, use it as the result. */ |
320 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
321 | return build_type_attribute_variant (t1, attributes); | |
322 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
323 | return build_type_attribute_variant (t2, attributes); | |
324 | ||
8c18e707 | 325 | /* Both real or both integers; use the one with greater precision. */ |
326 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
327 | return build_type_attribute_variant (t1, attributes); | |
328 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
329 | return build_type_attribute_variant (t2, attributes); | |
330 | ||
05d24012 | 331 | /* The types are the same; no need to do anything fancy. */ |
332 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
333 | return build_type_attribute_variant (t1, attributes); | |
334 | ||
8c18e707 | 335 | if (code1 != REAL_TYPE) |
336 | { | |
337 | /* If one is unsigned long long, then convert the other to unsigned | |
338 | long long. */ | |
339 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node) | |
340 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node)) | |
341 | return build_type_attribute_variant (long_long_unsigned_type_node, | |
342 | attributes); | |
343 | /* If one is a long long, and the other is an unsigned long, and | |
344 | long long can represent all the values of an unsigned long, then | |
345 | convert to a long long. Otherwise, convert to an unsigned long | |
346 | long. Otherwise, if either operand is long long, convert the | |
347 | other to long long. | |
9031d10b | 348 | |
8c18e707 | 349 | Since we're here, we know the TYPE_PRECISION is the same; |
350 | therefore converting to long long cannot represent all the values | |
351 | of an unsigned long, so we choose unsigned long long in that | |
352 | case. */ | |
353 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node) | |
354 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node)) | |
355 | { | |
78a8ed03 | 356 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
9031d10b | 357 | ? long_long_unsigned_type_node |
8c18e707 | 358 | : long_long_integer_type_node); |
359 | return build_type_attribute_variant (t, attributes); | |
360 | } | |
9031d10b | 361 | |
8c18e707 | 362 | /* Go through the same procedure, but for longs. */ |
363 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node) | |
364 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node)) | |
365 | return build_type_attribute_variant (long_unsigned_type_node, | |
366 | attributes); | |
367 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node) | |
368 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node)) | |
369 | { | |
78a8ed03 | 370 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
8c18e707 | 371 | ? long_unsigned_type_node : long_integer_type_node); |
372 | return build_type_attribute_variant (t, attributes); | |
373 | } | |
19d81ec0 | 374 | |
375 | /* For __intN types, either the type is __int128 (and is lower | |
376 | priority than the types checked above, but higher than other | |
377 | 128-bit types) or it's known to not be the same size as other | |
378 | types (enforced in toplev.c). Prefer the unsigned type. */ | |
379 | for (i = 0; i < NUM_INT_N_ENTS; i ++) | |
380 | { | |
381 | if (int_n_enabled_p [i] | |
382 | && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type) | |
383 | || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type) | |
384 | || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type) | |
385 | || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type))) | |
386 | { | |
387 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
388 | ? int_n_trees[i].unsigned_type | |
389 | : int_n_trees[i].signed_type); | |
390 | return build_type_attribute_variant (t, attributes); | |
391 | } | |
392 | } | |
393 | ||
8c18e707 | 394 | /* Otherwise prefer the unsigned one. */ |
78a8ed03 | 395 | if (TYPE_UNSIGNED (t1)) |
8c18e707 | 396 | return build_type_attribute_variant (t1, attributes); |
397 | else | |
398 | return build_type_attribute_variant (t2, attributes); | |
399 | } | |
400 | else | |
401 | { | |
402 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node) | |
403 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node)) | |
404 | return build_type_attribute_variant (long_double_type_node, | |
405 | attributes); | |
406 | if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node) | |
407 | || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node)) | |
408 | return build_type_attribute_variant (double_type_node, | |
409 | attributes); | |
05d24012 | 410 | if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node) |
411 | || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node)) | |
8c18e707 | 412 | return build_type_attribute_variant (float_type_node, |
413 | attributes); | |
9031d10b | 414 | |
05d24012 | 415 | /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of |
653e5405 | 416 | the standard C++ floating-point types. Logic earlier in this |
417 | function has already eliminated the possibility that | |
418 | TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no | |
419 | compelling reason to choose one or the other. */ | |
05d24012 | 420 | return build_type_attribute_variant (t1, attributes); |
8c18e707 | 421 | } |
422 | } | |
423 | ||
8021532e | 424 | /* T1 and T2 are arithmetic or enumeration types. Return the type |
425 | that will result from the "usual arithmetic conversions" on T1 and | |
426 | T2 as described in [expr]. */ | |
427 | ||
428 | tree | |
429 | type_after_usual_arithmetic_conversions (tree t1, tree t2) | |
430 | { | |
431 | gcc_assert (ARITHMETIC_TYPE_P (t1) | |
76249021 | 432 | || VECTOR_TYPE_P (t1) |
8021532e | 433 | || UNSCOPED_ENUM_P (t1)); |
434 | gcc_assert (ARITHMETIC_TYPE_P (t2) | |
76249021 | 435 | || VECTOR_TYPE_P (t2) |
8021532e | 436 | || UNSCOPED_ENUM_P (t2)); |
437 | ||
438 | /* Perform the integral promotions. We do not promote real types here. */ | |
439 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1) | |
16732484 | 440 | && INTEGRAL_OR_ENUMERATION_TYPE_P (t2)) |
8021532e | 441 | { |
442 | t1 = type_promotes_to (t1); | |
443 | t2 = type_promotes_to (t2); | |
444 | } | |
445 | ||
446 | return cp_common_type (t1, t2); | |
447 | } | |
448 | ||
fe5f0da4 | 449 | static void |
450 | composite_pointer_error (diagnostic_t kind, tree t1, tree t2, | |
451 | composite_pointer_operation operation) | |
452 | { | |
453 | switch (operation) | |
454 | { | |
455 | case CPO_COMPARISON: | |
456 | emit_diagnostic (kind, input_location, 0, | |
457 | "comparison between " | |
458 | "distinct pointer types %qT and %qT lacks a cast", | |
459 | t1, t2); | |
460 | break; | |
461 | case CPO_CONVERSION: | |
462 | emit_diagnostic (kind, input_location, 0, | |
463 | "conversion between " | |
464 | "distinct pointer types %qT and %qT lacks a cast", | |
465 | t1, t2); | |
466 | break; | |
467 | case CPO_CONDITIONAL_EXPR: | |
468 | emit_diagnostic (kind, input_location, 0, | |
469 | "conditional expression between " | |
470 | "distinct pointer types %qT and %qT lacks a cast", | |
471 | t1, t2); | |
472 | break; | |
473 | default: | |
474 | gcc_unreachable (); | |
475 | } | |
476 | } | |
477 | ||
1bc16cab | 478 | /* Subroutine of composite_pointer_type to implement the recursive |
2f182218 | 479 | case. See that function for documentation of the parameters. */ |
1bc16cab | 480 | |
481 | static tree | |
2f182218 | 482 | composite_pointer_type_r (tree t1, tree t2, |
483 | composite_pointer_operation operation, | |
ebd21de4 | 484 | tsubst_flags_t complain) |
1bc16cab | 485 | { |
486 | tree pointee1; | |
487 | tree pointee2; | |
488 | tree result_type; | |
489 | tree attributes; | |
490 | ||
491 | /* Determine the types pointed to by T1 and T2. */ | |
c21c015b | 492 | if (TYPE_PTR_P (t1)) |
1bc16cab | 493 | { |
494 | pointee1 = TREE_TYPE (t1); | |
495 | pointee2 = TREE_TYPE (t2); | |
496 | } | |
497 | else | |
498 | { | |
499 | pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1); | |
500 | pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2); | |
501 | } | |
502 | ||
503 | /* [expr.rel] | |
504 | ||
505 | Otherwise, the composite pointer type is a pointer type | |
506 | similar (_conv.qual_) to the type of one of the operands, | |
507 | with a cv-qualification signature (_conv.qual_) that is the | |
508 | union of the cv-qualification signatures of the operand | |
509 | types. */ | |
510 | if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2)) | |
511 | result_type = pointee1; | |
c21c015b | 512 | else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2)) |
05765a91 | 513 | || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2))) |
6deac0c2 | 514 | { |
515 | result_type = composite_pointer_type_r (pointee1, pointee2, operation, | |
516 | complain); | |
517 | if (result_type == error_mark_node) | |
518 | return error_mark_node; | |
519 | } | |
1bc16cab | 520 | else |
521 | { | |
ebd21de4 | 522 | if (complain & tf_error) |
fe5f0da4 | 523 | composite_pointer_error (DK_PERMERROR, t1, t2, operation); |
15031017 | 524 | else |
525 | return error_mark_node; | |
1bc16cab | 526 | result_type = void_type_node; |
527 | } | |
528 | result_type = cp_build_qualified_type (result_type, | |
529 | (cp_type_quals (pointee1) | |
530 | | cp_type_quals (pointee2))); | |
1bc16cab | 531 | /* If the original types were pointers to members, so is the |
532 | result. */ | |
05765a91 | 533 | if (TYPE_PTRMEM_P (t1)) |
1bc16cab | 534 | { |
535 | if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), | |
15031017 | 536 | TYPE_PTRMEM_CLASS_TYPE (t2))) |
537 | { | |
538 | if (complain & tf_error) | |
539 | composite_pointer_error (DK_PERMERROR, t1, t2, operation); | |
540 | else | |
541 | return error_mark_node; | |
542 | } | |
1bc16cab | 543 | result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), |
544 | result_type); | |
545 | } | |
3180c04a | 546 | else |
547 | result_type = build_pointer_type (result_type); | |
1bc16cab | 548 | |
549 | /* Merge the attributes. */ | |
550 | attributes = (*targetm.merge_type_attributes) (t1, t2); | |
551 | return build_type_attribute_variant (result_type, attributes); | |
552 | } | |
553 | ||
8c18e707 | 554 | /* Return the composite pointer type (see [expr.rel]) for T1 and T2. |
2f182218 | 555 | ARG1 and ARG2 are the values with those types. The OPERATION is to |
556 | describe the operation between the pointer types, | |
557 | in case an error occurs. | |
1bc16cab | 558 | |
559 | This routine also implements the computation of a common type for | |
560 | pointers-to-members as per [expr.eq]. */ | |
8c18e707 | 561 | |
9031d10b | 562 | tree |
f82b06e0 | 563 | composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2, |
2f182218 | 564 | composite_pointer_operation operation, |
565 | tsubst_flags_t complain) | |
8c18e707 | 566 | { |
1bc16cab | 567 | tree class1; |
568 | tree class2; | |
8c18e707 | 569 | |
570 | /* [expr.rel] | |
571 | ||
572 | If one operand is a null pointer constant, the composite pointer | |
573 | type is the type of the other operand. */ | |
574 | if (null_ptr_cst_p (arg1)) | |
575 | return t2; | |
576 | if (null_ptr_cst_p (arg2)) | |
577 | return t1; | |
9031d10b | 578 | |
59f02dd8 | 579 | /* We have: |
580 | ||
581 | [expr.rel] | |
582 | ||
583 | If one of the operands has type "pointer to cv1 void*", then | |
584 | the other has type "pointer to cv2T", and the composite pointer | |
585 | type is "pointer to cv12 void", where cv12 is the union of cv1 | |
586 | and cv2. | |
587 | ||
588 | If either type is a pointer to void, make sure it is T1. */ | |
c21c015b | 589 | if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2))) |
dfcf26a5 | 590 | std::swap (t1, t2); |
1bc16cab | 591 | |
59f02dd8 | 592 | /* Now, if T1 is a pointer to void, merge the qualifiers. */ |
c21c015b | 593 | if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1))) |
8c18e707 | 594 | { |
1bc16cab | 595 | tree attributes; |
596 | tree result_type; | |
597 | ||
b34e07a1 | 598 | if (TYPE_PTRFN_P (t2)) |
599 | { | |
600 | if (complain & tf_error) | |
601 | { | |
602 | switch (operation) | |
603 | { | |
604 | case CPO_COMPARISON: | |
605 | pedwarn (input_location, OPT_Wpedantic, | |
606 | "ISO C++ forbids comparison between pointer " | |
607 | "of type %<void *%> and pointer-to-function"); | |
608 | break; | |
609 | case CPO_CONVERSION: | |
610 | pedwarn (input_location, OPT_Wpedantic, | |
611 | "ISO C++ forbids conversion between pointer " | |
612 | "of type %<void *%> and pointer-to-function"); | |
613 | break; | |
614 | case CPO_CONDITIONAL_EXPR: | |
615 | pedwarn (input_location, OPT_Wpedantic, | |
616 | "ISO C++ forbids conditional expression between " | |
617 | "pointer of type %<void *%> and " | |
618 | "pointer-to-function"); | |
619 | break; | |
620 | default: | |
621 | gcc_unreachable (); | |
622 | } | |
623 | } | |
624 | else | |
625 | return error_mark_node; | |
2f182218 | 626 | } |
9031d10b | 627 | result_type |
1bc16cab | 628 | = cp_build_qualified_type (void_type_node, |
629 | (cp_type_quals (TREE_TYPE (t1)) | |
630 | | cp_type_quals (TREE_TYPE (t2)))); | |
59f02dd8 | 631 | result_type = build_pointer_type (result_type); |
1bc16cab | 632 | /* Merge the attributes. */ |
633 | attributes = (*targetm.merge_type_attributes) (t1, t2); | |
634 | return build_type_attribute_variant (result_type, attributes); | |
635 | } | |
636 | ||
c21c015b | 637 | if (c_dialect_objc () && TYPE_PTR_P (t1) |
638 | && TYPE_PTR_P (t2)) | |
aa796005 | 639 | { |
4abfc532 | 640 | if (objc_have_common_type (t1, t2, -3, NULL_TREE)) |
641 | return objc_common_type (t1, t2); | |
aa796005 | 642 | } |
643 | ||
2e9e9363 | 644 | /* if T1 or T2 is "pointer to noexcept function" and the other type is |
645 | "pointer to function", where the function types are otherwise the same, | |
646 | "pointer to function" */ | |
647 | if (fnptr_conv_p (t1, t2)) | |
648 | return t1; | |
649 | if (fnptr_conv_p (t2, t1)) | |
650 | return t2; | |
651 | ||
1bc16cab | 652 | /* [expr.eq] permits the application of a pointer conversion to |
653 | bring the pointers to a common type. */ | |
c21c015b | 654 | if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2) |
1bc16cab | 655 | && CLASS_TYPE_P (TREE_TYPE (t1)) |
656 | && CLASS_TYPE_P (TREE_TYPE (t2)) | |
657 | && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1), | |
658 | TREE_TYPE (t2))) | |
659 | { | |
660 | class1 = TREE_TYPE (t1); | |
661 | class2 = TREE_TYPE (t2); | |
662 | ||
aa796005 | 663 | if (DERIVED_FROM_P (class1, class2)) |
9031d10b | 664 | t2 = (build_pointer_type |
ce494fcf | 665 | (cp_build_qualified_type (class1, cp_type_quals (class2)))); |
aa796005 | 666 | else if (DERIVED_FROM_P (class2, class1)) |
9031d10b | 667 | t1 = (build_pointer_type |
ce494fcf | 668 | (cp_build_qualified_type (class2, cp_type_quals (class1)))); |
1bc16cab | 669 | else |
2f182218 | 670 | { |
671 | if (complain & tf_error) | |
fe5f0da4 | 672 | composite_pointer_error (DK_ERROR, t1, t2, operation); |
2f182218 | 673 | return error_mark_node; |
674 | } | |
8c18e707 | 675 | } |
1bc16cab | 676 | /* [expr.eq] permits the application of a pointer-to-member |
677 | conversion to change the class type of one of the types. */ | |
05765a91 | 678 | else if (TYPE_PTRMEM_P (t1) |
2f182218 | 679 | && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), |
1bc16cab | 680 | TYPE_PTRMEM_CLASS_TYPE (t2))) |
30de7d91 | 681 | { |
1bc16cab | 682 | class1 = TYPE_PTRMEM_CLASS_TYPE (t1); |
683 | class2 = TYPE_PTRMEM_CLASS_TYPE (t2); | |
30de7d91 | 684 | |
1bc16cab | 685 | if (DERIVED_FROM_P (class1, class2)) |
686 | t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1)); | |
687 | else if (DERIVED_FROM_P (class2, class1)) | |
688 | t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2)); | |
8c18e707 | 689 | else |
2f182218 | 690 | { |
691 | if (complain & tf_error) | |
692 | switch (operation) | |
693 | { | |
694 | case CPO_COMPARISON: | |
695 | error ("comparison between distinct " | |
696 | "pointer-to-member types %qT and %qT lacks a cast", | |
697 | t1, t2); | |
698 | break; | |
699 | case CPO_CONVERSION: | |
700 | error ("conversion between distinct " | |
701 | "pointer-to-member types %qT and %qT lacks a cast", | |
702 | t1, t2); | |
703 | break; | |
704 | case CPO_CONDITIONAL_EXPR: | |
705 | error ("conditional expression between distinct " | |
706 | "pointer-to-member types %qT and %qT lacks a cast", | |
707 | t1, t2); | |
708 | break; | |
709 | default: | |
710 | gcc_unreachable (); | |
711 | } | |
712 | return error_mark_node; | |
713 | } | |
714 | } | |
715 | ||
716 | return composite_pointer_type_r (t1, t2, operation, complain); | |
8c18e707 | 717 | } |
718 | ||
be94aed9 | 719 | /* Return the merged type of two types. |
a09db423 | 720 | We assume that comptypes has already been done and returned 1; |
471086d6 | 721 | if that isn't so, this may crash. |
722 | ||
be94aed9 | 723 | This just combines attributes and default arguments; any other |
724 | differences would cause the two types to compare unalike. */ | |
471086d6 | 725 | |
726 | tree | |
f82b06e0 | 727 | merge_types (tree t1, tree t2) |
471086d6 | 728 | { |
cd16867a | 729 | enum tree_code code1; |
730 | enum tree_code code2; | |
bb0726a1 | 731 | tree attributes; |
471086d6 | 732 | |
733 | /* Save time if the two types are the same. */ | |
7ece5c25 | 734 | if (t1 == t2) |
1459abb8 | 735 | return t1; |
be94aed9 | 736 | if (original_type (t1) == original_type (t2)) |
1459abb8 | 737 | return t1; |
471086d6 | 738 | |
739 | /* If one type is nonsense, use the other. */ | |
740 | if (t1 == error_mark_node) | |
1459abb8 | 741 | return t2; |
471086d6 | 742 | if (t2 == error_mark_node) |
1459abb8 | 743 | return t1; |
471086d6 | 744 | |
86359a65 | 745 | /* Handle merging an auto redeclaration with a previous deduced |
746 | return type. */ | |
747 | if (is_auto (t1)) | |
748 | return t2; | |
749 | ||
0bf60c2b | 750 | /* Merge the attributes. */ |
57e4bbfb | 751 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
bb0726a1 | 752 | |
e857e9c7 | 753 | if (TYPE_PTRMEMFUNC_P (t1)) |
754 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); | |
755 | if (TYPE_PTRMEMFUNC_P (t2)) | |
756 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); | |
757 | ||
471086d6 | 758 | code1 = TREE_CODE (t1); |
759 | code2 = TREE_CODE (t2); | |
3cc2d970 | 760 | if (code1 != code2) |
761 | { | |
762 | gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE); | |
763 | if (code1 == TYPENAME_TYPE) | |
764 | { | |
765 | t1 = resolve_typename_type (t1, /*only_current_p=*/true); | |
766 | code1 = TREE_CODE (t1); | |
767 | } | |
768 | else | |
769 | { | |
770 | t2 = resolve_typename_type (t2, /*only_current_p=*/true); | |
771 | code2 = TREE_CODE (t2); | |
772 | } | |
773 | } | |
471086d6 | 774 | |
775 | switch (code1) | |
776 | { | |
471086d6 | 777 | case POINTER_TYPE: |
778 | case REFERENCE_TYPE: | |
be94aed9 | 779 | /* For two pointers, do this recursively on the target type. */ |
471086d6 | 780 | { |
be94aed9 | 781 | tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
13ddc993 | 782 | int quals = cp_type_quals (t1); |
de6778f0 | 783 | |
471086d6 | 784 | if (code1 == POINTER_TYPE) |
a6b9f513 | 785 | { |
786 | t1 = build_pointer_type (target); | |
787 | if (TREE_CODE (target) == METHOD_TYPE) | |
788 | t1 = build_ptrmemfunc_type (t1); | |
789 | } | |
471086d6 | 790 | else |
3d3d76c9 | 791 | t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1)); |
74002e1d | 792 | t1 = build_type_attribute_variant (t1, attributes); |
13ddc993 | 793 | t1 = cp_build_qualified_type (t1, quals); |
74002e1d | 794 | |
74002e1d | 795 | return t1; |
471086d6 | 796 | } |
471086d6 | 797 | |
be94aed9 | 798 | case OFFSET_TYPE: |
799 | { | |
25892cbf | 800 | int quals; |
801 | tree pointee; | |
802 | quals = cp_type_quals (t1); | |
803 | pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1), | |
804 | TYPE_PTRMEM_POINTED_TO_TYPE (t2)); | |
805 | t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), | |
806 | pointee); | |
807 | t1 = cp_build_qualified_type (t1, quals); | |
be94aed9 | 808 | break; |
809 | } | |
810 | ||
471086d6 | 811 | case ARRAY_TYPE: |
812 | { | |
be94aed9 | 813 | tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
471086d6 | 814 | /* Save space: see if the result is identical to one of the args. */ |
815 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) | |
bb0726a1 | 816 | return build_type_attribute_variant (t1, attributes); |
471086d6 | 817 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) |
bb0726a1 | 818 | return build_type_attribute_variant (t2, attributes); |
471086d6 | 819 | /* Merge the element types, and have a size if either arg has one. */ |
1ad432f2 | 820 | t1 = build_cplus_array_type |
821 | (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); | |
be94aed9 | 822 | break; |
471086d6 | 823 | } |
824 | ||
825 | case FUNCTION_TYPE: | |
826 | /* Function types: prefer the one that specified arg types. | |
827 | If both do, merge the arg types. Also merge the return types. */ | |
828 | { | |
be94aed9 | 829 | tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
471086d6 | 830 | tree p1 = TYPE_ARG_TYPES (t1); |
831 | tree p2 = TYPE_ARG_TYPES (t2); | |
57d30ef6 | 832 | tree parms; |
471086d6 | 833 | tree rval, raises; |
0fad3000 | 834 | bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1); |
471086d6 | 835 | |
836 | /* Save space: see if the result is identical to one of the args. */ | |
837 | if (valtype == TREE_TYPE (t1) && ! p2) | |
b3beaf30 | 838 | return cp_build_type_attribute_variant (t1, attributes); |
471086d6 | 839 | if (valtype == TREE_TYPE (t2) && ! p1) |
b3beaf30 | 840 | return cp_build_type_attribute_variant (t2, attributes); |
471086d6 | 841 | |
842 | /* Simple way if one arg fails to specify argument types. */ | |
843 | if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node) | |
c0f83203 | 844 | parms = p2; |
57d30ef6 | 845 | else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node) |
c0f83203 | 846 | parms = p1; |
57d30ef6 | 847 | else |
c0f83203 | 848 | parms = commonparms (p1, p2); |
471086d6 | 849 | |
57d30ef6 | 850 | rval = build_function_type (valtype, parms); |
851 | gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2)); | |
e116411c | 852 | gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2)); |
853 | rval = apply_memfn_quals (rval, | |
854 | type_memfn_quals (t1), | |
855 | type_memfn_rqual (t1)); | |
c0f83203 | 856 | raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), |
f2c1aabc | 857 | TYPE_RAISES_EXCEPTIONS (t2)); |
be94aed9 | 858 | t1 = build_exception_variant (rval, raises); |
0fad3000 | 859 | if (late_return_type_p) |
860 | TYPE_HAS_LATE_RETURN_TYPE (t1) = 1; | |
be94aed9 | 861 | break; |
471086d6 | 862 | } |
863 | ||
be94aed9 | 864 | case METHOD_TYPE: |
865 | { | |
866 | /* Get this value the long way, since TYPE_METHOD_BASETYPE | |
867 | is just the main variant of this. */ | |
a8d966c1 | 868 | tree basetype = class_of_this_parm (t2); |
c0f83203 | 869 | tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), |
f2c1aabc | 870 | TYPE_RAISES_EXCEPTIONS (t2)); |
14e29aaa | 871 | cp_ref_qualifier rqual = type_memfn_rqual (t1); |
be94aed9 | 872 | tree t3; |
0fad3000 | 873 | bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1); |
874 | bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2); | |
be94aed9 | 875 | |
876 | /* If this was a member function type, get back to the | |
877 | original type of type member function (i.e., without | |
878 | the class instance variable up front. */ | |
879 | t1 = build_function_type (TREE_TYPE (t1), | |
880 | TREE_CHAIN (TYPE_ARG_TYPES (t1))); | |
881 | t2 = build_function_type (TREE_TYPE (t2), | |
882 | TREE_CHAIN (TYPE_ARG_TYPES (t2))); | |
883 | t3 = merge_types (t1, t2); | |
5bfb0742 | 884 | t3 = build_method_type_directly (basetype, TREE_TYPE (t3), |
885 | TYPE_ARG_TYPES (t3)); | |
be94aed9 | 886 | t1 = build_exception_variant (t3, raises); |
14e29aaa | 887 | t1 = build_ref_qualified_type (t1, rqual); |
0fad3000 | 888 | if (late_return_type_1_p) |
889 | TYPE_HAS_LATE_RETURN_TYPE (t1) = 1; | |
890 | if (late_return_type_2_p) | |
891 | TYPE_HAS_LATE_RETURN_TYPE (t2) = 1; | |
be94aed9 | 892 | break; |
893 | } | |
471086d6 | 894 | |
4a4215bd | 895 | case TYPENAME_TYPE: |
896 | /* There is no need to merge attributes into a TYPENAME_TYPE. | |
897 | When the type is instantiated it will have whatever | |
898 | attributes result from the instantiation. */ | |
899 | return t1; | |
900 | ||
be94aed9 | 901 | default:; |
902 | } | |
b5de1aa4 | 903 | |
904 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) | |
905 | return t1; | |
906 | else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
907 | return t2; | |
908 | else | |
909 | return cp_build_type_attribute_variant (t1, attributes); | |
be94aed9 | 910 | } |
471086d6 | 911 | |
8b25863e | 912 | /* Return the ARRAY_TYPE type without its domain. */ |
913 | ||
914 | tree | |
915 | strip_array_domain (tree type) | |
916 | { | |
917 | tree t2; | |
918 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
919 | if (TYPE_DOMAIN (type) == NULL_TREE) | |
920 | return type; | |
921 | t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); | |
922 | return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type)); | |
923 | } | |
924 | ||
8021532e | 925 | /* Wrapper around cp_common_type that is used by c-common.c and other |
926 | front end optimizations that remove promotions. | |
74002e1d | 927 | |
8021532e | 928 | Return the common type for two arithmetic types T1 and T2 under the |
929 | usual arithmetic conversions. The default conversions have already | |
930 | been applied, and enumerated types converted to their compatible | |
931 | integer types. */ | |
be94aed9 | 932 | |
933 | tree | |
f82b06e0 | 934 | common_type (tree t1, tree t2) |
be94aed9 | 935 | { |
8021532e | 936 | /* If one type is nonsense, use the other */ |
937 | if (t1 == error_mark_node) | |
938 | return t2; | |
939 | if (t2 == error_mark_node) | |
940 | return t1; | |
bb0726a1 | 941 | |
8021532e | 942 | return cp_common_type (t1, t2); |
943 | } | |
471086d6 | 944 | |
8021532e | 945 | /* Return the common type of two pointer types T1 and T2. This is the |
946 | type for the result of most arithmetic operations if the operands | |
947 | have the given two types. | |
948 | ||
949 | We assume that comp_target_types has already been done and returned | |
950 | nonzero; if that isn't so, this may crash. */ | |
471086d6 | 951 | |
8021532e | 952 | tree |
953 | common_pointer_type (tree t1, tree t2) | |
954 | { | |
955 | gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2)) | |
05765a91 | 956 | || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2)) |
8021532e | 957 | || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2))); |
958 | ||
959 | return composite_pointer_type (t1, t2, error_mark_node, error_mark_node, | |
2f182218 | 960 | CPO_CONVERSION, tf_warning_or_error); |
471086d6 | 961 | } |
962 | \f | |
316b7a44 | 963 | /* Compare two exception specifier types for exactness or subsetness, if |
f0dd33b5 | 964 | allowed. Returns false for mismatch, true for match (same, or |
965 | derived and !exact). | |
9031d10b | 966 | |
316b7a44 | 967 | [except.spec] "If a class X ... objects of class X or any class publicly |
755edffd | 968 | and unambiguously derived from X. Similarly, if a pointer type Y * ... |
316b7a44 | 969 | exceptions of type Y * or that are pointers to any type publicly and |
755edffd | 970 | unambiguously derived from Y. Otherwise a function only allows exceptions |
316b7a44 | 971 | that have the same type ..." |
972 | This does not mention cv qualifiers and is different to what throw | |
973 | [except.throw] and catch [except.catch] will do. They will ignore the | |
974 | top level cv qualifiers, and allow qualifiers in the pointer to class | |
975 | example. | |
9031d10b | 976 | |
316b7a44 | 977 | We implement the letter of the standard. */ |
978 | ||
f0dd33b5 | 979 | static bool |
980 | comp_except_types (tree a, tree b, bool exact) | |
316b7a44 | 981 | { |
982 | if (same_type_p (a, b)) | |
f0dd33b5 | 983 | return true; |
316b7a44 | 984 | else if (!exact) |
985 | { | |
3119c950 | 986 | if (cp_type_quals (a) || cp_type_quals (b)) |
653e5405 | 987 | return false; |
9031d10b | 988 | |
c21c015b | 989 | if (TYPE_PTR_P (a) && TYPE_PTR_P (b)) |
653e5405 | 990 | { |
991 | a = TREE_TYPE (a); | |
992 | b = TREE_TYPE (b); | |
993 | if (cp_type_quals (a) || cp_type_quals (b)) | |
994 | return false; | |
995 | } | |
9031d10b | 996 | |
316b7a44 | 997 | if (TREE_CODE (a) != RECORD_TYPE |
653e5405 | 998 | || TREE_CODE (b) != RECORD_TYPE) |
999 | return false; | |
9031d10b | 1000 | |
ae260dcc | 1001 | if (publicly_uniquely_derived_p (a, b)) |
653e5405 | 1002 | return true; |
316b7a44 | 1003 | } |
f0dd33b5 | 1004 | return false; |
316b7a44 | 1005 | } |
1006 | ||
f0dd33b5 | 1007 | /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers. |
3644efa5 | 1008 | If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5). |
2e9e9363 | 1009 | If EXACT is ce_type, the C++17 type compatibility rules apply. |
3644efa5 | 1010 | If EXACT is ce_normal, the compatibility rules in 15.4/3 apply. |
1011 | If EXACT is ce_exact, the specs must be exactly the same. Exception lists | |
1012 | are unordered, but we've already filtered out duplicates. Most lists will | |
1013 | be in order, we should try to make use of that. */ | |
96624a9e | 1014 | |
f0dd33b5 | 1015 | bool |
3644efa5 | 1016 | comp_except_specs (const_tree t1, const_tree t2, int exact) |
471086d6 | 1017 | { |
9f627b1a | 1018 | const_tree probe; |
1019 | const_tree base; | |
316b7a44 | 1020 | int length = 0; |
1021 | ||
1022 | if (t1 == t2) | |
f0dd33b5 | 1023 | return true; |
9031d10b | 1024 | |
6f67f57c | 1025 | /* First handle noexcept. */ |
1026 | if (exact < ce_exact) | |
3644efa5 | 1027 | { |
2e9e9363 | 1028 | if (exact == ce_type |
1029 | && (canonical_eh_spec (CONST_CAST_TREE (t1)) | |
1030 | == canonical_eh_spec (CONST_CAST_TREE (t2)))) | |
1031 | return true; | |
1032 | ||
9f354539 | 1033 | /* noexcept(false) is compatible with no exception-specification, |
2e9e9363 | 1034 | and less strict than any spec. */ |
6f67f57c | 1035 | if (t1 == noexcept_false_spec) |
9f354539 | 1036 | return t2 == NULL_TREE || exact == ce_derived; |
1037 | /* Even a derived noexcept(false) is compatible with no | |
1038 | exception-specification. */ | |
6f67f57c | 1039 | if (t2 == noexcept_false_spec) |
9f354539 | 1040 | return t1 == NULL_TREE; |
6f67f57c | 1041 | |
1042 | /* Otherwise, if we aren't looking for an exact match, noexcept is | |
1043 | equivalent to throw(). */ | |
1044 | if (t1 == noexcept_true_spec) | |
1045 | t1 = empty_except_spec; | |
1046 | if (t2 == noexcept_true_spec) | |
1047 | t2 = empty_except_spec; | |
3644efa5 | 1048 | } |
1049 | ||
6f67f57c | 1050 | /* If any noexcept is left, it is only comparable to itself; |
1051 | either we're looking for an exact match or we're redeclaring a | |
1052 | template with dependent noexcept. */ | |
1053 | if ((t1 && TREE_PURPOSE (t1)) | |
1054 | || (t2 && TREE_PURPOSE (t2))) | |
1055 | return (t1 && t2 | |
1056 | && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))); | |
1057 | ||
653e5405 | 1058 | if (t1 == NULL_TREE) /* T1 is ... */ |
3644efa5 | 1059 | return t2 == NULL_TREE || exact == ce_derived; |
653e5405 | 1060 | if (!TREE_VALUE (t1)) /* t1 is EMPTY */ |
316b7a44 | 1061 | return t2 != NULL_TREE && !TREE_VALUE (t2); |
653e5405 | 1062 | if (t2 == NULL_TREE) /* T2 is ... */ |
f0dd33b5 | 1063 | return false; |
a7eb08ed | 1064 | if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */ |
3644efa5 | 1065 | return exact == ce_derived; |
9031d10b | 1066 | |
316b7a44 | 1067 | /* Neither set is ... or EMPTY, make sure each part of T2 is in T1. |
1068 | Count how many we find, to determine exactness. For exact matching and | |
1069 | ordered T1, T2, this is an O(n) operation, otherwise its worst case is | |
1070 | O(nm). */ | |
1071 | for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2)) | |
1072 | { | |
1073 | for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe)) | |
653e5405 | 1074 | { |
1075 | tree a = TREE_VALUE (probe); | |
1076 | tree b = TREE_VALUE (t2); | |
1077 | ||
1078 | if (comp_except_types (a, b, exact)) | |
1079 | { | |
3644efa5 | 1080 | if (probe == base && exact > ce_derived) |
653e5405 | 1081 | base = TREE_CHAIN (probe); |
1082 | length++; | |
1083 | break; | |
1084 | } | |
1085 | } | |
316b7a44 | 1086 | if (probe == NULL_TREE) |
653e5405 | 1087 | return false; |
316b7a44 | 1088 | } |
3644efa5 | 1089 | return exact == ce_derived || base == NULL_TREE || length == list_length (t1); |
471086d6 | 1090 | } |
1091 | ||
f0dd33b5 | 1092 | /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if |
1093 | [] can match [size]. */ | |
daf9ff67 | 1094 | |
00952d10 | 1095 | static bool |
9f627b1a | 1096 | comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration) |
471086d6 | 1097 | { |
daf9ff67 | 1098 | tree d1; |
1099 | tree d2; | |
b84f4a9f | 1100 | tree max1, max2; |
daf9ff67 | 1101 | |
1102 | if (t1 == t2) | |
f0dd33b5 | 1103 | return true; |
471086d6 | 1104 | |
daf9ff67 | 1105 | /* The type of the array elements must be the same. */ |
f0dd33b5 | 1106 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
00952d10 | 1107 | return false; |
471086d6 | 1108 | |
daf9ff67 | 1109 | d1 = TYPE_DOMAIN (t1); |
1110 | d2 = TYPE_DOMAIN (t2); | |
1111 | ||
1112 | if (d1 == d2) | |
00952d10 | 1113 | return true; |
471086d6 | 1114 | |
daf9ff67 | 1115 | /* If one of the arrays is dimensionless, and the other has a |
6c0cc2cd | 1116 | dimension, they are of different types. However, it is valid to |
daf9ff67 | 1117 | write: |
1118 | ||
1119 | extern int a[]; | |
1120 | int a[3]; | |
1121 | ||
9031d10b | 1122 | by [basic.link]: |
daf9ff67 | 1123 | |
1124 | declarations for an array object can specify | |
1125 | array types that differ by the presence or absence of a major | |
1126 | array bound (_dcl.array_). */ | |
1127 | if (!d1 || !d2) | |
f0dd33b5 | 1128 | return allow_redeclaration; |
daf9ff67 | 1129 | |
1130 | /* Check that the dimensions are the same. */ | |
b84f4a9f | 1131 | |
1132 | if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))) | |
1133 | return false; | |
1134 | max1 = TYPE_MAX_VALUE (d1); | |
1135 | max2 = TYPE_MAX_VALUE (d2); | |
b84f4a9f | 1136 | |
1137 | if (!cp_tree_equal (max1, max2)) | |
1138 | return false; | |
1139 | ||
1140 | return true; | |
471086d6 | 1141 | } |
1142 | ||
8a03d6ff | 1143 | /* Compare the relative position of T1 and T2 into their respective |
1144 | template parameter list. | |
1145 | T1 and T2 must be template parameter types. | |
1146 | Return TRUE if T1 and T2 have the same position, FALSE otherwise. */ | |
1147 | ||
1148 | static bool | |
1149 | comp_template_parms_position (tree t1, tree t2) | |
1150 | { | |
0d432ee0 | 1151 | tree index1, index2; |
8a03d6ff | 1152 | gcc_assert (t1 && t2 |
1153 | && TREE_CODE (t1) == TREE_CODE (t2) | |
1154 | && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM | |
1155 | || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM | |
1156 | || TREE_CODE (t1) == TEMPLATE_TYPE_PARM)); | |
1157 | ||
0d432ee0 | 1158 | index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1)); |
1159 | index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2)); | |
d7f79c88 | 1160 | |
0d432ee0 | 1161 | /* Then compare their relative position. */ |
1162 | if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2) | |
1163 | || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2) | |
1164 | || (TEMPLATE_PARM_PARAMETER_PACK (index1) | |
1165 | != TEMPLATE_PARM_PARAMETER_PACK (index2))) | |
d7f79c88 | 1166 | return false; |
1167 | ||
f8ccb73b | 1168 | /* In C++14 we can end up comparing 'auto' to a normal template |
1169 | parameter. Don't confuse them. */ | |
4e454776 | 1170 | if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2))) |
f8ccb73b | 1171 | return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2); |
1172 | ||
0d432ee0 | 1173 | return true; |
d7f79c88 | 1174 | } |
1175 | ||
a09db423 | 1176 | /* Subroutine in comptypes. */ |
96624a9e | 1177 | |
6753bca0 | 1178 | static bool |
1179 | structural_comptypes (tree t1, tree t2, int strict) | |
471086d6 | 1180 | { |
471086d6 | 1181 | if (t1 == t2) |
00952d10 | 1182 | return true; |
1183 | ||
331bc0ad | 1184 | /* Suppress errors caused by previously reported errors. */ |
8d33f186 | 1185 | if (t1 == error_mark_node || t2 == error_mark_node) |
00952d10 | 1186 | return false; |
9031d10b | 1187 | |
b4df430b | 1188 | gcc_assert (TYPE_P (t1) && TYPE_P (t2)); |
9031d10b | 1189 | |
00952d10 | 1190 | /* TYPENAME_TYPEs should be resolved if the qualifying scope is the |
1191 | current instantiation. */ | |
1192 | if (TREE_CODE (t1) == TYPENAME_TYPE) | |
8826a863 | 1193 | t1 = resolve_typename_type (t1, /*only_current_p=*/true); |
9031d10b | 1194 | |
00952d10 | 1195 | if (TREE_CODE (t2) == TYPENAME_TYPE) |
8826a863 | 1196 | t2 = resolve_typename_type (t2, /*only_current_p=*/true); |
471086d6 | 1197 | |
e857e9c7 | 1198 | if (TYPE_PTRMEMFUNC_P (t1)) |
1199 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); | |
1200 | if (TYPE_PTRMEMFUNC_P (t2)) | |
1201 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); | |
1202 | ||
471086d6 | 1203 | /* Different classes of types can't be compatible. */ |
471086d6 | 1204 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
00952d10 | 1205 | return false; |
471086d6 | 1206 | |
7f233616 | 1207 | /* Qualifiers must match. For array types, we will check when we |
1208 | recur on the array element types. */ | |
1209 | if (TREE_CODE (t1) != ARRAY_TYPE | |
ce494fcf | 1210 | && cp_type_quals (t1) != cp_type_quals (t2)) |
1211 | return false; | |
1212 | if (TREE_CODE (t1) == FUNCTION_TYPE | |
1213 | && type_memfn_quals (t1) != type_memfn_quals (t2)) | |
00952d10 | 1214 | return false; |
e116411c | 1215 | /* Need to check this before TYPE_MAIN_VARIANT. |
1216 | FIXME function qualifiers should really change the main variant. */ | |
2e9e9363 | 1217 | if (TREE_CODE (t1) == FUNCTION_TYPE |
1218 | || TREE_CODE (t1) == METHOD_TYPE) | |
1219 | { | |
1220 | if (type_memfn_rqual (t1) != type_memfn_rqual (t2)) | |
1221 | return false; | |
1222 | if (flag_noexcept_type | |
1223 | && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1), | |
1224 | TYPE_RAISES_EXCEPTIONS (t2), | |
1225 | ce_type)) | |
1226 | return false; | |
1227 | } | |
471086d6 | 1228 | |
1229 | /* Allow for two different type nodes which have essentially the same | |
1230 | definition. Note that we already checked for equality of the type | |
3398e91d | 1231 | qualifiers (just above). */ |
471086d6 | 1232 | |
7f233616 | 1233 | if (TREE_CODE (t1) != ARRAY_TYPE |
1234 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
00952d10 | 1235 | return true; |
471086d6 | 1236 | |
7feec189 | 1237 | |
b7837065 | 1238 | /* Compare the types. Break out if they could be the same. */ |
471086d6 | 1239 | switch (TREE_CODE (t1)) |
1240 | { | |
9a984729 | 1241 | case VOID_TYPE: |
1242 | case BOOLEAN_TYPE: | |
1243 | /* All void and bool types are the same. */ | |
1244 | break; | |
1245 | ||
1246 | case INTEGER_TYPE: | |
1247 | case FIXED_POINT_TYPE: | |
1248 | case REAL_TYPE: | |
1249 | /* With these nodes, we can't determine type equivalence by | |
1250 | looking at what is stored in the nodes themselves, because | |
1251 | two nodes might have different TYPE_MAIN_VARIANTs but still | |
1252 | represent the same type. For example, wchar_t and int could | |
1253 | have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE, | |
1254 | TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs | |
1255 | and are distinct types. On the other hand, int and the | |
1256 | following typedef | |
1257 | ||
1258 | typedef int INT __attribute((may_alias)); | |
1259 | ||
1260 | have identical properties, different TYPE_MAIN_VARIANTs, but | |
1261 | represent the same type. The canonical type system keeps | |
1262 | track of equivalence in this case, so we fall back on it. */ | |
1263 | return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); | |
1264 | ||
4d192fd8 | 1265 | case TEMPLATE_TEMPLATE_PARM: |
1e93ca27 | 1266 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
8a03d6ff | 1267 | if (!comp_template_parms_position (t1, t2)) |
00952d10 | 1268 | return false; |
1269 | if (!comp_template_parms | |
1270 | (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)), | |
1271 | DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2)))) | |
1272 | return false; | |
1e93ca27 | 1273 | if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM) |
b7837065 | 1274 | break; |
4d192fd8 | 1275 | /* Don't check inheritance. */ |
f0dd33b5 | 1276 | strict = COMPARE_STRICT; |
331bc0ad | 1277 | /* Fall through. */ |
4d192fd8 | 1278 | |
471086d6 | 1279 | case RECORD_TYPE: |
1280 | case UNION_TYPE: | |
e4e283ec | 1281 | if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2) |
1282 | && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2) | |
00952d10 | 1283 | || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM) |
1284 | && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2))) | |
b7837065 | 1285 | break; |
9031d10b | 1286 | |
f0dd33b5 | 1287 | if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2)) |
b7837065 | 1288 | break; |
f0dd33b5 | 1289 | else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1)) |
b7837065 | 1290 | break; |
9031d10b | 1291 | |
00952d10 | 1292 | return false; |
471086d6 | 1293 | |
1294 | case OFFSET_TYPE: | |
a09db423 | 1295 | if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2), |
1296 | strict & ~COMPARE_REDECLARATION)) | |
00952d10 | 1297 | return false; |
b7837065 | 1298 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1299 | return false; | |
1300 | break; | |
471086d6 | 1301 | |
471086d6 | 1302 | case REFERENCE_TYPE: |
63949b38 | 1303 | if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2)) |
1304 | return false; | |
1305 | /* fall through to checks for pointer types */ | |
3c77f69c | 1306 | gcc_fallthrough (); |
63949b38 | 1307 | |
1308 | case POINTER_TYPE: | |
b7837065 | 1309 | if (TYPE_MODE (t1) != TYPE_MODE (t2) |
b7837065 | 1310 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1311 | return false; | |
1312 | break; | |
471086d6 | 1313 | |
a7eb08ed | 1314 | case METHOD_TYPE: |
471086d6 | 1315 | case FUNCTION_TYPE: |
f0dd33b5 | 1316 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
00952d10 | 1317 | return false; |
b7837065 | 1318 | if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))) |
1319 | return false; | |
1320 | break; | |
471086d6 | 1321 | |
1322 | case ARRAY_TYPE: | |
6beb3f76 | 1323 | /* Target types must match incl. qualifiers. */ |
b7837065 | 1324 | if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION))) |
1325 | return false; | |
1326 | break; | |
471086d6 | 1327 | |
ac9386a0 | 1328 | case TEMPLATE_TYPE_PARM: |
0d432ee0 | 1329 | /* If T1 and T2 don't have the same relative position in their |
1330 | template parameters set, they can't be equal. */ | |
1331 | if (!comp_template_parms_position (t1, t2)) | |
1332 | return false; | |
433fc17c | 1333 | /* Constrained 'auto's are distinct from parms that don't have the same |
1334 | constraints. */ | |
1335 | if (!equivalent_placeholder_constraints (t1, t2)) | |
1336 | return false; | |
b7837065 | 1337 | break; |
e4ce2dc4 | 1338 | |
1339 | case TYPENAME_TYPE: | |
00952d10 | 1340 | if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1), |
1341 | TYPENAME_TYPE_FULLNAME (t2))) | |
653e5405 | 1342 | return false; |
ec467086 | 1343 | /* Qualifiers don't matter on scopes. */ |
1344 | if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1), | |
1345 | TYPE_CONTEXT (t2))) | |
b7837065 | 1346 | return false; |
1347 | break; | |
d913511e | 1348 | |
47d727d4 | 1349 | case UNBOUND_CLASS_TEMPLATE: |
00952d10 | 1350 | if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2))) |
653e5405 | 1351 | return false; |
b7837065 | 1352 | if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))) |
1353 | return false; | |
1354 | break; | |
47d727d4 | 1355 | |
8c18e707 | 1356 | case COMPLEX_TYPE: |
b7837065 | 1357 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1358 | return false; | |
1359 | break; | |
8c18e707 | 1360 | |
8b4b9810 | 1361 | case VECTOR_TYPE: |
b7837065 | 1362 | if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2) |
1363 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) | |
1364 | return false; | |
8b4b9810 | 1365 | break; |
1366 | ||
d95d815d | 1367 | case TYPE_PACK_EXPANSION: |
2b6ed700 | 1368 | return (same_type_p (PACK_EXPANSION_PATTERN (t1), |
1369 | PACK_EXPANSION_PATTERN (t2)) | |
1370 | && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1), | |
1371 | PACK_EXPANSION_EXTRA_ARGS (t2))); | |
d95d815d | 1372 | |
34da8800 | 1373 | case DECLTYPE_TYPE: |
1374 | if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1) | |
1375 | != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2) | |
a8b75081 | 1376 | || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) |
1377 | != DECLTYPE_FOR_LAMBDA_CAPTURE (t2)) | |
a75d43a4 | 1378 | || (DECLTYPE_FOR_LAMBDA_PROXY (t1) |
1379 | != DECLTYPE_FOR_LAMBDA_PROXY (t2)) | |
34da8800 | 1380 | || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), |
1381 | DECLTYPE_TYPE_EXPR (t2))) | |
1382 | return false; | |
1383 | break; | |
1384 | ||
8de5c43e | 1385 | case UNDERLYING_TYPE: |
1386 | return same_type_p (UNDERLYING_TYPE_TYPE (t1), | |
1387 | UNDERLYING_TYPE_TYPE (t2)); | |
1388 | ||
d913511e | 1389 | default: |
b7837065 | 1390 | return false; |
471086d6 | 1391 | } |
b7837065 | 1392 | |
1393 | /* If we get here, we know that from a target independent POV the | |
1394 | types are the same. Make sure the target attributes are also | |
1395 | the same. */ | |
309303cf | 1396 | return comp_type_attributes (t1, t2); |
471086d6 | 1397 | } |
1398 | ||
6753bca0 | 1399 | /* Return true if T1 and T2 are related as allowed by STRICT. STRICT |
1400 | is a bitwise-or of the COMPARE_* flags. */ | |
1401 | ||
1402 | bool | |
a09db423 | 1403 | comptypes (tree t1, tree t2, int strict) |
6753bca0 | 1404 | { |
1405 | if (strict == COMPARE_STRICT) | |
1406 | { | |
6753bca0 | 1407 | if (t1 == t2) |
1408 | return true; | |
1409 | ||
1410 | if (t1 == error_mark_node || t2 == error_mark_node) | |
1411 | return false; | |
1412 | ||
1413 | if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2)) | |
1414 | /* At least one of the types requires structural equality, so | |
1415 | perform a deep check. */ | |
1416 | return structural_comptypes (t1, t2, strict); | |
1417 | ||
5e8689fb | 1418 | if (flag_checking && USE_CANONICAL_TYPES) |
6753bca0 | 1419 | { |
a1406b2e | 1420 | bool result = structural_comptypes (t1, t2, strict); |
1421 | ||
6753bca0 | 1422 | if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2)) |
a1406b2e | 1423 | /* The two types are structurally equivalent, but their |
1424 | canonical types were different. This is a failure of the | |
1425 | canonical type propagation code.*/ | |
1426 | internal_error | |
8c41abe8 | 1427 | ("canonical types differ for identical types %qT and %qT", |
a1406b2e | 1428 | t1, t2); |
6753bca0 | 1429 | else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)) |
a1406b2e | 1430 | /* Two types are structurally different, but the canonical |
1431 | types are the same. This means we were over-eager in | |
1432 | assigning canonical types. */ | |
1433 | internal_error | |
8c41abe8 | 1434 | ("same canonical type node for different types %qT and %qT", |
a1406b2e | 1435 | t1, t2); |
6753bca0 | 1436 | |
1437 | return result; | |
1438 | } | |
5e8689fb | 1439 | if (!flag_checking && USE_CANONICAL_TYPES) |
6753bca0 | 1440 | return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); |
a1406b2e | 1441 | else |
1442 | return structural_comptypes (t1, t2, strict); | |
6753bca0 | 1443 | } |
1444 | else if (strict == COMPARE_STRUCTURAL) | |
1445 | return structural_comptypes (t1, t2, COMPARE_STRICT); | |
1446 | else | |
1447 | return structural_comptypes (t1, t2, strict); | |
1448 | } | |
1449 | ||
c1e4c34a | 1450 | /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring |
1451 | top-level qualifiers. */ | |
1452 | ||
1453 | bool | |
1454 | same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2) | |
1455 | { | |
1456 | if (type1 == error_mark_node || type2 == error_mark_node) | |
1457 | return false; | |
1458 | ||
547c8796 | 1459 | type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED); |
1460 | type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED); | |
1461 | return same_type_p (type1, type2); | |
c1e4c34a | 1462 | } |
1463 | ||
3e04bd45 | 1464 | /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */ |
1465 | ||
f0dd33b5 | 1466 | bool |
9f627b1a | 1467 | at_least_as_qualified_p (const_tree type1, const_tree type2) |
3e04bd45 | 1468 | { |
f0dd33b5 | 1469 | int q1 = cp_type_quals (type1); |
1470 | int q2 = cp_type_quals (type2); | |
9031d10b | 1471 | |
3e04bd45 | 1472 | /* All qualifiers for TYPE2 must also appear in TYPE1. */ |
f0dd33b5 | 1473 | return (q1 & q2) == q2; |
3e04bd45 | 1474 | } |
1475 | ||
772e70a1 | 1476 | /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is |
1477 | more cv-qualified that TYPE1, and 0 otherwise. */ | |
1478 | ||
1479 | int | |
fdbd4827 | 1480 | comp_cv_qualification (int q1, int q2) |
772e70a1 | 1481 | { |
f0dd33b5 | 1482 | if (q1 == q2) |
772e70a1 | 1483 | return 0; |
1484 | ||
f0dd33b5 | 1485 | if ((q1 & q2) == q2) |
772e70a1 | 1486 | return 1; |
f0dd33b5 | 1487 | else if ((q1 & q2) == q1) |
772e70a1 | 1488 | return -1; |
1489 | ||
1490 | return 0; | |
1491 | } | |
1492 | ||
fdbd4827 | 1493 | int |
1494 | comp_cv_qualification (const_tree type1, const_tree type2) | |
1495 | { | |
1496 | int q1 = cp_type_quals (type1); | |
1497 | int q2 = cp_type_quals (type2); | |
1498 | return comp_cv_qualification (q1, q2); | |
1499 | } | |
1500 | ||
772e70a1 | 1501 | /* Returns 1 if the cv-qualification signature of TYPE1 is a proper |
1502 | subset of the cv-qualification signature of TYPE2, and the types | |
1503 | are similar. Returns -1 if the other way 'round, and 0 otherwise. */ | |
1504 | ||
1505 | int | |
f0dd33b5 | 1506 | comp_cv_qual_signature (tree type1, tree type2) |
772e70a1 | 1507 | { |
1508 | if (comp_ptr_ttypes_real (type2, type1, -1)) | |
1509 | return 1; | |
1510 | else if (comp_ptr_ttypes_real (type1, type2, -1)) | |
1511 | return -1; | |
1512 | else | |
1513 | return 0; | |
1514 | } | |
471086d6 | 1515 | \f |
a09db423 | 1516 | /* Subroutines of `comptypes'. */ |
471086d6 | 1517 | |
f0dd33b5 | 1518 | /* Return true if two parameter type lists PARMS1 and PARMS2 are |
88e449ea | 1519 | equivalent in the sense that functions with those parameter types |
1520 | can have equivalent types. The two lists must be equivalent, | |
f0dd33b5 | 1521 | element by element. */ |
471086d6 | 1522 | |
f0dd33b5 | 1523 | bool |
9f627b1a | 1524 | compparms (const_tree parms1, const_tree parms2) |
471086d6 | 1525 | { |
9f627b1a | 1526 | const_tree t1, t2; |
471086d6 | 1527 | |
1528 | /* An unspecified parmlist matches any specified parmlist | |
1529 | whose argument types don't need default promotions. */ | |
1530 | ||
f0dd33b5 | 1531 | for (t1 = parms1, t2 = parms2; |
1532 | t1 || t2; | |
1533 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) | |
471086d6 | 1534 | { |
471086d6 | 1535 | /* If one parmlist is shorter than the other, |
683c151c | 1536 | they fail to match. */ |
f0dd33b5 | 1537 | if (!t1 || !t2) |
1538 | return false; | |
8d33f186 | 1539 | if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) |
f0dd33b5 | 1540 | return false; |
471086d6 | 1541 | } |
f0dd33b5 | 1542 | return true; |
471086d6 | 1543 | } |
1544 | ||
471086d6 | 1545 | \f |
e47f82ba | 1546 | /* Process a sizeof or alignof expression where the operand is a |
1547 | type. */ | |
1548 | ||
471086d6 | 1549 | tree |
e47f82ba | 1550 | cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain) |
471086d6 | 1551 | { |
d4c4d95c | 1552 | tree value; |
420d15dc | 1553 | bool dependent_p; |
471086d6 | 1554 | |
b4df430b | 1555 | gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR); |
e47f82ba | 1556 | if (type == error_mark_node) |
1557 | return error_mark_node; | |
9031d10b | 1558 | |
ef4534a3 | 1559 | type = non_reference (type); |
44bfd07f | 1560 | if (TREE_CODE (type) == METHOD_TYPE) |
471086d6 | 1561 | { |
8864917d | 1562 | if (complain) |
9205a6cc | 1563 | pedwarn (input_location, OPT_Wpointer_arith, |
8864917d | 1564 | "invalid application of %qs to a member function", |
ca16a224 | 1565 | OVL_OP_INFO (false, op)->name); |
b34e07a1 | 1566 | else |
1567 | return error_mark_node; | |
d4c4d95c | 1568 | value = size_one_node; |
471086d6 | 1569 | } |
471086d6 | 1570 | |
420d15dc | 1571 | dependent_p = dependent_type_p (type); |
1572 | if (!dependent_p) | |
1573 | complete_type (type); | |
1574 | if (dependent_p | |
44bfd07f | 1575 | /* VLA types will have a non-constant size. In the body of an |
1576 | uninstantiated template, we don't need to try to compute the | |
1577 | value, because the sizeof expression is not an integral | |
1578 | constant expression in that case. And, if we do try to | |
1579 | compute the value, we'll likely end up with SAVE_EXPRs, which | |
1580 | the template substitution machinery does not expect to see. */ | |
420d15dc | 1581 | || (processing_template_decl |
1582 | && COMPLETE_TYPE_P (type) | |
1583 | && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)) | |
44bfd07f | 1584 | { |
1585 | value = build_min (op, size_type_node, type); | |
1586 | TREE_READONLY (value) = 1; | |
1587 | return value; | |
1588 | } | |
1589 | ||
e60a6f7b | 1590 | return c_sizeof_or_alignof_type (input_location, complete_type (type), |
a179a7dc | 1591 | op == SIZEOF_EXPR, false, |
44bfd07f | 1592 | complain); |
471086d6 | 1593 | } |
1594 | ||
ebd21de4 | 1595 | /* Return the size of the type, without producing any warnings for |
1596 | types whose size cannot be taken. This routine should be used only | |
1597 | in some other routine that has already produced a diagnostic about | |
1598 | using the size of such a type. */ | |
1599 | tree | |
1600 | cxx_sizeof_nowarn (tree type) | |
1601 | { | |
1602 | if (TREE_CODE (type) == FUNCTION_TYPE | |
c21c015b | 1603 | || VOID_TYPE_P (type) |
ebd21de4 | 1604 | || TREE_CODE (type) == ERROR_MARK) |
1605 | return size_one_node; | |
1606 | else if (!COMPLETE_TYPE_P (type)) | |
1607 | return size_zero_node; | |
1608 | else | |
1609 | return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false); | |
1610 | } | |
1611 | ||
ed1c8ebc | 1612 | /* Process a sizeof expression where the operand is an expression. */ |
e47f82ba | 1613 | |
ed1c8ebc | 1614 | static tree |
ebd21de4 | 1615 | cxx_sizeof_expr (tree e, tsubst_flags_t complain) |
e857e9c7 | 1616 | { |
e47f82ba | 1617 | if (e == error_mark_node) |
1618 | return error_mark_node; | |
9031d10b | 1619 | |
3cc0b4b9 | 1620 | if (processing_template_decl) |
e47f82ba | 1621 | { |
ed1c8ebc | 1622 | e = build_min (SIZEOF_EXPR, size_type_node, e); |
e47f82ba | 1623 | TREE_SIDE_EFFECTS (e) = 0; |
1624 | TREE_READONLY (e) = 1; | |
9031d10b | 1625 | |
e47f82ba | 1626 | return e; |
1627 | } | |
9031d10b | 1628 | |
8b25863e | 1629 | /* To get the size of a static data member declared as an array of |
1630 | unknown bound, we need to instantiate it. */ | |
80a58eb0 | 1631 | if (VAR_P (e) |
8b25863e | 1632 | && VAR_HAD_UNKNOWN_BOUND (e) |
1633 | && DECL_TEMPLATE_INSTANTIATION (e)) | |
1634 | instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false); | |
1635 | ||
87d59e72 | 1636 | if (TREE_CODE (e) == PARM_DECL |
1637 | && DECL_ARRAY_PARAMETER_P (e) | |
1638 | && (complain & tf_warning)) | |
1639 | { | |
1640 | if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function " | |
1641 | "parameter %qE will return size of %qT", e, TREE_TYPE (e))) | |
1642 | inform (DECL_SOURCE_LOCATION (e), "declared here"); | |
1643 | } | |
1644 | ||
fbb73d9b | 1645 | e = mark_type_use (e); |
1646 | ||
357b9645 | 1647 | if (bitfield_p (e)) |
e857e9c7 | 1648 | { |
ebd21de4 | 1649 | if (complain & tf_error) |
1650 | error ("invalid application of %<sizeof%> to a bit-field"); | |
1651 | else | |
1652 | return error_mark_node; | |
e47f82ba | 1653 | e = char_type_node; |
1654 | } | |
1655 | else if (is_overloaded_fn (e)) | |
1656 | { | |
ebd21de4 | 1657 | if (complain & tf_error) |
2b9e3597 | 1658 | permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of " |
561fec9d | 1659 | "function type"); |
ebd21de4 | 1660 | else |
1661 | return error_mark_node; | |
e47f82ba | 1662 | e = char_type_node; |
e857e9c7 | 1663 | } |
42b9ec6a | 1664 | else if (type_unknown_p (e)) |
1665 | { | |
ebd21de4 | 1666 | if (complain & tf_error) |
1667 | cxx_incomplete_type_error (e, TREE_TYPE (e)); | |
1668 | else | |
1669 | return error_mark_node; | |
e47f82ba | 1670 | e = char_type_node; |
42b9ec6a | 1671 | } |
e47f82ba | 1672 | else |
1673 | e = TREE_TYPE (e); | |
9031d10b | 1674 | |
ebd21de4 | 1675 | return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error); |
e857e9c7 | 1676 | } |
9031d10b | 1677 | |
ed1c8ebc | 1678 | /* Implement the __alignof keyword: Return the minimum required |
dc5d8324 | 1679 | alignment of E, measured in bytes. For VAR_DECL's and |
ed1c8ebc | 1680 | FIELD_DECL's return DECL_ALIGN (which can be set from an |
1681 | "aligned" __attribute__ specification). */ | |
1682 | ||
1683 | static tree | |
ebd21de4 | 1684 | cxx_alignof_expr (tree e, tsubst_flags_t complain) |
ed1c8ebc | 1685 | { |
1686 | tree t; | |
074ab442 | 1687 | |
ed1c8ebc | 1688 | if (e == error_mark_node) |
1689 | return error_mark_node; | |
1690 | ||
1691 | if (processing_template_decl) | |
1692 | { | |
1693 | e = build_min (ALIGNOF_EXPR, size_type_node, e); | |
1694 | TREE_SIDE_EFFECTS (e) = 0; | |
1695 | TREE_READONLY (e) = 1; | |
1696 | ||
1697 | return e; | |
1698 | } | |
1699 | ||
fbb73d9b | 1700 | e = mark_type_use (e); |
1701 | ||
80a58eb0 | 1702 | if (VAR_P (e)) |
ed1c8ebc | 1703 | t = size_int (DECL_ALIGN_UNIT (e)); |
357b9645 | 1704 | else if (bitfield_p (e)) |
ed1c8ebc | 1705 | { |
ebd21de4 | 1706 | if (complain & tf_error) |
1707 | error ("invalid application of %<__alignof%> to a bit-field"); | |
1708 | else | |
1709 | return error_mark_node; | |
ed1c8ebc | 1710 | t = size_one_node; |
1711 | } | |
1712 | else if (TREE_CODE (e) == COMPONENT_REF | |
1713 | && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL) | |
1714 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1))); | |
1715 | else if (is_overloaded_fn (e)) | |
1716 | { | |
ebd21de4 | 1717 | if (complain & tf_error) |
2b9e3597 | 1718 | permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of " |
561fec9d | 1719 | "function type"); |
ebd21de4 | 1720 | else |
1721 | return error_mark_node; | |
097b5c8b | 1722 | if (TREE_CODE (e) == FUNCTION_DECL) |
1723 | t = size_int (DECL_ALIGN_UNIT (e)); | |
1724 | else | |
1725 | t = size_one_node; | |
ed1c8ebc | 1726 | } |
1727 | else if (type_unknown_p (e)) | |
1728 | { | |
ebd21de4 | 1729 | if (complain & tf_error) |
1730 | cxx_incomplete_type_error (e, TREE_TYPE (e)); | |
1731 | else | |
1732 | return error_mark_node; | |
ed1c8ebc | 1733 | t = size_one_node; |
1734 | } | |
1735 | else | |
ebd21de4 | 1736 | return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, |
1737 | complain & tf_error); | |
ed1c8ebc | 1738 | |
1739 | return fold_convert (size_type_node, t); | |
1740 | } | |
1741 | ||
dc5d8324 | 1742 | /* Process a sizeof or alignof expression E with code OP where the operand |
1743 | is an expression. */ | |
ed1c8ebc | 1744 | |
1745 | tree | |
ebd21de4 | 1746 | cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain) |
ed1c8ebc | 1747 | { |
1748 | if (op == SIZEOF_EXPR) | |
ebd21de4 | 1749 | return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none); |
ed1c8ebc | 1750 | else |
ebd21de4 | 1751 | return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none); |
ed1c8ebc | 1752 | } |
ffcdbf9c | 1753 | |
1754 | /* Build a representation of an expression 'alignas(E).' Return the | |
1755 | folded integer value of E if it is an integral constant expression | |
1756 | that resolves to a valid alignment. If E depends on a template | |
1757 | parameter, return a syntactic representation tree of kind | |
1758 | ALIGNOF_EXPR. Otherwise, return an error_mark_node if the | |
1759 | expression is ill formed, or NULL_TREE if E is NULL_TREE. */ | |
1760 | ||
1761 | tree | |
1762 | cxx_alignas_expr (tree e) | |
1763 | { | |
1764 | if (e == NULL_TREE || e == error_mark_node | |
1765 | || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e))) | |
1766 | return e; | |
1767 | ||
1768 | if (TYPE_P (e)) | |
1769 | /* [dcl.align]/3: | |
1770 | ||
1771 | When the alignment-specifier is of the form | |
1772 | alignas(type-id ), it shall have the same effect as | |
1ed95933 | 1773 | alignas(alignof(type-id )). */ |
ffcdbf9c | 1774 | |
1775 | return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false); | |
1776 | ||
ffcdbf9c | 1777 | /* If we reach this point, it means the alignas expression if of |
1778 | the form "alignas(assignment-expression)", so we should follow | |
1779 | what is stated by [dcl.align]/2. */ | |
1780 | ||
1ed95933 | 1781 | if (value_dependent_expression_p (e)) |
1782 | /* Leave value-dependent expression alone for now. */ | |
1783 | return e; | |
1784 | ||
21131a05 | 1785 | e = instantiate_non_dependent_expr (e); |
ffcdbf9c | 1786 | e = mark_rvalue_use (e); |
1787 | ||
1788 | /* [dcl.align]/2 says: | |
1789 | ||
1790 | the assignment-expression shall be an integral constant | |
1791 | expression. */ | |
4488d9ee | 1792 | |
1793 | if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e))) | |
1794 | { | |
1795 | error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e)); | |
1796 | return error_mark_node; | |
1797 | } | |
ffcdbf9c | 1798 | |
1ed95933 | 1799 | return cxx_constant_value (e); |
ffcdbf9c | 1800 | } |
1801 | ||
471086d6 | 1802 | \f |
334f6ce1 | 1803 | /* EXPR is being used in a context that is not a function call. |
1804 | Enforce: | |
1805 | ||
9031d10b | 1806 | [expr.ref] |
334f6ce1 | 1807 | |
1808 | The expression can be used only as the left-hand operand of a | |
9031d10b | 1809 | member function call. |
334f6ce1 | 1810 | |
1811 | [expr.mptr.operator] | |
1812 | ||
1813 | If the result of .* or ->* is a function, then that result can be | |
9031d10b | 1814 | used only as the operand for the function call operator (). |
334f6ce1 | 1815 | |
1816 | by issuing an error message if appropriate. Returns true iff EXPR | |
1817 | violates these rules. */ | |
1818 | ||
1819 | bool | |
5c874cd3 | 1820 | invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain) |
334f6ce1 | 1821 | { |
e1079908 | 1822 | if (expr == NULL_TREE) |
1823 | return false; | |
1824 | /* Don't enforce this in MS mode. */ | |
1825 | if (flag_ms_extensions) | |
1826 | return false; | |
1827 | if (is_overloaded_fn (expr) && !really_overloaded_fn (expr)) | |
1828 | expr = get_first_fn (expr); | |
1829 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr)) | |
334f6ce1 | 1830 | { |
ebd21de4 | 1831 | if (complain & tf_error) |
5c874cd3 | 1832 | { |
1833 | if (DECL_P (expr)) | |
1834 | { | |
1835 | error_at (loc, "invalid use of non-static member function %qD", | |
1836 | expr); | |
1837 | inform (DECL_SOURCE_LOCATION (expr), "declared here"); | |
1838 | } | |
1839 | else | |
1840 | error_at (loc, "invalid use of non-static member function of " | |
1841 | "type %qT", TREE_TYPE (expr)); | |
1842 | } | |
334f6ce1 | 1843 | return true; |
1844 | } | |
1845 | return false; | |
1846 | } | |
1847 | ||
c0e47fd4 | 1848 | /* If EXP is a reference to a bitfield, and the type of EXP does not |
1849 | match the declared type of the bitfield, return the declared type | |
1850 | of the bitfield. Otherwise, return NULL_TREE. */ | |
1851 | ||
1852 | tree | |
9f627b1a | 1853 | is_bitfield_expr_with_lowered_type (const_tree exp) |
c0e47fd4 | 1854 | { |
154a4e70 | 1855 | switch (TREE_CODE (exp)) |
c0e47fd4 | 1856 | { |
154a4e70 | 1857 | case COND_EXPR: |
7ae94827 | 1858 | if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1) |
1859 | ? TREE_OPERAND (exp, 1) | |
1860 | : TREE_OPERAND (exp, 0))) | |
c0e47fd4 | 1861 | return NULL_TREE; |
1862 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2)); | |
154a4e70 | 1863 | |
1864 | case COMPOUND_EXPR: | |
1865 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)); | |
1866 | ||
1867 | case MODIFY_EXPR: | |
1868 | case SAVE_EXPR: | |
1869 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); | |
1870 | ||
1871 | case COMPONENT_REF: | |
1872 | { | |
1873 | tree field; | |
1874 | ||
1875 | field = TREE_OPERAND (exp, 1); | |
557902f7 | 1876 | if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field)) |
154a4e70 | 1877 | return NULL_TREE; |
1878 | if (same_type_ignoring_top_level_qualifiers_p | |
1879 | (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field))) | |
1880 | return NULL_TREE; | |
1881 | return DECL_BIT_FIELD_TYPE (field); | |
1882 | } | |
1883 | ||
9c8aeb66 | 1884 | case VAR_DECL: |
1885 | if (DECL_HAS_VALUE_EXPR_P (exp)) | |
1886 | return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR | |
1887 | (CONST_CAST_TREE (exp))); | |
1888 | return NULL_TREE; | |
1889 | ||
40d0b157 | 1890 | CASE_CONVERT: |
f79d1de1 | 1891 | if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0))) |
1892 | == TYPE_MAIN_VARIANT (TREE_TYPE (exp))) | |
1893 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); | |
1894 | /* Fallthrough. */ | |
1895 | ||
154a4e70 | 1896 | default: |
1897 | return NULL_TREE; | |
c0e47fd4 | 1898 | } |
154a4e70 | 1899 | } |
1900 | ||
1901 | /* Like is_bitfield_with_lowered_type, except that if EXP is not a | |
1902 | bitfield with a lowered type, the type of EXP is returned, rather | |
1903 | than NULL_TREE. */ | |
1904 | ||
1905 | tree | |
9f627b1a | 1906 | unlowered_expr_type (const_tree exp) |
154a4e70 | 1907 | { |
1908 | tree type; | |
dbeb1e25 | 1909 | tree etype = TREE_TYPE (exp); |
154a4e70 | 1910 | |
1911 | type = is_bitfield_expr_with_lowered_type (exp); | |
dbeb1e25 | 1912 | if (type) |
1913 | type = cp_build_qualified_type (type, cp_type_quals (etype)); | |
1914 | else | |
1915 | type = etype; | |
154a4e70 | 1916 | |
1917 | return type; | |
c0e47fd4 | 1918 | } |
1919 | ||
a681799d | 1920 | /* Perform the conversions in [expr] that apply when an lvalue appears |
1921 | in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and | |
3af524f9 | 1922 | function-to-pointer conversions. In addition, bitfield references are |
1923 | converted to their declared types. Note that this function does not perform | |
1924 | the lvalue-to-rvalue conversion for class types. If you need that conversion | |
1925 | for class types, then you probably need to use force_rvalue. | |
471086d6 | 1926 | |
a4a591f9 | 1927 | Although the returned value is being used as an rvalue, this |
1928 | function does not wrap the returned expression in a | |
1929 | NON_LVALUE_EXPR; the caller is expected to be mindful of the fact | |
1930 | that the return value is no longer an lvalue. */ | |
471086d6 | 1931 | |
1932 | tree | |
547c6b1f | 1933 | decay_conversion (tree exp, |
1934 | tsubst_flags_t complain, | |
1935 | bool reject_builtin /* = true */) | |
471086d6 | 1936 | { |
cd16867a | 1937 | tree type; |
1938 | enum tree_code code; | |
3df42822 | 1939 | location_t loc = EXPR_LOC_OR_LOC (exp, input_location); |
471086d6 | 1940 | |
69efd650 | 1941 | type = TREE_TYPE (exp); |
3d411d73 | 1942 | if (type == error_mark_node) |
1943 | return error_mark_node; | |
1944 | ||
703eb775 | 1945 | exp = resolve_nondeduced_context (exp, complain); |
b93d9a53 | 1946 | if (type_unknown_p (exp)) |
1947 | { | |
4405c1ad | 1948 | if (complain & tf_error) |
1949 | cxx_incomplete_type_error (exp, TREE_TYPE (exp)); | |
b93d9a53 | 1950 | return error_mark_node; |
1951 | } | |
471086d6 | 1952 | |
00e5e809 | 1953 | code = TREE_CODE (type); |
1954 | ||
2055d27a | 1955 | if (error_operand_p (exp)) |
1956 | return error_mark_node; | |
9031d10b | 1957 | |
d5bcfd11 | 1958 | if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp)) |
ff2370be | 1959 | return nullptr_node; |
1960 | ||
471086d6 | 1961 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
1962 | Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */ | |
471086d6 | 1963 | if (code == VOID_TYPE) |
1964 | { | |
4405c1ad | 1965 | if (complain & tf_error) |
61f69bc9 | 1966 | error_at (loc, "void value not ignored as it ought to be"); |
471086d6 | 1967 | return error_mark_node; |
1968 | } | |
5c874cd3 | 1969 | if (invalid_nonstatic_memfn_p (loc, exp, complain)) |
334f6ce1 | 1970 | return error_mark_node; |
cc4d0855 | 1971 | if (code == FUNCTION_TYPE || is_overloaded_fn (exp)) |
3af524f9 | 1972 | { |
1973 | exp = mark_lvalue_use (exp); | |
1974 | if (reject_builtin && reject_gcc_builtin (exp, loc)) | |
1975 | return error_mark_node; | |
1976 | return cp_build_addr_expr (exp, complain); | |
1977 | } | |
471086d6 | 1978 | if (code == ARRAY_TYPE) |
1979 | { | |
cd16867a | 1980 | tree adr; |
471086d6 | 1981 | tree ptrtype; |
471086d6 | 1982 | |
3af524f9 | 1983 | exp = mark_lvalue_use (exp); |
1984 | ||
86ccc124 | 1985 | if (INDIRECT_REF_P (exp)) |
9031d10b | 1986 | return build_nop (build_pointer_type (TREE_TYPE (type)), |
ee1ab431 | 1987 | TREE_OPERAND (exp, 0)); |
471086d6 | 1988 | |
1989 | if (TREE_CODE (exp) == COMPOUND_EXPR) | |
1990 | { | |
4405c1ad | 1991 | tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain); |
1992 | if (op1 == error_mark_node) | |
1993 | return error_mark_node; | |
831d52a2 | 1994 | return build2 (COMPOUND_EXPR, TREE_TYPE (op1), |
1995 | TREE_OPERAND (exp, 0), op1); | |
471086d6 | 1996 | } |
1997 | ||
049132bc | 1998 | if (!obvalue_p (exp) |
471086d6 | 1999 | && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) |
2000 | { | |
4405c1ad | 2001 | if (complain & tf_error) |
61f69bc9 | 2002 | error_at (loc, "invalid use of non-lvalue array"); |
471086d6 | 2003 | return error_mark_node; |
2004 | } | |
2005 | ||
eb5d9b7b | 2006 | /* Don't let an array compound literal decay to a pointer. It can |
2007 | still be used to initialize an array or bind to a reference. */ | |
2008 | if (TREE_CODE (exp) == TARGET_EXPR) | |
2009 | { | |
2010 | if (complain & tf_error) | |
2011 | error_at (loc, "taking address of temporary array"); | |
2012 | return error_mark_node; | |
2013 | } | |
2014 | ||
ca23ec64 | 2015 | ptrtype = build_pointer_type (TREE_TYPE (type)); |
471086d6 | 2016 | |
80a58eb0 | 2017 | if (VAR_P (exp)) |
471086d6 | 2018 | { |
9b86eec0 | 2019 | if (!cxx_mark_addressable (exp)) |
471086d6 | 2020 | return error_mark_node; |
b4ce9ded | 2021 | adr = build_nop (ptrtype, build_address (exp)); |
471086d6 | 2022 | return adr; |
2023 | } | |
2024 | /* This way is better for a COMPONENT_REF since it can | |
2025 | simplify the offset for a component. */ | |
4405c1ad | 2026 | adr = cp_build_addr_expr (exp, complain); |
c4698a21 | 2027 | return cp_convert (ptrtype, adr, complain); |
471086d6 | 2028 | } |
074ab442 | 2029 | |
3af524f9 | 2030 | /* Otherwise, it's the lvalue-to-rvalue conversion. */ |
2031 | exp = mark_rvalue_use (exp, loc, reject_builtin); | |
2032 | ||
a4a591f9 | 2033 | /* If a bitfield is used in a context where integral promotion |
2034 | applies, then the caller is expected to have used | |
2035 | default_conversion. That function promotes bitfields correctly | |
2036 | before calling this function. At this point, if we have a | |
2037 | bitfield referenced, we may assume that is not subject to | |
2038 | promotion, and that, therefore, the type of the resulting rvalue | |
2039 | is the declared type of the bitfield. */ | |
2040 | exp = convert_bitfield_to_declared_type (exp); | |
d0622bdf | 2041 | |
a4a591f9 | 2042 | /* We do not call rvalue() here because we do not want to wrap EXP |
2043 | in a NON_LVALUE_EXPR. */ | |
2044 | ||
2045 | /* [basic.lval] | |
2046 | ||
2047 | Non-class rvalues always have cv-unqualified types. */ | |
2048 | type = TREE_TYPE (exp); | |
f3943982 | 2049 | if (!CLASS_TYPE_P (type) && cv_qualified_p (type)) |
749ecbf6 | 2050 | exp = build_nop (cv_unqualified (type), exp); |
cc2a360d | 2051 | |
3af524f9 | 2052 | if (!complete_type_or_maybe_complain (type, exp, complain)) |
2053 | return error_mark_node; | |
2054 | ||
d0622bdf | 2055 | return exp; |
2056 | } | |
2057 | ||
08cc44e7 | 2058 | /* Perform preparatory conversions, as part of the "usual arithmetic |
a4a591f9 | 2059 | conversions". In particular, as per [expr]: |
2060 | ||
2061 | Whenever an lvalue expression appears as an operand of an | |
2062 | operator that expects the rvalue for that operand, the | |
2063 | lvalue-to-rvalue, array-to-pointer, or function-to-pointer | |
2064 | standard conversions are applied to convert the expression to an | |
2065 | rvalue. | |
2066 | ||
2067 | In addition, we perform integral promotions here, as those are | |
2068 | applied to both operands to a binary operator before determining | |
2069 | what additional conversions should apply. */ | |
2070 | ||
4405c1ad | 2071 | static tree |
2072 | cp_default_conversion (tree exp, tsubst_flags_t complain) | |
d0622bdf | 2073 | { |
2b3c93a3 | 2074 | /* Check for target-specific promotions. */ |
2075 | tree promoted_type = targetm.promoted_type (TREE_TYPE (exp)); | |
2076 | if (promoted_type) | |
c4698a21 | 2077 | exp = cp_convert (promoted_type, exp, complain); |
a4a591f9 | 2078 | /* Perform the integral promotions first so that bitfield |
2079 | expressions (which may promote to "int", even if the bitfield is | |
074ab442 | 2080 | declared "unsigned") are promoted correctly. */ |
2b3c93a3 | 2081 | else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp))) |
c4698a21 | 2082 | exp = cp_perform_integral_promotions (exp, complain); |
a4a591f9 | 2083 | /* Perform the other conversions. */ |
4405c1ad | 2084 | exp = decay_conversion (exp, complain); |
d0622bdf | 2085 | |
471086d6 | 2086 | return exp; |
2087 | } | |
3a8c5440 | 2088 | |
4405c1ad | 2089 | /* C version. */ |
2090 | ||
2091 | tree | |
2092 | default_conversion (tree exp) | |
2093 | { | |
2094 | return cp_default_conversion (exp, tf_warning_or_error); | |
2095 | } | |
2096 | ||
a681799d | 2097 | /* EXPR is an expression with an integral or enumeration type. |
2098 | Perform the integral promotions in [conv.prom], and return the | |
2099 | converted value. */ | |
2100 | ||
2101 | tree | |
c4698a21 | 2102 | cp_perform_integral_promotions (tree expr, tsubst_flags_t complain) |
a681799d | 2103 | { |
2104 | tree type; | |
2105 | tree promoted_type; | |
2106 | ||
fbb73d9b | 2107 | expr = mark_rvalue_use (expr); |
2108 | ||
661c81f5 | 2109 | /* [conv.prom] |
2110 | ||
2111 | If the bitfield has an enumerated type, it is treated as any | |
2112 | other value of that type for promotion purposes. */ | |
2113 | type = is_bitfield_expr_with_lowered_type (expr); | |
2114 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) | |
2115 | type = TREE_TYPE (expr); | |
b4df430b | 2116 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); |
9fd75d52 | 2117 | /* Scoped enums don't promote. */ |
2118 | if (SCOPED_ENUM_P (type)) | |
2119 | return expr; | |
a681799d | 2120 | promoted_type = type_promotes_to (type); |
2121 | if (type != promoted_type) | |
c4698a21 | 2122 | expr = cp_convert (promoted_type, expr, complain); |
a681799d | 2123 | return expr; |
2124 | } | |
2125 | ||
c4698a21 | 2126 | /* C version. */ |
2127 | ||
2128 | tree | |
2129 | perform_integral_promotions (tree expr) | |
2130 | { | |
2131 | return cp_perform_integral_promotions (expr, tf_warning_or_error); | |
2132 | } | |
2133 | ||
3a10ba35 | 2134 | /* Returns nonzero iff exp is a STRING_CST or the result of applying |
2135 | decay_conversion to one. */ | |
2136 | ||
2137 | int | |
9f627b1a | 2138 | string_conv_p (const_tree totype, const_tree exp, int warn) |
3a10ba35 | 2139 | { |
2140 | tree t; | |
2141 | ||
c21c015b | 2142 | if (!TYPE_PTR_P (totype)) |
3a10ba35 | 2143 | return 0; |
2144 | ||
2145 | t = TREE_TYPE (totype); | |
daf9ff67 | 2146 | if (!same_type_p (t, char_type_node) |
924bbf02 | 2147 | && !same_type_p (t, char16_type_node) |
2148 | && !same_type_p (t, char32_type_node) | |
daf9ff67 | 2149 | && !same_type_p (t, wchar_type_node)) |
3a10ba35 | 2150 | return 0; |
2151 | ||
95b2ac55 | 2152 | if (TREE_CODE (exp) == STRING_CST) |
3a10ba35 | 2153 | { |
924bbf02 | 2154 | /* Make sure that we don't try to convert between char and wide chars. */ |
82904609 | 2155 | if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t)) |
95b2ac55 | 2156 | return 0; |
2157 | } | |
2158 | else | |
2159 | { | |
2160 | /* Is this a string constant which has decayed to 'const char *'? */ | |
ce494fcf | 2161 | t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST)); |
daf9ff67 | 2162 | if (!same_type_p (TREE_TYPE (exp), t)) |
3a10ba35 | 2163 | return 0; |
2164 | STRIP_NOPS (exp); | |
2165 | if (TREE_CODE (exp) != ADDR_EXPR | |
2166 | || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST) | |
2167 | return 0; | |
2168 | } | |
ced7c954 | 2169 | if (warn) |
2aeccf73 | 2170 | { |
2171 | if (cxx_dialect >= cxx11) | |
b4b5f44c | 2172 | pedwarn (input_location, OPT_Wwrite_strings, |
2aeccf73 | 2173 | "ISO C++ forbids converting a string constant to %qT", |
2174 | totype); | |
2175 | else | |
2176 | warning (OPT_Wwrite_strings, | |
2177 | "deprecated conversion from string constant to %qT", | |
2178 | totype); | |
2179 | } | |
3a10ba35 | 2180 | |
2181 | return 1; | |
2182 | } | |
471086d6 | 2183 | |
0dbd1c74 | 2184 | /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we |
2185 | can, for example, use as an lvalue. This code used to be in | |
2186 | unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c' | |
2187 | expressions, where we're dealing with aggregates. But now it's again only | |
2188 | called from unary_complex_lvalue. The case (in particular) that led to | |
2189 | this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd | |
2190 | get it there. */ | |
96624a9e | 2191 | |
471086d6 | 2192 | static tree |
ebd21de4 | 2193 | rationalize_conditional_expr (enum tree_code code, tree t, |
2194 | tsubst_flags_t complain) | |
471086d6 | 2195 | { |
3df42822 | 2196 | location_t loc = EXPR_LOC_OR_LOC (t, input_location); |
2197 | ||
0dbd1c74 | 2198 | /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that |
2199 | the first operand is always the one to be used if both operands | |
2200 | are equal, so we know what conditional expression this used to be. */ | |
2201 | if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR) | |
2202 | { | |
e534436e | 2203 | tree op0 = TREE_OPERAND (t, 0); |
2204 | tree op1 = TREE_OPERAND (t, 1); | |
2205 | ||
79f25e9a | 2206 | /* The following code is incorrect if either operand side-effects. */ |
e534436e | 2207 | gcc_assert (!TREE_SIDE_EFFECTS (op0) |
2208 | && !TREE_SIDE_EFFECTS (op1)); | |
0dbd1c74 | 2209 | return |
3df42822 | 2210 | build_conditional_expr (loc, |
2211 | build_x_binary_op (loc, | |
ef0b0c72 | 2212 | (TREE_CODE (t) == MIN_EXPR |
0dbd1c74 | 2213 | ? LE_EXPR : GE_EXPR), |
e534436e | 2214 | op0, TREE_CODE (op0), |
2215 | op1, TREE_CODE (op1), | |
9951fe5d | 2216 | /*overload=*/NULL, |
ebd21de4 | 2217 | complain), |
b99cc6da | 2218 | cp_build_unary_op (code, op0, false, complain), |
2219 | cp_build_unary_op (code, op1, false, complain), | |
ebd21de4 | 2220 | complain); |
0dbd1c74 | 2221 | } |
2222 | ||
471086d6 | 2223 | return |
3df42822 | 2224 | build_conditional_expr (loc, TREE_OPERAND (t, 0), |
b99cc6da | 2225 | cp_build_unary_op (code, TREE_OPERAND (t, 1), false, |
ebd21de4 | 2226 | complain), |
b99cc6da | 2227 | cp_build_unary_op (code, TREE_OPERAND (t, 2), false, |
ebd21de4 | 2228 | complain), |
2229 | complain); | |
471086d6 | 2230 | } |
2231 | ||
63b1d638 | 2232 | /* Given the TYPE of an anonymous union field inside T, return the |
2233 | FIELD_DECL for the field. If not found return NULL_TREE. Because | |
2234 | anonymous unions can nest, we must also search all anonymous unions | |
2235 | that are directly reachable. */ | |
96624a9e | 2236 | |
962f99f9 | 2237 | tree |
f0dd33b5 | 2238 | lookup_anon_field (tree t, tree type) |
63b1d638 | 2239 | { |
2240 | tree field; | |
2241 | ||
436b5628 | 2242 | t = TYPE_MAIN_VARIANT (t); |
2243 | ||
1767a056 | 2244 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
63b1d638 | 2245 | { |
2246 | if (TREE_STATIC (field)) | |
2247 | continue; | |
23ed74d8 | 2248 | if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) |
63b1d638 | 2249 | continue; |
2250 | ||
2251 | /* If we find it directly, return the field. */ | |
2252 | if (DECL_NAME (field) == NULL_TREE | |
c7e700df | 2253 | && type == TYPE_MAIN_VARIANT (TREE_TYPE (field))) |
63b1d638 | 2254 | { |
2255 | return field; | |
2256 | } | |
2257 | ||
2258 | /* Otherwise, it could be nested, search harder. */ | |
2259 | if (DECL_NAME (field) == NULL_TREE | |
128e1d72 | 2260 | && ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
63b1d638 | 2261 | { |
2262 | tree subfield = lookup_anon_field (TREE_TYPE (field), type); | |
2263 | if (subfield) | |
2264 | return subfield; | |
2265 | } | |
2266 | } | |
2267 | return NULL_TREE; | |
2268 | } | |
2269 | ||
4ac852cb | 2270 | /* Build an expression representing OBJECT.MEMBER. OBJECT is an |
2271 | expression; MEMBER is a DECL or baselink. If ACCESS_PATH is | |
2272 | non-NULL, it indicates the path to the base used to name MEMBER. | |
2273 | If PRESERVE_REFERENCE is true, the expression returned will have | |
2274 | REFERENCE_TYPE if the MEMBER does. Otherwise, the expression | |
9031d10b | 2275 | returned will have the type referred to by the reference. |
4ac852cb | 2276 | |
2277 | This function does not perform access control; that is either done | |
2278 | earlier by the parser when the name of MEMBER is resolved to MEMBER | |
2279 | itself, or later when overload resolution selects one of the | |
2280 | functions indicated by MEMBER. */ | |
96624a9e | 2281 | |
471086d6 | 2282 | tree |
3d27a0fa | 2283 | build_class_member_access_expr (cp_expr object, tree member, |
ebd21de4 | 2284 | tree access_path, bool preserve_reference, |
2285 | tsubst_flags_t complain) | |
471086d6 | 2286 | { |
4ac852cb | 2287 | tree object_type; |
2288 | tree member_scope; | |
2289 | tree result = NULL_TREE; | |
807f85cf | 2290 | tree using_decl = NULL_TREE; |
471086d6 | 2291 | |
e9432e8f | 2292 | if (error_operand_p (object) || error_operand_p (member)) |
ca23ec64 | 2293 | return error_mark_node; |
e857e9c7 | 2294 | |
b4df430b | 2295 | gcc_assert (DECL_P (member) || BASELINK_P (member)); |
471086d6 | 2296 | |
4ac852cb | 2297 | /* [expr.ref] |
e857e9c7 | 2298 | |
4ac852cb | 2299 | The type of the first expression shall be "class object" (of a |
2300 | complete type). */ | |
2301 | object_type = TREE_TYPE (object); | |
9031d10b | 2302 | if (!currently_open_class (object_type) |
a5f2d620 | 2303 | && !complete_type_or_maybe_complain (object_type, object, complain)) |
4ac852cb | 2304 | return error_mark_node; |
2305 | if (!CLASS_TYPE_P (object_type)) | |
d2a15a12 | 2306 | { |
ebd21de4 | 2307 | if (complain & tf_error) |
1317e9b0 | 2308 | { |
2309 | if (POINTER_TYPE_P (object_type) | |
2310 | && CLASS_TYPE_P (TREE_TYPE (object_type))) | |
2311 | error ("request for member %qD in %qE, which is of pointer " | |
2312 | "type %qT (maybe you meant to use %<->%> ?)", | |
3d27a0fa | 2313 | member, object.get_value (), object_type); |
1317e9b0 | 2314 | else |
2315 | error ("request for member %qD in %qE, which is of non-class " | |
3d27a0fa | 2316 | "type %qT", member, object.get_value (), object_type); |
1317e9b0 | 2317 | } |
4ac852cb | 2318 | return error_mark_node; |
d2a15a12 | 2319 | } |
471086d6 | 2320 | |
4ac852cb | 2321 | /* The standard does not seem to actually say that MEMBER must be a |
2322 | member of OBJECT_TYPE. However, that is clearly what is | |
2323 | intended. */ | |
2324 | if (DECL_P (member)) | |
471086d6 | 2325 | { |
4ac852cb | 2326 | member_scope = DECL_CLASS_CONTEXT (member); |
4430f688 | 2327 | if (!mark_used (member, complain) && !(complain & tf_error)) |
2328 | return error_mark_node; | |
4ac852cb | 2329 | if (TREE_DEPRECATED (member)) |
45c4e798 | 2330 | warn_deprecated_use (member, NULL_TREE); |
4ac852cb | 2331 | } |
2332 | else | |
e240f813 | 2333 | member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member)); |
4ac852cb | 2334 | /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will |
2335 | presently be the anonymous union. Go outwards until we find a | |
2336 | type related to OBJECT_TYPE. */ | |
c28ddc97 | 2337 | while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope)) |
4ac852cb | 2338 | && !same_type_ignoring_top_level_qualifiers_p (member_scope, |
2339 | object_type)) | |
2340 | member_scope = TYPE_CONTEXT (member_scope); | |
2341 | if (!member_scope || !DERIVED_FROM_P (member_scope, object_type)) | |
2342 | { | |
ebd21de4 | 2343 | if (complain & tf_error) |
2344 | { | |
2345 | if (TREE_CODE (member) == FIELD_DECL) | |
2346 | error ("invalid use of nonstatic data member %qE", member); | |
2347 | else | |
2348 | error ("%qD is not a member of %qT", member, object_type); | |
2349 | } | |
4ac852cb | 2350 | return error_mark_node; |
471086d6 | 2351 | } |
471086d6 | 2352 | |
db0ec145 | 2353 | /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into |
2354 | `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue | |
a17c2a3a | 2355 | in the front end; only _DECLs and _REFs are lvalues in the back end. */ |
db0ec145 | 2356 | { |
2357 | tree temp = unary_complex_lvalue (ADDR_EXPR, object); | |
2358 | if (temp) | |
0744a0c1 | 2359 | object = cp_build_fold_indirect_ref (temp); |
db0ec145 | 2360 | } |
b3c48b5d | 2361 | |
4ac852cb | 2362 | /* In [expr.ref], there is an explicit list of the valid choices for |
2363 | MEMBER. We check for each of those cases here. */ | |
80a58eb0 | 2364 | if (VAR_P (member)) |
471086d6 | 2365 | { |
4ac852cb | 2366 | /* A static data member. */ |
2367 | result = member; | |
35f2d7a7 | 2368 | mark_exp_read (object); |
4ac852cb | 2369 | /* If OBJECT has side-effects, they are supposed to occur. */ |
2370 | if (TREE_SIDE_EFFECTS (object)) | |
831d52a2 | 2371 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result); |
471086d6 | 2372 | } |
4ac852cb | 2373 | else if (TREE_CODE (member) == FIELD_DECL) |
2374 | { | |
2375 | /* A non-static data member. */ | |
2376 | bool null_object_p; | |
2377 | int type_quals; | |
2378 | tree member_type; | |
471086d6 | 2379 | |
3c6824f5 | 2380 | if (INDIRECT_REF_P (object)) |
2381 | null_object_p = | |
2382 | integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0))); | |
2383 | else | |
2384 | null_object_p = false; | |
471086d6 | 2385 | |
4ac852cb | 2386 | /* Convert OBJECT to the type of MEMBER. */ |
dbdac186 | 2387 | if (!same_type_p (TYPE_MAIN_VARIANT (object_type), |
2388 | TYPE_MAIN_VARIANT (member_scope))) | |
471086d6 | 2389 | { |
4ac852cb | 2390 | tree binfo; |
2391 | base_kind kind; | |
2392 | ||
2393 | binfo = lookup_base (access_path ? access_path : object_type, | |
ae260dcc | 2394 | member_scope, ba_unique, &kind, complain); |
4ac852cb | 2395 | if (binfo == error_mark_node) |
2396 | return error_mark_node; | |
2397 | ||
dbdac186 | 2398 | /* It is invalid to try to get to a virtual base of a |
4ac852cb | 2399 | NULL object. The most common cause is invalid use of |
2400 | offsetof macro. */ | |
2401 | if (null_object_p && kind == bk_via_virtual) | |
2402 | { | |
ebd21de4 | 2403 | if (complain & tf_error) |
2404 | { | |
65dd83dc | 2405 | error ("invalid access to non-static data member %qD in " |
2406 | "virtual base of NULL object", member); | |
ebd21de4 | 2407 | } |
4ac852cb | 2408 | return error_mark_node; |
2409 | } | |
2410 | ||
2411 | /* Convert to the base. */ | |
9031d10b | 2412 | object = build_base_path (PLUS_EXPR, object, binfo, |
1e74225a | 2413 | /*nonnull=*/1, complain); |
4ac852cb | 2414 | /* If we found the base successfully then we should be able |
2415 | to convert to it successfully. */ | |
b4df430b | 2416 | gcc_assert (object != error_mark_node); |
471086d6 | 2417 | } |
292ca827 | 2418 | |
4ac852cb | 2419 | /* If MEMBER is from an anonymous aggregate, we have converted |
2420 | OBJECT so that it refers to the class containing the | |
2421 | anonymous union. Generate a reference to the anonymous union | |
2422 | itself, and recur to find MEMBER. */ | |
e657e37c | 2423 | if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member)) |
0a3b29ad | 2424 | /* When this code is called from build_field_call, the |
2425 | object already has the type of the anonymous union. | |
2426 | That is because the COMPONENT_REF was already | |
2427 | constructed, and was then disassembled before calling | |
2428 | build_field_call. After the function-call code is | |
2429 | cleaned up, this waste can be eliminated. */ | |
9031d10b | 2430 | && (!same_type_ignoring_top_level_qualifiers_p |
0a3b29ad | 2431 | (TREE_TYPE (object), DECL_CONTEXT (member)))) |
4ac852cb | 2432 | { |
2433 | tree anonymous_union; | |
2434 | ||
2435 | anonymous_union = lookup_anon_field (TREE_TYPE (object), | |
2436 | DECL_CONTEXT (member)); | |
2437 | object = build_class_member_access_expr (object, | |
2438 | anonymous_union, | |
2439 | /*access_path=*/NULL_TREE, | |
ebd21de4 | 2440 | preserve_reference, |
2441 | complain); | |
4ac852cb | 2442 | } |
2443 | ||
2444 | /* Compute the type of the field, as described in [expr.ref]. */ | |
2445 | type_quals = TYPE_UNQUALIFIED; | |
2446 | member_type = TREE_TYPE (member); | |
2447 | if (TREE_CODE (member_type) != REFERENCE_TYPE) | |
2448 | { | |
9031d10b | 2449 | type_quals = (cp_type_quals (member_type) |
4ac852cb | 2450 | | cp_type_quals (object_type)); |
9031d10b | 2451 | |
4ac852cb | 2452 | /* A field is const (volatile) if the enclosing object, or the |
2453 | field itself, is const (volatile). But, a mutable field is | |
2454 | not const, even within a const object. */ | |
2455 | if (DECL_MUTABLE_P (member)) | |
2456 | type_quals &= ~TYPE_QUAL_CONST; | |
2457 | member_type = cp_build_qualified_type (member_type, type_quals); | |
2458 | } | |
2459 | ||
32cf7025 | 2460 | result = build3_loc (input_location, COMPONENT_REF, member_type, |
2461 | object, member, NULL_TREE); | |
4ac852cb | 2462 | |
2463 | /* Mark the expression const or volatile, as appropriate. Even | |
2464 | though we've dealt with the type above, we still have to mark the | |
2465 | expression itself. */ | |
2466 | if (type_quals & TYPE_QUAL_CONST) | |
2467 | TREE_READONLY (result) = 1; | |
79581672 | 2468 | if (type_quals & TYPE_QUAL_VOLATILE) |
4ac852cb | 2469 | TREE_THIS_VOLATILE (result) = 1; |
2470 | } | |
2471 | else if (BASELINK_P (member)) | |
63b1d638 | 2472 | { |
4ac852cb | 2473 | /* The member is a (possibly overloaded) member function. */ |
2474 | tree functions; | |
0a3b29ad | 2475 | tree type; |
4ac852cb | 2476 | |
2477 | /* If the MEMBER is exactly one static member function, then we | |
2478 | know the type of the expression. Otherwise, we must wait | |
2479 | until overload resolution has been performed. */ | |
2480 | functions = BASELINK_FUNCTIONS (member); | |
2481 | if (TREE_CODE (functions) == FUNCTION_DECL | |
2482 | && DECL_STATIC_FUNCTION_P (functions)) | |
0a3b29ad | 2483 | type = TREE_TYPE (functions); |
4ac852cb | 2484 | else |
0a3b29ad | 2485 | type = unknown_type_node; |
2486 | /* Note that we do not convert OBJECT to the BASELINK_BINFO | |
2487 | base. That will happen when the function is called. */ | |
831d52a2 | 2488 | result = build3 (COMPONENT_REF, type, object, member, NULL_TREE); |
63b1d638 | 2489 | } |
4ac852cb | 2490 | else if (TREE_CODE (member) == CONST_DECL) |
a17aefa2 | 2491 | { |
4ac852cb | 2492 | /* The member is an enumerator. */ |
2493 | result = member; | |
2494 | /* If OBJECT has side-effects, they are supposed to occur. */ | |
2495 | if (TREE_SIDE_EFFECTS (object)) | |
831d52a2 | 2496 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
2497 | object, result); | |
a17aefa2 | 2498 | } |
807f85cf | 2499 | else if ((using_decl = strip_using_decl (member)) != member) |
2500 | result = build_class_member_access_expr (object, | |
2501 | using_decl, | |
2502 | access_path, preserve_reference, | |
2503 | complain); | |
471086d6 | 2504 | else |
2505 | { | |
ebd21de4 | 2506 | if (complain & tf_error) |
2507 | error ("invalid use of %qD", member); | |
4ac852cb | 2508 | return error_mark_node; |
2509 | } | |
471086d6 | 2510 | |
4ac852cb | 2511 | if (!preserve_reference) |
2512 | /* [expr.ref] | |
9031d10b | 2513 | |
4ac852cb | 2514 | If E2 is declared to have type "reference to T", then ... the |
2515 | type of E1.E2 is T. */ | |
2516 | result = convert_from_reference (result); | |
6025dea3 | 2517 | |
4ac852cb | 2518 | return result; |
2519 | } | |
471086d6 | 2520 | |
c6cc092b | 2521 | /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if |
2522 | SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */ | |
0a3b29ad | 2523 | |
2524 | static tree | |
19b271a9 | 2525 | lookup_destructor (tree object, tree scope, tree dtor_name, |
2526 | tsubst_flags_t complain) | |
0a3b29ad | 2527 | { |
2528 | tree object_type = TREE_TYPE (object); | |
2529 | tree dtor_type = TREE_OPERAND (dtor_name, 0); | |
7ef14399 | 2530 | tree expr; |
0a3b29ad | 2531 | |
fcd59115 | 2532 | /* We've already complained about this destructor. */ |
2533 | if (dtor_type == error_mark_node) | |
2534 | return error_mark_node; | |
2535 | ||
a32f68f5 | 2536 | if (scope && !check_dtor_name (scope, dtor_type)) |
0a3b29ad | 2537 | { |
19b271a9 | 2538 | if (complain & tf_error) |
2539 | error ("qualified type %qT does not match destructor name ~%qT", | |
2540 | scope, dtor_type); | |
0a3b29ad | 2541 | return error_mark_node; |
2542 | } | |
d800169d | 2543 | if (is_auto (dtor_type)) |
2544 | dtor_type = object_type; | |
2545 | else if (identifier_p (dtor_type)) | |
c6cc092b | 2546 | { |
2547 | /* In a template, names we can't find a match for are still accepted | |
2548 | destructor names, and we check them here. */ | |
2549 | if (check_dtor_name (object_type, dtor_type)) | |
2550 | dtor_type = object_type; | |
2551 | else | |
2552 | { | |
19b271a9 | 2553 | if (complain & tf_error) |
2554 | error ("object type %qT does not match destructor name ~%qT", | |
2555 | object_type, dtor_type); | |
c6cc092b | 2556 | return error_mark_node; |
2557 | } | |
2558 | ||
2559 | } | |
2560 | else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type))) | |
0a3b29ad | 2561 | { |
19b271a9 | 2562 | if (complain & tf_error) |
2563 | error ("the type being destroyed is %qT, but the destructor " | |
2564 | "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type); | |
0a3b29ad | 2565 | return error_mark_node; |
2566 | } | |
7ef14399 | 2567 | expr = lookup_member (dtor_type, complete_dtor_identifier, |
2cbaacd9 | 2568 | /*protect=*/1, /*want_type=*/false, |
2569 | tf_warning_or_error); | |
9b46a0dd | 2570 | if (!expr) |
2571 | { | |
2572 | if (complain & tf_error) | |
2573 | cxx_incomplete_type_error (dtor_name, dtor_type); | |
2574 | return error_mark_node; | |
2575 | } | |
7ef14399 | 2576 | expr = (adjust_result_of_qualified_name_lookup |
2577 | (expr, dtor_type, object_type)); | |
47a1eb5e | 2578 | if (scope == NULL_TREE) |
2579 | /* We need to call adjust_result_of_qualified_name_lookup in case the | |
2580 | destructor names a base class, but we unset BASELINK_QUALIFIED_P so | |
2581 | that we still get virtual function binding. */ | |
2582 | BASELINK_QUALIFIED_P (expr) = false; | |
7ef14399 | 2583 | return expr; |
0a3b29ad | 2584 | } |
2585 | ||
fbb01da7 | 2586 | /* An expression of the form "A::template B" has been resolved to |
2587 | DECL. Issue a diagnostic if B is not a template or template | |
2588 | specialization. */ | |
2589 | ||
2590 | void | |
2591 | check_template_keyword (tree decl) | |
2592 | { | |
2593 | /* The standard says: | |
2594 | ||
2595 | [temp.names] | |
2596 | ||
2597 | If a name prefixed by the keyword template is not a member | |
2598 | template, the program is ill-formed. | |
2599 | ||
2600 | DR 228 removed the restriction that the template be a member | |
074ab442 | 2601 | template. |
2602 | ||
ae915c82 | 2603 | DR 96, if accepted would add the further restriction that explicit |
fbb01da7 | 2604 | template arguments must be provided if the template keyword is |
2605 | used, but, as of 2005-10-16, that DR is still in "drafting". If | |
2606 | this DR is accepted, then the semantic checks here can be | |
2607 | simplified, as the entity named must in fact be a template | |
2608 | specialization, rather than, as at present, a set of overloaded | |
2609 | functions containing at least one template function. */ | |
2610 | if (TREE_CODE (decl) != TEMPLATE_DECL | |
2611 | && TREE_CODE (decl) != TEMPLATE_ID_EXPR) | |
2612 | { | |
9ddb20d6 | 2613 | if (VAR_P (decl)) |
2614 | { | |
2615 | if (DECL_USE_TEMPLATE (decl) | |
2616 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) | |
2617 | ; | |
2618 | else | |
2619 | permerror (input_location, "%qD is not a template", decl); | |
2620 | } | |
2621 | else if (!is_overloaded_fn (decl)) | |
2b9e3597 | 2622 | permerror (input_location, "%qD is not a template", decl); |
fbb01da7 | 2623 | else |
2624 | { | |
97a86f58 | 2625 | bool found = false; |
2626 | ||
2627 | for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl)); | |
2628 | !found && iter; ++iter) | |
fbb01da7 | 2629 | { |
97a86f58 | 2630 | tree fn = *iter; |
fbb01da7 | 2631 | if (TREE_CODE (fn) == TEMPLATE_DECL |
97a86f58 | 2632 | || TREE_CODE (fn) == TEMPLATE_ID_EXPR |
2633 | || (TREE_CODE (fn) == FUNCTION_DECL | |
2634 | && DECL_USE_TEMPLATE (fn) | |
2635 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))) | |
2636 | found = true; | |
fbb01da7 | 2637 | } |
97a86f58 | 2638 | if (!found) |
2b9e3597 | 2639 | permerror (input_location, "%qD is not a template", decl); |
fbb01da7 | 2640 | } |
2641 | } | |
2642 | } | |
2643 | ||
8134a948 | 2644 | /* Record that an access failure occurred on BASETYPE_PATH attempting |
2645 | to access FIELD_DECL. */ | |
2646 | ||
2647 | void | |
2648 | access_failure_info::record_access_failure (tree basetype_path, | |
2649 | tree field_decl) | |
2650 | { | |
2651 | m_was_inaccessible = true; | |
2652 | m_basetype_path = basetype_path; | |
2653 | m_field_decl = field_decl; | |
2654 | } | |
2655 | ||
2656 | /* If an access failure was recorded, then attempt to locate an | |
2657 | accessor function for the pertinent field, and if one is | |
2658 | available, add a note and fix-it hint suggesting using it. */ | |
2659 | ||
2660 | void | |
2661 | access_failure_info::maybe_suggest_accessor (bool const_p) const | |
2662 | { | |
2663 | if (!m_was_inaccessible) | |
2664 | return; | |
2665 | ||
2666 | tree accessor | |
2667 | = locate_field_accessor (m_basetype_path, m_field_decl, const_p); | |
2668 | if (!accessor) | |
2669 | return; | |
2670 | ||
2671 | /* The accessor must itself be accessible for it to be a reasonable | |
2672 | suggestion. */ | |
2673 | if (!accessible_p (m_basetype_path, accessor, true)) | |
2674 | return; | |
2675 | ||
2676 | rich_location richloc (line_table, input_location); | |
2677 | pretty_printer pp; | |
2678 | pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor))); | |
2679 | richloc.add_fixit_replace (pp_formatted_text (&pp)); | |
3b6578b3 | 2680 | inform (&richloc, "field %q#D can be accessed via %q#D", |
2681 | m_field_decl, accessor); | |
8134a948 | 2682 | } |
2683 | ||
4ac852cb | 2684 | /* This function is called by the parser to process a class member |
2685 | access expression of the form OBJECT.NAME. NAME is a node used by | |
2686 | the parser to represent a name; it is not yet a DECL. It may, | |
2687 | however, be a BASELINK where the BASELINK_FUNCTIONS is a | |
2688 | TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and | |
2689 | there is no reason to do the lookup twice, so the parser keeps the | |
fbb01da7 | 2690 | BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to |
2691 | be a template via the use of the "A::template B" syntax. */ | |
471086d6 | 2692 | |
4ac852cb | 2693 | tree |
3d27a0fa | 2694 | finish_class_member_access_expr (cp_expr object, tree name, bool template_p, |
ebd21de4 | 2695 | tsubst_flags_t complain) |
4ac852cb | 2696 | { |
13795292 | 2697 | tree expr; |
4ac852cb | 2698 | tree object_type; |
2699 | tree member; | |
2700 | tree access_path = NULL_TREE; | |
13795292 | 2701 | tree orig_object = object; |
2702 | tree orig_name = name; | |
5516f3c3 | 2703 | |
4ac852cb | 2704 | if (object == error_mark_node || name == error_mark_node) |
2705 | return error_mark_node; | |
5516f3c3 | 2706 | |
7a4e126b | 2707 | /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */ |
2708 | if (!objc_is_public (object, name)) | |
2709 | return error_mark_node; | |
2710 | ||
13795292 | 2711 | object_type = TREE_TYPE (object); |
2712 | ||
4ac852cb | 2713 | if (processing_template_decl) |
13795292 | 2714 | { |
2ec3c1d8 | 2715 | if (/* If OBJECT is dependent, so is OBJECT.NAME. */ |
eee80116 | 2716 | type_dependent_object_expression_p (object) |
13795292 | 2717 | /* If NAME is "f<args>", where either 'f' or 'args' is |
2718 | dependent, then the expression is dependent. */ | |
2719 | || (TREE_CODE (name) == TEMPLATE_ID_EXPR | |
2720 | && dependent_template_id_p (TREE_OPERAND (name, 0), | |
2721 | TREE_OPERAND (name, 1))) | |
2722 | /* If NAME is "T::X" where "T" is dependent, then the | |
2723 | expression is dependent. */ | |
2724 | || (TREE_CODE (name) == SCOPE_REF | |
2725 | && TYPE_P (TREE_OPERAND (name, 0)) | |
eee80116 | 2726 | && dependent_scope_p (TREE_OPERAND (name, 0)))) |
2727 | { | |
2728 | dependent: | |
2729 | return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, | |
4e6d68ba | 2730 | orig_object, orig_name, NULL_TREE); |
eee80116 | 2731 | } |
13795292 | 2732 | object = build_non_dependent_expr (object); |
2733 | } | |
86c110ac | 2734 | else if (c_dialect_objc () |
694683bb | 2735 | && identifier_p (name) |
9d9f5bb3 | 2736 | && (expr = objc_maybe_build_component_ref (object, name))) |
86c110ac | 2737 | return expr; |
2738 | ||
4ac852cb | 2739 | /* [expr.ref] |
88da234d | 2740 | |
4ac852cb | 2741 | The type of the first expression shall be "class object" (of a |
2742 | complete type). */ | |
9031d10b | 2743 | if (!currently_open_class (object_type) |
a5f2d620 | 2744 | && !complete_type_or_maybe_complain (object_type, object, complain)) |
4ac852cb | 2745 | return error_mark_node; |
2746 | if (!CLASS_TYPE_P (object_type)) | |
2747 | { | |
ebd21de4 | 2748 | if (complain & tf_error) |
1317e9b0 | 2749 | { |
2750 | if (POINTER_TYPE_P (object_type) | |
2751 | && CLASS_TYPE_P (TREE_TYPE (object_type))) | |
2752 | error ("request for member %qD in %qE, which is of pointer " | |
2753 | "type %qT (maybe you meant to use %<->%> ?)", | |
3d27a0fa | 2754 | name, object.get_value (), object_type); |
1317e9b0 | 2755 | else |
2756 | error ("request for member %qD in %qE, which is of non-class " | |
3d27a0fa | 2757 | "type %qT", name, object.get_value (), object_type); |
1317e9b0 | 2758 | } |
4ac852cb | 2759 | return error_mark_node; |
2760 | } | |
33d31608 | 2761 | |
4ac852cb | 2762 | if (BASELINK_P (name)) |
5d6d17b7 | 2763 | /* A member function that has already been looked up. */ |
2764 | member = name; | |
4ac852cb | 2765 | else |
2766 | { | |
2767 | bool is_template_id = false; | |
2768 | tree template_args = NULL_TREE; | |
340a2497 | 2769 | tree scope = NULL_TREE; |
de9554eb | 2770 | |
340a2497 | 2771 | access_path = object_type; |
33d31608 | 2772 | |
4ac852cb | 2773 | if (TREE_CODE (name) == SCOPE_REF) |
2774 | { | |
f368fabb | 2775 | /* A qualified name. The qualifying class or namespace `S' |
2776 | has already been looked up; it is either a TYPE or a | |
2777 | NAMESPACE_DECL. */ | |
4ac852cb | 2778 | scope = TREE_OPERAND (name, 0); |
2779 | name = TREE_OPERAND (name, 1); | |
4ac852cb | 2780 | |
2781 | /* If SCOPE is a namespace, then the qualified name does not | |
2782 | name a member of OBJECT_TYPE. */ | |
2783 | if (TREE_CODE (scope) == NAMESPACE_DECL) | |
de9554eb | 2784 | { |
ebd21de4 | 2785 | if (complain & tf_error) |
2786 | error ("%<%D::%D%> is not a member of %qT", | |
2787 | scope, name, object_type); | |
de9554eb | 2788 | return error_mark_node; |
2789 | } | |
340a2497 | 2790 | } |
2791 | ||
2792 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
2793 | { | |
2794 | is_template_id = true; | |
2795 | template_args = TREE_OPERAND (name, 1); | |
2796 | name = TREE_OPERAND (name, 0); | |
2797 | ||
6767ca9a | 2798 | if (!identifier_p (name)) |
2799 | name = OVL_NAME (name); | |
340a2497 | 2800 | } |
4ac852cb | 2801 | |
340a2497 | 2802 | if (scope) |
2803 | { | |
942d930c | 2804 | if (TREE_CODE (scope) == ENUMERAL_TYPE) |
2805 | { | |
340a2497 | 2806 | gcc_assert (!is_template_id); |
942d930c | 2807 | /* Looking up a member enumerator (c++/56793). */ |
2808 | if (!TYPE_CLASS_SCOPE_P (scope) | |
2809 | || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type)) | |
2810 | { | |
2811 | if (complain & tf_error) | |
2812 | error ("%<%D::%D%> is not a member of %qT", | |
2813 | scope, name, object_type); | |
2814 | return error_mark_node; | |
2815 | } | |
2816 | tree val = lookup_enumerator (scope, name); | |
3dc48db7 | 2817 | if (!val) |
2818 | { | |
2819 | if (complain & tf_error) | |
2820 | error ("%qD is not a member of %qD", | |
2821 | name, scope); | |
2822 | return error_mark_node; | |
2823 | } | |
2824 | ||
942d930c | 2825 | if (TREE_SIDE_EFFECTS (object)) |
2826 | val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val); | |
2827 | return val; | |
2828 | } | |
2829 | ||
f368fabb | 2830 | gcc_assert (CLASS_TYPE_P (scope)); |
694683bb | 2831 | gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR); |
f368fabb | 2832 | |
a70e3c37 | 2833 | if (constructor_name_p (name, scope)) |
2834 | { | |
2835 | if (complain & tf_error) | |
2836 | error ("cannot call constructor %<%T::%D%> directly", | |
2837 | scope, name); | |
2838 | return error_mark_node; | |
2839 | } | |
2840 | ||
4ac852cb | 2841 | /* Find the base of OBJECT_TYPE corresponding to SCOPE. */ |
ae260dcc | 2842 | access_path = lookup_base (object_type, scope, ba_check, |
2843 | NULL, complain); | |
023e0303 | 2844 | if (access_path == error_mark_node) |
4ac852cb | 2845 | return error_mark_node; |
023e0303 | 2846 | if (!access_path) |
2847 | { | |
5acbc91d | 2848 | if (any_dependent_bases_p (object_type)) |
2849 | goto dependent; | |
ebd21de4 | 2850 | if (complain & tf_error) |
2851 | error ("%qT is not a base of %qT", scope, object_type); | |
023e0303 | 2852 | return error_mark_node; |
2853 | } | |
de9554eb | 2854 | } |
13795292 | 2855 | |
2856 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
eee80116 | 2857 | { |
2858 | if (dependent_type_p (object_type)) | |
2859 | /* The destructor isn't declared yet. */ | |
2860 | goto dependent; | |
2861 | member = lookup_destructor (object, scope, name, complain); | |
2862 | } | |
13795292 | 2863 | else |
63b1d638 | 2864 | { |
13795292 | 2865 | /* Look up the member. */ |
8134a948 | 2866 | access_failure_info afi; |
9031d10b | 2867 | member = lookup_member (access_path, name, /*protect=*/1, |
8134a948 | 2868 | /*want_type=*/false, complain, |
2869 | &afi); | |
2870 | afi.maybe_suggest_accessor (TYPE_READONLY (object_type)); | |
b89c04ef | 2871 | if (member == NULL_TREE) |
2872 | { | |
eee80116 | 2873 | if (dependent_type_p (object_type)) |
2874 | /* Try again at instantiation time. */ | |
2875 | goto dependent; | |
ebd21de4 | 2876 | if (complain & tf_error) |
11d8dd6f | 2877 | { |
2878 | tree guessed_id = lookup_member_fuzzy (access_path, name, | |
2879 | /*want_type=*/false); | |
2880 | if (guessed_id) | |
aa3d1155 | 2881 | { |
2882 | location_t bogus_component_loc = input_location; | |
2a990300 | 2883 | gcc_rich_location rich_loc (bogus_component_loc); |
2884 | rich_loc.add_fixit_misspelled_id (bogus_component_loc, | |
2885 | guessed_id); | |
3b6578b3 | 2886 | error_at (&rich_loc, |
2887 | "%q#T has no member named %qE;" | |
2888 | " did you mean %qE?", | |
2889 | TREE_CODE (access_path) == TREE_BINFO | |
2890 | ? TREE_TYPE (access_path) : object_type, | |
2891 | name, guessed_id); | |
aa3d1155 | 2892 | } |
11d8dd6f | 2893 | else |
2894 | error ("%q#T has no member named %qE", | |
2895 | TREE_CODE (access_path) == TREE_BINFO | |
2896 | ? TREE_TYPE (access_path) : object_type, name); | |
2897 | } | |
b89c04ef | 2898 | return error_mark_node; |
2899 | } | |
13795292 | 2900 | if (member == error_mark_node) |
4ac852cb | 2901 | return error_mark_node; |
80488e20 | 2902 | if (DECL_P (member) |
2903 | && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member))) | |
2904 | /* Dependent type attributes on the decl mean that the TREE_TYPE is | |
2905 | wrong, so don't use it. */ | |
2906 | goto dependent; | |
eee80116 | 2907 | if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member)) |
2908 | goto dependent; | |
4ac852cb | 2909 | } |
9031d10b | 2910 | |
4ac852cb | 2911 | if (is_template_id) |
2912 | { | |
607a5d68 | 2913 | tree templ = member; |
9031d10b | 2914 | |
607a5d68 | 2915 | if (BASELINK_P (templ)) |
4e2cb1d9 | 2916 | member = lookup_template_function (templ, template_args); |
2917 | else if (variable_template_p (templ)) | |
2918 | member = (lookup_and_finish_template_variable | |
2919 | (templ, template_args, complain)); | |
4ac852cb | 2920 | else |
2921 | { | |
ebd21de4 | 2922 | if (complain & tf_error) |
2923 | error ("%qD is not a member template function", name); | |
4ac852cb | 2924 | return error_mark_node; |
2925 | } | |
63b1d638 | 2926 | } |
2927 | } | |
2928 | ||
0a3b29ad | 2929 | if (TREE_DEPRECATED (member)) |
45c4e798 | 2930 | warn_deprecated_use (member, NULL_TREE); |
0a3b29ad | 2931 | |
fbb01da7 | 2932 | if (template_p) |
2933 | check_template_keyword (member); | |
2934 | ||
13795292 | 2935 | expr = build_class_member_access_expr (object, member, access_path, |
ebd21de4 | 2936 | /*preserve_reference=*/false, |
2937 | complain); | |
13795292 | 2938 | if (processing_template_decl && expr != error_mark_node) |
7276a1d0 | 2939 | { |
2940 | if (BASELINK_P (member)) | |
2941 | { | |
2942 | if (TREE_CODE (orig_name) == SCOPE_REF) | |
2943 | BASELINK_QUALIFIED_P (member) = 1; | |
2944 | orig_name = member; | |
2945 | } | |
2946 | return build_min_non_dep (COMPONENT_REF, expr, | |
2947 | orig_object, orig_name, | |
2948 | NULL_TREE); | |
2949 | } | |
074ab442 | 2950 | |
13795292 | 2951 | return expr; |
471086d6 | 2952 | } |
3cc0b4b9 | 2953 | |
b749017f | 2954 | /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate |
2955 | type. */ | |
2956 | ||
2957 | tree | |
2958 | build_simple_component_ref (tree object, tree member) | |
2959 | { | |
2960 | tree type = cp_build_qualified_type (TREE_TYPE (member), | |
2961 | cp_type_quals (TREE_TYPE (object))); | |
d2c63826 | 2962 | return build3_loc (input_location, |
2963 | COMPONENT_REF, type, | |
2964 | object, member, NULL_TREE); | |
b749017f | 2965 | } |
2966 | ||
4ac852cb | 2967 | /* Return an expression for the MEMBER_NAME field in the internal |
2968 | representation of PTRMEM, a pointer-to-member function. (Each | |
2969 | pointer-to-member function type gets its own RECORD_TYPE so it is | |
2970 | more convenient to access the fields by name than by FIELD_DECL.) | |
2971 | This routine converts the NAME to a FIELD_DECL and then creates the | |
2972 | node for the complete expression. */ | |
3cc0b4b9 | 2973 | |
2974 | tree | |
4ac852cb | 2975 | build_ptrmemfunc_access_expr (tree ptrmem, tree member_name) |
3cc0b4b9 | 2976 | { |
4ac852cb | 2977 | tree ptrmem_type; |
2978 | tree member; | |
4ac852cb | 2979 | |
2980 | /* This code is a stripped down version of | |
2981 | build_class_member_access_expr. It does not work to use that | |
2982 | routine directly because it expects the object to be of class | |
2983 | type. */ | |
2984 | ptrmem_type = TREE_TYPE (ptrmem); | |
b4df430b | 2985 | gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type)); |
d93128cb | 2986 | for (member = TYPE_FIELDS (ptrmem_type); member; |
2987 | member = DECL_CHAIN (member)) | |
2988 | if (DECL_NAME (member) == member_name) | |
2989 | break; | |
3ce5daa2 | 2990 | tree res = build_simple_component_ref (ptrmem, member); |
2991 | ||
2992 | TREE_NO_WARNING (res) = 1; | |
2993 | return res; | |
3cc0b4b9 | 2994 | } |
4ac852cb | 2995 | |
471086d6 | 2996 | /* Given an expression PTR for a pointer, return an expression |
2997 | for the value pointed to. | |
2998 | ERRORSTRING is the name of the operator to appear in error messages. | |
2999 | ||
3000 | This function may need to overload OPERATOR_FNNAME. | |
3001 | Must also handle REFERENCE_TYPEs for C++. */ | |
3002 | ||
3003 | tree | |
ef0b0c72 | 3004 | build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring, |
ebd21de4 | 3005 | tsubst_flags_t complain) |
471086d6 | 3006 | { |
13795292 | 3007 | tree orig_expr = expr; |
e857e9c7 | 3008 | tree rval; |
d175f019 | 3009 | tree overload = NULL_TREE; |
e857e9c7 | 3010 | |
3cc0b4b9 | 3011 | if (processing_template_decl) |
13795292 | 3012 | { |
cb542a2d | 3013 | /* Retain the type if we know the operand is a pointer. */ |
6b8740d8 | 3014 | if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr))) |
3015 | return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr); | |
13795292 | 3016 | if (type_dependent_expression_p (expr)) |
255b5d15 | 3017 | return build_min_nt_loc (loc, INDIRECT_REF, expr); |
13795292 | 3018 | expr = build_non_dependent_expr (expr); |
3019 | } | |
e857e9c7 | 3020 | |
ef0b0c72 | 3021 | rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr, |
d175f019 | 3022 | NULL_TREE, NULL_TREE, &overload, complain); |
13795292 | 3023 | if (!rval) |
ebd21de4 | 3024 | rval = cp_build_indirect_ref (expr, errorstring, complain); |
13795292 | 3025 | |
3026 | if (processing_template_decl && rval != error_mark_node) | |
d175f019 | 3027 | { |
3028 | if (overload != NULL_TREE) | |
3029 | return (build_min_non_dep_op_overload | |
3030 | (INDIRECT_REF, rval, overload, orig_expr)); | |
3031 | ||
3032 | return build_min_non_dep (INDIRECT_REF, rval, orig_expr); | |
3033 | } | |
13795292 | 3034 | else |
471086d6 | 3035 | return rval; |
471086d6 | 3036 | } |
3037 | ||
0744a0c1 | 3038 | /* The implementation of the above, and of indirection implied by other |
3039 | constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */ | |
ebd21de4 | 3040 | |
0744a0c1 | 3041 | static tree |
3042 | cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring, | |
3043 | tsubst_flags_t complain, bool do_fold) | |
471086d6 | 3044 | { |
cd16867a | 3045 | tree pointer, type; |
f9670f72 | 3046 | |
0744a0c1 | 3047 | /* RO_NULL should only be used with the folding entry points below, not |
3048 | cp_build_indirect_ref. */ | |
3049 | gcc_checking_assert (errorstring != RO_NULL || do_fold); | |
3050 | ||
8d8b140c | 3051 | if (ptr == current_class_ptr |
3052 | || (TREE_CODE (ptr) == NOP_EXPR | |
3053 | && TREE_OPERAND (ptr, 0) == current_class_ptr | |
3054 | && (same_type_ignoring_top_level_qualifiers_p | |
3055 | (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr))))) | |
3e8b52bc | 3056 | return current_class_ref; |
3057 | ||
f9670f72 | 3058 | pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE |
4405c1ad | 3059 | ? ptr : decay_conversion (ptr, complain)); |
3060 | if (pointer == error_mark_node) | |
3061 | return error_mark_node; | |
3062 | ||
f9670f72 | 3063 | type = TREE_TYPE (pointer); |
471086d6 | 3064 | |
729f89ff | 3065 | if (POINTER_TYPE_P (type)) |
471086d6 | 3066 | { |
ca23ec64 | 3067 | /* [expr.unary.op] |
9031d10b | 3068 | |
ca23ec64 | 3069 | If the type of the expression is "pointer to T," the type |
2fb61329 | 3070 | of the result is "T." */ |
3071 | tree t = TREE_TYPE (type); | |
ca23ec64 | 3072 | |
80de168d | 3073 | if ((CONVERT_EXPR_P (ptr) |
3074 | || TREE_CODE (ptr) == VIEW_CONVERT_EXPR) | |
3075 | && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t))) | |
e6fa0ea6 | 3076 | { |
3077 | /* If a warning is issued, mark it to avoid duplicates from | |
3078 | the backend. This only needs to be done at | |
3079 | warn_strict_aliasing > 2. */ | |
3080 | if (warn_strict_aliasing > 2) | |
3081 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)), | |
3082 | type, TREE_OPERAND (ptr, 0))) | |
3083 | TREE_NO_WARNING (ptr) = 1; | |
3084 | } | |
3085 | ||
e3cfe3ce | 3086 | if (VOID_TYPE_P (t)) |
653e5405 | 3087 | { |
3088 | /* A pointer to incomplete type (other than cv void) can be | |
3089 | dereferenced [expr.unary.op]/1 */ | |
ebd21de4 | 3090 | if (complain & tf_error) |
3091 | error ("%qT is not a pointer-to-object type", type); | |
653e5405 | 3092 | return error_mark_node; |
3093 | } | |
0744a0c1 | 3094 | else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR |
0441885c | 3095 | && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0)))) |
ca23ec64 | 3096 | /* The POINTER was something like `&x'. We simplify `*&x' to |
3097 | `x'. */ | |
471086d6 | 3098 | return TREE_OPERAND (pointer, 0); |
3099 | else | |
3100 | { | |
ca23ec64 | 3101 | tree ref = build1 (INDIRECT_REF, t, pointer); |
471086d6 | 3102 | |
c517e9be | 3103 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, |
3104 | so that we get the proper error message if the result is used | |
3105 | to assign to. Also, &* is supposed to be a no-op. */ | |
3e04bd45 | 3106 | TREE_READONLY (ref) = CP_TYPE_CONST_P (t); |
3107 | TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t); | |
471086d6 | 3108 | TREE_SIDE_EFFECTS (ref) |
c1771f2c | 3109 | = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)); |
471086d6 | 3110 | return ref; |
3111 | } | |
3112 | } | |
ebd21de4 | 3113 | else if (!(complain & tf_error)) |
3114 | /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */ | |
3115 | ; | |
471086d6 | 3116 | /* `pointer' won't be an error_mark_node if we were given a |
3117 | pointer to member, so it's cool to check for this here. */ | |
05765a91 | 3118 | else if (TYPE_PTRMEM_P (type)) |
f08923b3 | 3119 | switch (errorstring) |
3120 | { | |
3121 | case RO_ARRAY_INDEXING: | |
3122 | error ("invalid use of array indexing on pointer to member"); | |
3123 | break; | |
3124 | case RO_UNARY_STAR: | |
3125 | error ("invalid use of unary %<*%> on pointer to member"); | |
3126 | break; | |
3127 | case RO_IMPLICIT_CONVERSION: | |
3128 | error ("invalid use of implicit conversion on pointer to member"); | |
3129 | break; | |
7354ad2e | 3130 | case RO_ARROW_STAR: |
3131 | error ("left hand operand of %<->*%> must be a pointer to class, " | |
3132 | "but is a pointer to member of type %qT", type); | |
3133 | break; | |
f08923b3 | 3134 | default: |
3135 | gcc_unreachable (); | |
3136 | } | |
471086d6 | 3137 | else if (pointer != error_mark_node) |
b1bbc8e5 | 3138 | invalid_indirection_error (input_location, type, errorstring); |
3139 | ||
471086d6 | 3140 | return error_mark_node; |
3141 | } | |
3142 | ||
0744a0c1 | 3143 | /* Entry point used by c-common, which expects folding. */ |
3144 | ||
3145 | tree | |
3146 | build_indirect_ref (location_t /*loc*/, | |
3147 | tree ptr, ref_operator errorstring) | |
3148 | { | |
3149 | return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true); | |
3150 | } | |
3151 | ||
3152 | /* Entry point used by internal indirection needs that don't correspond to any | |
3153 | syntactic construct. */ | |
3154 | ||
3155 | tree | |
3156 | cp_build_fold_indirect_ref (tree pointer) | |
3157 | { | |
3158 | return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true); | |
3159 | } | |
3160 | ||
3161 | /* Entry point used by indirection needs that correspond to some syntactic | |
3162 | construct. */ | |
3163 | ||
3164 | tree | |
3165 | cp_build_indirect_ref (tree ptr, ref_operator errorstring, | |
3166 | tsubst_flags_t complain) | |
3167 | { | |
3168 | return cp_build_indirect_ref_1 (ptr, errorstring, complain, false); | |
3169 | } | |
3170 | ||
471086d6 | 3171 | /* This handles expressions of the form "a[i]", which denotes |
3172 | an array reference. | |
3173 | ||
3174 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
3175 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
3176 | This avoids forcing the array out of registers, and can work on | |
3177 | arrays that are not lvalues (for example, members of structures returned | |
3178 | by functions). | |
3179 | ||
3180 | If INDEX is of some user-defined type, it must be converted to | |
3181 | integer type. Otherwise, to make a compatible PLUS_EXPR, it | |
3bc1df45 | 3182 | will inherit the type of the array, which will be some pointer type. |
3183 | ||
3184 | LOC is the location to use in building the array reference. */ | |
471086d6 | 3185 | |
471086d6 | 3186 | tree |
1d291991 | 3187 | cp_build_array_ref (location_t loc, tree array, tree idx, |
3188 | tsubst_flags_t complain) | |
471086d6 | 3189 | { |
3bc1df45 | 3190 | tree ret; |
3191 | ||
471086d6 | 3192 | if (idx == 0) |
3193 | { | |
b4818603 | 3194 | if (complain & tf_error) |
3195 | error_at (loc, "subscript missing in array reference"); | |
471086d6 | 3196 | return error_mark_node; |
3197 | } | |
3198 | ||
3199 | if (TREE_TYPE (array) == error_mark_node | |
3200 | || TREE_TYPE (idx) == error_mark_node) | |
3201 | return error_mark_node; | |
3202 | ||
15f780bd | 3203 | /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference |
3204 | inside it. */ | |
3205 | switch (TREE_CODE (array)) | |
3206 | { | |
3207 | case COMPOUND_EXPR: | |
3208 | { | |
1d291991 | 3209 | tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx, |
3210 | complain); | |
3bc1df45 | 3211 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (value), |
3212 | TREE_OPERAND (array, 0), value); | |
3213 | SET_EXPR_LOCATION (ret, loc); | |
3214 | return ret; | |
15f780bd | 3215 | } |
3216 | ||
3217 | case COND_EXPR: | |
3bc1df45 | 3218 | ret = build_conditional_expr |
1273a0b4 | 3219 | (loc, TREE_OPERAND (array, 0), |
1d291991 | 3220 | cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx, |
3221 | complain), | |
3222 | cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx, | |
3223 | complain), | |
34f12878 | 3224 | complain); |
64a513fb | 3225 | protected_set_expr_location (ret, loc); |
3bc1df45 | 3226 | return ret; |
15f780bd | 3227 | |
3228 | default: | |
3229 | break; | |
3230 | } | |
3231 | ||
7345b977 | 3232 | bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx); |
7059d45d | 3233 | |
4ee9c684 | 3234 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) |
471086d6 | 3235 | { |
8e31b4b2 | 3236 | tree rval, type; |
471086d6 | 3237 | |
92b63884 | 3238 | warn_array_subscript_with_type_char (loc, idx); |
471086d6 | 3239 | |
3f00a6c0 | 3240 | if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx))) |
471086d6 | 3241 | { |
b4818603 | 3242 | if (complain & tf_error) |
3243 | error_at (loc, "array subscript is not an integer"); | |
471086d6 | 3244 | return error_mark_node; |
3245 | } | |
3246 | ||
a681799d | 3247 | /* Apply integral promotions *after* noticing character types. |
3248 | (It is unclear why we do these promotions -- the standard | |
9b67edb7 | 3249 | does not say that we should. In fact, the natural thing would |
a681799d | 3250 | seem to be to convert IDX to ptrdiff_t; we're performing |
3251 | pointer arithmetic.) */ | |
c4698a21 | 3252 | idx = cp_perform_integral_promotions (idx, complain); |
a681799d | 3253 | |
471086d6 | 3254 | /* An array that is indexed by a non-constant |
3255 | cannot be stored in a register; we must be able to do | |
3256 | address arithmetic on its address. | |
3257 | Likewise an array of elements of variable size. */ | |
3258 | if (TREE_CODE (idx) != INTEGER_CST | |
4b72716d | 3259 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
985e9ee0 | 3260 | && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) |
3261 | != INTEGER_CST))) | |
471086d6 | 3262 | { |
3385a573 | 3263 | if (!cxx_mark_addressable (array, true)) |
471086d6 | 3264 | return error_mark_node; |
3265 | } | |
15f780bd | 3266 | |
471086d6 | 3267 | /* An array that is indexed by a constant value which is not within |
3268 | the array bounds cannot be stored in a register either; because we | |
3269 | would get a crash in store_bit_field/extract_bit_field when trying | |
3270 | to access a non-existent part of the register. */ | |
3271 | if (TREE_CODE (idx) == INTEGER_CST | |
82bb2115 | 3272 | && TYPE_DOMAIN (TREE_TYPE (array)) |
3273 | && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array)))) | |
471086d6 | 3274 | { |
9b86eec0 | 3275 | if (!cxx_mark_addressable (array)) |
471086d6 | 3276 | return error_mark_node; |
3277 | } | |
3278 | ||
1e66592c | 3279 | /* Note in C++ it is valid to subscript a `register' array, since |
3280 | it is valid to take the address of something with that | |
3281 | storage specification. */ | |
3282 | if (extra_warnings) | |
471086d6 | 3283 | { |
3284 | tree foo = array; | |
3285 | while (TREE_CODE (foo) == COMPONENT_REF) | |
3286 | foo = TREE_OPERAND (foo, 0); | |
80a58eb0 | 3287 | if (VAR_P (foo) && DECL_REGISTER (foo) |
b4818603 | 3288 | && (complain & tf_warning)) |
3bc1df45 | 3289 | warning_at (loc, OPT_Wextra, |
3290 | "subscripting array declared %<register%>"); | |
471086d6 | 3291 | } |
3292 | ||
ca23ec64 | 3293 | type = TREE_TYPE (TREE_TYPE (array)); |
831d52a2 | 3294 | rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE); |
471086d6 | 3295 | /* Array ref is const/volatile if the array elements are |
3296 | or if the array is.. */ | |
3297 | TREE_READONLY (rval) | |
3e04bd45 | 3298 | |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array)); |
471086d6 | 3299 | TREE_SIDE_EFFECTS (rval) |
3e04bd45 | 3300 | |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array)); |
471086d6 | 3301 | TREE_THIS_VOLATILE (rval) |
3e04bd45 | 3302 | |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array)); |
d2c63826 | 3303 | ret = require_complete_type_sfinae (rval, complain); |
64a513fb | 3304 | protected_set_expr_location (ret, loc); |
c61ef207 | 3305 | if (non_lvalue) |
3306 | ret = non_lvalue_loc (loc, ret); | |
3bc1df45 | 3307 | return ret; |
471086d6 | 3308 | } |
3309 | ||
3310 | { | |
4405c1ad | 3311 | tree ar = cp_default_conversion (array, complain); |
3312 | tree ind = cp_default_conversion (idx, complain); | |
471086d6 | 3313 | |
3314 | /* Put the integer in IND to simplify error checking. */ | |
3315 | if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) | |
a4f59596 | 3316 | std::swap (ar, ind); |
471086d6 | 3317 | |
4405c1ad | 3318 | if (ar == error_mark_node || ind == error_mark_node) |
3319 | return error_mark_node; | |
471086d6 | 3320 | |
c21c015b | 3321 | if (!TYPE_PTR_P (TREE_TYPE (ar))) |
471086d6 | 3322 | { |
b4818603 | 3323 | if (complain & tf_error) |
3324 | error_at (loc, "subscripted value is neither array nor pointer"); | |
471086d6 | 3325 | return error_mark_node; |
3326 | } | |
3327 | if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) | |
3328 | { | |
b4818603 | 3329 | if (complain & tf_error) |
3330 | error_at (loc, "array subscript is not an integer"); | |
471086d6 | 3331 | return error_mark_node; |
3332 | } | |
3333 | ||
92b63884 | 3334 | warn_array_subscript_with_type_char (loc, idx); |
ab51d490 | 3335 | |
8e70fb09 | 3336 | ret = cp_build_indirect_ref (cp_build_binary_op (input_location, |
3337 | PLUS_EXPR, ar, ind, | |
b4818603 | 3338 | complain), |
f08923b3 | 3339 | RO_ARRAY_INDEXING, |
b4818603 | 3340 | complain); |
3bc1df45 | 3341 | protected_set_expr_location (ret, loc); |
c61ef207 | 3342 | if (non_lvalue) |
3343 | ret = non_lvalue_loc (loc, ret); | |
3bc1df45 | 3344 | return ret; |
471086d6 | 3345 | } |
3346 | } | |
1d291991 | 3347 | |
3348 | /* Entry point for Obj-C++. */ | |
3349 | ||
3350 | tree | |
3351 | build_array_ref (location_t loc, tree array, tree idx) | |
3352 | { | |
3353 | return cp_build_array_ref (loc, array, idx, tf_warning_or_error); | |
3354 | } | |
471086d6 | 3355 | \f |
471086d6 | 3356 | /* Resolve a pointer to member function. INSTANCE is the object |
311e42bc | 3357 | instance to use, if the member points to a virtual member. |
3358 | ||
3359 | This used to avoid checking for virtual functions if basetype | |
3360 | has no virtual functions, according to an earlier ANSI draft. | |
3361 | With the final ISO C++ rules, such an optimization is | |
3362 | incorrect: A pointer to a derived member can be static_cast | |
3363 | to pointer-to-base-member, as long as the dynamic object | |
70791196 | 3364 | later has the right member. So now we only do this optimization |
3365 | when we know the dynamic type of the object. */ | |
471086d6 | 3366 | |
3367 | tree | |
4405c1ad | 3368 | get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function, |
3369 | tsubst_flags_t complain) | |
471086d6 | 3370 | { |
3371 | if (TREE_CODE (function) == OFFSET_REF) | |
4a2680fc | 3372 | function = TREE_OPERAND (function, 1); |
471086d6 | 3373 | |
3374 | if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function))) | |
3375 | { | |
70791196 | 3376 | tree idx, delta, e1, e2, e3, vtbl; |
3377 | bool nonvirtual; | |
311e42bc | 3378 | tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)); |
70791196 | 3379 | tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)); |
ce28ee2e | 3380 | |
1e66592c | 3381 | tree instance_ptr = *instance_ptrptr; |
aef309a1 | 3382 | tree instance_save_expr = 0; |
311e42bc | 3383 | if (instance_ptr == error_mark_node) |
788bd876 | 3384 | { |
311e42bc | 3385 | if (TREE_CODE (function) == PTRMEM_CST) |
3386 | { | |
3387 | /* Extracting the function address from a pmf is only | |
3388 | allowed with -Wno-pmf-conversions. It only works for | |
47cd6605 | 3389 | pmf constants. */ |
4405c1ad | 3390 | e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain); |
311e42bc | 3391 | e1 = convert (fntype, e1); |
3392 | return e1; | |
3393 | } | |
3394 | else | |
3395 | { | |
4405c1ad | 3396 | if (complain & tf_error) |
3397 | error ("object missing in use of %qE", function); | |
311e42bc | 3398 | return error_mark_node; |
3399 | } | |
788bd876 | 3400 | } |
3401 | ||
70791196 | 3402 | /* True if we know that the dynamic type of the object doesn't have |
3403 | virtual functions, so we can assume the PFN field is a pointer. */ | |
3404 | nonvirtual = (COMPLETE_TYPE_P (basetype) | |
3405 | && !TYPE_POLYMORPHIC_P (basetype) | |
3406 | && resolves_to_fixed_type_p (instance_ptr, 0)); | |
3407 | ||
31a434dd | 3408 | /* If we don't really have an object (i.e. in an ill-formed |
3409 | conversion from PMF to pointer), we can't resolve virtual | |
3410 | functions anyway. */ | |
3411 | if (!nonvirtual && is_dummy_object (instance_ptr)) | |
3412 | nonvirtual = true; | |
3413 | ||
1e66592c | 3414 | if (TREE_SIDE_EFFECTS (instance_ptr)) |
aef309a1 | 3415 | instance_ptr = instance_save_expr = save_expr (instance_ptr); |
1e66592c | 3416 | |
ce28ee2e | 3417 | if (TREE_SIDE_EFFECTS (function)) |
3418 | function = save_expr (function); | |
3419 | ||
311e42bc | 3420 | /* Start by extracting all the information from the PMF itself. */ |
e6c0970b | 3421 | e3 = pfn_from_ptrmemfunc (function); |
426ab4bb | 3422 | delta = delta_from_ptrmemfunc (function); |
33920115 | 3423 | idx = build1 (NOP_EXPR, vtable_index_type, e3); |
177d7cf5 | 3424 | switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) |
5e89f6fb | 3425 | { |
4150a431 | 3426 | int flag_sanitize_save; |
5e89f6fb | 3427 | case ptrmemfunc_vbit_in_pfn: |
8e70fb09 | 3428 | e1 = cp_build_binary_op (input_location, |
3429 | BIT_AND_EXPR, idx, integer_one_node, | |
4405c1ad | 3430 | complain); |
8e70fb09 | 3431 | idx = cp_build_binary_op (input_location, |
3432 | MINUS_EXPR, idx, integer_one_node, | |
4405c1ad | 3433 | complain); |
3434 | if (idx == error_mark_node) | |
3435 | return error_mark_node; | |
5e89f6fb | 3436 | break; |
3437 | ||
3438 | case ptrmemfunc_vbit_in_delta: | |
8e70fb09 | 3439 | e1 = cp_build_binary_op (input_location, |
3440 | BIT_AND_EXPR, delta, integer_one_node, | |
4405c1ad | 3441 | complain); |
4150a431 | 3442 | /* Don't instrument the RSHIFT_EXPR we're about to create because |
3443 | we're going to use DELTA number of times, and that wouldn't play | |
3444 | well with SAVE_EXPRs therein. */ | |
3445 | flag_sanitize_save = flag_sanitize; | |
3446 | flag_sanitize = 0; | |
8e70fb09 | 3447 | delta = cp_build_binary_op (input_location, |
3448 | RSHIFT_EXPR, delta, integer_one_node, | |
4405c1ad | 3449 | complain); |
4150a431 | 3450 | flag_sanitize = flag_sanitize_save; |
4405c1ad | 3451 | if (delta == error_mark_node) |
3452 | return error_mark_node; | |
5e89f6fb | 3453 | break; |
3454 | ||
3455 | default: | |
2e3e31d2 | 3456 | gcc_unreachable (); |
5e89f6fb | 3457 | } |
6095d882 | 3458 | |
4405c1ad | 3459 | if (e1 == error_mark_node) |
3460 | return error_mark_node; | |
3461 | ||
7e116ea8 | 3462 | /* Convert down to the right base before using the instance. A |
3463 | special case is that in a pointer to member of class C, C may | |
3464 | be incomplete. In that case, the function will of course be | |
3465 | a member of C, and no conversion is required. In fact, | |
3466 | lookup_base will fail in that case, because incomplete | |
9031d10b | 3467 | classes do not have BINFOs. */ |
9031d10b | 3468 | if (!same_type_ignoring_top_level_qualifiers_p |
7e116ea8 | 3469 | (basetype, TREE_TYPE (TREE_TYPE (instance_ptr)))) |
3470 | { | |
3471 | basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)), | |
ae260dcc | 3472 | basetype, ba_check, NULL, complain); |
9031d10b | 3473 | instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, |
4405c1ad | 3474 | 1, complain); |
7e116ea8 | 3475 | if (instance_ptr == error_mark_node) |
3476 | return error_mark_node; | |
3477 | } | |
311e42bc | 3478 | /* ...and then the delta in the PMF. */ |
2cc66f2a | 3479 | instance_ptr = fold_build_pointer_plus (instance_ptr, delta); |
311e42bc | 3480 | |
3481 | /* Hand back the adjusted 'this' argument to our caller. */ | |
3482 | *instance_ptrptr = instance_ptr; | |
3483 | ||
70791196 | 3484 | if (nonvirtual) |
3485 | /* Now just return the pointer. */ | |
3486 | return e3; | |
3487 | ||
311e42bc | 3488 | /* Next extract the vtable pointer from the object. */ |
3489 | vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node), | |
3490 | instance_ptr); | |
0744a0c1 | 3491 | vtbl = cp_build_fold_indirect_ref (vtbl); |
4405c1ad | 3492 | if (vtbl == error_mark_node) |
3493 | return error_mark_node; | |
3494 | ||
311e42bc | 3495 | /* Finally, extract the function pointer from the vtable. */ |
2cc66f2a | 3496 | e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx); |
0744a0c1 | 3497 | e2 = cp_build_fold_indirect_ref (e2); |
4405c1ad | 3498 | if (e2 == error_mark_node) |
3499 | return error_mark_node; | |
8bfe5753 | 3500 | TREE_CONSTANT (e2) = 1; |
63a6a423 | 3501 | |
6bfa2cc1 | 3502 | /* When using function descriptors, the address of the |
3503 | vtable entry is treated as a function pointer. */ | |
3504 | if (TARGET_VTABLE_USES_DESCRIPTORS) | |
3505 | e2 = build1 (NOP_EXPR, TREE_TYPE (e2), | |
4405c1ad | 3506 | cp_build_addr_expr (e2, complain)); |
6bfa2cc1 | 3507 | |
f607c97b | 3508 | e2 = fold_convert (TREE_TYPE (e3), e2); |
1273a0b4 | 3509 | e1 = build_conditional_expr (input_location, e1, e2, e3, complain); |
4405c1ad | 3510 | if (e1 == error_mark_node) |
3511 | return error_mark_node; | |
9031d10b | 3512 | |
6095d882 | 3513 | /* Make sure this doesn't get evaluated first inside one of the |
3514 | branches of the COND_EXPR. */ | |
aef309a1 | 3515 | if (instance_save_expr) |
831d52a2 | 3516 | e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1), |
3517 | instance_save_expr, e1); | |
6095d882 | 3518 | |
f17e56ef | 3519 | function = e1; |
471086d6 | 3520 | } |
3521 | return function; | |
3522 | } | |
3523 | ||
ebd21de4 | 3524 | /* Used by the C-common bits. */ |
471086d6 | 3525 | tree |
a49c5913 | 3526 | build_function_call (location_t /*loc*/, |
e60a6f7b | 3527 | tree function, tree params) |
ebd21de4 | 3528 | { |
3529 | return cp_build_function_call (function, params, tf_warning_or_error); | |
3530 | } | |
3531 | ||
b9c74b4d | 3532 | /* Used by the C-common bits. */ |
3533 | tree | |
ec761d5a | 3534 | build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/, |
f1f41a6c | 3535 | tree function, vec<tree, va_gc> *params, |
3536 | vec<tree, va_gc> * /*origtypes*/) | |
b9c74b4d | 3537 | { |
f1f41a6c | 3538 | vec<tree, va_gc> *orig_params = params; |
f352a3fb | 3539 | tree ret = cp_build_function_call_vec (function, ¶ms, |
3540 | tf_warning_or_error); | |
b9c74b4d | 3541 | |
f352a3fb | 3542 | /* cp_build_function_call_vec can reallocate PARAMS by adding |
3543 | default arguments. That should never happen here. Verify | |
3544 | that. */ | |
3545 | gcc_assert (params == orig_params); | |
3546 | ||
3547 | return ret; | |
b9c74b4d | 3548 | } |
3549 | ||
f352a3fb | 3550 | /* Build a function call using a tree list of arguments. */ |
3551 | ||
aafa8c0a | 3552 | static tree |
ebd21de4 | 3553 | cp_build_function_call (tree function, tree params, tsubst_flags_t complain) |
f352a3fb | 3554 | { |
f1f41a6c | 3555 | vec<tree, va_gc> *vec; |
f352a3fb | 3556 | tree ret; |
3557 | ||
3558 | vec = make_tree_vector (); | |
3559 | for (; params != NULL_TREE; params = TREE_CHAIN (params)) | |
f1f41a6c | 3560 | vec_safe_push (vec, TREE_VALUE (params)); |
f352a3fb | 3561 | ret = cp_build_function_call_vec (function, &vec, complain); |
3562 | release_tree_vector (vec); | |
3563 | return ret; | |
3564 | } | |
3565 | ||
054da6d7 | 3566 | /* Build a function call using varargs. */ |
3567 | ||
3568 | tree | |
3569 | cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...) | |
3570 | { | |
f1f41a6c | 3571 | vec<tree, va_gc> *vec; |
054da6d7 | 3572 | va_list args; |
3573 | tree ret, t; | |
3574 | ||
3575 | vec = make_tree_vector (); | |
3576 | va_start (args, complain); | |
3577 | for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree)) | |
f1f41a6c | 3578 | vec_safe_push (vec, t); |
054da6d7 | 3579 | va_end (args); |
3580 | ret = cp_build_function_call_vec (function, &vec, complain); | |
3581 | release_tree_vector (vec); | |
3582 | return ret; | |
3583 | } | |
3584 | ||
f352a3fb | 3585 | /* Build a function call using a vector of arguments. PARAMS may be |
3586 | NULL if there are no parameters. This changes the contents of | |
3587 | PARAMS. */ | |
3588 | ||
3589 | tree | |
f1f41a6c | 3590 | cp_build_function_call_vec (tree function, vec<tree, va_gc> **params, |
f352a3fb | 3591 | tsubst_flags_t complain) |
471086d6 | 3592 | { |
cd16867a | 3593 | tree fntype, fndecl; |
471086d6 | 3594 | int is_method; |
01b5d786 | 3595 | tree original = function; |
f352a3fb | 3596 | int nargs; |
d01f58f9 | 3597 | tree *argarray; |
3598 | tree parm_types; | |
f1f41a6c | 3599 | vec<tree, va_gc> *allocated = NULL; |
f352a3fb | 3600 | tree ret; |
471086d6 | 3601 | |
7a4e126b | 3602 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
3603 | expressions, like those used for ObjC messenger dispatches. */ | |
f1f41a6c | 3604 | if (params != NULL && !vec_safe_is_empty (*params)) |
3605 | function = objc_rewrite_function_call (function, (**params)[0]); | |
7a4e126b | 3606 | |
471086d6 | 3607 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
3608 | Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */ | |
3609 | if (TREE_CODE (function) == NOP_EXPR | |
3610 | && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0))) | |
3611 | function = TREE_OPERAND (function, 0); | |
3612 | ||
3613 | if (TREE_CODE (function) == FUNCTION_DECL) | |
3614 | { | |
56c12fd4 | 3615 | /* If the function is a non-template member function |
3616 | or a non-template friend, then we need to check the | |
3617 | constraints. | |
3618 | ||
3619 | Note that if overload resolution failed with a single | |
3620 | candidate this function will be used to explicitly diagnose | |
3621 | the failure for the single call expression. The check is | |
3622 | technically redundant since we also would have failed in | |
3623 | add_function_candidate. */ | |
3624 | if (flag_concepts | |
3625 | && (complain & tf_error) | |
3626 | && !constraints_satisfied_p (function)) | |
3627 | { | |
3628 | error ("cannot call function %qD", function); | |
3629 | location_t loc = DECL_SOURCE_LOCATION (function); | |
3630 | diagnose_constraints (loc, function, NULL_TREE); | |
3631 | return error_mark_node; | |
3632 | } | |
3633 | ||
4430f688 | 3634 | if (!mark_used (function, complain) && !(complain & tf_error)) |
3635 | return error_mark_node; | |
471086d6 | 3636 | fndecl = function; |
3637 | ||
3638 | /* Convert anything with function type to a pointer-to-function. */ | |
b34e07a1 | 3639 | if (DECL_MAIN_P (function)) |
3640 | { | |
3641 | if (complain & tf_error) | |
3642 | pedwarn (input_location, OPT_Wpedantic, | |
3643 | "ISO C++ forbids calling %<::main%> from within program"); | |
3644 | else | |
3645 | return error_mark_node; | |
3646 | } | |
4405c1ad | 3647 | function = build_addr_func (function, complain); |
471086d6 | 3648 | } |
3649 | else | |
3650 | { | |
3651 | fndecl = NULL_TREE; | |
3652 | ||
4405c1ad | 3653 | function = build_addr_func (function, complain); |
471086d6 | 3654 | } |
3655 | ||
e1dcbf32 | 3656 | if (function == error_mark_node) |
3657 | return error_mark_node; | |
3658 | ||
471086d6 | 3659 | fntype = TREE_TYPE (function); |
3660 | ||
3661 | if (TYPE_PTRMEMFUNC_P (fntype)) | |
3662 | { | |
ebd21de4 | 3663 | if (complain & tf_error) |
3664 | error ("must use %<.*%> or %<->*%> to call pointer-to-member " | |
6b9d606a | 3665 | "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>", |
3666 | original, original); | |
e1dcbf32 | 3667 | return error_mark_node; |
471086d6 | 3668 | } |
3669 | ||
c21c015b | 3670 | is_method = (TYPE_PTR_P (fntype) |
471086d6 | 3671 | && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE); |
3672 | ||
c21c015b | 3673 | if (!(TYPE_PTRFN_P (fntype) |
b1cfe2be | 3674 | || is_method |
3675 | || TREE_CODE (function) == TEMPLATE_ID_EXPR)) | |
471086d6 | 3676 | { |
ebd21de4 | 3677 | if (complain & tf_error) |
28738b20 | 3678 | { |
3679 | if (!flag_diagnostics_show_caret) | |
3680 | error_at (input_location, | |
3681 | "%qE cannot be used as a function", original); | |
3682 | else if (DECL_P (original)) | |
3683 | error_at (input_location, | |
3684 | "%qD cannot be used as a function", original); | |
3685 | else | |
3686 | error_at (input_location, | |
3687 | "expression cannot be used as a function"); | |
3688 | } | |
3689 | ||
471086d6 | 3690 | return error_mark_node; |
3691 | } | |
3692 | ||
3693 | /* fntype now gets the type of function pointed to. */ | |
3694 | fntype = TREE_TYPE (fntype); | |
d01f58f9 | 3695 | parm_types = TYPE_ARG_TYPES (fntype); |
3696 | ||
f352a3fb | 3697 | if (params == NULL) |
3698 | { | |
3699 | allocated = make_tree_vector (); | |
3700 | params = &allocated; | |
3701 | } | |
3702 | ||
e9331eab | 3703 | nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL, |
3704 | complain); | |
d01f58f9 | 3705 | if (nargs < 0) |
c6a06e1f | 3706 | return error_mark_node; |
8c5c575d | 3707 | |
f1f41a6c | 3708 | argarray = (*params)->address (); |
f352a3fb | 3709 | |
8f9758e6 | 3710 | /* Check for errors in format strings and inappropriately |
3711 | null parameters. */ | |
bbd5521e | 3712 | bool warned_p = check_function_arguments (input_location, fndecl, fntype, |
a115c319 | 3713 | nargs, argarray, NULL); |
471086d6 | 3714 | |
14376b10 | 3715 | ret = build_cxx_call (function, nargs, argarray, complain); |
f352a3fb | 3716 | |
184fac50 | 3717 | if (warned_p) |
3718 | { | |
3719 | tree c = extract_call_expr (ret); | |
3720 | if (TREE_CODE (c) == CALL_EXPR) | |
3721 | TREE_NO_WARNING (c) = 1; | |
3722 | } | |
3723 | ||
f352a3fb | 3724 | if (allocated != NULL) |
3725 | release_tree_vector (allocated); | |
3726 | ||
3727 | return ret; | |
471086d6 | 3728 | } |
471086d6 | 3729 | \f |
1c4a12a3 | 3730 | /* Subroutine of convert_arguments. |
e14647a7 | 3731 | Print an error message about a wrong number of arguments. */ |
1c4a12a3 | 3732 | |
3733 | static void | |
e14647a7 | 3734 | error_args_num (location_t loc, tree fndecl, bool too_many_p) |
1c4a12a3 | 3735 | { |
3736 | if (fndecl) | |
3737 | { | |
3738 | if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE) | |
3739 | { | |
3740 | if (DECL_NAME (fndecl) == NULL_TREE | |
3741 | || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl))) | |
3742 | error_at (loc, | |
3743 | too_many_p | |
3744 | ? G_("too many arguments to constructor %q#D") | |
3745 | : G_("too few arguments to constructor %q#D"), | |
3746 | fndecl); | |
3747 | else | |
3748 | error_at (loc, | |
3749 | too_many_p | |
3750 | ? G_("too many arguments to member function %q#D") | |
3751 | : G_("too few arguments to member function %q#D"), | |
3752 | fndecl); | |
3753 | } | |
3754 | else | |
3755 | error_at (loc, | |
3756 | too_many_p | |
3757 | ? G_("too many arguments to function %q#D") | |
3758 | : G_("too few arguments to function %q#D"), | |
3759 | fndecl); | |
312b8911 | 3760 | if (!DECL_IS_BUILTIN (fndecl)) |
3761 | inform (DECL_SOURCE_LOCATION (fndecl), "declared here"); | |
1c4a12a3 | 3762 | } |
3763 | else | |
1f3a51d6 | 3764 | { |
3765 | if (c_dialect_objc () && objc_message_selector ()) | |
3766 | error_at (loc, | |
3767 | too_many_p | |
3768 | ? G_("too many arguments to method %q#D") | |
3769 | : G_("too few arguments to method %q#D"), | |
3770 | objc_message_selector ()); | |
3771 | else | |
3772 | error_at (loc, too_many_p ? G_("too many arguments to function") | |
3773 | : G_("too few arguments to function")); | |
3774 | } | |
1c4a12a3 | 3775 | } |
3776 | ||
f352a3fb | 3777 | /* Convert the actual parameter expressions in the list VALUES to the |
3778 | types in the list TYPELIST. The converted expressions are stored | |
3779 | back in the VALUES vector. | |
471086d6 | 3780 | If parmdecls is exhausted, or when an element has NULL as its type, |
3781 | perform the default conversions. | |
3782 | ||
471086d6 | 3783 | NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. |
3784 | ||
3785 | This is also where warnings about wrong number of args are generated. | |
9031d10b | 3786 | |
d01f58f9 | 3787 | Returns the actual number of arguments processed (which might be less |
f352a3fb | 3788 | than the length of the vector), or -1 on error. |
471086d6 | 3789 | |
3790 | In C++, unspecified trailing parameters can be filled in with their | |
3791 | default arguments, if such were specified. Do so here. */ | |
3792 | ||
d01f58f9 | 3793 | static int |
f1f41a6c | 3794 | convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl, |
f352a3fb | 3795 | int flags, tsubst_flags_t complain) |
471086d6 | 3796 | { |
f352a3fb | 3797 | tree typetail; |
f352a3fb | 3798 | unsigned int i; |
471086d6 | 3799 | |
860740a7 | 3800 | /* Argument passing is always copy-initialization. */ |
3801 | flags |= LOOKUP_ONLYCONVERTING; | |
3802 | ||
f352a3fb | 3803 | for (i = 0, typetail = typelist; |
f1f41a6c | 3804 | i < vec_safe_length (*values); |
f352a3fb | 3805 | i++) |
471086d6 | 3806 | { |
cd16867a | 3807 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
f1f41a6c | 3808 | tree val = (**values)[i]; |
471086d6 | 3809 | |
7f623b33 | 3810 | if (val == error_mark_node || type == error_mark_node) |
d01f58f9 | 3811 | return -1; |
1a3f833b | 3812 | |
471086d6 | 3813 | if (type == void_type_node) |
3814 | { | |
ebd21de4 | 3815 | if (complain & tf_error) |
3816 | { | |
e14647a7 | 3817 | error_args_num (input_location, fndecl, /*too_many_p=*/true); |
ebd21de4 | 3818 | return i; |
3819 | } | |
3820 | else | |
3821 | return -1; | |
471086d6 | 3822 | } |
3823 | ||
471086d6 | 3824 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
3825 | Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */ | |
3826 | if (TREE_CODE (val) == NOP_EXPR | |
ddb9bca7 | 3827 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)) |
3828 | && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)) | |
471086d6 | 3829 | val = TREE_OPERAND (val, 0); |
3830 | ||
6495357a | 3831 | if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE) |
3832 | { | |
3833 | if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE | |
471086d6 | 3834 | || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE |
6495357a | 3835 | || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE) |
4405c1ad | 3836 | val = decay_conversion (val, complain); |
6495357a | 3837 | } |
471086d6 | 3838 | |
3839 | if (val == error_mark_node) | |
d01f58f9 | 3840 | return -1; |
471086d6 | 3841 | |
471086d6 | 3842 | if (type != 0) |
3843 | { | |
3844 | /* Formal parm type is specified by a function prototype. */ | |
3845 | tree parmval; | |
3846 | ||
4b72716d | 3847 | if (!COMPLETE_TYPE_P (complete_type (type))) |
471086d6 | 3848 | { |
ebd21de4 | 3849 | if (complain & tf_error) |
3850 | { | |
3851 | if (fndecl) | |
3852 | error ("parameter %P of %qD has incomplete type %qT", | |
3853 | i, fndecl, type); | |
3854 | else | |
3855 | error ("parameter %P has incomplete type %qT", i, type); | |
3856 | } | |
a4ad28aa | 3857 | parmval = error_mark_node; |
471086d6 | 3858 | } |
3859 | else | |
3860 | { | |
38281c46 | 3861 | parmval = convert_for_initialization |
5eaed30b | 3862 | (NULL_TREE, type, val, flags, |
183407ee | 3863 | ICR_ARGPASS, fndecl, i, complain); |
66bbeb85 | 3864 | parmval = convert_for_arg_passing (type, parmval, complain); |
471086d6 | 3865 | } |
b248d3f7 | 3866 | |
3867 | if (parmval == error_mark_node) | |
d01f58f9 | 3868 | return -1; |
b248d3f7 | 3869 | |
f1f41a6c | 3870 | (**values)[i] = parmval; |
471086d6 | 3871 | } |
3872 | else | |
3873 | { | |
936c3081 | 3874 | if (fndecl && magic_varargs_p (fndecl)) |
154e2d49 | 3875 | /* Don't do ellipsis conversion for __built_in_constant_p |
c1c67b4f | 3876 | as this will result in spurious errors for non-trivial |
154e2d49 | 3877 | types. */ |
dba1ea2f | 3878 | val = require_complete_type_sfinae (val, complain); |
154e2d49 | 3879 | else |
66bbeb85 | 3880 | val = convert_arg_to_ellipsis (val, complain); |
154e2d49 | 3881 | |
f1f41a6c | 3882 | (**values)[i] = val; |
471086d6 | 3883 | } |
3884 | ||
471086d6 | 3885 | if (typetail) |
3886 | typetail = TREE_CHAIN (typetail); | |
3887 | } | |
3888 | ||
3889 | if (typetail != 0 && typetail != void_list_node) | |
3890 | { | |
9330edc6 | 3891 | /* See if there are default arguments that can be used. Because |
3892 | we hold default arguments in the FUNCTION_TYPE (which is so | |
3893 | wrong), we can see default parameters here from deduced | |
3894 | contexts (and via typeof) for indirect function calls. | |
3895 | Fortunately we know whether we have a function decl to | |
3896 | provide default arguments in a language conformant | |
3897 | manner. */ | |
3898 | if (fndecl && TREE_PURPOSE (typetail) | |
0a3b29ad | 3899 | && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG) |
471086d6 | 3900 | { |
c38086bd | 3901 | for (; typetail != void_list_node; ++i) |
471086d6 | 3902 | { |
50909e97 | 3903 | /* After DR777, with explicit template args we can end up with a |
3904 | default argument followed by no default argument. */ | |
3905 | if (!TREE_PURPOSE (typetail)) | |
3906 | break; | |
9031d10b | 3907 | tree parmval |
3908 | = convert_default_arg (TREE_VALUE (typetail), | |
3909 | TREE_PURPOSE (typetail), | |
66bbeb85 | 3910 | fndecl, i, complain); |
471086d6 | 3911 | |
b248d3f7 | 3912 | if (parmval == error_mark_node) |
d01f58f9 | 3913 | return -1; |
b248d3f7 | 3914 | |
f1f41a6c | 3915 | vec_safe_push (*values, parmval); |
471086d6 | 3916 | typetail = TREE_CHAIN (typetail); |
3917 | /* ends with `...'. */ | |
3918 | if (typetail == NULL_TREE) | |
3919 | break; | |
3920 | } | |
3921 | } | |
50909e97 | 3922 | |
3923 | if (typetail && typetail != void_list_node) | |
471086d6 | 3924 | { |
50909e97 | 3925 | if (complain & tf_error) |
e14647a7 | 3926 | error_args_num (input_location, fndecl, /*too_many_p=*/false); |
d01f58f9 | 3927 | return -1; |
471086d6 | 3928 | } |
3929 | } | |
471086d6 | 3930 | |
f352a3fb | 3931 | return (int) i; |
471086d6 | 3932 | } |
3933 | \f | |
3934 | /* Build a binary-operation expression, after performing default | |
e534436e | 3935 | conversions on the operands. CODE is the kind of expression to |
3936 | build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE | |
3937 | are the tree codes which correspond to ARG1 and ARG2 when issuing | |
3938 | warnings about possibly misplaced parentheses. They may differ | |
3939 | from the TREE_CODE of ARG1 and ARG2 if the parser has done constant | |
3940 | folding (e.g., if the parser sees "a | 1 + 1", it may call this | |
3941 | routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR). | |
3942 | To avoid issuing any parentheses warnings, pass ARG1_CODE and/or | |
3943 | ARG2_CODE as ERROR_MARK. */ | |
471086d6 | 3944 | |
3945 | tree | |
ef0b0c72 | 3946 | build_x_binary_op (location_t loc, enum tree_code code, tree arg1, |
3947 | enum tree_code arg1_code, tree arg2, | |
d175f019 | 3948 | enum tree_code arg2_code, tree *overload_p, |
ebd21de4 | 3949 | tsubst_flags_t complain) |
471086d6 | 3950 | { |
13795292 | 3951 | tree orig_arg1; |
3952 | tree orig_arg2; | |
3953 | tree expr; | |
d175f019 | 3954 | tree overload = NULL_TREE; |
e857e9c7 | 3955 | |
13795292 | 3956 | orig_arg1 = arg1; |
3957 | orig_arg2 = arg2; | |
a63bc44c | 3958 | |
13795292 | 3959 | if (processing_template_decl) |
5f6526e1 | 3960 | { |
13795292 | 3961 | if (type_dependent_expression_p (arg1) |
3962 | || type_dependent_expression_p (arg2)) | |
255b5d15 | 3963 | return build_min_nt_loc (loc, code, arg1, arg2); |
13795292 | 3964 | arg1 = build_non_dependent_expr (arg1); |
3965 | arg2 = build_non_dependent_expr (arg2); | |
3966 | } | |
5f6526e1 | 3967 | |
13795292 | 3968 | if (code == DOTSTAR_EXPR) |
4405c1ad | 3969 | expr = build_m_component_ref (arg1, arg2, complain); |
13795292 | 3970 | else |
ef0b0c72 | 3971 | expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE, |
d175f019 | 3972 | &overload, complain); |
3973 | ||
3974 | if (overload_p != NULL) | |
3975 | *overload_p = overload; | |
5f6526e1 | 3976 | |
e534436e | 3977 | /* Check for cases such as x+y<<z which users are likely to |
3978 | misinterpret. But don't warn about obj << x + y, since that is a | |
3979 | common idiom for I/O. */ | |
3980 | if (warn_parentheses | |
7d926126 | 3981 | && (complain & tf_warning) |
e534436e | 3982 | && !processing_template_decl |
3983 | && !error_operand_p (arg1) | |
3984 | && !error_operand_p (arg2) | |
3985 | && (code != LSHIFT_EXPR | |
3986 | || !CLASS_TYPE_P (TREE_TYPE (arg1)))) | |
b0e7825e | 3987 | warn_about_parentheses (loc, code, arg1_code, orig_arg1, |
3988 | arg2_code, orig_arg2); | |
e534436e | 3989 | |
13795292 | 3990 | if (processing_template_decl && expr != error_mark_node) |
d175f019 | 3991 | { |
3992 | if (overload != NULL_TREE) | |
3993 | return (build_min_non_dep_op_overload | |
3994 | (code, expr, overload, orig_arg1, orig_arg2)); | |
3995 | ||
3996 | return build_min_non_dep (code, expr, orig_arg1, orig_arg2); | |
3997 | } | |
9031d10b | 3998 | |
13795292 | 3999 | return expr; |
5f6526e1 | 4000 | } |
4001 | ||
849a3bb4 | 4002 | /* Build and return an ARRAY_REF expression. */ |
4003 | ||
4004 | tree | |
255b5d15 | 4005 | build_x_array_ref (location_t loc, tree arg1, tree arg2, |
4006 | tsubst_flags_t complain) | |
849a3bb4 | 4007 | { |
4008 | tree orig_arg1 = arg1; | |
4009 | tree orig_arg2 = arg2; | |
4010 | tree expr; | |
d175f019 | 4011 | tree overload = NULL_TREE; |
849a3bb4 | 4012 | |
4013 | if (processing_template_decl) | |
4014 | { | |
4015 | if (type_dependent_expression_p (arg1) | |
4016 | || type_dependent_expression_p (arg2)) | |
255b5d15 | 4017 | return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2, |
4018 | NULL_TREE, NULL_TREE); | |
849a3bb4 | 4019 | arg1 = build_non_dependent_expr (arg1); |
4020 | arg2 = build_non_dependent_expr (arg2); | |
4021 | } | |
4022 | ||
255b5d15 | 4023 | expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, |
d175f019 | 4024 | NULL_TREE, &overload, complain); |
849a3bb4 | 4025 | |
4026 | if (processing_template_decl && expr != error_mark_node) | |
d175f019 | 4027 | { |
4028 | if (overload != NULL_TREE) | |
4029 | return (build_min_non_dep_op_overload | |
4030 | (ARRAY_REF, expr, overload, orig_arg1, orig_arg2)); | |
4031 | ||
4032 | return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2, | |
4033 | NULL_TREE, NULL_TREE); | |
4034 | } | |
849a3bb4 | 4035 | return expr; |
4036 | } | |
4037 | ||
3637bb87 | 4038 | /* Return whether OP is an expression of enum type cast to integer |
4039 | type. In C++ even unsigned enum types are cast to signed integer | |
4040 | types. We do not want to issue warnings about comparisons between | |
4041 | signed and unsigned types when one of the types is an enum type. | |
4042 | Those warnings are always false positives in practice. */ | |
4043 | ||
4044 | static bool | |
4045 | enum_cast_to_int (tree op) | |
4046 | { | |
033c0b51 | 4047 | if (CONVERT_EXPR_P (op) |
3637bb87 | 4048 | && TREE_TYPE (op) == integer_type_node |
4049 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE | |
4050 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0)))) | |
4051 | return true; | |
4052 | ||
4053 | /* The cast may have been pushed into a COND_EXPR. */ | |
4054 | if (TREE_CODE (op) == COND_EXPR) | |
4055 | return (enum_cast_to_int (TREE_OPERAND (op, 1)) | |
4056 | || enum_cast_to_int (TREE_OPERAND (op, 2))); | |
4057 | ||
4058 | return false; | |
4059 | } | |
4060 | ||
ebd21de4 | 4061 | /* For the c-common bits. */ |
4062 | tree | |
8e70fb09 | 4063 | build_binary_op (location_t location, enum tree_code code, tree op0, tree op1, |
623ee358 | 4064 | bool /*convert_p*/) |
ebd21de4 | 4065 | { |
8e70fb09 | 4066 | return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error); |
ebd21de4 | 4067 | } |
4068 | ||
1f137e6d | 4069 | /* Build a vector comparison of ARG0 and ARG1 using CODE opcode |
4070 | into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */ | |
4071 | ||
4072 | static tree | |
4073 | build_vec_cmp (tree_code code, tree type, | |
4074 | tree arg0, tree arg1) | |
4075 | { | |
4076 | tree zero_vec = build_zero_cst (type); | |
4077 | tree minus_one_vec = build_minus_one_cst (type); | |
4078 | tree cmp_type = build_same_sized_truth_vector_type(type); | |
4079 | tree cmp = build2 (code, cmp_type, arg0, arg1); | |
1f137e6d | 4080 | return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec); |
4081 | } | |
ebd21de4 | 4082 | |
9a80bc1a | 4083 | /* Possibly warn about an address never being NULL. */ |
4084 | ||
4085 | static void | |
4086 | warn_for_null_address (location_t location, tree op, tsubst_flags_t complain) | |
4087 | { | |
4088 | if (!warn_address | |
4089 | || (complain & tf_warning) == 0 | |
4090 | || c_inhibit_evaluation_warnings != 0 | |
4091 | || TREE_NO_WARNING (op)) | |
4092 | return; | |
4093 | ||
4094 | tree cop = fold_non_dependent_expr (op); | |
4095 | ||
4096 | if (TREE_CODE (cop) == ADDR_EXPR | |
4097 | && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0)) | |
4098 | && !TREE_NO_WARNING (cop)) | |
4099 | warning_at (location, OPT_Waddress, "the address of %qD will never " | |
4100 | "be NULL", TREE_OPERAND (cop, 0)); | |
4101 | ||
4102 | if (CONVERT_EXPR_P (op) | |
4103 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE) | |
4104 | { | |
4105 | tree inner_op = op; | |
4106 | STRIP_NOPS (inner_op); | |
4107 | ||
4108 | if (DECL_P (inner_op)) | |
4109 | warning_at (location, OPT_Waddress, | |
4110 | "the compiler can assume that the address of " | |
4111 | "%qD will never be NULL", inner_op); | |
4112 | } | |
4113 | } | |
4114 | ||
471086d6 | 4115 | /* Build a binary-operation expression without default conversions. |
4116 | CODE is the kind of expression to build. | |
8e70fb09 | 4117 | LOCATION is the location_t of the operator in the source code. |
471086d6 | 4118 | This function differs from `build' in several ways: |
4119 | the data type of the result is computed and recorded in it, | |
4120 | warnings are generated if arg data types are invalid, | |
4121 | special handling for addition and subtraction of pointers is known, | |
4122 | and some optimization is done (operations on narrow ints | |
4123 | are done in the narrower type when that gives the same result). | |
4124 | Constant folding is also done before the result is returned. | |
4125 | ||
471086d6 | 4126 | Note that the operands will never have enumeral types |
4127 | because either they have just had the default conversions performed | |
4128 | or they have both just been converted to some other type in which | |
4129 | the arithmetic is to be done. | |
4130 | ||
4131 | C++: must do special pointer arithmetic when implementing | |
4132 | multiple inheritance, and deal with pointer to member functions. */ | |
4133 | ||
4134 | tree | |
8e70fb09 | 4135 | cp_build_binary_op (location_t location, |
4136 | enum tree_code code, tree orig_op0, tree orig_op1, | |
ebd21de4 | 4137 | tsubst_flags_t complain) |
471086d6 | 4138 | { |
bc3887cf | 4139 | tree op0, op1; |
cd16867a | 4140 | enum tree_code code0, code1; |
bc3887cf | 4141 | tree type0, type1; |
7a979707 | 4142 | const char *invalid_op_diag; |
471086d6 | 4143 | |
4144 | /* Expression code to give to the expression when it is built. | |
4145 | Normally this is CODE, which is what the caller asked for, | |
4146 | but in some special cases we change it. */ | |
cd16867a | 4147 | enum tree_code resultcode = code; |
471086d6 | 4148 | |
4149 | /* Data type in which the computation is to be performed. | |
4150 | In the simplest cases this is the common type of the arguments. */ | |
69450474 | 4151 | tree result_type = NULL_TREE; |
471086d6 | 4152 | |
4153 | /* Nonzero means operands have already been type-converted | |
4154 | in whatever way is necessary. | |
4155 | Zero means they need to be converted to RESULT_TYPE. */ | |
4156 | int converted = 0; | |
4157 | ||
bea7d742 | 4158 | /* Nonzero means create the expression with this type, rather than |
4159 | RESULT_TYPE. */ | |
4160 | tree build_type = 0; | |
4161 | ||
471086d6 | 4162 | /* Nonzero means after finally constructing the expression |
bb09dca5 | 4163 | convert it to this type. */ |
471086d6 | 4164 | tree final_type = 0; |
4165 | ||
d2c63826 | 4166 | tree result, result_ovl; |
5d7ed6c7 | 4167 | |
471086d6 | 4168 | /* Nonzero if this is an operation like MIN or MAX which can |
4169 | safely be computed in short if both args are promoted shorts. | |
4170 | Also implies COMMON. | |
4171 | -1 indicates a bitwise operation; this makes a difference | |
4172 | in the exact conditions for when it is safe to do the operation | |
4173 | in a narrower mode. */ | |
4174 | int shorten = 0; | |
4175 | ||
4176 | /* Nonzero if this is a comparison operation; | |
4177 | if both args are promoted shorts, compare the original shorts. | |
4178 | Also implies COMMON. */ | |
4179 | int short_compare = 0; | |
4180 | ||
471086d6 | 4181 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ |
4182 | int common = 0; | |
4183 | ||
5d7ed6c7 | 4184 | /* True if both operands have arithmetic type. */ |
4185 | bool arithmetic_types_p; | |
4186 | ||
bc3887cf | 4187 | /* Apply default conversions. */ |
b93d9a53 | 4188 | op0 = orig_op0; |
4189 | op1 = orig_op1; | |
9031d10b | 4190 | |
9e46467d | 4191 | /* Remember whether we're doing / or %. */ |
4192 | bool doing_div_or_mod = false; | |
4193 | ||
4194 | /* Remember whether we're doing << or >>. */ | |
4195 | bool doing_shift = false; | |
4196 | ||
4197 | /* Tree holding instrumentation expression. */ | |
69450474 | 4198 | tree instrument_expr = NULL_TREE; |
9e46467d | 4199 | |
d0622bdf | 4200 | if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR |
4201 | || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR | |
4202 | || code == TRUTH_XOR_EXPR) | |
4203 | { | |
6dd054d5 | 4204 | if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0))) |
4405c1ad | 4205 | op0 = decay_conversion (op0, complain); |
6dd054d5 | 4206 | if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1))) |
4405c1ad | 4207 | op1 = decay_conversion (op1, complain); |
d0622bdf | 4208 | } |
4209 | else | |
4210 | { | |
6dd054d5 | 4211 | if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0))) |
4405c1ad | 4212 | op0 = cp_default_conversion (op0, complain); |
6dd054d5 | 4213 | if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1))) |
4405c1ad | 4214 | op1 = cp_default_conversion (op1, complain); |
d0622bdf | 4215 | } |
bc3887cf | 4216 | |
69efd650 | 4217 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
4218 | STRIP_TYPE_NOPS (op0); | |
4219 | STRIP_TYPE_NOPS (op1); | |
4220 | ||
4221 | /* DTRT if one side is an overloaded function, but complain about it. */ | |
4222 | if (type_unknown_p (op0)) | |
4223 | { | |
4b471722 | 4224 | tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none); |
69efd650 | 4225 | if (t != error_mark_node) |
4226 | { | |
ebd21de4 | 4227 | if (complain & tf_error) |
2b9e3597 | 4228 | permerror (input_location, "assuming cast to type %qT from overloaded function", |
561fec9d | 4229 | TREE_TYPE (t)); |
69efd650 | 4230 | op0 = t; |
4231 | } | |
4232 | } | |
4233 | if (type_unknown_p (op1)) | |
4234 | { | |
4b471722 | 4235 | tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none); |
69efd650 | 4236 | if (t != error_mark_node) |
4237 | { | |
ebd21de4 | 4238 | if (complain & tf_error) |
2b9e3597 | 4239 | permerror (input_location, "assuming cast to type %qT from overloaded function", |
561fec9d | 4240 | TREE_TYPE (t)); |
69efd650 | 4241 | op1 = t; |
4242 | } | |
4243 | } | |
4244 | ||
936c3081 | 4245 | type0 = TREE_TYPE (op0); |
4246 | type1 = TREE_TYPE (op1); | |
bc3887cf | 4247 | |
4248 | /* The expression codes of the data types of the arguments tell us | |
4249 | whether the arguments are integers, floating, pointers, etc. */ | |
4250 | code0 = TREE_CODE (type0); | |
4251 | code1 = TREE_CODE (type1); | |
4252 | ||
471086d6 | 4253 | /* If an error was already reported for one of the arguments, |
4254 | avoid reporting another error. */ | |
471086d6 | 4255 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
4256 | return error_mark_node; | |
4257 | ||
7a979707 | 4258 | if ((invalid_op_diag |
4259 | = targetm.invalid_binary_op (code, type0, type1))) | |
4260 | { | |
19b271a9 | 4261 | if (complain & tf_error) |
4262 | error (invalid_op_diag); | |
7a979707 | 4263 | return error_mark_node; |
4264 | } | |
4265 | ||
6e25bee3 | 4266 | /* Issue warnings about peculiar, but valid, uses of NULL. */ |
4267 | if ((orig_op0 == null_node || orig_op1 == null_node) | |
4268 | /* It's reasonable to use pointer values as operands of && | |
4269 | and ||, so NULL is no exception. */ | |
4270 | && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR | |
4271 | && ( /* Both are NULL (or 0) and the operation was not a | |
4272 | comparison or a pointer subtraction. */ | |
4273 | (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1) | |
4274 | && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR) | |
4275 | /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */ | |
4276 | || (!null_ptr_cst_p (orig_op0) | |
05765a91 | 4277 | && !TYPE_PTR_OR_PTRMEM_P (type0)) |
6e25bee3 | 4278 | || (!null_ptr_cst_p (orig_op1) |
05765a91 | 4279 | && !TYPE_PTR_OR_PTRMEM_P (type1))) |
6e25bee3 | 4280 | && (complain & tf_warning)) |
db30b351 | 4281 | { |
4282 | source_location loc = | |
4283 | expansion_point_location_if_in_system_header (input_location); | |
4284 | ||
4285 | warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic"); | |
4286 | } | |
6e25bee3 | 4287 | |
41ed701a | 4288 | /* In case when one of the operands of the binary operation is |
4289 | a vector and another is a scalar -- convert scalar to vector. */ | |
4290 | if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE)) | |
4291 | { | |
4292 | enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1, | |
4293 | complain & tf_error); | |
4294 | ||
4295 | switch (convert_flag) | |
4296 | { | |
4297 | case stv_error: | |
4298 | return error_mark_node; | |
4299 | case stv_firstarg: | |
4300 | { | |
4301 | op0 = convert (TREE_TYPE (type1), op0); | |
5e84569c | 4302 | op0 = save_expr (op0); |
41ed701a | 4303 | op0 = build_vector_from_val (type1, op0); |
4304 | type0 = TREE_TYPE (op0); | |
4305 | code0 = TREE_CODE (type0); | |
4306 | converted = 1; | |
4307 | break; | |
4308 | } | |
4309 | case stv_secondarg: | |
4310 | { | |
4311 | op1 = convert (TREE_TYPE (type0), op1); | |
5e84569c | 4312 | op1 = save_expr (op1); |
41ed701a | 4313 | op1 = build_vector_from_val (type0, op1); |
4314 | type1 = TREE_TYPE (op1); | |
4315 | code1 = TREE_CODE (type1); | |
4316 | converted = 1; | |
4317 | break; | |
4318 | } | |
4319 | default: | |
4320 | break; | |
4321 | } | |
4322 | } | |
4323 | ||
471086d6 | 4324 | switch (code) |
4325 | { | |
471086d6 | 4326 | case MINUS_EXPR: |
4327 | /* Subtraction of two similar pointers. | |
4328 | We must subtract them as integers, then divide by object size. */ | |
4329 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
1bc16cab | 4330 | && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0), |
4331 | TREE_TYPE (type1))) | |
eabe2d94 | 4332 | { |
4333 | result = pointer_diff (location, op0, op1, | |
4334 | common_pointer_type (type0, type1), complain, | |
4335 | &instrument_expr); | |
4336 | if (instrument_expr != NULL) | |
4337 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), | |
4338 | instrument_expr, result); | |
4339 | ||
4340 | return result; | |
4341 | } | |
8c387dd6 | 4342 | /* In all other cases except pointer - int, the usual arithmetic |
08cc44e7 | 4343 | rules apply. */ |
8c387dd6 | 4344 | else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE)) |
4345 | { | |
4346 | common = 1; | |
4347 | break; | |
4348 | } | |
4349 | /* The pointer - int case is just like pointer + int; fall | |
4350 | through. */ | |
3c77f69c | 4351 | gcc_fallthrough (); |
8c387dd6 | 4352 | case PLUS_EXPR: |
4353 | if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE) | |
4354 | && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE)) | |
4355 | { | |
4356 | tree ptr_operand; | |
4357 | tree int_operand; | |
4358 | ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1); | |
4359 | int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1); | |
4360 | if (processing_template_decl) | |
4361 | { | |
4362 | result_type = TREE_TYPE (ptr_operand); | |
4363 | break; | |
4364 | } | |
22a3f7bd | 4365 | return cp_pointer_int_sum (location, code, |
4366 | ptr_operand, | |
42f9a786 | 4367 | int_operand, |
4368 | complain); | |
8c387dd6 | 4369 | } |
4370 | common = 1; | |
471086d6 | 4371 | break; |
4372 | ||
4373 | case MULT_EXPR: | |
4374 | common = 1; | |
4375 | break; | |
4376 | ||
4377 | case TRUNC_DIV_EXPR: | |
4378 | case CEIL_DIV_EXPR: | |
4379 | case FLOOR_DIV_EXPR: | |
4380 | case ROUND_DIV_EXPR: | |
4381 | case EXACT_DIV_EXPR: | |
0ca70bfd | 4382 | if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR) |
4383 | { | |
4384 | tree type0 = TREE_OPERAND (op0, 0); | |
4385 | tree type1 = TREE_OPERAND (op1, 0); | |
4386 | tree first_arg = type0; | |
4387 | if (!TYPE_P (type0)) | |
4388 | type0 = TREE_TYPE (type0); | |
4389 | if (!TYPE_P (type1)) | |
4390 | type1 = TREE_TYPE (type1); | |
4391 | if (POINTER_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1) | |
4392 | && !(TREE_CODE (first_arg) == PARM_DECL | |
4393 | && DECL_ARRAY_PARAMETER_P (first_arg) | |
4394 | && warn_sizeof_array_argument) | |
4395 | && (complain & tf_warning)) | |
4396 | if (warning_at (location, OPT_Wsizeof_pointer_div, | |
4397 | "division %<sizeof (%T) / sizeof (%T)%> does " | |
4398 | "not compute the number of array elements", | |
4399 | type0, type1)) | |
4400 | if (DECL_P (first_arg)) | |
4401 | inform (DECL_SOURCE_LOCATION (first_arg), | |
4402 | "first %<sizeof%> operand was declared here"); | |
4403 | } | |
4404 | ||
c4a8ac95 | 4405 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
4418a1d4 | 4406 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
c4a8ac95 | 4407 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
4418a1d4 | 4408 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
471086d6 | 4409 | { |
3aa45f71 | 4410 | enum tree_code tcode0 = code0, tcode1 = code1; |
21131a05 | 4411 | tree cop1 = fold_non_dependent_expr (op1); |
52cc0072 | 4412 | doing_div_or_mod = true; |
9e46467d | 4413 | warn_for_div_by_zero (location, cop1); |
9031d10b | 4414 | |
3aa45f71 | 4415 | if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE) |
4416 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); | |
4417 | if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE) | |
4418 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); | |
4418a1d4 | 4419 | |
3aa45f71 | 4420 | if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)) |
471086d6 | 4421 | resultcode = RDIV_EXPR; |
4422 | else | |
4423 | /* When dividing two signed integers, we have to promote to int. | |
01cc3b75 | 4424 | unless we divide by a constant != -1. Note that default |
471086d6 | 4425 | conversion will have been performed on the operands at this |
4426 | point, so we have to dig out the original type to find out if | |
4427 | it was unsigned. */ | |
4428 | shorten = ((TREE_CODE (op0) == NOP_EXPR | |
78a8ed03 | 4429 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) |
471086d6 | 4430 | || (TREE_CODE (op1) == INTEGER_CST |
a0c2c45b | 4431 | && ! integer_all_onesp (op1))); |
4432 | ||
471086d6 | 4433 | common = 1; |
4434 | } | |
4435 | break; | |
4436 | ||
4437 | case BIT_AND_EXPR: | |
471086d6 | 4438 | case BIT_IOR_EXPR: |
4439 | case BIT_XOR_EXPR: | |
955f5493 | 4440 | if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
f8629d35 | 4441 | || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
4442 | && !VECTOR_FLOAT_TYPE_P (type0) | |
4443 | && !VECTOR_FLOAT_TYPE_P (type1))) | |
471086d6 | 4444 | shorten = -1; |
471086d6 | 4445 | break; |
4446 | ||
4447 | case TRUNC_MOD_EXPR: | |
4448 | case FLOOR_MOD_EXPR: | |
8d24d167 | 4449 | { |
21131a05 | 4450 | tree cop1 = fold_non_dependent_expr (op1); |
52cc0072 | 4451 | doing_div_or_mod = true; |
9e46467d | 4452 | warn_for_div_by_zero (location, cop1); |
8d24d167 | 4453 | } |
9031d10b | 4454 | |
b6d7101e | 4455 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
4456 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
4457 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
4458 | common = 1; | |
4459 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
471086d6 | 4460 | { |
4461 | /* Although it would be tempting to shorten always here, that loses | |
4462 | on some targets, since the modulo instruction is undefined if the | |
4463 | quotient can't be represented in the computation mode. We shorten | |
4464 | only if unsigned or if dividing by something we know != -1. */ | |
4465 | shorten = ((TREE_CODE (op0) == NOP_EXPR | |
78a8ed03 | 4466 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) |
471086d6 | 4467 | || (TREE_CODE (op1) == INTEGER_CST |
a0c2c45b | 4468 | && ! integer_all_onesp (op1))); |
471086d6 | 4469 | common = 1; |
4470 | } | |
4471 | break; | |
4472 | ||
4473 | case TRUTH_ANDIF_EXPR: | |
4474 | case TRUTH_ORIF_EXPR: | |
4475 | case TRUTH_AND_EXPR: | |
4476 | case TRUTH_OR_EXPR: | |
5e84569c | 4477 | if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1)) |
bad99c7d | 4478 | { |
5e84569c | 4479 | if (!COMPARISON_CLASS_P (op1)) |
4480 | op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1, | |
4481 | build_zero_cst (type1), complain); | |
4482 | if (code == TRUTH_ANDIF_EXPR) | |
4483 | { | |
4484 | tree z = build_zero_cst (TREE_TYPE (op1)); | |
4485 | return build_conditional_expr (location, op0, op1, z, complain); | |
4486 | } | |
4487 | else if (code == TRUTH_ORIF_EXPR) | |
4488 | { | |
4489 | tree m1 = build_all_ones_cst (TREE_TYPE (op1)); | |
4490 | return build_conditional_expr (location, op0, m1, op1, complain); | |
4491 | } | |
4492 | else | |
4493 | gcc_unreachable (); | |
4494 | } | |
4495 | if (VECTOR_TYPE_P (type0)) | |
4496 | { | |
4497 | if (!COMPARISON_CLASS_P (op0)) | |
4498 | op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0, | |
4499 | build_zero_cst (type0), complain); | |
4500 | if (!VECTOR_TYPE_P (type1)) | |
4501 | { | |
4502 | tree m1 = build_all_ones_cst (TREE_TYPE (op0)); | |
4503 | tree z = build_zero_cst (TREE_TYPE (op0)); | |
c689c252 | 4504 | op1 = build_conditional_expr (location, op1, m1, z, complain); |
5e84569c | 4505 | } |
4506 | else if (!COMPARISON_CLASS_P (op1)) | |
4507 | op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1, | |
4508 | build_zero_cst (type1), complain); | |
4509 | ||
4510 | if (code == TRUTH_ANDIF_EXPR) | |
4511 | code = BIT_AND_EXPR; | |
4512 | else if (code == TRUTH_ORIF_EXPR) | |
4513 | code = BIT_IOR_EXPR; | |
4514 | else | |
4515 | gcc_unreachable (); | |
4516 | ||
4517 | return cp_build_binary_op (location, code, op0, op1, complain); | |
bad99c7d | 4518 | } |
5e84569c | 4519 | |
ca2be8b5 | 4520 | result_type = boolean_type_node; |
471086d6 | 4521 | break; |
4522 | ||
4523 | /* Shift operations: result has same type as first operand; | |
4524 | always convert second operand to int. | |
4525 | Also set SHORT_SHIFT if shifting rightward. */ | |
4526 | ||
4527 | case RSHIFT_EXPR: | |
41ed701a | 4528 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
4529 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
4530 | { | |
4531 | result_type = type0; | |
4532 | converted = 1; | |
4533 | } | |
4534 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
357d8e5d | 4535 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
4536 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
4537 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
4538 | { | |
4539 | result_type = type0; | |
4540 | converted = 1; | |
4541 | } | |
4542 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
471086d6 | 4543 | { |
21131a05 | 4544 | tree const_op1 = fold_non_dependent_expr (op1); |
7c834436 | 4545 | if (TREE_CODE (const_op1) != INTEGER_CST) |
4546 | const_op1 = op1; | |
471086d6 | 4547 | result_type = type0; |
9e46467d | 4548 | doing_shift = true; |
7c834436 | 4549 | if (TREE_CODE (const_op1) == INTEGER_CST) |
471086d6 | 4550 | { |
7c834436 | 4551 | if (tree_int_cst_lt (const_op1, integer_zero_node)) |
ebd21de4 | 4552 | { |
48d94ede | 4553 | if ((complain & tf_warning) |
4554 | && c_inhibit_evaluation_warnings == 0) | |
0feb4de1 | 4555 | warning (OPT_Wshift_count_negative, |
4556 | "right shift count is negative"); | |
ebd21de4 | 4557 | } |
471086d6 | 4558 | else |
4559 | { | |
7c834436 | 4560 | if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0 |
48d94ede | 4561 | && (complain & tf_warning) |
4562 | && c_inhibit_evaluation_warnings == 0) | |
0feb4de1 | 4563 | warning (OPT_Wshift_count_overflow, |
4564 | "right shift count >= width of type"); | |
471086d6 | 4565 | } |
4566 | } | |
471086d6 | 4567 | /* Avoid converting op1 to result_type later. */ |
4568 | converted = 1; | |
4569 | } | |
4570 | break; | |
4571 | ||
4572 | case LSHIFT_EXPR: | |
41ed701a | 4573 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
4574 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
4575 | { | |
4576 | result_type = type0; | |
4577 | converted = 1; | |
4578 | } | |
4579 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
357d8e5d | 4580 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
4581 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
4582 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
4583 | { | |
4584 | result_type = type0; | |
4585 | converted = 1; | |
4586 | } | |
4587 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
471086d6 | 4588 | { |
cdc64059 | 4589 | tree const_op0 = fold_non_dependent_expr (op0); |
4590 | if (TREE_CODE (const_op0) != INTEGER_CST) | |
4591 | const_op0 = op0; | |
21131a05 | 4592 | tree const_op1 = fold_non_dependent_expr (op1); |
7c834436 | 4593 | if (TREE_CODE (const_op1) != INTEGER_CST) |
4594 | const_op1 = op1; | |
471086d6 | 4595 | result_type = type0; |
9e46467d | 4596 | doing_shift = true; |
cdc64059 | 4597 | if (TREE_CODE (const_op0) == INTEGER_CST |
4598 | && tree_int_cst_sgn (const_op0) < 0 | |
4599 | && (complain & tf_warning) | |
4600 | && c_inhibit_evaluation_warnings == 0) | |
4601 | warning (OPT_Wshift_negative_value, | |
4602 | "left shift of negative value"); | |
7c834436 | 4603 | if (TREE_CODE (const_op1) == INTEGER_CST) |
471086d6 | 4604 | { |
7c834436 | 4605 | if (tree_int_cst_lt (const_op1, integer_zero_node)) |
ebd21de4 | 4606 | { |
48d94ede | 4607 | if ((complain & tf_warning) |
4608 | && c_inhibit_evaluation_warnings == 0) | |
0feb4de1 | 4609 | warning (OPT_Wshift_count_negative, |
4610 | "left shift count is negative"); | |
ebd21de4 | 4611 | } |
7c834436 | 4612 | else if (compare_tree_int (const_op1, |
4613 | TYPE_PRECISION (type0)) >= 0) | |
ebd21de4 | 4614 | { |
48d94ede | 4615 | if ((complain & tf_warning) |
4616 | && c_inhibit_evaluation_warnings == 0) | |
0feb4de1 | 4617 | warning (OPT_Wshift_count_overflow, |
4618 | "left shift count >= width of type"); | |
ebd21de4 | 4619 | } |
8d669e79 | 4620 | else if (TREE_CODE (const_op0) == INTEGER_CST |
4621 | && (complain & tf_warning)) | |
4622 | maybe_warn_shift_overflow (location, const_op0, const_op1); | |
471086d6 | 4623 | } |
471086d6 | 4624 | /* Avoid converting op1 to result_type later. */ |
4625 | converted = 1; | |
4626 | } | |
4627 | break; | |
4628 | ||
4629 | case RROTATE_EXPR: | |
4630 | case LROTATE_EXPR: | |
4631 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
4632 | { | |
4633 | result_type = type0; | |
4634 | if (TREE_CODE (op1) == INTEGER_CST) | |
4635 | { | |
4636 | if (tree_int_cst_lt (op1, integer_zero_node)) | |
ebd21de4 | 4637 | { |
4638 | if (complain & tf_warning) | |
4639 | warning (0, (code == LROTATE_EXPR) | |
4640 | ? G_("left rotate count is negative") | |
4641 | : G_("right rotate count is negative")); | |
4642 | } | |
5d844ba2 | 4643 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
ebd21de4 | 4644 | { |
4645 | if (complain & tf_warning) | |
4646 | warning (0, (code == LROTATE_EXPR) | |
4647 | ? G_("left rotate count >= width of type") | |
4648 | : G_("right rotate count >= width of type")); | |
4649 | } | |
471086d6 | 4650 | } |
4651 | /* Convert the shift-count to an integer, regardless of | |
4652 | size of value being shifted. */ | |
4653 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
c4698a21 | 4654 | op1 = cp_convert (integer_type_node, op1, complain); |
471086d6 | 4655 | } |
4656 | break; | |
4657 | ||
4658 | case EQ_EXPR: | |
4659 | case NE_EXPR: | |
357d8e5d | 4660 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
4661 | goto vector_compare; | |
ebd21de4 | 4662 | if ((complain & tf_warning) |
4663 | && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))) | |
074ab442 | 4664 | warning (OPT_Wfloat_equal, |
4665 | "comparing floating point with == or != is unsafe"); | |
ebd21de4 | 4666 | if ((complain & tf_warning) |
40b38ace | 4667 | && ((TREE_CODE (orig_op0) == STRING_CST |
4668 | && !integer_zerop (cp_fully_fold (op1))) | |
4669 | || (TREE_CODE (orig_op1) == STRING_CST | |
4670 | && !integer_zerop (cp_fully_fold (op0))))) | |
4671 | warning (OPT_Waddress, "comparison with string literal results " | |
67cf9b55 | 4672 | "in unspecified behavior"); |
a0785646 | 4673 | |
9031d10b | 4674 | build_type = boolean_type_node; |
c4a8ac95 | 4675 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
b3353c23 | 4676 | || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE) |
c4a8ac95 | 4677 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
b3353c23 | 4678 | || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)) |
471086d6 | 4679 | short_compare = 1; |
19222566 | 4680 | else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0)) |
fcfa0d6a | 4681 | && null_ptr_cst_p (orig_op1)) |
19222566 | 4682 | /* Handle, eg, (void*)0 (c++/43906), and more. */ |
4683 | || (code0 == POINTER_TYPE | |
4684 | && TYPE_PTR_P (type1) && integer_zerop (op1))) | |
6b68e71a | 4685 | { |
19222566 | 4686 | if (TYPE_PTR_P (type1)) |
4687 | result_type = composite_pointer_type (type0, type1, op0, op1, | |
4688 | CPO_COMPARISON, complain); | |
4689 | else | |
4690 | result_type = type0; | |
4691 | ||
6124217c | 4692 | if (char_type_p (TREE_TYPE (orig_op1)) |
4693 | && warning (OPT_Wpointer_compare, | |
4694 | "comparison between pointer and zero character " | |
4695 | "constant")) | |
4696 | inform (input_location, | |
4697 | "did you mean to dereference the pointer?"); | |
9a80bc1a | 4698 | warn_for_null_address (location, op0, complain); |
6b68e71a | 4699 | } |
19222566 | 4700 | else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1)) |
fcfa0d6a | 4701 | && null_ptr_cst_p (orig_op0)) |
19222566 | 4702 | /* Handle, eg, (void*)0 (c++/43906), and more. */ |
4703 | || (code1 == POINTER_TYPE | |
4704 | && TYPE_PTR_P (type0) && integer_zerop (op0))) | |
6b68e71a | 4705 | { |
19222566 | 4706 | if (TYPE_PTR_P (type0)) |
4707 | result_type = composite_pointer_type (type0, type1, op0, op1, | |
4708 | CPO_COMPARISON, complain); | |
4709 | else | |
4710 | result_type = type1; | |
4711 | ||
6124217c | 4712 | if (char_type_p (TREE_TYPE (orig_op0)) |
4713 | && warning (OPT_Wpointer_compare, | |
4714 | "comparison between pointer and zero character " | |
4715 | "constant")) | |
4716 | inform (input_location, | |
4717 | "did you mean to dereference the pointer?"); | |
9a80bc1a | 4718 | warn_for_null_address (location, op1, complain); |
6b68e71a | 4719 | } |
19222566 | 4720 | else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
4721 | || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1))) | |
4722 | result_type = composite_pointer_type (type0, type1, op0, op1, | |
4723 | CPO_COMPARISON, complain); | |
fcfa0d6a | 4724 | else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)) |
6fe11077 | 4725 | /* One of the operands must be of nullptr_t type. */ |
4726 | result_type = TREE_TYPE (nullptr_node); | |
471086d6 | 4727 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
4728 | { | |
bb09dca5 | 4729 | result_type = type0; |
ebd21de4 | 4730 | if (complain & tf_error) |
2b9e3597 | 4731 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
ebd21de4 | 4732 | else |
4733 | return error_mark_node; | |
471086d6 | 4734 | } |
4735 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
4736 | { | |
bb09dca5 | 4737 | result_type = type1; |
ebd21de4 | 4738 | if (complain & tf_error) |
2b9e3597 | 4739 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
ebd21de4 | 4740 | else |
4741 | return error_mark_node; | |
471086d6 | 4742 | } |
fcfa0d6a | 4743 | else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1)) |
471086d6 | 4744 | { |
894342c1 | 4745 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION |
4746 | == ptrmemfunc_vbit_in_delta) | |
4747 | { | |
c31bd1ae | 4748 | tree pfn0, delta0, e1, e2; |
4749 | ||
4750 | if (TREE_SIDE_EFFECTS (op0)) | |
4751 | op0 = save_expr (op0); | |
4752 | ||
4753 | pfn0 = pfn_from_ptrmemfunc (op0); | |
4754 | delta0 = delta_from_ptrmemfunc (op0); | |
4755 | e1 = cp_build_binary_op (location, | |
4756 | EQ_EXPR, | |
4757 | pfn0, | |
4758 | build_zero_cst (TREE_TYPE (pfn0)), | |
4759 | complain); | |
4760 | e2 = cp_build_binary_op (location, | |
4761 | BIT_AND_EXPR, | |
4762 | delta0, | |
4763 | integer_one_node, | |
4764 | complain); | |
49858f84 | 4765 | |
4766 | if (complain & tf_warning) | |
4767 | maybe_warn_zero_as_null_pointer_constant (op1, input_location); | |
7c47b69c | 4768 | |
8e70fb09 | 4769 | e2 = cp_build_binary_op (location, |
4770 | EQ_EXPR, e2, integer_zero_node, | |
ebd21de4 | 4771 | complain); |
8e70fb09 | 4772 | op0 = cp_build_binary_op (location, |
4773 | TRUTH_ANDIF_EXPR, e1, e2, | |
ebd21de4 | 4774 | complain); |
d2c63826 | 4775 | op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain); |
894342c1 | 4776 | } |
4777 | else | |
4778 | { | |
4779 | op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier); | |
c4698a21 | 4780 | op1 = cp_convert (TREE_TYPE (op0), op1, complain); |
894342c1 | 4781 | } |
bb09dca5 | 4782 | result_type = TREE_TYPE (op0); |
471086d6 | 4783 | } |
fcfa0d6a | 4784 | else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0)) |
8e70fb09 | 4785 | return cp_build_binary_op (location, code, op1, op0, complain); |
6a57f391 | 4786 | else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)) |
471086d6 | 4787 | { |
6a57f391 | 4788 | tree type; |
63a6a423 | 4789 | /* E will be the final comparison. */ |
4790 | tree e; | |
4791 | /* E1 and E2 are for scratch. */ | |
4792 | tree e1; | |
4793 | tree e2; | |
606b494c | 4794 | tree pfn0; |
4795 | tree pfn1; | |
4796 | tree delta0; | |
4797 | tree delta1; | |
63a6a423 | 4798 | |
2f182218 | 4799 | type = composite_pointer_type (type0, type1, op0, op1, |
4800 | CPO_COMPARISON, complain); | |
6a57f391 | 4801 | |
4802 | if (!same_type_p (TREE_TYPE (op0), type)) | |
c4698a21 | 4803 | op0 = cp_convert_and_check (type, op0, complain); |
6a57f391 | 4804 | if (!same_type_p (TREE_TYPE (op1), type)) |
c4698a21 | 4805 | op1 = cp_convert_and_check (type, op1, complain); |
6a57f391 | 4806 | |
4807 | if (op0 == error_mark_node || op1 == error_mark_node) | |
4808 | return error_mark_node; | |
4809 | ||
815b1d75 | 4810 | if (TREE_SIDE_EFFECTS (op0)) |
4811 | op0 = save_expr (op0); | |
4812 | if (TREE_SIDE_EFFECTS (op1)) | |
4813 | op1 = save_expr (op1); | |
4814 | ||
606b494c | 4815 | pfn0 = pfn_from_ptrmemfunc (op0); |
d2c63826 | 4816 | pfn0 = cp_fully_fold (pfn0); |
617a6dfb | 4817 | /* Avoid -Waddress warnings (c++/64877). */ |
4818 | if (TREE_CODE (pfn0) == ADDR_EXPR) | |
4819 | TREE_NO_WARNING (pfn0) = 1; | |
606b494c | 4820 | pfn1 = pfn_from_ptrmemfunc (op1); |
d2c63826 | 4821 | pfn1 = cp_fully_fold (pfn1); |
426ab4bb | 4822 | delta0 = delta_from_ptrmemfunc (op0); |
4823 | delta1 = delta_from_ptrmemfunc (op1); | |
894342c1 | 4824 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION |
4825 | == ptrmemfunc_vbit_in_delta) | |
4826 | { | |
4827 | /* We generate: | |
4828 | ||
4829 | (op0.pfn == op1.pfn | |
4830 | && ((op0.delta == op1.delta) | |
4831 | || (!op0.pfn && op0.delta & 1 == 0 | |
4832 | && op1.delta & 1 == 0)) | |
4833 | ||
4834 | The reason for the `!op0.pfn' bit is that a NULL | |
4835 | pointer-to-member is any member with a zero PFN and | |
4836 | LSB of the DELTA field is 0. */ | |
4837 | ||
8e70fb09 | 4838 | e1 = cp_build_binary_op (location, BIT_AND_EXPR, |
894342c1 | 4839 | delta0, |
ebd21de4 | 4840 | integer_one_node, |
4841 | complain); | |
8e70fb09 | 4842 | e1 = cp_build_binary_op (location, |
4843 | EQ_EXPR, e1, integer_zero_node, | |
ebd21de4 | 4844 | complain); |
8e70fb09 | 4845 | e2 = cp_build_binary_op (location, BIT_AND_EXPR, |
894342c1 | 4846 | delta1, |
ebd21de4 | 4847 | integer_one_node, |
4848 | complain); | |
8e70fb09 | 4849 | e2 = cp_build_binary_op (location, |
4850 | EQ_EXPR, e2, integer_zero_node, | |
ebd21de4 | 4851 | complain); |
8e70fb09 | 4852 | e1 = cp_build_binary_op (location, |
4853 | TRUTH_ANDIF_EXPR, e2, e1, | |
ebd21de4 | 4854 | complain); |
8e70fb09 | 4855 | e2 = cp_build_binary_op (location, EQ_EXPR, |
894342c1 | 4856 | pfn0, |
385f3f36 | 4857 | build_zero_cst (TREE_TYPE (pfn0)), |
ebd21de4 | 4858 | complain); |
8e70fb09 | 4859 | e2 = cp_build_binary_op (location, |
4860 | TRUTH_ANDIF_EXPR, e2, e1, complain); | |
4861 | e1 = cp_build_binary_op (location, | |
4862 | EQ_EXPR, delta0, delta1, complain); | |
4863 | e1 = cp_build_binary_op (location, | |
4864 | TRUTH_ORIF_EXPR, e1, e2, complain); | |
894342c1 | 4865 | } |
4866 | else | |
4867 | { | |
4868 | /* We generate: | |
4869 | ||
4870 | (op0.pfn == op1.pfn | |
4871 | && (!op0.pfn || op0.delta == op1.delta)) | |
4872 | ||
4873 | The reason for the `!op0.pfn' bit is that a NULL | |
4874 | pointer-to-member is any member with a zero PFN; the | |
4875 | DELTA field is unspecified. */ | |
4876 | ||
8e70fb09 | 4877 | e1 = cp_build_binary_op (location, |
4878 | EQ_EXPR, delta0, delta1, complain); | |
4879 | e2 = cp_build_binary_op (location, | |
4880 | EQ_EXPR, | |
894342c1 | 4881 | pfn0, |
385f3f36 | 4882 | build_zero_cst (TREE_TYPE (pfn0)), |
ebd21de4 | 4883 | complain); |
8e70fb09 | 4884 | e1 = cp_build_binary_op (location, |
4885 | TRUTH_ORIF_EXPR, e1, e2, complain); | |
894342c1 | 4886 | } |
831d52a2 | 4887 | e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1); |
8e70fb09 | 4888 | e = cp_build_binary_op (location, |
4889 | TRUTH_ANDIF_EXPR, e2, e1, complain); | |
471086d6 | 4890 | if (code == EQ_EXPR) |
63a6a423 | 4891 | return e; |
8e70fb09 | 4892 | return cp_build_binary_op (location, |
4893 | EQ_EXPR, e, integer_zero_node, complain); | |
471086d6 | 4894 | } |
2e3e31d2 | 4895 | else |
4896 | { | |
4897 | gcc_assert (!TYPE_PTRMEMFUNC_P (type0) | |
4898 | || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), | |
4899 | type1)); | |
4900 | gcc_assert (!TYPE_PTRMEMFUNC_P (type1) | |
4901 | || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), | |
4902 | type0)); | |
4903 | } | |
9031d10b | 4904 | |
471086d6 | 4905 | break; |
4906 | ||
4907 | case MAX_EXPR: | |
4908 | case MIN_EXPR: | |
4909 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) | |
4910 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
4911 | shorten = 1; | |
4912 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
30de7d91 | 4913 | result_type = composite_pointer_type (type0, type1, op0, op1, |
2f182218 | 4914 | CPO_COMPARISON, complain); |
471086d6 | 4915 | break; |
4916 | ||
4917 | case LE_EXPR: | |
4918 | case GE_EXPR: | |
4919 | case LT_EXPR: | |
4920 | case GT_EXPR: | |
3039f6d3 | 4921 | if (TREE_CODE (orig_op0) == STRING_CST |
4922 | || TREE_CODE (orig_op1) == STRING_CST) | |
ebd21de4 | 4923 | { |
4924 | if (complain & tf_warning) | |
67cf9b55 | 4925 | warning (OPT_Waddress, "comparison with string literal results " |
4926 | "in unspecified behavior"); | |
ebd21de4 | 4927 | } |
3039f6d3 | 4928 | |
357d8e5d | 4929 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
4930 | { | |
4931 | vector_compare: | |
4932 | tree intt; | |
4933 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0), | |
73437615 | 4934 | TREE_TYPE (type1)) |
4935 | && !vector_types_compatible_elements_p (type0, type1)) | |
357d8e5d | 4936 | { |
3dbad1a0 | 4937 | if (complain & tf_error) |
4938 | { | |
4939 | error_at (location, "comparing vectors with different " | |
4940 | "element types"); | |
4941 | inform (location, "operand types are %qT and %qT", | |
4942 | type0, type1); | |
4943 | } | |
357d8e5d | 4944 | return error_mark_node; |
4945 | } | |
4946 | ||
4947 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
4948 | { | |
3dbad1a0 | 4949 | if (complain & tf_error) |
4950 | { | |
4951 | error_at (location, "comparing vectors with different " | |
4952 | "number of elements"); | |
4953 | inform (location, "operand types are %qT and %qT", | |
4954 | type0, type1); | |
4955 | } | |
357d8e5d | 4956 | return error_mark_node; |
4957 | } | |
4958 | ||
fee5e491 | 4959 | /* It's not precisely specified how the usual arithmetic |
4960 | conversions apply to the vector types. Here, we use | |
4961 | the unsigned type if one of the operands is signed and | |
4962 | the other one is unsigned. */ | |
4963 | if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)) | |
4964 | { | |
4965 | if (!TYPE_UNSIGNED (type0)) | |
4966 | op0 = build1 (VIEW_CONVERT_EXPR, type1, op0); | |
4967 | else | |
4968 | op1 = build1 (VIEW_CONVERT_EXPR, type0, op1); | |
4969 | warning_at (location, OPT_Wsign_compare, "comparison between " | |
4970 | "types %qT and %qT", type0, type1); | |
4971 | } | |
4972 | ||
357d8e5d | 4973 | /* Always construct signed integer vector type. */ |
3d2b0034 | 4974 | intt = c_common_type_for_size |
4975 | (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0); | |
3dbad1a0 | 4976 | if (!intt) |
4977 | { | |
4978 | if (complain & tf_error) | |
4979 | error_at (location, "could not find an integer type " | |
4980 | "of the same size as %qT", TREE_TYPE (type0)); | |
4981 | return error_mark_node; | |
4982 | } | |
357d8e5d | 4983 | result_type = build_opaque_vector_type (intt, |
4984 | TYPE_VECTOR_SUBPARTS (type0)); | |
4985 | converted = 1; | |
1f137e6d | 4986 | return build_vec_cmp (resultcode, result_type, op0, op1); |
357d8e5d | 4987 | } |
bea7d742 | 4988 | build_type = boolean_type_node; |
1e97d78a | 4989 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
4990 | || code0 == ENUMERAL_TYPE) | |
4991 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
4992 | || code1 == ENUMERAL_TYPE)) | |
471086d6 | 4993 | short_compare = 1; |
4994 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
30de7d91 | 4995 | result_type = composite_pointer_type (type0, type1, op0, op1, |
2f182218 | 4996 | CPO_COMPARISON, complain); |
fcfa0d6a | 4997 | else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1)) |
3b0d4f6d | 4998 | { |
4999 | result_type = type0; | |
5000 | if (extra_warnings && (complain & tf_warning)) | |
5001 | warning (OPT_Wextra, | |
5002 | "ordered comparison of pointer with integer zero"); | |
5003 | } | |
fcfa0d6a | 5004 | else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0)) |
3b0d4f6d | 5005 | { |
5006 | result_type = type1; | |
5007 | if (extra_warnings && (complain & tf_warning)) | |
5008 | warning (OPT_Wextra, | |
5009 | "ordered comparison of pointer with integer zero"); | |
5010 | } | |
fcfa0d6a | 5011 | else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)) |
6fe11077 | 5012 | /* One of the operands must be of nullptr_t type. */ |
5013 | result_type = TREE_TYPE (nullptr_node); | |
471086d6 | 5014 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
5015 | { | |
bb09dca5 | 5016 | result_type = type0; |
ebd21de4 | 5017 | if (complain & tf_error) |
2b9e3597 | 5018 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
ebd21de4 | 5019 | else |
5020 | return error_mark_node; | |
471086d6 | 5021 | } |
5022 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
5023 | { | |
bb09dca5 | 5024 | result_type = type1; |
ebd21de4 | 5025 | if (complain & tf_error) |
2b9e3597 | 5026 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
ebd21de4 | 5027 | else |
5028 | return error_mark_node; | |
471086d6 | 5029 | } |
eabe2d94 | 5030 | |
5031 | if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE) | |
5032 | && sanitize_flags_p (SANITIZE_POINTER_COMPARE)) | |
5033 | { | |
5034 | op0 = save_expr (op0); | |
5035 | op1 = save_expr (op1); | |
5036 | ||
5037 | tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE); | |
5038 | instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1); | |
5039 | } | |
5040 | ||
471086d6 | 5041 | break; |
d913511e | 5042 | |
a4110d9a | 5043 | case UNORDERED_EXPR: |
5044 | case ORDERED_EXPR: | |
5045 | case UNLT_EXPR: | |
5046 | case UNLE_EXPR: | |
5047 | case UNGT_EXPR: | |
5048 | case UNGE_EXPR: | |
5049 | case UNEQ_EXPR: | |
a4110d9a | 5050 | build_type = integer_type_node; |
5051 | if (code0 != REAL_TYPE || code1 != REAL_TYPE) | |
5052 | { | |
ebd21de4 | 5053 | if (complain & tf_error) |
5054 | error ("unordered comparison on non-floating point argument"); | |
a4110d9a | 5055 | return error_mark_node; |
5056 | } | |
5057 | common = 1; | |
5058 | break; | |
5059 | ||
d913511e | 5060 | default: |
5061 | break; | |
471086d6 | 5062 | } |
5063 | ||
b3353c23 | 5064 | if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
5065 | || code0 == ENUMERAL_TYPE) | |
9031d10b | 5066 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
b3353c23 | 5067 | || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))) |
955f5493 | 5068 | arithmetic_types_p = 1; |
5069 | else | |
5070 | { | |
5071 | arithmetic_types_p = 0; | |
5072 | /* Vector arithmetic is only allowed when both sides are vectors. */ | |
5073 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) | |
5074 | { | |
979bdcfe | 5075 | if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) |
73437615 | 5076 | || !vector_types_compatible_elements_p (type0, type1)) |
979bdcfe | 5077 | { |
19b271a9 | 5078 | if (complain & tf_error) |
c0bf500c | 5079 | { |
5080 | /* "location" already embeds the locations of the | |
5081 | operands, so we don't need to add them separately | |
5082 | to richloc. */ | |
5083 | rich_location richloc (line_table, location); | |
5084 | binary_op_error (&richloc, code, type0, type1); | |
5085 | } | |
979bdcfe | 5086 | return error_mark_node; |
5087 | } | |
955f5493 | 5088 | arithmetic_types_p = 1; |
5089 | } | |
5090 | } | |
5d7ed6c7 | 5091 | /* Determine the RESULT_TYPE, if it is not already known. */ |
5092 | if (!result_type | |
9031d10b | 5093 | && arithmetic_types_p |
5d7ed6c7 | 5094 | && (shorten || common || short_compare)) |
5ba33bf4 | 5095 | { |
5096 | result_type = cp_common_type (type0, type1); | |
19b271a9 | 5097 | if (complain & tf_warning) |
5098 | do_warn_double_promotion (result_type, type0, type1, | |
4d1eda3a | 5099 | "implicit conversion from %qH to %qI " |
19b271a9 | 5100 | "to match other operand of binary " |
5101 | "expression", | |
5102 | location); | |
5ba33bf4 | 5103 | } |
5d7ed6c7 | 5104 | |
5105 | if (!result_type) | |
471086d6 | 5106 | { |
ebd21de4 | 5107 | if (complain & tf_error) |
c0bf500c | 5108 | error_at (location, |
5109 | "invalid operands of types %qT and %qT to binary %qO", | |
5110 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code); | |
5d7ed6c7 | 5111 | return error_mark_node; |
5112 | } | |
c4a8ac95 | 5113 | |
5d7ed6c7 | 5114 | /* If we're in a template, the only thing we need to know is the |
5115 | RESULT_TYPE. */ | |
5116 | if (processing_template_decl) | |
0de36bdb | 5117 | { |
5118 | /* Since the middle-end checks the type when doing a build2, we | |
5119 | need to build the tree in pieces. This built tree will never | |
5120 | get out of the front-end as we replace it when instantiating | |
5121 | the template. */ | |
5122 | tree tmp = build2 (resultcode, | |
5123 | build_type ? build_type : result_type, | |
5124 | NULL_TREE, op1); | |
5125 | TREE_OPERAND (tmp, 0) = op0; | |
5126 | return tmp; | |
5127 | } | |
5d7ed6c7 | 5128 | |
69450474 | 5129 | /* Remember the original type; RESULT_TYPE might be changed later on |
5130 | by shorten_binary_op. */ | |
5131 | tree orig_type = result_type; | |
5132 | ||
5d7ed6c7 | 5133 | if (arithmetic_types_p) |
5134 | { | |
64ec1ad6 | 5135 | bool first_complex = (code0 == COMPLEX_TYPE); |
5136 | bool second_complex = (code1 == COMPLEX_TYPE); | |
5137 | int none_complex = (!first_complex && !second_complex); | |
5138 | ||
5139 | /* Adapted from patch for c/24581. */ | |
5140 | if (first_complex != second_complex | |
5141 | && (code == PLUS_EXPR | |
5142 | || code == MINUS_EXPR | |
5143 | || code == MULT_EXPR | |
5144 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
5145 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
5146 | && flag_signed_zeros) | |
5147 | { | |
5148 | /* An operation on mixed real/complex operands must be | |
5149 | handled specially, but the language-independent code can | |
5150 | more easily optimize the plain complex arithmetic if | |
5151 | -fno-signed-zeros. */ | |
5152 | tree real_type = TREE_TYPE (result_type); | |
5153 | tree real, imag; | |
5154 | if (first_complex) | |
5155 | { | |
5156 | if (TREE_TYPE (op0) != result_type) | |
c4698a21 | 5157 | op0 = cp_convert_and_check (result_type, op0, complain); |
64ec1ad6 | 5158 | if (TREE_TYPE (op1) != real_type) |
c4698a21 | 5159 | op1 = cp_convert_and_check (real_type, op1, complain); |
64ec1ad6 | 5160 | } |
5161 | else | |
5162 | { | |
5163 | if (TREE_TYPE (op0) != real_type) | |
c4698a21 | 5164 | op0 = cp_convert_and_check (real_type, op0, complain); |
64ec1ad6 | 5165 | if (TREE_TYPE (op1) != result_type) |
c4698a21 | 5166 | op1 = cp_convert_and_check (result_type, op1, complain); |
64ec1ad6 | 5167 | } |
5168 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
5169 | return error_mark_node; | |
5170 | if (first_complex) | |
5171 | { | |
5172 | op0 = save_expr (op0); | |
b99cc6da | 5173 | real = cp_build_unary_op (REALPART_EXPR, op0, true, complain); |
5174 | imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain); | |
64ec1ad6 | 5175 | switch (code) |
5176 | { | |
5177 | case MULT_EXPR: | |
5178 | case TRUNC_DIV_EXPR: | |
caa56249 | 5179 | op1 = save_expr (op1); |
64ec1ad6 | 5180 | imag = build2 (resultcode, real_type, imag, op1); |
5181 | /* Fall through. */ | |
5182 | case PLUS_EXPR: | |
5183 | case MINUS_EXPR: | |
5184 | real = build2 (resultcode, real_type, real, op1); | |
5185 | break; | |
5186 | default: | |
5187 | gcc_unreachable(); | |
5188 | } | |
5189 | } | |
5190 | else | |
5191 | { | |
5192 | op1 = save_expr (op1); | |
b99cc6da | 5193 | real = cp_build_unary_op (REALPART_EXPR, op1, true, complain); |
5194 | imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain); | |
64ec1ad6 | 5195 | switch (code) |
5196 | { | |
5197 | case MULT_EXPR: | |
caa56249 | 5198 | op0 = save_expr (op0); |
64ec1ad6 | 5199 | imag = build2 (resultcode, real_type, op0, imag); |
5200 | /* Fall through. */ | |
5201 | case PLUS_EXPR: | |
5202 | real = build2 (resultcode, real_type, op0, real); | |
5203 | break; | |
5204 | case MINUS_EXPR: | |
5205 | real = build2 (resultcode, real_type, op0, real); | |
5206 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
5207 | break; | |
5208 | default: | |
5209 | gcc_unreachable(); | |
5210 | } | |
5211 | } | |
62b0a2a5 | 5212 | result = build2 (COMPLEX_EXPR, result_type, real, imag); |
62b0a2a5 | 5213 | return result; |
64ec1ad6 | 5214 | } |
471086d6 | 5215 | |
5216 | /* For certain operations (which identify themselves by shorten != 0) | |
5217 | if both args were extended from the same smaller type, | |
5218 | do the arithmetic in that type and then extend. | |
5219 | ||
5220 | shorten !=0 and !=1 indicates a bitwise operation. | |
5221 | For them, this optimization is safe only if | |
5222 | both args are zero-extended or both are sign-extended. | |
5223 | Otherwise, we might change the result. | |
08cc44e7 | 5224 | E.g., (short)-1 | (unsigned short)-1 is (int)-1 |
471086d6 | 5225 | but calculated in (unsigned short) it would be (unsigned short)-1. */ |
5226 | ||
c4a8ac95 | 5227 | if (shorten && none_complex) |
471086d6 | 5228 | { |
471086d6 | 5229 | final_type = result_type; |
9e46467d | 5230 | result_type = shorten_binary_op (result_type, op0, op1, |
2561cea2 | 5231 | shorten == -1); |
471086d6 | 5232 | } |
5233 | ||
471086d6 | 5234 | /* Comparison operations are shortened too but differently. |
5235 | They identify themselves by setting short_compare = 1. */ | |
5236 | ||
5237 | if (short_compare) | |
5238 | { | |
d2c63826 | 5239 | /* We call shorten_compare only for diagnostic-reason. */ |
5240 | tree xop0 = fold_simple (op0), xop1 = fold_simple (op1), | |
5241 | xresult_type = result_type; | |
471086d6 | 5242 | enum tree_code xresultcode = resultcode; |
d2c63826 | 5243 | shorten_compare (location, &xop0, &xop1, &xresult_type, |
2623625f | 5244 | &xresultcode); |
471086d6 | 5245 | } |
5246 | ||
45658a15 | 5247 | if ((short_compare || code == MIN_EXPR || code == MAX_EXPR) |
37c009d6 | 5248 | && warn_sign_compare |
5249 | /* Do not warn until the template is instantiated; we cannot | |
5250 | bound the ranges of the arguments until that point. */ | |
13869a99 | 5251 | && !processing_template_decl |
6dd536b9 | 5252 | && (complain & tf_warning) |
3637bb87 | 5253 | && c_inhibit_evaluation_warnings == 0 |
5254 | /* Even unsigned enum types promote to signed int. We don't | |
5255 | want to issue -Wsign-compare warnings for this case. */ | |
5256 | && !enum_cast_to_int (orig_op0) | |
5257 | && !enum_cast_to_int (orig_op1)) | |
471086d6 | 5258 | { |
121296ee | 5259 | tree oop0 = maybe_constant_value (orig_op0); |
5260 | tree oop1 = maybe_constant_value (orig_op1); | |
5261 | ||
5262 | if (TREE_CODE (oop0) != INTEGER_CST) | |
d2c63826 | 5263 | oop0 = cp_fully_fold (orig_op0); |
121296ee | 5264 | if (TREE_CODE (oop1) != INTEGER_CST) |
d2c63826 | 5265 | oop1 = cp_fully_fold (orig_op1); |
121296ee | 5266 | warn_for_sign_compare (location, oop0, oop1, op0, op1, |
8e70fb09 | 5267 | result_type, resultcode); |
471086d6 | 5268 | } |
5269 | } | |
5270 | ||
a07d3cd8 | 5271 | /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE. |
5272 | Then the expression will be built. | |
5273 | It will be given type FINAL_TYPE if that is nonzero; | |
5274 | otherwise, it will be given type RESULT_TYPE. */ | |
471086d6 | 5275 | if (! converted) |
5276 | { | |
5277 | if (TREE_TYPE (op0) != result_type) | |
c4698a21 | 5278 | op0 = cp_convert_and_check (result_type, op0, complain); |
471086d6 | 5279 | if (TREE_TYPE (op1) != result_type) |
c4698a21 | 5280 | op1 = cp_convert_and_check (result_type, op1, complain); |
95b2ac55 | 5281 | |
5282 | if (op0 == error_mark_node || op1 == error_mark_node) | |
5283 | return error_mark_node; | |
471086d6 | 5284 | } |
5285 | ||
bea7d742 | 5286 | if (build_type == NULL_TREE) |
5287 | build_type = result_type; | |
5288 | ||
9917317a | 5289 | if (sanitize_flags_p ((SANITIZE_SHIFT |
5290 | | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE)) | |
dadc219c | 5291 | && current_function_decl != NULL_TREE |
9e46467d | 5292 | && !processing_template_decl |
9e46467d | 5293 | && (doing_div_or_mod || doing_shift)) |
5294 | { | |
5295 | /* OP0 and/or OP1 might have side-effects. */ | |
5296 | op0 = cp_save_expr (op0); | |
5297 | op1 = cp_save_expr (op1); | |
21131a05 | 5298 | op0 = fold_non_dependent_expr (op0); |
5299 | op1 = fold_non_dependent_expr (op1); | |
9917317a | 5300 | if (doing_div_or_mod |
5301 | && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE)) | |
9e46467d | 5302 | { |
5303 | /* For diagnostics we want to use the promoted types without | |
5304 | shorten_binary_op. So convert the arguments to the | |
5305 | original result_type. */ | |
5306 | tree cop0 = op0; | |
5307 | tree cop1 = op1; | |
69450474 | 5308 | if (TREE_TYPE (cop0) != orig_type) |
5309 | cop0 = cp_convert (orig_type, op0, complain); | |
5310 | if (TREE_TYPE (cop1) != orig_type) | |
5311 | cop1 = cp_convert (orig_type, op1, complain); | |
9e46467d | 5312 | instrument_expr = ubsan_instrument_division (location, cop0, cop1); |
5313 | } | |
9917317a | 5314 | else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT)) |
9e46467d | 5315 | instrument_expr = ubsan_instrument_shift (location, code, op0, op1); |
5316 | } | |
5317 | ||
3d27a0fa | 5318 | result = build2_loc (location, resultcode, build_type, op0, op1); |
5d7ed6c7 | 5319 | if (final_type != 0) |
c4698a21 | 5320 | result = cp_convert (final_type, result, complain); |
baab3ea2 | 5321 | |
1b309377 | 5322 | if (instrument_expr != NULL) |
d2c63826 | 5323 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
5324 | instrument_expr, result); | |
5325 | ||
5326 | if (!processing_template_decl) | |
5327 | { | |
5328 | op0 = cp_fully_fold (op0); | |
5329 | /* Only consider the second argument if the first isn't overflowed. */ | |
5330 | if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0)) | |
5331 | return result; | |
5332 | op1 = cp_fully_fold (op1); | |
5333 | if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1)) | |
5334 | return result; | |
5335 | } | |
5336 | else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1) | |
5337 | || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1)) | |
5338 | return result; | |
5339 | ||
5340 | result_ovl = fold_build2 (resultcode, build_type, op0, op1); | |
5341 | if (TREE_OVERFLOW_P (result_ovl)) | |
5342 | overflow_warning (location, result_ovl); | |
9e46467d | 5343 | |
5d7ed6c7 | 5344 | return result; |
471086d6 | 5345 | } |
68ea4406 | 5346 | |
5347 | /* Build a VEC_PERM_EXPR. | |
5348 | This is a simple wrapper for c_build_vec_perm_expr. */ | |
5349 | tree | |
5350 | build_x_vec_perm_expr (location_t loc, | |
5351 | tree arg0, tree arg1, tree arg2, | |
5352 | tsubst_flags_t complain) | |
5353 | { | |
5a5f1b48 | 5354 | tree orig_arg0 = arg0; |
5355 | tree orig_arg1 = arg1; | |
5356 | tree orig_arg2 = arg2; | |
5357 | if (processing_template_decl) | |
5358 | { | |
5359 | if (type_dependent_expression_p (arg0) | |
68ea4406 | 5360 | || type_dependent_expression_p (arg1) |
5a5f1b48 | 5361 | || type_dependent_expression_p (arg2)) |
5362 | return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2); | |
5363 | arg0 = build_non_dependent_expr (arg0); | |
5364 | if (arg1) | |
5365 | arg1 = build_non_dependent_expr (arg1); | |
5366 | arg2 = build_non_dependent_expr (arg2); | |
5367 | } | |
5368 | tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error); | |
5369 | if (processing_template_decl && exp != error_mark_node) | |
5370 | return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0, | |
5371 | orig_arg1, orig_arg2); | |
5372 | return exp; | |
68ea4406 | 5373 | } |
471086d6 | 5374 | \f |
5375 | /* Return a tree for the sum or difference (RESULTCODE says which) | |
5376 | of pointer PTROP and integer INTOP. */ | |
5377 | ||
5378 | static tree | |
22a3f7bd | 5379 | cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop, |
5380 | tree intop, tsubst_flags_t complain) | |
471086d6 | 5381 | { |
d2cc46cb | 5382 | tree res_type = TREE_TYPE (ptrop); |
5383 | ||
5384 | /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type) | |
5385 | in certain circumstance (when it's valid to do so). So we need | |
5386 | to make sure it's complete. We don't need to check here, if we | |
5387 | can actually complete it at all, as those checks will be done in | |
5388 | pointer_int_sum() anyway. */ | |
5389 | complete_type (TREE_TYPE (res_type)); | |
b3a558fd | 5390 | |
22a3f7bd | 5391 | return pointer_int_sum (loc, resultcode, ptrop, |
d2c63826 | 5392 | intop, complain & tf_warning_or_error); |
471086d6 | 5393 | } |
5394 | ||
5395 | /* Return a tree for the difference of pointers OP0 and OP1. | |
eabe2d94 | 5396 | The resulting tree has type int. If POINTER_SUBTRACT sanitization is |
5397 | enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */ | |
471086d6 | 5398 | |
5399 | static tree | |
22a3f7bd | 5400 | pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype, |
eabe2d94 | 5401 | tsubst_flags_t complain, tree *instrument_expr) |
471086d6 | 5402 | { |
57e83b58 | 5403 | tree result, inttype; |
471086d6 | 5404 | tree restype = ptrdiff_type_node; |
d509e46d | 5405 | tree target_type = TREE_TYPE (ptrtype); |
471086d6 | 5406 | |
aeef2be5 | 5407 | if (!complete_type_or_else (target_type, NULL_TREE)) |
b3a558fd | 5408 | return error_mark_node; |
5409 | ||
c21c015b | 5410 | if (VOID_TYPE_P (target_type)) |
c4698a21 | 5411 | { |
5412 | if (complain & tf_error) | |
22a3f7bd | 5413 | permerror (loc, "ISO C++ forbids using pointer of " |
c4698a21 | 5414 | "type %<void *%> in subtraction"); |
5415 | else | |
5416 | return error_mark_node; | |
5417 | } | |
8864917d | 5418 | if (TREE_CODE (target_type) == FUNCTION_TYPE) |
c4698a21 | 5419 | { |
5420 | if (complain & tf_error) | |
22a3f7bd | 5421 | permerror (loc, "ISO C++ forbids using pointer to " |
c4698a21 | 5422 | "a function in subtraction"); |
5423 | else | |
5424 | return error_mark_node; | |
5425 | } | |
8864917d | 5426 | if (TREE_CODE (target_type) == METHOD_TYPE) |
c4698a21 | 5427 | { |
5428 | if (complain & tf_error) | |
22a3f7bd | 5429 | permerror (loc, "ISO C++ forbids using pointer to " |
c4698a21 | 5430 | "a method in subtraction"); |
5431 | else | |
5432 | return error_mark_node; | |
5433 | } | |
471086d6 | 5434 | |
57e83b58 | 5435 | /* Determine integer type result of the subtraction. This will usually |
5436 | be the same as the result type (ptrdiff_t), but may need to be a wider | |
5437 | type if pointers for the address space are wider than ptrdiff_t. */ | |
5438 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) | |
5439 | inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); | |
5440 | else | |
5441 | inttype = restype; | |
5442 | ||
eabe2d94 | 5443 | if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT)) |
5444 | { | |
5445 | op0 = save_expr (op0); | |
5446 | op1 = save_expr (op1); | |
5447 | ||
5448 | tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT); | |
5449 | *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1); | |
5450 | } | |
5451 | ||
57e83b58 | 5452 | /* First do the subtraction, then build the divide operator |
5453 | and only convert at the very end. | |
5454 | Do not do default conversions in case restype is a short type. */ | |
5455 | ||
5456 | /* POINTER_DIFF_EXPR requires a signed integer type of the same size as | |
5457 | pointers. If some platform cannot provide that, or has a larger | |
5458 | ptrdiff_type to support differences larger than half the address | |
5459 | space, cast the pointers to some larger integer type and do the | |
5460 | computations in that type. */ | |
5461 | if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0))) | |
5462 | op0 = cp_build_binary_op (loc, | |
5463 | MINUS_EXPR, | |
5464 | cp_convert (inttype, op0, complain), | |
5465 | cp_convert (inttype, op1, complain), | |
5466 | complain); | |
5467 | else | |
5468 | op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1); | |
471086d6 | 5469 | |
5470 | /* This generates an error if op1 is a pointer to an incomplete type. */ | |
4b72716d | 5471 | if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1)))) |
c4698a21 | 5472 | { |
5473 | if (complain & tf_error) | |
22a3f7bd | 5474 | error_at (loc, "invalid use of a pointer to an incomplete type in " |
5475 | "pointer arithmetic"); | |
c4698a21 | 5476 | else |
5477 | return error_mark_node; | |
5478 | } | |
471086d6 | 5479 | |
46da3601 | 5480 | if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1))) |
5481 | { | |
5482 | if (complain & tf_error) | |
22a3f7bd | 5483 | error_at (loc, "arithmetic on pointer to an empty aggregate"); |
46da3601 | 5484 | else |
5485 | return error_mark_node; | |
5486 | } | |
5487 | ||
9031d10b | 5488 | op1 = (TYPE_PTROB_P (ptrtype) |
22a3f7bd | 5489 | ? size_in_bytes_loc (loc, target_type) |
1bc16cab | 5490 | : integer_one_node); |
471086d6 | 5491 | |
5492 | /* Do the division. */ | |
5493 | ||
57e83b58 | 5494 | result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0, |
5495 | cp_convert (inttype, op1, complain)); | |
5496 | return cp_convert (restype, result, complain); | |
471086d6 | 5497 | } |
5498 | \f | |
471086d6 | 5499 | /* Construct and perhaps optimize a tree representation |
5500 | for a unary operation. CODE, a tree_code, specifies the operation | |
5501 | and XARG is the operand. */ | |
5502 | ||
5503 | tree | |
3d27a0fa | 5504 | build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg, |
ef0b0c72 | 5505 | tsubst_flags_t complain) |
471086d6 | 5506 | { |
13795292 | 5507 | tree orig_expr = xarg; |
30efa7ed | 5508 | tree exp; |
5509 | int ptrmem = 0; | |
d175f019 | 5510 | tree overload = NULL_TREE; |
9031d10b | 5511 | |
3cc0b4b9 | 5512 | if (processing_template_decl) |
13795292 | 5513 | { |
5514 | if (type_dependent_expression_p (xarg)) | |
3d27a0fa | 5515 | return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE); |
c3c0139b | 5516 | |
13795292 | 5517 | xarg = build_non_dependent_expr (xarg); |
5518 | } | |
5519 | ||
5520 | exp = NULL_TREE; | |
e857e9c7 | 5521 | |
550bbfa8 | 5522 | /* [expr.unary.op] says: |
5523 | ||
5524 | The address of an object of incomplete type can be taken. | |
5525 | ||
5526 | (And is just the ordinary address operator, not an overloaded | |
5527 | "operator &".) However, if the type is a template | |
5528 | specialization, we must complete the type at this point so that | |
5529 | an overloaded "operator &" will be available if required. */ | |
c25194fd | 5530 | if (code == ADDR_EXPR |
b1cfe2be | 5531 | && TREE_CODE (xarg) != TEMPLATE_ID_EXPR |
550bbfa8 | 5532 | && ((CLASS_TYPE_P (TREE_TYPE (xarg)) |
5533 | && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg)))) | |
c25194fd | 5534 | || (TREE_CODE (xarg) == OFFSET_REF))) |
331bc0ad | 5535 | /* Don't look for a function. */; |
c25194fd | 5536 | else |
ef0b0c72 | 5537 | exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE, |
d175f019 | 5538 | NULL_TREE, &overload, complain); |
5539 | ||
13795292 | 5540 | if (!exp && code == ADDR_EXPR) |
1e28ccf1 | 5541 | { |
eac53a7c | 5542 | if (is_overloaded_fn (xarg)) |
5543 | { | |
5544 | tree fn = get_first_fn (xarg); | |
5545 | if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn)) | |
5546 | { | |
19b271a9 | 5547 | if (complain & tf_error) |
5548 | error (DECL_CONSTRUCTOR_P (fn) | |
c5e55d1c | 5549 | ? G_("taking address of constructor %qD") |
5550 | : G_("taking address of destructor %qD"), | |
5551 | fn); | |
eac53a7c | 5552 | return error_mark_node; |
5553 | } | |
5554 | } | |
5555 | ||
5556 | /* A pointer to member-function can be formed only by saying | |
5557 | &X::mf. */ | |
90cd798a | 5558 | if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE |
5559 | && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg))) | |
5560 | { | |
1d620b2e | 5561 | if (TREE_CODE (xarg) != OFFSET_REF |
5562 | || !TYPE_P (TREE_OPERAND (xarg, 0))) | |
90cd798a | 5563 | { |
19b271a9 | 5564 | if (complain & tf_error) |
5565 | { | |
5566 | error ("invalid use of %qE to form a " | |
3d27a0fa | 5567 | "pointer-to-member-function", xarg.get_value ()); |
19b271a9 | 5568 | if (TREE_CODE (xarg) != OFFSET_REF) |
5569 | inform (input_location, " a qualified-id is required"); | |
5570 | } | |
90cd798a | 5571 | return error_mark_node; |
5572 | } | |
5573 | else | |
5574 | { | |
19b271a9 | 5575 | if (complain & tf_error) |
5576 | error ("parentheses around %qE cannot be used to form a" | |
5577 | " pointer-to-member-function", | |
3d27a0fa | 5578 | xarg.get_value ()); |
19b271a9 | 5579 | else |
5580 | return error_mark_node; | |
90cd798a | 5581 | PTRMEM_OK_P (xarg) = 1; |
5582 | } | |
5583 | } | |
9031d10b | 5584 | |
30efa7ed | 5585 | if (TREE_CODE (xarg) == OFFSET_REF) |
653e5405 | 5586 | { |
5587 | ptrmem = PTRMEM_OK_P (xarg); | |
9031d10b | 5588 | |
653e5405 | 5589 | if (!ptrmem && !flag_ms_extensions |
5590 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE) | |
5a9496a4 | 5591 | { |
5592 | /* A single non-static member, make sure we don't allow a | |
653e5405 | 5593 | pointer-to-member. */ |
831d52a2 | 5594 | xarg = build2 (OFFSET_REF, TREE_TYPE (xarg), |
5595 | TREE_OPERAND (xarg, 0), | |
c9d02844 | 5596 | ovl_make (TREE_OPERAND (xarg, 1))); |
5a9496a4 | 5597 | PTRMEM_OK_P (xarg) = ptrmem; |
9031d10b | 5598 | } |
653e5405 | 5599 | } |
d6fbd579 | 5600 | |
5601 | exp = cp_build_addr_expr_strict (xarg, complain); | |
1e28ccf1 | 5602 | } |
5603 | ||
13795292 | 5604 | if (processing_template_decl && exp != error_mark_node) |
d175f019 | 5605 | { |
5606 | if (overload != NULL_TREE) | |
5607 | return (build_min_non_dep_op_overload | |
5608 | (code, exp, overload, orig_expr, integer_zero_node)); | |
5609 | ||
5610 | exp = build_min_non_dep (code, exp, orig_expr, | |
5611 | /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE); | |
5612 | } | |
33d18491 | 5613 | if (TREE_CODE (exp) == ADDR_EXPR) |
5614 | PTRMEM_OK_P (exp) = ptrmem; | |
30efa7ed | 5615 | return exp; |
471086d6 | 5616 | } |
5617 | ||
cd45162d | 5618 | /* Construct and perhaps optimize a tree representation |
5619 | for __builtin_addressof operation. ARG specifies the operand. */ | |
5620 | ||
5621 | tree | |
5622 | cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain) | |
5623 | { | |
5624 | tree orig_expr = arg; | |
5625 | ||
5626 | if (processing_template_decl) | |
5627 | { | |
5628 | if (type_dependent_expression_p (arg)) | |
5629 | return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE); | |
5630 | ||
5631 | arg = build_non_dependent_expr (arg); | |
5632 | } | |
5633 | ||
5634 | tree exp = cp_build_addr_expr_strict (arg, complain); | |
5635 | ||
5636 | if (processing_template_decl && exp != error_mark_node) | |
5637 | exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE); | |
5638 | return exp; | |
5639 | } | |
5640 | ||
aff9e656 | 5641 | /* Like c_common_truthvalue_conversion, but handle pointer-to-member |
5642 | constants, where a null value is represented by an INTEGER_CST of | |
5643 | -1. */ | |
2e0e6579 | 5644 | |
5645 | tree | |
f82b06e0 | 5646 | cp_truthvalue_conversion (tree expr) |
2e0e6579 | 5647 | { |
5648 | tree type = TREE_TYPE (expr); | |
0c0f63cb | 5649 | if (TYPE_PTR_OR_PTRMEM_P (type) |
b0d3b0d8 | 5650 | /* Avoid ICE on invalid use of non-static member function. */ |
5651 | || TREE_CODE (expr) == FUNCTION_DECL) | |
623ee358 | 5652 | return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true); |
2e0e6579 | 5653 | else |
8e70fb09 | 5654 | return c_common_truthvalue_conversion (input_location, expr); |
2e0e6579 | 5655 | } |
5656 | ||
5657 | /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */ | |
9031d10b | 5658 | |
bb0726a1 | 5659 | tree |
f82b06e0 | 5660 | condition_conversion (tree expr) |
bb0726a1 | 5661 | { |
e857e9c7 | 5662 | tree t; |
672871ce | 5663 | /* Anything that might happen in a template should go through |
5664 | maybe_convert_cond. */ | |
5665 | gcc_assert (!processing_template_decl); | |
97e2368b | 5666 | t = perform_implicit_conversion_flags (boolean_type_node, expr, |
5667 | tf_warning_or_error, LOOKUP_NORMAL); | |
acbc760a | 5668 | t = fold_build_cleanup_point_expr (boolean_type_node, t); |
1a3f833b | 5669 | return t; |
bb0726a1 | 5670 | } |
9031d10b | 5671 | |
dddfd70e | 5672 | /* Returns the address of T. This function will fold away |
5673 | ADDR_EXPR of INDIRECT_REF. */ | |
8999978b | 5674 | |
5675 | tree | |
5676 | build_address (tree t) | |
5677 | { | |
8999978b | 5678 | if (error_operand_p (t) || !cxx_mark_addressable (t)) |
5679 | return error_mark_node; | |
1eb418cc | 5680 | gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR); |
dddfd70e | 5681 | t = build_fold_addr_expr (t); |
5682 | if (TREE_CODE (t) != ADDR_EXPR) | |
5683 | t = rvalue (t); | |
5684 | return t; | |
8999978b | 5685 | } |
5686 | ||
5687 | /* Return a NOP_EXPR converting EXPR to TYPE. */ | |
5688 | ||
5689 | tree | |
5690 | build_nop (tree type, tree expr) | |
5691 | { | |
8999978b | 5692 | if (type == error_mark_node || error_operand_p (expr)) |
5693 | return expr; | |
3d27a0fa | 5694 | return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr); |
8999978b | 5695 | } |
5696 | ||
d6fbd579 | 5697 | /* Take the address of ARG, whatever that means under C++ semantics. |
5698 | If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues | |
5699 | and class rvalues as well. | |
5700 | ||
5701 | Nothing should call this function directly; instead, callers should use | |
5702 | cp_build_addr_expr or cp_build_addr_expr_strict. */ | |
5703 | ||
5704 | static tree | |
5705 | cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain) | |
5706 | { | |
5707 | tree argtype; | |
5708 | tree val; | |
5709 | ||
5710 | if (!arg || error_operand_p (arg)) | |
5711 | return error_mark_node; | |
5712 | ||
5713 | arg = mark_lvalue_use (arg); | |
c017458d | 5714 | if (error_operand_p (arg)) |
5715 | return error_mark_node; | |
5716 | ||
d6fbd579 | 5717 | argtype = lvalue_type (arg); |
5718 | ||
991449b2 | 5719 | gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg))); |
d6fbd579 | 5720 | |
5721 | if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg) | |
bc9c1170 | 5722 | && !really_overloaded_fn (arg)) |
d6fbd579 | 5723 | { |
5724 | /* They're trying to take the address of a unique non-static | |
5725 | member function. This is ill-formed (except in MS-land), | |
5726 | but let's try to DTRT. | |
5727 | Note: We only handle unique functions here because we don't | |
5728 | want to complain if there's a static overload; non-unique | |
5729 | cases will be handled by instantiate_type. But we need to | |
5730 | handle this case here to allow casts on the resulting PMF. | |
5731 | We could defer this in non-MS mode, but it's easier to give | |
5732 | a useful error here. */ | |
5733 | ||
5734 | /* Inside constant member functions, the `this' pointer | |
5735 | contains an extra const qualifier. TYPE_MAIN_VARIANT | |
5736 | is used here to remove this const from the diagnostics | |
5737 | and the created OFFSET_REF. */ | |
5738 | tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0))); | |
5739 | tree fn = get_first_fn (TREE_OPERAND (arg, 1)); | |
4430f688 | 5740 | if (!mark_used (fn, complain) && !(complain & tf_error)) |
5741 | return error_mark_node; | |
d6fbd579 | 5742 | |
5743 | if (! flag_ms_extensions) | |
5744 | { | |
5745 | tree name = DECL_NAME (fn); | |
5746 | if (!(complain & tf_error)) | |
5747 | return error_mark_node; | |
5748 | else if (current_class_type | |
5749 | && TREE_OPERAND (arg, 0) == current_class_ref) | |
5750 | /* An expression like &memfn. */ | |
5751 | permerror (input_location, "ISO C++ forbids taking the address of an unqualified" | |
5752 | " or parenthesized non-static member function to form" | |
5753 | " a pointer to member function. Say %<&%T::%D%>", | |
5754 | base, name); | |
5755 | else | |
5756 | permerror (input_location, "ISO C++ forbids taking the address of a bound member" | |
5757 | " function to form a pointer to member function." | |
5758 | " Say %<&%T::%D%>", | |
5759 | base, name); | |
5760 | } | |
20ce13a9 | 5761 | arg = build_offset_ref (base, fn, /*address_p=*/true, complain); |
d6fbd579 | 5762 | } |
5763 | ||
5764 | /* Uninstantiated types are all functions. Taking the | |
5765 | address of a function is a no-op, so just return the | |
5766 | argument. */ | |
5767 | if (type_unknown_p (arg)) | |
5768 | return build1 (ADDR_EXPR, unknown_type_node, arg); | |
5769 | ||
5770 | if (TREE_CODE (arg) == OFFSET_REF) | |
5771 | /* We want a pointer to member; bypass all the code for actually taking | |
5772 | the address of something. */ | |
5773 | goto offset_ref; | |
5774 | ||
5775 | /* Anything not already handled and not a true memory reference | |
5776 | is an error. */ | |
5777 | if (TREE_CODE (argtype) != FUNCTION_TYPE | |
5778 | && TREE_CODE (argtype) != METHOD_TYPE) | |
5779 | { | |
0eb0b6ca | 5780 | cp_lvalue_kind kind = lvalue_kind (arg); |
5781 | if (kind == clk_none) | |
d6fbd579 | 5782 | { |
5783 | if (complain & tf_error) | |
fdd84b77 | 5784 | lvalue_error (input_location, lv_addressof); |
d6fbd579 | 5785 | return error_mark_node; |
5786 | } | |
0eb0b6ca | 5787 | if (strict_lvalue && (kind & (clk_rvalueref|clk_class))) |
5788 | { | |
5789 | if (!(complain & tf_error)) | |
5790 | return error_mark_node; | |
5791 | if (kind & clk_class) | |
5792 | /* Make this a permerror because we used to accept it. */ | |
5793 | permerror (input_location, "taking address of temporary"); | |
5794 | else | |
5795 | error ("taking address of xvalue (rvalue reference)"); | |
5796 | } | |
d6fbd579 | 5797 | } |
5798 | ||
5799 | if (TREE_CODE (argtype) == REFERENCE_TYPE) | |
5800 | { | |
5801 | tree type = build_pointer_type (TREE_TYPE (argtype)); | |
5802 | arg = build1 (CONVERT_EXPR, type, arg); | |
5803 | return arg; | |
5804 | } | |
5805 | else if (pedantic && DECL_MAIN_P (arg)) | |
5806 | { | |
5807 | /* ARM $3.4 */ | |
5808 | /* Apparently a lot of autoconf scripts for C++ packages do this, | |
29438999 | 5809 | so only complain if -Wpedantic. */ |
d6fbd579 | 5810 | if (complain & (flag_pedantic_errors ? tf_error : tf_warning)) |
29438999 | 5811 | pedwarn (input_location, OPT_Wpedantic, |
d6fbd579 | 5812 | "ISO C++ forbids taking address of function %<::main%>"); |
5813 | else if (flag_pedantic_errors) | |
5814 | return error_mark_node; | |
5815 | } | |
5816 | ||
5817 | /* Let &* cancel out to simplify resulting code. */ | |
86ccc124 | 5818 | if (INDIRECT_REF_P (arg)) |
d6fbd579 | 5819 | { |
d6fbd579 | 5820 | arg = TREE_OPERAND (arg, 0); |
5821 | if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE) | |
5822 | { | |
5823 | tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg))); | |
5824 | arg = build1 (CONVERT_EXPR, type, arg); | |
5825 | } | |
5826 | else | |
5827 | /* Don't let this be an lvalue. */ | |
5828 | arg = rvalue (arg); | |
5829 | return arg; | |
5830 | } | |
5831 | ||
5832 | /* ??? Cope with user tricks that amount to offsetof. */ | |
5833 | if (TREE_CODE (argtype) != FUNCTION_TYPE | |
5834 | && TREE_CODE (argtype) != METHOD_TYPE | |
5835 | && argtype != unknown_type_node | |
5836 | && (val = get_base_address (arg)) | |
22bdb2aa | 5837 | && COMPLETE_TYPE_P (TREE_TYPE (val)) |
86ccc124 | 5838 | && INDIRECT_REF_P (val) |
d6fbd579 | 5839 | && TREE_CONSTANT (TREE_OPERAND (val, 0))) |
5840 | { | |
5841 | tree type = build_pointer_type (argtype); | |
7549df0d | 5842 | return fold_convert (type, fold_offsetof_1 (arg)); |
d6fbd579 | 5843 | } |
5844 | ||
5845 | /* Handle complex lvalues (when permitted) | |
5846 | by reduction to simpler cases. */ | |
5847 | val = unary_complex_lvalue (ADDR_EXPR, arg); | |
5848 | if (val != 0) | |
5849 | return val; | |
5850 | ||
5851 | switch (TREE_CODE (arg)) | |
5852 | { | |
5853 | CASE_CONVERT: | |
5854 | case FLOAT_EXPR: | |
5855 | case FIX_TRUNC_EXPR: | |
049132bc | 5856 | /* We should have handled this above in the lvalue_kind check. */ |
5857 | gcc_unreachable (); | |
d6fbd579 | 5858 | break; |
5859 | ||
5860 | case BASELINK: | |
5861 | arg = BASELINK_FUNCTIONS (arg); | |
5862 | /* Fall through. */ | |
5863 | ||
5864 | case OVERLOAD: | |
6767ca9a | 5865 | arg = OVL_FIRST (arg); |
d6fbd579 | 5866 | break; |
5867 | ||
5868 | case OFFSET_REF: | |
5869 | offset_ref: | |
5870 | /* Turn a reference to a non-static data member into a | |
5871 | pointer-to-member. */ | |
5872 | { | |
5873 | tree type; | |
5874 | tree t; | |
5875 | ||
5876 | gcc_assert (PTRMEM_OK_P (arg)); | |
5877 | ||
5878 | t = TREE_OPERAND (arg, 1); | |
5879 | if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE) | |
5880 | { | |
5881 | if (complain & tf_error) | |
5882 | error ("cannot create pointer to reference member %qD", t); | |
5883 | return error_mark_node; | |
5884 | } | |
5885 | ||
5886 | type = build_ptrmem_type (context_for_name_lookup (t), | |
5887 | TREE_TYPE (t)); | |
5888 | t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1)); | |
5889 | return t; | |
5890 | } | |
5891 | ||
5892 | default: | |
5893 | break; | |
5894 | } | |
5895 | ||
5896 | if (argtype != error_mark_node) | |
4972ed5d | 5897 | argtype = build_pointer_type (argtype); |
d6fbd579 | 5898 | |
357b9645 | 5899 | if (bitfield_p (arg)) |
5900 | { | |
5901 | if (complain & tf_error) | |
5902 | error ("attempt to take address of bit-field"); | |
5903 | return error_mark_node; | |
5904 | } | |
5905 | ||
d6fbd579 | 5906 | /* In a template, we are processing a non-dependent expression |
5907 | so we can just form an ADDR_EXPR with the correct type. */ | |
5908 | if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF) | |
5909 | { | |
5910 | val = build_address (arg); | |
5911 | if (TREE_CODE (arg) == OFFSET_REF) | |
5912 | PTRMEM_OK_P (val) = PTRMEM_OK_P (arg); | |
5913 | } | |
a00f651a | 5914 | else if (BASELINK_P (TREE_OPERAND (arg, 1))) |
d6fbd579 | 5915 | { |
5916 | tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1)); | |
5917 | ||
5918 | /* We can only get here with a single static member | |
5919 | function. */ | |
5920 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL | |
5921 | && DECL_STATIC_FUNCTION_P (fn)); | |
4430f688 | 5922 | if (!mark_used (fn, complain) && !(complain & tf_error)) |
5923 | return error_mark_node; | |
d6fbd579 | 5924 | val = build_address (fn); |
5925 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0))) | |
5926 | /* Do not lose object's side effects. */ | |
5927 | val = build2 (COMPOUND_EXPR, TREE_TYPE (val), | |
5928 | TREE_OPERAND (arg, 0), val); | |
5929 | } | |
d6fbd579 | 5930 | else |
5931 | { | |
5932 | tree object = TREE_OPERAND (arg, 0); | |
5933 | tree field = TREE_OPERAND (arg, 1); | |
5934 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
5935 | (TREE_TYPE (object), decl_type_context (field))); | |
5936 | val = build_address (arg); | |
5937 | } | |
5938 | ||
c21c015b | 5939 | if (TYPE_PTR_P (argtype) |
d6fbd579 | 5940 | && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE) |
5941 | { | |
5942 | build_ptrmemfunc_type (argtype); | |
5943 | val = build_ptrmemfunc (argtype, val, 0, | |
5944 | /*c_cast_p=*/false, | |
34f12878 | 5945 | complain); |
d6fbd579 | 5946 | } |
5947 | ||
5948 | return val; | |
5949 | } | |
5950 | ||
5951 | /* Take the address of ARG if it has one, even if it's an rvalue. */ | |
5952 | ||
5953 | tree | |
5954 | cp_build_addr_expr (tree arg, tsubst_flags_t complain) | |
5955 | { | |
5956 | return cp_build_addr_expr_1 (arg, 0, complain); | |
5957 | } | |
5958 | ||
5959 | /* Take the address of ARG, but only if it's an lvalue. */ | |
5960 | ||
aafa8c0a | 5961 | static tree |
d6fbd579 | 5962 | cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain) |
5963 | { | |
5964 | return cp_build_addr_expr_1 (arg, 1, complain); | |
5965 | } | |
5966 | ||
471086d6 | 5967 | /* C++: Must handle pointers to members. |
5968 | ||
5969 | Perhaps type instantiation should be extended to handle conversion | |
5970 | from aggregates to types we don't yet know we want? (Or are those | |
5971 | cases typically errors which should be reported?) | |
5972 | ||
b99cc6da | 5973 | NOCONVERT suppresses the default promotions (such as from short to int). */ |
96624a9e | 5974 | |
471086d6 | 5975 | tree |
b99cc6da | 5976 | cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert, |
ebd21de4 | 5977 | tsubst_flags_t complain) |
471086d6 | 5978 | { |
5979 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
cd16867a | 5980 | tree arg = xarg; |
481ce481 | 5981 | location_t location = EXPR_LOC_OR_LOC (arg, input_location); |
cd16867a | 5982 | tree argtype = 0; |
e1721763 | 5983 | const char *errstring = NULL; |
471086d6 | 5984 | tree val; |
7a979707 | 5985 | const char *invalid_op_diag; |
471086d6 | 5986 | |
001dc0e9 | 5987 | if (!arg || error_operand_p (arg)) |
471086d6 | 5988 | return error_mark_node; |
5989 | ||
7a979707 | 5990 | if ((invalid_op_diag |
5991 | = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR | |
5992 | ? CONVERT_EXPR | |
5993 | : code), | |
5994 | TREE_TYPE (xarg)))) | |
5995 | { | |
19b271a9 | 5996 | if (complain & tf_error) |
5997 | error (invalid_op_diag); | |
7a979707 | 5998 | return error_mark_node; |
5999 | } | |
6000 | ||
471086d6 | 6001 | switch (code) |
6002 | { | |
97d541d5 | 6003 | case UNARY_PLUS_EXPR: |
1e66592c | 6004 | case NEGATE_EXPR: |
064966e8 | 6005 | { |
6006 | int flags = WANT_ARITH | WANT_ENUM; | |
6007 | /* Unary plus (but not unary minus) is allowed on pointers. */ | |
97d541d5 | 6008 | if (code == UNARY_PLUS_EXPR) |
064966e8 | 6009 | flags |= WANT_POINTER; |
6010 | arg = build_expr_type_conversion (flags, arg, true); | |
67e5facb | 6011 | if (!arg) |
6012 | errstring = (code == NEGATE_EXPR | |
5c892d4a | 6013 | ? _("wrong type argument to unary minus") |
6014 | : _("wrong type argument to unary plus")); | |
67e5facb | 6015 | else |
6016 | { | |
6017 | if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg))) | |
c4698a21 | 6018 | arg = cp_perform_integral_promotions (arg, complain); |
67e5facb | 6019 | |
cd7043f9 | 6020 | /* Make sure the result is not an lvalue: a unary plus or minus |
67e5facb | 6021 | expression is always a rvalue. */ |
fcb3f76e | 6022 | arg = rvalue (arg); |
67e5facb | 6023 | } |
6024 | } | |
471086d6 | 6025 | break; |
6026 | ||
6027 | case BIT_NOT_EXPR: | |
c4a8ac95 | 6028 | if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) |
6029 | { | |
6030 | code = CONJ_EXPR; | |
6031 | if (!noconvert) | |
4405c1ad | 6032 | { |
6033 | arg = cp_default_conversion (arg, complain); | |
6034 | if (arg == error_mark_node) | |
6035 | return error_mark_node; | |
6036 | } | |
c4a8ac95 | 6037 | } |
96ec9e30 | 6038 | else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM |
64ec1ad6 | 6039 | | WANT_VECTOR_OR_COMPLEX, |
35771a9a | 6040 | arg, true))) |
5c892d4a | 6041 | errstring = _("wrong type argument to bit-complement"); |
eb7dcea7 | 6042 | else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg))) |
481ce481 | 6043 | { |
6044 | /* Warn if the expression has boolean value. */ | |
6045 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE | |
e55400dd | 6046 | && (complain & tf_warning) |
481ce481 | 6047 | && warning_at (location, OPT_Wbool_operation, |
6048 | "%<~%> on an expression of type bool")) | |
6049 | inform (location, "did you mean to use logical not (%<!%>)?"); | |
6050 | arg = cp_perform_integral_promotions (arg, complain); | |
6051 | } | |
0b16b811 | 6052 | else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg))) |
6053 | arg = mark_rvalue_use (arg); | |
471086d6 | 6054 | break; |
6055 | ||
6056 | case ABS_EXPR: | |
35771a9a | 6057 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) |
5c892d4a | 6058 | errstring = _("wrong type argument to abs"); |
471086d6 | 6059 | else if (!noconvert) |
4405c1ad | 6060 | { |
6061 | arg = cp_default_conversion (arg, complain); | |
6062 | if (arg == error_mark_node) | |
6063 | return error_mark_node; | |
6064 | } | |
471086d6 | 6065 | break; |
6066 | ||
c4a8ac95 | 6067 | case CONJ_EXPR: |
6068 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
35771a9a | 6069 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) |
5c892d4a | 6070 | errstring = _("wrong type argument to conjugation"); |
c4a8ac95 | 6071 | else if (!noconvert) |
4405c1ad | 6072 | { |
6073 | arg = cp_default_conversion (arg, complain); | |
6074 | if (arg == error_mark_node) | |
6075 | return error_mark_node; | |
6076 | } | |
c4a8ac95 | 6077 | break; |
6078 | ||
471086d6 | 6079 | case TRUTH_NOT_EXPR: |
6b9c511d | 6080 | if (VECTOR_TYPE_P (TREE_TYPE (arg))) |
5e84569c | 6081 | return cp_build_binary_op (input_location, EQ_EXPR, arg, |
6082 | build_zero_cst (TREE_TYPE (arg)), complain); | |
ebd21de4 | 6083 | arg = perform_implicit_conversion (boolean_type_node, arg, |
6084 | complain); | |
389dd41b | 6085 | val = invert_truthvalue_loc (input_location, arg); |
bb0726a1 | 6086 | if (arg != error_mark_node) |
6087 | return val; | |
5c892d4a | 6088 | errstring = _("in argument to unary !"); |
471086d6 | 6089 | break; |
6090 | ||
6091 | case NOP_EXPR: | |
6092 | break; | |
9031d10b | 6093 | |
c4a8ac95 | 6094 | case REALPART_EXPR: |
c4a8ac95 | 6095 | case IMAGPART_EXPR: |
0b5fc5d6 | 6096 | arg = build_real_imag_expr (input_location, code, arg); |
d2c63826 | 6097 | return arg; |
9031d10b | 6098 | |
471086d6 | 6099 | case PREINCREMENT_EXPR: |
6100 | case POSTINCREMENT_EXPR: | |
6101 | case PREDECREMENT_EXPR: | |
6102 | case POSTDECREMENT_EXPR: | |
6103 | /* Handle complex lvalues (when permitted) | |
6104 | by reduction to simpler cases. */ | |
6105 | ||
6106 | val = unary_complex_lvalue (code, arg); | |
6107 | if (val != 0) | |
6108 | return val; | |
6109 | ||
fbb73d9b | 6110 | arg = mark_lvalue_use (arg); |
6111 | ||
c4a8ac95 | 6112 | /* Increment or decrement the real part of the value, |
6113 | and don't change the imaginary part. */ | |
6114 | if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
6115 | { | |
6116 | tree real, imag; | |
6117 | ||
514727a9 | 6118 | arg = cp_stabilize_reference (arg); |
b99cc6da | 6119 | real = cp_build_unary_op (REALPART_EXPR, arg, true, complain); |
6120 | imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain); | |
6121 | real = cp_build_unary_op (code, real, true, complain); | |
3c550432 | 6122 | if (real == error_mark_node || imag == error_mark_node) |
6123 | return error_mark_node; | |
831d52a2 | 6124 | return build2 (COMPLEX_EXPR, TREE_TYPE (arg), |
3c550432 | 6125 | real, imag); |
c4a8ac95 | 6126 | } |
6127 | ||
471086d6 | 6128 | /* Report invalid types. */ |
6129 | ||
1e66592c | 6130 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER, |
35771a9a | 6131 | arg, true))) |
471086d6 | 6132 | { |
6133 | if (code == PREINCREMENT_EXPR) | |
5c892d4a | 6134 | errstring = _("no pre-increment operator for type"); |
471086d6 | 6135 | else if (code == POSTINCREMENT_EXPR) |
5c892d4a | 6136 | errstring = _("no post-increment operator for type"); |
471086d6 | 6137 | else if (code == PREDECREMENT_EXPR) |
5c892d4a | 6138 | errstring = _("no pre-decrement operator for type"); |
471086d6 | 6139 | else |
5c892d4a | 6140 | errstring = _("no post-decrement operator for type"); |
471086d6 | 6141 | break; |
6142 | } | |
638bed71 | 6143 | else if (arg == error_mark_node) |
6144 | return error_mark_node; | |
471086d6 | 6145 | |
6146 | /* Report something read-only. */ | |
6147 | ||
3e04bd45 | 6148 | if (CP_TYPE_CONST_P (TREE_TYPE (arg)) |
ebd21de4 | 6149 | || TREE_READONLY (arg)) |
6150 | { | |
6151 | if (complain & tf_error) | |
a1f90215 | 6152 | cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR |
6153 | || code == POSTINCREMENT_EXPR) | |
6154 | ? lv_increment : lv_decrement)); | |
ebd21de4 | 6155 | else |
6156 | return error_mark_node; | |
6157 | } | |
471086d6 | 6158 | |
6159 | { | |
cd16867a | 6160 | tree inc; |
20a44a69 | 6161 | tree declared_type = unlowered_expr_type (arg); |
471086d6 | 6162 | |
471086d6 | 6163 | argtype = TREE_TYPE (arg); |
6164 | ||
6165 | /* ARM $5.2.5 last annotation says this should be forbidden. */ | |
6166 | if (TREE_CODE (argtype) == ENUMERAL_TYPE) | |
ebd21de4 | 6167 | { |
6168 | if (complain & tf_error) | |
2b9e3597 | 6169 | permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
561fec9d | 6170 | ? G_("ISO C++ forbids incrementing an enum") |
6171 | : G_("ISO C++ forbids decrementing an enum")); | |
ebd21de4 | 6172 | else |
6173 | return error_mark_node; | |
6174 | } | |
9031d10b | 6175 | |
471086d6 | 6176 | /* Compute the increment. */ |
6177 | ||
c21c015b | 6178 | if (TYPE_PTR_P (argtype)) |
471086d6 | 6179 | { |
4b72716d | 6180 | tree type = complete_type (TREE_TYPE (argtype)); |
9031d10b | 6181 | |
4b72716d | 6182 | if (!COMPLETE_OR_VOID_TYPE_P (type)) |
ebd21de4 | 6183 | { |
6184 | if (complain & tf_error) | |
6185 | error (((code == PREINCREMENT_EXPR | |
6186 | || code == POSTINCREMENT_EXPR)) | |
6187 | ? G_("cannot increment a pointer to incomplete type %qT") | |
6188 | : G_("cannot decrement a pointer to incomplete type %qT"), | |
6189 | TREE_TYPE (argtype)); | |
6190 | else | |
6191 | return error_mark_node; | |
6192 | } | |
24a7eec2 | 6193 | else if (!TYPE_PTROB_P (argtype)) |
ebd21de4 | 6194 | { |
6195 | if (complain & tf_error) | |
9205a6cc | 6196 | pedwarn (input_location, OPT_Wpointer_arith, |
24a7eec2 | 6197 | (code == PREINCREMENT_EXPR |
561fec9d | 6198 | || code == POSTINCREMENT_EXPR) |
24a7eec2 | 6199 | ? G_("ISO C++ forbids incrementing a pointer of type %qT") |
6200 | : G_("ISO C++ forbids decrementing a pointer of type %qT"), | |
6201 | argtype); | |
ebd21de4 | 6202 | else |
6203 | return error_mark_node; | |
6204 | } | |
6205 | ||
d3a4d008 | 6206 | inc = cxx_sizeof_nowarn (TREE_TYPE (argtype)); |
471086d6 | 6207 | } |
6208 | else | |
73fd9cbe | 6209 | inc = VECTOR_TYPE_P (argtype) |
6210 | ? build_one_cst (argtype) | |
6211 | : integer_one_node; | |
471086d6 | 6212 | |
c4698a21 | 6213 | inc = cp_convert (argtype, inc, complain); |
471086d6 | 6214 | |
e4a7640a | 6215 | /* If 'arg' is an Objective-C PROPERTY_REF expression, then we |
6216 | need to ask Objective-C to build the increment or decrement | |
6217 | expression for it. */ | |
6218 | if (objc_is_property_ref (arg)) | |
6219 | return objc_build_incr_expr_for_property_ref (input_location, code, | |
6220 | arg, inc); | |
6221 | ||
471086d6 | 6222 | /* Complain about anything else that is not a true lvalue. */ |
6223 | if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR | |
6224 | || code == POSTINCREMENT_EXPR) | |
ebd21de4 | 6225 | ? lv_increment : lv_decrement), |
6226 | complain)) | |
471086d6 | 6227 | return error_mark_node; |
6228 | ||
5be60f03 | 6229 | /* Forbid using -- or ++ in C++17 on `bool'. */ |
9e1a8234 | 6230 | if (TREE_CODE (declared_type) == BOOLEAN_TYPE) |
1e66592c | 6231 | { |
6232 | if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR) | |
6233 | { | |
ebd21de4 | 6234 | if (complain & tf_error) |
5be60f03 | 6235 | error ("use of an operand of type %qT in %<operator--%> " |
6236 | "is forbidden", boolean_type_node); | |
1e66592c | 6237 | return error_mark_node; |
6238 | } | |
5be60f03 | 6239 | else |
6240 | { | |
40e2decb | 6241 | if (cxx_dialect >= cxx17) |
5be60f03 | 6242 | { |
6243 | if (complain & tf_error) | |
6244 | error ("use of an operand of type %qT in " | |
40e2decb | 6245 | "%<operator++%> is forbidden in C++17", |
5be60f03 | 6246 | boolean_type_node); |
6247 | return error_mark_node; | |
6248 | } | |
6249 | /* Otherwise, [depr.incr.bool] says this is deprecated. */ | |
6250 | else if (!in_system_header_at (input_location)) | |
6251 | warning (OPT_Wdeprecated, "use of an operand of type %qT " | |
6252 | "in %<operator++%> is deprecated", | |
6253 | boolean_type_node); | |
6254 | } | |
4f9a1c9b | 6255 | val = boolean_increment (code, arg); |
1e66592c | 6256 | } |
f6878ed0 | 6257 | else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR) |
6258 | /* An rvalue has no cv-qualifiers. */ | |
6259 | val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc); | |
1e66592c | 6260 | else |
831d52a2 | 6261 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
1e66592c | 6262 | |
471086d6 | 6263 | TREE_SIDE_EFFECTS (val) = 1; |
20a44a69 | 6264 | return val; |
471086d6 | 6265 | } |
6266 | ||
6267 | case ADDR_EXPR: | |
6268 | /* Note that this operation never does default_conversion | |
6269 | regardless of NOCONVERT. */ | |
d6fbd579 | 6270 | return cp_build_addr_expr (arg, complain); |
d913511e | 6271 | |
6272 | default: | |
6273 | break; | |
471086d6 | 6274 | } |
6275 | ||
6276 | if (!errstring) | |
6277 | { | |
6278 | if (argtype == 0) | |
6279 | argtype = TREE_TYPE (arg); | |
d2c63826 | 6280 | return build1 (code, argtype, arg); |
471086d6 | 6281 | } |
6282 | ||
ebd21de4 | 6283 | if (complain & tf_error) |
6284 | error ("%s", errstring); | |
471086d6 | 6285 | return error_mark_node; |
6286 | } | |
6287 | ||
ebd21de4 | 6288 | /* Hook for the c-common bits that build a unary op. */ |
6289 | tree | |
a49c5913 | 6290 | build_unary_op (location_t /*location*/, |
b99cc6da | 6291 | enum tree_code code, tree xarg, bool noconvert) |
ebd21de4 | 6292 | { |
6293 | return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error); | |
6294 | } | |
6295 | ||
471086d6 | 6296 | /* Apply unary lvalue-demanding operator CODE to the expression ARG |
6297 | for certain kinds of expressions which are not really lvalues | |
6298 | but which we can accept as lvalues. | |
6299 | ||
1fe46df1 | 6300 | If ARG is not a kind of expression we can handle, return |
6301 | NULL_TREE. */ | |
9031d10b | 6302 | |
471086d6 | 6303 | tree |
f0dd33b5 | 6304 | unary_complex_lvalue (enum tree_code code, tree arg) |
471086d6 | 6305 | { |
1fe46df1 | 6306 | /* Inside a template, making these kinds of adjustments is |
6307 | pointless; we are only concerned with the type of the | |
6308 | expression. */ | |
6309 | if (processing_template_decl) | |
6310 | return NULL_TREE; | |
6311 | ||
471086d6 | 6312 | /* Handle (a, b) used as an "lvalue". */ |
6313 | if (TREE_CODE (arg) == COMPOUND_EXPR) | |
6314 | { | |
b99cc6da | 6315 | tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false, |
ebd21de4 | 6316 | tf_warning_or_error); |
831d52a2 | 6317 | return build2 (COMPOUND_EXPR, TREE_TYPE (real_result), |
6318 | TREE_OPERAND (arg, 0), real_result); | |
471086d6 | 6319 | } |
6320 | ||
6321 | /* Handle (a ? b : c) used as an "lvalue". */ | |
0dbd1c74 | 6322 | if (TREE_CODE (arg) == COND_EXPR |
6323 | || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR) | |
ebd21de4 | 6324 | return rationalize_conditional_expr (code, arg, tf_warning_or_error); |
d81e00a4 | 6325 | |
21ca8a32 | 6326 | /* Handle (a = b), (++a), and (--a) used as an "lvalue". */ |
bcf789d7 | 6327 | if (TREE_CODE (arg) == MODIFY_EXPR |
6328 | || TREE_CODE (arg) == PREINCREMENT_EXPR | |
6329 | || TREE_CODE (arg) == PREDECREMENT_EXPR) | |
21ca8a32 | 6330 | { |
6331 | tree lvalue = TREE_OPERAND (arg, 0); | |
6332 | if (TREE_SIDE_EFFECTS (lvalue)) | |
6333 | { | |
514727a9 | 6334 | lvalue = cp_stabilize_reference (lvalue); |
831d52a2 | 6335 | arg = build2 (TREE_CODE (arg), TREE_TYPE (arg), |
6336 | lvalue, TREE_OPERAND (arg, 1)); | |
21ca8a32 | 6337 | } |
6338 | return unary_complex_lvalue | |
831d52a2 | 6339 | (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue)); |
21ca8a32 | 6340 | } |
471086d6 | 6341 | |
6342 | if (code != ADDR_EXPR) | |
aed62d2b | 6343 | return NULL_TREE; |
471086d6 | 6344 | |
6345 | /* Handle (a = b) used as an "lvalue" for `&'. */ | |
6346 | if (TREE_CODE (arg) == MODIFY_EXPR | |
6347 | || TREE_CODE (arg) == INIT_EXPR) | |
6348 | { | |
b99cc6da | 6349 | tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false, |
ebd21de4 | 6350 | tf_warning_or_error); |
831d52a2 | 6351 | arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result), |
6352 | arg, real_result); | |
4ee9c684 | 6353 | TREE_NO_WARNING (arg) = 1; |
860740a7 | 6354 | return arg; |
471086d6 | 6355 | } |
6356 | ||
471086d6 | 6357 | if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE |
6358 | || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE | |
1bc16cab | 6359 | || TREE_CODE (arg) == OFFSET_REF) |
80d7b035 | 6360 | return NULL_TREE; |
9031d10b | 6361 | |
471086d6 | 6362 | /* We permit compiler to make function calls returning |
6363 | objects of aggregate type look like lvalues. */ | |
6364 | { | |
6365 | tree targ = arg; | |
6366 | ||
6367 | if (TREE_CODE (targ) == SAVE_EXPR) | |
6368 | targ = TREE_OPERAND (targ, 0); | |
6369 | ||
95397ff9 | 6370 | if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ))) |
471086d6 | 6371 | { |
6372 | if (TREE_CODE (arg) == SAVE_EXPR) | |
6373 | targ = arg; | |
6374 | else | |
3d4bed93 | 6375 | targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error); |
ce28ee2e | 6376 | return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ); |
471086d6 | 6377 | } |
6378 | ||
86ccc124 | 6379 | if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ)) |
831d52a2 | 6380 | return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)), |
471086d6 | 6381 | TREE_OPERAND (targ, 0), current_function_decl, NULL); |
471086d6 | 6382 | } |
6383 | ||
6384 | /* Don't let anything else be handled specially. */ | |
aed62d2b | 6385 | return NULL_TREE; |
471086d6 | 6386 | } |
471086d6 | 6387 | \f |
6388 | /* Mark EXP saying that we need to be able to take the | |
6389 | address of it; it should not be allocated in a register. | |
3385a573 | 6390 | Value is true if successful. ARRAY_REF_P is true if this |
6391 | is for ARRAY_REF construction - in that case we don't want | |
6392 | to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE, | |
6393 | it is fine to use ARRAY_REFs for vector subscripts on vector | |
6394 | register variables. | |
471086d6 | 6395 | |
e1dcbf32 | 6396 | C++: we do not allow `current_class_ptr' to be addressable. */ |
471086d6 | 6397 | |
9b86eec0 | 6398 | bool |
3385a573 | 6399 | cxx_mark_addressable (tree exp, bool array_ref_p) |
471086d6 | 6400 | { |
cd16867a | 6401 | tree x = exp; |
471086d6 | 6402 | |
471086d6 | 6403 | while (1) |
6404 | switch (TREE_CODE (x)) | |
6405 | { | |
3385a573 | 6406 | case VIEW_CONVERT_EXPR: |
6407 | if (array_ref_p | |
6408 | && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE | |
6409 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0)))) | |
6410 | return true; | |
6411 | /* FALLTHRU */ | |
471086d6 | 6412 | case ADDR_EXPR: |
6413 | case COMPONENT_REF: | |
6414 | case ARRAY_REF: | |
c4a8ac95 | 6415 | case REALPART_EXPR: |
6416 | case IMAGPART_EXPR: | |
471086d6 | 6417 | x = TREE_OPERAND (x, 0); |
6418 | break; | |
6419 | ||
6420 | case PARM_DECL: | |
e1dcbf32 | 6421 | if (x == current_class_ptr) |
471086d6 | 6422 | { |
653e5405 | 6423 | error ("cannot take the address of %<this%>, which is an rvalue expression"); |
331bc0ad | 6424 | TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */ |
9b86eec0 | 6425 | return true; |
471086d6 | 6426 | } |
331bc0ad | 6427 | /* Fall through. */ |
4f20a742 | 6428 | |
471086d6 | 6429 | case VAR_DECL: |
471086d6 | 6430 | /* Caller should not be trying to mark initialized |
6431 | constant fields addressable. */ | |
b4df430b | 6432 | gcc_assert (DECL_LANG_SPECIFIC (x) == 0 |
6433 | || DECL_IN_AGGR_P (x) == 0 | |
6434 | || TREE_STATIC (x) | |
6435 | || DECL_EXTERNAL (x)); | |
331bc0ad | 6436 | /* Fall through. */ |
471086d6 | 6437 | |
471086d6 | 6438 | case RESULT_DECL: |
38281c46 | 6439 | if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) |
d09ef0b3 | 6440 | && !DECL_ARTIFICIAL (x)) |
6278b80d | 6441 | { |
80a58eb0 | 6442 | if (VAR_P (x) && DECL_HARD_REGISTER (x)) |
6278b80d | 6443 | { |
6444 | error | |
6445 | ("address of explicit register variable %qD requested", x); | |
6446 | return false; | |
6447 | } | |
b6bc0551 | 6448 | else if (extra_warnings) |
6278b80d | 6449 | warning |
ced7c954 | 6450 | (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x); |
6278b80d | 6451 | } |
471086d6 | 6452 | TREE_ADDRESSABLE (x) = 1; |
9b86eec0 | 6453 | return true; |
471086d6 | 6454 | |
744a9bd0 | 6455 | case CONST_DECL: |
471086d6 | 6456 | case FUNCTION_DECL: |
471086d6 | 6457 | TREE_ADDRESSABLE (x) = 1; |
9b86eec0 | 6458 | return true; |
471086d6 | 6459 | |
96624a9e | 6460 | case CONSTRUCTOR: |
6461 | TREE_ADDRESSABLE (x) = 1; | |
9b86eec0 | 6462 | return true; |
96624a9e | 6463 | |
38281c46 | 6464 | case TARGET_EXPR: |
6465 | TREE_ADDRESSABLE (x) = 1; | |
9b86eec0 | 6466 | cxx_mark_addressable (TREE_OPERAND (x, 0)); |
6467 | return true; | |
38281c46 | 6468 | |
471086d6 | 6469 | default: |
9b86eec0 | 6470 | return true; |
471086d6 | 6471 | } |
6472 | } | |
6473 | \f | |
6474 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. */ | |
6475 | ||
6476 | tree | |
255b5d15 | 6477 | build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2, |
ebd21de4 | 6478 | tsubst_flags_t complain) |
471086d6 | 6479 | { |
13795292 | 6480 | tree orig_ifexp = ifexp; |
6481 | tree orig_op1 = op1; | |
6482 | tree orig_op2 = op2; | |
6483 | tree expr; | |
6484 | ||
3cc0b4b9 | 6485 | if (processing_template_decl) |
13795292 | 6486 | { |
6487 | /* The standard says that the expression is type-dependent if | |
6488 | IFEXP is type-dependent, even though the eventual type of the | |
6489 | expression doesn't dependent on IFEXP. */ | |
6490 | if (type_dependent_expression_p (ifexp) | |
716521b5 | 6491 | /* As a GNU extension, the middle operand may be omitted. */ |
6492 | || (op1 && type_dependent_expression_p (op1)) | |
13795292 | 6493 | || type_dependent_expression_p (op2)) |
255b5d15 | 6494 | return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2); |
13795292 | 6495 | ifexp = build_non_dependent_expr (ifexp); |
716521b5 | 6496 | if (op1) |
6497 | op1 = build_non_dependent_expr (op1); | |
13795292 | 6498 | op2 = build_non_dependent_expr (op2); |
6499 | } | |
e857e9c7 | 6500 | |
1273a0b4 | 6501 | expr = build_conditional_expr (loc, ifexp, op1, op2, complain); |
e6517a48 | 6502 | if (processing_template_decl && expr != error_mark_node) |
b3166e78 | 6503 | { |
6504 | tree min = build_min_non_dep (COND_EXPR, expr, | |
6505 | orig_ifexp, orig_op1, orig_op2); | |
9231d5c2 | 6506 | /* Remember that the result is an lvalue or xvalue. */ |
f3250f7c | 6507 | if (glvalue_p (expr) && !glvalue_p (min)) |
b3166e78 | 6508 | TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min), |
18bede74 | 6509 | !lvalue_p (expr)); |
b3166e78 | 6510 | expr = convert_from_reference (min); |
6511 | } | |
13795292 | 6512 | return expr; |
471086d6 | 6513 | } |
6514 | \f | |
8a4008da | 6515 | /* Given a list of expressions, return a compound expression |
bd8962d5 | 6516 | that performs them all and returns the value of the last of them. */ |
8a4008da | 6517 | |
183407ee | 6518 | tree |
1f3d2e3f | 6519 | build_x_compound_expr_from_list (tree list, expr_list_kind exp, |
6520 | tsubst_flags_t complain) | |
8a4008da | 6521 | { |
6522 | tree expr = TREE_VALUE (list); | |
9031d10b | 6523 | |
175fdc24 | 6524 | if (BRACE_ENCLOSED_INITIALIZER_P (expr) |
6525 | && !CONSTRUCTOR_IS_DIRECT_INIT (expr)) | |
6526 | { | |
6527 | if (complain & tf_error) | |
3df42822 | 6528 | pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0, |
6529 | "list-initializer for non-class type must not " | |
6530 | "be parenthesized"); | |
175fdc24 | 6531 | else |
6532 | return error_mark_node; | |
6533 | } | |
6534 | ||
8a4008da | 6535 | if (TREE_CHAIN (list)) |
6536 | { | |
1f3d2e3f | 6537 | if (complain & tf_error) |
6538 | switch (exp) | |
6539 | { | |
183407ee | 6540 | case ELK_INIT: |
6541 | permerror (input_location, "expression list treated as compound " | |
6542 | "expression in initializer"); | |
6543 | break; | |
6544 | case ELK_MEM_INIT: | |
6545 | permerror (input_location, "expression list treated as compound " | |
6546 | "expression in mem-initializer"); | |
6547 | break; | |
6548 | case ELK_FUNC_CAST: | |
6549 | permerror (input_location, "expression list treated as compound " | |
6550 | "expression in functional cast"); | |
6551 | break; | |
6552 | default: | |
6553 | gcc_unreachable (); | |
1f3d2e3f | 6554 | } |
ae813c19 | 6555 | else |
6556 | return error_mark_node; | |
8a4008da | 6557 | |
6558 | for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list)) | |
255b5d15 | 6559 | expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)), |
6560 | expr, TREE_VALUE (list), complain); | |
8a4008da | 6561 | } |
9031d10b | 6562 | |
8a4008da | 6563 | return expr; |
6564 | } | |
6565 | ||
f352a3fb | 6566 | /* Like build_x_compound_expr_from_list, but using a VEC. */ |
6567 | ||
6568 | tree | |
f1f41a6c | 6569 | build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg, |
14376b10 | 6570 | tsubst_flags_t complain) |
f352a3fb | 6571 | { |
f1f41a6c | 6572 | if (vec_safe_is_empty (vec)) |
f352a3fb | 6573 | return NULL_TREE; |
f1f41a6c | 6574 | else if (vec->length () == 1) |
6575 | return (*vec)[0]; | |
f352a3fb | 6576 | else |
6577 | { | |
6578 | tree expr; | |
6579 | unsigned int ix; | |
6580 | tree t; | |
6581 | ||
6582 | if (msg != NULL) | |
14376b10 | 6583 | { |
6584 | if (complain & tf_error) | |
6585 | permerror (input_location, | |
6586 | "%s expression list treated as compound expression", | |
6587 | msg); | |
6588 | else | |
6589 | return error_mark_node; | |
6590 | } | |
f352a3fb | 6591 | |
f1f41a6c | 6592 | expr = (*vec)[0]; |
6593 | for (ix = 1; vec->iterate (ix, &t); ++ix) | |
255b5d15 | 6594 | expr = build_x_compound_expr (EXPR_LOCATION (t), expr, |
14376b10 | 6595 | t, complain); |
f352a3fb | 6596 | |
6597 | return expr; | |
6598 | } | |
6599 | } | |
6600 | ||
8a4008da | 6601 | /* Handle overloading of the ',' operator when needed. */ |
96624a9e | 6602 | |
471086d6 | 6603 | tree |
255b5d15 | 6604 | build_x_compound_expr (location_t loc, tree op1, tree op2, |
6605 | tsubst_flags_t complain) | |
471086d6 | 6606 | { |
471086d6 | 6607 | tree result; |
13795292 | 6608 | tree orig_op1 = op1; |
6609 | tree orig_op2 = op2; | |
d175f019 | 6610 | tree overload = NULL_TREE; |
471086d6 | 6611 | |
3cc0b4b9 | 6612 | if (processing_template_decl) |
13795292 | 6613 | { |
6614 | if (type_dependent_expression_p (op1) | |
6615 | || type_dependent_expression_p (op2)) | |
255b5d15 | 6616 | return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2); |
13795292 | 6617 | op1 = build_non_dependent_expr (op1); |
6618 | op2 = build_non_dependent_expr (op2); | |
6619 | } | |
11682839 | 6620 | |
255b5d15 | 6621 | result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, |
d175f019 | 6622 | NULL_TREE, &overload, complain); |
13795292 | 6623 | if (!result) |
717ecce9 | 6624 | result = cp_build_compound_expr (op1, op2, complain); |
11682839 | 6625 | |
13795292 | 6626 | if (processing_template_decl && result != error_mark_node) |
d175f019 | 6627 | { |
6628 | if (overload != NULL_TREE) | |
6629 | return (build_min_non_dep_op_overload | |
6630 | (COMPOUND_EXPR, result, overload, orig_op1, orig_op2)); | |
6631 | ||
6632 | return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2); | |
6633 | } | |
9031d10b | 6634 | |
13795292 | 6635 | return result; |
471086d6 | 6636 | } |
6637 | ||
717ecce9 | 6638 | /* Like cp_build_compound_expr, but for the c-common bits. */ |
6639 | ||
6640 | tree | |
a49c5913 | 6641 | build_compound_expr (location_t /*loc*/, tree lhs, tree rhs) |
717ecce9 | 6642 | { |
6643 | return cp_build_compound_expr (lhs, rhs, tf_warning_or_error); | |
6644 | } | |
6645 | ||
bd8962d5 | 6646 | /* Build a compound expression. */ |
471086d6 | 6647 | |
6648 | tree | |
717ecce9 | 6649 | cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain) |
471086d6 | 6650 | { |
dab3247a | 6651 | lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain); |
9031d10b | 6652 | |
8a4008da | 6653 | if (lhs == error_mark_node || rhs == error_mark_node) |
aeef2be5 | 6654 | return error_mark_node; |
9031d10b | 6655 | |
1cc57cf8 | 6656 | if (TREE_CODE (rhs) == TARGET_EXPR) |
6657 | { | |
6658 | /* If the rhs is a TARGET_EXPR, then build the compound | |
653e5405 | 6659 | expression inside the target_expr's initializer. This |
63eff20d | 6660 | helps the compiler to eliminate unnecessary temporaries. */ |
1cc57cf8 | 6661 | tree init = TREE_OPERAND (rhs, 1); |
9031d10b | 6662 | |
831d52a2 | 6663 | init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init); |
1cc57cf8 | 6664 | TREE_OPERAND (rhs, 1) = init; |
9031d10b | 6665 | |
1cc57cf8 | 6666 | return rhs; |
6667 | } | |
9031d10b | 6668 | |
8d82d1c8 | 6669 | if (type_unknown_p (rhs)) |
6670 | { | |
19b271a9 | 6671 | if (complain & tf_error) |
6672 | error ("no context to resolve type of %qE", rhs); | |
8d82d1c8 | 6673 | return error_mark_node; |
6674 | } | |
6675 | ||
831d52a2 | 6676 | return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs); |
471086d6 | 6677 | } |
6678 | ||
6ab399e8 | 6679 | /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE |
6bac3cd5 | 6680 | casts away constness. CAST gives the type of cast. Returns true |
6681 | if the cast is ill-formed, false if it is well-formed. | |
9258414e | 6682 | |
6683 | ??? This function warns for casting away any qualifier not just | |
6684 | const. We would like to specify exactly what qualifiers are casted | |
6685 | away. | |
6686 | */ | |
ec965e9b | 6687 | |
6bac3cd5 | 6688 | static bool |
1bc16cab | 6689 | check_for_casting_away_constness (tree src_type, tree dest_type, |
6bac3cd5 | 6690 | enum tree_code cast, tsubst_flags_t complain) |
ec965e9b | 6691 | { |
868ff017 | 6692 | /* C-style casts are allowed to cast away constness. With |
6693 | WARN_CAST_QUAL, we still want to issue a warning. */ | |
6694 | if (cast == CAST_EXPR && !warn_cast_qual) | |
6bac3cd5 | 6695 | return false; |
868ff017 | 6696 | |
66bbeb85 | 6697 | if (!casts_away_constness (src_type, dest_type, complain)) |
6bac3cd5 | 6698 | return false; |
9258414e | 6699 | |
6700 | switch (cast) | |
6701 | { | |
6702 | case CAST_EXPR: | |
6bac3cd5 | 6703 | if (complain & tf_warning) |
6704 | warning (OPT_Wcast_qual, | |
6705 | "cast from type %qT to type %qT casts away qualifiers", | |
6706 | src_type, dest_type); | |
6707 | return false; | |
9258414e | 6708 | |
6709 | case STATIC_CAST_EXPR: | |
6bac3cd5 | 6710 | if (complain & tf_error) |
6711 | error ("static_cast from type %qT to type %qT casts away qualifiers", | |
6712 | src_type, dest_type); | |
6713 | return true; | |
9258414e | 6714 | |
6715 | case REINTERPRET_CAST_EXPR: | |
6bac3cd5 | 6716 | if (complain & tf_error) |
6717 | error ("reinterpret_cast from type %qT to type %qT casts away qualifiers", | |
6718 | src_type, dest_type); | |
6719 | return true; | |
6720 | ||
9258414e | 6721 | default: |
6722 | gcc_unreachable(); | |
6723 | } | |
ec965e9b | 6724 | } |
6725 | ||
220a51b0 | 6726 | /* Warns if the cast from expression EXPR to type TYPE is useless. */ |
62206d34 | 6727 | void |
6728 | maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain) | |
6729 | { | |
6730 | if (warn_useless_cast | |
734ec290 | 6731 | && complain & tf_warning) |
62206d34 | 6732 | { |
62206d34 | 6733 | if ((TREE_CODE (type) == REFERENCE_TYPE |
6734 | && (TYPE_REF_IS_RVALUE (type) | |
18bede74 | 6735 | ? xvalue_p (expr) : lvalue_p (expr)) |
62206d34 | 6736 | && same_type_p (TREE_TYPE (expr), TREE_TYPE (type))) |
6737 | || same_type_p (TREE_TYPE (expr), type)) | |
d4f5410d | 6738 | warning (OPT_Wuseless_cast, "useless cast to type %q#T", type); |
62206d34 | 6739 | } |
6740 | } | |
6741 | ||
220a51b0 | 6742 | /* Warns if the cast ignores cv-qualifiers on TYPE. */ |
6743 | void | |
6744 | maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain) | |
6745 | { | |
6746 | if (warn_ignored_qualifiers | |
6747 | && complain & tf_warning | |
6748 | && !CLASS_TYPE_P (type) | |
6749 | && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))) | |
6750 | { | |
6751 | warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast " | |
6752 | "result type"); | |
6753 | } | |
6754 | } | |
6755 | ||
cb02169c | 6756 | /* Convert EXPR (an expression with pointer-to-member type) to TYPE |
6757 | (another pointer-to-member type in the same hierarchy) and return | |
6758 | the converted expression. If ALLOW_INVERSE_P is permitted, a | |
6759 | pointer-to-derived may be converted to pointer-to-base; otherwise, | |
6760 | only the other direction is permitted. If C_CAST_P is true, this | |
6761 | conversion is taking place as part of a C-style cast. */ | |
6762 | ||
9031d10b | 6763 | tree |
cb02169c | 6764 | convert_ptrmem (tree type, tree expr, bool allow_inverse_p, |
85132186 | 6765 | bool c_cast_p, tsubst_flags_t complain) |
cb02169c | 6766 | { |
0a8a2108 | 6767 | if (same_type_p (type, TREE_TYPE (expr))) |
6768 | return expr; | |
6769 | ||
05765a91 | 6770 | if (TYPE_PTRDATAMEM_P (type)) |
cb02169c | 6771 | { |
6772 | tree delta; | |
6773 | ||
cb02169c | 6774 | delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)), |
9031d10b | 6775 | TYPE_PTRMEM_CLASS_TYPE (type), |
cb02169c | 6776 | allow_inverse_p, |
85132186 | 6777 | c_cast_p, complain); |
6778 | if (delta == error_mark_node) | |
6779 | return error_mark_node; | |
6780 | ||
cb02169c | 6781 | if (!integer_zerop (delta)) |
e5ead12a | 6782 | { |
6783 | tree cond, op1, op2; | |
6784 | ||
0a8a2108 | 6785 | if (TREE_CODE (expr) == PTRMEM_CST) |
6786 | expr = cplus_expand_constant (expr); | |
8e70fb09 | 6787 | cond = cp_build_binary_op (input_location, |
6788 | EQ_EXPR, | |
e5ead12a | 6789 | expr, |
ebd21de4 | 6790 | build_int_cst (TREE_TYPE (expr), -1), |
34f12878 | 6791 | complain); |
e5ead12a | 6792 | op1 = build_nop (ptrdiff_type_node, expr); |
8e70fb09 | 6793 | op2 = cp_build_binary_op (input_location, |
6794 | PLUS_EXPR, op1, delta, | |
34f12878 | 6795 | complain); |
e5ead12a | 6796 | |
389dd41b | 6797 | expr = fold_build3_loc (input_location, |
6798 | COND_EXPR, ptrdiff_type_node, cond, op1, op2); | |
e5ead12a | 6799 | |
6800 | } | |
6801 | ||
cb02169c | 6802 | return build_nop (type, expr); |
6803 | } | |
6804 | else | |
9031d10b | 6805 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, |
85132186 | 6806 | allow_inverse_p, c_cast_p, complain); |
cb02169c | 6807 | } |
6808 | ||
6ab399e8 | 6809 | /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true, |
6810 | this static_cast is being attempted as one of the possible casts | |
6811 | allowed by a C-style cast. (In that case, accessibility of base | |
6812 | classes is not considered, and it is OK to cast away | |
6813 | constness.) Return the result of the cast. *VALID_P is set to | |
6814 | indicate whether or not the cast was valid. */ | |
ec965e9b | 6815 | |
6ab399e8 | 6816 | static tree |
6817 | build_static_cast_1 (tree type, tree expr, bool c_cast_p, | |
ebd21de4 | 6818 | bool *valid_p, tsubst_flags_t complain) |
d81e00a4 | 6819 | { |
3ec59b57 | 6820 | tree intype; |
ec965e9b | 6821 | tree result; |
899e9211 | 6822 | cp_lvalue_kind clk; |
f9670f72 | 6823 | |
6ab399e8 | 6824 | /* Assume the cast is valid. */ |
6825 | *valid_p = true; | |
f9670f72 | 6826 | |
899e9211 | 6827 | intype = unlowered_expr_type (expr); |
f9670f72 | 6828 | |
35df6eb4 | 6829 | /* Save casted types in the function's used types hash table. */ |
6830 | used_types_insert (type); | |
6831 | ||
220a51b0 | 6832 | /* A prvalue of non-class type is cv-unqualified. */ |
6833 | if (!CLASS_TYPE_P (type)) | |
6834 | type = cv_unqualified (type); | |
6835 | ||
ec965e9b | 6836 | /* [expr.static.cast] |
f9670f72 | 6837 | |
ec965e9b | 6838 | An lvalue of type "cv1 B", where B is a class type, can be cast |
6839 | to type "reference to cv2 D", where D is a class derived (clause | |
6840 | _class.derived_) from B, if a valid standard conversion from | |
6841 | "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the | |
6842 | same cv-qualification as, or greater cv-qualification than, cv1, | |
6843 | and B is not a virtual base class of D. */ | |
a681799d | 6844 | /* We check this case before checking the validity of "TYPE t = |
6845 | EXPR;" below because for this case: | |
6846 | ||
6847 | struct B {}; | |
6848 | struct D : public B { D(const B&); }; | |
6849 | extern B& b; | |
6850 | void f() { static_cast<const D&>(b); } | |
6851 | ||
6852 | we want to avoid constructing a new D. The standard is not | |
6853 | completely clear about this issue, but our interpretation is | |
6854 | consistent with other compilers. */ | |
ec965e9b | 6855 | if (TREE_CODE (type) == REFERENCE_TYPE |
6856 | && CLASS_TYPE_P (TREE_TYPE (type)) | |
6857 | && CLASS_TYPE_P (intype) | |
18bede74 | 6858 | && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr)) |
ec965e9b | 6859 | && DERIVED_FROM_P (intype, TREE_TYPE (type)) |
6860 | && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)), | |
9031d10b | 6861 | build_pointer_type (TYPE_MAIN_VARIANT |
66bbeb85 | 6862 | (TREE_TYPE (type))), |
6863 | complain) | |
6ab399e8 | 6864 | && (c_cast_p |
6865 | || at_least_as_qualified_p (TREE_TYPE (type), intype))) | |
f9670f72 | 6866 | { |
6ab399e8 | 6867 | tree base; |
6868 | ||
c36e63fe | 6869 | if (processing_template_decl) |
6870 | return expr; | |
6871 | ||
f46d4a11 | 6872 | /* There is a standard conversion from "D*" to "B*" even if "B" |
6ab399e8 | 6873 | is ambiguous or inaccessible. If this is really a |
6874 | static_cast, then we check both for inaccessibility and | |
6875 | ambiguity. However, if this is a static_cast being performed | |
6876 | because the user wrote a C-style cast, then accessibility is | |
6877 | not considered. */ | |
9031d10b | 6878 | base = lookup_base (TREE_TYPE (type), intype, |
6879 | c_cast_p ? ba_unique : ba_check, | |
ae260dcc | 6880 | NULL, complain); |
32cf7025 | 6881 | expr = build_address (expr); |
6882 | ||
9917317a | 6883 | if (sanitize_flags_p (SANITIZE_VPTR)) |
32cf7025 | 6884 | { |
6885 | tree ubsan_check | |
e2225bd4 | 6886 | = cp_ubsan_maybe_instrument_downcast (input_location, type, |
6887 | intype, expr); | |
32cf7025 | 6888 | if (ubsan_check) |
6889 | expr = ubsan_check; | |
6890 | } | |
ec965e9b | 6891 | |
f46d4a11 | 6892 | /* Convert from "B*" to "D*". This function will check that "B" |
6893 | is not a virtual base of "D". */ | |
32cf7025 | 6894 | expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false, |
6895 | complain); | |
6896 | ||
66976059 | 6897 | /* Convert the pointer to a reference -- but then remember that |
a1f05651 | 6898 | there are no expressions with reference type in C++. |
6899 | ||
6900 | We call rvalue so that there's an actual tree code | |
6901 | (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand | |
6902 | is a variable with the same type, the conversion would get folded | |
6903 | away, leaving just the variable and causing lvalue_kind to give | |
6904 | the wrong answer. */ | |
6905 | return convert_from_reference (rvalue (cp_fold_convert (type, expr))); | |
f9670f72 | 6906 | } |
ec965e9b | 6907 | |
d295f848 | 6908 | /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to |
aaa6e1c2 | 6909 | cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */ |
6910 | if (TREE_CODE (type) == REFERENCE_TYPE | |
6911 | && TYPE_REF_IS_RVALUE (type) | |
899e9211 | 6912 | && (clk = real_lvalue_p (expr)) |
aaa6e1c2 | 6913 | && reference_related_p (TREE_TYPE (type), intype) |
6914 | && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype))) | |
6915 | { | |
c36e63fe | 6916 | if (processing_template_decl) |
6917 | return expr; | |
899e9211 | 6918 | if (clk == clk_ordinary) |
6919 | { | |
6920 | /* Handle the (non-bit-field) lvalue case here by casting to | |
6921 | lvalue reference and then changing it to an rvalue reference. | |
6922 | Casting an xvalue to rvalue reference will be handled by the | |
6923 | main code path. */ | |
6924 | tree lref = cp_build_reference_type (TREE_TYPE (type), false); | |
6925 | result = (perform_direct_initialization_if_possible | |
6926 | (lref, expr, c_cast_p, complain)); | |
b0531ab4 | 6927 | result = build1 (NON_LVALUE_EXPR, type, result); |
899e9211 | 6928 | return convert_from_reference (result); |
6929 | } | |
6930 | else | |
6931 | /* For a bit-field or packed field, bind to a temporary. */ | |
6932 | expr = rvalue (expr); | |
aaa6e1c2 | 6933 | } |
6934 | ||
4d5c38fc | 6935 | /* Resolve overloaded address here rather than once in |
6936 | implicit_conversion and again in the inverse code below. */ | |
6937 | if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr)) | |
6938 | { | |
6939 | expr = instantiate_type (type, expr, complain); | |
6940 | intype = TREE_TYPE (expr); | |
6941 | } | |
6942 | ||
a75f187e | 6943 | /* [expr.static.cast] |
6944 | ||
6945 | Any expression can be explicitly converted to type cv void. */ | |
c21c015b | 6946 | if (VOID_TYPE_P (type)) |
a75f187e | 6947 | return convert_to_void (expr, ICV_CAST, complain); |
6948 | ||
d28993f1 | 6949 | /* [class.abstract] |
6950 | An abstract class shall not be used ... as the type of an explicit | |
6951 | conversion. */ | |
6952 | if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain)) | |
6953 | return error_mark_node; | |
6954 | ||
a681799d | 6955 | /* [expr.static.cast] |
6956 | ||
6957 | An expression e can be explicitly converted to a type T using a | |
6958 | static_cast of the form static_cast<T>(e) if the declaration T | |
6959 | t(e);" is well-formed, for some invented temporary variable | |
6960 | t. */ | |
6ab399e8 | 6961 | result = perform_direct_initialization_if_possible (type, expr, |
ebd21de4 | 6962 | c_cast_p, complain); |
a681799d | 6963 | if (result) |
bcac70f4 | 6964 | { |
c36e63fe | 6965 | if (processing_template_decl) |
6966 | return expr; | |
6967 | ||
bcac70f4 | 6968 | result = convert_from_reference (result); |
6ab399e8 | 6969 | |
bcac70f4 | 6970 | /* [expr.static.cast] |
6971 | ||
653e5405 | 6972 | If T is a reference type, the result is an lvalue; otherwise, |
bcac70f4 | 6973 | the result is an rvalue. */ |
fcb3f76e | 6974 | if (TREE_CODE (type) != REFERENCE_TYPE) |
3c77e6e5 | 6975 | { |
6976 | result = rvalue (result); | |
6977 | ||
6978 | if (result == expr && SCALAR_TYPE_P (type)) | |
6979 | /* Leave some record of the cast. */ | |
6980 | result = build_nop (type, expr); | |
6981 | } | |
bcac70f4 | 6982 | return result; |
6983 | } | |
9031d10b | 6984 | |
ec965e9b | 6985 | /* [expr.static.cast] |
6986 | ||
6987 | The inverse of any standard conversion sequence (clause _conv_), | |
6988 | other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer | |
6989 | (_conv.array_), function-to-pointer (_conv.func_), and boolean | |
6990 | (_conv.bool_) conversions, can be performed explicitly using | |
6991 | static_cast subject to the restriction that the explicit | |
6992 | conversion does not cast away constness (_expr.const.cast_), and | |
6993 | the following additional rules for specific cases: */ | |
6994 | /* For reference, the conversions not excluded are: integral | |
6995 | promotions, floating point promotion, integral conversions, | |
6996 | floating point conversions, floating-integral conversions, | |
6997 | pointer conversions, and pointer to member conversions. */ | |
e34aad80 | 6998 | /* DR 128 |
9031d10b | 6999 | |
e34aad80 | 7000 | A value of integral _or enumeration_ type can be explicitly |
7001 | converted to an enumeration type. */ | |
7002 | /* The effect of all that is that any conversion between any two | |
7003 | types which are integral, floating, or enumeration types can be | |
7004 | performed. */ | |
b3353c23 | 7005 | if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
7006 | || SCALAR_FLOAT_TYPE_P (type)) | |
7007 | && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype) | |
7008 | || SCALAR_FLOAT_TYPE_P (intype))) | |
c36e63fe | 7009 | { |
7010 | if (processing_template_decl) | |
7011 | return expr; | |
7012 | return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain); | |
7013 | } | |
6ab399e8 | 7014 | |
ec965e9b | 7015 | if (TYPE_PTR_P (type) && TYPE_PTR_P (intype) |
7016 | && CLASS_TYPE_P (TREE_TYPE (type)) | |
7017 | && CLASS_TYPE_P (TREE_TYPE (intype)) | |
9031d10b | 7018 | && can_convert (build_pointer_type (TYPE_MAIN_VARIANT |
7019 | (TREE_TYPE (intype))), | |
7020 | build_pointer_type (TYPE_MAIN_VARIANT | |
66bbeb85 | 7021 | (TREE_TYPE (type))), |
7022 | complain)) | |
0a106a56 | 7023 | { |
ec965e9b | 7024 | tree base; |
7025 | ||
c36e63fe | 7026 | if (processing_template_decl) |
7027 | return expr; | |
7028 | ||
6bac3cd5 | 7029 | if (!c_cast_p |
7030 | && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR, | |
7031 | complain)) | |
7032 | return error_mark_node; | |
9031d10b | 7033 | base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype), |
7034 | c_cast_p ? ba_unique : ba_check, | |
ae260dcc | 7035 | NULL, complain); |
1e74225a | 7036 | expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false, |
7037 | complain); | |
32cf7025 | 7038 | |
9917317a | 7039 | if (sanitize_flags_p (SANITIZE_VPTR)) |
32cf7025 | 7040 | { |
7041 | tree ubsan_check | |
e2225bd4 | 7042 | = cp_ubsan_maybe_instrument_downcast (input_location, type, |
7043 | intype, expr); | |
32cf7025 | 7044 | if (ubsan_check) |
7045 | expr = ubsan_check; | |
7046 | } | |
7047 | ||
7048 | return cp_fold_convert (type, expr); | |
0a106a56 | 7049 | } |
9031d10b | 7050 | |
05765a91 | 7051 | if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype)) |
ec965e9b | 7052 | || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) |
7053 | { | |
7054 | tree c1; | |
7055 | tree c2; | |
7056 | tree t1; | |
7057 | tree t2; | |
f9670f72 | 7058 | |
ec965e9b | 7059 | c1 = TYPE_PTRMEM_CLASS_TYPE (intype); |
7060 | c2 = TYPE_PTRMEM_CLASS_TYPE (type); | |
7061 | ||
05765a91 | 7062 | if (TYPE_PTRDATAMEM_P (type)) |
ec965e9b | 7063 | { |
9031d10b | 7064 | t1 = (build_ptrmem_type |
ec965e9b | 7065 | (c1, |
7066 | TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype)))); | |
9031d10b | 7067 | t2 = (build_ptrmem_type |
ec965e9b | 7068 | (c2, |
7069 | TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type)))); | |
7070 | } | |
7071 | else | |
7072 | { | |
7073 | t1 = intype; | |
7074 | t2 = type; | |
7075 | } | |
66bbeb85 | 7076 | if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain)) |
ec965e9b | 7077 | { |
6bac3cd5 | 7078 | if (!c_cast_p |
7079 | && check_for_casting_away_constness (intype, type, | |
7080 | STATIC_CAST_EXPR, | |
7081 | complain)) | |
7082 | return error_mark_node; | |
c36e63fe | 7083 | if (processing_template_decl) |
7084 | return expr; | |
cb02169c | 7085 | return convert_ptrmem (type, expr, /*allow_inverse_p=*/1, |
6bac3cd5 | 7086 | c_cast_p, complain); |
ec965e9b | 7087 | } |
7088 | } | |
9031d10b | 7089 | |
f5139c2f | 7090 | /* [expr.static.cast] |
7091 | ||
ec965e9b | 7092 | An rvalue of type "pointer to cv void" can be explicitly |
7093 | converted to a pointer to object type. A value of type pointer | |
7094 | to object converted to "pointer to cv void" and back to the | |
7095 | original pointer type will have its original value. */ | |
c21c015b | 7096 | if (TYPE_PTR_P (intype) |
ec965e9b | 7097 | && VOID_TYPE_P (TREE_TYPE (intype)) |
7098 | && TYPE_PTROB_P (type)) | |
f5139c2f | 7099 | { |
6bac3cd5 | 7100 | if (!c_cast_p |
7101 | && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR, | |
7102 | complain)) | |
7103 | return error_mark_node; | |
c36e63fe | 7104 | if (processing_template_decl) |
7105 | return expr; | |
ec965e9b | 7106 | return build_nop (type, expr); |
f5139c2f | 7107 | } |
7108 | ||
6ab399e8 | 7109 | *valid_p = false; |
f9670f72 | 7110 | return error_mark_node; |
d81e00a4 | 7111 | } |
7112 | ||
6ab399e8 | 7113 | /* Return an expression representing static_cast<TYPE>(EXPR). */ |
7114 | ||
bae9fce7 | 7115 | tree |
4cedc476 | 7116 | build_static_cast (tree type, tree oexpr, tsubst_flags_t complain) |
d81e00a4 | 7117 | { |
4cedc476 | 7118 | tree expr = oexpr; |
6ab399e8 | 7119 | tree result; |
7120 | bool valid_p; | |
f9670f72 | 7121 | |
7122 | if (type == error_mark_node || expr == error_mark_node) | |
7123 | return error_mark_node; | |
7124 | ||
4cedc476 | 7125 | bool dependent = (dependent_type_p (type) |
7126 | || type_dependent_expression_p (expr)); | |
7127 | if (dependent) | |
e857e9c7 | 7128 | { |
4cedc476 | 7129 | tmpl: |
c017458d | 7130 | expr = build_min (STATIC_CAST_EXPR, type, oexpr); |
6ab399e8 | 7131 | /* We don't know if it will or will not have side effects. */ |
7132 | TREE_SIDE_EFFECTS (expr) = 1; | |
34da5a16 | 7133 | return convert_from_reference (expr); |
e857e9c7 | 7134 | } |
7135 | ||
6ab399e8 | 7136 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
7137 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ | |
7138 | if (TREE_CODE (type) != REFERENCE_TYPE | |
7139 | && TREE_CODE (expr) == NOP_EXPR | |
7140 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
7141 | expr = TREE_OPERAND (expr, 0); | |
f9670f72 | 7142 | |
ebd21de4 | 7143 | result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p, |
7144 | complain); | |
6ab399e8 | 7145 | if (valid_p) |
62206d34 | 7146 | { |
7147 | if (result != error_mark_node) | |
220a51b0 | 7148 | { |
7149 | maybe_warn_about_useless_cast (type, expr, complain); | |
7150 | maybe_warn_about_cast_ignoring_quals (type, complain); | |
7151 | } | |
4cedc476 | 7152 | if (processing_template_decl) |
7153 | goto tmpl; | |
62206d34 | 7154 | return result; |
7155 | } | |
6ab399e8 | 7156 | |
ebd21de4 | 7157 | if (complain & tf_error) |
7158 | error ("invalid static_cast from type %qT to type %qT", | |
7159 | TREE_TYPE (expr), type); | |
6ab399e8 | 7160 | return error_mark_node; |
7161 | } | |
7162 | ||
7163 | /* EXPR is an expression with member function or pointer-to-member | |
7164 | function type. TYPE is a pointer type. Converting EXPR to TYPE is | |
7165 | not permitted by ISO C++, but we accept it in some modes. If we | |
7166 | are not in one of those modes, issue a diagnostic. Return the | |
7167 | converted expression. */ | |
7168 | ||
7169 | tree | |
4405c1ad | 7170 | convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain) |
6ab399e8 | 7171 | { |
7172 | tree intype; | |
7173 | tree decl; | |
f9670f72 | 7174 | |
f9670f72 | 7175 | intype = TREE_TYPE (expr); |
6ab399e8 | 7176 | gcc_assert (TYPE_PTRMEMFUNC_P (intype) |
7177 | || TREE_CODE (intype) == METHOD_TYPE); | |
bccffb3a | 7178 | |
4405c1ad | 7179 | if (!(complain & tf_warning_or_error)) |
7180 | return error_mark_node; | |
7181 | ||
6ab399e8 | 7182 | if (pedantic || warn_pmf2ptr) |
29438999 | 7183 | pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions, |
4d1eda3a | 7184 | "converting from %qH to %qI", intype, type); |
9031d10b | 7185 | |
6ab399e8 | 7186 | if (TREE_CODE (intype) == METHOD_TYPE) |
4405c1ad | 7187 | expr = build_addr_func (expr, complain); |
6ab399e8 | 7188 | else if (TREE_CODE (expr) == PTRMEM_CST) |
7189 | expr = build_address (PTRMEM_CST_MEMBER (expr)); | |
7190 | else | |
7191 | { | |
7192 | decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0); | |
7193 | decl = build_address (decl); | |
4405c1ad | 7194 | expr = get_member_function_from_ptrfunc (&decl, expr, complain); |
6ab399e8 | 7195 | } |
7196 | ||
4405c1ad | 7197 | if (expr == error_mark_node) |
7198 | return error_mark_node; | |
7199 | ||
6ab399e8 | 7200 | return build_nop (type, expr); |
7201 | } | |
7202 | ||
7203 | /* Return a representation for a reinterpret_cast from EXPR to TYPE. | |
7204 | If C_CAST_P is true, this reinterpret cast is being done as part of | |
7205 | a C-style cast. If VALID_P is non-NULL, *VALID_P is set to | |
7206 | indicate whether or not reinterpret_cast was valid. */ | |
7207 | ||
7208 | static tree | |
7209 | build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p, | |
ebd21de4 | 7210 | bool *valid_p, tsubst_flags_t complain) |
6ab399e8 | 7211 | { |
7212 | tree intype; | |
7213 | ||
7214 | /* Assume the cast is invalid. */ | |
7215 | if (valid_p) | |
7216 | *valid_p = true; | |
7217 | ||
7218 | if (type == error_mark_node || error_operand_p (expr)) | |
4154c014 | 7219 | return error_mark_node; |
7220 | ||
6ab399e8 | 7221 | intype = TREE_TYPE (expr); |
7222 | ||
35df6eb4 | 7223 | /* Save casted types in the function's used types hash table. */ |
7224 | used_types_insert (type); | |
7225 | ||
220a51b0 | 7226 | /* A prvalue of non-class type is cv-unqualified. */ |
7227 | if (!CLASS_TYPE_P (type)) | |
7228 | type = cv_unqualified (type); | |
7229 | ||
6ab399e8 | 7230 | /* [expr.reinterpret.cast] |
7231 | An lvalue expression of type T1 can be cast to the type | |
7232 | "reference to T2" if an expression of type "pointer to T1" can be | |
7233 | explicitly converted to the type "pointer to T2" using a | |
7234 | reinterpret_cast. */ | |
f9670f72 | 7235 | if (TREE_CODE (type) == REFERENCE_TYPE) |
7236 | { | |
18bede74 | 7237 | if (! lvalue_p (expr)) |
f9670f72 | 7238 | { |
ebd21de4 | 7239 | if (complain & tf_error) |
7240 | error ("invalid cast of an rvalue expression of type " | |
7241 | "%qT to type %qT", | |
7242 | intype, type); | |
f9670f72 | 7243 | return error_mark_node; |
7244 | } | |
6ab399e8 | 7245 | |
7246 | /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and | |
7247 | "B" are related class types; the reinterpret_cast does not | |
7248 | adjust the pointer. */ | |
7249 | if (TYPE_PTR_P (intype) | |
ebd21de4 | 7250 | && (complain & tf_warning) |
a09db423 | 7251 | && (comptypes (TREE_TYPE (intype), TREE_TYPE (type), |
7252 | COMPARE_BASE | COMPARE_DERIVED))) | |
c3ceba8e | 7253 | warning (0, "casting %qT to %qT does not dereference pointer", |
6ab399e8 | 7254 | intype, type); |
7255 | ||
d6fbd579 | 7256 | expr = cp_build_addr_expr (expr, complain); |
a0ab002f | 7257 | |
7258 | if (warn_strict_aliasing > 2) | |
7259 | strict_aliasing_warning (TREE_TYPE (expr), type, expr); | |
7260 | ||
f9670f72 | 7261 | if (expr != error_mark_node) |
6ab399e8 | 7262 | expr = build_reinterpret_cast_1 |
2e365731 | 7263 | (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p, |
ebd21de4 | 7264 | valid_p, complain); |
f9670f72 | 7265 | if (expr != error_mark_node) |
a0ab002f | 7266 | /* cp_build_indirect_ref isn't right for rvalue refs. */ |
7267 | expr = convert_from_reference (fold_convert (type, expr)); | |
f9670f72 | 7268 | return expr; |
7269 | } | |
1a3f833b | 7270 | |
6ab399e8 | 7271 | /* As a G++ extension, we consider conversions from member |
7272 | functions, and pointers to member functions to | |
7273 | pointer-to-function and pointer-to-void types. If | |
7274 | -Wno-pmf-conversions has not been specified, | |
7275 | convert_member_func_to_ptr will issue an error message. */ | |
9031d10b | 7276 | if ((TYPE_PTRMEMFUNC_P (intype) |
6ab399e8 | 7277 | || TREE_CODE (intype) == METHOD_TYPE) |
7278 | && TYPE_PTR_P (type) | |
7279 | && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
7280 | || VOID_TYPE_P (TREE_TYPE (type)))) | |
4405c1ad | 7281 | return convert_member_func_to_ptr (type, expr, complain); |
6ab399e8 | 7282 | |
ea36645e | 7283 | /* If the cast is not to a reference type, the lvalue-to-rvalue, |
6ab399e8 | 7284 | array-to-pointer, and function-to-pointer conversions are |
7285 | performed. */ | |
4405c1ad | 7286 | expr = decay_conversion (expr, complain); |
9031d10b | 7287 | |
6ab399e8 | 7288 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
7289 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ | |
7290 | if (TREE_CODE (expr) == NOP_EXPR | |
7291 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
7292 | expr = TREE_OPERAND (expr, 0); | |
7293 | ||
7294 | if (error_operand_p (expr)) | |
7295 | return error_mark_node; | |
7296 | ||
7297 | intype = TREE_TYPE (expr); | |
7298 | ||
7299 | /* [expr.reinterpret.cast] | |
7300 | A pointer can be converted to any integral type large enough to | |
6fe11077 | 7301 | hold it. ... A value of type std::nullptr_t can be converted to |
7302 | an integral type; the conversion has the same meaning and | |
7303 | validity as a conversion of (void*)0 to the integral type. */ | |
7304 | if (CP_INTEGRAL_TYPE_P (type) | |
c7ca48ea | 7305 | && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype))) |
1a3f833b | 7306 | { |
f9670f72 | 7307 | if (TYPE_PRECISION (type) < TYPE_PRECISION (intype)) |
ebd21de4 | 7308 | { |
7309 | if (complain & tf_error) | |
4d1eda3a | 7310 | permerror (input_location, "cast from %qH to %qI loses precision", |
561fec9d | 7311 | intype, type); |
ebd21de4 | 7312 | else |
7313 | return error_mark_node; | |
7314 | } | |
c7ca48ea | 7315 | if (NULLPTR_TYPE_P (intype)) |
6fe11077 | 7316 | return build_int_cst (type, 0); |
1a3f833b | 7317 | } |
6ab399e8 | 7318 | /* [expr.reinterpret.cast] |
7319 | A value of integral or enumeration type can be explicitly | |
7320 | converted to a pointer. */ | |
7321 | else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype)) | |
7322 | /* OK */ | |
7323 | ; | |
8eae84ad | 7324 | else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
05765a91 | 7325 | || TYPE_PTR_OR_PTRMEM_P (type)) |
8eae84ad | 7326 | && same_type_p (type, intype)) |
7327 | /* DR 799 */ | |
6800de3f | 7328 | return rvalue (expr); |
f9670f72 | 7329 | else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype)) |
7330 | || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) | |
d2c63826 | 7331 | return build_nop (type, expr); |
05765a91 | 7332 | else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype)) |
f9670f72 | 7333 | || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype))) |
1a3f833b | 7334 | { |
bcf22371 | 7335 | tree sexpr = expr; |
7336 | ||
6bac3cd5 | 7337 | if (!c_cast_p |
7338 | && check_for_casting_away_constness (intype, type, | |
7339 | REINTERPRET_CAST_EXPR, | |
7340 | complain)) | |
7341 | return error_mark_node; | |
6ab399e8 | 7342 | /* Warn about possible alignment problems. */ |
7ceee42d | 7343 | if ((STRICT_ALIGNMENT || warn_cast_align == 2) |
7344 | && (complain & tf_warning) | |
6ab399e8 | 7345 | && !VOID_TYPE_P (type) |
7346 | && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE | |
7347 | && COMPLETE_TYPE_P (TREE_TYPE (type)) | |
7348 | && COMPLETE_TYPE_P (TREE_TYPE (intype)) | |
7ceee42d | 7349 | && min_align_of_type (TREE_TYPE (type)) |
7350 | > min_align_of_type (TREE_TYPE (intype))) | |
4d1eda3a | 7351 | warning (OPT_Wcast_align, "cast from %qH to %qI " |
7ceee42d | 7352 | "increases required alignment of target type", intype, type); |
9031d10b | 7353 | |
a17c2a3a | 7354 | /* We need to strip nops here, because the front end likes to |
bcf22371 | 7355 | create (int *)&a for array-to-pointer decay, instead of &a[0]. */ |
7356 | STRIP_NOPS (sexpr); | |
e6fa0ea6 | 7357 | if (warn_strict_aliasing <= 2) |
7358 | strict_aliasing_warning (intype, type, sexpr); | |
bcf22371 | 7359 | |
d2c63826 | 7360 | return build_nop (type, expr); |
1a3f833b | 7361 | } |
241ce711 | 7362 | else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype)) |
02e19191 | 7363 | || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type))) |
241ce711 | 7364 | { |
839f2f70 | 7365 | if (complain & tf_warning) |
7366 | /* C++11 5.2.10 p8 says that "Converting a function pointer to an | |
7367 | object pointer type or vice versa is conditionally-supported." */ | |
7368 | warning (OPT_Wconditionally_supported, | |
7369 | "casting between pointer-to-function and pointer-to-object " | |
7370 | "is conditionally-supported"); | |
d2c63826 | 7371 | return build_nop (type, expr); |
241ce711 | 7372 | } |
76249021 | 7373 | else if (VECTOR_TYPE_P (type)) |
d2c63826 | 7374 | return convert_to_vector (type, expr); |
76249021 | 7375 | else if (VECTOR_TYPE_P (intype) |
b3353c23 | 7376 | && INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
d2c63826 | 7377 | return convert_to_integer_nofold (type, expr); |
f9670f72 | 7378 | else |
1a3f833b | 7379 | { |
6ab399e8 | 7380 | if (valid_p) |
7381 | *valid_p = false; | |
ebd21de4 | 7382 | if (complain & tf_error) |
7383 | error ("invalid cast from type %qT to type %qT", intype, type); | |
1a3f833b | 7384 | return error_mark_node; |
7385 | } | |
9031d10b | 7386 | |
c4698a21 | 7387 | return cp_convert (type, expr, complain); |
d81e00a4 | 7388 | } |
7389 | ||
bae9fce7 | 7390 | tree |
ebd21de4 | 7391 | build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain) |
d81e00a4 | 7392 | { |
62206d34 | 7393 | tree r; |
7394 | ||
ce28ee2e | 7395 | if (type == error_mark_node || expr == error_mark_node) |
7396 | return error_mark_node; | |
7397 | ||
3cc0b4b9 | 7398 | if (processing_template_decl) |
96624a9e | 7399 | { |
6ab399e8 | 7400 | tree t = build_min (REINTERPRET_CAST_EXPR, type, expr); |
9031d10b | 7401 | |
b6691ff5 | 7402 | if (!TREE_SIDE_EFFECTS (t) |
7403 | && type_dependent_expression_p (expr)) | |
7404 | /* There might turn out to be side effects inside expr. */ | |
7405 | TREE_SIDE_EFFECTS (t) = 1; | |
34da5a16 | 7406 | return convert_from_reference (t); |
96624a9e | 7407 | } |
7408 | ||
62206d34 | 7409 | r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false, |
7410 | /*valid_p=*/NULL, complain); | |
7411 | if (r != error_mark_node) | |
220a51b0 | 7412 | { |
7413 | maybe_warn_about_useless_cast (type, expr, complain); | |
7414 | maybe_warn_about_cast_ignoring_quals (type, complain); | |
7415 | } | |
62206d34 | 7416 | return r; |
6ab399e8 | 7417 | } |
7418 | ||
7419 | /* Perform a const_cast from EXPR to TYPE. If the cast is valid, | |
7420 | return an appropriate expression. Otherwise, return | |
7421 | error_mark_node. If the cast is not valid, and COMPLAIN is true, | |
e3fc4282 | 7422 | then a diagnostic will be issued. If VALID_P is non-NULL, we are |
7423 | performing a C-style cast, its value upon return will indicate | |
7424 | whether or not the conversion succeeded. */ | |
6ab399e8 | 7425 | |
7426 | static tree | |
6bac3cd5 | 7427 | build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain, |
6ab399e8 | 7428 | bool *valid_p) |
7429 | { | |
7430 | tree src_type; | |
7431 | tree reference_type; | |
7432 | ||
7433 | /* Callers are responsible for handling error_mark_node as a | |
7434 | destination type. */ | |
7435 | gcc_assert (dst_type != error_mark_node); | |
7436 | /* In a template, callers should be building syntactic | |
7437 | representations of casts, not using this machinery. */ | |
7438 | gcc_assert (!processing_template_decl); | |
7439 | ||
7440 | /* Assume the conversion is invalid. */ | |
7441 | if (valid_p) | |
7442 | *valid_p = false; | |
7443 | ||
05765a91 | 7444 | if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type)) |
6ab399e8 | 7445 | { |
6bac3cd5 | 7446 | if (complain & tf_error) |
6ab399e8 | 7447 | error ("invalid use of const_cast with type %qT, " |
7448 | "which is not a pointer, " | |
7449 | "reference, nor a pointer-to-data-member type", dst_type); | |
0cacb196 | 7450 | return error_mark_node; |
7451 | } | |
7452 | ||
6ab399e8 | 7453 | if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE) |
1a3f833b | 7454 | { |
6bac3cd5 | 7455 | if (complain & tf_error) |
6ab399e8 | 7456 | error ("invalid use of const_cast with type %qT, which is a pointer " |
7457 | "or reference to a function type", dst_type); | |
7458 | return error_mark_node; | |
1a3f833b | 7459 | } |
7460 | ||
220a51b0 | 7461 | /* A prvalue of non-class type is cv-unqualified. */ |
7462 | dst_type = cv_unqualified (dst_type); | |
7463 | ||
35df6eb4 | 7464 | /* Save casted types in the function's used types hash table. */ |
7465 | used_types_insert (dst_type); | |
7466 | ||
6ab399e8 | 7467 | src_type = TREE_TYPE (expr); |
7468 | /* Expressions do not really have reference types. */ | |
7469 | if (TREE_CODE (src_type) == REFERENCE_TYPE) | |
7470 | src_type = TREE_TYPE (src_type); | |
7471 | ||
7472 | /* [expr.const.cast] | |
7473 | ||
b32d1825 | 7474 | For two object types T1 and T2, if a pointer to T1 can be explicitly |
7475 | converted to the type "pointer to T2" using a const_cast, then the | |
7476 | following conversions can also be made: | |
7477 | ||
7478 | -- an lvalue of type T1 can be explicitly converted to an lvalue of | |
7479 | type T2 using the cast const_cast<T2&>; | |
7480 | ||
7481 | -- a glvalue of type T1 can be explicitly converted to an xvalue of | |
7482 | type T2 using the cast const_cast<T2&&>; and | |
7483 | ||
7484 | -- if T1 is a class type, a prvalue of type T1 can be explicitly | |
7485 | converted to an xvalue of type T2 using the cast const_cast<T2&&>. */ | |
7486 | ||
6ab399e8 | 7487 | if (TREE_CODE (dst_type) == REFERENCE_TYPE) |
1a3f833b | 7488 | { |
6ab399e8 | 7489 | reference_type = dst_type; |
b32d1825 | 7490 | if (!TYPE_REF_IS_RVALUE (dst_type) |
18bede74 | 7491 | ? lvalue_p (expr) |
049132bc | 7492 | : obvalue_p (expr)) |
b32d1825 | 7493 | /* OK. */; |
7494 | else | |
1a3f833b | 7495 | { |
6bac3cd5 | 7496 | if (complain & tf_error) |
6ab399e8 | 7497 | error ("invalid const_cast of an rvalue of type %qT to type %qT", |
7498 | src_type, dst_type); | |
1a3f833b | 7499 | return error_mark_node; |
7500 | } | |
6ab399e8 | 7501 | dst_type = build_pointer_type (TREE_TYPE (dst_type)); |
7502 | src_type = build_pointer_type (src_type); | |
7503 | } | |
7504 | else | |
7505 | { | |
7506 | reference_type = NULL_TREE; | |
7507 | /* If the destination type is not a reference type, the | |
7508 | lvalue-to-rvalue, array-to-pointer, and function-to-pointer | |
7509 | conversions are performed. */ | |
7510 | src_type = type_decays_to (src_type); | |
7511 | if (src_type == error_mark_node) | |
7512 | return error_mark_node; | |
7513 | } | |
1a3f833b | 7514 | |
05765a91 | 7515 | if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type)) |
6ab399e8 | 7516 | { |
609cab98 | 7517 | if (comp_ptr_ttypes_const (dst_type, src_type)) |
7975626b | 7518 | { |
609cab98 | 7519 | if (valid_p) |
7520 | { | |
7521 | *valid_p = true; | |
7522 | /* This cast is actually a C-style cast. Issue a warning if | |
7523 | the user is making a potentially unsafe cast. */ | |
7524 | check_for_casting_away_constness (src_type, dst_type, | |
7525 | CAST_EXPR, complain); | |
7ceee42d | 7526 | /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */ |
7527 | if ((STRICT_ALIGNMENT || warn_cast_align == 2) | |
7528 | && (complain & tf_warning) | |
7529 | && min_align_of_type (TREE_TYPE (dst_type)) | |
7530 | > min_align_of_type (TREE_TYPE (src_type))) | |
7531 | warning (OPT_Wcast_align, "cast from %qH to %qI " | |
7532 | "increases required alignment of target type", | |
7533 | src_type, dst_type); | |
609cab98 | 7534 | } |
7535 | if (reference_type) | |
7536 | { | |
7537 | expr = cp_build_addr_expr (expr, complain); | |
4405c1ad | 7538 | if (expr == error_mark_node) |
7539 | return error_mark_node; | |
609cab98 | 7540 | expr = build_nop (reference_type, expr); |
7541 | return convert_from_reference (expr); | |
7542 | } | |
7543 | else | |
7544 | { | |
4405c1ad | 7545 | expr = decay_conversion (expr, complain); |
7546 | if (expr == error_mark_node) | |
7547 | return error_mark_node; | |
7548 | ||
609cab98 | 7549 | /* build_c_cast puts on a NOP_EXPR to make the result not an |
7550 | lvalue. Strip such NOP_EXPRs if VALUE is being used in | |
7551 | non-lvalue context. */ | |
7552 | if (TREE_CODE (expr) == NOP_EXPR | |
7553 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
7554 | expr = TREE_OPERAND (expr, 0); | |
7555 | return build_nop (dst_type, expr); | |
7556 | } | |
6ab399e8 | 7557 | } |
609cab98 | 7558 | else if (valid_p |
7559 | && !at_least_as_qualified_p (TREE_TYPE (dst_type), | |
7560 | TREE_TYPE (src_type))) | |
7561 | check_for_casting_away_constness (src_type, dst_type, CAST_EXPR, | |
7562 | complain); | |
1a3f833b | 7563 | } |
7564 | ||
6bac3cd5 | 7565 | if (complain & tf_error) |
9031d10b | 7566 | error ("invalid const_cast from type %qT to type %qT", |
6ab399e8 | 7567 | src_type, dst_type); |
f9670f72 | 7568 | return error_mark_node; |
d81e00a4 | 7569 | } |
7570 | ||
6ab399e8 | 7571 | tree |
ebd21de4 | 7572 | build_const_cast (tree type, tree expr, tsubst_flags_t complain) |
6ab399e8 | 7573 | { |
62206d34 | 7574 | tree r; |
7575 | ||
2a03dcc3 | 7576 | if (type == error_mark_node || error_operand_p (expr)) |
6ab399e8 | 7577 | return error_mark_node; |
a74e8896 | 7578 | |
6ab399e8 | 7579 | if (processing_template_decl) |
7580 | { | |
7581 | tree t = build_min (CONST_CAST_EXPR, type, expr); | |
9031d10b | 7582 | |
6ab399e8 | 7583 | if (!TREE_SIDE_EFFECTS (t) |
7584 | && type_dependent_expression_p (expr)) | |
7585 | /* There might turn out to be side effects inside expr. */ | |
7586 | TREE_SIDE_EFFECTS (t) = 1; | |
34da5a16 | 7587 | return convert_from_reference (t); |
6ab399e8 | 7588 | } |
7589 | ||
62206d34 | 7590 | r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL); |
7591 | if (r != error_mark_node) | |
220a51b0 | 7592 | { |
7593 | maybe_warn_about_useless_cast (type, expr, complain); | |
7594 | maybe_warn_about_cast_ignoring_quals (type, complain); | |
7595 | } | |
62206d34 | 7596 | return r; |
6ab399e8 | 7597 | } |
7598 | ||
717ecce9 | 7599 | /* Like cp_build_c_cast, but for the c-common bits. */ |
7600 | ||
7601 | tree | |
a49c5913 | 7602 | build_c_cast (location_t /*loc*/, tree type, tree expr) |
717ecce9 | 7603 | { |
7604 | return cp_build_c_cast (type, expr, tf_warning_or_error); | |
7605 | } | |
7606 | ||
3d27a0fa | 7607 | /* Like the "build_c_cast" used for c-common, but using cp_expr to |
7608 | preserve location information even for tree nodes that don't | |
7609 | support it. */ | |
7610 | ||
7611 | cp_expr | |
7612 | build_c_cast (location_t loc, tree type, cp_expr expr) | |
7613 | { | |
7614 | cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error); | |
7615 | result.set_location (loc); | |
7616 | return result; | |
7617 | } | |
7618 | ||
6ab399e8 | 7619 | /* Build an expression representing an explicit C-style cast to type |
7620 | TYPE of expression EXPR. */ | |
471086d6 | 7621 | |
7622 | tree | |
717ecce9 | 7623 | cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain) |
471086d6 | 7624 | { |
cd16867a | 7625 | tree value = expr; |
6ab399e8 | 7626 | tree result; |
7627 | bool valid_p; | |
471086d6 | 7628 | |
6ab399e8 | 7629 | if (type == error_mark_node || error_operand_p (expr)) |
471086d6 | 7630 | return error_mark_node; |
7631 | ||
1c7b0075 | 7632 | if (processing_template_decl) |
7633 | { | |
7634 | tree t = build_min (CAST_EXPR, type, | |
7635 | tree_cons (NULL_TREE, value, NULL_TREE)); | |
bd8962d5 | 7636 | /* We don't know if it will or will not have side effects. */ |
b6691ff5 | 7637 | TREE_SIDE_EFFECTS (t) = 1; |
34da5a16 | 7638 | return convert_from_reference (t); |
1c7b0075 | 7639 | } |
7640 | ||
10928a2b | 7641 | /* Casts to a (pointer to a) specific ObjC class (or 'id' or |
7642 | 'Class') should always be retained, because this information aids | |
7643 | in method lookup. */ | |
7644 | if (objc_is_object_ptr (type) | |
7645 | && objc_is_object_ptr (TREE_TYPE (expr))) | |
7646 | return build_nop (type, expr); | |
7647 | ||
471086d6 | 7648 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
1a3f833b | 7649 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ |
7650 | if (TREE_CODE (type) != REFERENCE_TYPE | |
7651 | && TREE_CODE (value) == NOP_EXPR | |
471086d6 | 7652 | && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0))) |
7653 | value = TREE_OPERAND (value, 0); | |
7654 | ||
471086d6 | 7655 | if (TREE_CODE (type) == ARRAY_TYPE) |
7656 | { | |
7657 | /* Allow casting from T1* to T2[] because Cfront allows it. | |
657c76e1 | 7658 | NIHCL uses it. It is not valid ISO C++ however. */ |
c21c015b | 7659 | if (TYPE_PTR_P (TREE_TYPE (expr))) |
471086d6 | 7660 | { |
ebd21de4 | 7661 | if (complain & tf_error) |
2b9e3597 | 7662 | permerror (input_location, "ISO C++ forbids casting to an array type %qT", type); |
ebd21de4 | 7663 | else |
7664 | return error_mark_node; | |
471086d6 | 7665 | type = build_pointer_type (TREE_TYPE (type)); |
7666 | } | |
7667 | else | |
7668 | { | |
ebd21de4 | 7669 | if (complain & tf_error) |
7670 | error ("ISO C++ forbids casting to an array type %qT", type); | |
471086d6 | 7671 | return error_mark_node; |
7672 | } | |
7673 | } | |
7674 | ||
ddb9bca7 | 7675 | if (TREE_CODE (type) == FUNCTION_TYPE |
7676 | || TREE_CODE (type) == METHOD_TYPE) | |
7677 | { | |
ebd21de4 | 7678 | if (complain & tf_error) |
7679 | error ("invalid cast to function type %qT", type); | |
ddb9bca7 | 7680 | return error_mark_node; |
7681 | } | |
7682 | ||
c21c015b | 7683 | if (TYPE_PTR_P (type) |
bbecaa22 | 7684 | && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE |
7685 | /* Casting to an integer of smaller size is an error detected elsewhere. */ | |
7686 | && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value)) | |
7687 | /* Don't warn about converting any constant. */ | |
7688 | && !TREE_CONSTANT (value)) | |
7689 | warning_at (input_location, OPT_Wint_to_pointer_cast, | |
7690 | "cast to pointer from integer of different size"); | |
7691 | ||
6ab399e8 | 7692 | /* A C-style cast can be a const_cast. */ |
6bac3cd5 | 7693 | result = build_const_cast_1 (type, value, complain & tf_warning, |
6ab399e8 | 7694 | &valid_p); |
7695 | if (valid_p) | |
62206d34 | 7696 | { |
7697 | if (result != error_mark_node) | |
220a51b0 | 7698 | { |
7699 | maybe_warn_about_useless_cast (type, value, complain); | |
7700 | maybe_warn_about_cast_ignoring_quals (type, complain); | |
7701 | } | |
62206d34 | 7702 | return result; |
7703 | } | |
471086d6 | 7704 | |
6ab399e8 | 7705 | /* Or a static cast. */ |
7706 | result = build_static_cast_1 (type, value, /*c_cast_p=*/true, | |
ebd21de4 | 7707 | &valid_p, complain); |
6ab399e8 | 7708 | /* Or a reinterpret_cast. */ |
7709 | if (!valid_p) | |
7710 | result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true, | |
ebd21de4 | 7711 | &valid_p, complain); |
6ab399e8 | 7712 | /* The static_cast or reinterpret_cast may be followed by a |
7713 | const_cast. */ | |
9031d10b | 7714 | if (valid_p |
6ab399e8 | 7715 | /* A valid cast may result in errors if, for example, a |
62206d34 | 7716 | conversion to an ambiguous base class is required. */ |
6ab399e8 | 7717 | && !error_operand_p (result)) |
cc4d0855 | 7718 | { |
6ab399e8 | 7719 | tree result_type; |
1a3f833b | 7720 | |
62206d34 | 7721 | maybe_warn_about_useless_cast (type, value, complain); |
220a51b0 | 7722 | maybe_warn_about_cast_ignoring_quals (type, complain); |
62206d34 | 7723 | |
6ab399e8 | 7724 | /* Non-class rvalues always have cv-unqualified type. */ |
7725 | if (!CLASS_TYPE_P (type)) | |
7726 | type = TYPE_MAIN_VARIANT (type); | |
7727 | result_type = TREE_TYPE (result); | |
d295f848 | 7728 | if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE) |
6ab399e8 | 7729 | result_type = TYPE_MAIN_VARIANT (result_type); |
7730 | /* If the type of RESULT does not match TYPE, perform a | |
7731 | const_cast to make it match. If the static_cast or | |
7732 | reinterpret_cast succeeded, we will differ by at most | |
7733 | cv-qualification, so the follow-on const_cast is guaranteed | |
7734 | to succeed. */ | |
7735 | if (!same_type_p (non_reference (type), non_reference (result_type))) | |
cc4d0855 | 7736 | { |
6ab399e8 | 7737 | result = build_const_cast_1 (type, result, false, &valid_p); |
7738 | gcc_assert (valid_p); | |
471086d6 | 7739 | } |
6ab399e8 | 7740 | return result; |
471086d6 | 7741 | } |
7742 | ||
6ab399e8 | 7743 | return error_mark_node; |
471086d6 | 7744 | } |
7745 | \f | |
ebd21de4 | 7746 | /* For use from the C common bits. */ |
7747 | tree | |
22a3f7bd | 7748 | build_modify_expr (location_t location, |
a49c5913 | 7749 | tree lhs, tree /*lhs_origtype*/, |
e60a6f7b | 7750 | enum tree_code modifycode, |
a49c5913 | 7751 | location_t /*rhs_location*/, tree rhs, |
7752 | tree /*rhs_origtype*/) | |
ebd21de4 | 7753 | { |
22a3f7bd | 7754 | return cp_build_modify_expr (location, lhs, modifycode, rhs, |
7755 | tf_warning_or_error); | |
ebd21de4 | 7756 | } |
7757 | ||
471086d6 | 7758 | /* Build an assignment expression of lvalue LHS from value RHS. |
7759 | MODIFYCODE is the code for a binary operator that we use | |
7760 | to combine the old value of LHS with RHS to get the new value. | |
7761 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. | |
7762 | ||
96624a9e | 7763 | C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */ |
7764 | ||
471086d6 | 7765 | tree |
22a3f7bd | 7766 | cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, |
7767 | tree rhs, tsubst_flags_t complain) | |
471086d6 | 7768 | { |
c017458d | 7769 | lhs = mark_lvalue_use_nonread (lhs); |
7770 | ||
cd7580a0 | 7771 | tree result = NULL_TREE; |
471086d6 | 7772 | tree newrhs = rhs; |
7773 | tree lhstype = TREE_TYPE (lhs); | |
cd7580a0 | 7774 | tree olhs = lhs; |
471086d6 | 7775 | tree olhstype = lhstype; |
066f26bf | 7776 | bool plain_assign = (modifycode == NOP_EXPR); |
cd7580a0 | 7777 | bool compound_side_effects_p = false; |
7778 | tree preeval = NULL_TREE; | |
471086d6 | 7779 | |
471086d6 | 7780 | /* Avoid duplicate error messages from operands that had errors. */ |
02bae266 | 7781 | if (error_operand_p (lhs) || error_operand_p (rhs)) |
471086d6 | 7782 | return error_mark_node; |
7783 | ||
cd7580a0 | 7784 | while (TREE_CODE (lhs) == COMPOUND_EXPR) |
7785 | { | |
7786 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))) | |
7787 | compound_side_effects_p = true; | |
7788 | lhs = TREE_OPERAND (lhs, 1); | |
7789 | } | |
7790 | ||
176aa551 | 7791 | /* Handle control structure constructs used as "lvalues". Note that we |
7792 | leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */ | |
471086d6 | 7793 | switch (TREE_CODE (lhs)) |
7794 | { | |
331bc0ad | 7795 | /* Handle --foo = 5; as these are valid constructs in C++. */ |
471086d6 | 7796 | case PREDECREMENT_EXPR: |
7797 | case PREINCREMENT_EXPR: | |
cd7580a0 | 7798 | if (compound_side_effects_p) |
7799 | newrhs = rhs = stabilize_expr (rhs, &preeval); | |
471086d6 | 7800 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))) |
831d52a2 | 7801 | lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs), |
514727a9 | 7802 | cp_stabilize_reference (TREE_OPERAND (lhs, 0)), |
831d52a2 | 7803 | TREE_OPERAND (lhs, 1)); |
176aa551 | 7804 | lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0)); |
cd7580a0 | 7805 | maybe_add_compound: |
7806 | /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5; | |
7807 | and looked through the COMPOUND_EXPRs, readd them now around | |
7808 | the resulting lhs. */ | |
7809 | if (TREE_CODE (olhs) == COMPOUND_EXPR) | |
7810 | { | |
7811 | lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs); | |
7812 | tree *ptr = &TREE_OPERAND (lhs, 1); | |
7813 | for (olhs = TREE_OPERAND (olhs, 1); | |
7814 | TREE_CODE (olhs) == COMPOUND_EXPR; | |
7815 | olhs = TREE_OPERAND (olhs, 1)) | |
7816 | { | |
7817 | *ptr = build2 (COMPOUND_EXPR, lhstype, | |
7818 | TREE_OPERAND (olhs, 0), *ptr); | |
7819 | ptr = &TREE_OPERAND (*ptr, 1); | |
7820 | } | |
7821 | } | |
176aa551 | 7822 | break; |
471086d6 | 7823 | |
d81e00a4 | 7824 | case MODIFY_EXPR: |
cd7580a0 | 7825 | if (compound_side_effects_p) |
7826 | newrhs = rhs = stabilize_expr (rhs, &preeval); | |
e62f1d3a | 7827 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))) |
831d52a2 | 7828 | lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs), |
514727a9 | 7829 | cp_stabilize_reference (TREE_OPERAND (lhs, 0)), |
831d52a2 | 7830 | TREE_OPERAND (lhs, 1)); |
176aa551 | 7831 | lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0)); |
cd7580a0 | 7832 | goto maybe_add_compound; |
d81e00a4 | 7833 | |
79f25e9a | 7834 | case MIN_EXPR: |
7835 | case MAX_EXPR: | |
7836 | /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues, | |
7837 | when neither operand has side-effects. */ | |
ebd21de4 | 7838 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
79f25e9a | 7839 | return error_mark_node; |
7840 | ||
7841 | gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)) | |
7842 | && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1))); | |
7843 | ||
7844 | lhs = build3 (COND_EXPR, TREE_TYPE (lhs), | |
7845 | build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR, | |
7846 | boolean_type_node, | |
7847 | TREE_OPERAND (lhs, 0), | |
7848 | TREE_OPERAND (lhs, 1)), | |
7849 | TREE_OPERAND (lhs, 0), | |
7850 | TREE_OPERAND (lhs, 1)); | |
3c77f69c | 7851 | gcc_fallthrough (); |
79f25e9a | 7852 | |
471086d6 | 7853 | /* Handle (a ? b : c) used as an "lvalue". */ |
7854 | case COND_EXPR: | |
471086d6 | 7855 | { |
7856 | /* Produce (a ? (b = rhs) : (c = rhs)) | |
7857 | except that the RHS goes through a save-expr | |
7858 | so the code to compute it is only emitted once. */ | |
f3bb5d4e | 7859 | tree cond; |
7860 | ||
76008ac4 | 7861 | if (VOID_TYPE_P (TREE_TYPE (rhs))) |
7862 | { | |
ebd21de4 | 7863 | if (complain & tf_error) |
7864 | error ("void value not ignored as it ought to be"); | |
76008ac4 | 7865 | return error_mark_node; |
7866 | } | |
7867 | ||
c3f16ae3 | 7868 | rhs = stabilize_expr (rhs, &preeval); |
9031d10b | 7869 | |
f3bb5d4e | 7870 | /* Check this here to avoid odd errors when trying to convert |
7871 | a throw to the type of the COND_EXPR. */ | |
ebd21de4 | 7872 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
f3bb5d4e | 7873 | return error_mark_node; |
7874 | ||
7875 | cond = build_conditional_expr | |
1273a0b4 | 7876 | (input_location, TREE_OPERAND (lhs, 0), |
22a3f7bd | 7877 | cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1), |
ebd21de4 | 7878 | modifycode, rhs, complain), |
22a3f7bd | 7879 | cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2), |
ebd21de4 | 7880 | modifycode, rhs, complain), |
7881 | complain); | |
f3bb5d4e | 7882 | |
b465397d | 7883 | if (cond == error_mark_node) |
471086d6 | 7884 | return cond; |
cd7580a0 | 7885 | /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d; |
7886 | and looked through the COMPOUND_EXPRs, readd them now around | |
7887 | the resulting cond before adding the preevaluated rhs. */ | |
7888 | if (TREE_CODE (olhs) == COMPOUND_EXPR) | |
7889 | { | |
7890 | cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond), | |
7891 | TREE_OPERAND (olhs, 0), cond); | |
7892 | tree *ptr = &TREE_OPERAND (cond, 1); | |
7893 | for (olhs = TREE_OPERAND (olhs, 1); | |
7894 | TREE_CODE (olhs) == COMPOUND_EXPR; | |
7895 | olhs = TREE_OPERAND (olhs, 1)) | |
7896 | { | |
7897 | *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond), | |
7898 | TREE_OPERAND (olhs, 0), *ptr); | |
7899 | ptr = &TREE_OPERAND (*ptr, 1); | |
7900 | } | |
7901 | } | |
471086d6 | 7902 | /* Make sure the code to compute the rhs comes out |
7903 | before the split. */ | |
cd7580a0 | 7904 | result = cond; |
7905 | goto ret; | |
471086d6 | 7906 | } |
9031d10b | 7907 | |
d913511e | 7908 | default: |
cd7580a0 | 7909 | lhs = olhs; |
d913511e | 7910 | break; |
471086d6 | 7911 | } |
7912 | ||
471086d6 | 7913 | if (modifycode == INIT_EXPR) |
7914 | { | |
ce734c95 | 7915 | if (BRACE_ENCLOSED_INITIALIZER_P (rhs)) |
7916 | /* Do the default thing. */; | |
7917 | else if (TREE_CODE (rhs) == CONSTRUCTOR) | |
a3691386 | 7918 | { |
ce734c95 | 7919 | /* Compound literal. */ |
08b91c0b | 7920 | if (! same_type_p (TREE_TYPE (rhs), lhstype)) |
7921 | /* Call convert to generate an error; see PR 11063. */ | |
7922 | rhs = convert (lhstype, rhs); | |
831d52a2 | 7923 | result = build2 (INIT_EXPR, lhstype, lhs, rhs); |
a3691386 | 7924 | TREE_SIDE_EFFECTS (result) = 1; |
cd7580a0 | 7925 | goto ret; |
a3691386 | 7926 | } |
95397ff9 | 7927 | else if (! MAYBE_CLASS_TYPE_P (lhstype)) |
331bc0ad | 7928 | /* Do the default thing. */; |
c25194fd | 7929 | else |
471086d6 | 7930 | { |
f1f41a6c | 7931 | vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs); |
f70cb9e6 | 7932 | result = build_special_member_call (lhs, complete_ctor_identifier, |
f352a3fb | 7933 | &rhs_vec, lhstype, LOOKUP_NORMAL, |
ebd21de4 | 7934 | complain); |
f352a3fb | 7935 | release_tree_vector (rhs_vec); |
471086d6 | 7936 | if (result == NULL_TREE) |
7937 | return error_mark_node; | |
cd7580a0 | 7938 | goto ret; |
471086d6 | 7939 | } |
7940 | } | |
e6d125f7 | 7941 | else |
471086d6 | 7942 | { |
dba1ea2f | 7943 | lhs = require_complete_type_sfinae (lhs, complain); |
e6d125f7 | 7944 | if (lhs == error_mark_node) |
7945 | return error_mark_node; | |
7946 | ||
7947 | if (modifycode == NOP_EXPR) | |
1e28ccf1 | 7948 | { |
86c110ac | 7949 | if (c_dialect_objc ()) |
7950 | { | |
9d9f5bb3 | 7951 | result = objc_maybe_build_modify_expr (lhs, rhs); |
86c110ac | 7952 | if (result) |
cd7580a0 | 7953 | goto ret; |
86c110ac | 7954 | } |
7955 | ||
e6d125f7 | 7956 | /* `operator=' is not an inheritable operator. */ |
95397ff9 | 7957 | if (! MAYBE_CLASS_TYPE_P (lhstype)) |
331bc0ad | 7958 | /* Do the default thing. */; |
e6d125f7 | 7959 | else |
7960 | { | |
ef0b0c72 | 7961 | result = build_new_op (input_location, MODIFY_EXPR, |
7962 | LOOKUP_NORMAL, lhs, rhs, | |
7963 | make_node (NOP_EXPR), /*overload=*/NULL, | |
ebd21de4 | 7964 | complain); |
e6d125f7 | 7965 | if (result == NULL_TREE) |
7966 | return error_mark_node; | |
cd7580a0 | 7967 | goto ret; |
e6d125f7 | 7968 | } |
7969 | lhstype = olhstype; | |
7970 | } | |
7971 | else | |
7972 | { | |
3115bda0 | 7973 | tree init = NULL_TREE; |
7974 | ||
e6d125f7 | 7975 | /* A binary op has been requested. Combine the old LHS |
653e5405 | 7976 | value with the RHS producing the value we should actually |
7977 | store into the LHS. */ | |
95397ff9 | 7978 | gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE |
7979 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype))) | |
7980 | || MAYBE_CLASS_TYPE_P (lhstype))); | |
e6d125f7 | 7981 | |
3115bda0 | 7982 | /* Preevaluate the RHS to make sure its evaluation is complete |
7983 | before the lvalue-to-rvalue conversion of the LHS: | |
7984 | ||
7985 | [expr.ass] With respect to an indeterminately-sequenced | |
7986 | function call, the operation of a compound assignment is a | |
7987 | single evaluation. [ Note: Therefore, a function call shall | |
7988 | not intervene between the lvalue-to-rvalue conversion and the | |
7989 | side effect associated with any single compound assignment | |
7990 | operator. -- end note ] */ | |
514727a9 | 7991 | lhs = cp_stabilize_reference (lhs); |
caccde3c | 7992 | rhs = rvalue (rhs); |
c017458d | 7993 | if (rhs == error_mark_node) |
7994 | return error_mark_node; | |
3115bda0 | 7995 | rhs = stabilize_expr (rhs, &init); |
22a3f7bd | 7996 | newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain); |
e6d125f7 | 7997 | if (newrhs == error_mark_node) |
7998 | { | |
ebd21de4 | 7999 | if (complain & tf_error) |
8000 | error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode, | |
8001 | TREE_TYPE (lhs), TREE_TYPE (rhs)); | |
e6d125f7 | 8002 | return error_mark_node; |
8003 | } | |
9031d10b | 8004 | |
3115bda0 | 8005 | if (init) |
8006 | newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs); | |
8007 | ||
e6d125f7 | 8008 | /* Now it looks like a plain assignment. */ |
8009 | modifycode = NOP_EXPR; | |
86c110ac | 8010 | if (c_dialect_objc ()) |
8011 | { | |
9d9f5bb3 | 8012 | result = objc_maybe_build_modify_expr (lhs, newrhs); |
86c110ac | 8013 | if (result) |
cd7580a0 | 8014 | goto ret; |
86c110ac | 8015 | } |
1e28ccf1 | 8016 | } |
b4df430b | 8017 | gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE); |
8018 | gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE); | |
471086d6 | 8019 | } |
8020 | ||
16bd3ffe | 8021 | /* The left-hand side must be an lvalue. */ |
ebd21de4 | 8022 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
471086d6 | 8023 | return error_mark_node; |
8024 | ||
e6d125f7 | 8025 | /* Warn about modifying something that is `const'. Don't warn if |
8026 | this is initialization. */ | |
471086d6 | 8027 | if (modifycode != INIT_EXPR |
3e04bd45 | 8028 | && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype) |
938b347c | 8029 | /* Functions are not modifiable, even though they are |
8030 | lvalues. */ | |
8031 | || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE | |
e6d125f7 | 8032 | || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE |
8033 | /* If it's an aggregate and any field is const, then it is | |
8034 | effectively const. */ | |
7f77d1f0 | 8035 | || (CLASS_TYPE_P (lhstype) |
e6d125f7 | 8036 | && C_TYPE_FIELDS_READONLY (lhstype)))) |
ebd21de4 | 8037 | { |
8038 | if (complain & tf_error) | |
a1f90215 | 8039 | cxx_readonly_error (lhs, lv_assign); |
ebd21de4 | 8040 | else |
8041 | return error_mark_node; | |
8042 | } | |
471086d6 | 8043 | |
0c4abe5b | 8044 | /* If storing into a structure or union member, it may have been given a |
8045 | lowered bitfield type. We need to convert to the declared type first, | |
8046 | so retrieve it now. */ | |
471086d6 | 8047 | |
0c4abe5b | 8048 | olhstype = unlowered_expr_type (lhs); |
471086d6 | 8049 | |
471086d6 | 8050 | /* Convert new value to destination type. */ |
8051 | ||
8052 | if (TREE_CODE (lhstype) == ARRAY_TYPE) | |
8053 | { | |
c38086bd | 8054 | int from_array; |
9031d10b | 8055 | |
58184926 | 8056 | if (BRACE_ENCLOSED_INITIALIZER_P (newrhs)) |
ffda1768 | 8057 | { |
4e88e128 | 8058 | if (modifycode != INIT_EXPR) |
8059 | { | |
8060 | if (complain & tf_error) | |
8061 | error ("assigning to an array from an initializer list"); | |
8062 | return error_mark_node; | |
8063 | } | |
58184926 | 8064 | if (check_array_initializer (lhs, lhstype, newrhs)) |
ffda1768 | 8065 | return error_mark_node; |
b52bd4ae | 8066 | newrhs = digest_init (lhstype, newrhs, complain); |
ca338b27 | 8067 | if (newrhs == error_mark_node) |
8068 | return error_mark_node; | |
ffda1768 | 8069 | } |
a8fe6bf4 | 8070 | |
40196b78 | 8071 | /* C++11 8.5/17: "If the destination type is an array of characters, |
8072 | an array of char16_t, an array of char32_t, or an array of wchar_t, | |
8073 | and the initializer is a string literal...". */ | |
8074 | else if (TREE_CODE (newrhs) == STRING_CST | |
8075 | && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype))) | |
8076 | && modifycode == INIT_EXPR) | |
8077 | { | |
8078 | newrhs = digest_init (lhstype, newrhs, complain); | |
8079 | if (newrhs == error_mark_node) | |
8080 | return error_mark_node; | |
8081 | } | |
8082 | ||
a8fe6bf4 | 8083 | else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype), |
58184926 | 8084 | TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))) |
dc76caae | 8085 | { |
ebd21de4 | 8086 | if (complain & tf_error) |
8087 | error ("incompatible types in assignment of %qT to %qT", | |
8088 | TREE_TYPE (rhs), lhstype); | |
dc76caae | 8089 | return error_mark_node; |
8090 | } | |
8091 | ||
bc3887cf | 8092 | /* Allow array assignment in compiler-generated code. */ |
a8fe6bf4 | 8093 | else if (!current_function_decl |
d4e7527c | 8094 | || !DECL_DEFAULTED_FN (current_function_decl)) |
2948e011 | 8095 | { |
74a3991f | 8096 | /* This routine is used for both initialization and assignment. |
8097 | Make sure the diagnostic message differentiates the context. */ | |
ebd21de4 | 8098 | if (complain & tf_error) |
8099 | { | |
8100 | if (modifycode == INIT_EXPR) | |
8101 | error ("array used as initializer"); | |
8102 | else | |
8103 | error ("invalid array assignment"); | |
8104 | } | |
2948e011 | 8105 | return error_mark_node; |
8106 | } | |
bc3887cf | 8107 | |
c38086bd | 8108 | from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE |
653e5405 | 8109 | ? 1 + (modifycode != INIT_EXPR): 0; |
cd7580a0 | 8110 | result = build_vec_init (lhs, NULL_TREE, newrhs, |
8111 | /*explicit_value_init_p=*/false, | |
8112 | from_array, complain); | |
8113 | goto ret; | |
471086d6 | 8114 | } |
8115 | ||
8116 | if (modifycode == INIT_EXPR) | |
cf7aa2e5 | 8117 | /* Calls with INIT_EXPR are all direct-initialization, so don't set |
8118 | LOOKUP_ONLYCONVERTING. */ | |
0c4abe5b | 8119 | newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL, |
183407ee | 8120 | ICR_INIT, NULL_TREE, 0, |
ebd21de4 | 8121 | complain); |
471086d6 | 8122 | else |
183407ee | 8123 | newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN, |
cf7aa2e5 | 8124 | NULL_TREE, 0, complain, LOOKUP_IMPLICIT); |
0c4abe5b | 8125 | |
8126 | if (!same_type_p (lhstype, olhstype)) | |
c4698a21 | 8127 | newrhs = cp_convert_and_check (lhstype, newrhs, complain); |
0c4abe5b | 8128 | |
8129 | if (modifycode != INIT_EXPR) | |
471086d6 | 8130 | { |
1e66592c | 8131 | if (TREE_CODE (newrhs) == CALL_EXPR |
8132 | && TYPE_NEEDS_CONSTRUCTING (lhstype)) | |
3d4bed93 | 8133 | newrhs = build_cplus_new (lhstype, newrhs, complain); |
1e66592c | 8134 | |
3f7d79e4 | 8135 | /* Can't initialize directly from a TARGET_EXPR, since that would |
2fbef0e3 | 8136 | cause the lhs to be constructed twice, and possibly result in |
8137 | accidental self-initialization. So we force the TARGET_EXPR to be | |
652e1a2d | 8138 | expanded without a target. */ |
1e66592c | 8139 | if (TREE_CODE (newrhs) == TARGET_EXPR) |
831d52a2 | 8140 | newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs, |
8141 | TREE_OPERAND (newrhs, 0)); | |
471086d6 | 8142 | } |
8143 | ||
b465397d | 8144 | if (newrhs == error_mark_node) |
471086d6 | 8145 | return error_mark_node; |
8146 | ||
7a4e126b | 8147 | if (c_dialect_objc () && flag_objc_gc) |
8148 | { | |
8149 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
8150 | ||
8151 | if (result) | |
cd7580a0 | 8152 | goto ret; |
7a4e126b | 8153 | } |
8154 | ||
831d52a2 | 8155 | result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR, |
8156 | lhstype, lhs, newrhs); | |
de9554eb | 8157 | |
471086d6 | 8158 | TREE_SIDE_EFFECTS (result) = 1; |
066f26bf | 8159 | if (!plain_assign) |
8160 | TREE_NO_WARNING (result) = 1; | |
471086d6 | 8161 | |
cd7580a0 | 8162 | ret: |
8163 | if (preeval) | |
8164 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result); | |
0c4abe5b | 8165 | return result; |
471086d6 | 8166 | } |
8167 | ||
3d27a0fa | 8168 | cp_expr |
255b5d15 | 8169 | build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, |
8170 | tree rhs, tsubst_flags_t complain) | |
e857e9c7 | 8171 | { |
d175f019 | 8172 | tree orig_lhs = lhs; |
8173 | tree orig_rhs = rhs; | |
8174 | tree overload = NULL_TREE; | |
8175 | tree op = build_nt (modifycode, NULL_TREE, NULL_TREE); | |
8176 | ||
3cc0b4b9 | 8177 | if (processing_template_decl) |
d175f019 | 8178 | { |
8179 | if (modifycode == NOP_EXPR | |
8180 | || type_dependent_expression_p (lhs) | |
8181 | || type_dependent_expression_p (rhs)) | |
8182 | return build_min_nt_loc (loc, MODOP_EXPR, lhs, | |
8183 | build_min_nt_loc (loc, modifycode, NULL_TREE, | |
8184 | NULL_TREE), rhs); | |
8185 | ||
8186 | lhs = build_non_dependent_expr (lhs); | |
8187 | rhs = build_non_dependent_expr (rhs); | |
8188 | } | |
e857e9c7 | 8189 | |
8190 | if (modifycode != NOP_EXPR) | |
8191 | { | |
d175f019 | 8192 | tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, |
8193 | lhs, rhs, op, &overload, complain); | |
e857e9c7 | 8194 | if (rval) |
066f26bf | 8195 | { |
d175f019 | 8196 | if (rval == error_mark_node) |
8197 | return rval; | |
066f26bf | 8198 | TREE_NO_WARNING (rval) = 1; |
d175f019 | 8199 | if (processing_template_decl) |
8200 | { | |
8201 | if (overload != NULL_TREE) | |
8202 | return (build_min_non_dep_op_overload | |
8203 | (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs)); | |
8204 | ||
8205 | return (build_min_non_dep | |
8206 | (MODOP_EXPR, rval, orig_lhs, op, orig_rhs)); | |
8207 | } | |
066f26bf | 8208 | return rval; |
8209 | } | |
e857e9c7 | 8210 | } |
22a3f7bd | 8211 | return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain); |
e857e9c7 | 8212 | } |
471086d6 | 8213 | |
e5ead12a | 8214 | /* Helper function for get_delta_difference which assumes FROM is a base |
8215 | class of TO. Returns a delta for the conversion of pointer-to-member | |
85132186 | 8216 | of FROM to pointer-to-member of TO. If the conversion is invalid and |
8217 | tf_error is not set in COMPLAIN returns error_mark_node, otherwise | |
e5ead12a | 8218 | returns zero. If FROM is not a base class of TO, returns NULL_TREE. |
85132186 | 8219 | If C_CAST_P is true, this conversion is taking place as part of a |
8220 | C-style cast. */ | |
e5ead12a | 8221 | |
8222 | static tree | |
85132186 | 8223 | get_delta_difference_1 (tree from, tree to, bool c_cast_p, |
8224 | tsubst_flags_t complain) | |
e5ead12a | 8225 | { |
8226 | tree binfo; | |
8227 | base_kind kind; | |
85132186 | 8228 | |
ae260dcc | 8229 | binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check, |
8230 | &kind, complain); | |
85132186 | 8231 | |
ae260dcc | 8232 | if (binfo == error_mark_node) |
e5ead12a | 8233 | { |
85132186 | 8234 | if (!(complain & tf_error)) |
8235 | return error_mark_node; | |
8236 | ||
e5ead12a | 8237 | error (" in pointer to member function conversion"); |
8238 | return size_zero_node; | |
8239 | } | |
8240 | else if (binfo) | |
8241 | { | |
8242 | if (kind != bk_via_virtual) | |
8243 | return BINFO_OFFSET (binfo); | |
8244 | else | |
8245 | /* FROM is a virtual base class of TO. Issue an error or warning | |
8246 | depending on whether or not this is a reinterpret cast. */ | |
8247 | { | |
85132186 | 8248 | if (!(complain & tf_error)) |
8249 | return error_mark_node; | |
8250 | ||
e5ead12a | 8251 | error ("pointer to member conversion via virtual base %qT", |
8252 | BINFO_TYPE (binfo_from_vbase (binfo))); | |
8253 | ||
8254 | return size_zero_node; | |
8255 | } | |
8256 | } | |
85132186 | 8257 | else |
8258 | return NULL_TREE; | |
e5ead12a | 8259 | } |
8260 | ||
01cc3b75 | 8261 | /* Get difference in deltas for different pointer to member function |
85132186 | 8262 | types. If the conversion is invalid and tf_error is not set in |
8263 | COMPLAIN, returns error_mark_node, otherwise returns an integer | |
8264 | constant of type PTRDIFF_TYPE_NODE and its value is zero if the | |
8265 | conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse | |
8266 | conversions as well. If C_CAST_P is true this conversion is taking | |
8267 | place as part of a C-style cast. | |
e54ba4f1 | 8268 | |
8269 | Note that the naming of FROM and TO is kind of backwards; the return | |
8270 | value is what we add to a TO in order to get a FROM. They are named | |
8271 | this way because we call this function to find out how to convert from | |
8272 | a pointer to member of FROM to a pointer to member of TO. */ | |
96624a9e | 8273 | |
ac9386a0 | 8274 | static tree |
9031d10b | 8275 | get_delta_difference (tree from, tree to, |
cb02169c | 8276 | bool allow_inverse_p, |
85132186 | 8277 | bool c_cast_p, tsubst_flags_t complain) |
ac9386a0 | 8278 | { |
a7169c34 | 8279 | tree result; |
8280 | ||
e5ead12a | 8281 | if (same_type_ignoring_top_level_qualifiers_p (from, to)) |
8282 | /* Pointer to member of incomplete class is permitted*/ | |
8283 | result = size_zero_node; | |
7debc2bc | 8284 | else |
85132186 | 8285 | result = get_delta_difference_1 (from, to, c_cast_p, complain); |
8286 | ||
8287 | if (result == error_mark_node) | |
8288 | return error_mark_node; | |
9031d10b | 8289 | |
e5ead12a | 8290 | if (!result) |
8291 | { | |
8292 | if (!allow_inverse_p) | |
8293 | { | |
85132186 | 8294 | if (!(complain & tf_error)) |
8295 | return error_mark_node; | |
8296 | ||
e5ead12a | 8297 | error_not_base_type (from, to); |
8298 | error (" in pointer to member conversion"); | |
85132186 | 8299 | result = size_zero_node; |
e5ead12a | 8300 | } |
8301 | else | |
8302 | { | |
85132186 | 8303 | result = get_delta_difference_1 (to, from, c_cast_p, complain); |
8304 | ||
8305 | if (result == error_mark_node) | |
8306 | return error_mark_node; | |
e5ead12a | 8307 | |
8308 | if (result) | |
389dd41b | 8309 | result = size_diffop_loc (input_location, |
85132186 | 8310 | size_zero_node, result); |
e5ead12a | 8311 | else |
8312 | { | |
85132186 | 8313 | if (!(complain & tf_error)) |
8314 | return error_mark_node; | |
8315 | ||
e5ead12a | 8316 | error_not_base_type (from, to); |
8317 | error (" in pointer to member conversion"); | |
8318 | result = size_zero_node; | |
8319 | } | |
8320 | } | |
8321 | } | |
e4ba3cac | 8322 | |
d2c63826 | 8323 | return convert_to_integer (ptrdiff_type_node, result); |
ac9386a0 | 8324 | } |
8325 | ||
63a6a423 | 8326 | /* Return a constructor for the pointer-to-member-function TYPE using |
8327 | the other components as specified. */ | |
8328 | ||
1c16607c | 8329 | tree |
f0dd33b5 | 8330 | build_ptrmemfunc1 (tree type, tree delta, tree pfn) |
1d321d15 | 8331 | { |
991ab8f3 | 8332 | tree u = NULL_TREE; |
63a6a423 | 8333 | tree delta_field; |
63a6a423 | 8334 | tree pfn_field; |
f1f41a6c | 8335 | vec<constructor_elt, va_gc> *v; |
1d321d15 | 8336 | |
63a6a423 | 8337 | /* Pull the FIELD_DECLs out of the type. */ |
606b494c | 8338 | pfn_field = TYPE_FIELDS (type); |
1767a056 | 8339 | delta_field = DECL_CHAIN (pfn_field); |
1d321d15 | 8340 | |
63a6a423 | 8341 | /* Make sure DELTA has the type we want. */ |
22a75734 | 8342 | delta = convert_and_check (input_location, delta_type_node, delta); |
1d321d15 | 8343 | |
f607c97b | 8344 | /* Convert to the correct target type if necessary. */ |
8345 | pfn = fold_convert (TREE_TYPE (pfn_field), pfn); | |
8346 | ||
63a6a423 | 8347 | /* Finish creating the initializer. */ |
f1f41a6c | 8348 | vec_alloc (v, 2); |
c75b4594 | 8349 | CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn); |
8350 | CONSTRUCTOR_APPEND_ELT(v, delta_field, delta); | |
8351 | u = build_constructor (type, v); | |
4ee9c684 | 8352 | TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta); |
d1cabdaf | 8353 | TREE_STATIC (u) = (TREE_CONSTANT (u) |
8354 | && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn)) | |
8355 | != NULL_TREE) | |
9031d10b | 8356 | && (initializer_constant_valid_p (delta, TREE_TYPE (delta)) |
d1cabdaf | 8357 | != NULL_TREE)); |
1d321d15 | 8358 | return u; |
1d321d15 | 8359 | } |
8360 | ||
471086d6 | 8361 | /* Build a constructor for a pointer to member function. It can be |
8362 | used to initialize global variables, local variable, or used | |
8363 | as a value in expressions. TYPE is the POINTER to METHOD_TYPE we | |
8364 | want to be. | |
8365 | ||
3160db1d | 8366 | If FORCE is nonzero, then force this conversion, even if |
471086d6 | 8367 | we would rather not do it. Usually set when using an explicit |
cb02169c | 8368 | cast. A C-style cast is being processed iff C_CAST_P is true. |
ac9386a0 | 8369 | |
8370 | Return error_mark_node, if something goes wrong. */ | |
471086d6 | 8371 | |
8372 | tree | |
85132186 | 8373 | build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p, |
8374 | tsubst_flags_t complain) | |
471086d6 | 8375 | { |
1c16607c | 8376 | tree fn; |
8999978b | 8377 | tree pfn_type; |
8378 | tree to_type; | |
8379 | ||
8380 | if (error_operand_p (pfn)) | |
8381 | return error_mark_node; | |
8382 | ||
8383 | pfn_type = TREE_TYPE (pfn); | |
8384 | to_type = build_ptrmemfunc_type (type); | |
a17aefa2 | 8385 | |
96624a9e | 8386 | /* Handle multiple conversions of pointer to member functions. */ |
8999978b | 8387 | if (TYPE_PTRMEMFUNC_P (pfn_type)) |
471086d6 | 8388 | { |
d1cabdaf | 8389 | tree delta = NULL_TREE; |
aa74f96b | 8390 | tree npfn = NULL_TREE; |
606b494c | 8391 | tree n; |
1d321d15 | 8392 | |
9031d10b | 8393 | if (!force |
66bbeb85 | 8394 | && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, |
8395 | LOOKUP_NORMAL, complain)) | |
19b271a9 | 8396 | { |
8397 | if (complain & tf_error) | |
8398 | error ("invalid conversion to type %qT from type %qT", | |
8399 | to_type, pfn_type); | |
8400 | else | |
8401 | return error_mark_node; | |
8402 | } | |
f215734b | 8403 | |
e90f4ee9 | 8404 | n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type), |
8405 | TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type), | |
cb02169c | 8406 | force, |
85132186 | 8407 | c_cast_p, complain); |
8408 | if (n == error_mark_node) | |
8409 | return error_mark_node; | |
e90f4ee9 | 8410 | |
63a6a423 | 8411 | /* We don't have to do any conversion to convert a |
8412 | pointer-to-member to its own type. But, we don't want to | |
8413 | just return a PTRMEM_CST if there's an explicit cast; that | |
8414 | cast should make the expression an invalid template argument. */ | |
e90f4ee9 | 8415 | if (TREE_CODE (pfn) != PTRMEM_CST) |
8416 | { | |
8417 | if (same_type_p (to_type, pfn_type)) | |
8418 | return pfn; | |
8419 | else if (integer_zerop (n)) | |
ebd21de4 | 8420 | return build_reinterpret_cast (to_type, pfn, |
34f12878 | 8421 | complain); |
e90f4ee9 | 8422 | } |
63a6a423 | 8423 | |
3beda4b4 | 8424 | if (TREE_SIDE_EFFECTS (pfn)) |
8425 | pfn = save_expr (pfn); | |
8426 | ||
d1cabdaf | 8427 | /* Obtain the function pointer and the current DELTA. */ |
8428 | if (TREE_CODE (pfn) == PTRMEM_CST) | |
d75b8a68 | 8429 | expand_ptrmemfunc_cst (pfn, &delta, &npfn); |
d1cabdaf | 8430 | else |
8431 | { | |
4ac852cb | 8432 | npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier); |
8433 | delta = build_ptrmemfunc_access_expr (pfn, delta_identifier); | |
d1cabdaf | 8434 | } |
8435 | ||
5ad590ad | 8436 | /* Just adjust the DELTA field. */ |
b4df430b | 8437 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
8438 | (TREE_TYPE (delta), ptrdiff_type_node)); | |
5e89f6fb | 8439 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta) |
8e70fb09 | 8440 | n = cp_build_binary_op (input_location, |
8441 | LSHIFT_EXPR, n, integer_one_node, | |
34f12878 | 8442 | complain); |
8e70fb09 | 8443 | delta = cp_build_binary_op (input_location, |
34f12878 | 8444 | PLUS_EXPR, delta, n, complain); |
d75b8a68 | 8445 | return build_ptrmemfunc1 (to_type, delta, npfn); |
471086d6 | 8446 | } |
ac9386a0 | 8447 | |
96624a9e | 8448 | /* Handle null pointer to member function conversions. */ |
ef434176 | 8449 | if (null_ptr_cst_p (pfn)) |
ac9386a0 | 8450 | { |
106c3d67 | 8451 | pfn = cp_build_c_cast (type, pfn, complain); |
a17aefa2 | 8452 | return build_ptrmemfunc1 (to_type, |
9031d10b | 8453 | integer_zero_node, |
d75b8a68 | 8454 | pfn); |
ac9386a0 | 8455 | } |
8456 | ||
cc4d0855 | 8457 | if (type_unknown_p (pfn)) |
34f12878 | 8458 | return instantiate_type (type, pfn, complain); |
ddb9bca7 | 8459 | |
1c16607c | 8460 | fn = TREE_OPERAND (pfn, 0); |
5760bc87 | 8461 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL |
8462 | /* In a template, we will have preserved the | |
8463 | OFFSET_REF. */ | |
8464 | || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF)); | |
a17aefa2 | 8465 | return make_ptrmem_cst (to_type, fn); |
1c16607c | 8466 | } |
471086d6 | 8467 | |
d75b8a68 | 8468 | /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST |
902de8ed | 8469 | given by CST. |
8470 | ||
8471 | ??? There is no consistency as to the types returned for the above | |
0f3ccaa3 | 8472 | values. Some code acts as if it were a sizetype and some as if it were |
902de8ed | 8473 | integer_type_node. */ |
471086d6 | 8474 | |
1c16607c | 8475 | void |
f0dd33b5 | 8476 | expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn) |
1c16607c | 8477 | { |
8478 | tree type = TREE_TYPE (cst); | |
8479 | tree fn = PTRMEM_CST_MEMBER (cst); | |
e54ba4f1 | 8480 | tree ptr_class, fn_class; |
471086d6 | 8481 | |
b4df430b | 8482 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); |
e54ba4f1 | 8483 | |
8484 | /* The class that the function belongs to. */ | |
9ba4048d | 8485 | fn_class = DECL_CONTEXT (fn); |
e54ba4f1 | 8486 | |
8487 | /* The class that we're creating a pointer to member of. */ | |
8488 | ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type); | |
8489 | ||
8490 | /* First, calculate the adjustment to the function's class. */ | |
cb02169c | 8491 | *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0, |
85132186 | 8492 | /*c_cast_p=*/0, tf_warning_or_error); |
e54ba4f1 | 8493 | |
1c16607c | 8494 | if (!DECL_VIRTUAL_P (fn)) |
4405c1ad | 8495 | *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), |
8496 | build_addr_func (fn, tf_warning_or_error)); | |
471086d6 | 8497 | else |
ac9386a0 | 8498 | { |
e54ba4f1 | 8499 | /* If we're dealing with a virtual function, we have to adjust 'this' |
653e5405 | 8500 | again, to point to the base which provides the vtable entry for |
8501 | fn; the call will do the opposite adjustment. */ | |
c83788c9 | 8502 | tree orig_class = DECL_CONTEXT (fn); |
e54ba4f1 | 8503 | tree binfo = binfo_or_else (orig_class, fn_class); |
d2c63826 | 8504 | *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta), |
8505 | *delta, BINFO_OFFSET (binfo)); | |
e54ba4f1 | 8506 | |
5ad590ad | 8507 | /* We set PFN to the vtable offset at which the function can be |
8508 | found, plus one (unless ptrmemfunc_vbit_in_delta, in which | |
8509 | case delta is shifted left, and then incremented). */ | |
5e89f6fb | 8510 | *pfn = DECL_VINDEX (fn); |
d2c63826 | 8511 | *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn, |
8512 | TYPE_SIZE_UNIT (vtable_entry_type)); | |
5e89f6fb | 8513 | |
8514 | switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) | |
8515 | { | |
8516 | case ptrmemfunc_vbit_in_pfn: | |
d2c63826 | 8517 | *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn, |
8518 | integer_one_node); | |
5e89f6fb | 8519 | break; |
8520 | ||
8521 | case ptrmemfunc_vbit_in_delta: | |
d2c63826 | 8522 | *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta), |
8523 | *delta, integer_one_node); | |
8524 | *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta), | |
8525 | *delta, integer_one_node); | |
5e89f6fb | 8526 | break; |
8527 | ||
8528 | default: | |
2e3e31d2 | 8529 | gcc_unreachable (); |
5e89f6fb | 8530 | } |
8531 | ||
d2c63826 | 8532 | *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn); |
1c16607c | 8533 | } |
8534 | } | |
471086d6 | 8535 | |
1c16607c | 8536 | /* Return an expression for PFN from the pointer-to-member function |
8537 | given by T. */ | |
8538 | ||
e75bce43 | 8539 | static tree |
f0dd33b5 | 8540 | pfn_from_ptrmemfunc (tree t) |
1c16607c | 8541 | { |
8542 | if (TREE_CODE (t) == PTRMEM_CST) | |
8543 | { | |
8544 | tree delta; | |
1c16607c | 8545 | tree pfn; |
9031d10b | 8546 | |
d75b8a68 | 8547 | expand_ptrmemfunc_cst (t, &delta, &pfn); |
1c16607c | 8548 | if (pfn) |
8549 | return pfn; | |
ac9386a0 | 8550 | } |
471086d6 | 8551 | |
4ac852cb | 8552 | return build_ptrmemfunc_access_expr (t, pfn_identifier); |
471086d6 | 8553 | } |
8554 | ||
426ab4bb | 8555 | /* Return an expression for DELTA from the pointer-to-member function |
8556 | given by T. */ | |
8557 | ||
8558 | static tree | |
8559 | delta_from_ptrmemfunc (tree t) | |
8560 | { | |
8561 | if (TREE_CODE (t) == PTRMEM_CST) | |
8562 | { | |
8563 | tree delta; | |
8564 | tree pfn; | |
8565 | ||
8566 | expand_ptrmemfunc_cst (t, &delta, &pfn); | |
8567 | if (delta) | |
8568 | return delta; | |
8569 | } | |
8570 | ||
8571 | return build_ptrmemfunc_access_expr (t, delta_identifier); | |
8572 | } | |
8573 | ||
8c18e707 | 8574 | /* Convert value RHS to type TYPE as preparation for an assignment to |
ec89d23f | 8575 | an lvalue of type TYPE. ERRTYPE indicates what kind of error the |
8576 | implicit conversion is. If FNDECL is non-NULL, we are doing the | |
8577 | conversion in order to pass the PARMNUMth argument of FNDECL. | |
8578 | If FNDECL is NULL, we are doing the conversion in function pointer | |
8579 | argument passing, conversion in initialization, etc. */ | |
471086d6 | 8580 | |
8581 | static tree | |
f0dd33b5 | 8582 | convert_for_assignment (tree type, tree rhs, |
183407ee | 8583 | impl_conv_rhs errtype, tree fndecl, int parmnum, |
cf7aa2e5 | 8584 | tsubst_flags_t complain, int flags) |
471086d6 | 8585 | { |
cd16867a | 8586 | tree rhstype; |
8587 | enum tree_code coder; | |
471086d6 | 8588 | |
471086d6 | 8589 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ |
8590 | if (TREE_CODE (rhs) == NON_LVALUE_EXPR) | |
8591 | rhs = TREE_OPERAND (rhs, 0); | |
8592 | ||
9b5db70f | 8593 | /* Handle [dcl.init.list] direct-list-initialization from |
8594 | single element of enumeration with a fixed underlying type. */ | |
8595 | if (is_direct_enum_init (type, rhs)) | |
8596 | { | |
8597 | tree elt = CONSTRUCTOR_ELT (rhs, 0)->value; | |
8598 | if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain)) | |
3d866864 | 8599 | { |
8600 | warning_sentinel w (warn_useless_cast); | |
8601 | rhs = cp_build_c_cast (type, elt, complain); | |
8602 | } | |
9b5db70f | 8603 | else |
8604 | rhs = error_mark_node; | |
8605 | } | |
8606 | ||
8c18e707 | 8607 | rhstype = TREE_TYPE (rhs); |
8608 | coder = TREE_CODE (rhstype); | |
8609 | ||
76249021 | 8610 | if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE |
546c4794 | 8611 | && vector_types_convertible_p (type, rhstype, true)) |
6ac80fa2 | 8612 | { |
8613 | rhs = mark_rvalue_use (rhs); | |
8614 | return convert (type, rhs); | |
8615 | } | |
fdb4703c | 8616 | |
8c18e707 | 8617 | if (rhs == error_mark_node || rhstype == error_mark_node) |
471086d6 | 8618 | return error_mark_node; |
8417823c | 8619 | if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node) |
471086d6 | 8620 | return error_mark_node; |
8621 | ||
8c18e707 | 8622 | /* The RHS of an assignment cannot have void type. */ |
471086d6 | 8623 | if (coder == VOID_TYPE) |
8624 | { | |
ebd21de4 | 8625 | if (complain & tf_error) |
8626 | error ("void value not ignored as it ought to be"); | |
471086d6 | 8627 | return error_mark_node; |
8628 | } | |
471086d6 | 8629 | |
aa796005 | 8630 | if (c_dialect_objc ()) |
8631 | { | |
8632 | int parmno; | |
183407ee | 8633 | tree selector; |
aa796005 | 8634 | tree rname = fndecl; |
8635 | ||
183407ee | 8636 | switch (errtype) |
8637 | { | |
8638 | case ICR_ASSIGN: | |
8639 | parmno = -1; | |
8640 | break; | |
8641 | case ICR_INIT: | |
8642 | parmno = -2; | |
8643 | break; | |
8644 | default: | |
8645 | selector = objc_message_selector (); | |
8646 | parmno = parmnum; | |
8647 | if (selector && parmno > 1) | |
8648 | { | |
8649 | rname = selector; | |
8650 | parmno -= 1; | |
8651 | } | |
aa796005 | 8652 | } |
8653 | ||
8654 | if (objc_compare_types (type, rhstype, parmno, rname)) | |
6ac80fa2 | 8655 | { |
8656 | rhs = mark_rvalue_use (rhs); | |
8657 | return convert (type, rhs); | |
8658 | } | |
aa796005 | 8659 | } |
8660 | ||
8c18e707 | 8661 | /* [expr.ass] |
8662 | ||
8663 | The expression is implicitly converted (clause _conv_) to the | |
1fab1557 | 8664 | cv-unqualified type of the left operand. |
8665 | ||
8666 | We allow bad conversions here because by the time we get to this point | |
8667 | we are committed to doing the conversion. If we end up doing a bad | |
8668 | conversion, convert_like will complain. */ | |
66bbeb85 | 8669 | if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain)) |
8c18e707 | 8670 | { |
788bd876 | 8671 | /* When -Wno-pmf-conversions is use, we just silently allow |
8c18e707 | 8672 | conversions from pointers-to-members to plain pointers. If |
8673 | the conversion doesn't work, cp_convert will complain. */ | |
9031d10b | 8674 | if (!warn_pmf2ptr |
8675 | && TYPE_PTR_P (type) | |
8c18e707 | 8676 | && TYPE_PTRMEMFUNC_P (rhstype)) |
c4698a21 | 8677 | rhs = cp_convert (strip_top_quals (type), rhs, complain); |
1fab1557 | 8678 | else |
788bd876 | 8679 | { |
ebd21de4 | 8680 | if (complain & tf_error) |
8681 | { | |
8682 | /* If the right-hand side has unknown type, then it is an | |
8683 | overloaded function. Call instantiate_type to get error | |
8684 | messages. */ | |
8685 | if (rhstype == unknown_type_node) | |
2c7d98a7 | 8686 | { |
8687 | tree r = instantiate_type (type, rhs, tf_warning_or_error); | |
9e37fbc2 | 8688 | /* -fpermissive might allow this; recurse. */ |
2c7d98a7 | 8689 | if (!seen_error ()) |
9e37fbc2 | 8690 | return convert_for_assignment (type, r, errtype, fndecl, |
8691 | parmnum, complain, flags); | |
2c7d98a7 | 8692 | } |
ebd21de4 | 8693 | else if (fndecl) |
4d1eda3a | 8694 | error ("cannot convert %qH to %qI for argument %qP to %qD", |
ebd21de4 | 8695 | rhstype, type, parmnum, fndecl); |
8696 | else | |
183407ee | 8697 | switch (errtype) |
8698 | { | |
8699 | case ICR_DEFAULT_ARGUMENT: | |
4d1eda3a | 8700 | error ("cannot convert %qH to %qI in default argument", |
183407ee | 8701 | rhstype, type); |
8702 | break; | |
8703 | case ICR_ARGPASS: | |
4d1eda3a | 8704 | error ("cannot convert %qH to %qI in argument passing", |
183407ee | 8705 | rhstype, type); |
8706 | break; | |
8707 | case ICR_CONVERTING: | |
4d1eda3a | 8708 | error ("cannot convert %qH to %qI", |
183407ee | 8709 | rhstype, type); |
8710 | break; | |
8711 | case ICR_INIT: | |
4d1eda3a | 8712 | error ("cannot convert %qH to %qI in initialization", |
183407ee | 8713 | rhstype, type); |
8714 | break; | |
8715 | case ICR_RETURN: | |
4d1eda3a | 8716 | error ("cannot convert %qH to %qI in return", |
183407ee | 8717 | rhstype, type); |
8718 | break; | |
8719 | case ICR_ASSIGN: | |
4d1eda3a | 8720 | error ("cannot convert %qH to %qI in assignment", |
183407ee | 8721 | rhstype, type); |
8722 | break; | |
8723 | default: | |
8724 | gcc_unreachable(); | |
8725 | } | |
5544070d | 8726 | if (TYPE_PTR_P (rhstype) |
8727 | && TYPE_PTR_P (type) | |
8728 | && CLASS_TYPE_P (TREE_TYPE (rhstype)) | |
8729 | && CLASS_TYPE_P (TREE_TYPE (type)) | |
8730 | && !COMPLETE_TYPE_P (TREE_TYPE (rhstype))) | |
8731 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL | |
8732 | (TREE_TYPE (rhstype))), | |
8733 | "class type %qT is incomplete", TREE_TYPE (rhstype)); | |
ebd21de4 | 8734 | } |
788bd876 | 8735 | return error_mark_node; |
8736 | } | |
ac9386a0 | 8737 | } |
b86527d8 | 8738 | if (warn_suggest_attribute_format) |
be7350e7 | 8739 | { |
8740 | const enum tree_code codel = TREE_CODE (type); | |
8741 | if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
8742 | && coder == codel | |
ebd21de4 | 8743 | && check_missing_format_attribute (type, rhstype) |
8744 | && (complain & tf_warning)) | |
183407ee | 8745 | switch (errtype) |
8746 | { | |
8747 | case ICR_ARGPASS: | |
8748 | case ICR_DEFAULT_ARGUMENT: | |
8749 | if (fndecl) | |
b86527d8 | 8750 | warning (OPT_Wsuggest_attribute_format, |
ec89d23f | 8751 | "parameter %qP of %qD might be a candidate " |
183407ee | 8752 | "for a format attribute", parmnum, fndecl); |
8753 | else | |
b86527d8 | 8754 | warning (OPT_Wsuggest_attribute_format, |
183407ee | 8755 | "parameter might be a candidate " |
8756 | "for a format attribute"); | |
8757 | break; | |
8758 | case ICR_CONVERTING: | |
b86527d8 | 8759 | warning (OPT_Wsuggest_attribute_format, |
362094d7 | 8760 | "target of conversion might be a candidate " |
183407ee | 8761 | "for a format attribute"); |
8762 | break; | |
8763 | case ICR_INIT: | |
b86527d8 | 8764 | warning (OPT_Wsuggest_attribute_format, |
183407ee | 8765 | "target of initialization might be a candidate " |
8766 | "for a format attribute"); | |
8767 | break; | |
8768 | case ICR_RETURN: | |
b86527d8 | 8769 | warning (OPT_Wsuggest_attribute_format, |
183407ee | 8770 | "return type might be a candidate " |
8771 | "for a format attribute"); | |
8772 | break; | |
8773 | case ICR_ASSIGN: | |
b86527d8 | 8774 | warning (OPT_Wsuggest_attribute_format, |
183407ee | 8775 | "left-hand side of assignment might be a candidate " |
8776 | "for a format attribute"); | |
8777 | break; | |
8778 | default: | |
8779 | gcc_unreachable(); | |
8780 | } | |
be7350e7 | 8781 | } |
074ab442 | 8782 | |
b5af6702 | 8783 | /* If -Wparentheses, warn about a = b = c when a has type bool and b |
8784 | does not. */ | |
60a0513e | 8785 | if (warn_parentheses |
9e1a8234 | 8786 | && TREE_CODE (type) == BOOLEAN_TYPE |
60a0513e | 8787 | && TREE_CODE (rhs) == MODIFY_EXPR |
b5af6702 | 8788 | && !TREE_NO_WARNING (rhs) |
6095fcdf | 8789 | && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE |
ebd21de4 | 8790 | && (complain & tf_warning)) |
60a0513e | 8791 | { |
3df42822 | 8792 | location_t loc = EXPR_LOC_OR_LOC (rhs, input_location); |
6095fcdf | 8793 | |
8794 | warning_at (loc, OPT_Wparentheses, | |
8795 | "suggest parentheses around assignment used as truth value"); | |
60a0513e | 8796 | TREE_NO_WARNING (rhs) = 1; |
8797 | } | |
8798 | ||
cf7aa2e5 | 8799 | return perform_implicit_conversion_flags (strip_top_quals (type), rhs, |
8800 | complain, flags); | |
471086d6 | 8801 | } |
8802 | ||
5eaed30b | 8803 | /* Convert RHS to be of type TYPE. |
3160db1d | 8804 | If EXP is nonzero, it is the target of the initialization. |
ec89d23f | 8805 | ERRTYPE indicates what kind of error the implicit conversion is. |
471086d6 | 8806 | |
8807 | Two major differences between the behavior of | |
8808 | `convert_for_assignment' and `convert_for_initialization' | |
8809 | are that references are bashed in the former, while | |
8810 | copied in the latter, and aggregates are assigned in | |
8811 | the former (operator=) while initialized in the | |
8812 | latter (X(X&)). | |
8813 | ||
8814 | If using constructor make sure no conversion operator exists, if one does | |
c4698a21 | 8815 | exist, an ambiguity exists. */ |
96624a9e | 8816 | |
471086d6 | 8817 | tree |
f0dd33b5 | 8818 | convert_for_initialization (tree exp, tree type, tree rhs, int flags, |
183407ee | 8819 | impl_conv_rhs errtype, tree fndecl, int parmnum, |
ebd21de4 | 8820 | tsubst_flags_t complain) |
471086d6 | 8821 | { |
cd16867a | 8822 | enum tree_code codel = TREE_CODE (type); |
8823 | tree rhstype; | |
8824 | enum tree_code coder; | |
471086d6 | 8825 | |
8826 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. | |
8827 | Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */ | |
8828 | if (TREE_CODE (rhs) == NOP_EXPR | |
ddb9bca7 | 8829 | && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0)) |
8830 | && codel != REFERENCE_TYPE) | |
471086d6 | 8831 | rhs = TREE_OPERAND (rhs, 0); |
8832 | ||
4e99a396 | 8833 | if (type == error_mark_node |
8834 | || rhs == error_mark_node | |
471086d6 | 8835 | || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)) |
8836 | return error_mark_node; | |
8837 | ||
ea7ec26a | 8838 | if (MAYBE_CLASS_TYPE_P (non_reference (type))) |
8839 | ; | |
8840 | else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE | |
8841 | && TREE_CODE (type) != ARRAY_TYPE | |
8842 | && (TREE_CODE (type) != REFERENCE_TYPE | |
8843 | || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)) | |
8844 | || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE | |
8845 | && !TYPE_REFFN_P (type)) | |
8846 | || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE) | |
4405c1ad | 8847 | rhs = decay_conversion (rhs, complain); |
471086d6 | 8848 | |
8849 | rhstype = TREE_TYPE (rhs); | |
8850 | coder = TREE_CODE (rhstype); | |
8851 | ||
471086d6 | 8852 | if (coder == ERROR_MARK) |
8853 | return error_mark_node; | |
8854 | ||
471086d6 | 8855 | /* We accept references to incomplete types, so we can |
8856 | return here before checking if RHS is of complete type. */ | |
9031d10b | 8857 | |
471086d6 | 8858 | if (codel == REFERENCE_TYPE) |
bb0726a1 | 8859 | { |
8860 | /* This should eventually happen in convert_arguments. */ | |
034b484a | 8861 | int savew = 0, savee = 0; |
bb0726a1 | 8862 | |
8863 | if (fndecl) | |
d9d1b0a4 | 8864 | savew = warningcount + werrorcount, savee = errorcount; |
c7b89256 | 8865 | rhs = initialize_reference (type, rhs, flags, complain); |
405d08f9 | 8866 | |
8867 | if (fndecl | |
8868 | && (warningcount + werrorcount > savew || errorcount > savee)) | |
0f4714ef | 8869 | inform (DECL_SOURCE_LOCATION (fndecl), |
8870 | "in passing argument %P of %qD", parmnum, fndecl); | |
405d08f9 | 8871 | |
bb0726a1 | 8872 | return rhs; |
9031d10b | 8873 | } |
471086d6 | 8874 | |
aeef2be5 | 8875 | if (exp != 0) |
dba1ea2f | 8876 | exp = require_complete_type_sfinae (exp, complain); |
471086d6 | 8877 | if (exp == error_mark_node) |
8878 | return error_mark_node; | |
8879 | ||
ef4534a3 | 8880 | rhstype = non_reference (rhstype); |
471086d6 | 8881 | |
596c0ae6 | 8882 | type = complete_type (type); |
8883 | ||
7da3c25a | 8884 | if (DIRECT_INIT_EXPR_P (type, rhs)) |
8885 | /* Don't try to do copy-initialization if we already have | |
8886 | direct-initialization. */ | |
8887 | return rhs; | |
8888 | ||
95397ff9 | 8889 | if (MAYBE_CLASS_TYPE_P (type)) |
13f56139 | 8890 | return perform_implicit_conversion_flags (type, rhs, complain, flags); |
471086d6 | 8891 | |
ebd21de4 | 8892 | return convert_for_assignment (type, rhs, errtype, fndecl, parmnum, |
cf7aa2e5 | 8893 | complain, flags); |
471086d6 | 8894 | } |
8895 | \f | |
51046b65 | 8896 | /* If RETVAL is the address of, or a reference to, a local variable or |
f22a2cb7 | 8897 | temporary give an appropriate warning and return true. */ |
471086d6 | 8898 | |
f22a2cb7 | 8899 | static bool |
f0dd33b5 | 8900 | maybe_warn_about_returning_address_of_local (tree retval) |
51046b65 | 8901 | { |
8902 | tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl)); | |
1041ae98 | 8903 | tree whats_returned = fold_for_warn (retval); |
471086d6 | 8904 | |
f11a4817 | 8905 | for (;;) |
51046b65 | 8906 | { |
51046b65 | 8907 | if (TREE_CODE (whats_returned) == COMPOUND_EXPR) |
f11a4817 | 8908 | whats_returned = TREE_OPERAND (whats_returned, 1); |
40d0b157 | 8909 | else if (CONVERT_EXPR_P (whats_returned) |
8910 | || TREE_CODE (whats_returned) == NON_LVALUE_EXPR) | |
51046b65 | 8911 | whats_returned = TREE_OPERAND (whats_returned, 0); |
f11a4817 | 8912 | else |
8913 | break; | |
8914 | } | |
8915 | ||
8916 | if (TREE_CODE (whats_returned) != ADDR_EXPR) | |
f22a2cb7 | 8917 | return false; |
9031d10b | 8918 | whats_returned = TREE_OPERAND (whats_returned, 0); |
f11a4817 | 8919 | |
7f9fe03b | 8920 | while (TREE_CODE (whats_returned) == COMPONENT_REF |
8921 | || TREE_CODE (whats_returned) == ARRAY_REF) | |
8922 | whats_returned = TREE_OPERAND (whats_returned, 0); | |
8923 | ||
f11a4817 | 8924 | if (TREE_CODE (valtype) == REFERENCE_TYPE) |
8925 | { | |
8926 | if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR | |
8927 | || TREE_CODE (whats_returned) == TARGET_EXPR) | |
51046b65 | 8928 | { |
8b447d3f | 8929 | warning (OPT_Wreturn_local_addr, "returning reference to temporary"); |
f22a2cb7 | 8930 | return true; |
51046b65 | 8931 | } |
80a58eb0 | 8932 | if (VAR_P (whats_returned) |
f11a4817 | 8933 | && DECL_NAME (whats_returned) |
8934 | && TEMP_NAME_P (DECL_NAME (whats_returned))) | |
51046b65 | 8935 | { |
8b447d3f | 8936 | warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned"); |
f22a2cb7 | 8937 | return true; |
51046b65 | 8938 | } |
8939 | } | |
51046b65 | 8940 | |
aaadf915 | 8941 | if (DECL_P (whats_returned) |
f11a4817 | 8942 | && DECL_NAME (whats_returned) |
8943 | && DECL_FUNCTION_SCOPE_P (whats_returned) | |
4da0e4ef | 8944 | && !is_capture_proxy (whats_returned) |
f11a4817 | 8945 | && !(TREE_STATIC (whats_returned) |
8946 | || TREE_PUBLIC (whats_returned))) | |
8947 | { | |
8948 | if (TREE_CODE (valtype) == REFERENCE_TYPE) | |
0f4714ef | 8949 | warning_at (DECL_SOURCE_LOCATION (whats_returned), |
8950 | OPT_Wreturn_local_addr, | |
8951 | "reference to local variable %qD returned", | |
8952 | whats_returned); | |
27ff10cb | 8953 | else if (TREE_CODE (whats_returned) == LABEL_DECL) |
0f4714ef | 8954 | warning_at (DECL_SOURCE_LOCATION (whats_returned), |
8955 | OPT_Wreturn_local_addr, "address of label %qD returned", | |
8956 | whats_returned); | |
f11a4817 | 8957 | else |
0f4714ef | 8958 | warning_at (DECL_SOURCE_LOCATION (whats_returned), |
8959 | OPT_Wreturn_local_addr, "address of local variable %qD " | |
8960 | "returned", whats_returned); | |
f22a2cb7 | 8961 | return true; |
51046b65 | 8962 | } |
f22a2cb7 | 8963 | |
8964 | return false; | |
51046b65 | 8965 | } |
8966 | ||
6c0cc2cd | 8967 | /* Check that returning RETVAL from the current function is valid. |
51046b65 | 8968 | Return an expression explicitly showing all conversions required to |
8969 | change RETVAL into the function return type, and to assign it to | |
81a3c55b | 8970 | the DECL_RESULT for the function. Set *NO_WARNING to true if |
8971 | code reaches end of non-void function warning shouldn't be issued | |
8972 | on this RETURN_EXPR. */ | |
51046b65 | 8973 | |
8974 | tree | |
81a3c55b | 8975 | check_return_expr (tree retval, bool *no_warning) |
471086d6 | 8976 | { |
51046b65 | 8977 | tree result; |
86359a65 | 8978 | /* The type actually returned by the function. */ |
51046b65 | 8979 | tree valtype; |
86359a65 | 8980 | /* The type the function is declared to return, or void if |
8981 | the declared type is incomplete. */ | |
8982 | tree functype; | |
51046b65 | 8983 | int fn_returns_value_p; |
63949b38 | 8984 | bool named_return_value_okay_p; |
51046b65 | 8985 | |
81a3c55b | 8986 | *no_warning = false; |
8987 | ||
51046b65 | 8988 | /* A `volatile' function is one that isn't supposed to return, ever. |
8989 | (This is a G++ extension, used to get better code for functions | |
8990 | that call the `volatile' function.) */ | |
471086d6 | 8991 | if (TREE_THIS_VOLATILE (current_function_decl)) |
c3ceba8e | 8992 | warning (0, "function declared %<noreturn%> has a %<return%> statement"); |
471086d6 | 8993 | |
51046b65 | 8994 | /* Check for various simple errors. */ |
fb75f6dc | 8995 | if (DECL_DESTRUCTOR_P (current_function_decl)) |
471086d6 | 8996 | { |
dc56db3b | 8997 | if (retval) |
905d4035 | 8998 | error ("returning a value from a destructor"); |
51046b65 | 8999 | return NULL_TREE; |
dc56db3b | 9000 | } |
8b1e0315 | 9001 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) |
2c440a13 | 9002 | { |
8b1e0315 | 9003 | if (in_function_try_handler) |
9004 | /* If a return statement appears in a handler of the | |
47cd6605 | 9005 | function-try-block of a constructor, the program is ill-formed. */ |
8b1e0315 | 9006 | error ("cannot return from a handler of a function-try-block of a constructor"); |
9007 | else if (retval) | |
9008 | /* You can't return a value from a constructor. */ | |
9009 | error ("returning a value from a constructor"); | |
9010 | return NULL_TREE; | |
51046b65 | 9011 | } |
51046b65 | 9012 | |
2ce308ea | 9013 | const tree saved_retval = retval; |
9014 | ||
261c3c20 | 9015 | if (processing_template_decl) |
9016 | { | |
9017 | current_function_returns_value = 1; | |
2ce308ea | 9018 | |
830a6615 | 9019 | if (check_for_bare_parameter_packs (retval)) |
2ce308ea | 9020 | return error_mark_node; |
9021 | ||
8b001668 | 9022 | /* If one of the types might be void, we can't tell whether we're |
9023 | returning a value. */ | |
9024 | if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))) | |
9025 | && !current_function_auto_return_pattern) | |
2ce308ea | 9026 | || (retval != NULL_TREE |
8b001668 | 9027 | && (TREE_TYPE (retval) == NULL_TREE |
9028 | || WILDCARD_TYPE_P (TREE_TYPE (retval))))) | |
9029 | goto dependent; | |
261c3c20 | 9030 | } |
9031d10b | 9031 | |
86359a65 | 9032 | functype = TREE_TYPE (TREE_TYPE (current_function_decl)); |
9033 | ||
9034 | /* Deduce auto return type from a return statement. */ | |
9035 | if (current_function_auto_return_pattern) | |
9036 | { | |
9037 | tree auto_node; | |
9038 | tree type; | |
9039 | ||
9040 | if (!retval && !is_auto (current_function_auto_return_pattern)) | |
9041 | { | |
9042 | /* Give a helpful error message. */ | |
9043 | error ("return-statement with no value, in function returning %qT", | |
9044 | current_function_auto_return_pattern); | |
9045 | inform (input_location, "only plain %<auto%> return type can be " | |
9046 | "deduced to %<void%>"); | |
9047 | type = error_mark_node; | |
9048 | } | |
8789c9b5 | 9049 | else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval)) |
9050 | { | |
9051 | error ("returning initializer list"); | |
9052 | type = error_mark_node; | |
9053 | } | |
86359a65 | 9054 | else |
9055 | { | |
9056 | if (!retval) | |
3ab4693e | 9057 | retval = void_node; |
86359a65 | 9058 | auto_node = type_uses_auto (current_function_auto_return_pattern); |
9059 | type = do_auto_deduction (current_function_auto_return_pattern, | |
9060 | retval, auto_node); | |
9061 | } | |
9062 | ||
9063 | if (type == error_mark_node) | |
9064 | /* Leave it. */; | |
9065 | else if (functype == current_function_auto_return_pattern) | |
9066 | apply_deduced_return_type (current_function_decl, type); | |
b34f7e66 | 9067 | else if (!same_type_p (type, functype)) |
9068 | { | |
9069 | if (LAMBDA_FUNCTION_P (current_function_decl)) | |
9070 | error ("inconsistent types %qT and %qT deduced for " | |
9071 | "lambda return type", functype, type); | |
9072 | else | |
9073 | error ("inconsistent deduction for auto return type: " | |
9074 | "%qT and then %qT", functype, type); | |
9075 | } | |
86359a65 | 9076 | functype = type; |
9077 | } | |
9078 | ||
51046b65 | 9079 | result = DECL_RESULT (current_function_decl); |
9080 | valtype = TREE_TYPE (result); | |
b4df430b | 9081 | gcc_assert (valtype != NULL_TREE); |
e3cfe3ce | 9082 | fn_returns_value_p = !VOID_TYPE_P (valtype); |
51046b65 | 9083 | |
9084 | /* Check for a return statement with no return value in a function | |
9085 | that's supposed to return a value. */ | |
9086 | if (!retval && fn_returns_value_p) | |
9087 | { | |
86359a65 | 9088 | if (functype != error_mark_node) |
9089 | permerror (input_location, "return-statement with no value, in " | |
9090 | "function returning %qT", valtype); | |
9091 | /* Remember that this function did return. */ | |
9092 | current_function_returns_value = 1; | |
81a3c55b | 9093 | /* And signal caller that TREE_NO_WARNING should be set on the |
074ab442 | 9094 | RETURN_EXPR to avoid control reaches end of non-void function |
9095 | warnings in tree-cfg.c. */ | |
81a3c55b | 9096 | *no_warning = true; |
51046b65 | 9097 | } |
9098 | /* Check for a return statement with a value in a function that | |
9099 | isn't supposed to return a value. */ | |
9100 | else if (retval && !fn_returns_value_p) | |
9031d10b | 9101 | { |
e3cfe3ce | 9102 | if (VOID_TYPE_P (TREE_TYPE (retval))) |
51046b65 | 9103 | /* You can return a `void' value from a function of `void' |
9104 | type. In that case, we have to evaluate the expression for | |
9105 | its side-effects. */ | |
e2665e8b | 9106 | finish_expr_stmt (retval); |
51046b65 | 9107 | else |
e2665e8b | 9108 | permerror (input_location, |
9109 | "return-statement with a value, in function " | |
9110 | "returning %qT", valtype); | |
51046b65 | 9111 | current_function_returns_null = 1; |
9112 | ||
9113 | /* There's really no value to return, after all. */ | |
9114 | return NULL_TREE; | |
2c440a13 | 9115 | } |
51046b65 | 9116 | else if (!retval) |
9117 | /* Remember that this function can sometimes return without a | |
9118 | value. */ | |
9119 | current_function_returns_null = 1; | |
8b1e0315 | 9120 | else |
9121 | /* Remember that this function did return a value. */ | |
9122 | current_function_returns_value = 1; | |
dc56db3b | 9123 | |
5407b68e | 9124 | /* Check for erroneous operands -- but after giving ourselves a |
ab6bb714 | 9125 | chance to provide an error about returning a value from a void |
9126 | function. */ | |
9127 | if (error_operand_p (retval)) | |
9128 | { | |
9129 | current_function_return_value = error_mark_node; | |
9130 | return error_mark_node; | |
9131 | } | |
9132 | ||
b7d68727 | 9133 | /* Only operator new(...) throw(), can return NULL [expr.new/13]. */ |
4767026d | 9134 | if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl)) |
b7d68727 | 9135 | && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl)) |
30de7d91 | 9136 | && ! flag_check_new |
7a1eab9a | 9137 | && retval && null_ptr_cst_p (retval)) |
c3ceba8e | 9138 | warning (0, "%<operator new%> must not return NULL unless it is " |
653e5405 | 9139 | "declared %<throw()%> (or -fcheck-new is in effect)"); |
471086d6 | 9140 | |
02d7f858 | 9141 | /* Effective C++ rule 15. See also start_function. */ |
f85d646a | 9142 | if (warn_ecpp |
ef8f6502 | 9143 | && DECL_NAME (current_function_decl) == assign_op_identifier) |
cd752d0f | 9144 | { |
9145 | bool warn = true; | |
9146 | ||
9147 | /* The function return type must be a reference to the current | |
9148 | class. */ | |
9149 | if (TREE_CODE (valtype) == REFERENCE_TYPE | |
9150 | && same_type_ignoring_top_level_qualifiers_p | |
9151 | (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))) | |
9152 | { | |
9153 | /* Returning '*this' is obviously OK. */ | |
9154 | if (retval == current_class_ref) | |
9155 | warn = false; | |
9156 | /* If we are calling a function whose return type is the same of | |
9157 | the current class reference, it is ok. */ | |
86ccc124 | 9158 | else if (INDIRECT_REF_P (retval) |
cd752d0f | 9159 | && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR) |
e78103d9 | 9160 | warn = false; |
cd752d0f | 9161 | } |
9162 | ||
9163 | if (warn) | |
ced7c954 | 9164 | warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>"); |
cd752d0f | 9165 | } |
02d7f858 | 9166 | |
8b001668 | 9167 | if (dependent_type_p (functype) |
9168 | || type_dependent_expression_p (retval)) | |
2ce308ea | 9169 | { |
8b001668 | 9170 | dependent: |
2ce308ea | 9171 | /* We should not have changed the return value. */ |
9172 | gcc_assert (retval == saved_retval); | |
297de7bc | 9173 | return retval; |
2ce308ea | 9174 | } |
9175 | ||
4cfd9030 | 9176 | /* The fabled Named Return Value optimization, as per [class.copy]/15: |
9177 | ||
9178 | [...] For a function with a class return type, if the expression | |
9179 | in the return statement is the name of a local object, and the cv- | |
9180 | unqualified type of the local object is the same as the function | |
9181 | return type, an implementation is permitted to omit creating the tem- | |
9182 | porary object to hold the function return value [...] | |
9183 | ||
9184 | So, if this is a value-returning function that always returns the same | |
9185 | local variable, remember it. | |
80ac742d | 9186 | |
9187 | It might be nice to be more flexible, and choose the first suitable | |
9188 | variable even if the function sometimes returns something else, but | |
9189 | then we run the risk of clobbering the variable we chose if the other | |
9190 | returned expression uses the chosen variable somehow. And people expect | |
4cfd9030 | 9191 | this restriction, anyway. (jason 2000-11-19) |
9192 | ||
7677ebb9 | 9193 | See finish_function and finalize_nrv for the rest of this optimization. */ |
80ac742d | 9194 | |
63949b38 | 9195 | named_return_value_okay_p = |
9196 | (retval != NULL_TREE | |
8b001668 | 9197 | && !processing_template_decl |
63949b38 | 9198 | /* Must be a local, automatic variable. */ |
80a58eb0 | 9199 | && VAR_P (retval) |
63949b38 | 9200 | && DECL_CONTEXT (retval) == current_function_decl |
9201 | && ! TREE_STATIC (retval) | |
62983391 | 9202 | /* And not a lambda or anonymous union proxy. */ |
9203 | && !DECL_HAS_VALUE_EXPR_P (retval) | |
05b733e3 | 9204 | && (DECL_ALIGN (retval) <= DECL_ALIGN (result)) |
63949b38 | 9205 | /* The cv-unqualified type of the returned value must be the |
9206 | same as the cv-unqualified return type of the | |
9207 | function. */ | |
9208 | && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))), | |
86359a65 | 9209 | (TYPE_MAIN_VARIANT (functype))) |
7fc437a9 | 9210 | /* And the returned value must be non-volatile. */ |
9211 | && ! TYPE_VOLATILE (TREE_TYPE (retval))); | |
63949b38 | 9212 | |
dbacd3bd | 9213 | if (fn_returns_value_p && flag_elide_constructors) |
80ac742d | 9214 | { |
63949b38 | 9215 | if (named_return_value_okay_p |
9216 | && (current_function_return_value == NULL_TREE | |
9217 | || current_function_return_value == retval)) | |
80ac742d | 9218 | current_function_return_value = retval; |
9219 | else | |
9220 | current_function_return_value = error_mark_node; | |
9221 | } | |
9222 | ||
51046b65 | 9223 | /* We don't need to do any conversions when there's nothing being |
9224 | returned. */ | |
ab6bb714 | 9225 | if (!retval) |
9226 | return NULL_TREE; | |
51046b65 | 9227 | |
9228 | /* Do any required conversions. */ | |
9229 | if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl)) | |
9230 | /* No conversions are required. */ | |
9231 | ; | |
471086d6 | 9232 | else |
9233 | { | |
63949b38 | 9234 | int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING; |
0762a377 | 9235 | |
49eec3d7 | 9236 | /* The functype's return type will have been set to void, if it |
9237 | was an incomplete type. Just treat this as 'return;' */ | |
9238 | if (VOID_TYPE_P (functype)) | |
9239 | return error_mark_node; | |
074ab442 | 9240 | |
a5369bbd | 9241 | /* If we had an id-expression obfuscated by force_paren_expr, we need |
9242 | to undo it so we can try to treat it as an rvalue below. */ | |
4003c807 | 9243 | retval = maybe_undo_parenthesized_ref (retval); |
a5369bbd | 9244 | |
8dd9e61e | 9245 | /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes |
63949b38 | 9246 | treated as an rvalue for the purposes of overload resolution to |
988ff48d | 9247 | favor move constructors over copy constructors. |
9248 | ||
9249 | Note that these conditions are similar to, but not as strict as, | |
9250 | the conditions for the named return value optimization. */ | |
ff00c674 | 9251 | bool converted = false; |
988ff48d | 9252 | if ((cxx_dialect != cxx98) |
62983391 | 9253 | && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval)) |
988ff48d | 9254 | || TREE_CODE (retval) == PARM_DECL) |
9255 | && DECL_CONTEXT (retval) == current_function_decl | |
9256 | && !TREE_STATIC (retval) | |
988ff48d | 9257 | /* This is only interesting for class type. */ |
86359a65 | 9258 | && CLASS_TYPE_P (functype)) |
ff00c674 | 9259 | { |
9260 | tree moved = move (retval); | |
9261 | moved = convert_for_initialization | |
9262 | (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE, | |
9263 | ICR_RETURN, NULL_TREE, 0, tf_none); | |
9264 | if (moved != error_mark_node) | |
9265 | { | |
9266 | retval = moved; | |
9267 | converted = true; | |
9268 | } | |
9269 | } | |
63949b38 | 9270 | |
0762a377 | 9271 | /* First convert the value to the function's return type, then |
9272 | to the type of return value's location to handle the | |
653e5405 | 9273 | case that functype is smaller than the valtype. */ |
ff00c674 | 9274 | if (!converted) |
9275 | retval = convert_for_initialization | |
9276 | (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0, | |
9277 | tf_warning_or_error); | |
0762a377 | 9278 | retval = convert (valtype, retval); |
9279 | ||
51046b65 | 9280 | /* If the conversion failed, treat this just like `return;'. */ |
373dfaa9 | 9281 | if (retval == error_mark_node) |
8b1e0315 | 9282 | return retval; |
f64b1d68 | 9283 | /* We can't initialize a register from a AGGR_INIT_EXPR. */ |
18d50ae6 | 9284 | else if (! cfun->returns_struct |
435ab961 | 9285 | && TREE_CODE (retval) == TARGET_EXPR |
f64b1d68 | 9286 | && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR) |
831d52a2 | 9287 | retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, |
9288 | TREE_OPERAND (retval, 0)); | |
e6e98789 | 9289 | else if (!processing_template_decl |
9290 | && maybe_warn_about_returning_address_of_local (retval)) | |
f22a2cb7 | 9291 | retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, |
9292 | build_zero_cst (TREE_TYPE (retval))); | |
471086d6 | 9293 | } |
9031d10b | 9294 | |
8b001668 | 9295 | if (processing_template_decl) |
9296 | return saved_retval; | |
9297 | ||
51046b65 | 9298 | /* Actually copy the value returned into the appropriate location. */ |
373dfaa9 | 9299 | if (retval && retval != result) |
831d52a2 | 9300 | retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval); |
fe773f95 | 9301 | |
51046b65 | 9302 | return retval; |
9303 | } | |
9304 | ||
471086d6 | 9305 | \f |
3160db1d | 9306 | /* Returns nonzero if the pointer-type FROM can be converted to the |
772e70a1 | 9307 | pointer-type TO via a qualification conversion. If CONSTP is -1, |
3160db1d | 9308 | then we return nonzero if the pointers are similar, and the |
772e70a1 | 9309 | cv-qualification signature of FROM is a proper subset of that of TO. |
9310 | ||
9311 | If CONSTP is positive, then all outer pointers have been | |
9312 | const-qualified. */ | |
96624a9e | 9313 | |
b465397d | 9314 | static int |
f0dd33b5 | 9315 | comp_ptr_ttypes_real (tree to, tree from, int constp) |
ddb9bca7 | 9316 | { |
1bc16cab | 9317 | bool to_more_cv_qualified = false; |
ed7c4e62 | 9318 | bool is_opaque_pointer = false; |
772e70a1 | 9319 | |
ddb9bca7 | 9320 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) |
9321 | { | |
9322 | if (TREE_CODE (to) != TREE_CODE (from)) | |
9323 | return 0; | |
9324 | ||
2739960c | 9325 | if (TREE_CODE (from) == OFFSET_TYPE |
149a7f80 | 9326 | && !same_type_p (TYPE_OFFSET_BASETYPE (from), |
9327 | TYPE_OFFSET_BASETYPE (to))) | |
9328 | return 0; | |
2739960c | 9329 | |
ce28ee2e | 9330 | /* Const and volatile mean something different for function types, |
9331 | so the usual checks are not appropriate. */ | |
9332 | if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE) | |
9333 | { | |
d7489d8d | 9334 | if (!at_least_as_qualified_p (to, from)) |
bdd152ce | 9335 | return 0; |
ddb9bca7 | 9336 | |
d7489d8d | 9337 | if (!at_least_as_qualified_p (from, to)) |
bdd152ce | 9338 | { |
772e70a1 | 9339 | if (constp == 0) |
9340 | return 0; | |
1bc16cab | 9341 | to_more_cv_qualified = true; |
772e70a1 | 9342 | } |
9343 | ||
9344 | if (constp > 0) | |
d7489d8d | 9345 | constp &= TYPE_READONLY (to); |
ce28ee2e | 9346 | } |
ddb9bca7 | 9347 | |
76249021 | 9348 | if (VECTOR_TYPE_P (to)) |
ed7c4e62 | 9349 | is_opaque_pointer = vector_targets_convertible_p (to, from); |
9350 | ||
c21c015b | 9351 | if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to)) |
457a1f67 | 9352 | return ((constp >= 0 || to_more_cv_qualified) |
ed7c4e62 | 9353 | && (is_opaque_pointer |
9354 | || same_type_ignoring_top_level_qualifiers_p (to, from))); | |
ddb9bca7 | 9355 | } |
9356 | } | |
9357 | ||
457a1f67 | 9358 | /* When comparing, say, char ** to char const **, this function takes |
9359 | the 'char *' and 'char const *'. Do not pass non-pointer/reference | |
9360 | types to this function. */ | |
96624a9e | 9361 | |
ddb9bca7 | 9362 | int |
f0dd33b5 | 9363 | comp_ptr_ttypes (tree to, tree from) |
ddb9bca7 | 9364 | { |
9365 | return comp_ptr_ttypes_real (to, from, 1); | |
9366 | } | |
2739960c | 9367 | |
16826b91 | 9368 | /* Returns true iff FNTYPE is a non-class type that involves |
9369 | error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node | |
9370 | if a parameter type is ill-formed. */ | |
9371 | ||
9372 | bool | |
9373 | error_type_p (const_tree type) | |
9374 | { | |
9375 | tree t; | |
9376 | ||
9377 | switch (TREE_CODE (type)) | |
9378 | { | |
9379 | case ERROR_MARK: | |
9380 | return true; | |
9381 | ||
9382 | case POINTER_TYPE: | |
9383 | case REFERENCE_TYPE: | |
9384 | case OFFSET_TYPE: | |
9385 | return error_type_p (TREE_TYPE (type)); | |
9386 | ||
9387 | case FUNCTION_TYPE: | |
9388 | case METHOD_TYPE: | |
9389 | if (error_type_p (TREE_TYPE (type))) | |
9390 | return true; | |
9391 | for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t)) | |
9392 | if (error_type_p (TREE_VALUE (t))) | |
9393 | return true; | |
9394 | return false; | |
9395 | ||
9396 | case RECORD_TYPE: | |
9397 | if (TYPE_PTRMEMFUNC_P (type)) | |
9398 | return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type)); | |
9399 | return false; | |
9400 | ||
9401 | default: | |
9402 | return false; | |
9403 | } | |
9404 | } | |
9405 | ||
8797ee50 | 9406 | /* Returns true if to and from are (possibly multi-level) pointers to the same |
2739960c | 9407 | type or inheritance-related types, regardless of cv-quals. */ |
9408 | ||
8797ee50 | 9409 | bool |
9f627b1a | 9410 | ptr_reasonably_similar (const_tree to, const_tree from) |
2739960c | 9411 | { |
9412 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) | |
9413 | { | |
1fab1557 | 9414 | /* Any target type is similar enough to void. */ |
c21c015b | 9415 | if (VOID_TYPE_P (to)) |
16826b91 | 9416 | return !error_type_p (from); |
c21c015b | 9417 | if (VOID_TYPE_P (from)) |
16826b91 | 9418 | return !error_type_p (to); |
1fab1557 | 9419 | |
2739960c | 9420 | if (TREE_CODE (to) != TREE_CODE (from)) |
8797ee50 | 9421 | return false; |
2739960c | 9422 | |
9423 | if (TREE_CODE (from) == OFFSET_TYPE | |
a09db423 | 9424 | && comptypes (TYPE_OFFSET_BASETYPE (to), |
9425 | TYPE_OFFSET_BASETYPE (from), | |
9426 | COMPARE_BASE | COMPARE_DERIVED)) | |
2739960c | 9427 | continue; |
9428 | ||
76249021 | 9429 | if (VECTOR_TYPE_P (to) |
546c4794 | 9430 | && vector_types_convertible_p (to, from, false)) |
8797ee50 | 9431 | return true; |
bd971849 | 9432 | |
69773da0 | 9433 | if (TREE_CODE (to) == INTEGER_TYPE |
9434 | && TYPE_PRECISION (to) == TYPE_PRECISION (from)) | |
8797ee50 | 9435 | return true; |
69773da0 | 9436 | |
9437 | if (TREE_CODE (to) == FUNCTION_TYPE) | |
16826b91 | 9438 | return !error_type_p (to) && !error_type_p (from); |
69773da0 | 9439 | |
c21c015b | 9440 | if (!TYPE_PTR_P (to)) |
8797ee50 | 9441 | { |
9442 | /* When either type is incomplete avoid DERIVED_FROM_P, | |
9443 | which may call complete_type (c++/57942). */ | |
9444 | bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from); | |
9445 | return comptypes | |
9446 | (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), | |
9447 | b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED); | |
9448 | } | |
2739960c | 9449 | } |
9450 | } | |
f9670f72 | 9451 | |
8de1f703 | 9452 | /* Return true if TO and FROM (both of which are POINTER_TYPEs or |
9453 | pointer-to-member types) are the same, ignoring cv-qualification at | |
9454 | all levels. */ | |
f9670f72 | 9455 | |
8de1f703 | 9456 | bool |
f0dd33b5 | 9457 | comp_ptr_ttypes_const (tree to, tree from) |
f9670f72 | 9458 | { |
ed7c4e62 | 9459 | bool is_opaque_pointer = false; |
9460 | ||
f9670f72 | 9461 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) |
9462 | { | |
9463 | if (TREE_CODE (to) != TREE_CODE (from)) | |
8de1f703 | 9464 | return false; |
f9670f72 | 9465 | |
9466 | if (TREE_CODE (from) == OFFSET_TYPE | |
daf9ff67 | 9467 | && same_type_p (TYPE_OFFSET_BASETYPE (from), |
9468 | TYPE_OFFSET_BASETYPE (to))) | |
f9670f72 | 9469 | continue; |
9470 | ||
76249021 | 9471 | if (VECTOR_TYPE_P (to)) |
ed7c4e62 | 9472 | is_opaque_pointer = vector_targets_convertible_p (to, from); |
9473 | ||
c21c015b | 9474 | if (!TYPE_PTR_P (to)) |
ed7c4e62 | 9475 | return (is_opaque_pointer |
9476 | || same_type_ignoring_top_level_qualifiers_p (to, from)); | |
f9670f72 | 9477 | } |
9478 | } | |
9479 | ||
3119c950 | 9480 | /* Returns the type qualifiers for this type, including the qualifiers on the |
9481 | elements for an array type. */ | |
ece7bde7 | 9482 | |
9483 | int | |
f8fd23c0 | 9484 | cp_type_quals (const_tree type) |
ece7bde7 | 9485 | { |
2ec48521 | 9486 | int quals; |
5ca94202 | 9487 | /* This CONST_CAST is okay because strip_array_types returns its |
ce4469fa | 9488 | argument unmodified and we assign it to a const_tree. */ |
2ec48521 | 9489 | type = strip_array_types (CONST_CAST_TREE (type)); |
9490 | if (type == error_mark_node | |
9491 | /* Quals on a FUNCTION_TYPE are memfn quals. */ | |
9492 | || TREE_CODE (type) == FUNCTION_TYPE) | |
2c73aca0 | 9493 | return TYPE_UNQUALIFIED; |
2ec48521 | 9494 | quals = TYPE_QUALS (type); |
9495 | /* METHOD and REFERENCE_TYPEs should never have quals. */ | |
9496 | gcc_assert ((TREE_CODE (type) != METHOD_TYPE | |
9497 | && TREE_CODE (type) != REFERENCE_TYPE) | |
9498 | || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) | |
9499 | == TYPE_UNQUALIFIED)); | |
9500 | return quals; | |
9501 | } | |
9502 | ||
e116411c | 9503 | /* Returns the function-ref-qualifier for TYPE */ |
9504 | ||
9505 | cp_ref_qualifier | |
9506 | type_memfn_rqual (const_tree type) | |
9507 | { | |
9508 | gcc_assert (TREE_CODE (type) == FUNCTION_TYPE | |
9509 | || TREE_CODE (type) == METHOD_TYPE); | |
9510 | ||
9511 | if (!FUNCTION_REF_QUALIFIED (type)) | |
9512 | return REF_QUAL_NONE; | |
9513 | else if (FUNCTION_RVALUE_QUALIFIED (type)) | |
9514 | return REF_QUAL_RVALUE; | |
9515 | else | |
9516 | return REF_QUAL_LVALUE; | |
9517 | } | |
9518 | ||
2ec48521 | 9519 | /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or |
9520 | METHOD_TYPE. */ | |
9521 | ||
9522 | int | |
9523 | type_memfn_quals (const_tree type) | |
9524 | { | |
9525 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
9526 | return TYPE_QUALS (type); | |
9527 | else if (TREE_CODE (type) == METHOD_TYPE) | |
a8d966c1 | 9528 | return cp_type_quals (class_of_this_parm (type)); |
2ec48521 | 9529 | else |
9530 | gcc_unreachable (); | |
9531 | } | |
9532 | ||
9533 | /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to | |
e116411c | 9534 | MEMFN_QUALS and its ref-qualifier to RQUAL. */ |
2ec48521 | 9535 | |
9536 | tree | |
e116411c | 9537 | apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual) |
2ec48521 | 9538 | { |
9539 | /* Could handle METHOD_TYPE here if necessary. */ | |
9540 | gcc_assert (TREE_CODE (type) == FUNCTION_TYPE); | |
e116411c | 9541 | if (TYPE_QUALS (type) == memfn_quals |
9542 | && type_memfn_rqual (type) == rqual) | |
2ec48521 | 9543 | return type; |
e116411c | 9544 | |
2ec48521 | 9545 | /* This should really have a different TYPE_MAIN_VARIANT, but that gets |
9546 | complex. */ | |
e116411c | 9547 | tree result = build_qualified_type (type, memfn_quals); |
e116411c | 9548 | return build_ref_qualified_type (result, rqual); |
3e04bd45 | 9549 | } |
6f9b1420 | 9550 | |
f3943982 | 9551 | /* Returns nonzero if TYPE is const or volatile. */ |
9552 | ||
9553 | bool | |
9554 | cv_qualified_p (const_tree type) | |
9555 | { | |
9556 | int quals = cp_type_quals (type); | |
9557 | return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0; | |
9558 | } | |
9559 | ||
331bc0ad | 9560 | /* Returns nonzero if the TYPE contains a mutable member. */ |
6f9b1420 | 9561 | |
f0dd33b5 | 9562 | bool |
9f627b1a | 9563 | cp_has_mutable_p (const_tree type) |
6f9b1420 | 9564 | { |
5ca94202 | 9565 | /* This CONST_CAST is okay because strip_array_types returns its |
ce4469fa | 9566 | argument unmodified and we assign it to a const_tree. */ |
e47a6f81 | 9567 | type = strip_array_types (CONST_CAST_TREE(type)); |
6f9b1420 | 9568 | |
9569 | return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type); | |
9570 | } | |
f5139c2f | 9571 | |
d1d739f8 | 9572 | /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the |
9573 | TYPE_QUALS. For a VAR_DECL, this may be an optimistic | |
9574 | approximation. In particular, consider: | |
9575 | ||
9576 | int f(); | |
9577 | struct S { int i; }; | |
9578 | const S s = { f(); } | |
9579 | ||
9580 | Here, we will make "s" as TREE_READONLY (because it is declared | |
9581 | "const") -- only to reverse ourselves upon seeing that the | |
9582 | initializer is non-constant. */ | |
9583 | ||
11ef5c28 | 9584 | void |
9585 | cp_apply_type_quals_to_decl (int type_quals, tree decl) | |
9586 | { | |
9587 | tree type = TREE_TYPE (decl); | |
9588 | ||
9589 | if (type == error_mark_node) | |
9590 | return; | |
9591 | ||
75eaa947 | 9592 | if (TREE_CODE (decl) == TYPE_DECL) |
9593 | return; | |
9594 | ||
5211f810 | 9595 | gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE |
9596 | && type_quals != TYPE_UNQUALIFIED)); | |
11ef5c28 | 9597 | |
f06b15bf | 9598 | /* Avoid setting TREE_READONLY incorrectly. */ |
98a33d9f | 9599 | /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr |
9600 | constructor can produce constant init, so rely on cp_finish_decl to | |
9601 | clear TREE_READONLY if the variable has non-constant init. */ | |
92806ebe | 9602 | |
78636c55 | 9603 | /* If the type has (or might have) a mutable component, that component |
9604 | might be modified. */ | |
9605 | if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type)) | |
f06b15bf | 9606 | type_quals &= ~TYPE_QUAL_CONST; |
9607 | ||
11ef5c28 | 9608 | c_apply_type_quals_to_decl (type_quals, decl); |
9609 | } | |
9610 | ||
f5139c2f | 9611 | /* Subroutine of casts_away_constness. Make T1 and T2 point at |
6ab399e8 | 9612 | exemplar types such that casting T1 to T2 is casting away constness |
f5139c2f | 9613 | if and only if there is no implicit conversion from T1 to T2. */ |
9614 | ||
9615 | static void | |
66bbeb85 | 9616 | casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain) |
f5139c2f | 9617 | { |
9618 | int quals1; | |
9619 | int quals2; | |
9620 | ||
9621 | /* [expr.const.cast] | |
9622 | ||
9623 | For multi-level pointer to members and multi-level mixed pointers | |
9624 | and pointers to members (conv.qual), the "member" aspect of a | |
9625 | pointer to member level is ignored when determining if a const | |
9626 | cv-qualifier has been cast away. */ | |
f5139c2f | 9627 | /* [expr.const.cast] |
9628 | ||
9629 | For two pointer types: | |
9630 | ||
653e5405 | 9631 | X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type |
9632 | X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type | |
9633 | K is min(N,M) | |
f5139c2f | 9634 | |
9635 | casting from X1 to X2 casts away constness if, for a non-pointer | |
9636 | type T there does not exist an implicit conversion (clause | |
9637 | _conv_) from: | |
9638 | ||
653e5405 | 9639 | Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N * |
9031d10b | 9640 | |
f5139c2f | 9641 | to |
9642 | ||
653e5405 | 9643 | Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */ |
05765a91 | 9644 | if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1)) |
9645 | || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2))) | |
f5139c2f | 9646 | { |
9647 | *t1 = cp_build_qualified_type (void_type_node, | |
3119c950 | 9648 | cp_type_quals (*t1)); |
f5139c2f | 9649 | *t2 = cp_build_qualified_type (void_type_node, |
3119c950 | 9650 | cp_type_quals (*t2)); |
f5139c2f | 9651 | return; |
9652 | } | |
9031d10b | 9653 | |
3119c950 | 9654 | quals1 = cp_type_quals (*t1); |
9655 | quals2 = cp_type_quals (*t2); | |
9e8dd3d2 | 9656 | |
05765a91 | 9657 | if (TYPE_PTRDATAMEM_P (*t1)) |
9e8dd3d2 | 9658 | *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1); |
9659 | else | |
9660 | *t1 = TREE_TYPE (*t1); | |
05765a91 | 9661 | if (TYPE_PTRDATAMEM_P (*t2)) |
9e8dd3d2 | 9662 | *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2); |
9663 | else | |
9664 | *t2 = TREE_TYPE (*t2); | |
9665 | ||
66bbeb85 | 9666 | casts_away_constness_r (t1, t2, complain); |
f5139c2f | 9667 | *t1 = build_pointer_type (*t1); |
9668 | *t2 = build_pointer_type (*t2); | |
9669 | *t1 = cp_build_qualified_type (*t1, quals1); | |
9670 | *t2 = cp_build_qualified_type (*t2, quals2); | |
9671 | } | |
9672 | ||
3160db1d | 9673 | /* Returns nonzero if casting from TYPE1 to TYPE2 casts away |
9258414e | 9674 | constness. |
9675 | ||
9676 | ??? This function returns non-zero if casting away qualifiers not | |
9677 | just const. We would like to return to the caller exactly which | |
9678 | qualifiers are casted away to give more accurate diagnostics. | |
9679 | */ | |
f5139c2f | 9680 | |
f0dd33b5 | 9681 | static bool |
66bbeb85 | 9682 | casts_away_constness (tree t1, tree t2, tsubst_flags_t complain) |
f5139c2f | 9683 | { |
9684 | if (TREE_CODE (t2) == REFERENCE_TYPE) | |
9685 | { | |
9686 | /* [expr.const.cast] | |
9031d10b | 9687 | |
f5139c2f | 9688 | Casting from an lvalue of type T1 to an lvalue of type T2 |
9689 | using a reference cast casts away constness if a cast from an | |
9690 | rvalue of type "pointer to T1" to the type "pointer to T2" | |
9691 | casts away constness. */ | |
f0dd33b5 | 9692 | t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1); |
f5139c2f | 9693 | return casts_away_constness (build_pointer_type (t1), |
66bbeb85 | 9694 | build_pointer_type (TREE_TYPE (t2)), |
9695 | complain); | |
f5139c2f | 9696 | } |
9697 | ||
05765a91 | 9698 | if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2)) |
f5139c2f | 9699 | /* [expr.const.cast] |
9031d10b | 9700 | |
f5139c2f | 9701 | Casting from an rvalue of type "pointer to data member of X |
9702 | of type T1" to the type "pointer to data member of Y of type | |
9703 | T2" casts away constness if a cast from an rvalue of type | |
4109ca29 | 9704 | "pointer to T1" to the type "pointer to T2" casts away |
f5139c2f | 9705 | constness. */ |
3380c7cd | 9706 | return casts_away_constness |
1bc16cab | 9707 | (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)), |
66bbeb85 | 9708 | build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)), |
9709 | complain); | |
f5139c2f | 9710 | |
9711 | /* Casting away constness is only something that makes sense for | |
9712 | pointer or reference types. */ | |
c21c015b | 9713 | if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2)) |
f0dd33b5 | 9714 | return false; |
f5139c2f | 9715 | |
9716 | /* Top-level qualifiers don't matter. */ | |
9717 | t1 = TYPE_MAIN_VARIANT (t1); | |
9718 | t2 = TYPE_MAIN_VARIANT (t2); | |
66bbeb85 | 9719 | casts_away_constness_r (&t1, &t2, complain); |
9720 | if (!can_convert (t2, t1, complain)) | |
f0dd33b5 | 9721 | return true; |
f5139c2f | 9722 | |
f0dd33b5 | 9723 | return false; |
f5139c2f | 9724 | } |
01632c0a | 9725 | |
ef4534a3 | 9726 | /* If T is a REFERENCE_TYPE return the type to which T refers. |
9727 | Otherwise, return T itself. */ | |
9728 | ||
9729 | tree | |
9730 | non_reference (tree t) | |
9731 | { | |
a00f651a | 9732 | if (t && TREE_CODE (t) == REFERENCE_TYPE) |
ef4534a3 | 9733 | t = TREE_TYPE (t); |
9734 | return t; | |
9735 | } | |
e35976b1 | 9736 | |
9737 | ||
9738 | /* Return nonzero if REF is an lvalue valid for this language; | |
9739 | otherwise, print an error message and return zero. USE says | |
9740 | how the lvalue is being used and so selects the error message. */ | |
9741 | ||
9742 | int | |
db9317ce | 9743 | lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain) |
e35976b1 | 9744 | { |
0eb0b6ca | 9745 | cp_lvalue_kind kind = lvalue_kind (ref); |
e35976b1 | 9746 | |
0eb0b6ca | 9747 | if (kind == clk_none) |
9748 | { | |
9749 | if (complain & tf_error) | |
fdd84b77 | 9750 | lvalue_error (input_location, use); |
0eb0b6ca | 9751 | return 0; |
9752 | } | |
9753 | else if (kind & (clk_rvalueref|clk_class)) | |
9754 | { | |
9755 | if (!(complain & tf_error)) | |
9756 | return 0; | |
9757 | if (kind & clk_class) | |
9758 | /* Make this a permerror because we used to accept it. */ | |
9759 | permerror (input_location, "using temporary as lvalue"); | |
9760 | else | |
9761 | error ("using xvalue (rvalue reference) as lvalue"); | |
9762 | } | |
9763 | return 1; | |
e35976b1 | 9764 | } |
fbb73d9b | 9765 | |
244db24d | 9766 | /* Return true if a user-defined literal operator is a raw operator. */ |
9767 | ||
9768 | bool | |
9769 | check_raw_literal_operator (const_tree decl) | |
9770 | { | |
9771 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
9772 | tree argtype; | |
9773 | int arity; | |
9774 | bool maybe_raw_p = false; | |
9775 | ||
9776 | /* Count the number and type of arguments and check for ellipsis. */ | |
9777 | for (argtype = argtypes, arity = 0; | |
9778 | argtype && argtype != void_list_node; | |
9779 | ++arity, argtype = TREE_CHAIN (argtype)) | |
9780 | { | |
9781 | tree t = TREE_VALUE (argtype); | |
9782 | ||
9783 | if (same_type_p (t, const_string_type_node)) | |
9784 | maybe_raw_p = true; | |
9785 | } | |
9786 | if (!argtype) | |
9787 | return false; /* Found ellipsis. */ | |
9788 | ||
9789 | if (!maybe_raw_p || arity != 1) | |
9790 | return false; | |
9791 | ||
9792 | return true; | |
9793 | } | |
9794 | ||
9795 | ||
9796 | /* Return true if a user-defined literal operator has one of the allowed | |
9797 | argument types. */ | |
9798 | ||
9799 | bool | |
9800 | check_literal_operator_args (const_tree decl, | |
9801 | bool *long_long_unsigned_p, bool *long_double_p) | |
9802 | { | |
9803 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
9ba809de | 9804 | |
9805 | *long_long_unsigned_p = false; | |
9806 | *long_double_p = false; | |
ffadbdec | 9807 | if (processing_template_decl || processing_specialization) |
9808 | return argtypes == void_list_node; | |
244db24d | 9809 | else |
9810 | { | |
9811 | tree argtype; | |
9812 | int arity; | |
9813 | int max_arity = 2; | |
244db24d | 9814 | |
244db24d | 9815 | /* Count the number and type of arguments and check for ellipsis. */ |
9816 | for (argtype = argtypes, arity = 0; | |
9817 | argtype && argtype != void_list_node; | |
9818 | argtype = TREE_CHAIN (argtype)) | |
9819 | { | |
9820 | tree t = TREE_VALUE (argtype); | |
9821 | ++arity; | |
9822 | ||
c21c015b | 9823 | if (TYPE_PTR_P (t)) |
244db24d | 9824 | { |
916caf36 | 9825 | bool maybe_raw_p = false; |
278e795d | 9826 | t = TREE_TYPE (t); |
9827 | if (cp_type_quals (t) != TYPE_QUAL_CONST) | |
9828 | return false; | |
9829 | t = TYPE_MAIN_VARIANT (t); | |
916caf36 | 9830 | if ((maybe_raw_p = same_type_p (t, char_type_node)) |
9831 | || same_type_p (t, wchar_type_node) | |
9832 | || same_type_p (t, char16_type_node) | |
9833 | || same_type_p (t, char32_type_node)) | |
278e795d | 9834 | { |
916caf36 | 9835 | argtype = TREE_CHAIN (argtype); |
9836 | if (!argtype) | |
9837 | return false; | |
9838 | t = TREE_VALUE (argtype); | |
9839 | if (maybe_raw_p && argtype == void_list_node) | |
9840 | return true; | |
9841 | else if (same_type_p (t, size_type_node)) | |
9842 | { | |
9843 | ++arity; | |
9844 | continue; | |
9845 | } | |
9846 | else | |
9847 | return false; | |
278e795d | 9848 | } |
244db24d | 9849 | } |
9850 | else if (same_type_p (t, long_long_unsigned_type_node)) | |
9851 | { | |
9852 | max_arity = 1; | |
9853 | *long_long_unsigned_p = true; | |
9854 | } | |
9855 | else if (same_type_p (t, long_double_type_node)) | |
9856 | { | |
9857 | max_arity = 1; | |
9858 | *long_double_p = true; | |
9859 | } | |
9860 | else if (same_type_p (t, char_type_node)) | |
9861 | max_arity = 1; | |
9862 | else if (same_type_p (t, wchar_type_node)) | |
9863 | max_arity = 1; | |
9864 | else if (same_type_p (t, char16_type_node)) | |
9865 | max_arity = 1; | |
9866 | else if (same_type_p (t, char32_type_node)) | |
9867 | max_arity = 1; | |
9868 | else | |
9869 | return false; | |
9870 | } | |
9871 | if (!argtype) | |
9872 | return false; /* Found ellipsis. */ | |
9873 | ||
ffadbdec | 9874 | if (arity != max_arity) |
244db24d | 9875 | return false; |
9876 | ||
244db24d | 9877 | return true; |
9878 | } | |
9879 | } | |
547c6b1f | 9880 | |
9881 | /* Always returns false since unlike C90, C++ has no concept of implicit | |
9882 | function declarations. */ | |
9883 | ||
9884 | bool | |
9885 | c_decl_implicit (const_tree) | |
9886 | { | |
9887 | return false; | |
9888 | } |