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