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