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