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