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