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