]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Build expressions with type checking for C++ compiler. |
a5544970 | 2 | Copyright (C) 1987-2019 Free Software Foundation, Inc. |
8d08fdba MS |
3 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
4 | ||
f5adbb8d | 5 | This file is part of GCC. |
8d08fdba | 6 | |
f5adbb8d | 7 | GCC is free software; you can redistribute it and/or modify |
8d08fdba | 8 | it under the terms of the GNU General Public License as published by |
e77f031d | 9 | the Free Software Foundation; either version 3, or (at your option) |
8d08fdba MS |
10 | any later version. |
11 | ||
f5adbb8d | 12 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
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 | |
e77f031d NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
8d08fdba MS |
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 | |
5088b058 | 25 | checks, and some optimization. */ |
8d08fdba | 26 | |
8d08fdba | 27 | #include "config.h" |
8d052bc7 | 28 | #include "system.h" |
4977bab6 | 29 | #include "coretypes.h" |
2adfab87 | 30 | #include "target.h" |
2adfab87 | 31 | #include "cp-tree.h" |
d8a2d370 DN |
32 | #include "stor-layout.h" |
33 | #include "varasm.h" | |
ff84991f | 34 | #include "intl.h" |
7b6d72fc | 35 | #include "convert.h" |
61d3ce20 | 36 | #include "c-family/c-objc.h" |
de5a5fa1 | 37 | #include "c-family/c-ubsan.h" |
06d40de8 | 38 | #include "params.h" |
264757fb | 39 | #include "gcc-rich-location.h" |
314e6352 ML |
40 | #include "stringpool.h" |
41 | #include "attribs.h" | |
45b2222a | 42 | #include "asan.h" |
2b53407e | 43 | #include "gimplify.h" |
8d08fdba | 44 | |
882de214 KT |
45 | static tree cp_build_addr_expr_strict (tree, tsubst_flags_t); |
46 | static tree cp_build_function_call (tree, tree, tsubst_flags_t); | |
b5791fdc | 47 | static tree pfn_from_ptrmemfunc (tree); |
a298680c | 48 | static tree delta_from_ptrmemfunc (tree); |
2f5b91f5 | 49 | static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int, |
e57d93c6 | 50 | tsubst_flags_t, int); |
4f2e1536 MP |
51 | static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree, |
52 | tsubst_flags_t); | |
5ade1ed2 DG |
53 | static tree rationalize_conditional_expr (enum tree_code, tree, |
54 | tsubst_flags_t); | |
acd8e2d0 | 55 | static int comp_ptr_ttypes_real (tree, tree, int); |
acd8e2d0 | 56 | static bool comp_except_types (tree, tree, bool); |
58f9752a | 57 | static bool comp_array_types (const_tree, const_tree, bool); |
05abad4c | 58 | static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *); |
135d47df | 59 | static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t); |
b40e334f PC |
60 | static void casts_away_constness_r (tree *, tree *, tsubst_flags_t); |
61 | static bool casts_away_constness (tree, tree, tsubst_flags_t); | |
b4dfdc11 | 62 | static bool maybe_warn_about_returning_address_of_local (tree); |
76545796 | 63 | static tree lookup_destructor (tree, tree, tree, tsubst_flags_t); |
09bae928 | 64 | static void error_args_num (location_t, tree, bool); |
9771b263 | 65 | static int convert_arguments (tree, vec<tree, va_gc> **, tree, int, |
5ade1ed2 | 66 | tsubst_flags_t); |
308d9cda MP |
67 | static bool is_std_move_p (tree); |
68 | static bool is_std_forward_p (tree); | |
8d08fdba | 69 | |
8d08fdba | 70 | /* Do `exp = require_complete_type (exp);' to make sure exp |
ae58fa02 | 71 | does not have an incomplete type. (That includes void types.) |
79fe346e | 72 | Returns error_mark_node if the VALUE does not have |
ae58fa02 | 73 | complete type when this function returns. */ |
8d08fdba MS |
74 | |
75 | tree | |
79fe346e | 76 | require_complete_type_sfinae (tree value, tsubst_flags_t complain) |
8d08fdba | 77 | { |
5566b478 MS |
78 | tree type; |
79 | ||
66543169 | 80 | if (processing_template_decl || value == error_mark_node) |
5566b478 MS |
81 | return value; |
82 | ||
2c73f9f5 ML |
83 | if (TREE_CODE (value) == OVERLOAD) |
84 | type = unknown_type_node; | |
85 | else | |
86 | type = TREE_TYPE (value); | |
8d08fdba | 87 | |
ae5cbc33 RS |
88 | if (type == error_mark_node) |
89 | return error_mark_node; | |
90 | ||
8d08fdba | 91 | /* First, detect a valid value with a complete type. */ |
d0f062fb | 92 | if (COMPLETE_TYPE_P (type)) |
8d08fdba MS |
93 | return value; |
94 | ||
79fe346e | 95 | if (complete_type_or_maybe_complain (type, value, complain)) |
8f259df3 MM |
96 | return value; |
97 | else | |
ae58fa02 | 98 | return error_mark_node; |
8d08fdba MS |
99 | } |
100 | ||
79fe346e JM |
101 | tree |
102 | require_complete_type (tree value) | |
103 | { | |
104 | return require_complete_type_sfinae (value, tf_warning_or_error); | |
105 | } | |
106 | ||
8f259df3 MM |
107 | /* Try to complete TYPE, if it is incomplete. For example, if TYPE is |
108 | a template instantiation, do the instantiation. Returns TYPE, | |
109 | whether or not it could be completed, unless something goes | |
110 | horribly wrong, in which case the error_mark_node is returned. */ | |
111 | ||
5566b478 | 112 | tree |
acd8e2d0 | 113 | complete_type (tree type) |
5566b478 | 114 | { |
8857f91e MM |
115 | if (type == NULL_TREE) |
116 | /* Rather than crash, we return something sure to cause an error | |
117 | at some point. */ | |
118 | return error_mark_node; | |
119 | ||
d0f062fb | 120 | if (type == error_mark_node || COMPLETE_TYPE_P (type)) |
5566b478 | 121 | ; |
f65e97fd | 122 | else if (TREE_CODE (type) == ARRAY_TYPE) |
5566b478 MS |
123 | { |
124 | tree t = complete_type (TREE_TYPE (type)); | |
73bebd55 | 125 | unsigned int needs_constructing, has_nontrivial_dtor; |
1e2e9f54 | 126 | if (COMPLETE_TYPE_P (t) && !dependent_type_p (type)) |
6467930b | 127 | layout_type (type); |
73bebd55 | 128 | needs_constructing |
c73964b2 | 129 | = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t)); |
73bebd55 | 130 | has_nontrivial_dtor |
834c6dff | 131 | = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t)); |
73bebd55 JJ |
132 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
133 | { | |
134 | TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing; | |
135 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor; | |
136 | } | |
5566b478 | 137 | } |
7ddedda4 | 138 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type)) |
e76a2646 | 139 | instantiate_class_template (TYPE_MAIN_VARIANT (type)); |
5566b478 MS |
140 | |
141 | return type; | |
142 | } | |
143 | ||
5aa3396c | 144 | /* Like complete_type, but issue an error if the TYPE cannot be completed. |
be20e673 | 145 | VALUE is used for informative diagnostics. |
66543169 | 146 | Returns NULL_TREE if the type cannot be made complete. */ |
8f259df3 MM |
147 | |
148 | tree | |
309714d4 | 149 | complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain) |
8f259df3 MM |
150 | { |
151 | type = complete_type (type); | |
ae58fa02 MM |
152 | if (type == error_mark_node) |
153 | /* We already issued an error. */ | |
154 | return NULL_TREE; | |
d0f062fb | 155 | else if (!COMPLETE_TYPE_P (type)) |
8f259df3 | 156 | { |
309714d4 JM |
157 | if (complain & tf_error) |
158 | cxx_incomplete_type_diagnostic (value, type, DK_ERROR); | |
8f259df3 MM |
159 | return NULL_TREE; |
160 | } | |
161 | else | |
162 | return type; | |
163 | } | |
164 | ||
309714d4 JM |
165 | tree |
166 | complete_type_or_else (tree type, tree value) | |
167 | { | |
168 | return complete_type_or_maybe_complain (type, value, tf_warning_or_error); | |
169 | } | |
170 | ||
8d08fdba MS |
171 | \f |
172 | /* Return the common type of two parameter lists. | |
96d84882 | 173 | We assume that comptypes has already been done and returned 1; |
8d08fdba MS |
174 | if that isn't so, this may crash. |
175 | ||
176 | As an optimization, free the space we allocate if the parameter | |
177 | lists are already common. */ | |
178 | ||
10b2bcdd | 179 | static tree |
5671bf27 | 180 | commonparms (tree p1, tree p2) |
8d08fdba MS |
181 | { |
182 | tree oldargs = p1, newargs, n; | |
183 | int i, len; | |
184 | int any_change = 0; | |
8d08fdba MS |
185 | |
186 | len = list_length (p1); | |
187 | newargs = tree_last (p1); | |
188 | ||
189 | if (newargs == void_list_node) | |
190 | i = 1; | |
191 | else | |
192 | { | |
193 | i = 0; | |
194 | newargs = 0; | |
195 | } | |
196 | ||
197 | for (; i < len; i++) | |
198 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); | |
199 | ||
200 | n = newargs; | |
201 | ||
202 | for (i = 0; p1; | |
203 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++) | |
204 | { | |
205 | if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2)) | |
206 | { | |
8d08fdba MS |
207 | TREE_PURPOSE (n) = TREE_PURPOSE (p1); |
208 | any_change = 1; | |
209 | } | |
210 | else if (! TREE_PURPOSE (p1)) | |
211 | { | |
212 | if (TREE_PURPOSE (p2)) | |
213 | { | |
214 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); | |
215 | any_change = 1; | |
216 | } | |
217 | } | |
218 | else | |
219 | { | |
01512446 | 220 | if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1) |
a4443a08 | 221 | any_change = 1; |
8d08fdba MS |
222 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); |
223 | } | |
224 | if (TREE_VALUE (p1) != TREE_VALUE (p2)) | |
225 | { | |
226 | any_change = 1; | |
6da794e8 | 227 | TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2)); |
8d08fdba MS |
228 | } |
229 | else | |
230 | TREE_VALUE (n) = TREE_VALUE (p1); | |
231 | } | |
232 | if (! any_change) | |
970d6386 | 233 | return oldargs; |
8d08fdba MS |
234 | |
235 | return newargs; | |
236 | } | |
237 | ||
f2ee215b BK |
238 | /* Given a type, perhaps copied for a typedef, |
239 | find the "original" version of it. */ | |
993acaec | 240 | static tree |
5671bf27 | 241 | original_type (tree t) |
f2ee215b | 242 | { |
8e30dcf3 | 243 | int quals = cp_type_quals (t); |
2b643eda MM |
244 | while (t != error_mark_node |
245 | && TYPE_NAME (t) != NULL_TREE) | |
f2ee215b BK |
246 | { |
247 | tree x = TYPE_NAME (t); | |
248 | if (TREE_CODE (x) != TYPE_DECL) | |
249 | break; | |
250 | x = DECL_ORIGINAL_TYPE (x); | |
251 | if (x == NULL_TREE) | |
252 | break; | |
253 | t = x; | |
254 | } | |
8e30dcf3 | 255 | return cp_build_qualified_type (t, quals); |
f2ee215b BK |
256 | } |
257 | ||
98f2f3a2 MLI |
258 | /* Return the common type for two arithmetic types T1 and T2 under the |
259 | usual arithmetic conversions. The default conversions have already | |
260 | been applied, and enumerated types converted to their compatible | |
261 | integer types. */ | |
a7a64a77 | 262 | |
98f2f3a2 MLI |
263 | static tree |
264 | cp_common_type (tree t1, tree t2) | |
a7a64a77 MM |
265 | { |
266 | enum tree_code code1 = TREE_CODE (t1); | |
267 | enum tree_code code2 = TREE_CODE (t2); | |
268 | tree attributes; | |
78a7c317 | 269 | int i; |
a7a64a77 | 270 | |
c5ee1358 | 271 | |
550a799d JM |
272 | /* In what follows, we slightly generalize the rules given in [expr] so |
273 | as to deal with `long long' and `complex'. First, merge the | |
274 | attributes. */ | |
275 | attributes = (*targetm.merge_type_attributes) (t1, t2); | |
276 | ||
277 | if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2)) | |
278 | { | |
279 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
280 | return build_type_attribute_variant (t1, attributes); | |
281 | else | |
282 | return NULL_TREE; | |
283 | } | |
284 | ||
a7a64a77 | 285 | /* FIXME: Attributes. */ |
c8094d83 | 286 | gcc_assert (ARITHMETIC_TYPE_P (t1) |
b55b02ea | 287 | || VECTOR_TYPE_P (t1) |
adf2edec | 288 | || UNSCOPED_ENUM_P (t1)); |
c8094d83 | 289 | gcc_assert (ARITHMETIC_TYPE_P (t2) |
b55b02ea | 290 | || VECTOR_TYPE_P (t2) |
adf2edec | 291 | || UNSCOPED_ENUM_P (t2)); |
a7a64a77 | 292 | |
6da794e8 JM |
293 | /* If one type is complex, form the common type of the non-complex |
294 | components, then make that complex. Use T1 or T2 if it is the | |
295 | required type. */ | |
296 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
297 | { | |
298 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
299 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
300 | tree subtype | |
301 | = type_after_usual_arithmetic_conversions (subtype1, subtype2); | |
302 | ||
303 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
304 | return build_type_attribute_variant (t1, attributes); | |
305 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
306 | return build_type_attribute_variant (t2, attributes); | |
307 | else | |
308 | return build_type_attribute_variant (build_complex_type (subtype), | |
309 | attributes); | |
310 | } | |
311 | ||
73042643 AH |
312 | if (code1 == VECTOR_TYPE) |
313 | { | |
314 | /* When we get here we should have two vectors of the same size. | |
315 | Just prefer the unsigned one if present. */ | |
316 | if (TYPE_UNSIGNED (t1)) | |
317 | return build_type_attribute_variant (t1, attributes); | |
318 | else | |
319 | return build_type_attribute_variant (t2, attributes); | |
320 | } | |
321 | ||
a7a64a77 MM |
322 | /* If only one is real, use it as the result. */ |
323 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
324 | return build_type_attribute_variant (t1, attributes); | |
325 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
326 | return build_type_attribute_variant (t2, attributes); | |
327 | ||
a7a64a77 MM |
328 | /* Both real or both integers; use the one with greater precision. */ |
329 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
330 | return build_type_attribute_variant (t1, attributes); | |
331 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
332 | return build_type_attribute_variant (t2, attributes); | |
333 | ||
2f4d058f AH |
334 | /* The types are the same; no need to do anything fancy. */ |
335 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
336 | return build_type_attribute_variant (t1, attributes); | |
337 | ||
a7a64a77 MM |
338 | if (code1 != REAL_TYPE) |
339 | { | |
340 | /* If one is unsigned long long, then convert the other to unsigned | |
341 | long long. */ | |
342 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node) | |
343 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node)) | |
344 | return build_type_attribute_variant (long_long_unsigned_type_node, | |
345 | attributes); | |
346 | /* If one is a long long, and the other is an unsigned long, and | |
347 | long long can represent all the values of an unsigned long, then | |
348 | convert to a long long. Otherwise, convert to an unsigned long | |
349 | long. Otherwise, if either operand is long long, convert the | |
350 | other to long long. | |
c8094d83 | 351 | |
a7a64a77 MM |
352 | Since we're here, we know the TYPE_PRECISION is the same; |
353 | therefore converting to long long cannot represent all the values | |
354 | of an unsigned long, so we choose unsigned long long in that | |
355 | case. */ | |
356 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node) | |
357 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node)) | |
358 | { | |
8df83eae | 359 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
c8094d83 | 360 | ? long_long_unsigned_type_node |
a7a64a77 MM |
361 | : long_long_integer_type_node); |
362 | return build_type_attribute_variant (t, attributes); | |
363 | } | |
c8094d83 | 364 | |
a7a64a77 MM |
365 | /* Go through the same procedure, but for longs. */ |
366 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node) | |
367 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node)) | |
368 | return build_type_attribute_variant (long_unsigned_type_node, | |
369 | attributes); | |
370 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node) | |
371 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node)) | |
372 | { | |
8df83eae | 373 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
a7a64a77 MM |
374 | ? long_unsigned_type_node : long_integer_type_node); |
375 | return build_type_attribute_variant (t, attributes); | |
376 | } | |
9edc6e4c DD |
377 | |
378 | /* For __intN types, either the type is __int128 (and is lower | |
379 | priority than the types checked above, but higher than other | |
380 | 128-bit types) or it's known to not be the same size as other | |
381 | types (enforced in toplev.c). Prefer the unsigned type. */ | |
382 | for (i = 0; i < NUM_INT_N_ENTS; i ++) | |
383 | { | |
384 | if (int_n_enabled_p [i] | |
385 | && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type) | |
386 | || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type) | |
387 | || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type) | |
388 | || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type))) | |
389 | { | |
390 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
391 | ? int_n_trees[i].unsigned_type | |
392 | : int_n_trees[i].signed_type); | |
393 | return build_type_attribute_variant (t, attributes); | |
394 | } | |
395 | } | |
396 | ||
a7a64a77 | 397 | /* Otherwise prefer the unsigned one. */ |
8df83eae | 398 | if (TYPE_UNSIGNED (t1)) |
a7a64a77 MM |
399 | return build_type_attribute_variant (t1, attributes); |
400 | else | |
401 | return build_type_attribute_variant (t2, attributes); | |
402 | } | |
403 | else | |
404 | { | |
405 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node) | |
406 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node)) | |
407 | return build_type_attribute_variant (long_double_type_node, | |
408 | attributes); | |
409 | if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node) | |
410 | || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node)) | |
411 | return build_type_attribute_variant (double_type_node, | |
412 | attributes); | |
2f4d058f AH |
413 | if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node) |
414 | || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node)) | |
a7a64a77 MM |
415 | return build_type_attribute_variant (float_type_node, |
416 | attributes); | |
c8094d83 | 417 | |
2f4d058f | 418 | /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of |
0cbd7506 MS |
419 | the standard C++ floating-point types. Logic earlier in this |
420 | function has already eliminated the possibility that | |
421 | TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no | |
422 | compelling reason to choose one or the other. */ | |
2f4d058f | 423 | return build_type_attribute_variant (t1, attributes); |
a7a64a77 MM |
424 | } |
425 | } | |
426 | ||
98f2f3a2 MLI |
427 | /* T1 and T2 are arithmetic or enumeration types. Return the type |
428 | that will result from the "usual arithmetic conversions" on T1 and | |
429 | T2 as described in [expr]. */ | |
430 | ||
431 | tree | |
432 | type_after_usual_arithmetic_conversions (tree t1, tree t2) | |
433 | { | |
434 | gcc_assert (ARITHMETIC_TYPE_P (t1) | |
b55b02ea | 435 | || VECTOR_TYPE_P (t1) |
98f2f3a2 MLI |
436 | || UNSCOPED_ENUM_P (t1)); |
437 | gcc_assert (ARITHMETIC_TYPE_P (t2) | |
b55b02ea | 438 | || VECTOR_TYPE_P (t2) |
98f2f3a2 MLI |
439 | || UNSCOPED_ENUM_P (t2)); |
440 | ||
441 | /* Perform the integral promotions. We do not promote real types here. */ | |
442 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1) | |
6f9b088b | 443 | && INTEGRAL_OR_ENUMERATION_TYPE_P (t2)) |
98f2f3a2 MLI |
444 | { |
445 | t1 = type_promotes_to (t1); | |
446 | t2 = type_promotes_to (t2); | |
447 | } | |
448 | ||
449 | return cp_common_type (t1, t2); | |
450 | } | |
451 | ||
083586b8 NF |
452 | static void |
453 | composite_pointer_error (diagnostic_t kind, tree t1, tree t2, | |
454 | composite_pointer_operation operation) | |
455 | { | |
456 | switch (operation) | |
457 | { | |
458 | case CPO_COMPARISON: | |
459 | emit_diagnostic (kind, input_location, 0, | |
460 | "comparison between " | |
461 | "distinct pointer types %qT and %qT lacks a cast", | |
462 | t1, t2); | |
463 | break; | |
464 | case CPO_CONVERSION: | |
465 | emit_diagnostic (kind, input_location, 0, | |
466 | "conversion between " | |
467 | "distinct pointer types %qT and %qT lacks a cast", | |
468 | t1, t2); | |
469 | break; | |
470 | case CPO_CONDITIONAL_EXPR: | |
471 | emit_diagnostic (kind, input_location, 0, | |
472 | "conditional expression between " | |
473 | "distinct pointer types %qT and %qT lacks a cast", | |
474 | t1, t2); | |
475 | break; | |
476 | default: | |
477 | gcc_unreachable (); | |
478 | } | |
479 | } | |
480 | ||
a5ac359a | 481 | /* Subroutine of composite_pointer_type to implement the recursive |
c86818cf | 482 | case. See that function for documentation of the parameters. */ |
a5ac359a MM |
483 | |
484 | static tree | |
c86818cf SZ |
485 | composite_pointer_type_r (tree t1, tree t2, |
486 | composite_pointer_operation operation, | |
5ade1ed2 | 487 | tsubst_flags_t complain) |
a5ac359a MM |
488 | { |
489 | tree pointee1; | |
490 | tree pointee2; | |
491 | tree result_type; | |
492 | tree attributes; | |
493 | ||
494 | /* Determine the types pointed to by T1 and T2. */ | |
50e10fa8 | 495 | if (TYPE_PTR_P (t1)) |
a5ac359a MM |
496 | { |
497 | pointee1 = TREE_TYPE (t1); | |
498 | pointee2 = TREE_TYPE (t2); | |
499 | } | |
500 | else | |
501 | { | |
502 | pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1); | |
503 | pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2); | |
504 | } | |
505 | ||
506 | /* [expr.rel] | |
507 | ||
508 | Otherwise, the composite pointer type is a pointer type | |
509 | similar (_conv.qual_) to the type of one of the operands, | |
510 | with a cv-qualification signature (_conv.qual_) that is the | |
511 | union of the cv-qualification signatures of the operand | |
512 | types. */ | |
513 | if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2)) | |
514 | result_type = pointee1; | |
50e10fa8 | 515 | else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2)) |
66b1156a | 516 | || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2))) |
df9ee5c8 PC |
517 | { |
518 | result_type = composite_pointer_type_r (pointee1, pointee2, operation, | |
519 | complain); | |
520 | if (result_type == error_mark_node) | |
521 | return error_mark_node; | |
522 | } | |
a5ac359a MM |
523 | else |
524 | { | |
5ade1ed2 | 525 | if (complain & tf_error) |
083586b8 | 526 | composite_pointer_error (DK_PERMERROR, t1, t2, operation); |
7a1db261 JM |
527 | else |
528 | return error_mark_node; | |
a5ac359a MM |
529 | result_type = void_type_node; |
530 | } | |
531 | result_type = cp_build_qualified_type (result_type, | |
532 | (cp_type_quals (pointee1) | |
533 | | cp_type_quals (pointee2))); | |
a5ac359a MM |
534 | /* If the original types were pointers to members, so is the |
535 | result. */ | |
66b1156a | 536 | if (TYPE_PTRMEM_P (t1)) |
a5ac359a MM |
537 | { |
538 | if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), | |
7a1db261 JM |
539 | TYPE_PTRMEM_CLASS_TYPE (t2))) |
540 | { | |
541 | if (complain & tf_error) | |
542 | composite_pointer_error (DK_PERMERROR, t1, t2, operation); | |
543 | else | |
544 | return error_mark_node; | |
545 | } | |
a5ac359a MM |
546 | result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), |
547 | result_type); | |
548 | } | |
9d363a56 MM |
549 | else |
550 | result_type = build_pointer_type (result_type); | |
a5ac359a MM |
551 | |
552 | /* Merge the attributes. */ | |
553 | attributes = (*targetm.merge_type_attributes) (t1, t2); | |
554 | return build_type_attribute_variant (result_type, attributes); | |
555 | } | |
556 | ||
a7a64a77 | 557 | /* Return the composite pointer type (see [expr.rel]) for T1 and T2. |
c86818cf SZ |
558 | ARG1 and ARG2 are the values with those types. The OPERATION is to |
559 | describe the operation between the pointer types, | |
560 | in case an error occurs. | |
a5ac359a MM |
561 | |
562 | This routine also implements the computation of a common type for | |
563 | pointers-to-members as per [expr.eq]. */ | |
a7a64a77 | 564 | |
c8094d83 | 565 | tree |
5671bf27 | 566 | composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2, |
c86818cf SZ |
567 | composite_pointer_operation operation, |
568 | tsubst_flags_t complain) | |
a7a64a77 | 569 | { |
a5ac359a MM |
570 | tree class1; |
571 | tree class2; | |
a7a64a77 MM |
572 | |
573 | /* [expr.rel] | |
574 | ||
575 | If one operand is a null pointer constant, the composite pointer | |
576 | type is the type of the other operand. */ | |
577 | if (null_ptr_cst_p (arg1)) | |
578 | return t2; | |
579 | if (null_ptr_cst_p (arg2)) | |
580 | return t1; | |
c8094d83 | 581 | |
634790f4 MM |
582 | /* We have: |
583 | ||
584 | [expr.rel] | |
585 | ||
586 | If one of the operands has type "pointer to cv1 void*", then | |
587 | the other has type "pointer to cv2T", and the composite pointer | |
588 | type is "pointer to cv12 void", where cv12 is the union of cv1 | |
589 | and cv2. | |
590 | ||
591 | If either type is a pointer to void, make sure it is T1. */ | |
50e10fa8 | 592 | if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2))) |
fab27f52 | 593 | std::swap (t1, t2); |
a5ac359a | 594 | |
634790f4 | 595 | /* Now, if T1 is a pointer to void, merge the qualifiers. */ |
50e10fa8 | 596 | if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1))) |
a7a64a77 | 597 | { |
a5ac359a MM |
598 | tree attributes; |
599 | tree result_type; | |
600 | ||
aa0a659c PC |
601 | if (TYPE_PTRFN_P (t2)) |
602 | { | |
603 | if (complain & tf_error) | |
604 | { | |
605 | switch (operation) | |
606 | { | |
607 | case CPO_COMPARISON: | |
608 | pedwarn (input_location, OPT_Wpedantic, | |
609 | "ISO C++ forbids comparison between pointer " | |
610 | "of type %<void *%> and pointer-to-function"); | |
611 | break; | |
612 | case CPO_CONVERSION: | |
613 | pedwarn (input_location, OPT_Wpedantic, | |
614 | "ISO C++ forbids conversion between pointer " | |
615 | "of type %<void *%> and pointer-to-function"); | |
616 | break; | |
617 | case CPO_CONDITIONAL_EXPR: | |
618 | pedwarn (input_location, OPT_Wpedantic, | |
619 | "ISO C++ forbids conditional expression between " | |
620 | "pointer of type %<void *%> and " | |
621 | "pointer-to-function"); | |
622 | break; | |
623 | default: | |
624 | gcc_unreachable (); | |
625 | } | |
626 | } | |
627 | else | |
628 | return error_mark_node; | |
c86818cf | 629 | } |
c8094d83 | 630 | result_type |
a5ac359a MM |
631 | = cp_build_qualified_type (void_type_node, |
632 | (cp_type_quals (TREE_TYPE (t1)) | |
633 | | cp_type_quals (TREE_TYPE (t2)))); | |
634790f4 | 634 | result_type = build_pointer_type (result_type); |
a5ac359a MM |
635 | /* Merge the attributes. */ |
636 | attributes = (*targetm.merge_type_attributes) (t1, t2); | |
637 | return build_type_attribute_variant (result_type, attributes); | |
638 | } | |
639 | ||
50e10fa8 PC |
640 | if (c_dialect_objc () && TYPE_PTR_P (t1) |
641 | && TYPE_PTR_P (t2)) | |
660845bf | 642 | { |
b581b85b NP |
643 | if (objc_have_common_type (t1, t2, -3, NULL_TREE)) |
644 | return objc_common_type (t1, t2); | |
660845bf ZL |
645 | } |
646 | ||
51dc6603 JM |
647 | /* if T1 or T2 is "pointer to noexcept function" and the other type is |
648 | "pointer to function", where the function types are otherwise the same, | |
649 | "pointer to function" */ | |
650 | if (fnptr_conv_p (t1, t2)) | |
651 | return t1; | |
652 | if (fnptr_conv_p (t2, t1)) | |
653 | return t2; | |
654 | ||
a5ac359a MM |
655 | /* [expr.eq] permits the application of a pointer conversion to |
656 | bring the pointers to a common type. */ | |
50e10fa8 | 657 | if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2) |
a5ac359a MM |
658 | && CLASS_TYPE_P (TREE_TYPE (t1)) |
659 | && CLASS_TYPE_P (TREE_TYPE (t2)) | |
660 | && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1), | |
661 | TREE_TYPE (t2))) | |
662 | { | |
663 | class1 = TREE_TYPE (t1); | |
664 | class2 = TREE_TYPE (t2); | |
665 | ||
660845bf | 666 | if (DERIVED_FROM_P (class1, class2)) |
c8094d83 | 667 | t2 = (build_pointer_type |
a3360e77 | 668 | (cp_build_qualified_type (class1, cp_type_quals (class2)))); |
660845bf | 669 | else if (DERIVED_FROM_P (class2, class1)) |
c8094d83 | 670 | t1 = (build_pointer_type |
a3360e77 | 671 | (cp_build_qualified_type (class2, cp_type_quals (class1)))); |
a5ac359a | 672 | else |
c86818cf SZ |
673 | { |
674 | if (complain & tf_error) | |
083586b8 | 675 | composite_pointer_error (DK_ERROR, t1, t2, operation); |
c86818cf SZ |
676 | return error_mark_node; |
677 | } | |
a7a64a77 | 678 | } |
a5ac359a MM |
679 | /* [expr.eq] permits the application of a pointer-to-member |
680 | conversion to change the class type of one of the types. */ | |
66b1156a | 681 | else if (TYPE_PTRMEM_P (t1) |
c86818cf | 682 | && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), |
a5ac359a | 683 | TYPE_PTRMEM_CLASS_TYPE (t2))) |
708cae97 | 684 | { |
a5ac359a MM |
685 | class1 = TYPE_PTRMEM_CLASS_TYPE (t1); |
686 | class2 = TYPE_PTRMEM_CLASS_TYPE (t2); | |
708cae97 | 687 | |
a5ac359a MM |
688 | if (DERIVED_FROM_P (class1, class2)) |
689 | t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1)); | |
690 | else if (DERIVED_FROM_P (class2, class1)) | |
691 | t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2)); | |
a7a64a77 | 692 | else |
c86818cf SZ |
693 | { |
694 | if (complain & tf_error) | |
695 | switch (operation) | |
696 | { | |
697 | case CPO_COMPARISON: | |
698 | error ("comparison between distinct " | |
699 | "pointer-to-member types %qT and %qT lacks a cast", | |
700 | t1, t2); | |
701 | break; | |
702 | case CPO_CONVERSION: | |
703 | error ("conversion between distinct " | |
704 | "pointer-to-member types %qT and %qT lacks a cast", | |
705 | t1, t2); | |
706 | break; | |
707 | case CPO_CONDITIONAL_EXPR: | |
708 | error ("conditional expression between distinct " | |
709 | "pointer-to-member types %qT and %qT lacks a cast", | |
710 | t1, t2); | |
711 | break; | |
712 | default: | |
713 | gcc_unreachable (); | |
714 | } | |
715 | return error_mark_node; | |
716 | } | |
717 | } | |
718 | ||
719 | return composite_pointer_type_r (t1, t2, operation, complain); | |
a7a64a77 MM |
720 | } |
721 | ||
6da794e8 | 722 | /* Return the merged type of two types. |
96d84882 | 723 | We assume that comptypes has already been done and returned 1; |
8d08fdba MS |
724 | if that isn't so, this may crash. |
725 | ||
6da794e8 JM |
726 | This just combines attributes and default arguments; any other |
727 | differences would cause the two types to compare unalike. */ | |
8d08fdba MS |
728 | |
729 | tree | |
5671bf27 | 730 | merge_types (tree t1, tree t2) |
8d08fdba | 731 | { |
926ce8bd KH |
732 | enum tree_code code1; |
733 | enum tree_code code2; | |
2986ae00 | 734 | tree attributes; |
8d08fdba MS |
735 | |
736 | /* Save time if the two types are the same. */ | |
f2ee215b | 737 | if (t1 == t2) |
9076e292 | 738 | return t1; |
6da794e8 | 739 | if (original_type (t1) == original_type (t2)) |
9076e292 | 740 | return t1; |
8d08fdba MS |
741 | |
742 | /* If one type is nonsense, use the other. */ | |
743 | if (t1 == error_mark_node) | |
9076e292 | 744 | return t2; |
8d08fdba | 745 | if (t2 == error_mark_node) |
9076e292 | 746 | return t1; |
8d08fdba | 747 | |
852497a3 JM |
748 | /* Handle merging an auto redeclaration with a previous deduced |
749 | return type. */ | |
750 | if (is_auto (t1)) | |
751 | return t2; | |
752 | ||
d9525bec | 753 | /* Merge the attributes. */ |
f6897b10 | 754 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
2986ae00 | 755 | |
5566b478 MS |
756 | if (TYPE_PTRMEMFUNC_P (t1)) |
757 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); | |
758 | if (TYPE_PTRMEMFUNC_P (t2)) | |
759 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); | |
760 | ||
8d08fdba MS |
761 | code1 = TREE_CODE (t1); |
762 | code2 = TREE_CODE (t2); | |
e350dbbd PB |
763 | if (code1 != code2) |
764 | { | |
765 | gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE); | |
766 | if (code1 == TYPENAME_TYPE) | |
767 | { | |
768 | t1 = resolve_typename_type (t1, /*only_current_p=*/true); | |
769 | code1 = TREE_CODE (t1); | |
770 | } | |
771 | else | |
772 | { | |
773 | t2 = resolve_typename_type (t2, /*only_current_p=*/true); | |
774 | code2 = TREE_CODE (t2); | |
775 | } | |
776 | } | |
8d08fdba MS |
777 | |
778 | switch (code1) | |
779 | { | |
8d08fdba MS |
780 | case POINTER_TYPE: |
781 | case REFERENCE_TYPE: | |
6da794e8 | 782 | /* For two pointers, do this recursively on the target type. */ |
8d08fdba | 783 | { |
6da794e8 | 784 | tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
77adef84 | 785 | int quals = cp_type_quals (t1); |
5b163de4 | 786 | |
8d08fdba | 787 | if (code1 == POINTER_TYPE) |
62245e6f JM |
788 | { |
789 | t1 = build_pointer_type (target); | |
790 | if (TREE_CODE (target) == METHOD_TYPE) | |
791 | t1 = build_ptrmemfunc_type (t1); | |
792 | } | |
8d08fdba | 793 | else |
86089be5 | 794 | t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1)); |
71851aaa | 795 | t1 = build_type_attribute_variant (t1, attributes); |
77adef84 | 796 | t1 = cp_build_qualified_type (t1, quals); |
71851aaa | 797 | |
71851aaa | 798 | return t1; |
8d08fdba | 799 | } |
8d08fdba | 800 | |
6da794e8 JM |
801 | case OFFSET_TYPE: |
802 | { | |
fe0378ed MM |
803 | int quals; |
804 | tree pointee; | |
805 | quals = cp_type_quals (t1); | |
806 | pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1), | |
807 | TYPE_PTRMEM_POINTED_TO_TYPE (t2)); | |
808 | t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), | |
809 | pointee); | |
810 | t1 = cp_build_qualified_type (t1, quals); | |
6da794e8 JM |
811 | break; |
812 | } | |
813 | ||
8d08fdba MS |
814 | case ARRAY_TYPE: |
815 | { | |
6da794e8 | 816 | tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
8d08fdba MS |
817 | /* Save space: see if the result is identical to one of the args. */ |
818 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) | |
2986ae00 | 819 | return build_type_attribute_variant (t1, attributes); |
8d08fdba | 820 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) |
2986ae00 | 821 | return build_type_attribute_variant (t2, attributes); |
8d08fdba | 822 | /* Merge the element types, and have a size if either arg has one. */ |
e349ee73 MS |
823 | t1 = build_cplus_array_type |
824 | (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); | |
6da794e8 | 825 | break; |
8d08fdba MS |
826 | } |
827 | ||
828 | case FUNCTION_TYPE: | |
829 | /* Function types: prefer the one that specified arg types. | |
830 | If both do, merge the arg types. Also merge the return types. */ | |
831 | { | |
6da794e8 | 832 | tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
8d08fdba MS |
833 | tree p1 = TYPE_ARG_TYPES (t1); |
834 | tree p2 = TYPE_ARG_TYPES (t2); | |
a0685b73 | 835 | tree parms; |
8d08fdba MS |
836 | |
837 | /* Save space: see if the result is identical to one of the args. */ | |
838 | if (valtype == TREE_TYPE (t1) && ! p2) | |
e9525111 | 839 | return cp_build_type_attribute_variant (t1, attributes); |
8d08fdba | 840 | if (valtype == TREE_TYPE (t2) && ! p1) |
e9525111 | 841 | return cp_build_type_attribute_variant (t2, attributes); |
8d08fdba MS |
842 | |
843 | /* Simple way if one arg fails to specify argument types. */ | |
844 | if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node) | |
d8a0d13e | 845 | parms = p2; |
a0685b73 | 846 | else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node) |
d8a0d13e | 847 | parms = p1; |
a0685b73 | 848 | else |
d8a0d13e | 849 | parms = commonparms (p1, p2); |
8d08fdba | 850 | |
403f22aa JM |
851 | cp_cv_quals quals = type_memfn_quals (t1); |
852 | cp_ref_qualifier rqual = type_memfn_rqual (t1); | |
853 | gcc_assert (quals == type_memfn_quals (t2)); | |
854 | gcc_assert (rqual == type_memfn_rqual (t2)); | |
855 | ||
856 | tree rval = build_function_type (valtype, parms); | |
857 | rval = apply_memfn_quals (rval, quals); | |
858 | tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), | |
859 | TYPE_RAISES_EXCEPTIONS (t2)); | |
860 | bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1); | |
861 | t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p); | |
6da794e8 | 862 | break; |
8d08fdba MS |
863 | } |
864 | ||
6da794e8 JM |
865 | case METHOD_TYPE: |
866 | { | |
867 | /* Get this value the long way, since TYPE_METHOD_BASETYPE | |
868 | is just the main variant of this. */ | |
7e1352fe | 869 | tree basetype = class_of_this_parm (t2); |
d8a0d13e | 870 | tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), |
b15ea309 | 871 | TYPE_RAISES_EXCEPTIONS (t2)); |
51543dc7 | 872 | cp_ref_qualifier rqual = type_memfn_rqual (t1); |
6da794e8 | 873 | tree t3; |
cab421f4 | 874 | bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1); |
6da794e8 JM |
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); | |
43dc123f MM |
884 | t3 = build_method_type_directly (basetype, TREE_TYPE (t3), |
885 | TYPE_ARG_TYPES (t3)); | |
403f22aa | 886 | t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p); |
6da794e8 JM |
887 | break; |
888 | } | |
8d08fdba | 889 | |
b1a95e0b MM |
890 | case TYPENAME_TYPE: |
891 | /* There is no need to merge attributes into a TYPENAME_TYPE. | |
892 | When the type is instantiated it will have whatever | |
893 | attributes result from the instantiation. */ | |
894 | return t1; | |
895 | ||
6da794e8 | 896 | default:; |
3f759575 JM |
897 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) |
898 | return t1; | |
899 | else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
900 | return t2; | |
901 | break; | |
6da794e8 | 902 | } |
8e30dcf3 | 903 | |
3f759575 | 904 | return cp_build_type_attribute_variant (t1, attributes); |
6da794e8 | 905 | } |
8d08fdba | 906 | |
48326487 JM |
907 | /* Return the ARRAY_TYPE type without its domain. */ |
908 | ||
909 | tree | |
910 | strip_array_domain (tree type) | |
911 | { | |
912 | tree t2; | |
913 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
914 | if (TYPE_DOMAIN (type) == NULL_TREE) | |
915 | return type; | |
916 | t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); | |
917 | return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type)); | |
918 | } | |
919 | ||
98f2f3a2 MLI |
920 | /* Wrapper around cp_common_type that is used by c-common.c and other |
921 | front end optimizations that remove promotions. | |
71851aaa | 922 | |
98f2f3a2 MLI |
923 | Return the common type for two arithmetic types T1 and T2 under the |
924 | usual arithmetic conversions. The default conversions have already | |
925 | been applied, and enumerated types converted to their compatible | |
926 | integer types. */ | |
6da794e8 JM |
927 | |
928 | tree | |
5671bf27 | 929 | common_type (tree t1, tree t2) |
6da794e8 | 930 | { |
98f2f3a2 MLI |
931 | /* If one type is nonsense, use the other */ |
932 | if (t1 == error_mark_node) | |
933 | return t2; | |
934 | if (t2 == error_mark_node) | |
935 | return t1; | |
2986ae00 | 936 | |
98f2f3a2 MLI |
937 | return cp_common_type (t1, t2); |
938 | } | |
8d08fdba | 939 | |
98f2f3a2 MLI |
940 | /* Return the common type of two pointer types T1 and T2. This is the |
941 | type for the result of most arithmetic operations if the operands | |
942 | have the given two types. | |
943 | ||
944 | We assume that comp_target_types has already been done and returned | |
945 | nonzero; if that isn't so, this may crash. */ | |
8d08fdba | 946 | |
98f2f3a2 MLI |
947 | tree |
948 | common_pointer_type (tree t1, tree t2) | |
949 | { | |
950 | gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2)) | |
66b1156a | 951 | || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2)) |
98f2f3a2 MLI |
952 | || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2))); |
953 | ||
954 | return composite_pointer_type (t1, t2, error_mark_node, error_mark_node, | |
c86818cf | 955 | CPO_CONVERSION, tf_warning_or_error); |
8d08fdba MS |
956 | } |
957 | \f | |
4cc1d462 | 958 | /* Compare two exception specifier types for exactness or subsetness, if |
acd8e2d0 NS |
959 | allowed. Returns false for mismatch, true for match (same, or |
960 | derived and !exact). | |
c8094d83 | 961 | |
4cc1d462 | 962 | [except.spec] "If a class X ... objects of class X or any class publicly |
34cd5ae7 | 963 | and unambiguously derived from X. Similarly, if a pointer type Y * ... |
4cc1d462 | 964 | exceptions of type Y * or that are pointers to any type publicly and |
34cd5ae7 | 965 | unambiguously derived from Y. Otherwise a function only allows exceptions |
4cc1d462 NS |
966 | that have the same type ..." |
967 | This does not mention cv qualifiers and is different to what throw | |
968 | [except.throw] and catch [except.catch] will do. They will ignore the | |
969 | top level cv qualifiers, and allow qualifiers in the pointer to class | |
970 | example. | |
c8094d83 | 971 | |
4cc1d462 NS |
972 | We implement the letter of the standard. */ |
973 | ||
acd8e2d0 NS |
974 | static bool |
975 | comp_except_types (tree a, tree b, bool exact) | |
4cc1d462 NS |
976 | { |
977 | if (same_type_p (a, b)) | |
acd8e2d0 | 978 | return true; |
4cc1d462 NS |
979 | else if (!exact) |
980 | { | |
89d684bb | 981 | if (cp_type_quals (a) || cp_type_quals (b)) |
0cbd7506 | 982 | return false; |
c8094d83 | 983 | |
50e10fa8 | 984 | if (TYPE_PTR_P (a) && TYPE_PTR_P (b)) |
0cbd7506 MS |
985 | { |
986 | a = TREE_TYPE (a); | |
987 | b = TREE_TYPE (b); | |
988 | if (cp_type_quals (a) || cp_type_quals (b)) | |
989 | return false; | |
990 | } | |
c8094d83 | 991 | |
4cc1d462 | 992 | if (TREE_CODE (a) != RECORD_TYPE |
0cbd7506 MS |
993 | || TREE_CODE (b) != RECORD_TYPE) |
994 | return false; | |
c8094d83 | 995 | |
22854930 | 996 | if (publicly_uniquely_derived_p (a, b)) |
0cbd7506 | 997 | return true; |
4cc1d462 | 998 | } |
acd8e2d0 | 999 | return false; |
4cc1d462 NS |
1000 | } |
1001 | ||
acd8e2d0 | 1002 | /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers. |
3a55fb4c | 1003 | If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5). |
51dc6603 | 1004 | If EXACT is ce_type, the C++17 type compatibility rules apply. |
3a55fb4c JM |
1005 | If EXACT is ce_normal, the compatibility rules in 15.4/3 apply. |
1006 | If EXACT is ce_exact, the specs must be exactly the same. Exception lists | |
1007 | are unordered, but we've already filtered out duplicates. Most lists will | |
1008 | be in order, we should try to make use of that. */ | |
e92cc029 | 1009 | |
acd8e2d0 | 1010 | bool |
3a55fb4c | 1011 | comp_except_specs (const_tree t1, const_tree t2, int exact) |
8d08fdba | 1012 | { |
58f9752a KG |
1013 | const_tree probe; |
1014 | const_tree base; | |
4cc1d462 NS |
1015 | int length = 0; |
1016 | ||
1017 | if (t1 == t2) | |
acd8e2d0 | 1018 | return true; |
c8094d83 | 1019 | |
6d812dd3 JM |
1020 | /* First handle noexcept. */ |
1021 | if (exact < ce_exact) | |
3a55fb4c | 1022 | { |
51dc6603 JM |
1023 | if (exact == ce_type |
1024 | && (canonical_eh_spec (CONST_CAST_TREE (t1)) | |
1025 | == canonical_eh_spec (CONST_CAST_TREE (t2)))) | |
1026 | return true; | |
1027 | ||
5e3f417f | 1028 | /* noexcept(false) is compatible with no exception-specification, |
51dc6603 | 1029 | and less strict than any spec. */ |
6d812dd3 | 1030 | if (t1 == noexcept_false_spec) |
5e3f417f JM |
1031 | return t2 == NULL_TREE || exact == ce_derived; |
1032 | /* Even a derived noexcept(false) is compatible with no | |
1033 | exception-specification. */ | |
6d812dd3 | 1034 | if (t2 == noexcept_false_spec) |
5e3f417f | 1035 | return t1 == NULL_TREE; |
6d812dd3 JM |
1036 | |
1037 | /* Otherwise, if we aren't looking for an exact match, noexcept is | |
1038 | equivalent to throw(). */ | |
1039 | if (t1 == noexcept_true_spec) | |
1040 | t1 = empty_except_spec; | |
1041 | if (t2 == noexcept_true_spec) | |
1042 | t2 = empty_except_spec; | |
3a55fb4c JM |
1043 | } |
1044 | ||
6d812dd3 JM |
1045 | /* If any noexcept is left, it is only comparable to itself; |
1046 | either we're looking for an exact match or we're redeclaring a | |
1047 | template with dependent noexcept. */ | |
1048 | if ((t1 && TREE_PURPOSE (t1)) | |
1049 | || (t2 && TREE_PURPOSE (t2))) | |
1050 | return (t1 && t2 | |
1051 | && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))); | |
1052 | ||
0cbd7506 | 1053 | if (t1 == NULL_TREE) /* T1 is ... */ |
3a55fb4c | 1054 | return t2 == NULL_TREE || exact == ce_derived; |
0cbd7506 | 1055 | if (!TREE_VALUE (t1)) /* t1 is EMPTY */ |
4cc1d462 | 1056 | return t2 != NULL_TREE && !TREE_VALUE (t2); |
0cbd7506 | 1057 | if (t2 == NULL_TREE) /* T2 is ... */ |
acd8e2d0 | 1058 | return false; |
dffa4176 | 1059 | if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */ |
3a55fb4c | 1060 | return exact == ce_derived; |
c8094d83 | 1061 | |
4cc1d462 NS |
1062 | /* Neither set is ... or EMPTY, make sure each part of T2 is in T1. |
1063 | Count how many we find, to determine exactness. For exact matching and | |
1064 | ordered T1, T2, this is an O(n) operation, otherwise its worst case is | |
1065 | O(nm). */ | |
1066 | for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2)) | |
1067 | { | |
1068 | for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe)) | |
0cbd7506 MS |
1069 | { |
1070 | tree a = TREE_VALUE (probe); | |
1071 | tree b = TREE_VALUE (t2); | |
1072 | ||
1073 | if (comp_except_types (a, b, exact)) | |
1074 | { | |
3a55fb4c | 1075 | if (probe == base && exact > ce_derived) |
0cbd7506 MS |
1076 | base = TREE_CHAIN (probe); |
1077 | length++; | |
1078 | break; | |
1079 | } | |
1080 | } | |
4cc1d462 | 1081 | if (probe == NULL_TREE) |
0cbd7506 | 1082 | return false; |
4cc1d462 | 1083 | } |
3a55fb4c | 1084 | return exact == ce_derived || base == NULL_TREE || length == list_length (t1); |
8d08fdba MS |
1085 | } |
1086 | ||
acd8e2d0 NS |
1087 | /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if |
1088 | [] can match [size]. */ | |
3bfdc719 | 1089 | |
c8a209ca | 1090 | static bool |
58f9752a | 1091 | comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration) |
8d08fdba | 1092 | { |
3bfdc719 MM |
1093 | tree d1; |
1094 | tree d2; | |
30a03508 | 1095 | tree max1, max2; |
3bfdc719 MM |
1096 | |
1097 | if (t1 == t2) | |
acd8e2d0 | 1098 | return true; |
8d08fdba | 1099 | |
3bfdc719 | 1100 | /* The type of the array elements must be the same. */ |
acd8e2d0 | 1101 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
c8a209ca | 1102 | return false; |
8d08fdba | 1103 | |
3bfdc719 MM |
1104 | d1 = TYPE_DOMAIN (t1); |
1105 | d2 = TYPE_DOMAIN (t2); | |
1106 | ||
1107 | if (d1 == d2) | |
c8a209ca | 1108 | return true; |
8d08fdba | 1109 | |
3bfdc719 | 1110 | /* If one of the arrays is dimensionless, and the other has a |
0e339752 | 1111 | dimension, they are of different types. However, it is valid to |
3bfdc719 MM |
1112 | write: |
1113 | ||
1114 | extern int a[]; | |
1115 | int a[3]; | |
1116 | ||
c8094d83 | 1117 | by [basic.link]: |
3bfdc719 MM |
1118 | |
1119 | declarations for an array object can specify | |
1120 | array types that differ by the presence or absence of a major | |
1121 | array bound (_dcl.array_). */ | |
1122 | if (!d1 || !d2) | |
acd8e2d0 | 1123 | return allow_redeclaration; |
3bfdc719 MM |
1124 | |
1125 | /* Check that the dimensions are the same. */ | |
30a03508 NS |
1126 | |
1127 | if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))) | |
1128 | return false; | |
1129 | max1 = TYPE_MAX_VALUE (d1); | |
1130 | max2 = TYPE_MAX_VALUE (d2); | |
30a03508 NS |
1131 | |
1132 | if (!cp_tree_equal (max1, max2)) | |
1133 | return false; | |
1134 | ||
1135 | return true; | |
8d08fdba MS |
1136 | } |
1137 | ||
e96ce650 DS |
1138 | /* Compare the relative position of T1 and T2 into their respective |
1139 | template parameter list. | |
1140 | T1 and T2 must be template parameter types. | |
1141 | Return TRUE if T1 and T2 have the same position, FALSE otherwise. */ | |
1142 | ||
1143 | static bool | |
1144 | comp_template_parms_position (tree t1, tree t2) | |
1145 | { | |
6f1abb06 | 1146 | tree index1, index2; |
e96ce650 DS |
1147 | gcc_assert (t1 && t2 |
1148 | && TREE_CODE (t1) == TREE_CODE (t2) | |
1149 | && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM | |
1150 | || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM | |
1151 | || TREE_CODE (t1) == TEMPLATE_TYPE_PARM)); | |
1152 | ||
6f1abb06 DS |
1153 | index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1)); |
1154 | index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2)); | |
9cf10655 | 1155 | |
6f1abb06 DS |
1156 | /* Then compare their relative position. */ |
1157 | if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2) | |
1158 | || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2) | |
1159 | || (TEMPLATE_PARM_PARAMETER_PACK (index1) | |
1160 | != TEMPLATE_PARM_PARAMETER_PACK (index2))) | |
9cf10655 DS |
1161 | return false; |
1162 | ||
d08a068b JM |
1163 | /* In C++14 we can end up comparing 'auto' to a normal template |
1164 | parameter. Don't confuse them. */ | |
e4276ba5 | 1165 | if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2))) |
d08a068b JM |
1166 | return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2); |
1167 | ||
6f1abb06 | 1168 | return true; |
9cf10655 DS |
1169 | } |
1170 | ||
c65e18d3 BE |
1171 | /* Heuristic check if two parameter types can be considered ABI-equivalent. */ |
1172 | ||
1173 | static bool | |
1174 | cxx_safe_arg_type_equiv_p (tree t1, tree t2) | |
1175 | { | |
1176 | t1 = TYPE_MAIN_VARIANT (t1); | |
1177 | t2 = TYPE_MAIN_VARIANT (t2); | |
1178 | ||
9f613f06 PC |
1179 | if (TYPE_PTR_P (t1) |
1180 | && TYPE_PTR_P (t2)) | |
c65e18d3 BE |
1181 | return true; |
1182 | ||
1183 | /* The signedness of the parameter matters only when an integral | |
1184 | type smaller than int is promoted to int, otherwise only the | |
1185 | precision of the parameter matters. | |
1186 | This check should make sure that the callee does not see | |
1187 | undefined values in argument registers. */ | |
1188 | if (INTEGRAL_TYPE_P (t1) | |
1189 | && INTEGRAL_TYPE_P (t2) | |
1190 | && TYPE_PRECISION (t1) == TYPE_PRECISION (t2) | |
1191 | && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2) | |
1192 | || !targetm.calls.promote_prototypes (NULL_TREE) | |
1193 | || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node))) | |
1194 | return true; | |
1195 | ||
1196 | return same_type_p (t1, t2); | |
1197 | } | |
1198 | ||
1199 | /* Check if a type cast between two function types can be considered safe. */ | |
1200 | ||
1201 | static bool | |
1202 | cxx_safe_function_type_cast_p (tree t1, tree t2) | |
1203 | { | |
1204 | if (TREE_TYPE (t1) == void_type_node && | |
1205 | TYPE_ARG_TYPES (t1) == void_list_node) | |
1206 | return true; | |
1207 | ||
1208 | if (TREE_TYPE (t2) == void_type_node && | |
1209 | TYPE_ARG_TYPES (t2) == void_list_node) | |
1210 | return true; | |
1211 | ||
1212 | if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2))) | |
1213 | return false; | |
1214 | ||
1215 | for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2); | |
1216 | t1 && t2; | |
1217 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) | |
1218 | if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2))) | |
1219 | return false; | |
1220 | ||
1221 | return true; | |
1222 | } | |
1223 | ||
96d84882 | 1224 | /* Subroutine in comptypes. */ |
e92cc029 | 1225 | |
06d40de8 DG |
1226 | static bool |
1227 | structural_comptypes (tree t1, tree t2, int strict) | |
8d08fdba | 1228 | { |
8d08fdba | 1229 | if (t1 == t2) |
c8a209ca NS |
1230 | return true; |
1231 | ||
f4f206f4 | 1232 | /* Suppress errors caused by previously reported errors. */ |
ae8803a8 | 1233 | if (t1 == error_mark_node || t2 == error_mark_node) |
c8a209ca | 1234 | return false; |
c8094d83 | 1235 | |
50bc768d | 1236 | gcc_assert (TYPE_P (t1) && TYPE_P (t2)); |
c8094d83 | 1237 | |
c8a209ca NS |
1238 | /* TYPENAME_TYPEs should be resolved if the qualifying scope is the |
1239 | current instantiation. */ | |
1240 | if (TREE_CODE (t1) == TYPENAME_TYPE) | |
4195a767 | 1241 | t1 = resolve_typename_type (t1, /*only_current_p=*/true); |
c8094d83 | 1242 | |
c8a209ca | 1243 | if (TREE_CODE (t2) == TYPENAME_TYPE) |
4195a767 | 1244 | t2 = resolve_typename_type (t2, /*only_current_p=*/true); |
8d08fdba | 1245 | |
5566b478 MS |
1246 | if (TYPE_PTRMEMFUNC_P (t1)) |
1247 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); | |
1248 | if (TYPE_PTRMEMFUNC_P (t2)) | |
1249 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); | |
1250 | ||
8d08fdba | 1251 | /* Different classes of types can't be compatible. */ |
8d08fdba | 1252 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
c8a209ca | 1253 | return false; |
8d08fdba | 1254 | |
c353b8e3 MM |
1255 | /* Qualifiers must match. For array types, we will check when we |
1256 | recur on the array element types. */ | |
1257 | if (TREE_CODE (t1) != ARRAY_TYPE | |
a3360e77 JM |
1258 | && cp_type_quals (t1) != cp_type_quals (t2)) |
1259 | return false; | |
1260 | if (TREE_CODE (t1) == FUNCTION_TYPE | |
1261 | && type_memfn_quals (t1) != type_memfn_quals (t2)) | |
c8a209ca | 1262 | return false; |
2eed8e37 BK |
1263 | /* Need to check this before TYPE_MAIN_VARIANT. |
1264 | FIXME function qualifiers should really change the main variant. */ | |
51dc6603 JM |
1265 | if (TREE_CODE (t1) == FUNCTION_TYPE |
1266 | || TREE_CODE (t1) == METHOD_TYPE) | |
1267 | { | |
1268 | if (type_memfn_rqual (t1) != type_memfn_rqual (t2)) | |
1269 | return false; | |
1270 | if (flag_noexcept_type | |
1271 | && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1), | |
1272 | TYPE_RAISES_EXCEPTIONS (t2), | |
1273 | ce_type)) | |
1274 | return false; | |
1275 | } | |
8d08fdba MS |
1276 | |
1277 | /* Allow for two different type nodes which have essentially the same | |
1278 | definition. Note that we already checked for equality of the type | |
38e01259 | 1279 | qualifiers (just above). */ |
8d08fdba | 1280 | |
c353b8e3 MM |
1281 | if (TREE_CODE (t1) != ARRAY_TYPE |
1282 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
c8a209ca | 1283 | return true; |
8d08fdba | 1284 | |
20c202f3 | 1285 | |
7866705a | 1286 | /* Compare the types. Break out if they could be the same. */ |
8d08fdba MS |
1287 | switch (TREE_CODE (t1)) |
1288 | { | |
0451301c DG |
1289 | case VOID_TYPE: |
1290 | case BOOLEAN_TYPE: | |
1291 | /* All void and bool types are the same. */ | |
1292 | break; | |
1293 | ||
1294 | case INTEGER_TYPE: | |
1295 | case FIXED_POINT_TYPE: | |
1296 | case REAL_TYPE: | |
1297 | /* With these nodes, we can't determine type equivalence by | |
1298 | looking at what is stored in the nodes themselves, because | |
1299 | two nodes might have different TYPE_MAIN_VARIANTs but still | |
1300 | represent the same type. For example, wchar_t and int could | |
1301 | have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE, | |
1302 | TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs | |
1303 | and are distinct types. On the other hand, int and the | |
1304 | following typedef | |
1305 | ||
1306 | typedef int INT __attribute((may_alias)); | |
1307 | ||
1308 | have identical properties, different TYPE_MAIN_VARIANTs, but | |
1309 | represent the same type. The canonical type system keeps | |
1310 | track of equivalence in this case, so we fall back on it. */ | |
1311 | return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); | |
1312 | ||
e7e66632 | 1313 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 1314 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
e96ce650 | 1315 | if (!comp_template_parms_position (t1, t2)) |
c8a209ca NS |
1316 | return false; |
1317 | if (!comp_template_parms | |
1318 | (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)), | |
1319 | DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2)))) | |
1320 | return false; | |
a1281f45 | 1321 | if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM) |
7866705a | 1322 | break; |
e7e66632 | 1323 | /* Don't check inheritance. */ |
acd8e2d0 | 1324 | strict = COMPARE_STRICT; |
f4f206f4 | 1325 | /* Fall through. */ |
e7e66632 | 1326 | |
8d08fdba MS |
1327 | case RECORD_TYPE: |
1328 | case UNION_TYPE: | |
7ddedda4 MM |
1329 | if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2) |
1330 | && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2) | |
c8a209ca NS |
1331 | || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM) |
1332 | && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2))) | |
7866705a | 1333 | break; |
c8094d83 | 1334 | |
acd8e2d0 | 1335 | if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2)) |
7866705a | 1336 | break; |
acd8e2d0 | 1337 | else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1)) |
7866705a | 1338 | break; |
c8094d83 | 1339 | |
c8a209ca | 1340 | return false; |
8d08fdba MS |
1341 | |
1342 | case OFFSET_TYPE: | |
96d84882 PB |
1343 | if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2), |
1344 | strict & ~COMPARE_REDECLARATION)) | |
c8a209ca | 1345 | return false; |
7866705a SB |
1346 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1347 | return false; | |
1348 | break; | |
8d08fdba | 1349 | |
8d08fdba | 1350 | case REFERENCE_TYPE: |
8af2fec4 RY |
1351 | if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2)) |
1352 | return false; | |
1353 | /* fall through to checks for pointer types */ | |
81fea426 | 1354 | gcc_fallthrough (); |
8af2fec4 RY |
1355 | |
1356 | case POINTER_TYPE: | |
7866705a | 1357 | if (TYPE_MODE (t1) != TYPE_MODE (t2) |
7866705a SB |
1358 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1359 | return false; | |
1360 | break; | |
8d08fdba | 1361 | |
dffa4176 | 1362 | case METHOD_TYPE: |
8d08fdba | 1363 | case FUNCTION_TYPE: |
acd8e2d0 | 1364 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
c8a209ca | 1365 | return false; |
7866705a SB |
1366 | if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))) |
1367 | return false; | |
1368 | break; | |
8d08fdba MS |
1369 | |
1370 | case ARRAY_TYPE: | |
9bcb9aae | 1371 | /* Target types must match incl. qualifiers. */ |
7866705a SB |
1372 | if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION))) |
1373 | return false; | |
1374 | break; | |
8d08fdba | 1375 | |
51c184be | 1376 | case TEMPLATE_TYPE_PARM: |
6f1abb06 DS |
1377 | /* If T1 and T2 don't have the same relative position in their |
1378 | template parameters set, they can't be equal. */ | |
1379 | if (!comp_template_parms_position (t1, t2)) | |
1380 | return false; | |
79a9afa5 JM |
1381 | /* If T1 and T2 don't represent the same class template deduction, |
1382 | they aren't equal. */ | |
1383 | if (CLASS_PLACEHOLDER_TEMPLATE (t1) | |
1384 | != CLASS_PLACEHOLDER_TEMPLATE (t2)) | |
1385 | return false; | |
4fea442d JM |
1386 | /* Constrained 'auto's are distinct from parms that don't have the same |
1387 | constraints. */ | |
1388 | if (!equivalent_placeholder_constraints (t1, t2)) | |
1389 | return false; | |
7866705a | 1390 | break; |
e76a2646 MS |
1391 | |
1392 | case TYPENAME_TYPE: | |
c8a209ca NS |
1393 | if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1), |
1394 | TYPENAME_TYPE_FULLNAME (t2))) | |
0cbd7506 | 1395 | return false; |
69d28b4f JM |
1396 | /* Qualifiers don't matter on scopes. */ |
1397 | if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1), | |
1398 | TYPE_CONTEXT (t2))) | |
7866705a SB |
1399 | return false; |
1400 | break; | |
7f85441b | 1401 | |
b8c6534b | 1402 | case UNBOUND_CLASS_TEMPLATE: |
c8a209ca | 1403 | if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2))) |
0cbd7506 | 1404 | return false; |
7866705a SB |
1405 | if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))) |
1406 | return false; | |
1407 | break; | |
b8c6534b | 1408 | |
a7a64a77 | 1409 | case COMPLEX_TYPE: |
7866705a SB |
1410 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1411 | return false; | |
1412 | break; | |
a7a64a77 | 1413 | |
cc27e657 | 1414 | case VECTOR_TYPE: |
928686b1 | 1415 | if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2)) |
7866705a SB |
1416 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1417 | return false; | |
cc27e657 PB |
1418 | break; |
1419 | ||
5d80a306 | 1420 | case TYPE_PACK_EXPANSION: |
c67dd256 JM |
1421 | return (same_type_p (PACK_EXPANSION_PATTERN (t1), |
1422 | PACK_EXPANSION_PATTERN (t2)) | |
1423 | && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1), | |
1424 | PACK_EXPANSION_EXTRA_ARGS (t2))); | |
5d80a306 | 1425 | |
3ad6a8e1 DG |
1426 | case DECLTYPE_TYPE: |
1427 | if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1) | |
1428 | != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2) | |
d5f4eddd JM |
1429 | || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) |
1430 | != DECLTYPE_FOR_LAMBDA_CAPTURE (t2)) | |
9b8662c2 JM |
1431 | || (DECLTYPE_FOR_LAMBDA_PROXY (t1) |
1432 | != DECLTYPE_FOR_LAMBDA_PROXY (t2)) | |
3ad6a8e1 DG |
1433 | || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), |
1434 | DECLTYPE_TYPE_EXPR (t2))) | |
1435 | return false; | |
1436 | break; | |
1437 | ||
a0d260fc PC |
1438 | case UNDERLYING_TYPE: |
1439 | return same_type_p (UNDERLYING_TYPE_TYPE (t1), | |
1440 | UNDERLYING_TYPE_TYPE (t2)); | |
1441 | ||
7f85441b | 1442 | default: |
7866705a | 1443 | return false; |
8d08fdba | 1444 | } |
7866705a SB |
1445 | |
1446 | /* If we get here, we know that from a target independent POV the | |
1447 | types are the same. Make sure the target attributes are also | |
1448 | the same. */ | |
ac9a30ae | 1449 | return comp_type_attributes (t1, t2); |
8d08fdba MS |
1450 | } |
1451 | ||
06d40de8 DG |
1452 | /* Return true if T1 and T2 are related as allowed by STRICT. STRICT |
1453 | is a bitwise-or of the COMPARE_* flags. */ | |
1454 | ||
1455 | bool | |
96d84882 | 1456 | comptypes (tree t1, tree t2, int strict) |
06d40de8 DG |
1457 | { |
1458 | if (strict == COMPARE_STRICT) | |
1459 | { | |
06d40de8 DG |
1460 | if (t1 == t2) |
1461 | return true; | |
1462 | ||
1463 | if (t1 == error_mark_node || t2 == error_mark_node) | |
1464 | return false; | |
1465 | ||
1466 | if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2)) | |
1467 | /* At least one of the types requires structural equality, so | |
1468 | perform a deep check. */ | |
1469 | return structural_comptypes (t1, t2, strict); | |
1470 | ||
595278be | 1471 | if (flag_checking && USE_CANONICAL_TYPES) |
06d40de8 | 1472 | { |
7313518b DG |
1473 | bool result = structural_comptypes (t1, t2, strict); |
1474 | ||
06d40de8 | 1475 | if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2)) |
7313518b DG |
1476 | /* The two types are structurally equivalent, but their |
1477 | canonical types were different. This is a failure of the | |
1478 | canonical type propagation code.*/ | |
1479 | internal_error | |
0f2c4a8f | 1480 | ("canonical types differ for identical types %qT and %qT", |
7313518b | 1481 | t1, t2); |
06d40de8 | 1482 | else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)) |
7313518b DG |
1483 | /* Two types are structurally different, but the canonical |
1484 | types are the same. This means we were over-eager in | |
1485 | assigning canonical types. */ | |
1486 | internal_error | |
0f2c4a8f | 1487 | ("same canonical type node for different types %qT and %qT", |
7313518b | 1488 | t1, t2); |
06d40de8 DG |
1489 | |
1490 | return result; | |
1491 | } | |
595278be | 1492 | if (!flag_checking && USE_CANONICAL_TYPES) |
06d40de8 | 1493 | return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); |
7313518b DG |
1494 | else |
1495 | return structural_comptypes (t1, t2, strict); | |
06d40de8 DG |
1496 | } |
1497 | else if (strict == COMPARE_STRUCTURAL) | |
1498 | return structural_comptypes (t1, t2, COMPARE_STRICT); | |
1499 | else | |
1500 | return structural_comptypes (t1, t2, strict); | |
1501 | } | |
1502 | ||
10746f37 JM |
1503 | /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring |
1504 | top-level qualifiers. */ | |
1505 | ||
1506 | bool | |
1507 | same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2) | |
1508 | { | |
1509 | if (type1 == error_mark_node || type2 == error_mark_node) | |
1510 | return false; | |
1511 | ||
452811eb JM |
1512 | type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED); |
1513 | type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED); | |
1514 | return same_type_p (type1, type2); | |
10746f37 JM |
1515 | } |
1516 | ||
91063b51 MM |
1517 | /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */ |
1518 | ||
acd8e2d0 | 1519 | bool |
58f9752a | 1520 | at_least_as_qualified_p (const_tree type1, const_tree type2) |
91063b51 | 1521 | { |
acd8e2d0 NS |
1522 | int q1 = cp_type_quals (type1); |
1523 | int q2 = cp_type_quals (type2); | |
c8094d83 | 1524 | |
91063b51 | 1525 | /* All qualifiers for TYPE2 must also appear in TYPE1. */ |
acd8e2d0 | 1526 | return (q1 & q2) == q2; |
91063b51 MM |
1527 | } |
1528 | ||
ceab47eb MM |
1529 | /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is |
1530 | more cv-qualified that TYPE1, and 0 otherwise. */ | |
1531 | ||
1532 | int | |
d02f620d | 1533 | comp_cv_qualification (int q1, int q2) |
ceab47eb | 1534 | { |
acd8e2d0 | 1535 | if (q1 == q2) |
ceab47eb MM |
1536 | return 0; |
1537 | ||
acd8e2d0 | 1538 | if ((q1 & q2) == q2) |
ceab47eb | 1539 | return 1; |
acd8e2d0 | 1540 | else if ((q1 & q2) == q1) |
ceab47eb MM |
1541 | return -1; |
1542 | ||
1543 | return 0; | |
1544 | } | |
1545 | ||
d02f620d JM |
1546 | int |
1547 | comp_cv_qualification (const_tree type1, const_tree type2) | |
1548 | { | |
1549 | int q1 = cp_type_quals (type1); | |
1550 | int q2 = cp_type_quals (type2); | |
1551 | return comp_cv_qualification (q1, q2); | |
1552 | } | |
1553 | ||
ceab47eb MM |
1554 | /* Returns 1 if the cv-qualification signature of TYPE1 is a proper |
1555 | subset of the cv-qualification signature of TYPE2, and the types | |
1556 | are similar. Returns -1 if the other way 'round, and 0 otherwise. */ | |
1557 | ||
1558 | int | |
acd8e2d0 | 1559 | comp_cv_qual_signature (tree type1, tree type2) |
ceab47eb MM |
1560 | { |
1561 | if (comp_ptr_ttypes_real (type2, type1, -1)) | |
1562 | return 1; | |
1563 | else if (comp_ptr_ttypes_real (type1, type2, -1)) | |
1564 | return -1; | |
1565 | else | |
1566 | return 0; | |
1567 | } | |
8d08fdba | 1568 | \f |
96d84882 | 1569 | /* Subroutines of `comptypes'. */ |
8d08fdba | 1570 | |
acd8e2d0 | 1571 | /* Return true if two parameter type lists PARMS1 and PARMS2 are |
03017874 MM |
1572 | equivalent in the sense that functions with those parameter types |
1573 | can have equivalent types. The two lists must be equivalent, | |
acd8e2d0 | 1574 | element by element. */ |
8d08fdba | 1575 | |
acd8e2d0 | 1576 | bool |
58f9752a | 1577 | compparms (const_tree parms1, const_tree parms2) |
8d08fdba | 1578 | { |
58f9752a | 1579 | const_tree t1, t2; |
8d08fdba MS |
1580 | |
1581 | /* An unspecified parmlist matches any specified parmlist | |
1582 | whose argument types don't need default promotions. */ | |
1583 | ||
acd8e2d0 NS |
1584 | for (t1 = parms1, t2 = parms2; |
1585 | t1 || t2; | |
1586 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) | |
8d08fdba | 1587 | { |
8d08fdba | 1588 | /* If one parmlist is shorter than the other, |
4daa9c14 | 1589 | they fail to match. */ |
acd8e2d0 NS |
1590 | if (!t1 || !t2) |
1591 | return false; | |
ae8803a8 | 1592 | if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) |
acd8e2d0 | 1593 | return false; |
8d08fdba | 1594 | } |
acd8e2d0 | 1595 | return true; |
8d08fdba MS |
1596 | } |
1597 | ||
8d08fdba | 1598 | \f |
7a18b933 | 1599 | /* Process a sizeof or alignof expression where the operand is a |
aa2b1055 JM |
1600 | type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment) |
1601 | or GNU (preferred alignment) semantics; it is ignored if op is | |
1602 | SIZEOF_EXPR. */ | |
7a18b933 | 1603 | |
8d08fdba | 1604 | tree |
aa2b1055 JM |
1605 | cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool std_alignof, |
1606 | bool complain) | |
8d08fdba | 1607 | { |
50bc768d | 1608 | gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR); |
7a18b933 NS |
1609 | if (type == error_mark_node) |
1610 | return error_mark_node; | |
c8094d83 | 1611 | |
ee76b931 | 1612 | type = non_reference (type); |
3c17e16e | 1613 | if (TREE_CODE (type) == METHOD_TYPE) |
8d08fdba | 1614 | { |
fcf73884 | 1615 | if (complain) |
b005486b JW |
1616 | { |
1617 | pedwarn (input_location, OPT_Wpointer_arith, | |
1618 | "invalid application of %qs to a member function", | |
1619 | OVL_OP_INFO (false, op)->name); | |
1620 | return size_one_node; | |
1621 | } | |
aa0a659c PC |
1622 | else |
1623 | return error_mark_node; | |
8d08fdba | 1624 | } |
8d08fdba | 1625 | |
b005486b | 1626 | bool dependent_p = dependent_type_p (type); |
b4c74ba2 MM |
1627 | if (!dependent_p) |
1628 | complete_type (type); | |
1629 | if (dependent_p | |
3c17e16e MM |
1630 | /* VLA types will have a non-constant size. In the body of an |
1631 | uninstantiated template, we don't need to try to compute the | |
1632 | value, because the sizeof expression is not an integral | |
1633 | constant expression in that case. And, if we do try to | |
1634 | compute the value, we'll likely end up with SAVE_EXPRs, which | |
1635 | the template substitution machinery does not expect to see. */ | |
b4c74ba2 MM |
1636 | || (processing_template_decl |
1637 | && COMPLETE_TYPE_P (type) | |
1638 | && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)) | |
3c17e16e | 1639 | { |
b005486b | 1640 | tree value = build_min (op, size_type_node, type); |
3c17e16e | 1641 | TREE_READONLY (value) = 1; |
aa2b1055 JM |
1642 | if (op == ALIGNOF_EXPR && std_alignof) |
1643 | ALIGNOF_EXPR_STD_P (value) = true; | |
3c17e16e MM |
1644 | return value; |
1645 | } | |
1646 | ||
c2255bc4 | 1647 | return c_sizeof_or_alignof_type (input_location, complete_type (type), |
aa2b1055 | 1648 | op == SIZEOF_EXPR, std_alignof, |
3c17e16e | 1649 | complain); |
8d08fdba MS |
1650 | } |
1651 | ||
5ade1ed2 DG |
1652 | /* Return the size of the type, without producing any warnings for |
1653 | types whose size cannot be taken. This routine should be used only | |
1654 | in some other routine that has already produced a diagnostic about | |
1655 | using the size of such a type. */ | |
1656 | tree | |
1657 | cxx_sizeof_nowarn (tree type) | |
1658 | { | |
1659 | if (TREE_CODE (type) == FUNCTION_TYPE | |
50e10fa8 | 1660 | || VOID_TYPE_P (type) |
5ade1ed2 DG |
1661 | || TREE_CODE (type) == ERROR_MARK) |
1662 | return size_one_node; | |
1663 | else if (!COMPLETE_TYPE_P (type)) | |
1664 | return size_zero_node; | |
1665 | else | |
aa2b1055 | 1666 | return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false, false); |
5ade1ed2 DG |
1667 | } |
1668 | ||
229970c4 | 1669 | /* Process a sizeof expression where the operand is an expression. */ |
7a18b933 | 1670 | |
229970c4 | 1671 | static tree |
5ade1ed2 | 1672 | cxx_sizeof_expr (tree e, tsubst_flags_t complain) |
5566b478 | 1673 | { |
7a18b933 NS |
1674 | if (e == error_mark_node) |
1675 | return error_mark_node; | |
c8094d83 | 1676 | |
5156628f | 1677 | if (processing_template_decl) |
7a18b933 | 1678 | { |
229970c4 | 1679 | e = build_min (SIZEOF_EXPR, size_type_node, e); |
7a18b933 NS |
1680 | TREE_SIDE_EFFECTS (e) = 0; |
1681 | TREE_READONLY (e) = 1; | |
c8094d83 | 1682 | |
7a18b933 NS |
1683 | return e; |
1684 | } | |
c8094d83 | 1685 | |
dfd7fdca DM |
1686 | STRIP_ANY_LOCATION_WRAPPER (e); |
1687 | ||
48326487 JM |
1688 | /* To get the size of a static data member declared as an array of |
1689 | unknown bound, we need to instantiate it. */ | |
5a6ccc94 | 1690 | if (VAR_P (e) |
48326487 JM |
1691 | && VAR_HAD_UNKNOWN_BOUND (e) |
1692 | && DECL_TEMPLATE_INSTANTIATION (e)) | |
1693 | instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false); | |
1694 | ||
773ec47f MP |
1695 | if (TREE_CODE (e) == PARM_DECL |
1696 | && DECL_ARRAY_PARAMETER_P (e) | |
1697 | && (complain & tf_warning)) | |
1698 | { | |
097f82ec | 1699 | auto_diagnostic_group d; |
773ec47f MP |
1700 | if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function " |
1701 | "parameter %qE will return size of %qT", e, TREE_TYPE (e))) | |
1702 | inform (DECL_SOURCE_LOCATION (e), "declared here"); | |
1703 | } | |
1704 | ||
03a904b5 JJ |
1705 | e = mark_type_use (e); |
1706 | ||
47e5d7cc | 1707 | if (bitfield_p (e)) |
5566b478 | 1708 | { |
5ade1ed2 DG |
1709 | if (complain & tf_error) |
1710 | error ("invalid application of %<sizeof%> to a bit-field"); | |
1711 | else | |
1712 | return error_mark_node; | |
7a18b933 NS |
1713 | e = char_type_node; |
1714 | } | |
1715 | else if (is_overloaded_fn (e)) | |
1716 | { | |
5ade1ed2 | 1717 | if (complain & tf_error) |
cbe5f3b3 | 1718 | permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of " |
37ec60ed | 1719 | "function type"); |
5ade1ed2 DG |
1720 | else |
1721 | return error_mark_node; | |
7a18b933 | 1722 | e = char_type_node; |
5566b478 | 1723 | } |
05e0b2f4 JM |
1724 | else if (type_unknown_p (e)) |
1725 | { | |
5ade1ed2 DG |
1726 | if (complain & tf_error) |
1727 | cxx_incomplete_type_error (e, TREE_TYPE (e)); | |
1728 | else | |
1729 | return error_mark_node; | |
7a18b933 | 1730 | e = char_type_node; |
05e0b2f4 | 1731 | } |
7a18b933 NS |
1732 | else |
1733 | e = TREE_TYPE (e); | |
c8094d83 | 1734 | |
aa2b1055 | 1735 | return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, false, complain & tf_error); |
5566b478 | 1736 | } |
c8094d83 | 1737 | |
229970c4 | 1738 | /* Implement the __alignof keyword: Return the minimum required |
69d1a403 | 1739 | alignment of E, measured in bytes. For VAR_DECL's and |
229970c4 JM |
1740 | FIELD_DECL's return DECL_ALIGN (which can be set from an |
1741 | "aligned" __attribute__ specification). */ | |
1742 | ||
1743 | static tree | |
5ade1ed2 | 1744 | cxx_alignof_expr (tree e, tsubst_flags_t complain) |
229970c4 JM |
1745 | { |
1746 | tree t; | |
3db45ab5 | 1747 | |
229970c4 JM |
1748 | if (e == error_mark_node) |
1749 | return error_mark_node; | |
1750 | ||
1751 | if (processing_template_decl) | |
1752 | { | |
1753 | e = build_min (ALIGNOF_EXPR, size_type_node, e); | |
1754 | TREE_SIDE_EFFECTS (e) = 0; | |
1755 | TREE_READONLY (e) = 1; | |
1756 | ||
1757 | return e; | |
1758 | } | |
1759 | ||
dfd7fdca DM |
1760 | STRIP_ANY_LOCATION_WRAPPER (e); |
1761 | ||
03a904b5 JJ |
1762 | e = mark_type_use (e); |
1763 | ||
5a6ccc94 | 1764 | if (VAR_P (e)) |
229970c4 | 1765 | t = size_int (DECL_ALIGN_UNIT (e)); |
47e5d7cc | 1766 | else if (bitfield_p (e)) |
229970c4 | 1767 | { |
5ade1ed2 DG |
1768 | if (complain & tf_error) |
1769 | error ("invalid application of %<__alignof%> to a bit-field"); | |
1770 | else | |
1771 | return error_mark_node; | |
229970c4 JM |
1772 | t = size_one_node; |
1773 | } | |
1774 | else if (TREE_CODE (e) == COMPONENT_REF | |
1775 | && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL) | |
1776 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1))); | |
1777 | else if (is_overloaded_fn (e)) | |
1778 | { | |
5ade1ed2 | 1779 | if (complain & tf_error) |
cbe5f3b3 | 1780 | permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of " |
37ec60ed | 1781 | "function type"); |
5ade1ed2 DG |
1782 | else |
1783 | return error_mark_node; | |
837edd5f GK |
1784 | if (TREE_CODE (e) == FUNCTION_DECL) |
1785 | t = size_int (DECL_ALIGN_UNIT (e)); | |
1786 | else | |
1787 | t = size_one_node; | |
229970c4 JM |
1788 | } |
1789 | else if (type_unknown_p (e)) | |
1790 | { | |
5ade1ed2 DG |
1791 | if (complain & tf_error) |
1792 | cxx_incomplete_type_error (e, TREE_TYPE (e)); | |
1793 | else | |
1794 | return error_mark_node; | |
229970c4 JM |
1795 | t = size_one_node; |
1796 | } | |
1797 | else | |
aa2b1055 | 1798 | return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, false, |
5ade1ed2 | 1799 | complain & tf_error); |
229970c4 JM |
1800 | |
1801 | return fold_convert (size_type_node, t); | |
1802 | } | |
1803 | ||
69d1a403 JM |
1804 | /* Process a sizeof or alignof expression E with code OP where the operand |
1805 | is an expression. */ | |
229970c4 JM |
1806 | |
1807 | tree | |
5ade1ed2 | 1808 | cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain) |
229970c4 JM |
1809 | { |
1810 | if (op == SIZEOF_EXPR) | |
5ade1ed2 | 1811 | return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none); |
229970c4 | 1812 | else |
5ade1ed2 | 1813 | return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none); |
229970c4 | 1814 | } |
e28d52cf DS |
1815 | |
1816 | /* Build a representation of an expression 'alignas(E).' Return the | |
1817 | folded integer value of E if it is an integral constant expression | |
1818 | that resolves to a valid alignment. If E depends on a template | |
1819 | parameter, return a syntactic representation tree of kind | |
1820 | ALIGNOF_EXPR. Otherwise, return an error_mark_node if the | |
1821 | expression is ill formed, or NULL_TREE if E is NULL_TREE. */ | |
1822 | ||
1823 | tree | |
1824 | cxx_alignas_expr (tree e) | |
1825 | { | |
1826 | if (e == NULL_TREE || e == error_mark_node | |
1827 | || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e))) | |
1828 | return e; | |
1829 | ||
1830 | if (TYPE_P (e)) | |
1831 | /* [dcl.align]/3: | |
1832 | ||
1833 | When the alignment-specifier is of the form | |
1834 | alignas(type-id ), it shall have the same effect as | |
17365662 | 1835 | alignas(alignof(type-id )). */ |
e28d52cf | 1836 | |
aa2b1055 | 1837 | return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, true, false); |
e28d52cf | 1838 | |
e28d52cf DS |
1839 | /* If we reach this point, it means the alignas expression if of |
1840 | the form "alignas(assignment-expression)", so we should follow | |
1841 | what is stated by [dcl.align]/2. */ | |
1842 | ||
17365662 JM |
1843 | if (value_dependent_expression_p (e)) |
1844 | /* Leave value-dependent expression alone for now. */ | |
1845 | return e; | |
1846 | ||
234bef96 | 1847 | e = instantiate_non_dependent_expr (e); |
e28d52cf DS |
1848 | e = mark_rvalue_use (e); |
1849 | ||
1850 | /* [dcl.align]/2 says: | |
1851 | ||
1852 | the assignment-expression shall be an integral constant | |
1853 | expression. */ | |
f46e2bc9 PC |
1854 | |
1855 | if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e))) | |
1856 | { | |
1857 | error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e)); | |
1858 | return error_mark_node; | |
1859 | } | |
e28d52cf | 1860 | |
17365662 | 1861 | return cxx_constant_value (e); |
e28d52cf DS |
1862 | } |
1863 | ||
8d08fdba | 1864 | \f |
c8b2e872 MM |
1865 | /* EXPR is being used in a context that is not a function call. |
1866 | Enforce: | |
1867 | ||
c8094d83 | 1868 | [expr.ref] |
c8b2e872 MM |
1869 | |
1870 | The expression can be used only as the left-hand operand of a | |
c8094d83 | 1871 | member function call. |
c8b2e872 MM |
1872 | |
1873 | [expr.mptr.operator] | |
1874 | ||
1875 | If the result of .* or ->* is a function, then that result can be | |
c8094d83 | 1876 | used only as the operand for the function call operator (). |
c8b2e872 MM |
1877 | |
1878 | by issuing an error message if appropriate. Returns true iff EXPR | |
1879 | violates these rules. */ | |
1880 | ||
1881 | bool | |
d3ea4c06 | 1882 | invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain) |
c8b2e872 | 1883 | { |
6c74ff23 JM |
1884 | if (expr == NULL_TREE) |
1885 | return false; | |
1886 | /* Don't enforce this in MS mode. */ | |
1887 | if (flag_ms_extensions) | |
1888 | return false; | |
1889 | if (is_overloaded_fn (expr) && !really_overloaded_fn (expr)) | |
1890 | expr = get_first_fn (expr); | |
1891 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr)) | |
c8b2e872 | 1892 | { |
5ade1ed2 | 1893 | if (complain & tf_error) |
d3ea4c06 MLI |
1894 | { |
1895 | if (DECL_P (expr)) | |
1896 | { | |
1897 | error_at (loc, "invalid use of non-static member function %qD", | |
1898 | expr); | |
1899 | inform (DECL_SOURCE_LOCATION (expr), "declared here"); | |
1900 | } | |
1901 | else | |
1902 | error_at (loc, "invalid use of non-static member function of " | |
1903 | "type %qT", TREE_TYPE (expr)); | |
1904 | } | |
c8b2e872 MM |
1905 | return true; |
1906 | } | |
1907 | return false; | |
1908 | } | |
1909 | ||
38a4afee MM |
1910 | /* If EXP is a reference to a bitfield, and the type of EXP does not |
1911 | match the declared type of the bitfield, return the declared type | |
1912 | of the bitfield. Otherwise, return NULL_TREE. */ | |
1913 | ||
1914 | tree | |
58f9752a | 1915 | is_bitfield_expr_with_lowered_type (const_tree exp) |
38a4afee | 1916 | { |
f9aa54d3 | 1917 | switch (TREE_CODE (exp)) |
38a4afee | 1918 | { |
f9aa54d3 | 1919 | case COND_EXPR: |
42924ed7 JJ |
1920 | if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1) |
1921 | ? TREE_OPERAND (exp, 1) | |
1922 | : TREE_OPERAND (exp, 0))) | |
38a4afee MM |
1923 | return NULL_TREE; |
1924 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2)); | |
f9aa54d3 MM |
1925 | |
1926 | case COMPOUND_EXPR: | |
1927 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)); | |
1928 | ||
1929 | case MODIFY_EXPR: | |
1930 | case SAVE_EXPR: | |
1931 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); | |
1932 | ||
1933 | case COMPONENT_REF: | |
1934 | { | |
1935 | tree field; | |
1936 | ||
1937 | field = TREE_OPERAND (exp, 1); | |
e76d7cc7 | 1938 | if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field)) |
f9aa54d3 MM |
1939 | return NULL_TREE; |
1940 | if (same_type_ignoring_top_level_qualifiers_p | |
1941 | (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field))) | |
1942 | return NULL_TREE; | |
1943 | return DECL_BIT_FIELD_TYPE (field); | |
1944 | } | |
1945 | ||
70f40fea JJ |
1946 | case VAR_DECL: |
1947 | if (DECL_HAS_VALUE_EXPR_P (exp)) | |
1948 | return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR | |
1949 | (CONST_CAST_TREE (exp))); | |
1950 | return NULL_TREE; | |
1951 | ||
dfd7fdca DM |
1952 | case VIEW_CONVERT_EXPR: |
1953 | if (location_wrapper_p (exp)) | |
1954 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); | |
1955 | else | |
1956 | return NULL_TREE; | |
1957 | ||
5f9e56b3 | 1958 | CASE_CONVERT: |
be2b5483 RG |
1959 | if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0))) |
1960 | == TYPE_MAIN_VARIANT (TREE_TYPE (exp))) | |
1961 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); | |
1962 | /* Fallthrough. */ | |
1963 | ||
f9aa54d3 MM |
1964 | default: |
1965 | return NULL_TREE; | |
38a4afee | 1966 | } |
f9aa54d3 MM |
1967 | } |
1968 | ||
1969 | /* Like is_bitfield_with_lowered_type, except that if EXP is not a | |
1970 | bitfield with a lowered type, the type of EXP is returned, rather | |
1971 | than NULL_TREE. */ | |
1972 | ||
1973 | tree | |
58f9752a | 1974 | unlowered_expr_type (const_tree exp) |
f9aa54d3 MM |
1975 | { |
1976 | tree type; | |
c1e41527 | 1977 | tree etype = TREE_TYPE (exp); |
f9aa54d3 MM |
1978 | |
1979 | type = is_bitfield_expr_with_lowered_type (exp); | |
c1e41527 JM |
1980 | if (type) |
1981 | type = cp_build_qualified_type (type, cp_type_quals (etype)); | |
1982 | else | |
1983 | type = etype; | |
f9aa54d3 MM |
1984 | |
1985 | return type; | |
38a4afee MM |
1986 | } |
1987 | ||
0a72704b MM |
1988 | /* Perform the conversions in [expr] that apply when an lvalue appears |
1989 | in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and | |
56233bd6 JM |
1990 | function-to-pointer conversions. In addition, bitfield references are |
1991 | converted to their declared types. Note that this function does not perform | |
1992 | the lvalue-to-rvalue conversion for class types. If you need that conversion | |
1993 | for class types, then you probably need to use force_rvalue. | |
8d08fdba | 1994 | |
41990f96 MM |
1995 | Although the returned value is being used as an rvalue, this |
1996 | function does not wrap the returned expression in a | |
1997 | NON_LVALUE_EXPR; the caller is expected to be mindful of the fact | |
1998 | that the return value is no longer an lvalue. */ | |
8d08fdba MS |
1999 | |
2000 | tree | |
1807ffc1 MS |
2001 | decay_conversion (tree exp, |
2002 | tsubst_flags_t complain, | |
2003 | bool reject_builtin /* = true */) | |
8d08fdba | 2004 | { |
926ce8bd KH |
2005 | tree type; |
2006 | enum tree_code code; | |
6bdfada4 | 2007 | location_t loc = cp_expr_loc_or_loc (exp, input_location); |
8d08fdba | 2008 | |
a359be75 | 2009 | type = TREE_TYPE (exp); |
07c88314 MM |
2010 | if (type == error_mark_node) |
2011 | return error_mark_node; | |
2012 | ||
b0f42222 | 2013 | exp = resolve_nondeduced_context_or_error (exp, complain); |
8d08fdba | 2014 | |
9bdae013 DS |
2015 | code = TREE_CODE (type); |
2016 | ||
69eb4fde JM |
2017 | if (error_operand_p (exp)) |
2018 | return error_mark_node; | |
c8094d83 | 2019 | |
ec62cbe1 | 2020 | if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp)) |
73974561 PC |
2021 | { |
2022 | mark_rvalue_use (exp, loc, reject_builtin); | |
2023 | return nullptr_node; | |
2024 | } | |
0c32fb95 | 2025 | |
8d08fdba MS |
2026 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
2027 | Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */ | |
8d08fdba MS |
2028 | if (code == VOID_TYPE) |
2029 | { | |
89fcabaf | 2030 | if (complain & tf_error) |
5a3c9cf2 | 2031 | error_at (loc, "void value not ignored as it ought to be"); |
8d08fdba MS |
2032 | return error_mark_node; |
2033 | } | |
d3ea4c06 | 2034 | if (invalid_nonstatic_memfn_p (loc, exp, complain)) |
c8b2e872 | 2035 | return error_mark_node; |
e6e174e5 | 2036 | if (code == FUNCTION_TYPE || is_overloaded_fn (exp)) |
56233bd6 JM |
2037 | { |
2038 | exp = mark_lvalue_use (exp); | |
2039 | if (reject_builtin && reject_gcc_builtin (exp, loc)) | |
2040 | return error_mark_node; | |
2041 | return cp_build_addr_expr (exp, complain); | |
2042 | } | |
8d08fdba MS |
2043 | if (code == ARRAY_TYPE) |
2044 | { | |
926ce8bd | 2045 | tree adr; |
8d08fdba | 2046 | tree ptrtype; |
8d08fdba | 2047 | |
56233bd6 JM |
2048 | exp = mark_lvalue_use (exp); |
2049 | ||
591cb3cf | 2050 | if (INDIRECT_REF_P (exp)) |
c8094d83 | 2051 | return build_nop (build_pointer_type (TREE_TYPE (type)), |
7b6d72fc | 2052 | TREE_OPERAND (exp, 0)); |
8d08fdba MS |
2053 | |
2054 | if (TREE_CODE (exp) == COMPOUND_EXPR) | |
2055 | { | |
89fcabaf PC |
2056 | tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain); |
2057 | if (op1 == error_mark_node) | |
2058 | return error_mark_node; | |
f293ce4b RS |
2059 | return build2 (COMPOUND_EXPR, TREE_TYPE (op1), |
2060 | TREE_OPERAND (exp, 0), op1); | |
8d08fdba MS |
2061 | } |
2062 | ||
bb19d4af | 2063 | if (!obvalue_p (exp) |
8d08fdba MS |
2064 | && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) |
2065 | { | |
89fcabaf | 2066 | if (complain & tf_error) |
5a3c9cf2 | 2067 | error_at (loc, "invalid use of non-lvalue array"); |
8d08fdba MS |
2068 | return error_mark_node; |
2069 | } | |
2070 | ||
1f37c583 JM |
2071 | /* Don't let an array compound literal decay to a pointer. It can |
2072 | still be used to initialize an array or bind to a reference. */ | |
2073 | if (TREE_CODE (exp) == TARGET_EXPR) | |
2074 | { | |
2075 | if (complain & tf_error) | |
2076 | error_at (loc, "taking address of temporary array"); | |
2077 | return error_mark_node; | |
2078 | } | |
2079 | ||
01240200 | 2080 | ptrtype = build_pointer_type (TREE_TYPE (type)); |
8d08fdba | 2081 | |
5a6ccc94 | 2082 | if (VAR_P (exp)) |
8d08fdba | 2083 | { |
dffd7eb6 | 2084 | if (!cxx_mark_addressable (exp)) |
8d08fdba | 2085 | return error_mark_node; |
0e8c9b28 | 2086 | adr = build_nop (ptrtype, build_address (exp)); |
8d08fdba MS |
2087 | return adr; |
2088 | } | |
2089 | /* This way is better for a COMPONENT_REF since it can | |
2090 | simplify the offset for a component. */ | |
89fcabaf | 2091 | adr = cp_build_addr_expr (exp, complain); |
4b978f96 | 2092 | return cp_convert (ptrtype, adr, complain); |
8d08fdba | 2093 | } |
3db45ab5 | 2094 | |
56233bd6 JM |
2095 | /* Otherwise, it's the lvalue-to-rvalue conversion. */ |
2096 | exp = mark_rvalue_use (exp, loc, reject_builtin); | |
2097 | ||
41990f96 MM |
2098 | /* If a bitfield is used in a context where integral promotion |
2099 | applies, then the caller is expected to have used | |
2100 | default_conversion. That function promotes bitfields correctly | |
2101 | before calling this function. At this point, if we have a | |
2102 | bitfield referenced, we may assume that is not subject to | |
2103 | promotion, and that, therefore, the type of the resulting rvalue | |
2104 | is the declared type of the bitfield. */ | |
2105 | exp = convert_bitfield_to_declared_type (exp); | |
a9aedbc2 | 2106 | |
41990f96 MM |
2107 | /* We do not call rvalue() here because we do not want to wrap EXP |
2108 | in a NON_LVALUE_EXPR. */ | |
2109 | ||
2110 | /* [basic.lval] | |
2111 | ||
2112 | Non-class rvalues always have cv-unqualified types. */ | |
2113 | type = TREE_TYPE (exp); | |
36c37128 | 2114 | if (!CLASS_TYPE_P (type) && cv_qualified_p (type)) |
164247b0 | 2115 | exp = build_nop (cv_unqualified (type), exp); |
ba9c33e8 | 2116 | |
56233bd6 JM |
2117 | if (!complete_type_or_maybe_complain (type, exp, complain)) |
2118 | return error_mark_node; | |
2119 | ||
a9aedbc2 MS |
2120 | return exp; |
2121 | } | |
2122 | ||
39a13be5 | 2123 | /* Perform preparatory conversions, as part of the "usual arithmetic |
41990f96 MM |
2124 | conversions". In particular, as per [expr]: |
2125 | ||
2126 | Whenever an lvalue expression appears as an operand of an | |
2127 | operator that expects the rvalue for that operand, the | |
2128 | lvalue-to-rvalue, array-to-pointer, or function-to-pointer | |
2129 | standard conversions are applied to convert the expression to an | |
2130 | rvalue. | |
2131 | ||
2132 | In addition, we perform integral promotions here, as those are | |
2133 | applied to both operands to a binary operator before determining | |
2134 | what additional conversions should apply. */ | |
2135 | ||
89fcabaf PC |
2136 | static tree |
2137 | cp_default_conversion (tree exp, tsubst_flags_t complain) | |
a9aedbc2 | 2138 | { |
40449a90 SL |
2139 | /* Check for target-specific promotions. */ |
2140 | tree promoted_type = targetm.promoted_type (TREE_TYPE (exp)); | |
2141 | if (promoted_type) | |
4b978f96 | 2142 | exp = cp_convert (promoted_type, exp, complain); |
41990f96 MM |
2143 | /* Perform the integral promotions first so that bitfield |
2144 | expressions (which may promote to "int", even if the bitfield is | |
3db45ab5 | 2145 | declared "unsigned") are promoted correctly. */ |
40449a90 | 2146 | else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp))) |
4b978f96 | 2147 | exp = cp_perform_integral_promotions (exp, complain); |
41990f96 | 2148 | /* Perform the other conversions. */ |
89fcabaf | 2149 | exp = decay_conversion (exp, complain); |
a9aedbc2 | 2150 | |
8d08fdba MS |
2151 | return exp; |
2152 | } | |
6b5fbb55 | 2153 | |
89fcabaf PC |
2154 | /* C version. */ |
2155 | ||
2156 | tree | |
2157 | default_conversion (tree exp) | |
2158 | { | |
2159 | return cp_default_conversion (exp, tf_warning_or_error); | |
2160 | } | |
2161 | ||
0a72704b MM |
2162 | /* EXPR is an expression with an integral or enumeration type. |
2163 | Perform the integral promotions in [conv.prom], and return the | |
2164 | converted value. */ | |
2165 | ||
2166 | tree | |
4b978f96 | 2167 | cp_perform_integral_promotions (tree expr, tsubst_flags_t complain) |
0a72704b MM |
2168 | { |
2169 | tree type; | |
2170 | tree promoted_type; | |
2171 | ||
03a904b5 | 2172 | expr = mark_rvalue_use (expr); |
743ee9d5 MP |
2173 | if (error_operand_p (expr)) |
2174 | return error_mark_node; | |
03a904b5 | 2175 | |
efe1ad46 MM |
2176 | /* [conv.prom] |
2177 | ||
2178 | If the bitfield has an enumerated type, it is treated as any | |
2179 | other value of that type for promotion purposes. */ | |
2180 | type = is_bitfield_expr_with_lowered_type (expr); | |
2181 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) | |
2182 | type = TREE_TYPE (expr); | |
50bc768d | 2183 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); |
3c395ecf JM |
2184 | /* Scoped enums don't promote. */ |
2185 | if (SCOPED_ENUM_P (type)) | |
2186 | return expr; | |
0a72704b MM |
2187 | promoted_type = type_promotes_to (type); |
2188 | if (type != promoted_type) | |
4b978f96 | 2189 | expr = cp_convert (promoted_type, expr, complain); |
0a72704b MM |
2190 | return expr; |
2191 | } | |
2192 | ||
4b978f96 PC |
2193 | /* C version. */ |
2194 | ||
2195 | tree | |
2196 | perform_integral_promotions (tree expr) | |
2197 | { | |
2198 | return cp_perform_integral_promotions (expr, tf_warning_or_error); | |
2199 | } | |
2200 | ||
d9cf7c82 JM |
2201 | /* Returns nonzero iff exp is a STRING_CST or the result of applying |
2202 | decay_conversion to one. */ | |
2203 | ||
2204 | int | |
58f9752a | 2205 | string_conv_p (const_tree totype, const_tree exp, int warn) |
d9cf7c82 JM |
2206 | { |
2207 | tree t; | |
2208 | ||
50e10fa8 | 2209 | if (!TYPE_PTR_P (totype)) |
d9cf7c82 JM |
2210 | return 0; |
2211 | ||
2212 | t = TREE_TYPE (totype); | |
3bfdc719 | 2213 | if (!same_type_p (t, char_type_node) |
2d91f79d | 2214 | && !same_type_p (t, char8_type_node) |
b6baa67d KVH |
2215 | && !same_type_p (t, char16_type_node) |
2216 | && !same_type_p (t, char32_type_node) | |
3bfdc719 | 2217 | && !same_type_p (t, wchar_type_node)) |
d9cf7c82 JM |
2218 | return 0; |
2219 | ||
505ce5ab DM |
2220 | location_t loc = EXPR_LOC_OR_LOC (exp, input_location); |
2221 | ||
9a004410 DM |
2222 | STRIP_ANY_LOCATION_WRAPPER (exp); |
2223 | ||
848b92e1 | 2224 | if (TREE_CODE (exp) == STRING_CST) |
d9cf7c82 | 2225 | { |
b6baa67d | 2226 | /* Make sure that we don't try to convert between char and wide chars. */ |
6e176bd6 | 2227 | if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t)) |
848b92e1 JM |
2228 | return 0; |
2229 | } | |
2230 | else | |
2231 | { | |
2232 | /* Is this a string constant which has decayed to 'const char *'? */ | |
a3360e77 | 2233 | t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST)); |
3bfdc719 | 2234 | if (!same_type_p (TREE_TYPE (exp), t)) |
d9cf7c82 JM |
2235 | return 0; |
2236 | STRIP_NOPS (exp); | |
2237 | if (TREE_CODE (exp) != ADDR_EXPR | |
2238 | || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST) | |
2239 | return 0; | |
2240 | } | |
b323323f | 2241 | if (warn) |
aaf176ca VV |
2242 | { |
2243 | if (cxx_dialect >= cxx11) | |
505ce5ab | 2244 | pedwarn (loc, OPT_Wwrite_strings, |
aaf176ca VV |
2245 | "ISO C++ forbids converting a string constant to %qT", |
2246 | totype); | |
2247 | else | |
505ce5ab DM |
2248 | warning_at (loc, OPT_Wwrite_strings, |
2249 | "deprecated conversion from string constant to %qT", | |
2250 | totype); | |
aaf176ca | 2251 | } |
d9cf7c82 JM |
2252 | |
2253 | return 1; | |
2254 | } | |
8d08fdba | 2255 | |
e9a25f70 JL |
2256 | /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we |
2257 | can, for example, use as an lvalue. This code used to be in | |
2258 | unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c' | |
2259 | expressions, where we're dealing with aggregates. But now it's again only | |
2260 | called from unary_complex_lvalue. The case (in particular) that led to | |
2261 | this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd | |
2262 | get it there. */ | |
e92cc029 | 2263 | |
8d08fdba | 2264 | static tree |
5ade1ed2 DG |
2265 | rationalize_conditional_expr (enum tree_code code, tree t, |
2266 | tsubst_flags_t complain) | |
8d08fdba | 2267 | { |
6bdfada4 | 2268 | location_t loc = cp_expr_loc_or_loc (t, input_location); |
8400e75e | 2269 | |
e9a25f70 JL |
2270 | /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that |
2271 | the first operand is always the one to be used if both operands | |
2272 | are equal, so we know what conditional expression this used to be. */ | |
2273 | if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR) | |
2274 | { | |
2a67bec2 ILT |
2275 | tree op0 = TREE_OPERAND (t, 0); |
2276 | tree op1 = TREE_OPERAND (t, 1); | |
2277 | ||
d211a298 | 2278 | /* The following code is incorrect if either operand side-effects. */ |
2a67bec2 ILT |
2279 | gcc_assert (!TREE_SIDE_EFFECTS (op0) |
2280 | && !TREE_SIDE_EFFECTS (op1)); | |
e9a25f70 | 2281 | return |
8400e75e DM |
2282 | build_conditional_expr (loc, |
2283 | build_x_binary_op (loc, | |
4fe977f2 | 2284 | (TREE_CODE (t) == MIN_EXPR |
e9a25f70 | 2285 | ? LE_EXPR : GE_EXPR), |
2a67bec2 ILT |
2286 | op0, TREE_CODE (op0), |
2287 | op1, TREE_CODE (op1), | |
6904f4b4 | 2288 | /*overload=*/NULL, |
5ade1ed2 | 2289 | complain), |
e51fbec3 MP |
2290 | cp_build_unary_op (code, op0, false, complain), |
2291 | cp_build_unary_op (code, op1, false, complain), | |
5ade1ed2 | 2292 | complain); |
e9a25f70 JL |
2293 | } |
2294 | ||
8d08fdba | 2295 | return |
8400e75e | 2296 | build_conditional_expr (loc, TREE_OPERAND (t, 0), |
e51fbec3 | 2297 | cp_build_unary_op (code, TREE_OPERAND (t, 1), false, |
5ade1ed2 | 2298 | complain), |
e51fbec3 | 2299 | cp_build_unary_op (code, TREE_OPERAND (t, 2), false, |
5ade1ed2 DG |
2300 | complain), |
2301 | complain); | |
8d08fdba MS |
2302 | } |
2303 | ||
9e9ff709 MS |
2304 | /* Given the TYPE of an anonymous union field inside T, return the |
2305 | FIELD_DECL for the field. If not found return NULL_TREE. Because | |
2306 | anonymous unions can nest, we must also search all anonymous unions | |
2307 | that are directly reachable. */ | |
e92cc029 | 2308 | |
b3dd05b1 | 2309 | tree |
acd8e2d0 | 2310 | lookup_anon_field (tree t, tree type) |
9e9ff709 MS |
2311 | { |
2312 | tree field; | |
2313 | ||
b75cebc4 MP |
2314 | t = TYPE_MAIN_VARIANT (t); |
2315 | ||
910ad8de | 2316 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
9e9ff709 MS |
2317 | { |
2318 | if (TREE_STATIC (field)) | |
2319 | continue; | |
17bbb839 | 2320 | if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) |
9e9ff709 MS |
2321 | continue; |
2322 | ||
2323 | /* If we find it directly, return the field. */ | |
2324 | if (DECL_NAME (field) == NULL_TREE | |
71631a1f | 2325 | && type == TYPE_MAIN_VARIANT (TREE_TYPE (field))) |
9e9ff709 MS |
2326 | { |
2327 | return field; | |
2328 | } | |
2329 | ||
2330 | /* Otherwise, it could be nested, search harder. */ | |
2331 | if (DECL_NAME (field) == NULL_TREE | |
6bdb8141 | 2332 | && ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
9e9ff709 MS |
2333 | { |
2334 | tree subfield = lookup_anon_field (TREE_TYPE (field), type); | |
2335 | if (subfield) | |
2336 | return subfield; | |
2337 | } | |
2338 | } | |
2339 | return NULL_TREE; | |
2340 | } | |
2341 | ||
50ad9642 MM |
2342 | /* Build an expression representing OBJECT.MEMBER. OBJECT is an |
2343 | expression; MEMBER is a DECL or baselink. If ACCESS_PATH is | |
2344 | non-NULL, it indicates the path to the base used to name MEMBER. | |
2345 | If PRESERVE_REFERENCE is true, the expression returned will have | |
2346 | REFERENCE_TYPE if the MEMBER does. Otherwise, the expression | |
c8094d83 | 2347 | returned will have the type referred to by the reference. |
50ad9642 MM |
2348 | |
2349 | This function does not perform access control; that is either done | |
2350 | earlier by the parser when the name of MEMBER is resolved to MEMBER | |
2351 | itself, or later when overload resolution selects one of the | |
2352 | functions indicated by MEMBER. */ | |
e92cc029 | 2353 | |
8d08fdba | 2354 | tree |
e87eed2a | 2355 | build_class_member_access_expr (cp_expr object, tree member, |
5ade1ed2 DG |
2356 | tree access_path, bool preserve_reference, |
2357 | tsubst_flags_t complain) | |
8d08fdba | 2358 | { |
50ad9642 MM |
2359 | tree object_type; |
2360 | tree member_scope; | |
2361 | tree result = NULL_TREE; | |
557831a9 | 2362 | tree using_decl = NULL_TREE; |
8d08fdba | 2363 | |
2e5d2970 | 2364 | if (error_operand_p (object) || error_operand_p (member)) |
01240200 | 2365 | return error_mark_node; |
5566b478 | 2366 | |
50bc768d | 2367 | gcc_assert (DECL_P (member) || BASELINK_P (member)); |
8d08fdba | 2368 | |
50ad9642 | 2369 | /* [expr.ref] |
5566b478 | 2370 | |
50ad9642 MM |
2371 | The type of the first expression shall be "class object" (of a |
2372 | complete type). */ | |
2373 | object_type = TREE_TYPE (object); | |
c8094d83 | 2374 | if (!currently_open_class (object_type) |
309714d4 | 2375 | && !complete_type_or_maybe_complain (object_type, object, complain)) |
50ad9642 MM |
2376 | return error_mark_node; |
2377 | if (!CLASS_TYPE_P (object_type)) | |
fc378698 | 2378 | { |
5ade1ed2 | 2379 | if (complain & tf_error) |
a7248d5f | 2380 | { |
71a93b08 | 2381 | if (INDIRECT_TYPE_P (object_type) |
a7248d5f PC |
2382 | && CLASS_TYPE_P (TREE_TYPE (object_type))) |
2383 | error ("request for member %qD in %qE, which is of pointer " | |
2384 | "type %qT (maybe you meant to use %<->%> ?)", | |
e87eed2a | 2385 | member, object.get_value (), object_type); |
a7248d5f PC |
2386 | else |
2387 | error ("request for member %qD in %qE, which is of non-class " | |
e87eed2a | 2388 | "type %qT", member, object.get_value (), object_type); |
a7248d5f | 2389 | } |
50ad9642 | 2390 | return error_mark_node; |
fc378698 | 2391 | } |
8d08fdba | 2392 | |
50ad9642 MM |
2393 | /* The standard does not seem to actually say that MEMBER must be a |
2394 | member of OBJECT_TYPE. However, that is clearly what is | |
2395 | intended. */ | |
2396 | if (DECL_P (member)) | |
8d08fdba | 2397 | { |
50ad9642 | 2398 | member_scope = DECL_CLASS_CONTEXT (member); |
9f635aba PC |
2399 | if (!mark_used (member, complain) && !(complain & tf_error)) |
2400 | return error_mark_node; | |
50ad9642 | 2401 | if (TREE_DEPRECATED (member)) |
9b86d6bb | 2402 | warn_deprecated_use (member, NULL_TREE); |
50ad9642 MM |
2403 | } |
2404 | else | |
0197270c | 2405 | member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member)); |
50ad9642 MM |
2406 | /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will |
2407 | presently be the anonymous union. Go outwards until we find a | |
2408 | type related to OBJECT_TYPE. */ | |
8d0d1915 | 2409 | while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope)) |
50ad9642 MM |
2410 | && !same_type_ignoring_top_level_qualifiers_p (member_scope, |
2411 | object_type)) | |
2412 | member_scope = TYPE_CONTEXT (member_scope); | |
2413 | if (!member_scope || !DERIVED_FROM_P (member_scope, object_type)) | |
2414 | { | |
5ade1ed2 DG |
2415 | if (complain & tf_error) |
2416 | { | |
2417 | if (TREE_CODE (member) == FIELD_DECL) | |
2418 | error ("invalid use of nonstatic data member %qE", member); | |
2419 | else | |
2420 | error ("%qD is not a member of %qT", member, object_type); | |
2421 | } | |
50ad9642 | 2422 | return error_mark_node; |
8d08fdba | 2423 | } |
8d08fdba | 2424 | |
f576dfc4 JM |
2425 | /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into |
2426 | `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue | |
3b426391 | 2427 | in the front end; only _DECLs and _REFs are lvalues in the back end. */ |
f576dfc4 JM |
2428 | { |
2429 | tree temp = unary_complex_lvalue (ADDR_EXPR, object); | |
2430 | if (temp) | |
580b8d9b JJ |
2431 | { |
2432 | temp = cp_build_fold_indirect_ref (temp); | |
2433 | if (xvalue_p (object) && !xvalue_p (temp)) | |
2434 | /* Preserve xvalue kind. */ | |
2435 | temp = move (temp); | |
2436 | object = temp; | |
2437 | } | |
f576dfc4 | 2438 | } |
2050a1bb | 2439 | |
50ad9642 MM |
2440 | /* In [expr.ref], there is an explicit list of the valid choices for |
2441 | MEMBER. We check for each of those cases here. */ | |
5a6ccc94 | 2442 | if (VAR_P (member)) |
8d08fdba | 2443 | { |
50ad9642 MM |
2444 | /* A static data member. */ |
2445 | result = member; | |
2aa64966 | 2446 | mark_exp_read (object); |
5c3703c5 JJ |
2447 | |
2448 | if (tree wrap = maybe_get_tls_wrapper_call (result)) | |
2449 | /* Replace an evaluated use of the thread_local variable with | |
2450 | a call to its wrapper. */ | |
2451 | result = wrap; | |
2452 | ||
50ad9642 MM |
2453 | /* If OBJECT has side-effects, they are supposed to occur. */ |
2454 | if (TREE_SIDE_EFFECTS (object)) | |
f293ce4b | 2455 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result); |
8d08fdba | 2456 | } |
50ad9642 MM |
2457 | else if (TREE_CODE (member) == FIELD_DECL) |
2458 | { | |
2459 | /* A non-static data member. */ | |
2460 | bool null_object_p; | |
2461 | int type_quals; | |
2462 | tree member_type; | |
8d08fdba | 2463 | |
1a161cd7 MS |
2464 | if (INDIRECT_REF_P (object)) |
2465 | null_object_p = | |
2466 | integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0))); | |
2467 | else | |
2468 | null_object_p = false; | |
8d08fdba | 2469 | |
50ad9642 | 2470 | /* Convert OBJECT to the type of MEMBER. */ |
bdaa131b JM |
2471 | if (!same_type_p (TYPE_MAIN_VARIANT (object_type), |
2472 | TYPE_MAIN_VARIANT (member_scope))) | |
8d08fdba | 2473 | { |
50ad9642 MM |
2474 | tree binfo; |
2475 | base_kind kind; | |
2476 | ||
b8836dbe JM |
2477 | /* We didn't complain above about a currently open class, but now we |
2478 | must: we don't know how to refer to a base member before layout is | |
2479 | complete. But still don't complain in a template. */ | |
2480 | if (!dependent_type_p (object_type) | |
2481 | && !complete_type_or_maybe_complain (object_type, object, | |
2482 | complain)) | |
2483 | return error_mark_node; | |
2484 | ||
50ad9642 | 2485 | binfo = lookup_base (access_path ? access_path : object_type, |
22854930 | 2486 | member_scope, ba_unique, &kind, complain); |
50ad9642 MM |
2487 | if (binfo == error_mark_node) |
2488 | return error_mark_node; | |
2489 | ||
bdaa131b | 2490 | /* It is invalid to try to get to a virtual base of a |
50ad9642 MM |
2491 | NULL object. The most common cause is invalid use of |
2492 | offsetof macro. */ | |
2493 | if (null_object_p && kind == bk_via_virtual) | |
2494 | { | |
5ade1ed2 DG |
2495 | if (complain & tf_error) |
2496 | { | |
8591575f JM |
2497 | error ("invalid access to non-static data member %qD in " |
2498 | "virtual base of NULL object", member); | |
5ade1ed2 | 2499 | } |
50ad9642 MM |
2500 | return error_mark_node; |
2501 | } | |
2502 | ||
2503 | /* Convert to the base. */ | |
c8094d83 | 2504 | object = build_base_path (PLUS_EXPR, object, binfo, |
a271590a | 2505 | /*nonnull=*/1, complain); |
50ad9642 MM |
2506 | /* If we found the base successfully then we should be able |
2507 | to convert to it successfully. */ | |
50bc768d | 2508 | gcc_assert (object != error_mark_node); |
8d08fdba | 2509 | } |
3b5b4904 | 2510 | |
50ad9642 MM |
2511 | /* If MEMBER is from an anonymous aggregate, we have converted |
2512 | OBJECT so that it refers to the class containing the | |
2513 | anonymous union. Generate a reference to the anonymous union | |
2514 | itself, and recur to find MEMBER. */ | |
0ca7178c | 2515 | if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member)) |
a723baf1 MM |
2516 | /* When this code is called from build_field_call, the |
2517 | object already has the type of the anonymous union. | |
2518 | That is because the COMPONENT_REF was already | |
2519 | constructed, and was then disassembled before calling | |
2520 | build_field_call. After the function-call code is | |
2521 | cleaned up, this waste can be eliminated. */ | |
c8094d83 | 2522 | && (!same_type_ignoring_top_level_qualifiers_p |
a723baf1 | 2523 | (TREE_TYPE (object), DECL_CONTEXT (member)))) |
50ad9642 MM |
2524 | { |
2525 | tree anonymous_union; | |
2526 | ||
2527 | anonymous_union = lookup_anon_field (TREE_TYPE (object), | |
2528 | DECL_CONTEXT (member)); | |
2529 | object = build_class_member_access_expr (object, | |
2530 | anonymous_union, | |
2531 | /*access_path=*/NULL_TREE, | |
5ade1ed2 DG |
2532 | preserve_reference, |
2533 | complain); | |
50ad9642 MM |
2534 | } |
2535 | ||
2536 | /* Compute the type of the field, as described in [expr.ref]. */ | |
2537 | type_quals = TYPE_UNQUALIFIED; | |
2538 | member_type = TREE_TYPE (member); | |
9f613f06 | 2539 | if (!TYPE_REF_P (member_type)) |
50ad9642 | 2540 | { |
c8094d83 | 2541 | type_quals = (cp_type_quals (member_type) |
50ad9642 | 2542 | | cp_type_quals (object_type)); |
c8094d83 | 2543 | |
50ad9642 MM |
2544 | /* A field is const (volatile) if the enclosing object, or the |
2545 | field itself, is const (volatile). But, a mutable field is | |
2546 | not const, even within a const object. */ | |
2547 | if (DECL_MUTABLE_P (member)) | |
2548 | type_quals &= ~TYPE_QUAL_CONST; | |
2549 | member_type = cp_build_qualified_type (member_type, type_quals); | |
2550 | } | |
2551 | ||
35228ac7 JJ |
2552 | result = build3_loc (input_location, COMPONENT_REF, member_type, |
2553 | object, member, NULL_TREE); | |
50ad9642 MM |
2554 | |
2555 | /* Mark the expression const or volatile, as appropriate. Even | |
2556 | though we've dealt with the type above, we still have to mark the | |
2557 | expression itself. */ | |
2558 | if (type_quals & TYPE_QUAL_CONST) | |
2559 | TREE_READONLY (result) = 1; | |
12a669d1 | 2560 | if (type_quals & TYPE_QUAL_VOLATILE) |
50ad9642 MM |
2561 | TREE_THIS_VOLATILE (result) = 1; |
2562 | } | |
2563 | else if (BASELINK_P (member)) | |
9e9ff709 | 2564 | { |
50ad9642 MM |
2565 | /* The member is a (possibly overloaded) member function. */ |
2566 | tree functions; | |
a723baf1 | 2567 | tree type; |
50ad9642 MM |
2568 | |
2569 | /* If the MEMBER is exactly one static member function, then we | |
2570 | know the type of the expression. Otherwise, we must wait | |
2571 | until overload resolution has been performed. */ | |
2572 | functions = BASELINK_FUNCTIONS (member); | |
2573 | if (TREE_CODE (functions) == FUNCTION_DECL | |
2574 | && DECL_STATIC_FUNCTION_P (functions)) | |
a723baf1 | 2575 | type = TREE_TYPE (functions); |
50ad9642 | 2576 | else |
a723baf1 MM |
2577 | type = unknown_type_node; |
2578 | /* Note that we do not convert OBJECT to the BASELINK_BINFO | |
2579 | base. That will happen when the function is called. */ | |
6fe7ce18 MP |
2580 | result = build3_loc (input_location, COMPONENT_REF, type, object, member, |
2581 | NULL_TREE); | |
9e9ff709 | 2582 | } |
50ad9642 | 2583 | else if (TREE_CODE (member) == CONST_DECL) |
b928a651 | 2584 | { |
50ad9642 MM |
2585 | /* The member is an enumerator. */ |
2586 | result = member; | |
2587 | /* If OBJECT has side-effects, they are supposed to occur. */ | |
2588 | if (TREE_SIDE_EFFECTS (object)) | |
f293ce4b RS |
2589 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
2590 | object, result); | |
b928a651 | 2591 | } |
557831a9 FC |
2592 | else if ((using_decl = strip_using_decl (member)) != member) |
2593 | result = build_class_member_access_expr (object, | |
2594 | using_decl, | |
2595 | access_path, preserve_reference, | |
2596 | complain); | |
8d08fdba MS |
2597 | else |
2598 | { | |
5ade1ed2 DG |
2599 | if (complain & tf_error) |
2600 | error ("invalid use of %qD", member); | |
50ad9642 MM |
2601 | return error_mark_node; |
2602 | } | |
8d08fdba | 2603 | |
50ad9642 MM |
2604 | if (!preserve_reference) |
2605 | /* [expr.ref] | |
c8094d83 | 2606 | |
50ad9642 MM |
2607 | If E2 is declared to have type "reference to T", then ... the |
2608 | type of E1.E2 is T. */ | |
2609 | result = convert_from_reference (result); | |
e6f62286 | 2610 | |
50ad9642 MM |
2611 | return result; |
2612 | } | |
8d08fdba | 2613 | |
05134211 JM |
2614 | /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if |
2615 | SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */ | |
a723baf1 MM |
2616 | |
2617 | static tree | |
76545796 PC |
2618 | lookup_destructor (tree object, tree scope, tree dtor_name, |
2619 | tsubst_flags_t complain) | |
a723baf1 MM |
2620 | { |
2621 | tree object_type = TREE_TYPE (object); | |
2622 | tree dtor_type = TREE_OPERAND (dtor_name, 0); | |
4546865e | 2623 | tree expr; |
a723baf1 | 2624 | |
9ca57929 PC |
2625 | /* We've already complained about this destructor. */ |
2626 | if (dtor_type == error_mark_node) | |
2627 | return error_mark_node; | |
2628 | ||
1b3d28a8 | 2629 | if (scope && !check_dtor_name (scope, dtor_type)) |
a723baf1 | 2630 | { |
76545796 PC |
2631 | if (complain & tf_error) |
2632 | error ("qualified type %qT does not match destructor name ~%qT", | |
2633 | scope, dtor_type); | |
a723baf1 MM |
2634 | return error_mark_node; |
2635 | } | |
3df70c62 JM |
2636 | if (is_auto (dtor_type)) |
2637 | dtor_type = object_type; | |
2638 | else if (identifier_p (dtor_type)) | |
05134211 JM |
2639 | { |
2640 | /* In a template, names we can't find a match for are still accepted | |
2641 | destructor names, and we check them here. */ | |
2642 | if (check_dtor_name (object_type, dtor_type)) | |
2643 | dtor_type = object_type; | |
2644 | else | |
2645 | { | |
76545796 PC |
2646 | if (complain & tf_error) |
2647 | error ("object type %qT does not match destructor name ~%qT", | |
2648 | object_type, dtor_type); | |
05134211 JM |
2649 | return error_mark_node; |
2650 | } | |
2651 | ||
2652 | } | |
2653 | else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type))) | |
a723baf1 | 2654 | { |
76545796 PC |
2655 | if (complain & tf_error) |
2656 | error ("the type being destroyed is %qT, but the destructor " | |
2657 | "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type); | |
a723baf1 MM |
2658 | return error_mark_node; |
2659 | } | |
4546865e | 2660 | expr = lookup_member (dtor_type, complete_dtor_identifier, |
db422ace PC |
2661 | /*protect=*/1, /*want_type=*/false, |
2662 | tf_warning_or_error); | |
e383e095 KT |
2663 | if (!expr) |
2664 | { | |
2665 | if (complain & tf_error) | |
2666 | cxx_incomplete_type_error (dtor_name, dtor_type); | |
2667 | return error_mark_node; | |
2668 | } | |
4546865e MM |
2669 | expr = (adjust_result_of_qualified_name_lookup |
2670 | (expr, dtor_type, object_type)); | |
917c1602 JM |
2671 | if (scope == NULL_TREE) |
2672 | /* We need to call adjust_result_of_qualified_name_lookup in case the | |
2673 | destructor names a base class, but we unset BASELINK_QUALIFIED_P so | |
2674 | that we still get virtual function binding. */ | |
2675 | BASELINK_QUALIFIED_P (expr) = false; | |
4546865e | 2676 | return expr; |
a723baf1 MM |
2677 | } |
2678 | ||
02ed62dd MM |
2679 | /* An expression of the form "A::template B" has been resolved to |
2680 | DECL. Issue a diagnostic if B is not a template or template | |
2681 | specialization. */ | |
2682 | ||
2683 | void | |
2684 | check_template_keyword (tree decl) | |
2685 | { | |
2686 | /* The standard says: | |
2687 | ||
2688 | [temp.names] | |
2689 | ||
2690 | If a name prefixed by the keyword template is not a member | |
2691 | template, the program is ill-formed. | |
2692 | ||
2693 | DR 228 removed the restriction that the template be a member | |
3db45ab5 MS |
2694 | template. |
2695 | ||
dd36d4e1 | 2696 | DR 96, if accepted would add the further restriction that explicit |
02ed62dd MM |
2697 | template arguments must be provided if the template keyword is |
2698 | used, but, as of 2005-10-16, that DR is still in "drafting". If | |
2699 | this DR is accepted, then the semantic checks here can be | |
2700 | simplified, as the entity named must in fact be a template | |
2701 | specialization, rather than, as at present, a set of overloaded | |
2702 | functions containing at least one template function. */ | |
2703 | if (TREE_CODE (decl) != TEMPLATE_DECL | |
2704 | && TREE_CODE (decl) != TEMPLATE_ID_EXPR) | |
2705 | { | |
38d795d2 PP |
2706 | if (VAR_P (decl)) |
2707 | { | |
2708 | if (DECL_USE_TEMPLATE (decl) | |
2709 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) | |
2710 | ; | |
2711 | else | |
2712 | permerror (input_location, "%qD is not a template", decl); | |
2713 | } | |
2714 | else if (!is_overloaded_fn (decl)) | |
cbe5f3b3 | 2715 | permerror (input_location, "%qD is not a template", decl); |
02ed62dd MM |
2716 | else |
2717 | { | |
3f267553 NS |
2718 | bool found = false; |
2719 | ||
2720 | for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl)); | |
2721 | !found && iter; ++iter) | |
02ed62dd | 2722 | { |
3f267553 | 2723 | tree fn = *iter; |
02ed62dd | 2724 | if (TREE_CODE (fn) == TEMPLATE_DECL |
3f267553 NS |
2725 | || TREE_CODE (fn) == TEMPLATE_ID_EXPR |
2726 | || (TREE_CODE (fn) == FUNCTION_DECL | |
2727 | && DECL_USE_TEMPLATE (fn) | |
2728 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))) | |
2729 | found = true; | |
02ed62dd | 2730 | } |
3f267553 | 2731 | if (!found) |
cbe5f3b3 | 2732 | permerror (input_location, "%qD is not a template", decl); |
02ed62dd MM |
2733 | } |
2734 | } | |
2735 | } | |
2736 | ||
10791753 | 2737 | /* Record that an access failure occurred on BASETYPE_PATH attempting |
03f6d32e | 2738 | to access DECL, where DIAG_DECL should be used for diagnostics. */ |
10791753 DM |
2739 | |
2740 | void | |
2741 | access_failure_info::record_access_failure (tree basetype_path, | |
03f6d32e | 2742 | tree decl, tree diag_decl) |
10791753 DM |
2743 | { |
2744 | m_was_inaccessible = true; | |
2745 | m_basetype_path = basetype_path; | |
03f6d32e DM |
2746 | m_decl = decl; |
2747 | m_diag_decl = diag_decl; | |
10791753 DM |
2748 | } |
2749 | ||
2750 | /* If an access failure was recorded, then attempt to locate an | |
03f6d32e DM |
2751 | accessor function for the pertinent field. |
2752 | Otherwise, return NULL_TREE. */ | |
10791753 | 2753 | |
03f6d32e DM |
2754 | tree |
2755 | access_failure_info::get_any_accessor (bool const_p) const | |
10791753 | 2756 | { |
03f6d32e DM |
2757 | if (!was_inaccessible_p ()) |
2758 | return NULL_TREE; | |
10791753 DM |
2759 | |
2760 | tree accessor | |
03f6d32e | 2761 | = locate_field_accessor (m_basetype_path, m_diag_decl, const_p); |
10791753 | 2762 | if (!accessor) |
03f6d32e | 2763 | return NULL_TREE; |
10791753 DM |
2764 | |
2765 | /* The accessor must itself be accessible for it to be a reasonable | |
2766 | suggestion. */ | |
2767 | if (!accessible_p (m_basetype_path, accessor, true)) | |
03f6d32e | 2768 | return NULL_TREE; |
10791753 | 2769 | |
03f6d32e DM |
2770 | return accessor; |
2771 | } | |
2772 | ||
2773 | /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by | |
2774 | replacing the primary location in RICHLOC with "accessor()". */ | |
2775 | ||
2776 | void | |
2777 | access_failure_info::add_fixit_hint (rich_location *richloc, | |
2778 | tree accessor_decl) | |
2779 | { | |
10791753 | 2780 | pretty_printer pp; |
03f6d32e DM |
2781 | pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor_decl))); |
2782 | richloc->add_fixit_replace (pp_formatted_text (&pp)); | |
2783 | } | |
2784 | ||
2785 | /* If an access failure was recorded, then attempt to locate an | |
2786 | accessor function for the pertinent field, and if one is | |
2787 | available, add a note and fix-it hint suggesting using it. */ | |
2788 | ||
2789 | void | |
2790 | access_failure_info::maybe_suggest_accessor (bool const_p) const | |
2791 | { | |
2792 | tree accessor = get_any_accessor (const_p); | |
2793 | if (accessor == NULL_TREE) | |
2794 | return; | |
2795 | rich_location richloc (line_table, input_location); | |
2796 | add_fixit_hint (&richloc, accessor); | |
64a5912c | 2797 | inform (&richloc, "field %q#D can be accessed via %q#D", |
03f6d32e DM |
2798 | m_diag_decl, accessor); |
2799 | } | |
2800 | ||
2801 | /* Subroutine of finish_class_member_access_expr. | |
2802 | Issue an error about NAME not being a member of ACCESS_PATH (or | |
2803 | OBJECT_TYPE), potentially providing a fix-it hint for misspelled | |
2804 | names. */ | |
2805 | ||
2806 | static void | |
2807 | complain_about_unrecognized_member (tree access_path, tree name, | |
2808 | tree object_type) | |
2809 | { | |
2810 | /* Attempt to provide a hint about misspelled names. */ | |
2811 | tree guessed_id = lookup_member_fuzzy (access_path, name, | |
2812 | /*want_type=*/false); | |
2813 | if (guessed_id == NULL_TREE) | |
2814 | { | |
2815 | /* No hint. */ | |
2816 | error ("%q#T has no member named %qE", | |
2817 | TREE_CODE (access_path) == TREE_BINFO | |
2818 | ? TREE_TYPE (access_path) : object_type, name); | |
2819 | return; | |
2820 | } | |
2821 | ||
2822 | location_t bogus_component_loc = input_location; | |
2823 | gcc_rich_location rich_loc (bogus_component_loc); | |
2824 | ||
2825 | /* Check that the guessed name is accessible along access_path. */ | |
2826 | access_failure_info afi; | |
2827 | lookup_member (access_path, guessed_id, /*protect=*/1, | |
2828 | /*want_type=*/false, /*complain=*/false, | |
2829 | &afi); | |
2830 | if (afi.was_inaccessible_p ()) | |
2831 | { | |
2832 | tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type)); | |
2833 | if (accessor) | |
2834 | { | |
2835 | /* The guessed name isn't directly accessible, but can be accessed | |
2836 | via an accessor member function. */ | |
2837 | afi.add_fixit_hint (&rich_loc, accessor); | |
2838 | error_at (&rich_loc, | |
2839 | "%q#T has no member named %qE;" | |
2840 | " did you mean %q#D? (accessible via %q#D)", | |
2841 | TREE_CODE (access_path) == TREE_BINFO | |
2842 | ? TREE_TYPE (access_path) : object_type, | |
2843 | name, afi.get_diag_decl (), accessor); | |
2844 | } | |
2845 | else | |
2846 | { | |
2847 | /* The guessed name isn't directly accessible, and no accessor | |
2848 | member function could be found. */ | |
2849 | error_at (&rich_loc, | |
2850 | "%q#T has no member named %qE;" | |
2851 | " did you mean %q#D? (not accessible from this context)", | |
2852 | TREE_CODE (access_path) == TREE_BINFO | |
2853 | ? TREE_TYPE (access_path) : object_type, | |
2854 | name, afi.get_diag_decl ()); | |
2855 | complain_about_access (afi.get_decl (), afi.get_diag_decl (), false); | |
2856 | } | |
2857 | } | |
2858 | else | |
2859 | { | |
2860 | /* The guessed name is directly accessible; suggest it. */ | |
2861 | rich_loc.add_fixit_misspelled_id (bogus_component_loc, | |
2862 | guessed_id); | |
2863 | error_at (&rich_loc, | |
2864 | "%q#T has no member named %qE;" | |
2865 | " did you mean %qE?", | |
2866 | TREE_CODE (access_path) == TREE_BINFO | |
2867 | ? TREE_TYPE (access_path) : object_type, | |
2868 | name, guessed_id); | |
2869 | } | |
10791753 DM |
2870 | } |
2871 | ||
50ad9642 MM |
2872 | /* This function is called by the parser to process a class member |
2873 | access expression of the form OBJECT.NAME. NAME is a node used by | |
2874 | the parser to represent a name; it is not yet a DECL. It may, | |
2875 | however, be a BASELINK where the BASELINK_FUNCTIONS is a | |
2876 | TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and | |
2877 | there is no reason to do the lookup twice, so the parser keeps the | |
02ed62dd MM |
2878 | BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to |
2879 | be a template via the use of the "A::template B" syntax. */ | |
8d08fdba | 2880 | |
50ad9642 | 2881 | tree |
e87eed2a | 2882 | finish_class_member_access_expr (cp_expr object, tree name, bool template_p, |
5ade1ed2 | 2883 | tsubst_flags_t complain) |
50ad9642 | 2884 | { |
d17811fd | 2885 | tree expr; |
50ad9642 MM |
2886 | tree object_type; |
2887 | tree member; | |
2888 | tree access_path = NULL_TREE; | |
d17811fd MM |
2889 | tree orig_object = object; |
2890 | tree orig_name = name; | |
3c8c2a0a | 2891 | |
50ad9642 MM |
2892 | if (object == error_mark_node || name == error_mark_node) |
2893 | return error_mark_node; | |
3c8c2a0a | 2894 | |
e58a9aa1 ZL |
2895 | /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */ |
2896 | if (!objc_is_public (object, name)) | |
2897 | return error_mark_node; | |
2898 | ||
d17811fd MM |
2899 | object_type = TREE_TYPE (object); |
2900 | ||
50ad9642 | 2901 | if (processing_template_decl) |
d17811fd | 2902 | { |
a2b4cfaa | 2903 | if (/* If OBJECT is dependent, so is OBJECT.NAME. */ |
23cb7266 | 2904 | type_dependent_object_expression_p (object) |
d17811fd MM |
2905 | /* If NAME is "f<args>", where either 'f' or 'args' is |
2906 | dependent, then the expression is dependent. */ | |
2907 | || (TREE_CODE (name) == TEMPLATE_ID_EXPR | |
2908 | && dependent_template_id_p (TREE_OPERAND (name, 0), | |
2909 | TREE_OPERAND (name, 1))) | |
2910 | /* If NAME is "T::X" where "T" is dependent, then the | |
2911 | expression is dependent. */ | |
2912 | || (TREE_CODE (name) == SCOPE_REF | |
2913 | && TYPE_P (TREE_OPERAND (name, 0)) | |
2a4030ef NS |
2914 | && dependent_scope_p (TREE_OPERAND (name, 0))) |
2915 | /* If NAME is operator T where "T" is dependent, we can't | |
2916 | lookup until we instantiate the T. */ | |
2917 | || (TREE_CODE (name) == IDENTIFIER_NODE | |
2918 | && IDENTIFIER_CONV_OP_P (name) | |
2919 | && dependent_type_p (TREE_TYPE (name)))) | |
23cb7266 JM |
2920 | { |
2921 | dependent: | |
2922 | return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, | |
99676625 | 2923 | orig_object, orig_name, NULL_TREE); |
23cb7266 | 2924 | } |
d17811fd MM |
2925 | object = build_non_dependent_expr (object); |
2926 | } | |
668ea4b1 | 2927 | else if (c_dialect_objc () |
9dc6f476 | 2928 | && identifier_p (name) |
46a88c12 | 2929 | && (expr = objc_maybe_build_component_ref (object, name))) |
668ea4b1 IS |
2930 | return expr; |
2931 | ||
50ad9642 | 2932 | /* [expr.ref] |
e23bd218 | 2933 | |
50ad9642 MM |
2934 | The type of the first expression shall be "class object" (of a |
2935 | complete type). */ | |
c8094d83 | 2936 | if (!currently_open_class (object_type) |
309714d4 | 2937 | && !complete_type_or_maybe_complain (object_type, object, complain)) |
50ad9642 MM |
2938 | return error_mark_node; |
2939 | if (!CLASS_TYPE_P (object_type)) | |
2940 | { | |
5ade1ed2 | 2941 | if (complain & tf_error) |
a7248d5f | 2942 | { |
71a93b08 | 2943 | if (INDIRECT_TYPE_P (object_type) |
a7248d5f PC |
2944 | && CLASS_TYPE_P (TREE_TYPE (object_type))) |
2945 | error ("request for member %qD in %qE, which is of pointer " | |
2946 | "type %qT (maybe you meant to use %<->%> ?)", | |
e87eed2a | 2947 | name, object.get_value (), object_type); |
a7248d5f PC |
2948 | else |
2949 | error ("request for member %qD in %qE, which is of non-class " | |
e87eed2a | 2950 | "type %qT", name, object.get_value (), object_type); |
a7248d5f | 2951 | } |
50ad9642 MM |
2952 | return error_mark_node; |
2953 | } | |
f2d9afec | 2954 | |
50ad9642 | 2955 | if (BASELINK_P (name)) |
fdeff563 NS |
2956 | /* A member function that has already been looked up. */ |
2957 | member = name; | |
50ad9642 MM |
2958 | else |
2959 | { | |
2960 | bool is_template_id = false; | |
2961 | tree template_args = NULL_TREE; | |
e6b8075c | 2962 | tree scope = NULL_TREE; |
2ee887f2 | 2963 | |
e6b8075c | 2964 | access_path = object_type; |
f2d9afec | 2965 | |
50ad9642 MM |
2966 | if (TREE_CODE (name) == SCOPE_REF) |
2967 | { | |
e13d2b4e MM |
2968 | /* A qualified name. The qualifying class or namespace `S' |
2969 | has already been looked up; it is either a TYPE or a | |
2970 | NAMESPACE_DECL. */ | |
50ad9642 MM |
2971 | scope = TREE_OPERAND (name, 0); |
2972 | name = TREE_OPERAND (name, 1); | |
50ad9642 MM |
2973 | |
2974 | /* If SCOPE is a namespace, then the qualified name does not | |
2975 | name a member of OBJECT_TYPE. */ | |
2976 | if (TREE_CODE (scope) == NAMESPACE_DECL) | |
2ee887f2 | 2977 | { |
5ade1ed2 DG |
2978 | if (complain & tf_error) |
2979 | error ("%<%D::%D%> is not a member of %qT", | |
2980 | scope, name, object_type); | |
2ee887f2 MS |
2981 | return error_mark_node; |
2982 | } | |
e6b8075c NS |
2983 | } |
2984 | ||
2985 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
2986 | { | |
2987 | is_template_id = true; | |
2988 | template_args = TREE_OPERAND (name, 1); | |
2989 | name = TREE_OPERAND (name, 0); | |
2990 | ||
848bf88d NS |
2991 | if (!identifier_p (name)) |
2992 | name = OVL_NAME (name); | |
e6b8075c | 2993 | } |
50ad9642 | 2994 | |
e6b8075c NS |
2995 | if (scope) |
2996 | { | |
c53d966d JM |
2997 | if (TREE_CODE (scope) == ENUMERAL_TYPE) |
2998 | { | |
e6b8075c | 2999 | gcc_assert (!is_template_id); |
c53d966d JM |
3000 | /* Looking up a member enumerator (c++/56793). */ |
3001 | if (!TYPE_CLASS_SCOPE_P (scope) | |
3002 | || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type)) | |
3003 | { | |
3004 | if (complain & tf_error) | |
3005 | error ("%<%D::%D%> is not a member of %qT", | |
3006 | scope, name, object_type); | |
3007 | return error_mark_node; | |
3008 | } | |
3009 | tree val = lookup_enumerator (scope, name); | |
09b59d4d NS |
3010 | if (!val) |
3011 | { | |
3012 | if (complain & tf_error) | |
3013 | error ("%qD is not a member of %qD", | |
3014 | name, scope); | |
3015 | return error_mark_node; | |
3016 | } | |
3017 | ||
c53d966d JM |
3018 | if (TREE_SIDE_EFFECTS (object)) |
3019 | val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val); | |
3020 | return val; | |
3021 | } | |
3022 | ||
e13d2b4e | 3023 | gcc_assert (CLASS_TYPE_P (scope)); |
9dc6f476 | 3024 | gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR); |
e13d2b4e | 3025 | |
abd5730b JM |
3026 | if (constructor_name_p (name, scope)) |
3027 | { | |
3028 | if (complain & tf_error) | |
3029 | error ("cannot call constructor %<%T::%D%> directly", | |
3030 | scope, name); | |
3031 | return error_mark_node; | |
3032 | } | |
3033 | ||
50ad9642 | 3034 | /* Find the base of OBJECT_TYPE corresponding to SCOPE. */ |
22854930 PC |
3035 | access_path = lookup_base (object_type, scope, ba_check, |
3036 | NULL, complain); | |
c8a65a25 | 3037 | if (access_path == error_mark_node) |
50ad9642 | 3038 | return error_mark_node; |
c8a65a25 MM |
3039 | if (!access_path) |
3040 | { | |
92354dc7 JM |
3041 | if (any_dependent_bases_p (object_type)) |
3042 | goto dependent; | |
5ade1ed2 DG |
3043 | if (complain & tf_error) |
3044 | error ("%qT is not a base of %qT", scope, object_type); | |
c8a65a25 MM |
3045 | return error_mark_node; |
3046 | } | |
2ee887f2 | 3047 | } |
d17811fd MM |
3048 | |
3049 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
23cb7266 JM |
3050 | { |
3051 | if (dependent_type_p (object_type)) | |
3052 | /* The destructor isn't declared yet. */ | |
3053 | goto dependent; | |
3054 | member = lookup_destructor (object, scope, name, complain); | |
3055 | } | |
d17811fd | 3056 | else |
9e9ff709 | 3057 | { |
d17811fd | 3058 | /* Look up the member. */ |
10791753 | 3059 | access_failure_info afi; |
c8094d83 | 3060 | member = lookup_member (access_path, name, /*protect=*/1, |
10791753 DM |
3061 | /*want_type=*/false, complain, |
3062 | &afi); | |
3063 | afi.maybe_suggest_accessor (TYPE_READONLY (object_type)); | |
8a5f4379 GDR |
3064 | if (member == NULL_TREE) |
3065 | { | |
23cb7266 JM |
3066 | if (dependent_type_p (object_type)) |
3067 | /* Try again at instantiation time. */ | |
3068 | goto dependent; | |
5ade1ed2 | 3069 | if (complain & tf_error) |
03f6d32e DM |
3070 | complain_about_unrecognized_member (access_path, name, |
3071 | object_type); | |
8a5f4379 GDR |
3072 | return error_mark_node; |
3073 | } | |
d17811fd | 3074 | if (member == error_mark_node) |
50ad9642 | 3075 | return error_mark_node; |
f3365c12 JM |
3076 | if (DECL_P (member) |
3077 | && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member))) | |
3078 | /* Dependent type attributes on the decl mean that the TREE_TYPE is | |
3079 | wrong, so don't use it. */ | |
3080 | goto dependent; | |
23cb7266 JM |
3081 | if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member)) |
3082 | goto dependent; | |
50ad9642 | 3083 | } |
c8094d83 | 3084 | |
50ad9642 MM |
3085 | if (is_template_id) |
3086 | { | |
be93747e | 3087 | tree templ = member; |
c8094d83 | 3088 | |
be93747e | 3089 | if (BASELINK_P (templ)) |
8b1346a8 JM |
3090 | member = lookup_template_function (templ, template_args); |
3091 | else if (variable_template_p (templ)) | |
3092 | member = (lookup_and_finish_template_variable | |
3093 | (templ, template_args, complain)); | |
50ad9642 MM |
3094 | else |
3095 | { | |
5ade1ed2 DG |
3096 | if (complain & tf_error) |
3097 | error ("%qD is not a member template function", name); | |
50ad9642 MM |
3098 | return error_mark_node; |
3099 | } | |
9e9ff709 MS |
3100 | } |
3101 | } | |
3102 | ||
a723baf1 | 3103 | if (TREE_DEPRECATED (member)) |
9b86d6bb | 3104 | warn_deprecated_use (member, NULL_TREE); |
a723baf1 | 3105 | |
02ed62dd MM |
3106 | if (template_p) |
3107 | check_template_keyword (member); | |
3108 | ||
d17811fd | 3109 | expr = build_class_member_access_expr (object, member, access_path, |
5ade1ed2 DG |
3110 | /*preserve_reference=*/false, |
3111 | complain); | |
d17811fd | 3112 | if (processing_template_decl && expr != error_mark_node) |
61e71a9e NS |
3113 | { |
3114 | if (BASELINK_P (member)) | |
3115 | { | |
3116 | if (TREE_CODE (orig_name) == SCOPE_REF) | |
3117 | BASELINK_QUALIFIED_P (member) = 1; | |
3118 | orig_name = member; | |
3119 | } | |
3120 | return build_min_non_dep (COMPONENT_REF, expr, | |
3121 | orig_object, orig_name, | |
3122 | NULL_TREE); | |
3123 | } | |
3db45ab5 | 3124 | |
d17811fd | 3125 | return expr; |
8d08fdba | 3126 | } |
5156628f | 3127 | |
0138d6b2 JM |
3128 | /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate |
3129 | type. */ | |
3130 | ||
3131 | tree | |
3132 | build_simple_component_ref (tree object, tree member) | |
3133 | { | |
3134 | tree type = cp_build_qualified_type (TREE_TYPE (member), | |
3135 | cp_type_quals (TREE_TYPE (object))); | |
cda0a029 JM |
3136 | return build3_loc (input_location, |
3137 | COMPONENT_REF, type, | |
3138 | object, member, NULL_TREE); | |
0138d6b2 JM |
3139 | } |
3140 | ||
50ad9642 MM |
3141 | /* Return an expression for the MEMBER_NAME field in the internal |
3142 | representation of PTRMEM, a pointer-to-member function. (Each | |
3143 | pointer-to-member function type gets its own RECORD_TYPE so it is | |
3144 | more convenient to access the fields by name than by FIELD_DECL.) | |
3145 | This routine converts the NAME to a FIELD_DECL and then creates the | |
3146 | node for the complete expression. */ | |
5156628f MS |
3147 | |
3148 | tree | |
50ad9642 | 3149 | build_ptrmemfunc_access_expr (tree ptrmem, tree member_name) |
5156628f | 3150 | { |
50ad9642 MM |
3151 | tree ptrmem_type; |
3152 | tree member; | |
50ad9642 | 3153 | |
6d0e87b2 JM |
3154 | if (TREE_CODE (ptrmem) == CONSTRUCTOR) |
3155 | { | |
3156 | unsigned int ix; | |
3157 | tree index, value; | |
3158 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem), | |
3159 | ix, index, value) | |
3160 | if (index && DECL_P (index) && DECL_NAME (index) == member_name) | |
3161 | return value; | |
3162 | gcc_unreachable (); | |
3163 | } | |
3164 | ||
50ad9642 MM |
3165 | /* This code is a stripped down version of |
3166 | build_class_member_access_expr. It does not work to use that | |
3167 | routine directly because it expects the object to be of class | |
3168 | type. */ | |
3169 | ptrmem_type = TREE_TYPE (ptrmem); | |
50bc768d | 3170 | gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type)); |
1d7e2ec7 JM |
3171 | for (member = TYPE_FIELDS (ptrmem_type); member; |
3172 | member = DECL_CHAIN (member)) | |
3173 | if (DECL_NAME (member) == member_name) | |
3174 | break; | |
5ae37bdf NS |
3175 | tree res = build_simple_component_ref (ptrmem, member); |
3176 | ||
3177 | TREE_NO_WARNING (res) = 1; | |
3178 | return res; | |
5156628f | 3179 | } |
50ad9642 | 3180 | |
8d08fdba MS |
3181 | /* Given an expression PTR for a pointer, return an expression |
3182 | for the value pointed to. | |
3183 | ERRORSTRING is the name of the operator to appear in error messages. | |
3184 | ||
3185 | This function may need to overload OPERATOR_FNNAME. | |
3186 | Must also handle REFERENCE_TYPEs for C++. */ | |
3187 | ||
3188 | tree | |
4fe977f2 | 3189 | build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring, |
5ade1ed2 | 3190 | tsubst_flags_t complain) |
8d08fdba | 3191 | { |
d17811fd | 3192 | tree orig_expr = expr; |
5566b478 | 3193 | tree rval; |
fcb9363e | 3194 | tree overload = NULL_TREE; |
5566b478 | 3195 | |
5156628f | 3196 | if (processing_template_decl) |
d17811fd | 3197 | { |
d84572a4 | 3198 | /* Retain the type if we know the operand is a pointer. */ |
71a93b08 | 3199 | if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr))) |
ccafc19b | 3200 | return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr); |
d17811fd | 3201 | if (type_dependent_expression_p (expr)) |
f330f599 | 3202 | return build_min_nt_loc (loc, INDIRECT_REF, expr); |
d17811fd MM |
3203 | expr = build_non_dependent_expr (expr); |
3204 | } | |
5566b478 | 3205 | |
4fe977f2 | 3206 | rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr, |
fcb9363e | 3207 | NULL_TREE, NULL_TREE, &overload, complain); |
d17811fd | 3208 | if (!rval) |
5ade1ed2 | 3209 | rval = cp_build_indirect_ref (expr, errorstring, complain); |
d17811fd MM |
3210 | |
3211 | if (processing_template_decl && rval != error_mark_node) | |
fcb9363e PP |
3212 | { |
3213 | if (overload != NULL_TREE) | |
3214 | return (build_min_non_dep_op_overload | |
3215 | (INDIRECT_REF, rval, overload, orig_expr)); | |
3216 | ||
3217 | return build_min_non_dep (INDIRECT_REF, rval, orig_expr); | |
3218 | } | |
d17811fd | 3219 | else |
8d08fdba | 3220 | return rval; |
8d08fdba MS |
3221 | } |
3222 | ||
04757a2a JM |
3223 | /* The implementation of the above, and of indirection implied by other |
3224 | constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */ | |
5ade1ed2 | 3225 | |
04757a2a JM |
3226 | static tree |
3227 | cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring, | |
3228 | tsubst_flags_t complain, bool do_fold) | |
8d08fdba | 3229 | { |
926ce8bd | 3230 | tree pointer, type; |
c11b6f21 | 3231 | |
04757a2a JM |
3232 | /* RO_NULL should only be used with the folding entry points below, not |
3233 | cp_build_indirect_ref. */ | |
3234 | gcc_checking_assert (errorstring != RO_NULL || do_fold); | |
3235 | ||
374b2837 JM |
3236 | if (ptr == current_class_ptr |
3237 | || (TREE_CODE (ptr) == NOP_EXPR | |
3238 | && TREE_OPERAND (ptr, 0) == current_class_ptr | |
3239 | && (same_type_ignoring_top_level_qualifiers_p | |
3240 | (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr))))) | |
90e734a8 JM |
3241 | return current_class_ref; |
3242 | ||
9f613f06 | 3243 | pointer = (TYPE_REF_P (TREE_TYPE (ptr)) |
89fcabaf PC |
3244 | ? ptr : decay_conversion (ptr, complain)); |
3245 | if (pointer == error_mark_node) | |
3246 | return error_mark_node; | |
3247 | ||
c11b6f21 | 3248 | type = TREE_TYPE (pointer); |
8d08fdba | 3249 | |
71a93b08 | 3250 | if (INDIRECT_TYPE_P (type)) |
8d08fdba | 3251 | { |
01240200 | 3252 | /* [expr.unary.op] |
c8094d83 | 3253 | |
01240200 | 3254 | If the type of the expression is "pointer to T," the type |
cd41d410 DS |
3255 | of the result is "T." */ |
3256 | tree t = TREE_TYPE (type); | |
01240200 | 3257 | |
5ae98e82 RB |
3258 | if ((CONVERT_EXPR_P (ptr) |
3259 | || TREE_CODE (ptr) == VIEW_CONVERT_EXPR) | |
3260 | && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t))) | |
79bedddc SR |
3261 | { |
3262 | /* If a warning is issued, mark it to avoid duplicates from | |
3263 | the backend. This only needs to be done at | |
3264 | warn_strict_aliasing > 2. */ | |
3265 | if (warn_strict_aliasing > 2) | |
0444aa9c | 3266 | if (strict_aliasing_warning (EXPR_LOCATION (ptr), |
79bedddc SR |
3267 | type, TREE_OPERAND (ptr, 0))) |
3268 | TREE_NO_WARNING (ptr) = 1; | |
3269 | } | |
3270 | ||
b72801e2 | 3271 | if (VOID_TYPE_P (t)) |
0cbd7506 MS |
3272 | { |
3273 | /* A pointer to incomplete type (other than cv void) can be | |
3274 | dereferenced [expr.unary.op]/1 */ | |
5ade1ed2 DG |
3275 | if (complain & tf_error) |
3276 | error ("%qT is not a pointer-to-object type", type); | |
0cbd7506 MS |
3277 | return error_mark_node; |
3278 | } | |
04757a2a | 3279 | else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR |
688f6688 | 3280 | && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0)))) |
01240200 MM |
3281 | /* The POINTER was something like `&x'. We simplify `*&x' to |
3282 | `x'. */ | |
8d08fdba MS |
3283 | return TREE_OPERAND (pointer, 0); |
3284 | else | |
3285 | { | |
01240200 | 3286 | tree ref = build1 (INDIRECT_REF, t, pointer); |
8d08fdba | 3287 | |
b0d75c1e BK |
3288 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, |
3289 | so that we get the proper error message if the result is used | |
3290 | to assign to. Also, &* is supposed to be a no-op. */ | |
91063b51 MM |
3291 | TREE_READONLY (ref) = CP_TYPE_CONST_P (t); |
3292 | TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t); | |
8d08fdba | 3293 | TREE_SIDE_EFFECTS (ref) |
271bd540 | 3294 | = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)); |
8d08fdba MS |
3295 | return ref; |
3296 | } | |
3297 | } | |
5ade1ed2 DG |
3298 | else if (!(complain & tf_error)) |
3299 | /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */ | |
3300 | ; | |
8d08fdba MS |
3301 | /* `pointer' won't be an error_mark_node if we were given a |
3302 | pointer to member, so it's cool to check for this here. */ | |
66b1156a | 3303 | else if (TYPE_PTRMEM_P (type)) |
dd865ef6 SZ |
3304 | switch (errorstring) |
3305 | { | |
3306 | case RO_ARRAY_INDEXING: | |
3307 | error ("invalid use of array indexing on pointer to member"); | |
3308 | break; | |
3309 | case RO_UNARY_STAR: | |
3310 | error ("invalid use of unary %<*%> on pointer to member"); | |
3311 | break; | |
3312 | case RO_IMPLICIT_CONVERSION: | |
3313 | error ("invalid use of implicit conversion on pointer to member"); | |
3314 | break; | |
5e54f81d MG |
3315 | case RO_ARROW_STAR: |
3316 | error ("left hand operand of %<->*%> must be a pointer to class, " | |
3317 | "but is a pointer to member of type %qT", type); | |
3318 | break; | |
dd865ef6 SZ |
3319 | default: |
3320 | gcc_unreachable (); | |
3321 | } | |
8d08fdba | 3322 | else if (pointer != error_mark_node) |
7a6daeb0 NF |
3323 | invalid_indirection_error (input_location, type, errorstring); |
3324 | ||
8d08fdba MS |
3325 | return error_mark_node; |
3326 | } | |
3327 | ||
04757a2a JM |
3328 | /* Entry point used by c-common, which expects folding. */ |
3329 | ||
3330 | tree | |
3331 | build_indirect_ref (location_t /*loc*/, | |
3332 | tree ptr, ref_operator errorstring) | |
3333 | { | |
3334 | return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true); | |
3335 | } | |
3336 | ||
3337 | /* Entry point used by internal indirection needs that don't correspond to any | |
3338 | syntactic construct. */ | |
3339 | ||
3340 | tree | |
3341 | cp_build_fold_indirect_ref (tree pointer) | |
3342 | { | |
3343 | return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true); | |
3344 | } | |
3345 | ||
3346 | /* Entry point used by indirection needs that correspond to some syntactic | |
3347 | construct. */ | |
3348 | ||
3349 | tree | |
3350 | cp_build_indirect_ref (tree ptr, ref_operator errorstring, | |
3351 | tsubst_flags_t complain) | |
3352 | { | |
3353 | return cp_build_indirect_ref_1 (ptr, errorstring, complain, false); | |
3354 | } | |
3355 | ||
8d08fdba MS |
3356 | /* This handles expressions of the form "a[i]", which denotes |
3357 | an array reference. | |
3358 | ||
3359 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
3360 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
3361 | This avoids forcing the array out of registers, and can work on | |
3362 | arrays that are not lvalues (for example, members of structures returned | |
3363 | by functions). | |
3364 | ||
3365 | If INDEX is of some user-defined type, it must be converted to | |
3366 | integer type. Otherwise, to make a compatible PLUS_EXPR, it | |
a63068b6 AH |
3367 | will inherit the type of the array, which will be some pointer type. |
3368 | ||
3369 | LOC is the location to use in building the array reference. */ | |
8d08fdba | 3370 | |
8d08fdba | 3371 | tree |
3a11c665 JM |
3372 | cp_build_array_ref (location_t loc, tree array, tree idx, |
3373 | tsubst_flags_t complain) | |
8d08fdba | 3374 | { |
a63068b6 AH |
3375 | tree ret; |
3376 | ||
8d08fdba MS |
3377 | if (idx == 0) |
3378 | { | |
73647d75 JM |
3379 | if (complain & tf_error) |
3380 | error_at (loc, "subscript missing in array reference"); | |
8d08fdba MS |
3381 | return error_mark_node; |
3382 | } | |
3383 | ||
3384 | if (TREE_TYPE (array) == error_mark_node | |
3385 | || TREE_TYPE (idx) == error_mark_node) | |
3386 | return error_mark_node; | |
3387 | ||
0450d74d RH |
3388 | /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference |
3389 | inside it. */ | |
3390 | switch (TREE_CODE (array)) | |
3391 | { | |
3392 | case COMPOUND_EXPR: | |
3393 | { | |
3a11c665 JM |
3394 | tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx, |
3395 | complain); | |
a63068b6 AH |
3396 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (value), |
3397 | TREE_OPERAND (array, 0), value); | |
3398 | SET_EXPR_LOCATION (ret, loc); | |
3399 | return ret; | |
0450d74d RH |
3400 | } |
3401 | ||
3402 | case COND_EXPR: | |
a63068b6 | 3403 | ret = build_conditional_expr |
4cbc4bd7 | 3404 | (loc, TREE_OPERAND (array, 0), |
3a11c665 JM |
3405 | cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx, |
3406 | complain), | |
3407 | cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx, | |
3408 | complain), | |
a3299120 | 3409 | complain); |
8aff539b | 3410 | protected_set_expr_location (ret, loc); |
a63068b6 | 3411 | return ret; |
0450d74d RH |
3412 | |
3413 | default: | |
3414 | break; | |
3415 | } | |
3416 | ||
f17a223d | 3417 | bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx); |
7edaa4d2 | 3418 | |
6de9cd9a | 3419 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) |
8d08fdba | 3420 | { |
e4d35515 | 3421 | tree rval, type; |
8d08fdba | 3422 | |
5bd012f8 | 3423 | warn_array_subscript_with_type_char (loc, idx); |
8d08fdba | 3424 | |
adf2edec | 3425 | if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx))) |
8d08fdba | 3426 | { |
73647d75 JM |
3427 | if (complain & tf_error) |
3428 | error_at (loc, "array subscript is not an integer"); | |
8d08fdba MS |
3429 | return error_mark_node; |
3430 | } | |
3431 | ||
0a72704b MM |
3432 | /* Apply integral promotions *after* noticing character types. |
3433 | (It is unclear why we do these promotions -- the standard | |
6ccf2f7d | 3434 | does not say that we should. In fact, the natural thing would |
0a72704b MM |
3435 | seem to be to convert IDX to ptrdiff_t; we're performing |
3436 | pointer arithmetic.) */ | |
4b978f96 | 3437 | idx = cp_perform_integral_promotions (idx, complain); |
0a72704b | 3438 | |
dfd7fdca DM |
3439 | idx = maybe_constant_value (idx); |
3440 | ||
8d08fdba MS |
3441 | /* An array that is indexed by a non-constant |
3442 | cannot be stored in a register; we must be able to do | |
3443 | address arithmetic on its address. | |
3444 | Likewise an array of elements of variable size. */ | |
3445 | if (TREE_CODE (idx) != INTEGER_CST | |
d0f062fb | 3446 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
3c215895 JM |
3447 | && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) |
3448 | != INTEGER_CST))) | |
8d08fdba | 3449 | { |
a9e4a1a5 | 3450 | if (!cxx_mark_addressable (array, true)) |
8d08fdba MS |
3451 | return error_mark_node; |
3452 | } | |
0450d74d | 3453 | |
8d08fdba MS |
3454 | /* An array that is indexed by a constant value which is not within |
3455 | the array bounds cannot be stored in a register either; because we | |
3456 | would get a crash in store_bit_field/extract_bit_field when trying | |
3457 | to access a non-existent part of the register. */ | |
3458 | if (TREE_CODE (idx) == INTEGER_CST | |
eb34af89 RK |
3459 | && TYPE_DOMAIN (TREE_TYPE (array)) |
3460 | && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array)))) | |
8d08fdba | 3461 | { |
dffd7eb6 | 3462 | if (!cxx_mark_addressable (array)) |
8d08fdba MS |
3463 | return error_mark_node; |
3464 | } | |
3465 | ||
b7484fbe MS |
3466 | /* Note in C++ it is valid to subscript a `register' array, since |
3467 | it is valid to take the address of something with that | |
3468 | storage specification. */ | |
3469 | if (extra_warnings) | |
8d08fdba MS |
3470 | { |
3471 | tree foo = array; | |
3472 | while (TREE_CODE (foo) == COMPONENT_REF) | |
3473 | foo = TREE_OPERAND (foo, 0); | |
5a6ccc94 | 3474 | if (VAR_P (foo) && DECL_REGISTER (foo) |
73647d75 | 3475 | && (complain & tf_warning)) |
a63068b6 AH |
3476 | warning_at (loc, OPT_Wextra, |
3477 | "subscripting array declared %<register%>"); | |
8d08fdba MS |
3478 | } |
3479 | ||
01240200 | 3480 | type = TREE_TYPE (TREE_TYPE (array)); |
f293ce4b | 3481 | rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE); |
8d08fdba MS |
3482 | /* Array ref is const/volatile if the array elements are |
3483 | or if the array is.. */ | |
3484 | TREE_READONLY (rval) | |
91063b51 | 3485 | |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array)); |
8d08fdba | 3486 | TREE_SIDE_EFFECTS (rval) |
91063b51 | 3487 | |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array)); |
8d08fdba | 3488 | TREE_THIS_VOLATILE (rval) |
91063b51 | 3489 | |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array)); |
cda0a029 | 3490 | ret = require_complete_type_sfinae (rval, complain); |
8aff539b | 3491 | protected_set_expr_location (ret, loc); |
aa7da51a JJ |
3492 | if (non_lvalue) |
3493 | ret = non_lvalue_loc (loc, ret); | |
a63068b6 | 3494 | return ret; |
8d08fdba MS |
3495 | } |
3496 | ||
3497 | { | |
89fcabaf PC |
3498 | tree ar = cp_default_conversion (array, complain); |
3499 | tree ind = cp_default_conversion (idx, complain); | |
8d08fdba MS |
3500 | |
3501 | /* Put the integer in IND to simplify error checking. */ | |
3502 | if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) | |
6b4db501 | 3503 | std::swap (ar, ind); |
8d08fdba | 3504 | |
89fcabaf PC |
3505 | if (ar == error_mark_node || ind == error_mark_node) |
3506 | return error_mark_node; | |
8d08fdba | 3507 | |
50e10fa8 | 3508 | if (!TYPE_PTR_P (TREE_TYPE (ar))) |
8d08fdba | 3509 | { |
73647d75 JM |
3510 | if (complain & tf_error) |
3511 | error_at (loc, "subscripted value is neither array nor pointer"); | |
8d08fdba MS |
3512 | return error_mark_node; |
3513 | } | |
3514 | if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) | |
3515 | { | |
73647d75 JM |
3516 | if (complain & tf_error) |
3517 | error_at (loc, "array subscript is not an integer"); | |
8d08fdba MS |
3518 | return error_mark_node; |
3519 | } | |
3520 | ||
5bd012f8 | 3521 | warn_array_subscript_with_type_char (loc, idx); |
0958b0d3 | 3522 | |
ba47d38d AH |
3523 | ret = cp_build_indirect_ref (cp_build_binary_op (input_location, |
3524 | PLUS_EXPR, ar, ind, | |
73647d75 | 3525 | complain), |
dd865ef6 | 3526 | RO_ARRAY_INDEXING, |
73647d75 | 3527 | complain); |
a63068b6 | 3528 | protected_set_expr_location (ret, loc); |
aa7da51a JJ |
3529 | if (non_lvalue) |
3530 | ret = non_lvalue_loc (loc, ret); | |
a63068b6 | 3531 | return ret; |
8d08fdba MS |
3532 | } |
3533 | } | |
3a11c665 JM |
3534 | |
3535 | /* Entry point for Obj-C++. */ | |
3536 | ||
3537 | tree | |
3538 | build_array_ref (location_t loc, tree array, tree idx) | |
3539 | { | |
3540 | return cp_build_array_ref (loc, array, idx, tf_warning_or_error); | |
3541 | } | |
8d08fdba | 3542 | \f |
8d08fdba | 3543 | /* Resolve a pointer to member function. INSTANCE is the object |
c87978aa JM |
3544 | instance to use, if the member points to a virtual member. |
3545 | ||
3546 | This used to avoid checking for virtual functions if basetype | |
3547 | has no virtual functions, according to an earlier ANSI draft. | |
3548 | With the final ISO C++ rules, such an optimization is | |
3549 | incorrect: A pointer to a derived member can be static_cast | |
3550 | to pointer-to-base-member, as long as the dynamic object | |
e18724aa JM |
3551 | later has the right member. So now we only do this optimization |
3552 | when we know the dynamic type of the object. */ | |
8d08fdba MS |
3553 | |
3554 | tree | |
89fcabaf PC |
3555 | get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function, |
3556 | tsubst_flags_t complain) | |
8d08fdba MS |
3557 | { |
3558 | if (TREE_CODE (function) == OFFSET_REF) | |
338d90b8 | 3559 | function = TREE_OPERAND (function, 1); |
8d08fdba MS |
3560 | |
3561 | if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function))) | |
3562 | { | |
e18724aa JM |
3563 | tree idx, delta, e1, e2, e3, vtbl; |
3564 | bool nonvirtual; | |
c87978aa | 3565 | tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)); |
e18724aa | 3566 | tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)); |
f30432d7 | 3567 | |
b7484fbe | 3568 | tree instance_ptr = *instance_ptrptr; |
1c83ea9f | 3569 | tree instance_save_expr = 0; |
c87978aa | 3570 | if (instance_ptr == error_mark_node) |
32facac8 | 3571 | { |
c87978aa JM |
3572 | if (TREE_CODE (function) == PTRMEM_CST) |
3573 | { | |
3574 | /* Extracting the function address from a pmf is only | |
3575 | allowed with -Wno-pmf-conversions. It only works for | |
c6002625 | 3576 | pmf constants. */ |
89fcabaf | 3577 | e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain); |
c87978aa JM |
3578 | e1 = convert (fntype, e1); |
3579 | return e1; | |
3580 | } | |
3581 | else | |
3582 | { | |
89fcabaf PC |
3583 | if (complain & tf_error) |
3584 | error ("object missing in use of %qE", function); | |
c87978aa JM |
3585 | return error_mark_node; |
3586 | } | |
32facac8 ML |
3587 | } |
3588 | ||
e18724aa JM |
3589 | /* True if we know that the dynamic type of the object doesn't have |
3590 | virtual functions, so we can assume the PFN field is a pointer. */ | |
3591 | nonvirtual = (COMPLETE_TYPE_P (basetype) | |
3592 | && !TYPE_POLYMORPHIC_P (basetype) | |
3593 | && resolves_to_fixed_type_p (instance_ptr, 0)); | |
3594 | ||
f9873232 JM |
3595 | /* If we don't really have an object (i.e. in an ill-formed |
3596 | conversion from PMF to pointer), we can't resolve virtual | |
3597 | functions anyway. */ | |
3598 | if (!nonvirtual && is_dummy_object (instance_ptr)) | |
3599 | nonvirtual = true; | |
3600 | ||
b7484fbe | 3601 | if (TREE_SIDE_EFFECTS (instance_ptr)) |
1c83ea9f | 3602 | instance_ptr = instance_save_expr = save_expr (instance_ptr); |
b7484fbe | 3603 | |
f30432d7 MS |
3604 | if (TREE_SIDE_EFFECTS (function)) |
3605 | function = save_expr (function); | |
3606 | ||
c87978aa | 3607 | /* Start by extracting all the information from the PMF itself. */ |
10b2bcdd | 3608 | e3 = pfn_from_ptrmemfunc (function); |
a298680c | 3609 | delta = delta_from_ptrmemfunc (function); |
85b7def6 | 3610 | idx = build1 (NOP_EXPR, vtable_index_type, e3); |
b5fbde92 | 3611 | switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) |
cb7fdde2 | 3612 | { |
ec043522 | 3613 | int flag_sanitize_save; |
cb7fdde2 | 3614 | case ptrmemfunc_vbit_in_pfn: |
ba47d38d AH |
3615 | e1 = cp_build_binary_op (input_location, |
3616 | BIT_AND_EXPR, idx, integer_one_node, | |
89fcabaf | 3617 | complain); |
ba47d38d AH |
3618 | idx = cp_build_binary_op (input_location, |
3619 | MINUS_EXPR, idx, integer_one_node, | |
89fcabaf PC |
3620 | complain); |
3621 | if (idx == error_mark_node) | |
3622 | return error_mark_node; | |
cb7fdde2 AO |
3623 | break; |
3624 | ||
3625 | case ptrmemfunc_vbit_in_delta: | |
ba47d38d AH |
3626 | e1 = cp_build_binary_op (input_location, |
3627 | BIT_AND_EXPR, delta, integer_one_node, | |
89fcabaf | 3628 | complain); |
ec043522 MP |
3629 | /* Don't instrument the RSHIFT_EXPR we're about to create because |
3630 | we're going to use DELTA number of times, and that wouldn't play | |
3631 | well with SAVE_EXPRs therein. */ | |
3632 | flag_sanitize_save = flag_sanitize; | |
3633 | flag_sanitize = 0; | |
ba47d38d AH |
3634 | delta = cp_build_binary_op (input_location, |
3635 | RSHIFT_EXPR, delta, integer_one_node, | |
89fcabaf | 3636 | complain); |
ec043522 | 3637 | flag_sanitize = flag_sanitize_save; |
89fcabaf PC |
3638 | if (delta == error_mark_node) |
3639 | return error_mark_node; | |
cb7fdde2 AO |
3640 | break; |
3641 | ||
3642 | default: | |
315fb5db | 3643 | gcc_unreachable (); |
cb7fdde2 | 3644 | } |
03b256e4 | 3645 | |
89fcabaf PC |
3646 | if (e1 == error_mark_node) |
3647 | return error_mark_node; | |
3648 | ||
81ae598b MM |
3649 | /* Convert down to the right base before using the instance. A |
3650 | special case is that in a pointer to member of class C, C may | |
3651 | be incomplete. In that case, the function will of course be | |
3652 | a member of C, and no conversion is required. In fact, | |
3653 | lookup_base will fail in that case, because incomplete | |
c8094d83 | 3654 | classes do not have BINFOs. */ |
c8094d83 | 3655 | if (!same_type_ignoring_top_level_qualifiers_p |
81ae598b MM |
3656 | (basetype, TREE_TYPE (TREE_TYPE (instance_ptr)))) |
3657 | { | |
3658 | basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)), | |
22854930 | 3659 | basetype, ba_check, NULL, complain); |
c8094d83 | 3660 | instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, |
89fcabaf | 3661 | 1, complain); |
81ae598b MM |
3662 | if (instance_ptr == error_mark_node) |
3663 | return error_mark_node; | |
3664 | } | |
c87978aa | 3665 | /* ...and then the delta in the PMF. */ |
5d49b6a7 | 3666 | instance_ptr = fold_build_pointer_plus (instance_ptr, delta); |
c87978aa JM |
3667 | |
3668 | /* Hand back the adjusted 'this' argument to our caller. */ | |
3669 | *instance_ptrptr = instance_ptr; | |
3670 | ||
e18724aa JM |
3671 | if (nonvirtual) |
3672 | /* Now just return the pointer. */ | |
3673 | return e3; | |
3674 | ||
c87978aa JM |
3675 | /* Next extract the vtable pointer from the object. */ |
3676 | vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node), | |
3677 | instance_ptr); | |
04757a2a | 3678 | vtbl = cp_build_fold_indirect_ref (vtbl); |
89fcabaf PC |
3679 | if (vtbl == error_mark_node) |
3680 | return error_mark_node; | |
3681 | ||
c87978aa | 3682 | /* Finally, extract the function pointer from the vtable. */ |
5d49b6a7 | 3683 | e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx); |
04757a2a | 3684 | e2 = cp_build_fold_indirect_ref (e2); |
89fcabaf PC |
3685 | if (e2 == error_mark_node) |
3686 | return error_mark_node; | |
f63ab951 | 3687 | TREE_CONSTANT (e2) = 1; |
c7e266a6 | 3688 | |
67231816 RH |
3689 | /* When using function descriptors, the address of the |
3690 | vtable entry is treated as a function pointer. */ | |
3691 | if (TARGET_VTABLE_USES_DESCRIPTORS) | |
3692 | e2 = build1 (NOP_EXPR, TREE_TYPE (e2), | |
89fcabaf | 3693 | cp_build_addr_expr (e2, complain)); |
67231816 | 3694 | |
a489b1f0 | 3695 | e2 = fold_convert (TREE_TYPE (e3), e2); |
4cbc4bd7 | 3696 | e1 = build_conditional_expr (input_location, e1, e2, e3, complain); |
89fcabaf PC |
3697 | if (e1 == error_mark_node) |
3698 | return error_mark_node; | |
c8094d83 | 3699 | |
03b256e4 ML |
3700 | /* Make sure this doesn't get evaluated first inside one of the |
3701 | branches of the COND_EXPR. */ | |
1c83ea9f | 3702 | if (instance_save_expr) |
f293ce4b RS |
3703 | e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1), |
3704 | instance_save_expr, e1); | |
03b256e4 | 3705 | |
f49422da | 3706 | function = e1; |
8d08fdba MS |
3707 | } |
3708 | return function; | |
3709 | } | |
3710 | ||
5ade1ed2 | 3711 | /* Used by the C-common bits. */ |
8d08fdba | 3712 | tree |
12308bc6 | 3713 | build_function_call (location_t /*loc*/, |
c2255bc4 | 3714 | tree function, tree params) |
5ade1ed2 DG |
3715 | { |
3716 | return cp_build_function_call (function, params, tf_warning_or_error); | |
3717 | } | |
3718 | ||
bbbbb16a ILT |
3719 | /* Used by the C-common bits. */ |
3720 | tree | |
81e5eca8 | 3721 | build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/, |
9771b263 DN |
3722 | tree function, vec<tree, va_gc> *params, |
3723 | vec<tree, va_gc> * /*origtypes*/) | |
bbbbb16a | 3724 | { |
9771b263 | 3725 | vec<tree, va_gc> *orig_params = params; |
c166b898 ILT |
3726 | tree ret = cp_build_function_call_vec (function, ¶ms, |
3727 | tf_warning_or_error); | |
bbbbb16a | 3728 | |
c166b898 ILT |
3729 | /* cp_build_function_call_vec can reallocate PARAMS by adding |
3730 | default arguments. That should never happen here. Verify | |
3731 | that. */ | |
3732 | gcc_assert (params == orig_params); | |
3733 | ||
3734 | return ret; | |
bbbbb16a ILT |
3735 | } |
3736 | ||
c166b898 ILT |
3737 | /* Build a function call using a tree list of arguments. */ |
3738 | ||
882de214 | 3739 | static tree |
5ade1ed2 | 3740 | cp_build_function_call (tree function, tree params, tsubst_flags_t complain) |
c166b898 | 3741 | { |
9771b263 | 3742 | vec<tree, va_gc> *vec; |
c166b898 ILT |
3743 | tree ret; |
3744 | ||
3745 | vec = make_tree_vector (); | |
3746 | for (; params != NULL_TREE; params = TREE_CHAIN (params)) | |
9771b263 | 3747 | vec_safe_push (vec, TREE_VALUE (params)); |
c166b898 ILT |
3748 | ret = cp_build_function_call_vec (function, &vec, complain); |
3749 | release_tree_vector (vec); | |
3750 | return ret; | |
3751 | } | |
3752 | ||
450f4293 NF |
3753 | /* Build a function call using varargs. */ |
3754 | ||
3755 | tree | |
3756 | cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...) | |
3757 | { | |
9771b263 | 3758 | vec<tree, va_gc> *vec; |
450f4293 NF |
3759 | va_list args; |
3760 | tree ret, t; | |
3761 | ||
3762 | vec = make_tree_vector (); | |
3763 | va_start (args, complain); | |
3764 | for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree)) | |
9771b263 | 3765 | vec_safe_push (vec, t); |
450f4293 NF |
3766 | va_end (args); |
3767 | ret = cp_build_function_call_vec (function, &vec, complain); | |
3768 | release_tree_vector (vec); | |
3769 | return ret; | |
3770 | } | |
3771 | ||
c166b898 ILT |
3772 | /* Build a function call using a vector of arguments. PARAMS may be |
3773 | NULL if there are no parameters. This changes the contents of | |
3774 | PARAMS. */ | |
3775 | ||
3776 | tree | |
9771b263 | 3777 | cp_build_function_call_vec (tree function, vec<tree, va_gc> **params, |
c166b898 | 3778 | tsubst_flags_t complain) |
8d08fdba | 3779 | { |
926ce8bd | 3780 | tree fntype, fndecl; |
8d08fdba | 3781 | int is_method; |
2e4cf9dc | 3782 | tree original = function; |
c166b898 | 3783 | int nargs; |
94a0dd7b SL |
3784 | tree *argarray; |
3785 | tree parm_types; | |
9771b263 | 3786 | vec<tree, va_gc> *allocated = NULL; |
c166b898 | 3787 | tree ret; |
8d08fdba | 3788 | |
e58a9aa1 ZL |
3789 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
3790 | expressions, like those used for ObjC messenger dispatches. */ | |
9771b263 DN |
3791 | if (params != NULL && !vec_safe_is_empty (*params)) |
3792 | function = objc_rewrite_function_call (function, (**params)[0]); | |
e58a9aa1 | 3793 | |
8d08fdba MS |
3794 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
3795 | Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */ | |
3796 | if (TREE_CODE (function) == NOP_EXPR | |
3797 | && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0))) | |
3798 | function = TREE_OPERAND (function, 0); | |
3799 | ||
3800 | if (TREE_CODE (function) == FUNCTION_DECL) | |
3801 | { | |
971e17ff AS |
3802 | /* If the function is a non-template member function |
3803 | or a non-template friend, then we need to check the | |
3804 | constraints. | |
3805 | ||
3806 | Note that if overload resolution failed with a single | |
3807 | candidate this function will be used to explicitly diagnose | |
3808 | the failure for the single call expression. The check is | |
3809 | technically redundant since we also would have failed in | |
3810 | add_function_candidate. */ | |
3811 | if (flag_concepts | |
3812 | && (complain & tf_error) | |
3813 | && !constraints_satisfied_p (function)) | |
3814 | { | |
097f82ec | 3815 | auto_diagnostic_group d; |
971e17ff AS |
3816 | error ("cannot call function %qD", function); |
3817 | location_t loc = DECL_SOURCE_LOCATION (function); | |
3818 | diagnose_constraints (loc, function, NULL_TREE); | |
3819 | return error_mark_node; | |
3820 | } | |
3821 | ||
9f635aba PC |
3822 | if (!mark_used (function, complain) && !(complain & tf_error)) |
3823 | return error_mark_node; | |
8d08fdba MS |
3824 | fndecl = function; |
3825 | ||
3826 | /* Convert anything with function type to a pointer-to-function. */ | |
aa0a659c PC |
3827 | if (DECL_MAIN_P (function)) |
3828 | { | |
3829 | if (complain & tf_error) | |
3830 | pedwarn (input_location, OPT_Wpedantic, | |
3831 | "ISO C++ forbids calling %<::main%> from within program"); | |
3832 | else | |
3833 | return error_mark_node; | |
3834 | } | |
89fcabaf | 3835 | function = build_addr_func (function, complain); |
8d08fdba MS |
3836 | } |
3837 | else | |
3838 | { | |
3839 | fndecl = NULL_TREE; | |
3840 | ||
89fcabaf | 3841 | function = build_addr_func (function, complain); |
8d08fdba MS |
3842 | } |
3843 | ||
4ac14744 MS |
3844 | if (function == error_mark_node) |
3845 | return error_mark_node; | |
3846 | ||
8d08fdba MS |
3847 | fntype = TREE_TYPE (function); |
3848 | ||
3849 | if (TYPE_PTRMEMFUNC_P (fntype)) | |
3850 | { | |
5ade1ed2 DG |
3851 | if (complain & tf_error) |
3852 | error ("must use %<.*%> or %<->*%> to call pointer-to-member " | |
7b98a725 JW |
3853 | "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>", |
3854 | original, original); | |
4ac14744 | 3855 | return error_mark_node; |
8d08fdba MS |
3856 | } |
3857 | ||
50e10fa8 | 3858 | is_method = (TYPE_PTR_P (fntype) |
8d08fdba MS |
3859 | && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE); |
3860 | ||
50e10fa8 | 3861 | if (!(TYPE_PTRFN_P (fntype) |
386b8a85 JM |
3862 | || is_method |
3863 | || TREE_CODE (function) == TEMPLATE_ID_EXPR)) | |
8d08fdba | 3864 | { |
5ade1ed2 | 3865 | if (complain & tf_error) |
ae52741c MLI |
3866 | { |
3867 | if (!flag_diagnostics_show_caret) | |
3868 | error_at (input_location, | |
3869 | "%qE cannot be used as a function", original); | |
3870 | else if (DECL_P (original)) | |
3871 | error_at (input_location, | |
3872 | "%qD cannot be used as a function", original); | |
3873 | else | |
3874 | error_at (input_location, | |
3875 | "expression cannot be used as a function"); | |
3876 | } | |
3877 | ||
8d08fdba MS |
3878 | return error_mark_node; |
3879 | } | |
3880 | ||
3881 | /* fntype now gets the type of function pointed to. */ | |
3882 | fntype = TREE_TYPE (fntype); | |
94a0dd7b SL |
3883 | parm_types = TYPE_ARG_TYPES (fntype); |
3884 | ||
c166b898 ILT |
3885 | if (params == NULL) |
3886 | { | |
3887 | allocated = make_tree_vector (); | |
3888 | params = &allocated; | |
3889 | } | |
3890 | ||
2ce86d2e BI |
3891 | nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL, |
3892 | complain); | |
94a0dd7b | 3893 | if (nargs < 0) |
125e6594 | 3894 | return error_mark_node; |
863adfc0 | 3895 | |
9771b263 | 3896 | argarray = (*params)->address (); |
c166b898 | 3897 | |
dd66b8e8 JM |
3898 | /* Check for errors in format strings and inappropriately |
3899 | null parameters. */ | |
4227c9ad | 3900 | bool warned_p = check_function_arguments (input_location, fndecl, fntype, |
2f687306 | 3901 | nargs, argarray, NULL); |
8d08fdba | 3902 | |
e2e03032 | 3903 | ret = build_cxx_call (function, nargs, argarray, complain); |
c166b898 | 3904 | |
0dba7960 JJ |
3905 | if (warned_p) |
3906 | { | |
3907 | tree c = extract_call_expr (ret); | |
3908 | if (TREE_CODE (c) == CALL_EXPR) | |
3909 | TREE_NO_WARNING (c) = 1; | |
3910 | } | |
3911 | ||
c166b898 ILT |
3912 | if (allocated != NULL) |
3913 | release_tree_vector (allocated); | |
3914 | ||
3915 | return ret; | |
8d08fdba | 3916 | } |
8d08fdba | 3917 | \f |
8cdea6ab | 3918 | /* Subroutine of convert_arguments. |
09bae928 | 3919 | Print an error message about a wrong number of arguments. */ |
8cdea6ab SZ |
3920 | |
3921 | static void | |
09bae928 | 3922 | error_args_num (location_t loc, tree fndecl, bool too_many_p) |
8cdea6ab SZ |
3923 | { |
3924 | if (fndecl) | |
3925 | { | |
3926 | if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE) | |
3927 | { | |
3928 | if (DECL_NAME (fndecl) == NULL_TREE | |
3929 | || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl))) | |
3930 | error_at (loc, | |
3931 | too_many_p | |
3932 | ? G_("too many arguments to constructor %q#D") | |
3933 | : G_("too few arguments to constructor %q#D"), | |
3934 | fndecl); | |
3935 | else | |
3936 | error_at (loc, | |
3937 | too_many_p | |
3938 | ? G_("too many arguments to member function %q#D") | |
3939 | : G_("too few arguments to member function %q#D"), | |
3940 | fndecl); | |
3941 | } | |
3942 | else | |
3943 | error_at (loc, | |
3944 | too_many_p | |
3945 | ? G_("too many arguments to function %q#D") | |
3946 | : G_("too few arguments to function %q#D"), | |
3947 | fndecl); | |
6b6dd1aa MP |
3948 | if (!DECL_IS_BUILTIN (fndecl)) |
3949 | inform (DECL_SOURCE_LOCATION (fndecl), "declared here"); | |
8cdea6ab SZ |
3950 | } |
3951 | else | |
19dc6d01 NP |
3952 | { |
3953 | if (c_dialect_objc () && objc_message_selector ()) | |
3954 | error_at (loc, | |
3955 | too_many_p | |
3956 | ? G_("too many arguments to method %q#D") | |
3957 | : G_("too few arguments to method %q#D"), | |
3958 | objc_message_selector ()); | |
3959 | else | |
3960 | error_at (loc, too_many_p ? G_("too many arguments to function") | |
3961 | : G_("too few arguments to function")); | |
3962 | } | |
8cdea6ab SZ |
3963 | } |
3964 | ||
c166b898 ILT |
3965 | /* Convert the actual parameter expressions in the list VALUES to the |
3966 | types in the list TYPELIST. The converted expressions are stored | |
3967 | back in the VALUES vector. | |
8d08fdba MS |
3968 | If parmdecls is exhausted, or when an element has NULL as its type, |
3969 | perform the default conversions. | |
3970 | ||
8d08fdba MS |
3971 | NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. |
3972 | ||
3973 | This is also where warnings about wrong number of args are generated. | |
c8094d83 | 3974 | |
94a0dd7b | 3975 | Returns the actual number of arguments processed (which might be less |
c166b898 | 3976 | than the length of the vector), or -1 on error. |
8d08fdba MS |
3977 | |
3978 | In C++, unspecified trailing parameters can be filled in with their | |
3979 | default arguments, if such were specified. Do so here. */ | |
3980 | ||
94a0dd7b | 3981 | static int |
9771b263 | 3982 | convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl, |
c166b898 | 3983 | int flags, tsubst_flags_t complain) |
8d08fdba | 3984 | { |
c166b898 | 3985 | tree typetail; |
c166b898 | 3986 | unsigned int i; |
8d08fdba | 3987 | |
faf5394a MS |
3988 | /* Argument passing is always copy-initialization. */ |
3989 | flags |= LOOKUP_ONLYCONVERTING; | |
3990 | ||
c166b898 | 3991 | for (i = 0, typetail = typelist; |
9771b263 | 3992 | i < vec_safe_length (*values); |
c166b898 | 3993 | i++) |
8d08fdba | 3994 | { |
926ce8bd | 3995 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
9771b263 | 3996 | tree val = (**values)[i]; |
8d08fdba | 3997 | |
6af455d7 | 3998 | if (val == error_mark_node || type == error_mark_node) |
94a0dd7b | 3999 | return -1; |
8ccc31eb | 4000 | |
8d08fdba MS |
4001 | if (type == void_type_node) |
4002 | { | |
5ade1ed2 DG |
4003 | if (complain & tf_error) |
4004 | { | |
09bae928 | 4005 | error_args_num (input_location, fndecl, /*too_many_p=*/true); |
5ade1ed2 DG |
4006 | return i; |
4007 | } | |
4008 | else | |
4009 | return -1; | |
8d08fdba MS |
4010 | } |
4011 | ||
8d08fdba MS |
4012 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
4013 | Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */ | |
4014 | if (TREE_CODE (val) == NOP_EXPR | |
a0a33927 | 4015 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)) |
9f613f06 | 4016 | && (type == 0 || !TYPE_REF_P (type))) |
8d08fdba MS |
4017 | val = TREE_OPERAND (val, 0); |
4018 | ||
9f613f06 | 4019 | if (type == 0 || !TYPE_REF_P (type)) |
00595019 MS |
4020 | { |
4021 | if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE | |
8d08fdba | 4022 | || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE |
00595019 | 4023 | || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE) |
89fcabaf | 4024 | val = decay_conversion (val, complain); |
00595019 | 4025 | } |
8d08fdba MS |
4026 | |
4027 | if (val == error_mark_node) | |
94a0dd7b | 4028 | return -1; |
8d08fdba | 4029 | |
8d08fdba MS |
4030 | if (type != 0) |
4031 | { | |
4032 | /* Formal parm type is specified by a function prototype. */ | |
4033 | tree parmval; | |
4034 | ||
d0f062fb | 4035 | if (!COMPLETE_TYPE_P (complete_type (type))) |
8d08fdba | 4036 | { |
5ade1ed2 DG |
4037 | if (complain & tf_error) |
4038 | { | |
f11854d4 | 4039 | location_t loc = EXPR_LOC_OR_LOC (val, input_location); |
5ade1ed2 | 4040 | if (fndecl) |
f11854d4 DM |
4041 | { |
4042 | auto_diagnostic_group d; | |
4043 | error_at (loc, | |
4044 | "parameter %P of %qD has incomplete type %qT", | |
4045 | i, fndecl, type); | |
4046 | inform (get_fndecl_argument_location (fndecl, i), | |
4047 | " declared here"); | |
4048 | } | |
5ade1ed2 | 4049 | else |
f11854d4 DM |
4050 | error_at (loc, "parameter %P has incomplete type %qT", i, |
4051 | type); | |
5ade1ed2 | 4052 | } |
3afd2e20 | 4053 | parmval = error_mark_node; |
8d08fdba MS |
4054 | } |
4055 | else | |
4056 | { | |
9a3b49ac | 4057 | parmval = convert_for_initialization |
56ae6d77 | 4058 | (NULL_TREE, type, val, flags, |
2f5b91f5 | 4059 | ICR_ARGPASS, fndecl, i, complain); |
b40e334f | 4060 | parmval = convert_for_arg_passing (type, parmval, complain); |
8d08fdba | 4061 | } |
878cd289 MS |
4062 | |
4063 | if (parmval == error_mark_node) | |
94a0dd7b | 4064 | return -1; |
878cd289 | 4065 | |
9771b263 | 4066 | (**values)[i] = parmval; |
8d08fdba MS |
4067 | } |
4068 | else | |
4069 | { | |
1141ed3f | 4070 | if (fndecl && magic_varargs_p (fndecl)) |
4eaf1d5b | 4071 | /* Don't do ellipsis conversion for __built_in_constant_p |
c32097d8 | 4072 | as this will result in spurious errors for non-trivial |
4eaf1d5b | 4073 | types. */ |
79fe346e | 4074 | val = require_complete_type_sfinae (val, complain); |
4eaf1d5b | 4075 | else |
b40e334f | 4076 | val = convert_arg_to_ellipsis (val, complain); |
4eaf1d5b | 4077 | |
9771b263 | 4078 | (**values)[i] = val; |
8d08fdba MS |
4079 | } |
4080 | ||
8d08fdba MS |
4081 | if (typetail) |
4082 | typetail = TREE_CHAIN (typetail); | |
4083 | } | |
4084 | ||
4085 | if (typetail != 0 && typetail != void_list_node) | |
4086 | { | |
823c22f9 NS |
4087 | /* See if there are default arguments that can be used. Because |
4088 | we hold default arguments in the FUNCTION_TYPE (which is so | |
4089 | wrong), we can see default parameters here from deduced | |
4090 | contexts (and via typeof) for indirect function calls. | |
4091 | Fortunately we know whether we have a function decl to | |
4092 | provide default arguments in a language conformant | |
4093 | manner. */ | |
4094 | if (fndecl && TREE_PURPOSE (typetail) | |
a723baf1 | 4095 | && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG) |
8d08fdba | 4096 | { |
f376e137 | 4097 | for (; typetail != void_list_node; ++i) |
8d08fdba | 4098 | { |
559f2bbc JM |
4099 | /* After DR777, with explicit template args we can end up with a |
4100 | default argument followed by no default argument. */ | |
4101 | if (!TREE_PURPOSE (typetail)) | |
4102 | break; | |
c8094d83 MS |
4103 | tree parmval |
4104 | = convert_default_arg (TREE_VALUE (typetail), | |
4105 | TREE_PURPOSE (typetail), | |
b40e334f | 4106 | fndecl, i, complain); |
8d08fdba | 4107 | |
878cd289 | 4108 | if (parmval == error_mark_node) |
94a0dd7b | 4109 | return -1; |
878cd289 | 4110 | |
9771b263 | 4111 | vec_safe_push (*values, parmval); |
8d08fdba MS |
4112 | typetail = TREE_CHAIN (typetail); |
4113 | /* ends with `...'. */ | |
4114 | if (typetail == NULL_TREE) | |
4115 | break; | |
4116 | } | |
4117 | } | |
559f2bbc JM |
4118 | |
4119 | if (typetail && typetail != void_list_node) | |
8d08fdba | 4120 | { |
559f2bbc | 4121 | if (complain & tf_error) |
09bae928 | 4122 | error_args_num (input_location, fndecl, /*too_many_p=*/false); |
94a0dd7b | 4123 | return -1; |
8d08fdba MS |
4124 | } |
4125 | } | |
8d08fdba | 4126 | |
c166b898 | 4127 | return (int) i; |
8d08fdba MS |
4128 | } |
4129 | \f | |
4130 | /* Build a binary-operation expression, after performing default | |
2a67bec2 ILT |
4131 | conversions on the operands. CODE is the kind of expression to |
4132 | build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE | |
4133 | are the tree codes which correspond to ARG1 and ARG2 when issuing | |
4134 | warnings about possibly misplaced parentheses. They may differ | |
4135 | from the TREE_CODE of ARG1 and ARG2 if the parser has done constant | |
4136 | folding (e.g., if the parser sees "a | 1 + 1", it may call this | |
4137 | routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR). | |
4138 | To avoid issuing any parentheses warnings, pass ARG1_CODE and/or | |
4139 | ARG2_CODE as ERROR_MARK. */ | |
8d08fdba MS |
4140 | |
4141 | tree | |
a14feb3c | 4142 | build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1, |
4fe977f2 | 4143 | enum tree_code arg1_code, tree arg2, |
fcb9363e | 4144 | enum tree_code arg2_code, tree *overload_p, |
5ade1ed2 | 4145 | tsubst_flags_t complain) |
8d08fdba | 4146 | { |
d17811fd MM |
4147 | tree orig_arg1; |
4148 | tree orig_arg2; | |
4149 | tree expr; | |
fcb9363e | 4150 | tree overload = NULL_TREE; |
5566b478 | 4151 | |
d17811fd MM |
4152 | orig_arg1 = arg1; |
4153 | orig_arg2 = arg2; | |
d6b4ea85 | 4154 | |
d17811fd | 4155 | if (processing_template_decl) |
14d22dd6 | 4156 | { |
d17811fd MM |
4157 | if (type_dependent_expression_p (arg1) |
4158 | || type_dependent_expression_p (arg2)) | |
bddee796 JM |
4159 | { |
4160 | expr = build_min_nt_loc (loc, code, arg1, arg2); | |
4161 | maybe_save_operator_binding (expr); | |
4162 | return expr; | |
4163 | } | |
d17811fd MM |
4164 | arg1 = build_non_dependent_expr (arg1); |
4165 | arg2 = build_non_dependent_expr (arg2); | |
4166 | } | |
14d22dd6 | 4167 | |
d17811fd | 4168 | if (code == DOTSTAR_EXPR) |
89fcabaf | 4169 | expr = build_m_component_ref (arg1, arg2, complain); |
d17811fd | 4170 | else |
4fe977f2 | 4171 | expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE, |
fcb9363e PP |
4172 | &overload, complain); |
4173 | ||
4174 | if (overload_p != NULL) | |
4175 | *overload_p = overload; | |
14d22dd6 | 4176 | |
2a67bec2 ILT |
4177 | /* Check for cases such as x+y<<z which users are likely to |
4178 | misinterpret. But don't warn about obj << x + y, since that is a | |
4179 | common idiom for I/O. */ | |
4180 | if (warn_parentheses | |
aa9b7569 | 4181 | && (complain & tf_warning) |
2a67bec2 ILT |
4182 | && !processing_template_decl |
4183 | && !error_operand_p (arg1) | |
4184 | && !error_operand_p (arg2) | |
4185 | && (code != LSHIFT_EXPR | |
4186 | || !CLASS_TYPE_P (TREE_TYPE (arg1)))) | |
5d9de0d0 PC |
4187 | warn_about_parentheses (loc, code, arg1_code, orig_arg1, |
4188 | arg2_code, orig_arg2); | |
2a67bec2 | 4189 | |
d17811fd | 4190 | if (processing_template_decl && expr != error_mark_node) |
fcb9363e PP |
4191 | { |
4192 | if (overload != NULL_TREE) | |
4193 | return (build_min_non_dep_op_overload | |
4194 | (code, expr, overload, orig_arg1, orig_arg2)); | |
4195 | ||
4196 | return build_min_non_dep (code, expr, orig_arg1, orig_arg2); | |
4197 | } | |
c8094d83 | 4198 | |
d17811fd | 4199 | return expr; |
14d22dd6 MM |
4200 | } |
4201 | ||
493e377c PC |
4202 | /* Build and return an ARRAY_REF expression. */ |
4203 | ||
4204 | tree | |
f330f599 PC |
4205 | build_x_array_ref (location_t loc, tree arg1, tree arg2, |
4206 | tsubst_flags_t complain) | |
493e377c PC |
4207 | { |
4208 | tree orig_arg1 = arg1; | |
4209 | tree orig_arg2 = arg2; | |
4210 | tree expr; | |
fcb9363e | 4211 | tree overload = NULL_TREE; |
493e377c PC |
4212 | |
4213 | if (processing_template_decl) | |
4214 | { | |
4215 | if (type_dependent_expression_p (arg1) | |
4216 | || type_dependent_expression_p (arg2)) | |
f330f599 PC |
4217 | return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2, |
4218 | NULL_TREE, NULL_TREE); | |
493e377c PC |
4219 | arg1 = build_non_dependent_expr (arg1); |
4220 | arg2 = build_non_dependent_expr (arg2); | |
4221 | } | |
4222 | ||
f330f599 | 4223 | expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, |
fcb9363e | 4224 | NULL_TREE, &overload, complain); |
493e377c PC |
4225 | |
4226 | if (processing_template_decl && expr != error_mark_node) | |
fcb9363e PP |
4227 | { |
4228 | if (overload != NULL_TREE) | |
4229 | return (build_min_non_dep_op_overload | |
4230 | (ARRAY_REF, expr, overload, orig_arg1, orig_arg2)); | |
4231 | ||
4232 | return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2, | |
4233 | NULL_TREE, NULL_TREE); | |
4234 | } | |
493e377c PC |
4235 | return expr; |
4236 | } | |
4237 | ||
12448f77 ILT |
4238 | /* Return whether OP is an expression of enum type cast to integer |
4239 | type. In C++ even unsigned enum types are cast to signed integer | |
4240 | types. We do not want to issue warnings about comparisons between | |
4241 | signed and unsigned types when one of the types is an enum type. | |
4242 | Those warnings are always false positives in practice. */ | |
4243 | ||
4244 | static bool | |
4245 | enum_cast_to_int (tree op) | |
4246 | { | |
f764304c | 4247 | if (CONVERT_EXPR_P (op) |
12448f77 ILT |
4248 | && TREE_TYPE (op) == integer_type_node |
4249 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE | |
4250 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0)))) | |
4251 | return true; | |
4252 | ||
4253 | /* The cast may have been pushed into a COND_EXPR. */ | |
4254 | if (TREE_CODE (op) == COND_EXPR) | |
4255 | return (enum_cast_to_int (TREE_OPERAND (op, 1)) | |
4256 | || enum_cast_to_int (TREE_OPERAND (op, 2))); | |
4257 | ||
4258 | return false; | |
4259 | } | |
4260 | ||
5ade1ed2 DG |
4261 | /* For the c-common bits. */ |
4262 | tree | |
ba47d38d | 4263 | build_binary_op (location_t location, enum tree_code code, tree op0, tree op1, |
30af3a2b | 4264 | bool /*convert_p*/) |
5ade1ed2 | 4265 | { |
ba47d38d | 4266 | return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error); |
5ade1ed2 DG |
4267 | } |
4268 | ||
9f47c7e5 IE |
4269 | /* Build a vector comparison of ARG0 and ARG1 using CODE opcode |
4270 | into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */ | |
4271 | ||
4272 | static tree | |
4273 | build_vec_cmp (tree_code code, tree type, | |
4274 | tree arg0, tree arg1) | |
4275 | { | |
4276 | tree zero_vec = build_zero_cst (type); | |
4277 | tree minus_one_vec = build_minus_one_cst (type); | |
4278 | tree cmp_type = build_same_sized_truth_vector_type(type); | |
4279 | tree cmp = build2 (code, cmp_type, arg0, arg1); | |
9f47c7e5 IE |
4280 | return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec); |
4281 | } | |
5ade1ed2 | 4282 | |
b1970801 MP |
4283 | /* Possibly warn about an address never being NULL. */ |
4284 | ||
4285 | static void | |
4286 | warn_for_null_address (location_t location, tree op, tsubst_flags_t complain) | |
4287 | { | |
4288 | if (!warn_address | |
4289 | || (complain & tf_warning) == 0 | |
4290 | || c_inhibit_evaluation_warnings != 0 | |
4291 | || TREE_NO_WARNING (op)) | |
4292 | return; | |
4293 | ||
e56f6629 | 4294 | tree cop = fold_non_dependent_expr (op, complain); |
b1970801 MP |
4295 | |
4296 | if (TREE_CODE (cop) == ADDR_EXPR | |
4297 | && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0)) | |
4298 | && !TREE_NO_WARNING (cop)) | |
4299 | warning_at (location, OPT_Waddress, "the address of %qD will never " | |
4300 | "be NULL", TREE_OPERAND (cop, 0)); | |
4301 | ||
4302 | if (CONVERT_EXPR_P (op) | |
9f613f06 | 4303 | && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0)))) |
b1970801 MP |
4304 | { |
4305 | tree inner_op = op; | |
4306 | STRIP_NOPS (inner_op); | |
4307 | ||
4308 | if (DECL_P (inner_op)) | |
4309 | warning_at (location, OPT_Waddress, | |
4310 | "the compiler can assume that the address of " | |
4311 | "%qD will never be NULL", inner_op); | |
4312 | } | |
4313 | } | |
4314 | ||
8d08fdba MS |
4315 | /* Build a binary-operation expression without default conversions. |
4316 | CODE is the kind of expression to build. | |
ba47d38d | 4317 | LOCATION is the location_t of the operator in the source code. |
8d08fdba MS |
4318 | This function differs from `build' in several ways: |
4319 | the data type of the result is computed and recorded in it, | |
4320 | warnings are generated if arg data types are invalid, | |
4321 | special handling for addition and subtraction of pointers is known, | |
4322 | and some optimization is done (operations on narrow ints | |
4323 | are done in the narrower type when that gives the same result). | |
4324 | Constant folding is also done before the result is returned. | |
4325 | ||
8d08fdba MS |
4326 | Note that the operands will never have enumeral types |
4327 | because either they have just had the default conversions performed | |
4328 | or they have both just been converted to some other type in which | |
4329 | the arithmetic is to be done. | |
4330 | ||
4331 | C++: must do special pointer arithmetic when implementing | |
4332 | multiple inheritance, and deal with pointer to member functions. */ | |
4333 | ||
4334 | tree | |
a14feb3c | 4335 | cp_build_binary_op (const op_location_t &location, |
ba47d38d | 4336 | enum tree_code code, tree orig_op0, tree orig_op1, |
5ade1ed2 | 4337 | tsubst_flags_t complain) |
8d08fdba | 4338 | { |
39211cd5 | 4339 | tree op0, op1; |
926ce8bd | 4340 | enum tree_code code0, code1; |
39211cd5 | 4341 | tree type0, type1; |
4de67c26 | 4342 | const char *invalid_op_diag; |
8d08fdba MS |
4343 | |
4344 | /* Expression code to give to the expression when it is built. | |
4345 | Normally this is CODE, which is what the caller asked for, | |
4346 | but in some special cases we change it. */ | |
926ce8bd | 4347 | enum tree_code resultcode = code; |
8d08fdba MS |
4348 | |
4349 | /* Data type in which the computation is to be performed. | |
4350 | In the simplest cases this is the common type of the arguments. */ | |
98a7a34e | 4351 | tree result_type = NULL_TREE; |
8d08fdba MS |
4352 | |
4353 | /* Nonzero means operands have already been type-converted | |
4354 | in whatever way is necessary. | |
4355 | Zero means they need to be converted to RESULT_TYPE. */ | |
4356 | int converted = 0; | |
4357 | ||
28cbf42c MS |
4358 | /* Nonzero means create the expression with this type, rather than |
4359 | RESULT_TYPE. */ | |
4360 | tree build_type = 0; | |
4361 | ||
8d08fdba | 4362 | /* Nonzero means after finally constructing the expression |
faae18ab | 4363 | convert it to this type. */ |
8d08fdba MS |
4364 | tree final_type = 0; |
4365 | ||
cda0a029 | 4366 | tree result, result_ovl; |
455f19cb | 4367 | |
8d08fdba MS |
4368 | /* Nonzero if this is an operation like MIN or MAX which can |
4369 | safely be computed in short if both args are promoted shorts. | |
4370 | Also implies COMMON. | |
4371 | -1 indicates a bitwise operation; this makes a difference | |
4372 | in the exact conditions for when it is safe to do the operation | |
4373 | in a narrower mode. */ | |
4374 | int shorten = 0; | |
4375 | ||
4376 | /* Nonzero if this is a comparison operation; | |
4377 | if both args are promoted shorts, compare the original shorts. | |
4378 | Also implies COMMON. */ | |
4379 | int short_compare = 0; | |
4380 | ||
8d08fdba MS |
4381 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ |
4382 | int common = 0; | |
4383 | ||
455f19cb MM |
4384 | /* True if both operands have arithmetic type. */ |
4385 | bool arithmetic_types_p; | |
4386 | ||
39211cd5 | 4387 | /* Apply default conversions. */ |
40260429 NS |
4388 | op0 = orig_op0; |
4389 | op1 = orig_op1; | |
c8094d83 | 4390 | |
de5a5fa1 MP |
4391 | /* Remember whether we're doing / or %. */ |
4392 | bool doing_div_or_mod = false; | |
4393 | ||
4394 | /* Remember whether we're doing << or >>. */ | |
4395 | bool doing_shift = false; | |
4396 | ||
4397 | /* Tree holding instrumentation expression. */ | |
98a7a34e | 4398 | tree instrument_expr = NULL_TREE; |
de5a5fa1 | 4399 | |
a9aedbc2 MS |
4400 | if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR |
4401 | || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR | |
4402 | || code == TRUTH_XOR_EXPR) | |
4403 | { | |
a66e8081 | 4404 | if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0))) |
89fcabaf | 4405 | op0 = decay_conversion (op0, complain); |
a66e8081 | 4406 | if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1))) |
89fcabaf | 4407 | op1 = decay_conversion (op1, complain); |
a9aedbc2 MS |
4408 | } |
4409 | else | |
4410 | { | |
a66e8081 | 4411 | if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0))) |
89fcabaf | 4412 | op0 = cp_default_conversion (op0, complain); |
a66e8081 | 4413 | if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1))) |
89fcabaf | 4414 | op1 = cp_default_conversion (op1, complain); |
a9aedbc2 | 4415 | } |
39211cd5 | 4416 | |
a359be75 JM |
4417 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
4418 | STRIP_TYPE_NOPS (op0); | |
4419 | STRIP_TYPE_NOPS (op1); | |
4420 | ||
4421 | /* DTRT if one side is an overloaded function, but complain about it. */ | |
4422 | if (type_unknown_p (op0)) | |
4423 | { | |
c2ea3a40 | 4424 | tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none); |
a359be75 JM |
4425 | if (t != error_mark_node) |
4426 | { | |
5ade1ed2 | 4427 | if (complain & tf_error) |
cbe5f3b3 | 4428 | permerror (input_location, "assuming cast to type %qT from overloaded function", |
37ec60ed | 4429 | TREE_TYPE (t)); |
a359be75 JM |
4430 | op0 = t; |
4431 | } | |
4432 | } | |
4433 | if (type_unknown_p (op1)) | |
4434 | { | |
c2ea3a40 | 4435 | tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none); |
a359be75 JM |
4436 | if (t != error_mark_node) |
4437 | { | |
5ade1ed2 | 4438 | if (complain & tf_error) |
cbe5f3b3 | 4439 | permerror (input_location, "assuming cast to type %qT from overloaded function", |
37ec60ed | 4440 | TREE_TYPE (t)); |
a359be75 JM |
4441 | op1 = t; |
4442 | } | |
4443 | } | |
4444 | ||
1141ed3f BI |
4445 | type0 = TREE_TYPE (op0); |
4446 | type1 = TREE_TYPE (op1); | |
39211cd5 MS |
4447 | |
4448 | /* The expression codes of the data types of the arguments tell us | |
4449 | whether the arguments are integers, floating, pointers, etc. */ | |
4450 | code0 = TREE_CODE (type0); | |
4451 | code1 = TREE_CODE (type1); | |
4452 | ||
8d08fdba MS |
4453 | /* If an error was already reported for one of the arguments, |
4454 | avoid reporting another error. */ | |
8d08fdba MS |
4455 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
4456 | return error_mark_node; | |
4457 | ||
4de67c26 JM |
4458 | if ((invalid_op_diag |
4459 | = targetm.invalid_binary_op (code, type0, type1))) | |
4460 | { | |
76545796 PC |
4461 | if (complain & tf_error) |
4462 | error (invalid_op_diag); | |
4de67c26 JM |
4463 | return error_mark_node; |
4464 | } | |
4465 | ||
ef3ddd4f | 4466 | /* Issue warnings about peculiar, but valid, uses of NULL. */ |
9a004410 | 4467 | if ((null_node_p (orig_op0) || null_node_p (orig_op1)) |
ef3ddd4f MM |
4468 | /* It's reasonable to use pointer values as operands of && |
4469 | and ||, so NULL is no exception. */ | |
4470 | && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR | |
4471 | && ( /* Both are NULL (or 0) and the operation was not a | |
4472 | comparison or a pointer subtraction. */ | |
4473 | (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1) | |
4474 | && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR) | |
4475 | /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */ | |
4476 | || (!null_ptr_cst_p (orig_op0) | |
66b1156a | 4477 | && !TYPE_PTR_OR_PTRMEM_P (type0)) |
ef3ddd4f | 4478 | || (!null_ptr_cst_p (orig_op1) |
66b1156a | 4479 | && !TYPE_PTR_OR_PTRMEM_P (type1))) |
ef3ddd4f | 4480 | && (complain & tf_warning)) |
70dc395a | 4481 | { |
620e594b | 4482 | location_t loc = |
70dc395a DS |
4483 | expansion_point_location_if_in_system_header (input_location); |
4484 | ||
4485 | warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic"); | |
4486 | } | |
ef3ddd4f | 4487 | |
a212e43f MG |
4488 | /* In case when one of the operands of the binary operation is |
4489 | a vector and another is a scalar -- convert scalar to vector. */ | |
4490 | if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE)) | |
4491 | { | |
4492 | enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1, | |
4493 | complain & tf_error); | |
4494 | ||
4495 | switch (convert_flag) | |
4496 | { | |
4497 | case stv_error: | |
4498 | return error_mark_node; | |
4499 | case stv_firstarg: | |
4500 | { | |
4501 | op0 = convert (TREE_TYPE (type1), op0); | |
3c9aabbd | 4502 | op0 = save_expr (op0); |
a212e43f MG |
4503 | op0 = build_vector_from_val (type1, op0); |
4504 | type0 = TREE_TYPE (op0); | |
4505 | code0 = TREE_CODE (type0); | |
4506 | converted = 1; | |
4507 | break; | |
4508 | } | |
4509 | case stv_secondarg: | |
4510 | { | |
4511 | op1 = convert (TREE_TYPE (type0), op1); | |
3c9aabbd | 4512 | op1 = save_expr (op1); |
a212e43f MG |
4513 | op1 = build_vector_from_val (type0, op1); |
4514 | type1 = TREE_TYPE (op1); | |
4515 | code1 = TREE_CODE (type1); | |
4516 | converted = 1; | |
4517 | break; | |
4518 | } | |
4519 | default: | |
4520 | break; | |
4521 | } | |
4522 | } | |
4523 | ||
8d08fdba MS |
4524 | switch (code) |
4525 | { | |
8d08fdba MS |
4526 | case MINUS_EXPR: |
4527 | /* Subtraction of two similar pointers. | |
4528 | We must subtract them as integers, then divide by object size. */ | |
4529 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
a5ac359a MM |
4530 | && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0), |
4531 | TREE_TYPE (type1))) | |
05abad4c ML |
4532 | { |
4533 | result = pointer_diff (location, op0, op1, | |
4534 | common_pointer_type (type0, type1), complain, | |
4535 | &instrument_expr); | |
4536 | if (instrument_expr != NULL) | |
4537 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), | |
4538 | instrument_expr, result); | |
4539 | ||
4540 | return result; | |
4541 | } | |
0225a19d | 4542 | /* In all other cases except pointer - int, the usual arithmetic |
39a13be5 | 4543 | rules apply. */ |
0225a19d MM |
4544 | else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE)) |
4545 | { | |
4546 | common = 1; | |
4547 | break; | |
4548 | } | |
4549 | /* The pointer - int case is just like pointer + int; fall | |
4550 | through. */ | |
81fea426 | 4551 | gcc_fallthrough (); |
0225a19d MM |
4552 | case PLUS_EXPR: |
4553 | if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE) | |
4554 | && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE)) | |
4555 | { | |
4556 | tree ptr_operand; | |
4557 | tree int_operand; | |
4558 | ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1); | |
4559 | int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1); | |
4560 | if (processing_template_decl) | |
4561 | { | |
4562 | result_type = TREE_TYPE (ptr_operand); | |
4563 | break; | |
4564 | } | |
4f2e1536 MP |
4565 | return cp_pointer_int_sum (location, code, |
4566 | ptr_operand, | |
fd9b0f32 PC |
4567 | int_operand, |
4568 | complain); | |
0225a19d MM |
4569 | } |
4570 | common = 1; | |
8d08fdba MS |
4571 | break; |
4572 | ||
4573 | case MULT_EXPR: | |
4574 | common = 1; | |
4575 | break; | |
4576 | ||
4577 | case TRUNC_DIV_EXPR: | |
4578 | case CEIL_DIV_EXPR: | |
4579 | case FLOOR_DIV_EXPR: | |
4580 | case ROUND_DIV_EXPR: | |
4581 | case EXACT_DIV_EXPR: | |
40ffd95f BE |
4582 | if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR) |
4583 | { | |
4584 | tree type0 = TREE_OPERAND (op0, 0); | |
4585 | tree type1 = TREE_OPERAND (op1, 0); | |
4586 | tree first_arg = type0; | |
4587 | if (!TYPE_P (type0)) | |
4588 | type0 = TREE_TYPE (type0); | |
4589 | if (!TYPE_P (type1)) | |
4590 | type1 = TREE_TYPE (type1); | |
dfd7fdca | 4591 | if (INDIRECT_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)) |
097f82ec | 4592 | { |
dfd7fdca DM |
4593 | STRIP_ANY_LOCATION_WRAPPER (first_arg); |
4594 | if (!(TREE_CODE (first_arg) == PARM_DECL | |
4595 | && DECL_ARRAY_PARAMETER_P (first_arg) | |
4596 | && warn_sizeof_array_argument) | |
4597 | && (complain & tf_warning)) | |
4598 | { | |
4599 | auto_diagnostic_group d; | |
4600 | if (warning_at (location, OPT_Wsizeof_pointer_div, | |
4601 | "division %<sizeof (%T) / sizeof (%T)%> does " | |
4602 | "not compute the number of array elements", | |
4603 | type0, type1)) | |
4604 | if (DECL_P (first_arg)) | |
4605 | inform (DECL_SOURCE_LOCATION (first_arg), | |
4606 | "first %<sizeof%> operand was declared here"); | |
4607 | } | |
097f82ec | 4608 | } |
40ffd95f BE |
4609 | } |
4610 | ||
37c46b43 | 4611 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
3a021db2 | 4612 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
37c46b43 | 4613 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
3a021db2 | 4614 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
8d08fdba | 4615 | { |
01c15146 | 4616 | enum tree_code tcode0 = code0, tcode1 = code1; |
e56f6629 | 4617 | tree cop1 = fold_non_dependent_expr (op1, complain); |
f8ed5150 | 4618 | doing_div_or_mod = true; |
de5a5fa1 | 4619 | warn_for_div_by_zero (location, cop1); |
c8094d83 | 4620 | |
01c15146 VR |
4621 | if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE) |
4622 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); | |
4623 | if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE) | |
4624 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); | |
3a021db2 | 4625 | |
01c15146 | 4626 | if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)) |
8d08fdba MS |
4627 | resultcode = RDIV_EXPR; |
4628 | else | |
dfd7fdca DM |
4629 | { |
4630 | /* When dividing two signed integers, we have to promote to int. | |
4631 | unless we divide by a constant != -1. Note that default | |
4632 | conversion will have been performed on the operands at this | |
4633 | point, so we have to dig out the original type to find out if | |
4634 | it was unsigned. */ | |
4635 | tree stripped_op1 = tree_strip_any_location_wrapper (op1); | |
4636 | shorten = ((TREE_CODE (op0) == NOP_EXPR | |
4637 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) | |
4638 | || (TREE_CODE (stripped_op1) == INTEGER_CST | |
4639 | && ! integer_all_onesp (stripped_op1))); | |
4640 | } | |
05bccae2 | 4641 | |
8d08fdba MS |
4642 | common = 1; |
4643 | } | |
4644 | break; | |
4645 | ||
4646 | case BIT_AND_EXPR: | |
8d08fdba MS |
4647 | case BIT_IOR_EXPR: |
4648 | case BIT_XOR_EXPR: | |
73042643 | 4649 | if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
9ef0c8d9 AP |
4650 | || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
4651 | && !VECTOR_FLOAT_TYPE_P (type0) | |
4652 | && !VECTOR_FLOAT_TYPE_P (type1))) | |
8d08fdba | 4653 | shorten = -1; |
8d08fdba MS |
4654 | break; |
4655 | ||
4656 | case TRUNC_MOD_EXPR: | |
4657 | case FLOOR_MOD_EXPR: | |
f7e8cd25 | 4658 | { |
e56f6629 | 4659 | tree cop1 = fold_non_dependent_expr (op1, complain); |
f8ed5150 | 4660 | doing_div_or_mod = true; |
de5a5fa1 | 4661 | warn_for_div_by_zero (location, cop1); |
f7e8cd25 | 4662 | } |
c8094d83 | 4663 | |
5cfd5d9b AP |
4664 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
4665 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
4666 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
4667 | common = 1; | |
4668 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
8d08fdba MS |
4669 | { |
4670 | /* Although it would be tempting to shorten always here, that loses | |
4671 | on some targets, since the modulo instruction is undefined if the | |
4672 | quotient can't be represented in the computation mode. We shorten | |
4673 | only if unsigned or if dividing by something we know != -1. */ | |
dfd7fdca | 4674 | tree stripped_op1 = tree_strip_any_location_wrapper (op1); |
8d08fdba | 4675 | shorten = ((TREE_CODE (op0) == NOP_EXPR |
8df83eae | 4676 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) |
dfd7fdca DM |
4677 | || (TREE_CODE (stripped_op1) == INTEGER_CST |
4678 | && ! integer_all_onesp (stripped_op1))); | |
8d08fdba MS |
4679 | common = 1; |
4680 | } | |
4681 | break; | |
4682 | ||
4683 | case TRUTH_ANDIF_EXPR: | |
4684 | case TRUTH_ORIF_EXPR: | |
4685 | case TRUTH_AND_EXPR: | |
4686 | case TRUTH_OR_EXPR: | |
3c9aabbd | 4687 | if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1)) |
abb839ec | 4688 | { |
3c9aabbd MG |
4689 | if (!COMPARISON_CLASS_P (op1)) |
4690 | op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1, | |
4691 | build_zero_cst (type1), complain); | |
4692 | if (code == TRUTH_ANDIF_EXPR) | |
4693 | { | |
4694 | tree z = build_zero_cst (TREE_TYPE (op1)); | |
4695 | return build_conditional_expr (location, op0, op1, z, complain); | |
4696 | } | |
4697 | else if (code == TRUTH_ORIF_EXPR) | |
4698 | { | |
4699 | tree m1 = build_all_ones_cst (TREE_TYPE (op1)); | |
4700 | return build_conditional_expr (location, op0, m1, op1, complain); | |
4701 | } | |
4702 | else | |
4703 | gcc_unreachable (); | |
4704 | } | |
4705 | if (VECTOR_TYPE_P (type0)) | |
4706 | { | |
4707 | if (!COMPARISON_CLASS_P (op0)) | |
4708 | op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0, | |
4709 | build_zero_cst (type0), complain); | |
4710 | if (!VECTOR_TYPE_P (type1)) | |
4711 | { | |
4712 | tree m1 = build_all_ones_cst (TREE_TYPE (op0)); | |
4713 | tree z = build_zero_cst (TREE_TYPE (op0)); | |
efc3536f | 4714 | op1 = build_conditional_expr (location, op1, m1, z, complain); |
3c9aabbd MG |
4715 | } |
4716 | else if (!COMPARISON_CLASS_P (op1)) | |
4717 | op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1, | |
4718 | build_zero_cst (type1), complain); | |
4719 | ||
4720 | if (code == TRUTH_ANDIF_EXPR) | |
4721 | code = BIT_AND_EXPR; | |
4722 | else if (code == TRUTH_ORIF_EXPR) | |
4723 | code = BIT_IOR_EXPR; | |
4724 | else | |
4725 | gcc_unreachable (); | |
4726 | ||
4727 | return cp_build_binary_op (location, code, op0, op1, complain); | |
abb839ec | 4728 | } |
3c9aabbd | 4729 | |
255512c1 | 4730 | result_type = boolean_type_node; |
8d08fdba MS |
4731 | break; |
4732 | ||
4733 | /* Shift operations: result has same type as first operand; | |
4734 | always convert second operand to int. | |
4735 | Also set SHORT_SHIFT if shifting rightward. */ | |
4736 | ||
4737 | case RSHIFT_EXPR: | |
a212e43f MG |
4738 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
4739 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
4740 | { | |
4741 | result_type = type0; | |
4742 | converted = 1; | |
4743 | } | |
4744 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
928686b1 RS |
4745 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
4746 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
4747 | && known_eq (TYPE_VECTOR_SUBPARTS (type0), | |
4748 | TYPE_VECTOR_SUBPARTS (type1))) | |
31ed6226 MG |
4749 | { |
4750 | result_type = type0; | |
4751 | converted = 1; | |
4752 | } | |
4753 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
8d08fdba | 4754 | { |
e56f6629 | 4755 | tree const_op1 = fold_non_dependent_expr (op1, complain); |
cc83c823 JJ |
4756 | if (TREE_CODE (const_op1) != INTEGER_CST) |
4757 | const_op1 = op1; | |
8d08fdba | 4758 | result_type = type0; |
de5a5fa1 | 4759 | doing_shift = true; |
cc83c823 | 4760 | if (TREE_CODE (const_op1) == INTEGER_CST) |
8d08fdba | 4761 | { |
cc83c823 | 4762 | if (tree_int_cst_lt (const_op1, integer_zero_node)) |
5ade1ed2 | 4763 | { |
7d882b83 ILT |
4764 | if ((complain & tf_warning) |
4765 | && c_inhibit_evaluation_warnings == 0) | |
13c21655 PC |
4766 | warning (OPT_Wshift_count_negative, |
4767 | "right shift count is negative"); | |
5ade1ed2 | 4768 | } |
8d08fdba MS |
4769 | else |
4770 | { | |
cc83c823 | 4771 | if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0 |
7d882b83 ILT |
4772 | && (complain & tf_warning) |
4773 | && c_inhibit_evaluation_warnings == 0) | |
13c21655 PC |
4774 | warning (OPT_Wshift_count_overflow, |
4775 | "right shift count >= width of type"); | |
8d08fdba MS |
4776 | } |
4777 | } | |
8d08fdba MS |
4778 | /* Avoid converting op1 to result_type later. */ |
4779 | converted = 1; | |
4780 | } | |
4781 | break; | |
4782 | ||
4783 | case LSHIFT_EXPR: | |
a212e43f MG |
4784 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
4785 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
4786 | { | |
4787 | result_type = type0; | |
4788 | converted = 1; | |
4789 | } | |
4790 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
928686b1 RS |
4791 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
4792 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
4793 | && known_eq (TYPE_VECTOR_SUBPARTS (type0), | |
4794 | TYPE_VECTOR_SUBPARTS (type1))) | |
31ed6226 MG |
4795 | { |
4796 | result_type = type0; | |
4797 | converted = 1; | |
4798 | } | |
4799 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
8d08fdba | 4800 | { |
e56f6629 | 4801 | tree const_op0 = fold_non_dependent_expr (op0, complain); |
0173bd2a MP |
4802 | if (TREE_CODE (const_op0) != INTEGER_CST) |
4803 | const_op0 = op0; | |
e56f6629 | 4804 | tree const_op1 = fold_non_dependent_expr (op1, complain); |
cc83c823 JJ |
4805 | if (TREE_CODE (const_op1) != INTEGER_CST) |
4806 | const_op1 = op1; | |
8d08fdba | 4807 | result_type = type0; |
de5a5fa1 | 4808 | doing_shift = true; |
0173bd2a MP |
4809 | if (TREE_CODE (const_op0) == INTEGER_CST |
4810 | && tree_int_cst_sgn (const_op0) < 0 | |
4811 | && (complain & tf_warning) | |
4812 | && c_inhibit_evaluation_warnings == 0) | |
4813 | warning (OPT_Wshift_negative_value, | |
4814 | "left shift of negative value"); | |
cc83c823 | 4815 | if (TREE_CODE (const_op1) == INTEGER_CST) |
8d08fdba | 4816 | { |
cc83c823 | 4817 | if (tree_int_cst_lt (const_op1, integer_zero_node)) |
5ade1ed2 | 4818 | { |
7d882b83 ILT |
4819 | if ((complain & tf_warning) |
4820 | && c_inhibit_evaluation_warnings == 0) | |
13c21655 PC |
4821 | warning (OPT_Wshift_count_negative, |
4822 | "left shift count is negative"); | |
5ade1ed2 | 4823 | } |
cc83c823 JJ |
4824 | else if (compare_tree_int (const_op1, |
4825 | TYPE_PRECISION (type0)) >= 0) | |
5ade1ed2 | 4826 | { |
7d882b83 ILT |
4827 | if ((complain & tf_warning) |
4828 | && c_inhibit_evaluation_warnings == 0) | |
13c21655 PC |
4829 | warning (OPT_Wshift_count_overflow, |
4830 | "left shift count >= width of type"); | |
5ade1ed2 | 4831 | } |
451b5e48 MP |
4832 | else if (TREE_CODE (const_op0) == INTEGER_CST |
4833 | && (complain & tf_warning)) | |
4834 | maybe_warn_shift_overflow (location, const_op0, const_op1); | |
8d08fdba | 4835 | } |
8d08fdba MS |
4836 | /* Avoid converting op1 to result_type later. */ |
4837 | converted = 1; | |
4838 | } | |
4839 | break; | |
4840 | ||
4841 | case RROTATE_EXPR: | |
4842 | case LROTATE_EXPR: | |
4843 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
4844 | { | |
4845 | result_type = type0; | |
4846 | if (TREE_CODE (op1) == INTEGER_CST) | |
4847 | { | |
4848 | if (tree_int_cst_lt (op1, integer_zero_node)) | |
5ade1ed2 DG |
4849 | { |
4850 | if (complain & tf_warning) | |
4851 | warning (0, (code == LROTATE_EXPR) | |
4852 | ? G_("left rotate count is negative") | |
4853 | : G_("right rotate count is negative")); | |
4854 | } | |
665f2503 | 4855 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
5ade1ed2 DG |
4856 | { |
4857 | if (complain & tf_warning) | |
4858 | warning (0, (code == LROTATE_EXPR) | |
4859 | ? G_("left rotate count >= width of type") | |
4860 | : G_("right rotate count >= width of type")); | |
4861 | } | |
8d08fdba MS |
4862 | } |
4863 | /* Convert the shift-count to an integer, regardless of | |
4864 | size of value being shifted. */ | |
4865 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
4b978f96 | 4866 | op1 = cp_convert (integer_type_node, op1, complain); |
8d08fdba MS |
4867 | } |
4868 | break; | |
4869 | ||
4870 | case EQ_EXPR: | |
4871 | case NE_EXPR: | |
31ed6226 MG |
4872 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
4873 | goto vector_compare; | |
5ade1ed2 | 4874 | if ((complain & tf_warning) |
44b0c9ae | 4875 | && c_inhibit_evaluation_warnings == 0 |
5ade1ed2 | 4876 | && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))) |
3db45ab5 MS |
4877 | warning (OPT_Wfloat_equal, |
4878 | "comparing floating point with == or != is unsafe"); | |
dfd7fdca DM |
4879 | if (complain & tf_warning) |
4880 | { | |
4881 | tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0); | |
4882 | tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1); | |
4883 | if ((TREE_CODE (stripped_orig_op0) == STRING_CST | |
66756373 | 4884 | && !integer_zerop (cp_fully_fold (op1))) |
dfd7fdca DM |
4885 | || (TREE_CODE (stripped_orig_op1) == STRING_CST |
4886 | && !integer_zerop (cp_fully_fold (op0)))) | |
4887 | warning (OPT_Waddress, "comparison with string literal results " | |
4888 | "in unspecified behavior"); | |
4889 | } | |
1bdba2c0 | 4890 | |
c8094d83 | 4891 | build_type = boolean_type_node; |
37c46b43 | 4892 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
550a799d | 4893 | || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE) |
37c46b43 | 4894 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
550a799d | 4895 | || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)) |
8d08fdba | 4896 | short_compare = 1; |
81caef48 | 4897 | else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0)) |
f41f1ceb | 4898 | && null_ptr_cst_p (orig_op1)) |
81caef48 PC |
4899 | /* Handle, eg, (void*)0 (c++/43906), and more. */ |
4900 | || (code0 == POINTER_TYPE | |
4901 | && TYPE_PTR_P (type1) && integer_zerop (op1))) | |
b3c6d2ea | 4902 | { |
81caef48 PC |
4903 | if (TYPE_PTR_P (type1)) |
4904 | result_type = composite_pointer_type (type0, type1, op0, op1, | |
4905 | CPO_COMPARISON, complain); | |
4906 | else | |
4907 | result_type = type0; | |
4908 | ||
097f82ec DM |
4909 | if (char_type_p (TREE_TYPE (orig_op1))) |
4910 | { | |
4911 | auto_diagnostic_group d; | |
4912 | if (warning (OPT_Wpointer_compare, | |
4913 | "comparison between pointer and zero character " | |
4914 | "constant")) | |
4915 | inform (input_location, | |
4916 | "did you mean to dereference the pointer?"); | |
4917 | } | |
b1970801 | 4918 | warn_for_null_address (location, op0, complain); |
b3c6d2ea | 4919 | } |
81caef48 | 4920 | else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1)) |
f41f1ceb | 4921 | && null_ptr_cst_p (orig_op0)) |
81caef48 PC |
4922 | /* Handle, eg, (void*)0 (c++/43906), and more. */ |
4923 | || (code1 == POINTER_TYPE | |
4924 | && TYPE_PTR_P (type0) && integer_zerop (op0))) | |
b3c6d2ea | 4925 | { |
81caef48 PC |
4926 | if (TYPE_PTR_P (type0)) |
4927 | result_type = composite_pointer_type (type0, type1, op0, op1, | |
4928 | CPO_COMPARISON, complain); | |
4929 | else | |
4930 | result_type = type1; | |
4931 | ||
097f82ec DM |
4932 | if (char_type_p (TREE_TYPE (orig_op0))) |
4933 | { | |
4934 | auto_diagnostic_group d; | |
4935 | if (warning (OPT_Wpointer_compare, | |
4936 | "comparison between pointer and zero character " | |
4937 | "constant")) | |
4938 | inform (input_location, | |
4939 | "did you mean to dereference the pointer?"); | |
4940 | } | |
b1970801 | 4941 | warn_for_null_address (location, op1, complain); |
b3c6d2ea | 4942 | } |
81caef48 PC |
4943 | else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
4944 | || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1))) | |
4945 | result_type = composite_pointer_type (type0, type1, op0, op1, | |
4946 | CPO_COMPARISON, complain); | |
f41f1ceb | 4947 | else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)) |
14c2101d JM |
4948 | /* One of the operands must be of nullptr_t type. */ |
4949 | result_type = TREE_TYPE (nullptr_node); | |
8d08fdba MS |
4950 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
4951 | { | |
faae18ab | 4952 | result_type = type0; |
5ade1ed2 | 4953 | if (complain & tf_error) |
cbe5f3b3 | 4954 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
4955 | else |
4956 | return error_mark_node; | |
8d08fdba MS |
4957 | } |
4958 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
4959 | { | |
faae18ab | 4960 | result_type = type1; |
5ade1ed2 | 4961 | if (complain & tf_error) |
cbe5f3b3 | 4962 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
4963 | else |
4964 | return error_mark_node; | |
8d08fdba | 4965 | } |
f41f1ceb | 4966 | else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1)) |
8d08fdba | 4967 | { |
c3a88be8 RM |
4968 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION |
4969 | == ptrmemfunc_vbit_in_delta) | |
4970 | { | |
20265464 ZC |
4971 | tree pfn0, delta0, e1, e2; |
4972 | ||
4973 | if (TREE_SIDE_EFFECTS (op0)) | |
2426ee77 | 4974 | op0 = cp_save_expr (op0); |
20265464 ZC |
4975 | |
4976 | pfn0 = pfn_from_ptrmemfunc (op0); | |
4977 | delta0 = delta_from_ptrmemfunc (op0); | |
4978 | e1 = cp_build_binary_op (location, | |
4979 | EQ_EXPR, | |
4980 | pfn0, | |
4981 | build_zero_cst (TREE_TYPE (pfn0)), | |
4982 | complain); | |
4983 | e2 = cp_build_binary_op (location, | |
4984 | BIT_AND_EXPR, | |
4985 | delta0, | |
4986 | integer_one_node, | |
4987 | complain); | |
30b07d03 PC |
4988 | |
4989 | if (complain & tf_warning) | |
4990 | maybe_warn_zero_as_null_pointer_constant (op1, input_location); | |
51e355aa | 4991 | |
ba47d38d AH |
4992 | e2 = cp_build_binary_op (location, |
4993 | EQ_EXPR, e2, integer_zero_node, | |
5ade1ed2 | 4994 | complain); |
ba47d38d AH |
4995 | op0 = cp_build_binary_op (location, |
4996 | TRUTH_ANDIF_EXPR, e1, e2, | |
5ade1ed2 | 4997 | complain); |
cda0a029 | 4998 | op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain); |
c3a88be8 RM |
4999 | } |
5000 | else | |
5001 | { | |
5002 | op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier); | |
4b978f96 | 5003 | op1 = cp_convert (TREE_TYPE (op0), op1, complain); |
c3a88be8 | 5004 | } |
faae18ab | 5005 | result_type = TREE_TYPE (op0); |
8d08fdba | 5006 | } |
f41f1ceb | 5007 | else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0)) |
ba47d38d | 5008 | return cp_build_binary_op (location, code, op1, op0, complain); |
867f133e | 5009 | else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)) |
8d08fdba | 5010 | { |
867f133e | 5011 | tree type; |
c7e266a6 MM |
5012 | /* E will be the final comparison. */ |
5013 | tree e; | |
5014 | /* E1 and E2 are for scratch. */ | |
5015 | tree e1; | |
5016 | tree e2; | |
1f84ec23 MM |
5017 | tree pfn0; |
5018 | tree pfn1; | |
5019 | tree delta0; | |
5020 | tree delta1; | |
c7e266a6 | 5021 | |
c86818cf SZ |
5022 | type = composite_pointer_type (type0, type1, op0, op1, |
5023 | CPO_COMPARISON, complain); | |
867f133e OW |
5024 | |
5025 | if (!same_type_p (TREE_TYPE (op0), type)) | |
4b978f96 | 5026 | op0 = cp_convert_and_check (type, op0, complain); |
867f133e | 5027 | if (!same_type_p (TREE_TYPE (op1), type)) |
4b978f96 | 5028 | op1 = cp_convert_and_check (type, op1, complain); |
867f133e OW |
5029 | |
5030 | if (op0 == error_mark_node || op1 == error_mark_node) | |
5031 | return error_mark_node; | |
5032 | ||
ebb1abc3 JM |
5033 | if (TREE_SIDE_EFFECTS (op0)) |
5034 | op0 = save_expr (op0); | |
5035 | if (TREE_SIDE_EFFECTS (op1)) | |
5036 | op1 = save_expr (op1); | |
5037 | ||
1f84ec23 | 5038 | pfn0 = pfn_from_ptrmemfunc (op0); |
cda0a029 | 5039 | pfn0 = cp_fully_fold (pfn0); |
457d0ace PC |
5040 | /* Avoid -Waddress warnings (c++/64877). */ |
5041 | if (TREE_CODE (pfn0) == ADDR_EXPR) | |
5042 | TREE_NO_WARNING (pfn0) = 1; | |
1f84ec23 | 5043 | pfn1 = pfn_from_ptrmemfunc (op1); |
cda0a029 | 5044 | pfn1 = cp_fully_fold (pfn1); |
a298680c OW |
5045 | delta0 = delta_from_ptrmemfunc (op0); |
5046 | delta1 = delta_from_ptrmemfunc (op1); | |
c3a88be8 RM |
5047 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION |
5048 | == ptrmemfunc_vbit_in_delta) | |
5049 | { | |
5050 | /* We generate: | |
5051 | ||
5052 | (op0.pfn == op1.pfn | |
5053 | && ((op0.delta == op1.delta) | |
5054 | || (!op0.pfn && op0.delta & 1 == 0 | |
5055 | && op1.delta & 1 == 0)) | |
5056 | ||
5057 | The reason for the `!op0.pfn' bit is that a NULL | |
5058 | pointer-to-member is any member with a zero PFN and | |
5059 | LSB of the DELTA field is 0. */ | |
5060 | ||
ba47d38d | 5061 | e1 = cp_build_binary_op (location, BIT_AND_EXPR, |
c3a88be8 | 5062 | delta0, |
5ade1ed2 DG |
5063 | integer_one_node, |
5064 | complain); | |
ba47d38d AH |
5065 | e1 = cp_build_binary_op (location, |
5066 | EQ_EXPR, e1, integer_zero_node, | |
5ade1ed2 | 5067 | complain); |
ba47d38d | 5068 | e2 = cp_build_binary_op (location, BIT_AND_EXPR, |
c3a88be8 | 5069 | delta1, |
5ade1ed2 DG |
5070 | integer_one_node, |
5071 | complain); | |
ba47d38d AH |
5072 | e2 = cp_build_binary_op (location, |
5073 | EQ_EXPR, e2, integer_zero_node, | |
5ade1ed2 | 5074 | complain); |
ba47d38d AH |
5075 | e1 = cp_build_binary_op (location, |
5076 | TRUTH_ANDIF_EXPR, e2, e1, | |
5ade1ed2 | 5077 | complain); |
ba47d38d | 5078 | e2 = cp_build_binary_op (location, EQ_EXPR, |
c3a88be8 | 5079 | pfn0, |
e8160c9a | 5080 | build_zero_cst (TREE_TYPE (pfn0)), |
5ade1ed2 | 5081 | complain); |
ba47d38d AH |
5082 | e2 = cp_build_binary_op (location, |
5083 | TRUTH_ANDIF_EXPR, e2, e1, complain); | |
5084 | e1 = cp_build_binary_op (location, | |
5085 | EQ_EXPR, delta0, delta1, complain); | |
5086 | e1 = cp_build_binary_op (location, | |
5087 | TRUTH_ORIF_EXPR, e1, e2, complain); | |
c3a88be8 RM |
5088 | } |
5089 | else | |
5090 | { | |
5091 | /* We generate: | |
5092 | ||
5093 | (op0.pfn == op1.pfn | |
5094 | && (!op0.pfn || op0.delta == op1.delta)) | |
5095 | ||
5096 | The reason for the `!op0.pfn' bit is that a NULL | |
5097 | pointer-to-member is any member with a zero PFN; the | |
5098 | DELTA field is unspecified. */ | |
5099 | ||
ba47d38d AH |
5100 | e1 = cp_build_binary_op (location, |
5101 | EQ_EXPR, delta0, delta1, complain); | |
5102 | e2 = cp_build_binary_op (location, | |
5103 | EQ_EXPR, | |
c3a88be8 | 5104 | pfn0, |
e8160c9a | 5105 | build_zero_cst (TREE_TYPE (pfn0)), |
5ade1ed2 | 5106 | complain); |
ba47d38d AH |
5107 | e1 = cp_build_binary_op (location, |
5108 | TRUTH_ORIF_EXPR, e1, e2, complain); | |
c3a88be8 | 5109 | } |
f293ce4b | 5110 | e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1); |
ba47d38d AH |
5111 | e = cp_build_binary_op (location, |
5112 | TRUTH_ANDIF_EXPR, e2, e1, complain); | |
8d08fdba | 5113 | if (code == EQ_EXPR) |
c7e266a6 | 5114 | return e; |
ba47d38d AH |
5115 | return cp_build_binary_op (location, |
5116 | EQ_EXPR, e, integer_zero_node, complain); | |
8d08fdba | 5117 | } |
315fb5db NS |
5118 | else |
5119 | { | |
5120 | gcc_assert (!TYPE_PTRMEMFUNC_P (type0) | |
5121 | || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), | |
5122 | type1)); | |
5123 | gcc_assert (!TYPE_PTRMEMFUNC_P (type1) | |
5124 | || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), | |
5125 | type0)); | |
5126 | } | |
c8094d83 | 5127 | |
8d08fdba MS |
5128 | break; |
5129 | ||
5130 | case MAX_EXPR: | |
5131 | case MIN_EXPR: | |
5132 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) | |
5133 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
5134 | shorten = 1; | |
5135 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
708cae97 | 5136 | result_type = composite_pointer_type (type0, type1, op0, op1, |
c86818cf | 5137 | CPO_COMPARISON, complain); |
8d08fdba MS |
5138 | break; |
5139 | ||
5140 | case LE_EXPR: | |
5141 | case GE_EXPR: | |
5142 | case LT_EXPR: | |
5143 | case GT_EXPR: | |
eda0cd98 RS |
5144 | if (TREE_CODE (orig_op0) == STRING_CST |
5145 | || TREE_CODE (orig_op1) == STRING_CST) | |
5ade1ed2 DG |
5146 | { |
5147 | if (complain & tf_warning) | |
9c582551 JJ |
5148 | warning (OPT_Waddress, "comparison with string literal results " |
5149 | "in unspecified behavior"); | |
5ade1ed2 | 5150 | } |
eda0cd98 | 5151 | |
31ed6226 MG |
5152 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
5153 | { | |
5154 | vector_compare: | |
5155 | tree intt; | |
5156 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0), | |
0af94e6f JR |
5157 | TREE_TYPE (type1)) |
5158 | && !vector_types_compatible_elements_p (type0, type1)) | |
31ed6226 | 5159 | { |
07298ffd MG |
5160 | if (complain & tf_error) |
5161 | { | |
5162 | error_at (location, "comparing vectors with different " | |
5163 | "element types"); | |
5164 | inform (location, "operand types are %qT and %qT", | |
5165 | type0, type1); | |
5166 | } | |
31ed6226 MG |
5167 | return error_mark_node; |
5168 | } | |
5169 | ||
928686b1 RS |
5170 | if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0), |
5171 | TYPE_VECTOR_SUBPARTS (type1))) | |
31ed6226 | 5172 | { |
07298ffd MG |
5173 | if (complain & tf_error) |
5174 | { | |
5175 | error_at (location, "comparing vectors with different " | |
5176 | "number of elements"); | |
5177 | inform (location, "operand types are %qT and %qT", | |
5178 | type0, type1); | |
5179 | } | |
31ed6226 MG |
5180 | return error_mark_node; |
5181 | } | |
5182 | ||
fa74a4bc MP |
5183 | /* It's not precisely specified how the usual arithmetic |
5184 | conversions apply to the vector types. Here, we use | |
5185 | the unsigned type if one of the operands is signed and | |
5186 | the other one is unsigned. */ | |
5187 | if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)) | |
5188 | { | |
5189 | if (!TYPE_UNSIGNED (type0)) | |
5190 | op0 = build1 (VIEW_CONVERT_EXPR, type1, op0); | |
5191 | else | |
5192 | op1 = build1 (VIEW_CONVERT_EXPR, type0, op1); | |
5193 | warning_at (location, OPT_Wsign_compare, "comparison between " | |
5194 | "types %qT and %qT", type0, type1); | |
5195 | } | |
5196 | ||
31ed6226 | 5197 | /* Always construct signed integer vector type. */ |
b397965c RS |
5198 | intt = c_common_type_for_size |
5199 | (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0); | |
07298ffd MG |
5200 | if (!intt) |
5201 | { | |
5202 | if (complain & tf_error) | |
5203 | error_at (location, "could not find an integer type " | |
5204 | "of the same size as %qT", TREE_TYPE (type0)); | |
5205 | return error_mark_node; | |
5206 | } | |
31ed6226 MG |
5207 | result_type = build_opaque_vector_type (intt, |
5208 | TYPE_VECTOR_SUBPARTS (type0)); | |
5209 | converted = 1; | |
9f47c7e5 | 5210 | return build_vec_cmp (resultcode, result_type, op0, op1); |
31ed6226 | 5211 | } |
28cbf42c | 5212 | build_type = boolean_type_node; |
b5c0a77e JM |
5213 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
5214 | || code0 == ENUMERAL_TYPE) | |
5215 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
5216 | || code1 == ENUMERAL_TYPE)) | |
8d08fdba MS |
5217 | short_compare = 1; |
5218 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
708cae97 | 5219 | result_type = composite_pointer_type (type0, type1, op0, op1, |
c86818cf | 5220 | CPO_COMPARISON, complain); |
f41f1ceb | 5221 | else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1)) |
6f94398e PC |
5222 | { |
5223 | result_type = type0; | |
5224 | if (extra_warnings && (complain & tf_warning)) | |
5225 | warning (OPT_Wextra, | |
5226 | "ordered comparison of pointer with integer zero"); | |
5227 | } | |
f41f1ceb | 5228 | else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0)) |
6f94398e PC |
5229 | { |
5230 | result_type = type1; | |
5231 | if (extra_warnings && (complain & tf_warning)) | |
5232 | warning (OPT_Wextra, | |
5233 | "ordered comparison of pointer with integer zero"); | |
5234 | } | |
f41f1ceb | 5235 | else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)) |
14c2101d JM |
5236 | /* One of the operands must be of nullptr_t type. */ |
5237 | result_type = TREE_TYPE (nullptr_node); | |
8d08fdba MS |
5238 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
5239 | { | |
faae18ab | 5240 | result_type = type0; |
5ade1ed2 | 5241 | if (complain & tf_error) |
cbe5f3b3 | 5242 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
5243 | else |
5244 | return error_mark_node; | |
8d08fdba MS |
5245 | } |
5246 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
5247 | { | |
faae18ab | 5248 | result_type = type1; |
5ade1ed2 | 5249 | if (complain & tf_error) |
cbe5f3b3 | 5250 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
5251 | else |
5252 | return error_mark_node; | |
8d08fdba | 5253 | } |
05abad4c ML |
5254 | |
5255 | if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE) | |
52e67970 | 5256 | && !processing_template_decl |
05abad4c ML |
5257 | && sanitize_flags_p (SANITIZE_POINTER_COMPARE)) |
5258 | { | |
5259 | op0 = save_expr (op0); | |
5260 | op1 = save_expr (op1); | |
5261 | ||
5262 | tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE); | |
5263 | instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1); | |
5264 | } | |
5265 | ||
8d08fdba | 5266 | break; |
7f85441b | 5267 | |
1eb8759b RH |
5268 | case UNORDERED_EXPR: |
5269 | case ORDERED_EXPR: | |
5270 | case UNLT_EXPR: | |
5271 | case UNLE_EXPR: | |
5272 | case UNGT_EXPR: | |
5273 | case UNGE_EXPR: | |
5274 | case UNEQ_EXPR: | |
1eb8759b RH |
5275 | build_type = integer_type_node; |
5276 | if (code0 != REAL_TYPE || code1 != REAL_TYPE) | |
5277 | { | |
5ade1ed2 DG |
5278 | if (complain & tf_error) |
5279 | error ("unordered comparison on non-floating point argument"); | |
1eb8759b RH |
5280 | return error_mark_node; |
5281 | } | |
5282 | common = 1; | |
5283 | break; | |
5284 | ||
7f85441b KG |
5285 | default: |
5286 | break; | |
8d08fdba MS |
5287 | } |
5288 | ||
550a799d JM |
5289 | if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
5290 | || code0 == ENUMERAL_TYPE) | |
c8094d83 | 5291 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
550a799d | 5292 | || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))) |
73042643 AH |
5293 | arithmetic_types_p = 1; |
5294 | else | |
5295 | { | |
5296 | arithmetic_types_p = 0; | |
5297 | /* Vector arithmetic is only allowed when both sides are vectors. */ | |
5298 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) | |
5299 | { | |
f6d7e7d8 | 5300 | if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) |
0af94e6f | 5301 | || !vector_types_compatible_elements_p (type0, type1)) |
f6d7e7d8 | 5302 | { |
76545796 | 5303 | if (complain & tf_error) |
745e411d DM |
5304 | { |
5305 | /* "location" already embeds the locations of the | |
5306 | operands, so we don't need to add them separately | |
5307 | to richloc. */ | |
5308 | rich_location richloc (line_table, location); | |
5309 | binary_op_error (&richloc, code, type0, type1); | |
5310 | } | |
f6d7e7d8 AH |
5311 | return error_mark_node; |
5312 | } | |
73042643 AH |
5313 | arithmetic_types_p = 1; |
5314 | } | |
5315 | } | |
455f19cb MM |
5316 | /* Determine the RESULT_TYPE, if it is not already known. */ |
5317 | if (!result_type | |
c8094d83 | 5318 | && arithmetic_types_p |
455f19cb | 5319 | && (shorten || common || short_compare)) |
c5ee1358 MM |
5320 | { |
5321 | result_type = cp_common_type (type0, type1); | |
76545796 PC |
5322 | if (complain & tf_warning) |
5323 | do_warn_double_promotion (result_type, type0, type1, | |
f012c8ef | 5324 | "implicit conversion from %qH to %qI " |
76545796 PC |
5325 | "to match other operand of binary " |
5326 | "expression", | |
5327 | location); | |
c5ee1358 | 5328 | } |
455f19cb MM |
5329 | |
5330 | if (!result_type) | |
8d08fdba | 5331 | { |
5ade1ed2 | 5332 | if (complain & tf_error) |
a14feb3c DM |
5333 | { |
5334 | binary_op_rich_location richloc (location, | |
5335 | orig_op0, orig_op1, true); | |
5336 | error_at (&richloc, | |
5337 | "invalid operands of types %qT and %qT to binary %qO", | |
5338 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code); | |
5339 | } | |
455f19cb MM |
5340 | return error_mark_node; |
5341 | } | |
37c46b43 | 5342 | |
455f19cb MM |
5343 | /* If we're in a template, the only thing we need to know is the |
5344 | RESULT_TYPE. */ | |
5345 | if (processing_template_decl) | |
5be014d5 AP |
5346 | { |
5347 | /* Since the middle-end checks the type when doing a build2, we | |
5348 | need to build the tree in pieces. This built tree will never | |
5349 | get out of the front-end as we replace it when instantiating | |
5350 | the template. */ | |
5351 | tree tmp = build2 (resultcode, | |
5352 | build_type ? build_type : result_type, | |
5353 | NULL_TREE, op1); | |
5354 | TREE_OPERAND (tmp, 0) = op0; | |
5355 | return tmp; | |
5356 | } | |
455f19cb | 5357 | |
98a7a34e MP |
5358 | /* Remember the original type; RESULT_TYPE might be changed later on |
5359 | by shorten_binary_op. */ | |
5360 | tree orig_type = result_type; | |
5361 | ||
455f19cb MM |
5362 | if (arithmetic_types_p) |
5363 | { | |
1ff6b2c8 JM |
5364 | bool first_complex = (code0 == COMPLEX_TYPE); |
5365 | bool second_complex = (code1 == COMPLEX_TYPE); | |
5366 | int none_complex = (!first_complex && !second_complex); | |
5367 | ||
5368 | /* Adapted from patch for c/24581. */ | |
5369 | if (first_complex != second_complex | |
5370 | && (code == PLUS_EXPR | |
5371 | || code == MINUS_EXPR | |
5372 | || code == MULT_EXPR | |
5373 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
5374 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
5375 | && flag_signed_zeros) | |
5376 | { | |
5377 | /* An operation on mixed real/complex operands must be | |
5378 | handled specially, but the language-independent code can | |
5379 | more easily optimize the plain complex arithmetic if | |
5380 | -fno-signed-zeros. */ | |
5381 | tree real_type = TREE_TYPE (result_type); | |
5382 | tree real, imag; | |
5383 | if (first_complex) | |
5384 | { | |
5385 | if (TREE_TYPE (op0) != result_type) | |
4b978f96 | 5386 | op0 = cp_convert_and_check (result_type, op0, complain); |
1ff6b2c8 | 5387 | if (TREE_TYPE (op1) != real_type) |
4b978f96 | 5388 | op1 = cp_convert_and_check (real_type, op1, complain); |
1ff6b2c8 JM |
5389 | } |
5390 | else | |
5391 | { | |
5392 | if (TREE_TYPE (op0) != real_type) | |
4b978f96 | 5393 | op0 = cp_convert_and_check (real_type, op0, complain); |
1ff6b2c8 | 5394 | if (TREE_TYPE (op1) != result_type) |
4b978f96 | 5395 | op1 = cp_convert_and_check (result_type, op1, complain); |
1ff6b2c8 JM |
5396 | } |
5397 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
5398 | return error_mark_node; | |
5399 | if (first_complex) | |
5400 | { | |
5401 | op0 = save_expr (op0); | |
e51fbec3 MP |
5402 | real = cp_build_unary_op (REALPART_EXPR, op0, true, complain); |
5403 | imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain); | |
1ff6b2c8 JM |
5404 | switch (code) |
5405 | { | |
5406 | case MULT_EXPR: | |
5407 | case TRUNC_DIV_EXPR: | |
e834d848 | 5408 | op1 = save_expr (op1); |
1ff6b2c8 JM |
5409 | imag = build2 (resultcode, real_type, imag, op1); |
5410 | /* Fall through. */ | |
5411 | case PLUS_EXPR: | |
5412 | case MINUS_EXPR: | |
5413 | real = build2 (resultcode, real_type, real, op1); | |
5414 | break; | |
5415 | default: | |
5416 | gcc_unreachable(); | |
5417 | } | |
5418 | } | |
5419 | else | |
5420 | { | |
5421 | op1 = save_expr (op1); | |
e51fbec3 MP |
5422 | real = cp_build_unary_op (REALPART_EXPR, op1, true, complain); |
5423 | imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain); | |
1ff6b2c8 JM |
5424 | switch (code) |
5425 | { | |
5426 | case MULT_EXPR: | |
e834d848 | 5427 | op0 = save_expr (op0); |
1ff6b2c8 JM |
5428 | imag = build2 (resultcode, real_type, op0, imag); |
5429 | /* Fall through. */ | |
5430 | case PLUS_EXPR: | |
5431 | real = build2 (resultcode, real_type, op0, real); | |
5432 | break; | |
5433 | case MINUS_EXPR: | |
5434 | real = build2 (resultcode, real_type, op0, real); | |
5435 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
5436 | break; | |
5437 | default: | |
5438 | gcc_unreachable(); | |
5439 | } | |
5440 | } | |
68a29e1c | 5441 | result = build2 (COMPLEX_EXPR, result_type, real, imag); |
68a29e1c | 5442 | return result; |
1ff6b2c8 | 5443 | } |
8d08fdba MS |
5444 | |
5445 | /* For certain operations (which identify themselves by shorten != 0) | |
5446 | if both args were extended from the same smaller type, | |
5447 | do the arithmetic in that type and then extend. | |
5448 | ||
5449 | shorten !=0 and !=1 indicates a bitwise operation. | |
5450 | For them, this optimization is safe only if | |
5451 | both args are zero-extended or both are sign-extended. | |
5452 | Otherwise, we might change the result. | |
39a13be5 | 5453 | E.g., (short)-1 | (unsigned short)-1 is (int)-1 |
8d08fdba MS |
5454 | but calculated in (unsigned short) it would be (unsigned short)-1. */ |
5455 | ||
37c46b43 | 5456 | if (shorten && none_complex) |
8d08fdba | 5457 | { |
8d08fdba | 5458 | final_type = result_type; |
de5a5fa1 | 5459 | result_type = shorten_binary_op (result_type, op0, op1, |
6715192c | 5460 | shorten == -1); |
8d08fdba MS |
5461 | } |
5462 | ||
8d08fdba MS |
5463 | /* Comparison operations are shortened too but differently. |
5464 | They identify themselves by setting short_compare = 1. */ | |
5465 | ||
5466 | if (short_compare) | |
5467 | { | |
c56e9727 MP |
5468 | /* We call shorten_compare only for diagnostics. */ |
5469 | tree xop0 = fold_simple (op0); | |
5470 | tree xop1 = fold_simple (op1); | |
5471 | tree xresult_type = result_type; | |
8d08fdba | 5472 | enum tree_code xresultcode = resultcode; |
cda0a029 | 5473 | shorten_compare (location, &xop0, &xop1, &xresult_type, |
c56e9727 | 5474 | &xresultcode); |
8d08fdba MS |
5475 | } |
5476 | ||
8d3b081e | 5477 | if ((short_compare || code == MIN_EXPR || code == MAX_EXPR) |
7d48af30 MM |
5478 | && warn_sign_compare |
5479 | /* Do not warn until the template is instantiated; we cannot | |
5480 | bound the ranges of the arguments until that point. */ | |
2d12797c | 5481 | && !processing_template_decl |
98f80e91 | 5482 | && (complain & tf_warning) |
12448f77 ILT |
5483 | && c_inhibit_evaluation_warnings == 0 |
5484 | /* Even unsigned enum types promote to signed int. We don't | |
5485 | want to issue -Wsign-compare warnings for this case. */ | |
5486 | && !enum_cast_to_int (orig_op0) | |
5487 | && !enum_cast_to_int (orig_op1)) | |
8d08fdba | 5488 | { |
0d23cf7a JJ |
5489 | tree oop0 = maybe_constant_value (orig_op0); |
5490 | tree oop1 = maybe_constant_value (orig_op1); | |
5491 | ||
5492 | if (TREE_CODE (oop0) != INTEGER_CST) | |
cda0a029 | 5493 | oop0 = cp_fully_fold (orig_op0); |
0d23cf7a | 5494 | if (TREE_CODE (oop1) != INTEGER_CST) |
cda0a029 | 5495 | oop1 = cp_fully_fold (orig_op1); |
0d23cf7a | 5496 | warn_for_sign_compare (location, oop0, oop1, op0, op1, |
ba47d38d | 5497 | result_type, resultcode); |
8d08fdba MS |
5498 | } |
5499 | } | |
5500 | ||
b9edb4b1 MLI |
5501 | /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE. |
5502 | Then the expression will be built. | |
5503 | It will be given type FINAL_TYPE if that is nonzero; | |
5504 | otherwise, it will be given type RESULT_TYPE. */ | |
8d08fdba MS |
5505 | if (! converted) |
5506 | { | |
c56e9727 | 5507 | warning_sentinel w (warn_sign_conversion, short_compare); |
8d08fdba | 5508 | if (TREE_TYPE (op0) != result_type) |
4b978f96 | 5509 | op0 = cp_convert_and_check (result_type, op0, complain); |
8d08fdba | 5510 | if (TREE_TYPE (op1) != result_type) |
4b978f96 | 5511 | op1 = cp_convert_and_check (result_type, op1, complain); |
848b92e1 JM |
5512 | |
5513 | if (op0 == error_mark_node || op1 == error_mark_node) | |
5514 | return error_mark_node; | |
8d08fdba MS |
5515 | } |
5516 | ||
28cbf42c MS |
5517 | if (build_type == NULL_TREE) |
5518 | build_type = result_type; | |
5519 | ||
45b2222a ML |
5520 | if (sanitize_flags_p ((SANITIZE_SHIFT |
5521 | | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE)) | |
f34ebeb2 | 5522 | && current_function_decl != NULL_TREE |
de5a5fa1 | 5523 | && !processing_template_decl |
de5a5fa1 MP |
5524 | && (doing_div_or_mod || doing_shift)) |
5525 | { | |
5526 | /* OP0 and/or OP1 might have side-effects. */ | |
5527 | op0 = cp_save_expr (op0); | |
5528 | op1 = cp_save_expr (op1); | |
e56f6629 JM |
5529 | op0 = fold_non_dependent_expr (op0, complain); |
5530 | op1 = fold_non_dependent_expr (op1, complain); | |
45b2222a ML |
5531 | if (doing_div_or_mod |
5532 | && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE)) | |
de5a5fa1 MP |
5533 | { |
5534 | /* For diagnostics we want to use the promoted types without | |
5535 | shorten_binary_op. So convert the arguments to the | |
5536 | original result_type. */ | |
5537 | tree cop0 = op0; | |
5538 | tree cop1 = op1; | |
98a7a34e MP |
5539 | if (TREE_TYPE (cop0) != orig_type) |
5540 | cop0 = cp_convert (orig_type, op0, complain); | |
5541 | if (TREE_TYPE (cop1) != orig_type) | |
5542 | cop1 = cp_convert (orig_type, op1, complain); | |
de5a5fa1 MP |
5543 | instrument_expr = ubsan_instrument_division (location, cop0, cop1); |
5544 | } | |
45b2222a | 5545 | else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT)) |
de5a5fa1 MP |
5546 | instrument_expr = ubsan_instrument_shift (location, code, op0, op1); |
5547 | } | |
5548 | ||
e87eed2a | 5549 | result = build2_loc (location, resultcode, build_type, op0, op1); |
455f19cb | 5550 | if (final_type != 0) |
4b978f96 | 5551 | result = cp_convert (final_type, result, complain); |
16fd4d2d | 5552 | |
a24d975c | 5553 | if (instrument_expr != NULL) |
cda0a029 JM |
5554 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
5555 | instrument_expr, result); | |
5556 | ||
5557 | if (!processing_template_decl) | |
5558 | { | |
5559 | op0 = cp_fully_fold (op0); | |
5560 | /* Only consider the second argument if the first isn't overflowed. */ | |
5561 | if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0)) | |
5562 | return result; | |
5563 | op1 = cp_fully_fold (op1); | |
5564 | if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1)) | |
5565 | return result; | |
5566 | } | |
5567 | else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1) | |
5568 | || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1)) | |
5569 | return result; | |
5570 | ||
5571 | result_ovl = fold_build2 (resultcode, build_type, op0, op1); | |
5572 | if (TREE_OVERFLOW_P (result_ovl)) | |
5573 | overflow_warning (location, result_ovl); | |
de5a5fa1 | 5574 | |
455f19cb | 5575 | return result; |
8d08fdba | 5576 | } |
bedc293e MG |
5577 | |
5578 | /* Build a VEC_PERM_EXPR. | |
5579 | This is a simple wrapper for c_build_vec_perm_expr. */ | |
5580 | tree | |
5581 | build_x_vec_perm_expr (location_t loc, | |
5582 | tree arg0, tree arg1, tree arg2, | |
5583 | tsubst_flags_t complain) | |
5584 | { | |
4939c5f3 MG |
5585 | tree orig_arg0 = arg0; |
5586 | tree orig_arg1 = arg1; | |
5587 | tree orig_arg2 = arg2; | |
5588 | if (processing_template_decl) | |
5589 | { | |
5590 | if (type_dependent_expression_p (arg0) | |
bedc293e | 5591 | || type_dependent_expression_p (arg1) |
4939c5f3 MG |
5592 | || type_dependent_expression_p (arg2)) |
5593 | return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2); | |
5594 | arg0 = build_non_dependent_expr (arg0); | |
5595 | if (arg1) | |
5596 | arg1 = build_non_dependent_expr (arg1); | |
5597 | arg2 = build_non_dependent_expr (arg2); | |
5598 | } | |
5599 | tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error); | |
5600 | if (processing_template_decl && exp != error_mark_node) | |
5601 | return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0, | |
5602 | orig_arg1, orig_arg2); | |
5603 | return exp; | |
bedc293e | 5604 | } |
8d08fdba MS |
5605 | \f |
5606 | /* Return a tree for the sum or difference (RESULTCODE says which) | |
5607 | of pointer PTROP and integer INTOP. */ | |
5608 | ||
5609 | static tree | |
4f2e1536 MP |
5610 | cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop, |
5611 | tree intop, tsubst_flags_t complain) | |
8d08fdba | 5612 | { |
bfba94bd MM |
5613 | tree res_type = TREE_TYPE (ptrop); |
5614 | ||
5615 | /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type) | |
5616 | in certain circumstance (when it's valid to do so). So we need | |
5617 | to make sure it's complete. We don't need to check here, if we | |
5618 | can actually complete it at all, as those checks will be done in | |
5619 | pointer_int_sum() anyway. */ | |
5620 | complete_type (TREE_TYPE (res_type)); | |
8f259df3 | 5621 | |
4f2e1536 | 5622 | return pointer_int_sum (loc, resultcode, ptrop, |
cda0a029 | 5623 | intop, complain & tf_warning_or_error); |
8d08fdba MS |
5624 | } |
5625 | ||
5626 | /* Return a tree for the difference of pointers OP0 and OP1. | |
05abad4c ML |
5627 | The resulting tree has type int. If POINTER_SUBTRACT sanitization is |
5628 | enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */ | |
8d08fdba MS |
5629 | |
5630 | static tree | |
4f2e1536 | 5631 | pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype, |
05abad4c | 5632 | tsubst_flags_t complain, tree *instrument_expr) |
8d08fdba | 5633 | { |
1af4ebf5 | 5634 | tree result, inttype; |
8d08fdba | 5635 | tree restype = ptrdiff_type_node; |
79a7c7fa | 5636 | tree target_type = TREE_TYPE (ptrtype); |
8d08fdba | 5637 | |
66543169 | 5638 | if (!complete_type_or_else (target_type, NULL_TREE)) |
8f259df3 MM |
5639 | return error_mark_node; |
5640 | ||
50e10fa8 | 5641 | if (VOID_TYPE_P (target_type)) |
4b978f96 PC |
5642 | { |
5643 | if (complain & tf_error) | |
4f2e1536 | 5644 | permerror (loc, "ISO C++ forbids using pointer of " |
4b978f96 PC |
5645 | "type %<void *%> in subtraction"); |
5646 | else | |
5647 | return error_mark_node; | |
5648 | } | |
fcf73884 | 5649 | if (TREE_CODE (target_type) == FUNCTION_TYPE) |
4b978f96 PC |
5650 | { |
5651 | if (complain & tf_error) | |
4f2e1536 | 5652 | permerror (loc, "ISO C++ forbids using pointer to " |
4b978f96 PC |
5653 | "a function in subtraction"); |
5654 | else | |
5655 | return error_mark_node; | |
5656 | } | |
fcf73884 | 5657 | if (TREE_CODE (target_type) == METHOD_TYPE) |
4b978f96 PC |
5658 | { |
5659 | if (complain & tf_error) | |
4f2e1536 | 5660 | permerror (loc, "ISO C++ forbids using pointer to " |
4b978f96 PC |
5661 | "a method in subtraction"); |
5662 | else | |
5663 | return error_mark_node; | |
5664 | } | |
8d08fdba | 5665 | |
1af4ebf5 MG |
5666 | /* Determine integer type result of the subtraction. This will usually |
5667 | be the same as the result type (ptrdiff_t), but may need to be a wider | |
5668 | type if pointers for the address space are wider than ptrdiff_t. */ | |
5669 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) | |
5670 | inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); | |
5671 | else | |
5672 | inttype = restype; | |
5673 | ||
52e67970 JJ |
5674 | if (!processing_template_decl |
5675 | && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT)) | |
05abad4c ML |
5676 | { |
5677 | op0 = save_expr (op0); | |
5678 | op1 = save_expr (op1); | |
5679 | ||
5680 | tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT); | |
5681 | *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1); | |
5682 | } | |
5683 | ||
1af4ebf5 MG |
5684 | /* First do the subtraction, then build the divide operator |
5685 | and only convert at the very end. | |
5686 | Do not do default conversions in case restype is a short type. */ | |
5687 | ||
5688 | /* POINTER_DIFF_EXPR requires a signed integer type of the same size as | |
5689 | pointers. If some platform cannot provide that, or has a larger | |
5690 | ptrdiff_type to support differences larger than half the address | |
5691 | space, cast the pointers to some larger integer type and do the | |
5692 | computations in that type. */ | |
5693 | if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0))) | |
5694 | op0 = cp_build_binary_op (loc, | |
5695 | MINUS_EXPR, | |
5696 | cp_convert (inttype, op0, complain), | |
5697 | cp_convert (inttype, op1, complain), | |
5698 | complain); | |
5699 | else | |
5700 | op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1); | |
8d08fdba MS |
5701 | |
5702 | /* This generates an error if op1 is a pointer to an incomplete type. */ | |
d0f062fb | 5703 | if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1)))) |
4b978f96 PC |
5704 | { |
5705 | if (complain & tf_error) | |
4f2e1536 MP |
5706 | error_at (loc, "invalid use of a pointer to an incomplete type in " |
5707 | "pointer arithmetic"); | |
4b978f96 PC |
5708 | else |
5709 | return error_mark_node; | |
5710 | } | |
8d08fdba | 5711 | |
f04dda30 MP |
5712 | if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1))) |
5713 | { | |
5714 | if (complain & tf_error) | |
4f2e1536 | 5715 | error_at (loc, "arithmetic on pointer to an empty aggregate"); |
f04dda30 MP |
5716 | else |
5717 | return error_mark_node; | |
5718 | } | |
5719 | ||
c8094d83 | 5720 | op1 = (TYPE_PTROB_P (ptrtype) |
4f2e1536 | 5721 | ? size_in_bytes_loc (loc, target_type) |
a5ac359a | 5722 | : integer_one_node); |
8d08fdba MS |
5723 | |
5724 | /* Do the division. */ | |
5725 | ||
1af4ebf5 MG |
5726 | result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0, |
5727 | cp_convert (inttype, op1, complain)); | |
5728 | return cp_convert (restype, result, complain); | |
8d08fdba MS |
5729 | } |
5730 | \f | |
8d08fdba MS |
5731 | /* Construct and perhaps optimize a tree representation |
5732 | for a unary operation. CODE, a tree_code, specifies the operation | |
5733 | and XARG is the operand. */ | |
5734 | ||
5735 | tree | |
e87eed2a | 5736 | build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg, |
4fe977f2 | 5737 | tsubst_flags_t complain) |
8d08fdba | 5738 | { |
d17811fd | 5739 | tree orig_expr = xarg; |
19420d00 NS |
5740 | tree exp; |
5741 | int ptrmem = 0; | |
fcb9363e | 5742 | tree overload = NULL_TREE; |
c8094d83 | 5743 | |
5156628f | 5744 | if (processing_template_decl) |
d17811fd MM |
5745 | { |
5746 | if (type_dependent_expression_p (xarg)) | |
bddee796 JM |
5747 | { |
5748 | tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE); | |
5749 | maybe_save_operator_binding (e); | |
5750 | return e; | |
5751 | } | |
3601f003 | 5752 | |
d17811fd MM |
5753 | xarg = build_non_dependent_expr (xarg); |
5754 | } | |
5755 | ||
5756 | exp = NULL_TREE; | |
5566b478 | 5757 | |
1e2e9f54 MM |
5758 | /* [expr.unary.op] says: |
5759 | ||
5760 | The address of an object of incomplete type can be taken. | |
5761 | ||
5762 | (And is just the ordinary address operator, not an overloaded | |
5763 | "operator &".) However, if the type is a template | |
5764 | specialization, we must complete the type at this point so that | |
5765 | an overloaded "operator &" will be available if required. */ | |
db5ae43f | 5766 | if (code == ADDR_EXPR |
386b8a85 | 5767 | && TREE_CODE (xarg) != TEMPLATE_ID_EXPR |
1e2e9f54 MM |
5768 | && ((CLASS_TYPE_P (TREE_TYPE (xarg)) |
5769 | && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg)))) | |
db5ae43f | 5770 | || (TREE_CODE (xarg) == OFFSET_REF))) |
f4f206f4 | 5771 | /* Don't look for a function. */; |
db5ae43f | 5772 | else |
4fe977f2 | 5773 | exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE, |
fcb9363e PP |
5774 | NULL_TREE, &overload, complain); |
5775 | ||
d17811fd | 5776 | if (!exp && code == ADDR_EXPR) |
c91a56d2 | 5777 | { |
7e361ae6 JM |
5778 | if (is_overloaded_fn (xarg)) |
5779 | { | |
5780 | tree fn = get_first_fn (xarg); | |
5781 | if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn)) | |
5782 | { | |
76545796 PC |
5783 | if (complain & tf_error) |
5784 | error (DECL_CONSTRUCTOR_P (fn) | |
e5e691a5 PC |
5785 | ? G_("taking address of constructor %qD") |
5786 | : G_("taking address of destructor %qD"), | |
5787 | fn); | |
7e361ae6 JM |
5788 | return error_mark_node; |
5789 | } | |
5790 | } | |
5791 | ||
5792 | /* A pointer to member-function can be formed only by saying | |
5793 | &X::mf. */ | |
1e14c7f0 GDR |
5794 | if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE |
5795 | && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg))) | |
5796 | { | |
5ebbc0ee NS |
5797 | if (TREE_CODE (xarg) != OFFSET_REF |
5798 | || !TYPE_P (TREE_OPERAND (xarg, 0))) | |
1e14c7f0 | 5799 | { |
76545796 PC |
5800 | if (complain & tf_error) |
5801 | { | |
5802 | error ("invalid use of %qE to form a " | |
e87eed2a | 5803 | "pointer-to-member-function", xarg.get_value ()); |
76545796 PC |
5804 | if (TREE_CODE (xarg) != OFFSET_REF) |
5805 | inform (input_location, " a qualified-id is required"); | |
5806 | } | |
1e14c7f0 GDR |
5807 | return error_mark_node; |
5808 | } | |
5809 | else | |
5810 | { | |
76545796 PC |
5811 | if (complain & tf_error) |
5812 | error ("parentheses around %qE cannot be used to form a" | |
5813 | " pointer-to-member-function", | |
e87eed2a | 5814 | xarg.get_value ()); |
76545796 PC |
5815 | else |
5816 | return error_mark_node; | |
1e14c7f0 GDR |
5817 | PTRMEM_OK_P (xarg) = 1; |
5818 | } | |
5819 | } | |
c8094d83 | 5820 | |
19420d00 | 5821 | if (TREE_CODE (xarg) == OFFSET_REF) |
0cbd7506 MS |
5822 | { |
5823 | ptrmem = PTRMEM_OK_P (xarg); | |
c8094d83 | 5824 | |
0cbd7506 MS |
5825 | if (!ptrmem && !flag_ms_extensions |
5826 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE) | |
4542128e NS |
5827 | { |
5828 | /* A single non-static member, make sure we don't allow a | |
0cbd7506 | 5829 | pointer-to-member. */ |
f293ce4b RS |
5830 | xarg = build2 (OFFSET_REF, TREE_TYPE (xarg), |
5831 | TREE_OPERAND (xarg, 0), | |
19b476fb | 5832 | ovl_make (TREE_OPERAND (xarg, 1))); |
4542128e | 5833 | PTRMEM_OK_P (xarg) = ptrmem; |
c8094d83 | 5834 | } |
0cbd7506 | 5835 | } |
93c0e0bb JM |
5836 | |
5837 | exp = cp_build_addr_expr_strict (xarg, complain); | |
c91a56d2 MS |
5838 | } |
5839 | ||
d17811fd | 5840 | if (processing_template_decl && exp != error_mark_node) |
fcb9363e PP |
5841 | { |
5842 | if (overload != NULL_TREE) | |
5843 | return (build_min_non_dep_op_overload | |
5844 | (code, exp, overload, orig_expr, integer_zero_node)); | |
5845 | ||
5846 | exp = build_min_non_dep (code, exp, orig_expr, | |
5847 | /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE); | |
5848 | } | |
6439fffd KL |
5849 | if (TREE_CODE (exp) == ADDR_EXPR) |
5850 | PTRMEM_OK_P (exp) = ptrmem; | |
19420d00 | 5851 | return exp; |
8d08fdba MS |
5852 | } |
5853 | ||
be845b04 JJ |
5854 | /* Construct and perhaps optimize a tree representation |
5855 | for __builtin_addressof operation. ARG specifies the operand. */ | |
5856 | ||
5857 | tree | |
5858 | cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain) | |
5859 | { | |
5860 | tree orig_expr = arg; | |
5861 | ||
5862 | if (processing_template_decl) | |
5863 | { | |
5864 | if (type_dependent_expression_p (arg)) | |
5865 | return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE); | |
5866 | ||
5867 | arg = build_non_dependent_expr (arg); | |
5868 | } | |
5869 | ||
5870 | tree exp = cp_build_addr_expr_strict (arg, complain); | |
5871 | ||
5872 | if (processing_template_decl && exp != error_mark_node) | |
5873 | exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE); | |
5874 | return exp; | |
5875 | } | |
5876 | ||
78ef5b89 NB |
5877 | /* Like c_common_truthvalue_conversion, but handle pointer-to-member |
5878 | constants, where a null value is represented by an INTEGER_CST of | |
5879 | -1. */ | |
96d6c610 JM |
5880 | |
5881 | tree | |
5671bf27 | 5882 | cp_truthvalue_conversion (tree expr) |
96d6c610 JM |
5883 | { |
5884 | tree type = TREE_TYPE (expr); | |
451dcc66 | 5885 | if (TYPE_PTR_OR_PTRMEM_P (type) |
9cca4e3d JM |
5886 | /* Avoid ICE on invalid use of non-static member function. */ |
5887 | || TREE_CODE (expr) == FUNCTION_DECL) | |
30af3a2b | 5888 | return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true); |
96d6c610 | 5889 | else |
ba47d38d | 5890 | return c_common_truthvalue_conversion (input_location, expr); |
96d6c610 JM |
5891 | } |
5892 | ||
4556c5b3 JM |
5893 | /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This |
5894 | is a low-level function; most callers should use maybe_convert_cond. */ | |
c8094d83 | 5895 | |
2986ae00 | 5896 | tree |
5671bf27 | 5897 | condition_conversion (tree expr) |
2986ae00 | 5898 | { |
5566b478 | 5899 | tree t; |
1dad57e6 JM |
5900 | t = perform_implicit_conversion_flags (boolean_type_node, expr, |
5901 | tf_warning_or_error, LOOKUP_NORMAL); | |
4556c5b3 JM |
5902 | if (!processing_template_decl) |
5903 | t = fold_build_cleanup_point_expr (boolean_type_node, t); | |
8ccc31eb | 5904 | return t; |
2986ae00 | 5905 | } |
c8094d83 | 5906 | |
d3e1e89e JM |
5907 | /* Returns the address of T. This function will fold away |
5908 | ADDR_EXPR of INDIRECT_REF. */ | |
7993382e MM |
5909 | |
5910 | tree | |
5911 | build_address (tree t) | |
5912 | { | |
7993382e MM |
5913 | if (error_operand_p (t) || !cxx_mark_addressable (t)) |
5914 | return error_mark_node; | |
d15f0fa7 MP |
5915 | gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR |
5916 | || processing_template_decl); | |
9a004410 | 5917 | t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t); |
d3e1e89e JM |
5918 | if (TREE_CODE (t) != ADDR_EXPR) |
5919 | t = rvalue (t); | |
5920 | return t; | |
7993382e MM |
5921 | } |
5922 | ||
5923 | /* Return a NOP_EXPR converting EXPR to TYPE. */ | |
5924 | ||
5925 | tree | |
5926 | build_nop (tree type, tree expr) | |
5927 | { | |
7993382e MM |
5928 | if (type == error_mark_node || error_operand_p (expr)) |
5929 | return expr; | |
e87eed2a | 5930 | return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr); |
7993382e MM |
5931 | } |
5932 | ||
93c0e0bb JM |
5933 | /* Take the address of ARG, whatever that means under C++ semantics. |
5934 | If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues | |
5935 | and class rvalues as well. | |
5936 | ||
5937 | Nothing should call this function directly; instead, callers should use | |
5938 | cp_build_addr_expr or cp_build_addr_expr_strict. */ | |
5939 | ||
5940 | static tree | |
5941 | cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain) | |
5942 | { | |
5943 | tree argtype; | |
5944 | tree val; | |
5945 | ||
5946 | if (!arg || error_operand_p (arg)) | |
5947 | return error_mark_node; | |
5948 | ||
5949 | arg = mark_lvalue_use (arg); | |
84dd815f JM |
5950 | if (error_operand_p (arg)) |
5951 | return error_mark_node; | |
5952 | ||
93c0e0bb JM |
5953 | argtype = lvalue_type (arg); |
5954 | ||
84c0088f | 5955 | gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg))); |
93c0e0bb JM |
5956 | |
5957 | if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg) | |
1f0ed17c | 5958 | && !really_overloaded_fn (arg)) |
93c0e0bb JM |
5959 | { |
5960 | /* They're trying to take the address of a unique non-static | |
5961 | member function. This is ill-formed (except in MS-land), | |
5962 | but let's try to DTRT. | |
5963 | Note: We only handle unique functions here because we don't | |
5964 | want to complain if there's a static overload; non-unique | |
5965 | cases will be handled by instantiate_type. But we need to | |
5966 | handle this case here to allow casts on the resulting PMF. | |
5967 | We could defer this in non-MS mode, but it's easier to give | |
5968 | a useful error here. */ | |
5969 | ||
5970 | /* Inside constant member functions, the `this' pointer | |
5971 | contains an extra const qualifier. TYPE_MAIN_VARIANT | |
5972 | is used here to remove this const from the diagnostics | |
5973 | and the created OFFSET_REF. */ | |
5974 | tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0))); | |
5975 | tree fn = get_first_fn (TREE_OPERAND (arg, 1)); | |
9f635aba PC |
5976 | if (!mark_used (fn, complain) && !(complain & tf_error)) |
5977 | return error_mark_node; | |
93c0e0bb JM |
5978 | |
5979 | if (! flag_ms_extensions) | |
5980 | { | |
5981 | tree name = DECL_NAME (fn); | |
5982 | if (!(complain & tf_error)) | |
5983 | return error_mark_node; | |
5984 | else if (current_class_type | |
5985 | && TREE_OPERAND (arg, 0) == current_class_ref) | |
5986 | /* An expression like &memfn. */ | |
5987 | permerror (input_location, "ISO C++ forbids taking the address of an unqualified" | |
5988 | " or parenthesized non-static member function to form" | |
5989 | " a pointer to member function. Say %<&%T::%D%>", | |
5990 | base, name); | |
5991 | else | |
5992 | permerror (input_location, "ISO C++ forbids taking the address of a bound member" | |
5993 | " function to form a pointer to member function." | |
5994 | " Say %<&%T::%D%>", | |
5995 | base, name); | |
5996 | } | |
a378996b | 5997 | arg = build_offset_ref (base, fn, /*address_p=*/true, complain); |
93c0e0bb JM |
5998 | } |
5999 | ||
6000 | /* Uninstantiated types are all functions. Taking the | |
6001 | address of a function is a no-op, so just return the | |
6002 | argument. */ | |
6003 | if (type_unknown_p (arg)) | |
6004 | return build1 (ADDR_EXPR, unknown_type_node, arg); | |
6005 | ||
6006 | if (TREE_CODE (arg) == OFFSET_REF) | |
6007 | /* We want a pointer to member; bypass all the code for actually taking | |
6008 | the address of something. */ | |
6009 | goto offset_ref; | |
6010 | ||
6011 | /* Anything not already handled and not a true memory reference | |
6012 | is an error. */ | |
6013 | if (TREE_CODE (argtype) != FUNCTION_TYPE | |
6014 | && TREE_CODE (argtype) != METHOD_TYPE) | |
6015 | { | |
4e9ca9b0 JM |
6016 | cp_lvalue_kind kind = lvalue_kind (arg); |
6017 | if (kind == clk_none) | |
93c0e0bb JM |
6018 | { |
6019 | if (complain & tf_error) | |
7bd11157 | 6020 | lvalue_error (input_location, lv_addressof); |
93c0e0bb JM |
6021 | return error_mark_node; |
6022 | } | |
4e9ca9b0 JM |
6023 | if (strict_lvalue && (kind & (clk_rvalueref|clk_class))) |
6024 | { | |
6025 | if (!(complain & tf_error)) | |
6026 | return error_mark_node; | |
955da5e5 JM |
6027 | /* Make this a permerror because we used to accept it. */ |
6028 | permerror (input_location, "taking address of rvalue"); | |
4e9ca9b0 | 6029 | } |
93c0e0bb JM |
6030 | } |
6031 | ||
9f613f06 | 6032 | if (TYPE_REF_P (argtype)) |
93c0e0bb JM |
6033 | { |
6034 | tree type = build_pointer_type (TREE_TYPE (argtype)); | |
6035 | arg = build1 (CONVERT_EXPR, type, arg); | |
6036 | return arg; | |
6037 | } | |
6038 | else if (pedantic && DECL_MAIN_P (arg)) | |
6039 | { | |
6040 | /* ARM $3.4 */ | |
6041 | /* Apparently a lot of autoconf scripts for C++ packages do this, | |
c1771a20 | 6042 | so only complain if -Wpedantic. */ |
93c0e0bb | 6043 | if (complain & (flag_pedantic_errors ? tf_error : tf_warning)) |
c1771a20 | 6044 | pedwarn (input_location, OPT_Wpedantic, |
93c0e0bb JM |
6045 | "ISO C++ forbids taking address of function %<::main%>"); |
6046 | else if (flag_pedantic_errors) | |
6047 | return error_mark_node; | |
6048 | } | |
6049 | ||
6050 | /* Let &* cancel out to simplify resulting code. */ | |
591cb3cf | 6051 | if (INDIRECT_REF_P (arg)) |
93c0e0bb | 6052 | { |
93c0e0bb | 6053 | arg = TREE_OPERAND (arg, 0); |
9f613f06 | 6054 | if (TYPE_REF_P (TREE_TYPE (arg))) |
93c0e0bb JM |
6055 | { |
6056 | tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg))); | |
6057 | arg = build1 (CONVERT_EXPR, type, arg); | |
6058 | } | |
6059 | else | |
6060 | /* Don't let this be an lvalue. */ | |
6061 | arg = rvalue (arg); | |
6062 | return arg; | |
6063 | } | |
6064 | ||
93c0e0bb JM |
6065 | /* Handle complex lvalues (when permitted) |
6066 | by reduction to simpler cases. */ | |
6067 | val = unary_complex_lvalue (ADDR_EXPR, arg); | |
6068 | if (val != 0) | |
6069 | return val; | |
6070 | ||
6071 | switch (TREE_CODE (arg)) | |
6072 | { | |
6073 | CASE_CONVERT: | |
6074 | case FLOAT_EXPR: | |
6075 | case FIX_TRUNC_EXPR: | |
bb19d4af JM |
6076 | /* We should have handled this above in the lvalue_kind check. */ |
6077 | gcc_unreachable (); | |
93c0e0bb JM |
6078 | break; |
6079 | ||
6080 | case BASELINK: | |
6081 | arg = BASELINK_FUNCTIONS (arg); | |
6082 | /* Fall through. */ | |
6083 | ||
6084 | case OVERLOAD: | |
848bf88d | 6085 | arg = OVL_FIRST (arg); |
93c0e0bb JM |
6086 | break; |
6087 | ||
6088 | case OFFSET_REF: | |
6089 | offset_ref: | |
6090 | /* Turn a reference to a non-static data member into a | |
6091 | pointer-to-member. */ | |
6092 | { | |
6093 | tree type; | |
6094 | tree t; | |
6095 | ||
6096 | gcc_assert (PTRMEM_OK_P (arg)); | |
6097 | ||
6098 | t = TREE_OPERAND (arg, 1); | |
9f613f06 | 6099 | if (TYPE_REF_P (TREE_TYPE (t))) |
93c0e0bb JM |
6100 | { |
6101 | if (complain & tf_error) | |
6102 | error ("cannot create pointer to reference member %qD", t); | |
6103 | return error_mark_node; | |
6104 | } | |
6105 | ||
6106 | type = build_ptrmem_type (context_for_name_lookup (t), | |
6107 | TREE_TYPE (t)); | |
6108 | t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1)); | |
6109 | return t; | |
6110 | } | |
6111 | ||
6112 | default: | |
6113 | break; | |
6114 | } | |
6115 | ||
6116 | if (argtype != error_mark_node) | |
94a073b2 | 6117 | argtype = build_pointer_type (argtype); |
93c0e0bb | 6118 | |
47e5d7cc JM |
6119 | if (bitfield_p (arg)) |
6120 | { | |
6121 | if (complain & tf_error) | |
6122 | error ("attempt to take address of bit-field"); | |
6123 | return error_mark_node; | |
6124 | } | |
6125 | ||
93c0e0bb JM |
6126 | /* In a template, we are processing a non-dependent expression |
6127 | so we can just form an ADDR_EXPR with the correct type. */ | |
6128 | if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF) | |
6129 | { | |
dfd7fdca DM |
6130 | tree stripped_arg = tree_strip_any_location_wrapper (arg); |
6131 | if (TREE_CODE (stripped_arg) == FUNCTION_DECL | |
6132 | && !mark_used (stripped_arg, complain) && !(complain & tf_error)) | |
24d21a0b | 6133 | return error_mark_node; |
93c0e0bb JM |
6134 | val = build_address (arg); |
6135 | if (TREE_CODE (arg) == OFFSET_REF) | |
6136 | PTRMEM_OK_P (val) = PTRMEM_OK_P (arg); | |
6137 | } | |
c5ce25ce | 6138 | else if (BASELINK_P (TREE_OPERAND (arg, 1))) |
93c0e0bb JM |
6139 | { |
6140 | tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1)); | |
6141 | ||
6142 | /* We can only get here with a single static member | |
6143 | function. */ | |
6144 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL | |
6145 | && DECL_STATIC_FUNCTION_P (fn)); | |
9f635aba PC |
6146 | if (!mark_used (fn, complain) && !(complain & tf_error)) |
6147 | return error_mark_node; | |
93c0e0bb JM |
6148 | val = build_address (fn); |
6149 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0))) | |
6150 | /* Do not lose object's side effects. */ | |
6151 | val = build2 (COMPOUND_EXPR, TREE_TYPE (val), | |
6152 | TREE_OPERAND (arg, 0), val); | |
6153 | } | |
93c0e0bb JM |
6154 | else |
6155 | { | |
6156 | tree object = TREE_OPERAND (arg, 0); | |
6157 | tree field = TREE_OPERAND (arg, 1); | |
6158 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
6159 | (TREE_TYPE (object), decl_type_context (field))); | |
6160 | val = build_address (arg); | |
6161 | } | |
6162 | ||
50e10fa8 | 6163 | if (TYPE_PTR_P (argtype) |
93c0e0bb JM |
6164 | && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE) |
6165 | { | |
6166 | build_ptrmemfunc_type (argtype); | |
6167 | val = build_ptrmemfunc (argtype, val, 0, | |
6168 | /*c_cast_p=*/false, | |
a3299120 | 6169 | complain); |
93c0e0bb JM |
6170 | } |
6171 | ||
6172 | return val; | |
6173 | } | |
6174 | ||
6175 | /* Take the address of ARG if it has one, even if it's an rvalue. */ | |
6176 | ||
6177 | tree | |
6178 | cp_build_addr_expr (tree arg, tsubst_flags_t complain) | |
6179 | { | |
6180 | return cp_build_addr_expr_1 (arg, 0, complain); | |
6181 | } | |
6182 | ||
6183 | /* Take the address of ARG, but only if it's an lvalue. */ | |
6184 | ||
882de214 | 6185 | static tree |
93c0e0bb JM |
6186 | cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain) |
6187 | { | |
6188 | return cp_build_addr_expr_1 (arg, 1, complain); | |
6189 | } | |
6190 | ||
8d08fdba MS |
6191 | /* C++: Must handle pointers to members. |
6192 | ||
6193 | Perhaps type instantiation should be extended to handle conversion | |
6194 | from aggregates to types we don't yet know we want? (Or are those | |
6195 | cases typically errors which should be reported?) | |
6196 | ||
e51fbec3 | 6197 | NOCONVERT suppresses the default promotions (such as from short to int). */ |
e92cc029 | 6198 | |
8d08fdba | 6199 | tree |
e51fbec3 | 6200 | cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert, |
5ade1ed2 | 6201 | tsubst_flags_t complain) |
8d08fdba MS |
6202 | { |
6203 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
926ce8bd | 6204 | tree arg = xarg; |
6bdfada4 | 6205 | location_t location = cp_expr_loc_or_loc (arg, input_location); |
926ce8bd | 6206 | tree argtype = 0; |
d8e178a0 | 6207 | const char *errstring = NULL; |
8d08fdba | 6208 | tree val; |
4de67c26 | 6209 | const char *invalid_op_diag; |
8d08fdba | 6210 | |
fb80065c | 6211 | if (!arg || error_operand_p (arg)) |
8d08fdba MS |
6212 | return error_mark_node; |
6213 | ||
4de67c26 JM |
6214 | if ((invalid_op_diag |
6215 | = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR | |
6216 | ? CONVERT_EXPR | |
6217 | : code), | |
6218 | TREE_TYPE (xarg)))) | |
6219 | { | |
76545796 PC |
6220 | if (complain & tf_error) |
6221 | error (invalid_op_diag); | |
4de67c26 JM |
6222 | return error_mark_node; |
6223 | } | |
6224 | ||
8d08fdba MS |
6225 | switch (code) |
6226 | { | |
392e3d51 | 6227 | case UNARY_PLUS_EXPR: |
b7484fbe | 6228 | case NEGATE_EXPR: |
8a21aa30 KC |
6229 | { |
6230 | int flags = WANT_ARITH | WANT_ENUM; | |
6231 | /* Unary plus (but not unary minus) is allowed on pointers. */ | |
392e3d51 | 6232 | if (code == UNARY_PLUS_EXPR) |
8a21aa30 KC |
6233 | flags |= WANT_POINTER; |
6234 | arg = build_expr_type_conversion (flags, arg, true); | |
e99f332f GB |
6235 | if (!arg) |
6236 | errstring = (code == NEGATE_EXPR | |
4cd5a50a PB |
6237 | ? _("wrong type argument to unary minus") |
6238 | : _("wrong type argument to unary plus")); | |
e99f332f GB |
6239 | else |
6240 | { | |
6241 | if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg))) | |
4b978f96 | 6242 | arg = cp_perform_integral_promotions (arg, complain); |
e99f332f | 6243 | |
13a44ee0 | 6244 | /* Make sure the result is not an lvalue: a unary plus or minus |
e99f332f | 6245 | expression is always a rvalue. */ |
5cc53d4e | 6246 | arg = rvalue (arg); |
e99f332f GB |
6247 | } |
6248 | } | |
8d08fdba MS |
6249 | break; |
6250 | ||
6251 | case BIT_NOT_EXPR: | |
37c46b43 MS |
6252 | if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) |
6253 | { | |
6254 | code = CONJ_EXPR; | |
6255 | if (!noconvert) | |
89fcabaf PC |
6256 | { |
6257 | arg = cp_default_conversion (arg, complain); | |
6258 | if (arg == error_mark_node) | |
6259 | return error_mark_node; | |
6260 | } | |
37c46b43 | 6261 | } |
4576ceaf | 6262 | else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM |
1ff6b2c8 | 6263 | | WANT_VECTOR_OR_COMPLEX, |
b746c5dc | 6264 | arg, true))) |
4cd5a50a | 6265 | errstring = _("wrong type argument to bit-complement"); |
bd289f54 | 6266 | else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg))) |
9a2300e9 MP |
6267 | { |
6268 | /* Warn if the expression has boolean value. */ | |
6269 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE | |
8dc9277a | 6270 | && (complain & tf_warning) |
9a2300e9 MP |
6271 | && warning_at (location, OPT_Wbool_operation, |
6272 | "%<~%> on an expression of type bool")) | |
6273 | inform (location, "did you mean to use logical not (%<!%>)?"); | |
6274 | arg = cp_perform_integral_promotions (arg, complain); | |
6275 | } | |
abec4284 JJ |
6276 | else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg))) |
6277 | arg = mark_rvalue_use (arg); | |
8d08fdba MS |
6278 | break; |
6279 | ||
6280 | case ABS_EXPR: | |
b746c5dc | 6281 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) |
4cd5a50a | 6282 | errstring = _("wrong type argument to abs"); |
8d08fdba | 6283 | else if (!noconvert) |
89fcabaf PC |
6284 | { |
6285 | arg = cp_default_conversion (arg, complain); | |
6286 | if (arg == error_mark_node) | |
6287 | return error_mark_node; | |
6288 | } | |
8d08fdba MS |
6289 | break; |
6290 | ||
37c46b43 MS |
6291 | case CONJ_EXPR: |
6292 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
b746c5dc | 6293 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) |
4cd5a50a | 6294 | errstring = _("wrong type argument to conjugation"); |
37c46b43 | 6295 | else if (!noconvert) |
89fcabaf PC |
6296 | { |
6297 | arg = cp_default_conversion (arg, complain); | |
6298 | if (arg == error_mark_node) | |
6299 | return error_mark_node; | |
6300 | } | |
37c46b43 MS |
6301 | break; |
6302 | ||
8d08fdba | 6303 | case TRUTH_NOT_EXPR: |
48d3af12 | 6304 | if (VECTOR_TYPE_P (TREE_TYPE (arg))) |
3c9aabbd MG |
6305 | return cp_build_binary_op (input_location, EQ_EXPR, arg, |
6306 | build_zero_cst (TREE_TYPE (arg)), complain); | |
5ade1ed2 DG |
6307 | arg = perform_implicit_conversion (boolean_type_node, arg, |
6308 | complain); | |
db3927fb | 6309 | val = invert_truthvalue_loc (input_location, arg); |
2986ae00 MS |
6310 | if (arg != error_mark_node) |
6311 | return val; | |
4cd5a50a | 6312 | errstring = _("in argument to unary !"); |
8d08fdba MS |
6313 | break; |
6314 | ||
6315 | case NOP_EXPR: | |
6316 | break; | |
c8094d83 | 6317 | |
37c46b43 | 6318 | case REALPART_EXPR: |
37c46b43 | 6319 | case IMAGPART_EXPR: |
fb52b50a | 6320 | arg = build_real_imag_expr (input_location, code, arg); |
cda0a029 | 6321 | return arg; |
c8094d83 | 6322 | |
8d08fdba MS |
6323 | case PREINCREMENT_EXPR: |
6324 | case POSTINCREMENT_EXPR: | |
6325 | case PREDECREMENT_EXPR: | |
6326 | case POSTDECREMENT_EXPR: | |
6327 | /* Handle complex lvalues (when permitted) | |
6328 | by reduction to simpler cases. */ | |
6329 | ||
6330 | val = unary_complex_lvalue (code, arg); | |
6331 | if (val != 0) | |
6332 | return val; | |
6333 | ||
03a904b5 JJ |
6334 | arg = mark_lvalue_use (arg); |
6335 | ||
37c46b43 MS |
6336 | /* Increment or decrement the real part of the value, |
6337 | and don't change the imaginary part. */ | |
6338 | if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
6339 | { | |
6340 | tree real, imag; | |
6341 | ||
5a0802ea | 6342 | arg = cp_stabilize_reference (arg); |
e51fbec3 MP |
6343 | real = cp_build_unary_op (REALPART_EXPR, arg, true, complain); |
6344 | imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain); | |
6345 | real = cp_build_unary_op (code, real, true, complain); | |
a87db577 AP |
6346 | if (real == error_mark_node || imag == error_mark_node) |
6347 | return error_mark_node; | |
f293ce4b | 6348 | return build2 (COMPLEX_EXPR, TREE_TYPE (arg), |
a87db577 | 6349 | real, imag); |
37c46b43 MS |
6350 | } |
6351 | ||
8d08fdba MS |
6352 | /* Report invalid types. */ |
6353 | ||
b7484fbe | 6354 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER, |
b746c5dc | 6355 | arg, true))) |
8d08fdba MS |
6356 | { |
6357 | if (code == PREINCREMENT_EXPR) | |
4cd5a50a | 6358 | errstring = _("no pre-increment operator for type"); |
8d08fdba | 6359 | else if (code == POSTINCREMENT_EXPR) |
4cd5a50a | 6360 | errstring = _("no post-increment operator for type"); |
8d08fdba | 6361 | else if (code == PREDECREMENT_EXPR) |
4cd5a50a | 6362 | errstring = _("no pre-decrement operator for type"); |
8d08fdba | 6363 | else |
4cd5a50a | 6364 | errstring = _("no post-decrement operator for type"); |
8d08fdba MS |
6365 | break; |
6366 | } | |
affb3cb2 JJ |
6367 | else if (arg == error_mark_node) |
6368 | return error_mark_node; | |
8d08fdba MS |
6369 | |
6370 | /* Report something read-only. */ | |
6371 | ||
91063b51 | 6372 | if (CP_TYPE_CONST_P (TREE_TYPE (arg)) |
5ade1ed2 DG |
6373 | || TREE_READONLY (arg)) |
6374 | { | |
6375 | if (complain & tf_error) | |
d4714a1b JJ |
6376 | cxx_readonly_error (location, arg, |
6377 | ((code == PREINCREMENT_EXPR | |
6378 | || code == POSTINCREMENT_EXPR) | |
6379 | ? lv_increment : lv_decrement)); | |
5ade1ed2 DG |
6380 | else |
6381 | return error_mark_node; | |
6382 | } | |
8d08fdba MS |
6383 | |
6384 | { | |
926ce8bd | 6385 | tree inc; |
b70cef5d | 6386 | tree declared_type = unlowered_expr_type (arg); |
8d08fdba | 6387 | |
8d08fdba MS |
6388 | argtype = TREE_TYPE (arg); |
6389 | ||
6390 | /* ARM $5.2.5 last annotation says this should be forbidden. */ | |
6391 | if (TREE_CODE (argtype) == ENUMERAL_TYPE) | |
5ade1ed2 DG |
6392 | { |
6393 | if (complain & tf_error) | |
cbe5f3b3 | 6394 | permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
37ec60ed JW |
6395 | ? G_("ISO C++ forbids incrementing an enum") |
6396 | : G_("ISO C++ forbids decrementing an enum")); | |
5ade1ed2 DG |
6397 | else |
6398 | return error_mark_node; | |
6399 | } | |
c8094d83 | 6400 | |
8d08fdba MS |
6401 | /* Compute the increment. */ |
6402 | ||
50e10fa8 | 6403 | if (TYPE_PTR_P (argtype)) |
8d08fdba | 6404 | { |
d0f062fb | 6405 | tree type = complete_type (TREE_TYPE (argtype)); |
c8094d83 | 6406 | |
d0f062fb | 6407 | if (!COMPLETE_OR_VOID_TYPE_P (type)) |
5ade1ed2 DG |
6408 | { |
6409 | if (complain & tf_error) | |
6410 | error (((code == PREINCREMENT_EXPR | |
6411 | || code == POSTINCREMENT_EXPR)) | |
6412 | ? G_("cannot increment a pointer to incomplete type %qT") | |
6413 | : G_("cannot decrement a pointer to incomplete type %qT"), | |
6414 | TREE_TYPE (argtype)); | |
6415 | else | |
6416 | return error_mark_node; | |
6417 | } | |
c42a832a | 6418 | else if (!TYPE_PTROB_P (argtype)) |
5ade1ed2 DG |
6419 | { |
6420 | if (complain & tf_error) | |
44d90fe1 | 6421 | pedwarn (input_location, OPT_Wpointer_arith, |
c42a832a | 6422 | (code == PREINCREMENT_EXPR |
37ec60ed | 6423 | || code == POSTINCREMENT_EXPR) |
c42a832a PC |
6424 | ? G_("ISO C++ forbids incrementing a pointer of type %qT") |
6425 | : G_("ISO C++ forbids decrementing a pointer of type %qT"), | |
6426 | argtype); | |
5ade1ed2 DG |
6427 | else |
6428 | return error_mark_node; | |
6429 | } | |
6430 | ||
ea793912 | 6431 | inc = cxx_sizeof_nowarn (TREE_TYPE (argtype)); |
8d08fdba MS |
6432 | } |
6433 | else | |
241b71bb TV |
6434 | inc = VECTOR_TYPE_P (argtype) |
6435 | ? build_one_cst (argtype) | |
6436 | : integer_one_node; | |
8d08fdba | 6437 | |
4b978f96 | 6438 | inc = cp_convert (argtype, inc, complain); |
8d08fdba | 6439 | |
925e8657 NP |
6440 | /* If 'arg' is an Objective-C PROPERTY_REF expression, then we |
6441 | need to ask Objective-C to build the increment or decrement | |
6442 | expression for it. */ | |
6443 | if (objc_is_property_ref (arg)) | |
6444 | return objc_build_incr_expr_for_property_ref (input_location, code, | |
6445 | arg, inc); | |
6446 | ||
8d08fdba MS |
6447 | /* Complain about anything else that is not a true lvalue. */ |
6448 | if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR | |
6449 | || code == POSTINCREMENT_EXPR) | |
5ade1ed2 DG |
6450 | ? lv_increment : lv_decrement), |
6451 | complain)) | |
8d08fdba MS |
6452 | return error_mark_node; |
6453 | ||
20a1e5b8 | 6454 | /* Forbid using -- or ++ in C++17 on `bool'. */ |
66be89f0 | 6455 | if (TREE_CODE (declared_type) == BOOLEAN_TYPE) |
b7484fbe MS |
6456 | { |
6457 | if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR) | |
6458 | { | |
5ade1ed2 | 6459 | if (complain & tf_error) |
20a1e5b8 MP |
6460 | error ("use of an operand of type %qT in %<operator--%> " |
6461 | "is forbidden", boolean_type_node); | |
b7484fbe MS |
6462 | return error_mark_node; |
6463 | } | |
20a1e5b8 MP |
6464 | else |
6465 | { | |
7b936140 | 6466 | if (cxx_dialect >= cxx17) |
20a1e5b8 MP |
6467 | { |
6468 | if (complain & tf_error) | |
6469 | error ("use of an operand of type %qT in " | |
7b936140 | 6470 | "%<operator++%> is forbidden in C++17", |
20a1e5b8 MP |
6471 | boolean_type_node); |
6472 | return error_mark_node; | |
6473 | } | |
6474 | /* Otherwise, [depr.incr.bool] says this is deprecated. */ | |
6475 | else if (!in_system_header_at (input_location)) | |
6476 | warning (OPT_Wdeprecated, "use of an operand of type %qT " | |
6477 | "in %<operator++%> is deprecated", | |
6478 | boolean_type_node); | |
6479 | } | |
19552aa5 | 6480 | val = boolean_increment (code, arg); |
b7484fbe | 6481 | } |
9127e994 JM |
6482 | else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR) |
6483 | /* An rvalue has no cv-qualifiers. */ | |
6484 | val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc); | |
b7484fbe | 6485 | else |
f293ce4b | 6486 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
b7484fbe | 6487 | |
8d08fdba | 6488 | TREE_SIDE_EFFECTS (val) = 1; |
b70cef5d | 6489 | return val; |
8d08fdba MS |
6490 | } |
6491 | ||
6492 | case ADDR_EXPR: | |
6493 | /* Note that this operation never does default_conversion | |
6494 | regardless of NOCONVERT. */ | |
93c0e0bb | 6495 | return cp_build_addr_expr (arg, complain); |
7f85441b KG |
6496 | |
6497 | default: | |
6498 | break; | |
8d08fdba MS |
6499 | } |
6500 | ||
6501 | if (!errstring) | |
6502 | { | |
6503 | if (argtype == 0) | |
6504 | argtype = TREE_TYPE (arg); | |
cda0a029 | 6505 | return build1 (code, argtype, arg); |
8d08fdba MS |
6506 | } |
6507 | ||
5ade1ed2 DG |
6508 | if (complain & tf_error) |
6509 | error ("%s", errstring); | |
8d08fdba MS |
6510 | return error_mark_node; |
6511 | } | |
6512 | ||
5ade1ed2 DG |
6513 | /* Hook for the c-common bits that build a unary op. */ |
6514 | tree | |
12308bc6 | 6515 | build_unary_op (location_t /*location*/, |
e51fbec3 | 6516 | enum tree_code code, tree xarg, bool noconvert) |
5ade1ed2 DG |
6517 | { |
6518 | return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error); | |
6519 | } | |
6520 | ||
57e20f74 JJ |
6521 | /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR, |
6522 | so that it is a valid lvalue even for GENERIC by replacing | |
6523 | (lhs = rhs) with ((lhs = rhs), lhs) | |
6524 | (--lhs) with ((--lhs), lhs) | |
6525 | (++lhs) with ((++lhs), lhs) | |
6526 | and if lhs has side-effects, calling cp_stabilize_reference on it, so | |
6527 | that it can be evaluated multiple times. */ | |
6528 | ||
6529 | tree | |
6530 | genericize_compound_lvalue (tree lvalue) | |
6531 | { | |
6532 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0))) | |
6533 | lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue), | |
6534 | cp_stabilize_reference (TREE_OPERAND (lvalue, 0)), | |
6535 | TREE_OPERAND (lvalue, 1)); | |
6536 | return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)), | |
6537 | lvalue, TREE_OPERAND (lvalue, 0)); | |
6538 | } | |
6539 | ||
8d08fdba MS |
6540 | /* Apply unary lvalue-demanding operator CODE to the expression ARG |
6541 | for certain kinds of expressions which are not really lvalues | |
6542 | but which we can accept as lvalues. | |
6543 | ||
dfb5c523 MM |
6544 | If ARG is not a kind of expression we can handle, return |
6545 | NULL_TREE. */ | |
c8094d83 | 6546 | |
8d08fdba | 6547 | tree |
acd8e2d0 | 6548 | unary_complex_lvalue (enum tree_code code, tree arg) |
8d08fdba | 6549 | { |
dfb5c523 MM |
6550 | /* Inside a template, making these kinds of adjustments is |
6551 | pointless; we are only concerned with the type of the | |
6552 | expression. */ | |
6553 | if (processing_template_decl) | |
6554 | return NULL_TREE; | |
6555 | ||
8d08fdba MS |
6556 | /* Handle (a, b) used as an "lvalue". */ |
6557 | if (TREE_CODE (arg) == COMPOUND_EXPR) | |
6558 | { | |
e51fbec3 | 6559 | tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false, |
5ade1ed2 | 6560 | tf_warning_or_error); |
f293ce4b RS |
6561 | return build2 (COMPOUND_EXPR, TREE_TYPE (real_result), |
6562 | TREE_OPERAND (arg, 0), real_result); | |
8d08fdba MS |
6563 | } |
6564 | ||
6565 | /* Handle (a ? b : c) used as an "lvalue". */ | |
e9a25f70 JL |
6566 | if (TREE_CODE (arg) == COND_EXPR |
6567 | || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR) | |
5ade1ed2 | 6568 | return rationalize_conditional_expr (code, arg, tf_warning_or_error); |
a4443a08 | 6569 | |
ae818d3b | 6570 | /* Handle (a = b), (++a), and (--a) used as an "lvalue". */ |
e1cd6e56 MS |
6571 | if (TREE_CODE (arg) == MODIFY_EXPR |
6572 | || TREE_CODE (arg) == PREINCREMENT_EXPR | |
6573 | || TREE_CODE (arg) == PREDECREMENT_EXPR) | |
57e20f74 | 6574 | return unary_complex_lvalue (code, genericize_compound_lvalue (arg)); |
8d08fdba MS |
6575 | |
6576 | if (code != ADDR_EXPR) | |
33fd4f49 | 6577 | return NULL_TREE; |
8d08fdba MS |
6578 | |
6579 | /* Handle (a = b) used as an "lvalue" for `&'. */ | |
6580 | if (TREE_CODE (arg) == MODIFY_EXPR | |
6581 | || TREE_CODE (arg) == INIT_EXPR) | |
6582 | { | |
e51fbec3 | 6583 | tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false, |
5ade1ed2 | 6584 | tf_warning_or_error); |
f293ce4b RS |
6585 | arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result), |
6586 | arg, real_result); | |
6de9cd9a | 6587 | TREE_NO_WARNING (arg) = 1; |
faf5394a | 6588 | return arg; |
8d08fdba MS |
6589 | } |
6590 | ||
8d08fdba MS |
6591 | if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE |
6592 | || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE | |
a5ac359a | 6593 | || TREE_CODE (arg) == OFFSET_REF) |
6d05585b | 6594 | return NULL_TREE; |
c8094d83 | 6595 | |
8d08fdba MS |
6596 | /* We permit compiler to make function calls returning |
6597 | objects of aggregate type look like lvalues. */ | |
6598 | { | |
6599 | tree targ = arg; | |
6600 | ||
6601 | if (TREE_CODE (targ) == SAVE_EXPR) | |
6602 | targ = TREE_OPERAND (targ, 0); | |
6603 | ||
9e1e64ec | 6604 | if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ))) |
8d08fdba MS |
6605 | { |
6606 | if (TREE_CODE (arg) == SAVE_EXPR) | |
6607 | targ = arg; | |
6608 | else | |
362115a9 | 6609 | targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error); |
f30432d7 | 6610 | return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ); |
8d08fdba MS |
6611 | } |
6612 | ||
591cb3cf | 6613 | if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ)) |
f293ce4b | 6614 | return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)), |
8d08fdba | 6615 | TREE_OPERAND (targ, 0), current_function_decl, NULL); |
8d08fdba MS |
6616 | } |
6617 | ||
6618 | /* Don't let anything else be handled specially. */ | |
33fd4f49 | 6619 | return NULL_TREE; |
8d08fdba | 6620 | } |
8d08fdba MS |
6621 | \f |
6622 | /* Mark EXP saying that we need to be able to take the | |
6623 | address of it; it should not be allocated in a register. | |
a9e4a1a5 JJ |
6624 | Value is true if successful. ARRAY_REF_P is true if this |
6625 | is for ARRAY_REF construction - in that case we don't want | |
6626 | to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE, | |
6627 | it is fine to use ARRAY_REFs for vector subscripts on vector | |
6628 | register variables. | |
8d08fdba | 6629 | |
4ac14744 | 6630 | C++: we do not allow `current_class_ptr' to be addressable. */ |
8d08fdba | 6631 | |
dffd7eb6 | 6632 | bool |
a9e4a1a5 | 6633 | cxx_mark_addressable (tree exp, bool array_ref_p) |
8d08fdba | 6634 | { |
926ce8bd | 6635 | tree x = exp; |
8d08fdba | 6636 | |
8d08fdba MS |
6637 | while (1) |
6638 | switch (TREE_CODE (x)) | |
6639 | { | |
a9e4a1a5 JJ |
6640 | case VIEW_CONVERT_EXPR: |
6641 | if (array_ref_p | |
6642 | && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE | |
6643 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0)))) | |
6644 | return true; | |
6645 | /* FALLTHRU */ | |
8d08fdba MS |
6646 | case ADDR_EXPR: |
6647 | case COMPONENT_REF: | |
6648 | case ARRAY_REF: | |
37c46b43 MS |
6649 | case REALPART_EXPR: |
6650 | case IMAGPART_EXPR: | |
8d08fdba MS |
6651 | x = TREE_OPERAND (x, 0); |
6652 | break; | |
6653 | ||
6654 | case PARM_DECL: | |
4ac14744 | 6655 | if (x == current_class_ptr) |
8d08fdba | 6656 | { |
0cbd7506 | 6657 | error ("cannot take the address of %<this%>, which is an rvalue expression"); |
f4f206f4 | 6658 | TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */ |
dffd7eb6 | 6659 | return true; |
8d08fdba | 6660 | } |
f4f206f4 | 6661 | /* Fall through. */ |
aa8dea09 | 6662 | |
8d08fdba | 6663 | case VAR_DECL: |
8d08fdba MS |
6664 | /* Caller should not be trying to mark initialized |
6665 | constant fields addressable. */ | |
50bc768d NS |
6666 | gcc_assert (DECL_LANG_SPECIFIC (x) == 0 |
6667 | || DECL_IN_AGGR_P (x) == 0 | |
6668 | || TREE_STATIC (x) | |
6669 | || DECL_EXTERNAL (x)); | |
f4f206f4 | 6670 | /* Fall through. */ |
8d08fdba | 6671 | |
8d08fdba | 6672 | case RESULT_DECL: |
9a3b49ac | 6673 | if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) |
1b8d7c9a | 6674 | && !DECL_ARTIFICIAL (x)) |
8ef36086 | 6675 | { |
5a6ccc94 | 6676 | if (VAR_P (x) && DECL_HARD_REGISTER (x)) |
8ef36086 AP |
6677 | { |
6678 | error | |
6679 | ("address of explicit register variable %qD requested", x); | |
6680 | return false; | |
6681 | } | |
778f6a08 | 6682 | else if (extra_warnings) |
8ef36086 | 6683 | warning |
b323323f | 6684 | (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x); |
8ef36086 | 6685 | } |
8d08fdba | 6686 | TREE_ADDRESSABLE (x) = 1; |
dffd7eb6 | 6687 | return true; |
8d08fdba | 6688 | |
81c1113a | 6689 | case CONST_DECL: |
8d08fdba | 6690 | case FUNCTION_DECL: |
8d08fdba | 6691 | TREE_ADDRESSABLE (x) = 1; |
dffd7eb6 | 6692 | return true; |
8d08fdba | 6693 | |
e92cc029 MS |
6694 | case CONSTRUCTOR: |
6695 | TREE_ADDRESSABLE (x) = 1; | |
dffd7eb6 | 6696 | return true; |
e92cc029 | 6697 | |
9a3b49ac MS |
6698 | case TARGET_EXPR: |
6699 | TREE_ADDRESSABLE (x) = 1; | |
dffd7eb6 NB |
6700 | cxx_mark_addressable (TREE_OPERAND (x, 0)); |
6701 | return true; | |
9a3b49ac | 6702 | |
8d08fdba | 6703 | default: |
dffd7eb6 | 6704 | return true; |
8d08fdba MS |
6705 | } |
6706 | } | |
6707 | \f | |
6708 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. */ | |
6709 | ||
6710 | tree | |
f330f599 | 6711 | build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2, |
5ade1ed2 | 6712 | tsubst_flags_t complain) |
8d08fdba | 6713 | { |
d17811fd MM |
6714 | tree orig_ifexp = ifexp; |
6715 | tree orig_op1 = op1; | |
6716 | tree orig_op2 = op2; | |
6717 | tree expr; | |
6718 | ||
5156628f | 6719 | if (processing_template_decl) |
d17811fd MM |
6720 | { |
6721 | /* The standard says that the expression is type-dependent if | |
6722 | IFEXP is type-dependent, even though the eventual type of the | |
6723 | expression doesn't dependent on IFEXP. */ | |
6724 | if (type_dependent_expression_p (ifexp) | |
18fd68a8 MM |
6725 | /* As a GNU extension, the middle operand may be omitted. */ |
6726 | || (op1 && type_dependent_expression_p (op1)) | |
d17811fd | 6727 | || type_dependent_expression_p (op2)) |
f330f599 | 6728 | return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2); |
d17811fd | 6729 | ifexp = build_non_dependent_expr (ifexp); |
18fd68a8 MM |
6730 | if (op1) |
6731 | op1 = build_non_dependent_expr (op1); | |
d17811fd MM |
6732 | op2 = build_non_dependent_expr (op2); |
6733 | } | |
5566b478 | 6734 | |
4cbc4bd7 | 6735 | expr = build_conditional_expr (loc, ifexp, op1, op2, complain); |
f2111a36 | 6736 | if (processing_template_decl && expr != error_mark_node) |
ca8dc274 JM |
6737 | { |
6738 | tree min = build_min_non_dep (COND_EXPR, expr, | |
6739 | orig_ifexp, orig_op1, orig_op2); | |
ca8dc274 JM |
6740 | expr = convert_from_reference (min); |
6741 | } | |
d17811fd | 6742 | return expr; |
8d08fdba MS |
6743 | } |
6744 | \f | |
c7b62f14 | 6745 | /* Given a list of expressions, return a compound expression |
04c06002 | 6746 | that performs them all and returns the value of the last of them. */ |
c7b62f14 | 6747 | |
2f5b91f5 | 6748 | tree |
d555b1c7 PC |
6749 | build_x_compound_expr_from_list (tree list, expr_list_kind exp, |
6750 | tsubst_flags_t complain) | |
c7b62f14 NS |
6751 | { |
6752 | tree expr = TREE_VALUE (list); | |
c8094d83 | 6753 | |
d3de8016 JM |
6754 | if (BRACE_ENCLOSED_INITIALIZER_P (expr) |
6755 | && !CONSTRUCTOR_IS_DIRECT_INIT (expr)) | |
6756 | { | |
6757 | if (complain & tf_error) | |
6bdfada4 | 6758 | pedwarn (cp_expr_loc_or_loc (expr, input_location), 0, |
8400e75e DM |
6759 | "list-initializer for non-class type must not " |
6760 | "be parenthesized"); | |
d3de8016 JM |
6761 | else |
6762 | return error_mark_node; | |
6763 | } | |
6764 | ||
c7b62f14 NS |
6765 | if (TREE_CHAIN (list)) |
6766 | { | |
d555b1c7 PC |
6767 | if (complain & tf_error) |
6768 | switch (exp) | |
6769 | { | |
2f5b91f5 SZ |
6770 | case ELK_INIT: |
6771 | permerror (input_location, "expression list treated as compound " | |
6772 | "expression in initializer"); | |
6773 | break; | |
6774 | case ELK_MEM_INIT: | |
6775 | permerror (input_location, "expression list treated as compound " | |
6776 | "expression in mem-initializer"); | |
6777 | break; | |
6778 | case ELK_FUNC_CAST: | |
6779 | permerror (input_location, "expression list treated as compound " | |
6780 | "expression in functional cast"); | |
6781 | break; | |
6782 | default: | |
6783 | gcc_unreachable (); | |
d555b1c7 | 6784 | } |
6dab6cf1 JM |
6785 | else |
6786 | return error_mark_node; | |
c7b62f14 NS |
6787 | |
6788 | for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list)) | |
f330f599 PC |
6789 | expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)), |
6790 | expr, TREE_VALUE (list), complain); | |
c7b62f14 | 6791 | } |
c8094d83 | 6792 | |
c7b62f14 NS |
6793 | return expr; |
6794 | } | |
6795 | ||
c166b898 ILT |
6796 | /* Like build_x_compound_expr_from_list, but using a VEC. */ |
6797 | ||
6798 | tree | |
9771b263 | 6799 | build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg, |
e2e03032 | 6800 | tsubst_flags_t complain) |
c166b898 | 6801 | { |
9771b263 | 6802 | if (vec_safe_is_empty (vec)) |
c166b898 | 6803 | return NULL_TREE; |
9771b263 DN |
6804 | else if (vec->length () == 1) |
6805 | return (*vec)[0]; | |
c166b898 ILT |
6806 | else |
6807 | { | |
6808 | tree expr; | |
6809 | unsigned int ix; | |
6810 | tree t; | |
6811 | ||
6812 | if (msg != NULL) | |
e2e03032 PC |
6813 | { |
6814 | if (complain & tf_error) | |
6815 | permerror (input_location, | |
6816 | "%s expression list treated as compound expression", | |
6817 | msg); | |
6818 | else | |
6819 | return error_mark_node; | |
6820 | } | |
c166b898 | 6821 | |
9771b263 DN |
6822 | expr = (*vec)[0]; |
6823 | for (ix = 1; vec->iterate (ix, &t); ++ix) | |
f330f599 | 6824 | expr = build_x_compound_expr (EXPR_LOCATION (t), expr, |
e2e03032 | 6825 | t, complain); |
c166b898 ILT |
6826 | |
6827 | return expr; | |
6828 | } | |
6829 | } | |
6830 | ||
c7b62f14 | 6831 | /* Handle overloading of the ',' operator when needed. */ |
e92cc029 | 6832 | |
8d08fdba | 6833 | tree |
f330f599 PC |
6834 | build_x_compound_expr (location_t loc, tree op1, tree op2, |
6835 | tsubst_flags_t complain) | |
8d08fdba | 6836 | { |
8d08fdba | 6837 | tree result; |
d17811fd MM |
6838 | tree orig_op1 = op1; |
6839 | tree orig_op2 = op2; | |
fcb9363e | 6840 | tree overload = NULL_TREE; |
8d08fdba | 6841 | |
5156628f | 6842 | if (processing_template_decl) |
d17811fd MM |
6843 | { |
6844 | if (type_dependent_expression_p (op1) | |
6845 | || type_dependent_expression_p (op2)) | |
f330f599 | 6846 | return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2); |
d17811fd MM |
6847 | op1 = build_non_dependent_expr (op1); |
6848 | op2 = build_non_dependent_expr (op2); | |
6849 | } | |
b19b4a78 | 6850 | |
f330f599 | 6851 | result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, |
fcb9363e | 6852 | NULL_TREE, &overload, complain); |
d17811fd | 6853 | if (!result) |
525521b6 | 6854 | result = cp_build_compound_expr (op1, op2, complain); |
b19b4a78 | 6855 | |
d17811fd | 6856 | if (processing_template_decl && result != error_mark_node) |
fcb9363e PP |
6857 | { |
6858 | if (overload != NULL_TREE) | |
6859 | return (build_min_non_dep_op_overload | |
6860 | (COMPOUND_EXPR, result, overload, orig_op1, orig_op2)); | |
6861 | ||
6862 | return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2); | |
6863 | } | |
c8094d83 | 6864 | |
d17811fd | 6865 | return result; |
8d08fdba MS |
6866 | } |
6867 | ||
525521b6 DG |
6868 | /* Like cp_build_compound_expr, but for the c-common bits. */ |
6869 | ||
6870 | tree | |
12308bc6 | 6871 | build_compound_expr (location_t /*loc*/, tree lhs, tree rhs) |
525521b6 DG |
6872 | { |
6873 | return cp_build_compound_expr (lhs, rhs, tf_warning_or_error); | |
6874 | } | |
6875 | ||
04c06002 | 6876 | /* Build a compound expression. */ |
8d08fdba MS |
6877 | |
6878 | tree | |
525521b6 | 6879 | cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain) |
8d08fdba | 6880 | { |
ebeb2c24 | 6881 | lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain); |
c8094d83 | 6882 | |
c7b62f14 | 6883 | if (lhs == error_mark_node || rhs == error_mark_node) |
66543169 | 6884 | return error_mark_node; |
c8094d83 | 6885 | |
d340e53f NS |
6886 | if (TREE_CODE (rhs) == TARGET_EXPR) |
6887 | { | |
6888 | /* If the rhs is a TARGET_EXPR, then build the compound | |
0cbd7506 | 6889 | expression inside the target_expr's initializer. This |
cd0be382 | 6890 | helps the compiler to eliminate unnecessary temporaries. */ |
d340e53f | 6891 | tree init = TREE_OPERAND (rhs, 1); |
c8094d83 | 6892 | |
f293ce4b | 6893 | init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init); |
d340e53f | 6894 | TREE_OPERAND (rhs, 1) = init; |
c8094d83 | 6895 | |
d340e53f NS |
6896 | return rhs; |
6897 | } | |
c8094d83 | 6898 | |
95e20768 NS |
6899 | if (type_unknown_p (rhs)) |
6900 | { | |
76545796 PC |
6901 | if (complain & tf_error) |
6902 | error ("no context to resolve type of %qE", rhs); | |
95e20768 NS |
6903 | return error_mark_node; |
6904 | } | |
6905 | ||
f293ce4b | 6906 | return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs); |
8d08fdba MS |
6907 | } |
6908 | ||
33c25e5c | 6909 | /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE |
3f89235e JM |
6910 | casts away constness. CAST gives the type of cast. Returns true |
6911 | if the cast is ill-formed, false if it is well-formed. | |
36ef7262 MLI |
6912 | |
6913 | ??? This function warns for casting away any qualifier not just | |
6914 | const. We would like to specify exactly what qualifiers are casted | |
6915 | away. | |
6916 | */ | |
3fe18f1d | 6917 | |
3f89235e | 6918 | static bool |
a5ac359a | 6919 | check_for_casting_away_constness (tree src_type, tree dest_type, |
3f89235e | 6920 | enum tree_code cast, tsubst_flags_t complain) |
3fe18f1d | 6921 | { |
86b7b98b MLI |
6922 | /* C-style casts are allowed to cast away constness. With |
6923 | WARN_CAST_QUAL, we still want to issue a warning. */ | |
6924 | if (cast == CAST_EXPR && !warn_cast_qual) | |
3f89235e | 6925 | return false; |
86b7b98b | 6926 | |
b40e334f | 6927 | if (!casts_away_constness (src_type, dest_type, complain)) |
3f89235e | 6928 | return false; |
36ef7262 MLI |
6929 | |
6930 | switch (cast) | |
6931 | { | |
6932 | case CAST_EXPR: | |
3f89235e JM |
6933 | if (complain & tf_warning) |
6934 | warning (OPT_Wcast_qual, | |
6935 | "cast from type %qT to type %qT casts away qualifiers", | |
6936 | src_type, dest_type); | |
6937 | return false; | |
36ef7262 MLI |
6938 | |
6939 | case STATIC_CAST_EXPR: | |
3f89235e JM |
6940 | if (complain & tf_error) |
6941 | error ("static_cast from type %qT to type %qT casts away qualifiers", | |
6942 | src_type, dest_type); | |
6943 | return true; | |
36ef7262 MLI |
6944 | |
6945 | case REINTERPRET_CAST_EXPR: | |
3f89235e JM |
6946 | if (complain & tf_error) |
6947 | error ("reinterpret_cast from type %qT to type %qT casts away qualifiers", | |
6948 | src_type, dest_type); | |
6949 | return true; | |
6950 | ||
36ef7262 MLI |
6951 | default: |
6952 | gcc_unreachable(); | |
6953 | } | |
3fe18f1d MM |
6954 | } |
6955 | ||
d4a760d8 | 6956 | /* Warns if the cast from expression EXPR to type TYPE is useless. */ |
04398fa8 PC |
6957 | void |
6958 | maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain) | |
6959 | { | |
6960 | if (warn_useless_cast | |
ca7e759d | 6961 | && complain & tf_warning) |
04398fa8 | 6962 | { |
9f613f06 | 6963 | if ((TYPE_REF_P (type) |
04398fa8 | 6964 | && (TYPE_REF_IS_RVALUE (type) |
72b3e203 | 6965 | ? xvalue_p (expr) : lvalue_p (expr)) |
04398fa8 PC |
6966 | && same_type_p (TREE_TYPE (expr), TREE_TYPE (type))) |
6967 | || same_type_p (TREE_TYPE (expr), type)) | |
a160ed77 | 6968 | warning (OPT_Wuseless_cast, "useless cast to type %q#T", type); |
04398fa8 PC |
6969 | } |
6970 | } | |
6971 | ||
d4a760d8 JW |
6972 | /* Warns if the cast ignores cv-qualifiers on TYPE. */ |
6973 | void | |
6974 | maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain) | |
6975 | { | |
6976 | if (warn_ignored_qualifiers | |
6977 | && complain & tf_warning | |
6978 | && !CLASS_TYPE_P (type) | |
6979 | && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))) | |
6980 | { | |
6981 | warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast " | |
6982 | "result type"); | |
6983 | } | |
6984 | } | |
6985 | ||
08e17d9d MM |
6986 | /* Convert EXPR (an expression with pointer-to-member type) to TYPE |
6987 | (another pointer-to-member type in the same hierarchy) and return | |
6988 | the converted expression. If ALLOW_INVERSE_P is permitted, a | |
6989 | pointer-to-derived may be converted to pointer-to-base; otherwise, | |
6990 | only the other direction is permitted. If C_CAST_P is true, this | |
6991 | conversion is taking place as part of a C-style cast. */ | |
6992 | ||
c8094d83 | 6993 | tree |
08e17d9d | 6994 | convert_ptrmem (tree type, tree expr, bool allow_inverse_p, |
135d47df | 6995 | bool c_cast_p, tsubst_flags_t complain) |
08e17d9d | 6996 | { |
8b8b203a JM |
6997 | if (same_type_p (type, TREE_TYPE (expr))) |
6998 | return expr; | |
6999 | ||
66b1156a | 7000 | if (TYPE_PTRDATAMEM_P (type)) |
08e17d9d | 7001 | { |
d760b068 JM |
7002 | tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)); |
7003 | tree nbase = TYPE_PTRMEM_CLASS_TYPE (type); | |
036b5ad1 | 7004 | tree delta = (get_delta_difference |
d760b068 | 7005 | (obase, nbase, |
036b5ad1 | 7006 | allow_inverse_p, c_cast_p, complain)); |
08e17d9d | 7007 | |
135d47df PC |
7008 | if (delta == error_mark_node) |
7009 | return error_mark_node; | |
7010 | ||
d760b068 | 7011 | if (!same_type_p (obase, nbase)) |
6e03b280 | 7012 | { |
8b8b203a JM |
7013 | if (TREE_CODE (expr) == PTRMEM_CST) |
7014 | expr = cplus_expand_constant (expr); | |
036b5ad1 NS |
7015 | |
7016 | tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr, | |
7017 | build_int_cst (TREE_TYPE (expr), -1), | |
7018 | complain); | |
7019 | tree op1 = build_nop (ptrdiff_type_node, expr); | |
7020 | tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta, | |
7021 | complain); | |
6e03b280 | 7022 | |
db3927fb | 7023 | expr = fold_build3_loc (input_location, |
036b5ad1 | 7024 | COND_EXPR, ptrdiff_type_node, cond, op1, op2); |
6e03b280 OW |
7025 | } |
7026 | ||
08e17d9d MM |
7027 | return build_nop (type, expr); |
7028 | } | |
7029 | else | |
c8094d83 | 7030 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, |
135d47df | 7031 | allow_inverse_p, c_cast_p, complain); |
08e17d9d MM |
7032 | } |
7033 | ||
33c25e5c MM |
7034 | /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true, |
7035 | this static_cast is being attempted as one of the possible casts | |
7036 | allowed by a C-style cast. (In that case, accessibility of base | |
7037 | classes is not considered, and it is OK to cast away | |
7038 | constness.) Return the result of the cast. *VALID_P is set to | |
7039 | indicate whether or not the cast was valid. */ | |
3fe18f1d | 7040 | |
33c25e5c MM |
7041 | static tree |
7042 | build_static_cast_1 (tree type, tree expr, bool c_cast_p, | |
5ade1ed2 | 7043 | bool *valid_p, tsubst_flags_t complain) |
a4443a08 | 7044 | { |
00bb3dad | 7045 | tree intype; |
3fe18f1d | 7046 | tree result; |
645c7a55 | 7047 | cp_lvalue_kind clk; |
c11b6f21 | 7048 | |
33c25e5c MM |
7049 | /* Assume the cast is valid. */ |
7050 | *valid_p = true; | |
c11b6f21 | 7051 | |
645c7a55 | 7052 | intype = unlowered_expr_type (expr); |
c11b6f21 | 7053 | |
8d8d1a28 AH |
7054 | /* Save casted types in the function's used types hash table. */ |
7055 | used_types_insert (type); | |
7056 | ||
d4a760d8 JW |
7057 | /* A prvalue of non-class type is cv-unqualified. */ |
7058 | if (!CLASS_TYPE_P (type)) | |
7059 | type = cv_unqualified (type); | |
7060 | ||
3fe18f1d | 7061 | /* [expr.static.cast] |
c11b6f21 | 7062 | |
3fe18f1d MM |
7063 | An lvalue of type "cv1 B", where B is a class type, can be cast |
7064 | to type "reference to cv2 D", where D is a class derived (clause | |
7065 | _class.derived_) from B, if a valid standard conversion from | |
7066 | "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the | |
7067 | same cv-qualification as, or greater cv-qualification than, cv1, | |
7068 | and B is not a virtual base class of D. */ | |
0a72704b MM |
7069 | /* We check this case before checking the validity of "TYPE t = |
7070 | EXPR;" below because for this case: | |
7071 | ||
7072 | struct B {}; | |
7073 | struct D : public B { D(const B&); }; | |
7074 | extern B& b; | |
7075 | void f() { static_cast<const D&>(b); } | |
7076 | ||
7077 | we want to avoid constructing a new D. The standard is not | |
7078 | completely clear about this issue, but our interpretation is | |
7079 | consistent with other compilers. */ | |
9f613f06 | 7080 | if (TYPE_REF_P (type) |
3fe18f1d MM |
7081 | && CLASS_TYPE_P (TREE_TYPE (type)) |
7082 | && CLASS_TYPE_P (intype) | |
72b3e203 | 7083 | && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr)) |
3fe18f1d MM |
7084 | && DERIVED_FROM_P (intype, TREE_TYPE (type)) |
7085 | && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)), | |
c8094d83 | 7086 | build_pointer_type (TYPE_MAIN_VARIANT |
b40e334f PC |
7087 | (TREE_TYPE (type))), |
7088 | complain) | |
33c25e5c MM |
7089 | && (c_cast_p |
7090 | || at_least_as_qualified_p (TREE_TYPE (type), intype))) | |
c11b6f21 | 7091 | { |
33c25e5c MM |
7092 | tree base; |
7093 | ||
c360a668 JM |
7094 | if (processing_template_decl) |
7095 | return expr; | |
7096 | ||
385bce06 | 7097 | /* There is a standard conversion from "D*" to "B*" even if "B" |
33c25e5c MM |
7098 | is ambiguous or inaccessible. If this is really a |
7099 | static_cast, then we check both for inaccessibility and | |
7100 | ambiguity. However, if this is a static_cast being performed | |
7101 | because the user wrote a C-style cast, then accessibility is | |
7102 | not considered. */ | |
c8094d83 MS |
7103 | base = lookup_base (TREE_TYPE (type), intype, |
7104 | c_cast_p ? ba_unique : ba_check, | |
22854930 | 7105 | NULL, complain); |
35228ac7 JJ |
7106 | expr = build_address (expr); |
7107 | ||
45b2222a | 7108 | if (sanitize_flags_p (SANITIZE_VPTR)) |
35228ac7 JJ |
7109 | { |
7110 | tree ubsan_check | |
62775f0d JJ |
7111 | = cp_ubsan_maybe_instrument_downcast (input_location, type, |
7112 | intype, expr); | |
35228ac7 JJ |
7113 | if (ubsan_check) |
7114 | expr = ubsan_check; | |
7115 | } | |
3fe18f1d | 7116 | |
385bce06 | 7117 | /* Convert from "B*" to "D*". This function will check that "B" |
830421fc JJ |
7118 | is not a virtual base of "D". Even if we don't have a guarantee |
7119 | that expr is NULL, if the static_cast is to a reference type, | |
7120 | it is UB if it would be NULL, so omit the non-NULL check. */ | |
7121 | expr = build_base_path (MINUS_EXPR, expr, base, | |
7122 | /*nonnull=*/flag_delete_null_pointer_checks, | |
35228ac7 JJ |
7123 | complain); |
7124 | ||
3a73bcc6 | 7125 | /* Convert the pointer to a reference -- but then remember that |
9e115cec JM |
7126 | there are no expressions with reference type in C++. |
7127 | ||
7128 | We call rvalue so that there's an actual tree code | |
7129 | (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand | |
7130 | is a variable with the same type, the conversion would get folded | |
7131 | away, leaving just the variable and causing lvalue_kind to give | |
7132 | the wrong answer. */ | |
830421fc JJ |
7133 | expr = cp_fold_convert (type, expr); |
7134 | ||
7135 | /* When -fsanitize=null, make sure to diagnose reference binding to | |
7136 | NULL even when the reference is converted to pointer later on. */ | |
7137 | if (sanitize_flags_p (SANITIZE_NULL) | |
7138 | && TREE_CODE (expr) == COND_EXPR | |
7139 | && TREE_OPERAND (expr, 2) | |
7140 | && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST | |
7141 | && TREE_TYPE (TREE_OPERAND (expr, 2)) == type) | |
7142 | ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2)); | |
7143 | ||
7144 | return convert_from_reference (rvalue (expr)); | |
c11b6f21 | 7145 | } |
3fe18f1d | 7146 | |
8e3fb544 | 7147 | /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to |
e7f1930f | 7148 | cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */ |
9f613f06 | 7149 | if (TYPE_REF_P (type) |
e7f1930f | 7150 | && TYPE_REF_IS_RVALUE (type) |
645c7a55 | 7151 | && (clk = real_lvalue_p (expr)) |
e7f1930f JM |
7152 | && reference_related_p (TREE_TYPE (type), intype) |
7153 | && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype))) | |
7154 | { | |
c360a668 JM |
7155 | if (processing_template_decl) |
7156 | return expr; | |
645c7a55 JM |
7157 | if (clk == clk_ordinary) |
7158 | { | |
7159 | /* Handle the (non-bit-field) lvalue case here by casting to | |
7160 | lvalue reference and then changing it to an rvalue reference. | |
7161 | Casting an xvalue to rvalue reference will be handled by the | |
7162 | main code path. */ | |
7163 | tree lref = cp_build_reference_type (TREE_TYPE (type), false); | |
7164 | result = (perform_direct_initialization_if_possible | |
7165 | (lref, expr, c_cast_p, complain)); | |
16dc6b17 | 7166 | result = build1 (NON_LVALUE_EXPR, type, result); |
645c7a55 JM |
7167 | return convert_from_reference (result); |
7168 | } | |
7169 | else | |
7170 | /* For a bit-field or packed field, bind to a temporary. */ | |
7171 | expr = rvalue (expr); | |
e7f1930f JM |
7172 | } |
7173 | ||
7bead48f JM |
7174 | /* Resolve overloaded address here rather than once in |
7175 | implicit_conversion and again in the inverse code below. */ | |
7176 | if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr)) | |
7177 | { | |
7178 | expr = instantiate_type (type, expr, complain); | |
7179 | intype = TREE_TYPE (expr); | |
7180 | } | |
7181 | ||
ca73dc29 MG |
7182 | /* [expr.static.cast] |
7183 | ||
7184 | Any expression can be explicitly converted to type cv void. */ | |
50e10fa8 | 7185 | if (VOID_TYPE_P (type)) |
ca73dc29 MG |
7186 | return convert_to_void (expr, ICV_CAST, complain); |
7187 | ||
2df663cc JM |
7188 | /* [class.abstract] |
7189 | An abstract class shall not be used ... as the type of an explicit | |
7190 | conversion. */ | |
7191 | if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain)) | |
7192 | return error_mark_node; | |
7193 | ||
0a72704b MM |
7194 | /* [expr.static.cast] |
7195 | ||
7196 | An expression e can be explicitly converted to a type T using a | |
7197 | static_cast of the form static_cast<T>(e) if the declaration T | |
7198 | t(e);" is well-formed, for some invented temporary variable | |
7199 | t. */ | |
33c25e5c | 7200 | result = perform_direct_initialization_if_possible (type, expr, |
5ade1ed2 | 7201 | c_cast_p, complain); |
0a72704b | 7202 | if (result) |
109e0040 | 7203 | { |
c360a668 JM |
7204 | if (processing_template_decl) |
7205 | return expr; | |
7206 | ||
109e0040 | 7207 | result = convert_from_reference (result); |
33c25e5c | 7208 | |
109e0040 MM |
7209 | /* [expr.static.cast] |
7210 | ||
0cbd7506 | 7211 | If T is a reference type, the result is an lvalue; otherwise, |
109e0040 | 7212 | the result is an rvalue. */ |
9f613f06 | 7213 | if (!TYPE_REF_P (type)) |
0465369f JM |
7214 | { |
7215 | result = rvalue (result); | |
7216 | ||
7217 | if (result == expr && SCALAR_TYPE_P (type)) | |
7218 | /* Leave some record of the cast. */ | |
7219 | result = build_nop (type, expr); | |
7220 | } | |
109e0040 MM |
7221 | return result; |
7222 | } | |
c8094d83 | 7223 | |
3fe18f1d MM |
7224 | /* [expr.static.cast] |
7225 | ||
7226 | The inverse of any standard conversion sequence (clause _conv_), | |
7227 | other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer | |
7228 | (_conv.array_), function-to-pointer (_conv.func_), and boolean | |
7229 | (_conv.bool_) conversions, can be performed explicitly using | |
7230 | static_cast subject to the restriction that the explicit | |
7231 | conversion does not cast away constness (_expr.const.cast_), and | |
7232 | the following additional rules for specific cases: */ | |
7233 | /* For reference, the conversions not excluded are: integral | |
7234 | promotions, floating point promotion, integral conversions, | |
7235 | floating point conversions, floating-integral conversions, | |
7236 | pointer conversions, and pointer to member conversions. */ | |
a10ce2f8 | 7237 | /* DR 128 |
c8094d83 | 7238 | |
a10ce2f8 GK |
7239 | A value of integral _or enumeration_ type can be explicitly |
7240 | converted to an enumeration type. */ | |
7241 | /* The effect of all that is that any conversion between any two | |
7242 | types which are integral, floating, or enumeration types can be | |
7243 | performed. */ | |
550a799d JM |
7244 | if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
7245 | || SCALAR_FLOAT_TYPE_P (type)) | |
7246 | && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype) | |
7247 | || SCALAR_FLOAT_TYPE_P (intype))) | |
c360a668 JM |
7248 | { |
7249 | if (processing_template_decl) | |
7250 | return expr; | |
7251 | return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain); | |
7252 | } | |
33c25e5c | 7253 | |
3fe18f1d MM |
7254 | if (TYPE_PTR_P (type) && TYPE_PTR_P (intype) |
7255 | && CLASS_TYPE_P (TREE_TYPE (type)) | |
7256 | && CLASS_TYPE_P (TREE_TYPE (intype)) | |
c8094d83 MS |
7257 | && can_convert (build_pointer_type (TYPE_MAIN_VARIANT |
7258 | (TREE_TYPE (intype))), | |
7259 | build_pointer_type (TYPE_MAIN_VARIANT | |
b40e334f PC |
7260 | (TREE_TYPE (type))), |
7261 | complain)) | |
999cc24c | 7262 | { |
3fe18f1d MM |
7263 | tree base; |
7264 | ||
c360a668 JM |
7265 | if (processing_template_decl) |
7266 | return expr; | |
7267 | ||
3f89235e JM |
7268 | if (!c_cast_p |
7269 | && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR, | |
7270 | complain)) | |
7271 | return error_mark_node; | |
c8094d83 MS |
7272 | base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype), |
7273 | c_cast_p ? ba_unique : ba_check, | |
22854930 | 7274 | NULL, complain); |
a271590a PC |
7275 | expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false, |
7276 | complain); | |
35228ac7 | 7277 | |
45b2222a | 7278 | if (sanitize_flags_p (SANITIZE_VPTR)) |
35228ac7 JJ |
7279 | { |
7280 | tree ubsan_check | |
62775f0d JJ |
7281 | = cp_ubsan_maybe_instrument_downcast (input_location, type, |
7282 | intype, expr); | |
35228ac7 JJ |
7283 | if (ubsan_check) |
7284 | expr = ubsan_check; | |
7285 | } | |
7286 | ||
7287 | return cp_fold_convert (type, expr); | |
999cc24c | 7288 | } |
c8094d83 | 7289 | |
66b1156a | 7290 | if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype)) |
3fe18f1d MM |
7291 | || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) |
7292 | { | |
7293 | tree c1; | |
7294 | tree c2; | |
7295 | tree t1; | |
7296 | tree t2; | |
c11b6f21 | 7297 | |
3fe18f1d MM |
7298 | c1 = TYPE_PTRMEM_CLASS_TYPE (intype); |
7299 | c2 = TYPE_PTRMEM_CLASS_TYPE (type); | |
7300 | ||
66b1156a | 7301 | if (TYPE_PTRDATAMEM_P (type)) |
3fe18f1d | 7302 | { |
c8094d83 | 7303 | t1 = (build_ptrmem_type |
3fe18f1d MM |
7304 | (c1, |
7305 | TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype)))); | |
c8094d83 | 7306 | t2 = (build_ptrmem_type |
3fe18f1d MM |
7307 | (c2, |
7308 | TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type)))); | |
7309 | } | |
7310 | else | |
7311 | { | |
7312 | t1 = intype; | |
7313 | t2 = type; | |
7314 | } | |
b40e334f | 7315 | if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain)) |
3fe18f1d | 7316 | { |
3f89235e JM |
7317 | if (!c_cast_p |
7318 | && check_for_casting_away_constness (intype, type, | |
7319 | STATIC_CAST_EXPR, | |
7320 | complain)) | |
7321 | return error_mark_node; | |
c360a668 JM |
7322 | if (processing_template_decl) |
7323 | return expr; | |
08e17d9d | 7324 | return convert_ptrmem (type, expr, /*allow_inverse_p=*/1, |
3f89235e | 7325 | c_cast_p, complain); |
3fe18f1d MM |
7326 | } |
7327 | } | |
c8094d83 | 7328 | |
af7b9902 MM |
7329 | /* [expr.static.cast] |
7330 | ||
3fe18f1d MM |
7331 | An rvalue of type "pointer to cv void" can be explicitly |
7332 | converted to a pointer to object type. A value of type pointer | |
7333 | to object converted to "pointer to cv void" and back to the | |
7334 | original pointer type will have its original value. */ | |
50e10fa8 | 7335 | if (TYPE_PTR_P (intype) |
3fe18f1d MM |
7336 | && VOID_TYPE_P (TREE_TYPE (intype)) |
7337 | && TYPE_PTROB_P (type)) | |
af7b9902 | 7338 | { |
3f89235e JM |
7339 | if (!c_cast_p |
7340 | && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR, | |
7341 | complain)) | |
7342 | return error_mark_node; | |
c360a668 JM |
7343 | if (processing_template_decl) |
7344 | return expr; | |
3fe18f1d | 7345 | return build_nop (type, expr); |
af7b9902 MM |
7346 | } |
7347 | ||
33c25e5c | 7348 | *valid_p = false; |
c11b6f21 | 7349 | return error_mark_node; |
a4443a08 MS |
7350 | } |
7351 | ||
33c25e5c MM |
7352 | /* Return an expression representing static_cast<TYPE>(EXPR). */ |
7353 | ||
cdf5b885 | 7354 | tree |
281e6c1d | 7355 | build_static_cast (tree type, tree oexpr, tsubst_flags_t complain) |
a4443a08 | 7356 | { |
281e6c1d | 7357 | tree expr = oexpr; |
33c25e5c MM |
7358 | tree result; |
7359 | bool valid_p; | |
c11b6f21 MS |
7360 | |
7361 | if (type == error_mark_node || expr == error_mark_node) | |
7362 | return error_mark_node; | |
7363 | ||
281e6c1d JM |
7364 | bool dependent = (dependent_type_p (type) |
7365 | || type_dependent_expression_p (expr)); | |
7366 | if (dependent) | |
5566b478 | 7367 | { |
281e6c1d | 7368 | tmpl: |
84dd815f | 7369 | expr = build_min (STATIC_CAST_EXPR, type, oexpr); |
33c25e5c MM |
7370 | /* We don't know if it will or will not have side effects. */ |
7371 | TREE_SIDE_EFFECTS (expr) = 1; | |
e8c66fe0 | 7372 | return convert_from_reference (expr); |
5566b478 | 7373 | } |
abdca01e JM |
7374 | else if (processing_template_decl) |
7375 | expr = build_non_dependent_expr (expr); | |
5566b478 | 7376 | |
33c25e5c MM |
7377 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
7378 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ | |
9f613f06 | 7379 | if (!TYPE_REF_P (type) |
33c25e5c MM |
7380 | && TREE_CODE (expr) == NOP_EXPR |
7381 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
7382 | expr = TREE_OPERAND (expr, 0); | |
c11b6f21 | 7383 | |
5ade1ed2 DG |
7384 | result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p, |
7385 | complain); | |
33c25e5c | 7386 | if (valid_p) |
04398fa8 PC |
7387 | { |
7388 | if (result != error_mark_node) | |
d4a760d8 JW |
7389 | { |
7390 | maybe_warn_about_useless_cast (type, expr, complain); | |
7391 | maybe_warn_about_cast_ignoring_quals (type, complain); | |
7392 | } | |
281e6c1d JM |
7393 | if (processing_template_decl) |
7394 | goto tmpl; | |
04398fa8 PC |
7395 | return result; |
7396 | } | |
33c25e5c | 7397 | |
5ade1ed2 | 7398 | if (complain & tf_error) |
f4d458f3 JW |
7399 | { |
7400 | error ("invalid static_cast from type %qT to type %qT", | |
7401 | TREE_TYPE (expr), type); | |
7402 | if ((TYPE_PTR_P (type) || TYPE_REF_P (type)) | |
7403 | && CLASS_TYPE_P (TREE_TYPE (type)) | |
7404 | && !COMPLETE_TYPE_P (TREE_TYPE (type))) | |
7405 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))), | |
7406 | "class type %qT is incomplete", TREE_TYPE (type)); | |
7407 | tree expr_type = TREE_TYPE (expr); | |
7408 | if (TYPE_PTR_P (expr_type)) | |
7409 | expr_type = TREE_TYPE (expr_type); | |
7410 | if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type)) | |
7411 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)), | |
7412 | "class type %qT is incomplete", expr_type); | |
7413 | } | |
33c25e5c MM |
7414 | return error_mark_node; |
7415 | } | |
7416 | ||
7417 | /* EXPR is an expression with member function or pointer-to-member | |
7418 | function type. TYPE is a pointer type. Converting EXPR to TYPE is | |
7419 | not permitted by ISO C++, but we accept it in some modes. If we | |
7420 | are not in one of those modes, issue a diagnostic. Return the | |
7421 | converted expression. */ | |
7422 | ||
7423 | tree | |
89fcabaf | 7424 | convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain) |
33c25e5c MM |
7425 | { |
7426 | tree intype; | |
7427 | tree decl; | |
c11b6f21 | 7428 | |
c11b6f21 | 7429 | intype = TREE_TYPE (expr); |
33c25e5c MM |
7430 | gcc_assert (TYPE_PTRMEMFUNC_P (intype) |
7431 | || TREE_CODE (intype) == METHOD_TYPE); | |
a80e4195 | 7432 | |
89fcabaf PC |
7433 | if (!(complain & tf_warning_or_error)) |
7434 | return error_mark_node; | |
7435 | ||
33c25e5c | 7436 | if (pedantic || warn_pmf2ptr) |
c1771a20 | 7437 | pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions, |
f012c8ef | 7438 | "converting from %qH to %qI", intype, type); |
c8094d83 | 7439 | |
33c25e5c | 7440 | if (TREE_CODE (intype) == METHOD_TYPE) |
89fcabaf | 7441 | expr = build_addr_func (expr, complain); |
33c25e5c MM |
7442 | else if (TREE_CODE (expr) == PTRMEM_CST) |
7443 | expr = build_address (PTRMEM_CST_MEMBER (expr)); | |
7444 | else | |
7445 | { | |
7446 | decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0); | |
7447 | decl = build_address (decl); | |
89fcabaf | 7448 | expr = get_member_function_from_ptrfunc (&decl, expr, complain); |
33c25e5c MM |
7449 | } |
7450 | ||
89fcabaf PC |
7451 | if (expr == error_mark_node) |
7452 | return error_mark_node; | |
7453 | ||
33c25e5c MM |
7454 | return build_nop (type, expr); |
7455 | } | |
7456 | ||
7d75ea04 JJ |
7457 | /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that |
7458 | constexpr evaluation knows to reject it. */ | |
7459 | ||
7460 | static tree | |
7461 | build_nop_reinterpret (tree type, tree expr) | |
7462 | { | |
7463 | tree ret = build_nop (type, expr); | |
7464 | if (ret != expr) | |
7465 | REINTERPRET_CAST_P (ret) = true; | |
7466 | return ret; | |
7467 | } | |
7468 | ||
33c25e5c MM |
7469 | /* Return a representation for a reinterpret_cast from EXPR to TYPE. |
7470 | If C_CAST_P is true, this reinterpret cast is being done as part of | |
7471 | a C-style cast. If VALID_P is non-NULL, *VALID_P is set to | |
7472 | indicate whether or not reinterpret_cast was valid. */ | |
7473 | ||
7474 | static tree | |
7475 | build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p, | |
5ade1ed2 | 7476 | bool *valid_p, tsubst_flags_t complain) |
33c25e5c MM |
7477 | { |
7478 | tree intype; | |
7479 | ||
7480 | /* Assume the cast is invalid. */ | |
7481 | if (valid_p) | |
7482 | *valid_p = true; | |
7483 | ||
7484 | if (type == error_mark_node || error_operand_p (expr)) | |
a8c2c492 AP |
7485 | return error_mark_node; |
7486 | ||
33c25e5c MM |
7487 | intype = TREE_TYPE (expr); |
7488 | ||
8d8d1a28 AH |
7489 | /* Save casted types in the function's used types hash table. */ |
7490 | used_types_insert (type); | |
7491 | ||
d4a760d8 JW |
7492 | /* A prvalue of non-class type is cv-unqualified. */ |
7493 | if (!CLASS_TYPE_P (type)) | |
7494 | type = cv_unqualified (type); | |
7495 | ||
33c25e5c | 7496 | /* [expr.reinterpret.cast] |
a347241b | 7497 | A glvalue expression of type T1 can be cast to the type |
33c25e5c MM |
7498 | "reference to T2" if an expression of type "pointer to T1" can be |
7499 | explicitly converted to the type "pointer to T2" using a | |
7500 | reinterpret_cast. */ | |
9f613f06 | 7501 | if (TYPE_REF_P (type)) |
c11b6f21 | 7502 | { |
50aaebab | 7503 | if (TYPE_REF_IS_RVALUE (type) && !VOID_TYPE_P (intype)) |
a347241b JM |
7504 | { |
7505 | if (!obvalue_p (expr)) | |
7506 | /* Perform the temporary materialization conversion. */ | |
7507 | expr = get_target_expr_sfinae (expr, complain); | |
7508 | } | |
7509 | else if (!lvalue_p (expr)) | |
c11b6f21 | 7510 | { |
5ade1ed2 DG |
7511 | if (complain & tf_error) |
7512 | error ("invalid cast of an rvalue expression of type " | |
7513 | "%qT to type %qT", | |
7514 | intype, type); | |
c11b6f21 MS |
7515 | return error_mark_node; |
7516 | } | |
33c25e5c MM |
7517 | |
7518 | /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and | |
7519 | "B" are related class types; the reinterpret_cast does not | |
7520 | adjust the pointer. */ | |
7521 | if (TYPE_PTR_P (intype) | |
5ade1ed2 | 7522 | && (complain & tf_warning) |
96d84882 PB |
7523 | && (comptypes (TREE_TYPE (intype), TREE_TYPE (type), |
7524 | COMPARE_BASE | COMPARE_DERIVED))) | |
d4ee4d25 | 7525 | warning (0, "casting %qT to %qT does not dereference pointer", |
33c25e5c MM |
7526 | intype, type); |
7527 | ||
93c0e0bb | 7528 | expr = cp_build_addr_expr (expr, complain); |
45fe7947 JM |
7529 | |
7530 | if (warn_strict_aliasing > 2) | |
0444aa9c | 7531 | strict_aliasing_warning (EXPR_LOCATION (expr), type, expr); |
45fe7947 | 7532 | |
c11b6f21 | 7533 | if (expr != error_mark_node) |
33c25e5c | 7534 | expr = build_reinterpret_cast_1 |
ec7e5618 | 7535 | (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p, |
5ade1ed2 | 7536 | valid_p, complain); |
c11b6f21 | 7537 | if (expr != error_mark_node) |
45fe7947 JM |
7538 | /* cp_build_indirect_ref isn't right for rvalue refs. */ |
7539 | expr = convert_from_reference (fold_convert (type, expr)); | |
c11b6f21 MS |
7540 | return expr; |
7541 | } | |
8ccc31eb | 7542 | |
33c25e5c MM |
7543 | /* As a G++ extension, we consider conversions from member |
7544 | functions, and pointers to member functions to | |
7545 | pointer-to-function and pointer-to-void types. If | |
7546 | -Wno-pmf-conversions has not been specified, | |
7547 | convert_member_func_to_ptr will issue an error message. */ | |
c8094d83 | 7548 | if ((TYPE_PTRMEMFUNC_P (intype) |
33c25e5c MM |
7549 | || TREE_CODE (intype) == METHOD_TYPE) |
7550 | && TYPE_PTR_P (type) | |
7551 | && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
7552 | || VOID_TYPE_P (TREE_TYPE (type)))) | |
89fcabaf | 7553 | return convert_member_func_to_ptr (type, expr, complain); |
33c25e5c | 7554 | |
5acd0bed | 7555 | /* If the cast is not to a reference type, the lvalue-to-rvalue, |
33c25e5c MM |
7556 | array-to-pointer, and function-to-pointer conversions are |
7557 | performed. */ | |
89fcabaf | 7558 | expr = decay_conversion (expr, complain); |
c8094d83 | 7559 | |
33c25e5c MM |
7560 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
7561 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ | |
7562 | if (TREE_CODE (expr) == NOP_EXPR | |
7563 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
7564 | expr = TREE_OPERAND (expr, 0); | |
7565 | ||
7566 | if (error_operand_p (expr)) | |
7567 | return error_mark_node; | |
7568 | ||
7569 | intype = TREE_TYPE (expr); | |
7570 | ||
7571 | /* [expr.reinterpret.cast] | |
7572 | A pointer can be converted to any integral type large enough to | |
14c2101d JM |
7573 | hold it. ... A value of type std::nullptr_t can be converted to |
7574 | an integral type; the conversion has the same meaning and | |
7575 | validity as a conversion of (void*)0 to the integral type. */ | |
7576 | if (CP_INTEGRAL_TYPE_P (type) | |
5116acc6 | 7577 | && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype))) |
8ccc31eb | 7578 | { |
c11b6f21 | 7579 | if (TYPE_PRECISION (type) < TYPE_PRECISION (intype)) |
5ade1ed2 DG |
7580 | { |
7581 | if (complain & tf_error) | |
f012c8ef | 7582 | permerror (input_location, "cast from %qH to %qI loses precision", |
37ec60ed | 7583 | intype, type); |
5ade1ed2 DG |
7584 | else |
7585 | return error_mark_node; | |
7586 | } | |
5116acc6 | 7587 | if (NULLPTR_TYPE_P (intype)) |
14c2101d | 7588 | return build_int_cst (type, 0); |
8ccc31eb | 7589 | } |
33c25e5c MM |
7590 | /* [expr.reinterpret.cast] |
7591 | A value of integral or enumeration type can be explicitly | |
7592 | converted to a pointer. */ | |
7593 | else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype)) | |
7594 | /* OK */ | |
7595 | ; | |
9f88ff5a | 7596 | else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
66b1156a | 7597 | || TYPE_PTR_OR_PTRMEM_P (type)) |
9f88ff5a PC |
7598 | && same_type_p (type, intype)) |
7599 | /* DR 799 */ | |
6d1301f2 | 7600 | return rvalue (expr); |
c65e18d3 BE |
7601 | else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype)) |
7602 | { | |
7603 | if ((complain & tf_warning) | |
7604 | && !cxx_safe_function_type_cast_p (TREE_TYPE (type), | |
7605 | TREE_TYPE (intype))) | |
7606 | warning (OPT_Wcast_function_type, | |
7607 | "cast between incompatible function types" | |
7608 | " from %qH to %qI", intype, type); | |
7d75ea04 | 7609 | return build_nop_reinterpret (type, expr); |
c65e18d3 BE |
7610 | } |
7611 | else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)) | |
7612 | { | |
7613 | if ((complain & tf_warning) | |
7614 | && !cxx_safe_function_type_cast_p | |
7615 | (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)), | |
7616 | TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype)))) | |
7617 | warning (OPT_Wcast_function_type, | |
7618 | "cast between incompatible pointer to member types" | |
7619 | " from %qH to %qI", intype, type); | |
7d75ea04 | 7620 | return build_nop_reinterpret (type, expr); |
c65e18d3 | 7621 | } |
66b1156a | 7622 | else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype)) |
c11b6f21 | 7623 | || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype))) |
8ccc31eb | 7624 | { |
3f89235e JM |
7625 | if (!c_cast_p |
7626 | && check_for_casting_away_constness (intype, type, | |
7627 | REINTERPRET_CAST_EXPR, | |
7628 | complain)) | |
7629 | return error_mark_node; | |
33c25e5c | 7630 | /* Warn about possible alignment problems. */ |
1d933576 BE |
7631 | if ((STRICT_ALIGNMENT || warn_cast_align == 2) |
7632 | && (complain & tf_warning) | |
33c25e5c MM |
7633 | && !VOID_TYPE_P (type) |
7634 | && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE | |
7635 | && COMPLETE_TYPE_P (TREE_TYPE (type)) | |
7636 | && COMPLETE_TYPE_P (TREE_TYPE (intype)) | |
1d933576 BE |
7637 | && min_align_of_type (TREE_TYPE (type)) |
7638 | > min_align_of_type (TREE_TYPE (intype))) | |
f012c8ef | 7639 | warning (OPT_Wcast_align, "cast from %qH to %qI " |
1d933576 | 7640 | "increases required alignment of target type", intype, type); |
c8094d83 | 7641 | |
79bedddc | 7642 | if (warn_strict_aliasing <= 2) |
0444aa9c NS |
7643 | /* strict_aliasing_warning STRIP_NOPs its expr. */ |
7644 | strict_aliasing_warning (EXPR_LOCATION (expr), type, expr); | |
de9c56a4 | 7645 | |
7d75ea04 | 7646 | return build_nop_reinterpret (type, expr); |
8ccc31eb | 7647 | } |
ffb690bd | 7648 | else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype)) |
a06d48ef | 7649 | || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type))) |
ffb690bd | 7650 | { |
dfeadaa0 PC |
7651 | if (complain & tf_warning) |
7652 | /* C++11 5.2.10 p8 says that "Converting a function pointer to an | |
7653 | object pointer type or vice versa is conditionally-supported." */ | |
7654 | warning (OPT_Wconditionally_supported, | |
7655 | "casting between pointer-to-function and pointer-to-object " | |
7656 | "is conditionally-supported"); | |
7d75ea04 | 7657 | return build_nop_reinterpret (type, expr); |
ffb690bd | 7658 | } |
b55b02ea | 7659 | else if (VECTOR_TYPE_P (type)) |
cda0a029 | 7660 | return convert_to_vector (type, expr); |
b55b02ea | 7661 | else if (VECTOR_TYPE_P (intype) |
550a799d | 7662 | && INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
cda0a029 | 7663 | return convert_to_integer_nofold (type, expr); |
c11b6f21 | 7664 | else |
8ccc31eb | 7665 | { |
33c25e5c MM |
7666 | if (valid_p) |
7667 | *valid_p = false; | |
5ade1ed2 DG |
7668 | if (complain & tf_error) |
7669 | error ("invalid cast from type %qT to type %qT", intype, type); | |
8ccc31eb MS |
7670 | return error_mark_node; |
7671 | } | |
c8094d83 | 7672 | |
7d75ea04 JJ |
7673 | expr = cp_convert (type, expr, complain); |
7674 | if (TREE_CODE (expr) == NOP_EXPR) | |
7675 | /* Mark any nop_expr that created as a reintepret_cast. */ | |
7676 | REINTERPRET_CAST_P (expr) = true; | |
7677 | return expr; | |
a4443a08 MS |
7678 | } |
7679 | ||
cdf5b885 | 7680 | tree |
5ade1ed2 | 7681 | build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain) |
a4443a08 | 7682 | { |
04398fa8 PC |
7683 | tree r; |
7684 | ||
f30432d7 MS |
7685 | if (type == error_mark_node || expr == error_mark_node) |
7686 | return error_mark_node; | |
7687 | ||
5156628f | 7688 | if (processing_template_decl) |
e92cc029 | 7689 | { |
33c25e5c | 7690 | tree t = build_min (REINTERPRET_CAST_EXPR, type, expr); |
c8094d83 | 7691 | |
8e1daa34 NS |
7692 | if (!TREE_SIDE_EFFECTS (t) |
7693 | && type_dependent_expression_p (expr)) | |
7694 | /* There might turn out to be side effects inside expr. */ | |
7695 | TREE_SIDE_EFFECTS (t) = 1; | |
e8c66fe0 | 7696 | return convert_from_reference (t); |
e92cc029 MS |
7697 | } |
7698 | ||
04398fa8 PC |
7699 | r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false, |
7700 | /*valid_p=*/NULL, complain); | |
7701 | if (r != error_mark_node) | |
d4a760d8 JW |
7702 | { |
7703 | maybe_warn_about_useless_cast (type, expr, complain); | |
7704 | maybe_warn_about_cast_ignoring_quals (type, complain); | |
7705 | } | |
04398fa8 | 7706 | return r; |
33c25e5c MM |
7707 | } |
7708 | ||
7709 | /* Perform a const_cast from EXPR to TYPE. If the cast is valid, | |
7710 | return an appropriate expression. Otherwise, return | |
7711 | error_mark_node. If the cast is not valid, and COMPLAIN is true, | |
7735d402 MM |
7712 | then a diagnostic will be issued. If VALID_P is non-NULL, we are |
7713 | performing a C-style cast, its value upon return will indicate | |
7714 | whether or not the conversion succeeded. */ | |
33c25e5c MM |
7715 | |
7716 | static tree | |
3f89235e | 7717 | build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain, |
33c25e5c MM |
7718 | bool *valid_p) |
7719 | { | |
7720 | tree src_type; | |
7721 | tree reference_type; | |
7722 | ||
7723 | /* Callers are responsible for handling error_mark_node as a | |
7724 | destination type. */ | |
7725 | gcc_assert (dst_type != error_mark_node); | |
7726 | /* In a template, callers should be building syntactic | |
7727 | representations of casts, not using this machinery. */ | |
7728 | gcc_assert (!processing_template_decl); | |
7729 | ||
7730 | /* Assume the conversion is invalid. */ | |
7731 | if (valid_p) | |
7732 | *valid_p = false; | |
7733 | ||
71a93b08 | 7734 | if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type)) |
33c25e5c | 7735 | { |
3f89235e | 7736 | if (complain & tf_error) |
33c25e5c MM |
7737 | error ("invalid use of const_cast with type %qT, " |
7738 | "which is not a pointer, " | |
7739 | "reference, nor a pointer-to-data-member type", dst_type); | |
3fd91cbd MM |
7740 | return error_mark_node; |
7741 | } | |
7742 | ||
33c25e5c | 7743 | if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE) |
8ccc31eb | 7744 | { |
3f89235e | 7745 | if (complain & tf_error) |
33c25e5c MM |
7746 | error ("invalid use of const_cast with type %qT, which is a pointer " |
7747 | "or reference to a function type", dst_type); | |
7748 | return error_mark_node; | |
8ccc31eb MS |
7749 | } |
7750 | ||
d4a760d8 JW |
7751 | /* A prvalue of non-class type is cv-unqualified. */ |
7752 | dst_type = cv_unqualified (dst_type); | |
7753 | ||
8d8d1a28 AH |
7754 | /* Save casted types in the function's used types hash table. */ |
7755 | used_types_insert (dst_type); | |
7756 | ||
33c25e5c MM |
7757 | src_type = TREE_TYPE (expr); |
7758 | /* Expressions do not really have reference types. */ | |
9f613f06 | 7759 | if (TYPE_REF_P (src_type)) |
33c25e5c MM |
7760 | src_type = TREE_TYPE (src_type); |
7761 | ||
7762 | /* [expr.const.cast] | |
7763 | ||
66d83eee JM |
7764 | For two object types T1 and T2, if a pointer to T1 can be explicitly |
7765 | converted to the type "pointer to T2" using a const_cast, then the | |
7766 | following conversions can also be made: | |
7767 | ||
7768 | -- an lvalue of type T1 can be explicitly converted to an lvalue of | |
7769 | type T2 using the cast const_cast<T2&>; | |
7770 | ||
7771 | -- a glvalue of type T1 can be explicitly converted to an xvalue of | |
7772 | type T2 using the cast const_cast<T2&&>; and | |
7773 | ||
7774 | -- if T1 is a class type, a prvalue of type T1 can be explicitly | |
7775 | converted to an xvalue of type T2 using the cast const_cast<T2&&>. */ | |
7776 | ||
9f613f06 | 7777 | if (TYPE_REF_P (dst_type)) |
8ccc31eb | 7778 | { |
33c25e5c | 7779 | reference_type = dst_type; |
66d83eee | 7780 | if (!TYPE_REF_IS_RVALUE (dst_type) |
72b3e203 | 7781 | ? lvalue_p (expr) |
bb19d4af | 7782 | : obvalue_p (expr)) |
66d83eee JM |
7783 | /* OK. */; |
7784 | else | |
8ccc31eb | 7785 | { |
3f89235e | 7786 | if (complain & tf_error) |
33c25e5c MM |
7787 | error ("invalid const_cast of an rvalue of type %qT to type %qT", |
7788 | src_type, dst_type); | |
8ccc31eb MS |
7789 | return error_mark_node; |
7790 | } | |
33c25e5c MM |
7791 | dst_type = build_pointer_type (TREE_TYPE (dst_type)); |
7792 | src_type = build_pointer_type (src_type); | |
7793 | } | |
7794 | else | |
7795 | { | |
7796 | reference_type = NULL_TREE; | |
7797 | /* If the destination type is not a reference type, the | |
7798 | lvalue-to-rvalue, array-to-pointer, and function-to-pointer | |
7799 | conversions are performed. */ | |
7800 | src_type = type_decays_to (src_type); | |
7801 | if (src_type == error_mark_node) | |
7802 | return error_mark_node; | |
7803 | } | |
8ccc31eb | 7804 | |
66b1156a | 7805 | if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type)) |
33c25e5c | 7806 | { |
2d4e2a68 | 7807 | if (comp_ptr_ttypes_const (dst_type, src_type)) |
c2840dfb | 7808 | { |
2d4e2a68 PC |
7809 | if (valid_p) |
7810 | { | |
7811 | *valid_p = true; | |
7812 | /* This cast is actually a C-style cast. Issue a warning if | |
7813 | the user is making a potentially unsafe cast. */ | |
7814 | check_for_casting_away_constness (src_type, dst_type, | |
7815 | CAST_EXPR, complain); | |
1d933576 BE |
7816 | /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */ |
7817 | if ((STRICT_ALIGNMENT || warn_cast_align == 2) | |
7818 | && (complain & tf_warning) | |
7819 | && min_align_of_type (TREE_TYPE (dst_type)) | |
7820 | > min_align_of_type (TREE_TYPE (src_type))) | |
7821 | warning (OPT_Wcast_align, "cast from %qH to %qI " | |
7822 | "increases required alignment of target type", | |
7823 | src_type, dst_type); | |
2d4e2a68 PC |
7824 | } |
7825 | if (reference_type) | |
7826 | { | |
7827 | expr = cp_build_addr_expr (expr, complain); | |
89fcabaf PC |
7828 | if (expr == error_mark_node) |
7829 | return error_mark_node; | |
2d4e2a68 PC |
7830 | expr = build_nop (reference_type, expr); |
7831 | return convert_from_reference (expr); | |
7832 | } | |
7833 | else | |
7834 | { | |
89fcabaf PC |
7835 | expr = decay_conversion (expr, complain); |
7836 | if (expr == error_mark_node) | |
7837 | return error_mark_node; | |
7838 | ||
2d4e2a68 PC |
7839 | /* build_c_cast puts on a NOP_EXPR to make the result not an |
7840 | lvalue. Strip such NOP_EXPRs if VALUE is being used in | |
7841 | non-lvalue context. */ | |
7842 | if (TREE_CODE (expr) == NOP_EXPR | |
7843 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
7844 | expr = TREE_OPERAND (expr, 0); | |
7845 | return build_nop (dst_type, expr); | |
7846 | } | |
33c25e5c | 7847 | } |
2d4e2a68 PC |
7848 | else if (valid_p |
7849 | && !at_least_as_qualified_p (TREE_TYPE (dst_type), | |
7850 | TREE_TYPE (src_type))) | |
7851 | check_for_casting_away_constness (src_type, dst_type, CAST_EXPR, | |
7852 | complain); | |
8ccc31eb MS |
7853 | } |
7854 | ||
3f89235e | 7855 | if (complain & tf_error) |
c8094d83 | 7856 | error ("invalid const_cast from type %qT to type %qT", |
33c25e5c | 7857 | src_type, dst_type); |
c11b6f21 | 7858 | return error_mark_node; |
a4443a08 MS |
7859 | } |
7860 | ||
33c25e5c | 7861 | tree |
5ade1ed2 | 7862 | build_const_cast (tree type, tree expr, tsubst_flags_t complain) |
33c25e5c | 7863 | { |
04398fa8 PC |
7864 | tree r; |
7865 | ||
71bd7186 | 7866 | if (type == error_mark_node || error_operand_p (expr)) |
33c25e5c | 7867 | return error_mark_node; |
6060a796 | 7868 | |
33c25e5c MM |
7869 | if (processing_template_decl) |
7870 | { | |
7871 | tree t = build_min (CONST_CAST_EXPR, type, expr); | |
c8094d83 | 7872 | |
33c25e5c MM |
7873 | if (!TREE_SIDE_EFFECTS (t) |
7874 | && type_dependent_expression_p (expr)) | |
7875 | /* There might turn out to be side effects inside expr. */ | |
7876 | TREE_SIDE_EFFECTS (t) = 1; | |
e8c66fe0 | 7877 | return convert_from_reference (t); |
33c25e5c MM |
7878 | } |
7879 | ||
04398fa8 PC |
7880 | r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL); |
7881 | if (r != error_mark_node) | |
d4a760d8 JW |
7882 | { |
7883 | maybe_warn_about_useless_cast (type, expr, complain); | |
7884 | maybe_warn_about_cast_ignoring_quals (type, complain); | |
7885 | } | |
04398fa8 | 7886 | return r; |
33c25e5c MM |
7887 | } |
7888 | ||
525521b6 DG |
7889 | /* Like cp_build_c_cast, but for the c-common bits. */ |
7890 | ||
7891 | tree | |
12308bc6 | 7892 | build_c_cast (location_t /*loc*/, tree type, tree expr) |
525521b6 DG |
7893 | { |
7894 | return cp_build_c_cast (type, expr, tf_warning_or_error); | |
7895 | } | |
7896 | ||
e87eed2a DM |
7897 | /* Like the "build_c_cast" used for c-common, but using cp_expr to |
7898 | preserve location information even for tree nodes that don't | |
7899 | support it. */ | |
7900 | ||
7901 | cp_expr | |
7902 | build_c_cast (location_t loc, tree type, cp_expr expr) | |
7903 | { | |
7904 | cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error); | |
7905 | result.set_location (loc); | |
7906 | return result; | |
7907 | } | |
7908 | ||
33c25e5c MM |
7909 | /* Build an expression representing an explicit C-style cast to type |
7910 | TYPE of expression EXPR. */ | |
8d08fdba MS |
7911 | |
7912 | tree | |
525521b6 | 7913 | cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain) |
8d08fdba | 7914 | { |
926ce8bd | 7915 | tree value = expr; |
33c25e5c MM |
7916 | tree result; |
7917 | bool valid_p; | |
8d08fdba | 7918 | |
33c25e5c | 7919 | if (type == error_mark_node || error_operand_p (expr)) |
8d08fdba MS |
7920 | return error_mark_node; |
7921 | ||
0949f723 NS |
7922 | if (processing_template_decl) |
7923 | { | |
7924 | tree t = build_min (CAST_EXPR, type, | |
7925 | tree_cons (NULL_TREE, value, NULL_TREE)); | |
04c06002 | 7926 | /* We don't know if it will or will not have side effects. */ |
8e1daa34 | 7927 | TREE_SIDE_EFFECTS (t) = 1; |
e8c66fe0 | 7928 | return convert_from_reference (t); |
0949f723 NS |
7929 | } |
7930 | ||
2e2da467 ZL |
7931 | /* Casts to a (pointer to a) specific ObjC class (or 'id' or |
7932 | 'Class') should always be retained, because this information aids | |
7933 | in method lookup. */ | |
7934 | if (objc_is_object_ptr (type) | |
7935 | && objc_is_object_ptr (TREE_TYPE (expr))) | |
7936 | return build_nop (type, expr); | |
7937 | ||
8d08fdba | 7938 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
8ccc31eb | 7939 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ |
9f613f06 | 7940 | if (!TYPE_REF_P (type) |
8ccc31eb | 7941 | && TREE_CODE (value) == NOP_EXPR |
8d08fdba MS |
7942 | && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0))) |
7943 | value = TREE_OPERAND (value, 0); | |
7944 | ||
8d08fdba MS |
7945 | if (TREE_CODE (type) == ARRAY_TYPE) |
7946 | { | |
7947 | /* Allow casting from T1* to T2[] because Cfront allows it. | |
cab1f180 | 7948 | NIHCL uses it. It is not valid ISO C++ however. */ |
50e10fa8 | 7949 | if (TYPE_PTR_P (TREE_TYPE (expr))) |
8d08fdba | 7950 | { |
5ade1ed2 | 7951 | if (complain & tf_error) |
cbe5f3b3 | 7952 | permerror (input_location, "ISO C++ forbids casting to an array type %qT", type); |
5ade1ed2 DG |
7953 | else |
7954 | return error_mark_node; | |
8d08fdba MS |
7955 | type = build_pointer_type (TREE_TYPE (type)); |
7956 | } | |
7957 | else | |
7958 | { | |
5ade1ed2 DG |
7959 | if (complain & tf_error) |
7960 | error ("ISO C++ forbids casting to an array type %qT", type); | |
8d08fdba MS |
7961 | return error_mark_node; |
7962 | } | |
7963 | } | |
7964 | ||
a0a33927 MS |
7965 | if (TREE_CODE (type) == FUNCTION_TYPE |
7966 | || TREE_CODE (type) == METHOD_TYPE) | |
7967 | { | |
5ade1ed2 DG |
7968 | if (complain & tf_error) |
7969 | error ("invalid cast to function type %qT", type); | |
a0a33927 MS |
7970 | return error_mark_node; |
7971 | } | |
7972 | ||
50e10fa8 | 7973 | if (TYPE_PTR_P (type) |
dc8d2739 MLI |
7974 | && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE |
7975 | /* Casting to an integer of smaller size is an error detected elsewhere. */ | |
7976 | && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value)) | |
7977 | /* Don't warn about converting any constant. */ | |
7978 | && !TREE_CONSTANT (value)) | |
7979 | warning_at (input_location, OPT_Wint_to_pointer_cast, | |
7980 | "cast to pointer from integer of different size"); | |
7981 | ||
33c25e5c | 7982 | /* A C-style cast can be a const_cast. */ |
3f89235e | 7983 | result = build_const_cast_1 (type, value, complain & tf_warning, |
33c25e5c MM |
7984 | &valid_p); |
7985 | if (valid_p) | |
04398fa8 PC |
7986 | { |
7987 | if (result != error_mark_node) | |
d4a760d8 JW |
7988 | { |
7989 | maybe_warn_about_useless_cast (type, value, complain); | |
7990 | maybe_warn_about_cast_ignoring_quals (type, complain); | |
7991 | } | |
04398fa8 PC |
7992 | return result; |
7993 | } | |
8d08fdba | 7994 | |
33c25e5c MM |
7995 | /* Or a static cast. */ |
7996 | result = build_static_cast_1 (type, value, /*c_cast_p=*/true, | |
5ade1ed2 | 7997 | &valid_p, complain); |
33c25e5c MM |
7998 | /* Or a reinterpret_cast. */ |
7999 | if (!valid_p) | |
8000 | result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true, | |
5ade1ed2 | 8001 | &valid_p, complain); |
33c25e5c MM |
8002 | /* The static_cast or reinterpret_cast may be followed by a |
8003 | const_cast. */ | |
c8094d83 | 8004 | if (valid_p |
33c25e5c | 8005 | /* A valid cast may result in errors if, for example, a |
04398fa8 | 8006 | conversion to an ambiguous base class is required. */ |
33c25e5c | 8007 | && !error_operand_p (result)) |
e6e174e5 | 8008 | { |
33c25e5c | 8009 | tree result_type; |
8ccc31eb | 8010 | |
04398fa8 | 8011 | maybe_warn_about_useless_cast (type, value, complain); |
d4a760d8 | 8012 | maybe_warn_about_cast_ignoring_quals (type, complain); |
04398fa8 | 8013 | |
33c25e5c MM |
8014 | /* Non-class rvalues always have cv-unqualified type. */ |
8015 | if (!CLASS_TYPE_P (type)) | |
8016 | type = TYPE_MAIN_VARIANT (type); | |
8017 | result_type = TREE_TYPE (result); | |
9f613f06 | 8018 | if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type)) |
33c25e5c MM |
8019 | result_type = TYPE_MAIN_VARIANT (result_type); |
8020 | /* If the type of RESULT does not match TYPE, perform a | |
8021 | const_cast to make it match. If the static_cast or | |
8022 | reinterpret_cast succeeded, we will differ by at most | |
8023 | cv-qualification, so the follow-on const_cast is guaranteed | |
8024 | to succeed. */ | |
8025 | if (!same_type_p (non_reference (type), non_reference (result_type))) | |
e6e174e5 | 8026 | { |
33c25e5c MM |
8027 | result = build_const_cast_1 (type, result, false, &valid_p); |
8028 | gcc_assert (valid_p); | |
8d08fdba | 8029 | } |
33c25e5c | 8030 | return result; |
8d08fdba MS |
8031 | } |
8032 | ||
33c25e5c | 8033 | return error_mark_node; |
8d08fdba MS |
8034 | } |
8035 | \f | |
5ade1ed2 DG |
8036 | /* For use from the C common bits. */ |
8037 | tree | |
4f2e1536 | 8038 | build_modify_expr (location_t location, |
12308bc6 | 8039 | tree lhs, tree /*lhs_origtype*/, |
c2255bc4 | 8040 | enum tree_code modifycode, |
12308bc6 PC |
8041 | location_t /*rhs_location*/, tree rhs, |
8042 | tree /*rhs_origtype*/) | |
5ade1ed2 | 8043 | { |
4f2e1536 MP |
8044 | return cp_build_modify_expr (location, lhs, modifycode, rhs, |
8045 | tf_warning_or_error); | |
5ade1ed2 DG |
8046 | } |
8047 | ||
8d08fdba MS |
8048 | /* Build an assignment expression of lvalue LHS from value RHS. |
8049 | MODIFYCODE is the code for a binary operator that we use | |
8050 | to combine the old value of LHS with RHS to get the new value. | |
8051 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. | |
8052 | ||
e92cc029 MS |
8053 | C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */ |
8054 | ||
8d08fdba | 8055 | tree |
4f2e1536 MP |
8056 | cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, |
8057 | tree rhs, tsubst_flags_t complain) | |
8d08fdba | 8058 | { |
84dd815f JM |
8059 | lhs = mark_lvalue_use_nonread (lhs); |
8060 | ||
b84702c0 | 8061 | tree result = NULL_TREE; |
8d08fdba MS |
8062 | tree newrhs = rhs; |
8063 | tree lhstype = TREE_TYPE (lhs); | |
b84702c0 | 8064 | tree olhs = lhs; |
8d08fdba | 8065 | tree olhstype = lhstype; |
487a92fe | 8066 | bool plain_assign = (modifycode == NOP_EXPR); |
b84702c0 JJ |
8067 | bool compound_side_effects_p = false; |
8068 | tree preeval = NULL_TREE; | |
8d08fdba | 8069 | |
8d08fdba | 8070 | /* Avoid duplicate error messages from operands that had errors. */ |
1bd229b7 | 8071 | if (error_operand_p (lhs) || error_operand_p (rhs)) |
8d08fdba MS |
8072 | return error_mark_node; |
8073 | ||
b84702c0 JJ |
8074 | while (TREE_CODE (lhs) == COMPOUND_EXPR) |
8075 | { | |
8076 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))) | |
8077 | compound_side_effects_p = true; | |
8078 | lhs = TREE_OPERAND (lhs, 1); | |
8079 | } | |
8080 | ||
a25bd9e6 JM |
8081 | /* Handle control structure constructs used as "lvalues". Note that we |
8082 | leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */ | |
8d08fdba MS |
8083 | switch (TREE_CODE (lhs)) |
8084 | { | |
f4f206f4 | 8085 | /* Handle --foo = 5; as these are valid constructs in C++. */ |
8d08fdba MS |
8086 | case PREDECREMENT_EXPR: |
8087 | case PREINCREMENT_EXPR: | |
b84702c0 JJ |
8088 | if (compound_side_effects_p) |
8089 | newrhs = rhs = stabilize_expr (rhs, &preeval); | |
57e20f74 | 8090 | lhs = genericize_compound_lvalue (lhs); |
b84702c0 JJ |
8091 | maybe_add_compound: |
8092 | /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5; | |
8093 | and looked through the COMPOUND_EXPRs, readd them now around | |
8094 | the resulting lhs. */ | |
8095 | if (TREE_CODE (olhs) == COMPOUND_EXPR) | |
8096 | { | |
8097 | lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs); | |
8098 | tree *ptr = &TREE_OPERAND (lhs, 1); | |
8099 | for (olhs = TREE_OPERAND (olhs, 1); | |
8100 | TREE_CODE (olhs) == COMPOUND_EXPR; | |
8101 | olhs = TREE_OPERAND (olhs, 1)) | |
8102 | { | |
8103 | *ptr = build2 (COMPOUND_EXPR, lhstype, | |
8104 | TREE_OPERAND (olhs, 0), *ptr); | |
8105 | ptr = &TREE_OPERAND (*ptr, 1); | |
8106 | } | |
8107 | } | |
a25bd9e6 | 8108 | break; |
8d08fdba | 8109 | |
a4443a08 | 8110 | case MODIFY_EXPR: |
b84702c0 JJ |
8111 | if (compound_side_effects_p) |
8112 | newrhs = rhs = stabilize_expr (rhs, &preeval); | |
57e20f74 | 8113 | lhs = genericize_compound_lvalue (lhs); |
b84702c0 | 8114 | goto maybe_add_compound; |
a4443a08 | 8115 | |
d211a298 RS |
8116 | case MIN_EXPR: |
8117 | case MAX_EXPR: | |
8118 | /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues, | |
8119 | when neither operand has side-effects. */ | |
5ade1ed2 | 8120 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
d211a298 RS |
8121 | return error_mark_node; |
8122 | ||
8123 | gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)) | |
8124 | && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1))); | |
8125 | ||
8126 | lhs = build3 (COND_EXPR, TREE_TYPE (lhs), | |
8127 | build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR, | |
8128 | boolean_type_node, | |
8129 | TREE_OPERAND (lhs, 0), | |
8130 | TREE_OPERAND (lhs, 1)), | |
8131 | TREE_OPERAND (lhs, 0), | |
8132 | TREE_OPERAND (lhs, 1)); | |
81fea426 | 8133 | gcc_fallthrough (); |
d211a298 | 8134 | |
8d08fdba MS |
8135 | /* Handle (a ? b : c) used as an "lvalue". */ |
8136 | case COND_EXPR: | |
8d08fdba MS |
8137 | { |
8138 | /* Produce (a ? (b = rhs) : (c = rhs)) | |
8139 | except that the RHS goes through a save-expr | |
8140 | so the code to compute it is only emitted once. */ | |
df3473fa JJ |
8141 | if (VOID_TYPE_P (TREE_TYPE (rhs))) |
8142 | { | |
5ade1ed2 DG |
8143 | if (complain & tf_error) |
8144 | error ("void value not ignored as it ought to be"); | |
df3473fa JJ |
8145 | return error_mark_node; |
8146 | } | |
8147 | ||
a77a9a18 | 8148 | rhs = stabilize_expr (rhs, &preeval); |
c8094d83 | 8149 | |
df39af7d JM |
8150 | /* Check this here to avoid odd errors when trying to convert |
8151 | a throw to the type of the COND_EXPR. */ | |
5ade1ed2 | 8152 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
df39af7d JM |
8153 | return error_mark_node; |
8154 | ||
2b53407e JJ |
8155 | tree op1 = cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1), |
8156 | modifycode, rhs, complain); | |
8157 | /* When sanitizing undefined behavior, even when rhs doesn't need | |
8158 | stabilization at this point, the sanitization might add extra | |
8159 | SAVE_EXPRs in there and so make sure there is no tree sharing | |
8160 | in the rhs, otherwise those SAVE_EXPRs will have initialization | |
8161 | only in one of the two branches. */ | |
8162 | if (sanitize_flags_p (SANITIZE_UNDEFINED | |
8163 | | SANITIZE_UNDEFINED_NONDEFAULT)) | |
8164 | rhs = unshare_expr (rhs); | |
8165 | tree op2 = cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2), | |
8166 | modifycode, rhs, complain); | |
8167 | tree cond = build_conditional_expr (input_location, | |
8168 | TREE_OPERAND (lhs, 0), op1, op2, | |
8169 | complain); | |
df39af7d | 8170 | |
bd6dd845 | 8171 | if (cond == error_mark_node) |
8d08fdba | 8172 | return cond; |
b84702c0 JJ |
8173 | /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d; |
8174 | and looked through the COMPOUND_EXPRs, readd them now around | |
8175 | the resulting cond before adding the preevaluated rhs. */ | |
8176 | if (TREE_CODE (olhs) == COMPOUND_EXPR) | |
8177 | { | |
8178 | cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond), | |
8179 | TREE_OPERAND (olhs, 0), cond); | |
8180 | tree *ptr = &TREE_OPERAND (cond, 1); | |
8181 | for (olhs = TREE_OPERAND (olhs, 1); | |
8182 | TREE_CODE (olhs) == COMPOUND_EXPR; | |
8183 | olhs = TREE_OPERAND (olhs, 1)) | |
8184 | { | |
8185 | *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond), | |
8186 | TREE_OPERAND (olhs, 0), *ptr); | |
8187 | ptr = &TREE_OPERAND (*ptr, 1); | |
8188 | } | |
8189 | } | |
8d08fdba MS |
8190 | /* Make sure the code to compute the rhs comes out |
8191 | before the split. */ | |
b84702c0 JJ |
8192 | result = cond; |
8193 | goto ret; | |
8d08fdba | 8194 | } |
c8094d83 | 8195 | |
7f85441b | 8196 | default: |
b84702c0 | 8197 | lhs = olhs; |
7f85441b | 8198 | break; |
8d08fdba MS |
8199 | } |
8200 | ||
8d08fdba MS |
8201 | if (modifycode == INIT_EXPR) |
8202 | { | |
4ea08463 JM |
8203 | if (BRACE_ENCLOSED_INITIALIZER_P (rhs)) |
8204 | /* Do the default thing. */; | |
8205 | else if (TREE_CODE (rhs) == CONSTRUCTOR) | |
f30efcb7 | 8206 | { |
4ea08463 | 8207 | /* Compound literal. */ |
182609b5 JM |
8208 | if (! same_type_p (TREE_TYPE (rhs), lhstype)) |
8209 | /* Call convert to generate an error; see PR 11063. */ | |
8210 | rhs = convert (lhstype, rhs); | |
f293ce4b | 8211 | result = build2 (INIT_EXPR, lhstype, lhs, rhs); |
f30efcb7 | 8212 | TREE_SIDE_EFFECTS (result) = 1; |
b84702c0 | 8213 | goto ret; |
f30efcb7 | 8214 | } |
9e1e64ec | 8215 | else if (! MAYBE_CLASS_TYPE_P (lhstype)) |
f4f206f4 | 8216 | /* Do the default thing. */; |
db5ae43f | 8217 | else |
8d08fdba | 8218 | { |
9771b263 | 8219 | vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs); |
4ba126e4 | 8220 | result = build_special_member_call (lhs, complete_ctor_identifier, |
c166b898 | 8221 | &rhs_vec, lhstype, LOOKUP_NORMAL, |
5ade1ed2 | 8222 | complain); |
c166b898 | 8223 | release_tree_vector (rhs_vec); |
8d08fdba MS |
8224 | if (result == NULL_TREE) |
8225 | return error_mark_node; | |
b84702c0 | 8226 | goto ret; |
8d08fdba MS |
8227 | } |
8228 | } | |
a56ca899 | 8229 | else |
8d08fdba | 8230 | { |
79fe346e | 8231 | lhs = require_complete_type_sfinae (lhs, complain); |
a56ca899 NS |
8232 | if (lhs == error_mark_node) |
8233 | return error_mark_node; | |
8234 | ||
8235 | if (modifycode == NOP_EXPR) | |
c91a56d2 | 8236 | { |
668ea4b1 IS |
8237 | if (c_dialect_objc ()) |
8238 | { | |
46a88c12 | 8239 | result = objc_maybe_build_modify_expr (lhs, rhs); |
668ea4b1 | 8240 | if (result) |
b84702c0 | 8241 | goto ret; |
668ea4b1 IS |
8242 | } |
8243 | ||
a56ca899 | 8244 | /* `operator=' is not an inheritable operator. */ |
9e1e64ec | 8245 | if (! MAYBE_CLASS_TYPE_P (lhstype)) |
f4f206f4 | 8246 | /* Do the default thing. */; |
a56ca899 NS |
8247 | else |
8248 | { | |
4fe977f2 PC |
8249 | result = build_new_op (input_location, MODIFY_EXPR, |
8250 | LOOKUP_NORMAL, lhs, rhs, | |
8251 | make_node (NOP_EXPR), /*overload=*/NULL, | |
5ade1ed2 | 8252 | complain); |
a56ca899 NS |
8253 | if (result == NULL_TREE) |
8254 | return error_mark_node; | |
b84702c0 | 8255 | goto ret; |
a56ca899 NS |
8256 | } |
8257 | lhstype = olhstype; | |
8258 | } | |
8259 | else | |
8260 | { | |
4063e61b JM |
8261 | tree init = NULL_TREE; |
8262 | ||
a56ca899 | 8263 | /* A binary op has been requested. Combine the old LHS |
0cbd7506 MS |
8264 | value with the RHS producing the value we should actually |
8265 | store into the LHS. */ | |
9f613f06 | 8266 | gcc_assert (!((TYPE_REF_P (lhstype) |
9e1e64ec PC |
8267 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype))) |
8268 | || MAYBE_CLASS_TYPE_P (lhstype))); | |
a56ca899 | 8269 | |
4063e61b JM |
8270 | /* Preevaluate the RHS to make sure its evaluation is complete |
8271 | before the lvalue-to-rvalue conversion of the LHS: | |
8272 | ||
8273 | [expr.ass] With respect to an indeterminately-sequenced | |
8274 | function call, the operation of a compound assignment is a | |
8275 | single evaluation. [ Note: Therefore, a function call shall | |
8276 | not intervene between the lvalue-to-rvalue conversion and the | |
8277 | side effect associated with any single compound assignment | |
8278 | operator. -- end note ] */ | |
5a0802ea | 8279 | lhs = cp_stabilize_reference (lhs); |
7447f1d7 | 8280 | rhs = decay_conversion (rhs, complain); |
84dd815f JM |
8281 | if (rhs == error_mark_node) |
8282 | return error_mark_node; | |
4063e61b | 8283 | rhs = stabilize_expr (rhs, &init); |
4f2e1536 | 8284 | newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain); |
a56ca899 NS |
8285 | if (newrhs == error_mark_node) |
8286 | { | |
5ade1ed2 DG |
8287 | if (complain & tf_error) |
8288 | error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode, | |
8289 | TREE_TYPE (lhs), TREE_TYPE (rhs)); | |
a56ca899 NS |
8290 | return error_mark_node; |
8291 | } | |
c8094d83 | 8292 | |
4063e61b JM |
8293 | if (init) |
8294 | newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs); | |
8295 | ||
a56ca899 NS |
8296 | /* Now it looks like a plain assignment. */ |
8297 | modifycode = NOP_EXPR; | |
668ea4b1 IS |
8298 | if (c_dialect_objc ()) |
8299 | { | |
46a88c12 | 8300 | result = objc_maybe_build_modify_expr (lhs, newrhs); |
668ea4b1 | 8301 | if (result) |
b84702c0 | 8302 | goto ret; |
668ea4b1 | 8303 | } |
c91a56d2 | 8304 | } |
9f613f06 PC |
8305 | gcc_assert (!TYPE_REF_P (lhstype)); |
8306 | gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs))); | |
8d08fdba MS |
8307 | } |
8308 | ||
719f407a | 8309 | /* The left-hand side must be an lvalue. */ |
5ade1ed2 | 8310 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
8d08fdba MS |
8311 | return error_mark_node; |
8312 | ||
a56ca899 NS |
8313 | /* Warn about modifying something that is `const'. Don't warn if |
8314 | this is initialization. */ | |
8d08fdba | 8315 | if (modifycode != INIT_EXPR |
91063b51 | 8316 | && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype) |
69851283 MM |
8317 | /* Functions are not modifiable, even though they are |
8318 | lvalues. */ | |
8319 | || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE | |
a56ca899 NS |
8320 | || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE |
8321 | /* If it's an aggregate and any field is const, then it is | |
8322 | effectively const. */ | |
5552b43c | 8323 | || (CLASS_TYPE_P (lhstype) |
a56ca899 | 8324 | && C_TYPE_FIELDS_READONLY (lhstype)))) |
5ade1ed2 DG |
8325 | { |
8326 | if (complain & tf_error) | |
d4714a1b | 8327 | cxx_readonly_error (loc, lhs, lv_assign); |
ae51fb47 | 8328 | return error_mark_node; |
5ade1ed2 | 8329 | } |
8d08fdba | 8330 | |
41b81065 JM |
8331 | /* If storing into a structure or union member, it may have been given a |
8332 | lowered bitfield type. We need to convert to the declared type first, | |
8333 | so retrieve it now. */ | |
8d08fdba | 8334 | |
41b81065 | 8335 | olhstype = unlowered_expr_type (lhs); |
8d08fdba | 8336 | |
8d08fdba MS |
8337 | /* Convert new value to destination type. */ |
8338 | ||
8339 | if (TREE_CODE (lhstype) == ARRAY_TYPE) | |
8340 | { | |
f376e137 | 8341 | int from_array; |
c8094d83 | 8342 | |
5666bb32 | 8343 | if (BRACE_ENCLOSED_INITIALIZER_P (newrhs)) |
84526801 | 8344 | { |
eaa9d009 JM |
8345 | if (modifycode != INIT_EXPR) |
8346 | { | |
8347 | if (complain & tf_error) | |
8348 | error ("assigning to an array from an initializer list"); | |
8349 | return error_mark_node; | |
8350 | } | |
5666bb32 | 8351 | if (check_array_initializer (lhs, lhstype, newrhs)) |
84526801 | 8352 | return error_mark_node; |
754af126 | 8353 | newrhs = digest_init (lhstype, newrhs, complain); |
625f85e9 PC |
8354 | if (newrhs == error_mark_node) |
8355 | return error_mark_node; | |
84526801 | 8356 | } |
25357d1e | 8357 | |
82b3da6a PC |
8358 | /* C++11 8.5/17: "If the destination type is an array of characters, |
8359 | an array of char16_t, an array of char32_t, or an array of wchar_t, | |
8360 | and the initializer is a string literal...". */ | |
dfd7fdca DM |
8361 | else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs)) |
8362 | == STRING_CST) | |
82b3da6a PC |
8363 | && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype))) |
8364 | && modifycode == INIT_EXPR) | |
8365 | { | |
8366 | newrhs = digest_init (lhstype, newrhs, complain); | |
8367 | if (newrhs == error_mark_node) | |
8368 | return error_mark_node; | |
8369 | } | |
8370 | ||
25357d1e | 8371 | else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype), |
5666bb32 | 8372 | TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))) |
44a8d0b3 | 8373 | { |
5ade1ed2 DG |
8374 | if (complain & tf_error) |
8375 | error ("incompatible types in assignment of %qT to %qT", | |
8376 | TREE_TYPE (rhs), lhstype); | |
44a8d0b3 MS |
8377 | return error_mark_node; |
8378 | } | |
8379 | ||
39211cd5 | 8380 | /* Allow array assignment in compiler-generated code. */ |
25357d1e | 8381 | else if (!current_function_decl |
5e242863 | 8382 | || !DECL_DEFAULTED_FN (current_function_decl)) |
cf24598a | 8383 | { |
decebe51 GDR |
8384 | /* This routine is used for both initialization and assignment. |
8385 | Make sure the diagnostic message differentiates the context. */ | |
5ade1ed2 DG |
8386 | if (complain & tf_error) |
8387 | { | |
8388 | if (modifycode == INIT_EXPR) | |
8389 | error ("array used as initializer"); | |
8390 | else | |
8391 | error ("invalid array assignment"); | |
8392 | } | |
cf24598a MM |
8393 | return error_mark_node; |
8394 | } | |
39211cd5 | 8395 | |
f376e137 | 8396 | from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE |
0cbd7506 | 8397 | ? 1 + (modifycode != INIT_EXPR): 0; |
b84702c0 JJ |
8398 | result = build_vec_init (lhs, NULL_TREE, newrhs, |
8399 | /*explicit_value_init_p=*/false, | |
8400 | from_array, complain); | |
8401 | goto ret; | |
8d08fdba MS |
8402 | } |
8403 | ||
8404 | if (modifycode == INIT_EXPR) | |
e57d93c6 JM |
8405 | /* Calls with INIT_EXPR are all direct-initialization, so don't set |
8406 | LOOKUP_ONLYCONVERTING. */ | |
41b81065 | 8407 | newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL, |
2f5b91f5 | 8408 | ICR_INIT, NULL_TREE, 0, |
5ade1ed2 | 8409 | complain); |
8d08fdba | 8410 | else |
2f5b91f5 | 8411 | newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN, |
e57d93c6 | 8412 | NULL_TREE, 0, complain, LOOKUP_IMPLICIT); |
41b81065 JM |
8413 | |
8414 | if (!same_type_p (lhstype, olhstype)) | |
4b978f96 | 8415 | newrhs = cp_convert_and_check (lhstype, newrhs, complain); |
41b81065 JM |
8416 | |
8417 | if (modifycode != INIT_EXPR) | |
8d08fdba | 8418 | { |
b7484fbe MS |
8419 | if (TREE_CODE (newrhs) == CALL_EXPR |
8420 | && TYPE_NEEDS_CONSTRUCTING (lhstype)) | |
362115a9 | 8421 | newrhs = build_cplus_new (lhstype, newrhs, complain); |
b7484fbe | 8422 | |
e8abc66f | 8423 | /* Can't initialize directly from a TARGET_EXPR, since that would |
07674418 MS |
8424 | cause the lhs to be constructed twice, and possibly result in |
8425 | accidental self-initialization. So we force the TARGET_EXPR to be | |
be99da77 | 8426 | expanded without a target. */ |
b7484fbe | 8427 | if (TREE_CODE (newrhs) == TARGET_EXPR) |
f293ce4b RS |
8428 | newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs, |
8429 | TREE_OPERAND (newrhs, 0)); | |
8d08fdba MS |
8430 | } |
8431 | ||
bd6dd845 | 8432 | if (newrhs == error_mark_node) |
8d08fdba MS |
8433 | return error_mark_node; |
8434 | ||
e58a9aa1 ZL |
8435 | if (c_dialect_objc () && flag_objc_gc) |
8436 | { | |
8437 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
8438 | ||
8439 | if (result) | |
b84702c0 | 8440 | goto ret; |
e58a9aa1 ZL |
8441 | } |
8442 | ||
f293ce4b RS |
8443 | result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR, |
8444 | lhstype, lhs, newrhs); | |
2ee887f2 | 8445 | |
8d08fdba | 8446 | TREE_SIDE_EFFECTS (result) = 1; |
487a92fe JM |
8447 | if (!plain_assign) |
8448 | TREE_NO_WARNING (result) = 1; | |
8d08fdba | 8449 | |
b84702c0 JJ |
8450 | ret: |
8451 | if (preeval) | |
8452 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result); | |
41b81065 | 8453 | return result; |
8d08fdba MS |
8454 | } |
8455 | ||
e87eed2a | 8456 | cp_expr |
f330f599 PC |
8457 | build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, |
8458 | tree rhs, tsubst_flags_t complain) | |
5566b478 | 8459 | { |
fcb9363e PP |
8460 | tree orig_lhs = lhs; |
8461 | tree orig_rhs = rhs; | |
8462 | tree overload = NULL_TREE; | |
8463 | tree op = build_nt (modifycode, NULL_TREE, NULL_TREE); | |
8464 | ||
5156628f | 8465 | if (processing_template_decl) |
fcb9363e PP |
8466 | { |
8467 | if (modifycode == NOP_EXPR | |
8468 | || type_dependent_expression_p (lhs) | |
8469 | || type_dependent_expression_p (rhs)) | |
8470 | return build_min_nt_loc (loc, MODOP_EXPR, lhs, | |
8471 | build_min_nt_loc (loc, modifycode, NULL_TREE, | |
8472 | NULL_TREE), rhs); | |
8473 | ||
8474 | lhs = build_non_dependent_expr (lhs); | |
8475 | rhs = build_non_dependent_expr (rhs); | |
8476 | } | |
5566b478 MS |
8477 | |
8478 | if (modifycode != NOP_EXPR) | |
8479 | { | |
fcb9363e PP |
8480 | tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, |
8481 | lhs, rhs, op, &overload, complain); | |
5566b478 | 8482 | if (rval) |
487a92fe | 8483 | { |
fcb9363e PP |
8484 | if (rval == error_mark_node) |
8485 | return rval; | |
487a92fe | 8486 | TREE_NO_WARNING (rval) = 1; |
fcb9363e PP |
8487 | if (processing_template_decl) |
8488 | { | |
8489 | if (overload != NULL_TREE) | |
8490 | return (build_min_non_dep_op_overload | |
8491 | (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs)); | |
8492 | ||
8493 | return (build_min_non_dep | |
8494 | (MODOP_EXPR, rval, orig_lhs, op, orig_rhs)); | |
8495 | } | |
487a92fe JM |
8496 | return rval; |
8497 | } | |
5566b478 | 8498 | } |
4f2e1536 | 8499 | return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain); |
5566b478 | 8500 | } |
8d08fdba | 8501 | |
6e03b280 OW |
8502 | /* Helper function for get_delta_difference which assumes FROM is a base |
8503 | class of TO. Returns a delta for the conversion of pointer-to-member | |
135d47df PC |
8504 | of FROM to pointer-to-member of TO. If the conversion is invalid and |
8505 | tf_error is not set in COMPLAIN returns error_mark_node, otherwise | |
6e03b280 | 8506 | returns zero. If FROM is not a base class of TO, returns NULL_TREE. |
135d47df PC |
8507 | If C_CAST_P is true, this conversion is taking place as part of a |
8508 | C-style cast. */ | |
6e03b280 OW |
8509 | |
8510 | static tree | |
135d47df PC |
8511 | get_delta_difference_1 (tree from, tree to, bool c_cast_p, |
8512 | tsubst_flags_t complain) | |
6e03b280 OW |
8513 | { |
8514 | tree binfo; | |
8515 | base_kind kind; | |
135d47df | 8516 | |
22854930 PC |
8517 | binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check, |
8518 | &kind, complain); | |
135d47df | 8519 | |
22854930 | 8520 | if (binfo == error_mark_node) |
6e03b280 | 8521 | { |
135d47df PC |
8522 | if (!(complain & tf_error)) |
8523 | return error_mark_node; | |
8524 | ||
6e03b280 OW |
8525 | error (" in pointer to member function conversion"); |
8526 | return size_zero_node; | |
8527 | } | |
8528 | else if (binfo) | |
8529 | { | |
8530 | if (kind != bk_via_virtual) | |
8531 | return BINFO_OFFSET (binfo); | |
8532 | else | |
8533 | /* FROM is a virtual base class of TO. Issue an error or warning | |
8534 | depending on whether or not this is a reinterpret cast. */ | |
8535 | { | |
135d47df PC |
8536 | if (!(complain & tf_error)) |
8537 | return error_mark_node; | |
8538 | ||
6e03b280 OW |
8539 | error ("pointer to member conversion via virtual base %qT", |
8540 | BINFO_TYPE (binfo_from_vbase (binfo))); | |
8541 | ||
8542 | return size_zero_node; | |
8543 | } | |
8544 | } | |
135d47df PC |
8545 | else |
8546 | return NULL_TREE; | |
6e03b280 OW |
8547 | } |
8548 | ||
ddd5a7c1 | 8549 | /* Get difference in deltas for different pointer to member function |
135d47df PC |
8550 | types. If the conversion is invalid and tf_error is not set in |
8551 | COMPLAIN, returns error_mark_node, otherwise returns an integer | |
8552 | constant of type PTRDIFF_TYPE_NODE and its value is zero if the | |
8553 | conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse | |
8554 | conversions as well. If C_CAST_P is true this conversion is taking | |
8555 | place as part of a C-style cast. | |
3927874d JM |
8556 | |
8557 | Note that the naming of FROM and TO is kind of backwards; the return | |
8558 | value is what we add to a TO in order to get a FROM. They are named | |
8559 | this way because we call this function to find out how to convert from | |
8560 | a pointer to member of FROM to a pointer to member of TO. */ | |
e92cc029 | 8561 | |
51c184be | 8562 | static tree |
c8094d83 | 8563 | get_delta_difference (tree from, tree to, |
08e17d9d | 8564 | bool allow_inverse_p, |
135d47df | 8565 | bool c_cast_p, tsubst_flags_t complain) |
51c184be | 8566 | { |
e8632777 MM |
8567 | tree result; |
8568 | ||
6e03b280 OW |
8569 | if (same_type_ignoring_top_level_qualifiers_p (from, to)) |
8570 | /* Pointer to member of incomplete class is permitted*/ | |
8571 | result = size_zero_node; | |
f879d298 | 8572 | else |
135d47df PC |
8573 | result = get_delta_difference_1 (from, to, c_cast_p, complain); |
8574 | ||
8575 | if (result == error_mark_node) | |
8576 | return error_mark_node; | |
c8094d83 | 8577 | |
6e03b280 OW |
8578 | if (!result) |
8579 | { | |
8580 | if (!allow_inverse_p) | |
8581 | { | |
135d47df PC |
8582 | if (!(complain & tf_error)) |
8583 | return error_mark_node; | |
8584 | ||
6e03b280 OW |
8585 | error_not_base_type (from, to); |
8586 | error (" in pointer to member conversion"); | |
135d47df | 8587 | result = size_zero_node; |
6e03b280 OW |
8588 | } |
8589 | else | |
8590 | { | |
135d47df PC |
8591 | result = get_delta_difference_1 (to, from, c_cast_p, complain); |
8592 | ||
8593 | if (result == error_mark_node) | |
8594 | return error_mark_node; | |
6e03b280 OW |
8595 | |
8596 | if (result) | |
db3927fb | 8597 | result = size_diffop_loc (input_location, |
135d47df | 8598 | size_zero_node, result); |
6e03b280 OW |
8599 | else |
8600 | { | |
135d47df PC |
8601 | if (!(complain & tf_error)) |
8602 | return error_mark_node; | |
8603 | ||
6e03b280 OW |
8604 | error_not_base_type (from, to); |
8605 | error (" in pointer to member conversion"); | |
8606 | result = size_zero_node; | |
8607 | } | |
8608 | } | |
8609 | } | |
32e02ee0 | 8610 | |
cda0a029 | 8611 | return convert_to_integer (ptrdiff_type_node, result); |
51c184be MS |
8612 | } |
8613 | ||
c7e266a6 MM |
8614 | /* Return a constructor for the pointer-to-member-function TYPE using |
8615 | the other components as specified. */ | |
8616 | ||
e08a8f45 | 8617 | tree |
acd8e2d0 | 8618 | build_ptrmemfunc1 (tree type, tree delta, tree pfn) |
594740f3 | 8619 | { |
f78c7bc6 | 8620 | tree u = NULL_TREE; |
c7e266a6 | 8621 | tree delta_field; |
c7e266a6 | 8622 | tree pfn_field; |
9771b263 | 8623 | vec<constructor_elt, va_gc> *v; |
594740f3 | 8624 | |
c7e266a6 | 8625 | /* Pull the FIELD_DECLs out of the type. */ |
1f84ec23 | 8626 | pfn_field = TYPE_FIELDS (type); |
910ad8de | 8627 | delta_field = DECL_CHAIN (pfn_field); |
594740f3 | 8628 | |
c7e266a6 | 8629 | /* Make sure DELTA has the type we want. */ |
68fca595 | 8630 | delta = convert_and_check (input_location, delta_type_node, delta); |
594740f3 | 8631 | |
a489b1f0 RG |
8632 | /* Convert to the correct target type if necessary. */ |
8633 | pfn = fold_convert (TREE_TYPE (pfn_field), pfn); | |
8634 | ||
c7e266a6 | 8635 | /* Finish creating the initializer. */ |
9771b263 | 8636 | vec_alloc (v, 2); |
4038c495 GB |
8637 | CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn); |
8638 | CONSTRUCTOR_APPEND_ELT(v, delta_field, delta); | |
8639 | u = build_constructor (type, v); | |
6de9cd9a | 8640 | TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta); |
530ec96d MM |
8641 | TREE_STATIC (u) = (TREE_CONSTANT (u) |
8642 | && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn)) | |
8643 | != NULL_TREE) | |
c8094d83 | 8644 | && (initializer_constant_valid_p (delta, TREE_TYPE (delta)) |
530ec96d | 8645 | != NULL_TREE)); |
594740f3 | 8646 | return u; |
594740f3 MS |
8647 | } |
8648 | ||
8d08fdba MS |
8649 | /* Build a constructor for a pointer to member function. It can be |
8650 | used to initialize global variables, local variable, or used | |
8651 | as a value in expressions. TYPE is the POINTER to METHOD_TYPE we | |
8652 | want to be. | |
8653 | ||
838dfd8a | 8654 | If FORCE is nonzero, then force this conversion, even if |
8d08fdba | 8655 | we would rather not do it. Usually set when using an explicit |
08e17d9d | 8656 | cast. A C-style cast is being processed iff C_CAST_P is true. |
51c184be MS |
8657 | |
8658 | Return error_mark_node, if something goes wrong. */ | |
8d08fdba MS |
8659 | |
8660 | tree | |
135d47df PC |
8661 | build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p, |
8662 | tsubst_flags_t complain) | |
8d08fdba | 8663 | { |
e08a8f45 | 8664 | tree fn; |
7993382e MM |
8665 | tree pfn_type; |
8666 | tree to_type; | |
8667 | ||
8668 | if (error_operand_p (pfn)) | |
8669 | return error_mark_node; | |
8670 | ||
8671 | pfn_type = TREE_TYPE (pfn); | |
8672 | to_type = build_ptrmemfunc_type (type); | |
b928a651 | 8673 | |
e92cc029 | 8674 | /* Handle multiple conversions of pointer to member functions. */ |
7993382e | 8675 | if (TYPE_PTRMEMFUNC_P (pfn_type)) |
8d08fdba | 8676 | { |
530ec96d | 8677 | tree delta = NULL_TREE; |
7133357a | 8678 | tree npfn = NULL_TREE; |
1f84ec23 | 8679 | tree n; |
594740f3 | 8680 | |
c8094d83 | 8681 | if (!force |
b40e334f PC |
8682 | && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, |
8683 | LOOKUP_NORMAL, complain)) | |
76545796 PC |
8684 | { |
8685 | if (complain & tf_error) | |
8686 | error ("invalid conversion to type %qT from type %qT", | |
8687 | to_type, pfn_type); | |
8688 | else | |
8689 | return error_mark_node; | |
8690 | } | |
9ca21c0a | 8691 | |
3f2b640a JM |
8692 | n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type), |
8693 | TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type), | |
08e17d9d | 8694 | force, |
135d47df PC |
8695 | c_cast_p, complain); |
8696 | if (n == error_mark_node) | |
8697 | return error_mark_node; | |
3f2b640a | 8698 | |
c7e266a6 MM |
8699 | /* We don't have to do any conversion to convert a |
8700 | pointer-to-member to its own type. But, we don't want to | |
8701 | just return a PTRMEM_CST if there's an explicit cast; that | |
8702 | cast should make the expression an invalid template argument. */ | |
3f2b640a JM |
8703 | if (TREE_CODE (pfn) != PTRMEM_CST) |
8704 | { | |
8705 | if (same_type_p (to_type, pfn_type)) | |
8706 | return pfn; | |
6d0e87b2 | 8707 | else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR) |
5ade1ed2 | 8708 | return build_reinterpret_cast (to_type, pfn, |
a3299120 | 8709 | complain); |
3f2b640a | 8710 | } |
c7e266a6 | 8711 | |
7def1251 JM |
8712 | if (TREE_SIDE_EFFECTS (pfn)) |
8713 | pfn = save_expr (pfn); | |
8714 | ||
530ec96d MM |
8715 | /* Obtain the function pointer and the current DELTA. */ |
8716 | if (TREE_CODE (pfn) == PTRMEM_CST) | |
18ae7f63 | 8717 | expand_ptrmemfunc_cst (pfn, &delta, &npfn); |
530ec96d MM |
8718 | else |
8719 | { | |
50ad9642 MM |
8720 | npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier); |
8721 | delta = build_ptrmemfunc_access_expr (pfn, delta_identifier); | |
530ec96d MM |
8722 | } |
8723 | ||
3461fba7 | 8724 | /* Just adjust the DELTA field. */ |
50bc768d NS |
8725 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
8726 | (TREE_TYPE (delta), ptrdiff_type_node)); | |
6d0e87b2 JM |
8727 | if (!integer_zerop (n)) |
8728 | { | |
8729 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta) | |
8730 | n = cp_build_binary_op (input_location, | |
8731 | LSHIFT_EXPR, n, integer_one_node, | |
8732 | complain); | |
8733 | delta = cp_build_binary_op (input_location, | |
8734 | PLUS_EXPR, delta, n, complain); | |
8735 | } | |
18ae7f63 | 8736 | return build_ptrmemfunc1 (to_type, delta, npfn); |
8d08fdba | 8737 | } |
51c184be | 8738 | |
e92cc029 | 8739 | /* Handle null pointer to member function conversions. */ |
33c2474d | 8740 | if (null_ptr_cst_p (pfn)) |
51c184be | 8741 | { |
f61af651 | 8742 | pfn = cp_build_c_cast (type, pfn, complain); |
b928a651 | 8743 | return build_ptrmemfunc1 (to_type, |
c8094d83 | 8744 | integer_zero_node, |
18ae7f63 | 8745 | pfn); |
51c184be MS |
8746 | } |
8747 | ||
e6e174e5 | 8748 | if (type_unknown_p (pfn)) |
a3299120 | 8749 | return instantiate_type (type, pfn, complain); |
a0a33927 | 8750 | |
e08a8f45 | 8751 | fn = TREE_OPERAND (pfn, 0); |
37048601 NS |
8752 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL |
8753 | /* In a template, we will have preserved the | |
8754 | OFFSET_REF. */ | |
8755 | || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF)); | |
b928a651 | 8756 | return make_ptrmem_cst (to_type, fn); |
e08a8f45 | 8757 | } |
8d08fdba | 8758 | |
18ae7f63 | 8759 | /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST |
fed3cef0 RK |
8760 | given by CST. |
8761 | ||
8762 | ??? There is no consistency as to the types returned for the above | |
852dcbdd | 8763 | values. Some code acts as if it were a sizetype and some as if it were |
fed3cef0 | 8764 | integer_type_node. */ |
8d08fdba | 8765 | |
e08a8f45 | 8766 | void |
acd8e2d0 | 8767 | expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn) |
e08a8f45 MM |
8768 | { |
8769 | tree type = TREE_TYPE (cst); | |
8770 | tree fn = PTRMEM_CST_MEMBER (cst); | |
3927874d | 8771 | tree ptr_class, fn_class; |
8d08fdba | 8772 | |
50bc768d | 8773 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); |
3927874d JM |
8774 | |
8775 | /* The class that the function belongs to. */ | |
4f1c5b7d | 8776 | fn_class = DECL_CONTEXT (fn); |
3927874d JM |
8777 | |
8778 | /* The class that we're creating a pointer to member of. */ | |
8779 | ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type); | |
8780 | ||
8781 | /* First, calculate the adjustment to the function's class. */ | |
08e17d9d | 8782 | *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0, |
135d47df | 8783 | /*c_cast_p=*/0, tf_warning_or_error); |
3927874d | 8784 | |
e08a8f45 | 8785 | if (!DECL_VIRTUAL_P (fn)) |
89fcabaf PC |
8786 | *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), |
8787 | build_addr_func (fn, tf_warning_or_error)); | |
8d08fdba | 8788 | else |
51c184be | 8789 | { |
3927874d | 8790 | /* If we're dealing with a virtual function, we have to adjust 'this' |
0cbd7506 MS |
8791 | again, to point to the base which provides the vtable entry for |
8792 | fn; the call will do the opposite adjustment. */ | |
e93ee644 | 8793 | tree orig_class = DECL_CONTEXT (fn); |
3927874d | 8794 | tree binfo = binfo_or_else (orig_class, fn_class); |
cda0a029 JM |
8795 | *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta), |
8796 | *delta, BINFO_OFFSET (binfo)); | |
3927874d | 8797 | |
3461fba7 NS |
8798 | /* We set PFN to the vtable offset at which the function can be |
8799 | found, plus one (unless ptrmemfunc_vbit_in_delta, in which | |
8800 | case delta is shifted left, and then incremented). */ | |
cb7fdde2 | 8801 | *pfn = DECL_VINDEX (fn); |
cda0a029 JM |
8802 | *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn, |
8803 | TYPE_SIZE_UNIT (vtable_entry_type)); | |
cb7fdde2 AO |
8804 | |
8805 | switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) | |
8806 | { | |
8807 | case ptrmemfunc_vbit_in_pfn: | |
cda0a029 JM |
8808 | *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn, |
8809 | integer_one_node); | |
cb7fdde2 AO |
8810 | break; |
8811 | ||
8812 | case ptrmemfunc_vbit_in_delta: | |
cda0a029 JM |
8813 | *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta), |
8814 | *delta, integer_one_node); | |
8815 | *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta), | |
8816 | *delta, integer_one_node); | |
cb7fdde2 AO |
8817 | break; |
8818 | ||
8819 | default: | |
315fb5db | 8820 | gcc_unreachable (); |
cb7fdde2 AO |
8821 | } |
8822 | ||
cda0a029 | 8823 | *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn); |
e08a8f45 MM |
8824 | } |
8825 | } | |
8d08fdba | 8826 | |
e08a8f45 MM |
8827 | /* Return an expression for PFN from the pointer-to-member function |
8828 | given by T. */ | |
8829 | ||
b5791fdc | 8830 | static tree |
acd8e2d0 | 8831 | pfn_from_ptrmemfunc (tree t) |
e08a8f45 MM |
8832 | { |
8833 | if (TREE_CODE (t) == PTRMEM_CST) | |
8834 | { | |
8835 | tree delta; | |
e08a8f45 | 8836 | tree pfn; |
c8094d83 | 8837 | |
18ae7f63 | 8838 | expand_ptrmemfunc_cst (t, &delta, &pfn); |
e08a8f45 MM |
8839 | if (pfn) |
8840 | return pfn; | |
51c184be | 8841 | } |
8d08fdba | 8842 | |
50ad9642 | 8843 | return build_ptrmemfunc_access_expr (t, pfn_identifier); |
8d08fdba MS |
8844 | } |
8845 | ||
a298680c OW |
8846 | /* Return an expression for DELTA from the pointer-to-member function |
8847 | given by T. */ | |
8848 | ||
8849 | static tree | |
8850 | delta_from_ptrmemfunc (tree t) | |
8851 | { | |
8852 | if (TREE_CODE (t) == PTRMEM_CST) | |
8853 | { | |
8854 | tree delta; | |
8855 | tree pfn; | |
8856 | ||
8857 | expand_ptrmemfunc_cst (t, &delta, &pfn); | |
8858 | if (delta) | |
8859 | return delta; | |
8860 | } | |
8861 | ||
8862 | return build_ptrmemfunc_access_expr (t, delta_identifier); | |
8863 | } | |
8864 | ||
a7a64a77 | 8865 | /* Convert value RHS to type TYPE as preparation for an assignment to |
9d9f52d3 SZ |
8866 | an lvalue of type TYPE. ERRTYPE indicates what kind of error the |
8867 | implicit conversion is. If FNDECL is non-NULL, we are doing the | |
8868 | conversion in order to pass the PARMNUMth argument of FNDECL. | |
8869 | If FNDECL is NULL, we are doing the conversion in function pointer | |
8870 | argument passing, conversion in initialization, etc. */ | |
8d08fdba MS |
8871 | |
8872 | static tree | |
acd8e2d0 | 8873 | convert_for_assignment (tree type, tree rhs, |
2f5b91f5 | 8874 | impl_conv_rhs errtype, tree fndecl, int parmnum, |
e57d93c6 | 8875 | tsubst_flags_t complain, int flags) |
8d08fdba | 8876 | { |
926ce8bd KH |
8877 | tree rhstype; |
8878 | enum tree_code coder; | |
8d08fdba | 8879 | |
f11854d4 | 8880 | location_t rhs_loc = EXPR_LOC_OR_LOC (rhs, input_location); |
15c40a3b | 8881 | bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION; |
dfd7fdca DM |
8882 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue, |
8883 | but preserve location wrappers. */ | |
8884 | if (TREE_CODE (rhs) == NON_LVALUE_EXPR | |
8885 | && !location_wrapper_p (rhs)) | |
8d08fdba MS |
8886 | rhs = TREE_OPERAND (rhs, 0); |
8887 | ||
d664d76d JJ |
8888 | /* Handle [dcl.init.list] direct-list-initialization from |
8889 | single element of enumeration with a fixed underlying type. */ | |
8890 | if (is_direct_enum_init (type, rhs)) | |
8891 | { | |
8892 | tree elt = CONSTRUCTOR_ELT (rhs, 0)->value; | |
8893 | if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain)) | |
7260f6f7 JJ |
8894 | { |
8895 | warning_sentinel w (warn_useless_cast); | |
e6f97c3f | 8896 | warning_sentinel w2 (warn_ignored_qualifiers); |
7260f6f7 JJ |
8897 | rhs = cp_build_c_cast (type, elt, complain); |
8898 | } | |
d664d76d JJ |
8899 | else |
8900 | rhs = error_mark_node; | |
8901 | } | |
8902 | ||
a7a64a77 MM |
8903 | rhstype = TREE_TYPE (rhs); |
8904 | coder = TREE_CODE (rhstype); | |
8905 | ||
b55b02ea | 8906 | if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE |
00c8e9f6 | 8907 | && vector_types_convertible_p (type, rhstype, true)) |
416f380b JJ |
8908 | { |
8909 | rhs = mark_rvalue_use (rhs); | |
8910 | return convert (type, rhs); | |
8911 | } | |
7d149679 | 8912 | |
a7a64a77 | 8913 | if (rhs == error_mark_node || rhstype == error_mark_node) |
8d08fdba | 8914 | return error_mark_node; |
2c73f9f5 | 8915 | if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node) |
8d08fdba MS |
8916 | return error_mark_node; |
8917 | ||
a7a64a77 | 8918 | /* The RHS of an assignment cannot have void type. */ |
8d08fdba MS |
8919 | if (coder == VOID_TYPE) |
8920 | { | |
5ade1ed2 | 8921 | if (complain & tf_error) |
15c40a3b | 8922 | error_at (rhs_loc, "void value not ignored as it ought to be"); |
8d08fdba MS |
8923 | return error_mark_node; |
8924 | } | |
8d08fdba | 8925 | |
660845bf ZL |
8926 | if (c_dialect_objc ()) |
8927 | { | |
8928 | int parmno; | |
2f5b91f5 | 8929 | tree selector; |
660845bf ZL |
8930 | tree rname = fndecl; |
8931 | ||
2f5b91f5 SZ |
8932 | switch (errtype) |
8933 | { | |
8934 | case ICR_ASSIGN: | |
8935 | parmno = -1; | |
8936 | break; | |
8937 | case ICR_INIT: | |
8938 | parmno = -2; | |
8939 | break; | |
8940 | default: | |
8941 | selector = objc_message_selector (); | |
8942 | parmno = parmnum; | |
8943 | if (selector && parmno > 1) | |
8944 | { | |
8945 | rname = selector; | |
8946 | parmno -= 1; | |
8947 | } | |
660845bf ZL |
8948 | } |
8949 | ||
8950 | if (objc_compare_types (type, rhstype, parmno, rname)) | |
416f380b JJ |
8951 | { |
8952 | rhs = mark_rvalue_use (rhs); | |
8953 | return convert (type, rhs); | |
8954 | } | |
660845bf ZL |
8955 | } |
8956 | ||
a7a64a77 MM |
8957 | /* [expr.ass] |
8958 | ||
8959 | The expression is implicitly converted (clause _conv_) to the | |
72a08131 JM |
8960 | cv-unqualified type of the left operand. |
8961 | ||
8962 | We allow bad conversions here because by the time we get to this point | |
8963 | we are committed to doing the conversion. If we end up doing a bad | |
8964 | conversion, convert_like will complain. */ | |
b40e334f | 8965 | if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain)) |
a7a64a77 | 8966 | { |
32facac8 | 8967 | /* When -Wno-pmf-conversions is use, we just silently allow |
a7a64a77 MM |
8968 | conversions from pointers-to-members to plain pointers. If |
8969 | the conversion doesn't work, cp_convert will complain. */ | |
c8094d83 MS |
8970 | if (!warn_pmf2ptr |
8971 | && TYPE_PTR_P (type) | |
a7a64a77 | 8972 | && TYPE_PTRMEMFUNC_P (rhstype)) |
4b978f96 | 8973 | rhs = cp_convert (strip_top_quals (type), rhs, complain); |
72a08131 | 8974 | else |
32facac8 | 8975 | { |
5ade1ed2 DG |
8976 | if (complain & tf_error) |
8977 | { | |
8978 | /* If the right-hand side has unknown type, then it is an | |
8979 | overloaded function. Call instantiate_type to get error | |
8980 | messages. */ | |
8981 | if (rhstype == unknown_type_node) | |
559a77e1 JM |
8982 | { |
8983 | tree r = instantiate_type (type, rhs, tf_warning_or_error); | |
b126bff4 | 8984 | /* -fpermissive might allow this; recurse. */ |
559a77e1 | 8985 | if (!seen_error ()) |
b126bff4 JM |
8986 | return convert_for_assignment (type, r, errtype, fndecl, |
8987 | parmnum, complain, flags); | |
559a77e1 | 8988 | } |
5ade1ed2 | 8989 | else if (fndecl) |
f11854d4 | 8990 | complain_about_bad_argument (rhs_loc, |
4911b24d DM |
8991 | rhstype, type, |
8992 | fndecl, parmnum); | |
5ade1ed2 | 8993 | else |
15c40a3b DM |
8994 | { |
8995 | range_label_for_type_mismatch label (rhstype, type); | |
8996 | gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL); | |
8997 | switch (errtype) | |
8998 | { | |
2f5b91f5 | 8999 | case ICR_DEFAULT_ARGUMENT: |
15c40a3b DM |
9000 | error_at (&richloc, |
9001 | "cannot convert %qH to %qI in default argument", | |
9002 | rhstype, type); | |
2f5b91f5 SZ |
9003 | break; |
9004 | case ICR_ARGPASS: | |
15c40a3b DM |
9005 | error_at (&richloc, |
9006 | "cannot convert %qH to %qI in argument passing", | |
9007 | rhstype, type); | |
2f5b91f5 SZ |
9008 | break; |
9009 | case ICR_CONVERTING: | |
15c40a3b DM |
9010 | error_at (&richloc, "cannot convert %qH to %qI", |
9011 | rhstype, type); | |
2f5b91f5 SZ |
9012 | break; |
9013 | case ICR_INIT: | |
15c40a3b DM |
9014 | error_at (&richloc, |
9015 | "cannot convert %qH to %qI in initialization", | |
9016 | rhstype, type); | |
2f5b91f5 SZ |
9017 | break; |
9018 | case ICR_RETURN: | |
15c40a3b DM |
9019 | error_at (&richloc, "cannot convert %qH to %qI in return", |
9020 | rhstype, type); | |
2f5b91f5 SZ |
9021 | break; |
9022 | case ICR_ASSIGN: | |
15c40a3b DM |
9023 | error_at (&richloc, |
9024 | "cannot convert %qH to %qI in assignment", | |
9025 | rhstype, type); | |
2f5b91f5 SZ |
9026 | break; |
9027 | default: | |
9028 | gcc_unreachable(); | |
9029 | } | |
15c40a3b | 9030 | } |
7e422c4f PC |
9031 | if (TYPE_PTR_P (rhstype) |
9032 | && TYPE_PTR_P (type) | |
9033 | && CLASS_TYPE_P (TREE_TYPE (rhstype)) | |
9034 | && CLASS_TYPE_P (TREE_TYPE (type)) | |
9035 | && !COMPLETE_TYPE_P (TREE_TYPE (rhstype))) | |
9036 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL | |
9037 | (TREE_TYPE (rhstype))), | |
9038 | "class type %qT is incomplete", TREE_TYPE (rhstype)); | |
5ade1ed2 | 9039 | } |
32facac8 ML |
9040 | return error_mark_node; |
9041 | } | |
51c184be | 9042 | } |
90137d8f | 9043 | if (warn_suggest_attribute_format) |
104f8784 KG |
9044 | { |
9045 | const enum tree_code codel = TREE_CODE (type); | |
9046 | if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
9047 | && coder == codel | |
5ade1ed2 DG |
9048 | && check_missing_format_attribute (type, rhstype) |
9049 | && (complain & tf_warning)) | |
2f5b91f5 SZ |
9050 | switch (errtype) |
9051 | { | |
9052 | case ICR_ARGPASS: | |
9053 | case ICR_DEFAULT_ARGUMENT: | |
9054 | if (fndecl) | |
90137d8f | 9055 | warning (OPT_Wsuggest_attribute_format, |
9d9f52d3 | 9056 | "parameter %qP of %qD might be a candidate " |
2f5b91f5 SZ |
9057 | "for a format attribute", parmnum, fndecl); |
9058 | else | |
90137d8f | 9059 | warning (OPT_Wsuggest_attribute_format, |
2f5b91f5 SZ |
9060 | "parameter might be a candidate " |
9061 | "for a format attribute"); | |
9062 | break; | |
9063 | case ICR_CONVERTING: | |
90137d8f | 9064 | warning (OPT_Wsuggest_attribute_format, |
59a67d73 | 9065 | "target of conversion might be a candidate " |
2f5b91f5 SZ |
9066 | "for a format attribute"); |
9067 | break; | |
9068 | case ICR_INIT: | |
90137d8f | 9069 | warning (OPT_Wsuggest_attribute_format, |
2f5b91f5 SZ |
9070 | "target of initialization might be a candidate " |
9071 | "for a format attribute"); | |
9072 | break; | |
9073 | case ICR_RETURN: | |
90137d8f | 9074 | warning (OPT_Wsuggest_attribute_format, |
2f5b91f5 SZ |
9075 | "return type might be a candidate " |
9076 | "for a format attribute"); | |
9077 | break; | |
9078 | case ICR_ASSIGN: | |
90137d8f | 9079 | warning (OPT_Wsuggest_attribute_format, |
2f5b91f5 SZ |
9080 | "left-hand side of assignment might be a candidate " |
9081 | "for a format attribute"); | |
9082 | break; | |
9083 | default: | |
9084 | gcc_unreachable(); | |
9085 | } | |
104f8784 | 9086 | } |
3db45ab5 | 9087 | |
a99e5cb4 ILT |
9088 | /* If -Wparentheses, warn about a = b = c when a has type bool and b |
9089 | does not. */ | |
fbc8d2d3 | 9090 | if (warn_parentheses |
66be89f0 | 9091 | && TREE_CODE (type) == BOOLEAN_TYPE |
fbc8d2d3 | 9092 | && TREE_CODE (rhs) == MODIFY_EXPR |
a99e5cb4 | 9093 | && !TREE_NO_WARNING (rhs) |
69fb9832 | 9094 | && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE |
1db01ff9 JJ |
9095 | && (complain & tf_warning) |
9096 | && warning_at (rhs_loc, OPT_Wparentheses, | |
9097 | "suggest parentheses around assignment used as " | |
9098 | "truth value")) | |
9099 | TREE_NO_WARNING (rhs) = 1; | |
fbc8d2d3 | 9100 | |
5b81a980 | 9101 | if (complain & tf_warning) |
420183d9 | 9102 | warn_for_address_or_pointer_of_packed_member (type, rhs); |
da77eace | 9103 | |
e57d93c6 JM |
9104 | return perform_implicit_conversion_flags (strip_top_quals (type), rhs, |
9105 | complain, flags); | |
8d08fdba MS |
9106 | } |
9107 | ||
56ae6d77 | 9108 | /* Convert RHS to be of type TYPE. |
838dfd8a | 9109 | If EXP is nonzero, it is the target of the initialization. |
9d9f52d3 | 9110 | ERRTYPE indicates what kind of error the implicit conversion is. |
8d08fdba MS |
9111 | |
9112 | Two major differences between the behavior of | |
9113 | `convert_for_assignment' and `convert_for_initialization' | |
9114 | are that references are bashed in the former, while | |
9115 | copied in the latter, and aggregates are assigned in | |
9116 | the former (operator=) while initialized in the | |
9117 | latter (X(X&)). | |
9118 | ||
9119 | If using constructor make sure no conversion operator exists, if one does | |
4b978f96 | 9120 | exist, an ambiguity exists. */ |
e92cc029 | 9121 | |
8d08fdba | 9122 | tree |
acd8e2d0 | 9123 | convert_for_initialization (tree exp, tree type, tree rhs, int flags, |
2f5b91f5 | 9124 | impl_conv_rhs errtype, tree fndecl, int parmnum, |
5ade1ed2 | 9125 | tsubst_flags_t complain) |
8d08fdba | 9126 | { |
926ce8bd KH |
9127 | enum tree_code codel = TREE_CODE (type); |
9128 | tree rhstype; | |
9129 | enum tree_code coder; | |
8d08fdba MS |
9130 | |
9131 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. | |
9132 | Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */ | |
9133 | if (TREE_CODE (rhs) == NOP_EXPR | |
a0a33927 MS |
9134 | && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0)) |
9135 | && codel != REFERENCE_TYPE) | |
8d08fdba MS |
9136 | rhs = TREE_OPERAND (rhs, 0); |
9137 | ||
57b52959 VR |
9138 | if (type == error_mark_node |
9139 | || rhs == error_mark_node | |
8d08fdba MS |
9140 | || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)) |
9141 | return error_mark_node; | |
9142 | ||
f85e1317 PP |
9143 | if (MAYBE_CLASS_TYPE_P (non_reference (type))) |
9144 | ; | |
9145 | else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE | |
9146 | && TREE_CODE (type) != ARRAY_TYPE | |
9f613f06 | 9147 | && (!TYPE_REF_P (type) |
f85e1317 PP |
9148 | || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)) |
9149 | || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE | |
9150 | && !TYPE_REFFN_P (type)) | |
9151 | || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE) | |
89fcabaf | 9152 | rhs = decay_conversion (rhs, complain); |
8d08fdba MS |
9153 | |
9154 | rhstype = TREE_TYPE (rhs); | |
9155 | coder = TREE_CODE (rhstype); | |
9156 | ||
8d08fdba MS |
9157 | if (coder == ERROR_MARK) |
9158 | return error_mark_node; | |
9159 | ||
8d08fdba MS |
9160 | /* We accept references to incomplete types, so we can |
9161 | return here before checking if RHS is of complete type. */ | |
c8094d83 | 9162 | |
8d08fdba | 9163 | if (codel == REFERENCE_TYPE) |
2986ae00 | 9164 | { |
f11854d4 | 9165 | auto_diagnostic_group d; |
2986ae00 | 9166 | /* This should eventually happen in convert_arguments. */ |
a703fb38 | 9167 | int savew = 0, savee = 0; |
2986ae00 MS |
9168 | |
9169 | if (fndecl) | |
37e99116 | 9170 | savew = warningcount + werrorcount, savee = errorcount; |
b25dd954 | 9171 | rhs = initialize_reference (type, rhs, flags, complain); |
7abebba7 PC |
9172 | |
9173 | if (fndecl | |
9174 | && (warningcount + werrorcount > savew || errorcount > savee)) | |
f11854d4 | 9175 | inform (get_fndecl_argument_location (fndecl, parmnum), |
af718670 | 9176 | "in passing argument %P of %qD", parmnum, fndecl); |
2986ae00 | 9177 | return rhs; |
c8094d83 | 9178 | } |
8d08fdba | 9179 | |
66543169 | 9180 | if (exp != 0) |
79fe346e | 9181 | exp = require_complete_type_sfinae (exp, complain); |
8d08fdba MS |
9182 | if (exp == error_mark_node) |
9183 | return error_mark_node; | |
9184 | ||
ee76b931 | 9185 | rhstype = non_reference (rhstype); |
8d08fdba | 9186 | |
6467930b MS |
9187 | type = complete_type (type); |
9188 | ||
37a7519a JM |
9189 | if (DIRECT_INIT_EXPR_P (type, rhs)) |
9190 | /* Don't try to do copy-initialization if we already have | |
9191 | direct-initialization. */ | |
9192 | return rhs; | |
9193 | ||
9e1e64ec | 9194 | if (MAYBE_CLASS_TYPE_P (type)) |
f8f12278 | 9195 | return perform_implicit_conversion_flags (type, rhs, complain, flags); |
8d08fdba | 9196 | |
5ade1ed2 | 9197 | return convert_for_assignment (type, rhs, errtype, fndecl, parmnum, |
e57d93c6 | 9198 | complain, flags); |
8d08fdba MS |
9199 | } |
9200 | \f | |
efee38a9 | 9201 | /* If RETVAL is the address of, or a reference to, a local variable or |
b4dfdc11 | 9202 | temporary give an appropriate warning and return true. */ |
8d08fdba | 9203 | |
b4dfdc11 | 9204 | static bool |
acd8e2d0 | 9205 | maybe_warn_about_returning_address_of_local (tree retval) |
efee38a9 MM |
9206 | { |
9207 | tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl)); | |
f4bb5c17 | 9208 | tree whats_returned = fold_for_warn (retval); |
6bdfada4 | 9209 | location_t loc = cp_expr_loc_or_loc (retval, input_location); |
8d08fdba | 9210 | |
2bfa73e4 | 9211 | for (;;) |
efee38a9 | 9212 | { |
efee38a9 | 9213 | if (TREE_CODE (whats_returned) == COMPOUND_EXPR) |
2bfa73e4 | 9214 | whats_returned = TREE_OPERAND (whats_returned, 1); |
5f9e56b3 TB |
9215 | else if (CONVERT_EXPR_P (whats_returned) |
9216 | || TREE_CODE (whats_returned) == NON_LVALUE_EXPR) | |
efee38a9 | 9217 | whats_returned = TREE_OPERAND (whats_returned, 0); |
2bfa73e4 BS |
9218 | else |
9219 | break; | |
9220 | } | |
9221 | ||
04eb9c55 JM |
9222 | if (TREE_CODE (whats_returned) == TARGET_EXPR |
9223 | && is_std_init_list (TREE_TYPE (whats_returned))) | |
9224 | { | |
9225 | tree init = TARGET_EXPR_INITIAL (whats_returned); | |
9226 | if (TREE_CODE (init) == CONSTRUCTOR) | |
9227 | /* Pull out the array address. */ | |
9228 | whats_returned = CONSTRUCTOR_ELT (init, 0)->value; | |
9229 | else if (TREE_CODE (init) == INDIRECT_REF) | |
9230 | /* The source of a trivial copy looks like *(T*)&var. */ | |
9231 | whats_returned = TREE_OPERAND (init, 0); | |
9232 | else | |
9233 | return false; | |
9234 | STRIP_NOPS (whats_returned); | |
9235 | } | |
9236 | ||
308d9cda MP |
9237 | /* As a special case, we handle a call to std::move or std::forward. */ |
9238 | if (TREE_CODE (whats_returned) == CALL_EXPR | |
9239 | && (is_std_move_p (whats_returned) | |
9240 | || is_std_forward_p (whats_returned))) | |
9241 | { | |
9242 | tree arg = CALL_EXPR_ARG (whats_returned, 0); | |
9243 | return maybe_warn_about_returning_address_of_local (arg); | |
9244 | } | |
9245 | ||
2bfa73e4 | 9246 | if (TREE_CODE (whats_returned) != ADDR_EXPR) |
b4dfdc11 | 9247 | return false; |
c8094d83 | 9248 | whats_returned = TREE_OPERAND (whats_returned, 0); |
2bfa73e4 | 9249 | |
75d850f3 PP |
9250 | while (TREE_CODE (whats_returned) == COMPONENT_REF |
9251 | || TREE_CODE (whats_returned) == ARRAY_REF) | |
9252 | whats_returned = TREE_OPERAND (whats_returned, 0); | |
9253 | ||
04eb9c55 JM |
9254 | if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR |
9255 | || TREE_CODE (whats_returned) == TARGET_EXPR) | |
2bfa73e4 | 9256 | { |
04eb9c55 JM |
9257 | if (TYPE_REF_P (valtype)) |
9258 | warning_at (loc, OPT_Wreturn_local_addr, | |
9259 | "returning reference to temporary"); | |
9260 | else if (is_std_init_list (valtype)) | |
9261 | warning_at (loc, OPT_Winit_list_lifetime, | |
9262 | "returning temporary initializer_list does not extend " | |
9263 | "the lifetime of the underlying array"); | |
9264 | return true; | |
efee38a9 | 9265 | } |
efee38a9 | 9266 | |
dfd7fdca DM |
9267 | STRIP_ANY_LOCATION_WRAPPER (whats_returned); |
9268 | ||
a2f1f4c3 | 9269 | if (DECL_P (whats_returned) |
2bfa73e4 BS |
9270 | && DECL_NAME (whats_returned) |
9271 | && DECL_FUNCTION_SCOPE_P (whats_returned) | |
5ced939e | 9272 | && !is_capture_proxy (whats_returned) |
2bfa73e4 BS |
9273 | && !(TREE_STATIC (whats_returned) |
9274 | || TREE_PUBLIC (whats_returned))) | |
9275 | { | |
f9f3b77c JJ |
9276 | if (VAR_P (whats_returned) |
9277 | && DECL_DECOMPOSITION_P (whats_returned) | |
9278 | && DECL_DECOMP_BASE (whats_returned) | |
9279 | && DECL_HAS_VALUE_EXPR_P (whats_returned)) | |
9280 | { | |
9281 | /* When returning address of a structured binding, if the structured | |
9282 | binding is not a reference, continue normally, if it is a | |
9283 | reference, recurse on the initializer of the structured | |
9284 | binding. */ | |
9285 | tree base = DECL_DECOMP_BASE (whats_returned); | |
9286 | if (TYPE_REF_P (TREE_TYPE (base))) | |
9287 | { | |
9288 | tree init = DECL_INITIAL (base); | |
9289 | return maybe_warn_about_returning_address_of_local (init); | |
9290 | } | |
9291 | } | |
04eb9c55 | 9292 | bool w = false; |
097f82ec | 9293 | auto_diagnostic_group d; |
9f613f06 | 9294 | if (TYPE_REF_P (valtype)) |
04eb9c55 JM |
9295 | w = warning_at (loc, OPT_Wreturn_local_addr, |
9296 | "reference to local variable %qD returned", | |
9297 | whats_returned); | |
9298 | else if (is_std_init_list (valtype)) | |
9299 | w = warning_at (loc, OPT_Winit_list_lifetime, | |
9300 | "returning local initializer_list variable %qD " | |
9301 | "does not extend the lifetime of the underlying array", | |
9302 | whats_returned); | |
19fc9faa | 9303 | else if (TREE_CODE (whats_returned) == LABEL_DECL) |
04eb9c55 JM |
9304 | w = warning_at (loc, OPT_Wreturn_local_addr, |
9305 | "address of label %qD returned", | |
9306 | whats_returned); | |
2bfa73e4 | 9307 | else |
04eb9c55 JM |
9308 | w = warning_at (loc, OPT_Wreturn_local_addr, |
9309 | "address of local variable %qD returned", | |
9310 | whats_returned); | |
9311 | if (w) | |
9312 | inform (DECL_SOURCE_LOCATION (whats_returned), | |
9313 | "declared here"); | |
b4dfdc11 | 9314 | return true; |
efee38a9 | 9315 | } |
b4dfdc11 MG |
9316 | |
9317 | return false; | |
efee38a9 MM |
9318 | } |
9319 | ||
79ca9c35 MP |
9320 | /* Returns true if DECL is in the std namespace. */ |
9321 | ||
9322 | static bool | |
9323 | decl_in_std_namespace_p (tree decl) | |
9324 | { | |
9325 | return (decl != NULL_TREE | |
9326 | && DECL_NAMESPACE_STD_P (decl_namespace_context (decl))); | |
9327 | } | |
9328 | ||
308d9cda MP |
9329 | /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */ |
9330 | ||
9331 | static bool | |
9332 | is_std_forward_p (tree fn) | |
9333 | { | |
9334 | /* std::forward only takes one argument. */ | |
9335 | if (call_expr_nargs (fn) != 1) | |
9336 | return false; | |
9337 | ||
9338 | tree fndecl = cp_get_callee_fndecl_nofold (fn); | |
9339 | if (!decl_in_std_namespace_p (fndecl)) | |
9340 | return false; | |
9341 | ||
9342 | tree name = DECL_NAME (fndecl); | |
9343 | return name && id_equal (name, "forward"); | |
9344 | } | |
9345 | ||
79ca9c35 MP |
9346 | /* Returns true if FN, a CALL_EXPR, is a call to std::move. */ |
9347 | ||
9348 | static bool | |
9349 | is_std_move_p (tree fn) | |
9350 | { | |
9351 | /* std::move only takes one argument. */ | |
9352 | if (call_expr_nargs (fn) != 1) | |
9353 | return false; | |
9354 | ||
9355 | tree fndecl = cp_get_callee_fndecl_nofold (fn); | |
9356 | if (!decl_in_std_namespace_p (fndecl)) | |
9357 | return false; | |
9358 | ||
9359 | tree name = DECL_NAME (fndecl); | |
9360 | return name && id_equal (name, "move"); | |
9361 | } | |
9362 | ||
9363 | /* Returns true if RETVAL is a good candidate for the NRVO as per | |
9364 | [class.copy.elision]. FUNCTYPE is the type the function is declared | |
9365 | to return. */ | |
9366 | ||
9367 | static bool | |
9368 | can_do_nrvo_p (tree retval, tree functype) | |
9369 | { | |
f80aa399 MP |
9370 | if (functype == error_mark_node) |
9371 | return false; | |
dfd7fdca DM |
9372 | if (retval) |
9373 | STRIP_ANY_LOCATION_WRAPPER (retval); | |
79ca9c35 MP |
9374 | tree result = DECL_RESULT (current_function_decl); |
9375 | return (retval != NULL_TREE | |
9376 | && !processing_template_decl | |
9377 | /* Must be a local, automatic variable. */ | |
9378 | && VAR_P (retval) | |
9379 | && DECL_CONTEXT (retval) == current_function_decl | |
9380 | && !TREE_STATIC (retval) | |
9381 | /* And not a lambda or anonymous union proxy. */ | |
9382 | && !DECL_HAS_VALUE_EXPR_P (retval) | |
9383 | && (DECL_ALIGN (retval) <= DECL_ALIGN (result)) | |
9384 | /* The cv-unqualified type of the returned value must be the | |
9385 | same as the cv-unqualified return type of the | |
9386 | function. */ | |
9387 | && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))), | |
9388 | (TYPE_MAIN_VARIANT (functype))) | |
9389 | /* And the returned value must be non-volatile. */ | |
9390 | && !TYPE_VOLATILE (TREE_TYPE (retval))); | |
9391 | } | |
9392 | ||
b5be6d0c | 9393 | /* Returns true if we should treat RETVAL, an expression being returned, |
74be6f51 MP |
9394 | as if it were designated by an rvalue. See [class.copy.elision]. |
9395 | PARM_P is true if a function parameter is OK in this context. */ | |
b5be6d0c | 9396 | |
74be6f51 MP |
9397 | bool |
9398 | treat_lvalue_as_rvalue_p (tree retval, bool parm_ok) | |
b5be6d0c | 9399 | { |
dfd7fdca | 9400 | STRIP_ANY_LOCATION_WRAPPER (retval); |
b5be6d0c MP |
9401 | return ((cxx_dialect != cxx98) |
9402 | && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval)) | |
74be6f51 | 9403 | || (parm_ok && TREE_CODE (retval) == PARM_DECL)) |
b5be6d0c MP |
9404 | && DECL_CONTEXT (retval) == current_function_decl |
9405 | && !TREE_STATIC (retval)); | |
9406 | } | |
9407 | ||
79ca9c35 MP |
9408 | /* Warn about wrong usage of std::move in a return statement. RETVAL |
9409 | is the expression we are returning; FUNCTYPE is the type the function | |
9410 | is declared to return. */ | |
9411 | ||
9412 | static void | |
9413 | maybe_warn_pessimizing_move (tree retval, tree functype) | |
9414 | { | |
b5be6d0c | 9415 | if (!(warn_pessimizing_move || warn_redundant_move)) |
79ca9c35 MP |
9416 | return; |
9417 | ||
b5be6d0c MP |
9418 | location_t loc = cp_expr_loc_or_loc (retval, input_location); |
9419 | ||
79ca9c35 MP |
9420 | /* C++98 doesn't know move. */ |
9421 | if (cxx_dialect < cxx11) | |
9422 | return; | |
9423 | ||
0940fe59 MP |
9424 | /* Wait until instantiation time, since we can't gauge if we should do |
9425 | the NRVO until then. */ | |
9426 | if (processing_template_decl) | |
9427 | return; | |
9428 | ||
79ca9c35 MP |
9429 | /* This is only interesting for class types. */ |
9430 | if (!CLASS_TYPE_P (functype)) | |
9431 | return; | |
9432 | ||
ef7e79a3 | 9433 | /* We're looking for *std::move<T&> ((T &) &arg). */ |
79ca9c35 MP |
9434 | if (REFERENCE_REF_P (retval) |
9435 | && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR) | |
9436 | { | |
9437 | tree fn = TREE_OPERAND (retval, 0); | |
9438 | if (is_std_move_p (fn)) | |
9439 | { | |
9440 | tree arg = CALL_EXPR_ARG (fn, 0); | |
ef7e79a3 MP |
9441 | if (TREE_CODE (arg) != NOP_EXPR) |
9442 | return; | |
9443 | arg = TREE_OPERAND (arg, 0); | |
1439f355 MP |
9444 | if (TREE_CODE (arg) != ADDR_EXPR) |
9445 | return; | |
9446 | arg = TREE_OPERAND (arg, 0); | |
b5be6d0c | 9447 | arg = convert_from_reference (arg); |
79ca9c35 MP |
9448 | /* Warn if we could do copy elision were it not for the move. */ |
9449 | if (can_do_nrvo_p (arg, functype)) | |
9450 | { | |
9451 | auto_diagnostic_group d; | |
b5be6d0c | 9452 | if (warning_at (loc, OPT_Wpessimizing_move, |
79ca9c35 MP |
9453 | "moving a local object in a return statement " |
9454 | "prevents copy elision")) | |
b5be6d0c MP |
9455 | inform (loc, "remove %<std::move%> call"); |
9456 | } | |
9457 | /* Warn if the move is redundant. It is redundant when we would | |
9458 | do maybe-rvalue overload resolution even without std::move. */ | |
ef7e79a3 MP |
9459 | else if (warn_redundant_move |
9460 | && treat_lvalue_as_rvalue_p (arg, /*parm_ok*/true)) | |
b5be6d0c | 9461 | { |
ec2de569 MP |
9462 | /* Make sure that the overload resolution would actually succeed |
9463 | if we removed the std::move call. */ | |
9464 | tree t = convert_for_initialization (NULL_TREE, functype, | |
9465 | move (arg), | |
9466 | (LOOKUP_NORMAL | |
9467 | | LOOKUP_ONLYCONVERTING | |
9468 | | LOOKUP_PREFER_RVALUE), | |
9469 | ICR_RETURN, NULL_TREE, 0, | |
9470 | tf_none); | |
9471 | /* If this worked, implicit rvalue would work, so the call to | |
9472 | std::move is redundant. */ | |
9473 | if (t != error_mark_node) | |
9474 | { | |
9475 | auto_diagnostic_group d; | |
9476 | if (warning_at (loc, OPT_Wredundant_move, | |
9477 | "redundant move in return statement")) | |
9478 | inform (loc, "remove %<std::move%> call"); | |
9479 | } | |
79ca9c35 MP |
9480 | } |
9481 | } | |
9482 | } | |
9483 | } | |
9484 | ||
0e339752 | 9485 | /* Check that returning RETVAL from the current function is valid. |
efee38a9 MM |
9486 | Return an expression explicitly showing all conversions required to |
9487 | change RETVAL into the function return type, and to assign it to | |
0c9b182b JJ |
9488 | the DECL_RESULT for the function. Set *NO_WARNING to true if |
9489 | code reaches end of non-void function warning shouldn't be issued | |
9490 | on this RETURN_EXPR. */ | |
efee38a9 MM |
9491 | |
9492 | tree | |
0c9b182b | 9493 | check_return_expr (tree retval, bool *no_warning) |
8d08fdba | 9494 | { |
efee38a9 | 9495 | tree result; |
852497a3 | 9496 | /* The type actually returned by the function. */ |
efee38a9 | 9497 | tree valtype; |
852497a3 JM |
9498 | /* The type the function is declared to return, or void if |
9499 | the declared type is incomplete. */ | |
9500 | tree functype; | |
efee38a9 MM |
9501 | int fn_returns_value_p; |
9502 | ||
0c9b182b JJ |
9503 | *no_warning = false; |
9504 | ||
efee38a9 MM |
9505 | /* A `volatile' function is one that isn't supposed to return, ever. |
9506 | (This is a G++ extension, used to get better code for functions | |
9507 | that call the `volatile' function.) */ | |
8d08fdba | 9508 | if (TREE_THIS_VOLATILE (current_function_decl)) |
d4ee4d25 | 9509 | warning (0, "function declared %<noreturn%> has a %<return%> statement"); |
8d08fdba | 9510 | |
efee38a9 | 9511 | /* Check for various simple errors. */ |
a0de9d20 | 9512 | if (DECL_DESTRUCTOR_P (current_function_decl)) |
8d08fdba | 9513 | { |
1c2c08a5 | 9514 | if (retval) |
8251199e | 9515 | error ("returning a value from a destructor"); |
efee38a9 | 9516 | return NULL_TREE; |
1c2c08a5 | 9517 | } |
90418208 | 9518 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) |
0dde4175 | 9519 | { |
90418208 JM |
9520 | if (in_function_try_handler) |
9521 | /* If a return statement appears in a handler of the | |
c6002625 | 9522 | function-try-block of a constructor, the program is ill-formed. */ |
90418208 JM |
9523 | error ("cannot return from a handler of a function-try-block of a constructor"); |
9524 | else if (retval) | |
9525 | /* You can't return a value from a constructor. */ | |
9526 | error ("returning a value from a constructor"); | |
9527 | return NULL_TREE; | |
efee38a9 | 9528 | } |
efee38a9 | 9529 | |
11f2c78a PP |
9530 | const tree saved_retval = retval; |
9531 | ||
efc7052d JM |
9532 | if (processing_template_decl) |
9533 | { | |
9534 | current_function_returns_value = 1; | |
11f2c78a | 9535 | |
7b3e2d46 | 9536 | if (check_for_bare_parameter_packs (retval)) |
11f2c78a PP |
9537 | return error_mark_node; |
9538 | ||
4d612bfd JM |
9539 | /* If one of the types might be void, we can't tell whether we're |
9540 | returning a value. */ | |
9541 | if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))) | |
9542 | && !current_function_auto_return_pattern) | |
11f2c78a | 9543 | || (retval != NULL_TREE |
4d612bfd JM |
9544 | && (TREE_TYPE (retval) == NULL_TREE |
9545 | || WILDCARD_TYPE_P (TREE_TYPE (retval))))) | |
9546 | goto dependent; | |
efc7052d | 9547 | } |
c8094d83 | 9548 | |
852497a3 JM |
9549 | functype = TREE_TYPE (TREE_TYPE (current_function_decl)); |
9550 | ||
9551 | /* Deduce auto return type from a return statement. */ | |
9552 | if (current_function_auto_return_pattern) | |
9553 | { | |
9554 | tree auto_node; | |
9555 | tree type; | |
9556 | ||
9557 | if (!retval && !is_auto (current_function_auto_return_pattern)) | |
9558 | { | |
9559 | /* Give a helpful error message. */ | |
9560 | error ("return-statement with no value, in function returning %qT", | |
9561 | current_function_auto_return_pattern); | |
9562 | inform (input_location, "only plain %<auto%> return type can be " | |
9563 | "deduced to %<void%>"); | |
9564 | type = error_mark_node; | |
9565 | } | |
2aa953d0 JM |
9566 | else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval)) |
9567 | { | |
9568 | error ("returning initializer list"); | |
9569 | type = error_mark_node; | |
9570 | } | |
852497a3 JM |
9571 | else |
9572 | { | |
9573 | if (!retval) | |
632f2871 | 9574 | retval = void_node; |
852497a3 JM |
9575 | auto_node = type_uses_auto (current_function_auto_return_pattern); |
9576 | type = do_auto_deduction (current_function_auto_return_pattern, | |
9577 | retval, auto_node); | |
9578 | } | |
9579 | ||
9580 | if (type == error_mark_node) | |
9581 | /* Leave it. */; | |
9582 | else if (functype == current_function_auto_return_pattern) | |
9583 | apply_deduced_return_type (current_function_decl, type); | |
5dc58049 JM |
9584 | else if (!same_type_p (type, functype)) |
9585 | { | |
9586 | if (LAMBDA_FUNCTION_P (current_function_decl)) | |
9587 | error ("inconsistent types %qT and %qT deduced for " | |
9588 | "lambda return type", functype, type); | |
9589 | else | |
9590 | error ("inconsistent deduction for auto return type: " | |
9591 | "%qT and then %qT", functype, type); | |
9592 | } | |
852497a3 JM |
9593 | functype = type; |
9594 | } | |
9595 | ||
efee38a9 MM |
9596 | result = DECL_RESULT (current_function_decl); |
9597 | valtype = TREE_TYPE (result); | |
50bc768d | 9598 | gcc_assert (valtype != NULL_TREE); |
b72801e2 | 9599 | fn_returns_value_p = !VOID_TYPE_P (valtype); |
efee38a9 MM |
9600 | |
9601 | /* Check for a return statement with no return value in a function | |
9602 | that's supposed to return a value. */ | |
9603 | if (!retval && fn_returns_value_p) | |
9604 | { | |
852497a3 JM |
9605 | if (functype != error_mark_node) |
9606 | permerror (input_location, "return-statement with no value, in " | |
9607 | "function returning %qT", valtype); | |
9608 | /* Remember that this function did return. */ | |
9609 | current_function_returns_value = 1; | |
0c9b182b | 9610 | /* And signal caller that TREE_NO_WARNING should be set on the |
3db45ab5 MS |
9611 | RETURN_EXPR to avoid control reaches end of non-void function |
9612 | warnings in tree-cfg.c. */ | |
0c9b182b | 9613 | *no_warning = true; |
efee38a9 MM |
9614 | } |
9615 | /* Check for a return statement with a value in a function that | |
9616 | isn't supposed to return a value. */ | |
9617 | else if (retval && !fn_returns_value_p) | |
c8094d83 | 9618 | { |
b72801e2 | 9619 | if (VOID_TYPE_P (TREE_TYPE (retval))) |
efee38a9 MM |
9620 | /* You can return a `void' value from a function of `void' |
9621 | type. In that case, we have to evaluate the expression for | |
9622 | its side-effects. */ | |
ff630487 | 9623 | finish_expr_stmt (retval); |
efee38a9 | 9624 | else |
ff630487 NS |
9625 | permerror (input_location, |
9626 | "return-statement with a value, in function " | |
9627 | "returning %qT", valtype); | |
efee38a9 MM |
9628 | current_function_returns_null = 1; |
9629 | ||
9630 | /* There's really no value to return, after all. */ | |
9631 | return NULL_TREE; | |
0dde4175 | 9632 | } |
efee38a9 MM |
9633 | else if (!retval) |
9634 | /* Remember that this function can sometimes return without a | |
9635 | value. */ | |
9636 | current_function_returns_null = 1; | |
90418208 JM |
9637 | else |
9638 | /* Remember that this function did return a value. */ | |
9639 | current_function_returns_value = 1; | |
1c2c08a5 | 9640 | |
276318a5 | 9641 | /* Check for erroneous operands -- but after giving ourselves a |
5ae9ba3e MM |
9642 | chance to provide an error about returning a value from a void |
9643 | function. */ | |
9644 | if (error_operand_p (retval)) | |
9645 | { | |
9646 | current_function_return_value = error_mark_node; | |
9647 | return error_mark_node; | |
9648 | } | |
9649 | ||
7f477e81 | 9650 | /* Only operator new(...) throw(), can return NULL [expr.new/13]. */ |
9918e796 | 9651 | if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl)) |
7f477e81 | 9652 | && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl)) |
708cae97 | 9653 | && ! flag_check_new |
aedfac0e | 9654 | && retval && null_ptr_cst_p (retval)) |
d4ee4d25 | 9655 | warning (0, "%<operator new%> must not return NULL unless it is " |
0cbd7506 | 9656 | "declared %<throw()%> (or -fcheck-new is in effect)"); |
8d08fdba | 9657 | |
824b9a4c | 9658 | /* Effective C++ rule 15. See also start_function. */ |
eb448459 | 9659 | if (warn_ecpp |
dbf71703 JJ |
9660 | && DECL_NAME (current_function_decl) == assign_op_identifier |
9661 | && !type_dependent_expression_p (retval)) | |
47293da3 GB |
9662 | { |
9663 | bool warn = true; | |
9664 | ||
9665 | /* The function return type must be a reference to the current | |
9666 | class. */ | |
9f613f06 | 9667 | if (TYPE_REF_P (valtype) |
47293da3 GB |
9668 | && same_type_ignoring_top_level_qualifiers_p |
9669 | (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))) | |
9670 | { | |
9671 | /* Returning '*this' is obviously OK. */ | |
9672 | if (retval == current_class_ref) | |
9673 | warn = false; | |
9674 | /* If we are calling a function whose return type is the same of | |
9675 | the current class reference, it is ok. */ | |
591cb3cf | 9676 | else if (INDIRECT_REF_P (retval) |
47293da3 | 9677 | && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR) |
616adc47 | 9678 | warn = false; |
47293da3 GB |
9679 | } |
9680 | ||
9681 | if (warn) | |
b323323f | 9682 | warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>"); |
47293da3 | 9683 | } |
824b9a4c | 9684 | |
4d612bfd JM |
9685 | if (dependent_type_p (functype) |
9686 | || type_dependent_expression_p (retval)) | |
11f2c78a | 9687 | { |
4d612bfd | 9688 | dependent: |
11f2c78a PP |
9689 | /* We should not have changed the return value. */ |
9690 | gcc_assert (retval == saved_retval); | |
c1051bf7 | 9691 | return retval; |
11f2c78a PP |
9692 | } |
9693 | ||
07b2f2fd JM |
9694 | /* The fabled Named Return Value optimization, as per [class.copy]/15: |
9695 | ||
9696 | [...] For a function with a class return type, if the expression | |
9697 | in the return statement is the name of a local object, and the cv- | |
9698 | unqualified type of the local object is the same as the function | |
9699 | return type, an implementation is permitted to omit creating the tem- | |
9700 | porary object to hold the function return value [...] | |
9701 | ||
9702 | So, if this is a value-returning function that always returns the same | |
9703 | local variable, remember it. | |
0d97bf4c JM |
9704 | |
9705 | It might be nice to be more flexible, and choose the first suitable | |
9706 | variable even if the function sometimes returns something else, but | |
9707 | then we run the risk of clobbering the variable we chose if the other | |
9708 | returned expression uses the chosen variable somehow. And people expect | |
07b2f2fd JM |
9709 | this restriction, anyway. (jason 2000-11-19) |
9710 | ||
324f9dfb | 9711 | See finish_function and finalize_nrv for the rest of this optimization. */ |
dfd7fdca DM |
9712 | if (retval) |
9713 | STRIP_ANY_LOCATION_WRAPPER (retval); | |
0d97bf4c | 9714 | |
79ca9c35 | 9715 | bool named_return_value_okay_p = can_do_nrvo_p (retval, functype); |
01f9e964 | 9716 | if (fn_returns_value_p && flag_elide_constructors) |
0d97bf4c | 9717 | { |
8af2fec4 RY |
9718 | if (named_return_value_okay_p |
9719 | && (current_function_return_value == NULL_TREE | |
9720 | || current_function_return_value == retval)) | |
0d97bf4c JM |
9721 | current_function_return_value = retval; |
9722 | else | |
9723 | current_function_return_value = error_mark_node; | |
9724 | } | |
9725 | ||
efee38a9 MM |
9726 | /* We don't need to do any conversions when there's nothing being |
9727 | returned. */ | |
5ae9ba3e MM |
9728 | if (!retval) |
9729 | return NULL_TREE; | |
efee38a9 | 9730 | |
79ca9c35 MP |
9731 | if (!named_return_value_okay_p) |
9732 | maybe_warn_pessimizing_move (retval, functype); | |
9733 | ||
efee38a9 MM |
9734 | /* Do any required conversions. */ |
9735 | if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl)) | |
9736 | /* No conversions are required. */ | |
9737 | ; | |
8d08fdba MS |
9738 | else |
9739 | { | |
8af2fec4 | 9740 | int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING; |
cce2be43 | 9741 | |
51b15ede NS |
9742 | /* The functype's return type will have been set to void, if it |
9743 | was an incomplete type. Just treat this as 'return;' */ | |
9744 | if (VOID_TYPE_P (functype)) | |
9745 | return error_mark_node; | |
3db45ab5 | 9746 | |
96e780c0 JM |
9747 | /* If we had an id-expression obfuscated by force_paren_expr, we need |
9748 | to undo it so we can try to treat it as an rvalue below. */ | |
1137001c | 9749 | retval = maybe_undo_parenthesized_ref (retval); |
96e780c0 | 9750 | |
035181c3 JM |
9751 | if (processing_template_decl) |
9752 | retval = build_non_dependent_expr (retval); | |
9753 | ||
fb682f94 | 9754 | /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes |
8af2fec4 | 9755 | treated as an rvalue for the purposes of overload resolution to |
532c679e JM |
9756 | favor move constructors over copy constructors. |
9757 | ||
9758 | Note that these conditions are similar to, but not as strict as, | |
9759 | the conditions for the named return value optimization. */ | |
4ce8c5de | 9760 | bool converted = false; |
74be6f51 | 9761 | if (treat_lvalue_as_rvalue_p (retval, /*parm_ok*/true) |
532c679e | 9762 | /* This is only interesting for class type. */ |
852497a3 | 9763 | && CLASS_TYPE_P (functype)) |
4ce8c5de JM |
9764 | { |
9765 | tree moved = move (retval); | |
9766 | moved = convert_for_initialization | |
9767 | (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE, | |
9768 | ICR_RETURN, NULL_TREE, 0, tf_none); | |
9769 | if (moved != error_mark_node) | |
9770 | { | |
9771 | retval = moved; | |
9772 | converted = true; | |
9773 | } | |
9774 | } | |
8af2fec4 | 9775 | |
538a5308 JM |
9776 | /* The call in a (lambda) thunk needs no conversions. */ |
9777 | if (TREE_CODE (retval) == CALL_EXPR | |
9778 | && CALL_FROM_THUNK_P (retval)) | |
9779 | converted = true; | |
9780 | ||
cce2be43 JM |
9781 | /* First convert the value to the function's return type, then |
9782 | to the type of return value's location to handle the | |
0cbd7506 | 9783 | case that functype is smaller than the valtype. */ |
4ce8c5de JM |
9784 | if (!converted) |
9785 | retval = convert_for_initialization | |
9786 | (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0, | |
9787 | tf_warning_or_error); | |
cce2be43 JM |
9788 | retval = convert (valtype, retval); |
9789 | ||
efee38a9 | 9790 | /* If the conversion failed, treat this just like `return;'. */ |
691c003d | 9791 | if (retval == error_mark_node) |
90418208 | 9792 | return retval; |
02531345 | 9793 | /* We can't initialize a register from a AGGR_INIT_EXPR. */ |
e3b5732b | 9794 | else if (! cfun->returns_struct |
c1bc6829 | 9795 | && TREE_CODE (retval) == TARGET_EXPR |
02531345 | 9796 | && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR) |
f293ce4b RS |
9797 | retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, |
9798 | TREE_OPERAND (retval, 0)); | |
d9697837 | 9799 | else if (!processing_template_decl |
04eb9c55 JM |
9800 | && maybe_warn_about_returning_address_of_local (retval) |
9801 | && INDIRECT_TYPE_P (valtype)) | |
b4dfdc11 MG |
9802 | retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, |
9803 | build_zero_cst (TREE_TYPE (retval))); | |
8d08fdba | 9804 | } |
c8094d83 | 9805 | |
4d612bfd JM |
9806 | if (processing_template_decl) |
9807 | return saved_retval; | |
9808 | ||
efee38a9 | 9809 | /* Actually copy the value returned into the appropriate location. */ |
691c003d | 9810 | if (retval && retval != result) |
f293ce4b | 9811 | retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval); |
b88c08b6 | 9812 | |
efee38a9 MM |
9813 | return retval; |
9814 | } | |
9815 | ||
8d08fdba | 9816 | \f |
838dfd8a | 9817 | /* Returns nonzero if the pointer-type FROM can be converted to the |
ceab47eb | 9818 | pointer-type TO via a qualification conversion. If CONSTP is -1, |
838dfd8a | 9819 | then we return nonzero if the pointers are similar, and the |
ceab47eb MM |
9820 | cv-qualification signature of FROM is a proper subset of that of TO. |
9821 | ||
9822 | If CONSTP is positive, then all outer pointers have been | |
9823 | const-qualified. */ | |
e92cc029 | 9824 | |
bd6dd845 | 9825 | static int |
acd8e2d0 | 9826 | comp_ptr_ttypes_real (tree to, tree from, int constp) |
a0a33927 | 9827 | { |
a5ac359a | 9828 | bool to_more_cv_qualified = false; |
f83c7f63 | 9829 | bool is_opaque_pointer = false; |
ceab47eb | 9830 | |
a0a33927 MS |
9831 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) |
9832 | { | |
9833 | if (TREE_CODE (to) != TREE_CODE (from)) | |
9834 | return 0; | |
9835 | ||
d11ad92e | 9836 | if (TREE_CODE (from) == OFFSET_TYPE |
b7a78333 MM |
9837 | && !same_type_p (TYPE_OFFSET_BASETYPE (from), |
9838 | TYPE_OFFSET_BASETYPE (to))) | |
9839 | return 0; | |
d11ad92e | 9840 | |
f30432d7 MS |
9841 | /* Const and volatile mean something different for function types, |
9842 | so the usual checks are not appropriate. */ | |
9843 | if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE) | |
9844 | { | |
6c39e757 | 9845 | if (!at_least_as_qualified_p (to, from)) |
02020185 | 9846 | return 0; |
a0a33927 | 9847 | |
6c39e757 | 9848 | if (!at_least_as_qualified_p (from, to)) |
02020185 | 9849 | { |
ceab47eb MM |
9850 | if (constp == 0) |
9851 | return 0; | |
a5ac359a | 9852 | to_more_cv_qualified = true; |
ceab47eb MM |
9853 | } |
9854 | ||
9855 | if (constp > 0) | |
6c39e757 | 9856 | constp &= TYPE_READONLY (to); |
f30432d7 | 9857 | } |
a0a33927 | 9858 | |
b55b02ea | 9859 | if (VECTOR_TYPE_P (to)) |
f83c7f63 DJ |
9860 | is_opaque_pointer = vector_targets_convertible_p (to, from); |
9861 | ||
50e10fa8 | 9862 | if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to)) |
8de9bb0e | 9863 | return ((constp >= 0 || to_more_cv_qualified) |
f83c7f63 DJ |
9864 | && (is_opaque_pointer |
9865 | || same_type_ignoring_top_level_qualifiers_p (to, from))); | |
a0a33927 MS |
9866 | } |
9867 | } | |
9868 | ||
8de9bb0e NS |
9869 | /* When comparing, say, char ** to char const **, this function takes |
9870 | the 'char *' and 'char const *'. Do not pass non-pointer/reference | |
9871 | types to this function. */ | |
e92cc029 | 9872 | |
a0a33927 | 9873 | int |
acd8e2d0 | 9874 | comp_ptr_ttypes (tree to, tree from) |
a0a33927 MS |
9875 | { |
9876 | return comp_ptr_ttypes_real (to, from, 1); | |
9877 | } | |
d11ad92e | 9878 | |
d4684c00 JM |
9879 | /* Returns true iff FNTYPE is a non-class type that involves |
9880 | error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node | |
9881 | if a parameter type is ill-formed. */ | |
9882 | ||
9883 | bool | |
9884 | error_type_p (const_tree type) | |
9885 | { | |
9886 | tree t; | |
9887 | ||
9888 | switch (TREE_CODE (type)) | |
9889 | { | |
9890 | case ERROR_MARK: | |
9891 | return true; | |
9892 | ||
9893 | case POINTER_TYPE: | |
9894 | case REFERENCE_TYPE: | |
9895 | case OFFSET_TYPE: | |
9896 | return error_type_p (TREE_TYPE (type)); | |
9897 | ||
9898 | case FUNCTION_TYPE: | |
9899 | case METHOD_TYPE: | |
9900 | if (error_type_p (TREE_TYPE (type))) | |
9901 | return true; | |
9902 | for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t)) | |
9903 | if (error_type_p (TREE_VALUE (t))) | |
9904 | return true; | |
9905 | return false; | |
9906 | ||
9907 | case RECORD_TYPE: | |
9908 | if (TYPE_PTRMEMFUNC_P (type)) | |
9909 | return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type)); | |
9910 | return false; | |
9911 | ||
9912 | default: | |
9913 | return false; | |
9914 | } | |
9915 | } | |
9916 | ||
3f2d352c | 9917 | /* Returns true if to and from are (possibly multi-level) pointers to the same |
d11ad92e MS |
9918 | type or inheritance-related types, regardless of cv-quals. */ |
9919 | ||
3f2d352c | 9920 | bool |
58f9752a | 9921 | ptr_reasonably_similar (const_tree to, const_tree from) |
d11ad92e MS |
9922 | { |
9923 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) | |
9924 | { | |
72a08131 | 9925 | /* Any target type is similar enough to void. */ |
50e10fa8 | 9926 | if (VOID_TYPE_P (to)) |
d4684c00 | 9927 | return !error_type_p (from); |
50e10fa8 | 9928 | if (VOID_TYPE_P (from)) |
d4684c00 | 9929 | return !error_type_p (to); |
72a08131 | 9930 | |
d11ad92e | 9931 | if (TREE_CODE (to) != TREE_CODE (from)) |
3f2d352c | 9932 | return false; |
d11ad92e MS |
9933 | |
9934 | if (TREE_CODE (from) == OFFSET_TYPE | |
96d84882 PB |
9935 | && comptypes (TYPE_OFFSET_BASETYPE (to), |
9936 | TYPE_OFFSET_BASETYPE (from), | |
9937 | COMPARE_BASE | COMPARE_DERIVED)) | |
d11ad92e MS |
9938 | continue; |
9939 | ||
b55b02ea | 9940 | if (VECTOR_TYPE_P (to) |
00c8e9f6 | 9941 | && vector_types_convertible_p (to, from, false)) |
3f2d352c | 9942 | return true; |
d70b8c3a | 9943 | |
8a2b77e7 JM |
9944 | if (TREE_CODE (to) == INTEGER_TYPE |
9945 | && TYPE_PRECISION (to) == TYPE_PRECISION (from)) | |
3f2d352c | 9946 | return true; |
8a2b77e7 JM |
9947 | |
9948 | if (TREE_CODE (to) == FUNCTION_TYPE) | |
d4684c00 | 9949 | return !error_type_p (to) && !error_type_p (from); |
8a2b77e7 | 9950 | |
50e10fa8 | 9951 | if (!TYPE_PTR_P (to)) |
3f2d352c PC |
9952 | { |
9953 | /* When either type is incomplete avoid DERIVED_FROM_P, | |
9954 | which may call complete_type (c++/57942). */ | |
9955 | bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from); | |
9956 | return comptypes | |
9957 | (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), | |
9958 | b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED); | |
9959 | } | |
d11ad92e MS |
9960 | } |
9961 | } | |
c11b6f21 | 9962 | |
34b5375f MM |
9963 | /* Return true if TO and FROM (both of which are POINTER_TYPEs or |
9964 | pointer-to-member types) are the same, ignoring cv-qualification at | |
9965 | all levels. */ | |
c11b6f21 | 9966 | |
34b5375f | 9967 | bool |
acd8e2d0 | 9968 | comp_ptr_ttypes_const (tree to, tree from) |
c11b6f21 | 9969 | { |
f83c7f63 DJ |
9970 | bool is_opaque_pointer = false; |
9971 | ||
c11b6f21 MS |
9972 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) |
9973 | { | |
9974 | if (TREE_CODE (to) != TREE_CODE (from)) | |
34b5375f | 9975 | return false; |
c11b6f21 MS |
9976 | |
9977 | if (TREE_CODE (from) == OFFSET_TYPE | |
3bfdc719 MM |
9978 | && same_type_p (TYPE_OFFSET_BASETYPE (from), |
9979 | TYPE_OFFSET_BASETYPE (to))) | |
c11b6f21 MS |
9980 | continue; |
9981 | ||
b55b02ea | 9982 | if (VECTOR_TYPE_P (to)) |
f83c7f63 DJ |
9983 | is_opaque_pointer = vector_targets_convertible_p (to, from); |
9984 | ||
50e10fa8 | 9985 | if (!TYPE_PTR_P (to)) |
f83c7f63 DJ |
9986 | return (is_opaque_pointer |
9987 | || same_type_ignoring_top_level_qualifiers_p (to, from)); | |
c11b6f21 MS |
9988 | } |
9989 | } | |
9990 | ||
89d684bb BM |
9991 | /* Returns the type qualifiers for this type, including the qualifiers on the |
9992 | elements for an array type. */ | |
27778b73 MM |
9993 | |
9994 | int | |
ac7d7749 | 9995 | cp_type_quals (const_tree type) |
27778b73 | 9996 | { |
2872152c | 9997 | int quals; |
4e9b57fa | 9998 | /* This CONST_CAST is okay because strip_array_types returns its |
75547801 | 9999 | argument unmodified and we assign it to a const_tree. */ |
2872152c JM |
10000 | type = strip_array_types (CONST_CAST_TREE (type)); |
10001 | if (type == error_mark_node | |
10002 | /* Quals on a FUNCTION_TYPE are memfn quals. */ | |
10003 | || TREE_CODE (type) == FUNCTION_TYPE) | |
328de7c2 | 10004 | return TYPE_UNQUALIFIED; |
2872152c JM |
10005 | quals = TYPE_QUALS (type); |
10006 | /* METHOD and REFERENCE_TYPEs should never have quals. */ | |
10007 | gcc_assert ((TREE_CODE (type) != METHOD_TYPE | |
9f613f06 | 10008 | && !TYPE_REF_P (type)) |
2872152c JM |
10009 | || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) |
10010 | == TYPE_UNQUALIFIED)); | |
10011 | return quals; | |
10012 | } | |
10013 | ||
2eed8e37 BK |
10014 | /* Returns the function-ref-qualifier for TYPE */ |
10015 | ||
10016 | cp_ref_qualifier | |
10017 | type_memfn_rqual (const_tree type) | |
10018 | { | |
10019 | gcc_assert (TREE_CODE (type) == FUNCTION_TYPE | |
10020 | || TREE_CODE (type) == METHOD_TYPE); | |
10021 | ||
10022 | if (!FUNCTION_REF_QUALIFIED (type)) | |
10023 | return REF_QUAL_NONE; | |
10024 | else if (FUNCTION_RVALUE_QUALIFIED (type)) | |
10025 | return REF_QUAL_RVALUE; | |
10026 | else | |
10027 | return REF_QUAL_LVALUE; | |
10028 | } | |
10029 | ||
2872152c JM |
10030 | /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or |
10031 | METHOD_TYPE. */ | |
10032 | ||
10033 | int | |
10034 | type_memfn_quals (const_tree type) | |
10035 | { | |
10036 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
10037 | return TYPE_QUALS (type); | |
10038 | else if (TREE_CODE (type) == METHOD_TYPE) | |
7e1352fe | 10039 | return cp_type_quals (class_of_this_parm (type)); |
2872152c JM |
10040 | else |
10041 | gcc_unreachable (); | |
10042 | } | |
10043 | ||
10044 | /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to | |
2eed8e37 | 10045 | MEMFN_QUALS and its ref-qualifier to RQUAL. */ |
2872152c JM |
10046 | |
10047 | tree | |
2eed8e37 | 10048 | apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual) |
2872152c JM |
10049 | { |
10050 | /* Could handle METHOD_TYPE here if necessary. */ | |
10051 | gcc_assert (TREE_CODE (type) == FUNCTION_TYPE); | |
2eed8e37 BK |
10052 | if (TYPE_QUALS (type) == memfn_quals |
10053 | && type_memfn_rqual (type) == rqual) | |
2872152c | 10054 | return type; |
2eed8e37 | 10055 | |
2872152c JM |
10056 | /* This should really have a different TYPE_MAIN_VARIANT, but that gets |
10057 | complex. */ | |
2eed8e37 | 10058 | tree result = build_qualified_type (type, memfn_quals); |
2eed8e37 | 10059 | return build_ref_qualified_type (result, rqual); |
91063b51 | 10060 | } |
a7a7710d | 10061 | |
36c37128 JM |
10062 | /* Returns nonzero if TYPE is const or volatile. */ |
10063 | ||
10064 | bool | |
10065 | cv_qualified_p (const_tree type) | |
10066 | { | |
10067 | int quals = cp_type_quals (type); | |
10068 | return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0; | |
10069 | } | |
10070 | ||
f4f206f4 | 10071 | /* Returns nonzero if the TYPE contains a mutable member. */ |
a7a7710d | 10072 | |
acd8e2d0 | 10073 | bool |
58f9752a | 10074 | cp_has_mutable_p (const_tree type) |
a7a7710d | 10075 | { |
4e9b57fa | 10076 | /* This CONST_CAST is okay because strip_array_types returns its |
75547801 | 10077 | argument unmodified and we assign it to a const_tree. */ |
b1d5455a | 10078 | type = strip_array_types (CONST_CAST_TREE(type)); |
a7a7710d NS |
10079 | |
10080 | return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type); | |
10081 | } | |
af7b9902 | 10082 | |
15896502 MM |
10083 | /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the |
10084 | TYPE_QUALS. For a VAR_DECL, this may be an optimistic | |
10085 | approximation. In particular, consider: | |
10086 | ||
10087 | int f(); | |
10088 | struct S { int i; }; | |
10089 | const S s = { f(); } | |
10090 | ||
10091 | Here, we will make "s" as TREE_READONLY (because it is declared | |
10092 | "const") -- only to reverse ourselves upon seeing that the | |
10093 | initializer is non-constant. */ | |
10094 | ||
9804209d DG |
10095 | void |
10096 | cp_apply_type_quals_to_decl (int type_quals, tree decl) | |
10097 | { | |
10098 | tree type = TREE_TYPE (decl); | |
10099 | ||
10100 | if (type == error_mark_node) | |
10101 | return; | |
10102 | ||
0d9c0892 JM |
10103 | if (TREE_CODE (decl) == TYPE_DECL) |
10104 | return; | |
10105 | ||
93e1ddcf JM |
10106 | gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE |
10107 | && type_quals != TYPE_UNQUALIFIED)); | |
9804209d | 10108 | |
67935995 | 10109 | /* Avoid setting TREE_READONLY incorrectly. */ |
329af3c7 JM |
10110 | /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr |
10111 | constructor can produce constant init, so rely on cp_finish_decl to | |
10112 | clear TREE_READONLY if the variable has non-constant init. */ | |
859f6cb4 | 10113 | |
6d876e0b JM |
10114 | /* If the type has (or might have) a mutable component, that component |
10115 | might be modified. */ | |
10116 | if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type)) | |
67935995 MM |
10117 | type_quals &= ~TYPE_QUAL_CONST; |
10118 | ||
9804209d DG |
10119 | c_apply_type_quals_to_decl (type_quals, decl); |
10120 | } | |
10121 | ||
af7b9902 | 10122 | /* Subroutine of casts_away_constness. Make T1 and T2 point at |
33c25e5c | 10123 | exemplar types such that casting T1 to T2 is casting away constness |
af7b9902 MM |
10124 | if and only if there is no implicit conversion from T1 to T2. */ |
10125 | ||
10126 | static void | |
b40e334f | 10127 | casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain) |
af7b9902 MM |
10128 | { |
10129 | int quals1; | |
10130 | int quals2; | |
10131 | ||
10132 | /* [expr.const.cast] | |
10133 | ||
10134 | For multi-level pointer to members and multi-level mixed pointers | |
10135 | and pointers to members (conv.qual), the "member" aspect of a | |
10136 | pointer to member level is ignored when determining if a const | |
10137 | cv-qualifier has been cast away. */ | |
af7b9902 MM |
10138 | /* [expr.const.cast] |
10139 | ||
10140 | For two pointer types: | |
10141 | ||
0cbd7506 MS |
10142 | X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type |
10143 | X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type | |
10144 | K is min(N,M) | |
af7b9902 MM |
10145 | |
10146 | casting from X1 to X2 casts away constness if, for a non-pointer | |
10147 | type T there does not exist an implicit conversion (clause | |
10148 | _conv_) from: | |
10149 | ||
0cbd7506 | 10150 | Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N * |
c8094d83 | 10151 | |
af7b9902 MM |
10152 | to |
10153 | ||
0cbd7506 | 10154 | Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */ |
66b1156a PC |
10155 | if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1)) |
10156 | || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2))) | |
af7b9902 MM |
10157 | { |
10158 | *t1 = cp_build_qualified_type (void_type_node, | |
89d684bb | 10159 | cp_type_quals (*t1)); |
af7b9902 | 10160 | *t2 = cp_build_qualified_type (void_type_node, |
89d684bb | 10161 | cp_type_quals (*t2)); |
af7b9902 MM |
10162 | return; |
10163 | } | |
c8094d83 | 10164 | |
89d684bb BM |
10165 | quals1 = cp_type_quals (*t1); |
10166 | quals2 = cp_type_quals (*t2); | |
dad732fa | 10167 | |
66b1156a | 10168 | if (TYPE_PTRDATAMEM_P (*t1)) |
dad732fa MM |
10169 | *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1); |
10170 | else | |
10171 | *t1 = TREE_TYPE (*t1); | |
66b1156a | 10172 | if (TYPE_PTRDATAMEM_P (*t2)) |
dad732fa MM |
10173 | *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2); |
10174 | else | |
10175 | *t2 = TREE_TYPE (*t2); | |
10176 | ||
b40e334f | 10177 | casts_away_constness_r (t1, t2, complain); |
af7b9902 MM |
10178 | *t1 = build_pointer_type (*t1); |
10179 | *t2 = build_pointer_type (*t2); | |
10180 | *t1 = cp_build_qualified_type (*t1, quals1); | |
10181 | *t2 = cp_build_qualified_type (*t2, quals2); | |
10182 | } | |
10183 | ||
838dfd8a | 10184 | /* Returns nonzero if casting from TYPE1 to TYPE2 casts away |
36ef7262 MLI |
10185 | constness. |
10186 | ||
10187 | ??? This function returns non-zero if casting away qualifiers not | |
10188 | just const. We would like to return to the caller exactly which | |
10189 | qualifiers are casted away to give more accurate diagnostics. | |
10190 | */ | |
af7b9902 | 10191 | |
acd8e2d0 | 10192 | static bool |
b40e334f | 10193 | casts_away_constness (tree t1, tree t2, tsubst_flags_t complain) |
af7b9902 | 10194 | { |
9f613f06 | 10195 | if (TYPE_REF_P (t2)) |
af7b9902 MM |
10196 | { |
10197 | /* [expr.const.cast] | |
c8094d83 | 10198 | |
af7b9902 MM |
10199 | Casting from an lvalue of type T1 to an lvalue of type T2 |
10200 | using a reference cast casts away constness if a cast from an | |
10201 | rvalue of type "pointer to T1" to the type "pointer to T2" | |
10202 | casts away constness. */ | |
9f613f06 | 10203 | t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1); |
af7b9902 | 10204 | return casts_away_constness (build_pointer_type (t1), |
b40e334f PC |
10205 | build_pointer_type (TREE_TYPE (t2)), |
10206 | complain); | |
af7b9902 MM |
10207 | } |
10208 | ||
66b1156a | 10209 | if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2)) |
af7b9902 | 10210 | /* [expr.const.cast] |
c8094d83 | 10211 | |
af7b9902 MM |
10212 | Casting from an rvalue of type "pointer to data member of X |
10213 | of type T1" to the type "pointer to data member of Y of type | |
10214 | T2" casts away constness if a cast from an rvalue of type | |
aba649ba | 10215 | "pointer to T1" to the type "pointer to T2" casts away |
af7b9902 | 10216 | constness. */ |
3e0b4710 | 10217 | return casts_away_constness |
a5ac359a | 10218 | (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)), |
b40e334f PC |
10219 | build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)), |
10220 | complain); | |
af7b9902 MM |
10221 | |
10222 | /* Casting away constness is only something that makes sense for | |
10223 | pointer or reference types. */ | |
50e10fa8 | 10224 | if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2)) |
acd8e2d0 | 10225 | return false; |
af7b9902 MM |
10226 | |
10227 | /* Top-level qualifiers don't matter. */ | |
10228 | t1 = TYPE_MAIN_VARIANT (t1); | |
10229 | t2 = TYPE_MAIN_VARIANT (t2); | |
b40e334f PC |
10230 | casts_away_constness_r (&t1, &t2, complain); |
10231 | if (!can_convert (t2, t1, complain)) | |
acd8e2d0 | 10232 | return true; |
af7b9902 | 10233 | |
acd8e2d0 | 10234 | return false; |
af7b9902 | 10235 | } |
6816f040 | 10236 | |
ee76b931 MM |
10237 | /* If T is a REFERENCE_TYPE return the type to which T refers. |
10238 | Otherwise, return T itself. */ | |
10239 | ||
10240 | tree | |
10241 | non_reference (tree t) | |
10242 | { | |
9f613f06 | 10243 | if (t && TYPE_REF_P (t)) |
ee76b931 MM |
10244 | t = TREE_TYPE (t); |
10245 | return t; | |
10246 | } | |
37dc0d8d JM |
10247 | |
10248 | ||
10249 | /* Return nonzero if REF is an lvalue valid for this language; | |
10250 | otherwise, print an error message and return zero. USE says | |
10251 | how the lvalue is being used and so selects the error message. */ | |
10252 | ||
10253 | int | |
4af9e878 | 10254 | lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain) |
37dc0d8d | 10255 | { |
4e9ca9b0 | 10256 | cp_lvalue_kind kind = lvalue_kind (ref); |
37dc0d8d | 10257 | |
4e9ca9b0 JM |
10258 | if (kind == clk_none) |
10259 | { | |
10260 | if (complain & tf_error) | |
7bd11157 | 10261 | lvalue_error (input_location, use); |
4e9ca9b0 JM |
10262 | return 0; |
10263 | } | |
10264 | else if (kind & (clk_rvalueref|clk_class)) | |
10265 | { | |
10266 | if (!(complain & tf_error)) | |
10267 | return 0; | |
955da5e5 JM |
10268 | /* Make this a permerror because we used to accept it. */ |
10269 | permerror (input_location, "using rvalue as lvalue"); | |
4e9ca9b0 JM |
10270 | } |
10271 | return 1; | |
37dc0d8d | 10272 | } |
03a904b5 | 10273 | |
3ce4f9e4 ESR |
10274 | /* Return true if a user-defined literal operator is a raw operator. */ |
10275 | ||
10276 | bool | |
10277 | check_raw_literal_operator (const_tree decl) | |
10278 | { | |
10279 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
10280 | tree argtype; | |
10281 | int arity; | |
10282 | bool maybe_raw_p = false; | |
10283 | ||
10284 | /* Count the number and type of arguments and check for ellipsis. */ | |
10285 | for (argtype = argtypes, arity = 0; | |
10286 | argtype && argtype != void_list_node; | |
10287 | ++arity, argtype = TREE_CHAIN (argtype)) | |
10288 | { | |
10289 | tree t = TREE_VALUE (argtype); | |
10290 | ||
10291 | if (same_type_p (t, const_string_type_node)) | |
10292 | maybe_raw_p = true; | |
10293 | } | |
10294 | if (!argtype) | |
10295 | return false; /* Found ellipsis. */ | |
10296 | ||
10297 | if (!maybe_raw_p || arity != 1) | |
10298 | return false; | |
10299 | ||
10300 | return true; | |
10301 | } | |
10302 | ||
10303 | ||
10304 | /* Return true if a user-defined literal operator has one of the allowed | |
10305 | argument types. */ | |
10306 | ||
10307 | bool | |
10308 | check_literal_operator_args (const_tree decl, | |
10309 | bool *long_long_unsigned_p, bool *long_double_p) | |
10310 | { | |
10311 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
6e887223 JJ |
10312 | |
10313 | *long_long_unsigned_p = false; | |
10314 | *long_double_p = false; | |
d2027d2a ESR |
10315 | if (processing_template_decl || processing_specialization) |
10316 | return argtypes == void_list_node; | |
3ce4f9e4 ESR |
10317 | else |
10318 | { | |
10319 | tree argtype; | |
10320 | int arity; | |
10321 | int max_arity = 2; | |
3ce4f9e4 | 10322 | |
3ce4f9e4 ESR |
10323 | /* Count the number and type of arguments and check for ellipsis. */ |
10324 | for (argtype = argtypes, arity = 0; | |
10325 | argtype && argtype != void_list_node; | |
10326 | argtype = TREE_CHAIN (argtype)) | |
10327 | { | |
10328 | tree t = TREE_VALUE (argtype); | |
10329 | ++arity; | |
10330 | ||
50e10fa8 | 10331 | if (TYPE_PTR_P (t)) |
3ce4f9e4 | 10332 | { |
f05eec4a | 10333 | bool maybe_raw_p = false; |
24847495 JM |
10334 | t = TREE_TYPE (t); |
10335 | if (cp_type_quals (t) != TYPE_QUAL_CONST) | |
10336 | return false; | |
10337 | t = TYPE_MAIN_VARIANT (t); | |
f05eec4a ESR |
10338 | if ((maybe_raw_p = same_type_p (t, char_type_node)) |
10339 | || same_type_p (t, wchar_type_node) | |
2d91f79d | 10340 | || same_type_p (t, char8_type_node) |
f05eec4a ESR |
10341 | || same_type_p (t, char16_type_node) |
10342 | || same_type_p (t, char32_type_node)) | |
24847495 | 10343 | { |
f05eec4a ESR |
10344 | argtype = TREE_CHAIN (argtype); |
10345 | if (!argtype) | |
10346 | return false; | |
10347 | t = TREE_VALUE (argtype); | |
10348 | if (maybe_raw_p && argtype == void_list_node) | |
10349 | return true; | |
10350 | else if (same_type_p (t, size_type_node)) | |
10351 | { | |
10352 | ++arity; | |
10353 | continue; | |
10354 | } | |
10355 | else | |
10356 | return false; | |
24847495 | 10357 | } |
3ce4f9e4 ESR |
10358 | } |
10359 | else if (same_type_p (t, long_long_unsigned_type_node)) | |
10360 | { | |
10361 | max_arity = 1; | |
10362 | *long_long_unsigned_p = true; | |
10363 | } | |
10364 | else if (same_type_p (t, long_double_type_node)) | |
10365 | { | |
10366 | max_arity = 1; | |
10367 | *long_double_p = true; | |
10368 | } | |
10369 | else if (same_type_p (t, char_type_node)) | |
10370 | max_arity = 1; | |
10371 | else if (same_type_p (t, wchar_type_node)) | |
10372 | max_arity = 1; | |
2d91f79d TH |
10373 | else if (same_type_p (t, char8_type_node)) |
10374 | max_arity = 1; | |
3ce4f9e4 ESR |
10375 | else if (same_type_p (t, char16_type_node)) |
10376 | max_arity = 1; | |
10377 | else if (same_type_p (t, char32_type_node)) | |
10378 | max_arity = 1; | |
10379 | else | |
10380 | return false; | |
10381 | } | |
10382 | if (!argtype) | |
10383 | return false; /* Found ellipsis. */ | |
10384 | ||
d2027d2a | 10385 | if (arity != max_arity) |
3ce4f9e4 ESR |
10386 | return false; |
10387 | ||
3ce4f9e4 ESR |
10388 | return true; |
10389 | } | |
10390 | } | |
1807ffc1 MS |
10391 | |
10392 | /* Always returns false since unlike C90, C++ has no concept of implicit | |
10393 | function declarations. */ | |
10394 | ||
10395 | bool | |
10396 | c_decl_implicit (const_tree) | |
10397 | { | |
10398 | return false; | |
10399 | } |