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