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