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