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