]>
Commit | Line | Data |
---|---|---|
400fbf9f | 1 | /* Build expressions with type checking for C compiler. |
06ceef4e RK |
2 | Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
3 | 1998, 1999, 2000 Free Software Foundation, Inc. | |
400fbf9f JW |
4 | |
5 | This file is part of GNU CC. | |
6 | ||
7 | GNU CC is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GNU CC is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU CC; see the file COPYING. If not, write to | |
940d9d63 RK |
19 | the Free Software Foundation, 59 Temple Place - Suite 330, |
20 | Boston, MA 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, | |
26 | and some optimization. | |
27 | ||
28 | There are also routines to build RETURN_STMT nodes and CASE_STMT nodes, | |
29 | and to process initializations in declarations (since they work | |
30 | like a strange sort of assignment). */ | |
31 | ||
32 | #include "config.h" | |
670ee920 | 33 | #include "system.h" |
400fbf9f JW |
34 | #include "tree.h" |
35 | #include "c-tree.h" | |
6baf1cc8 | 36 | #include "tm_p.h" |
400fbf9f | 37 | #include "flags.h" |
e14417fa | 38 | #include "output.h" |
234042f4 JL |
39 | #include "rtl.h" |
40 | #include "expr.h" | |
5f6da302 | 41 | #include "toplev.h" |
ab87f8c8 | 42 | #include "intl.h" |
246833ac | 43 | #include "defaults.h" |
4dd7201e | 44 | #include "ggc.h" |
400fbf9f | 45 | |
b71c7f8a | 46 | /* Nonzero if we've already printed a "missing braces around initializer" |
103b7b17 | 47 | message within this initializer. */ |
b71c7f8a | 48 | static int missing_braces_mentioned; |
103b7b17 | 49 | |
6e090c76 KG |
50 | static tree qualify_type PARAMS ((tree, tree)); |
51 | static int comp_target_types PARAMS ((tree, tree)); | |
52 | static int function_types_compatible_p PARAMS ((tree, tree)); | |
53 | static int type_lists_compatible_p PARAMS ((tree, tree)); | |
54 | static tree decl_constant_value PARAMS ((tree)); | |
55 | static tree lookup_field PARAMS ((tree, tree, tree *)); | |
56 | static tree convert_arguments PARAMS ((tree, tree, tree, tree)); | |
57 | static tree pointer_int_sum PARAMS ((enum tree_code, tree, tree)); | |
58 | static tree pointer_diff PARAMS ((tree, tree)); | |
59 | static tree unary_complex_lvalue PARAMS ((enum tree_code, tree)); | |
60 | static void pedantic_lvalue_warning PARAMS ((enum tree_code)); | |
61 | static tree internal_build_compound_expr PARAMS ((tree, int)); | |
62 | static tree convert_for_assignment PARAMS ((tree, tree, const char *, | |
63 | tree, tree, int)); | |
64 | static void warn_for_assignment PARAMS ((const char *, const char *, | |
65 | tree, int)); | |
66 | static tree valid_compound_expr_initializer PARAMS ((tree, tree)); | |
67 | static void push_string PARAMS ((const char *)); | |
68 | static void push_member_name PARAMS ((tree)); | |
69 | static void push_array_bounds PARAMS ((int)); | |
70 | static int spelling_length PARAMS ((void)); | |
71 | static char *print_spelling PARAMS ((char *)); | |
72 | static void warning_init PARAMS ((const char *)); | |
73 | static tree digest_init PARAMS ((tree, tree, int, int)); | |
74 | static void check_init_type_bitfields PARAMS ((tree)); | |
75 | static void output_init_element PARAMS ((tree, tree, tree, int)); | |
76 | static void output_pending_init_elements PARAMS ((int)); | |
77 | static void add_pending_init PARAMS ((tree, tree)); | |
78 | static int pending_init_member PARAMS ((tree)); | |
400fbf9f JW |
79 | \f |
80 | /* Do `exp = require_complete_type (exp);' to make sure exp | |
81 | does not have an incomplete type. (That includes void types.) */ | |
82 | ||
83 | tree | |
84 | require_complete_type (value) | |
85 | tree value; | |
86 | { | |
87 | tree type = TREE_TYPE (value); | |
88 | ||
ea0f786b CB |
89 | if (TREE_CODE (value) == ERROR_MARK) |
90 | return error_mark_node; | |
91 | ||
400fbf9f JW |
92 | /* First, detect a valid value with a complete type. */ |
93 | if (TYPE_SIZE (type) != 0 | |
94 | && type != void_type_node) | |
95 | return value; | |
96 | ||
97 | incomplete_type_error (value, type); | |
98 | return error_mark_node; | |
99 | } | |
100 | ||
101 | /* Print an error message for invalid use of an incomplete type. | |
102 | VALUE is the expression that was used (or 0 if that isn't known) | |
103 | and TYPE is the type that was invalid. */ | |
104 | ||
105 | void | |
106 | incomplete_type_error (value, type) | |
107 | tree value; | |
108 | tree type; | |
109 | { | |
5d5993dd | 110 | const char *type_code_string; |
400fbf9f JW |
111 | |
112 | /* Avoid duplicate error message. */ | |
113 | if (TREE_CODE (type) == ERROR_MARK) | |
114 | return; | |
115 | ||
116 | if (value != 0 && (TREE_CODE (value) == VAR_DECL | |
117 | || TREE_CODE (value) == PARM_DECL)) | |
118 | error ("`%s' has an incomplete type", | |
119 | IDENTIFIER_POINTER (DECL_NAME (value))); | |
120 | else | |
121 | { | |
122 | retry: | |
123 | /* We must print an error message. Be clever about what it says. */ | |
124 | ||
125 | switch (TREE_CODE (type)) | |
126 | { | |
127 | case RECORD_TYPE: | |
ab87f8c8 | 128 | type_code_string = "struct"; |
400fbf9f JW |
129 | break; |
130 | ||
131 | case UNION_TYPE: | |
ab87f8c8 | 132 | type_code_string = "union"; |
400fbf9f JW |
133 | break; |
134 | ||
135 | case ENUMERAL_TYPE: | |
ab87f8c8 | 136 | type_code_string = "enum"; |
400fbf9f JW |
137 | break; |
138 | ||
139 | case VOID_TYPE: | |
140 | error ("invalid use of void expression"); | |
141 | return; | |
142 | ||
143 | case ARRAY_TYPE: | |
144 | if (TYPE_DOMAIN (type)) | |
145 | { | |
146 | type = TREE_TYPE (type); | |
147 | goto retry; | |
148 | } | |
149 | error ("invalid use of array with unspecified bounds"); | |
150 | return; | |
151 | ||
152 | default: | |
153 | abort (); | |
154 | } | |
155 | ||
156 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
ab87f8c8 JL |
157 | error ("invalid use of undefined type `%s %s'", |
158 | type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type))); | |
400fbf9f JW |
159 | else |
160 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ | |
161 | error ("invalid use of incomplete typedef `%s'", | |
162 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))); | |
163 | } | |
164 | } | |
165 | ||
166 | /* Return a variant of TYPE which has all the type qualifiers of LIKE | |
167 | as well as those of TYPE. */ | |
168 | ||
169 | static tree | |
170 | qualify_type (type, like) | |
171 | tree type, like; | |
172 | { | |
afbadaa7 MM |
173 | return c_build_qualified_type (type, |
174 | TYPE_QUALS (type) | TYPE_QUALS (like)); | |
400fbf9f JW |
175 | } |
176 | \f | |
177 | /* Return the common type of two types. | |
178 | We assume that comptypes has already been done and returned 1; | |
6cb72a7d RS |
179 | if that isn't so, this may crash. In particular, we assume that qualifiers |
180 | match. | |
400fbf9f JW |
181 | |
182 | This is the type for the result of most arithmetic operations | |
6cb72a7d | 183 | if the operands have the given two types. */ |
400fbf9f JW |
184 | |
185 | tree | |
186 | common_type (t1, t2) | |
187 | tree t1, t2; | |
188 | { | |
189 | register enum tree_code code1; | |
190 | register enum tree_code code2; | |
4b027d16 | 191 | tree attributes; |
400fbf9f JW |
192 | |
193 | /* Save time if the two types are the same. */ | |
194 | ||
195 | if (t1 == t2) return t1; | |
196 | ||
197 | /* If one type is nonsense, use the other. */ | |
198 | if (t1 == error_mark_node) | |
199 | return t2; | |
200 | if (t2 == error_mark_node) | |
201 | return t1; | |
202 | ||
d9525bec BK |
203 | /* Merge the attributes. */ |
204 | attributes = merge_machine_type_attributes (t1, t2); | |
4b027d16 | 205 | |
400fbf9f JW |
206 | /* Treat an enum type as the unsigned integer type of the same width. */ |
207 | ||
208 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
209 | t1 = type_for_size (TYPE_PRECISION (t1), 1); | |
210 | if (TREE_CODE (t2) == ENUMERAL_TYPE) | |
211 | t2 = type_for_size (TYPE_PRECISION (t2), 1); | |
212 | ||
213 | code1 = TREE_CODE (t1); | |
214 | code2 = TREE_CODE (t2); | |
215 | ||
75326e8c RK |
216 | /* If one type is complex, form the common type of the non-complex |
217 | components, then make that complex. Use T1 or T2 if it is the | |
218 | required type. */ | |
b6a10c9f RS |
219 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) |
220 | { | |
75326e8c RK |
221 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; |
222 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
223 | tree subtype = common_type (subtype1, subtype2); | |
224 | ||
225 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
4b027d16 | 226 | return build_type_attribute_variant (t1, attributes); |
75326e8c | 227 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) |
4b027d16 | 228 | return build_type_attribute_variant (t2, attributes); |
b6a10c9f | 229 | else |
4b027d16 RK |
230 | return build_type_attribute_variant (build_complex_type (subtype), |
231 | attributes); | |
b6a10c9f RS |
232 | } |
233 | ||
400fbf9f JW |
234 | switch (code1) |
235 | { | |
236 | case INTEGER_TYPE: | |
237 | case REAL_TYPE: | |
238 | /* If only one is real, use it as the result. */ | |
239 | ||
240 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
4b027d16 | 241 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
242 | |
243 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
4b027d16 | 244 | return build_type_attribute_variant (t2, attributes); |
400fbf9f JW |
245 | |
246 | /* Both real or both integers; use the one with greater precision. */ | |
247 | ||
248 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
4b027d16 | 249 | return build_type_attribute_variant (t1, attributes); |
400fbf9f | 250 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) |
4b027d16 | 251 | return build_type_attribute_variant (t2, attributes); |
400fbf9f JW |
252 | |
253 | /* Same precision. Prefer longs to ints even when same size. */ | |
254 | ||
36618528 RS |
255 | if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node |
256 | || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) | |
4b027d16 RK |
257 | return build_type_attribute_variant (long_unsigned_type_node, |
258 | attributes); | |
400fbf9f | 259 | |
36618528 RS |
260 | if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node |
261 | || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) | |
400fbf9f JW |
262 | { |
263 | /* But preserve unsignedness from the other type, | |
264 | since long cannot hold all the values of an unsigned int. */ | |
265 | if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2)) | |
4b027d16 RK |
266 | t1 = long_unsigned_type_node; |
267 | else | |
268 | t1 = long_integer_type_node; | |
269 | return build_type_attribute_variant (t1, attributes); | |
400fbf9f JW |
270 | } |
271 | ||
e9a25f70 JL |
272 | /* Likewise, prefer long double to double even if same size. */ |
273 | if (TYPE_MAIN_VARIANT (t1) == long_double_type_node | |
274 | || TYPE_MAIN_VARIANT (t2) == long_double_type_node) | |
275 | return build_type_attribute_variant (long_double_type_node, | |
276 | attributes); | |
277 | ||
400fbf9f JW |
278 | /* Otherwise prefer the unsigned one. */ |
279 | ||
280 | if (TREE_UNSIGNED (t1)) | |
4b027d16 RK |
281 | return build_type_attribute_variant (t1, attributes); |
282 | else | |
283 | return build_type_attribute_variant (t2, attributes); | |
400fbf9f JW |
284 | |
285 | case POINTER_TYPE: | |
400fbf9f JW |
286 | /* For two pointers, do this recursively on the target type, |
287 | and combine the qualifiers of the two types' targets. */ | |
8706edbc RS |
288 | /* This code was turned off; I don't know why. |
289 | But ANSI C specifies doing this with the qualifiers. | |
290 | So I turned it on again. */ | |
400fbf9f | 291 | { |
3932261a MM |
292 | tree pointed_to_1 = TREE_TYPE (t1); |
293 | tree pointed_to_2 = TREE_TYPE (t2); | |
294 | tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1), | |
295 | TYPE_MAIN_VARIANT (pointed_to_2)); | |
296 | t1 = build_pointer_type (c_build_qualified_type | |
297 | (target, | |
298 | TYPE_QUALS (pointed_to_1) | | |
299 | TYPE_QUALS (pointed_to_2))); | |
4b027d16 | 300 | return build_type_attribute_variant (t1, attributes); |
400fbf9f | 301 | } |
8706edbc | 302 | #if 0 |
4b027d16 RK |
303 | t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2))); |
304 | return build_type_attribute_variant (t1, attributes); | |
8706edbc | 305 | #endif |
400fbf9f JW |
306 | |
307 | case ARRAY_TYPE: | |
308 | { | |
309 | tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2)); | |
310 | /* Save space: see if the result is identical to one of the args. */ | |
311 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) | |
4b027d16 | 312 | return build_type_attribute_variant (t1, attributes); |
400fbf9f | 313 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) |
4b027d16 | 314 | return build_type_attribute_variant (t2, attributes); |
400fbf9f | 315 | /* Merge the element types, and have a size if either arg has one. */ |
4b027d16 RK |
316 | t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); |
317 | return build_type_attribute_variant (t1, attributes); | |
400fbf9f JW |
318 | } |
319 | ||
320 | case FUNCTION_TYPE: | |
321 | /* Function types: prefer the one that specified arg types. | |
322 | If both do, merge the arg types. Also merge the return types. */ | |
323 | { | |
324 | tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2)); | |
325 | tree p1 = TYPE_ARG_TYPES (t1); | |
326 | tree p2 = TYPE_ARG_TYPES (t2); | |
327 | int len; | |
328 | tree newargs, n; | |
329 | int i; | |
330 | ||
331 | /* Save space: see if the result is identical to one of the args. */ | |
332 | if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2)) | |
4b027d16 | 333 | return build_type_attribute_variant (t1, attributes); |
400fbf9f | 334 | if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1)) |
4b027d16 | 335 | return build_type_attribute_variant (t2, attributes); |
400fbf9f JW |
336 | |
337 | /* Simple way if one arg fails to specify argument types. */ | |
338 | if (TYPE_ARG_TYPES (t1) == 0) | |
4b027d16 RK |
339 | { |
340 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); | |
341 | return build_type_attribute_variant (t1, attributes); | |
342 | } | |
400fbf9f | 343 | if (TYPE_ARG_TYPES (t2) == 0) |
4b027d16 RK |
344 | { |
345 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); | |
346 | return build_type_attribute_variant (t1, attributes); | |
347 | } | |
400fbf9f JW |
348 | |
349 | /* If both args specify argument types, we must merge the two | |
350 | lists, argument by argument. */ | |
351 | ||
352 | len = list_length (p1); | |
353 | newargs = 0; | |
354 | ||
355 | for (i = 0; i < len; i++) | |
8d9bfdc5 | 356 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
400fbf9f JW |
357 | |
358 | n = newargs; | |
359 | ||
360 | for (; p1; | |
361 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) | |
362 | { | |
363 | /* A null type means arg type is not specified. | |
364 | Take whatever the other function type has. */ | |
365 | if (TREE_VALUE (p1) == 0) | |
366 | { | |
367 | TREE_VALUE (n) = TREE_VALUE (p2); | |
368 | goto parm_done; | |
369 | } | |
370 | if (TREE_VALUE (p2) == 0) | |
371 | { | |
372 | TREE_VALUE (n) = TREE_VALUE (p1); | |
373 | goto parm_done; | |
374 | } | |
375 | ||
376 | /* Given wait (union {union wait *u; int *i} *) | |
377 | and wait (union wait *), | |
378 | prefer union wait * as type of parm. */ | |
379 | if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE | |
380 | && TREE_VALUE (p1) != TREE_VALUE (p2)) | |
381 | { | |
382 | tree memb; | |
383 | for (memb = TYPE_FIELDS (TREE_VALUE (p1)); | |
384 | memb; memb = TREE_CHAIN (memb)) | |
385 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2))) | |
386 | { | |
387 | TREE_VALUE (n) = TREE_VALUE (p2); | |
388 | if (pedantic) | |
389 | pedwarn ("function types not truly compatible in ANSI C"); | |
390 | goto parm_done; | |
391 | } | |
392 | } | |
393 | if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE | |
394 | && TREE_VALUE (p2) != TREE_VALUE (p1)) | |
395 | { | |
396 | tree memb; | |
397 | for (memb = TYPE_FIELDS (TREE_VALUE (p2)); | |
398 | memb; memb = TREE_CHAIN (memb)) | |
399 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1))) | |
400 | { | |
401 | TREE_VALUE (n) = TREE_VALUE (p1); | |
402 | if (pedantic) | |
403 | pedwarn ("function types not truly compatible in ANSI C"); | |
404 | goto parm_done; | |
405 | } | |
406 | } | |
407 | TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2)); | |
408 | parm_done: ; | |
409 | } | |
410 | ||
4b027d16 | 411 | t1 = build_function_type (valtype, newargs); |
0f41302f | 412 | /* ... falls through ... */ |
400fbf9f JW |
413 | } |
414 | ||
415 | default: | |
4b027d16 | 416 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
417 | } |
418 | ||
419 | } | |
420 | \f | |
421 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment | |
422 | or various other operations. Return 2 if they are compatible | |
423 | but a warning may be needed if you use them together. */ | |
424 | ||
425 | int | |
426 | comptypes (type1, type2) | |
427 | tree type1, type2; | |
428 | { | |
429 | register tree t1 = type1; | |
430 | register tree t2 = type2; | |
4b027d16 | 431 | int attrval, val; |
400fbf9f JW |
432 | |
433 | /* Suppress errors caused by previously reported errors. */ | |
434 | ||
8d47dfc5 RH |
435 | if (t1 == t2 || !t1 || !t2 |
436 | || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) | |
400fbf9f JW |
437 | return 1; |
438 | ||
21318741 RK |
439 | /* If either type is the internal version of sizetype, return the |
440 | language version. */ | |
441 | if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1) | |
442 | && TYPE_DOMAIN (t1) != 0) | |
443 | t1 = TYPE_DOMAIN (t1); | |
444 | ||
445 | if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2) | |
446 | && TYPE_DOMAIN (t2) != 0) | |
447 | t2 = TYPE_DOMAIN (t2); | |
448 | ||
b8c21346 RK |
449 | /* Treat an enum type as the integer type of the same width and |
450 | signedness. */ | |
400fbf9f JW |
451 | |
452 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
b8c21346 | 453 | t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1)); |
400fbf9f | 454 | if (TREE_CODE (t2) == ENUMERAL_TYPE) |
b8c21346 | 455 | t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2)); |
400fbf9f JW |
456 | |
457 | if (t1 == t2) | |
458 | return 1; | |
459 | ||
460 | /* Different classes of types can't be compatible. */ | |
461 | ||
462 | if (TREE_CODE (t1) != TREE_CODE (t2)) return 0; | |
463 | ||
464 | /* Qualifiers must match. */ | |
465 | ||
3932261a | 466 | if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) |
400fbf9f JW |
467 | return 0; |
468 | ||
08632da2 RS |
469 | /* Allow for two different type nodes which have essentially the same |
470 | definition. Note that we already checked for equality of the type | |
38e01259 | 471 | qualifiers (just above). */ |
400fbf9f JW |
472 | |
473 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
474 | return 1; | |
475 | ||
4b027d16 RK |
476 | #ifndef COMP_TYPE_ATTRIBUTES |
477 | #define COMP_TYPE_ATTRIBUTES(t1,t2) 1 | |
478 | #endif | |
479 | ||
480 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
481 | if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2))) | |
482 | return 0; | |
483 | ||
484 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
485 | val = 0; | |
486 | ||
400fbf9f JW |
487 | switch (TREE_CODE (t1)) |
488 | { | |
489 | case POINTER_TYPE: | |
4b027d16 | 490 | val = (TREE_TYPE (t1) == TREE_TYPE (t2) |
400fbf9f | 491 | ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2))); |
4b027d16 | 492 | break; |
400fbf9f JW |
493 | |
494 | case FUNCTION_TYPE: | |
4b027d16 RK |
495 | val = function_types_compatible_p (t1, t2); |
496 | break; | |
400fbf9f JW |
497 | |
498 | case ARRAY_TYPE: | |
499 | { | |
400fbf9f JW |
500 | tree d1 = TYPE_DOMAIN (t1); |
501 | tree d2 = TYPE_DOMAIN (t2); | |
4b027d16 | 502 | val = 1; |
400fbf9f JW |
503 | |
504 | /* Target types must match incl. qualifiers. */ | |
505 | if (TREE_TYPE (t1) != TREE_TYPE (t2) | |
506 | && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))) | |
507 | return 0; | |
508 | ||
509 | /* Sizes must match unless one is missing or variable. */ | |
510 | if (d1 == 0 || d2 == 0 || d1 == d2 | |
511 | || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
512 | || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
513 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST | |
514 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST) | |
4b027d16 | 515 | break; |
400fbf9f | 516 | |
4b027d16 | 517 | if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1)) |
400fbf9f JW |
518 | == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2))) |
519 | && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1)) | |
520 | == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2))) | |
521 | && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1)) | |
522 | == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2))) | |
523 | && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1)) | |
4b027d16 RK |
524 | == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))))) |
525 | val = 0; | |
526 | break; | |
400fbf9f JW |
527 | } |
528 | ||
529 | case RECORD_TYPE: | |
392202b0 | 530 | if (maybe_objc_comptypes (t1, t2, 0) == 1) |
4b027d16 RK |
531 | val = 1; |
532 | break; | |
e9a25f70 JL |
533 | |
534 | default: | |
535 | break; | |
400fbf9f | 536 | } |
4b027d16 | 537 | return attrval == 2 && val == 1 ? 2 : val; |
400fbf9f JW |
538 | } |
539 | ||
540 | /* Return 1 if TTL and TTR are pointers to types that are equivalent, | |
541 | ignoring their qualifiers. */ | |
542 | ||
543 | static int | |
544 | comp_target_types (ttl, ttr) | |
545 | tree ttl, ttr; | |
546 | { | |
392202b0 | 547 | int val; |
8b40563c | 548 | |
392202b0 | 549 | /* Give maybe_objc_comptypes a crack at letting these types through. */ |
1d300e19 | 550 | if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0) |
392202b0 | 551 | return val; |
8b40563c | 552 | |
392202b0 TW |
553 | val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)), |
554 | TYPE_MAIN_VARIANT (TREE_TYPE (ttr))); | |
8b40563c | 555 | |
400fbf9f JW |
556 | if (val == 2 && pedantic) |
557 | pedwarn ("types are not quite compatible"); | |
558 | return val; | |
559 | } | |
560 | \f | |
561 | /* Subroutines of `comptypes'. */ | |
562 | ||
563 | /* Return 1 if two function types F1 and F2 are compatible. | |
564 | If either type specifies no argument types, | |
565 | the other must specify a fixed number of self-promoting arg types. | |
566 | Otherwise, if one type specifies only the number of arguments, | |
567 | the other must specify that number of self-promoting arg types. | |
568 | Otherwise, the argument types must match. */ | |
569 | ||
570 | static int | |
571 | function_types_compatible_p (f1, f2) | |
572 | tree f1, f2; | |
573 | { | |
574 | tree args1, args2; | |
575 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
576 | int val = 1; | |
577 | int val1; | |
578 | ||
579 | if (!(TREE_TYPE (f1) == TREE_TYPE (f2) | |
580 | || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2))))) | |
581 | return 0; | |
582 | ||
583 | args1 = TYPE_ARG_TYPES (f1); | |
584 | args2 = TYPE_ARG_TYPES (f2); | |
585 | ||
586 | /* An unspecified parmlist matches any specified parmlist | |
587 | whose argument types don't need default promotions. */ | |
588 | ||
589 | if (args1 == 0) | |
590 | { | |
591 | if (!self_promoting_args_p (args2)) | |
592 | return 0; | |
593 | /* If one of these types comes from a non-prototype fn definition, | |
594 | compare that with the other type's arglist. | |
595 | If they don't match, ask for a warning (but no error). */ | |
596 | if (TYPE_ACTUAL_ARG_TYPES (f1) | |
597 | && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1))) | |
598 | val = 2; | |
599 | return val; | |
600 | } | |
601 | if (args2 == 0) | |
602 | { | |
603 | if (!self_promoting_args_p (args1)) | |
604 | return 0; | |
605 | if (TYPE_ACTUAL_ARG_TYPES (f2) | |
606 | && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2))) | |
607 | val = 2; | |
608 | return val; | |
609 | } | |
610 | ||
611 | /* Both types have argument lists: compare them and propagate results. */ | |
612 | val1 = type_lists_compatible_p (args1, args2); | |
613 | return val1 != 1 ? val1 : val; | |
614 | } | |
615 | ||
616 | /* Check two lists of types for compatibility, | |
617 | returning 0 for incompatible, 1 for compatible, | |
618 | or 2 for compatible with warning. */ | |
619 | ||
620 | static int | |
621 | type_lists_compatible_p (args1, args2) | |
622 | tree args1, args2; | |
623 | { | |
624 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
625 | int val = 1; | |
9d5f3e49 | 626 | int newval = 0; |
400fbf9f JW |
627 | |
628 | while (1) | |
629 | { | |
630 | if (args1 == 0 && args2 == 0) | |
631 | return val; | |
632 | /* If one list is shorter than the other, | |
633 | they fail to match. */ | |
634 | if (args1 == 0 || args2 == 0) | |
635 | return 0; | |
636 | /* A null pointer instead of a type | |
637 | means there is supposed to be an argument | |
638 | but nothing is specified about what type it has. | |
639 | So match anything that self-promotes. */ | |
640 | if (TREE_VALUE (args1) == 0) | |
641 | { | |
c530479e | 642 | if (simple_type_promotes_to (TREE_VALUE (args2)) != NULL_TREE) |
400fbf9f JW |
643 | return 0; |
644 | } | |
645 | else if (TREE_VALUE (args2) == 0) | |
646 | { | |
c530479e | 647 | if (simple_type_promotes_to (TREE_VALUE (args1)) != NULL_TREE) |
400fbf9f JW |
648 | return 0; |
649 | } | |
650 | else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2)))) | |
651 | { | |
652 | /* Allow wait (union {union wait *u; int *i} *) | |
653 | and wait (union wait *) to be compatible. */ | |
654 | if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE | |
ea3373cd RK |
655 | && (TYPE_NAME (TREE_VALUE (args1)) == 0 |
656 | || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1))) | |
400fbf9f JW |
657 | && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST |
658 | && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)), | |
659 | TYPE_SIZE (TREE_VALUE (args2)))) | |
660 | { | |
661 | tree memb; | |
662 | for (memb = TYPE_FIELDS (TREE_VALUE (args1)); | |
663 | memb; memb = TREE_CHAIN (memb)) | |
664 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2))) | |
665 | break; | |
666 | if (memb == 0) | |
667 | return 0; | |
668 | } | |
669 | else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE | |
ea3373cd RK |
670 | && (TYPE_NAME (TREE_VALUE (args2)) == 0 |
671 | || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2))) | |
400fbf9f JW |
672 | && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST |
673 | && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)), | |
674 | TYPE_SIZE (TREE_VALUE (args1)))) | |
675 | { | |
676 | tree memb; | |
677 | for (memb = TYPE_FIELDS (TREE_VALUE (args2)); | |
678 | memb; memb = TREE_CHAIN (memb)) | |
679 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1))) | |
680 | break; | |
681 | if (memb == 0) | |
682 | return 0; | |
683 | } | |
684 | else | |
685 | return 0; | |
686 | } | |
687 | ||
688 | /* comptypes said ok, but record if it said to warn. */ | |
689 | if (newval > val) | |
690 | val = newval; | |
691 | ||
692 | args1 = TREE_CHAIN (args1); | |
693 | args2 = TREE_CHAIN (args2); | |
694 | } | |
695 | } | |
400fbf9f | 696 | \f |
400fbf9f JW |
697 | /* Compute the value of the `sizeof' operator. */ |
698 | ||
699 | tree | |
700 | c_sizeof (type) | |
701 | tree type; | |
702 | { | |
703 | enum tree_code code = TREE_CODE (type); | |
704 | ||
705 | if (code == FUNCTION_TYPE) | |
706 | { | |
707 | if (pedantic || warn_pointer_arith) | |
708 | pedwarn ("sizeof applied to a function type"); | |
fed3cef0 | 709 | return size_one_node; |
400fbf9f JW |
710 | } |
711 | if (code == VOID_TYPE) | |
712 | { | |
713 | if (pedantic || warn_pointer_arith) | |
714 | pedwarn ("sizeof applied to a void type"); | |
fed3cef0 | 715 | return size_one_node; |
400fbf9f | 716 | } |
fed3cef0 | 717 | |
400fbf9f | 718 | if (code == ERROR_MARK) |
fed3cef0 RK |
719 | return size_one_node; |
720 | ||
400fbf9f JW |
721 | if (TYPE_SIZE (type) == 0) |
722 | { | |
723 | error ("sizeof applied to an incomplete type"); | |
fed3cef0 | 724 | return size_zero_node; |
400fbf9f JW |
725 | } |
726 | ||
727 | /* Convert in case a char is more than one unit. */ | |
fed3cef0 RK |
728 | return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
729 | size_int (TYPE_PRECISION (char_type_node) | |
730 | / BITS_PER_UNIT)); | |
400fbf9f JW |
731 | } |
732 | ||
733 | tree | |
734 | c_sizeof_nowarn (type) | |
735 | tree type; | |
736 | { | |
737 | enum tree_code code = TREE_CODE (type); | |
738 | ||
fed3cef0 RK |
739 | if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK) |
740 | return size_one_node; | |
741 | ||
400fbf9f | 742 | if (TYPE_SIZE (type) == 0) |
fed3cef0 | 743 | return size_zero_node; |
400fbf9f JW |
744 | |
745 | /* Convert in case a char is more than one unit. */ | |
fed3cef0 RK |
746 | return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
747 | size_int (TYPE_PRECISION (char_type_node) | |
748 | / BITS_PER_UNIT)); | |
400fbf9f JW |
749 | } |
750 | ||
751 | /* Compute the size to increment a pointer by. */ | |
752 | ||
753 | tree | |
754 | c_size_in_bytes (type) | |
755 | tree type; | |
756 | { | |
757 | enum tree_code code = TREE_CODE (type); | |
758 | ||
fed3cef0 RK |
759 | if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK) |
760 | return size_one_node; | |
761 | ||
400fbf9f JW |
762 | if (TYPE_SIZE (type) == 0) |
763 | { | |
764 | error ("arithmetic on pointer to an incomplete type"); | |
fed3cef0 | 765 | return size_one_node; |
400fbf9f JW |
766 | } |
767 | ||
768 | /* Convert in case a char is more than one unit. */ | |
fed3cef0 RK |
769 | return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
770 | size_int (TYPE_PRECISION (char_type_node) | |
771 | / BITS_PER_UNIT)); | |
400fbf9f JW |
772 | } |
773 | ||
774 | /* Implement the __alignof keyword: Return the minimum required | |
775 | alignment of TYPE, measured in bytes. */ | |
776 | ||
777 | tree | |
778 | c_alignof (type) | |
779 | tree type; | |
780 | { | |
781 | enum tree_code code = TREE_CODE (type); | |
782 | ||
783 | if (code == FUNCTION_TYPE) | |
784 | return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); | |
785 | ||
786 | if (code == VOID_TYPE || code == ERROR_MARK) | |
fed3cef0 | 787 | return size_one_node; |
400fbf9f JW |
788 | |
789 | return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT); | |
790 | } | |
791 | \f | |
792 | /* Implement the __alignof keyword: Return the minimum required | |
793 | alignment of EXPR, measured in bytes. For VAR_DECL's and | |
794 | FIELD_DECL's return DECL_ALIGN (which can be set from an | |
795 | "aligned" __attribute__ specification). */ | |
9e9bd45d | 796 | |
400fbf9f JW |
797 | tree |
798 | c_alignof_expr (expr) | |
799 | tree expr; | |
800 | { | |
801 | if (TREE_CODE (expr) == VAR_DECL) | |
802 | return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT); | |
803 | ||
804 | if (TREE_CODE (expr) == COMPONENT_REF | |
ef86d2a6 | 805 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) |
400fbf9f JW |
806 | { |
807 | error ("`__alignof' applied to a bit-field"); | |
fed3cef0 | 808 | return size_one_node; |
400fbf9f JW |
809 | } |
810 | else if (TREE_CODE (expr) == COMPONENT_REF | |
811 | && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) | |
812 | return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT); | |
813 | ||
814 | if (TREE_CODE (expr) == INDIRECT_REF) | |
815 | { | |
816 | tree t = TREE_OPERAND (expr, 0); | |
817 | tree best = t; | |
818 | int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
819 | ||
820 | while (TREE_CODE (t) == NOP_EXPR | |
821 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) | |
822 | { | |
823 | int thisalign; | |
824 | ||
825 | t = TREE_OPERAND (t, 0); | |
826 | thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
827 | if (thisalign > bestalign) | |
828 | best = t, bestalign = thisalign; | |
829 | } | |
830 | return c_alignof (TREE_TYPE (TREE_TYPE (best))); | |
831 | } | |
832 | else | |
833 | return c_alignof (TREE_TYPE (expr)); | |
834 | } | |
a7c1916a | 835 | |
400fbf9f JW |
836 | /* Return either DECL or its known constant value (if it has one). */ |
837 | ||
838 | static tree | |
839 | decl_constant_value (decl) | |
840 | tree decl; | |
841 | { | |
a7c1916a | 842 | if (/* Don't change a variable array bound or initial value to a constant |
400fbf9f | 843 | in a place where a variable is invalid. */ |
a7c1916a | 844 | current_function_decl != 0 |
400fbf9f JW |
845 | && ! pedantic |
846 | && ! TREE_THIS_VOLATILE (decl) | |
8c3a6477 | 847 | && TREE_READONLY (decl) && ! ITERATOR_P (decl) |
400fbf9f JW |
848 | && DECL_INITIAL (decl) != 0 |
849 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
850 | /* This is invalid if initial value is not constant. | |
851 | If it has either a function call, a memory reference, | |
852 | or a variable, then re-evaluating it could give different results. */ | |
853 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
854 | /* Check for cases where this is sub-optimal, even though valid. */ | |
855 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR | |
856 | && DECL_MODE (decl) != BLKmode) | |
857 | return DECL_INITIAL (decl); | |
858 | return decl; | |
859 | } | |
860 | ||
861 | /* Perform default promotions for C data used in expressions. | |
862 | Arrays and functions are converted to pointers; | |
863 | enumeral types or short or char, to int. | |
864 | In addition, manifest constants symbols are replaced by their values. */ | |
865 | ||
866 | tree | |
867 | default_conversion (exp) | |
868 | tree exp; | |
869 | { | |
870 | register tree type = TREE_TYPE (exp); | |
871 | register enum tree_code code = TREE_CODE (type); | |
872 | ||
873 | /* Constants can be used directly unless they're not loadable. */ | |
874 | if (TREE_CODE (exp) == CONST_DECL) | |
875 | exp = DECL_INITIAL (exp); | |
d4424a75 RK |
876 | |
877 | /* Replace a nonvolatile const static variable with its value unless | |
878 | it is an array, in which case we must be sure that taking the | |
879 | address of the array produces consistent results. */ | |
880 | else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE) | |
400fbf9f JW |
881 | { |
882 | exp = decl_constant_value (exp); | |
883 | type = TREE_TYPE (exp); | |
884 | } | |
885 | ||
a7d53fce RS |
886 | /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as |
887 | an lvalue. */ | |
888 | /* Do not use STRIP_NOPS here! It will remove conversions from pointer | |
889 | to integer and cause infinite recursion. */ | |
890 | while (TREE_CODE (exp) == NON_LVALUE_EXPR | |
891 | || (TREE_CODE (exp) == NOP_EXPR | |
892 | && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp))) | |
893 | exp = TREE_OPERAND (exp, 0); | |
400fbf9f JW |
894 | |
895 | /* Normally convert enums to int, | |
896 | but convert wide enums to something wider. */ | |
897 | if (code == ENUMERAL_TYPE) | |
898 | { | |
899 | type = type_for_size (MAX (TYPE_PRECISION (type), | |
900 | TYPE_PRECISION (integer_type_node)), | |
86463d5d | 901 | ((flag_traditional |
e9a25f70 JL |
902 | || (TYPE_PRECISION (type) |
903 | >= TYPE_PRECISION (integer_type_node))) | |
86463d5d | 904 | && TREE_UNSIGNED (type))); |
400fbf9f JW |
905 | return convert (type, exp); |
906 | } | |
907 | ||
9753f113 | 908 | if (TREE_CODE (exp) == COMPONENT_REF |
e9a25f70 | 909 | && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))) |
9753f113 | 910 | { |
cff9c407 RK |
911 | tree width = DECL_SIZE (TREE_OPERAND (exp, 1)); |
912 | HOST_WIDE_INT low = TREE_INT_CST_LOW (width); | |
9753f113 | 913 | |
cff9c407 RK |
914 | /* If it's thinner than an int, promote it like a |
915 | C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */ | |
9753f113 | 916 | |
cff9c407 RK |
917 | if (low < TYPE_PRECISION (integer_type_node)) |
918 | { | |
919 | if (flag_traditional && TREE_UNSIGNED (type)) | |
920 | return convert (unsigned_type_node, exp); | |
921 | else | |
922 | return convert (integer_type_node, exp); | |
923 | } | |
9753f113 RK |
924 | } |
925 | ||
d627ed1b | 926 | if (C_PROMOTING_INTEGER_TYPE_P (type)) |
400fbf9f | 927 | { |
e83d45c4 RS |
928 | /* Traditionally, unsignedness is preserved in default promotions. |
929 | Also preserve unsignedness if not really getting any wider. */ | |
930 | if (TREE_UNSIGNED (type) | |
931 | && (flag_traditional | |
932 | || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) | |
400fbf9f JW |
933 | return convert (unsigned_type_node, exp); |
934 | return convert (integer_type_node, exp); | |
935 | } | |
19d76e60 RK |
936 | if (flag_traditional && !flag_allow_single_precision |
937 | && TYPE_MAIN_VARIANT (type) == float_type_node) | |
400fbf9f JW |
938 | return convert (double_type_node, exp); |
939 | if (code == VOID_TYPE) | |
940 | { | |
941 | error ("void value not ignored as it ought to be"); | |
942 | return error_mark_node; | |
943 | } | |
944 | if (code == FUNCTION_TYPE) | |
945 | { | |
946 | return build_unary_op (ADDR_EXPR, exp, 0); | |
947 | } | |
948 | if (code == ARRAY_TYPE) | |
949 | { | |
950 | register tree adr; | |
951 | tree restype = TREE_TYPE (type); | |
952 | tree ptrtype; | |
d11fdb45 RS |
953 | int constp = 0; |
954 | int volatilep = 0; | |
955 | ||
956 | if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' | |
957 | || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd') | |
958 | { | |
959 | constp = TREE_READONLY (exp); | |
960 | volatilep = TREE_THIS_VOLATILE (exp); | |
961 | } | |
962 | ||
3932261a MM |
963 | if (TYPE_QUALS (type) || constp || volatilep) |
964 | restype | |
965 | = c_build_qualified_type (restype, | |
966 | TYPE_QUALS (type) | |
967 | | (constp * TYPE_QUAL_CONST) | |
968 | | (volatilep * TYPE_QUAL_VOLATILE)); | |
400fbf9f JW |
969 | |
970 | if (TREE_CODE (exp) == INDIRECT_REF) | |
971 | return convert (TYPE_POINTER_TO (restype), | |
972 | TREE_OPERAND (exp, 0)); | |
973 | ||
974 | if (TREE_CODE (exp) == COMPOUND_EXPR) | |
975 | { | |
976 | tree op1 = default_conversion (TREE_OPERAND (exp, 1)); | |
977 | return build (COMPOUND_EXPR, TREE_TYPE (op1), | |
978 | TREE_OPERAND (exp, 0), op1); | |
979 | } | |
980 | ||
cff9c407 | 981 | if (! lvalue_p (exp) |
400fbf9f JW |
982 | && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) |
983 | { | |
8efabd13 RS |
984 | error ("invalid use of non-lvalue array"); |
985 | return error_mark_node; | |
400fbf9f JW |
986 | } |
987 | ||
400fbf9f JW |
988 | ptrtype = build_pointer_type (restype); |
989 | ||
990 | if (TREE_CODE (exp) == VAR_DECL) | |
991 | { | |
992 | /* ??? This is not really quite correct | |
993 | in that the type of the operand of ADDR_EXPR | |
994 | is not the target type of the type of the ADDR_EXPR itself. | |
995 | Question is, can this lossage be avoided? */ | |
996 | adr = build1 (ADDR_EXPR, ptrtype, exp); | |
997 | if (mark_addressable (exp) == 0) | |
998 | return error_mark_node; | |
999 | TREE_CONSTANT (adr) = staticp (exp); | |
1000 | TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */ | |
1001 | return adr; | |
1002 | } | |
1003 | /* This way is better for a COMPONENT_REF since it can | |
1004 | simplify the offset for a component. */ | |
1005 | adr = build_unary_op (ADDR_EXPR, exp, 1); | |
1006 | return convert (ptrtype, adr); | |
1007 | } | |
1008 | return exp; | |
1009 | } | |
1010 | \f | |
19d76e60 RK |
1011 | /* Look up component name in the structure type definition. |
1012 | ||
1013 | If this component name is found indirectly within an anonymous union, | |
1014 | store in *INDIRECT the component which directly contains | |
1015 | that anonymous union. Otherwise, set *INDIRECT to 0. */ | |
2f2d13da DE |
1016 | |
1017 | static tree | |
19d76e60 | 1018 | lookup_field (type, component, indirect) |
2f2d13da | 1019 | tree type, component; |
19d76e60 | 1020 | tree *indirect; |
2f2d13da DE |
1021 | { |
1022 | tree field; | |
1023 | ||
1024 | /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers | |
1025 | to the field elements. Use a binary search on this array to quickly | |
1026 | find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC | |
1027 | will always be set for structures which have many elements. */ | |
1028 | ||
1029 | if (TYPE_LANG_SPECIFIC (type)) | |
1030 | { | |
1031 | int bot, top, half; | |
1032 | tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0]; | |
1033 | ||
1034 | field = TYPE_FIELDS (type); | |
1035 | bot = 0; | |
1036 | top = TYPE_LANG_SPECIFIC (type)->len; | |
1037 | while (top - bot > 1) | |
1038 | { | |
2f2d13da DE |
1039 | half = (top - bot + 1) >> 1; |
1040 | field = field_array[bot+half]; | |
1041 | ||
1042 | if (DECL_NAME (field) == NULL_TREE) | |
1043 | { | |
1044 | /* Step through all anon unions in linear fashion. */ | |
1045 | while (DECL_NAME (field_array[bot]) == NULL_TREE) | |
1046 | { | |
a68b98cf | 1047 | tree anon = 0, junk; |
19d76e60 | 1048 | |
2f2d13da | 1049 | field = field_array[bot++]; |
a68b98cf RK |
1050 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE |
1051 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
1052 | anon = lookup_field (TREE_TYPE (field), component, &junk); | |
1053 | ||
2f2d13da | 1054 | if (anon != NULL_TREE) |
19d76e60 RK |
1055 | { |
1056 | *indirect = field; | |
1057 | return anon; | |
1058 | } | |
2f2d13da DE |
1059 | } |
1060 | ||
1061 | /* Entire record is only anon unions. */ | |
1062 | if (bot > top) | |
1063 | return NULL_TREE; | |
1064 | ||
1065 | /* Restart the binary search, with new lower bound. */ | |
1066 | continue; | |
1067 | } | |
1068 | ||
e8b87aac | 1069 | if (DECL_NAME (field) == component) |
2f2d13da | 1070 | break; |
e8b87aac | 1071 | if (DECL_NAME (field) < component) |
2f2d13da DE |
1072 | bot += half; |
1073 | else | |
1074 | top = bot + half; | |
1075 | } | |
1076 | ||
1077 | if (DECL_NAME (field_array[bot]) == component) | |
1078 | field = field_array[bot]; | |
1079 | else if (DECL_NAME (field) != component) | |
1080 | field = 0; | |
1081 | } | |
1082 | else | |
1083 | { | |
1084 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
1085 | { | |
1086 | if (DECL_NAME (field) == NULL_TREE) | |
1087 | { | |
19d76e60 | 1088 | tree junk; |
a68b98cf RK |
1089 | tree anon = 0; |
1090 | ||
1091 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
1092 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
1093 | anon = lookup_field (TREE_TYPE (field), component, &junk); | |
1094 | ||
2f2d13da | 1095 | if (anon != NULL_TREE) |
19d76e60 RK |
1096 | { |
1097 | *indirect = field; | |
1098 | return anon; | |
1099 | } | |
2f2d13da DE |
1100 | } |
1101 | ||
1102 | if (DECL_NAME (field) == component) | |
1103 | break; | |
1104 | } | |
1105 | } | |
1106 | ||
19d76e60 | 1107 | *indirect = NULL_TREE; |
2f2d13da DE |
1108 | return field; |
1109 | } | |
1110 | ||
400fbf9f JW |
1111 | /* Make an expression to refer to the COMPONENT field of |
1112 | structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */ | |
1113 | ||
1114 | tree | |
1115 | build_component_ref (datum, component) | |
1116 | tree datum, component; | |
1117 | { | |
1118 | register tree type = TREE_TYPE (datum); | |
1119 | register enum tree_code code = TREE_CODE (type); | |
1120 | register tree field = NULL; | |
1121 | register tree ref; | |
1122 | ||
1123 | /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it | |
1124 | unless we are not to support things not strictly ANSI. */ | |
1125 | switch (TREE_CODE (datum)) | |
1126 | { | |
1127 | case COMPOUND_EXPR: | |
1128 | { | |
1129 | tree value = build_component_ref (TREE_OPERAND (datum, 1), component); | |
400fbf9f JW |
1130 | return build (COMPOUND_EXPR, TREE_TYPE (value), |
1131 | TREE_OPERAND (datum, 0), value); | |
1132 | } | |
1133 | case COND_EXPR: | |
400fbf9f JW |
1134 | return build_conditional_expr |
1135 | (TREE_OPERAND (datum, 0), | |
1136 | build_component_ref (TREE_OPERAND (datum, 1), component), | |
1137 | build_component_ref (TREE_OPERAND (datum, 2), component)); | |
e9a25f70 JL |
1138 | |
1139 | default: | |
1140 | break; | |
400fbf9f JW |
1141 | } |
1142 | ||
1143 | /* See if there is a field or component with name COMPONENT. */ | |
1144 | ||
1145 | if (code == RECORD_TYPE || code == UNION_TYPE) | |
1146 | { | |
19d76e60 RK |
1147 | tree indirect = 0; |
1148 | ||
400fbf9f JW |
1149 | if (TYPE_SIZE (type) == 0) |
1150 | { | |
8d9bfdc5 | 1151 | incomplete_type_error (NULL_TREE, type); |
400fbf9f JW |
1152 | return error_mark_node; |
1153 | } | |
1154 | ||
19d76e60 | 1155 | field = lookup_field (type, component, &indirect); |
400fbf9f JW |
1156 | |
1157 | if (!field) | |
1158 | { | |
913d0833 KG |
1159 | error ("%s has no member named `%s'", |
1160 | code == RECORD_TYPE ? "structure" : "union", | |
400fbf9f JW |
1161 | IDENTIFIER_POINTER (component)); |
1162 | return error_mark_node; | |
1163 | } | |
1164 | if (TREE_TYPE (field) == error_mark_node) | |
1165 | return error_mark_node; | |
1166 | ||
19d76e60 RK |
1167 | /* If FIELD was found buried within an anonymous union, |
1168 | make one COMPONENT_REF to get that anonymous union, | |
1169 | then fall thru to make a second COMPONENT_REF to get FIELD. */ | |
1170 | if (indirect != 0) | |
1171 | { | |
1172 | ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect); | |
1173 | if (TREE_READONLY (datum) || TREE_READONLY (indirect)) | |
1174 | TREE_READONLY (ref) = 1; | |
1175 | if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect)) | |
1176 | TREE_THIS_VOLATILE (ref) = 1; | |
1177 | datum = ref; | |
1178 | } | |
1179 | ||
400fbf9f JW |
1180 | ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field); |
1181 | ||
1182 | if (TREE_READONLY (datum) || TREE_READONLY (field)) | |
1183 | TREE_READONLY (ref) = 1; | |
1184 | if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field)) | |
1185 | TREE_THIS_VOLATILE (ref) = 1; | |
1186 | ||
1187 | return ref; | |
1188 | } | |
1189 | else if (code != ERROR_MARK) | |
1190 | error ("request for member `%s' in something not a structure or union", | |
1191 | IDENTIFIER_POINTER (component)); | |
1192 | ||
1193 | return error_mark_node; | |
1194 | } | |
1195 | \f | |
1196 | /* Given an expression PTR for a pointer, return an expression | |
1197 | for the value pointed to. | |
1198 | ERRORSTRING is the name of the operator to appear in error messages. */ | |
1199 | ||
1200 | tree | |
1201 | build_indirect_ref (ptr, errorstring) | |
1202 | tree ptr; | |
5d5993dd | 1203 | const char *errorstring; |
400fbf9f JW |
1204 | { |
1205 | register tree pointer = default_conversion (ptr); | |
1206 | register tree type = TREE_TYPE (pointer); | |
1207 | ||
1208 | if (TREE_CODE (type) == POINTER_TYPE) | |
870cc33b RS |
1209 | { |
1210 | if (TREE_CODE (pointer) == ADDR_EXPR | |
1211 | && !flag_volatile | |
1212 | && (TREE_TYPE (TREE_OPERAND (pointer, 0)) | |
1213 | == TREE_TYPE (type))) | |
1214 | return TREE_OPERAND (pointer, 0); | |
1215 | else | |
1216 | { | |
1217 | tree t = TREE_TYPE (type); | |
1218 | register tree ref = build1 (INDIRECT_REF, | |
1219 | TYPE_MAIN_VARIANT (t), pointer); | |
400fbf9f | 1220 | |
870cc33b RS |
1221 | if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE) |
1222 | { | |
1223 | error ("dereferencing pointer to incomplete type"); | |
1224 | return error_mark_node; | |
1225 | } | |
bd5b5c85 | 1226 | if (TREE_CODE (t) == VOID_TYPE && skip_evaluation == 0) |
870cc33b RS |
1227 | warning ("dereferencing `void *' pointer"); |
1228 | ||
1229 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, | |
1230 | so that we get the proper error message if the result is used | |
1231 | to assign to. Also, &* is supposed to be a no-op. | |
1232 | And ANSI C seems to specify that the type of the result | |
1233 | should be the const type. */ | |
1234 | /* A de-reference of a pointer to const is not a const. It is valid | |
1235 | to change it via some other pointer. */ | |
1236 | TREE_READONLY (ref) = TYPE_READONLY (t); | |
1237 | TREE_SIDE_EFFECTS (ref) | |
1238 | = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile; | |
493692cd | 1239 | TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); |
870cc33b RS |
1240 | return ref; |
1241 | } | |
1242 | } | |
400fbf9f JW |
1243 | else if (TREE_CODE (pointer) != ERROR_MARK) |
1244 | error ("invalid type argument of `%s'", errorstring); | |
1245 | return error_mark_node; | |
1246 | } | |
1247 | ||
1248 | /* This handles expressions of the form "a[i]", which denotes | |
1249 | an array reference. | |
1250 | ||
1251 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
1252 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
1253 | This avoids forcing the array out of registers, and can work on | |
1254 | arrays that are not lvalues (for example, members of structures returned | |
1255 | by functions). */ | |
1256 | ||
1257 | tree | |
1258 | build_array_ref (array, index) | |
1259 | tree array, index; | |
1260 | { | |
1261 | if (index == 0) | |
1262 | { | |
1263 | error ("subscript missing in array reference"); | |
1264 | return error_mark_node; | |
1265 | } | |
1266 | ||
1267 | if (TREE_TYPE (array) == error_mark_node | |
1268 | || TREE_TYPE (index) == error_mark_node) | |
1269 | return error_mark_node; | |
1270 | ||
1271 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE | |
1272 | && TREE_CODE (array) != INDIRECT_REF) | |
1273 | { | |
1274 | tree rval, type; | |
1275 | ||
400fbf9f JW |
1276 | /* Subscripting with type char is likely to lose |
1277 | on a machine where chars are signed. | |
1278 | So warn on any machine, but optionally. | |
1279 | Don't warn for unsigned char since that type is safe. | |
1280 | Don't warn for signed char because anyone who uses that | |
1281 | must have done so deliberately. */ | |
1282 | if (warn_char_subscripts | |
1283 | && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node) | |
1284 | warning ("array subscript has type `char'"); | |
1285 | ||
0e51ef9b RS |
1286 | /* Apply default promotions *after* noticing character types. */ |
1287 | index = default_conversion (index); | |
1288 | ||
fdeefd49 RS |
1289 | /* Require integer *after* promotion, for sake of enums. */ |
1290 | if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE) | |
1291 | { | |
1292 | error ("array subscript is not an integer"); | |
1293 | return error_mark_node; | |
1294 | } | |
1295 | ||
400fbf9f JW |
1296 | /* An array that is indexed by a non-constant |
1297 | cannot be stored in a register; we must be able to do | |
1298 | address arithmetic on its address. | |
1299 | Likewise an array of elements of variable size. */ | |
1300 | if (TREE_CODE (index) != INTEGER_CST | |
1301 | || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0 | |
1302 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) | |
1303 | { | |
1304 | if (mark_addressable (array) == 0) | |
1305 | return error_mark_node; | |
1306 | } | |
e6d52559 JW |
1307 | /* An array that is indexed by a constant value which is not within |
1308 | the array bounds cannot be stored in a register either; because we | |
1309 | would get a crash in store_bit_field/extract_bit_field when trying | |
1310 | to access a non-existent part of the register. */ | |
1311 | if (TREE_CODE (index) == INTEGER_CST | |
1312 | && TYPE_VALUES (TREE_TYPE (array)) | |
1313 | && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array)))) | |
1314 | { | |
1315 | if (mark_addressable (array) == 0) | |
1316 | return error_mark_node; | |
1317 | } | |
400fbf9f JW |
1318 | |
1319 | if (pedantic && !lvalue_p (array)) | |
1320 | { | |
1394aabd | 1321 | if (DECL_REGISTER (array)) |
400fbf9f JW |
1322 | pedwarn ("ANSI C forbids subscripting `register' array"); |
1323 | else | |
1324 | pedwarn ("ANSI C forbids subscripting non-lvalue array"); | |
1325 | } | |
1326 | ||
1327 | if (pedantic) | |
1328 | { | |
1329 | tree foo = array; | |
1330 | while (TREE_CODE (foo) == COMPONENT_REF) | |
1331 | foo = TREE_OPERAND (foo, 0); | |
1394aabd | 1332 | if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)) |
400fbf9f JW |
1333 | pedwarn ("ANSI C forbids subscripting non-lvalue array"); |
1334 | } | |
1335 | ||
1336 | type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array))); | |
1337 | rval = build (ARRAY_REF, type, array, index); | |
1338 | /* Array ref is const/volatile if the array elements are | |
1339 | or if the array is. */ | |
1340 | TREE_READONLY (rval) | |
1341 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
1342 | | TREE_READONLY (array)); | |
1343 | TREE_SIDE_EFFECTS (rval) | |
1344 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
1345 | | TREE_SIDE_EFFECTS (array)); | |
1346 | TREE_THIS_VOLATILE (rval) | |
1347 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
1348 | /* This was added by rms on 16 Nov 91. | |
1349 | It fixes vol struct foo *a; a->elts[1] | |
1350 | in an inline function. | |
1351 | Hope it doesn't break something else. */ | |
1352 | | TREE_THIS_VOLATILE (array)); | |
1353 | return require_complete_type (fold (rval)); | |
1354 | } | |
1355 | ||
1356 | { | |
1357 | tree ar = default_conversion (array); | |
1358 | tree ind = default_conversion (index); | |
1359 | ||
aed11452 RK |
1360 | /* Do the same warning check as above, but only on the part that's |
1361 | syntactically the index and only if it is also semantically | |
1362 | the index. */ | |
1363 | if (warn_char_subscripts | |
1364 | && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE | |
1365 | && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node) | |
1366 | warning ("subscript has type `char'"); | |
1367 | ||
400fbf9f JW |
1368 | /* Put the integer in IND to simplify error checking. */ |
1369 | if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) | |
1370 | { | |
1371 | tree temp = ar; | |
1372 | ar = ind; | |
1373 | ind = temp; | |
1374 | } | |
1375 | ||
1376 | if (ar == error_mark_node) | |
1377 | return ar; | |
1378 | ||
004252d7 RK |
1379 | if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE |
1380 | || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE) | |
400fbf9f JW |
1381 | { |
1382 | error ("subscripted value is neither array nor pointer"); | |
1383 | return error_mark_node; | |
1384 | } | |
1385 | if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) | |
1386 | { | |
1387 | error ("array subscript is not an integer"); | |
1388 | return error_mark_node; | |
1389 | } | |
1390 | ||
1391 | return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0), | |
1392 | "array indexing"); | |
1393 | } | |
1394 | } | |
1395 | \f | |
400fbf9f JW |
1396 | /* Build a function call to function FUNCTION with parameters PARAMS. |
1397 | PARAMS is a list--a chain of TREE_LIST nodes--in which the | |
1398 | TREE_VALUE of each node is a parameter-expression. | |
1399 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
1400 | ||
1401 | tree | |
1402 | build_function_call (function, params) | |
1403 | tree function, params; | |
1404 | { | |
346d29dc | 1405 | register tree fntype, fundecl = 0; |
400fbf9f | 1406 | register tree coerced_params; |
1eb8759b | 1407 | tree name = NULL_TREE, assembler_name = NULL_TREE, result; |
400fbf9f | 1408 | |
fc76e425 | 1409 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce | 1410 | STRIP_TYPE_NOPS (function); |
400fbf9f JW |
1411 | |
1412 | /* Convert anything with function type to a pointer-to-function. */ | |
1413 | if (TREE_CODE (function) == FUNCTION_DECL) | |
1414 | { | |
1415 | name = DECL_NAME (function); | |
19d76e60 RK |
1416 | assembler_name = DECL_ASSEMBLER_NAME (function); |
1417 | ||
400fbf9f JW |
1418 | /* Differs from default_conversion by not setting TREE_ADDRESSABLE |
1419 | (because calling an inline function does not mean the function | |
1420 | needs to be separately compiled). */ | |
1421 | fntype = build_type_variant (TREE_TYPE (function), | |
1422 | TREE_READONLY (function), | |
1423 | TREE_THIS_VOLATILE (function)); | |
9b7267b8 | 1424 | fundecl = function; |
400fbf9f JW |
1425 | function = build1 (ADDR_EXPR, build_pointer_type (fntype), function); |
1426 | } | |
1427 | else | |
1428 | function = default_conversion (function); | |
1429 | ||
1430 | fntype = TREE_TYPE (function); | |
1431 | ||
1432 | if (TREE_CODE (fntype) == ERROR_MARK) | |
1433 | return error_mark_node; | |
1434 | ||
1435 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
1436 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
1437 | { | |
1438 | error ("called object is not a function"); | |
1439 | return error_mark_node; | |
1440 | } | |
1441 | ||
1442 | /* fntype now gets the type of function pointed to. */ | |
1443 | fntype = TREE_TYPE (fntype); | |
1444 | ||
1445 | /* Convert the parameters to the types declared in the | |
1446 | function prototype, or apply default promotions. */ | |
1447 | ||
1448 | coerced_params | |
9b7267b8 | 1449 | = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl); |
400fbf9f JW |
1450 | |
1451 | /* Check for errors in format strings. */ | |
400fbf9f | 1452 | |
19d76e60 RK |
1453 | if (warn_format && (name || assembler_name)) |
1454 | check_function_format (name, assembler_name, coerced_params); | |
400fbf9f JW |
1455 | |
1456 | /* Recognize certain built-in functions so we can make tree-codes | |
1457 | other than CALL_EXPR. We do this when it enables fold-const.c | |
1458 | to do something useful. */ | |
1459 | ||
1460 | if (TREE_CODE (function) == ADDR_EXPR | |
1461 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL | |
1eb8759b RH |
1462 | && DECL_BUILT_IN (TREE_OPERAND (function, 0))) |
1463 | { | |
1464 | result = expand_tree_builtin (TREE_OPERAND (function, 0), | |
1465 | params, coerced_params); | |
1466 | if (result) | |
1467 | return result; | |
1468 | } | |
400fbf9f | 1469 | |
1eb8759b RH |
1470 | result = build (CALL_EXPR, TREE_TYPE (fntype), |
1471 | function, coerced_params, NULL_TREE); | |
1472 | ||
1473 | TREE_SIDE_EFFECTS (result) = 1; | |
1474 | if (TREE_TYPE (result) == void_type_node) | |
1475 | return result; | |
1476 | return require_complete_type (result); | |
400fbf9f JW |
1477 | } |
1478 | \f | |
1479 | /* Convert the argument expressions in the list VALUES | |
1480 | to the types in the list TYPELIST. The result is a list of converted | |
1481 | argument expressions. | |
1482 | ||
1483 | If TYPELIST is exhausted, or when an element has NULL as its type, | |
1484 | perform the default conversions. | |
1485 | ||
1486 | PARMLIST is the chain of parm decls for the function being called. | |
1487 | It may be 0, if that info is not available. | |
1488 | It is used only for generating error messages. | |
1489 | ||
1490 | NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. | |
1491 | ||
1492 | This is also where warnings about wrong number of args are generated. | |
1493 | ||
1494 | Both VALUES and the returned value are chains of TREE_LIST nodes | |
1495 | with the elements of the list in the TREE_VALUE slots of those nodes. */ | |
1496 | ||
1497 | static tree | |
9b7267b8 RS |
1498 | convert_arguments (typelist, values, name, fundecl) |
1499 | tree typelist, values, name, fundecl; | |
400fbf9f JW |
1500 | { |
1501 | register tree typetail, valtail; | |
1502 | register tree result = NULL; | |
1503 | int parmnum; | |
1504 | ||
1505 | /* Scan the given expressions and types, producing individual | |
1506 | converted arguments and pushing them on RESULT in reverse order. */ | |
1507 | ||
1508 | for (valtail = values, typetail = typelist, parmnum = 0; | |
1509 | valtail; | |
1510 | valtail = TREE_CHAIN (valtail), parmnum++) | |
1511 | { | |
1512 | register tree type = typetail ? TREE_VALUE (typetail) : 0; | |
1513 | register tree val = TREE_VALUE (valtail); | |
1514 | ||
1515 | if (type == void_type_node) | |
1516 | { | |
1517 | if (name) | |
1518 | error ("too many arguments to function `%s'", | |
1519 | IDENTIFIER_POINTER (name)); | |
1520 | else | |
1521 | error ("too many arguments to function"); | |
1522 | break; | |
1523 | } | |
1524 | ||
1525 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
fc76e425 RS |
1526 | /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0 |
1527 | to convert automatically to a pointer. */ | |
400fbf9f JW |
1528 | if (TREE_CODE (val) == NON_LVALUE_EXPR) |
1529 | val = TREE_OPERAND (val, 0); | |
1530 | ||
1531 | if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE | |
1532 | || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE) | |
1533 | val = default_conversion (val); | |
1534 | ||
1535 | val = require_complete_type (val); | |
1536 | ||
1537 | if (type != 0) | |
1538 | { | |
1539 | /* Formal parm type is specified by a function prototype. */ | |
1540 | tree parmval; | |
1541 | ||
1542 | if (TYPE_SIZE (type) == 0) | |
1543 | { | |
1544 | error ("type of formal parameter %d is incomplete", parmnum + 1); | |
1545 | parmval = val; | |
1546 | } | |
1547 | else | |
1548 | { | |
d45cf215 RS |
1549 | /* Optionally warn about conversions that |
1550 | differ from the default conversions. */ | |
400fbf9f JW |
1551 | if (warn_conversion) |
1552 | { | |
1553 | int formal_prec = TYPE_PRECISION (type); | |
400fbf9f | 1554 | |
aae43c5f | 1555 | if (INTEGRAL_TYPE_P (type) |
400fbf9f | 1556 | && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) |
754a4d82 | 1557 | warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1); |
aae43c5f RK |
1558 | else if (TREE_CODE (type) == COMPLEX_TYPE |
1559 | && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) | |
1560 | warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1); | |
400fbf9f | 1561 | else if (TREE_CODE (type) == REAL_TYPE |
aae43c5f | 1562 | && INTEGRAL_TYPE_P (TREE_TYPE (val))) |
754a4d82 | 1563 | warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1); |
aae43c5f RK |
1564 | else if (TREE_CODE (type) == REAL_TYPE |
1565 | && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE) | |
1566 | warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1); | |
1567 | /* ??? At some point, messages should be written about | |
1568 | conversions between complex types, but that's too messy | |
1569 | to do now. */ | |
d45cf215 RS |
1570 | else if (TREE_CODE (type) == REAL_TYPE |
1571 | && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) | |
1572 | { | |
1573 | /* Warn if any argument is passed as `float', | |
047de90b | 1574 | since without a prototype it would be `double'. */ |
d45cf215 | 1575 | if (formal_prec == TYPE_PRECISION (float_type_node)) |
754a4d82 | 1576 | warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1); |
d45cf215 | 1577 | } |
400fbf9f | 1578 | /* Detect integer changing in width or signedness. */ |
aae43c5f RK |
1579 | else if (INTEGRAL_TYPE_P (type) |
1580 | && INTEGRAL_TYPE_P (TREE_TYPE (val))) | |
400fbf9f | 1581 | { |
d45cf215 RS |
1582 | tree would_have_been = default_conversion (val); |
1583 | tree type1 = TREE_TYPE (would_have_been); | |
1584 | ||
754a4d82 RS |
1585 | if (TREE_CODE (type) == ENUMERAL_TYPE |
1586 | && type == TREE_TYPE (val)) | |
1587 | /* No warning if function asks for enum | |
1588 | and the actual arg is that enum type. */ | |
1589 | ; | |
1590 | else if (formal_prec != TYPE_PRECISION (type1)) | |
1591 | warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1); | |
d45cf215 RS |
1592 | else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1)) |
1593 | ; | |
800cd3b9 RS |
1594 | /* Don't complain if the formal parameter type |
1595 | is an enum, because we can't tell now whether | |
1596 | the value was an enum--even the same enum. */ | |
1597 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
1598 | ; | |
400fbf9f JW |
1599 | else if (TREE_CODE (val) == INTEGER_CST |
1600 | && int_fits_type_p (val, type)) | |
1601 | /* Change in signedness doesn't matter | |
1602 | if a constant value is unaffected. */ | |
1603 | ; | |
4bbbc5d9 RS |
1604 | /* Likewise for a constant in a NOP_EXPR. */ |
1605 | else if (TREE_CODE (val) == NOP_EXPR | |
1606 | && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST | |
1607 | && int_fits_type_p (TREE_OPERAND (val, 0), type)) | |
1608 | ; | |
1609 | #if 0 /* We never get such tree structure here. */ | |
047de90b RS |
1610 | else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE |
1611 | && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type) | |
1612 | && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type)) | |
1613 | /* Change in signedness doesn't matter | |
1614 | if an enum value is unaffected. */ | |
1615 | ; | |
4bbbc5d9 | 1616 | #endif |
ce9895ae RS |
1617 | /* If the value is extended from a narrower |
1618 | unsigned type, it doesn't matter whether we | |
1619 | pass it as signed or unsigned; the value | |
1620 | certainly is the same either way. */ | |
1621 | else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type) | |
1622 | && TREE_UNSIGNED (TREE_TYPE (val))) | |
1623 | ; | |
400fbf9f | 1624 | else if (TREE_UNSIGNED (type)) |
754a4d82 | 1625 | warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1); |
400fbf9f | 1626 | else |
754a4d82 | 1627 | warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1); |
400fbf9f JW |
1628 | } |
1629 | } | |
1630 | ||
1631 | parmval = convert_for_assignment (type, val, | |
0f41302f | 1632 | (char *) 0, /* arg passing */ |
9b7267b8 | 1633 | fundecl, name, parmnum + 1); |
400fbf9f | 1634 | |
7d473569 JJ |
1635 | if (PROMOTE_PROTOTYPES |
1636 | && (TREE_CODE (type) == INTEGER_TYPE | |
1637 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
400fbf9f JW |
1638 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
1639 | parmval = default_conversion (parmval); | |
400fbf9f | 1640 | } |
8d9bfdc5 | 1641 | result = tree_cons (NULL_TREE, parmval, result); |
400fbf9f JW |
1642 | } |
1643 | else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE | |
1644 | && (TYPE_PRECISION (TREE_TYPE (val)) | |
1645 | < TYPE_PRECISION (double_type_node))) | |
1646 | /* Convert `float' to `double'. */ | |
1647 | result = tree_cons (NULL_TREE, convert (double_type_node, val), result); | |
1648 | else | |
1649 | /* Convert `short' and `char' to full-size `int'. */ | |
1650 | result = tree_cons (NULL_TREE, default_conversion (val), result); | |
1651 | ||
1652 | if (typetail) | |
1653 | typetail = TREE_CHAIN (typetail); | |
1654 | } | |
1655 | ||
1656 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) | |
1657 | { | |
1658 | if (name) | |
1659 | error ("too few arguments to function `%s'", | |
1660 | IDENTIFIER_POINTER (name)); | |
1661 | else | |
1662 | error ("too few arguments to function"); | |
1663 | } | |
1664 | ||
1665 | return nreverse (result); | |
1666 | } | |
1667 | \f | |
1668 | /* This is the entry point used by the parser | |
1669 | for binary operators in the input. | |
1670 | In addition to constructing the expression, | |
1671 | we check for operands that were written with other binary operators | |
1672 | in a way that is likely to confuse the user. */ | |
edc7c4ec | 1673 | |
400fbf9f JW |
1674 | tree |
1675 | parser_build_binary_op (code, arg1, arg2) | |
1676 | enum tree_code code; | |
1677 | tree arg1, arg2; | |
1678 | { | |
1679 | tree result = build_binary_op (code, arg1, arg2, 1); | |
1680 | ||
1681 | char class; | |
1682 | char class1 = TREE_CODE_CLASS (TREE_CODE (arg1)); | |
1683 | char class2 = TREE_CODE_CLASS (TREE_CODE (arg2)); | |
1684 | enum tree_code code1 = ERROR_MARK; | |
1685 | enum tree_code code2 = ERROR_MARK; | |
1686 | ||
1687 | if (class1 == 'e' || class1 == '1' | |
1688 | || class1 == '2' || class1 == '<') | |
1689 | code1 = C_EXP_ORIGINAL_CODE (arg1); | |
1690 | if (class2 == 'e' || class2 == '1' | |
1691 | || class2 == '2' || class2 == '<') | |
1692 | code2 = C_EXP_ORIGINAL_CODE (arg2); | |
1693 | ||
1694 | /* Check for cases such as x+y<<z which users are likely | |
1695 | to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE | |
1696 | is cleared to prevent these warnings. */ | |
1697 | if (warn_parentheses) | |
1698 | { | |
1699 | if (code == LSHIFT_EXPR || code == RSHIFT_EXPR) | |
1700 | { | |
1701 | if (code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
1702 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
1703 | warning ("suggest parentheses around + or - inside shift"); | |
1704 | } | |
1705 | ||
1706 | if (code == TRUTH_ORIF_EXPR) | |
1707 | { | |
1708 | if (code1 == TRUTH_ANDIF_EXPR | |
1709 | || code2 == TRUTH_ANDIF_EXPR) | |
1710 | warning ("suggest parentheses around && within ||"); | |
1711 | } | |
1712 | ||
1713 | if (code == BIT_IOR_EXPR) | |
1714 | { | |
1715 | if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR | |
1716 | || code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
1717 | || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR | |
1718 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
1719 | warning ("suggest parentheses around arithmetic in operand of |"); | |
7e9d002a RK |
1720 | /* Check cases like x|y==z */ |
1721 | if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') | |
1722 | warning ("suggest parentheses around comparison in operand of |"); | |
400fbf9f JW |
1723 | } |
1724 | ||
1725 | if (code == BIT_XOR_EXPR) | |
1726 | { | |
1727 | if (code1 == BIT_AND_EXPR | |
1728 | || code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
1729 | || code2 == BIT_AND_EXPR | |
1730 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
1731 | warning ("suggest parentheses around arithmetic in operand of ^"); | |
7e9d002a RK |
1732 | /* Check cases like x^y==z */ |
1733 | if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') | |
1734 | warning ("suggest parentheses around comparison in operand of ^"); | |
400fbf9f JW |
1735 | } |
1736 | ||
1737 | if (code == BIT_AND_EXPR) | |
1738 | { | |
1739 | if (code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
1740 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
1741 | warning ("suggest parentheses around + or - in operand of &"); | |
7e9d002a RK |
1742 | /* Check cases like x&y==z */ |
1743 | if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') | |
1744 | warning ("suggest parentheses around comparison in operand of &"); | |
400fbf9f JW |
1745 | } |
1746 | } | |
1747 | ||
001af587 | 1748 | /* Similarly, check for cases like 1<=i<=10 that are probably errors. */ |
edc7c4ec | 1749 | if (TREE_CODE_CLASS (code) == '<' && extra_warnings |
001af587 RS |
1750 | && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')) |
1751 | warning ("comparisons like X<=Y<=Z do not have their mathematical meaning"); | |
1752 | ||
e58cd767 RS |
1753 | unsigned_conversion_warning (result, arg1); |
1754 | unsigned_conversion_warning (result, arg2); | |
1755 | overflow_warning (result); | |
1756 | ||
edc7c4ec RS |
1757 | class = TREE_CODE_CLASS (TREE_CODE (result)); |
1758 | ||
400fbf9f JW |
1759 | /* Record the code that was specified in the source, |
1760 | for the sake of warnings about confusing nesting. */ | |
1761 | if (class == 'e' || class == '1' | |
1762 | || class == '2' || class == '<') | |
1763 | C_SET_EXP_ORIGINAL_CODE (result, code); | |
1764 | else | |
1765 | { | |
1766 | int flag = TREE_CONSTANT (result); | |
d11fdb45 RS |
1767 | /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR |
1768 | so that convert_for_assignment wouldn't strip it. | |
1769 | That way, we got warnings for things like p = (1 - 1). | |
1770 | But it turns out we should not get those warnings. */ | |
1771 | result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result); | |
400fbf9f JW |
1772 | C_SET_EXP_ORIGINAL_CODE (result, code); |
1773 | TREE_CONSTANT (result) = flag; | |
1774 | } | |
1775 | ||
1776 | return result; | |
1777 | } | |
1778 | ||
1779 | /* Build a binary-operation expression without default conversions. | |
1780 | CODE is the kind of expression to build. | |
1781 | This function differs from `build' in several ways: | |
1782 | the data type of the result is computed and recorded in it, | |
1783 | warnings are generated if arg data types are invalid, | |
1784 | special handling for addition and subtraction of pointers is known, | |
1785 | and some optimization is done (operations on narrow ints | |
1786 | are done in the narrower type when that gives the same result). | |
1787 | Constant folding is also done before the result is returned. | |
1788 | ||
1789 | Note that the operands will never have enumeral types, or function | |
1790 | or array types, because either they will have the default conversions | |
1791 | performed or they have both just been converted to some other type in which | |
1792 | the arithmetic is to be done. */ | |
1793 | ||
1794 | tree | |
1795 | build_binary_op (code, orig_op0, orig_op1, convert_p) | |
1796 | enum tree_code code; | |
1797 | tree orig_op0, orig_op1; | |
1798 | int convert_p; | |
1799 | { | |
1800 | tree type0, type1; | |
1801 | register enum tree_code code0, code1; | |
1802 | tree op0, op1; | |
1803 | ||
1804 | /* Expression code to give to the expression when it is built. | |
1805 | Normally this is CODE, which is what the caller asked for, | |
1806 | but in some special cases we change it. */ | |
1807 | register enum tree_code resultcode = code; | |
1808 | ||
1809 | /* Data type in which the computation is to be performed. | |
1810 | In the simplest cases this is the common type of the arguments. */ | |
1811 | register tree result_type = NULL; | |
1812 | ||
1813 | /* Nonzero means operands have already been type-converted | |
1814 | in whatever way is necessary. | |
1815 | Zero means they need to be converted to RESULT_TYPE. */ | |
1816 | int converted = 0; | |
1817 | ||
293c9fdd JM |
1818 | /* Nonzero means create the expression with this type, rather than |
1819 | RESULT_TYPE. */ | |
1820 | tree build_type = 0; | |
1821 | ||
400fbf9f | 1822 | /* Nonzero means after finally constructing the expression |
293c9fdd | 1823 | convert it to this type. */ |
400fbf9f JW |
1824 | tree final_type = 0; |
1825 | ||
1826 | /* Nonzero if this is an operation like MIN or MAX which can | |
1827 | safely be computed in short if both args are promoted shorts. | |
1828 | Also implies COMMON. | |
1829 | -1 indicates a bitwise operation; this makes a difference | |
1830 | in the exact conditions for when it is safe to do the operation | |
1831 | in a narrower mode. */ | |
1832 | int shorten = 0; | |
1833 | ||
1834 | /* Nonzero if this is a comparison operation; | |
1835 | if both args are promoted shorts, compare the original shorts. | |
1836 | Also implies COMMON. */ | |
1837 | int short_compare = 0; | |
1838 | ||
1839 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
1840 | original short and then promoted if the operand is a promoted short. */ | |
1841 | int short_shift = 0; | |
1842 | ||
1843 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
1844 | int common = 0; | |
1845 | ||
1846 | if (convert_p) | |
1847 | { | |
1848 | op0 = default_conversion (orig_op0); | |
1849 | op1 = default_conversion (orig_op1); | |
1850 | } | |
1851 | else | |
1852 | { | |
1853 | op0 = orig_op0; | |
1854 | op1 = orig_op1; | |
1855 | } | |
1856 | ||
1857 | type0 = TREE_TYPE (op0); | |
1858 | type1 = TREE_TYPE (op1); | |
1859 | ||
1860 | /* The expression codes of the data types of the arguments tell us | |
1861 | whether the arguments are integers, floating, pointers, etc. */ | |
1862 | code0 = TREE_CODE (type0); | |
1863 | code1 = TREE_CODE (type1); | |
1864 | ||
fc76e425 | 1865 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce RS |
1866 | STRIP_TYPE_NOPS (op0); |
1867 | STRIP_TYPE_NOPS (op1); | |
400fbf9f JW |
1868 | |
1869 | /* If an error was already reported for one of the arguments, | |
1870 | avoid reporting another error. */ | |
1871 | ||
1872 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
1873 | return error_mark_node; | |
1874 | ||
1875 | switch (code) | |
1876 | { | |
1877 | case PLUS_EXPR: | |
1878 | /* Handle the pointer + int case. */ | |
1879 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
1880 | return pointer_int_sum (PLUS_EXPR, op0, op1); | |
1881 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) | |
1882 | return pointer_int_sum (PLUS_EXPR, op1, op0); | |
1883 | else | |
1884 | common = 1; | |
1885 | break; | |
1886 | ||
1887 | case MINUS_EXPR: | |
1888 | /* Subtraction of two similar pointers. | |
1889 | We must subtract them as integers, then divide by object size. */ | |
1890 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
1891 | && comp_target_types (type0, type1)) | |
1892 | return pointer_diff (op0, op1); | |
1893 | /* Handle pointer minus int. Just like pointer plus int. */ | |
1894 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
1895 | return pointer_int_sum (MINUS_EXPR, op0, op1); | |
1896 | else | |
1897 | common = 1; | |
1898 | break; | |
1899 | ||
1900 | case MULT_EXPR: | |
1901 | common = 1; | |
1902 | break; | |
1903 | ||
1904 | case TRUNC_DIV_EXPR: | |
1905 | case CEIL_DIV_EXPR: | |
1906 | case FLOOR_DIV_EXPR: | |
1907 | case ROUND_DIV_EXPR: | |
1908 | case EXACT_DIV_EXPR: | |
b6a10c9f RS |
1909 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
1910 | || code0 == COMPLEX_TYPE) | |
1911 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
1912 | || code1 == COMPLEX_TYPE)) | |
400fbf9f JW |
1913 | { |
1914 | if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)) | |
1915 | resultcode = RDIV_EXPR; | |
1916 | else | |
8b39ed65 TG |
1917 | { |
1918 | /* Although it would be tempting to shorten always here, that | |
1919 | loses on some targets, since the modulo instruction is | |
1920 | undefined if the quotient can't be represented in the | |
1921 | computation mode. We shorten only if unsigned or if | |
1922 | dividing by something we know != -1. */ | |
96d8f1d8 | 1923 | shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0)) |
8b39ed65 TG |
1924 | || (TREE_CODE (op1) == INTEGER_CST |
1925 | && (TREE_INT_CST_LOW (op1) != -1 | |
1926 | || TREE_INT_CST_HIGH (op1) != -1))); | |
1927 | } | |
400fbf9f JW |
1928 | common = 1; |
1929 | } | |
1930 | break; | |
1931 | ||
1932 | case BIT_AND_EXPR: | |
1933 | case BIT_ANDTC_EXPR: | |
1934 | case BIT_IOR_EXPR: | |
1935 | case BIT_XOR_EXPR: | |
1936 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
1937 | shorten = -1; | |
1938 | /* If one operand is a constant, and the other is a short type | |
1939 | that has been converted to an int, | |
1940 | really do the work in the short type and then convert the | |
1941 | result to int. If we are lucky, the constant will be 0 or 1 | |
1942 | in the short type, making the entire operation go away. */ | |
1943 | if (TREE_CODE (op0) == INTEGER_CST | |
1944 | && TREE_CODE (op1) == NOP_EXPR | |
1945 | && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))) | |
1946 | && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0)))) | |
1947 | { | |
1948 | final_type = result_type; | |
1949 | op1 = TREE_OPERAND (op1, 0); | |
1950 | result_type = TREE_TYPE (op1); | |
1951 | } | |
1952 | if (TREE_CODE (op1) == INTEGER_CST | |
1953 | && TREE_CODE (op0) == NOP_EXPR | |
1954 | && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))) | |
1955 | && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) | |
1956 | { | |
1957 | final_type = result_type; | |
1958 | op0 = TREE_OPERAND (op0, 0); | |
1959 | result_type = TREE_TYPE (op0); | |
1960 | } | |
1961 | break; | |
1962 | ||
1963 | case TRUNC_MOD_EXPR: | |
047de90b | 1964 | case FLOOR_MOD_EXPR: |
400fbf9f | 1965 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
03d5b1f5 RS |
1966 | { |
1967 | /* Although it would be tempting to shorten always here, that loses | |
1968 | on some targets, since the modulo instruction is undefined if the | |
1969 | quotient can't be represented in the computation mode. We shorten | |
1970 | only if unsigned or if dividing by something we know != -1. */ | |
96d8f1d8 | 1971 | shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0)) |
03d5b1f5 RS |
1972 | || (TREE_CODE (op1) == INTEGER_CST |
1973 | && (TREE_INT_CST_LOW (op1) != -1 | |
1974 | || TREE_INT_CST_HIGH (op1) != -1))); | |
1975 | common = 1; | |
1976 | } | |
400fbf9f JW |
1977 | break; |
1978 | ||
1979 | case TRUTH_ANDIF_EXPR: | |
1980 | case TRUTH_ORIF_EXPR: | |
1981 | case TRUTH_AND_EXPR: | |
1982 | case TRUTH_OR_EXPR: | |
1eca8b1e | 1983 | case TRUTH_XOR_EXPR: |
b6a10c9f RS |
1984 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE |
1985 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE) | |
1986 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE | |
1987 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE)) | |
400fbf9f JW |
1988 | { |
1989 | /* Result of these operations is always an int, | |
1990 | but that does not mean the operands should be | |
1991 | converted to ints! */ | |
1992 | result_type = integer_type_node; | |
1993 | op0 = truthvalue_conversion (op0); | |
1994 | op1 = truthvalue_conversion (op1); | |
1995 | converted = 1; | |
1996 | } | |
1997 | break; | |
1998 | ||
1999 | /* Shift operations: result has same type as first operand; | |
2000 | always convert second operand to int. | |
2001 | Also set SHORT_SHIFT if shifting rightward. */ | |
2002 | ||
2003 | case RSHIFT_EXPR: | |
2004 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
2005 | { | |
47ee6837 | 2006 | if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0) |
400fbf9f | 2007 | { |
ff3225e7 | 2008 | if (tree_int_cst_sgn (op1) < 0) |
315da535 | 2009 | warning ("right shift count is negative"); |
17651386 RS |
2010 | else |
2011 | { | |
2012 | if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1)) | |
2013 | short_shift = 1; | |
2014 | if (TREE_INT_CST_HIGH (op1) != 0 | |
2015 | || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) | |
2016 | >= TYPE_PRECISION (type0))) | |
315da535 | 2017 | warning ("right shift count >= width of type"); |
17651386 | 2018 | } |
400fbf9f | 2019 | } |
d45cf215 RS |
2020 | /* Use the type of the value to be shifted. |
2021 | This is what most traditional C compilers do. */ | |
2022 | result_type = type0; | |
400fbf9f JW |
2023 | /* Unless traditional, convert the shift-count to an integer, |
2024 | regardless of size of value being shifted. */ | |
2025 | if (! flag_traditional) | |
2026 | { | |
6cb72a7d | 2027 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) |
400fbf9f JW |
2028 | op1 = convert (integer_type_node, op1); |
2029 | /* Avoid converting op1 to result_type later. */ | |
2030 | converted = 1; | |
2031 | } | |
400fbf9f JW |
2032 | } |
2033 | break; | |
2034 | ||
2035 | case LSHIFT_EXPR: | |
2036 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
2037 | { | |
47ee6837 | 2038 | if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0) |
17651386 | 2039 | { |
ff3225e7 | 2040 | if (tree_int_cst_sgn (op1) < 0) |
315da535 | 2041 | warning ("left shift count is negative"); |
17651386 RS |
2042 | else if (TREE_INT_CST_HIGH (op1) != 0 |
2043 | || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) | |
2044 | >= TYPE_PRECISION (type0))) | |
315da535 | 2045 | warning ("left shift count >= width of type"); |
17651386 | 2046 | } |
d45cf215 RS |
2047 | /* Use the type of the value to be shifted. |
2048 | This is what most traditional C compilers do. */ | |
2049 | result_type = type0; | |
400fbf9f JW |
2050 | /* Unless traditional, convert the shift-count to an integer, |
2051 | regardless of size of value being shifted. */ | |
2052 | if (! flag_traditional) | |
2053 | { | |
6cb72a7d | 2054 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) |
400fbf9f JW |
2055 | op1 = convert (integer_type_node, op1); |
2056 | /* Avoid converting op1 to result_type later. */ | |
2057 | converted = 1; | |
2058 | } | |
400fbf9f JW |
2059 | } |
2060 | break; | |
2061 | ||
2062 | case RROTATE_EXPR: | |
2063 | case LROTATE_EXPR: | |
2064 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
2065 | { | |
47ee6837 | 2066 | if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0) |
17651386 | 2067 | { |
ff3225e7 | 2068 | if (tree_int_cst_sgn (op1) < 0) |
17651386 RS |
2069 | warning ("shift count is negative"); |
2070 | else if (TREE_INT_CST_HIGH (op1) != 0 | |
2071 | || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) | |
2072 | >= TYPE_PRECISION (type0))) | |
2073 | warning ("shift count >= width of type"); | |
2074 | } | |
d45cf215 RS |
2075 | /* Use the type of the value to be shifted. |
2076 | This is what most traditional C compilers do. */ | |
2077 | result_type = type0; | |
400fbf9f JW |
2078 | /* Unless traditional, convert the shift-count to an integer, |
2079 | regardless of size of value being shifted. */ | |
2080 | if (! flag_traditional) | |
2081 | { | |
6cb72a7d | 2082 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) |
400fbf9f JW |
2083 | op1 = convert (integer_type_node, op1); |
2084 | /* Avoid converting op1 to result_type later. */ | |
2085 | converted = 1; | |
2086 | } | |
400fbf9f JW |
2087 | } |
2088 | break; | |
2089 | ||
2090 | case EQ_EXPR: | |
2091 | case NE_EXPR: | |
b843d210 DZ |
2092 | if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE)) |
2093 | warning ("comparing floating point with == or != is unsafe"); | |
400fbf9f JW |
2094 | /* Result of comparison is always int, |
2095 | but don't convert the args to int! */ | |
293c9fdd | 2096 | build_type = integer_type_node; |
b6a10c9f RS |
2097 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
2098 | || code0 == COMPLEX_TYPE) | |
2099 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
2100 | || code1 == COMPLEX_TYPE)) | |
400fbf9f JW |
2101 | short_compare = 1; |
2102 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
2103 | { | |
2104 | register tree tt0 = TREE_TYPE (type0); | |
2105 | register tree tt1 = TREE_TYPE (type1); | |
2106 | /* Anything compares with void *. void * compares with anything. | |
d11fdb45 RS |
2107 | Otherwise, the targets must be compatible |
2108 | and both must be object or both incomplete. */ | |
400fbf9f | 2109 | if (comp_target_types (type0, type1)) |
605a99f6 | 2110 | result_type = common_type (type0, type1); |
400fbf9f JW |
2111 | else if (TYPE_MAIN_VARIANT (tt0) == void_type_node) |
2112 | { | |
fd5d5b94 RS |
2113 | /* op0 != orig_op0 detects the case of something |
2114 | whose value is 0 but which isn't a valid null ptr const. */ | |
2115 | if (pedantic && (!integer_zerop (op0) || op0 != orig_op0) | |
400fbf9f JW |
2116 | && TREE_CODE (tt1) == FUNCTION_TYPE) |
2117 | pedwarn ("ANSI C forbids comparison of `void *' with function pointer"); | |
2118 | } | |
2119 | else if (TYPE_MAIN_VARIANT (tt1) == void_type_node) | |
2120 | { | |
fd5d5b94 | 2121 | if (pedantic && (!integer_zerop (op1) || op1 != orig_op1) |
400fbf9f JW |
2122 | && TREE_CODE (tt0) == FUNCTION_TYPE) |
2123 | pedwarn ("ANSI C forbids comparison of `void *' with function pointer"); | |
2124 | } | |
2125 | else | |
2126 | pedwarn ("comparison of distinct pointer types lacks a cast"); | |
605a99f6 JM |
2127 | |
2128 | if (result_type == NULL_TREE) | |
2129 | result_type = ptr_type_node; | |
400fbf9f JW |
2130 | } |
2131 | else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST | |
2132 | && integer_zerop (op1)) | |
293c9fdd | 2133 | result_type = type0; |
400fbf9f JW |
2134 | else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST |
2135 | && integer_zerop (op0)) | |
293c9fdd | 2136 | result_type = type1; |
400fbf9f JW |
2137 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
2138 | { | |
293c9fdd | 2139 | result_type = type0; |
400fbf9f JW |
2140 | if (! flag_traditional) |
2141 | pedwarn ("comparison between pointer and integer"); | |
400fbf9f JW |
2142 | } |
2143 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
2144 | { | |
293c9fdd | 2145 | result_type = type1; |
400fbf9f JW |
2146 | if (! flag_traditional) |
2147 | pedwarn ("comparison between pointer and integer"); | |
400fbf9f | 2148 | } |
400fbf9f JW |
2149 | break; |
2150 | ||
2151 | case MAX_EXPR: | |
2152 | case MIN_EXPR: | |
9db931af RS |
2153 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) |
2154 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
400fbf9f JW |
2155 | shorten = 1; |
2156 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
2157 | { | |
605a99f6 JM |
2158 | if (comp_target_types (type0, type1)) |
2159 | { | |
2160 | result_type = common_type (type0, type1); | |
2161 | if (pedantic | |
2162 | && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) | |
2163 | pedwarn ("ANSI C forbids ordered comparisons of pointers to functions"); | |
2164 | } | |
2165 | else | |
2166 | { | |
2167 | result_type = ptr_type_node; | |
2168 | pedwarn ("comparison of distinct pointer types lacks a cast"); | |
2169 | } | |
400fbf9f JW |
2170 | } |
2171 | break; | |
2172 | ||
2173 | case LE_EXPR: | |
2174 | case GE_EXPR: | |
2175 | case LT_EXPR: | |
2176 | case GT_EXPR: | |
293c9fdd | 2177 | build_type = integer_type_node; |
9db931af RS |
2178 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) |
2179 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
400fbf9f JW |
2180 | short_compare = 1; |
2181 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
2182 | { | |
605a99f6 JM |
2183 | if (comp_target_types (type0, type1)) |
2184 | { | |
2185 | result_type = common_type (type0, type1); | |
2186 | if ((TYPE_SIZE (TREE_TYPE (type0)) != 0) | |
2187 | != (TYPE_SIZE (TREE_TYPE (type1)) != 0)) | |
2188 | pedwarn ("comparison of complete and incomplete pointers"); | |
2189 | else if (pedantic | |
2190 | && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) | |
2191 | pedwarn ("ANSI C forbids ordered comparisons of pointers to functions"); | |
2192 | } | |
2193 | else | |
2194 | { | |
2195 | result_type = ptr_type_node; | |
2196 | pedwarn ("comparison of distinct pointer types lacks a cast"); | |
2197 | } | |
400fbf9f JW |
2198 | } |
2199 | else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST | |
2200 | && integer_zerop (op1)) | |
2201 | { | |
293c9fdd | 2202 | result_type = type0; |
ddcf4abc | 2203 | if (pedantic || extra_warnings) |
400fbf9f JW |
2204 | pedwarn ("ordered comparison of pointer with integer zero"); |
2205 | } | |
2206 | else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST | |
2207 | && integer_zerop (op0)) | |
2208 | { | |
293c9fdd | 2209 | result_type = type1; |
400fbf9f JW |
2210 | if (pedantic) |
2211 | pedwarn ("ordered comparison of pointer with integer zero"); | |
2212 | } | |
2213 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
2214 | { | |
293c9fdd | 2215 | result_type = type0; |
400fbf9f JW |
2216 | if (! flag_traditional) |
2217 | pedwarn ("comparison between pointer and integer"); | |
400fbf9f JW |
2218 | } |
2219 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
2220 | { | |
293c9fdd | 2221 | result_type = type1; |
400fbf9f JW |
2222 | if (! flag_traditional) |
2223 | pedwarn ("comparison between pointer and integer"); | |
400fbf9f | 2224 | } |
400fbf9f | 2225 | break; |
1eb8759b RH |
2226 | |
2227 | case UNORDERED_EXPR: | |
2228 | case ORDERED_EXPR: | |
2229 | case UNLT_EXPR: | |
2230 | case UNLE_EXPR: | |
2231 | case UNGT_EXPR: | |
2232 | case UNGE_EXPR: | |
2233 | case UNEQ_EXPR: | |
1eb8759b RH |
2234 | build_type = integer_type_node; |
2235 | if (code0 != REAL_TYPE || code1 != REAL_TYPE) | |
2236 | { | |
2237 | error ("unordered comparison on non-floating point argument"); | |
2238 | return error_mark_node; | |
2239 | } | |
2240 | common = 1; | |
2241 | break; | |
2242 | ||
e9a25f70 JL |
2243 | default: |
2244 | break; | |
400fbf9f JW |
2245 | } |
2246 | ||
b6a10c9f RS |
2247 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE) |
2248 | && | |
2249 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE)) | |
400fbf9f | 2250 | { |
b6a10c9f RS |
2251 | int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE); |
2252 | ||
400fbf9f JW |
2253 | if (shorten || common || short_compare) |
2254 | result_type = common_type (type0, type1); | |
2255 | ||
2256 | /* For certain operations (which identify themselves by shorten != 0) | |
2257 | if both args were extended from the same smaller type, | |
2258 | do the arithmetic in that type and then extend. | |
2259 | ||
2260 | shorten !=0 and !=1 indicates a bitwise operation. | |
2261 | For them, this optimization is safe only if | |
2262 | both args are zero-extended or both are sign-extended. | |
2263 | Otherwise, we might change the result. | |
2264 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
2265 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
2266 | ||
b6a10c9f | 2267 | if (shorten && none_complex) |
400fbf9f JW |
2268 | { |
2269 | int unsigned0, unsigned1; | |
2270 | tree arg0 = get_narrower (op0, &unsigned0); | |
2271 | tree arg1 = get_narrower (op1, &unsigned1); | |
2272 | /* UNS is 1 if the operation to be done is an unsigned one. */ | |
2273 | int uns = TREE_UNSIGNED (result_type); | |
2274 | tree type; | |
2275 | ||
2276 | final_type = result_type; | |
2277 | ||
e7951b3f | 2278 | /* Handle the case that OP0 (or OP1) does not *contain* a conversion |
400fbf9f JW |
2279 | but it *requires* conversion to FINAL_TYPE. */ |
2280 | ||
e7951b3f RS |
2281 | if ((TYPE_PRECISION (TREE_TYPE (op0)) |
2282 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2283 | && TREE_TYPE (op0) != final_type) | |
400fbf9f | 2284 | unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0)); |
e7951b3f RS |
2285 | if ((TYPE_PRECISION (TREE_TYPE (op1)) |
2286 | == TYPE_PRECISION (TREE_TYPE (arg1))) | |
2287 | && TREE_TYPE (op1) != final_type) | |
400fbf9f JW |
2288 | unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1)); |
2289 | ||
2290 | /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ | |
2291 | ||
2292 | /* For bitwise operations, signedness of nominal type | |
2293 | does not matter. Consider only how operands were extended. */ | |
2294 | if (shorten == -1) | |
2295 | uns = unsigned0; | |
2296 | ||
2297 | /* Note that in all three cases below we refrain from optimizing | |
2298 | an unsigned operation on sign-extended args. | |
2299 | That would not be valid. */ | |
2300 | ||
2301 | /* Both args variable: if both extended in same way | |
2302 | from same width, do it in that width. | |
2303 | Do it unsigned if args were zero-extended. */ | |
2304 | if ((TYPE_PRECISION (TREE_TYPE (arg0)) | |
2305 | < TYPE_PRECISION (result_type)) | |
2306 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2307 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2308 | && unsigned0 == unsigned1 | |
2309 | && (unsigned0 || !uns)) | |
2310 | result_type | |
2311 | = signed_or_unsigned_type (unsigned0, | |
2312 | common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); | |
2313 | else if (TREE_CODE (arg0) == INTEGER_CST | |
2314 | && (unsigned1 || !uns) | |
2315 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2316 | < TYPE_PRECISION (result_type)) | |
2317 | && (type = signed_or_unsigned_type (unsigned1, | |
2318 | TREE_TYPE (arg1)), | |
2319 | int_fits_type_p (arg0, type))) | |
2320 | result_type = type; | |
2321 | else if (TREE_CODE (arg1) == INTEGER_CST | |
2322 | && (unsigned0 || !uns) | |
2323 | && (TYPE_PRECISION (TREE_TYPE (arg0)) | |
2324 | < TYPE_PRECISION (result_type)) | |
2325 | && (type = signed_or_unsigned_type (unsigned0, | |
2326 | TREE_TYPE (arg0)), | |
2327 | int_fits_type_p (arg1, type))) | |
2328 | result_type = type; | |
2329 | } | |
2330 | ||
2331 | /* Shifts can be shortened if shifting right. */ | |
2332 | ||
2333 | if (short_shift) | |
2334 | { | |
2335 | int unsigned_arg; | |
2336 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
2337 | ||
2338 | final_type = result_type; | |
2339 | ||
2340 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) | |
2341 | unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0)); | |
2342 | ||
2343 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) | |
6cb70f0c JW |
2344 | /* We can shorten only if the shift count is less than the |
2345 | number of bits in the smaller type size. */ | |
2346 | && TREE_INT_CST_HIGH (op1) == 0 | |
2347 | && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1) | |
400fbf9f JW |
2348 | /* If arg is sign-extended and then unsigned-shifted, |
2349 | we can simulate this with a signed shift in arg's type | |
2350 | only if the extended result is at least twice as wide | |
2351 | as the arg. Otherwise, the shift could use up all the | |
2352 | ones made by sign-extension and bring in zeros. | |
2353 | We can't optimize that case at all, but in most machines | |
2354 | it never happens because available widths are 2**N. */ | |
2355 | && (!TREE_UNSIGNED (final_type) | |
2356 | || unsigned_arg | |
2357 | || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type))) | |
2358 | { | |
2359 | /* Do an unsigned shift if the operand was zero-extended. */ | |
2360 | result_type | |
2361 | = signed_or_unsigned_type (unsigned_arg, | |
2362 | TREE_TYPE (arg0)); | |
2363 | /* Convert value-to-be-shifted to that type. */ | |
2364 | if (TREE_TYPE (op0) != result_type) | |
2365 | op0 = convert (result_type, op0); | |
2366 | converted = 1; | |
2367 | } | |
2368 | } | |
2369 | ||
2370 | /* Comparison operations are shortened too but differently. | |
2371 | They identify themselves by setting short_compare = 1. */ | |
2372 | ||
75326e8c | 2373 | if (short_compare) |
400fbf9f JW |
2374 | { |
2375 | /* Don't write &op0, etc., because that would prevent op0 | |
2376 | from being kept in a register. | |
2377 | Instead, make copies of the our local variables and | |
2378 | pass the copies by reference, then copy them back afterward. */ | |
2379 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
2380 | enum tree_code xresultcode = resultcode; | |
2381 | tree val | |
2382 | = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); | |
2383 | if (val != 0) | |
2384 | return val; | |
293c9fdd JM |
2385 | op0 = xop0, op1 = xop1; |
2386 | converted = 1; | |
400fbf9f JW |
2387 | resultcode = xresultcode; |
2388 | ||
407cb092 PE |
2389 | if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0) |
2390 | && skip_evaluation == 0) | |
400fbf9f | 2391 | { |
d2d7ed3e JM |
2392 | int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0)); |
2393 | int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1)); | |
2394 | ||
64c01f80 DE |
2395 | int unsignedp0, unsignedp1; |
2396 | tree primop0 = get_narrower (op0, &unsignedp0); | |
2397 | tree primop1 = get_narrower (op1, &unsignedp1); | |
2398 | ||
912b4fc3 JM |
2399 | xop0 = orig_op0; |
2400 | xop1 = orig_op1; | |
2401 | STRIP_TYPE_NOPS (xop0); | |
2402 | STRIP_TYPE_NOPS (xop1); | |
2403 | ||
400fbf9f | 2404 | /* Give warnings for comparisons between signed and unsigned |
293c9fdd | 2405 | quantities that may fail. */ |
400fbf9f JW |
2406 | /* Do the checking based on the original operand trees, so that |
2407 | casts will be considered, but default promotions won't be. */ | |
293c9fdd JM |
2408 | |
2409 | /* Do not warn if the comparison is being done in a signed type, | |
2410 | since the signed type will only be chosen if it can represent | |
2411 | all the values of the unsigned type. */ | |
2412 | if (! TREE_UNSIGNED (result_type)) | |
2413 | /* OK */; | |
cb3ca04e | 2414 | /* Do not warn if both operands are the same signedness. */ |
2e14370e JM |
2415 | else if (op0_signed == op1_signed) |
2416 | /* OK */; | |
293c9fdd | 2417 | else |
cb3ca04e ZW |
2418 | { |
2419 | tree sop, uop; | |
2420 | if (op0_signed) | |
2421 | sop = xop0, uop = xop1; | |
2422 | else | |
2423 | sop = xop1, uop = xop0; | |
2424 | ||
2425 | /* Do not warn if the signed quantity is an unsuffixed | |
2426 | integer literal (or some static constant expression | |
2427 | involving such literals) and it is non-negative. */ | |
2428 | if (TREE_CODE (sop) == INTEGER_CST | |
2429 | && tree_int_cst_sgn (sop) >= 0) | |
2430 | /* OK */; | |
2431 | /* Do not warn if the comparison is an equality operation, | |
2432 | the unsigned quantity is an integral constant, and it | |
2433 | would fit in the result if the result were signed. */ | |
2434 | else if (TREE_CODE (uop) == INTEGER_CST | |
2435 | && (resultcode == EQ_EXPR || resultcode == NE_EXPR) | |
2436 | && int_fits_type_p (uop, signed_type (result_type))) | |
2437 | /* OK */; | |
2438 | /* Do not warn if the unsigned quantity is an enumeration | |
2439 | constant and its maximum value would fit in the result | |
2440 | if the result were signed. */ | |
2441 | else if (TREE_CODE (uop) == INTEGER_CST | |
2442 | && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE | |
2443 | && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE(uop)), | |
2444 | signed_type (result_type))) | |
2445 | /* OK */; | |
2446 | else | |
2447 | warning ("comparison between signed and unsigned"); | |
2448 | } | |
64c01f80 DE |
2449 | |
2450 | /* Warn if two unsigned values are being compared in a size | |
2451 | larger than their original size, and one (and only one) is the | |
2452 | result of a `~' operator. This comparison will always fail. | |
2453 | ||
2454 | Also warn if one operand is a constant, and the constant | |
2455 | does not have all bits set that are set in the ~ operand | |
2456 | when it is extended. */ | |
2457 | ||
2458 | if ((TREE_CODE (primop0) == BIT_NOT_EXPR) | |
2459 | != (TREE_CODE (primop1) == BIT_NOT_EXPR)) | |
2460 | { | |
2461 | if (TREE_CODE (primop0) == BIT_NOT_EXPR) | |
2462 | primop0 = get_narrower (TREE_OPERAND (primop0, 0), | |
2463 | &unsignedp0); | |
2464 | else | |
2465 | primop1 = get_narrower (TREE_OPERAND (primop1, 0), | |
2466 | &unsignedp1); | |
2467 | ||
2468 | if (TREE_CODE (primop0) == INTEGER_CST | |
2469 | || TREE_CODE (primop1) == INTEGER_CST) | |
2470 | { | |
2471 | tree primop; | |
2472 | long constant, mask; | |
2473 | int unsignedp, bits; | |
2474 | ||
2475 | if (TREE_CODE (primop0) == INTEGER_CST) | |
2476 | { | |
2477 | primop = primop1; | |
2478 | unsignedp = unsignedp1; | |
2479 | constant = TREE_INT_CST_LOW (primop0); | |
2480 | } | |
2481 | else | |
2482 | { | |
2483 | primop = primop0; | |
2484 | unsignedp = unsignedp0; | |
2485 | constant = TREE_INT_CST_LOW (primop1); | |
2486 | } | |
2487 | ||
2488 | bits = TYPE_PRECISION (TREE_TYPE (primop)); | |
2489 | if (bits < TYPE_PRECISION (result_type) | |
2490 | && bits < HOST_BITS_PER_LONG && unsignedp) | |
2491 | { | |
2492 | mask = (~0L) << bits; | |
2493 | if ((mask & constant) != mask) | |
2494 | warning ("comparison of promoted ~unsigned with constant"); | |
2495 | } | |
2496 | } | |
2497 | else if (unsignedp0 && unsignedp1 | |
2498 | && (TYPE_PRECISION (TREE_TYPE (primop0)) | |
2499 | < TYPE_PRECISION (result_type)) | |
2500 | && (TYPE_PRECISION (TREE_TYPE (primop1)) | |
2501 | < TYPE_PRECISION (result_type))) | |
2502 | warning ("comparison of promoted ~unsigned with unsigned"); | |
2503 | } | |
400fbf9f JW |
2504 | } |
2505 | } | |
2506 | } | |
2507 | ||
2508 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. | |
2509 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
2510 | Then the expression will be built. | |
2511 | It will be given type FINAL_TYPE if that is nonzero; | |
2512 | otherwise, it will be given type RESULT_TYPE. */ | |
2513 | ||
2514 | if (!result_type) | |
2515 | { | |
2516 | binary_op_error (code); | |
2517 | return error_mark_node; | |
2518 | } | |
2519 | ||
2520 | if (! converted) | |
2521 | { | |
2522 | if (TREE_TYPE (op0) != result_type) | |
2523 | op0 = convert (result_type, op0); | |
2524 | if (TREE_TYPE (op1) != result_type) | |
2525 | op1 = convert (result_type, op1); | |
2526 | } | |
2527 | ||
293c9fdd JM |
2528 | if (build_type == NULL_TREE) |
2529 | build_type = result_type; | |
2530 | ||
400fbf9f | 2531 | { |
293c9fdd | 2532 | register tree result = build (resultcode, build_type, op0, op1); |
400fbf9f JW |
2533 | register tree folded; |
2534 | ||
2535 | folded = fold (result); | |
2536 | if (folded == result) | |
2537 | TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1); | |
2538 | if (final_type != 0) | |
2539 | return convert (final_type, folded); | |
2540 | return folded; | |
2541 | } | |
2542 | } | |
2543 | \f | |
2544 | /* Return a tree for the sum or difference (RESULTCODE says which) | |
2545 | of pointer PTROP and integer INTOP. */ | |
2546 | ||
2547 | static tree | |
2548 | pointer_int_sum (resultcode, ptrop, intop) | |
2549 | enum tree_code resultcode; | |
2550 | register tree ptrop, intop; | |
2551 | { | |
2552 | tree size_exp; | |
2553 | ||
2554 | register tree result; | |
2555 | register tree folded; | |
2556 | ||
2557 | /* The result is a pointer of the same type that is being added. */ | |
2558 | ||
2559 | register tree result_type = TREE_TYPE (ptrop); | |
2560 | ||
2561 | if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) | |
2562 | { | |
2563 | if (pedantic || warn_pointer_arith) | |
2564 | pedwarn ("pointer of type `void *' used in arithmetic"); | |
2565 | size_exp = integer_one_node; | |
2566 | } | |
2567 | else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) | |
2568 | { | |
2569 | if (pedantic || warn_pointer_arith) | |
2570 | pedwarn ("pointer to a function used in arithmetic"); | |
2571 | size_exp = integer_one_node; | |
2572 | } | |
2573 | else | |
2574 | size_exp = c_size_in_bytes (TREE_TYPE (result_type)); | |
2575 | ||
2576 | /* If what we are about to multiply by the size of the elements | |
2577 | contains a constant term, apply distributive law | |
2578 | and multiply that constant term separately. | |
2579 | This helps produce common subexpressions. */ | |
2580 | ||
2581 | if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) | |
2582 | && ! TREE_CONSTANT (intop) | |
2583 | && TREE_CONSTANT (TREE_OPERAND (intop, 1)) | |
2584 | && TREE_CONSTANT (size_exp) | |
2585 | /* If the constant comes from pointer subtraction, | |
2586 | skip this optimization--it would cause an error. */ | |
ba11c179 RK |
2587 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE |
2588 | /* If the constant is unsigned, and smaller than the pointer size, | |
2589 | then we must skip this optimization. This is because it could cause | |
2590 | an overflow error if the constant is negative but INTOP is not. */ | |
2591 | && (! TREE_UNSIGNED (TREE_TYPE (intop)) | |
2592 | || (TYPE_PRECISION (TREE_TYPE (intop)) | |
2593 | == TYPE_PRECISION (TREE_TYPE (ptrop))))) | |
400fbf9f JW |
2594 | { |
2595 | enum tree_code subcode = resultcode; | |
d45cf215 | 2596 | tree int_type = TREE_TYPE (intop); |
400fbf9f JW |
2597 | if (TREE_CODE (intop) == MINUS_EXPR) |
2598 | subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); | |
d45cf215 RS |
2599 | /* Convert both subexpression types to the type of intop, |
2600 | because weird cases involving pointer arithmetic | |
2601 | can result in a sum or difference with different type args. */ | |
2602 | ptrop = build_binary_op (subcode, ptrop, | |
2603 | convert (int_type, TREE_OPERAND (intop, 1)), 1); | |
2604 | intop = convert (int_type, TREE_OPERAND (intop, 0)); | |
400fbf9f JW |
2605 | } |
2606 | ||
b200d1aa | 2607 | /* Convert the integer argument to a type the same size as sizetype |
400fbf9f JW |
2608 | so the multiply won't overflow spuriously. */ |
2609 | ||
489af5d1 RK |
2610 | if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) |
2611 | || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype)) | |
2612 | intop = convert (type_for_size (TYPE_PRECISION (sizetype), | |
2613 | TREE_UNSIGNED (sizetype)), intop); | |
400fbf9f | 2614 | |
6946afd3 RK |
2615 | /* Replace the integer argument with a suitable product by the object size. |
2616 | Do this multiplication as signed, then convert to the appropriate | |
2617 | pointer type (actually unsigned integral). */ | |
400fbf9f | 2618 | |
6946afd3 RK |
2619 | intop = convert (result_type, |
2620 | build_binary_op (MULT_EXPR, intop, | |
2621 | convert (TREE_TYPE (intop), size_exp), 1)); | |
400fbf9f JW |
2622 | |
2623 | /* Create the sum or difference. */ | |
2624 | ||
2625 | result = build (resultcode, result_type, ptrop, intop); | |
2626 | ||
2627 | folded = fold (result); | |
2628 | if (folded == result) | |
2629 | TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop); | |
2630 | return folded; | |
2631 | } | |
2632 | ||
2633 | /* Return a tree for the difference of pointers OP0 and OP1. | |
2634 | The resulting tree has type int. */ | |
2635 | ||
2636 | static tree | |
2637 | pointer_diff (op0, op1) | |
2638 | register tree op0, op1; | |
2639 | { | |
2640 | register tree result, folded; | |
2641 | tree restype = ptrdiff_type_node; | |
2642 | ||
2643 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); | |
2644 | ||
2645 | if (pedantic || warn_pointer_arith) | |
2646 | { | |
2647 | if (TREE_CODE (target_type) == VOID_TYPE) | |
2648 | pedwarn ("pointer of type `void *' used in subtraction"); | |
2649 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
2650 | pedwarn ("pointer to a function used in subtraction"); | |
2651 | } | |
2652 | ||
2653 | /* First do the subtraction as integers; | |
04044297 MM |
2654 | then drop through to build the divide operator. |
2655 | Do not do default conversions on the minus operator | |
2656 | in case restype is a short type. */ | |
400fbf9f JW |
2657 | |
2658 | op0 = build_binary_op (MINUS_EXPR, convert (restype, op0), | |
04044297 | 2659 | convert (restype, op1), 0); |
ea8dd784 JW |
2660 | /* This generates an error if op1 is pointer to incomplete type. */ |
2661 | if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0) | |
2662 | error ("arithmetic on pointer to an incomplete type"); | |
6946afd3 | 2663 | |
ea8dd784 | 2664 | /* This generates an error if op0 is pointer to incomplete type. */ |
400fbf9f JW |
2665 | op1 = c_size_in_bytes (target_type); |
2666 | ||
2667 | /* Divide by the size, in easiest possible way. */ | |
2668 | ||
6946afd3 | 2669 | result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)); |
400fbf9f JW |
2670 | |
2671 | folded = fold (result); | |
2672 | if (folded == result) | |
2673 | TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1); | |
2674 | return folded; | |
2675 | } | |
2676 | \f | |
2677 | /* Construct and perhaps optimize a tree representation | |
2678 | for a unary operation. CODE, a tree_code, specifies the operation | |
2679 | and XARG is the operand. NOCONVERT nonzero suppresses | |
2680 | the default promotions (such as from short to int). */ | |
2681 | ||
2682 | tree | |
2683 | build_unary_op (code, xarg, noconvert) | |
2684 | enum tree_code code; | |
2685 | tree xarg; | |
2686 | int noconvert; | |
2687 | { | |
2688 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
2689 | register tree arg = xarg; | |
2690 | register tree argtype = 0; | |
2691 | register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg)); | |
400fbf9f JW |
2692 | tree val; |
2693 | ||
2694 | if (typecode == ERROR_MARK) | |
2695 | return error_mark_node; | |
2696 | if (typecode == ENUMERAL_TYPE) | |
2697 | typecode = INTEGER_TYPE; | |
2698 | ||
2699 | switch (code) | |
2700 | { | |
2701 | case CONVERT_EXPR: | |
2702 | /* This is used for unary plus, because a CONVERT_EXPR | |
2703 | is enough to prevent anybody from looking inside for | |
2704 | associativity, but won't generate any code. */ | |
b6a10c9f RS |
2705 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE |
2706 | || typecode == COMPLEX_TYPE)) | |
ab87f8c8 JL |
2707 | { |
2708 | error ("wrong type argument to unary plus"); | |
2709 | return error_mark_node; | |
2710 | } | |
400fbf9f JW |
2711 | else if (!noconvert) |
2712 | arg = default_conversion (arg); | |
2713 | break; | |
2714 | ||
2715 | case NEGATE_EXPR: | |
b6a10c9f RS |
2716 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE |
2717 | || typecode == COMPLEX_TYPE)) | |
ab87f8c8 JL |
2718 | { |
2719 | error ("wrong type argument to unary minus"); | |
2720 | return error_mark_node; | |
2721 | } | |
400fbf9f JW |
2722 | else if (!noconvert) |
2723 | arg = default_conversion (arg); | |
2724 | break; | |
2725 | ||
2726 | case BIT_NOT_EXPR: | |
1c2a9b35 RS |
2727 | if (typecode == COMPLEX_TYPE) |
2728 | { | |
2729 | code = CONJ_EXPR; | |
2730 | if (!noconvert) | |
2731 | arg = default_conversion (arg); | |
2732 | } | |
2733 | else if (typecode != INTEGER_TYPE) | |
ab87f8c8 JL |
2734 | { |
2735 | error ("wrong type argument to bit-complement"); | |
2736 | return error_mark_node; | |
2737 | } | |
400fbf9f JW |
2738 | else if (!noconvert) |
2739 | arg = default_conversion (arg); | |
2740 | break; | |
2741 | ||
2742 | case ABS_EXPR: | |
b6a10c9f RS |
2743 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE |
2744 | || typecode == COMPLEX_TYPE)) | |
ab87f8c8 JL |
2745 | { |
2746 | error ("wrong type argument to abs"); | |
2747 | return error_mark_node; | |
2748 | } | |
400fbf9f JW |
2749 | else if (!noconvert) |
2750 | arg = default_conversion (arg); | |
2751 | break; | |
2752 | ||
1c2a9b35 RS |
2753 | case CONJ_EXPR: |
2754 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
2755 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
2756 | || typecode == COMPLEX_TYPE)) | |
ab87f8c8 JL |
2757 | { |
2758 | error ("wrong type argument to conjugation"); | |
2759 | return error_mark_node; | |
2760 | } | |
1c2a9b35 RS |
2761 | else if (!noconvert) |
2762 | arg = default_conversion (arg); | |
2763 | break; | |
2764 | ||
400fbf9f JW |
2765 | case TRUTH_NOT_EXPR: |
2766 | if (typecode != INTEGER_TYPE | |
2767 | && typecode != REAL_TYPE && typecode != POINTER_TYPE | |
b6a10c9f | 2768 | && typecode != COMPLEX_TYPE |
400fbf9f JW |
2769 | /* These will convert to a pointer. */ |
2770 | && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE) | |
2771 | { | |
ab87f8c8 JL |
2772 | error ("wrong type argument to unary exclamation mark"); |
2773 | return error_mark_node; | |
400fbf9f JW |
2774 | } |
2775 | arg = truthvalue_conversion (arg); | |
2776 | return invert_truthvalue (arg); | |
2777 | ||
2778 | case NOP_EXPR: | |
2779 | break; | |
b6a10c9f RS |
2780 | |
2781 | case REALPART_EXPR: | |
2782 | if (TREE_CODE (arg) == COMPLEX_CST) | |
2783 | return TREE_REALPART (arg); | |
2784 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
2785 | return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); | |
2786 | else | |
2787 | return arg; | |
2788 | ||
2789 | case IMAGPART_EXPR: | |
2790 | if (TREE_CODE (arg) == COMPLEX_CST) | |
2791 | return TREE_IMAGPART (arg); | |
2792 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
2793 | return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); | |
2794 | else | |
2795 | return convert (TREE_TYPE (arg), integer_zero_node); | |
400fbf9f JW |
2796 | |
2797 | case PREINCREMENT_EXPR: | |
2798 | case POSTINCREMENT_EXPR: | |
2799 | case PREDECREMENT_EXPR: | |
2800 | case POSTDECREMENT_EXPR: | |
2801 | /* Handle complex lvalues (when permitted) | |
2802 | by reduction to simpler cases. */ | |
2803 | ||
2804 | val = unary_complex_lvalue (code, arg); | |
2805 | if (val != 0) | |
2806 | return val; | |
2807 | ||
b6a10c9f RS |
2808 | /* Increment or decrement the real part of the value, |
2809 | and don't change the imaginary part. */ | |
2810 | if (typecode == COMPLEX_TYPE) | |
2811 | { | |
2812 | tree real, imag; | |
2813 | ||
2814 | arg = stabilize_reference (arg); | |
2815 | real = build_unary_op (REALPART_EXPR, arg, 1); | |
2816 | imag = build_unary_op (IMAGPART_EXPR, arg, 1); | |
2817 | return build (COMPLEX_EXPR, TREE_TYPE (arg), | |
2818 | build_unary_op (code, real, 1), imag); | |
2819 | } | |
2820 | ||
400fbf9f JW |
2821 | /* Report invalid types. */ |
2822 | ||
2823 | if (typecode != POINTER_TYPE | |
2824 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE) | |
2825 | { | |
913d0833 KG |
2826 | error ("wrong type argument to %s", |
2827 | code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR | |
2828 | ? "increment" : "decrement"); | |
ab87f8c8 | 2829 | return error_mark_node; |
400fbf9f JW |
2830 | } |
2831 | ||
2832 | { | |
2833 | register tree inc; | |
2834 | tree result_type = TREE_TYPE (arg); | |
2835 | ||
2836 | arg = get_unwidened (arg, 0); | |
2837 | argtype = TREE_TYPE (arg); | |
2838 | ||
2839 | /* Compute the increment. */ | |
2840 | ||
2841 | if (typecode == POINTER_TYPE) | |
2842 | { | |
6bc4e3d0 RS |
2843 | /* If pointer target is an undefined struct, |
2844 | we just cannot know how to do the arithmetic. */ | |
2845 | if (TYPE_SIZE (TREE_TYPE (result_type)) == 0) | |
913d0833 KG |
2846 | error ("%s of pointer to unknown structure", |
2847 | code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR | |
2848 | ? "increment" : "decrement"); | |
6bc4e3d0 RS |
2849 | else if ((pedantic || warn_pointer_arith) |
2850 | && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE | |
2851 | || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)) | |
913d0833 KG |
2852 | pedwarn ("wrong type argument to %s", |
2853 | code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR | |
2854 | ? "increment" : "decrement"); | |
0e9cff7f | 2855 | inc = c_size_in_bytes (TREE_TYPE (result_type)); |
400fbf9f JW |
2856 | } |
2857 | else | |
2858 | inc = integer_one_node; | |
2859 | ||
2860 | inc = convert (argtype, inc); | |
2861 | ||
2862 | /* Handle incrementing a cast-expression. */ | |
2863 | ||
2864 | while (1) | |
2865 | switch (TREE_CODE (arg)) | |
2866 | { | |
2867 | case NOP_EXPR: | |
2868 | case CONVERT_EXPR: | |
2869 | case FLOAT_EXPR: | |
2870 | case FIX_TRUNC_EXPR: | |
2871 | case FIX_FLOOR_EXPR: | |
2872 | case FIX_ROUND_EXPR: | |
2873 | case FIX_CEIL_EXPR: | |
ee71df46 | 2874 | pedantic_lvalue_warning (CONVERT_EXPR); |
400fbf9f JW |
2875 | /* If the real type has the same machine representation |
2876 | as the type it is cast to, we can make better output | |
2877 | by adding directly to the inside of the cast. */ | |
2878 | if ((TREE_CODE (TREE_TYPE (arg)) | |
2879 | == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
2880 | && (TYPE_MODE (TREE_TYPE (arg)) | |
2881 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0))))) | |
2882 | arg = TREE_OPERAND (arg, 0); | |
2883 | else | |
2884 | { | |
2885 | tree incremented, modify, value; | |
400fbf9f JW |
2886 | arg = stabilize_reference (arg); |
2887 | if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) | |
2888 | value = arg; | |
2889 | else | |
2890 | value = save_expr (arg); | |
2891 | incremented = build (((code == PREINCREMENT_EXPR | |
2892 | || code == POSTINCREMENT_EXPR) | |
2893 | ? PLUS_EXPR : MINUS_EXPR), | |
2894 | argtype, value, inc); | |
2895 | TREE_SIDE_EFFECTS (incremented) = 1; | |
2896 | modify = build_modify_expr (arg, NOP_EXPR, incremented); | |
2897 | value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value); | |
2898 | TREE_USED (value) = 1; | |
2899 | return value; | |
2900 | } | |
2901 | break; | |
2902 | ||
2903 | default: | |
2904 | goto give_up; | |
2905 | } | |
2906 | give_up: | |
2907 | ||
2908 | /* Complain about anything else that is not a true lvalue. */ | |
2909 | if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR | |
2910 | || code == POSTINCREMENT_EXPR) | |
ab87f8c8 JL |
2911 | ? "invalid lvalue in increment" |
2912 | : "invalid lvalue in decrement"))) | |
400fbf9f JW |
2913 | return error_mark_node; |
2914 | ||
2915 | /* Report a read-only lvalue. */ | |
26b3c423 | 2916 | if (TREE_READONLY (arg)) |
400fbf9f JW |
2917 | readonly_warning (arg, |
2918 | ((code == PREINCREMENT_EXPR | |
2919 | || code == POSTINCREMENT_EXPR) | |
2920 | ? "increment" : "decrement")); | |
2921 | ||
2922 | val = build (code, TREE_TYPE (arg), arg, inc); | |
2923 | TREE_SIDE_EFFECTS (val) = 1; | |
2924 | val = convert (result_type, val); | |
2925 | if (TREE_CODE (val) != code) | |
2926 | TREE_NO_UNUSED_WARNING (val) = 1; | |
2927 | return val; | |
2928 | } | |
2929 | ||
2930 | case ADDR_EXPR: | |
2931 | /* Note that this operation never does default_conversion | |
2932 | regardless of NOCONVERT. */ | |
2933 | ||
2934 | /* Let &* cancel out to simplify resulting code. */ | |
2935 | if (TREE_CODE (arg) == INDIRECT_REF) | |
2936 | { | |
2937 | /* Don't let this be an lvalue. */ | |
2938 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
2939 | return non_lvalue (TREE_OPERAND (arg, 0)); | |
2940 | return TREE_OPERAND (arg, 0); | |
2941 | } | |
2942 | ||
2943 | /* For &x[y], return x+y */ | |
2944 | if (TREE_CODE (arg) == ARRAY_REF) | |
2945 | { | |
2946 | if (mark_addressable (TREE_OPERAND (arg, 0)) == 0) | |
2947 | return error_mark_node; | |
2948 | return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0), | |
2949 | TREE_OPERAND (arg, 1), 1); | |
2950 | } | |
2951 | ||
2952 | /* Handle complex lvalues (when permitted) | |
2953 | by reduction to simpler cases. */ | |
2954 | val = unary_complex_lvalue (code, arg); | |
2955 | if (val != 0) | |
2956 | return val; | |
2957 | ||
2958 | #if 0 /* Turned off because inconsistent; | |
2959 | float f; *&(int)f = 3.4 stores in int format | |
2960 | whereas (int)f = 3.4 stores in float format. */ | |
2961 | /* Address of a cast is just a cast of the address | |
2962 | of the operand of the cast. */ | |
2963 | switch (TREE_CODE (arg)) | |
2964 | { | |
2965 | case NOP_EXPR: | |
2966 | case CONVERT_EXPR: | |
2967 | case FLOAT_EXPR: | |
2968 | case FIX_TRUNC_EXPR: | |
2969 | case FIX_FLOOR_EXPR: | |
2970 | case FIX_ROUND_EXPR: | |
2971 | case FIX_CEIL_EXPR: | |
2972 | if (pedantic) | |
2973 | pedwarn ("ANSI C forbids the address of a cast expression"); | |
2974 | return convert (build_pointer_type (TREE_TYPE (arg)), | |
2975 | build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), | |
2976 | 0)); | |
2977 | } | |
2978 | #endif | |
2979 | ||
2980 | /* Allow the address of a constructor if all the elements | |
2981 | are constant. */ | |
2982 | if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg)) | |
2983 | ; | |
2984 | /* Anything not already handled and not a true memory reference | |
2985 | is an error. */ | |
ab87f8c8 JL |
2986 | else if (typecode != FUNCTION_TYPE |
2987 | && !lvalue_or_else (arg, "invalid lvalue in unary `&'")) | |
400fbf9f JW |
2988 | return error_mark_node; |
2989 | ||
2990 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ | |
2991 | argtype = TREE_TYPE (arg); | |
3932261a MM |
2992 | /* If the lvalue is const or volatile, merge that into the type |
2993 | to which the address will point. Note that you can't get a | |
2994 | restricted pointer by taking the address of something, so we | |
2995 | only have to deal with `const' and `volatile' here. */ | |
400fbf9f JW |
2996 | if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd' |
2997 | || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r') | |
2998 | { | |
2999 | if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) | |
3000 | argtype = c_build_type_variant (argtype, | |
3001 | TREE_READONLY (arg), | |
3002 | TREE_THIS_VOLATILE (arg)); | |
3003 | } | |
3004 | ||
3005 | argtype = build_pointer_type (argtype); | |
3006 | ||
3007 | if (mark_addressable (arg) == 0) | |
3008 | return error_mark_node; | |
3009 | ||
3010 | { | |
3011 | tree addr; | |
3012 | ||
3013 | if (TREE_CODE (arg) == COMPONENT_REF) | |
3014 | { | |
3015 | tree field = TREE_OPERAND (arg, 1); | |
3016 | ||
3017 | addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0); | |
3018 | ||
ef86d2a6 | 3019 | if (DECL_C_BIT_FIELD (field)) |
400fbf9f JW |
3020 | { |
3021 | error ("attempt to take address of bit-field structure member `%s'", | |
3022 | IDENTIFIER_POINTER (DECL_NAME (field))); | |
3023 | return error_mark_node; | |
3024 | } | |
3025 | ||
3026 | addr = convert (argtype, addr); | |
3027 | ||
3028 | if (! integer_zerop (DECL_FIELD_BITPOS (field))) | |
3029 | { | |
3030 | tree offset | |
3031 | = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field), | |
fed3cef0 | 3032 | bitsize_int (BITS_PER_UNIT)); |
400fbf9f | 3033 | int flag = TREE_CONSTANT (addr); |
fed3cef0 | 3034 | |
400fbf9f JW |
3035 | addr = fold (build (PLUS_EXPR, argtype, |
3036 | addr, convert (argtype, offset))); | |
3037 | TREE_CONSTANT (addr) = flag; | |
3038 | } | |
3039 | } | |
3040 | else | |
3041 | addr = build1 (code, argtype, arg); | |
3042 | ||
3043 | /* Address of a static or external variable or | |
8706edbc RS |
3044 | file-scope function counts as a constant. */ |
3045 | if (staticp (arg) | |
3046 | && ! (TREE_CODE (arg) == FUNCTION_DECL | |
3047 | && DECL_CONTEXT (arg) != 0)) | |
7d2d49af | 3048 | TREE_CONSTANT (addr) = 1; |
400fbf9f JW |
3049 | return addr; |
3050 | } | |
e9a25f70 JL |
3051 | |
3052 | default: | |
3053 | break; | |
400fbf9f JW |
3054 | } |
3055 | ||
ab87f8c8 JL |
3056 | if (argtype == 0) |
3057 | argtype = TREE_TYPE (arg); | |
3058 | return fold (build1 (code, argtype, arg)); | |
400fbf9f JW |
3059 | } |
3060 | ||
3061 | #if 0 | |
3062 | /* If CONVERSIONS is a conversion expression or a nested sequence of such, | |
3063 | convert ARG with the same conversions in the same order | |
3064 | and return the result. */ | |
3065 | ||
3066 | static tree | |
3067 | convert_sequence (conversions, arg) | |
3068 | tree conversions; | |
3069 | tree arg; | |
3070 | { | |
3071 | switch (TREE_CODE (conversions)) | |
3072 | { | |
3073 | case NOP_EXPR: | |
3074 | case CONVERT_EXPR: | |
3075 | case FLOAT_EXPR: | |
3076 | case FIX_TRUNC_EXPR: | |
3077 | case FIX_FLOOR_EXPR: | |
3078 | case FIX_ROUND_EXPR: | |
3079 | case FIX_CEIL_EXPR: | |
3080 | return convert (TREE_TYPE (conversions), | |
3081 | convert_sequence (TREE_OPERAND (conversions, 0), | |
3082 | arg)); | |
3083 | ||
3084 | default: | |
3085 | return arg; | |
3086 | } | |
3087 | } | |
3088 | #endif /* 0 */ | |
3089 | ||
3090 | /* Return nonzero if REF is an lvalue valid for this language. | |
3091 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
1394aabd | 3092 | Lvalues can have their address taken, unless they have DECL_REGISTER. */ |
400fbf9f JW |
3093 | |
3094 | int | |
3095 | lvalue_p (ref) | |
3096 | tree ref; | |
3097 | { | |
3098 | register enum tree_code code = TREE_CODE (ref); | |
3099 | ||
3100 | switch (code) | |
3101 | { | |
b6a10c9f RS |
3102 | case REALPART_EXPR: |
3103 | case IMAGPART_EXPR: | |
400fbf9f JW |
3104 | case COMPONENT_REF: |
3105 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
3106 | ||
3107 | case STRING_CST: | |
3108 | return 1; | |
3109 | ||
3110 | case INDIRECT_REF: | |
3111 | case ARRAY_REF: | |
3112 | case VAR_DECL: | |
3113 | case PARM_DECL: | |
3114 | case RESULT_DECL: | |
3115 | case ERROR_MARK: | |
e9a25f70 JL |
3116 | return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE |
3117 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); | |
cff9c407 RK |
3118 | |
3119 | case BIND_EXPR: | |
3120 | case RTL_EXPR: | |
e9a25f70 JL |
3121 | return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; |
3122 | ||
3123 | default: | |
3124 | return 0; | |
400fbf9f | 3125 | } |
400fbf9f JW |
3126 | } |
3127 | ||
3128 | /* Return nonzero if REF is an lvalue valid for this language; | |
3129 | otherwise, print an error message and return zero. */ | |
3130 | ||
3131 | int | |
ab87f8c8 | 3132 | lvalue_or_else (ref, msgid) |
400fbf9f | 3133 | tree ref; |
5d5993dd | 3134 | const char *msgid; |
400fbf9f JW |
3135 | { |
3136 | int win = lvalue_p (ref); | |
c5c76735 | 3137 | |
400fbf9f | 3138 | if (! win) |
913d0833 | 3139 | error ("%s", msgid); |
c5c76735 | 3140 | |
400fbf9f JW |
3141 | return win; |
3142 | } | |
3143 | ||
3144 | /* Apply unary lvalue-demanding operator CODE to the expression ARG | |
3145 | for certain kinds of expressions which are not really lvalues | |
3146 | but which we can accept as lvalues. | |
3147 | ||
3148 | If ARG is not a kind of expression we can handle, return zero. */ | |
3149 | ||
3150 | static tree | |
3151 | unary_complex_lvalue (code, arg) | |
3152 | enum tree_code code; | |
3153 | tree arg; | |
3154 | { | |
3155 | /* Handle (a, b) used as an "lvalue". */ | |
3156 | if (TREE_CODE (arg) == COMPOUND_EXPR) | |
3157 | { | |
3158 | tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0); | |
e9a25f70 JL |
3159 | |
3160 | /* If this returns a function type, it isn't really being used as | |
3161 | an lvalue, so don't issue a warning about it. */ | |
3162 | if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE) | |
3163 | pedantic_lvalue_warning (COMPOUND_EXPR); | |
3164 | ||
400fbf9f JW |
3165 | return build (COMPOUND_EXPR, TREE_TYPE (real_result), |
3166 | TREE_OPERAND (arg, 0), real_result); | |
3167 | } | |
3168 | ||
3169 | /* Handle (a ? b : c) used as an "lvalue". */ | |
3170 | if (TREE_CODE (arg) == COND_EXPR) | |
3171 | { | |
3172 | pedantic_lvalue_warning (COND_EXPR); | |
e9a25f70 JL |
3173 | if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE) |
3174 | pedantic_lvalue_warning (COMPOUND_EXPR); | |
3175 | ||
400fbf9f JW |
3176 | return (build_conditional_expr |
3177 | (TREE_OPERAND (arg, 0), | |
3178 | build_unary_op (code, TREE_OPERAND (arg, 1), 0), | |
3179 | build_unary_op (code, TREE_OPERAND (arg, 2), 0))); | |
3180 | } | |
3181 | ||
3182 | return 0; | |
3183 | } | |
3184 | ||
3185 | /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR | |
3186 | COMPOUND_EXPR, or CONVERT_EXPR (for casts). */ | |
3187 | ||
3188 | static void | |
3189 | pedantic_lvalue_warning (code) | |
3190 | enum tree_code code; | |
3191 | { | |
3192 | if (pedantic) | |
913d0833 KG |
3193 | switch (code) |
3194 | { | |
3195 | case COND_EXPR: | |
3196 | pedwarn ("ANSI C forbids use of conditional expressions as lvalues"); | |
3197 | break; | |
3198 | case COMPOUND_EXPR: | |
3199 | pedwarn ("ANSI C forbids use of compound expressions as lvalues"); | |
3200 | break; | |
3201 | default: | |
3202 | pedwarn ("ANSI C forbids use of cast expressions as lvalues"); | |
3203 | break; | |
3204 | } | |
400fbf9f JW |
3205 | } |
3206 | \f | |
3207 | /* Warn about storing in something that is `const'. */ | |
3208 | ||
3209 | void | |
ab87f8c8 | 3210 | readonly_warning (arg, msgid) |
400fbf9f | 3211 | tree arg; |
5d5993dd | 3212 | const char *msgid; |
400fbf9f | 3213 | { |
3791970d | 3214 | /* Forbid assignments to iterators. */ |
550707f7 | 3215 | if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg)) |
ab87f8c8 JL |
3216 | pedwarn ("%s of iterator `%s'", _(msgid), |
3217 | IDENTIFIER_POINTER (DECL_NAME (arg))); | |
3791970d | 3218 | |
400fbf9f JW |
3219 | if (TREE_CODE (arg) == COMPONENT_REF) |
3220 | { | |
3221 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
ab87f8c8 | 3222 | readonly_warning (TREE_OPERAND (arg, 0), msgid); |
400fbf9f | 3223 | else |
ab87f8c8 JL |
3224 | pedwarn ("%s of read-only member `%s'", _(msgid), |
3225 | IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1)))); | |
400fbf9f JW |
3226 | } |
3227 | else if (TREE_CODE (arg) == VAR_DECL) | |
ab87f8c8 JL |
3228 | pedwarn ("%s of read-only variable `%s'", _(msgid), |
3229 | IDENTIFIER_POINTER (DECL_NAME (arg))); | |
400fbf9f | 3230 | else |
ab87f8c8 | 3231 | pedwarn ("%s of read-only location", _(msgid)); |
400fbf9f JW |
3232 | } |
3233 | \f | |
3234 | /* Mark EXP saying that we need to be able to take the | |
3235 | address of it; it should not be allocated in a register. | |
3236 | Value is 1 if successful. */ | |
3237 | ||
3238 | int | |
3239 | mark_addressable (exp) | |
3240 | tree exp; | |
3241 | { | |
3242 | register tree x = exp; | |
3243 | while (1) | |
3244 | switch (TREE_CODE (x)) | |
3245 | { | |
400fbf9f | 3246 | case COMPONENT_REF: |
1598f4da | 3247 | if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) |
36c336d1 RK |
3248 | { |
3249 | error ("cannot take address of bitfield `%s'", | |
3250 | IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1)))); | |
3251 | return 0; | |
3252 | } | |
1598f4da | 3253 | |
0f41302f | 3254 | /* ... fall through ... */ |
1598f4da RK |
3255 | |
3256 | case ADDR_EXPR: | |
400fbf9f | 3257 | case ARRAY_REF: |
ce95080d RS |
3258 | case REALPART_EXPR: |
3259 | case IMAGPART_EXPR: | |
400fbf9f JW |
3260 | x = TREE_OPERAND (x, 0); |
3261 | break; | |
3262 | ||
3263 | case CONSTRUCTOR: | |
3264 | TREE_ADDRESSABLE (x) = 1; | |
3265 | return 1; | |
3266 | ||
3267 | case VAR_DECL: | |
3268 | case CONST_DECL: | |
3269 | case PARM_DECL: | |
3270 | case RESULT_DECL: | |
1394aabd RS |
3271 | if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) |
3272 | && DECL_NONLOCAL (x)) | |
4bb6d2f8 RS |
3273 | { |
3274 | if (TREE_PUBLIC (x)) | |
3275 | { | |
3276 | error ("global register variable `%s' used in nested function", | |
3277 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
3278 | return 0; | |
3279 | } | |
3280 | pedwarn ("register variable `%s' used in nested function", | |
3281 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
3282 | } | |
1394aabd | 3283 | else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)) |
400fbf9f JW |
3284 | { |
3285 | if (TREE_PUBLIC (x)) | |
3286 | { | |
3287 | error ("address of global register variable `%s' requested", | |
3288 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
3289 | return 0; | |
3290 | } | |
bbbd6700 RK |
3291 | |
3292 | /* If we are making this addressable due to its having | |
3293 | volatile components, give a different error message. Also | |
3294 | handle the case of an unnamed parameter by not trying | |
3295 | to give the name. */ | |
3296 | ||
3297 | else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x))) | |
3298 | { | |
3299 | error ("cannot put object with volatile field into register"); | |
3300 | return 0; | |
3301 | } | |
3302 | ||
400fbf9f JW |
3303 | pedwarn ("address of register variable `%s' requested", |
3304 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
3305 | } | |
3306 | put_var_into_stack (x); | |
3307 | ||
3308 | /* drops in */ | |
3309 | case FUNCTION_DECL: | |
3310 | TREE_ADDRESSABLE (x) = 1; | |
3311 | #if 0 /* poplevel deals with this now. */ | |
3312 | if (DECL_CONTEXT (x) == 0) | |
3313 | TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1; | |
3314 | #endif | |
3315 | ||
3316 | default: | |
3317 | return 1; | |
3318 | } | |
3319 | } | |
3320 | \f | |
3321 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. */ | |
3322 | ||
3323 | tree | |
3324 | build_conditional_expr (ifexp, op1, op2) | |
3325 | tree ifexp, op1, op2; | |
3326 | { | |
3327 | register tree type1; | |
3328 | register tree type2; | |
3329 | register enum tree_code code1; | |
3330 | register enum tree_code code2; | |
3331 | register tree result_type = NULL; | |
fd5d5b94 | 3332 | tree orig_op1 = op1, orig_op2 = op2; |
400fbf9f | 3333 | |
400fbf9f JW |
3334 | ifexp = truthvalue_conversion (default_conversion (ifexp)); |
3335 | ||
400fbf9f JW |
3336 | #if 0 /* Produces wrong result if within sizeof. */ |
3337 | /* Don't promote the operands separately if they promote | |
3338 | the same way. Return the unpromoted type and let the combined | |
3339 | value get promoted if necessary. */ | |
3340 | ||
3341 | if (TREE_TYPE (op1) == TREE_TYPE (op2) | |
3342 | && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE | |
3343 | && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE | |
3344 | && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE) | |
3345 | { | |
3346 | if (TREE_CODE (ifexp) == INTEGER_CST) | |
a29f2ec1 | 3347 | return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1); |
400fbf9f JW |
3348 | |
3349 | return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2)); | |
3350 | } | |
3351 | #endif | |
3352 | ||
e855c5ce | 3353 | /* Promote both alternatives. */ |
400fbf9f JW |
3354 | |
3355 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
3356 | op1 = default_conversion (op1); | |
3357 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
3358 | op2 = default_conversion (op2); | |
3359 | ||
e855c5ce RS |
3360 | if (TREE_CODE (ifexp) == ERROR_MARK |
3361 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
3362 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
3363 | return error_mark_node; | |
3364 | ||
400fbf9f JW |
3365 | type1 = TREE_TYPE (op1); |
3366 | code1 = TREE_CODE (type1); | |
3367 | type2 = TREE_TYPE (op2); | |
3368 | code2 = TREE_CODE (type2); | |
3369 | ||
3370 | /* Quickly detect the usual case where op1 and op2 have the same type | |
3371 | after promotion. */ | |
1ad409d2 RS |
3372 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) |
3373 | { | |
3374 | if (type1 == type2) | |
3375 | result_type = type1; | |
3376 | else | |
3377 | result_type = TYPE_MAIN_VARIANT (type1); | |
3378 | } | |
400fbf9f JW |
3379 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE) |
3380 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE)) | |
3381 | { | |
3382 | result_type = common_type (type1, type2); | |
cb3ca04e ZW |
3383 | |
3384 | /* If -Wsign-compare, warn here if type1 and type2 have | |
3385 | different signedness. We'll promote the signed to unsigned | |
3386 | and later code won't know it used to be different. | |
3387 | Do this check on the original types, so that explicit casts | |
3388 | will be considered, but default promotions won't. */ | |
3389 | if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare) | |
3390 | && !skip_evaluation) | |
3391 | { | |
3392 | int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1)); | |
3393 | int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2)); | |
3394 | ||
3395 | if (unsigned_op1 ^ unsigned_op2) | |
3396 | { | |
3397 | /* Do not warn if the result type is signed, since the | |
3398 | signed type will only be chosen if it can represent | |
3399 | all the values of the unsigned type. */ | |
3400 | if (! TREE_UNSIGNED (result_type)) | |
3401 | /* OK */; | |
3402 | /* Do not warn if the signed quantity is an unsuffixed | |
3403 | integer literal (or some static constant expression | |
3404 | involving such literals) and it is non-negative. */ | |
3405 | else if ((unsigned_op2 && TREE_CODE (op1) == INTEGER_CST | |
3406 | && tree_int_cst_sgn (op1) >= 0) | |
3407 | || (unsigned_op1 && TREE_CODE (op2) == INTEGER_CST | |
3408 | && tree_int_cst_sgn (op2) >= 0)) | |
3409 | /* OK */; | |
3410 | else | |
3411 | warning ("signed and unsigned type in conditional expression"); | |
3412 | } | |
3413 | } | |
400fbf9f JW |
3414 | } |
3415 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
3416 | { | |
3417 | if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE)) | |
3418 | pedwarn ("ANSI C forbids conditional expr with only one void side"); | |
3419 | result_type = void_type_node; | |
3420 | } | |
3421 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
3422 | { | |
3423 | if (comp_target_types (type1, type2)) | |
3424 | result_type = common_type (type1, type2); | |
fd5d5b94 RS |
3425 | else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node |
3426 | && TREE_CODE (orig_op1) != NOP_EXPR) | |
400fbf9f | 3427 | result_type = qualify_type (type2, type1); |
fd5d5b94 RS |
3428 | else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node |
3429 | && TREE_CODE (orig_op2) != NOP_EXPR) | |
400fbf9f JW |
3430 | result_type = qualify_type (type1, type2); |
3431 | else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node) | |
3432 | { | |
3433 | if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) | |
3434 | pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer"); | |
3435 | result_type = qualify_type (type1, type2); | |
3436 | } | |
3437 | else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node) | |
3438 | { | |
3439 | if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) | |
3440 | pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer"); | |
3441 | result_type = qualify_type (type2, type1); | |
3442 | } | |
3443 | else | |
3444 | { | |
3445 | pedwarn ("pointer type mismatch in conditional expression"); | |
3446 | result_type = build_pointer_type (void_type_node); | |
3447 | } | |
3448 | } | |
3449 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
3450 | { | |
3451 | if (! integer_zerop (op2)) | |
3452 | pedwarn ("pointer/integer type mismatch in conditional expression"); | |
3453 | else | |
3454 | { | |
3455 | op2 = null_pointer_node; | |
3456 | #if 0 /* The spec seems to say this is permitted. */ | |
3457 | if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE) | |
3458 | pedwarn ("ANSI C forbids conditional expr between 0 and function pointer"); | |
3459 | #endif | |
3460 | } | |
3461 | result_type = type1; | |
3462 | } | |
3463 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
3464 | { | |
3465 | if (!integer_zerop (op1)) | |
3466 | pedwarn ("pointer/integer type mismatch in conditional expression"); | |
3467 | else | |
3468 | { | |
3469 | op1 = null_pointer_node; | |
3470 | #if 0 /* The spec seems to say this is permitted. */ | |
3471 | if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE) | |
3472 | pedwarn ("ANSI C forbids conditional expr between 0 and function pointer"); | |
3473 | #endif | |
3474 | } | |
3475 | result_type = type2; | |
3476 | } | |
3477 | ||
3478 | if (!result_type) | |
3479 | { | |
3480 | if (flag_cond_mismatch) | |
3481 | result_type = void_type_node; | |
3482 | else | |
3483 | { | |
3484 | error ("type mismatch in conditional expression"); | |
3485 | return error_mark_node; | |
3486 | } | |
3487 | } | |
3488 | ||
1dfdf85d RS |
3489 | /* Merge const and volatile flags of the incoming types. */ |
3490 | result_type | |
3491 | = build_type_variant (result_type, | |
48c73063 RS |
3492 | TREE_READONLY (op1) || TREE_READONLY (op2), |
3493 | TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2)); | |
e58cd767 | 3494 | |
400fbf9f | 3495 | if (result_type != TREE_TYPE (op1)) |
e58cd767 | 3496 | op1 = convert_and_check (result_type, op1); |
400fbf9f | 3497 | if (result_type != TREE_TYPE (op2)) |
e58cd767 | 3498 | op2 = convert_and_check (result_type, op2); |
400fbf9f | 3499 | |
5abb45f2 | 3500 | if (TREE_CODE (ifexp) == INTEGER_CST) |
a29f2ec1 | 3501 | return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1); |
400fbf9f | 3502 | |
400fbf9f JW |
3503 | return fold (build (COND_EXPR, result_type, ifexp, op1, op2)); |
3504 | } | |
3505 | \f | |
3506 | /* Given a list of expressions, return a compound expression | |
3507 | that performs them all and returns the value of the last of them. */ | |
3508 | ||
3509 | tree | |
3510 | build_compound_expr (list) | |
3511 | tree list; | |
82bde854 | 3512 | { |
43a5a542 | 3513 | return internal_build_compound_expr (list, TRUE); |
82bde854 MM |
3514 | } |
3515 | ||
3516 | static tree | |
3517 | internal_build_compound_expr (list, first_p) | |
3518 | tree list; | |
3519 | int first_p; | |
400fbf9f JW |
3520 | { |
3521 | register tree rest; | |
3522 | ||
3523 | if (TREE_CHAIN (list) == 0) | |
3524 | { | |
6dc42e49 | 3525 | #if 0 /* If something inside inhibited lvalueness, we should not override. */ |
400fbf9f JW |
3526 | /* Consider (x, y+0), which is not an lvalue since y+0 is not. */ |
3527 | ||
3528 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
3529 | if (TREE_CODE (list) == NON_LVALUE_EXPR) | |
3530 | list = TREE_OPERAND (list, 0); | |
3531 | #endif | |
3532 | ||
439f6027 | 3533 | /* Don't let (0, 0) be null pointer constant. */ |
82bde854 | 3534 | if (!first_p && integer_zerop (TREE_VALUE (list))) |
439f6027 RS |
3535 | return non_lvalue (TREE_VALUE (list)); |
3536 | return TREE_VALUE (list); | |
400fbf9f JW |
3537 | } |
3538 | ||
3539 | if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0) | |
3540 | { | |
3541 | /* Convert arrays to pointers when there really is a comma operator. */ | |
3542 | if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE) | |
3543 | TREE_VALUE (TREE_CHAIN (list)) | |
3544 | = default_conversion (TREE_VALUE (TREE_CHAIN (list))); | |
3545 | } | |
3546 | ||
82bde854 | 3547 | rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE); |
400fbf9f | 3548 | |
0e7c47fa RK |
3549 | if (! TREE_SIDE_EFFECTS (TREE_VALUE (list))) |
3550 | { | |
3551 | /* The left-hand operand of a comma expression is like an expression | |
3552 | statement: with -W or -Wunused, we should warn if it doesn't have | |
3553 | any side-effects, unless it was explicitly cast to (void). */ | |
3554 | if ((extra_warnings || warn_unused) | |
3555 | && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR | |
3556 | && TREE_TYPE (TREE_VALUE (list)) == void_type_node)) | |
3557 | warning ("left-hand operand of comma expression has no effect"); | |
3558 | ||
3559 | /* When pedantic, a compound expression can be neither an lvalue | |
3560 | nor an integer constant expression. */ | |
3561 | if (! pedantic) | |
3562 | return rest; | |
3563 | } | |
3564 | ||
3565 | /* With -Wunused, we should also warn if the left-hand operand does have | |
3566 | side-effects, but computes a value which is not used. For example, in | |
3567 | `foo() + bar(), baz()' the result of the `+' operator is not used, | |
3568 | so we should issue a warning. */ | |
3569 | else if (warn_unused) | |
3570 | warn_if_unused_value (TREE_VALUE (list)); | |
400fbf9f JW |
3571 | |
3572 | return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest); | |
3573 | } | |
3574 | ||
3575 | /* Build an expression representing a cast to type TYPE of expression EXPR. */ | |
3576 | ||
3577 | tree | |
3578 | build_c_cast (type, expr) | |
3579 | register tree type; | |
3580 | tree expr; | |
3581 | { | |
3582 | register tree value = expr; | |
3583 | ||
3584 | if (type == error_mark_node || expr == error_mark_node) | |
3585 | return error_mark_node; | |
3586 | type = TYPE_MAIN_VARIANT (type); | |
3587 | ||
3588 | #if 0 | |
3589 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
3590 | if (TREE_CODE (value) == NON_LVALUE_EXPR) | |
3591 | value = TREE_OPERAND (value, 0); | |
3592 | #endif | |
3593 | ||
3594 | if (TREE_CODE (type) == ARRAY_TYPE) | |
3595 | { | |
3596 | error ("cast specifies array type"); | |
3597 | return error_mark_node; | |
3598 | } | |
3599 | ||
3600 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
3601 | { | |
3602 | error ("cast specifies function type"); | |
3603 | return error_mark_node; | |
3604 | } | |
3605 | ||
3606 | if (type == TREE_TYPE (value)) | |
3607 | { | |
3608 | if (pedantic) | |
3609 | { | |
3610 | if (TREE_CODE (type) == RECORD_TYPE | |
3611 | || TREE_CODE (type) == UNION_TYPE) | |
3612 | pedwarn ("ANSI C forbids casting nonscalar to the same type"); | |
3613 | } | |
3614 | } | |
3615 | else if (TREE_CODE (type) == UNION_TYPE) | |
3616 | { | |
3617 | tree field; | |
0c16ddf7 RS |
3618 | if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE |
3619 | || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE) | |
3620 | value = default_conversion (value); | |
3621 | ||
400fbf9f JW |
3622 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) |
3623 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
3624 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) | |
3625 | break; | |
3626 | ||
3627 | if (field) | |
3628 | { | |
5d5993dd | 3629 | const char *name; |
281ec92f | 3630 | tree t; |
805f961c | 3631 | |
400fbf9f JW |
3632 | if (pedantic) |
3633 | pedwarn ("ANSI C forbids casts to union type"); | |
805f961c RS |
3634 | if (TYPE_NAME (type) != 0) |
3635 | { | |
3636 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
3637 | name = IDENTIFIER_POINTER (TYPE_NAME (type)); | |
3638 | else | |
3639 | name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))); | |
3640 | } | |
3641 | else | |
3642 | name = ""; | |
281ec92f RS |
3643 | t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE, |
3644 | build_tree_list (field, value)), | |
3645 | 0, 0); | |
3646 | TREE_CONSTANT (t) = TREE_CONSTANT (value); | |
3647 | return t; | |
400fbf9f JW |
3648 | } |
3649 | error ("cast to union type from type not present in union"); | |
3650 | return error_mark_node; | |
3651 | } | |
3652 | else | |
3653 | { | |
10d5caec | 3654 | tree otype, ovalue; |
53b01f59 RS |
3655 | |
3656 | /* If casting to void, avoid the error that would come | |
3657 | from default_conversion in the case of a non-lvalue array. */ | |
3658 | if (type == void_type_node) | |
3659 | return build1 (CONVERT_EXPR, type, value); | |
3660 | ||
400fbf9f JW |
3661 | /* Convert functions and arrays to pointers, |
3662 | but don't convert any other types. */ | |
3663 | if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE | |
3664 | || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE) | |
3665 | value = default_conversion (value); | |
3666 | otype = TREE_TYPE (value); | |
3667 | ||
d45cf215 | 3668 | /* Optionally warn about potentially worrisome casts. */ |
400fbf9f JW |
3669 | |
3670 | if (warn_cast_qual | |
3671 | && TREE_CODE (type) == POINTER_TYPE | |
3672 | && TREE_CODE (otype) == POINTER_TYPE) | |
3673 | { | |
f5963e61 JL |
3674 | tree in_type = type; |
3675 | tree in_otype = otype; | |
cd6311ef | 3676 | int warn = 0; |
f5963e61 | 3677 | |
cd6311ef ZW |
3678 | /* Check that the qualifiers on IN_TYPE are a superset of |
3679 | the qualifiers of IN_OTYPE. The outermost level of | |
3680 | POINTER_TYPE nodes is uninteresting and we stop as soon | |
3681 | as we hit a non-POINTER_TYPE node on either type. */ | |
3682 | do | |
3683 | { | |
3684 | in_otype = TREE_TYPE (in_otype); | |
3685 | in_type = TREE_TYPE (in_type); | |
3686 | warn |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type)); | |
3687 | } | |
3688 | while (TREE_CODE (in_type) == POINTER_TYPE | |
3689 | && TREE_CODE (in_otype) == POINTER_TYPE); | |
3690 | ||
3691 | if (warn) | |
3932261a MM |
3692 | /* There are qualifiers present in IN_OTYPE that are not |
3693 | present in IN_TYPE. */ | |
3694 | pedwarn ("cast discards qualifiers from pointer target type"); | |
400fbf9f JW |
3695 | } |
3696 | ||
3697 | /* Warn about possible alignment problems. */ | |
d45cf215 | 3698 | if (STRICT_ALIGNMENT && warn_cast_align |
400fbf9f JW |
3699 | && TREE_CODE (type) == POINTER_TYPE |
3700 | && TREE_CODE (otype) == POINTER_TYPE | |
3701 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
3702 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
ec9aa895 RK |
3703 | /* Don't warn about opaque types, where the actual alignment |
3704 | restriction is unknown. */ | |
3705 | && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE | |
3706 | || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) | |
3707 | && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) | |
400fbf9f JW |
3708 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) |
3709 | warning ("cast increases required alignment of target type"); | |
400fbf9f JW |
3710 | |
3711 | if (TREE_CODE (type) == INTEGER_TYPE | |
3712 | && TREE_CODE (otype) == POINTER_TYPE | |
c9b7f31c RS |
3713 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) |
3714 | && !TREE_CONSTANT (value)) | |
400fbf9f JW |
3715 | warning ("cast from pointer to integer of different size"); |
3716 | ||
796bb373 RK |
3717 | if (warn_bad_function_cast |
3718 | && TREE_CODE (value) == CALL_EXPR | |
3719 | && TREE_CODE (type) != TREE_CODE (otype)) | |
3720 | warning ("cast does not match function type"); | |
3721 | ||
400fbf9f JW |
3722 | if (TREE_CODE (type) == POINTER_TYPE |
3723 | && TREE_CODE (otype) == INTEGER_TYPE | |
2918ed3c | 3724 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) |
c9b7f31c | 3725 | #if 0 |
2918ed3c RS |
3726 | /* Don't warn about converting 0 to pointer, |
3727 | provided the 0 was explicit--not cast or made by folding. */ | |
c9b7f31c RS |
3728 | && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)) |
3729 | #endif | |
3730 | /* Don't warn about converting any constant. */ | |
3731 | && !TREE_CONSTANT (value)) | |
400fbf9f JW |
3732 | warning ("cast to pointer from integer of different size"); |
3733 | ||
10d5caec | 3734 | ovalue = value; |
400fbf9f | 3735 | value = convert (type, value); |
e58cd767 RS |
3736 | |
3737 | /* Ignore any integer overflow caused by the cast. */ | |
3738 | if (TREE_CODE (value) == INTEGER_CST) | |
10d5caec PE |
3739 | { |
3740 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); | |
3741 | TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue); | |
3742 | } | |
400fbf9f JW |
3743 | } |
3744 | ||
fd5d5b94 RS |
3745 | /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */ |
3746 | if (pedantic && TREE_CODE (value) == INTEGER_CST | |
3747 | && TREE_CODE (expr) == INTEGER_CST | |
3748 | && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE) | |
3749 | value = non_lvalue (value); | |
3750 | ||
3751 | /* If pedantic, don't let a cast be an lvalue. */ | |
400fbf9f | 3752 | if (value == expr && pedantic) |
fd5d5b94 RS |
3753 | value = non_lvalue (value); |
3754 | ||
400fbf9f JW |
3755 | return value; |
3756 | } | |
3757 | \f | |
3758 | /* Build an assignment expression of lvalue LHS from value RHS. | |
3759 | MODIFYCODE is the code for a binary operator that we use | |
3760 | to combine the old value of LHS with RHS to get the new value. | |
3761 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */ | |
3762 | ||
3763 | tree | |
3764 | build_modify_expr (lhs, modifycode, rhs) | |
3765 | tree lhs, rhs; | |
3766 | enum tree_code modifycode; | |
3767 | { | |
3768 | register tree result; | |
3769 | tree newrhs; | |
3770 | tree lhstype = TREE_TYPE (lhs); | |
3771 | tree olhstype = lhstype; | |
3772 | ||
3773 | /* Types that aren't fully specified cannot be used in assignments. */ | |
3774 | lhs = require_complete_type (lhs); | |
3775 | ||
3776 | /* Avoid duplicate error messages from operands that had errors. */ | |
3777 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
3778 | return error_mark_node; | |
3779 | ||
3780 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
fc76e425 RS |
3781 | /* Do not use STRIP_NOPS here. We do not want an enumerator |
3782 | whose value is 0 to count as a null pointer constant. */ | |
400fbf9f JW |
3783 | if (TREE_CODE (rhs) == NON_LVALUE_EXPR) |
3784 | rhs = TREE_OPERAND (rhs, 0); | |
3785 | ||
3786 | newrhs = rhs; | |
3787 | ||
3788 | /* Handle control structure constructs used as "lvalues". */ | |
3789 | ||
3790 | switch (TREE_CODE (lhs)) | |
3791 | { | |
3792 | /* Handle (a, b) used as an "lvalue". */ | |
3793 | case COMPOUND_EXPR: | |
3794 | pedantic_lvalue_warning (COMPOUND_EXPR); | |
c5c76735 | 3795 | newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs); |
19d76e60 RK |
3796 | if (TREE_CODE (newrhs) == ERROR_MARK) |
3797 | return error_mark_node; | |
400fbf9f | 3798 | return build (COMPOUND_EXPR, lhstype, |
19d76e60 RK |
3799 | TREE_OPERAND (lhs, 0), newrhs); |
3800 | ||
400fbf9f JW |
3801 | /* Handle (a ? b : c) used as an "lvalue". */ |
3802 | case COND_EXPR: | |
3803 | pedantic_lvalue_warning (COND_EXPR); | |
3804 | rhs = save_expr (rhs); | |
3805 | { | |
3806 | /* Produce (a ? (b = rhs) : (c = rhs)) | |
3807 | except that the RHS goes through a save-expr | |
3808 | so the code to compute it is only emitted once. */ | |
3809 | tree cond | |
3810 | = build_conditional_expr (TREE_OPERAND (lhs, 0), | |
3811 | build_modify_expr (TREE_OPERAND (lhs, 1), | |
3812 | modifycode, rhs), | |
3813 | build_modify_expr (TREE_OPERAND (lhs, 2), | |
3814 | modifycode, rhs)); | |
19d76e60 RK |
3815 | if (TREE_CODE (cond) == ERROR_MARK) |
3816 | return cond; | |
400fbf9f JW |
3817 | /* Make sure the code to compute the rhs comes out |
3818 | before the split. */ | |
3819 | return build (COMPOUND_EXPR, TREE_TYPE (lhs), | |
3820 | /* But cast it to void to avoid an "unused" error. */ | |
3821 | convert (void_type_node, rhs), cond); | |
3822 | } | |
e9a25f70 JL |
3823 | default: |
3824 | break; | |
400fbf9f JW |
3825 | } |
3826 | ||
3827 | /* If a binary op has been requested, combine the old LHS value with the RHS | |
3828 | producing the value we should actually store into the LHS. */ | |
3829 | ||
3830 | if (modifycode != NOP_EXPR) | |
3831 | { | |
3832 | lhs = stabilize_reference (lhs); | |
3833 | newrhs = build_binary_op (modifycode, lhs, rhs, 1); | |
3834 | } | |
3835 | ||
3836 | /* Handle a cast used as an "lvalue". | |
3837 | We have already performed any binary operator using the value as cast. | |
3838 | Now convert the result to the cast type of the lhs, | |
3839 | and then true type of the lhs and store it there; | |
3840 | then convert result back to the cast type to be the value | |
3841 | of the assignment. */ | |
3842 | ||
3843 | switch (TREE_CODE (lhs)) | |
3844 | { | |
3845 | case NOP_EXPR: | |
3846 | case CONVERT_EXPR: | |
3847 | case FLOAT_EXPR: | |
3848 | case FIX_TRUNC_EXPR: | |
3849 | case FIX_FLOOR_EXPR: | |
3850 | case FIX_ROUND_EXPR: | |
3851 | case FIX_CEIL_EXPR: | |
3852 | if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE | |
3853 | || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE) | |
3854 | newrhs = default_conversion (newrhs); | |
3855 | { | |
3856 | tree inner_lhs = TREE_OPERAND (lhs, 0); | |
3857 | tree result; | |
3858 | result = build_modify_expr (inner_lhs, NOP_EXPR, | |
3859 | convert (TREE_TYPE (inner_lhs), | |
3860 | convert (lhstype, newrhs))); | |
19d76e60 RK |
3861 | if (TREE_CODE (result) == ERROR_MARK) |
3862 | return result; | |
400fbf9f JW |
3863 | pedantic_lvalue_warning (CONVERT_EXPR); |
3864 | return convert (TREE_TYPE (lhs), result); | |
3865 | } | |
e9a25f70 JL |
3866 | |
3867 | default: | |
3868 | break; | |
400fbf9f JW |
3869 | } |
3870 | ||
3871 | /* Now we have handled acceptable kinds of LHS that are not truly lvalues. | |
3872 | Reject anything strange now. */ | |
3873 | ||
ab87f8c8 | 3874 | if (!lvalue_or_else (lhs, "invalid lvalue in assignment")) |
400fbf9f JW |
3875 | return error_mark_node; |
3876 | ||
3877 | /* Warn about storing in something that is `const'. */ | |
3878 | ||
3879 | if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype) | |
3880 | || ((TREE_CODE (lhstype) == RECORD_TYPE | |
3881 | || TREE_CODE (lhstype) == UNION_TYPE) | |
3882 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
3883 | readonly_warning (lhs, "assignment"); | |
3884 | ||
3885 | /* If storing into a structure or union member, | |
3886 | it has probably been given type `int'. | |
3887 | Compute the type that would go with | |
3888 | the actual amount of storage the member occupies. */ | |
3889 | ||
3890 | if (TREE_CODE (lhs) == COMPONENT_REF | |
3891 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
3892 | || TREE_CODE (lhstype) == REAL_TYPE | |
3893 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
3894 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
3895 | ||
3896 | /* If storing in a field that is in actuality a short or narrower than one, | |
3897 | we must store in the field in its actual type. */ | |
3898 | ||
3899 | if (lhstype != TREE_TYPE (lhs)) | |
3900 | { | |
3901 | lhs = copy_node (lhs); | |
3902 | TREE_TYPE (lhs) = lhstype; | |
3903 | } | |
3904 | ||
3905 | /* Convert new value to destination type. */ | |
3906 | ||
ab87f8c8 | 3907 | newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"), |
9b7267b8 | 3908 | NULL_TREE, NULL_TREE, 0); |
400fbf9f JW |
3909 | if (TREE_CODE (newrhs) == ERROR_MARK) |
3910 | return error_mark_node; | |
3911 | ||
3912 | result = build (MODIFY_EXPR, lhstype, lhs, newrhs); | |
3913 | TREE_SIDE_EFFECTS (result) = 1; | |
3914 | ||
3915 | /* If we got the LHS in a different type for storing in, | |
3916 | convert the result back to the nominal type of LHS | |
3917 | so that the value we return always has the same type | |
3918 | as the LHS argument. */ | |
3919 | ||
3920 | if (olhstype == TREE_TYPE (result)) | |
3921 | return result; | |
ab87f8c8 | 3922 | return convert_for_assignment (olhstype, result, _("assignment"), |
9b7267b8 | 3923 | NULL_TREE, NULL_TREE, 0); |
400fbf9f JW |
3924 | } |
3925 | \f | |
3926 | /* Convert value RHS to type TYPE as preparation for an assignment | |
3927 | to an lvalue of type TYPE. | |
3928 | The real work of conversion is done by `convert'. | |
3929 | The purpose of this function is to generate error messages | |
3930 | for assignments that are not allowed in C. | |
3931 | ERRTYPE is a string to use in error messages: | |
3932 | "assignment", "return", etc. If it is null, this is parameter passing | |
ab87f8c8 | 3933 | for a function call (and different error messages are output). |
400fbf9f JW |
3934 | |
3935 | FUNNAME is the name of the function being called, | |
3936 | as an IDENTIFIER_NODE, or null. | |
3937 | PARMNUM is the number of the argument, for printing in error messages. */ | |
3938 | ||
3939 | static tree | |
9b7267b8 | 3940 | convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum) |
400fbf9f | 3941 | tree type, rhs; |
5d5993dd | 3942 | const char *errtype; |
9b7267b8 | 3943 | tree fundecl, funname; |
400fbf9f JW |
3944 | int parmnum; |
3945 | { | |
3946 | register enum tree_code codel = TREE_CODE (type); | |
3947 | register tree rhstype; | |
3948 | register enum tree_code coder; | |
3949 | ||
3950 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
fc76e425 RS |
3951 | /* Do not use STRIP_NOPS here. We do not want an enumerator |
3952 | whose value is 0 to count as a null pointer constant. */ | |
400fbf9f JW |
3953 | if (TREE_CODE (rhs) == NON_LVALUE_EXPR) |
3954 | rhs = TREE_OPERAND (rhs, 0); | |
3955 | ||
3956 | if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE | |
3957 | || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE) | |
3958 | rhs = default_conversion (rhs); | |
8c3a6477 RK |
3959 | else if (optimize && TREE_CODE (rhs) == VAR_DECL) |
3960 | rhs = decl_constant_value (rhs); | |
400fbf9f JW |
3961 | |
3962 | rhstype = TREE_TYPE (rhs); | |
3963 | coder = TREE_CODE (rhstype); | |
3964 | ||
3965 | if (coder == ERROR_MARK) | |
3966 | return error_mark_node; | |
3967 | ||
3968 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) | |
e58cd767 RS |
3969 | { |
3970 | overflow_warning (rhs); | |
8b40563c TW |
3971 | /* Check for Objective-C protocols. This will issue a warning if |
3972 | there are protocol violations. No need to use the return value. */ | |
3973 | maybe_objc_comptypes (type, rhstype, 0); | |
e58cd767 RS |
3974 | return rhs; |
3975 | } | |
400fbf9f JW |
3976 | |
3977 | if (coder == VOID_TYPE) | |
3978 | { | |
3979 | error ("void value not ignored as it ought to be"); | |
3980 | return error_mark_node; | |
3981 | } | |
3982 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
b6a10c9f RS |
3983 | if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE |
3984 | || codel == COMPLEX_TYPE) | |
61179109 RK |
3985 | && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE |
3986 | || coder == COMPLEX_TYPE)) | |
da3c6115 | 3987 | return convert_and_check (type, rhs); |
61179109 | 3988 | |
7e842ef8 PE |
3989 | /* Conversion to a transparent union from its member types. |
3990 | This applies only to function arguments. */ | |
3991 | else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype) | |
3992 | { | |
3993 | tree memb_types; | |
3994 | tree marginal_memb_type = 0; | |
3995 | ||
3996 | for (memb_types = TYPE_FIELDS (type); memb_types; | |
3997 | memb_types = TREE_CHAIN (memb_types)) | |
3998 | { | |
3999 | tree memb_type = TREE_TYPE (memb_types); | |
4000 | ||
4001 | if (comptypes (TYPE_MAIN_VARIANT (memb_type), | |
4002 | TYPE_MAIN_VARIANT (rhstype))) | |
4003 | break; | |
4004 | ||
4005 | if (TREE_CODE (memb_type) != POINTER_TYPE) | |
4006 | continue; | |
4007 | ||
4008 | if (coder == POINTER_TYPE) | |
4009 | { | |
4010 | register tree ttl = TREE_TYPE (memb_type); | |
4011 | register tree ttr = TREE_TYPE (rhstype); | |
4012 | ||
4013 | /* Any non-function converts to a [const][volatile] void * | |
4014 | and vice versa; otherwise, targets must be the same. | |
4015 | Meanwhile, the lhs target must have all the qualifiers of | |
4016 | the rhs. */ | |
4017 | if (TYPE_MAIN_VARIANT (ttl) == void_type_node | |
4018 | || TYPE_MAIN_VARIANT (ttr) == void_type_node | |
4019 | || comp_target_types (memb_type, rhstype)) | |
4020 | { | |
4021 | /* If this type won't generate any warnings, use it. */ | |
3932261a MM |
4022 | if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr) |
4023 | || ((TREE_CODE (ttr) == FUNCTION_TYPE | |
4024 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
4025 | ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
4026 | == TYPE_QUALS (ttr)) | |
b58c9a79 | 4027 | : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) |
3932261a | 4028 | == TYPE_QUALS (ttl)))) |
7e842ef8 PE |
4029 | break; |
4030 | ||
4031 | /* Keep looking for a better type, but remember this one. */ | |
4032 | if (! marginal_memb_type) | |
4033 | marginal_memb_type = memb_type; | |
4034 | } | |
4035 | } | |
4036 | ||
4037 | /* Can convert integer zero to any pointer type. */ | |
4038 | if (integer_zerop (rhs) | |
4039 | || (TREE_CODE (rhs) == NOP_EXPR | |
4040 | && integer_zerop (TREE_OPERAND (rhs, 0)))) | |
4041 | { | |
4042 | rhs = null_pointer_node; | |
4043 | break; | |
4044 | } | |
4045 | } | |
4046 | ||
4047 | if (memb_types || marginal_memb_type) | |
4048 | { | |
4049 | if (! memb_types) | |
4050 | { | |
4051 | /* We have only a marginally acceptable member type; | |
0f41302f | 4052 | it needs a warning. */ |
7e842ef8 PE |
4053 | register tree ttl = TREE_TYPE (marginal_memb_type); |
4054 | register tree ttr = TREE_TYPE (rhstype); | |
4055 | ||
4056 | /* Const and volatile mean something different for function | |
4057 | types, so the usual warnings are not appropriate. */ | |
4058 | if (TREE_CODE (ttr) == FUNCTION_TYPE | |
4059 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
4060 | { | |
4061 | /* Because const and volatile on functions are | |
4062 | restrictions that say the function will not do | |
4063 | certain things, it is okay to use a const or volatile | |
4064 | function where an ordinary one is wanted, but not | |
4065 | vice-versa. */ | |
3932261a MM |
4066 | if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)) |
4067 | warn_for_assignment ("%s makes qualified function pointer from unqualified", | |
ab87f8c8 | 4068 | errtype, funname, parmnum); |
7e842ef8 | 4069 | } |
3932261a MM |
4070 | else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)) |
4071 | warn_for_assignment ("%s discards qualifiers from pointer target type", | |
ab87f8c8 | 4072 | errtype, funname, |
3932261a | 4073 | parmnum); |
7e842ef8 PE |
4074 | } |
4075 | ||
4076 | if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl)) | |
4077 | pedwarn ("ANSI C prohibits argument conversion to union type"); | |
4078 | ||
4079 | return build1 (NOP_EXPR, type, rhs); | |
4080 | } | |
4081 | } | |
4082 | ||
400fbf9f JW |
4083 | /* Conversions among pointers */ |
4084 | else if (codel == POINTER_TYPE && coder == POINTER_TYPE) | |
4085 | { | |
4086 | register tree ttl = TREE_TYPE (type); | |
4087 | register tree ttr = TREE_TYPE (rhstype); | |
4088 | ||
4089 | /* Any non-function converts to a [const][volatile] void * | |
4090 | and vice versa; otherwise, targets must be the same. | |
4091 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
4092 | if (TYPE_MAIN_VARIANT (ttl) == void_type_node | |
4093 | || TYPE_MAIN_VARIANT (ttr) == void_type_node | |
790e9490 RS |
4094 | || comp_target_types (type, rhstype) |
4095 | || (unsigned_type (TYPE_MAIN_VARIANT (ttl)) | |
4096 | == unsigned_type (TYPE_MAIN_VARIANT (ttr)))) | |
400fbf9f JW |
4097 | { |
4098 | if (pedantic | |
4099 | && ((TYPE_MAIN_VARIANT (ttl) == void_type_node | |
4100 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
4101 | || | |
4102 | (TYPE_MAIN_VARIANT (ttr) == void_type_node | |
fd5d5b94 RS |
4103 | /* Check TREE_CODE to catch cases like (void *) (char *) 0 |
4104 | which are not ANSI null ptr constants. */ | |
4105 | && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR) | |
400fbf9f JW |
4106 | && TREE_CODE (ttl) == FUNCTION_TYPE))) |
4107 | warn_for_assignment ("ANSI forbids %s between function pointer and `void *'", | |
ab87f8c8 | 4108 | errtype, funname, parmnum); |
400fbf9f JW |
4109 | /* Const and volatile mean something different for function types, |
4110 | so the usual warnings are not appropriate. */ | |
4111 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
caf2e8e4 | 4112 | && TREE_CODE (ttl) != FUNCTION_TYPE) |
400fbf9f | 4113 | { |
3932261a MM |
4114 | if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)) |
4115 | warn_for_assignment ("%s discards qualifiers from pointer target type", | |
ab87f8c8 | 4116 | errtype, funname, parmnum); |
790e9490 RS |
4117 | /* If this is not a case of ignoring a mismatch in signedness, |
4118 | no warning. */ | |
4119 | else if (TYPE_MAIN_VARIANT (ttl) == void_type_node | |
4120 | || TYPE_MAIN_VARIANT (ttr) == void_type_node | |
4121 | || comp_target_types (type, rhstype)) | |
4122 | ; | |
4123 | /* If there is a mismatch, do warn. */ | |
4124 | else if (pedantic) | |
4125 | warn_for_assignment ("pointer targets in %s differ in signedness", | |
ab87f8c8 | 4126 | errtype, funname, parmnum); |
400fbf9f | 4127 | } |
d949d5df RK |
4128 | else if (TREE_CODE (ttl) == FUNCTION_TYPE |
4129 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
400fbf9f JW |
4130 | { |
4131 | /* Because const and volatile on functions are restrictions | |
4132 | that say the function will not do certain things, | |
4133 | it is okay to use a const or volatile function | |
4134 | where an ordinary one is wanted, but not vice-versa. */ | |
3932261a MM |
4135 | if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)) |
4136 | warn_for_assignment ("%s makes qualified function pointer from unqualified", | |
ab87f8c8 | 4137 | errtype, funname, parmnum); |
400fbf9f JW |
4138 | } |
4139 | } | |
400fbf9f JW |
4140 | else |
4141 | warn_for_assignment ("%s from incompatible pointer type", | |
ab87f8c8 | 4142 | errtype, funname, parmnum); |
400fbf9f JW |
4143 | return convert (type, rhs); |
4144 | } | |
4145 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) | |
4146 | { | |
2918ed3c | 4147 | /* An explicit constant 0 can convert to a pointer, |
f1a2b955 RS |
4148 | or one that results from arithmetic, even including |
4149 | a cast to integer type. */ | |
4150 | if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)) | |
4151 | && | |
4152 | ! (TREE_CODE (rhs) == NOP_EXPR | |
4153 | && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE | |
4154 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST | |
4155 | && integer_zerop (TREE_OPERAND (rhs, 0)))) | |
400fbf9f JW |
4156 | { |
4157 | warn_for_assignment ("%s makes pointer from integer without a cast", | |
ab87f8c8 | 4158 | errtype, funname, parmnum); |
400fbf9f JW |
4159 | return convert (type, rhs); |
4160 | } | |
4161 | return null_pointer_node; | |
4162 | } | |
4163 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) | |
4164 | { | |
4165 | warn_for_assignment ("%s makes integer from pointer without a cast", | |
ab87f8c8 | 4166 | errtype, funname, parmnum); |
400fbf9f JW |
4167 | return convert (type, rhs); |
4168 | } | |
4169 | ||
4170 | if (!errtype) | |
4171 | { | |
4172 | if (funname) | |
8b40563c TW |
4173 | { |
4174 | tree selector = maybe_building_objc_message_expr (); | |
4175 | ||
4176 | if (selector && parmnum > 2) | |
4177 | error ("incompatible type for argument %d of `%s'", | |
4178 | parmnum - 2, IDENTIFIER_POINTER (selector)); | |
4179 | else | |
4180 | error ("incompatible type for argument %d of `%s'", | |
4181 | parmnum, IDENTIFIER_POINTER (funname)); | |
4182 | } | |
400fbf9f JW |
4183 | else |
4184 | error ("incompatible type for argument %d of indirect function call", | |
4185 | parmnum); | |
4186 | } | |
4187 | else | |
ab87f8c8 | 4188 | error ("incompatible types in %s", errtype); |
400fbf9f JW |
4189 | |
4190 | return error_mark_node; | |
4191 | } | |
4192 | ||
ab87f8c8 | 4193 | /* Print a warning using MSGID. |
400fbf9f JW |
4194 | It gets OPNAME as its one parameter. |
4195 | If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'". | |
4196 | FUNCTION and ARGNUM are handled specially if we are building an | |
4197 | Objective-C selector. */ | |
4198 | ||
4199 | static void | |
ab87f8c8 | 4200 | warn_for_assignment (msgid, opname, function, argnum) |
5d5993dd KG |
4201 | const char *msgid; |
4202 | const char *opname; | |
400fbf9f JW |
4203 | tree function; |
4204 | int argnum; | |
4205 | { | |
400fbf9f JW |
4206 | if (opname == 0) |
4207 | { | |
4208 | tree selector = maybe_building_objc_message_expr (); | |
5d5993dd | 4209 | char * new_opname; |
400fbf9f JW |
4210 | |
4211 | if (selector && argnum > 2) | |
4212 | { | |
4213 | function = selector; | |
4214 | argnum -= 2; | |
4215 | } | |
4216 | if (function) | |
4217 | { | |
4218 | /* Function name is known; supply it. */ | |
5d5993dd KG |
4219 | const char *argstring = _("passing arg %d of `%s'"); |
4220 | new_opname = (char *) alloca (IDENTIFIER_LENGTH (function) | |
4221 | + strlen (argstring) + 1 + 25 | |
4222 | /*%d*/ + 1); | |
4223 | sprintf (new_opname, argstring, argnum, | |
4224 | IDENTIFIER_POINTER (function)); | |
400fbf9f JW |
4225 | } |
4226 | else | |
4227 | { | |
5d5993dd KG |
4228 | /* Function name unknown (call through ptr); just give arg number.*/ |
4229 | const char *argnofun = _("passing arg %d of pointer to function"); | |
4230 | new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1); | |
4231 | sprintf (new_opname, argnofun, argnum); | |
400fbf9f | 4232 | } |
5d5993dd | 4233 | opname = new_opname; |
400fbf9f | 4234 | } |
ab87f8c8 | 4235 | pedwarn (msgid, opname); |
400fbf9f JW |
4236 | } |
4237 | \f | |
d9fc6069 JW |
4238 | /* If VALUE is a compound expr all of whose expressions are constant, then |
4239 | return its value. Otherwise, return error_mark_node. | |
4240 | ||
4241 | This is for handling COMPOUND_EXPRs as initializer elements | |
4242 | which is allowed with a warning when -pedantic is specified. */ | |
4243 | ||
4244 | static tree | |
4245 | valid_compound_expr_initializer (value, endtype) | |
4246 | tree value; | |
4247 | tree endtype; | |
4248 | { | |
4249 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
4250 | { | |
4251 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
4252 | == error_mark_node) | |
4253 | return error_mark_node; | |
4254 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
4255 | endtype); | |
4256 | } | |
4257 | else if (! TREE_CONSTANT (value) | |
4258 | && ! initializer_constant_valid_p (value, endtype)) | |
4259 | return error_mark_node; | |
4260 | else | |
4261 | return value; | |
4262 | } | |
400fbf9f JW |
4263 | \f |
4264 | /* Perform appropriate conversions on the initial value of a variable, | |
4265 | store it in the declaration DECL, | |
4266 | and print any error messages that are appropriate. | |
4267 | If the init is invalid, store an ERROR_MARK. */ | |
4268 | ||
4269 | void | |
4270 | store_init_value (decl, init) | |
4271 | tree decl, init; | |
4272 | { | |
4273 | register tree value, type; | |
4274 | ||
4275 | /* If variable's type was invalidly declared, just ignore it. */ | |
4276 | ||
4277 | type = TREE_TYPE (decl); | |
4278 | if (TREE_CODE (type) == ERROR_MARK) | |
4279 | return; | |
4280 | ||
4281 | /* Digest the specified initializer into an expression. */ | |
4282 | ||
790e9490 RS |
4283 | value = digest_init (type, init, TREE_STATIC (decl), |
4284 | TREE_STATIC (decl) || pedantic); | |
400fbf9f JW |
4285 | |
4286 | /* Store the expression if valid; else report error. */ | |
4287 | ||
4288 | #if 0 | |
4289 | /* Note that this is the only place we can detect the error | |
4290 | in a case such as struct foo bar = (struct foo) { x, y }; | |
d45cf215 | 4291 | where there is one initial value which is a constructor expression. */ |
400fbf9f JW |
4292 | if (value == error_mark_node) |
4293 | ; | |
4294 | else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value)) | |
4295 | { | |
4296 | error ("initializer for static variable is not constant"); | |
4297 | value = error_mark_node; | |
4298 | } | |
4299 | else if (TREE_STATIC (decl) | |
f0c70ef0 | 4300 | && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0) |
400fbf9f JW |
4301 | { |
4302 | error ("initializer for static variable uses complicated arithmetic"); | |
4303 | value = error_mark_node; | |
4304 | } | |
4305 | else | |
4306 | { | |
4307 | if (pedantic && TREE_CODE (value) == CONSTRUCTOR) | |
4308 | { | |
4309 | if (! TREE_CONSTANT (value)) | |
4310 | pedwarn ("aggregate initializer is not constant"); | |
4311 | else if (! TREE_STATIC (value)) | |
4312 | pedwarn ("aggregate initializer uses complicated arithmetic"); | |
4313 | } | |
4314 | } | |
4315 | #endif | |
4316 | ||
10d5caec PE |
4317 | DECL_INITIAL (decl) = value; |
4318 | ||
26b3c423 | 4319 | /* ANSI wants warnings about out-of-range constant initializers. */ |
10d5caec | 4320 | STRIP_TYPE_NOPS (value); |
26b3c423 | 4321 | constant_expression_warning (value); |
400fbf9f JW |
4322 | } |
4323 | \f | |
075fc632 | 4324 | /* Methods for storing and printing names for error messages. */ |
d45cf215 RS |
4325 | |
4326 | /* Implement a spelling stack that allows components of a name to be pushed | |
4327 | and popped. Each element on the stack is this structure. */ | |
4328 | ||
4329 | struct spelling | |
4330 | { | |
4331 | int kind; | |
4332 | union | |
4333 | { | |
4334 | int i; | |
5d5993dd | 4335 | const char *s; |
d45cf215 RS |
4336 | } u; |
4337 | }; | |
4338 | ||
4339 | #define SPELLING_STRING 1 | |
4340 | #define SPELLING_MEMBER 2 | |
4341 | #define SPELLING_BOUNDS 3 | |
4342 | ||
4343 | static struct spelling *spelling; /* Next stack element (unused). */ | |
4344 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
4345 | static int spelling_size; /* Size of the spelling stack. */ | |
4346 | ||
4347 | /* Macros to save and restore the spelling stack around push_... functions. | |
4348 | Alternative to SAVE_SPELLING_STACK. */ | |
4349 | ||
4350 | #define SPELLING_DEPTH() (spelling - spelling_base) | |
4351 | #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth) | |
4352 | ||
4353 | /* Save and restore the spelling stack around arbitrary C code. */ | |
4354 | ||
4355 | #define SAVE_SPELLING_DEPTH(code) \ | |
4356 | { \ | |
4357 | int __depth = SPELLING_DEPTH (); \ | |
4358 | code; \ | |
4359 | RESTORE_SPELLING_DEPTH (__depth); \ | |
4360 | } | |
4361 | ||
4362 | /* Push an element on the spelling stack with type KIND and assign VALUE | |
4363 | to MEMBER. */ | |
4364 | ||
4365 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ | |
4366 | { \ | |
4367 | int depth = SPELLING_DEPTH (); \ | |
4368 | \ | |
4369 | if (depth >= spelling_size) \ | |
4370 | { \ | |
4371 | spelling_size += 10; \ | |
4372 | if (spelling_base == 0) \ | |
4373 | spelling_base \ | |
4374 | = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \ | |
4375 | else \ | |
4376 | spelling_base \ | |
4377 | = (struct spelling *) xrealloc (spelling_base, \ | |
4378 | spelling_size * sizeof (struct spelling)); \ | |
4379 | RESTORE_SPELLING_DEPTH (depth); \ | |
4380 | } \ | |
4381 | \ | |
4382 | spelling->kind = (KIND); \ | |
4383 | spelling->MEMBER = (VALUE); \ | |
4384 | spelling++; \ | |
4385 | } | |
4386 | ||
4387 | /* Push STRING on the stack. Printed literally. */ | |
4388 | ||
4389 | static void | |
4390 | push_string (string) | |
5d5993dd | 4391 | const char *string; |
d45cf215 RS |
4392 | { |
4393 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
4394 | } | |
4395 | ||
4396 | /* Push a member name on the stack. Printed as '.' STRING. */ | |
4397 | ||
4398 | static void | |
19d76e60 RK |
4399 | push_member_name (decl) |
4400 | tree decl; | |
4401 | ||
d45cf215 | 4402 | { |
5d5993dd | 4403 | const char *string |
19d76e60 | 4404 | = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>"; |
d45cf215 RS |
4405 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); |
4406 | } | |
4407 | ||
4408 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ | |
4409 | ||
4410 | static void | |
4411 | push_array_bounds (bounds) | |
4412 | int bounds; | |
4413 | { | |
4414 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
4415 | } | |
4416 | ||
4417 | /* Compute the maximum size in bytes of the printed spelling. */ | |
4418 | ||
4419 | static int | |
4420 | spelling_length () | |
4421 | { | |
4422 | register int size = 0; | |
4423 | register struct spelling *p; | |
4424 | ||
4425 | for (p = spelling_base; p < spelling; p++) | |
4426 | { | |
4427 | if (p->kind == SPELLING_BOUNDS) | |
4428 | size += 25; | |
4429 | else | |
4430 | size += strlen (p->u.s) + 1; | |
4431 | } | |
4432 | ||
4433 | return size; | |
4434 | } | |
4435 | ||
4436 | /* Print the spelling to BUFFER and return it. */ | |
4437 | ||
4438 | static char * | |
4439 | print_spelling (buffer) | |
4440 | register char *buffer; | |
4441 | { | |
4442 | register char *d = buffer; | |
d45cf215 RS |
4443 | register struct spelling *p; |
4444 | ||
4445 | for (p = spelling_base; p < spelling; p++) | |
4446 | if (p->kind == SPELLING_BOUNDS) | |
4447 | { | |
4448 | sprintf (d, "[%d]", p->u.i); | |
4449 | d += strlen (d); | |
4450 | } | |
4451 | else | |
4452 | { | |
5d5993dd | 4453 | register const char *s; |
d45cf215 RS |
4454 | if (p->kind == SPELLING_MEMBER) |
4455 | *d++ = '.'; | |
1d300e19 | 4456 | for (s = p->u.s; (*d = *s++); d++) |
d45cf215 RS |
4457 | ; |
4458 | } | |
4459 | *d++ = '\0'; | |
4460 | return buffer; | |
4461 | } | |
4462 | ||
400fbf9f | 4463 | /* Issue an error message for a bad initializer component. |
ab87f8c8 JL |
4464 | MSGID identifies the message. |
4465 | The component name is taken from the spelling stack. */ | |
400fbf9f JW |
4466 | |
4467 | void | |
ab87f8c8 | 4468 | error_init (msgid) |
5d5993dd | 4469 | const char *msgid; |
400fbf9f | 4470 | { |
ab87f8c8 | 4471 | char *ofwhat; |
400fbf9f | 4472 | |
913d0833 | 4473 | error ("%s", msgid); |
ab87f8c8 | 4474 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
400fbf9f | 4475 | if (*ofwhat) |
ab87f8c8 | 4476 | error ("(near initialization for `%s')", ofwhat); |
400fbf9f JW |
4477 | } |
4478 | ||
4479 | /* Issue a pedantic warning for a bad initializer component. | |
ab87f8c8 JL |
4480 | MSGID identifies the message. |
4481 | The component name is taken from the spelling stack. */ | |
400fbf9f JW |
4482 | |
4483 | void | |
ab87f8c8 | 4484 | pedwarn_init (msgid) |
5d5993dd | 4485 | const char *msgid; |
400fbf9f | 4486 | { |
ab87f8c8 | 4487 | char *ofwhat; |
400fbf9f | 4488 | |
913d0833 | 4489 | pedwarn ("%s", msgid); |
ab87f8c8 | 4490 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
400fbf9f | 4491 | if (*ofwhat) |
ab87f8c8 | 4492 | pedwarn ("(near initialization for `%s')", ofwhat); |
400fbf9f | 4493 | } |
b71c7f8a RK |
4494 | |
4495 | /* Issue a warning for a bad initializer component. | |
ab87f8c8 JL |
4496 | MSGID identifies the message. |
4497 | The component name is taken from the spelling stack. */ | |
b71c7f8a RK |
4498 | |
4499 | static void | |
ab87f8c8 | 4500 | warning_init (msgid) |
5d5993dd | 4501 | const char *msgid; |
b71c7f8a | 4502 | { |
ab87f8c8 | 4503 | char *ofwhat; |
b71c7f8a | 4504 | |
913d0833 | 4505 | warning ("%s", msgid); |
ab87f8c8 | 4506 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
b71c7f8a | 4507 | if (*ofwhat) |
ab87f8c8 | 4508 | warning ("(near initialization for `%s')", ofwhat); |
b71c7f8a | 4509 | } |
400fbf9f JW |
4510 | \f |
4511 | /* Digest the parser output INIT as an initializer for type TYPE. | |
4512 | Return a C expression of type TYPE to represent the initial value. | |
4513 | ||
400fbf9f JW |
4514 | The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors |
4515 | if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT | |
59b22f64 | 4516 | applies only to elements of constructors. */ |
400fbf9f | 4517 | |
b62acd60 | 4518 | static tree |
790e9490 RS |
4519 | digest_init (type, init, require_constant, constructor_constant) |
4520 | tree type, init; | |
400fbf9f | 4521 | int require_constant, constructor_constant; |
400fbf9f JW |
4522 | { |
4523 | enum tree_code code = TREE_CODE (type); | |
047de90b | 4524 | tree inside_init = init; |
400fbf9f | 4525 | |
21a427cc AS |
4526 | if (type == error_mark_node || init == error_mark_node) |
4527 | return error_mark_node; | |
400fbf9f JW |
4528 | |
4529 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
fc76e425 RS |
4530 | /* Do not use STRIP_NOPS here. We do not want an enumerator |
4531 | whose value is 0 to count as a null pointer constant. */ | |
400fbf9f | 4532 | if (TREE_CODE (init) == NON_LVALUE_EXPR) |
047de90b | 4533 | inside_init = TREE_OPERAND (init, 0); |
400fbf9f | 4534 | |
400fbf9f JW |
4535 | /* Initialization of an array of chars from a string constant |
4536 | optionally enclosed in braces. */ | |
4537 | ||
4538 | if (code == ARRAY_TYPE) | |
4539 | { | |
4540 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
4541 | if ((typ1 == char_type_node | |
4542 | || typ1 == signed_char_type_node | |
4543 | || typ1 == unsigned_char_type_node | |
4544 | || typ1 == unsigned_wchar_type_node | |
4545 | || typ1 == signed_wchar_type_node) | |
fd5d5b94 | 4546 | && ((inside_init && TREE_CODE (inside_init) == STRING_CST))) |
400fbf9f | 4547 | { |
4d65300e RS |
4548 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
4549 | TYPE_MAIN_VARIANT (type))) | |
fd5d5b94 | 4550 | return inside_init; |
d11fdb45 | 4551 | |
fd5d5b94 | 4552 | if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))) |
400fbf9f JW |
4553 | != char_type_node) |
4554 | && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node)) | |
4555 | { | |
ab87f8c8 | 4556 | error_init ("char-array initialized from wide string"); |
400fbf9f JW |
4557 | return error_mark_node; |
4558 | } | |
fd5d5b94 | 4559 | if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))) |
400fbf9f JW |
4560 | == char_type_node) |
4561 | && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)) | |
4562 | { | |
ab87f8c8 | 4563 | error_init ("int-array initialized from non-wide string"); |
400fbf9f JW |
4564 | return error_mark_node; |
4565 | } | |
4566 | ||
fd5d5b94 | 4567 | TREE_TYPE (inside_init) = type; |
400fbf9f JW |
4568 | if (TYPE_DOMAIN (type) != 0 |
4569 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) | |
4570 | { | |
4571 | register int size = TREE_INT_CST_LOW (TYPE_SIZE (type)); | |
4572 | size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; | |
fe9ef5d7 RS |
4573 | /* Subtract 1 (or sizeof (wchar_t)) |
4574 | because it's ok to ignore the terminating null char | |
400fbf9f | 4575 | that is counted in the length of the constant. */ |
fd5d5b94 | 4576 | if (size < TREE_STRING_LENGTH (inside_init) |
fe9ef5d7 RS |
4577 | - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node) |
4578 | ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT | |
4579 | : 1)) | |
ab87f8c8 | 4580 | pedwarn_init ("initializer-string for array of chars is too long"); |
400fbf9f | 4581 | } |
fd5d5b94 | 4582 | return inside_init; |
400fbf9f JW |
4583 | } |
4584 | } | |
4585 | ||
de520661 RS |
4586 | /* Any type can be initialized |
4587 | from an expression of the same type, optionally with braces. */ | |
400fbf9f | 4588 | |
2726966d | 4589 | if (inside_init && TREE_TYPE (inside_init) != 0 |
5522c047 PB |
4590 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
4591 | TYPE_MAIN_VARIANT (type)) | |
2726966d | 4592 | || (code == ARRAY_TYPE |
3c3fa147 RS |
4593 | && comptypes (TREE_TYPE (inside_init), type)) |
4594 | || (code == POINTER_TYPE | |
3c3fa147 RS |
4595 | && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
4596 | || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE) | |
4597 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), | |
4598 | TREE_TYPE (type))))) | |
400fbf9f JW |
4599 | { |
4600 | if (code == POINTER_TYPE | |
047de90b RS |
4601 | && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
4602 | || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)) | |
4603 | inside_init = default_conversion (inside_init); | |
de520661 RS |
4604 | else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST |
4605 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
400fbf9f | 4606 | { |
ab87f8c8 | 4607 | error_init ("array initialized from non-constant array expression"); |
400fbf9f JW |
4608 | return error_mark_node; |
4609 | } | |
4610 | ||
8c3a6477 | 4611 | if (optimize && TREE_CODE (inside_init) == VAR_DECL) |
047de90b | 4612 | inside_init = decl_constant_value (inside_init); |
400fbf9f | 4613 | |
d9fc6069 JW |
4614 | /* Compound expressions can only occur here if -pedantic or |
4615 | -pedantic-errors is specified. In the later case, we always want | |
4616 | an error. In the former case, we simply want a warning. */ | |
4617 | if (require_constant && pedantic | |
4618 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
4619 | { | |
4620 | inside_init | |
4621 | = valid_compound_expr_initializer (inside_init, | |
4622 | TREE_TYPE (inside_init)); | |
4623 | if (inside_init == error_mark_node) | |
ab87f8c8 | 4624 | error_init ("initializer element is not constant"); |
d9fc6069 | 4625 | else |
ab87f8c8 | 4626 | pedwarn_init ("initializer element is not constant"); |
d9fc6069 JW |
4627 | if (flag_pedantic_errors) |
4628 | inside_init = error_mark_node; | |
4629 | } | |
4630 | else if (require_constant && ! TREE_CONSTANT (inside_init)) | |
400fbf9f | 4631 | { |
ab87f8c8 | 4632 | error_init ("initializer element is not constant"); |
047de90b | 4633 | inside_init = error_mark_node; |
400fbf9f | 4634 | } |
f0c70ef0 RS |
4635 | else if (require_constant |
4636 | && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0) | |
400fbf9f | 4637 | { |
ab87f8c8 | 4638 | error_init ("initializer element is not computable at load time"); |
047de90b | 4639 | inside_init = error_mark_node; |
400fbf9f JW |
4640 | } |
4641 | ||
047de90b | 4642 | return inside_init; |
400fbf9f JW |
4643 | } |
4644 | ||
400fbf9f JW |
4645 | /* Handle scalar types, including conversions. */ |
4646 | ||
4647 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE | |
337633f9 | 4648 | || code == ENUMERAL_TYPE || code == COMPLEX_TYPE) |
400fbf9f | 4649 | { |
e3a12f0c RS |
4650 | /* Note that convert_for_assignment calls default_conversion |
4651 | for arrays and functions. We must not call it in the | |
4652 | case where inside_init is a null pointer constant. */ | |
4653 | inside_init | |
ab87f8c8 | 4654 | = convert_for_assignment (type, init, _("initialization"), |
e3a12f0c | 4655 | NULL_TREE, NULL_TREE, 0); |
400fbf9f | 4656 | |
047de90b | 4657 | if (require_constant && ! TREE_CONSTANT (inside_init)) |
400fbf9f | 4658 | { |
ab87f8c8 | 4659 | error_init ("initializer element is not constant"); |
047de90b | 4660 | inside_init = error_mark_node; |
400fbf9f | 4661 | } |
f0c70ef0 RS |
4662 | else if (require_constant |
4663 | && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0) | |
400fbf9f | 4664 | { |
ab87f8c8 | 4665 | error_init ("initializer element is not computable at load time"); |
047de90b | 4666 | inside_init = error_mark_node; |
400fbf9f JW |
4667 | } |
4668 | ||
047de90b | 4669 | return inside_init; |
400fbf9f JW |
4670 | } |
4671 | ||
4672 | /* Come here only for records and arrays. */ | |
4673 | ||
4674 | if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) | |
4675 | { | |
ab87f8c8 | 4676 | error_init ("variable-sized object may not be initialized"); |
400fbf9f JW |
4677 | return error_mark_node; |
4678 | } | |
4679 | ||
81a55c6c RS |
4680 | /* Traditionally, you can write struct foo x = 0; |
4681 | and it initializes the first element of x to 0. */ | |
4682 | if (flag_traditional) | |
4683 | { | |
6c99c37b | 4684 | tree top = 0, prev = 0, otype = type; |
81a55c6c RS |
4685 | while (TREE_CODE (type) == RECORD_TYPE |
4686 | || TREE_CODE (type) == ARRAY_TYPE | |
4687 | || TREE_CODE (type) == QUAL_UNION_TYPE | |
4688 | || TREE_CODE (type) == UNION_TYPE) | |
4689 | { | |
4690 | tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE); | |
4691 | if (prev == 0) | |
4692 | top = temp; | |
4693 | else | |
4694 | TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp); | |
4695 | prev = temp; | |
4696 | if (TREE_CODE (type) == ARRAY_TYPE) | |
4697 | type = TREE_TYPE (type); | |
4698 | else if (TYPE_FIELDS (type)) | |
4699 | type = TREE_TYPE (TYPE_FIELDS (type)); | |
4700 | else | |
4701 | { | |
ab87f8c8 | 4702 | error_init ("invalid initializer"); |
81a55c6c RS |
4703 | return error_mark_node; |
4704 | } | |
4705 | } | |
6c99c37b RK |
4706 | |
4707 | if (otype != type) | |
4708 | { | |
4709 | TREE_OPERAND (prev, 1) | |
4710 | = build_tree_list (NULL_TREE, | |
4711 | digest_init (type, init, require_constant, | |
4712 | constructor_constant)); | |
4713 | return top; | |
4714 | } | |
4715 | else | |
4716 | return error_mark_node; | |
81a55c6c | 4717 | } |
ab87f8c8 | 4718 | error_init ("invalid initializer"); |
400fbf9f JW |
4719 | return error_mark_node; |
4720 | } | |
4721 | \f | |
de520661 | 4722 | /* Handle initializers that use braces. */ |
400fbf9f | 4723 | |
de520661 RS |
4724 | /* Type of object we are accumulating a constructor for. |
4725 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
4726 | static tree constructor_type; | |
400fbf9f | 4727 | |
de520661 RS |
4728 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields |
4729 | left to fill. */ | |
4730 | static tree constructor_fields; | |
400fbf9f | 4731 | |
de520661 RS |
4732 | /* For an ARRAY_TYPE, this is the specified index |
4733 | at which to store the next element we get. | |
4734 | This is a special INTEGER_CST node that we modify in place. */ | |
4735 | static tree constructor_index; | |
400fbf9f | 4736 | |
de520661 | 4737 | /* For an ARRAY_TYPE, this is the end index of the range |
ddd5a7c1 | 4738 | to initialize with the next element, or NULL in the ordinary case |
de520661 RS |
4739 | where the element is used just once. */ |
4740 | static tree constructor_range_end; | |
400fbf9f | 4741 | |
de520661 RS |
4742 | /* For an ARRAY_TYPE, this is the maximum index. */ |
4743 | static tree constructor_max_index; | |
103b7b17 | 4744 | |
de520661 RS |
4745 | /* For a RECORD_TYPE, this is the first field not yet written out. */ |
4746 | static tree constructor_unfilled_fields; | |
400fbf9f | 4747 | |
de520661 RS |
4748 | /* For an ARRAY_TYPE, this is the index of the first element |
4749 | not yet written out. | |
4750 | This is a special INTEGER_CST node that we modify in place. */ | |
4751 | static tree constructor_unfilled_index; | |
4752 | ||
b62acd60 RS |
4753 | /* In a RECORD_TYPE, the byte index of the next consecutive field. |
4754 | This is so we can generate gaps between fields, when appropriate. | |
4755 | This is a special INTEGER_CST node that we modify in place. */ | |
4756 | static tree constructor_bit_index; | |
4757 | ||
de520661 RS |
4758 | /* If we are saving up the elements rather than allocating them, |
4759 | this is the list of elements so far (in reverse order, | |
4760 | most recent first). */ | |
4761 | static tree constructor_elements; | |
4762 | ||
4763 | /* 1 if so far this constructor's elements are all compile-time constants. */ | |
4764 | static int constructor_constant; | |
4765 | ||
4766 | /* 1 if so far this constructor's elements are all valid address constants. */ | |
4767 | static int constructor_simple; | |
4768 | ||
4769 | /* 1 if this constructor is erroneous so far. */ | |
4770 | static int constructor_erroneous; | |
4771 | ||
4772 | /* 1 if have called defer_addressed_constants. */ | |
4773 | static int constructor_subconstants_deferred; | |
4774 | ||
e5e809f4 JL |
4775 | /* Structure for managing pending initializer elements, organized as an |
4776 | AVL tree. */ | |
4777 | ||
4778 | struct init_node | |
4779 | { | |
4780 | struct init_node *left, *right; | |
4781 | struct init_node *parent; | |
4782 | int balance; | |
4783 | tree purpose; | |
4784 | tree value; | |
4785 | }; | |
4786 | ||
4787 | /* Tree of pending elements at this constructor level. | |
de520661 RS |
4788 | These are elements encountered out of order |
4789 | which belong at places we haven't reached yet in actually | |
4dd7201e ZW |
4790 | writing the output. |
4791 | Will never hold tree nodes across GC runs. */ | |
e5e809f4 | 4792 | static struct init_node *constructor_pending_elts; |
de520661 RS |
4793 | |
4794 | /* The SPELLING_DEPTH of this constructor. */ | |
4795 | static int constructor_depth; | |
4796 | ||
cc77d4d5 | 4797 | /* 0 if implicitly pushing constructor levels is allowed. */ |
0f41302f | 4798 | int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */ |
cc77d4d5 | 4799 | |
de520661 RS |
4800 | static int require_constant_value; |
4801 | static int require_constant_elements; | |
4802 | ||
4803 | /* 1 if it is ok to output this constructor as we read it. | |
4804 | 0 means must accumulate a CONSTRUCTOR expression. */ | |
4805 | static int constructor_incremental; | |
4806 | ||
4807 | /* DECL node for which an initializer is being read. | |
4808 | 0 means we are reading a constructor expression | |
4809 | such as (struct foo) {...}. */ | |
4810 | static tree constructor_decl; | |
4811 | ||
4812 | /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */ | |
4813 | static char *constructor_asmspec; | |
4814 | ||
4815 | /* Nonzero if this is an initializer for a top-level decl. */ | |
4816 | static int constructor_top_level; | |
4817 | ||
b62acd60 RS |
4818 | \f |
4819 | /* This stack has a level for each implicit or explicit level of | |
4820 | structuring in the initializer, including the outermost one. It | |
4821 | saves the values of most of the variables above. */ | |
de520661 RS |
4822 | |
4823 | struct constructor_stack | |
400fbf9f | 4824 | { |
de520661 RS |
4825 | struct constructor_stack *next; |
4826 | tree type; | |
4827 | tree fields; | |
4828 | tree index; | |
4829 | tree range_end; | |
4830 | tree max_index; | |
4831 | tree unfilled_index; | |
4832 | tree unfilled_fields; | |
b62acd60 | 4833 | tree bit_index; |
de520661 RS |
4834 | tree elements; |
4835 | int offset; | |
e5e809f4 | 4836 | struct init_node *pending_elts; |
de520661 | 4837 | int depth; |
790e9490 RS |
4838 | /* If nonzero, this value should replace the entire |
4839 | constructor at this level. */ | |
4840 | tree replacement_value; | |
de520661 RS |
4841 | char constant; |
4842 | char simple; | |
4843 | char implicit; | |
4844 | char incremental; | |
4845 | char erroneous; | |
4846 | char outer; | |
4847 | }; | |
d45cf215 | 4848 | |
de520661 | 4849 | struct constructor_stack *constructor_stack; |
400fbf9f | 4850 | |
de520661 RS |
4851 | /* This stack records separate initializers that are nested. |
4852 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
4853 | in cases like { ... (struct foo) { ... } ... }. */ | |
400fbf9f | 4854 | |
de520661 RS |
4855 | struct initializer_stack |
4856 | { | |
4857 | struct initializer_stack *next; | |
4858 | tree decl; | |
4859 | char *asmspec; | |
4860 | struct constructor_stack *constructor_stack; | |
dea273df | 4861 | tree elements; |
de520661 RS |
4862 | struct spelling *spelling; |
4863 | struct spelling *spelling_base; | |
4864 | int spelling_size; | |
4865 | char top_level; | |
4866 | char incremental; | |
4867 | char require_constant_value; | |
4868 | char require_constant_elements; | |
4869 | char deferred; | |
4870 | }; | |
4871 | ||
4872 | struct initializer_stack *initializer_stack; | |
4873 | \f | |
4874 | /* Prepare to parse and output the initializer for variable DECL. */ | |
4875 | ||
4876 | void | |
e28cae4f | 4877 | start_init (decl, asmspec_tree, top_level) |
de520661 | 4878 | tree decl; |
e28cae4f | 4879 | tree asmspec_tree; |
de520661 RS |
4880 | int top_level; |
4881 | { | |
5d5993dd | 4882 | const char *locus; |
de520661 RS |
4883 | struct initializer_stack *p |
4884 | = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack)); | |
e28cae4f RS |
4885 | char *asmspec = 0; |
4886 | ||
4887 | if (asmspec_tree) | |
4888 | asmspec = TREE_STRING_POINTER (asmspec_tree); | |
de520661 RS |
4889 | |
4890 | p->decl = constructor_decl; | |
4891 | p->asmspec = constructor_asmspec; | |
4892 | p->incremental = constructor_incremental; | |
4893 | p->require_constant_value = require_constant_value; | |
4894 | p->require_constant_elements = require_constant_elements; | |
4895 | p->constructor_stack = constructor_stack; | |
dea273df | 4896 | p->elements = constructor_elements; |
de520661 RS |
4897 | p->spelling = spelling; |
4898 | p->spelling_base = spelling_base; | |
4899 | p->spelling_size = spelling_size; | |
4900 | p->deferred = constructor_subconstants_deferred; | |
4901 | p->top_level = constructor_top_level; | |
b62acd60 | 4902 | p->next = initializer_stack; |
de520661 RS |
4903 | initializer_stack = p; |
4904 | ||
4905 | constructor_decl = decl; | |
4906 | constructor_incremental = top_level; | |
4907 | constructor_asmspec = asmspec; | |
4908 | constructor_subconstants_deferred = 0; | |
4909 | constructor_top_level = top_level; | |
4910 | ||
4911 | if (decl != 0) | |
3c3fa147 | 4912 | { |
de520661 | 4913 | require_constant_value = TREE_STATIC (decl); |
f1a2b955 RS |
4914 | require_constant_elements |
4915 | = ((TREE_STATIC (decl) || pedantic) | |
4916 | /* For a scalar, you can always use any value to initialize, | |
4917 | even within braces. */ | |
4918 | && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
4919 | || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
4920 | || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE | |
4921 | || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); | |
de520661 RS |
4922 | locus = IDENTIFIER_POINTER (DECL_NAME (decl)); |
4923 | constructor_incremental |= TREE_STATIC (decl); | |
3c3fa147 | 4924 | } |
400fbf9f | 4925 | else |
de520661 RS |
4926 | { |
4927 | require_constant_value = 0; | |
4928 | require_constant_elements = 0; | |
4929 | locus = "(anonymous)"; | |
4930 | } | |
400fbf9f | 4931 | |
de520661 | 4932 | constructor_stack = 0; |
400fbf9f | 4933 | |
b71c7f8a RK |
4934 | missing_braces_mentioned = 0; |
4935 | ||
de520661 RS |
4936 | spelling_base = 0; |
4937 | spelling_size = 0; | |
4938 | RESTORE_SPELLING_DEPTH (0); | |
d45cf215 | 4939 | |
de520661 RS |
4940 | if (locus) |
4941 | push_string (locus); | |
4942 | } | |
400fbf9f | 4943 | |
de520661 RS |
4944 | void |
4945 | finish_init () | |
4946 | { | |
4947 | struct initializer_stack *p = initializer_stack; | |
400fbf9f | 4948 | |
de520661 RS |
4949 | /* Output subconstants (string constants, usually) |
4950 | that were referenced within this initializer and saved up. | |
4951 | Must do this if and only if we called defer_addressed_constants. */ | |
4952 | if (constructor_subconstants_deferred) | |
4953 | output_deferred_addressed_constants (); | |
4f77a31b | 4954 | |
de520661 RS |
4955 | /* Free the whole constructor stack of this initializer. */ |
4956 | while (constructor_stack) | |
4957 | { | |
4958 | struct constructor_stack *q = constructor_stack; | |
4959 | constructor_stack = q->next; | |
4960 | free (q); | |
4961 | } | |
400fbf9f | 4962 | |
de520661 RS |
4963 | /* Pop back to the data of the outer initializer (if any). */ |
4964 | constructor_decl = p->decl; | |
4965 | constructor_asmspec = p->asmspec; | |
4966 | constructor_incremental = p->incremental; | |
4967 | require_constant_value = p->require_constant_value; | |
4968 | require_constant_elements = p->require_constant_elements; | |
4969 | constructor_stack = p->constructor_stack; | |
dea273df | 4970 | constructor_elements = p->elements; |
de520661 RS |
4971 | spelling = p->spelling; |
4972 | spelling_base = p->spelling_base; | |
4973 | spelling_size = p->spelling_size; | |
4974 | constructor_subconstants_deferred = p->deferred; | |
4975 | constructor_top_level = p->top_level; | |
4976 | initializer_stack = p->next; | |
4977 | free (p); | |
4978 | } | |
4979 | \f | |
4980 | /* Call here when we see the initializer is surrounded by braces. | |
4981 | This is instead of a call to push_init_level; | |
4982 | it is matched by a call to pop_init_level. | |
400fbf9f | 4983 | |
de520661 RS |
4984 | TYPE is the type to initialize, for a constructor expression. |
4985 | For an initializer for a decl, TYPE is zero. */ | |
5a7ec9d9 | 4986 | |
de520661 RS |
4987 | void |
4988 | really_start_incremental_init (type) | |
4989 | tree type; | |
4990 | { | |
4991 | struct constructor_stack *p | |
4992 | = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack)); | |
4993 | ||
4994 | if (type == 0) | |
4995 | type = TREE_TYPE (constructor_decl); | |
4996 | ||
4997 | /* Turn off constructor_incremental if type is a struct with bitfields. | |
4998 | Do this before the first push, so that the corrected value | |
4999 | is available in finish_init. */ | |
5000 | check_init_type_bitfields (type); | |
5001 | ||
5002 | p->type = constructor_type; | |
5003 | p->fields = constructor_fields; | |
5004 | p->index = constructor_index; | |
5005 | p->range_end = constructor_range_end; | |
5006 | p->max_index = constructor_max_index; | |
5007 | p->unfilled_index = constructor_unfilled_index; | |
5008 | p->unfilled_fields = constructor_unfilled_fields; | |
b62acd60 | 5009 | p->bit_index = constructor_bit_index; |
5cb7368c | 5010 | p->elements = constructor_elements; |
de520661 RS |
5011 | p->constant = constructor_constant; |
5012 | p->simple = constructor_simple; | |
5013 | p->erroneous = constructor_erroneous; | |
5014 | p->pending_elts = constructor_pending_elts; | |
5015 | p->depth = constructor_depth; | |
790e9490 | 5016 | p->replacement_value = 0; |
de520661 RS |
5017 | p->implicit = 0; |
5018 | p->incremental = constructor_incremental; | |
5019 | p->outer = 0; | |
5020 | p->next = 0; | |
5021 | constructor_stack = p; | |
5022 | ||
5023 | constructor_constant = 1; | |
5024 | constructor_simple = 1; | |
5025 | constructor_depth = SPELLING_DEPTH (); | |
5026 | constructor_elements = 0; | |
5027 | constructor_pending_elts = 0; | |
5028 | constructor_type = type; | |
5029 | ||
5030 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
5031 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5032 | { | |
5033 | constructor_fields = TYPE_FIELDS (constructor_type); | |
abc95ed3 | 5034 | /* Skip any nameless bit fields at the beginning. */ |
ef86d2a6 | 5035 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) |
fc623854 RS |
5036 | && DECL_NAME (constructor_fields) == 0) |
5037 | constructor_fields = TREE_CHAIN (constructor_fields); | |
de520661 | 5038 | constructor_unfilled_fields = constructor_fields; |
fed3cef0 | 5039 | constructor_bit_index = copy_node (size_zero_node); |
de520661 RS |
5040 | } |
5041 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5042 | { | |
de520661 | 5043 | constructor_range_end = 0; |
de520661 | 5044 | if (TYPE_DOMAIN (constructor_type)) |
2bede729 PB |
5045 | { |
5046 | constructor_max_index | |
5047 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
5048 | constructor_index | |
fed3cef0 RK |
5049 | = copy_node (convert (bitsizetype, |
5050 | TYPE_MIN_VALUE | |
5051 | (TYPE_DOMAIN (constructor_type)))); | |
2bede729 PB |
5052 | } |
5053 | else | |
fed3cef0 RK |
5054 | constructor_index = copy_node (bitsize_int (0)); |
5055 | ||
2bede729 | 5056 | constructor_unfilled_index = copy_node (constructor_index); |
de520661 RS |
5057 | } |
5058 | else | |
5059 | { | |
5060 | /* Handle the case of int x = {5}; */ | |
5061 | constructor_fields = constructor_type; | |
5062 | constructor_unfilled_fields = constructor_type; | |
5063 | } | |
400fbf9f | 5064 | |
de520661 RS |
5065 | if (constructor_incremental) |
5066 | { | |
de520661 RS |
5067 | make_decl_rtl (constructor_decl, constructor_asmspec, |
5068 | constructor_top_level); | |
5069 | assemble_variable (constructor_decl, constructor_top_level, 0, 1); | |
400fbf9f | 5070 | |
de520661 RS |
5071 | defer_addressed_constants (); |
5072 | constructor_subconstants_deferred = 1; | |
5073 | } | |
5074 | } | |
5075 | \f | |
5076 | /* Push down into a subobject, for initialization. | |
5077 | If this is for an explicit set of braces, IMPLICIT is 0. | |
5078 | If it is because the next element belongs at a lower level, | |
5079 | IMPLICIT is 1. */ | |
400fbf9f | 5080 | |
de520661 RS |
5081 | void |
5082 | push_init_level (implicit) | |
5083 | int implicit; | |
5084 | { | |
94ba5069 RS |
5085 | struct constructor_stack *p; |
5086 | ||
5087 | /* If we've exhausted any levels that didn't have braces, | |
5088 | pop them now. */ | |
5089 | while (constructor_stack->implicit) | |
5090 | { | |
5091 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
5092 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5093 | && constructor_fields == 0) | |
5094 | process_init_element (pop_init_level (1)); | |
5095 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5096 | && tree_int_cst_lt (constructor_max_index, constructor_index)) | |
5097 | process_init_element (pop_init_level (1)); | |
5098 | else | |
5099 | break; | |
5100 | } | |
5101 | ||
bdc49177 JW |
5102 | /* Structure elements may require alignment. Do this now if necessary |
5103 | for the subaggregate, and if it comes next in sequence. Don't do | |
5104 | this for subaggregates that will go on the pending list. */ | |
7eec3328 | 5105 | if (constructor_incremental && constructor_type != 0 |
bdc49177 JW |
5106 | && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields |
5107 | && constructor_fields == constructor_unfilled_fields) | |
e700c8ec RS |
5108 | { |
5109 | /* Advance to offset of this element. */ | |
5110 | if (! tree_int_cst_equal (constructor_bit_index, | |
5111 | DECL_FIELD_BITPOS (constructor_fields))) | |
5112 | { | |
f8dac6eb R |
5113 | /* By using unsigned arithmetic, the result will be correct even |
5114 | in case of overflows, if BITS_PER_UNIT is a power of two. */ | |
5115 | unsigned next = (TREE_INT_CST_LOW | |
5116 | (DECL_FIELD_BITPOS (constructor_fields)) | |
5117 | / (unsigned)BITS_PER_UNIT); | |
5118 | unsigned here = (TREE_INT_CST_LOW (constructor_bit_index) | |
5119 | / (unsigned)BITS_PER_UNIT); | |
5120 | ||
5121 | assemble_zeros ((next - here) | |
5122 | * (unsigned)BITS_PER_UNIT | |
5123 | / (unsigned)BITS_PER_UNIT); | |
e700c8ec | 5124 | } |
24c032e9 JW |
5125 | /* Indicate that we have now filled the structure up to the current |
5126 | field. */ | |
5127 | constructor_unfilled_fields = constructor_fields; | |
e700c8ec RS |
5128 | } |
5129 | ||
94ba5069 | 5130 | p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack)); |
de520661 RS |
5131 | p->type = constructor_type; |
5132 | p->fields = constructor_fields; | |
5133 | p->index = constructor_index; | |
5134 | p->range_end = constructor_range_end; | |
5135 | p->max_index = constructor_max_index; | |
5136 | p->unfilled_index = constructor_unfilled_index; | |
5137 | p->unfilled_fields = constructor_unfilled_fields; | |
b62acd60 | 5138 | p->bit_index = constructor_bit_index; |
de520661 RS |
5139 | p->elements = constructor_elements; |
5140 | p->constant = constructor_constant; | |
5141 | p->simple = constructor_simple; | |
5142 | p->erroneous = constructor_erroneous; | |
5143 | p->pending_elts = constructor_pending_elts; | |
5144 | p->depth = constructor_depth; | |
790e9490 | 5145 | p->replacement_value = 0; |
de520661 RS |
5146 | p->implicit = implicit; |
5147 | p->incremental = constructor_incremental; | |
5148 | p->outer = 0; | |
5149 | p->next = constructor_stack; | |
5150 | constructor_stack = p; | |
5151 | ||
5152 | constructor_constant = 1; | |
5153 | constructor_simple = 1; | |
5154 | constructor_depth = SPELLING_DEPTH (); | |
5155 | constructor_elements = 0; | |
5156 | constructor_pending_elts = 0; | |
5157 | ||
94ba5069 RS |
5158 | /* Don't die if an entire brace-pair level is superfluous |
5159 | in the containing level. */ | |
5160 | if (constructor_type == 0) | |
5161 | ; | |
5162 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
5163 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
de520661 | 5164 | { |
91fa3c30 RS |
5165 | /* Don't die if there are extra init elts at the end. */ |
5166 | if (constructor_fields == 0) | |
5167 | constructor_type = 0; | |
5168 | else | |
5169 | { | |
5170 | constructor_type = TREE_TYPE (constructor_fields); | |
19d76e60 | 5171 | push_member_name (constructor_fields); |
e4376e63 | 5172 | constructor_depth++; |
81f415f0 RK |
5173 | if (constructor_fields != constructor_unfilled_fields) |
5174 | constructor_incremental = 0; | |
91fa3c30 | 5175 | } |
de520661 RS |
5176 | } |
5177 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5178 | { | |
5179 | constructor_type = TREE_TYPE (constructor_type); | |
5180 | push_array_bounds (TREE_INT_CST_LOW (constructor_index)); | |
e4376e63 | 5181 | constructor_depth++; |
20e5a991 RK |
5182 | if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index) |
5183 | || constructor_range_end != 0) | |
81f415f0 | 5184 | constructor_incremental = 0; |
de520661 | 5185 | } |
400fbf9f | 5186 | |
91fa3c30 RS |
5187 | if (constructor_type == 0) |
5188 | { | |
ab87f8c8 | 5189 | error_init ("extra brace group at end of initializer"); |
91fa3c30 RS |
5190 | constructor_fields = 0; |
5191 | constructor_unfilled_fields = 0; | |
b71c7f8a | 5192 | return; |
91fa3c30 | 5193 | } |
b71c7f8a RK |
5194 | |
5195 | /* Turn off constructor_incremental if type is a struct with bitfields. */ | |
5196 | check_init_type_bitfields (constructor_type); | |
5197 | ||
5198 | if (implicit && warn_missing_braces && !missing_braces_mentioned) | |
5199 | { | |
5200 | missing_braces_mentioned = 1; | |
ab87f8c8 | 5201 | warning_init ("missing braces around initializer"); |
b71c7f8a RK |
5202 | } |
5203 | ||
5204 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
91fa3c30 | 5205 | || TREE_CODE (constructor_type) == UNION_TYPE) |
de520661 RS |
5206 | { |
5207 | constructor_fields = TYPE_FIELDS (constructor_type); | |
abc95ed3 | 5208 | /* Skip any nameless bit fields at the beginning. */ |
ef86d2a6 | 5209 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) |
fc623854 RS |
5210 | && DECL_NAME (constructor_fields) == 0) |
5211 | constructor_fields = TREE_CHAIN (constructor_fields); | |
de520661 | 5212 | constructor_unfilled_fields = constructor_fields; |
fed3cef0 | 5213 | constructor_bit_index = copy_node (size_zero_node); |
de520661 RS |
5214 | } |
5215 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5216 | { | |
de520661 | 5217 | constructor_range_end = 0; |
de520661 | 5218 | if (TYPE_DOMAIN (constructor_type)) |
2bede729 PB |
5219 | { |
5220 | constructor_max_index | |
5221 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
5222 | constructor_index | |
fed3cef0 RK |
5223 | = copy_node (convert (bitsizetype, |
5224 | TYPE_MIN_VALUE | |
5225 | (TYPE_DOMAIN (constructor_type)))); | |
2bede729 PB |
5226 | } |
5227 | else | |
fed3cef0 RK |
5228 | constructor_index = bitsize_int (0); |
5229 | ||
2bede729 | 5230 | constructor_unfilled_index = copy_node (constructor_index); |
de520661 RS |
5231 | } |
5232 | else | |
5233 | { | |
ab87f8c8 | 5234 | warning_init ("braces around scalar initializer"); |
de520661 RS |
5235 | constructor_fields = constructor_type; |
5236 | constructor_unfilled_fields = constructor_type; | |
5237 | } | |
5238 | } | |
400fbf9f | 5239 | |
de520661 RS |
5240 | /* Don't read a struct incrementally if it has any bitfields, |
5241 | because the incremental reading code doesn't know how to | |
5242 | handle bitfields yet. */ | |
d45cf215 | 5243 | |
de520661 RS |
5244 | static void |
5245 | check_init_type_bitfields (type) | |
5246 | tree type; | |
5247 | { | |
5248 | if (TREE_CODE (type) == RECORD_TYPE) | |
5249 | { | |
5250 | tree tail; | |
5251 | for (tail = TYPE_FIELDS (type); tail; | |
5252 | tail = TREE_CHAIN (tail)) | |
3c9d8baf | 5253 | { |
43f7bed5 | 5254 | if (DECL_C_BIT_FIELD (tail)) |
3c9d8baf RK |
5255 | { |
5256 | constructor_incremental = 0; | |
5257 | break; | |
5258 | } | |
5259 | ||
5260 | check_init_type_bitfields (TREE_TYPE (tail)); | |
5261 | } | |
400fbf9f | 5262 | } |
3c9d8baf | 5263 | |
43f7bed5 VM |
5264 | else if (TREE_CODE (type) == UNION_TYPE) |
5265 | { | |
5266 | tree tail = TYPE_FIELDS (type); | |
5267 | if (tail && DECL_C_BIT_FIELD (tail)) | |
5268 | /* We also use the nonincremental algorithm for initiliazation | |
5269 | of unions whose first member is a bitfield, becuase the | |
5270 | incremental algorithm has no code for dealing with | |
5271 | bitfields. */ | |
5272 | constructor_incremental = 0; | |
5273 | } | |
5274 | ||
3c9d8baf RK |
5275 | else if (TREE_CODE (type) == ARRAY_TYPE) |
5276 | check_init_type_bitfields (TREE_TYPE (type)); | |
de520661 RS |
5277 | } |
5278 | ||
5279 | /* At the end of an implicit or explicit brace level, | |
5280 | finish up that level of constructor. | |
5281 | If we were outputting the elements as they are read, return 0 | |
5282 | from inner levels (process_init_element ignores that), | |
5283 | but return error_mark_node from the outermost level | |
5284 | (that's what we want to put in DECL_INITIAL). | |
5285 | Otherwise, return a CONSTRUCTOR expression. */ | |
5286 | ||
5287 | tree | |
5288 | pop_init_level (implicit) | |
5289 | int implicit; | |
5290 | { | |
5291 | struct constructor_stack *p; | |
9d5f3e49 | 5292 | int size = 0; |
de520661 RS |
5293 | tree constructor = 0; |
5294 | ||
5295 | if (implicit == 0) | |
400fbf9f | 5296 | { |
de520661 RS |
5297 | /* When we come to an explicit close brace, |
5298 | pop any inner levels that didn't have explicit braces. */ | |
5299 | while (constructor_stack->implicit) | |
5300 | process_init_element (pop_init_level (1)); | |
5301 | } | |
400fbf9f | 5302 | |
de520661 | 5303 | p = constructor_stack; |
91fa3c30 RS |
5304 | |
5305 | if (constructor_type != 0) | |
5306 | size = int_size_in_bytes (constructor_type); | |
400fbf9f | 5307 | |
9dfcc8db BH |
5308 | /* Warn when some struct elements are implicitly initialized to zero. */ |
5309 | if (extra_warnings | |
5310 | && constructor_type | |
5311 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
5312 | && constructor_unfilled_fields) | |
5313 | { | |
5314 | push_member_name (constructor_unfilled_fields); | |
ab87f8c8 | 5315 | warning_init ("missing initializer"); |
9dfcc8db BH |
5316 | RESTORE_SPELLING_DEPTH (constructor_depth); |
5317 | } | |
5318 | ||
de520661 RS |
5319 | /* Now output all pending elements. */ |
5320 | output_pending_init_elements (1); | |
5321 | ||
b62acd60 RS |
5322 | #if 0 /* c-parse.in warns about {}. */ |
5323 | /* In ANSI, each brace level must have at least one element. */ | |
5324 | if (! implicit && pedantic | |
5325 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5326 | ? integer_zerop (constructor_unfilled_index) | |
5327 | : constructor_unfilled_fields == TYPE_FIELDS (constructor_type))) | |
ab87f8c8 | 5328 | pedwarn_init ("empty braces in initializer"); |
b62acd60 RS |
5329 | #endif |
5330 | ||
de520661 RS |
5331 | /* Pad out the end of the structure. */ |
5332 | ||
790e9490 RS |
5333 | if (p->replacement_value) |
5334 | { | |
5335 | /* If this closes a superfluous brace pair, | |
5336 | just pass out the element between them. */ | |
5337 | constructor = p->replacement_value; | |
5338 | /* If this is the top level thing within the initializer, | |
d11fdb45 | 5339 | and it's for a variable, then since we already called |
790e9490 RS |
5340 | assemble_variable, we must output the value now. */ |
5341 | if (p->next == 0 && constructor_decl != 0 | |
5342 | && constructor_incremental) | |
5343 | { | |
5344 | constructor = digest_init (constructor_type, constructor, | |
48dd3a7c RK |
5345 | require_constant_value, |
5346 | require_constant_elements); | |
790e9490 RS |
5347 | |
5348 | /* If initializing an array of unknown size, | |
5349 | determine the size now. */ | |
5350 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5351 | && TYPE_DOMAIN (constructor_type) == 0) | |
5352 | { | |
790e9490 RS |
5353 | /* We shouldn't have an incomplete array type within |
5354 | some other type. */ | |
5355 | if (constructor_stack->next) | |
5356 | abort (); | |
5357 | ||
4dd7201e | 5358 | if (complete_array_type (constructor_type, constructor, 0)) |
790e9490 RS |
5359 | abort (); |
5360 | ||
5361 | size = int_size_in_bytes (constructor_type); | |
790e9490 RS |
5362 | } |
5363 | ||
5364 | output_constant (constructor, size); | |
5365 | } | |
5366 | } | |
91fa3c30 RS |
5367 | else if (constructor_type == 0) |
5368 | ; | |
19d76e60 RK |
5369 | else if (TREE_CODE (constructor_type) != RECORD_TYPE |
5370 | && TREE_CODE (constructor_type) != UNION_TYPE | |
5371 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
5372 | && ! constructor_incremental) | |
5373 | { | |
5374 | /* A nonincremental scalar initializer--just return | |
5375 | the element, after verifying there is just one. */ | |
5376 | if (constructor_elements == 0) | |
5377 | { | |
ab87f8c8 | 5378 | error_init ("empty scalar initializer"); |
19d76e60 RK |
5379 | constructor = error_mark_node; |
5380 | } | |
5381 | else if (TREE_CHAIN (constructor_elements) != 0) | |
5382 | { | |
ab87f8c8 | 5383 | error_init ("extra elements in scalar initializer"); |
19d76e60 RK |
5384 | constructor = TREE_VALUE (constructor_elements); |
5385 | } | |
5386 | else | |
5387 | constructor = TREE_VALUE (constructor_elements); | |
5388 | } | |
790e9490 | 5389 | else if (! constructor_incremental) |
de520661 RS |
5390 | { |
5391 | if (constructor_erroneous) | |
5392 | constructor = error_mark_node; | |
5393 | else | |
400fbf9f | 5394 | { |
de520661 RS |
5395 | constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE, |
5396 | nreverse (constructor_elements)); | |
5397 | if (constructor_constant) | |
5398 | TREE_CONSTANT (constructor) = 1; | |
5399 | if (constructor_constant && constructor_simple) | |
5400 | TREE_STATIC (constructor) = 1; | |
de520661 RS |
5401 | } |
5402 | } | |
5403 | else | |
5404 | { | |
5405 | tree filled; | |
fed3cef0 | 5406 | |
de520661 RS |
5407 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
5408 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
fed3cef0 RK |
5409 | /* Find the offset of the end of that field. */ |
5410 | filled = size_binop (CEIL_DIV_EXPR, | |
5411 | constructor_bit_index, | |
5412 | size_int (BITS_PER_UNIT)); | |
5413 | ||
de520661 RS |
5414 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
5415 | { | |
5416 | /* If initializing an array of unknown size, | |
5417 | determine the size now. */ | |
5418 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5419 | && TYPE_DOMAIN (constructor_type) == 0) | |
400fbf9f | 5420 | { |
de520661 | 5421 | tree maxindex |
fed3cef0 RK |
5422 | = copy_node (size_diffop (constructor_unfilled_index, |
5423 | bitsize_int (1))); | |
de520661 | 5424 | |
de520661 RS |
5425 | TYPE_DOMAIN (constructor_type) = build_index_type (maxindex); |
5426 | TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type); | |
5427 | ||
45ce961e JW |
5428 | /* TYPE_MAX_VALUE is always one less than the number of elements |
5429 | in the array, because we start counting at zero. Therefore, | |
5430 | warn only if the value is less than zero. */ | |
de520661 | 5431 | if (pedantic |
ff3225e7 | 5432 | && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) |
45ce961e | 5433 | < 0)) |
ff3225e7 RK |
5434 | error_with_decl (constructor_decl, |
5435 | "zero or negative array size `%s'"); | |
de520661 RS |
5436 | layout_type (constructor_type); |
5437 | size = int_size_in_bytes (constructor_type); | |
400fbf9f JW |
5438 | } |
5439 | ||
fed3cef0 RK |
5440 | filled |
5441 | = size_binop (MULT_EXPR, constructor_unfilled_index, | |
5442 | convert (bitsizetype, | |
5443 | TYPE_SIZE_UNIT | |
5444 | (TREE_TYPE (constructor_type)))); | |
de520661 RS |
5445 | } |
5446 | else | |
5447 | filled = 0; | |
400fbf9f | 5448 | |
de520661 RS |
5449 | if (filled != 0) |
5450 | assemble_zeros (size - TREE_INT_CST_LOW (filled)); | |
de520661 RS |
5451 | } |
5452 | ||
5453 | ||
5454 | constructor_type = p->type; | |
5455 | constructor_fields = p->fields; | |
5456 | constructor_index = p->index; | |
5457 | constructor_range_end = p->range_end; | |
5458 | constructor_max_index = p->max_index; | |
5459 | constructor_unfilled_index = p->unfilled_index; | |
5460 | constructor_unfilled_fields = p->unfilled_fields; | |
b62acd60 | 5461 | constructor_bit_index = p->bit_index; |
de520661 RS |
5462 | constructor_elements = p->elements; |
5463 | constructor_constant = p->constant; | |
5464 | constructor_simple = p->simple; | |
5465 | constructor_erroneous = p->erroneous; | |
5466 | constructor_pending_elts = p->pending_elts; | |
5467 | constructor_depth = p->depth; | |
5468 | constructor_incremental = p->incremental; | |
5469 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
5470 | ||
5471 | constructor_stack = p->next; | |
5472 | free (p); | |
5473 | ||
5474 | if (constructor == 0) | |
5475 | { | |
5476 | if (constructor_stack == 0) | |
5477 | return error_mark_node; | |
5478 | return NULL_TREE; | |
5479 | } | |
5480 | return constructor; | |
5481 | } | |
5482 | ||
5483 | /* Within an array initializer, specify the next index to be initialized. | |
5484 | FIRST is that index. If LAST is nonzero, then initialize a range | |
5485 | of indices, running from FIRST through LAST. */ | |
5486 | ||
5487 | void | |
5488 | set_init_index (first, last) | |
5489 | tree first, last; | |
5490 | { | |
19d76e60 RK |
5491 | while ((TREE_CODE (first) == NOP_EXPR |
5492 | || TREE_CODE (first) == CONVERT_EXPR | |
5493 | || TREE_CODE (first) == NON_LVALUE_EXPR) | |
5494 | && (TYPE_MODE (TREE_TYPE (first)) | |
5495 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0))))) | |
5496 | (first) = TREE_OPERAND (first, 0); | |
5497 | if (last) | |
5498 | while ((TREE_CODE (last) == NOP_EXPR | |
5499 | || TREE_CODE (last) == CONVERT_EXPR | |
5500 | || TREE_CODE (last) == NON_LVALUE_EXPR) | |
5501 | && (TYPE_MODE (TREE_TYPE (last)) | |
5502 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0))))) | |
5503 | (last) = TREE_OPERAND (last, 0); | |
5504 | ||
94ba5069 | 5505 | if (TREE_CODE (first) != INTEGER_CST) |
ab87f8c8 | 5506 | error_init ("nonconstant array index in initializer"); |
94ba5069 | 5507 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) |
ab87f8c8 | 5508 | error_init ("nonconstant array index in initializer"); |
7b1d6e6e | 5509 | else if (! constructor_unfilled_index) |
ab87f8c8 | 5510 | error_init ("array index in non-array initializer"); |
94ba5069 | 5511 | else if (tree_int_cst_lt (first, constructor_unfilled_index)) |
ab87f8c8 | 5512 | error_init ("duplicate array index in initializer"); |
de520661 RS |
5513 | else |
5514 | { | |
ee2990e7 RK |
5515 | TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (first); |
5516 | TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (first); | |
de520661 RS |
5517 | |
5518 | if (last != 0 && tree_int_cst_lt (last, first)) | |
ab87f8c8 | 5519 | error_init ("empty index range in initializer"); |
de520661 | 5520 | else |
b62acd60 RS |
5521 | { |
5522 | if (pedantic) | |
5523 | pedwarn ("ANSI C forbids specifying element to initialize"); | |
5524 | constructor_range_end = last; | |
5525 | } | |
de520661 RS |
5526 | } |
5527 | } | |
5528 | ||
5529 | /* Within a struct initializer, specify the next field to be initialized. */ | |
5530 | ||
94ba5069 | 5531 | void |
de520661 RS |
5532 | set_init_label (fieldname) |
5533 | tree fieldname; | |
5534 | { | |
5535 | tree tail; | |
5536 | int passed = 0; | |
5537 | ||
e5cfb88f RK |
5538 | /* Don't die if an entire brace-pair level is superfluous |
5539 | in the containing level. */ | |
5540 | if (constructor_type == 0) | |
5541 | return; | |
5542 | ||
de520661 RS |
5543 | for (tail = TYPE_FIELDS (constructor_type); tail; |
5544 | tail = TREE_CHAIN (tail)) | |
5545 | { | |
5546 | if (tail == constructor_unfilled_fields) | |
5547 | passed = 1; | |
5548 | if (DECL_NAME (tail) == fieldname) | |
5549 | break; | |
5550 | } | |
5551 | ||
5552 | if (tail == 0) | |
5553 | error ("unknown field `%s' specified in initializer", | |
5554 | IDENTIFIER_POINTER (fieldname)); | |
5555 | else if (!passed) | |
5556 | error ("field `%s' already initialized", | |
5557 | IDENTIFIER_POINTER (fieldname)); | |
5558 | else | |
b62acd60 RS |
5559 | { |
5560 | constructor_fields = tail; | |
5561 | if (pedantic) | |
5562 | pedwarn ("ANSI C forbids specifying structure member to initialize"); | |
5563 | } | |
de520661 RS |
5564 | } |
5565 | \f | |
e5e809f4 JL |
5566 | /* Add a new initializer to the tree of pending initializers. PURPOSE |
5567 | indentifies the initializer, either array index or field in a structure. | |
5568 | VALUE is the value of that index or field. */ | |
5569 | ||
5570 | static void | |
5571 | add_pending_init (purpose, value) | |
5572 | tree purpose, value; | |
5573 | { | |
5574 | struct init_node *p, **q, *r; | |
5575 | ||
5576 | q = &constructor_pending_elts; | |
5577 | p = 0; | |
5578 | ||
5579 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5580 | { | |
5581 | while (*q != 0) | |
5582 | { | |
5583 | p = *q; | |
5584 | if (tree_int_cst_lt (purpose, p->purpose)) | |
5585 | q = &p->left; | |
83b091c7 | 5586 | else if (p->purpose != purpose) |
e5e809f4 JL |
5587 | q = &p->right; |
5588 | else | |
5589 | abort (); | |
5590 | } | |
5591 | } | |
5592 | else | |
5593 | { | |
5594 | while (*q != NULL) | |
5595 | { | |
5596 | p = *q; | |
5597 | if (tree_int_cst_lt (DECL_FIELD_BITPOS (purpose), | |
5598 | DECL_FIELD_BITPOS (p->purpose))) | |
5599 | q = &p->left; | |
83b091c7 | 5600 | else if (p->purpose != purpose) |
e5e809f4 JL |
5601 | q = &p->right; |
5602 | else | |
5603 | abort (); | |
5604 | } | |
5605 | } | |
5606 | ||
4dd7201e | 5607 | r = (struct init_node *) ggc_alloc_obj (sizeof (struct init_node), 0); |
e5e809f4 JL |
5608 | r->purpose = purpose; |
5609 | r->value = value; | |
5610 | ||
5611 | *q = r; | |
5612 | r->parent = p; | |
5613 | r->left = 0; | |
5614 | r->right = 0; | |
5615 | r->balance = 0; | |
5616 | ||
5617 | while (p) | |
5618 | { | |
5619 | struct init_node *s; | |
5620 | ||
5621 | if (r == p->left) | |
5622 | { | |
5623 | if (p->balance == 0) | |
5624 | p->balance = -1; | |
5625 | else if (p->balance < 0) | |
5626 | { | |
5627 | if (r->balance < 0) | |
5628 | { | |
5629 | /* L rotation. */ | |
5630 | p->left = r->right; | |
5631 | if (p->left) | |
5632 | p->left->parent = p; | |
5633 | r->right = p; | |
5634 | ||
5635 | p->balance = 0; | |
5636 | r->balance = 0; | |
5637 | ||
5638 | s = p->parent; | |
5639 | p->parent = r; | |
5640 | r->parent = s; | |
5641 | if (s) | |
5642 | { | |
5643 | if (s->left == p) | |
5644 | s->left = r; | |
5645 | else | |
5646 | s->right = r; | |
5647 | } | |
5648 | else | |
5649 | constructor_pending_elts = r; | |
5650 | } | |
5651 | else | |
5652 | { | |
5653 | /* LR rotation. */ | |
5654 | struct init_node *t = r->right; | |
5655 | ||
5656 | r->right = t->left; | |
5657 | if (r->right) | |
5658 | r->right->parent = r; | |
5659 | t->left = r; | |
5660 | ||
5661 | p->left = t->right; | |
5662 | if (p->left) | |
5663 | p->left->parent = p; | |
5664 | t->right = p; | |
5665 | ||
5666 | p->balance = t->balance < 0; | |
5667 | r->balance = -(t->balance > 0); | |
5668 | t->balance = 0; | |
5669 | ||
5670 | s = p->parent; | |
5671 | p->parent = t; | |
5672 | r->parent = t; | |
5673 | t->parent = s; | |
5674 | if (s) | |
5675 | { | |
5676 | if (s->left == p) | |
5677 | s->left = t; | |
5678 | else | |
5679 | s->right = t; | |
5680 | } | |
5681 | else | |
5682 | constructor_pending_elts = t; | |
5683 | } | |
5684 | break; | |
5685 | } | |
5686 | else | |
5687 | { | |
5688 | /* p->balance == +1; growth of left side balances the node. */ | |
5689 | p->balance = 0; | |
5690 | break; | |
5691 | } | |
5692 | } | |
5693 | else /* r == p->right */ | |
5694 | { | |
5695 | if (p->balance == 0) | |
5696 | /* Growth propagation from right side. */ | |
5697 | p->balance++; | |
5698 | else if (p->balance > 0) | |
5699 | { | |
5700 | if (r->balance > 0) | |
5701 | { | |
5702 | /* R rotation. */ | |
5703 | p->right = r->left; | |
5704 | if (p->right) | |
5705 | p->right->parent = p; | |
5706 | r->left = p; | |
5707 | ||
5708 | p->balance = 0; | |
5709 | r->balance = 0; | |
5710 | ||
5711 | s = p->parent; | |
5712 | p->parent = r; | |
5713 | r->parent = s; | |
5714 | if (s) | |
5715 | { | |
5716 | if (s->left == p) | |
5717 | s->left = r; | |
5718 | else | |
5719 | s->right = r; | |
5720 | } | |
5721 | else | |
5722 | constructor_pending_elts = r; | |
5723 | } | |
5724 | else /* r->balance == -1 */ | |
5725 | { | |
5726 | /* RL rotation */ | |
5727 | struct init_node *t = r->left; | |
5728 | ||
5729 | r->left = t->right; | |
5730 | if (r->left) | |
5731 | r->left->parent = r; | |
5732 | t->right = r; | |
5733 | ||
5734 | p->right = t->left; | |
5735 | if (p->right) | |
5736 | p->right->parent = p; | |
5737 | t->left = p; | |
5738 | ||
5739 | r->balance = (t->balance < 0); | |
5740 | p->balance = -(t->balance > 0); | |
5741 | t->balance = 0; | |
5742 | ||
5743 | s = p->parent; | |
5744 | p->parent = t; | |
5745 | r->parent = t; | |
5746 | t->parent = s; | |
5747 | if (s) | |
5748 | { | |
5749 | if (s->left == p) | |
5750 | s->left = t; | |
5751 | else | |
5752 | s->right = t; | |
5753 | } | |
5754 | else | |
5755 | constructor_pending_elts = t; | |
5756 | } | |
5757 | break; | |
5758 | } | |
5759 | else | |
5760 | { | |
5761 | /* p->balance == -1; growth of right side balances the node. */ | |
5762 | p->balance = 0; | |
5763 | break; | |
5764 | } | |
5765 | } | |
5766 | ||
5767 | r = p; | |
5768 | p = p->parent; | |
5769 | } | |
5770 | } | |
5771 | ||
5772 | /* Return nonzero if FIELD is equal to the index of a pending initializer. */ | |
5773 | ||
5774 | static int | |
5775 | pending_init_member (field) | |
5776 | tree field; | |
5777 | { | |
5778 | struct init_node *p; | |
5779 | ||
5780 | p = constructor_pending_elts; | |
5781 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5782 | { | |
5783 | while (p) | |
5784 | { | |
83b091c7 | 5785 | if (field == p->purpose) |
e5e809f4 JL |
5786 | return 1; |
5787 | else if (tree_int_cst_lt (field, p->purpose)) | |
5788 | p = p->left; | |
5789 | else | |
5790 | p = p->right; | |
5791 | } | |
5792 | } | |
5793 | else | |
5794 | { | |
5795 | while (p) | |
5796 | { | |
5797 | if (field == p->purpose) | |
5798 | return 1; | |
5799 | else if (tree_int_cst_lt (DECL_FIELD_BITPOS (field), | |
5800 | DECL_FIELD_BITPOS (p->purpose))) | |
5801 | p = p->left; | |
5802 | else | |
5803 | p = p->right; | |
5804 | } | |
5805 | } | |
5806 | ||
5807 | return 0; | |
5808 | } | |
5809 | ||
de520661 RS |
5810 | /* "Output" the next constructor element. |
5811 | At top level, really output it to assembler code now. | |
5812 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
5813 | TYPE is the data type that the containing data type wants here. | |
5814 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
5815 | ||
5816 | PENDING if non-nil means output pending elements that belong | |
5817 | right after this element. (PENDING is normally 1; | |
5818 | it is 0 while outputting pending elements, to avoid recursion.) */ | |
5819 | ||
34403047 | 5820 | static void |
de520661 RS |
5821 | output_init_element (value, type, field, pending) |
5822 | tree value, type, field; | |
5823 | int pending; | |
5824 | { | |
5825 | int duplicate = 0; | |
5826 | ||
d3ab9753 RS |
5827 | if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE |
5828 | || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE | |
fd5d5b94 RS |
5829 | && !(TREE_CODE (value) == STRING_CST |
5830 | && TREE_CODE (type) == ARRAY_TYPE | |
5831 | && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE) | |
1e40eab8 RS |
5832 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), |
5833 | TYPE_MAIN_VARIANT (type)))) | |
d3ab9753 RS |
5834 | value = default_conversion (value); |
5835 | ||
5836 | if (value == error_mark_node) | |
5837 | constructor_erroneous = 1; | |
5838 | else if (!TREE_CONSTANT (value)) | |
5839 | constructor_constant = 0; | |
4160009f RK |
5840 | else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0 |
5841 | || ((TREE_CODE (constructor_type) == RECORD_TYPE | |
5842 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
ef86d2a6 RK |
5843 | && DECL_C_BIT_FIELD (field) |
5844 | && TREE_CODE (value) != INTEGER_CST)) | |
d3ab9753 RS |
5845 | constructor_simple = 0; |
5846 | ||
de520661 RS |
5847 | if (require_constant_value && ! TREE_CONSTANT (value)) |
5848 | { | |
ab87f8c8 | 5849 | error_init ("initializer element is not constant"); |
de520661 RS |
5850 | value = error_mark_node; |
5851 | } | |
5852 | else if (require_constant_elements | |
5853 | && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0) | |
5854 | { | |
ab87f8c8 | 5855 | error_init ("initializer element is not computable at load time"); |
de520661 RS |
5856 | value = error_mark_node; |
5857 | } | |
5858 | ||
5859 | /* If this element duplicates one on constructor_pending_elts, | |
5860 | print a message and ignore it. Don't do this when we're | |
5861 | processing elements taken off constructor_pending_elts, | |
5862 | because we'd always get spurious errors. */ | |
5863 | if (pending) | |
5864 | { | |
5865 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
e5e809f4 JL |
5866 | || TREE_CODE (constructor_type) == UNION_TYPE |
5867 | || TREE_CODE (constructor_type) == ARRAY_TYPE) | |
de520661 | 5868 | { |
e5e809f4 | 5869 | if (pending_init_member (field)) |
400fbf9f | 5870 | { |
ab87f8c8 | 5871 | error_init ("duplicate initializer"); |
de520661 | 5872 | duplicate = 1; |
400fbf9f | 5873 | } |
400fbf9f JW |
5874 | } |
5875 | } | |
400fbf9f | 5876 | |
de520661 RS |
5877 | /* If this element doesn't come next in sequence, |
5878 | put it on constructor_pending_elts. */ | |
5879 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5880 | && !tree_int_cst_equal (field, constructor_unfilled_index)) | |
5881 | { | |
5882 | if (! duplicate) | |
8348547a RS |
5883 | /* The copy_node is needed in case field is actually |
5884 | constructor_index, which is modified in place. */ | |
e5e809f4 JL |
5885 | add_pending_init (copy_node (field), |
5886 | digest_init (type, value, require_constant_value, | |
5887 | require_constant_elements)); | |
de520661 | 5888 | } |
76aaaae2 | 5889 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
de520661 RS |
5890 | && field != constructor_unfilled_fields) |
5891 | { | |
76aaaae2 RS |
5892 | /* We do this for records but not for unions. In a union, |
5893 | no matter which field is specified, it can be initialized | |
5894 | right away since it starts at the beginning of the union. */ | |
de520661 | 5895 | if (!duplicate) |
e5e809f4 JL |
5896 | add_pending_init (field, |
5897 | digest_init (type, value, require_constant_value, | |
5898 | require_constant_elements)); | |
de520661 RS |
5899 | } |
5900 | else | |
5901 | { | |
5902 | /* Otherwise, output this element either to | |
5903 | constructor_elements or to the assembler file. */ | |
400fbf9f | 5904 | |
de520661 | 5905 | if (!duplicate) |
c2f4acb7 | 5906 | { |
de520661 | 5907 | if (! constructor_incremental) |
94ba5069 | 5908 | { |
19d76e60 | 5909 | if (field && TREE_CODE (field) == INTEGER_CST) |
94ba5069 RS |
5910 | field = copy_node (field); |
5911 | constructor_elements | |
48dd3a7c RK |
5912 | = tree_cons (field, digest_init (type, value, |
5913 | require_constant_value, | |
5914 | require_constant_elements), | |
94ba5069 RS |
5915 | constructor_elements); |
5916 | } | |
de520661 | 5917 | else |
b62acd60 RS |
5918 | { |
5919 | /* Structure elements may require alignment. | |
5920 | Do this, if necessary. */ | |
5921 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
5922 | { | |
5923 | /* Advance to offset of this element. */ | |
5924 | if (! tree_int_cst_equal (constructor_bit_index, | |
b5ff0f70 | 5925 | DECL_FIELD_BITPOS (field))) |
b62acd60 | 5926 | { |
f8dac6eb R |
5927 | /* By using unsigned arithmetic, the result will be |
5928 | correct even in case of overflows, if BITS_PER_UNIT | |
5929 | is a power of two. */ | |
5930 | unsigned next = (TREE_INT_CST_LOW | |
5931 | (DECL_FIELD_BITPOS (field)) | |
5932 | / (unsigned)BITS_PER_UNIT); | |
5933 | unsigned here = (TREE_INT_CST_LOW | |
5934 | (constructor_bit_index) | |
5935 | / (unsigned)BITS_PER_UNIT); | |
5936 | ||
5937 | assemble_zeros ((next - here) | |
5938 | * (unsigned)BITS_PER_UNIT | |
5939 | / (unsigned)BITS_PER_UNIT); | |
b62acd60 RS |
5940 | } |
5941 | } | |
48dd3a7c RK |
5942 | output_constant (digest_init (type, value, |
5943 | require_constant_value, | |
5944 | require_constant_elements), | |
d11fdb45 | 5945 | int_size_in_bytes (type)); |
b62acd60 | 5946 | |
925d5bbf RS |
5947 | /* For a record or union, |
5948 | keep track of end position of last field. */ | |
5949 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
5950 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
b62acd60 | 5951 | { |
b5ff0f70 RK |
5952 | tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field), |
5953 | DECL_SIZE (field)); | |
06ceef4e | 5954 | |
b62acd60 RS |
5955 | TREE_INT_CST_LOW (constructor_bit_index) |
5956 | = TREE_INT_CST_LOW (temp); | |
5957 | TREE_INT_CST_HIGH (constructor_bit_index) | |
5958 | = TREE_INT_CST_HIGH (temp); | |
5959 | } | |
5960 | } | |
c2f4acb7 RS |
5961 | } |
5962 | ||
de520661 RS |
5963 | /* Advance the variable that indicates sequential elements output. */ |
5964 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
400fbf9f | 5965 | { |
de520661 | 5966 | tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index, |
fed3cef0 RK |
5967 | bitsize_int (1)); |
5968 | ||
de520661 RS |
5969 | TREE_INT_CST_LOW (constructor_unfilled_index) |
5970 | = TREE_INT_CST_LOW (tem); | |
5971 | TREE_INT_CST_HIGH (constructor_unfilled_index) | |
5972 | = TREE_INT_CST_HIGH (tem); | |
5973 | } | |
5974 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
9bbecbc4 R |
5975 | { |
5976 | constructor_unfilled_fields = | |
5977 | TREE_CHAIN (constructor_unfilled_fields); | |
5978 | /* Skip any nameless bit fields. */ | |
5979 | while (constructor_unfilled_fields != 0 | |
5980 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
5981 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
5982 | constructor_unfilled_fields = | |
5983 | TREE_CHAIN (constructor_unfilled_fields); | |
5984 | } | |
de520661 RS |
5985 | else if (TREE_CODE (constructor_type) == UNION_TYPE) |
5986 | constructor_unfilled_fields = 0; | |
5987 | ||
5988 | /* Now output any pending elements which have become next. */ | |
5989 | if (pending) | |
5990 | output_pending_init_elements (0); | |
5991 | } | |
5992 | } | |
400fbf9f | 5993 | |
de520661 RS |
5994 | /* Output any pending elements which have become next. |
5995 | As we output elements, constructor_unfilled_{fields,index} | |
5996 | advances, which may cause other elements to become next; | |
5997 | if so, they too are output. | |
5998 | ||
5999 | If ALL is 0, we return when there are | |
6000 | no more pending elements to output now. | |
6001 | ||
6002 | If ALL is 1, we output space as necessary so that | |
6003 | we can output all the pending elements. */ | |
6004 | ||
6005 | static void | |
6006 | output_pending_init_elements (all) | |
6007 | int all; | |
6008 | { | |
e5e809f4 | 6009 | struct init_node *elt = constructor_pending_elts; |
de520661 RS |
6010 | tree next; |
6011 | ||
6012 | retry: | |
6013 | ||
e5e809f4 | 6014 | /* Look thru the whole pending tree. |
de520661 RS |
6015 | If we find an element that should be output now, |
6016 | output it. Otherwise, set NEXT to the element | |
6017 | that comes first among those still pending. */ | |
6018 | ||
6019 | next = 0; | |
e5e809f4 | 6020 | while (elt) |
de520661 RS |
6021 | { |
6022 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6023 | { | |
e5e809f4 | 6024 | if (tree_int_cst_equal (elt->purpose, |
de520661 | 6025 | constructor_unfilled_index)) |
e5e809f4 JL |
6026 | output_init_element (elt->value, |
6027 | TREE_TYPE (constructor_type), | |
6028 | constructor_unfilled_index, 0); | |
6029 | else if (tree_int_cst_lt (constructor_unfilled_index, | |
6030 | elt->purpose)) | |
400fbf9f | 6031 | { |
e5e809f4 JL |
6032 | /* Advance to the next smaller node. */ |
6033 | if (elt->left) | |
6034 | elt = elt->left; | |
6035 | else | |
6036 | { | |
6037 | /* We have reached the smallest node bigger than the | |
6038 | current unfilled index. Fill the space first. */ | |
6039 | next = elt->purpose; | |
6040 | break; | |
6041 | } | |
6042 | } | |
6043 | else | |
6044 | { | |
6045 | /* Advance to the next bigger node. */ | |
6046 | if (elt->right) | |
6047 | elt = elt->right; | |
6048 | else | |
6049 | { | |
6050 | /* We have reached the biggest node in a subtree. Find | |
6051 | the parent of it, which is the next bigger node. */ | |
6052 | while (elt->parent && elt->parent->right == elt) | |
6053 | elt = elt->parent; | |
6054 | elt = elt->parent; | |
6055 | if (elt && tree_int_cst_lt (constructor_unfilled_index, | |
6056 | elt->purpose)) | |
6057 | { | |
6058 | next = elt->purpose; | |
6059 | break; | |
6060 | } | |
6061 | } | |
de520661 | 6062 | } |
de520661 RS |
6063 | } |
6064 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
6065 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6066 | { | |
e5e809f4 JL |
6067 | /* If the current record is complete we are done. */ |
6068 | if (constructor_unfilled_fields == 0) | |
6069 | break; | |
6070 | if (elt->purpose == constructor_unfilled_fields) | |
de520661 | 6071 | { |
e5e809f4 | 6072 | output_init_element (elt->value, |
de520661 RS |
6073 | TREE_TYPE (constructor_unfilled_fields), |
6074 | constructor_unfilled_fields, | |
6075 | 0); | |
400fbf9f | 6076 | } |
e5e809f4 JL |
6077 | else if (tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields), |
6078 | DECL_FIELD_BITPOS (elt->purpose))) | |
6079 | { | |
6080 | /* Advance to the next smaller node. */ | |
6081 | if (elt->left) | |
6082 | elt = elt->left; | |
6083 | else | |
6084 | { | |
6085 | /* We have reached the smallest node bigger than the | |
6086 | current unfilled field. Fill the space first. */ | |
6087 | next = elt->purpose; | |
6088 | break; | |
6089 | } | |
6090 | } | |
6091 | else | |
6092 | { | |
6093 | /* Advance to the next bigger node. */ | |
6094 | if (elt->right) | |
6095 | elt = elt->right; | |
6096 | else | |
6097 | { | |
6098 | /* We have reached the biggest node in a subtree. Find | |
6099 | the parent of it, which is the next bigger node. */ | |
6100 | while (elt->parent && elt->parent->right == elt) | |
6101 | elt = elt->parent; | |
6102 | elt = elt->parent; | |
6103 | if (elt | |
6104 | && tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields), | |
6105 | DECL_FIELD_BITPOS (elt->purpose))) | |
6106 | { | |
6107 | next = elt->purpose; | |
6108 | break; | |
6109 | } | |
6110 | } | |
6111 | } | |
400fbf9f | 6112 | } |
de520661 RS |
6113 | } |
6114 | ||
6115 | /* Ordinarily return, but not if we want to output all | |
6116 | and there are elements left. */ | |
6117 | if (! (all && next != 0)) | |
6118 | return; | |
6119 | ||
6120 | /* Generate space up to the position of NEXT. */ | |
6121 | if (constructor_incremental) | |
6122 | { | |
6123 | tree filled; | |
fed3cef0 | 6124 | tree nextpos_tree = bitsize_int (0); |
400fbf9f | 6125 | |
de520661 RS |
6126 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
6127 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
400fbf9f | 6128 | { |
e5e809f4 | 6129 | tree tail; |
fed3cef0 | 6130 | |
b5ff0f70 | 6131 | /* Find the last field written out, if any. */ |
de520661 RS |
6132 | for (tail = TYPE_FIELDS (constructor_type); tail; |
6133 | tail = TREE_CHAIN (tail)) | |
6134 | if (TREE_CHAIN (tail) == constructor_unfilled_fields) | |
6135 | break; | |
b5ff0f70 RK |
6136 | |
6137 | if (tail) | |
6138 | /* Find the offset of the end of that field. */ | |
6139 | filled = size_binop (CEIL_DIV_EXPR, | |
6140 | size_binop (PLUS_EXPR, | |
6141 | DECL_FIELD_BITPOS (tail), | |
6142 | DECL_SIZE (tail)), | |
fed3cef0 | 6143 | bitsize_int (BITS_PER_UNIT)); |
b5ff0f70 | 6144 | else |
fed3cef0 | 6145 | filled = bitsize_int (0); |
b5ff0f70 | 6146 | |
de520661 RS |
6147 | nextpos_tree = size_binop (CEIL_DIV_EXPR, |
6148 | DECL_FIELD_BITPOS (next), | |
fed3cef0 | 6149 | bitsize_int (BITS_PER_UNIT)); |
b5ff0f70 RK |
6150 | |
6151 | TREE_INT_CST_HIGH (constructor_bit_index) | |
6152 | = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next)); | |
6153 | TREE_INT_CST_LOW (constructor_bit_index) | |
6154 | = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next)); | |
de520661 | 6155 | constructor_unfilled_fields = next; |
400fbf9f | 6156 | } |
de520661 | 6157 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
400fbf9f | 6158 | { |
fed3cef0 RK |
6159 | filled |
6160 | = size_binop (MULT_EXPR, constructor_unfilled_index, | |
6161 | convert (bitsizetype, | |
6162 | TYPE_SIZE_UNIT | |
6163 | (TREE_TYPE (constructor_type)))); | |
de520661 RS |
6164 | nextpos_tree |
6165 | = size_binop (MULT_EXPR, next, | |
fed3cef0 RK |
6166 | convert (bitsizetype, TYPE_SIZE_UNIT |
6167 | (TREE_TYPE (constructor_type)))); | |
de520661 RS |
6168 | TREE_INT_CST_LOW (constructor_unfilled_index) |
6169 | = TREE_INT_CST_LOW (next); | |
6170 | TREE_INT_CST_HIGH (constructor_unfilled_index) | |
6171 | = TREE_INT_CST_HIGH (next); | |
400fbf9f | 6172 | } |
de520661 RS |
6173 | else |
6174 | filled = 0; | |
400fbf9f | 6175 | |
de520661 | 6176 | if (filled) |
fe67cf58 | 6177 | { |
de520661 RS |
6178 | int nextpos = TREE_INT_CST_LOW (nextpos_tree); |
6179 | ||
6180 | assemble_zeros (nextpos - TREE_INT_CST_LOW (filled)); | |
fe67cf58 | 6181 | } |
de520661 | 6182 | } |
94ba5069 RS |
6183 | else |
6184 | { | |
6185 | /* If it's not incremental, just skip over the gap, | |
6186 | so that after jumping to retry we will output the next | |
6187 | successive element. */ | |
6188 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
6189 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6190 | constructor_unfilled_fields = next; | |
6191 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6192 | { | |
6193 | TREE_INT_CST_LOW (constructor_unfilled_index) | |
6194 | = TREE_INT_CST_LOW (next); | |
6195 | TREE_INT_CST_HIGH (constructor_unfilled_index) | |
6196 | = TREE_INT_CST_HIGH (next); | |
6197 | } | |
6198 | } | |
de520661 | 6199 | |
e5e809f4 JL |
6200 | /* ELT now points to the node in the pending tree with the next |
6201 | initializer to output. */ | |
de520661 RS |
6202 | goto retry; |
6203 | } | |
6204 | \f | |
6205 | /* Add one non-braced element to the current constructor level. | |
6206 | This adjusts the current position within the constructor's type. | |
6207 | This may also start or terminate implicit levels | |
6208 | to handle a partly-braced initializer. | |
6209 | ||
6210 | Once this has found the correct level for the new element, | |
6211 | it calls output_init_element. | |
6212 | ||
6213 | Note: if we are incrementally outputting this constructor, | |
6214 | this function may be called with a null argument | |
6215 | representing a sub-constructor that was already incrementally output. | |
6216 | When that happens, we output nothing, but we do the bookkeeping | |
6217 | to skip past that element of the current constructor. */ | |
6218 | ||
6219 | void | |
6220 | process_init_element (value) | |
6221 | tree value; | |
6222 | { | |
b62acd60 RS |
6223 | tree orig_value = value; |
6224 | int string_flag = value != 0 && TREE_CODE (value) == STRING_CST; | |
6225 | ||
790e9490 RS |
6226 | /* Handle superfluous braces around string cst as in |
6227 | char x[] = {"foo"}; */ | |
6228 | if (string_flag | |
d27c148b | 6229 | && constructor_type |
790e9490 | 6230 | && TREE_CODE (constructor_type) == ARRAY_TYPE |
61e215dd | 6231 | && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE |
790e9490 RS |
6232 | && integer_zerop (constructor_unfilled_index)) |
6233 | { | |
d739a3bc NS |
6234 | if (constructor_stack->replacement_value) |
6235 | error_init ("excess elements in char array initializer"); | |
790e9490 RS |
6236 | constructor_stack->replacement_value = value; |
6237 | return; | |
6238 | } | |
6239 | ||
790e9490 RS |
6240 | if (constructor_stack->replacement_value != 0) |
6241 | { | |
ab87f8c8 | 6242 | error_init ("excess elements in struct initializer"); |
790e9490 RS |
6243 | return; |
6244 | } | |
6245 | ||
91fa3c30 RS |
6246 | /* Ignore elements of a brace group if it is entirely superfluous |
6247 | and has already been diagnosed. */ | |
6248 | if (constructor_type == 0) | |
6249 | return; | |
6250 | ||
de520661 RS |
6251 | /* If we've exhausted any levels that didn't have braces, |
6252 | pop them now. */ | |
6253 | while (constructor_stack->implicit) | |
6254 | { | |
6255 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
6256 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6257 | && constructor_fields == 0) | |
6258 | process_init_element (pop_init_level (1)); | |
6259 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
ec0bc8b6 RK |
6260 | && (constructor_max_index == 0 |
6261 | || tree_int_cst_lt (constructor_max_index, | |
6262 | constructor_index))) | |
de520661 | 6263 | process_init_element (pop_init_level (1)); |
fe67cf58 | 6264 | else |
de520661 | 6265 | break; |
400fbf9f JW |
6266 | } |
6267 | ||
de520661 | 6268 | while (1) |
400fbf9f | 6269 | { |
de520661 | 6270 | if (TREE_CODE (constructor_type) == RECORD_TYPE) |
400fbf9f | 6271 | { |
de520661 RS |
6272 | tree fieldtype; |
6273 | enum tree_code fieldcode; | |
6274 | ||
6275 | if (constructor_fields == 0) | |
6276 | { | |
ab87f8c8 | 6277 | pedwarn_init ("excess elements in struct initializer"); |
de520661 RS |
6278 | break; |
6279 | } | |
6280 | ||
1d33b2a9 JW |
6281 | fieldtype = TREE_TYPE (constructor_fields); |
6282 | if (fieldtype != error_mark_node) | |
6283 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
de520661 RS |
6284 | fieldcode = TREE_CODE (fieldtype); |
6285 | ||
b62acd60 RS |
6286 | /* Accept a string constant to initialize a subarray. */ |
6287 | if (value != 0 | |
6288 | && fieldcode == ARRAY_TYPE | |
6289 | && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE | |
6290 | && string_flag) | |
6291 | value = orig_value; | |
6292 | /* Otherwise, if we have come to a subaggregate, | |
6293 | and we don't have an element of its type, push into it. */ | |
cc77d4d5 | 6294 | else if (value != 0 && !constructor_no_implicit |
ee7204ee | 6295 | && value != error_mark_node |
b62acd60 RS |
6296 | && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype |
6297 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE | |
6298 | || fieldcode == UNION_TYPE)) | |
de520661 RS |
6299 | { |
6300 | push_init_level (1); | |
6301 | continue; | |
6302 | } | |
6303 | ||
6304 | if (value) | |
6305 | { | |
19d76e60 | 6306 | push_member_name (constructor_fields); |
de520661 RS |
6307 | output_init_element (value, fieldtype, constructor_fields, 1); |
6308 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
6309 | } | |
6310 | else | |
b62acd60 RS |
6311 | /* Do the bookkeeping for an element that was |
6312 | directly output as a constructor. */ | |
6313 | { | |
6314 | /* For a record, keep track of end position of last field. */ | |
6315 | tree temp = size_binop (PLUS_EXPR, | |
6316 | DECL_FIELD_BITPOS (constructor_fields), | |
6317 | DECL_SIZE (constructor_fields)); | |
fed3cef0 | 6318 | |
b62acd60 RS |
6319 | TREE_INT_CST_LOW (constructor_bit_index) |
6320 | = TREE_INT_CST_LOW (temp); | |
6321 | TREE_INT_CST_HIGH (constructor_bit_index) | |
6322 | = TREE_INT_CST_HIGH (temp); | |
6323 | ||
6324 | constructor_unfilled_fields = TREE_CHAIN (constructor_fields); | |
9bbecbc4 R |
6325 | /* Skip any nameless bit fields. */ |
6326 | while (constructor_unfilled_fields != 0 | |
6327 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
6328 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
6329 | constructor_unfilled_fields = | |
6330 | TREE_CHAIN (constructor_unfilled_fields); | |
b62acd60 | 6331 | } |
de520661 RS |
6332 | |
6333 | constructor_fields = TREE_CHAIN (constructor_fields); | |
abc95ed3 | 6334 | /* Skip any nameless bit fields at the beginning. */ |
ef86d2a6 RK |
6335 | while (constructor_fields != 0 |
6336 | && DECL_C_BIT_FIELD (constructor_fields) | |
fc623854 RS |
6337 | && DECL_NAME (constructor_fields) == 0) |
6338 | constructor_fields = TREE_CHAIN (constructor_fields); | |
de520661 | 6339 | break; |
400fbf9f | 6340 | } |
de520661 | 6341 | if (TREE_CODE (constructor_type) == UNION_TYPE) |
400fbf9f | 6342 | { |
de520661 RS |
6343 | tree fieldtype; |
6344 | enum tree_code fieldcode; | |
6345 | ||
6346 | if (constructor_fields == 0) | |
6347 | { | |
ab87f8c8 | 6348 | pedwarn_init ("excess elements in union initializer"); |
de520661 RS |
6349 | break; |
6350 | } | |
6351 | ||
1d33b2a9 JW |
6352 | fieldtype = TREE_TYPE (constructor_fields); |
6353 | if (fieldtype != error_mark_node) | |
6354 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
de520661 RS |
6355 | fieldcode = TREE_CODE (fieldtype); |
6356 | ||
b62acd60 RS |
6357 | /* Accept a string constant to initialize a subarray. */ |
6358 | if (value != 0 | |
6359 | && fieldcode == ARRAY_TYPE | |
6360 | && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE | |
6361 | && string_flag) | |
6362 | value = orig_value; | |
6363 | /* Otherwise, if we have come to a subaggregate, | |
6364 | and we don't have an element of its type, push into it. */ | |
cc77d4d5 | 6365 | else if (value != 0 && !constructor_no_implicit |
ee7204ee | 6366 | && value != error_mark_node |
b62acd60 RS |
6367 | && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype |
6368 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE | |
6369 | || fieldcode == UNION_TYPE)) | |
de520661 RS |
6370 | { |
6371 | push_init_level (1); | |
6372 | continue; | |
6373 | } | |
6374 | ||
6375 | if (value) | |
6376 | { | |
19d76e60 | 6377 | push_member_name (constructor_fields); |
de520661 RS |
6378 | output_init_element (value, fieldtype, constructor_fields, 1); |
6379 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
6380 | } | |
6381 | else | |
94ba5069 RS |
6382 | /* Do the bookkeeping for an element that was |
6383 | directly output as a constructor. */ | |
6384 | { | |
6385 | TREE_INT_CST_LOW (constructor_bit_index) | |
6386 | = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields)); | |
6387 | TREE_INT_CST_HIGH (constructor_bit_index) | |
6388 | = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields)); | |
6389 | ||
6390 | constructor_unfilled_fields = TREE_CHAIN (constructor_fields); | |
6391 | } | |
de520661 RS |
6392 | |
6393 | constructor_fields = 0; | |
6394 | break; | |
400fbf9f | 6395 | } |
de520661 RS |
6396 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
6397 | { | |
6398 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
6399 | enum tree_code eltcode = TREE_CODE (elttype); | |
6400 | ||
b62acd60 RS |
6401 | /* Accept a string constant to initialize a subarray. */ |
6402 | if (value != 0 | |
6403 | && eltcode == ARRAY_TYPE | |
6404 | && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE | |
6405 | && string_flag) | |
6406 | value = orig_value; | |
6407 | /* Otherwise, if we have come to a subaggregate, | |
6408 | and we don't have an element of its type, push into it. */ | |
cc77d4d5 | 6409 | else if (value != 0 && !constructor_no_implicit |
ee7204ee | 6410 | && value != error_mark_node |
b62acd60 RS |
6411 | && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype |
6412 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE | |
6413 | || eltcode == UNION_TYPE)) | |
de520661 RS |
6414 | { |
6415 | push_init_level (1); | |
6416 | continue; | |
6417 | } | |
6418 | ||
6419 | if (constructor_max_index != 0 | |
6420 | && tree_int_cst_lt (constructor_max_index, constructor_index)) | |
6421 | { | |
ab87f8c8 | 6422 | pedwarn_init ("excess elements in array initializer"); |
de520661 RS |
6423 | break; |
6424 | } | |
400fbf9f | 6425 | |
0f41302f | 6426 | /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */ |
333a5dae | 6427 | if (constructor_range_end) |
ee2990e7 RK |
6428 | { |
6429 | if (constructor_max_index != 0 | |
6430 | && tree_int_cst_lt (constructor_max_index, | |
6431 | constructor_range_end)) | |
6432 | { | |
ab87f8c8 | 6433 | pedwarn_init ("excess elements in array initializer"); |
ee2990e7 RK |
6434 | TREE_INT_CST_HIGH (constructor_range_end) |
6435 | = TREE_INT_CST_HIGH (constructor_max_index); | |
6436 | TREE_INT_CST_LOW (constructor_range_end) | |
6437 | = TREE_INT_CST_LOW (constructor_max_index); | |
6438 | } | |
6439 | ||
6440 | value = save_expr (value); | |
6441 | } | |
333a5dae | 6442 | |
de520661 RS |
6443 | /* Now output the actual element. |
6444 | Ordinarily, output once. | |
6445 | If there is a range, repeat it till we advance past the range. */ | |
6446 | do | |
6447 | { | |
6448 | tree tem; | |
d45cf215 | 6449 | |
de520661 RS |
6450 | if (value) |
6451 | { | |
6452 | push_array_bounds (TREE_INT_CST_LOW (constructor_index)); | |
6453 | output_init_element (value, elttype, constructor_index, 1); | |
6454 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
6455 | } | |
d45cf215 | 6456 | |
fed3cef0 | 6457 | tem = size_binop (PLUS_EXPR, constructor_index, bitsize_int (1)); |
ee2990e7 RK |
6458 | TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (tem); |
6459 | TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (tem); | |
de520661 RS |
6460 | |
6461 | if (!value) | |
6462 | /* If we are doing the bookkeeping for an element that was | |
6463 | directly output as a constructor, | |
6464 | we must update constructor_unfilled_index. */ | |
6465 | { | |
6466 | TREE_INT_CST_LOW (constructor_unfilled_index) | |
6467 | = TREE_INT_CST_LOW (constructor_index); | |
6468 | TREE_INT_CST_HIGH (constructor_unfilled_index) | |
6469 | = TREE_INT_CST_HIGH (constructor_index); | |
6470 | } | |
6471 | } | |
6472 | while (! (constructor_range_end == 0 | |
6473 | || tree_int_cst_lt (constructor_range_end, | |
6474 | constructor_index))); | |
400fbf9f | 6475 | |
de520661 RS |
6476 | break; |
6477 | } | |
6478 | ||
6479 | /* Handle the sole element allowed in a braced initializer | |
6480 | for a scalar variable. */ | |
6481 | if (constructor_fields == 0) | |
6482 | { | |
ab87f8c8 | 6483 | pedwarn_init ("excess elements in scalar initializer"); |
de520661 RS |
6484 | break; |
6485 | } | |
6486 | ||
6487 | if (value) | |
6488 | output_init_element (value, constructor_type, NULL_TREE, 1); | |
6489 | constructor_fields = 0; | |
6490 | break; | |
fe67cf58 | 6491 | } |
400fbf9f JW |
6492 | } |
6493 | \f | |
6494 | /* Expand an ASM statement with operands, handling output operands | |
6495 | that are not variables or INDIRECT_REFS by transforming such | |
6496 | cases into cases that expand_asm_operands can handle. | |
6497 | ||
6498 | Arguments are same as for expand_asm_operands. */ | |
6499 | ||
6500 | void | |
6501 | c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line) | |
6502 | tree string, outputs, inputs, clobbers; | |
6503 | int vol; | |
6504 | char *filename; | |
6505 | int line; | |
6506 | { | |
6507 | int noutputs = list_length (outputs); | |
6508 | register int i; | |
6509 | /* o[I] is the place that output number I should be written. */ | |
6510 | register tree *o = (tree *) alloca (noutputs * sizeof (tree)); | |
6511 | register tree tail; | |
6512 | ||
6513 | if (TREE_CODE (string) == ADDR_EXPR) | |
6514 | string = TREE_OPERAND (string, 0); | |
6515 | if (TREE_CODE (string) != STRING_CST) | |
6516 | { | |
6517 | error ("asm template is not a string constant"); | |
6518 | return; | |
6519 | } | |
6520 | ||
7b6327ae | 6521 | /* Record the contents of OUTPUTS before it is modified. */ |
400fbf9f | 6522 | for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++) |
c5c76735 JL |
6523 | { |
6524 | tree output = TREE_VALUE (tail); | |
6525 | ||
6526 | /* We can remove conversions that just change the type, not the mode. */ | |
6527 | STRIP_NOPS (output); | |
6528 | o[i] = output; | |
6529 | ||
6530 | /* Allow conversions as LHS here. build_modify_expr as called below | |
6531 | will do the right thing with them. */ | |
6532 | while (TREE_CODE (output) == NOP_EXPR | |
6533 | || TREE_CODE (output) == CONVERT_EXPR | |
6534 | || TREE_CODE (output) == FLOAT_EXPR | |
6535 | || TREE_CODE (output) == FIX_TRUNC_EXPR | |
6536 | || TREE_CODE (output) == FIX_FLOOR_EXPR | |
6537 | || TREE_CODE (output) == FIX_ROUND_EXPR | |
6538 | || TREE_CODE (output) == FIX_CEIL_EXPR) | |
1bef1e7c | 6539 | output = TREE_OPERAND (output, 0); |
c5c76735 JL |
6540 | |
6541 | lvalue_or_else (o[i], "invalid lvalue in asm statement"); | |
6542 | } | |
400fbf9f JW |
6543 | |
6544 | /* Perform default conversions on array and function inputs. */ | |
6545 | /* Don't do this for other types-- | |
6546 | it would screw up operands expected to be in memory. */ | |
6547 | for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++) | |
6548 | if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE | |
6549 | || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE) | |
6550 | TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail)); | |
6551 | ||
6552 | /* Generate the ASM_OPERANDS insn; | |
6553 | store into the TREE_VALUEs of OUTPUTS some trees for | |
6554 | where the values were actually stored. */ | |
6555 | expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line); | |
6556 | ||
6557 | /* Copy all the intermediate outputs into the specified outputs. */ | |
6558 | for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++) | |
6559 | { | |
6560 | if (o[i] != TREE_VALUE (tail)) | |
6561 | { | |
6562 | expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)), | |
f5a8bfff | 6563 | NULL_RTX, VOIDmode, EXPAND_NORMAL); |
400fbf9f JW |
6564 | free_temp_slots (); |
6565 | } | |
6566 | /* Detect modification of read-only values. | |
6567 | (Otherwise done by build_modify_expr.) */ | |
6568 | else | |
6569 | { | |
6570 | tree type = TREE_TYPE (o[i]); | |
a43ea319 RK |
6571 | if (TREE_READONLY (o[i]) |
6572 | || TYPE_READONLY (type) | |
400fbf9f JW |
6573 | || ((TREE_CODE (type) == RECORD_TYPE |
6574 | || TREE_CODE (type) == UNION_TYPE) | |
6575 | && C_TYPE_FIELDS_READONLY (type))) | |
6576 | readonly_warning (o[i], "modification by `asm'"); | |
6577 | } | |
6578 | } | |
6579 | ||
6580 | /* Those MODIFY_EXPRs could do autoincrements. */ | |
6581 | emit_queue (); | |
6582 | } | |
6583 | \f | |
6584 | /* Expand a C `return' statement. | |
6585 | RETVAL is the expression for what to return, | |
6586 | or a null pointer for `return;' with no value. */ | |
6587 | ||
6588 | void | |
6589 | c_expand_return (retval) | |
6590 | tree retval; | |
6591 | { | |
6592 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)); | |
6593 | ||
6594 | if (TREE_THIS_VOLATILE (current_function_decl)) | |
08bf538e | 6595 | warning ("function declared `noreturn' has a `return' statement"); |
400fbf9f JW |
6596 | |
6597 | if (!retval) | |
6598 | { | |
6599 | current_function_returns_null = 1; | |
6600 | if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
6601 | warning ("`return' with no value, in function returning non-void"); | |
6602 | expand_null_return (); | |
6603 | } | |
6604 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) | |
6605 | { | |
6606 | current_function_returns_null = 1; | |
6607 | if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) | |
6608 | pedwarn ("`return' with a value, in function returning void"); | |
6609 | expand_return (retval); | |
6610 | } | |
6611 | else | |
6612 | { | |
ab87f8c8 | 6613 | tree t = convert_for_assignment (valtype, retval, _("return"), |
9b7267b8 | 6614 | NULL_TREE, NULL_TREE, 0); |
400fbf9f | 6615 | tree res = DECL_RESULT (current_function_decl); |
88a3dbc1 | 6616 | tree inner; |
70768eda RK |
6617 | |
6618 | if (t == error_mark_node) | |
6619 | return; | |
6620 | ||
88a3dbc1 RK |
6621 | inner = t = convert (TREE_TYPE (res), t); |
6622 | ||
6623 | /* Strip any conversions, additions, and subtractions, and see if | |
6624 | we are returning the address of a local variable. Warn if so. */ | |
abe80e6d | 6625 | while (1) |
88a3dbc1 | 6626 | { |
abe80e6d RK |
6627 | switch (TREE_CODE (inner)) |
6628 | { | |
6629 | case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR: | |
6630 | case PLUS_EXPR: | |
6631 | inner = TREE_OPERAND (inner, 0); | |
6632 | continue; | |
6633 | ||
6634 | case MINUS_EXPR: | |
6635 | /* If the second operand of the MINUS_EXPR has a pointer | |
6636 | type (or is converted from it), this may be valid, so | |
6637 | don't give a warning. */ | |
6638 | { | |
6639 | tree op1 = TREE_OPERAND (inner, 1); | |
6640 | ||
6641 | while (! POINTER_TYPE_P (TREE_TYPE (op1)) | |
6642 | && (TREE_CODE (op1) == NOP_EXPR | |
6643 | || TREE_CODE (op1) == NON_LVALUE_EXPR | |
6644 | || TREE_CODE (op1) == CONVERT_EXPR)) | |
6645 | op1 = TREE_OPERAND (op1, 0); | |
6646 | ||
6647 | if (POINTER_TYPE_P (TREE_TYPE (op1))) | |
6648 | break; | |
88a3dbc1 | 6649 | |
abe80e6d RK |
6650 | inner = TREE_OPERAND (inner, 0); |
6651 | continue; | |
6652 | } | |
6653 | ||
6654 | case ADDR_EXPR: | |
6655 | inner = TREE_OPERAND (inner, 0); | |
88a3dbc1 | 6656 | |
abe80e6d RK |
6657 | while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r') |
6658 | inner = TREE_OPERAND (inner, 0); | |
6659 | ||
6660 | if (TREE_CODE (inner) == VAR_DECL | |
6661 | && ! DECL_EXTERNAL (inner) | |
6662 | && ! TREE_STATIC (inner) | |
6663 | && DECL_CONTEXT (inner) == current_function_decl) | |
6664 | warning ("function returns address of local variable"); | |
6665 | break; | |
e9a25f70 JL |
6666 | |
6667 | default: | |
6668 | break; | |
abe80e6d RK |
6669 | } |
6670 | ||
6671 | break; | |
88a3dbc1 RK |
6672 | } |
6673 | ||
6674 | t = build (MODIFY_EXPR, TREE_TYPE (res), res, t); | |
1c2a9b35 | 6675 | TREE_SIDE_EFFECTS (t) = 1; |
400fbf9f JW |
6676 | expand_return (t); |
6677 | current_function_returns_value = 1; | |
6678 | } | |
6679 | } | |
6680 | \f | |
6681 | /* Start a C switch statement, testing expression EXP. | |
6682 | Return EXP if it is valid, an error node otherwise. */ | |
6683 | ||
6684 | tree | |
6685 | c_expand_start_case (exp) | |
6686 | tree exp; | |
6687 | { | |
e89a9554 ZW |
6688 | register enum tree_code code; |
6689 | tree type; | |
6690 | ||
6691 | if (TREE_CODE (exp) == ERROR_MARK) | |
6692 | return exp; | |
6693 | ||
6694 | code = TREE_CODE (TREE_TYPE (exp)); | |
6695 | type = TREE_TYPE (exp); | |
400fbf9f JW |
6696 | |
6697 | if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK) | |
6698 | { | |
6699 | error ("switch quantity not an integer"); | |
6700 | exp = error_mark_node; | |
6701 | } | |
6702 | else | |
6703 | { | |
6704 | tree index; | |
6cb72a7d | 6705 | type = TYPE_MAIN_VARIANT (TREE_TYPE (exp)); |
400fbf9f JW |
6706 | |
6707 | if (warn_traditional | |
b00c3006 | 6708 | && ! in_system_header |
6cb72a7d RS |
6709 | && (type == long_integer_type_node |
6710 | || type == long_unsigned_type_node)) | |
400fbf9f JW |
6711 | pedwarn ("`long' switch expression not converted to `int' in ANSI C"); |
6712 | ||
6713 | exp = default_conversion (exp); | |
6714 | type = TREE_TYPE (exp); | |
8d9bfdc5 | 6715 | index = get_unwidened (exp, NULL_TREE); |
400fbf9f JW |
6716 | /* We can't strip a conversion from a signed type to an unsigned, |
6717 | because if we did, int_fits_type_p would do the wrong thing | |
6718 | when checking case values for being in range, | |
6719 | and it's too hard to do the right thing. */ | |
6720 | if (TREE_UNSIGNED (TREE_TYPE (exp)) | |
6721 | == TREE_UNSIGNED (TREE_TYPE (index))) | |
6722 | exp = index; | |
6723 | } | |
6724 | ||
6725 | expand_start_case (1, exp, type, "switch statement"); | |
6726 | ||
6727 | return exp; | |
6728 | } |