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