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