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