* recog.c (preproces_constraints): Zero recog_op_alt before
[official-gcc.git] / gcc / c-typeck.c
blob73d98e96419ba82a30879f49659d1495865132d0
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 88, 91-97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This file is part of the C front end.
23 It contains routines to build C expressions given their operands,
24 including computing the types of the result, C-specific error checks,
25 and some optimization.
27 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
28 and to process initializations in declarations (since they work
29 like a strange sort of assignment). */
31 #include "config.h"
32 #include "system.h"
33 #include "tree.h"
34 #include "c-tree.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "rtl.h"
38 #include "expr.h"
39 #include "toplev.h"
40 #include "intl.h"
41 #include "defaults.h"
43 /* Nonzero if we've already printed a "missing braces around initializer"
44 message within this initializer. */
45 static int missing_braces_mentioned;
47 static tree qualify_type PROTO((tree, tree));
48 static int comp_target_types PROTO((tree, tree));
49 static int function_types_compatible_p PROTO((tree, tree));
50 static int type_lists_compatible_p PROTO((tree, tree));
51 static int self_promoting_type_p PROTO((tree));
52 static tree decl_constant_value PROTO((tree));
53 static tree lookup_field PROTO((tree, tree, tree *));
54 static tree convert_arguments PROTO((tree, tree, tree, tree));
55 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
56 static tree pointer_diff PROTO((tree, tree));
57 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
58 static void pedantic_lvalue_warning PROTO((enum tree_code));
59 static tree internal_build_compound_expr PROTO((tree, int));
60 static tree convert_for_assignment PROTO((tree, tree, const char *, tree,
61 tree, int));
62 static void warn_for_assignment PROTO((const char *, const char *,
63 tree, int));
64 static tree valid_compound_expr_initializer PROTO((tree, tree));
65 static void push_string PROTO((const char *));
66 static void push_member_name PROTO((tree));
67 static void push_array_bounds PROTO((int));
68 static int spelling_length PROTO((void));
69 static char *print_spelling PROTO((char *));
70 static void warning_init PROTO((const char *));
71 static tree digest_init PROTO((tree, tree, int, int));
72 static void check_init_type_bitfields PROTO((tree));
73 static void output_init_element PROTO((tree, tree, tree, int));
74 static void output_pending_init_elements PROTO((int));
75 static void add_pending_init PROTO((tree, tree));
76 static int pending_init_member PROTO((tree));
78 /* Do `exp = require_complete_type (exp);' to make sure exp
79 does not have an incomplete type. (That includes void types.) */
81 tree
82 require_complete_type (value)
83 tree value;
85 tree type = TREE_TYPE (value);
87 if (TREE_CODE (value) == ERROR_MARK)
88 return error_mark_node;
90 /* First, detect a valid value with a complete type. */
91 if (TYPE_SIZE (type) != 0
92 && type != void_type_node)
93 return value;
95 incomplete_type_error (value, type);
96 return error_mark_node;
99 /* Print an error message for invalid use of an incomplete type.
100 VALUE is the expression that was used (or 0 if that isn't known)
101 and TYPE is the type that was invalid. */
103 void
104 incomplete_type_error (value, type)
105 tree value;
106 tree type;
108 const char *type_code_string;
110 /* Avoid duplicate error message. */
111 if (TREE_CODE (type) == ERROR_MARK)
112 return;
114 if (value != 0 && (TREE_CODE (value) == VAR_DECL
115 || TREE_CODE (value) == PARM_DECL))
116 error ("`%s' has an incomplete type",
117 IDENTIFIER_POINTER (DECL_NAME (value)));
118 else
120 retry:
121 /* We must print an error message. Be clever about what it says. */
123 switch (TREE_CODE (type))
125 case RECORD_TYPE:
126 type_code_string = "struct";
127 break;
129 case UNION_TYPE:
130 type_code_string = "union";
131 break;
133 case ENUMERAL_TYPE:
134 type_code_string = "enum";
135 break;
137 case VOID_TYPE:
138 error ("invalid use of void expression");
139 return;
141 case ARRAY_TYPE:
142 if (TYPE_DOMAIN (type))
144 type = TREE_TYPE (type);
145 goto retry;
147 error ("invalid use of array with unspecified bounds");
148 return;
150 default:
151 abort ();
154 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
155 error ("invalid use of undefined type `%s %s'",
156 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
157 else
158 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
159 error ("invalid use of incomplete typedef `%s'",
160 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
164 /* Return a variant of TYPE which has all the type qualifiers of LIKE
165 as well as those of TYPE. */
167 static tree
168 qualify_type (type, like)
169 tree type, like;
171 return c_build_qualified_type (type, TYPE_QUALS (like));
174 /* Return the common type of two types.
175 We assume that comptypes has already been done and returned 1;
176 if that isn't so, this may crash. In particular, we assume that qualifiers
177 match.
179 This is the type for the result of most arithmetic operations
180 if the operands have the given two types. */
182 tree
183 common_type (t1, t2)
184 tree t1, t2;
186 register enum tree_code code1;
187 register enum tree_code code2;
188 tree attributes;
190 /* Save time if the two types are the same. */
192 if (t1 == t2) return t1;
194 /* If one type is nonsense, use the other. */
195 if (t1 == error_mark_node)
196 return t2;
197 if (t2 == error_mark_node)
198 return t1;
200 /* Merge the attributes. */
201 attributes = merge_machine_type_attributes (t1, t2);
203 /* Treat an enum type as the unsigned integer type of the same width. */
205 if (TREE_CODE (t1) == ENUMERAL_TYPE)
206 t1 = type_for_size (TYPE_PRECISION (t1), 1);
207 if (TREE_CODE (t2) == ENUMERAL_TYPE)
208 t2 = type_for_size (TYPE_PRECISION (t2), 1);
210 code1 = TREE_CODE (t1);
211 code2 = TREE_CODE (t2);
213 /* If one type is complex, form the common type of the non-complex
214 components, then make that complex. Use T1 or T2 if it is the
215 required type. */
216 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
218 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
219 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
220 tree subtype = common_type (subtype1, subtype2);
222 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
223 return build_type_attribute_variant (t1, attributes);
224 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
225 return build_type_attribute_variant (t2, attributes);
226 else
227 return build_type_attribute_variant (build_complex_type (subtype),
228 attributes);
231 switch (code1)
233 case INTEGER_TYPE:
234 case REAL_TYPE:
235 /* If only one is real, use it as the result. */
237 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
238 return build_type_attribute_variant (t1, attributes);
240 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
241 return build_type_attribute_variant (t2, attributes);
243 /* Both real or both integers; use the one with greater precision. */
245 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
246 return build_type_attribute_variant (t1, attributes);
247 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
248 return build_type_attribute_variant (t2, attributes);
250 /* Same precision. Prefer longs to ints even when same size. */
252 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
253 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
254 return build_type_attribute_variant (long_unsigned_type_node,
255 attributes);
257 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
258 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
260 /* But preserve unsignedness from the other type,
261 since long cannot hold all the values of an unsigned int. */
262 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
263 t1 = long_unsigned_type_node;
264 else
265 t1 = long_integer_type_node;
266 return build_type_attribute_variant (t1, attributes);
269 /* Likewise, prefer long double to double even if same size. */
270 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
271 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
272 return build_type_attribute_variant (long_double_type_node,
273 attributes);
275 /* Otherwise prefer the unsigned one. */
277 if (TREE_UNSIGNED (t1))
278 return build_type_attribute_variant (t1, attributes);
279 else
280 return build_type_attribute_variant (t2, attributes);
282 case POINTER_TYPE:
283 /* For two pointers, do this recursively on the target type,
284 and combine the qualifiers of the two types' targets. */
285 /* This code was turned off; I don't know why.
286 But ANSI C specifies doing this with the qualifiers.
287 So I turned it on again. */
289 tree pointed_to_1 = TREE_TYPE (t1);
290 tree pointed_to_2 = TREE_TYPE (t2);
291 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
292 TYPE_MAIN_VARIANT (pointed_to_2));
293 t1 = build_pointer_type (c_build_qualified_type
294 (target,
295 TYPE_QUALS (pointed_to_1) |
296 TYPE_QUALS (pointed_to_2)));
297 return build_type_attribute_variant (t1, attributes);
299 #if 0
300 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
301 return build_type_attribute_variant (t1, attributes);
302 #endif
304 case ARRAY_TYPE:
306 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
307 /* Save space: see if the result is identical to one of the args. */
308 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
309 return build_type_attribute_variant (t1, attributes);
310 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
311 return build_type_attribute_variant (t2, attributes);
312 /* Merge the element types, and have a size if either arg has one. */
313 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
314 return build_type_attribute_variant (t1, attributes);
317 case FUNCTION_TYPE:
318 /* Function types: prefer the one that specified arg types.
319 If both do, merge the arg types. Also merge the return types. */
321 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
322 tree p1 = TYPE_ARG_TYPES (t1);
323 tree p2 = TYPE_ARG_TYPES (t2);
324 int len;
325 tree newargs, n;
326 int i;
328 /* Save space: see if the result is identical to one of the args. */
329 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
330 return build_type_attribute_variant (t1, attributes);
331 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
332 return build_type_attribute_variant (t2, attributes);
334 /* Simple way if one arg fails to specify argument types. */
335 if (TYPE_ARG_TYPES (t1) == 0)
337 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
338 return build_type_attribute_variant (t1, attributes);
340 if (TYPE_ARG_TYPES (t2) == 0)
342 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
343 return build_type_attribute_variant (t1, attributes);
346 /* If both args specify argument types, we must merge the two
347 lists, argument by argument. */
349 len = list_length (p1);
350 newargs = 0;
352 for (i = 0; i < len; i++)
353 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
355 n = newargs;
357 for (; p1;
358 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
360 /* A null type means arg type is not specified.
361 Take whatever the other function type has. */
362 if (TREE_VALUE (p1) == 0)
364 TREE_VALUE (n) = TREE_VALUE (p2);
365 goto parm_done;
367 if (TREE_VALUE (p2) == 0)
369 TREE_VALUE (n) = TREE_VALUE (p1);
370 goto parm_done;
373 /* Given wait (union {union wait *u; int *i} *)
374 and wait (union wait *),
375 prefer union wait * as type of parm. */
376 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
377 && TREE_VALUE (p1) != TREE_VALUE (p2))
379 tree memb;
380 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
381 memb; memb = TREE_CHAIN (memb))
382 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
384 TREE_VALUE (n) = TREE_VALUE (p2);
385 if (pedantic)
386 pedwarn ("function types not truly compatible in ANSI C");
387 goto parm_done;
390 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
391 && TREE_VALUE (p2) != TREE_VALUE (p1))
393 tree memb;
394 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
395 memb; memb = TREE_CHAIN (memb))
396 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
398 TREE_VALUE (n) = TREE_VALUE (p1);
399 if (pedantic)
400 pedwarn ("function types not truly compatible in ANSI C");
401 goto parm_done;
404 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
405 parm_done: ;
408 t1 = build_function_type (valtype, newargs);
409 /* ... falls through ... */
412 default:
413 return build_type_attribute_variant (t1, attributes);
418 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
419 or various other operations. Return 2 if they are compatible
420 but a warning may be needed if you use them together. */
423 comptypes (type1, type2)
424 tree type1, type2;
426 register tree t1 = type1;
427 register tree t2 = type2;
428 int attrval, val;
430 /* Suppress errors caused by previously reported errors. */
432 if (t1 == t2 || !t1 || !t2
433 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
434 return 1;
436 /* Treat an enum type as the integer type of the same width and
437 signedness. */
439 if (TREE_CODE (t1) == ENUMERAL_TYPE)
440 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
441 if (TREE_CODE (t2) == ENUMERAL_TYPE)
442 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
444 if (t1 == t2)
445 return 1;
447 /* Different classes of types can't be compatible. */
449 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
451 /* Qualifiers must match. */
453 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
454 return 0;
456 /* Allow for two different type nodes which have essentially the same
457 definition. Note that we already checked for equality of the type
458 qualifiers (just above). */
460 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
461 return 1;
463 #ifndef COMP_TYPE_ATTRIBUTES
464 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
465 #endif
467 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
468 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
469 return 0;
471 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
472 val = 0;
474 switch (TREE_CODE (t1))
476 case POINTER_TYPE:
477 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
478 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
479 break;
481 case FUNCTION_TYPE:
482 val = function_types_compatible_p (t1, t2);
483 break;
485 case ARRAY_TYPE:
487 tree d1 = TYPE_DOMAIN (t1);
488 tree d2 = TYPE_DOMAIN (t2);
489 val = 1;
491 /* Target types must match incl. qualifiers. */
492 if (TREE_TYPE (t1) != TREE_TYPE (t2)
493 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
494 return 0;
496 /* Sizes must match unless one is missing or variable. */
497 if (d1 == 0 || d2 == 0 || d1 == d2
498 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
499 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
500 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
501 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
502 break;
504 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
505 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
506 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
507 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
508 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
509 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
510 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
511 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
512 val = 0;
513 break;
516 case RECORD_TYPE:
517 if (maybe_objc_comptypes (t1, t2, 0) == 1)
518 val = 1;
519 break;
521 default:
522 break;
524 return attrval == 2 && val == 1 ? 2 : val;
527 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
528 ignoring their qualifiers. */
530 static int
531 comp_target_types (ttl, ttr)
532 tree ttl, ttr;
534 int val;
536 /* Give maybe_objc_comptypes a crack at letting these types through. */
537 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
538 return val;
540 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
541 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
543 if (val == 2 && pedantic)
544 pedwarn ("types are not quite compatible");
545 return val;
548 /* Subroutines of `comptypes'. */
550 /* Return 1 if two function types F1 and F2 are compatible.
551 If either type specifies no argument types,
552 the other must specify a fixed number of self-promoting arg types.
553 Otherwise, if one type specifies only the number of arguments,
554 the other must specify that number of self-promoting arg types.
555 Otherwise, the argument types must match. */
557 static int
558 function_types_compatible_p (f1, f2)
559 tree f1, f2;
561 tree args1, args2;
562 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
563 int val = 1;
564 int val1;
566 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
567 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
568 return 0;
570 args1 = TYPE_ARG_TYPES (f1);
571 args2 = TYPE_ARG_TYPES (f2);
573 /* An unspecified parmlist matches any specified parmlist
574 whose argument types don't need default promotions. */
576 if (args1 == 0)
578 if (!self_promoting_args_p (args2))
579 return 0;
580 /* If one of these types comes from a non-prototype fn definition,
581 compare that with the other type's arglist.
582 If they don't match, ask for a warning (but no error). */
583 if (TYPE_ACTUAL_ARG_TYPES (f1)
584 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
585 val = 2;
586 return val;
588 if (args2 == 0)
590 if (!self_promoting_args_p (args1))
591 return 0;
592 if (TYPE_ACTUAL_ARG_TYPES (f2)
593 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
594 val = 2;
595 return val;
598 /* Both types have argument lists: compare them and propagate results. */
599 val1 = type_lists_compatible_p (args1, args2);
600 return val1 != 1 ? val1 : val;
603 /* Check two lists of types for compatibility,
604 returning 0 for incompatible, 1 for compatible,
605 or 2 for compatible with warning. */
607 static int
608 type_lists_compatible_p (args1, args2)
609 tree args1, args2;
611 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
612 int val = 1;
613 int newval = 0;
615 while (1)
617 if (args1 == 0 && args2 == 0)
618 return val;
619 /* If one list is shorter than the other,
620 they fail to match. */
621 if (args1 == 0 || args2 == 0)
622 return 0;
623 /* A null pointer instead of a type
624 means there is supposed to be an argument
625 but nothing is specified about what type it has.
626 So match anything that self-promotes. */
627 if (TREE_VALUE (args1) == 0)
629 if (! self_promoting_type_p (TREE_VALUE (args2)))
630 return 0;
632 else if (TREE_VALUE (args2) == 0)
634 if (! self_promoting_type_p (TREE_VALUE (args1)))
635 return 0;
637 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
639 /* Allow wait (union {union wait *u; int *i} *)
640 and wait (union wait *) to be compatible. */
641 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
642 && (TYPE_NAME (TREE_VALUE (args1)) == 0
643 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
644 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
645 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
646 TYPE_SIZE (TREE_VALUE (args2))))
648 tree memb;
649 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
650 memb; memb = TREE_CHAIN (memb))
651 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
652 break;
653 if (memb == 0)
654 return 0;
656 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
657 && (TYPE_NAME (TREE_VALUE (args2)) == 0
658 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
659 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
660 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
661 TYPE_SIZE (TREE_VALUE (args1))))
663 tree memb;
664 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
665 memb; memb = TREE_CHAIN (memb))
666 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
667 break;
668 if (memb == 0)
669 return 0;
671 else
672 return 0;
675 /* comptypes said ok, but record if it said to warn. */
676 if (newval > val)
677 val = newval;
679 args1 = TREE_CHAIN (args1);
680 args2 = TREE_CHAIN (args2);
684 /* Return 1 if PARMS specifies a fixed number of parameters
685 and none of their types is affected by default promotions. */
688 self_promoting_args_p (parms)
689 tree parms;
691 register tree t;
692 for (t = parms; t; t = TREE_CHAIN (t))
694 register tree type = TREE_VALUE (t);
696 if (TREE_CHAIN (t) == 0 && type != void_type_node)
697 return 0;
699 if (type == 0)
700 return 0;
702 if (TYPE_MAIN_VARIANT (type) == float_type_node)
703 return 0;
705 if (C_PROMOTING_INTEGER_TYPE_P (type))
706 return 0;
708 return 1;
711 /* Return 1 if TYPE is not affected by default promotions. */
713 static int
714 self_promoting_type_p (type)
715 tree type;
717 if (TYPE_MAIN_VARIANT (type) == float_type_node)
718 return 0;
720 if (C_PROMOTING_INTEGER_TYPE_P (type))
721 return 0;
723 return 1;
726 /* Compute the value of the `sizeof' operator. */
728 tree
729 c_sizeof (type)
730 tree type;
732 enum tree_code code = TREE_CODE (type);
733 tree t;
735 if (code == FUNCTION_TYPE)
737 if (pedantic || warn_pointer_arith)
738 pedwarn ("sizeof applied to a function type");
739 return size_int (1);
741 if (code == VOID_TYPE)
743 if (pedantic || warn_pointer_arith)
744 pedwarn ("sizeof applied to a void type");
745 return size_int (1);
747 if (code == ERROR_MARK)
748 return size_int (1);
749 if (TYPE_SIZE (type) == 0)
751 error ("sizeof applied to an incomplete type");
752 return size_int (0);
755 /* Convert in case a char is more than one unit. */
756 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
757 size_int (TYPE_PRECISION (char_type_node)));
758 t = convert (sizetype, t);
759 /* size_binop does not put the constant in range, so do it now. */
760 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
761 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
762 return t;
765 tree
766 c_sizeof_nowarn (type)
767 tree type;
769 enum tree_code code = TREE_CODE (type);
770 tree t;
772 if (code == FUNCTION_TYPE
773 || code == VOID_TYPE
774 || code == ERROR_MARK)
775 return size_int (1);
776 if (TYPE_SIZE (type) == 0)
777 return size_int (0);
779 /* Convert in case a char is more than one unit. */
780 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
781 size_int (TYPE_PRECISION (char_type_node)));
782 t = convert (sizetype, t);
783 force_fit_type (t, 0);
784 return t;
787 /* Compute the size to increment a pointer by. */
789 tree
790 c_size_in_bytes (type)
791 tree type;
793 enum tree_code code = TREE_CODE (type);
794 tree t;
796 if (code == FUNCTION_TYPE)
797 return size_int (1);
798 if (code == VOID_TYPE)
799 return size_int (1);
800 if (code == ERROR_MARK)
801 return size_int (1);
802 if (TYPE_SIZE (type) == 0)
804 error ("arithmetic on pointer to an incomplete type");
805 return size_int (1);
808 /* Convert in case a char is more than one unit. */
809 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
810 size_int (BITS_PER_UNIT));
811 t = convert (sizetype, t);
812 force_fit_type (t, 0);
813 return t;
816 /* Implement the __alignof keyword: Return the minimum required
817 alignment of TYPE, measured in bytes. */
819 tree
820 c_alignof (type)
821 tree type;
823 enum tree_code code = TREE_CODE (type);
825 if (code == FUNCTION_TYPE)
826 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
828 if (code == VOID_TYPE || code == ERROR_MARK)
829 return size_int (1);
831 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
834 /* Implement the __alignof keyword: Return the minimum required
835 alignment of EXPR, measured in bytes. For VAR_DECL's and
836 FIELD_DECL's return DECL_ALIGN (which can be set from an
837 "aligned" __attribute__ specification). */
839 tree
840 c_alignof_expr (expr)
841 tree expr;
843 if (TREE_CODE (expr) == VAR_DECL)
844 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
846 if (TREE_CODE (expr) == COMPONENT_REF
847 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
849 error ("`__alignof' applied to a bit-field");
850 return size_int (1);
852 else if (TREE_CODE (expr) == COMPONENT_REF
853 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
854 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
856 if (TREE_CODE (expr) == INDIRECT_REF)
858 tree t = TREE_OPERAND (expr, 0);
859 tree best = t;
860 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
862 while (TREE_CODE (t) == NOP_EXPR
863 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
865 int thisalign;
867 t = TREE_OPERAND (t, 0);
868 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
869 if (thisalign > bestalign)
870 best = t, bestalign = thisalign;
872 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
874 else
875 return c_alignof (TREE_TYPE (expr));
878 /* Return either DECL or its known constant value (if it has one). */
880 static tree
881 decl_constant_value (decl)
882 tree decl;
884 if (/* Don't change a variable array bound or initial value to a constant
885 in a place where a variable is invalid. */
886 current_function_decl != 0
887 && ! pedantic
888 && ! TREE_THIS_VOLATILE (decl)
889 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
890 && DECL_INITIAL (decl) != 0
891 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
892 /* This is invalid if initial value is not constant.
893 If it has either a function call, a memory reference,
894 or a variable, then re-evaluating it could give different results. */
895 && TREE_CONSTANT (DECL_INITIAL (decl))
896 /* Check for cases where this is sub-optimal, even though valid. */
897 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
898 && DECL_MODE (decl) != BLKmode)
899 return DECL_INITIAL (decl);
900 return decl;
903 /* Perform default promotions for C data used in expressions.
904 Arrays and functions are converted to pointers;
905 enumeral types or short or char, to int.
906 In addition, manifest constants symbols are replaced by their values. */
908 tree
909 default_conversion (exp)
910 tree exp;
912 register tree type = TREE_TYPE (exp);
913 register enum tree_code code = TREE_CODE (type);
915 /* Constants can be used directly unless they're not loadable. */
916 if (TREE_CODE (exp) == CONST_DECL)
917 exp = DECL_INITIAL (exp);
919 /* Replace a nonvolatile const static variable with its value unless
920 it is an array, in which case we must be sure that taking the
921 address of the array produces consistent results. */
922 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
924 exp = decl_constant_value (exp);
925 type = TREE_TYPE (exp);
928 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
929 an lvalue. */
930 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
931 to integer and cause infinite recursion. */
932 while (TREE_CODE (exp) == NON_LVALUE_EXPR
933 || (TREE_CODE (exp) == NOP_EXPR
934 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
935 exp = TREE_OPERAND (exp, 0);
937 /* Normally convert enums to int,
938 but convert wide enums to something wider. */
939 if (code == ENUMERAL_TYPE)
941 type = type_for_size (MAX (TYPE_PRECISION (type),
942 TYPE_PRECISION (integer_type_node)),
943 ((flag_traditional
944 || (TYPE_PRECISION (type)
945 >= TYPE_PRECISION (integer_type_node)))
946 && TREE_UNSIGNED (type)));
947 return convert (type, exp);
950 if (TREE_CODE (exp) == COMPONENT_REF
951 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
953 tree width = DECL_SIZE (TREE_OPERAND (exp, 1));
954 HOST_WIDE_INT low = TREE_INT_CST_LOW (width);
956 /* If it's thinner than an int, promote it like a
957 C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */
959 if (low < TYPE_PRECISION (integer_type_node))
961 if (flag_traditional && TREE_UNSIGNED (type))
962 return convert (unsigned_type_node, exp);
963 else
964 return convert (integer_type_node, exp);
968 if (C_PROMOTING_INTEGER_TYPE_P (type))
970 /* Traditionally, unsignedness is preserved in default promotions.
971 Also preserve unsignedness if not really getting any wider. */
972 if (TREE_UNSIGNED (type)
973 && (flag_traditional
974 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
975 return convert (unsigned_type_node, exp);
976 return convert (integer_type_node, exp);
978 if (flag_traditional && !flag_allow_single_precision
979 && TYPE_MAIN_VARIANT (type) == float_type_node)
980 return convert (double_type_node, exp);
981 if (code == VOID_TYPE)
983 error ("void value not ignored as it ought to be");
984 return error_mark_node;
986 if (code == FUNCTION_TYPE)
988 return build_unary_op (ADDR_EXPR, exp, 0);
990 if (code == ARRAY_TYPE)
992 register tree adr;
993 tree restype = TREE_TYPE (type);
994 tree ptrtype;
995 int constp = 0;
996 int volatilep = 0;
998 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
999 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1001 constp = TREE_READONLY (exp);
1002 volatilep = TREE_THIS_VOLATILE (exp);
1005 if (TYPE_QUALS (type) || constp || volatilep)
1006 restype
1007 = c_build_qualified_type (restype,
1008 TYPE_QUALS (type)
1009 | (constp * TYPE_QUAL_CONST)
1010 | (volatilep * TYPE_QUAL_VOLATILE));
1012 if (TREE_CODE (exp) == INDIRECT_REF)
1013 return convert (TYPE_POINTER_TO (restype),
1014 TREE_OPERAND (exp, 0));
1016 if (TREE_CODE (exp) == COMPOUND_EXPR)
1018 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1019 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1020 TREE_OPERAND (exp, 0), op1);
1023 if (! lvalue_p (exp)
1024 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1026 error ("invalid use of non-lvalue array");
1027 return error_mark_node;
1030 ptrtype = build_pointer_type (restype);
1032 if (TREE_CODE (exp) == VAR_DECL)
1034 /* ??? This is not really quite correct
1035 in that the type of the operand of ADDR_EXPR
1036 is not the target type of the type of the ADDR_EXPR itself.
1037 Question is, can this lossage be avoided? */
1038 adr = build1 (ADDR_EXPR, ptrtype, exp);
1039 if (mark_addressable (exp) == 0)
1040 return error_mark_node;
1041 TREE_CONSTANT (adr) = staticp (exp);
1042 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1043 return adr;
1045 /* This way is better for a COMPONENT_REF since it can
1046 simplify the offset for a component. */
1047 adr = build_unary_op (ADDR_EXPR, exp, 1);
1048 return convert (ptrtype, adr);
1050 return exp;
1053 /* Look up component name in the structure type definition.
1055 If this component name is found indirectly within an anonymous union,
1056 store in *INDIRECT the component which directly contains
1057 that anonymous union. Otherwise, set *INDIRECT to 0. */
1059 static tree
1060 lookup_field (type, component, indirect)
1061 tree type, component;
1062 tree *indirect;
1064 tree field;
1066 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1067 to the field elements. Use a binary search on this array to quickly
1068 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1069 will always be set for structures which have many elements. */
1071 if (TYPE_LANG_SPECIFIC (type))
1073 int bot, top, half;
1074 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1076 field = TYPE_FIELDS (type);
1077 bot = 0;
1078 top = TYPE_LANG_SPECIFIC (type)->len;
1079 while (top - bot > 1)
1081 half = (top - bot + 1) >> 1;
1082 field = field_array[bot+half];
1084 if (DECL_NAME (field) == NULL_TREE)
1086 /* Step through all anon unions in linear fashion. */
1087 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1089 tree anon = 0, junk;
1091 field = field_array[bot++];
1092 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1093 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1094 anon = lookup_field (TREE_TYPE (field), component, &junk);
1096 if (anon != NULL_TREE)
1098 *indirect = field;
1099 return anon;
1103 /* Entire record is only anon unions. */
1104 if (bot > top)
1105 return NULL_TREE;
1107 /* Restart the binary search, with new lower bound. */
1108 continue;
1111 if (DECL_NAME (field) == component)
1112 break;
1113 if (DECL_NAME (field) < component)
1114 bot += half;
1115 else
1116 top = bot + half;
1119 if (DECL_NAME (field_array[bot]) == component)
1120 field = field_array[bot];
1121 else if (DECL_NAME (field) != component)
1122 field = 0;
1124 else
1126 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1128 if (DECL_NAME (field) == NULL_TREE)
1130 tree junk;
1131 tree anon = 0;
1133 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1134 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1135 anon = lookup_field (TREE_TYPE (field), component, &junk);
1137 if (anon != NULL_TREE)
1139 *indirect = field;
1140 return anon;
1144 if (DECL_NAME (field) == component)
1145 break;
1149 *indirect = NULL_TREE;
1150 return field;
1153 /* Make an expression to refer to the COMPONENT field of
1154 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1156 tree
1157 build_component_ref (datum, component)
1158 tree datum, component;
1160 register tree type = TREE_TYPE (datum);
1161 register enum tree_code code = TREE_CODE (type);
1162 register tree field = NULL;
1163 register tree ref;
1165 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1166 unless we are not to support things not strictly ANSI. */
1167 switch (TREE_CODE (datum))
1169 case COMPOUND_EXPR:
1171 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1172 return build (COMPOUND_EXPR, TREE_TYPE (value),
1173 TREE_OPERAND (datum, 0), value);
1175 case COND_EXPR:
1176 return build_conditional_expr
1177 (TREE_OPERAND (datum, 0),
1178 build_component_ref (TREE_OPERAND (datum, 1), component),
1179 build_component_ref (TREE_OPERAND (datum, 2), component));
1181 default:
1182 break;
1185 /* See if there is a field or component with name COMPONENT. */
1187 if (code == RECORD_TYPE || code == UNION_TYPE)
1189 tree indirect = 0;
1191 if (TYPE_SIZE (type) == 0)
1193 incomplete_type_error (NULL_TREE, type);
1194 return error_mark_node;
1197 field = lookup_field (type, component, &indirect);
1199 if (!field)
1201 error (code == RECORD_TYPE
1202 ? "structure has no member named `%s'"
1203 : "union has no member named `%s'",
1204 IDENTIFIER_POINTER (component));
1205 return error_mark_node;
1207 if (TREE_TYPE (field) == error_mark_node)
1208 return error_mark_node;
1210 /* If FIELD was found buried within an anonymous union,
1211 make one COMPONENT_REF to get that anonymous union,
1212 then fall thru to make a second COMPONENT_REF to get FIELD. */
1213 if (indirect != 0)
1215 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1216 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1217 TREE_READONLY (ref) = 1;
1218 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1219 TREE_THIS_VOLATILE (ref) = 1;
1220 datum = ref;
1223 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1225 if (TREE_READONLY (datum) || TREE_READONLY (field))
1226 TREE_READONLY (ref) = 1;
1227 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1228 TREE_THIS_VOLATILE (ref) = 1;
1230 return ref;
1232 else if (code != ERROR_MARK)
1233 error ("request for member `%s' in something not a structure or union",
1234 IDENTIFIER_POINTER (component));
1236 return error_mark_node;
1239 /* Given an expression PTR for a pointer, return an expression
1240 for the value pointed to.
1241 ERRORSTRING is the name of the operator to appear in error messages. */
1243 tree
1244 build_indirect_ref (ptr, errorstring)
1245 tree ptr;
1246 const char *errorstring;
1248 register tree pointer = default_conversion (ptr);
1249 register tree type = TREE_TYPE (pointer);
1251 if (TREE_CODE (type) == POINTER_TYPE)
1253 if (TREE_CODE (pointer) == ADDR_EXPR
1254 && !flag_volatile
1255 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1256 == TREE_TYPE (type)))
1257 return TREE_OPERAND (pointer, 0);
1258 else
1260 tree t = TREE_TYPE (type);
1261 register tree ref = build1 (INDIRECT_REF,
1262 TYPE_MAIN_VARIANT (t), pointer);
1264 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1266 error ("dereferencing pointer to incomplete type");
1267 return error_mark_node;
1269 if (TREE_CODE (t) == VOID_TYPE && skip_evaluation == 0)
1270 warning ("dereferencing `void *' pointer");
1272 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1273 so that we get the proper error message if the result is used
1274 to assign to. Also, &* is supposed to be a no-op.
1275 And ANSI C seems to specify that the type of the result
1276 should be the const type. */
1277 /* A de-reference of a pointer to const is not a const. It is valid
1278 to change it via some other pointer. */
1279 TREE_READONLY (ref) = TYPE_READONLY (t);
1280 TREE_SIDE_EFFECTS (ref)
1281 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1282 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1283 return ref;
1286 else if (TREE_CODE (pointer) != ERROR_MARK)
1287 error ("invalid type argument of `%s'", errorstring);
1288 return error_mark_node;
1291 /* This handles expressions of the form "a[i]", which denotes
1292 an array reference.
1294 This is logically equivalent in C to *(a+i), but we may do it differently.
1295 If A is a variable or a member, we generate a primitive ARRAY_REF.
1296 This avoids forcing the array out of registers, and can work on
1297 arrays that are not lvalues (for example, members of structures returned
1298 by functions). */
1300 tree
1301 build_array_ref (array, index)
1302 tree array, index;
1304 if (index == 0)
1306 error ("subscript missing in array reference");
1307 return error_mark_node;
1310 if (TREE_TYPE (array) == error_mark_node
1311 || TREE_TYPE (index) == error_mark_node)
1312 return error_mark_node;
1314 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1315 && TREE_CODE (array) != INDIRECT_REF)
1317 tree rval, type;
1319 /* Subscripting with type char is likely to lose
1320 on a machine where chars are signed.
1321 So warn on any machine, but optionally.
1322 Don't warn for unsigned char since that type is safe.
1323 Don't warn for signed char because anyone who uses that
1324 must have done so deliberately. */
1325 if (warn_char_subscripts
1326 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1327 warning ("array subscript has type `char'");
1329 /* Apply default promotions *after* noticing character types. */
1330 index = default_conversion (index);
1332 /* Require integer *after* promotion, for sake of enums. */
1333 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1335 error ("array subscript is not an integer");
1336 return error_mark_node;
1339 /* An array that is indexed by a non-constant
1340 cannot be stored in a register; we must be able to do
1341 address arithmetic on its address.
1342 Likewise an array of elements of variable size. */
1343 if (TREE_CODE (index) != INTEGER_CST
1344 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1345 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1347 if (mark_addressable (array) == 0)
1348 return error_mark_node;
1350 /* An array that is indexed by a constant value which is not within
1351 the array bounds cannot be stored in a register either; because we
1352 would get a crash in store_bit_field/extract_bit_field when trying
1353 to access a non-existent part of the register. */
1354 if (TREE_CODE (index) == INTEGER_CST
1355 && TYPE_VALUES (TREE_TYPE (array))
1356 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1358 if (mark_addressable (array) == 0)
1359 return error_mark_node;
1362 if (pedantic && !lvalue_p (array))
1364 if (DECL_REGISTER (array))
1365 pedwarn ("ANSI C forbids subscripting `register' array");
1366 else
1367 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1370 if (pedantic)
1372 tree foo = array;
1373 while (TREE_CODE (foo) == COMPONENT_REF)
1374 foo = TREE_OPERAND (foo, 0);
1375 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1376 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1379 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1380 rval = build (ARRAY_REF, type, array, index);
1381 /* Array ref is const/volatile if the array elements are
1382 or if the array is. */
1383 TREE_READONLY (rval)
1384 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1385 | TREE_READONLY (array));
1386 TREE_SIDE_EFFECTS (rval)
1387 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1388 | TREE_SIDE_EFFECTS (array));
1389 TREE_THIS_VOLATILE (rval)
1390 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1391 /* This was added by rms on 16 Nov 91.
1392 It fixes vol struct foo *a; a->elts[1]
1393 in an inline function.
1394 Hope it doesn't break something else. */
1395 | TREE_THIS_VOLATILE (array));
1396 return require_complete_type (fold (rval));
1400 tree ar = default_conversion (array);
1401 tree ind = default_conversion (index);
1403 /* Do the same warning check as above, but only on the part that's
1404 syntactically the index and only if it is also semantically
1405 the index. */
1406 if (warn_char_subscripts
1407 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1408 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1409 warning ("subscript has type `char'");
1411 /* Put the integer in IND to simplify error checking. */
1412 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1414 tree temp = ar;
1415 ar = ind;
1416 ind = temp;
1419 if (ar == error_mark_node)
1420 return ar;
1422 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1423 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1425 error ("subscripted value is neither array nor pointer");
1426 return error_mark_node;
1428 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1430 error ("array subscript is not an integer");
1431 return error_mark_node;
1434 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1435 "array indexing");
1439 /* Build a function call to function FUNCTION with parameters PARAMS.
1440 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1441 TREE_VALUE of each node is a parameter-expression.
1442 FUNCTION's data type may be a function type or a pointer-to-function. */
1444 tree
1445 build_function_call (function, params)
1446 tree function, params;
1448 register tree fntype, fundecl = 0;
1449 register tree coerced_params;
1450 tree name = NULL_TREE, assembler_name = NULL_TREE;
1452 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1453 STRIP_TYPE_NOPS (function);
1455 /* Convert anything with function type to a pointer-to-function. */
1456 if (TREE_CODE (function) == FUNCTION_DECL)
1458 name = DECL_NAME (function);
1459 assembler_name = DECL_ASSEMBLER_NAME (function);
1461 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1462 (because calling an inline function does not mean the function
1463 needs to be separately compiled). */
1464 fntype = build_type_variant (TREE_TYPE (function),
1465 TREE_READONLY (function),
1466 TREE_THIS_VOLATILE (function));
1467 fundecl = function;
1468 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1470 else
1471 function = default_conversion (function);
1473 fntype = TREE_TYPE (function);
1475 if (TREE_CODE (fntype) == ERROR_MARK)
1476 return error_mark_node;
1478 if (!(TREE_CODE (fntype) == POINTER_TYPE
1479 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1481 error ("called object is not a function");
1482 return error_mark_node;
1485 /* fntype now gets the type of function pointed to. */
1486 fntype = TREE_TYPE (fntype);
1488 /* Convert the parameters to the types declared in the
1489 function prototype, or apply default promotions. */
1491 coerced_params
1492 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1494 /* Check for errors in format strings. */
1496 if (warn_format && (name || assembler_name))
1497 check_function_format (name, assembler_name, coerced_params);
1499 /* Recognize certain built-in functions so we can make tree-codes
1500 other than CALL_EXPR. We do this when it enables fold-const.c
1501 to do something useful. */
1503 if (TREE_CODE (function) == ADDR_EXPR
1504 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1505 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1506 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1508 case BUILT_IN_ABS:
1509 case BUILT_IN_LABS:
1510 case BUILT_IN_FABS:
1511 if (coerced_params == 0)
1512 return integer_zero_node;
1513 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1514 default:
1515 break;
1519 register tree result
1520 = build (CALL_EXPR, TREE_TYPE (fntype),
1521 function, coerced_params, NULL_TREE);
1523 TREE_SIDE_EFFECTS (result) = 1;
1524 if (TREE_TYPE (result) == void_type_node)
1525 return result;
1526 return require_complete_type (result);
1530 /* Convert the argument expressions in the list VALUES
1531 to the types in the list TYPELIST. The result is a list of converted
1532 argument expressions.
1534 If TYPELIST is exhausted, or when an element has NULL as its type,
1535 perform the default conversions.
1537 PARMLIST is the chain of parm decls for the function being called.
1538 It may be 0, if that info is not available.
1539 It is used only for generating error messages.
1541 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1543 This is also where warnings about wrong number of args are generated.
1545 Both VALUES and the returned value are chains of TREE_LIST nodes
1546 with the elements of the list in the TREE_VALUE slots of those nodes. */
1548 static tree
1549 convert_arguments (typelist, values, name, fundecl)
1550 tree typelist, values, name, fundecl;
1552 register tree typetail, valtail;
1553 register tree result = NULL;
1554 int parmnum;
1556 /* Scan the given expressions and types, producing individual
1557 converted arguments and pushing them on RESULT in reverse order. */
1559 for (valtail = values, typetail = typelist, parmnum = 0;
1560 valtail;
1561 valtail = TREE_CHAIN (valtail), parmnum++)
1563 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1564 register tree val = TREE_VALUE (valtail);
1566 if (type == void_type_node)
1568 if (name)
1569 error ("too many arguments to function `%s'",
1570 IDENTIFIER_POINTER (name));
1571 else
1572 error ("too many arguments to function");
1573 break;
1576 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1577 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1578 to convert automatically to a pointer. */
1579 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1580 val = TREE_OPERAND (val, 0);
1582 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1583 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1584 val = default_conversion (val);
1586 val = require_complete_type (val);
1588 if (type != 0)
1590 /* Formal parm type is specified by a function prototype. */
1591 tree parmval;
1593 if (TYPE_SIZE (type) == 0)
1595 error ("type of formal parameter %d is incomplete", parmnum + 1);
1596 parmval = val;
1598 else
1600 /* Optionally warn about conversions that
1601 differ from the default conversions. */
1602 if (warn_conversion)
1604 int formal_prec = TYPE_PRECISION (type);
1606 if (INTEGRAL_TYPE_P (type)
1607 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1608 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1609 else if (TREE_CODE (type) == COMPLEX_TYPE
1610 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1611 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1612 else if (TREE_CODE (type) == REAL_TYPE
1613 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1614 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1615 else if (TREE_CODE (type) == REAL_TYPE
1616 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1617 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1618 /* ??? At some point, messages should be written about
1619 conversions between complex types, but that's too messy
1620 to do now. */
1621 else if (TREE_CODE (type) == REAL_TYPE
1622 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1624 /* Warn if any argument is passed as `float',
1625 since without a prototype it would be `double'. */
1626 if (formal_prec == TYPE_PRECISION (float_type_node))
1627 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1629 /* Detect integer changing in width or signedness. */
1630 else if (INTEGRAL_TYPE_P (type)
1631 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1633 tree would_have_been = default_conversion (val);
1634 tree type1 = TREE_TYPE (would_have_been);
1636 if (TREE_CODE (type) == ENUMERAL_TYPE
1637 && type == TREE_TYPE (val))
1638 /* No warning if function asks for enum
1639 and the actual arg is that enum type. */
1641 else if (formal_prec != TYPE_PRECISION (type1))
1642 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1643 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1645 /* Don't complain if the formal parameter type
1646 is an enum, because we can't tell now whether
1647 the value was an enum--even the same enum. */
1648 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1650 else if (TREE_CODE (val) == INTEGER_CST
1651 && int_fits_type_p (val, type))
1652 /* Change in signedness doesn't matter
1653 if a constant value is unaffected. */
1655 /* Likewise for a constant in a NOP_EXPR. */
1656 else if (TREE_CODE (val) == NOP_EXPR
1657 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1658 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1660 #if 0 /* We never get such tree structure here. */
1661 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1662 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1663 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1664 /* Change in signedness doesn't matter
1665 if an enum value is unaffected. */
1667 #endif
1668 /* If the value is extended from a narrower
1669 unsigned type, it doesn't matter whether we
1670 pass it as signed or unsigned; the value
1671 certainly is the same either way. */
1672 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1673 && TREE_UNSIGNED (TREE_TYPE (val)))
1675 else if (TREE_UNSIGNED (type))
1676 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1677 else
1678 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1682 parmval = convert_for_assignment (type, val,
1683 (char *) 0, /* arg passing */
1684 fundecl, name, parmnum + 1);
1686 if (PROMOTE_PROTOTYPES
1687 && (TREE_CODE (type) == INTEGER_TYPE
1688 || TREE_CODE (type) == ENUMERAL_TYPE)
1689 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1690 parmval = default_conversion (parmval);
1692 result = tree_cons (NULL_TREE, parmval, result);
1694 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1695 && (TYPE_PRECISION (TREE_TYPE (val))
1696 < TYPE_PRECISION (double_type_node)))
1697 /* Convert `float' to `double'. */
1698 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1699 else
1700 /* Convert `short' and `char' to full-size `int'. */
1701 result = tree_cons (NULL_TREE, default_conversion (val), result);
1703 if (typetail)
1704 typetail = TREE_CHAIN (typetail);
1707 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1709 if (name)
1710 error ("too few arguments to function `%s'",
1711 IDENTIFIER_POINTER (name));
1712 else
1713 error ("too few arguments to function");
1716 return nreverse (result);
1719 /* This is the entry point used by the parser
1720 for binary operators in the input.
1721 In addition to constructing the expression,
1722 we check for operands that were written with other binary operators
1723 in a way that is likely to confuse the user. */
1725 tree
1726 parser_build_binary_op (code, arg1, arg2)
1727 enum tree_code code;
1728 tree arg1, arg2;
1730 tree result = build_binary_op (code, arg1, arg2, 1);
1732 char class;
1733 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1734 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1735 enum tree_code code1 = ERROR_MARK;
1736 enum tree_code code2 = ERROR_MARK;
1738 if (class1 == 'e' || class1 == '1'
1739 || class1 == '2' || class1 == '<')
1740 code1 = C_EXP_ORIGINAL_CODE (arg1);
1741 if (class2 == 'e' || class2 == '1'
1742 || class2 == '2' || class2 == '<')
1743 code2 = C_EXP_ORIGINAL_CODE (arg2);
1745 /* Check for cases such as x+y<<z which users are likely
1746 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1747 is cleared to prevent these warnings. */
1748 if (warn_parentheses)
1750 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1752 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1753 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1754 warning ("suggest parentheses around + or - inside shift");
1757 if (code == TRUTH_ORIF_EXPR)
1759 if (code1 == TRUTH_ANDIF_EXPR
1760 || code2 == TRUTH_ANDIF_EXPR)
1761 warning ("suggest parentheses around && within ||");
1764 if (code == BIT_IOR_EXPR)
1766 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1767 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1768 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1769 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1770 warning ("suggest parentheses around arithmetic in operand of |");
1771 /* Check cases like x|y==z */
1772 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1773 warning ("suggest parentheses around comparison in operand of |");
1776 if (code == BIT_XOR_EXPR)
1778 if (code1 == BIT_AND_EXPR
1779 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1780 || code2 == BIT_AND_EXPR
1781 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1782 warning ("suggest parentheses around arithmetic in operand of ^");
1783 /* Check cases like x^y==z */
1784 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1785 warning ("suggest parentheses around comparison in operand of ^");
1788 if (code == BIT_AND_EXPR)
1790 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1791 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1792 warning ("suggest parentheses around + or - in operand of &");
1793 /* Check cases like x&y==z */
1794 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1795 warning ("suggest parentheses around comparison in operand of &");
1799 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1800 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1801 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1802 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1804 unsigned_conversion_warning (result, arg1);
1805 unsigned_conversion_warning (result, arg2);
1806 overflow_warning (result);
1808 class = TREE_CODE_CLASS (TREE_CODE (result));
1810 /* Record the code that was specified in the source,
1811 for the sake of warnings about confusing nesting. */
1812 if (class == 'e' || class == '1'
1813 || class == '2' || class == '<')
1814 C_SET_EXP_ORIGINAL_CODE (result, code);
1815 else
1817 int flag = TREE_CONSTANT (result);
1818 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1819 so that convert_for_assignment wouldn't strip it.
1820 That way, we got warnings for things like p = (1 - 1).
1821 But it turns out we should not get those warnings. */
1822 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1823 C_SET_EXP_ORIGINAL_CODE (result, code);
1824 TREE_CONSTANT (result) = flag;
1827 return result;
1830 /* Build a binary-operation expression without default conversions.
1831 CODE is the kind of expression to build.
1832 This function differs from `build' in several ways:
1833 the data type of the result is computed and recorded in it,
1834 warnings are generated if arg data types are invalid,
1835 special handling for addition and subtraction of pointers is known,
1836 and some optimization is done (operations on narrow ints
1837 are done in the narrower type when that gives the same result).
1838 Constant folding is also done before the result is returned.
1840 Note that the operands will never have enumeral types, or function
1841 or array types, because either they will have the default conversions
1842 performed or they have both just been converted to some other type in which
1843 the arithmetic is to be done. */
1845 tree
1846 build_binary_op (code, orig_op0, orig_op1, convert_p)
1847 enum tree_code code;
1848 tree orig_op0, orig_op1;
1849 int convert_p;
1851 tree type0, type1;
1852 register enum tree_code code0, code1;
1853 tree op0, op1;
1855 /* Expression code to give to the expression when it is built.
1856 Normally this is CODE, which is what the caller asked for,
1857 but in some special cases we change it. */
1858 register enum tree_code resultcode = code;
1860 /* Data type in which the computation is to be performed.
1861 In the simplest cases this is the common type of the arguments. */
1862 register tree result_type = NULL;
1864 /* Nonzero means operands have already been type-converted
1865 in whatever way is necessary.
1866 Zero means they need to be converted to RESULT_TYPE. */
1867 int converted = 0;
1869 /* Nonzero means create the expression with this type, rather than
1870 RESULT_TYPE. */
1871 tree build_type = 0;
1873 /* Nonzero means after finally constructing the expression
1874 convert it to this type. */
1875 tree final_type = 0;
1877 /* Nonzero if this is an operation like MIN or MAX which can
1878 safely be computed in short if both args are promoted shorts.
1879 Also implies COMMON.
1880 -1 indicates a bitwise operation; this makes a difference
1881 in the exact conditions for when it is safe to do the operation
1882 in a narrower mode. */
1883 int shorten = 0;
1885 /* Nonzero if this is a comparison operation;
1886 if both args are promoted shorts, compare the original shorts.
1887 Also implies COMMON. */
1888 int short_compare = 0;
1890 /* Nonzero if this is a right-shift operation, which can be computed on the
1891 original short and then promoted if the operand is a promoted short. */
1892 int short_shift = 0;
1894 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1895 int common = 0;
1897 if (convert_p)
1899 op0 = default_conversion (orig_op0);
1900 op1 = default_conversion (orig_op1);
1902 else
1904 op0 = orig_op0;
1905 op1 = orig_op1;
1908 type0 = TREE_TYPE (op0);
1909 type1 = TREE_TYPE (op1);
1911 /* The expression codes of the data types of the arguments tell us
1912 whether the arguments are integers, floating, pointers, etc. */
1913 code0 = TREE_CODE (type0);
1914 code1 = TREE_CODE (type1);
1916 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1917 STRIP_TYPE_NOPS (op0);
1918 STRIP_TYPE_NOPS (op1);
1920 /* If an error was already reported for one of the arguments,
1921 avoid reporting another error. */
1923 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1924 return error_mark_node;
1926 switch (code)
1928 case PLUS_EXPR:
1929 /* Handle the pointer + int case. */
1930 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1931 return pointer_int_sum (PLUS_EXPR, op0, op1);
1932 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1933 return pointer_int_sum (PLUS_EXPR, op1, op0);
1934 else
1935 common = 1;
1936 break;
1938 case MINUS_EXPR:
1939 /* Subtraction of two similar pointers.
1940 We must subtract them as integers, then divide by object size. */
1941 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1942 && comp_target_types (type0, type1))
1943 return pointer_diff (op0, op1);
1944 /* Handle pointer minus int. Just like pointer plus int. */
1945 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1946 return pointer_int_sum (MINUS_EXPR, op0, op1);
1947 else
1948 common = 1;
1949 break;
1951 case MULT_EXPR:
1952 common = 1;
1953 break;
1955 case TRUNC_DIV_EXPR:
1956 case CEIL_DIV_EXPR:
1957 case FLOOR_DIV_EXPR:
1958 case ROUND_DIV_EXPR:
1959 case EXACT_DIV_EXPR:
1960 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1961 || code0 == COMPLEX_TYPE)
1962 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1963 || code1 == COMPLEX_TYPE))
1965 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1966 resultcode = RDIV_EXPR;
1967 else
1969 /* Although it would be tempting to shorten always here, that
1970 loses on some targets, since the modulo instruction is
1971 undefined if the quotient can't be represented in the
1972 computation mode. We shorten only if unsigned or if
1973 dividing by something we know != -1. */
1974 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1975 || (TREE_CODE (op1) == INTEGER_CST
1976 && (TREE_INT_CST_LOW (op1) != -1
1977 || TREE_INT_CST_HIGH (op1) != -1)));
1979 common = 1;
1981 break;
1983 case BIT_AND_EXPR:
1984 case BIT_ANDTC_EXPR:
1985 case BIT_IOR_EXPR:
1986 case BIT_XOR_EXPR:
1987 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1988 shorten = -1;
1989 /* If one operand is a constant, and the other is a short type
1990 that has been converted to an int,
1991 really do the work in the short type and then convert the
1992 result to int. If we are lucky, the constant will be 0 or 1
1993 in the short type, making the entire operation go away. */
1994 if (TREE_CODE (op0) == INTEGER_CST
1995 && TREE_CODE (op1) == NOP_EXPR
1996 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
1997 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
1999 final_type = result_type;
2000 op1 = TREE_OPERAND (op1, 0);
2001 result_type = TREE_TYPE (op1);
2003 if (TREE_CODE (op1) == INTEGER_CST
2004 && TREE_CODE (op0) == NOP_EXPR
2005 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2006 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2008 final_type = result_type;
2009 op0 = TREE_OPERAND (op0, 0);
2010 result_type = TREE_TYPE (op0);
2012 break;
2014 case TRUNC_MOD_EXPR:
2015 case FLOOR_MOD_EXPR:
2016 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2018 /* Although it would be tempting to shorten always here, that loses
2019 on some targets, since the modulo instruction is undefined if the
2020 quotient can't be represented in the computation mode. We shorten
2021 only if unsigned or if dividing by something we know != -1. */
2022 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2023 || (TREE_CODE (op1) == INTEGER_CST
2024 && (TREE_INT_CST_LOW (op1) != -1
2025 || TREE_INT_CST_HIGH (op1) != -1)));
2026 common = 1;
2028 break;
2030 case TRUTH_ANDIF_EXPR:
2031 case TRUTH_ORIF_EXPR:
2032 case TRUTH_AND_EXPR:
2033 case TRUTH_OR_EXPR:
2034 case TRUTH_XOR_EXPR:
2035 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2036 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2037 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2038 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2040 /* Result of these operations is always an int,
2041 but that does not mean the operands should be
2042 converted to ints! */
2043 result_type = integer_type_node;
2044 op0 = truthvalue_conversion (op0);
2045 op1 = truthvalue_conversion (op1);
2046 converted = 1;
2048 break;
2050 /* Shift operations: result has same type as first operand;
2051 always convert second operand to int.
2052 Also set SHORT_SHIFT if shifting rightward. */
2054 case RSHIFT_EXPR:
2055 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2057 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2059 if (tree_int_cst_sgn (op1) < 0)
2060 warning ("right shift count is negative");
2061 else
2063 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2064 short_shift = 1;
2065 if (TREE_INT_CST_HIGH (op1) != 0
2066 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2067 >= TYPE_PRECISION (type0)))
2068 warning ("right shift count >= width of type");
2071 /* Use the type of the value to be shifted.
2072 This is what most traditional C compilers do. */
2073 result_type = type0;
2074 /* Unless traditional, convert the shift-count to an integer,
2075 regardless of size of value being shifted. */
2076 if (! flag_traditional)
2078 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2079 op1 = convert (integer_type_node, op1);
2080 /* Avoid converting op1 to result_type later. */
2081 converted = 1;
2084 break;
2086 case LSHIFT_EXPR:
2087 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2089 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2091 if (tree_int_cst_sgn (op1) < 0)
2092 warning ("left shift count is negative");
2093 else if (TREE_INT_CST_HIGH (op1) != 0
2094 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2095 >= TYPE_PRECISION (type0)))
2096 warning ("left shift count >= width of type");
2098 /* Use the type of the value to be shifted.
2099 This is what most traditional C compilers do. */
2100 result_type = type0;
2101 /* Unless traditional, convert the shift-count to an integer,
2102 regardless of size of value being shifted. */
2103 if (! flag_traditional)
2105 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2106 op1 = convert (integer_type_node, op1);
2107 /* Avoid converting op1 to result_type later. */
2108 converted = 1;
2111 break;
2113 case RROTATE_EXPR:
2114 case LROTATE_EXPR:
2115 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2117 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2119 if (tree_int_cst_sgn (op1) < 0)
2120 warning ("shift count is negative");
2121 else if (TREE_INT_CST_HIGH (op1) != 0
2122 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2123 >= TYPE_PRECISION (type0)))
2124 warning ("shift count >= width of type");
2126 /* Use the type of the value to be shifted.
2127 This is what most traditional C compilers do. */
2128 result_type = type0;
2129 /* Unless traditional, convert the shift-count to an integer,
2130 regardless of size of value being shifted. */
2131 if (! flag_traditional)
2133 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2134 op1 = convert (integer_type_node, op1);
2135 /* Avoid converting op1 to result_type later. */
2136 converted = 1;
2139 break;
2141 case EQ_EXPR:
2142 case NE_EXPR:
2143 /* Result of comparison is always int,
2144 but don't convert the args to int! */
2145 build_type = integer_type_node;
2146 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2147 || code0 == COMPLEX_TYPE)
2148 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2149 || code1 == COMPLEX_TYPE))
2150 short_compare = 1;
2151 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2153 register tree tt0 = TREE_TYPE (type0);
2154 register tree tt1 = TREE_TYPE (type1);
2155 /* Anything compares with void *. void * compares with anything.
2156 Otherwise, the targets must be compatible
2157 and both must be object or both incomplete. */
2158 if (comp_target_types (type0, type1))
2159 result_type = common_type (type0, type1);
2160 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2162 /* op0 != orig_op0 detects the case of something
2163 whose value is 0 but which isn't a valid null ptr const. */
2164 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2165 && TREE_CODE (tt1) == FUNCTION_TYPE)
2166 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2168 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2170 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2171 && TREE_CODE (tt0) == FUNCTION_TYPE)
2172 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2174 else
2175 pedwarn ("comparison of distinct pointer types lacks a cast");
2177 if (result_type == NULL_TREE)
2178 result_type = ptr_type_node;
2180 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2181 && integer_zerop (op1))
2182 result_type = type0;
2183 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2184 && integer_zerop (op0))
2185 result_type = type1;
2186 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2188 result_type = type0;
2189 if (! flag_traditional)
2190 pedwarn ("comparison between pointer and integer");
2192 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2194 result_type = type1;
2195 if (! flag_traditional)
2196 pedwarn ("comparison between pointer and integer");
2198 break;
2200 case MAX_EXPR:
2201 case MIN_EXPR:
2202 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2203 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2204 shorten = 1;
2205 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2207 if (comp_target_types (type0, type1))
2209 result_type = common_type (type0, type1);
2210 if (pedantic
2211 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2212 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2214 else
2216 result_type = ptr_type_node;
2217 pedwarn ("comparison of distinct pointer types lacks a cast");
2220 break;
2222 case LE_EXPR:
2223 case GE_EXPR:
2224 case LT_EXPR:
2225 case GT_EXPR:
2226 build_type = integer_type_node;
2227 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2228 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2229 short_compare = 1;
2230 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2232 if (comp_target_types (type0, type1))
2234 result_type = common_type (type0, type1);
2235 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2236 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2237 pedwarn ("comparison of complete and incomplete pointers");
2238 else if (pedantic
2239 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2240 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2242 else
2244 result_type = ptr_type_node;
2245 pedwarn ("comparison of distinct pointer types lacks a cast");
2248 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2249 && integer_zerop (op1))
2251 result_type = type0;
2252 if (pedantic || extra_warnings)
2253 pedwarn ("ordered comparison of pointer with integer zero");
2255 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2256 && integer_zerop (op0))
2258 result_type = type1;
2259 if (pedantic)
2260 pedwarn ("ordered comparison of pointer with integer zero");
2262 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2264 result_type = type0;
2265 if (! flag_traditional)
2266 pedwarn ("comparison between pointer and integer");
2268 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2270 result_type = type1;
2271 if (! flag_traditional)
2272 pedwarn ("comparison between pointer and integer");
2274 break;
2276 default:
2277 break;
2280 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2282 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2284 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2286 if (shorten || common || short_compare)
2287 result_type = common_type (type0, type1);
2289 /* For certain operations (which identify themselves by shorten != 0)
2290 if both args were extended from the same smaller type,
2291 do the arithmetic in that type and then extend.
2293 shorten !=0 and !=1 indicates a bitwise operation.
2294 For them, this optimization is safe only if
2295 both args are zero-extended or both are sign-extended.
2296 Otherwise, we might change the result.
2297 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2298 but calculated in (unsigned short) it would be (unsigned short)-1. */
2300 if (shorten && none_complex)
2302 int unsigned0, unsigned1;
2303 tree arg0 = get_narrower (op0, &unsigned0);
2304 tree arg1 = get_narrower (op1, &unsigned1);
2305 /* UNS is 1 if the operation to be done is an unsigned one. */
2306 int uns = TREE_UNSIGNED (result_type);
2307 tree type;
2309 final_type = result_type;
2311 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2312 but it *requires* conversion to FINAL_TYPE. */
2314 if ((TYPE_PRECISION (TREE_TYPE (op0))
2315 == TYPE_PRECISION (TREE_TYPE (arg0)))
2316 && TREE_TYPE (op0) != final_type)
2317 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2318 if ((TYPE_PRECISION (TREE_TYPE (op1))
2319 == TYPE_PRECISION (TREE_TYPE (arg1)))
2320 && TREE_TYPE (op1) != final_type)
2321 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2323 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2325 /* For bitwise operations, signedness of nominal type
2326 does not matter. Consider only how operands were extended. */
2327 if (shorten == -1)
2328 uns = unsigned0;
2330 /* Note that in all three cases below we refrain from optimizing
2331 an unsigned operation on sign-extended args.
2332 That would not be valid. */
2334 /* Both args variable: if both extended in same way
2335 from same width, do it in that width.
2336 Do it unsigned if args were zero-extended. */
2337 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2338 < TYPE_PRECISION (result_type))
2339 && (TYPE_PRECISION (TREE_TYPE (arg1))
2340 == TYPE_PRECISION (TREE_TYPE (arg0)))
2341 && unsigned0 == unsigned1
2342 && (unsigned0 || !uns))
2343 result_type
2344 = signed_or_unsigned_type (unsigned0,
2345 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2346 else if (TREE_CODE (arg0) == INTEGER_CST
2347 && (unsigned1 || !uns)
2348 && (TYPE_PRECISION (TREE_TYPE (arg1))
2349 < TYPE_PRECISION (result_type))
2350 && (type = signed_or_unsigned_type (unsigned1,
2351 TREE_TYPE (arg1)),
2352 int_fits_type_p (arg0, type)))
2353 result_type = type;
2354 else if (TREE_CODE (arg1) == INTEGER_CST
2355 && (unsigned0 || !uns)
2356 && (TYPE_PRECISION (TREE_TYPE (arg0))
2357 < TYPE_PRECISION (result_type))
2358 && (type = signed_or_unsigned_type (unsigned0,
2359 TREE_TYPE (arg0)),
2360 int_fits_type_p (arg1, type)))
2361 result_type = type;
2364 /* Shifts can be shortened if shifting right. */
2366 if (short_shift)
2368 int unsigned_arg;
2369 tree arg0 = get_narrower (op0, &unsigned_arg);
2371 final_type = result_type;
2373 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2374 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2376 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2377 /* We can shorten only if the shift count is less than the
2378 number of bits in the smaller type size. */
2379 && TREE_INT_CST_HIGH (op1) == 0
2380 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2381 /* If arg is sign-extended and then unsigned-shifted,
2382 we can simulate this with a signed shift in arg's type
2383 only if the extended result is at least twice as wide
2384 as the arg. Otherwise, the shift could use up all the
2385 ones made by sign-extension and bring in zeros.
2386 We can't optimize that case at all, but in most machines
2387 it never happens because available widths are 2**N. */
2388 && (!TREE_UNSIGNED (final_type)
2389 || unsigned_arg
2390 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2392 /* Do an unsigned shift if the operand was zero-extended. */
2393 result_type
2394 = signed_or_unsigned_type (unsigned_arg,
2395 TREE_TYPE (arg0));
2396 /* Convert value-to-be-shifted to that type. */
2397 if (TREE_TYPE (op0) != result_type)
2398 op0 = convert (result_type, op0);
2399 converted = 1;
2403 /* Comparison operations are shortened too but differently.
2404 They identify themselves by setting short_compare = 1. */
2406 if (short_compare)
2408 /* Don't write &op0, etc., because that would prevent op0
2409 from being kept in a register.
2410 Instead, make copies of the our local variables and
2411 pass the copies by reference, then copy them back afterward. */
2412 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2413 enum tree_code xresultcode = resultcode;
2414 tree val
2415 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2416 if (val != 0)
2417 return val;
2418 op0 = xop0, op1 = xop1;
2419 converted = 1;
2420 resultcode = xresultcode;
2422 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2423 && skip_evaluation == 0)
2425 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2426 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2428 int unsignedp0, unsignedp1;
2429 tree primop0 = get_narrower (op0, &unsignedp0);
2430 tree primop1 = get_narrower (op1, &unsignedp1);
2432 /* Avoid spurious warnings for comparison with enumerators. */
2434 xop0 = orig_op0;
2435 xop1 = orig_op1;
2436 STRIP_TYPE_NOPS (xop0);
2437 STRIP_TYPE_NOPS (xop1);
2439 /* Give warnings for comparisons between signed and unsigned
2440 quantities that may fail. */
2441 /* Do the checking based on the original operand trees, so that
2442 casts will be considered, but default promotions won't be. */
2444 /* Do not warn if the comparison is being done in a signed type,
2445 since the signed type will only be chosen if it can represent
2446 all the values of the unsigned type. */
2447 if (! TREE_UNSIGNED (result_type))
2448 /* OK */;
2449 /* Do not warn if both operands are unsigned. */
2450 else if (op0_signed == op1_signed)
2451 /* OK */;
2452 /* Do not warn if the signed quantity is an unsuffixed
2453 integer literal (or some static constant expression
2454 involving such literals) and it is non-negative. */
2455 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2456 && tree_int_cst_sgn (xop0) >= 0)
2457 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2458 && tree_int_cst_sgn (xop1) >= 0))
2459 /* OK */;
2460 /* Do not warn if the comparison is an equality operation,
2461 the unsigned quantity is an integral constant and it does
2462 not use the most significant bit of result_type. */
2463 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2464 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2465 && int_fits_type_p (xop1, signed_type (result_type)))
2466 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2467 && int_fits_type_p (xop0, signed_type (result_type)))))
2468 /* OK */;
2469 else
2470 warning ("comparison between signed and unsigned");
2472 /* Warn if two unsigned values are being compared in a size
2473 larger than their original size, and one (and only one) is the
2474 result of a `~' operator. This comparison will always fail.
2476 Also warn if one operand is a constant, and the constant
2477 does not have all bits set that are set in the ~ operand
2478 when it is extended. */
2480 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2481 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2483 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2484 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2485 &unsignedp0);
2486 else
2487 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2488 &unsignedp1);
2490 if (TREE_CODE (primop0) == INTEGER_CST
2491 || TREE_CODE (primop1) == INTEGER_CST)
2493 tree primop;
2494 long constant, mask;
2495 int unsignedp, bits;
2497 if (TREE_CODE (primop0) == INTEGER_CST)
2499 primop = primop1;
2500 unsignedp = unsignedp1;
2501 constant = TREE_INT_CST_LOW (primop0);
2503 else
2505 primop = primop0;
2506 unsignedp = unsignedp0;
2507 constant = TREE_INT_CST_LOW (primop1);
2510 bits = TYPE_PRECISION (TREE_TYPE (primop));
2511 if (bits < TYPE_PRECISION (result_type)
2512 && bits < HOST_BITS_PER_LONG && unsignedp)
2514 mask = (~0L) << bits;
2515 if ((mask & constant) != mask)
2516 warning ("comparison of promoted ~unsigned with constant");
2519 else if (unsignedp0 && unsignedp1
2520 && (TYPE_PRECISION (TREE_TYPE (primop0))
2521 < TYPE_PRECISION (result_type))
2522 && (TYPE_PRECISION (TREE_TYPE (primop1))
2523 < TYPE_PRECISION (result_type)))
2524 warning ("comparison of promoted ~unsigned with unsigned");
2530 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2531 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2532 Then the expression will be built.
2533 It will be given type FINAL_TYPE if that is nonzero;
2534 otherwise, it will be given type RESULT_TYPE. */
2536 if (!result_type)
2538 binary_op_error (code);
2539 return error_mark_node;
2542 if (! converted)
2544 if (TREE_TYPE (op0) != result_type)
2545 op0 = convert (result_type, op0);
2546 if (TREE_TYPE (op1) != result_type)
2547 op1 = convert (result_type, op1);
2550 if (build_type == NULL_TREE)
2551 build_type = result_type;
2554 register tree result = build (resultcode, build_type, op0, op1);
2555 register tree folded;
2557 folded = fold (result);
2558 if (folded == result)
2559 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2560 if (final_type != 0)
2561 return convert (final_type, folded);
2562 return folded;
2566 /* Return a tree for the sum or difference (RESULTCODE says which)
2567 of pointer PTROP and integer INTOP. */
2569 static tree
2570 pointer_int_sum (resultcode, ptrop, intop)
2571 enum tree_code resultcode;
2572 register tree ptrop, intop;
2574 tree size_exp;
2576 register tree result;
2577 register tree folded;
2579 /* The result is a pointer of the same type that is being added. */
2581 register tree result_type = TREE_TYPE (ptrop);
2583 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2585 if (pedantic || warn_pointer_arith)
2586 pedwarn ("pointer of type `void *' used in arithmetic");
2587 size_exp = integer_one_node;
2589 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2591 if (pedantic || warn_pointer_arith)
2592 pedwarn ("pointer to a function used in arithmetic");
2593 size_exp = integer_one_node;
2595 else
2596 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2598 /* If what we are about to multiply by the size of the elements
2599 contains a constant term, apply distributive law
2600 and multiply that constant term separately.
2601 This helps produce common subexpressions. */
2603 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2604 && ! TREE_CONSTANT (intop)
2605 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2606 && TREE_CONSTANT (size_exp)
2607 /* If the constant comes from pointer subtraction,
2608 skip this optimization--it would cause an error. */
2609 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2610 /* If the constant is unsigned, and smaller than the pointer size,
2611 then we must skip this optimization. This is because it could cause
2612 an overflow error if the constant is negative but INTOP is not. */
2613 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2614 || (TYPE_PRECISION (TREE_TYPE (intop))
2615 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2617 enum tree_code subcode = resultcode;
2618 tree int_type = TREE_TYPE (intop);
2619 if (TREE_CODE (intop) == MINUS_EXPR)
2620 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2621 /* Convert both subexpression types to the type of intop,
2622 because weird cases involving pointer arithmetic
2623 can result in a sum or difference with different type args. */
2624 ptrop = build_binary_op (subcode, ptrop,
2625 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2626 intop = convert (int_type, TREE_OPERAND (intop, 0));
2629 /* Convert the integer argument to a type the same size as sizetype
2630 so the multiply won't overflow spuriously. */
2632 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2633 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2634 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2635 TREE_UNSIGNED (sizetype)), intop);
2637 /* Replace the integer argument with a suitable product by the object size.
2638 Do this multiplication as signed, then convert to the appropriate
2639 pointer type (actually unsigned integral). */
2641 intop = convert (result_type,
2642 build_binary_op (MULT_EXPR, intop,
2643 convert (TREE_TYPE (intop), size_exp), 1));
2645 /* Create the sum or difference. */
2647 result = build (resultcode, result_type, ptrop, intop);
2649 folded = fold (result);
2650 if (folded == result)
2651 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2652 return folded;
2655 /* Return a tree for the difference of pointers OP0 and OP1.
2656 The resulting tree has type int. */
2658 static tree
2659 pointer_diff (op0, op1)
2660 register tree op0, op1;
2662 register tree result, folded;
2663 tree restype = ptrdiff_type_node;
2665 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2667 if (pedantic || warn_pointer_arith)
2669 if (TREE_CODE (target_type) == VOID_TYPE)
2670 pedwarn ("pointer of type `void *' used in subtraction");
2671 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2672 pedwarn ("pointer to a function used in subtraction");
2675 /* First do the subtraction as integers;
2676 then drop through to build the divide operator.
2677 Do not do default conversions on the minus operator
2678 in case restype is a short type. */
2680 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2681 convert (restype, op1), 0);
2682 /* This generates an error if op1 is pointer to incomplete type. */
2683 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2684 error ("arithmetic on pointer to an incomplete type");
2686 /* This generates an error if op0 is pointer to incomplete type. */
2687 op1 = c_size_in_bytes (target_type);
2689 /* Divide by the size, in easiest possible way. */
2691 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2693 folded = fold (result);
2694 if (folded == result)
2695 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2696 return folded;
2699 /* Construct and perhaps optimize a tree representation
2700 for a unary operation. CODE, a tree_code, specifies the operation
2701 and XARG is the operand. NOCONVERT nonzero suppresses
2702 the default promotions (such as from short to int). */
2704 tree
2705 build_unary_op (code, xarg, noconvert)
2706 enum tree_code code;
2707 tree xarg;
2708 int noconvert;
2710 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2711 register tree arg = xarg;
2712 register tree argtype = 0;
2713 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2714 tree val;
2716 if (typecode == ERROR_MARK)
2717 return error_mark_node;
2718 if (typecode == ENUMERAL_TYPE)
2719 typecode = INTEGER_TYPE;
2721 switch (code)
2723 case CONVERT_EXPR:
2724 /* This is used for unary plus, because a CONVERT_EXPR
2725 is enough to prevent anybody from looking inside for
2726 associativity, but won't generate any code. */
2727 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2728 || typecode == COMPLEX_TYPE))
2730 error ("wrong type argument to unary plus");
2731 return error_mark_node;
2733 else if (!noconvert)
2734 arg = default_conversion (arg);
2735 break;
2737 case NEGATE_EXPR:
2738 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2739 || typecode == COMPLEX_TYPE))
2741 error ("wrong type argument to unary minus");
2742 return error_mark_node;
2744 else if (!noconvert)
2745 arg = default_conversion (arg);
2746 break;
2748 case BIT_NOT_EXPR:
2749 if (typecode == COMPLEX_TYPE)
2751 code = CONJ_EXPR;
2752 if (!noconvert)
2753 arg = default_conversion (arg);
2755 else if (typecode != INTEGER_TYPE)
2757 error ("wrong type argument to bit-complement");
2758 return error_mark_node;
2760 else if (!noconvert)
2761 arg = default_conversion (arg);
2762 break;
2764 case ABS_EXPR:
2765 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2766 || typecode == COMPLEX_TYPE))
2768 error ("wrong type argument to abs");
2769 return error_mark_node;
2771 else if (!noconvert)
2772 arg = default_conversion (arg);
2773 break;
2775 case CONJ_EXPR:
2776 /* Conjugating a real value is a no-op, but allow it anyway. */
2777 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2778 || typecode == COMPLEX_TYPE))
2780 error ("wrong type argument to conjugation");
2781 return error_mark_node;
2783 else if (!noconvert)
2784 arg = default_conversion (arg);
2785 break;
2787 case TRUTH_NOT_EXPR:
2788 if (typecode != INTEGER_TYPE
2789 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2790 && typecode != COMPLEX_TYPE
2791 /* These will convert to a pointer. */
2792 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2794 error ("wrong type argument to unary exclamation mark");
2795 return error_mark_node;
2797 arg = truthvalue_conversion (arg);
2798 return invert_truthvalue (arg);
2800 case NOP_EXPR:
2801 break;
2803 case REALPART_EXPR:
2804 if (TREE_CODE (arg) == COMPLEX_CST)
2805 return TREE_REALPART (arg);
2806 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2807 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2808 else
2809 return arg;
2811 case IMAGPART_EXPR:
2812 if (TREE_CODE (arg) == COMPLEX_CST)
2813 return TREE_IMAGPART (arg);
2814 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2815 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2816 else
2817 return convert (TREE_TYPE (arg), integer_zero_node);
2819 case PREINCREMENT_EXPR:
2820 case POSTINCREMENT_EXPR:
2821 case PREDECREMENT_EXPR:
2822 case POSTDECREMENT_EXPR:
2823 /* Handle complex lvalues (when permitted)
2824 by reduction to simpler cases. */
2826 val = unary_complex_lvalue (code, arg);
2827 if (val != 0)
2828 return val;
2830 /* Increment or decrement the real part of the value,
2831 and don't change the imaginary part. */
2832 if (typecode == COMPLEX_TYPE)
2834 tree real, imag;
2836 arg = stabilize_reference (arg);
2837 real = build_unary_op (REALPART_EXPR, arg, 1);
2838 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2839 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2840 build_unary_op (code, real, 1), imag);
2843 /* Report invalid types. */
2845 if (typecode != POINTER_TYPE
2846 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2848 error (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2849 ? "wrong type argument to increment"
2850 : "wrong type argument to decrement");
2851 return error_mark_node;
2855 register tree inc;
2856 tree result_type = TREE_TYPE (arg);
2858 arg = get_unwidened (arg, 0);
2859 argtype = TREE_TYPE (arg);
2861 /* Compute the increment. */
2863 if (typecode == POINTER_TYPE)
2865 /* If pointer target is an undefined struct,
2866 we just cannot know how to do the arithmetic. */
2867 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2868 error (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2869 ? "increment of pointer to unknown structure"
2870 : "decrement of pointer to unknown structure");
2871 else if ((pedantic || warn_pointer_arith)
2872 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2873 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2874 pedwarn (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2875 ? "wrong type argument to increment"
2876 : "wrong type argument to decrement");
2877 inc = c_size_in_bytes (TREE_TYPE (result_type));
2879 else
2880 inc = integer_one_node;
2882 inc = convert (argtype, inc);
2884 /* Handle incrementing a cast-expression. */
2886 while (1)
2887 switch (TREE_CODE (arg))
2889 case NOP_EXPR:
2890 case CONVERT_EXPR:
2891 case FLOAT_EXPR:
2892 case FIX_TRUNC_EXPR:
2893 case FIX_FLOOR_EXPR:
2894 case FIX_ROUND_EXPR:
2895 case FIX_CEIL_EXPR:
2896 pedantic_lvalue_warning (CONVERT_EXPR);
2897 /* If the real type has the same machine representation
2898 as the type it is cast to, we can make better output
2899 by adding directly to the inside of the cast. */
2900 if ((TREE_CODE (TREE_TYPE (arg))
2901 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2902 && (TYPE_MODE (TREE_TYPE (arg))
2903 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2904 arg = TREE_OPERAND (arg, 0);
2905 else
2907 tree incremented, modify, value;
2908 arg = stabilize_reference (arg);
2909 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2910 value = arg;
2911 else
2912 value = save_expr (arg);
2913 incremented = build (((code == PREINCREMENT_EXPR
2914 || code == POSTINCREMENT_EXPR)
2915 ? PLUS_EXPR : MINUS_EXPR),
2916 argtype, value, inc);
2917 TREE_SIDE_EFFECTS (incremented) = 1;
2918 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2919 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2920 TREE_USED (value) = 1;
2921 return value;
2923 break;
2925 default:
2926 goto give_up;
2928 give_up:
2930 /* Complain about anything else that is not a true lvalue. */
2931 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2932 || code == POSTINCREMENT_EXPR)
2933 ? "invalid lvalue in increment"
2934 : "invalid lvalue in decrement")))
2935 return error_mark_node;
2937 /* Report a read-only lvalue. */
2938 if (TREE_READONLY (arg))
2939 readonly_warning (arg,
2940 ((code == PREINCREMENT_EXPR
2941 || code == POSTINCREMENT_EXPR)
2942 ? "increment" : "decrement"));
2944 val = build (code, TREE_TYPE (arg), arg, inc);
2945 TREE_SIDE_EFFECTS (val) = 1;
2946 val = convert (result_type, val);
2947 if (TREE_CODE (val) != code)
2948 TREE_NO_UNUSED_WARNING (val) = 1;
2949 return val;
2952 case ADDR_EXPR:
2953 /* Note that this operation never does default_conversion
2954 regardless of NOCONVERT. */
2956 /* Let &* cancel out to simplify resulting code. */
2957 if (TREE_CODE (arg) == INDIRECT_REF)
2959 /* Don't let this be an lvalue. */
2960 if (lvalue_p (TREE_OPERAND (arg, 0)))
2961 return non_lvalue (TREE_OPERAND (arg, 0));
2962 return TREE_OPERAND (arg, 0);
2965 /* For &x[y], return x+y */
2966 if (TREE_CODE (arg) == ARRAY_REF)
2968 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2969 return error_mark_node;
2970 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2971 TREE_OPERAND (arg, 1), 1);
2974 /* Handle complex lvalues (when permitted)
2975 by reduction to simpler cases. */
2976 val = unary_complex_lvalue (code, arg);
2977 if (val != 0)
2978 return val;
2980 #if 0 /* Turned off because inconsistent;
2981 float f; *&(int)f = 3.4 stores in int format
2982 whereas (int)f = 3.4 stores in float format. */
2983 /* Address of a cast is just a cast of the address
2984 of the operand of the cast. */
2985 switch (TREE_CODE (arg))
2987 case NOP_EXPR:
2988 case CONVERT_EXPR:
2989 case FLOAT_EXPR:
2990 case FIX_TRUNC_EXPR:
2991 case FIX_FLOOR_EXPR:
2992 case FIX_ROUND_EXPR:
2993 case FIX_CEIL_EXPR:
2994 if (pedantic)
2995 pedwarn ("ANSI C forbids the address of a cast expression");
2996 return convert (build_pointer_type (TREE_TYPE (arg)),
2997 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
2998 0));
3000 #endif
3002 /* Allow the address of a constructor if all the elements
3003 are constant. */
3004 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3006 /* Anything not already handled and not a true memory reference
3007 is an error. */
3008 else if (typecode != FUNCTION_TYPE
3009 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3010 return error_mark_node;
3012 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3013 argtype = TREE_TYPE (arg);
3014 /* If the lvalue is const or volatile, merge that into the type
3015 to which the address will point. Note that you can't get a
3016 restricted pointer by taking the address of something, so we
3017 only have to deal with `const' and `volatile' here. */
3018 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
3019 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3021 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3022 argtype = c_build_type_variant (argtype,
3023 TREE_READONLY (arg),
3024 TREE_THIS_VOLATILE (arg));
3027 argtype = build_pointer_type (argtype);
3029 if (mark_addressable (arg) == 0)
3030 return error_mark_node;
3033 tree addr;
3035 if (TREE_CODE (arg) == COMPONENT_REF)
3037 tree field = TREE_OPERAND (arg, 1);
3039 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3041 if (DECL_C_BIT_FIELD (field))
3043 error ("attempt to take address of bit-field structure member `%s'",
3044 IDENTIFIER_POINTER (DECL_NAME (field)));
3045 return error_mark_node;
3048 addr = convert (argtype, addr);
3050 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3052 tree offset
3053 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3054 size_int (BITS_PER_UNIT));
3055 int flag = TREE_CONSTANT (addr);
3056 addr = fold (build (PLUS_EXPR, argtype,
3057 addr, convert (argtype, offset)));
3058 TREE_CONSTANT (addr) = flag;
3061 else
3062 addr = build1 (code, argtype, arg);
3064 /* Address of a static or external variable or
3065 file-scope function counts as a constant. */
3066 if (staticp (arg)
3067 && ! (TREE_CODE (arg) == FUNCTION_DECL
3068 && DECL_CONTEXT (arg) != 0))
3069 TREE_CONSTANT (addr) = 1;
3070 return addr;
3073 default:
3074 break;
3077 if (argtype == 0)
3078 argtype = TREE_TYPE (arg);
3079 return fold (build1 (code, argtype, arg));
3082 #if 0
3083 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3084 convert ARG with the same conversions in the same order
3085 and return the result. */
3087 static tree
3088 convert_sequence (conversions, arg)
3089 tree conversions;
3090 tree arg;
3092 switch (TREE_CODE (conversions))
3094 case NOP_EXPR:
3095 case CONVERT_EXPR:
3096 case FLOAT_EXPR:
3097 case FIX_TRUNC_EXPR:
3098 case FIX_FLOOR_EXPR:
3099 case FIX_ROUND_EXPR:
3100 case FIX_CEIL_EXPR:
3101 return convert (TREE_TYPE (conversions),
3102 convert_sequence (TREE_OPERAND (conversions, 0),
3103 arg));
3105 default:
3106 return arg;
3109 #endif /* 0 */
3111 /* Return nonzero if REF is an lvalue valid for this language.
3112 Lvalues can be assigned, unless their type has TYPE_READONLY.
3113 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3116 lvalue_p (ref)
3117 tree ref;
3119 register enum tree_code code = TREE_CODE (ref);
3121 switch (code)
3123 case REALPART_EXPR:
3124 case IMAGPART_EXPR:
3125 case COMPONENT_REF:
3126 return lvalue_p (TREE_OPERAND (ref, 0));
3128 case STRING_CST:
3129 return 1;
3131 case INDIRECT_REF:
3132 case ARRAY_REF:
3133 case VAR_DECL:
3134 case PARM_DECL:
3135 case RESULT_DECL:
3136 case ERROR_MARK:
3137 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3138 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3140 case BIND_EXPR:
3141 case RTL_EXPR:
3142 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3144 default:
3145 return 0;
3149 /* Return nonzero if REF is an lvalue valid for this language;
3150 otherwise, print an error message and return zero. */
3153 lvalue_or_else (ref, msgid)
3154 tree ref;
3155 const char *msgid;
3157 int win = lvalue_p (ref);
3158 if (! win)
3159 error (msgid);
3160 return win;
3163 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3164 for certain kinds of expressions which are not really lvalues
3165 but which we can accept as lvalues.
3167 If ARG is not a kind of expression we can handle, return zero. */
3169 static tree
3170 unary_complex_lvalue (code, arg)
3171 enum tree_code code;
3172 tree arg;
3174 /* Handle (a, b) used as an "lvalue". */
3175 if (TREE_CODE (arg) == COMPOUND_EXPR)
3177 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3179 /* If this returns a function type, it isn't really being used as
3180 an lvalue, so don't issue a warning about it. */
3181 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3182 pedantic_lvalue_warning (COMPOUND_EXPR);
3184 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3185 TREE_OPERAND (arg, 0), real_result);
3188 /* Handle (a ? b : c) used as an "lvalue". */
3189 if (TREE_CODE (arg) == COND_EXPR)
3191 pedantic_lvalue_warning (COND_EXPR);
3192 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3193 pedantic_lvalue_warning (COMPOUND_EXPR);
3195 return (build_conditional_expr
3196 (TREE_OPERAND (arg, 0),
3197 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3198 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3201 return 0;
3204 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3205 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3207 static void
3208 pedantic_lvalue_warning (code)
3209 enum tree_code code;
3211 if (pedantic)
3212 pedwarn (code == COND_EXPR
3213 ? "ANSI C forbids use of conditional expressions as lvalues"
3214 : code == COMPOUND_EXPR
3215 ? "ANSI C forbids use of compound expressions as lvalues"
3216 : "ANSI C forbids use of cast expressions as lvalues");
3219 /* Warn about storing in something that is `const'. */
3221 void
3222 readonly_warning (arg, msgid)
3223 tree arg;
3224 const char *msgid;
3226 /* Forbid assignments to iterators. */
3227 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3228 pedwarn ("%s of iterator `%s'", _(msgid),
3229 IDENTIFIER_POINTER (DECL_NAME (arg)));
3231 if (TREE_CODE (arg) == COMPONENT_REF)
3233 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3234 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3235 else
3236 pedwarn ("%s of read-only member `%s'", _(msgid),
3237 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3239 else if (TREE_CODE (arg) == VAR_DECL)
3240 pedwarn ("%s of read-only variable `%s'", _(msgid),
3241 IDENTIFIER_POINTER (DECL_NAME (arg)));
3242 else
3243 pedwarn ("%s of read-only location", _(msgid));
3246 /* Mark EXP saying that we need to be able to take the
3247 address of it; it should not be allocated in a register.
3248 Value is 1 if successful. */
3251 mark_addressable (exp)
3252 tree exp;
3254 register tree x = exp;
3255 while (1)
3256 switch (TREE_CODE (x))
3258 case COMPONENT_REF:
3259 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3261 error ("cannot take address of bitfield `%s'",
3262 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3263 return 0;
3266 /* ... fall through ... */
3268 case ADDR_EXPR:
3269 case ARRAY_REF:
3270 case REALPART_EXPR:
3271 case IMAGPART_EXPR:
3272 x = TREE_OPERAND (x, 0);
3273 break;
3275 case CONSTRUCTOR:
3276 TREE_ADDRESSABLE (x) = 1;
3277 return 1;
3279 case VAR_DECL:
3280 case CONST_DECL:
3281 case PARM_DECL:
3282 case RESULT_DECL:
3283 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3284 && DECL_NONLOCAL (x))
3286 if (TREE_PUBLIC (x))
3288 error ("global register variable `%s' used in nested function",
3289 IDENTIFIER_POINTER (DECL_NAME (x)));
3290 return 0;
3292 pedwarn ("register variable `%s' used in nested function",
3293 IDENTIFIER_POINTER (DECL_NAME (x)));
3295 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3297 if (TREE_PUBLIC (x))
3299 error ("address of global register variable `%s' requested",
3300 IDENTIFIER_POINTER (DECL_NAME (x)));
3301 return 0;
3304 /* If we are making this addressable due to its having
3305 volatile components, give a different error message. Also
3306 handle the case of an unnamed parameter by not trying
3307 to give the name. */
3309 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3311 error ("cannot put object with volatile field into register");
3312 return 0;
3315 pedwarn ("address of register variable `%s' requested",
3316 IDENTIFIER_POINTER (DECL_NAME (x)));
3318 put_var_into_stack (x);
3320 /* drops in */
3321 case FUNCTION_DECL:
3322 TREE_ADDRESSABLE (x) = 1;
3323 #if 0 /* poplevel deals with this now. */
3324 if (DECL_CONTEXT (x) == 0)
3325 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3326 #endif
3328 default:
3329 return 1;
3333 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3335 tree
3336 build_conditional_expr (ifexp, op1, op2)
3337 tree ifexp, op1, op2;
3339 register tree type1;
3340 register tree type2;
3341 register enum tree_code code1;
3342 register enum tree_code code2;
3343 register tree result_type = NULL;
3344 tree orig_op1 = op1, orig_op2 = op2;
3346 ifexp = truthvalue_conversion (default_conversion (ifexp));
3348 #if 0 /* Produces wrong result if within sizeof. */
3349 /* Don't promote the operands separately if they promote
3350 the same way. Return the unpromoted type and let the combined
3351 value get promoted if necessary. */
3353 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3354 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3355 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3356 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3358 if (TREE_CODE (ifexp) == INTEGER_CST)
3359 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3361 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3363 #endif
3365 /* Promote both alternatives. */
3367 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3368 op1 = default_conversion (op1);
3369 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3370 op2 = default_conversion (op2);
3372 if (TREE_CODE (ifexp) == ERROR_MARK
3373 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3374 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3375 return error_mark_node;
3377 type1 = TREE_TYPE (op1);
3378 code1 = TREE_CODE (type1);
3379 type2 = TREE_TYPE (op2);
3380 code2 = TREE_CODE (type2);
3382 /* Quickly detect the usual case where op1 and op2 have the same type
3383 after promotion. */
3384 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3386 if (type1 == type2)
3387 result_type = type1;
3388 else
3389 result_type = TYPE_MAIN_VARIANT (type1);
3391 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3392 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3394 result_type = common_type (type1, type2);
3396 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3398 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3399 pedwarn ("ANSI C forbids conditional expr with only one void side");
3400 result_type = void_type_node;
3402 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3404 if (comp_target_types (type1, type2))
3405 result_type = common_type (type1, type2);
3406 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3407 && TREE_CODE (orig_op1) != NOP_EXPR)
3408 result_type = qualify_type (type2, type1);
3409 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3410 && TREE_CODE (orig_op2) != NOP_EXPR)
3411 result_type = qualify_type (type1, type2);
3412 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3414 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3415 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3416 result_type = qualify_type (type1, type2);
3418 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3420 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3421 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3422 result_type = qualify_type (type2, type1);
3424 else
3426 pedwarn ("pointer type mismatch in conditional expression");
3427 result_type = build_pointer_type (void_type_node);
3430 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3432 if (! integer_zerop (op2))
3433 pedwarn ("pointer/integer type mismatch in conditional expression");
3434 else
3436 op2 = null_pointer_node;
3437 #if 0 /* The spec seems to say this is permitted. */
3438 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3439 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3440 #endif
3442 result_type = type1;
3444 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3446 if (!integer_zerop (op1))
3447 pedwarn ("pointer/integer type mismatch in conditional expression");
3448 else
3450 op1 = null_pointer_node;
3451 #if 0 /* The spec seems to say this is permitted. */
3452 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3453 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3454 #endif
3456 result_type = type2;
3459 if (!result_type)
3461 if (flag_cond_mismatch)
3462 result_type = void_type_node;
3463 else
3465 error ("type mismatch in conditional expression");
3466 return error_mark_node;
3470 /* Merge const and volatile flags of the incoming types. */
3471 result_type
3472 = build_type_variant (result_type,
3473 TREE_READONLY (op1) || TREE_READONLY (op2),
3474 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3476 if (result_type != TREE_TYPE (op1))
3477 op1 = convert_and_check (result_type, op1);
3478 if (result_type != TREE_TYPE (op2))
3479 op2 = convert_and_check (result_type, op2);
3481 #if 0
3482 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3484 result_type = TREE_TYPE (op1);
3485 if (TREE_CONSTANT (ifexp))
3486 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3488 if (TYPE_MODE (result_type) == BLKmode)
3490 register tree tempvar
3491 = build_decl (VAR_DECL, NULL_TREE, result_type);
3492 register tree xop1 = build_modify_expr (tempvar, op1);
3493 register tree xop2 = build_modify_expr (tempvar, op2);
3494 register tree result = fold (build (COND_EXPR, result_type,
3495 ifexp, xop1, xop2));
3497 layout_decl (tempvar, TYPE_ALIGN (result_type));
3498 /* No way to handle variable-sized objects here.
3499 I fear that the entire handling of BLKmode conditional exprs
3500 needs to be redone. */
3501 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3502 abort ();
3503 DECL_RTL (tempvar)
3504 = assign_stack_local (DECL_MODE (tempvar),
3505 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3506 + BITS_PER_UNIT - 1)
3507 / BITS_PER_UNIT,
3510 TREE_SIDE_EFFECTS (result)
3511 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3512 | TREE_SIDE_EFFECTS (op2);
3513 return build (COMPOUND_EXPR, result_type, result, tempvar);
3516 #endif /* 0 */
3518 if (TREE_CODE (ifexp) == INTEGER_CST)
3519 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3521 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3524 /* Given a list of expressions, return a compound expression
3525 that performs them all and returns the value of the last of them. */
3527 tree
3528 build_compound_expr (list)
3529 tree list;
3531 return internal_build_compound_expr (list, TRUE);
3534 static tree
3535 internal_build_compound_expr (list, first_p)
3536 tree list;
3537 int first_p;
3539 register tree rest;
3541 if (TREE_CHAIN (list) == 0)
3543 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3544 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3546 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3547 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3548 list = TREE_OPERAND (list, 0);
3549 #endif
3551 /* Don't let (0, 0) be null pointer constant. */
3552 if (!first_p && integer_zerop (TREE_VALUE (list)))
3553 return non_lvalue (TREE_VALUE (list));
3554 return TREE_VALUE (list);
3557 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3559 /* Convert arrays to pointers when there really is a comma operator. */
3560 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3561 TREE_VALUE (TREE_CHAIN (list))
3562 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3565 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3567 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3569 /* The left-hand operand of a comma expression is like an expression
3570 statement: with -W or -Wunused, we should warn if it doesn't have
3571 any side-effects, unless it was explicitly cast to (void). */
3572 if ((extra_warnings || warn_unused)
3573 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3574 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3575 warning ("left-hand operand of comma expression has no effect");
3577 /* When pedantic, a compound expression can be neither an lvalue
3578 nor an integer constant expression. */
3579 if (! pedantic)
3580 return rest;
3583 /* With -Wunused, we should also warn if the left-hand operand does have
3584 side-effects, but computes a value which is not used. For example, in
3585 `foo() + bar(), baz()' the result of the `+' operator is not used,
3586 so we should issue a warning. */
3587 else if (warn_unused)
3588 warn_if_unused_value (TREE_VALUE (list));
3590 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3593 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3595 tree
3596 build_c_cast (type, expr)
3597 register tree type;
3598 tree expr;
3600 register tree value = expr;
3602 if (type == error_mark_node || expr == error_mark_node)
3603 return error_mark_node;
3604 type = TYPE_MAIN_VARIANT (type);
3606 #if 0
3607 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3608 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3609 value = TREE_OPERAND (value, 0);
3610 #endif
3612 if (TREE_CODE (type) == ARRAY_TYPE)
3614 error ("cast specifies array type");
3615 return error_mark_node;
3618 if (TREE_CODE (type) == FUNCTION_TYPE)
3620 error ("cast specifies function type");
3621 return error_mark_node;
3624 if (type == TREE_TYPE (value))
3626 if (pedantic)
3628 if (TREE_CODE (type) == RECORD_TYPE
3629 || TREE_CODE (type) == UNION_TYPE)
3630 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3633 else if (TREE_CODE (type) == UNION_TYPE)
3635 tree field;
3636 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3637 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3638 value = default_conversion (value);
3640 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3641 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3642 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3643 break;
3645 if (field)
3647 const char *name;
3648 tree t;
3650 if (pedantic)
3651 pedwarn ("ANSI C forbids casts to union type");
3652 if (TYPE_NAME (type) != 0)
3654 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3655 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3656 else
3657 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3659 else
3660 name = "";
3661 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3662 build_tree_list (field, value)),
3663 0, 0);
3664 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3665 return t;
3667 error ("cast to union type from type not present in union");
3668 return error_mark_node;
3670 else
3672 tree otype, ovalue;
3674 /* If casting to void, avoid the error that would come
3675 from default_conversion in the case of a non-lvalue array. */
3676 if (type == void_type_node)
3677 return build1 (CONVERT_EXPR, type, value);
3679 /* Convert functions and arrays to pointers,
3680 but don't convert any other types. */
3681 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3682 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3683 value = default_conversion (value);
3684 otype = TREE_TYPE (value);
3686 /* Optionally warn about potentially worrisome casts. */
3688 if (warn_cast_qual
3689 && TREE_CODE (type) == POINTER_TYPE
3690 && TREE_CODE (otype) == POINTER_TYPE)
3692 /* Go to the innermost object being pointed to. */
3693 tree in_type = type;
3694 tree in_otype = otype;
3696 while (TREE_CODE (in_type) == POINTER_TYPE)
3697 in_type = TREE_TYPE (in_type);
3698 while (TREE_CODE (in_otype) == POINTER_TYPE)
3699 in_otype = TREE_TYPE (in_otype);
3701 if (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type))
3702 /* There are qualifiers present in IN_OTYPE that are not
3703 present in IN_TYPE. */
3704 pedwarn ("cast discards qualifiers from pointer target type");
3707 /* Warn about possible alignment problems. */
3708 if (STRICT_ALIGNMENT && warn_cast_align
3709 && TREE_CODE (type) == POINTER_TYPE
3710 && TREE_CODE (otype) == POINTER_TYPE
3711 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3712 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3713 /* Don't warn about opaque types, where the actual alignment
3714 restriction is unknown. */
3715 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3716 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3717 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3718 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3719 warning ("cast increases required alignment of target type");
3721 if (TREE_CODE (type) == INTEGER_TYPE
3722 && TREE_CODE (otype) == POINTER_TYPE
3723 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3724 && !TREE_CONSTANT (value))
3725 warning ("cast from pointer to integer of different size");
3727 if (warn_bad_function_cast
3728 && TREE_CODE (value) == CALL_EXPR
3729 && TREE_CODE (type) != TREE_CODE (otype))
3730 warning ("cast does not match function type");
3732 if (TREE_CODE (type) == POINTER_TYPE
3733 && TREE_CODE (otype) == INTEGER_TYPE
3734 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3735 #if 0
3736 /* Don't warn about converting 0 to pointer,
3737 provided the 0 was explicit--not cast or made by folding. */
3738 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3739 #endif
3740 /* Don't warn about converting any constant. */
3741 && !TREE_CONSTANT (value))
3742 warning ("cast to pointer from integer of different size");
3744 ovalue = value;
3745 value = convert (type, value);
3747 /* Ignore any integer overflow caused by the cast. */
3748 if (TREE_CODE (value) == INTEGER_CST)
3750 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3751 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3755 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3756 if (pedantic && TREE_CODE (value) == INTEGER_CST
3757 && TREE_CODE (expr) == INTEGER_CST
3758 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3759 value = non_lvalue (value);
3761 /* If pedantic, don't let a cast be an lvalue. */
3762 if (value == expr && pedantic)
3763 value = non_lvalue (value);
3765 return value;
3768 /* Build an assignment expression of lvalue LHS from value RHS.
3769 MODIFYCODE is the code for a binary operator that we use
3770 to combine the old value of LHS with RHS to get the new value.
3771 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3773 tree
3774 build_modify_expr (lhs, modifycode, rhs)
3775 tree lhs, rhs;
3776 enum tree_code modifycode;
3778 register tree result;
3779 tree newrhs;
3780 tree lhstype = TREE_TYPE (lhs);
3781 tree olhstype = lhstype;
3783 /* Types that aren't fully specified cannot be used in assignments. */
3784 lhs = require_complete_type (lhs);
3786 /* Avoid duplicate error messages from operands that had errors. */
3787 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3788 return error_mark_node;
3790 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3791 /* Do not use STRIP_NOPS here. We do not want an enumerator
3792 whose value is 0 to count as a null pointer constant. */
3793 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3794 rhs = TREE_OPERAND (rhs, 0);
3796 newrhs = rhs;
3798 /* Handle control structure constructs used as "lvalues". */
3800 switch (TREE_CODE (lhs))
3802 /* Handle (a, b) used as an "lvalue". */
3803 case COMPOUND_EXPR:
3804 pedantic_lvalue_warning (COMPOUND_EXPR);
3805 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3806 modifycode, rhs);
3807 if (TREE_CODE (newrhs) == ERROR_MARK)
3808 return error_mark_node;
3809 return build (COMPOUND_EXPR, lhstype,
3810 TREE_OPERAND (lhs, 0), newrhs);
3812 /* Handle (a ? b : c) used as an "lvalue". */
3813 case COND_EXPR:
3814 pedantic_lvalue_warning (COND_EXPR);
3815 rhs = save_expr (rhs);
3817 /* Produce (a ? (b = rhs) : (c = rhs))
3818 except that the RHS goes through a save-expr
3819 so the code to compute it is only emitted once. */
3820 tree cond
3821 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3822 build_modify_expr (TREE_OPERAND (lhs, 1),
3823 modifycode, rhs),
3824 build_modify_expr (TREE_OPERAND (lhs, 2),
3825 modifycode, rhs));
3826 if (TREE_CODE (cond) == ERROR_MARK)
3827 return cond;
3828 /* Make sure the code to compute the rhs comes out
3829 before the split. */
3830 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3831 /* But cast it to void to avoid an "unused" error. */
3832 convert (void_type_node, rhs), cond);
3834 default:
3835 break;
3838 /* If a binary op has been requested, combine the old LHS value with the RHS
3839 producing the value we should actually store into the LHS. */
3841 if (modifycode != NOP_EXPR)
3843 lhs = stabilize_reference (lhs);
3844 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3847 /* Handle a cast used as an "lvalue".
3848 We have already performed any binary operator using the value as cast.
3849 Now convert the result to the cast type of the lhs,
3850 and then true type of the lhs and store it there;
3851 then convert result back to the cast type to be the value
3852 of the assignment. */
3854 switch (TREE_CODE (lhs))
3856 case NOP_EXPR:
3857 case CONVERT_EXPR:
3858 case FLOAT_EXPR:
3859 case FIX_TRUNC_EXPR:
3860 case FIX_FLOOR_EXPR:
3861 case FIX_ROUND_EXPR:
3862 case FIX_CEIL_EXPR:
3863 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3864 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3865 newrhs = default_conversion (newrhs);
3867 tree inner_lhs = TREE_OPERAND (lhs, 0);
3868 tree result;
3869 result = build_modify_expr (inner_lhs, NOP_EXPR,
3870 convert (TREE_TYPE (inner_lhs),
3871 convert (lhstype, newrhs)));
3872 if (TREE_CODE (result) == ERROR_MARK)
3873 return result;
3874 pedantic_lvalue_warning (CONVERT_EXPR);
3875 return convert (TREE_TYPE (lhs), result);
3878 default:
3879 break;
3882 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3883 Reject anything strange now. */
3885 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3886 return error_mark_node;
3888 /* Warn about storing in something that is `const'. */
3890 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3891 || ((TREE_CODE (lhstype) == RECORD_TYPE
3892 || TREE_CODE (lhstype) == UNION_TYPE)
3893 && C_TYPE_FIELDS_READONLY (lhstype)))
3894 readonly_warning (lhs, "assignment");
3896 /* If storing into a structure or union member,
3897 it has probably been given type `int'.
3898 Compute the type that would go with
3899 the actual amount of storage the member occupies. */
3901 if (TREE_CODE (lhs) == COMPONENT_REF
3902 && (TREE_CODE (lhstype) == INTEGER_TYPE
3903 || TREE_CODE (lhstype) == REAL_TYPE
3904 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3905 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3907 /* If storing in a field that is in actuality a short or narrower than one,
3908 we must store in the field in its actual type. */
3910 if (lhstype != TREE_TYPE (lhs))
3912 lhs = copy_node (lhs);
3913 TREE_TYPE (lhs) = lhstype;
3916 /* Convert new value to destination type. */
3918 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3919 NULL_TREE, NULL_TREE, 0);
3920 if (TREE_CODE (newrhs) == ERROR_MARK)
3921 return error_mark_node;
3923 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3924 TREE_SIDE_EFFECTS (result) = 1;
3926 /* If we got the LHS in a different type for storing in,
3927 convert the result back to the nominal type of LHS
3928 so that the value we return always has the same type
3929 as the LHS argument. */
3931 if (olhstype == TREE_TYPE (result))
3932 return result;
3933 return convert_for_assignment (olhstype, result, _("assignment"),
3934 NULL_TREE, NULL_TREE, 0);
3937 /* Convert value RHS to type TYPE as preparation for an assignment
3938 to an lvalue of type TYPE.
3939 The real work of conversion is done by `convert'.
3940 The purpose of this function is to generate error messages
3941 for assignments that are not allowed in C.
3942 ERRTYPE is a string to use in error messages:
3943 "assignment", "return", etc. If it is null, this is parameter passing
3944 for a function call (and different error messages are output).
3946 FUNNAME is the name of the function being called,
3947 as an IDENTIFIER_NODE, or null.
3948 PARMNUM is the number of the argument, for printing in error messages. */
3950 static tree
3951 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3952 tree type, rhs;
3953 const char *errtype;
3954 tree fundecl, funname;
3955 int parmnum;
3957 register enum tree_code codel = TREE_CODE (type);
3958 register tree rhstype;
3959 register enum tree_code coder;
3961 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3962 /* Do not use STRIP_NOPS here. We do not want an enumerator
3963 whose value is 0 to count as a null pointer constant. */
3964 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3965 rhs = TREE_OPERAND (rhs, 0);
3967 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3968 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3969 rhs = default_conversion (rhs);
3970 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3971 rhs = decl_constant_value (rhs);
3973 rhstype = TREE_TYPE (rhs);
3974 coder = TREE_CODE (rhstype);
3976 if (coder == ERROR_MARK)
3977 return error_mark_node;
3979 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3981 overflow_warning (rhs);
3982 /* Check for Objective-C protocols. This will issue a warning if
3983 there are protocol violations. No need to use the return value. */
3984 maybe_objc_comptypes (type, rhstype, 0);
3985 return rhs;
3988 if (coder == VOID_TYPE)
3990 error ("void value not ignored as it ought to be");
3991 return error_mark_node;
3993 /* Arithmetic types all interconvert, and enum is treated like int. */
3994 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
3995 || codel == COMPLEX_TYPE)
3996 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
3997 || coder == COMPLEX_TYPE))
3998 return convert_and_check (type, rhs);
4000 /* Conversion to a transparent union from its member types.
4001 This applies only to function arguments. */
4002 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4004 tree memb_types;
4005 tree marginal_memb_type = 0;
4007 for (memb_types = TYPE_FIELDS (type); memb_types;
4008 memb_types = TREE_CHAIN (memb_types))
4010 tree memb_type = TREE_TYPE (memb_types);
4012 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4013 TYPE_MAIN_VARIANT (rhstype)))
4014 break;
4016 if (TREE_CODE (memb_type) != POINTER_TYPE)
4017 continue;
4019 if (coder == POINTER_TYPE)
4021 register tree ttl = TREE_TYPE (memb_type);
4022 register tree ttr = TREE_TYPE (rhstype);
4024 /* Any non-function converts to a [const][volatile] void *
4025 and vice versa; otherwise, targets must be the same.
4026 Meanwhile, the lhs target must have all the qualifiers of
4027 the rhs. */
4028 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4029 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4030 || comp_target_types (memb_type, rhstype))
4032 /* If this type won't generate any warnings, use it. */
4033 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4034 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4035 && TREE_CODE (ttl) == FUNCTION_TYPE)
4036 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4037 == TYPE_QUALS (ttr))
4038 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4039 == TYPE_QUALS (ttl))))
4040 break;
4042 /* Keep looking for a better type, but remember this one. */
4043 if (! marginal_memb_type)
4044 marginal_memb_type = memb_type;
4048 /* Can convert integer zero to any pointer type. */
4049 if (integer_zerop (rhs)
4050 || (TREE_CODE (rhs) == NOP_EXPR
4051 && integer_zerop (TREE_OPERAND (rhs, 0))))
4053 rhs = null_pointer_node;
4054 break;
4058 if (memb_types || marginal_memb_type)
4060 if (! memb_types)
4062 /* We have only a marginally acceptable member type;
4063 it needs a warning. */
4064 register tree ttl = TREE_TYPE (marginal_memb_type);
4065 register tree ttr = TREE_TYPE (rhstype);
4067 /* Const and volatile mean something different for function
4068 types, so the usual warnings are not appropriate. */
4069 if (TREE_CODE (ttr) == FUNCTION_TYPE
4070 && TREE_CODE (ttl) == FUNCTION_TYPE)
4072 /* Because const and volatile on functions are
4073 restrictions that say the function will not do
4074 certain things, it is okay to use a const or volatile
4075 function where an ordinary one is wanted, but not
4076 vice-versa. */
4077 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4078 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4079 errtype, funname, parmnum);
4081 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4082 warn_for_assignment ("%s discards qualifiers from pointer target type",
4083 errtype, funname,
4084 parmnum);
4087 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4088 pedwarn ("ANSI C prohibits argument conversion to union type");
4090 return build1 (NOP_EXPR, type, rhs);
4094 /* Conversions among pointers */
4095 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4097 register tree ttl = TREE_TYPE (type);
4098 register tree ttr = TREE_TYPE (rhstype);
4100 /* Any non-function converts to a [const][volatile] void *
4101 and vice versa; otherwise, targets must be the same.
4102 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4103 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4104 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4105 || comp_target_types (type, rhstype)
4106 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4107 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4109 if (pedantic
4110 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4111 && TREE_CODE (ttr) == FUNCTION_TYPE)
4113 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4114 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4115 which are not ANSI null ptr constants. */
4116 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4117 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4118 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4119 errtype, funname, parmnum);
4120 /* Const and volatile mean something different for function types,
4121 so the usual warnings are not appropriate. */
4122 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4123 && TREE_CODE (ttl) != FUNCTION_TYPE)
4125 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4126 warn_for_assignment ("%s discards qualifiers from pointer target type",
4127 errtype, funname, parmnum);
4128 /* If this is not a case of ignoring a mismatch in signedness,
4129 no warning. */
4130 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4131 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4132 || comp_target_types (type, rhstype))
4134 /* If there is a mismatch, do warn. */
4135 else if (pedantic)
4136 warn_for_assignment ("pointer targets in %s differ in signedness",
4137 errtype, funname, parmnum);
4139 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4140 && TREE_CODE (ttr) == FUNCTION_TYPE)
4142 /* Because const and volatile on functions are restrictions
4143 that say the function will not do certain things,
4144 it is okay to use a const or volatile function
4145 where an ordinary one is wanted, but not vice-versa. */
4146 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4147 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4148 errtype, funname, parmnum);
4151 else
4152 warn_for_assignment ("%s from incompatible pointer type",
4153 errtype, funname, parmnum);
4154 return convert (type, rhs);
4156 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4158 /* An explicit constant 0 can convert to a pointer,
4159 or one that results from arithmetic, even including
4160 a cast to integer type. */
4161 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4163 ! (TREE_CODE (rhs) == NOP_EXPR
4164 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4165 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4166 && integer_zerop (TREE_OPERAND (rhs, 0))))
4168 warn_for_assignment ("%s makes pointer from integer without a cast",
4169 errtype, funname, parmnum);
4170 return convert (type, rhs);
4172 return null_pointer_node;
4174 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4176 warn_for_assignment ("%s makes integer from pointer without a cast",
4177 errtype, funname, parmnum);
4178 return convert (type, rhs);
4181 if (!errtype)
4183 if (funname)
4185 tree selector = maybe_building_objc_message_expr ();
4187 if (selector && parmnum > 2)
4188 error ("incompatible type for argument %d of `%s'",
4189 parmnum - 2, IDENTIFIER_POINTER (selector));
4190 else
4191 error ("incompatible type for argument %d of `%s'",
4192 parmnum, IDENTIFIER_POINTER (funname));
4194 else
4195 error ("incompatible type for argument %d of indirect function call",
4196 parmnum);
4198 else
4199 error ("incompatible types in %s", errtype);
4201 return error_mark_node;
4204 /* Print a warning using MSGID.
4205 It gets OPNAME as its one parameter.
4206 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4207 FUNCTION and ARGNUM are handled specially if we are building an
4208 Objective-C selector. */
4210 static void
4211 warn_for_assignment (msgid, opname, function, argnum)
4212 const char *msgid;
4213 const char *opname;
4214 tree function;
4215 int argnum;
4217 if (opname == 0)
4219 tree selector = maybe_building_objc_message_expr ();
4220 char * new_opname;
4222 if (selector && argnum > 2)
4224 function = selector;
4225 argnum -= 2;
4227 if (function)
4229 /* Function name is known; supply it. */
4230 const char *argstring = _("passing arg %d of `%s'");
4231 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4232 + strlen (argstring) + 1 + 25
4233 /*%d*/ + 1);
4234 sprintf (new_opname, argstring, argnum,
4235 IDENTIFIER_POINTER (function));
4237 else
4239 /* Function name unknown (call through ptr); just give arg number.*/
4240 const char *argnofun = _("passing arg %d of pointer to function");
4241 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4242 sprintf (new_opname, argnofun, argnum);
4244 opname = new_opname;
4246 pedwarn (msgid, opname);
4249 /* If VALUE is a compound expr all of whose expressions are constant, then
4250 return its value. Otherwise, return error_mark_node.
4252 This is for handling COMPOUND_EXPRs as initializer elements
4253 which is allowed with a warning when -pedantic is specified. */
4255 static tree
4256 valid_compound_expr_initializer (value, endtype)
4257 tree value;
4258 tree endtype;
4260 if (TREE_CODE (value) == COMPOUND_EXPR)
4262 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4263 == error_mark_node)
4264 return error_mark_node;
4265 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4266 endtype);
4268 else if (! TREE_CONSTANT (value)
4269 && ! initializer_constant_valid_p (value, endtype))
4270 return error_mark_node;
4271 else
4272 return value;
4275 /* Perform appropriate conversions on the initial value of a variable,
4276 store it in the declaration DECL,
4277 and print any error messages that are appropriate.
4278 If the init is invalid, store an ERROR_MARK. */
4280 void
4281 store_init_value (decl, init)
4282 tree decl, init;
4284 register tree value, type;
4286 /* If variable's type was invalidly declared, just ignore it. */
4288 type = TREE_TYPE (decl);
4289 if (TREE_CODE (type) == ERROR_MARK)
4290 return;
4292 /* Digest the specified initializer into an expression. */
4294 value = digest_init (type, init, TREE_STATIC (decl),
4295 TREE_STATIC (decl) || pedantic);
4297 /* Store the expression if valid; else report error. */
4299 #if 0
4300 /* Note that this is the only place we can detect the error
4301 in a case such as struct foo bar = (struct foo) { x, y };
4302 where there is one initial value which is a constructor expression. */
4303 if (value == error_mark_node)
4305 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4307 error ("initializer for static variable is not constant");
4308 value = error_mark_node;
4310 else if (TREE_STATIC (decl)
4311 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4313 error ("initializer for static variable uses complicated arithmetic");
4314 value = error_mark_node;
4316 else
4318 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4320 if (! TREE_CONSTANT (value))
4321 pedwarn ("aggregate initializer is not constant");
4322 else if (! TREE_STATIC (value))
4323 pedwarn ("aggregate initializer uses complicated arithmetic");
4326 #endif
4328 DECL_INITIAL (decl) = value;
4330 /* ANSI wants warnings about out-of-range constant initializers. */
4331 STRIP_TYPE_NOPS (value);
4332 constant_expression_warning (value);
4335 /* Methods for storing and printing names for error messages. */
4337 /* Implement a spelling stack that allows components of a name to be pushed
4338 and popped. Each element on the stack is this structure. */
4340 struct spelling
4342 int kind;
4343 union
4345 int i;
4346 const char *s;
4347 } u;
4350 #define SPELLING_STRING 1
4351 #define SPELLING_MEMBER 2
4352 #define SPELLING_BOUNDS 3
4354 static struct spelling *spelling; /* Next stack element (unused). */
4355 static struct spelling *spelling_base; /* Spelling stack base. */
4356 static int spelling_size; /* Size of the spelling stack. */
4358 /* Macros to save and restore the spelling stack around push_... functions.
4359 Alternative to SAVE_SPELLING_STACK. */
4361 #define SPELLING_DEPTH() (spelling - spelling_base)
4362 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4364 /* Save and restore the spelling stack around arbitrary C code. */
4366 #define SAVE_SPELLING_DEPTH(code) \
4368 int __depth = SPELLING_DEPTH (); \
4369 code; \
4370 RESTORE_SPELLING_DEPTH (__depth); \
4373 /* Push an element on the spelling stack with type KIND and assign VALUE
4374 to MEMBER. */
4376 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4378 int depth = SPELLING_DEPTH (); \
4380 if (depth >= spelling_size) \
4382 spelling_size += 10; \
4383 if (spelling_base == 0) \
4384 spelling_base \
4385 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4386 else \
4387 spelling_base \
4388 = (struct spelling *) xrealloc (spelling_base, \
4389 spelling_size * sizeof (struct spelling)); \
4390 RESTORE_SPELLING_DEPTH (depth); \
4393 spelling->kind = (KIND); \
4394 spelling->MEMBER = (VALUE); \
4395 spelling++; \
4398 /* Push STRING on the stack. Printed literally. */
4400 static void
4401 push_string (string)
4402 const char *string;
4404 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4407 /* Push a member name on the stack. Printed as '.' STRING. */
4409 static void
4410 push_member_name (decl)
4411 tree decl;
4414 const char *string
4415 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4416 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4419 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4421 static void
4422 push_array_bounds (bounds)
4423 int bounds;
4425 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4428 /* Compute the maximum size in bytes of the printed spelling. */
4430 static int
4431 spelling_length ()
4433 register int size = 0;
4434 register struct spelling *p;
4436 for (p = spelling_base; p < spelling; p++)
4438 if (p->kind == SPELLING_BOUNDS)
4439 size += 25;
4440 else
4441 size += strlen (p->u.s) + 1;
4444 return size;
4447 /* Print the spelling to BUFFER and return it. */
4449 static char *
4450 print_spelling (buffer)
4451 register char *buffer;
4453 register char *d = buffer;
4454 register struct spelling *p;
4456 for (p = spelling_base; p < spelling; p++)
4457 if (p->kind == SPELLING_BOUNDS)
4459 sprintf (d, "[%d]", p->u.i);
4460 d += strlen (d);
4462 else
4464 register const char *s;
4465 if (p->kind == SPELLING_MEMBER)
4466 *d++ = '.';
4467 for (s = p->u.s; (*d = *s++); d++)
4470 *d++ = '\0';
4471 return buffer;
4474 /* Issue an error message for a bad initializer component.
4475 MSGID identifies the message.
4476 The component name is taken from the spelling stack. */
4478 void
4479 error_init (msgid)
4480 const char *msgid;
4482 char *ofwhat;
4484 error (msgid);
4485 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4486 if (*ofwhat)
4487 error ("(near initialization for `%s')", ofwhat);
4490 /* Issue a pedantic warning for a bad initializer component.
4491 MSGID identifies the message.
4492 The component name is taken from the spelling stack. */
4494 void
4495 pedwarn_init (msgid)
4496 const char *msgid;
4498 char *ofwhat;
4500 pedwarn (msgid);
4501 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4502 if (*ofwhat)
4503 pedwarn ("(near initialization for `%s')", ofwhat);
4506 /* Issue a warning for a bad initializer component.
4507 MSGID identifies the message.
4508 The component name is taken from the spelling stack. */
4510 static void
4511 warning_init (msgid)
4512 const char *msgid;
4514 char *ofwhat;
4516 warning (msgid);
4517 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4518 if (*ofwhat)
4519 warning ("(near initialization for `%s')", ofwhat);
4522 /* Digest the parser output INIT as an initializer for type TYPE.
4523 Return a C expression of type TYPE to represent the initial value.
4525 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4526 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4527 applies only to elements of constructors. */
4529 static tree
4530 digest_init (type, init, require_constant, constructor_constant)
4531 tree type, init;
4532 int require_constant, constructor_constant;
4534 enum tree_code code = TREE_CODE (type);
4535 tree inside_init = init;
4537 if (init == error_mark_node)
4538 return init;
4540 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4541 /* Do not use STRIP_NOPS here. We do not want an enumerator
4542 whose value is 0 to count as a null pointer constant. */
4543 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4544 inside_init = TREE_OPERAND (init, 0);
4546 /* Initialization of an array of chars from a string constant
4547 optionally enclosed in braces. */
4549 if (code == ARRAY_TYPE)
4551 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4552 if ((typ1 == char_type_node
4553 || typ1 == signed_char_type_node
4554 || typ1 == unsigned_char_type_node
4555 || typ1 == unsigned_wchar_type_node
4556 || typ1 == signed_wchar_type_node)
4557 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4559 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4560 TYPE_MAIN_VARIANT (type)))
4561 return inside_init;
4563 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4564 != char_type_node)
4565 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4567 error_init ("char-array initialized from wide string");
4568 return error_mark_node;
4570 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4571 == char_type_node)
4572 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4574 error_init ("int-array initialized from non-wide string");
4575 return error_mark_node;
4578 TREE_TYPE (inside_init) = type;
4579 if (TYPE_DOMAIN (type) != 0
4580 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4582 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4583 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4584 /* Subtract 1 (or sizeof (wchar_t))
4585 because it's ok to ignore the terminating null char
4586 that is counted in the length of the constant. */
4587 if (size < TREE_STRING_LENGTH (inside_init)
4588 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4589 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4590 : 1))
4591 pedwarn_init ("initializer-string for array of chars is too long");
4593 return inside_init;
4597 /* Any type can be initialized
4598 from an expression of the same type, optionally with braces. */
4600 if (inside_init && TREE_TYPE (inside_init) != 0
4601 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4602 TYPE_MAIN_VARIANT (type))
4603 || (code == ARRAY_TYPE
4604 && comptypes (TREE_TYPE (inside_init), type))
4605 || (code == POINTER_TYPE
4606 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4607 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4608 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4609 TREE_TYPE (type)))))
4611 if (code == POINTER_TYPE
4612 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4613 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4614 inside_init = default_conversion (inside_init);
4615 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4616 && TREE_CODE (inside_init) != CONSTRUCTOR)
4618 error_init ("array initialized from non-constant array expression");
4619 return error_mark_node;
4622 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4623 inside_init = decl_constant_value (inside_init);
4625 /* Compound expressions can only occur here if -pedantic or
4626 -pedantic-errors is specified. In the later case, we always want
4627 an error. In the former case, we simply want a warning. */
4628 if (require_constant && pedantic
4629 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4631 inside_init
4632 = valid_compound_expr_initializer (inside_init,
4633 TREE_TYPE (inside_init));
4634 if (inside_init == error_mark_node)
4635 error_init ("initializer element is not constant");
4636 else
4637 pedwarn_init ("initializer element is not constant");
4638 if (flag_pedantic_errors)
4639 inside_init = error_mark_node;
4641 else if (require_constant && ! TREE_CONSTANT (inside_init))
4643 error_init ("initializer element is not constant");
4644 inside_init = error_mark_node;
4646 else if (require_constant
4647 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4649 error_init ("initializer element is not computable at load time");
4650 inside_init = error_mark_node;
4653 return inside_init;
4656 /* Handle scalar types, including conversions. */
4658 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4659 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4661 /* Note that convert_for_assignment calls default_conversion
4662 for arrays and functions. We must not call it in the
4663 case where inside_init is a null pointer constant. */
4664 inside_init
4665 = convert_for_assignment (type, init, _("initialization"),
4666 NULL_TREE, NULL_TREE, 0);
4668 if (require_constant && ! TREE_CONSTANT (inside_init))
4670 error_init ("initializer element is not constant");
4671 inside_init = error_mark_node;
4673 else if (require_constant
4674 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4676 error_init ("initializer element is not computable at load time");
4677 inside_init = error_mark_node;
4680 return inside_init;
4683 /* Come here only for records and arrays. */
4685 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4687 error_init ("variable-sized object may not be initialized");
4688 return error_mark_node;
4691 /* Traditionally, you can write struct foo x = 0;
4692 and it initializes the first element of x to 0. */
4693 if (flag_traditional)
4695 tree top = 0, prev = 0, otype = type;
4696 while (TREE_CODE (type) == RECORD_TYPE
4697 || TREE_CODE (type) == ARRAY_TYPE
4698 || TREE_CODE (type) == QUAL_UNION_TYPE
4699 || TREE_CODE (type) == UNION_TYPE)
4701 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4702 if (prev == 0)
4703 top = temp;
4704 else
4705 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4706 prev = temp;
4707 if (TREE_CODE (type) == ARRAY_TYPE)
4708 type = TREE_TYPE (type);
4709 else if (TYPE_FIELDS (type))
4710 type = TREE_TYPE (TYPE_FIELDS (type));
4711 else
4713 error_init ("invalid initializer");
4714 return error_mark_node;
4718 if (otype != type)
4720 TREE_OPERAND (prev, 1)
4721 = build_tree_list (NULL_TREE,
4722 digest_init (type, init, require_constant,
4723 constructor_constant));
4724 return top;
4726 else
4727 return error_mark_node;
4729 error_init ("invalid initializer");
4730 return error_mark_node;
4733 /* Handle initializers that use braces. */
4735 /* Type of object we are accumulating a constructor for.
4736 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4737 static tree constructor_type;
4739 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4740 left to fill. */
4741 static tree constructor_fields;
4743 /* For an ARRAY_TYPE, this is the specified index
4744 at which to store the next element we get.
4745 This is a special INTEGER_CST node that we modify in place. */
4746 static tree constructor_index;
4748 /* For an ARRAY_TYPE, this is the end index of the range
4749 to initialize with the next element, or NULL in the ordinary case
4750 where the element is used just once. */
4751 static tree constructor_range_end;
4753 /* For an ARRAY_TYPE, this is the maximum index. */
4754 static tree constructor_max_index;
4756 /* For a RECORD_TYPE, this is the first field not yet written out. */
4757 static tree constructor_unfilled_fields;
4759 /* For an ARRAY_TYPE, this is the index of the first element
4760 not yet written out.
4761 This is a special INTEGER_CST node that we modify in place. */
4762 static tree constructor_unfilled_index;
4764 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4765 This is so we can generate gaps between fields, when appropriate.
4766 This is a special INTEGER_CST node that we modify in place. */
4767 static tree constructor_bit_index;
4769 /* If we are saving up the elements rather than allocating them,
4770 this is the list of elements so far (in reverse order,
4771 most recent first). */
4772 static tree constructor_elements;
4774 /* 1 if so far this constructor's elements are all compile-time constants. */
4775 static int constructor_constant;
4777 /* 1 if so far this constructor's elements are all valid address constants. */
4778 static int constructor_simple;
4780 /* 1 if this constructor is erroneous so far. */
4781 static int constructor_erroneous;
4783 /* 1 if have called defer_addressed_constants. */
4784 static int constructor_subconstants_deferred;
4786 /* Structure for managing pending initializer elements, organized as an
4787 AVL tree. */
4789 struct init_node
4791 struct init_node *left, *right;
4792 struct init_node *parent;
4793 int balance;
4794 tree purpose;
4795 tree value;
4798 /* Tree of pending elements at this constructor level.
4799 These are elements encountered out of order
4800 which belong at places we haven't reached yet in actually
4801 writing the output. */
4802 static struct init_node *constructor_pending_elts;
4804 /* The SPELLING_DEPTH of this constructor. */
4805 static int constructor_depth;
4807 /* 0 if implicitly pushing constructor levels is allowed. */
4808 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4810 static int require_constant_value;
4811 static int require_constant_elements;
4813 /* 1 if it is ok to output this constructor as we read it.
4814 0 means must accumulate a CONSTRUCTOR expression. */
4815 static int constructor_incremental;
4817 /* DECL node for which an initializer is being read.
4818 0 means we are reading a constructor expression
4819 such as (struct foo) {...}. */
4820 static tree constructor_decl;
4822 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4823 static char *constructor_asmspec;
4825 /* Nonzero if this is an initializer for a top-level decl. */
4826 static int constructor_top_level;
4829 /* This stack has a level for each implicit or explicit level of
4830 structuring in the initializer, including the outermost one. It
4831 saves the values of most of the variables above. */
4833 struct constructor_stack
4835 struct constructor_stack *next;
4836 tree type;
4837 tree fields;
4838 tree index;
4839 tree range_end;
4840 tree max_index;
4841 tree unfilled_index;
4842 tree unfilled_fields;
4843 tree bit_index;
4844 tree elements;
4845 int offset;
4846 struct init_node *pending_elts;
4847 int depth;
4848 /* If nonzero, this value should replace the entire
4849 constructor at this level. */
4850 tree replacement_value;
4851 char constant;
4852 char simple;
4853 char implicit;
4854 char incremental;
4855 char erroneous;
4856 char outer;
4859 struct constructor_stack *constructor_stack;
4861 /* This stack records separate initializers that are nested.
4862 Nested initializers can't happen in ANSI C, but GNU C allows them
4863 in cases like { ... (struct foo) { ... } ... }. */
4865 struct initializer_stack
4867 struct initializer_stack *next;
4868 tree decl;
4869 char *asmspec;
4870 struct constructor_stack *constructor_stack;
4871 tree elements;
4872 struct spelling *spelling;
4873 struct spelling *spelling_base;
4874 int spelling_size;
4875 char top_level;
4876 char incremental;
4877 char require_constant_value;
4878 char require_constant_elements;
4879 char deferred;
4882 struct initializer_stack *initializer_stack;
4884 /* Prepare to parse and output the initializer for variable DECL. */
4886 void
4887 start_init (decl, asmspec_tree, top_level)
4888 tree decl;
4889 tree asmspec_tree;
4890 int top_level;
4892 const char *locus;
4893 struct initializer_stack *p
4894 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4895 char *asmspec = 0;
4897 if (asmspec_tree)
4898 asmspec = TREE_STRING_POINTER (asmspec_tree);
4900 p->decl = constructor_decl;
4901 p->asmspec = constructor_asmspec;
4902 p->incremental = constructor_incremental;
4903 p->require_constant_value = require_constant_value;
4904 p->require_constant_elements = require_constant_elements;
4905 p->constructor_stack = constructor_stack;
4906 p->elements = constructor_elements;
4907 p->spelling = spelling;
4908 p->spelling_base = spelling_base;
4909 p->spelling_size = spelling_size;
4910 p->deferred = constructor_subconstants_deferred;
4911 p->top_level = constructor_top_level;
4912 p->next = initializer_stack;
4913 initializer_stack = p;
4915 constructor_decl = decl;
4916 constructor_incremental = top_level;
4917 constructor_asmspec = asmspec;
4918 constructor_subconstants_deferred = 0;
4919 constructor_top_level = top_level;
4921 if (decl != 0)
4923 require_constant_value = TREE_STATIC (decl);
4924 require_constant_elements
4925 = ((TREE_STATIC (decl) || pedantic)
4926 /* For a scalar, you can always use any value to initialize,
4927 even within braces. */
4928 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4929 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4930 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4931 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4932 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4933 constructor_incremental |= TREE_STATIC (decl);
4935 else
4937 require_constant_value = 0;
4938 require_constant_elements = 0;
4939 locus = "(anonymous)";
4942 constructor_stack = 0;
4944 missing_braces_mentioned = 0;
4946 spelling_base = 0;
4947 spelling_size = 0;
4948 RESTORE_SPELLING_DEPTH (0);
4950 if (locus)
4951 push_string (locus);
4954 void
4955 finish_init ()
4957 struct initializer_stack *p = initializer_stack;
4959 /* Output subconstants (string constants, usually)
4960 that were referenced within this initializer and saved up.
4961 Must do this if and only if we called defer_addressed_constants. */
4962 if (constructor_subconstants_deferred)
4963 output_deferred_addressed_constants ();
4965 /* Free the whole constructor stack of this initializer. */
4966 while (constructor_stack)
4968 struct constructor_stack *q = constructor_stack;
4969 constructor_stack = q->next;
4970 free (q);
4973 /* Pop back to the data of the outer initializer (if any). */
4974 constructor_decl = p->decl;
4975 constructor_asmspec = p->asmspec;
4976 constructor_incremental = p->incremental;
4977 require_constant_value = p->require_constant_value;
4978 require_constant_elements = p->require_constant_elements;
4979 constructor_stack = p->constructor_stack;
4980 constructor_elements = p->elements;
4981 spelling = p->spelling;
4982 spelling_base = p->spelling_base;
4983 spelling_size = p->spelling_size;
4984 constructor_subconstants_deferred = p->deferred;
4985 constructor_top_level = p->top_level;
4986 initializer_stack = p->next;
4987 free (p);
4990 /* Call here when we see the initializer is surrounded by braces.
4991 This is instead of a call to push_init_level;
4992 it is matched by a call to pop_init_level.
4994 TYPE is the type to initialize, for a constructor expression.
4995 For an initializer for a decl, TYPE is zero. */
4997 void
4998 really_start_incremental_init (type)
4999 tree type;
5001 struct constructor_stack *p
5002 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5004 if (type == 0)
5005 type = TREE_TYPE (constructor_decl);
5007 /* Turn off constructor_incremental if type is a struct with bitfields.
5008 Do this before the first push, so that the corrected value
5009 is available in finish_init. */
5010 check_init_type_bitfields (type);
5012 p->type = constructor_type;
5013 p->fields = constructor_fields;
5014 p->index = constructor_index;
5015 p->range_end = constructor_range_end;
5016 p->max_index = constructor_max_index;
5017 p->unfilled_index = constructor_unfilled_index;
5018 p->unfilled_fields = constructor_unfilled_fields;
5019 p->bit_index = constructor_bit_index;
5020 p->elements = constructor_elements;
5021 p->constant = constructor_constant;
5022 p->simple = constructor_simple;
5023 p->erroneous = constructor_erroneous;
5024 p->pending_elts = constructor_pending_elts;
5025 p->depth = constructor_depth;
5026 p->replacement_value = 0;
5027 p->implicit = 0;
5028 p->incremental = constructor_incremental;
5029 p->outer = 0;
5030 p->next = 0;
5031 constructor_stack = p;
5033 constructor_constant = 1;
5034 constructor_simple = 1;
5035 constructor_depth = SPELLING_DEPTH ();
5036 constructor_elements = 0;
5037 constructor_pending_elts = 0;
5038 constructor_type = type;
5040 if (TREE_CODE (constructor_type) == RECORD_TYPE
5041 || TREE_CODE (constructor_type) == UNION_TYPE)
5043 constructor_fields = TYPE_FIELDS (constructor_type);
5044 /* Skip any nameless bit fields at the beginning. */
5045 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5046 && DECL_NAME (constructor_fields) == 0)
5047 constructor_fields = TREE_CHAIN (constructor_fields);
5048 constructor_unfilled_fields = constructor_fields;
5049 constructor_bit_index = copy_node (integer_zero_node);
5050 TREE_TYPE (constructor_bit_index) = sbitsizetype;
5052 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5054 constructor_range_end = 0;
5055 if (TYPE_DOMAIN (constructor_type))
5057 constructor_max_index
5058 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5059 constructor_index
5060 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5062 else
5063 constructor_index = copy_node (integer_zero_node);
5064 constructor_unfilled_index = copy_node (constructor_index);
5066 else
5068 /* Handle the case of int x = {5}; */
5069 constructor_fields = constructor_type;
5070 constructor_unfilled_fields = constructor_type;
5073 if (constructor_incremental)
5075 int momentary = suspend_momentary ();
5076 push_obstacks_nochange ();
5077 if (TREE_PERMANENT (constructor_decl))
5078 end_temporary_allocation ();
5079 make_decl_rtl (constructor_decl, constructor_asmspec,
5080 constructor_top_level);
5081 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5082 pop_obstacks ();
5083 resume_momentary (momentary);
5086 if (constructor_incremental)
5088 defer_addressed_constants ();
5089 constructor_subconstants_deferred = 1;
5093 /* Push down into a subobject, for initialization.
5094 If this is for an explicit set of braces, IMPLICIT is 0.
5095 If it is because the next element belongs at a lower level,
5096 IMPLICIT is 1. */
5098 void
5099 push_init_level (implicit)
5100 int implicit;
5102 struct constructor_stack *p;
5104 /* If we've exhausted any levels that didn't have braces,
5105 pop them now. */
5106 while (constructor_stack->implicit)
5108 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5109 || TREE_CODE (constructor_type) == UNION_TYPE)
5110 && constructor_fields == 0)
5111 process_init_element (pop_init_level (1));
5112 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5113 && tree_int_cst_lt (constructor_max_index, constructor_index))
5114 process_init_element (pop_init_level (1));
5115 else
5116 break;
5119 /* Structure elements may require alignment. Do this now if necessary
5120 for the subaggregate, and if it comes next in sequence. Don't do
5121 this for subaggregates that will go on the pending list. */
5122 if (constructor_incremental && constructor_type != 0
5123 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields
5124 && constructor_fields == constructor_unfilled_fields)
5126 /* Advance to offset of this element. */
5127 if (! tree_int_cst_equal (constructor_bit_index,
5128 DECL_FIELD_BITPOS (constructor_fields)))
5130 /* By using unsigned arithmetic, the result will be correct even
5131 in case of overflows, if BITS_PER_UNIT is a power of two. */
5132 unsigned next = (TREE_INT_CST_LOW
5133 (DECL_FIELD_BITPOS (constructor_fields))
5134 / (unsigned)BITS_PER_UNIT);
5135 unsigned here = (TREE_INT_CST_LOW (constructor_bit_index)
5136 / (unsigned)BITS_PER_UNIT);
5138 assemble_zeros ((next - here)
5139 * (unsigned)BITS_PER_UNIT
5140 / (unsigned)BITS_PER_UNIT);
5142 /* Indicate that we have now filled the structure up to the current
5143 field. */
5144 constructor_unfilled_fields = constructor_fields;
5147 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5148 p->type = constructor_type;
5149 p->fields = constructor_fields;
5150 p->index = constructor_index;
5151 p->range_end = constructor_range_end;
5152 p->max_index = constructor_max_index;
5153 p->unfilled_index = constructor_unfilled_index;
5154 p->unfilled_fields = constructor_unfilled_fields;
5155 p->bit_index = constructor_bit_index;
5156 p->elements = constructor_elements;
5157 p->constant = constructor_constant;
5158 p->simple = constructor_simple;
5159 p->erroneous = constructor_erroneous;
5160 p->pending_elts = constructor_pending_elts;
5161 p->depth = constructor_depth;
5162 p->replacement_value = 0;
5163 p->implicit = implicit;
5164 p->incremental = constructor_incremental;
5165 p->outer = 0;
5166 p->next = constructor_stack;
5167 constructor_stack = p;
5169 constructor_constant = 1;
5170 constructor_simple = 1;
5171 constructor_depth = SPELLING_DEPTH ();
5172 constructor_elements = 0;
5173 constructor_pending_elts = 0;
5175 /* Don't die if an entire brace-pair level is superfluous
5176 in the containing level. */
5177 if (constructor_type == 0)
5179 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5180 || TREE_CODE (constructor_type) == UNION_TYPE)
5182 /* Don't die if there are extra init elts at the end. */
5183 if (constructor_fields == 0)
5184 constructor_type = 0;
5185 else
5187 constructor_type = TREE_TYPE (constructor_fields);
5188 push_member_name (constructor_fields);
5189 constructor_depth++;
5190 if (constructor_fields != constructor_unfilled_fields)
5191 constructor_incremental = 0;
5194 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5196 constructor_type = TREE_TYPE (constructor_type);
5197 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5198 constructor_depth++;
5199 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5200 || constructor_range_end != 0)
5201 constructor_incremental = 0;
5204 if (constructor_type == 0)
5206 error_init ("extra brace group at end of initializer");
5207 constructor_fields = 0;
5208 constructor_unfilled_fields = 0;
5209 return;
5212 /* Turn off constructor_incremental if type is a struct with bitfields. */
5213 check_init_type_bitfields (constructor_type);
5215 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5217 missing_braces_mentioned = 1;
5218 warning_init ("missing braces around initializer");
5221 if (TREE_CODE (constructor_type) == RECORD_TYPE
5222 || TREE_CODE (constructor_type) == UNION_TYPE)
5224 constructor_fields = TYPE_FIELDS (constructor_type);
5225 /* Skip any nameless bit fields at the beginning. */
5226 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5227 && DECL_NAME (constructor_fields) == 0)
5228 constructor_fields = TREE_CHAIN (constructor_fields);
5229 constructor_unfilled_fields = constructor_fields;
5230 constructor_bit_index = copy_node (integer_zero_node);
5231 TREE_TYPE (constructor_bit_index) = sbitsizetype;
5233 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5235 constructor_range_end = 0;
5236 if (TYPE_DOMAIN (constructor_type))
5238 constructor_max_index
5239 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5240 constructor_index
5241 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5243 else
5244 constructor_index = copy_node (integer_zero_node);
5245 constructor_unfilled_index = copy_node (constructor_index);
5247 else
5249 warning_init ("braces around scalar initializer");
5250 constructor_fields = constructor_type;
5251 constructor_unfilled_fields = constructor_type;
5255 /* Don't read a struct incrementally if it has any bitfields,
5256 because the incremental reading code doesn't know how to
5257 handle bitfields yet. */
5259 static void
5260 check_init_type_bitfields (type)
5261 tree type;
5263 if (TREE_CODE (type) == RECORD_TYPE)
5265 tree tail;
5266 for (tail = TYPE_FIELDS (type); tail;
5267 tail = TREE_CHAIN (tail))
5269 if (DECL_C_BIT_FIELD (tail))
5271 constructor_incremental = 0;
5272 break;
5275 check_init_type_bitfields (TREE_TYPE (tail));
5279 else if (TREE_CODE (type) == UNION_TYPE)
5281 tree tail = TYPE_FIELDS (type);
5282 if (tail && DECL_C_BIT_FIELD (tail))
5283 /* We also use the nonincremental algorithm for initiliazation
5284 of unions whose first member is a bitfield, becuase the
5285 incremental algorithm has no code for dealing with
5286 bitfields. */
5287 constructor_incremental = 0;
5290 else if (TREE_CODE (type) == ARRAY_TYPE)
5291 check_init_type_bitfields (TREE_TYPE (type));
5294 /* At the end of an implicit or explicit brace level,
5295 finish up that level of constructor.
5296 If we were outputting the elements as they are read, return 0
5297 from inner levels (process_init_element ignores that),
5298 but return error_mark_node from the outermost level
5299 (that's what we want to put in DECL_INITIAL).
5300 Otherwise, return a CONSTRUCTOR expression. */
5302 tree
5303 pop_init_level (implicit)
5304 int implicit;
5306 struct constructor_stack *p;
5307 int size = 0;
5308 tree constructor = 0;
5310 if (implicit == 0)
5312 /* When we come to an explicit close brace,
5313 pop any inner levels that didn't have explicit braces. */
5314 while (constructor_stack->implicit)
5315 process_init_element (pop_init_level (1));
5318 p = constructor_stack;
5320 if (constructor_type != 0)
5321 size = int_size_in_bytes (constructor_type);
5323 /* Warn when some struct elements are implicitly initialized to zero. */
5324 if (extra_warnings
5325 && constructor_type
5326 && TREE_CODE (constructor_type) == RECORD_TYPE
5327 && constructor_unfilled_fields)
5329 push_member_name (constructor_unfilled_fields);
5330 warning_init ("missing initializer");
5331 RESTORE_SPELLING_DEPTH (constructor_depth);
5334 /* Now output all pending elements. */
5335 output_pending_init_elements (1);
5337 #if 0 /* c-parse.in warns about {}. */
5338 /* In ANSI, each brace level must have at least one element. */
5339 if (! implicit && pedantic
5340 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5341 ? integer_zerop (constructor_unfilled_index)
5342 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5343 pedwarn_init ("empty braces in initializer");
5344 #endif
5346 /* Pad out the end of the structure. */
5348 if (p->replacement_value)
5350 /* If this closes a superfluous brace pair,
5351 just pass out the element between them. */
5352 constructor = p->replacement_value;
5353 /* If this is the top level thing within the initializer,
5354 and it's for a variable, then since we already called
5355 assemble_variable, we must output the value now. */
5356 if (p->next == 0 && constructor_decl != 0
5357 && constructor_incremental)
5359 constructor = digest_init (constructor_type, constructor,
5360 require_constant_value,
5361 require_constant_elements);
5363 /* If initializing an array of unknown size,
5364 determine the size now. */
5365 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5366 && TYPE_DOMAIN (constructor_type) == 0)
5368 int failure;
5369 int momentary_p;
5371 push_obstacks_nochange ();
5372 if (TREE_PERMANENT (constructor_type))
5373 end_temporary_allocation ();
5375 momentary_p = suspend_momentary ();
5377 /* We shouldn't have an incomplete array type within
5378 some other type. */
5379 if (constructor_stack->next)
5380 abort ();
5382 failure
5383 = complete_array_type (constructor_type,
5384 constructor, 0);
5385 if (failure)
5386 abort ();
5388 size = int_size_in_bytes (constructor_type);
5389 resume_momentary (momentary_p);
5390 pop_obstacks ();
5393 output_constant (constructor, size);
5396 else if (constructor_type == 0)
5398 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5399 && TREE_CODE (constructor_type) != UNION_TYPE
5400 && TREE_CODE (constructor_type) != ARRAY_TYPE
5401 && ! constructor_incremental)
5403 /* A nonincremental scalar initializer--just return
5404 the element, after verifying there is just one. */
5405 if (constructor_elements == 0)
5407 error_init ("empty scalar initializer");
5408 constructor = error_mark_node;
5410 else if (TREE_CHAIN (constructor_elements) != 0)
5412 error_init ("extra elements in scalar initializer");
5413 constructor = TREE_VALUE (constructor_elements);
5415 else
5416 constructor = TREE_VALUE (constructor_elements);
5418 else if (! constructor_incremental)
5420 if (constructor_erroneous)
5421 constructor = error_mark_node;
5422 else
5424 int momentary = suspend_momentary ();
5426 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5427 nreverse (constructor_elements));
5428 if (constructor_constant)
5429 TREE_CONSTANT (constructor) = 1;
5430 if (constructor_constant && constructor_simple)
5431 TREE_STATIC (constructor) = 1;
5433 resume_momentary (momentary);
5436 else
5438 tree filled;
5439 int momentary = suspend_momentary ();
5441 if (TREE_CODE (constructor_type) == RECORD_TYPE
5442 || TREE_CODE (constructor_type) == UNION_TYPE)
5444 /* Find the offset of the end of that field. */
5445 filled = size_binop (CEIL_DIV_EXPR,
5446 constructor_bit_index,
5447 size_int (BITS_PER_UNIT));
5449 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5451 /* If initializing an array of unknown size,
5452 determine the size now. */
5453 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5454 && TYPE_DOMAIN (constructor_type) == 0)
5456 tree maxindex
5457 = size_binop (MINUS_EXPR,
5458 constructor_unfilled_index,
5459 integer_one_node);
5461 push_obstacks_nochange ();
5462 if (TREE_PERMANENT (constructor_type))
5463 end_temporary_allocation ();
5464 maxindex = copy_node (maxindex);
5465 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5466 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5468 /* TYPE_MAX_VALUE is always one less than the number of elements
5469 in the array, because we start counting at zero. Therefore,
5470 warn only if the value is less than zero. */
5471 if (pedantic
5472 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5473 < 0))
5474 error_with_decl (constructor_decl,
5475 "zero or negative array size `%s'");
5476 layout_type (constructor_type);
5477 size = int_size_in_bytes (constructor_type);
5478 pop_obstacks ();
5481 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5482 size_in_bytes (TREE_TYPE (constructor_type)));
5484 else
5485 filled = 0;
5487 if (filled != 0)
5488 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5490 resume_momentary (momentary);
5494 constructor_type = p->type;
5495 constructor_fields = p->fields;
5496 constructor_index = p->index;
5497 constructor_range_end = p->range_end;
5498 constructor_max_index = p->max_index;
5499 constructor_unfilled_index = p->unfilled_index;
5500 constructor_unfilled_fields = p->unfilled_fields;
5501 constructor_bit_index = p->bit_index;
5502 constructor_elements = p->elements;
5503 constructor_constant = p->constant;
5504 constructor_simple = p->simple;
5505 constructor_erroneous = p->erroneous;
5506 constructor_pending_elts = p->pending_elts;
5507 constructor_depth = p->depth;
5508 constructor_incremental = p->incremental;
5509 RESTORE_SPELLING_DEPTH (constructor_depth);
5511 constructor_stack = p->next;
5512 free (p);
5514 if (constructor == 0)
5516 if (constructor_stack == 0)
5517 return error_mark_node;
5518 return NULL_TREE;
5520 return constructor;
5523 /* Within an array initializer, specify the next index to be initialized.
5524 FIRST is that index. If LAST is nonzero, then initialize a range
5525 of indices, running from FIRST through LAST. */
5527 void
5528 set_init_index (first, last)
5529 tree first, last;
5531 while ((TREE_CODE (first) == NOP_EXPR
5532 || TREE_CODE (first) == CONVERT_EXPR
5533 || TREE_CODE (first) == NON_LVALUE_EXPR)
5534 && (TYPE_MODE (TREE_TYPE (first))
5535 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5536 (first) = TREE_OPERAND (first, 0);
5537 if (last)
5538 while ((TREE_CODE (last) == NOP_EXPR
5539 || TREE_CODE (last) == CONVERT_EXPR
5540 || TREE_CODE (last) == NON_LVALUE_EXPR)
5541 && (TYPE_MODE (TREE_TYPE (last))
5542 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5543 (last) = TREE_OPERAND (last, 0);
5545 if (TREE_CODE (first) != INTEGER_CST)
5546 error_init ("nonconstant array index in initializer");
5547 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5548 error_init ("nonconstant array index in initializer");
5549 else if (! constructor_unfilled_index)
5550 error_init ("array index in non-array initializer");
5551 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5552 error_init ("duplicate array index in initializer");
5553 else
5555 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (first);
5556 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (first);
5558 if (last != 0 && tree_int_cst_lt (last, first))
5559 error_init ("empty index range in initializer");
5560 else
5562 if (pedantic)
5563 pedwarn ("ANSI C forbids specifying element to initialize");
5564 constructor_range_end = last;
5569 /* Within a struct initializer, specify the next field to be initialized. */
5571 void
5572 set_init_label (fieldname)
5573 tree fieldname;
5575 tree tail;
5576 int passed = 0;
5578 /* Don't die if an entire brace-pair level is superfluous
5579 in the containing level. */
5580 if (constructor_type == 0)
5581 return;
5583 for (tail = TYPE_FIELDS (constructor_type); tail;
5584 tail = TREE_CHAIN (tail))
5586 if (tail == constructor_unfilled_fields)
5587 passed = 1;
5588 if (DECL_NAME (tail) == fieldname)
5589 break;
5592 if (tail == 0)
5593 error ("unknown field `%s' specified in initializer",
5594 IDENTIFIER_POINTER (fieldname));
5595 else if (!passed)
5596 error ("field `%s' already initialized",
5597 IDENTIFIER_POINTER (fieldname));
5598 else
5600 constructor_fields = tail;
5601 if (pedantic)
5602 pedwarn ("ANSI C forbids specifying structure member to initialize");
5606 /* Add a new initializer to the tree of pending initializers. PURPOSE
5607 indentifies the initializer, either array index or field in a structure.
5608 VALUE is the value of that index or field. */
5610 static void
5611 add_pending_init (purpose, value)
5612 tree purpose, value;
5614 struct init_node *p, **q, *r;
5616 q = &constructor_pending_elts;
5617 p = 0;
5619 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5621 while (*q != 0)
5623 p = *q;
5624 if (tree_int_cst_lt (purpose, p->purpose))
5625 q = &p->left;
5626 else if (tree_int_cst_lt (p->purpose, purpose))
5627 q = &p->right;
5628 else
5629 abort ();
5632 else
5634 while (*q != NULL)
5636 p = *q;
5637 if (tree_int_cst_lt (DECL_FIELD_BITPOS (purpose),
5638 DECL_FIELD_BITPOS (p->purpose)))
5639 q = &p->left;
5640 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (p->purpose),
5641 DECL_FIELD_BITPOS (purpose)))
5642 q = &p->right;
5643 else
5644 abort ();
5648 r = (struct init_node *) oballoc (sizeof (struct init_node));
5649 r->purpose = purpose;
5650 r->value = value;
5652 *q = r;
5653 r->parent = p;
5654 r->left = 0;
5655 r->right = 0;
5656 r->balance = 0;
5658 while (p)
5660 struct init_node *s;
5662 if (r == p->left)
5664 if (p->balance == 0)
5665 p->balance = -1;
5666 else if (p->balance < 0)
5668 if (r->balance < 0)
5670 /* L rotation. */
5671 p->left = r->right;
5672 if (p->left)
5673 p->left->parent = p;
5674 r->right = p;
5676 p->balance = 0;
5677 r->balance = 0;
5679 s = p->parent;
5680 p->parent = r;
5681 r->parent = s;
5682 if (s)
5684 if (s->left == p)
5685 s->left = r;
5686 else
5687 s->right = r;
5689 else
5690 constructor_pending_elts = r;
5692 else
5694 /* LR rotation. */
5695 struct init_node *t = r->right;
5697 r->right = t->left;
5698 if (r->right)
5699 r->right->parent = r;
5700 t->left = r;
5702 p->left = t->right;
5703 if (p->left)
5704 p->left->parent = p;
5705 t->right = p;
5707 p->balance = t->balance < 0;
5708 r->balance = -(t->balance > 0);
5709 t->balance = 0;
5711 s = p->parent;
5712 p->parent = t;
5713 r->parent = t;
5714 t->parent = s;
5715 if (s)
5717 if (s->left == p)
5718 s->left = t;
5719 else
5720 s->right = t;
5722 else
5723 constructor_pending_elts = t;
5725 break;
5727 else
5729 /* p->balance == +1; growth of left side balances the node. */
5730 p->balance = 0;
5731 break;
5734 else /* r == p->right */
5736 if (p->balance == 0)
5737 /* Growth propagation from right side. */
5738 p->balance++;
5739 else if (p->balance > 0)
5741 if (r->balance > 0)
5743 /* R rotation. */
5744 p->right = r->left;
5745 if (p->right)
5746 p->right->parent = p;
5747 r->left = p;
5749 p->balance = 0;
5750 r->balance = 0;
5752 s = p->parent;
5753 p->parent = r;
5754 r->parent = s;
5755 if (s)
5757 if (s->left == p)
5758 s->left = r;
5759 else
5760 s->right = r;
5762 else
5763 constructor_pending_elts = r;
5765 else /* r->balance == -1 */
5767 /* RL rotation */
5768 struct init_node *t = r->left;
5770 r->left = t->right;
5771 if (r->left)
5772 r->left->parent = r;
5773 t->right = r;
5775 p->right = t->left;
5776 if (p->right)
5777 p->right->parent = p;
5778 t->left = p;
5780 r->balance = (t->balance < 0);
5781 p->balance = -(t->balance > 0);
5782 t->balance = 0;
5784 s = p->parent;
5785 p->parent = t;
5786 r->parent = t;
5787 t->parent = s;
5788 if (s)
5790 if (s->left == p)
5791 s->left = t;
5792 else
5793 s->right = t;
5795 else
5796 constructor_pending_elts = t;
5798 break;
5800 else
5802 /* p->balance == -1; growth of right side balances the node. */
5803 p->balance = 0;
5804 break;
5808 r = p;
5809 p = p->parent;
5813 /* Return nonzero if FIELD is equal to the index of a pending initializer. */
5815 static int
5816 pending_init_member (field)
5817 tree field;
5819 struct init_node *p;
5821 p = constructor_pending_elts;
5822 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5824 while (p)
5826 if (tree_int_cst_equal (field, p->purpose))
5827 return 1;
5828 else if (tree_int_cst_lt (field, p->purpose))
5829 p = p->left;
5830 else
5831 p = p->right;
5834 else
5836 while (p)
5838 if (field == p->purpose)
5839 return 1;
5840 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (field),
5841 DECL_FIELD_BITPOS (p->purpose)))
5842 p = p->left;
5843 else
5844 p = p->right;
5848 return 0;
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.
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.) */
5861 static void
5862 output_init_element (value, type, field, pending)
5863 tree value, type, field;
5864 int pending;
5866 int duplicate = 0;
5868 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5869 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5870 && !(TREE_CODE (value) == STRING_CST
5871 && TREE_CODE (type) == ARRAY_TYPE
5872 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5873 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5874 TYPE_MAIN_VARIANT (type))))
5875 value = default_conversion (value);
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 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5883 || TREE_CODE (constructor_type) == UNION_TYPE)
5884 && DECL_C_BIT_FIELD (field)
5885 && TREE_CODE (value) != INTEGER_CST))
5886 constructor_simple = 0;
5888 if (require_constant_value && ! TREE_CONSTANT (value))
5890 error_init ("initializer element is not constant");
5891 value = error_mark_node;
5893 else if (require_constant_elements
5894 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5896 error_init ("initializer element is not computable at load time");
5897 value = error_mark_node;
5900 /* If this element duplicates one on constructor_pending_elts,
5901 print a message and ignore it. Don't do this when we're
5902 processing elements taken off constructor_pending_elts,
5903 because we'd always get spurious errors. */
5904 if (pending)
5906 if (TREE_CODE (constructor_type) == RECORD_TYPE
5907 || TREE_CODE (constructor_type) == UNION_TYPE
5908 || TREE_CODE (constructor_type) == ARRAY_TYPE)
5910 if (pending_init_member (field))
5912 error_init ("duplicate initializer");
5913 duplicate = 1;
5918 /* If this element doesn't come next in sequence,
5919 put it on constructor_pending_elts. */
5920 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5921 && !tree_int_cst_equal (field, constructor_unfilled_index))
5923 if (! duplicate)
5924 /* The copy_node is needed in case field is actually
5925 constructor_index, which is modified in place. */
5926 add_pending_init (copy_node (field),
5927 digest_init (type, value, require_constant_value,
5928 require_constant_elements));
5930 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5931 && field != constructor_unfilled_fields)
5933 /* We do this for records but not for unions. In a union,
5934 no matter which field is specified, it can be initialized
5935 right away since it starts at the beginning of the union. */
5936 if (!duplicate)
5937 add_pending_init (field,
5938 digest_init (type, value, require_constant_value,
5939 require_constant_elements));
5941 else
5943 /* Otherwise, output this element either to
5944 constructor_elements or to the assembler file. */
5946 if (!duplicate)
5948 if (! constructor_incremental)
5950 if (field && TREE_CODE (field) == INTEGER_CST)
5951 field = copy_node (field);
5952 constructor_elements
5953 = tree_cons (field, digest_init (type, value,
5954 require_constant_value,
5955 require_constant_elements),
5956 constructor_elements);
5958 else
5960 /* Structure elements may require alignment.
5961 Do this, if necessary. */
5962 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5964 /* Advance to offset of this element. */
5965 if (! tree_int_cst_equal (constructor_bit_index,
5966 DECL_FIELD_BITPOS (field)))
5968 /* By using unsigned arithmetic, the result will be
5969 correct even in case of overflows, if BITS_PER_UNIT
5970 is a power of two. */
5971 unsigned next = (TREE_INT_CST_LOW
5972 (DECL_FIELD_BITPOS (field))
5973 / (unsigned)BITS_PER_UNIT);
5974 unsigned here = (TREE_INT_CST_LOW
5975 (constructor_bit_index)
5976 / (unsigned)BITS_PER_UNIT);
5978 assemble_zeros ((next - here)
5979 * (unsigned)BITS_PER_UNIT
5980 / (unsigned)BITS_PER_UNIT);
5983 output_constant (digest_init (type, value,
5984 require_constant_value,
5985 require_constant_elements),
5986 int_size_in_bytes (type));
5988 /* For a record or union,
5989 keep track of end position of last field. */
5990 if (TREE_CODE (constructor_type) == RECORD_TYPE
5991 || TREE_CODE (constructor_type) == UNION_TYPE)
5993 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5994 DECL_SIZE (field));
5995 TREE_INT_CST_LOW (constructor_bit_index)
5996 = TREE_INT_CST_LOW (temp);
5997 TREE_INT_CST_HIGH (constructor_bit_index)
5998 = TREE_INT_CST_HIGH (temp);
6003 /* Advance the variable that indicates sequential elements output. */
6004 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6006 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
6007 integer_one_node);
6008 TREE_INT_CST_LOW (constructor_unfilled_index)
6009 = TREE_INT_CST_LOW (tem);
6010 TREE_INT_CST_HIGH (constructor_unfilled_index)
6011 = TREE_INT_CST_HIGH (tem);
6013 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6015 constructor_unfilled_fields =
6016 TREE_CHAIN (constructor_unfilled_fields);
6017 /* Skip any nameless bit fields. */
6018 while (constructor_unfilled_fields != 0
6019 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6020 && DECL_NAME (constructor_unfilled_fields) == 0)
6021 constructor_unfilled_fields =
6022 TREE_CHAIN (constructor_unfilled_fields);
6024 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6025 constructor_unfilled_fields = 0;
6027 /* Now output any pending elements which have become next. */
6028 if (pending)
6029 output_pending_init_elements (0);
6033 /* Output any pending elements which have become next.
6034 As we output elements, constructor_unfilled_{fields,index}
6035 advances, which may cause other elements to become next;
6036 if so, they too are output.
6038 If ALL is 0, we return when there are
6039 no more pending elements to output now.
6041 If ALL is 1, we output space as necessary so that
6042 we can output all the pending elements. */
6044 static void
6045 output_pending_init_elements (all)
6046 int all;
6048 struct init_node *elt = constructor_pending_elts;
6049 tree next;
6051 retry:
6053 /* Look thru the whole pending tree.
6054 If we find an element that should be output now,
6055 output it. Otherwise, set NEXT to the element
6056 that comes first among those still pending. */
6058 next = 0;
6059 while (elt)
6061 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6063 if (tree_int_cst_equal (elt->purpose,
6064 constructor_unfilled_index))
6065 output_init_element (elt->value,
6066 TREE_TYPE (constructor_type),
6067 constructor_unfilled_index, 0);
6068 else if (tree_int_cst_lt (constructor_unfilled_index,
6069 elt->purpose))
6071 /* Advance to the next smaller node. */
6072 if (elt->left)
6073 elt = elt->left;
6074 else
6076 /* We have reached the smallest node bigger than the
6077 current unfilled index. Fill the space first. */
6078 next = elt->purpose;
6079 break;
6082 else
6084 /* Advance to the next bigger node. */
6085 if (elt->right)
6086 elt = elt->right;
6087 else
6089 /* We have reached the biggest node in a subtree. Find
6090 the parent of it, which is the next bigger node. */
6091 while (elt->parent && elt->parent->right == elt)
6092 elt = elt->parent;
6093 elt = elt->parent;
6094 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6095 elt->purpose))
6097 next = elt->purpose;
6098 break;
6103 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6104 || TREE_CODE (constructor_type) == UNION_TYPE)
6106 /* If the current record is complete we are done. */
6107 if (constructor_unfilled_fields == 0)
6108 break;
6109 if (elt->purpose == constructor_unfilled_fields)
6111 output_init_element (elt->value,
6112 TREE_TYPE (constructor_unfilled_fields),
6113 constructor_unfilled_fields,
6116 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6117 DECL_FIELD_BITPOS (elt->purpose)))
6119 /* Advance to the next smaller node. */
6120 if (elt->left)
6121 elt = elt->left;
6122 else
6124 /* We have reached the smallest node bigger than the
6125 current unfilled field. Fill the space first. */
6126 next = elt->purpose;
6127 break;
6130 else
6132 /* Advance to the next bigger node. */
6133 if (elt->right)
6134 elt = elt->right;
6135 else
6137 /* We have reached the biggest node in a subtree. Find
6138 the parent of it, which is the next bigger node. */
6139 while (elt->parent && elt->parent->right == elt)
6140 elt = elt->parent;
6141 elt = elt->parent;
6142 if (elt
6143 && tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6144 DECL_FIELD_BITPOS (elt->purpose)))
6146 next = elt->purpose;
6147 break;
6154 /* Ordinarily return, but not if we want to output all
6155 and there are elements left. */
6156 if (! (all && next != 0))
6157 return;
6159 /* Generate space up to the position of NEXT. */
6160 if (constructor_incremental)
6162 tree filled;
6163 tree nextpos_tree = size_int (0);
6165 if (TREE_CODE (constructor_type) == RECORD_TYPE
6166 || TREE_CODE (constructor_type) == UNION_TYPE)
6168 tree tail;
6169 /* Find the last field written out, if any. */
6170 for (tail = TYPE_FIELDS (constructor_type); tail;
6171 tail = TREE_CHAIN (tail))
6172 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6173 break;
6175 if (tail)
6176 /* Find the offset of the end of that field. */
6177 filled = size_binop (CEIL_DIV_EXPR,
6178 size_binop (PLUS_EXPR,
6179 DECL_FIELD_BITPOS (tail),
6180 DECL_SIZE (tail)),
6181 size_int (BITS_PER_UNIT));
6182 else
6183 filled = size_int (0);
6185 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6186 DECL_FIELD_BITPOS (next),
6187 size_int (BITS_PER_UNIT));
6189 TREE_INT_CST_HIGH (constructor_bit_index)
6190 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6191 TREE_INT_CST_LOW (constructor_bit_index)
6192 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6193 constructor_unfilled_fields = next;
6195 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6197 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6198 size_in_bytes (TREE_TYPE (constructor_type)));
6199 nextpos_tree
6200 = size_binop (MULT_EXPR, next,
6201 size_in_bytes (TREE_TYPE (constructor_type)));
6202 TREE_INT_CST_LOW (constructor_unfilled_index)
6203 = TREE_INT_CST_LOW (next);
6204 TREE_INT_CST_HIGH (constructor_unfilled_index)
6205 = TREE_INT_CST_HIGH (next);
6207 else
6208 filled = 0;
6210 if (filled)
6212 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6214 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6217 else
6219 /* If it's not incremental, just skip over the gap,
6220 so that after jumping to retry we will output the next
6221 successive element. */
6222 if (TREE_CODE (constructor_type) == RECORD_TYPE
6223 || TREE_CODE (constructor_type) == UNION_TYPE)
6224 constructor_unfilled_fields = next;
6225 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6227 TREE_INT_CST_LOW (constructor_unfilled_index)
6228 = TREE_INT_CST_LOW (next);
6229 TREE_INT_CST_HIGH (constructor_unfilled_index)
6230 = TREE_INT_CST_HIGH (next);
6234 /* ELT now points to the node in the pending tree with the next
6235 initializer to output. */
6236 goto retry;
6239 /* Add one non-braced element to the current constructor level.
6240 This adjusts the current position within the constructor's type.
6241 This may also start or terminate implicit levels
6242 to handle a partly-braced initializer.
6244 Once this has found the correct level for the new element,
6245 it calls output_init_element.
6247 Note: if we are incrementally outputting this constructor,
6248 this function may be called with a null argument
6249 representing a sub-constructor that was already incrementally output.
6250 When that happens, we output nothing, but we do the bookkeeping
6251 to skip past that element of the current constructor. */
6253 void
6254 process_init_element (value)
6255 tree value;
6257 tree orig_value = value;
6258 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6260 /* Handle superfluous braces around string cst as in
6261 char x[] = {"foo"}; */
6262 if (string_flag
6263 && constructor_type
6264 && TREE_CODE (constructor_type) == ARRAY_TYPE
6265 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6266 && integer_zerop (constructor_unfilled_index))
6268 if (constructor_stack->replacement_value)
6269 error_init ("excess elements in char array initializer");
6270 constructor_stack->replacement_value = value;
6271 return;
6274 if (constructor_stack->replacement_value != 0)
6276 error_init ("excess elements in struct initializer");
6277 return;
6280 /* Ignore elements of a brace group if it is entirely superfluous
6281 and has already been diagnosed. */
6282 if (constructor_type == 0)
6283 return;
6285 /* If we've exhausted any levels that didn't have braces,
6286 pop them now. */
6287 while (constructor_stack->implicit)
6289 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6290 || TREE_CODE (constructor_type) == UNION_TYPE)
6291 && constructor_fields == 0)
6292 process_init_element (pop_init_level (1));
6293 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6294 && (constructor_max_index == 0
6295 || tree_int_cst_lt (constructor_max_index,
6296 constructor_index)))
6297 process_init_element (pop_init_level (1));
6298 else
6299 break;
6302 while (1)
6304 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6306 tree fieldtype;
6307 enum tree_code fieldcode;
6309 if (constructor_fields == 0)
6311 pedwarn_init ("excess elements in struct initializer");
6312 break;
6315 fieldtype = TREE_TYPE (constructor_fields);
6316 if (fieldtype != error_mark_node)
6317 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6318 fieldcode = TREE_CODE (fieldtype);
6320 /* Accept a string constant to initialize a subarray. */
6321 if (value != 0
6322 && fieldcode == ARRAY_TYPE
6323 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6324 && string_flag)
6325 value = orig_value;
6326 /* Otherwise, if we have come to a subaggregate,
6327 and we don't have an element of its type, push into it. */
6328 else if (value != 0 && !constructor_no_implicit
6329 && value != error_mark_node
6330 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6331 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6332 || fieldcode == UNION_TYPE))
6334 push_init_level (1);
6335 continue;
6338 if (value)
6340 push_member_name (constructor_fields);
6341 output_init_element (value, fieldtype, constructor_fields, 1);
6342 RESTORE_SPELLING_DEPTH (constructor_depth);
6344 else
6345 /* Do the bookkeeping for an element that was
6346 directly output as a constructor. */
6348 /* For a record, keep track of end position of last field. */
6349 tree temp = size_binop (PLUS_EXPR,
6350 DECL_FIELD_BITPOS (constructor_fields),
6351 DECL_SIZE (constructor_fields));
6352 TREE_INT_CST_LOW (constructor_bit_index)
6353 = TREE_INT_CST_LOW (temp);
6354 TREE_INT_CST_HIGH (constructor_bit_index)
6355 = TREE_INT_CST_HIGH (temp);
6357 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6358 /* Skip any nameless bit fields. */
6359 while (constructor_unfilled_fields != 0
6360 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6361 && DECL_NAME (constructor_unfilled_fields) == 0)
6362 constructor_unfilled_fields =
6363 TREE_CHAIN (constructor_unfilled_fields);
6366 constructor_fields = TREE_CHAIN (constructor_fields);
6367 /* Skip any nameless bit fields at the beginning. */
6368 while (constructor_fields != 0
6369 && DECL_C_BIT_FIELD (constructor_fields)
6370 && DECL_NAME (constructor_fields) == 0)
6371 constructor_fields = TREE_CHAIN (constructor_fields);
6372 break;
6374 if (TREE_CODE (constructor_type) == UNION_TYPE)
6376 tree fieldtype;
6377 enum tree_code fieldcode;
6379 if (constructor_fields == 0)
6381 pedwarn_init ("excess elements in union initializer");
6382 break;
6385 fieldtype = TREE_TYPE (constructor_fields);
6386 if (fieldtype != error_mark_node)
6387 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6388 fieldcode = TREE_CODE (fieldtype);
6390 /* Accept a string constant to initialize a subarray. */
6391 if (value != 0
6392 && fieldcode == ARRAY_TYPE
6393 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6394 && string_flag)
6395 value = orig_value;
6396 /* Otherwise, if we have come to a subaggregate,
6397 and we don't have an element of its type, push into it. */
6398 else if (value != 0 && !constructor_no_implicit
6399 && value != error_mark_node
6400 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6401 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6402 || fieldcode == UNION_TYPE))
6404 push_init_level (1);
6405 continue;
6408 if (value)
6410 push_member_name (constructor_fields);
6411 output_init_element (value, fieldtype, constructor_fields, 1);
6412 RESTORE_SPELLING_DEPTH (constructor_depth);
6414 else
6415 /* Do the bookkeeping for an element that was
6416 directly output as a constructor. */
6418 TREE_INT_CST_LOW (constructor_bit_index)
6419 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6420 TREE_INT_CST_HIGH (constructor_bit_index)
6421 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6423 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6426 constructor_fields = 0;
6427 break;
6429 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6431 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6432 enum tree_code eltcode = TREE_CODE (elttype);
6434 /* Accept a string constant to initialize a subarray. */
6435 if (value != 0
6436 && eltcode == ARRAY_TYPE
6437 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6438 && string_flag)
6439 value = orig_value;
6440 /* Otherwise, if we have come to a subaggregate,
6441 and we don't have an element of its type, push into it. */
6442 else if (value != 0 && !constructor_no_implicit
6443 && value != error_mark_node
6444 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6445 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6446 || eltcode == UNION_TYPE))
6448 push_init_level (1);
6449 continue;
6452 if (constructor_max_index != 0
6453 && tree_int_cst_lt (constructor_max_index, constructor_index))
6455 pedwarn_init ("excess elements in array initializer");
6456 break;
6459 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6460 if (constructor_range_end)
6462 if (constructor_max_index != 0
6463 && tree_int_cst_lt (constructor_max_index,
6464 constructor_range_end))
6466 pedwarn_init ("excess elements in array initializer");
6467 TREE_INT_CST_HIGH (constructor_range_end)
6468 = TREE_INT_CST_HIGH (constructor_max_index);
6469 TREE_INT_CST_LOW (constructor_range_end)
6470 = TREE_INT_CST_LOW (constructor_max_index);
6473 value = save_expr (value);
6476 /* Now output the actual element.
6477 Ordinarily, output once.
6478 If there is a range, repeat it till we advance past the range. */
6481 tree tem;
6483 if (value)
6485 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6486 output_init_element (value, elttype, constructor_index, 1);
6487 RESTORE_SPELLING_DEPTH (constructor_depth);
6490 tem = size_binop (PLUS_EXPR, constructor_index,
6491 integer_one_node);
6492 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (tem);
6493 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (tem);
6495 if (!value)
6496 /* If we are doing the bookkeeping for an element that was
6497 directly output as a constructor,
6498 we must update constructor_unfilled_index. */
6500 TREE_INT_CST_LOW (constructor_unfilled_index)
6501 = TREE_INT_CST_LOW (constructor_index);
6502 TREE_INT_CST_HIGH (constructor_unfilled_index)
6503 = TREE_INT_CST_HIGH (constructor_index);
6506 while (! (constructor_range_end == 0
6507 || tree_int_cst_lt (constructor_range_end,
6508 constructor_index)));
6510 break;
6513 /* Handle the sole element allowed in a braced initializer
6514 for a scalar variable. */
6515 if (constructor_fields == 0)
6517 pedwarn_init ("excess elements in scalar initializer");
6518 break;
6521 if (value)
6522 output_init_element (value, constructor_type, NULL_TREE, 1);
6523 constructor_fields = 0;
6524 break;
6527 /* If the (lexically) previous elments are not now saved,
6528 we can discard the storage for them. */
6529 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6530 && constructor_stack == 0)
6531 clear_momentary ();
6534 /* Expand an ASM statement with operands, handling output operands
6535 that are not variables or INDIRECT_REFS by transforming such
6536 cases into cases that expand_asm_operands can handle.
6538 Arguments are same as for expand_asm_operands. */
6540 void
6541 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6542 tree string, outputs, inputs, clobbers;
6543 int vol;
6544 char *filename;
6545 int line;
6547 int noutputs = list_length (outputs);
6548 register int i;
6549 /* o[I] is the place that output number I should be written. */
6550 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6551 register tree tail;
6553 if (TREE_CODE (string) == ADDR_EXPR)
6554 string = TREE_OPERAND (string, 0);
6555 if (TREE_CODE (string) != STRING_CST)
6557 error ("asm template is not a string constant");
6558 return;
6561 /* Record the contents of OUTPUTS before it is modified. */
6562 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6563 o[i] = TREE_VALUE (tail);
6565 /* Perform default conversions on array and function inputs. */
6566 /* Don't do this for other types--
6567 it would screw up operands expected to be in memory. */
6568 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6569 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6570 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6571 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6573 /* Generate the ASM_OPERANDS insn;
6574 store into the TREE_VALUEs of OUTPUTS some trees for
6575 where the values were actually stored. */
6576 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6578 /* Copy all the intermediate outputs into the specified outputs. */
6579 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6581 if (o[i] != TREE_VALUE (tail))
6583 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6584 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6585 free_temp_slots ();
6587 /* Detect modification of read-only values.
6588 (Otherwise done by build_modify_expr.) */
6589 else
6591 tree type = TREE_TYPE (o[i]);
6592 if (TREE_READONLY (o[i])
6593 || TYPE_READONLY (type)
6594 || ((TREE_CODE (type) == RECORD_TYPE
6595 || TREE_CODE (type) == UNION_TYPE)
6596 && C_TYPE_FIELDS_READONLY (type)))
6597 readonly_warning (o[i], "modification by `asm'");
6601 /* Those MODIFY_EXPRs could do autoincrements. */
6602 emit_queue ();
6605 /* Expand a C `return' statement.
6606 RETVAL is the expression for what to return,
6607 or a null pointer for `return;' with no value. */
6609 void
6610 c_expand_return (retval)
6611 tree retval;
6613 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6615 if (TREE_THIS_VOLATILE (current_function_decl))
6616 warning ("function declared `noreturn' has a `return' statement");
6618 if (!retval)
6620 current_function_returns_null = 1;
6621 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6622 warning ("`return' with no value, in function returning non-void");
6623 expand_null_return ();
6625 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6627 current_function_returns_null = 1;
6628 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6629 pedwarn ("`return' with a value, in function returning void");
6630 expand_return (retval);
6632 else
6634 tree t = convert_for_assignment (valtype, retval, _("return"),
6635 NULL_TREE, NULL_TREE, 0);
6636 tree res = DECL_RESULT (current_function_decl);
6637 tree inner;
6639 if (t == error_mark_node)
6640 return;
6642 inner = t = convert (TREE_TYPE (res), t);
6644 /* Strip any conversions, additions, and subtractions, and see if
6645 we are returning the address of a local variable. Warn if so. */
6646 while (1)
6648 switch (TREE_CODE (inner))
6650 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6651 case PLUS_EXPR:
6652 inner = TREE_OPERAND (inner, 0);
6653 continue;
6655 case MINUS_EXPR:
6656 /* If the second operand of the MINUS_EXPR has a pointer
6657 type (or is converted from it), this may be valid, so
6658 don't give a warning. */
6660 tree op1 = TREE_OPERAND (inner, 1);
6662 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6663 && (TREE_CODE (op1) == NOP_EXPR
6664 || TREE_CODE (op1) == NON_LVALUE_EXPR
6665 || TREE_CODE (op1) == CONVERT_EXPR))
6666 op1 = TREE_OPERAND (op1, 0);
6668 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6669 break;
6671 inner = TREE_OPERAND (inner, 0);
6672 continue;
6675 case ADDR_EXPR:
6676 inner = TREE_OPERAND (inner, 0);
6678 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6679 inner = TREE_OPERAND (inner, 0);
6681 if (TREE_CODE (inner) == VAR_DECL
6682 && ! DECL_EXTERNAL (inner)
6683 && ! TREE_STATIC (inner)
6684 && DECL_CONTEXT (inner) == current_function_decl)
6685 warning ("function returns address of local variable");
6686 break;
6688 default:
6689 break;
6692 break;
6695 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6696 TREE_SIDE_EFFECTS (t) = 1;
6697 expand_return (t);
6698 current_function_returns_value = 1;
6702 /* Start a C switch statement, testing expression EXP.
6703 Return EXP if it is valid, an error node otherwise. */
6705 tree
6706 c_expand_start_case (exp)
6707 tree exp;
6709 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6710 tree type = TREE_TYPE (exp);
6712 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6714 error ("switch quantity not an integer");
6715 exp = error_mark_node;
6717 else
6719 tree index;
6720 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6722 if (warn_traditional
6723 && (type == long_integer_type_node
6724 || type == long_unsigned_type_node))
6725 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6727 exp = default_conversion (exp);
6728 type = TREE_TYPE (exp);
6729 index = get_unwidened (exp, NULL_TREE);
6730 /* We can't strip a conversion from a signed type to an unsigned,
6731 because if we did, int_fits_type_p would do the wrong thing
6732 when checking case values for being in range,
6733 and it's too hard to do the right thing. */
6734 if (TREE_UNSIGNED (TREE_TYPE (exp))
6735 == TREE_UNSIGNED (TREE_TYPE (index)))
6736 exp = index;
6739 expand_start_case (1, exp, type, "switch statement");
6741 return exp;