]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Build expressions with type checking for C++ compiler. |
d6a8bdff | 2 | Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
9e1e64ec | 3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
fbc8d2d3 | 4 | Free Software Foundation, Inc. |
8d08fdba MS |
5 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
6 | ||
f5adbb8d | 7 | This file is part of GCC. |
8d08fdba | 8 | |
f5adbb8d | 9 | GCC is free software; you can redistribute it and/or modify |
8d08fdba | 10 | it under the terms of the GNU General Public License as published by |
e77f031d | 11 | the Free Software Foundation; either version 3, or (at your option) |
8d08fdba MS |
12 | any later version. |
13 | ||
f5adbb8d | 14 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
20 | along with GCC; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ | |
8d08fdba MS |
22 | |
23 | ||
24 | /* This file is part of the C++ front end. | |
25 | It contains routines to build C++ expressions given their operands, | |
26 | including computing the types of the result, C and C++ specific error | |
5088b058 | 27 | checks, and some optimization. */ |
8d08fdba | 28 | |
8d08fdba | 29 | #include "config.h" |
8d052bc7 | 30 | #include "system.h" |
4977bab6 ZW |
31 | #include "coretypes.h" |
32 | #include "tm.h" | |
8d08fdba MS |
33 | #include "tree.h" |
34 | #include "rtl.h" | |
8f17b5c5 | 35 | #include "expr.h" |
8d08fdba | 36 | #include "cp-tree.h" |
a091679a | 37 | #include "tm_p.h" |
8d08fdba | 38 | #include "flags.h" |
e8abc66f | 39 | #include "output.h" |
12027a89 | 40 | #include "toplev.h" |
2a2b2d43 | 41 | #include "diagnostic.h" |
ff84991f | 42 | #include "intl.h" |
672a6f42 | 43 | #include "target.h" |
7b6d72fc | 44 | #include "convert.h" |
d063960a | 45 | #include "c-common.h" |
06d40de8 | 46 | #include "params.h" |
8d08fdba | 47 | |
b5791fdc | 48 | static tree pfn_from_ptrmemfunc (tree); |
a298680c | 49 | static tree delta_from_ptrmemfunc (tree); |
5ade1ed2 DG |
50 | static tree convert_for_assignment (tree, tree, const char *, tree, int, |
51 | tsubst_flags_t); | |
acd8e2d0 | 52 | static tree cp_pointer_int_sum (enum tree_code, tree, tree); |
5ade1ed2 DG |
53 | static tree rationalize_conditional_expr (enum tree_code, tree, |
54 | tsubst_flags_t); | |
acd8e2d0 | 55 | static int comp_ptr_ttypes_real (tree, tree, int); |
acd8e2d0 | 56 | static bool comp_except_types (tree, tree, bool); |
58f9752a | 57 | static bool comp_array_types (const_tree, const_tree, bool); |
acd8e2d0 | 58 | static tree pointer_diff (tree, tree, tree); |
08e17d9d | 59 | static tree get_delta_difference (tree, tree, bool, bool); |
acd8e2d0 NS |
60 | static void casts_away_constness_r (tree *, tree *); |
61 | static bool casts_away_constness (tree, tree); | |
62 | static void maybe_warn_about_returning_address_of_local (tree); | |
a723baf1 | 63 | static tree lookup_destructor (tree, tree, tree); |
5ade1ed2 DG |
64 | static int convert_arguments (int, tree *, tree, tree, tree, int, |
65 | tsubst_flags_t); | |
8d08fdba | 66 | |
8d08fdba | 67 | /* Do `exp = require_complete_type (exp);' to make sure exp |
ae58fa02 MM |
68 | does not have an incomplete type. (That includes void types.) |
69 | Returns the error_mark_node if the VALUE does not have | |
70 | complete type when this function returns. */ | |
8d08fdba MS |
71 | |
72 | tree | |
acd8e2d0 | 73 | require_complete_type (tree value) |
8d08fdba | 74 | { |
5566b478 MS |
75 | tree type; |
76 | ||
66543169 | 77 | if (processing_template_decl || value == error_mark_node) |
5566b478 MS |
78 | return value; |
79 | ||
2c73f9f5 ML |
80 | if (TREE_CODE (value) == OVERLOAD) |
81 | type = unknown_type_node; | |
82 | else | |
83 | type = TREE_TYPE (value); | |
8d08fdba | 84 | |
ae5cbc33 RS |
85 | if (type == error_mark_node) |
86 | return error_mark_node; | |
87 | ||
8d08fdba | 88 | /* First, detect a valid value with a complete type. */ |
d0f062fb | 89 | if (COMPLETE_TYPE_P (type)) |
8d08fdba MS |
90 | return value; |
91 | ||
66543169 | 92 | if (complete_type_or_else (type, value)) |
8f259df3 MM |
93 | return value; |
94 | else | |
ae58fa02 | 95 | return error_mark_node; |
8d08fdba MS |
96 | } |
97 | ||
8f259df3 MM |
98 | /* Try to complete TYPE, if it is incomplete. For example, if TYPE is |
99 | a template instantiation, do the instantiation. Returns TYPE, | |
100 | whether or not it could be completed, unless something goes | |
101 | horribly wrong, in which case the error_mark_node is returned. */ | |
102 | ||
5566b478 | 103 | tree |
acd8e2d0 | 104 | complete_type (tree type) |
5566b478 | 105 | { |
8857f91e MM |
106 | if (type == NULL_TREE) |
107 | /* Rather than crash, we return something sure to cause an error | |
108 | at some point. */ | |
109 | return error_mark_node; | |
110 | ||
d0f062fb | 111 | if (type == error_mark_node || COMPLETE_TYPE_P (type)) |
5566b478 | 112 | ; |
e349ee73 | 113 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) |
5566b478 MS |
114 | { |
115 | tree t = complete_type (TREE_TYPE (type)); | |
73bebd55 | 116 | unsigned int needs_constructing, has_nontrivial_dtor; |
1e2e9f54 | 117 | if (COMPLETE_TYPE_P (t) && !dependent_type_p (type)) |
6467930b | 118 | layout_type (type); |
73bebd55 | 119 | needs_constructing |
c73964b2 | 120 | = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t)); |
73bebd55 | 121 | has_nontrivial_dtor |
834c6dff | 122 | = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t)); |
73bebd55 JJ |
123 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
124 | { | |
125 | TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing; | |
126 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor; | |
127 | } | |
5566b478 | 128 | } |
7ddedda4 | 129 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type)) |
e76a2646 | 130 | instantiate_class_template (TYPE_MAIN_VARIANT (type)); |
5566b478 MS |
131 | |
132 | return type; | |
133 | } | |
134 | ||
5aa3396c | 135 | /* Like complete_type, but issue an error if the TYPE cannot be completed. |
be20e673 | 136 | VALUE is used for informative diagnostics. |
66543169 | 137 | Returns NULL_TREE if the type cannot be made complete. */ |
8f259df3 MM |
138 | |
139 | tree | |
be20e673 | 140 | complete_type_or_else (tree type, tree value) |
8f259df3 MM |
141 | { |
142 | type = complete_type (type); | |
ae58fa02 MM |
143 | if (type == error_mark_node) |
144 | /* We already issued an error. */ | |
145 | return NULL_TREE; | |
d0f062fb | 146 | else if (!COMPLETE_TYPE_P (type)) |
8f259df3 | 147 | { |
71205d17 | 148 | cxx_incomplete_type_diagnostic (value, type, DK_ERROR); |
8f259df3 MM |
149 | return NULL_TREE; |
150 | } | |
151 | else | |
152 | return type; | |
153 | } | |
154 | ||
8d08fdba | 155 | /* Return truthvalue of whether type of EXP is instantiated. */ |
e92cc029 | 156 | |
8d08fdba | 157 | int |
58f9752a | 158 | type_unknown_p (const_tree exp) |
8d08fdba | 159 | { |
26bcf8fc | 160 | return (TREE_CODE (exp) == TREE_LIST |
a5ac359a | 161 | || TREE_TYPE (exp) == unknown_type_node); |
8d08fdba MS |
162 | } |
163 | ||
8d08fdba MS |
164 | \f |
165 | /* Return the common type of two parameter lists. | |
96d84882 | 166 | We assume that comptypes has already been done and returned 1; |
8d08fdba MS |
167 | if that isn't so, this may crash. |
168 | ||
169 | As an optimization, free the space we allocate if the parameter | |
170 | lists are already common. */ | |
171 | ||
10b2bcdd | 172 | static tree |
5671bf27 | 173 | commonparms (tree p1, tree p2) |
8d08fdba MS |
174 | { |
175 | tree oldargs = p1, newargs, n; | |
176 | int i, len; | |
177 | int any_change = 0; | |
8d08fdba MS |
178 | |
179 | len = list_length (p1); | |
180 | newargs = tree_last (p1); | |
181 | ||
182 | if (newargs == void_list_node) | |
183 | i = 1; | |
184 | else | |
185 | { | |
186 | i = 0; | |
187 | newargs = 0; | |
188 | } | |
189 | ||
190 | for (; i < len; i++) | |
191 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); | |
192 | ||
193 | n = newargs; | |
194 | ||
195 | for (i = 0; p1; | |
196 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++) | |
197 | { | |
198 | if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2)) | |
199 | { | |
8d08fdba MS |
200 | TREE_PURPOSE (n) = TREE_PURPOSE (p1); |
201 | any_change = 1; | |
202 | } | |
203 | else if (! TREE_PURPOSE (p1)) | |
204 | { | |
205 | if (TREE_PURPOSE (p2)) | |
206 | { | |
207 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); | |
208 | any_change = 1; | |
209 | } | |
210 | } | |
211 | else | |
212 | { | |
1743ca29 | 213 | if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2))) |
a4443a08 | 214 | any_change = 1; |
8d08fdba MS |
215 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); |
216 | } | |
217 | if (TREE_VALUE (p1) != TREE_VALUE (p2)) | |
218 | { | |
219 | any_change = 1; | |
6da794e8 | 220 | TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2)); |
8d08fdba MS |
221 | } |
222 | else | |
223 | TREE_VALUE (n) = TREE_VALUE (p1); | |
224 | } | |
225 | if (! any_change) | |
970d6386 | 226 | return oldargs; |
8d08fdba MS |
227 | |
228 | return newargs; | |
229 | } | |
230 | ||
f2ee215b BK |
231 | /* Given a type, perhaps copied for a typedef, |
232 | find the "original" version of it. */ | |
993acaec | 233 | static tree |
5671bf27 | 234 | original_type (tree t) |
f2ee215b | 235 | { |
8e30dcf3 | 236 | int quals = cp_type_quals (t); |
2b643eda MM |
237 | while (t != error_mark_node |
238 | && TYPE_NAME (t) != NULL_TREE) | |
f2ee215b BK |
239 | { |
240 | tree x = TYPE_NAME (t); | |
241 | if (TREE_CODE (x) != TYPE_DECL) | |
242 | break; | |
243 | x = DECL_ORIGINAL_TYPE (x); | |
244 | if (x == NULL_TREE) | |
245 | break; | |
246 | t = x; | |
247 | } | |
8e30dcf3 | 248 | return cp_build_qualified_type (t, quals); |
f2ee215b BK |
249 | } |
250 | ||
a7a64a77 | 251 | /* T1 and T2 are arithmetic or enumeration types. Return the type |
aba649ba | 252 | that will result from the "usual arithmetic conversions" on T1 and |
a7a64a77 MM |
253 | T2 as described in [expr]. */ |
254 | ||
255 | tree | |
5671bf27 | 256 | type_after_usual_arithmetic_conversions (tree t1, tree t2) |
a7a64a77 MM |
257 | { |
258 | enum tree_code code1 = TREE_CODE (t1); | |
259 | enum tree_code code2 = TREE_CODE (t2); | |
260 | tree attributes; | |
261 | ||
262 | /* FIXME: Attributes. */ | |
c8094d83 | 263 | gcc_assert (ARITHMETIC_TYPE_P (t1) |
73042643 | 264 | || TREE_CODE (t1) == VECTOR_TYPE |
adf2edec | 265 | || UNSCOPED_ENUM_P (t1)); |
c8094d83 | 266 | gcc_assert (ARITHMETIC_TYPE_P (t2) |
01c15146 | 267 | || TREE_CODE (t2) == VECTOR_TYPE |
adf2edec | 268 | || UNSCOPED_ENUM_P (t2)); |
a7a64a77 | 269 | |
6da794e8 JM |
270 | /* In what follows, we slightly generalize the rules given in [expr] so |
271 | as to deal with `long long' and `complex'. First, merge the | |
272 | attributes. */ | |
f6897b10 | 273 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
a7a64a77 | 274 | |
6da794e8 JM |
275 | /* If one type is complex, form the common type of the non-complex |
276 | components, then make that complex. Use T1 or T2 if it is the | |
277 | required type. */ | |
278 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
279 | { | |
280 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
281 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
282 | tree subtype | |
283 | = type_after_usual_arithmetic_conversions (subtype1, subtype2); | |
284 | ||
285 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
286 | return build_type_attribute_variant (t1, attributes); | |
287 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
288 | return build_type_attribute_variant (t2, attributes); | |
289 | else | |
290 | return build_type_attribute_variant (build_complex_type (subtype), | |
291 | attributes); | |
292 | } | |
293 | ||
73042643 AH |
294 | if (code1 == VECTOR_TYPE) |
295 | { | |
296 | /* When we get here we should have two vectors of the same size. | |
297 | Just prefer the unsigned one if present. */ | |
298 | if (TYPE_UNSIGNED (t1)) | |
299 | return build_type_attribute_variant (t1, attributes); | |
300 | else | |
301 | return build_type_attribute_variant (t2, attributes); | |
302 | } | |
303 | ||
a7a64a77 MM |
304 | /* If only one is real, use it as the result. */ |
305 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
306 | return build_type_attribute_variant (t1, attributes); | |
307 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
308 | return build_type_attribute_variant (t2, attributes); | |
309 | ||
310 | /* Perform the integral promotions. */ | |
311 | if (code1 != REAL_TYPE) | |
312 | { | |
313 | t1 = type_promotes_to (t1); | |
314 | t2 = type_promotes_to (t2); | |
315 | } | |
316 | ||
317 | /* Both real or both integers; use the one with greater precision. */ | |
318 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
319 | return build_type_attribute_variant (t1, attributes); | |
320 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
321 | return build_type_attribute_variant (t2, attributes); | |
322 | ||
2f4d058f AH |
323 | /* The types are the same; no need to do anything fancy. */ |
324 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
325 | return build_type_attribute_variant (t1, attributes); | |
326 | ||
a7a64a77 MM |
327 | if (code1 != REAL_TYPE) |
328 | { | |
329 | /* If one is unsigned long long, then convert the other to unsigned | |
330 | long long. */ | |
331 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node) | |
332 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node)) | |
333 | return build_type_attribute_variant (long_long_unsigned_type_node, | |
334 | attributes); | |
335 | /* If one is a long long, and the other is an unsigned long, and | |
336 | long long can represent all the values of an unsigned long, then | |
337 | convert to a long long. Otherwise, convert to an unsigned long | |
338 | long. Otherwise, if either operand is long long, convert the | |
339 | other to long long. | |
c8094d83 | 340 | |
a7a64a77 MM |
341 | Since we're here, we know the TYPE_PRECISION is the same; |
342 | therefore converting to long long cannot represent all the values | |
343 | of an unsigned long, so we choose unsigned long long in that | |
344 | case. */ | |
345 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node) | |
346 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node)) | |
347 | { | |
8df83eae | 348 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
c8094d83 | 349 | ? long_long_unsigned_type_node |
a7a64a77 MM |
350 | : long_long_integer_type_node); |
351 | return build_type_attribute_variant (t, attributes); | |
352 | } | |
c8094d83 | 353 | |
a7a64a77 MM |
354 | /* Go through the same procedure, but for longs. */ |
355 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node) | |
356 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node)) | |
357 | return build_type_attribute_variant (long_unsigned_type_node, | |
358 | attributes); | |
359 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node) | |
360 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node)) | |
361 | { | |
8df83eae | 362 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
a7a64a77 MM |
363 | ? long_unsigned_type_node : long_integer_type_node); |
364 | return build_type_attribute_variant (t, attributes); | |
365 | } | |
366 | /* Otherwise prefer the unsigned one. */ | |
8df83eae | 367 | if (TYPE_UNSIGNED (t1)) |
a7a64a77 MM |
368 | return build_type_attribute_variant (t1, attributes); |
369 | else | |
370 | return build_type_attribute_variant (t2, attributes); | |
371 | } | |
372 | else | |
373 | { | |
374 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node) | |
375 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node)) | |
376 | return build_type_attribute_variant (long_double_type_node, | |
377 | attributes); | |
378 | if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node) | |
379 | || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node)) | |
380 | return build_type_attribute_variant (double_type_node, | |
381 | attributes); | |
2f4d058f AH |
382 | if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node) |
383 | || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node)) | |
a7a64a77 MM |
384 | return build_type_attribute_variant (float_type_node, |
385 | attributes); | |
c8094d83 | 386 | |
2f4d058f | 387 | /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of |
0cbd7506 MS |
388 | the standard C++ floating-point types. Logic earlier in this |
389 | function has already eliminated the possibility that | |
390 | TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no | |
391 | compelling reason to choose one or the other. */ | |
2f4d058f | 392 | return build_type_attribute_variant (t1, attributes); |
a7a64a77 MM |
393 | } |
394 | } | |
395 | ||
a5ac359a MM |
396 | /* Subroutine of composite_pointer_type to implement the recursive |
397 | case. See that function for documentation fo the parameters. */ | |
398 | ||
399 | static tree | |
5ade1ed2 DG |
400 | composite_pointer_type_r (tree t1, tree t2, const char* location, |
401 | tsubst_flags_t complain) | |
a5ac359a MM |
402 | { |
403 | tree pointee1; | |
404 | tree pointee2; | |
405 | tree result_type; | |
406 | tree attributes; | |
407 | ||
408 | /* Determine the types pointed to by T1 and T2. */ | |
409 | if (TREE_CODE (t1) == POINTER_TYPE) | |
410 | { | |
411 | pointee1 = TREE_TYPE (t1); | |
412 | pointee2 = TREE_TYPE (t2); | |
413 | } | |
414 | else | |
415 | { | |
416 | pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1); | |
417 | pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2); | |
418 | } | |
419 | ||
420 | /* [expr.rel] | |
421 | ||
422 | Otherwise, the composite pointer type is a pointer type | |
423 | similar (_conv.qual_) to the type of one of the operands, | |
424 | with a cv-qualification signature (_conv.qual_) that is the | |
425 | union of the cv-qualification signatures of the operand | |
426 | types. */ | |
427 | if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2)) | |
428 | result_type = pointee1; | |
429 | else if ((TREE_CODE (pointee1) == POINTER_TYPE | |
430 | && TREE_CODE (pointee2) == POINTER_TYPE) | |
431 | || (TYPE_PTR_TO_MEMBER_P (pointee1) | |
432 | && TYPE_PTR_TO_MEMBER_P (pointee2))) | |
5ade1ed2 DG |
433 | result_type = composite_pointer_type_r (pointee1, pointee2, location, |
434 | complain); | |
a5ac359a MM |
435 | else |
436 | { | |
5ade1ed2 | 437 | if (complain & tf_error) |
cbe5f3b3 | 438 | permerror (input_location, "%s between distinct pointer types %qT and %qT " |
37ec60ed JW |
439 | "lacks a cast", |
440 | location, t1, t2); | |
a5ac359a MM |
441 | result_type = void_type_node; |
442 | } | |
443 | result_type = cp_build_qualified_type (result_type, | |
444 | (cp_type_quals (pointee1) | |
445 | | cp_type_quals (pointee2))); | |
a5ac359a MM |
446 | /* If the original types were pointers to members, so is the |
447 | result. */ | |
448 | if (TYPE_PTR_TO_MEMBER_P (t1)) | |
449 | { | |
450 | if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), | |
5ade1ed2 DG |
451 | TYPE_PTRMEM_CLASS_TYPE (t2)) |
452 | && (complain & tf_error)) | |
cbe5f3b3 | 453 | permerror (input_location, "%s between distinct pointer types %qT and %qT " |
37ec60ed JW |
454 | "lacks a cast", |
455 | location, t1, t2); | |
a5ac359a MM |
456 | result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), |
457 | result_type); | |
458 | } | |
9d363a56 MM |
459 | else |
460 | result_type = build_pointer_type (result_type); | |
a5ac359a MM |
461 | |
462 | /* Merge the attributes. */ | |
463 | attributes = (*targetm.merge_type_attributes) (t1, t2); | |
464 | return build_type_attribute_variant (result_type, attributes); | |
465 | } | |
466 | ||
a7a64a77 MM |
467 | /* Return the composite pointer type (see [expr.rel]) for T1 and T2. |
468 | ARG1 and ARG2 are the values with those types. The LOCATION is a | |
c8094d83 | 469 | string describing the current location, in case an error occurs. |
a5ac359a MM |
470 | |
471 | This routine also implements the computation of a common type for | |
472 | pointers-to-members as per [expr.eq]. */ | |
a7a64a77 | 473 | |
c8094d83 | 474 | tree |
5671bf27 | 475 | composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2, |
5ade1ed2 | 476 | const char* location, tsubst_flags_t complain) |
a7a64a77 | 477 | { |
a5ac359a MM |
478 | tree class1; |
479 | tree class2; | |
a7a64a77 MM |
480 | |
481 | /* [expr.rel] | |
482 | ||
483 | If one operand is a null pointer constant, the composite pointer | |
484 | type is the type of the other operand. */ | |
485 | if (null_ptr_cst_p (arg1)) | |
486 | return t2; | |
487 | if (null_ptr_cst_p (arg2)) | |
488 | return t1; | |
c8094d83 | 489 | |
634790f4 MM |
490 | /* We have: |
491 | ||
492 | [expr.rel] | |
493 | ||
494 | If one of the operands has type "pointer to cv1 void*", then | |
495 | the other has type "pointer to cv2T", and the composite pointer | |
496 | type is "pointer to cv12 void", where cv12 is the union of cv1 | |
497 | and cv2. | |
498 | ||
499 | If either type is a pointer to void, make sure it is T1. */ | |
a5ac359a | 500 | if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2))) |
a7a64a77 | 501 | { |
634790f4 MM |
502 | tree t; |
503 | t = t1; | |
504 | t1 = t2; | |
505 | t2 = t; | |
a7a64a77 | 506 | } |
a5ac359a | 507 | |
634790f4 | 508 | /* Now, if T1 is a pointer to void, merge the qualifiers. */ |
a5ac359a | 509 | if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1))) |
a7a64a77 | 510 | { |
a5ac359a MM |
511 | tree attributes; |
512 | tree result_type; | |
513 | ||
fcf73884 | 514 | if (TYPE_PTRFN_P (t2) && (complain & tf_error)) |
509c9d60 | 515 | pedwarn (input_location, OPT_pedantic, "ISO C++ forbids %s " |
fcf73884 MLI |
516 | "between pointer of type %<void *%> and pointer-to-function", |
517 | location); | |
c8094d83 | 518 | result_type |
a5ac359a MM |
519 | = cp_build_qualified_type (void_type_node, |
520 | (cp_type_quals (TREE_TYPE (t1)) | |
521 | | cp_type_quals (TREE_TYPE (t2)))); | |
634790f4 | 522 | result_type = build_pointer_type (result_type); |
a5ac359a MM |
523 | /* Merge the attributes. */ |
524 | attributes = (*targetm.merge_type_attributes) (t1, t2); | |
525 | return build_type_attribute_variant (result_type, attributes); | |
526 | } | |
527 | ||
660845bf ZL |
528 | if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE |
529 | && TREE_CODE (t2) == POINTER_TYPE) | |
530 | { | |
531 | if (objc_compare_types (t1, t2, -3, NULL_TREE)) | |
532 | return t1; | |
533 | } | |
534 | ||
a5ac359a MM |
535 | /* [expr.eq] permits the application of a pointer conversion to |
536 | bring the pointers to a common type. */ | |
537 | if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE | |
538 | && CLASS_TYPE_P (TREE_TYPE (t1)) | |
539 | && CLASS_TYPE_P (TREE_TYPE (t2)) | |
540 | && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1), | |
541 | TREE_TYPE (t2))) | |
542 | { | |
543 | class1 = TREE_TYPE (t1); | |
544 | class2 = TREE_TYPE (t2); | |
545 | ||
660845bf | 546 | if (DERIVED_FROM_P (class1, class2)) |
c8094d83 | 547 | t2 = (build_pointer_type |
a5ac359a | 548 | (cp_build_qualified_type (class1, TYPE_QUALS (class2)))); |
660845bf | 549 | else if (DERIVED_FROM_P (class2, class1)) |
c8094d83 | 550 | t1 = (build_pointer_type |
a5ac359a MM |
551 | (cp_build_qualified_type (class2, TYPE_QUALS (class1)))); |
552 | else | |
553 | { | |
5ade1ed2 DG |
554 | if (complain & tf_error) |
555 | error ("%s between distinct pointer types %qT and %qT " | |
556 | "lacks a cast", location, t1, t2); | |
a5ac359a MM |
557 | return error_mark_node; |
558 | } | |
a7a64a77 | 559 | } |
a5ac359a MM |
560 | /* [expr.eq] permits the application of a pointer-to-member |
561 | conversion to change the class type of one of the types. */ | |
562 | else if (TYPE_PTR_TO_MEMBER_P (t1) | |
563 | && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), | |
564 | TYPE_PTRMEM_CLASS_TYPE (t2))) | |
708cae97 | 565 | { |
a5ac359a MM |
566 | class1 = TYPE_PTRMEM_CLASS_TYPE (t1); |
567 | class2 = TYPE_PTRMEM_CLASS_TYPE (t2); | |
708cae97 | 568 | |
a5ac359a MM |
569 | if (DERIVED_FROM_P (class1, class2)) |
570 | t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1)); | |
571 | else if (DERIVED_FROM_P (class2, class1)) | |
572 | t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2)); | |
a7a64a77 MM |
573 | else |
574 | { | |
5ade1ed2 DG |
575 | if (complain & tf_error) |
576 | error ("%s between distinct pointer-to-member types %qT and %qT " | |
577 | "lacks a cast", location, t1, t2); | |
a5ac359a | 578 | return error_mark_node; |
a7a64a77 MM |
579 | } |
580 | } | |
a7a64a77 | 581 | |
5ade1ed2 | 582 | return composite_pointer_type_r (t1, t2, location, complain); |
a7a64a77 MM |
583 | } |
584 | ||
6da794e8 | 585 | /* Return the merged type of two types. |
96d84882 | 586 | We assume that comptypes has already been done and returned 1; |
8d08fdba MS |
587 | if that isn't so, this may crash. |
588 | ||
6da794e8 JM |
589 | This just combines attributes and default arguments; any other |
590 | differences would cause the two types to compare unalike. */ | |
8d08fdba MS |
591 | |
592 | tree | |
5671bf27 | 593 | merge_types (tree t1, tree t2) |
8d08fdba | 594 | { |
926ce8bd KH |
595 | enum tree_code code1; |
596 | enum tree_code code2; | |
2986ae00 | 597 | tree attributes; |
8d08fdba MS |
598 | |
599 | /* Save time if the two types are the same. */ | |
f2ee215b | 600 | if (t1 == t2) |
9076e292 | 601 | return t1; |
6da794e8 | 602 | if (original_type (t1) == original_type (t2)) |
9076e292 | 603 | return t1; |
8d08fdba MS |
604 | |
605 | /* If one type is nonsense, use the other. */ | |
606 | if (t1 == error_mark_node) | |
9076e292 | 607 | return t2; |
8d08fdba | 608 | if (t2 == error_mark_node) |
9076e292 | 609 | return t1; |
8d08fdba | 610 | |
d9525bec | 611 | /* Merge the attributes. */ |
f6897b10 | 612 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
2986ae00 | 613 | |
5566b478 MS |
614 | if (TYPE_PTRMEMFUNC_P (t1)) |
615 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); | |
616 | if (TYPE_PTRMEMFUNC_P (t2)) | |
617 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); | |
618 | ||
8d08fdba MS |
619 | code1 = TREE_CODE (t1); |
620 | code2 = TREE_CODE (t2); | |
621 | ||
622 | switch (code1) | |
623 | { | |
8d08fdba MS |
624 | case POINTER_TYPE: |
625 | case REFERENCE_TYPE: | |
6da794e8 | 626 | /* For two pointers, do this recursively on the target type. */ |
8d08fdba | 627 | { |
6da794e8 | 628 | tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
77adef84 | 629 | int quals = cp_type_quals (t1); |
5b163de4 | 630 | |
8d08fdba | 631 | if (code1 == POINTER_TYPE) |
2986ae00 | 632 | t1 = build_pointer_type (target); |
8d08fdba | 633 | else |
86089be5 | 634 | t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1)); |
71851aaa | 635 | t1 = build_type_attribute_variant (t1, attributes); |
77adef84 | 636 | t1 = cp_build_qualified_type (t1, quals); |
71851aaa MS |
637 | |
638 | if (TREE_CODE (target) == METHOD_TYPE) | |
639 | t1 = build_ptrmemfunc_type (t1); | |
640 | ||
641 | return t1; | |
8d08fdba | 642 | } |
8d08fdba | 643 | |
6da794e8 JM |
644 | case OFFSET_TYPE: |
645 | { | |
fe0378ed MM |
646 | int quals; |
647 | tree pointee; | |
648 | quals = cp_type_quals (t1); | |
649 | pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1), | |
650 | TYPE_PTRMEM_POINTED_TO_TYPE (t2)); | |
651 | t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), | |
652 | pointee); | |
653 | t1 = cp_build_qualified_type (t1, quals); | |
6da794e8 JM |
654 | break; |
655 | } | |
656 | ||
8d08fdba MS |
657 | case ARRAY_TYPE: |
658 | { | |
6da794e8 | 659 | tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
8d08fdba MS |
660 | /* Save space: see if the result is identical to one of the args. */ |
661 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) | |
2986ae00 | 662 | return build_type_attribute_variant (t1, attributes); |
8d08fdba | 663 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) |
2986ae00 | 664 | return build_type_attribute_variant (t2, attributes); |
8d08fdba | 665 | /* Merge the element types, and have a size if either arg has one. */ |
e349ee73 MS |
666 | t1 = build_cplus_array_type |
667 | (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); | |
6da794e8 | 668 | break; |
8d08fdba MS |
669 | } |
670 | ||
671 | case FUNCTION_TYPE: | |
672 | /* Function types: prefer the one that specified arg types. | |
673 | If both do, merge the arg types. Also merge the return types. */ | |
674 | { | |
6da794e8 | 675 | tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
8d08fdba MS |
676 | tree p1 = TYPE_ARG_TYPES (t1); |
677 | tree p2 = TYPE_ARG_TYPES (t2); | |
678 | tree rval, raises; | |
679 | ||
680 | /* Save space: see if the result is identical to one of the args. */ | |
681 | if (valtype == TREE_TYPE (t1) && ! p2) | |
e9525111 | 682 | return cp_build_type_attribute_variant (t1, attributes); |
8d08fdba | 683 | if (valtype == TREE_TYPE (t2) && ! p1) |
e9525111 | 684 | return cp_build_type_attribute_variant (t2, attributes); |
8d08fdba MS |
685 | |
686 | /* Simple way if one arg fails to specify argument types. */ | |
687 | if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node) | |
688 | { | |
689 | rval = build_function_type (valtype, p2); | |
8926095f | 690 | if ((raises = TYPE_RAISES_EXCEPTIONS (t2))) |
f30432d7 | 691 | rval = build_exception_variant (rval, raises); |
e9525111 | 692 | return cp_build_type_attribute_variant (rval, attributes); |
8d08fdba MS |
693 | } |
694 | raises = TYPE_RAISES_EXCEPTIONS (t1); | |
695 | if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node) | |
696 | { | |
697 | rval = build_function_type (valtype, p1); | |
698 | if (raises) | |
f30432d7 | 699 | rval = build_exception_variant (rval, raises); |
e9525111 | 700 | return cp_build_type_attribute_variant (rval, attributes); |
8d08fdba MS |
701 | } |
702 | ||
703 | rval = build_function_type (valtype, commonparms (p1, p2)); | |
6da794e8 JM |
704 | t1 = build_exception_variant (rval, raises); |
705 | break; | |
8d08fdba MS |
706 | } |
707 | ||
6da794e8 JM |
708 | case METHOD_TYPE: |
709 | { | |
710 | /* Get this value the long way, since TYPE_METHOD_BASETYPE | |
711 | is just the main variant of this. */ | |
712 | tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2))); | |
713 | tree raises = TYPE_RAISES_EXCEPTIONS (t1); | |
714 | tree t3; | |
715 | ||
716 | /* If this was a member function type, get back to the | |
717 | original type of type member function (i.e., without | |
718 | the class instance variable up front. */ | |
719 | t1 = build_function_type (TREE_TYPE (t1), | |
720 | TREE_CHAIN (TYPE_ARG_TYPES (t1))); | |
721 | t2 = build_function_type (TREE_TYPE (t2), | |
722 | TREE_CHAIN (TYPE_ARG_TYPES (t2))); | |
723 | t3 = merge_types (t1, t2); | |
43dc123f MM |
724 | t3 = build_method_type_directly (basetype, TREE_TYPE (t3), |
725 | TYPE_ARG_TYPES (t3)); | |
6da794e8 JM |
726 | t1 = build_exception_variant (t3, raises); |
727 | break; | |
728 | } | |
8d08fdba | 729 | |
b1a95e0b MM |
730 | case TYPENAME_TYPE: |
731 | /* There is no need to merge attributes into a TYPENAME_TYPE. | |
732 | When the type is instantiated it will have whatever | |
733 | attributes result from the instantiation. */ | |
734 | return t1; | |
735 | ||
6da794e8 JM |
736 | default:; |
737 | } | |
8e30dcf3 JM |
738 | |
739 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) | |
740 | return t1; | |
741 | else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
742 | return t2; | |
743 | else | |
744 | return cp_build_type_attribute_variant (t1, attributes); | |
6da794e8 | 745 | } |
8d08fdba | 746 | |
6da794e8 | 747 | /* Return the common type of two types. |
96d84882 | 748 | We assume that comptypes has already been done and returned 1; |
6da794e8 | 749 | if that isn't so, this may crash. |
71851aaa | 750 | |
6da794e8 JM |
751 | This is the type for the result of most arithmetic operations |
752 | if the operands have the given two types. */ | |
753 | ||
754 | tree | |
5671bf27 | 755 | common_type (tree t1, tree t2) |
6da794e8 JM |
756 | { |
757 | enum tree_code code1; | |
758 | enum tree_code code2; | |
2986ae00 | 759 | |
6da794e8 JM |
760 | /* If one type is nonsense, bail. */ |
761 | if (t1 == error_mark_node || t2 == error_mark_node) | |
762 | return error_mark_node; | |
8d08fdba | 763 | |
6da794e8 JM |
764 | code1 = TREE_CODE (t1); |
765 | code2 = TREE_CODE (t2); | |
8d08fdba | 766 | |
adf2edec | 767 | if ((ARITHMETIC_TYPE_P (t1) || UNSCOPED_ENUM_P (t1) |
b8063b29 | 768 | || code1 == VECTOR_TYPE) |
adf2edec | 769 | && (ARITHMETIC_TYPE_P (t2) || UNSCOPED_ENUM_P (t2) |
b8063b29 | 770 | || code2 == VECTOR_TYPE)) |
6da794e8 JM |
771 | return type_after_usual_arithmetic_conversions (t1, t2); |
772 | ||
773 | else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2)) | |
774 | || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)) | |
775 | || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2))) | |
776 | return composite_pointer_type (t1, t2, error_mark_node, error_mark_node, | |
5ade1ed2 | 777 | "conversion", tf_warning_or_error); |
6da794e8 | 778 | else |
315fb5db | 779 | gcc_unreachable (); |
8d08fdba MS |
780 | } |
781 | \f | |
4cc1d462 | 782 | /* Compare two exception specifier types for exactness or subsetness, if |
acd8e2d0 NS |
783 | allowed. Returns false for mismatch, true for match (same, or |
784 | derived and !exact). | |
c8094d83 | 785 | |
4cc1d462 | 786 | [except.spec] "If a class X ... objects of class X or any class publicly |
34cd5ae7 | 787 | and unambiguously derived from X. Similarly, if a pointer type Y * ... |
4cc1d462 | 788 | exceptions of type Y * or that are pointers to any type publicly and |
34cd5ae7 | 789 | unambiguously derived from Y. Otherwise a function only allows exceptions |
4cc1d462 NS |
790 | that have the same type ..." |
791 | This does not mention cv qualifiers and is different to what throw | |
792 | [except.throw] and catch [except.catch] will do. They will ignore the | |
793 | top level cv qualifiers, and allow qualifiers in the pointer to class | |
794 | example. | |
c8094d83 | 795 | |
4cc1d462 NS |
796 | We implement the letter of the standard. */ |
797 | ||
acd8e2d0 NS |
798 | static bool |
799 | comp_except_types (tree a, tree b, bool exact) | |
4cc1d462 NS |
800 | { |
801 | if (same_type_p (a, b)) | |
acd8e2d0 | 802 | return true; |
4cc1d462 NS |
803 | else if (!exact) |
804 | { | |
89d684bb | 805 | if (cp_type_quals (a) || cp_type_quals (b)) |
0cbd7506 | 806 | return false; |
c8094d83 | 807 | |
4cc1d462 | 808 | if (TREE_CODE (a) == POINTER_TYPE |
0cbd7506 MS |
809 | && TREE_CODE (b) == POINTER_TYPE) |
810 | { | |
811 | a = TREE_TYPE (a); | |
812 | b = TREE_TYPE (b); | |
813 | if (cp_type_quals (a) || cp_type_quals (b)) | |
814 | return false; | |
815 | } | |
c8094d83 | 816 | |
4cc1d462 | 817 | if (TREE_CODE (a) != RECORD_TYPE |
0cbd7506 MS |
818 | || TREE_CODE (b) != RECORD_TYPE) |
819 | return false; | |
c8094d83 | 820 | |
18e4be85 | 821 | if (PUBLICLY_UNIQUELY_DERIVED_P (a, b)) |
0cbd7506 | 822 | return true; |
4cc1d462 | 823 | } |
acd8e2d0 | 824 | return false; |
4cc1d462 NS |
825 | } |
826 | ||
acd8e2d0 NS |
827 | /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers. |
828 | If EXACT is false, T2 can be stricter than T1 (according to 15.4/7), | |
4cc1d462 NS |
829 | otherwise it must be exact. Exception lists are unordered, but |
830 | we've already filtered out duplicates. Most lists will be in order, | |
831 | we should try to make use of that. */ | |
e92cc029 | 832 | |
acd8e2d0 | 833 | bool |
58f9752a | 834 | comp_except_specs (const_tree t1, const_tree t2, bool exact) |
8d08fdba | 835 | { |
58f9752a KG |
836 | const_tree probe; |
837 | const_tree base; | |
4cc1d462 NS |
838 | int length = 0; |
839 | ||
840 | if (t1 == t2) | |
acd8e2d0 | 841 | return true; |
c8094d83 | 842 | |
0cbd7506 | 843 | if (t1 == NULL_TREE) /* T1 is ... */ |
4cc1d462 | 844 | return t2 == NULL_TREE || !exact; |
0cbd7506 | 845 | if (!TREE_VALUE (t1)) /* t1 is EMPTY */ |
4cc1d462 | 846 | return t2 != NULL_TREE && !TREE_VALUE (t2); |
0cbd7506 | 847 | if (t2 == NULL_TREE) /* T2 is ... */ |
acd8e2d0 | 848 | return false; |
dffa4176 | 849 | if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */ |
4cc1d462 | 850 | return !exact; |
c8094d83 | 851 | |
4cc1d462 NS |
852 | /* Neither set is ... or EMPTY, make sure each part of T2 is in T1. |
853 | Count how many we find, to determine exactness. For exact matching and | |
854 | ordered T1, T2, this is an O(n) operation, otherwise its worst case is | |
855 | O(nm). */ | |
856 | for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2)) | |
857 | { | |
858 | for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe)) | |
0cbd7506 MS |
859 | { |
860 | tree a = TREE_VALUE (probe); | |
861 | tree b = TREE_VALUE (t2); | |
862 | ||
863 | if (comp_except_types (a, b, exact)) | |
864 | { | |
865 | if (probe == base && exact) | |
866 | base = TREE_CHAIN (probe); | |
867 | length++; | |
868 | break; | |
869 | } | |
870 | } | |
4cc1d462 | 871 | if (probe == NULL_TREE) |
0cbd7506 | 872 | return false; |
4cc1d462 NS |
873 | } |
874 | return !exact || base == NULL_TREE || length == list_length (t1); | |
8d08fdba MS |
875 | } |
876 | ||
acd8e2d0 NS |
877 | /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if |
878 | [] can match [size]. */ | |
3bfdc719 | 879 | |
c8a209ca | 880 | static bool |
58f9752a | 881 | comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration) |
8d08fdba | 882 | { |
3bfdc719 MM |
883 | tree d1; |
884 | tree d2; | |
30a03508 | 885 | tree max1, max2; |
3bfdc719 MM |
886 | |
887 | if (t1 == t2) | |
acd8e2d0 | 888 | return true; |
8d08fdba | 889 | |
3bfdc719 | 890 | /* The type of the array elements must be the same. */ |
acd8e2d0 | 891 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
c8a209ca | 892 | return false; |
8d08fdba | 893 | |
3bfdc719 MM |
894 | d1 = TYPE_DOMAIN (t1); |
895 | d2 = TYPE_DOMAIN (t2); | |
896 | ||
897 | if (d1 == d2) | |
c8a209ca | 898 | return true; |
8d08fdba | 899 | |
3bfdc719 | 900 | /* If one of the arrays is dimensionless, and the other has a |
0e339752 | 901 | dimension, they are of different types. However, it is valid to |
3bfdc719 MM |
902 | write: |
903 | ||
904 | extern int a[]; | |
905 | int a[3]; | |
906 | ||
c8094d83 | 907 | by [basic.link]: |
3bfdc719 MM |
908 | |
909 | declarations for an array object can specify | |
910 | array types that differ by the presence or absence of a major | |
911 | array bound (_dcl.array_). */ | |
912 | if (!d1 || !d2) | |
acd8e2d0 | 913 | return allow_redeclaration; |
3bfdc719 MM |
914 | |
915 | /* Check that the dimensions are the same. */ | |
30a03508 NS |
916 | |
917 | if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))) | |
918 | return false; | |
919 | max1 = TYPE_MAX_VALUE (d1); | |
920 | max2 = TYPE_MAX_VALUE (d2); | |
921 | if (processing_template_decl && !abi_version_at_least (2) | |
922 | && !value_dependent_expression_p (max1) | |
923 | && !value_dependent_expression_p (max2)) | |
924 | { | |
925 | /* With abi-1 we do not fold non-dependent array bounds, (and | |
0cbd7506 MS |
926 | consequently mangle them incorrectly). We must therefore |
927 | fold them here, to verify the domains have the same | |
928 | value. */ | |
30a03508 NS |
929 | max1 = fold (max1); |
930 | max2 = fold (max2); | |
931 | } | |
932 | ||
933 | if (!cp_tree_equal (max1, max2)) | |
934 | return false; | |
935 | ||
936 | return true; | |
8d08fdba MS |
937 | } |
938 | ||
96d84882 | 939 | /* Subroutine in comptypes. */ |
e92cc029 | 940 | |
06d40de8 DG |
941 | static bool |
942 | structural_comptypes (tree t1, tree t2, int strict) | |
8d08fdba | 943 | { |
8d08fdba | 944 | if (t1 == t2) |
c8a209ca NS |
945 | return true; |
946 | ||
f4f206f4 | 947 | /* Suppress errors caused by previously reported errors. */ |
ae8803a8 | 948 | if (t1 == error_mark_node || t2 == error_mark_node) |
c8a209ca | 949 | return false; |
c8094d83 | 950 | |
50bc768d | 951 | gcc_assert (TYPE_P (t1) && TYPE_P (t2)); |
c8094d83 | 952 | |
c8a209ca NS |
953 | /* TYPENAME_TYPEs should be resolved if the qualifying scope is the |
954 | current instantiation. */ | |
955 | if (TREE_CODE (t1) == TYPENAME_TYPE) | |
4195a767 | 956 | t1 = resolve_typename_type (t1, /*only_current_p=*/true); |
c8094d83 | 957 | |
c8a209ca | 958 | if (TREE_CODE (t2) == TYPENAME_TYPE) |
4195a767 | 959 | t2 = resolve_typename_type (t2, /*only_current_p=*/true); |
8d08fdba | 960 | |
5566b478 MS |
961 | if (TYPE_PTRMEMFUNC_P (t1)) |
962 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); | |
963 | if (TYPE_PTRMEMFUNC_P (t2)) | |
964 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); | |
965 | ||
8d08fdba | 966 | /* Different classes of types can't be compatible. */ |
8d08fdba | 967 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
c8a209ca | 968 | return false; |
8d08fdba | 969 | |
c353b8e3 MM |
970 | /* Qualifiers must match. For array types, we will check when we |
971 | recur on the array element types. */ | |
972 | if (TREE_CODE (t1) != ARRAY_TYPE | |
973 | && TYPE_QUALS (t1) != TYPE_QUALS (t2)) | |
c8a209ca | 974 | return false; |
70fef63a RG |
975 | if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2)) |
976 | return false; | |
8d08fdba MS |
977 | |
978 | /* Allow for two different type nodes which have essentially the same | |
979 | definition. Note that we already checked for equality of the type | |
38e01259 | 980 | qualifiers (just above). */ |
8d08fdba | 981 | |
c353b8e3 MM |
982 | if (TREE_CODE (t1) != ARRAY_TYPE |
983 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
c8a209ca | 984 | return true; |
8d08fdba | 985 | |
7866705a | 986 | /* Compare the types. Break out if they could be the same. */ |
8d08fdba MS |
987 | switch (TREE_CODE (t1)) |
988 | { | |
0451301c DG |
989 | case VOID_TYPE: |
990 | case BOOLEAN_TYPE: | |
991 | /* All void and bool types are the same. */ | |
992 | break; | |
993 | ||
994 | case INTEGER_TYPE: | |
995 | case FIXED_POINT_TYPE: | |
996 | case REAL_TYPE: | |
997 | /* With these nodes, we can't determine type equivalence by | |
998 | looking at what is stored in the nodes themselves, because | |
999 | two nodes might have different TYPE_MAIN_VARIANTs but still | |
1000 | represent the same type. For example, wchar_t and int could | |
1001 | have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE, | |
1002 | TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs | |
1003 | and are distinct types. On the other hand, int and the | |
1004 | following typedef | |
1005 | ||
1006 | typedef int INT __attribute((may_alias)); | |
1007 | ||
1008 | have identical properties, different TYPE_MAIN_VARIANTs, but | |
1009 | represent the same type. The canonical type system keeps | |
1010 | track of equivalence in this case, so we fall back on it. */ | |
1011 | return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); | |
1012 | ||
e7e66632 | 1013 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 1014 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
e7e66632 | 1015 | if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2) |
5d80a306 DG |
1016 | || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2) |
1017 | || (TEMPLATE_TYPE_PARAMETER_PACK (t1) | |
1018 | != TEMPLATE_TYPE_PARAMETER_PACK (t2))) | |
c8a209ca NS |
1019 | return false; |
1020 | if (!comp_template_parms | |
1021 | (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)), | |
1022 | DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2)))) | |
1023 | return false; | |
a1281f45 | 1024 | if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM) |
7866705a | 1025 | break; |
e7e66632 | 1026 | /* Don't check inheritance. */ |
acd8e2d0 | 1027 | strict = COMPARE_STRICT; |
f4f206f4 | 1028 | /* Fall through. */ |
e7e66632 | 1029 | |
8d08fdba MS |
1030 | case RECORD_TYPE: |
1031 | case UNION_TYPE: | |
7ddedda4 MM |
1032 | if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2) |
1033 | && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2) | |
c8a209ca NS |
1034 | || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM) |
1035 | && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2))) | |
7866705a | 1036 | break; |
c8094d83 | 1037 | |
acd8e2d0 | 1038 | if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2)) |
7866705a | 1039 | break; |
acd8e2d0 | 1040 | else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1)) |
7866705a | 1041 | break; |
c8094d83 | 1042 | |
c8a209ca | 1043 | return false; |
8d08fdba MS |
1044 | |
1045 | case OFFSET_TYPE: | |
96d84882 PB |
1046 | if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2), |
1047 | strict & ~COMPARE_REDECLARATION)) | |
c8a209ca | 1048 | return false; |
7866705a SB |
1049 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1050 | return false; | |
1051 | break; | |
8d08fdba | 1052 | |
8d08fdba | 1053 | case REFERENCE_TYPE: |
8af2fec4 RY |
1054 | if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2)) |
1055 | return false; | |
1056 | /* fall through to checks for pointer types */ | |
1057 | ||
1058 | case POINTER_TYPE: | |
7866705a SB |
1059 | if (TYPE_MODE (t1) != TYPE_MODE (t2) |
1060 | || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2) | |
1061 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) | |
1062 | return false; | |
1063 | break; | |
8d08fdba | 1064 | |
dffa4176 | 1065 | case METHOD_TYPE: |
8d08fdba | 1066 | case FUNCTION_TYPE: |
acd8e2d0 | 1067 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
c8a209ca | 1068 | return false; |
7866705a SB |
1069 | if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))) |
1070 | return false; | |
1071 | break; | |
8d08fdba MS |
1072 | |
1073 | case ARRAY_TYPE: | |
9bcb9aae | 1074 | /* Target types must match incl. qualifiers. */ |
7866705a SB |
1075 | if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION))) |
1076 | return false; | |
1077 | break; | |
8d08fdba | 1078 | |
51c184be | 1079 | case TEMPLATE_TYPE_PARM: |
7866705a | 1080 | if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2) |
5d80a306 DG |
1081 | || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2) |
1082 | || (TEMPLATE_TYPE_PARAMETER_PACK (t1) | |
1083 | != TEMPLATE_TYPE_PARAMETER_PACK (t2))) | |
7866705a SB |
1084 | return false; |
1085 | break; | |
e76a2646 MS |
1086 | |
1087 | case TYPENAME_TYPE: | |
c8a209ca NS |
1088 | if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1), |
1089 | TYPENAME_TYPE_FULLNAME (t2))) | |
0cbd7506 | 1090 | return false; |
7866705a SB |
1091 | if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))) |
1092 | return false; | |
1093 | break; | |
7f85441b | 1094 | |
b8c6534b | 1095 | case UNBOUND_CLASS_TEMPLATE: |
c8a209ca | 1096 | if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2))) |
0cbd7506 | 1097 | return false; |
7866705a SB |
1098 | if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))) |
1099 | return false; | |
1100 | break; | |
b8c6534b | 1101 | |
a7a64a77 | 1102 | case COMPLEX_TYPE: |
7866705a SB |
1103 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1104 | return false; | |
1105 | break; | |
a7a64a77 | 1106 | |
cc27e657 | 1107 | case VECTOR_TYPE: |
7866705a SB |
1108 | if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2) |
1109 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) | |
1110 | return false; | |
cc27e657 PB |
1111 | break; |
1112 | ||
5d80a306 DG |
1113 | case TYPE_PACK_EXPANSION: |
1114 | return same_type_p (PACK_EXPANSION_PATTERN (t1), | |
1115 | PACK_EXPANSION_PATTERN (t2)); | |
1116 | ||
3ad6a8e1 DG |
1117 | case DECLTYPE_TYPE: |
1118 | if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1) | |
1119 | != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2) | |
1120 | || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), | |
1121 | DECLTYPE_TYPE_EXPR (t2))) | |
1122 | return false; | |
1123 | break; | |
1124 | ||
7f85441b | 1125 | default: |
7866705a | 1126 | return false; |
8d08fdba | 1127 | } |
7866705a SB |
1128 | |
1129 | /* If we get here, we know that from a target independent POV the | |
1130 | types are the same. Make sure the target attributes are also | |
1131 | the same. */ | |
1132 | return targetm.comp_type_attributes (t1, t2); | |
8d08fdba MS |
1133 | } |
1134 | ||
06d40de8 DG |
1135 | /* Return true if T1 and T2 are related as allowed by STRICT. STRICT |
1136 | is a bitwise-or of the COMPARE_* flags. */ | |
1137 | ||
1138 | bool | |
96d84882 | 1139 | comptypes (tree t1, tree t2, int strict) |
06d40de8 DG |
1140 | { |
1141 | if (strict == COMPARE_STRICT) | |
1142 | { | |
06d40de8 DG |
1143 | if (t1 == t2) |
1144 | return true; | |
1145 | ||
1146 | if (t1 == error_mark_node || t2 == error_mark_node) | |
1147 | return false; | |
1148 | ||
1149 | if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2)) | |
1150 | /* At least one of the types requires structural equality, so | |
1151 | perform a deep check. */ | |
1152 | return structural_comptypes (t1, t2, strict); | |
1153 | ||
7313518b DG |
1154 | #ifdef ENABLE_CHECKING |
1155 | if (USE_CANONICAL_TYPES) | |
06d40de8 | 1156 | { |
7313518b DG |
1157 | bool result = structural_comptypes (t1, t2, strict); |
1158 | ||
06d40de8 | 1159 | if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2)) |
7313518b DG |
1160 | /* The two types are structurally equivalent, but their |
1161 | canonical types were different. This is a failure of the | |
1162 | canonical type propagation code.*/ | |
1163 | internal_error | |
1164 | ("canonical types differ for identical types %T and %T", | |
1165 | t1, t2); | |
06d40de8 | 1166 | else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)) |
7313518b DG |
1167 | /* Two types are structurally different, but the canonical |
1168 | types are the same. This means we were over-eager in | |
1169 | assigning canonical types. */ | |
1170 | internal_error | |
1171 | ("same canonical type node for different types %T and %T", | |
1172 | t1, t2); | |
06d40de8 DG |
1173 | |
1174 | return result; | |
1175 | } | |
7313518b DG |
1176 | #else |
1177 | if (USE_CANONICAL_TYPES) | |
06d40de8 | 1178 | return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); |
7313518b DG |
1179 | #endif |
1180 | else | |
1181 | return structural_comptypes (t1, t2, strict); | |
06d40de8 DG |
1182 | } |
1183 | else if (strict == COMPARE_STRUCTURAL) | |
1184 | return structural_comptypes (t1, t2, COMPARE_STRICT); | |
1185 | else | |
1186 | return structural_comptypes (t1, t2, strict); | |
1187 | } | |
1188 | ||
91063b51 MM |
1189 | /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */ |
1190 | ||
acd8e2d0 | 1191 | bool |
58f9752a | 1192 | at_least_as_qualified_p (const_tree type1, const_tree type2) |
91063b51 | 1193 | { |
acd8e2d0 NS |
1194 | int q1 = cp_type_quals (type1); |
1195 | int q2 = cp_type_quals (type2); | |
c8094d83 | 1196 | |
91063b51 | 1197 | /* All qualifiers for TYPE2 must also appear in TYPE1. */ |
acd8e2d0 | 1198 | return (q1 & q2) == q2; |
91063b51 MM |
1199 | } |
1200 | ||
ceab47eb MM |
1201 | /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is |
1202 | more cv-qualified that TYPE1, and 0 otherwise. */ | |
1203 | ||
1204 | int | |
58f9752a | 1205 | comp_cv_qualification (const_tree type1, const_tree type2) |
ceab47eb | 1206 | { |
acd8e2d0 NS |
1207 | int q1 = cp_type_quals (type1); |
1208 | int q2 = cp_type_quals (type2); | |
1209 | ||
1210 | if (q1 == q2) | |
ceab47eb MM |
1211 | return 0; |
1212 | ||
acd8e2d0 | 1213 | if ((q1 & q2) == q2) |
ceab47eb | 1214 | return 1; |
acd8e2d0 | 1215 | else if ((q1 & q2) == q1) |
ceab47eb MM |
1216 | return -1; |
1217 | ||
1218 | return 0; | |
1219 | } | |
1220 | ||
1221 | /* Returns 1 if the cv-qualification signature of TYPE1 is a proper | |
1222 | subset of the cv-qualification signature of TYPE2, and the types | |
1223 | are similar. Returns -1 if the other way 'round, and 0 otherwise. */ | |
1224 | ||
1225 | int | |
acd8e2d0 | 1226 | comp_cv_qual_signature (tree type1, tree type2) |
ceab47eb MM |
1227 | { |
1228 | if (comp_ptr_ttypes_real (type2, type1, -1)) | |
1229 | return 1; | |
1230 | else if (comp_ptr_ttypes_real (type1, type2, -1)) | |
1231 | return -1; | |
1232 | else | |
1233 | return 0; | |
1234 | } | |
8d08fdba | 1235 | \f |
96d84882 | 1236 | /* Subroutines of `comptypes'. */ |
8d08fdba | 1237 | |
acd8e2d0 | 1238 | /* Return true if two parameter type lists PARMS1 and PARMS2 are |
03017874 MM |
1239 | equivalent in the sense that functions with those parameter types |
1240 | can have equivalent types. The two lists must be equivalent, | |
acd8e2d0 | 1241 | element by element. */ |
8d08fdba | 1242 | |
acd8e2d0 | 1243 | bool |
58f9752a | 1244 | compparms (const_tree parms1, const_tree parms2) |
8d08fdba | 1245 | { |
58f9752a | 1246 | const_tree t1, t2; |
8d08fdba MS |
1247 | |
1248 | /* An unspecified parmlist matches any specified parmlist | |
1249 | whose argument types don't need default promotions. */ | |
1250 | ||
acd8e2d0 NS |
1251 | for (t1 = parms1, t2 = parms2; |
1252 | t1 || t2; | |
1253 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) | |
8d08fdba | 1254 | { |
8d08fdba | 1255 | /* If one parmlist is shorter than the other, |
4daa9c14 | 1256 | they fail to match. */ |
acd8e2d0 NS |
1257 | if (!t1 || !t2) |
1258 | return false; | |
ae8803a8 | 1259 | if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) |
acd8e2d0 | 1260 | return false; |
8d08fdba | 1261 | } |
acd8e2d0 | 1262 | return true; |
8d08fdba MS |
1263 | } |
1264 | ||
8d08fdba | 1265 | \f |
7a18b933 NS |
1266 | /* Process a sizeof or alignof expression where the operand is a |
1267 | type. */ | |
1268 | ||
8d08fdba | 1269 | tree |
7a18b933 | 1270 | cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain) |
8d08fdba | 1271 | { |
fa72b064 | 1272 | tree value; |
b4c74ba2 | 1273 | bool dependent_p; |
8d08fdba | 1274 | |
50bc768d | 1275 | gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR); |
7a18b933 NS |
1276 | if (type == error_mark_node) |
1277 | return error_mark_node; | |
c8094d83 | 1278 | |
ee76b931 | 1279 | type = non_reference (type); |
3c17e16e | 1280 | if (TREE_CODE (type) == METHOD_TYPE) |
8d08fdba | 1281 | { |
fcf73884 | 1282 | if (complain) |
509c9d60 | 1283 | pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
fcf73884 | 1284 | "invalid application of %qs to a member function", |
3c17e16e | 1285 | operator_name_info[(int) op].name); |
fa72b064 | 1286 | value = size_one_node; |
8d08fdba | 1287 | } |
8d08fdba | 1288 | |
b4c74ba2 MM |
1289 | dependent_p = dependent_type_p (type); |
1290 | if (!dependent_p) | |
1291 | complete_type (type); | |
1292 | if (dependent_p | |
3c17e16e MM |
1293 | /* VLA types will have a non-constant size. In the body of an |
1294 | uninstantiated template, we don't need to try to compute the | |
1295 | value, because the sizeof expression is not an integral | |
1296 | constant expression in that case. And, if we do try to | |
1297 | compute the value, we'll likely end up with SAVE_EXPRs, which | |
1298 | the template substitution machinery does not expect to see. */ | |
b4c74ba2 MM |
1299 | || (processing_template_decl |
1300 | && COMPLETE_TYPE_P (type) | |
1301 | && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)) | |
3c17e16e MM |
1302 | { |
1303 | value = build_min (op, size_type_node, type); | |
1304 | TREE_READONLY (value) = 1; | |
1305 | return value; | |
1306 | } | |
1307 | ||
1308 | return c_sizeof_or_alignof_type (complete_type (type), | |
1309 | op == SIZEOF_EXPR, | |
1310 | complain); | |
8d08fdba MS |
1311 | } |
1312 | ||
5ade1ed2 DG |
1313 | /* Return the size of the type, without producing any warnings for |
1314 | types whose size cannot be taken. This routine should be used only | |
1315 | in some other routine that has already produced a diagnostic about | |
1316 | using the size of such a type. */ | |
1317 | tree | |
1318 | cxx_sizeof_nowarn (tree type) | |
1319 | { | |
1320 | if (TREE_CODE (type) == FUNCTION_TYPE | |
1321 | || TREE_CODE (type) == VOID_TYPE | |
1322 | || TREE_CODE (type) == ERROR_MARK) | |
1323 | return size_one_node; | |
1324 | else if (!COMPLETE_TYPE_P (type)) | |
1325 | return size_zero_node; | |
1326 | else | |
1327 | return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false); | |
1328 | } | |
1329 | ||
229970c4 | 1330 | /* Process a sizeof expression where the operand is an expression. */ |
7a18b933 | 1331 | |
229970c4 | 1332 | static tree |
5ade1ed2 | 1333 | cxx_sizeof_expr (tree e, tsubst_flags_t complain) |
5566b478 | 1334 | { |
7a18b933 NS |
1335 | if (e == error_mark_node) |
1336 | return error_mark_node; | |
c8094d83 | 1337 | |
5156628f | 1338 | if (processing_template_decl) |
7a18b933 | 1339 | { |
229970c4 | 1340 | e = build_min (SIZEOF_EXPR, size_type_node, e); |
7a18b933 NS |
1341 | TREE_SIDE_EFFECTS (e) = 0; |
1342 | TREE_READONLY (e) = 1; | |
c8094d83 | 1343 | |
7a18b933 NS |
1344 | return e; |
1345 | } | |
c8094d83 | 1346 | |
5566b478 | 1347 | if (TREE_CODE (e) == COMPONENT_REF |
ea0e2a51 | 1348 | && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL |
162bc98d | 1349 | && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1))) |
5566b478 | 1350 | { |
5ade1ed2 DG |
1351 | if (complain & tf_error) |
1352 | error ("invalid application of %<sizeof%> to a bit-field"); | |
1353 | else | |
1354 | return error_mark_node; | |
7a18b933 NS |
1355 | e = char_type_node; |
1356 | } | |
1357 | else if (is_overloaded_fn (e)) | |
1358 | { | |
5ade1ed2 | 1359 | if (complain & tf_error) |
cbe5f3b3 | 1360 | permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of " |
37ec60ed | 1361 | "function type"); |
5ade1ed2 DG |
1362 | else |
1363 | return error_mark_node; | |
7a18b933 | 1364 | e = char_type_node; |
5566b478 | 1365 | } |
05e0b2f4 JM |
1366 | else if (type_unknown_p (e)) |
1367 | { | |
5ade1ed2 DG |
1368 | if (complain & tf_error) |
1369 | cxx_incomplete_type_error (e, TREE_TYPE (e)); | |
1370 | else | |
1371 | return error_mark_node; | |
7a18b933 | 1372 | e = char_type_node; |
05e0b2f4 | 1373 | } |
7a18b933 NS |
1374 | else |
1375 | e = TREE_TYPE (e); | |
c8094d83 | 1376 | |
5ade1ed2 | 1377 | return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error); |
5566b478 | 1378 | } |
c8094d83 | 1379 | |
229970c4 | 1380 | /* Implement the __alignof keyword: Return the minimum required |
69d1a403 | 1381 | alignment of E, measured in bytes. For VAR_DECL's and |
229970c4 JM |
1382 | FIELD_DECL's return DECL_ALIGN (which can be set from an |
1383 | "aligned" __attribute__ specification). */ | |
1384 | ||
1385 | static tree | |
5ade1ed2 | 1386 | cxx_alignof_expr (tree e, tsubst_flags_t complain) |
229970c4 JM |
1387 | { |
1388 | tree t; | |
3db45ab5 | 1389 | |
229970c4 JM |
1390 | if (e == error_mark_node) |
1391 | return error_mark_node; | |
1392 | ||
1393 | if (processing_template_decl) | |
1394 | { | |
1395 | e = build_min (ALIGNOF_EXPR, size_type_node, e); | |
1396 | TREE_SIDE_EFFECTS (e) = 0; | |
1397 | TREE_READONLY (e) = 1; | |
1398 | ||
1399 | return e; | |
1400 | } | |
1401 | ||
1402 | if (TREE_CODE (e) == VAR_DECL) | |
1403 | t = size_int (DECL_ALIGN_UNIT (e)); | |
1404 | else if (TREE_CODE (e) == COMPONENT_REF | |
1405 | && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL | |
1406 | && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1))) | |
1407 | { | |
5ade1ed2 DG |
1408 | if (complain & tf_error) |
1409 | error ("invalid application of %<__alignof%> to a bit-field"); | |
1410 | else | |
1411 | return error_mark_node; | |
229970c4 JM |
1412 | t = size_one_node; |
1413 | } | |
1414 | else if (TREE_CODE (e) == COMPONENT_REF | |
1415 | && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL) | |
1416 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1))); | |
1417 | else if (is_overloaded_fn (e)) | |
1418 | { | |
5ade1ed2 | 1419 | if (complain & tf_error) |
cbe5f3b3 | 1420 | permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of " |
37ec60ed | 1421 | "function type"); |
5ade1ed2 DG |
1422 | else |
1423 | return error_mark_node; | |
837edd5f GK |
1424 | if (TREE_CODE (e) == FUNCTION_DECL) |
1425 | t = size_int (DECL_ALIGN_UNIT (e)); | |
1426 | else | |
1427 | t = size_one_node; | |
229970c4 JM |
1428 | } |
1429 | else if (type_unknown_p (e)) | |
1430 | { | |
5ade1ed2 DG |
1431 | if (complain & tf_error) |
1432 | cxx_incomplete_type_error (e, TREE_TYPE (e)); | |
1433 | else | |
1434 | return error_mark_node; | |
229970c4 JM |
1435 | t = size_one_node; |
1436 | } | |
1437 | else | |
5ade1ed2 DG |
1438 | return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, |
1439 | complain & tf_error); | |
229970c4 JM |
1440 | |
1441 | return fold_convert (size_type_node, t); | |
1442 | } | |
1443 | ||
69d1a403 JM |
1444 | /* Process a sizeof or alignof expression E with code OP where the operand |
1445 | is an expression. */ | |
229970c4 JM |
1446 | |
1447 | tree | |
5ade1ed2 | 1448 | cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain) |
229970c4 JM |
1449 | { |
1450 | if (op == SIZEOF_EXPR) | |
5ade1ed2 | 1451 | return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none); |
229970c4 | 1452 | else |
5ade1ed2 | 1453 | return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none); |
229970c4 | 1454 | } |
8d08fdba | 1455 | \f |
c8b2e872 MM |
1456 | /* EXPR is being used in a context that is not a function call. |
1457 | Enforce: | |
1458 | ||
c8094d83 | 1459 | [expr.ref] |
c8b2e872 MM |
1460 | |
1461 | The expression can be used only as the left-hand operand of a | |
c8094d83 | 1462 | member function call. |
c8b2e872 MM |
1463 | |
1464 | [expr.mptr.operator] | |
1465 | ||
1466 | If the result of .* or ->* is a function, then that result can be | |
c8094d83 | 1467 | used only as the operand for the function call operator (). |
c8b2e872 MM |
1468 | |
1469 | by issuing an error message if appropriate. Returns true iff EXPR | |
1470 | violates these rules. */ | |
1471 | ||
1472 | bool | |
5ade1ed2 | 1473 | invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain) |
c8b2e872 MM |
1474 | { |
1475 | if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE) | |
1476 | { | |
5ade1ed2 DG |
1477 | if (complain & tf_error) |
1478 | error ("invalid use of non-static member function"); | |
c8b2e872 MM |
1479 | return true; |
1480 | } | |
1481 | return false; | |
1482 | } | |
1483 | ||
38a4afee MM |
1484 | /* If EXP is a reference to a bitfield, and the type of EXP does not |
1485 | match the declared type of the bitfield, return the declared type | |
1486 | of the bitfield. Otherwise, return NULL_TREE. */ | |
1487 | ||
1488 | tree | |
58f9752a | 1489 | is_bitfield_expr_with_lowered_type (const_tree exp) |
38a4afee | 1490 | { |
f9aa54d3 | 1491 | switch (TREE_CODE (exp)) |
38a4afee | 1492 | { |
f9aa54d3 | 1493 | case COND_EXPR: |
42924ed7 JJ |
1494 | if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1) |
1495 | ? TREE_OPERAND (exp, 1) | |
1496 | : TREE_OPERAND (exp, 0))) | |
38a4afee MM |
1497 | return NULL_TREE; |
1498 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2)); | |
f9aa54d3 MM |
1499 | |
1500 | case COMPOUND_EXPR: | |
1501 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)); | |
1502 | ||
1503 | case MODIFY_EXPR: | |
1504 | case SAVE_EXPR: | |
1505 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); | |
1506 | ||
1507 | case COMPONENT_REF: | |
1508 | { | |
1509 | tree field; | |
1510 | ||
1511 | field = TREE_OPERAND (exp, 1); | |
e76d7cc7 | 1512 | if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field)) |
f9aa54d3 MM |
1513 | return NULL_TREE; |
1514 | if (same_type_ignoring_top_level_qualifiers_p | |
1515 | (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field))) | |
1516 | return NULL_TREE; | |
1517 | return DECL_BIT_FIELD_TYPE (field); | |
1518 | } | |
1519 | ||
5f9e56b3 | 1520 | CASE_CONVERT: |
be2b5483 RG |
1521 | if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0))) |
1522 | == TYPE_MAIN_VARIANT (TREE_TYPE (exp))) | |
1523 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); | |
1524 | /* Fallthrough. */ | |
1525 | ||
f9aa54d3 MM |
1526 | default: |
1527 | return NULL_TREE; | |
38a4afee | 1528 | } |
f9aa54d3 MM |
1529 | } |
1530 | ||
1531 | /* Like is_bitfield_with_lowered_type, except that if EXP is not a | |
1532 | bitfield with a lowered type, the type of EXP is returned, rather | |
1533 | than NULL_TREE. */ | |
1534 | ||
1535 | tree | |
58f9752a | 1536 | unlowered_expr_type (const_tree exp) |
f9aa54d3 MM |
1537 | { |
1538 | tree type; | |
1539 | ||
1540 | type = is_bitfield_expr_with_lowered_type (exp); | |
1541 | if (!type) | |
1542 | type = TREE_TYPE (exp); | |
1543 | ||
1544 | return type; | |
38a4afee MM |
1545 | } |
1546 | ||
0a72704b MM |
1547 | /* Perform the conversions in [expr] that apply when an lvalue appears |
1548 | in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and | |
41990f96 MM |
1549 | function-to-pointer conversions. In addition, manifest constants |
1550 | are replaced by their values, and bitfield references are converted | |
1551 | to their declared types. | |
8d08fdba | 1552 | |
41990f96 MM |
1553 | Although the returned value is being used as an rvalue, this |
1554 | function does not wrap the returned expression in a | |
1555 | NON_LVALUE_EXPR; the caller is expected to be mindful of the fact | |
1556 | that the return value is no longer an lvalue. */ | |
8d08fdba MS |
1557 | |
1558 | tree | |
acd8e2d0 | 1559 | decay_conversion (tree exp) |
8d08fdba | 1560 | { |
926ce8bd KH |
1561 | tree type; |
1562 | enum tree_code code; | |
8d08fdba | 1563 | |
a359be75 | 1564 | type = TREE_TYPE (exp); |
07c88314 MM |
1565 | if (type == error_mark_node) |
1566 | return error_mark_node; | |
1567 | ||
40260429 NS |
1568 | if (type_unknown_p (exp)) |
1569 | { | |
7a228918 | 1570 | cxx_incomplete_type_error (exp, TREE_TYPE (exp)); |
40260429 NS |
1571 | return error_mark_node; |
1572 | } | |
8d08fdba | 1573 | |
393e756d | 1574 | exp = decl_constant_value (exp); |
41990f96 MM |
1575 | if (error_operand_p (exp)) |
1576 | return error_mark_node; | |
c8094d83 | 1577 | |
8d08fdba MS |
1578 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
1579 | Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */ | |
38a4afee | 1580 | code = TREE_CODE (type); |
8d08fdba MS |
1581 | if (code == VOID_TYPE) |
1582 | { | |
8251199e | 1583 | error ("void value not ignored as it ought to be"); |
8d08fdba MS |
1584 | return error_mark_node; |
1585 | } | |
5ade1ed2 | 1586 | if (invalid_nonstatic_memfn_p (exp, tf_warning_or_error)) |
c8b2e872 | 1587 | return error_mark_node; |
e6e174e5 | 1588 | if (code == FUNCTION_TYPE || is_overloaded_fn (exp)) |
5ade1ed2 | 1589 | return cp_build_unary_op (ADDR_EXPR, exp, 0, tf_warning_or_error); |
8d08fdba MS |
1590 | if (code == ARRAY_TYPE) |
1591 | { | |
926ce8bd | 1592 | tree adr; |
8d08fdba | 1593 | tree ptrtype; |
8d08fdba MS |
1594 | |
1595 | if (TREE_CODE (exp) == INDIRECT_REF) | |
c8094d83 | 1596 | return build_nop (build_pointer_type (TREE_TYPE (type)), |
7b6d72fc | 1597 | TREE_OPERAND (exp, 0)); |
8d08fdba MS |
1598 | |
1599 | if (TREE_CODE (exp) == COMPOUND_EXPR) | |
1600 | { | |
a9aedbc2 | 1601 | tree op1 = decay_conversion (TREE_OPERAND (exp, 1)); |
f293ce4b RS |
1602 | return build2 (COMPOUND_EXPR, TREE_TYPE (op1), |
1603 | TREE_OPERAND (exp, 0), op1); | |
8d08fdba MS |
1604 | } |
1605 | ||
1606 | if (!lvalue_p (exp) | |
1607 | && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) | |
1608 | { | |
8251199e | 1609 | error ("invalid use of non-lvalue array"); |
8d08fdba MS |
1610 | return error_mark_node; |
1611 | } | |
1612 | ||
01240200 | 1613 | ptrtype = build_pointer_type (TREE_TYPE (type)); |
8d08fdba MS |
1614 | |
1615 | if (TREE_CODE (exp) == VAR_DECL) | |
1616 | { | |
dffd7eb6 | 1617 | if (!cxx_mark_addressable (exp)) |
8d08fdba | 1618 | return error_mark_node; |
0e8c9b28 | 1619 | adr = build_nop (ptrtype, build_address (exp)); |
8d08fdba MS |
1620 | return adr; |
1621 | } | |
1622 | /* This way is better for a COMPONENT_REF since it can | |
1623 | simplify the offset for a component. */ | |
5ade1ed2 | 1624 | adr = cp_build_unary_op (ADDR_EXPR, exp, 1, tf_warning_or_error); |
37c46b43 | 1625 | return cp_convert (ptrtype, adr); |
8d08fdba | 1626 | } |
3db45ab5 | 1627 | |
41990f96 MM |
1628 | /* If a bitfield is used in a context where integral promotion |
1629 | applies, then the caller is expected to have used | |
1630 | default_conversion. That function promotes bitfields correctly | |
1631 | before calling this function. At this point, if we have a | |
1632 | bitfield referenced, we may assume that is not subject to | |
1633 | promotion, and that, therefore, the type of the resulting rvalue | |
1634 | is the declared type of the bitfield. */ | |
1635 | exp = convert_bitfield_to_declared_type (exp); | |
a9aedbc2 | 1636 | |
41990f96 MM |
1637 | /* We do not call rvalue() here because we do not want to wrap EXP |
1638 | in a NON_LVALUE_EXPR. */ | |
1639 | ||
1640 | /* [basic.lval] | |
1641 | ||
1642 | Non-class rvalues always have cv-unqualified types. */ | |
1643 | type = TREE_TYPE (exp); | |
1644 | if (!CLASS_TYPE_P (type) && cp_type_quals (type)) | |
1645 | exp = build_nop (TYPE_MAIN_VARIANT (type), exp); | |
ba9c33e8 | 1646 | |
a9aedbc2 MS |
1647 | return exp; |
1648 | } | |
1649 | ||
39a13be5 | 1650 | /* Perform preparatory conversions, as part of the "usual arithmetic |
41990f96 MM |
1651 | conversions". In particular, as per [expr]: |
1652 | ||
1653 | Whenever an lvalue expression appears as an operand of an | |
1654 | operator that expects the rvalue for that operand, the | |
1655 | lvalue-to-rvalue, array-to-pointer, or function-to-pointer | |
1656 | standard conversions are applied to convert the expression to an | |
1657 | rvalue. | |
1658 | ||
1659 | In addition, we perform integral promotions here, as those are | |
1660 | applied to both operands to a binary operator before determining | |
1661 | what additional conversions should apply. */ | |
1662 | ||
d593cb3d | 1663 | tree |
acd8e2d0 | 1664 | default_conversion (tree exp) |
a9aedbc2 | 1665 | { |
41990f96 MM |
1666 | /* Perform the integral promotions first so that bitfield |
1667 | expressions (which may promote to "int", even if the bitfield is | |
3db45ab5 | 1668 | declared "unsigned") are promoted correctly. */ |
adf2edec | 1669 | if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp))) |
0a72704b | 1670 | exp = perform_integral_promotions (exp); |
41990f96 MM |
1671 | /* Perform the other conversions. */ |
1672 | exp = decay_conversion (exp); | |
a9aedbc2 | 1673 | |
8d08fdba MS |
1674 | return exp; |
1675 | } | |
6b5fbb55 | 1676 | |
0a72704b MM |
1677 | /* EXPR is an expression with an integral or enumeration type. |
1678 | Perform the integral promotions in [conv.prom], and return the | |
1679 | converted value. */ | |
1680 | ||
1681 | tree | |
1682 | perform_integral_promotions (tree expr) | |
1683 | { | |
1684 | tree type; | |
1685 | tree promoted_type; | |
1686 | ||
efe1ad46 MM |
1687 | /* [conv.prom] |
1688 | ||
1689 | If the bitfield has an enumerated type, it is treated as any | |
1690 | other value of that type for promotion purposes. */ | |
1691 | type = is_bitfield_expr_with_lowered_type (expr); | |
1692 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) | |
1693 | type = TREE_TYPE (expr); | |
50bc768d | 1694 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); |
0a72704b MM |
1695 | promoted_type = type_promotes_to (type); |
1696 | if (type != promoted_type) | |
1697 | expr = cp_convert (promoted_type, expr); | |
1698 | return expr; | |
1699 | } | |
1700 | ||
d9cf7c82 JM |
1701 | /* Returns nonzero iff exp is a STRING_CST or the result of applying |
1702 | decay_conversion to one. */ | |
1703 | ||
1704 | int | |
58f9752a | 1705 | string_conv_p (const_tree totype, const_tree exp, int warn) |
d9cf7c82 JM |
1706 | { |
1707 | tree t; | |
1708 | ||
cfb10bd3 | 1709 | if (TREE_CODE (totype) != POINTER_TYPE) |
d9cf7c82 JM |
1710 | return 0; |
1711 | ||
1712 | t = TREE_TYPE (totype); | |
3bfdc719 | 1713 | if (!same_type_p (t, char_type_node) |
b6baa67d KVH |
1714 | && !same_type_p (t, char16_type_node) |
1715 | && !same_type_p (t, char32_type_node) | |
3bfdc719 | 1716 | && !same_type_p (t, wchar_type_node)) |
d9cf7c82 JM |
1717 | return 0; |
1718 | ||
848b92e1 | 1719 | if (TREE_CODE (exp) == STRING_CST) |
d9cf7c82 | 1720 | { |
b6baa67d | 1721 | /* Make sure that we don't try to convert between char and wide chars. */ |
6e176bd6 | 1722 | if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t)) |
848b92e1 JM |
1723 | return 0; |
1724 | } | |
1725 | else | |
1726 | { | |
1727 | /* Is this a string constant which has decayed to 'const char *'? */ | |
91063b51 | 1728 | t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST)); |
3bfdc719 | 1729 | if (!same_type_p (TREE_TYPE (exp), t)) |
d9cf7c82 JM |
1730 | return 0; |
1731 | STRIP_NOPS (exp); | |
1732 | if (TREE_CODE (exp) != ADDR_EXPR | |
1733 | || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST) | |
1734 | return 0; | |
1735 | } | |
1736 | ||
1737 | /* This warning is not very useful, as it complains about printf. */ | |
b323323f | 1738 | if (warn) |
4b641750 MM |
1739 | warning (OPT_Wwrite_strings, |
1740 | "deprecated conversion from string constant to %qT", | |
1741 | totype); | |
d9cf7c82 JM |
1742 | |
1743 | return 1; | |
1744 | } | |
8d08fdba | 1745 | |
e9a25f70 JL |
1746 | /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we |
1747 | can, for example, use as an lvalue. This code used to be in | |
1748 | unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c' | |
1749 | expressions, where we're dealing with aggregates. But now it's again only | |
1750 | called from unary_complex_lvalue. The case (in particular) that led to | |
1751 | this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd | |
1752 | get it there. */ | |
e92cc029 | 1753 | |
8d08fdba | 1754 | static tree |
5ade1ed2 DG |
1755 | rationalize_conditional_expr (enum tree_code code, tree t, |
1756 | tsubst_flags_t complain) | |
8d08fdba | 1757 | { |
e9a25f70 JL |
1758 | /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that |
1759 | the first operand is always the one to be used if both operands | |
1760 | are equal, so we know what conditional expression this used to be. */ | |
1761 | if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR) | |
1762 | { | |
2a67bec2 ILT |
1763 | tree op0 = TREE_OPERAND (t, 0); |
1764 | tree op1 = TREE_OPERAND (t, 1); | |
1765 | ||
d211a298 | 1766 | /* The following code is incorrect if either operand side-effects. */ |
2a67bec2 ILT |
1767 | gcc_assert (!TREE_SIDE_EFFECTS (op0) |
1768 | && !TREE_SIDE_EFFECTS (op1)); | |
e9a25f70 JL |
1769 | return |
1770 | build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR | |
1771 | ? LE_EXPR : GE_EXPR), | |
2a67bec2 ILT |
1772 | op0, TREE_CODE (op0), |
1773 | op1, TREE_CODE (op1), | |
5ade1ed2 DG |
1774 | /*overloaded_p=*/NULL, |
1775 | complain), | |
1776 | cp_build_unary_op (code, op0, 0, complain), | |
1777 | cp_build_unary_op (code, op1, 0, complain), | |
1778 | complain); | |
e9a25f70 JL |
1779 | } |
1780 | ||
8d08fdba MS |
1781 | return |
1782 | build_conditional_expr (TREE_OPERAND (t, 0), | |
5ade1ed2 DG |
1783 | cp_build_unary_op (code, TREE_OPERAND (t, 1), 0, |
1784 | complain), | |
1785 | cp_build_unary_op (code, TREE_OPERAND (t, 2), 0, | |
1786 | complain), | |
1787 | complain); | |
8d08fdba MS |
1788 | } |
1789 | ||
9e9ff709 MS |
1790 | /* Given the TYPE of an anonymous union field inside T, return the |
1791 | FIELD_DECL for the field. If not found return NULL_TREE. Because | |
1792 | anonymous unions can nest, we must also search all anonymous unions | |
1793 | that are directly reachable. */ | |
e92cc029 | 1794 | |
b3dd05b1 | 1795 | tree |
acd8e2d0 | 1796 | lookup_anon_field (tree t, tree type) |
9e9ff709 MS |
1797 | { |
1798 | tree field; | |
1799 | ||
1800 | for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) | |
1801 | { | |
1802 | if (TREE_STATIC (field)) | |
1803 | continue; | |
17bbb839 | 1804 | if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) |
9e9ff709 MS |
1805 | continue; |
1806 | ||
1807 | /* If we find it directly, return the field. */ | |
1808 | if (DECL_NAME (field) == NULL_TREE | |
71631a1f | 1809 | && type == TYPE_MAIN_VARIANT (TREE_TYPE (field))) |
9e9ff709 MS |
1810 | { |
1811 | return field; | |
1812 | } | |
1813 | ||
1814 | /* Otherwise, it could be nested, search harder. */ | |
1815 | if (DECL_NAME (field) == NULL_TREE | |
6bdb8141 | 1816 | && ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
9e9ff709 MS |
1817 | { |
1818 | tree subfield = lookup_anon_field (TREE_TYPE (field), type); | |
1819 | if (subfield) | |
1820 | return subfield; | |
1821 | } | |
1822 | } | |
1823 | return NULL_TREE; | |
1824 | } | |
1825 | ||
50ad9642 MM |
1826 | /* Build an expression representing OBJECT.MEMBER. OBJECT is an |
1827 | expression; MEMBER is a DECL or baselink. If ACCESS_PATH is | |
1828 | non-NULL, it indicates the path to the base used to name MEMBER. | |
1829 | If PRESERVE_REFERENCE is true, the expression returned will have | |
1830 | REFERENCE_TYPE if the MEMBER does. Otherwise, the expression | |
c8094d83 | 1831 | returned will have the type referred to by the reference. |
50ad9642 MM |
1832 | |
1833 | This function does not perform access control; that is either done | |
1834 | earlier by the parser when the name of MEMBER is resolved to MEMBER | |
1835 | itself, or later when overload resolution selects one of the | |
1836 | functions indicated by MEMBER. */ | |
e92cc029 | 1837 | |
8d08fdba | 1838 | tree |
c8094d83 | 1839 | build_class_member_access_expr (tree object, tree member, |
5ade1ed2 DG |
1840 | tree access_path, bool preserve_reference, |
1841 | tsubst_flags_t complain) | |
8d08fdba | 1842 | { |
50ad9642 MM |
1843 | tree object_type; |
1844 | tree member_scope; | |
1845 | tree result = NULL_TREE; | |
8d08fdba | 1846 | |
2e5d2970 | 1847 | if (error_operand_p (object) || error_operand_p (member)) |
01240200 | 1848 | return error_mark_node; |
5566b478 | 1849 | |
50bc768d | 1850 | gcc_assert (DECL_P (member) || BASELINK_P (member)); |
8d08fdba | 1851 | |
50ad9642 | 1852 | /* [expr.ref] |
5566b478 | 1853 | |
50ad9642 MM |
1854 | The type of the first expression shall be "class object" (of a |
1855 | complete type). */ | |
1856 | object_type = TREE_TYPE (object); | |
c8094d83 | 1857 | if (!currently_open_class (object_type) |
21b34b9c | 1858 | && !complete_type_or_else (object_type, object)) |
50ad9642 MM |
1859 | return error_mark_node; |
1860 | if (!CLASS_TYPE_P (object_type)) | |
fc378698 | 1861 | { |
5ade1ed2 DG |
1862 | if (complain & tf_error) |
1863 | error ("request for member %qD in %qE, which is of non-class type %qT", | |
1864 | member, object, object_type); | |
50ad9642 | 1865 | return error_mark_node; |
fc378698 | 1866 | } |
8d08fdba | 1867 | |
50ad9642 MM |
1868 | /* The standard does not seem to actually say that MEMBER must be a |
1869 | member of OBJECT_TYPE. However, that is clearly what is | |
1870 | intended. */ | |
1871 | if (DECL_P (member)) | |
8d08fdba | 1872 | { |
50ad9642 MM |
1873 | member_scope = DECL_CLASS_CONTEXT (member); |
1874 | mark_used (member); | |
1875 | if (TREE_DEPRECATED (member)) | |
1876 | warn_deprecated_use (member); | |
1877 | } | |
1878 | else | |
0197270c | 1879 | member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member)); |
50ad9642 MM |
1880 | /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will |
1881 | presently be the anonymous union. Go outwards until we find a | |
1882 | type related to OBJECT_TYPE. */ | |
1883 | while (ANON_AGGR_TYPE_P (member_scope) | |
1884 | && !same_type_ignoring_top_level_qualifiers_p (member_scope, | |
1885 | object_type)) | |
1886 | member_scope = TYPE_CONTEXT (member_scope); | |
1887 | if (!member_scope || !DERIVED_FROM_P (member_scope, object_type)) | |
1888 | { | |
5ade1ed2 DG |
1889 | if (complain & tf_error) |
1890 | { | |
1891 | if (TREE_CODE (member) == FIELD_DECL) | |
1892 | error ("invalid use of nonstatic data member %qE", member); | |
1893 | else | |
1894 | error ("%qD is not a member of %qT", member, object_type); | |
1895 | } | |
50ad9642 | 1896 | return error_mark_node; |
8d08fdba | 1897 | } |
8d08fdba | 1898 | |
f576dfc4 JM |
1899 | /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into |
1900 | `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue | |
3b426391 | 1901 | in the front end; only _DECLs and _REFs are lvalues in the back end. */ |
f576dfc4 JM |
1902 | { |
1903 | tree temp = unary_complex_lvalue (ADDR_EXPR, object); | |
1904 | if (temp) | |
5ade1ed2 | 1905 | object = cp_build_indirect_ref (temp, NULL, complain); |
f576dfc4 | 1906 | } |
2050a1bb | 1907 | |
50ad9642 MM |
1908 | /* In [expr.ref], there is an explicit list of the valid choices for |
1909 | MEMBER. We check for each of those cases here. */ | |
1910 | if (TREE_CODE (member) == VAR_DECL) | |
8d08fdba | 1911 | { |
50ad9642 MM |
1912 | /* A static data member. */ |
1913 | result = member; | |
1914 | /* If OBJECT has side-effects, they are supposed to occur. */ | |
1915 | if (TREE_SIDE_EFFECTS (object)) | |
f293ce4b | 1916 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result); |
8d08fdba | 1917 | } |
50ad9642 MM |
1918 | else if (TREE_CODE (member) == FIELD_DECL) |
1919 | { | |
1920 | /* A non-static data member. */ | |
1921 | bool null_object_p; | |
1922 | int type_quals; | |
1923 | tree member_type; | |
8d08fdba | 1924 | |
50ad9642 MM |
1925 | null_object_p = (TREE_CODE (object) == INDIRECT_REF |
1926 | && integer_zerop (TREE_OPERAND (object, 0))); | |
8d08fdba | 1927 | |
50ad9642 | 1928 | /* Convert OBJECT to the type of MEMBER. */ |
bdaa131b JM |
1929 | if (!same_type_p (TYPE_MAIN_VARIANT (object_type), |
1930 | TYPE_MAIN_VARIANT (member_scope))) | |
8d08fdba | 1931 | { |
50ad9642 MM |
1932 | tree binfo; |
1933 | base_kind kind; | |
1934 | ||
1935 | binfo = lookup_base (access_path ? access_path : object_type, | |
18e4be85 | 1936 | member_scope, ba_unique, &kind); |
50ad9642 MM |
1937 | if (binfo == error_mark_node) |
1938 | return error_mark_node; | |
1939 | ||
bdaa131b | 1940 | /* It is invalid to try to get to a virtual base of a |
50ad9642 MM |
1941 | NULL object. The most common cause is invalid use of |
1942 | offsetof macro. */ | |
1943 | if (null_object_p && kind == bk_via_virtual) | |
1944 | { | |
5ade1ed2 DG |
1945 | if (complain & tf_error) |
1946 | { | |
1947 | error ("invalid access to non-static data member %qD of " | |
1948 | "NULL object", | |
1949 | member); | |
1950 | error ("(perhaps the %<offsetof%> macro was used incorrectly)"); | |
1951 | } | |
50ad9642 MM |
1952 | return error_mark_node; |
1953 | } | |
1954 | ||
1955 | /* Convert to the base. */ | |
c8094d83 | 1956 | object = build_base_path (PLUS_EXPR, object, binfo, |
50ad9642 MM |
1957 | /*nonnull=*/1); |
1958 | /* If we found the base successfully then we should be able | |
1959 | to convert to it successfully. */ | |
50bc768d | 1960 | gcc_assert (object != error_mark_node); |
8d08fdba | 1961 | } |
3b5b4904 JM |
1962 | |
1963 | /* Complain about other invalid uses of offsetof, even though they will | |
1964 | give the right answer. Note that we complain whether or not they | |
1965 | actually used the offsetof macro, since there's no way to know at this | |
1966 | point. So we just give a warning, instead of a pedwarn. */ | |
981c353e RH |
1967 | /* Do not produce this warning for base class field references, because |
1968 | we know for a fact that didn't come from offsetof. This does occur | |
1969 | in various testsuite cases where a null object is passed where a | |
1970 | vtable access is required. */ | |
a01fff59 | 1971 | if (null_object_p && warn_invalid_offsetof |
999a1ad4 | 1972 | && CLASSTYPE_NON_POD_P (object_type) |
981c353e | 1973 | && !DECL_FIELD_IS_BASE (member) |
5ade1ed2 DG |
1974 | && !skip_evaluation |
1975 | && (complain & tf_warning)) | |
8d08fdba | 1976 | { |
683d6ff9 MLI |
1977 | warning (OPT_Winvalid_offsetof, |
1978 | "invalid access to non-static data member %qD " | |
1979 | " of NULL object", member); | |
1980 | warning (OPT_Winvalid_offsetof, | |
1981 | "(perhaps the %<offsetof%> macro was used incorrectly)"); | |
8d08fdba | 1982 | } |
8d08fdba | 1983 | |
50ad9642 MM |
1984 | /* If MEMBER is from an anonymous aggregate, we have converted |
1985 | OBJECT so that it refers to the class containing the | |
1986 | anonymous union. Generate a reference to the anonymous union | |
1987 | itself, and recur to find MEMBER. */ | |
0ca7178c | 1988 | if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member)) |
a723baf1 MM |
1989 | /* When this code is called from build_field_call, the |
1990 | object already has the type of the anonymous union. | |
1991 | That is because the COMPONENT_REF was already | |
1992 | constructed, and was then disassembled before calling | |
1993 | build_field_call. After the function-call code is | |
1994 | cleaned up, this waste can be eliminated. */ | |
c8094d83 | 1995 | && (!same_type_ignoring_top_level_qualifiers_p |
a723baf1 | 1996 | (TREE_TYPE (object), DECL_CONTEXT (member)))) |
50ad9642 MM |
1997 | { |
1998 | tree anonymous_union; | |
1999 | ||
2000 | anonymous_union = lookup_anon_field (TREE_TYPE (object), | |
2001 | DECL_CONTEXT (member)); | |
2002 | object = build_class_member_access_expr (object, | |
2003 | anonymous_union, | |
2004 | /*access_path=*/NULL_TREE, | |
5ade1ed2 DG |
2005 | preserve_reference, |
2006 | complain); | |
50ad9642 MM |
2007 | } |
2008 | ||
2009 | /* Compute the type of the field, as described in [expr.ref]. */ | |
2010 | type_quals = TYPE_UNQUALIFIED; | |
2011 | member_type = TREE_TYPE (member); | |
2012 | if (TREE_CODE (member_type) != REFERENCE_TYPE) | |
2013 | { | |
c8094d83 | 2014 | type_quals = (cp_type_quals (member_type) |
50ad9642 | 2015 | | cp_type_quals (object_type)); |
c8094d83 | 2016 | |
50ad9642 MM |
2017 | /* A field is const (volatile) if the enclosing object, or the |
2018 | field itself, is const (volatile). But, a mutable field is | |
2019 | not const, even within a const object. */ | |
2020 | if (DECL_MUTABLE_P (member)) | |
2021 | type_quals &= ~TYPE_QUAL_CONST; | |
2022 | member_type = cp_build_qualified_type (member_type, type_quals); | |
2023 | } | |
2024 | ||
455f19cb MM |
2025 | result = build3 (COMPONENT_REF, member_type, object, member, |
2026 | NULL_TREE); | |
2027 | result = fold_if_not_in_template (result); | |
50ad9642 MM |
2028 | |
2029 | /* Mark the expression const or volatile, as appropriate. Even | |
2030 | though we've dealt with the type above, we still have to mark the | |
2031 | expression itself. */ | |
2032 | if (type_quals & TYPE_QUAL_CONST) | |
2033 | TREE_READONLY (result) = 1; | |
12a669d1 | 2034 | if (type_quals & TYPE_QUAL_VOLATILE) |
50ad9642 MM |
2035 | TREE_THIS_VOLATILE (result) = 1; |
2036 | } | |
2037 | else if (BASELINK_P (member)) | |
9e9ff709 | 2038 | { |
50ad9642 MM |
2039 | /* The member is a (possibly overloaded) member function. */ |
2040 | tree functions; | |
a723baf1 | 2041 | tree type; |
50ad9642 MM |
2042 | |
2043 | /* If the MEMBER is exactly one static member function, then we | |
2044 | know the type of the expression. Otherwise, we must wait | |
2045 | until overload resolution has been performed. */ | |
2046 | functions = BASELINK_FUNCTIONS (member); | |
2047 | if (TREE_CODE (functions) == FUNCTION_DECL | |
2048 | && DECL_STATIC_FUNCTION_P (functions)) | |
a723baf1 | 2049 | type = TREE_TYPE (functions); |
50ad9642 | 2050 | else |
a723baf1 MM |
2051 | type = unknown_type_node; |
2052 | /* Note that we do not convert OBJECT to the BASELINK_BINFO | |
2053 | base. That will happen when the function is called. */ | |
f293ce4b | 2054 | result = build3 (COMPONENT_REF, type, object, member, NULL_TREE); |
9e9ff709 | 2055 | } |
50ad9642 | 2056 | else if (TREE_CODE (member) == CONST_DECL) |
b928a651 | 2057 | { |
50ad9642 MM |
2058 | /* The member is an enumerator. */ |
2059 | result = member; | |
2060 | /* If OBJECT has side-effects, they are supposed to occur. */ | |
2061 | if (TREE_SIDE_EFFECTS (object)) | |
f293ce4b RS |
2062 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
2063 | object, result); | |
b928a651 | 2064 | } |
8d08fdba MS |
2065 | else |
2066 | { | |
5ade1ed2 DG |
2067 | if (complain & tf_error) |
2068 | error ("invalid use of %qD", member); | |
50ad9642 MM |
2069 | return error_mark_node; |
2070 | } | |
8d08fdba | 2071 | |
50ad9642 MM |
2072 | if (!preserve_reference) |
2073 | /* [expr.ref] | |
c8094d83 | 2074 | |
50ad9642 MM |
2075 | If E2 is declared to have type "reference to T", then ... the |
2076 | type of E1.E2 is T. */ | |
2077 | result = convert_from_reference (result); | |
e6f62286 | 2078 | |
50ad9642 MM |
2079 | return result; |
2080 | } | |
8d08fdba | 2081 | |
a723baf1 MM |
2082 | /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if |
2083 | SCOPE is NULL, by OBJECT.~DTOR_NAME. */ | |
2084 | ||
2085 | static tree | |
2086 | lookup_destructor (tree object, tree scope, tree dtor_name) | |
2087 | { | |
2088 | tree object_type = TREE_TYPE (object); | |
2089 | tree dtor_type = TREE_OPERAND (dtor_name, 0); | |
4546865e | 2090 | tree expr; |
a723baf1 | 2091 | |
1b3d28a8 | 2092 | if (scope && !check_dtor_name (scope, dtor_type)) |
a723baf1 | 2093 | { |
a82e1a7d | 2094 | error ("qualified type %qT does not match destructor name ~%qT", |
a723baf1 MM |
2095 | scope, dtor_type); |
2096 | return error_mark_node; | |
2097 | } | |
4546865e | 2098 | if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type))) |
a723baf1 | 2099 | { |
a82e1a7d | 2100 | error ("the type being destroyed is %qT, but the destructor refers to %qT", |
fca00ffb | 2101 | TYPE_MAIN_VARIANT (object_type), dtor_type); |
a723baf1 MM |
2102 | return error_mark_node; |
2103 | } | |
4546865e | 2104 | expr = lookup_member (dtor_type, complete_dtor_identifier, |
86ac0575 | 2105 | /*protect=*/1, /*want_type=*/false); |
4546865e MM |
2106 | expr = (adjust_result_of_qualified_name_lookup |
2107 | (expr, dtor_type, object_type)); | |
2108 | return expr; | |
a723baf1 MM |
2109 | } |
2110 | ||
02ed62dd MM |
2111 | /* An expression of the form "A::template B" has been resolved to |
2112 | DECL. Issue a diagnostic if B is not a template or template | |
2113 | specialization. */ | |
2114 | ||
2115 | void | |
2116 | check_template_keyword (tree decl) | |
2117 | { | |
2118 | /* The standard says: | |
2119 | ||
2120 | [temp.names] | |
2121 | ||
2122 | If a name prefixed by the keyword template is not a member | |
2123 | template, the program is ill-formed. | |
2124 | ||
2125 | DR 228 removed the restriction that the template be a member | |
3db45ab5 MS |
2126 | template. |
2127 | ||
dd36d4e1 | 2128 | DR 96, if accepted would add the further restriction that explicit |
02ed62dd MM |
2129 | template arguments must be provided if the template keyword is |
2130 | used, but, as of 2005-10-16, that DR is still in "drafting". If | |
2131 | this DR is accepted, then the semantic checks here can be | |
2132 | simplified, as the entity named must in fact be a template | |
2133 | specialization, rather than, as at present, a set of overloaded | |
2134 | functions containing at least one template function. */ | |
2135 | if (TREE_CODE (decl) != TEMPLATE_DECL | |
2136 | && TREE_CODE (decl) != TEMPLATE_ID_EXPR) | |
2137 | { | |
2138 | if (!is_overloaded_fn (decl)) | |
cbe5f3b3 | 2139 | permerror (input_location, "%qD is not a template", decl); |
02ed62dd MM |
2140 | else |
2141 | { | |
2142 | tree fns; | |
459051a0 MM |
2143 | fns = decl; |
2144 | if (BASELINK_P (fns)) | |
2145 | fns = BASELINK_FUNCTIONS (fns); | |
02ed62dd MM |
2146 | while (fns) |
2147 | { | |
2148 | tree fn = OVL_CURRENT (fns); | |
2149 | if (TREE_CODE (fn) == TEMPLATE_DECL | |
2150 | || TREE_CODE (fn) == TEMPLATE_ID_EXPR) | |
2151 | break; | |
2152 | if (TREE_CODE (fn) == FUNCTION_DECL | |
2153 | && DECL_USE_TEMPLATE (fn) | |
2154 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))) | |
2155 | break; | |
2156 | fns = OVL_NEXT (fns); | |
2157 | } | |
2158 | if (!fns) | |
cbe5f3b3 | 2159 | permerror (input_location, "%qD is not a template", decl); |
02ed62dd MM |
2160 | } |
2161 | } | |
2162 | } | |
2163 | ||
50ad9642 MM |
2164 | /* This function is called by the parser to process a class member |
2165 | access expression of the form OBJECT.NAME. NAME is a node used by | |
2166 | the parser to represent a name; it is not yet a DECL. It may, | |
2167 | however, be a BASELINK where the BASELINK_FUNCTIONS is a | |
2168 | TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and | |
2169 | there is no reason to do the lookup twice, so the parser keeps the | |
02ed62dd MM |
2170 | BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to |
2171 | be a template via the use of the "A::template B" syntax. */ | |
8d08fdba | 2172 | |
50ad9642 | 2173 | tree |
5ade1ed2 DG |
2174 | finish_class_member_access_expr (tree object, tree name, bool template_p, |
2175 | tsubst_flags_t complain) | |
50ad9642 | 2176 | { |
d17811fd | 2177 | tree expr; |
50ad9642 MM |
2178 | tree object_type; |
2179 | tree member; | |
2180 | tree access_path = NULL_TREE; | |
d17811fd MM |
2181 | tree orig_object = object; |
2182 | tree orig_name = name; | |
3c8c2a0a | 2183 | |
50ad9642 MM |
2184 | if (object == error_mark_node || name == error_mark_node) |
2185 | return error_mark_node; | |
3c8c2a0a | 2186 | |
e58a9aa1 ZL |
2187 | /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */ |
2188 | if (!objc_is_public (object, name)) | |
2189 | return error_mark_node; | |
2190 | ||
d17811fd MM |
2191 | object_type = TREE_TYPE (object); |
2192 | ||
50ad9642 | 2193 | if (processing_template_decl) |
d17811fd MM |
2194 | { |
2195 | if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */ | |
2196 | dependent_type_p (object_type) | |
1ffe6573 MM |
2197 | /* If NAME is just an IDENTIFIER_NODE, then the expression |
2198 | is dependent. */ | |
2199 | || TREE_CODE (object) == IDENTIFIER_NODE | |
d17811fd MM |
2200 | /* If NAME is "f<args>", where either 'f' or 'args' is |
2201 | dependent, then the expression is dependent. */ | |
2202 | || (TREE_CODE (name) == TEMPLATE_ID_EXPR | |
2203 | && dependent_template_id_p (TREE_OPERAND (name, 0), | |
2204 | TREE_OPERAND (name, 1))) | |
2205 | /* If NAME is "T::X" where "T" is dependent, then the | |
2206 | expression is dependent. */ | |
2207 | || (TREE_CODE (name) == SCOPE_REF | |
2208 | && TYPE_P (TREE_OPERAND (name, 0)) | |
2209 | && dependent_type_p (TREE_OPERAND (name, 0)))) | |
44de5aeb | 2210 | return build_min_nt (COMPONENT_REF, object, name, NULL_TREE); |
d17811fd MM |
2211 | object = build_non_dependent_expr (object); |
2212 | } | |
c8094d83 | 2213 | |
50ad9642 | 2214 | /* [expr.ref] |
e23bd218 | 2215 | |
50ad9642 MM |
2216 | The type of the first expression shall be "class object" (of a |
2217 | complete type). */ | |
c8094d83 | 2218 | if (!currently_open_class (object_type) |
21b34b9c | 2219 | && !complete_type_or_else (object_type, object)) |
50ad9642 MM |
2220 | return error_mark_node; |
2221 | if (!CLASS_TYPE_P (object_type)) | |
2222 | { | |
5ade1ed2 DG |
2223 | if (complain & tf_error) |
2224 | error ("request for member %qD in %qE, which is of non-class type %qT", | |
2225 | name, object, object_type); | |
50ad9642 MM |
2226 | return error_mark_node; |
2227 | } | |
f2d9afec | 2228 | |
50ad9642 | 2229 | if (BASELINK_P (name)) |
fdeff563 NS |
2230 | /* A member function that has already been looked up. */ |
2231 | member = name; | |
50ad9642 MM |
2232 | else |
2233 | { | |
2234 | bool is_template_id = false; | |
2235 | tree template_args = NULL_TREE; | |
d17811fd | 2236 | tree scope; |
2ee887f2 | 2237 | |
50ad9642 | 2238 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
2ee887f2 | 2239 | { |
50ad9642 MM |
2240 | is_template_id = true; |
2241 | template_args = TREE_OPERAND (name, 1); | |
2242 | name = TREE_OPERAND (name, 0); | |
4864cc4a KL |
2243 | |
2244 | if (TREE_CODE (name) == OVERLOAD) | |
2245 | name = DECL_NAME (get_first_fn (name)); | |
2246 | else if (DECL_P (name)) | |
2247 | name = DECL_NAME (name); | |
50ad9642 | 2248 | } |
f2d9afec | 2249 | |
50ad9642 MM |
2250 | if (TREE_CODE (name) == SCOPE_REF) |
2251 | { | |
e13d2b4e MM |
2252 | /* A qualified name. The qualifying class or namespace `S' |
2253 | has already been looked up; it is either a TYPE or a | |
2254 | NAMESPACE_DECL. */ | |
50ad9642 MM |
2255 | scope = TREE_OPERAND (name, 0); |
2256 | name = TREE_OPERAND (name, 1); | |
50ad9642 MM |
2257 | |
2258 | /* If SCOPE is a namespace, then the qualified name does not | |
2259 | name a member of OBJECT_TYPE. */ | |
2260 | if (TREE_CODE (scope) == NAMESPACE_DECL) | |
2ee887f2 | 2261 | { |
5ade1ed2 DG |
2262 | if (complain & tf_error) |
2263 | error ("%<%D::%D%> is not a member of %qT", | |
2264 | scope, name, object_type); | |
2ee887f2 MS |
2265 | return error_mark_node; |
2266 | } | |
50ad9642 | 2267 | |
e13d2b4e MM |
2268 | gcc_assert (CLASS_TYPE_P (scope)); |
2269 | gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE | |
2270 | || TREE_CODE (name) == BIT_NOT_EXPR); | |
2271 | ||
50ad9642 MM |
2272 | /* Find the base of OBJECT_TYPE corresponding to SCOPE. */ |
2273 | access_path = lookup_base (object_type, scope, ba_check, NULL); | |
c8a65a25 | 2274 | if (access_path == error_mark_node) |
50ad9642 | 2275 | return error_mark_node; |
c8a65a25 MM |
2276 | if (!access_path) |
2277 | { | |
5ade1ed2 DG |
2278 | if (complain & tf_error) |
2279 | error ("%qT is not a base of %qT", scope, object_type); | |
c8a65a25 MM |
2280 | return error_mark_node; |
2281 | } | |
2ee887f2 | 2282 | } |
d17811fd MM |
2283 | else |
2284 | { | |
2285 | scope = NULL_TREE; | |
2286 | access_path = object_type; | |
2287 | } | |
2288 | ||
2289 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
2290 | member = lookup_destructor (object, scope, name); | |
2291 | else | |
9e9ff709 | 2292 | { |
d17811fd | 2293 | /* Look up the member. */ |
c8094d83 | 2294 | member = lookup_member (access_path, name, /*protect=*/1, |
86ac0575 | 2295 | /*want_type=*/false); |
8a5f4379 GDR |
2296 | if (member == NULL_TREE) |
2297 | { | |
5ade1ed2 DG |
2298 | if (complain & tf_error) |
2299 | error ("%qD has no member named %qE", object_type, name); | |
8a5f4379 GDR |
2300 | return error_mark_node; |
2301 | } | |
d17811fd | 2302 | if (member == error_mark_node) |
50ad9642 MM |
2303 | return error_mark_node; |
2304 | } | |
c8094d83 | 2305 | |
50ad9642 MM |
2306 | if (is_template_id) |
2307 | { | |
be93747e | 2308 | tree templ = member; |
c8094d83 | 2309 | |
be93747e KG |
2310 | if (BASELINK_P (templ)) |
2311 | templ = lookup_template_function (templ, template_args); | |
50ad9642 MM |
2312 | else |
2313 | { | |
5ade1ed2 DG |
2314 | if (complain & tf_error) |
2315 | error ("%qD is not a member template function", name); | |
50ad9642 MM |
2316 | return error_mark_node; |
2317 | } | |
9e9ff709 MS |
2318 | } |
2319 | } | |
2320 | ||
a723baf1 MM |
2321 | if (TREE_DEPRECATED (member)) |
2322 | warn_deprecated_use (member); | |
2323 | ||
02ed62dd MM |
2324 | if (template_p) |
2325 | check_template_keyword (member); | |
2326 | ||
d17811fd | 2327 | expr = build_class_member_access_expr (object, member, access_path, |
5ade1ed2 DG |
2328 | /*preserve_reference=*/false, |
2329 | complain); | |
d17811fd | 2330 | if (processing_template_decl && expr != error_mark_node) |
61e71a9e NS |
2331 | { |
2332 | if (BASELINK_P (member)) | |
2333 | { | |
2334 | if (TREE_CODE (orig_name) == SCOPE_REF) | |
2335 | BASELINK_QUALIFIED_P (member) = 1; | |
2336 | orig_name = member; | |
2337 | } | |
2338 | return build_min_non_dep (COMPONENT_REF, expr, | |
2339 | orig_object, orig_name, | |
2340 | NULL_TREE); | |
2341 | } | |
3db45ab5 | 2342 | |
d17811fd | 2343 | return expr; |
8d08fdba | 2344 | } |
5156628f | 2345 | |
50ad9642 MM |
2346 | /* Return an expression for the MEMBER_NAME field in the internal |
2347 | representation of PTRMEM, a pointer-to-member function. (Each | |
2348 | pointer-to-member function type gets its own RECORD_TYPE so it is | |
2349 | more convenient to access the fields by name than by FIELD_DECL.) | |
2350 | This routine converts the NAME to a FIELD_DECL and then creates the | |
2351 | node for the complete expression. */ | |
5156628f MS |
2352 | |
2353 | tree | |
50ad9642 | 2354 | build_ptrmemfunc_access_expr (tree ptrmem, tree member_name) |
5156628f | 2355 | { |
50ad9642 MM |
2356 | tree ptrmem_type; |
2357 | tree member; | |
2358 | tree member_type; | |
2359 | ||
2360 | /* This code is a stripped down version of | |
2361 | build_class_member_access_expr. It does not work to use that | |
2362 | routine directly because it expects the object to be of class | |
2363 | type. */ | |
2364 | ptrmem_type = TREE_TYPE (ptrmem); | |
50bc768d | 2365 | gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type)); |
50ad9642 | 2366 | member = lookup_member (ptrmem_type, member_name, /*protect=*/0, |
86ac0575 | 2367 | /*want_type=*/false); |
50ad9642 MM |
2368 | member_type = cp_build_qualified_type (TREE_TYPE (member), |
2369 | cp_type_quals (ptrmem_type)); | |
7866705a SB |
2370 | return fold_build3 (COMPONENT_REF, member_type, |
2371 | ptrmem, member, NULL_TREE); | |
5156628f | 2372 | } |
50ad9642 | 2373 | |
8d08fdba MS |
2374 | /* Given an expression PTR for a pointer, return an expression |
2375 | for the value pointed to. | |
2376 | ERRORSTRING is the name of the operator to appear in error messages. | |
2377 | ||
2378 | This function may need to overload OPERATOR_FNNAME. | |
2379 | Must also handle REFERENCE_TYPEs for C++. */ | |
2380 | ||
2381 | tree | |
5ade1ed2 DG |
2382 | build_x_indirect_ref (tree expr, const char *errorstring, |
2383 | tsubst_flags_t complain) | |
8d08fdba | 2384 | { |
d17811fd | 2385 | tree orig_expr = expr; |
5566b478 MS |
2386 | tree rval; |
2387 | ||
5156628f | 2388 | if (processing_template_decl) |
d17811fd MM |
2389 | { |
2390 | if (type_dependent_expression_p (expr)) | |
2391 | return build_min_nt (INDIRECT_REF, expr); | |
2392 | expr = build_non_dependent_expr (expr); | |
2393 | } | |
5566b478 | 2394 | |
d17811fd | 2395 | rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE, |
5ade1ed2 | 2396 | NULL_TREE, /*overloaded_p=*/NULL, complain); |
d17811fd | 2397 | if (!rval) |
5ade1ed2 | 2398 | rval = cp_build_indirect_ref (expr, errorstring, complain); |
d17811fd MM |
2399 | |
2400 | if (processing_template_decl && rval != error_mark_node) | |
8e1daa34 | 2401 | return build_min_non_dep (INDIRECT_REF, rval, orig_expr); |
d17811fd | 2402 | else |
8d08fdba | 2403 | return rval; |
8d08fdba MS |
2404 | } |
2405 | ||
5ade1ed2 | 2406 | /* Helper function called from c-common. */ |
8d08fdba | 2407 | tree |
c9f9eb5d AH |
2408 | build_indirect_ref (location_t loc __attribute__ ((__unused__)), |
2409 | tree ptr, const char *errorstring) | |
5ade1ed2 DG |
2410 | { |
2411 | return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error); | |
2412 | } | |
2413 | ||
2414 | tree | |
2415 | cp_build_indirect_ref (tree ptr, const char *errorstring, | |
2416 | tsubst_flags_t complain) | |
8d08fdba | 2417 | { |
926ce8bd | 2418 | tree pointer, type; |
c11b6f21 MS |
2419 | |
2420 | if (ptr == error_mark_node) | |
2421 | return error_mark_node; | |
2422 | ||
90e734a8 JM |
2423 | if (ptr == current_class_ptr) |
2424 | return current_class_ref; | |
2425 | ||
c11b6f21 | 2426 | pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE |
0a72704b | 2427 | ? ptr : decay_conversion (ptr)); |
c11b6f21 | 2428 | type = TREE_TYPE (pointer); |
8d08fdba | 2429 | |
db24eb1f | 2430 | if (POINTER_TYPE_P (type)) |
8d08fdba | 2431 | { |
01240200 | 2432 | /* [expr.unary.op] |
c8094d83 | 2433 | |
01240200 | 2434 | If the type of the expression is "pointer to T," the type |
c8094d83 | 2435 | of the result is "T." |
01240200 | 2436 | |
0cbd7506 | 2437 | We must use the canonical variant because certain parts of |
01240200 MM |
2438 | the back end, like fold, do pointer comparisons between |
2439 | types. */ | |
2440 | tree t = canonical_type_variant (TREE_TYPE (type)); | |
2441 | ||
5f9e56b3 | 2442 | if (CONVERT_EXPR_P (ptr) |
79bedddc SR |
2443 | || TREE_CODE (ptr) == VIEW_CONVERT_EXPR) |
2444 | { | |
2445 | /* If a warning is issued, mark it to avoid duplicates from | |
2446 | the backend. This only needs to be done at | |
2447 | warn_strict_aliasing > 2. */ | |
2448 | if (warn_strict_aliasing > 2) | |
2449 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)), | |
2450 | type, TREE_OPERAND (ptr, 0))) | |
2451 | TREE_NO_WARNING (ptr) = 1; | |
2452 | } | |
2453 | ||
b72801e2 | 2454 | if (VOID_TYPE_P (t)) |
0cbd7506 MS |
2455 | { |
2456 | /* A pointer to incomplete type (other than cv void) can be | |
2457 | dereferenced [expr.unary.op]/1 */ | |
5ade1ed2 DG |
2458 | if (complain & tf_error) |
2459 | error ("%qT is not a pointer-to-object type", type); | |
0cbd7506 MS |
2460 | return error_mark_node; |
2461 | } | |
a9183fef | 2462 | else if (TREE_CODE (pointer) == ADDR_EXPR |
688f6688 | 2463 | && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0)))) |
01240200 MM |
2464 | /* The POINTER was something like `&x'. We simplify `*&x' to |
2465 | `x'. */ | |
8d08fdba MS |
2466 | return TREE_OPERAND (pointer, 0); |
2467 | else | |
2468 | { | |
01240200 | 2469 | tree ref = build1 (INDIRECT_REF, t, pointer); |
8d08fdba | 2470 | |
b0d75c1e BK |
2471 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, |
2472 | so that we get the proper error message if the result is used | |
2473 | to assign to. Also, &* is supposed to be a no-op. */ | |
91063b51 MM |
2474 | TREE_READONLY (ref) = CP_TYPE_CONST_P (t); |
2475 | TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t); | |
8d08fdba | 2476 | TREE_SIDE_EFFECTS (ref) |
271bd540 | 2477 | = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)); |
8d08fdba MS |
2478 | return ref; |
2479 | } | |
2480 | } | |
5ade1ed2 DG |
2481 | else if (!(complain & tf_error)) |
2482 | /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */ | |
2483 | ; | |
8d08fdba MS |
2484 | /* `pointer' won't be an error_mark_node if we were given a |
2485 | pointer to member, so it's cool to check for this here. */ | |
a5ac359a | 2486 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
a82e1a7d | 2487 | error ("invalid use of %qs on pointer to member", errorstring); |
8d08fdba MS |
2488 | else if (pointer != error_mark_node) |
2489 | { | |
2490 | if (errorstring) | |
a82e1a7d | 2491 | error ("invalid type argument of %qs", errorstring); |
8d08fdba | 2492 | else |
8251199e | 2493 | error ("invalid type argument"); |
8d08fdba MS |
2494 | } |
2495 | return error_mark_node; | |
2496 | } | |
2497 | ||
2498 | /* This handles expressions of the form "a[i]", which denotes | |
2499 | an array reference. | |
2500 | ||
2501 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
2502 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
2503 | This avoids forcing the array out of registers, and can work on | |
2504 | arrays that are not lvalues (for example, members of structures returned | |
2505 | by functions). | |
2506 | ||
2507 | If INDEX is of some user-defined type, it must be converted to | |
2508 | integer type. Otherwise, to make a compatible PLUS_EXPR, it | |
a63068b6 AH |
2509 | will inherit the type of the array, which will be some pointer type. |
2510 | ||
2511 | LOC is the location to use in building the array reference. */ | |
8d08fdba | 2512 | |
8d08fdba | 2513 | tree |
a63068b6 | 2514 | build_array_ref (tree array, tree idx, location_t loc) |
8d08fdba | 2515 | { |
a63068b6 AH |
2516 | tree ret; |
2517 | ||
8d08fdba MS |
2518 | if (idx == 0) |
2519 | { | |
a63068b6 | 2520 | error_at (loc, "subscript missing in array reference"); |
8d08fdba MS |
2521 | return error_mark_node; |
2522 | } | |
2523 | ||
2524 | if (TREE_TYPE (array) == error_mark_node | |
2525 | || TREE_TYPE (idx) == error_mark_node) | |
2526 | return error_mark_node; | |
2527 | ||
0450d74d RH |
2528 | /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference |
2529 | inside it. */ | |
2530 | switch (TREE_CODE (array)) | |
2531 | { | |
2532 | case COMPOUND_EXPR: | |
2533 | { | |
a63068b6 AH |
2534 | tree value = build_array_ref (TREE_OPERAND (array, 1), idx, loc); |
2535 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (value), | |
2536 | TREE_OPERAND (array, 0), value); | |
2537 | SET_EXPR_LOCATION (ret, loc); | |
2538 | return ret; | |
0450d74d RH |
2539 | } |
2540 | ||
2541 | case COND_EXPR: | |
a63068b6 AH |
2542 | ret = build_conditional_expr |
2543 | (TREE_OPERAND (array, 0), | |
2544 | build_array_ref (TREE_OPERAND (array, 1), idx, loc), | |
2545 | build_array_ref (TREE_OPERAND (array, 2), idx, loc), | |
2546 | tf_warning_or_error); | |
8aff539b | 2547 | protected_set_expr_location (ret, loc); |
a63068b6 | 2548 | return ret; |
0450d74d RH |
2549 | |
2550 | default: | |
2551 | break; | |
2552 | } | |
2553 | ||
6de9cd9a | 2554 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) |
8d08fdba | 2555 | { |
e4d35515 | 2556 | tree rval, type; |
8d08fdba | 2557 | |
ff6b6641 | 2558 | warn_array_subscript_with_type_char (idx); |
8d08fdba | 2559 | |
adf2edec | 2560 | if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx))) |
8d08fdba | 2561 | { |
a63068b6 | 2562 | error_at (loc, "array subscript is not an integer"); |
8d08fdba MS |
2563 | return error_mark_node; |
2564 | } | |
2565 | ||
0a72704b MM |
2566 | /* Apply integral promotions *after* noticing character types. |
2567 | (It is unclear why we do these promotions -- the standard | |
6ccf2f7d | 2568 | does not say that we should. In fact, the natural thing would |
0a72704b MM |
2569 | seem to be to convert IDX to ptrdiff_t; we're performing |
2570 | pointer arithmetic.) */ | |
2571 | idx = perform_integral_promotions (idx); | |
2572 | ||
8d08fdba MS |
2573 | /* An array that is indexed by a non-constant |
2574 | cannot be stored in a register; we must be able to do | |
2575 | address arithmetic on its address. | |
2576 | Likewise an array of elements of variable size. */ | |
2577 | if (TREE_CODE (idx) != INTEGER_CST | |
d0f062fb | 2578 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
3c215895 JM |
2579 | && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) |
2580 | != INTEGER_CST))) | |
8d08fdba | 2581 | { |
dffd7eb6 | 2582 | if (!cxx_mark_addressable (array)) |
8d08fdba MS |
2583 | return error_mark_node; |
2584 | } | |
0450d74d | 2585 | |
8d08fdba MS |
2586 | /* An array that is indexed by a constant value which is not within |
2587 | the array bounds cannot be stored in a register either; because we | |
2588 | would get a crash in store_bit_field/extract_bit_field when trying | |
2589 | to access a non-existent part of the register. */ | |
2590 | if (TREE_CODE (idx) == INTEGER_CST | |
eb34af89 RK |
2591 | && TYPE_DOMAIN (TREE_TYPE (array)) |
2592 | && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array)))) | |
8d08fdba | 2593 | { |
dffd7eb6 | 2594 | if (!cxx_mark_addressable (array)) |
8d08fdba MS |
2595 | return error_mark_node; |
2596 | } | |
2597 | ||
fcf73884 | 2598 | if (!lvalue_p (array)) |
a63068b6 AH |
2599 | pedwarn (loc, OPT_pedantic, |
2600 | "ISO C++ forbids subscripting non-lvalue array"); | |
8d08fdba | 2601 | |
b7484fbe MS |
2602 | /* Note in C++ it is valid to subscript a `register' array, since |
2603 | it is valid to take the address of something with that | |
2604 | storage specification. */ | |
2605 | if (extra_warnings) | |
8d08fdba MS |
2606 | { |
2607 | tree foo = array; | |
2608 | while (TREE_CODE (foo) == COMPONENT_REF) | |
2609 | foo = TREE_OPERAND (foo, 0); | |
2610 | if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)) | |
a63068b6 AH |
2611 | warning_at (loc, OPT_Wextra, |
2612 | "subscripting array declared %<register%>"); | |
8d08fdba MS |
2613 | } |
2614 | ||
01240200 | 2615 | type = TREE_TYPE (TREE_TYPE (array)); |
f293ce4b | 2616 | rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE); |
8d08fdba MS |
2617 | /* Array ref is const/volatile if the array elements are |
2618 | or if the array is.. */ | |
2619 | TREE_READONLY (rval) | |
91063b51 | 2620 | |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array)); |
8d08fdba | 2621 | TREE_SIDE_EFFECTS (rval) |
91063b51 | 2622 | |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array)); |
8d08fdba | 2623 | TREE_THIS_VOLATILE (rval) |
91063b51 | 2624 | |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array)); |
a63068b6 | 2625 | ret = require_complete_type (fold_if_not_in_template (rval)); |
8aff539b | 2626 | protected_set_expr_location (ret, loc); |
a63068b6 | 2627 | return ret; |
8d08fdba MS |
2628 | } |
2629 | ||
2630 | { | |
2631 | tree ar = default_conversion (array); | |
2632 | tree ind = default_conversion (idx); | |
2633 | ||
2634 | /* Put the integer in IND to simplify error checking. */ | |
2635 | if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) | |
2636 | { | |
2637 | tree temp = ar; | |
2638 | ar = ind; | |
2639 | ind = temp; | |
2640 | } | |
2641 | ||
2642 | if (ar == error_mark_node) | |
2643 | return ar; | |
2644 | ||
2645 | if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE) | |
2646 | { | |
a63068b6 | 2647 | error_at (loc, "subscripted value is neither array nor pointer"); |
8d08fdba MS |
2648 | return error_mark_node; |
2649 | } | |
2650 | if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) | |
2651 | { | |
a63068b6 | 2652 | error_at (loc, "array subscript is not an integer"); |
8d08fdba MS |
2653 | return error_mark_node; |
2654 | } | |
2655 | ||
0958b0d3 DM |
2656 | warn_array_subscript_with_type_char (idx); |
2657 | ||
ba47d38d AH |
2658 | ret = cp_build_indirect_ref (cp_build_binary_op (input_location, |
2659 | PLUS_EXPR, ar, ind, | |
a63068b6 AH |
2660 | tf_warning_or_error), |
2661 | "array indexing", | |
2662 | tf_warning_or_error); | |
2663 | protected_set_expr_location (ret, loc); | |
2664 | return ret; | |
8d08fdba MS |
2665 | } |
2666 | } | |
2667 | \f | |
8d08fdba | 2668 | /* Resolve a pointer to member function. INSTANCE is the object |
c87978aa JM |
2669 | instance to use, if the member points to a virtual member. |
2670 | ||
2671 | This used to avoid checking for virtual functions if basetype | |
2672 | has no virtual functions, according to an earlier ANSI draft. | |
2673 | With the final ISO C++ rules, such an optimization is | |
2674 | incorrect: A pointer to a derived member can be static_cast | |
2675 | to pointer-to-base-member, as long as the dynamic object | |
c6002625 | 2676 | later has the right member. */ |
8d08fdba MS |
2677 | |
2678 | tree | |
5671bf27 | 2679 | get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function) |
8d08fdba MS |
2680 | { |
2681 | if (TREE_CODE (function) == OFFSET_REF) | |
338d90b8 | 2682 | function = TREE_OPERAND (function, 1); |
8d08fdba MS |
2683 | |
2684 | if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function))) | |
2685 | { | |
c87978aa JM |
2686 | tree idx, delta, e1, e2, e3, vtbl, basetype; |
2687 | tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)); | |
f30432d7 | 2688 | |
b7484fbe | 2689 | tree instance_ptr = *instance_ptrptr; |
1c83ea9f | 2690 | tree instance_save_expr = 0; |
c87978aa | 2691 | if (instance_ptr == error_mark_node) |
32facac8 | 2692 | { |
c87978aa JM |
2693 | if (TREE_CODE (function) == PTRMEM_CST) |
2694 | { | |
2695 | /* Extracting the function address from a pmf is only | |
2696 | allowed with -Wno-pmf-conversions. It only works for | |
c6002625 | 2697 | pmf constants. */ |
c87978aa JM |
2698 | e1 = build_addr_func (PTRMEM_CST_MEMBER (function)); |
2699 | e1 = convert (fntype, e1); | |
2700 | return e1; | |
2701 | } | |
2702 | else | |
2703 | { | |
a82e1a7d | 2704 | error ("object missing in use of %qE", function); |
c87978aa JM |
2705 | return error_mark_node; |
2706 | } | |
32facac8 ML |
2707 | } |
2708 | ||
b7484fbe | 2709 | if (TREE_SIDE_EFFECTS (instance_ptr)) |
1c83ea9f | 2710 | instance_ptr = instance_save_expr = save_expr (instance_ptr); |
b7484fbe | 2711 | |
f30432d7 MS |
2712 | if (TREE_SIDE_EFFECTS (function)) |
2713 | function = save_expr (function); | |
2714 | ||
c87978aa | 2715 | /* Start by extracting all the information from the PMF itself. */ |
10b2bcdd | 2716 | e3 = pfn_from_ptrmemfunc (function); |
a298680c | 2717 | delta = delta_from_ptrmemfunc (function); |
85b7def6 | 2718 | idx = build1 (NOP_EXPR, vtable_index_type, e3); |
cb7fdde2 AO |
2719 | switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) |
2720 | { | |
2721 | case ptrmemfunc_vbit_in_pfn: | |
ba47d38d AH |
2722 | e1 = cp_build_binary_op (input_location, |
2723 | BIT_AND_EXPR, idx, integer_one_node, | |
5ade1ed2 | 2724 | tf_warning_or_error); |
ba47d38d AH |
2725 | idx = cp_build_binary_op (input_location, |
2726 | MINUS_EXPR, idx, integer_one_node, | |
5ade1ed2 | 2727 | tf_warning_or_error); |
cb7fdde2 AO |
2728 | break; |
2729 | ||
2730 | case ptrmemfunc_vbit_in_delta: | |
ba47d38d AH |
2731 | e1 = cp_build_binary_op (input_location, |
2732 | BIT_AND_EXPR, delta, integer_one_node, | |
5ade1ed2 | 2733 | tf_warning_or_error); |
ba47d38d AH |
2734 | delta = cp_build_binary_op (input_location, |
2735 | RSHIFT_EXPR, delta, integer_one_node, | |
5ade1ed2 | 2736 | tf_warning_or_error); |
cb7fdde2 AO |
2737 | break; |
2738 | ||
2739 | default: | |
315fb5db | 2740 | gcc_unreachable (); |
cb7fdde2 | 2741 | } |
03b256e4 | 2742 | |
81ae598b MM |
2743 | /* Convert down to the right base before using the instance. A |
2744 | special case is that in a pointer to member of class C, C may | |
2745 | be incomplete. In that case, the function will of course be | |
2746 | a member of C, and no conversion is required. In fact, | |
2747 | lookup_base will fail in that case, because incomplete | |
c8094d83 | 2748 | classes do not have BINFOs. */ |
c87978aa | 2749 | basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)); |
c8094d83 | 2750 | if (!same_type_ignoring_top_level_qualifiers_p |
81ae598b MM |
2751 | (basetype, TREE_TYPE (TREE_TYPE (instance_ptr)))) |
2752 | { | |
2753 | basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)), | |
2754 | basetype, ba_check, NULL); | |
c8094d83 | 2755 | instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, |
81ae598b MM |
2756 | 1); |
2757 | if (instance_ptr == error_mark_node) | |
2758 | return error_mark_node; | |
2759 | } | |
c87978aa | 2760 | /* ...and then the delta in the PMF. */ |
5be014d5 AP |
2761 | instance_ptr = build2 (POINTER_PLUS_EXPR, TREE_TYPE (instance_ptr), |
2762 | instance_ptr, fold_convert (sizetype, delta)); | |
c87978aa JM |
2763 | |
2764 | /* Hand back the adjusted 'this' argument to our caller. */ | |
2765 | *instance_ptrptr = instance_ptr; | |
2766 | ||
2767 | /* Next extract the vtable pointer from the object. */ | |
2768 | vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node), | |
2769 | instance_ptr); | |
5ade1ed2 | 2770 | vtbl = cp_build_indirect_ref (vtbl, NULL, tf_warning_or_error); |
85b7def6 | 2771 | |
c87978aa | 2772 | /* Finally, extract the function pointer from the vtable. */ |
5be014d5 AP |
2773 | e2 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtbl), vtbl, |
2774 | fold_convert (sizetype, idx)); | |
5ade1ed2 | 2775 | e2 = cp_build_indirect_ref (e2, NULL, tf_warning_or_error); |
f63ab951 | 2776 | TREE_CONSTANT (e2) = 1; |
c7e266a6 | 2777 | |
67231816 RH |
2778 | /* When using function descriptors, the address of the |
2779 | vtable entry is treated as a function pointer. */ | |
2780 | if (TARGET_VTABLE_USES_DESCRIPTORS) | |
2781 | e2 = build1 (NOP_EXPR, TREE_TYPE (e2), | |
5ade1ed2 DG |
2782 | cp_build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1, |
2783 | tf_warning_or_error)); | |
67231816 | 2784 | |
a489b1f0 | 2785 | e2 = fold_convert (TREE_TYPE (e3), e2); |
5ade1ed2 | 2786 | e1 = build_conditional_expr (e1, e2, e3, tf_warning_or_error); |
c8094d83 | 2787 | |
03b256e4 ML |
2788 | /* Make sure this doesn't get evaluated first inside one of the |
2789 | branches of the COND_EXPR. */ | |
1c83ea9f | 2790 | if (instance_save_expr) |
f293ce4b RS |
2791 | e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1), |
2792 | instance_save_expr, e1); | |
03b256e4 | 2793 | |
f49422da | 2794 | function = e1; |
8d08fdba MS |
2795 | } |
2796 | return function; | |
2797 | } | |
2798 | ||
5ade1ed2 | 2799 | /* Used by the C-common bits. */ |
8d08fdba | 2800 | tree |
5671bf27 | 2801 | build_function_call (tree function, tree params) |
5ade1ed2 DG |
2802 | { |
2803 | return cp_build_function_call (function, params, tf_warning_or_error); | |
2804 | } | |
2805 | ||
2806 | tree | |
2807 | cp_build_function_call (tree function, tree params, tsubst_flags_t complain) | |
8d08fdba | 2808 | { |
926ce8bd | 2809 | tree fntype, fndecl; |
58ec3cc5 | 2810 | tree name = NULL_TREE; |
8d08fdba | 2811 | int is_method; |
2e4cf9dc | 2812 | tree original = function; |
94a0dd7b SL |
2813 | int nargs, parm_types_len; |
2814 | tree *argarray; | |
2815 | tree parm_types; | |
8d08fdba | 2816 | |
e58a9aa1 ZL |
2817 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
2818 | expressions, like those used for ObjC messenger dispatches. */ | |
2819 | function = objc_rewrite_function_call (function, params); | |
2820 | ||
8d08fdba MS |
2821 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
2822 | Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */ | |
2823 | if (TREE_CODE (function) == NOP_EXPR | |
2824 | && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0))) | |
2825 | function = TREE_OPERAND (function, 0); | |
2826 | ||
2827 | if (TREE_CODE (function) == FUNCTION_DECL) | |
2828 | { | |
2829 | name = DECL_NAME (function); | |
8d08fdba | 2830 | |
72b7eeff | 2831 | mark_used (function); |
8d08fdba MS |
2832 | fndecl = function; |
2833 | ||
2834 | /* Convert anything with function type to a pointer-to-function. */ | |
fcf73884 | 2835 | if (DECL_MAIN_P (function) && (complain & tf_error)) |
509c9d60 | 2836 | pedwarn (input_location, OPT_pedantic, |
fcf73884 | 2837 | "ISO C++ forbids calling %<::main%> from within program"); |
8d08fdba | 2838 | |
4847609e | 2839 | function = build_addr_func (function); |
8d08fdba MS |
2840 | } |
2841 | else | |
2842 | { | |
2843 | fndecl = NULL_TREE; | |
2844 | ||
4ac14744 | 2845 | function = build_addr_func (function); |
8d08fdba MS |
2846 | } |
2847 | ||
4ac14744 MS |
2848 | if (function == error_mark_node) |
2849 | return error_mark_node; | |
2850 | ||
8d08fdba MS |
2851 | fntype = TREE_TYPE (function); |
2852 | ||
2853 | if (TYPE_PTRMEMFUNC_P (fntype)) | |
2854 | { | |
5ade1ed2 DG |
2855 | if (complain & tf_error) |
2856 | error ("must use %<.*%> or %<->*%> to call pointer-to-member " | |
7b98a725 JW |
2857 | "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>", |
2858 | original, original); | |
4ac14744 | 2859 | return error_mark_node; |
8d08fdba MS |
2860 | } |
2861 | ||
2862 | is_method = (TREE_CODE (fntype) == POINTER_TYPE | |
2863 | && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE); | |
2864 | ||
2865 | if (!((TREE_CODE (fntype) == POINTER_TYPE | |
2866 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE) | |
386b8a85 JM |
2867 | || is_method |
2868 | || TREE_CODE (function) == TEMPLATE_ID_EXPR)) | |
8d08fdba | 2869 | { |
5ade1ed2 DG |
2870 | if (complain & tf_error) |
2871 | error ("%qE cannot be used as a function", original); | |
8d08fdba MS |
2872 | return error_mark_node; |
2873 | } | |
2874 | ||
2875 | /* fntype now gets the type of function pointed to. */ | |
2876 | fntype = TREE_TYPE (fntype); | |
94a0dd7b SL |
2877 | parm_types = TYPE_ARG_TYPES (fntype); |
2878 | ||
2879 | /* Allocate storage for converted arguments. */ | |
2880 | parm_types_len = list_length (parm_types); | |
2881 | nargs = list_length (params); | |
2882 | if (parm_types_len > nargs) | |
2883 | nargs = parm_types_len; | |
2884 | argarray = (tree *) alloca (nargs * sizeof (tree)); | |
8d08fdba MS |
2885 | |
2886 | /* Convert the parameters to the types declared in the | |
2887 | function prototype, or apply default promotions. */ | |
94a0dd7b | 2888 | nargs = convert_arguments (nargs, argarray, parm_types, |
5ade1ed2 DG |
2889 | params, fndecl, LOOKUP_NORMAL, |
2890 | complain); | |
94a0dd7b | 2891 | if (nargs < 0) |
125e6594 | 2892 | return error_mark_node; |
863adfc0 | 2893 | |
dd66b8e8 JM |
2894 | /* Check for errors in format strings and inappropriately |
2895 | null parameters. */ | |
94a0dd7b SL |
2896 | check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray, |
2897 | parm_types); | |
8d08fdba | 2898 | |
94a0dd7b | 2899 | return build_cxx_call (function, nargs, argarray); |
8d08fdba | 2900 | } |
8d08fdba MS |
2901 | \f |
2902 | /* Convert the actual parameter expressions in the list VALUES | |
2903 | to the types in the list TYPELIST. | |
2904 | If parmdecls is exhausted, or when an element has NULL as its type, | |
2905 | perform the default conversions. | |
2906 | ||
94a0dd7b SL |
2907 | Store the converted arguments in ARGARRAY. NARGS is the size of this array. |
2908 | ||
8d08fdba MS |
2909 | NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. |
2910 | ||
2911 | This is also where warnings about wrong number of args are generated. | |
c8094d83 | 2912 | |
94a0dd7b SL |
2913 | Returns the actual number of arguments processed (which might be less |
2914 | than NARGS), or -1 on error. | |
8d08fdba | 2915 | |
94a0dd7b SL |
2916 | VALUES is a chain of TREE_LIST nodes with the elements of the list |
2917 | in the TREE_VALUE slots of those nodes. | |
8d08fdba MS |
2918 | |
2919 | In C++, unspecified trailing parameters can be filled in with their | |
2920 | default arguments, if such were specified. Do so here. */ | |
2921 | ||
94a0dd7b SL |
2922 | static int |
2923 | convert_arguments (int nargs, tree *argarray, | |
5ade1ed2 DG |
2924 | tree typelist, tree values, tree fndecl, int flags, |
2925 | tsubst_flags_t complain) | |
8d08fdba | 2926 | { |
926ce8bd | 2927 | tree typetail, valtail; |
d8e178a0 | 2928 | const char *called_thing = 0; |
8d08fdba MS |
2929 | int i = 0; |
2930 | ||
faf5394a MS |
2931 | /* Argument passing is always copy-initialization. */ |
2932 | flags |= LOOKUP_ONLYCONVERTING; | |
2933 | ||
8d08fdba MS |
2934 | if (fndecl) |
2935 | { | |
2936 | if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE) | |
2937 | { | |
2938 | if (DECL_NAME (fndecl) == NULL_TREE | |
2939 | || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl))) | |
2940 | called_thing = "constructor"; | |
2941 | else | |
2942 | called_thing = "member function"; | |
8d08fdba MS |
2943 | } |
2944 | else | |
a0a33927 | 2945 | called_thing = "function"; |
8d08fdba MS |
2946 | } |
2947 | ||
2948 | for (valtail = values, typetail = typelist; | |
2949 | valtail; | |
2950 | valtail = TREE_CHAIN (valtail), i++) | |
2951 | { | |
926ce8bd KH |
2952 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
2953 | tree val = TREE_VALUE (valtail); | |
8d08fdba | 2954 | |
6af455d7 | 2955 | if (val == error_mark_node || type == error_mark_node) |
94a0dd7b | 2956 | return -1; |
8ccc31eb | 2957 | |
8d08fdba MS |
2958 | if (type == void_type_node) |
2959 | { | |
5ade1ed2 DG |
2960 | if (complain & tf_error) |
2961 | { | |
2962 | if (fndecl) | |
2963 | { | |
2964 | error ("too many arguments to %s %q+#D", | |
2965 | called_thing, fndecl); | |
2966 | error ("at this point in file"); | |
2967 | } | |
2968 | else | |
2969 | error ("too many arguments to function"); | |
2970 | return i; | |
2971 | } | |
2972 | else | |
2973 | return -1; | |
8d08fdba MS |
2974 | } |
2975 | ||
8d08fdba MS |
2976 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
2977 | Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */ | |
2978 | if (TREE_CODE (val) == NOP_EXPR | |
a0a33927 MS |
2979 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)) |
2980 | && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)) | |
8d08fdba MS |
2981 | val = TREE_OPERAND (val, 0); |
2982 | ||
00595019 MS |
2983 | if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE) |
2984 | { | |
2985 | if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE | |
8d08fdba | 2986 | || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE |
00595019 | 2987 | || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE) |
0a72704b | 2988 | val = decay_conversion (val); |
00595019 | 2989 | } |
8d08fdba MS |
2990 | |
2991 | if (val == error_mark_node) | |
94a0dd7b | 2992 | return -1; |
8d08fdba | 2993 | |
8d08fdba MS |
2994 | if (type != 0) |
2995 | { | |
2996 | /* Formal parm type is specified by a function prototype. */ | |
2997 | tree parmval; | |
2998 | ||
d0f062fb | 2999 | if (!COMPLETE_TYPE_P (complete_type (type))) |
8d08fdba | 3000 | { |
5ade1ed2 DG |
3001 | if (complain & tf_error) |
3002 | { | |
3003 | if (fndecl) | |
3004 | error ("parameter %P of %qD has incomplete type %qT", | |
3005 | i, fndecl, type); | |
3006 | else | |
3007 | error ("parameter %P has incomplete type %qT", i, type); | |
3008 | } | |
3afd2e20 | 3009 | parmval = error_mark_node; |
8d08fdba MS |
3010 | } |
3011 | else | |
3012 | { | |
9a3b49ac | 3013 | parmval = convert_for_initialization |
56ae6d77 | 3014 | (NULL_TREE, type, val, flags, |
5ade1ed2 | 3015 | "argument passing", fndecl, i, complain); |
8e51619a | 3016 | parmval = convert_for_arg_passing (type, parmval); |
8d08fdba | 3017 | } |
878cd289 MS |
3018 | |
3019 | if (parmval == error_mark_node) | |
94a0dd7b | 3020 | return -1; |
878cd289 | 3021 | |
94a0dd7b | 3022 | argarray[i] = parmval; |
8d08fdba MS |
3023 | } |
3024 | else | |
3025 | { | |
d1188d91 | 3026 | if (fndecl && DECL_BUILT_IN (fndecl) |
4eaf1d5b AH |
3027 | && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P) |
3028 | /* Don't do ellipsis conversion for __built_in_constant_p | |
3029 | as this will result in spurious warnings for non-POD | |
3030 | types. */ | |
3031 | val = require_complete_type (val); | |
3032 | else | |
3033 | val = convert_arg_to_ellipsis (val); | |
3034 | ||
94a0dd7b | 3035 | argarray[i] = val; |
8d08fdba MS |
3036 | } |
3037 | ||
8d08fdba MS |
3038 | if (typetail) |
3039 | typetail = TREE_CHAIN (typetail); | |
3040 | } | |
3041 | ||
3042 | if (typetail != 0 && typetail != void_list_node) | |
3043 | { | |
823c22f9 NS |
3044 | /* See if there are default arguments that can be used. Because |
3045 | we hold default arguments in the FUNCTION_TYPE (which is so | |
3046 | wrong), we can see default parameters here from deduced | |
3047 | contexts (and via typeof) for indirect function calls. | |
3048 | Fortunately we know whether we have a function decl to | |
3049 | provide default arguments in a language conformant | |
3050 | manner. */ | |
3051 | if (fndecl && TREE_PURPOSE (typetail) | |
a723baf1 | 3052 | && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG) |
8d08fdba | 3053 | { |
f376e137 | 3054 | for (; typetail != void_list_node; ++i) |
8d08fdba | 3055 | { |
c8094d83 MS |
3056 | tree parmval |
3057 | = convert_default_arg (TREE_VALUE (typetail), | |
3058 | TREE_PURPOSE (typetail), | |
c3f08228 | 3059 | fndecl, i); |
8d08fdba | 3060 | |
878cd289 | 3061 | if (parmval == error_mark_node) |
94a0dd7b | 3062 | return -1; |
878cd289 | 3063 | |
94a0dd7b | 3064 | argarray[i] = parmval; |
8d08fdba MS |
3065 | typetail = TREE_CHAIN (typetail); |
3066 | /* ends with `...'. */ | |
3067 | if (typetail == NULL_TREE) | |
3068 | break; | |
3069 | } | |
3070 | } | |
3071 | else | |
3072 | { | |
5ade1ed2 DG |
3073 | if (complain & tf_error) |
3074 | { | |
3075 | if (fndecl) | |
3076 | { | |
3077 | error ("too few arguments to %s %q+#D", | |
3078 | called_thing, fndecl); | |
3079 | error ("at this point in file"); | |
3080 | } | |
3081 | else | |
3082 | error ("too few arguments to function"); | |
3083 | } | |
94a0dd7b | 3084 | return -1; |
8d08fdba MS |
3085 | } |
3086 | } | |
8d08fdba | 3087 | |
94a0dd7b SL |
3088 | gcc_assert (i <= nargs); |
3089 | return i; | |
8d08fdba MS |
3090 | } |
3091 | \f | |
3092 | /* Build a binary-operation expression, after performing default | |
2a67bec2 ILT |
3093 | conversions on the operands. CODE is the kind of expression to |
3094 | build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE | |
3095 | are the tree codes which correspond to ARG1 and ARG2 when issuing | |
3096 | warnings about possibly misplaced parentheses. They may differ | |
3097 | from the TREE_CODE of ARG1 and ARG2 if the parser has done constant | |
3098 | folding (e.g., if the parser sees "a | 1 + 1", it may call this | |
3099 | routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR). | |
3100 | To avoid issuing any parentheses warnings, pass ARG1_CODE and/or | |
3101 | ARG2_CODE as ERROR_MARK. */ | |
8d08fdba MS |
3102 | |
3103 | tree | |
2a67bec2 | 3104 | build_x_binary_op (enum tree_code code, tree arg1, enum tree_code arg1_code, |
5ade1ed2 DG |
3105 | tree arg2, enum tree_code arg2_code, bool *overloaded_p, |
3106 | tsubst_flags_t complain) | |
8d08fdba | 3107 | { |
d17811fd MM |
3108 | tree orig_arg1; |
3109 | tree orig_arg2; | |
3110 | tree expr; | |
5566b478 | 3111 | |
d17811fd MM |
3112 | orig_arg1 = arg1; |
3113 | orig_arg2 = arg2; | |
d6b4ea85 | 3114 | |
d17811fd | 3115 | if (processing_template_decl) |
14d22dd6 | 3116 | { |
d17811fd MM |
3117 | if (type_dependent_expression_p (arg1) |
3118 | || type_dependent_expression_p (arg2)) | |
3119 | return build_min_nt (code, arg1, arg2); | |
3120 | arg1 = build_non_dependent_expr (arg1); | |
3121 | arg2 = build_non_dependent_expr (arg2); | |
3122 | } | |
14d22dd6 | 3123 | |
d17811fd MM |
3124 | if (code == DOTSTAR_EXPR) |
3125 | expr = build_m_component_ref (arg1, arg2); | |
3126 | else | |
c8094d83 | 3127 | expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE, |
5ade1ed2 | 3128 | overloaded_p, complain); |
14d22dd6 | 3129 | |
2a67bec2 ILT |
3130 | /* Check for cases such as x+y<<z which users are likely to |
3131 | misinterpret. But don't warn about obj << x + y, since that is a | |
3132 | common idiom for I/O. */ | |
3133 | if (warn_parentheses | |
3134 | && !processing_template_decl | |
3135 | && !error_operand_p (arg1) | |
3136 | && !error_operand_p (arg2) | |
3137 | && (code != LSHIFT_EXPR | |
3138 | || !CLASS_TYPE_P (TREE_TYPE (arg1)))) | |
3139 | warn_about_parentheses (code, arg1_code, arg2_code); | |
3140 | ||
d17811fd | 3141 | if (processing_template_decl && expr != error_mark_node) |
8e1daa34 | 3142 | return build_min_non_dep (code, expr, orig_arg1, orig_arg2); |
c8094d83 | 3143 | |
d17811fd | 3144 | return expr; |
14d22dd6 MM |
3145 | } |
3146 | ||
5ade1ed2 DG |
3147 | /* For the c-common bits. */ |
3148 | tree | |
ba47d38d | 3149 | build_binary_op (location_t location, enum tree_code code, tree op0, tree op1, |
5ade1ed2 DG |
3150 | int convert_p ATTRIBUTE_UNUSED) |
3151 | { | |
ba47d38d | 3152 | return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error); |
5ade1ed2 DG |
3153 | } |
3154 | ||
3155 | ||
8d08fdba MS |
3156 | /* Build a binary-operation expression without default conversions. |
3157 | CODE is the kind of expression to build. | |
ba47d38d | 3158 | LOCATION is the location_t of the operator in the source code. |
8d08fdba MS |
3159 | This function differs from `build' in several ways: |
3160 | the data type of the result is computed and recorded in it, | |
3161 | warnings are generated if arg data types are invalid, | |
3162 | special handling for addition and subtraction of pointers is known, | |
3163 | and some optimization is done (operations on narrow ints | |
3164 | are done in the narrower type when that gives the same result). | |
3165 | Constant folding is also done before the result is returned. | |
3166 | ||
8d08fdba MS |
3167 | Note that the operands will never have enumeral types |
3168 | because either they have just had the default conversions performed | |
3169 | or they have both just been converted to some other type in which | |
3170 | the arithmetic is to be done. | |
3171 | ||
3172 | C++: must do special pointer arithmetic when implementing | |
3173 | multiple inheritance, and deal with pointer to member functions. */ | |
3174 | ||
3175 | tree | |
ba47d38d AH |
3176 | cp_build_binary_op (location_t location, |
3177 | enum tree_code code, tree orig_op0, tree orig_op1, | |
5ade1ed2 | 3178 | tsubst_flags_t complain) |
8d08fdba | 3179 | { |
39211cd5 | 3180 | tree op0, op1; |
926ce8bd | 3181 | enum tree_code code0, code1; |
39211cd5 | 3182 | tree type0, type1; |
4de67c26 | 3183 | const char *invalid_op_diag; |
8d08fdba MS |
3184 | |
3185 | /* Expression code to give to the expression when it is built. | |
3186 | Normally this is CODE, which is what the caller asked for, | |
3187 | but in some special cases we change it. */ | |
926ce8bd | 3188 | enum tree_code resultcode = code; |
8d08fdba MS |
3189 | |
3190 | /* Data type in which the computation is to be performed. | |
3191 | In the simplest cases this is the common type of the arguments. */ | |
926ce8bd | 3192 | tree result_type = NULL; |
8d08fdba MS |
3193 | |
3194 | /* Nonzero means operands have already been type-converted | |
3195 | in whatever way is necessary. | |
3196 | Zero means they need to be converted to RESULT_TYPE. */ | |
3197 | int converted = 0; | |
3198 | ||
28cbf42c MS |
3199 | /* Nonzero means create the expression with this type, rather than |
3200 | RESULT_TYPE. */ | |
3201 | tree build_type = 0; | |
3202 | ||
8d08fdba | 3203 | /* Nonzero means after finally constructing the expression |
faae18ab | 3204 | convert it to this type. */ |
8d08fdba MS |
3205 | tree final_type = 0; |
3206 | ||
455f19cb MM |
3207 | tree result; |
3208 | ||
8d08fdba MS |
3209 | /* Nonzero if this is an operation like MIN or MAX which can |
3210 | safely be computed in short if both args are promoted shorts. | |
3211 | Also implies COMMON. | |
3212 | -1 indicates a bitwise operation; this makes a difference | |
3213 | in the exact conditions for when it is safe to do the operation | |
3214 | in a narrower mode. */ | |
3215 | int shorten = 0; | |
3216 | ||
3217 | /* Nonzero if this is a comparison operation; | |
3218 | if both args are promoted shorts, compare the original shorts. | |
3219 | Also implies COMMON. */ | |
3220 | int short_compare = 0; | |
3221 | ||
8d08fdba MS |
3222 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ |
3223 | int common = 0; | |
3224 | ||
455f19cb MM |
3225 | /* True if both operands have arithmetic type. */ |
3226 | bool arithmetic_types_p; | |
3227 | ||
39211cd5 | 3228 | /* Apply default conversions. */ |
40260429 NS |
3229 | op0 = orig_op0; |
3230 | op1 = orig_op1; | |
c8094d83 | 3231 | |
a9aedbc2 MS |
3232 | if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR |
3233 | || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR | |
3234 | || code == TRUTH_XOR_EXPR) | |
3235 | { | |
40260429 NS |
3236 | if (!really_overloaded_fn (op0)) |
3237 | op0 = decay_conversion (op0); | |
3238 | if (!really_overloaded_fn (op1)) | |
3239 | op1 = decay_conversion (op1); | |
a9aedbc2 MS |
3240 | } |
3241 | else | |
3242 | { | |
40260429 NS |
3243 | if (!really_overloaded_fn (op0)) |
3244 | op0 = default_conversion (op0); | |
3245 | if (!really_overloaded_fn (op1)) | |
3246 | op1 = default_conversion (op1); | |
a9aedbc2 | 3247 | } |
39211cd5 | 3248 | |
a359be75 JM |
3249 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
3250 | STRIP_TYPE_NOPS (op0); | |
3251 | STRIP_TYPE_NOPS (op1); | |
3252 | ||
3253 | /* DTRT if one side is an overloaded function, but complain about it. */ | |
3254 | if (type_unknown_p (op0)) | |
3255 | { | |
c2ea3a40 | 3256 | tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none); |
a359be75 JM |
3257 | if (t != error_mark_node) |
3258 | { | |
5ade1ed2 | 3259 | if (complain & tf_error) |
cbe5f3b3 | 3260 | permerror (input_location, "assuming cast to type %qT from overloaded function", |
37ec60ed | 3261 | TREE_TYPE (t)); |
a359be75 JM |
3262 | op0 = t; |
3263 | } | |
3264 | } | |
3265 | if (type_unknown_p (op1)) | |
3266 | { | |
c2ea3a40 | 3267 | tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none); |
a359be75 JM |
3268 | if (t != error_mark_node) |
3269 | { | |
5ade1ed2 | 3270 | if (complain & tf_error) |
cbe5f3b3 | 3271 | permerror (input_location, "assuming cast to type %qT from overloaded function", |
37ec60ed | 3272 | TREE_TYPE (t)); |
a359be75 JM |
3273 | op1 = t; |
3274 | } | |
3275 | } | |
3276 | ||
39211cd5 MS |
3277 | type0 = TREE_TYPE (op0); |
3278 | type1 = TREE_TYPE (op1); | |
3279 | ||
3280 | /* The expression codes of the data types of the arguments tell us | |
3281 | whether the arguments are integers, floating, pointers, etc. */ | |
3282 | code0 = TREE_CODE (type0); | |
3283 | code1 = TREE_CODE (type1); | |
3284 | ||
8d08fdba MS |
3285 | /* If an error was already reported for one of the arguments, |
3286 | avoid reporting another error. */ | |
3287 | ||
3288 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
3289 | return error_mark_node; | |
3290 | ||
4de67c26 JM |
3291 | if ((invalid_op_diag |
3292 | = targetm.invalid_binary_op (code, type0, type1))) | |
3293 | { | |
3294 | error (invalid_op_diag); | |
3295 | return error_mark_node; | |
3296 | } | |
3297 | ||
8d08fdba MS |
3298 | switch (code) |
3299 | { | |
8d08fdba MS |
3300 | case MINUS_EXPR: |
3301 | /* Subtraction of two similar pointers. | |
3302 | We must subtract them as integers, then divide by object size. */ | |
3303 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
a5ac359a MM |
3304 | && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0), |
3305 | TREE_TYPE (type1))) | |
79a7c7fa | 3306 | return pointer_diff (op0, op1, common_type (type0, type1)); |
0225a19d | 3307 | /* In all other cases except pointer - int, the usual arithmetic |
39a13be5 | 3308 | rules apply. */ |
0225a19d MM |
3309 | else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE)) |
3310 | { | |
3311 | common = 1; | |
3312 | break; | |
3313 | } | |
3314 | /* The pointer - int case is just like pointer + int; fall | |
3315 | through. */ | |
3316 | case PLUS_EXPR: | |
3317 | if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE) | |
3318 | && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE)) | |
3319 | { | |
3320 | tree ptr_operand; | |
3321 | tree int_operand; | |
3322 | ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1); | |
3323 | int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1); | |
3324 | if (processing_template_decl) | |
3325 | { | |
3326 | result_type = TREE_TYPE (ptr_operand); | |
3327 | break; | |
3328 | } | |
3329 | return cp_pointer_int_sum (code, | |
3330 | ptr_operand, | |
3331 | int_operand); | |
3332 | } | |
3333 | common = 1; | |
8d08fdba MS |
3334 | break; |
3335 | ||
3336 | case MULT_EXPR: | |
3337 | common = 1; | |
3338 | break; | |
3339 | ||
3340 | case TRUNC_DIV_EXPR: | |
3341 | case CEIL_DIV_EXPR: | |
3342 | case FLOOR_DIV_EXPR: | |
3343 | case ROUND_DIV_EXPR: | |
3344 | case EXACT_DIV_EXPR: | |
37c46b43 | 3345 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
3a021db2 | 3346 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
37c46b43 | 3347 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
3a021db2 | 3348 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
8d08fdba | 3349 | { |
01c15146 VR |
3350 | enum tree_code tcode0 = code0, tcode1 = code1; |
3351 | ||
c9f9eb5d | 3352 | warn_for_div_by_zero (location, op1); |
c8094d83 | 3353 | |
01c15146 VR |
3354 | if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE) |
3355 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); | |
3356 | if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE) | |
3357 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); | |
3a021db2 | 3358 | |
01c15146 | 3359 | if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)) |
8d08fdba MS |
3360 | resultcode = RDIV_EXPR; |
3361 | else | |
3362 | /* When dividing two signed integers, we have to promote to int. | |
ddd5a7c1 | 3363 | unless we divide by a constant != -1. Note that default |
8d08fdba MS |
3364 | conversion will have been performed on the operands at this |
3365 | point, so we have to dig out the original type to find out if | |
3366 | it was unsigned. */ | |
3367 | shorten = ((TREE_CODE (op0) == NOP_EXPR | |
8df83eae | 3368 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) |
8d08fdba | 3369 | || (TREE_CODE (op1) == INTEGER_CST |
05bccae2 RK |
3370 | && ! integer_all_onesp (op1))); |
3371 | ||
8d08fdba MS |
3372 | common = 1; |
3373 | } | |
3374 | break; | |
3375 | ||
3376 | case BIT_AND_EXPR: | |
8d08fdba MS |
3377 | case BIT_IOR_EXPR: |
3378 | case BIT_XOR_EXPR: | |
73042643 | 3379 | if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
9ef0c8d9 AP |
3380 | || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
3381 | && !VECTOR_FLOAT_TYPE_P (type0) | |
3382 | && !VECTOR_FLOAT_TYPE_P (type1))) | |
8d08fdba | 3383 | shorten = -1; |
8d08fdba MS |
3384 | break; |
3385 | ||
3386 | case TRUNC_MOD_EXPR: | |
3387 | case FLOOR_MOD_EXPR: | |
c9f9eb5d | 3388 | warn_for_div_by_zero (location, op1); |
c8094d83 | 3389 | |
8d08fdba MS |
3390 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
3391 | { | |
3392 | /* Although it would be tempting to shorten always here, that loses | |
3393 | on some targets, since the modulo instruction is undefined if the | |
3394 | quotient can't be represented in the computation mode. We shorten | |
3395 | only if unsigned or if dividing by something we know != -1. */ | |
3396 | shorten = ((TREE_CODE (op0) == NOP_EXPR | |
8df83eae | 3397 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) |
8d08fdba | 3398 | || (TREE_CODE (op1) == INTEGER_CST |
05bccae2 | 3399 | && ! integer_all_onesp (op1))); |
8d08fdba MS |
3400 | common = 1; |
3401 | } | |
3402 | break; | |
3403 | ||
3404 | case TRUTH_ANDIF_EXPR: | |
3405 | case TRUTH_ORIF_EXPR: | |
3406 | case TRUTH_AND_EXPR: | |
3407 | case TRUTH_OR_EXPR: | |
255512c1 | 3408 | result_type = boolean_type_node; |
8d08fdba MS |
3409 | break; |
3410 | ||
3411 | /* Shift operations: result has same type as first operand; | |
3412 | always convert second operand to int. | |
3413 | Also set SHORT_SHIFT if shifting rightward. */ | |
3414 | ||
3415 | case RSHIFT_EXPR: | |
3416 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
3417 | { | |
3418 | result_type = type0; | |
3419 | if (TREE_CODE (op1) == INTEGER_CST) | |
3420 | { | |
3421 | if (tree_int_cst_lt (op1, integer_zero_node)) | |
5ade1ed2 DG |
3422 | { |
3423 | if (complain & tf_warning) | |
3424 | warning (0, "right shift count is negative"); | |
3425 | } | |
8d08fdba MS |
3426 | else |
3427 | { | |
5ade1ed2 DG |
3428 | if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0 |
3429 | && (complain & tf_warning)) | |
d4ee4d25 | 3430 | warning (0, "right shift count >= width of type"); |
8d08fdba MS |
3431 | } |
3432 | } | |
3433 | /* Convert the shift-count to an integer, regardless of | |
3434 | size of value being shifted. */ | |
3435 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
37c46b43 | 3436 | op1 = cp_convert (integer_type_node, op1); |
8d08fdba MS |
3437 | /* Avoid converting op1 to result_type later. */ |
3438 | converted = 1; | |
3439 | } | |
3440 | break; | |
3441 | ||
3442 | case LSHIFT_EXPR: | |
3443 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
3444 | { | |
3445 | result_type = type0; | |
3446 | if (TREE_CODE (op1) == INTEGER_CST) | |
3447 | { | |
3448 | if (tree_int_cst_lt (op1, integer_zero_node)) | |
5ade1ed2 DG |
3449 | { |
3450 | if (complain & tf_warning) | |
3451 | warning (0, "left shift count is negative"); | |
3452 | } | |
665f2503 | 3453 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
5ade1ed2 DG |
3454 | { |
3455 | if (complain & tf_warning) | |
3456 | warning (0, "left shift count >= width of type"); | |
3457 | } | |
8d08fdba MS |
3458 | } |
3459 | /* Convert the shift-count to an integer, regardless of | |
3460 | size of value being shifted. */ | |
3461 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
37c46b43 | 3462 | op1 = cp_convert (integer_type_node, op1); |
8d08fdba MS |
3463 | /* Avoid converting op1 to result_type later. */ |
3464 | converted = 1; | |
3465 | } | |
3466 | break; | |
3467 | ||
3468 | case RROTATE_EXPR: | |
3469 | case LROTATE_EXPR: | |
3470 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
3471 | { | |
3472 | result_type = type0; | |
3473 | if (TREE_CODE (op1) == INTEGER_CST) | |
3474 | { | |
3475 | if (tree_int_cst_lt (op1, integer_zero_node)) | |
5ade1ed2 DG |
3476 | { |
3477 | if (complain & tf_warning) | |
3478 | warning (0, (code == LROTATE_EXPR) | |
3479 | ? G_("left rotate count is negative") | |
3480 | : G_("right rotate count is negative")); | |
3481 | } | |
665f2503 | 3482 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
5ade1ed2 DG |
3483 | { |
3484 | if (complain & tf_warning) | |
3485 | warning (0, (code == LROTATE_EXPR) | |
3486 | ? G_("left rotate count >= width of type") | |
3487 | : G_("right rotate count >= width of type")); | |
3488 | } | |
8d08fdba MS |
3489 | } |
3490 | /* Convert the shift-count to an integer, regardless of | |
3491 | size of value being shifted. */ | |
3492 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
37c46b43 | 3493 | op1 = cp_convert (integer_type_node, op1); |
8d08fdba MS |
3494 | } |
3495 | break; | |
3496 | ||
3497 | case EQ_EXPR: | |
3498 | case NE_EXPR: | |
5ade1ed2 DG |
3499 | if ((complain & tf_warning) |
3500 | && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))) | |
3db45ab5 MS |
3501 | warning (OPT_Wfloat_equal, |
3502 | "comparing floating point with == or != is unsafe"); | |
5ade1ed2 DG |
3503 | if ((complain & tf_warning) |
3504 | && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1)) | |
3505 | || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0)))) | |
c116cd05 | 3506 | warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour"); |
1bdba2c0 | 3507 | |
c8094d83 | 3508 | build_type = boolean_type_node; |
37c46b43 MS |
3509 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
3510 | || code0 == COMPLEX_TYPE) | |
3511 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
3512 | || code1 == COMPLEX_TYPE)) | |
8d08fdba | 3513 | short_compare = 1; |
a5ac359a MM |
3514 | else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
3515 | || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1))) | |
708cae97 | 3516 | result_type = composite_pointer_type (type0, type1, op0, op1, |
5ade1ed2 | 3517 | "comparison", complain); |
a5ac359a MM |
3518 | else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0)) |
3519 | && null_ptr_cst_p (op1)) | |
b3c6d2ea ILT |
3520 | { |
3521 | if (TREE_CODE (op0) == ADDR_EXPR | |
3522 | && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) | |
5ade1ed2 DG |
3523 | { |
3524 | if (complain & tf_warning) | |
3525 | warning (OPT_Waddress, "the address of %qD will never be NULL", | |
3526 | TREE_OPERAND (op0, 0)); | |
3527 | } | |
b3c6d2ea ILT |
3528 | result_type = type0; |
3529 | } | |
a5ac359a MM |
3530 | else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1)) |
3531 | && null_ptr_cst_p (op0)) | |
b3c6d2ea ILT |
3532 | { |
3533 | if (TREE_CODE (op1) == ADDR_EXPR | |
3534 | && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) | |
5ade1ed2 DG |
3535 | { |
3536 | if (complain & tf_warning) | |
3537 | warning (OPT_Waddress, "the address of %qD will never be NULL", | |
3538 | TREE_OPERAND (op1, 0)); | |
3539 | } | |
b3c6d2ea ILT |
3540 | result_type = type1; |
3541 | } | |
8d08fdba MS |
3542 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
3543 | { | |
faae18ab | 3544 | result_type = type0; |
5ade1ed2 | 3545 | if (complain & tf_error) |
cbe5f3b3 | 3546 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
3547 | else |
3548 | return error_mark_node; | |
8d08fdba MS |
3549 | } |
3550 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
3551 | { | |
faae18ab | 3552 | result_type = type1; |
5ade1ed2 | 3553 | if (complain & tf_error) |
cbe5f3b3 | 3554 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
3555 | else |
3556 | return error_mark_node; | |
8d08fdba | 3557 | } |
a7a64a77 | 3558 | else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1)) |
8d08fdba | 3559 | { |
c3a88be8 RM |
3560 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION |
3561 | == ptrmemfunc_vbit_in_delta) | |
3562 | { | |
3563 | tree pfn0 = pfn_from_ptrmemfunc (op0); | |
a298680c | 3564 | tree delta0 = delta_from_ptrmemfunc (op0); |
ba47d38d AH |
3565 | tree e1 = cp_build_binary_op (location, |
3566 | EQ_EXPR, | |
c3a88be8 RM |
3567 | pfn0, |
3568 | fold_convert (TREE_TYPE (pfn0), | |
5ade1ed2 DG |
3569 | integer_zero_node), |
3570 | complain); | |
ba47d38d AH |
3571 | tree e2 = cp_build_binary_op (location, |
3572 | BIT_AND_EXPR, | |
c3a88be8 | 3573 | delta0, |
5ade1ed2 DG |
3574 | integer_one_node, |
3575 | complain); | |
ba47d38d AH |
3576 | e2 = cp_build_binary_op (location, |
3577 | EQ_EXPR, e2, integer_zero_node, | |
5ade1ed2 | 3578 | complain); |
ba47d38d AH |
3579 | op0 = cp_build_binary_op (location, |
3580 | TRUTH_ANDIF_EXPR, e1, e2, | |
5ade1ed2 | 3581 | complain); |
c3a88be8 RM |
3582 | op1 = cp_convert (TREE_TYPE (op0), integer_one_node); |
3583 | } | |
3584 | else | |
3585 | { | |
3586 | op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier); | |
3587 | op1 = cp_convert (TREE_TYPE (op0), integer_zero_node); | |
3588 | } | |
faae18ab | 3589 | result_type = TREE_TYPE (op0); |
8d08fdba | 3590 | } |
a7a64a77 | 3591 | else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0)) |
ba47d38d | 3592 | return cp_build_binary_op (location, code, op1, op0, complain); |
867f133e | 3593 | else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)) |
8d08fdba | 3594 | { |
867f133e | 3595 | tree type; |
c7e266a6 MM |
3596 | /* E will be the final comparison. */ |
3597 | tree e; | |
3598 | /* E1 and E2 are for scratch. */ | |
3599 | tree e1; | |
3600 | tree e2; | |
1f84ec23 MM |
3601 | tree pfn0; |
3602 | tree pfn1; | |
3603 | tree delta0; | |
3604 | tree delta1; | |
c7e266a6 | 3605 | |
5ade1ed2 DG |
3606 | type = composite_pointer_type (type0, type1, op0, op1, "comparison", |
3607 | complain); | |
867f133e OW |
3608 | |
3609 | if (!same_type_p (TREE_TYPE (op0), type)) | |
3610 | op0 = cp_convert_and_check (type, op0); | |
3611 | if (!same_type_p (TREE_TYPE (op1), type)) | |
3612 | op1 = cp_convert_and_check (type, op1); | |
3613 | ||
3614 | if (op0 == error_mark_node || op1 == error_mark_node) | |
3615 | return error_mark_node; | |
3616 | ||
ebb1abc3 JM |
3617 | if (TREE_SIDE_EFFECTS (op0)) |
3618 | op0 = save_expr (op0); | |
3619 | if (TREE_SIDE_EFFECTS (op1)) | |
3620 | op1 = save_expr (op1); | |
3621 | ||
1f84ec23 MM |
3622 | pfn0 = pfn_from_ptrmemfunc (op0); |
3623 | pfn1 = pfn_from_ptrmemfunc (op1); | |
a298680c OW |
3624 | delta0 = delta_from_ptrmemfunc (op0); |
3625 | delta1 = delta_from_ptrmemfunc (op1); | |
c3a88be8 RM |
3626 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION |
3627 | == ptrmemfunc_vbit_in_delta) | |
3628 | { | |
3629 | /* We generate: | |
3630 | ||
3631 | (op0.pfn == op1.pfn | |
3632 | && ((op0.delta == op1.delta) | |
3633 | || (!op0.pfn && op0.delta & 1 == 0 | |
3634 | && op1.delta & 1 == 0)) | |
3635 | ||
3636 | The reason for the `!op0.pfn' bit is that a NULL | |
3637 | pointer-to-member is any member with a zero PFN and | |
3638 | LSB of the DELTA field is 0. */ | |
3639 | ||
ba47d38d | 3640 | e1 = cp_build_binary_op (location, BIT_AND_EXPR, |
c3a88be8 | 3641 | delta0, |
5ade1ed2 DG |
3642 | integer_one_node, |
3643 | complain); | |
ba47d38d AH |
3644 | e1 = cp_build_binary_op (location, |
3645 | EQ_EXPR, e1, integer_zero_node, | |
5ade1ed2 | 3646 | complain); |
ba47d38d | 3647 | e2 = cp_build_binary_op (location, BIT_AND_EXPR, |
c3a88be8 | 3648 | delta1, |
5ade1ed2 DG |
3649 | integer_one_node, |
3650 | complain); | |
ba47d38d AH |
3651 | e2 = cp_build_binary_op (location, |
3652 | EQ_EXPR, e2, integer_zero_node, | |
5ade1ed2 | 3653 | complain); |
ba47d38d AH |
3654 | e1 = cp_build_binary_op (location, |
3655 | TRUTH_ANDIF_EXPR, e2, e1, | |
5ade1ed2 | 3656 | complain); |
ba47d38d | 3657 | e2 = cp_build_binary_op (location, EQ_EXPR, |
c3a88be8 RM |
3658 | pfn0, |
3659 | fold_convert (TREE_TYPE (pfn0), | |
5ade1ed2 DG |
3660 | integer_zero_node), |
3661 | complain); | |
ba47d38d AH |
3662 | e2 = cp_build_binary_op (location, |
3663 | TRUTH_ANDIF_EXPR, e2, e1, complain); | |
3664 | e1 = cp_build_binary_op (location, | |
3665 | EQ_EXPR, delta0, delta1, complain); | |
3666 | e1 = cp_build_binary_op (location, | |
3667 | TRUTH_ORIF_EXPR, e1, e2, complain); | |
c3a88be8 RM |
3668 | } |
3669 | else | |
3670 | { | |
3671 | /* We generate: | |
3672 | ||
3673 | (op0.pfn == op1.pfn | |
3674 | && (!op0.pfn || op0.delta == op1.delta)) | |
3675 | ||
3676 | The reason for the `!op0.pfn' bit is that a NULL | |
3677 | pointer-to-member is any member with a zero PFN; the | |
3678 | DELTA field is unspecified. */ | |
3679 | ||
ba47d38d AH |
3680 | e1 = cp_build_binary_op (location, |
3681 | EQ_EXPR, delta0, delta1, complain); | |
3682 | e2 = cp_build_binary_op (location, | |
3683 | EQ_EXPR, | |
c3a88be8 RM |
3684 | pfn0, |
3685 | fold_convert (TREE_TYPE (pfn0), | |
5ade1ed2 DG |
3686 | integer_zero_node), |
3687 | complain); | |
ba47d38d AH |
3688 | e1 = cp_build_binary_op (location, |
3689 | TRUTH_ORIF_EXPR, e1, e2, complain); | |
c3a88be8 | 3690 | } |
f293ce4b | 3691 | e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1); |
ba47d38d AH |
3692 | e = cp_build_binary_op (location, |
3693 | TRUTH_ANDIF_EXPR, e2, e1, complain); | |
8d08fdba | 3694 | if (code == EQ_EXPR) |
c7e266a6 | 3695 | return e; |
ba47d38d AH |
3696 | return cp_build_binary_op (location, |
3697 | EQ_EXPR, e, integer_zero_node, complain); | |
8d08fdba | 3698 | } |
315fb5db NS |
3699 | else |
3700 | { | |
3701 | gcc_assert (!TYPE_PTRMEMFUNC_P (type0) | |
3702 | || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), | |
3703 | type1)); | |
3704 | gcc_assert (!TYPE_PTRMEMFUNC_P (type1) | |
3705 | || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), | |
3706 | type0)); | |
3707 | } | |
c8094d83 | 3708 | |
8d08fdba MS |
3709 | break; |
3710 | ||
3711 | case MAX_EXPR: | |
3712 | case MIN_EXPR: | |
3713 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) | |
3714 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
3715 | shorten = 1; | |
3716 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
708cae97 | 3717 | result_type = composite_pointer_type (type0, type1, op0, op1, |
5ade1ed2 | 3718 | "comparison", complain); |
8d08fdba MS |
3719 | break; |
3720 | ||
3721 | case LE_EXPR: | |
3722 | case GE_EXPR: | |
3723 | case LT_EXPR: | |
3724 | case GT_EXPR: | |
eda0cd98 RS |
3725 | if (TREE_CODE (orig_op0) == STRING_CST |
3726 | || TREE_CODE (orig_op1) == STRING_CST) | |
5ade1ed2 DG |
3727 | { |
3728 | if (complain & tf_warning) | |
3729 | warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour"); | |
3730 | } | |
eda0cd98 | 3731 | |
28cbf42c | 3732 | build_type = boolean_type_node; |
8d08fdba MS |
3733 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) |
3734 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
3735 | short_compare = 1; | |
3736 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
708cae97 | 3737 | result_type = composite_pointer_type (type0, type1, op0, op1, |
5ade1ed2 | 3738 | "comparison", complain); |
8d08fdba MS |
3739 | else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST |
3740 | && integer_zerop (op1)) | |
faae18ab | 3741 | result_type = type0; |
8d08fdba MS |
3742 | else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST |
3743 | && integer_zerop (op0)) | |
faae18ab | 3744 | result_type = type1; |
8d08fdba MS |
3745 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
3746 | { | |
faae18ab | 3747 | result_type = type0; |
5ade1ed2 | 3748 | if (complain & tf_error) |
cbe5f3b3 | 3749 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
3750 | else |
3751 | return error_mark_node; | |
8d08fdba MS |
3752 | } |
3753 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
3754 | { | |
faae18ab | 3755 | result_type = type1; |
5ade1ed2 | 3756 | if (complain & tf_error) |
cbe5f3b3 | 3757 | permerror (input_location, "ISO C++ forbids comparison between pointer and integer"); |
5ade1ed2 DG |
3758 | else |
3759 | return error_mark_node; | |
8d08fdba | 3760 | } |
8d08fdba | 3761 | break; |
7f85441b | 3762 | |
1eb8759b RH |
3763 | case UNORDERED_EXPR: |
3764 | case ORDERED_EXPR: | |
3765 | case UNLT_EXPR: | |
3766 | case UNLE_EXPR: | |
3767 | case UNGT_EXPR: | |
3768 | case UNGE_EXPR: | |
3769 | case UNEQ_EXPR: | |
1eb8759b RH |
3770 | build_type = integer_type_node; |
3771 | if (code0 != REAL_TYPE || code1 != REAL_TYPE) | |
3772 | { | |
5ade1ed2 DG |
3773 | if (complain & tf_error) |
3774 | error ("unordered comparison on non-floating point argument"); | |
1eb8759b RH |
3775 | return error_mark_node; |
3776 | } | |
3777 | common = 1; | |
3778 | break; | |
3779 | ||
7f85441b KG |
3780 | default: |
3781 | break; | |
8d08fdba MS |
3782 | } |
3783 | ||
73042643 | 3784 | if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE) |
c8094d83 | 3785 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
73042643 AH |
3786 | || code1 == COMPLEX_TYPE))) |
3787 | arithmetic_types_p = 1; | |
3788 | else | |
3789 | { | |
3790 | arithmetic_types_p = 0; | |
3791 | /* Vector arithmetic is only allowed when both sides are vectors. */ | |
3792 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) | |
3793 | { | |
f6d7e7d8 AH |
3794 | if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) |
3795 | || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0), | |
3796 | TREE_TYPE (type1))) | |
3797 | { | |
ba47d38d | 3798 | binary_op_error (location, code, type0, type1); |
f6d7e7d8 AH |
3799 | return error_mark_node; |
3800 | } | |
73042643 AH |
3801 | arithmetic_types_p = 1; |
3802 | } | |
3803 | } | |
455f19cb MM |
3804 | /* Determine the RESULT_TYPE, if it is not already known. */ |
3805 | if (!result_type | |
c8094d83 | 3806 | && arithmetic_types_p |
455f19cb MM |
3807 | && (shorten || common || short_compare)) |
3808 | result_type = common_type (type0, type1); | |
3809 | ||
3810 | if (!result_type) | |
8d08fdba | 3811 | { |
5ade1ed2 DG |
3812 | if (complain & tf_error) |
3813 | error ("invalid operands of types %qT and %qT to binary %qO", | |
3814 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code); | |
455f19cb MM |
3815 | return error_mark_node; |
3816 | } | |
37c46b43 | 3817 | |
455f19cb MM |
3818 | /* If we're in a template, the only thing we need to know is the |
3819 | RESULT_TYPE. */ | |
3820 | if (processing_template_decl) | |
5be014d5 AP |
3821 | { |
3822 | /* Since the middle-end checks the type when doing a build2, we | |
3823 | need to build the tree in pieces. This built tree will never | |
3824 | get out of the front-end as we replace it when instantiating | |
3825 | the template. */ | |
3826 | tree tmp = build2 (resultcode, | |
3827 | build_type ? build_type : result_type, | |
3828 | NULL_TREE, op1); | |
3829 | TREE_OPERAND (tmp, 0) = op0; | |
3830 | return tmp; | |
3831 | } | |
455f19cb MM |
3832 | |
3833 | if (arithmetic_types_p) | |
3834 | { | |
3835 | int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE); | |
8d08fdba MS |
3836 | |
3837 | /* For certain operations (which identify themselves by shorten != 0) | |
3838 | if both args were extended from the same smaller type, | |
3839 | do the arithmetic in that type and then extend. | |
3840 | ||
3841 | shorten !=0 and !=1 indicates a bitwise operation. | |
3842 | For them, this optimization is safe only if | |
3843 | both args are zero-extended or both are sign-extended. | |
3844 | Otherwise, we might change the result. | |
39a13be5 | 3845 | E.g., (short)-1 | (unsigned short)-1 is (int)-1 |
8d08fdba MS |
3846 | but calculated in (unsigned short) it would be (unsigned short)-1. */ |
3847 | ||
37c46b43 | 3848 | if (shorten && none_complex) |
8d08fdba | 3849 | { |
8d08fdba | 3850 | final_type = result_type; |
6715192c MLI |
3851 | result_type = shorten_binary_op (result_type, op0, op1, |
3852 | shorten == -1); | |
8d08fdba MS |
3853 | } |
3854 | ||
8d08fdba MS |
3855 | /* Comparison operations are shortened too but differently. |
3856 | They identify themselves by setting short_compare = 1. */ | |
3857 | ||
3858 | if (short_compare) | |
3859 | { | |
3860 | /* Don't write &op0, etc., because that would prevent op0 | |
3861 | from being kept in a register. | |
3862 | Instead, make copies of the our local variables and | |
3863 | pass the copies by reference, then copy them back afterward. */ | |
3864 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
3865 | enum tree_code xresultcode = resultcode; | |
c8094d83 | 3866 | tree val |
8d08fdba MS |
3867 | = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); |
3868 | if (val != 0) | |
37c46b43 | 3869 | return cp_convert (boolean_type_node, val); |
28cbf42c MS |
3870 | op0 = xop0, op1 = xop1; |
3871 | converted = 1; | |
8d08fdba MS |
3872 | resultcode = xresultcode; |
3873 | } | |
3874 | ||
8d3b081e | 3875 | if ((short_compare || code == MIN_EXPR || code == MAX_EXPR) |
7d48af30 MM |
3876 | && warn_sign_compare |
3877 | /* Do not warn until the template is instantiated; we cannot | |
3878 | bound the ranges of the arguments until that point. */ | |
2d12797c MLI |
3879 | && !processing_template_decl |
3880 | && (complain & tf_warning)) | |
8d08fdba | 3881 | { |
ba47d38d AH |
3882 | warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1, |
3883 | result_type, resultcode); | |
8d08fdba MS |
3884 | } |
3885 | } | |
3886 | ||
0e339752 | 3887 | /* Issue warnings about peculiar, but valid, uses of NULL. */ |
b9edb4b1 MLI |
3888 | if ((orig_op0 == null_node || orig_op1 == null_node) |
3889 | /* It's reasonable to use pointer values as operands of && | |
03d0f4af | 3890 | and ||, so NULL is no exception. */ |
b9edb4b1 MLI |
3891 | && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR |
3892 | && ( /* Both are NULL (or 0) and the operation was not a comparison. */ | |
3893 | (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1) | |
3894 | && code != EQ_EXPR && code != NE_EXPR) | |
3895 | /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */ | |
3896 | || (!null_ptr_cst_p (orig_op0) && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE) | |
5ade1ed2 DG |
3897 | || (!null_ptr_cst_p (orig_op1) && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)) |
3898 | && (complain & tf_warning)) | |
e0f9a8bc MM |
3899 | /* Some sort of arithmetic operation involving NULL was |
3900 | performed. Note that pointer-difference and pointer-addition | |
3901 | have already been handled above, and so we don't end up here in | |
3902 | that case. */ | |
b9edb4b1 MLI |
3903 | warning (OPT_Wpointer_arith, "NULL used in arithmetic"); |
3904 | ||
e0f9a8bc | 3905 | |
b9edb4b1 MLI |
3906 | /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE. |
3907 | Then the expression will be built. | |
3908 | It will be given type FINAL_TYPE if that is nonzero; | |
3909 | otherwise, it will be given type RESULT_TYPE. */ | |
8d08fdba MS |
3910 | if (! converted) |
3911 | { | |
3912 | if (TREE_TYPE (op0) != result_type) | |
07231d4f | 3913 | op0 = cp_convert_and_check (result_type, op0); |
8d08fdba | 3914 | if (TREE_TYPE (op1) != result_type) |
07231d4f | 3915 | op1 = cp_convert_and_check (result_type, op1); |
848b92e1 JM |
3916 | |
3917 | if (op0 == error_mark_node || op1 == error_mark_node) | |
3918 | return error_mark_node; | |
8d08fdba MS |
3919 | } |
3920 | ||
28cbf42c MS |
3921 | if (build_type == NULL_TREE) |
3922 | build_type = result_type; | |
3923 | ||
455f19cb MM |
3924 | result = build2 (resultcode, build_type, op0, op1); |
3925 | result = fold_if_not_in_template (result); | |
3926 | if (final_type != 0) | |
3927 | result = cp_convert (final_type, result); | |
16fd4d2d MLI |
3928 | |
3929 | if (TREE_OVERFLOW_P (result) | |
3930 | && !TREE_OVERFLOW_P (op0) | |
3931 | && !TREE_OVERFLOW_P (op1)) | |
3932 | overflow_warning (result); | |
3933 | ||
455f19cb | 3934 | return result; |
8d08fdba MS |
3935 | } |
3936 | \f | |
3937 | /* Return a tree for the sum or difference (RESULTCODE says which) | |
3938 | of pointer PTROP and integer INTOP. */ | |
3939 | ||
3940 | static tree | |
926ce8bd | 3941 | cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop) |
8d08fdba | 3942 | { |
bfba94bd MM |
3943 | tree res_type = TREE_TYPE (ptrop); |
3944 | ||
3945 | /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type) | |
3946 | in certain circumstance (when it's valid to do so). So we need | |
3947 | to make sure it's complete. We don't need to check here, if we | |
3948 | can actually complete it at all, as those checks will be done in | |
3949 | pointer_int_sum() anyway. */ | |
3950 | complete_type (TREE_TYPE (res_type)); | |
8f259df3 | 3951 | |
455f19cb MM |
3952 | return pointer_int_sum (resultcode, ptrop, |
3953 | fold_if_not_in_template (intop)); | |
8d08fdba MS |
3954 | } |
3955 | ||
3956 | /* Return a tree for the difference of pointers OP0 and OP1. | |
3957 | The resulting tree has type int. */ | |
3958 | ||
3959 | static tree | |
926ce8bd | 3960 | pointer_diff (tree op0, tree op1, tree ptrtype) |
8d08fdba | 3961 | { |
6de9cd9a | 3962 | tree result; |
8d08fdba | 3963 | tree restype = ptrdiff_type_node; |
79a7c7fa | 3964 | tree target_type = TREE_TYPE (ptrtype); |
8d08fdba | 3965 | |
66543169 | 3966 | if (!complete_type_or_else (target_type, NULL_TREE)) |
8f259df3 MM |
3967 | return error_mark_node; |
3968 | ||
fcf73884 | 3969 | if (TREE_CODE (target_type) == VOID_TYPE) |
cbe5f3b3 | 3970 | permerror (input_location, "ISO C++ forbids using pointer of type %<void *%> in subtraction"); |
fcf73884 | 3971 | if (TREE_CODE (target_type) == FUNCTION_TYPE) |
cbe5f3b3 | 3972 | permerror (input_location, "ISO C++ forbids using pointer to a function in subtraction"); |
fcf73884 | 3973 | if (TREE_CODE (target_type) == METHOD_TYPE) |
cbe5f3b3 | 3974 | permerror (input_location, "ISO C++ forbids using pointer to a method in subtraction"); |
8d08fdba MS |
3975 | |
3976 | /* First do the subtraction as integers; | |
3977 | then drop through to build the divide operator. */ | |
3978 | ||
ba47d38d AH |
3979 | op0 = cp_build_binary_op (input_location, |
3980 | MINUS_EXPR, | |
ab76ca54 | 3981 | cp_convert (restype, op0), |
5ade1ed2 DG |
3982 | cp_convert (restype, op1), |
3983 | tf_warning_or_error); | |
8d08fdba MS |
3984 | |
3985 | /* This generates an error if op1 is a pointer to an incomplete type. */ | |
d0f062fb | 3986 | if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1)))) |
7e4d7898 | 3987 | error ("invalid use of a pointer to an incomplete type in pointer arithmetic"); |
8d08fdba | 3988 | |
c8094d83 | 3989 | op1 = (TYPE_PTROB_P (ptrtype) |
a5ac359a MM |
3990 | ? size_in_bytes (target_type) |
3991 | : integer_one_node); | |
8d08fdba MS |
3992 | |
3993 | /* Do the division. */ | |
3994 | ||
f293ce4b | 3995 | result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1)); |
455f19cb | 3996 | return fold_if_not_in_template (result); |
8d08fdba MS |
3997 | } |
3998 | \f | |
8d08fdba MS |
3999 | /* Construct and perhaps optimize a tree representation |
4000 | for a unary operation. CODE, a tree_code, specifies the operation | |
4001 | and XARG is the operand. */ | |
4002 | ||
4003 | tree | |
5ade1ed2 | 4004 | build_x_unary_op (enum tree_code code, tree xarg, tsubst_flags_t complain) |
8d08fdba | 4005 | { |
d17811fd | 4006 | tree orig_expr = xarg; |
19420d00 NS |
4007 | tree exp; |
4008 | int ptrmem = 0; | |
c8094d83 | 4009 | |
5156628f | 4010 | if (processing_template_decl) |
d17811fd MM |
4011 | { |
4012 | if (type_dependent_expression_p (xarg)) | |
4013 | return build_min_nt (code, xarg, NULL_TREE); | |
3601f003 | 4014 | |
d17811fd MM |
4015 | xarg = build_non_dependent_expr (xarg); |
4016 | } | |
4017 | ||
4018 | exp = NULL_TREE; | |
5566b478 | 4019 | |
1e2e9f54 MM |
4020 | /* [expr.unary.op] says: |
4021 | ||
4022 | The address of an object of incomplete type can be taken. | |
4023 | ||
4024 | (And is just the ordinary address operator, not an overloaded | |
4025 | "operator &".) However, if the type is a template | |
4026 | specialization, we must complete the type at this point so that | |
4027 | an overloaded "operator &" will be available if required. */ | |
db5ae43f | 4028 | if (code == ADDR_EXPR |
386b8a85 | 4029 | && TREE_CODE (xarg) != TEMPLATE_ID_EXPR |
1e2e9f54 MM |
4030 | && ((CLASS_TYPE_P (TREE_TYPE (xarg)) |
4031 | && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg)))) | |
db5ae43f | 4032 | || (TREE_CODE (xarg) == OFFSET_REF))) |
f4f206f4 | 4033 | /* Don't look for a function. */; |
db5ae43f | 4034 | else |
ec835fb2 | 4035 | exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE, |
5ade1ed2 | 4036 | /*overloaded_p=*/NULL, complain); |
d17811fd | 4037 | if (!exp && code == ADDR_EXPR) |
c91a56d2 | 4038 | { |
1e14c7f0 GDR |
4039 | /* A pointer to member-function can be formed only by saying |
4040 | &X::mf. */ | |
4041 | if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE | |
4042 | && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg))) | |
4043 | { | |
5ebbc0ee NS |
4044 | if (TREE_CODE (xarg) != OFFSET_REF |
4045 | || !TYPE_P (TREE_OPERAND (xarg, 0))) | |
1e14c7f0 | 4046 | { |
5ade1ed2 DG |
4047 | error ("invalid use of %qE to form a pointer-to-member-function", |
4048 | xarg); | |
4049 | if (TREE_CODE (xarg) != OFFSET_REF) | |
1f5b3869 | 4050 | inform (input_location, " a qualified-id is required"); |
1e14c7f0 GDR |
4051 | return error_mark_node; |
4052 | } | |
4053 | else | |
4054 | { | |
719fbd99 | 4055 | error ("parentheses around %qE cannot be used to form a" |
0cbd7506 | 4056 | " pointer-to-member-function", |
1e14c7f0 GDR |
4057 | xarg); |
4058 | PTRMEM_OK_P (xarg) = 1; | |
4059 | } | |
4060 | } | |
c8094d83 | 4061 | |
19420d00 | 4062 | if (TREE_CODE (xarg) == OFFSET_REF) |
0cbd7506 MS |
4063 | { |
4064 | ptrmem = PTRMEM_OK_P (xarg); | |
c8094d83 | 4065 | |
0cbd7506 MS |
4066 | if (!ptrmem && !flag_ms_extensions |
4067 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE) | |
4542128e NS |
4068 | { |
4069 | /* A single non-static member, make sure we don't allow a | |
0cbd7506 | 4070 | pointer-to-member. */ |
f293ce4b RS |
4071 | xarg = build2 (OFFSET_REF, TREE_TYPE (xarg), |
4072 | TREE_OPERAND (xarg, 0), | |
4073 | ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE)); | |
4542128e | 4074 | PTRMEM_OK_P (xarg) = ptrmem; |
c8094d83 | 4075 | } |
0cbd7506 | 4076 | } |
5ade1ed2 | 4077 | else if (TREE_CODE (xarg) == TARGET_EXPR && (complain & tf_warning)) |
d4ee4d25 | 4078 | warning (0, "taking address of temporary"); |
5ade1ed2 | 4079 | exp = cp_build_unary_op (ADDR_EXPR, xarg, 0, complain); |
c91a56d2 MS |
4080 | } |
4081 | ||
d17811fd | 4082 | if (processing_template_decl && exp != error_mark_node) |
6439fffd KL |
4083 | exp = build_min_non_dep (code, exp, orig_expr, |
4084 | /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE); | |
4085 | if (TREE_CODE (exp) == ADDR_EXPR) | |
4086 | PTRMEM_OK_P (exp) = ptrmem; | |
19420d00 | 4087 | return exp; |
8d08fdba MS |
4088 | } |
4089 | ||
78ef5b89 NB |
4090 | /* Like c_common_truthvalue_conversion, but handle pointer-to-member |
4091 | constants, where a null value is represented by an INTEGER_CST of | |
4092 | -1. */ | |
96d6c610 JM |
4093 | |
4094 | tree | |
5671bf27 | 4095 | cp_truthvalue_conversion (tree expr) |
96d6c610 JM |
4096 | { |
4097 | tree type = TREE_TYPE (expr); | |
4098 | if (TYPE_PTRMEM_P (type)) | |
ba47d38d AH |
4099 | return build_binary_op (EXPR_LOCATION (expr), |
4100 | NE_EXPR, expr, integer_zero_node, 1); | |
96d6c610 | 4101 | else |
ba47d38d | 4102 | return c_common_truthvalue_conversion (input_location, expr); |
96d6c610 JM |
4103 | } |
4104 | ||
4105 | /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */ | |
c8094d83 | 4106 | |
2986ae00 | 4107 | tree |
5671bf27 | 4108 | condition_conversion (tree expr) |
2986ae00 | 4109 | { |
5566b478 | 4110 | tree t; |
5156628f | 4111 | if (processing_template_decl) |
5566b478 | 4112 | return expr; |
5ade1ed2 DG |
4113 | t = perform_implicit_conversion (boolean_type_node, expr, |
4114 | tf_warning_or_error); | |
0ad28dde | 4115 | t = fold_build_cleanup_point_expr (boolean_type_node, t); |
8ccc31eb | 4116 | return t; |
2986ae00 | 4117 | } |
c8094d83 | 4118 | |
7993382e MM |
4119 | /* Return an ADDR_EXPR giving the address of T. This function |
4120 | attempts no optimizations or simplifications; it is a low-level | |
4121 | primitive. */ | |
4122 | ||
4123 | tree | |
4124 | build_address (tree t) | |
4125 | { | |
4126 | tree addr; | |
4127 | ||
4128 | if (error_operand_p (t) || !cxx_mark_addressable (t)) | |
4129 | return error_mark_node; | |
4130 | ||
92af500d | 4131 | addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t); |
7993382e MM |
4132 | |
4133 | return addr; | |
4134 | } | |
4135 | ||
4136 | /* Return a NOP_EXPR converting EXPR to TYPE. */ | |
4137 | ||
4138 | tree | |
4139 | build_nop (tree type, tree expr) | |
4140 | { | |
7993382e MM |
4141 | if (type == error_mark_node || error_operand_p (expr)) |
4142 | return expr; | |
6de9cd9a | 4143 | return build1 (NOP_EXPR, type, expr); |
7993382e MM |
4144 | } |
4145 | ||
8d08fdba MS |
4146 | /* C++: Must handle pointers to members. |
4147 | ||
4148 | Perhaps type instantiation should be extended to handle conversion | |
4149 | from aggregates to types we don't yet know we want? (Or are those | |
4150 | cases typically errors which should be reported?) | |
4151 | ||
4152 | NOCONVERT nonzero suppresses the default promotions | |
4153 | (such as from short to int). */ | |
e92cc029 | 4154 | |
8d08fdba | 4155 | tree |
5ade1ed2 DG |
4156 | cp_build_unary_op (enum tree_code code, tree xarg, int noconvert, |
4157 | tsubst_flags_t complain) | |
8d08fdba MS |
4158 | { |
4159 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
926ce8bd KH |
4160 | tree arg = xarg; |
4161 | tree argtype = 0; | |
d8e178a0 | 4162 | const char *errstring = NULL; |
8d08fdba | 4163 | tree val; |
4de67c26 | 4164 | const char *invalid_op_diag; |
8d08fdba | 4165 | |
b7484fbe | 4166 | if (arg == error_mark_node) |
8d08fdba MS |
4167 | return error_mark_node; |
4168 | ||
4de67c26 JM |
4169 | if ((invalid_op_diag |
4170 | = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR | |
4171 | ? CONVERT_EXPR | |
4172 | : code), | |
4173 | TREE_TYPE (xarg)))) | |
4174 | { | |
4175 | error (invalid_op_diag); | |
4176 | return error_mark_node; | |
4177 | } | |
4178 | ||
8d08fdba MS |
4179 | switch (code) |
4180 | { | |
392e3d51 | 4181 | case UNARY_PLUS_EXPR: |
b7484fbe | 4182 | case NEGATE_EXPR: |
8a21aa30 KC |
4183 | { |
4184 | int flags = WANT_ARITH | WANT_ENUM; | |
4185 | /* Unary plus (but not unary minus) is allowed on pointers. */ | |
392e3d51 | 4186 | if (code == UNARY_PLUS_EXPR) |
8a21aa30 KC |
4187 | flags |= WANT_POINTER; |
4188 | arg = build_expr_type_conversion (flags, arg, true); | |
e99f332f GB |
4189 | if (!arg) |
4190 | errstring = (code == NEGATE_EXPR | |
4191 | ? "wrong type argument to unary minus" | |
4192 | : "wrong type argument to unary plus"); | |
4193 | else | |
4194 | { | |
4195 | if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg))) | |
4196 | arg = perform_integral_promotions (arg); | |
4197 | ||
13a44ee0 | 4198 | /* Make sure the result is not an lvalue: a unary plus or minus |
e99f332f | 4199 | expression is always a rvalue. */ |
5cc53d4e | 4200 | arg = rvalue (arg); |
e99f332f GB |
4201 | } |
4202 | } | |
8d08fdba MS |
4203 | break; |
4204 | ||
4205 | case BIT_NOT_EXPR: | |
37c46b43 MS |
4206 | if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) |
4207 | { | |
4208 | code = CONJ_EXPR; | |
4209 | if (!noconvert) | |
4210 | arg = default_conversion (arg); | |
4211 | } | |
4576ceaf JJ |
4212 | else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM |
4213 | | WANT_VECTOR, | |
b746c5dc | 4214 | arg, true))) |
b7484fbe | 4215 | errstring = "wrong type argument to bit-complement"; |
bd289f54 | 4216 | else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg))) |
0a72704b | 4217 | arg = perform_integral_promotions (arg); |
8d08fdba MS |
4218 | break; |
4219 | ||
4220 | case ABS_EXPR: | |
b746c5dc | 4221 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) |
b7484fbe | 4222 | errstring = "wrong type argument to abs"; |
8d08fdba MS |
4223 | else if (!noconvert) |
4224 | arg = default_conversion (arg); | |
4225 | break; | |
4226 | ||
37c46b43 MS |
4227 | case CONJ_EXPR: |
4228 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
b746c5dc | 4229 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) |
37c46b43 MS |
4230 | errstring = "wrong type argument to conjugation"; |
4231 | else if (!noconvert) | |
4232 | arg = default_conversion (arg); | |
4233 | break; | |
4234 | ||
8d08fdba | 4235 | case TRUTH_NOT_EXPR: |
5ade1ed2 DG |
4236 | arg = perform_implicit_conversion (boolean_type_node, arg, |
4237 | complain); | |
8d08fdba | 4238 | val = invert_truthvalue (arg); |
2986ae00 MS |
4239 | if (arg != error_mark_node) |
4240 | return val; | |
4241 | errstring = "in argument to unary !"; | |
8d08fdba MS |
4242 | break; |
4243 | ||
4244 | case NOP_EXPR: | |
4245 | break; | |
c8094d83 | 4246 | |
37c46b43 MS |
4247 | case REALPART_EXPR: |
4248 | if (TREE_CODE (arg) == COMPLEX_CST) | |
4249 | return TREE_REALPART (arg); | |
4250 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
455f19cb MM |
4251 | { |
4252 | arg = build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg); | |
4253 | return fold_if_not_in_template (arg); | |
4254 | } | |
37c46b43 MS |
4255 | else |
4256 | return arg; | |
4257 | ||
4258 | case IMAGPART_EXPR: | |
4259 | if (TREE_CODE (arg) == COMPLEX_CST) | |
4260 | return TREE_IMAGPART (arg); | |
4261 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
455f19cb MM |
4262 | { |
4263 | arg = build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg); | |
4264 | return fold_if_not_in_template (arg); | |
4265 | } | |
37c46b43 MS |
4266 | else |
4267 | return cp_convert (TREE_TYPE (arg), integer_zero_node); | |
c8094d83 | 4268 | |
8d08fdba MS |
4269 | case PREINCREMENT_EXPR: |
4270 | case POSTINCREMENT_EXPR: | |
4271 | case PREDECREMENT_EXPR: | |
4272 | case POSTDECREMENT_EXPR: | |
4273 | /* Handle complex lvalues (when permitted) | |
4274 | by reduction to simpler cases. */ | |
4275 | ||
4276 | val = unary_complex_lvalue (code, arg); | |
4277 | if (val != 0) | |
4278 | return val; | |
4279 | ||
37c46b43 MS |
4280 | /* Increment or decrement the real part of the value, |
4281 | and don't change the imaginary part. */ | |
4282 | if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
4283 | { | |
4284 | tree real, imag; | |
4285 | ||
4286 | arg = stabilize_reference (arg); | |
5ade1ed2 DG |
4287 | real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain); |
4288 | imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain); | |
4289 | real = cp_build_unary_op (code, real, 1, complain); | |
a87db577 AP |
4290 | if (real == error_mark_node || imag == error_mark_node) |
4291 | return error_mark_node; | |
f293ce4b | 4292 | return build2 (COMPLEX_EXPR, TREE_TYPE (arg), |
a87db577 | 4293 | real, imag); |
37c46b43 MS |
4294 | } |
4295 | ||
8d08fdba MS |
4296 | /* Report invalid types. */ |
4297 | ||
b7484fbe | 4298 | if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER, |
b746c5dc | 4299 | arg, true))) |
8d08fdba MS |
4300 | { |
4301 | if (code == PREINCREMENT_EXPR) | |
4302 | errstring ="no pre-increment operator for type"; | |
4303 | else if (code == POSTINCREMENT_EXPR) | |
4304 | errstring ="no post-increment operator for type"; | |
4305 | else if (code == PREDECREMENT_EXPR) | |
4306 | errstring ="no pre-decrement operator for type"; | |
4307 | else | |
4308 | errstring ="no post-decrement operator for type"; | |
4309 | break; | |
4310 | } | |
affb3cb2 JJ |
4311 | else if (arg == error_mark_node) |
4312 | return error_mark_node; | |
8d08fdba MS |
4313 | |
4314 | /* Report something read-only. */ | |
4315 | ||
91063b51 | 4316 | if (CP_TYPE_CONST_P (TREE_TYPE (arg)) |
5ade1ed2 DG |
4317 | || TREE_READONLY (arg)) |
4318 | { | |
4319 | if (complain & tf_error) | |
4320 | readonly_error (arg, ((code == PREINCREMENT_EXPR | |
4321 | || code == POSTINCREMENT_EXPR) | |
4322 | ? "increment" : "decrement")); | |
4323 | else | |
4324 | return error_mark_node; | |
4325 | } | |
8d08fdba MS |
4326 | |
4327 | { | |
926ce8bd | 4328 | tree inc; |
f9aa54d3 | 4329 | tree declared_type; |
8d08fdba MS |
4330 | tree result_type = TREE_TYPE (arg); |
4331 | ||
f9aa54d3 MM |
4332 | declared_type = unlowered_expr_type (arg); |
4333 | ||
8d08fdba MS |
4334 | arg = get_unwidened (arg, 0); |
4335 | argtype = TREE_TYPE (arg); | |
4336 | ||
4337 | /* ARM $5.2.5 last annotation says this should be forbidden. */ | |
4338 | if (TREE_CODE (argtype) == ENUMERAL_TYPE) | |
5ade1ed2 DG |
4339 | { |
4340 | if (complain & tf_error) | |
cbe5f3b3 | 4341 | permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
37ec60ed JW |
4342 | ? G_("ISO C++ forbids incrementing an enum") |
4343 | : G_("ISO C++ forbids decrementing an enum")); | |
5ade1ed2 DG |
4344 | else |
4345 | return error_mark_node; | |
4346 | } | |
c8094d83 | 4347 | |
8d08fdba MS |
4348 | /* Compute the increment. */ |
4349 | ||
b7484fbe | 4350 | if (TREE_CODE (argtype) == POINTER_TYPE) |
8d08fdba | 4351 | { |
d0f062fb | 4352 | tree type = complete_type (TREE_TYPE (argtype)); |
c8094d83 | 4353 | |
d0f062fb | 4354 | if (!COMPLETE_OR_VOID_TYPE_P (type)) |
5ade1ed2 DG |
4355 | { |
4356 | if (complain & tf_error) | |
4357 | error (((code == PREINCREMENT_EXPR | |
4358 | || code == POSTINCREMENT_EXPR)) | |
4359 | ? G_("cannot increment a pointer to incomplete type %qT") | |
4360 | : G_("cannot decrement a pointer to incomplete type %qT"), | |
4361 | TREE_TYPE (argtype)); | |
4362 | else | |
4363 | return error_mark_node; | |
4364 | } | |
e62d5b58 | 4365 | else if ((pedantic || warn_pointer_arith) |
5ade1ed2 DG |
4366 | && !TYPE_PTROB_P (argtype)) |
4367 | { | |
4368 | if (complain & tf_error) | |
cbe5f3b3 | 4369 | permerror (input_location, (code == PREINCREMENT_EXPR |
37ec60ed JW |
4370 | || code == POSTINCREMENT_EXPR) |
4371 | ? G_("ISO C++ forbids incrementing a pointer of type %qT") | |
4372 | : G_("ISO C++ forbids decrementing a pointer of type %qT"), | |
4373 | argtype); | |
5ade1ed2 DG |
4374 | else |
4375 | return error_mark_node; | |
4376 | } | |
4377 | ||
ea793912 | 4378 | inc = cxx_sizeof_nowarn (TREE_TYPE (argtype)); |
8d08fdba MS |
4379 | } |
4380 | else | |
4381 | inc = integer_one_node; | |
4382 | ||
37c46b43 | 4383 | inc = cp_convert (argtype, inc); |
8d08fdba | 4384 | |
8d08fdba MS |
4385 | /* Complain about anything else that is not a true lvalue. */ |
4386 | if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR | |
4387 | || code == POSTINCREMENT_EXPR) | |
5ade1ed2 DG |
4388 | ? lv_increment : lv_decrement), |
4389 | complain)) | |
8d08fdba MS |
4390 | return error_mark_node; |
4391 | ||
b7484fbe | 4392 | /* Forbid using -- on `bool'. */ |
f9aa54d3 | 4393 | if (same_type_p (declared_type, boolean_type_node)) |
b7484fbe MS |
4394 | { |
4395 | if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR) | |
4396 | { | |
5ade1ed2 DG |
4397 | if (complain & tf_error) |
4398 | error ("invalid use of Boolean expression as operand " | |
4399 | "to %<operator--%>"); | |
b7484fbe MS |
4400 | return error_mark_node; |
4401 | } | |
19552aa5 | 4402 | val = boolean_increment (code, arg); |
b7484fbe MS |
4403 | } |
4404 | else | |
f293ce4b | 4405 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
b7484fbe | 4406 | |
8d08fdba | 4407 | TREE_SIDE_EFFECTS (val) = 1; |
37c46b43 | 4408 | return cp_convert (result_type, val); |
8d08fdba MS |
4409 | } |
4410 | ||
4411 | case ADDR_EXPR: | |
4412 | /* Note that this operation never does default_conversion | |
4413 | regardless of NOCONVERT. */ | |
4414 | ||
8cd4c175 | 4415 | argtype = lvalue_type (arg); |
a5ac359a MM |
4416 | |
4417 | if (TREE_CODE (arg) == OFFSET_REF) | |
4418 | goto offset_ref; | |
4419 | ||
b7484fbe | 4420 | if (TREE_CODE (argtype) == REFERENCE_TYPE) |
8d08fdba | 4421 | { |
6de9cd9a DN |
4422 | tree type = build_pointer_type (TREE_TYPE (argtype)); |
4423 | arg = build1 (CONVERT_EXPR, type, arg); | |
8d08fdba MS |
4424 | return arg; |
4425 | } | |
fcf73884 | 4426 | else if (DECL_MAIN_P (arg)) |
5ade1ed2 DG |
4427 | { |
4428 | /* ARM $3.4 */ | |
4429 | if (complain & tf_error) | |
cbe5f3b3 | 4430 | permerror (input_location, "ISO C++ forbids taking address of function %<::main%>"); |
5ade1ed2 DG |
4431 | else |
4432 | return error_mark_node; | |
4433 | } | |
8d08fdba MS |
4434 | |
4435 | /* Let &* cancel out to simplify resulting code. */ | |
4436 | if (TREE_CODE (arg) == INDIRECT_REF) | |
4437 | { | |
4ac14744 | 4438 | /* We don't need to have `current_class_ptr' wrapped in a |
8d08fdba | 4439 | NON_LVALUE_EXPR node. */ |
4ac14744 MS |
4440 | if (arg == current_class_ref) |
4441 | return current_class_ptr; | |
8d08fdba | 4442 | |
8d08fdba MS |
4443 | arg = TREE_OPERAND (arg, 0); |
4444 | if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE) | |
4445 | { | |
6de9cd9a DN |
4446 | tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg))); |
4447 | arg = build1 (CONVERT_EXPR, type, arg); | |
8d08fdba | 4448 | } |
5cc53d4e | 4449 | else |
8d08fdba | 4450 | /* Don't let this be an lvalue. */ |
5cc53d4e | 4451 | arg = rvalue (arg); |
8d08fdba MS |
4452 | return arg; |
4453 | } | |
4454 | ||
8d08fdba MS |
4455 | /* Uninstantiated types are all functions. Taking the |
4456 | address of a function is a no-op, so just return the | |
4457 | argument. */ | |
4458 | ||
315fb5db NS |
4459 | gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE |
4460 | || !IDENTIFIER_OPNAME_P (arg)); | |
8d08fdba | 4461 | |
96d6c610 | 4462 | if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg) |
50ad9642 | 4463 | && !really_overloaded_fn (TREE_OPERAND (arg, 1))) |
0cbd7506 | 4464 | { |
fbf6f1ba | 4465 | /* They're trying to take the address of a unique non-static |
96d6c610 JM |
4466 | member function. This is ill-formed (except in MS-land), |
4467 | but let's try to DTRT. | |
4468 | Note: We only handle unique functions here because we don't | |
4469 | want to complain if there's a static overload; non-unique | |
4470 | cases will be handled by instantiate_type. But we need to | |
4471 | handle this case here to allow casts on the resulting PMF. | |
4472 | We could defer this in non-MS mode, but it's easier to give | |
4473 | a useful error here. */ | |
fbf6f1ba | 4474 | |
d219f3ff KL |
4475 | /* Inside constant member functions, the `this' pointer |
4476 | contains an extra const qualifier. TYPE_MAIN_VARIANT | |
4477 | is used here to remove this const from the diagnostics | |
4478 | and the created OFFSET_REF. */ | |
4479 | tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0))); | |
d4f0f205 | 4480 | tree fn = get_first_fn (TREE_OPERAND (arg, 1)); |
5c147e22 | 4481 | mark_used (fn); |
96d6c610 JM |
4482 | |
4483 | if (! flag_ms_extensions) | |
4484 | { | |
d4f0f205 | 4485 | tree name = DECL_NAME (fn); |
5ade1ed2 DG |
4486 | if (!(complain & tf_error)) |
4487 | return error_mark_node; | |
4488 | else if (current_class_type | |
4489 | && TREE_OPERAND (arg, 0) == current_class_ref) | |
4490 | /* An expression like &memfn. */ | |
cbe5f3b3 | 4491 | permerror (input_location, "ISO C++ forbids taking the address of an unqualified" |
37ec60ed JW |
4492 | " or parenthesized non-static member function to form" |
4493 | " a pointer to member function. Say %<&%T::%D%>", | |
4494 | base, name); | |
96d6c610 | 4495 | else |
cbe5f3b3 | 4496 | permerror (input_location, "ISO C++ forbids taking the address of a bound member" |
37ec60ed JW |
4497 | " function to form a pointer to member function." |
4498 | " Say %<&%T::%D%>", | |
4499 | base, name); | |
96d6c610 | 4500 | } |
d4f0f205 | 4501 | arg = build_offset_ref (base, fn, /*address_p=*/true); |
0cbd7506 | 4502 | } |
a5ac359a | 4503 | |
c8094d83 | 4504 | offset_ref: |
03017874 | 4505 | if (type_unknown_p (arg)) |
51924768 | 4506 | return build1 (ADDR_EXPR, unknown_type_node, arg); |
c8094d83 | 4507 | |
8d08fdba MS |
4508 | /* Handle complex lvalues (when permitted) |
4509 | by reduction to simpler cases. */ | |
4510 | val = unary_complex_lvalue (code, arg); | |
4511 | if (val != 0) | |
4512 | return val; | |
4513 | ||
8d08fdba MS |
4514 | switch (TREE_CODE (arg)) |
4515 | { | |
5f9e56b3 | 4516 | CASE_CONVERT: |
8d08fdba MS |
4517 | case FLOAT_EXPR: |
4518 | case FIX_TRUNC_EXPR: | |
5ade1ed2 DG |
4519 | /* Even if we're not being pedantic, we cannot allow this |
4520 | extension when we're instantiating in a SFINAE | |
4521 | context. */ | |
fcf73884 | 4522 | if (! lvalue_p (arg) && complain == tf_none) |
5ade1ed2 DG |
4523 | { |
4524 | if (complain & tf_error) | |
cbe5f3b3 | 4525 | permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression"); |
5ade1ed2 DG |
4526 | else |
4527 | return error_mark_node; | |
4528 | } | |
7f85441b | 4529 | break; |
26bcf8fc | 4530 | |
b76d34e9 MM |
4531 | case BASELINK: |
4532 | arg = BASELINK_FUNCTIONS (arg); | |
4533 | /* Fall through. */ | |
4534 | ||
26bcf8fc MM |
4535 | case OVERLOAD: |
4536 | arg = OVL_CURRENT (arg); | |
4537 | break; | |
4538 | ||
6d05585b MM |
4539 | case OFFSET_REF: |
4540 | /* Turn a reference to a non-static data member into a | |
4541 | pointer-to-member. */ | |
4542 | { | |
4543 | tree type; | |
4544 | tree t; | |
4545 | ||
4546 | if (!PTRMEM_OK_P (arg)) | |
5ade1ed2 | 4547 | return cp_build_unary_op (code, arg, 0, complain); |
c8094d83 | 4548 | |
6d05585b MM |
4549 | t = TREE_OPERAND (arg, 1); |
4550 | if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE) | |
4551 | { | |
5ade1ed2 DG |
4552 | if (complain & tf_error) |
4553 | error ("cannot create pointer to reference member %qD", t); | |
6d05585b MM |
4554 | return error_mark_node; |
4555 | } | |
c8094d83 MS |
4556 | |
4557 | type = build_ptrmem_type (context_for_name_lookup (t), | |
6d05585b MM |
4558 | TREE_TYPE (t)); |
4559 | t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1)); | |
4560 | return t; | |
4561 | } | |
4562 | ||
7f85441b KG |
4563 | default: |
4564 | break; | |
8d08fdba | 4565 | } |
8d08fdba | 4566 | |
8d08fdba MS |
4567 | /* Anything not already handled and not a true memory reference |
4568 | is an error. */ | |
3b2db49f MM |
4569 | if (TREE_CODE (argtype) != FUNCTION_TYPE |
4570 | && TREE_CODE (argtype) != METHOD_TYPE | |
4571 | && TREE_CODE (arg) != OFFSET_REF | |
5ade1ed2 | 4572 | && !lvalue_or_else (arg, lv_addressof, complain)) |
8d08fdba MS |
4573 | return error_mark_node; |
4574 | ||
01240200 MM |
4575 | if (argtype != error_mark_node) |
4576 | argtype = build_pointer_type (argtype); | |
8d08fdba | 4577 | |
3b2db49f MM |
4578 | /* In a template, we are processing a non-dependent expression |
4579 | so we can just form an ADDR_EXPR with the correct type. */ | |
0f67bdf1 | 4580 | if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF) |
3b2db49f MM |
4581 | { |
4582 | val = build_address (arg); | |
4583 | if (TREE_CODE (arg) == OFFSET_REF) | |
4584 | PTRMEM_OK_P (val) = PTRMEM_OK_P (arg); | |
4585 | } | |
4586 | else if (TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK) | |
4587 | { | |
4588 | tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1)); | |
4589 | ||
4590 | /* We can only get here with a single static member | |
4591 | function. */ | |
4592 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL | |
4593 | && DECL_STATIC_FUNCTION_P (fn)); | |
4594 | mark_used (fn); | |
4595 | val = build_address (fn); | |
4596 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0))) | |
4597 | /* Do not lose object's side effects. */ | |
4598 | val = build2 (COMPOUND_EXPR, TREE_TYPE (val), | |
4599 | TREE_OPERAND (arg, 0), val); | |
4600 | } | |
4601 | else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))) | |
4602 | { | |
5ade1ed2 DG |
4603 | if (complain & tf_error) |
4604 | error ("attempt to take address of bit-field structure member %qD", | |
4605 | TREE_OPERAND (arg, 1)); | |
3b2db49f MM |
4606 | return error_mark_node; |
4607 | } | |
4608 | else | |
4609 | { | |
4610 | tree object = TREE_OPERAND (arg, 0); | |
4611 | tree field = TREE_OPERAND (arg, 1); | |
4612 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4613 | (TREE_TYPE (object), decl_type_context (field))); | |
4614 | val = build_address (arg); | |
4615 | } | |
4ac14744 | 4616 | |
3b2db49f MM |
4617 | if (TREE_CODE (argtype) == POINTER_TYPE |
4618 | && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE) | |
4619 | { | |
4620 | build_ptrmemfunc_type (argtype); | |
4621 | val = build_ptrmemfunc (argtype, val, 0, | |
4622 | /*c_cast_p=*/false); | |
4623 | } | |
4ac14744 | 4624 | |
3b2db49f | 4625 | return val; |
7f85441b KG |
4626 | |
4627 | default: | |
4628 | break; | |
8d08fdba MS |
4629 | } |
4630 | ||
4631 | if (!errstring) | |
4632 | { | |
4633 | if (argtype == 0) | |
4634 | argtype = TREE_TYPE (arg); | |
455f19cb | 4635 | return fold_if_not_in_template (build1 (code, argtype, arg)); |
8d08fdba MS |
4636 | } |
4637 | ||
5ade1ed2 DG |
4638 | if (complain & tf_error) |
4639 | error ("%s", errstring); | |
8d08fdba MS |
4640 | return error_mark_node; |
4641 | } | |
4642 | ||
5ade1ed2 DG |
4643 | /* Hook for the c-common bits that build a unary op. */ |
4644 | tree | |
c9f9eb5d AH |
4645 | build_unary_op (location_t location ATTRIBUTE_UNUSED, |
4646 | enum tree_code code, tree xarg, int noconvert) | |
5ade1ed2 DG |
4647 | { |
4648 | return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error); | |
4649 | } | |
4650 | ||
8d08fdba MS |
4651 | /* Apply unary lvalue-demanding operator CODE to the expression ARG |
4652 | for certain kinds of expressions which are not really lvalues | |
4653 | but which we can accept as lvalues. | |
4654 | ||
dfb5c523 MM |
4655 | If ARG is not a kind of expression we can handle, return |
4656 | NULL_TREE. */ | |
c8094d83 | 4657 | |
8d08fdba | 4658 | tree |
acd8e2d0 | 4659 | unary_complex_lvalue (enum tree_code code, tree arg) |
8d08fdba | 4660 | { |
dfb5c523 MM |
4661 | /* Inside a template, making these kinds of adjustments is |
4662 | pointless; we are only concerned with the type of the | |
4663 | expression. */ | |
4664 | if (processing_template_decl) | |
4665 | return NULL_TREE; | |
4666 | ||
8d08fdba MS |
4667 | /* Handle (a, b) used as an "lvalue". */ |
4668 | if (TREE_CODE (arg) == COMPOUND_EXPR) | |
4669 | { | |
5ade1ed2 DG |
4670 | tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0, |
4671 | tf_warning_or_error); | |
f293ce4b RS |
4672 | return build2 (COMPOUND_EXPR, TREE_TYPE (real_result), |
4673 | TREE_OPERAND (arg, 0), real_result); | |
8d08fdba MS |
4674 | } |
4675 | ||
4676 | /* Handle (a ? b : c) used as an "lvalue". */ | |
e9a25f70 JL |
4677 | if (TREE_CODE (arg) == COND_EXPR |
4678 | || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR) | |
5ade1ed2 | 4679 | return rationalize_conditional_expr (code, arg, tf_warning_or_error); |
a4443a08 | 4680 | |
ae818d3b | 4681 | /* Handle (a = b), (++a), and (--a) used as an "lvalue". */ |
e1cd6e56 MS |
4682 | if (TREE_CODE (arg) == MODIFY_EXPR |
4683 | || TREE_CODE (arg) == PREINCREMENT_EXPR | |
4684 | || TREE_CODE (arg) == PREDECREMENT_EXPR) | |
ae818d3b ER |
4685 | { |
4686 | tree lvalue = TREE_OPERAND (arg, 0); | |
4687 | if (TREE_SIDE_EFFECTS (lvalue)) | |
4688 | { | |
4689 | lvalue = stabilize_reference (lvalue); | |
f293ce4b RS |
4690 | arg = build2 (TREE_CODE (arg), TREE_TYPE (arg), |
4691 | lvalue, TREE_OPERAND (arg, 1)); | |
ae818d3b ER |
4692 | } |
4693 | return unary_complex_lvalue | |
f293ce4b | 4694 | (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue)); |
ae818d3b | 4695 | } |
8d08fdba MS |
4696 | |
4697 | if (code != ADDR_EXPR) | |
33fd4f49 | 4698 | return NULL_TREE; |
8d08fdba MS |
4699 | |
4700 | /* Handle (a = b) used as an "lvalue" for `&'. */ | |
4701 | if (TREE_CODE (arg) == MODIFY_EXPR | |
4702 | || TREE_CODE (arg) == INIT_EXPR) | |
4703 | { | |
5ade1ed2 DG |
4704 | tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0, |
4705 | tf_warning_or_error); | |
f293ce4b RS |
4706 | arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result), |
4707 | arg, real_result); | |
6de9cd9a | 4708 | TREE_NO_WARNING (arg) = 1; |
faf5394a | 4709 | return arg; |
8d08fdba MS |
4710 | } |
4711 | ||
8d08fdba MS |
4712 | if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE |
4713 | || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE | |
a5ac359a | 4714 | || TREE_CODE (arg) == OFFSET_REF) |
6d05585b | 4715 | return NULL_TREE; |
c8094d83 | 4716 | |
8d08fdba MS |
4717 | /* We permit compiler to make function calls returning |
4718 | objects of aggregate type look like lvalues. */ | |
4719 | { | |
4720 | tree targ = arg; | |
4721 | ||
4722 | if (TREE_CODE (targ) == SAVE_EXPR) | |
4723 | targ = TREE_OPERAND (targ, 0); | |
4724 | ||
9e1e64ec | 4725 | if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ))) |
8d08fdba MS |
4726 | { |
4727 | if (TREE_CODE (arg) == SAVE_EXPR) | |
4728 | targ = arg; | |
4729 | else | |
5566b478 | 4730 | targ = build_cplus_new (TREE_TYPE (arg), arg); |
f30432d7 | 4731 | return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ); |
8d08fdba MS |
4732 | } |
4733 | ||
4734 | if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF) | |
f293ce4b | 4735 | return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)), |
8d08fdba | 4736 | TREE_OPERAND (targ, 0), current_function_decl, NULL); |
8d08fdba MS |
4737 | } |
4738 | ||
4739 | /* Don't let anything else be handled specially. */ | |
33fd4f49 | 4740 | return NULL_TREE; |
8d08fdba | 4741 | } |
8d08fdba MS |
4742 | \f |
4743 | /* Mark EXP saying that we need to be able to take the | |
4744 | address of it; it should not be allocated in a register. | |
dffd7eb6 | 4745 | Value is true if successful. |
8d08fdba | 4746 | |
4ac14744 | 4747 | C++: we do not allow `current_class_ptr' to be addressable. */ |
8d08fdba | 4748 | |
dffd7eb6 | 4749 | bool |
acd8e2d0 | 4750 | cxx_mark_addressable (tree exp) |
8d08fdba | 4751 | { |
926ce8bd | 4752 | tree x = exp; |
8d08fdba | 4753 | |
8d08fdba MS |
4754 | while (1) |
4755 | switch (TREE_CODE (x)) | |
4756 | { | |
4757 | case ADDR_EXPR: | |
4758 | case COMPONENT_REF: | |
4759 | case ARRAY_REF: | |
37c46b43 MS |
4760 | case REALPART_EXPR: |
4761 | case IMAGPART_EXPR: | |
8d08fdba MS |
4762 | x = TREE_OPERAND (x, 0); |
4763 | break; | |
4764 | ||
4765 | case PARM_DECL: | |
4ac14744 | 4766 | if (x == current_class_ptr) |
8d08fdba | 4767 | { |
0cbd7506 | 4768 | error ("cannot take the address of %<this%>, which is an rvalue expression"); |
f4f206f4 | 4769 | TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */ |
dffd7eb6 | 4770 | return true; |
8d08fdba | 4771 | } |
f4f206f4 | 4772 | /* Fall through. */ |
aa8dea09 | 4773 | |
8d08fdba | 4774 | case VAR_DECL: |
8d08fdba MS |
4775 | /* Caller should not be trying to mark initialized |
4776 | constant fields addressable. */ | |
50bc768d NS |
4777 | gcc_assert (DECL_LANG_SPECIFIC (x) == 0 |
4778 | || DECL_IN_AGGR_P (x) == 0 | |
4779 | || TREE_STATIC (x) | |
4780 | || DECL_EXTERNAL (x)); | |
f4f206f4 | 4781 | /* Fall through. */ |
8d08fdba MS |
4782 | |
4783 | case CONST_DECL: | |
4784 | case RESULT_DECL: | |
9a3b49ac | 4785 | if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) |
1b8d7c9a | 4786 | && !DECL_ARTIFICIAL (x)) |
8ef36086 | 4787 | { |
7b09c6a4 | 4788 | if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x)) |
8ef36086 AP |
4789 | { |
4790 | error | |
4791 | ("address of explicit register variable %qD requested", x); | |
4792 | return false; | |
4793 | } | |
778f6a08 | 4794 | else if (extra_warnings) |
8ef36086 | 4795 | warning |
b323323f | 4796 | (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x); |
8ef36086 | 4797 | } |
8d08fdba | 4798 | TREE_ADDRESSABLE (x) = 1; |
dffd7eb6 | 4799 | return true; |
8d08fdba MS |
4800 | |
4801 | case FUNCTION_DECL: | |
8d08fdba | 4802 | TREE_ADDRESSABLE (x) = 1; |
dffd7eb6 | 4803 | return true; |
8d08fdba | 4804 | |
e92cc029 MS |
4805 | case CONSTRUCTOR: |
4806 | TREE_ADDRESSABLE (x) = 1; | |
dffd7eb6 | 4807 | return true; |
e92cc029 | 4808 | |
9a3b49ac MS |
4809 | case TARGET_EXPR: |
4810 | TREE_ADDRESSABLE (x) = 1; | |
dffd7eb6 NB |
4811 | cxx_mark_addressable (TREE_OPERAND (x, 0)); |
4812 | return true; | |
9a3b49ac | 4813 | |
8d08fdba | 4814 | default: |
dffd7eb6 | 4815 | return true; |
8d08fdba MS |
4816 | } |
4817 | } | |
4818 | \f | |
4819 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. */ | |
4820 | ||
4821 | tree | |
5ade1ed2 DG |
4822 | build_x_conditional_expr (tree ifexp, tree op1, tree op2, |
4823 | tsubst_flags_t complain) | |
8d08fdba | 4824 | { |
d17811fd MM |
4825 | tree orig_ifexp = ifexp; |
4826 | tree orig_op1 = op1; | |
4827 | tree orig_op2 = op2; | |
4828 | tree expr; | |
4829 | ||
5156628f | 4830 | if (processing_template_decl) |
d17811fd MM |
4831 | { |
4832 | /* The standard says that the expression is type-dependent if | |
4833 | IFEXP is type-dependent, even though the eventual type of the | |
4834 | expression doesn't dependent on IFEXP. */ | |
4835 | if (type_dependent_expression_p (ifexp) | |
18fd68a8 MM |
4836 | /* As a GNU extension, the middle operand may be omitted. */ |
4837 | || (op1 && type_dependent_expression_p (op1)) | |
d17811fd MM |
4838 | || type_dependent_expression_p (op2)) |
4839 | return build_min_nt (COND_EXPR, ifexp, op1, op2); | |
4840 | ifexp = build_non_dependent_expr (ifexp); | |
18fd68a8 MM |
4841 | if (op1) |
4842 | op1 = build_non_dependent_expr (op1); | |
d17811fd MM |
4843 | op2 = build_non_dependent_expr (op2); |
4844 | } | |
5566b478 | 4845 | |
5ade1ed2 | 4846 | expr = build_conditional_expr (ifexp, op1, op2, complain); |
d17811fd | 4847 | if (processing_template_decl && expr != error_mark_node) |
c8094d83 | 4848 | return build_min_non_dep (COND_EXPR, expr, |
8e1daa34 | 4849 | orig_ifexp, orig_op1, orig_op2); |
d17811fd | 4850 | return expr; |
8d08fdba MS |
4851 | } |
4852 | \f | |
c7b62f14 | 4853 | /* Given a list of expressions, return a compound expression |
04c06002 | 4854 | that performs them all and returns the value of the last of them. */ |
c7b62f14 NS |
4855 | |
4856 | tree build_x_compound_expr_from_list (tree list, const char *msg) | |
4857 | { | |
4858 | tree expr = TREE_VALUE (list); | |
c8094d83 | 4859 | |
c7b62f14 NS |
4860 | if (TREE_CHAIN (list)) |
4861 | { | |
4862 | if (msg) | |
cbe5f3b3 | 4863 | permerror (input_location, "%s expression list treated as compound expression", msg); |
c7b62f14 NS |
4864 | |
4865 | for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list)) | |
5ade1ed2 DG |
4866 | expr = build_x_compound_expr (expr, TREE_VALUE (list), |
4867 | tf_warning_or_error); | |
c7b62f14 | 4868 | } |
c8094d83 | 4869 | |
c7b62f14 NS |
4870 | return expr; |
4871 | } | |
4872 | ||
4873 | /* Handle overloading of the ',' operator when needed. */ | |
e92cc029 | 4874 | |
8d08fdba | 4875 | tree |
5ade1ed2 | 4876 | build_x_compound_expr (tree op1, tree op2, tsubst_flags_t complain) |
8d08fdba | 4877 | { |
8d08fdba | 4878 | tree result; |
d17811fd MM |
4879 | tree orig_op1 = op1; |
4880 | tree orig_op2 = op2; | |
8d08fdba | 4881 | |
5156628f | 4882 | if (processing_template_decl) |
d17811fd MM |
4883 | { |
4884 | if (type_dependent_expression_p (op1) | |
4885 | || type_dependent_expression_p (op2)) | |
4886 | return build_min_nt (COMPOUND_EXPR, op1, op2); | |
4887 | op1 = build_non_dependent_expr (op1); | |
4888 | op2 = build_non_dependent_expr (op2); | |
4889 | } | |
b19b4a78 | 4890 | |
ec835fb2 | 4891 | result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE, |
5ade1ed2 | 4892 | /*overloaded_p=*/NULL, complain); |
d17811fd | 4893 | if (!result) |
525521b6 | 4894 | result = cp_build_compound_expr (op1, op2, complain); |
b19b4a78 | 4895 | |
d17811fd | 4896 | if (processing_template_decl && result != error_mark_node) |
8e1daa34 | 4897 | return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2); |
c8094d83 | 4898 | |
d17811fd | 4899 | return result; |
8d08fdba MS |
4900 | } |
4901 | ||
525521b6 DG |
4902 | /* Like cp_build_compound_expr, but for the c-common bits. */ |
4903 | ||
4904 | tree | |
4905 | build_compound_expr (tree lhs, tree rhs) | |
4906 | { | |
4907 | return cp_build_compound_expr (lhs, rhs, tf_warning_or_error); | |
4908 | } | |
4909 | ||
04c06002 | 4910 | /* Build a compound expression. */ |
8d08fdba MS |
4911 | |
4912 | tree | |
525521b6 | 4913 | cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain) |
8d08fdba | 4914 | { |
5ade1ed2 | 4915 | lhs = convert_to_void (lhs, "left-hand operand of comma", complain); |
c8094d83 | 4916 | |
c7b62f14 | 4917 | if (lhs == error_mark_node || rhs == error_mark_node) |
66543169 | 4918 | return error_mark_node; |
c8094d83 | 4919 | |
d340e53f NS |
4920 | if (TREE_CODE (rhs) == TARGET_EXPR) |
4921 | { | |
4922 | /* If the rhs is a TARGET_EXPR, then build the compound | |
0cbd7506 | 4923 | expression inside the target_expr's initializer. This |
cd0be382 | 4924 | helps the compiler to eliminate unnecessary temporaries. */ |
d340e53f | 4925 | tree init = TREE_OPERAND (rhs, 1); |
c8094d83 | 4926 | |
f293ce4b | 4927 | init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init); |
d340e53f | 4928 | TREE_OPERAND (rhs, 1) = init; |
c8094d83 | 4929 | |
d340e53f NS |
4930 | return rhs; |
4931 | } | |
c8094d83 | 4932 | |
f293ce4b | 4933 | return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs); |
8d08fdba MS |
4934 | } |
4935 | ||
33c25e5c | 4936 | /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE |
86b7b98b | 4937 | casts away constness. CAST gives the type of cast. */ |
3fe18f1d MM |
4938 | |
4939 | static void | |
a5ac359a | 4940 | check_for_casting_away_constness (tree src_type, tree dest_type, |
86b7b98b | 4941 | enum tree_code cast) |
3fe18f1d | 4942 | { |
86b7b98b MLI |
4943 | /* C-style casts are allowed to cast away constness. With |
4944 | WARN_CAST_QUAL, we still want to issue a warning. */ | |
4945 | if (cast == CAST_EXPR && !warn_cast_qual) | |
4946 | return; | |
4947 | ||
4948 | if (casts_away_constness (src_type, dest_type)) | |
4949 | switch (cast) | |
4950 | { | |
4951 | case CAST_EXPR: | |
4952 | warning (OPT_Wcast_qual, | |
4953 | "cast from type %qT to type %qT casts away constness", | |
4954 | src_type, dest_type); | |
4955 | return; | |
4956 | ||
4957 | case STATIC_CAST_EXPR: | |
4958 | error ("static_cast from type %qT to type %qT casts away constness", | |
4959 | src_type, dest_type); | |
4960 | return; | |
4961 | ||
4962 | case REINTERPRET_CAST_EXPR: | |
4963 | error ("reinterpret_cast from type %qT to type %qT casts away constness", | |
4964 | src_type, dest_type); | |
4965 | return; | |
4966 | default: | |
4967 | gcc_unreachable(); | |
4968 | } | |
3fe18f1d MM |
4969 | } |
4970 | ||
08e17d9d MM |
4971 | /* Convert EXPR (an expression with pointer-to-member type) to TYPE |
4972 | (another pointer-to-member type in the same hierarchy) and return | |
4973 | the converted expression. If ALLOW_INVERSE_P is permitted, a | |
4974 | pointer-to-derived may be converted to pointer-to-base; otherwise, | |
4975 | only the other direction is permitted. If C_CAST_P is true, this | |
4976 | conversion is taking place as part of a C-style cast. */ | |
4977 | ||
c8094d83 | 4978 | tree |
08e17d9d MM |
4979 | convert_ptrmem (tree type, tree expr, bool allow_inverse_p, |
4980 | bool c_cast_p) | |
4981 | { | |
4982 | if (TYPE_PTRMEM_P (type)) | |
4983 | { | |
4984 | tree delta; | |
4985 | ||
4986 | if (TREE_CODE (expr) == PTRMEM_CST) | |
4987 | expr = cplus_expand_constant (expr); | |
4988 | delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)), | |
c8094d83 | 4989 | TYPE_PTRMEM_CLASS_TYPE (type), |
08e17d9d MM |
4990 | allow_inverse_p, |
4991 | c_cast_p); | |
4992 | if (!integer_zerop (delta)) | |
6e03b280 OW |
4993 | { |
4994 | tree cond, op1, op2; | |
4995 | ||
ba47d38d AH |
4996 | cond = cp_build_binary_op (input_location, |
4997 | EQ_EXPR, | |
6e03b280 | 4998 | expr, |
5ade1ed2 DG |
4999 | build_int_cst (TREE_TYPE (expr), -1), |
5000 | tf_warning_or_error); | |
6e03b280 | 5001 | op1 = build_nop (ptrdiff_type_node, expr); |
ba47d38d AH |
5002 | op2 = cp_build_binary_op (input_location, |
5003 | PLUS_EXPR, op1, delta, | |
5ade1ed2 | 5004 | tf_warning_or_error); |
6e03b280 OW |
5005 | |
5006 | expr = fold_build3 (COND_EXPR, ptrdiff_type_node, cond, op1, op2); | |
5007 | ||
5008 | } | |
5009 | ||
08e17d9d MM |
5010 | return build_nop (type, expr); |
5011 | } | |
5012 | else | |
c8094d83 | 5013 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, |
08e17d9d MM |
5014 | allow_inverse_p, c_cast_p); |
5015 | } | |
5016 | ||
822971c1 | 5017 | /* If EXPR is an INTEGER_CST and ORIG is an arithmetic constant, return |
dc569621 RS |
5018 | a version of EXPR that has TREE_OVERFLOW set if it is set in ORIG. |
5019 | Otherwise, return EXPR unchanged. */ | |
822971c1 JJ |
5020 | |
5021 | static tree | |
5022 | ignore_overflows (tree expr, tree orig) | |
5023 | { | |
5024 | if (TREE_CODE (expr) == INTEGER_CST | |
5025 | && CONSTANT_CLASS_P (orig) | |
5026 | && TREE_CODE (orig) != STRING_CST | |
dc569621 | 5027 | && TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig)) |
822971c1 | 5028 | { |
dc569621 | 5029 | if (!TREE_OVERFLOW (orig)) |
822971c1 JJ |
5030 | /* Ensure constant sharing. */ |
5031 | expr = build_int_cst_wide (TREE_TYPE (expr), | |
5032 | TREE_INT_CST_LOW (expr), | |
5033 | TREE_INT_CST_HIGH (expr)); | |
5034 | else | |
5035 | { | |
5036 | /* Avoid clobbering a shared constant. */ | |
5037 | expr = copy_node (expr); | |
5038 | TREE_OVERFLOW (expr) = TREE_OVERFLOW (orig); | |
822971c1 JJ |
5039 | } |
5040 | } | |
5041 | return expr; | |
5042 | } | |
5043 | ||
33c25e5c MM |
5044 | /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true, |
5045 | this static_cast is being attempted as one of the possible casts | |
5046 | allowed by a C-style cast. (In that case, accessibility of base | |
5047 | classes is not considered, and it is OK to cast away | |
5048 | constness.) Return the result of the cast. *VALID_P is set to | |
5049 | indicate whether or not the cast was valid. */ | |
3fe18f1d | 5050 | |
33c25e5c MM |
5051 | static tree |
5052 | build_static_cast_1 (tree type, tree expr, bool c_cast_p, | |
5ade1ed2 | 5053 | bool *valid_p, tsubst_flags_t complain) |
a4443a08 | 5054 | { |
00bb3dad | 5055 | tree intype; |
3fe18f1d | 5056 | tree result; |
33c25e5c | 5057 | tree orig; |
c11b6f21 | 5058 | |
33c25e5c MM |
5059 | /* Assume the cast is valid. */ |
5060 | *valid_p = true; | |
c11b6f21 | 5061 | |
3fe18f1d | 5062 | intype = TREE_TYPE (expr); |
c11b6f21 | 5063 | |
8d8d1a28 AH |
5064 | /* Save casted types in the function's used types hash table. */ |
5065 | used_types_insert (type); | |
5066 | ||
3fe18f1d | 5067 | /* [expr.static.cast] |
c11b6f21 | 5068 | |
3fe18f1d MM |
5069 | An lvalue of type "cv1 B", where B is a class type, can be cast |
5070 | to type "reference to cv2 D", where D is a class derived (clause | |
5071 | _class.derived_) from B, if a valid standard conversion from | |
5072 | "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the | |
5073 | same cv-qualification as, or greater cv-qualification than, cv1, | |
5074 | and B is not a virtual base class of D. */ | |
0a72704b MM |
5075 | /* We check this case before checking the validity of "TYPE t = |
5076 | EXPR;" below because for this case: | |
5077 | ||
5078 | struct B {}; | |
5079 | struct D : public B { D(const B&); }; | |
5080 | extern B& b; | |
5081 | void f() { static_cast<const D&>(b); } | |
5082 | ||
5083 | we want to avoid constructing a new D. The standard is not | |
5084 | completely clear about this issue, but our interpretation is | |
5085 | consistent with other compilers. */ | |
3fe18f1d MM |
5086 | if (TREE_CODE (type) == REFERENCE_TYPE |
5087 | && CLASS_TYPE_P (TREE_TYPE (type)) | |
5088 | && CLASS_TYPE_P (intype) | |
d18a8251 | 5089 | && real_lvalue_p (expr) |
3fe18f1d MM |
5090 | && DERIVED_FROM_P (intype, TREE_TYPE (type)) |
5091 | && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)), | |
c8094d83 | 5092 | build_pointer_type (TYPE_MAIN_VARIANT |
3fe18f1d | 5093 | (TREE_TYPE (type)))) |
33c25e5c MM |
5094 | && (c_cast_p |
5095 | || at_least_as_qualified_p (TREE_TYPE (type), intype))) | |
c11b6f21 | 5096 | { |
33c25e5c MM |
5097 | tree base; |
5098 | ||
385bce06 | 5099 | /* There is a standard conversion from "D*" to "B*" even if "B" |
33c25e5c MM |
5100 | is ambiguous or inaccessible. If this is really a |
5101 | static_cast, then we check both for inaccessibility and | |
5102 | ambiguity. However, if this is a static_cast being performed | |
5103 | because the user wrote a C-style cast, then accessibility is | |
5104 | not considered. */ | |
c8094d83 MS |
5105 | base = lookup_base (TREE_TYPE (type), intype, |
5106 | c_cast_p ? ba_unique : ba_check, | |
33c25e5c | 5107 | NULL); |
3fe18f1d | 5108 | |
385bce06 MM |
5109 | /* Convert from "B*" to "D*". This function will check that "B" |
5110 | is not a virtual base of "D". */ | |
c8094d83 | 5111 | expr = build_base_path (MINUS_EXPR, build_address (expr), |
3fe18f1d | 5112 | base, /*nonnull=*/false); |
3a73bcc6 MM |
5113 | /* Convert the pointer to a reference -- but then remember that |
5114 | there are no expressions with reference type in C++. */ | |
5115 | return convert_from_reference (build_nop (type, expr)); | |
c11b6f21 | 5116 | } |
3fe18f1d | 5117 | |
33c25e5c MM |
5118 | orig = expr; |
5119 | ||
0a72704b MM |
5120 | /* [expr.static.cast] |
5121 | ||
5122 | An expression e can be explicitly converted to a type T using a | |
5123 | static_cast of the form static_cast<T>(e) if the declaration T | |
5124 | t(e);" is well-formed, for some invented temporary variable | |
5125 | t. */ | |
33c25e5c | 5126 | result = perform_direct_initialization_if_possible (type, expr, |
5ade1ed2 | 5127 | c_cast_p, complain); |
0a72704b | 5128 | if (result) |
109e0040 MM |
5129 | { |
5130 | result = convert_from_reference (result); | |
33c25e5c MM |
5131 | |
5132 | /* Ignore any integer overflow caused by the cast. */ | |
822971c1 JJ |
5133 | result = ignore_overflows (result, orig); |
5134 | ||
109e0040 MM |
5135 | /* [expr.static.cast] |
5136 | ||
0cbd7506 | 5137 | If T is a reference type, the result is an lvalue; otherwise, |
109e0040 | 5138 | the result is an rvalue. */ |
5cc53d4e MM |
5139 | if (TREE_CODE (type) != REFERENCE_TYPE) |
5140 | result = rvalue (result); | |
109e0040 MM |
5141 | return result; |
5142 | } | |
c8094d83 | 5143 | |
0a72704b MM |
5144 | /* [expr.static.cast] |
5145 | ||
5146 | Any expression can be explicitly converted to type cv void. */ | |
5147 | if (TREE_CODE (type) == VOID_TYPE) | |
5ade1ed2 | 5148 | return convert_to_void (expr, /*implicit=*/NULL, complain); |
0a72704b | 5149 | |
3fe18f1d MM |
5150 | /* [expr.static.cast] |
5151 | ||
5152 | The inverse of any standard conversion sequence (clause _conv_), | |
5153 | other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer | |
5154 | (_conv.array_), function-to-pointer (_conv.func_), and boolean | |
5155 | (_conv.bool_) conversions, can be performed explicitly using | |
5156 | static_cast subject to the restriction that the explicit | |
5157 | conversion does not cast away constness (_expr.const.cast_), and | |
5158 | the following additional rules for specific cases: */ | |
5159 | /* For reference, the conversions not excluded are: integral | |
5160 | promotions, floating point promotion, integral conversions, | |
5161 | floating point conversions, floating-integral conversions, | |
5162 | pointer conversions, and pointer to member conversions. */ | |
a10ce2f8 | 5163 | /* DR 128 |
c8094d83 | 5164 | |
a10ce2f8 GK |
5165 | A value of integral _or enumeration_ type can be explicitly |
5166 | converted to an enumeration type. */ | |
5167 | /* The effect of all that is that any conversion between any two | |
5168 | types which are integral, floating, or enumeration types can be | |
5169 | performed. */ | |
5170 | if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type)) | |
5171 | && (INTEGRAL_TYPE_P (intype) || SCALAR_FLOAT_TYPE_P (intype))) | |
33c25e5c | 5172 | { |
33c25e5c MM |
5173 | expr = ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL); |
5174 | ||
5175 | /* Ignore any integer overflow caused by the cast. */ | |
822971c1 | 5176 | expr = ignore_overflows (expr, orig); |
33c25e5c MM |
5177 | return expr; |
5178 | } | |
5179 | ||
3fe18f1d MM |
5180 | if (TYPE_PTR_P (type) && TYPE_PTR_P (intype) |
5181 | && CLASS_TYPE_P (TREE_TYPE (type)) | |
5182 | && CLASS_TYPE_P (TREE_TYPE (intype)) | |
c8094d83 MS |
5183 | && can_convert (build_pointer_type (TYPE_MAIN_VARIANT |
5184 | (TREE_TYPE (intype))), | |
5185 | build_pointer_type (TYPE_MAIN_VARIANT | |
3fe18f1d | 5186 | (TREE_TYPE (type))))) |
999cc24c | 5187 | { |
3fe18f1d MM |
5188 | tree base; |
5189 | ||
33c25e5c | 5190 | if (!c_cast_p) |
86b7b98b | 5191 | check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR); |
c8094d83 MS |
5192 | base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype), |
5193 | c_cast_p ? ba_unique : ba_check, | |
385bce06 | 5194 | NULL); |
3fe18f1d | 5195 | return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false); |
999cc24c | 5196 | } |
c8094d83 | 5197 | |
3fe18f1d MM |
5198 | if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype)) |
5199 | || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) | |
5200 | { | |
5201 | tree c1; | |
5202 | tree c2; | |
5203 | tree t1; | |
5204 | tree t2; | |
c11b6f21 | 5205 | |
3fe18f1d MM |
5206 | c1 = TYPE_PTRMEM_CLASS_TYPE (intype); |
5207 | c2 = TYPE_PTRMEM_CLASS_TYPE (type); | |
5208 | ||
5209 | if (TYPE_PTRMEM_P (type)) | |
5210 | { | |
c8094d83 | 5211 | t1 = (build_ptrmem_type |
3fe18f1d MM |
5212 | (c1, |
5213 | TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype)))); | |
c8094d83 | 5214 | t2 = (build_ptrmem_type |
3fe18f1d MM |
5215 | (c2, |
5216 | TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type)))); | |
5217 | } | |
5218 | else | |
5219 | { | |
5220 | t1 = intype; | |
5221 | t2 = type; | |
5222 | } | |
6e03b280 | 5223 | if (can_convert (t1, t2) || can_convert (t2, t1)) |
3fe18f1d | 5224 | { |
33c25e5c | 5225 | if (!c_cast_p) |
86b7b98b | 5226 | check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR); |
08e17d9d MM |
5227 | return convert_ptrmem (type, expr, /*allow_inverse_p=*/1, |
5228 | c_cast_p); | |
3fe18f1d MM |
5229 | } |
5230 | } | |
c8094d83 | 5231 | |
af7b9902 MM |
5232 | /* [expr.static.cast] |
5233 | ||
3fe18f1d MM |
5234 | An rvalue of type "pointer to cv void" can be explicitly |
5235 | converted to a pointer to object type. A value of type pointer | |
5236 | to object converted to "pointer to cv void" and back to the | |
5237 | original pointer type will have its original value. */ | |
c8094d83 | 5238 | if (TREE_CODE (intype) == POINTER_TYPE |
3fe18f1d MM |
5239 | && VOID_TYPE_P (TREE_TYPE (intype)) |
5240 | && TYPE_PTROB_P (type)) | |
af7b9902 | 5241 | { |
33c25e5c | 5242 | if (!c_cast_p) |
86b7b98b | 5243 | check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR); |
3fe18f1d | 5244 | return build_nop (type, expr); |
af7b9902 MM |
5245 | } |
5246 | ||
33c25e5c | 5247 | *valid_p = false; |
c11b6f21 | 5248 | return error_mark_node; |
a4443a08 MS |
5249 | } |
5250 | ||
33c25e5c MM |
5251 | /* Return an expression representing static_cast<TYPE>(EXPR). */ |
5252 | ||
cdf5b885 | 5253 | tree |
5ade1ed2 | 5254 | build_static_cast (tree type, tree expr, tsubst_flags_t complain) |
a4443a08 | 5255 | { |
33c25e5c MM |
5256 | tree result; |
5257 | bool valid_p; | |
c11b6f21 MS |
5258 | |
5259 | if (type == error_mark_node || expr == error_mark_node) | |
5260 | return error_mark_node; | |
5261 | ||
5156628f | 5262 | if (processing_template_decl) |
5566b478 | 5263 | { |
33c25e5c MM |
5264 | expr = build_min (STATIC_CAST_EXPR, type, expr); |
5265 | /* We don't know if it will or will not have side effects. */ | |
5266 | TREE_SIDE_EFFECTS (expr) = 1; | |
e8c66fe0 | 5267 | return convert_from_reference (expr); |
5566b478 MS |
5268 | } |
5269 | ||
33c25e5c MM |
5270 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
5271 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ | |
5272 | if (TREE_CODE (type) != REFERENCE_TYPE | |
5273 | && TREE_CODE (expr) == NOP_EXPR | |
5274 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
5275 | expr = TREE_OPERAND (expr, 0); | |
c11b6f21 | 5276 | |
5ade1ed2 DG |
5277 | result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p, |
5278 | complain); | |
33c25e5c MM |
5279 | if (valid_p) |
5280 | return result; | |
5281 | ||
5ade1ed2 DG |
5282 | if (complain & tf_error) |
5283 | error ("invalid static_cast from type %qT to type %qT", | |
5284 | TREE_TYPE (expr), type); | |
33c25e5c MM |
5285 | return error_mark_node; |
5286 | } | |
5287 | ||
5288 | /* EXPR is an expression with member function or pointer-to-member | |
5289 | function type. TYPE is a pointer type. Converting EXPR to TYPE is | |
5290 | not permitted by ISO C++, but we accept it in some modes. If we | |
5291 | are not in one of those modes, issue a diagnostic. Return the | |
5292 | converted expression. */ | |
5293 | ||
5294 | tree | |
5295 | convert_member_func_to_ptr (tree type, tree expr) | |
5296 | { | |
5297 | tree intype; | |
5298 | tree decl; | |
c11b6f21 | 5299 | |
c11b6f21 | 5300 | intype = TREE_TYPE (expr); |
33c25e5c MM |
5301 | gcc_assert (TYPE_PTRMEMFUNC_P (intype) |
5302 | || TREE_CODE (intype) == METHOD_TYPE); | |
a80e4195 | 5303 | |
33c25e5c | 5304 | if (pedantic || warn_pmf2ptr) |
509c9d60 | 5305 | pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpmf_conversions, |
fcf73884 | 5306 | "converting from %qT to %qT", intype, type); |
c8094d83 | 5307 | |
33c25e5c MM |
5308 | if (TREE_CODE (intype) == METHOD_TYPE) |
5309 | expr = build_addr_func (expr); | |
5310 | else if (TREE_CODE (expr) == PTRMEM_CST) | |
5311 | expr = build_address (PTRMEM_CST_MEMBER (expr)); | |
5312 | else | |
5313 | { | |
5314 | decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0); | |
5315 | decl = build_address (decl); | |
5316 | expr = get_member_function_from_ptrfunc (&decl, expr); | |
5317 | } | |
5318 | ||
5319 | return build_nop (type, expr); | |
5320 | } | |
5321 | ||
5322 | /* Return a representation for a reinterpret_cast from EXPR to TYPE. | |
5323 | If C_CAST_P is true, this reinterpret cast is being done as part of | |
5324 | a C-style cast. If VALID_P is non-NULL, *VALID_P is set to | |
5325 | indicate whether or not reinterpret_cast was valid. */ | |
5326 | ||
5327 | static tree | |
5328 | build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p, | |
5ade1ed2 | 5329 | bool *valid_p, tsubst_flags_t complain) |
33c25e5c MM |
5330 | { |
5331 | tree intype; | |
5332 | ||
5333 | /* Assume the cast is invalid. */ | |
5334 | if (valid_p) | |
5335 | *valid_p = true; | |
5336 | ||
5337 | if (type == error_mark_node || error_operand_p (expr)) | |
a8c2c492 AP |
5338 | return error_mark_node; |
5339 | ||
33c25e5c MM |
5340 | intype = TREE_TYPE (expr); |
5341 | ||
8d8d1a28 AH |
5342 | /* Save casted types in the function's used types hash table. */ |
5343 | used_types_insert (type); | |
5344 | ||
33c25e5c MM |
5345 | /* [expr.reinterpret.cast] |
5346 | An lvalue expression of type T1 can be cast to the type | |
5347 | "reference to T2" if an expression of type "pointer to T1" can be | |
5348 | explicitly converted to the type "pointer to T2" using a | |
5349 | reinterpret_cast. */ | |
c11b6f21 MS |
5350 | if (TREE_CODE (type) == REFERENCE_TYPE) |
5351 | { | |
5352 | if (! real_lvalue_p (expr)) | |
5353 | { | |
5ade1ed2 DG |
5354 | if (complain & tf_error) |
5355 | error ("invalid cast of an rvalue expression of type " | |
5356 | "%qT to type %qT", | |
5357 | intype, type); | |
c11b6f21 MS |
5358 | return error_mark_node; |
5359 | } | |
33c25e5c MM |
5360 | |
5361 | /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and | |
5362 | "B" are related class types; the reinterpret_cast does not | |
5363 | adjust the pointer. */ | |
5364 | if (TYPE_PTR_P (intype) | |
5ade1ed2 | 5365 | && (complain & tf_warning) |
96d84882 PB |
5366 | && (comptypes (TREE_TYPE (intype), TREE_TYPE (type), |
5367 | COMPARE_BASE | COMPARE_DERIVED))) | |
d4ee4d25 | 5368 | warning (0, "casting %qT to %qT does not dereference pointer", |
33c25e5c MM |
5369 | intype, type); |
5370 | ||
5ade1ed2 | 5371 | expr = cp_build_unary_op (ADDR_EXPR, expr, 0, complain); |
c11b6f21 | 5372 | if (expr != error_mark_node) |
33c25e5c | 5373 | expr = build_reinterpret_cast_1 |
ec7e5618 | 5374 | (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p, |
5ade1ed2 | 5375 | valid_p, complain); |
c11b6f21 | 5376 | if (expr != error_mark_node) |
5ade1ed2 | 5377 | expr = cp_build_indirect_ref (expr, 0, complain); |
c11b6f21 MS |
5378 | return expr; |
5379 | } | |
8ccc31eb | 5380 | |
33c25e5c MM |
5381 | /* As a G++ extension, we consider conversions from member |
5382 | functions, and pointers to member functions to | |
5383 | pointer-to-function and pointer-to-void types. If | |
5384 | -Wno-pmf-conversions has not been specified, | |
5385 | convert_member_func_to_ptr will issue an error message. */ | |
c8094d83 | 5386 | if ((TYPE_PTRMEMFUNC_P (intype) |
33c25e5c MM |
5387 | || TREE_CODE (intype) == METHOD_TYPE) |
5388 | && TYPE_PTR_P (type) | |
5389 | && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
5390 | || VOID_TYPE_P (TREE_TYPE (type)))) | |
5391 | return convert_member_func_to_ptr (type, expr); | |
5392 | ||
5acd0bed | 5393 | /* If the cast is not to a reference type, the lvalue-to-rvalue, |
33c25e5c MM |
5394 | array-to-pointer, and function-to-pointer conversions are |
5395 | performed. */ | |
5396 | expr = decay_conversion (expr); | |
c8094d83 | 5397 | |
33c25e5c MM |
5398 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
5399 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ | |
5400 | if (TREE_CODE (expr) == NOP_EXPR | |
5401 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
5402 | expr = TREE_OPERAND (expr, 0); | |
5403 | ||
5404 | if (error_operand_p (expr)) | |
5405 | return error_mark_node; | |
5406 | ||
5407 | intype = TREE_TYPE (expr); | |
5408 | ||
5409 | /* [expr.reinterpret.cast] | |
5410 | A pointer can be converted to any integral type large enough to | |
5411 | hold it. */ | |
5412 | if (CP_INTEGRAL_TYPE_P (type) && TYPE_PTR_P (intype)) | |
8ccc31eb | 5413 | { |
c11b6f21 | 5414 | if (TYPE_PRECISION (type) < TYPE_PRECISION (intype)) |
5ade1ed2 DG |
5415 | { |
5416 | if (complain & tf_error) | |
cbe5f3b3 | 5417 | permerror (input_location, "cast from %qT to %qT loses precision", |
37ec60ed | 5418 | intype, type); |
5ade1ed2 DG |
5419 | else |
5420 | return error_mark_node; | |
5421 | } | |
8ccc31eb | 5422 | } |
33c25e5c MM |
5423 | /* [expr.reinterpret.cast] |
5424 | A value of integral or enumeration type can be explicitly | |
5425 | converted to a pointer. */ | |
5426 | else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype)) | |
5427 | /* OK */ | |
5428 | ; | |
c11b6f21 MS |
5429 | else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype)) |
5430 | || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) | |
8a784e4a | 5431 | return fold_if_not_in_template (build_nop (type, expr)); |
c11b6f21 MS |
5432 | else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype)) |
5433 | || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype))) | |
8ccc31eb | 5434 | { |
de9c56a4 RG |
5435 | tree sexpr = expr; |
5436 | ||
33c25e5c | 5437 | if (!c_cast_p) |
86b7b98b | 5438 | check_for_casting_away_constness (intype, type, REINTERPRET_CAST_EXPR); |
33c25e5c MM |
5439 | /* Warn about possible alignment problems. */ |
5440 | if (STRICT_ALIGNMENT && warn_cast_align | |
5ade1ed2 | 5441 | && (complain & tf_warning) |
33c25e5c MM |
5442 | && !VOID_TYPE_P (type) |
5443 | && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE | |
5444 | && COMPLETE_TYPE_P (TREE_TYPE (type)) | |
5445 | && COMPLETE_TYPE_P (TREE_TYPE (intype)) | |
5446 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype))) | |
683d6ff9 MLI |
5447 | warning (OPT_Wcast_align, "cast from %qT to %qT " |
5448 | "increases required alignment of target type", intype, type); | |
c8094d83 | 5449 | |
3b426391 | 5450 | /* We need to strip nops here, because the front end likes to |
de9c56a4 RG |
5451 | create (int *)&a for array-to-pointer decay, instead of &a[0]. */ |
5452 | STRIP_NOPS (sexpr); | |
79bedddc SR |
5453 | if (warn_strict_aliasing <= 2) |
5454 | strict_aliasing_warning (intype, type, sexpr); | |
de9c56a4 | 5455 | |
455f19cb | 5456 | return fold_if_not_in_template (build_nop (type, expr)); |
8ccc31eb | 5457 | } |
ffb690bd | 5458 | else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype)) |
a06d48ef | 5459 | || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type))) |
ffb690bd | 5460 | { |
5ade1ed2 | 5461 | if (pedantic && (complain & tf_warning)) |
ec7e5618 | 5462 | /* Only issue a warning, as we have always supported this |
0cbd7506 MS |
5463 | where possible, and it is necessary in some cases. DR 195 |
5464 | addresses this issue, but as of 2004/10/26 is still in | |
5465 | drafting. */ | |
5466 | warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object"); | |
455f19cb | 5467 | return fold_if_not_in_template (build_nop (type, expr)); |
ffb690bd | 5468 | } |
08e17d9d MM |
5469 | else if (TREE_CODE (type) == VECTOR_TYPE) |
5470 | return fold_if_not_in_template (convert_to_vector (type, expr)); | |
d333b74f | 5471 | else if (TREE_CODE (intype) == VECTOR_TYPE && INTEGRAL_TYPE_P (type)) |
257d5f32 | 5472 | return fold_if_not_in_template (convert_to_integer (type, expr)); |
c11b6f21 | 5473 | else |
8ccc31eb | 5474 | { |
33c25e5c MM |
5475 | if (valid_p) |
5476 | *valid_p = false; | |
5ade1ed2 DG |
5477 | if (complain & tf_error) |
5478 | error ("invalid cast from type %qT to type %qT", intype, type); | |
8ccc31eb MS |
5479 | return error_mark_node; |
5480 | } | |
c8094d83 | 5481 | |
37c46b43 | 5482 | return cp_convert (type, expr); |
a4443a08 MS |
5483 | } |
5484 | ||
cdf5b885 | 5485 | tree |
5ade1ed2 | 5486 | build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain) |
a4443a08 | 5487 | { |
f30432d7 MS |
5488 | if (type == error_mark_node || expr == error_mark_node) |
5489 | return error_mark_node; | |
5490 | ||
5156628f | 5491 | if (processing_template_decl) |
e92cc029 | 5492 | { |
33c25e5c | 5493 | tree t = build_min (REINTERPRET_CAST_EXPR, type, expr); |
c8094d83 | 5494 | |
8e1daa34 NS |
5495 | if (!TREE_SIDE_EFFECTS (t) |
5496 | && type_dependent_expression_p (expr)) | |
5497 | /* There might turn out to be side effects inside expr. */ | |
5498 | TREE_SIDE_EFFECTS (t) = 1; | |
e8c66fe0 | 5499 | return convert_from_reference (t); |
e92cc029 MS |
5500 | } |
5501 | ||
33c25e5c | 5502 | return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false, |
5ade1ed2 | 5503 | /*valid_p=*/NULL, complain); |
33c25e5c MM |
5504 | } |
5505 | ||
5506 | /* Perform a const_cast from EXPR to TYPE. If the cast is valid, | |
5507 | return an appropriate expression. Otherwise, return | |
5508 | error_mark_node. If the cast is not valid, and COMPLAIN is true, | |
7735d402 MM |
5509 | then a diagnostic will be issued. If VALID_P is non-NULL, we are |
5510 | performing a C-style cast, its value upon return will indicate | |
5511 | whether or not the conversion succeeded. */ | |
33c25e5c MM |
5512 | |
5513 | static tree | |
5514 | build_const_cast_1 (tree dst_type, tree expr, bool complain, | |
5515 | bool *valid_p) | |
5516 | { | |
5517 | tree src_type; | |
5518 | tree reference_type; | |
5519 | ||
5520 | /* Callers are responsible for handling error_mark_node as a | |
5521 | destination type. */ | |
5522 | gcc_assert (dst_type != error_mark_node); | |
5523 | /* In a template, callers should be building syntactic | |
5524 | representations of casts, not using this machinery. */ | |
5525 | gcc_assert (!processing_template_decl); | |
5526 | ||
5527 | /* Assume the conversion is invalid. */ | |
5528 | if (valid_p) | |
5529 | *valid_p = false; | |
5530 | ||
5531 | if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRMEM_P (dst_type)) | |
5532 | { | |
5533 | if (complain) | |
5534 | error ("invalid use of const_cast with type %qT, " | |
5535 | "which is not a pointer, " | |
5536 | "reference, nor a pointer-to-data-member type", dst_type); | |
3fd91cbd MM |
5537 | return error_mark_node; |
5538 | } | |
5539 | ||
33c25e5c | 5540 | if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE) |
8ccc31eb | 5541 | { |
33c25e5c MM |
5542 | if (complain) |
5543 | error ("invalid use of const_cast with type %qT, which is a pointer " | |
5544 | "or reference to a function type", dst_type); | |
5545 | return error_mark_node; | |
8ccc31eb MS |
5546 | } |
5547 | ||
8d8d1a28 AH |
5548 | /* Save casted types in the function's used types hash table. */ |
5549 | used_types_insert (dst_type); | |
5550 | ||
33c25e5c MM |
5551 | src_type = TREE_TYPE (expr); |
5552 | /* Expressions do not really have reference types. */ | |
5553 | if (TREE_CODE (src_type) == REFERENCE_TYPE) | |
5554 | src_type = TREE_TYPE (src_type); | |
5555 | ||
5556 | /* [expr.const.cast] | |
5557 | ||
5558 | An lvalue of type T1 can be explicitly converted to an lvalue of | |
5559 | type T2 using the cast const_cast<T2&> (where T1 and T2 are object | |
5560 | types) if a pointer to T1 can be explicitly converted to the type | |
5561 | pointer to T2 using a const_cast. */ | |
5562 | if (TREE_CODE (dst_type) == REFERENCE_TYPE) | |
8ccc31eb | 5563 | { |
33c25e5c | 5564 | reference_type = dst_type; |
c11b6f21 | 5565 | if (! real_lvalue_p (expr)) |
8ccc31eb | 5566 | { |
33c25e5c MM |
5567 | if (complain) |
5568 | error ("invalid const_cast of an rvalue of type %qT to type %qT", | |
5569 | src_type, dst_type); | |
8ccc31eb MS |
5570 | return error_mark_node; |
5571 | } | |
33c25e5c MM |
5572 | dst_type = build_pointer_type (TREE_TYPE (dst_type)); |
5573 | src_type = build_pointer_type (src_type); | |
5574 | } | |
5575 | else | |
5576 | { | |
5577 | reference_type = NULL_TREE; | |
5578 | /* If the destination type is not a reference type, the | |
5579 | lvalue-to-rvalue, array-to-pointer, and function-to-pointer | |
5580 | conversions are performed. */ | |
5581 | src_type = type_decays_to (src_type); | |
5582 | if (src_type == error_mark_node) | |
5583 | return error_mark_node; | |
5584 | } | |
8ccc31eb | 5585 | |
33c25e5c MM |
5586 | if ((TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type)) |
5587 | && comp_ptr_ttypes_const (dst_type, src_type)) | |
5588 | { | |
5589 | if (valid_p) | |
7735d402 MM |
5590 | { |
5591 | *valid_p = true; | |
5592 | /* This cast is actually a C-style cast. Issue a warning if | |
5593 | the user is making a potentially unsafe cast. */ | |
86b7b98b | 5594 | check_for_casting_away_constness (src_type, dst_type, CAST_EXPR); |
7735d402 | 5595 | } |
33c25e5c | 5596 | if (reference_type) |
c2840dfb | 5597 | { |
5ade1ed2 DG |
5598 | expr = cp_build_unary_op (ADDR_EXPR, expr, 0, |
5599 | complain? tf_warning_or_error : tf_none); | |
33c25e5c | 5600 | expr = build_nop (reference_type, expr); |
c2840dfb JM |
5601 | return convert_from_reference (expr); |
5602 | } | |
33c25e5c MM |
5603 | else |
5604 | { | |
5605 | expr = decay_conversion (expr); | |
5606 | /* build_c_cast puts on a NOP_EXPR to make the result not an | |
5607 | lvalue. Strip such NOP_EXPRs if VALUE is being used in | |
5608 | non-lvalue context. */ | |
5609 | if (TREE_CODE (expr) == NOP_EXPR | |
5610 | && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) | |
5611 | expr = TREE_OPERAND (expr, 0); | |
5612 | return build_nop (dst_type, expr); | |
5613 | } | |
8ccc31eb MS |
5614 | } |
5615 | ||
33c25e5c | 5616 | if (complain) |
c8094d83 | 5617 | error ("invalid const_cast from type %qT to type %qT", |
33c25e5c | 5618 | src_type, dst_type); |
c11b6f21 | 5619 | return error_mark_node; |
a4443a08 MS |
5620 | } |
5621 | ||
33c25e5c | 5622 | tree |
5ade1ed2 | 5623 | build_const_cast (tree type, tree expr, tsubst_flags_t complain) |
33c25e5c | 5624 | { |
71bd7186 | 5625 | if (type == error_mark_node || error_operand_p (expr)) |
33c25e5c | 5626 | return error_mark_node; |
6060a796 | 5627 | |
33c25e5c MM |
5628 | if (processing_template_decl) |
5629 | { | |
5630 | tree t = build_min (CONST_CAST_EXPR, type, expr); | |
c8094d83 | 5631 | |
33c25e5c MM |
5632 | if (!TREE_SIDE_EFFECTS (t) |
5633 | && type_dependent_expression_p (expr)) | |
5634 | /* There might turn out to be side effects inside expr. */ | |
5635 | TREE_SIDE_EFFECTS (t) = 1; | |
e8c66fe0 | 5636 | return convert_from_reference (t); |
33c25e5c MM |
5637 | } |
5638 | ||
5ade1ed2 | 5639 | return build_const_cast_1 (type, expr, complain & tf_error, |
33c25e5c MM |
5640 | /*valid_p=*/NULL); |
5641 | } | |
5642 | ||
525521b6 DG |
5643 | /* Like cp_build_c_cast, but for the c-common bits. */ |
5644 | ||
5645 | tree | |
5646 | build_c_cast (tree type, tree expr) | |
5647 | { | |
5648 | return cp_build_c_cast (type, expr, tf_warning_or_error); | |
5649 | } | |
5650 | ||
33c25e5c MM |
5651 | /* Build an expression representing an explicit C-style cast to type |
5652 | TYPE of expression EXPR. */ | |
8d08fdba MS |
5653 | |
5654 | tree | |
525521b6 | 5655 | cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain) |
8d08fdba | 5656 | { |
926ce8bd | 5657 | tree value = expr; |
33c25e5c MM |
5658 | tree result; |
5659 | bool valid_p; | |
8d08fdba | 5660 | |
33c25e5c | 5661 | if (type == error_mark_node || error_operand_p (expr)) |
8d08fdba MS |
5662 | return error_mark_node; |
5663 | ||
0949f723 NS |
5664 | if (processing_template_decl) |
5665 | { | |
5666 | tree t = build_min (CAST_EXPR, type, | |
5667 | tree_cons (NULL_TREE, value, NULL_TREE)); | |
04c06002 | 5668 | /* We don't know if it will or will not have side effects. */ |
8e1daa34 | 5669 | TREE_SIDE_EFFECTS (t) = 1; |
e8c66fe0 | 5670 | return convert_from_reference (t); |
0949f723 NS |
5671 | } |
5672 | ||
2e2da467 ZL |
5673 | /* Casts to a (pointer to a) specific ObjC class (or 'id' or |
5674 | 'Class') should always be retained, because this information aids | |
5675 | in method lookup. */ | |
5676 | if (objc_is_object_ptr (type) | |
5677 | && objc_is_object_ptr (TREE_TYPE (expr))) | |
5678 | return build_nop (type, expr); | |
5679 | ||
8d08fdba | 5680 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
8ccc31eb MS |
5681 | Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ |
5682 | if (TREE_CODE (type) != REFERENCE_TYPE | |
5683 | && TREE_CODE (value) == NOP_EXPR | |
8d08fdba MS |
5684 | && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0))) |
5685 | value = TREE_OPERAND (value, 0); | |
5686 | ||
8d08fdba MS |
5687 | if (TREE_CODE (type) == ARRAY_TYPE) |
5688 | { | |
5689 | /* Allow casting from T1* to T2[] because Cfront allows it. | |
cab1f180 | 5690 | NIHCL uses it. It is not valid ISO C++ however. */ |
8d08fdba MS |
5691 | if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE) |
5692 | { | |
5ade1ed2 | 5693 | if (complain & tf_error) |
cbe5f3b3 | 5694 | permerror (input_location, "ISO C++ forbids casting to an array type %qT", type); |
5ade1ed2 DG |
5695 | else |
5696 | return error_mark_node; | |
8d08fdba MS |
5697 | type = build_pointer_type (TREE_TYPE (type)); |
5698 | } | |
5699 | else | |
5700 | { | |
5ade1ed2 DG |
5701 | if (complain & tf_error) |
5702 | error ("ISO C++ forbids casting to an array type %qT", type); | |
8d08fdba MS |
5703 | return error_mark_node; |
5704 | } | |
5705 | } | |
5706 | ||
a0a33927 MS |
5707 | if (TREE_CODE (type) == FUNCTION_TYPE |
5708 | || TREE_CODE (type) == METHOD_TYPE) | |
5709 | { | |
5ade1ed2 DG |
5710 | if (complain & tf_error) |
5711 | error ("invalid cast to function type %qT", type); | |
a0a33927 MS |
5712 | return error_mark_node; |
5713 | } | |
5714 | ||
33c25e5c MM |
5715 | /* A C-style cast can be a const_cast. */ |
5716 | result = build_const_cast_1 (type, value, /*complain=*/false, | |
5717 | &valid_p); | |
5718 | if (valid_p) | |
5719 | return result; | |
8d08fdba | 5720 | |
33c25e5c MM |
5721 | /* Or a static cast. */ |
5722 | result = build_static_cast_1 (type, value, /*c_cast_p=*/true, | |
5ade1ed2 | 5723 | &valid_p, complain); |
33c25e5c MM |
5724 | /* Or a reinterpret_cast. */ |
5725 | if (!valid_p) | |
5726 | result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true, | |
5ade1ed2 | 5727 | &valid_p, complain); |
33c25e5c MM |
5728 | /* The static_cast or reinterpret_cast may be followed by a |
5729 | const_cast. */ | |
c8094d83 | 5730 | if (valid_p |
33c25e5c MM |
5731 | /* A valid cast may result in errors if, for example, a |
5732 | conversion to am ambiguous base class is required. */ | |
5733 | && !error_operand_p (result)) | |
e6e174e5 | 5734 | { |
33c25e5c | 5735 | tree result_type; |
8ccc31eb | 5736 | |
33c25e5c MM |
5737 | /* Non-class rvalues always have cv-unqualified type. */ |
5738 | if (!CLASS_TYPE_P (type)) | |
5739 | type = TYPE_MAIN_VARIANT (type); | |
5740 | result_type = TREE_TYPE (result); | |
5741 | if (!CLASS_TYPE_P (result_type)) | |
5742 | result_type = TYPE_MAIN_VARIANT (result_type); | |
5743 | /* If the type of RESULT does not match TYPE, perform a | |
5744 | const_cast to make it match. If the static_cast or | |
5745 | reinterpret_cast succeeded, we will differ by at most | |
5746 | cv-qualification, so the follow-on const_cast is guaranteed | |
5747 | to succeed. */ | |
5748 | if (!same_type_p (non_reference (type), non_reference (result_type))) | |
e6e174e5 | 5749 | { |
33c25e5c MM |
5750 | result = build_const_cast_1 (type, result, false, &valid_p); |
5751 | gcc_assert (valid_p); | |
8d08fdba | 5752 | } |
33c25e5c | 5753 | return result; |
8d08fdba MS |
5754 | } |
5755 | ||
33c25e5c | 5756 | return error_mark_node; |
8d08fdba MS |
5757 | } |
5758 | \f | |
5ade1ed2 DG |
5759 | /* For use from the C common bits. */ |
5760 | tree | |
c9f9eb5d AH |
5761 | build_modify_expr (location_t location ATTRIBUTE_UNUSED, |
5762 | tree lhs, enum tree_code modifycode, tree rhs) | |
5ade1ed2 DG |
5763 | { |
5764 | return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error); | |
5765 | } | |
5766 | ||
8d08fdba MS |
5767 | /* Build an assignment expression of lvalue LHS from value RHS. |
5768 | MODIFYCODE is the code for a binary operator that we use | |
5769 | to combine the old value of LHS with RHS to get the new value. | |
5770 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. | |
5771 | ||
e92cc029 MS |
5772 | C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */ |
5773 | ||
8d08fdba | 5774 | tree |
5ade1ed2 DG |
5775 | cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs, |
5776 | tsubst_flags_t complain) | |
8d08fdba | 5777 | { |
926ce8bd | 5778 | tree result; |
8d08fdba MS |
5779 | tree newrhs = rhs; |
5780 | tree lhstype = TREE_TYPE (lhs); | |
5781 | tree olhstype = lhstype; | |
60cd3e8b | 5782 | tree olhs = NULL_TREE; |
487a92fe | 5783 | bool plain_assign = (modifycode == NOP_EXPR); |
8d08fdba | 5784 | |
8d08fdba | 5785 | /* Avoid duplicate error messages from operands that had errors. */ |
1bd229b7 | 5786 | if (error_operand_p (lhs) || error_operand_p (rhs)) |
8d08fdba MS |
5787 | return error_mark_node; |
5788 | ||
8d08fdba | 5789 | /* Handle control structure constructs used as "lvalues". */ |
8d08fdba MS |
5790 | switch (TREE_CODE (lhs)) |
5791 | { | |
f4f206f4 | 5792 | /* Handle --foo = 5; as these are valid constructs in C++. */ |
8d08fdba MS |
5793 | case PREDECREMENT_EXPR: |
5794 | case PREINCREMENT_EXPR: | |
5795 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))) | |
f293ce4b RS |
5796 | lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs), |
5797 | stabilize_reference (TREE_OPERAND (lhs, 0)), | |
5798 | TREE_OPERAND (lhs, 1)); | |
f2be060f JJ |
5799 | newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), |
5800 | modifycode, rhs, complain); | |
5801 | if (newrhs == error_mark_node) | |
5802 | return error_mark_node; | |
5803 | return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs); | |
8d08fdba MS |
5804 | |
5805 | /* Handle (a, b) used as an "lvalue". */ | |
5806 | case COMPOUND_EXPR: | |
5ade1ed2 DG |
5807 | newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1), |
5808 | modifycode, rhs, complain); | |
bd6dd845 | 5809 | if (newrhs == error_mark_node) |
8d08fdba | 5810 | return error_mark_node; |
f293ce4b RS |
5811 | return build2 (COMPOUND_EXPR, lhstype, |
5812 | TREE_OPERAND (lhs, 0), newrhs); | |
8d08fdba | 5813 | |
a4443a08 | 5814 | case MODIFY_EXPR: |
a65cddcf | 5815 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))) |
f293ce4b RS |
5816 | lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs), |
5817 | stabilize_reference (TREE_OPERAND (lhs, 0)), | |
5818 | TREE_OPERAND (lhs, 1)); | |
5ade1ed2 DG |
5819 | newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs, |
5820 | complain); | |
bd6dd845 | 5821 | if (newrhs == error_mark_node) |
a4443a08 | 5822 | return error_mark_node; |
f293ce4b | 5823 | return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs); |
a4443a08 | 5824 | |
d211a298 RS |
5825 | case MIN_EXPR: |
5826 | case MAX_EXPR: | |
5827 | /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues, | |
5828 | when neither operand has side-effects. */ | |
5ade1ed2 | 5829 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
d211a298 RS |
5830 | return error_mark_node; |
5831 | ||
5832 | gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)) | |
5833 | && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1))); | |
5834 | ||
5835 | lhs = build3 (COND_EXPR, TREE_TYPE (lhs), | |
5836 | build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR, | |
5837 | boolean_type_node, | |
5838 | TREE_OPERAND (lhs, 0), | |
5839 | TREE_OPERAND (lhs, 1)), | |
5840 | TREE_OPERAND (lhs, 0), | |
5841 | TREE_OPERAND (lhs, 1)); | |
5842 | /* Fall through. */ | |
5843 | ||
8d08fdba MS |
5844 | /* Handle (a ? b : c) used as an "lvalue". */ |
5845 | case COND_EXPR: | |
8d08fdba MS |
5846 | { |
5847 | /* Produce (a ? (b = rhs) : (c = rhs)) | |
5848 | except that the RHS goes through a save-expr | |
5849 | so the code to compute it is only emitted once. */ | |
df39af7d | 5850 | tree cond; |
a77a9a18 | 5851 | tree preeval = NULL_TREE; |
df39af7d | 5852 | |
df3473fa JJ |
5853 | if (VOID_TYPE_P (TREE_TYPE (rhs))) |
5854 | { | |
5ade1ed2 DG |
5855 | if (complain & tf_error) |
5856 | error ("void value not ignored as it ought to be"); | |
df3473fa JJ |
5857 | return error_mark_node; |
5858 | } | |
5859 | ||
a77a9a18 | 5860 | rhs = stabilize_expr (rhs, &preeval); |
c8094d83 | 5861 | |
df39af7d JM |
5862 | /* Check this here to avoid odd errors when trying to convert |
5863 | a throw to the type of the COND_EXPR. */ | |
5ade1ed2 | 5864 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
df39af7d JM |
5865 | return error_mark_node; |
5866 | ||
5867 | cond = build_conditional_expr | |
5868 | (TREE_OPERAND (lhs, 0), | |
5ade1ed2 DG |
5869 | cp_build_modify_expr (TREE_OPERAND (lhs, 1), |
5870 | modifycode, rhs, complain), | |
5871 | cp_build_modify_expr (TREE_OPERAND (lhs, 2), | |
5872 | modifycode, rhs, complain), | |
5873 | complain); | |
df39af7d | 5874 | |
bd6dd845 | 5875 | if (cond == error_mark_node) |
8d08fdba MS |
5876 | return cond; |
5877 | /* Make sure the code to compute the rhs comes out | |
5878 | before the split. */ | |
6de9cd9a | 5879 | if (preeval) |
f293ce4b | 5880 | cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond); |
6de9cd9a | 5881 | return cond; |
8d08fdba | 5882 | } |
c8094d83 | 5883 | |
7f85441b KG |
5884 | default: |
5885 | break; | |
8d08fdba MS |
5886 | } |
5887 | ||
8d08fdba MS |
5888 | if (modifycode == INIT_EXPR) |
5889 | { | |
f30efcb7 JM |
5890 | if (TREE_CODE (rhs) == CONSTRUCTOR) |
5891 | { | |
182609b5 JM |
5892 | if (! same_type_p (TREE_TYPE (rhs), lhstype)) |
5893 | /* Call convert to generate an error; see PR 11063. */ | |
5894 | rhs = convert (lhstype, rhs); | |
f293ce4b | 5895 | result = build2 (INIT_EXPR, lhstype, lhs, rhs); |
f30efcb7 JM |
5896 | TREE_SIDE_EFFECTS (result) = 1; |
5897 | return result; | |
5898 | } | |
9e1e64ec | 5899 | else if (! MAYBE_CLASS_TYPE_P (lhstype)) |
f4f206f4 | 5900 | /* Do the default thing. */; |
db5ae43f | 5901 | else |
8d08fdba | 5902 | { |
4ba126e4 MM |
5903 | result = build_special_member_call (lhs, complete_ctor_identifier, |
5904 | build_tree_list (NULL_TREE, rhs), | |
5ade1ed2 DG |
5905 | lhstype, LOOKUP_NORMAL, |
5906 | complain); | |
8d08fdba MS |
5907 | if (result == NULL_TREE) |
5908 | return error_mark_node; | |
5909 | return result; | |
5910 | } | |
5911 | } | |
a56ca899 | 5912 | else |
8d08fdba | 5913 | { |
a56ca899 NS |
5914 | lhs = require_complete_type (lhs); |
5915 | if (lhs == error_mark_node) | |
5916 | return error_mark_node; | |
5917 | ||
5918 | if (modifycode == NOP_EXPR) | |
c91a56d2 | 5919 | { |
a56ca899 | 5920 | /* `operator=' is not an inheritable operator. */ |
9e1e64ec | 5921 | if (! MAYBE_CLASS_TYPE_P (lhstype)) |
f4f206f4 | 5922 | /* Do the default thing. */; |
a56ca899 NS |
5923 | else |
5924 | { | |
14d22dd6 | 5925 | result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, |
ec835fb2 | 5926 | lhs, rhs, make_node (NOP_EXPR), |
5ade1ed2 DG |
5927 | /*overloaded_p=*/NULL, |
5928 | complain); | |
a56ca899 NS |
5929 | if (result == NULL_TREE) |
5930 | return error_mark_node; | |
5931 | return result; | |
5932 | } | |
5933 | lhstype = olhstype; | |
5934 | } | |
5935 | else | |
5936 | { | |
5937 | /* A binary op has been requested. Combine the old LHS | |
0cbd7506 MS |
5938 | value with the RHS producing the value we should actually |
5939 | store into the LHS. */ | |
9e1e64ec PC |
5940 | gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE |
5941 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype))) | |
5942 | || MAYBE_CLASS_TYPE_P (lhstype))); | |
a56ca899 | 5943 | |
a56ca899 | 5944 | lhs = stabilize_reference (lhs); |
c9f9eb5d | 5945 | newrhs = cp_build_binary_op (input_location, |
ba47d38d | 5946 | modifycode, lhs, rhs, |
5ade1ed2 | 5947 | complain); |
a56ca899 NS |
5948 | if (newrhs == error_mark_node) |
5949 | { | |
5ade1ed2 DG |
5950 | if (complain & tf_error) |
5951 | error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode, | |
5952 | TREE_TYPE (lhs), TREE_TYPE (rhs)); | |
a56ca899 NS |
5953 | return error_mark_node; |
5954 | } | |
c8094d83 | 5955 | |
a56ca899 NS |
5956 | /* Now it looks like a plain assignment. */ |
5957 | modifycode = NOP_EXPR; | |
c91a56d2 | 5958 | } |
50bc768d NS |
5959 | gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE); |
5960 | gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE); | |
8d08fdba MS |
5961 | } |
5962 | ||
719f407a | 5963 | /* The left-hand side must be an lvalue. */ |
5ade1ed2 | 5964 | if (!lvalue_or_else (lhs, lv_assign, complain)) |
8d08fdba MS |
5965 | return error_mark_node; |
5966 | ||
a56ca899 NS |
5967 | /* Warn about modifying something that is `const'. Don't warn if |
5968 | this is initialization. */ | |
8d08fdba | 5969 | if (modifycode != INIT_EXPR |
91063b51 | 5970 | && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype) |
69851283 MM |
5971 | /* Functions are not modifiable, even though they are |
5972 | lvalues. */ | |
5973 | || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE | |
a56ca899 NS |
5974 | || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE |
5975 | /* If it's an aggregate and any field is const, then it is | |
5976 | effectively const. */ | |
5552b43c | 5977 | || (CLASS_TYPE_P (lhstype) |
a56ca899 | 5978 | && C_TYPE_FIELDS_READONLY (lhstype)))) |
5ade1ed2 DG |
5979 | { |
5980 | if (complain & tf_error) | |
5981 | readonly_error (lhs, "assignment"); | |
5982 | else | |
5983 | return error_mark_node; | |
5984 | } | |
8d08fdba | 5985 | |
a56ca899 NS |
5986 | /* If storing into a structure or union member, it has probably been |
5987 | given type `int'. Compute the type that would go with the actual | |
5988 | amount of storage the member occupies. */ | |
8d08fdba MS |
5989 | |
5990 | if (TREE_CODE (lhs) == COMPONENT_REF | |
5991 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
5992 | || TREE_CODE (lhstype) == REAL_TYPE | |
5993 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
a0a33927 MS |
5994 | { |
5995 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
5996 | ||
5997 | /* If storing in a field that is in actuality a short or narrower | |
5998 | than one, we must store in the field in its actual type. */ | |
5999 | ||
6000 | if (lhstype != TREE_TYPE (lhs)) | |
6001 | { | |
60cd3e8b JM |
6002 | /* Avoid warnings converting integral types back into enums for |
6003 | enum bit fields. */ | |
6004 | if (TREE_CODE (lhstype) == INTEGER_TYPE | |
6005 | && TREE_CODE (olhstype) == ENUMERAL_TYPE) | |
6006 | { | |
6007 | if (TREE_SIDE_EFFECTS (lhs)) | |
6008 | lhs = stabilize_reference (lhs); | |
6009 | olhs = lhs; | |
6010 | } | |
a0a33927 MS |
6011 | lhs = copy_node (lhs); |
6012 | TREE_TYPE (lhs) = lhstype; | |
6013 | } | |
6014 | } | |
8d08fdba | 6015 | |
8d08fdba MS |
6016 | /* Convert new value to destination type. */ |
6017 | ||
6018 | if (TREE_CODE (lhstype) == ARRAY_TYPE) | |
6019 | { | |
f376e137 | 6020 | int from_array; |
c8094d83 | 6021 | |
985723ce JM |
6022 | if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype), |
6023 | TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))) | |
44a8d0b3 | 6024 | { |
5ade1ed2 DG |
6025 | if (complain & tf_error) |
6026 | error ("incompatible types in assignment of %qT to %qT", | |
6027 | TREE_TYPE (rhs), lhstype); | |
44a8d0b3 MS |
6028 | return error_mark_node; |
6029 | } | |
6030 | ||
39211cd5 | 6031 | /* Allow array assignment in compiler-generated code. */ |
a49cfba8 | 6032 | if (! DECL_ARTIFICIAL (current_function_decl)) |
cf24598a | 6033 | { |
decebe51 GDR |
6034 | /* This routine is used for both initialization and assignment. |
6035 | Make sure the diagnostic message differentiates the context. */ | |
5ade1ed2 DG |
6036 | if (complain & tf_error) |
6037 | { | |
6038 | if (modifycode == INIT_EXPR) | |
6039 | error ("array used as initializer"); | |
6040 | else | |
6041 | error ("invalid array assignment"); | |
6042 | } | |
cf24598a MM |
6043 | return error_mark_node; |
6044 | } | |
39211cd5 | 6045 | |
f376e137 | 6046 | from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE |
0cbd7506 | 6047 | ? 1 + (modifycode != INIT_EXPR): 0; |
3db45ab5 | 6048 | return build_vec_init (lhs, NULL_TREE, newrhs, |
844ae01d | 6049 | /*explicit_value_init_p=*/false, |
5ade1ed2 | 6050 | from_array, complain); |
8d08fdba MS |
6051 | } |
6052 | ||
6053 | if (modifycode == INIT_EXPR) | |
a56ca899 | 6054 | newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL, |
5ade1ed2 DG |
6055 | "initialization", NULL_TREE, 0, |
6056 | complain); | |
8d08fdba MS |
6057 | else |
6058 | { | |
e92cc029 | 6059 | /* Avoid warnings on enum bit fields. */ |
8d08fdba MS |
6060 | if (TREE_CODE (olhstype) == ENUMERAL_TYPE |
6061 | && TREE_CODE (lhstype) == INTEGER_TYPE) | |
6062 | { | |
6063 | newrhs = convert_for_assignment (olhstype, newrhs, "assignment", | |
5ade1ed2 | 6064 | NULL_TREE, 0, complain); |
6060a796 | 6065 | newrhs = convert_force (lhstype, newrhs, 0); |
8d08fdba MS |
6066 | } |
6067 | else | |
6068 | newrhs = convert_for_assignment (lhstype, newrhs, "assignment", | |
5ade1ed2 | 6069 | NULL_TREE, 0, complain); |
b7484fbe MS |
6070 | if (TREE_CODE (newrhs) == CALL_EXPR |
6071 | && TYPE_NEEDS_CONSTRUCTING (lhstype)) | |
5566b478 | 6072 | newrhs = build_cplus_new (lhstype, newrhs); |
b7484fbe | 6073 | |
e8abc66f | 6074 | /* Can't initialize directly from a TARGET_EXPR, since that would |
07674418 MS |
6075 | cause the lhs to be constructed twice, and possibly result in |
6076 | accidental self-initialization. So we force the TARGET_EXPR to be | |
be99da77 | 6077 | expanded without a target. */ |
b7484fbe | 6078 | if (TREE_CODE (newrhs) == TARGET_EXPR) |
f293ce4b RS |
6079 | newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs, |
6080 | TREE_OPERAND (newrhs, 0)); | |
8d08fdba MS |
6081 | } |
6082 | ||
bd6dd845 | 6083 | if (newrhs == error_mark_node) |
8d08fdba MS |
6084 | return error_mark_node; |
6085 | ||
e58a9aa1 ZL |
6086 | if (c_dialect_objc () && flag_objc_gc) |
6087 | { | |
6088 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
6089 | ||
6090 | if (result) | |
6091 | return result; | |
6092 | } | |
6093 | ||
f293ce4b RS |
6094 | result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR, |
6095 | lhstype, lhs, newrhs); | |
2ee887f2 | 6096 | |
8d08fdba | 6097 | TREE_SIDE_EFFECTS (result) = 1; |
487a92fe JM |
6098 | if (!plain_assign) |
6099 | TREE_NO_WARNING (result) = 1; | |
8d08fdba MS |
6100 | |
6101 | /* If we got the LHS in a different type for storing in, | |
6102 | convert the result back to the nominal type of LHS | |
6103 | so that the value we return always has the same type | |
6104 | as the LHS argument. */ | |
6105 | ||
6106 | if (olhstype == TREE_TYPE (result)) | |
6107 | return result; | |
60cd3e8b | 6108 | if (olhs) |
a3203465 | 6109 | { |
f293ce4b | 6110 | result = build2 (COMPOUND_EXPR, olhstype, result, olhs); |
6de9cd9a | 6111 | TREE_NO_WARNING (result) = 1; |
a3203465 MS |
6112 | return result; |
6113 | } | |
8d08fdba | 6114 | return convert_for_assignment (olhstype, result, "assignment", |
5ade1ed2 | 6115 | NULL_TREE, 0, complain); |
8d08fdba MS |
6116 | } |
6117 | ||
5566b478 | 6118 | tree |
5ade1ed2 DG |
6119 | build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs, |
6120 | tsubst_flags_t complain) | |
5566b478 | 6121 | { |
5156628f | 6122 | if (processing_template_decl) |
5566b478 | 6123 | return build_min_nt (MODOP_EXPR, lhs, |
4dabb379 | 6124 | build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs); |
5566b478 MS |
6125 | |
6126 | if (modifycode != NOP_EXPR) | |
6127 | { | |
14d22dd6 | 6128 | tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs, |
ec835fb2 | 6129 | make_node (modifycode), |
5ade1ed2 DG |
6130 | /*overloaded_p=*/NULL, |
6131 | complain); | |
5566b478 | 6132 | if (rval) |
487a92fe JM |
6133 | { |
6134 | TREE_NO_WARNING (rval) = 1; | |
6135 | return rval; | |
6136 | } | |
5566b478 | 6137 | } |
5ade1ed2 | 6138 | return cp_build_modify_expr (lhs, modifycode, rhs, complain); |
5566b478 | 6139 | } |
8d08fdba | 6140 | |
6e03b280 OW |
6141 | /* Helper function for get_delta_difference which assumes FROM is a base |
6142 | class of TO. Returns a delta for the conversion of pointer-to-member | |
6143 | of FROM to pointer-to-member of TO. If the conversion is invalid, | |
6144 | returns zero. If FROM is not a base class of TO, returns NULL_TREE. | |
6145 | If C_CAST_P is true, this conversion is taking place as part of a C-style | |
6146 | cast. */ | |
6147 | ||
6148 | static tree | |
6149 | get_delta_difference_1 (tree from, tree to, bool c_cast_p) | |
6150 | { | |
6151 | tree binfo; | |
6152 | base_kind kind; | |
6153 | ||
6154 | binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check, &kind); | |
6155 | if (kind == bk_inaccessible || kind == bk_ambig) | |
6156 | { | |
6157 | error (" in pointer to member function conversion"); | |
6158 | return size_zero_node; | |
6159 | } | |
6160 | else if (binfo) | |
6161 | { | |
6162 | if (kind != bk_via_virtual) | |
6163 | return BINFO_OFFSET (binfo); | |
6164 | else | |
6165 | /* FROM is a virtual base class of TO. Issue an error or warning | |
6166 | depending on whether or not this is a reinterpret cast. */ | |
6167 | { | |
6168 | error ("pointer to member conversion via virtual base %qT", | |
6169 | BINFO_TYPE (binfo_from_vbase (binfo))); | |
6170 | ||
6171 | return size_zero_node; | |
6172 | } | |
6173 | } | |
6174 | else | |
6175 | return NULL_TREE; | |
6176 | } | |
6177 | ||
ddd5a7c1 | 6178 | /* Get difference in deltas for different pointer to member function |
7b6d72fc | 6179 | types. Returns an integer constant of type PTRDIFF_TYPE_NODE. If |
08e17d9d MM |
6180 | the conversion is invalid, the constant is zero. If |
6181 | ALLOW_INVERSE_P is true, then allow reverse conversions as well. | |
6182 | If C_CAST_P is true this conversion is taking place as part of a | |
6183 | C-style cast. | |
3927874d JM |
6184 | |
6185 | Note that the naming of FROM and TO is kind of backwards; the return | |
6186 | value is what we add to a TO in order to get a FROM. They are named | |
6187 | this way because we call this function to find out how to convert from | |
6188 | a pointer to member of FROM to a pointer to member of TO. */ | |
e92cc029 | 6189 | |
51c184be | 6190 | static tree |
c8094d83 | 6191 | get_delta_difference (tree from, tree to, |
08e17d9d MM |
6192 | bool allow_inverse_p, |
6193 | bool c_cast_p) | |
51c184be | 6194 | { |
e8632777 MM |
6195 | tree result; |
6196 | ||
6e03b280 OW |
6197 | if (same_type_ignoring_top_level_qualifiers_p (from, to)) |
6198 | /* Pointer to member of incomplete class is permitted*/ | |
6199 | result = size_zero_node; | |
f879d298 | 6200 | else |
6e03b280 | 6201 | result = get_delta_difference_1 (from, to, c_cast_p); |
c8094d83 | 6202 | |
6e03b280 OW |
6203 | if (!result) |
6204 | { | |
6205 | if (!allow_inverse_p) | |
6206 | { | |
6207 | error_not_base_type (from, to); | |
6208 | error (" in pointer to member conversion"); | |
6209 | result = size_zero_node; | |
6210 | } | |
6211 | else | |
6212 | { | |
6213 | result = get_delta_difference_1 (to, from, c_cast_p); | |
6214 | ||
6215 | if (result) | |
6216 | result = size_diffop (size_zero_node, result); | |
6217 | else | |
6218 | { | |
6219 | error_not_base_type (from, to); | |
6220 | error (" in pointer to member conversion"); | |
6221 | result = size_zero_node; | |
6222 | } | |
6223 | } | |
6224 | } | |
32e02ee0 | 6225 | |
c8094d83 | 6226 | return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node, |
455f19cb | 6227 | result)); |
51c184be MS |
6228 | } |
6229 | ||
c7e266a6 MM |
6230 | /* Return a constructor for the pointer-to-member-function TYPE using |
6231 | the other components as specified. */ | |
6232 | ||
e08a8f45 | 6233 | tree |
acd8e2d0 | 6234 | build_ptrmemfunc1 (tree type, tree delta, tree pfn) |
594740f3 | 6235 | { |
f78c7bc6 | 6236 | tree u = NULL_TREE; |
c7e266a6 | 6237 | tree delta_field; |
c7e266a6 | 6238 | tree pfn_field; |
4038c495 | 6239 | VEC(constructor_elt, gc) *v; |
594740f3 | 6240 | |
c7e266a6 | 6241 | /* Pull the FIELD_DECLs out of the type. */ |
1f84ec23 MM |
6242 | pfn_field = TYPE_FIELDS (type); |
6243 | delta_field = TREE_CHAIN (pfn_field); | |
594740f3 | 6244 | |
c7e266a6 MM |
6245 | /* Make sure DELTA has the type we want. */ |
6246 | delta = convert_and_check (delta_type_node, delta); | |
594740f3 | 6247 | |
a489b1f0 RG |
6248 | /* Convert to the correct target type if necessary. */ |
6249 | pfn = fold_convert (TREE_TYPE (pfn_field), pfn); | |
6250 | ||
c7e266a6 | 6251 | /* Finish creating the initializer. */ |
4038c495 GB |
6252 | v = VEC_alloc(constructor_elt, gc, 2); |
6253 | CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn); | |
6254 | CONSTRUCTOR_APPEND_ELT(v, delta_field, delta); | |
6255 | u = build_constructor (type, v); | |
6de9cd9a | 6256 | TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta); |
530ec96d MM |
6257 | TREE_STATIC (u) = (TREE_CONSTANT (u) |
6258 | && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn)) | |
6259 | != NULL_TREE) | |
c8094d83 | 6260 | && (initializer_constant_valid_p (delta, TREE_TYPE (delta)) |
530ec96d | 6261 | != NULL_TREE)); |
594740f3 | 6262 | return u; |
594740f3 MS |
6263 | } |
6264 | ||
8d08fdba MS |
6265 | /* Build a constructor for a pointer to member function. It can be |
6266 | used to initialize global variables, local variable, or used | |
6267 | as a value in expressions. TYPE is the POINTER to METHOD_TYPE we | |
6268 | want to be. | |
6269 | ||
838dfd8a | 6270 | If FORCE is nonzero, then force this conversion, even if |
8d08fdba | 6271 | we would rather not do it. Usually set when using an explicit |
08e17d9d | 6272 | cast. A C-style cast is being processed iff C_CAST_P is true. |
51c184be MS |
6273 | |
6274 | Return error_mark_node, if something goes wrong. */ | |
8d08fdba MS |
6275 | |
6276 | tree | |
08e17d9d | 6277 | build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p) |
8d08fdba | 6278 | { |
e08a8f45 | 6279 | tree fn; |
7993382e MM |
6280 | tree pfn_type; |
6281 | tree to_type; | |
6282 | ||
6283 | if (error_operand_p (pfn)) | |
6284 | return error_mark_node; | |
6285 | ||
6286 | pfn_type = TREE_TYPE (pfn); | |
6287 | to_type = build_ptrmemfunc_type (type); | |
b928a651 | 6288 | |
e92cc029 | 6289 | /* Handle multiple conversions of pointer to member functions. */ |
7993382e | 6290 | if (TYPE_PTRMEMFUNC_P (pfn_type)) |
8d08fdba | 6291 | { |
530ec96d | 6292 | tree delta = NULL_TREE; |
7133357a | 6293 | tree npfn = NULL_TREE; |
1f84ec23 | 6294 | tree n; |
594740f3 | 6295 | |
c8094d83 | 6296 | if (!force |
30f86ec3 | 6297 | && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, LOOKUP_NORMAL)) |
c8094d83 | 6298 | error ("invalid conversion to type %qT from type %qT", |
0cbd7506 | 6299 | to_type, pfn_type); |
9ca21c0a | 6300 | |
3f2b640a JM |
6301 | n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type), |
6302 | TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type), | |
08e17d9d MM |
6303 | force, |
6304 | c_cast_p); | |
3f2b640a | 6305 | |
c7e266a6 MM |
6306 | /* We don't have to do any conversion to convert a |
6307 | pointer-to-member to its own type. But, we don't want to | |
6308 | just return a PTRMEM_CST if there's an explicit cast; that | |
6309 | cast should make the expression an invalid template argument. */ | |
3f2b640a JM |
6310 | if (TREE_CODE (pfn) != PTRMEM_CST) |
6311 | { | |
6312 | if (same_type_p (to_type, pfn_type)) | |
6313 | return pfn; | |
6314 | else if (integer_zerop (n)) | |
5ade1ed2 DG |
6315 | return build_reinterpret_cast (to_type, pfn, |
6316 | tf_warning_or_error); | |
3f2b640a | 6317 | } |
c7e266a6 | 6318 | |
7def1251 JM |
6319 | if (TREE_SIDE_EFFECTS (pfn)) |
6320 | pfn = save_expr (pfn); | |
6321 | ||
530ec96d MM |
6322 | /* Obtain the function pointer and the current DELTA. */ |
6323 | if (TREE_CODE (pfn) == PTRMEM_CST) | |
18ae7f63 | 6324 | expand_ptrmemfunc_cst (pfn, &delta, &npfn); |
530ec96d MM |
6325 | else |
6326 | { | |
50ad9642 MM |
6327 | npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier); |
6328 | delta = build_ptrmemfunc_access_expr (pfn, delta_identifier); | |
530ec96d MM |
6329 | } |
6330 | ||
3461fba7 | 6331 | /* Just adjust the DELTA field. */ |
50bc768d NS |
6332 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
6333 | (TREE_TYPE (delta), ptrdiff_type_node)); | |
cb7fdde2 | 6334 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta) |
ba47d38d AH |
6335 | n = cp_build_binary_op (input_location, |
6336 | LSHIFT_EXPR, n, integer_one_node, | |
5ade1ed2 | 6337 | tf_warning_or_error); |
ba47d38d AH |
6338 | delta = cp_build_binary_op (input_location, |
6339 | PLUS_EXPR, delta, n, tf_warning_or_error); | |
18ae7f63 | 6340 | return build_ptrmemfunc1 (to_type, delta, npfn); |
8d08fdba | 6341 | } |
51c184be | 6342 | |
e92cc029 | 6343 | /* Handle null pointer to member function conversions. */ |
51c184be MS |
6344 | if (integer_zerop (pfn)) |
6345 | { | |
525521b6 | 6346 | pfn = build_c_cast (type, integer_zero_node); |
b928a651 | 6347 | return build_ptrmemfunc1 (to_type, |
c8094d83 | 6348 | integer_zero_node, |
18ae7f63 | 6349 | pfn); |
51c184be MS |
6350 | } |
6351 | ||
e6e174e5 | 6352 | if (type_unknown_p (pfn)) |
23fca1f5 | 6353 | return instantiate_type (type, pfn, tf_warning_or_error); |
a0a33927 | 6354 | |
e08a8f45 | 6355 | fn = TREE_OPERAND (pfn, 0); |
37048601 NS |
6356 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL |
6357 | /* In a template, we will have preserved the | |
6358 | OFFSET_REF. */ | |
6359 | || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF)); | |
b928a651 | 6360 | return make_ptrmem_cst (to_type, fn); |
e08a8f45 | 6361 | } |
8d08fdba | 6362 | |
18ae7f63 | 6363 | /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST |
fed3cef0 RK |
6364 | given by CST. |
6365 | ||
6366 | ??? There is no consistency as to the types returned for the above | |
852dcbdd | 6367 | values. Some code acts as if it were a sizetype and some as if it were |
fed3cef0 | 6368 | integer_type_node. */ |
8d08fdba | 6369 | |
e08a8f45 | 6370 | void |
acd8e2d0 | 6371 | expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn) |
e08a8f45 MM |
6372 | { |
6373 | tree type = TREE_TYPE (cst); | |
6374 | tree fn = PTRMEM_CST_MEMBER (cst); | |
3927874d | 6375 | tree ptr_class, fn_class; |
8d08fdba | 6376 | |
50bc768d | 6377 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); |
3927874d JM |
6378 | |
6379 | /* The class that the function belongs to. */ | |
4f1c5b7d | 6380 | fn_class = DECL_CONTEXT (fn); |
3927874d JM |
6381 | |
6382 | /* The class that we're creating a pointer to member of. */ | |
6383 | ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type); | |
6384 | ||
6385 | /* First, calculate the adjustment to the function's class. */ | |
08e17d9d MM |
6386 | *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0, |
6387 | /*c_cast_p=*/0); | |
3927874d | 6388 | |
e08a8f45 | 6389 | if (!DECL_VIRTUAL_P (fn)) |
18ae7f63 | 6390 | *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn)); |
8d08fdba | 6391 | else |
51c184be | 6392 | { |
3927874d | 6393 | /* If we're dealing with a virtual function, we have to adjust 'this' |
0cbd7506 MS |
6394 | again, to point to the base which provides the vtable entry for |
6395 | fn; the call will do the opposite adjustment. */ | |
e93ee644 | 6396 | tree orig_class = DECL_CONTEXT (fn); |
3927874d | 6397 | tree binfo = binfo_or_else (orig_class, fn_class); |
455f19cb MM |
6398 | *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta), |
6399 | *delta, BINFO_OFFSET (binfo)); | |
6400 | *delta = fold_if_not_in_template (*delta); | |
3927874d | 6401 | |
3461fba7 NS |
6402 | /* We set PFN to the vtable offset at which the function can be |
6403 | found, plus one (unless ptrmemfunc_vbit_in_delta, in which | |
6404 | case delta is shifted left, and then incremented). */ | |
cb7fdde2 | 6405 | *pfn = DECL_VINDEX (fn); |
455f19cb MM |
6406 | *pfn = build2 (MULT_EXPR, integer_type_node, *pfn, |
6407 | TYPE_SIZE_UNIT (vtable_entry_type)); | |
6408 | *pfn = fold_if_not_in_template (*pfn); | |
cb7fdde2 AO |
6409 | |
6410 | switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) | |
6411 | { | |
6412 | case ptrmemfunc_vbit_in_pfn: | |
455f19cb MM |
6413 | *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn, |
6414 | integer_one_node); | |
6415 | *pfn = fold_if_not_in_template (*pfn); | |
cb7fdde2 AO |
6416 | break; |
6417 | ||
6418 | case ptrmemfunc_vbit_in_delta: | |
455f19cb MM |
6419 | *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta), |
6420 | *delta, integer_one_node); | |
6421 | *delta = fold_if_not_in_template (*delta); | |
6422 | *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta), | |
6423 | *delta, integer_one_node); | |
6424 | *delta = fold_if_not_in_template (*delta); | |
cb7fdde2 AO |
6425 | break; |
6426 | ||
6427 | default: | |
315fb5db | 6428 | gcc_unreachable (); |
cb7fdde2 AO |
6429 | } |
6430 | ||
455f19cb MM |
6431 | *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn); |
6432 | *pfn = fold_if_not_in_template (*pfn); | |
e08a8f45 MM |
6433 | } |
6434 | } | |
8d08fdba | 6435 | |
e08a8f45 MM |
6436 | /* Return an expression for PFN from the pointer-to-member function |
6437 | given by T. */ | |
6438 | ||
b5791fdc | 6439 | static tree |
acd8e2d0 | 6440 | pfn_from_ptrmemfunc (tree t) |
e08a8f45 MM |
6441 | { |
6442 | if (TREE_CODE (t) == PTRMEM_CST) | |
6443 | { | |
6444 | tree delta; | |
e08a8f45 | 6445 | tree pfn; |
c8094d83 | 6446 | |
18ae7f63 | 6447 | expand_ptrmemfunc_cst (t, &delta, &pfn); |
e08a8f45 MM |
6448 | if (pfn) |
6449 | return pfn; | |
51c184be | 6450 | } |
8d08fdba | 6451 | |
50ad9642 | 6452 | return build_ptrmemfunc_access_expr (t, pfn_identifier); |
8d08fdba MS |
6453 | } |
6454 | ||
a298680c OW |
6455 | /* Return an expression for DELTA from the pointer-to-member function |
6456 | given by T. */ | |
6457 | ||
6458 | static tree | |
6459 | delta_from_ptrmemfunc (tree t) | |
6460 | { | |
6461 | if (TREE_CODE (t) == PTRMEM_CST) | |
6462 | { | |
6463 | tree delta; | |
6464 | tree pfn; | |
6465 | ||
6466 | expand_ptrmemfunc_cst (t, &delta, &pfn); | |
6467 | if (delta) | |
6468 | return delta; | |
6469 | } | |
6470 | ||
6471 | return build_ptrmemfunc_access_expr (t, delta_identifier); | |
6472 | } | |
6473 | ||
a7a64a77 MM |
6474 | /* Convert value RHS to type TYPE as preparation for an assignment to |
6475 | an lvalue of type TYPE. ERRTYPE is a string to use in error | |
6476 | messages: "assignment", "return", etc. If FNDECL is non-NULL, we | |
6477 | are doing the conversion in order to pass the PARMNUMth argument of | |
6478 | FNDECL. */ | |
8d08fdba MS |
6479 | |
6480 | static tree | |
acd8e2d0 | 6481 | convert_for_assignment (tree type, tree rhs, |
5ade1ed2 DG |
6482 | const char *errtype, tree fndecl, int parmnum, |
6483 | tsubst_flags_t complain) | |
8d08fdba | 6484 | { |
926ce8bd KH |
6485 | tree rhstype; |
6486 | enum tree_code coder; | |
8d08fdba | 6487 | |
8d08fdba MS |
6488 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ |
6489 | if (TREE_CODE (rhs) == NON_LVALUE_EXPR) | |
6490 | rhs = TREE_OPERAND (rhs, 0); | |
6491 | ||
a7a64a77 MM |
6492 | rhstype = TREE_TYPE (rhs); |
6493 | coder = TREE_CODE (rhstype); | |
6494 | ||
7d149679 | 6495 | if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE |
00c8e9f6 | 6496 | && vector_types_convertible_p (type, rhstype, true)) |
7d149679 AH |
6497 | return convert (type, rhs); |
6498 | ||
a7a64a77 | 6499 | if (rhs == error_mark_node || rhstype == error_mark_node) |
8d08fdba | 6500 | return error_mark_node; |
2c73f9f5 | 6501 | if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node) |
8d08fdba MS |
6502 | return error_mark_node; |
6503 | ||
a7a64a77 | 6504 | /* The RHS of an assignment cannot have void type. */ |
8d08fdba MS |
6505 | if (coder == VOID_TYPE) |
6506 | { | |
5ade1ed2 DG |
6507 | if (complain & tf_error) |
6508 | error ("void value not ignored as it ought to be"); | |
8d08fdba MS |
6509 | return error_mark_node; |
6510 | } | |
8d08fdba | 6511 | |
a7a64a77 MM |
6512 | /* Simplify the RHS if possible. */ |
6513 | if (TREE_CODE (rhs) == CONST_DECL) | |
6514 | rhs = DECL_INITIAL (rhs); | |
660845bf ZL |
6515 | |
6516 | if (c_dialect_objc ()) | |
6517 | { | |
6518 | int parmno; | |
6519 | tree rname = fndecl; | |
6520 | ||
6521 | if (!strcmp (errtype, "assignment")) | |
6522 | parmno = -1; | |
6523 | else if (!strcmp (errtype, "initialization")) | |
6524 | parmno = -2; | |
6525 | else | |
6526 | { | |
6527 | tree selector = objc_message_selector (); | |
6528 | ||
6529 | parmno = parmnum; | |
6530 | ||
6531 | if (selector && parmno > 1) | |
6532 | { | |
6533 | rname = selector; | |
6534 | parmno -= 1; | |
6535 | } | |
6536 | } | |
6537 | ||
6538 | if (objc_compare_types (type, rhstype, parmno, rname)) | |
6539 | return convert (type, rhs); | |
6540 | } | |
6541 | ||
a7a64a77 MM |
6542 | /* [expr.ass] |
6543 | ||
6544 | The expression is implicitly converted (clause _conv_) to the | |
72a08131 JM |
6545 | cv-unqualified type of the left operand. |
6546 | ||
6547 | We allow bad conversions here because by the time we get to this point | |
6548 | we are committed to doing the conversion. If we end up doing a bad | |
6549 | conversion, convert_like will complain. */ | |
6550 | if (!can_convert_arg_bad (type, rhstype, rhs)) | |
a7a64a77 | 6551 | { |
32facac8 | 6552 | /* When -Wno-pmf-conversions is use, we just silently allow |
a7a64a77 MM |
6553 | conversions from pointers-to-members to plain pointers. If |
6554 | the conversion doesn't work, cp_convert will complain. */ | |
c8094d83 MS |
6555 | if (!warn_pmf2ptr |
6556 | && TYPE_PTR_P (type) | |
a7a64a77 MM |
6557 | && TYPE_PTRMEMFUNC_P (rhstype)) |
6558 | rhs = cp_convert (strip_top_quals (type), rhs); | |
72a08131 | 6559 | else |
32facac8 | 6560 | { |
5ade1ed2 DG |
6561 | if (complain & tf_error) |
6562 | { | |
6563 | /* If the right-hand side has unknown type, then it is an | |
6564 | overloaded function. Call instantiate_type to get error | |
6565 | messages. */ | |
6566 | if (rhstype == unknown_type_node) | |
6567 | instantiate_type (type, rhs, tf_warning_or_error); | |
6568 | else if (fndecl) | |
6569 | error ("cannot convert %qT to %qT for argument %qP to %qD", | |
6570 | rhstype, type, parmnum, fndecl); | |
6571 | else | |
6572 | error ("cannot convert %qT to %qT in %s", rhstype, type, | |
6573 | errtype); | |
6574 | } | |
32facac8 ML |
6575 | return error_mark_node; |
6576 | } | |
51c184be | 6577 | } |
104f8784 KG |
6578 | if (warn_missing_format_attribute) |
6579 | { | |
6580 | const enum tree_code codel = TREE_CODE (type); | |
6581 | if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
6582 | && coder == codel | |
5ade1ed2 DG |
6583 | && check_missing_format_attribute (type, rhstype) |
6584 | && (complain & tf_warning)) | |
104f8784 KG |
6585 | warning (OPT_Wmissing_format_attribute, |
6586 | "%s might be a candidate for a format attribute", | |
6587 | errtype); | |
6588 | } | |
3db45ab5 | 6589 | |
a99e5cb4 ILT |
6590 | /* If -Wparentheses, warn about a = b = c when a has type bool and b |
6591 | does not. */ | |
fbc8d2d3 ILT |
6592 | if (warn_parentheses |
6593 | && type == boolean_type_node | |
6594 | && TREE_CODE (rhs) == MODIFY_EXPR | |
a99e5cb4 | 6595 | && !TREE_NO_WARNING (rhs) |
5ade1ed2 DG |
6596 | && TREE_TYPE (rhs) != boolean_type_node |
6597 | && (complain & tf_warning)) | |
fbc8d2d3 ILT |
6598 | { |
6599 | warning (OPT_Wparentheses, | |
6600 | "suggest parentheses around assignment used as truth value"); | |
6601 | TREE_NO_WARNING (rhs) = 1; | |
6602 | } | |
6603 | ||
5ade1ed2 | 6604 | return perform_implicit_conversion (strip_top_quals (type), rhs, complain); |
8d08fdba MS |
6605 | } |
6606 | ||
56ae6d77 | 6607 | /* Convert RHS to be of type TYPE. |
838dfd8a | 6608 | If EXP is nonzero, it is the target of the initialization. |
8d08fdba MS |
6609 | ERRTYPE is a string to use in error messages. |
6610 | ||
6611 | Two major differences between the behavior of | |
6612 | `convert_for_assignment' and `convert_for_initialization' | |
6613 | are that references are bashed in the former, while | |
6614 | copied in the latter, and aggregates are assigned in | |
6615 | the former (operator=) while initialized in the | |
6616 | latter (X(X&)). | |
6617 | ||
6618 | If using constructor make sure no conversion operator exists, if one does | |
878cd289 MS |
6619 | exist, an ambiguity exists. |
6620 | ||
6621 | If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */ | |
e92cc029 | 6622 | |
8d08fdba | 6623 | tree |
acd8e2d0 | 6624 | convert_for_initialization (tree exp, tree type, tree rhs, int flags, |
5ade1ed2 DG |
6625 | const char *errtype, tree fndecl, int parmnum, |
6626 | tsubst_flags_t complain) | |
8d08fdba | 6627 | { |
926ce8bd KH |
6628 | enum tree_code codel = TREE_CODE (type); |
6629 | tree rhstype; | |
6630 | enum tree_code coder; | |
8d08fdba MS |
6631 | |
6632 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. | |
6633 | Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */ | |
6634 | if (TREE_CODE (rhs) == NOP_EXPR | |
a0a33927 MS |
6635 | && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0)) |
6636 | && codel != REFERENCE_TYPE) | |
8d08fdba MS |
6637 | rhs = TREE_OPERAND (rhs, 0); |
6638 | ||
57b52959 VR |
6639 | if (type == error_mark_node |
6640 | || rhs == error_mark_node | |
8d08fdba MS |
6641 | || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)) |
6642 | return error_mark_node; | |
6643 | ||
8d08fdba | 6644 | if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE |
8ccc31eb MS |
6645 | && TREE_CODE (type) != ARRAY_TYPE |
6646 | && (TREE_CODE (type) != REFERENCE_TYPE | |
6647 | || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)) | |
59e76fc6 JM |
6648 | || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE |
6649 | && (TREE_CODE (type) != REFERENCE_TYPE | |
6650 | || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)) | |
8d08fdba | 6651 | || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE) |
0a72704b | 6652 | rhs = decay_conversion (rhs); |
8d08fdba MS |
6653 | |
6654 | rhstype = TREE_TYPE (rhs); | |
6655 | coder = TREE_CODE (rhstype); | |
6656 | ||
8d08fdba MS |
6657 | if (coder == ERROR_MARK) |
6658 | return error_mark_node; | |
6659 | ||
8d08fdba MS |
6660 | /* We accept references to incomplete types, so we can |
6661 | return here before checking if RHS is of complete type. */ | |
c8094d83 | 6662 | |
8d08fdba | 6663 | if (codel == REFERENCE_TYPE) |
2986ae00 MS |
6664 | { |
6665 | /* This should eventually happen in convert_arguments. */ | |
a703fb38 | 6666 | int savew = 0, savee = 0; |
2986ae00 MS |
6667 | |
6668 | if (fndecl) | |
6669 | savew = warningcount, savee = errorcount; | |
7e99327d MM |
6670 | rhs = initialize_reference (type, rhs, /*decl=*/NULL_TREE, |
6671 | /*cleanup=*/NULL); | |
2986ae00 MS |
6672 | if (fndecl) |
6673 | { | |
6674 | if (warningcount > savew) | |
dee15844 | 6675 | warning (0, "in passing argument %P of %q+D", parmnum, fndecl); |
2986ae00 | 6676 | else if (errorcount > savee) |
dee15844 | 6677 | error ("in passing argument %P of %q+D", parmnum, fndecl); |
2986ae00 MS |
6678 | } |
6679 | return rhs; | |
c8094d83 | 6680 | } |
8d08fdba | 6681 | |
66543169 NS |
6682 | if (exp != 0) |
6683 | exp = require_complete_type (exp); | |
8d08fdba MS |
6684 | if (exp == error_mark_node) |
6685 | return error_mark_node; | |
6686 | ||
ee76b931 | 6687 | rhstype = non_reference (rhstype); |
8d08fdba | 6688 | |
6467930b MS |
6689 | type = complete_type (type); |
6690 | ||
9e1e64ec | 6691 | if (MAYBE_CLASS_TYPE_P (type)) |
277294d7 | 6692 | return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags); |
8d08fdba | 6693 | |
5ade1ed2 DG |
6694 | return convert_for_assignment (type, rhs, errtype, fndecl, parmnum, |
6695 | complain); | |
8d08fdba MS |
6696 | } |
6697 | \f | |
efee38a9 | 6698 | /* If RETVAL is the address of, or a reference to, a local variable or |
34cd5ae7 | 6699 | temporary give an appropriate warning. */ |
8d08fdba | 6700 | |
efee38a9 | 6701 | static void |
acd8e2d0 | 6702 | maybe_warn_about_returning_address_of_local (tree retval) |
efee38a9 MM |
6703 | { |
6704 | tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl)); | |
2bfa73e4 | 6705 | tree whats_returned = retval; |
8d08fdba | 6706 | |
2bfa73e4 | 6707 | for (;;) |
efee38a9 | 6708 | { |
efee38a9 | 6709 | if (TREE_CODE (whats_returned) == COMPOUND_EXPR) |
2bfa73e4 | 6710 | whats_returned = TREE_OPERAND (whats_returned, 1); |
5f9e56b3 TB |
6711 | else if (CONVERT_EXPR_P (whats_returned) |
6712 | || TREE_CODE (whats_returned) == NON_LVALUE_EXPR) | |
efee38a9 | 6713 | whats_returned = TREE_OPERAND (whats_returned, 0); |
2bfa73e4 BS |
6714 | else |
6715 | break; | |
6716 | } | |
6717 | ||
6718 | if (TREE_CODE (whats_returned) != ADDR_EXPR) | |
6719 | return; | |
c8094d83 | 6720 | whats_returned = TREE_OPERAND (whats_returned, 0); |
2bfa73e4 BS |
6721 | |
6722 | if (TREE_CODE (valtype) == REFERENCE_TYPE) | |
6723 | { | |
6724 | if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR | |
6725 | || TREE_CODE (whats_returned) == TARGET_EXPR) | |
efee38a9 | 6726 | { |
d4ee4d25 | 6727 | warning (0, "returning reference to temporary"); |
2bfa73e4 | 6728 | return; |
efee38a9 | 6729 | } |
c8094d83 | 6730 | if (TREE_CODE (whats_returned) == VAR_DECL |
2bfa73e4 BS |
6731 | && DECL_NAME (whats_returned) |
6732 | && TEMP_NAME_P (DECL_NAME (whats_returned))) | |
efee38a9 | 6733 | { |
d4ee4d25 | 6734 | warning (0, "reference to non-lvalue returned"); |
2bfa73e4 | 6735 | return; |
efee38a9 MM |
6736 | } |
6737 | } | |
efee38a9 | 6738 | |
a1a95249 JM |
6739 | while (TREE_CODE (whats_returned) == COMPONENT_REF |
6740 | || TREE_CODE (whats_returned) == ARRAY_REF) | |
6741 | whats_returned = TREE_OPERAND (whats_returned, 0); | |
6742 | ||
a2f1f4c3 | 6743 | if (DECL_P (whats_returned) |
2bfa73e4 BS |
6744 | && DECL_NAME (whats_returned) |
6745 | && DECL_FUNCTION_SCOPE_P (whats_returned) | |
6746 | && !(TREE_STATIC (whats_returned) | |
6747 | || TREE_PUBLIC (whats_returned))) | |
6748 | { | |
6749 | if (TREE_CODE (valtype) == REFERENCE_TYPE) | |
dee15844 JM |
6750 | warning (0, "reference to local variable %q+D returned", |
6751 | whats_returned); | |
2bfa73e4 | 6752 | else |
dee15844 JM |
6753 | warning (0, "address of local variable %q+D returned", |
6754 | whats_returned); | |
2bfa73e4 | 6755 | return; |
efee38a9 MM |
6756 | } |
6757 | } | |
6758 | ||
0e339752 | 6759 | /* Check that returning RETVAL from the current function is valid. |
efee38a9 MM |
6760 | Return an expression explicitly showing all conversions required to |
6761 | change RETVAL into the function return type, and to assign it to | |
0c9b182b JJ |
6762 | the DECL_RESULT for the function. Set *NO_WARNING to true if |
6763 | code reaches end of non-void function warning shouldn't be issued | |
6764 | on this RETURN_EXPR. */ | |
efee38a9 MM |
6765 | |
6766 | tree | |
0c9b182b | 6767 | check_return_expr (tree retval, bool *no_warning) |
8d08fdba | 6768 | { |
efee38a9 MM |
6769 | tree result; |
6770 | /* The type actually returned by the function, after any | |
6771 | promotions. */ | |
6772 | tree valtype; | |
6773 | int fn_returns_value_p; | |
8af2fec4 | 6774 | bool named_return_value_okay_p; |
efee38a9 | 6775 | |
0c9b182b JJ |
6776 | *no_warning = false; |
6777 | ||
efee38a9 MM |
6778 | /* A `volatile' function is one that isn't supposed to return, ever. |
6779 | (This is a G++ extension, used to get better code for functions | |
6780 | that call the `volatile' function.) */ | |
8d08fdba | 6781 | if (TREE_THIS_VOLATILE (current_function_decl)) |
d4ee4d25 | 6782 | warning (0, "function declared %<noreturn%> has a %<return%> statement"); |
8d08fdba | 6783 | |
efee38a9 | 6784 | /* Check for various simple errors. */ |
a0de9d20 | 6785 | if (DECL_DESTRUCTOR_P (current_function_decl)) |
8d08fdba | 6786 | { |
1c2c08a5 | 6787 | if (retval) |
8251199e | 6788 | error ("returning a value from a destructor"); |
efee38a9 | 6789 | return NULL_TREE; |
1c2c08a5 | 6790 | } |
90418208 | 6791 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) |
0dde4175 | 6792 | { |
90418208 JM |
6793 | if (in_function_try_handler) |
6794 | /* If a return statement appears in a handler of the | |
c6002625 | 6795 | function-try-block of a constructor, the program is ill-formed. */ |
90418208 JM |
6796 | error ("cannot return from a handler of a function-try-block of a constructor"); |
6797 | else if (retval) | |
6798 | /* You can't return a value from a constructor. */ | |
6799 | error ("returning a value from a constructor"); | |
6800 | return NULL_TREE; | |
efee38a9 | 6801 | } |
efee38a9 | 6802 | |
efc7052d JM |
6803 | if (processing_template_decl) |
6804 | { | |
6805 | current_function_returns_value = 1; | |
7b3e2d46 | 6806 | if (check_for_bare_parameter_packs (retval)) |
4439d02f | 6807 | retval = error_mark_node; |
efc7052d JM |
6808 | return retval; |
6809 | } | |
c8094d83 | 6810 | |
efee38a9 MM |
6811 | /* When no explicit return-value is given in a function with a named |
6812 | return value, the named return value is used. */ | |
6813 | result = DECL_RESULT (current_function_decl); | |
6814 | valtype = TREE_TYPE (result); | |
50bc768d | 6815 | gcc_assert (valtype != NULL_TREE); |
b72801e2 | 6816 | fn_returns_value_p = !VOID_TYPE_P (valtype); |
efee38a9 MM |
6817 | if (!retval && DECL_NAME (result) && fn_returns_value_p) |
6818 | retval = result; | |
6819 | ||
6820 | /* Check for a return statement with no return value in a function | |
6821 | that's supposed to return a value. */ | |
6822 | if (!retval && fn_returns_value_p) | |
6823 | { | |
cbe5f3b3 | 6824 | permerror (input_location, "return-statement with no value, in function returning %qT", |
37ec60ed | 6825 | valtype); |
efee38a9 MM |
6826 | /* Clear this, so finish_function won't say that we reach the |
6827 | end of a non-void function (which we don't, we gave a | |
6828 | return!). */ | |
6829 | current_function_returns_null = 0; | |
0c9b182b | 6830 | /* And signal caller that TREE_NO_WARNING should be set on the |
3db45ab5 MS |
6831 | RETURN_EXPR to avoid control reaches end of non-void function |
6832 | warnings in tree-cfg.c. */ | |
0c9b182b | 6833 | *no_warning = true; |
efee38a9 MM |
6834 | } |
6835 | /* Check for a return statement with a value in a function that | |
6836 | isn't supposed to return a value. */ | |
6837 | else if (retval && !fn_returns_value_p) | |
c8094d83 | 6838 | { |
b72801e2 | 6839 | if (VOID_TYPE_P (TREE_TYPE (retval))) |
efee38a9 MM |
6840 | /* You can return a `void' value from a function of `void' |
6841 | type. In that case, we have to evaluate the expression for | |
6842 | its side-effects. */ | |
6843 | finish_expr_stmt (retval); | |
6844 | else | |
cbe5f3b3 | 6845 | permerror (input_location, "return-statement with a value, in function " |
37ec60ed | 6846 | "returning 'void'"); |
efee38a9 MM |
6847 | current_function_returns_null = 1; |
6848 | ||
6849 | /* There's really no value to return, after all. */ | |
6850 | return NULL_TREE; | |
0dde4175 | 6851 | } |
efee38a9 MM |
6852 | else if (!retval) |
6853 | /* Remember that this function can sometimes return without a | |
6854 | value. */ | |
6855 | current_function_returns_null = 1; | |
90418208 JM |
6856 | else |
6857 | /* Remember that this function did return a value. */ | |
6858 | current_function_returns_value = 1; | |
1c2c08a5 | 6859 | |
276318a5 | 6860 | /* Check for erroneous operands -- but after giving ourselves a |
5ae9ba3e MM |
6861 | chance to provide an error about returning a value from a void |
6862 | function. */ | |
6863 | if (error_operand_p (retval)) | |
6864 | { | |
6865 | current_function_return_value = error_mark_node; | |
6866 | return error_mark_node; | |
6867 | } | |
6868 | ||
7f477e81 | 6869 | /* Only operator new(...) throw(), can return NULL [expr.new/13]. */ |
596ea4e5 AS |
6870 | if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR |
6871 | || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR) | |
7f477e81 | 6872 | && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl)) |
708cae97 | 6873 | && ! flag_check_new |
aedfac0e | 6874 | && retval && null_ptr_cst_p (retval)) |
d4ee4d25 | 6875 | warning (0, "%<operator new%> must not return NULL unless it is " |
0cbd7506 | 6876 | "declared %<throw()%> (or -fcheck-new is in effect)"); |
8d08fdba | 6877 | |
824b9a4c | 6878 | /* Effective C++ rule 15. See also start_function. */ |
eb448459 | 6879 | if (warn_ecpp |
47293da3 GB |
6880 | && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)) |
6881 | { | |
6882 | bool warn = true; | |
6883 | ||
6884 | /* The function return type must be a reference to the current | |
6885 | class. */ | |
6886 | if (TREE_CODE (valtype) == REFERENCE_TYPE | |
6887 | && same_type_ignoring_top_level_qualifiers_p | |
6888 | (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))) | |
6889 | { | |
6890 | /* Returning '*this' is obviously OK. */ | |
6891 | if (retval == current_class_ref) | |
6892 | warn = false; | |
6893 | /* If we are calling a function whose return type is the same of | |
6894 | the current class reference, it is ok. */ | |
6895 | else if (TREE_CODE (retval) == INDIRECT_REF | |
6896 | && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR) | |
616adc47 | 6897 | warn = false; |
47293da3 GB |
6898 | } |
6899 | ||
6900 | if (warn) | |
b323323f | 6901 | warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>"); |
47293da3 | 6902 | } |
824b9a4c | 6903 | |
07b2f2fd JM |
6904 | /* The fabled Named Return Value optimization, as per [class.copy]/15: |
6905 | ||
6906 | [...] For a function with a class return type, if the expression | |
6907 | in the return statement is the name of a local object, and the cv- | |
6908 | unqualified type of the local object is the same as the function | |
6909 | return type, an implementation is permitted to omit creating the tem- | |
6910 | porary object to hold the function return value [...] | |
6911 | ||
6912 | So, if this is a value-returning function that always returns the same | |
6913 | local variable, remember it. | |
0d97bf4c JM |
6914 | |
6915 | It might be nice to be more flexible, and choose the first suitable | |
6916 | variable even if the function sometimes returns something else, but | |
6917 | then we run the risk of clobbering the variable we chose if the other | |
6918 | returned expression uses the chosen variable somehow. And people expect | |
07b2f2fd JM |
6919 | this restriction, anyway. (jason 2000-11-19) |
6920 | ||
324f9dfb | 6921 | See finish_function and finalize_nrv for the rest of this optimization. */ |
0d97bf4c | 6922 | |
8af2fec4 RY |
6923 | named_return_value_okay_p = |
6924 | (retval != NULL_TREE | |
6925 | /* Must be a local, automatic variable. */ | |
6926 | && TREE_CODE (retval) == VAR_DECL | |
6927 | && DECL_CONTEXT (retval) == current_function_decl | |
6928 | && ! TREE_STATIC (retval) | |
59fe9274 | 6929 | && ! DECL_ANON_UNION_VAR_P (retval) |
8af2fec4 RY |
6930 | && (DECL_ALIGN (retval) |
6931 | >= DECL_ALIGN (DECL_RESULT (current_function_decl))) | |
6932 | /* The cv-unqualified type of the returned value must be the | |
6933 | same as the cv-unqualified return type of the | |
6934 | function. */ | |
6935 | && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))), | |
6936 | (TYPE_MAIN_VARIANT | |
0e95aec1 CB |
6937 | (TREE_TYPE (TREE_TYPE (current_function_decl))))) |
6938 | /* And the returned value must be non-volatile. */ | |
6939 | && ! TYPE_VOLATILE (TREE_TYPE (retval))); | |
8af2fec4 | 6940 | |
01f9e964 | 6941 | if (fn_returns_value_p && flag_elide_constructors) |
0d97bf4c | 6942 | { |
8af2fec4 RY |
6943 | if (named_return_value_okay_p |
6944 | && (current_function_return_value == NULL_TREE | |
6945 | || current_function_return_value == retval)) | |
0d97bf4c JM |
6946 | current_function_return_value = retval; |
6947 | else | |
6948 | current_function_return_value = error_mark_node; | |
6949 | } | |
6950 | ||
efee38a9 MM |
6951 | /* We don't need to do any conversions when there's nothing being |
6952 | returned. */ | |
5ae9ba3e MM |
6953 | if (!retval) |
6954 | return NULL_TREE; | |
efee38a9 MM |
6955 | |
6956 | /* Do any required conversions. */ | |
6957 | if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl)) | |
6958 | /* No conversions are required. */ | |
6959 | ; | |
8d08fdba MS |
6960 | else |
6961 | { | |
efee38a9 | 6962 | /* The type the function is declared to return. */ |
cce2be43 | 6963 | tree functype = TREE_TYPE (TREE_TYPE (current_function_decl)); |
8af2fec4 | 6964 | int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING; |
cce2be43 | 6965 | |
51b15ede NS |
6966 | /* The functype's return type will have been set to void, if it |
6967 | was an incomplete type. Just treat this as 'return;' */ | |
6968 | if (VOID_TYPE_P (functype)) | |
6969 | return error_mark_node; | |
3db45ab5 | 6970 | |
8af2fec4 RY |
6971 | /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes |
6972 | treated as an rvalue for the purposes of overload resolution to | |
6973 | favor move constructors over copy constructors. */ | |
c1ae8be5 | 6974 | if ((cxx_dialect != cxx98) |
8af2fec4 RY |
6975 | && named_return_value_okay_p |
6976 | /* The variable must not have the `volatile' qualifier. */ | |
6977 | && !(cp_type_quals (TREE_TYPE (retval)) & TYPE_QUAL_VOLATILE) | |
6978 | /* The return type must be a class type. */ | |
6979 | && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))) | |
6980 | flags = flags | LOOKUP_PREFER_RVALUE; | |
6981 | ||
cce2be43 JM |
6982 | /* First convert the value to the function's return type, then |
6983 | to the type of return value's location to handle the | |
0cbd7506 | 6984 | case that functype is smaller than the valtype. */ |
c1bc6829 | 6985 | retval = convert_for_initialization |
5ade1ed2 DG |
6986 | (NULL_TREE, functype, retval, flags, "return", NULL_TREE, 0, |
6987 | tf_warning_or_error); | |
cce2be43 JM |
6988 | retval = convert (valtype, retval); |
6989 | ||
efee38a9 | 6990 | /* If the conversion failed, treat this just like `return;'. */ |
691c003d | 6991 | if (retval == error_mark_node) |
90418208 | 6992 | return retval; |
02531345 | 6993 | /* We can't initialize a register from a AGGR_INIT_EXPR. */ |
e3b5732b | 6994 | else if (! cfun->returns_struct |
c1bc6829 | 6995 | && TREE_CODE (retval) == TARGET_EXPR |
02531345 | 6996 | && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR) |
f293ce4b RS |
6997 | retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, |
6998 | TREE_OPERAND (retval, 0)); | |
efee38a9 MM |
6999 | else |
7000 | maybe_warn_about_returning_address_of_local (retval); | |
8d08fdba | 7001 | } |
c8094d83 | 7002 | |
efee38a9 | 7003 | /* Actually copy the value returned into the appropriate location. */ |
691c003d | 7004 | if (retval && retval != result) |
f293ce4b | 7005 | retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval); |
b88c08b6 | 7006 | |
efee38a9 MM |
7007 | return retval; |
7008 | } | |
7009 | ||
8d08fdba | 7010 | \f |
838dfd8a | 7011 | /* Returns nonzero if the pointer-type FROM can be converted to the |
ceab47eb | 7012 | pointer-type TO via a qualification conversion. If CONSTP is -1, |
838dfd8a | 7013 | then we return nonzero if the pointers are similar, and the |
ceab47eb MM |
7014 | cv-qualification signature of FROM is a proper subset of that of TO. |
7015 | ||
7016 | If CONSTP is positive, then all outer pointers have been | |
7017 | const-qualified. */ | |
e92cc029 | 7018 | |
bd6dd845 | 7019 | static int |
acd8e2d0 | 7020 | comp_ptr_ttypes_real (tree to, tree from, int constp) |
a0a33927 | 7021 | { |
a5ac359a | 7022 | bool to_more_cv_qualified = false; |
f83c7f63 | 7023 | bool is_opaque_pointer = false; |
ceab47eb | 7024 | |
a0a33927 MS |
7025 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) |
7026 | { | |
7027 | if (TREE_CODE (to) != TREE_CODE (from)) | |
7028 | return 0; | |
7029 | ||
d11ad92e | 7030 | if (TREE_CODE (from) == OFFSET_TYPE |
b7a78333 MM |
7031 | && !same_type_p (TYPE_OFFSET_BASETYPE (from), |
7032 | TYPE_OFFSET_BASETYPE (to))) | |
7033 | return 0; | |
d11ad92e | 7034 | |
f30432d7 MS |
7035 | /* Const and volatile mean something different for function types, |
7036 | so the usual checks are not appropriate. */ | |
7037 | if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE) | |
7038 | { | |
660845bf ZL |
7039 | /* In Objective-C++, some types may have been 'volatilized' by |
7040 | the compiler for EH; when comparing them here, the volatile | |
7041 | qualification must be ignored. */ | |
7042 | bool objc_quals_match = objc_type_quals_match (to, from); | |
7043 | ||
7044 | if (!at_least_as_qualified_p (to, from) && !objc_quals_match) | |
02020185 | 7045 | return 0; |
a0a33927 | 7046 | |
660845bf | 7047 | if (!at_least_as_qualified_p (from, to) && !objc_quals_match) |
02020185 | 7048 | { |
ceab47eb MM |
7049 | if (constp == 0) |
7050 | return 0; | |
a5ac359a | 7051 | to_more_cv_qualified = true; |
ceab47eb MM |
7052 | } |
7053 | ||
7054 | if (constp > 0) | |
7055 | constp &= TYPE_READONLY (to); | |
f30432d7 | 7056 | } |
a0a33927 | 7057 | |
f83c7f63 DJ |
7058 | if (TREE_CODE (to) == VECTOR_TYPE) |
7059 | is_opaque_pointer = vector_targets_convertible_p (to, from); | |
7060 | ||
ae0b7dfc | 7061 | if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to)) |
8de9bb0e | 7062 | return ((constp >= 0 || to_more_cv_qualified) |
f83c7f63 DJ |
7063 | && (is_opaque_pointer |
7064 | || same_type_ignoring_top_level_qualifiers_p (to, from))); | |
a0a33927 MS |
7065 | } |
7066 | } | |
7067 | ||
8de9bb0e NS |
7068 | /* When comparing, say, char ** to char const **, this function takes |
7069 | the 'char *' and 'char const *'. Do not pass non-pointer/reference | |
7070 | types to this function. */ | |
e92cc029 | 7071 | |
a0a33927 | 7072 | int |
acd8e2d0 | 7073 | comp_ptr_ttypes (tree to, tree from) |
a0a33927 MS |
7074 | { |
7075 | return comp_ptr_ttypes_real (to, from, 1); | |
7076 | } | |
d11ad92e MS |
7077 | |
7078 | /* Returns 1 if to and from are (possibly multi-level) pointers to the same | |
7079 | type or inheritance-related types, regardless of cv-quals. */ | |
7080 | ||
7081 | int | |
58f9752a | 7082 | ptr_reasonably_similar (const_tree to, const_tree from) |
d11ad92e MS |
7083 | { |
7084 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) | |
7085 | { | |
72a08131 JM |
7086 | /* Any target type is similar enough to void. */ |
7087 | if (TREE_CODE (to) == VOID_TYPE | |
7088 | || TREE_CODE (from) == VOID_TYPE) | |
7089 | return 1; | |
7090 | ||
d11ad92e MS |
7091 | if (TREE_CODE (to) != TREE_CODE (from)) |
7092 | return 0; | |
7093 | ||
7094 | if (TREE_CODE (from) == OFFSET_TYPE | |
96d84882 PB |
7095 | && comptypes (TYPE_OFFSET_BASETYPE (to), |
7096 | TYPE_OFFSET_BASETYPE (from), | |
7097 | COMPARE_BASE | COMPARE_DERIVED)) | |
d11ad92e MS |
7098 | continue; |
7099 | ||
d70b8c3a | 7100 | if (TREE_CODE (to) == VECTOR_TYPE |
00c8e9f6 | 7101 | && vector_types_convertible_p (to, from, false)) |
d70b8c3a PB |
7102 | return 1; |
7103 | ||
8a2b77e7 JM |
7104 | if (TREE_CODE (to) == INTEGER_TYPE |
7105 | && TYPE_PRECISION (to) == TYPE_PRECISION (from)) | |
7106 | return 1; | |
7107 | ||
7108 | if (TREE_CODE (to) == FUNCTION_TYPE) | |
7109 | return 1; | |
7110 | ||
d11ad92e | 7111 | if (TREE_CODE (to) != POINTER_TYPE) |
96d84882 | 7112 | return comptypes |
c8094d83 | 7113 | (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), |
c8a209ca | 7114 | COMPARE_BASE | COMPARE_DERIVED); |
d11ad92e MS |
7115 | } |
7116 | } | |
c11b6f21 | 7117 | |
34b5375f MM |
7118 | /* Return true if TO and FROM (both of which are POINTER_TYPEs or |
7119 | pointer-to-member types) are the same, ignoring cv-qualification at | |
7120 | all levels. */ | |
c11b6f21 | 7121 | |
34b5375f | 7122 | bool |
acd8e2d0 | 7123 | comp_ptr_ttypes_const (tree to, tree from) |
c11b6f21 | 7124 | { |
f83c7f63 DJ |
7125 | bool is_opaque_pointer = false; |
7126 | ||
c11b6f21 MS |
7127 | for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) |
7128 | { | |
7129 | if (TREE_CODE (to) != TREE_CODE (from)) | |
34b5375f | 7130 | return false; |
c11b6f21 MS |
7131 | |
7132 | if (TREE_CODE (from) == OFFSET_TYPE | |
3bfdc719 MM |
7133 | && same_type_p (TYPE_OFFSET_BASETYPE (from), |
7134 | TYPE_OFFSET_BASETYPE (to))) | |
c11b6f21 MS |
7135 | continue; |
7136 | ||
f83c7f63 DJ |
7137 | if (TREE_CODE (to) == VECTOR_TYPE) |
7138 | is_opaque_pointer = vector_targets_convertible_p (to, from); | |
7139 | ||
c11b6f21 | 7140 | if (TREE_CODE (to) != POINTER_TYPE) |
f83c7f63 DJ |
7141 | return (is_opaque_pointer |
7142 | || same_type_ignoring_top_level_qualifiers_p (to, from)); | |
c11b6f21 MS |
7143 | } |
7144 | } | |
7145 | ||
89d684bb BM |
7146 | /* Returns the type qualifiers for this type, including the qualifiers on the |
7147 | elements for an array type. */ | |
27778b73 MM |
7148 | |
7149 | int | |
ac7d7749 | 7150 | cp_type_quals (const_tree type) |
27778b73 | 7151 | { |
4e9b57fa | 7152 | /* This CONST_CAST is okay because strip_array_types returns its |
75547801 | 7153 | argument unmodified and we assign it to a const_tree. */ |
b1d5455a | 7154 | type = strip_array_types (CONST_CAST_TREE(type)); |
328de7c2 MM |
7155 | if (type == error_mark_node) |
7156 | return TYPE_UNQUALIFIED; | |
38da6039 | 7157 | return TYPE_QUALS (type); |
91063b51 | 7158 | } |
a7a7710d | 7159 | |
c3b7031d JM |
7160 | /* Returns nonzero if the TYPE is const from a C++ perspective: look inside |
7161 | arrays. */ | |
7162 | ||
7163 | bool | |
58f9752a | 7164 | cp_type_readonly (const_tree type) |
c3b7031d | 7165 | { |
4e9b57fa | 7166 | /* This CONST_CAST is okay because strip_array_types returns its |
75547801 | 7167 | argument unmodified and we assign it to a const_tree. */ |
b1d5455a | 7168 | type = strip_array_types (CONST_CAST_TREE(type)); |
c3b7031d JM |
7169 | return TYPE_READONLY (type); |
7170 | } | |
7171 | ||
f4f206f4 | 7172 | /* Returns nonzero if the TYPE contains a mutable member. */ |
a7a7710d | 7173 | |
acd8e2d0 | 7174 | bool |
58f9752a | 7175 | cp_has_mutable_p (const_tree type) |
a7a7710d | 7176 | { |
4e9b57fa | 7177 | /* This CONST_CAST is okay because strip_array_types returns its |
75547801 | 7178 | argument unmodified and we assign it to a const_tree. */ |
b1d5455a | 7179 | type = strip_array_types (CONST_CAST_TREE(type)); |
a7a7710d NS |
7180 | |
7181 | return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type); | |
7182 | } | |
af7b9902 | 7183 | |
15896502 MM |
7184 | /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the |
7185 | TYPE_QUALS. For a VAR_DECL, this may be an optimistic | |
7186 | approximation. In particular, consider: | |
7187 | ||
7188 | int f(); | |
7189 | struct S { int i; }; | |
7190 | const S s = { f(); } | |
7191 | ||
7192 | Here, we will make "s" as TREE_READONLY (because it is declared | |
7193 | "const") -- only to reverse ourselves upon seeing that the | |
7194 | initializer is non-constant. */ | |
7195 | ||
9804209d DG |
7196 | void |
7197 | cp_apply_type_quals_to_decl (int type_quals, tree decl) | |
7198 | { | |
7199 | tree type = TREE_TYPE (decl); | |
7200 | ||
7201 | if (type == error_mark_node) | |
7202 | return; | |
7203 | ||
c8094d83 | 7204 | if (TREE_CODE (type) == FUNCTION_TYPE |
9804209d DG |
7205 | && type_quals != TYPE_UNQUALIFIED) |
7206 | { | |
7207 | /* This was an error in C++98 (cv-qualifiers cannot be added to | |
0cbd7506 MS |
7208 | a function type), but DR 295 makes the code well-formed by |
7209 | dropping the extra qualifiers. */ | |
9804209d | 7210 | if (pedantic) |
0cbd7506 MS |
7211 | { |
7212 | tree bad_type = build_qualified_type (type, type_quals); | |
509c9d60 | 7213 | pedwarn (input_location, OPT_pedantic, |
fcf73884 | 7214 | "ignoring %qV qualifiers added to function type %qT", |
0cbd7506 MS |
7215 | bad_type, type); |
7216 | } | |
9804209d DG |
7217 | |
7218 | TREE_TYPE (decl) = TYPE_MAIN_VARIANT (type); | |
7219 | return; | |
7220 | } | |
7221 | ||
67935995 MM |
7222 | /* Avoid setting TREE_READONLY incorrectly. */ |
7223 | if (/* If the object has a constructor, the constructor may modify | |
7224 | the object. */ | |
7225 | TYPE_NEEDS_CONSTRUCTING (type) | |
7226 | /* If the type isn't complete, we don't know yet if it will need | |
7227 | constructing. */ | |
c8094d83 | 7228 | || !COMPLETE_TYPE_P (type) |
67935995 MM |
7229 | /* If the type has a mutable component, that component might be |
7230 | modified. */ | |
7231 | || TYPE_HAS_MUTABLE_P (type)) | |
7232 | type_quals &= ~TYPE_QUAL_CONST; | |
7233 | ||
9804209d DG |
7234 | c_apply_type_quals_to_decl (type_quals, decl); |
7235 | } | |
7236 | ||
af7b9902 | 7237 | /* Subroutine of casts_away_constness. Make T1 and T2 point at |
33c25e5c | 7238 | exemplar types such that casting T1 to T2 is casting away constness |
af7b9902 MM |
7239 | if and only if there is no implicit conversion from T1 to T2. */ |
7240 | ||
7241 | static void | |
acd8e2d0 | 7242 | casts_away_constness_r (tree *t1, tree *t2) |
af7b9902 MM |
7243 | { |
7244 | int quals1; | |
7245 | int quals2; | |
7246 | ||
7247 | /* [expr.const.cast] | |
7248 | ||
7249 | For multi-level pointer to members and multi-level mixed pointers | |
7250 | and pointers to members (conv.qual), the "member" aspect of a | |
7251 | pointer to member level is ignored when determining if a const | |
7252 | cv-qualifier has been cast away. */ | |
af7b9902 MM |
7253 | /* [expr.const.cast] |
7254 | ||
7255 | For two pointer types: | |
7256 | ||
0cbd7506 MS |
7257 | X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type |
7258 | X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type | |
7259 | K is min(N,M) | |
af7b9902 MM |
7260 | |
7261 | casting from X1 to X2 casts away constness if, for a non-pointer | |
7262 | type T there does not exist an implicit conversion (clause | |
7263 | _conv_) from: | |
7264 | ||
0cbd7506 | 7265 | Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N * |
c8094d83 | 7266 | |
af7b9902 MM |
7267 | to |
7268 | ||
0cbd7506 | 7269 | Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */ |
dad732fa MM |
7270 | if ((!TYPE_PTR_P (*t1) && !TYPE_PTRMEM_P (*t1)) |
7271 | || (!TYPE_PTR_P (*t2) && !TYPE_PTRMEM_P (*t2))) | |
af7b9902 MM |
7272 | { |
7273 | *t1 = cp_build_qualified_type (void_type_node, | |
89d684bb | 7274 | cp_type_quals (*t1)); |
af7b9902 | 7275 | *t2 = cp_build_qualified_type (void_type_node, |
89d684bb | 7276 | cp_type_quals (*t2)); |
af7b9902 MM |
7277 | return; |
7278 | } | |
c8094d83 | 7279 | |
89d684bb BM |
7280 | quals1 = cp_type_quals (*t1); |
7281 | quals2 = cp_type_quals (*t2); | |
dad732fa MM |
7282 | |
7283 | if (TYPE_PTRMEM_P (*t1)) | |
7284 | *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1); | |
7285 | else | |
7286 | *t1 = TREE_TYPE (*t1); | |
7287 | if (TYPE_PTRMEM_P (*t2)) | |
7288 | *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2); | |
7289 | else | |
7290 | *t2 = TREE_TYPE (*t2); | |
7291 | ||
af7b9902 MM |
7292 | casts_away_constness_r (t1, t2); |
7293 | *t1 = build_pointer_type (*t1); | |
7294 | *t2 = build_pointer_type (*t2); | |
7295 | *t1 = cp_build_qualified_type (*t1, quals1); | |
7296 | *t2 = cp_build_qualified_type (*t2, quals2); | |
7297 | } | |
7298 | ||
838dfd8a | 7299 | /* Returns nonzero if casting from TYPE1 to TYPE2 casts away |
af7b9902 MM |
7300 | constness. */ |
7301 | ||
acd8e2d0 NS |
7302 | static bool |
7303 | casts_away_constness (tree t1, tree t2) | |
af7b9902 MM |
7304 | { |
7305 | if (TREE_CODE (t2) == REFERENCE_TYPE) | |
7306 | { | |
7307 | /* [expr.const.cast] | |
c8094d83 | 7308 | |
af7b9902 MM |
7309 | Casting from an lvalue of type T1 to an lvalue of type T2 |
7310 | using a reference cast casts away constness if a cast from an | |
7311 | rvalue of type "pointer to T1" to the type "pointer to T2" | |
7312 | casts away constness. */ | |
acd8e2d0 | 7313 | t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1); |
af7b9902 MM |
7314 | return casts_away_constness (build_pointer_type (t1), |
7315 | build_pointer_type (TREE_TYPE (t2))); | |
7316 | } | |
7317 | ||
7318 | if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)) | |
7319 | /* [expr.const.cast] | |
c8094d83 | 7320 | |
af7b9902 MM |
7321 | Casting from an rvalue of type "pointer to data member of X |
7322 | of type T1" to the type "pointer to data member of Y of type | |
7323 | T2" casts away constness if a cast from an rvalue of type | |
aba649ba | 7324 | "pointer to T1" to the type "pointer to T2" casts away |
af7b9902 | 7325 | constness. */ |
3e0b4710 | 7326 | return casts_away_constness |
a5ac359a MM |
7327 | (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)), |
7328 | build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2))); | |
af7b9902 MM |
7329 | |
7330 | /* Casting away constness is only something that makes sense for | |
7331 | pointer or reference types. */ | |
c8094d83 | 7332 | if (TREE_CODE (t1) != POINTER_TYPE |
af7b9902 | 7333 | || TREE_CODE (t2) != POINTER_TYPE) |
acd8e2d0 | 7334 | return false; |
af7b9902 MM |
7335 | |
7336 | /* Top-level qualifiers don't matter. */ | |
7337 | t1 = TYPE_MAIN_VARIANT (t1); | |
7338 | t2 = TYPE_MAIN_VARIANT (t2); | |
7339 | casts_away_constness_r (&t1, &t2); | |
7340 | if (!can_convert (t2, t1)) | |
acd8e2d0 | 7341 | return true; |
af7b9902 | 7342 | |
acd8e2d0 | 7343 | return false; |
af7b9902 | 7344 | } |
6816f040 | 7345 | |
ee76b931 MM |
7346 | /* If T is a REFERENCE_TYPE return the type to which T refers. |
7347 | Otherwise, return T itself. */ | |
7348 | ||
7349 | tree | |
7350 | non_reference (tree t) | |
7351 | { | |
7352 | if (TREE_CODE (t) == REFERENCE_TYPE) | |
7353 | t = TREE_TYPE (t); | |
7354 | return t; | |
7355 | } | |
37dc0d8d JM |
7356 | |
7357 | ||
7358 | /* Return nonzero if REF is an lvalue valid for this language; | |
7359 | otherwise, print an error message and return zero. USE says | |
7360 | how the lvalue is being used and so selects the error message. */ | |
7361 | ||
7362 | int | |
5ade1ed2 | 7363 | lvalue_or_else (const_tree ref, enum lvalue_use use, tsubst_flags_t complain) |
37dc0d8d JM |
7364 | { |
7365 | int win = lvalue_p (ref); | |
7366 | ||
5ade1ed2 | 7367 | if (!win && (complain & tf_error)) |
37dc0d8d JM |
7368 | lvalue_error (use); |
7369 | ||
7370 | return win; | |
7371 | } |