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