]>
Commit | Line | Data |
---|---|---|
400fbf9f | 1 | /* Build expressions with type checking for C compiler. |
06ceef4e | 2 | Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
2f89bbc1 | 3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
400fbf9f | 4 | |
1322177d | 5 | This file is part of GCC. |
400fbf9f | 6 | |
1322177d LB |
7 | GCC is free software; you can redistribute it and/or modify it under |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 2, or (at your option) any later | |
10 | version. | |
400fbf9f | 11 | |
1322177d LB |
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
400fbf9f JW |
16 | |
17 | You should have received a copy of the GNU General Public License | |
1322177d LB |
18 | along with GCC; see the file COPYING. If not, write to the Free |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
400fbf9f JW |
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-specific error checks, | |
5088b058 | 26 | and some optimization. */ |
400fbf9f JW |
27 | |
28 | #include "config.h" | |
670ee920 | 29 | #include "system.h" |
4977bab6 ZW |
30 | #include "coretypes.h" |
31 | #include "tm.h" | |
742b62e7 | 32 | #include "rtl.h" |
400fbf9f | 33 | #include "tree.h" |
e57e265b | 34 | #include "langhooks.h" |
400fbf9f | 35 | #include "c-tree.h" |
6baf1cc8 | 36 | #include "tm_p.h" |
400fbf9f | 37 | #include "flags.h" |
e14417fa | 38 | #include "output.h" |
234042f4 | 39 | #include "expr.h" |
5f6da302 | 40 | #include "toplev.h" |
ab87f8c8 | 41 | #include "intl.h" |
4dd7201e | 42 | #include "ggc.h" |
672a6f42 | 43 | #include "target.h" |
325c3691 | 44 | #include "tree-iterator.h" |
3a5b9284 | 45 | #include "tree-gimple.h" |
325c3691 | 46 | |
400fbf9f | 47 | |
b71c7f8a | 48 | /* Nonzero if we've already printed a "missing braces around initializer" |
103b7b17 | 49 | message within this initializer. */ |
b71c7f8a | 50 | static int missing_braces_mentioned; |
103b7b17 | 51 | |
bf730f15 RS |
52 | static int require_constant_value; |
53 | static int require_constant_elements; | |
54 | ||
f55ade6e | 55 | static tree qualify_type (tree, tree); |
132da1a5 | 56 | static int tagged_types_tu_compatible_p (tree, tree); |
f55ade6e | 57 | static int comp_target_types (tree, tree, int); |
132da1a5 JM |
58 | static int function_types_compatible_p (tree, tree); |
59 | static int type_lists_compatible_p (tree, tree); | |
f55ade6e AJ |
60 | static tree decl_constant_value_for_broken_optimization (tree); |
61 | static tree default_function_array_conversion (tree); | |
62 | static tree lookup_field (tree, tree); | |
f55ade6e AJ |
63 | static tree convert_arguments (tree, tree, tree, tree); |
64 | static tree pointer_diff (tree, tree); | |
f55ade6e AJ |
65 | static tree convert_for_assignment (tree, tree, const char *, tree, tree, |
66 | int); | |
67 | static void warn_for_assignment (const char *, const char *, tree, int); | |
68 | static tree valid_compound_expr_initializer (tree, tree); | |
69 | static void push_string (const char *); | |
70 | static void push_member_name (tree); | |
71 | static void push_array_bounds (int); | |
72 | static int spelling_length (void); | |
73 | static char *print_spelling (char *); | |
74 | static void warning_init (const char *); | |
916c5919 JM |
75 | static tree digest_init (tree, tree, bool, int); |
76 | static void output_init_element (tree, bool, tree, tree, int); | |
f55ade6e AJ |
77 | static void output_pending_init_elements (int); |
78 | static int set_designator (int); | |
79 | static void push_range_stack (tree); | |
80 | static void add_pending_init (tree, tree); | |
81 | static void set_nonincremental_init (void); | |
82 | static void set_nonincremental_init_from_string (tree); | |
83 | static tree find_init_member (tree); | |
25a1a39e | 84 | static int lvalue_or_else (tree, const char *); |
400fbf9f JW |
85 | \f |
86 | /* Do `exp = require_complete_type (exp);' to make sure exp | |
87 | does not have an incomplete type. (That includes void types.) */ | |
88 | ||
89 | tree | |
2f6e4e97 | 90 | require_complete_type (tree value) |
400fbf9f JW |
91 | { |
92 | tree type = TREE_TYPE (value); | |
93 | ||
c3d5c3fa | 94 | if (value == error_mark_node || type == error_mark_node) |
ea0f786b CB |
95 | return error_mark_node; |
96 | ||
400fbf9f | 97 | /* First, detect a valid value with a complete type. */ |
d0f062fb | 98 | if (COMPLETE_TYPE_P (type)) |
400fbf9f JW |
99 | return value; |
100 | ||
7a228918 | 101 | c_incomplete_type_error (value, type); |
400fbf9f JW |
102 | return error_mark_node; |
103 | } | |
104 | ||
105 | /* Print an error message for invalid use of an incomplete type. | |
106 | VALUE is the expression that was used (or 0 if that isn't known) | |
107 | and TYPE is the type that was invalid. */ | |
108 | ||
109 | void | |
2f6e4e97 | 110 | c_incomplete_type_error (tree value, tree type) |
400fbf9f | 111 | { |
5d5993dd | 112 | const char *type_code_string; |
400fbf9f JW |
113 | |
114 | /* Avoid duplicate error message. */ | |
115 | if (TREE_CODE (type) == ERROR_MARK) | |
116 | return; | |
117 | ||
118 | if (value != 0 && (TREE_CODE (value) == VAR_DECL | |
119 | || TREE_CODE (value) == PARM_DECL)) | |
120 | error ("`%s' has an incomplete type", | |
121 | IDENTIFIER_POINTER (DECL_NAME (value))); | |
122 | else | |
123 | { | |
124 | retry: | |
125 | /* We must print an error message. Be clever about what it says. */ | |
126 | ||
127 | switch (TREE_CODE (type)) | |
128 | { | |
129 | case RECORD_TYPE: | |
ab87f8c8 | 130 | type_code_string = "struct"; |
400fbf9f JW |
131 | break; |
132 | ||
133 | case UNION_TYPE: | |
ab87f8c8 | 134 | type_code_string = "union"; |
400fbf9f JW |
135 | break; |
136 | ||
137 | case ENUMERAL_TYPE: | |
ab87f8c8 | 138 | type_code_string = "enum"; |
400fbf9f JW |
139 | break; |
140 | ||
141 | case VOID_TYPE: | |
142 | error ("invalid use of void expression"); | |
143 | return; | |
144 | ||
145 | case ARRAY_TYPE: | |
146 | if (TYPE_DOMAIN (type)) | |
147 | { | |
fba78abb RH |
148 | if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL) |
149 | { | |
150 | error ("invalid use of flexible array member"); | |
151 | return; | |
152 | } | |
400fbf9f JW |
153 | type = TREE_TYPE (type); |
154 | goto retry; | |
155 | } | |
156 | error ("invalid use of array with unspecified bounds"); | |
157 | return; | |
158 | ||
159 | default: | |
160 | abort (); | |
161 | } | |
162 | ||
163 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
ab87f8c8 JL |
164 | error ("invalid use of undefined type `%s %s'", |
165 | type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type))); | |
400fbf9f JW |
166 | else |
167 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ | |
168 | error ("invalid use of incomplete typedef `%s'", | |
169 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))); | |
170 | } | |
171 | } | |
172 | ||
ab393bf1 NB |
173 | /* Given a type, apply default promotions wrt unnamed function |
174 | arguments and return the new type. */ | |
175 | ||
176 | tree | |
2f6e4e97 | 177 | c_type_promotes_to (tree type) |
ab393bf1 NB |
178 | { |
179 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
180 | return double_type_node; | |
181 | ||
182 | if (c_promoting_integer_type_p (type)) | |
183 | { | |
184 | /* Preserve unsignedness if not really getting any wider. */ | |
8df83eae | 185 | if (TYPE_UNSIGNED (type) |
ab393bf1 NB |
186 | && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) |
187 | return unsigned_type_node; | |
188 | return integer_type_node; | |
189 | } | |
190 | ||
191 | return type; | |
192 | } | |
193 | ||
400fbf9f JW |
194 | /* Return a variant of TYPE which has all the type qualifiers of LIKE |
195 | as well as those of TYPE. */ | |
196 | ||
197 | static tree | |
2f6e4e97 | 198 | qualify_type (tree type, tree like) |
400fbf9f | 199 | { |
2f6e4e97 | 200 | return c_build_qualified_type (type, |
afbadaa7 | 201 | TYPE_QUALS (type) | TYPE_QUALS (like)); |
400fbf9f JW |
202 | } |
203 | \f | |
10bc1b1b | 204 | /* Return the composite type of two compatible types. |
5305f6d7 | 205 | |
10bc1b1b JM |
206 | We assume that comptypes has already been done and returned |
207 | nonzero; if that isn't so, this may crash. In particular, we | |
208 | assume that qualifiers match. */ | |
400fbf9f JW |
209 | |
210 | tree | |
10bc1b1b | 211 | composite_type (tree t1, tree t2) |
400fbf9f | 212 | { |
b3694847 SS |
213 | enum tree_code code1; |
214 | enum tree_code code2; | |
4b027d16 | 215 | tree attributes; |
400fbf9f JW |
216 | |
217 | /* Save time if the two types are the same. */ | |
218 | ||
219 | if (t1 == t2) return t1; | |
220 | ||
221 | /* If one type is nonsense, use the other. */ | |
222 | if (t1 == error_mark_node) | |
223 | return t2; | |
224 | if (t2 == error_mark_node) | |
225 | return t1; | |
226 | ||
10bc1b1b JM |
227 | code1 = TREE_CODE (t1); |
228 | code2 = TREE_CODE (t2); | |
229 | ||
d9525bec | 230 | /* Merge the attributes. */ |
5fd9b178 | 231 | attributes = targetm.merge_type_attributes (t1, t2); |
4b027d16 | 232 | |
10bc1b1b JM |
233 | /* If one is an enumerated type and the other is the compatible |
234 | integer type, the composite type might be either of the two | |
235 | (DR#013 question 3). For consistency, use the enumerated type as | |
236 | the composite type. */ | |
400fbf9f | 237 | |
10bc1b1b JM |
238 | if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE) |
239 | return t1; | |
240 | if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE) | |
241 | return t2; | |
75326e8c | 242 | |
10bc1b1b JM |
243 | if (code1 != code2) |
244 | abort (); | |
b6a10c9f | 245 | |
400fbf9f JW |
246 | switch (code1) |
247 | { | |
400fbf9f | 248 | case POINTER_TYPE: |
10bc1b1b | 249 | /* For two pointers, do this recursively on the target type. */ |
400fbf9f | 250 | { |
3932261a MM |
251 | tree pointed_to_1 = TREE_TYPE (t1); |
252 | tree pointed_to_2 = TREE_TYPE (t2); | |
10bc1b1b JM |
253 | tree target = composite_type (pointed_to_1, pointed_to_2); |
254 | t1 = build_pointer_type (target); | |
fe7080d2 AP |
255 | t1 = build_type_attribute_variant (t1, attributes); |
256 | return qualify_type (t1, t2); | |
400fbf9f | 257 | } |
400fbf9f JW |
258 | |
259 | case ARRAY_TYPE: | |
260 | { | |
10bc1b1b | 261 | tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
de46b2fe AP |
262 | |
263 | /* We should not have any type quals on arrays at all. */ | |
264 | if (TYPE_QUALS (t1) || TYPE_QUALS (t2)) | |
265 | abort (); | |
266 | ||
400fbf9f JW |
267 | /* Save space: see if the result is identical to one of the args. */ |
268 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) | |
4b027d16 | 269 | return build_type_attribute_variant (t1, attributes); |
400fbf9f | 270 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) |
4b027d16 | 271 | return build_type_attribute_variant (t2, attributes); |
de46b2fe AP |
272 | |
273 | if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) | |
274 | return build_type_attribute_variant (t1, attributes); | |
275 | if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) | |
276 | return build_type_attribute_variant (t2, attributes); | |
277 | ||
400fbf9f | 278 | /* Merge the element types, and have a size if either arg has one. */ |
4b027d16 | 279 | t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); |
de46b2fe | 280 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
281 | } |
282 | ||
283 | case FUNCTION_TYPE: | |
284 | /* Function types: prefer the one that specified arg types. | |
285 | If both do, merge the arg types. Also merge the return types. */ | |
286 | { | |
10bc1b1b | 287 | tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
400fbf9f JW |
288 | tree p1 = TYPE_ARG_TYPES (t1); |
289 | tree p2 = TYPE_ARG_TYPES (t2); | |
290 | int len; | |
291 | tree newargs, n; | |
292 | int i; | |
293 | ||
294 | /* Save space: see if the result is identical to one of the args. */ | |
295 | if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2)) | |
4b027d16 | 296 | return build_type_attribute_variant (t1, attributes); |
400fbf9f | 297 | if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1)) |
4b027d16 | 298 | return build_type_attribute_variant (t2, attributes); |
400fbf9f JW |
299 | |
300 | /* Simple way if one arg fails to specify argument types. */ | |
301 | if (TYPE_ARG_TYPES (t1) == 0) | |
4b027d16 | 302 | { |
fe7080d2 AP |
303 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); |
304 | t1 = build_type_attribute_variant (t1, attributes); | |
305 | return qualify_type (t1, t2); | |
4b027d16 | 306 | } |
400fbf9f | 307 | if (TYPE_ARG_TYPES (t2) == 0) |
4b027d16 RK |
308 | { |
309 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); | |
fe7080d2 AP |
310 | t1 = build_type_attribute_variant (t1, attributes); |
311 | return qualify_type (t1, t2); | |
4b027d16 | 312 | } |
400fbf9f JW |
313 | |
314 | /* If both args specify argument types, we must merge the two | |
315 | lists, argument by argument. */ | |
f75fbaf7 ZW |
316 | /* Tell global_bindings_p to return false so that variable_size |
317 | doesn't abort on VLAs in parameter types. */ | |
318 | c_override_global_bindings_to_false = true; | |
2f4e8f2b | 319 | |
400fbf9f JW |
320 | len = list_length (p1); |
321 | newargs = 0; | |
322 | ||
323 | for (i = 0; i < len; i++) | |
8d9bfdc5 | 324 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
400fbf9f JW |
325 | |
326 | n = newargs; | |
327 | ||
328 | for (; p1; | |
329 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) | |
330 | { | |
331 | /* A null type means arg type is not specified. | |
332 | Take whatever the other function type has. */ | |
333 | if (TREE_VALUE (p1) == 0) | |
334 | { | |
335 | TREE_VALUE (n) = TREE_VALUE (p2); | |
336 | goto parm_done; | |
337 | } | |
338 | if (TREE_VALUE (p2) == 0) | |
339 | { | |
340 | TREE_VALUE (n) = TREE_VALUE (p1); | |
341 | goto parm_done; | |
342 | } | |
2f6e4e97 | 343 | |
400fbf9f JW |
344 | /* Given wait (union {union wait *u; int *i} *) |
345 | and wait (union wait *), | |
346 | prefer union wait * as type of parm. */ | |
347 | if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE | |
348 | && TREE_VALUE (p1) != TREE_VALUE (p2)) | |
349 | { | |
350 | tree memb; | |
351 | for (memb = TYPE_FIELDS (TREE_VALUE (p1)); | |
352 | memb; memb = TREE_CHAIN (memb)) | |
132da1a5 | 353 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2))) |
400fbf9f JW |
354 | { |
355 | TREE_VALUE (n) = TREE_VALUE (p2); | |
356 | if (pedantic) | |
89abf8d1 | 357 | pedwarn ("function types not truly compatible in ISO C"); |
400fbf9f JW |
358 | goto parm_done; |
359 | } | |
360 | } | |
361 | if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE | |
362 | && TREE_VALUE (p2) != TREE_VALUE (p1)) | |
363 | { | |
364 | tree memb; | |
365 | for (memb = TYPE_FIELDS (TREE_VALUE (p2)); | |
366 | memb; memb = TREE_CHAIN (memb)) | |
132da1a5 | 367 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1))) |
400fbf9f JW |
368 | { |
369 | TREE_VALUE (n) = TREE_VALUE (p1); | |
370 | if (pedantic) | |
89abf8d1 | 371 | pedwarn ("function types not truly compatible in ISO C"); |
400fbf9f JW |
372 | goto parm_done; |
373 | } | |
374 | } | |
10bc1b1b | 375 | TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2)); |
400fbf9f JW |
376 | parm_done: ; |
377 | } | |
378 | ||
f75fbaf7 | 379 | c_override_global_bindings_to_false = false; |
4b027d16 | 380 | t1 = build_function_type (valtype, newargs); |
fe7080d2 | 381 | t1 = qualify_type (t1, t2); |
0f41302f | 382 | /* ... falls through ... */ |
400fbf9f JW |
383 | } |
384 | ||
385 | default: | |
4b027d16 | 386 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
387 | } |
388 | ||
389 | } | |
10bc1b1b JM |
390 | |
391 | /* Return the type of a conditional expression between pointers to | |
392 | possibly differently qualified versions of compatible types. | |
393 | ||
394 | We assume that comp_target_types has already been done and returned | |
395 | nonzero; if that isn't so, this may crash. */ | |
396 | ||
397 | static tree | |
398 | common_pointer_type (tree t1, tree t2) | |
399 | { | |
400 | tree attributes; | |
401 | tree pointed_to_1; | |
402 | tree pointed_to_2; | |
403 | tree target; | |
404 | ||
405 | /* Save time if the two types are the same. */ | |
406 | ||
407 | if (t1 == t2) return t1; | |
408 | ||
409 | /* If one type is nonsense, use the other. */ | |
410 | if (t1 == error_mark_node) | |
411 | return t2; | |
412 | if (t2 == error_mark_node) | |
413 | return t1; | |
414 | ||
415 | if (TREE_CODE (t1) != POINTER_TYPE || TREE_CODE (t2) != POINTER_TYPE) | |
416 | abort (); | |
417 | ||
418 | /* Merge the attributes. */ | |
419 | attributes = targetm.merge_type_attributes (t1, t2); | |
420 | ||
421 | /* Find the composite type of the target types, and combine the | |
422 | qualifiers of the two types' targets. */ | |
423 | pointed_to_1 = TREE_TYPE (t1); | |
424 | pointed_to_2 = TREE_TYPE (t2); | |
425 | target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1), | |
426 | TYPE_MAIN_VARIANT (pointed_to_2)); | |
427 | t1 = build_pointer_type (c_build_qualified_type | |
428 | (target, | |
429 | TYPE_QUALS (pointed_to_1) | | |
430 | TYPE_QUALS (pointed_to_2))); | |
431 | return build_type_attribute_variant (t1, attributes); | |
432 | } | |
433 | ||
434 | /* Return the common type for two arithmetic types under the usual | |
435 | arithmetic conversions. The default conversions have already been | |
436 | applied, and enumerated types converted to their compatible integer | |
437 | types. The resulting type is unqualified and has no attributes. | |
438 | ||
439 | This is the type for the result of most arithmetic operations | |
440 | if the operands have the given two types. */ | |
441 | ||
442 | tree | |
443 | common_type (tree t1, tree t2) | |
444 | { | |
445 | enum tree_code code1; | |
446 | enum tree_code code2; | |
447 | ||
448 | /* If one type is nonsense, use the other. */ | |
449 | if (t1 == error_mark_node) | |
450 | return t2; | |
451 | if (t2 == error_mark_node) | |
452 | return t1; | |
453 | ||
454 | if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED) | |
455 | t1 = TYPE_MAIN_VARIANT (t1); | |
456 | ||
457 | if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED) | |
458 | t2 = TYPE_MAIN_VARIANT (t2); | |
459 | ||
460 | if (TYPE_ATTRIBUTES (t1) != NULL_TREE) | |
461 | t1 = build_type_attribute_variant (t1, NULL_TREE); | |
462 | ||
463 | if (TYPE_ATTRIBUTES (t2) != NULL_TREE) | |
464 | t2 = build_type_attribute_variant (t2, NULL_TREE); | |
465 | ||
466 | /* Save time if the two types are the same. */ | |
467 | ||
468 | if (t1 == t2) return t1; | |
469 | ||
470 | code1 = TREE_CODE (t1); | |
471 | code2 = TREE_CODE (t2); | |
472 | ||
473 | if (code1 != VECTOR_TYPE && code1 != COMPLEX_TYPE | |
474 | && code1 != REAL_TYPE && code1 != INTEGER_TYPE) | |
475 | abort (); | |
476 | ||
477 | if (code2 != VECTOR_TYPE && code2 != COMPLEX_TYPE | |
478 | && code2 != REAL_TYPE && code2 != INTEGER_TYPE) | |
479 | abort (); | |
480 | ||
481 | /* If one type is a vector type, return that type. (How the usual | |
482 | arithmetic conversions apply to the vector types extension is not | |
483 | precisely specified.) */ | |
484 | if (code1 == VECTOR_TYPE) | |
485 | return t1; | |
486 | ||
487 | if (code2 == VECTOR_TYPE) | |
488 | return t2; | |
489 | ||
490 | /* If one type is complex, form the common type of the non-complex | |
491 | components, then make that complex. Use T1 or T2 if it is the | |
492 | required type. */ | |
493 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
494 | { | |
495 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
496 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
497 | tree subtype = common_type (subtype1, subtype2); | |
498 | ||
499 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
500 | return t1; | |
501 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
502 | return t2; | |
503 | else | |
504 | return build_complex_type (subtype); | |
505 | } | |
506 | ||
507 | /* If only one is real, use it as the result. */ | |
508 | ||
509 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
510 | return t1; | |
511 | ||
512 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
513 | return t2; | |
514 | ||
515 | /* Both real or both integers; use the one with greater precision. */ | |
516 | ||
517 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
518 | return t1; | |
519 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
520 | return t2; | |
521 | ||
522 | /* Same precision. Prefer long longs to longs to ints when the | |
523 | same precision, following the C99 rules on integer type rank | |
524 | (which are equivalent to the C90 rules for C90 types). */ | |
525 | ||
526 | if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node | |
527 | || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node) | |
528 | return long_long_unsigned_type_node; | |
529 | ||
530 | if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node | |
531 | || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node) | |
532 | { | |
533 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
534 | return long_long_unsigned_type_node; | |
535 | else | |
536 | return long_long_integer_type_node; | |
537 | } | |
538 | ||
539 | if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node | |
540 | || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) | |
541 | return long_unsigned_type_node; | |
542 | ||
543 | if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node | |
544 | || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) | |
545 | { | |
546 | /* But preserve unsignedness from the other type, | |
547 | since long cannot hold all the values of an unsigned int. */ | |
548 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
549 | return long_unsigned_type_node; | |
550 | else | |
551 | return long_integer_type_node; | |
552 | } | |
553 | ||
554 | /* Likewise, prefer long double to double even if same size. */ | |
555 | if (TYPE_MAIN_VARIANT (t1) == long_double_type_node | |
556 | || TYPE_MAIN_VARIANT (t2) == long_double_type_node) | |
557 | return long_double_type_node; | |
558 | ||
559 | /* Otherwise prefer the unsigned one. */ | |
560 | ||
561 | if (TYPE_UNSIGNED (t1)) | |
562 | return t1; | |
563 | else | |
564 | return t2; | |
565 | } | |
400fbf9f JW |
566 | \f |
567 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment | |
568 | or various other operations. Return 2 if they are compatible | |
569 | but a warning may be needed if you use them together. */ | |
570 | ||
571 | int | |
132da1a5 | 572 | comptypes (tree type1, tree type2) |
400fbf9f | 573 | { |
b3694847 SS |
574 | tree t1 = type1; |
575 | tree t2 = type2; | |
4b027d16 | 576 | int attrval, val; |
400fbf9f JW |
577 | |
578 | /* Suppress errors caused by previously reported errors. */ | |
579 | ||
8d47dfc5 RH |
580 | if (t1 == t2 || !t1 || !t2 |
581 | || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) | |
400fbf9f JW |
582 | return 1; |
583 | ||
21318741 RK |
584 | /* If either type is the internal version of sizetype, return the |
585 | language version. */ | |
586 | if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1) | |
eb1a2c88 DN |
587 | && TYPE_ORIG_SIZE_TYPE (t1)) |
588 | t1 = TYPE_ORIG_SIZE_TYPE (t1); | |
21318741 RK |
589 | |
590 | if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2) | |
eb1a2c88 DN |
591 | && TYPE_ORIG_SIZE_TYPE (t2)) |
592 | t2 = TYPE_ORIG_SIZE_TYPE (t2); | |
593 | ||
21318741 | 594 | |
bca63328 JM |
595 | /* Enumerated types are compatible with integer types, but this is |
596 | not transitive: two enumerated types in the same translation unit | |
597 | are compatible with each other only if they are the same type. */ | |
400fbf9f | 598 | |
bca63328 | 599 | if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE) |
8df83eae | 600 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1)); |
bca63328 | 601 | else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE) |
8df83eae | 602 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2)); |
400fbf9f JW |
603 | |
604 | if (t1 == t2) | |
605 | return 1; | |
606 | ||
607 | /* Different classes of types can't be compatible. */ | |
608 | ||
3aeb3655 EC |
609 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
610 | return 0; | |
400fbf9f | 611 | |
118a3a8b | 612 | /* Qualifiers must match. C99 6.7.3p9 */ |
400fbf9f | 613 | |
3932261a | 614 | if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) |
400fbf9f JW |
615 | return 0; |
616 | ||
08632da2 RS |
617 | /* Allow for two different type nodes which have essentially the same |
618 | definition. Note that we already checked for equality of the type | |
38e01259 | 619 | qualifiers (just above). */ |
400fbf9f JW |
620 | |
621 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
622 | return 1; | |
623 | ||
4b027d16 | 624 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ |
5fd9b178 | 625 | if (! (attrval = targetm.comp_type_attributes (t1, t2))) |
4b027d16 RK |
626 | return 0; |
627 | ||
628 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
629 | val = 0; | |
630 | ||
400fbf9f JW |
631 | switch (TREE_CODE (t1)) |
632 | { | |
633 | case POINTER_TYPE: | |
264fa2db ZL |
634 | /* We must give ObjC the first crack at comparing pointers, since |
635 | protocol qualifiers may be involved. */ | |
636 | if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0) | |
637 | break; | |
4b027d16 | 638 | val = (TREE_TYPE (t1) == TREE_TYPE (t2) |
132da1a5 | 639 | ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2))); |
4b027d16 | 640 | break; |
400fbf9f JW |
641 | |
642 | case FUNCTION_TYPE: | |
132da1a5 | 643 | val = function_types_compatible_p (t1, t2); |
4b027d16 | 644 | break; |
400fbf9f JW |
645 | |
646 | case ARRAY_TYPE: | |
647 | { | |
400fbf9f JW |
648 | tree d1 = TYPE_DOMAIN (t1); |
649 | tree d2 = TYPE_DOMAIN (t2); | |
3f85558f RH |
650 | bool d1_variable, d2_variable; |
651 | bool d1_zero, d2_zero; | |
4b027d16 | 652 | val = 1; |
400fbf9f JW |
653 | |
654 | /* Target types must match incl. qualifiers. */ | |
655 | if (TREE_TYPE (t1) != TREE_TYPE (t2) | |
132da1a5 | 656 | && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))) |
400fbf9f JW |
657 | return 0; |
658 | ||
659 | /* Sizes must match unless one is missing or variable. */ | |
3f85558f | 660 | if (d1 == 0 || d2 == 0 || d1 == d2) |
4b027d16 | 661 | break; |
400fbf9f | 662 | |
3f85558f RH |
663 | d1_zero = ! TYPE_MAX_VALUE (d1); |
664 | d2_zero = ! TYPE_MAX_VALUE (d2); | |
665 | ||
666 | d1_variable = (! d1_zero | |
667 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
668 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
669 | d2_variable = (! d2_zero | |
670 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
671 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
672 | ||
673 | if (d1_variable || d2_variable) | |
674 | break; | |
675 | if (d1_zero && d2_zero) | |
676 | break; | |
677 | if (d1_zero || d2_zero | |
678 | || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)) | |
05bccae2 RK |
679 | || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2))) |
680 | val = 0; | |
681 | ||
4b027d16 | 682 | break; |
400fbf9f JW |
683 | } |
684 | ||
685 | case RECORD_TYPE: | |
264fa2db ZL |
686 | /* We are dealing with two distinct structs. In assorted Objective-C |
687 | corner cases, however, these can still be deemed equivalent. */ | |
37fa72e9 | 688 | if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1) |
4b027d16 | 689 | val = 1; |
d1bd0ded GK |
690 | |
691 | case ENUMERAL_TYPE: | |
692 | case UNION_TYPE: | |
766beae1 | 693 | if (val != 1 && !same_translation_unit_p (t1, t2)) |
132da1a5 | 694 | val = tagged_types_tu_compatible_p (t1, t2); |
4b027d16 | 695 | break; |
e9a25f70 | 696 | |
62e1dfcf | 697 | case VECTOR_TYPE: |
cc27e657 | 698 | val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) |
132da1a5 | 699 | && comptypes (TREE_TYPE (t1), TREE_TYPE (t2)); |
62e1dfcf NC |
700 | break; |
701 | ||
e9a25f70 JL |
702 | default: |
703 | break; | |
400fbf9f | 704 | } |
4b027d16 | 705 | return attrval == 2 && val == 1 ? 2 : val; |
400fbf9f JW |
706 | } |
707 | ||
708 | /* Return 1 if TTL and TTR are pointers to types that are equivalent, | |
1074d9d4 NP |
709 | ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it |
710 | to 1 or 0 depending if the check of the pointer types is meant to | |
711 | be reflexive or not (typically, assignments are not reflexive, | |
712 | while comparisons are reflexive). | |
713 | */ | |
400fbf9f JW |
714 | |
715 | static int | |
2f6e4e97 | 716 | comp_target_types (tree ttl, tree ttr, int reflexive) |
400fbf9f | 717 | { |
392202b0 | 718 | int val; |
8b40563c | 719 | |
b50d021d | 720 | /* Give objc_comptypes a crack at letting these types through. */ |
1074d9d4 | 721 | if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0) |
392202b0 | 722 | return val; |
8b40563c | 723 | |
392202b0 | 724 | val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)), |
132da1a5 | 725 | TYPE_MAIN_VARIANT (TREE_TYPE (ttr))); |
8b40563c | 726 | |
400fbf9f JW |
727 | if (val == 2 && pedantic) |
728 | pedwarn ("types are not quite compatible"); | |
729 | return val; | |
730 | } | |
731 | \f | |
732 | /* Subroutines of `comptypes'. */ | |
733 | ||
f75fbaf7 ZW |
734 | /* Determine whether two trees derive from the same translation unit. |
735 | If the CONTEXT chain ends in a null, that tree's context is still | |
736 | being parsed, so if two trees have context chains ending in null, | |
766beae1 | 737 | they're in the same translation unit. */ |
f75fbaf7 | 738 | int |
766beae1 ZW |
739 | same_translation_unit_p (tree t1, tree t2) |
740 | { | |
741 | while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL) | |
742 | switch (TREE_CODE_CLASS (TREE_CODE (t1))) | |
743 | { | |
744 | case 'd': t1 = DECL_CONTEXT (t1); break; | |
745 | case 't': t1 = TYPE_CONTEXT (t1); break; | |
90afe2c9 | 746 | case 'x': t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */ |
766beae1 ZW |
747 | default: abort (); |
748 | } | |
749 | ||
750 | while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL) | |
751 | switch (TREE_CODE_CLASS (TREE_CODE (t2))) | |
752 | { | |
63185fab | 753 | case 'd': t2 = DECL_CONTEXT (t2); break; |
766beae1 | 754 | case 't': t2 = TYPE_CONTEXT (t2); break; |
90afe2c9 | 755 | case 'x': t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */ |
766beae1 ZW |
756 | default: abort (); |
757 | } | |
758 | ||
759 | return t1 == t2; | |
760 | } | |
761 | ||
d1bd0ded GK |
762 | /* The C standard says that two structures in different translation |
763 | units are compatible with each other only if the types of their | |
764 | fields are compatible (among other things). So, consider two copies | |
765 | of this structure: */ | |
766 | ||
767 | struct tagged_tu_seen { | |
768 | const struct tagged_tu_seen * next; | |
769 | tree t1; | |
770 | tree t2; | |
771 | }; | |
772 | ||
773 | /* Can they be compatible with each other? We choose to break the | |
774 | recursion by allowing those types to be compatible. */ | |
775 | ||
776 | static const struct tagged_tu_seen * tagged_tu_seen_base; | |
777 | ||
778 | /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are | |
779 | compatible. If the two types are not the same (which has been | |
780 | checked earlier), this can only happen when multiple translation | |
781 | units are being compiled. See C99 6.2.7 paragraph 1 for the exact | |
782 | rules. */ | |
783 | ||
784 | static int | |
132da1a5 | 785 | tagged_types_tu_compatible_p (tree t1, tree t2) |
d1bd0ded GK |
786 | { |
787 | tree s1, s2; | |
788 | bool needs_warning = false; | |
3aeb3655 | 789 | |
d1bd0ded GK |
790 | /* We have to verify that the tags of the types are the same. This |
791 | is harder than it looks because this may be a typedef, so we have | |
792 | to go look at the original type. It may even be a typedef of a | |
6de9cd9a DN |
793 | typedef... |
794 | In the case of compiler-created builtin structs the TYPE_DECL | |
795 | may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */ | |
dea984dc ILT |
796 | while (TYPE_NAME (t1) |
797 | && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL | |
798 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t1))) | |
d1bd0ded GK |
799 | t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1)); |
800 | ||
dea984dc ILT |
801 | while (TYPE_NAME (t2) |
802 | && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL | |
803 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t2))) | |
d1bd0ded GK |
804 | t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2)); |
805 | ||
806 | /* C90 didn't have the requirement that the two tags be the same. */ | |
807 | if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2)) | |
808 | return 0; | |
3aeb3655 | 809 | |
d1bd0ded GK |
810 | /* C90 didn't say what happened if one or both of the types were |
811 | incomplete; we choose to follow C99 rules here, which is that they | |
812 | are compatible. */ | |
813 | if (TYPE_SIZE (t1) == NULL | |
814 | || TYPE_SIZE (t2) == NULL) | |
815 | return 1; | |
3aeb3655 | 816 | |
d1bd0ded GK |
817 | { |
818 | const struct tagged_tu_seen * tts_i; | |
819 | for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next) | |
820 | if (tts_i->t1 == t1 && tts_i->t2 == t2) | |
821 | return 1; | |
822 | } | |
3aeb3655 | 823 | |
d1bd0ded GK |
824 | switch (TREE_CODE (t1)) |
825 | { | |
826 | case ENUMERAL_TYPE: | |
827 | { | |
3aeb3655 | 828 | |
71cc389b | 829 | /* Speed up the case where the type values are in the same order. */ |
dedbabed AP |
830 | tree tv1 = TYPE_VALUES (t1); |
831 | tree tv2 = TYPE_VALUES (t2); | |
3aeb3655 | 832 | |
dedbabed AP |
833 | if (tv1 == tv2) |
834 | return 1; | |
3aeb3655 | 835 | |
f38f747d | 836 | for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2)) |
dedbabed | 837 | { |
8cd6bdd1 | 838 | if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2)) |
dedbabed AP |
839 | break; |
840 | if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1) | |
841 | return 0; | |
842 | } | |
3aeb3655 | 843 | |
dedbabed AP |
844 | if (tv1 == NULL_TREE && tv2 == NULL_TREE) |
845 | return 1; | |
846 | if (tv1 == NULL_TREE || tv2 == NULL_TREE) | |
847 | return 0; | |
3aeb3655 | 848 | |
d1bd0ded GK |
849 | if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2))) |
850 | return 0; | |
3aeb3655 | 851 | |
d1bd0ded GK |
852 | for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1)) |
853 | { | |
854 | s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2)); | |
855 | if (s2 == NULL | |
856 | || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1) | |
857 | return 0; | |
858 | } | |
859 | return 1; | |
860 | } | |
861 | ||
862 | case UNION_TYPE: | |
863 | { | |
864 | if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2))) | |
865 | return 0; | |
866 | ||
867 | for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1)) | |
868 | { | |
869 | bool ok = false; | |
870 | struct tagged_tu_seen tts; | |
871 | ||
872 | tts.next = tagged_tu_seen_base; | |
873 | tts.t1 = t1; | |
874 | tts.t2 = t2; | |
875 | tagged_tu_seen_base = &tts; | |
3aeb3655 | 876 | |
d1bd0ded | 877 | if (DECL_NAME (s1) != NULL) |
398ce3dd | 878 | for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2)) |
d1bd0ded GK |
879 | if (DECL_NAME (s1) == DECL_NAME (s2)) |
880 | { | |
881 | int result; | |
132da1a5 | 882 | result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2)); |
d1bd0ded GK |
883 | if (result == 0) |
884 | break; | |
885 | if (result == 2) | |
886 | needs_warning = true; | |
3aeb3655 | 887 | |
d1bd0ded GK |
888 | if (TREE_CODE (s1) == FIELD_DECL |
889 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
890 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
891 | break; | |
892 | ||
893 | ok = true; | |
894 | break; | |
895 | } | |
896 | tagged_tu_seen_base = tts.next; | |
897 | if (! ok) | |
898 | return 0; | |
899 | } | |
900 | return needs_warning ? 2 : 1; | |
901 | } | |
902 | ||
903 | case RECORD_TYPE: | |
904 | { | |
905 | struct tagged_tu_seen tts; | |
3aeb3655 | 906 | |
d1bd0ded GK |
907 | tts.next = tagged_tu_seen_base; |
908 | tts.t1 = t1; | |
909 | tts.t2 = t2; | |
910 | tagged_tu_seen_base = &tts; | |
3aeb3655 EC |
911 | |
912 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); | |
d1bd0ded GK |
913 | s1 && s2; |
914 | s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2)) | |
915 | { | |
916 | int result; | |
917 | if (TREE_CODE (s1) != TREE_CODE (s2) | |
918 | || DECL_NAME (s1) != DECL_NAME (s2)) | |
919 | break; | |
132da1a5 | 920 | result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2)); |
d1bd0ded GK |
921 | if (result == 0) |
922 | break; | |
923 | if (result == 2) | |
924 | needs_warning = true; | |
3aeb3655 | 925 | |
d1bd0ded GK |
926 | if (TREE_CODE (s1) == FIELD_DECL |
927 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
928 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
929 | break; | |
930 | } | |
931 | tagged_tu_seen_base = tts.next; | |
932 | if (s1 && s2) | |
933 | return 0; | |
934 | return needs_warning ? 2 : 1; | |
935 | } | |
936 | ||
937 | default: | |
938 | abort (); | |
939 | } | |
940 | } | |
941 | ||
400fbf9f JW |
942 | /* Return 1 if two function types F1 and F2 are compatible. |
943 | If either type specifies no argument types, | |
944 | the other must specify a fixed number of self-promoting arg types. | |
2f6e4e97 | 945 | Otherwise, if one type specifies only the number of arguments, |
400fbf9f JW |
946 | the other must specify that number of self-promoting arg types. |
947 | Otherwise, the argument types must match. */ | |
948 | ||
949 | static int | |
132da1a5 | 950 | function_types_compatible_p (tree f1, tree f2) |
400fbf9f JW |
951 | { |
952 | tree args1, args2; | |
953 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
954 | int val = 1; | |
955 | int val1; | |
a6fdc086 GK |
956 | tree ret1, ret2; |
957 | ||
958 | ret1 = TREE_TYPE (f1); | |
959 | ret2 = TREE_TYPE (f2); | |
960 | ||
e508a019 JM |
961 | /* 'volatile' qualifiers on a function's return type used to mean |
962 | the function is noreturn. */ | |
963 | if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2)) | |
a6fdc086 GK |
964 | pedwarn ("function return types not compatible due to `volatile'"); |
965 | if (TYPE_VOLATILE (ret1)) | |
966 | ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1), | |
967 | TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE); | |
968 | if (TYPE_VOLATILE (ret2)) | |
969 | ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2), | |
970 | TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE); | |
132da1a5 | 971 | val = comptypes (ret1, ret2); |
a6fdc086 | 972 | if (val == 0) |
400fbf9f JW |
973 | return 0; |
974 | ||
975 | args1 = TYPE_ARG_TYPES (f1); | |
976 | args2 = TYPE_ARG_TYPES (f2); | |
977 | ||
978 | /* An unspecified parmlist matches any specified parmlist | |
979 | whose argument types don't need default promotions. */ | |
980 | ||
981 | if (args1 == 0) | |
982 | { | |
983 | if (!self_promoting_args_p (args2)) | |
984 | return 0; | |
985 | /* If one of these types comes from a non-prototype fn definition, | |
986 | compare that with the other type's arglist. | |
987 | If they don't match, ask for a warning (but no error). */ | |
988 | if (TYPE_ACTUAL_ARG_TYPES (f1) | |
132da1a5 | 989 | && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1))) |
400fbf9f JW |
990 | val = 2; |
991 | return val; | |
992 | } | |
993 | if (args2 == 0) | |
994 | { | |
995 | if (!self_promoting_args_p (args1)) | |
996 | return 0; | |
997 | if (TYPE_ACTUAL_ARG_TYPES (f2) | |
132da1a5 | 998 | && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2))) |
400fbf9f JW |
999 | val = 2; |
1000 | return val; | |
1001 | } | |
1002 | ||
1003 | /* Both types have argument lists: compare them and propagate results. */ | |
132da1a5 | 1004 | val1 = type_lists_compatible_p (args1, args2); |
400fbf9f JW |
1005 | return val1 != 1 ? val1 : val; |
1006 | } | |
1007 | ||
1008 | /* Check two lists of types for compatibility, | |
1009 | returning 0 for incompatible, 1 for compatible, | |
1010 | or 2 for compatible with warning. */ | |
1011 | ||
1012 | static int | |
132da1a5 | 1013 | type_lists_compatible_p (tree args1, tree args2) |
400fbf9f JW |
1014 | { |
1015 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1016 | int val = 1; | |
9d5f3e49 | 1017 | int newval = 0; |
400fbf9f JW |
1018 | |
1019 | while (1) | |
1020 | { | |
1021 | if (args1 == 0 && args2 == 0) | |
1022 | return val; | |
1023 | /* If one list is shorter than the other, | |
1024 | they fail to match. */ | |
1025 | if (args1 == 0 || args2 == 0) | |
1026 | return 0; | |
1027 | /* A null pointer instead of a type | |
1028 | means there is supposed to be an argument | |
1029 | but nothing is specified about what type it has. | |
1030 | So match anything that self-promotes. */ | |
1031 | if (TREE_VALUE (args1) == 0) | |
1032 | { | |
ab393bf1 | 1033 | if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2)) |
400fbf9f JW |
1034 | return 0; |
1035 | } | |
1036 | else if (TREE_VALUE (args2) == 0) | |
1037 | { | |
ab393bf1 | 1038 | if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1)) |
400fbf9f JW |
1039 | return 0; |
1040 | } | |
8f5b6d29 SB |
1041 | /* If one of the lists has an error marker, ignore this arg. */ |
1042 | else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK | |
1043 | || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK) | |
1044 | ; | |
2f6e4e97 | 1045 | else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)), |
132da1a5 | 1046 | TYPE_MAIN_VARIANT (TREE_VALUE (args2))))) |
400fbf9f JW |
1047 | { |
1048 | /* Allow wait (union {union wait *u; int *i} *) | |
1049 | and wait (union wait *) to be compatible. */ | |
1050 | if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE | |
ea3373cd RK |
1051 | && (TYPE_NAME (TREE_VALUE (args1)) == 0 |
1052 | || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1))) | |
400fbf9f JW |
1053 | && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST |
1054 | && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)), | |
1055 | TYPE_SIZE (TREE_VALUE (args2)))) | |
1056 | { | |
1057 | tree memb; | |
1058 | for (memb = TYPE_FIELDS (TREE_VALUE (args1)); | |
1059 | memb; memb = TREE_CHAIN (memb)) | |
132da1a5 | 1060 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2))) |
400fbf9f JW |
1061 | break; |
1062 | if (memb == 0) | |
1063 | return 0; | |
1064 | } | |
1065 | else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE | |
ea3373cd RK |
1066 | && (TYPE_NAME (TREE_VALUE (args2)) == 0 |
1067 | || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2))) | |
400fbf9f JW |
1068 | && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST |
1069 | && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)), | |
1070 | TYPE_SIZE (TREE_VALUE (args1)))) | |
1071 | { | |
1072 | tree memb; | |
1073 | for (memb = TYPE_FIELDS (TREE_VALUE (args2)); | |
1074 | memb; memb = TREE_CHAIN (memb)) | |
132da1a5 | 1075 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1))) |
400fbf9f JW |
1076 | break; |
1077 | if (memb == 0) | |
1078 | return 0; | |
1079 | } | |
1080 | else | |
1081 | return 0; | |
1082 | } | |
1083 | ||
1084 | /* comptypes said ok, but record if it said to warn. */ | |
1085 | if (newval > val) | |
1086 | val = newval; | |
1087 | ||
1088 | args1 = TREE_CHAIN (args1); | |
1089 | args2 = TREE_CHAIN (args2); | |
1090 | } | |
1091 | } | |
400fbf9f | 1092 | \f |
400fbf9f JW |
1093 | /* Compute the size to increment a pointer by. */ |
1094 | ||
1095 | tree | |
2f6e4e97 | 1096 | c_size_in_bytes (tree type) |
400fbf9f JW |
1097 | { |
1098 | enum tree_code code = TREE_CODE (type); | |
1099 | ||
fed3cef0 RK |
1100 | if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK) |
1101 | return size_one_node; | |
1102 | ||
d0f062fb | 1103 | if (!COMPLETE_OR_VOID_TYPE_P (type)) |
400fbf9f JW |
1104 | { |
1105 | error ("arithmetic on pointer to an incomplete type"); | |
fed3cef0 | 1106 | return size_one_node; |
400fbf9f JW |
1107 | } |
1108 | ||
1109 | /* Convert in case a char is more than one unit. */ | |
fed3cef0 RK |
1110 | return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
1111 | size_int (TYPE_PRECISION (char_type_node) | |
1112 | / BITS_PER_UNIT)); | |
400fbf9f | 1113 | } |
400fbf9f | 1114 | \f |
400fbf9f JW |
1115 | /* Return either DECL or its known constant value (if it has one). */ |
1116 | ||
56cb9733 | 1117 | tree |
2f6e4e97 | 1118 | decl_constant_value (tree decl) |
400fbf9f | 1119 | { |
a7c1916a | 1120 | if (/* Don't change a variable array bound or initial value to a constant |
4f976745 RK |
1121 | in a place where a variable is invalid. Note that DECL_INITIAL |
1122 | isn't valid for a PARM_DECL. */ | |
a7c1916a | 1123 | current_function_decl != 0 |
4f976745 | 1124 | && TREE_CODE (decl) != PARM_DECL |
400fbf9f | 1125 | && ! TREE_THIS_VOLATILE (decl) |
83bab8db | 1126 | && TREE_READONLY (decl) |
400fbf9f JW |
1127 | && DECL_INITIAL (decl) != 0 |
1128 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
1129 | /* This is invalid if initial value is not constant. | |
1130 | If it has either a function call, a memory reference, | |
1131 | or a variable, then re-evaluating it could give different results. */ | |
1132 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
1133 | /* Check for cases where this is sub-optimal, even though valid. */ | |
2f74f7e9 | 1134 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) |
400fbf9f JW |
1135 | return DECL_INITIAL (decl); |
1136 | return decl; | |
1137 | } | |
1138 | ||
2f74f7e9 JM |
1139 | /* Return either DECL or its known constant value (if it has one), but |
1140 | return DECL if pedantic or DECL has mode BLKmode. This is for | |
1141 | bug-compatibility with the old behavior of decl_constant_value | |
1142 | (before GCC 3.0); every use of this function is a bug and it should | |
1143 | be removed before GCC 3.1. It is not appropriate to use pedantic | |
1144 | in a way that affects optimization, and BLKmode is probably not the | |
1145 | right test for avoiding misoptimizations either. */ | |
1146 | ||
1147 | static tree | |
2f6e4e97 | 1148 | decl_constant_value_for_broken_optimization (tree decl) |
2f74f7e9 JM |
1149 | { |
1150 | if (pedantic || DECL_MODE (decl) == BLKmode) | |
1151 | return decl; | |
1152 | else | |
1153 | return decl_constant_value (decl); | |
1154 | } | |
1155 | ||
207bf485 JM |
1156 | |
1157 | /* Perform the default conversion of arrays and functions to pointers. | |
1158 | Return the result of converting EXP. For any other expression, just | |
1159 | return EXP. */ | |
1160 | ||
1161 | static tree | |
2f6e4e97 | 1162 | default_function_array_conversion (tree exp) |
207bf485 JM |
1163 | { |
1164 | tree orig_exp; | |
1165 | tree type = TREE_TYPE (exp); | |
1166 | enum tree_code code = TREE_CODE (type); | |
1167 | int not_lvalue = 0; | |
1168 | ||
1169 | /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as | |
2f6e4e97 | 1170 | an lvalue. |
207bf485 JM |
1171 | |
1172 | Do not use STRIP_NOPS here! It will remove conversions from pointer | |
1173 | to integer and cause infinite recursion. */ | |
1174 | orig_exp = exp; | |
1175 | while (TREE_CODE (exp) == NON_LVALUE_EXPR | |
1176 | || (TREE_CODE (exp) == NOP_EXPR | |
1177 | && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp))) | |
1178 | { | |
1179 | if (TREE_CODE (exp) == NON_LVALUE_EXPR) | |
1180 | not_lvalue = 1; | |
1181 | exp = TREE_OPERAND (exp, 0); | |
1182 | } | |
1183 | ||
487a92fe JM |
1184 | if (TREE_NO_WARNING (orig_exp)) |
1185 | TREE_NO_WARNING (exp) = 1; | |
207bf485 JM |
1186 | |
1187 | if (code == FUNCTION_TYPE) | |
1188 | { | |
1189 | return build_unary_op (ADDR_EXPR, exp, 0); | |
1190 | } | |
1191 | if (code == ARRAY_TYPE) | |
1192 | { | |
1193 | tree adr; | |
1194 | tree restype = TREE_TYPE (type); | |
1195 | tree ptrtype; | |
1196 | int constp = 0; | |
1197 | int volatilep = 0; | |
1198 | int lvalue_array_p; | |
1199 | ||
1200 | if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp)) | |
1201 | { | |
1202 | constp = TREE_READONLY (exp); | |
1203 | volatilep = TREE_THIS_VOLATILE (exp); | |
1204 | } | |
1205 | ||
1206 | if (TYPE_QUALS (type) || constp || volatilep) | |
2f6e4e97 | 1207 | restype |
207bf485 | 1208 | = c_build_qualified_type (restype, |
2f6e4e97 | 1209 | TYPE_QUALS (type) |
207bf485 JM |
1210 | | (constp * TYPE_QUAL_CONST) |
1211 | | (volatilep * TYPE_QUAL_VOLATILE)); | |
1212 | ||
1213 | if (TREE_CODE (exp) == INDIRECT_REF) | |
019c8e80 | 1214 | return convert (build_pointer_type (restype), |
207bf485 JM |
1215 | TREE_OPERAND (exp, 0)); |
1216 | ||
1217 | if (TREE_CODE (exp) == COMPOUND_EXPR) | |
1218 | { | |
1219 | tree op1 = default_conversion (TREE_OPERAND (exp, 1)); | |
53fb4de3 RS |
1220 | return build2 (COMPOUND_EXPR, TREE_TYPE (op1), |
1221 | TREE_OPERAND (exp, 0), op1); | |
207bf485 JM |
1222 | } |
1223 | ||
1224 | lvalue_array_p = !not_lvalue && lvalue_p (exp); | |
db3acfa5 | 1225 | if (!flag_isoc99 && !lvalue_array_p) |
207bf485 JM |
1226 | { |
1227 | /* Before C99, non-lvalue arrays do not decay to pointers. | |
1228 | Normally, using such an array would be invalid; but it can | |
1229 | be used correctly inside sizeof or as a statement expression. | |
1230 | Thus, do not give an error here; an error will result later. */ | |
1231 | return exp; | |
1232 | } | |
1233 | ||
1234 | ptrtype = build_pointer_type (restype); | |
1235 | ||
1236 | if (TREE_CODE (exp) == VAR_DECL) | |
1237 | { | |
44de5aeb RK |
1238 | /* We are making an ADDR_EXPR of ptrtype. This is a valid |
1239 | ADDR_EXPR because it's the best way of representing what | |
1240 | happens in C when we take the address of an array and place | |
1241 | it in a pointer to the element type. */ | |
207bf485 | 1242 | adr = build1 (ADDR_EXPR, ptrtype, exp); |
dffd7eb6 | 1243 | if (!c_mark_addressable (exp)) |
207bf485 | 1244 | return error_mark_node; |
207bf485 JM |
1245 | TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */ |
1246 | return adr; | |
1247 | } | |
1248 | /* This way is better for a COMPONENT_REF since it can | |
1249 | simplify the offset for a component. */ | |
1250 | adr = build_unary_op (ADDR_EXPR, exp, 1); | |
1251 | return convert (ptrtype, adr); | |
1252 | } | |
1253 | return exp; | |
1254 | } | |
1255 | ||
400fbf9f JW |
1256 | /* Perform default promotions for C data used in expressions. |
1257 | Arrays and functions are converted to pointers; | |
1258 | enumeral types or short or char, to int. | |
1259 | In addition, manifest constants symbols are replaced by their values. */ | |
1260 | ||
1261 | tree | |
2f6e4e97 | 1262 | default_conversion (tree exp) |
400fbf9f | 1263 | { |
157689c6 | 1264 | tree orig_exp; |
b3694847 SS |
1265 | tree type = TREE_TYPE (exp); |
1266 | enum tree_code code = TREE_CODE (type); | |
400fbf9f | 1267 | |
207bf485 JM |
1268 | if (code == FUNCTION_TYPE || code == ARRAY_TYPE) |
1269 | return default_function_array_conversion (exp); | |
1270 | ||
400fbf9f JW |
1271 | /* Constants can be used directly unless they're not loadable. */ |
1272 | if (TREE_CODE (exp) == CONST_DECL) | |
1273 | exp = DECL_INITIAL (exp); | |
d4424a75 RK |
1274 | |
1275 | /* Replace a nonvolatile const static variable with its value unless | |
1276 | it is an array, in which case we must be sure that taking the | |
1277 | address of the array produces consistent results. */ | |
1278 | else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE) | |
400fbf9f | 1279 | { |
2f74f7e9 | 1280 | exp = decl_constant_value_for_broken_optimization (exp); |
400fbf9f JW |
1281 | type = TREE_TYPE (exp); |
1282 | } | |
1283 | ||
a7d53fce | 1284 | /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as |
2f6e4e97 | 1285 | an lvalue. |
05bccae2 RK |
1286 | |
1287 | Do not use STRIP_NOPS here! It will remove conversions from pointer | |
a7d53fce | 1288 | to integer and cause infinite recursion. */ |
157689c6 | 1289 | orig_exp = exp; |
a7d53fce RS |
1290 | while (TREE_CODE (exp) == NON_LVALUE_EXPR |
1291 | || (TREE_CODE (exp) == NOP_EXPR | |
1292 | && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp))) | |
1293 | exp = TREE_OPERAND (exp, 0); | |
400fbf9f | 1294 | |
487a92fe JM |
1295 | if (TREE_NO_WARNING (orig_exp)) |
1296 | TREE_NO_WARNING (exp) = 1; | |
157689c6 | 1297 | |
400fbf9f JW |
1298 | /* Normally convert enums to int, |
1299 | but convert wide enums to something wider. */ | |
1300 | if (code == ENUMERAL_TYPE) | |
1301 | { | |
b0c48229 NB |
1302 | type = c_common_type_for_size (MAX (TYPE_PRECISION (type), |
1303 | TYPE_PRECISION (integer_type_node)), | |
1304 | ((TYPE_PRECISION (type) | |
1305 | >= TYPE_PRECISION (integer_type_node)) | |
8df83eae | 1306 | && TYPE_UNSIGNED (type))); |
05bccae2 | 1307 | |
400fbf9f JW |
1308 | return convert (type, exp); |
1309 | } | |
1310 | ||
9753f113 | 1311 | if (TREE_CODE (exp) == COMPONENT_REF |
05bccae2 | 1312 | && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)) |
cff9c407 | 1313 | /* If it's thinner than an int, promote it like a |
d72040f5 | 1314 | c_promoting_integer_type_p, otherwise leave it alone. */ |
05bccae2 RK |
1315 | && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), |
1316 | TYPE_PRECISION (integer_type_node))) | |
f458d1d5 | 1317 | return convert (integer_type_node, exp); |
9753f113 | 1318 | |
d72040f5 | 1319 | if (c_promoting_integer_type_p (type)) |
400fbf9f | 1320 | { |
f458d1d5 | 1321 | /* Preserve unsignedness if not really getting any wider. */ |
8df83eae | 1322 | if (TYPE_UNSIGNED (type) |
f458d1d5 | 1323 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
400fbf9f | 1324 | return convert (unsigned_type_node, exp); |
05bccae2 | 1325 | |
400fbf9f JW |
1326 | return convert (integer_type_node, exp); |
1327 | } | |
05bccae2 | 1328 | |
400fbf9f JW |
1329 | if (code == VOID_TYPE) |
1330 | { | |
1331 | error ("void value not ignored as it ought to be"); | |
1332 | return error_mark_node; | |
1333 | } | |
400fbf9f JW |
1334 | return exp; |
1335 | } | |
1336 | \f | |
e9b2c823 NB |
1337 | /* Look up COMPONENT in a structure or union DECL. |
1338 | ||
1339 | If the component name is not found, returns NULL_TREE. Otherwise, | |
1340 | the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL | |
1341 | stepping down the chain to the component, which is in the last | |
1342 | TREE_VALUE of the list. Normally the list is of length one, but if | |
1343 | the component is embedded within (nested) anonymous structures or | |
1344 | unions, the list steps down the chain to the component. */ | |
2f6e4e97 | 1345 | |
2f2d13da | 1346 | static tree |
2f6e4e97 | 1347 | lookup_field (tree decl, tree component) |
2f2d13da | 1348 | { |
e9b2c823 | 1349 | tree type = TREE_TYPE (decl); |
2f2d13da DE |
1350 | tree field; |
1351 | ||
1352 | /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers | |
1353 | to the field elements. Use a binary search on this array to quickly | |
1354 | find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC | |
1355 | will always be set for structures which have many elements. */ | |
1356 | ||
1357 | if (TYPE_LANG_SPECIFIC (type)) | |
1358 | { | |
1359 | int bot, top, half; | |
d07605f5 | 1360 | tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0]; |
2f2d13da DE |
1361 | |
1362 | field = TYPE_FIELDS (type); | |
1363 | bot = 0; | |
d07605f5 | 1364 | top = TYPE_LANG_SPECIFIC (type)->s->len; |
2f2d13da DE |
1365 | while (top - bot > 1) |
1366 | { | |
2f2d13da DE |
1367 | half = (top - bot + 1) >> 1; |
1368 | field = field_array[bot+half]; | |
1369 | ||
1370 | if (DECL_NAME (field) == NULL_TREE) | |
1371 | { | |
1372 | /* Step through all anon unions in linear fashion. */ | |
1373 | while (DECL_NAME (field_array[bot]) == NULL_TREE) | |
1374 | { | |
2f2d13da | 1375 | field = field_array[bot++]; |
a68b98cf RK |
1376 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE |
1377 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
19d76e60 | 1378 | { |
e9b2c823 NB |
1379 | tree anon = lookup_field (field, component); |
1380 | ||
1381 | if (anon) | |
1382 | return tree_cons (NULL_TREE, field, anon); | |
2f6e4e97 | 1383 | } |
2f2d13da DE |
1384 | } |
1385 | ||
1386 | /* Entire record is only anon unions. */ | |
1387 | if (bot > top) | |
1388 | return NULL_TREE; | |
1389 | ||
1390 | /* Restart the binary search, with new lower bound. */ | |
1391 | continue; | |
1392 | } | |
1393 | ||
e8b87aac | 1394 | if (DECL_NAME (field) == component) |
2f2d13da | 1395 | break; |
e8b87aac | 1396 | if (DECL_NAME (field) < component) |
2f2d13da DE |
1397 | bot += half; |
1398 | else | |
1399 | top = bot + half; | |
1400 | } | |
1401 | ||
1402 | if (DECL_NAME (field_array[bot]) == component) | |
1403 | field = field_array[bot]; | |
1404 | else if (DECL_NAME (field) != component) | |
e9b2c823 | 1405 | return NULL_TREE; |
2f2d13da DE |
1406 | } |
1407 | else | |
1408 | { | |
1409 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
1410 | { | |
e9b2c823 NB |
1411 | if (DECL_NAME (field) == NULL_TREE |
1412 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
1413 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)) | |
2f2d13da | 1414 | { |
e9b2c823 | 1415 | tree anon = lookup_field (field, component); |
a68b98cf | 1416 | |
e9b2c823 NB |
1417 | if (anon) |
1418 | return tree_cons (NULL_TREE, field, anon); | |
2f2d13da DE |
1419 | } |
1420 | ||
1421 | if (DECL_NAME (field) == component) | |
1422 | break; | |
1423 | } | |
e9b2c823 NB |
1424 | |
1425 | if (field == NULL_TREE) | |
1426 | return NULL_TREE; | |
2f2d13da DE |
1427 | } |
1428 | ||
e9b2c823 | 1429 | return tree_cons (NULL_TREE, field, NULL_TREE); |
2f2d13da DE |
1430 | } |
1431 | ||
400fbf9f JW |
1432 | /* Make an expression to refer to the COMPONENT field of |
1433 | structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */ | |
1434 | ||
1435 | tree | |
2f6e4e97 | 1436 | build_component_ref (tree datum, tree component) |
400fbf9f | 1437 | { |
b3694847 SS |
1438 | tree type = TREE_TYPE (datum); |
1439 | enum tree_code code = TREE_CODE (type); | |
1440 | tree field = NULL; | |
1441 | tree ref; | |
400fbf9f | 1442 | |
7a3ea201 RH |
1443 | if (!objc_is_public (datum, component)) |
1444 | return error_mark_node; | |
1445 | ||
207bf485 | 1446 | /* If DATUM is a COMPOUND_EXPR, move our reference inside it. |
53cd18ec | 1447 | Ensure that the arguments are not lvalues; otherwise, |
207bf485 JM |
1448 | if the component is an array, it would wrongly decay to a pointer in |
1449 | C89 mode. | |
1450 | We cannot do this with a COND_EXPR, because in a conditional expression | |
1451 | the default promotions are applied to both sides, and this would yield | |
1452 | the wrong type of the result; for example, if the components have | |
1453 | type "char". */ | |
400fbf9f JW |
1454 | switch (TREE_CODE (datum)) |
1455 | { | |
1456 | case COMPOUND_EXPR: | |
1457 | { | |
1458 | tree value = build_component_ref (TREE_OPERAND (datum, 1), component); | |
53fb4de3 RS |
1459 | return build2 (COMPOUND_EXPR, TREE_TYPE (value), |
1460 | TREE_OPERAND (datum, 0), non_lvalue (value)); | |
400fbf9f | 1461 | } |
e9a25f70 JL |
1462 | default: |
1463 | break; | |
400fbf9f JW |
1464 | } |
1465 | ||
1466 | /* See if there is a field or component with name COMPONENT. */ | |
1467 | ||
1468 | if (code == RECORD_TYPE || code == UNION_TYPE) | |
1469 | { | |
d0f062fb | 1470 | if (!COMPLETE_TYPE_P (type)) |
400fbf9f | 1471 | { |
7a228918 | 1472 | c_incomplete_type_error (NULL_TREE, type); |
400fbf9f JW |
1473 | return error_mark_node; |
1474 | } | |
1475 | ||
e9b2c823 | 1476 | field = lookup_field (datum, component); |
400fbf9f JW |
1477 | |
1478 | if (!field) | |
1479 | { | |
913d0833 KG |
1480 | error ("%s has no member named `%s'", |
1481 | code == RECORD_TYPE ? "structure" : "union", | |
400fbf9f JW |
1482 | IDENTIFIER_POINTER (component)); |
1483 | return error_mark_node; | |
1484 | } | |
400fbf9f | 1485 | |
e9b2c823 NB |
1486 | /* Chain the COMPONENT_REFs if necessary down to the FIELD. |
1487 | This might be better solved in future the way the C++ front | |
1488 | end does it - by giving the anonymous entities each a | |
1489 | separate name and type, and then have build_component_ref | |
1490 | recursively call itself. We can't do that here. */ | |
46ea50cb | 1491 | do |
19d76e60 | 1492 | { |
e9b2c823 NB |
1493 | tree subdatum = TREE_VALUE (field); |
1494 | ||
1495 | if (TREE_TYPE (subdatum) == error_mark_node) | |
1496 | return error_mark_node; | |
1497 | ||
53fb4de3 RS |
1498 | ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum, |
1499 | NULL_TREE); | |
e9b2c823 | 1500 | if (TREE_READONLY (datum) || TREE_READONLY (subdatum)) |
19d76e60 | 1501 | TREE_READONLY (ref) = 1; |
e9b2c823 | 1502 | if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum)) |
19d76e60 | 1503 | TREE_THIS_VOLATILE (ref) = 1; |
e23bd218 IR |
1504 | |
1505 | if (TREE_DEPRECATED (subdatum)) | |
1506 | warn_deprecated_use (subdatum); | |
1507 | ||
19d76e60 | 1508 | datum = ref; |
46ea50cb RS |
1509 | |
1510 | field = TREE_CHAIN (field); | |
19d76e60 | 1511 | } |
46ea50cb | 1512 | while (field); |
19d76e60 | 1513 | |
400fbf9f JW |
1514 | return ref; |
1515 | } | |
1516 | else if (code != ERROR_MARK) | |
1517 | error ("request for member `%s' in something not a structure or union", | |
1518 | IDENTIFIER_POINTER (component)); | |
1519 | ||
1520 | return error_mark_node; | |
1521 | } | |
1522 | \f | |
1523 | /* Given an expression PTR for a pointer, return an expression | |
1524 | for the value pointed to. | |
1525 | ERRORSTRING is the name of the operator to appear in error messages. */ | |
1526 | ||
1527 | tree | |
2f6e4e97 | 1528 | build_indirect_ref (tree ptr, const char *errorstring) |
400fbf9f | 1529 | { |
b3694847 SS |
1530 | tree pointer = default_conversion (ptr); |
1531 | tree type = TREE_TYPE (pointer); | |
400fbf9f JW |
1532 | |
1533 | if (TREE_CODE (type) == POINTER_TYPE) | |
870cc33b RS |
1534 | { |
1535 | if (TREE_CODE (pointer) == ADDR_EXPR | |
870cc33b RS |
1536 | && (TREE_TYPE (TREE_OPERAND (pointer, 0)) |
1537 | == TREE_TYPE (type))) | |
1538 | return TREE_OPERAND (pointer, 0); | |
1539 | else | |
1540 | { | |
1541 | tree t = TREE_TYPE (type); | |
b3694847 | 1542 | tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer); |
400fbf9f | 1543 | |
baae9b65 | 1544 | if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE) |
870cc33b RS |
1545 | { |
1546 | error ("dereferencing pointer to incomplete type"); | |
1547 | return error_mark_node; | |
1548 | } | |
baae9b65 | 1549 | if (VOID_TYPE_P (t) && skip_evaluation == 0) |
870cc33b RS |
1550 | warning ("dereferencing `void *' pointer"); |
1551 | ||
1552 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, | |
1553 | so that we get the proper error message if the result is used | |
1554 | to assign to. Also, &* is supposed to be a no-op. | |
1555 | And ANSI C seems to specify that the type of the result | |
1556 | should be the const type. */ | |
1557 | /* A de-reference of a pointer to const is not a const. It is valid | |
1558 | to change it via some other pointer. */ | |
1559 | TREE_READONLY (ref) = TYPE_READONLY (t); | |
1560 | TREE_SIDE_EFFECTS (ref) | |
271bd540 | 1561 | = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer); |
493692cd | 1562 | TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); |
870cc33b RS |
1563 | return ref; |
1564 | } | |
1565 | } | |
400fbf9f JW |
1566 | else if (TREE_CODE (pointer) != ERROR_MARK) |
1567 | error ("invalid type argument of `%s'", errorstring); | |
1568 | return error_mark_node; | |
1569 | } | |
1570 | ||
1571 | /* This handles expressions of the form "a[i]", which denotes | |
1572 | an array reference. | |
1573 | ||
1574 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
1575 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
1576 | This avoids forcing the array out of registers, and can work on | |
1577 | arrays that are not lvalues (for example, members of structures returned | |
1578 | by functions). */ | |
1579 | ||
1580 | tree | |
2f6e4e97 | 1581 | build_array_ref (tree array, tree index) |
400fbf9f JW |
1582 | { |
1583 | if (index == 0) | |
1584 | { | |
1585 | error ("subscript missing in array reference"); | |
1586 | return error_mark_node; | |
1587 | } | |
1588 | ||
1589 | if (TREE_TYPE (array) == error_mark_node | |
1590 | || TREE_TYPE (index) == error_mark_node) | |
1591 | return error_mark_node; | |
1592 | ||
6de9cd9a | 1593 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) |
400fbf9f JW |
1594 | { |
1595 | tree rval, type; | |
1596 | ||
400fbf9f JW |
1597 | /* Subscripting with type char is likely to lose |
1598 | on a machine where chars are signed. | |
1599 | So warn on any machine, but optionally. | |
1600 | Don't warn for unsigned char since that type is safe. | |
1601 | Don't warn for signed char because anyone who uses that | |
1602 | must have done so deliberately. */ | |
1603 | if (warn_char_subscripts | |
1604 | && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node) | |
1605 | warning ("array subscript has type `char'"); | |
1606 | ||
0e51ef9b RS |
1607 | /* Apply default promotions *after* noticing character types. */ |
1608 | index = default_conversion (index); | |
1609 | ||
fdeefd49 RS |
1610 | /* Require integer *after* promotion, for sake of enums. */ |
1611 | if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE) | |
1612 | { | |
1613 | error ("array subscript is not an integer"); | |
1614 | return error_mark_node; | |
1615 | } | |
1616 | ||
400fbf9f JW |
1617 | /* An array that is indexed by a non-constant |
1618 | cannot be stored in a register; we must be able to do | |
1619 | address arithmetic on its address. | |
1620 | Likewise an array of elements of variable size. */ | |
1621 | if (TREE_CODE (index) != INTEGER_CST | |
d0f062fb | 1622 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
400fbf9f JW |
1623 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) |
1624 | { | |
dffd7eb6 | 1625 | if (!c_mark_addressable (array)) |
400fbf9f JW |
1626 | return error_mark_node; |
1627 | } | |
e6d52559 JW |
1628 | /* An array that is indexed by a constant value which is not within |
1629 | the array bounds cannot be stored in a register either; because we | |
1630 | would get a crash in store_bit_field/extract_bit_field when trying | |
1631 | to access a non-existent part of the register. */ | |
1632 | if (TREE_CODE (index) == INTEGER_CST | |
eb34af89 RK |
1633 | && TYPE_DOMAIN (TREE_TYPE (array)) |
1634 | && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) | |
e6d52559 | 1635 | { |
dffd7eb6 | 1636 | if (!c_mark_addressable (array)) |
e6d52559 JW |
1637 | return error_mark_node; |
1638 | } | |
400fbf9f | 1639 | |
400fbf9f JW |
1640 | if (pedantic) |
1641 | { | |
1642 | tree foo = array; | |
1643 | while (TREE_CODE (foo) == COMPONENT_REF) | |
1644 | foo = TREE_OPERAND (foo, 0); | |
5baeaac0 | 1645 | if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo)) |
05273f08 GK |
1646 | pedwarn ("ISO C forbids subscripting `register' array"); |
1647 | else if (! flag_isoc99 && ! lvalue_p (foo)) | |
56508306 | 1648 | pedwarn ("ISO C90 forbids subscripting non-lvalue array"); |
400fbf9f JW |
1649 | } |
1650 | ||
1651 | type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array))); | |
53fb4de3 | 1652 | rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); |
400fbf9f JW |
1653 | /* Array ref is const/volatile if the array elements are |
1654 | or if the array is. */ | |
1655 | TREE_READONLY (rval) | |
1656 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
1657 | | TREE_READONLY (array)); | |
1658 | TREE_SIDE_EFFECTS (rval) | |
1659 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
1660 | | TREE_SIDE_EFFECTS (array)); | |
1661 | TREE_THIS_VOLATILE (rval) | |
1662 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
1663 | /* This was added by rms on 16 Nov 91. | |
2f6e4e97 | 1664 | It fixes vol struct foo *a; a->elts[1] |
400fbf9f JW |
1665 | in an inline function. |
1666 | Hope it doesn't break something else. */ | |
1667 | | TREE_THIS_VOLATILE (array)); | |
1668 | return require_complete_type (fold (rval)); | |
1669 | } | |
1670 | ||
1671 | { | |
1672 | tree ar = default_conversion (array); | |
1673 | tree ind = default_conversion (index); | |
1674 | ||
aed11452 RK |
1675 | /* Do the same warning check as above, but only on the part that's |
1676 | syntactically the index and only if it is also semantically | |
1677 | the index. */ | |
1678 | if (warn_char_subscripts | |
1679 | && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE | |
1680 | && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node) | |
1681 | warning ("subscript has type `char'"); | |
1682 | ||
400fbf9f JW |
1683 | /* Put the integer in IND to simplify error checking. */ |
1684 | if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) | |
1685 | { | |
1686 | tree temp = ar; | |
1687 | ar = ind; | |
1688 | ind = temp; | |
1689 | } | |
1690 | ||
1691 | if (ar == error_mark_node) | |
1692 | return ar; | |
1693 | ||
004252d7 RK |
1694 | if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE |
1695 | || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE) | |
400fbf9f JW |
1696 | { |
1697 | error ("subscripted value is neither array nor pointer"); | |
1698 | return error_mark_node; | |
1699 | } | |
1700 | if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) | |
1701 | { | |
1702 | error ("array subscript is not an integer"); | |
1703 | return error_mark_node; | |
1704 | } | |
1705 | ||
1706 | return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0), | |
1707 | "array indexing"); | |
1708 | } | |
1709 | } | |
1710 | \f | |
7e585d16 ZW |
1711 | /* Build an external reference to identifier ID. FUN indicates |
1712 | whether this will be used for a function call. */ | |
1713 | tree | |
2f6e4e97 | 1714 | build_external_ref (tree id, int fun) |
7e585d16 ZW |
1715 | { |
1716 | tree ref; | |
1717 | tree decl = lookup_name (id); | |
1718 | tree objc_ivar = lookup_objc_ivar (id); | |
1719 | ||
339a28b9 | 1720 | if (decl && decl != error_mark_node) |
7e585d16 ZW |
1721 | { |
1722 | /* Properly declared variable or function reference. */ | |
1723 | if (!objc_ivar) | |
1724 | ref = decl; | |
4b1e44be | 1725 | else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl)) |
7e585d16 ZW |
1726 | { |
1727 | warning ("local declaration of `%s' hides instance variable", | |
1728 | IDENTIFIER_POINTER (id)); | |
1729 | ref = decl; | |
1730 | } | |
1731 | else | |
1732 | ref = objc_ivar; | |
1733 | } | |
339a28b9 ZW |
1734 | else if (objc_ivar) |
1735 | ref = objc_ivar; | |
1736 | else if (fun) | |
1737 | /* Implicit function declaration. */ | |
1738 | ref = implicitly_declare (id); | |
1739 | else if (decl == error_mark_node) | |
1740 | /* Don't complain about something that's already been | |
1741 | complained about. */ | |
1742 | return error_mark_node; | |
1743 | else | |
1744 | { | |
1745 | undeclared_variable (id); | |
1746 | return error_mark_node; | |
1747 | } | |
7e585d16 ZW |
1748 | |
1749 | if (TREE_TYPE (ref) == error_mark_node) | |
1750 | return error_mark_node; | |
1751 | ||
339a28b9 ZW |
1752 | if (TREE_DEPRECATED (ref)) |
1753 | warn_deprecated_use (ref); | |
1754 | ||
25587e40 AO |
1755 | if (!skip_evaluation) |
1756 | assemble_external (ref); | |
7e585d16 ZW |
1757 | TREE_USED (ref) = 1; |
1758 | ||
1759 | if (TREE_CODE (ref) == CONST_DECL) | |
1760 | { | |
1761 | ref = DECL_INITIAL (ref); | |
1762 | TREE_CONSTANT (ref) = 1; | |
6de9cd9a | 1763 | TREE_INVARIANT (ref) = 1; |
7e585d16 | 1764 | } |
6a29edea | 1765 | else if (current_function_decl != 0 |
4b1e44be | 1766 | && !DECL_FILE_SCOPE_P (current_function_decl) |
6a29edea EB |
1767 | && (TREE_CODE (ref) == VAR_DECL |
1768 | || TREE_CODE (ref) == PARM_DECL | |
1769 | || TREE_CODE (ref) == FUNCTION_DECL)) | |
1770 | { | |
1771 | tree context = decl_function_context (ref); | |
2f6e4e97 | 1772 | |
6a29edea EB |
1773 | if (context != 0 && context != current_function_decl) |
1774 | DECL_NONLOCAL (ref) = 1; | |
1775 | } | |
7e585d16 ZW |
1776 | |
1777 | return ref; | |
1778 | } | |
1779 | ||
400fbf9f JW |
1780 | /* Build a function call to function FUNCTION with parameters PARAMS. |
1781 | PARAMS is a list--a chain of TREE_LIST nodes--in which the | |
1782 | TREE_VALUE of each node is a parameter-expression. | |
1783 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
1784 | ||
1785 | tree | |
2f6e4e97 | 1786 | build_function_call (tree function, tree params) |
400fbf9f | 1787 | { |
b3694847 SS |
1788 | tree fntype, fundecl = 0; |
1789 | tree coerced_params; | |
4977bab6 | 1790 | tree name = NULL_TREE, result; |
c96f4f73 | 1791 | tree tem; |
400fbf9f | 1792 | |
fc76e425 | 1793 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce | 1794 | STRIP_TYPE_NOPS (function); |
400fbf9f JW |
1795 | |
1796 | /* Convert anything with function type to a pointer-to-function. */ | |
1797 | if (TREE_CODE (function) == FUNCTION_DECL) | |
1798 | { | |
1799 | name = DECL_NAME (function); | |
19d76e60 | 1800 | |
400fbf9f JW |
1801 | /* Differs from default_conversion by not setting TREE_ADDRESSABLE |
1802 | (because calling an inline function does not mean the function | |
1803 | needs to be separately compiled). */ | |
1804 | fntype = build_type_variant (TREE_TYPE (function), | |
1805 | TREE_READONLY (function), | |
1806 | TREE_THIS_VOLATILE (function)); | |
9b7267b8 | 1807 | fundecl = function; |
400fbf9f JW |
1808 | function = build1 (ADDR_EXPR, build_pointer_type (fntype), function); |
1809 | } | |
1810 | else | |
1811 | function = default_conversion (function); | |
1812 | ||
1813 | fntype = TREE_TYPE (function); | |
1814 | ||
1815 | if (TREE_CODE (fntype) == ERROR_MARK) | |
1816 | return error_mark_node; | |
1817 | ||
1818 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
1819 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
1820 | { | |
1821 | error ("called object is not a function"); | |
1822 | return error_mark_node; | |
1823 | } | |
1824 | ||
5ce89b2e JM |
1825 | if (fundecl && TREE_THIS_VOLATILE (fundecl)) |
1826 | current_function_returns_abnormally = 1; | |
1827 | ||
400fbf9f JW |
1828 | /* fntype now gets the type of function pointed to. */ |
1829 | fntype = TREE_TYPE (fntype); | |
1830 | ||
c96f4f73 EB |
1831 | /* Check that the function is called through a compatible prototype. |
1832 | If it is not, replace the call by a trap, wrapped up in a compound | |
1833 | expression if necessary. This has the nice side-effect to prevent | |
1834 | the tree-inliner from generating invalid assignment trees which may | |
1835 | blow up in the RTL expander later. | |
1836 | ||
1837 | ??? This doesn't work for Objective-C because objc_comptypes | |
1838 | refuses to compare function prototypes, yet the compiler appears | |
1839 | to build calls that are flagged as invalid by C's comptypes. */ | |
1840 | if (! c_dialect_objc () | |
1841 | && TREE_CODE (function) == NOP_EXPR | |
1842 | && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR | |
1843 | && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL | |
132da1a5 | 1844 | && ! comptypes (fntype, TREE_TYPE (tem))) |
c96f4f73 EB |
1845 | { |
1846 | tree return_type = TREE_TYPE (fntype); | |
1847 | tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP], | |
1848 | NULL_TREE); | |
1849 | ||
1850 | /* This situation leads to run-time undefined behavior. We can't, | |
1851 | therefore, simply error unless we can prove that all possible | |
1852 | executions of the program must execute the code. */ | |
1853 | warning ("function called through a non-compatible type"); | |
1854 | ||
bba745c1 EB |
1855 | /* We can, however, treat "undefined" any way we please. |
1856 | Call abort to encourage the user to fix the program. */ | |
1857 | inform ("if this code is reached, the program will abort"); | |
1858 | ||
c96f4f73 EB |
1859 | if (VOID_TYPE_P (return_type)) |
1860 | return trap; | |
1861 | else | |
1862 | { | |
1863 | tree rhs; | |
1864 | ||
1865 | if (AGGREGATE_TYPE_P (return_type)) | |
1866 | rhs = build_compound_literal (return_type, | |
1867 | build_constructor (return_type, | |
1868 | NULL_TREE)); | |
1869 | else | |
1870 | rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node)); | |
1871 | ||
53fb4de3 | 1872 | return build2 (COMPOUND_EXPR, return_type, trap, rhs); |
c96f4f73 EB |
1873 | } |
1874 | } | |
1875 | ||
400fbf9f JW |
1876 | /* Convert the parameters to the types declared in the |
1877 | function prototype, or apply default promotions. */ | |
1878 | ||
1879 | coerced_params | |
9b7267b8 | 1880 | = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl); |
400fbf9f | 1881 | |
b34c7881 | 1882 | /* Check that the arguments to the function are valid. */ |
400fbf9f | 1883 | |
b34c7881 | 1884 | check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params); |
400fbf9f | 1885 | |
53fb4de3 RS |
1886 | result = build3 (CALL_EXPR, TREE_TYPE (fntype), |
1887 | function, coerced_params, NULL_TREE); | |
1eb8759b | 1888 | TREE_SIDE_EFFECTS (result) = 1; |
bf730f15 RS |
1889 | |
1890 | if (require_constant_value) | |
1891 | { | |
1892 | result = fold_initializer (result); | |
1893 | ||
1894 | if (TREE_CONSTANT (result) | |
1895 | && (name == NULL_TREE | |
1896 | || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0)) | |
1897 | pedwarn_init ("initializer element is not constant"); | |
1898 | } | |
1899 | else | |
1900 | result = fold (result); | |
b0b3afb2 | 1901 | |
71653180 | 1902 | if (VOID_TYPE_P (TREE_TYPE (result))) |
1eb8759b RH |
1903 | return result; |
1904 | return require_complete_type (result); | |
400fbf9f JW |
1905 | } |
1906 | \f | |
1907 | /* Convert the argument expressions in the list VALUES | |
1908 | to the types in the list TYPELIST. The result is a list of converted | |
1909 | argument expressions. | |
1910 | ||
1911 | If TYPELIST is exhausted, or when an element has NULL as its type, | |
1912 | perform the default conversions. | |
1913 | ||
1914 | PARMLIST is the chain of parm decls for the function being called. | |
1915 | It may be 0, if that info is not available. | |
1916 | It is used only for generating error messages. | |
1917 | ||
1918 | NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. | |
1919 | ||
1920 | This is also where warnings about wrong number of args are generated. | |
1921 | ||
1922 | Both VALUES and the returned value are chains of TREE_LIST nodes | |
1923 | with the elements of the list in the TREE_VALUE slots of those nodes. */ | |
1924 | ||
1925 | static tree | |
2f6e4e97 | 1926 | convert_arguments (tree typelist, tree values, tree name, tree fundecl) |
400fbf9f | 1927 | { |
b3694847 SS |
1928 | tree typetail, valtail; |
1929 | tree result = NULL; | |
400fbf9f JW |
1930 | int parmnum; |
1931 | ||
1932 | /* Scan the given expressions and types, producing individual | |
1933 | converted arguments and pushing them on RESULT in reverse order. */ | |
1934 | ||
1935 | for (valtail = values, typetail = typelist, parmnum = 0; | |
1936 | valtail; | |
1937 | valtail = TREE_CHAIN (valtail), parmnum++) | |
1938 | { | |
b3694847 SS |
1939 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
1940 | tree val = TREE_VALUE (valtail); | |
400fbf9f JW |
1941 | |
1942 | if (type == void_type_node) | |
1943 | { | |
1944 | if (name) | |
1945 | error ("too many arguments to function `%s'", | |
1946 | IDENTIFIER_POINTER (name)); | |
1947 | else | |
1948 | error ("too many arguments to function"); | |
1949 | break; | |
1950 | } | |
1951 | ||
1952 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
fc76e425 RS |
1953 | /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0 |
1954 | to convert automatically to a pointer. */ | |
400fbf9f JW |
1955 | if (TREE_CODE (val) == NON_LVALUE_EXPR) |
1956 | val = TREE_OPERAND (val, 0); | |
1957 | ||
207bf485 | 1958 | val = default_function_array_conversion (val); |
400fbf9f JW |
1959 | |
1960 | val = require_complete_type (val); | |
1961 | ||
1962 | if (type != 0) | |
1963 | { | |
1964 | /* Formal parm type is specified by a function prototype. */ | |
1965 | tree parmval; | |
1966 | ||
d0f062fb | 1967 | if (!COMPLETE_TYPE_P (type)) |
400fbf9f JW |
1968 | { |
1969 | error ("type of formal parameter %d is incomplete", parmnum + 1); | |
1970 | parmval = val; | |
1971 | } | |
1972 | else | |
1973 | { | |
d45cf215 RS |
1974 | /* Optionally warn about conversions that |
1975 | differ from the default conversions. */ | |
03829ad2 | 1976 | if (warn_conversion || warn_traditional) |
400fbf9f | 1977 | { |
e3a64162 | 1978 | unsigned int formal_prec = TYPE_PRECISION (type); |
400fbf9f | 1979 | |
aae43c5f | 1980 | if (INTEGRAL_TYPE_P (type) |
400fbf9f | 1981 | && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) |
754a4d82 | 1982 | warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1); |
03829ad2 KG |
1983 | if (INTEGRAL_TYPE_P (type) |
1984 | && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE) | |
1985 | warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1); | |
aae43c5f RK |
1986 | else if (TREE_CODE (type) == COMPLEX_TYPE |
1987 | && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) | |
1988 | warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1); | |
400fbf9f | 1989 | else if (TREE_CODE (type) == REAL_TYPE |
aae43c5f | 1990 | && INTEGRAL_TYPE_P (TREE_TYPE (val))) |
754a4d82 | 1991 | warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1); |
03829ad2 KG |
1992 | else if (TREE_CODE (type) == COMPLEX_TYPE |
1993 | && INTEGRAL_TYPE_P (TREE_TYPE (val))) | |
1994 | warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1); | |
aae43c5f RK |
1995 | else if (TREE_CODE (type) == REAL_TYPE |
1996 | && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE) | |
1997 | warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1); | |
1998 | /* ??? At some point, messages should be written about | |
1999 | conversions between complex types, but that's too messy | |
2000 | to do now. */ | |
d45cf215 RS |
2001 | else if (TREE_CODE (type) == REAL_TYPE |
2002 | && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) | |
2003 | { | |
2004 | /* Warn if any argument is passed as `float', | |
047de90b | 2005 | since without a prototype it would be `double'. */ |
d45cf215 | 2006 | if (formal_prec == TYPE_PRECISION (float_type_node)) |
754a4d82 | 2007 | warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1); |
d45cf215 | 2008 | } |
3ed56f8a KG |
2009 | /* Detect integer changing in width or signedness. |
2010 | These warnings are only activated with | |
2011 | -Wconversion, not with -Wtraditional. */ | |
2012 | else if (warn_conversion && INTEGRAL_TYPE_P (type) | |
aae43c5f | 2013 | && INTEGRAL_TYPE_P (TREE_TYPE (val))) |
400fbf9f | 2014 | { |
d45cf215 RS |
2015 | tree would_have_been = default_conversion (val); |
2016 | tree type1 = TREE_TYPE (would_have_been); | |
2017 | ||
754a4d82 | 2018 | if (TREE_CODE (type) == ENUMERAL_TYPE |
a38b987a NB |
2019 | && (TYPE_MAIN_VARIANT (type) |
2020 | == TYPE_MAIN_VARIANT (TREE_TYPE (val)))) | |
754a4d82 RS |
2021 | /* No warning if function asks for enum |
2022 | and the actual arg is that enum type. */ | |
2023 | ; | |
2024 | else if (formal_prec != TYPE_PRECISION (type1)) | |
2025 | warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1); | |
8df83eae | 2026 | else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) |
d45cf215 | 2027 | ; |
800cd3b9 RS |
2028 | /* Don't complain if the formal parameter type |
2029 | is an enum, because we can't tell now whether | |
2030 | the value was an enum--even the same enum. */ | |
2031 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
2032 | ; | |
400fbf9f JW |
2033 | else if (TREE_CODE (val) == INTEGER_CST |
2034 | && int_fits_type_p (val, type)) | |
2035 | /* Change in signedness doesn't matter | |
2036 | if a constant value is unaffected. */ | |
2037 | ; | |
4bbbc5d9 RS |
2038 | /* Likewise for a constant in a NOP_EXPR. */ |
2039 | else if (TREE_CODE (val) == NOP_EXPR | |
2040 | && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST | |
2041 | && int_fits_type_p (TREE_OPERAND (val, 0), type)) | |
2042 | ; | |
ce9895ae RS |
2043 | /* If the value is extended from a narrower |
2044 | unsigned type, it doesn't matter whether we | |
2045 | pass it as signed or unsigned; the value | |
2046 | certainly is the same either way. */ | |
2047 | else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type) | |
8df83eae | 2048 | && TYPE_UNSIGNED (TREE_TYPE (val))) |
ce9895ae | 2049 | ; |
8df83eae | 2050 | else if (TYPE_UNSIGNED (type)) |
3ed56f8a KG |
2051 | warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1); |
2052 | else | |
2053 | warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1); | |
400fbf9f JW |
2054 | } |
2055 | } | |
2056 | ||
2f6e4e97 | 2057 | parmval = convert_for_assignment (type, val, |
0f41302f | 2058 | (char *) 0, /* arg passing */ |
9b7267b8 | 2059 | fundecl, name, parmnum + 1); |
2f6e4e97 | 2060 | |
61f71b34 | 2061 | if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) |
b6d6aa84 | 2062 | && INTEGRAL_TYPE_P (type) |
400fbf9f JW |
2063 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
2064 | parmval = default_conversion (parmval); | |
400fbf9f | 2065 | } |
8d9bfdc5 | 2066 | result = tree_cons (NULL_TREE, parmval, result); |
400fbf9f JW |
2067 | } |
2068 | else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE | |
2069 | && (TYPE_PRECISION (TREE_TYPE (val)) | |
2070 | < TYPE_PRECISION (double_type_node))) | |
2071 | /* Convert `float' to `double'. */ | |
2072 | result = tree_cons (NULL_TREE, convert (double_type_node, val), result); | |
2073 | else | |
2074 | /* Convert `short' and `char' to full-size `int'. */ | |
2075 | result = tree_cons (NULL_TREE, default_conversion (val), result); | |
2076 | ||
2077 | if (typetail) | |
2078 | typetail = TREE_CHAIN (typetail); | |
2079 | } | |
2080 | ||
2081 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) | |
2082 | { | |
2083 | if (name) | |
2084 | error ("too few arguments to function `%s'", | |
2085 | IDENTIFIER_POINTER (name)); | |
2086 | else | |
2087 | error ("too few arguments to function"); | |
2088 | } | |
2089 | ||
2090 | return nreverse (result); | |
2091 | } | |
2092 | \f | |
2093 | /* This is the entry point used by the parser | |
2094 | for binary operators in the input. | |
2095 | In addition to constructing the expression, | |
2096 | we check for operands that were written with other binary operators | |
2097 | in a way that is likely to confuse the user. */ | |
edc7c4ec | 2098 | |
487a92fe JM |
2099 | struct c_expr |
2100 | parser_build_binary_op (enum tree_code code, struct c_expr arg1, | |
2101 | struct c_expr arg2) | |
400fbf9f | 2102 | { |
487a92fe | 2103 | struct c_expr result; |
400fbf9f | 2104 | |
487a92fe JM |
2105 | enum tree_code code1 = arg1.original_code; |
2106 | enum tree_code code2 = arg2.original_code; | |
400fbf9f | 2107 | |
487a92fe JM |
2108 | result.value = build_binary_op (code, arg1.value, arg2.value, 1); |
2109 | result.original_code = code; | |
58bf601b | 2110 | |
487a92fe JM |
2111 | if (TREE_CODE (result.value) == ERROR_MARK) |
2112 | return result; | |
400fbf9f JW |
2113 | |
2114 | /* Check for cases such as x+y<<z which users are likely | |
487a92fe | 2115 | to misinterpret. */ |
400fbf9f JW |
2116 | if (warn_parentheses) |
2117 | { | |
2118 | if (code == LSHIFT_EXPR || code == RSHIFT_EXPR) | |
2119 | { | |
2120 | if (code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
2121 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
2122 | warning ("suggest parentheses around + or - inside shift"); | |
2123 | } | |
2124 | ||
2125 | if (code == TRUTH_ORIF_EXPR) | |
2126 | { | |
2127 | if (code1 == TRUTH_ANDIF_EXPR | |
2128 | || code2 == TRUTH_ANDIF_EXPR) | |
2129 | warning ("suggest parentheses around && within ||"); | |
2130 | } | |
2131 | ||
2132 | if (code == BIT_IOR_EXPR) | |
2133 | { | |
2134 | if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR | |
2135 | || code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
2136 | || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR | |
2137 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
2138 | warning ("suggest parentheses around arithmetic in operand of |"); | |
7e9d002a RK |
2139 | /* Check cases like x|y==z */ |
2140 | if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') | |
2141 | warning ("suggest parentheses around comparison in operand of |"); | |
400fbf9f JW |
2142 | } |
2143 | ||
2144 | if (code == BIT_XOR_EXPR) | |
2145 | { | |
2146 | if (code1 == BIT_AND_EXPR | |
2147 | || code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
2148 | || code2 == BIT_AND_EXPR | |
2149 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
2150 | warning ("suggest parentheses around arithmetic in operand of ^"); | |
7e9d002a RK |
2151 | /* Check cases like x^y==z */ |
2152 | if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') | |
2153 | warning ("suggest parentheses around comparison in operand of ^"); | |
400fbf9f JW |
2154 | } |
2155 | ||
2156 | if (code == BIT_AND_EXPR) | |
2157 | { | |
2158 | if (code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
2159 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
2160 | warning ("suggest parentheses around + or - in operand of &"); | |
7e9d002a RK |
2161 | /* Check cases like x&y==z */ |
2162 | if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') | |
2163 | warning ("suggest parentheses around comparison in operand of &"); | |
400fbf9f | 2164 | } |
3e3970a2 JM |
2165 | /* Similarly, check for cases like 1<=i<=10 that are probably errors. */ |
2166 | if (TREE_CODE_CLASS (code) == '<' | |
2167 | && (TREE_CODE_CLASS (code1) == '<' | |
2168 | || TREE_CODE_CLASS (code2) == '<')) | |
2169 | warning ("comparisons like X<=Y<=Z do not have their mathematical meaning"); | |
400fbf9f | 2170 | |
3e3970a2 | 2171 | } |
001af587 | 2172 | |
487a92fe JM |
2173 | unsigned_conversion_warning (result.value, arg1.value); |
2174 | unsigned_conversion_warning (result.value, arg2.value); | |
2175 | overflow_warning (result.value); | |
400fbf9f JW |
2176 | |
2177 | return result; | |
2178 | } | |
3e4093b6 | 2179 | \f |
3e4093b6 RS |
2180 | /* Return a tree for the difference of pointers OP0 and OP1. |
2181 | The resulting tree has type int. */ | |
293c9fdd | 2182 | |
3e4093b6 RS |
2183 | static tree |
2184 | pointer_diff (tree op0, tree op1) | |
2185 | { | |
3e4093b6 | 2186 | tree restype = ptrdiff_type_node; |
400fbf9f | 2187 | |
3e4093b6 RS |
2188 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); |
2189 | tree con0, con1, lit0, lit1; | |
2190 | tree orig_op1 = op1; | |
400fbf9f | 2191 | |
3e4093b6 RS |
2192 | if (pedantic || warn_pointer_arith) |
2193 | { | |
2194 | if (TREE_CODE (target_type) == VOID_TYPE) | |
2195 | pedwarn ("pointer of type `void *' used in subtraction"); | |
2196 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
2197 | pedwarn ("pointer to a function used in subtraction"); | |
2198 | } | |
400fbf9f | 2199 | |
3e4093b6 RS |
2200 | /* If the conversion to ptrdiff_type does anything like widening or |
2201 | converting a partial to an integral mode, we get a convert_expression | |
2202 | that is in the way to do any simplifications. | |
2203 | (fold-const.c doesn't know that the extra bits won't be needed. | |
2204 | split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a | |
2205 | different mode in place.) | |
2206 | So first try to find a common term here 'by hand'; we want to cover | |
2207 | at least the cases that occur in legal static initializers. */ | |
2208 | con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0; | |
2209 | con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1; | |
400fbf9f | 2210 | |
3e4093b6 RS |
2211 | if (TREE_CODE (con0) == PLUS_EXPR) |
2212 | { | |
2213 | lit0 = TREE_OPERAND (con0, 1); | |
2214 | con0 = TREE_OPERAND (con0, 0); | |
2215 | } | |
2216 | else | |
2217 | lit0 = integer_zero_node; | |
400fbf9f | 2218 | |
3e4093b6 | 2219 | if (TREE_CODE (con1) == PLUS_EXPR) |
400fbf9f | 2220 | { |
3e4093b6 RS |
2221 | lit1 = TREE_OPERAND (con1, 1); |
2222 | con1 = TREE_OPERAND (con1, 0); | |
400fbf9f JW |
2223 | } |
2224 | else | |
3e4093b6 RS |
2225 | lit1 = integer_zero_node; |
2226 | ||
2227 | if (operand_equal_p (con0, con1, 0)) | |
400fbf9f | 2228 | { |
3e4093b6 RS |
2229 | op0 = lit0; |
2230 | op1 = lit1; | |
400fbf9f JW |
2231 | } |
2232 | ||
400fbf9f | 2233 | |
3e4093b6 RS |
2234 | /* First do the subtraction as integers; |
2235 | then drop through to build the divide operator. | |
2236 | Do not do default conversions on the minus operator | |
2237 | in case restype is a short type. */ | |
400fbf9f | 2238 | |
3e4093b6 RS |
2239 | op0 = build_binary_op (MINUS_EXPR, convert (restype, op0), |
2240 | convert (restype, op1), 0); | |
2241 | /* This generates an error if op1 is pointer to incomplete type. */ | |
2242 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) | |
2243 | error ("arithmetic on pointer to an incomplete type"); | |
400fbf9f | 2244 | |
3e4093b6 RS |
2245 | /* This generates an error if op0 is pointer to incomplete type. */ |
2246 | op1 = c_size_in_bytes (target_type); | |
400fbf9f | 2247 | |
3e4093b6 | 2248 | /* Divide by the size, in easiest possible way. */ |
53fb4de3 | 2249 | return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1))); |
3e4093b6 RS |
2250 | } |
2251 | \f | |
2252 | /* Construct and perhaps optimize a tree representation | |
2253 | for a unary operation. CODE, a tree_code, specifies the operation | |
2254 | and XARG is the operand. | |
2255 | For any CODE other than ADDR_EXPR, FLAG nonzero suppresses | |
2256 | the default promotions (such as from short to int). | |
2257 | For ADDR_EXPR, the default promotions are not applied; FLAG nonzero | |
2258 | allows non-lvalues; this is only used to handle conversion of non-lvalue | |
2259 | arrays to pointers in C99. */ | |
400fbf9f | 2260 | |
3e4093b6 RS |
2261 | tree |
2262 | build_unary_op (enum tree_code code, tree xarg, int flag) | |
2263 | { | |
2264 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
2265 | tree arg = xarg; | |
2266 | tree argtype = 0; | |
2267 | enum tree_code typecode = TREE_CODE (TREE_TYPE (arg)); | |
2268 | tree val; | |
2269 | int noconvert = flag; | |
400fbf9f | 2270 | |
3e4093b6 RS |
2271 | if (typecode == ERROR_MARK) |
2272 | return error_mark_node; | |
2273 | if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) | |
2274 | typecode = INTEGER_TYPE; | |
6c36d76b | 2275 | |
3e4093b6 RS |
2276 | switch (code) |
2277 | { | |
2278 | case CONVERT_EXPR: | |
2279 | /* This is used for unary plus, because a CONVERT_EXPR | |
2280 | is enough to prevent anybody from looking inside for | |
2281 | associativity, but won't generate any code. */ | |
2282 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
8a2cee38 JB |
2283 | || typecode == COMPLEX_TYPE |
2284 | || typecode == VECTOR_TYPE)) | |
400fbf9f | 2285 | { |
3e4093b6 RS |
2286 | error ("wrong type argument to unary plus"); |
2287 | return error_mark_node; | |
400fbf9f | 2288 | } |
3e4093b6 RS |
2289 | else if (!noconvert) |
2290 | arg = default_conversion (arg); | |
2291 | arg = non_lvalue (arg); | |
400fbf9f JW |
2292 | break; |
2293 | ||
3e4093b6 RS |
2294 | case NEGATE_EXPR: |
2295 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
2296 | || typecode == COMPLEX_TYPE | |
2297 | || typecode == VECTOR_TYPE)) | |
2298 | { | |
2299 | error ("wrong type argument to unary minus"); | |
2300 | return error_mark_node; | |
2301 | } | |
2302 | else if (!noconvert) | |
2303 | arg = default_conversion (arg); | |
400fbf9f JW |
2304 | break; |
2305 | ||
3e4093b6 RS |
2306 | case BIT_NOT_EXPR: |
2307 | if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE) | |
03d5b1f5 | 2308 | { |
3e4093b6 RS |
2309 | if (!noconvert) |
2310 | arg = default_conversion (arg); | |
03d5b1f5 | 2311 | } |
3e4093b6 | 2312 | else if (typecode == COMPLEX_TYPE) |
400fbf9f | 2313 | { |
3e4093b6 RS |
2314 | code = CONJ_EXPR; |
2315 | if (pedantic) | |
2316 | pedwarn ("ISO C does not support `~' for complex conjugation"); | |
2317 | if (!noconvert) | |
2318 | arg = default_conversion (arg); | |
2319 | } | |
2320 | else | |
2321 | { | |
2322 | error ("wrong type argument to bit-complement"); | |
2323 | return error_mark_node; | |
400fbf9f JW |
2324 | } |
2325 | break; | |
2326 | ||
3e4093b6 | 2327 | case ABS_EXPR: |
11017cc7 | 2328 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) |
400fbf9f | 2329 | { |
3e4093b6 RS |
2330 | error ("wrong type argument to abs"); |
2331 | return error_mark_node; | |
400fbf9f | 2332 | } |
3e4093b6 RS |
2333 | else if (!noconvert) |
2334 | arg = default_conversion (arg); | |
400fbf9f JW |
2335 | break; |
2336 | ||
3e4093b6 RS |
2337 | case CONJ_EXPR: |
2338 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
2339 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
2340 | || typecode == COMPLEX_TYPE)) | |
400fbf9f | 2341 | { |
3e4093b6 RS |
2342 | error ("wrong type argument to conjugation"); |
2343 | return error_mark_node; | |
400fbf9f | 2344 | } |
3e4093b6 RS |
2345 | else if (!noconvert) |
2346 | arg = default_conversion (arg); | |
400fbf9f JW |
2347 | break; |
2348 | ||
3e4093b6 RS |
2349 | case TRUTH_NOT_EXPR: |
2350 | if (typecode != INTEGER_TYPE | |
2351 | && typecode != REAL_TYPE && typecode != POINTER_TYPE | |
2352 | && typecode != COMPLEX_TYPE | |
2353 | /* These will convert to a pointer. */ | |
2354 | && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE) | |
400fbf9f | 2355 | { |
3e4093b6 RS |
2356 | error ("wrong type argument to unary exclamation mark"); |
2357 | return error_mark_node; | |
400fbf9f | 2358 | } |
ae2bcd98 | 2359 | arg = lang_hooks.truthvalue_conversion (arg); |
3e4093b6 RS |
2360 | return invert_truthvalue (arg); |
2361 | ||
2362 | case NOP_EXPR: | |
400fbf9f JW |
2363 | break; |
2364 | ||
3e4093b6 RS |
2365 | case REALPART_EXPR: |
2366 | if (TREE_CODE (arg) == COMPLEX_CST) | |
2367 | return TREE_REALPART (arg); | |
2368 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
2369 | return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); | |
2370 | else | |
2371 | return arg; | |
605a99f6 | 2372 | |
3e4093b6 RS |
2373 | case IMAGPART_EXPR: |
2374 | if (TREE_CODE (arg) == COMPLEX_CST) | |
2375 | return TREE_IMAGPART (arg); | |
2376 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
2377 | return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); | |
2378 | else | |
2379 | return convert (TREE_TYPE (arg), integer_zero_node); | |
2380 | ||
2381 | case PREINCREMENT_EXPR: | |
2382 | case POSTINCREMENT_EXPR: | |
2383 | case PREDECREMENT_EXPR: | |
2384 | case POSTDECREMENT_EXPR: | |
3e4093b6 RS |
2385 | |
2386 | /* Increment or decrement the real part of the value, | |
2387 | and don't change the imaginary part. */ | |
2388 | if (typecode == COMPLEX_TYPE) | |
400fbf9f | 2389 | { |
3e4093b6 RS |
2390 | tree real, imag; |
2391 | ||
2392 | if (pedantic) | |
2393 | pedwarn ("ISO C does not support `++' and `--' on complex types"); | |
2394 | ||
2395 | arg = stabilize_reference (arg); | |
2396 | real = build_unary_op (REALPART_EXPR, arg, 1); | |
2397 | imag = build_unary_op (IMAGPART_EXPR, arg, 1); | |
53fb4de3 RS |
2398 | return build2 (COMPLEX_EXPR, TREE_TYPE (arg), |
2399 | build_unary_op (code, real, 1), imag); | |
400fbf9f | 2400 | } |
3e4093b6 RS |
2401 | |
2402 | /* Report invalid types. */ | |
2403 | ||
2404 | if (typecode != POINTER_TYPE | |
2405 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE) | |
400fbf9f | 2406 | { |
3e4093b6 RS |
2407 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
2408 | error ("wrong type argument to increment"); | |
2409 | else | |
2410 | error ("wrong type argument to decrement"); | |
2411 | ||
2412 | return error_mark_node; | |
400fbf9f | 2413 | } |
400fbf9f | 2414 | |
3e4093b6 RS |
2415 | { |
2416 | tree inc; | |
2417 | tree result_type = TREE_TYPE (arg); | |
400fbf9f | 2418 | |
3e4093b6 RS |
2419 | arg = get_unwidened (arg, 0); |
2420 | argtype = TREE_TYPE (arg); | |
2421 | ||
2422 | /* Compute the increment. */ | |
2423 | ||
2424 | if (typecode == POINTER_TYPE) | |
2425 | { | |
2426 | /* If pointer target is an undefined struct, | |
2427 | we just cannot know how to do the arithmetic. */ | |
2428 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type))) | |
2429 | { | |
2430 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
2431 | error ("increment of pointer to unknown structure"); | |
2432 | else | |
2433 | error ("decrement of pointer to unknown structure"); | |
2434 | } | |
2435 | else if ((pedantic || warn_pointer_arith) | |
2436 | && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE | |
2437 | || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)) | |
2438 | { | |
2439 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
2440 | pedwarn ("wrong type argument to increment"); | |
2441 | else | |
2442 | pedwarn ("wrong type argument to decrement"); | |
2443 | } | |
2444 | ||
2445 | inc = c_size_in_bytes (TREE_TYPE (result_type)); | |
2446 | } | |
2447 | else | |
2448 | inc = integer_one_node; | |
2449 | ||
2450 | inc = convert (argtype, inc); | |
2451 | ||
3e4093b6 RS |
2452 | /* Complain about anything else that is not a true lvalue. */ |
2453 | if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR | |
2454 | || code == POSTINCREMENT_EXPR) | |
2455 | ? "invalid lvalue in increment" | |
2456 | : "invalid lvalue in decrement"))) | |
2457 | return error_mark_node; | |
2458 | ||
2459 | /* Report a read-only lvalue. */ | |
2460 | if (TREE_READONLY (arg)) | |
c5b6f18e MM |
2461 | readonly_error (arg, |
2462 | ((code == PREINCREMENT_EXPR | |
2463 | || code == POSTINCREMENT_EXPR) | |
2464 | ? "increment" : "decrement")); | |
3e4093b6 RS |
2465 | |
2466 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) | |
2467 | val = boolean_increment (code, arg); | |
2468 | else | |
53fb4de3 | 2469 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
3e4093b6 RS |
2470 | TREE_SIDE_EFFECTS (val) = 1; |
2471 | val = convert (result_type, val); | |
2472 | if (TREE_CODE (val) != code) | |
6de9cd9a | 2473 | TREE_NO_WARNING (val) = 1; |
3e4093b6 RS |
2474 | return val; |
2475 | } | |
2476 | ||
2477 | case ADDR_EXPR: | |
2478 | /* Note that this operation never does default_conversion. */ | |
2479 | ||
2480 | /* Let &* cancel out to simplify resulting code. */ | |
2481 | if (TREE_CODE (arg) == INDIRECT_REF) | |
400fbf9f | 2482 | { |
3e4093b6 RS |
2483 | /* Don't let this be an lvalue. */ |
2484 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
2485 | return non_lvalue (TREE_OPERAND (arg, 0)); | |
2486 | return TREE_OPERAND (arg, 0); | |
400fbf9f | 2487 | } |
1eb8759b | 2488 | |
3e4093b6 RS |
2489 | /* For &x[y], return x+y */ |
2490 | if (TREE_CODE (arg) == ARRAY_REF) | |
1eb8759b | 2491 | { |
3e4093b6 RS |
2492 | if (!c_mark_addressable (TREE_OPERAND (arg, 0))) |
2493 | return error_mark_node; | |
2494 | return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0), | |
2495 | TREE_OPERAND (arg, 1), 1); | |
1eb8759b | 2496 | } |
1eb8759b | 2497 | |
3e4093b6 RS |
2498 | /* Anything not already handled and not a true memory reference |
2499 | or a non-lvalue array is an error. */ | |
2500 | else if (typecode != FUNCTION_TYPE && !flag | |
2501 | && !lvalue_or_else (arg, "invalid lvalue in unary `&'")) | |
2502 | return error_mark_node; | |
b6a10c9f | 2503 | |
3e4093b6 RS |
2504 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ |
2505 | argtype = TREE_TYPE (arg); | |
400fbf9f | 2506 | |
3e4093b6 RS |
2507 | /* If the lvalue is const or volatile, merge that into the type |
2508 | to which the address will point. Note that you can't get a | |
2509 | restricted pointer by taking the address of something, so we | |
2510 | only have to deal with `const' and `volatile' here. */ | |
2511 | if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r') | |
2512 | && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))) | |
2513 | argtype = c_build_type_variant (argtype, | |
2514 | TREE_READONLY (arg), | |
2515 | TREE_THIS_VOLATILE (arg)); | |
400fbf9f | 2516 | |
3e4093b6 | 2517 | argtype = build_pointer_type (argtype); |
400fbf9f | 2518 | |
3e4093b6 RS |
2519 | if (!c_mark_addressable (arg)) |
2520 | return error_mark_node; | |
400fbf9f | 2521 | |
3e4093b6 RS |
2522 | { |
2523 | tree addr; | |
400fbf9f | 2524 | |
3e4093b6 RS |
2525 | if (TREE_CODE (arg) == COMPONENT_REF) |
2526 | { | |
2527 | tree field = TREE_OPERAND (arg, 1); | |
400fbf9f | 2528 | |
3e4093b6 | 2529 | addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag); |
400fbf9f | 2530 | |
3e4093b6 RS |
2531 | if (DECL_C_BIT_FIELD (field)) |
2532 | { | |
2533 | error ("attempt to take address of bit-field structure member `%s'", | |
2534 | IDENTIFIER_POINTER (DECL_NAME (field))); | |
2535 | return error_mark_node; | |
2536 | } | |
400fbf9f | 2537 | |
53fb4de3 RS |
2538 | addr = fold (build2 (PLUS_EXPR, argtype, |
2539 | convert (argtype, addr), | |
2540 | convert (argtype, byte_position (field)))); | |
9fc3b39a AP |
2541 | |
2542 | /* If the folded PLUS_EXPR is not a constant address, wrap | |
2543 | it in an ADDR_EXPR. */ | |
2544 | if (!TREE_CONSTANT (addr)) | |
2545 | addr = build1 (ADDR_EXPR, argtype, arg); | |
3e4093b6 RS |
2546 | } |
2547 | else | |
9fc3b39a | 2548 | addr = build1 (ADDR_EXPR, argtype, arg); |
400fbf9f | 2549 | |
44de5aeb RK |
2550 | if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR) |
2551 | TREE_INVARIANT (addr) = TREE_CONSTANT (addr) = 1; | |
2552 | ||
3e4093b6 RS |
2553 | return addr; |
2554 | } | |
400fbf9f | 2555 | |
3e4093b6 RS |
2556 | default: |
2557 | break; | |
2558 | } | |
400fbf9f | 2559 | |
3e4093b6 RS |
2560 | if (argtype == 0) |
2561 | argtype = TREE_TYPE (arg); | |
bf730f15 RS |
2562 | val = build1 (code, argtype, arg); |
2563 | return require_constant_value ? fold_initializer (val) : fold (val); | |
3e4093b6 | 2564 | } |
400fbf9f | 2565 | |
3e4093b6 RS |
2566 | /* Return nonzero if REF is an lvalue valid for this language. |
2567 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
5baeaac0 | 2568 | Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ |
400fbf9f | 2569 | |
3e4093b6 RS |
2570 | int |
2571 | lvalue_p (tree ref) | |
2572 | { | |
2573 | enum tree_code code = TREE_CODE (ref); | |
400fbf9f | 2574 | |
3e4093b6 RS |
2575 | switch (code) |
2576 | { | |
2577 | case REALPART_EXPR: | |
2578 | case IMAGPART_EXPR: | |
2579 | case COMPONENT_REF: | |
2580 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
400fbf9f | 2581 | |
3e4093b6 RS |
2582 | case COMPOUND_LITERAL_EXPR: |
2583 | case STRING_CST: | |
2584 | return 1; | |
400fbf9f | 2585 | |
3e4093b6 RS |
2586 | case INDIRECT_REF: |
2587 | case ARRAY_REF: | |
2588 | case VAR_DECL: | |
2589 | case PARM_DECL: | |
2590 | case RESULT_DECL: | |
2591 | case ERROR_MARK: | |
2592 | return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE | |
2593 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); | |
665f2503 | 2594 | |
3e4093b6 | 2595 | case BIND_EXPR: |
3e4093b6 | 2596 | return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; |
665f2503 | 2597 | |
3e4093b6 RS |
2598 | default: |
2599 | return 0; | |
2600 | } | |
2601 | } | |
400fbf9f | 2602 | |
3e4093b6 RS |
2603 | /* Return nonzero if REF is an lvalue valid for this language; |
2604 | otherwise, print an error message and return zero. */ | |
64c01f80 | 2605 | |
25a1a39e | 2606 | static int |
3e4093b6 RS |
2607 | lvalue_or_else (tree ref, const char *msgid) |
2608 | { | |
2609 | int win = lvalue_p (ref); | |
912b4fc3 | 2610 | |
3e4093b6 RS |
2611 | if (! win) |
2612 | error ("%s", msgid); | |
293c9fdd | 2613 | |
3e4093b6 RS |
2614 | return win; |
2615 | } | |
665f2503 | 2616 | |
400fbf9f | 2617 | \f |
3e4093b6 | 2618 | /* Warn about storing in something that is `const'. */ |
54c93c30 | 2619 | |
3e4093b6 | 2620 | void |
c5b6f18e | 2621 | readonly_error (tree arg, const char *msgid) |
54c93c30 | 2622 | { |
3e4093b6 | 2623 | if (TREE_CODE (arg) == COMPONENT_REF) |
54c93c30 | 2624 | { |
3e4093b6 | 2625 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) |
c5b6f18e | 2626 | readonly_error (TREE_OPERAND (arg, 0), msgid); |
3e4093b6 | 2627 | else |
c5b6f18e MM |
2628 | error ("%s of read-only member `%s'", _(msgid), |
2629 | IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1)))); | |
54c93c30 | 2630 | } |
3e4093b6 | 2631 | else if (TREE_CODE (arg) == VAR_DECL) |
c5b6f18e MM |
2632 | error ("%s of read-only variable `%s'", _(msgid), |
2633 | IDENTIFIER_POINTER (DECL_NAME (arg))); | |
3e4093b6 | 2634 | else |
c5b6f18e | 2635 | error ("%s of read-only location", _(msgid)); |
54c93c30 | 2636 | } |
3e4093b6 RS |
2637 | \f |
2638 | /* Mark EXP saying that we need to be able to take the | |
2639 | address of it; it should not be allocated in a register. | |
2640 | Returns true if successful. */ | |
54c93c30 | 2641 | |
3e4093b6 RS |
2642 | bool |
2643 | c_mark_addressable (tree exp) | |
400fbf9f | 2644 | { |
3e4093b6 | 2645 | tree x = exp; |
95602da1 | 2646 | |
3e4093b6 RS |
2647 | while (1) |
2648 | switch (TREE_CODE (x)) | |
2649 | { | |
2650 | case COMPONENT_REF: | |
2651 | if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) | |
2652 | { | |
2653 | error ("cannot take address of bit-field `%s'", | |
2654 | IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1)))); | |
2655 | return false; | |
2656 | } | |
95602da1 | 2657 | |
3e4093b6 | 2658 | /* ... fall through ... */ |
95602da1 | 2659 | |
3e4093b6 RS |
2660 | case ADDR_EXPR: |
2661 | case ARRAY_REF: | |
2662 | case REALPART_EXPR: | |
2663 | case IMAGPART_EXPR: | |
2664 | x = TREE_OPERAND (x, 0); | |
2665 | break; | |
95602da1 | 2666 | |
3e4093b6 RS |
2667 | case COMPOUND_LITERAL_EXPR: |
2668 | case CONSTRUCTOR: | |
2669 | TREE_ADDRESSABLE (x) = 1; | |
2670 | return true; | |
95602da1 | 2671 | |
3e4093b6 RS |
2672 | case VAR_DECL: |
2673 | case CONST_DECL: | |
2674 | case PARM_DECL: | |
2675 | case RESULT_DECL: | |
5baeaac0 | 2676 | if (C_DECL_REGISTER (x) |
3e4093b6 RS |
2677 | && DECL_NONLOCAL (x)) |
2678 | { | |
e697b20f | 2679 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
3e4093b6 RS |
2680 | { |
2681 | error ("global register variable `%s' used in nested function", | |
2682 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
2683 | return false; | |
2684 | } | |
2685 | pedwarn ("register variable `%s' used in nested function", | |
2686 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
2687 | } | |
5baeaac0 | 2688 | else if (C_DECL_REGISTER (x)) |
3e4093b6 | 2689 | { |
e697b20f | 2690 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
3e4093b6 RS |
2691 | { |
2692 | error ("address of global register variable `%s' requested", | |
2693 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
2694 | return false; | |
2695 | } | |
400fbf9f | 2696 | |
3e4093b6 RS |
2697 | pedwarn ("address of register variable `%s' requested", |
2698 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
2699 | } | |
400fbf9f | 2700 | |
3e4093b6 RS |
2701 | /* drops in */ |
2702 | case FUNCTION_DECL: | |
2703 | TREE_ADDRESSABLE (x) = 1; | |
2704 | /* drops out */ | |
2705 | default: | |
2706 | return true; | |
2707 | } | |
2708 | } | |
2709 | \f | |
2710 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. */ | |
400fbf9f JW |
2711 | |
2712 | tree | |
3e4093b6 | 2713 | build_conditional_expr (tree ifexp, tree op1, tree op2) |
400fbf9f | 2714 | { |
3e4093b6 RS |
2715 | tree type1; |
2716 | tree type2; | |
2717 | enum tree_code code1; | |
2718 | enum tree_code code2; | |
2719 | tree result_type = NULL; | |
2720 | tree orig_op1 = op1, orig_op2 = op2; | |
400fbf9f | 2721 | |
ae2bcd98 | 2722 | ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp)); |
3e4093b6 RS |
2723 | |
2724 | /* Promote both alternatives. */ | |
2725 | ||
2726 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
2727 | op1 = default_conversion (op1); | |
2728 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
2729 | op2 = default_conversion (op2); | |
2730 | ||
2731 | if (TREE_CODE (ifexp) == ERROR_MARK | |
2732 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
2733 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
400fbf9f | 2734 | return error_mark_node; |
400fbf9f | 2735 | |
3e4093b6 RS |
2736 | type1 = TREE_TYPE (op1); |
2737 | code1 = TREE_CODE (type1); | |
2738 | type2 = TREE_TYPE (op2); | |
2739 | code2 = TREE_CODE (type2); | |
2740 | ||
b1adf557 JM |
2741 | /* C90 does not permit non-lvalue arrays in conditional expressions. |
2742 | In C99 they will be pointers by now. */ | |
2743 | if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) | |
2744 | { | |
2745 | error ("non-lvalue array in conditional expression"); | |
2746 | return error_mark_node; | |
2747 | } | |
2748 | ||
3e4093b6 RS |
2749 | /* Quickly detect the usual case where op1 and op2 have the same type |
2750 | after promotion. */ | |
2751 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) | |
400fbf9f | 2752 | { |
3e4093b6 RS |
2753 | if (type1 == type2) |
2754 | result_type = type1; | |
2755 | else | |
2756 | result_type = TYPE_MAIN_VARIANT (type1); | |
2757 | } | |
2758 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
2759 | || code1 == COMPLEX_TYPE) | |
2760 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
2761 | || code2 == COMPLEX_TYPE)) | |
2762 | { | |
2763 | result_type = common_type (type1, type2); | |
400fbf9f | 2764 | |
3e4093b6 RS |
2765 | /* If -Wsign-compare, warn here if type1 and type2 have |
2766 | different signedness. We'll promote the signed to unsigned | |
2767 | and later code won't know it used to be different. | |
2768 | Do this check on the original types, so that explicit casts | |
2769 | will be considered, but default promotions won't. */ | |
2770 | if (warn_sign_compare && !skip_evaluation) | |
ab87f8c8 | 2771 | { |
8df83eae RK |
2772 | int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); |
2773 | int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); | |
400fbf9f | 2774 | |
3e4093b6 RS |
2775 | if (unsigned_op1 ^ unsigned_op2) |
2776 | { | |
2777 | /* Do not warn if the result type is signed, since the | |
2778 | signed type will only be chosen if it can represent | |
2779 | all the values of the unsigned type. */ | |
8df83eae | 2780 | if (! TYPE_UNSIGNED (result_type)) |
3e4093b6 RS |
2781 | /* OK */; |
2782 | /* Do not warn if the signed quantity is an unsuffixed | |
2783 | integer literal (or some static constant expression | |
2784 | involving such literals) and it is non-negative. */ | |
3a5b9284 RH |
2785 | else if ((unsigned_op2 && tree_expr_nonnegative_p (op1)) |
2786 | || (unsigned_op1 && tree_expr_nonnegative_p (op2))) | |
3e4093b6 RS |
2787 | /* OK */; |
2788 | else | |
2789 | warning ("signed and unsigned type in conditional expression"); | |
2790 | } | |
2791 | } | |
2792 | } | |
2793 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
2794 | { | |
2795 | if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE)) | |
2796 | pedwarn ("ISO C forbids conditional expr with only one void side"); | |
2797 | result_type = void_type_node; | |
2798 | } | |
2799 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
2800 | { | |
2801 | if (comp_target_types (type1, type2, 1)) | |
10bc1b1b | 2802 | result_type = common_pointer_type (type1, type2); |
3e4093b6 RS |
2803 | else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node |
2804 | && TREE_CODE (orig_op1) != NOP_EXPR) | |
2805 | result_type = qualify_type (type2, type1); | |
2806 | else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node | |
2807 | && TREE_CODE (orig_op2) != NOP_EXPR) | |
2808 | result_type = qualify_type (type1, type2); | |
2809 | else if (VOID_TYPE_P (TREE_TYPE (type1))) | |
34a80643 | 2810 | { |
3e4093b6 RS |
2811 | if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) |
2812 | pedwarn ("ISO C forbids conditional expr between `void *' and function pointer"); | |
2813 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type1), | |
2814 | TREE_TYPE (type2))); | |
34a80643 | 2815 | } |
3e4093b6 | 2816 | else if (VOID_TYPE_P (TREE_TYPE (type2))) |
1c2a9b35 | 2817 | { |
3e4093b6 RS |
2818 | if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) |
2819 | pedwarn ("ISO C forbids conditional expr between `void *' and function pointer"); | |
2820 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type2), | |
2821 | TREE_TYPE (type1))); | |
1c2a9b35 | 2822 | } |
34a80643 | 2823 | else |
ab87f8c8 | 2824 | { |
3e4093b6 RS |
2825 | pedwarn ("pointer type mismatch in conditional expression"); |
2826 | result_type = build_pointer_type (void_type_node); | |
ab87f8c8 | 2827 | } |
3e4093b6 RS |
2828 | } |
2829 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
2830 | { | |
2831 | if (! integer_zerop (op2)) | |
2832 | pedwarn ("pointer/integer type mismatch in conditional expression"); | |
2833 | else | |
ab87f8c8 | 2834 | { |
3e4093b6 | 2835 | op2 = null_pointer_node; |
ab87f8c8 | 2836 | } |
3e4093b6 RS |
2837 | result_type = type1; |
2838 | } | |
2839 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
2840 | { | |
2841 | if (!integer_zerop (op1)) | |
2842 | pedwarn ("pointer/integer type mismatch in conditional expression"); | |
2843 | else | |
ab87f8c8 | 2844 | { |
3e4093b6 | 2845 | op1 = null_pointer_node; |
ab87f8c8 | 2846 | } |
3e4093b6 RS |
2847 | result_type = type2; |
2848 | } | |
1c2a9b35 | 2849 | |
3e4093b6 RS |
2850 | if (!result_type) |
2851 | { | |
2852 | if (flag_cond_mismatch) | |
2853 | result_type = void_type_node; | |
2854 | else | |
400fbf9f | 2855 | { |
3e4093b6 | 2856 | error ("type mismatch in conditional expression"); |
ab87f8c8 | 2857 | return error_mark_node; |
400fbf9f | 2858 | } |
3e4093b6 | 2859 | } |
400fbf9f | 2860 | |
3e4093b6 RS |
2861 | /* Merge const and volatile flags of the incoming types. */ |
2862 | result_type | |
2863 | = build_type_variant (result_type, | |
2864 | TREE_READONLY (op1) || TREE_READONLY (op2), | |
2865 | TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2)); | |
b6a10c9f | 2866 | |
3e4093b6 RS |
2867 | if (result_type != TREE_TYPE (op1)) |
2868 | op1 = convert_and_check (result_type, op1); | |
2869 | if (result_type != TREE_TYPE (op2)) | |
2870 | op2 = convert_and_check (result_type, op2); | |
b6a10c9f | 2871 | |
3e4093b6 | 2872 | if (TREE_CODE (ifexp) == INTEGER_CST) |
53cd18ec | 2873 | return non_lvalue (integer_zerop (ifexp) ? op2 : op1); |
2f6e4e97 | 2874 | |
53fb4de3 | 2875 | return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2)); |
3e4093b6 RS |
2876 | } |
2877 | \f | |
487a92fe JM |
2878 | /* Return a compound expression that performs two expressions and |
2879 | returns the value of the second of them. */ | |
400fbf9f | 2880 | |
3e4093b6 | 2881 | tree |
487a92fe | 2882 | build_compound_expr (tree expr1, tree expr2) |
3e4093b6 | 2883 | { |
487a92fe JM |
2884 | /* Convert arrays and functions to pointers. */ |
2885 | expr2 = default_function_array_conversion (expr2); | |
400fbf9f | 2886 | |
487a92fe JM |
2887 | /* Don't let (0, 0) be null pointer constant. */ |
2888 | if (integer_zerop (expr2)) | |
2889 | expr2 = non_lvalue (expr2); | |
400fbf9f | 2890 | |
487a92fe | 2891 | if (! TREE_SIDE_EFFECTS (expr1)) |
3e4093b6 RS |
2892 | { |
2893 | /* The left-hand operand of a comma expression is like an expression | |
2894 | statement: with -Wextra or -Wunused, we should warn if it doesn't have | |
2895 | any side-effects, unless it was explicitly cast to (void). */ | |
2896 | if (warn_unused_value | |
487a92fe JM |
2897 | && ! (TREE_CODE (expr1) == CONVERT_EXPR |
2898 | && VOID_TYPE_P (TREE_TYPE (expr1)))) | |
3e4093b6 | 2899 | warning ("left-hand operand of comma expression has no effect"); |
3e4093b6 | 2900 | } |
400fbf9f | 2901 | |
3e4093b6 RS |
2902 | /* With -Wunused, we should also warn if the left-hand operand does have |
2903 | side-effects, but computes a value which is not used. For example, in | |
2904 | `foo() + bar(), baz()' the result of the `+' operator is not used, | |
2905 | so we should issue a warning. */ | |
2906 | else if (warn_unused_value) | |
487a92fe | 2907 | warn_if_unused_value (expr1, input_location); |
400fbf9f | 2908 | |
53fb4de3 | 2909 | return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); |
3e4093b6 | 2910 | } |
400fbf9f | 2911 | |
3e4093b6 | 2912 | /* Build an expression representing a cast to type TYPE of expression EXPR. */ |
400fbf9f | 2913 | |
3e4093b6 RS |
2914 | tree |
2915 | build_c_cast (tree type, tree expr) | |
2916 | { | |
2917 | tree value = expr; | |
400fbf9f | 2918 | |
3e4093b6 RS |
2919 | if (type == error_mark_node || expr == error_mark_node) |
2920 | return error_mark_node; | |
400fbf9f | 2921 | |
3e4093b6 RS |
2922 | /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing |
2923 | only in <protocol> qualifications. But when constructing cast expressions, | |
2924 | the protocols do matter and must be kept around. */ | |
264fa2db | 2925 | if (!c_dialect_objc () || !objc_is_object_ptr (type)) |
3e4093b6 | 2926 | type = TYPE_MAIN_VARIANT (type); |
400fbf9f | 2927 | |
3e4093b6 RS |
2928 | if (TREE_CODE (type) == ARRAY_TYPE) |
2929 | { | |
2930 | error ("cast specifies array type"); | |
2931 | return error_mark_node; | |
2932 | } | |
400fbf9f | 2933 | |
3e4093b6 RS |
2934 | if (TREE_CODE (type) == FUNCTION_TYPE) |
2935 | { | |
2936 | error ("cast specifies function type"); | |
2937 | return error_mark_node; | |
2938 | } | |
400fbf9f | 2939 | |
3e4093b6 RS |
2940 | if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) |
2941 | { | |
2942 | if (pedantic) | |
400fbf9f | 2943 | { |
3e4093b6 RS |
2944 | if (TREE_CODE (type) == RECORD_TYPE |
2945 | || TREE_CODE (type) == UNION_TYPE) | |
2946 | pedwarn ("ISO C forbids casting nonscalar to the same type"); | |
400fbf9f | 2947 | } |
3e4093b6 RS |
2948 | } |
2949 | else if (TREE_CODE (type) == UNION_TYPE) | |
2950 | { | |
2951 | tree field; | |
2952 | value = default_function_array_conversion (value); | |
400fbf9f | 2953 | |
3e4093b6 RS |
2954 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) |
2955 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
132da1a5 | 2956 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) |
3e4093b6 RS |
2957 | break; |
2958 | ||
2959 | if (field) | |
400fbf9f | 2960 | { |
3e4093b6 RS |
2961 | tree t; |
2962 | ||
2963 | if (pedantic) | |
2964 | pedwarn ("ISO C forbids casts to union type"); | |
2965 | t = digest_init (type, | |
2966 | build_constructor (type, | |
2967 | build_tree_list (field, value)), | |
916c5919 | 2968 | true, 0); |
3e4093b6 | 2969 | TREE_CONSTANT (t) = TREE_CONSTANT (value); |
6de9cd9a | 2970 | TREE_INVARIANT (t) = TREE_INVARIANT (value); |
3e4093b6 | 2971 | return t; |
400fbf9f | 2972 | } |
3e4093b6 RS |
2973 | error ("cast to union type from type not present in union"); |
2974 | return error_mark_node; | |
2975 | } | |
2976 | else | |
2977 | { | |
2978 | tree otype, ovalue; | |
400fbf9f | 2979 | |
3e4093b6 RS |
2980 | /* If casting to void, avoid the error that would come |
2981 | from default_conversion in the case of a non-lvalue array. */ | |
2982 | if (type == void_type_node) | |
2983 | return build1 (CONVERT_EXPR, type, value); | |
400fbf9f | 2984 | |
3e4093b6 RS |
2985 | /* Convert functions and arrays to pointers, |
2986 | but don't convert any other types. */ | |
2987 | value = default_function_array_conversion (value); | |
2988 | otype = TREE_TYPE (value); | |
400fbf9f | 2989 | |
3e4093b6 | 2990 | /* Optionally warn about potentially worrisome casts. */ |
770ae6cc | 2991 | |
3e4093b6 RS |
2992 | if (warn_cast_qual |
2993 | && TREE_CODE (type) == POINTER_TYPE | |
2994 | && TREE_CODE (otype) == POINTER_TYPE) | |
2995 | { | |
2996 | tree in_type = type; | |
2997 | tree in_otype = otype; | |
2998 | int added = 0; | |
2999 | int discarded = 0; | |
400fbf9f | 3000 | |
3e4093b6 RS |
3001 | /* Check that the qualifiers on IN_TYPE are a superset of |
3002 | the qualifiers of IN_OTYPE. The outermost level of | |
3003 | POINTER_TYPE nodes is uninteresting and we stop as soon | |
3004 | as we hit a non-POINTER_TYPE node on either type. */ | |
3005 | do | |
3006 | { | |
3007 | in_otype = TREE_TYPE (in_otype); | |
3008 | in_type = TREE_TYPE (in_type); | |
400fbf9f | 3009 | |
3e4093b6 RS |
3010 | /* GNU C allows cv-qualified function types. 'const' |
3011 | means the function is very pure, 'volatile' means it | |
3012 | can't return. We need to warn when such qualifiers | |
3013 | are added, not when they're taken away. */ | |
3014 | if (TREE_CODE (in_otype) == FUNCTION_TYPE | |
3015 | && TREE_CODE (in_type) == FUNCTION_TYPE) | |
3016 | added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype)); | |
3017 | else | |
3018 | discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type)); | |
3019 | } | |
3020 | while (TREE_CODE (in_type) == POINTER_TYPE | |
3021 | && TREE_CODE (in_otype) == POINTER_TYPE); | |
400fbf9f | 3022 | |
3e4093b6 RS |
3023 | if (added) |
3024 | warning ("cast adds new qualifiers to function type"); | |
400fbf9f | 3025 | |
3e4093b6 RS |
3026 | if (discarded) |
3027 | /* There are qualifiers present in IN_OTYPE that are not | |
3028 | present in IN_TYPE. */ | |
3029 | warning ("cast discards qualifiers from pointer target type"); | |
3030 | } | |
400fbf9f | 3031 | |
3e4093b6 RS |
3032 | /* Warn about possible alignment problems. */ |
3033 | if (STRICT_ALIGNMENT && warn_cast_align | |
3034 | && TREE_CODE (type) == POINTER_TYPE | |
3035 | && TREE_CODE (otype) == POINTER_TYPE | |
3036 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
3037 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
3038 | /* Don't warn about opaque types, where the actual alignment | |
3039 | restriction is unknown. */ | |
3040 | && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE | |
3041 | || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) | |
3042 | && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) | |
3043 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) | |
3044 | warning ("cast increases required alignment of target type"); | |
e9a25f70 | 3045 | |
3e4093b6 RS |
3046 | if (TREE_CODE (type) == INTEGER_TYPE |
3047 | && TREE_CODE (otype) == POINTER_TYPE | |
3048 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) | |
3049 | && !TREE_CONSTANT (value)) | |
3050 | warning ("cast from pointer to integer of different size"); | |
400fbf9f | 3051 | |
3e4093b6 RS |
3052 | if (warn_bad_function_cast |
3053 | && TREE_CODE (value) == CALL_EXPR | |
3054 | && TREE_CODE (type) != TREE_CODE (otype)) | |
3055 | warning ("cast does not match function type"); | |
400fbf9f | 3056 | |
3e4093b6 RS |
3057 | if (TREE_CODE (type) == POINTER_TYPE |
3058 | && TREE_CODE (otype) == INTEGER_TYPE | |
3059 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) | |
3060 | /* Don't warn about converting any constant. */ | |
3061 | && !TREE_CONSTANT (value)) | |
3062 | warning ("cast to pointer from integer of different size"); | |
400fbf9f | 3063 | |
3e4093b6 RS |
3064 | if (TREE_CODE (type) == POINTER_TYPE |
3065 | && TREE_CODE (otype) == POINTER_TYPE | |
3066 | && TREE_CODE (expr) == ADDR_EXPR | |
3067 | && DECL_P (TREE_OPERAND (expr, 0)) | |
3068 | && flag_strict_aliasing && warn_strict_aliasing | |
3069 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
3070 | { | |
3071 | /* Casting the address of a decl to non void pointer. Warn | |
3072 | if the cast breaks type based aliasing. */ | |
3073 | if (!COMPLETE_TYPE_P (TREE_TYPE (type))) | |
3074 | warning ("type-punning to incomplete type might break strict-aliasing rules"); | |
5399d643 JW |
3075 | else |
3076 | { | |
3077 | HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); | |
3078 | HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type)); | |
3079 | ||
3080 | if (!alias_sets_conflict_p (set1, set2)) | |
3081 | warning ("dereferencing type-punned pointer will break strict-aliasing rules"); | |
3082 | else if (warn_strict_aliasing > 1 | |
3083 | && !alias_sets_might_conflict_p (set1, set2)) | |
3084 | warning ("dereferencing type-punned pointer might break strict-aliasing rules"); | |
3085 | } | |
3e4093b6 | 3086 | } |
400fbf9f | 3087 | |
3897f229 JM |
3088 | /* If pedantic, warn for conversions between function and object |
3089 | pointer types, except for converting a null pointer constant | |
3090 | to function pointer type. */ | |
3091 | if (pedantic | |
3092 | && TREE_CODE (type) == POINTER_TYPE | |
3093 | && TREE_CODE (otype) == POINTER_TYPE | |
3094 | && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE | |
3095 | && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) | |
3096 | pedwarn ("ISO C forbids conversion of function pointer to object pointer type"); | |
3097 | ||
3098 | if (pedantic | |
3099 | && TREE_CODE (type) == POINTER_TYPE | |
3100 | && TREE_CODE (otype) == POINTER_TYPE | |
3101 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
3102 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
3103 | && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node | |
3104 | && TREE_CODE (expr) != NOP_EXPR)) | |
3105 | pedwarn ("ISO C forbids conversion of object pointer to function pointer type"); | |
3106 | ||
3e4093b6 RS |
3107 | ovalue = value; |
3108 | /* Replace a nonvolatile const static variable with its value. */ | |
3109 | if (optimize && TREE_CODE (value) == VAR_DECL) | |
3110 | value = decl_constant_value (value); | |
3111 | value = convert (type, value); | |
400fbf9f | 3112 | |
3e4093b6 RS |
3113 | /* Ignore any integer overflow caused by the cast. */ |
3114 | if (TREE_CODE (value) == INTEGER_CST) | |
3115 | { | |
092313ae | 3116 | if (EXPR_P (ovalue)) |
89b0433e NS |
3117 | /* If OVALUE had overflow set, then so will VALUE, so it |
3118 | is safe to overwrite. */ | |
092313ae NS |
3119 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); |
3120 | else | |
3121 | TREE_OVERFLOW (value) = 0; | |
3122 | ||
22421b79 | 3123 | if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c') |
89b0433e NS |
3124 | /* Similarly, constant_overflow cannot have become |
3125 | cleared. */ | |
22421b79 | 3126 | TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue); |
3e4093b6 RS |
3127 | } |
3128 | } | |
400fbf9f | 3129 | |
53cd18ec JM |
3130 | /* Don't let (void *) (FOO *) 0 be a null pointer constant. */ |
3131 | if (TREE_CODE (value) == INTEGER_CST | |
3e4093b6 RS |
3132 | && TREE_CODE (expr) == INTEGER_CST |
3133 | && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE) | |
3134 | value = non_lvalue (value); | |
cff9c407 | 3135 | |
53cd18ec JM |
3136 | /* Don't let a cast be an lvalue. */ |
3137 | if (value == expr) | |
3e4093b6 | 3138 | value = non_lvalue (value); |
e9a25f70 | 3139 | |
3e4093b6 | 3140 | return value; |
400fbf9f JW |
3141 | } |
3142 | ||
3e4093b6 RS |
3143 | /* Interpret a cast of expression EXPR to type TYPE. */ |
3144 | tree | |
3145 | c_cast_expr (tree type, tree expr) | |
400fbf9f | 3146 | { |
3e4093b6 | 3147 | int saved_wsp = warn_strict_prototypes; |
c5c76735 | 3148 | |
3e4093b6 RS |
3149 | /* This avoids warnings about unprototyped casts on |
3150 | integers. E.g. "#define SIG_DFL (void(*)())0". */ | |
3151 | if (TREE_CODE (expr) == INTEGER_CST) | |
3152 | warn_strict_prototypes = 0; | |
3153 | type = groktypename (type); | |
3154 | warn_strict_prototypes = saved_wsp; | |
c5c76735 | 3155 | |
3e4093b6 | 3156 | return build_c_cast (type, expr); |
400fbf9f JW |
3157 | } |
3158 | ||
3e4093b6 RS |
3159 | \f |
3160 | /* Build an assignment expression of lvalue LHS from value RHS. | |
3161 | MODIFYCODE is the code for a binary operator that we use | |
3162 | to combine the old value of LHS with RHS to get the new value. | |
3163 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */ | |
2f6e4e97 | 3164 | |
3e4093b6 RS |
3165 | tree |
3166 | build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs) | |
400fbf9f | 3167 | { |
3e4093b6 RS |
3168 | tree result; |
3169 | tree newrhs; | |
3170 | tree lhstype = TREE_TYPE (lhs); | |
3171 | tree olhstype = lhstype; | |
e9a25f70 | 3172 | |
3e4093b6 RS |
3173 | /* Types that aren't fully specified cannot be used in assignments. */ |
3174 | lhs = require_complete_type (lhs); | |
e9a25f70 | 3175 | |
3e4093b6 RS |
3176 | /* Avoid duplicate error messages from operands that had errors. */ |
3177 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
3178 | return error_mark_node; | |
400fbf9f | 3179 | |
3e4093b6 RS |
3180 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ |
3181 | /* Do not use STRIP_NOPS here. We do not want an enumerator | |
3182 | whose value is 0 to count as a null pointer constant. */ | |
3183 | if (TREE_CODE (rhs) == NON_LVALUE_EXPR) | |
3184 | rhs = TREE_OPERAND (rhs, 0); | |
e9a25f70 | 3185 | |
3e4093b6 | 3186 | newrhs = rhs; |
400fbf9f | 3187 | |
3e4093b6 RS |
3188 | /* If a binary op has been requested, combine the old LHS value with the RHS |
3189 | producing the value we should actually store into the LHS. */ | |
3190 | ||
3191 | if (modifycode != NOP_EXPR) | |
400fbf9f | 3192 | { |
3e4093b6 RS |
3193 | lhs = stabilize_reference (lhs); |
3194 | newrhs = build_binary_op (modifycode, lhs, rhs, 1); | |
400fbf9f | 3195 | } |
400fbf9f | 3196 | |
3e4093b6 RS |
3197 | if (!lvalue_or_else (lhs, "invalid lvalue in assignment")) |
3198 | return error_mark_node; | |
400fbf9f | 3199 | |
3e4093b6 | 3200 | /* Warn about storing in something that is `const'. */ |
bbbd6700 | 3201 | |
3e4093b6 RS |
3202 | if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype) |
3203 | || ((TREE_CODE (lhstype) == RECORD_TYPE | |
3204 | || TREE_CODE (lhstype) == UNION_TYPE) | |
3205 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
c5b6f18e | 3206 | readonly_error (lhs, "assignment"); |
bbbd6700 | 3207 | |
3e4093b6 RS |
3208 | /* If storing into a structure or union member, |
3209 | it has probably been given type `int'. | |
3210 | Compute the type that would go with | |
3211 | the actual amount of storage the member occupies. */ | |
bbbd6700 | 3212 | |
3e4093b6 RS |
3213 | if (TREE_CODE (lhs) == COMPONENT_REF |
3214 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
3215 | || TREE_CODE (lhstype) == BOOLEAN_TYPE | |
3216 | || TREE_CODE (lhstype) == REAL_TYPE | |
3217 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
3218 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
400fbf9f | 3219 | |
3e4093b6 RS |
3220 | /* If storing in a field that is in actuality a short or narrower than one, |
3221 | we must store in the field in its actual type. */ | |
3222 | ||
3223 | if (lhstype != TREE_TYPE (lhs)) | |
3224 | { | |
3225 | lhs = copy_node (lhs); | |
3226 | TREE_TYPE (lhs) = lhstype; | |
400fbf9f | 3227 | } |
400fbf9f | 3228 | |
3e4093b6 | 3229 | /* Convert new value to destination type. */ |
400fbf9f | 3230 | |
3e4093b6 RS |
3231 | newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"), |
3232 | NULL_TREE, NULL_TREE, 0); | |
3233 | if (TREE_CODE (newrhs) == ERROR_MARK) | |
3234 | return error_mark_node; | |
400fbf9f | 3235 | |
3e4093b6 | 3236 | /* Scan operands */ |
400fbf9f | 3237 | |
53fb4de3 | 3238 | result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); |
3e4093b6 | 3239 | TREE_SIDE_EFFECTS (result) = 1; |
400fbf9f | 3240 | |
3e4093b6 RS |
3241 | /* If we got the LHS in a different type for storing in, |
3242 | convert the result back to the nominal type of LHS | |
3243 | so that the value we return always has the same type | |
3244 | as the LHS argument. */ | |
e855c5ce | 3245 | |
3e4093b6 RS |
3246 | if (olhstype == TREE_TYPE (result)) |
3247 | return result; | |
3248 | return convert_for_assignment (olhstype, result, _("assignment"), | |
3249 | NULL_TREE, NULL_TREE, 0); | |
3250 | } | |
3251 | \f | |
3252 | /* Convert value RHS to type TYPE as preparation for an assignment | |
3253 | to an lvalue of type TYPE. | |
3254 | The real work of conversion is done by `convert'. | |
3255 | The purpose of this function is to generate error messages | |
3256 | for assignments that are not allowed in C. | |
3257 | ERRTYPE is a string to use in error messages: | |
3258 | "assignment", "return", etc. If it is null, this is parameter passing | |
3259 | for a function call (and different error messages are output). | |
2f6e4e97 | 3260 | |
3e4093b6 RS |
3261 | FUNNAME is the name of the function being called, |
3262 | as an IDENTIFIER_NODE, or null. | |
3263 | PARMNUM is the number of the argument, for printing in error messages. */ | |
cb3ca04e | 3264 | |
3e4093b6 RS |
3265 | static tree |
3266 | convert_for_assignment (tree type, tree rhs, const char *errtype, | |
3267 | tree fundecl, tree funname, int parmnum) | |
3268 | { | |
3269 | enum tree_code codel = TREE_CODE (type); | |
3270 | tree rhstype; | |
3271 | enum tree_code coder; | |
cb3ca04e | 3272 | |
3e4093b6 RS |
3273 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ |
3274 | /* Do not use STRIP_NOPS here. We do not want an enumerator | |
3275 | whose value is 0 to count as a null pointer constant. */ | |
3276 | if (TREE_CODE (rhs) == NON_LVALUE_EXPR) | |
3277 | rhs = TREE_OPERAND (rhs, 0); | |
3278 | ||
3279 | if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE | |
3280 | || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE) | |
3281 | rhs = default_conversion (rhs); | |
3282 | else if (optimize && TREE_CODE (rhs) == VAR_DECL) | |
3283 | rhs = decl_constant_value_for_broken_optimization (rhs); | |
3284 | ||
3285 | rhstype = TREE_TYPE (rhs); | |
3286 | coder = TREE_CODE (rhstype); | |
3287 | ||
3288 | if (coder == ERROR_MARK) | |
3289 | return error_mark_node; | |
3290 | ||
3291 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) | |
400fbf9f | 3292 | { |
3e4093b6 RS |
3293 | overflow_warning (rhs); |
3294 | /* Check for Objective-C protocols. This will automatically | |
3295 | issue a warning if there are protocol violations. No need to | |
3296 | use the return value. */ | |
3297 | if (c_dialect_objc ()) | |
3298 | objc_comptypes (type, rhstype, 0); | |
3299 | return rhs; | |
400fbf9f | 3300 | } |
3e4093b6 RS |
3301 | |
3302 | if (coder == VOID_TYPE) | |
400fbf9f | 3303 | { |
3e4093b6 RS |
3304 | error ("void value not ignored as it ought to be"); |
3305 | return error_mark_node; | |
400fbf9f | 3306 | } |
3e4093b6 RS |
3307 | /* A type converts to a reference to it. |
3308 | This code doesn't fully support references, it's just for the | |
3309 | special case of va_start and va_copy. */ | |
3310 | if (codel == REFERENCE_TYPE | |
132da1a5 | 3311 | && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1) |
400fbf9f | 3312 | { |
3e4093b6 | 3313 | if (!lvalue_p (rhs)) |
400fbf9f | 3314 | { |
3e4093b6 RS |
3315 | error ("cannot pass rvalue to reference parameter"); |
3316 | return error_mark_node; | |
400fbf9f | 3317 | } |
3e4093b6 RS |
3318 | if (!c_mark_addressable (rhs)) |
3319 | return error_mark_node; | |
3320 | rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); | |
3321 | ||
3322 | /* We already know that these two types are compatible, but they | |
3323 | may not be exactly identical. In fact, `TREE_TYPE (type)' is | |
3324 | likely to be __builtin_va_list and `TREE_TYPE (rhs)' is | |
3325 | likely to be va_list, a typedef to __builtin_va_list, which | |
3326 | is different enough that it will cause problems later. */ | |
3327 | if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type)) | |
3328 | rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs); | |
3329 | ||
3330 | rhs = build1 (NOP_EXPR, type, rhs); | |
3331 | return rhs; | |
400fbf9f | 3332 | } |
3e4093b6 | 3333 | /* Some types can interconvert without explicit casts. */ |
4a5eab38 | 3334 | else if (codel == VECTOR_TYPE |
cc27e657 | 3335 | && vector_types_convertible_p (type, TREE_TYPE (rhs))) |
3e4093b6 RS |
3336 | return convert (type, rhs); |
3337 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
3338 | else if ((codel == INTEGER_TYPE || codel == REAL_TYPE | |
3339 | || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE | |
3340 | || codel == BOOLEAN_TYPE) | |
3341 | && (coder == INTEGER_TYPE || coder == REAL_TYPE | |
3342 | || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE | |
3343 | || coder == BOOLEAN_TYPE)) | |
3344 | return convert_and_check (type, rhs); | |
400fbf9f | 3345 | |
3e4093b6 RS |
3346 | /* Conversion to a transparent union from its member types. |
3347 | This applies only to function arguments. */ | |
3348 | else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype) | |
400fbf9f | 3349 | { |
3e4093b6 RS |
3350 | tree memb_types; |
3351 | tree marginal_memb_type = 0; | |
3352 | ||
3353 | for (memb_types = TYPE_FIELDS (type); memb_types; | |
3354 | memb_types = TREE_CHAIN (memb_types)) | |
400fbf9f | 3355 | { |
3e4093b6 | 3356 | tree memb_type = TREE_TYPE (memb_types); |
400fbf9f | 3357 | |
3e4093b6 | 3358 | if (comptypes (TYPE_MAIN_VARIANT (memb_type), |
132da1a5 | 3359 | TYPE_MAIN_VARIANT (rhstype))) |
3e4093b6 | 3360 | break; |
e58cd767 | 3361 | |
3e4093b6 RS |
3362 | if (TREE_CODE (memb_type) != POINTER_TYPE) |
3363 | continue; | |
2f6e4e97 | 3364 | |
3e4093b6 RS |
3365 | if (coder == POINTER_TYPE) |
3366 | { | |
3367 | tree ttl = TREE_TYPE (memb_type); | |
3368 | tree ttr = TREE_TYPE (rhstype); | |
400fbf9f | 3369 | |
3e4093b6 RS |
3370 | /* Any non-function converts to a [const][volatile] void * |
3371 | and vice versa; otherwise, targets must be the same. | |
3372 | Meanwhile, the lhs target must have all the qualifiers of | |
3373 | the rhs. */ | |
3374 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
3375 | || comp_target_types (memb_type, rhstype, 0)) | |
3376 | { | |
3377 | /* If this type won't generate any warnings, use it. */ | |
3378 | if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr) | |
3379 | || ((TREE_CODE (ttr) == FUNCTION_TYPE | |
3380 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
3381 | ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
3382 | == TYPE_QUALS (ttr)) | |
3383 | : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
3384 | == TYPE_QUALS (ttl)))) | |
3385 | break; | |
400fbf9f | 3386 | |
3e4093b6 RS |
3387 | /* Keep looking for a better type, but remember this one. */ |
3388 | if (! marginal_memb_type) | |
3389 | marginal_memb_type = memb_type; | |
3390 | } | |
3391 | } | |
82bde854 | 3392 | |
3e4093b6 RS |
3393 | /* Can convert integer zero to any pointer type. */ |
3394 | if (integer_zerop (rhs) | |
3395 | || (TREE_CODE (rhs) == NOP_EXPR | |
3396 | && integer_zerop (TREE_OPERAND (rhs, 0)))) | |
3397 | { | |
3398 | rhs = null_pointer_node; | |
3399 | break; | |
3400 | } | |
3401 | } | |
400fbf9f | 3402 | |
3e4093b6 RS |
3403 | if (memb_types || marginal_memb_type) |
3404 | { | |
3405 | if (! memb_types) | |
3406 | { | |
3407 | /* We have only a marginally acceptable member type; | |
3408 | it needs a warning. */ | |
3409 | tree ttl = TREE_TYPE (marginal_memb_type); | |
3410 | tree ttr = TREE_TYPE (rhstype); | |
714a0864 | 3411 | |
3e4093b6 RS |
3412 | /* Const and volatile mean something different for function |
3413 | types, so the usual warnings are not appropriate. */ | |
3414 | if (TREE_CODE (ttr) == FUNCTION_TYPE | |
3415 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
3416 | { | |
3417 | /* Because const and volatile on functions are | |
3418 | restrictions that say the function will not do | |
3419 | certain things, it is okay to use a const or volatile | |
3420 | function where an ordinary one is wanted, but not | |
3421 | vice-versa. */ | |
3422 | if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)) | |
3423 | warn_for_assignment ("%s makes qualified function pointer from unqualified", | |
3424 | errtype, funname, parmnum); | |
3425 | } | |
3426 | else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)) | |
3427 | warn_for_assignment ("%s discards qualifiers from pointer target type", | |
3428 | errtype, funname, | |
3429 | parmnum); | |
3430 | } | |
400fbf9f | 3431 | |
3e4093b6 RS |
3432 | if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl)) |
3433 | pedwarn ("ISO C prohibits argument conversion to union type"); | |
0e7c47fa | 3434 | |
3e4093b6 RS |
3435 | return build1 (NOP_EXPR, type, rhs); |
3436 | } | |
0e7c47fa RK |
3437 | } |
3438 | ||
3e4093b6 RS |
3439 | /* Conversions among pointers */ |
3440 | else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
3441 | && (coder == codel)) | |
400fbf9f | 3442 | { |
3e4093b6 RS |
3443 | tree ttl = TREE_TYPE (type); |
3444 | tree ttr = TREE_TYPE (rhstype); | |
3445 | bool is_opaque_pointer; | |
264fa2db | 3446 | int target_cmp = 0; /* Cache comp_target_types () result. */ |
400fbf9f | 3447 | |
3e4093b6 | 3448 | /* Opaque pointers are treated like void pointers. */ |
5fd9b178 KH |
3449 | is_opaque_pointer = (targetm.vector_opaque_p (type) |
3450 | || targetm.vector_opaque_p (rhstype)) | |
3e4093b6 RS |
3451 | && TREE_CODE (ttl) == VECTOR_TYPE |
3452 | && TREE_CODE (ttr) == VECTOR_TYPE; | |
400fbf9f | 3453 | |
3e4093b6 RS |
3454 | /* Any non-function converts to a [const][volatile] void * |
3455 | and vice versa; otherwise, targets must be the same. | |
3456 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
3457 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
264fa2db | 3458 | || (target_cmp = comp_target_types (type, rhstype, 0)) |
3e4093b6 RS |
3459 | || is_opaque_pointer |
3460 | || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl)) | |
3461 | == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr)))) | |
3462 | { | |
3463 | if (pedantic | |
3464 | && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) | |
3465 | || | |
3466 | (VOID_TYPE_P (ttr) | |
3467 | /* Check TREE_CODE to catch cases like (void *) (char *) 0 | |
3468 | which are not ANSI null ptr constants. */ | |
3469 | && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR) | |
3470 | && TREE_CODE (ttl) == FUNCTION_TYPE))) | |
3471 | warn_for_assignment ("ISO C forbids %s between function pointer and `void *'", | |
3472 | errtype, funname, parmnum); | |
3473 | /* Const and volatile mean something different for function types, | |
3474 | so the usual warnings are not appropriate. */ | |
3475 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
3476 | && TREE_CODE (ttl) != FUNCTION_TYPE) | |
3477 | { | |
3478 | if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)) | |
3479 | warn_for_assignment ("%s discards qualifiers from pointer target type", | |
3480 | errtype, funname, parmnum); | |
3481 | /* If this is not a case of ignoring a mismatch in signedness, | |
3482 | no warning. */ | |
3483 | else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
264fa2db | 3484 | || target_cmp) |
3e4093b6 RS |
3485 | ; |
3486 | /* If there is a mismatch, do warn. */ | |
0c6d8428 | 3487 | else |
3e4093b6 RS |
3488 | warn_for_assignment ("pointer targets in %s differ in signedness", |
3489 | errtype, funname, parmnum); | |
3490 | } | |
3491 | else if (TREE_CODE (ttl) == FUNCTION_TYPE | |
3492 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
3493 | { | |
3494 | /* Because const and volatile on functions are restrictions | |
3495 | that say the function will not do certain things, | |
3496 | it is okay to use a const or volatile function | |
3497 | where an ordinary one is wanted, but not vice-versa. */ | |
3498 | if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)) | |
3499 | warn_for_assignment ("%s makes qualified function pointer from unqualified", | |
3500 | errtype, funname, parmnum); | |
3501 | } | |
3502 | } | |
3503 | else | |
3504 | warn_for_assignment ("%s from incompatible pointer type", | |
3505 | errtype, funname, parmnum); | |
3506 | return convert (type, rhs); | |
3507 | } | |
b494fd98 EB |
3508 | else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) |
3509 | { | |
3510 | error ("invalid use of non-lvalue array"); | |
3511 | return error_mark_node; | |
3512 | } | |
3e4093b6 | 3513 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) |
400fbf9f | 3514 | { |
3e4093b6 RS |
3515 | /* An explicit constant 0 can convert to a pointer, |
3516 | or one that results from arithmetic, even including | |
3517 | a cast to integer type. */ | |
3518 | if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)) | |
3519 | && | |
3520 | ! (TREE_CODE (rhs) == NOP_EXPR | |
3521 | && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE | |
3522 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST | |
3523 | && integer_zerop (TREE_OPERAND (rhs, 0)))) | |
3e4093b6 RS |
3524 | warn_for_assignment ("%s makes pointer from integer without a cast", |
3525 | errtype, funname, parmnum); | |
b3006337 EB |
3526 | |
3527 | return convert (type, rhs); | |
400fbf9f | 3528 | } |
3e4093b6 | 3529 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) |
400fbf9f | 3530 | { |
3e4093b6 RS |
3531 | warn_for_assignment ("%s makes integer from pointer without a cast", |
3532 | errtype, funname, parmnum); | |
3533 | return convert (type, rhs); | |
3534 | } | |
3535 | else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE) | |
3536 | return convert (type, rhs); | |
400fbf9f | 3537 | |
3e4093b6 RS |
3538 | if (!errtype) |
3539 | { | |
3540 | if (funname) | |
400fbf9f | 3541 | { |
3e4093b6 | 3542 | tree selector = objc_message_selector (); |
805f961c | 3543 | |
3e4093b6 RS |
3544 | if (selector && parmnum > 2) |
3545 | error ("incompatible type for argument %d of `%s'", | |
3546 | parmnum - 2, IDENTIFIER_POINTER (selector)); | |
3547 | else | |
3548 | error ("incompatible type for argument %d of `%s'", | |
3549 | parmnum, IDENTIFIER_POINTER (funname)); | |
400fbf9f | 3550 | } |
3e4093b6 RS |
3551 | else |
3552 | error ("incompatible type for argument %d of indirect function call", | |
3553 | parmnum); | |
400fbf9f JW |
3554 | } |
3555 | else | |
3e4093b6 | 3556 | error ("incompatible types in %s", errtype); |
53b01f59 | 3557 | |
3e4093b6 RS |
3558 | return error_mark_node; |
3559 | } | |
53b01f59 | 3560 | |
d5123bae MS |
3561 | /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM |
3562 | is used for error and waring reporting and indicates which argument | |
3563 | is being processed. */ | |
400fbf9f | 3564 | |
3e4093b6 | 3565 | tree |
d5123bae | 3566 | c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum) |
3e4093b6 RS |
3567 | { |
3568 | tree ret, type; | |
400fbf9f | 3569 | |
3e4093b6 RS |
3570 | /* If FN was prototyped, the value has been converted already |
3571 | in convert_arguments. */ | |
3572 | if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn))) | |
3573 | return value; | |
f5963e61 | 3574 | |
3e4093b6 RS |
3575 | type = TREE_TYPE (parm); |
3576 | ret = convert_for_assignment (type, value, | |
3577 | (char *) 0 /* arg passing */, fn, | |
d5123bae | 3578 | DECL_NAME (fn), argnum); |
61f71b34 | 3579 | if (targetm.calls.promote_prototypes (TREE_TYPE (fn)) |
3e4093b6 RS |
3580 | && INTEGRAL_TYPE_P (type) |
3581 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) | |
3582 | ret = default_conversion (ret); | |
3583 | return ret; | |
3584 | } | |
af702de8 | 3585 | |
3e4093b6 RS |
3586 | /* Print a warning using MSGID. |
3587 | It gets OPNAME as its one parameter. | |
3588 | if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'". | |
3589 | Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'". | |
3590 | FUNCTION and ARGNUM are handled specially if we are building an | |
3591 | Objective-C selector. */ | |
cd6311ef | 3592 | |
3e4093b6 RS |
3593 | static void |
3594 | warn_for_assignment (const char *msgid, const char *opname, tree function, | |
3595 | int argnum) | |
3596 | { | |
3597 | if (opname == 0) | |
3598 | { | |
3599 | tree selector = objc_message_selector (); | |
3600 | char * new_opname; | |
af702de8 | 3601 | |
3e4093b6 RS |
3602 | if (selector && argnum > 2) |
3603 | { | |
3604 | function = selector; | |
3605 | argnum -= 2; | |
400fbf9f | 3606 | } |
3e4093b6 | 3607 | if (argnum == 0) |
73a73768 | 3608 | { |
3e4093b6 RS |
3609 | if (function) |
3610 | { | |
3611 | /* Function name is known; supply it. */ | |
3612 | const char *const argstring = _("passing arg of `%s'"); | |
28dab132 | 3613 | new_opname = (char *) alloca (IDENTIFIER_LENGTH (function) |
703ad42b | 3614 | + strlen (argstring) + 1 + 1); |
3e4093b6 RS |
3615 | sprintf (new_opname, argstring, |
3616 | IDENTIFIER_POINTER (function)); | |
3617 | } | |
3618 | else | |
3619 | { | |
3620 | /* Function name unknown (call through ptr). */ | |
3621 | const char *const argnofun = _("passing arg of pointer to function"); | |
28dab132 | 3622 | new_opname = (char *) alloca (strlen (argnofun) + 1 + 1); |
3e4093b6 RS |
3623 | sprintf (new_opname, argnofun); |
3624 | } | |
73a73768 | 3625 | } |
3e4093b6 | 3626 | else if (function) |
10d5caec | 3627 | { |
3e4093b6 RS |
3628 | /* Function name is known; supply it. */ |
3629 | const char *const argstring = _("passing arg %d of `%s'"); | |
28dab132 | 3630 | new_opname = (char *) alloca (IDENTIFIER_LENGTH (function) |
703ad42b | 3631 | + strlen (argstring) + 1 + 25 /*%d*/ + 1); |
3e4093b6 RS |
3632 | sprintf (new_opname, argstring, argnum, |
3633 | IDENTIFIER_POINTER (function)); | |
3634 | } | |
3635 | else | |
3636 | { | |
3637 | /* Function name unknown (call through ptr); just give arg number. */ | |
3638 | const char *const argnofun = _("passing arg %d of pointer to function"); | |
28dab132 | 3639 | new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1); |
3e4093b6 | 3640 | sprintf (new_opname, argnofun, argnum); |
10d5caec | 3641 | } |
3e4093b6 | 3642 | opname = new_opname; |
400fbf9f | 3643 | } |
3e4093b6 | 3644 | pedwarn (msgid, opname); |
400fbf9f | 3645 | } |
3e4093b6 RS |
3646 | \f |
3647 | /* If VALUE is a compound expr all of whose expressions are constant, then | |
3648 | return its value. Otherwise, return error_mark_node. | |
15b732b2 | 3649 | |
3e4093b6 RS |
3650 | This is for handling COMPOUND_EXPRs as initializer elements |
3651 | which is allowed with a warning when -pedantic is specified. */ | |
15b732b2 | 3652 | |
3e4093b6 RS |
3653 | static tree |
3654 | valid_compound_expr_initializer (tree value, tree endtype) | |
3655 | { | |
3656 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
3657 | { | |
3658 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
3659 | == error_mark_node) | |
3660 | return error_mark_node; | |
3661 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
3662 | endtype); | |
3663 | } | |
3664 | else if (! TREE_CONSTANT (value) | |
3665 | && ! initializer_constant_valid_p (value, endtype)) | |
3666 | return error_mark_node; | |
3667 | else | |
3668 | return value; | |
15b732b2 | 3669 | } |
400fbf9f | 3670 | \f |
3e4093b6 RS |
3671 | /* Perform appropriate conversions on the initial value of a variable, |
3672 | store it in the declaration DECL, | |
3673 | and print any error messages that are appropriate. | |
3674 | If the init is invalid, store an ERROR_MARK. */ | |
400fbf9f | 3675 | |
3e4093b6 RS |
3676 | void |
3677 | store_init_value (tree decl, tree init) | |
400fbf9f | 3678 | { |
3e4093b6 | 3679 | tree value, type; |
400fbf9f | 3680 | |
3e4093b6 | 3681 | /* If variable's type was invalidly declared, just ignore it. */ |
400fbf9f | 3682 | |
3e4093b6 RS |
3683 | type = TREE_TYPE (decl); |
3684 | if (TREE_CODE (type) == ERROR_MARK) | |
3685 | return; | |
400fbf9f | 3686 | |
3e4093b6 | 3687 | /* Digest the specified initializer into an expression. */ |
400fbf9f | 3688 | |
916c5919 | 3689 | value = digest_init (type, init, true, TREE_STATIC (decl)); |
400fbf9f | 3690 | |
3e4093b6 | 3691 | /* Store the expression if valid; else report error. */ |
400fbf9f | 3692 | |
3e4093b6 RS |
3693 | if (warn_traditional && !in_system_header |
3694 | && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl)) | |
3695 | warning ("traditional C rejects automatic aggregate initialization"); | |
2f6e4e97 | 3696 | |
3e4093b6 | 3697 | DECL_INITIAL (decl) = value; |
400fbf9f | 3698 | |
3e4093b6 RS |
3699 | /* ANSI wants warnings about out-of-range constant initializers. */ |
3700 | STRIP_TYPE_NOPS (value); | |
3701 | constant_expression_warning (value); | |
400fbf9f | 3702 | |
3e4093b6 RS |
3703 | /* Check if we need to set array size from compound literal size. */ |
3704 | if (TREE_CODE (type) == ARRAY_TYPE | |
3705 | && TYPE_DOMAIN (type) == 0 | |
3706 | && value != error_mark_node) | |
400fbf9f | 3707 | { |
3e4093b6 RS |
3708 | tree inside_init = init; |
3709 | ||
3710 | if (TREE_CODE (init) == NON_LVALUE_EXPR) | |
3711 | inside_init = TREE_OPERAND (init, 0); | |
3712 | inside_init = fold (inside_init); | |
3713 | ||
3714 | if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
3715 | { | |
3716 | tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); | |
3717 | ||
3718 | if (TYPE_DOMAIN (TREE_TYPE (decl))) | |
3719 | { | |
3720 | /* For int foo[] = (int [3]){1}; we need to set array size | |
3721 | now since later on array initializer will be just the | |
3722 | brace enclosed list of the compound literal. */ | |
3723 | TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl)); | |
3724 | layout_type (type); | |
3725 | layout_decl (decl, 0); | |
3726 | } | |
3727 | } | |
400fbf9f | 3728 | } |
3e4093b6 RS |
3729 | } |
3730 | \f | |
3731 | /* Methods for storing and printing names for error messages. */ | |
400fbf9f | 3732 | |
3e4093b6 RS |
3733 | /* Implement a spelling stack that allows components of a name to be pushed |
3734 | and popped. Each element on the stack is this structure. */ | |
400fbf9f | 3735 | |
3e4093b6 RS |
3736 | struct spelling |
3737 | { | |
3738 | int kind; | |
3739 | union | |
400fbf9f | 3740 | { |
3e4093b6 RS |
3741 | int i; |
3742 | const char *s; | |
3743 | } u; | |
3744 | }; | |
2f6e4e97 | 3745 | |
3e4093b6 RS |
3746 | #define SPELLING_STRING 1 |
3747 | #define SPELLING_MEMBER 2 | |
3748 | #define SPELLING_BOUNDS 3 | |
400fbf9f | 3749 | |
3e4093b6 RS |
3750 | static struct spelling *spelling; /* Next stack element (unused). */ |
3751 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
3752 | static int spelling_size; /* Size of the spelling stack. */ | |
400fbf9f | 3753 | |
3e4093b6 RS |
3754 | /* Macros to save and restore the spelling stack around push_... functions. |
3755 | Alternative to SAVE_SPELLING_STACK. */ | |
400fbf9f | 3756 | |
3e4093b6 RS |
3757 | #define SPELLING_DEPTH() (spelling - spelling_base) |
3758 | #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) | |
400fbf9f | 3759 | |
3e4093b6 RS |
3760 | /* Push an element on the spelling stack with type KIND and assign VALUE |
3761 | to MEMBER. */ | |
400fbf9f | 3762 | |
3e4093b6 RS |
3763 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ |
3764 | { \ | |
3765 | int depth = SPELLING_DEPTH (); \ | |
3766 | \ | |
3767 | if (depth >= spelling_size) \ | |
3768 | { \ | |
3769 | spelling_size += 10; \ | |
cca8ead2 BI |
3770 | spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ |
3771 | spelling_size); \ | |
3e4093b6 RS |
3772 | RESTORE_SPELLING_DEPTH (depth); \ |
3773 | } \ | |
3774 | \ | |
3775 | spelling->kind = (KIND); \ | |
3776 | spelling->MEMBER = (VALUE); \ | |
3777 | spelling++; \ | |
3778 | } | |
400fbf9f | 3779 | |
3e4093b6 | 3780 | /* Push STRING on the stack. Printed literally. */ |
400fbf9f | 3781 | |
3e4093b6 RS |
3782 | static void |
3783 | push_string (const char *string) | |
3784 | { | |
3785 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
3786 | } | |
400fbf9f | 3787 | |
3e4093b6 | 3788 | /* Push a member name on the stack. Printed as '.' STRING. */ |
400fbf9f | 3789 | |
3e4093b6 RS |
3790 | static void |
3791 | push_member_name (tree decl) | |
3792 | { | |
3793 | const char *const string | |
3794 | = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>"; | |
3795 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); | |
3796 | } | |
400fbf9f | 3797 | |
3e4093b6 | 3798 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ |
400fbf9f | 3799 | |
3e4093b6 RS |
3800 | static void |
3801 | push_array_bounds (int bounds) | |
3802 | { | |
3803 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
3804 | } | |
bb58bec5 | 3805 | |
3e4093b6 | 3806 | /* Compute the maximum size in bytes of the printed spelling. */ |
400fbf9f | 3807 | |
3e4093b6 RS |
3808 | static int |
3809 | spelling_length (void) | |
3810 | { | |
3811 | int size = 0; | |
3812 | struct spelling *p; | |
400fbf9f | 3813 | |
3e4093b6 RS |
3814 | for (p = spelling_base; p < spelling; p++) |
3815 | { | |
3816 | if (p->kind == SPELLING_BOUNDS) | |
3817 | size += 25; | |
3818 | else | |
3819 | size += strlen (p->u.s) + 1; | |
3820 | } | |
3821 | ||
3822 | return size; | |
400fbf9f | 3823 | } |
400fbf9f | 3824 | |
3e4093b6 | 3825 | /* Print the spelling to BUFFER and return it. */ |
400fbf9f | 3826 | |
3e4093b6 RS |
3827 | static char * |
3828 | print_spelling (char *buffer) | |
400fbf9f | 3829 | { |
3e4093b6 RS |
3830 | char *d = buffer; |
3831 | struct spelling *p; | |
400fbf9f | 3832 | |
3e4093b6 RS |
3833 | for (p = spelling_base; p < spelling; p++) |
3834 | if (p->kind == SPELLING_BOUNDS) | |
3835 | { | |
3836 | sprintf (d, "[%d]", p->u.i); | |
3837 | d += strlen (d); | |
3838 | } | |
3839 | else | |
3840 | { | |
3841 | const char *s; | |
3842 | if (p->kind == SPELLING_MEMBER) | |
3843 | *d++ = '.'; | |
3844 | for (s = p->u.s; (*d = *s++); d++) | |
3845 | ; | |
3846 | } | |
3847 | *d++ = '\0'; | |
3848 | return buffer; | |
3849 | } | |
400fbf9f | 3850 | |
3e4093b6 RS |
3851 | /* Issue an error message for a bad initializer component. |
3852 | MSGID identifies the message. | |
3853 | The component name is taken from the spelling stack. */ | |
400fbf9f | 3854 | |
3e4093b6 RS |
3855 | void |
3856 | error_init (const char *msgid) | |
3857 | { | |
3858 | char *ofwhat; | |
400fbf9f | 3859 | |
3e4093b6 | 3860 | error ("%s", _(msgid)); |
28dab132 | 3861 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 RS |
3862 | if (*ofwhat) |
3863 | error ("(near initialization for `%s')", ofwhat); | |
3864 | } | |
400fbf9f | 3865 | |
3e4093b6 RS |
3866 | /* Issue a pedantic warning for a bad initializer component. |
3867 | MSGID identifies the message. | |
3868 | The component name is taken from the spelling stack. */ | |
400fbf9f | 3869 | |
3e4093b6 RS |
3870 | void |
3871 | pedwarn_init (const char *msgid) | |
3872 | { | |
3873 | char *ofwhat; | |
9f720c3e | 3874 | |
3e4093b6 | 3875 | pedwarn ("%s", _(msgid)); |
28dab132 | 3876 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 RS |
3877 | if (*ofwhat) |
3878 | pedwarn ("(near initialization for `%s')", ofwhat); | |
3879 | } | |
9f720c3e | 3880 | |
3e4093b6 RS |
3881 | /* Issue a warning for a bad initializer component. |
3882 | MSGID identifies the message. | |
3883 | The component name is taken from the spelling stack. */ | |
61179109 | 3884 | |
3e4093b6 RS |
3885 | static void |
3886 | warning_init (const char *msgid) | |
3887 | { | |
3888 | char *ofwhat; | |
7e842ef8 | 3889 | |
3e4093b6 | 3890 | warning ("%s", _(msgid)); |
28dab132 | 3891 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 RS |
3892 | if (*ofwhat) |
3893 | warning ("(near initialization for `%s')", ofwhat); | |
3894 | } | |
3895 | \f | |
916c5919 JM |
3896 | /* If TYPE is an array type and EXPR is a parenthesized string |
3897 | constant, warn if pedantic that EXPR is being used to initialize an | |
3898 | object of type TYPE. */ | |
3899 | ||
3900 | void | |
3901 | maybe_warn_string_init (tree type, struct c_expr expr) | |
3902 | { | |
3903 | if (pedantic | |
3904 | && TREE_CODE (type) == ARRAY_TYPE | |
3905 | && TREE_CODE (expr.value) == STRING_CST | |
3906 | && expr.original_code != STRING_CST) | |
3907 | pedwarn_init ("array initialized from parenthesized string constant"); | |
3908 | } | |
3909 | ||
3e4093b6 RS |
3910 | /* Digest the parser output INIT as an initializer for type TYPE. |
3911 | Return a C expression of type TYPE to represent the initial value. | |
7e842ef8 | 3912 | |
916c5919 JM |
3913 | If INIT is a string constant, STRICT_STRING is true if it is |
3914 | unparenthesized or we should not warn here for it being parenthesized. | |
3915 | For other types of INIT, STRICT_STRING is not used. | |
3916 | ||
3e4093b6 RS |
3917 | REQUIRE_CONSTANT requests an error if non-constant initializers or |
3918 | elements are seen. */ | |
7e842ef8 | 3919 | |
3e4093b6 | 3920 | static tree |
916c5919 | 3921 | digest_init (tree type, tree init, bool strict_string, int require_constant) |
3e4093b6 RS |
3922 | { |
3923 | enum tree_code code = TREE_CODE (type); | |
3924 | tree inside_init = init; | |
7e842ef8 | 3925 | |
3e4093b6 RS |
3926 | if (type == error_mark_node |
3927 | || init == error_mark_node | |
3928 | || TREE_TYPE (init) == error_mark_node) | |
3929 | return error_mark_node; | |
7e842ef8 | 3930 | |
3e4093b6 RS |
3931 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ |
3932 | /* Do not use STRIP_NOPS here. We do not want an enumerator | |
3933 | whose value is 0 to count as a null pointer constant. */ | |
3934 | if (TREE_CODE (init) == NON_LVALUE_EXPR) | |
3935 | inside_init = TREE_OPERAND (init, 0); | |
7e842ef8 | 3936 | |
3e4093b6 | 3937 | inside_init = fold (inside_init); |
7e842ef8 | 3938 | |
3e4093b6 RS |
3939 | /* Initialization of an array of chars from a string constant |
3940 | optionally enclosed in braces. */ | |
7e842ef8 | 3941 | |
197463ae JM |
3942 | if (code == ARRAY_TYPE && inside_init |
3943 | && TREE_CODE (inside_init) == STRING_CST) | |
3e4093b6 RS |
3944 | { |
3945 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
197463ae JM |
3946 | /* Note that an array could be both an array of character type |
3947 | and an array of wchar_t if wchar_t is signed char or unsigned | |
3948 | char. */ | |
3949 | bool char_array = (typ1 == char_type_node | |
3950 | || typ1 == signed_char_type_node | |
3951 | || typ1 == unsigned_char_type_node); | |
3952 | bool wchar_array = !!comptypes (typ1, wchar_type_node); | |
3953 | if (char_array || wchar_array) | |
7e842ef8 | 3954 | { |
916c5919 | 3955 | struct c_expr expr; |
197463ae | 3956 | bool char_string; |
916c5919 JM |
3957 | expr.value = inside_init; |
3958 | expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); | |
3959 | maybe_warn_string_init (type, expr); | |
3960 | ||
197463ae JM |
3961 | char_string |
3962 | = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))) | |
3963 | == char_type_node); | |
3964 | ||
3e4093b6 | 3965 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
132da1a5 | 3966 | TYPE_MAIN_VARIANT (type))) |
3e4093b6 | 3967 | return inside_init; |
7e842ef8 | 3968 | |
197463ae | 3969 | if (!wchar_array && !char_string) |
3e4093b6 RS |
3970 | { |
3971 | error_init ("char-array initialized from wide string"); | |
3972 | return error_mark_node; | |
3973 | } | |
197463ae | 3974 | if (char_string && !char_array) |
3e4093b6 | 3975 | { |
197463ae | 3976 | error_init ("wchar_t-array initialized from non-wide string"); |
3e4093b6 | 3977 | return error_mark_node; |
7e842ef8 | 3978 | } |
2f6e4e97 | 3979 | |
3e4093b6 RS |
3980 | TREE_TYPE (inside_init) = type; |
3981 | if (TYPE_DOMAIN (type) != 0 | |
3982 | && TYPE_SIZE (type) != 0 | |
3983 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST | |
3984 | /* Subtract 1 (or sizeof (wchar_t)) | |
3985 | because it's ok to ignore the terminating null char | |
3986 | that is counted in the length of the constant. */ | |
3987 | && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), | |
3988 | TREE_STRING_LENGTH (inside_init) | |
3989 | - ((TYPE_PRECISION (typ1) | |
3990 | != TYPE_PRECISION (char_type_node)) | |
3991 | ? (TYPE_PRECISION (wchar_type_node) | |
3992 | / BITS_PER_UNIT) | |
3993 | : 1))) | |
3994 | pedwarn_init ("initializer-string for array of chars is too long"); | |
7e842ef8 | 3995 | |
3e4093b6 | 3996 | return inside_init; |
7e842ef8 | 3997 | } |
197463ae JM |
3998 | else if (INTEGRAL_TYPE_P (typ1)) |
3999 | { | |
4000 | error_init ("array of inappropriate type initialized " | |
4001 | "from string constant"); | |
4002 | return error_mark_node; | |
4003 | } | |
7e842ef8 PE |
4004 | } |
4005 | ||
3e4093b6 RS |
4006 | /* Build a VECTOR_CST from a *constant* vector constructor. If the |
4007 | vector constructor is not constant (e.g. {1,2,3,foo()}) then punt | |
4008 | below and handle as a constructor. */ | |
4009 | if (code == VECTOR_TYPE | |
cc27e657 | 4010 | && vector_types_convertible_p (TREE_TYPE (inside_init), type) |
3e4093b6 RS |
4011 | && TREE_CONSTANT (inside_init)) |
4012 | { | |
4013 | if (TREE_CODE (inside_init) == VECTOR_CST | |
cc27e657 | 4014 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
132da1a5 | 4015 | TYPE_MAIN_VARIANT (type))) |
3e4093b6 RS |
4016 | return inside_init; |
4017 | else | |
4018 | return build_vector (type, CONSTRUCTOR_ELTS (inside_init)); | |
4019 | } | |
6035d635 | 4020 | |
3e4093b6 RS |
4021 | /* Any type can be initialized |
4022 | from an expression of the same type, optionally with braces. */ | |
400fbf9f | 4023 | |
3e4093b6 RS |
4024 | if (inside_init && TREE_TYPE (inside_init) != 0 |
4025 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
132da1a5 | 4026 | TYPE_MAIN_VARIANT (type)) |
3e4093b6 | 4027 | || (code == ARRAY_TYPE |
132da1a5 | 4028 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 4029 | || (code == VECTOR_TYPE |
132da1a5 | 4030 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 4031 | || (code == POINTER_TYPE |
3897f229 | 4032 | && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
3e4093b6 | 4033 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), |
132da1a5 | 4034 | TREE_TYPE (type))) |
3897f229 JM |
4035 | || (code == POINTER_TYPE |
4036 | && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE | |
4037 | && comptypes (TREE_TYPE (inside_init), | |
132da1a5 | 4038 | TREE_TYPE (type))))) |
3e4093b6 RS |
4039 | { |
4040 | if (code == POINTER_TYPE) | |
b494fd98 EB |
4041 | { |
4042 | inside_init = default_function_array_conversion (inside_init); | |
4043 | ||
4044 | if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) | |
4045 | { | |
4046 | error_init ("invalid use of non-lvalue array"); | |
4047 | return error_mark_node; | |
4048 | } | |
4049 | } | |
4050 | ||
bae39a73 NS |
4051 | if (code == VECTOR_TYPE) |
4052 | /* Although the types are compatible, we may require a | |
4053 | conversion. */ | |
4054 | inside_init = convert (type, inside_init); | |
3e4093b6 RS |
4055 | |
4056 | if (require_constant && !flag_isoc99 | |
4057 | && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
400fbf9f | 4058 | { |
3e4093b6 RS |
4059 | /* As an extension, allow initializing objects with static storage |
4060 | duration with compound literals (which are then treated just as | |
4061 | the brace enclosed list they contain). */ | |
4062 | tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); | |
4063 | inside_init = DECL_INITIAL (decl); | |
400fbf9f | 4064 | } |
3e4093b6 RS |
4065 | |
4066 | if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST | |
4067 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
400fbf9f | 4068 | { |
3e4093b6 RS |
4069 | error_init ("array initialized from non-constant array expression"); |
4070 | return error_mark_node; | |
400fbf9f | 4071 | } |
400fbf9f | 4072 | |
3e4093b6 RS |
4073 | if (optimize && TREE_CODE (inside_init) == VAR_DECL) |
4074 | inside_init = decl_constant_value_for_broken_optimization (inside_init); | |
2f6e4e97 | 4075 | |
3e4093b6 RS |
4076 | /* Compound expressions can only occur here if -pedantic or |
4077 | -pedantic-errors is specified. In the later case, we always want | |
4078 | an error. In the former case, we simply want a warning. */ | |
4079 | if (require_constant && pedantic | |
4080 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
4081 | { | |
4082 | inside_init | |
4083 | = valid_compound_expr_initializer (inside_init, | |
4084 | TREE_TYPE (inside_init)); | |
4085 | if (inside_init == error_mark_node) | |
4086 | error_init ("initializer element is not constant"); | |
2f6e4e97 | 4087 | else |
3e4093b6 RS |
4088 | pedwarn_init ("initializer element is not constant"); |
4089 | if (flag_pedantic_errors) | |
4090 | inside_init = error_mark_node; | |
4091 | } | |
4092 | else if (require_constant | |
4093 | && (!TREE_CONSTANT (inside_init) | |
4094 | /* This test catches things like `7 / 0' which | |
4095 | result in an expression for which TREE_CONSTANT | |
4096 | is true, but which is not actually something | |
4097 | that is a legal constant. We really should not | |
4098 | be using this function, because it is a part of | |
4099 | the back-end. Instead, the expression should | |
4100 | already have been turned into ERROR_MARK_NODE. */ | |
4101 | || !initializer_constant_valid_p (inside_init, | |
4102 | TREE_TYPE (inside_init)))) | |
4103 | { | |
4104 | error_init ("initializer element is not constant"); | |
4105 | inside_init = error_mark_node; | |
8b40563c | 4106 | } |
f735a153 | 4107 | |
3e4093b6 RS |
4108 | return inside_init; |
4109 | } | |
f735a153 | 4110 | |
3e4093b6 | 4111 | /* Handle scalar types, including conversions. */ |
400fbf9f | 4112 | |
3e4093b6 | 4113 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE |
cc27e657 PB |
4114 | || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE |
4115 | || code == VECTOR_TYPE) | |
400fbf9f | 4116 | { |
3e4093b6 RS |
4117 | /* Note that convert_for_assignment calls default_conversion |
4118 | for arrays and functions. We must not call it in the | |
4119 | case where inside_init is a null pointer constant. */ | |
4120 | inside_init | |
4121 | = convert_for_assignment (type, init, _("initialization"), | |
4122 | NULL_TREE, NULL_TREE, 0); | |
2f6e4e97 | 4123 | |
3e4093b6 | 4124 | if (require_constant && ! TREE_CONSTANT (inside_init)) |
400fbf9f | 4125 | { |
3e4093b6 RS |
4126 | error_init ("initializer element is not constant"); |
4127 | inside_init = error_mark_node; | |
400fbf9f | 4128 | } |
3e4093b6 RS |
4129 | else if (require_constant |
4130 | && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0) | |
400fbf9f | 4131 | { |
3e4093b6 RS |
4132 | error_init ("initializer element is not computable at load time"); |
4133 | inside_init = error_mark_node; | |
400fbf9f | 4134 | } |
3e4093b6 RS |
4135 | |
4136 | return inside_init; | |
400fbf9f | 4137 | } |
d9fc6069 | 4138 | |
3e4093b6 | 4139 | /* Come here only for records and arrays. */ |
d9fc6069 | 4140 | |
3e4093b6 | 4141 | if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) |
d9fc6069 | 4142 | { |
3e4093b6 RS |
4143 | error_init ("variable-sized object may not be initialized"); |
4144 | return error_mark_node; | |
d9fc6069 | 4145 | } |
3e4093b6 RS |
4146 | |
4147 | error_init ("invalid initializer"); | |
4148 | return error_mark_node; | |
d9fc6069 | 4149 | } |
400fbf9f | 4150 | \f |
3e4093b6 | 4151 | /* Handle initializers that use braces. */ |
400fbf9f | 4152 | |
3e4093b6 RS |
4153 | /* Type of object we are accumulating a constructor for. |
4154 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
4155 | static tree constructor_type; | |
400fbf9f | 4156 | |
3e4093b6 RS |
4157 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields |
4158 | left to fill. */ | |
4159 | static tree constructor_fields; | |
400fbf9f | 4160 | |
3e4093b6 RS |
4161 | /* For an ARRAY_TYPE, this is the specified index |
4162 | at which to store the next element we get. */ | |
4163 | static tree constructor_index; | |
400fbf9f | 4164 | |
3e4093b6 RS |
4165 | /* For an ARRAY_TYPE, this is the maximum index. */ |
4166 | static tree constructor_max_index; | |
400fbf9f | 4167 | |
3e4093b6 RS |
4168 | /* For a RECORD_TYPE, this is the first field not yet written out. */ |
4169 | static tree constructor_unfilled_fields; | |
400fbf9f | 4170 | |
3e4093b6 RS |
4171 | /* For an ARRAY_TYPE, this is the index of the first element |
4172 | not yet written out. */ | |
4173 | static tree constructor_unfilled_index; | |
895ea614 | 4174 | |
3e4093b6 RS |
4175 | /* In a RECORD_TYPE, the byte index of the next consecutive field. |
4176 | This is so we can generate gaps between fields, when appropriate. */ | |
4177 | static tree constructor_bit_index; | |
10d5caec | 4178 | |
3e4093b6 RS |
4179 | /* If we are saving up the elements rather than allocating them, |
4180 | this is the list of elements so far (in reverse order, | |
4181 | most recent first). */ | |
4182 | static tree constructor_elements; | |
ad47f1e5 | 4183 | |
3e4093b6 RS |
4184 | /* 1 if constructor should be incrementally stored into a constructor chain, |
4185 | 0 if all the elements should be kept in AVL tree. */ | |
4186 | static int constructor_incremental; | |
ad47f1e5 | 4187 | |
3e4093b6 RS |
4188 | /* 1 if so far this constructor's elements are all compile-time constants. */ |
4189 | static int constructor_constant; | |
ad47f1e5 | 4190 | |
3e4093b6 RS |
4191 | /* 1 if so far this constructor's elements are all valid address constants. */ |
4192 | static int constructor_simple; | |
ad47f1e5 | 4193 | |
3e4093b6 RS |
4194 | /* 1 if this constructor is erroneous so far. */ |
4195 | static int constructor_erroneous; | |
d45cf215 | 4196 | |
3e4093b6 RS |
4197 | /* Structure for managing pending initializer elements, organized as an |
4198 | AVL tree. */ | |
d45cf215 | 4199 | |
3e4093b6 | 4200 | struct init_node |
d45cf215 | 4201 | { |
3e4093b6 RS |
4202 | struct init_node *left, *right; |
4203 | struct init_node *parent; | |
4204 | int balance; | |
4205 | tree purpose; | |
4206 | tree value; | |
d45cf215 RS |
4207 | }; |
4208 | ||
3e4093b6 RS |
4209 | /* Tree of pending elements at this constructor level. |
4210 | These are elements encountered out of order | |
4211 | which belong at places we haven't reached yet in actually | |
4212 | writing the output. | |
4213 | Will never hold tree nodes across GC runs. */ | |
4214 | static struct init_node *constructor_pending_elts; | |
d45cf215 | 4215 | |
3e4093b6 RS |
4216 | /* The SPELLING_DEPTH of this constructor. */ |
4217 | static int constructor_depth; | |
d45cf215 | 4218 | |
3e4093b6 RS |
4219 | /* 0 if implicitly pushing constructor levels is allowed. */ |
4220 | int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */ | |
d45cf215 | 4221 | |
3e4093b6 RS |
4222 | /* DECL node for which an initializer is being read. |
4223 | 0 means we are reading a constructor expression | |
4224 | such as (struct foo) {...}. */ | |
4225 | static tree constructor_decl; | |
d45cf215 | 4226 | |
839ee4bc RO |
4227 | /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */ |
4228 | static const char *constructor_asmspec; | |
4229 | ||
3e4093b6 RS |
4230 | /* Nonzero if this is an initializer for a top-level decl. */ |
4231 | static int constructor_top_level; | |
d45cf215 | 4232 | |
3e4093b6 RS |
4233 | /* Nonzero if there were any member designators in this initializer. */ |
4234 | static int constructor_designated; | |
d45cf215 | 4235 | |
3e4093b6 RS |
4236 | /* Nesting depth of designator list. */ |
4237 | static int designator_depth; | |
d45cf215 | 4238 | |
3e4093b6 RS |
4239 | /* Nonzero if there were diagnosed errors in this designator list. */ |
4240 | static int designator_errorneous; | |
d45cf215 | 4241 | |
3e4093b6 RS |
4242 | \f |
4243 | /* This stack has a level for each implicit or explicit level of | |
4244 | structuring in the initializer, including the outermost one. It | |
4245 | saves the values of most of the variables above. */ | |
d45cf215 | 4246 | |
3e4093b6 RS |
4247 | struct constructor_range_stack; |
4248 | ||
4249 | struct constructor_stack | |
d45cf215 | 4250 | { |
3e4093b6 RS |
4251 | struct constructor_stack *next; |
4252 | tree type; | |
4253 | tree fields; | |
4254 | tree index; | |
4255 | tree max_index; | |
4256 | tree unfilled_index; | |
4257 | tree unfilled_fields; | |
4258 | tree bit_index; | |
4259 | tree elements; | |
4260 | struct init_node *pending_elts; | |
4261 | int offset; | |
4262 | int depth; | |
916c5919 | 4263 | /* If value nonzero, this value should replace the entire |
3e4093b6 | 4264 | constructor at this level. */ |
916c5919 | 4265 | struct c_expr replacement_value; |
3e4093b6 RS |
4266 | struct constructor_range_stack *range_stack; |
4267 | char constant; | |
4268 | char simple; | |
4269 | char implicit; | |
4270 | char erroneous; | |
4271 | char outer; | |
4272 | char incremental; | |
4273 | char designated; | |
4274 | }; | |
d45cf215 | 4275 | |
3e4093b6 | 4276 | struct constructor_stack *constructor_stack; |
d45cf215 | 4277 | |
3e4093b6 RS |
4278 | /* This stack represents designators from some range designator up to |
4279 | the last designator in the list. */ | |
d45cf215 | 4280 | |
3e4093b6 RS |
4281 | struct constructor_range_stack |
4282 | { | |
4283 | struct constructor_range_stack *next, *prev; | |
4284 | struct constructor_stack *stack; | |
4285 | tree range_start; | |
4286 | tree index; | |
4287 | tree range_end; | |
4288 | tree fields; | |
4289 | }; | |
d45cf215 | 4290 | |
3e4093b6 | 4291 | struct constructor_range_stack *constructor_range_stack; |
d45cf215 | 4292 | |
3e4093b6 RS |
4293 | /* This stack records separate initializers that are nested. |
4294 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
4295 | in cases like { ... (struct foo) { ... } ... }. */ | |
d45cf215 | 4296 | |
3e4093b6 | 4297 | struct initializer_stack |
d45cf215 | 4298 | { |
3e4093b6 RS |
4299 | struct initializer_stack *next; |
4300 | tree decl; | |
839ee4bc | 4301 | const char *asmspec; |
3e4093b6 RS |
4302 | struct constructor_stack *constructor_stack; |
4303 | struct constructor_range_stack *constructor_range_stack; | |
4304 | tree elements; | |
4305 | struct spelling *spelling; | |
4306 | struct spelling *spelling_base; | |
4307 | int spelling_size; | |
4308 | char top_level; | |
4309 | char require_constant_value; | |
4310 | char require_constant_elements; | |
4311 | }; | |
d45cf215 | 4312 | |
3e4093b6 RS |
4313 | struct initializer_stack *initializer_stack; |
4314 | \f | |
4315 | /* Prepare to parse and output the initializer for variable DECL. */ | |
400fbf9f JW |
4316 | |
4317 | void | |
839ee4bc | 4318 | start_init (tree decl, tree asmspec_tree, int top_level) |
400fbf9f | 4319 | { |
3e4093b6 | 4320 | const char *locus; |
5d038c4c | 4321 | struct initializer_stack *p = XNEW (struct initializer_stack); |
839ee4bc RO |
4322 | const char *asmspec = 0; |
4323 | ||
4324 | if (asmspec_tree) | |
4325 | asmspec = TREE_STRING_POINTER (asmspec_tree); | |
400fbf9f | 4326 | |
3e4093b6 | 4327 | p->decl = constructor_decl; |
839ee4bc | 4328 | p->asmspec = constructor_asmspec; |
3e4093b6 RS |
4329 | p->require_constant_value = require_constant_value; |
4330 | p->require_constant_elements = require_constant_elements; | |
4331 | p->constructor_stack = constructor_stack; | |
4332 | p->constructor_range_stack = constructor_range_stack; | |
4333 | p->elements = constructor_elements; | |
4334 | p->spelling = spelling; | |
4335 | p->spelling_base = spelling_base; | |
4336 | p->spelling_size = spelling_size; | |
4337 | p->top_level = constructor_top_level; | |
4338 | p->next = initializer_stack; | |
4339 | initializer_stack = p; | |
400fbf9f | 4340 | |
3e4093b6 | 4341 | constructor_decl = decl; |
839ee4bc | 4342 | constructor_asmspec = asmspec; |
3e4093b6 RS |
4343 | constructor_designated = 0; |
4344 | constructor_top_level = top_level; | |
400fbf9f | 4345 | |
3e4093b6 RS |
4346 | if (decl != 0) |
4347 | { | |
4348 | require_constant_value = TREE_STATIC (decl); | |
4349 | require_constant_elements | |
4350 | = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99)) | |
4351 | /* For a scalar, you can always use any value to initialize, | |
4352 | even within braces. */ | |
4353 | && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
4354 | || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
4355 | || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE | |
4356 | || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); | |
4357 | locus = IDENTIFIER_POINTER (DECL_NAME (decl)); | |
4358 | } | |
4359 | else | |
4360 | { | |
4361 | require_constant_value = 0; | |
4362 | require_constant_elements = 0; | |
4363 | locus = "(anonymous)"; | |
4364 | } | |
b71c7f8a | 4365 | |
3e4093b6 RS |
4366 | constructor_stack = 0; |
4367 | constructor_range_stack = 0; | |
b71c7f8a | 4368 | |
3e4093b6 RS |
4369 | missing_braces_mentioned = 0; |
4370 | ||
4371 | spelling_base = 0; | |
4372 | spelling_size = 0; | |
4373 | RESTORE_SPELLING_DEPTH (0); | |
4374 | ||
4375 | if (locus) | |
4376 | push_string (locus); | |
4377 | } | |
4378 | ||
4379 | void | |
4380 | finish_init (void) | |
b71c7f8a | 4381 | { |
3e4093b6 | 4382 | struct initializer_stack *p = initializer_stack; |
b71c7f8a | 4383 | |
3e4093b6 RS |
4384 | /* Free the whole constructor stack of this initializer. */ |
4385 | while (constructor_stack) | |
4386 | { | |
4387 | struct constructor_stack *q = constructor_stack; | |
4388 | constructor_stack = q->next; | |
4389 | free (q); | |
4390 | } | |
4391 | ||
4392 | if (constructor_range_stack) | |
4393 | abort (); | |
4394 | ||
4395 | /* Pop back to the data of the outer initializer (if any). */ | |
36579663 | 4396 | free (spelling_base); |
3aeb3655 | 4397 | |
3e4093b6 | 4398 | constructor_decl = p->decl; |
839ee4bc | 4399 | constructor_asmspec = p->asmspec; |
3e4093b6 RS |
4400 | require_constant_value = p->require_constant_value; |
4401 | require_constant_elements = p->require_constant_elements; | |
4402 | constructor_stack = p->constructor_stack; | |
4403 | constructor_range_stack = p->constructor_range_stack; | |
4404 | constructor_elements = p->elements; | |
4405 | spelling = p->spelling; | |
4406 | spelling_base = p->spelling_base; | |
4407 | spelling_size = p->spelling_size; | |
4408 | constructor_top_level = p->top_level; | |
4409 | initializer_stack = p->next; | |
4410 | free (p); | |
b71c7f8a | 4411 | } |
400fbf9f | 4412 | \f |
3e4093b6 RS |
4413 | /* Call here when we see the initializer is surrounded by braces. |
4414 | This is instead of a call to push_init_level; | |
4415 | it is matched by a call to pop_init_level. | |
400fbf9f | 4416 | |
3e4093b6 RS |
4417 | TYPE is the type to initialize, for a constructor expression. |
4418 | For an initializer for a decl, TYPE is zero. */ | |
400fbf9f | 4419 | |
3e4093b6 RS |
4420 | void |
4421 | really_start_incremental_init (tree type) | |
400fbf9f | 4422 | { |
5d038c4c | 4423 | struct constructor_stack *p = XNEW (struct constructor_stack); |
400fbf9f | 4424 | |
3e4093b6 RS |
4425 | if (type == 0) |
4426 | type = TREE_TYPE (constructor_decl); | |
400fbf9f | 4427 | |
5fd9b178 | 4428 | if (targetm.vector_opaque_p (type)) |
3e4093b6 | 4429 | error ("opaque vector types cannot be initialized"); |
400fbf9f | 4430 | |
3e4093b6 RS |
4431 | p->type = constructor_type; |
4432 | p->fields = constructor_fields; | |
4433 | p->index = constructor_index; | |
4434 | p->max_index = constructor_max_index; | |
4435 | p->unfilled_index = constructor_unfilled_index; | |
4436 | p->unfilled_fields = constructor_unfilled_fields; | |
4437 | p->bit_index = constructor_bit_index; | |
4438 | p->elements = constructor_elements; | |
4439 | p->constant = constructor_constant; | |
4440 | p->simple = constructor_simple; | |
4441 | p->erroneous = constructor_erroneous; | |
4442 | p->pending_elts = constructor_pending_elts; | |
4443 | p->depth = constructor_depth; | |
916c5919 JM |
4444 | p->replacement_value.value = 0; |
4445 | p->replacement_value.original_code = ERROR_MARK; | |
3e4093b6 RS |
4446 | p->implicit = 0; |
4447 | p->range_stack = 0; | |
4448 | p->outer = 0; | |
4449 | p->incremental = constructor_incremental; | |
4450 | p->designated = constructor_designated; | |
4451 | p->next = 0; | |
4452 | constructor_stack = p; | |
b13aca19 | 4453 | |
3e4093b6 RS |
4454 | constructor_constant = 1; |
4455 | constructor_simple = 1; | |
4456 | constructor_depth = SPELLING_DEPTH (); | |
4457 | constructor_elements = 0; | |
4458 | constructor_pending_elts = 0; | |
4459 | constructor_type = type; | |
4460 | constructor_incremental = 1; | |
4461 | constructor_designated = 0; | |
4462 | designator_depth = 0; | |
4463 | designator_errorneous = 0; | |
400fbf9f | 4464 | |
3e4093b6 RS |
4465 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
4466 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
400fbf9f | 4467 | { |
3e4093b6 RS |
4468 | constructor_fields = TYPE_FIELDS (constructor_type); |
4469 | /* Skip any nameless bit fields at the beginning. */ | |
4470 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
4471 | && DECL_NAME (constructor_fields) == 0) | |
4472 | constructor_fields = TREE_CHAIN (constructor_fields); | |
05bccae2 | 4473 | |
3e4093b6 RS |
4474 | constructor_unfilled_fields = constructor_fields; |
4475 | constructor_bit_index = bitsize_zero_node; | |
400fbf9f | 4476 | } |
3e4093b6 RS |
4477 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
4478 | { | |
4479 | if (TYPE_DOMAIN (constructor_type)) | |
4480 | { | |
4481 | constructor_max_index | |
4482 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 4483 | |
3e4093b6 RS |
4484 | /* Detect non-empty initializations of zero-length arrays. */ |
4485 | if (constructor_max_index == NULL_TREE | |
4486 | && TYPE_SIZE (constructor_type)) | |
4a90aeeb | 4487 | constructor_max_index = build_int_cst (NULL_TREE, -1, -1); |
400fbf9f | 4488 | |
3e4093b6 RS |
4489 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
4490 | to initialize VLAs will cause a proper error; avoid tree | |
4491 | checking errors as well by setting a safe value. */ | |
4492 | if (constructor_max_index | |
4493 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
4a90aeeb | 4494 | constructor_max_index = build_int_cst (NULL_TREE, -1, -1); |
59c83dbf | 4495 | |
3e4093b6 RS |
4496 | constructor_index |
4497 | = convert (bitsizetype, | |
4498 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
59c83dbf | 4499 | } |
3e4093b6 RS |
4500 | else |
4501 | constructor_index = bitsize_zero_node; | |
59c83dbf | 4502 | |
3e4093b6 RS |
4503 | constructor_unfilled_index = constructor_index; |
4504 | } | |
4505 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
4506 | { | |
4507 | /* Vectors are like simple fixed-size arrays. */ | |
4508 | constructor_max_index = | |
4a90aeeb NS |
4509 | build_int_cst (NULL_TREE, |
4510 | TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0); | |
3e4093b6 RS |
4511 | constructor_index = convert (bitsizetype, bitsize_zero_node); |
4512 | constructor_unfilled_index = constructor_index; | |
4513 | } | |
4514 | else | |
4515 | { | |
4516 | /* Handle the case of int x = {5}; */ | |
4517 | constructor_fields = constructor_type; | |
4518 | constructor_unfilled_fields = constructor_type; | |
4519 | } | |
4520 | } | |
4521 | \f | |
4522 | /* Push down into a subobject, for initialization. | |
4523 | If this is for an explicit set of braces, IMPLICIT is 0. | |
4524 | If it is because the next element belongs at a lower level, | |
4525 | IMPLICIT is 1 (or 2 if the push is because of designator list). */ | |
400fbf9f | 4526 | |
3e4093b6 RS |
4527 | void |
4528 | push_init_level (int implicit) | |
4529 | { | |
4530 | struct constructor_stack *p; | |
4531 | tree value = NULL_TREE; | |
400fbf9f | 4532 | |
3e4093b6 RS |
4533 | /* If we've exhausted any levels that didn't have braces, |
4534 | pop them now. */ | |
4535 | while (constructor_stack->implicit) | |
4536 | { | |
4537 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
4538 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
4539 | && constructor_fields == 0) | |
4540 | process_init_element (pop_init_level (1)); | |
4541 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
4542 | && constructor_max_index | |
4543 | && tree_int_cst_lt (constructor_max_index, constructor_index)) | |
4544 | process_init_element (pop_init_level (1)); | |
4545 | else | |
4546 | break; | |
4547 | } | |
400fbf9f | 4548 | |
3e4093b6 RS |
4549 | /* Unless this is an explicit brace, we need to preserve previous |
4550 | content if any. */ | |
4551 | if (implicit) | |
4552 | { | |
4553 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
4554 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
4555 | && constructor_fields) | |
4556 | value = find_init_member (constructor_fields); | |
4557 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
4558 | value = find_init_member (constructor_index); | |
400fbf9f JW |
4559 | } |
4560 | ||
5d038c4c | 4561 | p = XNEW (struct constructor_stack); |
3e4093b6 RS |
4562 | p->type = constructor_type; |
4563 | p->fields = constructor_fields; | |
4564 | p->index = constructor_index; | |
4565 | p->max_index = constructor_max_index; | |
4566 | p->unfilled_index = constructor_unfilled_index; | |
4567 | p->unfilled_fields = constructor_unfilled_fields; | |
4568 | p->bit_index = constructor_bit_index; | |
4569 | p->elements = constructor_elements; | |
4570 | p->constant = constructor_constant; | |
4571 | p->simple = constructor_simple; | |
4572 | p->erroneous = constructor_erroneous; | |
4573 | p->pending_elts = constructor_pending_elts; | |
4574 | p->depth = constructor_depth; | |
916c5919 JM |
4575 | p->replacement_value.value = 0; |
4576 | p->replacement_value.original_code = ERROR_MARK; | |
3e4093b6 RS |
4577 | p->implicit = implicit; |
4578 | p->outer = 0; | |
4579 | p->incremental = constructor_incremental; | |
4580 | p->designated = constructor_designated; | |
4581 | p->next = constructor_stack; | |
4582 | p->range_stack = 0; | |
4583 | constructor_stack = p; | |
400fbf9f | 4584 | |
3e4093b6 RS |
4585 | constructor_constant = 1; |
4586 | constructor_simple = 1; | |
4587 | constructor_depth = SPELLING_DEPTH (); | |
4588 | constructor_elements = 0; | |
4589 | constructor_incremental = 1; | |
4590 | constructor_designated = 0; | |
4591 | constructor_pending_elts = 0; | |
4592 | if (!implicit) | |
400fbf9f | 4593 | { |
3e4093b6 RS |
4594 | p->range_stack = constructor_range_stack; |
4595 | constructor_range_stack = 0; | |
4596 | designator_depth = 0; | |
4597 | designator_errorneous = 0; | |
4598 | } | |
400fbf9f | 4599 | |
3e4093b6 RS |
4600 | /* Don't die if an entire brace-pair level is superfluous |
4601 | in the containing level. */ | |
4602 | if (constructor_type == 0) | |
4603 | ; | |
4604 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
4605 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
4606 | { | |
4607 | /* Don't die if there are extra init elts at the end. */ | |
4608 | if (constructor_fields == 0) | |
4609 | constructor_type = 0; | |
4610 | else | |
400fbf9f | 4611 | { |
3e4093b6 RS |
4612 | constructor_type = TREE_TYPE (constructor_fields); |
4613 | push_member_name (constructor_fields); | |
4614 | constructor_depth++; | |
400fbf9f | 4615 | } |
3e4093b6 RS |
4616 | } |
4617 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
4618 | { | |
4619 | constructor_type = TREE_TYPE (constructor_type); | |
4620 | push_array_bounds (tree_low_cst (constructor_index, 0)); | |
4621 | constructor_depth++; | |
400fbf9f JW |
4622 | } |
4623 | ||
3e4093b6 | 4624 | if (constructor_type == 0) |
400fbf9f | 4625 | { |
3e4093b6 RS |
4626 | error_init ("extra brace group at end of initializer"); |
4627 | constructor_fields = 0; | |
4628 | constructor_unfilled_fields = 0; | |
4629 | return; | |
400fbf9f JW |
4630 | } |
4631 | ||
3e4093b6 RS |
4632 | if (value && TREE_CODE (value) == CONSTRUCTOR) |
4633 | { | |
4634 | constructor_constant = TREE_CONSTANT (value); | |
4635 | constructor_simple = TREE_STATIC (value); | |
4636 | constructor_elements = CONSTRUCTOR_ELTS (value); | |
4637 | if (constructor_elements | |
4638 | && (TREE_CODE (constructor_type) == RECORD_TYPE | |
4639 | || TREE_CODE (constructor_type) == ARRAY_TYPE)) | |
4640 | set_nonincremental_init (); | |
4641 | } | |
400fbf9f | 4642 | |
3e4093b6 RS |
4643 | if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned) |
4644 | { | |
4645 | missing_braces_mentioned = 1; | |
4646 | warning_init ("missing braces around initializer"); | |
4647 | } | |
400fbf9f | 4648 | |
3e4093b6 RS |
4649 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
4650 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
4651 | { | |
4652 | constructor_fields = TYPE_FIELDS (constructor_type); | |
4653 | /* Skip any nameless bit fields at the beginning. */ | |
4654 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
4655 | && DECL_NAME (constructor_fields) == 0) | |
4656 | constructor_fields = TREE_CHAIN (constructor_fields); | |
103b7b17 | 4657 | |
3e4093b6 RS |
4658 | constructor_unfilled_fields = constructor_fields; |
4659 | constructor_bit_index = bitsize_zero_node; | |
4660 | } | |
4661 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
4662 | { | |
4663 | /* Vectors are like simple fixed-size arrays. */ | |
4664 | constructor_max_index = | |
4a90aeeb NS |
4665 | build_int_cst (NULL_TREE, |
4666 | TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0); | |
3e4093b6 RS |
4667 | constructor_index = convert (bitsizetype, integer_zero_node); |
4668 | constructor_unfilled_index = constructor_index; | |
4669 | } | |
4670 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
4671 | { | |
4672 | if (TYPE_DOMAIN (constructor_type)) | |
4673 | { | |
4674 | constructor_max_index | |
4675 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 4676 | |
3e4093b6 RS |
4677 | /* Detect non-empty initializations of zero-length arrays. */ |
4678 | if (constructor_max_index == NULL_TREE | |
4679 | && TYPE_SIZE (constructor_type)) | |
4a90aeeb | 4680 | constructor_max_index = build_int_cst (NULL_TREE, -1, -1); |
de520661 | 4681 | |
3e4093b6 RS |
4682 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
4683 | to initialize VLAs will cause a proper error; avoid tree | |
4684 | checking errors as well by setting a safe value. */ | |
4685 | if (constructor_max_index | |
4686 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
4a90aeeb | 4687 | constructor_max_index = build_int_cst (NULL_TREE, -1, -1); |
b62acd60 | 4688 | |
3e4093b6 RS |
4689 | constructor_index |
4690 | = convert (bitsizetype, | |
4691 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
4692 | } | |
4693 | else | |
4694 | constructor_index = bitsize_zero_node; | |
de520661 | 4695 | |
3e4093b6 RS |
4696 | constructor_unfilled_index = constructor_index; |
4697 | if (value && TREE_CODE (value) == STRING_CST) | |
4698 | { | |
4699 | /* We need to split the char/wchar array into individual | |
4700 | characters, so that we don't have to special case it | |
4701 | everywhere. */ | |
4702 | set_nonincremental_init_from_string (value); | |
4703 | } | |
4704 | } | |
4705 | else | |
4706 | { | |
4707 | warning_init ("braces around scalar initializer"); | |
4708 | constructor_fields = constructor_type; | |
4709 | constructor_unfilled_fields = constructor_type; | |
4710 | } | |
4711 | } | |
8b6a5902 | 4712 | |
3e4093b6 | 4713 | /* At the end of an implicit or explicit brace level, |
916c5919 JM |
4714 | finish up that level of constructor. If a single expression |
4715 | with redundant braces initialized that level, return the | |
4716 | c_expr structure for that expression. Otherwise, the original_code | |
4717 | element is set to ERROR_MARK. | |
4718 | If we were outputting the elements as they are read, return 0 as the value | |
3e4093b6 | 4719 | from inner levels (process_init_element ignores that), |
916c5919 | 4720 | but return error_mark_node as the value from the outermost level |
3e4093b6 | 4721 | (that's what we want to put in DECL_INITIAL). |
916c5919 | 4722 | Otherwise, return a CONSTRUCTOR expression as the value. */ |
de520661 | 4723 | |
916c5919 | 4724 | struct c_expr |
3e4093b6 RS |
4725 | pop_init_level (int implicit) |
4726 | { | |
4727 | struct constructor_stack *p; | |
916c5919 JM |
4728 | struct c_expr ret; |
4729 | ret.value = 0; | |
4730 | ret.original_code = ERROR_MARK; | |
de520661 | 4731 | |
3e4093b6 RS |
4732 | if (implicit == 0) |
4733 | { | |
4734 | /* When we come to an explicit close brace, | |
4735 | pop any inner levels that didn't have explicit braces. */ | |
4736 | while (constructor_stack->implicit) | |
4737 | process_init_element (pop_init_level (1)); | |
de520661 | 4738 | |
3e4093b6 RS |
4739 | if (constructor_range_stack) |
4740 | abort (); | |
4741 | } | |
e5e809f4 | 4742 | |
0066ef9c RH |
4743 | /* Now output all pending elements. */ |
4744 | constructor_incremental = 1; | |
4745 | output_pending_init_elements (1); | |
4746 | ||
3e4093b6 | 4747 | p = constructor_stack; |
e5e809f4 | 4748 | |
3e4093b6 RS |
4749 | /* Error for initializing a flexible array member, or a zero-length |
4750 | array member in an inappropriate context. */ | |
4751 | if (constructor_type && constructor_fields | |
4752 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
4753 | && TYPE_DOMAIN (constructor_type) | |
4754 | && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) | |
4755 | { | |
4756 | /* Silently discard empty initializations. The parser will | |
4757 | already have pedwarned for empty brackets. */ | |
4758 | if (integer_zerop (constructor_unfilled_index)) | |
4759 | constructor_type = NULL_TREE; | |
4760 | else if (! TYPE_SIZE (constructor_type)) | |
4761 | { | |
4762 | if (constructor_depth > 2) | |
4763 | error_init ("initialization of flexible array member in a nested context"); | |
4764 | else if (pedantic) | |
4765 | pedwarn_init ("initialization of a flexible array member"); | |
de520661 | 4766 | |
3e4093b6 RS |
4767 | /* We have already issued an error message for the existence |
4768 | of a flexible array member not at the end of the structure. | |
4769 | Discard the initializer so that we do not abort later. */ | |
4770 | if (TREE_CHAIN (constructor_fields) != NULL_TREE) | |
4771 | constructor_type = NULL_TREE; | |
4772 | } | |
4773 | else | |
4774 | /* Zero-length arrays are no longer special, so we should no longer | |
4775 | get here. */ | |
4776 | abort (); | |
4777 | } | |
de520661 | 4778 | |
3e4093b6 | 4779 | /* Warn when some struct elements are implicitly initialized to zero. */ |
eaac4679 | 4780 | if (warn_missing_field_initializers |
3e4093b6 RS |
4781 | && constructor_type |
4782 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
4783 | && constructor_unfilled_fields) | |
4784 | { | |
4785 | /* Do not warn for flexible array members or zero-length arrays. */ | |
4786 | while (constructor_unfilled_fields | |
4787 | && (! DECL_SIZE (constructor_unfilled_fields) | |
4788 | || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) | |
4789 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
cc77d4d5 | 4790 | |
3e4093b6 RS |
4791 | /* Do not warn if this level of the initializer uses member |
4792 | designators; it is likely to be deliberate. */ | |
4793 | if (constructor_unfilled_fields && !constructor_designated) | |
4794 | { | |
4795 | push_member_name (constructor_unfilled_fields); | |
4796 | warning_init ("missing initializer"); | |
4797 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
4798 | } | |
4799 | } | |
de520661 | 4800 | |
3e4093b6 | 4801 | /* Pad out the end of the structure. */ |
916c5919 | 4802 | if (p->replacement_value.value) |
3e4093b6 RS |
4803 | /* If this closes a superfluous brace pair, |
4804 | just pass out the element between them. */ | |
916c5919 | 4805 | ret = p->replacement_value; |
3e4093b6 RS |
4806 | else if (constructor_type == 0) |
4807 | ; | |
4808 | else if (TREE_CODE (constructor_type) != RECORD_TYPE | |
4809 | && TREE_CODE (constructor_type) != UNION_TYPE | |
4810 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
4811 | && TREE_CODE (constructor_type) != VECTOR_TYPE) | |
4812 | { | |
4813 | /* A nonincremental scalar initializer--just return | |
4814 | the element, after verifying there is just one. */ | |
4815 | if (constructor_elements == 0) | |
4816 | { | |
4817 | if (!constructor_erroneous) | |
4818 | error_init ("empty scalar initializer"); | |
916c5919 | 4819 | ret.value = error_mark_node; |
3e4093b6 RS |
4820 | } |
4821 | else if (TREE_CHAIN (constructor_elements) != 0) | |
4822 | { | |
4823 | error_init ("extra elements in scalar initializer"); | |
916c5919 | 4824 | ret.value = TREE_VALUE (constructor_elements); |
3e4093b6 RS |
4825 | } |
4826 | else | |
916c5919 | 4827 | ret.value = TREE_VALUE (constructor_elements); |
3e4093b6 RS |
4828 | } |
4829 | else | |
4830 | { | |
4831 | if (constructor_erroneous) | |
916c5919 | 4832 | ret.value = error_mark_node; |
3e4093b6 RS |
4833 | else |
4834 | { | |
916c5919 JM |
4835 | ret.value = build_constructor (constructor_type, |
4836 | nreverse (constructor_elements)); | |
3e4093b6 | 4837 | if (constructor_constant) |
916c5919 | 4838 | TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1; |
3e4093b6 | 4839 | if (constructor_constant && constructor_simple) |
916c5919 | 4840 | TREE_STATIC (ret.value) = 1; |
3e4093b6 RS |
4841 | } |
4842 | } | |
de520661 | 4843 | |
3e4093b6 RS |
4844 | constructor_type = p->type; |
4845 | constructor_fields = p->fields; | |
4846 | constructor_index = p->index; | |
4847 | constructor_max_index = p->max_index; | |
4848 | constructor_unfilled_index = p->unfilled_index; | |
4849 | constructor_unfilled_fields = p->unfilled_fields; | |
4850 | constructor_bit_index = p->bit_index; | |
4851 | constructor_elements = p->elements; | |
4852 | constructor_constant = p->constant; | |
4853 | constructor_simple = p->simple; | |
4854 | constructor_erroneous = p->erroneous; | |
4855 | constructor_incremental = p->incremental; | |
4856 | constructor_designated = p->designated; | |
4857 | constructor_pending_elts = p->pending_elts; | |
4858 | constructor_depth = p->depth; | |
4859 | if (!p->implicit) | |
4860 | constructor_range_stack = p->range_stack; | |
4861 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
de520661 | 4862 | |
3e4093b6 RS |
4863 | constructor_stack = p->next; |
4864 | free (p); | |
b621a4dd | 4865 | |
916c5919 | 4866 | if (ret.value == 0) |
3e4093b6 RS |
4867 | { |
4868 | if (constructor_stack == 0) | |
916c5919 JM |
4869 | { |
4870 | ret.value = error_mark_node; | |
4871 | return ret; | |
4872 | } | |
4873 | return ret; | |
3e4093b6 | 4874 | } |
916c5919 | 4875 | return ret; |
3e4093b6 | 4876 | } |
8b6a5902 | 4877 | |
3e4093b6 RS |
4878 | /* Common handling for both array range and field name designators. |
4879 | ARRAY argument is nonzero for array ranges. Returns zero for success. */ | |
400fbf9f | 4880 | |
3e4093b6 RS |
4881 | static int |
4882 | set_designator (int array) | |
de520661 | 4883 | { |
3e4093b6 RS |
4884 | tree subtype; |
4885 | enum tree_code subcode; | |
de520661 | 4886 | |
3e4093b6 RS |
4887 | /* Don't die if an entire brace-pair level is superfluous |
4888 | in the containing level. */ | |
4889 | if (constructor_type == 0) | |
4890 | return 1; | |
de520661 | 4891 | |
3e4093b6 RS |
4892 | /* If there were errors in this designator list already, bail out silently. */ |
4893 | if (designator_errorneous) | |
4894 | return 1; | |
e28cae4f | 4895 | |
3e4093b6 RS |
4896 | if (!designator_depth) |
4897 | { | |
4898 | if (constructor_range_stack) | |
4899 | abort (); | |
de520661 | 4900 | |
3e4093b6 RS |
4901 | /* Designator list starts at the level of closest explicit |
4902 | braces. */ | |
4903 | while (constructor_stack->implicit) | |
4904 | process_init_element (pop_init_level (1)); | |
4905 | constructor_designated = 1; | |
4906 | return 0; | |
4907 | } | |
de520661 | 4908 | |
3e4093b6 RS |
4909 | if (constructor_no_implicit) |
4910 | { | |
4911 | error_init ("initialization designators may not nest"); | |
4912 | return 1; | |
4913 | } | |
de520661 | 4914 | |
3e4093b6 RS |
4915 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
4916 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
3c3fa147 | 4917 | { |
3e4093b6 RS |
4918 | subtype = TREE_TYPE (constructor_fields); |
4919 | if (subtype != error_mark_node) | |
4920 | subtype = TYPE_MAIN_VARIANT (subtype); | |
3c3fa147 | 4921 | } |
3e4093b6 | 4922 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
de520661 | 4923 | { |
3e4093b6 | 4924 | subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); |
de520661 | 4925 | } |
3e4093b6 RS |
4926 | else |
4927 | abort (); | |
400fbf9f | 4928 | |
3e4093b6 RS |
4929 | subcode = TREE_CODE (subtype); |
4930 | if (array && subcode != ARRAY_TYPE) | |
4931 | { | |
4932 | error_init ("array index in non-array initializer"); | |
4933 | return 1; | |
4934 | } | |
4935 | else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) | |
4936 | { | |
4937 | error_init ("field name not in record or union initializer"); | |
4938 | return 1; | |
4939 | } | |
d45cf215 | 4940 | |
3e4093b6 RS |
4941 | constructor_designated = 1; |
4942 | push_init_level (2); | |
4943 | return 0; | |
de520661 | 4944 | } |
400fbf9f | 4945 | |
3e4093b6 RS |
4946 | /* If there are range designators in designator list, push a new designator |
4947 | to constructor_range_stack. RANGE_END is end of such stack range or | |
4948 | NULL_TREE if there is no range designator at this level. */ | |
400fbf9f | 4949 | |
3e4093b6 RS |
4950 | static void |
4951 | push_range_stack (tree range_end) | |
4952 | { | |
4953 | struct constructor_range_stack *p; | |
400fbf9f | 4954 | |
5d038c4c | 4955 | p = GGC_NEW (struct constructor_range_stack); |
3e4093b6 RS |
4956 | p->prev = constructor_range_stack; |
4957 | p->next = 0; | |
4958 | p->fields = constructor_fields; | |
4959 | p->range_start = constructor_index; | |
4960 | p->index = constructor_index; | |
4961 | p->stack = constructor_stack; | |
4962 | p->range_end = range_end; | |
8b6a5902 | 4963 | if (constructor_range_stack) |
3e4093b6 RS |
4964 | constructor_range_stack->next = p; |
4965 | constructor_range_stack = p; | |
de520661 | 4966 | } |
400fbf9f | 4967 | |
3e4093b6 RS |
4968 | /* Within an array initializer, specify the next index to be initialized. |
4969 | FIRST is that index. If LAST is nonzero, then initialize a range | |
4970 | of indices, running from FIRST through LAST. */ | |
5a7ec9d9 | 4971 | |
de520661 | 4972 | void |
3e4093b6 | 4973 | set_init_index (tree first, tree last) |
de520661 | 4974 | { |
3e4093b6 RS |
4975 | if (set_designator (1)) |
4976 | return; | |
de520661 | 4977 | |
3e4093b6 | 4978 | designator_errorneous = 1; |
de520661 | 4979 | |
3ea8cd06 JM |
4980 | if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) |
4981 | || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) | |
4982 | { | |
4983 | error_init ("array index in initializer not of integer type"); | |
4984 | return; | |
4985 | } | |
4986 | ||
3e4093b6 RS |
4987 | while ((TREE_CODE (first) == NOP_EXPR |
4988 | || TREE_CODE (first) == CONVERT_EXPR | |
4989 | || TREE_CODE (first) == NON_LVALUE_EXPR) | |
4990 | && (TYPE_MODE (TREE_TYPE (first)) | |
4991 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0))))) | |
4992 | first = TREE_OPERAND (first, 0); | |
c8e4f0e9 | 4993 | |
3e4093b6 RS |
4994 | if (last) |
4995 | while ((TREE_CODE (last) == NOP_EXPR | |
4996 | || TREE_CODE (last) == CONVERT_EXPR | |
4997 | || TREE_CODE (last) == NON_LVALUE_EXPR) | |
4998 | && (TYPE_MODE (TREE_TYPE (last)) | |
4999 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0))))) | |
5000 | last = TREE_OPERAND (last, 0); | |
de520661 | 5001 | |
3e4093b6 RS |
5002 | if (TREE_CODE (first) != INTEGER_CST) |
5003 | error_init ("nonconstant array index in initializer"); | |
5004 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) | |
5005 | error_init ("nonconstant array index in initializer"); | |
5006 | else if (TREE_CODE (constructor_type) != ARRAY_TYPE) | |
5007 | error_init ("array index in non-array initializer"); | |
622adc7e MK |
5008 | else if (tree_int_cst_sgn (first) == -1) |
5009 | error_init ("array index in initializer exceeds array bounds"); | |
3e4093b6 RS |
5010 | else if (constructor_max_index |
5011 | && tree_int_cst_lt (constructor_max_index, first)) | |
5012 | error_init ("array index in initializer exceeds array bounds"); | |
5013 | else | |
de520661 | 5014 | { |
3e4093b6 | 5015 | constructor_index = convert (bitsizetype, first); |
665f2503 | 5016 | |
3e4093b6 | 5017 | if (last) |
2bede729 | 5018 | { |
3e4093b6 RS |
5019 | if (tree_int_cst_equal (first, last)) |
5020 | last = 0; | |
5021 | else if (tree_int_cst_lt (last, first)) | |
5022 | { | |
5023 | error_init ("empty index range in initializer"); | |
5024 | last = 0; | |
5025 | } | |
5026 | else | |
5027 | { | |
5028 | last = convert (bitsizetype, last); | |
5029 | if (constructor_max_index != 0 | |
5030 | && tree_int_cst_lt (constructor_max_index, last)) | |
5031 | { | |
5032 | error_init ("array index range in initializer exceeds array bounds"); | |
5033 | last = 0; | |
5034 | } | |
5035 | } | |
2bede729 | 5036 | } |
fed3cef0 | 5037 | |
3e4093b6 RS |
5038 | designator_depth++; |
5039 | designator_errorneous = 0; | |
5040 | if (constructor_range_stack || last) | |
5041 | push_range_stack (last); | |
de520661 | 5042 | } |
de520661 | 5043 | } |
3e4093b6 RS |
5044 | |
5045 | /* Within a struct initializer, specify the next field to be initialized. */ | |
400fbf9f | 5046 | |
de520661 | 5047 | void |
3e4093b6 | 5048 | set_init_label (tree fieldname) |
de520661 | 5049 | { |
3e4093b6 | 5050 | tree tail; |
94ba5069 | 5051 | |
3e4093b6 RS |
5052 | if (set_designator (0)) |
5053 | return; | |
5054 | ||
5055 | designator_errorneous = 1; | |
5056 | ||
5057 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
5058 | && TREE_CODE (constructor_type) != UNION_TYPE) | |
94ba5069 | 5059 | { |
3e4093b6 RS |
5060 | error_init ("field name not in record or union initializer"); |
5061 | return; | |
94ba5069 RS |
5062 | } |
5063 | ||
3e4093b6 RS |
5064 | for (tail = TYPE_FIELDS (constructor_type); tail; |
5065 | tail = TREE_CHAIN (tail)) | |
8b6a5902 | 5066 | { |
3e4093b6 RS |
5067 | if (DECL_NAME (tail) == fieldname) |
5068 | break; | |
8b6a5902 JJ |
5069 | } |
5070 | ||
3e4093b6 RS |
5071 | if (tail == 0) |
5072 | error ("unknown field `%s' specified in initializer", | |
5073 | IDENTIFIER_POINTER (fieldname)); | |
5074 | else | |
8b6a5902 | 5075 | { |
3e4093b6 RS |
5076 | constructor_fields = tail; |
5077 | designator_depth++; | |
8b6a5902 | 5078 | designator_errorneous = 0; |
3e4093b6 RS |
5079 | if (constructor_range_stack) |
5080 | push_range_stack (NULL_TREE); | |
8b6a5902 | 5081 | } |
3e4093b6 RS |
5082 | } |
5083 | \f | |
5084 | /* Add a new initializer to the tree of pending initializers. PURPOSE | |
5085 | identifies the initializer, either array index or field in a structure. | |
5086 | VALUE is the value of that index or field. */ | |
de520661 | 5087 | |
3e4093b6 RS |
5088 | static void |
5089 | add_pending_init (tree purpose, tree value) | |
5090 | { | |
5091 | struct init_node *p, **q, *r; | |
5092 | ||
5093 | q = &constructor_pending_elts; | |
5094 | p = 0; | |
5095 | ||
5096 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
de520661 | 5097 | { |
3e4093b6 | 5098 | while (*q != 0) |
91fa3c30 | 5099 | { |
3e4093b6 RS |
5100 | p = *q; |
5101 | if (tree_int_cst_lt (purpose, p->purpose)) | |
5102 | q = &p->left; | |
5103 | else if (tree_int_cst_lt (p->purpose, purpose)) | |
5104 | q = &p->right; | |
5105 | else | |
5106 | { | |
5107 | if (TREE_SIDE_EFFECTS (p->value)) | |
5108 | warning_init ("initialized field with side-effects overwritten"); | |
5109 | p->value = value; | |
5110 | return; | |
5111 | } | |
91fa3c30 | 5112 | } |
de520661 | 5113 | } |
3e4093b6 | 5114 | else |
de520661 | 5115 | { |
3e4093b6 | 5116 | tree bitpos; |
400fbf9f | 5117 | |
3e4093b6 RS |
5118 | bitpos = bit_position (purpose); |
5119 | while (*q != NULL) | |
5120 | { | |
5121 | p = *q; | |
5122 | if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
5123 | q = &p->left; | |
5124 | else if (p->purpose != purpose) | |
5125 | q = &p->right; | |
5126 | else | |
5127 | { | |
5128 | if (TREE_SIDE_EFFECTS (p->value)) | |
5129 | warning_init ("initialized field with side-effects overwritten"); | |
5130 | p->value = value; | |
5131 | return; | |
5132 | } | |
5133 | } | |
91fa3c30 | 5134 | } |
b71c7f8a | 5135 | |
5d038c4c | 5136 | r = GGC_NEW (struct init_node); |
3e4093b6 RS |
5137 | r->purpose = purpose; |
5138 | r->value = value; | |
8b6a5902 | 5139 | |
3e4093b6 RS |
5140 | *q = r; |
5141 | r->parent = p; | |
5142 | r->left = 0; | |
5143 | r->right = 0; | |
5144 | r->balance = 0; | |
b71c7f8a | 5145 | |
3e4093b6 | 5146 | while (p) |
de520661 | 5147 | { |
3e4093b6 | 5148 | struct init_node *s; |
665f2503 | 5149 | |
3e4093b6 | 5150 | if (r == p->left) |
2bede729 | 5151 | { |
3e4093b6 RS |
5152 | if (p->balance == 0) |
5153 | p->balance = -1; | |
5154 | else if (p->balance < 0) | |
5155 | { | |
5156 | if (r->balance < 0) | |
5157 | { | |
5158 | /* L rotation. */ | |
5159 | p->left = r->right; | |
5160 | if (p->left) | |
5161 | p->left->parent = p; | |
5162 | r->right = p; | |
e7b6a0ee | 5163 | |
3e4093b6 RS |
5164 | p->balance = 0; |
5165 | r->balance = 0; | |
39bc99c2 | 5166 | |
3e4093b6 RS |
5167 | s = p->parent; |
5168 | p->parent = r; | |
5169 | r->parent = s; | |
5170 | if (s) | |
5171 | { | |
5172 | if (s->left == p) | |
5173 | s->left = r; | |
5174 | else | |
5175 | s->right = r; | |
5176 | } | |
5177 | else | |
5178 | constructor_pending_elts = r; | |
5179 | } | |
5180 | else | |
5181 | { | |
5182 | /* LR rotation. */ | |
5183 | struct init_node *t = r->right; | |
e7b6a0ee | 5184 | |
3e4093b6 RS |
5185 | r->right = t->left; |
5186 | if (r->right) | |
5187 | r->right->parent = r; | |
5188 | t->left = r; | |
5189 | ||
5190 | p->left = t->right; | |
5191 | if (p->left) | |
5192 | p->left->parent = p; | |
5193 | t->right = p; | |
5194 | ||
5195 | p->balance = t->balance < 0; | |
5196 | r->balance = -(t->balance > 0); | |
5197 | t->balance = 0; | |
5198 | ||
5199 | s = p->parent; | |
5200 | p->parent = t; | |
5201 | r->parent = t; | |
5202 | t->parent = s; | |
5203 | if (s) | |
5204 | { | |
5205 | if (s->left == p) | |
5206 | s->left = t; | |
5207 | else | |
5208 | s->right = t; | |
5209 | } | |
5210 | else | |
5211 | constructor_pending_elts = t; | |
5212 | } | |
5213 | break; | |
5214 | } | |
5215 | else | |
5216 | { | |
5217 | /* p->balance == +1; growth of left side balances the node. */ | |
5218 | p->balance = 0; | |
5219 | break; | |
5220 | } | |
2bede729 | 5221 | } |
3e4093b6 RS |
5222 | else /* r == p->right */ |
5223 | { | |
5224 | if (p->balance == 0) | |
5225 | /* Growth propagation from right side. */ | |
5226 | p->balance++; | |
5227 | else if (p->balance > 0) | |
5228 | { | |
5229 | if (r->balance > 0) | |
5230 | { | |
5231 | /* R rotation. */ | |
5232 | p->right = r->left; | |
5233 | if (p->right) | |
5234 | p->right->parent = p; | |
5235 | r->left = p; | |
5236 | ||
5237 | p->balance = 0; | |
5238 | r->balance = 0; | |
5239 | ||
5240 | s = p->parent; | |
5241 | p->parent = r; | |
5242 | r->parent = s; | |
5243 | if (s) | |
5244 | { | |
5245 | if (s->left == p) | |
5246 | s->left = r; | |
5247 | else | |
5248 | s->right = r; | |
5249 | } | |
5250 | else | |
5251 | constructor_pending_elts = r; | |
5252 | } | |
5253 | else /* r->balance == -1 */ | |
5254 | { | |
5255 | /* RL rotation */ | |
5256 | struct init_node *t = r->left; | |
5257 | ||
5258 | r->left = t->right; | |
5259 | if (r->left) | |
5260 | r->left->parent = r; | |
5261 | t->right = r; | |
5262 | ||
5263 | p->right = t->left; | |
5264 | if (p->right) | |
5265 | p->right->parent = p; | |
5266 | t->left = p; | |
5267 | ||
5268 | r->balance = (t->balance < 0); | |
5269 | p->balance = -(t->balance > 0); | |
5270 | t->balance = 0; | |
5271 | ||
5272 | s = p->parent; | |
5273 | p->parent = t; | |
5274 | r->parent = t; | |
5275 | t->parent = s; | |
5276 | if (s) | |
5277 | { | |
5278 | if (s->left == p) | |
5279 | s->left = t; | |
5280 | else | |
5281 | s->right = t; | |
5282 | } | |
5283 | else | |
5284 | constructor_pending_elts = t; | |
5285 | } | |
5286 | break; | |
5287 | } | |
5288 | else | |
5289 | { | |
5290 | /* p->balance == -1; growth of right side balances the node. */ | |
5291 | p->balance = 0; | |
5292 | break; | |
5293 | } | |
5294 | } | |
5295 | ||
5296 | r = p; | |
5297 | p = p->parent; | |
5298 | } | |
5299 | } | |
5300 | ||
5301 | /* Build AVL tree from a sorted chain. */ | |
5302 | ||
5303 | static void | |
5304 | set_nonincremental_init (void) | |
5305 | { | |
5306 | tree chain; | |
5307 | ||
5308 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
5309 | && TREE_CODE (constructor_type) != ARRAY_TYPE) | |
5310 | return; | |
5311 | ||
5312 | for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain)) | |
5313 | add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain)); | |
5314 | constructor_elements = 0; | |
5315 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
5316 | { | |
5317 | constructor_unfilled_fields = TYPE_FIELDS (constructor_type); | |
5318 | /* Skip any nameless bit fields at the beginning. */ | |
5319 | while (constructor_unfilled_fields != 0 | |
5320 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
5321 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
5322 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
fed3cef0 | 5323 | |
de520661 | 5324 | } |
3e4093b6 | 5325 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
de520661 | 5326 | { |
3e4093b6 RS |
5327 | if (TYPE_DOMAIN (constructor_type)) |
5328 | constructor_unfilled_index | |
5329 | = convert (bitsizetype, | |
5330 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
5331 | else | |
5332 | constructor_unfilled_index = bitsize_zero_node; | |
de520661 | 5333 | } |
3e4093b6 | 5334 | constructor_incremental = 0; |
de520661 | 5335 | } |
400fbf9f | 5336 | |
3e4093b6 | 5337 | /* Build AVL tree from a string constant. */ |
de520661 | 5338 | |
3e4093b6 RS |
5339 | static void |
5340 | set_nonincremental_init_from_string (tree str) | |
de520661 | 5341 | { |
3e4093b6 RS |
5342 | tree value, purpose, type; |
5343 | HOST_WIDE_INT val[2]; | |
5344 | const char *p, *end; | |
5345 | int byte, wchar_bytes, charwidth, bitpos; | |
de520661 | 5346 | |
3e4093b6 RS |
5347 | if (TREE_CODE (constructor_type) != ARRAY_TYPE) |
5348 | abort (); | |
940ff66d | 5349 | |
3e4093b6 RS |
5350 | if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) |
5351 | == TYPE_PRECISION (char_type_node)) | |
5352 | wchar_bytes = 1; | |
5353 | else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) | |
5354 | == TYPE_PRECISION (wchar_type_node)) | |
5355 | wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT; | |
5356 | else | |
5357 | abort (); | |
400fbf9f | 5358 | |
3e4093b6 RS |
5359 | charwidth = TYPE_PRECISION (char_type_node); |
5360 | type = TREE_TYPE (constructor_type); | |
5361 | p = TREE_STRING_POINTER (str); | |
5362 | end = p + TREE_STRING_LENGTH (str); | |
91fa3c30 | 5363 | |
3e4093b6 RS |
5364 | for (purpose = bitsize_zero_node; |
5365 | p < end && !tree_int_cst_lt (constructor_max_index, purpose); | |
5366 | purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) | |
584ef5fe | 5367 | { |
3e4093b6 | 5368 | if (wchar_bytes == 1) |
ffc5c6a9 | 5369 | { |
3e4093b6 RS |
5370 | val[1] = (unsigned char) *p++; |
5371 | val[0] = 0; | |
ffc5c6a9 RH |
5372 | } |
5373 | else | |
3e4093b6 RS |
5374 | { |
5375 | val[0] = 0; | |
5376 | val[1] = 0; | |
5377 | for (byte = 0; byte < wchar_bytes; byte++) | |
5378 | { | |
5379 | if (BYTES_BIG_ENDIAN) | |
5380 | bitpos = (wchar_bytes - byte - 1) * charwidth; | |
5381 | else | |
5382 | bitpos = byte * charwidth; | |
5383 | val[bitpos < HOST_BITS_PER_WIDE_INT] | |
5384 | |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) | |
5385 | << (bitpos % HOST_BITS_PER_WIDE_INT); | |
5386 | } | |
5387 | } | |
584ef5fe | 5388 | |
8df83eae | 5389 | if (!TYPE_UNSIGNED (type)) |
3e4093b6 RS |
5390 | { |
5391 | bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; | |
5392 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
5393 | { | |
5394 | if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1))) | |
5395 | { | |
5396 | val[1] |= ((HOST_WIDE_INT) -1) << bitpos; | |
5397 | val[0] = -1; | |
5398 | } | |
5399 | } | |
5400 | else if (bitpos == HOST_BITS_PER_WIDE_INT) | |
5401 | { | |
5402 | if (val[1] < 0) | |
5403 | val[0] = -1; | |
5404 | } | |
5405 | else if (val[0] & (((HOST_WIDE_INT) 1) | |
5406 | << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) | |
5407 | val[0] |= ((HOST_WIDE_INT) -1) | |
5408 | << (bitpos - HOST_BITS_PER_WIDE_INT); | |
5409 | } | |
ffc5c6a9 | 5410 | |
4a90aeeb | 5411 | value = build_int_cst (type, val[1], val[0]); |
3e4093b6 | 5412 | add_pending_init (purpose, value); |
9dfcc8db BH |
5413 | } |
5414 | ||
3e4093b6 RS |
5415 | constructor_incremental = 0; |
5416 | } | |
de520661 | 5417 | |
3e4093b6 RS |
5418 | /* Return value of FIELD in pending initializer or zero if the field was |
5419 | not initialized yet. */ | |
5420 | ||
5421 | static tree | |
5422 | find_init_member (tree field) | |
5423 | { | |
5424 | struct init_node *p; | |
5425 | ||
5426 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
19d76e60 | 5427 | { |
3e4093b6 RS |
5428 | if (constructor_incremental |
5429 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
5430 | set_nonincremental_init (); | |
5431 | ||
5432 | p = constructor_pending_elts; | |
5433 | while (p) | |
19d76e60 | 5434 | { |
3e4093b6 RS |
5435 | if (tree_int_cst_lt (field, p->purpose)) |
5436 | p = p->left; | |
5437 | else if (tree_int_cst_lt (p->purpose, field)) | |
5438 | p = p->right; | |
5439 | else | |
5440 | return p->value; | |
19d76e60 | 5441 | } |
19d76e60 | 5442 | } |
3e4093b6 | 5443 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
de520661 | 5444 | { |
3e4093b6 | 5445 | tree bitpos = bit_position (field); |
de520661 | 5446 | |
3e4093b6 RS |
5447 | if (constructor_incremental |
5448 | && (!constructor_unfilled_fields | |
5449 | || tree_int_cst_lt (bitpos, | |
5450 | bit_position (constructor_unfilled_fields)))) | |
5451 | set_nonincremental_init (); | |
de520661 | 5452 | |
3e4093b6 RS |
5453 | p = constructor_pending_elts; |
5454 | while (p) | |
5455 | { | |
5456 | if (field == p->purpose) | |
5457 | return p->value; | |
5458 | else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
5459 | p = p->left; | |
5460 | else | |
5461 | p = p->right; | |
5462 | } | |
5463 | } | |
5464 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
de520661 | 5465 | { |
3e4093b6 RS |
5466 | if (constructor_elements |
5467 | && TREE_PURPOSE (constructor_elements) == field) | |
5468 | return TREE_VALUE (constructor_elements); | |
de520661 | 5469 | } |
3e4093b6 | 5470 | return 0; |
de520661 RS |
5471 | } |
5472 | ||
3e4093b6 RS |
5473 | /* "Output" the next constructor element. |
5474 | At top level, really output it to assembler code now. | |
5475 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
5476 | TYPE is the data type that the containing data type wants here. | |
5477 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
916c5919 JM |
5478 | If VALUE is a string constant, STRICT_STRING is true if it is |
5479 | unparenthesized or we should not warn here for it being parenthesized. | |
5480 | For other types of VALUE, STRICT_STRING is not used. | |
8b6a5902 | 5481 | |
3e4093b6 RS |
5482 | PENDING if non-nil means output pending elements that belong |
5483 | right after this element. (PENDING is normally 1; | |
5484 | it is 0 while outputting pending elements, to avoid recursion.) */ | |
8b6a5902 | 5485 | |
3e4093b6 | 5486 | static void |
916c5919 JM |
5487 | output_init_element (tree value, bool strict_string, tree type, tree field, |
5488 | int pending) | |
3e4093b6 RS |
5489 | { |
5490 | if (type == error_mark_node) | |
8b6a5902 | 5491 | { |
3e4093b6 RS |
5492 | constructor_erroneous = 1; |
5493 | return; | |
8b6a5902 | 5494 | } |
3e4093b6 RS |
5495 | if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE |
5496 | || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE | |
5497 | && !(TREE_CODE (value) == STRING_CST | |
5498 | && TREE_CODE (type) == ARRAY_TYPE | |
197463ae | 5499 | && INTEGRAL_TYPE_P (TREE_TYPE (type))) |
3e4093b6 | 5500 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), |
132da1a5 | 5501 | TYPE_MAIN_VARIANT (type)))) |
3e4093b6 | 5502 | value = default_conversion (value); |
8b6a5902 | 5503 | |
3e4093b6 RS |
5504 | if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR |
5505 | && require_constant_value && !flag_isoc99 && pending) | |
8b6a5902 | 5506 | { |
3e4093b6 RS |
5507 | /* As an extension, allow initializing objects with static storage |
5508 | duration with compound literals (which are then treated just as | |
5509 | the brace enclosed list they contain). */ | |
5510 | tree decl = COMPOUND_LITERAL_EXPR_DECL (value); | |
5511 | value = DECL_INITIAL (decl); | |
8b6a5902 JJ |
5512 | } |
5513 | ||
3e4093b6 RS |
5514 | if (value == error_mark_node) |
5515 | constructor_erroneous = 1; | |
5516 | else if (!TREE_CONSTANT (value)) | |
5517 | constructor_constant = 0; | |
5518 | else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0 | |
5519 | || ((TREE_CODE (constructor_type) == RECORD_TYPE | |
5520 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5521 | && DECL_C_BIT_FIELD (field) | |
5522 | && TREE_CODE (value) != INTEGER_CST)) | |
5523 | constructor_simple = 0; | |
5524 | ||
5525 | if (require_constant_value && ! TREE_CONSTANT (value)) | |
8b6a5902 | 5526 | { |
3e4093b6 RS |
5527 | error_init ("initializer element is not constant"); |
5528 | value = error_mark_node; | |
8b6a5902 | 5529 | } |
3e4093b6 RS |
5530 | else if (require_constant_elements |
5531 | && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0) | |
5532 | pedwarn ("initializer element is not computable at load time"); | |
5533 | ||
5534 | /* If this field is empty (and not at the end of structure), | |
5535 | don't do anything other than checking the initializer. */ | |
5536 | if (field | |
5537 | && (TREE_TYPE (field) == error_mark_node | |
5538 | || (COMPLETE_TYPE_P (TREE_TYPE (field)) | |
5539 | && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) | |
5540 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5541 | || TREE_CHAIN (field))))) | |
5542 | return; | |
5543 | ||
916c5919 | 5544 | value = digest_init (type, value, strict_string, require_constant_value); |
3e4093b6 | 5545 | if (value == error_mark_node) |
8b6a5902 | 5546 | { |
3e4093b6 RS |
5547 | constructor_erroneous = 1; |
5548 | return; | |
8b6a5902 | 5549 | } |
8b6a5902 | 5550 | |
3e4093b6 RS |
5551 | /* If this element doesn't come next in sequence, |
5552 | put it on constructor_pending_elts. */ | |
5553 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5554 | && (!constructor_incremental | |
5555 | || !tree_int_cst_equal (field, constructor_unfilled_index))) | |
8b6a5902 | 5556 | { |
3e4093b6 RS |
5557 | if (constructor_incremental |
5558 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
5559 | set_nonincremental_init (); | |
5560 | ||
5561 | add_pending_init (field, value); | |
5562 | return; | |
8b6a5902 | 5563 | } |
3e4093b6 RS |
5564 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
5565 | && (!constructor_incremental | |
5566 | || field != constructor_unfilled_fields)) | |
8b6a5902 | 5567 | { |
3e4093b6 RS |
5568 | /* We do this for records but not for unions. In a union, |
5569 | no matter which field is specified, it can be initialized | |
5570 | right away since it starts at the beginning of the union. */ | |
5571 | if (constructor_incremental) | |
5572 | { | |
5573 | if (!constructor_unfilled_fields) | |
5574 | set_nonincremental_init (); | |
5575 | else | |
5576 | { | |
5577 | tree bitpos, unfillpos; | |
5578 | ||
5579 | bitpos = bit_position (field); | |
5580 | unfillpos = bit_position (constructor_unfilled_fields); | |
5581 | ||
5582 | if (tree_int_cst_lt (bitpos, unfillpos)) | |
5583 | set_nonincremental_init (); | |
5584 | } | |
5585 | } | |
5586 | ||
5587 | add_pending_init (field, value); | |
5588 | return; | |
8b6a5902 | 5589 | } |
3e4093b6 RS |
5590 | else if (TREE_CODE (constructor_type) == UNION_TYPE |
5591 | && constructor_elements) | |
5592 | { | |
5593 | if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements))) | |
5594 | warning_init ("initialized field with side-effects overwritten"); | |
8b6a5902 | 5595 | |
3e4093b6 RS |
5596 | /* We can have just one union field set. */ |
5597 | constructor_elements = 0; | |
5598 | } | |
8b6a5902 | 5599 | |
3e4093b6 RS |
5600 | /* Otherwise, output this element either to |
5601 | constructor_elements or to the assembler file. */ | |
8b6a5902 | 5602 | |
3e4093b6 RS |
5603 | if (field && TREE_CODE (field) == INTEGER_CST) |
5604 | field = copy_node (field); | |
5605 | constructor_elements | |
5606 | = tree_cons (field, value, constructor_elements); | |
8b6a5902 | 5607 | |
3e4093b6 RS |
5608 | /* Advance the variable that indicates sequential elements output. */ |
5609 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5610 | constructor_unfilled_index | |
5611 | = size_binop (PLUS_EXPR, constructor_unfilled_index, | |
5612 | bitsize_one_node); | |
5613 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
5614 | { | |
5615 | constructor_unfilled_fields | |
5616 | = TREE_CHAIN (constructor_unfilled_fields); | |
8b6a5902 | 5617 | |
3e4093b6 RS |
5618 | /* Skip any nameless bit fields. */ |
5619 | while (constructor_unfilled_fields != 0 | |
5620 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
5621 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
5622 | constructor_unfilled_fields = | |
5623 | TREE_CHAIN (constructor_unfilled_fields); | |
5624 | } | |
5625 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
5626 | constructor_unfilled_fields = 0; | |
de520661 | 5627 | |
3e4093b6 RS |
5628 | /* Now output any pending elements which have become next. */ |
5629 | if (pending) | |
5630 | output_pending_init_elements (0); | |
5631 | } | |
8b6a5902 | 5632 | |
3e4093b6 RS |
5633 | /* Output any pending elements which have become next. |
5634 | As we output elements, constructor_unfilled_{fields,index} | |
5635 | advances, which may cause other elements to become next; | |
5636 | if so, they too are output. | |
8b6a5902 | 5637 | |
3e4093b6 RS |
5638 | If ALL is 0, we return when there are |
5639 | no more pending elements to output now. | |
665f2503 | 5640 | |
3e4093b6 RS |
5641 | If ALL is 1, we output space as necessary so that |
5642 | we can output all the pending elements. */ | |
19d76e60 | 5643 | |
3e4093b6 RS |
5644 | static void |
5645 | output_pending_init_elements (int all) | |
5646 | { | |
5647 | struct init_node *elt = constructor_pending_elts; | |
5648 | tree next; | |
de520661 | 5649 | |
3e4093b6 RS |
5650 | retry: |
5651 | ||
ba228239 | 5652 | /* Look through the whole pending tree. |
3e4093b6 RS |
5653 | If we find an element that should be output now, |
5654 | output it. Otherwise, set NEXT to the element | |
5655 | that comes first among those still pending. */ | |
5656 | ||
5657 | next = 0; | |
5658 | while (elt) | |
5659 | { | |
5660 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8b6a5902 | 5661 | { |
3e4093b6 RS |
5662 | if (tree_int_cst_equal (elt->purpose, |
5663 | constructor_unfilled_index)) | |
916c5919 | 5664 | output_init_element (elt->value, true, |
3e4093b6 RS |
5665 | TREE_TYPE (constructor_type), |
5666 | constructor_unfilled_index, 0); | |
5667 | else if (tree_int_cst_lt (constructor_unfilled_index, | |
5668 | elt->purpose)) | |
8b6a5902 | 5669 | { |
3e4093b6 RS |
5670 | /* Advance to the next smaller node. */ |
5671 | if (elt->left) | |
5672 | elt = elt->left; | |
5673 | else | |
5674 | { | |
5675 | /* We have reached the smallest node bigger than the | |
5676 | current unfilled index. Fill the space first. */ | |
5677 | next = elt->purpose; | |
5678 | break; | |
5679 | } | |
8b6a5902 | 5680 | } |
ce662d4c JJ |
5681 | else |
5682 | { | |
3e4093b6 RS |
5683 | /* Advance to the next bigger node. */ |
5684 | if (elt->right) | |
5685 | elt = elt->right; | |
5686 | else | |
ce662d4c | 5687 | { |
3e4093b6 RS |
5688 | /* We have reached the biggest node in a subtree. Find |
5689 | the parent of it, which is the next bigger node. */ | |
5690 | while (elt->parent && elt->parent->right == elt) | |
5691 | elt = elt->parent; | |
5692 | elt = elt->parent; | |
5693 | if (elt && tree_int_cst_lt (constructor_unfilled_index, | |
5694 | elt->purpose)) | |
5695 | { | |
5696 | next = elt->purpose; | |
5697 | break; | |
5698 | } | |
ce662d4c JJ |
5699 | } |
5700 | } | |
8b6a5902 | 5701 | } |
3e4093b6 RS |
5702 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
5703 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5704 | { | |
5705 | tree ctor_unfilled_bitpos, elt_bitpos; | |
ce662d4c | 5706 | |
3e4093b6 RS |
5707 | /* If the current record is complete we are done. */ |
5708 | if (constructor_unfilled_fields == 0) | |
5709 | break; | |
de520661 | 5710 | |
3e4093b6 RS |
5711 | ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields); |
5712 | elt_bitpos = bit_position (elt->purpose); | |
5713 | /* We can't compare fields here because there might be empty | |
5714 | fields in between. */ | |
5715 | if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos)) | |
5716 | { | |
5717 | constructor_unfilled_fields = elt->purpose; | |
916c5919 | 5718 | output_init_element (elt->value, true, TREE_TYPE (elt->purpose), |
3e4093b6 RS |
5719 | elt->purpose, 0); |
5720 | } | |
5721 | else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos)) | |
5722 | { | |
5723 | /* Advance to the next smaller node. */ | |
5724 | if (elt->left) | |
5725 | elt = elt->left; | |
5726 | else | |
5727 | { | |
5728 | /* We have reached the smallest node bigger than the | |
5729 | current unfilled field. Fill the space first. */ | |
5730 | next = elt->purpose; | |
5731 | break; | |
5732 | } | |
5733 | } | |
5734 | else | |
5735 | { | |
5736 | /* Advance to the next bigger node. */ | |
5737 | if (elt->right) | |
5738 | elt = elt->right; | |
5739 | else | |
5740 | { | |
5741 | /* We have reached the biggest node in a subtree. Find | |
5742 | the parent of it, which is the next bigger node. */ | |
5743 | while (elt->parent && elt->parent->right == elt) | |
5744 | elt = elt->parent; | |
5745 | elt = elt->parent; | |
5746 | if (elt | |
5747 | && (tree_int_cst_lt (ctor_unfilled_bitpos, | |
5748 | bit_position (elt->purpose)))) | |
5749 | { | |
5750 | next = elt->purpose; | |
5751 | break; | |
5752 | } | |
5753 | } | |
5754 | } | |
5755 | } | |
5756 | } | |
de520661 | 5757 | |
3e4093b6 RS |
5758 | /* Ordinarily return, but not if we want to output all |
5759 | and there are elements left. */ | |
5760 | if (! (all && next != 0)) | |
e5cfb88f RK |
5761 | return; |
5762 | ||
3e4093b6 RS |
5763 | /* If it's not incremental, just skip over the gap, so that after |
5764 | jumping to retry we will output the next successive element. */ | |
5765 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
5766 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5767 | constructor_unfilled_fields = next; | |
5768 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5769 | constructor_unfilled_index = next; | |
de520661 | 5770 | |
3e4093b6 RS |
5771 | /* ELT now points to the node in the pending tree with the next |
5772 | initializer to output. */ | |
5773 | goto retry; | |
de520661 RS |
5774 | } |
5775 | \f | |
3e4093b6 RS |
5776 | /* Add one non-braced element to the current constructor level. |
5777 | This adjusts the current position within the constructor's type. | |
5778 | This may also start or terminate implicit levels | |
5779 | to handle a partly-braced initializer. | |
e5e809f4 | 5780 | |
3e4093b6 RS |
5781 | Once this has found the correct level for the new element, |
5782 | it calls output_init_element. */ | |
5783 | ||
5784 | void | |
916c5919 | 5785 | process_init_element (struct c_expr value) |
e5e809f4 | 5786 | { |
916c5919 JM |
5787 | tree orig_value = value.value; |
5788 | int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; | |
5789 | bool strict_string = value.original_code == STRING_CST; | |
e5e809f4 | 5790 | |
3e4093b6 RS |
5791 | designator_depth = 0; |
5792 | designator_errorneous = 0; | |
e5e809f4 | 5793 | |
3e4093b6 RS |
5794 | /* Handle superfluous braces around string cst as in |
5795 | char x[] = {"foo"}; */ | |
5796 | if (string_flag | |
5797 | && constructor_type | |
5798 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
197463ae | 5799 | && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) |
3e4093b6 | 5800 | && integer_zerop (constructor_unfilled_index)) |
e5e809f4 | 5801 | { |
916c5919 | 5802 | if (constructor_stack->replacement_value.value) |
3e4093b6 RS |
5803 | error_init ("excess elements in char array initializer"); |
5804 | constructor_stack->replacement_value = value; | |
5805 | return; | |
e5e809f4 | 5806 | } |
8b6a5902 | 5807 | |
916c5919 | 5808 | if (constructor_stack->replacement_value.value != 0) |
3e4093b6 RS |
5809 | { |
5810 | error_init ("excess elements in struct initializer"); | |
5811 | return; | |
e5e809f4 JL |
5812 | } |
5813 | ||
3e4093b6 RS |
5814 | /* Ignore elements of a brace group if it is entirely superfluous |
5815 | and has already been diagnosed. */ | |
5816 | if (constructor_type == 0) | |
5817 | return; | |
e5e809f4 | 5818 | |
3e4093b6 RS |
5819 | /* If we've exhausted any levels that didn't have braces, |
5820 | pop them now. */ | |
5821 | while (constructor_stack->implicit) | |
5822 | { | |
5823 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
5824 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5825 | && constructor_fields == 0) | |
5826 | process_init_element (pop_init_level (1)); | |
5827 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5828 | && (constructor_max_index == 0 | |
5829 | || tree_int_cst_lt (constructor_max_index, | |
5830 | constructor_index))) | |
5831 | process_init_element (pop_init_level (1)); | |
5832 | else | |
5833 | break; | |
5834 | } | |
e5e809f4 | 5835 | |
3e4093b6 RS |
5836 | /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ |
5837 | if (constructor_range_stack) | |
e5e809f4 | 5838 | { |
3e4093b6 RS |
5839 | /* If value is a compound literal and we'll be just using its |
5840 | content, don't put it into a SAVE_EXPR. */ | |
916c5919 | 5841 | if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR |
3e4093b6 RS |
5842 | || !require_constant_value |
5843 | || flag_isoc99) | |
916c5919 | 5844 | value.value = save_expr (value.value); |
3e4093b6 | 5845 | } |
e5e809f4 | 5846 | |
3e4093b6 RS |
5847 | while (1) |
5848 | { | |
5849 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
e5e809f4 | 5850 | { |
3e4093b6 RS |
5851 | tree fieldtype; |
5852 | enum tree_code fieldcode; | |
e5e809f4 | 5853 | |
3e4093b6 RS |
5854 | if (constructor_fields == 0) |
5855 | { | |
5856 | pedwarn_init ("excess elements in struct initializer"); | |
5857 | break; | |
5858 | } | |
e5e809f4 | 5859 | |
3e4093b6 RS |
5860 | fieldtype = TREE_TYPE (constructor_fields); |
5861 | if (fieldtype != error_mark_node) | |
5862 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
5863 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 5864 | |
3e4093b6 RS |
5865 | /* Error for non-static initialization of a flexible array member. */ |
5866 | if (fieldcode == ARRAY_TYPE | |
5867 | && !require_constant_value | |
5868 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
5869 | && TREE_CHAIN (constructor_fields) == NULL_TREE) | |
5870 | { | |
5871 | error_init ("non-static initialization of a flexible array member"); | |
5872 | break; | |
5873 | } | |
e5e809f4 | 5874 | |
3e4093b6 | 5875 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 5876 | if (value.value != 0 |
3e4093b6 | 5877 | && fieldcode == ARRAY_TYPE |
197463ae | 5878 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 5879 | && string_flag) |
916c5919 | 5880 | value.value = orig_value; |
3e4093b6 RS |
5881 | /* Otherwise, if we have come to a subaggregate, |
5882 | and we don't have an element of its type, push into it. */ | |
916c5919 JM |
5883 | else if (value.value != 0 && !constructor_no_implicit |
5884 | && value.value != error_mark_node | |
5885 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 RS |
5886 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
5887 | || fieldcode == UNION_TYPE)) | |
5888 | { | |
5889 | push_init_level (1); | |
5890 | continue; | |
5891 | } | |
e5e809f4 | 5892 | |
916c5919 | 5893 | if (value.value) |
3e4093b6 RS |
5894 | { |
5895 | push_member_name (constructor_fields); | |
916c5919 JM |
5896 | output_init_element (value.value, strict_string, |
5897 | fieldtype, constructor_fields, 1); | |
3e4093b6 | 5898 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
5899 | } |
5900 | else | |
3e4093b6 RS |
5901 | /* Do the bookkeeping for an element that was |
5902 | directly output as a constructor. */ | |
e5e809f4 | 5903 | { |
3e4093b6 RS |
5904 | /* For a record, keep track of end position of last field. */ |
5905 | if (DECL_SIZE (constructor_fields)) | |
5906 | constructor_bit_index | |
5907 | = size_binop (PLUS_EXPR, | |
5908 | bit_position (constructor_fields), | |
5909 | DECL_SIZE (constructor_fields)); | |
5910 | ||
5911 | /* If the current field was the first one not yet written out, | |
5912 | it isn't now, so update. */ | |
5913 | if (constructor_unfilled_fields == constructor_fields) | |
5914 | { | |
5915 | constructor_unfilled_fields = TREE_CHAIN (constructor_fields); | |
5916 | /* Skip any nameless bit fields. */ | |
5917 | while (constructor_unfilled_fields != 0 | |
5918 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
5919 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
5920 | constructor_unfilled_fields = | |
5921 | TREE_CHAIN (constructor_unfilled_fields); | |
5922 | } | |
e5e809f4 | 5923 | } |
3e4093b6 RS |
5924 | |
5925 | constructor_fields = TREE_CHAIN (constructor_fields); | |
5926 | /* Skip any nameless bit fields at the beginning. */ | |
5927 | while (constructor_fields != 0 | |
5928 | && DECL_C_BIT_FIELD (constructor_fields) | |
5929 | && DECL_NAME (constructor_fields) == 0) | |
5930 | constructor_fields = TREE_CHAIN (constructor_fields); | |
e5e809f4 | 5931 | } |
3e4093b6 | 5932 | else if (TREE_CODE (constructor_type) == UNION_TYPE) |
e5e809f4 | 5933 | { |
3e4093b6 RS |
5934 | tree fieldtype; |
5935 | enum tree_code fieldcode; | |
e5e809f4 | 5936 | |
3e4093b6 RS |
5937 | if (constructor_fields == 0) |
5938 | { | |
5939 | pedwarn_init ("excess elements in union initializer"); | |
5940 | break; | |
5941 | } | |
e5e809f4 | 5942 | |
3e4093b6 RS |
5943 | fieldtype = TREE_TYPE (constructor_fields); |
5944 | if (fieldtype != error_mark_node) | |
5945 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
5946 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 5947 | |
3e4093b6 RS |
5948 | /* Warn that traditional C rejects initialization of unions. |
5949 | We skip the warning if the value is zero. This is done | |
5950 | under the assumption that the zero initializer in user | |
5951 | code appears conditioned on e.g. __STDC__ to avoid | |
5952 | "missing initializer" warnings and relies on default | |
5953 | initialization to zero in the traditional C case. | |
5954 | We also skip the warning if the initializer is designated, | |
5955 | again on the assumption that this must be conditional on | |
5956 | __STDC__ anyway (and we've already complained about the | |
5957 | member-designator already). */ | |
5958 | if (warn_traditional && !in_system_header && !constructor_designated | |
916c5919 JM |
5959 | && !(value.value && (integer_zerop (value.value) |
5960 | || real_zerop (value.value)))) | |
3e4093b6 | 5961 | warning ("traditional C rejects initialization of unions"); |
e5e809f4 | 5962 | |
3e4093b6 | 5963 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 5964 | if (value.value != 0 |
3e4093b6 | 5965 | && fieldcode == ARRAY_TYPE |
197463ae | 5966 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 5967 | && string_flag) |
916c5919 | 5968 | value.value = orig_value; |
3e4093b6 RS |
5969 | /* Otherwise, if we have come to a subaggregate, |
5970 | and we don't have an element of its type, push into it. */ | |
916c5919 JM |
5971 | else if (value.value != 0 && !constructor_no_implicit |
5972 | && value.value != error_mark_node | |
5973 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 RS |
5974 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
5975 | || fieldcode == UNION_TYPE)) | |
5976 | { | |
5977 | push_init_level (1); | |
5978 | continue; | |
5979 | } | |
e5e809f4 | 5980 | |
916c5919 | 5981 | if (value.value) |
3e4093b6 RS |
5982 | { |
5983 | push_member_name (constructor_fields); | |
916c5919 JM |
5984 | output_init_element (value.value, strict_string, |
5985 | fieldtype, constructor_fields, 1); | |
3e4093b6 | 5986 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
5987 | } |
5988 | else | |
3e4093b6 RS |
5989 | /* Do the bookkeeping for an element that was |
5990 | directly output as a constructor. */ | |
e5e809f4 | 5991 | { |
3e4093b6 RS |
5992 | constructor_bit_index = DECL_SIZE (constructor_fields); |
5993 | constructor_unfilled_fields = TREE_CHAIN (constructor_fields); | |
e5e809f4 | 5994 | } |
e5e809f4 | 5995 | |
3e4093b6 RS |
5996 | constructor_fields = 0; |
5997 | } | |
5998 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5999 | { | |
6000 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
6001 | enum tree_code eltcode = TREE_CODE (elttype); | |
e5e809f4 | 6002 | |
3e4093b6 | 6003 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 6004 | if (value.value != 0 |
3e4093b6 | 6005 | && eltcode == ARRAY_TYPE |
197463ae | 6006 | && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) |
3e4093b6 | 6007 | && string_flag) |
916c5919 | 6008 | value.value = orig_value; |
3e4093b6 RS |
6009 | /* Otherwise, if we have come to a subaggregate, |
6010 | and we don't have an element of its type, push into it. */ | |
916c5919 JM |
6011 | else if (value.value != 0 && !constructor_no_implicit |
6012 | && value.value != error_mark_node | |
6013 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype | |
3e4093b6 RS |
6014 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE |
6015 | || eltcode == UNION_TYPE)) | |
6016 | { | |
6017 | push_init_level (1); | |
6018 | continue; | |
6019 | } | |
8b6a5902 | 6020 | |
3e4093b6 RS |
6021 | if (constructor_max_index != 0 |
6022 | && (tree_int_cst_lt (constructor_max_index, constructor_index) | |
6023 | || integer_all_onesp (constructor_max_index))) | |
6024 | { | |
6025 | pedwarn_init ("excess elements in array initializer"); | |
6026 | break; | |
6027 | } | |
8b6a5902 | 6028 | |
3e4093b6 | 6029 | /* Now output the actual element. */ |
916c5919 | 6030 | if (value.value) |
3e4093b6 RS |
6031 | { |
6032 | push_array_bounds (tree_low_cst (constructor_index, 0)); | |
916c5919 JM |
6033 | output_init_element (value.value, strict_string, |
6034 | elttype, constructor_index, 1); | |
3e4093b6 RS |
6035 | RESTORE_SPELLING_DEPTH (constructor_depth); |
6036 | } | |
2f6e4e97 | 6037 | |
3e4093b6 RS |
6038 | constructor_index |
6039 | = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node); | |
8b6a5902 | 6040 | |
916c5919 | 6041 | if (!value.value) |
3e4093b6 RS |
6042 | /* If we are doing the bookkeeping for an element that was |
6043 | directly output as a constructor, we must update | |
6044 | constructor_unfilled_index. */ | |
6045 | constructor_unfilled_index = constructor_index; | |
6046 | } | |
6047 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
6048 | { | |
6049 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8b6a5902 | 6050 | |
3e4093b6 RS |
6051 | /* Do a basic check of initializer size. Note that vectors |
6052 | always have a fixed size derived from their type. */ | |
6053 | if (tree_int_cst_lt (constructor_max_index, constructor_index)) | |
6054 | { | |
6055 | pedwarn_init ("excess elements in vector initializer"); | |
6056 | break; | |
6057 | } | |
8b6a5902 | 6058 | |
3e4093b6 | 6059 | /* Now output the actual element. */ |
916c5919 JM |
6060 | if (value.value) |
6061 | output_init_element (value.value, strict_string, | |
6062 | elttype, constructor_index, 1); | |
8b6a5902 | 6063 | |
3e4093b6 RS |
6064 | constructor_index |
6065 | = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node); | |
8b6a5902 | 6066 | |
916c5919 | 6067 | if (!value.value) |
3e4093b6 RS |
6068 | /* If we are doing the bookkeeping for an element that was |
6069 | directly output as a constructor, we must update | |
6070 | constructor_unfilled_index. */ | |
6071 | constructor_unfilled_index = constructor_index; | |
6072 | } | |
8b6a5902 | 6073 | |
3e4093b6 RS |
6074 | /* Handle the sole element allowed in a braced initializer |
6075 | for a scalar variable. */ | |
6076 | else if (constructor_fields == 0) | |
8b6a5902 | 6077 | { |
3e4093b6 RS |
6078 | pedwarn_init ("excess elements in scalar initializer"); |
6079 | break; | |
8b6a5902 JJ |
6080 | } |
6081 | else | |
6082 | { | |
916c5919 JM |
6083 | if (value.value) |
6084 | output_init_element (value.value, strict_string, | |
6085 | constructor_type, NULL_TREE, 1); | |
3e4093b6 | 6086 | constructor_fields = 0; |
8b6a5902 JJ |
6087 | } |
6088 | ||
3e4093b6 RS |
6089 | /* Handle range initializers either at this level or anywhere higher |
6090 | in the designator stack. */ | |
6091 | if (constructor_range_stack) | |
8b6a5902 | 6092 | { |
3e4093b6 RS |
6093 | struct constructor_range_stack *p, *range_stack; |
6094 | int finish = 0; | |
6095 | ||
6096 | range_stack = constructor_range_stack; | |
6097 | constructor_range_stack = 0; | |
6098 | while (constructor_stack != range_stack->stack) | |
8b6a5902 | 6099 | { |
3e4093b6 RS |
6100 | if (!constructor_stack->implicit) |
6101 | abort (); | |
6102 | process_init_element (pop_init_level (1)); | |
8b6a5902 | 6103 | } |
3e4093b6 RS |
6104 | for (p = range_stack; |
6105 | !p->range_end || tree_int_cst_equal (p->index, p->range_end); | |
6106 | p = p->prev) | |
8b6a5902 | 6107 | { |
3e4093b6 RS |
6108 | if (!constructor_stack->implicit) |
6109 | abort (); | |
6110 | process_init_element (pop_init_level (1)); | |
8b6a5902 | 6111 | } |
3e4093b6 RS |
6112 | |
6113 | p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node); | |
6114 | if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) | |
6115 | finish = 1; | |
6116 | ||
6117 | while (1) | |
6118 | { | |
6119 | constructor_index = p->index; | |
6120 | constructor_fields = p->fields; | |
6121 | if (finish && p->range_end && p->index == p->range_start) | |
6122 | { | |
6123 | finish = 0; | |
6124 | p->prev = 0; | |
6125 | } | |
6126 | p = p->next; | |
6127 | if (!p) | |
6128 | break; | |
6129 | push_init_level (2); | |
6130 | p->stack = constructor_stack; | |
6131 | if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) | |
6132 | p->index = p->range_start; | |
6133 | } | |
6134 | ||
6135 | if (!finish) | |
6136 | constructor_range_stack = range_stack; | |
6137 | continue; | |
8b6a5902 JJ |
6138 | } |
6139 | ||
3e4093b6 | 6140 | break; |
8b6a5902 JJ |
6141 | } |
6142 | ||
3e4093b6 RS |
6143 | constructor_range_stack = 0; |
6144 | } | |
6145 | \f | |
9f0e2d86 ZW |
6146 | /* Build a complete asm-statement, whose components are a CV_QUALIFIER |
6147 | (guaranteed to be 'volatile' or null) and ARGS (represented using | |
e130a54b | 6148 | an ASM_EXPR node). */ |
3e4093b6 | 6149 | tree |
9f0e2d86 | 6150 | build_asm_stmt (tree cv_qualifier, tree args) |
3e4093b6 | 6151 | { |
6de9cd9a DN |
6152 | if (!ASM_VOLATILE_P (args) && cv_qualifier) |
6153 | ASM_VOLATILE_P (args) = 1; | |
9f0e2d86 | 6154 | return add_stmt (args); |
8b6a5902 JJ |
6155 | } |
6156 | ||
9f0e2d86 ZW |
6157 | /* Build an asm-expr, whose components are a STRING, some OUTPUTS, |
6158 | some INPUTS, and some CLOBBERS. The latter three may be NULL. | |
6159 | SIMPLE indicates whether there was anything at all after the | |
6160 | string in the asm expression -- asm("blah") and asm("blah" : ) | |
e130a54b | 6161 | are subtly different. We use a ASM_EXPR node to represent this. */ |
3e4093b6 | 6162 | tree |
9f0e2d86 ZW |
6163 | build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers, |
6164 | bool simple) | |
e5e809f4 | 6165 | { |
3e4093b6 | 6166 | tree tail; |
9f0e2d86 | 6167 | tree args; |
6de9cd9a DN |
6168 | int i; |
6169 | const char *constraint; | |
6170 | bool allows_mem, allows_reg, is_inout; | |
6171 | int ninputs; | |
6172 | int noutputs; | |
6173 | ||
6174 | ninputs = list_length (inputs); | |
6175 | noutputs = list_length (outputs); | |
3e4093b6 | 6176 | |
6de9cd9a DN |
6177 | /* Remove output conversions that change the type but not the mode. */ |
6178 | for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
e5e809f4 | 6179 | { |
3e4093b6 | 6180 | tree output = TREE_VALUE (tail); |
3e4093b6 RS |
6181 | STRIP_NOPS (output); |
6182 | TREE_VALUE (tail) = output; | |
6de9cd9a | 6183 | lvalue_or_else (output, "invalid lvalue in asm statement"); |
8b6a5902 | 6184 | |
6de9cd9a | 6185 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); |
3e4093b6 | 6186 | |
6de9cd9a DN |
6187 | if (!parse_output_constraint (&constraint, i, ninputs, noutputs, |
6188 | &allows_mem, &allows_reg, &is_inout)) | |
6189 | { | |
6190 | /* By marking this operand as erroneous, we will not try | |
6191 | to process this operand again in expand_asm_operands. */ | |
6192 | TREE_VALUE (tail) = error_mark_node; | |
6193 | continue; | |
6194 | } | |
3e4093b6 | 6195 | |
6de9cd9a DN |
6196 | /* If the operand is a DECL that is going to end up in |
6197 | memory, assume it is addressable. This is a bit more | |
6198 | conservative than it would ideally be; the exact test is | |
6199 | buried deep in expand_asm_operands and depends on the | |
6200 | DECL_RTL for the OPERAND -- which we don't have at this | |
6201 | point. */ | |
6202 | if (!allows_reg && DECL_P (output)) | |
6203 | c_mark_addressable (output); | |
8b6a5902 | 6204 | } |
3e4093b6 RS |
6205 | |
6206 | /* Perform default conversions on array and function inputs. | |
6207 | Don't do this for other types as it would screw up operands | |
6208 | expected to be in memory. */ | |
6209 | for (tail = inputs; tail; tail = TREE_CHAIN (tail)) | |
6210 | TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail)); | |
6211 | ||
e130a54b | 6212 | args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers); |
9f0e2d86 ZW |
6213 | |
6214 | /* Simple asm statements are treated as volatile. */ | |
6215 | if (simple) | |
6216 | { | |
6de9cd9a | 6217 | ASM_VOLATILE_P (args) = 1; |
9f0e2d86 ZW |
6218 | ASM_INPUT_P (args) = 1; |
6219 | } | |
6220 | return args; | |
e5e809f4 | 6221 | } |
3e4093b6 | 6222 | \f |
506e2710 RH |
6223 | /* Generate a goto statement to LABEL. */ |
6224 | ||
6225 | tree | |
6226 | c_finish_goto_label (tree label) | |
6227 | { | |
6228 | tree decl = lookup_label (label); | |
6229 | if (!decl) | |
6230 | return NULL_TREE; | |
6231 | ||
6232 | TREE_USED (decl) = 1; | |
53fb4de3 | 6233 | return add_stmt (build1 (GOTO_EXPR, void_type_node, decl)); |
506e2710 RH |
6234 | } |
6235 | ||
6236 | /* Generate a computed goto statement to EXPR. */ | |
6237 | ||
6238 | tree | |
6239 | c_finish_goto_ptr (tree expr) | |
6240 | { | |
6241 | if (pedantic) | |
6242 | pedwarn ("ISO C forbids `goto *expr;'"); | |
6243 | expr = convert (ptr_type_node, expr); | |
53fb4de3 | 6244 | return add_stmt (build1 (GOTO_EXPR, void_type_node, expr)); |
506e2710 RH |
6245 | } |
6246 | ||
5088b058 RH |
6247 | /* Generate a C `return' statement. RETVAL is the expression for what |
6248 | to return, or a null pointer for `return;' with no value. */ | |
de520661 | 6249 | |
506e2710 | 6250 | tree |
5088b058 | 6251 | c_finish_return (tree retval) |
3e4093b6 RS |
6252 | { |
6253 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)); | |
6254 | ||
6255 | if (TREE_THIS_VOLATILE (current_function_decl)) | |
6256 | warning ("function declared `noreturn' has a `return' statement"); | |
6257 | ||
6258 | if (!retval) | |
de520661 | 6259 | { |
3e4093b6 RS |
6260 | current_function_returns_null = 1; |
6261 | if ((warn_return_type || flag_isoc99) | |
6262 | && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
6263 | pedwarn_c99 ("`return' with no value, in function returning non-void"); | |
400fbf9f | 6264 | } |
3e4093b6 | 6265 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) |
de520661 | 6266 | { |
3e4093b6 RS |
6267 | current_function_returns_null = 1; |
6268 | if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) | |
6269 | pedwarn ("`return' with a value, in function returning void"); | |
de520661 | 6270 | } |
3e4093b6 | 6271 | else |
de520661 | 6272 | { |
3e4093b6 RS |
6273 | tree t = convert_for_assignment (valtype, retval, _("return"), |
6274 | NULL_TREE, NULL_TREE, 0); | |
6275 | tree res = DECL_RESULT (current_function_decl); | |
6276 | tree inner; | |
6277 | ||
6278 | current_function_returns_value = 1; | |
6279 | if (t == error_mark_node) | |
506e2710 | 6280 | return NULL_TREE; |
3e4093b6 RS |
6281 | |
6282 | inner = t = convert (TREE_TYPE (res), t); | |
6283 | ||
6284 | /* Strip any conversions, additions, and subtractions, and see if | |
6285 | we are returning the address of a local variable. Warn if so. */ | |
6286 | while (1) | |
8b6a5902 | 6287 | { |
3e4093b6 | 6288 | switch (TREE_CODE (inner)) |
8b6a5902 | 6289 | { |
3e4093b6 RS |
6290 | case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR: |
6291 | case PLUS_EXPR: | |
6292 | inner = TREE_OPERAND (inner, 0); | |
6293 | continue; | |
6294 | ||
6295 | case MINUS_EXPR: | |
6296 | /* If the second operand of the MINUS_EXPR has a pointer | |
6297 | type (or is converted from it), this may be valid, so | |
6298 | don't give a warning. */ | |
6299 | { | |
6300 | tree op1 = TREE_OPERAND (inner, 1); | |
8b6a5902 | 6301 | |
3e4093b6 RS |
6302 | while (! POINTER_TYPE_P (TREE_TYPE (op1)) |
6303 | && (TREE_CODE (op1) == NOP_EXPR | |
6304 | || TREE_CODE (op1) == NON_LVALUE_EXPR | |
6305 | || TREE_CODE (op1) == CONVERT_EXPR)) | |
6306 | op1 = TREE_OPERAND (op1, 0); | |
8b6a5902 | 6307 | |
3e4093b6 RS |
6308 | if (POINTER_TYPE_P (TREE_TYPE (op1))) |
6309 | break; | |
8b6a5902 | 6310 | |
3e4093b6 RS |
6311 | inner = TREE_OPERAND (inner, 0); |
6312 | continue; | |
6313 | } | |
400fbf9f | 6314 | |
3e4093b6 RS |
6315 | case ADDR_EXPR: |
6316 | inner = TREE_OPERAND (inner, 0); | |
c2f4acb7 | 6317 | |
9fc3b39a AP |
6318 | while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r' |
6319 | && TREE_CODE (inner) != INDIRECT_REF) | |
3e4093b6 | 6320 | inner = TREE_OPERAND (inner, 0); |
8b6a5902 | 6321 | |
a2f1f4c3 | 6322 | if (DECL_P (inner) |
3e4093b6 RS |
6323 | && ! DECL_EXTERNAL (inner) |
6324 | && ! TREE_STATIC (inner) | |
6325 | && DECL_CONTEXT (inner) == current_function_decl) | |
6326 | warning ("function returns address of local variable"); | |
6327 | break; | |
8b6a5902 | 6328 | |
3e4093b6 RS |
6329 | default: |
6330 | break; | |
6331 | } | |
de520661 | 6332 | |
3e4093b6 RS |
6333 | break; |
6334 | } | |
6335 | ||
53fb4de3 | 6336 | retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); |
de520661 | 6337 | } |
8b6a5902 | 6338 | |
506e2710 | 6339 | return add_stmt (build_stmt (RETURN_EXPR, retval)); |
de520661 | 6340 | } |
3e4093b6 RS |
6341 | \f |
6342 | struct c_switch { | |
6343 | /* The SWITCH_STMT being built. */ | |
6344 | tree switch_stmt; | |
a6c0a76c SB |
6345 | |
6346 | /* The original type of the testing expression, ie. before the | |
6347 | default conversion is applied. */ | |
6348 | tree orig_type; | |
6349 | ||
3e4093b6 RS |
6350 | /* A splay-tree mapping the low element of a case range to the high |
6351 | element, or NULL_TREE if there is no high element. Used to | |
6352 | determine whether or not a new case label duplicates an old case | |
6353 | label. We need a tree, rather than simply a hash table, because | |
6354 | of the GNU case range extension. */ | |
6355 | splay_tree cases; | |
a6c0a76c | 6356 | |
3e4093b6 RS |
6357 | /* The next node on the stack. */ |
6358 | struct c_switch *next; | |
6359 | }; | |
400fbf9f | 6360 | |
3e4093b6 RS |
6361 | /* A stack of the currently active switch statements. The innermost |
6362 | switch statement is on the top of the stack. There is no need to | |
6363 | mark the stack for garbage collection because it is only active | |
6364 | during the processing of the body of a function, and we never | |
6365 | collect at that point. */ | |
de520661 | 6366 | |
506e2710 | 6367 | struct c_switch *c_switch_stack; |
de520661 | 6368 | |
3e4093b6 RS |
6369 | /* Start a C switch statement, testing expression EXP. Return the new |
6370 | SWITCH_STMT. */ | |
de520661 | 6371 | |
3e4093b6 RS |
6372 | tree |
6373 | c_start_case (tree exp) | |
de520661 | 6374 | { |
3e4093b6 RS |
6375 | enum tree_code code; |
6376 | tree type, orig_type = error_mark_node; | |
6377 | struct c_switch *cs; | |
2f6e4e97 | 6378 | |
3e4093b6 | 6379 | if (exp != error_mark_node) |
de520661 | 6380 | { |
3e4093b6 RS |
6381 | code = TREE_CODE (TREE_TYPE (exp)); |
6382 | orig_type = TREE_TYPE (exp); | |
6383 | ||
6384 | if (! INTEGRAL_TYPE_P (orig_type) | |
6385 | && code != ERROR_MARK) | |
de520661 | 6386 | { |
3e4093b6 RS |
6387 | error ("switch quantity not an integer"); |
6388 | exp = integer_zero_node; | |
de520661 | 6389 | } |
3e4093b6 | 6390 | else |
de520661 | 6391 | { |
3e4093b6 | 6392 | type = TYPE_MAIN_VARIANT (TREE_TYPE (exp)); |
8b6a5902 | 6393 | |
3e4093b6 RS |
6394 | if (warn_traditional && !in_system_header |
6395 | && (type == long_integer_type_node | |
6396 | || type == long_unsigned_type_node)) | |
6397 | warning ("`long' switch expression not converted to `int' in ISO C"); | |
8b6a5902 | 6398 | |
3e4093b6 RS |
6399 | exp = default_conversion (exp); |
6400 | type = TREE_TYPE (exp); | |
6401 | } | |
6402 | } | |
6403 | ||
6404 | /* Add this new SWITCH_STMT to the stack. */ | |
5d038c4c BI |
6405 | cs = XNEW (struct c_switch); |
6406 | cs->switch_stmt = build_stmt ((enum tree_code) SWITCH_STMT, exp, NULL_TREE, | |
6407 | orig_type); | |
a6c0a76c | 6408 | cs->orig_type = orig_type; |
3e4093b6 | 6409 | cs->cases = splay_tree_new (case_compare, NULL, NULL); |
506e2710 RH |
6410 | cs->next = c_switch_stack; |
6411 | c_switch_stack = cs; | |
3e4093b6 | 6412 | |
506e2710 | 6413 | return add_stmt (cs->switch_stmt); |
3e4093b6 RS |
6414 | } |
6415 | ||
6416 | /* Process a case label. */ | |
6417 | ||
6418 | tree | |
6419 | do_case (tree low_value, tree high_value) | |
6420 | { | |
6421 | tree label = NULL_TREE; | |
6422 | ||
506e2710 | 6423 | if (c_switch_stack) |
3e4093b6 | 6424 | { |
506e2710 RH |
6425 | label = c_add_case_label (c_switch_stack->cases, |
6426 | SWITCH_COND (c_switch_stack->switch_stmt), | |
a6c0a76c | 6427 | c_switch_stack->orig_type, |
3e4093b6 RS |
6428 | low_value, high_value); |
6429 | if (label == error_mark_node) | |
6430 | label = NULL_TREE; | |
de520661 | 6431 | } |
3e4093b6 RS |
6432 | else if (low_value) |
6433 | error ("case label not within a switch statement"); | |
6434 | else | |
6435 | error ("`default' label not within a switch statement"); | |
de520661 | 6436 | |
3e4093b6 RS |
6437 | return label; |
6438 | } | |
de520661 | 6439 | |
3e4093b6 | 6440 | /* Finish the switch statement. */ |
de520661 | 6441 | |
3e4093b6 | 6442 | void |
325c3691 | 6443 | c_finish_case (tree body) |
3e4093b6 | 6444 | { |
506e2710 | 6445 | struct c_switch *cs = c_switch_stack; |
3e4093b6 | 6446 | |
325c3691 RH |
6447 | SWITCH_BODY (cs->switch_stmt) = body; |
6448 | ||
6de9cd9a DN |
6449 | /* Emit warnings as needed. */ |
6450 | c_do_switch_warnings (cs->cases, cs->switch_stmt); | |
6451 | ||
3e4093b6 | 6452 | /* Pop the stack. */ |
506e2710 | 6453 | c_switch_stack = cs->next; |
3e4093b6 | 6454 | splay_tree_delete (cs->cases); |
5d038c4c | 6455 | XDELETE (cs); |
de520661 | 6456 | } |
325c3691 | 6457 | \f |
506e2710 RH |
6458 | /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, |
6459 | THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK | |
6460 | may be null. NESTED_IF is true if THEN_BLOCK contains another IF | |
6461 | statement, and was not surrounded with parenthesis. */ | |
325c3691 | 6462 | |
9e51cf9d | 6463 | void |
506e2710 RH |
6464 | c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, |
6465 | tree else_block, bool nested_if) | |
325c3691 | 6466 | { |
506e2710 | 6467 | tree stmt; |
325c3691 | 6468 | |
506e2710 RH |
6469 | /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */ |
6470 | if (warn_parentheses && nested_if && else_block == NULL) | |
325c3691 | 6471 | { |
506e2710 | 6472 | tree inner_if = then_block; |
16865eaa | 6473 | |
61ada8ae | 6474 | /* We know from the grammar productions that there is an IF nested |
506e2710 RH |
6475 | within THEN_BLOCK. Due to labels and c99 conditional declarations, |
6476 | it might not be exactly THEN_BLOCK, but should be the last | |
6477 | non-container statement within. */ | |
6478 | while (1) | |
6479 | switch (TREE_CODE (inner_if)) | |
6480 | { | |
6481 | case COND_EXPR: | |
6482 | goto found; | |
6483 | case BIND_EXPR: | |
6484 | inner_if = BIND_EXPR_BODY (inner_if); | |
6485 | break; | |
6486 | case STATEMENT_LIST: | |
6487 | inner_if = expr_last (then_block); | |
6488 | break; | |
6489 | case TRY_FINALLY_EXPR: | |
6490 | case TRY_CATCH_EXPR: | |
6491 | inner_if = TREE_OPERAND (inner_if, 0); | |
6492 | break; | |
6493 | default: | |
6494 | abort (); | |
6495 | } | |
6496 | found: | |
16865eaa | 6497 | |
506e2710 RH |
6498 | if (COND_EXPR_ELSE (inner_if)) |
6499 | warning ("%Hsuggest explicit braces to avoid ambiguous `else'", | |
6500 | &if_locus); | |
6501 | } | |
16865eaa | 6502 | |
506e2710 RH |
6503 | /* Diagnose ";" via the special empty statement node that we create. */ |
6504 | if (extra_warnings) | |
16865eaa | 6505 | { |
506e2710 RH |
6506 | if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block)) |
6507 | { | |
6508 | if (!else_block) | |
6509 | warning ("%Hempty body in an if-statement", | |
6510 | EXPR_LOCUS (then_block)); | |
6511 | then_block = alloc_stmt_list (); | |
6512 | } | |
6513 | if (else_block | |
6514 | && TREE_CODE (else_block) == NOP_EXPR | |
6515 | && !TREE_TYPE (else_block)) | |
6516 | { | |
6517 | warning ("%Hempty body in an else-statement", | |
6518 | EXPR_LOCUS (else_block)); | |
6519 | else_block = alloc_stmt_list (); | |
6520 | } | |
16865eaa | 6521 | } |
325c3691 | 6522 | |
506e2710 | 6523 | stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block); |
a281759f | 6524 | SET_EXPR_LOCATION (stmt, if_locus); |
506e2710 | 6525 | add_stmt (stmt); |
325c3691 RH |
6526 | } |
6527 | ||
506e2710 RH |
6528 | /* Emit a general-purpose loop construct. START_LOCUS is the location of |
6529 | the beginning of the loop. COND is the loop condition. COND_IS_FIRST | |
6530 | is false for DO loops. INCR is the FOR increment expression. BODY is | |
61ada8ae | 6531 | the statement controlled by the loop. BLAB is the break label. CLAB is |
506e2710 | 6532 | the continue label. Everything is allowed to be NULL. */ |
325c3691 RH |
6533 | |
6534 | void | |
506e2710 RH |
6535 | c_finish_loop (location_t start_locus, tree cond, tree incr, tree body, |
6536 | tree blab, tree clab, bool cond_is_first) | |
325c3691 | 6537 | { |
506e2710 RH |
6538 | tree entry = NULL, exit = NULL, t; |
6539 | ||
506e2710 | 6540 | /* Detect do { ... } while (0) and don't generate loop construct. */ |
1ec7a978 RH |
6541 | if (cond && !cond_is_first && integer_zerop (cond)) |
6542 | cond = NULL; | |
506e2710 RH |
6543 | if (cond_is_first || cond) |
6544 | { | |
6545 | tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
6546 | ||
6547 | /* If we have an exit condition, then we build an IF with gotos either | |
6548 | out of the loop, or to the top of it. If there's no exit condition, | |
6549 | then we just build a jump back to the top. */ | |
6550 | exit = build_and_jump (&LABEL_EXPR_LABEL (top)); | |
6551 | ||
6552 | if (cond) | |
6553 | { | |
6554 | /* Canonicalize the loop condition to the end. This means | |
6555 | generating a branch to the loop condition. Reuse the | |
6556 | continue label, if possible. */ | |
6557 | if (cond_is_first) | |
6558 | { | |
6559 | if (incr || !clab) | |
6560 | { | |
6561 | entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
6562 | t = build_and_jump (&LABEL_EXPR_LABEL (entry)); | |
6563 | } | |
6564 | else | |
6565 | t = build1 (GOTO_EXPR, void_type_node, clab); | |
a281759f | 6566 | SET_EXPR_LOCATION (t, start_locus); |
506e2710 RH |
6567 | add_stmt (t); |
6568 | } | |
6569 | ||
6570 | t = build_and_jump (&blab); | |
53fb4de3 | 6571 | exit = build3 (COND_EXPR, void_type_node, cond, exit, t); |
506e2710 RH |
6572 | exit = fold (exit); |
6573 | if (cond_is_first) | |
a281759f | 6574 | SET_EXPR_LOCATION (exit, start_locus); |
506e2710 | 6575 | else |
a281759f | 6576 | SET_EXPR_LOCATION (exit, input_location); |
506e2710 RH |
6577 | } |
6578 | ||
6579 | add_stmt (top); | |
6580 | } | |
6581 | ||
6582 | if (body) | |
6583 | add_stmt (body); | |
6584 | if (clab) | |
6585 | add_stmt (build1 (LABEL_EXPR, void_type_node, clab)); | |
6586 | if (incr) | |
6587 | add_stmt (incr); | |
6588 | if (entry) | |
6589 | add_stmt (entry); | |
6590 | if (exit) | |
6591 | add_stmt (exit); | |
6592 | if (blab) | |
6593 | add_stmt (build1 (LABEL_EXPR, void_type_node, blab)); | |
325c3691 | 6594 | } |
325c3691 RH |
6595 | |
6596 | tree | |
506e2710 | 6597 | c_finish_bc_stmt (tree *label_p, bool is_break) |
325c3691 | 6598 | { |
506e2710 | 6599 | tree label = *label_p; |
325c3691 | 6600 | |
506e2710 RH |
6601 | if (!label) |
6602 | *label_p = label = create_artificial_label (); | |
6603 | else if (TREE_CODE (label) != LABEL_DECL) | |
6604 | { | |
6605 | if (is_break) | |
6606 | error ("break statement not within loop or switch"); | |
6607 | else | |
6608 | error ("continue statement not within a loop"); | |
6609 | return NULL_TREE; | |
6610 | } | |
325c3691 | 6611 | |
53fb4de3 | 6612 | return add_stmt (build1 (GOTO_EXPR, void_type_node, label)); |
325c3691 RH |
6613 | } |
6614 | ||
506e2710 | 6615 | /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ |
3a5b9284 RH |
6616 | |
6617 | static void | |
6618 | emit_side_effect_warnings (tree expr) | |
6619 | { | |
e6b5a630 RH |
6620 | if (expr == error_mark_node) |
6621 | ; | |
6622 | else if (!TREE_SIDE_EFFECTS (expr)) | |
3a5b9284 RH |
6623 | { |
6624 | if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr)) | |
6625 | warning ("%Hstatement with no effect", | |
607bdeaa | 6626 | EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location); |
3a5b9284 RH |
6627 | } |
6628 | else if (warn_unused_value) | |
6629 | warn_if_unused_value (expr, input_location); | |
6630 | } | |
6631 | ||
506e2710 RH |
6632 | /* Process an expression as if it were a complete statement. Emit |
6633 | diagnostics, but do not call ADD_STMT. */ | |
3a5b9284 | 6634 | |
506e2710 RH |
6635 | tree |
6636 | c_process_expr_stmt (tree expr) | |
3a5b9284 RH |
6637 | { |
6638 | if (!expr) | |
506e2710 | 6639 | return NULL_TREE; |
3a5b9284 RH |
6640 | |
6641 | /* Do default conversion if safe and possibly important, | |
6642 | in case within ({...}). */ | |
6643 | if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE | |
6644 | && (flag_isoc99 || lvalue_p (expr))) | |
6645 | || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE) | |
6646 | expr = default_conversion (expr); | |
6647 | ||
6648 | if (warn_sequence_point) | |
6649 | verify_sequence_points (expr); | |
6650 | ||
6651 | if (TREE_TYPE (expr) != error_mark_node | |
6652 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) | |
6653 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) | |
6654 | error ("expression statement has incomplete type"); | |
6655 | ||
6656 | /* If we're not processing a statement expression, warn about unused values. | |
6657 | Warnings for statement expressions will be emitted later, once we figure | |
6658 | out which is the result. */ | |
6659 | if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
6660 | && (extra_warnings || warn_unused_value)) | |
6661 | emit_side_effect_warnings (expr); | |
6662 | ||
6663 | /* If the expression is not of a type to which we cannot assign a line | |
6664 | number, wrap the thing in a no-op NOP_EXPR. */ | |
6665 | if (DECL_P (expr) || TREE_CODE_CLASS (TREE_CODE (expr)) == 'c') | |
6666 | expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
6667 | ||
506e2710 | 6668 | if (EXPR_P (expr)) |
a281759f | 6669 | SET_EXPR_LOCATION (expr, input_location); |
506e2710 RH |
6670 | |
6671 | return expr; | |
6672 | } | |
6673 | ||
6674 | /* Emit an expression as a statement. */ | |
6675 | ||
6676 | tree | |
6677 | c_finish_expr_stmt (tree expr) | |
6678 | { | |
6679 | if (expr) | |
6680 | return add_stmt (c_process_expr_stmt (expr)); | |
6681 | else | |
6682 | return NULL; | |
3a5b9284 RH |
6683 | } |
6684 | ||
6685 | /* Do the opposite and emit a statement as an expression. To begin, | |
6686 | create a new binding level and return it. */ | |
325c3691 RH |
6687 | |
6688 | tree | |
6689 | c_begin_stmt_expr (void) | |
6690 | { | |
6691 | tree ret; | |
6692 | ||
6693 | /* We must force a BLOCK for this level so that, if it is not expanded | |
6694 | later, there is a way to turn off the entire subtree of blocks that | |
6695 | are contained in it. */ | |
6696 | keep_next_level (); | |
6697 | ret = c_begin_compound_stmt (true); | |
6698 | ||
6699 | /* Mark the current statement list as belonging to a statement list. */ | |
6700 | STATEMENT_LIST_STMT_EXPR (ret) = 1; | |
6701 | ||
6702 | return ret; | |
6703 | } | |
6704 | ||
6705 | tree | |
6706 | c_finish_stmt_expr (tree body) | |
6707 | { | |
3a5b9284 | 6708 | tree last, type, tmp, val; |
325c3691 RH |
6709 | tree *last_p; |
6710 | ||
6711 | body = c_end_compound_stmt (body, true); | |
6712 | ||
3a5b9284 RH |
6713 | /* Locate the last statement in BODY. See c_end_compound_stmt |
6714 | about always returning a BIND_EXPR. */ | |
6715 | last_p = &BIND_EXPR_BODY (body); | |
6716 | last = BIND_EXPR_BODY (body); | |
6717 | ||
6718 | continue_searching: | |
325c3691 RH |
6719 | if (TREE_CODE (last) == STATEMENT_LIST) |
6720 | { | |
3a5b9284 RH |
6721 | tree_stmt_iterator i; |
6722 | ||
6723 | /* This can happen with degenerate cases like ({ }). No value. */ | |
6724 | if (!TREE_SIDE_EFFECTS (last)) | |
6725 | return body; | |
6726 | ||
6727 | /* If we're supposed to generate side effects warnings, process | |
6728 | all of the statements except the last. */ | |
6729 | if (extra_warnings || warn_unused_value) | |
325c3691 | 6730 | { |
3a5b9284 RH |
6731 | for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i)) |
6732 | emit_side_effect_warnings (tsi_stmt (i)); | |
325c3691 RH |
6733 | } |
6734 | else | |
3a5b9284 RH |
6735 | i = tsi_last (last); |
6736 | last_p = tsi_stmt_ptr (i); | |
6737 | last = *last_p; | |
325c3691 RH |
6738 | } |
6739 | ||
3a5b9284 RH |
6740 | /* If the end of the list is exception related, then the list was split |
6741 | by a call to push_cleanup. Continue searching. */ | |
6742 | if (TREE_CODE (last) == TRY_FINALLY_EXPR | |
6743 | || TREE_CODE (last) == TRY_CATCH_EXPR) | |
6744 | { | |
6745 | last_p = &TREE_OPERAND (last, 0); | |
6746 | last = *last_p; | |
6747 | goto continue_searching; | |
6748 | } | |
6749 | ||
6750 | /* In the case that the BIND_EXPR is not necessary, return the | |
6751 | expression out from inside it. */ | |
e6b5a630 RH |
6752 | if (last == error_mark_node |
6753 | || (last == BIND_EXPR_BODY (body) | |
6754 | && BIND_EXPR_VARS (body) == NULL)) | |
3a5b9284 | 6755 | return last; |
325c3691 RH |
6756 | |
6757 | /* Extract the type of said expression. */ | |
6758 | type = TREE_TYPE (last); | |
325c3691 | 6759 | |
3a5b9284 RH |
6760 | /* If we're not returning a value at all, then the BIND_EXPR that |
6761 | we already have is a fine expression to return. */ | |
6762 | if (!type || VOID_TYPE_P (type)) | |
6763 | return body; | |
6764 | ||
6765 | /* Now that we've located the expression containing the value, it seems | |
6766 | silly to make voidify_wrapper_expr repeat the process. Create a | |
6767 | temporary of the appropriate type and stick it in a TARGET_EXPR. */ | |
6768 | tmp = create_tmp_var_raw (type, NULL); | |
6769 | ||
6770 | /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids | |
6771 | tree_expr_nonnegative_p giving up immediately. */ | |
6772 | val = last; | |
6773 | if (TREE_CODE (val) == NOP_EXPR | |
6774 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) | |
6775 | val = TREE_OPERAND (val, 0); | |
6776 | ||
53fb4de3 | 6777 | *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); |
3a5b9284 RH |
6778 | SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last)); |
6779 | ||
53fb4de3 | 6780 | return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); |
325c3691 RH |
6781 | } |
6782 | \f | |
6783 | /* Begin and end compound statements. This is as simple as pushing | |
6784 | and popping new statement lists from the tree. */ | |
6785 | ||
6786 | tree | |
6787 | c_begin_compound_stmt (bool do_scope) | |
6788 | { | |
6789 | tree stmt = push_stmt_list (); | |
6790 | if (do_scope) | |
4dfa0342 | 6791 | push_scope (); |
325c3691 RH |
6792 | return stmt; |
6793 | } | |
6794 | ||
6795 | tree | |
6796 | c_end_compound_stmt (tree stmt, bool do_scope) | |
6797 | { | |
6798 | tree block = NULL; | |
6799 | ||
6800 | if (do_scope) | |
6801 | { | |
6802 | if (c_dialect_objc ()) | |
6803 | objc_clear_super_receiver (); | |
6804 | block = pop_scope (); | |
6805 | } | |
6806 | ||
6807 | stmt = pop_stmt_list (stmt); | |
6808 | stmt = c_build_bind_expr (block, stmt); | |
6809 | ||
6810 | /* If this compound statement is nested immediately inside a statement | |
6811 | expression, then force a BIND_EXPR to be created. Otherwise we'll | |
6812 | do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, | |
6813 | STATEMENT_LISTs merge, and thus we can lose track of what statement | |
6814 | was really last. */ | |
6815 | if (cur_stmt_list | |
6816 | && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
6817 | && TREE_CODE (stmt) != BIND_EXPR) | |
6818 | { | |
53fb4de3 | 6819 | stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); |
325c3691 RH |
6820 | TREE_SIDE_EFFECTS (stmt) = 1; |
6821 | } | |
6822 | ||
6823 | return stmt; | |
6824 | } | |
5a508662 RH |
6825 | |
6826 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed | |
6827 | when the current scope is exited. EH_ONLY is true when this is not | |
6828 | meant to apply to normal control flow transfer. */ | |
6829 | ||
6830 | void | |
e18476eb | 6831 | push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only) |
5a508662 | 6832 | { |
3a5b9284 RH |
6833 | enum tree_code code; |
6834 | tree stmt, list; | |
6835 | bool stmt_expr; | |
6836 | ||
6837 | code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; | |
6838 | stmt = build_stmt (code, NULL, cleanup); | |
5a508662 | 6839 | add_stmt (stmt); |
3a5b9284 RH |
6840 | stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); |
6841 | list = push_stmt_list (); | |
6842 | TREE_OPERAND (stmt, 0) = list; | |
6843 | STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; | |
5a508662 | 6844 | } |
325c3691 | 6845 | \f |
3e4093b6 RS |
6846 | /* Build a binary-operation expression without default conversions. |
6847 | CODE is the kind of expression to build. | |
6848 | This function differs from `build' in several ways: | |
6849 | the data type of the result is computed and recorded in it, | |
6850 | warnings are generated if arg data types are invalid, | |
6851 | special handling for addition and subtraction of pointers is known, | |
6852 | and some optimization is done (operations on narrow ints | |
6853 | are done in the narrower type when that gives the same result). | |
6854 | Constant folding is also done before the result is returned. | |
de520661 | 6855 | |
3e4093b6 RS |
6856 | Note that the operands will never have enumeral types, or function |
6857 | or array types, because either they will have the default conversions | |
6858 | performed or they have both just been converted to some other type in which | |
6859 | the arithmetic is to be done. */ | |
6860 | ||
6861 | tree | |
6862 | build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1, | |
6863 | int convert_p) | |
de520661 | 6864 | { |
3e4093b6 RS |
6865 | tree type0, type1; |
6866 | enum tree_code code0, code1; | |
6867 | tree op0, op1; | |
b62acd60 | 6868 | |
3e4093b6 RS |
6869 | /* Expression code to give to the expression when it is built. |
6870 | Normally this is CODE, which is what the caller asked for, | |
6871 | but in some special cases we change it. */ | |
6872 | enum tree_code resultcode = code; | |
8b6a5902 | 6873 | |
3e4093b6 RS |
6874 | /* Data type in which the computation is to be performed. |
6875 | In the simplest cases this is the common type of the arguments. */ | |
6876 | tree result_type = NULL; | |
6877 | ||
6878 | /* Nonzero means operands have already been type-converted | |
6879 | in whatever way is necessary. | |
6880 | Zero means they need to be converted to RESULT_TYPE. */ | |
6881 | int converted = 0; | |
6882 | ||
6883 | /* Nonzero means create the expression with this type, rather than | |
6884 | RESULT_TYPE. */ | |
6885 | tree build_type = 0; | |
6886 | ||
6887 | /* Nonzero means after finally constructing the expression | |
6888 | convert it to this type. */ | |
6889 | tree final_type = 0; | |
6890 | ||
6891 | /* Nonzero if this is an operation like MIN or MAX which can | |
6892 | safely be computed in short if both args are promoted shorts. | |
6893 | Also implies COMMON. | |
6894 | -1 indicates a bitwise operation; this makes a difference | |
6895 | in the exact conditions for when it is safe to do the operation | |
6896 | in a narrower mode. */ | |
6897 | int shorten = 0; | |
6898 | ||
6899 | /* Nonzero if this is a comparison operation; | |
6900 | if both args are promoted shorts, compare the original shorts. | |
6901 | Also implies COMMON. */ | |
6902 | int short_compare = 0; | |
6903 | ||
6904 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
6905 | original short and then promoted if the operand is a promoted short. */ | |
6906 | int short_shift = 0; | |
6907 | ||
6908 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
6909 | int common = 0; | |
6910 | ||
6911 | if (convert_p) | |
790e9490 | 6912 | { |
3e4093b6 RS |
6913 | op0 = default_conversion (orig_op0); |
6914 | op1 = default_conversion (orig_op1); | |
790e9490 | 6915 | } |
3e4093b6 | 6916 | else |
790e9490 | 6917 | { |
3e4093b6 RS |
6918 | op0 = orig_op0; |
6919 | op1 = orig_op1; | |
790e9490 RS |
6920 | } |
6921 | ||
3e4093b6 RS |
6922 | type0 = TREE_TYPE (op0); |
6923 | type1 = TREE_TYPE (op1); | |
91fa3c30 | 6924 | |
3e4093b6 RS |
6925 | /* The expression codes of the data types of the arguments tell us |
6926 | whether the arguments are integers, floating, pointers, etc. */ | |
6927 | code0 = TREE_CODE (type0); | |
6928 | code1 = TREE_CODE (type1); | |
6929 | ||
6930 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
6931 | STRIP_TYPE_NOPS (op0); | |
6932 | STRIP_TYPE_NOPS (op1); | |
6933 | ||
6934 | /* If an error was already reported for one of the arguments, | |
6935 | avoid reporting another error. */ | |
6936 | ||
6937 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
6938 | return error_mark_node; | |
6939 | ||
6940 | switch (code) | |
de520661 | 6941 | { |
3e4093b6 RS |
6942 | case PLUS_EXPR: |
6943 | /* Handle the pointer + int case. */ | |
6944 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
6945 | return pointer_int_sum (PLUS_EXPR, op0, op1); | |
6946 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) | |
6947 | return pointer_int_sum (PLUS_EXPR, op1, op0); | |
fe67cf58 | 6948 | else |
3e4093b6 RS |
6949 | common = 1; |
6950 | break; | |
400fbf9f | 6951 | |
3e4093b6 RS |
6952 | case MINUS_EXPR: |
6953 | /* Subtraction of two similar pointers. | |
6954 | We must subtract them as integers, then divide by object size. */ | |
6955 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
6956 | && comp_target_types (type0, type1, 1)) | |
6957 | return pointer_diff (op0, op1); | |
6958 | /* Handle pointer minus int. Just like pointer plus int. */ | |
6959 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
6960 | return pointer_int_sum (MINUS_EXPR, op0, op1); | |
6961 | else | |
6962 | common = 1; | |
6963 | break; | |
8b6a5902 | 6964 | |
3e4093b6 RS |
6965 | case MULT_EXPR: |
6966 | common = 1; | |
6967 | break; | |
6968 | ||
6969 | case TRUNC_DIV_EXPR: | |
6970 | case CEIL_DIV_EXPR: | |
6971 | case FLOOR_DIV_EXPR: | |
6972 | case ROUND_DIV_EXPR: | |
6973 | case EXACT_DIV_EXPR: | |
6974 | /* Floating point division by zero is a legitimate way to obtain | |
6975 | infinities and NaNs. */ | |
6976 | if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1)) | |
6977 | warning ("division by zero"); | |
6978 | ||
6979 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
6980 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) | |
6981 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
6982 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) | |
400fbf9f | 6983 | { |
3a021db2 PB |
6984 | if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
6985 | code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); | |
6986 | if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) | |
6987 | code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); | |
6988 | ||
3e4093b6 RS |
6989 | if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)) |
6990 | resultcode = RDIV_EXPR; | |
6991 | else | |
6992 | /* Although it would be tempting to shorten always here, that | |
6993 | loses on some targets, since the modulo instruction is | |
6994 | undefined if the quotient can't be represented in the | |
6995 | computation mode. We shorten only if unsigned or if | |
6996 | dividing by something we know != -1. */ | |
8df83eae | 6997 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 RS |
6998 | || (TREE_CODE (op1) == INTEGER_CST |
6999 | && ! integer_all_onesp (op1))); | |
7000 | common = 1; | |
7001 | } | |
7002 | break; | |
de520661 | 7003 | |
3e4093b6 | 7004 | case BIT_AND_EXPR: |
3e4093b6 RS |
7005 | case BIT_IOR_EXPR: |
7006 | case BIT_XOR_EXPR: | |
7007 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
7008 | shorten = -1; | |
7009 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) | |
7010 | common = 1; | |
7011 | break; | |
7012 | ||
7013 | case TRUNC_MOD_EXPR: | |
7014 | case FLOOR_MOD_EXPR: | |
7015 | if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1)) | |
7016 | warning ("division by zero"); | |
de520661 | 7017 | |
3e4093b6 RS |
7018 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
7019 | { | |
7020 | /* Although it would be tempting to shorten always here, that loses | |
7021 | on some targets, since the modulo instruction is undefined if the | |
7022 | quotient can't be represented in the computation mode. We shorten | |
7023 | only if unsigned or if dividing by something we know != -1. */ | |
8df83eae | 7024 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 RS |
7025 | || (TREE_CODE (op1) == INTEGER_CST |
7026 | && ! integer_all_onesp (op1))); | |
7027 | common = 1; | |
7028 | } | |
7029 | break; | |
de520661 | 7030 | |
3e4093b6 RS |
7031 | case TRUTH_ANDIF_EXPR: |
7032 | case TRUTH_ORIF_EXPR: | |
7033 | case TRUTH_AND_EXPR: | |
7034 | case TRUTH_OR_EXPR: | |
7035 | case TRUTH_XOR_EXPR: | |
7036 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE | |
7037 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE) | |
7038 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE | |
7039 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE)) | |
7040 | { | |
7041 | /* Result of these operations is always an int, | |
7042 | but that does not mean the operands should be | |
7043 | converted to ints! */ | |
7044 | result_type = integer_type_node; | |
ae2bcd98 RS |
7045 | op0 = lang_hooks.truthvalue_conversion (op0); |
7046 | op1 = lang_hooks.truthvalue_conversion (op1); | |
3e4093b6 RS |
7047 | converted = 1; |
7048 | } | |
7049 | break; | |
eba80994 | 7050 | |
3e4093b6 RS |
7051 | /* Shift operations: result has same type as first operand; |
7052 | always convert second operand to int. | |
7053 | Also set SHORT_SHIFT if shifting rightward. */ | |
de520661 | 7054 | |
3e4093b6 RS |
7055 | case RSHIFT_EXPR: |
7056 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
7057 | { | |
7058 | if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0) | |
b62acd60 | 7059 | { |
3e4093b6 RS |
7060 | if (tree_int_cst_sgn (op1) < 0) |
7061 | warning ("right shift count is negative"); | |
7062 | else | |
bbb818c6 | 7063 | { |
3e4093b6 RS |
7064 | if (! integer_zerop (op1)) |
7065 | short_shift = 1; | |
7066 | ||
7067 | if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
7068 | warning ("right shift count >= width of type"); | |
bbb818c6 | 7069 | } |
b62acd60 | 7070 | } |
de520661 | 7071 | |
3e4093b6 RS |
7072 | /* Use the type of the value to be shifted. */ |
7073 | result_type = type0; | |
7074 | /* Convert the shift-count to an integer, regardless of size | |
7075 | of value being shifted. */ | |
7076 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
7077 | op1 = convert (integer_type_node, op1); | |
7078 | /* Avoid converting op1 to result_type later. */ | |
7079 | converted = 1; | |
400fbf9f | 7080 | } |
3e4093b6 | 7081 | break; |
253b6b82 | 7082 | |
3e4093b6 RS |
7083 | case LSHIFT_EXPR: |
7084 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
7085 | { | |
7086 | if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0) | |
de520661 | 7087 | { |
3e4093b6 RS |
7088 | if (tree_int_cst_sgn (op1) < 0) |
7089 | warning ("left shift count is negative"); | |
de520661 | 7090 | |
3e4093b6 RS |
7091 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
7092 | warning ("left shift count >= width of type"); | |
94ba5069 | 7093 | } |
de520661 | 7094 | |
3e4093b6 RS |
7095 | /* Use the type of the value to be shifted. */ |
7096 | result_type = type0; | |
7097 | /* Convert the shift-count to an integer, regardless of size | |
7098 | of value being shifted. */ | |
7099 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
7100 | op1 = convert (integer_type_node, op1); | |
7101 | /* Avoid converting op1 to result_type later. */ | |
7102 | converted = 1; | |
400fbf9f | 7103 | } |
3e4093b6 | 7104 | break; |
de520661 | 7105 | |
3e4093b6 RS |
7106 | case RROTATE_EXPR: |
7107 | case LROTATE_EXPR: | |
7108 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
7109 | { | |
7110 | if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0) | |
de520661 | 7111 | { |
3e4093b6 RS |
7112 | if (tree_int_cst_sgn (op1) < 0) |
7113 | warning ("shift count is negative"); | |
7114 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
7115 | warning ("shift count >= width of type"); | |
de520661 RS |
7116 | } |
7117 | ||
3e4093b6 RS |
7118 | /* Use the type of the value to be shifted. */ |
7119 | result_type = type0; | |
7120 | /* Convert the shift-count to an integer, regardless of size | |
7121 | of value being shifted. */ | |
7122 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
7123 | op1 = convert (integer_type_node, op1); | |
7124 | /* Avoid converting op1 to result_type later. */ | |
7125 | converted = 1; | |
7126 | } | |
7127 | break; | |
400fbf9f | 7128 | |
3e4093b6 RS |
7129 | case EQ_EXPR: |
7130 | case NE_EXPR: | |
7131 | if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE)) | |
7132 | warning ("comparing floating point with == or != is unsafe"); | |
7133 | /* Result of comparison is always int, | |
7134 | but don't convert the args to int! */ | |
7135 | build_type = integer_type_node; | |
7136 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
eabe2b29 | 7137 | || code0 == COMPLEX_TYPE) |
3e4093b6 | 7138 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
eabe2b29 | 7139 | || code1 == COMPLEX_TYPE)) |
3e4093b6 RS |
7140 | short_compare = 1; |
7141 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
7142 | { | |
7143 | tree tt0 = TREE_TYPE (type0); | |
7144 | tree tt1 = TREE_TYPE (type1); | |
7145 | /* Anything compares with void *. void * compares with anything. | |
7146 | Otherwise, the targets must be compatible | |
7147 | and both must be object or both incomplete. */ | |
7148 | if (comp_target_types (type0, type1, 1)) | |
10bc1b1b | 7149 | result_type = common_pointer_type (type0, type1); |
3e4093b6 | 7150 | else if (VOID_TYPE_P (tt0)) |
ee2990e7 | 7151 | { |
3e4093b6 RS |
7152 | /* op0 != orig_op0 detects the case of something |
7153 | whose value is 0 but which isn't a valid null ptr const. */ | |
7154 | if (pedantic && (!integer_zerop (op0) || op0 != orig_op0) | |
7155 | && TREE_CODE (tt1) == FUNCTION_TYPE) | |
7156 | pedwarn ("ISO C forbids comparison of `void *' with function pointer"); | |
ee2990e7 | 7157 | } |
3e4093b6 | 7158 | else if (VOID_TYPE_P (tt1)) |
e6834654 | 7159 | { |
3e4093b6 RS |
7160 | if (pedantic && (!integer_zerop (op1) || op1 != orig_op1) |
7161 | && TREE_CODE (tt0) == FUNCTION_TYPE) | |
7162 | pedwarn ("ISO C forbids comparison of `void *' with function pointer"); | |
e6834654 | 7163 | } |
3e4093b6 RS |
7164 | else |
7165 | pedwarn ("comparison of distinct pointer types lacks a cast"); | |
e6834654 | 7166 | |
3e4093b6 RS |
7167 | if (result_type == NULL_TREE) |
7168 | result_type = ptr_type_node; | |
e6834654 | 7169 | } |
3e4093b6 RS |
7170 | else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST |
7171 | && integer_zerop (op1)) | |
7172 | result_type = type0; | |
7173 | else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST | |
7174 | && integer_zerop (op0)) | |
7175 | result_type = type1; | |
7176 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
de520661 | 7177 | { |
3e4093b6 RS |
7178 | result_type = type0; |
7179 | pedwarn ("comparison between pointer and integer"); | |
de520661 | 7180 | } |
3e4093b6 | 7181 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) |
8b6a5902 | 7182 | { |
3e4093b6 RS |
7183 | result_type = type1; |
7184 | pedwarn ("comparison between pointer and integer"); | |
8b6a5902 | 7185 | } |
3e4093b6 | 7186 | break; |
8b6a5902 | 7187 | |
3e4093b6 RS |
7188 | case MAX_EXPR: |
7189 | case MIN_EXPR: | |
7190 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) | |
7191 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
7192 | shorten = 1; | |
7193 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
8b6a5902 | 7194 | { |
3e4093b6 | 7195 | if (comp_target_types (type0, type1, 1)) |
8b6a5902 | 7196 | { |
10bc1b1b | 7197 | result_type = common_pointer_type (type0, type1); |
3e4093b6 RS |
7198 | if (pedantic |
7199 | && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) | |
7200 | pedwarn ("ISO C forbids ordered comparisons of pointers to functions"); | |
8b6a5902 | 7201 | } |
3e4093b6 | 7202 | else |
8b6a5902 | 7203 | { |
3e4093b6 RS |
7204 | result_type = ptr_type_node; |
7205 | pedwarn ("comparison of distinct pointer types lacks a cast"); | |
8b6a5902 | 7206 | } |
8b6a5902 | 7207 | } |
de520661 | 7208 | break; |
400fbf9f | 7209 | |
3e4093b6 RS |
7210 | case LE_EXPR: |
7211 | case GE_EXPR: | |
7212 | case LT_EXPR: | |
7213 | case GT_EXPR: | |
7214 | build_type = integer_type_node; | |
7215 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) | |
7216 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
7217 | short_compare = 1; | |
7218 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
7219 | { | |
7220 | if (comp_target_types (type0, type1, 1)) | |
7221 | { | |
10bc1b1b | 7222 | result_type = common_pointer_type (type0, type1); |
3e4093b6 RS |
7223 | if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) |
7224 | != !COMPLETE_TYPE_P (TREE_TYPE (type1))) | |
7225 | pedwarn ("comparison of complete and incomplete pointers"); | |
7226 | else if (pedantic | |
7227 | && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) | |
7228 | pedwarn ("ISO C forbids ordered comparisons of pointers to functions"); | |
7229 | } | |
7230 | else | |
7231 | { | |
7232 | result_type = ptr_type_node; | |
7233 | pedwarn ("comparison of distinct pointer types lacks a cast"); | |
7234 | } | |
7235 | } | |
7236 | else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST | |
7237 | && integer_zerop (op1)) | |
7238 | { | |
7239 | result_type = type0; | |
7240 | if (pedantic || extra_warnings) | |
7241 | pedwarn ("ordered comparison of pointer with integer zero"); | |
7242 | } | |
7243 | else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST | |
7244 | && integer_zerop (op0)) | |
7245 | { | |
7246 | result_type = type1; | |
7247 | if (pedantic) | |
7248 | pedwarn ("ordered comparison of pointer with integer zero"); | |
7249 | } | |
7250 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
7251 | { | |
7252 | result_type = type0; | |
7253 | pedwarn ("comparison between pointer and integer"); | |
7254 | } | |
7255 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
7256 | { | |
7257 | result_type = type1; | |
7258 | pedwarn ("comparison between pointer and integer"); | |
7259 | } | |
7260 | break; | |
64094f6a | 7261 | |
3e4093b6 RS |
7262 | case UNORDERED_EXPR: |
7263 | case ORDERED_EXPR: | |
7264 | case UNLT_EXPR: | |
7265 | case UNLE_EXPR: | |
7266 | case UNGT_EXPR: | |
7267 | case UNGE_EXPR: | |
7268 | case UNEQ_EXPR: | |
d1a7edaf | 7269 | case LTGT_EXPR: |
3e4093b6 RS |
7270 | build_type = integer_type_node; |
7271 | if (code0 != REAL_TYPE || code1 != REAL_TYPE) | |
7272 | { | |
7273 | error ("unordered comparison on non-floating point argument"); | |
7274 | return error_mark_node; | |
7275 | } | |
7276 | common = 1; | |
7277 | break; | |
64094f6a | 7278 | |
3e4093b6 RS |
7279 | default: |
7280 | break; | |
c9fe6f9f | 7281 | } |
8f17b5c5 | 7282 | |
e57e265b PB |
7283 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
7284 | return error_mark_node; | |
7285 | ||
3e4093b6 RS |
7286 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
7287 | || code0 == VECTOR_TYPE) | |
7288 | && | |
7289 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE | |
7290 | || code1 == VECTOR_TYPE)) | |
400fbf9f | 7291 | { |
3e4093b6 | 7292 | int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE); |
39b726dd | 7293 | |
3e4093b6 RS |
7294 | if (shorten || common || short_compare) |
7295 | result_type = common_type (type0, type1); | |
400fbf9f | 7296 | |
3e4093b6 RS |
7297 | /* For certain operations (which identify themselves by shorten != 0) |
7298 | if both args were extended from the same smaller type, | |
7299 | do the arithmetic in that type and then extend. | |
400fbf9f | 7300 | |
3e4093b6 RS |
7301 | shorten !=0 and !=1 indicates a bitwise operation. |
7302 | For them, this optimization is safe only if | |
7303 | both args are zero-extended or both are sign-extended. | |
7304 | Otherwise, we might change the result. | |
7305 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
7306 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
400fbf9f | 7307 | |
3e4093b6 RS |
7308 | if (shorten && none_complex) |
7309 | { | |
7310 | int unsigned0, unsigned1; | |
7311 | tree arg0 = get_narrower (op0, &unsigned0); | |
7312 | tree arg1 = get_narrower (op1, &unsigned1); | |
7313 | /* UNS is 1 if the operation to be done is an unsigned one. */ | |
8df83eae | 7314 | int uns = TYPE_UNSIGNED (result_type); |
3e4093b6 | 7315 | tree type; |
400fbf9f | 7316 | |
3e4093b6 | 7317 | final_type = result_type; |
70768eda | 7318 | |
3e4093b6 RS |
7319 | /* Handle the case that OP0 (or OP1) does not *contain* a conversion |
7320 | but it *requires* conversion to FINAL_TYPE. */ | |
70768eda | 7321 | |
3e4093b6 RS |
7322 | if ((TYPE_PRECISION (TREE_TYPE (op0)) |
7323 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
7324 | && TREE_TYPE (op0) != final_type) | |
8df83eae | 7325 | unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
3e4093b6 RS |
7326 | if ((TYPE_PRECISION (TREE_TYPE (op1)) |
7327 | == TYPE_PRECISION (TREE_TYPE (arg1))) | |
7328 | && TREE_TYPE (op1) != final_type) | |
8df83eae | 7329 | unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); |
88a3dbc1 | 7330 | |
3e4093b6 | 7331 | /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ |
abe80e6d | 7332 | |
3e4093b6 RS |
7333 | /* For bitwise operations, signedness of nominal type |
7334 | does not matter. Consider only how operands were extended. */ | |
7335 | if (shorten == -1) | |
7336 | uns = unsigned0; | |
abe80e6d | 7337 | |
3e4093b6 RS |
7338 | /* Note that in all three cases below we refrain from optimizing |
7339 | an unsigned operation on sign-extended args. | |
7340 | That would not be valid. */ | |
abe80e6d | 7341 | |
3e4093b6 RS |
7342 | /* Both args variable: if both extended in same way |
7343 | from same width, do it in that width. | |
7344 | Do it unsigned if args were zero-extended. */ | |
7345 | if ((TYPE_PRECISION (TREE_TYPE (arg0)) | |
7346 | < TYPE_PRECISION (result_type)) | |
7347 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
7348 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
7349 | && unsigned0 == unsigned1 | |
7350 | && (unsigned0 || !uns)) | |
7351 | result_type | |
7352 | = c_common_signed_or_unsigned_type | |
7353 | (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); | |
7354 | else if (TREE_CODE (arg0) == INTEGER_CST | |
7355 | && (unsigned1 || !uns) | |
7356 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
7357 | < TYPE_PRECISION (result_type)) | |
7358 | && (type | |
7359 | = c_common_signed_or_unsigned_type (unsigned1, | |
7360 | TREE_TYPE (arg1)), | |
7361 | int_fits_type_p (arg0, type))) | |
7362 | result_type = type; | |
7363 | else if (TREE_CODE (arg1) == INTEGER_CST | |
7364 | && (unsigned0 || !uns) | |
7365 | && (TYPE_PRECISION (TREE_TYPE (arg0)) | |
7366 | < TYPE_PRECISION (result_type)) | |
7367 | && (type | |
7368 | = c_common_signed_or_unsigned_type (unsigned0, | |
7369 | TREE_TYPE (arg0)), | |
7370 | int_fits_type_p (arg1, type))) | |
7371 | result_type = type; | |
7372 | } | |
88a3dbc1 | 7373 | |
3e4093b6 | 7374 | /* Shifts can be shortened if shifting right. */ |
2f6e4e97 | 7375 | |
3e4093b6 RS |
7376 | if (short_shift) |
7377 | { | |
7378 | int unsigned_arg; | |
7379 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
88a3dbc1 | 7380 | |
3e4093b6 | 7381 | final_type = result_type; |
abe80e6d | 7382 | |
3e4093b6 | 7383 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) |
8df83eae | 7384 | unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); |
e9a25f70 | 7385 | |
3e4093b6 RS |
7386 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) |
7387 | /* We can shorten only if the shift count is less than the | |
7388 | number of bits in the smaller type size. */ | |
7389 | && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 | |
7390 | /* We cannot drop an unsigned shift after sign-extension. */ | |
8df83eae | 7391 | && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) |
3e4093b6 RS |
7392 | { |
7393 | /* Do an unsigned shift if the operand was zero-extended. */ | |
7394 | result_type | |
7395 | = c_common_signed_or_unsigned_type (unsigned_arg, | |
7396 | TREE_TYPE (arg0)); | |
7397 | /* Convert value-to-be-shifted to that type. */ | |
7398 | if (TREE_TYPE (op0) != result_type) | |
7399 | op0 = convert (result_type, op0); | |
7400 | converted = 1; | |
abe80e6d | 7401 | } |
88a3dbc1 RK |
7402 | } |
7403 | ||
3e4093b6 RS |
7404 | /* Comparison operations are shortened too but differently. |
7405 | They identify themselves by setting short_compare = 1. */ | |
56cb9733 | 7406 | |
3e4093b6 RS |
7407 | if (short_compare) |
7408 | { | |
7409 | /* Don't write &op0, etc., because that would prevent op0 | |
7410 | from being kept in a register. | |
7411 | Instead, make copies of the our local variables and | |
7412 | pass the copies by reference, then copy them back afterward. */ | |
7413 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
7414 | enum tree_code xresultcode = resultcode; | |
7415 | tree val | |
7416 | = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); | |
8f17b5c5 | 7417 | |
3e4093b6 RS |
7418 | if (val != 0) |
7419 | return val; | |
8f17b5c5 | 7420 | |
3e4093b6 RS |
7421 | op0 = xop0, op1 = xop1; |
7422 | converted = 1; | |
7423 | resultcode = xresultcode; | |
8f17b5c5 | 7424 | |
3e4093b6 RS |
7425 | if (warn_sign_compare && skip_evaluation == 0) |
7426 | { | |
8df83eae RK |
7427 | int op0_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op0)); |
7428 | int op1_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op1)); | |
3e4093b6 RS |
7429 | int unsignedp0, unsignedp1; |
7430 | tree primop0 = get_narrower (op0, &unsignedp0); | |
7431 | tree primop1 = get_narrower (op1, &unsignedp1); | |
400fbf9f | 7432 | |
3e4093b6 RS |
7433 | xop0 = orig_op0; |
7434 | xop1 = orig_op1; | |
7435 | STRIP_TYPE_NOPS (xop0); | |
7436 | STRIP_TYPE_NOPS (xop1); | |
e89a9554 | 7437 | |
3e4093b6 RS |
7438 | /* Give warnings for comparisons between signed and unsigned |
7439 | quantities that may fail. | |
e89a9554 | 7440 | |
3e4093b6 RS |
7441 | Do the checking based on the original operand trees, so that |
7442 | casts will be considered, but default promotions won't be. | |
400fbf9f | 7443 | |
3e4093b6 RS |
7444 | Do not warn if the comparison is being done in a signed type, |
7445 | since the signed type will only be chosen if it can represent | |
7446 | all the values of the unsigned type. */ | |
8df83eae | 7447 | if (! TYPE_UNSIGNED (result_type)) |
3e4093b6 RS |
7448 | /* OK */; |
7449 | /* Do not warn if both operands are the same signedness. */ | |
7450 | else if (op0_signed == op1_signed) | |
7451 | /* OK */; | |
7452 | else | |
7453 | { | |
7454 | tree sop, uop; | |
8f17b5c5 | 7455 | |
3e4093b6 RS |
7456 | if (op0_signed) |
7457 | sop = xop0, uop = xop1; | |
7458 | else | |
7459 | sop = xop1, uop = xop0; | |
8f17b5c5 | 7460 | |
3e4093b6 RS |
7461 | /* Do not warn if the signed quantity is an |
7462 | unsuffixed integer literal (or some static | |
7463 | constant expression involving such literals or a | |
7464 | conditional expression involving such literals) | |
7465 | and it is non-negative. */ | |
3a5b9284 | 7466 | if (tree_expr_nonnegative_p (sop)) |
3e4093b6 RS |
7467 | /* OK */; |
7468 | /* Do not warn if the comparison is an equality operation, | |
7469 | the unsigned quantity is an integral constant, and it | |
7470 | would fit in the result if the result were signed. */ | |
7471 | else if (TREE_CODE (uop) == INTEGER_CST | |
7472 | && (resultcode == EQ_EXPR || resultcode == NE_EXPR) | |
7473 | && int_fits_type_p | |
7474 | (uop, c_common_signed_type (result_type))) | |
7475 | /* OK */; | |
7476 | /* Do not warn if the unsigned quantity is an enumeration | |
7477 | constant and its maximum value would fit in the result | |
7478 | if the result were signed. */ | |
7479 | else if (TREE_CODE (uop) == INTEGER_CST | |
7480 | && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE | |
7481 | && int_fits_type_p | |
7482 | (TYPE_MAX_VALUE (TREE_TYPE(uop)), | |
7483 | c_common_signed_type (result_type))) | |
7484 | /* OK */; | |
7485 | else | |
7486 | warning ("comparison between signed and unsigned"); | |
7487 | } | |
8f17b5c5 | 7488 | |
3e4093b6 RS |
7489 | /* Warn if two unsigned values are being compared in a size |
7490 | larger than their original size, and one (and only one) is the | |
7491 | result of a `~' operator. This comparison will always fail. | |
8f17b5c5 | 7492 | |
3e4093b6 RS |
7493 | Also warn if one operand is a constant, and the constant |
7494 | does not have all bits set that are set in the ~ operand | |
7495 | when it is extended. */ | |
8f17b5c5 | 7496 | |
3e4093b6 RS |
7497 | if ((TREE_CODE (primop0) == BIT_NOT_EXPR) |
7498 | != (TREE_CODE (primop1) == BIT_NOT_EXPR)) | |
7499 | { | |
7500 | if (TREE_CODE (primop0) == BIT_NOT_EXPR) | |
7501 | primop0 = get_narrower (TREE_OPERAND (primop0, 0), | |
7502 | &unsignedp0); | |
7503 | else | |
7504 | primop1 = get_narrower (TREE_OPERAND (primop1, 0), | |
7505 | &unsignedp1); | |
64094f6a | 7506 | |
3e4093b6 RS |
7507 | if (host_integerp (primop0, 0) || host_integerp (primop1, 0)) |
7508 | { | |
7509 | tree primop; | |
7510 | HOST_WIDE_INT constant, mask; | |
7511 | int unsignedp, bits; | |
2ad1815d | 7512 | |
3e4093b6 RS |
7513 | if (host_integerp (primop0, 0)) |
7514 | { | |
7515 | primop = primop1; | |
7516 | unsignedp = unsignedp1; | |
7517 | constant = tree_low_cst (primop0, 0); | |
7518 | } | |
7519 | else | |
7520 | { | |
7521 | primop = primop0; | |
7522 | unsignedp = unsignedp0; | |
7523 | constant = tree_low_cst (primop1, 0); | |
7524 | } | |
7525 | ||
7526 | bits = TYPE_PRECISION (TREE_TYPE (primop)); | |
7527 | if (bits < TYPE_PRECISION (result_type) | |
7528 | && bits < HOST_BITS_PER_WIDE_INT && unsignedp) | |
7529 | { | |
7530 | mask = (~ (HOST_WIDE_INT) 0) << bits; | |
7531 | if ((mask & constant) != mask) | |
7532 | warning ("comparison of promoted ~unsigned with constant"); | |
7533 | } | |
7534 | } | |
7535 | else if (unsignedp0 && unsignedp1 | |
7536 | && (TYPE_PRECISION (TREE_TYPE (primop0)) | |
7537 | < TYPE_PRECISION (result_type)) | |
7538 | && (TYPE_PRECISION (TREE_TYPE (primop1)) | |
7539 | < TYPE_PRECISION (result_type))) | |
7540 | warning ("comparison of promoted ~unsigned with unsigned"); | |
7541 | } | |
7542 | } | |
2ad1815d | 7543 | } |
64094f6a | 7544 | } |
64094f6a | 7545 | |
3e4093b6 RS |
7546 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. |
7547 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
7548 | Then the expression will be built. | |
7549 | It will be given type FINAL_TYPE if that is nonzero; | |
7550 | otherwise, it will be given type RESULT_TYPE. */ | |
400fbf9f | 7551 | |
3e4093b6 RS |
7552 | if (!result_type) |
7553 | { | |
7554 | binary_op_error (code); | |
7555 | return error_mark_node; | |
7556 | } | |
400fbf9f | 7557 | |
3e4093b6 RS |
7558 | if (! converted) |
7559 | { | |
7560 | if (TREE_TYPE (op0) != result_type) | |
7561 | op0 = convert (result_type, op0); | |
7562 | if (TREE_TYPE (op1) != result_type) | |
7563 | op1 = convert (result_type, op1); | |
d97c6333 JW |
7564 | |
7565 | /* This can happen if one operand has a vector type, and the other | |
7566 | has a different type. */ | |
7567 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
7568 | return error_mark_node; | |
3e4093b6 | 7569 | } |
400fbf9f | 7570 | |
3e4093b6 RS |
7571 | if (build_type == NULL_TREE) |
7572 | build_type = result_type; | |
400fbf9f | 7573 | |
3e4093b6 | 7574 | { |
53fb4de3 | 7575 | tree result = build2 (resultcode, build_type, op0, op1); |
3e4093b6 RS |
7576 | |
7577 | /* Treat expressions in initializers specially as they can't trap. */ | |
bf730f15 RS |
7578 | result = require_constant_value ? fold_initializer (result) |
7579 | : fold (result); | |
6de9cd9a | 7580 | |
3e4093b6 | 7581 | if (final_type != 0) |
6de9cd9a DN |
7582 | result = convert (final_type, result); |
7583 | return result; | |
3e4093b6 | 7584 | } |
400fbf9f | 7585 | } |
3e4093b6 | 7586 | |
7a3ea201 RH |
7587 | /* Build the result of __builtin_offsetof. TYPE is the first argument to |
7588 | offsetof, i.e. a type. LIST is a tree_list that encodes component and | |
7589 | array references; PURPOSE is set for the former and VALUE is set for | |
7590 | the later. */ | |
7591 | ||
7592 | tree | |
7593 | build_offsetof (tree type, tree list) | |
7594 | { | |
7595 | tree t; | |
7596 | ||
7597 | /* Build "*(type *)0". */ | |
7598 | t = convert (build_pointer_type (type), null_pointer_node); | |
7599 | t = build_indirect_ref (t, ""); | |
7600 | ||
7601 | /* Build COMPONENT and ARRAY_REF expressions as needed. */ | |
7602 | for (list = nreverse (list); list ; list = TREE_CHAIN (list)) | |
7603 | if (TREE_PURPOSE (list)) | |
7604 | t = build_component_ref (t, TREE_PURPOSE (list)); | |
7605 | else | |
7606 | t = build_array_ref (t, TREE_VALUE (list)); | |
7607 | ||
7608 | /* Finalize the offsetof expression. For now all we need to do is take | |
7609 | the address of the expression we created, and cast that to an integer | |
7610 | type; this mirrors the traditional macro implementation of offsetof. */ | |
7611 | t = build_unary_op (ADDR_EXPR, t, 0); | |
7612 | return convert (size_type_node, t); | |
7613 | } |