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