]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Build expressions with type checking for C++ compiler. |
818ab71a | 2 | Copyright (C) 1987-2016 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" |
8d08fdba | 40 | |
882de214 KT |
41 | static tree cp_build_addr_expr_strict (tree, tsubst_flags_t); |
42 | static tree cp_build_function_call (tree, tree, tsubst_flags_t); | |
b5791fdc | 43 | static tree pfn_from_ptrmemfunc (tree); |
a298680c | 44 | static tree delta_from_ptrmemfunc (tree); |
2f5b91f5 | 45 | static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int, |
e57d93c6 | 46 | tsubst_flags_t, int); |
4f2e1536 MP |
47 | static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree, |
48 | tsubst_flags_t); | |
5ade1ed2 DG |
49 | static tree rationalize_conditional_expr (enum tree_code, tree, |
50 | tsubst_flags_t); | |
acd8e2d0 | 51 | static int comp_ptr_ttypes_real (tree, tree, int); |
acd8e2d0 | 52 | static bool comp_except_types (tree, tree, bool); |
58f9752a | 53 | static bool comp_array_types (const_tree, const_tree, bool); |
4f2e1536 | 54 | static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t); |
135d47df | 55 | static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t); |
b40e334f PC |
56 | static void casts_away_constness_r (tree *, tree *, tsubst_flags_t); |
57 | static bool casts_away_constness (tree, tree, tsubst_flags_t); | |
b4dfdc11 | 58 | static bool maybe_warn_about_returning_address_of_local (tree); |
76545796 | 59 | static tree lookup_destructor (tree, tree, tree, tsubst_flags_t); |
09bae928 | 60 | static void error_args_num (location_t, tree, bool); |
9771b263 | 61 | static int convert_arguments (tree, vec<tree, va_gc> **, tree, int, |
5ade1ed2 | 62 | tsubst_flags_t); |
8d08fdba | 63 | |
8d08fdba | 64 | /* Do `exp = require_complete_type (exp);' to make sure exp |
ae58fa02 | 65 | does not have an incomplete type. (That includes void types.) |
79fe346e | 66 | Returns error_mark_node if the VALUE does not have |
ae58fa02 | 67 | complete type when this function returns. */ |
8d08fdba MS |
68 | |
69 | tree | |
79fe346e | 70 | require_complete_type_sfinae (tree value, tsubst_flags_t complain) |
8d08fdba | 71 | { |
5566b478 MS |
72 | tree type; |
73 | ||
66543169 | 74 | if (processing_template_decl || value == error_mark_node) |
5566b478 MS |
75 | return value; |
76 | ||
2c73f9f5 ML |
77 | if (TREE_CODE (value) == OVERLOAD) |
78 | type = unknown_type_node; | |
79 | else | |
80 | type = TREE_TYPE (value); | |
8d08fdba | 81 | |
ae5cbc33 RS |
82 | if (type == error_mark_node) |
83 | return error_mark_node; | |
84 | ||
8d08fdba | 85 | /* First, detect a valid value with a complete type. */ |
d0f062fb | 86 | if (COMPLETE_TYPE_P (type)) |
8d08fdba MS |
87 | return value; |
88 | ||
79fe346e | 89 | if (complete_type_or_maybe_complain (type, value, complain)) |
8f259df3 MM |
90 | return value; |
91 | else | |
ae58fa02 | 92 | return error_mark_node; |
8d08fdba MS |
93 | } |
94 | ||
79fe346e JM |
95 | tree |
96 | require_complete_type (tree value) | |
97 | { | |
98 | return require_complete_type_sfinae (value, tf_warning_or_error); | |
99 | } | |
100 | ||
8f259df3 MM |
101 | /* Try to complete TYPE, if it is incomplete. For example, if TYPE is |
102 | a template instantiation, do the instantiation. Returns TYPE, | |
103 | whether or not it could be completed, unless something goes | |
104 | horribly wrong, in which case the error_mark_node is returned. */ | |
105 | ||
5566b478 | 106 | tree |
acd8e2d0 | 107 | complete_type (tree type) |
5566b478 | 108 | { |
8857f91e MM |
109 | if (type == NULL_TREE) |
110 | /* Rather than crash, we return something sure to cause an error | |
111 | at some point. */ | |
112 | return error_mark_node; | |
113 | ||
d0f062fb | 114 | if (type == error_mark_node || COMPLETE_TYPE_P (type)) |
5566b478 | 115 | ; |
f65e97fd | 116 | else if (TREE_CODE (type) == ARRAY_TYPE) |
5566b478 MS |
117 | { |
118 | tree t = complete_type (TREE_TYPE (type)); | |
73bebd55 | 119 | unsigned int needs_constructing, has_nontrivial_dtor; |
1e2e9f54 | 120 | if (COMPLETE_TYPE_P (t) && !dependent_type_p (type)) |
6467930b | 121 | layout_type (type); |
73bebd55 | 122 | needs_constructing |
c73964b2 | 123 | = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t)); |
73bebd55 | 124 | has_nontrivial_dtor |
834c6dff | 125 | = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t)); |
73bebd55 JJ |
126 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
127 | { | |
128 | TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing; | |
129 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor; | |
130 | } | |
5566b478 | 131 | } |
7ddedda4 | 132 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type)) |
e76a2646 | 133 | instantiate_class_template (TYPE_MAIN_VARIANT (type)); |
5566b478 MS |
134 | |
135 | return type; | |
136 | } | |
137 | ||
5aa3396c | 138 | /* Like complete_type, but issue an error if the TYPE cannot be completed. |
be20e673 | 139 | VALUE is used for informative diagnostics. |
66543169 | 140 | Returns NULL_TREE if the type cannot be made complete. */ |
8f259df3 MM |
141 | |
142 | tree | |
309714d4 | 143 | complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain) |
8f259df3 MM |
144 | { |
145 | type = complete_type (type); | |
ae58fa02 MM |
146 | if (type == error_mark_node) |
147 | /* We already issued an error. */ | |
148 | return NULL_TREE; | |
d0f062fb | 149 | else if (!COMPLETE_TYPE_P (type)) |
8f259df3 | 150 | { |
309714d4 JM |
151 | if (complain & tf_error) |
152 | cxx_incomplete_type_diagnostic (value, type, DK_ERROR); | |
8f259df3 MM |
153 | return NULL_TREE; |
154 | } | |
155 | else | |
156 | return type; | |
157 | } | |
158 | ||
309714d4 JM |
159 | tree |
160 | complete_type_or_else (tree type, tree value) | |
161 | { | |
162 | return complete_type_or_maybe_complain (type, value, tf_warning_or_error); | |
163 | } | |
164 | ||
8d08fdba | 165 | /* Return truthvalue of whether type of EXP is instantiated. */ |
e92cc029 | 166 | |
8d08fdba | 167 | int |
58f9752a | 168 | type_unknown_p (const_tree exp) |
8d08fdba | 169 | { |
26bcf8fc | 170 | return (TREE_CODE (exp) == TREE_LIST |
a5ac359a | 171 | || TREE_TYPE (exp) == unknown_type_node); |
8d08fdba MS |
172 | } |
173 | ||
8d08fdba MS |
174 | \f |
175 | /* Return the common type of two parameter lists. | |
96d84882 | 176 | We assume that comptypes has already been done and returned 1; |
8d08fdba MS |
177 | if that isn't so, this may crash. |
178 | ||
179 | As an optimization, free the space we allocate if the parameter | |
180 | lists are already common. */ | |
181 | ||
10b2bcdd | 182 | static tree |
5671bf27 | 183 | commonparms (tree p1, tree p2) |
8d08fdba MS |
184 | { |
185 | tree oldargs = p1, newargs, n; | |
186 | int i, len; | |
187 | int any_change = 0; | |
8d08fdba MS |
188 | |
189 | len = list_length (p1); | |
190 | newargs = tree_last (p1); | |
191 | ||
192 | if (newargs == void_list_node) | |
193 | i = 1; | |
194 | else | |
195 | { | |
196 | i = 0; | |
197 | newargs = 0; | |
198 | } | |
199 | ||
200 | for (; i < len; i++) | |
201 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); | |
202 | ||
203 | n = newargs; | |
204 | ||
205 | for (i = 0; p1; | |
206 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++) | |
207 | { | |
208 | if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2)) | |
209 | { | |
8d08fdba MS |
210 | TREE_PURPOSE (n) = TREE_PURPOSE (p1); |
211 | any_change = 1; | |
212 | } | |
213 | else if (! TREE_PURPOSE (p1)) | |
214 | { | |
215 | if (TREE_PURPOSE (p2)) | |
216 | { | |
217 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); | |
218 | any_change = 1; | |
219 | } | |
220 | } | |
221 | else | |
222 | { | |
1743ca29 | 223 | if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2))) |
a4443a08 | 224 | any_change = 1; |
8d08fdba MS |
225 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); |
226 | } | |
227 | if (TREE_VALUE (p1) != TREE_VALUE (p2)) | |
228 | { | |
229 | any_change = 1; | |
6da794e8 | 230 | TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2)); |
8d08fdba MS |
231 | } |
232 | else | |
233 | TREE_VALUE (n) = TREE_VALUE (p1); | |
234 | } | |
235 | if (! any_change) | |
970d6386 | 236 | return oldargs; |
8d08fdba MS |
237 | |
238 | return newargs; | |
239 | } | |
240 | ||
f2ee215b BK |
241 | /* Given a type, perhaps copied for a typedef, |
242 | find the "original" version of it. */ | |
993acaec | 243 | static tree |
5671bf27 | 244 | original_type (tree t) |
f2ee215b | 245 | { |
8e30dcf3 | 246 | int quals = cp_type_quals (t); |
2b643eda MM |
247 | while (t != error_mark_node |
248 | && TYPE_NAME (t) != NULL_TREE) | |
f2ee215b BK |
249 | { |
250 | tree x = TYPE_NAME (t); | |
251 | if (TREE_CODE (x) != TYPE_DECL) | |
252 | break; | |
253 | x = DECL_ORIGINAL_TYPE (x); | |
254 | if (x == NULL_TREE) | |
255 | break; | |
256 | t = x; | |
257 | } | |
8e30dcf3 | 258 | return cp_build_qualified_type (t, quals); |
f2ee215b BK |
259 | } |
260 | ||
98f2f3a2 MLI |
261 | /* Return the common type for two arithmetic types T1 and T2 under the |
262 | usual arithmetic conversions. The default conversions have already | |
263 | been applied, and enumerated types converted to their compatible | |
264 | integer types. */ | |
a7a64a77 | 265 | |
98f2f3a2 MLI |
266 | static tree |
267 | cp_common_type (tree t1, tree t2) | |
a7a64a77 MM |
268 | { |
269 | enum tree_code code1 = TREE_CODE (t1); | |
270 | enum tree_code code2 = TREE_CODE (t2); | |
271 | tree attributes; | |
78a7c317 | 272 | int i; |
a7a64a77 | 273 | |
c5ee1358 | 274 | |
550a799d JM |
275 | /* In what follows, we slightly generalize the rules given in [expr] so |
276 | as to deal with `long long' and `complex'. First, merge the | |
277 | attributes. */ | |
278 | attributes = (*targetm.merge_type_attributes) (t1, t2); | |
279 | ||
280 | if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2)) | |
281 | { | |
282 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
283 | return build_type_attribute_variant (t1, attributes); | |
284 | else | |
285 | return NULL_TREE; | |
286 | } | |
287 | ||
a7a64a77 | 288 | /* FIXME: Attributes. */ |
c8094d83 | 289 | gcc_assert (ARITHMETIC_TYPE_P (t1) |
b55b02ea | 290 | || VECTOR_TYPE_P (t1) |
adf2edec | 291 | || UNSCOPED_ENUM_P (t1)); |
c8094d83 | 292 | gcc_assert (ARITHMETIC_TYPE_P (t2) |
b55b02ea | 293 | || VECTOR_TYPE_P (t2) |
adf2edec | 294 | || UNSCOPED_ENUM_P (t2)); |
a7a64a77 | 295 | |
6da794e8 JM |
296 | /* If one type is complex, form the common type of the non-complex |
297 | components, then make that complex. Use T1 or T2 if it is the | |
298 | required type. */ | |
299 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
300 | { | |
301 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
302 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
303 | tree subtype | |
304 | = type_after_usual_arithmetic_conversions (subtype1, subtype2); | |
305 | ||
306 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
307 | return build_type_attribute_variant (t1, attributes); | |
308 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
309 | return build_type_attribute_variant (t2, attributes); | |
310 | else | |
311 | return build_type_attribute_variant (build_complex_type (subtype), | |
312 | attributes); | |
313 | } | |
314 | ||
73042643 AH |
315 | if (code1 == VECTOR_TYPE) |
316 | { | |
317 | /* When we get here we should have two vectors of the same size. | |
318 | Just prefer the unsigned one if present. */ | |
319 | if (TYPE_UNSIGNED (t1)) | |
320 | return build_type_attribute_variant (t1, attributes); | |
321 | else | |
322 | return build_type_attribute_variant (t2, attributes); | |
323 | } | |
324 | ||
a7a64a77 MM |
325 | /* If only one is real, use it as the result. */ |
326 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
327 | return build_type_attribute_variant (t1, attributes); | |
328 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
329 | return build_type_attribute_variant (t2, attributes); | |
330 | ||
a7a64a77 MM |
331 | /* Both real or both integers; use the one with greater precision. */ |
332 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
333 | return build_type_attribute_variant (t1, attributes); | |
334 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
335 | return build_type_attribute_variant (t2, attributes); | |
336 | ||
2f4d058f AH |
337 | /* The types are the same; no need to do anything fancy. */ |
338 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
339 | return build_type_attribute_variant (t1, attributes); | |
340 | ||
a7a64a77 MM |
341 | if (code1 != REAL_TYPE) |
342 | { | |
343 | /* If one is unsigned long long, then convert the other to unsigned | |
344 | long long. */ | |
345 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node) | |
346 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node)) | |
347 | return build_type_attribute_variant (long_long_unsigned_type_node, | |
348 | attributes); | |
349 | /* If one is a long long, and the other is an unsigned long, and | |
350 | long long can represent all the values of an unsigned long, then | |
351 | convert to a long long. Otherwise, convert to an unsigned long | |
352 | long. Otherwise, if either operand is long long, convert the | |
353 | other to long long. | |
c8094d83 | 354 | |
a7a64a77 MM |
355 | Since we're here, we know the TYPE_PRECISION is the same; |
356 | therefore converting to long long cannot represent all the values | |
357 | of an unsigned long, so we choose unsigned long long in that | |
358 | case. */ | |
359 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node) | |
360 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node)) | |
361 | { | |
8df83eae | 362 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
c8094d83 | 363 | ? long_long_unsigned_type_node |
a7a64a77 MM |
364 | : long_long_integer_type_node); |
365 | return build_type_attribute_variant (t, attributes); | |
366 | } | |
c8094d83 | 367 | |
a7a64a77 MM |
368 | /* Go through the same procedure, but for longs. */ |
369 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node) | |
370 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node)) | |
371 | return build_type_attribute_variant (long_unsigned_type_node, | |
372 | attributes); | |
373 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node) | |
374 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node)) | |
375 | { | |
8df83eae | 376 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
a7a64a77 MM |
377 | ? long_unsigned_type_node : long_integer_type_node); |
378 | return build_type_attribute_variant (t, attributes); | |
379 | } | |
9edc6e4c DD |
380 | |
381 | /* For __intN types, either the type is __int128 (and is lower | |
382 | priority than the types checked above, but higher than other | |
383 | 128-bit types) or it's known to not be the same size as other | |
384 | types (enforced in toplev.c). Prefer the unsigned type. */ | |
385 | for (i = 0; i < NUM_INT_N_ENTS; i ++) | |
386 | { | |
387 | if (int_n_enabled_p [i] | |
388 | && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type) | |
389 | || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type) | |
390 | || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type) | |
391 | || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type))) | |
392 | { | |
393 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
394 | ? int_n_trees[i].unsigned_type | |
395 | : int_n_trees[i].signed_type); | |
396 | return build_type_attribute_variant (t, attributes); | |
397 | } | |
398 | } | |
399 | ||
a7a64a77 | 400 | /* Otherwise prefer the unsigned one. */ |
8df83eae | 401 | if (TYPE_UNSIGNED (t1)) |
a7a64a77 MM |
402 | return build_type_attribute_variant (t1, attributes); |
403 | else | |
404 | return build_type_attribute_variant (t2, attributes); | |
405 | } | |
406 | else | |
407 | { | |
408 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node) | |
409 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node)) | |
410 | return build_type_attribute_variant (long_double_type_node, | |
411 | attributes); | |
412 | if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node) | |
413 | || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node)) | |
414 | return build_type_attribute_variant (double_type_node, | |
415 | attributes); | |
2f4d058f AH |
416 | if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node) |
417 | || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node)) | |
a7a64a77 MM |
418 | return build_type_attribute_variant (float_type_node, |
419 | attributes); | |
c8094d83 | 420 | |
2f4d058f | 421 | /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of |
0cbd7506 MS |
422 | the standard C++ floating-point types. Logic earlier in this |
423 | function has already eliminated the possibility that | |
424 | TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no | |
425 | compelling reason to choose one or the other. */ | |
2f4d058f | 426 | return build_type_attribute_variant (t1, attributes); |
a7a64a77 MM |
427 | } |
428 | } | |
429 | ||
98f2f3a2 MLI |
430 | /* T1 and T2 are arithmetic or enumeration types. Return the type |
431 | that will result from the "usual arithmetic conversions" on T1 and | |
432 | T2 as described in [expr]. */ | |
433 | ||
434 | tree | |
435 | type_after_usual_arithmetic_conversions (tree t1, tree t2) | |
436 | { | |
437 | gcc_assert (ARITHMETIC_TYPE_P (t1) | |
b55b02ea | 438 | || VECTOR_TYPE_P (t1) |
98f2f3a2 MLI |
439 | || UNSCOPED_ENUM_P (t1)); |
440 | gcc_assert (ARITHMETIC_TYPE_P (t2) | |
b55b02ea | 441 | || VECTOR_TYPE_P (t2) |
98f2f3a2 MLI |
442 | || UNSCOPED_ENUM_P (t2)); |
443 | ||
444 | /* Perform the integral promotions. We do not promote real types here. */ | |
445 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1) | |
6f9b088b | 446 | && INTEGRAL_OR_ENUMERATION_TYPE_P (t2)) |
98f2f3a2 MLI |
447 | { |
448 | t1 = type_promotes_to (t1); | |
449 | t2 = type_promotes_to (t2); | |
450 | } | |
451 | ||
452 | return cp_common_type (t1, t2); | |
453 | } | |
454 | ||
083586b8 NF |
455 | static void |
456 | composite_pointer_error (diagnostic_t kind, tree t1, tree t2, | |
457 | composite_pointer_operation operation) | |
458 | { | |
459 | switch (operation) | |
460 | { | |
461 | case CPO_COMPARISON: | |
462 | emit_diagnostic (kind, input_location, 0, | |
463 | "comparison between " | |
464 | "distinct pointer types %qT and %qT lacks a cast", | |
465 | t1, t2); | |
466 | break; | |
467 | case CPO_CONVERSION: | |
468 | emit_diagnostic (kind, input_location, 0, | |
469 | "conversion between " | |
470 | "distinct pointer types %qT and %qT lacks a cast", | |
471 | t1, t2); | |
472 | break; | |
473 | case CPO_CONDITIONAL_EXPR: | |
474 | emit_diagnostic (kind, input_location, 0, | |
475 | "conditional expression between " | |
476 | "distinct pointer types %qT and %qT lacks a cast", | |
477 | t1, t2); | |
478 | break; | |
479 | default: | |
480 | gcc_unreachable (); | |
481 | } | |
482 | } | |
483 | ||
a5ac359a | 484 | /* Subroutine of composite_pointer_type to implement the recursive |
c86818cf | 485 | case. See that function for documentation of the parameters. */ |
a5ac359a MM |
486 | |
487 | static tree | |
c86818cf SZ |
488 | composite_pointer_type_r (tree t1, tree t2, |
489 | composite_pointer_operation operation, | |
5ade1ed2 | 490 | tsubst_flags_t complain) |
a5ac359a MM |
491 | { |
492 | tree pointee1; | |
493 | tree pointee2; | |
494 | tree result_type; | |
495 | tree attributes; | |
496 | ||
497 | /* Determine the types pointed to by T1 and T2. */ | |
50e10fa8 | 498 | if (TYPE_PTR_P (t1)) |
a5ac359a MM |
499 | { |
500 | pointee1 = TREE_TYPE (t1); | |
501 | pointee2 = TREE_TYPE (t2); | |
502 | } | |
503 | else | |
504 | { | |
505 | pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1); | |
506 | pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2); | |
507 | } | |
508 | ||
509 | /* [expr.rel] | |
510 | ||
511 | Otherwise, the composite pointer type is a pointer type | |
512 | similar (_conv.qual_) to the type of one of the operands, | |
513 | with a cv-qualification signature (_conv.qual_) that is the | |
514 | union of the cv-qualification signatures of the operand | |
515 | types. */ | |
516 | if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2)) | |
517 | result_type = pointee1; | |
50e10fa8 | 518 | else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2)) |
66b1156a | 519 | || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2))) |
df9ee5c8 PC |
520 | { |
521 | result_type = composite_pointer_type_r (pointee1, pointee2, operation, | |
522 | complain); | |
523 | if (result_type == error_mark_node) | |
524 | return error_mark_node; | |
525 | } | |
a5ac359a MM |
526 | else |
527 | { | |
5ade1ed2 | 528 | if (complain & tf_error) |
083586b8 | 529 | composite_pointer_error (DK_PERMERROR, t1, t2, operation); |
7a1db261 JM |
530 | else |
531 | return error_mark_node; | |
a5ac359a MM |
532 | result_type = void_type_node; |
533 | } | |
534 | result_type = cp_build_qualified_type (result_type, | |
535 | (cp_type_quals (pointee1) | |
536 | | cp_type_quals (pointee2))); | |
a5ac359a MM |
537 | /* If the original types were pointers to members, so is the |
538 | result. */ | |
66b1156a | 539 | if (TYPE_PTRMEM_P (t1)) |
a5ac359a MM |
540 | { |
541 | if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), | |
7a1db261 JM |
542 | TYPE_PTRMEM_CLASS_TYPE (t2))) |
543 | { | |
544 | if (complain & tf_error) | |
545 | composite_pointer_error (DK_PERMERROR, t1, t2, operation); | |
546 | else | |
547 | return error_mark_node; | |
548 | } | |
a5ac359a MM |
549 | result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), |
550 | result_type); | |
551 | } | |
9d363a56 MM |
552 | else |
553 | result_type = build_pointer_type (result_type); | |
a5ac359a MM |
554 | |
555 | /* Merge the attributes. */ | |
556 | attributes = (*targetm.merge_type_attributes) (t1, t2); | |
557 | return build_type_attribute_variant (result_type, attributes); | |
558 | } | |
559 | ||
a7a64a77 | 560 | /* Return the composite pointer type (see [expr.rel]) for T1 and T2. |
c86818cf SZ |
561 | ARG1 and ARG2 are the values with those types. The OPERATION is to |
562 | describe the operation between the pointer types, | |
563 | in case an error occurs. | |
a5ac359a MM |
564 | |
565 | This routine also implements the computation of a common type for | |
566 | pointers-to-members as per [expr.eq]. */ | |
a7a64a77 | 567 | |
c8094d83 | 568 | tree |
5671bf27 | 569 | composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2, |
c86818cf SZ |
570 | composite_pointer_operation operation, |
571 | tsubst_flags_t complain) | |
a7a64a77 | 572 | { |
a5ac359a MM |
573 | tree class1; |
574 | tree class2; | |
a7a64a77 MM |
575 | |
576 | /* [expr.rel] | |
577 | ||
578 | If one operand is a null pointer constant, the composite pointer | |
579 | type is the type of the other operand. */ | |
580 | if (null_ptr_cst_p (arg1)) | |
581 | return t2; | |
582 | if (null_ptr_cst_p (arg2)) | |
583 | return t1; | |
c8094d83 | 584 | |
634790f4 MM |
585 | /* We have: |
586 | ||
587 | [expr.rel] | |
588 | ||
589 | If one of the operands has type "pointer to cv1 void*", then | |
590 | the other has type "pointer to cv2T", and the composite pointer | |
591 | type is "pointer to cv12 void", where cv12 is the union of cv1 | |
592 | and cv2. | |
593 | ||
594 | If either type is a pointer to void, make sure it is T1. */ | |
50e10fa8 | 595 | if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2))) |
fab27f52 | 596 | std::swap (t1, t2); |
a5ac359a | 597 | |
634790f4 | 598 | /* Now, if T1 is a pointer to void, merge the qualifiers. */ |
50e10fa8 | 599 | if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1))) |
a7a64a77 | 600 | { |
a5ac359a MM |
601 | tree attributes; |
602 | tree result_type; | |
603 | ||
aa0a659c PC |
604 | if (TYPE_PTRFN_P (t2)) |
605 | { | |
606 | if (complain & tf_error) | |
607 | { | |
608 | switch (operation) | |
609 | { | |
610 | case CPO_COMPARISON: | |
611 | pedwarn (input_location, OPT_Wpedantic, | |
612 | "ISO C++ forbids comparison between pointer " | |
613 | "of type %<void *%> and pointer-to-function"); | |
614 | break; | |
615 | case CPO_CONVERSION: | |
616 | pedwarn (input_location, OPT_Wpedantic, | |
617 | "ISO C++ forbids conversion between pointer " | |
618 | "of type %<void *%> and pointer-to-function"); | |
619 | break; | |
620 | case CPO_CONDITIONAL_EXPR: | |
621 | pedwarn (input_location, OPT_Wpedantic, | |
622 | "ISO C++ forbids conditional expression between " | |
623 | "pointer of type %<void *%> and " | |
624 | "pointer-to-function"); | |
625 | break; | |
626 | default: | |
627 | gcc_unreachable (); | |
628 | } | |
629 | } | |
630 | else | |
631 | return error_mark_node; | |
c86818cf | 632 | } |
c8094d83 | 633 | result_type |
a5ac359a MM |
634 | = cp_build_qualified_type (void_type_node, |
635 | (cp_type_quals (TREE_TYPE (t1)) | |
636 | | cp_type_quals (TREE_TYPE (t2)))); | |
634790f4 | 637 | result_type = build_pointer_type (result_type); |
a5ac359a MM |
638 | /* Merge the attributes. */ |
639 | attributes = (*targetm.merge_type_attributes) (t1, t2); | |
640 | return build_type_attribute_variant (result_type, attributes); | |
641 | } | |
642 | ||
50e10fa8 PC |
643 | if (c_dialect_objc () && TYPE_PTR_P (t1) |
644 | && TYPE_PTR_P (t2)) | |
660845bf | 645 | { |
b581b85b NP |
646 | if (objc_have_common_type (t1, t2, -3, NULL_TREE)) |
647 | return objc_common_type (t1, t2); | |
660845bf ZL |
648 | } |
649 | ||
a5ac359a MM |
650 | /* [expr.eq] permits the application of a pointer conversion to |
651 | bring the pointers to a common type. */ | |
50e10fa8 | 652 | if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2) |
a5ac359a MM |
653 | && CLASS_TYPE_P (TREE_TYPE (t1)) |
654 | && CLASS_TYPE_P (TREE_TYPE (t2)) | |
655 | && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1), | |
656 | TREE_TYPE (t2))) | |
657 | { | |
658 | class1 = TREE_TYPE (t1); | |
659 | class2 = TREE_TYPE (t2); | |
660 | ||
660845bf | 661 | if (DERIVED_FROM_P (class1, class2)) |
c8094d83 | 662 | t2 = (build_pointer_type |
a3360e77 | 663 | (cp_build_qualified_type (class1, cp_type_quals (class2)))); |
660845bf | 664 | else if (DERIVED_FROM_P (class2, class1)) |
c8094d83 | 665 | t1 = (build_pointer_type |
a3360e77 | 666 | (cp_build_qualified_type (class2, cp_type_quals (class1)))); |
a5ac359a | 667 | else |
c86818cf SZ |
668 | { |
669 | if (complain & tf_error) | |
083586b8 | 670 | composite_pointer_error (DK_ERROR, t1, t2, operation); |
c86818cf SZ |
671 | return error_mark_node; |
672 | } | |
a7a64a77 | 673 | } |
a5ac359a MM |
674 | /* [expr.eq] permits the application of a pointer-to-member |
675 | conversion to change the class type of one of the types. */ | |
66b1156a | 676 | else if (TYPE_PTRMEM_P (t1) |
c86818cf | 677 | && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), |
a5ac359a | 678 | TYPE_PTRMEM_CLASS_TYPE (t2))) |
708cae97 | 679 | { |
a5ac359a MM |
680 | class1 = TYPE_PTRMEM_CLASS_TYPE (t1); |
681 | class2 = TYPE_PTRMEM_CLASS_TYPE (t2); | |
708cae97 | 682 | |
a5ac359a MM |
683 | if (DERIVED_FROM_P (class1, class2)) |
684 | t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1)); | |
685 | else if (DERIVED_FROM_P (class2, class1)) | |
686 | t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2)); | |
a7a64a77 | 687 | else |
c86818cf SZ |
688 | { |
689 | if (complain & tf_error) | |
690 | switch (operation) | |
691 | { | |
692 | case CPO_COMPARISON: | |
693 | error ("comparison between distinct " | |
694 | "pointer-to-member types %qT and %qT lacks a cast", | |
695 | t1, t2); | |
696 | break; | |
697 | case CPO_CONVERSION: | |
698 | error ("conversion between distinct " | |
699 | "pointer-to-member types %qT and %qT lacks a cast", | |
700 | t1, t2); | |
701 | break; | |
702 | case CPO_CONDITIONAL_EXPR: | |
703 | error ("conditional expression between distinct " | |
704 | "pointer-to-member types %qT and %qT lacks a cast", | |
705 | t1, t2); | |
706 | break; | |
707 | default: | |
708 | gcc_unreachable (); | |
709 | } | |
710 | return error_mark_node; | |
711 | } | |
712 | } | |
b8fd7909 JM |
713 | else if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2) |
714 | && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t1)) | |
715 | && TREE_CODE (TREE_TYPE (t2)) == TREE_CODE (TREE_TYPE (t1))) | |
716 | { | |
717 | /* ...if T1 is "pointer to transaction_safe function" and T2 is "pointer | |
718 | to function", where the function types are otherwise the same, T2, and | |
719 | vice versa.... */ | |
720 | tree f1 = TREE_TYPE (t1); | |
721 | tree f2 = TREE_TYPE (t2); | |
722 | bool safe1 = tx_safe_fn_type_p (f1); | |
723 | bool safe2 = tx_safe_fn_type_p (f2); | |
724 | if (safe1 && !safe2) | |
725 | t1 = build_pointer_type (tx_unsafe_fn_variant (f1)); | |
726 | else if (safe2 && !safe1) | |
727 | t2 = build_pointer_type (tx_unsafe_fn_variant (f2)); | |
728 | } | |
c86818cf SZ |
729 | |
730 | return composite_pointer_type_r (t1, t2, operation, complain); | |
a7a64a77 MM |
731 | } |
732 | ||
6da794e8 | 733 | /* Return the merged type of two types. |
96d84882 | 734 | We assume that comptypes has already been done and returned 1; |
8d08fdba MS |
735 | if that isn't so, this may crash. |
736 | ||
6da794e8 JM |
737 | This just combines attributes and default arguments; any other |
738 | differences would cause the two types to compare unalike. */ | |
8d08fdba MS |
739 | |
740 | tree | |
5671bf27 | 741 | merge_types (tree t1, tree t2) |
8d08fdba | 742 | { |
926ce8bd KH |
743 | enum tree_code code1; |
744 | enum tree_code code2; | |
2986ae00 | 745 | tree attributes; |
8d08fdba MS |
746 | |
747 | /* Save time if the two types are the same. */ | |
f2ee215b | 748 | if (t1 == t2) |
9076e292 | 749 | return t1; |
6da794e8 | 750 | if (original_type (t1) == original_type (t2)) |
9076e292 | 751 | return t1; |
8d08fdba MS |
752 | |
753 | /* If one type is nonsense, use the other. */ | |
754 | if (t1 == error_mark_node) | |
9076e292 | 755 | return t2; |
8d08fdba | 756 | if (t2 == error_mark_node) |
9076e292 | 757 | return t1; |
8d08fdba | 758 | |
852497a3 JM |
759 | /* Handle merging an auto redeclaration with a previous deduced |
760 | return type. */ | |
761 | if (is_auto (t1)) | |
762 | return t2; | |
763 | ||
d9525bec | 764 | /* Merge the attributes. */ |
f6897b10 | 765 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
2986ae00 | 766 | |
5566b478 MS |
767 | if (TYPE_PTRMEMFUNC_P (t1)) |
768 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); | |
769 | if (TYPE_PTRMEMFUNC_P (t2)) | |
770 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); | |
771 | ||
8d08fdba MS |
772 | code1 = TREE_CODE (t1); |
773 | code2 = TREE_CODE (t2); | |
e350dbbd PB |
774 | if (code1 != code2) |
775 | { | |
776 | gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE); | |
777 | if (code1 == TYPENAME_TYPE) | |
778 | { | |
779 | t1 = resolve_typename_type (t1, /*only_current_p=*/true); | |
780 | code1 = TREE_CODE (t1); | |
781 | } | |
782 | else | |
783 | { | |
784 | t2 = resolve_typename_type (t2, /*only_current_p=*/true); | |
785 | code2 = TREE_CODE (t2); | |
786 | } | |
787 | } | |
8d08fdba MS |
788 | |
789 | switch (code1) | |
790 | { | |
8d08fdba MS |
791 | case POINTER_TYPE: |
792 | case REFERENCE_TYPE: | |
6da794e8 | 793 | /* For two pointers, do this recursively on the target type. */ |
8d08fdba | 794 | { |
6da794e8 | 795 | tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
77adef84 | 796 | int quals = cp_type_quals (t1); |
5b163de4 | 797 | |
8d08fdba | 798 | if (code1 == POINTER_TYPE) |
62245e6f JM |
799 | { |
800 | t1 = build_pointer_type (target); | |
801 | if (TREE_CODE (target) == METHOD_TYPE) | |
802 | t1 = build_ptrmemfunc_type (t1); | |
803 | } | |
8d08fdba | 804 | else |
86089be5 | 805 | t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1)); |
71851aaa | 806 | t1 = build_type_attribute_variant (t1, attributes); |
77adef84 | 807 | t1 = cp_build_qualified_type (t1, quals); |
71851aaa | 808 | |
71851aaa | 809 | return t1; |
8d08fdba | 810 | } |
8d08fdba | 811 | |
6da794e8 JM |
812 | case OFFSET_TYPE: |
813 | { | |
fe0378ed MM |
814 | int quals; |
815 | tree pointee; | |
816 | quals = cp_type_quals (t1); | |
817 | pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1), | |
818 | TYPE_PTRMEM_POINTED_TO_TYPE (t2)); | |
819 | t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), | |
820 | pointee); | |
821 | t1 = cp_build_qualified_type (t1, quals); | |
6da794e8 JM |
822 | break; |
823 | } | |
824 | ||
8d08fdba MS |
825 | case ARRAY_TYPE: |
826 | { | |
6da794e8 | 827 | tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
8d08fdba MS |
828 | /* Save space: see if the result is identical to one of the args. */ |
829 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) | |
2986ae00 | 830 | return build_type_attribute_variant (t1, attributes); |
8d08fdba | 831 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) |
2986ae00 | 832 | return build_type_attribute_variant (t2, attributes); |
8d08fdba | 833 | /* Merge the element types, and have a size if either arg has one. */ |
e349ee73 MS |
834 | t1 = build_cplus_array_type |
835 | (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); | |
6da794e8 | 836 | break; |
8d08fdba MS |
837 | } |
838 | ||
839 | case FUNCTION_TYPE: | |
840 | /* Function types: prefer the one that specified arg types. | |
841 | If both do, merge the arg types. Also merge the return types. */ | |
842 | { | |
6da794e8 | 843 | tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
8d08fdba MS |
844 | tree p1 = TYPE_ARG_TYPES (t1); |
845 | tree p2 = TYPE_ARG_TYPES (t2); | |
a0685b73 | 846 | tree parms; |
8d08fdba | 847 | tree rval, raises; |
cab421f4 | 848 | bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1); |
8d08fdba MS |
849 | |
850 | /* Save space: see if the result is identical to one of the args. */ | |
851 | if (valtype == TREE_TYPE (t1) && ! p2) | |
e9525111 | 852 | return cp_build_type_attribute_variant (t1, attributes); |
8d08fdba | 853 | if (valtype == TREE_TYPE (t2) && ! p1) |
e9525111 | 854 | return cp_build_type_attribute_variant (t2, attributes); |
8d08fdba MS |
855 | |
856 | /* Simple way if one arg fails to specify argument types. */ | |
857 | if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node) | |
d8a0d13e | 858 | parms = p2; |
a0685b73 | 859 | else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node) |
d8a0d13e | 860 | parms = p1; |
a0685b73 | 861 | else |
d8a0d13e | 862 | parms = commonparms (p1, p2); |
8d08fdba | 863 | |
a0685b73 JM |
864 | rval = build_function_type (valtype, parms); |
865 | gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2)); | |
2eed8e37 BK |
866 | gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2)); |
867 | rval = apply_memfn_quals (rval, | |
868 | type_memfn_quals (t1), | |
869 | type_memfn_rqual (t1)); | |
d8a0d13e | 870 | raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), |
b15ea309 | 871 | TYPE_RAISES_EXCEPTIONS (t2)); |
6da794e8 | 872 | t1 = build_exception_variant (rval, raises); |
cab421f4 PC |
873 | if (late_return_type_p) |
874 | TYPE_HAS_LATE_RETURN_TYPE (t1) = 1; | |
6da794e8 | 875 | break; |
8d08fdba MS |
876 | } |
877 | ||
6da794e8 JM |
878 | case METHOD_TYPE: |
879 | { | |
880 | /* Get this value the long way, since TYPE_METHOD_BASETYPE | |
881 | is just the main variant of this. */ | |
7e1352fe | 882 | tree basetype = class_of_this_parm (t2); |
d8a0d13e | 883 | tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), |
b15ea309 | 884 | TYPE_RAISES_EXCEPTIONS (t2)); |
51543dc7 | 885 | cp_ref_qualifier rqual = type_memfn_rqual (t1); |
6da794e8 | 886 | tree t3; |
cab421f4 PC |
887 | bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1); |
888 | bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2); | |
6da794e8 JM |
889 | |
890 | /* If this was a member function type, get back to the | |
891 | original type of type member function (i.e., without | |
892 | the class instance variable up front. */ | |
893 | t1 = build_function_type (TREE_TYPE (t1), | |
894 | TREE_CHAIN (TYPE_ARG_TYPES (t1))); | |
895 | t2 = build_function_type (TREE_TYPE (t2), | |
896 | TREE_CHAIN (TYPE_ARG_TYPES (t2))); | |
897 | t3 = merge_types (t1, t2); | |
43dc123f MM |
898 | t3 = build_method_type_directly (basetype, TREE_TYPE (t3), |
899 | TYPE_ARG_TYPES (t3)); | |
6da794e8 | 900 | t1 = build_exception_variant (t3, raises); |
51543dc7 | 901 | t1 = build_ref_qualified_type (t1, rqual); |
cab421f4 PC |
902 | if (late_return_type_1_p) |
903 | TYPE_HAS_LATE_RETURN_TYPE (t1) = 1; | |
904 | if (late_return_type_2_p) | |
905 | TYPE_HAS_LATE_RETURN_TYPE (t2) = 1; | |
6da794e8 JM |
906 | break; |
907 | } | |
8d08fdba | 908 | |
b1a95e0b MM |
909 | case TYPENAME_TYPE: |
910 | /* There is no need to merge attributes into a TYPENAME_TYPE. | |
911 | When the type is instantiated it will have whatever | |
912 | attributes result from the instantiation. */ | |
913 | return t1; | |
914 | ||
6da794e8 JM |
915 | default:; |
916 | } | |
8e30dcf3 JM |
917 | |
918 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) | |
919 | return t1; | |
920 | else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
921 | return t2; | |
922 | else | |
923 | return cp_build_type_attribute_variant (t1, attributes); | |
6da794e8 | 924 | } |
8d08fdba | 925 | |
48326487 JM |
926 | /* Return the ARRAY_TYPE type without its domain. */ |
927 | ||
928 | tree | |
929 | strip_array_domain (tree type) | |
930 | { | |
931 | tree t2; | |
932 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
933 | if (TYPE_DOMAIN (type) == NULL_TREE) | |
934 | return type; | |
935 | t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); | |
936 | return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type)); | |
937 | } | |
938 | ||
98f2f3a2 MLI |
939 | /* Wrapper around cp_common_type that is used by c-common.c and other |
940 | front end optimizations that remove promotions. | |
71851aaa | 941 | |
98f2f3a2 MLI |
942 | Return the common type for two arithmetic types T1 and T2 under the |
943 | usual arithmetic conversions. The default conversions have already | |
944 | been applied, and enumerated types converted to their compatible | |
945 | integer types. */ | |
6da794e8 JM |
946 | |
947 | tree | |
5671bf27 | 948 | common_type (tree t1, tree t2) |
6da794e8 | 949 | { |
98f2f3a2 MLI |
950 | /* If one type is nonsense, use the other */ |
951 | if (t1 == error_mark_node) | |
952 | return t2; | |
953 | if (t2 == error_mark_node) | |
954 | return t1; | |
2986ae00 | 955 | |
98f2f3a2 MLI |
956 | return cp_common_type (t1, t2); |
957 | } | |
8d08fdba | 958 | |
98f2f3a2 MLI |
959 | /* Return the common type of two pointer types T1 and T2. This is the |
960 | type for the result of most arithmetic operations if the operands | |
961 | have the given two types. | |
962 | ||
963 | We assume that comp_target_types has already been done and returned | |
964 | nonzero; if that isn't so, this may crash. */ | |
8d08fdba | 965 | |
98f2f3a2 MLI |
966 | tree |
967 | common_pointer_type (tree t1, tree t2) | |
968 | { | |
969 | gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2)) | |
66b1156a | 970 | || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2)) |
98f2f3a2 MLI |
971 | || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2))); |
972 | ||
973 | return composite_pointer_type (t1, t2, error_mark_node, error_mark_node, | |
c86818cf | 974 | CPO_CONVERSION, tf_warning_or_error); |
8d08fdba MS |
975 | } |
976 | \f | |
4cc1d462 | 977 | /* Compare two exception specifier types for exactness or subsetness, if |
acd8e2d0 NS |
978 | allowed. Returns false for mismatch, true for match (same, or |
979 | derived and !exact). | |
c8094d83 | 980 | |
4cc1d462 | 981 | [except.spec] "If a class X ... objects of class X or any class publicly |
34cd5ae7 | 982 | and unambiguously derived from X. Similarly, if a pointer type Y * ... |
4cc1d462 | 983 | exceptions of type Y * or that are pointers to any type publicly and |
34cd5ae7 | 984 | unambiguously derived from Y. Otherwise a function only allows exceptions |
4cc1d462 NS |
985 | that have the same type ..." |
986 | This does not mention cv qualifiers and is different to what throw | |
987 | [except.throw] and catch [except.catch] will do. They will ignore the | |
988 | top level cv qualifiers, and allow qualifiers in the pointer to class | |
989 | example. | |
c8094d83 | 990 | |
4cc1d462 NS |
991 | We implement the letter of the standard. */ |
992 | ||
acd8e2d0 NS |
993 | static bool |
994 | comp_except_types (tree a, tree b, bool exact) | |
4cc1d462 NS |
995 | { |
996 | if (same_type_p (a, b)) | |
acd8e2d0 | 997 | return true; |
4cc1d462 NS |
998 | else if (!exact) |
999 | { | |
89d684bb | 1000 | if (cp_type_quals (a) || cp_type_quals (b)) |
0cbd7506 | 1001 | return false; |
c8094d83 | 1002 | |
50e10fa8 | 1003 | if (TYPE_PTR_P (a) && TYPE_PTR_P (b)) |
0cbd7506 MS |
1004 | { |
1005 | a = TREE_TYPE (a); | |
1006 | b = TREE_TYPE (b); | |
1007 | if (cp_type_quals (a) || cp_type_quals (b)) | |
1008 | return false; | |
1009 | } | |
c8094d83 | 1010 | |
4cc1d462 | 1011 | if (TREE_CODE (a) != RECORD_TYPE |
0cbd7506 MS |
1012 | || TREE_CODE (b) != RECORD_TYPE) |
1013 | return false; | |
c8094d83 | 1014 | |
22854930 | 1015 | if (publicly_uniquely_derived_p (a, b)) |
0cbd7506 | 1016 | return true; |
4cc1d462 | 1017 | } |
acd8e2d0 | 1018 | return false; |
4cc1d462 NS |
1019 | } |
1020 | ||
acd8e2d0 | 1021 | /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers. |
3a55fb4c JM |
1022 | If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5). |
1023 | If EXACT is ce_normal, the compatibility rules in 15.4/3 apply. | |
1024 | If EXACT is ce_exact, the specs must be exactly the same. Exception lists | |
1025 | are unordered, but we've already filtered out duplicates. Most lists will | |
1026 | be in order, we should try to make use of that. */ | |
e92cc029 | 1027 | |
acd8e2d0 | 1028 | bool |
3a55fb4c | 1029 | comp_except_specs (const_tree t1, const_tree t2, int exact) |
8d08fdba | 1030 | { |
58f9752a KG |
1031 | const_tree probe; |
1032 | const_tree base; | |
4cc1d462 NS |
1033 | int length = 0; |
1034 | ||
1035 | if (t1 == t2) | |
acd8e2d0 | 1036 | return true; |
c8094d83 | 1037 | |
6d812dd3 JM |
1038 | /* First handle noexcept. */ |
1039 | if (exact < ce_exact) | |
3a55fb4c | 1040 | { |
5e3f417f | 1041 | /* noexcept(false) is compatible with no exception-specification, |
6d812dd3 JM |
1042 | and stricter than any spec. */ |
1043 | if (t1 == noexcept_false_spec) | |
5e3f417f JM |
1044 | return t2 == NULL_TREE || exact == ce_derived; |
1045 | /* Even a derived noexcept(false) is compatible with no | |
1046 | exception-specification. */ | |
6d812dd3 | 1047 | if (t2 == noexcept_false_spec) |
5e3f417f | 1048 | return t1 == NULL_TREE; |
6d812dd3 JM |
1049 | |
1050 | /* Otherwise, if we aren't looking for an exact match, noexcept is | |
1051 | equivalent to throw(). */ | |
1052 | if (t1 == noexcept_true_spec) | |
1053 | t1 = empty_except_spec; | |
1054 | if (t2 == noexcept_true_spec) | |
1055 | t2 = empty_except_spec; | |
3a55fb4c JM |
1056 | } |
1057 | ||
6d812dd3 JM |
1058 | /* If any noexcept is left, it is only comparable to itself; |
1059 | either we're looking for an exact match or we're redeclaring a | |
1060 | template with dependent noexcept. */ | |
1061 | if ((t1 && TREE_PURPOSE (t1)) | |
1062 | || (t2 && TREE_PURPOSE (t2))) | |
1063 | return (t1 && t2 | |
1064 | && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))); | |
1065 | ||
0cbd7506 | 1066 | if (t1 == NULL_TREE) /* T1 is ... */ |
3a55fb4c | 1067 | return t2 == NULL_TREE || exact == ce_derived; |
0cbd7506 | 1068 | if (!TREE_VALUE (t1)) /* t1 is EMPTY */ |
4cc1d462 | 1069 | return t2 != NULL_TREE && !TREE_VALUE (t2); |
0cbd7506 | 1070 | if (t2 == NULL_TREE) /* T2 is ... */ |
acd8e2d0 | 1071 | return false; |
dffa4176 | 1072 | if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */ |
3a55fb4c | 1073 | return exact == ce_derived; |
c8094d83 | 1074 | |
4cc1d462 NS |
1075 | /* Neither set is ... or EMPTY, make sure each part of T2 is in T1. |
1076 | Count how many we find, to determine exactness. For exact matching and | |
1077 | ordered T1, T2, this is an O(n) operation, otherwise its worst case is | |
1078 | O(nm). */ | |
1079 | for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2)) | |
1080 | { | |
1081 | for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe)) | |
0cbd7506 MS |
1082 | { |
1083 | tree a = TREE_VALUE (probe); | |
1084 | tree b = TREE_VALUE (t2); | |
1085 | ||
1086 | if (comp_except_types (a, b, exact)) | |
1087 | { | |
3a55fb4c | 1088 | if (probe == base && exact > ce_derived) |
0cbd7506 MS |
1089 | base = TREE_CHAIN (probe); |
1090 | length++; | |
1091 | break; | |
1092 | } | |
1093 | } | |
4cc1d462 | 1094 | if (probe == NULL_TREE) |
0cbd7506 | 1095 | return false; |
4cc1d462 | 1096 | } |
3a55fb4c | 1097 | return exact == ce_derived || base == NULL_TREE || length == list_length (t1); |
8d08fdba MS |
1098 | } |
1099 | ||
acd8e2d0 NS |
1100 | /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if |
1101 | [] can match [size]. */ | |
3bfdc719 | 1102 | |
c8a209ca | 1103 | static bool |
58f9752a | 1104 | comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration) |
8d08fdba | 1105 | { |
3bfdc719 MM |
1106 | tree d1; |
1107 | tree d2; | |
30a03508 | 1108 | tree max1, max2; |
3bfdc719 MM |
1109 | |
1110 | if (t1 == t2) | |
acd8e2d0 | 1111 | return true; |
8d08fdba | 1112 | |
3bfdc719 | 1113 | /* The type of the array elements must be the same. */ |
acd8e2d0 | 1114 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
c8a209ca | 1115 | return false; |
8d08fdba | 1116 | |
3bfdc719 MM |
1117 | d1 = TYPE_DOMAIN (t1); |
1118 | d2 = TYPE_DOMAIN (t2); | |
1119 | ||
1120 | if (d1 == d2) | |
c8a209ca | 1121 | return true; |
8d08fdba | 1122 | |
3bfdc719 | 1123 | /* If one of the arrays is dimensionless, and the other has a |
0e339752 | 1124 | dimension, they are of different types. However, it is valid to |
3bfdc719 MM |
1125 | write: |
1126 | ||
1127 | extern int a[]; | |
1128 | int a[3]; | |
1129 | ||
c8094d83 | 1130 | by [basic.link]: |
3bfdc719 MM |
1131 | |
1132 | declarations for an array object can specify | |
1133 | array types that differ by the presence or absence of a major | |
1134 | array bound (_dcl.array_). */ | |
1135 | if (!d1 || !d2) | |
acd8e2d0 | 1136 | return allow_redeclaration; |
3bfdc719 MM |
1137 | |
1138 | /* Check that the dimensions are the same. */ | |
30a03508 NS |
1139 | |
1140 | if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))) | |
1141 | return false; | |
1142 | max1 = TYPE_MAX_VALUE (d1); | |
1143 | max2 = TYPE_MAX_VALUE (d2); | |
30a03508 NS |
1144 | |
1145 | if (!cp_tree_equal (max1, max2)) | |
1146 | return false; | |
1147 | ||
1148 | return true; | |
8d08fdba MS |
1149 | } |
1150 | ||
e96ce650 DS |
1151 | /* Compare the relative position of T1 and T2 into their respective |
1152 | template parameter list. | |
1153 | T1 and T2 must be template parameter types. | |
1154 | Return TRUE if T1 and T2 have the same position, FALSE otherwise. */ | |
1155 | ||
1156 | static bool | |
1157 | comp_template_parms_position (tree t1, tree t2) | |
1158 | { | |
6f1abb06 | 1159 | tree index1, index2; |
e96ce650 DS |
1160 | gcc_assert (t1 && t2 |
1161 | && TREE_CODE (t1) == TREE_CODE (t2) | |
1162 | && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM | |
1163 | || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM | |
1164 | || TREE_CODE (t1) == TEMPLATE_TYPE_PARM)); | |
1165 | ||
6f1abb06 DS |
1166 | index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1)); |
1167 | index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2)); | |
9cf10655 | 1168 | |
6f1abb06 DS |
1169 | /* Then compare their relative position. */ |
1170 | if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2) | |
1171 | || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2) | |
1172 | || (TEMPLATE_PARM_PARAMETER_PACK (index1) | |
1173 | != TEMPLATE_PARM_PARAMETER_PACK (index2))) | |
9cf10655 DS |
1174 | return false; |
1175 | ||
d08a068b JM |
1176 | /* In C++14 we can end up comparing 'auto' to a normal template |
1177 | parameter. Don't confuse them. */ | |
e4276ba5 | 1178 | if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2))) |
d08a068b JM |
1179 | return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2); |
1180 | ||
6f1abb06 | 1181 | return true; |
9cf10655 DS |
1182 | } |
1183 | ||
96d84882 | 1184 | /* Subroutine in comptypes. */ |
e92cc029 | 1185 | |
06d40de8 DG |
1186 | static bool |
1187 | structural_comptypes (tree t1, tree t2, int strict) | |
8d08fdba | 1188 | { |
8d08fdba | 1189 | if (t1 == t2) |
c8a209ca NS |
1190 | return true; |
1191 | ||
f4f206f4 | 1192 | /* Suppress errors caused by previously reported errors. */ |
ae8803a8 | 1193 | if (t1 == error_mark_node || t2 == error_mark_node) |
c8a209ca | 1194 | return false; |
c8094d83 | 1195 | |
50bc768d | 1196 | gcc_assert (TYPE_P (t1) && TYPE_P (t2)); |
c8094d83 | 1197 | |
c8a209ca NS |
1198 | /* TYPENAME_TYPEs should be resolved if the qualifying scope is the |
1199 | current instantiation. */ | |
1200 | if (TREE_CODE (t1) == TYPENAME_TYPE) | |
4195a767 | 1201 | t1 = resolve_typename_type (t1, /*only_current_p=*/true); |
c8094d83 | 1202 | |
c8a209ca | 1203 | if (TREE_CODE (t2) == TYPENAME_TYPE) |
4195a767 | 1204 | t2 = resolve_typename_type (t2, /*only_current_p=*/true); |
8d08fdba | 1205 | |
5566b478 MS |
1206 | if (TYPE_PTRMEMFUNC_P (t1)) |
1207 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); | |
1208 | if (TYPE_PTRMEMFUNC_P (t2)) | |
1209 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); | |
1210 | ||
8d08fdba | 1211 | /* Different classes of types can't be compatible. */ |
8d08fdba | 1212 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
c8a209ca | 1213 | return false; |
8d08fdba | 1214 | |
c353b8e3 MM |
1215 | /* Qualifiers must match. For array types, we will check when we |
1216 | recur on the array element types. */ | |
1217 | if (TREE_CODE (t1) != ARRAY_TYPE | |
a3360e77 JM |
1218 | && cp_type_quals (t1) != cp_type_quals (t2)) |
1219 | return false; | |
1220 | if (TREE_CODE (t1) == FUNCTION_TYPE | |
1221 | && type_memfn_quals (t1) != type_memfn_quals (t2)) | |
c8a209ca | 1222 | return false; |
2eed8e37 BK |
1223 | /* Need to check this before TYPE_MAIN_VARIANT. |
1224 | FIXME function qualifiers should really change the main variant. */ | |
1225 | if ((TREE_CODE (t1) == FUNCTION_TYPE | |
1226 | || TREE_CODE (t1) == METHOD_TYPE) | |
1227 | && type_memfn_rqual (t1) != type_memfn_rqual (t2)) | |
1228 | return false; | |
8d08fdba MS |
1229 | |
1230 | /* Allow for two different type nodes which have essentially the same | |
1231 | definition. Note that we already checked for equality of the type | |
38e01259 | 1232 | qualifiers (just above). */ |
8d08fdba | 1233 | |
c353b8e3 MM |
1234 | if (TREE_CODE (t1) != ARRAY_TYPE |
1235 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
c8a209ca | 1236 | return true; |
8d08fdba | 1237 | |
20c202f3 | 1238 | |
7866705a | 1239 | /* Compare the types. Break out if they could be the same. */ |
8d08fdba MS |
1240 | switch (TREE_CODE (t1)) |
1241 | { | |
0451301c DG |
1242 | case VOID_TYPE: |
1243 | case BOOLEAN_TYPE: | |
1244 | /* All void and bool types are the same. */ | |
1245 | break; | |
1246 | ||
1247 | case INTEGER_TYPE: | |
1248 | case FIXED_POINT_TYPE: | |
1249 | case REAL_TYPE: | |
1250 | /* With these nodes, we can't determine type equivalence by | |
1251 | looking at what is stored in the nodes themselves, because | |
1252 | two nodes might have different TYPE_MAIN_VARIANTs but still | |
1253 | represent the same type. For example, wchar_t and int could | |
1254 | have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE, | |
1255 | TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs | |
1256 | and are distinct types. On the other hand, int and the | |
1257 | following typedef | |
1258 | ||
1259 | typedef int INT __attribute((may_alias)); | |
1260 | ||
1261 | have identical properties, different TYPE_MAIN_VARIANTs, but | |
1262 | represent the same type. The canonical type system keeps | |
1263 | track of equivalence in this case, so we fall back on it. */ | |
1264 | return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); | |
1265 | ||
e7e66632 | 1266 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 1267 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
e96ce650 | 1268 | if (!comp_template_parms_position (t1, t2)) |
c8a209ca NS |
1269 | return false; |
1270 | if (!comp_template_parms | |
1271 | (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)), | |
1272 | DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2)))) | |
1273 | return false; | |
a1281f45 | 1274 | if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM) |
7866705a | 1275 | break; |
e7e66632 | 1276 | /* Don't check inheritance. */ |
acd8e2d0 | 1277 | strict = COMPARE_STRICT; |
f4f206f4 | 1278 | /* Fall through. */ |
e7e66632 | 1279 | |
8d08fdba MS |
1280 | case RECORD_TYPE: |
1281 | case UNION_TYPE: | |
7ddedda4 MM |
1282 | if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2) |
1283 | && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2) | |
c8a209ca NS |
1284 | || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM) |
1285 | && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2))) | |
7866705a | 1286 | break; |
c8094d83 | 1287 | |
acd8e2d0 | 1288 | if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2)) |
7866705a | 1289 | break; |
acd8e2d0 | 1290 | else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1)) |
7866705a | 1291 | break; |
c8094d83 | 1292 | |
c8a209ca | 1293 | return false; |
8d08fdba MS |
1294 | |
1295 | case OFFSET_TYPE: | |
96d84882 PB |
1296 | if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2), |
1297 | strict & ~COMPARE_REDECLARATION)) | |
c8a209ca | 1298 | return false; |
7866705a SB |
1299 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1300 | return false; | |
1301 | break; | |
8d08fdba | 1302 | |
8d08fdba | 1303 | case REFERENCE_TYPE: |
8af2fec4 RY |
1304 | if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2)) |
1305 | return false; | |
1306 | /* fall through to checks for pointer types */ | |
81fea426 | 1307 | gcc_fallthrough (); |
8af2fec4 RY |
1308 | |
1309 | case POINTER_TYPE: | |
7866705a | 1310 | if (TYPE_MODE (t1) != TYPE_MODE (t2) |
7866705a SB |
1311 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1312 | return false; | |
1313 | break; | |
8d08fdba | 1314 | |
dffa4176 | 1315 | case METHOD_TYPE: |
8d08fdba | 1316 | case FUNCTION_TYPE: |
acd8e2d0 | 1317 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
c8a209ca | 1318 | return false; |
7866705a SB |
1319 | if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))) |
1320 | return false; | |
1321 | break; | |
8d08fdba MS |
1322 | |
1323 | case ARRAY_TYPE: | |
9bcb9aae | 1324 | /* Target types must match incl. qualifiers. */ |
7866705a SB |
1325 | if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION))) |
1326 | return false; | |
1327 | break; | |
8d08fdba | 1328 | |
51c184be | 1329 | case TEMPLATE_TYPE_PARM: |
6f1abb06 DS |
1330 | /* If T1 and T2 don't have the same relative position in their |
1331 | template parameters set, they can't be equal. */ | |
1332 | if (!comp_template_parms_position (t1, t2)) | |
1333 | return false; | |
4fea442d JM |
1334 | /* Constrained 'auto's are distinct from parms that don't have the same |
1335 | constraints. */ | |
1336 | if (!equivalent_placeholder_constraints (t1, t2)) | |
1337 | return false; | |
7866705a | 1338 | break; |
e76a2646 MS |
1339 | |
1340 | case TYPENAME_TYPE: | |
c8a209ca NS |
1341 | if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1), |
1342 | TYPENAME_TYPE_FULLNAME (t2))) | |
0cbd7506 | 1343 | return false; |
69d28b4f JM |
1344 | /* Qualifiers don't matter on scopes. */ |
1345 | if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1), | |
1346 | TYPE_CONTEXT (t2))) | |
7866705a SB |
1347 | return false; |
1348 | break; | |
7f85441b | 1349 | |
b8c6534b | 1350 | case UNBOUND_CLASS_TEMPLATE: |
c8a209ca | 1351 | if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2))) |
0cbd7506 | 1352 | return false; |
7866705a SB |
1353 | if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))) |
1354 | return false; | |
1355 | break; | |
b8c6534b | 1356 | |
a7a64a77 | 1357 | case COMPLEX_TYPE: |
7866705a SB |
1358 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1359 | return false; | |
1360 | break; | |
a7a64a77 | 1361 | |
cc27e657 | 1362 | case VECTOR_TYPE: |
7866705a SB |
1363 | if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2) |
1364 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) | |
1365 | return false; | |
cc27e657 PB |
1366 | break; |
1367 | ||
5d80a306 | 1368 | case TYPE_PACK_EXPANSION: |
c67dd256 JM |
1369 | return (same_type_p (PACK_EXPANSION_PATTERN (t1), |
1370 | PACK_EXPANSION_PATTERN (t2)) | |
1371 | && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1), | |
1372 | PACK_EXPANSION_EXTRA_ARGS (t2))); | |
5d80a306 | 1373 | |
3ad6a8e1 DG |
1374 | case DECLTYPE_TYPE: |
1375 | if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1) | |
1376 | != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2) | |
d5f4eddd JM |
1377 | || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) |
1378 | != DECLTYPE_FOR_LAMBDA_CAPTURE (t2)) | |
9b8662c2 JM |
1379 | || (DECLTYPE_FOR_LAMBDA_PROXY (t1) |
1380 | != DECLTYPE_FOR_LAMBDA_PROXY (t2)) | |
3ad6a8e1 DG |
1381 | || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), |
1382 | DECLTYPE_TYPE_EXPR (t2))) | |
1383 | return false; | |
1384 | break; | |
1385 | ||
a0d260fc PC |
1386 | case UNDERLYING_TYPE: |
1387 | return same_type_p (UNDERLYING_TYPE_TYPE (t1), | |
1388 | UNDERLYING_TYPE_TYPE (t2)); | |
1389 | ||
7f85441b | 1390 | default: |
7866705a | 1391 | return false; |
8d08fdba | 1392 | } |
7866705a SB |
1393 | |
1394 | /* If we get here, we know that from a target independent POV the | |
1395 | types are the same. Make sure the target attributes are also | |
1396 | the same. */ | |
ac9a30ae | 1397 | return comp_type_attributes (t1, t2); |
8d08fdba MS |
1398 | } |
1399 | ||
06d40de8 DG |
1400 | /* Return true if T1 and T2 are related as allowed by STRICT. STRICT |
1401 | is a bitwise-or of the COMPARE_* flags. */ | |
1402 | ||
1403 | bool | |
96d84882 | 1404 | comptypes (tree t1, tree t2, int strict) |
06d40de8 DG |
1405 | { |
1406 | if (strict == COMPARE_STRICT) | |
1407 | { | |
06d40de8 DG |
1408 | if (t1 == t2) |
1409 | return true; | |
1410 | ||
1411 | if (t1 == error_mark_node || t2 == error_mark_node) | |
1412 | return false; | |
1413 | ||
1414 | if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2)) | |
1415 | /* At least one of the types requires structural equality, so | |
1416 | perform a deep check. */ | |
1417 | return structural_comptypes (t1, t2, strict); | |
1418 | ||
595278be | 1419 | if (flag_checking && USE_CANONICAL_TYPES) |
06d40de8 | 1420 | { |
7313518b DG |
1421 | bool result = structural_comptypes (t1, t2, strict); |
1422 | ||
06d40de8 | 1423 | if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2)) |
7313518b DG |
1424 | /* The two types are structurally equivalent, but their |
1425 | canonical types were different. This is a failure of the | |
1426 | canonical type propagation code.*/ | |
1427 | internal_error | |
1428 | ("canonical types differ for identical types %T and %T", | |
1429 | t1, t2); | |
06d40de8 | 1430 | else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)) |
7313518b DG |
1431 | /* Two types are structurally different, but the canonical |
1432 | types are the same. This means we were over-eager in | |
1433 | assigning canonical types. */ | |
1434 | internal_error | |
1435 | ("same canonical type node for different types %T and %T", | |
1436 | t1, t2); | |
06d40de8 DG |
1437 | |
1438 | return result; | |
1439 | } | |
595278be | 1440 | if (!flag_checking && USE_CANONICAL_TYPES) |
06d40de8 | 1441 | return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); |
7313518b DG |
1442 | else |
1443 | return structural_comptypes (t1, t2, strict); | |
06d40de8 DG |
1444 | } |
1445 | else if (strict == COMPARE_STRUCTURAL) | |
1446 | return structural_comptypes (t1, t2, COMPARE_STRICT); | |
1447 | else | |
1448 | return structural_comptypes (t1, t2, strict); | |
1449 | } | |
1450 | ||
10746f37 JM |
1451 | /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring |
1452 | top-level qualifiers. */ | |
1453 | ||
1454 | bool | |
1455 | same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2) | |
1456 | { | |
1457 | if (type1 == error_mark_node || type2 == error_mark_node) | |
1458 | return false; | |
1459 | ||
1460 | return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2)); | |
1461 | } | |
1462 | ||
91063b51 MM |
1463 | /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */ |
1464 | ||
acd8e2d0 | 1465 | bool |
58f9752a | 1466 | at_least_as_qualified_p (const_tree type1, const_tree type2) |
91063b51 | 1467 | { |
acd8e2d0 NS |
1468 | int q1 = cp_type_quals (type1); |
1469 | int q2 = cp_type_quals (type2); | |
c8094d83 | 1470 | |
91063b51 | 1471 | /* All qualifiers for TYPE2 must also appear in TYPE1. */ |
acd8e2d0 | 1472 | return (q1 & q2) == q2; |
91063b51 MM |
1473 | } |
1474 | ||
ceab47eb MM |
1475 | /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is |
1476 | more cv-qualified that TYPE1, and 0 otherwise. */ | |
1477 | ||
1478 | int | |
d02f620d | 1479 | comp_cv_qualification (int q1, int q2) |
ceab47eb | 1480 | { |
acd8e2d0 | 1481 | if (q1 == q2) |
ceab47eb MM |
1482 | return 0; |
1483 | ||
acd8e2d0 | 1484 | if ((q1 & q2) == q2) |
ceab47eb | 1485 | return 1; |
acd8e2d0 | 1486 | else if ((q1 & q2) == q1) |
ceab47eb MM |
1487 | return -1; |
1488 | ||
1489 | return 0; | |
1490 | } | |
1491 | ||
d02f620d JM |
1492 | int |
1493 | comp_cv_qualification (const_tree type1, const_tree type2) | |
1494 | { | |
1495 | int q1 = cp_type_quals (type1); | |
1496 | int q2 = cp_type_quals (type2); | |
1497 | return comp_cv_qualification (q1, q2); | |
1498 | } | |
1499 | ||
ceab47eb MM |
1500 | /* Returns 1 if the cv-qualification signature of TYPE1 is a proper |
1501 | subset of the cv-qualification signature of TYPE2, and the types | |
1502 | are similar. Returns -1 if the other way 'round, and 0 otherwise. */ | |
1503 | ||
1504 | int | |
acd8e2d0 | 1505 | comp_cv_qual_signature (tree type1, tree type2) |
ceab47eb MM |
1506 | { |
1507 | if (comp_ptr_ttypes_real (type2, type1, -1)) | |
1508 | return 1; | |
1509 | else if (comp_ptr_ttypes_real (type1, type2, -1)) | |
1510 | return -1; | |
1511 | else | |
1512 | return 0; | |
1513 | } | |
8d08fdba | 1514 | \f |
96d84882 | 1515 | /* Subroutines of `comptypes'. */ |
8d08fdba | 1516 | |
acd8e2d0 | 1517 | /* Return true if two parameter type lists PARMS1 and PARMS2 are |
03017874 MM |
1518 | equivalent in the sense that functions with those parameter types |
1519 | can have equivalent types. The two lists must be equivalent, | |
acd8e2d0 | 1520 | element by element. */ |
8d08fdba | 1521 | |
acd8e2d0 | 1522 | bool |
58f9752a | 1523 | compparms (const_tree parms1, const_tree parms2) |
8d08fdba | 1524 | { |
58f9752a | 1525 | const_tree t1, t2; |
8d08fdba MS |
1526 | |
1527 | /* An unspecified parmlist matches any specified parmlist | |
1528 | whose argument types don't need default promotions. */ | |
1529 | ||
acd8e2d0 NS |
1530 | for (t1 = parms1, t2 = parms2; |
1531 | t1 || t2; | |
1532 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) | |
8d08fdba | 1533 | { |
8d08fdba | 1534 | /* If one parmlist is shorter than the other, |
4daa9c14 | 1535 | they fail to match. */ |
acd8e2d0 NS |
1536 | if (!t1 || !t2) |
1537 | return false; | |
ae8803a8 | 1538 | if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) |
acd8e2d0 | 1539 | return false; |
8d08fdba | 1540 | } |
acd8e2d0 | 1541 | return true; |
8d08fdba MS |
1542 | } |
1543 | ||
8d08fdba | 1544 | \f |
7a18b933 NS |
1545 | /* Process a sizeof or alignof expression where the operand is a |
1546 | type. */ | |
1547 | ||
8d08fdba | 1548 | tree |
7a18b933 | 1549 | cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain) |
8d08fdba | 1550 | { |
fa72b064 | 1551 | tree value; |
b4c74ba2 | 1552 | bool dependent_p; |
8d08fdba | 1553 | |
50bc768d | 1554 | gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR); |
7a18b933 NS |
1555 | if (type == error_mark_node) |
1556 | return error_mark_node; | |
c8094d83 | 1557 | |
ee76b931 | 1558 | type = non_reference (type); |
3c17e16e | 1559 | if (TREE_CODE (type) == METHOD_TYPE) |
8d08fdba | 1560 | { |
fcf73884 | 1561 | if (complain) |
44d90fe1 | 1562 | pedwarn (input_location, OPT_Wpointer_arith, |
fcf73884 | 1563 | "invalid application of %qs to a member function", |
3c17e16e | 1564 | operator_name_info[(int) op].name); |
aa0a659c PC |
1565 | else |
1566 | return error_mark_node; | |
fa72b064 | 1567 | value = size_one_node; |
8d08fdba | 1568 | } |
8d08fdba | 1569 | |
b4c74ba2 MM |
1570 | dependent_p = dependent_type_p (type); |
1571 | if (!dependent_p) | |
1572 | complete_type (type); | |
1573 | if (dependent_p | |
3c17e16e MM |
1574 | /* VLA types will have a non-constant size. In the body of an |
1575 | uninstantiated template, we don't need to try to compute the | |
1576 | value, because the sizeof expression is not an integral | |
1577 | constant expression in that case. And, if we do try to | |
1578 | compute the value, we'll likely end up with SAVE_EXPRs, which | |
1579 | the template substitution machinery does not expect to see. */ | |
b4c74ba2 MM |
1580 | || (processing_template_decl |
1581 | && COMPLETE_TYPE_P (type) | |
1582 | && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)) | |
3c17e16e MM |
1583 | { |
1584 | value = build_min (op, size_type_node, type); | |
1585 | TREE_READONLY (value) = 1; | |
1586 | return value; | |
1587 | } | |
1588 | ||
c2255bc4 | 1589 | return c_sizeof_or_alignof_type (input_location, complete_type (type), |
296674db | 1590 | op == SIZEOF_EXPR, false, |
3c17e16e | 1591 | complain); |
8d08fdba MS |
1592 | } |
1593 | ||
5ade1ed2 DG |
1594 | /* Return the size of the type, without producing any warnings for |
1595 | types whose size cannot be taken. This routine should be used only | |
1596 | in some other routine that has already produced a diagnostic about | |
1597 | using the size of such a type. */ | |
1598 | tree | |
1599 | cxx_sizeof_nowarn (tree type) | |
1600 | { | |
1601 | if (TREE_CODE (type) == FUNCTION_TYPE | |
50e10fa8 | 1602 | || VOID_TYPE_P (type) |
5ade1ed2 DG |
1603 | || TREE_CODE (type) == ERROR_MARK) |
1604 | return size_one_node; | |
1605 | else if (!COMPLETE_TYPE_P (type)) | |
1606 | return size_zero_node; | |
1607 | else | |
1608 | return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false); | |
1609 | } | |
1610 | ||
229970c4 | 1611 | /* Process a sizeof expression where the operand is an expression. */ |
7a18b933 | 1612 | |
229970c4 | 1613 | static tree |
5ade1ed2 | 1614 | cxx_sizeof_expr (tree e, tsubst_flags_t complain) |
5566b478 | 1615 | { |
7a18b933 NS |
1616 | if (e == error_mark_node) |
1617 | return error_mark_node; | |
c8094d83 | 1618 | |
5156628f | 1619 | if (processing_template_decl) |
7a18b933 | 1620 | { |
229970c4 | 1621 | e = build_min (SIZEOF_EXPR, size_type_node, e); |
7a18b933 NS |
1622 | TREE_SIDE_EFFECTS (e) = 0; |
1623 | TREE_READONLY (e) = 1; | |
c8094d83 | 1624 | |
7a18b933 NS |
1625 | return e; |
1626 | } | |
c8094d83 | 1627 | |
48326487 JM |
1628 | /* To get the size of a static data member declared as an array of |
1629 | unknown bound, we need to instantiate it. */ | |
5a6ccc94 | 1630 | if (VAR_P (e) |
48326487 JM |
1631 | && VAR_HAD_UNKNOWN_BOUND (e) |
1632 | && DECL_TEMPLATE_INSTANTIATION (e)) | |
1633 | instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false); | |
1634 | ||
773ec47f MP |
1635 | if (TREE_CODE (e) == PARM_DECL |
1636 | && DECL_ARRAY_PARAMETER_P (e) | |
1637 | && (complain & tf_warning)) | |
1638 | { | |
1639 | if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function " | |
1640 | "parameter %qE will return size of %qT", e, TREE_TYPE (e))) | |
1641 | inform (DECL_SOURCE_LOCATION (e), "declared here"); | |
1642 | } | |
1643 | ||
03a904b5 JJ |
1644 | e = mark_type_use (e); |
1645 | ||
5566b478 | 1646 | if (TREE_CODE (e) == COMPONENT_REF |
ea0e2a51 | 1647 | && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL |
162bc98d | 1648 | && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1))) |
5566b478 | 1649 | { |
5ade1ed2 DG |
1650 | if (complain & tf_error) |
1651 | error ("invalid application of %<sizeof%> to a bit-field"); | |
1652 | else | |
1653 | return error_mark_node; | |
7a18b933 NS |
1654 | e = char_type_node; |
1655 | } | |
1656 | else if (is_overloaded_fn (e)) | |
1657 | { | |
5ade1ed2 | 1658 | if (complain & tf_error) |
cbe5f3b3 | 1659 | permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of " |
37ec60ed | 1660 | "function type"); |
5ade1ed2 DG |
1661 | else |
1662 | return error_mark_node; | |
7a18b933 | 1663 | e = char_type_node; |
5566b478 | 1664 | } |
05e0b2f4 JM |
1665 | else if (type_unknown_p (e)) |
1666 | { | |
5ade1ed2 DG |
1667 | if (complain & tf_error) |
1668 | cxx_incomplete_type_error (e, TREE_TYPE (e)); | |
1669 | else | |
1670 | return error_mark_node; | |
7a18b933 | 1671 | e = char_type_node; |
05e0b2f4 | 1672 | } |
7a18b933 NS |
1673 | else |
1674 | e = TREE_TYPE (e); | |
c8094d83 | 1675 | |
5ade1ed2 | 1676 | return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error); |
5566b478 | 1677 | } |
c8094d83 | 1678 | |
229970c4 | 1679 | /* Implement the __alignof keyword: Return the minimum required |
69d1a403 | 1680 | alignment of E, measured in bytes. For VAR_DECL's and |
229970c4 JM |
1681 | FIELD_DECL's return DECL_ALIGN (which can be set from an |
1682 | "aligned" __attribute__ specification). */ | |
1683 | ||
1684 | static tree | |
5ade1ed2 | 1685 | cxx_alignof_expr (tree e, tsubst_flags_t complain) |
229970c4 JM |
1686 | { |
1687 | tree t; | |
3db45ab5 | 1688 | |
229970c4 JM |
1689 | if (e == error_mark_node) |
1690 | return error_mark_node; | |
1691 | ||
1692 | if (processing_template_decl) | |
1693 | { | |
1694 | e = build_min (ALIGNOF_EXPR, size_type_node, e); | |
1695 | TREE_SIDE_EFFECTS (e) = 0; | |
1696 | TREE_READONLY (e) = 1; | |
1697 | ||
1698 | return e; | |
1699 | } | |
1700 | ||
03a904b5 JJ |
1701 | e = mark_type_use (e); |
1702 | ||
5a6ccc94 | 1703 | if (VAR_P (e)) |
229970c4 JM |
1704 | t = size_int (DECL_ALIGN_UNIT (e)); |
1705 | else if (TREE_CODE (e) == COMPONENT_REF | |
1706 | && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL | |
1707 | && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1))) | |
1708 | { | |
5ade1ed2 DG |
1709 | if (complain & tf_error) |
1710 | error ("invalid application of %<__alignof%> to a bit-field"); | |
1711 | else | |
1712 | return error_mark_node; | |
229970c4 JM |
1713 | t = size_one_node; |
1714 | } | |
1715 | else if (TREE_CODE (e) == COMPONENT_REF | |
1716 | && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL) | |
1717 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1))); | |
1718 | else if (is_overloaded_fn (e)) | |
1719 | { | |
5ade1ed2 | 1720 | if (complain & tf_error) |
cbe5f3b3 | 1721 | permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of " |
37ec60ed | 1722 | "function type"); |
5ade1ed2 DG |
1723 | else |
1724 | return error_mark_node; | |
837edd5f GK |
1725 | if (TREE_CODE (e) == FUNCTION_DECL) |
1726 | t = size_int (DECL_ALIGN_UNIT (e)); | |
1727 | else | |
1728 | t = size_one_node; | |
229970c4 JM |
1729 | } |
1730 | else if (type_unknown_p (e)) | |
1731 | { | |
5ade1ed2 DG |
1732 | if (complain & tf_error) |
1733 | cxx_incomplete_type_error (e, TREE_TYPE (e)); | |
1734 | else | |
1735 | return error_mark_node; | |
229970c4 JM |
1736 | t = size_one_node; |
1737 | } | |
1738 | else | |
5ade1ed2 DG |
1739 | return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, |
1740 | complain & tf_error); | |
229970c4 JM |
1741 | |
1742 | return fold_convert (size_type_node, t); | |
1743 | } | |
1744 | ||
69d1a403 JM |
1745 | /* Process a sizeof or alignof expression E with code OP where the operand |
1746 | is an expression. */ | |
229970c4 JM |
1747 | |
1748 | tree | |
5ade1ed2 | 1749 | cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain) |
229970c4 JM |
1750 | { |
1751 | if (op == SIZEOF_EXPR) | |
5ade1ed2 | 1752 | return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none); |
229970c4 | 1753 | else |
5ade1ed2 | 1754 | return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none); |
229970c4 | 1755 | } |
e28d52cf DS |
1756 | |
1757 | /* Build a representation of an expression 'alignas(E).' Return the | |
1758 | folded integer value of E if it is an integral constant expression | |
1759 | that resolves to a valid alignment. If E depends on a template | |
1760 | parameter, return a syntactic representation tree of kind | |
1761 | ALIGNOF_EXPR. Otherwise, return an error_mark_node if the | |
1762 | expression is ill formed, or NULL_TREE if E is NULL_TREE. */ | |
1763 | ||
1764 | tree | |
1765 | cxx_alignas_expr (tree e) | |
1766 | { | |
1767 | if (e == NULL_TREE || e == error_mark_node | |
1768 | || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e))) | |
1769 | return e; | |
1770 | ||
1771 | if (TYPE_P (e)) | |
1772 | /* [dcl.align]/3: | |
1773 | ||
1774 | When the alignment-specifier is of the form | |
1775 | alignas(type-id ), it shall have the same effect as | |
17365662 | 1776 | alignas(alignof(type-id )). */ |
e28d52cf DS |
1777 | |
1778 | return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false); | |
1779 | ||
e28d52cf DS |
1780 | /* If we reach this point, it means the alignas expression if of |
1781 | the form "alignas(assignment-expression)", so we should follow | |
1782 | what is stated by [dcl.align]/2. */ | |
1783 | ||
17365662 JM |
1784 | if (value_dependent_expression_p (e)) |
1785 | /* Leave value-dependent expression alone for now. */ | |
1786 | return e; | |
1787 | ||
234bef96 | 1788 | e = instantiate_non_dependent_expr (e); |
e28d52cf DS |
1789 | e = mark_rvalue_use (e); |
1790 | ||
1791 | /* [dcl.align]/2 says: | |
1792 | ||
1793 | the assignment-expression shall be an integral constant | |
1794 | expression. */ | |
1795 | ||
17365662 | 1796 | return cxx_constant_value (e); |
e28d52cf DS |
1797 | } |
1798 | ||
8d08fdba | 1799 | \f |
c8b2e872 MM |
1800 | /* EXPR is being used in a context that is not a function call. |
1801 | Enforce: | |
1802 | ||
c8094d83 | 1803 | [expr.ref] |
c8b2e872 MM |
1804 | |
1805 | The expression can be used only as the left-hand operand of a | |
c8094d83 | 1806 | member function call. |
c8b2e872 MM |
1807 | |
1808 | [expr.mptr.operator] | |
1809 | ||
1810 | If the result of .* or ->* is a function, then that result can be | |
c8094d83 | 1811 | used only as the operand for the function call operator (). |
c8b2e872 MM |
1812 | |
1813 | by issuing an error message if appropriate. Returns true iff EXPR | |
1814 | violates these rules. */ | |
1815 | ||
1816 | bool | |
d3ea4c06 | 1817 | invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain) |
c8b2e872 | 1818 | { |
6c74ff23 JM |
1819 | if (expr == NULL_TREE) |
1820 | return false; | |
1821 | /* Don't enforce this in MS mode. */ | |
1822 | if (flag_ms_extensions) | |
1823 | return false; | |
1824 | if (is_overloaded_fn (expr) && !really_overloaded_fn (expr)) | |
1825 | expr = get_first_fn (expr); | |
1826 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr)) | |
c8b2e872 | 1827 | { |
5ade1ed2 | 1828 | if (complain & tf_error) |
d3ea4c06 MLI |
1829 | { |
1830 | if (DECL_P (expr)) | |
1831 | { | |
1832 | error_at (loc, "invalid use of non-static member function %qD", | |
1833 | expr); | |
1834 | inform (DECL_SOURCE_LOCATION (expr), "declared here"); | |
1835 | } | |
1836 | else | |
1837 | error_at (loc, "invalid use of non-static member function of " | |
1838 | "type %qT", TREE_TYPE (expr)); | |
1839 | } | |
c8b2e872 MM |
1840 | return true; |
1841 | } | |
1842 | return false; | |
1843 | } | |
1844 | ||
38a4afee MM |
1845 | /* If EXP is a reference to a bitfield, and the type of EXP does not |
1846 | match the declared type of the bitfield, return the declared type | |
1847 | of the bitfield. Otherwise, return NULL_TREE. */ | |
1848 | ||
1849 | tree | |
58f9752a | 1850 | is_bitfield_expr_with_lowered_type (const_tree exp) |
38a4afee | 1851 | { |
f9aa54d3 | 1852 | switch (TREE_CODE (exp)) |
38a4afee | 1853 | { |
f9aa54d3 | 1854 | case COND_EXPR: |
42924ed7 JJ |
1855 | if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1) |
1856 | ? TREE_OPERAND (exp, 1) | |
1857 | : TREE_OPERAND (exp, 0))) | |
38a4afee MM |
1858 | return NULL_TREE; |
1859 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2)); | |
f9aa54d3 MM |
1860 | |
1861 | case COMPOUND_EXPR: | |
1862 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)); | |
1863 | ||
1864 | case MODIFY_EXPR: | |
1865 | case SAVE_EXPR: | |
1866 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); | |
1867 | ||
1868 | case COMPONENT_REF: | |
1869 | { | |
1870 | tree field; | |
1871 | ||
1872 | field = TREE_OPERAND (exp, 1); | |
e76d7cc7 | 1873 | if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field)) |
f9aa54d3 MM |
1874 | return NULL_TREE; |
1875 | if (same_type_ignoring_top_level_qualifiers_p | |
1876 | (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field))) | |
1877 | return NULL_TREE; | |
1878 | return DECL_BIT_FIELD_TYPE (field); | |
1879 | } | |
1880 | ||
5f9e56b3 | 1881 | CASE_CONVERT: |
be2b5483 RG |
1882 | if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0))) |
1883 | == TYPE_MAIN_VARIANT (TREE_TYPE (exp))) | |
1884 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); | |
1885 | /* Fallthrough. */ | |
1886 | ||
f9aa54d3 MM |
1887 | default: |
1888 | return NULL_TREE; | |
38a4afee | 1889 | } |
f9aa54d3 MM |
1890 | } |
1891 | ||
1892 | /* Like is_bitfield_with_lowered_type, except that if EXP is not a | |
1893 | bitfield with a lowered type, the type of EXP is returned, rather | |
1894 | than NULL_TREE. */ | |
1895 | ||
1896 | tree | |
58f9752a | 1897 | unlowered_expr_type (const_tree exp) |
f9aa54d3 MM |
1898 | { |
1899 | tree type; | |
c1e41527 | 1900 | tree etype = TREE_TYPE (exp); |
f9aa54d3 MM |
1901 | |
1902 | type = is_bitfield_expr_with_lowered_type (exp); | |
c1e41527 JM |
1903 | if (type) |
1904 | type = cp_build_qualified_type (type, cp_type_quals (etype)); | |
1905 | else | |
1906 | type = etype; | |
f9aa54d3 MM |
1907 | |
1908 | return type; | |
38a4afee MM |
1909 | } |
1910 | ||
0a72704b MM |
1911 | /* Perform the conversions in [expr] that apply when an lvalue appears |
1912 | in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and | |
56233bd6 JM |
1913 | function-to-pointer conversions. In addition, bitfield references are |
1914 | converted to their declared types. Note that this function does not perform | |
1915 | the lvalue-to-rvalue conversion for class types. If you need that conversion | |
1916 | for class types, then you probably need to use force_rvalue. | |
8d08fdba | 1917 | |
41990f96 MM |
1918 | Although the returned value is being used as an rvalue, this |
1919 | function does not wrap the returned expression in a | |
1920 | NON_LVALUE_EXPR; the caller is expected to be mindful of the fact | |
1921 | that the return value is no longer an lvalue. */ | |
8d08fdba MS |
1922 | |
1923 | tree | |
1807ffc1 MS |
1924 | decay_conversion (tree exp, |
1925 | tsubst_flags_t complain, | |
1926 | bool reject_builtin /* = true */) | |
8d08fdba | 1927 | { |
926ce8bd KH |
1928 | tree type; |
1929 | enum tree_code code; | |
8400e75e | 1930 | location_t loc = EXPR_LOC_OR_LOC (exp, input_location); |
8d08fdba | 1931 | |
a359be75 | 1932 | type = TREE_TYPE (exp); |
07c88314 MM |
1933 | if (type == error_mark_node) |
1934 | return error_mark_node; | |
1935 | ||
46f0d909 | 1936 | exp = resolve_nondeduced_context (exp, complain); |
40260429 NS |
1937 | if (type_unknown_p (exp)) |
1938 | { | |
89fcabaf PC |
1939 | if (complain & tf_error) |
1940 | cxx_incomplete_type_error (exp, TREE_TYPE (exp)); | |
40260429 NS |
1941 | return error_mark_node; |
1942 | } | |
8d08fdba | 1943 | |
9bdae013 DS |
1944 | code = TREE_CODE (type); |
1945 | ||
69eb4fde JM |
1946 | if (error_operand_p (exp)) |
1947 | return error_mark_node; | |
c8094d83 | 1948 | |
ec62cbe1 | 1949 | if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp)) |
0c32fb95 JM |
1950 | return nullptr_node; |
1951 | ||
8d08fdba MS |
1952 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
1953 | Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */ | |
8d08fdba MS |
1954 | if (code == VOID_TYPE) |
1955 | { | |
89fcabaf | 1956 | if (complain & tf_error) |
5a3c9cf2 | 1957 | error_at (loc, "void value not ignored as it ought to be"); |
8d08fdba MS |
1958 | return error_mark_node; |
1959 | } | |
d3ea4c06 | 1960 | if (invalid_nonstatic_memfn_p (loc, exp, complain)) |
c8b2e872 | 1961 | return error_mark_node; |
e6e174e5 | 1962 | if (code == FUNCTION_TYPE || is_overloaded_fn (exp)) |
56233bd6 JM |
1963 | { |
1964 | exp = mark_lvalue_use (exp); | |
1965 | if (reject_builtin && reject_gcc_builtin (exp, loc)) | |
1966 | return error_mark_node; | |
1967 | return cp_build_addr_expr (exp, complain); | |
1968 | } | |
8d08fdba MS |
1969 | if (code == ARRAY_TYPE) |
1970 | { | |
926ce8bd | 1971 | tree adr; |
8d08fdba | 1972 | tree ptrtype; |
8d08fdba | 1973 | |
56233bd6 JM |
1974 | exp = mark_lvalue_use (exp); |
1975 | ||
591cb3cf | 1976 | if (INDIRECT_REF_P (exp)) |
c8094d83 | 1977 | return build_nop (build_pointer_type (TREE_TYPE (type)), |
7b6d72fc | 1978 | TREE_OPERAND (exp, 0)); |
8d08fdba MS |
1979 | |
1980 | if (TREE_CODE (exp) == COMPOUND_EXPR) | |
1981 | { | |
89fcabaf PC |
1982 | tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain); |
1983 | if (op1 == error_mark_node) | |
1984 | return error_mark_node; | |
f293ce4b RS |
1985 | return build2 (COMPOUND_EXPR, TREE_TYPE (op1), |
1986 | TREE_OPERAND (exp, 0), op1); | |
8d08fdba MS |
1987 | } |
1988 | ||
bb19d4af | 1989 | if (!obvalue_p (exp) |
8d08fdba MS |
1990 | && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) |
1991 | { | |
89fcabaf | 1992 | if (complain & tf_error) |
5a3c9cf2 | 1993 | error_at (loc, "invalid use of non-lvalue array"); |
8d08fdba MS |
1994 | return error_mark_node; |
1995 | } | |
1996 | ||
1f37c583 JM |
1997 | /* Don't let an array compound literal decay to a pointer. It can |
1998 | still be used to initialize an array or bind to a reference. */ | |
1999 | if (TREE_CODE (exp) == TARGET_EXPR) | |
2000 | { | |
2001 | if (complain & tf_error) | |
2002 | error_at (loc, "taking address of temporary array"); | |
2003 | return error_mark_node; | |
2004 | } | |
2005 | ||
01240200 | 2006 | ptrtype = build_pointer_type (TREE_TYPE (type)); |
8d08fdba | 2007 | |
5a6ccc94 | 2008 | if (VAR_P (exp)) |
8d08fdba | 2009 | { |
dffd7eb6 | 2010 | if (!cxx_mark_addressable (exp)) |
8d08fdba | 2011 | return error_mark_node; |
0e8c9b28 | 2012 | adr = build_nop (ptrtype, build_address (exp)); |
8d08fdba MS |
2013 | return adr; |
2014 | } | |
2015 | /* This way is better for a COMPONENT_REF since it can | |
2016 | simplify the offset for a component. */ | |
89fcabaf | 2017 | adr = cp_build_addr_expr (exp, complain); |
4b978f96 | 2018 | return cp_convert (ptrtype, adr, complain); |
8d08fdba | 2019 | } |
3db45ab5 | 2020 | |
56233bd6 JM |
2021 | /* Otherwise, it's the lvalue-to-rvalue conversion. */ |
2022 | exp = mark_rvalue_use (exp, loc, reject_builtin); | |
2023 | ||
41990f96 MM |
2024 | /* If a bitfield is used in a context where integral promotion |
2025 | applies, then the caller is expected to have used | |
2026 | default_conversion. That function promotes bitfields correctly | |
2027 | before calling this function. At this point, if we have a | |
2028 | bitfield referenced, we may assume that is not subject to | |
2029 | promotion, and that, therefore, the type of the resulting rvalue | |
2030 | is the declared type of the bitfield. */ | |
2031 | exp = convert_bitfield_to_declared_type (exp); | |
a9aedbc2 | 2032 | |
41990f96 MM |
2033 | /* We do not call rvalue() here because we do not want to wrap EXP |
2034 | in a NON_LVALUE_EXPR. */ | |
2035 | ||
2036 | /* [basic.lval] | |
2037 | ||
2038 | Non-class rvalues always have cv-unqualified types. */ | |
2039 | type = TREE_TYPE (exp); | |
36c37128 | 2040 | if (!CLASS_TYPE_P (type) && cv_qualified_p (type)) |
164247b0 | 2041 | exp = build_nop (cv_unqualified (type), exp); |
ba9c33e8 | 2042 | |
56233bd6 JM |
2043 | if (!complete_type_or_maybe_complain (type, exp, complain)) |
2044 | return error_mark_node; | |
2045 | ||
a9aedbc2 MS |
2046 | return exp; |
2047 | } | |
2048 | ||
39a13be5 | 2049 | /* Perform preparatory conversions, as part of the "usual arithmetic |
41990f96 MM |
2050 | conversions". In particular, as per [expr]: |
2051 | ||
2052 | Whenever an lvalue expression appears as an operand of an | |
2053 | operator that expects the rvalue for that operand, the | |
2054 | lvalue-to-rvalue, array-to-pointer, or function-to-pointer | |
2055 | standard conversions are applied to convert the expression to an | |
2056 | rvalue. | |
2057 | ||
2058 | In addition, we perform integral promotions here, as those are | |
2059 | applied to both operands to a binary operator before determining | |
2060 | what additional conversions should apply. */ | |
2061 | ||
89fcabaf PC |
2062 | static tree |
2063 | cp_default_conversion (tree exp, tsubst_flags_t complain) | |
a9aedbc2 | 2064 | { |
40449a90 SL |
2065 | /* Check for target-specific promotions. */ |
2066 | tree promoted_type = targetm.promoted_type (TREE_TYPE (exp)); | |
2067 | if (promoted_type) | |
4b978f96 | 2068 | exp = cp_convert (promoted_type, exp, complain); |
41990f96 MM |
2069 | /* Perform the integral promotions first so that bitfield |
2070 | expressions (which may promote to "int", even if the bitfield is | |
3db45ab5 | 2071 | declared "unsigned") are promoted correctly. */ |
40449a90 | 2072 | else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp))) |
4b978f96 | 2073 | exp = cp_perform_integral_promotions (exp, complain); |
41990f96 | 2074 | /* Perform the other conversions. */ |
89fcabaf | 2075 | exp = decay_conversion (exp, complain); |
a9aedbc2 | 2076 | |
8d08fdba MS |
2077 | return exp; |
2078 | } | |
6b5fbb55 | 2079 | |
89fcabaf PC |
2080 | /* C version. */ |
2081 | ||
2082 | tree | |
2083 | default_conversion (tree exp) | |
2084 | { | |
2085 | return cp_default_conversion (exp, tf_warning_or_error); | |
2086 | } | |
2087 | ||
0a72704b MM |
2088 | /* EXPR is an expression with an integral or enumeration type. |
2089 | Perform the integral promotions in [conv.prom], and return the | |
2090 | converted value. */ | |
2091 | ||
2092 | tree | |
4b978f96 | 2093 | cp_perform_integral_promotions (tree expr, tsubst_flags_t complain) |
0a72704b MM |
2094 | { |
2095 | tree type; | |
2096 | tree promoted_type; | |
2097 | ||
03a904b5 JJ |
2098 | expr = mark_rvalue_use (expr); |
2099 | ||
efe1ad46 MM |
2100 | /* [conv.prom] |
2101 | ||
2102 | If the bitfield has an enumerated type, it is treated as any | |
2103 | other value of that type for promotion purposes. */ | |
2104 | type = is_bitfield_expr_with_lowered_type (expr); | |
2105 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) | |
2106 | type = TREE_TYPE (expr); | |
50bc768d | 2107 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); |
3c395ecf JM |
2108 | /* Scoped enums don't promote. */ |
2109 | if (SCOPED_ENUM_P (type)) | |
2110 | return expr; | |
0a72704b MM |
2111 | promoted_type = type_promotes_to (type); |
2112 | if (type != promoted_type) | |
4b978f96 | 2113 | expr = cp_convert (promoted_type, expr, complain); |
0a72704b MM |
2114 | return expr; |
2115 | } | |
2116 | ||
4b978f96 PC |
2117 | /* C version. */ |
2118 | ||
2119 | tree | |
2120 | perform_integral_promotions (tree expr) | |
2121 | { | |
2122 | return cp_perform_integral_promotions (expr, tf_warning_or_error); | |
2123 | } | |
2124 | ||
d9cf7c82 JM |
2125 | /* Returns nonzero iff exp is a STRING_CST or the result of applying |
2126 | decay_conversion to one. */ | |
2127 | ||
2128 | int | |
58f9752a | 2129 | string_conv_p (const_tree totype, const_tree exp, int warn) |
d9cf7c82 JM |
2130 | { |
2131 | tree t; | |
2132 | ||
50e10fa8 | 2133 | if (!TYPE_PTR_P (totype)) |
d9cf7c82 JM |
2134 | return 0; |
2135 | ||
2136 | t = TREE_TYPE (totype); | |
3bfdc719 | 2137 | if (!same_type_p (t, char_type_node) |
b6baa67d KVH |
2138 | && !same_type_p (t, char16_type_node) |
2139 | && !same_type_p (t, char32_type_node) | |
3bfdc719 | 2140 | && !same_type_p (t, wchar_type_node)) |
d9cf7c82 JM |
2141 | return 0; |
2142 | ||
848b92e1 | 2143 | if (TREE_CODE (exp) == STRING_CST) |
d9cf7c82 | 2144 | { |
b6baa67d | 2145 | /* Make sure that we don't try to convert between char and wide chars. */ |
6e176bd6 | 2146 | if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t)) |
848b92e1 JM |
2147 | return 0; |
2148 | } | |
2149 | else | |
2150 | { | |
2151 | /* Is this a string constant which has decayed to 'const char *'? */ | |
a3360e77 | 2152 | t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST)); |
3bfdc719 | 2153 | if (!same_type_p (TREE_TYPE (exp), t)) |
d9cf7c82 JM |
2154 | return 0; |
2155 | STRIP_NOPS (exp); | |
2156 | if (TREE_CODE (exp) != ADDR_EXPR | |
2157 | || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST) | |
2158 | return 0; | |
2159 | } | |
b323323f | 2160 | if (warn) |
aaf176ca VV |
2161 | { |
2162 | if (cxx_dialect >= cxx11) | |
2163 | pedwarn (input_location, | |
2164 | pedantic ? OPT_Wpedantic : OPT_Wwrite_strings, | |
2165 | "ISO C++ forbids converting a string constant to %qT", | |
2166 | totype); | |
2167 | else | |
2168 | warning (OPT_Wwrite_strings, | |
2169 | "deprecated conversion from string constant to %qT", | |
2170 | totype); | |
2171 | } | |
d9cf7c82 JM |
2172 | |
2173 | return 1; | |
2174 | } | |
8d08fdba | 2175 | |
e9a25f70 JL |
2176 | /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we |
2177 | can, for example, use as an lvalue. This code used to be in | |
2178 | unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c' | |
2179 | expressions, where we're dealing with aggregates. But now it's again only | |
2180 | called from unary_complex_lvalue. The case (in particular) that led to | |
2181 | this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd | |
2182 | get it there. */ | |
e92cc029 | 2183 | |
8d08fdba | 2184 | static tree |
5ade1ed2 DG |
2185 | rationalize_conditional_expr (enum tree_code code, tree t, |
2186 | tsubst_flags_t complain) | |
8d08fdba | 2187 | { |
8400e75e DM |
2188 | location_t loc = EXPR_LOC_OR_LOC (t, input_location); |
2189 | ||
e9a25f70 JL |
2190 | /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that |
2191 | the first operand is always the one to be used if both operands | |
2192 | are equal, so we know what conditional expression this used to be. */ | |
2193 | if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR) | |
2194 | { | |
2a67bec2 ILT |
2195 | tree op0 = TREE_OPERAND (t, 0); |
2196 | tree op1 = TREE_OPERAND (t, 1); | |
2197 | ||
d211a298 | 2198 | /* The following code is incorrect if either operand side-effects. */ |
2a67bec2 ILT |
2199 | gcc_assert (!TREE_SIDE_EFFECTS (op0) |
2200 | && !TREE_SIDE_EFFECTS (op1)); | |
e9a25f70 | 2201 | return |
8400e75e DM |
2202 | build_conditional_expr (loc, |
2203 | build_x_binary_op (loc, | |
4fe977f2 | 2204 | (TREE_CODE (t) == MIN_EXPR |
e9a25f70 | 2205 | ? LE_EXPR : GE_EXPR), |
2a67bec2 ILT |
2206 | op0, TREE_CODE (op0), |
2207 | op1, TREE_CODE (op1), | |
6904f4b4 | 2208 | /*overload=*/NULL, |
5ade1ed2 | 2209 | complain), |
e51fbec3 MP |
2210 | cp_build_unary_op (code, op0, false, complain), |
2211 | cp_build_unary_op (code, op1, false, complain), | |
5ade1ed2 | 2212 | complain); |
e9a25f70 JL |
2213 | } |
2214 | ||
8d08fdba | 2215 | return |
8400e75e | 2216 | build_conditional_expr (loc, TREE_OPERAND (t, 0), |
e51fbec3 | 2217 | cp_build_unary_op (code, TREE_OPERAND (t, 1), false, |
5ade1ed2 | 2218 | complain), |
e51fbec3 | 2219 | cp_build_unary_op (code, TREE_OPERAND (t, 2), false, |
5ade1ed2 DG |
2220 | complain), |
2221 | complain); | |
8d08fdba MS |
2222 | } |
2223 | ||
9e9ff709 MS |
2224 | /* Given the TYPE of an anonymous union field inside T, return the |
2225 | FIELD_DECL for the field. If not found return NULL_TREE. Because | |
2226 | anonymous unions can nest, we must also search all anonymous unions | |
2227 | that are directly reachable. */ | |
e92cc029 | 2228 | |
b3dd05b1 | 2229 | tree |
acd8e2d0 | 2230 | lookup_anon_field (tree t, tree type) |
9e9ff709 MS |
2231 | { |
2232 | tree field; | |
2233 | ||
b75cebc4 MP |
2234 | t = TYPE_MAIN_VARIANT (t); |
2235 | ||
910ad8de | 2236 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
9e9ff709 MS |
2237 | { |
2238 | if (TREE_STATIC (field)) | |
2239 | continue; | |
17bbb839 | 2240 | if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) |
9e9ff709 MS |
2241 | continue; |
2242 | ||
2243 | /* If we find it directly, return the field. */ | |
2244 | if (DECL_NAME (field) == NULL_TREE | |
71631a1f | 2245 | && type == TYPE_MAIN_VARIANT (TREE_TYPE (field))) |
9e9ff709 MS |
2246 | { |
2247 | return field; | |
2248 | } | |
2249 | ||
2250 | /* Otherwise, it could be nested, search harder. */ | |
2251 | if (DECL_NAME (field) == NULL_TREE | |
6bdb8141 | 2252 | && ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
9e9ff709 MS |
2253 | { |
2254 | tree subfield = lookup_anon_field (TREE_TYPE (field), type); | |
2255 | if (subfield) | |
2256 | return subfield; | |
2257 | } | |
2258 | } | |
2259 | return NULL_TREE; | |
2260 | } | |
2261 | ||
50ad9642 MM |
2262 | /* Build an expression representing OBJECT.MEMBER. OBJECT is an |
2263 | expression; MEMBER is a DECL or baselink. If ACCESS_PATH is | |
2264 | non-NULL, it indicates the path to the base used to name MEMBER. | |
2265 | If PRESERVE_REFERENCE is true, the expression returned will have | |
2266 | REFERENCE_TYPE if the MEMBER does. Otherwise, the expression | |
c8094d83 | 2267 | returned will have the type referred to by the reference. |
50ad9642 MM |
2268 | |
2269 | This function does not perform access control; that is either done | |
2270 | earlier by the parser when the name of MEMBER is resolved to MEMBER | |
2271 | itself, or later when overload resolution selects one of the | |
2272 | functions indicated by MEMBER. */ | |
e92cc029 | 2273 | |
8d08fdba | 2274 | tree |
e87eed2a | 2275 | build_class_member_access_expr (cp_expr object, tree member, |
5ade1ed2 DG |
2276 | tree access_path, bool preserve_reference, |
2277 | tsubst_flags_t complain) | |
8d08fdba | 2278 | { |
50ad9642 MM |
2279 | tree object_type; |
2280 | tree member_scope; | |
2281 | tree result = NULL_TREE; | |
557831a9 | 2282 | tree using_decl = NULL_TREE; |
8d08fdba | 2283 | |
2e5d2970 | 2284 | if (error_operand_p (object) || error_operand_p (member)) |
01240200 | 2285 | return error_mark_node; |
5566b478 | 2286 | |
50bc768d | 2287 | gcc_assert (DECL_P (member) || BASELINK_P (member)); |
8d08fdba | 2288 | |
50ad9642 | 2289 | /* [expr.ref] |
5566b478 | 2290 | |
50ad9642 MM |
2291 | The type of the first expression shall be "class object" (of a |
2292 | complete type). */ | |
2293 | object_type = TREE_TYPE (object); | |
c8094d83 | 2294 | if (!currently_open_class (object_type) |
309714d4 | 2295 | && !complete_type_or_maybe_complain (object_type, object, complain)) |
50ad9642 MM |
2296 | return error_mark_node; |
2297 | if (!CLASS_TYPE_P (object_type)) | |
fc378698 | 2298 | { |
5ade1ed2 | 2299 | if (complain & tf_error) |
a7248d5f PC |
2300 | { |
2301 | if (POINTER_TYPE_P (object_type) | |
2302 | && CLASS_TYPE_P (TREE_TYPE (object_type))) | |
2303 | error ("request for member %qD in %qE, which is of pointer " | |
2304 | "type %qT (maybe you meant to use %<->%> ?)", | |
e87eed2a | 2305 | member, object.get_value (), object_type); |
a7248d5f PC |
2306 | else |
2307 | error ("request for member %qD in %qE, which is of non-class " | |
e87eed2a | 2308 | "type %qT", member, object.get_value (), object_type); |
a7248d5f | 2309 | } |
50ad9642 | 2310 | return error_mark_node; |
fc378698 | 2311 | } |
8d08fdba | 2312 | |
50ad9642 MM |
2313 | /* The standard does not seem to actually say that MEMBER must be a |
2314 | member of OBJECT_TYPE. However, that is clearly what is | |
2315 | intended. */ | |
2316 | if (DECL_P (member)) | |
8d08fdba | 2317 | { |
50ad9642 | 2318 | member_scope = DECL_CLASS_CONTEXT (member); |
9f635aba PC |
2319 | if (!mark_used (member, complain) && !(complain & tf_error)) |
2320 | return error_mark_node; | |
50ad9642 | 2321 | if (TREE_DEPRECATED (member)) |
9b86d6bb | 2322 | warn_deprecated_use (member, NULL_TREE); |
50ad9642 MM |
2323 | } |
2324 | else | |
0197270c | 2325 | member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member)); |
50ad9642 MM |
2326 | /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will |
2327 | presently be the anonymous union. Go outwards until we find a | |
2328 | type related to OBJECT_TYPE. */ | |
8d0d1915 | 2329 | while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope)) |
50ad9642 MM |
2330 | && !same_type_ignoring_top_level_qualifiers_p (member_scope, |
2331 | object_type)) | |
2332 | member_scope = TYPE_CONTEXT (member_scope); | |
2333 | if (!member_scope || !DERIVED_FROM_P (member_scope, object_type)) | |
2334 | { | |
5ade1ed2 DG |
2335 | if (complain & tf_error) |
2336 | { | |
2337 | if (TREE_CODE (member) == FIELD_DECL) | |
2338 | error ("invalid use of nonstatic data member %qE", member); | |
2339 | else | |
2340 | error ("%qD is not a member of %qT", member, object_type); | |
2341 | } | |
50ad9642 | 2342 | return error_mark_node; |
8d08fdba | 2343 | } |
8d08fdba | 2344 | |
f576dfc4 JM |
2345 | /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into |
2346 | `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue | |
3b426391 | 2347 | in the front end; only _DECLs and _REFs are lvalues in the back end. */ |
f576dfc4 JM |
2348 | { |
2349 | tree temp = unary_complex_lvalue (ADDR_EXPR, object); | |
2350 | if (temp) | |
dd865ef6 | 2351 | object = cp_build_indirect_ref (temp, RO_NULL, complain); |
f576dfc4 | 2352 | } |
2050a1bb | 2353 | |
50ad9642 MM |
2354 | /* In [expr.ref], there is an explicit list of the valid choices for |
2355 | MEMBER. We check for each of those cases here. */ | |
5a6ccc94 | 2356 | if (VAR_P (member)) |
8d08fdba | 2357 | { |
50ad9642 MM |
2358 | /* A static data member. */ |
2359 | result = member; | |
2aa64966 | 2360 | mark_exp_read (object); |
50ad9642 MM |
2361 | /* If OBJECT has side-effects, they are supposed to occur. */ |
2362 | if (TREE_SIDE_EFFECTS (object)) | |
f293ce4b | 2363 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result); |
8d08fdba | 2364 | } |
50ad9642 MM |
2365 | else if (TREE_CODE (member) == FIELD_DECL) |
2366 | { | |
2367 | /* A non-static data member. */ | |
2368 | bool null_object_p; | |
2369 | int type_quals; | |
2370 | tree member_type; | |
8d08fdba | 2371 | |
1a161cd7 MS |
2372 | if (INDIRECT_REF_P (object)) |
2373 | null_object_p = | |
2374 | integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0))); | |
2375 | else | |
2376 | null_object_p = false; | |
8d08fdba | 2377 | |
50ad9642 | 2378 | /* Convert OBJECT to the type of MEMBER. */ |
bdaa131b JM |
2379 | if (!same_type_p (TYPE_MAIN_VARIANT (object_type), |
2380 | TYPE_MAIN_VARIANT (member_scope))) | |
8d08fdba | 2381 | { |
50ad9642 MM |
2382 | tree binfo; |
2383 | base_kind kind; | |
2384 | ||
2385 | binfo = lookup_base (access_path ? access_path : object_type, | |
22854930 | 2386 | member_scope, ba_unique, &kind, complain); |
50ad9642 MM |
2387 | if (binfo == error_mark_node) |
2388 | return error_mark_node; | |
2389 | ||
bdaa131b | 2390 | /* It is invalid to try to get to a virtual base of a |
50ad9642 MM |
2391 | NULL object. The most common cause is invalid use of |
2392 | offsetof macro. */ | |
2393 | if (null_object_p && kind == bk_via_virtual) | |
2394 | { | |
5ade1ed2 DG |
2395 | if (complain & tf_error) |
2396 | { | |
8591575f JM |
2397 | error ("invalid access to non-static data member %qD in " |
2398 | "virtual base of NULL object", member); | |
5ade1ed2 | 2399 | } |
50ad9642 MM |
2400 | return error_mark_node; |
2401 | } | |
2402 | ||
2403 | /* Convert to the base. */ | |
c8094d83 | 2404 | object = build_base_path (PLUS_EXPR, object, binfo, |
a271590a | 2405 | /*nonnull=*/1, complain); |
50ad9642 MM |
2406 | /* If we found the base successfully then we should be able |
2407 | to convert to it successfully. */ | |
50bc768d | 2408 | gcc_assert (object != error_mark_node); |
8d08fdba | 2409 | } |
3b5b4904 | 2410 | |
50ad9642 MM |
2411 | /* If MEMBER is from an anonymous aggregate, we have converted |
2412 | OBJECT so that it refers to the class containing the | |
2413 | anonymous union. Generate a reference to the anonymous union | |
2414 | itself, and recur to find MEMBER. */ | |
0ca7178c | 2415 | if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member)) |
a723baf1 MM |
2416 | /* When this code is called from build_field_call, the |
2417 | object already has the type of the anonymous union. | |
2418 | That is because the COMPONENT_REF was already | |
2419 | constructed, and was then disassembled before calling | |
2420 | build_field_call. After the function-call code is | |
2421 | cleaned up, this waste can be eliminated. */ | |
c8094d83 | 2422 | && (!same_type_ignoring_top_level_qualifiers_p |
a723baf1 | 2423 | (TREE_TYPE (object), DECL_CONTEXT (member)))) |
50ad9642 MM |
2424 | { |
2425 | tree anonymous_union; | |
2426 | ||
2427 | anonymous_union = lookup_anon_field (TREE_TYPE (object), | |
2428 | DECL_CONTEXT (member)); | |
2429 | object = build_class_member_access_expr (object, | |
2430 | anonymous_union, | |
2431 | /*access_path=*/NULL_TREE, | |
5ade1ed2 DG |
2432 | preserve_reference, |
2433 | complain); | |
50ad9642 MM |
2434 | } |
2435 | ||
2436 | /* Compute the type of the field, as described in [expr.ref]. */ | |
2437 | type_quals = TYPE_UNQUALIFIED; | |
2438 | member_type = TREE_TYPE (member); | |
2439 | if (TREE_CODE (member_type) != REFERENCE_TYPE) | |
2440 | { | |
c8094d83 | 2441 | type_quals = (cp_type_quals (member_type) |
50ad9642 | 2442 | | cp_type_quals (object_type)); |
c8094d83 | 2443 | |
50ad9642 MM |
2444 | /* A field is const (volatile) if the enclosing object, or the |
2445 | field itself, is const (volatile). But, a mutable field is | |
2446 | not const, even within a const object. */ | |
2447 | if (DECL_MUTABLE_P (member)) | |
2448 | type_quals &= ~TYPE_QUAL_CONST; | |
2449 | member_type = cp_build_qualified_type (member_type, type_quals); | |
2450 | } | |
2451 | ||
35228ac7 JJ |
2452 | result = build3_loc (input_location, COMPONENT_REF, member_type, |
2453 | object, member, NULL_TREE); | |
50ad9642 MM |
2454 | |
2455 | /* Mark the expression const or volatile, as appropriate. Even | |
2456 | though we've dealt with the type above, we still have to mark the | |
2457 | expression itself. */ | |
2458 | if (type_quals & TYPE_QUAL_CONST) | |
2459 | TREE_READONLY (result) = 1; | |
12a669d1 | 2460 | if (type_quals & TYPE_QUAL_VOLATILE) |
50ad9642 MM |
2461 | TREE_THIS_VOLATILE (result) = 1; |
2462 | } | |
2463 | else if (BASELINK_P (member)) | |
9e9ff709 | 2464 | { |
50ad9642 MM |
2465 | /* The member is a (possibly overloaded) member function. */ |
2466 | tree functions; | |
a723baf1 | 2467 | tree type; |
50ad9642 MM |
2468 | |
2469 | /* If the MEMBER is exactly one static member function, then we | |
2470 | know the type of the expression. Otherwise, we must wait | |
2471 | until overload resolution has been performed. */ | |
2472 | functions = BASELINK_FUNCTIONS (member); | |
2473 | if (TREE_CODE (functions) == FUNCTION_DECL | |
2474 | && DECL_STATIC_FUNCTION_P (functions)) | |
a723baf1 | 2475 | type = TREE_TYPE (functions); |
50ad9642 | 2476 | else |
a723baf1 MM |
2477 | type = unknown_type_node; |
2478 | /* Note that we do not convert OBJECT to the BASELINK_BINFO | |
2479 | base. That will happen when the function is called. */ | |
f293ce4b | 2480 | result = build3 (COMPONENT_REF, type, object, member, NULL_TREE); |
9e9ff709 | 2481 | } |
50ad9642 | 2482 | else if (TREE_CODE (member) == CONST_DECL) |
b928a651 | 2483 | { |
50ad9642 MM |
2484 | /* The member is an enumerator. */ |
2485 | result = member; | |
2486 | /* If OBJECT has side-effects, they are supposed to occur. */ | |
2487 | if (TREE_SIDE_EFFECTS (object)) | |
f293ce4b RS |
2488 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
2489 | object, result); | |
b928a651 | 2490 | } |
557831a9 FC |
2491 | else if ((using_decl = strip_using_decl (member)) != member) |
2492 | result = build_class_member_access_expr (object, | |
2493 | using_decl, | |
2494 | access_path, preserve_reference, | |
2495 | complain); | |
8d08fdba MS |
2496 | else |
2497 | { | |
5ade1ed2 DG |
2498 | if (complain & tf_error) |
2499 | error ("invalid use of %qD", member); | |
50ad9642 MM |
2500 | return error_mark_node; |
2501 | } | |
8d08fdba | 2502 | |
50ad9642 MM |
2503 | if (!preserve_reference) |
2504 | /* [expr.ref] | |
c8094d83 | 2505 | |
50ad9642 MM |
2506 | If E2 is declared to have type "reference to T", then ... the |
2507 | type of E1.E2 is T. */ | |
2508 | result = convert_from_reference (result); | |
e6f62286 | 2509 | |
50ad9642 MM |
2510 | return result; |
2511 | } | |
8d08fdba | 2512 | |
05134211 JM |
2513 | /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if |
2514 | SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */ | |
a723baf1 MM |
2515 | |
2516 | static tree | |
76545796 PC |
2517 | lookup_destructor (tree object, tree scope, tree dtor_name, |
2518 | tsubst_flags_t complain) | |
a723baf1 MM |
2519 | { |
2520 | tree object_type = TREE_TYPE (object); | |
2521 | tree dtor_type = TREE_OPERAND (dtor_name, 0); | |
4546865e | 2522 | tree expr; |
a723baf1 | 2523 | |
9ca57929 PC |
2524 | /* We've already complained about this destructor. */ |
2525 | if (dtor_type == error_mark_node) | |
2526 | return error_mark_node; | |
2527 | ||
1b3d28a8 | 2528 | if (scope && !check_dtor_name (scope, dtor_type)) |
a723baf1 | 2529 | { |
76545796 PC |
2530 | if (complain & tf_error) |
2531 | error ("qualified type %qT does not match destructor name ~%qT", | |
2532 | scope, dtor_type); | |
a723baf1 MM |
2533 | return error_mark_node; |
2534 | } | |
3df70c62 JM |
2535 | if (is_auto (dtor_type)) |
2536 | dtor_type = object_type; | |
2537 | else if (identifier_p (dtor_type)) | |
05134211 JM |
2538 | { |
2539 | /* In a template, names we can't find a match for are still accepted | |
2540 | destructor names, and we check them here. */ | |
2541 | if (check_dtor_name (object_type, dtor_type)) | |
2542 | dtor_type = object_type; | |
2543 | else | |
2544 | { | |
76545796 PC |
2545 | if (complain & tf_error) |
2546 | error ("object type %qT does not match destructor name ~%qT", | |
2547 | object_type, dtor_type); | |
05134211 JM |
2548 | return error_mark_node; |
2549 | } | |
2550 | ||
2551 | } | |
2552 | else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type))) | |
a723baf1 | 2553 | { |
76545796 PC |
2554 | if (complain & tf_error) |
2555 | error ("the type being destroyed is %qT, but the destructor " | |
2556 | "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type); | |
a723baf1 MM |
2557 | return error_mark_node; |
2558 | } | |
4546865e | 2559 | expr = lookup_member (dtor_type, complete_dtor_identifier, |
db422ace PC |
2560 | /*protect=*/1, /*want_type=*/false, |
2561 | tf_warning_or_error); | |
e383e095 KT |
2562 | if (!expr) |
2563 | { | |
2564 | if (complain & tf_error) | |
2565 | cxx_incomplete_type_error (dtor_name, dtor_type); | |
2566 | return error_mark_node; | |
2567 | } | |
4546865e MM |
2568 | expr = (adjust_result_of_qualified_name_lookup |
2569 | (expr, dtor_type, object_type)); | |
917c1602 JM |
2570 | if (scope == NULL_TREE) |
2571 | /* We need to call adjust_result_of_qualified_name_lookup in case the | |
2572 | destructor names a base class, but we unset BASELINK_QUALIFIED_P so | |
2573 | that we still get virtual function binding. */ | |
2574 | BASELINK_QUALIFIED_P (expr) = false; | |
4546865e | 2575 | return expr; |
a723baf1 MM |
2576 | } |
2577 | ||
02ed62dd MM |
2578 | /* An expression of the form "A::template B" has been resolved to |
2579 | DECL. Issue a diagnostic if B is not a template or template | |
2580 | specialization. */ | |
2581 | ||
2582 | void | |
2583 | check_template_keyword (tree decl) | |
2584 | { | |
2585 | /* The standard says: | |
2586 | ||
2587 | [temp.names] | |
2588 | ||
2589 | If a name prefixed by the keyword template is not a member | |
2590 | template, the program is ill-formed. | |
2591 | ||
2592 | DR 228 removed the restriction that the template be a member | |
3db45ab5 MS |
2593 | template. |
2594 | ||
dd36d4e1 | 2595 | DR 96, if accepted would add the further restriction that explicit |
02ed62dd MM |
2596 | template arguments must be provided if the template keyword is |
2597 | used, but, as of 2005-10-16, that DR is still in "drafting". If | |
2598 | this DR is accepted, then the semantic checks here can be | |
2599 | simplified, as the entity named must in fact be a template | |
2600 | specialization, rather than, as at present, a set of overloaded | |
2601 | functions containing at least one template function. */ | |
2602 | if (TREE_CODE (decl) != TEMPLATE_DECL | |
2603 | && TREE_CODE (decl) != TEMPLATE_ID_EXPR) | |
2604 | { | |
38d795d2 PP |
2605 | if (VAR_P (decl)) |
2606 | { | |
2607 | if (DECL_USE_TEMPLATE (decl) | |
2608 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) | |
2609 | ; | |
2610 | else | |
2611 | permerror (input_location, "%qD is not a template", decl); | |
2612 | } | |
2613 | else if (!is_overloaded_fn (decl)) | |
cbe5f3b3 | 2614 | permerror (input_location, "%qD is not a template", decl); |
02ed62dd MM |
2615 | else |
2616 | { | |
2617 | tree fns; | |
459051a0 MM |
2618 | fns = decl; |
2619 | if (BASELINK_P (fns)) | |
2620 | fns = BASELINK_FUNCTIONS (fns); | |
02ed62dd MM |
2621 | while (fns) |
2622 | { | |
2623 | tree fn = OVL_CURRENT (fns); | |
2624 | if (TREE_CODE (fn) == TEMPLATE_DECL | |
2625 | || TREE_CODE (fn) == TEMPLATE_ID_EXPR) | |
2626 | break; | |
2627 | if (TREE_CODE (fn) == FUNCTION_DECL | |
2628 | && DECL_USE_TEMPLATE (fn) | |
2629 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))) | |
2630 | break; | |
2631 | fns = OVL_NEXT (fns); | |
2632 | } | |
2633 | if (!fns) | |
cbe5f3b3 | 2634 | permerror (input_location, "%qD is not a template", decl); |
02ed62dd MM |
2635 | } |
2636 | } | |
2637 | } | |
2638 | ||
50ad9642 MM |
2639 | /* This function is called by the parser to process a class member |
2640 | access expression of the form OBJECT.NAME. NAME is a node used by | |
2641 | the parser to represent a name; it is not yet a DECL. It may, | |
2642 | however, be a BASELINK where the BASELINK_FUNCTIONS is a | |
2643 | TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and | |
2644 | there is no reason to do the lookup twice, so the parser keeps the | |
02ed62dd MM |
2645 | BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to |
2646 | be a template via the use of the "A::template B" syntax. */ | |
8d08fdba | 2647 | |
50ad9642 | 2648 | tree |
e87eed2a | 2649 | finish_class_member_access_expr (cp_expr object, tree name, bool template_p, |
5ade1ed2 | 2650 | tsubst_flags_t complain) |
50ad9642 | 2651 | { |
d17811fd | 2652 | tree expr; |
50ad9642 MM |
2653 | tree object_type; |
2654 | tree member; | |
2655 | tree access_path = NULL_TREE; | |
d17811fd MM |
2656 | tree orig_object = object; |
2657 | tree orig_name = name; | |
3c8c2a0a | 2658 | |
50ad9642 MM |
2659 | if (object == error_mark_node || name == error_mark_node) |
2660 | return error_mark_node; | |
3c8c2a0a | 2661 | |
e58a9aa1 ZL |
2662 | /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */ |
2663 | if (!objc_is_public (object, name)) | |
2664 | return error_mark_node; | |
2665 | ||
d17811fd MM |
2666 | object_type = TREE_TYPE (object); |
2667 | ||
50ad9642 | 2668 | if (processing_template_decl) |
d17811fd | 2669 | { |
a2b4cfaa | 2670 | if (/* If OBJECT is dependent, so is OBJECT.NAME. */ |
23cb7266 | 2671 | type_dependent_object_expression_p (object) |
d17811fd MM |
2672 | /* If NAME is "f<args>", where either 'f' or 'args' is |
2673 | dependent, then the expression is dependent. */ | |
2674 | || (TREE_CODE (name) == TEMPLATE_ID_EXPR | |
2675 | && dependent_template_id_p (TREE_OPERAND (name, 0), | |
2676 | TREE_OPERAND (name, 1))) | |
2677 | /* If NAME is "T::X" where "T" is dependent, then the | |
2678 | expression is dependent. */ | |
2679 | || (TREE_CODE (name) == SCOPE_REF | |
2680 | && TYPE_P (TREE_OPERAND (name, 0)) | |
23cb7266 JM |
2681 | && dependent_scope_p (TREE_OPERAND (name, 0)))) |
2682 | { | |
2683 | dependent: | |
2684 | return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, | |
99676625 | 2685 | orig_object, orig_name, NULL_TREE); |
23cb7266 | 2686 | } |
d17811fd MM |
2687 | object = build_non_dependent_expr (object); |
2688 | } | |
668ea4b1 | 2689 | else if (c_dialect_objc () |
9dc6f476 | 2690 | && identifier_p (name) |
46a88c12 | 2691 | && (expr = objc_maybe_build_component_ref (object, name))) |
668ea4b1 IS |
2692 | return expr; |
2693 | ||
50ad9642 | 2694 | /* [expr.ref] |
e23bd218 | 2695 | |
50ad9642 MM |
2696 | The type of the first expression shall be "class object" (of a |
2697 | complete type). */ | |
c8094d83 | 2698 | if (!currently_open_class (object_type) |
309714d4 | 2699 | && !complete_type_or_maybe_complain (object_type, object, complain)) |
50ad9642 MM |
2700 | return error_mark_node; |
2701 | if (!CLASS_TYPE_P (object_type)) | |
2702 | { | |
5ade1ed2 | 2703 | if (complain & tf_error) |
a7248d5f PC |
2704 | { |
2705 | if (POINTER_TYPE_P (object_type) | |
2706 | && CLASS_TYPE_P (TREE_TYPE (object_type))) | |
2707 | error ("request for member %qD in %qE, which is of pointer " | |
2708 | "type %qT (maybe you meant to use %<->%> ?)", | |
e87eed2a | 2709 | name, object.get_value (), object_type); |
a7248d5f PC |
2710 | else |
2711 | error ("request for member %qD in %qE, which is of non-class " | |
e87eed2a | 2712 | "type %qT", name, object.get_value (), object_type); |
a7248d5f | 2713 | } |
50ad9642 MM |
2714 | return error_mark_node; |
2715 | } | |
f2d9afec | 2716 | |
50ad9642 | 2717 | if (BASELINK_P (name)) |
fdeff563 NS |
2718 | /* A member function that has already been looked up. */ |
2719 | member = name; | |
50ad9642 MM |
2720 | else |
2721 | { | |
2722 | bool is_template_id = false; | |
2723 | tree template_args = NULL_TREE; | |
d17811fd | 2724 | tree scope; |
2ee887f2 | 2725 | |
50ad9642 | 2726 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
2ee887f2 | 2727 | { |
50ad9642 MM |
2728 | is_template_id = true; |
2729 | template_args = TREE_OPERAND (name, 1); | |
2730 | name = TREE_OPERAND (name, 0); | |
4864cc4a KL |
2731 | |
2732 | if (TREE_CODE (name) == OVERLOAD) | |
2733 | name = DECL_NAME (get_first_fn (name)); | |
2734 | else if (DECL_P (name)) | |
2735 | name = DECL_NAME (name); | |
50ad9642 | 2736 | } |
f2d9afec | 2737 | |
50ad9642 MM |
2738 | if (TREE_CODE (name) == SCOPE_REF) |
2739 | { | |
e13d2b4e MM |
2740 | /* A qualified name. The qualifying class or namespace `S' |
2741 | has already been looked up; it is either a TYPE or a | |
2742 | NAMESPACE_DECL. */ | |
50ad9642 MM |
2743 | scope = TREE_OPERAND (name, 0); |
2744 | name = TREE_OPERAND (name, 1); | |
50ad9642 MM |
2745 | |
2746 | /* If SCOPE is a namespace, then the qualified name does not | |
2747 | name a member of OBJECT_TYPE. */ | |
2748 | if (TREE_CODE (scope) == NAMESPACE_DECL) | |
2ee887f2 | 2749 | { |
5ade1ed2 DG |
2750 | if (complain & tf_error) |
2751 | error ("%<%D::%D%> is not a member of %qT", | |
2752 | scope, name, object_type); | |
2ee887f2 MS |
2753 | return error_mark_node; |
2754 | } | |
50ad9642 | 2755 | |
c53d966d JM |
2756 | if (TREE_CODE (scope) == ENUMERAL_TYPE) |
2757 | { | |
2758 | /* Looking up a member enumerator (c++/56793). */ | |
2759 | if (!TYPE_CLASS_SCOPE_P (scope) | |
2760 | || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type)) | |
2761 | { | |
2762 | if (complain & tf_error) | |
2763 | error ("%<%D::%D%> is not a member of %qT", | |
2764 | scope, name, object_type); | |
2765 | return error_mark_node; | |
2766 | } | |
2767 | tree val = lookup_enumerator (scope, name); | |
09b59d4d NS |
2768 | if (!val) |
2769 | { | |
2770 | if (complain & tf_error) | |
2771 | error ("%qD is not a member of %qD", | |
2772 | name, scope); | |
2773 | return error_mark_node; | |
2774 | } | |
2775 | ||
c53d966d JM |
2776 | if (TREE_SIDE_EFFECTS (object)) |
2777 | val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val); | |
2778 | return val; | |
2779 | } | |
2780 | ||
e13d2b4e | 2781 | gcc_assert (CLASS_TYPE_P (scope)); |
9dc6f476 | 2782 | gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR); |
e13d2b4e | 2783 | |
abd5730b JM |
2784 | if (constructor_name_p (name, scope)) |
2785 | { | |
2786 | if (complain & tf_error) | |
2787 | error ("cannot call constructor %<%T::%D%> directly", | |
2788 | scope, name); | |
2789 | return error_mark_node; | |
2790 | } | |
2791 | ||
50ad9642 | 2792 | /* Find the base of OBJECT_TYPE corresponding to SCOPE. */ |
22854930 PC |
2793 | access_path = lookup_base (object_type, scope, ba_check, |
2794 | NULL, complain); | |
c8a65a25 | 2795 | if (access_path == error_mark_node) |
50ad9642 | 2796 | return error_mark_node; |
c8a65a25 MM |
2797 | if (!access_path) |
2798 | { | |
92354dc7 JM |
2799 | if (any_dependent_bases_p (object_type)) |
2800 | goto dependent; | |
5ade1ed2 DG |
2801 | if (complain & tf_error) |
2802 | error ("%qT is not a base of %qT", scope, object_type); | |
c8a65a25 MM |
2803 | return error_mark_node; |
2804 | } | |
2ee887f2 | 2805 | } |
d17811fd MM |
2806 | else |
2807 | { | |
2808 | scope = NULL_TREE; | |
2809 | access_path = object_type; | |
2810 | } | |
2811 | ||
2812 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
23cb7266 JM |
2813 | { |
2814 | if (dependent_type_p (object_type)) | |
2815 | /* The destructor isn't declared yet. */ | |
2816 | goto dependent; | |
2817 | member = lookup_destructor (object, scope, name, complain); | |
2818 | } | |
d17811fd | 2819 | else |
9e9ff709 | 2820 | { |
d17811fd | 2821 | /* Look up the member. */ |
c8094d83 | 2822 | member = lookup_member (access_path, name, /*protect=*/1, |
db422ace | 2823 | /*want_type=*/false, complain); |
8a5f4379 GDR |
2824 | if (member == NULL_TREE) |
2825 | { | |
23cb7266 JM |
2826 | if (dependent_type_p (object_type)) |
2827 | /* Try again at instantiation time. */ | |
2828 | goto dependent; | |
5ade1ed2 | 2829 | if (complain & tf_error) |
8ece8dfb DM |
2830 | { |
2831 | tree guessed_id = lookup_member_fuzzy (access_path, name, | |
2832 | /*want_type=*/false); | |
2833 | if (guessed_id) | |
bc1aee87 DM |
2834 | { |
2835 | location_t bogus_component_loc = input_location; | |
264757fb DM |
2836 | gcc_rich_location rich_loc (bogus_component_loc); |
2837 | rich_loc.add_fixit_misspelled_id (bogus_component_loc, | |
2838 | guessed_id); | |
bc1aee87 DM |
2839 | error_at_rich_loc |
2840 | (&rich_loc, | |
2841 | "%q#T has no member named %qE; did you mean %qE?", | |
2842 | TREE_CODE (access_path) == TREE_BINFO | |
2843 | ? TREE_TYPE (access_path) : object_type, name, | |
2844 | guessed_id); | |
2845 | } | |
8ece8dfb DM |
2846 | else |
2847 | error ("%q#T has no member named %qE", | |
2848 | TREE_CODE (access_path) == TREE_BINFO | |
2849 | ? TREE_TYPE (access_path) : object_type, name); | |
2850 | } | |
8a5f4379 GDR |
2851 | return error_mark_node; |
2852 | } | |
d17811fd | 2853 | if (member == error_mark_node) |
50ad9642 | 2854 | return error_mark_node; |
f3365c12 JM |
2855 | if (DECL_P (member) |
2856 | && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member))) | |
2857 | /* Dependent type attributes on the decl mean that the TREE_TYPE is | |
2858 | wrong, so don't use it. */ | |
2859 | goto dependent; | |
23cb7266 JM |
2860 | if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member)) |
2861 | goto dependent; | |
50ad9642 | 2862 | } |
c8094d83 | 2863 | |
50ad9642 MM |
2864 | if (is_template_id) |
2865 | { | |
be93747e | 2866 | tree templ = member; |
c8094d83 | 2867 | |
be93747e KG |
2868 | if (BASELINK_P (templ)) |
2869 | templ = lookup_template_function (templ, template_args); | |
50ad9642 MM |
2870 | else |
2871 | { | |
5ade1ed2 DG |
2872 | if (complain & tf_error) |
2873 | error ("%qD is not a member template function", name); | |
50ad9642 MM |
2874 | return error_mark_node; |
2875 | } | |
9e9ff709 MS |
2876 | } |
2877 | } | |
2878 | ||
a723baf1 | 2879 | if (TREE_DEPRECATED (member)) |
9b86d6bb | 2880 | warn_deprecated_use (member, NULL_TREE); |
a723baf1 | 2881 | |
02ed62dd MM |
2882 | if (template_p) |
2883 | check_template_keyword (member); | |
2884 | ||
d17811fd | 2885 | expr = build_class_member_access_expr (object, member, access_path, |
5ade1ed2 DG |
2886 | /*preserve_reference=*/false, |
2887 | complain); | |
d17811fd | 2888 | if (processing_template_decl && expr != error_mark_node) |
61e71a9e NS |
2889 | { |
2890 | if (BASELINK_P (member)) | |
2891 | { | |
2892 | if (TREE_CODE (orig_name) == SCOPE_REF) | |
2893 | BASELINK_QUALIFIED_P (member) = 1; | |
2894 | orig_name = member; | |
2895 | } | |
2896 | return build_min_non_dep (COMPONENT_REF, expr, | |
2897 | orig_object, orig_name, | |
2898 | NULL_TREE); | |
2899 | } | |
3db45ab5 | 2900 | |
d17811fd | 2901 | return expr; |
8d08fdba | 2902 | } |
5156628f | 2903 | |
0138d6b2 JM |
2904 | /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate |
2905 | type. */ | |
2906 | ||
2907 | tree | |
2908 | build_simple_component_ref (tree object, tree member) | |
2909 | { | |
2910 | tree type = cp_build_qualified_type (TREE_TYPE (member), | |
2911 | cp_type_quals (TREE_TYPE (object))); | |
cda0a029 JM |
2912 | return build3_loc (input_location, |
2913 | COMPONENT_REF, type, | |
2914 | object, member, NULL_TREE); | |
0138d6b2 JM |
2915 | } |
2916 | ||
50ad9642 MM |
2917 | /* Return an expression for the MEMBER_NAME field in the internal |
2918 | representation of PTRMEM, a pointer-to-member function. (Each | |
2919 | pointer-to-member function type gets its own RECORD_TYPE so it is | |
2920 | more convenient to access the fields by name than by FIELD_DECL.) | |
2921 | This routine converts the NAME to a FIELD_DECL and then creates the | |
2922 | node for the complete expression. */ | |
5156628f MS |
2923 | |
2924 | tree | |
50ad9642 | 2925 | build_ptrmemfunc_access_expr (tree ptrmem, tree member_name) |
5156628f | 2926 | { |
50ad9642 MM |
2927 | tree ptrmem_type; |
2928 | tree member; | |
50ad9642 MM |
2929 | |
2930 | /* This code is a stripped down version of | |
2931 | build_class_member_access_expr. It does not work to use that | |
2932 | routine directly because it expects the object to be of class | |
2933 | type. */ | |
2934 | ptrmem_type = TREE_TYPE (ptrmem); | |
50bc768d | 2935 | gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type)); |
1d7e2ec7 JM |
2936 | for (member = TYPE_FIELDS (ptrmem_type); member; |
2937 | member = DECL_CHAIN (member)) | |
2938 | if (DECL_NAME (member) == member_name) | |
2939 | break; | |
0138d6b2 | 2940 | return build_simple_component_ref (ptrmem, member); |
5156628f | 2941 | } |
50ad9642 | 2942 | |
8d08fdba MS |
2943 | /* Given an expression PTR for a pointer, return an expression |
2944 | for the value pointed to. | |
2945 | ERRORSTRING is the name of the operator to appear in error messages. | |
2946 | ||
2947 | This function may need to overload OPERATOR_FNNAME. | |
2948 | Must also handle REFERENCE_TYPEs for C++. */ | |
2949 | ||
2950 | tree | |
4fe977f2 | 2951 | build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring, |
5ade1ed2 | 2952 | tsubst_flags_t complain) |
8d08fdba | 2953 | { |
d17811fd | 2954 | tree orig_expr = expr; |
5566b478 | 2955 | tree rval; |
fcb9363e | 2956 | tree overload = NULL_TREE; |
5566b478 | 2957 | |
5156628f | 2958 | if (processing_template_decl) |
d17811fd | 2959 | { |
d84572a4 | 2960 | /* Retain the type if we know the operand is a pointer. */ |
ccafc19b JM |
2961 | if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr))) |
2962 | return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr); | |
d17811fd | 2963 | if (type_dependent_expression_p (expr)) |
f330f599 | 2964 | return build_min_nt_loc (loc, INDIRECT_REF, expr); |
d17811fd MM |
2965 | expr = build_non_dependent_expr (expr); |
2966 | } | |
5566b478 | 2967 | |
4fe977f2 | 2968 | rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr, |
fcb9363e | 2969 | NULL_TREE, NULL_TREE, &overload, complain); |
d17811fd | 2970 | if (!rval) |
5ade1ed2 | 2971 | rval = cp_build_indirect_ref (expr, errorstring, complain); |
d17811fd MM |
2972 | |
2973 | if (processing_template_decl && rval != error_mark_node) | |
fcb9363e PP |
2974 | { |
2975 | if (overload != NULL_TREE) | |
2976 | return (build_min_non_dep_op_overload | |
2977 | (INDIRECT_REF, rval, overload, orig_expr)); | |
2978 | ||
2979 | return build_min_non_dep (INDIRECT_REF, rval, orig_expr); | |
2980 | } | |
d17811fd | 2981 | else |
8d08fdba | 2982 | return rval; |
8d08fdba MS |
2983 | } |
2984 | ||
5ade1ed2 | 2985 | /* Helper function called from c-common. */ |
8d08fdba | 2986 | tree |
12308bc6 | 2987 | build_indirect_ref (location_t /*loc*/, |
dd865ef6 | 2988 | tree ptr, ref_operator errorstring) |
5ade1ed2 DG |
2989 | { |
2990 | return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error); | |
2991 | } | |
2992 | ||
2993 | tree | |
dd865ef6 | 2994 | cp_build_indirect_ref (tree ptr, ref_operator errorstring, |
5ade1ed2 | 2995 | tsubst_flags_t complain) |
8d08fdba | 2996 | { |
926ce8bd | 2997 | tree pointer, type; |
c11b6f21 | 2998 | |
374b2837 JM |
2999 | if (ptr == current_class_ptr |
3000 | || (TREE_CODE (ptr) == NOP_EXPR | |
3001 | && TREE_OPERAND (ptr, 0) == current_class_ptr | |
3002 | && (same_type_ignoring_top_level_qualifiers_p | |
3003 | (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr))))) | |
90e734a8 JM |
3004 | return current_class_ref; |
3005 | ||
c11b6f21 | 3006 | pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE |
89fcabaf PC |
3007 | ? ptr : decay_conversion (ptr, complain)); |
3008 | if (pointer == error_mark_node) | |
3009 | return error_mark_node; | |
3010 | ||
c11b6f21 | 3011 | type = TREE_TYPE (pointer); |
8d08fdba | 3012 | |
db24eb1f | 3013 | if (POINTER_TYPE_P (type)) |
8d08fdba | 3014 | { |
01240200 | 3015 | /* [expr.unary.op] |
c8094d83 | 3016 | |
01240200 | 3017 | If the type of the expression is "pointer to T," the type |
cd41d410 DS |
3018 | of the result is "T." */ |
3019 | tree t = TREE_TYPE (type); | |
01240200 | 3020 | |
5ae98e82 RB |
3021 | if ((CONVERT_EXPR_P (ptr) |
3022 | || TREE_CODE (ptr) == VIEW_CONVERT_EXPR) | |
3023 | && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t))) | |
79bedddc SR |
3024 | { |
3025 | /* If a warning is issued, mark it to avoid duplicates from | |
3026 | the backend. This only needs to be done at | |
3027 | warn_strict_aliasing > 2. */ | |
3028 | if (warn_strict_aliasing > 2) | |
3029 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)), | |
3030 | type, TREE_OPERAND (ptr, 0))) | |
3031 | TREE_NO_WARNING (ptr) = 1; | |
3032 | } | |
3033 | ||
b72801e2 | 3034 | if (VOID_TYPE_P (t)) |
0cbd7506 MS |
3035 | { |
3036 | /* A pointer to incomplete type (other than cv void) can be | |
3037 | dereferenced [expr.unary.op]/1 */ | |
5ade1ed2 DG |
3038 | if (complain & tf_error) |
3039 | error ("%qT is not a pointer-to-object type", type); | |
0cbd7506 MS |
3040 | return error_mark_node; |
3041 | } | |
a9183fef | 3042 | else if (TREE_CODE (pointer) == ADDR_EXPR |
688f6688 | 3043 | && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0)))) |
01240200 MM |
3044 | /* The POINTER was something like `&x'. We simplify `*&x' to |
3045 | `x'. */ | |
8d08fdba MS |
3046 | return TREE_OPERAND (pointer, 0); |
3047 | else | |
3048 | { | |
01240200 | 3049 | tree ref = build1 (INDIRECT_REF, t, pointer); |
8d08fdba | 3050 | |
b0d75c1e BK |
3051 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, |
3052 | so that we get the proper error message if the result is used | |
3053 | to assign to. Also, &* is supposed to be a no-op. */ | |
91063b51 MM |
3054 | TREE_READONLY (ref) = CP_TYPE_CONST_P (t); |
3055 | TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t); | |
8d08fdba | 3056 | TREE_SIDE_EFFECTS (ref) |
271bd540 | 3057 | = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)); |
8d08fdba MS |
3058 | return ref; |
3059 | } | |
3060 | } | |
5ade1ed2 DG |
3061 | else if (!(complain & tf_error)) |
3062 | /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */ | |
3063 | ; | |
8d08fdba MS |
3064 | /* `pointer' won't be an error_mark_node if we were given a |
3065 | pointer to member, so it's cool to check for this here. */ | |
66b1156a | 3066 | else if (TYPE_PTRMEM_P (type)) |
dd865ef6 SZ |
3067 | switch (errorstring) |
3068 | { | |
3069 | case RO_ARRAY_INDEXING: | |
3070 | error ("invalid use of array indexing on pointer to member"); | |
3071 | break; | |
3072 | case RO_UNARY_STAR: | |
3073 | error ("invalid use of unary %<*%> on pointer to member"); | |
3074 | break; | |
3075 | case RO_IMPLICIT_CONVERSION: | |
3076 | error ("invalid use of implicit conversion on pointer to member"); | |
3077 | break; | |
5e54f81d MG |
3078 | case RO_ARROW_STAR: |
3079 | error ("left hand operand of %<->*%> must be a pointer to class, " | |
3080 | "but is a pointer to member of type %qT", type); | |
3081 | break; | |
dd865ef6 SZ |
3082 | default: |
3083 | gcc_unreachable (); | |
3084 | } | |
8d08fdba | 3085 | else if (pointer != error_mark_node) |
7a6daeb0 NF |
3086 | invalid_indirection_error (input_location, type, errorstring); |
3087 | ||
8d08fdba MS |
3088 | return error_mark_node; |
3089 | } | |
3090 | ||
3091 | /* This handles expressions of the form "a[i]", which denotes | |
3092 | an array reference. | |
3093 | ||
3094 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
3095 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
3096 | This avoids forcing the array out of registers, and can work on | |
3097 | arrays that are not lvalues (for example, members of structures returned | |
3098 | by functions). | |
3099 | ||
3100 | If INDEX is of some user-defined type, it must be converted to | |
3101 | integer type. Otherwise, to make a compatible PLUS_EXPR, it | |
a63068b6 AH |
3102 | will inherit the type of the array, which will be some pointer type. |
3103 | ||
3104 | LOC is the location to use in building the array reference. */ | |
8d08fdba | 3105 | |
8d08fdba | 3106 | tree |
3a11c665 JM |
3107 | cp_build_array_ref (location_t loc, tree array, tree idx, |
3108 | tsubst_flags_t complain) | |
8d08fdba | 3109 | { |
a63068b6 AH |
3110 | tree ret; |
3111 | ||
8d08fdba MS |
3112 | if (idx == 0) |
3113 | { | |
73647d75 JM |
3114 | if (complain & tf_error) |
3115 | error_at (loc, "subscript missing in array reference"); | |
8d08fdba MS |
3116 | return error_mark_node; |
3117 | } | |
3118 | ||
2ce86d2e BI |
3119 | /* If an array's index is an array notation, then its rank cannot be |
3120 | greater than one. */ | |
b72271b9 | 3121 | if (flag_cilkplus && contains_array_notation_expr (idx)) |
2ce86d2e BI |
3122 | { |
3123 | size_t rank = 0; | |
3124 | ||
3125 | /* If find_rank returns false, then it should have reported an error, | |
3126 | thus it is unnecessary for repetition. */ | |
3127 | if (!find_rank (loc, idx, idx, true, &rank)) | |
3128 | return error_mark_node; | |
3129 | if (rank > 1) | |
3130 | { | |
3131 | error_at (loc, "rank of the array%'s index is greater than 1"); | |
3132 | return error_mark_node; | |
3133 | } | |
3134 | } | |
8d08fdba MS |
3135 | if (TREE_TYPE (array) == error_mark_node |
3136 | || TREE_TYPE (idx) == error_mark_node) | |
3137 | return error_mark_node; | |
3138 | ||
0450d74d RH |
3139 | /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference |
3140 | inside it. */ | |
3141 | switch (TREE_CODE (array)) | |
3142 | { | |
3143 | case COMPOUND_EXPR: | |
3144 | { | |
3a11c665 JM |
3145 | tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx, |
3146 | complain); | |
a63068b6 AH |
3147 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (value), |
3148 | TREE_OPERAND (array, 0), value); | |
3149 | SET_EXPR_LOCATION (ret, loc); | |
3150 | return ret; | |
0450d74d RH |
3151 | } |
3152 | ||
3153 | case COND_EXPR: | |
a63068b6 | 3154 | ret = build_conditional_expr |
4cbc4bd7 | 3155 | (loc, TREE_OPERAND (array, 0), |
3a11c665 JM |
3156 | cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx, |
3157 | complain), | |
3158 | cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx, | |
3159 | complain), | |
a3299120 | 3160 | complain); |
8aff539b | 3161 | protected_set_expr_location (ret, loc); |
a63068b6 | 3162 | return ret; |
0450d74d RH |
3163 | |
3164 | default: | |
3165 | break; | |
3166 | } | |
3167 | ||
f17a223d | 3168 | bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx); |
7edaa4d2 | 3169 | |
6de9cd9a | 3170 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) |
8d08fdba | 3171 | { |
e4d35515 | 3172 | tree rval, type; |
8d08fdba | 3173 | |
5bd012f8 | 3174 | warn_array_subscript_with_type_char (loc, idx); |
8d08fdba | 3175 | |
adf2edec | 3176 | if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx))) |
8d08fdba | 3177 | { |
73647d75 JM |
3178 | if (complain & tf_error) |
3179 | error_at (loc, "array subscript is not an integer"); | |
8d08fdba MS |
3180 | return error_mark_node; |
3181 | } | |
3182 | ||
0a72704b MM |
3183 | /* Apply integral promotions *after* noticing character types. |
3184 | (It is unclear why we do these promotions -- the standard | |
6ccf2f7d | 3185 | does not say that we should. In fact, the natural thing would |
0a72704b MM |
3186 | seem to be to convert IDX to ptrdiff_t; we're performing |
3187 | pointer arithmetic.) */ | |
4b978f96 | 3188 | idx = cp_perform_integral_promotions (idx, complain); |
0a72704b | 3189 | |
8d08fdba MS |
3190 | /* An array that is indexed by a non-constant |
3191 | cannot be stored in a register; we must be able to do | |
3192 | address arithmetic on its address. | |
3193 | Likewise an array of elements of variable size. */ | |
3194 | if (TREE_CODE (idx) != INTEGER_CST | |
d0f062fb | 3195 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
3c215895 JM |
3196 | && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) |
3197 | != INTEGER_CST))) | |
8d08fdba | 3198 | { |
dffd7eb6 | 3199 | if (!cxx_mark_addressable (array)) |
8d08fdba MS |
3200 | return error_mark_node; |
3201 | } | |
0450d74d | 3202 | |
8d08fdba MS |
3203 | /* An array that is indexed by a constant value which is not within |
3204 | the array bounds cannot be stored in a register either; because we | |
3205 | would get a crash in store_bit_field/extract_bit_field when trying | |
3206 | to access a non-existent part of the register. */ | |
3207 | if (TREE_CODE (idx) == INTEGER_CST | |
eb34af89 RK |
3208 | && TYPE_DOMAIN (TREE_TYPE (array)) |
3209 | && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array)))) | |
8d08fdba | 3210 | { |
dffd7eb6 | 3211 | if (!cxx_mark_addressable (array)) |
8d08fdba MS |
3212 | return error_mark_node; |
3213 | } | |
3214 | ||
b7484fbe MS |
3215 | /* Note in C++ it is valid to subscript a `register' array, since |
3216 | it is valid to take the address of something with that | |
3217 | storage specification. */ | |
3218 | if (extra_warnings) | |
8d08fdba MS |
3219 | { |
3220 | tree foo = array; | |
3221 | while (TREE_CODE (foo) == COMPONENT_REF) | |
3222 | foo = TREE_OPERAND (foo, 0); | |
5a6ccc94 | 3223 | if (VAR_P (foo) && DECL_REGISTER (foo) |
73647d75 | 3224 | && (complain & tf_warning)) |
a63068b6 AH |
3225 | warning_at (loc, OPT_Wextra, |
3226 | "subscripting array declared %<register%>"); | |
8d08fdba MS |
3227 | } |
3228 | ||
01240200 | 3229 | type = TREE_TYPE (TREE_TYPE (array)); |
f293ce4b | 3230 | rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE); |
8d08fdba MS |
3231 | /* Array ref is const/volatile if the array elements are |
3232 | or if the array is.. */ | |
3233 | TREE_READONLY (rval) | |
91063b51 | 3234 | |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array)); |
8d08fdba | 3235 | TREE_SIDE_EFFECTS (rval) |
91063b51 | 3236 | |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array)); |
8d08fdba | 3237 | TREE_THIS_VOLATILE (rval) |
91063b51 | 3238 | |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array)); |
cda0a029 | 3239 | ret = require_complete_type_sfinae (rval, complain); |
8aff539b | 3240 | protected_set_expr_location (ret, loc); |
aa7da51a JJ |
3241 | if (non_lvalue) |
3242 | ret = non_lvalue_loc (loc, ret); | |
a63068b6 | 3243 | return ret; |
8d08fdba MS |
3244 | } |
3245 | ||
3246 | { | |
89fcabaf PC |
3247 | tree ar = cp_default_conversion (array, complain); |
3248 | tree ind = cp_default_conversion (idx, complain); | |
8d08fdba MS |
3249 | |
3250 | /* Put the integer in IND to simplify error checking. */ | |
3251 | if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) | |
6b4db501 | 3252 | std::swap (ar, ind); |
8d08fdba | 3253 | |
89fcabaf PC |
3254 | if (ar == error_mark_node || ind == error_mark_node) |
3255 | return error_mark_node; | |
8d08fdba | 3256 | |
50e10fa8 | 3257 | if (!TYPE_PTR_P (TREE_TYPE (ar))) |
8d08fdba | 3258 | { |
73647d75 JM |
3259 | if (complain & tf_error) |
3260 | error_at (loc, "subscripted value is neither array nor pointer"); | |
8d08fdba MS |
3261 | return error_mark_node; |
3262 | } | |
3263 | if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) | |
3264 | { | |
73647d75 JM |
3265 | if (complain & tf_error) |
3266 | error_at (loc, "array subscript is not an integer"); | |
8d08fdba MS |
3267 | return error_mark_node; |
3268 | } | |
3269 | ||
5bd012f8 | 3270 | warn_array_subscript_with_type_char (loc, idx); |
0958b0d3 | 3271 | |
ba47d38d AH |
3272 | ret = cp_build_indirect_ref (cp_build_binary_op (input_location, |
3273 | PLUS_EXPR, ar, ind, | |
73647d75 | 3274 | complain), |
dd865ef6 | 3275 | RO_ARRAY_INDEXING, |
73647d75 | 3276 | complain); |
a63068b6 | 3277 | protected_set_expr_location (ret, loc); |
aa7da51a JJ |
3278 | if (non_lvalue) |
3279 | ret = non_lvalue_loc (loc, ret); | |
a63068b6 | 3280 | return ret; |
8d08fdba MS |
3281 | } |
3282 | } | |
3a11c665 JM |
3283 | |
3284 | /* Entry point for Obj-C++. */ | |
3285 | ||
3286 | tree | |
3287 | build_array_ref (location_t loc, tree array, tree idx) | |
3288 | { | |
3289 | return cp_build_array_ref (loc, array, idx, tf_warning_or_error); | |
3290 | } | |
8d08fdba | 3291 | \f |
8d08fdba | 3292 | /* Resolve a pointer to member function. INSTANCE is the object |
c87978aa JM |
3293 | instance to use, if the member points to a virtual member. |
3294 | ||
3295 | This used to avoid checking for virtual functions if basetype | |
3296 | has no virtual functions, according to an earlier ANSI draft. | |
3297 | With the final ISO C++ rules, such an optimization is | |
3298 | incorrect: A pointer to a derived member can be static_cast | |
3299 | to pointer-to-base-member, as long as the dynamic object | |
e18724aa JM |
3300 | later has the right member. So now we only do this optimization |
3301 | when we know the dynamic type of the object. */ | |
8d08fdba MS |
3302 | |
3303 | tree | |
89fcabaf PC |
3304 | get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function, |
3305 | tsubst_flags_t complain) | |
8d08fdba MS |
3306 | { |
3307 | if (TREE_CODE (function) == OFFSET_REF) | |
338d90b8 | 3308 | function = TREE_OPERAND (function, 1); |
8d08fdba MS |
3309 | |
3310 | if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function))) | |
3311 | { | |
e18724aa JM |
3312 | tree idx, delta, e1, e2, e3, vtbl; |
3313 | bool nonvirtual; | |
c87978aa | 3314 | tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)); |
e18724aa | 3315 | tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)); |
f30432d7 | 3316 | |
b7484fbe | 3317 | tree instance_ptr = *instance_ptrptr; |
1c83ea9f | 3318 | tree instance_save_expr = 0; |
c87978aa | 3319 | if (instance_ptr == error_mark_node) |
32facac8 | 3320 | { |
c87978aa JM |
3321 | if (TREE_CODE (function) == PTRMEM_CST) |
3322 | { | |
3323 | /* Extracting the function address from a pmf is only | |
3324 | allowed with -Wno-pmf-conversions. It only works for | |
c6002625 | 3325 | pmf constants. */ |
89fcabaf | 3326 | e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain); |
c87978aa JM |
3327 | e1 = convert (fntype, e1); |
3328 | return e1; | |
3329 | } | |
3330 | else | |
3331 | { | |
89fcabaf PC |
3332 | if (complain & tf_error) |
3333 | error ("object missing in use of %qE", function); | |
c87978aa JM |
3334 | return error_mark_node; |
3335 | } | |
32facac8 ML |
3336 | } |
3337 | ||
e18724aa JM |
3338 | /* True if we know that the dynamic type of the object doesn't have |
3339 | virtual functions, so we can assume the PFN field is a pointer. */ | |
3340 | nonvirtual = (COMPLETE_TYPE_P (basetype) | |
3341 | && !TYPE_POLYMORPHIC_P (basetype) | |
3342 | && resolves_to_fixed_type_p (instance_ptr, 0)); | |
3343 | ||
f9873232 JM |
3344 | /* If we don't really have an object (i.e. in an ill-formed |
3345 | conversion from PMF to pointer), we can't resolve virtual | |
3346 | functions anyway. */ | |
3347 | if (!nonvirtual && is_dummy_object (instance_ptr)) | |
3348 | nonvirtual = true; | |
3349 | ||
b7484fbe | 3350 | if (TREE_SIDE_EFFECTS (instance_ptr)) |
1c83ea9f | 3351 | instance_ptr = instance_save_expr = save_expr (instance_ptr); |
b7484fbe | 3352 | |
f30432d7 MS |
3353 | if (TREE_SIDE_EFFECTS (function)) |
3354 | function = save_expr (function); | |
3355 | ||
c87978aa | 3356 | /* Start by extracting all the information from the PMF itself. */ |
10b2bcdd | 3357 | e3 = pfn_from_ptrmemfunc (function); |
a298680c | 3358 | delta = delta_from_ptrmemfunc (function); |
85b7def6 | 3359 | idx = build1 (NOP_EXPR, vtable_index_type, e3); |
b5fbde92 | 3360 | switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) |
cb7fdde2 | 3361 | { |
ec043522 | 3362 | int flag_sanitize_save; |
cb7fdde2 | 3363 | case ptrmemfunc_vbit_in_pfn: |
ba47d38d AH |
3364 | e1 = cp_build_binary_op (input_location, |
3365 | BIT_AND_EXPR, idx, integer_one_node, | |
89fcabaf | 3366 | complain); |
ba47d38d AH |
3367 | idx = cp_build_binary_op (input_location, |
3368 | MINUS_EXPR, idx, integer_one_node, | |
89fcabaf PC |
3369 | complain); |
3370 | if (idx == error_mark_node) | |
3371 | return error_mark_node; | |
cb7fdde2 AO |
3372 | break; |
3373 | ||
3374 | case ptrmemfunc_vbit_in_delta: | |
ba47d38d AH |
3375 | e1 = cp_build_binary_op (input_location, |
3376 | BIT_AND_EXPR, delta, integer_one_node, | |
89fcabaf | 3377 | complain); |
ec043522 MP |
3378 | /* Don't instrument the RSHIFT_EXPR we're about to create because |
3379 | we're going to use DELTA number of times, and that wouldn't play | |
3380 | well with SAVE_EXPRs therein. */ | |
3381 | flag_sanitize_save = flag_sanitize; | |
3382 | flag_sanitize = 0; | |
ba47d38d AH |
3383 | delta = cp_build_binary_op (input_location, |
3384 | RSHIFT_EXPR, delta, integer_one_node, | |
89fcabaf | 3385 | complain); |
ec043522 | 3386 | flag_sanitize = flag_sanitize_save; |
89fcabaf PC |
3387 | if (delta == error_mark_node) |
3388 | return error_mark_node; | |
cb7fdde2 AO |
3389 | break; |
3390 | ||
3391 | default: | |
315fb5db | 3392 | gcc_unreachable (); |
cb7fdde2 | 3393 | } |
03b256e4 | 3394 | |
89fcabaf PC |
3395 | if (e1 == error_mark_node) |
3396 | return error_mark_node; | |
3397 | ||
81ae598b MM |
3398 | /* Convert down to the right base before using the instance. A |
3399 | special case is that in a pointer to member of class C, C may | |
3400 | be incomplete. In that case, the function will of course be | |
3401 | a member of C, and no conversion is required. In fact, | |
3402 | lookup_base will fail in that case, because incomplete | |
c8094d83 | 3403 | classes do not have BINFOs. */ |
c8094d83 | 3404 | if (!same_type_ignoring_top_level_qualifiers_p |
81ae598b MM |
3405 | (basetype, TREE_TYPE (TREE_TYPE (instance_ptr)))) |
3406 | { | |
3407 | basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)), | |
22854930 | 3408 | basetype, ba_check, NULL, complain); |
c8094d83 | 3409 | instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, |
89fcabaf | 3410 | 1, complain); |
81ae598b MM |
3411 | if (instance_ptr == error_mark_node) |
3412 | return error_mark_node; | |
3413 | } | |
c87978aa | 3414 | /* ...and then the delta in the PMF. */ |
5d49b6a7 | 3415 | instance_ptr = fold_build_pointer_plus (instance_ptr, delta); |
c87978aa JM |
3416 | |
3417 | /* Hand back the adjusted 'this' argument to our caller. */ | |
3418 | *instance_ptrptr = instance_ptr; | |
3419 | ||
e18724aa JM |
3420 | if (nonvirtual) |
3421 | /* Now just return the pointer. */ | |
3422 | return e3; | |
3423 | ||
c87978aa JM |
3424 | /* Next extract the vtable pointer from the object. */ |
3425 | vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node), | |
3426 | instance_ptr); | |
89fcabaf PC |
3427 | vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain); |
3428 | if (vtbl == error_mark_node) | |
3429 | return error_mark_node; | |
3430 | ||
c87978aa | 3431 | /* Finally, extract the function pointer from the vtable. */ |
5d49b6a7 | 3432 | e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx); |
89fcabaf PC |
3433 | e2 = cp_build_indirect_ref (e2, RO_NULL, complain); |
3434 | if (e2 == error_mark_node) | |
3435 | return error_mark_node; | |
f63ab951 | 3436 | TREE_CONSTANT (e2) = 1; |
c7e266a6 | 3437 | |
67231816 RH |
3438 | /* When using function descriptors, the address of the |
3439 | vtable entry is treated as a function pointer. */ | |
3440 | if (TARGET_VTABLE_USES_DESCRIPTORS) | |
3441 | e2 = build1 (NOP_EXPR, TREE_TYPE (e2), | |
89fcabaf | 3442 | cp_build_addr_expr (e2, complain)); |
67231816 | 3443 | |
a489b1f0 | 3444 | e2 = fold_convert (TREE_TYPE (e3), e2); |
4cbc4bd7 | 3445 | e1 = build_conditional_expr (input_location, e1, e2, e3, complain); |
89fcabaf PC |
3446 | if (e1 == error_mark_node) |
3447 | return error_mark_node; | |
c8094d83 | 3448 | |
03b256e4 ML |
3449 | /* Make sure this doesn't get evaluated first inside one of the |
3450 | branches of the COND_EXPR. */ | |
1c83ea9f | 3451 | if (instance_save_expr) |
f293ce4b RS |
3452 | e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1), |
3453 | instance_save_expr, e1); | |
03b256e4 | 3454 | |
f49422da | 3455 | function = e1; |
8d08fdba MS |
3456 | } |
3457 | return function; | |
3458 | } | |
3459 | ||
5ade1ed2 | 3460 | /* Used by the C-common bits. */ |
8d08fdba | 3461 | tree |
12308bc6 | 3462 | build_function_call (location_t /*loc*/, |
c2255bc4 | 3463 | tree function, tree params) |
5ade1ed2 DG |
3464 | { |
3465 | return cp_build_function_call (function, params, tf_warning_or_error); | |
3466 | } | |
3467 | ||
bbbbb16a ILT |
3468 | /* Used by the C-common bits. */ |
3469 | tree | |
81e5eca8 | 3470 | build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/, |
9771b263 DN |
3471 | tree function, vec<tree, va_gc> *params, |
3472 | vec<tree, va_gc> * /*origtypes*/) | |
bbbbb16a | 3473 | { |
9771b263 | 3474 | vec<tree, va_gc> *orig_params = params; |
c166b898 ILT |
3475 | tree ret = cp_build_function_call_vec (function, ¶ms, |
3476 | tf_warning_or_error); | |
bbbbb16a | 3477 | |
c166b898 ILT |
3478 | /* cp_build_function_call_vec can reallocate PARAMS by adding |
3479 | default arguments. That should never happen here. Verify | |
3480 | that. */ | |
3481 | gcc_assert (params == orig_params); | |
3482 | ||
3483 | return ret; | |
bbbbb16a ILT |
3484 | } |
3485 | ||
c166b898 ILT |
3486 | /* Build a function call using a tree list of arguments. */ |
3487 | ||
882de214 | 3488 | static tree |
5ade1ed2 | 3489 | cp_build_function_call (tree function, tree params, tsubst_flags_t complain) |
c166b898 | 3490 | { |
9771b263 | 3491 | vec<tree, va_gc> *vec; |
c166b898 ILT |
3492 | tree ret; |
3493 | ||
3494 | vec = make_tree_vector (); | |
3495 | for (; params != NULL_TREE; params = TREE_CHAIN (params)) | |
9771b263 | 3496 | vec_safe_push (vec, TREE_VALUE (params)); |
c166b898 ILT |
3497 | ret = cp_build_function_call_vec (function, &vec, complain); |
3498 | release_tree_vector (vec); | |
3499 | return ret; | |
3500 | } | |
3501 | ||
450f4293 NF |
3502 | /* Build a function call using varargs. */ |
3503 | ||
3504 | tree | |
3505 | cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...) | |
3506 | { | |
9771b263 | 3507 | vec<tree, va_gc> *vec; |
450f4293 NF |
3508 | va_list args; |
3509 | tree ret, t; | |
3510 | ||
3511 | vec = make_tree_vector (); | |
3512 | va_start (args, complain); | |
3513 | for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree)) | |
9771b263 | 3514 | vec_safe_push (vec, t); |
450f4293 NF |
3515 | va_end (args); |
3516 | ret = cp_build_function_call_vec (function, &vec, complain); | |
3517 | release_tree_vector (vec); | |
3518 | return ret; | |
3519 | } | |
3520 | ||
c166b898 ILT |
3521 | /* Build a function call using a vector of arguments. PARAMS may be |
3522 | NULL if there are no parameters. This changes the contents of | |
3523 | PARAMS. */ | |
3524 | ||
3525 | tree | |
9771b263 | 3526 | cp_build_function_call_vec (tree function, vec<tree, va_gc> **params, |
c166b898 | 3527 | tsubst_flags_t complain) |
8d08fdba | 3528 | { |
926ce8bd | 3529 | tree fntype, fndecl; |
8d08fdba | 3530 | int is_method; |
2e4cf9dc | 3531 | tree original = function; |
c166b898 | 3532 | int nargs; |
94a0dd7b SL |
3533 | tree *argarray; |
3534 | tree parm_types; | |
9771b263 | 3535 | vec<tree, va_gc> *allocated = NULL; |
c166b898 | 3536 | tree ret; |
8d08fdba | 3537 | |
e58a9aa1 ZL |
3538 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
3539 | expressions, like those used for ObjC messenger dispatches. */ | |
9771b263 DN |
3540 | if (params != NULL && !vec_safe_is_empty (*params)) |
3541 | function = objc_rewrite_function_call (function, (**params)[0]); | |
e58a9aa1 | 3542 | |
8d08fdba MS |
3543 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
3544 | Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */ | |
3545 | if (TREE_CODE (function) == NOP_EXPR | |
3546 | && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0))) | |
3547 | function = TREE_OPERAND (function, 0); | |
3548 | ||
3549 | if (TREE_CODE (function) == FUNCTION_DECL) | |
3550 | { | |
971e17ff AS |
3551 | /* If the function is a non-template member function |
3552 | or a non-template friend, then we need to check the | |
3553 | constraints. | |
3554 | ||
3555 | Note that if overload resolution failed with a single | |
3556 | candidate this function will be used to explicitly diagnose | |
3557 | the failure for the single call expression. The check is | |
3558 | technically redundant since we also would have failed in | |
3559 | add_function_candidate. */ | |
3560 | if (flag_concepts | |
3561 | && (complain & tf_error) | |
3562 | && !constraints_satisfied_p (function)) | |
3563 | { | |
3564 | error ("cannot call function %qD", function); | |
3565 | location_t loc = DECL_SOURCE_LOCATION (function); | |
3566 | diagnose_constraints (loc, function, NULL_TREE); | |
3567 | return error_mark_node; | |
3568 | } | |
3569 | ||
9f635aba PC |
3570 | if (!mark_used (function, complain) && !(complain & tf_error)) |
3571 | return error_mark_node; | |
8d08fdba MS |
3572 | fndecl = function; |
3573 | ||
3574 | /* Convert anything with function type to a pointer-to-function. */ | |
aa0a659c PC |
3575 | if (DECL_MAIN_P (function)) |
3576 | { | |
3577 | if (complain & tf_error) | |
3578 | pedwarn (input_location, OPT_Wpedantic, | |
3579 | "ISO C++ forbids calling %<::main%> from within program"); | |
3580 | else | |
3581 | return error_mark_node; | |
3582 | } | |
89fcabaf | 3583 | function = build_addr_func (function, complain); |
8d08fdba MS |
3584 | } |
3585 | else | |
3586 | { | |
3587 | fndecl = NULL_TREE; | |
3588 | ||
89fcabaf | 3589 | function = build_addr_func (function, complain); |
8d08fdba MS |
3590 | } |
3591 | ||
4ac14744 MS |
3592 | if (function == error_mark_node) |
3593 | return error_mark_node; | |
3594 | ||
8d08fdba MS |
3595 | fntype = TREE_TYPE (function); |
3596 | ||
3597 | if (TYPE_PTRMEMFUNC_P (fntype)) | |
3598 | { | |
5ade1ed2 DG |
3599 | if (complain & tf_error) |
3600 | error ("must use %<.*%> or %<->*%> to call pointer-to-member " | |
7b98a725 JW |
3601 | "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>", |
3602 | original, original); | |
4ac14744 | 3603 | return error_mark_node; |
8d08fdba MS |
3604 | } |
3605 | ||
50e10fa8 | 3606 | is_method = (TYPE_PTR_P (fntype) |
8d08fdba MS |
3607 | && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE); |
3608 | ||
50e10fa8 | 3609 | if (!(TYPE_PTRFN_P (fntype) |
386b8a85 JM |
3610 | || is_method |
3611 | || TREE_CODE (function) == TEMPLATE_ID_EXPR)) | |
8d08fdba | 3612 | { |
5ade1ed2 | 3613 | if (complain & tf_error) |
ae52741c MLI |
3614 | { |
3615 | if (!flag_diagnostics_show_caret) | |
3616 | error_at (input_location, | |
3617 | "%qE cannot be used as a function", original); | |
3618 | else if (DECL_P (original)) | |
3619 | error_at (input_location, | |
3620 | "%qD cannot be used as a function", original); | |
3621 | else | |
3622 | error_at (input_location, | |
3623 | "expression cannot be used as a function"); | |
3624 | } | |
3625 | ||
8d08fdba MS |
3626 | return error_mark_node; |
3627 | } | |
3628 | ||
3629 | /* fntype now gets the type of function pointed to. */ | |
3630 | fntype = TREE_TYPE (fntype); | |
94a0dd7b SL |
3631 | parm_types = TYPE_ARG_TYPES (fntype); |
3632 | ||
c166b898 ILT |
3633 | if (params == NULL) |
3634 | { | |
3635 | allocated = make_tree_vector (); | |
3636 | params = &allocated; | |
3637 | } | |
3638 | ||
2ce86d2e BI |
3639 | nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL, |
3640 | complain); | |
94a0dd7b | 3641 | if (nargs < 0) |
125e6594 | 3642 | return error_mark_node; |
863adfc0 | 3643 | |
9771b263 | 3644 | argarray = (*params)->address (); |
c166b898 | 3645 | |
dd66b8e8 JM |
3646 | /* Check for errors in format strings and inappropriately |
3647 | null parameters. */ | |
3342fd71 | 3648 | check_function_arguments (input_location, fntype, nargs, argarray); |
8d08fdba | 3649 | |
e2e03032 | 3650 | ret = build_cxx_call (function, nargs, argarray, complain); |
c166b898 ILT |
3651 | |
3652 | if (allocated != NULL) | |
3653 | release_tree_vector (allocated); | |
3654 | ||
3655 | return ret; | |
8d08fdba | 3656 | } |
8d08fdba | 3657 | \f |
8cdea6ab | 3658 | /* Subroutine of convert_arguments. |
09bae928 | 3659 | Print an error message about a wrong number of arguments. */ |
8cdea6ab SZ |
3660 | |
3661 | static void | |
09bae928 | 3662 | error_args_num (location_t loc, tree fndecl, bool too_many_p) |
8cdea6ab SZ |
3663 | { |
3664 | if (fndecl) | |
3665 | { | |
3666 | if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE) | |
3667 | { | |
3668 | if (DECL_NAME (fndecl) == NULL_TREE | |
3669 | || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl))) | |
3670 | error_at (loc, | |
3671 | too_many_p | |
3672 | ? G_("too many arguments to constructor %q#D") | |
3673 | : G_("too few arguments to constructor %q#D"), | |
3674 | fndecl); | |
3675 | else | |
3676 | error_at (loc, | |
3677 | too_many_p | |
3678 | ? G_("too many arguments to member function %q#D") | |
3679 | : G_("too few arguments to member function %q#D"), | |
3680 | fndecl); | |
3681 | } | |
3682 | else | |
3683 | error_at (loc, | |
3684 | too_many_p | |
3685 | ? G_("too many arguments to function %q#D") | |
3686 | : G_("too few arguments to function %q#D"), | |
3687 | fndecl); | |
6b6dd1aa MP |
3688 | if (!DECL_IS_BUILTIN (fndecl)) |
3689 | inform (DECL_SOURCE_LOCATION (fndecl), "declared here"); | |
8cdea6ab SZ |
3690 | } |
3691 | else | |
19dc6d01 NP |
3692 | { |
3693 | if (c_dialect_objc () && objc_message_selector ()) | |
3694 | error_at (loc, | |
3695 | too_many_p | |
3696 | ? G_("too many arguments to method %q#D") | |
3697 | : G_("too few arguments to method %q#D"), | |
3698 | objc_message_selector ()); | |
3699 | else | |
3700 | error_at (loc, too_many_p ? G_("too many arguments to function") | |
3701 | : G_("too few arguments to function")); | |
3702 | } | |
8cdea6ab SZ |
3703 | } |
3704 | ||
c166b898 ILT |
3705 | /* Convert the actual parameter expressions in the list VALUES to the |
3706 | types in the list TYPELIST. The converted expressions are stored | |
3707 | back in the VALUES vector. | |
8d08fdba MS |
3708 | If parmdecls is exhausted, or when an element has NULL as its type, |
3709 | perform the default conversions. | |
3710 | ||
8d08fdba MS |
3711 | NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. |
3712 | ||
3713 | This is also where warnings about wrong number of args are generated. | |
c8094d83 | 3714 | |
94a0dd7b | 3715 | Returns the actual number of arguments processed (which might be less |
c166b898 | 3716 | than the length of the vector), or -1 on error. |
8d08fdba MS |
3717 | |
3718 | In C++, unspecified trailing parameters can be filled in with their | |
3719 | default arguments, if such were specified. Do so here. */ | |
3720 | ||
94a0dd7b | 3721 | static int |
9771b263 | 3722 | convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl, |
c166b898 | 3723 | int flags, tsubst_flags_t complain) |
8d08fdba | 3724 | { |
c166b898 | 3725 | tree typetail; |
c166b898 | 3726 | unsigned int i; |
8d08fdba | 3727 | |
faf5394a MS |
3728 | /* Argument passing is always copy-initialization. */ |
3729 | flags |= LOOKUP_ONLYCONVERTING; | |
3730 | ||
c166b898 | 3731 | for (i = 0, typetail = typelist; |
9771b263 | 3732 | i < vec_safe_length (*values); |
c166b898 | 3733 | i++) |
8d08fdba | 3734 | { |
926ce8bd | 3735 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
9771b263 | 3736 | tree val = (**values)[i]; |
8d08fdba | 3737 | |
6af455d7 | 3738 | if (val == error_mark_node || type == error_mark_node) |
94a0dd7b | 3739 | return -1; |
8ccc31eb | 3740 | |
8d08fdba MS |
3741 | if (type == void_type_node) |
3742 | { | |
5ade1ed2 DG |
3743 | if (complain & tf_error) |
3744 | { | |
09bae928 | 3745 | error_args_num (input_location, fndecl, /*too_many_p=*/true); |
5ade1ed2 DG |
3746 | return i; |
3747 | } | |
3748 | else | |
3749 | return -1; | |
8d08fdba MS |
3750 | } |
3751 | ||
8d08fdba MS |
3752 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
3753 | Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */ | |
3754 | if (TREE_CODE (val) == NOP_EXPR | |
a0a33927 MS |
3755 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)) |
3756 | && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)) | |
8d08fdba MS |
3757 | val = TREE_OPERAND (val, 0); |
3758 | ||
00595019 MS |
3759 | if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE) |
3760 | { | |
3761 | if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE | |
8d08fdba | 3762 | || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE |
00595019 | 3763 | || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE) |
89fcabaf | 3764 | val = decay_conversion (val, complain); |
00595019 | 3765 | } |
8d08fdba MS |
3766 | |
3767 | if (val == error_mark_node) | |
94a0dd7b | 3768 | return -1; |
8d08fdba | 3769 | |
8d08fdba MS |
3770 | if (type != 0) |
3771 | { | |
3772 | /* Formal parm type is specified by a function prototype. */ | |
3773 | tree parmval; | |
3774 | ||
d0f062fb | 3775 | if (!COMPLETE_TYPE_P (complete_type (type))) |
8d08fdba | 3776 | { |
5ade1ed2 DG |
3777 | if (complain & tf_error) |
3778 | { | |
3779 | if (fndecl) | |
3780 | error ("parameter %P of %qD has incomplete type %qT", | |
3781 | i, fndecl, type); | |
3782 | else | |
3783 | error ("parameter %P has incomplete type %qT", i, type); | |
3784 | } | |
3afd2e20 | 3785 | parmval = error_mark_node; |
8d08fdba MS |
3786 | } |
3787 | else | |
3788 | { | |
9a3b49ac | 3789 | parmval = convert_for_initialization |
56ae6d77 | 3790 | (NULL_TREE, type, val, flags, |
2f5b91f5 | 3791 | ICR_ARGPASS, fndecl, i, complain); |
b40e334f | 3792 | parmval = convert_for_arg_passing (type, parmval, complain); |
8d08fdba | 3793 | } |
878cd289 MS |
3794 | |
3795 | if (parmval == error_mark_node) | |
94a0dd7b | 3796 | return -1; |
878cd289 | 3797 | |
9771b263 | 3798 | (**values)[i] = parmval; |
8d08fdba MS |
3799 | } |
3800 | else | |
3801 | { | |
1141ed3f | 3802 | if (fndecl && magic_varargs_p (fndecl)) |
4eaf1d5b | 3803 | /* Don't do ellipsis conversion for __built_in_constant_p |
c32097d8 | 3804 | as this will result in spurious errors for non-trivial |
4eaf1d5b | 3805 | types. */ |
79fe346e | 3806 | val = require_complete_type_sfinae (val, complain); |
4eaf1d5b | 3807 | else |
b40e334f | 3808 | val = convert_arg_to_ellipsis (val, complain); |
4eaf1d5b | 3809 | |
9771b263 | 3810 | (**values)[i] = val; |
8d08fdba MS |
3811 | } |
3812 | ||
8d08fdba MS |
3813 | if (typetail) |
3814 | typetail = TREE_CHAIN (typetail); | |
3815 | } | |
3816 | ||
3817 | if (typetail != 0 && typetail != void_list_node) | |
3818 | { | |
823c22f9 NS |
3819 | /* See if there are default arguments that can be used. Because |
3820 | we hold default arguments in the FUNCTION_TYPE (which is so | |
3821 | wrong), we can see default parameters here from deduced | |
3822 | contexts (and via typeof) for indirect function calls. | |
3823 | Fortunately we know whether we have a function decl to | |
3824 | provide default arguments in a language conformant | |
3825 | manner. */ | |
3826 | if (fndecl && TREE_PURPOSE (typetail) | |
a723baf1 | 3827 | && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG) |
8d08fdba | 3828 | { |
f376e137 | 3829 | for (; typetail != void_list_node; ++i) |
8d08fdba | 3830 | { |
c8094d83 MS |
3831 | tree parmval |
3832 | = convert_default_arg (TREE_VALUE (typetail), | |
3833 | TREE_PURPOSE (typetail), | |
b40e334f | 3834 | fndecl, i, complain); |
8d08fdba | 3835 | |
878cd289 | 3836 | if (parmval == error_mark_node) |
94a0dd7b | 3837 | return -1; |
878cd289 | 3838 | |
9771b263 | 3839 | vec_safe_push (*values, parmval); |
8d08fdba MS |
3840 | typetail = TREE_CHAIN (typetail); |
3841 | /* ends with `...'. */ | |
3842 | if (typetail == NULL_TREE) | |
3843 | break; | |
3844 | } | |
3845 | } | |
3846 | else | |
3847 | { | |
5ade1ed2 | 3848 | if (complain & tf_error) |
09bae928 | 3849 | error_args_num (input_location, fndecl, /*too_many_p=*/false); |
94a0dd7b | 3850 | return -1; |
8d08fdba MS |
3851 | } |
3852 | } | |
8d08fdba | 3853 | |
c166b898 | 3854 | return (int) i; |
8d08fdba MS |
3855 | } |
3856 | \f | |
3857 | /* Build a binary-operation expression, after performing default | |
2a67bec2 ILT |
3858 | conversions on the operands. CODE is the kind of expression to |
3859 | build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE | |
3860 | are the tree codes which correspond to ARG1 and ARG2 when issuing | |
3861 | warnings about possibly misplaced parentheses. They may differ | |
3862 | from the TREE_CODE of ARG1 and ARG2 if the parser has done constant | |
3863 | folding (e.g., if the parser sees "a | 1 + 1", it may call this | |
3864 | routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR). | |
3865 | To avoid issuing any parentheses warnings, pass ARG1_CODE and/or | |
3866 | ARG2_CODE as ERROR_MARK. */ | |
8d08fdba MS |
3867 | |
3868 | tree | |
4fe977f2 PC |
3869 | build_x_binary_op (location_t loc, enum tree_code code, tree arg1, |
3870 | enum tree_code arg1_code, tree arg2, | |
fcb9363e | 3871 | enum tree_code arg2_code, tree *overload_p, |
5ade1ed2 | 3872 | tsubst_flags_t complain) |
8d08fdba | 3873 | { |
d17811fd MM |
3874 | tree orig_arg1; |
3875 | tree orig_arg2; | |
3876 | tree expr; | |
fcb9363e | 3877 | tree overload = NULL_TREE; |
5566b478 | 3878 | |
d17811fd MM |
3879 | orig_arg1 = arg1; |
3880 | orig_arg2 = arg2; | |
d6b4ea85 | 3881 | |
d17811fd | 3882 | if (processing_template_decl) |
14d22dd6 | 3883 | { |
d17811fd MM |
3884 | if (type_dependent_expression_p (arg1) |
3885 | || type_dependent_expression_p (arg2)) | |
f330f599 | 3886 | return build_min_nt_loc (loc, code, arg1, arg2); |
d17811fd MM |
3887 | arg1 = build_non_dependent_expr (arg1); |
3888 | arg2 = build_non_dependent_expr (arg2); | |
3889 | } | |
14d22dd6 | 3890 | |
d17811fd | 3891 | if (code == DOTSTAR_EXPR) |
89fcabaf | 3892 | expr = build_m_component_ref (arg1, arg2, complain); |
d17811fd | 3893 | else |
4fe977f2 | 3894 | expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE, |
fcb9363e PP |
3895 | &overload, complain); |
3896 | ||
3897 | if (overload_p != NULL) | |
3898 | *overload_p = overload; | |
14d22dd6 | 3899 | |
2a67bec2 ILT |
3900 | /* Check for cases such as x+y<<z which users are likely to |
3901 | misinterpret. But don't warn about obj << x + y, since that is a | |
3902 | common idiom for I/O. */ | |
3903 | if (warn_parentheses | |
aa9b7569 | 3904 | && (complain & tf_warning) |
2a67bec2 ILT |
3905 | && !processing_template_decl |
3906 | && !error_operand_p (arg1) | |
3907 | && !error_operand_p (arg2) | |
3908 | && (code != LSHIFT_EXPR | |
3909 | || !CLASS_TYPE_P (TREE_TYPE (arg1)))) | |
5d9de0d0 PC |
3910 | warn_about_parentheses (loc, code, arg1_code, orig_arg1, |
3911 | arg2_code, orig_arg2); | |
2a67bec2 | 3912 | |
d17811fd | 3913 | if (processing_template_decl && expr != error_mark_node) |
fcb9363e PP |
3914 | { |
3915 | if (overload != NULL_TREE) | |
3916 | return (build_min_non_dep_op_overload | |
3917 | (code, expr, overload, orig_arg1, orig_arg2)); | |
3918 | ||
3919 | return build_min_non_dep (code, expr, orig_arg1, orig_arg2); | |
3920 | } | |
c8094d83 | 3921 | |
d17811fd | 3922 | return expr; |
14d22dd6 MM |
3923 | } |
3924 | ||
493e377c PC |
3925 | /* Build and return an ARRAY_REF expression. */ |
3926 | ||
3927 | tree | |
f330f599 PC |
3928 | build_x_array_ref (location_t loc, tree arg1, tree arg2, |
3929 | tsubst_flags_t complain) | |
493e377c PC |
3930 | { |
3931 | tree orig_arg1 = arg1; | |
3932 | tree orig_arg2 = arg2; | |
3933 | tree expr; | |
fcb9363e | 3934 | tree overload = NULL_TREE; |
493e377c PC |
3935 | |
3936 | if (processing_template_decl) | |
3937 | { | |
3938 | if (type_dependent_expression_p (arg1) | |
3939 | || type_dependent_expression_p (arg2)) | |
f330f599 PC |
3940 | return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2, |
3941 | NULL_TREE, NULL_TREE); | |
493e377c PC |
3942 | arg1 = build_non_dependent_expr (arg1); |
3943 | arg2 = build_non_dependent_expr (arg2); | |
3944 | } | |
3945 | ||
f330f599 | 3946 | expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, |
fcb9363e | 3947 | NULL_TREE, &overload, complain); |
493e377c PC |
3948 | |
3949 | if (processing_template_decl && expr != error_mark_node) | |
fcb9363e PP |
3950 | { |
3951 | if (overload != NULL_TREE) | |
3952 | return (build_min_non_dep_op_overload | |
3953 | (ARRAY_REF, expr, overload, orig_arg1, orig_arg2)); | |
3954 | ||
3955 | return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2, | |
3956 | NULL_TREE, NULL_TREE); | |
3957 | } | |
493e377c PC |
3958 | return expr; |
3959 | } | |
3960 | ||
12448f77 ILT |
3961 | /* Return whether OP is an expression of enum type cast to integer |
3962 | type. In C++ even unsigned enum types are cast to signed integer | |
3963 | types. We do not want to issue warnings about comparisons between | |
3964 | signed and unsigned types when one of the types is an enum type. | |
3965 | Those warnings are always false positives in practice. */ | |
3966 | ||
3967 | static bool | |
3968 | enum_cast_to_int (tree op) | |
3969 | { | |
f764304c | 3970 | if (CONVERT_EXPR_P (op) |
12448f77 ILT |
3971 | && TREE_TYPE (op) == integer_type_node |
3972 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE | |
3973 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0)))) | |
3974 | return true; | |
3975 | ||
3976 | /* The cast may have been pushed into a COND_EXPR. */ | |
3977 | if (TREE_CODE (op) == COND_EXPR) | |
3978 | return (enum_cast_to_int (TREE_OPERAND (op, 1)) | |
3979 | || enum_cast_to_int (TREE_OPERAND (op, 2))); | |
3980 | ||
3981 | return false; | |
3982 | } | |
3983 | ||
5ade1ed2 DG |
3984 | /* For the c-common bits. */ |
3985 | tree | |
ba47d38d | 3986 | build_binary_op (location_t location, enum tree_code code, tree op0, tree op1, |
12308bc6 | 3987 | int /*convert_p*/) |
5ade1ed2 | 3988 | { |
ba47d38d | 3989 | return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error); |
5ade1ed2 DG |
3990 | } |
3991 | ||
9f47c7e5 IE |
3992 | /* Build a vector comparison of ARG0 and ARG1 using CODE opcode |
3993 | into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */ | |
3994 | ||
3995 | static tree | |
3996 | build_vec_cmp (tree_code code, tree type, | |
3997 | tree arg0, tree arg1) | |
3998 | { | |
3999 | tree zero_vec = build_zero_cst (type); | |
4000 | tree minus_one_vec = build_minus_one_cst (type); | |
4001 | tree cmp_type = build_same_sized_truth_vector_type(type); | |
4002 | tree cmp = build2 (code, cmp_type, arg0, arg1); | |
9f47c7e5 IE |
4003 | return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec); |
4004 | } | |
5ade1ed2 | 4005 | |
b1970801 MP |
4006 | /* Possibly warn about an address never being NULL. */ |
4007 | ||
4008 | static void | |
4009 | warn_for_null_address (location_t location, tree op, tsubst_flags_t complain) | |
4010 | { | |
4011 | if (!warn_address | |
4012 | || (complain & tf_warning) == 0 | |
4013 | || c_inhibit_evaluation_warnings != 0 | |
4014 | || TREE_NO_WARNING (op)) | |
4015 | return; | |
4016 | ||
4017 | tree cop = fold_non_dependent_expr (op); | |
4018 | ||
4019 | if (TREE_CODE (cop) == ADDR_EXPR | |
4020 | && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0)) | |
4021 | && !TREE_NO_WARNING (cop)) | |
4022 | warning_at (location, OPT_Waddress, "the address of %qD will never " | |
4023 | "be NULL", TREE_OPERAND (cop, 0)); | |
4024 | ||
4025 | if (CONVERT_EXPR_P (op) | |
4026 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE) | |
4027 | { | |
4028 | tree inner_op = op; | |
4029 | STRIP_NOPS (inner_op); | |
4030 | ||
4031 | if (DECL_P (inner_op)) | |
4032 | warning_at (location, OPT_Waddress, | |
4033 | "the compiler can assume that the address of " | |
4034 | "%qD will never be NULL", inner_op); | |
4035 | } | |
4036 | } | |
4037 | ||
8d08fdba MS |
4038 | /* Build a binary-operation expression without default conversions. |
4039 | CODE is the kind of expression to build. | |
ba47d38d | 4040 | LOCATION is the location_t of the operator in the source code. |
8d08fdba MS |
4041 | This function differs from `build' in several ways: |
4042 | the data type of the result is computed and recorded in it, | |
4043 | warnings are generated if arg data types are invalid, | |
4044 | special handling for addition and subtraction of pointers is known, | |
4045 | and some optimization is done (operations on narrow ints | |
4046 | are done in the narrower type when that gives the same result). | |
4047 | Constant folding is also done before the result is returned. | |
4048 | ||
8d08fdba MS |
4049 | Note that the operands will never have enumeral types |
4050 | because either they have just had the default conversions performed | |
4051 | or they have both just been converted to some other type in which | |
4052 | the arithmetic is to be done. | |
4053 | ||
4054 | C++: must do special pointer arithmetic when implementing | |
4055 | multiple inheritance, and deal with pointer to member functions. */ | |
4056 | ||
4057 | tree | |
ba47d38d AH |
4058 | cp_build_binary_op (location_t location, |
4059 | enum tree_code code, tree orig_op0, tree orig_op1, | |
5ade1ed2 | 4060 | tsubst_flags_t complain) |
8d08fdba | 4061 | { |
39211cd5 | 4062 | tree op0, op1; |
926ce8bd | 4063 | enum tree_code code0, code1; |
39211cd5 | 4064 | tree type0, type1; |
4de67c26 | 4065 | const char *invalid_op_diag; |
8d08fdba MS |
4066 | |
4067 | /* Expression code to give to the expression when it is built. | |
4068 | Normally this is CODE, which is what the caller asked for, | |
4069 | but in some special cases we change it. */ | |
926ce8bd | 4070 | enum tree_code resultcode = code; |
8d08fdba MS |
4071 | |
4072 | /* Data type in which the computation is to be performed. | |
4073 | In the simplest cases this is the common type of the arguments. */ | |
926ce8bd | 4074 | tree result_type = NULL; |
8d08fdba MS |
4075 | |
4076 | /* Nonzero means operands have already been type-converted | |
4077 | in whatever way is necessary. | |
4078 | Zero means they need to be converted to RESULT_TYPE. */ | |
4079 | int converted = 0; | |
4080 | ||
28cbf42c MS |
4081 | /* Nonzero means create the expression with this type, rather than |
4082 | RESULT_TYPE. */ | |
4083 | tree build_type = 0; | |
4084 | ||
8d08fdba | 4085 | /* Nonzero means after finally constructing the expression |
faae18ab | 4086 | convert it to this type. */ |
8d08fdba MS |
4087 | tree final_type = 0; |
4088 | ||
cda0a029 | 4089 | tree result, result_ovl; |
de5a5fa1 | 4090 | tree orig_type = NULL; |
455f19cb | 4091 | |
8d08fdba MS |
4092 | /* Nonzero if this is an operation like MIN or MAX which can |
4093 | safely be computed in short if both args are promoted shorts. | |
4094 | Also implies COMMON. | |
4095 | -1 indicates a bitwise operation; this makes a difference | |
4096 | in the exact conditions for when it is safe to do the operation | |
4097 | in a narrower mode. */ | |
4098 | int shorten = 0; | |
4099 | ||
4100 | /* Nonzero if this is a comparison operation; | |
4101 | if both args are promoted shorts, compare the original shorts. | |
4102 | Also implies COMMON. */ | |
4103 | int short_compare = 0; | |
4104 | ||
8d08fdba MS |
4105 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ |
4106 | int common = 0; | |
4107 | ||
455f19cb MM |
4108 | /* True if both operands have arithmetic type. */ |
4109 | bool arithmetic_types_p; | |
4110 | ||
39211cd5 | 4111 | /* Apply default conversions. */ |
40260429 NS |
4112 | op0 = orig_op0; |
4113 | op1 = orig_op1; | |
c8094d83 | 4114 | |
de5a5fa1 MP |
4115 | /* Remember whether we're doing / or %. */ |
4116 | bool doing_div_or_mod = false; | |
4117 | ||
4118 | /* Remember whether we're doing << or >>. */ | |
4119 | bool doing_shift = false; | |
4120 | ||
4121 | /* Tree holding instrumentation expression. */ | |
4122 | tree instrument_expr = NULL; | |
4123 | ||
a9aedbc2 MS |
4124 | if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR |
4125 | || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR | |
4126 | || code == TRUTH_XOR_EXPR) | |
4127 | { | |
a66e8081 | 4128 | if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0))) |
89fcabaf | 4129 | op0 = decay_conversion (op0, complain); |
a66e8081 | 4130 | if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1))) |
89fcabaf | 4131 | op1 = decay_conversion (op1, complain); |
a9aedbc2 MS |
4132 | } |
4133 | else | |
4134 | { | |
a66e8081 | 4135 | if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0))) |
89fcabaf | 4136 | op0 = cp_default_conversion (op0, complain); |
a66e8081 | 4137 | if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1))) |
89fcabaf | 4138 | op1 = cp_default_conversion (op1, complain); |
a9aedbc2 | 4139 | } |
39211cd5 | 4140 | |
a359be75 JM |
4141 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
4142 | STRIP_TYPE_NOPS (op0); | |
4143 | STRIP_TYPE_NOPS (op1); | |
4144 | ||
4145 | /* DTRT if one side is an overloaded function, but complain about it. */ | |
4146 | if (type_unknown_p (op0)) | |
4147 | { | |
c2ea3a40 | 4148 | tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none); |
a359be75 JM |
4149 | if (t != error_mark_node) |
4150 | { | |
5ade1ed2 | 4151 | if (complain & tf_error) |
cbe5f3b3 | 4152 | permerror (input_location, "assuming cast to type %qT from overloaded function", |
37ec60ed | 4153 | TREE_TYPE (t)); |
a359be75 JM |
4154 | op0 = t; |
4155 | } | |
4156 | } | |
4157 | if (type_unknown_p (op1)) | |
4158 | { | |
c2ea3a40 | 4159 | tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none); |
a359be75 JM |
4160 | if (t != error_mark_node) |
4161 | { | |
5ade1ed2 | 4162 | if (complain & tf_error) |
cbe5f3b3 | 4163 | permerror (input_location, "assuming cast to type %qT from overloaded function", |
37ec60ed | 4164 | TREE_TYPE (t)); |
a359be75 JM |
4165 | op1 = t; |
4166 | } | |
4167 | } | |
4168 | ||
1141ed3f BI |
4169 | type0 = TREE_TYPE (op0); |
4170 | type1 = TREE_TYPE (op1); | |
39211cd5 MS |
4171 | |
4172 | /* The expression codes of the data types of the arguments tell us | |
4173 | whether the arguments are integers, floating, pointers, etc. */ | |
4174 | code0 = TREE_CODE (type0); | |
4175 | code1 = TREE_CODE (type1); | |
4176 | ||
8d08fdba MS |
4177 | /* If an error was already reported for one of the arguments, |
4178 | avoid reporting another error. */ | |
8d08fdba MS |
4179 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
4180 | return error_mark_node; | |
4181 | ||
4de67c26 JM |
4182 | if ((invalid_op_diag |
4183 | = targetm.invalid_binary_op (code, type0, type1))) | |
4184 | { | |
76545796 PC |
4185 | if (complain & tf_error) |
4186 | error (invalid_op_diag); | |
4de67c26 JM |
4187 | return error_mark_node; |
4188 | } | |
4189 | ||
ef3ddd4f MM |
4190 | /* Issue warnings about peculiar, but valid, uses of NULL. */ |
4191 | if ((orig_op0 == null_node || orig_op1 == null_node) | |
4192 | /* It's reasonable to use pointer values as operands of && | |
4193 | and ||, so NULL is no exception. */ | |
4194 | && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR | |
4195 | && ( /* Both are NULL (or 0) and the operation was not a | |
4196 | comparison or a pointer subtraction. */ | |
4197 | (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1) | |
4198 | && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR) | |
4199 | /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */ | |
4200 | || (!null_ptr_cst_p (orig_op0) | |
66b1156a | 4201 | && !TYPE_PTR_OR_PTRMEM_P (type0)) |
ef3ddd4f | 4202 | || (!null_ptr_cst_p (orig_op1) |
66b1156a | 4203 | && !TYPE_PTR_OR_PTRMEM_P (type1))) |
ef3ddd4f | 4204 | && (complain & tf_warning)) |
70dc395a DS |
4205 | { |
4206 | source_location loc = | |
4207 | expansion_point_location_if_in_system_header (input_location); | |
4208 | ||
4209 | warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic"); | |
4210 | } | |
ef3ddd4f | 4211 | |
a212e43f MG |
4212 | /* In case when one of the operands of the binary operation is |
4213 | a vector and another is a scalar -- convert scalar to vector. */ | |
4214 | if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE)) | |
4215 | { | |
4216 | enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1, | |
4217 | complain & tf_error); | |
4218 | ||
4219 | switch (convert_flag) | |
4220 | { | |
4221 | case stv_error: | |
4222 | return error_mark_node; | |
4223 | case stv_firstarg: | |
4224 | { | |
4225 | op0 = convert (TREE_TYPE (type1), op0); | |
3c9aabbd | 4226 | op0 = save_expr (op0); |
a212e43f MG |
4227 | op0 = build_vector_from_val (type1, op0); |
4228 | type0 = TREE_TYPE (op0); | |
4229 | code0 = TREE_CODE (type0); | |
4230 | converted = 1; | |
4231 | break; | |
4232 | } | |
4233 | case stv_secondarg: | |
4234 | { | |
4235 | op1 = convert (TREE_TYPE (type0), op1); | |
3c9aabbd | 4236 | op1 = save_expr (op1); |
a212e43f MG |
4237 | op1 = build_vector_from_val (type0, op1); |
4238 | type1 = TREE_TYPE (op1); | |
4239 | code1 = TREE_CODE (type1); | |
4240 | converted = 1; | |
4241 | break; | |
4242 | } | |
4243 | default: | |
4244 | break; | |
4245 | } | |
4246 | } | |
4247 | ||
8d08fdba MS |
4248 | switch (code) |
4249 | { | |
8d08fdba MS |
4250 | case MINUS_EXPR: |
4251 | /* Subtraction of two similar pointers. | |
4252 | We must subtract them as integers, then divide by object size. */ | |
4253 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
a5ac359a MM |
4254 | && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0), |
4255 | TREE_TYPE (type1))) | |
4f2e1536 MP |
4256 | return pointer_diff (location, op0, op1, |
4257 | common_pointer_type (type0, type1), complain); | |
0225a19d | 4258 | /* In all other cases except pointer - int, the usual arithmetic |
39a13be5 | 4259 | rules apply. */ |
0225a19d MM |
4260 | else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE)) |
4261 | { | |
4262 | common = 1; | |
4263 | break; | |
4264 | } | |
4265 | /* The pointer - int case is just like pointer + int; fall | |
4266 | through. */ | |
81fea426 | 4267 | gcc_fallthrough (); |
0225a19d MM |
4268 | case PLUS_EXPR: |
4269 | if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE) | |
4270 | && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE)) | |
4271 | { | |
4272 | tree ptr_operand; | |
4273 | tree int_operand; | |
4274 | ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1); | |
4275 | int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1); | |
4276 | if (processing_template_decl) | |
4277 | { | |
4278 | result_type = TREE_TYPE (ptr_operand); | |
4279 | break; | |
4280 | } | |
4f2e1536 MP |
4281 | return cp_pointer_int_sum (location, code, |
4282 | ptr_operand, | |
fd9b0f32 PC |
4283 | int_operand, |
4284 | complain); | |
0225a19d MM |
4285 | } |
4286 | common = 1; | |
8d08fdba MS |
4287 | break; |
4288 | ||
4289 | case MULT_EXPR: | |
4290 | common = 1; | |
4291 | break; | |
4292 | ||
4293 | case TRUNC_DIV_EXPR: | |
4294 | case CEIL_DIV_EXPR: | |
4295 | case FLOOR_DIV_EXPR: | |
4296 | case ROUND_DIV_EXPR: | |
4297 | case EXACT_DIV_EXPR: | |
37c46b43 | 4298 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
3a021db2 | 4299 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
37c46b43 | 4300 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
3a021db2 | 4301 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
8d08fdba | 4302 | { |
01c15146 | 4303 | enum tree_code tcode0 = code0, tcode1 = code1; |
234bef96 | 4304 | tree cop1 = fold_non_dependent_expr (op1); |
f8ed5150 | 4305 | doing_div_or_mod = true; |
de5a5fa1 | 4306 | warn_for_div_by_zero (location, cop1); |
c8094d83 | 4307 | |
01c15146 VR |
4308 | if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE) |
4309 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); | |
4310 | if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE) | |
4311 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); | |
3a021db2 | 4312 | |
01c15146 | 4313 | if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)) |
8d08fdba MS |
4314 | resultcode = RDIV_EXPR; |
4315 | else | |
4316 | /* When dividing two signed integers, we have to promote to int. | |
ddd5a7c1 | 4317 | unless we divide by a constant != -1. Note that default |
8d08fdba MS |
4318 | conversion will have been performed on the operands at this |
4319 | point, so we have to dig out the original type to find out if | |
4320 | it was unsigned. */ | |
4321 | shorten = ((TREE_CODE (op0) == NOP_EXPR | |
8df83eae | 4322 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) |
8d08fdba | 4323 | || (TREE_CODE (op1) == INTEGER_CST |
05bccae2 RK |
4324 | && ! integer_all_onesp (op1))); |
4325 | ||
8d08fdba MS |
4326 | common = 1; |
4327 | } | |
4328 | break; | |
4329 | ||
4330 | case BIT_AND_EXPR: | |
8d08fdba MS |
4331 | case BIT_IOR_EXPR: |
4332 | case BIT_XOR_EXPR: | |
73042643 | 4333 | if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
9ef0c8d9 AP |
4334 | || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
4335 | && !VECTOR_FLOAT_TYPE_P (type0) | |
4336 | && !VECTOR_FLOAT_TYPE_P (type1))) | |
8d08fdba | 4337 | shorten = -1; |
8d08fdba MS |
4338 | break; |
4339 | ||
4340 | case TRUNC_MOD_EXPR: | |
4341 | case FLOOR_MOD_EXPR: | |
f7e8cd25 | 4342 | { |
234bef96 | 4343 | tree cop1 = fold_non_dependent_expr (op1); |
f8ed5150 | 4344 | doing_div_or_mod = true; |
de5a5fa1 | 4345 | warn_for_div_by_zero (location, cop1); |
f7e8cd25 | 4346 | } |
c8094d83 | 4347 | |
5cfd5d9b AP |
4348 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
4349 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
4350 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
4351 | common = 1; | |
4352 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
8d08fdba MS |
4353 | { |
4354 | /* Although it would be tempting to shorten always here, that loses | |
4355 | on some targets, since the modulo instruction is undefined if the | |
4356 | quotient can't be represented in the computation mode. We shorten | |
4357 | only if unsigned or if dividing by something we know != -1. */ | |
4358 | shorten = ((TREE_CODE (op0) == NOP_EXPR | |
8df83eae | 4359 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) |
8d08fdba | 4360 | || (TREE_CODE (op1) == INTEGER_CST |
05bccae2 | 4361 | && ! integer_all_onesp (op1))); |
8d08fdba MS |
4362 | common = 1; |
4363 | } | |
4364 | break; | |
4365 | ||
4366 | case TRUTH_ANDIF_EXPR: | |
4367 | case TRUTH_ORIF_EXPR: | |
4368 | case TRUTH_AND_EXPR: | |
4369 | case TRUTH_OR_EXPR: | |
3c9aabbd | 4370 | if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1)) |
abb839ec | 4371 | { |
3c9aabbd MG |
4372 | if (!COMPARISON_CLASS_P (op1)) |
4373 | op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1, | |
4374 | build_zero_cst (type1), complain); | |
4375 | if (code == TRUTH_ANDIF_EXPR) | |
4376 | { | |
4377 | tree z = build_zero_cst (TREE_TYPE (op1)); | |
4378 | return build_conditional_expr (location, op0, op1, z, complain); | |
4379 | } | |
4380 | else if (code == TRUTH_ORIF_EXPR) | |
4381 | { | |
4382 | tree m1 = build_all_ones_cst (TREE_TYPE (op1)); | |
4383 | return build_conditional_expr (location, op0, m1, op1, complain); | |
4384 | } | |
4385 | else | |
4386 | gcc_unreachable (); | |
4387 | } | |
4388 | if (VECTOR_TYPE_P (type0)) | |
4389 | { | |
4390 | if (!COMPARISON_CLASS_P (op0)) | |
4391 | op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0, | |
4392 | build_zero_cst (type0), complain); | |
4393 | if (!VECTOR_TYPE_P (type1)) | |
4394 | { | |
4395 | tree m1 = build_all_ones_cst (TREE_TYPE (op0)); | |
4396 | tree z = build_zero_cst (TREE_TYPE (op0)); | |
efc3536f | 4397 | op1 = build_conditional_expr (location, op1, m1, z, complain); |
3c9aabbd MG |
4398 | } |
4399 | else if (!COMPARISON_CLASS_P (op1)) | |
4400 | op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1, | |
4401 | build_zero_cst (type1), complain); | |
4402 | ||
4403 | if (code == TRUTH_ANDIF_EXPR) | |
4404 | code = BIT_AND_EXPR; | |
4405 | else if (code == TRUTH_ORIF_EXPR) | |
4406 | code = BIT_IOR_EXPR; | |
4407 | else | |
4408 | gcc_unreachable (); | |
4409 | ||
4410 | return cp_build_binary_op (location, code, op0, op1, complain); | |
abb839ec | 4411 | } |
3c9aabbd | 4412 | |
255512c1 | 4413 | result_type = boolean_type_node; |
8d08fdba MS |
4414 | break; |
4415 | ||
4416 | /* Shift operations: result has same type as first operand; | |
4417 | always convert second operand to int. | |
4418 | Also set SHORT_SHIFT if shifting rightward. */ | |
4419 | ||
4420 | case RSHIFT_EXPR: | |
a212e43f MG |
4421 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
4422 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
4423 | { | |
4424 | result_type = type0; | |
4425 | converted = 1; | |
4426 | } | |
4427 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
31ed6226 MG |
4428 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
4429 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
4430 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
4431 | { | |
4432 | result_type = type0; | |
4433 | converted = 1; | |
4434 | } | |
4435 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
8d08fdba | 4436 | { |
234bef96 | 4437 | tree const_op1 = fold_non_dependent_expr (op1); |
cc83c823 JJ |
4438 | if (TREE_CODE (const_op1) != INTEGER_CST) |
4439 | const_op1 = op1; | |
8d08fdba | 4440 | result_type = type0; |
de5a5fa1 | 4441 | doing_shift = true; |
cc83c823 | 4442 | if (TREE_CODE (const_op1) == INTEGER_CST) |
8d08fdba | 4443 | { |
cc83c823 | 4444 | if (tree_int_cst_lt (const_op1, integer_zero_node)) |
5ade1ed2 | 4445 | { |
7d882b83 ILT |
4446 | if ((complain & tf_warning) |
4447 | && c_inhibit_evaluation_warnings == 0) | |
13c21655 PC |
4448 | warning (OPT_Wshift_count_negative, |
4449 | "right shift count is negative"); | |
5ade1ed2 | 4450 | } |
8d08fdba MS |
4451 | else |
4452 | { | |
cc83c823 | 4453 | if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0 |
7d882b83 ILT |
4454 | && (complain & tf_warning) |
4455 | && c_inhibit_evaluation_warnings == 0) | |
13c21655 PC |
4456 | warning (OPT_Wshift_count_overflow, |
4457 | "right shift count >= width of type"); | |
8d08fdba MS |
4458 | } |
4459 | } | |
8d08fdba MS |
4460 | /* Avoid converting op1 to result_type later. */ |
4461 | converted = 1; | |
4462 | } | |
4463 | break; | |
4464 | ||
4465 | case LSHIFT_EXPR: | |
a212e43f MG |
4466 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
4467 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
4468 | { | |
4469 | result_type = type0; | |
4470 | converted = 1; | |
4471 | } | |
4472 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
31ed6226 MG |
4473 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
4474 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
4475 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
4476 | { | |
4477 | result_type = type0; | |
4478 | converted = 1; | |
4479 | } | |
4480 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
8d08fdba | 4481 | { |
0173bd2a MP |
4482 | tree const_op0 = fold_non_dependent_expr (op0); |
4483 | if (TREE_CODE (const_op0) != INTEGER_CST) | |
4484 | const_op0 = op0; | |
234bef96 | 4485 | tree const_op1 = fold_non_dependent_expr (op1); |
cc83c823 JJ |
4486 | if (TREE_CODE (const_op1) != INTEGER_CST) |
4487 | const_op1 = op1; | |
8d08fdba | 4488 | result_type = type0; |
de5a5fa1 | 4489 | doing_shift = true; |
0173bd2a MP |
4490 | if (TREE_CODE (const_op0) == INTEGER_CST |
4491 | && tree_int_cst_sgn (const_op0) < 0 | |
4492 | && (complain & tf_warning) | |
4493 | && c_inhibit_evaluation_warnings == 0) | |
4494 | warning (OPT_Wshift_negative_value, | |
4495 | "left shift of negative value"); | |
cc83c823 | 4496 | if (TREE_CODE (const_op1) == INTEGER_CST) |
8d08fdba | 4497 | { |
cc83c823 | 4498 | if (tree_int_cst_lt (const_op1, integer_zero_node)) |
5ade1ed2 | 4499 | { |
7d882b83 ILT |
4500 | if ((complain & tf_warning) |
4501 | && c_inhibit_evaluation_warnings == 0) | |
13c21655 PC |
4502 | warning (OPT_Wshift_count_negative, |
4503 | "left shift count is negative"); | |
5ade1ed2 | 4504 | } |
cc83c823 JJ |
4505 | else if (compare_tree_int (const_op1, |
4506 | TYPE_PRECISION (type0)) >= 0) | |
5ade1ed2 | 4507 | { |
7d882b83 ILT |
4508 | if ((complain & tf_warning) |
4509 | && c_inhibit_evaluation_warnings == 0) | |
13c21655 PC |
4510 | warning (OPT_Wshift_count_overflow, |
4511 | "left shift count >= width of type"); | |
5ade1ed2 | 4512 | } |
451b5e48 MP |
4513 | else if (TREE_CODE (const_op0) == INTEGER_CST |
4514 | && (complain & tf_warning)) | |
4515 | maybe_warn_shift_overflow (location, const_op0, const_op1); | |
8d08fdba | 4516 | } |
8d08fdba MS |
4517 | /* Avoid converting op1 to result_type later. */ |
4518 | converted = 1; | |
4519 | } | |
4520 | break; | |
4521 | ||
4522 | case RROTATE_EXPR: | |
4523 | case LROTATE_EXPR: | |
4524 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
4525 | { | |
4526 | result_type = type0; | |
4527 | if (TREE_CODE (op1) == INTEGER_CST) | |
4528 | { | |
4529 | if (tree_int_cst_lt (op1, integer_zero_node)) | |
5ade1ed2 DG |
4530 | { |
4531 | if (complain & tf_warning) | |
4532 | warning (0, (code == LROTATE_EXPR) | |
4533 | ? G_("left rotate count is negative") | |
4534 | : G_("right rotate count is negative")); | |
4535 | } | |
665f2503 | 4536 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
5ade1ed2 DG |
4537 | { |
4538 | if (complain & tf_warning) | |
4539 | warning (0, (code == LROTATE_EXPR) | |
4540 | ? G_("left rotate count >= width of type") | |
4541 | : G_("right rotate count >= width of type")); | |
4542 | } | |
8d08fdba MS |
4543 | } |
4544 | /* Convert the shift-count to an integer, regardless of | |
4545 | size of value being shifted. */ | |
4546 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
4b978f96 | 4547 | op1 = cp_convert (integer_type_node, op1, complain); |
8d08fdba MS |
4548 | } |
4549 | break; | |
4550 | ||
4551 | case EQ_EXPR: | |
4552 | case NE_EXPR: | |
31ed6226 MG |
4553 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
4554 | goto vector_compare; | |
5ade1ed2 DG |
4555 | if ((complain & tf_warning) |
4556 | && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))) | |
3db45ab5 MS |
4557 | warning (OPT_Wfloat_equal, |
4558 | "comparing floating point with == or != is unsafe"); | |
5ade1ed2 | 4559 | if ((complain & tf_warning) |
66756373 JJ |
4560 | && ((TREE_CODE (orig_op0) == STRING_CST |
4561 | && !integer_zerop (cp_fully_fold (op1))) | |
4562 | || (TREE_CODE (orig_op1) == STRING_CST | |
4563 | && !integer_zerop (cp_fully_fold (op0))))) | |
4564 | warning (OPT_Waddress, "comparison with string literal results " | |
9c582551 | 4565 | "in unspecified behavior"); |
1bdba2c0 | 4566 | |
c8094d83 | 4567 | build_type = boolean_type_node; |
37c46b43 | 4568 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
550a799d | 4569 | || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE) |
37c46b43 | 4570 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
550a799d | 4571 | || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)) |
8d08fdba | 4572 | short_compare = 1; |
81caef48 | 4573 | else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0)) |
f41f1ceb | 4574 | && null_ptr_cst_p (orig_op1)) |
81caef48 PC |
4575 | /* Handle, eg, (void*)0 (c++/43906), and more. */ |
4576 | || (code0 == POINTER_TYPE | |
4577 | && TYPE_PTR_P (type1) && integer_zerop (op1))) | |
b3c6d2ea | 4578 | { |
81caef48 PC |
4579 | if (TYPE_PTR_P (type1)) |
4580 | result_type = composite_pointer_type (type0, type1, op0, op1, | |
4581 | CPO_COMPARISON, complain); | |
4582 | else | |
4583 | result_type = type0; | |
4584 | ||
b1970801 | 4585 | warn_for_null_address (location, op0, complain); |
b3c6d2ea | 4586 | } |
81caef48 | 4587 | else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1)) |
f41f1ceb | 4588 | && null_ptr_cst_p (orig_op0)) |
81caef48 PC |
4589 | /* Handle, eg, (void*)0 (c++/43906), and more. */ |
4590 | || (code1 == POINTER_TYPE | |
4591 | && TYPE_PTR_P (type0) && integer_zerop (op0))) | |
b3c6d2ea | 4592 | { |
81caef48 PC |
4593 | if (TYPE_PTR_P (type0)) |
4594 | result_type = composite_pointer_type (type0, type1, op0, op1, | |
4595 | CPO_COMPARISON, complain); | |
4596 | else | |
4597 | result_type = type1; | |
4598 | ||
b1970801 | 4599 | warn_for_null_address (location, op1, complain); |
b3c6d2ea | 4600 | } |
81caef48 PC |
4601 | else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
4602 | || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1))) | |
4603 | result_type = composite_pointer_type (type0, type1, op0, op1, | |
4604 | CPO_COMPARISON, complain); | |
f41f1ceb | 4605 | else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)) |
14c2101d JM |
4606 | /* One of the operands must be of nullptr_t type. */ |
4607 | result_type = TREE_TYPE (nullptr_node); | |
8d08fdba MS |
4608 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
4609 | { | |
faae18ab | 4610 | result_type = type0; |
5ade1ed2 | 4611 | if (complain & tf_error) |
cbe5f3b3 | 4612 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
4613 | else |
4614 | return error_mark_node; | |
8d08fdba MS |
4615 | } |
4616 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
4617 | { | |
faae18ab | 4618 | result_type = type1; |
5ade1ed2 | 4619 | if (complain & tf_error) |
cbe5f3b3 | 4620 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
4621 | else |
4622 | return error_mark_node; | |
8d08fdba | 4623 | } |
f41f1ceb | 4624 | else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1)) |
8d08fdba | 4625 | { |
c3a88be8 RM |
4626 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION |
4627 | == ptrmemfunc_vbit_in_delta) | |
4628 | { | |
20265464 ZC |
4629 | tree pfn0, delta0, e1, e2; |
4630 | ||
4631 | if (TREE_SIDE_EFFECTS (op0)) | |
4632 | op0 = save_expr (op0); | |
4633 | ||
4634 | pfn0 = pfn_from_ptrmemfunc (op0); | |
4635 | delta0 = delta_from_ptrmemfunc (op0); | |
4636 | e1 = cp_build_binary_op (location, | |
4637 | EQ_EXPR, | |
4638 | pfn0, | |
4639 | build_zero_cst (TREE_TYPE (pfn0)), | |
4640 | complain); | |
4641 | e2 = cp_build_binary_op (location, | |
4642 | BIT_AND_EXPR, | |
4643 | delta0, | |
4644 | integer_one_node, | |
4645 | complain); | |
30b07d03 PC |
4646 | |
4647 | if (complain & tf_warning) | |
4648 | maybe_warn_zero_as_null_pointer_constant (op1, input_location); | |
51e355aa | 4649 | |
ba47d38d AH |
4650 | e2 = cp_build_binary_op (location, |
4651 | EQ_EXPR, e2, integer_zero_node, | |
5ade1ed2 | 4652 | complain); |
ba47d38d AH |
4653 | op0 = cp_build_binary_op (location, |
4654 | TRUTH_ANDIF_EXPR, e1, e2, | |
5ade1ed2 | 4655 | complain); |
cda0a029 | 4656 | op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain); |
c3a88be8 RM |
4657 | } |
4658 | else | |
4659 | { | |
4660 | op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier); | |
4b978f96 | 4661 | op1 = cp_convert (TREE_TYPE (op0), op1, complain); |
c3a88be8 | 4662 | } |
faae18ab | 4663 | result_type = TREE_TYPE (op0); |
8d08fdba | 4664 | } |
f41f1ceb | 4665 | else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0)) |
ba47d38d | 4666 | return cp_build_binary_op (location, code, op1, op0, complain); |
867f133e | 4667 | else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)) |
8d08fdba | 4668 | { |
867f133e | 4669 | tree type; |
c7e266a6 MM |
4670 | /* E will be the final comparison. */ |
4671 | tree e; | |
4672 | /* E1 and E2 are for scratch. */ | |
4673 | tree e1; | |
4674 | tree e2; | |
1f84ec23 MM |
4675 | tree pfn0; |
4676 | tree pfn1; | |
4677 | tree delta0; | |
4678 | tree delta1; | |
c7e266a6 | 4679 | |
c86818cf SZ |
4680 | type = composite_pointer_type (type0, type1, op0, op1, |
4681 | CPO_COMPARISON, complain); | |
867f133e OW |
4682 | |
4683 | if (!same_type_p (TREE_TYPE (op0), type)) | |
4b978f96 | 4684 | op0 = cp_convert_and_check (type, op0, complain); |
867f133e | 4685 | if (!same_type_p (TREE_TYPE (op1), type)) |
4b978f96 | 4686 | op1 = cp_convert_and_check (type, op1, complain); |
867f133e OW |
4687 | |
4688 | if (op0 == error_mark_node || op1 == error_mark_node) | |
4689 | return error_mark_node; | |
4690 | ||
ebb1abc3 JM |
4691 | if (TREE_SIDE_EFFECTS (op0)) |
4692 | op0 = save_expr (op0); | |
4693 | if (TREE_SIDE_EFFECTS (op1)) | |
4694 | op1 = save_expr (op1); | |
4695 | ||
1f84ec23 | 4696 | pfn0 = pfn_from_ptrmemfunc (op0); |
cda0a029 | 4697 | pfn0 = cp_fully_fold (pfn0); |
457d0ace PC |
4698 | /* Avoid -Waddress warnings (c++/64877). */ |
4699 | if (TREE_CODE (pfn0) == ADDR_EXPR) | |
4700 | TREE_NO_WARNING (pfn0) = 1; | |
1f84ec23 | 4701 | pfn1 = pfn_from_ptrmemfunc (op1); |
cda0a029 | 4702 | pfn1 = cp_fully_fold (pfn1); |
a298680c OW |
4703 | delta0 = delta_from_ptrmemfunc (op0); |
4704 | delta1 = delta_from_ptrmemfunc (op1); | |
c3a88be8 RM |
4705 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION |
4706 | == ptrmemfunc_vbit_in_delta) | |
4707 | { | |
4708 | /* We generate: | |
4709 | ||
4710 | (op0.pfn == op1.pfn | |
4711 | && ((op0.delta == op1.delta) | |
4712 | || (!op0.pfn && op0.delta & 1 == 0 | |
4713 | && op1.delta & 1 == 0)) | |
4714 | ||
4715 | The reason for the `!op0.pfn' bit is that a NULL | |
4716 | pointer-to-member is any member with a zero PFN and | |
4717 | LSB of the DELTA field is 0. */ | |
4718 | ||
ba47d38d | 4719 | e1 = cp_build_binary_op (location, BIT_AND_EXPR, |
c3a88be8 | 4720 | delta0, |
5ade1ed2 DG |
4721 | integer_one_node, |
4722 | complain); | |
ba47d38d AH |
4723 | e1 = cp_build_binary_op (location, |
4724 | EQ_EXPR, e1, integer_zero_node, | |
5ade1ed2 | 4725 | complain); |
ba47d38d | 4726 | e2 = cp_build_binary_op (location, BIT_AND_EXPR, |
c3a88be8 | 4727 | delta1, |
5ade1ed2 DG |
4728 | integer_one_node, |
4729 | complain); | |
ba47d38d AH |
4730 | e2 = cp_build_binary_op (location, |
4731 | EQ_EXPR, e2, integer_zero_node, | |
5ade1ed2 | 4732 | complain); |
ba47d38d AH |
4733 | e1 = cp_build_binary_op (location, |
4734 | TRUTH_ANDIF_EXPR, e2, e1, | |
5ade1ed2 | 4735 | complain); |
ba47d38d | 4736 | e2 = cp_build_binary_op (location, EQ_EXPR, |
c3a88be8 | 4737 | pfn0, |
e8160c9a | 4738 | build_zero_cst (TREE_TYPE (pfn0)), |
5ade1ed2 | 4739 | complain); |
ba47d38d AH |
4740 | e2 = cp_build_binary_op (location, |
4741 | TRUTH_ANDIF_EXPR, e2, e1, complain); | |
4742 | e1 = cp_build_binary_op (location, | |
4743 | EQ_EXPR, delta0, delta1, complain); | |
4744 | e1 = cp_build_binary_op (location, | |
4745 | TRUTH_ORIF_EXPR, e1, e2, complain); | |
c3a88be8 RM |
4746 | } |
4747 | else | |
4748 | { | |
4749 | /* We generate: | |
4750 | ||
4751 | (op0.pfn == op1.pfn | |
4752 | && (!op0.pfn || op0.delta == op1.delta)) | |
4753 | ||
4754 | The reason for the `!op0.pfn' bit is that a NULL | |
4755 | pointer-to-member is any member with a zero PFN; the | |
4756 | DELTA field is unspecified. */ | |
4757 | ||
ba47d38d AH |
4758 | e1 = cp_build_binary_op (location, |
4759 | EQ_EXPR, delta0, delta1, complain); | |
4760 | e2 = cp_build_binary_op (location, | |
4761 | EQ_EXPR, | |
c3a88be8 | 4762 | pfn0, |
e8160c9a | 4763 | build_zero_cst (TREE_TYPE (pfn0)), |
5ade1ed2 | 4764 | complain); |
ba47d38d AH |
4765 | e1 = cp_build_binary_op (location, |
4766 | TRUTH_ORIF_EXPR, e1, e2, complain); | |
c3a88be8 | 4767 | } |
f293ce4b | 4768 | e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1); |
ba47d38d AH |
4769 | e = cp_build_binary_op (location, |
4770 | TRUTH_ANDIF_EXPR, e2, e1, complain); | |
8d08fdba | 4771 | if (code == EQ_EXPR) |
c7e266a6 | 4772 | return e; |
ba47d38d AH |
4773 | return cp_build_binary_op (location, |
4774 | EQ_EXPR, e, integer_zero_node, complain); | |
8d08fdba | 4775 | } |
315fb5db NS |
4776 | else |
4777 | { | |
4778 | gcc_assert (!TYPE_PTRMEMFUNC_P (type0) | |
4779 | || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), | |
4780 | type1)); | |
4781 | gcc_assert (!TYPE_PTRMEMFUNC_P (type1) | |
4782 | || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), | |
4783 | type0)); | |
4784 | } | |
c8094d83 | 4785 | |
8d08fdba MS |
4786 | break; |
4787 | ||
4788 | case MAX_EXPR: | |
4789 | case MIN_EXPR: | |
4790 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) | |
4791 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
4792 | shorten = 1; | |
4793 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
708cae97 | 4794 | result_type = composite_pointer_type (type0, type1, op0, op1, |
c86818cf | 4795 | CPO_COMPARISON, complain); |
8d08fdba MS |
4796 | break; |
4797 | ||
4798 | case LE_EXPR: | |
4799 | case GE_EXPR: | |
4800 | case LT_EXPR: | |
4801 | case GT_EXPR: | |
eda0cd98 RS |
4802 | if (TREE_CODE (orig_op0) == STRING_CST |
4803 | || TREE_CODE (orig_op1) == STRING_CST) | |
5ade1ed2 DG |
4804 | { |
4805 | if (complain & tf_warning) | |
9c582551 JJ |
4806 | warning (OPT_Waddress, "comparison with string literal results " |
4807 | "in unspecified behavior"); | |
5ade1ed2 | 4808 | } |
eda0cd98 | 4809 | |
31ed6226 MG |
4810 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
4811 | { | |
4812 | vector_compare: | |
4813 | tree intt; | |
4814 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0), | |
0af94e6f JR |
4815 | TREE_TYPE (type1)) |
4816 | && !vector_types_compatible_elements_p (type0, type1)) | |
31ed6226 | 4817 | { |
07298ffd MG |
4818 | if (complain & tf_error) |
4819 | { | |
4820 | error_at (location, "comparing vectors with different " | |
4821 | "element types"); | |
4822 | inform (location, "operand types are %qT and %qT", | |
4823 | type0, type1); | |
4824 | } | |
31ed6226 MG |
4825 | return error_mark_node; |
4826 | } | |
4827 | ||
4828 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
4829 | { | |
07298ffd MG |
4830 | if (complain & tf_error) |
4831 | { | |
4832 | error_at (location, "comparing vectors with different " | |
4833 | "number of elements"); | |
4834 | inform (location, "operand types are %qT and %qT", | |
4835 | type0, type1); | |
4836 | } | |
31ed6226 MG |
4837 | return error_mark_node; |
4838 | } | |
4839 | ||
fa74a4bc MP |
4840 | /* It's not precisely specified how the usual arithmetic |
4841 | conversions apply to the vector types. Here, we use | |
4842 | the unsigned type if one of the operands is signed and | |
4843 | the other one is unsigned. */ | |
4844 | if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)) | |
4845 | { | |
4846 | if (!TYPE_UNSIGNED (type0)) | |
4847 | op0 = build1 (VIEW_CONVERT_EXPR, type1, op0); | |
4848 | else | |
4849 | op1 = build1 (VIEW_CONVERT_EXPR, type0, op1); | |
4850 | warning_at (location, OPT_Wsign_compare, "comparison between " | |
4851 | "types %qT and %qT", type0, type1); | |
4852 | } | |
4853 | ||
31ed6226 MG |
4854 | /* Always construct signed integer vector type. */ |
4855 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
4856 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
07298ffd MG |
4857 | if (!intt) |
4858 | { | |
4859 | if (complain & tf_error) | |
4860 | error_at (location, "could not find an integer type " | |
4861 | "of the same size as %qT", TREE_TYPE (type0)); | |
4862 | return error_mark_node; | |
4863 | } | |
31ed6226 MG |
4864 | result_type = build_opaque_vector_type (intt, |
4865 | TYPE_VECTOR_SUBPARTS (type0)); | |
4866 | converted = 1; | |
9f47c7e5 | 4867 | return build_vec_cmp (resultcode, result_type, op0, op1); |
31ed6226 | 4868 | } |
28cbf42c | 4869 | build_type = boolean_type_node; |
b5c0a77e JM |
4870 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
4871 | || code0 == ENUMERAL_TYPE) | |
4872 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
4873 | || code1 == ENUMERAL_TYPE)) | |
8d08fdba MS |
4874 | short_compare = 1; |
4875 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
708cae97 | 4876 | result_type = composite_pointer_type (type0, type1, op0, op1, |
c86818cf | 4877 | CPO_COMPARISON, complain); |
f41f1ceb | 4878 | else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1)) |
6f94398e PC |
4879 | { |
4880 | result_type = type0; | |
4881 | if (extra_warnings && (complain & tf_warning)) | |
4882 | warning (OPT_Wextra, | |
4883 | "ordered comparison of pointer with integer zero"); | |
4884 | } | |
f41f1ceb | 4885 | else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0)) |
6f94398e PC |
4886 | { |
4887 | result_type = type1; | |
4888 | if (extra_warnings && (complain & tf_warning)) | |
4889 | warning (OPT_Wextra, | |
4890 | "ordered comparison of pointer with integer zero"); | |
4891 | } | |
f41f1ceb | 4892 | else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)) |
14c2101d JM |
4893 | /* One of the operands must be of nullptr_t type. */ |
4894 | result_type = TREE_TYPE (nullptr_node); | |
8d08fdba MS |
4895 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
4896 | { | |
faae18ab | 4897 | result_type = type0; |
5ade1ed2 | 4898 | if (complain & tf_error) |
cbe5f3b3 | 4899 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
4900 | else |
4901 | return error_mark_node; | |
8d08fdba MS |
4902 | } |
4903 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
4904 | { | |
faae18ab | 4905 | result_type = type1; |
5ade1ed2 | 4906 | if (complain & tf_error) |
cbe5f3b3 | 4907 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
4908 | else |
4909 | return error_mark_node; | |
8d08fdba | 4910 | } |
8d08fdba | 4911 | break; |
7f85441b | 4912 | |
1eb8759b RH |
4913 | case UNORDERED_EXPR: |
4914 | case ORDERED_EXPR: | |
4915 | case UNLT_EXPR: | |
4916 | case UNLE_EXPR: | |
4917 | case UNGT_EXPR: | |
4918 | case UNGE_EXPR: | |
4919 | case UNEQ_EXPR: | |
1eb8759b RH |
4920 | build_type = integer_type_node; |
4921 | if (code0 != REAL_TYPE || code1 != REAL_TYPE) | |
4922 | { | |
5ade1ed2 DG |
4923 | if (complain & tf_error) |
4924 | error ("unordered comparison on non-floating point argument"); | |
1eb8759b RH |
4925 | return error_mark_node; |
4926 | } | |
4927 | common = 1; | |
4928 | break; | |
4929 | ||
7f85441b KG |
4930 | default: |
4931 | break; | |
8d08fdba MS |
4932 | } |
4933 | ||
550a799d JM |
4934 | if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
4935 | || code0 == ENUMERAL_TYPE) | |
c8094d83 | 4936 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
550a799d | 4937 | || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))) |
73042643 AH |
4938 | arithmetic_types_p = 1; |
4939 | else | |
4940 | { | |
4941 | arithmetic_types_p = 0; | |
4942 | /* Vector arithmetic is only allowed when both sides are vectors. */ | |
4943 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) | |
4944 | { | |
f6d7e7d8 | 4945 | if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) |
0af94e6f | 4946 | || !vector_types_compatible_elements_p (type0, type1)) |
f6d7e7d8 | 4947 | { |
76545796 | 4948 | if (complain & tf_error) |
745e411d DM |
4949 | { |
4950 | /* "location" already embeds the locations of the | |
4951 | operands, so we don't need to add them separately | |
4952 | to richloc. */ | |
4953 | rich_location richloc (line_table, location); | |
4954 | binary_op_error (&richloc, code, type0, type1); | |
4955 | } | |
f6d7e7d8 AH |
4956 | return error_mark_node; |
4957 | } | |
73042643 AH |
4958 | arithmetic_types_p = 1; |
4959 | } | |
4960 | } | |
455f19cb MM |
4961 | /* Determine the RESULT_TYPE, if it is not already known. */ |
4962 | if (!result_type | |
c8094d83 | 4963 | && arithmetic_types_p |
455f19cb | 4964 | && (shorten || common || short_compare)) |
c5ee1358 MM |
4965 | { |
4966 | result_type = cp_common_type (type0, type1); | |
76545796 PC |
4967 | if (complain & tf_warning) |
4968 | do_warn_double_promotion (result_type, type0, type1, | |
4969 | "implicit conversion from %qT to %qT " | |
4970 | "to match other operand of binary " | |
4971 | "expression", | |
4972 | location); | |
c5ee1358 | 4973 | } |
455f19cb MM |
4974 | |
4975 | if (!result_type) | |
8d08fdba | 4976 | { |
5ade1ed2 | 4977 | if (complain & tf_error) |
745e411d DM |
4978 | error_at (location, |
4979 | "invalid operands of types %qT and %qT to binary %qO", | |
4980 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code); | |
455f19cb MM |
4981 | return error_mark_node; |
4982 | } | |
37c46b43 | 4983 | |
455f19cb MM |
4984 | /* If we're in a template, the only thing we need to know is the |
4985 | RESULT_TYPE. */ | |
4986 | if (processing_template_decl) | |
5be014d5 AP |
4987 | { |
4988 | /* Since the middle-end checks the type when doing a build2, we | |
4989 | need to build the tree in pieces. This built tree will never | |
4990 | get out of the front-end as we replace it when instantiating | |
4991 | the template. */ | |
4992 | tree tmp = build2 (resultcode, | |
4993 | build_type ? build_type : result_type, | |
4994 | NULL_TREE, op1); | |
4995 | TREE_OPERAND (tmp, 0) = op0; | |
4996 | return tmp; | |
4997 | } | |
455f19cb MM |
4998 | |
4999 | if (arithmetic_types_p) | |
5000 | { | |
1ff6b2c8 JM |
5001 | bool first_complex = (code0 == COMPLEX_TYPE); |
5002 | bool second_complex = (code1 == COMPLEX_TYPE); | |
5003 | int none_complex = (!first_complex && !second_complex); | |
5004 | ||
5005 | /* Adapted from patch for c/24581. */ | |
5006 | if (first_complex != second_complex | |
5007 | && (code == PLUS_EXPR | |
5008 | || code == MINUS_EXPR | |
5009 | || code == MULT_EXPR | |
5010 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
5011 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
5012 | && flag_signed_zeros) | |
5013 | { | |
5014 | /* An operation on mixed real/complex operands must be | |
5015 | handled specially, but the language-independent code can | |
5016 | more easily optimize the plain complex arithmetic if | |
5017 | -fno-signed-zeros. */ | |
5018 | tree real_type = TREE_TYPE (result_type); | |
5019 | tree real, imag; | |
5020 | if (first_complex) | |
5021 | { | |
5022 | if (TREE_TYPE (op0) != result_type) | |
4b978f96 | 5023 | op0 = cp_convert_and_check (result_type, op0, complain); |
1ff6b2c8 | 5024 | if (TREE_TYPE (op1) != real_type) |
4b978f96 | 5025 | op1 = cp_convert_and_check (real_type, op1, complain); |
1ff6b2c8 JM |
5026 | } |
5027 | else | |
5028 | { | |
5029 | if (TREE_TYPE (op0) != real_type) | |
4b978f96 | 5030 | op0 = cp_convert_and_check (real_type, op0, complain); |
1ff6b2c8 | 5031 | if (TREE_TYPE (op1) != result_type) |
4b978f96 | 5032 | op1 = cp_convert_and_check (result_type, op1, complain); |
1ff6b2c8 JM |
5033 | } |
5034 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
5035 | return error_mark_node; | |
5036 | if (first_complex) | |
5037 | { | |
5038 | op0 = save_expr (op0); | |
e51fbec3 MP |
5039 | real = cp_build_unary_op (REALPART_EXPR, op0, true, complain); |
5040 | imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain); | |
1ff6b2c8 JM |
5041 | switch (code) |
5042 | { | |
5043 | case MULT_EXPR: | |
5044 | case TRUNC_DIV_EXPR: | |
e834d848 | 5045 | op1 = save_expr (op1); |
1ff6b2c8 JM |
5046 | imag = build2 (resultcode, real_type, imag, op1); |
5047 | /* Fall through. */ | |
5048 | case PLUS_EXPR: | |
5049 | case MINUS_EXPR: | |
5050 | real = build2 (resultcode, real_type, real, op1); | |
5051 | break; | |
5052 | default: | |
5053 | gcc_unreachable(); | |
5054 | } | |
5055 | } | |
5056 | else | |
5057 | { | |
5058 | op1 = save_expr (op1); | |
e51fbec3 MP |
5059 | real = cp_build_unary_op (REALPART_EXPR, op1, true, complain); |
5060 | imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain); | |
1ff6b2c8 JM |
5061 | switch (code) |
5062 | { | |
5063 | case MULT_EXPR: | |
e834d848 | 5064 | op0 = save_expr (op0); |
1ff6b2c8 JM |
5065 | imag = build2 (resultcode, real_type, op0, imag); |
5066 | /* Fall through. */ | |
5067 | case PLUS_EXPR: | |
5068 | real = build2 (resultcode, real_type, op0, real); | |
5069 | break; | |
5070 | case MINUS_EXPR: | |
5071 | real = build2 (resultcode, real_type, op0, real); | |
5072 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
5073 | break; | |
5074 | default: | |
5075 | gcc_unreachable(); | |
5076 | } | |
5077 | } | |
68a29e1c | 5078 | result = build2 (COMPLEX_EXPR, result_type, real, imag); |
68a29e1c | 5079 | return result; |
1ff6b2c8 | 5080 | } |
8d08fdba MS |
5081 | |
5082 | /* For certain operations (which identify themselves by shorten != 0) | |
5083 | if both args were extended from the same smaller type, | |
5084 | do the arithmetic in that type and then extend. | |
5085 | ||
5086 | shorten !=0 and !=1 indicates a bitwise operation. | |
5087 | For them, this optimization is safe only if | |
5088 | both args are zero-extended or both are sign-extended. | |
5089 | Otherwise, we might change the result. | |
39a13be5 | 5090 | E.g., (short)-1 | (unsigned short)-1 is (int)-1 |
8d08fdba MS |
5091 | but calculated in (unsigned short) it would be (unsigned short)-1. */ |
5092 | ||
37c46b43 | 5093 | if (shorten && none_complex) |
8d08fdba | 5094 | { |
de5a5fa1 | 5095 | orig_type = result_type; |
8d08fdba | 5096 | final_type = result_type; |
de5a5fa1 | 5097 | result_type = shorten_binary_op (result_type, op0, op1, |
6715192c | 5098 | shorten == -1); |
8d08fdba MS |
5099 | } |
5100 | ||
8d08fdba MS |
5101 | /* Comparison operations are shortened too but differently. |
5102 | They identify themselves by setting short_compare = 1. */ | |
5103 | ||
5104 | if (short_compare) | |
5105 | { | |
cda0a029 JM |
5106 | /* We call shorten_compare only for diagnostic-reason. */ |
5107 | tree xop0 = fold_simple (op0), xop1 = fold_simple (op1), | |
5108 | xresult_type = result_type; | |
8d08fdba | 5109 | enum tree_code xresultcode = resultcode; |
cda0a029 | 5110 | shorten_compare (location, &xop0, &xop1, &xresult_type, |
393e8e8b | 5111 | &xresultcode); |
8d08fdba MS |
5112 | } |
5113 | ||
8d3b081e | 5114 | if ((short_compare || code == MIN_EXPR || code == MAX_EXPR) |
7d48af30 MM |
5115 | && warn_sign_compare |
5116 | /* Do not warn until the template is instantiated; we cannot | |
5117 | bound the ranges of the arguments until that point. */ | |
2d12797c | 5118 | && !processing_template_decl |
98f80e91 | 5119 | && (complain & tf_warning) |
12448f77 ILT |
5120 | && c_inhibit_evaluation_warnings == 0 |
5121 | /* Even unsigned enum types promote to signed int. We don't | |
5122 | want to issue -Wsign-compare warnings for this case. */ | |
5123 | && !enum_cast_to_int (orig_op0) | |
5124 | && !enum_cast_to_int (orig_op1)) | |
8d08fdba | 5125 | { |
0d23cf7a JJ |
5126 | tree oop0 = maybe_constant_value (orig_op0); |
5127 | tree oop1 = maybe_constant_value (orig_op1); | |
5128 | ||
5129 | if (TREE_CODE (oop0) != INTEGER_CST) | |
cda0a029 | 5130 | oop0 = cp_fully_fold (orig_op0); |
0d23cf7a | 5131 | if (TREE_CODE (oop1) != INTEGER_CST) |
cda0a029 | 5132 | oop1 = cp_fully_fold (orig_op1); |
0d23cf7a | 5133 | warn_for_sign_compare (location, oop0, oop1, op0, op1, |
ba47d38d | 5134 | result_type, resultcode); |
8d08fdba MS |
5135 | } |
5136 | } | |
5137 | ||
b9edb4b1 MLI |
5138 | /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE. |
5139 | Then the expression will be built. | |
5140 | It will be given type FINAL_TYPE if that is nonzero; | |
5141 | otherwise, it will be given type RESULT_TYPE. */ | |
8d08fdba MS |
5142 | if (! converted) |
5143 | { | |
5144 | if (TREE_TYPE (op0) != result_type) | |
4b978f96 | 5145 | op0 = cp_convert_and_check (result_type, op0, complain); |
8d08fdba | 5146 | if (TREE_TYPE (op1) != result_type) |
4b978f96 | 5147 | op1 = cp_convert_and_check (result_type, op1, complain); |
848b92e1 JM |
5148 | |
5149 | if (op0 == error_mark_node || op1 == error_mark_node) | |
5150 | return error_mark_node; | |
8d08fdba MS |
5151 | } |
5152 | ||
28cbf42c MS |
5153 | if (build_type == NULL_TREE) |
5154 | build_type = result_type; | |
5155 | ||
f8ed5150 MP |
5156 | if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE |
5157 | | SANITIZE_FLOAT_DIVIDE)) | |
de5a5fa1 | 5158 | && !processing_template_decl |
f5481fc4 | 5159 | && do_ubsan_in_current_function () |
de5a5fa1 MP |
5160 | && (doing_div_or_mod || doing_shift)) |
5161 | { | |
5162 | /* OP0 and/or OP1 might have side-effects. */ | |
5163 | op0 = cp_save_expr (op0); | |
5164 | op1 = cp_save_expr (op1); | |
234bef96 PC |
5165 | op0 = fold_non_dependent_expr (op0); |
5166 | op1 = fold_non_dependent_expr (op1); | |
f8ed5150 MP |
5167 | if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE |
5168 | | SANITIZE_FLOAT_DIVIDE))) | |
de5a5fa1 MP |
5169 | { |
5170 | /* For diagnostics we want to use the promoted types without | |
5171 | shorten_binary_op. So convert the arguments to the | |
5172 | original result_type. */ | |
5173 | tree cop0 = op0; | |
5174 | tree cop1 = op1; | |
5175 | if (orig_type != NULL && result_type != orig_type) | |
5176 | { | |
5177 | cop0 = cp_convert (orig_type, op0, complain); | |
5178 | cop1 = cp_convert (orig_type, op1, complain); | |
5179 | } | |
5180 | instrument_expr = ubsan_instrument_division (location, cop0, cop1); | |
5181 | } | |
a24d975c | 5182 | else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT)) |
de5a5fa1 MP |
5183 | instrument_expr = ubsan_instrument_shift (location, code, op0, op1); |
5184 | } | |
5185 | ||
e87eed2a | 5186 | result = build2_loc (location, resultcode, build_type, op0, op1); |
455f19cb | 5187 | if (final_type != 0) |
4b978f96 | 5188 | result = cp_convert (final_type, result, complain); |
16fd4d2d | 5189 | |
a24d975c | 5190 | if (instrument_expr != NULL) |
cda0a029 JM |
5191 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
5192 | instrument_expr, result); | |
5193 | ||
5194 | if (!processing_template_decl) | |
5195 | { | |
5196 | op0 = cp_fully_fold (op0); | |
5197 | /* Only consider the second argument if the first isn't overflowed. */ | |
5198 | if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0)) | |
5199 | return result; | |
5200 | op1 = cp_fully_fold (op1); | |
5201 | if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1)) | |
5202 | return result; | |
5203 | } | |
5204 | else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1) | |
5205 | || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1)) | |
5206 | return result; | |
5207 | ||
5208 | result_ovl = fold_build2 (resultcode, build_type, op0, op1); | |
5209 | if (TREE_OVERFLOW_P (result_ovl)) | |
5210 | overflow_warning (location, result_ovl); | |
de5a5fa1 | 5211 | |
455f19cb | 5212 | return result; |
8d08fdba | 5213 | } |
bedc293e MG |
5214 | |
5215 | /* Build a VEC_PERM_EXPR. | |
5216 | This is a simple wrapper for c_build_vec_perm_expr. */ | |
5217 | tree | |
5218 | build_x_vec_perm_expr (location_t loc, | |
5219 | tree arg0, tree arg1, tree arg2, | |
5220 | tsubst_flags_t complain) | |
5221 | { | |
4939c5f3 MG |
5222 | tree orig_arg0 = arg0; |
5223 | tree orig_arg1 = arg1; | |
5224 | tree orig_arg2 = arg2; | |
5225 | if (processing_template_decl) | |
5226 | { | |
5227 | if (type_dependent_expression_p (arg0) | |
bedc293e | 5228 | || type_dependent_expression_p (arg1) |
4939c5f3 MG |
5229 | || type_dependent_expression_p (arg2)) |
5230 | return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2); | |
5231 | arg0 = build_non_dependent_expr (arg0); | |
5232 | if (arg1) | |
5233 | arg1 = build_non_dependent_expr (arg1); | |
5234 | arg2 = build_non_dependent_expr (arg2); | |
5235 | } | |
5236 | tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error); | |
5237 | if (processing_template_decl && exp != error_mark_node) | |
5238 | return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0, | |
5239 | orig_arg1, orig_arg2); | |
5240 | return exp; | |
bedc293e | 5241 | } |
8d08fdba MS |
5242 | \f |
5243 | /* Return a tree for the sum or difference (RESULTCODE says which) | |
5244 | of pointer PTROP and integer INTOP. */ | |
5245 | ||
5246 | static tree | |
4f2e1536 MP |
5247 | cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop, |
5248 | tree intop, tsubst_flags_t complain) | |
8d08fdba | 5249 | { |
bfba94bd MM |
5250 | tree res_type = TREE_TYPE (ptrop); |
5251 | ||
5252 | /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type) | |
5253 | in certain circumstance (when it's valid to do so). So we need | |
5254 | to make sure it's complete. We don't need to check here, if we | |
5255 | can actually complete it at all, as those checks will be done in | |
5256 | pointer_int_sum() anyway. */ | |
5257 | complete_type (TREE_TYPE (res_type)); | |
8f259df3 | 5258 | |
4f2e1536 | 5259 | return pointer_int_sum (loc, resultcode, ptrop, |
cda0a029 | 5260 | intop, complain & tf_warning_or_error); |
8d08fdba MS |
5261 | } |
5262 | ||
5263 | /* Return a tree for the difference of pointers OP0 and OP1. | |
5264 | The resulting tree has type int. */ | |
5265 | ||
5266 | static tree | |
4f2e1536 MP |
5267 | pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype, |
5268 | tsubst_flags_t complain) | |
8d08fdba | 5269 | { |
6de9cd9a | 5270 | tree result; |
8d08fdba | 5271 | tree restype = ptrdiff_type_node; |
79a7c7fa | 5272 | tree target_type = TREE_TYPE (ptrtype); |
8d08fdba | 5273 | |
66543169 | 5274 | if (!complete_type_or_else (target_type, NULL_TREE)) |
8f259df3 MM |
5275 | return error_mark_node; |
5276 | ||
50e10fa8 | 5277 | if (VOID_TYPE_P (target_type)) |
4b978f96 PC |
5278 | { |
5279 | if (complain & tf_error) | |
4f2e1536 | 5280 | permerror (loc, "ISO C++ forbids using pointer of " |
4b978f96 PC |
5281 | "type %<void *%> in subtraction"); |
5282 | else | |
5283 | return error_mark_node; | |
5284 | } | |
fcf73884 | 5285 | if (TREE_CODE (target_type) == FUNCTION_TYPE) |
4b978f96 PC |
5286 | { |
5287 | if (complain & tf_error) | |
4f2e1536 | 5288 | permerror (loc, "ISO C++ forbids using pointer to " |
4b978f96 PC |
5289 | "a function in subtraction"); |
5290 | else | |
5291 | return error_mark_node; | |
5292 | } | |
fcf73884 | 5293 | if (TREE_CODE (target_type) == METHOD_TYPE) |
4b978f96 PC |
5294 | { |
5295 | if (complain & tf_error) | |
4f2e1536 | 5296 | permerror (loc, "ISO C++ forbids using pointer to " |
4b978f96 PC |
5297 | "a method in subtraction"); |
5298 | else | |
5299 | return error_mark_node; | |
5300 | } | |
8d08fdba MS |
5301 | |
5302 | /* First do the subtraction as integers; | |
5303 | then drop through to build the divide operator. */ | |
5304 | ||
4f2e1536 | 5305 | op0 = cp_build_binary_op (loc, |
ba47d38d | 5306 | MINUS_EXPR, |
4b978f96 PC |
5307 | cp_convert (restype, op0, complain), |
5308 | cp_convert (restype, op1, complain), | |
5309 | complain); | |
8d08fdba MS |
5310 | |
5311 | /* This generates an error if op1 is a pointer to an incomplete type. */ | |
d0f062fb | 5312 | if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1)))) |
4b978f96 PC |
5313 | { |
5314 | if (complain & tf_error) | |
4f2e1536 MP |
5315 | error_at (loc, "invalid use of a pointer to an incomplete type in " |
5316 | "pointer arithmetic"); | |
4b978f96 PC |
5317 | else |
5318 | return error_mark_node; | |
5319 | } | |
8d08fdba | 5320 | |
f04dda30 MP |
5321 | if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1))) |
5322 | { | |
5323 | if (complain & tf_error) | |
4f2e1536 | 5324 | error_at (loc, "arithmetic on pointer to an empty aggregate"); |
f04dda30 MP |
5325 | else |
5326 | return error_mark_node; | |
5327 | } | |
5328 | ||
c8094d83 | 5329 | op1 = (TYPE_PTROB_P (ptrtype) |
4f2e1536 | 5330 | ? size_in_bytes_loc (loc, target_type) |
a5ac359a | 5331 | : integer_one_node); |
8d08fdba MS |
5332 | |
5333 | /* Do the division. */ | |
5334 | ||
4f2e1536 MP |
5335 | result = build2_loc (loc, EXACT_DIV_EXPR, restype, op0, |
5336 | cp_convert (restype, op1, complain)); | |
cda0a029 | 5337 | return result; |
8d08fdba MS |
5338 | } |
5339 | \f | |
8d08fdba MS |
5340 | /* Construct and perhaps optimize a tree representation |
5341 | for a unary operation. CODE, a tree_code, specifies the operation | |
5342 | and XARG is the operand. */ | |
5343 | ||
5344 | tree | |
e87eed2a | 5345 | build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg, |
4fe977f2 | 5346 | tsubst_flags_t complain) |
8d08fdba | 5347 | { |
d17811fd | 5348 | tree orig_expr = xarg; |
19420d00 NS |
5349 | tree exp; |
5350 | int ptrmem = 0; | |
fcb9363e | 5351 | tree overload = NULL_TREE; |
c8094d83 | 5352 | |
5156628f | 5353 | if (processing_template_decl) |
d17811fd MM |
5354 | { |
5355 | if (type_dependent_expression_p (xarg)) | |
e87eed2a | 5356 | return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE); |
3601f003 | 5357 | |
d17811fd MM |
5358 | xarg = build_non_dependent_expr (xarg); |
5359 | } | |
5360 | ||
5361 | exp = NULL_TREE; | |
5566b478 | 5362 | |
1e2e9f54 MM |
5363 | /* [expr.unary.op] says: |
5364 | ||
5365 | The address of an object of incomplete type can be taken. | |
5366 | ||
5367 | (And is just the ordinary address operator, not an overloaded | |
5368 | "operator &".) However, if the type is a template | |
5369 | specialization, we must complete the type at this point so that | |
5370 | an overloaded "operator &" will be available if required. */ | |
db5ae43f | 5371 | if (code == ADDR_EXPR |
386b8a85 | 5372 | && TREE_CODE (xarg) != TEMPLATE_ID_EXPR |
1e2e9f54 MM |
5373 | && ((CLASS_TYPE_P (TREE_TYPE (xarg)) |
5374 | && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg)))) | |
db5ae43f | 5375 | || (TREE_CODE (xarg) == OFFSET_REF))) |
f4f206f4 | 5376 | /* Don't look for a function. */; |
db5ae43f | 5377 | else |
4fe977f2 | 5378 | exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE, |
fcb9363e PP |
5379 | NULL_TREE, &overload, complain); |
5380 | ||
d17811fd | 5381 | if (!exp && code == ADDR_EXPR) |
c91a56d2 | 5382 | { |
7e361ae6 JM |
5383 | if (is_overloaded_fn (xarg)) |
5384 | { | |
5385 | tree fn = get_first_fn (xarg); | |
5386 | if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn)) | |
5387 | { | |
76545796 PC |
5388 | if (complain & tf_error) |
5389 | error (DECL_CONSTRUCTOR_P (fn) | |
5390 | ? G_("taking address of constructor %qE") | |
5391 | : G_("taking address of destructor %qE"), | |
e87eed2a | 5392 | xarg.get_value ()); |
7e361ae6 JM |
5393 | return error_mark_node; |
5394 | } | |
5395 | } | |
5396 | ||
5397 | /* A pointer to member-function can be formed only by saying | |
5398 | &X::mf. */ | |
1e14c7f0 GDR |
5399 | if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE |
5400 | && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg))) | |
5401 | { | |
5ebbc0ee NS |
5402 | if (TREE_CODE (xarg) != OFFSET_REF |
5403 | || !TYPE_P (TREE_OPERAND (xarg, 0))) | |
1e14c7f0 | 5404 | { |
76545796 PC |
5405 | if (complain & tf_error) |
5406 | { | |
5407 | error ("invalid use of %qE to form a " | |
e87eed2a | 5408 | "pointer-to-member-function", xarg.get_value ()); |
76545796 PC |
5409 | if (TREE_CODE (xarg) != OFFSET_REF) |
5410 | inform (input_location, " a qualified-id is required"); | |
5411 | } | |
1e14c7f0 GDR |
5412 | return error_mark_node; |
5413 | } | |
5414 | else | |
5415 | { | |
76545796 PC |
5416 | if (complain & tf_error) |
5417 | error ("parentheses around %qE cannot be used to form a" | |
5418 | " pointer-to-member-function", | |
e87eed2a | 5419 | xarg.get_value ()); |
76545796 PC |
5420 | else |
5421 | return error_mark_node; | |
1e14c7f0 GDR |
5422 | PTRMEM_OK_P (xarg) = 1; |
5423 | } | |
5424 | } | |
c8094d83 | 5425 | |
19420d00 | 5426 | if (TREE_CODE (xarg) == OFFSET_REF) |
0cbd7506 MS |
5427 | { |
5428 | ptrmem = PTRMEM_OK_P (xarg); | |
c8094d83 | 5429 | |
0cbd7506 MS |
5430 | if (!ptrmem && !flag_ms_extensions |
5431 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE) | |
4542128e NS |
5432 | { |
5433 | /* A single non-static member, make sure we don't allow a | |
0cbd7506 | 5434 | pointer-to-member. */ |
f293ce4b RS |
5435 | xarg = build2 (OFFSET_REF, TREE_TYPE (xarg), |
5436 | TREE_OPERAND (xarg, 0), | |
5437 | ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE)); | |
4542128e | 5438 | PTRMEM_OK_P (xarg) = ptrmem; |
c8094d83 | 5439 | } |
0cbd7506 | 5440 | } |
93c0e0bb JM |
5441 | |
5442 | exp = cp_build_addr_expr_strict (xarg, complain); | |
c91a56d2 MS |
5443 | } |
5444 | ||
d17811fd | 5445 | if (processing_template_decl && exp != error_mark_node) |
fcb9363e PP |
5446 | { |
5447 | if (overload != NULL_TREE) | |
5448 | return (build_min_non_dep_op_overload | |
5449 | (code, exp, overload, orig_expr, integer_zero_node)); | |
5450 | ||
5451 | exp = build_min_non_dep (code, exp, orig_expr, | |
5452 | /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE); | |
5453 | } | |
6439fffd KL |
5454 | if (TREE_CODE (exp) == ADDR_EXPR) |
5455 | PTRMEM_OK_P (exp) = ptrmem; | |
19420d00 | 5456 | return exp; |
8d08fdba MS |
5457 | } |
5458 | ||
78ef5b89 NB |
5459 | /* Like c_common_truthvalue_conversion, but handle pointer-to-member |
5460 | constants, where a null value is represented by an INTEGER_CST of | |
5461 | -1. */ | |
96d6c610 JM |
5462 | |
5463 | tree | |
5671bf27 | 5464 | cp_truthvalue_conversion (tree expr) |
96d6c610 JM |
5465 | { |
5466 | tree type = TREE_TYPE (expr); | |
451dcc66 | 5467 | if (TYPE_PTR_OR_PTRMEM_P (type) |
9cca4e3d JM |
5468 | /* Avoid ICE on invalid use of non-static member function. */ |
5469 | || TREE_CODE (expr) == FUNCTION_DECL) | |
451dcc66 | 5470 | return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, 1); |
96d6c610 | 5471 | else |
ba47d38d | 5472 | return c_common_truthvalue_conversion (input_location, expr); |
96d6c610 JM |
5473 | } |
5474 | ||
5475 | /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */ | |
c8094d83 | 5476 | |
2986ae00 | 5477 | tree |
5671bf27 | 5478 | condition_conversion (tree expr) |
2986ae00 | 5479 | { |
5566b478 | 5480 | tree t; |
5156628f | 5481 | if (processing_template_decl) |
5566b478 | 5482 | return expr; |
1dad57e6 JM |
5483 | t = perform_implicit_conversion_flags (boolean_type_node, expr, |
5484 | tf_warning_or_error, LOOKUP_NORMAL); | |
0ad28dde | 5485 | t = fold_build_cleanup_point_expr (boolean_type_node, t); |
8ccc31eb | 5486 | return t; |
2986ae00 | 5487 | } |
c8094d83 | 5488 | |
d3e1e89e JM |
5489 | /* Returns the address of T. This function will fold away |
5490 | ADDR_EXPR of INDIRECT_REF. */ | |
7993382e MM |
5491 | |
5492 | tree | |
5493 | build_address (tree t) | |
5494 | { | |
7993382e MM |
5495 | if (error_operand_p (t) || !cxx_mark_addressable (t)) |
5496 | return error_mark_node; | |
f2acb8ad | 5497 | gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR); |
d3e1e89e JM |
5498 | t = build_fold_addr_expr (t); |
5499 | if (TREE_CODE (t) != ADDR_EXPR) | |
5500 | t = rvalue (t); | |
5501 | return t; | |
7993382e MM |
5502 | } |
5503 | ||
5504 | /* Return a NOP_EXPR converting EXPR to TYPE. */ | |
5505 | ||
5506 | tree | |
5507 | build_nop (tree type, tree expr) | |
5508 | { | |
7993382e MM |
5509 | if (type == error_mark_node || error_operand_p (expr)) |
5510 | return expr; | |
e87eed2a | 5511 | return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr); |
7993382e MM |
5512 | } |
5513 | ||
93c0e0bb JM |
5514 | /* Take the address of ARG, whatever that means under C++ semantics. |
5515 | If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues | |
5516 | and class rvalues as well. | |
5517 | ||
5518 | Nothing should call this function directly; instead, callers should use | |
5519 | cp_build_addr_expr or cp_build_addr_expr_strict. */ | |
5520 | ||
5521 | static tree | |
5522 | cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain) | |
5523 | { | |
5524 | tree argtype; | |
5525 | tree val; | |
5526 | ||
5527 | if (!arg || error_operand_p (arg)) | |
5528 | return error_mark_node; | |
5529 | ||
5530 | arg = mark_lvalue_use (arg); | |
5531 | argtype = lvalue_type (arg); | |
5532 | ||
9dc6f476 | 5533 | gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg)); |
93c0e0bb JM |
5534 | |
5535 | if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg) | |
5536 | && !really_overloaded_fn (TREE_OPERAND (arg, 1))) | |
5537 | { | |
5538 | /* They're trying to take the address of a unique non-static | |
5539 | member function. This is ill-formed (except in MS-land), | |
5540 | but let's try to DTRT. | |
5541 | Note: We only handle unique functions here because we don't | |
5542 | want to complain if there's a static overload; non-unique | |
5543 | cases will be handled by instantiate_type. But we need to | |
5544 | handle this case here to allow casts on the resulting PMF. | |
5545 | We could defer this in non-MS mode, but it's easier to give | |
5546 | a useful error here. */ | |
5547 | ||
5548 | /* Inside constant member functions, the `this' pointer | |
5549 | contains an extra const qualifier. TYPE_MAIN_VARIANT | |
5550 | is used here to remove this const from the diagnostics | |
5551 | and the created OFFSET_REF. */ | |
5552 | tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0))); | |
5553 | tree fn = get_first_fn (TREE_OPERAND (arg, 1)); | |
9f635aba PC |
5554 | if (!mark_used (fn, complain) && !(complain & tf_error)) |
5555 | return error_mark_node; | |
93c0e0bb JM |
5556 | |
5557 | if (! flag_ms_extensions) | |
5558 | { | |
5559 | tree name = DECL_NAME (fn); | |
5560 | if (!(complain & tf_error)) | |
5561 | return error_mark_node; | |
5562 | else if (current_class_type | |
5563 | && TREE_OPERAND (arg, 0) == current_class_ref) | |
5564 | /* An expression like &memfn. */ | |
5565 | permerror (input_location, "ISO C++ forbids taking the address of an unqualified" | |
5566 | " or parenthesized non-static member function to form" | |
5567 | " a pointer to member function. Say %<&%T::%D%>", | |
5568 | base, name); | |
5569 | else | |
5570 | permerror (input_location, "ISO C++ forbids taking the address of a bound member" | |
5571 | " function to form a pointer to member function." | |
5572 | " Say %<&%T::%D%>", | |
5573 | base, name); | |
5574 | } | |
a378996b | 5575 | arg = build_offset_ref (base, fn, /*address_p=*/true, complain); |
93c0e0bb JM |
5576 | } |
5577 | ||
5578 | /* Uninstantiated types are all functions. Taking the | |
5579 | address of a function is a no-op, so just return the | |
5580 | argument. */ | |
5581 | if (type_unknown_p (arg)) | |
5582 | return build1 (ADDR_EXPR, unknown_type_node, arg); | |
5583 | ||
5584 | if (TREE_CODE (arg) == OFFSET_REF) | |
5585 | /* We want a pointer to member; bypass all the code for actually taking | |
5586 | the address of something. */ | |
5587 | goto offset_ref; | |
5588 | ||
5589 | /* Anything not already handled and not a true memory reference | |
5590 | is an error. */ | |
5591 | if (TREE_CODE (argtype) != FUNCTION_TYPE | |
5592 | && TREE_CODE (argtype) != METHOD_TYPE) | |
5593 | { | |
4e9ca9b0 JM |
5594 | cp_lvalue_kind kind = lvalue_kind (arg); |
5595 | if (kind == clk_none) | |
93c0e0bb JM |
5596 | { |
5597 | if (complain & tf_error) | |
7bd11157 | 5598 | lvalue_error (input_location, lv_addressof); |
93c0e0bb JM |
5599 | return error_mark_node; |
5600 | } | |
4e9ca9b0 JM |
5601 | if (strict_lvalue && (kind & (clk_rvalueref|clk_class))) |
5602 | { | |
5603 | if (!(complain & tf_error)) | |
5604 | return error_mark_node; | |
5605 | if (kind & clk_class) | |
5606 | /* Make this a permerror because we used to accept it. */ | |
5607 | permerror (input_location, "taking address of temporary"); | |
5608 | else | |
5609 | error ("taking address of xvalue (rvalue reference)"); | |
5610 | } | |
93c0e0bb JM |
5611 | } |
5612 | ||
5613 | if (TREE_CODE (argtype) == REFERENCE_TYPE) | |
5614 | { | |
5615 | tree type = build_pointer_type (TREE_TYPE (argtype)); | |
5616 | arg = build1 (CONVERT_EXPR, type, arg); | |
5617 | return arg; | |
5618 | } | |
5619 | else if (pedantic && DECL_MAIN_P (arg)) | |
5620 | { | |
5621 | /* ARM $3.4 */ | |
5622 | /* Apparently a lot of autoconf scripts for C++ packages do this, | |
c1771a20 | 5623 | so only complain if -Wpedantic. */ |
93c0e0bb | 5624 | if (complain & (flag_pedantic_errors ? tf_error : tf_warning)) |
c1771a20 | 5625 | pedwarn (input_location, OPT_Wpedantic, |
93c0e0bb JM |
5626 | "ISO C++ forbids taking address of function %<::main%>"); |
5627 | else if (flag_pedantic_errors) | |
5628 | return error_mark_node; | |
5629 | } | |
5630 | ||
5631 | /* Let &* cancel out to simplify resulting code. */ | |
591cb3cf | 5632 | if (INDIRECT_REF_P (arg)) |
93c0e0bb | 5633 | { |
93c0e0bb JM |
5634 | arg = TREE_OPERAND (arg, 0); |
5635 | if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE) | |
5636 | { | |
5637 | tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg))); | |
5638 | arg = build1 (CONVERT_EXPR, type, arg); | |
5639 | } | |
5640 | else | |
5641 | /* Don't let this be an lvalue. */ | |
5642 | arg = rvalue (arg); | |
5643 | return arg; | |
5644 | } | |
5645 | ||
5646 | /* ??? Cope with user tricks that amount to offsetof. */ | |
5647 | if (TREE_CODE (argtype) != FUNCTION_TYPE | |
5648 | && TREE_CODE (argtype) != METHOD_TYPE | |
5649 | && argtype != unknown_type_node | |
5650 | && (val = get_base_address (arg)) | |
ccd2b322 | 5651 | && COMPLETE_TYPE_P (TREE_TYPE (val)) |
591cb3cf | 5652 | && INDIRECT_REF_P (val) |
93c0e0bb JM |
5653 | && TREE_CONSTANT (TREE_OPERAND (val, 0))) |
5654 | { | |
5655 | tree type = build_pointer_type (argtype); | |
cf9e9959 | 5656 | return fold_convert (type, fold_offsetof_1 (arg)); |
93c0e0bb JM |
5657 | } |
5658 | ||
5659 | /* Handle complex lvalues (when permitted) | |
5660 | by reduction to simpler cases. */ | |
5661 | val = unary_complex_lvalue (ADDR_EXPR, arg); | |
5662 | if (val != 0) | |
5663 | return val; | |
5664 | ||
5665 | switch (TREE_CODE (arg)) | |
5666 | { | |
5667 | CASE_CONVERT: | |
5668 | case FLOAT_EXPR: | |
5669 | case FIX_TRUNC_EXPR: | |
bb19d4af JM |
5670 | /* We should have handled this above in the lvalue_kind check. */ |
5671 | gcc_unreachable (); | |
93c0e0bb JM |
5672 | break; |
5673 | ||
5674 | case BASELINK: | |
5675 | arg = BASELINK_FUNCTIONS (arg); | |
5676 | /* Fall through. */ | |
5677 | ||
5678 | case OVERLOAD: | |
5679 | arg = OVL_CURRENT (arg); | |
5680 | break; | |
5681 | ||
5682 | case OFFSET_REF: | |
5683 | offset_ref: | |
5684 | /* Turn a reference to a non-static data member into a | |
5685 | pointer-to-member. */ | |
5686 | { | |
5687 | tree type; | |
5688 | tree t; | |
5689 | ||
5690 | gcc_assert (PTRMEM_OK_P (arg)); | |
5691 | ||
5692 | t = TREE_OPERAND (arg, 1); | |
5693 | if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE) | |
5694 | { | |
5695 | if (complain & tf_error) | |
5696 | error ("cannot create pointer to reference member %qD", t); | |
5697 | return error_mark_node; | |
5698 | } | |
5699 | ||
5700 | type = build_ptrmem_type (context_for_name_lookup (t), | |
5701 | TREE_TYPE (t)); | |
5702 | t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1)); | |
5703 | return t; | |
5704 | } | |
5705 | ||
5706 | default: | |
5707 | break; | |
5708 | } | |
5709 | ||
5710 | if (argtype != error_mark_node) | |
94a073b2 | 5711 | argtype = build_pointer_type (argtype); |
93c0e0bb JM |
5712 | |
5713 | /* In a template, we are processing a non-dependent expression | |
5714 | so we can just form an ADDR_EXPR with the correct type. */ | |
5715 | if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF) | |
5716 | { | |
5717 | val = build_address (arg); | |
5718 | if (TREE_CODE (arg) == OFFSET_REF) | |
5719 | PTRMEM_OK_P (val) = PTRMEM_OK_P (arg); | |
5720 | } | |
c5ce25ce | 5721 | else if (BASELINK_P (TREE_OPERAND (arg, 1))) |
93c0e0bb JM |
5722 | { |
5723 | tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1)); | |
5724 | ||
5725 | /* We can only get here with a single static member | |
5726 | function. */ | |
5727 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL | |
5728 | && DECL_STATIC_FUNCTION_P (fn)); | |
9f635aba PC |
5729 | if (!mark_used (fn, complain) && !(complain & tf_error)) |
5730 | return error_mark_node; | |
93c0e0bb JM |
5731 | val = build_address (fn); |
5732 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0))) | |
5733 | /* Do not lose object's side effects. */ | |
5734 | val = build2 (COMPOUND_EXPR, TREE_TYPE (val), | |
5735 | TREE_OPERAND (arg, 0), val); | |
5736 | } | |
5737 | else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))) | |
5738 | { | |
5739 | if (complain & tf_error) | |
5740 | error ("attempt to take address of bit-field structure member %qD", | |
5741 | TREE_OPERAND (arg, 1)); | |
5742 | return error_mark_node; | |
5743 | } | |
5744 | else | |
5745 | { | |
5746 | tree object = TREE_OPERAND (arg, 0); | |
5747 | tree field = TREE_OPERAND (arg, 1); | |
5748 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
5749 | (TREE_TYPE (object), decl_type_context (field))); | |
5750 | val = build_address (arg); | |
5751 | } | |
5752 | ||
50e10fa8 | 5753 | if (TYPE_PTR_P (argtype) |
93c0e0bb JM |
5754 | && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE) |
5755 | { | |
5756 | build_ptrmemfunc_type (argtype); | |
5757 | val = build_ptrmemfunc (argtype, val, 0, | |
5758 | /*c_cast_p=*/false, | |
a3299120 | 5759 | complain); |
93c0e0bb JM |
5760 | } |
5761 | ||
5762 | return val; | |
5763 | } | |
5764 | ||
5765 | /* Take the address of ARG if it has one, even if it's an rvalue. */ | |
5766 | ||
5767 | tree | |
5768 | cp_build_addr_expr (tree arg, tsubst_flags_t complain) | |
5769 | { | |
5770 | return cp_build_addr_expr_1 (arg, 0, complain); | |
5771 | } | |
5772 | ||
5773 | /* Take the address of ARG, but only if it's an lvalue. */ | |
5774 | ||
882de214 | 5775 | static tree |
93c0e0bb JM |
5776 | cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain) |
5777 | { | |
5778 | return cp_build_addr_expr_1 (arg, 1, complain); | |
5779 | } | |
5780 | ||
8d08fdba MS |
5781 | /* C++: Must handle pointers to members. |
5782 | ||
5783 | Perhaps type instantiation should be extended to handle conversion | |
5784 | from aggregates to types we don't yet know we want? (Or are those | |
5785 | cases typically errors which should be reported?) | |
5786 | ||
e51fbec3 | 5787 | NOCONVERT suppresses the default promotions (such as from short to int). */ |
e92cc029 | 5788 | |
8d08fdba | 5789 | tree |
e51fbec3 | 5790 | cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert, |
5ade1ed2 | 5791 | tsubst_flags_t complain) |
8d08fdba MS |
5792 | { |
5793 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
926ce8bd | 5794 | tree arg = xarg; |
9a2300e9 | 5795 | location_t location = EXPR_LOC_OR_LOC (arg, input_location); |
926ce8bd | 5796 | tree argtype = 0; |
d8e178a0 | 5797 | const char *errstring = NULL; |
8d08fdba | 5798 | tree val; |
4de67c26 | 5799 | const char *invalid_op_diag; |
8d08fdba | 5800 | |
fb80065c | 5801 | if (!arg || error_operand_p (arg)) |
8d08fdba MS |
5802 | return error_mark_node; |
5803 | ||
4de67c26 JM |
5804 | if ((invalid_op_diag |
5805 | = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR | |
5806 | ? CONVERT_EXPR | |
5807 | : code), | |
5808 | TREE_TYPE (xarg)))) | |
5809 | { | |
76545796 PC |
5810 | if (complain & tf_error) |
5811 | error (invalid_op_diag); | |
4de67c26 JM |
5812 | return error_mark_node; |
5813 | } | |
5814 | ||
8d08fdba MS |
5815 | switch (code) |
5816 | { | |
392e3d51 | 5817 | case UNARY_PLUS_EXPR: |
b7484fbe | 5818 | case NEGATE_EXPR: |
8a21aa30 KC |
5819 | { |
5820 | int flags = WANT_ARITH | WANT_ENUM; | |
5821 | /* Unary plus (but not unary minus) is allowed on pointers. */ | |
392e3d51 | 5822 | if (code == UNARY_PLUS_EXPR) |
8a21aa30 KC |
5823 | flags |= WANT_POINTER; |
5824 | arg = build_expr_type_conversion (flags, arg, true); | |
e99f332f GB |
5825 | if (!arg) |
5826 | errstring = (code == NEGATE_EXPR | |
4cd5a50a PB |
5827 | ? _("wrong type argument to unary minus") |
5828 | : _("wrong type argument to unary plus")); | |
e99f332f GB |
5829 | else |
5830 | { | |
5831 | if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg))) | |
4b978f96 | 5832 | arg = cp_perform_integral_promotions (arg, complain); |
e99f332f | 5833 | |
13a44ee0 | 5834 | /* Make sure the result is not an lvalue: a unary plus or minus |
e99f332f | 5835 | expression is always a rvalue. */ |
5cc53d4e | 5836 | arg = rvalue (arg); |
e99f332f GB |
5837 | } |
5838 | } | |
8d08fdba MS |
5839 | break; |
5840 | ||
5841 | case BIT_NOT_EXPR: | |
37c46b43 MS |
5842 | if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) |
5843 | { | |
5844 | code = CONJ_EXPR; | |
5845 | if (!noconvert) | |
89fcabaf PC |
5846 | { |
5847 | arg = cp_default_conversion (arg, complain); | |
5848 | if (arg == error_mark_node) | |
5849 | return error_mark_node; | |
5850 | } | |
37c46b43 | 5851 | } |
4576ceaf | 5852 | else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM |
1ff6b2c8 | 5853 | | WANT_VECTOR_OR_COMPLEX, |
b746c5dc | 5854 | arg, true))) |
4cd5a50a | 5855 | errstring = _("wrong type argument to bit-complement"); |
bd289f54 | 5856 | else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg))) |
9a2300e9 MP |
5857 | { |
5858 | /* Warn if the expression has boolean value. */ | |
5859 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE | |
5860 | && warning_at (location, OPT_Wbool_operation, | |
5861 | "%<~%> on an expression of type bool")) | |
5862 | inform (location, "did you mean to use logical not (%<!%>)?"); | |
5863 | arg = cp_perform_integral_promotions (arg, complain); | |
5864 | } | |
8d08fdba MS |
5865 | break; |
5866 | ||
5867 | case ABS_EXPR: | |
b746c5dc | 5868 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) |
4cd5a50a | 5869 | errstring = _("wrong type argument to abs"); |
8d08fdba | 5870 | else if (!noconvert) |
89fcabaf PC |
5871 | { |
5872 | arg = cp_default_conversion (arg, complain); | |
5873 | if (arg == error_mark_node) | |
5874 | return error_mark_node; | |
5875 | } | |
8d08fdba MS |
5876 | break; |
5877 | ||
37c46b43 MS |
5878 | case CONJ_EXPR: |
5879 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
b746c5dc | 5880 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) |
4cd5a50a | 5881 | errstring = _("wrong type argument to conjugation"); |
37c46b43 | 5882 | else if (!noconvert) |
89fcabaf PC |
5883 | { |
5884 | arg = cp_default_conversion (arg, complain); | |
5885 | if (arg == error_mark_node) | |
5886 | return error_mark_node; | |
5887 | } | |
37c46b43 MS |
5888 | break; |
5889 | ||
8d08fdba | 5890 | case TRUTH_NOT_EXPR: |
48d3af12 | 5891 | if (VECTOR_TYPE_P (TREE_TYPE (arg))) |
3c9aabbd MG |
5892 | return cp_build_binary_op (input_location, EQ_EXPR, arg, |
5893 | build_zero_cst (TREE_TYPE (arg)), complain); | |
5ade1ed2 DG |
5894 | arg = perform_implicit_conversion (boolean_type_node, arg, |
5895 | complain); | |
db3927fb | 5896 | val = invert_truthvalue_loc (input_location, arg); |
2986ae00 MS |
5897 | if (arg != error_mark_node) |
5898 | return val; | |
4cd5a50a | 5899 | errstring = _("in argument to unary !"); |
8d08fdba MS |
5900 | break; |
5901 | ||
5902 | case NOP_EXPR: | |
5903 | break; | |
c8094d83 | 5904 | |
37c46b43 | 5905 | case REALPART_EXPR: |
37c46b43 | 5906 | case IMAGPART_EXPR: |
fb52b50a | 5907 | arg = build_real_imag_expr (input_location, code, arg); |
cda0a029 | 5908 | return arg; |
c8094d83 | 5909 | |
8d08fdba MS |
5910 | case PREINCREMENT_EXPR: |
5911 | case POSTINCREMENT_EXPR: | |
5912 | case PREDECREMENT_EXPR: | |
5913 | case POSTDECREMENT_EXPR: | |
5914 | /* Handle complex lvalues (when permitted) | |
5915 | by reduction to simpler cases. */ | |
5916 | ||
5917 | val = unary_complex_lvalue (code, arg); | |
5918 | if (val != 0) | |
5919 | return val; | |
5920 | ||
03a904b5 JJ |
5921 | arg = mark_lvalue_use (arg); |
5922 | ||
37c46b43 MS |
5923 | /* Increment or decrement the real part of the value, |
5924 | and don't change the imaginary part. */ | |
5925 | if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
5926 | { | |
5927 | tree real, imag; | |
5928 | ||
5a0802ea | 5929 | arg = cp_stabilize_reference (arg); |
e51fbec3 MP |
5930 | real = cp_build_unary_op (REALPART_EXPR, arg, true, complain); |
5931 | imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain); | |
5932 | real = cp_build_unary_op (code, real, true, complain); | |
a87db577 AP |
5933 | if (real == error_mark_node || imag == error_mark_node) |
5934 | return error_mark_node; | |
f293ce4b | 5935 | return build2 (COMPLEX_EXPR, TREE_TYPE (arg), |
a87db577 | 5936 | real, imag); |
37c46b43 MS |
5937 | } |
5938 | ||
8d08fdba MS |
5939 | /* Report invalid types. */ |
5940 | ||
b7484fbe | 5941 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER, |
b746c5dc | 5942 | arg, true))) |
8d08fdba MS |
5943 | { |
5944 | if (code == PREINCREMENT_EXPR) | |
4cd5a50a | 5945 | errstring = _("no pre-increment operator for type"); |
8d08fdba | 5946 | else if (code == POSTINCREMENT_EXPR) |
4cd5a50a | 5947 | errstring = _("no post-increment operator for type"); |
8d08fdba | 5948 | else if (code == PREDECREMENT_EXPR) |
4cd5a50a | 5949 | errstring = _("no pre-decrement operator for type"); |
8d08fdba | 5950 | else |
4cd5a50a | 5951 | errstring = _("no post-decrement operator for type"); |
8d08fdba MS |
5952 | break; |
5953 | } | |
affb3cb2 JJ |
5954 | else if (arg == error_mark_node) |
5955 | return error_mark_node; | |
8d08fdba MS |
5956 | |
5957 | /* Report something read-only. */ | |
5958 | ||
91063b51 | 5959 | if (CP_TYPE_CONST_P (TREE_TYPE (arg)) |
5ade1ed2 DG |
5960 | || TREE_READONLY (arg)) |
5961 | { | |
5962 | if (complain & tf_error) | |
4816c593 NF |
5963 | cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR |
5964 | || code == POSTINCREMENT_EXPR) | |
5965 | ? lv_increment : lv_decrement)); | |
5ade1ed2 DG |
5966 | else |
5967 | return error_mark_node; | |
5968 | } | |
8d08fdba MS |
5969 | |
5970 | { | |
926ce8bd | 5971 | tree inc; |
b70cef5d | 5972 | tree declared_type = unlowered_expr_type (arg); |
8d08fdba | 5973 | |
8d08fdba MS |
5974 | argtype = TREE_TYPE (arg); |
5975 | ||
5976 | /* ARM $5.2.5 last annotation says this should be forbidden. */ | |
5977 | if (TREE_CODE (argtype) == ENUMERAL_TYPE) | |
5ade1ed2 DG |
5978 | { |
5979 | if (complain & tf_error) | |
cbe5f3b3 | 5980 | permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
37ec60ed JW |
5981 | ? G_("ISO C++ forbids incrementing an enum") |
5982 | : G_("ISO C++ forbids decrementing an enum")); | |
5ade1ed2 DG |
5983 | else |
5984 | return error_mark_node; | |
5985 | } | |
c8094d83 | 5986 | |
8d08fdba MS |
5987 | /* Compute the increment. */ |
5988 | ||
50e10fa8 | 5989 | if (TYPE_PTR_P (argtype)) |
8d08fdba | 5990 | { |
d0f062fb | 5991 | tree type = complete_type (TREE_TYPE (argtype)); |
c8094d83 | 5992 | |
d0f062fb | 5993 | if (!COMPLETE_OR_VOID_TYPE_P (type)) |
5ade1ed2 DG |
5994 | { |
5995 | if (complain & tf_error) | |
5996 | error (((code == PREINCREMENT_EXPR | |
5997 | || code == POSTINCREMENT_EXPR)) | |
5998 | ? G_("cannot increment a pointer to incomplete type %qT") | |
5999 | : G_("cannot decrement a pointer to incomplete type %qT"), | |
6000 | TREE_TYPE (argtype)); | |
6001 | else | |
6002 | return error_mark_node; | |
6003 | } | |
c42a832a | 6004 | else if (!TYPE_PTROB_P (argtype)) |
5ade1ed2 DG |
6005 | { |
6006 | if (complain & tf_error) | |
44d90fe1 | 6007 | pedwarn (input_location, OPT_Wpointer_arith, |
c42a832a | 6008 | (code == PREINCREMENT_EXPR |
37ec60ed | 6009 | || code == POSTINCREMENT_EXPR) |
c42a832a PC |
6010 | ? G_("ISO C++ forbids incrementing a pointer of type %qT") |
6011 | : G_("ISO C++ forbids decrementing a pointer of type %qT"), | |
6012 | argtype); | |
5ade1ed2 DG |
6013 | else |
6014 | return error_mark_node; | |
6015 | } | |
6016 | ||
ea793912 | 6017 | inc = cxx_sizeof_nowarn (TREE_TYPE (argtype)); |
8d08fdba MS |
6018 | } |
6019 | else | |
241b71bb TV |
6020 | inc = VECTOR_TYPE_P (argtype) |
6021 | ? build_one_cst (argtype) | |
6022 | : integer_one_node; | |
8d08fdba | 6023 | |
4b978f96 | 6024 | inc = cp_convert (argtype, inc, complain); |
8d08fdba | 6025 | |
925e8657 NP |
6026 | /* If 'arg' is an Objective-C PROPERTY_REF expression, then we |
6027 | need to ask Objective-C to build the increment or decrement | |
6028 | expression for it. */ | |
6029 | if (objc_is_property_ref (arg)) | |
6030 | return objc_build_incr_expr_for_property_ref (input_location, code, | |
6031 | arg, inc); | |
6032 | ||
8d08fdba MS |
6033 | /* Complain about anything else that is not a true lvalue. */ |
6034 | if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR | |
6035 | || code == POSTINCREMENT_EXPR) | |
5ade1ed2 DG |
6036 | ? lv_increment : lv_decrement), |
6037 | complain)) | |
8d08fdba MS |
6038 | return error_mark_node; |
6039 | ||
20a1e5b8 | 6040 | /* Forbid using -- or ++ in C++17 on `bool'. */ |
66be89f0 | 6041 | if (TREE_CODE (declared_type) == BOOLEAN_TYPE) |
b7484fbe MS |
6042 | { |
6043 | if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR) | |
6044 | { | |
5ade1ed2 | 6045 | if (complain & tf_error) |
20a1e5b8 MP |
6046 | error ("use of an operand of type %qT in %<operator--%> " |
6047 | "is forbidden", boolean_type_node); | |
b7484fbe MS |
6048 | return error_mark_node; |
6049 | } | |
20a1e5b8 MP |
6050 | else |
6051 | { | |
6052 | if (cxx_dialect >= cxx1z) | |
6053 | { | |
6054 | if (complain & tf_error) | |
6055 | error ("use of an operand of type %qT in " | |
6056 | "%<operator++%> is forbidden in C++1z", | |
6057 | boolean_type_node); | |
6058 | return error_mark_node; | |
6059 | } | |
6060 | /* Otherwise, [depr.incr.bool] says this is deprecated. */ | |
6061 | else if (!in_system_header_at (input_location)) | |
6062 | warning (OPT_Wdeprecated, "use of an operand of type %qT " | |
6063 | "in %<operator++%> is deprecated", | |
6064 | boolean_type_node); | |
6065 | } | |
19552aa5 | 6066 | val = boolean_increment (code, arg); |
b7484fbe | 6067 | } |
9127e994 JM |
6068 | else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR) |
6069 | /* An rvalue has no cv-qualifiers. */ | |
6070 | val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc); | |
b7484fbe | 6071 | else |
f293ce4b | 6072 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
b7484fbe | 6073 | |
8d08fdba | 6074 | TREE_SIDE_EFFECTS (val) = 1; |
b70cef5d | 6075 | return val; |
8d08fdba MS |
6076 | } |
6077 | ||
6078 | case ADDR_EXPR: | |
6079 | /* Note that this operation never does default_conversion | |
6080 | regardless of NOCONVERT. */ | |
93c0e0bb | 6081 | return cp_build_addr_expr (arg, complain); |
7f85441b KG |
6082 | |
6083 | default: | |
6084 | break; | |
8d08fdba MS |
6085 | } |
6086 | ||
6087 | if (!errstring) | |
6088 | { | |
6089 | if (argtype == 0) | |
6090 | argtype = TREE_TYPE (arg); | |
cda0a029 | 6091 | return build1 (code, argtype, arg); |
8d08fdba MS |
6092 | } |
6093 | ||
5ade1ed2 DG |
6094 | if (complain & tf_error) |
6095 | error ("%s", errstring); | |
8d08fdba MS |
6096 | return error_mark_node; |
6097 | } | |
6098 | ||
5ade1ed2 DG |
6099 | /* Hook for the c-common bits that build a unary op. */ |
6100 | tree | |
12308bc6 | 6101 | build_unary_op (location_t /*location*/, |
e51fbec3 | 6102 | enum tree_code code, tree xarg, bool noconvert) |
5ade1ed2 DG |
6103 | { |
6104 | return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error); | |
6105 | } | |
6106 | ||
8d08fdba MS |
6107 | /* Apply unary lvalue-demanding operator CODE to the expression ARG |
6108 | for certain kinds of expressions which are not really lvalues | |
6109 | but which we can accept as lvalues. | |
6110 | ||
dfb5c523 MM |
6111 | If ARG is not a kind of expression we can handle, return |
6112 | NULL_TREE. */ | |
c8094d83 | 6113 | |
8d08fdba | 6114 | tree |
acd8e2d0 | 6115 | unary_complex_lvalue (enum tree_code code, tree arg) |
8d08fdba | 6116 | { |
dfb5c523 MM |
6117 | /* Inside a template, making these kinds of adjustments is |
6118 | pointless; we are only concerned with the type of the | |
6119 | expression. */ | |
6120 | if (processing_template_decl) | |
6121 | return NULL_TREE; | |
6122 | ||
8d08fdba MS |
6123 | /* Handle (a, b) used as an "lvalue". */ |
6124 | if (TREE_CODE (arg) == COMPOUND_EXPR) | |
6125 | { | |
e51fbec3 | 6126 | tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false, |
5ade1ed2 | 6127 | tf_warning_or_error); |
f293ce4b RS |
6128 | return build2 (COMPOUND_EXPR, TREE_TYPE (real_result), |
6129 | TREE_OPERAND (arg, 0), real_result); | |
8d08fdba MS |
6130 | } |
6131 | ||
6132 | /* Handle (a ? b : c) used as an "lvalue". */ | |
e9a25f70 JL |
6133 | if (TREE_CODE (arg) == COND_EXPR |
6134 | || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR) | |
5ade1ed2 | 6135 | return rationalize_conditional_expr (code, arg, tf_warning_or_error); |
a4443a08 | 6136 | |
ae818d3b | 6137 | /* Handle (a = b), (++a), and (--a) used as an "lvalue". */ |
e1cd6e56 MS |
6138 | if (TREE_CODE (arg) == MODIFY_EXPR |
6139 | || TREE_CODE (arg) == PREINCREMENT_EXPR | |
6140 | || TREE_CODE (arg) == PREDECREMENT_EXPR) | |
ae818d3b ER |
6141 | { |
6142 | tree lvalue = TREE_OPERAND (arg, 0); | |
6143 | if (TREE_SIDE_EFFECTS (lvalue)) | |
6144 | { | |
5a0802ea | 6145 | lvalue = cp_stabilize_reference (lvalue); |
f293ce4b RS |
6146 | arg = build2 (TREE_CODE (arg), TREE_TYPE (arg), |
6147 | lvalue, TREE_OPERAND (arg, 1)); | |
ae818d3b ER |
6148 | } |
6149 | return unary_complex_lvalue | |
f293ce4b | 6150 | (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue)); |
ae818d3b | 6151 | } |
8d08fdba MS |
6152 | |
6153 | if (code != ADDR_EXPR) | |
33fd4f49 | 6154 | return NULL_TREE; |
8d08fdba MS |
6155 | |
6156 | /* Handle (a = b) used as an "lvalue" for `&'. */ | |
6157 | if (TREE_CODE (arg) == MODIFY_EXPR | |
6158 | || TREE_CODE (arg) == INIT_EXPR) | |
6159 | { | |
e51fbec3 | 6160 | tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false, |
5ade1ed2 | 6161 | tf_warning_or_error); |
f293ce4b RS |
6162 | arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result), |
6163 | arg, real_result); | |
6de9cd9a | 6164 | TREE_NO_WARNING (arg) = 1; |
faf5394a | 6165 | return arg; |
8d08fdba MS |
6166 | } |
6167 | ||
8d08fdba MS |
6168 | if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE |
6169 | || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE | |
a5ac359a | 6170 | || TREE_CODE (arg) == OFFSET_REF) |
6d05585b | 6171 | return NULL_TREE; |
c8094d83 | 6172 | |
8d08fdba MS |
6173 | /* We permit compiler to make function calls returning |
6174 | objects of aggregate type look like lvalues. */ | |
6175 | { | |
6176 | tree targ = arg; | |
6177 | ||
6178 | if (TREE_CODE (targ) == SAVE_EXPR) | |
6179 | targ = TREE_OPERAND (targ, 0); | |
6180 | ||
9e1e64ec | 6181 | if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ))) |
8d08fdba MS |
6182 | { |
6183 | if (TREE_CODE (arg) == SAVE_EXPR) | |
6184 | targ = arg; | |
6185 | else | |
362115a9 | 6186 | targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error); |
f30432d7 | 6187 | return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ); |
8d08fdba MS |
6188 | } |
6189 | ||
591cb3cf | 6190 | if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ)) |
f293ce4b | 6191 | return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)), |
8d08fdba | 6192 | TREE_OPERAND (targ, 0), current_function_decl, NULL); |
8d08fdba MS |
6193 | } |
6194 | ||
6195 | /* Don't let anything else be handled specially. */ | |
33fd4f49 | 6196 | return NULL_TREE; |
8d08fdba | 6197 | } |
8d08fdba MS |
6198 | \f |
6199 | /* Mark EXP saying that we need to be able to take the | |
6200 | address of it; it should not be allocated in a register. | |
dffd7eb6 | 6201 | Value is true if successful. |
8d08fdba | 6202 | |
4ac14744 | 6203 | C++: we do not allow `current_class_ptr' to be addressable. */ |
8d08fdba | 6204 | |
dffd7eb6 | 6205 | bool |
acd8e2d0 | 6206 | cxx_mark_addressable (tree exp) |
8d08fdba | 6207 | { |
926ce8bd | 6208 | tree x = exp; |
8d08fdba | 6209 | |
8d08fdba MS |
6210 | while (1) |
6211 | switch (TREE_CODE (x)) | |
6212 | { | |
6213 | case ADDR_EXPR: | |
6214 | case COMPONENT_REF: | |
6215 | case ARRAY_REF: | |
37c46b43 MS |
6216 | case REALPART_EXPR: |
6217 | case IMAGPART_EXPR: | |
8d08fdba MS |
6218 | x = TREE_OPERAND (x, 0); |
6219 | break; | |
6220 | ||
6221 | case PARM_DECL: | |
4ac14744 | 6222 | if (x == current_class_ptr) |
8d08fdba | 6223 | { |
0cbd7506 | 6224 | error ("cannot take the address of %<this%>, which is an rvalue expression"); |
f4f206f4 | 6225 | TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */ |
dffd7eb6 | 6226 | return true; |
8d08fdba | 6227 | } |
f4f206f4 | 6228 | /* Fall through. */ |
aa8dea09 | 6229 | |
8d08fdba | 6230 | case VAR_DECL: |
8d08fdba MS |
6231 | /* Caller should not be trying to mark initialized |
6232 | constant fields addressable. */ | |
50bc768d NS |
6233 | gcc_assert (DECL_LANG_SPECIFIC (x) == 0 |
6234 | || DECL_IN_AGGR_P (x) == 0 | |
6235 | || TREE_STATIC (x) | |
6236 | || DECL_EXTERNAL (x)); | |
f4f206f4 | 6237 | /* Fall through. */ |
8d08fdba | 6238 | |
8d08fdba | 6239 | case RESULT_DECL: |
9a3b49ac | 6240 | if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) |
1b8d7c9a | 6241 | && !DECL_ARTIFICIAL (x)) |
8ef36086 | 6242 | { |
5a6ccc94 | 6243 | if (VAR_P (x) && DECL_HARD_REGISTER (x)) |
8ef36086 AP |
6244 | { |
6245 | error | |
6246 | ("address of explicit register variable %qD requested", x); | |
6247 | return false; | |
6248 | } | |
778f6a08 | 6249 | else if (extra_warnings) |
8ef36086 | 6250 | warning |
b323323f | 6251 | (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x); |
8ef36086 | 6252 | } |
8d08fdba | 6253 | TREE_ADDRESSABLE (x) = 1; |
dffd7eb6 | 6254 | return true; |
8d08fdba | 6255 | |
81c1113a | 6256 | case CONST_DECL: |
8d08fdba | 6257 | case FUNCTION_DECL: |
8d08fdba | 6258 | TREE_ADDRESSABLE (x) = 1; |
dffd7eb6 | 6259 | return true; |
8d08fdba | 6260 | |
e92cc029 MS |
6261 | case CONSTRUCTOR: |
6262 | TREE_ADDRESSABLE (x) = 1; | |
dffd7eb6 | 6263 | return true; |
e92cc029 | 6264 | |
9a3b49ac MS |
6265 | case TARGET_EXPR: |
6266 | TREE_ADDRESSABLE (x) = 1; | |
dffd7eb6 NB |
6267 | cxx_mark_addressable (TREE_OPERAND (x, 0)); |
6268 | return true; | |
9a3b49ac | 6269 | |
8d08fdba | 6270 | default: |
dffd7eb6 | 6271 | return true; |
8d08fdba MS |
6272 | } |
6273 | } | |
6274 | \f | |
6275 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. */ | |
6276 | ||
6277 | tree | |
f330f599 | 6278 | build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2, |
5ade1ed2 | 6279 | tsubst_flags_t complain) |
8d08fdba | 6280 | { |
d17811fd MM |
6281 | tree orig_ifexp = ifexp; |
6282 | tree orig_op1 = op1; | |
6283 | tree orig_op2 = op2; | |
6284 | tree expr; | |
6285 | ||
5156628f | 6286 | if (processing_template_decl) |
d17811fd MM |
6287 | { |
6288 | /* The standard says that the expression is type-dependent if | |
6289 | IFEXP is type-dependent, even though the eventual type of the | |
6290 | expression doesn't dependent on IFEXP. */ | |
6291 | if (type_dependent_expression_p (ifexp) | |
18fd68a8 MM |
6292 | /* As a GNU extension, the middle operand may be omitted. */ |
6293 | || (op1 && type_dependent_expression_p (op1)) | |
d17811fd | 6294 | || type_dependent_expression_p (op2)) |
f330f599 | 6295 | return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2); |
d17811fd | 6296 | ifexp = build_non_dependent_expr (ifexp); |
18fd68a8 MM |
6297 | if (op1) |
6298 | op1 = build_non_dependent_expr (op1); | |
d17811fd MM |
6299 | op2 = build_non_dependent_expr (op2); |
6300 | } | |
5566b478 | 6301 | |
4cbc4bd7 | 6302 | expr = build_conditional_expr (loc, ifexp, op1, op2, complain); |
f2111a36 | 6303 | if (processing_template_decl && expr != error_mark_node) |
ca8dc274 JM |
6304 | { |
6305 | tree min = build_min_non_dep (COND_EXPR, expr, | |
6306 | orig_ifexp, orig_op1, orig_op2); | |
92886d3e | 6307 | /* Remember that the result is an lvalue or xvalue. */ |
c3edc633 | 6308 | if (glvalue_p (expr) && !glvalue_p (min)) |
ca8dc274 | 6309 | TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min), |
72b3e203 | 6310 | !lvalue_p (expr)); |
ca8dc274 JM |
6311 | expr = convert_from_reference (min); |
6312 | } | |
d17811fd | 6313 | return expr; |
8d08fdba MS |
6314 | } |
6315 | \f | |
c7b62f14 | 6316 | /* Given a list of expressions, return a compound expression |
04c06002 | 6317 | that performs them all and returns the value of the last of them. */ |
c7b62f14 | 6318 | |
2f5b91f5 | 6319 | tree |
d555b1c7 PC |
6320 | build_x_compound_expr_from_list (tree list, expr_list_kind exp, |
6321 | tsubst_flags_t complain) | |
c7b62f14 NS |
6322 | { |
6323 | tree expr = TREE_VALUE (list); | |
c8094d83 | 6324 | |
d3de8016 JM |
6325 | if (BRACE_ENCLOSED_INITIALIZER_P (expr) |
6326 | && !CONSTRUCTOR_IS_DIRECT_INIT (expr)) | |
6327 | { | |
6328 | if (complain & tf_error) | |
8400e75e DM |
6329 | pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0, |
6330 | "list-initializer for non-class type must not " | |
6331 | "be parenthesized"); | |
d3de8016 JM |
6332 | else |
6333 | return error_mark_node; | |
6334 | } | |
6335 | ||
c7b62f14 NS |
6336 | if (TREE_CHAIN (list)) |
6337 | { | |
d555b1c7 PC |
6338 | if (complain & tf_error) |
6339 | switch (exp) | |
6340 | { | |
2f5b91f5 SZ |
6341 | case ELK_INIT: |
6342 | permerror (input_location, "expression list treated as compound " | |
6343 | "expression in initializer"); | |
6344 | break; | |
6345 | case ELK_MEM_INIT: | |
6346 | permerror (input_location, "expression list treated as compound " | |
6347 | "expression in mem-initializer"); | |
6348 | break; | |
6349 | case ELK_FUNC_CAST: | |
6350 | permerror (input_location, "expression list treated as compound " | |
6351 | "expression in functional cast"); | |
6352 | break; | |
6353 | default: | |
6354 | gcc_unreachable (); | |
d555b1c7 | 6355 | } |
6dab6cf1 JM |
6356 | else |
6357 | return error_mark_node; | |
c7b62f14 NS |
6358 | |
6359 | for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list)) | |
f330f599 PC |
6360 | expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)), |
6361 | expr, TREE_VALUE (list), complain); | |
c7b62f14 | 6362 | } |
c8094d83 | 6363 | |
c7b62f14 NS |
6364 | return expr; |
6365 | } | |
6366 | ||
c166b898 ILT |
6367 | /* Like build_x_compound_expr_from_list, but using a VEC. */ |
6368 | ||
6369 | tree | |
9771b263 | 6370 | build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg, |
e2e03032 | 6371 | tsubst_flags_t complain) |
c166b898 | 6372 | { |
9771b263 | 6373 | if (vec_safe_is_empty (vec)) |
c166b898 | 6374 | return NULL_TREE; |
9771b263 DN |
6375 | else if (vec->length () == 1) |
6376 | return (*vec)[0]; | |
c166b898 ILT |
6377 | else |
6378 | { | |
6379 | tree expr; | |
6380 | unsigned int ix; | |
6381 | tree t; | |
6382 | ||
6383 | if (msg != NULL) | |
e2e03032 PC |
6384 | { |
6385 | if (complain & tf_error) | |
6386 | permerror (input_location, | |
6387 | "%s expression list treated as compound expression", | |
6388 | msg); | |
6389 | else | |
6390 | return error_mark_node; | |
6391 | } | |
c166b898 | 6392 | |
9771b263 DN |
6393 | expr = (*vec)[0]; |
6394 | for (ix = 1; vec->iterate (ix, &t); ++ix) | |
f330f599 | 6395 | expr = build_x_compound_expr (EXPR_LOCATION (t), expr, |
e2e03032 | 6396 | t, complain); |
c166b898 ILT |
6397 | |
6398 | return expr; | |
6399 | } | |
6400 | } | |
6401 | ||
c7b62f14 | 6402 | /* Handle overloading of the ',' operator when needed. */ |
e92cc029 | 6403 | |
8d08fdba | 6404 | tree |
f330f599 PC |
6405 | build_x_compound_expr (location_t loc, tree op1, tree op2, |
6406 | tsubst_flags_t complain) | |
8d08fdba | 6407 | { |
8d08fdba | 6408 | tree result; |
d17811fd MM |
6409 | tree orig_op1 = op1; |
6410 | tree orig_op2 = op2; | |
fcb9363e | 6411 | tree overload = NULL_TREE; |
8d08fdba | 6412 | |
5156628f | 6413 | if (processing_template_decl) |
d17811fd MM |
6414 | { |
6415 | if (type_dependent_expression_p (op1) | |
6416 | || type_dependent_expression_p (op2)) | |
f330f599 | 6417 | return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2); |
d17811fd MM |
6418 | op1 = build_non_dependent_expr (op1); |
6419 | op2 = build_non_dependent_expr (op2); | |
6420 | } | |
b19b4a78 | 6421 | |
f330f599 | 6422 | result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, |
fcb9363e | 6423 | NULL_TREE, &overload, complain); |
d17811fd | 6424 | if (!result) |
525521b6 | 6425 | result = cp_build_compound_expr (op1, op2, complain); |
b19b4a78 | 6426 | |
d17811fd | 6427 | if (processing_template_decl && result != error_mark_node) |
fcb9363e PP |
6428 | { |
6429 | if (overload != NULL_TREE) | |
6430 | return (build_min_non_dep_op_overload | |
6431 | (COMPOUND_EXPR, result, overload, orig_op1, orig_op2)); | |
6432 | ||
6433 | return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2); | |
6434 | } | |
c8094d83 | 6435 | |
d17811fd | 6436 | return result; |
8d08fdba MS |
6437 | } |
6438 | ||
525521b6 DG |
6439 | /* Like cp_build_compound_expr, but for the c-common bits. */ |
6440 | ||
6441 | tree | |
12308bc6 | 6442 | build_compound_expr (location_t /*loc*/, tree lhs, tree rhs) |
525521b6 DG |
6443 | { |
6444 | return cp_build_compound_expr (lhs, rhs, tf_warning_or_error); | |
6445 | } | |
6446 | ||
04c06002 | 6447 | /* Build a compound expression. */ |
8d08fdba MS |
6448 | |
6449 | tree | |
525521b6 | 6450 | cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain) |
8d08fdba | 6451 | { |
ebeb2c24 | 6452 | lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain); |
c8094d83 | 6453 | |
c7b62f14 | 6454 | if (lhs == error_mark_node || rhs == error_mark_node) |
66543169 | 6455 | return error_mark_node; |
c8094d83 | 6456 | |
b72271b9 | 6457 | if (flag_cilkplus |
12893402 BI |
6458 | && (TREE_CODE (lhs) == CILK_SPAWN_STMT |
6459 | || TREE_CODE (rhs) == CILK_SPAWN_STMT)) | |
6460 | { | |
6461 | location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs) | |
6462 | : EXPR_LOCATION (rhs)); | |
6463 | error_at (loc, | |
6464 | "spawned function call cannot be part of a comma expression"); | |
6465 | return error_mark_node; | |
6466 | } | |
6467 | ||
d340e53f NS |
6468 | if (TREE_CODE (rhs) == TARGET_EXPR) |
6469 | { | |
6470 | /* If the rhs is a TARGET_EXPR, then build the compound | |
0cbd7506 | 6471 | expression inside the target_expr's initializer. This |
cd0be382 | 6472 | helps the compiler to eliminate unnecessary temporaries. */ |
d340e53f | 6473 | tree init = TREE_OPERAND (rhs, 1); |
c8094d83 | 6474 | |
f293ce4b | 6475 | init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init); |
d340e53f | 6476 | TREE_OPERAND (rhs, 1) = init; |
c8094d83 | 6477 | |
d340e53f NS |
6478 | return rhs; |
6479 | } | |
c8094d83 | 6480 | |
95e20768 NS |
6481 | if (type_unknown_p (rhs)) |
6482 | { | |
76545796 PC |
6483 | if (complain & tf_error) |
6484 | error ("no context to resolve type of %qE", rhs); | |
95e20768 NS |
6485 | return error_mark_node; |
6486 | } | |
6487 | ||
f293ce4b | 6488 | return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs); |
8d08fdba MS |
6489 | } |
6490 | ||
33c25e5c | 6491 | /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE |
3f89235e JM |
6492 | casts away constness. CAST gives the type of cast. Returns true |
6493 | if the cast is ill-formed, false if it is well-formed. | |
36ef7262 MLI |
6494 | |
6495 | ??? This function warns for casting away any qualifier not just | |
6496 | const. We would like to specify exactly what qualifiers are casted | |
6497 | away. | |
6498 | */ | |
3fe18f1d | 6499 | |
3f89235e | 6500 | static bool |
a5ac359a | 6501 | check_for_casting_away_constness (tree src_type, tree dest_type, |
3f89235e | 6502 | enum tree_code cast, tsubst_flags_t complain) |
3fe18f1d | 6503 | { |
86b7b98b MLI |
6504 | /* C-style casts are allowed to cast away constness. With |
6505 | WARN_CAST_QUAL, we still want to issue a warning. */ | |
6506 | if (cast == CAST_EXPR && !warn_cast_qual) | |
3f89235e | 6507 | return false; |
86b7b98b | 6508 | |
b40e334f | 6509 | if (!casts_away_constness (src_type, dest_type, complain)) |
3f89235e | 6510 | return false; |
36ef7262 MLI |
6511 | |
6512 | switch (cast) | |
6513 | { | |
6514 | case CAST_EXPR: | |
3f89235e JM |
6515 | if (complain & tf_warning) |
6516 | warning (OPT_Wcast_qual, | |
6517 | "cast from type %qT to type %qT casts away qualifiers", | |
6518 | src_type, dest_type); | |
6519 | return false; | |
36ef7262 MLI |
6520 | |
6521 | case STATIC_CAST_EXPR: | |
3f89235e JM |
6522 | if (complain & tf_error) |
6523 | error ("static_cast from type %qT to type %qT casts away qualifiers", | |
6524 | src_type, dest_type); | |
6525 | return true; | |
36ef7262 MLI |
6526 | |
6527 | case REINTERPRET_CAST_EXPR: | |
3f89235e JM |
6528 | if (complain & tf_error) |
6529 | error ("reinterpret_cast from type %qT to type %qT casts away qualifiers", | |
6530 | src_type, dest_type); | |
6531 | return true; | |
6532 | ||
36ef7262 MLI |
6533 | default: |
6534 | gcc_unreachable(); | |
6535 | } | |
3fe18f1d MM |
6536 | } |
6537 | ||
04398fa8 PC |
6538 | /* |
6539 | Warns if the cast from expression EXPR to type TYPE is useless. | |
6540 | */ | |
6541 | void | |
6542 | maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain) | |
6543 | { | |
6544 | if (warn_useless_cast | |
ca7e759d | 6545 | && complain & tf_warning) |
04398fa8 | 6546 | { |
04398fa8 PC |
6547 | if ((TREE_CODE (type) == REFERENCE_TYPE |
6548 | && (TYPE_REF_IS_RVALUE (type) | |
72b3e203 | 6549 | ? xvalue_p (expr) : lvalue_p (expr)) |
04398fa8 PC |
6550 | && same_type_p (TREE_TYPE (expr), TREE_TYPE (type))) |
6551 | || same_type_p (TREE_TYPE (expr), type)) | |
6552 | warning (OPT_Wuseless_cast, "useless cast to type %qT", type); | |
6553 | } | |
6554 | } | |
6555 | ||
08e17d9d MM |
6556 | /* Convert EXPR (an expression with pointer-to-member type) to TYPE |
6557 | (another pointer-to-member type in the same hierarchy) and return | |
6558 | the converted expression. If ALLOW_INVERSE_P is permitted, a | |
6559 | pointer-to-derived may be converted to pointer-to-base; otherwise, | |
6560 | only the other direction is permitted. If C_CAST_P is true, this | |
6561 | conversion is taking place as part of a C-style cast. */ | |
6562 | ||
c8094d83 | 6563 | tree |
08e17d9d | 6564 | convert_ptrmem (tree type, tree expr, bool allow_inverse_p, |
135d47df | 6565 | bool c_cast_p, tsubst_flags_t complain) |
08e17d9d | 6566 | { |
66b1156a | 6567 | if (TYPE_PTRDATAMEM_P (type)) |
08e17d9d MM |
6568 | { |
6569 | tree delta; | |
6570 | ||
6571 | if (TREE_CODE (expr) == PTRMEM_CST) | |
6572 | expr = cplus_expand_constant (expr); | |
6573 | delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)), | |
c8094d83 | 6574 | TYPE_PTRMEM_CLASS_TYPE (type), |
08e17d9d | 6575 | allow_inverse_p, |
135d47df PC |
6576 | c_cast_p, complain); |
6577 | if (delta == error_mark_node) | |
6578 | return error_mark_node; | |
6579 | ||
08e17d9d | 6580 | if (!integer_zerop (delta)) |
6e03b280 OW |
6581 | { |
6582 | tree cond, op1, op2; | |
6583 | ||
ba47d38d AH |
6584 | cond = cp_build_binary_op (input_location, |
6585 | EQ_EXPR, | |
6e03b280 | 6586 | expr, |
5ade1ed2 | 6587 | build_int_cst (TREE_TYPE (expr), -1), |
a3299120 | 6588 | complain); |
6e03b280 | 6589 | op1 = build_nop (ptrdiff_type_node, expr); |
ba47d38d AH |
6590 | op2 = cp_build_binary_op (input_location, |
6591 | PLUS_EXPR, op1, delta, | |
a3299120 | 6592 | complain); |
6e03b280 | 6593 | |
db3927fb AH |
6594 | expr = fold_build3_loc (input_location, |
6595 | COND_EXPR, ptrdiff_type_node, cond, op1, op2); | |
6e03b280 OW |
6596 | |
6597 | } | |
6598 | ||
08e17d9d MM |
6599 | return build_nop (type, expr); |
6600 | } | |
6601 | else | |
c8094d83 | 6602 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, |
135d47df | 6603 | allow_inverse_p, c_cast_p, complain); |
08e17d9d MM |
6604 | } |
6605 | ||
33c25e5c MM |
6606 | /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true, |
6607 | this static_cast is being attempted as one of the possible casts | |
6608 | allowed by a C-style cast. (In that case, accessibility of base | |
6609 | classes is not considered, and it is OK to cast away | |
6610 | constness.) Return the result of the cast. *VALID_P is set to | |
6611 | indicate whether or not the cast was valid. */ | |
3fe18f1d | 6612 | |
33c25e5c MM |
6613 | static tree |
6614 | build_static_cast_1 (tree type, tree expr, bool c_cast_p, | |
5ade1ed2 | 6615 | bool *valid_p, tsubst_flags_t complain) |
a4443a08 | 6616 | { |
00bb3dad | 6617 | tree intype; |
3fe18f1d | 6618 | tree result; |
645c7a55 | 6619 | cp_lvalue_kind clk; |
c11b6f21 | 6620 | |
33c25e5c MM |
6621 | /* Assume the cast is valid. */ |
6622 | *valid_p = true; | |
c11b6f21 | 6623 | |
645c7a55 | 6624 | intype = unlowered_expr_type (expr); |
c11b6f21 | 6625 | |
8d8d1a28 AH |
6626 | /* Save casted types in the function's used types hash table. */ |
6627 | used_types_insert (type); | |
6628 | ||
3fe18f1d | 6629 | /* [expr.static.cast] |
c11b6f21 | 6630 | |
3fe18f1d MM |
6631 | An lvalue of type "cv1 B", where B is a class type, can be cast |
6632 | to type "reference to cv2 D", where D is a class derived (clause | |
6633 | _class.derived_) from B, if a valid standard conversion from | |
6634 | "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the | |
6635 | same cv-qualification as, or greater cv-qualification than, cv1, | |
6636 | and B is not a virtual base class of D. */ | |
0a72704b MM |
6637 | /* We check this case before checking the validity of "TYPE t = |
6638 | EXPR;" below because for this case: | |
6639 | ||
6640 | struct B {}; | |
6641 | struct D : public B { D(const B&); }; | |
6642 | extern B& b; | |
6643 | void f() { static_cast<const D&>(b); } | |
6644 | ||
6645 | we want to avoid constructing a new D. The standard is not | |
6646 | completely clear about this issue, but our interpretation is | |
6647 | consistent with other compilers. */ | |
3fe18f1d MM |
6648 | if (TREE_CODE (type) == REFERENCE_TYPE |
6649 | && CLASS_TYPE_P (TREE_TYPE (type)) | |
6650 | && CLASS_TYPE_P (intype) | |
72b3e203 | 6651 | && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr)) |
3fe18f1d MM |
6652 | && DERIVED_FROM_P (intype, TREE_TYPE (type)) |
6653 | && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)), | |
c8094d83 | 6654 | build_pointer_type (TYPE_MAIN_VARIANT |
b40e334f PC |
6655 | (TREE_TYPE (type))), |
6656 | complain) | |
33c25e5c MM |
6657 | && (c_cast_p |
6658 | || at_least_as_qualified_p (TREE_TYPE (type), intype))) | |
c11b6f21 | 6659 | { |
33c25e5c MM |
6660 | tree base; |
6661 | ||
385bce06 | 6662 | /* There is a standard conversion from "D*" to "B*" even if "B" |
33c25e5c MM |
6663 | is ambiguous or inaccessible. If this is really a |
6664 | static_cast, then we check both for inaccessibility and | |
6665 | ambiguity. However, if this is a static_cast being performed | |
6666 | because the user wrote a C-style cast, then accessibility is | |
6667 | not considered. */ | |
c8094d83 MS |
6668 | base = lookup_base (TREE_TYPE (type), intype, |
6669 | c_cast_p ? ba_unique : ba_check, | |
22854930 | 6670 | NULL, complain); |
35228ac7 JJ |
6671 | expr = build_address (expr); |
6672 | ||
6673 | if (flag_sanitize & SANITIZE_VPTR) | |
6674 | { | |
6675 | tree ubsan_check | |
62775f0d JJ |
6676 | = cp_ubsan_maybe_instrument_downcast (input_location, type, |
6677 | intype, expr); | |
35228ac7 JJ |
6678 | if (ubsan_check) |
6679 | expr = ubsan_check; | |
6680 | } | |
3fe18f1d | 6681 | |
385bce06 MM |
6682 | /* Convert from "B*" to "D*". This function will check that "B" |
6683 | is not a virtual base of "D". */ | |
35228ac7 JJ |
6684 | expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false, |
6685 | complain); | |
6686 | ||
3a73bcc6 | 6687 | /* Convert the pointer to a reference -- but then remember that |
9e115cec JM |
6688 | there are no expressions with reference type in C++. |
6689 | ||
6690 | We call rvalue so that there's an actual tree code | |
6691 | (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand | |
6692 | is a variable with the same type, the conversion would get folded | |
6693 | away, leaving just the variable and causing lvalue_kind to give | |
6694 | the wrong answer. */ | |
6695 | return convert_from_reference (rvalue (cp_fold_convert (type, expr))); | |
c11b6f21 | 6696 | } |
3fe18f1d | 6697 | |
8e3fb544 | 6698 | /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to |
e7f1930f JM |
6699 | cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */ |
6700 | if (TREE_CODE (type) == REFERENCE_TYPE | |
6701 | && TYPE_REF_IS_RVALUE (type) | |
645c7a55 | 6702 | && (clk = real_lvalue_p (expr)) |
e7f1930f JM |
6703 | && reference_related_p (TREE_TYPE (type), intype) |
6704 | && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype))) | |
6705 | { | |
645c7a55 JM |
6706 | if (clk == clk_ordinary) |
6707 | { | |
6708 | /* Handle the (non-bit-field) lvalue case here by casting to | |
6709 | lvalue reference and then changing it to an rvalue reference. | |
6710 | Casting an xvalue to rvalue reference will be handled by the | |
6711 | main code path. */ | |
6712 | tree lref = cp_build_reference_type (TREE_TYPE (type), false); | |
6713 | result = (perform_direct_initialization_if_possible | |
6714 | (lref, expr, c_cast_p, complain)); | |
16dc6b17 | 6715 | result = build1 (NON_LVALUE_EXPR, type, result); |
645c7a55 JM |
6716 | return convert_from_reference (result); |
6717 | } | |
6718 | else | |
6719 | /* For a bit-field or packed field, bind to a temporary. */ | |
6720 | expr = rvalue (expr); | |
e7f1930f JM |
6721 | } |
6722 | ||
7bead48f JM |
6723 | /* Resolve overloaded address here rather than once in |
6724 | implicit_conversion and again in the inverse code below. */ | |
6725 | if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr)) | |
6726 | { | |
6727 | expr = instantiate_type (type, expr, complain); | |
6728 | intype = TREE_TYPE (expr); | |
6729 | } | |
6730 | ||
ca73dc29 MG |
6731 | /* [expr.static.cast] |
6732 | ||
6733 | Any expression can be explicitly converted to type cv void. */ | |
50e10fa8 | 6734 | if (VOID_TYPE_P (type)) |
ca73dc29 MG |
6735 | return convert_to_void (expr, ICV_CAST, complain); |
6736 | ||
2df663cc JM |
6737 | /* [class.abstract] |
6738 | An abstract class shall not be used ... as the type of an explicit | |
6739 | conversion. */ | |
6740 | if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain)) | |
6741 | return error_mark_node; | |
6742 | ||
0a72704b MM |
6743 | /* [expr.static.cast] |
6744 | ||
6745 | An expression e can be explicitly converted to a type T using a | |
6746 | static_cast of the form static_cast<T>(e) if the declaration T | |
6747 | t(e);" is well-formed, for some invented temporary variable | |
6748 | t. */ | |
33c25e5c | 6749 | result = perform_direct_initialization_if_possible (type, expr, |
5ade1ed2 | 6750 | c_cast_p, complain); |
0a72704b | 6751 | if (result) |
109e0040 MM |
6752 | { |
6753 | result = convert_from_reference (result); | |
33c25e5c | 6754 | |
109e0040 MM |
6755 | /* [expr.static.cast] |
6756 | ||
0cbd7506 | 6757 | If T is a reference type, the result is an lvalue; otherwise, |
109e0040 | 6758 | the result is an rvalue. */ |
5cc53d4e | 6759 | if (TREE_CODE (type) != REFERENCE_TYPE) |
0465369f JM |
6760 | { |
6761 | result = rvalue (result); | |
6762 | ||
6763 | if (result == expr && SCALAR_TYPE_P (type)) | |
6764 | /* Leave some record of the cast. */ | |
6765 | result = build_nop (type, expr); | |
6766 | } | |
109e0040 MM |
6767 | return result; |
6768 | } | |
c8094d83 | 6769 | |
3fe18f1d MM |
6770 | /* [expr.static.cast] |
6771 | ||
6772 | The inverse of any standard conversion sequence (clause _conv_), | |
6773 | other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer | |
6774 | (_conv.array_), function-to-pointer (_conv.func_), and boolean | |
6775 | (_conv.bool_) conversions, can be performed explicitly using | |
6776 | static_cast subject to the restriction that the explicit | |
6777 | conversion does not cast away constness (_expr.const.cast_), and | |
6778 | the following additional rules for specific cases: */ | |
6779 | /* For reference, the conversions not excluded are: integral | |
6780 | promotions, floating point promotion, integral conversions, | |
6781 | floating point conversions, floating-integral conversions, | |
6782 | pointer conversions, and pointer to member conversions. */ | |
a10ce2f8 | 6783 | /* DR 128 |
c8094d83 | 6784 | |
a10ce2f8 GK |
6785 | A value of integral _or enumeration_ type can be explicitly |
6786 | converted to an enumeration type. */ | |
6787 | /* The effect of all that is that any conversion between any two | |
6788 | types which are integral, floating, or enumeration types can be | |
6789 | performed. */ | |
550a799d JM |
6790 | if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
6791 | || SCALAR_FLOAT_TYPE_P (type)) | |
6792 | && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype) | |
6793 | || SCALAR_FLOAT_TYPE_P (intype))) | |
4b978f96 | 6794 | return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain); |
33c25e5c | 6795 | |
3fe18f1d MM |
6796 | if (TYPE_PTR_P (type) && TYPE_PTR_P (intype) |
6797 | && CLASS_TYPE_P (TREE_TYPE (type)) | |
6798 | && CLASS_TYPE_P (TREE_TYPE (intype)) | |
c8094d83 MS |
6799 | && can_convert (build_pointer_type (TYPE_MAIN_VARIANT |
6800 | (TREE_TYPE (intype))), | |
6801 | build_pointer_type (TYPE_MAIN_VARIANT | |
b40e334f PC |
6802 | (TREE_TYPE (type))), |
6803 | complain)) | |
999cc24c | 6804 | { |
3fe18f1d MM |
6805 | tree base; |
6806 | ||
3f89235e JM |
6807 | if (!c_cast_p |
6808 | && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR, | |
6809 | complain)) | |
6810 | return error_mark_node; | |
c8094d83 MS |
6811 | base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype), |
6812 | c_cast_p ? ba_unique : ba_check, | |
22854930 | 6813 | NULL, complain); |
a271590a PC |
6814 | expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false, |
6815 | complain); | |
35228ac7 JJ |
6816 | |
6817 | if (flag_sanitize & SANITIZE_VPTR) | |
6818 | { | |
6819 | tree ubsan_check | |
62775f0d JJ |
6820 | = cp_ubsan_maybe_instrument_downcast (input_location, type, |
6821 | intype, expr); | |
35228ac7 JJ |
6822 | if (ubsan_check) |
6823 | expr = ubsan_check; | |
6824 | } | |
6825 | ||
6826 | return cp_fold_convert (type, expr); | |
999cc24c | 6827 | } |
c8094d83 | 6828 | |
66b1156a | 6829 | if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype)) |
3fe18f1d MM |
6830 | || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) |
6831 | { | |
6832 | tree c1; | |
6833 | tree c2; | |
6834 | tree t1; | |
6835 | tree t2; | |
c11b6f21 | 6836 | |
3fe18f1d MM |
6837 | c1 = TYPE_PTRMEM_CLASS_TYPE (intype); |
6838 | c2 = TYPE_PTRMEM_CLASS_TYPE (type); | |
6839 | ||
66b1156a | 6840 | if (TYPE_PTRDATAMEM_P (type)) |
3fe18f1d | 6841 | { |
c8094d83 | 6842 | t1 = (build_ptrmem_type |
3fe18f1d MM |
6843 | (c1, |
6844 | TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype)))); | |
c8094d83 | 6845 | t2 = (build_ptrmem_type |
3fe18f1d MM |
6846 | (c2, |
6847 | TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type)))); | |
6848 | } | |
6849 | else | |
6850 | { | |
6851 | t1 = intype; | |
6852 | t2 = type; | |
6853 | } | |
b40e334f | 6854 | if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain)) |
3fe18f1d | 6855 | { |
3f89235e JM |
6856 | if (!c_cast_p |
6857 | && check_for_casting_away_constness (intype, type, | |
6858 | STATIC_CAST_EXPR, | |
6859 | complain)) | |
6860 | return error_mark_node; | |
08e17d9d | 6861 | return convert_ptrmem (type, expr, /*allow_inverse_p=*/1, |
3f89235e | 6862 | c_cast_p, complain); |
3fe18f1d MM |
6863 | } |
6864 | } | |
c8094d83 | 6865 | |
af7b9902 MM |
6866 | /* [expr.static.cast] |
6867 | ||
3fe18f1d MM |
6868 | An rvalue of type "pointer to cv void" can be explicitly |
6869 | converted to a pointer to object type. A value of type pointer | |
6870 | to object converted to "pointer to cv void" and back to the | |
6871 | original pointer type will have its original value. */ | |
50e10fa8 | 6872 | if (TYPE_PTR_P (intype) |
3fe18f1d MM |
6873 | && VOID_TYPE_P (TREE_TYPE (intype)) |
6874 | && TYPE_PTROB_P (type)) | |
af7b9902 | 6875 | { |
3f89235e JM |
6876 | if (!c_cast_p |
6877 | && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR, | |
6878 | complain)) | |
6879 | return error_mark_node; | |
3fe18f1d | 6880 | return build_nop (type, expr); |
af7b9902 MM |
6881 | } |
6882 | ||
33c25e5c | 6883 | *valid_p = false; |
c11b6f21 | 6884 | return error_mark_node; |
a4443a08 MS |
6885 | } |
6886 | ||
33c25e5c MM |
6887 | /* Return an expression representing static_cast<TYPE>(EXPR). */ |
6888 | ||
cdf5b885 | 6889 | tree |
5ade1ed2 | 6890 | build_static_cast (tree type, tree expr, tsubst_flags_t complain) |
a4443a08 | 6891 | { |
33c25e5c MM |
6892 | tree result; |
6893 | bool valid_p; | |
c11b6f21 MS |
6894 | |
6895 | if (type == error_mark_node || expr == error_mark_node) | |
6896 | return error_mark_node; | |
6897 | ||
5156628f | 6898 | if (processing_template_decl) |
5566b478 | 6899 | { |
33c25e5c MM |
6900 | expr = build_min (STATIC_CAST_EXPR, type, expr); |
6901 | /* We don't know if it will or will not have side effects. */ | |
6902 | TREE_SIDE_EFFECTS (expr) = 1; | |
e8c66fe0 | 6903 | return convert_from_reference (expr); |
5566b478 MS |
6904 | } |
6905 | ||
33c25e5c MM |
6906 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
6907 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ | |
6908 | if (TREE_CODE (type) != REFERENCE_TYPE | |
6909 | && TREE_CODE (expr) == NOP_EXPR | |
6910 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
6911 | expr = TREE_OPERAND (expr, 0); | |
c11b6f21 | 6912 | |
5ade1ed2 DG |
6913 | result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p, |
6914 | complain); | |
33c25e5c | 6915 | if (valid_p) |
04398fa8 PC |
6916 | { |
6917 | if (result != error_mark_node) | |
6918 | maybe_warn_about_useless_cast (type, expr, complain); | |
6919 | return result; | |
6920 | } | |
33c25e5c | 6921 | |
5ade1ed2 DG |
6922 | if (complain & tf_error) |
6923 | error ("invalid static_cast from type %qT to type %qT", | |
6924 | TREE_TYPE (expr), type); | |
33c25e5c MM |
6925 | return error_mark_node; |
6926 | } | |
6927 | ||
6928 | /* EXPR is an expression with member function or pointer-to-member | |
6929 | function type. TYPE is a pointer type. Converting EXPR to TYPE is | |
6930 | not permitted by ISO C++, but we accept it in some modes. If we | |
6931 | are not in one of those modes, issue a diagnostic. Return the | |
6932 | converted expression. */ | |
6933 | ||
6934 | tree | |
89fcabaf | 6935 | convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain) |
33c25e5c MM |
6936 | { |
6937 | tree intype; | |
6938 | tree decl; | |
c11b6f21 | 6939 | |
c11b6f21 | 6940 | intype = TREE_TYPE (expr); |
33c25e5c MM |
6941 | gcc_assert (TYPE_PTRMEMFUNC_P (intype) |
6942 | || TREE_CODE (intype) == METHOD_TYPE); | |
a80e4195 | 6943 | |
89fcabaf PC |
6944 | if (!(complain & tf_warning_or_error)) |
6945 | return error_mark_node; | |
6946 | ||
33c25e5c | 6947 | if (pedantic || warn_pmf2ptr) |
c1771a20 | 6948 | pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions, |
fcf73884 | 6949 | "converting from %qT to %qT", intype, type); |
c8094d83 | 6950 | |
33c25e5c | 6951 | if (TREE_CODE (intype) == METHOD_TYPE) |
89fcabaf | 6952 | expr = build_addr_func (expr, complain); |
33c25e5c MM |
6953 | else if (TREE_CODE (expr) == PTRMEM_CST) |
6954 | expr = build_address (PTRMEM_CST_MEMBER (expr)); | |
6955 | else | |
6956 | { | |
6957 | decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0); | |
6958 | decl = build_address (decl); | |
89fcabaf | 6959 | expr = get_member_function_from_ptrfunc (&decl, expr, complain); |
33c25e5c MM |
6960 | } |
6961 | ||
89fcabaf PC |
6962 | if (expr == error_mark_node) |
6963 | return error_mark_node; | |
6964 | ||
33c25e5c MM |
6965 | return build_nop (type, expr); |
6966 | } | |
6967 | ||
6968 | /* Return a representation for a reinterpret_cast from EXPR to TYPE. | |
6969 | If C_CAST_P is true, this reinterpret cast is being done as part of | |
6970 | a C-style cast. If VALID_P is non-NULL, *VALID_P is set to | |
6971 | indicate whether or not reinterpret_cast was valid. */ | |
6972 | ||
6973 | static tree | |
6974 | build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p, | |
5ade1ed2 | 6975 | bool *valid_p, tsubst_flags_t complain) |
33c25e5c MM |
6976 | { |
6977 | tree intype; | |
6978 | ||
6979 | /* Assume the cast is invalid. */ | |
6980 | if (valid_p) | |
6981 | *valid_p = true; | |
6982 | ||
6983 | if (type == error_mark_node || error_operand_p (expr)) | |
a8c2c492 AP |
6984 | return error_mark_node; |
6985 | ||
33c25e5c MM |
6986 | intype = TREE_TYPE (expr); |
6987 | ||
8d8d1a28 AH |
6988 | /* Save casted types in the function's used types hash table. */ |
6989 | used_types_insert (type); | |
6990 | ||
33c25e5c MM |
6991 | /* [expr.reinterpret.cast] |
6992 | An lvalue expression of type T1 can be cast to the type | |
6993 | "reference to T2" if an expression of type "pointer to T1" can be | |
6994 | explicitly converted to the type "pointer to T2" using a | |
6995 | reinterpret_cast. */ | |
c11b6f21 MS |
6996 | if (TREE_CODE (type) == REFERENCE_TYPE) |
6997 | { | |
72b3e203 | 6998 | if (! lvalue_p (expr)) |
c11b6f21 | 6999 | { |
5ade1ed2 DG |
7000 | if (complain & tf_error) |
7001 | error ("invalid cast of an rvalue expression of type " | |
7002 | "%qT to type %qT", | |
7003 | intype, type); | |
c11b6f21 MS |
7004 | return error_mark_node; |
7005 | } | |
33c25e5c MM |
7006 | |
7007 | /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and | |
7008 | "B" are related class types; the reinterpret_cast does not | |
7009 | adjust the pointer. */ | |
7010 | if (TYPE_PTR_P (intype) | |
5ade1ed2 | 7011 | && (complain & tf_warning) |
96d84882 PB |
7012 | && (comptypes (TREE_TYPE (intype), TREE_TYPE (type), |
7013 | COMPARE_BASE | COMPARE_DERIVED))) | |
d4ee4d25 | 7014 | warning (0, "casting %qT to %qT does not dereference pointer", |
33c25e5c MM |
7015 | intype, type); |
7016 | ||
93c0e0bb | 7017 | expr = cp_build_addr_expr (expr, complain); |
45fe7947 JM |
7018 | |
7019 | if (warn_strict_aliasing > 2) | |
7020 | strict_aliasing_warning (TREE_TYPE (expr), type, expr); | |
7021 | ||
c11b6f21 | 7022 | if (expr != error_mark_node) |
33c25e5c | 7023 | expr = build_reinterpret_cast_1 |
ec7e5618 | 7024 | (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p, |
5ade1ed2 | 7025 | valid_p, complain); |
c11b6f21 | 7026 | if (expr != error_mark_node) |
45fe7947 JM |
7027 | /* cp_build_indirect_ref isn't right for rvalue refs. */ |
7028 | expr = convert_from_reference (fold_convert (type, expr)); | |
c11b6f21 MS |
7029 | return expr; |
7030 | } | |
8ccc31eb | 7031 | |
33c25e5c MM |
7032 | /* As a G++ extension, we consider conversions from member |
7033 | functions, and pointers to member functions to | |
7034 | pointer-to-function and pointer-to-void types. If | |
7035 | -Wno-pmf-conversions has not been specified, | |
7036 | convert_member_func_to_ptr will issue an error message. */ | |
c8094d83 | 7037 | if ((TYPE_PTRMEMFUNC_P (intype) |
33c25e5c MM |
7038 | || TREE_CODE (intype) == METHOD_TYPE) |
7039 | && TYPE_PTR_P (type) | |
7040 | && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
7041 | || VOID_TYPE_P (TREE_TYPE (type)))) | |
89fcabaf | 7042 | return convert_member_func_to_ptr (type, expr, complain); |
33c25e5c | 7043 | |
5acd0bed | 7044 | /* If the cast is not to a reference type, the lvalue-to-rvalue, |
33c25e5c MM |
7045 | array-to-pointer, and function-to-pointer conversions are |
7046 | performed. */ | |
89fcabaf | 7047 | expr = decay_conversion (expr, complain); |
c8094d83 | 7048 | |
33c25e5c MM |
7049 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
7050 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ | |
7051 | if (TREE_CODE (expr) == NOP_EXPR | |
7052 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
7053 | expr = TREE_OPERAND (expr, 0); | |
7054 | ||
7055 | if (error_operand_p (expr)) | |
7056 | return error_mark_node; | |
7057 | ||
7058 | intype = TREE_TYPE (expr); | |
7059 | ||
7060 | /* [expr.reinterpret.cast] | |
7061 | A pointer can be converted to any integral type large enough to | |
14c2101d JM |
7062 | hold it. ... A value of type std::nullptr_t can be converted to |
7063 | an integral type; the conversion has the same meaning and | |
7064 | validity as a conversion of (void*)0 to the integral type. */ | |
7065 | if (CP_INTEGRAL_TYPE_P (type) | |
5116acc6 | 7066 | && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype))) |
8ccc31eb | 7067 | { |
c11b6f21 | 7068 | if (TYPE_PRECISION (type) < TYPE_PRECISION (intype)) |
5ade1ed2 DG |
7069 | { |
7070 | if (complain & tf_error) | |
cbe5f3b3 | 7071 | permerror (input_location, "cast from %qT to %qT loses precision", |
37ec60ed | 7072 | intype, type); |
5ade1ed2 DG |
7073 | else |
7074 | return error_mark_node; | |
7075 | } | |
5116acc6 | 7076 | if (NULLPTR_TYPE_P (intype)) |
14c2101d | 7077 | return build_int_cst (type, 0); |
8ccc31eb | 7078 | } |
33c25e5c MM |
7079 | /* [expr.reinterpret.cast] |
7080 | A value of integral or enumeration type can be explicitly | |
7081 | converted to a pointer. */ | |
7082 | else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype)) | |
7083 | /* OK */ | |
7084 | ; | |
9f88ff5a | 7085 | else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
66b1156a | 7086 | || TYPE_PTR_OR_PTRMEM_P (type)) |
9f88ff5a PC |
7087 | && same_type_p (type, intype)) |
7088 | /* DR 799 */ | |
6d1301f2 | 7089 | return rvalue (expr); |
c11b6f21 MS |
7090 | else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype)) |
7091 | || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) | |
cda0a029 | 7092 | return build_nop (type, expr); |
66b1156a | 7093 | else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype)) |
c11b6f21 | 7094 | || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype))) |
8ccc31eb | 7095 | { |
de9c56a4 RG |
7096 | tree sexpr = expr; |
7097 | ||
3f89235e JM |
7098 | if (!c_cast_p |
7099 | && check_for_casting_away_constness (intype, type, | |
7100 | REINTERPRET_CAST_EXPR, | |
7101 | complain)) | |
7102 | return error_mark_node; | |
33c25e5c MM |
7103 | /* Warn about possible alignment problems. */ |
7104 | if (STRICT_ALIGNMENT && warn_cast_align | |
5ade1ed2 | 7105 | && (complain & tf_warning) |
33c25e5c MM |
7106 | && !VOID_TYPE_P (type) |
7107 | && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE | |
7108 | && COMPLETE_TYPE_P (TREE_TYPE (type)) | |
7109 | && COMPLETE_TYPE_P (TREE_TYPE (intype)) | |
7110 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype))) | |
683d6ff9 MLI |
7111 | warning (OPT_Wcast_align, "cast from %qT to %qT " |
7112 | "increases required alignment of target type", intype, type); | |
c8094d83 | 7113 | |
3b426391 | 7114 | /* We need to strip nops here, because the front end likes to |
de9c56a4 RG |
7115 | create (int *)&a for array-to-pointer decay, instead of &a[0]. */ |
7116 | STRIP_NOPS (sexpr); | |
79bedddc SR |
7117 | if (warn_strict_aliasing <= 2) |
7118 | strict_aliasing_warning (intype, type, sexpr); | |
de9c56a4 | 7119 | |
cda0a029 | 7120 | return build_nop (type, expr); |
8ccc31eb | 7121 | } |
ffb690bd | 7122 | else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype)) |
a06d48ef | 7123 | || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type))) |
ffb690bd | 7124 | { |
dfeadaa0 PC |
7125 | if (complain & tf_warning) |
7126 | /* C++11 5.2.10 p8 says that "Converting a function pointer to an | |
7127 | object pointer type or vice versa is conditionally-supported." */ | |
7128 | warning (OPT_Wconditionally_supported, | |
7129 | "casting between pointer-to-function and pointer-to-object " | |
7130 | "is conditionally-supported"); | |
cda0a029 | 7131 | return build_nop (type, expr); |
ffb690bd | 7132 | } |
b55b02ea | 7133 | else if (VECTOR_TYPE_P (type)) |
cda0a029 | 7134 | return convert_to_vector (type, expr); |
b55b02ea | 7135 | else if (VECTOR_TYPE_P (intype) |
550a799d | 7136 | && INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
cda0a029 | 7137 | return convert_to_integer_nofold (type, expr); |
c11b6f21 | 7138 | else |
8ccc31eb | 7139 | { |
33c25e5c MM |
7140 | if (valid_p) |
7141 | *valid_p = false; | |
5ade1ed2 DG |
7142 | if (complain & tf_error) |
7143 | error ("invalid cast from type %qT to type %qT", intype, type); | |
8ccc31eb MS |
7144 | return error_mark_node; |
7145 | } | |
c8094d83 | 7146 | |
4b978f96 | 7147 | return cp_convert (type, expr, complain); |
a4443a08 MS |
7148 | } |
7149 | ||
cdf5b885 | 7150 | tree |
5ade1ed2 | 7151 | build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain) |
a4443a08 | 7152 | { |
04398fa8 PC |
7153 | tree r; |
7154 | ||
f30432d7 MS |
7155 | if (type == error_mark_node || expr == error_mark_node) |
7156 | return error_mark_node; | |
7157 | ||
5156628f | 7158 | if (processing_template_decl) |
e92cc029 | 7159 | { |
33c25e5c | 7160 | tree t = build_min (REINTERPRET_CAST_EXPR, type, expr); |
c8094d83 | 7161 | |
8e1daa34 NS |
7162 | if (!TREE_SIDE_EFFECTS (t) |
7163 | && type_dependent_expression_p (expr)) | |
7164 | /* There might turn out to be side effects inside expr. */ | |
7165 | TREE_SIDE_EFFECTS (t) = 1; | |
e8c66fe0 | 7166 | return convert_from_reference (t); |
e92cc029 MS |
7167 | } |
7168 | ||
04398fa8 PC |
7169 | r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false, |
7170 | /*valid_p=*/NULL, complain); | |
7171 | if (r != error_mark_node) | |
7172 | maybe_warn_about_useless_cast (type, expr, complain); | |
7173 | return r; | |
33c25e5c MM |
7174 | } |
7175 | ||
7176 | /* Perform a const_cast from EXPR to TYPE. If the cast is valid, | |
7177 | return an appropriate expression. Otherwise, return | |
7178 | error_mark_node. If the cast is not valid, and COMPLAIN is true, | |
7735d402 MM |
7179 | then a diagnostic will be issued. If VALID_P is non-NULL, we are |
7180 | performing a C-style cast, its value upon return will indicate | |
7181 | whether or not the conversion succeeded. */ | |
33c25e5c MM |
7182 | |
7183 | static tree | |
3f89235e | 7184 | build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain, |
33c25e5c MM |
7185 | bool *valid_p) |
7186 | { | |
7187 | tree src_type; | |
7188 | tree reference_type; | |
7189 | ||
7190 | /* Callers are responsible for handling error_mark_node as a | |
7191 | destination type. */ | |
7192 | gcc_assert (dst_type != error_mark_node); | |
7193 | /* In a template, callers should be building syntactic | |
7194 | representations of casts, not using this machinery. */ | |
7195 | gcc_assert (!processing_template_decl); | |
7196 | ||
7197 | /* Assume the conversion is invalid. */ | |
7198 | if (valid_p) | |
7199 | *valid_p = false; | |
7200 | ||
66b1156a | 7201 | if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type)) |
33c25e5c | 7202 | { |
3f89235e | 7203 | if (complain & tf_error) |
33c25e5c MM |
7204 | error ("invalid use of const_cast with type %qT, " |
7205 | "which is not a pointer, " | |
7206 | "reference, nor a pointer-to-data-member type", dst_type); | |
3fd91cbd MM |
7207 | return error_mark_node; |
7208 | } | |
7209 | ||
33c25e5c | 7210 | if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE) |
8ccc31eb | 7211 | { |
3f89235e | 7212 | if (complain & tf_error) |
33c25e5c MM |
7213 | error ("invalid use of const_cast with type %qT, which is a pointer " |
7214 | "or reference to a function type", dst_type); | |
7215 | return error_mark_node; | |
8ccc31eb MS |
7216 | } |
7217 | ||
8d8d1a28 AH |
7218 | /* Save casted types in the function's used types hash table. */ |
7219 | used_types_insert (dst_type); | |
7220 | ||
33c25e5c MM |
7221 | src_type = TREE_TYPE (expr); |
7222 | /* Expressions do not really have reference types. */ | |
7223 | if (TREE_CODE (src_type) == REFERENCE_TYPE) | |
7224 | src_type = TREE_TYPE (src_type); | |
7225 | ||
7226 | /* [expr.const.cast] | |
7227 | ||
66d83eee JM |
7228 | For two object types T1 and T2, if a pointer to T1 can be explicitly |
7229 | converted to the type "pointer to T2" using a const_cast, then the | |
7230 | following conversions can also be made: | |
7231 | ||
7232 | -- an lvalue of type T1 can be explicitly converted to an lvalue of | |
7233 | type T2 using the cast const_cast<T2&>; | |
7234 | ||
7235 | -- a glvalue of type T1 can be explicitly converted to an xvalue of | |
7236 | type T2 using the cast const_cast<T2&&>; and | |
7237 | ||
7238 | -- if T1 is a class type, a prvalue of type T1 can be explicitly | |
7239 | converted to an xvalue of type T2 using the cast const_cast<T2&&>. */ | |
7240 | ||
33c25e5c | 7241 | if (TREE_CODE (dst_type) == REFERENCE_TYPE) |
8ccc31eb | 7242 | { |
33c25e5c | 7243 | reference_type = dst_type; |
66d83eee | 7244 | if (!TYPE_REF_IS_RVALUE (dst_type) |
72b3e203 | 7245 | ? lvalue_p (expr) |
bb19d4af | 7246 | : obvalue_p (expr)) |
66d83eee JM |
7247 | /* OK. */; |
7248 | else | |
8ccc31eb | 7249 | { |
3f89235e | 7250 | if (complain & tf_error) |
33c25e5c MM |
7251 | error ("invalid const_cast of an rvalue of type %qT to type %qT", |
7252 | src_type, dst_type); | |
8ccc31eb MS |
7253 | return error_mark_node; |
7254 | } | |
33c25e5c MM |
7255 | dst_type = build_pointer_type (TREE_TYPE (dst_type)); |
7256 | src_type = build_pointer_type (src_type); | |
7257 | } | |
7258 | else | |
7259 | { | |
7260 | reference_type = NULL_TREE; | |
7261 | /* If the destination type is not a reference type, the | |
7262 | lvalue-to-rvalue, array-to-pointer, and function-to-pointer | |
7263 | conversions are performed. */ | |
7264 | src_type = type_decays_to (src_type); | |
7265 | if (src_type == error_mark_node) | |
7266 | return error_mark_node; | |
7267 | } | |
8ccc31eb | 7268 | |
66b1156a | 7269 | if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type)) |
33c25e5c | 7270 | { |
2d4e2a68 | 7271 | if (comp_ptr_ttypes_const (dst_type, src_type)) |
c2840dfb | 7272 | { |
2d4e2a68 PC |
7273 | if (valid_p) |
7274 | { | |
7275 | *valid_p = true; | |
7276 | /* This cast is actually a C-style cast. Issue a warning if | |
7277 | the user is making a potentially unsafe cast. */ | |
7278 | check_for_casting_away_constness (src_type, dst_type, | |
7279 | CAST_EXPR, complain); | |
7280 | } | |
7281 | if (reference_type) | |
7282 | { | |
7283 | expr = cp_build_addr_expr (expr, complain); | |
89fcabaf PC |
7284 | if (expr == error_mark_node) |
7285 | return error_mark_node; | |
2d4e2a68 PC |
7286 | expr = build_nop (reference_type, expr); |
7287 | return convert_from_reference (expr); | |
7288 | } | |
7289 | else | |
7290 | { | |
89fcabaf PC |
7291 | expr = decay_conversion (expr, complain); |
7292 | if (expr == error_mark_node) | |
7293 | return error_mark_node; | |
7294 | ||
2d4e2a68 PC |
7295 | /* build_c_cast puts on a NOP_EXPR to make the result not an |
7296 | lvalue. Strip such NOP_EXPRs if VALUE is being used in | |
7297 | non-lvalue context. */ | |
7298 | if (TREE_CODE (expr) == NOP_EXPR | |
7299 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
7300 | expr = TREE_OPERAND (expr, 0); | |
7301 | return build_nop (dst_type, expr); | |
7302 | } | |
33c25e5c | 7303 | } |
2d4e2a68 PC |
7304 | else if (valid_p |
7305 | && !at_least_as_qualified_p (TREE_TYPE (dst_type), | |
7306 | TREE_TYPE (src_type))) | |
7307 | check_for_casting_away_constness (src_type, dst_type, CAST_EXPR, | |
7308 | complain); | |
8ccc31eb MS |
7309 | } |
7310 | ||
3f89235e | 7311 | if (complain & tf_error) |
c8094d83 | 7312 | error ("invalid const_cast from type %qT to type %qT", |
33c25e5c | 7313 | src_type, dst_type); |
c11b6f21 | 7314 | return error_mark_node; |
a4443a08 MS |
7315 | } |
7316 | ||
33c25e5c | 7317 | tree |
5ade1ed2 | 7318 | build_const_cast (tree type, tree expr, tsubst_flags_t complain) |
33c25e5c | 7319 | { |
04398fa8 PC |
7320 | tree r; |
7321 | ||
71bd7186 | 7322 | if (type == error_mark_node || error_operand_p (expr)) |
33c25e5c | 7323 | return error_mark_node; |
6060a796 | 7324 | |
33c25e5c MM |
7325 | if (processing_template_decl) |
7326 | { | |
7327 | tree t = build_min (CONST_CAST_EXPR, type, expr); | |
c8094d83 | 7328 | |
33c25e5c MM |
7329 | if (!TREE_SIDE_EFFECTS (t) |
7330 | && type_dependent_expression_p (expr)) | |
7331 | /* There might turn out to be side effects inside expr. */ | |
7332 | TREE_SIDE_EFFECTS (t) = 1; | |
e8c66fe0 | 7333 | return convert_from_reference (t); |
33c25e5c MM |
7334 | } |
7335 | ||
04398fa8 PC |
7336 | r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL); |
7337 | if (r != error_mark_node) | |
7338 | maybe_warn_about_useless_cast (type, expr, complain); | |
7339 | return r; | |
33c25e5c MM |
7340 | } |
7341 | ||
525521b6 DG |
7342 | /* Like cp_build_c_cast, but for the c-common bits. */ |
7343 | ||
7344 | tree | |
12308bc6 | 7345 | build_c_cast (location_t /*loc*/, tree type, tree expr) |
525521b6 DG |
7346 | { |
7347 | return cp_build_c_cast (type, expr, tf_warning_or_error); | |
7348 | } | |
7349 | ||
e87eed2a DM |
7350 | /* Like the "build_c_cast" used for c-common, but using cp_expr to |
7351 | preserve location information even for tree nodes that don't | |
7352 | support it. */ | |
7353 | ||
7354 | cp_expr | |
7355 | build_c_cast (location_t loc, tree type, cp_expr expr) | |
7356 | { | |
7357 | cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error); | |
7358 | result.set_location (loc); | |
7359 | return result; | |
7360 | } | |
7361 | ||
33c25e5c MM |
7362 | /* Build an expression representing an explicit C-style cast to type |
7363 | TYPE of expression EXPR. */ | |
8d08fdba MS |
7364 | |
7365 | tree | |
525521b6 | 7366 | cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain) |
8d08fdba | 7367 | { |
926ce8bd | 7368 | tree value = expr; |
33c25e5c MM |
7369 | tree result; |
7370 | bool valid_p; | |
8d08fdba | 7371 | |
33c25e5c | 7372 | if (type == error_mark_node || error_operand_p (expr)) |
8d08fdba MS |
7373 | return error_mark_node; |
7374 | ||
0949f723 NS |
7375 | if (processing_template_decl) |
7376 | { | |
7377 | tree t = build_min (CAST_EXPR, type, | |
7378 | tree_cons (NULL_TREE, value, NULL_TREE)); | |
04c06002 | 7379 | /* We don't know if it will or will not have side effects. */ |
8e1daa34 | 7380 | TREE_SIDE_EFFECTS (t) = 1; |
e8c66fe0 | 7381 | return convert_from_reference (t); |
0949f723 NS |
7382 | } |
7383 | ||
2e2da467 ZL |
7384 | /* Casts to a (pointer to a) specific ObjC class (or 'id' or |
7385 | 'Class') should always be retained, because this information aids | |
7386 | in method lookup. */ | |
7387 | if (objc_is_object_ptr (type) | |
7388 | && objc_is_object_ptr (TREE_TYPE (expr))) | |
7389 | return build_nop (type, expr); | |
7390 | ||
8d08fdba | 7391 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
8ccc31eb MS |
7392 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ |
7393 | if (TREE_CODE (type) != REFERENCE_TYPE | |
7394 | && TREE_CODE (value) == NOP_EXPR | |
8d08fdba MS |
7395 | && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0))) |
7396 | value = TREE_OPERAND (value, 0); | |
7397 | ||
8d08fdba MS |
7398 | if (TREE_CODE (type) == ARRAY_TYPE) |
7399 | { | |
7400 | /* Allow casting from T1* to T2[] because Cfront allows it. | |
cab1f180 | 7401 | NIHCL uses it. It is not valid ISO C++ however. */ |
50e10fa8 | 7402 | if (TYPE_PTR_P (TREE_TYPE (expr))) |
8d08fdba | 7403 | { |
5ade1ed2 | 7404 | if (complain & tf_error) |
cbe5f3b3 | 7405 | permerror (input_location, "ISO C++ forbids casting to an array type %qT", type); |
5ade1ed2 DG |
7406 | else |
7407 | return error_mark_node; | |
8d08fdba MS |
7408 | type = build_pointer_type (TREE_TYPE (type)); |
7409 | } | |
7410 | else | |
7411 | { | |
5ade1ed2 DG |
7412 | if (complain & tf_error) |
7413 | error ("ISO C++ forbids casting to an array type %qT", type); | |
8d08fdba MS |
7414 | return error_mark_node; |
7415 | } | |
7416 | } | |
7417 | ||
a0a33927 MS |
7418 | if (TREE_CODE (type) == FUNCTION_TYPE |
7419 | || TREE_CODE (type) == METHOD_TYPE) | |
7420 | { | |
5ade1ed2 DG |
7421 | if (complain & tf_error) |
7422 | error ("invalid cast to function type %qT", type); | |
a0a33927 MS |
7423 | return error_mark_node; |
7424 | } | |
7425 | ||
50e10fa8 | 7426 | if (TYPE_PTR_P (type) |
dc8d2739 MLI |
7427 | && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE |
7428 | /* Casting to an integer of smaller size is an error detected elsewhere. */ | |
7429 | && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value)) | |
7430 | /* Don't warn about converting any constant. */ | |
7431 | && !TREE_CONSTANT (value)) | |
7432 | warning_at (input_location, OPT_Wint_to_pointer_cast, | |
7433 | "cast to pointer from integer of different size"); | |
7434 | ||
33c25e5c | 7435 | /* A C-style cast can be a const_cast. */ |
3f89235e | 7436 | result = build_const_cast_1 (type, value, complain & tf_warning, |
33c25e5c MM |
7437 | &valid_p); |
7438 | if (valid_p) | |
04398fa8 PC |
7439 | { |
7440 | if (result != error_mark_node) | |
7441 | maybe_warn_about_useless_cast (type, value, complain); | |
7442 | return result; | |
7443 | } | |
8d08fdba | 7444 | |
33c25e5c MM |
7445 | /* Or a static cast. */ |
7446 | result = build_static_cast_1 (type, value, /*c_cast_p=*/true, | |
5ade1ed2 | 7447 | &valid_p, complain); |
33c25e5c MM |
7448 | /* Or a reinterpret_cast. */ |
7449 | if (!valid_p) | |
7450 | result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true, | |
5ade1ed2 | 7451 | &valid_p, complain); |
33c25e5c MM |
7452 | /* The static_cast or reinterpret_cast may be followed by a |
7453 | const_cast. */ | |
c8094d83 | 7454 | if (valid_p |
33c25e5c | 7455 | /* A valid cast may result in errors if, for example, a |
04398fa8 | 7456 | conversion to an ambiguous base class is required. */ |
33c25e5c | 7457 | && !error_operand_p (result)) |
e6e174e5 | 7458 | { |
33c25e5c | 7459 | tree result_type; |
8ccc31eb | 7460 | |
04398fa8 PC |
7461 | maybe_warn_about_useless_cast (type, value, complain); |
7462 | ||
33c25e5c MM |
7463 | /* Non-class rvalues always have cv-unqualified type. */ |
7464 | if (!CLASS_TYPE_P (type)) | |
7465 | type = TYPE_MAIN_VARIANT (type); | |
7466 | result_type = TREE_TYPE (result); | |
8e3fb544 | 7467 | if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE) |
33c25e5c MM |
7468 | result_type = TYPE_MAIN_VARIANT (result_type); |
7469 | /* If the type of RESULT does not match TYPE, perform a | |
7470 | const_cast to make it match. If the static_cast or | |
7471 | reinterpret_cast succeeded, we will differ by at most | |
7472 | cv-qualification, so the follow-on const_cast is guaranteed | |
7473 | to succeed. */ | |
7474 | if (!same_type_p (non_reference (type), non_reference (result_type))) | |
e6e174e5 | 7475 | { |
33c25e5c MM |
7476 | result = build_const_cast_1 (type, result, false, &valid_p); |
7477 | gcc_assert (valid_p); | |
8d08fdba | 7478 | } |
33c25e5c | 7479 | return result; |
8d08fdba MS |
7480 | } |
7481 | ||
33c25e5c | 7482 | return error_mark_node; |
8d08fdba MS |
7483 | } |
7484 | \f | |
5ade1ed2 DG |
7485 | /* For use from the C common bits. */ |
7486 | tree | |
4f2e1536 | 7487 | build_modify_expr (location_t location, |
12308bc6 | 7488 | tree lhs, tree /*lhs_origtype*/, |
c2255bc4 | 7489 | enum tree_code modifycode, |
12308bc6 PC |
7490 | location_t /*rhs_location*/, tree rhs, |
7491 | tree /*rhs_origtype*/) | |
5ade1ed2 | 7492 | { |
4f2e1536 MP |
7493 | return cp_build_modify_expr (location, lhs, modifycode, rhs, |
7494 | tf_warning_or_error); | |
5ade1ed2 DG |
7495 | } |
7496 | ||
8d08fdba MS |
7497 | /* Build an assignment expression of lvalue LHS from value RHS. |
7498 | MODIFYCODE is the code for a binary operator that we use | |
7499 | to combine the old value of LHS with RHS to get the new value. | |
7500 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. | |
7501 | ||
e92cc029 MS |
7502 | C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */ |
7503 | ||
8d08fdba | 7504 | tree |
4f2e1536 MP |
7505 | cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, |
7506 | tree rhs, tsubst_flags_t complain) | |
8d08fdba | 7507 | { |
926ce8bd | 7508 | tree result; |
8d08fdba MS |
7509 | tree newrhs = rhs; |
7510 | tree lhstype = TREE_TYPE (lhs); | |
7511 | tree olhstype = lhstype; | |
487a92fe | 7512 | bool plain_assign = (modifycode == NOP_EXPR); |
8d08fdba | 7513 | |
8d08fdba | 7514 | /* Avoid duplicate error messages from operands that had errors. */ |
1bd229b7 | 7515 | if (error_operand_p (lhs) || error_operand_p (rhs)) |
8d08fdba MS |
7516 | return error_mark_node; |
7517 | ||
a25bd9e6 JM |
7518 | /* Handle control structure constructs used as "lvalues". Note that we |
7519 | leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */ | |
8d08fdba MS |
7520 | switch (TREE_CODE (lhs)) |
7521 | { | |
f4f206f4 | 7522 | /* Handle --foo = 5; as these are valid constructs in C++. */ |
8d08fdba MS |
7523 | case PREDECREMENT_EXPR: |
7524 | case PREINCREMENT_EXPR: | |
7525 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))) | |
f293ce4b | 7526 | lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs), |
5a0802ea | 7527 | cp_stabilize_reference (TREE_OPERAND (lhs, 0)), |
f293ce4b | 7528 | TREE_OPERAND (lhs, 1)); |
a25bd9e6 JM |
7529 | lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0)); |
7530 | break; | |
8d08fdba | 7531 | |
a4443a08 | 7532 | case MODIFY_EXPR: |
a65cddcf | 7533 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))) |
f293ce4b | 7534 | lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs), |
5a0802ea | 7535 | cp_stabilize_reference (TREE_OPERAND (lhs, 0)), |
f293ce4b | 7536 | TREE_OPERAND (lhs, 1)); |
a25bd9e6 JM |
7537 | lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0)); |
7538 | break; | |
a4443a08 | 7539 | |
d211a298 RS |
7540 | case MIN_EXPR: |
7541 | case MAX_EXPR: | |
7542 | /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues, | |
7543 | when neither operand has side-effects. */ | |
5ade1ed2 | 7544 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
d211a298 RS |
7545 | return error_mark_node; |
7546 | ||
7547 | gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)) | |
7548 | && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1))); | |
7549 | ||
7550 | lhs = build3 (COND_EXPR, TREE_TYPE (lhs), | |
7551 | build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR, | |
7552 | boolean_type_node, | |
7553 | TREE_OPERAND (lhs, 0), | |
7554 | TREE_OPERAND (lhs, 1)), | |
7555 | TREE_OPERAND (lhs, 0), | |
7556 | TREE_OPERAND (lhs, 1)); | |
81fea426 | 7557 | gcc_fallthrough (); |
d211a298 | 7558 | |
8d08fdba MS |
7559 | /* Handle (a ? b : c) used as an "lvalue". */ |
7560 | case COND_EXPR: | |
8d08fdba MS |
7561 | { |
7562 | /* Produce (a ? (b = rhs) : (c = rhs)) | |
7563 | except that the RHS goes through a save-expr | |
7564 | so the code to compute it is only emitted once. */ | |
df39af7d | 7565 | tree cond; |
a77a9a18 | 7566 | tree preeval = NULL_TREE; |
df39af7d | 7567 | |
df3473fa JJ |
7568 | if (VOID_TYPE_P (TREE_TYPE (rhs))) |
7569 | { | |
5ade1ed2 DG |
7570 | if (complain & tf_error) |
7571 | error ("void value not ignored as it ought to be"); | |
df3473fa JJ |
7572 | return error_mark_node; |
7573 | } | |
7574 | ||
a77a9a18 | 7575 | rhs = stabilize_expr (rhs, &preeval); |
c8094d83 | 7576 | |
df39af7d JM |
7577 | /* Check this here to avoid odd errors when trying to convert |
7578 | a throw to the type of the COND_EXPR. */ | |
5ade1ed2 | 7579 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
df39af7d JM |
7580 | return error_mark_node; |
7581 | ||
7582 | cond = build_conditional_expr | |
4cbc4bd7 | 7583 | (input_location, TREE_OPERAND (lhs, 0), |
4f2e1536 | 7584 | cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1), |
5ade1ed2 | 7585 | modifycode, rhs, complain), |
4f2e1536 | 7586 | cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2), |
5ade1ed2 DG |
7587 | modifycode, rhs, complain), |
7588 | complain); | |
df39af7d | 7589 | |
bd6dd845 | 7590 | if (cond == error_mark_node) |
8d08fdba MS |
7591 | return cond; |
7592 | /* Make sure the code to compute the rhs comes out | |
7593 | before the split. */ | |
6de9cd9a | 7594 | if (preeval) |
f293ce4b | 7595 | cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond); |
6de9cd9a | 7596 | return cond; |
8d08fdba | 7597 | } |
c8094d83 | 7598 | |
7f85441b KG |
7599 | default: |
7600 | break; | |
8d08fdba MS |
7601 | } |
7602 | ||
8d08fdba MS |
7603 | if (modifycode == INIT_EXPR) |
7604 | { | |
4ea08463 JM |
7605 | if (BRACE_ENCLOSED_INITIALIZER_P (rhs)) |
7606 | /* Do the default thing. */; | |
7607 | else if (TREE_CODE (rhs) == CONSTRUCTOR) | |
f30efcb7 | 7608 | { |
4ea08463 | 7609 | /* Compound literal. */ |
182609b5 JM |
7610 | if (! same_type_p (TREE_TYPE (rhs), lhstype)) |
7611 | /* Call convert to generate an error; see PR 11063. */ | |
7612 | rhs = convert (lhstype, rhs); | |
f293ce4b | 7613 | result = build2 (INIT_EXPR, lhstype, lhs, rhs); |
f30efcb7 JM |
7614 | TREE_SIDE_EFFECTS (result) = 1; |
7615 | return result; | |
7616 | } | |
9e1e64ec | 7617 | else if (! MAYBE_CLASS_TYPE_P (lhstype)) |
f4f206f4 | 7618 | /* Do the default thing. */; |
db5ae43f | 7619 | else |
8d08fdba | 7620 | { |
9771b263 | 7621 | vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs); |
4ba126e4 | 7622 | result = build_special_member_call (lhs, complete_ctor_identifier, |
c166b898 | 7623 | &rhs_vec, lhstype, LOOKUP_NORMAL, |
5ade1ed2 | 7624 | complain); |
c166b898 | 7625 | release_tree_vector (rhs_vec); |
8d08fdba MS |
7626 | if (result == NULL_TREE) |
7627 | return error_mark_node; | |
7628 | return result; | |
7629 | } | |
7630 | } | |
a56ca899 | 7631 | else |
8d08fdba | 7632 | { |
79fe346e | 7633 | lhs = require_complete_type_sfinae (lhs, complain); |
a56ca899 NS |
7634 | if (lhs == error_mark_node) |
7635 | return error_mark_node; | |
7636 | ||
7637 | if (modifycode == NOP_EXPR) | |
c91a56d2 | 7638 | { |
668ea4b1 IS |
7639 | if (c_dialect_objc ()) |
7640 | { | |
46a88c12 | 7641 | result = objc_maybe_build_modify_expr (lhs, rhs); |
668ea4b1 IS |
7642 | if (result) |
7643 | return result; | |
7644 | } | |
7645 | ||
a56ca899 | 7646 | /* `operator=' is not an inheritable operator. */ |
9e1e64ec | 7647 | if (! MAYBE_CLASS_TYPE_P (lhstype)) |
f4f206f4 | 7648 | /* Do the default thing. */; |
a56ca899 NS |
7649 | else |
7650 | { | |
4fe977f2 PC |
7651 | result = build_new_op (input_location, MODIFY_EXPR, |
7652 | LOOKUP_NORMAL, lhs, rhs, | |
7653 | make_node (NOP_EXPR), /*overload=*/NULL, | |
5ade1ed2 | 7654 | complain); |
a56ca899 NS |
7655 | if (result == NULL_TREE) |
7656 | return error_mark_node; | |
7657 | return result; | |
7658 | } | |
7659 | lhstype = olhstype; | |
7660 | } | |
7661 | else | |
7662 | { | |
4063e61b JM |
7663 | tree init = NULL_TREE; |
7664 | ||
a56ca899 | 7665 | /* A binary op has been requested. Combine the old LHS |
0cbd7506 MS |
7666 | value with the RHS producing the value we should actually |
7667 | store into the LHS. */ | |
9e1e64ec PC |
7668 | gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE |
7669 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype))) | |
7670 | || MAYBE_CLASS_TYPE_P (lhstype))); | |
a56ca899 | 7671 | |
4063e61b JM |
7672 | /* Preevaluate the RHS to make sure its evaluation is complete |
7673 | before the lvalue-to-rvalue conversion of the LHS: | |
7674 | ||
7675 | [expr.ass] With respect to an indeterminately-sequenced | |
7676 | function call, the operation of a compound assignment is a | |
7677 | single evaluation. [ Note: Therefore, a function call shall | |
7678 | not intervene between the lvalue-to-rvalue conversion and the | |
7679 | side effect associated with any single compound assignment | |
7680 | operator. -- end note ] */ | |
5a0802ea | 7681 | lhs = cp_stabilize_reference (lhs); |
30f6b784 | 7682 | rhs = rvalue (rhs); |
4063e61b | 7683 | rhs = stabilize_expr (rhs, &init); |
4f2e1536 | 7684 | newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain); |
a56ca899 NS |
7685 | if (newrhs == error_mark_node) |
7686 | { | |
5ade1ed2 DG |
7687 | if (complain & tf_error) |
7688 | error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode, | |
7689 | TREE_TYPE (lhs), TREE_TYPE (rhs)); | |
a56ca899 NS |
7690 | return error_mark_node; |
7691 | } | |
c8094d83 | 7692 | |
4063e61b JM |
7693 | if (init) |
7694 | newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs); | |
7695 | ||
a56ca899 NS |
7696 | /* Now it looks like a plain assignment. */ |
7697 | modifycode = NOP_EXPR; | |
668ea4b1 IS |
7698 | if (c_dialect_objc ()) |
7699 | { | |
46a88c12 | 7700 | result = objc_maybe_build_modify_expr (lhs, newrhs); |
668ea4b1 IS |
7701 | if (result) |
7702 | return result; | |
7703 | } | |
c91a56d2 | 7704 | } |
50bc768d NS |
7705 | gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE); |
7706 | gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE); | |
8d08fdba MS |
7707 | } |
7708 | ||
719f407a | 7709 | /* The left-hand side must be an lvalue. */ |
5ade1ed2 | 7710 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
8d08fdba MS |
7711 | return error_mark_node; |
7712 | ||
a56ca899 NS |
7713 | /* Warn about modifying something that is `const'. Don't warn if |
7714 | this is initialization. */ | |
8d08fdba | 7715 | if (modifycode != INIT_EXPR |
91063b51 | 7716 | && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype) |
69851283 MM |
7717 | /* Functions are not modifiable, even though they are |
7718 | lvalues. */ | |
7719 | || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE | |
a56ca899 NS |
7720 | || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE |
7721 | /* If it's an aggregate and any field is const, then it is | |
7722 | effectively const. */ | |
5552b43c | 7723 | || (CLASS_TYPE_P (lhstype) |
a56ca899 | 7724 | && C_TYPE_FIELDS_READONLY (lhstype)))) |
5ade1ed2 DG |
7725 | { |
7726 | if (complain & tf_error) | |
4816c593 | 7727 | cxx_readonly_error (lhs, lv_assign); |
5ade1ed2 DG |
7728 | else |
7729 | return error_mark_node; | |
7730 | } | |
8d08fdba | 7731 | |
41b81065 JM |
7732 | /* If storing into a structure or union member, it may have been given a |
7733 | lowered bitfield type. We need to convert to the declared type first, | |
7734 | so retrieve it now. */ | |
8d08fdba | 7735 | |
41b81065 | 7736 | olhstype = unlowered_expr_type (lhs); |
8d08fdba | 7737 | |
8d08fdba MS |
7738 | /* Convert new value to destination type. */ |
7739 | ||
7740 | if (TREE_CODE (lhstype) == ARRAY_TYPE) | |
7741 | { | |
f376e137 | 7742 | int from_array; |
c8094d83 | 7743 | |
5666bb32 | 7744 | if (BRACE_ENCLOSED_INITIALIZER_P (newrhs)) |
84526801 | 7745 | { |
eaa9d009 JM |
7746 | if (modifycode != INIT_EXPR) |
7747 | { | |
7748 | if (complain & tf_error) | |
7749 | error ("assigning to an array from an initializer list"); | |
7750 | return error_mark_node; | |
7751 | } | |
5666bb32 | 7752 | if (check_array_initializer (lhs, lhstype, newrhs)) |
84526801 | 7753 | return error_mark_node; |
754af126 | 7754 | newrhs = digest_init (lhstype, newrhs, complain); |
625f85e9 PC |
7755 | if (newrhs == error_mark_node) |
7756 | return error_mark_node; | |
84526801 | 7757 | } |
25357d1e | 7758 | |
82b3da6a PC |
7759 | /* C++11 8.5/17: "If the destination type is an array of characters, |
7760 | an array of char16_t, an array of char32_t, or an array of wchar_t, | |
7761 | and the initializer is a string literal...". */ | |
7762 | else if (TREE_CODE (newrhs) == STRING_CST | |
7763 | && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype))) | |
7764 | && modifycode == INIT_EXPR) | |
7765 | { | |
7766 | newrhs = digest_init (lhstype, newrhs, complain); | |
7767 | if (newrhs == error_mark_node) | |
7768 | return error_mark_node; | |
7769 | } | |
7770 | ||
25357d1e | 7771 | else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype), |
5666bb32 | 7772 | TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))) |
44a8d0b3 | 7773 | { |
5ade1ed2 DG |
7774 | if (complain & tf_error) |
7775 | error ("incompatible types in assignment of %qT to %qT", | |
7776 | TREE_TYPE (rhs), lhstype); | |
44a8d0b3 MS |
7777 | return error_mark_node; |
7778 | } | |
7779 | ||
39211cd5 | 7780 | /* Allow array assignment in compiler-generated code. */ |
25357d1e | 7781 | else if (!current_function_decl |
5e242863 | 7782 | || !DECL_DEFAULTED_FN (current_function_decl)) |
cf24598a | 7783 | { |
decebe51 GDR |
7784 | /* This routine is used for both initialization and assignment. |
7785 | Make sure the diagnostic message differentiates the context. */ | |
5ade1ed2 DG |
7786 | if (complain & tf_error) |
7787 | { | |
7788 | if (modifycode == INIT_EXPR) | |
7789 | error ("array used as initializer"); | |
7790 | else | |
7791 | error ("invalid array assignment"); | |
7792 | } | |
cf24598a MM |
7793 | return error_mark_node; |
7794 | } | |
39211cd5 | 7795 | |
f376e137 | 7796 | from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE |
0cbd7506 | 7797 | ? 1 + (modifycode != INIT_EXPR): 0; |
3db45ab5 | 7798 | return build_vec_init (lhs, NULL_TREE, newrhs, |
844ae01d | 7799 | /*explicit_value_init_p=*/false, |
5ade1ed2 | 7800 | from_array, complain); |
8d08fdba MS |
7801 | } |
7802 | ||
7803 | if (modifycode == INIT_EXPR) | |
e57d93c6 JM |
7804 | /* Calls with INIT_EXPR are all direct-initialization, so don't set |
7805 | LOOKUP_ONLYCONVERTING. */ | |
41b81065 | 7806 | newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL, |
2f5b91f5 | 7807 | ICR_INIT, NULL_TREE, 0, |
5ade1ed2 | 7808 | complain); |
8d08fdba | 7809 | else |
2f5b91f5 | 7810 | newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN, |
e57d93c6 | 7811 | NULL_TREE, 0, complain, LOOKUP_IMPLICIT); |
41b81065 JM |
7812 | |
7813 | if (!same_type_p (lhstype, olhstype)) | |
4b978f96 | 7814 | newrhs = cp_convert_and_check (lhstype, newrhs, complain); |
41b81065 JM |
7815 | |
7816 | if (modifycode != INIT_EXPR) | |
8d08fdba | 7817 | { |
b7484fbe MS |
7818 | if (TREE_CODE (newrhs) == CALL_EXPR |
7819 | && TYPE_NEEDS_CONSTRUCTING (lhstype)) | |
362115a9 | 7820 | newrhs = build_cplus_new (lhstype, newrhs, complain); |
b7484fbe | 7821 | |
e8abc66f | 7822 | /* Can't initialize directly from a TARGET_EXPR, since that would |
07674418 MS |
7823 | cause the lhs to be constructed twice, and possibly result in |
7824 | accidental self-initialization. So we force the TARGET_EXPR to be | |
be99da77 | 7825 | expanded without a target. */ |
b7484fbe | 7826 | if (TREE_CODE (newrhs) == TARGET_EXPR) |
f293ce4b RS |
7827 | newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs, |
7828 | TREE_OPERAND (newrhs, 0)); | |
8d08fdba MS |
7829 | } |
7830 | ||
bd6dd845 | 7831 | if (newrhs == error_mark_node) |
8d08fdba MS |
7832 | return error_mark_node; |
7833 | ||
e58a9aa1 ZL |
7834 | if (c_dialect_objc () && flag_objc_gc) |
7835 | { | |
7836 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
7837 | ||
7838 | if (result) | |
7839 | return result; | |
7840 | } | |
7841 | ||
f293ce4b RS |
7842 | result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR, |
7843 | lhstype, lhs, newrhs); | |
2ee887f2 | 7844 | |
8d08fdba | 7845 | TREE_SIDE_EFFECTS (result) = 1; |
487a92fe JM |
7846 | if (!plain_assign) |
7847 | TREE_NO_WARNING (result) = 1; | |
8d08fdba | 7848 | |
41b81065 | 7849 | return result; |
8d08fdba MS |
7850 | } |
7851 | ||
e87eed2a | 7852 | cp_expr |
f330f599 PC |
7853 | build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, |
7854 | tree rhs, tsubst_flags_t complain) | |
5566b478 | 7855 | { |
fcb9363e PP |
7856 | tree orig_lhs = lhs; |
7857 | tree orig_rhs = rhs; | |
7858 | tree overload = NULL_TREE; | |
7859 | tree op = build_nt (modifycode, NULL_TREE, NULL_TREE); | |
7860 | ||
5156628f | 7861 | if (processing_template_decl) |
fcb9363e PP |
7862 | { |
7863 | if (modifycode == NOP_EXPR | |
7864 | || type_dependent_expression_p (lhs) | |
7865 | || type_dependent_expression_p (rhs)) | |
7866 | return build_min_nt_loc (loc, MODOP_EXPR, lhs, | |
7867 | build_min_nt_loc (loc, modifycode, NULL_TREE, | |
7868 | NULL_TREE), rhs); | |
7869 | ||
7870 | lhs = build_non_dependent_expr (lhs); | |
7871 | rhs = build_non_dependent_expr (rhs); | |
7872 | } | |
5566b478 MS |
7873 | |
7874 | if (modifycode != NOP_EXPR) | |
7875 | { | |
fcb9363e PP |
7876 | tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, |
7877 | lhs, rhs, op, &overload, complain); | |
5566b478 | 7878 | if (rval) |
487a92fe | 7879 | { |
fcb9363e PP |
7880 | if (rval == error_mark_node) |
7881 | return rval; | |
487a92fe | 7882 | TREE_NO_WARNING (rval) = 1; |
fcb9363e PP |
7883 | if (processing_template_decl) |
7884 | { | |
7885 | if (overload != NULL_TREE) | |
7886 | return (build_min_non_dep_op_overload | |
7887 | (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs)); | |
7888 | ||
7889 | return (build_min_non_dep | |
7890 | (MODOP_EXPR, rval, orig_lhs, op, orig_rhs)); | |
7891 | } | |
487a92fe JM |
7892 | return rval; |
7893 | } | |
5566b478 | 7894 | } |
4f2e1536 | 7895 | return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain); |
5566b478 | 7896 | } |
8d08fdba | 7897 | |
6e03b280 OW |
7898 | /* Helper function for get_delta_difference which assumes FROM is a base |
7899 | class of TO. Returns a delta for the conversion of pointer-to-member | |
135d47df PC |
7900 | of FROM to pointer-to-member of TO. If the conversion is invalid and |
7901 | tf_error is not set in COMPLAIN returns error_mark_node, otherwise | |
6e03b280 | 7902 | returns zero. If FROM is not a base class of TO, returns NULL_TREE. |
135d47df PC |
7903 | If C_CAST_P is true, this conversion is taking place as part of a |
7904 | C-style cast. */ | |
6e03b280 OW |
7905 | |
7906 | static tree | |
135d47df PC |
7907 | get_delta_difference_1 (tree from, tree to, bool c_cast_p, |
7908 | tsubst_flags_t complain) | |
6e03b280 OW |
7909 | { |
7910 | tree binfo; | |
7911 | base_kind kind; | |
135d47df | 7912 | |
22854930 PC |
7913 | binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check, |
7914 | &kind, complain); | |
135d47df | 7915 | |
22854930 | 7916 | if (binfo == error_mark_node) |
6e03b280 | 7917 | { |
135d47df PC |
7918 | if (!(complain & tf_error)) |
7919 | return error_mark_node; | |
7920 | ||
6e03b280 OW |
7921 | error (" in pointer to member function conversion"); |
7922 | return size_zero_node; | |
7923 | } | |
7924 | else if (binfo) | |
7925 | { | |
7926 | if (kind != bk_via_virtual) | |
7927 | return BINFO_OFFSET (binfo); | |
7928 | else | |
7929 | /* FROM is a virtual base class of TO. Issue an error or warning | |
7930 | depending on whether or not this is a reinterpret cast. */ | |
7931 | { | |
135d47df PC |
7932 | if (!(complain & tf_error)) |
7933 | return error_mark_node; | |
7934 | ||
6e03b280 OW |
7935 | error ("pointer to member conversion via virtual base %qT", |
7936 | BINFO_TYPE (binfo_from_vbase (binfo))); | |
7937 | ||
7938 | return size_zero_node; | |
7939 | } | |
7940 | } | |
135d47df PC |
7941 | else |
7942 | return NULL_TREE; | |
6e03b280 OW |
7943 | } |
7944 | ||
ddd5a7c1 | 7945 | /* Get difference in deltas for different pointer to member function |
135d47df PC |
7946 | types. If the conversion is invalid and tf_error is not set in |
7947 | COMPLAIN, returns error_mark_node, otherwise returns an integer | |
7948 | constant of type PTRDIFF_TYPE_NODE and its value is zero if the | |
7949 | conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse | |
7950 | conversions as well. If C_CAST_P is true this conversion is taking | |
7951 | place as part of a C-style cast. | |
3927874d JM |
7952 | |
7953 | Note that the naming of FROM and TO is kind of backwards; the return | |
7954 | value is what we add to a TO in order to get a FROM. They are named | |
7955 | this way because we call this function to find out how to convert from | |
7956 | a pointer to member of FROM to a pointer to member of TO. */ | |
e92cc029 | 7957 | |
51c184be | 7958 | static tree |
c8094d83 | 7959 | get_delta_difference (tree from, tree to, |
08e17d9d | 7960 | bool allow_inverse_p, |
135d47df | 7961 | bool c_cast_p, tsubst_flags_t complain) |
51c184be | 7962 | { |
e8632777 MM |
7963 | tree result; |
7964 | ||
6e03b280 OW |
7965 | if (same_type_ignoring_top_level_qualifiers_p (from, to)) |
7966 | /* Pointer to member of incomplete class is permitted*/ | |
7967 | result = size_zero_node; | |
f879d298 | 7968 | else |
135d47df PC |
7969 | result = get_delta_difference_1 (from, to, c_cast_p, complain); |
7970 | ||
7971 | if (result == error_mark_node) | |
7972 | return error_mark_node; | |
c8094d83 | 7973 | |
6e03b280 OW |
7974 | if (!result) |
7975 | { | |
7976 | if (!allow_inverse_p) | |
7977 | { | |
135d47df PC |
7978 | if (!(complain & tf_error)) |
7979 | return error_mark_node; | |
7980 | ||
6e03b280 OW |
7981 | error_not_base_type (from, to); |
7982 | error (" in pointer to member conversion"); | |
135d47df | 7983 | result = size_zero_node; |
6e03b280 OW |
7984 | } |
7985 | else | |
7986 | { | |
135d47df PC |
7987 | result = get_delta_difference_1 (to, from, c_cast_p, complain); |
7988 | ||
7989 | if (result == error_mark_node) | |
7990 | return error_mark_node; | |
6e03b280 OW |
7991 | |
7992 | if (result) | |
db3927fb | 7993 | result = size_diffop_loc (input_location, |
135d47df | 7994 | size_zero_node, result); |
6e03b280 OW |
7995 | else |
7996 | { | |
135d47df PC |
7997 | if (!(complain & tf_error)) |
7998 | return error_mark_node; | |
7999 | ||
6e03b280 OW |
8000 | error_not_base_type (from, to); |
8001 | error (" in pointer to member conversion"); | |
8002 | result = size_zero_node; | |
8003 | } | |
8004 | } | |
8005 | } | |
32e02ee0 | 8006 | |
cda0a029 | 8007 | return convert_to_integer (ptrdiff_type_node, result); |
51c184be MS |
8008 | } |
8009 | ||
c7e266a6 MM |
8010 | /* Return a constructor for the pointer-to-member-function TYPE using |
8011 | the other components as specified. */ | |
8012 | ||
e08a8f45 | 8013 | tree |
acd8e2d0 | 8014 | build_ptrmemfunc1 (tree type, tree delta, tree pfn) |
594740f3 | 8015 | { |
f78c7bc6 | 8016 | tree u = NULL_TREE; |
c7e266a6 | 8017 | tree delta_field; |
c7e266a6 | 8018 | tree pfn_field; |
9771b263 | 8019 | vec<constructor_elt, va_gc> *v; |
594740f3 | 8020 | |
c7e266a6 | 8021 | /* Pull the FIELD_DECLs out of the type. */ |
1f84ec23 | 8022 | pfn_field = TYPE_FIELDS (type); |
910ad8de | 8023 | delta_field = DECL_CHAIN (pfn_field); |
594740f3 | 8024 | |
c7e266a6 | 8025 | /* Make sure DELTA has the type we want. */ |
68fca595 | 8026 | delta = convert_and_check (input_location, delta_type_node, delta); |
594740f3 | 8027 | |
a489b1f0 RG |
8028 | /* Convert to the correct target type if necessary. */ |
8029 | pfn = fold_convert (TREE_TYPE (pfn_field), pfn); | |
8030 | ||
c7e266a6 | 8031 | /* Finish creating the initializer. */ |
9771b263 | 8032 | vec_alloc (v, 2); |
4038c495 GB |
8033 | CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn); |
8034 | CONSTRUCTOR_APPEND_ELT(v, delta_field, delta); | |
8035 | u = build_constructor (type, v); | |
6de9cd9a | 8036 | TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta); |
530ec96d MM |
8037 | TREE_STATIC (u) = (TREE_CONSTANT (u) |
8038 | && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn)) | |
8039 | != NULL_TREE) | |
c8094d83 | 8040 | && (initializer_constant_valid_p (delta, TREE_TYPE (delta)) |
530ec96d | 8041 | != NULL_TREE)); |
594740f3 | 8042 | return u; |
594740f3 MS |
8043 | } |
8044 | ||
8d08fdba MS |
8045 | /* Build a constructor for a pointer to member function. It can be |
8046 | used to initialize global variables, local variable, or used | |
8047 | as a value in expressions. TYPE is the POINTER to METHOD_TYPE we | |
8048 | want to be. | |
8049 | ||
838dfd8a | 8050 | If FORCE is nonzero, then force this conversion, even if |
8d08fdba | 8051 | we would rather not do it. Usually set when using an explicit |
08e17d9d | 8052 | cast. A C-style cast is being processed iff C_CAST_P is true. |
51c184be MS |
8053 | |
8054 | Return error_mark_node, if something goes wrong. */ | |
8d08fdba MS |
8055 | |
8056 | tree | |
135d47df PC |
8057 | build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p, |
8058 | tsubst_flags_t complain) | |
8d08fdba | 8059 | { |
e08a8f45 | 8060 | tree fn; |
7993382e MM |
8061 | tree pfn_type; |
8062 | tree to_type; | |
8063 | ||
8064 | if (error_operand_p (pfn)) | |
8065 | return error_mark_node; | |
8066 | ||
8067 | pfn_type = TREE_TYPE (pfn); | |
8068 | to_type = build_ptrmemfunc_type (type); | |
b928a651 | 8069 | |
e92cc029 | 8070 | /* Handle multiple conversions of pointer to member functions. */ |
7993382e | 8071 | if (TYPE_PTRMEMFUNC_P (pfn_type)) |
8d08fdba | 8072 | { |
530ec96d | 8073 | tree delta = NULL_TREE; |
7133357a | 8074 | tree npfn = NULL_TREE; |
1f84ec23 | 8075 | tree n; |
594740f3 | 8076 | |
c8094d83 | 8077 | if (!force |
b40e334f PC |
8078 | && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, |
8079 | LOOKUP_NORMAL, complain)) | |
76545796 PC |
8080 | { |
8081 | if (complain & tf_error) | |
8082 | error ("invalid conversion to type %qT from type %qT", | |
8083 | to_type, pfn_type); | |
8084 | else | |
8085 | return error_mark_node; | |
8086 | } | |
9ca21c0a | 8087 | |
3f2b640a JM |
8088 | n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type), |
8089 | TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type), | |
08e17d9d | 8090 | force, |
135d47df PC |
8091 | c_cast_p, complain); |
8092 | if (n == error_mark_node) | |
8093 | return error_mark_node; | |
3f2b640a | 8094 | |
c7e266a6 MM |
8095 | /* We don't have to do any conversion to convert a |
8096 | pointer-to-member to its own type. But, we don't want to | |
8097 | just return a PTRMEM_CST if there's an explicit cast; that | |
8098 | cast should make the expression an invalid template argument. */ | |
3f2b640a JM |
8099 | if (TREE_CODE (pfn) != PTRMEM_CST) |
8100 | { | |
8101 | if (same_type_p (to_type, pfn_type)) | |
8102 | return pfn; | |
8103 | else if (integer_zerop (n)) | |
5ade1ed2 | 8104 | return build_reinterpret_cast (to_type, pfn, |
a3299120 | 8105 | complain); |
3f2b640a | 8106 | } |
c7e266a6 | 8107 | |
7def1251 JM |
8108 | if (TREE_SIDE_EFFECTS (pfn)) |
8109 | pfn = save_expr (pfn); | |
8110 | ||
530ec96d MM |
8111 | /* Obtain the function pointer and the current DELTA. */ |
8112 | if (TREE_CODE (pfn) == PTRMEM_CST) | |
18ae7f63 | 8113 | expand_ptrmemfunc_cst (pfn, &delta, &npfn); |
530ec96d MM |
8114 | else |
8115 | { | |
50ad9642 MM |
8116 | npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier); |
8117 | delta = build_ptrmemfunc_access_expr (pfn, delta_identifier); | |
530ec96d MM |
8118 | } |
8119 | ||
3461fba7 | 8120 | /* Just adjust the DELTA field. */ |
50bc768d NS |
8121 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
8122 | (TREE_TYPE (delta), ptrdiff_type_node)); | |
cb7fdde2 | 8123 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta) |
ba47d38d AH |
8124 | n = cp_build_binary_op (input_location, |
8125 | LSHIFT_EXPR, n, integer_one_node, | |
a3299120 | 8126 | complain); |
ba47d38d | 8127 | delta = cp_build_binary_op (input_location, |
a3299120 | 8128 | PLUS_EXPR, delta, n, complain); |
18ae7f63 | 8129 | return build_ptrmemfunc1 (to_type, delta, npfn); |
8d08fdba | 8130 | } |
51c184be | 8131 | |
e92cc029 | 8132 | /* Handle null pointer to member function conversions. */ |
33c2474d | 8133 | if (null_ptr_cst_p (pfn)) |
51c184be | 8134 | { |
f61af651 | 8135 | pfn = cp_build_c_cast (type, pfn, complain); |
b928a651 | 8136 | return build_ptrmemfunc1 (to_type, |
c8094d83 | 8137 | integer_zero_node, |
18ae7f63 | 8138 | pfn); |
51c184be MS |
8139 | } |
8140 | ||
e6e174e5 | 8141 | if (type_unknown_p (pfn)) |
a3299120 | 8142 | return instantiate_type (type, pfn, complain); |
a0a33927 | 8143 | |
e08a8f45 | 8144 | fn = TREE_OPERAND (pfn, 0); |
37048601 NS |
8145 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL |
8146 | /* In a template, we will have preserved the | |
8147 | OFFSET_REF. */ | |
8148 | || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF)); | |
b928a651 | 8149 | return make_ptrmem_cst (to_type, fn); |
e08a8f45 | 8150 | } |
8d08fdba | 8151 | |
18ae7f63 | 8152 | /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST |
fed3cef0 RK |
8153 | given by CST. |
8154 | ||
8155 | ??? There is no consistency as to the types returned for the above | |
852dcbdd | 8156 | values. Some code acts as if it were a sizetype and some as if it were |
fed3cef0 | 8157 | integer_type_node. */ |
8d08fdba | 8158 | |
e08a8f45 | 8159 | void |
acd8e2d0 | 8160 | expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn) |
e08a8f45 MM |
8161 | { |
8162 | tree type = TREE_TYPE (cst); | |
8163 | tree fn = PTRMEM_CST_MEMBER (cst); | |
3927874d | 8164 | tree ptr_class, fn_class; |
8d08fdba | 8165 | |
50bc768d | 8166 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); |
3927874d JM |
8167 | |
8168 | /* The class that the function belongs to. */ | |
4f1c5b7d | 8169 | fn_class = DECL_CONTEXT (fn); |
3927874d JM |
8170 | |
8171 | /* The class that we're creating a pointer to member of. */ | |
8172 | ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type); | |
8173 | ||
8174 | /* First, calculate the adjustment to the function's class. */ | |
08e17d9d | 8175 | *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0, |
135d47df | 8176 | /*c_cast_p=*/0, tf_warning_or_error); |
3927874d | 8177 | |
e08a8f45 | 8178 | if (!DECL_VIRTUAL_P (fn)) |
89fcabaf PC |
8179 | *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), |
8180 | build_addr_func (fn, tf_warning_or_error)); | |
8d08fdba | 8181 | else |
51c184be | 8182 | { |
3927874d | 8183 | /* If we're dealing with a virtual function, we have to adjust 'this' |
0cbd7506 MS |
8184 | again, to point to the base which provides the vtable entry for |
8185 | fn; the call will do the opposite adjustment. */ | |
e93ee644 | 8186 | tree orig_class = DECL_CONTEXT (fn); |
3927874d | 8187 | tree binfo = binfo_or_else (orig_class, fn_class); |
cda0a029 JM |
8188 | *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta), |
8189 | *delta, BINFO_OFFSET (binfo)); | |
3927874d | 8190 | |
3461fba7 NS |
8191 | /* We set PFN to the vtable offset at which the function can be |
8192 | found, plus one (unless ptrmemfunc_vbit_in_delta, in which | |
8193 | case delta is shifted left, and then incremented). */ | |
cb7fdde2 | 8194 | *pfn = DECL_VINDEX (fn); |
cda0a029 JM |
8195 | *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn, |
8196 | TYPE_SIZE_UNIT (vtable_entry_type)); | |
cb7fdde2 AO |
8197 | |
8198 | switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) | |
8199 | { | |
8200 | case ptrmemfunc_vbit_in_pfn: | |
cda0a029 JM |
8201 | *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn, |
8202 | integer_one_node); | |
cb7fdde2 AO |
8203 | break; |
8204 | ||
8205 | case ptrmemfunc_vbit_in_delta: | |
cda0a029 JM |
8206 | *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta), |
8207 | *delta, integer_one_node); | |
8208 | *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta), | |
8209 | *delta, integer_one_node); | |
cb7fdde2 AO |
8210 | break; |
8211 | ||
8212 | default: | |
315fb5db | 8213 | gcc_unreachable (); |
cb7fdde2 AO |
8214 | } |
8215 | ||
cda0a029 | 8216 | *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn); |
e08a8f45 MM |
8217 | } |
8218 | } | |
8d08fdba | 8219 | |
e08a8f45 MM |
8220 | /* Return an expression for PFN from the pointer-to-member function |
8221 | given by T. */ | |
8222 | ||
b5791fdc | 8223 | static tree |
acd8e2d0 | 8224 | pfn_from_ptrmemfunc (tree t) |
e08a8f45 MM |
8225 | { |
8226 | if (TREE_CODE (t) == PTRMEM_CST) | |
8227 | { | |
8228 | tree delta; | |
e08a8f45 | 8229 | tree pfn; |
c8094d83 | 8230 | |
18ae7f63 | 8231 | expand_ptrmemfunc_cst (t, &delta, &pfn); |
e08a8f45 MM |
8232 | if (pfn) |
8233 | return pfn; | |
51c184be | 8234 | } |
8d08fdba | 8235 | |
50ad9642 | 8236 | return build_ptrmemfunc_access_expr (t, pfn_identifier); |
8d08fdba MS |
8237 | } |
8238 | ||
a298680c OW |
8239 | /* Return an expression for DELTA from the pointer-to-member function |
8240 | given by T. */ | |
8241 | ||
8242 | static tree | |
8243 | delta_from_ptrmemfunc (tree t) | |
8244 | { | |
8245 | if (TREE_CODE (t) == PTRMEM_CST) | |
8246 | { | |
8247 | tree delta; | |
8248 | tree pfn; | |
8249 | ||
8250 | expand_ptrmemfunc_cst (t, &delta, &pfn); | |
8251 | if (delta) | |
8252 | return delta; | |
8253 | } | |
8254 | ||
8255 | return build_ptrmemfunc_access_expr (t, delta_identifier); | |
8256 | } | |
8257 | ||
a7a64a77 | 8258 | /* Convert value RHS to type TYPE as preparation for an assignment to |
9d9f52d3 SZ |
8259 | an lvalue of type TYPE. ERRTYPE indicates what kind of error the |
8260 | implicit conversion is. If FNDECL is non-NULL, we are doing the | |
8261 | conversion in order to pass the PARMNUMth argument of FNDECL. | |
8262 | If FNDECL is NULL, we are doing the conversion in function pointer | |
8263 | argument passing, conversion in initialization, etc. */ | |
8d08fdba MS |
8264 | |
8265 | static tree | |
acd8e2d0 | 8266 | convert_for_assignment (tree type, tree rhs, |
2f5b91f5 | 8267 | impl_conv_rhs errtype, tree fndecl, int parmnum, |
e57d93c6 | 8268 | tsubst_flags_t complain, int flags) |
8d08fdba | 8269 | { |
926ce8bd KH |
8270 | tree rhstype; |
8271 | enum tree_code coder; | |
8d08fdba | 8272 | |
8d08fdba MS |
8273 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ |
8274 | if (TREE_CODE (rhs) == NON_LVALUE_EXPR) | |
8275 | rhs = TREE_OPERAND (rhs, 0); | |
8276 | ||
d664d76d JJ |
8277 | /* Handle [dcl.init.list] direct-list-initialization from |
8278 | single element of enumeration with a fixed underlying type. */ | |
8279 | if (is_direct_enum_init (type, rhs)) | |
8280 | { | |
8281 | tree elt = CONSTRUCTOR_ELT (rhs, 0)->value; | |
8282 | if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain)) | |
8283 | rhs = cp_build_c_cast (type, elt, complain); | |
8284 | else | |
8285 | rhs = error_mark_node; | |
8286 | } | |
8287 | ||
a7a64a77 MM |
8288 | rhstype = TREE_TYPE (rhs); |
8289 | coder = TREE_CODE (rhstype); | |
8290 | ||
b55b02ea | 8291 | if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE |
00c8e9f6 | 8292 | && vector_types_convertible_p (type, rhstype, true)) |
416f380b JJ |
8293 | { |
8294 | rhs = mark_rvalue_use (rhs); | |
8295 | return convert (type, rhs); | |
8296 | } | |
7d149679 | 8297 | |
a7a64a77 | 8298 | if (rhs == error_mark_node || rhstype == error_mark_node) |
8d08fdba | 8299 | return error_mark_node; |
2c73f9f5 | 8300 | if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node) |
8d08fdba MS |
8301 | return error_mark_node; |
8302 | ||
a7a64a77 | 8303 | /* The RHS of an assignment cannot have void type. */ |
8d08fdba MS |
8304 | if (coder == VOID_TYPE) |
8305 | { | |
5ade1ed2 DG |
8306 | if (complain & tf_error) |
8307 | error ("void value not ignored as it ought to be"); | |
8d08fdba MS |
8308 | return error_mark_node; |
8309 | } | |
8d08fdba | 8310 | |
660845bf ZL |
8311 | if (c_dialect_objc ()) |
8312 | { | |
8313 | int parmno; | |
2f5b91f5 | 8314 | tree selector; |
660845bf ZL |
8315 | tree rname = fndecl; |
8316 | ||
2f5b91f5 SZ |
8317 | switch (errtype) |
8318 | { | |
8319 | case ICR_ASSIGN: | |
8320 | parmno = -1; | |
8321 | break; | |
8322 | case ICR_INIT: | |
8323 | parmno = -2; | |
8324 | break; | |
8325 | default: | |
8326 | selector = objc_message_selector (); | |
8327 | parmno = parmnum; | |
8328 | if (selector && parmno > 1) | |
8329 | { | |
8330 | rname = selector; | |
8331 | parmno -= 1; | |
8332 | } | |
660845bf ZL |
8333 | } |
8334 | ||
8335 | if (objc_compare_types (type, rhstype, parmno, rname)) | |
416f380b JJ |
8336 | { |
8337 | rhs = mark_rvalue_use (rhs); | |
8338 | return convert (type, rhs); | |
8339 | } | |
660845bf ZL |
8340 | } |
8341 | ||
a7a64a77 MM |
8342 | /* [expr.ass] |
8343 | ||
8344 | The expression is implicitly converted (clause _conv_) to the | |
72a08131 JM |
8345 | cv-unqualified type of the left operand. |
8346 | ||
8347 | We allow bad conversions here because by the time we get to this point | |
8348 | we are committed to doing the conversion. If we end up doing a bad | |
8349 | conversion, convert_like will complain. */ | |
b40e334f | 8350 | if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain)) |
a7a64a77 | 8351 | { |
32facac8 | 8352 | /* When -Wno-pmf-conversions is use, we just silently allow |
a7a64a77 MM |
8353 | conversions from pointers-to-members to plain pointers. If |
8354 | the conversion doesn't work, cp_convert will complain. */ | |
c8094d83 MS |
8355 | if (!warn_pmf2ptr |
8356 | && TYPE_PTR_P (type) | |
a7a64a77 | 8357 | && TYPE_PTRMEMFUNC_P (rhstype)) |
4b978f96 | 8358 | rhs = cp_convert (strip_top_quals (type), rhs, complain); |
72a08131 | 8359 | else |
32facac8 | 8360 | { |
5ade1ed2 DG |
8361 | if (complain & tf_error) |
8362 | { | |
8363 | /* If the right-hand side has unknown type, then it is an | |
8364 | overloaded function. Call instantiate_type to get error | |
8365 | messages. */ | |
8366 | if (rhstype == unknown_type_node) | |
8367 | instantiate_type (type, rhs, tf_warning_or_error); | |
8368 | else if (fndecl) | |
8369 | error ("cannot convert %qT to %qT for argument %qP to %qD", | |
8370 | rhstype, type, parmnum, fndecl); | |
8371 | else | |
2f5b91f5 SZ |
8372 | switch (errtype) |
8373 | { | |
8374 | case ICR_DEFAULT_ARGUMENT: | |
8375 | error ("cannot convert %qT to %qT in default argument", | |
8376 | rhstype, type); | |
8377 | break; | |
8378 | case ICR_ARGPASS: | |
8379 | error ("cannot convert %qT to %qT in argument passing", | |
8380 | rhstype, type); | |
8381 | break; | |
8382 | case ICR_CONVERTING: | |
8383 | error ("cannot convert %qT to %qT", | |
8384 | rhstype, type); | |
8385 | break; | |
8386 | case ICR_INIT: | |
8387 | error ("cannot convert %qT to %qT in initialization", | |
8388 | rhstype, type); | |
8389 | break; | |
8390 | case ICR_RETURN: | |
8391 | error ("cannot convert %qT to %qT in return", | |
8392 | rhstype, type); | |
8393 | break; | |
8394 | case ICR_ASSIGN: | |
8395 | error ("cannot convert %qT to %qT in assignment", | |
8396 | rhstype, type); | |
8397 | break; | |
8398 | default: | |
8399 | gcc_unreachable(); | |
8400 | } | |
7e422c4f PC |
8401 | if (TYPE_PTR_P (rhstype) |
8402 | && TYPE_PTR_P (type) | |
8403 | && CLASS_TYPE_P (TREE_TYPE (rhstype)) | |
8404 | && CLASS_TYPE_P (TREE_TYPE (type)) | |
8405 | && !COMPLETE_TYPE_P (TREE_TYPE (rhstype))) | |
8406 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL | |
8407 | (TREE_TYPE (rhstype))), | |
8408 | "class type %qT is incomplete", TREE_TYPE (rhstype)); | |
5ade1ed2 | 8409 | } |
32facac8 ML |
8410 | return error_mark_node; |
8411 | } | |
51c184be | 8412 | } |
90137d8f | 8413 | if (warn_suggest_attribute_format) |
104f8784 KG |
8414 | { |
8415 | const enum tree_code codel = TREE_CODE (type); | |
8416 | if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
8417 | && coder == codel | |
5ade1ed2 DG |
8418 | && check_missing_format_attribute (type, rhstype) |
8419 | && (complain & tf_warning)) | |
2f5b91f5 SZ |
8420 | switch (errtype) |
8421 | { | |
8422 | case ICR_ARGPASS: | |
8423 | case ICR_DEFAULT_ARGUMENT: | |
8424 | if (fndecl) | |
90137d8f | 8425 | warning (OPT_Wsuggest_attribute_format, |
9d9f52d3 | 8426 | "parameter %qP of %qD might be a candidate " |
2f5b91f5 SZ |
8427 | "for a format attribute", parmnum, fndecl); |
8428 | else | |
90137d8f | 8429 | warning (OPT_Wsuggest_attribute_format, |
2f5b91f5 SZ |
8430 | "parameter might be a candidate " |
8431 | "for a format attribute"); | |
8432 | break; | |
8433 | case ICR_CONVERTING: | |
90137d8f | 8434 | warning (OPT_Wsuggest_attribute_format, |
59a67d73 | 8435 | "target of conversion might be a candidate " |
2f5b91f5 SZ |
8436 | "for a format attribute"); |
8437 | break; | |
8438 | case ICR_INIT: | |
90137d8f | 8439 | warning (OPT_Wsuggest_attribute_format, |
2f5b91f5 SZ |
8440 | "target of initialization might be a candidate " |
8441 | "for a format attribute"); | |
8442 | break; | |
8443 | case ICR_RETURN: | |
90137d8f | 8444 | warning (OPT_Wsuggest_attribute_format, |
2f5b91f5 SZ |
8445 | "return type might be a candidate " |
8446 | "for a format attribute"); | |
8447 | break; | |
8448 | case ICR_ASSIGN: | |
90137d8f | 8449 | warning (OPT_Wsuggest_attribute_format, |
2f5b91f5 SZ |
8450 | "left-hand side of assignment might be a candidate " |
8451 | "for a format attribute"); | |
8452 | break; | |
8453 | default: | |
8454 | gcc_unreachable(); | |
8455 | } | |
104f8784 | 8456 | } |
3db45ab5 | 8457 | |
a99e5cb4 ILT |
8458 | /* If -Wparentheses, warn about a = b = c when a has type bool and b |
8459 | does not. */ | |
fbc8d2d3 | 8460 | if (warn_parentheses |
66be89f0 | 8461 | && TREE_CODE (type) == BOOLEAN_TYPE |
fbc8d2d3 | 8462 | && TREE_CODE (rhs) == MODIFY_EXPR |
a99e5cb4 | 8463 | && !TREE_NO_WARNING (rhs) |
69fb9832 | 8464 | && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE |
5ade1ed2 | 8465 | && (complain & tf_warning)) |
fbc8d2d3 | 8466 | { |
8400e75e | 8467 | location_t loc = EXPR_LOC_OR_LOC (rhs, input_location); |
69fb9832 MLI |
8468 | |
8469 | warning_at (loc, OPT_Wparentheses, | |
8470 | "suggest parentheses around assignment used as truth value"); | |
fbc8d2d3 ILT |
8471 | TREE_NO_WARNING (rhs) = 1; |
8472 | } | |
8473 | ||
e57d93c6 JM |
8474 | return perform_implicit_conversion_flags (strip_top_quals (type), rhs, |
8475 | complain, flags); | |
8d08fdba MS |
8476 | } |
8477 | ||
56ae6d77 | 8478 | /* Convert RHS to be of type TYPE. |
838dfd8a | 8479 | If EXP is nonzero, it is the target of the initialization. |
9d9f52d3 | 8480 | ERRTYPE indicates what kind of error the implicit conversion is. |
8d08fdba MS |
8481 | |
8482 | Two major differences between the behavior of | |
8483 | `convert_for_assignment' and `convert_for_initialization' | |
8484 | are that references are bashed in the former, while | |
8485 | copied in the latter, and aggregates are assigned in | |
8486 | the former (operator=) while initialized in the | |
8487 | latter (X(X&)). | |
8488 | ||
8489 | If using constructor make sure no conversion operator exists, if one does | |
4b978f96 | 8490 | exist, an ambiguity exists. */ |
e92cc029 | 8491 | |
8d08fdba | 8492 | tree |
acd8e2d0 | 8493 | convert_for_initialization (tree exp, tree type, tree rhs, int flags, |
2f5b91f5 | 8494 | impl_conv_rhs errtype, tree fndecl, int parmnum, |
5ade1ed2 | 8495 | tsubst_flags_t complain) |
8d08fdba | 8496 | { |
926ce8bd KH |
8497 | enum tree_code codel = TREE_CODE (type); |
8498 | tree rhstype; | |
8499 | enum tree_code coder; | |
8d08fdba MS |
8500 | |
8501 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. | |
8502 | Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */ | |
8503 | if (TREE_CODE (rhs) == NOP_EXPR | |
a0a33927 MS |
8504 | && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0)) |
8505 | && codel != REFERENCE_TYPE) | |
8d08fdba MS |
8506 | rhs = TREE_OPERAND (rhs, 0); |
8507 | ||
57b52959 VR |
8508 | if (type == error_mark_node |
8509 | || rhs == error_mark_node | |
8d08fdba MS |
8510 | || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)) |
8511 | return error_mark_node; | |
8512 | ||
f85e1317 PP |
8513 | if (MAYBE_CLASS_TYPE_P (non_reference (type))) |
8514 | ; | |
8515 | else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE | |
8516 | && TREE_CODE (type) != ARRAY_TYPE | |
8517 | && (TREE_CODE (type) != REFERENCE_TYPE | |
8518 | || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)) | |
8519 | || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE | |
8520 | && !TYPE_REFFN_P (type)) | |
8521 | || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE) | |
89fcabaf | 8522 | rhs = decay_conversion (rhs, complain); |
8d08fdba MS |
8523 | |
8524 | rhstype = TREE_TYPE (rhs); | |
8525 | coder = TREE_CODE (rhstype); | |
8526 | ||
8d08fdba MS |
8527 | if (coder == ERROR_MARK) |
8528 | return error_mark_node; | |
8529 | ||
8d08fdba MS |
8530 | /* We accept references to incomplete types, so we can |
8531 | return here before checking if RHS is of complete type. */ | |
c8094d83 | 8532 | |
8d08fdba | 8533 | if (codel == REFERENCE_TYPE) |
2986ae00 MS |
8534 | { |
8535 | /* This should eventually happen in convert_arguments. */ | |
a703fb38 | 8536 | int savew = 0, savee = 0; |
2986ae00 MS |
8537 | |
8538 | if (fndecl) | |
37e99116 | 8539 | savew = warningcount + werrorcount, savee = errorcount; |
b25dd954 | 8540 | rhs = initialize_reference (type, rhs, flags, complain); |
7abebba7 PC |
8541 | |
8542 | if (fndecl | |
8543 | && (warningcount + werrorcount > savew || errorcount > savee)) | |
af718670 PC |
8544 | inform (DECL_SOURCE_LOCATION (fndecl), |
8545 | "in passing argument %P of %qD", parmnum, fndecl); | |
7abebba7 | 8546 | |
2986ae00 | 8547 | return rhs; |
c8094d83 | 8548 | } |
8d08fdba | 8549 | |
66543169 | 8550 | if (exp != 0) |
79fe346e | 8551 | exp = require_complete_type_sfinae (exp, complain); |
8d08fdba MS |
8552 | if (exp == error_mark_node) |
8553 | return error_mark_node; | |
8554 | ||
ee76b931 | 8555 | rhstype = non_reference (rhstype); |
8d08fdba | 8556 | |
6467930b MS |
8557 | type = complete_type (type); |
8558 | ||
37a7519a JM |
8559 | if (DIRECT_INIT_EXPR_P (type, rhs)) |
8560 | /* Don't try to do copy-initialization if we already have | |
8561 | direct-initialization. */ | |
8562 | return rhs; | |
8563 | ||
9e1e64ec | 8564 | if (MAYBE_CLASS_TYPE_P (type)) |
f8f12278 | 8565 | return perform_implicit_conversion_flags (type, rhs, complain, flags); |
8d08fdba | 8566 | |
5ade1ed2 | 8567 | return convert_for_assignment (type, rhs, errtype, fndecl, parmnum, |
e57d93c6 | 8568 | complain, flags); |
8d08fdba MS |
8569 | } |
8570 | \f | |
efee38a9 | 8571 | /* If RETVAL is the address of, or a reference to, a local variable or |
b4dfdc11 | 8572 | temporary give an appropriate warning and return true. */ |
8d08fdba | 8573 | |
b4dfdc11 | 8574 | static bool |
acd8e2d0 | 8575 | maybe_warn_about_returning_address_of_local (tree retval) |
efee38a9 MM |
8576 | { |
8577 | tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl)); | |
2bfa73e4 | 8578 | tree whats_returned = retval; |
8d08fdba | 8579 | |
2bfa73e4 | 8580 | for (;;) |
efee38a9 | 8581 | { |
efee38a9 | 8582 | if (TREE_CODE (whats_returned) == COMPOUND_EXPR) |
2bfa73e4 | 8583 | whats_returned = TREE_OPERAND (whats_returned, 1); |
5f9e56b3 TB |
8584 | else if (CONVERT_EXPR_P (whats_returned) |
8585 | || TREE_CODE (whats_returned) == NON_LVALUE_EXPR) | |
efee38a9 | 8586 | whats_returned = TREE_OPERAND (whats_returned, 0); |
2bfa73e4 BS |
8587 | else |
8588 | break; | |
8589 | } | |
8590 | ||
8591 | if (TREE_CODE (whats_returned) != ADDR_EXPR) | |
b4dfdc11 | 8592 | return false; |
c8094d83 | 8593 | whats_returned = TREE_OPERAND (whats_returned, 0); |
2bfa73e4 | 8594 | |
75d850f3 PP |
8595 | while (TREE_CODE (whats_returned) == COMPONENT_REF |
8596 | || TREE_CODE (whats_returned) == ARRAY_REF) | |
8597 | whats_returned = TREE_OPERAND (whats_returned, 0); | |
8598 | ||
2bfa73e4 BS |
8599 | if (TREE_CODE (valtype) == REFERENCE_TYPE) |
8600 | { | |
8601 | if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR | |
8602 | || TREE_CODE (whats_returned) == TARGET_EXPR) | |
efee38a9 | 8603 | { |
880661a4 | 8604 | warning (OPT_Wreturn_local_addr, "returning reference to temporary"); |
b4dfdc11 | 8605 | return true; |
efee38a9 | 8606 | } |
5a6ccc94 | 8607 | if (VAR_P (whats_returned) |
2bfa73e4 BS |
8608 | && DECL_NAME (whats_returned) |
8609 | && TEMP_NAME_P (DECL_NAME (whats_returned))) | |
efee38a9 | 8610 | { |
880661a4 | 8611 | warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned"); |
b4dfdc11 | 8612 | return true; |
efee38a9 MM |
8613 | } |
8614 | } | |
efee38a9 | 8615 | |
a2f1f4c3 | 8616 | if (DECL_P (whats_returned) |
2bfa73e4 BS |
8617 | && DECL_NAME (whats_returned) |
8618 | && DECL_FUNCTION_SCOPE_P (whats_returned) | |
5ced939e | 8619 | && !is_capture_proxy (whats_returned) |
2bfa73e4 BS |
8620 | && !(TREE_STATIC (whats_returned) |
8621 | || TREE_PUBLIC (whats_returned))) | |
8622 | { | |
8623 | if (TREE_CODE (valtype) == REFERENCE_TYPE) | |
af718670 PC |
8624 | warning_at (DECL_SOURCE_LOCATION (whats_returned), |
8625 | OPT_Wreturn_local_addr, | |
8626 | "reference to local variable %qD returned", | |
8627 | whats_returned); | |
19fc9faa | 8628 | else if (TREE_CODE (whats_returned) == LABEL_DECL) |
af718670 PC |
8629 | warning_at (DECL_SOURCE_LOCATION (whats_returned), |
8630 | OPT_Wreturn_local_addr, "address of label %qD returned", | |
8631 | whats_returned); | |
2bfa73e4 | 8632 | else |
af718670 PC |
8633 | warning_at (DECL_SOURCE_LOCATION (whats_returned), |
8634 | OPT_Wreturn_local_addr, "address of local variable %qD " | |
8635 | "returned", whats_returned); | |
b4dfdc11 | 8636 | return true; |
efee38a9 | 8637 | } |
b4dfdc11 MG |
8638 | |
8639 | return false; | |
efee38a9 MM |
8640 | } |
8641 | ||
0e339752 | 8642 | /* Check that returning RETVAL from the current function is valid. |
efee38a9 MM |
8643 | Return an expression explicitly showing all conversions required to |
8644 | change RETVAL into the function return type, and to assign it to | |
0c9b182b JJ |
8645 | the DECL_RESULT for the function. Set *NO_WARNING to true if |
8646 | code reaches end of non-void function warning shouldn't be issued | |
8647 | on this RETURN_EXPR. */ | |
efee38a9 MM |
8648 | |
8649 | tree | |
0c9b182b | 8650 | check_return_expr (tree retval, bool *no_warning) |
8d08fdba | 8651 | { |
efee38a9 | 8652 | tree result; |
852497a3 | 8653 | /* The type actually returned by the function. */ |
efee38a9 | 8654 | tree valtype; |
852497a3 JM |
8655 | /* The type the function is declared to return, or void if |
8656 | the declared type is incomplete. */ | |
8657 | tree functype; | |
efee38a9 | 8658 | int fn_returns_value_p; |
8af2fec4 | 8659 | bool named_return_value_okay_p; |
efee38a9 | 8660 | |
0c9b182b JJ |
8661 | *no_warning = false; |
8662 | ||
3af9c5e9 | 8663 | if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval)) |
12893402 BI |
8664 | { |
8665 | error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return " | |
8666 | "statement is not allowed"); | |
8667 | return NULL_TREE; | |
8668 | } | |
8669 | ||
efee38a9 MM |
8670 | /* A `volatile' function is one that isn't supposed to return, ever. |
8671 | (This is a G++ extension, used to get better code for functions | |
8672 | that call the `volatile' function.) */ | |
8d08fdba | 8673 | if (TREE_THIS_VOLATILE (current_function_decl)) |
d4ee4d25 | 8674 | warning (0, "function declared %<noreturn%> has a %<return%> statement"); |
8d08fdba | 8675 | |
efee38a9 | 8676 | /* Check for various simple errors. */ |
a0de9d20 | 8677 | if (DECL_DESTRUCTOR_P (current_function_decl)) |
8d08fdba | 8678 | { |
1c2c08a5 | 8679 | if (retval) |
8251199e | 8680 | error ("returning a value from a destructor"); |
efee38a9 | 8681 | return NULL_TREE; |
1c2c08a5 | 8682 | } |
90418208 | 8683 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) |
0dde4175 | 8684 | { |
90418208 JM |
8685 | if (in_function_try_handler) |
8686 | /* If a return statement appears in a handler of the | |
c6002625 | 8687 | function-try-block of a constructor, the program is ill-formed. */ |
90418208 JM |
8688 | error ("cannot return from a handler of a function-try-block of a constructor"); |
8689 | else if (retval) | |
8690 | /* You can't return a value from a constructor. */ | |
8691 | error ("returning a value from a constructor"); | |
8692 | return NULL_TREE; | |
efee38a9 | 8693 | } |
efee38a9 | 8694 | |
11f2c78a PP |
8695 | const tree saved_retval = retval; |
8696 | ||
efc7052d JM |
8697 | if (processing_template_decl) |
8698 | { | |
8699 | current_function_returns_value = 1; | |
11f2c78a | 8700 | |
7b3e2d46 | 8701 | if (check_for_bare_parameter_packs (retval)) |
11f2c78a PP |
8702 | return error_mark_node; |
8703 | ||
8704 | if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))) | |
8705 | || (retval != NULL_TREE | |
8706 | && type_dependent_expression_p (retval))) | |
8707 | return retval; | |
efc7052d | 8708 | } |
c8094d83 | 8709 | |
852497a3 JM |
8710 | functype = TREE_TYPE (TREE_TYPE (current_function_decl)); |
8711 | ||
8712 | /* Deduce auto return type from a return statement. */ | |
8713 | if (current_function_auto_return_pattern) | |
8714 | { | |
8715 | tree auto_node; | |
8716 | tree type; | |
8717 | ||
8718 | if (!retval && !is_auto (current_function_auto_return_pattern)) | |
8719 | { | |
8720 | /* Give a helpful error message. */ | |
8721 | error ("return-statement with no value, in function returning %qT", | |
8722 | current_function_auto_return_pattern); | |
8723 | inform (input_location, "only plain %<auto%> return type can be " | |
8724 | "deduced to %<void%>"); | |
8725 | type = error_mark_node; | |
8726 | } | |
2aa953d0 JM |
8727 | else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval)) |
8728 | { | |
8729 | error ("returning initializer list"); | |
8730 | type = error_mark_node; | |
8731 | } | |
852497a3 JM |
8732 | else |
8733 | { | |
8734 | if (!retval) | |
632f2871 | 8735 | retval = void_node; |
852497a3 JM |
8736 | auto_node = type_uses_auto (current_function_auto_return_pattern); |
8737 | type = do_auto_deduction (current_function_auto_return_pattern, | |
8738 | retval, auto_node); | |
8739 | } | |
8740 | ||
8741 | if (type == error_mark_node) | |
8742 | /* Leave it. */; | |
8743 | else if (functype == current_function_auto_return_pattern) | |
8744 | apply_deduced_return_type (current_function_decl, type); | |
5dc58049 JM |
8745 | else if (!same_type_p (type, functype)) |
8746 | { | |
8747 | if (LAMBDA_FUNCTION_P (current_function_decl)) | |
8748 | error ("inconsistent types %qT and %qT deduced for " | |
8749 | "lambda return type", functype, type); | |
8750 | else | |
8751 | error ("inconsistent deduction for auto return type: " | |
8752 | "%qT and then %qT", functype, type); | |
8753 | } | |
852497a3 JM |
8754 | functype = type; |
8755 | } | |
8756 | ||
efee38a9 MM |
8757 | result = DECL_RESULT (current_function_decl); |
8758 | valtype = TREE_TYPE (result); | |
50bc768d | 8759 | gcc_assert (valtype != NULL_TREE); |
b72801e2 | 8760 | fn_returns_value_p = !VOID_TYPE_P (valtype); |
efee38a9 MM |
8761 | |
8762 | /* Check for a return statement with no return value in a function | |
8763 | that's supposed to return a value. */ | |
8764 | if (!retval && fn_returns_value_p) | |
8765 | { | |
852497a3 JM |
8766 | if (functype != error_mark_node) |
8767 | permerror (input_location, "return-statement with no value, in " | |
8768 | "function returning %qT", valtype); | |
8769 | /* Remember that this function did return. */ | |
8770 | current_function_returns_value = 1; | |
0c9b182b | 8771 | /* And signal caller that TREE_NO_WARNING should be set on the |
3db45ab5 MS |
8772 | RETURN_EXPR to avoid control reaches end of non-void function |
8773 | warnings in tree-cfg.c. */ | |
0c9b182b | 8774 | *no_warning = true; |
efee38a9 MM |
8775 | } |
8776 | /* Check for a return statement with a value in a function that | |
8777 | isn't supposed to return a value. */ | |
8778 | else if (retval && !fn_returns_value_p) | |
c8094d83 | 8779 | { |
b72801e2 | 8780 | if (VOID_TYPE_P (TREE_TYPE (retval))) |
efee38a9 MM |
8781 | /* You can return a `void' value from a function of `void' |
8782 | type. In that case, we have to evaluate the expression for | |
8783 | its side-effects. */ | |
8784 | finish_expr_stmt (retval); | |
8785 | else | |
cbe5f3b3 | 8786 | permerror (input_location, "return-statement with a value, in function " |
37ec60ed | 8787 | "returning 'void'"); |
efee38a9 MM |
8788 | current_function_returns_null = 1; |
8789 | ||
8790 | /* There's really no value to return, after all. */ | |
8791 | return NULL_TREE; | |
0dde4175 | 8792 | } |
efee38a9 MM |
8793 | else if (!retval) |
8794 | /* Remember that this function can sometimes return without a | |
8795 | value. */ | |
8796 | current_function_returns_null = 1; | |
90418208 JM |
8797 | else |
8798 | /* Remember that this function did return a value. */ | |
8799 | current_function_returns_value = 1; | |
1c2c08a5 | 8800 | |
276318a5 | 8801 | /* Check for erroneous operands -- but after giving ourselves a |
5ae9ba3e MM |
8802 | chance to provide an error about returning a value from a void |
8803 | function. */ | |
8804 | if (error_operand_p (retval)) | |
8805 | { | |
8806 | current_function_return_value = error_mark_node; | |
8807 | return error_mark_node; | |
8808 | } | |
8809 | ||
7f477e81 | 8810 | /* Only operator new(...) throw(), can return NULL [expr.new/13]. */ |
596ea4e5 AS |
8811 | if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR |
8812 | || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR) | |
7f477e81 | 8813 | && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl)) |
708cae97 | 8814 | && ! flag_check_new |
aedfac0e | 8815 | && retval && null_ptr_cst_p (retval)) |
d4ee4d25 | 8816 | warning (0, "%<operator new%> must not return NULL unless it is " |
0cbd7506 | 8817 | "declared %<throw()%> (or -fcheck-new is in effect)"); |
8d08fdba | 8818 | |
824b9a4c | 8819 | /* Effective C++ rule 15. See also start_function. */ |
eb448459 | 8820 | if (warn_ecpp |
47293da3 GB |
8821 | && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)) |
8822 | { | |
8823 | bool warn = true; | |
8824 | ||
8825 | /* The function return type must be a reference to the current | |
8826 | class. */ | |
8827 | if (TREE_CODE (valtype) == REFERENCE_TYPE | |
8828 | && same_type_ignoring_top_level_qualifiers_p | |
8829 | (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))) | |
8830 | { | |
8831 | /* Returning '*this' is obviously OK. */ | |
8832 | if (retval == current_class_ref) | |
8833 | warn = false; | |
8834 | /* If we are calling a function whose return type is the same of | |
8835 | the current class reference, it is ok. */ | |
591cb3cf | 8836 | else if (INDIRECT_REF_P (retval) |
47293da3 | 8837 | && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR) |
616adc47 | 8838 | warn = false; |
47293da3 GB |
8839 | } |
8840 | ||
8841 | if (warn) | |
b323323f | 8842 | warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>"); |
47293da3 | 8843 | } |
824b9a4c | 8844 | |
11f2c78a PP |
8845 | if (processing_template_decl) |
8846 | { | |
8847 | /* We should not have changed the return value. */ | |
8848 | gcc_assert (retval == saved_retval); | |
8849 | return retval; | |
8850 | } | |
8851 | ||
07b2f2fd JM |
8852 | /* The fabled Named Return Value optimization, as per [class.copy]/15: |
8853 | ||
8854 | [...] For a function with a class return type, if the expression | |
8855 | in the return statement is the name of a local object, and the cv- | |
8856 | unqualified type of the local object is the same as the function | |
8857 | return type, an implementation is permitted to omit creating the tem- | |
8858 | porary object to hold the function return value [...] | |
8859 | ||
8860 | So, if this is a value-returning function that always returns the same | |
8861 | local variable, remember it. | |
0d97bf4c JM |
8862 | |
8863 | It might be nice to be more flexible, and choose the first suitable | |
8864 | variable even if the function sometimes returns something else, but | |
8865 | then we run the risk of clobbering the variable we chose if the other | |
8866 | returned expression uses the chosen variable somehow. And people expect | |
07b2f2fd JM |
8867 | this restriction, anyway. (jason 2000-11-19) |
8868 | ||
324f9dfb | 8869 | See finish_function and finalize_nrv for the rest of this optimization. */ |
0d97bf4c | 8870 | |
8af2fec4 RY |
8871 | named_return_value_okay_p = |
8872 | (retval != NULL_TREE | |
8873 | /* Must be a local, automatic variable. */ | |
5a6ccc94 | 8874 | && VAR_P (retval) |
8af2fec4 RY |
8875 | && DECL_CONTEXT (retval) == current_function_decl |
8876 | && ! TREE_STATIC (retval) | |
f9f91ddc JM |
8877 | /* And not a lambda or anonymous union proxy. */ |
8878 | && !DECL_HAS_VALUE_EXPR_P (retval) | |
5657d966 | 8879 | && (DECL_ALIGN (retval) <= DECL_ALIGN (result)) |
8af2fec4 RY |
8880 | /* The cv-unqualified type of the returned value must be the |
8881 | same as the cv-unqualified return type of the | |
8882 | function. */ | |
8883 | && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))), | |
852497a3 | 8884 | (TYPE_MAIN_VARIANT (functype))) |
0e95aec1 CB |
8885 | /* And the returned value must be non-volatile. */ |
8886 | && ! TYPE_VOLATILE (TREE_TYPE (retval))); | |
8af2fec4 | 8887 | |
01f9e964 | 8888 | if (fn_returns_value_p && flag_elide_constructors) |
0d97bf4c | 8889 | { |
8af2fec4 RY |
8890 | if (named_return_value_okay_p |
8891 | && (current_function_return_value == NULL_TREE | |
8892 | || current_function_return_value == retval)) | |
0d97bf4c JM |
8893 | current_function_return_value = retval; |
8894 | else | |
8895 | current_function_return_value = error_mark_node; | |
8896 | } | |
8897 | ||
efee38a9 MM |
8898 | /* We don't need to do any conversions when there's nothing being |
8899 | returned. */ | |
5ae9ba3e MM |
8900 | if (!retval) |
8901 | return NULL_TREE; | |
efee38a9 MM |
8902 | |
8903 | /* Do any required conversions. */ | |
8904 | if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl)) | |
8905 | /* No conversions are required. */ | |
8906 | ; | |
8d08fdba MS |
8907 | else |
8908 | { | |
8af2fec4 | 8909 | int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING; |
cce2be43 | 8910 | |
51b15ede NS |
8911 | /* The functype's return type will have been set to void, if it |
8912 | was an incomplete type. Just treat this as 'return;' */ | |
8913 | if (VOID_TYPE_P (functype)) | |
8914 | return error_mark_node; | |
3db45ab5 | 8915 | |
96e780c0 JM |
8916 | /* If we had an id-expression obfuscated by force_paren_expr, we need |
8917 | to undo it so we can try to treat it as an rvalue below. */ | |
1137001c | 8918 | retval = maybe_undo_parenthesized_ref (retval); |
96e780c0 | 8919 | |
fb682f94 | 8920 | /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes |
8af2fec4 | 8921 | treated as an rvalue for the purposes of overload resolution to |
532c679e JM |
8922 | favor move constructors over copy constructors. |
8923 | ||
8924 | Note that these conditions are similar to, but not as strict as, | |
8925 | the conditions for the named return value optimization. */ | |
8926 | if ((cxx_dialect != cxx98) | |
f9f91ddc | 8927 | && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval)) |
532c679e JM |
8928 | || TREE_CODE (retval) == PARM_DECL) |
8929 | && DECL_CONTEXT (retval) == current_function_decl | |
8930 | && !TREE_STATIC (retval) | |
532c679e | 8931 | /* This is only interesting for class type. */ |
852497a3 | 8932 | && CLASS_TYPE_P (functype)) |
8af2fec4 RY |
8933 | flags = flags | LOOKUP_PREFER_RVALUE; |
8934 | ||
cce2be43 JM |
8935 | /* First convert the value to the function's return type, then |
8936 | to the type of return value's location to handle the | |
0cbd7506 | 8937 | case that functype is smaller than the valtype. */ |
c1bc6829 | 8938 | retval = convert_for_initialization |
2f5b91f5 | 8939 | (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0, |
5ade1ed2 | 8940 | tf_warning_or_error); |
cce2be43 JM |
8941 | retval = convert (valtype, retval); |
8942 | ||
efee38a9 | 8943 | /* If the conversion failed, treat this just like `return;'. */ |
691c003d | 8944 | if (retval == error_mark_node) |
90418208 | 8945 | return retval; |
02531345 | 8946 | /* We can't initialize a register from a AGGR_INIT_EXPR. */ |
e3b5732b | 8947 | else if (! cfun->returns_struct |
c1bc6829 | 8948 | && TREE_CODE (retval) == TARGET_EXPR |
02531345 | 8949 | && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR) |
f293ce4b RS |
8950 | retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, |
8951 | TREE_OPERAND (retval, 0)); | |
b4dfdc11 MG |
8952 | else if (maybe_warn_about_returning_address_of_local (retval)) |
8953 | retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, | |
8954 | build_zero_cst (TREE_TYPE (retval))); | |
8d08fdba | 8955 | } |
c8094d83 | 8956 | |
efee38a9 | 8957 | /* Actually copy the value returned into the appropriate location. */ |
691c003d | 8958 | if (retval && retval != result) |
f293ce4b | 8959 | retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval); |
b88c08b6 | 8960 | |
efee38a9 MM |
8961 | return retval; |
8962 | } | |
8963 | ||
8d08fdba | 8964 | \f |
838dfd8a | 8965 | /* Returns nonzero if the pointer-type FROM can be converted to the |
ceab47eb | 8966 | pointer-type TO via a qualification conversion. If CONSTP is -1, |
838dfd8a | 8967 | then we return nonzero if the pointers are similar, and the |
ceab47eb MM |
8968 | cv-qualification signature of FROM is a proper subset of that of TO. |
8969 | ||
8970 | If CONSTP is positive, then all outer pointers have been | |
8971 | const-qualified. */ | |
e92cc029 | 8972 | |
bd6dd845 | 8973 | static int |
acd8e2d0 | 8974 | comp_ptr_ttypes_real (tree to, tree from, int constp) |
a0a33927 | 8975 | { |
a5ac359a | 8976 | bool to_more_cv_qualified = false; |
f83c7f63 | 8977 | bool is_opaque_pointer = false; |
ceab47eb | 8978 | |
a0a33927 MS |
8979 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) |
8980 | { | |
8981 | if (TREE_CODE (to) != TREE_CODE (from)) | |
8982 | return 0; | |
8983 | ||
d11ad92e | 8984 | if (TREE_CODE (from) == OFFSET_TYPE |
b7a78333 MM |
8985 | && !same_type_p (TYPE_OFFSET_BASETYPE (from), |
8986 | TYPE_OFFSET_BASETYPE (to))) | |
8987 | return 0; | |
d11ad92e | 8988 | |
f30432d7 MS |
8989 | /* Const and volatile mean something different for function types, |
8990 | so the usual checks are not appropriate. */ | |
8991 | if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE) | |
8992 | { | |
6c39e757 | 8993 | if (!at_least_as_qualified_p (to, from)) |
02020185 | 8994 | return 0; |
a0a33927 | 8995 | |
6c39e757 | 8996 | if (!at_least_as_qualified_p (from, to)) |
02020185 | 8997 | { |
ceab47eb MM |
8998 | if (constp == 0) |
8999 | return 0; | |
a5ac359a | 9000 | to_more_cv_qualified = true; |
ceab47eb MM |
9001 | } |
9002 | ||
9003 | if (constp > 0) | |
6c39e757 | 9004 | constp &= TYPE_READONLY (to); |
f30432d7 | 9005 | } |
a0a33927 | 9006 | |
b55b02ea | 9007 | if (VECTOR_TYPE_P (to)) |
f83c7f63 DJ |
9008 | is_opaque_pointer = vector_targets_convertible_p (to, from); |
9009 | ||
50e10fa8 | 9010 | if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to)) |
8de9bb0e | 9011 | return ((constp >= 0 || to_more_cv_qualified) |
f83c7f63 DJ |
9012 | && (is_opaque_pointer |
9013 | || same_type_ignoring_top_level_qualifiers_p (to, from))); | |
a0a33927 MS |
9014 | } |
9015 | } | |
9016 | ||
8de9bb0e NS |
9017 | /* When comparing, say, char ** to char const **, this function takes |
9018 | the 'char *' and 'char const *'. Do not pass non-pointer/reference | |
9019 | types to this function. */ | |
e92cc029 | 9020 | |
a0a33927 | 9021 | int |
acd8e2d0 | 9022 | comp_ptr_ttypes (tree to, tree from) |
a0a33927 MS |
9023 | { |
9024 | return comp_ptr_ttypes_real (to, from, 1); | |
9025 | } | |
d11ad92e | 9026 | |
d4684c00 JM |
9027 | /* Returns true iff FNTYPE is a non-class type that involves |
9028 | error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node | |
9029 | if a parameter type is ill-formed. */ | |
9030 | ||
9031 | bool | |
9032 | error_type_p (const_tree type) | |
9033 | { | |
9034 | tree t; | |
9035 | ||
9036 | switch (TREE_CODE (type)) | |
9037 | { | |
9038 | case ERROR_MARK: | |
9039 | return true; | |
9040 | ||
9041 | case POINTER_TYPE: | |
9042 | case REFERENCE_TYPE: | |
9043 | case OFFSET_TYPE: | |
9044 | return error_type_p (TREE_TYPE (type)); | |
9045 | ||
9046 | case FUNCTION_TYPE: | |
9047 | case METHOD_TYPE: | |
9048 | if (error_type_p (TREE_TYPE (type))) | |
9049 | return true; | |
9050 | for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t)) | |
9051 | if (error_type_p (TREE_VALUE (t))) | |
9052 | return true; | |
9053 | return false; | |
9054 | ||
9055 | case RECORD_TYPE: | |
9056 | if (TYPE_PTRMEMFUNC_P (type)) | |
9057 | return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type)); | |
9058 | return false; | |
9059 | ||
9060 | default: | |
9061 | return false; | |
9062 | } | |
9063 | } | |
9064 | ||
3f2d352c | 9065 | /* Returns true if to and from are (possibly multi-level) pointers to the same |
d11ad92e MS |
9066 | type or inheritance-related types, regardless of cv-quals. */ |
9067 | ||
3f2d352c | 9068 | bool |
58f9752a | 9069 | ptr_reasonably_similar (const_tree to, const_tree from) |
d11ad92e MS |
9070 | { |
9071 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) | |
9072 | { | |
72a08131 | 9073 | /* Any target type is similar enough to void. */ |
50e10fa8 | 9074 | if (VOID_TYPE_P (to)) |
d4684c00 | 9075 | return !error_type_p (from); |
50e10fa8 | 9076 | if (VOID_TYPE_P (from)) |
d4684c00 | 9077 | return !error_type_p (to); |
72a08131 | 9078 | |
d11ad92e | 9079 | if (TREE_CODE (to) != TREE_CODE (from)) |
3f2d352c | 9080 | return false; |
d11ad92e MS |
9081 | |
9082 | if (TREE_CODE (from) == OFFSET_TYPE | |
96d84882 PB |
9083 | && comptypes (TYPE_OFFSET_BASETYPE (to), |
9084 | TYPE_OFFSET_BASETYPE (from), | |
9085 | COMPARE_BASE | COMPARE_DERIVED)) | |
d11ad92e MS |
9086 | continue; |
9087 | ||
b55b02ea | 9088 | if (VECTOR_TYPE_P (to) |
00c8e9f6 | 9089 | && vector_types_convertible_p (to, from, false)) |
3f2d352c | 9090 | return true; |
d70b8c3a | 9091 | |
8a2b77e7 JM |
9092 | if (TREE_CODE (to) == INTEGER_TYPE |
9093 | && TYPE_PRECISION (to) == TYPE_PRECISION (from)) | |
3f2d352c | 9094 | return true; |
8a2b77e7 JM |
9095 | |
9096 | if (TREE_CODE (to) == FUNCTION_TYPE) | |
d4684c00 | 9097 | return !error_type_p (to) && !error_type_p (from); |
8a2b77e7 | 9098 | |
50e10fa8 | 9099 | if (!TYPE_PTR_P (to)) |
3f2d352c PC |
9100 | { |
9101 | /* When either type is incomplete avoid DERIVED_FROM_P, | |
9102 | which may call complete_type (c++/57942). */ | |
9103 | bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from); | |
9104 | return comptypes | |
9105 | (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), | |
9106 | b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED); | |
9107 | } | |
d11ad92e MS |
9108 | } |
9109 | } | |
c11b6f21 | 9110 | |
34b5375f MM |
9111 | /* Return true if TO and FROM (both of which are POINTER_TYPEs or |
9112 | pointer-to-member types) are the same, ignoring cv-qualification at | |
9113 | all levels. */ | |
c11b6f21 | 9114 | |
34b5375f | 9115 | bool |
acd8e2d0 | 9116 | comp_ptr_ttypes_const (tree to, tree from) |
c11b6f21 | 9117 | { |
f83c7f63 DJ |
9118 | bool is_opaque_pointer = false; |
9119 | ||
c11b6f21 MS |
9120 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) |
9121 | { | |
9122 | if (TREE_CODE (to) != TREE_CODE (from)) | |
34b5375f | 9123 | return false; |
c11b6f21 MS |
9124 | |
9125 | if (TREE_CODE (from) == OFFSET_TYPE | |
3bfdc719 MM |
9126 | && same_type_p (TYPE_OFFSET_BASETYPE (from), |
9127 | TYPE_OFFSET_BASETYPE (to))) | |
c11b6f21 MS |
9128 | continue; |
9129 | ||
b55b02ea | 9130 | if (VECTOR_TYPE_P (to)) |
f83c7f63 DJ |
9131 | is_opaque_pointer = vector_targets_convertible_p (to, from); |
9132 | ||
50e10fa8 | 9133 | if (!TYPE_PTR_P (to)) |
f83c7f63 DJ |
9134 | return (is_opaque_pointer |
9135 | || same_type_ignoring_top_level_qualifiers_p (to, from)); | |
c11b6f21 MS |
9136 | } |
9137 | } | |
9138 | ||
89d684bb BM |
9139 | /* Returns the type qualifiers for this type, including the qualifiers on the |
9140 | elements for an array type. */ | |
27778b73 MM |
9141 | |
9142 | int | |
ac7d7749 | 9143 | cp_type_quals (const_tree type) |
27778b73 | 9144 | { |
2872152c | 9145 | int quals; |
4e9b57fa | 9146 | /* This CONST_CAST is okay because strip_array_types returns its |
75547801 | 9147 | argument unmodified and we assign it to a const_tree. */ |
2872152c JM |
9148 | type = strip_array_types (CONST_CAST_TREE (type)); |
9149 | if (type == error_mark_node | |
9150 | /* Quals on a FUNCTION_TYPE are memfn quals. */ | |
9151 | || TREE_CODE (type) == FUNCTION_TYPE) | |
328de7c2 | 9152 | return TYPE_UNQUALIFIED; |
2872152c JM |
9153 | quals = TYPE_QUALS (type); |
9154 | /* METHOD and REFERENCE_TYPEs should never have quals. */ | |
9155 | gcc_assert ((TREE_CODE (type) != METHOD_TYPE | |
9156 | && TREE_CODE (type) != REFERENCE_TYPE) | |
9157 | || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) | |
9158 | == TYPE_UNQUALIFIED)); | |
9159 | return quals; | |
9160 | } | |
9161 | ||
2eed8e37 BK |
9162 | /* Returns the function-ref-qualifier for TYPE */ |
9163 | ||
9164 | cp_ref_qualifier | |
9165 | type_memfn_rqual (const_tree type) | |
9166 | { | |
9167 | gcc_assert (TREE_CODE (type) == FUNCTION_TYPE | |
9168 | || TREE_CODE (type) == METHOD_TYPE); | |
9169 | ||
9170 | if (!FUNCTION_REF_QUALIFIED (type)) | |
9171 | return REF_QUAL_NONE; | |
9172 | else if (FUNCTION_RVALUE_QUALIFIED (type)) | |
9173 | return REF_QUAL_RVALUE; | |
9174 | else | |
9175 | return REF_QUAL_LVALUE; | |
9176 | } | |
9177 | ||
2872152c JM |
9178 | /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or |
9179 | METHOD_TYPE. */ | |
9180 | ||
9181 | int | |
9182 | type_memfn_quals (const_tree type) | |
9183 | { | |
9184 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
9185 | return TYPE_QUALS (type); | |
9186 | else if (TREE_CODE (type) == METHOD_TYPE) | |
7e1352fe | 9187 | return cp_type_quals (class_of_this_parm (type)); |
2872152c JM |
9188 | else |
9189 | gcc_unreachable (); | |
9190 | } | |
9191 | ||
9192 | /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to | |
2eed8e37 | 9193 | MEMFN_QUALS and its ref-qualifier to RQUAL. */ |
2872152c JM |
9194 | |
9195 | tree | |
2eed8e37 | 9196 | apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual) |
2872152c JM |
9197 | { |
9198 | /* Could handle METHOD_TYPE here if necessary. */ | |
9199 | gcc_assert (TREE_CODE (type) == FUNCTION_TYPE); | |
2eed8e37 BK |
9200 | if (TYPE_QUALS (type) == memfn_quals |
9201 | && type_memfn_rqual (type) == rqual) | |
2872152c | 9202 | return type; |
2eed8e37 | 9203 | |
2872152c JM |
9204 | /* This should really have a different TYPE_MAIN_VARIANT, but that gets |
9205 | complex. */ | |
2eed8e37 | 9206 | tree result = build_qualified_type (type, memfn_quals); |
b07891da JM |
9207 | if (tree canon = TYPE_CANONICAL (result)) |
9208 | if (canon != result) | |
9209 | /* check_qualified_type doesn't check the ref-qualifier, so make sure | |
9210 | TYPE_CANONICAL is correct. */ | |
9211 | TYPE_CANONICAL (result) | |
9212 | = build_ref_qualified_type (canon, type_memfn_rqual (result)); | |
2eed8e37 BK |
9213 | result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type)); |
9214 | return build_ref_qualified_type (result, rqual); | |
91063b51 | 9215 | } |
a7a7710d | 9216 | |
36c37128 JM |
9217 | /* Returns nonzero if TYPE is const or volatile. */ |
9218 | ||
9219 | bool | |
9220 | cv_qualified_p (const_tree type) | |
9221 | { | |
9222 | int quals = cp_type_quals (type); | |
9223 | return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0; | |
9224 | } | |
9225 | ||
f4f206f4 | 9226 | /* Returns nonzero if the TYPE contains a mutable member. */ |
a7a7710d | 9227 | |
acd8e2d0 | 9228 | bool |
58f9752a | 9229 | cp_has_mutable_p (const_tree type) |
a7a7710d | 9230 | { |
4e9b57fa | 9231 | /* This CONST_CAST is okay because strip_array_types returns its |
75547801 | 9232 | argument unmodified and we assign it to a const_tree. */ |
b1d5455a | 9233 | type = strip_array_types (CONST_CAST_TREE(type)); |
a7a7710d NS |
9234 | |
9235 | return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type); | |
9236 | } | |
af7b9902 | 9237 | |
15896502 MM |
9238 | /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the |
9239 | TYPE_QUALS. For a VAR_DECL, this may be an optimistic | |
9240 | approximation. In particular, consider: | |
9241 | ||
9242 | int f(); | |
9243 | struct S { int i; }; | |
9244 | const S s = { f(); } | |
9245 | ||
9246 | Here, we will make "s" as TREE_READONLY (because it is declared | |
9247 | "const") -- only to reverse ourselves upon seeing that the | |
9248 | initializer is non-constant. */ | |
9249 | ||
9804209d DG |
9250 | void |
9251 | cp_apply_type_quals_to_decl (int type_quals, tree decl) | |
9252 | { | |
9253 | tree type = TREE_TYPE (decl); | |
9254 | ||
9255 | if (type == error_mark_node) | |
9256 | return; | |
9257 | ||
0d9c0892 JM |
9258 | if (TREE_CODE (decl) == TYPE_DECL) |
9259 | return; | |
9260 | ||
93e1ddcf JM |
9261 | gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE |
9262 | && type_quals != TYPE_UNQUALIFIED)); | |
9804209d | 9263 | |
67935995 | 9264 | /* Avoid setting TREE_READONLY incorrectly. */ |
329af3c7 JM |
9265 | /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr |
9266 | constructor can produce constant init, so rely on cp_finish_decl to | |
9267 | clear TREE_READONLY if the variable has non-constant init. */ | |
859f6cb4 | 9268 | |
6d876e0b JM |
9269 | /* If the type has (or might have) a mutable component, that component |
9270 | might be modified. */ | |
9271 | if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type)) | |
67935995 MM |
9272 | type_quals &= ~TYPE_QUAL_CONST; |
9273 | ||
9804209d DG |
9274 | c_apply_type_quals_to_decl (type_quals, decl); |
9275 | } | |
9276 | ||
af7b9902 | 9277 | /* Subroutine of casts_away_constness. Make T1 and T2 point at |
33c25e5c | 9278 | exemplar types such that casting T1 to T2 is casting away constness |
af7b9902 MM |
9279 | if and only if there is no implicit conversion from T1 to T2. */ |
9280 | ||
9281 | static void | |
b40e334f | 9282 | casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain) |
af7b9902 MM |
9283 | { |
9284 | int quals1; | |
9285 | int quals2; | |
9286 | ||
9287 | /* [expr.const.cast] | |
9288 | ||
9289 | For multi-level pointer to members and multi-level mixed pointers | |
9290 | and pointers to members (conv.qual), the "member" aspect of a | |
9291 | pointer to member level is ignored when determining if a const | |
9292 | cv-qualifier has been cast away. */ | |
af7b9902 MM |
9293 | /* [expr.const.cast] |
9294 | ||
9295 | For two pointer types: | |
9296 | ||
0cbd7506 MS |
9297 | X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type |
9298 | X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type | |
9299 | K is min(N,M) | |
af7b9902 MM |
9300 | |
9301 | casting from X1 to X2 casts away constness if, for a non-pointer | |
9302 | type T there does not exist an implicit conversion (clause | |
9303 | _conv_) from: | |
9304 | ||
0cbd7506 | 9305 | Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N * |
c8094d83 | 9306 | |
af7b9902 MM |
9307 | to |
9308 | ||
0cbd7506 | 9309 | Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */ |
66b1156a PC |
9310 | if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1)) |
9311 | || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2))) | |
af7b9902 MM |
9312 | { |
9313 | *t1 = cp_build_qualified_type (void_type_node, | |
89d684bb | 9314 | cp_type_quals (*t1)); |
af7b9902 | 9315 | *t2 = cp_build_qualified_type (void_type_node, |
89d684bb | 9316 | cp_type_quals (*t2)); |
af7b9902 MM |
9317 | return; |
9318 | } | |
c8094d83 | 9319 | |
89d684bb BM |
9320 | quals1 = cp_type_quals (*t1); |
9321 | quals2 = cp_type_quals (*t2); | |
dad732fa | 9322 | |
66b1156a | 9323 | if (TYPE_PTRDATAMEM_P (*t1)) |
dad732fa MM |
9324 | *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1); |
9325 | else | |
9326 | *t1 = TREE_TYPE (*t1); | |
66b1156a | 9327 | if (TYPE_PTRDATAMEM_P (*t2)) |
dad732fa MM |
9328 | *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2); |
9329 | else | |
9330 | *t2 = TREE_TYPE (*t2); | |
9331 | ||
b40e334f | 9332 | casts_away_constness_r (t1, t2, complain); |
af7b9902 MM |
9333 | *t1 = build_pointer_type (*t1); |
9334 | *t2 = build_pointer_type (*t2); | |
9335 | *t1 = cp_build_qualified_type (*t1, quals1); | |
9336 | *t2 = cp_build_qualified_type (*t2, quals2); | |
9337 | } | |
9338 | ||
838dfd8a | 9339 | /* Returns nonzero if casting from TYPE1 to TYPE2 casts away |
36ef7262 MLI |
9340 | constness. |
9341 | ||
9342 | ??? This function returns non-zero if casting away qualifiers not | |
9343 | just const. We would like to return to the caller exactly which | |
9344 | qualifiers are casted away to give more accurate diagnostics. | |
9345 | */ | |
af7b9902 | 9346 | |
acd8e2d0 | 9347 | static bool |
b40e334f | 9348 | casts_away_constness (tree t1, tree t2, tsubst_flags_t complain) |
af7b9902 MM |
9349 | { |
9350 | if (TREE_CODE (t2) == REFERENCE_TYPE) | |
9351 | { | |
9352 | /* [expr.const.cast] | |
c8094d83 | 9353 | |
af7b9902 MM |
9354 | Casting from an lvalue of type T1 to an lvalue of type T2 |
9355 | using a reference cast casts away constness if a cast from an | |
9356 | rvalue of type "pointer to T1" to the type "pointer to T2" | |
9357 | casts away constness. */ | |
acd8e2d0 | 9358 | t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1); |
af7b9902 | 9359 | return casts_away_constness (build_pointer_type (t1), |
b40e334f PC |
9360 | build_pointer_type (TREE_TYPE (t2)), |
9361 | complain); | |
af7b9902 MM |
9362 | } |
9363 | ||
66b1156a | 9364 | if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2)) |
af7b9902 | 9365 | /* [expr.const.cast] |
c8094d83 | 9366 | |
af7b9902 MM |
9367 | Casting from an rvalue of type "pointer to data member of X |
9368 | of type T1" to the type "pointer to data member of Y of type | |
9369 | T2" casts away constness if a cast from an rvalue of type | |
aba649ba | 9370 | "pointer to T1" to the type "pointer to T2" casts away |
af7b9902 | 9371 | constness. */ |
3e0b4710 | 9372 | return casts_away_constness |
a5ac359a | 9373 | (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)), |
b40e334f PC |
9374 | build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)), |
9375 | complain); | |
af7b9902 MM |
9376 | |
9377 | /* Casting away constness is only something that makes sense for | |
9378 | pointer or reference types. */ | |
50e10fa8 | 9379 | if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2)) |
acd8e2d0 | 9380 | return false; |
af7b9902 MM |
9381 | |
9382 | /* Top-level qualifiers don't matter. */ | |
9383 | t1 = TYPE_MAIN_VARIANT (t1); | |
9384 | t2 = TYPE_MAIN_VARIANT (t2); | |
b40e334f PC |
9385 | casts_away_constness_r (&t1, &t2, complain); |
9386 | if (!can_convert (t2, t1, complain)) | |
acd8e2d0 | 9387 | return true; |
af7b9902 | 9388 | |
acd8e2d0 | 9389 | return false; |
af7b9902 | 9390 | } |
6816f040 | 9391 | |
ee76b931 MM |
9392 | /* If T is a REFERENCE_TYPE return the type to which T refers. |
9393 | Otherwise, return T itself. */ | |
9394 | ||
9395 | tree | |
9396 | non_reference (tree t) | |
9397 | { | |
c5ce25ce | 9398 | if (t && TREE_CODE (t) == REFERENCE_TYPE) |
ee76b931 MM |
9399 | t = TREE_TYPE (t); |
9400 | return t; | |
9401 | } | |
37dc0d8d JM |
9402 | |
9403 | ||
9404 | /* Return nonzero if REF is an lvalue valid for this language; | |
9405 | otherwise, print an error message and return zero. USE says | |
9406 | how the lvalue is being used and so selects the error message. */ | |
9407 | ||
9408 | int | |
4af9e878 | 9409 | lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain) |
37dc0d8d | 9410 | { |
4e9ca9b0 | 9411 | cp_lvalue_kind kind = lvalue_kind (ref); |
37dc0d8d | 9412 | |
4e9ca9b0 JM |
9413 | if (kind == clk_none) |
9414 | { | |
9415 | if (complain & tf_error) | |
7bd11157 | 9416 | lvalue_error (input_location, use); |
4e9ca9b0 JM |
9417 | return 0; |
9418 | } | |
9419 | else if (kind & (clk_rvalueref|clk_class)) | |
9420 | { | |
9421 | if (!(complain & tf_error)) | |
9422 | return 0; | |
9423 | if (kind & clk_class) | |
9424 | /* Make this a permerror because we used to accept it. */ | |
9425 | permerror (input_location, "using temporary as lvalue"); | |
9426 | else | |
9427 | error ("using xvalue (rvalue reference) as lvalue"); | |
9428 | } | |
9429 | return 1; | |
37dc0d8d | 9430 | } |
03a904b5 | 9431 | |
3ce4f9e4 ESR |
9432 | /* Return true if a user-defined literal operator is a raw operator. */ |
9433 | ||
9434 | bool | |
9435 | check_raw_literal_operator (const_tree decl) | |
9436 | { | |
9437 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
9438 | tree argtype; | |
9439 | int arity; | |
9440 | bool maybe_raw_p = false; | |
9441 | ||
9442 | /* Count the number and type of arguments and check for ellipsis. */ | |
9443 | for (argtype = argtypes, arity = 0; | |
9444 | argtype && argtype != void_list_node; | |
9445 | ++arity, argtype = TREE_CHAIN (argtype)) | |
9446 | { | |
9447 | tree t = TREE_VALUE (argtype); | |
9448 | ||
9449 | if (same_type_p (t, const_string_type_node)) | |
9450 | maybe_raw_p = true; | |
9451 | } | |
9452 | if (!argtype) | |
9453 | return false; /* Found ellipsis. */ | |
9454 | ||
9455 | if (!maybe_raw_p || arity != 1) | |
9456 | return false; | |
9457 | ||
9458 | return true; | |
9459 | } | |
9460 | ||
9461 | ||
9462 | /* Return true if a user-defined literal operator has one of the allowed | |
9463 | argument types. */ | |
9464 | ||
9465 | bool | |
9466 | check_literal_operator_args (const_tree decl, | |
9467 | bool *long_long_unsigned_p, bool *long_double_p) | |
9468 | { | |
9469 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
6e887223 JJ |
9470 | |
9471 | *long_long_unsigned_p = false; | |
9472 | *long_double_p = false; | |
d2027d2a ESR |
9473 | if (processing_template_decl || processing_specialization) |
9474 | return argtypes == void_list_node; | |
3ce4f9e4 ESR |
9475 | else |
9476 | { | |
9477 | tree argtype; | |
9478 | int arity; | |
9479 | int max_arity = 2; | |
3ce4f9e4 | 9480 | |
3ce4f9e4 ESR |
9481 | /* Count the number and type of arguments and check for ellipsis. */ |
9482 | for (argtype = argtypes, arity = 0; | |
9483 | argtype && argtype != void_list_node; | |
9484 | argtype = TREE_CHAIN (argtype)) | |
9485 | { | |
9486 | tree t = TREE_VALUE (argtype); | |
9487 | ++arity; | |
9488 | ||
50e10fa8 | 9489 | if (TYPE_PTR_P (t)) |
3ce4f9e4 | 9490 | { |
f05eec4a | 9491 | bool maybe_raw_p = false; |
24847495 JM |
9492 | t = TREE_TYPE (t); |
9493 | if (cp_type_quals (t) != TYPE_QUAL_CONST) | |
9494 | return false; | |
9495 | t = TYPE_MAIN_VARIANT (t); | |
f05eec4a ESR |
9496 | if ((maybe_raw_p = same_type_p (t, char_type_node)) |
9497 | || same_type_p (t, wchar_type_node) | |
9498 | || same_type_p (t, char16_type_node) | |
9499 | || same_type_p (t, char32_type_node)) | |
24847495 | 9500 | { |
f05eec4a ESR |
9501 | argtype = TREE_CHAIN (argtype); |
9502 | if (!argtype) | |
9503 | return false; | |
9504 | t = TREE_VALUE (argtype); | |
9505 | if (maybe_raw_p && argtype == void_list_node) | |
9506 | return true; | |
9507 | else if (same_type_p (t, size_type_node)) | |
9508 | { | |
9509 | ++arity; | |
9510 | continue; | |
9511 | } | |
9512 | else | |
9513 | return false; | |
24847495 | 9514 | } |
3ce4f9e4 ESR |
9515 | } |
9516 | else if (same_type_p (t, long_long_unsigned_type_node)) | |
9517 | { | |
9518 | max_arity = 1; | |
9519 | *long_long_unsigned_p = true; | |
9520 | } | |
9521 | else if (same_type_p (t, long_double_type_node)) | |
9522 | { | |
9523 | max_arity = 1; | |
9524 | *long_double_p = true; | |
9525 | } | |
9526 | else if (same_type_p (t, char_type_node)) | |
9527 | max_arity = 1; | |
9528 | else if (same_type_p (t, wchar_type_node)) | |
9529 | max_arity = 1; | |
9530 | else if (same_type_p (t, char16_type_node)) | |
9531 | max_arity = 1; | |
9532 | else if (same_type_p (t, char32_type_node)) | |
9533 | max_arity = 1; | |
9534 | else | |
9535 | return false; | |
9536 | } | |
9537 | if (!argtype) | |
9538 | return false; /* Found ellipsis. */ | |
9539 | ||
d2027d2a | 9540 | if (arity != max_arity) |
3ce4f9e4 ESR |
9541 | return false; |
9542 | ||
3ce4f9e4 ESR |
9543 | return true; |
9544 | } | |
9545 | } | |
1807ffc1 MS |
9546 | |
9547 | /* Always returns false since unlike C90, C++ has no concept of implicit | |
9548 | function declarations. */ | |
9549 | ||
9550 | bool | |
9551 | c_decl_implicit (const_tree) | |
9552 | { | |
9553 | return false; | |
9554 | } |