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