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