* loop-invariant.c: New file.
[official-gcc.git] / gcc / c-typeck.c
blob99bb78d7c1d75fb8fbe07f203d6483f78db16209
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "rtl.h"
37 #include "tree.h"
38 #include "langhooks.h"
39 #include "c-tree.h"
40 #include "tm_p.h"
41 #include "flags.h"
42 #include "output.h"
43 #include "expr.h"
44 #include "toplev.h"
45 #include "intl.h"
46 #include "ggc.h"
47 #include "target.h"
48 #include "tree-iterator.h"
51 /* Nonzero if we've already printed a "missing braces around initializer"
52 message within this initializer. */
53 static int missing_braces_mentioned;
55 static int require_constant_value;
56 static int require_constant_elements;
58 static tree qualify_type (tree, tree);
59 static int tagged_types_tu_compatible_p (tree, tree);
60 static int comp_target_types (tree, tree, int);
61 static int function_types_compatible_p (tree, tree);
62 static int type_lists_compatible_p (tree, tree);
63 static tree decl_constant_value_for_broken_optimization (tree);
64 static tree default_function_array_conversion (tree);
65 static tree lookup_field (tree, tree);
66 static tree convert_arguments (tree, tree, tree, tree);
67 static tree pointer_diff (tree, tree);
68 static tree internal_build_compound_expr (tree, int);
69 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
70 int);
71 static void warn_for_assignment (const char *, const char *, tree, int);
72 static tree valid_compound_expr_initializer (tree, tree);
73 static void push_string (const char *);
74 static void push_member_name (tree);
75 static void push_array_bounds (int);
76 static int spelling_length (void);
77 static char *print_spelling (char *);
78 static void warning_init (const char *);
79 static tree digest_init (tree, tree, int);
80 static void output_init_element (tree, tree, tree, int);
81 static void output_pending_init_elements (int);
82 static int set_designator (int);
83 static void push_range_stack (tree);
84 static void add_pending_init (tree, tree);
85 static void set_nonincremental_init (void);
86 static void set_nonincremental_init_from_string (tree);
87 static tree find_init_member (tree);
88 static int lvalue_or_else (tree, const char *);
90 /* Do `exp = require_complete_type (exp);' to make sure exp
91 does not have an incomplete type. (That includes void types.) */
93 tree
94 require_complete_type (tree value)
96 tree type = TREE_TYPE (value);
98 if (value == error_mark_node || type == error_mark_node)
99 return error_mark_node;
101 /* First, detect a valid value with a complete type. */
102 if (COMPLETE_TYPE_P (type))
103 return value;
105 c_incomplete_type_error (value, type);
106 return error_mark_node;
109 /* Print an error message for invalid use of an incomplete type.
110 VALUE is the expression that was used (or 0 if that isn't known)
111 and TYPE is the type that was invalid. */
113 void
114 c_incomplete_type_error (tree value, tree type)
116 const char *type_code_string;
118 /* Avoid duplicate error message. */
119 if (TREE_CODE (type) == ERROR_MARK)
120 return;
122 if (value != 0 && (TREE_CODE (value) == VAR_DECL
123 || TREE_CODE (value) == PARM_DECL))
124 error ("`%s' has an incomplete type",
125 IDENTIFIER_POINTER (DECL_NAME (value)));
126 else
128 retry:
129 /* We must print an error message. Be clever about what it says. */
131 switch (TREE_CODE (type))
133 case RECORD_TYPE:
134 type_code_string = "struct";
135 break;
137 case UNION_TYPE:
138 type_code_string = "union";
139 break;
141 case ENUMERAL_TYPE:
142 type_code_string = "enum";
143 break;
145 case VOID_TYPE:
146 error ("invalid use of void expression");
147 return;
149 case ARRAY_TYPE:
150 if (TYPE_DOMAIN (type))
152 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
154 error ("invalid use of flexible array member");
155 return;
157 type = TREE_TYPE (type);
158 goto retry;
160 error ("invalid use of array with unspecified bounds");
161 return;
163 default:
164 abort ();
167 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
168 error ("invalid use of undefined type `%s %s'",
169 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
170 else
171 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
172 error ("invalid use of incomplete typedef `%s'",
173 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
177 /* Given a type, apply default promotions wrt unnamed function
178 arguments and return the new type. */
180 tree
181 c_type_promotes_to (tree type)
183 if (TYPE_MAIN_VARIANT (type) == float_type_node)
184 return double_type_node;
186 if (c_promoting_integer_type_p (type))
188 /* Preserve unsignedness if not really getting any wider. */
189 if (TYPE_UNSIGNED (type)
190 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
191 return unsigned_type_node;
192 return integer_type_node;
195 return type;
198 /* Return a variant of TYPE which has all the type qualifiers of LIKE
199 as well as those of TYPE. */
201 static tree
202 qualify_type (tree type, tree like)
204 return c_build_qualified_type (type,
205 TYPE_QUALS (type) | TYPE_QUALS (like));
208 /* Return the composite type of two compatible types.
210 We assume that comptypes has already been done and returned
211 nonzero; if that isn't so, this may crash. In particular, we
212 assume that qualifiers match. */
214 tree
215 composite_type (tree t1, tree t2)
217 enum tree_code code1;
218 enum tree_code code2;
219 tree attributes;
221 /* Save time if the two types are the same. */
223 if (t1 == t2) return t1;
225 /* If one type is nonsense, use the other. */
226 if (t1 == error_mark_node)
227 return t2;
228 if (t2 == error_mark_node)
229 return t1;
231 code1 = TREE_CODE (t1);
232 code2 = TREE_CODE (t2);
234 /* Merge the attributes. */
235 attributes = targetm.merge_type_attributes (t1, t2);
237 /* If one is an enumerated type and the other is the compatible
238 integer type, the composite type might be either of the two
239 (DR#013 question 3). For consistency, use the enumerated type as
240 the composite type. */
242 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
243 return t1;
244 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
245 return t2;
247 if (code1 != code2)
248 abort ();
250 switch (code1)
252 case POINTER_TYPE:
253 /* For two pointers, do this recursively on the target type. */
255 tree pointed_to_1 = TREE_TYPE (t1);
256 tree pointed_to_2 = TREE_TYPE (t2);
257 tree target = composite_type (pointed_to_1, pointed_to_2);
258 t1 = build_pointer_type (target);
259 return build_type_attribute_variant (t1, attributes);
262 case ARRAY_TYPE:
264 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
265 /* Save space: see if the result is identical to one of the args. */
266 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
267 return build_type_attribute_variant (t1, attributes);
268 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
269 return build_type_attribute_variant (t2, attributes);
270 /* Merge the element types, and have a size if either arg has one. */
271 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
272 return build_type_attribute_variant (t1, attributes);
275 case FUNCTION_TYPE:
276 /* Function types: prefer the one that specified arg types.
277 If both do, merge the arg types. Also merge the return types. */
279 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
280 tree p1 = TYPE_ARG_TYPES (t1);
281 tree p2 = TYPE_ARG_TYPES (t2);
282 int len;
283 tree newargs, n;
284 int i;
286 /* Save space: see if the result is identical to one of the args. */
287 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
288 return build_type_attribute_variant (t1, attributes);
289 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
290 return build_type_attribute_variant (t2, attributes);
292 /* Simple way if one arg fails to specify argument types. */
293 if (TYPE_ARG_TYPES (t1) == 0)
295 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
296 return build_type_attribute_variant (t1, attributes);
298 if (TYPE_ARG_TYPES (t2) == 0)
300 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
301 return build_type_attribute_variant (t1, attributes);
304 /* If both args specify argument types, we must merge the two
305 lists, argument by argument. */
306 /* Tell global_bindings_p to return false so that variable_size
307 doesn't abort on VLAs in parameter types. */
308 c_override_global_bindings_to_false = true;
310 len = list_length (p1);
311 newargs = 0;
313 for (i = 0; i < len; i++)
314 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
316 n = newargs;
318 for (; p1;
319 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
321 /* A null type means arg type is not specified.
322 Take whatever the other function type has. */
323 if (TREE_VALUE (p1) == 0)
325 TREE_VALUE (n) = TREE_VALUE (p2);
326 goto parm_done;
328 if (TREE_VALUE (p2) == 0)
330 TREE_VALUE (n) = TREE_VALUE (p1);
331 goto parm_done;
334 /* Given wait (union {union wait *u; int *i} *)
335 and wait (union wait *),
336 prefer union wait * as type of parm. */
337 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
338 && TREE_VALUE (p1) != TREE_VALUE (p2))
340 tree memb;
341 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
342 memb; memb = TREE_CHAIN (memb))
343 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
345 TREE_VALUE (n) = TREE_VALUE (p2);
346 if (pedantic)
347 pedwarn ("function types not truly compatible in ISO C");
348 goto parm_done;
351 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
352 && TREE_VALUE (p2) != TREE_VALUE (p1))
354 tree memb;
355 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
356 memb; memb = TREE_CHAIN (memb))
357 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
359 TREE_VALUE (n) = TREE_VALUE (p1);
360 if (pedantic)
361 pedwarn ("function types not truly compatible in ISO C");
362 goto parm_done;
365 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
366 parm_done: ;
369 c_override_global_bindings_to_false = false;
370 t1 = build_function_type (valtype, newargs);
371 /* ... falls through ... */
374 default:
375 return build_type_attribute_variant (t1, attributes);
380 /* Return the type of a conditional expression between pointers to
381 possibly differently qualified versions of compatible types.
383 We assume that comp_target_types has already been done and returned
384 nonzero; if that isn't so, this may crash. */
386 static tree
387 common_pointer_type (tree t1, tree t2)
389 tree attributes;
390 tree pointed_to_1;
391 tree pointed_to_2;
392 tree target;
394 /* Save time if the two types are the same. */
396 if (t1 == t2) return t1;
398 /* If one type is nonsense, use the other. */
399 if (t1 == error_mark_node)
400 return t2;
401 if (t2 == error_mark_node)
402 return t1;
404 if (TREE_CODE (t1) != POINTER_TYPE || TREE_CODE (t2) != POINTER_TYPE)
405 abort ();
407 /* Merge the attributes. */
408 attributes = targetm.merge_type_attributes (t1, t2);
410 /* Find the composite type of the target types, and combine the
411 qualifiers of the two types' targets. */
412 pointed_to_1 = TREE_TYPE (t1);
413 pointed_to_2 = TREE_TYPE (t2);
414 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
415 TYPE_MAIN_VARIANT (pointed_to_2));
416 t1 = build_pointer_type (c_build_qualified_type
417 (target,
418 TYPE_QUALS (pointed_to_1) |
419 TYPE_QUALS (pointed_to_2)));
420 return build_type_attribute_variant (t1, attributes);
423 /* Return the common type for two arithmetic types under the usual
424 arithmetic conversions. The default conversions have already been
425 applied, and enumerated types converted to their compatible integer
426 types. The resulting type is unqualified and has no attributes.
428 This is the type for the result of most arithmetic operations
429 if the operands have the given two types. */
431 tree
432 common_type (tree t1, tree t2)
434 enum tree_code code1;
435 enum tree_code code2;
437 /* If one type is nonsense, use the other. */
438 if (t1 == error_mark_node)
439 return t2;
440 if (t2 == error_mark_node)
441 return t1;
443 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
444 t1 = TYPE_MAIN_VARIANT (t1);
446 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
447 t2 = TYPE_MAIN_VARIANT (t2);
449 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
450 t1 = build_type_attribute_variant (t1, NULL_TREE);
452 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
453 t2 = build_type_attribute_variant (t2, NULL_TREE);
455 /* Save time if the two types are the same. */
457 if (t1 == t2) return t1;
459 code1 = TREE_CODE (t1);
460 code2 = TREE_CODE (t2);
462 if (code1 != VECTOR_TYPE && code1 != COMPLEX_TYPE
463 && code1 != REAL_TYPE && code1 != INTEGER_TYPE)
464 abort ();
466 if (code2 != VECTOR_TYPE && code2 != COMPLEX_TYPE
467 && code2 != REAL_TYPE && code2 != INTEGER_TYPE)
468 abort ();
470 /* If one type is a vector type, return that type. (How the usual
471 arithmetic conversions apply to the vector types extension is not
472 precisely specified.) */
473 if (code1 == VECTOR_TYPE)
474 return t1;
476 if (code2 == VECTOR_TYPE)
477 return t2;
479 /* If one type is complex, form the common type of the non-complex
480 components, then make that complex. Use T1 or T2 if it is the
481 required type. */
482 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
484 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
485 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
486 tree subtype = common_type (subtype1, subtype2);
488 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
489 return t1;
490 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
491 return t2;
492 else
493 return build_complex_type (subtype);
496 /* If only one is real, use it as the result. */
498 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
499 return t1;
501 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
502 return t2;
504 /* Both real or both integers; use the one with greater precision. */
506 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
507 return t1;
508 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
509 return t2;
511 /* Same precision. Prefer long longs to longs to ints when the
512 same precision, following the C99 rules on integer type rank
513 (which are equivalent to the C90 rules for C90 types). */
515 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
516 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
517 return long_long_unsigned_type_node;
519 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
520 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
522 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
523 return long_long_unsigned_type_node;
524 else
525 return long_long_integer_type_node;
528 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
529 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
530 return long_unsigned_type_node;
532 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
533 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
535 /* But preserve unsignedness from the other type,
536 since long cannot hold all the values of an unsigned int. */
537 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
538 return long_unsigned_type_node;
539 else
540 return long_integer_type_node;
543 /* Likewise, prefer long double to double even if same size. */
544 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
545 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
546 return long_double_type_node;
548 /* Otherwise prefer the unsigned one. */
550 if (TYPE_UNSIGNED (t1))
551 return t1;
552 else
553 return t2;
556 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
557 or various other operations. Return 2 if they are compatible
558 but a warning may be needed if you use them together. */
561 comptypes (tree type1, tree type2)
563 tree t1 = type1;
564 tree t2 = type2;
565 int attrval, val;
567 /* Suppress errors caused by previously reported errors. */
569 if (t1 == t2 || !t1 || !t2
570 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
571 return 1;
573 /* If either type is the internal version of sizetype, return the
574 language version. */
575 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
576 && TYPE_ORIG_SIZE_TYPE (t1))
577 t1 = TYPE_ORIG_SIZE_TYPE (t1);
579 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
580 && TYPE_ORIG_SIZE_TYPE (t2))
581 t2 = TYPE_ORIG_SIZE_TYPE (t2);
584 /* Enumerated types are compatible with integer types, but this is
585 not transitive: two enumerated types in the same translation unit
586 are compatible with each other only if they are the same type. */
588 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
589 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
590 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
591 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
593 if (t1 == t2)
594 return 1;
596 /* Different classes of types can't be compatible. */
598 if (TREE_CODE (t1) != TREE_CODE (t2))
599 return 0;
601 /* Qualifiers must match. C99 6.7.3p9 */
603 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
604 return 0;
606 /* Allow for two different type nodes which have essentially the same
607 definition. Note that we already checked for equality of the type
608 qualifiers (just above). */
610 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
611 return 1;
613 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
614 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
615 return 0;
617 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
618 val = 0;
620 switch (TREE_CODE (t1))
622 case POINTER_TYPE:
623 /* We must give ObjC the first crack at comparing pointers, since
624 protocol qualifiers may be involved. */
625 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
626 break;
627 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
628 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
629 break;
631 case FUNCTION_TYPE:
632 val = function_types_compatible_p (t1, t2);
633 break;
635 case ARRAY_TYPE:
637 tree d1 = TYPE_DOMAIN (t1);
638 tree d2 = TYPE_DOMAIN (t2);
639 bool d1_variable, d2_variable;
640 bool d1_zero, d2_zero;
641 val = 1;
643 /* Target types must match incl. qualifiers. */
644 if (TREE_TYPE (t1) != TREE_TYPE (t2)
645 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
646 return 0;
648 /* Sizes must match unless one is missing or variable. */
649 if (d1 == 0 || d2 == 0 || d1 == d2)
650 break;
652 d1_zero = ! TYPE_MAX_VALUE (d1);
653 d2_zero = ! TYPE_MAX_VALUE (d2);
655 d1_variable = (! d1_zero
656 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
657 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
658 d2_variable = (! d2_zero
659 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
660 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
662 if (d1_variable || d2_variable)
663 break;
664 if (d1_zero && d2_zero)
665 break;
666 if (d1_zero || d2_zero
667 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
668 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
669 val = 0;
671 break;
674 case RECORD_TYPE:
675 /* We are dealing with two distinct structs. In assorted Objective-C
676 corner cases, however, these can still be deemed equivalent. */
677 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
678 val = 1;
680 case ENUMERAL_TYPE:
681 case UNION_TYPE:
682 if (val != 1 && !same_translation_unit_p (t1, t2))
683 val = tagged_types_tu_compatible_p (t1, t2);
684 break;
686 case VECTOR_TYPE:
687 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
688 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
689 break;
691 default:
692 break;
694 return attrval == 2 && val == 1 ? 2 : val;
697 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
698 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
699 to 1 or 0 depending if the check of the pointer types is meant to
700 be reflexive or not (typically, assignments are not reflexive,
701 while comparisons are reflexive).
704 static int
705 comp_target_types (tree ttl, tree ttr, int reflexive)
707 int val;
709 /* Give objc_comptypes a crack at letting these types through. */
710 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
711 return val;
713 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
714 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
716 if (val == 2 && pedantic)
717 pedwarn ("types are not quite compatible");
718 return val;
721 /* Subroutines of `comptypes'. */
723 /* Determine whether two trees derive from the same translation unit.
724 If the CONTEXT chain ends in a null, that tree's context is still
725 being parsed, so if two trees have context chains ending in null,
726 they're in the same translation unit. */
728 same_translation_unit_p (tree t1, tree t2)
730 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
731 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
733 case 'd': t1 = DECL_CONTEXT (t1); break;
734 case 't': t1 = TYPE_CONTEXT (t1); break;
735 case 'x': t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
736 default: abort ();
739 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
740 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
742 case 'd': t2 = DECL_CONTEXT (t2); break;
743 case 't': t2 = TYPE_CONTEXT (t2); break;
744 case 'x': t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
745 default: abort ();
748 return t1 == t2;
751 /* The C standard says that two structures in different translation
752 units are compatible with each other only if the types of their
753 fields are compatible (among other things). So, consider two copies
754 of this structure: */
756 struct tagged_tu_seen {
757 const struct tagged_tu_seen * next;
758 tree t1;
759 tree t2;
762 /* Can they be compatible with each other? We choose to break the
763 recursion by allowing those types to be compatible. */
765 static const struct tagged_tu_seen * tagged_tu_seen_base;
767 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
768 compatible. If the two types are not the same (which has been
769 checked earlier), this can only happen when multiple translation
770 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
771 rules. */
773 static int
774 tagged_types_tu_compatible_p (tree t1, tree t2)
776 tree s1, s2;
777 bool needs_warning = false;
779 /* We have to verify that the tags of the types are the same. This
780 is harder than it looks because this may be a typedef, so we have
781 to go look at the original type. It may even be a typedef of a
782 typedef...
783 In the case of compiler-created builtin structs the TYPE_DECL
784 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
785 while (TYPE_NAME (t1)
786 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
787 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
788 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
790 while (TYPE_NAME (t2)
791 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
792 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
793 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
795 /* C90 didn't have the requirement that the two tags be the same. */
796 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
797 return 0;
799 /* C90 didn't say what happened if one or both of the types were
800 incomplete; we choose to follow C99 rules here, which is that they
801 are compatible. */
802 if (TYPE_SIZE (t1) == NULL
803 || TYPE_SIZE (t2) == NULL)
804 return 1;
807 const struct tagged_tu_seen * tts_i;
808 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
809 if (tts_i->t1 == t1 && tts_i->t2 == t2)
810 return 1;
813 switch (TREE_CODE (t1))
815 case ENUMERAL_TYPE:
818 /* Speed up the case where the type values are in the same order. */
819 tree tv1 = TYPE_VALUES (t1);
820 tree tv2 = TYPE_VALUES (t2);
822 if (tv1 == tv2)
823 return 1;
825 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
827 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
828 break;
829 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
830 return 0;
833 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
834 return 1;
835 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
836 return 0;
838 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
839 return 0;
841 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
843 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
844 if (s2 == NULL
845 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
846 return 0;
848 return 1;
851 case UNION_TYPE:
853 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
854 return 0;
856 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
858 bool ok = false;
859 struct tagged_tu_seen tts;
861 tts.next = tagged_tu_seen_base;
862 tts.t1 = t1;
863 tts.t2 = t2;
864 tagged_tu_seen_base = &tts;
866 if (DECL_NAME (s1) != NULL)
867 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
868 if (DECL_NAME (s1) == DECL_NAME (s2))
870 int result;
871 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
872 if (result == 0)
873 break;
874 if (result == 2)
875 needs_warning = true;
877 if (TREE_CODE (s1) == FIELD_DECL
878 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
879 DECL_FIELD_BIT_OFFSET (s2)) != 1)
880 break;
882 ok = true;
883 break;
885 tagged_tu_seen_base = tts.next;
886 if (! ok)
887 return 0;
889 return needs_warning ? 2 : 1;
892 case RECORD_TYPE:
894 struct tagged_tu_seen tts;
896 tts.next = tagged_tu_seen_base;
897 tts.t1 = t1;
898 tts.t2 = t2;
899 tagged_tu_seen_base = &tts;
901 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
902 s1 && s2;
903 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
905 int result;
906 if (TREE_CODE (s1) != TREE_CODE (s2)
907 || DECL_NAME (s1) != DECL_NAME (s2))
908 break;
909 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
910 if (result == 0)
911 break;
912 if (result == 2)
913 needs_warning = true;
915 if (TREE_CODE (s1) == FIELD_DECL
916 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
917 DECL_FIELD_BIT_OFFSET (s2)) != 1)
918 break;
920 tagged_tu_seen_base = tts.next;
921 if (s1 && s2)
922 return 0;
923 return needs_warning ? 2 : 1;
926 default:
927 abort ();
931 /* Return 1 if two function types F1 and F2 are compatible.
932 If either type specifies no argument types,
933 the other must specify a fixed number of self-promoting arg types.
934 Otherwise, if one type specifies only the number of arguments,
935 the other must specify that number of self-promoting arg types.
936 Otherwise, the argument types must match. */
938 static int
939 function_types_compatible_p (tree f1, tree f2)
941 tree args1, args2;
942 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
943 int val = 1;
944 int val1;
945 tree ret1, ret2;
947 ret1 = TREE_TYPE (f1);
948 ret2 = TREE_TYPE (f2);
950 /* 'volatile' qualifiers on a function's return type mean the function
951 is noreturn. */
952 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
953 pedwarn ("function return types not compatible due to `volatile'");
954 if (TYPE_VOLATILE (ret1))
955 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
956 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
957 if (TYPE_VOLATILE (ret2))
958 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
959 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
960 val = comptypes (ret1, ret2);
961 if (val == 0)
962 return 0;
964 args1 = TYPE_ARG_TYPES (f1);
965 args2 = TYPE_ARG_TYPES (f2);
967 /* An unspecified parmlist matches any specified parmlist
968 whose argument types don't need default promotions. */
970 if (args1 == 0)
972 if (!self_promoting_args_p (args2))
973 return 0;
974 /* If one of these types comes from a non-prototype fn definition,
975 compare that with the other type's arglist.
976 If they don't match, ask for a warning (but no error). */
977 if (TYPE_ACTUAL_ARG_TYPES (f1)
978 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
979 val = 2;
980 return val;
982 if (args2 == 0)
984 if (!self_promoting_args_p (args1))
985 return 0;
986 if (TYPE_ACTUAL_ARG_TYPES (f2)
987 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
988 val = 2;
989 return val;
992 /* Both types have argument lists: compare them and propagate results. */
993 val1 = type_lists_compatible_p (args1, args2);
994 return val1 != 1 ? val1 : val;
997 /* Check two lists of types for compatibility,
998 returning 0 for incompatible, 1 for compatible,
999 or 2 for compatible with warning. */
1001 static int
1002 type_lists_compatible_p (tree args1, tree args2)
1004 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1005 int val = 1;
1006 int newval = 0;
1008 while (1)
1010 if (args1 == 0 && args2 == 0)
1011 return val;
1012 /* If one list is shorter than the other,
1013 they fail to match. */
1014 if (args1 == 0 || args2 == 0)
1015 return 0;
1016 /* A null pointer instead of a type
1017 means there is supposed to be an argument
1018 but nothing is specified about what type it has.
1019 So match anything that self-promotes. */
1020 if (TREE_VALUE (args1) == 0)
1022 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1023 return 0;
1025 else if (TREE_VALUE (args2) == 0)
1027 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1028 return 0;
1030 /* If one of the lists has an error marker, ignore this arg. */
1031 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1032 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1034 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1035 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
1037 /* Allow wait (union {union wait *u; int *i} *)
1038 and wait (union wait *) to be compatible. */
1039 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1040 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1041 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1042 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1043 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1044 TYPE_SIZE (TREE_VALUE (args2))))
1046 tree memb;
1047 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1048 memb; memb = TREE_CHAIN (memb))
1049 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
1050 break;
1051 if (memb == 0)
1052 return 0;
1054 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1055 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1056 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1057 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1058 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1059 TYPE_SIZE (TREE_VALUE (args1))))
1061 tree memb;
1062 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1063 memb; memb = TREE_CHAIN (memb))
1064 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
1065 break;
1066 if (memb == 0)
1067 return 0;
1069 else
1070 return 0;
1073 /* comptypes said ok, but record if it said to warn. */
1074 if (newval > val)
1075 val = newval;
1077 args1 = TREE_CHAIN (args1);
1078 args2 = TREE_CHAIN (args2);
1082 /* Compute the size to increment a pointer by. */
1084 tree
1085 c_size_in_bytes (tree type)
1087 enum tree_code code = TREE_CODE (type);
1089 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1090 return size_one_node;
1092 if (!COMPLETE_OR_VOID_TYPE_P (type))
1094 error ("arithmetic on pointer to an incomplete type");
1095 return size_one_node;
1098 /* Convert in case a char is more than one unit. */
1099 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1100 size_int (TYPE_PRECISION (char_type_node)
1101 / BITS_PER_UNIT));
1104 /* Return either DECL or its known constant value (if it has one). */
1106 tree
1107 decl_constant_value (tree decl)
1109 if (/* Don't change a variable array bound or initial value to a constant
1110 in a place where a variable is invalid. Note that DECL_INITIAL
1111 isn't valid for a PARM_DECL. */
1112 current_function_decl != 0
1113 && TREE_CODE (decl) != PARM_DECL
1114 && ! TREE_THIS_VOLATILE (decl)
1115 && TREE_READONLY (decl)
1116 && DECL_INITIAL (decl) != 0
1117 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1118 /* This is invalid if initial value is not constant.
1119 If it has either a function call, a memory reference,
1120 or a variable, then re-evaluating it could give different results. */
1121 && TREE_CONSTANT (DECL_INITIAL (decl))
1122 /* Check for cases where this is sub-optimal, even though valid. */
1123 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1124 return DECL_INITIAL (decl);
1125 return decl;
1128 /* Return either DECL or its known constant value (if it has one), but
1129 return DECL if pedantic or DECL has mode BLKmode. This is for
1130 bug-compatibility with the old behavior of decl_constant_value
1131 (before GCC 3.0); every use of this function is a bug and it should
1132 be removed before GCC 3.1. It is not appropriate to use pedantic
1133 in a way that affects optimization, and BLKmode is probably not the
1134 right test for avoiding misoptimizations either. */
1136 static tree
1137 decl_constant_value_for_broken_optimization (tree decl)
1139 if (pedantic || DECL_MODE (decl) == BLKmode)
1140 return decl;
1141 else
1142 return decl_constant_value (decl);
1146 /* Perform the default conversion of arrays and functions to pointers.
1147 Return the result of converting EXP. For any other expression, just
1148 return EXP. */
1150 static tree
1151 default_function_array_conversion (tree exp)
1153 tree orig_exp;
1154 tree type = TREE_TYPE (exp);
1155 enum tree_code code = TREE_CODE (type);
1156 int not_lvalue = 0;
1158 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1159 an lvalue.
1161 Do not use STRIP_NOPS here! It will remove conversions from pointer
1162 to integer and cause infinite recursion. */
1163 orig_exp = exp;
1164 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1165 || (TREE_CODE (exp) == NOP_EXPR
1166 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1168 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1169 not_lvalue = 1;
1170 exp = TREE_OPERAND (exp, 0);
1173 /* Preserve the original expression code. */
1174 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1175 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1177 if (code == FUNCTION_TYPE)
1179 return build_unary_op (ADDR_EXPR, exp, 0);
1181 if (code == ARRAY_TYPE)
1183 tree adr;
1184 tree restype = TREE_TYPE (type);
1185 tree ptrtype;
1186 int constp = 0;
1187 int volatilep = 0;
1188 int lvalue_array_p;
1190 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1192 constp = TREE_READONLY (exp);
1193 volatilep = TREE_THIS_VOLATILE (exp);
1196 if (TYPE_QUALS (type) || constp || volatilep)
1197 restype
1198 = c_build_qualified_type (restype,
1199 TYPE_QUALS (type)
1200 | (constp * TYPE_QUAL_CONST)
1201 | (volatilep * TYPE_QUAL_VOLATILE));
1203 if (TREE_CODE (exp) == INDIRECT_REF)
1204 return convert (build_pointer_type (restype),
1205 TREE_OPERAND (exp, 0));
1207 if (TREE_CODE (exp) == COMPOUND_EXPR)
1209 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1210 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1211 TREE_OPERAND (exp, 0), op1);
1214 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1215 if (!flag_isoc99 && !lvalue_array_p)
1217 /* Before C99, non-lvalue arrays do not decay to pointers.
1218 Normally, using such an array would be invalid; but it can
1219 be used correctly inside sizeof or as a statement expression.
1220 Thus, do not give an error here; an error will result later. */
1221 return exp;
1224 ptrtype = build_pointer_type (restype);
1226 if (TREE_CODE (exp) == VAR_DECL)
1228 /* ??? This is not really quite correct
1229 in that the type of the operand of ADDR_EXPR
1230 is not the target type of the type of the ADDR_EXPR itself.
1231 Question is, can this lossage be avoided? */
1232 adr = build1 (ADDR_EXPR, ptrtype, exp);
1233 if (!c_mark_addressable (exp))
1234 return error_mark_node;
1235 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1236 return adr;
1238 /* This way is better for a COMPONENT_REF since it can
1239 simplify the offset for a component. */
1240 adr = build_unary_op (ADDR_EXPR, exp, 1);
1241 return convert (ptrtype, adr);
1243 return exp;
1246 /* Perform default promotions for C data used in expressions.
1247 Arrays and functions are converted to pointers;
1248 enumeral types or short or char, to int.
1249 In addition, manifest constants symbols are replaced by their values. */
1251 tree
1252 default_conversion (tree exp)
1254 tree orig_exp;
1255 tree type = TREE_TYPE (exp);
1256 enum tree_code code = TREE_CODE (type);
1258 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1259 return default_function_array_conversion (exp);
1261 /* Constants can be used directly unless they're not loadable. */
1262 if (TREE_CODE (exp) == CONST_DECL)
1263 exp = DECL_INITIAL (exp);
1265 /* Replace a nonvolatile const static variable with its value unless
1266 it is an array, in which case we must be sure that taking the
1267 address of the array produces consistent results. */
1268 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1270 exp = decl_constant_value_for_broken_optimization (exp);
1271 type = TREE_TYPE (exp);
1274 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1275 an lvalue.
1277 Do not use STRIP_NOPS here! It will remove conversions from pointer
1278 to integer and cause infinite recursion. */
1279 orig_exp = exp;
1280 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1281 || (TREE_CODE (exp) == NOP_EXPR
1282 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1283 exp = TREE_OPERAND (exp, 0);
1285 /* Preserve the original expression code. */
1286 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1287 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1289 /* Normally convert enums to int,
1290 but convert wide enums to something wider. */
1291 if (code == ENUMERAL_TYPE)
1293 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1294 TYPE_PRECISION (integer_type_node)),
1295 ((TYPE_PRECISION (type)
1296 >= TYPE_PRECISION (integer_type_node))
1297 && TYPE_UNSIGNED (type)));
1299 return convert (type, exp);
1302 if (TREE_CODE (exp) == COMPONENT_REF
1303 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1304 /* If it's thinner than an int, promote it like a
1305 c_promoting_integer_type_p, otherwise leave it alone. */
1306 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1307 TYPE_PRECISION (integer_type_node)))
1308 return convert (integer_type_node, exp);
1310 if (c_promoting_integer_type_p (type))
1312 /* Preserve unsignedness if not really getting any wider. */
1313 if (TYPE_UNSIGNED (type)
1314 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1315 return convert (unsigned_type_node, exp);
1317 return convert (integer_type_node, exp);
1320 if (code == VOID_TYPE)
1322 error ("void value not ignored as it ought to be");
1323 return error_mark_node;
1325 return exp;
1328 /* Look up COMPONENT in a structure or union DECL.
1330 If the component name is not found, returns NULL_TREE. Otherwise,
1331 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1332 stepping down the chain to the component, which is in the last
1333 TREE_VALUE of the list. Normally the list is of length one, but if
1334 the component is embedded within (nested) anonymous structures or
1335 unions, the list steps down the chain to the component. */
1337 static tree
1338 lookup_field (tree decl, tree component)
1340 tree type = TREE_TYPE (decl);
1341 tree field;
1343 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1344 to the field elements. Use a binary search on this array to quickly
1345 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1346 will always be set for structures which have many elements. */
1348 if (TYPE_LANG_SPECIFIC (type))
1350 int bot, top, half;
1351 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1353 field = TYPE_FIELDS (type);
1354 bot = 0;
1355 top = TYPE_LANG_SPECIFIC (type)->s->len;
1356 while (top - bot > 1)
1358 half = (top - bot + 1) >> 1;
1359 field = field_array[bot+half];
1361 if (DECL_NAME (field) == NULL_TREE)
1363 /* Step through all anon unions in linear fashion. */
1364 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1366 field = field_array[bot++];
1367 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1368 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1370 tree anon = lookup_field (field, component);
1372 if (anon)
1373 return tree_cons (NULL_TREE, field, anon);
1377 /* Entire record is only anon unions. */
1378 if (bot > top)
1379 return NULL_TREE;
1381 /* Restart the binary search, with new lower bound. */
1382 continue;
1385 if (DECL_NAME (field) == component)
1386 break;
1387 if (DECL_NAME (field) < component)
1388 bot += half;
1389 else
1390 top = bot + half;
1393 if (DECL_NAME (field_array[bot]) == component)
1394 field = field_array[bot];
1395 else if (DECL_NAME (field) != component)
1396 return NULL_TREE;
1398 else
1400 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1402 if (DECL_NAME (field) == NULL_TREE
1403 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1404 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1406 tree anon = lookup_field (field, component);
1408 if (anon)
1409 return tree_cons (NULL_TREE, field, anon);
1412 if (DECL_NAME (field) == component)
1413 break;
1416 if (field == NULL_TREE)
1417 return NULL_TREE;
1420 return tree_cons (NULL_TREE, field, NULL_TREE);
1423 /* Make an expression to refer to the COMPONENT field of
1424 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1426 tree
1427 build_component_ref (tree datum, tree component)
1429 tree type = TREE_TYPE (datum);
1430 enum tree_code code = TREE_CODE (type);
1431 tree field = NULL;
1432 tree ref;
1434 if (!objc_is_public (datum, component))
1435 return error_mark_node;
1437 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1438 Ensure that the arguments are not lvalues; otherwise,
1439 if the component is an array, it would wrongly decay to a pointer in
1440 C89 mode.
1441 We cannot do this with a COND_EXPR, because in a conditional expression
1442 the default promotions are applied to both sides, and this would yield
1443 the wrong type of the result; for example, if the components have
1444 type "char". */
1445 switch (TREE_CODE (datum))
1447 case COMPOUND_EXPR:
1449 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1450 return build (COMPOUND_EXPR, TREE_TYPE (value),
1451 TREE_OPERAND (datum, 0), non_lvalue (value));
1453 default:
1454 break;
1457 /* See if there is a field or component with name COMPONENT. */
1459 if (code == RECORD_TYPE || code == UNION_TYPE)
1461 if (!COMPLETE_TYPE_P (type))
1463 c_incomplete_type_error (NULL_TREE, type);
1464 return error_mark_node;
1467 field = lookup_field (datum, component);
1469 if (!field)
1471 error ("%s has no member named `%s'",
1472 code == RECORD_TYPE ? "structure" : "union",
1473 IDENTIFIER_POINTER (component));
1474 return error_mark_node;
1477 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1478 This might be better solved in future the way the C++ front
1479 end does it - by giving the anonymous entities each a
1480 separate name and type, and then have build_component_ref
1481 recursively call itself. We can't do that here. */
1484 tree subdatum = TREE_VALUE (field);
1486 if (TREE_TYPE (subdatum) == error_mark_node)
1487 return error_mark_node;
1489 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1490 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1491 TREE_READONLY (ref) = 1;
1492 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1493 TREE_THIS_VOLATILE (ref) = 1;
1495 if (TREE_DEPRECATED (subdatum))
1496 warn_deprecated_use (subdatum);
1498 datum = ref;
1500 field = TREE_CHAIN (field);
1502 while (field);
1504 return ref;
1506 else if (code != ERROR_MARK)
1507 error ("request for member `%s' in something not a structure or union",
1508 IDENTIFIER_POINTER (component));
1510 return error_mark_node;
1513 /* Given an expression PTR for a pointer, return an expression
1514 for the value pointed to.
1515 ERRORSTRING is the name of the operator to appear in error messages. */
1517 tree
1518 build_indirect_ref (tree ptr, const char *errorstring)
1520 tree pointer = default_conversion (ptr);
1521 tree type = TREE_TYPE (pointer);
1523 if (TREE_CODE (type) == POINTER_TYPE)
1525 if (TREE_CODE (pointer) == ADDR_EXPR
1526 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1527 == TREE_TYPE (type)))
1528 return TREE_OPERAND (pointer, 0);
1529 else
1531 tree t = TREE_TYPE (type);
1532 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1534 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1536 error ("dereferencing pointer to incomplete type");
1537 return error_mark_node;
1539 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1540 warning ("dereferencing `void *' pointer");
1542 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1543 so that we get the proper error message if the result is used
1544 to assign to. Also, &* is supposed to be a no-op.
1545 And ANSI C seems to specify that the type of the result
1546 should be the const type. */
1547 /* A de-reference of a pointer to const is not a const. It is valid
1548 to change it via some other pointer. */
1549 TREE_READONLY (ref) = TYPE_READONLY (t);
1550 TREE_SIDE_EFFECTS (ref)
1551 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1552 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1553 return ref;
1556 else if (TREE_CODE (pointer) != ERROR_MARK)
1557 error ("invalid type argument of `%s'", errorstring);
1558 return error_mark_node;
1561 /* This handles expressions of the form "a[i]", which denotes
1562 an array reference.
1564 This is logically equivalent in C to *(a+i), but we may do it differently.
1565 If A is a variable or a member, we generate a primitive ARRAY_REF.
1566 This avoids forcing the array out of registers, and can work on
1567 arrays that are not lvalues (for example, members of structures returned
1568 by functions). */
1570 tree
1571 build_array_ref (tree array, tree index)
1573 if (index == 0)
1575 error ("subscript missing in array reference");
1576 return error_mark_node;
1579 if (TREE_TYPE (array) == error_mark_node
1580 || TREE_TYPE (index) == error_mark_node)
1581 return error_mark_node;
1583 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1585 tree rval, type;
1587 /* Subscripting with type char is likely to lose
1588 on a machine where chars are signed.
1589 So warn on any machine, but optionally.
1590 Don't warn for unsigned char since that type is safe.
1591 Don't warn for signed char because anyone who uses that
1592 must have done so deliberately. */
1593 if (warn_char_subscripts
1594 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1595 warning ("array subscript has type `char'");
1597 /* Apply default promotions *after* noticing character types. */
1598 index = default_conversion (index);
1600 /* Require integer *after* promotion, for sake of enums. */
1601 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1603 error ("array subscript is not an integer");
1604 return error_mark_node;
1607 /* An array that is indexed by a non-constant
1608 cannot be stored in a register; we must be able to do
1609 address arithmetic on its address.
1610 Likewise an array of elements of variable size. */
1611 if (TREE_CODE (index) != INTEGER_CST
1612 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1613 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1615 if (!c_mark_addressable (array))
1616 return error_mark_node;
1618 /* An array that is indexed by a constant value which is not within
1619 the array bounds cannot be stored in a register either; because we
1620 would get a crash in store_bit_field/extract_bit_field when trying
1621 to access a non-existent part of the register. */
1622 if (TREE_CODE (index) == INTEGER_CST
1623 && TYPE_DOMAIN (TREE_TYPE (array))
1624 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1626 if (!c_mark_addressable (array))
1627 return error_mark_node;
1630 if (pedantic)
1632 tree foo = array;
1633 while (TREE_CODE (foo) == COMPONENT_REF)
1634 foo = TREE_OPERAND (foo, 0);
1635 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1636 pedwarn ("ISO C forbids subscripting `register' array");
1637 else if (! flag_isoc99 && ! lvalue_p (foo))
1638 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1641 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1642 rval = build (ARRAY_REF, type, array, index);
1643 /* Array ref is const/volatile if the array elements are
1644 or if the array is. */
1645 TREE_READONLY (rval)
1646 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1647 | TREE_READONLY (array));
1648 TREE_SIDE_EFFECTS (rval)
1649 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1650 | TREE_SIDE_EFFECTS (array));
1651 TREE_THIS_VOLATILE (rval)
1652 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1653 /* This was added by rms on 16 Nov 91.
1654 It fixes vol struct foo *a; a->elts[1]
1655 in an inline function.
1656 Hope it doesn't break something else. */
1657 | TREE_THIS_VOLATILE (array));
1658 return require_complete_type (fold (rval));
1662 tree ar = default_conversion (array);
1663 tree ind = default_conversion (index);
1665 /* Do the same warning check as above, but only on the part that's
1666 syntactically the index and only if it is also semantically
1667 the index. */
1668 if (warn_char_subscripts
1669 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1670 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1671 warning ("subscript has type `char'");
1673 /* Put the integer in IND to simplify error checking. */
1674 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1676 tree temp = ar;
1677 ar = ind;
1678 ind = temp;
1681 if (ar == error_mark_node)
1682 return ar;
1684 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1685 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1687 error ("subscripted value is neither array nor pointer");
1688 return error_mark_node;
1690 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1692 error ("array subscript is not an integer");
1693 return error_mark_node;
1696 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1697 "array indexing");
1701 /* Build an external reference to identifier ID. FUN indicates
1702 whether this will be used for a function call. */
1703 tree
1704 build_external_ref (tree id, int fun)
1706 tree ref;
1707 tree decl = lookup_name (id);
1708 tree objc_ivar = lookup_objc_ivar (id);
1710 if (decl && decl != error_mark_node)
1712 /* Properly declared variable or function reference. */
1713 if (!objc_ivar)
1714 ref = decl;
1715 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1717 warning ("local declaration of `%s' hides instance variable",
1718 IDENTIFIER_POINTER (id));
1719 ref = decl;
1721 else
1722 ref = objc_ivar;
1724 else if (objc_ivar)
1725 ref = objc_ivar;
1726 else if (fun)
1727 /* Implicit function declaration. */
1728 ref = implicitly_declare (id);
1729 else if (decl == error_mark_node)
1730 /* Don't complain about something that's already been
1731 complained about. */
1732 return error_mark_node;
1733 else
1735 undeclared_variable (id);
1736 return error_mark_node;
1739 if (TREE_TYPE (ref) == error_mark_node)
1740 return error_mark_node;
1742 if (TREE_DEPRECATED (ref))
1743 warn_deprecated_use (ref);
1745 if (!skip_evaluation)
1746 assemble_external (ref);
1747 TREE_USED (ref) = 1;
1749 if (TREE_CODE (ref) == CONST_DECL)
1751 ref = DECL_INITIAL (ref);
1752 TREE_CONSTANT (ref) = 1;
1753 TREE_INVARIANT (ref) = 1;
1755 else if (current_function_decl != 0
1756 && !DECL_FILE_SCOPE_P (current_function_decl)
1757 && (TREE_CODE (ref) == VAR_DECL
1758 || TREE_CODE (ref) == PARM_DECL
1759 || TREE_CODE (ref) == FUNCTION_DECL))
1761 tree context = decl_function_context (ref);
1763 if (context != 0 && context != current_function_decl)
1764 DECL_NONLOCAL (ref) = 1;
1767 return ref;
1770 /* Build a function call to function FUNCTION with parameters PARAMS.
1771 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1772 TREE_VALUE of each node is a parameter-expression.
1773 FUNCTION's data type may be a function type or a pointer-to-function. */
1775 tree
1776 build_function_call (tree function, tree params)
1778 tree fntype, fundecl = 0;
1779 tree coerced_params;
1780 tree name = NULL_TREE, result;
1781 tree tem;
1783 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1784 STRIP_TYPE_NOPS (function);
1786 /* Convert anything with function type to a pointer-to-function. */
1787 if (TREE_CODE (function) == FUNCTION_DECL)
1789 name = DECL_NAME (function);
1791 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1792 (because calling an inline function does not mean the function
1793 needs to be separately compiled). */
1794 fntype = build_type_variant (TREE_TYPE (function),
1795 TREE_READONLY (function),
1796 TREE_THIS_VOLATILE (function));
1797 fundecl = function;
1798 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1800 else
1801 function = default_conversion (function);
1803 fntype = TREE_TYPE (function);
1805 if (TREE_CODE (fntype) == ERROR_MARK)
1806 return error_mark_node;
1808 if (!(TREE_CODE (fntype) == POINTER_TYPE
1809 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1811 error ("called object is not a function");
1812 return error_mark_node;
1815 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1816 current_function_returns_abnormally = 1;
1818 /* fntype now gets the type of function pointed to. */
1819 fntype = TREE_TYPE (fntype);
1821 /* Check that the function is called through a compatible prototype.
1822 If it is not, replace the call by a trap, wrapped up in a compound
1823 expression if necessary. This has the nice side-effect to prevent
1824 the tree-inliner from generating invalid assignment trees which may
1825 blow up in the RTL expander later.
1827 ??? This doesn't work for Objective-C because objc_comptypes
1828 refuses to compare function prototypes, yet the compiler appears
1829 to build calls that are flagged as invalid by C's comptypes. */
1830 if (! c_dialect_objc ()
1831 && TREE_CODE (function) == NOP_EXPR
1832 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1833 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1834 && ! comptypes (fntype, TREE_TYPE (tem)))
1836 tree return_type = TREE_TYPE (fntype);
1837 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1838 NULL_TREE);
1840 /* This situation leads to run-time undefined behavior. We can't,
1841 therefore, simply error unless we can prove that all possible
1842 executions of the program must execute the code. */
1843 warning ("function called through a non-compatible type");
1845 /* We can, however, treat "undefined" any way we please.
1846 Call abort to encourage the user to fix the program. */
1847 inform ("if this code is reached, the program will abort");
1849 if (VOID_TYPE_P (return_type))
1850 return trap;
1851 else
1853 tree rhs;
1855 if (AGGREGATE_TYPE_P (return_type))
1856 rhs = build_compound_literal (return_type,
1857 build_constructor (return_type,
1858 NULL_TREE));
1859 else
1860 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1862 return build (COMPOUND_EXPR, return_type, trap, rhs);
1866 /* Convert the parameters to the types declared in the
1867 function prototype, or apply default promotions. */
1869 coerced_params
1870 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1872 /* Check that the arguments to the function are valid. */
1874 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1876 /* Recognize certain built-in functions so we can make tree-codes
1877 other than CALL_EXPR. We do this when it enables fold-const.c
1878 to do something useful. */
1880 if (TREE_CODE (function) == ADDR_EXPR
1881 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1882 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1884 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1885 params, coerced_params);
1886 if (result)
1887 return result;
1890 result = build (CALL_EXPR, TREE_TYPE (fntype),
1891 function, coerced_params, NULL_TREE);
1892 TREE_SIDE_EFFECTS (result) = 1;
1894 if (require_constant_value)
1896 result = fold_initializer (result);
1898 if (TREE_CONSTANT (result)
1899 && (name == NULL_TREE
1900 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
1901 pedwarn_init ("initializer element is not constant");
1903 else
1904 result = fold (result);
1906 if (VOID_TYPE_P (TREE_TYPE (result)))
1907 return result;
1908 return require_complete_type (result);
1911 /* Convert the argument expressions in the list VALUES
1912 to the types in the list TYPELIST. The result is a list of converted
1913 argument expressions.
1915 If TYPELIST is exhausted, or when an element has NULL as its type,
1916 perform the default conversions.
1918 PARMLIST is the chain of parm decls for the function being called.
1919 It may be 0, if that info is not available.
1920 It is used only for generating error messages.
1922 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1924 This is also where warnings about wrong number of args are generated.
1926 Both VALUES and the returned value are chains of TREE_LIST nodes
1927 with the elements of the list in the TREE_VALUE slots of those nodes. */
1929 static tree
1930 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1932 tree typetail, valtail;
1933 tree result = NULL;
1934 int parmnum;
1936 /* Scan the given expressions and types, producing individual
1937 converted arguments and pushing them on RESULT in reverse order. */
1939 for (valtail = values, typetail = typelist, parmnum = 0;
1940 valtail;
1941 valtail = TREE_CHAIN (valtail), parmnum++)
1943 tree type = typetail ? TREE_VALUE (typetail) : 0;
1944 tree val = TREE_VALUE (valtail);
1946 if (type == void_type_node)
1948 if (name)
1949 error ("too many arguments to function `%s'",
1950 IDENTIFIER_POINTER (name));
1951 else
1952 error ("too many arguments to function");
1953 break;
1956 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1957 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1958 to convert automatically to a pointer. */
1959 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1960 val = TREE_OPERAND (val, 0);
1962 val = default_function_array_conversion (val);
1964 val = require_complete_type (val);
1966 if (type != 0)
1968 /* Formal parm type is specified by a function prototype. */
1969 tree parmval;
1971 if (!COMPLETE_TYPE_P (type))
1973 error ("type of formal parameter %d is incomplete", parmnum + 1);
1974 parmval = val;
1976 else
1978 /* Optionally warn about conversions that
1979 differ from the default conversions. */
1980 if (warn_conversion || warn_traditional)
1982 int formal_prec = TYPE_PRECISION (type);
1984 if (INTEGRAL_TYPE_P (type)
1985 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1986 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1987 if (INTEGRAL_TYPE_P (type)
1988 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1989 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1990 else if (TREE_CODE (type) == COMPLEX_TYPE
1991 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1992 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1993 else if (TREE_CODE (type) == REAL_TYPE
1994 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1995 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1996 else if (TREE_CODE (type) == COMPLEX_TYPE
1997 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1998 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1999 else if (TREE_CODE (type) == REAL_TYPE
2000 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2001 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
2002 /* ??? At some point, messages should be written about
2003 conversions between complex types, but that's too messy
2004 to do now. */
2005 else if (TREE_CODE (type) == REAL_TYPE
2006 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2008 /* Warn if any argument is passed as `float',
2009 since without a prototype it would be `double'. */
2010 if (formal_prec == TYPE_PRECISION (float_type_node))
2011 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
2013 /* Detect integer changing in width or signedness.
2014 These warnings are only activated with
2015 -Wconversion, not with -Wtraditional. */
2016 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2017 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2019 tree would_have_been = default_conversion (val);
2020 tree type1 = TREE_TYPE (would_have_been);
2022 if (TREE_CODE (type) == ENUMERAL_TYPE
2023 && (TYPE_MAIN_VARIANT (type)
2024 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2025 /* No warning if function asks for enum
2026 and the actual arg is that enum type. */
2028 else if (formal_prec != TYPE_PRECISION (type1))
2029 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
2030 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2032 /* Don't complain if the formal parameter type
2033 is an enum, because we can't tell now whether
2034 the value was an enum--even the same enum. */
2035 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2037 else if (TREE_CODE (val) == INTEGER_CST
2038 && int_fits_type_p (val, type))
2039 /* Change in signedness doesn't matter
2040 if a constant value is unaffected. */
2042 /* Likewise for a constant in a NOP_EXPR. */
2043 else if (TREE_CODE (val) == NOP_EXPR
2044 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2045 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2047 /* If the value is extended from a narrower
2048 unsigned type, it doesn't matter whether we
2049 pass it as signed or unsigned; the value
2050 certainly is the same either way. */
2051 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2052 && TYPE_UNSIGNED (TREE_TYPE (val)))
2054 else if (TYPE_UNSIGNED (type))
2055 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
2056 else
2057 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
2061 parmval = convert_for_assignment (type, val,
2062 (char *) 0, /* arg passing */
2063 fundecl, name, parmnum + 1);
2065 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2066 && INTEGRAL_TYPE_P (type)
2067 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2068 parmval = default_conversion (parmval);
2070 result = tree_cons (NULL_TREE, parmval, result);
2072 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2073 && (TYPE_PRECISION (TREE_TYPE (val))
2074 < TYPE_PRECISION (double_type_node)))
2075 /* Convert `float' to `double'. */
2076 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2077 else
2078 /* Convert `short' and `char' to full-size `int'. */
2079 result = tree_cons (NULL_TREE, default_conversion (val), result);
2081 if (typetail)
2082 typetail = TREE_CHAIN (typetail);
2085 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2087 if (name)
2088 error ("too few arguments to function `%s'",
2089 IDENTIFIER_POINTER (name));
2090 else
2091 error ("too few arguments to function");
2094 return nreverse (result);
2097 /* This is the entry point used by the parser
2098 for binary operators in the input.
2099 In addition to constructing the expression,
2100 we check for operands that were written with other binary operators
2101 in a way that is likely to confuse the user. */
2103 tree
2104 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
2106 tree result = build_binary_op (code, arg1, arg2, 1);
2108 char class;
2109 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
2110 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
2111 enum tree_code code1 = ERROR_MARK;
2112 enum tree_code code2 = ERROR_MARK;
2114 if (TREE_CODE (result) == ERROR_MARK)
2115 return error_mark_node;
2117 if (IS_EXPR_CODE_CLASS (class1))
2118 code1 = C_EXP_ORIGINAL_CODE (arg1);
2119 if (IS_EXPR_CODE_CLASS (class2))
2120 code2 = C_EXP_ORIGINAL_CODE (arg2);
2122 /* Check for cases such as x+y<<z which users are likely
2123 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2124 is cleared to prevent these warnings. */
2125 if (warn_parentheses)
2127 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2129 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2130 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2131 warning ("suggest parentheses around + or - inside shift");
2134 if (code == TRUTH_ORIF_EXPR)
2136 if (code1 == TRUTH_ANDIF_EXPR
2137 || code2 == TRUTH_ANDIF_EXPR)
2138 warning ("suggest parentheses around && within ||");
2141 if (code == BIT_IOR_EXPR)
2143 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2144 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2145 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2146 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2147 warning ("suggest parentheses around arithmetic in operand of |");
2148 /* Check cases like x|y==z */
2149 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2150 warning ("suggest parentheses around comparison in operand of |");
2153 if (code == BIT_XOR_EXPR)
2155 if (code1 == BIT_AND_EXPR
2156 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2157 || code2 == BIT_AND_EXPR
2158 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2159 warning ("suggest parentheses around arithmetic in operand of ^");
2160 /* Check cases like x^y==z */
2161 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2162 warning ("suggest parentheses around comparison in operand of ^");
2165 if (code == BIT_AND_EXPR)
2167 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2168 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2169 warning ("suggest parentheses around + or - in operand of &");
2170 /* Check cases like x&y==z */
2171 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2172 warning ("suggest parentheses around comparison in operand of &");
2176 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2177 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2178 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2179 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2181 unsigned_conversion_warning (result, arg1);
2182 unsigned_conversion_warning (result, arg2);
2183 overflow_warning (result);
2185 class = TREE_CODE_CLASS (TREE_CODE (result));
2187 /* Record the code that was specified in the source,
2188 for the sake of warnings about confusing nesting. */
2189 if (IS_EXPR_CODE_CLASS (class))
2190 C_SET_EXP_ORIGINAL_CODE (result, code);
2191 else
2193 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2194 so that convert_for_assignment wouldn't strip it.
2195 That way, we got warnings for things like p = (1 - 1).
2196 But it turns out we should not get those warnings. */
2197 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2198 C_SET_EXP_ORIGINAL_CODE (result, code);
2201 return result;
2205 /* Return true if `t' is known to be non-negative. */
2208 c_tree_expr_nonnegative_p (tree t)
2210 if (TREE_CODE (t) == STMT_EXPR)
2211 t = expr_last (STMT_EXPR_STMT (t));
2212 return tree_expr_nonnegative_p (t);
2215 /* Return a tree for the difference of pointers OP0 and OP1.
2216 The resulting tree has type int. */
2218 static tree
2219 pointer_diff (tree op0, tree op1)
2221 tree restype = ptrdiff_type_node;
2223 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2224 tree con0, con1, lit0, lit1;
2225 tree orig_op1 = op1;
2227 if (pedantic || warn_pointer_arith)
2229 if (TREE_CODE (target_type) == VOID_TYPE)
2230 pedwarn ("pointer of type `void *' used in subtraction");
2231 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2232 pedwarn ("pointer to a function used in subtraction");
2235 /* If the conversion to ptrdiff_type does anything like widening or
2236 converting a partial to an integral mode, we get a convert_expression
2237 that is in the way to do any simplifications.
2238 (fold-const.c doesn't know that the extra bits won't be needed.
2239 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2240 different mode in place.)
2241 So first try to find a common term here 'by hand'; we want to cover
2242 at least the cases that occur in legal static initializers. */
2243 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2244 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2246 if (TREE_CODE (con0) == PLUS_EXPR)
2248 lit0 = TREE_OPERAND (con0, 1);
2249 con0 = TREE_OPERAND (con0, 0);
2251 else
2252 lit0 = integer_zero_node;
2254 if (TREE_CODE (con1) == PLUS_EXPR)
2256 lit1 = TREE_OPERAND (con1, 1);
2257 con1 = TREE_OPERAND (con1, 0);
2259 else
2260 lit1 = integer_zero_node;
2262 if (operand_equal_p (con0, con1, 0))
2264 op0 = lit0;
2265 op1 = lit1;
2269 /* First do the subtraction as integers;
2270 then drop through to build the divide operator.
2271 Do not do default conversions on the minus operator
2272 in case restype is a short type. */
2274 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2275 convert (restype, op1), 0);
2276 /* This generates an error if op1 is pointer to incomplete type. */
2277 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2278 error ("arithmetic on pointer to an incomplete type");
2280 /* This generates an error if op0 is pointer to incomplete type. */
2281 op1 = c_size_in_bytes (target_type);
2283 /* Divide by the size, in easiest possible way. */
2284 return fold (build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2287 /* Construct and perhaps optimize a tree representation
2288 for a unary operation. CODE, a tree_code, specifies the operation
2289 and XARG is the operand.
2290 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2291 the default promotions (such as from short to int).
2292 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2293 allows non-lvalues; this is only used to handle conversion of non-lvalue
2294 arrays to pointers in C99. */
2296 tree
2297 build_unary_op (enum tree_code code, tree xarg, int flag)
2299 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2300 tree arg = xarg;
2301 tree argtype = 0;
2302 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2303 tree val;
2304 int noconvert = flag;
2306 if (typecode == ERROR_MARK)
2307 return error_mark_node;
2308 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2309 typecode = INTEGER_TYPE;
2311 switch (code)
2313 case CONVERT_EXPR:
2314 /* This is used for unary plus, because a CONVERT_EXPR
2315 is enough to prevent anybody from looking inside for
2316 associativity, but won't generate any code. */
2317 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2318 || typecode == COMPLEX_TYPE))
2320 error ("wrong type argument to unary plus");
2321 return error_mark_node;
2323 else if (!noconvert)
2324 arg = default_conversion (arg);
2325 arg = non_lvalue (arg);
2326 break;
2328 case NEGATE_EXPR:
2329 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2330 || typecode == COMPLEX_TYPE
2331 || typecode == VECTOR_TYPE))
2333 error ("wrong type argument to unary minus");
2334 return error_mark_node;
2336 else if (!noconvert)
2337 arg = default_conversion (arg);
2338 break;
2340 case BIT_NOT_EXPR:
2341 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2343 if (!noconvert)
2344 arg = default_conversion (arg);
2346 else if (typecode == COMPLEX_TYPE)
2348 code = CONJ_EXPR;
2349 if (pedantic)
2350 pedwarn ("ISO C does not support `~' for complex conjugation");
2351 if (!noconvert)
2352 arg = default_conversion (arg);
2354 else
2356 error ("wrong type argument to bit-complement");
2357 return error_mark_node;
2359 break;
2361 case ABS_EXPR:
2362 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2364 error ("wrong type argument to abs");
2365 return error_mark_node;
2367 else if (!noconvert)
2368 arg = default_conversion (arg);
2369 break;
2371 case CONJ_EXPR:
2372 /* Conjugating a real value is a no-op, but allow it anyway. */
2373 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2374 || typecode == COMPLEX_TYPE))
2376 error ("wrong type argument to conjugation");
2377 return error_mark_node;
2379 else if (!noconvert)
2380 arg = default_conversion (arg);
2381 break;
2383 case TRUTH_NOT_EXPR:
2384 if (typecode != INTEGER_TYPE
2385 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2386 && typecode != COMPLEX_TYPE
2387 /* These will convert to a pointer. */
2388 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2390 error ("wrong type argument to unary exclamation mark");
2391 return error_mark_node;
2393 arg = lang_hooks.truthvalue_conversion (arg);
2394 return invert_truthvalue (arg);
2396 case NOP_EXPR:
2397 break;
2399 case REALPART_EXPR:
2400 if (TREE_CODE (arg) == COMPLEX_CST)
2401 return TREE_REALPART (arg);
2402 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2403 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2404 else
2405 return arg;
2407 case IMAGPART_EXPR:
2408 if (TREE_CODE (arg) == COMPLEX_CST)
2409 return TREE_IMAGPART (arg);
2410 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2411 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2412 else
2413 return convert (TREE_TYPE (arg), integer_zero_node);
2415 case PREINCREMENT_EXPR:
2416 case POSTINCREMENT_EXPR:
2417 case PREDECREMENT_EXPR:
2418 case POSTDECREMENT_EXPR:
2420 /* Increment or decrement the real part of the value,
2421 and don't change the imaginary part. */
2422 if (typecode == COMPLEX_TYPE)
2424 tree real, imag;
2426 if (pedantic)
2427 pedwarn ("ISO C does not support `++' and `--' on complex types");
2429 arg = stabilize_reference (arg);
2430 real = build_unary_op (REALPART_EXPR, arg, 1);
2431 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2432 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2433 build_unary_op (code, real, 1), imag);
2436 /* Report invalid types. */
2438 if (typecode != POINTER_TYPE
2439 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2441 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2442 error ("wrong type argument to increment");
2443 else
2444 error ("wrong type argument to decrement");
2446 return error_mark_node;
2450 tree inc;
2451 tree result_type = TREE_TYPE (arg);
2453 arg = get_unwidened (arg, 0);
2454 argtype = TREE_TYPE (arg);
2456 /* Compute the increment. */
2458 if (typecode == POINTER_TYPE)
2460 /* If pointer target is an undefined struct,
2461 we just cannot know how to do the arithmetic. */
2462 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2464 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2465 error ("increment of pointer to unknown structure");
2466 else
2467 error ("decrement of pointer to unknown structure");
2469 else if ((pedantic || warn_pointer_arith)
2470 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2471 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2473 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2474 pedwarn ("wrong type argument to increment");
2475 else
2476 pedwarn ("wrong type argument to decrement");
2479 inc = c_size_in_bytes (TREE_TYPE (result_type));
2481 else
2482 inc = integer_one_node;
2484 inc = convert (argtype, inc);
2486 /* Complain about anything else that is not a true lvalue. */
2487 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2488 || code == POSTINCREMENT_EXPR)
2489 ? "invalid lvalue in increment"
2490 : "invalid lvalue in decrement")))
2491 return error_mark_node;
2493 /* Report a read-only lvalue. */
2494 if (TREE_READONLY (arg))
2495 readonly_error (arg,
2496 ((code == PREINCREMENT_EXPR
2497 || code == POSTINCREMENT_EXPR)
2498 ? "increment" : "decrement"));
2500 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2501 val = boolean_increment (code, arg);
2502 else
2503 val = build (code, TREE_TYPE (arg), arg, inc);
2504 TREE_SIDE_EFFECTS (val) = 1;
2505 val = convert (result_type, val);
2506 if (TREE_CODE (val) != code)
2507 TREE_NO_WARNING (val) = 1;
2508 return val;
2511 case ADDR_EXPR:
2512 /* Note that this operation never does default_conversion. */
2514 /* Let &* cancel out to simplify resulting code. */
2515 if (TREE_CODE (arg) == INDIRECT_REF)
2517 /* Don't let this be an lvalue. */
2518 if (lvalue_p (TREE_OPERAND (arg, 0)))
2519 return non_lvalue (TREE_OPERAND (arg, 0));
2520 return TREE_OPERAND (arg, 0);
2523 /* For &x[y], return x+y */
2524 if (TREE_CODE (arg) == ARRAY_REF)
2526 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2527 return error_mark_node;
2528 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2529 TREE_OPERAND (arg, 1), 1);
2532 /* Anything not already handled and not a true memory reference
2533 or a non-lvalue array is an error. */
2534 else if (typecode != FUNCTION_TYPE && !flag
2535 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2536 return error_mark_node;
2538 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2539 argtype = TREE_TYPE (arg);
2541 /* If the lvalue is const or volatile, merge that into the type
2542 to which the address will point. Note that you can't get a
2543 restricted pointer by taking the address of something, so we
2544 only have to deal with `const' and `volatile' here. */
2545 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2546 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2547 argtype = c_build_type_variant (argtype,
2548 TREE_READONLY (arg),
2549 TREE_THIS_VOLATILE (arg));
2551 argtype = build_pointer_type (argtype);
2553 if (!c_mark_addressable (arg))
2554 return error_mark_node;
2557 tree addr;
2559 if (TREE_CODE (arg) == COMPONENT_REF)
2561 tree field = TREE_OPERAND (arg, 1);
2563 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2565 if (DECL_C_BIT_FIELD (field))
2567 error ("attempt to take address of bit-field structure member `%s'",
2568 IDENTIFIER_POINTER (DECL_NAME (field)));
2569 return error_mark_node;
2572 addr = fold (build (PLUS_EXPR, argtype,
2573 convert (argtype, addr),
2574 convert (argtype, byte_position (field))));
2576 else
2577 addr = build1 (code, argtype, arg);
2579 return addr;
2582 default:
2583 break;
2586 if (argtype == 0)
2587 argtype = TREE_TYPE (arg);
2588 val = build1 (code, argtype, arg);
2589 return require_constant_value ? fold_initializer (val) : fold (val);
2592 /* Return nonzero if REF is an lvalue valid for this language.
2593 Lvalues can be assigned, unless their type has TYPE_READONLY.
2594 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2597 lvalue_p (tree ref)
2599 enum tree_code code = TREE_CODE (ref);
2601 switch (code)
2603 case REALPART_EXPR:
2604 case IMAGPART_EXPR:
2605 case COMPONENT_REF:
2606 return lvalue_p (TREE_OPERAND (ref, 0));
2608 case COMPOUND_LITERAL_EXPR:
2609 case STRING_CST:
2610 return 1;
2612 case INDIRECT_REF:
2613 case ARRAY_REF:
2614 case VAR_DECL:
2615 case PARM_DECL:
2616 case RESULT_DECL:
2617 case ERROR_MARK:
2618 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2619 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2621 case BIND_EXPR:
2622 case RTL_EXPR:
2623 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2625 default:
2626 return 0;
2630 /* Return nonzero if REF is an lvalue valid for this language;
2631 otherwise, print an error message and return zero. */
2633 static int
2634 lvalue_or_else (tree ref, const char *msgid)
2636 int win = lvalue_p (ref);
2638 if (! win)
2639 error ("%s", msgid);
2641 return win;
2645 /* Warn about storing in something that is `const'. */
2647 void
2648 readonly_error (tree arg, const char *msgid)
2650 if (TREE_CODE (arg) == COMPONENT_REF)
2652 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2653 readonly_error (TREE_OPERAND (arg, 0), msgid);
2654 else
2655 error ("%s of read-only member `%s'", _(msgid),
2656 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2658 else if (TREE_CODE (arg) == VAR_DECL)
2659 error ("%s of read-only variable `%s'", _(msgid),
2660 IDENTIFIER_POINTER (DECL_NAME (arg)));
2661 else
2662 error ("%s of read-only location", _(msgid));
2665 /* Mark EXP saying that we need to be able to take the
2666 address of it; it should not be allocated in a register.
2667 Returns true if successful. */
2669 bool
2670 c_mark_addressable (tree exp)
2672 tree x = exp;
2674 while (1)
2675 switch (TREE_CODE (x))
2677 case COMPONENT_REF:
2678 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2680 error ("cannot take address of bit-field `%s'",
2681 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2682 return false;
2685 /* ... fall through ... */
2687 case ADDR_EXPR:
2688 case ARRAY_REF:
2689 case REALPART_EXPR:
2690 case IMAGPART_EXPR:
2691 x = TREE_OPERAND (x, 0);
2692 break;
2694 case COMPOUND_LITERAL_EXPR:
2695 case CONSTRUCTOR:
2696 TREE_ADDRESSABLE (x) = 1;
2697 return true;
2699 case VAR_DECL:
2700 case CONST_DECL:
2701 case PARM_DECL:
2702 case RESULT_DECL:
2703 if (C_DECL_REGISTER (x)
2704 && DECL_NONLOCAL (x))
2706 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2708 error ("global register variable `%s' used in nested function",
2709 IDENTIFIER_POINTER (DECL_NAME (x)));
2710 return false;
2712 pedwarn ("register variable `%s' used in nested function",
2713 IDENTIFIER_POINTER (DECL_NAME (x)));
2715 else if (C_DECL_REGISTER (x))
2717 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2719 error ("address of global register variable `%s' requested",
2720 IDENTIFIER_POINTER (DECL_NAME (x)));
2721 return false;
2724 pedwarn ("address of register variable `%s' requested",
2725 IDENTIFIER_POINTER (DECL_NAME (x)));
2727 put_var_into_stack (x, /*rescan=*/true);
2729 /* drops in */
2730 case FUNCTION_DECL:
2731 TREE_ADDRESSABLE (x) = 1;
2732 /* drops out */
2733 default:
2734 return true;
2738 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2740 tree
2741 build_conditional_expr (tree ifexp, tree op1, tree op2)
2743 tree type1;
2744 tree type2;
2745 enum tree_code code1;
2746 enum tree_code code2;
2747 tree result_type = NULL;
2748 tree orig_op1 = op1, orig_op2 = op2;
2750 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2752 /* Promote both alternatives. */
2754 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2755 op1 = default_conversion (op1);
2756 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2757 op2 = default_conversion (op2);
2759 if (TREE_CODE (ifexp) == ERROR_MARK
2760 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2761 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2762 return error_mark_node;
2764 type1 = TREE_TYPE (op1);
2765 code1 = TREE_CODE (type1);
2766 type2 = TREE_TYPE (op2);
2767 code2 = TREE_CODE (type2);
2769 /* C90 does not permit non-lvalue arrays in conditional expressions.
2770 In C99 they will be pointers by now. */
2771 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2773 error ("non-lvalue array in conditional expression");
2774 return error_mark_node;
2777 /* Quickly detect the usual case where op1 and op2 have the same type
2778 after promotion. */
2779 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2781 if (type1 == type2)
2782 result_type = type1;
2783 else
2784 result_type = TYPE_MAIN_VARIANT (type1);
2786 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2787 || code1 == COMPLEX_TYPE)
2788 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2789 || code2 == COMPLEX_TYPE))
2791 result_type = common_type (type1, type2);
2793 /* If -Wsign-compare, warn here if type1 and type2 have
2794 different signedness. We'll promote the signed to unsigned
2795 and later code won't know it used to be different.
2796 Do this check on the original types, so that explicit casts
2797 will be considered, but default promotions won't. */
2798 if (warn_sign_compare && !skip_evaluation)
2800 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2801 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2803 if (unsigned_op1 ^ unsigned_op2)
2805 /* Do not warn if the result type is signed, since the
2806 signed type will only be chosen if it can represent
2807 all the values of the unsigned type. */
2808 if (! TYPE_UNSIGNED (result_type))
2809 /* OK */;
2810 /* Do not warn if the signed quantity is an unsuffixed
2811 integer literal (or some static constant expression
2812 involving such literals) and it is non-negative. */
2813 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2814 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2815 /* OK */;
2816 else
2817 warning ("signed and unsigned type in conditional expression");
2821 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2823 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2824 pedwarn ("ISO C forbids conditional expr with only one void side");
2825 result_type = void_type_node;
2827 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2829 if (comp_target_types (type1, type2, 1))
2830 result_type = common_pointer_type (type1, type2);
2831 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2832 && TREE_CODE (orig_op1) != NOP_EXPR)
2833 result_type = qualify_type (type2, type1);
2834 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2835 && TREE_CODE (orig_op2) != NOP_EXPR)
2836 result_type = qualify_type (type1, type2);
2837 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2839 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2840 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2841 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2842 TREE_TYPE (type2)));
2844 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2846 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2847 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2848 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2849 TREE_TYPE (type1)));
2851 else
2853 pedwarn ("pointer type mismatch in conditional expression");
2854 result_type = build_pointer_type (void_type_node);
2857 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2859 if (! integer_zerop (op2))
2860 pedwarn ("pointer/integer type mismatch in conditional expression");
2861 else
2863 op2 = null_pointer_node;
2865 result_type = type1;
2867 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2869 if (!integer_zerop (op1))
2870 pedwarn ("pointer/integer type mismatch in conditional expression");
2871 else
2873 op1 = null_pointer_node;
2875 result_type = type2;
2878 if (!result_type)
2880 if (flag_cond_mismatch)
2881 result_type = void_type_node;
2882 else
2884 error ("type mismatch in conditional expression");
2885 return error_mark_node;
2889 /* Merge const and volatile flags of the incoming types. */
2890 result_type
2891 = build_type_variant (result_type,
2892 TREE_READONLY (op1) || TREE_READONLY (op2),
2893 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2895 if (result_type != TREE_TYPE (op1))
2896 op1 = convert_and_check (result_type, op1);
2897 if (result_type != TREE_TYPE (op2))
2898 op2 = convert_and_check (result_type, op2);
2900 if (TREE_CODE (ifexp) == INTEGER_CST)
2901 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2903 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2906 /* Given a list of expressions, return a compound expression
2907 that performs them all and returns the value of the last of them. */
2909 tree
2910 build_compound_expr (tree list)
2912 return internal_build_compound_expr (list, TRUE);
2915 static tree
2916 internal_build_compound_expr (tree list, int first_p)
2918 tree rest;
2920 if (TREE_CHAIN (list) == 0)
2922 /* Convert arrays and functions to pointers when there
2923 really is a comma operator. */
2924 if (!first_p)
2925 TREE_VALUE (list)
2926 = default_function_array_conversion (TREE_VALUE (list));
2928 /* Don't let (0, 0) be null pointer constant. */
2929 if (!first_p && integer_zerop (TREE_VALUE (list)))
2930 return non_lvalue (TREE_VALUE (list));
2931 return TREE_VALUE (list);
2934 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2936 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2938 /* The left-hand operand of a comma expression is like an expression
2939 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2940 any side-effects, unless it was explicitly cast to (void). */
2941 if (warn_unused_value
2942 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2943 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2944 warning ("left-hand operand of comma expression has no effect");
2947 /* With -Wunused, we should also warn if the left-hand operand does have
2948 side-effects, but computes a value which is not used. For example, in
2949 `foo() + bar(), baz()' the result of the `+' operator is not used,
2950 so we should issue a warning. */
2951 else if (warn_unused_value)
2952 warn_if_unused_value (TREE_VALUE (list), input_location);
2954 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2957 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2959 tree
2960 build_c_cast (tree type, tree expr)
2962 tree value = expr;
2964 if (type == error_mark_node || expr == error_mark_node)
2965 return error_mark_node;
2967 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2968 only in <protocol> qualifications. But when constructing cast expressions,
2969 the protocols do matter and must be kept around. */
2970 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2971 type = TYPE_MAIN_VARIANT (type);
2973 if (TREE_CODE (type) == ARRAY_TYPE)
2975 error ("cast specifies array type");
2976 return error_mark_node;
2979 if (TREE_CODE (type) == FUNCTION_TYPE)
2981 error ("cast specifies function type");
2982 return error_mark_node;
2985 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2987 if (pedantic)
2989 if (TREE_CODE (type) == RECORD_TYPE
2990 || TREE_CODE (type) == UNION_TYPE)
2991 pedwarn ("ISO C forbids casting nonscalar to the same type");
2994 else if (TREE_CODE (type) == UNION_TYPE)
2996 tree field;
2997 value = default_function_array_conversion (value);
2999 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3000 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3001 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3002 break;
3004 if (field)
3006 tree t;
3008 if (pedantic)
3009 pedwarn ("ISO C forbids casts to union type");
3010 t = digest_init (type,
3011 build_constructor (type,
3012 build_tree_list (field, value)),
3014 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3015 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3016 return t;
3018 error ("cast to union type from type not present in union");
3019 return error_mark_node;
3021 else
3023 tree otype, ovalue;
3025 /* If casting to void, avoid the error that would come
3026 from default_conversion in the case of a non-lvalue array. */
3027 if (type == void_type_node)
3028 return build1 (CONVERT_EXPR, type, value);
3030 /* Convert functions and arrays to pointers,
3031 but don't convert any other types. */
3032 value = default_function_array_conversion (value);
3033 otype = TREE_TYPE (value);
3035 /* Optionally warn about potentially worrisome casts. */
3037 if (warn_cast_qual
3038 && TREE_CODE (type) == POINTER_TYPE
3039 && TREE_CODE (otype) == POINTER_TYPE)
3041 tree in_type = type;
3042 tree in_otype = otype;
3043 int added = 0;
3044 int discarded = 0;
3046 /* Check that the qualifiers on IN_TYPE are a superset of
3047 the qualifiers of IN_OTYPE. The outermost level of
3048 POINTER_TYPE nodes is uninteresting and we stop as soon
3049 as we hit a non-POINTER_TYPE node on either type. */
3052 in_otype = TREE_TYPE (in_otype);
3053 in_type = TREE_TYPE (in_type);
3055 /* GNU C allows cv-qualified function types. 'const'
3056 means the function is very pure, 'volatile' means it
3057 can't return. We need to warn when such qualifiers
3058 are added, not when they're taken away. */
3059 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3060 && TREE_CODE (in_type) == FUNCTION_TYPE)
3061 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3062 else
3063 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3065 while (TREE_CODE (in_type) == POINTER_TYPE
3066 && TREE_CODE (in_otype) == POINTER_TYPE);
3068 if (added)
3069 warning ("cast adds new qualifiers to function type");
3071 if (discarded)
3072 /* There are qualifiers present in IN_OTYPE that are not
3073 present in IN_TYPE. */
3074 warning ("cast discards qualifiers from pointer target type");
3077 /* Warn about possible alignment problems. */
3078 if (STRICT_ALIGNMENT && warn_cast_align
3079 && TREE_CODE (type) == POINTER_TYPE
3080 && TREE_CODE (otype) == POINTER_TYPE
3081 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3082 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3083 /* Don't warn about opaque types, where the actual alignment
3084 restriction is unknown. */
3085 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3086 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3087 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3088 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3089 warning ("cast increases required alignment of target type");
3091 if (TREE_CODE (type) == INTEGER_TYPE
3092 && TREE_CODE (otype) == POINTER_TYPE
3093 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3094 && !TREE_CONSTANT (value))
3095 warning ("cast from pointer to integer of different size");
3097 if (warn_bad_function_cast
3098 && TREE_CODE (value) == CALL_EXPR
3099 && TREE_CODE (type) != TREE_CODE (otype))
3100 warning ("cast does not match function type");
3102 if (TREE_CODE (type) == POINTER_TYPE
3103 && TREE_CODE (otype) == INTEGER_TYPE
3104 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3105 /* Don't warn about converting any constant. */
3106 && !TREE_CONSTANT (value))
3107 warning ("cast to pointer from integer of different size");
3109 if (TREE_CODE (type) == POINTER_TYPE
3110 && TREE_CODE (otype) == POINTER_TYPE
3111 && TREE_CODE (expr) == ADDR_EXPR
3112 && DECL_P (TREE_OPERAND (expr, 0))
3113 && flag_strict_aliasing && warn_strict_aliasing
3114 && !VOID_TYPE_P (TREE_TYPE (type)))
3116 /* Casting the address of a decl to non void pointer. Warn
3117 if the cast breaks type based aliasing. */
3118 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3119 warning ("type-punning to incomplete type might break strict-aliasing rules");
3120 else
3122 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3123 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3125 if (!alias_sets_conflict_p (set1, set2))
3126 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3127 else if (warn_strict_aliasing > 1
3128 && !alias_sets_might_conflict_p (set1, set2))
3129 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3133 /* If pedantic, warn for conversions between function and object
3134 pointer types, except for converting a null pointer constant
3135 to function pointer type. */
3136 if (pedantic
3137 && TREE_CODE (type) == POINTER_TYPE
3138 && TREE_CODE (otype) == POINTER_TYPE
3139 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3140 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3141 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3143 if (pedantic
3144 && TREE_CODE (type) == POINTER_TYPE
3145 && TREE_CODE (otype) == POINTER_TYPE
3146 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3147 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3148 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3149 && TREE_CODE (expr) != NOP_EXPR))
3150 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3152 ovalue = value;
3153 /* Replace a nonvolatile const static variable with its value. */
3154 if (optimize && TREE_CODE (value) == VAR_DECL)
3155 value = decl_constant_value (value);
3156 value = convert (type, value);
3158 /* Ignore any integer overflow caused by the cast. */
3159 if (TREE_CODE (value) == INTEGER_CST)
3161 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3163 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3164 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3168 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3169 if (TREE_CODE (value) == INTEGER_CST
3170 && TREE_CODE (expr) == INTEGER_CST
3171 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3172 value = non_lvalue (value);
3174 /* Don't let a cast be an lvalue. */
3175 if (value == expr)
3176 value = non_lvalue (value);
3178 return value;
3181 /* Interpret a cast of expression EXPR to type TYPE. */
3182 tree
3183 c_cast_expr (tree type, tree expr)
3185 int saved_wsp = warn_strict_prototypes;
3187 /* This avoids warnings about unprototyped casts on
3188 integers. E.g. "#define SIG_DFL (void(*)())0". */
3189 if (TREE_CODE (expr) == INTEGER_CST)
3190 warn_strict_prototypes = 0;
3191 type = groktypename (type);
3192 warn_strict_prototypes = saved_wsp;
3194 return build_c_cast (type, expr);
3198 /* Build an assignment expression of lvalue LHS from value RHS.
3199 MODIFYCODE is the code for a binary operator that we use
3200 to combine the old value of LHS with RHS to get the new value.
3201 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3203 tree
3204 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3206 tree result;
3207 tree newrhs;
3208 tree lhstype = TREE_TYPE (lhs);
3209 tree olhstype = lhstype;
3211 /* Types that aren't fully specified cannot be used in assignments. */
3212 lhs = require_complete_type (lhs);
3214 /* Avoid duplicate error messages from operands that had errors. */
3215 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3216 return error_mark_node;
3218 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3219 /* Do not use STRIP_NOPS here. We do not want an enumerator
3220 whose value is 0 to count as a null pointer constant. */
3221 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3222 rhs = TREE_OPERAND (rhs, 0);
3224 newrhs = rhs;
3226 /* If a binary op has been requested, combine the old LHS value with the RHS
3227 producing the value we should actually store into the LHS. */
3229 if (modifycode != NOP_EXPR)
3231 lhs = stabilize_reference (lhs);
3232 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3235 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3236 return error_mark_node;
3238 /* Warn about storing in something that is `const'. */
3240 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3241 || ((TREE_CODE (lhstype) == RECORD_TYPE
3242 || TREE_CODE (lhstype) == UNION_TYPE)
3243 && C_TYPE_FIELDS_READONLY (lhstype)))
3244 readonly_error (lhs, "assignment");
3246 /* If storing into a structure or union member,
3247 it has probably been given type `int'.
3248 Compute the type that would go with
3249 the actual amount of storage the member occupies. */
3251 if (TREE_CODE (lhs) == COMPONENT_REF
3252 && (TREE_CODE (lhstype) == INTEGER_TYPE
3253 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3254 || TREE_CODE (lhstype) == REAL_TYPE
3255 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3256 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3258 /* If storing in a field that is in actuality a short or narrower than one,
3259 we must store in the field in its actual type. */
3261 if (lhstype != TREE_TYPE (lhs))
3263 lhs = copy_node (lhs);
3264 TREE_TYPE (lhs) = lhstype;
3267 /* Convert new value to destination type. */
3269 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3270 NULL_TREE, NULL_TREE, 0);
3271 if (TREE_CODE (newrhs) == ERROR_MARK)
3272 return error_mark_node;
3274 /* Scan operands */
3276 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3277 TREE_SIDE_EFFECTS (result) = 1;
3279 /* If we got the LHS in a different type for storing in,
3280 convert the result back to the nominal type of LHS
3281 so that the value we return always has the same type
3282 as the LHS argument. */
3284 if (olhstype == TREE_TYPE (result))
3285 return result;
3286 return convert_for_assignment (olhstype, result, _("assignment"),
3287 NULL_TREE, NULL_TREE, 0);
3290 /* Convert value RHS to type TYPE as preparation for an assignment
3291 to an lvalue of type TYPE.
3292 The real work of conversion is done by `convert'.
3293 The purpose of this function is to generate error messages
3294 for assignments that are not allowed in C.
3295 ERRTYPE is a string to use in error messages:
3296 "assignment", "return", etc. If it is null, this is parameter passing
3297 for a function call (and different error messages are output).
3299 FUNNAME is the name of the function being called,
3300 as an IDENTIFIER_NODE, or null.
3301 PARMNUM is the number of the argument, for printing in error messages. */
3303 static tree
3304 convert_for_assignment (tree type, tree rhs, const char *errtype,
3305 tree fundecl, tree funname, int parmnum)
3307 enum tree_code codel = TREE_CODE (type);
3308 tree rhstype;
3309 enum tree_code coder;
3311 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3312 /* Do not use STRIP_NOPS here. We do not want an enumerator
3313 whose value is 0 to count as a null pointer constant. */
3314 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3315 rhs = TREE_OPERAND (rhs, 0);
3317 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3318 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3319 rhs = default_conversion (rhs);
3320 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3321 rhs = decl_constant_value_for_broken_optimization (rhs);
3323 rhstype = TREE_TYPE (rhs);
3324 coder = TREE_CODE (rhstype);
3326 if (coder == ERROR_MARK)
3327 return error_mark_node;
3329 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3331 overflow_warning (rhs);
3332 /* Check for Objective-C protocols. This will automatically
3333 issue a warning if there are protocol violations. No need to
3334 use the return value. */
3335 if (c_dialect_objc ())
3336 objc_comptypes (type, rhstype, 0);
3337 return rhs;
3340 if (coder == VOID_TYPE)
3342 error ("void value not ignored as it ought to be");
3343 return error_mark_node;
3345 /* A type converts to a reference to it.
3346 This code doesn't fully support references, it's just for the
3347 special case of va_start and va_copy. */
3348 if (codel == REFERENCE_TYPE
3349 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3351 if (!lvalue_p (rhs))
3353 error ("cannot pass rvalue to reference parameter");
3354 return error_mark_node;
3356 if (!c_mark_addressable (rhs))
3357 return error_mark_node;
3358 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3360 /* We already know that these two types are compatible, but they
3361 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3362 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3363 likely to be va_list, a typedef to __builtin_va_list, which
3364 is different enough that it will cause problems later. */
3365 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3366 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3368 rhs = build1 (NOP_EXPR, type, rhs);
3369 return rhs;
3371 /* Some types can interconvert without explicit casts. */
3372 else if (codel == VECTOR_TYPE
3373 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3374 return convert (type, rhs);
3375 /* Arithmetic types all interconvert, and enum is treated like int. */
3376 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3377 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3378 || codel == BOOLEAN_TYPE)
3379 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3380 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3381 || coder == BOOLEAN_TYPE))
3382 return convert_and_check (type, rhs);
3384 /* Conversion to a transparent union from its member types.
3385 This applies only to function arguments. */
3386 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3388 tree memb_types;
3389 tree marginal_memb_type = 0;
3391 for (memb_types = TYPE_FIELDS (type); memb_types;
3392 memb_types = TREE_CHAIN (memb_types))
3394 tree memb_type = TREE_TYPE (memb_types);
3396 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3397 TYPE_MAIN_VARIANT (rhstype)))
3398 break;
3400 if (TREE_CODE (memb_type) != POINTER_TYPE)
3401 continue;
3403 if (coder == POINTER_TYPE)
3405 tree ttl = TREE_TYPE (memb_type);
3406 tree ttr = TREE_TYPE (rhstype);
3408 /* Any non-function converts to a [const][volatile] void *
3409 and vice versa; otherwise, targets must be the same.
3410 Meanwhile, the lhs target must have all the qualifiers of
3411 the rhs. */
3412 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3413 || comp_target_types (memb_type, rhstype, 0))
3415 /* If this type won't generate any warnings, use it. */
3416 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3417 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3418 && TREE_CODE (ttl) == FUNCTION_TYPE)
3419 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3420 == TYPE_QUALS (ttr))
3421 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3422 == TYPE_QUALS (ttl))))
3423 break;
3425 /* Keep looking for a better type, but remember this one. */
3426 if (! marginal_memb_type)
3427 marginal_memb_type = memb_type;
3431 /* Can convert integer zero to any pointer type. */
3432 if (integer_zerop (rhs)
3433 || (TREE_CODE (rhs) == NOP_EXPR
3434 && integer_zerop (TREE_OPERAND (rhs, 0))))
3436 rhs = null_pointer_node;
3437 break;
3441 if (memb_types || marginal_memb_type)
3443 if (! memb_types)
3445 /* We have only a marginally acceptable member type;
3446 it needs a warning. */
3447 tree ttl = TREE_TYPE (marginal_memb_type);
3448 tree ttr = TREE_TYPE (rhstype);
3450 /* Const and volatile mean something different for function
3451 types, so the usual warnings are not appropriate. */
3452 if (TREE_CODE (ttr) == FUNCTION_TYPE
3453 && TREE_CODE (ttl) == FUNCTION_TYPE)
3455 /* Because const and volatile on functions are
3456 restrictions that say the function will not do
3457 certain things, it is okay to use a const or volatile
3458 function where an ordinary one is wanted, but not
3459 vice-versa. */
3460 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3461 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3462 errtype, funname, parmnum);
3464 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3465 warn_for_assignment ("%s discards qualifiers from pointer target type",
3466 errtype, funname,
3467 parmnum);
3470 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3471 pedwarn ("ISO C prohibits argument conversion to union type");
3473 return build1 (NOP_EXPR, type, rhs);
3477 /* Conversions among pointers */
3478 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3479 && (coder == codel))
3481 tree ttl = TREE_TYPE (type);
3482 tree ttr = TREE_TYPE (rhstype);
3483 bool is_opaque_pointer;
3484 int target_cmp = 0; /* Cache comp_target_types () result. */
3486 /* Opaque pointers are treated like void pointers. */
3487 is_opaque_pointer = (targetm.vector_opaque_p (type)
3488 || targetm.vector_opaque_p (rhstype))
3489 && TREE_CODE (ttl) == VECTOR_TYPE
3490 && TREE_CODE (ttr) == VECTOR_TYPE;
3492 /* Any non-function converts to a [const][volatile] void *
3493 and vice versa; otherwise, targets must be the same.
3494 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3495 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3496 || (target_cmp = comp_target_types (type, rhstype, 0))
3497 || is_opaque_pointer
3498 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3499 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3501 if (pedantic
3502 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3504 (VOID_TYPE_P (ttr)
3505 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3506 which are not ANSI null ptr constants. */
3507 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3508 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3509 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3510 errtype, funname, parmnum);
3511 /* Const and volatile mean something different for function types,
3512 so the usual warnings are not appropriate. */
3513 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3514 && TREE_CODE (ttl) != FUNCTION_TYPE)
3516 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3517 warn_for_assignment ("%s discards qualifiers from pointer target type",
3518 errtype, funname, parmnum);
3519 /* If this is not a case of ignoring a mismatch in signedness,
3520 no warning. */
3521 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3522 || target_cmp)
3524 /* If there is a mismatch, do warn. */
3525 else if (pedantic)
3526 warn_for_assignment ("pointer targets in %s differ in signedness",
3527 errtype, funname, parmnum);
3529 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3530 && TREE_CODE (ttr) == FUNCTION_TYPE)
3532 /* Because const and volatile on functions are restrictions
3533 that say the function will not do certain things,
3534 it is okay to use a const or volatile function
3535 where an ordinary one is wanted, but not vice-versa. */
3536 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3537 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3538 errtype, funname, parmnum);
3541 else
3542 warn_for_assignment ("%s from incompatible pointer type",
3543 errtype, funname, parmnum);
3544 return convert (type, rhs);
3546 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3548 error ("invalid use of non-lvalue array");
3549 return error_mark_node;
3551 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3553 /* An explicit constant 0 can convert to a pointer,
3554 or one that results from arithmetic, even including
3555 a cast to integer type. */
3556 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3558 ! (TREE_CODE (rhs) == NOP_EXPR
3559 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3560 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3561 && integer_zerop (TREE_OPERAND (rhs, 0))))
3562 warn_for_assignment ("%s makes pointer from integer without a cast",
3563 errtype, funname, parmnum);
3565 return convert (type, rhs);
3567 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3569 warn_for_assignment ("%s makes integer from pointer without a cast",
3570 errtype, funname, parmnum);
3571 return convert (type, rhs);
3573 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3574 return convert (type, rhs);
3576 if (!errtype)
3578 if (funname)
3580 tree selector = objc_message_selector ();
3582 if (selector && parmnum > 2)
3583 error ("incompatible type for argument %d of `%s'",
3584 parmnum - 2, IDENTIFIER_POINTER (selector));
3585 else
3586 error ("incompatible type for argument %d of `%s'",
3587 parmnum, IDENTIFIER_POINTER (funname));
3589 else
3590 error ("incompatible type for argument %d of indirect function call",
3591 parmnum);
3593 else
3594 error ("incompatible types in %s", errtype);
3596 return error_mark_node;
3599 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3600 is used for error and waring reporting and indicates which argument
3601 is being processed. */
3603 tree
3604 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3606 tree ret, type;
3608 /* If FN was prototyped, the value has been converted already
3609 in convert_arguments. */
3610 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3611 return value;
3613 type = TREE_TYPE (parm);
3614 ret = convert_for_assignment (type, value,
3615 (char *) 0 /* arg passing */, fn,
3616 DECL_NAME (fn), argnum);
3617 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3618 && INTEGRAL_TYPE_P (type)
3619 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3620 ret = default_conversion (ret);
3621 return ret;
3624 /* Print a warning using MSGID.
3625 It gets OPNAME as its one parameter.
3626 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3627 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3628 FUNCTION and ARGNUM are handled specially if we are building an
3629 Objective-C selector. */
3631 static void
3632 warn_for_assignment (const char *msgid, const char *opname, tree function,
3633 int argnum)
3635 if (opname == 0)
3637 tree selector = objc_message_selector ();
3638 char * new_opname;
3640 if (selector && argnum > 2)
3642 function = selector;
3643 argnum -= 2;
3645 if (argnum == 0)
3647 if (function)
3649 /* Function name is known; supply it. */
3650 const char *const argstring = _("passing arg of `%s'");
3651 new_opname = alloca (IDENTIFIER_LENGTH (function)
3652 + strlen (argstring) + 1 + 1);
3653 sprintf (new_opname, argstring,
3654 IDENTIFIER_POINTER (function));
3656 else
3658 /* Function name unknown (call through ptr). */
3659 const char *const argnofun = _("passing arg of pointer to function");
3660 new_opname = alloca (strlen (argnofun) + 1 + 1);
3661 sprintf (new_opname, argnofun);
3664 else if (function)
3666 /* Function name is known; supply it. */
3667 const char *const argstring = _("passing arg %d of `%s'");
3668 new_opname = alloca (IDENTIFIER_LENGTH (function)
3669 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3670 sprintf (new_opname, argstring, argnum,
3671 IDENTIFIER_POINTER (function));
3673 else
3675 /* Function name unknown (call through ptr); just give arg number. */
3676 const char *const argnofun = _("passing arg %d of pointer to function");
3677 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3678 sprintf (new_opname, argnofun, argnum);
3680 opname = new_opname;
3682 pedwarn (msgid, opname);
3685 /* If VALUE is a compound expr all of whose expressions are constant, then
3686 return its value. Otherwise, return error_mark_node.
3688 This is for handling COMPOUND_EXPRs as initializer elements
3689 which is allowed with a warning when -pedantic is specified. */
3691 static tree
3692 valid_compound_expr_initializer (tree value, tree endtype)
3694 if (TREE_CODE (value) == COMPOUND_EXPR)
3696 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3697 == error_mark_node)
3698 return error_mark_node;
3699 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3700 endtype);
3702 else if (! TREE_CONSTANT (value)
3703 && ! initializer_constant_valid_p (value, endtype))
3704 return error_mark_node;
3705 else
3706 return value;
3709 /* Perform appropriate conversions on the initial value of a variable,
3710 store it in the declaration DECL,
3711 and print any error messages that are appropriate.
3712 If the init is invalid, store an ERROR_MARK. */
3714 void
3715 store_init_value (tree decl, tree init)
3717 tree value, type;
3719 /* If variable's type was invalidly declared, just ignore it. */
3721 type = TREE_TYPE (decl);
3722 if (TREE_CODE (type) == ERROR_MARK)
3723 return;
3725 /* Digest the specified initializer into an expression. */
3727 value = digest_init (type, init, TREE_STATIC (decl));
3729 /* Store the expression if valid; else report error. */
3731 if (warn_traditional && !in_system_header
3732 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3733 warning ("traditional C rejects automatic aggregate initialization");
3735 DECL_INITIAL (decl) = value;
3737 /* ANSI wants warnings about out-of-range constant initializers. */
3738 STRIP_TYPE_NOPS (value);
3739 constant_expression_warning (value);
3741 /* Check if we need to set array size from compound literal size. */
3742 if (TREE_CODE (type) == ARRAY_TYPE
3743 && TYPE_DOMAIN (type) == 0
3744 && value != error_mark_node)
3746 tree inside_init = init;
3748 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3749 inside_init = TREE_OPERAND (init, 0);
3750 inside_init = fold (inside_init);
3752 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3754 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3756 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3758 /* For int foo[] = (int [3]){1}; we need to set array size
3759 now since later on array initializer will be just the
3760 brace enclosed list of the compound literal. */
3761 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3762 layout_type (type);
3763 layout_decl (decl, 0);
3769 /* Methods for storing and printing names for error messages. */
3771 /* Implement a spelling stack that allows components of a name to be pushed
3772 and popped. Each element on the stack is this structure. */
3774 struct spelling
3776 int kind;
3777 union
3779 int i;
3780 const char *s;
3781 } u;
3784 #define SPELLING_STRING 1
3785 #define SPELLING_MEMBER 2
3786 #define SPELLING_BOUNDS 3
3788 static struct spelling *spelling; /* Next stack element (unused). */
3789 static struct spelling *spelling_base; /* Spelling stack base. */
3790 static int spelling_size; /* Size of the spelling stack. */
3792 /* Macros to save and restore the spelling stack around push_... functions.
3793 Alternative to SAVE_SPELLING_STACK. */
3795 #define SPELLING_DEPTH() (spelling - spelling_base)
3796 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3798 /* Push an element on the spelling stack with type KIND and assign VALUE
3799 to MEMBER. */
3801 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3803 int depth = SPELLING_DEPTH (); \
3805 if (depth >= spelling_size) \
3807 spelling_size += 10; \
3808 if (spelling_base == 0) \
3809 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3810 else \
3811 spelling_base = xrealloc (spelling_base, \
3812 spelling_size * sizeof (struct spelling)); \
3813 RESTORE_SPELLING_DEPTH (depth); \
3816 spelling->kind = (KIND); \
3817 spelling->MEMBER = (VALUE); \
3818 spelling++; \
3821 /* Push STRING on the stack. Printed literally. */
3823 static void
3824 push_string (const char *string)
3826 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3829 /* Push a member name on the stack. Printed as '.' STRING. */
3831 static void
3832 push_member_name (tree decl)
3834 const char *const string
3835 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3836 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3839 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3841 static void
3842 push_array_bounds (int bounds)
3844 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3847 /* Compute the maximum size in bytes of the printed spelling. */
3849 static int
3850 spelling_length (void)
3852 int size = 0;
3853 struct spelling *p;
3855 for (p = spelling_base; p < spelling; p++)
3857 if (p->kind == SPELLING_BOUNDS)
3858 size += 25;
3859 else
3860 size += strlen (p->u.s) + 1;
3863 return size;
3866 /* Print the spelling to BUFFER and return it. */
3868 static char *
3869 print_spelling (char *buffer)
3871 char *d = buffer;
3872 struct spelling *p;
3874 for (p = spelling_base; p < spelling; p++)
3875 if (p->kind == SPELLING_BOUNDS)
3877 sprintf (d, "[%d]", p->u.i);
3878 d += strlen (d);
3880 else
3882 const char *s;
3883 if (p->kind == SPELLING_MEMBER)
3884 *d++ = '.';
3885 for (s = p->u.s; (*d = *s++); d++)
3888 *d++ = '\0';
3889 return buffer;
3892 /* Issue an error message for a bad initializer component.
3893 MSGID identifies the message.
3894 The component name is taken from the spelling stack. */
3896 void
3897 error_init (const char *msgid)
3899 char *ofwhat;
3901 error ("%s", _(msgid));
3902 ofwhat = print_spelling (alloca (spelling_length () + 1));
3903 if (*ofwhat)
3904 error ("(near initialization for `%s')", ofwhat);
3907 /* Issue a pedantic warning for a bad initializer component.
3908 MSGID identifies the message.
3909 The component name is taken from the spelling stack. */
3911 void
3912 pedwarn_init (const char *msgid)
3914 char *ofwhat;
3916 pedwarn ("%s", _(msgid));
3917 ofwhat = print_spelling (alloca (spelling_length () + 1));
3918 if (*ofwhat)
3919 pedwarn ("(near initialization for `%s')", ofwhat);
3922 /* Issue a warning for a bad initializer component.
3923 MSGID identifies the message.
3924 The component name is taken from the spelling stack. */
3926 static void
3927 warning_init (const char *msgid)
3929 char *ofwhat;
3931 warning ("%s", _(msgid));
3932 ofwhat = print_spelling (alloca (spelling_length () + 1));
3933 if (*ofwhat)
3934 warning ("(near initialization for `%s')", ofwhat);
3937 /* Digest the parser output INIT as an initializer for type TYPE.
3938 Return a C expression of type TYPE to represent the initial value.
3940 REQUIRE_CONSTANT requests an error if non-constant initializers or
3941 elements are seen. */
3943 static tree
3944 digest_init (tree type, tree init, int require_constant)
3946 enum tree_code code = TREE_CODE (type);
3947 tree inside_init = init;
3949 if (type == error_mark_node
3950 || init == error_mark_node
3951 || TREE_TYPE (init) == error_mark_node)
3952 return error_mark_node;
3954 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3955 /* Do not use STRIP_NOPS here. We do not want an enumerator
3956 whose value is 0 to count as a null pointer constant. */
3957 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3958 inside_init = TREE_OPERAND (init, 0);
3960 inside_init = fold (inside_init);
3962 /* Initialization of an array of chars from a string constant
3963 optionally enclosed in braces. */
3965 if (code == ARRAY_TYPE)
3967 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3968 if ((typ1 == char_type_node
3969 || typ1 == signed_char_type_node
3970 || typ1 == unsigned_char_type_node
3971 || typ1 == unsigned_wchar_type_node
3972 || typ1 == signed_wchar_type_node)
3973 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3975 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3976 TYPE_MAIN_VARIANT (type)))
3977 return inside_init;
3979 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3980 != char_type_node)
3981 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3983 error_init ("char-array initialized from wide string");
3984 return error_mark_node;
3986 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3987 == char_type_node)
3988 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3990 error_init ("int-array initialized from non-wide string");
3991 return error_mark_node;
3994 TREE_TYPE (inside_init) = type;
3995 if (TYPE_DOMAIN (type) != 0
3996 && TYPE_SIZE (type) != 0
3997 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3998 /* Subtract 1 (or sizeof (wchar_t))
3999 because it's ok to ignore the terminating null char
4000 that is counted in the length of the constant. */
4001 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4002 TREE_STRING_LENGTH (inside_init)
4003 - ((TYPE_PRECISION (typ1)
4004 != TYPE_PRECISION (char_type_node))
4005 ? (TYPE_PRECISION (wchar_type_node)
4006 / BITS_PER_UNIT)
4007 : 1)))
4008 pedwarn_init ("initializer-string for array of chars is too long");
4010 return inside_init;
4014 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4015 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4016 below and handle as a constructor. */
4017 if (code == VECTOR_TYPE
4018 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4019 && TREE_CONSTANT (inside_init))
4021 if (TREE_CODE (inside_init) == VECTOR_CST
4022 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4023 TYPE_MAIN_VARIANT (type)))
4024 return inside_init;
4025 else
4026 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4029 /* Any type can be initialized
4030 from an expression of the same type, optionally with braces. */
4032 if (inside_init && TREE_TYPE (inside_init) != 0
4033 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4034 TYPE_MAIN_VARIANT (type))
4035 || (code == ARRAY_TYPE
4036 && comptypes (TREE_TYPE (inside_init), type))
4037 || (code == VECTOR_TYPE
4038 && comptypes (TREE_TYPE (inside_init), type))
4039 || (code == POINTER_TYPE
4040 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4041 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4042 TREE_TYPE (type)))
4043 || (code == POINTER_TYPE
4044 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4045 && comptypes (TREE_TYPE (inside_init),
4046 TREE_TYPE (type)))))
4048 if (code == POINTER_TYPE)
4050 inside_init = default_function_array_conversion (inside_init);
4052 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4054 error_init ("invalid use of non-lvalue array");
4055 return error_mark_node;
4059 if (code == VECTOR_TYPE)
4060 /* Although the types are compatible, we may require a
4061 conversion. */
4062 inside_init = convert (type, inside_init);
4064 if (require_constant && !flag_isoc99
4065 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4067 /* As an extension, allow initializing objects with static storage
4068 duration with compound literals (which are then treated just as
4069 the brace enclosed list they contain). */
4070 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4071 inside_init = DECL_INITIAL (decl);
4074 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4075 && TREE_CODE (inside_init) != CONSTRUCTOR)
4077 error_init ("array initialized from non-constant array expression");
4078 return error_mark_node;
4081 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4082 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4084 /* Compound expressions can only occur here if -pedantic or
4085 -pedantic-errors is specified. In the later case, we always want
4086 an error. In the former case, we simply want a warning. */
4087 if (require_constant && pedantic
4088 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4090 inside_init
4091 = valid_compound_expr_initializer (inside_init,
4092 TREE_TYPE (inside_init));
4093 if (inside_init == error_mark_node)
4094 error_init ("initializer element is not constant");
4095 else
4096 pedwarn_init ("initializer element is not constant");
4097 if (flag_pedantic_errors)
4098 inside_init = error_mark_node;
4100 else if (require_constant
4101 && (!TREE_CONSTANT (inside_init)
4102 /* This test catches things like `7 / 0' which
4103 result in an expression for which TREE_CONSTANT
4104 is true, but which is not actually something
4105 that is a legal constant. We really should not
4106 be using this function, because it is a part of
4107 the back-end. Instead, the expression should
4108 already have been turned into ERROR_MARK_NODE. */
4109 || !initializer_constant_valid_p (inside_init,
4110 TREE_TYPE (inside_init))))
4112 error_init ("initializer element is not constant");
4113 inside_init = error_mark_node;
4116 return inside_init;
4119 /* Handle scalar types, including conversions. */
4121 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4122 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4123 || code == VECTOR_TYPE)
4125 /* Note that convert_for_assignment calls default_conversion
4126 for arrays and functions. We must not call it in the
4127 case where inside_init is a null pointer constant. */
4128 inside_init
4129 = convert_for_assignment (type, init, _("initialization"),
4130 NULL_TREE, NULL_TREE, 0);
4132 if (require_constant && ! TREE_CONSTANT (inside_init))
4134 error_init ("initializer element is not constant");
4135 inside_init = error_mark_node;
4137 else if (require_constant
4138 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4140 error_init ("initializer element is not computable at load time");
4141 inside_init = error_mark_node;
4144 return inside_init;
4147 /* Come here only for records and arrays. */
4149 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4151 error_init ("variable-sized object may not be initialized");
4152 return error_mark_node;
4155 error_init ("invalid initializer");
4156 return error_mark_node;
4159 /* Handle initializers that use braces. */
4161 /* Type of object we are accumulating a constructor for.
4162 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4163 static tree constructor_type;
4165 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4166 left to fill. */
4167 static tree constructor_fields;
4169 /* For an ARRAY_TYPE, this is the specified index
4170 at which to store the next element we get. */
4171 static tree constructor_index;
4173 /* For an ARRAY_TYPE, this is the maximum index. */
4174 static tree constructor_max_index;
4176 /* For a RECORD_TYPE, this is the first field not yet written out. */
4177 static tree constructor_unfilled_fields;
4179 /* For an ARRAY_TYPE, this is the index of the first element
4180 not yet written out. */
4181 static tree constructor_unfilled_index;
4183 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4184 This is so we can generate gaps between fields, when appropriate. */
4185 static tree constructor_bit_index;
4187 /* If we are saving up the elements rather than allocating them,
4188 this is the list of elements so far (in reverse order,
4189 most recent first). */
4190 static tree constructor_elements;
4192 /* 1 if constructor should be incrementally stored into a constructor chain,
4193 0 if all the elements should be kept in AVL tree. */
4194 static int constructor_incremental;
4196 /* 1 if so far this constructor's elements are all compile-time constants. */
4197 static int constructor_constant;
4199 /* 1 if so far this constructor's elements are all valid address constants. */
4200 static int constructor_simple;
4202 /* 1 if this constructor is erroneous so far. */
4203 static int constructor_erroneous;
4205 /* Structure for managing pending initializer elements, organized as an
4206 AVL tree. */
4208 struct init_node
4210 struct init_node *left, *right;
4211 struct init_node *parent;
4212 int balance;
4213 tree purpose;
4214 tree value;
4217 /* Tree of pending elements at this constructor level.
4218 These are elements encountered out of order
4219 which belong at places we haven't reached yet in actually
4220 writing the output.
4221 Will never hold tree nodes across GC runs. */
4222 static struct init_node *constructor_pending_elts;
4224 /* The SPELLING_DEPTH of this constructor. */
4225 static int constructor_depth;
4227 /* 0 if implicitly pushing constructor levels is allowed. */
4228 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4230 /* DECL node for which an initializer is being read.
4231 0 means we are reading a constructor expression
4232 such as (struct foo) {...}. */
4233 static tree constructor_decl;
4235 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4236 static const char *constructor_asmspec;
4238 /* Nonzero if this is an initializer for a top-level decl. */
4239 static int constructor_top_level;
4241 /* Nonzero if there were any member designators in this initializer. */
4242 static int constructor_designated;
4244 /* Nesting depth of designator list. */
4245 static int designator_depth;
4247 /* Nonzero if there were diagnosed errors in this designator list. */
4248 static int designator_errorneous;
4251 /* This stack has a level for each implicit or explicit level of
4252 structuring in the initializer, including the outermost one. It
4253 saves the values of most of the variables above. */
4255 struct constructor_range_stack;
4257 struct constructor_stack
4259 struct constructor_stack *next;
4260 tree type;
4261 tree fields;
4262 tree index;
4263 tree max_index;
4264 tree unfilled_index;
4265 tree unfilled_fields;
4266 tree bit_index;
4267 tree elements;
4268 struct init_node *pending_elts;
4269 int offset;
4270 int depth;
4271 /* If nonzero, this value should replace the entire
4272 constructor at this level. */
4273 tree replacement_value;
4274 struct constructor_range_stack *range_stack;
4275 char constant;
4276 char simple;
4277 char implicit;
4278 char erroneous;
4279 char outer;
4280 char incremental;
4281 char designated;
4284 struct constructor_stack *constructor_stack;
4286 /* This stack represents designators from some range designator up to
4287 the last designator in the list. */
4289 struct constructor_range_stack
4291 struct constructor_range_stack *next, *prev;
4292 struct constructor_stack *stack;
4293 tree range_start;
4294 tree index;
4295 tree range_end;
4296 tree fields;
4299 struct constructor_range_stack *constructor_range_stack;
4301 /* This stack records separate initializers that are nested.
4302 Nested initializers can't happen in ANSI C, but GNU C allows them
4303 in cases like { ... (struct foo) { ... } ... }. */
4305 struct initializer_stack
4307 struct initializer_stack *next;
4308 tree decl;
4309 const char *asmspec;
4310 struct constructor_stack *constructor_stack;
4311 struct constructor_range_stack *constructor_range_stack;
4312 tree elements;
4313 struct spelling *spelling;
4314 struct spelling *spelling_base;
4315 int spelling_size;
4316 char top_level;
4317 char require_constant_value;
4318 char require_constant_elements;
4321 struct initializer_stack *initializer_stack;
4323 /* Prepare to parse and output the initializer for variable DECL. */
4325 void
4326 start_init (tree decl, tree asmspec_tree, int top_level)
4328 const char *locus;
4329 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4330 const char *asmspec = 0;
4332 if (asmspec_tree)
4333 asmspec = TREE_STRING_POINTER (asmspec_tree);
4335 p->decl = constructor_decl;
4336 p->asmspec = constructor_asmspec;
4337 p->require_constant_value = require_constant_value;
4338 p->require_constant_elements = require_constant_elements;
4339 p->constructor_stack = constructor_stack;
4340 p->constructor_range_stack = constructor_range_stack;
4341 p->elements = constructor_elements;
4342 p->spelling = spelling;
4343 p->spelling_base = spelling_base;
4344 p->spelling_size = spelling_size;
4345 p->top_level = constructor_top_level;
4346 p->next = initializer_stack;
4347 initializer_stack = p;
4349 constructor_decl = decl;
4350 constructor_asmspec = asmspec;
4351 constructor_designated = 0;
4352 constructor_top_level = top_level;
4354 if (decl != 0)
4356 require_constant_value = TREE_STATIC (decl);
4357 require_constant_elements
4358 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4359 /* For a scalar, you can always use any value to initialize,
4360 even within braces. */
4361 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4362 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4363 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4364 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4365 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4367 else
4369 require_constant_value = 0;
4370 require_constant_elements = 0;
4371 locus = "(anonymous)";
4374 constructor_stack = 0;
4375 constructor_range_stack = 0;
4377 missing_braces_mentioned = 0;
4379 spelling_base = 0;
4380 spelling_size = 0;
4381 RESTORE_SPELLING_DEPTH (0);
4383 if (locus)
4384 push_string (locus);
4387 void
4388 finish_init (void)
4390 struct initializer_stack *p = initializer_stack;
4392 /* Free the whole constructor stack of this initializer. */
4393 while (constructor_stack)
4395 struct constructor_stack *q = constructor_stack;
4396 constructor_stack = q->next;
4397 free (q);
4400 if (constructor_range_stack)
4401 abort ();
4403 /* Pop back to the data of the outer initializer (if any). */
4404 free (spelling_base);
4406 constructor_decl = p->decl;
4407 constructor_asmspec = p->asmspec;
4408 require_constant_value = p->require_constant_value;
4409 require_constant_elements = p->require_constant_elements;
4410 constructor_stack = p->constructor_stack;
4411 constructor_range_stack = p->constructor_range_stack;
4412 constructor_elements = p->elements;
4413 spelling = p->spelling;
4414 spelling_base = p->spelling_base;
4415 spelling_size = p->spelling_size;
4416 constructor_top_level = p->top_level;
4417 initializer_stack = p->next;
4418 free (p);
4421 /* Call here when we see the initializer is surrounded by braces.
4422 This is instead of a call to push_init_level;
4423 it is matched by a call to pop_init_level.
4425 TYPE is the type to initialize, for a constructor expression.
4426 For an initializer for a decl, TYPE is zero. */
4428 void
4429 really_start_incremental_init (tree type)
4431 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4433 if (type == 0)
4434 type = TREE_TYPE (constructor_decl);
4436 if (targetm.vector_opaque_p (type))
4437 error ("opaque vector types cannot be initialized");
4439 p->type = constructor_type;
4440 p->fields = constructor_fields;
4441 p->index = constructor_index;
4442 p->max_index = constructor_max_index;
4443 p->unfilled_index = constructor_unfilled_index;
4444 p->unfilled_fields = constructor_unfilled_fields;
4445 p->bit_index = constructor_bit_index;
4446 p->elements = constructor_elements;
4447 p->constant = constructor_constant;
4448 p->simple = constructor_simple;
4449 p->erroneous = constructor_erroneous;
4450 p->pending_elts = constructor_pending_elts;
4451 p->depth = constructor_depth;
4452 p->replacement_value = 0;
4453 p->implicit = 0;
4454 p->range_stack = 0;
4455 p->outer = 0;
4456 p->incremental = constructor_incremental;
4457 p->designated = constructor_designated;
4458 p->next = 0;
4459 constructor_stack = p;
4461 constructor_constant = 1;
4462 constructor_simple = 1;
4463 constructor_depth = SPELLING_DEPTH ();
4464 constructor_elements = 0;
4465 constructor_pending_elts = 0;
4466 constructor_type = type;
4467 constructor_incremental = 1;
4468 constructor_designated = 0;
4469 designator_depth = 0;
4470 designator_errorneous = 0;
4472 if (TREE_CODE (constructor_type) == RECORD_TYPE
4473 || TREE_CODE (constructor_type) == UNION_TYPE)
4475 constructor_fields = TYPE_FIELDS (constructor_type);
4476 /* Skip any nameless bit fields at the beginning. */
4477 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4478 && DECL_NAME (constructor_fields) == 0)
4479 constructor_fields = TREE_CHAIN (constructor_fields);
4481 constructor_unfilled_fields = constructor_fields;
4482 constructor_bit_index = bitsize_zero_node;
4484 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4486 if (TYPE_DOMAIN (constructor_type))
4488 constructor_max_index
4489 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4491 /* Detect non-empty initializations of zero-length arrays. */
4492 if (constructor_max_index == NULL_TREE
4493 && TYPE_SIZE (constructor_type))
4494 constructor_max_index = build_int_2 (-1, -1);
4496 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4497 to initialize VLAs will cause a proper error; avoid tree
4498 checking errors as well by setting a safe value. */
4499 if (constructor_max_index
4500 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4501 constructor_max_index = build_int_2 (-1, -1);
4503 constructor_index
4504 = convert (bitsizetype,
4505 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4507 else
4508 constructor_index = bitsize_zero_node;
4510 constructor_unfilled_index = constructor_index;
4512 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4514 /* Vectors are like simple fixed-size arrays. */
4515 constructor_max_index =
4516 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4517 constructor_index = convert (bitsizetype, bitsize_zero_node);
4518 constructor_unfilled_index = constructor_index;
4520 else
4522 /* Handle the case of int x = {5}; */
4523 constructor_fields = constructor_type;
4524 constructor_unfilled_fields = constructor_type;
4528 /* Push down into a subobject, for initialization.
4529 If this is for an explicit set of braces, IMPLICIT is 0.
4530 If it is because the next element belongs at a lower level,
4531 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4533 void
4534 push_init_level (int implicit)
4536 struct constructor_stack *p;
4537 tree value = NULL_TREE;
4539 /* If we've exhausted any levels that didn't have braces,
4540 pop them now. */
4541 while (constructor_stack->implicit)
4543 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4544 || TREE_CODE (constructor_type) == UNION_TYPE)
4545 && constructor_fields == 0)
4546 process_init_element (pop_init_level (1));
4547 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4548 && constructor_max_index
4549 && tree_int_cst_lt (constructor_max_index, constructor_index))
4550 process_init_element (pop_init_level (1));
4551 else
4552 break;
4555 /* Unless this is an explicit brace, we need to preserve previous
4556 content if any. */
4557 if (implicit)
4559 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4560 || TREE_CODE (constructor_type) == UNION_TYPE)
4561 && constructor_fields)
4562 value = find_init_member (constructor_fields);
4563 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4564 value = find_init_member (constructor_index);
4567 p = xmalloc (sizeof (struct constructor_stack));
4568 p->type = constructor_type;
4569 p->fields = constructor_fields;
4570 p->index = constructor_index;
4571 p->max_index = constructor_max_index;
4572 p->unfilled_index = constructor_unfilled_index;
4573 p->unfilled_fields = constructor_unfilled_fields;
4574 p->bit_index = constructor_bit_index;
4575 p->elements = constructor_elements;
4576 p->constant = constructor_constant;
4577 p->simple = constructor_simple;
4578 p->erroneous = constructor_erroneous;
4579 p->pending_elts = constructor_pending_elts;
4580 p->depth = constructor_depth;
4581 p->replacement_value = 0;
4582 p->implicit = implicit;
4583 p->outer = 0;
4584 p->incremental = constructor_incremental;
4585 p->designated = constructor_designated;
4586 p->next = constructor_stack;
4587 p->range_stack = 0;
4588 constructor_stack = p;
4590 constructor_constant = 1;
4591 constructor_simple = 1;
4592 constructor_depth = SPELLING_DEPTH ();
4593 constructor_elements = 0;
4594 constructor_incremental = 1;
4595 constructor_designated = 0;
4596 constructor_pending_elts = 0;
4597 if (!implicit)
4599 p->range_stack = constructor_range_stack;
4600 constructor_range_stack = 0;
4601 designator_depth = 0;
4602 designator_errorneous = 0;
4605 /* Don't die if an entire brace-pair level is superfluous
4606 in the containing level. */
4607 if (constructor_type == 0)
4609 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4610 || TREE_CODE (constructor_type) == UNION_TYPE)
4612 /* Don't die if there are extra init elts at the end. */
4613 if (constructor_fields == 0)
4614 constructor_type = 0;
4615 else
4617 constructor_type = TREE_TYPE (constructor_fields);
4618 push_member_name (constructor_fields);
4619 constructor_depth++;
4622 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4624 constructor_type = TREE_TYPE (constructor_type);
4625 push_array_bounds (tree_low_cst (constructor_index, 0));
4626 constructor_depth++;
4629 if (constructor_type == 0)
4631 error_init ("extra brace group at end of initializer");
4632 constructor_fields = 0;
4633 constructor_unfilled_fields = 0;
4634 return;
4637 if (value && TREE_CODE (value) == CONSTRUCTOR)
4639 constructor_constant = TREE_CONSTANT (value);
4640 constructor_simple = TREE_STATIC (value);
4641 constructor_elements = CONSTRUCTOR_ELTS (value);
4642 if (constructor_elements
4643 && (TREE_CODE (constructor_type) == RECORD_TYPE
4644 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4645 set_nonincremental_init ();
4648 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4650 missing_braces_mentioned = 1;
4651 warning_init ("missing braces around initializer");
4654 if (TREE_CODE (constructor_type) == RECORD_TYPE
4655 || TREE_CODE (constructor_type) == UNION_TYPE)
4657 constructor_fields = TYPE_FIELDS (constructor_type);
4658 /* Skip any nameless bit fields at the beginning. */
4659 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4660 && DECL_NAME (constructor_fields) == 0)
4661 constructor_fields = TREE_CHAIN (constructor_fields);
4663 constructor_unfilled_fields = constructor_fields;
4664 constructor_bit_index = bitsize_zero_node;
4666 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4668 /* Vectors are like simple fixed-size arrays. */
4669 constructor_max_index =
4670 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4671 constructor_index = convert (bitsizetype, integer_zero_node);
4672 constructor_unfilled_index = constructor_index;
4674 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4676 if (TYPE_DOMAIN (constructor_type))
4678 constructor_max_index
4679 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4681 /* Detect non-empty initializations of zero-length arrays. */
4682 if (constructor_max_index == NULL_TREE
4683 && TYPE_SIZE (constructor_type))
4684 constructor_max_index = build_int_2 (-1, -1);
4686 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4687 to initialize VLAs will cause a proper error; avoid tree
4688 checking errors as well by setting a safe value. */
4689 if (constructor_max_index
4690 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4691 constructor_max_index = build_int_2 (-1, -1);
4693 constructor_index
4694 = convert (bitsizetype,
4695 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4697 else
4698 constructor_index = bitsize_zero_node;
4700 constructor_unfilled_index = constructor_index;
4701 if (value && TREE_CODE (value) == STRING_CST)
4703 /* We need to split the char/wchar array into individual
4704 characters, so that we don't have to special case it
4705 everywhere. */
4706 set_nonincremental_init_from_string (value);
4709 else
4711 warning_init ("braces around scalar initializer");
4712 constructor_fields = constructor_type;
4713 constructor_unfilled_fields = constructor_type;
4717 /* At the end of an implicit or explicit brace level,
4718 finish up that level of constructor.
4719 If we were outputting the elements as they are read, return 0
4720 from inner levels (process_init_element ignores that),
4721 but return error_mark_node from the outermost level
4722 (that's what we want to put in DECL_INITIAL).
4723 Otherwise, return a CONSTRUCTOR expression. */
4725 tree
4726 pop_init_level (int implicit)
4728 struct constructor_stack *p;
4729 tree constructor = 0;
4731 if (implicit == 0)
4733 /* When we come to an explicit close brace,
4734 pop any inner levels that didn't have explicit braces. */
4735 while (constructor_stack->implicit)
4736 process_init_element (pop_init_level (1));
4738 if (constructor_range_stack)
4739 abort ();
4742 /* Now output all pending elements. */
4743 constructor_incremental = 1;
4744 output_pending_init_elements (1);
4746 p = constructor_stack;
4748 /* Error for initializing a flexible array member, or a zero-length
4749 array member in an inappropriate context. */
4750 if (constructor_type && constructor_fields
4751 && TREE_CODE (constructor_type) == ARRAY_TYPE
4752 && TYPE_DOMAIN (constructor_type)
4753 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4755 /* Silently discard empty initializations. The parser will
4756 already have pedwarned for empty brackets. */
4757 if (integer_zerop (constructor_unfilled_index))
4758 constructor_type = NULL_TREE;
4759 else if (! TYPE_SIZE (constructor_type))
4761 if (constructor_depth > 2)
4762 error_init ("initialization of flexible array member in a nested context");
4763 else if (pedantic)
4764 pedwarn_init ("initialization of a flexible array member");
4766 /* We have already issued an error message for the existence
4767 of a flexible array member not at the end of the structure.
4768 Discard the initializer so that we do not abort later. */
4769 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4770 constructor_type = NULL_TREE;
4772 else
4773 /* Zero-length arrays are no longer special, so we should no longer
4774 get here. */
4775 abort ();
4778 /* Warn when some struct elements are implicitly initialized to zero. */
4779 if (extra_warnings
4780 && constructor_type
4781 && TREE_CODE (constructor_type) == RECORD_TYPE
4782 && constructor_unfilled_fields)
4784 /* Do not warn for flexible array members or zero-length arrays. */
4785 while (constructor_unfilled_fields
4786 && (! DECL_SIZE (constructor_unfilled_fields)
4787 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4788 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4790 /* Do not warn if this level of the initializer uses member
4791 designators; it is likely to be deliberate. */
4792 if (constructor_unfilled_fields && !constructor_designated)
4794 push_member_name (constructor_unfilled_fields);
4795 warning_init ("missing initializer");
4796 RESTORE_SPELLING_DEPTH (constructor_depth);
4800 /* Pad out the end of the structure. */
4801 if (p->replacement_value)
4802 /* If this closes a superfluous brace pair,
4803 just pass out the element between them. */
4804 constructor = p->replacement_value;
4805 else if (constructor_type == 0)
4807 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4808 && TREE_CODE (constructor_type) != UNION_TYPE
4809 && TREE_CODE (constructor_type) != ARRAY_TYPE
4810 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4812 /* A nonincremental scalar initializer--just return
4813 the element, after verifying there is just one. */
4814 if (constructor_elements == 0)
4816 if (!constructor_erroneous)
4817 error_init ("empty scalar initializer");
4818 constructor = error_mark_node;
4820 else if (TREE_CHAIN (constructor_elements) != 0)
4822 error_init ("extra elements in scalar initializer");
4823 constructor = TREE_VALUE (constructor_elements);
4825 else
4826 constructor = TREE_VALUE (constructor_elements);
4828 else
4830 if (constructor_erroneous)
4831 constructor = error_mark_node;
4832 else
4834 constructor = build_constructor (constructor_type,
4835 nreverse (constructor_elements));
4836 if (constructor_constant)
4837 TREE_CONSTANT (constructor) = TREE_INVARIANT (constructor) = 1;
4838 if (constructor_constant && constructor_simple)
4839 TREE_STATIC (constructor) = 1;
4843 constructor_type = p->type;
4844 constructor_fields = p->fields;
4845 constructor_index = p->index;
4846 constructor_max_index = p->max_index;
4847 constructor_unfilled_index = p->unfilled_index;
4848 constructor_unfilled_fields = p->unfilled_fields;
4849 constructor_bit_index = p->bit_index;
4850 constructor_elements = p->elements;
4851 constructor_constant = p->constant;
4852 constructor_simple = p->simple;
4853 constructor_erroneous = p->erroneous;
4854 constructor_incremental = p->incremental;
4855 constructor_designated = p->designated;
4856 constructor_pending_elts = p->pending_elts;
4857 constructor_depth = p->depth;
4858 if (!p->implicit)
4859 constructor_range_stack = p->range_stack;
4860 RESTORE_SPELLING_DEPTH (constructor_depth);
4862 constructor_stack = p->next;
4863 free (p);
4865 if (constructor == 0)
4867 if (constructor_stack == 0)
4868 return error_mark_node;
4869 return NULL_TREE;
4871 return constructor;
4874 /* Common handling for both array range and field name designators.
4875 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4877 static int
4878 set_designator (int array)
4880 tree subtype;
4881 enum tree_code subcode;
4883 /* Don't die if an entire brace-pair level is superfluous
4884 in the containing level. */
4885 if (constructor_type == 0)
4886 return 1;
4888 /* If there were errors in this designator list already, bail out silently. */
4889 if (designator_errorneous)
4890 return 1;
4892 if (!designator_depth)
4894 if (constructor_range_stack)
4895 abort ();
4897 /* Designator list starts at the level of closest explicit
4898 braces. */
4899 while (constructor_stack->implicit)
4900 process_init_element (pop_init_level (1));
4901 constructor_designated = 1;
4902 return 0;
4905 if (constructor_no_implicit)
4907 error_init ("initialization designators may not nest");
4908 return 1;
4911 if (TREE_CODE (constructor_type) == RECORD_TYPE
4912 || TREE_CODE (constructor_type) == UNION_TYPE)
4914 subtype = TREE_TYPE (constructor_fields);
4915 if (subtype != error_mark_node)
4916 subtype = TYPE_MAIN_VARIANT (subtype);
4918 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4920 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4922 else
4923 abort ();
4925 subcode = TREE_CODE (subtype);
4926 if (array && subcode != ARRAY_TYPE)
4928 error_init ("array index in non-array initializer");
4929 return 1;
4931 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4933 error_init ("field name not in record or union initializer");
4934 return 1;
4937 constructor_designated = 1;
4938 push_init_level (2);
4939 return 0;
4942 /* If there are range designators in designator list, push a new designator
4943 to constructor_range_stack. RANGE_END is end of such stack range or
4944 NULL_TREE if there is no range designator at this level. */
4946 static void
4947 push_range_stack (tree range_end)
4949 struct constructor_range_stack *p;
4951 p = ggc_alloc (sizeof (struct constructor_range_stack));
4952 p->prev = constructor_range_stack;
4953 p->next = 0;
4954 p->fields = constructor_fields;
4955 p->range_start = constructor_index;
4956 p->index = constructor_index;
4957 p->stack = constructor_stack;
4958 p->range_end = range_end;
4959 if (constructor_range_stack)
4960 constructor_range_stack->next = p;
4961 constructor_range_stack = p;
4964 /* Within an array initializer, specify the next index to be initialized.
4965 FIRST is that index. If LAST is nonzero, then initialize a range
4966 of indices, running from FIRST through LAST. */
4968 void
4969 set_init_index (tree first, tree last)
4971 if (set_designator (1))
4972 return;
4974 designator_errorneous = 1;
4976 while ((TREE_CODE (first) == NOP_EXPR
4977 || TREE_CODE (first) == CONVERT_EXPR
4978 || TREE_CODE (first) == NON_LVALUE_EXPR)
4979 && (TYPE_MODE (TREE_TYPE (first))
4980 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4981 first = TREE_OPERAND (first, 0);
4983 if (last)
4984 while ((TREE_CODE (last) == NOP_EXPR
4985 || TREE_CODE (last) == CONVERT_EXPR
4986 || TREE_CODE (last) == NON_LVALUE_EXPR)
4987 && (TYPE_MODE (TREE_TYPE (last))
4988 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4989 last = TREE_OPERAND (last, 0);
4991 if (TREE_CODE (first) != INTEGER_CST)
4992 error_init ("nonconstant array index in initializer");
4993 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4994 error_init ("nonconstant array index in initializer");
4995 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4996 error_init ("array index in non-array initializer");
4997 else if (tree_int_cst_sgn (first) == -1)
4998 error_init ("array index in initializer exceeds array bounds");
4999 else if (constructor_max_index
5000 && tree_int_cst_lt (constructor_max_index, first))
5001 error_init ("array index in initializer exceeds array bounds");
5002 else
5004 constructor_index = convert (bitsizetype, first);
5006 if (last)
5008 if (tree_int_cst_equal (first, last))
5009 last = 0;
5010 else if (tree_int_cst_lt (last, first))
5012 error_init ("empty index range in initializer");
5013 last = 0;
5015 else
5017 last = convert (bitsizetype, last);
5018 if (constructor_max_index != 0
5019 && tree_int_cst_lt (constructor_max_index, last))
5021 error_init ("array index range in initializer exceeds array bounds");
5022 last = 0;
5027 designator_depth++;
5028 designator_errorneous = 0;
5029 if (constructor_range_stack || last)
5030 push_range_stack (last);
5034 /* Within a struct initializer, specify the next field to be initialized. */
5036 void
5037 set_init_label (tree fieldname)
5039 tree tail;
5041 if (set_designator (0))
5042 return;
5044 designator_errorneous = 1;
5046 if (TREE_CODE (constructor_type) != RECORD_TYPE
5047 && TREE_CODE (constructor_type) != UNION_TYPE)
5049 error_init ("field name not in record or union initializer");
5050 return;
5053 for (tail = TYPE_FIELDS (constructor_type); tail;
5054 tail = TREE_CHAIN (tail))
5056 if (DECL_NAME (tail) == fieldname)
5057 break;
5060 if (tail == 0)
5061 error ("unknown field `%s' specified in initializer",
5062 IDENTIFIER_POINTER (fieldname));
5063 else
5065 constructor_fields = tail;
5066 designator_depth++;
5067 designator_errorneous = 0;
5068 if (constructor_range_stack)
5069 push_range_stack (NULL_TREE);
5073 /* Add a new initializer to the tree of pending initializers. PURPOSE
5074 identifies the initializer, either array index or field in a structure.
5075 VALUE is the value of that index or field. */
5077 static void
5078 add_pending_init (tree purpose, tree value)
5080 struct init_node *p, **q, *r;
5082 q = &constructor_pending_elts;
5083 p = 0;
5085 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5087 while (*q != 0)
5089 p = *q;
5090 if (tree_int_cst_lt (purpose, p->purpose))
5091 q = &p->left;
5092 else if (tree_int_cst_lt (p->purpose, purpose))
5093 q = &p->right;
5094 else
5096 if (TREE_SIDE_EFFECTS (p->value))
5097 warning_init ("initialized field with side-effects overwritten");
5098 p->value = value;
5099 return;
5103 else
5105 tree bitpos;
5107 bitpos = bit_position (purpose);
5108 while (*q != NULL)
5110 p = *q;
5111 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5112 q = &p->left;
5113 else if (p->purpose != purpose)
5114 q = &p->right;
5115 else
5117 if (TREE_SIDE_EFFECTS (p->value))
5118 warning_init ("initialized field with side-effects overwritten");
5119 p->value = value;
5120 return;
5125 r = ggc_alloc (sizeof (struct init_node));
5126 r->purpose = purpose;
5127 r->value = value;
5129 *q = r;
5130 r->parent = p;
5131 r->left = 0;
5132 r->right = 0;
5133 r->balance = 0;
5135 while (p)
5137 struct init_node *s;
5139 if (r == p->left)
5141 if (p->balance == 0)
5142 p->balance = -1;
5143 else if (p->balance < 0)
5145 if (r->balance < 0)
5147 /* L rotation. */
5148 p->left = r->right;
5149 if (p->left)
5150 p->left->parent = p;
5151 r->right = p;
5153 p->balance = 0;
5154 r->balance = 0;
5156 s = p->parent;
5157 p->parent = r;
5158 r->parent = s;
5159 if (s)
5161 if (s->left == p)
5162 s->left = r;
5163 else
5164 s->right = r;
5166 else
5167 constructor_pending_elts = r;
5169 else
5171 /* LR rotation. */
5172 struct init_node *t = r->right;
5174 r->right = t->left;
5175 if (r->right)
5176 r->right->parent = r;
5177 t->left = r;
5179 p->left = t->right;
5180 if (p->left)
5181 p->left->parent = p;
5182 t->right = p;
5184 p->balance = t->balance < 0;
5185 r->balance = -(t->balance > 0);
5186 t->balance = 0;
5188 s = p->parent;
5189 p->parent = t;
5190 r->parent = t;
5191 t->parent = s;
5192 if (s)
5194 if (s->left == p)
5195 s->left = t;
5196 else
5197 s->right = t;
5199 else
5200 constructor_pending_elts = t;
5202 break;
5204 else
5206 /* p->balance == +1; growth of left side balances the node. */
5207 p->balance = 0;
5208 break;
5211 else /* r == p->right */
5213 if (p->balance == 0)
5214 /* Growth propagation from right side. */
5215 p->balance++;
5216 else if (p->balance > 0)
5218 if (r->balance > 0)
5220 /* R rotation. */
5221 p->right = r->left;
5222 if (p->right)
5223 p->right->parent = p;
5224 r->left = p;
5226 p->balance = 0;
5227 r->balance = 0;
5229 s = p->parent;
5230 p->parent = r;
5231 r->parent = s;
5232 if (s)
5234 if (s->left == p)
5235 s->left = r;
5236 else
5237 s->right = r;
5239 else
5240 constructor_pending_elts = r;
5242 else /* r->balance == -1 */
5244 /* RL rotation */
5245 struct init_node *t = r->left;
5247 r->left = t->right;
5248 if (r->left)
5249 r->left->parent = r;
5250 t->right = r;
5252 p->right = t->left;
5253 if (p->right)
5254 p->right->parent = p;
5255 t->left = p;
5257 r->balance = (t->balance < 0);
5258 p->balance = -(t->balance > 0);
5259 t->balance = 0;
5261 s = p->parent;
5262 p->parent = t;
5263 r->parent = t;
5264 t->parent = s;
5265 if (s)
5267 if (s->left == p)
5268 s->left = t;
5269 else
5270 s->right = t;
5272 else
5273 constructor_pending_elts = t;
5275 break;
5277 else
5279 /* p->balance == -1; growth of right side balances the node. */
5280 p->balance = 0;
5281 break;
5285 r = p;
5286 p = p->parent;
5290 /* Build AVL tree from a sorted chain. */
5292 static void
5293 set_nonincremental_init (void)
5295 tree chain;
5297 if (TREE_CODE (constructor_type) != RECORD_TYPE
5298 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5299 return;
5301 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5302 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5303 constructor_elements = 0;
5304 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5306 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5307 /* Skip any nameless bit fields at the beginning. */
5308 while (constructor_unfilled_fields != 0
5309 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5310 && DECL_NAME (constructor_unfilled_fields) == 0)
5311 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5314 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5316 if (TYPE_DOMAIN (constructor_type))
5317 constructor_unfilled_index
5318 = convert (bitsizetype,
5319 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5320 else
5321 constructor_unfilled_index = bitsize_zero_node;
5323 constructor_incremental = 0;
5326 /* Build AVL tree from a string constant. */
5328 static void
5329 set_nonincremental_init_from_string (tree str)
5331 tree value, purpose, type;
5332 HOST_WIDE_INT val[2];
5333 const char *p, *end;
5334 int byte, wchar_bytes, charwidth, bitpos;
5336 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5337 abort ();
5339 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5340 == TYPE_PRECISION (char_type_node))
5341 wchar_bytes = 1;
5342 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5343 == TYPE_PRECISION (wchar_type_node))
5344 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5345 else
5346 abort ();
5348 charwidth = TYPE_PRECISION (char_type_node);
5349 type = TREE_TYPE (constructor_type);
5350 p = TREE_STRING_POINTER (str);
5351 end = p + TREE_STRING_LENGTH (str);
5353 for (purpose = bitsize_zero_node;
5354 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5355 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5357 if (wchar_bytes == 1)
5359 val[1] = (unsigned char) *p++;
5360 val[0] = 0;
5362 else
5364 val[0] = 0;
5365 val[1] = 0;
5366 for (byte = 0; byte < wchar_bytes; byte++)
5368 if (BYTES_BIG_ENDIAN)
5369 bitpos = (wchar_bytes - byte - 1) * charwidth;
5370 else
5371 bitpos = byte * charwidth;
5372 val[bitpos < HOST_BITS_PER_WIDE_INT]
5373 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5374 << (bitpos % HOST_BITS_PER_WIDE_INT);
5378 if (!TYPE_UNSIGNED (type))
5380 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5381 if (bitpos < HOST_BITS_PER_WIDE_INT)
5383 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5385 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5386 val[0] = -1;
5389 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5391 if (val[1] < 0)
5392 val[0] = -1;
5394 else if (val[0] & (((HOST_WIDE_INT) 1)
5395 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5396 val[0] |= ((HOST_WIDE_INT) -1)
5397 << (bitpos - HOST_BITS_PER_WIDE_INT);
5400 value = build_int_2 (val[1], val[0]);
5401 TREE_TYPE (value) = type;
5402 add_pending_init (purpose, value);
5405 constructor_incremental = 0;
5408 /* Return value of FIELD in pending initializer or zero if the field was
5409 not initialized yet. */
5411 static tree
5412 find_init_member (tree field)
5414 struct init_node *p;
5416 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5418 if (constructor_incremental
5419 && tree_int_cst_lt (field, constructor_unfilled_index))
5420 set_nonincremental_init ();
5422 p = constructor_pending_elts;
5423 while (p)
5425 if (tree_int_cst_lt (field, p->purpose))
5426 p = p->left;
5427 else if (tree_int_cst_lt (p->purpose, field))
5428 p = p->right;
5429 else
5430 return p->value;
5433 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5435 tree bitpos = bit_position (field);
5437 if (constructor_incremental
5438 && (!constructor_unfilled_fields
5439 || tree_int_cst_lt (bitpos,
5440 bit_position (constructor_unfilled_fields))))
5441 set_nonincremental_init ();
5443 p = constructor_pending_elts;
5444 while (p)
5446 if (field == p->purpose)
5447 return p->value;
5448 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5449 p = p->left;
5450 else
5451 p = p->right;
5454 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5456 if (constructor_elements
5457 && TREE_PURPOSE (constructor_elements) == field)
5458 return TREE_VALUE (constructor_elements);
5460 return 0;
5463 /* "Output" the next constructor element.
5464 At top level, really output it to assembler code now.
5465 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5466 TYPE is the data type that the containing data type wants here.
5467 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5469 PENDING if non-nil means output pending elements that belong
5470 right after this element. (PENDING is normally 1;
5471 it is 0 while outputting pending elements, to avoid recursion.) */
5473 static void
5474 output_init_element (tree value, tree type, tree field, int pending)
5476 if (type == error_mark_node)
5478 constructor_erroneous = 1;
5479 return;
5481 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5482 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5483 && !(TREE_CODE (value) == STRING_CST
5484 && TREE_CODE (type) == ARRAY_TYPE
5485 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5486 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5487 TYPE_MAIN_VARIANT (type))))
5488 value = default_conversion (value);
5490 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5491 && require_constant_value && !flag_isoc99 && pending)
5493 /* As an extension, allow initializing objects with static storage
5494 duration with compound literals (which are then treated just as
5495 the brace enclosed list they contain). */
5496 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5497 value = DECL_INITIAL (decl);
5500 if (value == error_mark_node)
5501 constructor_erroneous = 1;
5502 else if (!TREE_CONSTANT (value))
5503 constructor_constant = 0;
5504 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5505 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5506 || TREE_CODE (constructor_type) == UNION_TYPE)
5507 && DECL_C_BIT_FIELD (field)
5508 && TREE_CODE (value) != INTEGER_CST))
5509 constructor_simple = 0;
5511 if (require_constant_value && ! TREE_CONSTANT (value))
5513 error_init ("initializer element is not constant");
5514 value = error_mark_node;
5516 else if (require_constant_elements
5517 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5518 pedwarn ("initializer element is not computable at load time");
5520 /* If this field is empty (and not at the end of structure),
5521 don't do anything other than checking the initializer. */
5522 if (field
5523 && (TREE_TYPE (field) == error_mark_node
5524 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5525 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5526 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5527 || TREE_CHAIN (field)))))
5528 return;
5530 value = digest_init (type, value, require_constant_value);
5531 if (value == error_mark_node)
5533 constructor_erroneous = 1;
5534 return;
5537 /* If this element doesn't come next in sequence,
5538 put it on constructor_pending_elts. */
5539 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5540 && (!constructor_incremental
5541 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5543 if (constructor_incremental
5544 && tree_int_cst_lt (field, constructor_unfilled_index))
5545 set_nonincremental_init ();
5547 add_pending_init (field, value);
5548 return;
5550 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5551 && (!constructor_incremental
5552 || field != constructor_unfilled_fields))
5554 /* We do this for records but not for unions. In a union,
5555 no matter which field is specified, it can be initialized
5556 right away since it starts at the beginning of the union. */
5557 if (constructor_incremental)
5559 if (!constructor_unfilled_fields)
5560 set_nonincremental_init ();
5561 else
5563 tree bitpos, unfillpos;
5565 bitpos = bit_position (field);
5566 unfillpos = bit_position (constructor_unfilled_fields);
5568 if (tree_int_cst_lt (bitpos, unfillpos))
5569 set_nonincremental_init ();
5573 add_pending_init (field, value);
5574 return;
5576 else if (TREE_CODE (constructor_type) == UNION_TYPE
5577 && constructor_elements)
5579 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5580 warning_init ("initialized field with side-effects overwritten");
5582 /* We can have just one union field set. */
5583 constructor_elements = 0;
5586 /* Otherwise, output this element either to
5587 constructor_elements or to the assembler file. */
5589 if (field && TREE_CODE (field) == INTEGER_CST)
5590 field = copy_node (field);
5591 constructor_elements
5592 = tree_cons (field, value, constructor_elements);
5594 /* Advance the variable that indicates sequential elements output. */
5595 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5596 constructor_unfilled_index
5597 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5598 bitsize_one_node);
5599 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5601 constructor_unfilled_fields
5602 = TREE_CHAIN (constructor_unfilled_fields);
5604 /* Skip any nameless bit fields. */
5605 while (constructor_unfilled_fields != 0
5606 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5607 && DECL_NAME (constructor_unfilled_fields) == 0)
5608 constructor_unfilled_fields =
5609 TREE_CHAIN (constructor_unfilled_fields);
5611 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5612 constructor_unfilled_fields = 0;
5614 /* Now output any pending elements which have become next. */
5615 if (pending)
5616 output_pending_init_elements (0);
5619 /* Output any pending elements which have become next.
5620 As we output elements, constructor_unfilled_{fields,index}
5621 advances, which may cause other elements to become next;
5622 if so, they too are output.
5624 If ALL is 0, we return when there are
5625 no more pending elements to output now.
5627 If ALL is 1, we output space as necessary so that
5628 we can output all the pending elements. */
5630 static void
5631 output_pending_init_elements (int all)
5633 struct init_node *elt = constructor_pending_elts;
5634 tree next;
5636 retry:
5638 /* Look through the whole pending tree.
5639 If we find an element that should be output now,
5640 output it. Otherwise, set NEXT to the element
5641 that comes first among those still pending. */
5643 next = 0;
5644 while (elt)
5646 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5648 if (tree_int_cst_equal (elt->purpose,
5649 constructor_unfilled_index))
5650 output_init_element (elt->value,
5651 TREE_TYPE (constructor_type),
5652 constructor_unfilled_index, 0);
5653 else if (tree_int_cst_lt (constructor_unfilled_index,
5654 elt->purpose))
5656 /* Advance to the next smaller node. */
5657 if (elt->left)
5658 elt = elt->left;
5659 else
5661 /* We have reached the smallest node bigger than the
5662 current unfilled index. Fill the space first. */
5663 next = elt->purpose;
5664 break;
5667 else
5669 /* Advance to the next bigger node. */
5670 if (elt->right)
5671 elt = elt->right;
5672 else
5674 /* We have reached the biggest node in a subtree. Find
5675 the parent of it, which is the next bigger node. */
5676 while (elt->parent && elt->parent->right == elt)
5677 elt = elt->parent;
5678 elt = elt->parent;
5679 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5680 elt->purpose))
5682 next = elt->purpose;
5683 break;
5688 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5689 || TREE_CODE (constructor_type) == UNION_TYPE)
5691 tree ctor_unfilled_bitpos, elt_bitpos;
5693 /* If the current record is complete we are done. */
5694 if (constructor_unfilled_fields == 0)
5695 break;
5697 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5698 elt_bitpos = bit_position (elt->purpose);
5699 /* We can't compare fields here because there might be empty
5700 fields in between. */
5701 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5703 constructor_unfilled_fields = elt->purpose;
5704 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5705 elt->purpose, 0);
5707 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5709 /* Advance to the next smaller node. */
5710 if (elt->left)
5711 elt = elt->left;
5712 else
5714 /* We have reached the smallest node bigger than the
5715 current unfilled field. Fill the space first. */
5716 next = elt->purpose;
5717 break;
5720 else
5722 /* Advance to the next bigger node. */
5723 if (elt->right)
5724 elt = elt->right;
5725 else
5727 /* We have reached the biggest node in a subtree. Find
5728 the parent of it, which is the next bigger node. */
5729 while (elt->parent && elt->parent->right == elt)
5730 elt = elt->parent;
5731 elt = elt->parent;
5732 if (elt
5733 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5734 bit_position (elt->purpose))))
5736 next = elt->purpose;
5737 break;
5744 /* Ordinarily return, but not if we want to output all
5745 and there are elements left. */
5746 if (! (all && next != 0))
5747 return;
5749 /* If it's not incremental, just skip over the gap, so that after
5750 jumping to retry we will output the next successive element. */
5751 if (TREE_CODE (constructor_type) == RECORD_TYPE
5752 || TREE_CODE (constructor_type) == UNION_TYPE)
5753 constructor_unfilled_fields = next;
5754 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5755 constructor_unfilled_index = next;
5757 /* ELT now points to the node in the pending tree with the next
5758 initializer to output. */
5759 goto retry;
5762 /* Add one non-braced element to the current constructor level.
5763 This adjusts the current position within the constructor's type.
5764 This may also start or terminate implicit levels
5765 to handle a partly-braced initializer.
5767 Once this has found the correct level for the new element,
5768 it calls output_init_element. */
5770 void
5771 process_init_element (tree value)
5773 tree orig_value = value;
5774 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5776 designator_depth = 0;
5777 designator_errorneous = 0;
5779 /* Handle superfluous braces around string cst as in
5780 char x[] = {"foo"}; */
5781 if (string_flag
5782 && constructor_type
5783 && TREE_CODE (constructor_type) == ARRAY_TYPE
5784 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5785 && integer_zerop (constructor_unfilled_index))
5787 if (constructor_stack->replacement_value)
5788 error_init ("excess elements in char array initializer");
5789 constructor_stack->replacement_value = value;
5790 return;
5793 if (constructor_stack->replacement_value != 0)
5795 error_init ("excess elements in struct initializer");
5796 return;
5799 /* Ignore elements of a brace group if it is entirely superfluous
5800 and has already been diagnosed. */
5801 if (constructor_type == 0)
5802 return;
5804 /* If we've exhausted any levels that didn't have braces,
5805 pop them now. */
5806 while (constructor_stack->implicit)
5808 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5809 || TREE_CODE (constructor_type) == UNION_TYPE)
5810 && constructor_fields == 0)
5811 process_init_element (pop_init_level (1));
5812 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5813 && (constructor_max_index == 0
5814 || tree_int_cst_lt (constructor_max_index,
5815 constructor_index)))
5816 process_init_element (pop_init_level (1));
5817 else
5818 break;
5821 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5822 if (constructor_range_stack)
5824 /* If value is a compound literal and we'll be just using its
5825 content, don't put it into a SAVE_EXPR. */
5826 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5827 || !require_constant_value
5828 || flag_isoc99)
5829 value = save_expr (value);
5832 while (1)
5834 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5836 tree fieldtype;
5837 enum tree_code fieldcode;
5839 if (constructor_fields == 0)
5841 pedwarn_init ("excess elements in struct initializer");
5842 break;
5845 fieldtype = TREE_TYPE (constructor_fields);
5846 if (fieldtype != error_mark_node)
5847 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5848 fieldcode = TREE_CODE (fieldtype);
5850 /* Error for non-static initialization of a flexible array member. */
5851 if (fieldcode == ARRAY_TYPE
5852 && !require_constant_value
5853 && TYPE_SIZE (fieldtype) == NULL_TREE
5854 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5856 error_init ("non-static initialization of a flexible array member");
5857 break;
5860 /* Accept a string constant to initialize a subarray. */
5861 if (value != 0
5862 && fieldcode == ARRAY_TYPE
5863 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5864 && string_flag)
5865 value = orig_value;
5866 /* Otherwise, if we have come to a subaggregate,
5867 and we don't have an element of its type, push into it. */
5868 else if (value != 0 && !constructor_no_implicit
5869 && value != error_mark_node
5870 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5871 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5872 || fieldcode == UNION_TYPE))
5874 push_init_level (1);
5875 continue;
5878 if (value)
5880 push_member_name (constructor_fields);
5881 output_init_element (value, fieldtype, constructor_fields, 1);
5882 RESTORE_SPELLING_DEPTH (constructor_depth);
5884 else
5885 /* Do the bookkeeping for an element that was
5886 directly output as a constructor. */
5888 /* For a record, keep track of end position of last field. */
5889 if (DECL_SIZE (constructor_fields))
5890 constructor_bit_index
5891 = size_binop (PLUS_EXPR,
5892 bit_position (constructor_fields),
5893 DECL_SIZE (constructor_fields));
5895 /* If the current field was the first one not yet written out,
5896 it isn't now, so update. */
5897 if (constructor_unfilled_fields == constructor_fields)
5899 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5900 /* Skip any nameless bit fields. */
5901 while (constructor_unfilled_fields != 0
5902 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5903 && DECL_NAME (constructor_unfilled_fields) == 0)
5904 constructor_unfilled_fields =
5905 TREE_CHAIN (constructor_unfilled_fields);
5909 constructor_fields = TREE_CHAIN (constructor_fields);
5910 /* Skip any nameless bit fields at the beginning. */
5911 while (constructor_fields != 0
5912 && DECL_C_BIT_FIELD (constructor_fields)
5913 && DECL_NAME (constructor_fields) == 0)
5914 constructor_fields = TREE_CHAIN (constructor_fields);
5916 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5918 tree fieldtype;
5919 enum tree_code fieldcode;
5921 if (constructor_fields == 0)
5923 pedwarn_init ("excess elements in union initializer");
5924 break;
5927 fieldtype = TREE_TYPE (constructor_fields);
5928 if (fieldtype != error_mark_node)
5929 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5930 fieldcode = TREE_CODE (fieldtype);
5932 /* Warn that traditional C rejects initialization of unions.
5933 We skip the warning if the value is zero. This is done
5934 under the assumption that the zero initializer in user
5935 code appears conditioned on e.g. __STDC__ to avoid
5936 "missing initializer" warnings and relies on default
5937 initialization to zero in the traditional C case.
5938 We also skip the warning if the initializer is designated,
5939 again on the assumption that this must be conditional on
5940 __STDC__ anyway (and we've already complained about the
5941 member-designator already). */
5942 if (warn_traditional && !in_system_header && !constructor_designated
5943 && !(value && (integer_zerop (value) || real_zerop (value))))
5944 warning ("traditional C rejects initialization of unions");
5946 /* Accept a string constant to initialize a subarray. */
5947 if (value != 0
5948 && fieldcode == ARRAY_TYPE
5949 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5950 && string_flag)
5951 value = orig_value;
5952 /* Otherwise, if we have come to a subaggregate,
5953 and we don't have an element of its type, push into it. */
5954 else if (value != 0 && !constructor_no_implicit
5955 && value != error_mark_node
5956 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5957 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5958 || fieldcode == UNION_TYPE))
5960 push_init_level (1);
5961 continue;
5964 if (value)
5966 push_member_name (constructor_fields);
5967 output_init_element (value, fieldtype, constructor_fields, 1);
5968 RESTORE_SPELLING_DEPTH (constructor_depth);
5970 else
5971 /* Do the bookkeeping for an element that was
5972 directly output as a constructor. */
5974 constructor_bit_index = DECL_SIZE (constructor_fields);
5975 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5978 constructor_fields = 0;
5980 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5982 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5983 enum tree_code eltcode = TREE_CODE (elttype);
5985 /* Accept a string constant to initialize a subarray. */
5986 if (value != 0
5987 && eltcode == ARRAY_TYPE
5988 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5989 && string_flag)
5990 value = orig_value;
5991 /* Otherwise, if we have come to a subaggregate,
5992 and we don't have an element of its type, push into it. */
5993 else if (value != 0 && !constructor_no_implicit
5994 && value != error_mark_node
5995 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5996 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5997 || eltcode == UNION_TYPE))
5999 push_init_level (1);
6000 continue;
6003 if (constructor_max_index != 0
6004 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6005 || integer_all_onesp (constructor_max_index)))
6007 pedwarn_init ("excess elements in array initializer");
6008 break;
6011 /* Now output the actual element. */
6012 if (value)
6014 push_array_bounds (tree_low_cst (constructor_index, 0));
6015 output_init_element (value, elttype, constructor_index, 1);
6016 RESTORE_SPELLING_DEPTH (constructor_depth);
6019 constructor_index
6020 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6022 if (! value)
6023 /* If we are doing the bookkeeping for an element that was
6024 directly output as a constructor, we must update
6025 constructor_unfilled_index. */
6026 constructor_unfilled_index = constructor_index;
6028 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6030 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6032 /* Do a basic check of initializer size. Note that vectors
6033 always have a fixed size derived from their type. */
6034 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6036 pedwarn_init ("excess elements in vector initializer");
6037 break;
6040 /* Now output the actual element. */
6041 if (value)
6042 output_init_element (value, elttype, constructor_index, 1);
6044 constructor_index
6045 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6047 if (! value)
6048 /* If we are doing the bookkeeping for an element that was
6049 directly output as a constructor, we must update
6050 constructor_unfilled_index. */
6051 constructor_unfilled_index = constructor_index;
6054 /* Handle the sole element allowed in a braced initializer
6055 for a scalar variable. */
6056 else if (constructor_fields == 0)
6058 pedwarn_init ("excess elements in scalar initializer");
6059 break;
6061 else
6063 if (value)
6064 output_init_element (value, constructor_type, NULL_TREE, 1);
6065 constructor_fields = 0;
6068 /* Handle range initializers either at this level or anywhere higher
6069 in the designator stack. */
6070 if (constructor_range_stack)
6072 struct constructor_range_stack *p, *range_stack;
6073 int finish = 0;
6075 range_stack = constructor_range_stack;
6076 constructor_range_stack = 0;
6077 while (constructor_stack != range_stack->stack)
6079 if (!constructor_stack->implicit)
6080 abort ();
6081 process_init_element (pop_init_level (1));
6083 for (p = range_stack;
6084 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6085 p = p->prev)
6087 if (!constructor_stack->implicit)
6088 abort ();
6089 process_init_element (pop_init_level (1));
6092 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6093 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6094 finish = 1;
6096 while (1)
6098 constructor_index = p->index;
6099 constructor_fields = p->fields;
6100 if (finish && p->range_end && p->index == p->range_start)
6102 finish = 0;
6103 p->prev = 0;
6105 p = p->next;
6106 if (!p)
6107 break;
6108 push_init_level (2);
6109 p->stack = constructor_stack;
6110 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6111 p->index = p->range_start;
6114 if (!finish)
6115 constructor_range_stack = range_stack;
6116 continue;
6119 break;
6122 constructor_range_stack = 0;
6125 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6126 (guaranteed to be 'volatile' or null) and ARGS (represented using
6127 an ASM_EXPR node). */
6128 tree
6129 build_asm_stmt (tree cv_qualifier, tree args)
6131 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6132 ASM_VOLATILE_P (args) = 1;
6133 return add_stmt (args);
6136 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6137 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6138 SIMPLE indicates whether there was anything at all after the
6139 string in the asm expression -- asm("blah") and asm("blah" : )
6140 are subtly different. We use a ASM_EXPR node to represent this. */
6141 tree
6142 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6143 bool simple)
6145 tree tail;
6146 tree args;
6147 int i;
6148 const char *constraint;
6149 bool allows_mem, allows_reg, is_inout;
6150 int ninputs;
6151 int noutputs;
6153 ninputs = list_length (inputs);
6154 noutputs = list_length (outputs);
6156 /* Remove output conversions that change the type but not the mode. */
6157 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6159 tree output = TREE_VALUE (tail);
6160 STRIP_NOPS (output);
6161 TREE_VALUE (tail) = output;
6162 lvalue_or_else (output, "invalid lvalue in asm statement");
6164 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6166 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
6167 &allows_mem, &allows_reg, &is_inout))
6169 /* By marking this operand as erroneous, we will not try
6170 to process this operand again in expand_asm_operands. */
6171 TREE_VALUE (tail) = error_mark_node;
6172 continue;
6175 /* If the operand is a DECL that is going to end up in
6176 memory, assume it is addressable. This is a bit more
6177 conservative than it would ideally be; the exact test is
6178 buried deep in expand_asm_operands and depends on the
6179 DECL_RTL for the OPERAND -- which we don't have at this
6180 point. */
6181 if (!allows_reg && DECL_P (output))
6182 c_mark_addressable (output);
6185 /* Perform default conversions on array and function inputs.
6186 Don't do this for other types as it would screw up operands
6187 expected to be in memory. */
6188 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6189 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6191 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6193 /* Simple asm statements are treated as volatile. */
6194 if (simple)
6196 ASM_VOLATILE_P (args) = 1;
6197 ASM_INPUT_P (args) = 1;
6199 return args;
6202 /* Expand an ASM statement with operands, handling output operands
6203 that are not variables or INDIRECT_REFS by transforming such
6204 cases into cases that expand_asm_operands can handle.
6206 Arguments are same as for expand_asm_operands. */
6208 void
6209 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6210 tree clobbers, int vol, location_t locus)
6212 int noutputs = list_length (outputs);
6213 int i;
6214 /* o[I] is the place that output number I should be written. */
6215 tree *o = alloca (noutputs * sizeof (tree));
6216 tree tail;
6218 /* Record the contents of OUTPUTS before it is modified. */
6219 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6221 o[i] = TREE_VALUE (tail);
6222 if (o[i] == error_mark_node)
6223 return;
6226 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6227 OUTPUTS some trees for where the values were actually stored. */
6228 expand_asm_operands (string, outputs, inputs, clobbers, vol, locus);
6230 /* Copy all the intermediate outputs into the specified outputs. */
6231 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6233 if (o[i] != TREE_VALUE (tail))
6235 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6236 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6237 free_temp_slots ();
6239 /* Restore the original value so that it's correct the next
6240 time we expand this function. */
6241 TREE_VALUE (tail) = o[i];
6243 /* Detect modification of read-only values.
6244 (Otherwise done by build_modify_expr.) */
6245 else
6247 tree type = TREE_TYPE (o[i]);
6248 if (TREE_READONLY (o[i])
6249 || TYPE_READONLY (type)
6250 || ((TREE_CODE (type) == RECORD_TYPE
6251 || TREE_CODE (type) == UNION_TYPE)
6252 && C_TYPE_FIELDS_READONLY (type)))
6253 readonly_error (o[i], "modification by `asm'");
6257 /* Those MODIFY_EXPRs could do autoincrements. */
6258 emit_queue ();
6261 /* Expand a C `return' statement.
6262 RETVAL is the expression for what to return,
6263 or a null pointer for `return;' with no value. */
6265 tree
6266 c_expand_return (tree retval)
6268 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6270 if (TREE_THIS_VOLATILE (current_function_decl))
6271 warning ("function declared `noreturn' has a `return' statement");
6273 if (!retval)
6275 current_function_returns_null = 1;
6276 if ((warn_return_type || flag_isoc99)
6277 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6278 pedwarn_c99 ("`return' with no value, in function returning non-void");
6280 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6282 current_function_returns_null = 1;
6283 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6284 pedwarn ("`return' with a value, in function returning void");
6286 else
6288 tree t = convert_for_assignment (valtype, retval, _("return"),
6289 NULL_TREE, NULL_TREE, 0);
6290 tree res = DECL_RESULT (current_function_decl);
6291 tree inner;
6293 current_function_returns_value = 1;
6294 if (t == error_mark_node)
6295 return NULL_TREE;
6297 inner = t = convert (TREE_TYPE (res), t);
6299 /* Strip any conversions, additions, and subtractions, and see if
6300 we are returning the address of a local variable. Warn if so. */
6301 while (1)
6303 switch (TREE_CODE (inner))
6305 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6306 case PLUS_EXPR:
6307 inner = TREE_OPERAND (inner, 0);
6308 continue;
6310 case MINUS_EXPR:
6311 /* If the second operand of the MINUS_EXPR has a pointer
6312 type (or is converted from it), this may be valid, so
6313 don't give a warning. */
6315 tree op1 = TREE_OPERAND (inner, 1);
6317 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6318 && (TREE_CODE (op1) == NOP_EXPR
6319 || TREE_CODE (op1) == NON_LVALUE_EXPR
6320 || TREE_CODE (op1) == CONVERT_EXPR))
6321 op1 = TREE_OPERAND (op1, 0);
6323 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6324 break;
6326 inner = TREE_OPERAND (inner, 0);
6327 continue;
6330 case ADDR_EXPR:
6331 inner = TREE_OPERAND (inner, 0);
6333 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6334 inner = TREE_OPERAND (inner, 0);
6336 if (DECL_P (inner)
6337 && ! DECL_EXTERNAL (inner)
6338 && ! TREE_STATIC (inner)
6339 && DECL_CONTEXT (inner) == current_function_decl)
6340 warning ("function returns address of local variable");
6341 break;
6343 default:
6344 break;
6347 break;
6350 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6353 return add_stmt (build_return_stmt (retval));
6356 struct c_switch {
6357 /* The SWITCH_STMT being built. */
6358 tree switch_stmt;
6359 /* A splay-tree mapping the low element of a case range to the high
6360 element, or NULL_TREE if there is no high element. Used to
6361 determine whether or not a new case label duplicates an old case
6362 label. We need a tree, rather than simply a hash table, because
6363 of the GNU case range extension. */
6364 splay_tree cases;
6365 /* The next node on the stack. */
6366 struct c_switch *next;
6369 /* A stack of the currently active switch statements. The innermost
6370 switch statement is on the top of the stack. There is no need to
6371 mark the stack for garbage collection because it is only active
6372 during the processing of the body of a function, and we never
6373 collect at that point. */
6375 static struct c_switch *switch_stack;
6377 /* Start a C switch statement, testing expression EXP. Return the new
6378 SWITCH_STMT. */
6380 tree
6381 c_start_case (tree exp)
6383 enum tree_code code;
6384 tree type, orig_type = error_mark_node;
6385 struct c_switch *cs;
6387 if (exp != error_mark_node)
6389 code = TREE_CODE (TREE_TYPE (exp));
6390 orig_type = TREE_TYPE (exp);
6392 if (! INTEGRAL_TYPE_P (orig_type)
6393 && code != ERROR_MARK)
6395 error ("switch quantity not an integer");
6396 exp = integer_zero_node;
6398 else
6400 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6402 if (warn_traditional && !in_system_header
6403 && (type == long_integer_type_node
6404 || type == long_unsigned_type_node))
6405 warning ("`long' switch expression not converted to `int' in ISO C");
6407 exp = default_conversion (exp);
6408 type = TREE_TYPE (exp);
6412 /* Add this new SWITCH_STMT to the stack. */
6413 cs = xmalloc (sizeof (*cs));
6414 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6415 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6416 cs->next = switch_stack;
6417 switch_stack = cs;
6419 return add_stmt (switch_stack->switch_stmt);
6422 /* Process a case label. */
6424 tree
6425 do_case (tree low_value, tree high_value)
6427 tree label = NULL_TREE;
6429 if (switch_stack)
6431 label = c_add_case_label (switch_stack->cases,
6432 SWITCH_COND (switch_stack->switch_stmt),
6433 low_value, high_value);
6434 if (label == error_mark_node)
6435 label = NULL_TREE;
6437 else if (low_value)
6438 error ("case label not within a switch statement");
6439 else
6440 error ("`default' label not within a switch statement");
6442 return label;
6445 /* Finish the switch statement. */
6447 void
6448 c_finish_case (tree body)
6450 struct c_switch *cs = switch_stack;
6452 SWITCH_BODY (cs->switch_stmt) = body;
6454 /* Emit warnings as needed. */
6455 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6457 /* Pop the stack. */
6458 switch_stack = switch_stack->next;
6459 splay_tree_delete (cs->cases);
6460 free (cs);
6463 /* Keep a stack of if statements. We record the number of compound
6464 statements seen up to the if keyword, as well as the line number
6465 and file of the if. If a potentially ambiguous else is seen, that
6466 fact is recorded; the warning is issued when we can be sure that
6467 the enclosing if statement does not have an else branch. */
6468 typedef struct
6470 tree if_stmt;
6471 location_t empty_locus;
6472 int compstmt_count;
6473 int stmt_count;
6474 unsigned int needs_warning : 1;
6475 unsigned int saw_else : 1;
6476 } if_elt;
6478 static if_elt *if_stack;
6480 /* Amount of space in the if statement stack. */
6481 static int if_stack_space = 0;
6483 /* Stack pointer. */
6484 static int if_stack_pointer = 0;
6486 /* Begin an if-statement. */
6488 void
6489 c_begin_if_stmt (void)
6491 tree r;
6492 if_elt *elt;
6494 /* Make sure there is enough space on the stack. */
6495 if (if_stack_space == 0)
6497 if_stack_space = 10;
6498 if_stack = xmalloc (10 * sizeof (if_elt));
6500 else if (if_stack_space == if_stack_pointer)
6502 if_stack_space += 10;
6503 if_stack = xrealloc (if_stack, if_stack_space * sizeof (if_elt));
6506 r = add_stmt (build_stmt (COND_EXPR, NULL_TREE, NULL_TREE, NULL_TREE));
6508 /* Record this if statement. */
6509 elt = &if_stack[if_stack_pointer++];
6510 memset (elt, 0, sizeof (*elt));
6511 elt->if_stmt = r;
6514 /* Record the start of an if-then, and record the start of it
6515 for ambiguous else detection.
6517 COND is the condition for the if-then statement.
6519 IF_STMT is the statement node that has already been created for
6520 this if-then statement. It is created before parsing the
6521 condition to keep line number information accurate. */
6523 void
6524 c_finish_if_cond (tree cond, int compstmt_count, int stmt_count)
6526 if_elt *elt = &if_stack[if_stack_pointer - 1];
6527 elt->compstmt_count = compstmt_count;
6528 elt->stmt_count = stmt_count;
6529 COND_EXPR_COND (elt->if_stmt) = lang_hooks.truthvalue_conversion (cond);
6532 /* Called after the then-clause for an if-statement is processed. */
6534 void
6535 c_finish_then (tree then_stmt)
6537 if_elt *elt = &if_stack[if_stack_pointer - 1];
6538 COND_EXPR_THEN (elt->if_stmt) = then_stmt;
6539 elt->empty_locus = input_location;
6542 /* Called between the then-clause and the else-clause
6543 of an if-then-else. */
6545 void
6546 c_begin_else (int stmt_count)
6548 if_elt *elt = &if_stack[if_stack_pointer - 1];
6550 /* An ambiguous else warning must be generated for the enclosing if
6551 statement, unless we see an else branch for that one, too. */
6552 if (warn_parentheses
6553 && if_stack_pointer > 1
6554 && (elt[0].compstmt_count == elt[-1].compstmt_count))
6555 elt[-1].needs_warning = 1;
6557 /* Even if a nested if statement had an else branch, it can't be
6558 ambiguous if this one also has an else. So don't warn in that
6559 case. Also don't warn for any if statements nested in this else. */
6560 elt->needs_warning = 0;
6561 elt->compstmt_count--;
6562 elt->saw_else = 1;
6563 elt->stmt_count = stmt_count;
6566 /* Called after the else-clause for an if-statement is processed. */
6568 void
6569 c_finish_else (tree else_stmt)
6571 if_elt *elt = &if_stack[if_stack_pointer - 1];
6572 COND_EXPR_ELSE (elt->if_stmt) = else_stmt;
6573 elt->empty_locus = input_location;
6576 /* Record the end of an if-then. Optionally warn if a nested
6577 if statement had an ambiguous else clause. */
6579 void
6580 c_finish_if_stmt (int stmt_count)
6582 if_elt *elt = &if_stack[--if_stack_pointer];
6584 if (COND_EXPR_ELSE (elt->if_stmt) == NULL)
6585 COND_EXPR_ELSE (elt->if_stmt) = build_empty_stmt ();
6587 if (elt->needs_warning)
6588 warning ("%Hsuggest explicit braces to avoid ambiguous `else'",
6589 EXPR_LOCUS (elt->if_stmt));
6591 if (extra_warnings && stmt_count == elt->stmt_count)
6593 if (elt->saw_else)
6594 warning ("%Hempty body in an else-statement", &elt->empty_locus);
6595 else
6596 warning ("%Hempty body in an if-statement", &elt->empty_locus);
6600 /* Begin a while statement. Returns a newly created WHILE_STMT if
6601 appropriate. */
6603 tree
6604 c_begin_while_stmt (void)
6606 tree r;
6607 r = add_stmt (build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE));
6608 return r;
6611 void
6612 c_finish_while_stmt_cond (tree cond, tree while_stmt)
6614 WHILE_COND (while_stmt) = (*lang_hooks.truthvalue_conversion) (cond);
6617 void
6618 c_finish_while_stmt (tree body, tree while_stmt)
6620 WHILE_BODY (while_stmt) = body;
6623 /* Create a for statement. */
6625 tree
6626 c_begin_for_stmt (void)
6628 tree r;
6629 r = add_stmt (build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
6630 NULL_TREE, NULL_TREE));
6631 FOR_INIT_STMT (r) = push_stmt_list ();
6632 return r;
6635 void
6636 c_finish_for_stmt_init (tree for_stmt)
6638 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
6641 void
6642 c_finish_for_stmt_cond (tree cond, tree for_stmt)
6644 if (cond)
6645 FOR_COND (for_stmt) = lang_hooks.truthvalue_conversion (cond);
6648 void
6649 c_finish_for_stmt_incr (tree expr, tree for_stmt)
6651 FOR_EXPR (for_stmt) = expr;
6654 void
6655 c_finish_for_stmt (tree body, tree for_stmt)
6657 FOR_BODY (for_stmt) = body;
6660 /* Create a statement expression. */
6662 tree
6663 c_begin_stmt_expr (void)
6665 tree ret;
6667 /* We must force a BLOCK for this level so that, if it is not expanded
6668 later, there is a way to turn off the entire subtree of blocks that
6669 are contained in it. */
6670 keep_next_level ();
6671 ret = c_begin_compound_stmt (true);
6673 /* Mark the current statement list as belonging to a statement list. */
6674 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6676 return ret;
6679 tree
6680 c_finish_stmt_expr (tree body)
6682 tree ret, last, type;
6683 tree *last_p;
6685 body = c_end_compound_stmt (body, true);
6687 /* Locate the last statement in BODY. */
6688 last = body, last_p = &body;
6689 if (TREE_CODE (last) == BIND_EXPR)
6691 last_p = &BIND_EXPR_BODY (last);
6692 last = BIND_EXPR_BODY (last);
6694 if (TREE_CODE (last) == STATEMENT_LIST)
6696 tree_stmt_iterator i = tsi_last (last);
6697 if (tsi_end_p (i))
6699 type = void_type_node;
6700 /* ??? Warn */
6701 goto no_expr;
6703 else
6705 last_p = tsi_stmt_ptr (i);
6706 last = *last_p;
6710 /* If the last statement is an EXPR_STMT, then unwrap it. Otherwise
6711 voidify_wrapper_expr will stuff it inside a MODIFY_EXPR and we'll
6712 fail gimplification. */
6713 /* ??? Should we go ahead and perform voidify_wrapper_expr here?
6714 We've got about all the information we need here. All we'd have
6715 to do even for proper type safety is to create, in effect,
6716 ( ({ ...; tmp = last; }), tmp )
6717 I.e. a COMPOUND_EXPR with the rhs being the compiler temporary.
6718 Not going to try this now, since it's not clear what should
6719 happen (wrt bindings) with new temporaries at this stage. It's
6720 easier once we begin gimplification. */
6721 if (TREE_CODE (last) == EXPR_STMT)
6722 *last_p = last = EXPR_STMT_EXPR (last);
6724 /* Extract the type of said expression. */
6725 type = TREE_TYPE (last);
6726 if (!type)
6727 type = void_type_node;
6729 no_expr:
6730 /* If what's left is compound, make sure we've got a BIND_EXPR, and
6731 that it has the proper type. */
6732 ret = body;
6733 if (TREE_CODE (ret) == STATEMENT_LIST)
6734 ret = build (BIND_EXPR, type, NULL, ret, NULL);
6735 else if (TREE_CODE (ret) == BIND_EXPR)
6736 TREE_TYPE (ret) = type;
6738 return ret;
6741 /* Begin and end compound statements. This is as simple as pushing
6742 and popping new statement lists from the tree. */
6744 tree
6745 c_begin_compound_stmt (bool do_scope)
6747 tree stmt = push_stmt_list ();
6748 if (do_scope)
6750 push_scope ();
6751 clear_last_expr ();
6753 return stmt;
6756 tree
6757 c_end_compound_stmt (tree stmt, bool do_scope)
6759 tree block = NULL;
6761 if (do_scope)
6763 if (c_dialect_objc ())
6764 objc_clear_super_receiver ();
6765 block = pop_scope ();
6768 stmt = pop_stmt_list (stmt);
6769 stmt = c_build_bind_expr (block, stmt);
6771 /* If this compound statement is nested immediately inside a statement
6772 expression, then force a BIND_EXPR to be created. Otherwise we'll
6773 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
6774 STATEMENT_LISTs merge, and thus we can lose track of what statement
6775 was really last. */
6776 if (cur_stmt_list
6777 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6778 && TREE_CODE (stmt) != BIND_EXPR)
6780 stmt = build (BIND_EXPR, void_type_node, NULL, stmt, NULL);
6781 TREE_SIDE_EFFECTS (stmt) = 1;
6784 return stmt;
6787 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
6788 when the current scope is exited. EH_ONLY is true when this is not
6789 meant to apply to normal control flow transfer. */
6791 void
6792 push_cleanup (tree decl ATTRIBUTE_UNUSED, tree cleanup, bool eh_only)
6794 enum tree_code code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
6795 tree stmt = build_stmt (code, NULL, cleanup);
6796 add_stmt (stmt);
6797 TREE_OPERAND (stmt, 0) = push_stmt_list ();
6800 /* Build a binary-operation expression without default conversions.
6801 CODE is the kind of expression to build.
6802 This function differs from `build' in several ways:
6803 the data type of the result is computed and recorded in it,
6804 warnings are generated if arg data types are invalid,
6805 special handling for addition and subtraction of pointers is known,
6806 and some optimization is done (operations on narrow ints
6807 are done in the narrower type when that gives the same result).
6808 Constant folding is also done before the result is returned.
6810 Note that the operands will never have enumeral types, or function
6811 or array types, because either they will have the default conversions
6812 performed or they have both just been converted to some other type in which
6813 the arithmetic is to be done. */
6815 tree
6816 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6817 int convert_p)
6819 tree type0, type1;
6820 enum tree_code code0, code1;
6821 tree op0, op1;
6823 /* Expression code to give to the expression when it is built.
6824 Normally this is CODE, which is what the caller asked for,
6825 but in some special cases we change it. */
6826 enum tree_code resultcode = code;
6828 /* Data type in which the computation is to be performed.
6829 In the simplest cases this is the common type of the arguments. */
6830 tree result_type = NULL;
6832 /* Nonzero means operands have already been type-converted
6833 in whatever way is necessary.
6834 Zero means they need to be converted to RESULT_TYPE. */
6835 int converted = 0;
6837 /* Nonzero means create the expression with this type, rather than
6838 RESULT_TYPE. */
6839 tree build_type = 0;
6841 /* Nonzero means after finally constructing the expression
6842 convert it to this type. */
6843 tree final_type = 0;
6845 /* Nonzero if this is an operation like MIN or MAX which can
6846 safely be computed in short if both args are promoted shorts.
6847 Also implies COMMON.
6848 -1 indicates a bitwise operation; this makes a difference
6849 in the exact conditions for when it is safe to do the operation
6850 in a narrower mode. */
6851 int shorten = 0;
6853 /* Nonzero if this is a comparison operation;
6854 if both args are promoted shorts, compare the original shorts.
6855 Also implies COMMON. */
6856 int short_compare = 0;
6858 /* Nonzero if this is a right-shift operation, which can be computed on the
6859 original short and then promoted if the operand is a promoted short. */
6860 int short_shift = 0;
6862 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6863 int common = 0;
6865 if (convert_p)
6867 op0 = default_conversion (orig_op0);
6868 op1 = default_conversion (orig_op1);
6870 else
6872 op0 = orig_op0;
6873 op1 = orig_op1;
6876 type0 = TREE_TYPE (op0);
6877 type1 = TREE_TYPE (op1);
6879 /* The expression codes of the data types of the arguments tell us
6880 whether the arguments are integers, floating, pointers, etc. */
6881 code0 = TREE_CODE (type0);
6882 code1 = TREE_CODE (type1);
6884 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6885 STRIP_TYPE_NOPS (op0);
6886 STRIP_TYPE_NOPS (op1);
6888 /* If an error was already reported for one of the arguments,
6889 avoid reporting another error. */
6891 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6892 return error_mark_node;
6894 switch (code)
6896 case PLUS_EXPR:
6897 /* Handle the pointer + int case. */
6898 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6899 return pointer_int_sum (PLUS_EXPR, op0, op1);
6900 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6901 return pointer_int_sum (PLUS_EXPR, op1, op0);
6902 else
6903 common = 1;
6904 break;
6906 case MINUS_EXPR:
6907 /* Subtraction of two similar pointers.
6908 We must subtract them as integers, then divide by object size. */
6909 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6910 && comp_target_types (type0, type1, 1))
6911 return pointer_diff (op0, op1);
6912 /* Handle pointer minus int. Just like pointer plus int. */
6913 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6914 return pointer_int_sum (MINUS_EXPR, op0, op1);
6915 else
6916 common = 1;
6917 break;
6919 case MULT_EXPR:
6920 common = 1;
6921 break;
6923 case TRUNC_DIV_EXPR:
6924 case CEIL_DIV_EXPR:
6925 case FLOOR_DIV_EXPR:
6926 case ROUND_DIV_EXPR:
6927 case EXACT_DIV_EXPR:
6928 /* Floating point division by zero is a legitimate way to obtain
6929 infinities and NaNs. */
6930 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6931 warning ("division by zero");
6933 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6934 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6935 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6936 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6938 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6939 resultcode = RDIV_EXPR;
6940 else
6941 /* Although it would be tempting to shorten always here, that
6942 loses on some targets, since the modulo instruction is
6943 undefined if the quotient can't be represented in the
6944 computation mode. We shorten only if unsigned or if
6945 dividing by something we know != -1. */
6946 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
6947 || (TREE_CODE (op1) == INTEGER_CST
6948 && ! integer_all_onesp (op1)));
6949 common = 1;
6951 break;
6953 case BIT_AND_EXPR:
6954 case BIT_IOR_EXPR:
6955 case BIT_XOR_EXPR:
6956 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6957 shorten = -1;
6958 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6959 common = 1;
6960 break;
6962 case TRUNC_MOD_EXPR:
6963 case FLOOR_MOD_EXPR:
6964 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6965 warning ("division by zero");
6967 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6969 /* Although it would be tempting to shorten always here, that loses
6970 on some targets, since the modulo instruction is undefined if the
6971 quotient can't be represented in the computation mode. We shorten
6972 only if unsigned or if dividing by something we know != -1. */
6973 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
6974 || (TREE_CODE (op1) == INTEGER_CST
6975 && ! integer_all_onesp (op1)));
6976 common = 1;
6978 break;
6980 case TRUTH_ANDIF_EXPR:
6981 case TRUTH_ORIF_EXPR:
6982 case TRUTH_AND_EXPR:
6983 case TRUTH_OR_EXPR:
6984 case TRUTH_XOR_EXPR:
6985 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6986 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6987 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6988 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6990 /* Result of these operations is always an int,
6991 but that does not mean the operands should be
6992 converted to ints! */
6993 result_type = integer_type_node;
6994 op0 = lang_hooks.truthvalue_conversion (op0);
6995 op1 = lang_hooks.truthvalue_conversion (op1);
6996 converted = 1;
6998 break;
7000 /* Shift operations: result has same type as first operand;
7001 always convert second operand to int.
7002 Also set SHORT_SHIFT if shifting rightward. */
7004 case RSHIFT_EXPR:
7005 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7007 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7009 if (tree_int_cst_sgn (op1) < 0)
7010 warning ("right shift count is negative");
7011 else
7013 if (! integer_zerop (op1))
7014 short_shift = 1;
7016 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7017 warning ("right shift count >= width of type");
7021 /* Use the type of the value to be shifted. */
7022 result_type = type0;
7023 /* Convert the shift-count to an integer, regardless of size
7024 of value being shifted. */
7025 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7026 op1 = convert (integer_type_node, op1);
7027 /* Avoid converting op1 to result_type later. */
7028 converted = 1;
7030 break;
7032 case LSHIFT_EXPR:
7033 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7035 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7037 if (tree_int_cst_sgn (op1) < 0)
7038 warning ("left shift count is negative");
7040 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7041 warning ("left shift count >= width of type");
7044 /* Use the type of the value to be shifted. */
7045 result_type = type0;
7046 /* Convert the shift-count to an integer, regardless of size
7047 of value being shifted. */
7048 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7049 op1 = convert (integer_type_node, op1);
7050 /* Avoid converting op1 to result_type later. */
7051 converted = 1;
7053 break;
7055 case RROTATE_EXPR:
7056 case LROTATE_EXPR:
7057 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7059 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7061 if (tree_int_cst_sgn (op1) < 0)
7062 warning ("shift count is negative");
7063 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7064 warning ("shift count >= width of type");
7067 /* Use the type of the value to be shifted. */
7068 result_type = type0;
7069 /* Convert the shift-count to an integer, regardless of size
7070 of value being shifted. */
7071 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7072 op1 = convert (integer_type_node, op1);
7073 /* Avoid converting op1 to result_type later. */
7074 converted = 1;
7076 break;
7078 case EQ_EXPR:
7079 case NE_EXPR:
7080 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7081 warning ("comparing floating point with == or != is unsafe");
7082 /* Result of comparison is always int,
7083 but don't convert the args to int! */
7084 build_type = integer_type_node;
7085 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7086 || code0 == COMPLEX_TYPE)
7087 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7088 || code1 == COMPLEX_TYPE))
7089 short_compare = 1;
7090 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7092 tree tt0 = TREE_TYPE (type0);
7093 tree tt1 = TREE_TYPE (type1);
7094 /* Anything compares with void *. void * compares with anything.
7095 Otherwise, the targets must be compatible
7096 and both must be object or both incomplete. */
7097 if (comp_target_types (type0, type1, 1))
7098 result_type = common_pointer_type (type0, type1);
7099 else if (VOID_TYPE_P (tt0))
7101 /* op0 != orig_op0 detects the case of something
7102 whose value is 0 but which isn't a valid null ptr const. */
7103 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7104 && TREE_CODE (tt1) == FUNCTION_TYPE)
7105 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
7107 else if (VOID_TYPE_P (tt1))
7109 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7110 && TREE_CODE (tt0) == FUNCTION_TYPE)
7111 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
7113 else
7114 pedwarn ("comparison of distinct pointer types lacks a cast");
7116 if (result_type == NULL_TREE)
7117 result_type = ptr_type_node;
7119 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7120 && integer_zerop (op1))
7121 result_type = type0;
7122 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7123 && integer_zerop (op0))
7124 result_type = type1;
7125 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7127 result_type = type0;
7128 pedwarn ("comparison between pointer and integer");
7130 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7132 result_type = type1;
7133 pedwarn ("comparison between pointer and integer");
7135 break;
7137 case MAX_EXPR:
7138 case MIN_EXPR:
7139 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7140 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7141 shorten = 1;
7142 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7144 if (comp_target_types (type0, type1, 1))
7146 result_type = common_pointer_type (type0, type1);
7147 if (pedantic
7148 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7149 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7151 else
7153 result_type = ptr_type_node;
7154 pedwarn ("comparison of distinct pointer types lacks a cast");
7157 break;
7159 case LE_EXPR:
7160 case GE_EXPR:
7161 case LT_EXPR:
7162 case GT_EXPR:
7163 build_type = integer_type_node;
7164 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7165 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7166 short_compare = 1;
7167 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7169 if (comp_target_types (type0, type1, 1))
7171 result_type = common_pointer_type (type0, type1);
7172 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7173 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7174 pedwarn ("comparison of complete and incomplete pointers");
7175 else if (pedantic
7176 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7177 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7179 else
7181 result_type = ptr_type_node;
7182 pedwarn ("comparison of distinct pointer types lacks a cast");
7185 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7186 && integer_zerop (op1))
7188 result_type = type0;
7189 if (pedantic || extra_warnings)
7190 pedwarn ("ordered comparison of pointer with integer zero");
7192 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7193 && integer_zerop (op0))
7195 result_type = type1;
7196 if (pedantic)
7197 pedwarn ("ordered comparison of pointer with integer zero");
7199 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7201 result_type = type0;
7202 pedwarn ("comparison between pointer and integer");
7204 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7206 result_type = type1;
7207 pedwarn ("comparison between pointer and integer");
7209 break;
7211 case UNORDERED_EXPR:
7212 case ORDERED_EXPR:
7213 case UNLT_EXPR:
7214 case UNLE_EXPR:
7215 case UNGT_EXPR:
7216 case UNGE_EXPR:
7217 case UNEQ_EXPR:
7218 case LTGT_EXPR:
7219 build_type = integer_type_node;
7220 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
7222 error ("unordered comparison on non-floating point argument");
7223 return error_mark_node;
7225 common = 1;
7226 break;
7228 default:
7229 break;
7232 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7233 return error_mark_node;
7235 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7236 || code0 == VECTOR_TYPE)
7238 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7239 || code1 == VECTOR_TYPE))
7241 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7243 if (shorten || common || short_compare)
7244 result_type = common_type (type0, type1);
7246 /* For certain operations (which identify themselves by shorten != 0)
7247 if both args were extended from the same smaller type,
7248 do the arithmetic in that type and then extend.
7250 shorten !=0 and !=1 indicates a bitwise operation.
7251 For them, this optimization is safe only if
7252 both args are zero-extended or both are sign-extended.
7253 Otherwise, we might change the result.
7254 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7255 but calculated in (unsigned short) it would be (unsigned short)-1. */
7257 if (shorten && none_complex)
7259 int unsigned0, unsigned1;
7260 tree arg0 = get_narrower (op0, &unsigned0);
7261 tree arg1 = get_narrower (op1, &unsigned1);
7262 /* UNS is 1 if the operation to be done is an unsigned one. */
7263 int uns = TYPE_UNSIGNED (result_type);
7264 tree type;
7266 final_type = result_type;
7268 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7269 but it *requires* conversion to FINAL_TYPE. */
7271 if ((TYPE_PRECISION (TREE_TYPE (op0))
7272 == TYPE_PRECISION (TREE_TYPE (arg0)))
7273 && TREE_TYPE (op0) != final_type)
7274 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7275 if ((TYPE_PRECISION (TREE_TYPE (op1))
7276 == TYPE_PRECISION (TREE_TYPE (arg1)))
7277 && TREE_TYPE (op1) != final_type)
7278 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7280 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7282 /* For bitwise operations, signedness of nominal type
7283 does not matter. Consider only how operands were extended. */
7284 if (shorten == -1)
7285 uns = unsigned0;
7287 /* Note that in all three cases below we refrain from optimizing
7288 an unsigned operation on sign-extended args.
7289 That would not be valid. */
7291 /* Both args variable: if both extended in same way
7292 from same width, do it in that width.
7293 Do it unsigned if args were zero-extended. */
7294 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7295 < TYPE_PRECISION (result_type))
7296 && (TYPE_PRECISION (TREE_TYPE (arg1))
7297 == TYPE_PRECISION (TREE_TYPE (arg0)))
7298 && unsigned0 == unsigned1
7299 && (unsigned0 || !uns))
7300 result_type
7301 = c_common_signed_or_unsigned_type
7302 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7303 else if (TREE_CODE (arg0) == INTEGER_CST
7304 && (unsigned1 || !uns)
7305 && (TYPE_PRECISION (TREE_TYPE (arg1))
7306 < TYPE_PRECISION (result_type))
7307 && (type
7308 = c_common_signed_or_unsigned_type (unsigned1,
7309 TREE_TYPE (arg1)),
7310 int_fits_type_p (arg0, type)))
7311 result_type = type;
7312 else if (TREE_CODE (arg1) == INTEGER_CST
7313 && (unsigned0 || !uns)
7314 && (TYPE_PRECISION (TREE_TYPE (arg0))
7315 < TYPE_PRECISION (result_type))
7316 && (type
7317 = c_common_signed_or_unsigned_type (unsigned0,
7318 TREE_TYPE (arg0)),
7319 int_fits_type_p (arg1, type)))
7320 result_type = type;
7323 /* Shifts can be shortened if shifting right. */
7325 if (short_shift)
7327 int unsigned_arg;
7328 tree arg0 = get_narrower (op0, &unsigned_arg);
7330 final_type = result_type;
7332 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7333 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7335 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7336 /* We can shorten only if the shift count is less than the
7337 number of bits in the smaller type size. */
7338 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7339 /* We cannot drop an unsigned shift after sign-extension. */
7340 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7342 /* Do an unsigned shift if the operand was zero-extended. */
7343 result_type
7344 = c_common_signed_or_unsigned_type (unsigned_arg,
7345 TREE_TYPE (arg0));
7346 /* Convert value-to-be-shifted to that type. */
7347 if (TREE_TYPE (op0) != result_type)
7348 op0 = convert (result_type, op0);
7349 converted = 1;
7353 /* Comparison operations are shortened too but differently.
7354 They identify themselves by setting short_compare = 1. */
7356 if (short_compare)
7358 /* Don't write &op0, etc., because that would prevent op0
7359 from being kept in a register.
7360 Instead, make copies of the our local variables and
7361 pass the copies by reference, then copy them back afterward. */
7362 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7363 enum tree_code xresultcode = resultcode;
7364 tree val
7365 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7367 if (val != 0)
7368 return val;
7370 op0 = xop0, op1 = xop1;
7371 converted = 1;
7372 resultcode = xresultcode;
7374 if (warn_sign_compare && skip_evaluation == 0)
7376 int op0_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7377 int op1_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7378 int unsignedp0, unsignedp1;
7379 tree primop0 = get_narrower (op0, &unsignedp0);
7380 tree primop1 = get_narrower (op1, &unsignedp1);
7382 xop0 = orig_op0;
7383 xop1 = orig_op1;
7384 STRIP_TYPE_NOPS (xop0);
7385 STRIP_TYPE_NOPS (xop1);
7387 /* Give warnings for comparisons between signed and unsigned
7388 quantities that may fail.
7390 Do the checking based on the original operand trees, so that
7391 casts will be considered, but default promotions won't be.
7393 Do not warn if the comparison is being done in a signed type,
7394 since the signed type will only be chosen if it can represent
7395 all the values of the unsigned type. */
7396 if (! TYPE_UNSIGNED (result_type))
7397 /* OK */;
7398 /* Do not warn if both operands are the same signedness. */
7399 else if (op0_signed == op1_signed)
7400 /* OK */;
7401 else
7403 tree sop, uop;
7405 if (op0_signed)
7406 sop = xop0, uop = xop1;
7407 else
7408 sop = xop1, uop = xop0;
7410 /* Do not warn if the signed quantity is an
7411 unsuffixed integer literal (or some static
7412 constant expression involving such literals or a
7413 conditional expression involving such literals)
7414 and it is non-negative. */
7415 if (c_tree_expr_nonnegative_p (sop))
7416 /* OK */;
7417 /* Do not warn if the comparison is an equality operation,
7418 the unsigned quantity is an integral constant, and it
7419 would fit in the result if the result were signed. */
7420 else if (TREE_CODE (uop) == INTEGER_CST
7421 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7422 && int_fits_type_p
7423 (uop, c_common_signed_type (result_type)))
7424 /* OK */;
7425 /* Do not warn if the unsigned quantity is an enumeration
7426 constant and its maximum value would fit in the result
7427 if the result were signed. */
7428 else if (TREE_CODE (uop) == INTEGER_CST
7429 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7430 && int_fits_type_p
7431 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7432 c_common_signed_type (result_type)))
7433 /* OK */;
7434 else
7435 warning ("comparison between signed and unsigned");
7438 /* Warn if two unsigned values are being compared in a size
7439 larger than their original size, and one (and only one) is the
7440 result of a `~' operator. This comparison will always fail.
7442 Also warn if one operand is a constant, and the constant
7443 does not have all bits set that are set in the ~ operand
7444 when it is extended. */
7446 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7447 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7449 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7450 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7451 &unsignedp0);
7452 else
7453 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7454 &unsignedp1);
7456 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7458 tree primop;
7459 HOST_WIDE_INT constant, mask;
7460 int unsignedp, bits;
7462 if (host_integerp (primop0, 0))
7464 primop = primop1;
7465 unsignedp = unsignedp1;
7466 constant = tree_low_cst (primop0, 0);
7468 else
7470 primop = primop0;
7471 unsignedp = unsignedp0;
7472 constant = tree_low_cst (primop1, 0);
7475 bits = TYPE_PRECISION (TREE_TYPE (primop));
7476 if (bits < TYPE_PRECISION (result_type)
7477 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7479 mask = (~ (HOST_WIDE_INT) 0) << bits;
7480 if ((mask & constant) != mask)
7481 warning ("comparison of promoted ~unsigned with constant");
7484 else if (unsignedp0 && unsignedp1
7485 && (TYPE_PRECISION (TREE_TYPE (primop0))
7486 < TYPE_PRECISION (result_type))
7487 && (TYPE_PRECISION (TREE_TYPE (primop1))
7488 < TYPE_PRECISION (result_type)))
7489 warning ("comparison of promoted ~unsigned with unsigned");
7495 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7496 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7497 Then the expression will be built.
7498 It will be given type FINAL_TYPE if that is nonzero;
7499 otherwise, it will be given type RESULT_TYPE. */
7501 if (!result_type)
7503 binary_op_error (code);
7504 return error_mark_node;
7507 if (! converted)
7509 if (TREE_TYPE (op0) != result_type)
7510 op0 = convert (result_type, op0);
7511 if (TREE_TYPE (op1) != result_type)
7512 op1 = convert (result_type, op1);
7515 if (build_type == NULL_TREE)
7516 build_type = result_type;
7519 tree result = build (resultcode, build_type, op0, op1);
7521 /* Treat expressions in initializers specially as they can't trap. */
7522 result = require_constant_value ? fold_initializer (result)
7523 : fold (result);
7525 if (final_type != 0)
7526 result = convert (final_type, result);
7527 return result;
7531 /* Build the result of __builtin_offsetof. TYPE is the first argument to
7532 offsetof, i.e. a type. LIST is a tree_list that encodes component and
7533 array references; PURPOSE is set for the former and VALUE is set for
7534 the later. */
7536 tree
7537 build_offsetof (tree type, tree list)
7539 tree t;
7541 /* Build "*(type *)0". */
7542 t = convert (build_pointer_type (type), null_pointer_node);
7543 t = build_indirect_ref (t, "");
7545 /* Build COMPONENT and ARRAY_REF expressions as needed. */
7546 for (list = nreverse (list); list ; list = TREE_CHAIN (list))
7547 if (TREE_PURPOSE (list))
7548 t = build_component_ref (t, TREE_PURPOSE (list));
7549 else
7550 t = build_array_ref (t, TREE_VALUE (list));
7552 /* Finalize the offsetof expression. For now all we need to do is take
7553 the address of the expression we created, and cast that to an integer
7554 type; this mirrors the traditional macro implementation of offsetof. */
7555 t = build_unary_op (ADDR_EXPR, t, 0);
7556 return convert (size_type_node, t);