PR target/19019
[official-gcc.git] / gcc / c-typeck.c
blob3a202929e9fc33e2e37b266d34797f4dd43704de
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 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 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_argpass_nonproto,
53 ic_assign,
54 ic_init,
55 ic_return
58 /* The level of nesting inside "__alignof__". */
59 int in_alignof;
61 /* The level of nesting inside "sizeof". */
62 int in_sizeof;
64 /* The level of nesting inside "typeof". */
65 int in_typeof;
67 /* Nonzero if we've already printed a "missing braces around initializer"
68 message within this initializer. */
69 static int missing_braces_mentioned;
71 static int require_constant_value;
72 static int require_constant_elements;
74 static tree qualify_type (tree, tree);
75 static int tagged_types_tu_compatible_p (tree, tree);
76 static int comp_target_types (tree, tree, int);
77 static int function_types_compatible_p (tree, tree);
78 static int type_lists_compatible_p (tree, tree);
79 static tree decl_constant_value_for_broken_optimization (tree);
80 static tree default_function_array_conversion (tree);
81 static tree lookup_field (tree, tree);
82 static tree convert_arguments (tree, tree, tree, tree);
83 static tree pointer_diff (tree, tree);
84 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
85 int);
86 static tree valid_compound_expr_initializer (tree, tree);
87 static void push_string (const char *);
88 static void push_member_name (tree);
89 static void push_array_bounds (int);
90 static int spelling_length (void);
91 static char *print_spelling (char *);
92 static void warning_init (const char *);
93 static tree digest_init (tree, tree, bool, int);
94 static void output_init_element (tree, bool, tree, tree, int);
95 static void output_pending_init_elements (int);
96 static int set_designator (int);
97 static void push_range_stack (tree);
98 static void add_pending_init (tree, tree);
99 static void set_nonincremental_init (void);
100 static void set_nonincremental_init_from_string (tree);
101 static tree find_init_member (tree);
102 static void readonly_error (tree, enum lvalue_use);
103 static void record_maybe_used_decl (tree);
105 /* Do `exp = require_complete_type (exp);' to make sure exp
106 does not have an incomplete type. (That includes void types.) */
108 tree
109 require_complete_type (tree value)
111 tree type = TREE_TYPE (value);
113 if (value == error_mark_node || type == error_mark_node)
114 return error_mark_node;
116 /* First, detect a valid value with a complete type. */
117 if (COMPLETE_TYPE_P (type))
118 return value;
120 c_incomplete_type_error (value, type);
121 return error_mark_node;
124 /* Print an error message for invalid use of an incomplete type.
125 VALUE is the expression that was used (or 0 if that isn't known)
126 and TYPE is the type that was invalid. */
128 void
129 c_incomplete_type_error (tree value, tree type)
131 const char *type_code_string;
133 /* Avoid duplicate error message. */
134 if (TREE_CODE (type) == ERROR_MARK)
135 return;
137 if (value != 0 && (TREE_CODE (value) == VAR_DECL
138 || TREE_CODE (value) == PARM_DECL))
139 error ("%qs has an incomplete type",
140 IDENTIFIER_POINTER (DECL_NAME (value)));
141 else
143 retry:
144 /* We must print an error message. Be clever about what it says. */
146 switch (TREE_CODE (type))
148 case RECORD_TYPE:
149 type_code_string = "struct";
150 break;
152 case UNION_TYPE:
153 type_code_string = "union";
154 break;
156 case ENUMERAL_TYPE:
157 type_code_string = "enum";
158 break;
160 case VOID_TYPE:
161 error ("invalid use of void expression");
162 return;
164 case ARRAY_TYPE:
165 if (TYPE_DOMAIN (type))
167 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
169 error ("invalid use of flexible array member");
170 return;
172 type = TREE_TYPE (type);
173 goto retry;
175 error ("invalid use of array with unspecified bounds");
176 return;
178 default:
179 gcc_unreachable ();
182 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
183 error ("invalid use of undefined type %<%s %s%>",
184 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
185 else
186 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
187 error ("invalid use of incomplete typedef %qs",
188 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
192 /* Given a type, apply default promotions wrt unnamed function
193 arguments and return the new type. */
195 tree
196 c_type_promotes_to (tree type)
198 if (TYPE_MAIN_VARIANT (type) == float_type_node)
199 return double_type_node;
201 if (c_promoting_integer_type_p (type))
203 /* Preserve unsignedness if not really getting any wider. */
204 if (TYPE_UNSIGNED (type)
205 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
206 return unsigned_type_node;
207 return integer_type_node;
210 return type;
213 /* Return a variant of TYPE which has all the type qualifiers of LIKE
214 as well as those of TYPE. */
216 static tree
217 qualify_type (tree type, tree like)
219 return c_build_qualified_type (type,
220 TYPE_QUALS (type) | TYPE_QUALS (like));
223 /* Return the composite type of two compatible types.
225 We assume that comptypes has already been done and returned
226 nonzero; if that isn't so, this may crash. In particular, we
227 assume that qualifiers match. */
229 tree
230 composite_type (tree t1, tree t2)
232 enum tree_code code1;
233 enum tree_code code2;
234 tree attributes;
236 /* Save time if the two types are the same. */
238 if (t1 == t2) return t1;
240 /* If one type is nonsense, use the other. */
241 if (t1 == error_mark_node)
242 return t2;
243 if (t2 == error_mark_node)
244 return t1;
246 code1 = TREE_CODE (t1);
247 code2 = TREE_CODE (t2);
249 /* Merge the attributes. */
250 attributes = targetm.merge_type_attributes (t1, t2);
252 /* If one is an enumerated type and the other is the compatible
253 integer type, the composite type might be either of the two
254 (DR#013 question 3). For consistency, use the enumerated type as
255 the composite type. */
257 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
258 return t1;
259 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
260 return t2;
262 gcc_assert (code1 == code2);
264 switch (code1)
266 case POINTER_TYPE:
267 /* For two pointers, do this recursively on the target type. */
269 tree pointed_to_1 = TREE_TYPE (t1);
270 tree pointed_to_2 = TREE_TYPE (t2);
271 tree target = composite_type (pointed_to_1, pointed_to_2);
272 t1 = build_pointer_type (target);
273 t1 = build_type_attribute_variant (t1, attributes);
274 return qualify_type (t1, t2);
277 case ARRAY_TYPE:
279 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
280 int quals;
281 tree unqual_elt;
283 /* We should not have any type quals on arrays at all. */
284 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
286 /* Save space: see if the result is identical to one of the args. */
287 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
288 return build_type_attribute_variant (t1, attributes);
289 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
290 return build_type_attribute_variant (t2, attributes);
292 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
293 return build_type_attribute_variant (t1, attributes);
294 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
295 return build_type_attribute_variant (t2, attributes);
297 /* Merge the element types, and have a size if either arg has
298 one. We may have qualifiers on the element types. To set
299 up TYPE_MAIN_VARIANT correctly, we need to form the
300 composite of the unqualified types and add the qualifiers
301 back at the end. */
302 quals = TYPE_QUALS (strip_array_types (elt));
303 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
304 t1 = build_array_type (unqual_elt,
305 TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
306 t1 = c_build_qualified_type (t1, quals);
307 return build_type_attribute_variant (t1, attributes);
310 case FUNCTION_TYPE:
311 /* Function types: prefer the one that specified arg types.
312 If both do, merge the arg types. Also merge the return types. */
314 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
315 tree p1 = TYPE_ARG_TYPES (t1);
316 tree p2 = TYPE_ARG_TYPES (t2);
317 int len;
318 tree newargs, n;
319 int i;
321 /* Save space: see if the result is identical to one of the args. */
322 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
323 return build_type_attribute_variant (t1, attributes);
324 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
325 return build_type_attribute_variant (t2, attributes);
327 /* Simple way if one arg fails to specify argument types. */
328 if (TYPE_ARG_TYPES (t1) == 0)
330 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
331 t1 = build_type_attribute_variant (t1, attributes);
332 return qualify_type (t1, t2);
334 if (TYPE_ARG_TYPES (t2) == 0)
336 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
337 t1 = build_type_attribute_variant (t1, attributes);
338 return qualify_type (t1, t2);
341 /* If both args specify argument types, we must merge the two
342 lists, argument by argument. */
343 /* Tell global_bindings_p to return false so that variable_size
344 doesn't abort on VLAs in parameter types. */
345 c_override_global_bindings_to_false = true;
347 len = list_length (p1);
348 newargs = 0;
350 for (i = 0; i < len; i++)
351 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
353 n = newargs;
355 for (; p1;
356 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
358 /* A null type means arg type is not specified.
359 Take whatever the other function type has. */
360 if (TREE_VALUE (p1) == 0)
362 TREE_VALUE (n) = TREE_VALUE (p2);
363 goto parm_done;
365 if (TREE_VALUE (p2) == 0)
367 TREE_VALUE (n) = TREE_VALUE (p1);
368 goto parm_done;
371 /* Given wait (union {union wait *u; int *i} *)
372 and wait (union wait *),
373 prefer union wait * as type of parm. */
374 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
375 && TREE_VALUE (p1) != TREE_VALUE (p2))
377 tree memb;
378 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
379 memb; memb = TREE_CHAIN (memb))
380 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
382 TREE_VALUE (n) = TREE_VALUE (p2);
383 if (pedantic)
384 pedwarn ("function types not truly compatible in ISO C");
385 goto parm_done;
388 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
389 && TREE_VALUE (p2) != TREE_VALUE (p1))
391 tree memb;
392 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
393 memb; memb = TREE_CHAIN (memb))
394 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
396 TREE_VALUE (n) = TREE_VALUE (p1);
397 if (pedantic)
398 pedwarn ("function types not truly compatible in ISO C");
399 goto parm_done;
402 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
403 parm_done: ;
406 c_override_global_bindings_to_false = false;
407 t1 = build_function_type (valtype, newargs);
408 t1 = qualify_type (t1, t2);
409 /* ... falls through ... */
412 default:
413 return build_type_attribute_variant (t1, attributes);
418 /* Return the type of a conditional expression between pointers to
419 possibly differently qualified versions of compatible types.
421 We assume that comp_target_types has already been done and returned
422 nonzero; if that isn't so, this may crash. */
424 static tree
425 common_pointer_type (tree t1, tree t2)
427 tree attributes;
428 tree pointed_to_1, mv1;
429 tree pointed_to_2, mv2;
430 tree target;
432 /* Save time if the two types are the same. */
434 if (t1 == t2) return t1;
436 /* If one type is nonsense, use the other. */
437 if (t1 == error_mark_node)
438 return t2;
439 if (t2 == error_mark_node)
440 return t1;
442 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
443 && TREE_CODE (t2) == POINTER_TYPE);
445 /* Merge the attributes. */
446 attributes = targetm.merge_type_attributes (t1, t2);
448 /* Find the composite type of the target types, and combine the
449 qualifiers of the two types' targets. Do not lose qualifiers on
450 array element types by taking the TYPE_MAIN_VARIANT. */
451 mv1 = pointed_to_1 = TREE_TYPE (t1);
452 mv2 = pointed_to_2 = TREE_TYPE (t2);
453 if (TREE_CODE (mv1) != ARRAY_TYPE)
454 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
455 if (TREE_CODE (mv2) != ARRAY_TYPE)
456 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
457 target = composite_type (mv1, mv2);
458 t1 = build_pointer_type (c_build_qualified_type
459 (target,
460 TYPE_QUALS (pointed_to_1) |
461 TYPE_QUALS (pointed_to_2)));
462 return build_type_attribute_variant (t1, attributes);
465 /* Return the common type for two arithmetic types under the usual
466 arithmetic conversions. The default conversions have already been
467 applied, and enumerated types converted to their compatible integer
468 types. The resulting type is unqualified and has no attributes.
470 This is the type for the result of most arithmetic operations
471 if the operands have the given two types. */
473 static tree
474 c_common_type (tree t1, tree t2)
476 enum tree_code code1;
477 enum tree_code code2;
479 /* If one type is nonsense, use the other. */
480 if (t1 == error_mark_node)
481 return t2;
482 if (t2 == error_mark_node)
483 return t1;
485 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
486 t1 = TYPE_MAIN_VARIANT (t1);
488 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
489 t2 = TYPE_MAIN_VARIANT (t2);
491 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
492 t1 = build_type_attribute_variant (t1, NULL_TREE);
494 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
495 t2 = build_type_attribute_variant (t2, NULL_TREE);
497 /* Save time if the two types are the same. */
499 if (t1 == t2) return t1;
501 code1 = TREE_CODE (t1);
502 code2 = TREE_CODE (t2);
504 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
505 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
506 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
507 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
509 /* If one type is a vector type, return that type. (How the usual
510 arithmetic conversions apply to the vector types extension is not
511 precisely specified.) */
512 if (code1 == VECTOR_TYPE)
513 return t1;
515 if (code2 == VECTOR_TYPE)
516 return t2;
518 /* If one type is complex, form the common type of the non-complex
519 components, then make that complex. Use T1 or T2 if it is the
520 required type. */
521 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
523 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
524 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
525 tree subtype = c_common_type (subtype1, subtype2);
527 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
528 return t1;
529 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
530 return t2;
531 else
532 return build_complex_type (subtype);
535 /* If only one is real, use it as the result. */
537 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
538 return t1;
540 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
541 return t2;
543 /* Both real or both integers; use the one with greater precision. */
545 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
546 return t1;
547 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
548 return t2;
550 /* Same precision. Prefer long longs to longs to ints when the
551 same precision, following the C99 rules on integer type rank
552 (which are equivalent to the C90 rules for C90 types). */
554 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
555 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
556 return long_long_unsigned_type_node;
558 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
559 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
561 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
562 return long_long_unsigned_type_node;
563 else
564 return long_long_integer_type_node;
567 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
568 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
569 return long_unsigned_type_node;
571 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
572 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
574 /* But preserve unsignedness from the other type,
575 since long cannot hold all the values of an unsigned int. */
576 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
577 return long_unsigned_type_node;
578 else
579 return long_integer_type_node;
582 /* Likewise, prefer long double to double even if same size. */
583 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
584 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
585 return long_double_type_node;
587 /* Otherwise prefer the unsigned one. */
589 if (TYPE_UNSIGNED (t1))
590 return t1;
591 else
592 return t2;
595 /* Wrapper around c_common_type that is used by c-common.c. ENUMERAL_TYPEs
596 are allowed here and are converted to their compatible integer types. */
597 tree
598 common_type (tree t1, tree t2)
600 if (TREE_CODE (t1) == ENUMERAL_TYPE)
601 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
602 if (TREE_CODE (t2) == ENUMERAL_TYPE)
603 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
604 return c_common_type (t1, t2);
607 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
608 or various other operations. Return 2 if they are compatible
609 but a warning may be needed if you use them together. */
612 comptypes (tree type1, tree type2)
614 tree t1 = type1;
615 tree t2 = type2;
616 int attrval, val;
618 /* Suppress errors caused by previously reported errors. */
620 if (t1 == t2 || !t1 || !t2
621 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
622 return 1;
624 /* If either type is the internal version of sizetype, return the
625 language version. */
626 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
627 && TYPE_ORIG_SIZE_TYPE (t1))
628 t1 = TYPE_ORIG_SIZE_TYPE (t1);
630 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
631 && TYPE_ORIG_SIZE_TYPE (t2))
632 t2 = TYPE_ORIG_SIZE_TYPE (t2);
635 /* Enumerated types are compatible with integer types, but this is
636 not transitive: two enumerated types in the same translation unit
637 are compatible with each other only if they are the same type. */
639 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
640 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
641 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
642 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
644 if (t1 == t2)
645 return 1;
647 /* Different classes of types can't be compatible. */
649 if (TREE_CODE (t1) != TREE_CODE (t2))
650 return 0;
652 /* Qualifiers must match. C99 6.7.3p9 */
654 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
655 return 0;
657 /* Allow for two different type nodes which have essentially the same
658 definition. Note that we already checked for equality of the type
659 qualifiers (just above). */
661 if (TREE_CODE (t1) != ARRAY_TYPE
662 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
663 return 1;
665 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
666 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
667 return 0;
669 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
670 val = 0;
672 switch (TREE_CODE (t1))
674 case POINTER_TYPE:
675 /* We must give ObjC the first crack at comparing pointers, since
676 protocol qualifiers may be involved. */
677 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
678 break;
679 /* Do not remove mode or aliasing information. */
680 if (TYPE_MODE (t1) != TYPE_MODE (t2)
681 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
682 break;
683 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
684 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
685 break;
687 case FUNCTION_TYPE:
688 val = function_types_compatible_p (t1, t2);
689 break;
691 case ARRAY_TYPE:
693 tree d1 = TYPE_DOMAIN (t1);
694 tree d2 = TYPE_DOMAIN (t2);
695 bool d1_variable, d2_variable;
696 bool d1_zero, d2_zero;
697 val = 1;
699 /* Target types must match incl. qualifiers. */
700 if (TREE_TYPE (t1) != TREE_TYPE (t2)
701 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
702 return 0;
704 /* Sizes must match unless one is missing or variable. */
705 if (d1 == 0 || d2 == 0 || d1 == d2)
706 break;
708 d1_zero = !TYPE_MAX_VALUE (d1);
709 d2_zero = !TYPE_MAX_VALUE (d2);
711 d1_variable = (!d1_zero
712 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
713 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
714 d2_variable = (!d2_zero
715 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
716 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
718 if (d1_variable || d2_variable)
719 break;
720 if (d1_zero && d2_zero)
721 break;
722 if (d1_zero || d2_zero
723 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
724 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
725 val = 0;
727 break;
730 case RECORD_TYPE:
731 /* We are dealing with two distinct structs. In assorted Objective-C
732 corner cases, however, these can still be deemed equivalent. */
733 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
734 val = 1;
736 case ENUMERAL_TYPE:
737 case UNION_TYPE:
738 if (val != 1 && !same_translation_unit_p (t1, t2))
739 val = tagged_types_tu_compatible_p (t1, t2);
740 break;
742 case VECTOR_TYPE:
743 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
744 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
745 break;
747 default:
748 break;
750 return attrval == 2 && val == 1 ? 2 : val;
753 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
754 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
755 to 1 or 0 depending if the check of the pointer types is meant to
756 be reflexive or not (typically, assignments are not reflexive,
757 while comparisons are reflexive).
760 static int
761 comp_target_types (tree ttl, tree ttr, int reflexive)
763 int val;
764 tree mvl, mvr;
766 /* Give objc_comptypes a crack at letting these types through. */
767 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
768 return val;
770 /* Do not lose qualifiers on element types of array types that are
771 pointer targets by taking their TYPE_MAIN_VARIANT. */
772 mvl = TREE_TYPE (ttl);
773 mvr = TREE_TYPE (ttr);
774 if (TREE_CODE (mvl) != ARRAY_TYPE)
775 mvl = TYPE_MAIN_VARIANT (mvl);
776 if (TREE_CODE (mvr) != ARRAY_TYPE)
777 mvr = TYPE_MAIN_VARIANT (mvr);
778 val = comptypes (mvl, mvr);
780 if (val == 2 && pedantic)
781 pedwarn ("types are not quite compatible");
782 return val;
785 /* Subroutines of `comptypes'. */
787 /* Determine whether two trees derive from the same translation unit.
788 If the CONTEXT chain ends in a null, that tree's context is still
789 being parsed, so if two trees have context chains ending in null,
790 they're in the same translation unit. */
792 same_translation_unit_p (tree t1, tree t2)
794 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
795 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
797 case tcc_declaration:
798 t1 = DECL_CONTEXT (t1); break;
799 case tcc_type:
800 t1 = TYPE_CONTEXT (t1); break;
801 case tcc_exceptional:
802 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
803 default: gcc_unreachable ();
806 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
807 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
809 case tcc_declaration:
810 t2 = DECL_CONTEXT (t2); break;
811 case tcc_type:
812 t2 = TYPE_CONTEXT (t2); break;
813 case tcc_exceptional:
814 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
815 default: gcc_unreachable ();
818 return t1 == t2;
821 /* The C standard says that two structures in different translation
822 units are compatible with each other only if the types of their
823 fields are compatible (among other things). So, consider two copies
824 of this structure: */
826 struct tagged_tu_seen {
827 const struct tagged_tu_seen * next;
828 tree t1;
829 tree t2;
832 /* Can they be compatible with each other? We choose to break the
833 recursion by allowing those types to be compatible. */
835 static const struct tagged_tu_seen * tagged_tu_seen_base;
837 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
838 compatible. If the two types are not the same (which has been
839 checked earlier), this can only happen when multiple translation
840 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
841 rules. */
843 static int
844 tagged_types_tu_compatible_p (tree t1, tree t2)
846 tree s1, s2;
847 bool needs_warning = false;
849 /* We have to verify that the tags of the types are the same. This
850 is harder than it looks because this may be a typedef, so we have
851 to go look at the original type. It may even be a typedef of a
852 typedef...
853 In the case of compiler-created builtin structs the TYPE_DECL
854 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
855 while (TYPE_NAME (t1)
856 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
857 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
858 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
860 while (TYPE_NAME (t2)
861 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
862 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
863 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
865 /* C90 didn't have the requirement that the two tags be the same. */
866 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
867 return 0;
869 /* C90 didn't say what happened if one or both of the types were
870 incomplete; we choose to follow C99 rules here, which is that they
871 are compatible. */
872 if (TYPE_SIZE (t1) == NULL
873 || TYPE_SIZE (t2) == NULL)
874 return 1;
877 const struct tagged_tu_seen * tts_i;
878 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
879 if (tts_i->t1 == t1 && tts_i->t2 == t2)
880 return 1;
883 switch (TREE_CODE (t1))
885 case ENUMERAL_TYPE:
888 /* Speed up the case where the type values are in the same order. */
889 tree tv1 = TYPE_VALUES (t1);
890 tree tv2 = TYPE_VALUES (t2);
892 if (tv1 == tv2)
893 return 1;
895 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
897 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
898 break;
899 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
900 return 0;
903 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
904 return 1;
905 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
906 return 0;
908 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
909 return 0;
911 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
913 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
914 if (s2 == NULL
915 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
916 return 0;
918 return 1;
921 case UNION_TYPE:
923 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
924 return 0;
926 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
928 bool ok = false;
929 struct tagged_tu_seen tts;
931 tts.next = tagged_tu_seen_base;
932 tts.t1 = t1;
933 tts.t2 = t2;
934 tagged_tu_seen_base = &tts;
936 if (DECL_NAME (s1) != NULL)
937 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
938 if (DECL_NAME (s1) == DECL_NAME (s2))
940 int result;
941 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
942 if (result == 0)
943 break;
944 if (result == 2)
945 needs_warning = true;
947 if (TREE_CODE (s1) == FIELD_DECL
948 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
949 DECL_FIELD_BIT_OFFSET (s2)) != 1)
950 break;
952 ok = true;
953 break;
955 tagged_tu_seen_base = tts.next;
956 if (!ok)
957 return 0;
959 return needs_warning ? 2 : 1;
962 case RECORD_TYPE:
964 struct tagged_tu_seen tts;
966 tts.next = tagged_tu_seen_base;
967 tts.t1 = t1;
968 tts.t2 = t2;
969 tagged_tu_seen_base = &tts;
971 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
972 s1 && s2;
973 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
975 int result;
976 if (TREE_CODE (s1) != TREE_CODE (s2)
977 || DECL_NAME (s1) != DECL_NAME (s2))
978 break;
979 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
980 if (result == 0)
981 break;
982 if (result == 2)
983 needs_warning = true;
985 if (TREE_CODE (s1) == FIELD_DECL
986 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
987 DECL_FIELD_BIT_OFFSET (s2)) != 1)
988 break;
990 tagged_tu_seen_base = tts.next;
991 if (s1 && s2)
992 return 0;
993 return needs_warning ? 2 : 1;
996 default:
997 gcc_unreachable ();
1001 /* Return 1 if two function types F1 and F2 are compatible.
1002 If either type specifies no argument types,
1003 the other must specify a fixed number of self-promoting arg types.
1004 Otherwise, if one type specifies only the number of arguments,
1005 the other must specify that number of self-promoting arg types.
1006 Otherwise, the argument types must match. */
1008 static int
1009 function_types_compatible_p (tree f1, tree f2)
1011 tree args1, args2;
1012 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1013 int val = 1;
1014 int val1;
1015 tree ret1, ret2;
1017 ret1 = TREE_TYPE (f1);
1018 ret2 = TREE_TYPE (f2);
1020 /* 'volatile' qualifiers on a function's return type used to mean
1021 the function is noreturn. */
1022 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1023 pedwarn ("function return types not compatible due to %<volatile%>");
1024 if (TYPE_VOLATILE (ret1))
1025 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1026 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1027 if (TYPE_VOLATILE (ret2))
1028 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1029 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1030 val = comptypes (ret1, ret2);
1031 if (val == 0)
1032 return 0;
1034 args1 = TYPE_ARG_TYPES (f1);
1035 args2 = TYPE_ARG_TYPES (f2);
1037 /* An unspecified parmlist matches any specified parmlist
1038 whose argument types don't need default promotions. */
1040 if (args1 == 0)
1042 if (!self_promoting_args_p (args2))
1043 return 0;
1044 /* If one of these types comes from a non-prototype fn definition,
1045 compare that with the other type's arglist.
1046 If they don't match, ask for a warning (but no error). */
1047 if (TYPE_ACTUAL_ARG_TYPES (f1)
1048 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1049 val = 2;
1050 return val;
1052 if (args2 == 0)
1054 if (!self_promoting_args_p (args1))
1055 return 0;
1056 if (TYPE_ACTUAL_ARG_TYPES (f2)
1057 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1058 val = 2;
1059 return val;
1062 /* Both types have argument lists: compare them and propagate results. */
1063 val1 = type_lists_compatible_p (args1, args2);
1064 return val1 != 1 ? val1 : val;
1067 /* Check two lists of types for compatibility,
1068 returning 0 for incompatible, 1 for compatible,
1069 or 2 for compatible with warning. */
1071 static int
1072 type_lists_compatible_p (tree args1, tree args2)
1074 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1075 int val = 1;
1076 int newval = 0;
1078 while (1)
1080 tree a1, mv1, a2, mv2;
1081 if (args1 == 0 && args2 == 0)
1082 return val;
1083 /* If one list is shorter than the other,
1084 they fail to match. */
1085 if (args1 == 0 || args2 == 0)
1086 return 0;
1087 mv1 = a1 = TREE_VALUE (args1);
1088 mv2 = a2 = TREE_VALUE (args2);
1089 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1090 mv1 = TYPE_MAIN_VARIANT (mv1);
1091 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1092 mv2 = TYPE_MAIN_VARIANT (mv2);
1093 /* A null pointer instead of a type
1094 means there is supposed to be an argument
1095 but nothing is specified about what type it has.
1096 So match anything that self-promotes. */
1097 if (a1 == 0)
1099 if (c_type_promotes_to (a2) != a2)
1100 return 0;
1102 else if (a2 == 0)
1104 if (c_type_promotes_to (a1) != a1)
1105 return 0;
1107 /* If one of the lists has an error marker, ignore this arg. */
1108 else if (TREE_CODE (a1) == ERROR_MARK
1109 || TREE_CODE (a2) == ERROR_MARK)
1111 else if (!(newval = comptypes (mv1, mv2)))
1113 /* Allow wait (union {union wait *u; int *i} *)
1114 and wait (union wait *) to be compatible. */
1115 if (TREE_CODE (a1) == UNION_TYPE
1116 && (TYPE_NAME (a1) == 0
1117 || TYPE_TRANSPARENT_UNION (a1))
1118 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1119 && tree_int_cst_equal (TYPE_SIZE (a1),
1120 TYPE_SIZE (a2)))
1122 tree memb;
1123 for (memb = TYPE_FIELDS (a1);
1124 memb; memb = TREE_CHAIN (memb))
1125 if (comptypes (TREE_TYPE (memb), a2))
1126 break;
1127 if (memb == 0)
1128 return 0;
1130 else if (TREE_CODE (a2) == UNION_TYPE
1131 && (TYPE_NAME (a2) == 0
1132 || TYPE_TRANSPARENT_UNION (a2))
1133 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1134 && tree_int_cst_equal (TYPE_SIZE (a2),
1135 TYPE_SIZE (a1)))
1137 tree memb;
1138 for (memb = TYPE_FIELDS (a2);
1139 memb; memb = TREE_CHAIN (memb))
1140 if (comptypes (TREE_TYPE (memb), a1))
1141 break;
1142 if (memb == 0)
1143 return 0;
1145 else
1146 return 0;
1149 /* comptypes said ok, but record if it said to warn. */
1150 if (newval > val)
1151 val = newval;
1153 args1 = TREE_CHAIN (args1);
1154 args2 = TREE_CHAIN (args2);
1158 /* Compute the size to increment a pointer by. */
1160 static tree
1161 c_size_in_bytes (tree type)
1163 enum tree_code code = TREE_CODE (type);
1165 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1166 return size_one_node;
1168 if (!COMPLETE_OR_VOID_TYPE_P (type))
1170 error ("arithmetic on pointer to an incomplete type");
1171 return size_one_node;
1174 /* Convert in case a char is more than one unit. */
1175 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1176 size_int (TYPE_PRECISION (char_type_node)
1177 / BITS_PER_UNIT));
1180 /* Return either DECL or its known constant value (if it has one). */
1182 tree
1183 decl_constant_value (tree decl)
1185 if (/* Don't change a variable array bound or initial value to a constant
1186 in a place where a variable is invalid. Note that DECL_INITIAL
1187 isn't valid for a PARM_DECL. */
1188 current_function_decl != 0
1189 && TREE_CODE (decl) != PARM_DECL
1190 && !TREE_THIS_VOLATILE (decl)
1191 && TREE_READONLY (decl)
1192 && DECL_INITIAL (decl) != 0
1193 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1194 /* This is invalid if initial value is not constant.
1195 If it has either a function call, a memory reference,
1196 or a variable, then re-evaluating it could give different results. */
1197 && TREE_CONSTANT (DECL_INITIAL (decl))
1198 /* Check for cases where this is sub-optimal, even though valid. */
1199 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1200 return DECL_INITIAL (decl);
1201 return decl;
1204 /* Return either DECL or its known constant value (if it has one), but
1205 return DECL if pedantic or DECL has mode BLKmode. This is for
1206 bug-compatibility with the old behavior of decl_constant_value
1207 (before GCC 3.0); every use of this function is a bug and it should
1208 be removed before GCC 3.1. It is not appropriate to use pedantic
1209 in a way that affects optimization, and BLKmode is probably not the
1210 right test for avoiding misoptimizations either. */
1212 static tree
1213 decl_constant_value_for_broken_optimization (tree decl)
1215 if (pedantic || DECL_MODE (decl) == BLKmode)
1216 return decl;
1217 else
1218 return decl_constant_value (decl);
1222 /* Perform the default conversion of arrays and functions to pointers.
1223 Return the result of converting EXP. For any other expression, just
1224 return EXP. */
1226 static tree
1227 default_function_array_conversion (tree exp)
1229 tree orig_exp;
1230 tree type = TREE_TYPE (exp);
1231 enum tree_code code = TREE_CODE (type);
1232 int not_lvalue = 0;
1234 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1235 an lvalue.
1237 Do not use STRIP_NOPS here! It will remove conversions from pointer
1238 to integer and cause infinite recursion. */
1239 orig_exp = exp;
1240 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1241 || (TREE_CODE (exp) == NOP_EXPR
1242 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1244 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1245 not_lvalue = 1;
1246 exp = TREE_OPERAND (exp, 0);
1249 if (TREE_NO_WARNING (orig_exp))
1250 TREE_NO_WARNING (exp) = 1;
1252 if (code == FUNCTION_TYPE)
1254 return build_unary_op (ADDR_EXPR, exp, 0);
1256 if (code == ARRAY_TYPE)
1258 tree adr;
1259 tree restype = TREE_TYPE (type);
1260 tree ptrtype;
1261 int constp = 0;
1262 int volatilep = 0;
1263 int lvalue_array_p;
1265 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1267 constp = TREE_READONLY (exp);
1268 volatilep = TREE_THIS_VOLATILE (exp);
1271 if (TYPE_QUALS (type) || constp || volatilep)
1272 restype
1273 = c_build_qualified_type (restype,
1274 TYPE_QUALS (type)
1275 | (constp * TYPE_QUAL_CONST)
1276 | (volatilep * TYPE_QUAL_VOLATILE));
1278 if (TREE_CODE (exp) == INDIRECT_REF)
1279 return convert (build_pointer_type (restype),
1280 TREE_OPERAND (exp, 0));
1282 if (TREE_CODE (exp) == COMPOUND_EXPR)
1284 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1285 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1286 TREE_OPERAND (exp, 0), op1);
1289 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1290 if (!flag_isoc99 && !lvalue_array_p)
1292 /* Before C99, non-lvalue arrays do not decay to pointers.
1293 Normally, using such an array would be invalid; but it can
1294 be used correctly inside sizeof or as a statement expression.
1295 Thus, do not give an error here; an error will result later. */
1296 return exp;
1299 ptrtype = build_pointer_type (restype);
1301 if (TREE_CODE (exp) == VAR_DECL)
1303 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1304 ADDR_EXPR because it's the best way of representing what
1305 happens in C when we take the address of an array and place
1306 it in a pointer to the element type. */
1307 adr = build1 (ADDR_EXPR, ptrtype, exp);
1308 if (!c_mark_addressable (exp))
1309 return error_mark_node;
1310 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1311 return adr;
1313 /* This way is better for a COMPONENT_REF since it can
1314 simplify the offset for a component. */
1315 adr = build_unary_op (ADDR_EXPR, exp, 1);
1316 return convert (ptrtype, adr);
1318 return exp;
1321 /* Perform default promotions for C data used in expressions.
1322 Arrays and functions are converted to pointers;
1323 enumeral types or short or char, to int.
1324 In addition, manifest constants symbols are replaced by their values. */
1326 tree
1327 default_conversion (tree exp)
1329 tree orig_exp;
1330 tree type = TREE_TYPE (exp);
1331 enum tree_code code = TREE_CODE (type);
1333 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1334 return default_function_array_conversion (exp);
1336 /* Constants can be used directly unless they're not loadable. */
1337 if (TREE_CODE (exp) == CONST_DECL)
1338 exp = DECL_INITIAL (exp);
1340 /* Replace a nonvolatile const static variable with its value unless
1341 it is an array, in which case we must be sure that taking the
1342 address of the array produces consistent results. */
1343 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1345 exp = decl_constant_value_for_broken_optimization (exp);
1346 type = TREE_TYPE (exp);
1349 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1350 an lvalue.
1352 Do not use STRIP_NOPS here! It will remove conversions from pointer
1353 to integer and cause infinite recursion. */
1354 orig_exp = exp;
1355 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1356 || (TREE_CODE (exp) == NOP_EXPR
1357 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1358 exp = TREE_OPERAND (exp, 0);
1360 if (TREE_NO_WARNING (orig_exp))
1361 TREE_NO_WARNING (exp) = 1;
1363 /* Normally convert enums to int,
1364 but convert wide enums to something wider. */
1365 if (code == ENUMERAL_TYPE)
1367 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1368 TYPE_PRECISION (integer_type_node)),
1369 ((TYPE_PRECISION (type)
1370 >= TYPE_PRECISION (integer_type_node))
1371 && TYPE_UNSIGNED (type)));
1373 return convert (type, exp);
1376 if (TREE_CODE (exp) == COMPONENT_REF
1377 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1378 /* If it's thinner than an int, promote it like a
1379 c_promoting_integer_type_p, otherwise leave it alone. */
1380 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1381 TYPE_PRECISION (integer_type_node)))
1382 return convert (integer_type_node, exp);
1384 if (c_promoting_integer_type_p (type))
1386 /* Preserve unsignedness if not really getting any wider. */
1387 if (TYPE_UNSIGNED (type)
1388 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1389 return convert (unsigned_type_node, exp);
1391 return convert (integer_type_node, exp);
1394 if (code == VOID_TYPE)
1396 error ("void value not ignored as it ought to be");
1397 return error_mark_node;
1399 return exp;
1402 /* Look up COMPONENT in a structure or union DECL.
1404 If the component name is not found, returns NULL_TREE. Otherwise,
1405 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1406 stepping down the chain to the component, which is in the last
1407 TREE_VALUE of the list. Normally the list is of length one, but if
1408 the component is embedded within (nested) anonymous structures or
1409 unions, the list steps down the chain to the component. */
1411 static tree
1412 lookup_field (tree decl, tree component)
1414 tree type = TREE_TYPE (decl);
1415 tree field;
1417 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1418 to the field elements. Use a binary search on this array to quickly
1419 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1420 will always be set for structures which have many elements. */
1422 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1424 int bot, top, half;
1425 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1427 field = TYPE_FIELDS (type);
1428 bot = 0;
1429 top = TYPE_LANG_SPECIFIC (type)->s->len;
1430 while (top - bot > 1)
1432 half = (top - bot + 1) >> 1;
1433 field = field_array[bot+half];
1435 if (DECL_NAME (field) == NULL_TREE)
1437 /* Step through all anon unions in linear fashion. */
1438 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1440 field = field_array[bot++];
1441 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1442 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1444 tree anon = lookup_field (field, component);
1446 if (anon)
1447 return tree_cons (NULL_TREE, field, anon);
1451 /* Entire record is only anon unions. */
1452 if (bot > top)
1453 return NULL_TREE;
1455 /* Restart the binary search, with new lower bound. */
1456 continue;
1459 if (DECL_NAME (field) == component)
1460 break;
1461 if (DECL_NAME (field) < component)
1462 bot += half;
1463 else
1464 top = bot + half;
1467 if (DECL_NAME (field_array[bot]) == component)
1468 field = field_array[bot];
1469 else if (DECL_NAME (field) != component)
1470 return NULL_TREE;
1472 else
1474 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1476 if (DECL_NAME (field) == NULL_TREE
1477 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1478 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1480 tree anon = lookup_field (field, component);
1482 if (anon)
1483 return tree_cons (NULL_TREE, field, anon);
1486 if (DECL_NAME (field) == component)
1487 break;
1490 if (field == NULL_TREE)
1491 return NULL_TREE;
1494 return tree_cons (NULL_TREE, field, NULL_TREE);
1497 /* Make an expression to refer to the COMPONENT field of
1498 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1500 tree
1501 build_component_ref (tree datum, tree component)
1503 tree type = TREE_TYPE (datum);
1504 enum tree_code code = TREE_CODE (type);
1505 tree field = NULL;
1506 tree ref;
1508 if (!objc_is_public (datum, component))
1509 return error_mark_node;
1511 /* See if there is a field or component with name COMPONENT. */
1513 if (code == RECORD_TYPE || code == UNION_TYPE)
1515 if (!COMPLETE_TYPE_P (type))
1517 c_incomplete_type_error (NULL_TREE, type);
1518 return error_mark_node;
1521 field = lookup_field (datum, component);
1523 if (!field)
1525 error ("%qT has no member named %qs", type,
1526 IDENTIFIER_POINTER (component));
1527 return error_mark_node;
1530 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1531 This might be better solved in future the way the C++ front
1532 end does it - by giving the anonymous entities each a
1533 separate name and type, and then have build_component_ref
1534 recursively call itself. We can't do that here. */
1537 tree subdatum = TREE_VALUE (field);
1539 if (TREE_TYPE (subdatum) == error_mark_node)
1540 return error_mark_node;
1542 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1543 NULL_TREE);
1544 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1545 TREE_READONLY (ref) = 1;
1546 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1547 TREE_THIS_VOLATILE (ref) = 1;
1549 if (TREE_DEPRECATED (subdatum))
1550 warn_deprecated_use (subdatum);
1552 datum = ref;
1554 field = TREE_CHAIN (field);
1556 while (field);
1558 return ref;
1560 else if (code != ERROR_MARK)
1561 error ("request for member %qs in something not a structure or union",
1562 IDENTIFIER_POINTER (component));
1564 return error_mark_node;
1567 /* Given an expression PTR for a pointer, return an expression
1568 for the value pointed to.
1569 ERRORSTRING is the name of the operator to appear in error messages. */
1571 tree
1572 build_indirect_ref (tree ptr, const char *errorstring)
1574 tree pointer = default_conversion (ptr);
1575 tree type = TREE_TYPE (pointer);
1577 if (TREE_CODE (type) == POINTER_TYPE)
1579 if (TREE_CODE (pointer) == ADDR_EXPR
1580 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1581 == TREE_TYPE (type)))
1582 return TREE_OPERAND (pointer, 0);
1583 else
1585 tree t = TREE_TYPE (type);
1586 tree mvt = t;
1587 tree ref;
1589 if (TREE_CODE (mvt) != ARRAY_TYPE)
1590 mvt = TYPE_MAIN_VARIANT (mvt);
1591 ref = build1 (INDIRECT_REF, mvt, pointer);
1593 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1595 error ("dereferencing pointer to incomplete type");
1596 return error_mark_node;
1598 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1599 warning ("dereferencing %<void *%> pointer");
1601 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1602 so that we get the proper error message if the result is used
1603 to assign to. Also, &* is supposed to be a no-op.
1604 And ANSI C seems to specify that the type of the result
1605 should be the const type. */
1606 /* A de-reference of a pointer to const is not a const. It is valid
1607 to change it via some other pointer. */
1608 TREE_READONLY (ref) = TYPE_READONLY (t);
1609 TREE_SIDE_EFFECTS (ref)
1610 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1611 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1612 return ref;
1615 else if (TREE_CODE (pointer) != ERROR_MARK)
1616 error ("invalid type argument of %qs", errorstring);
1617 return error_mark_node;
1620 /* This handles expressions of the form "a[i]", which denotes
1621 an array reference.
1623 This is logically equivalent in C to *(a+i), but we may do it differently.
1624 If A is a variable or a member, we generate a primitive ARRAY_REF.
1625 This avoids forcing the array out of registers, and can work on
1626 arrays that are not lvalues (for example, members of structures returned
1627 by functions). */
1629 tree
1630 build_array_ref (tree array, tree index)
1632 bool swapped = false;
1633 if (TREE_TYPE (array) == error_mark_node
1634 || TREE_TYPE (index) == error_mark_node)
1635 return error_mark_node;
1637 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1638 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1640 tree temp;
1641 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1642 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1644 error ("subscripted value is neither array nor pointer");
1645 return error_mark_node;
1647 temp = array;
1648 array = index;
1649 index = temp;
1650 swapped = true;
1653 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1655 error ("array subscript is not an integer");
1656 return error_mark_node;
1659 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1661 error ("subscripted value is pointer to function");
1662 return error_mark_node;
1665 /* Subscripting with type char is likely to lose on a machine where
1666 chars are signed. So warn on any machine, but optionally. Don't
1667 warn for unsigned char since that type is safe. Don't warn for
1668 signed char because anyone who uses that must have done so
1669 deliberately. ??? Existing practice has also been to warn only
1670 when the char index is syntactically the index, not for
1671 char[array]. */
1672 if (warn_char_subscripts && !swapped
1673 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1674 warning ("array subscript has type %<char%>");
1676 /* Apply default promotions *after* noticing character types. */
1677 index = default_conversion (index);
1679 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1681 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1683 tree rval, type;
1685 /* An array that is indexed by a non-constant
1686 cannot be stored in a register; we must be able to do
1687 address arithmetic on its address.
1688 Likewise an array of elements of variable size. */
1689 if (TREE_CODE (index) != INTEGER_CST
1690 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1691 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1693 if (!c_mark_addressable (array))
1694 return error_mark_node;
1696 /* An array that is indexed by a constant value which is not within
1697 the array bounds cannot be stored in a register either; because we
1698 would get a crash in store_bit_field/extract_bit_field when trying
1699 to access a non-existent part of the register. */
1700 if (TREE_CODE (index) == INTEGER_CST
1701 && TYPE_DOMAIN (TREE_TYPE (array))
1702 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1704 if (!c_mark_addressable (array))
1705 return error_mark_node;
1708 if (pedantic)
1710 tree foo = array;
1711 while (TREE_CODE (foo) == COMPONENT_REF)
1712 foo = TREE_OPERAND (foo, 0);
1713 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1714 pedwarn ("ISO C forbids subscripting %<register%> array");
1715 else if (!flag_isoc99 && !lvalue_p (foo))
1716 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1719 type = TREE_TYPE (TREE_TYPE (array));
1720 if (TREE_CODE (type) != ARRAY_TYPE)
1721 type = TYPE_MAIN_VARIANT (type);
1722 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1723 /* Array ref is const/volatile if the array elements are
1724 or if the array is. */
1725 TREE_READONLY (rval)
1726 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1727 | TREE_READONLY (array));
1728 TREE_SIDE_EFFECTS (rval)
1729 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1730 | TREE_SIDE_EFFECTS (array));
1731 TREE_THIS_VOLATILE (rval)
1732 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1733 /* This was added by rms on 16 Nov 91.
1734 It fixes vol struct foo *a; a->elts[1]
1735 in an inline function.
1736 Hope it doesn't break something else. */
1737 | TREE_THIS_VOLATILE (array));
1738 return require_complete_type (fold (rval));
1740 else
1742 tree ar = default_conversion (array);
1744 if (ar == error_mark_node)
1745 return ar;
1747 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1748 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1750 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1751 "array indexing");
1755 /* Build an external reference to identifier ID. FUN indicates
1756 whether this will be used for a function call. */
1757 tree
1758 build_external_ref (tree id, int fun)
1760 tree ref;
1761 tree decl = lookup_name (id);
1763 /* In Objective-C, an instance variable (ivar) may be preferred to
1764 whatever lookup_name() found. */
1765 decl = objc_lookup_ivar (decl, id);
1767 if (decl && decl != error_mark_node)
1768 ref = decl;
1769 else if (fun)
1770 /* Implicit function declaration. */
1771 ref = implicitly_declare (id);
1772 else if (decl == error_mark_node)
1773 /* Don't complain about something that's already been
1774 complained about. */
1775 return error_mark_node;
1776 else
1778 undeclared_variable (id);
1779 return error_mark_node;
1782 if (TREE_TYPE (ref) == error_mark_node)
1783 return error_mark_node;
1785 if (TREE_DEPRECATED (ref))
1786 warn_deprecated_use (ref);
1788 if (!skip_evaluation)
1789 assemble_external (ref);
1790 TREE_USED (ref) = 1;
1792 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1794 if (!in_sizeof && !in_typeof)
1795 C_DECL_USED (ref) = 1;
1796 else if (DECL_INITIAL (ref) == 0
1797 && DECL_EXTERNAL (ref)
1798 && !TREE_PUBLIC (ref))
1799 record_maybe_used_decl (ref);
1802 if (TREE_CODE (ref) == CONST_DECL)
1804 ref = DECL_INITIAL (ref);
1805 TREE_CONSTANT (ref) = 1;
1806 TREE_INVARIANT (ref) = 1;
1808 else if (current_function_decl != 0
1809 && !DECL_FILE_SCOPE_P (current_function_decl)
1810 && (TREE_CODE (ref) == VAR_DECL
1811 || TREE_CODE (ref) == PARM_DECL
1812 || TREE_CODE (ref) == FUNCTION_DECL))
1814 tree context = decl_function_context (ref);
1816 if (context != 0 && context != current_function_decl)
1817 DECL_NONLOCAL (ref) = 1;
1820 return ref;
1823 /* Record details of decls possibly used inside sizeof or typeof. */
1824 struct maybe_used_decl
1826 /* The decl. */
1827 tree decl;
1828 /* The level seen at (in_sizeof + in_typeof). */
1829 int level;
1830 /* The next one at this level or above, or NULL. */
1831 struct maybe_used_decl *next;
1834 static struct maybe_used_decl *maybe_used_decls;
1836 /* Record that DECL, an undefined static function reference seen
1837 inside sizeof or typeof, might be used if the operand of sizeof is
1838 a VLA type or the operand of typeof is a variably modified
1839 type. */
1841 static void
1842 record_maybe_used_decl (tree decl)
1844 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1845 t->decl = decl;
1846 t->level = in_sizeof + in_typeof;
1847 t->next = maybe_used_decls;
1848 maybe_used_decls = t;
1851 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1852 USED is false, just discard them. If it is true, mark them used
1853 (if no longer inside sizeof or typeof) or move them to the next
1854 level up (if still inside sizeof or typeof). */
1856 void
1857 pop_maybe_used (bool used)
1859 struct maybe_used_decl *p = maybe_used_decls;
1860 int cur_level = in_sizeof + in_typeof;
1861 while (p && p->level > cur_level)
1863 if (used)
1865 if (cur_level == 0)
1866 C_DECL_USED (p->decl) = 1;
1867 else
1868 p->level = cur_level;
1870 p = p->next;
1872 if (!used || cur_level == 0)
1873 maybe_used_decls = p;
1876 /* Return the result of sizeof applied to EXPR. */
1878 struct c_expr
1879 c_expr_sizeof_expr (struct c_expr expr)
1881 struct c_expr ret;
1882 if (expr.value == error_mark_node)
1884 ret.value = error_mark_node;
1885 ret.original_code = ERROR_MARK;
1886 pop_maybe_used (false);
1888 else
1890 ret.value = c_sizeof (TREE_TYPE (expr.value));
1891 ret.original_code = ERROR_MARK;
1892 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1894 return ret;
1897 /* Return the result of sizeof applied to T, a structure for the type
1898 name passed to sizeof (rather than the type itself). */
1900 struct c_expr
1901 c_expr_sizeof_type (struct c_type_name *t)
1903 tree type;
1904 struct c_expr ret;
1905 type = groktypename (t);
1906 ret.value = c_sizeof (type);
1907 ret.original_code = ERROR_MARK;
1908 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1909 return ret;
1912 /* Build a function call to function FUNCTION with parameters PARAMS.
1913 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1914 TREE_VALUE of each node is a parameter-expression.
1915 FUNCTION's data type may be a function type or a pointer-to-function. */
1917 tree
1918 build_function_call (tree function, tree params)
1920 tree fntype, fundecl = 0;
1921 tree coerced_params;
1922 tree name = NULL_TREE, result;
1923 tree tem;
1925 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1926 STRIP_TYPE_NOPS (function);
1928 /* Convert anything with function type to a pointer-to-function. */
1929 if (TREE_CODE (function) == FUNCTION_DECL)
1931 name = DECL_NAME (function);
1933 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1934 (because calling an inline function does not mean the function
1935 needs to be separately compiled). */
1936 fntype = build_type_variant (TREE_TYPE (function),
1937 TREE_READONLY (function),
1938 TREE_THIS_VOLATILE (function));
1939 fundecl = function;
1940 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1942 else
1943 function = default_conversion (function);
1945 fntype = TREE_TYPE (function);
1947 if (TREE_CODE (fntype) == ERROR_MARK)
1948 return error_mark_node;
1950 if (!(TREE_CODE (fntype) == POINTER_TYPE
1951 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1953 error ("called object %qE is not a function", function);
1954 return error_mark_node;
1957 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1958 current_function_returns_abnormally = 1;
1960 /* fntype now gets the type of function pointed to. */
1961 fntype = TREE_TYPE (fntype);
1963 /* Check that the function is called through a compatible prototype.
1964 If it is not, replace the call by a trap, wrapped up in a compound
1965 expression if necessary. This has the nice side-effect to prevent
1966 the tree-inliner from generating invalid assignment trees which may
1967 blow up in the RTL expander later.
1969 ??? This doesn't work for Objective-C because objc_comptypes
1970 refuses to compare function prototypes, yet the compiler appears
1971 to build calls that are flagged as invalid by C's comptypes. */
1972 if (!c_dialect_objc ()
1973 && TREE_CODE (function) == NOP_EXPR
1974 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1975 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1976 && !comptypes (fntype, TREE_TYPE (tem)))
1978 tree return_type = TREE_TYPE (fntype);
1979 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1980 NULL_TREE);
1982 /* This situation leads to run-time undefined behavior. We can't,
1983 therefore, simply error unless we can prove that all possible
1984 executions of the program must execute the code. */
1985 warning ("function called through a non-compatible type");
1987 /* We can, however, treat "undefined" any way we please.
1988 Call abort to encourage the user to fix the program. */
1989 inform ("if this code is reached, the program will abort");
1991 if (VOID_TYPE_P (return_type))
1992 return trap;
1993 else
1995 tree rhs;
1997 if (AGGREGATE_TYPE_P (return_type))
1998 rhs = build_compound_literal (return_type,
1999 build_constructor (return_type,
2000 NULL_TREE));
2001 else
2002 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
2004 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2008 /* Convert the parameters to the types declared in the
2009 function prototype, or apply default promotions. */
2011 coerced_params
2012 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2014 if (coerced_params == error_mark_node)
2015 return error_mark_node;
2017 /* Check that the arguments to the function are valid. */
2019 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
2021 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
2022 function, coerced_params, NULL_TREE);
2023 TREE_SIDE_EFFECTS (result) = 1;
2025 if (require_constant_value)
2027 result = fold_initializer (result);
2029 if (TREE_CONSTANT (result)
2030 && (name == NULL_TREE
2031 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2032 pedwarn_init ("initializer element is not constant");
2034 else
2035 result = fold (result);
2037 if (VOID_TYPE_P (TREE_TYPE (result)))
2038 return result;
2039 return require_complete_type (result);
2042 /* Convert the argument expressions in the list VALUES
2043 to the types in the list TYPELIST. The result is a list of converted
2044 argument expressions, unless there are too few arguments in which
2045 case it is error_mark_node.
2047 If TYPELIST is exhausted, or when an element has NULL as its type,
2048 perform the default conversions.
2050 PARMLIST is the chain of parm decls for the function being called.
2051 It may be 0, if that info is not available.
2052 It is used only for generating error messages.
2054 FUNCTION is a tree for the called function. It is used only for
2055 error messages, where it is formatted with %qE.
2057 This is also where warnings about wrong number of args are generated.
2059 Both VALUES and the returned value are chains of TREE_LIST nodes
2060 with the elements of the list in the TREE_VALUE slots of those nodes. */
2062 static tree
2063 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2065 tree typetail, valtail;
2066 tree result = NULL;
2067 int parmnum;
2068 tree selector;
2070 /* Change pointer to function to the function itself for
2071 diagnostics. */
2072 if (TREE_CODE (function) == ADDR_EXPR
2073 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2074 function = TREE_OPERAND (function, 0);
2076 /* Handle an ObjC selector specially for diagnostics. */
2077 selector = objc_message_selector ();
2079 /* Scan the given expressions and types, producing individual
2080 converted arguments and pushing them on RESULT in reverse order. */
2082 for (valtail = values, typetail = typelist, parmnum = 0;
2083 valtail;
2084 valtail = TREE_CHAIN (valtail), parmnum++)
2086 tree type = typetail ? TREE_VALUE (typetail) : 0;
2087 tree val = TREE_VALUE (valtail);
2088 tree rname = function;
2089 int argnum = parmnum + 1;
2091 if (type == void_type_node)
2093 error ("too many arguments to function %qE", function);
2094 break;
2097 if (selector && argnum > 2)
2099 rname = selector;
2100 argnum -= 2;
2103 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2104 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
2105 to convert automatically to a pointer. */
2106 if (TREE_CODE (val) == NON_LVALUE_EXPR)
2107 val = TREE_OPERAND (val, 0);
2109 val = default_function_array_conversion (val);
2111 val = require_complete_type (val);
2113 if (type != 0)
2115 /* Formal parm type is specified by a function prototype. */
2116 tree parmval;
2118 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2120 error ("type of formal parameter %d is incomplete", parmnum + 1);
2121 parmval = val;
2123 else
2125 /* Optionally warn about conversions that
2126 differ from the default conversions. */
2127 if (warn_conversion || warn_traditional)
2129 unsigned int formal_prec = TYPE_PRECISION (type);
2131 if (INTEGRAL_TYPE_P (type)
2132 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2133 warning ("passing argument %d of %qE as integer "
2134 "rather than floating due to prototype",
2135 argnum, rname);
2136 if (INTEGRAL_TYPE_P (type)
2137 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2138 warning ("passing argument %d of %qE as integer "
2139 "rather than complex due to prototype",
2140 argnum, rname);
2141 else if (TREE_CODE (type) == COMPLEX_TYPE
2142 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2143 warning ("passing argument %d of %qE as complex "
2144 "rather than floating due to prototype",
2145 argnum, rname);
2146 else if (TREE_CODE (type) == REAL_TYPE
2147 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2148 warning ("passing argument %d of %qE as floating "
2149 "rather than integer due to prototype",
2150 argnum, rname);
2151 else if (TREE_CODE (type) == COMPLEX_TYPE
2152 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2153 warning ("passing argument %d of %qE as complex "
2154 "rather than integer due to prototype",
2155 argnum, rname);
2156 else if (TREE_CODE (type) == REAL_TYPE
2157 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2158 warning ("passing argument %d of %qE as floating "
2159 "rather than complex due to prototype",
2160 argnum, rname);
2161 /* ??? At some point, messages should be written about
2162 conversions between complex types, but that's too messy
2163 to do now. */
2164 else if (TREE_CODE (type) == REAL_TYPE
2165 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2167 /* Warn if any argument is passed as `float',
2168 since without a prototype it would be `double'. */
2169 if (formal_prec == TYPE_PRECISION (float_type_node))
2170 warning ("passing argument %d of %qE as %<float%> "
2171 "rather than %<double%> due to prototype",
2172 argnum, rname);
2174 /* Detect integer changing in width or signedness.
2175 These warnings are only activated with
2176 -Wconversion, not with -Wtraditional. */
2177 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2178 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2180 tree would_have_been = default_conversion (val);
2181 tree type1 = TREE_TYPE (would_have_been);
2183 if (TREE_CODE (type) == ENUMERAL_TYPE
2184 && (TYPE_MAIN_VARIANT (type)
2185 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2186 /* No warning if function asks for enum
2187 and the actual arg is that enum type. */
2189 else if (formal_prec != TYPE_PRECISION (type1))
2190 warning ("passing argument %d of %qE with different "
2191 "width due to prototype", argnum, rname);
2192 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2194 /* Don't complain if the formal parameter type
2195 is an enum, because we can't tell now whether
2196 the value was an enum--even the same enum. */
2197 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2199 else if (TREE_CODE (val) == INTEGER_CST
2200 && int_fits_type_p (val, type))
2201 /* Change in signedness doesn't matter
2202 if a constant value is unaffected. */
2204 /* Likewise for a constant in a NOP_EXPR. */
2205 else if (TREE_CODE (val) == NOP_EXPR
2206 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2207 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2209 /* If the value is extended from a narrower
2210 unsigned type, it doesn't matter whether we
2211 pass it as signed or unsigned; the value
2212 certainly is the same either way. */
2213 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2214 && TYPE_UNSIGNED (TREE_TYPE (val)))
2216 else if (TYPE_UNSIGNED (type))
2217 warning ("passing argument %d of %qE as unsigned "
2218 "due to prototype", argnum, rname);
2219 else
2220 warning ("passing argument %d of %qE as signed "
2221 "due to prototype", argnum, rname);
2225 parmval = convert_for_assignment (type, val, ic_argpass,
2226 fundecl, function,
2227 parmnum + 1);
2229 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2230 && INTEGRAL_TYPE_P (type)
2231 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2232 parmval = default_conversion (parmval);
2234 result = tree_cons (NULL_TREE, parmval, result);
2236 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2237 && (TYPE_PRECISION (TREE_TYPE (val))
2238 < TYPE_PRECISION (double_type_node)))
2239 /* Convert `float' to `double'. */
2240 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2241 else
2242 /* Convert `short' and `char' to full-size `int'. */
2243 result = tree_cons (NULL_TREE, default_conversion (val), result);
2245 if (typetail)
2246 typetail = TREE_CHAIN (typetail);
2249 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2251 error ("too few arguments to function %qE", function);
2252 return error_mark_node;
2255 return nreverse (result);
2258 /* This is the entry point used by the parser
2259 for binary operators in the input.
2260 In addition to constructing the expression,
2261 we check for operands that were written with other binary operators
2262 in a way that is likely to confuse the user. */
2264 struct c_expr
2265 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2266 struct c_expr arg2)
2268 struct c_expr result;
2270 enum tree_code code1 = arg1.original_code;
2271 enum tree_code code2 = arg2.original_code;
2273 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2274 result.original_code = code;
2276 if (TREE_CODE (result.value) == ERROR_MARK)
2277 return result;
2279 /* Check for cases such as x+y<<z which users are likely
2280 to misinterpret. */
2281 if (warn_parentheses)
2283 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2285 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2286 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2287 warning ("suggest parentheses around + or - inside shift");
2290 if (code == TRUTH_ORIF_EXPR)
2292 if (code1 == TRUTH_ANDIF_EXPR
2293 || code2 == TRUTH_ANDIF_EXPR)
2294 warning ("suggest parentheses around && within ||");
2297 if (code == BIT_IOR_EXPR)
2299 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2300 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2301 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2302 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2303 warning ("suggest parentheses around arithmetic in operand of |");
2304 /* Check cases like x|y==z */
2305 if (TREE_CODE_CLASS (code1) == tcc_comparison
2306 || TREE_CODE_CLASS (code2) == tcc_comparison)
2307 warning ("suggest parentheses around comparison in operand of |");
2310 if (code == BIT_XOR_EXPR)
2312 if (code1 == BIT_AND_EXPR
2313 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2314 || code2 == BIT_AND_EXPR
2315 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2316 warning ("suggest parentheses around arithmetic in operand of ^");
2317 /* Check cases like x^y==z */
2318 if (TREE_CODE_CLASS (code1) == tcc_comparison
2319 || TREE_CODE_CLASS (code2) == tcc_comparison)
2320 warning ("suggest parentheses around comparison in operand of ^");
2323 if (code == BIT_AND_EXPR)
2325 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2326 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2327 warning ("suggest parentheses around + or - in operand of &");
2328 /* Check cases like x&y==z */
2329 if (TREE_CODE_CLASS (code1) == tcc_comparison
2330 || TREE_CODE_CLASS (code2) == tcc_comparison)
2331 warning ("suggest parentheses around comparison in operand of &");
2333 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2334 if (TREE_CODE_CLASS (code) == tcc_comparison
2335 && (TREE_CODE_CLASS (code1) == tcc_comparison
2336 || TREE_CODE_CLASS (code2) == tcc_comparison))
2337 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2341 unsigned_conversion_warning (result.value, arg1.value);
2342 unsigned_conversion_warning (result.value, arg2.value);
2343 overflow_warning (result.value);
2345 return result;
2348 /* Return a tree for the difference of pointers OP0 and OP1.
2349 The resulting tree has type int. */
2351 static tree
2352 pointer_diff (tree op0, tree op1)
2354 tree restype = ptrdiff_type_node;
2356 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2357 tree con0, con1, lit0, lit1;
2358 tree orig_op1 = op1;
2360 if (pedantic || warn_pointer_arith)
2362 if (TREE_CODE (target_type) == VOID_TYPE)
2363 pedwarn ("pointer of type %<void *%> used in subtraction");
2364 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2365 pedwarn ("pointer to a function used in subtraction");
2368 /* If the conversion to ptrdiff_type does anything like widening or
2369 converting a partial to an integral mode, we get a convert_expression
2370 that is in the way to do any simplifications.
2371 (fold-const.c doesn't know that the extra bits won't be needed.
2372 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2373 different mode in place.)
2374 So first try to find a common term here 'by hand'; we want to cover
2375 at least the cases that occur in legal static initializers. */
2376 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2377 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2379 if (TREE_CODE (con0) == PLUS_EXPR)
2381 lit0 = TREE_OPERAND (con0, 1);
2382 con0 = TREE_OPERAND (con0, 0);
2384 else
2385 lit0 = integer_zero_node;
2387 if (TREE_CODE (con1) == PLUS_EXPR)
2389 lit1 = TREE_OPERAND (con1, 1);
2390 con1 = TREE_OPERAND (con1, 0);
2392 else
2393 lit1 = integer_zero_node;
2395 if (operand_equal_p (con0, con1, 0))
2397 op0 = lit0;
2398 op1 = lit1;
2402 /* First do the subtraction as integers;
2403 then drop through to build the divide operator.
2404 Do not do default conversions on the minus operator
2405 in case restype is a short type. */
2407 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2408 convert (restype, op1), 0);
2409 /* This generates an error if op1 is pointer to incomplete type. */
2410 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2411 error ("arithmetic on pointer to an incomplete type");
2413 /* This generates an error if op0 is pointer to incomplete type. */
2414 op1 = c_size_in_bytes (target_type);
2416 /* Divide by the size, in easiest possible way. */
2417 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2420 /* Construct and perhaps optimize a tree representation
2421 for a unary operation. CODE, a tree_code, specifies the operation
2422 and XARG is the operand.
2423 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2424 the default promotions (such as from short to int).
2425 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2426 allows non-lvalues; this is only used to handle conversion of non-lvalue
2427 arrays to pointers in C99. */
2429 tree
2430 build_unary_op (enum tree_code code, tree xarg, int flag)
2432 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2433 tree arg = xarg;
2434 tree argtype = 0;
2435 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2436 tree val;
2437 int noconvert = flag;
2439 if (typecode == ERROR_MARK)
2440 return error_mark_node;
2441 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2442 typecode = INTEGER_TYPE;
2444 switch (code)
2446 case CONVERT_EXPR:
2447 /* This is used for unary plus, because a CONVERT_EXPR
2448 is enough to prevent anybody from looking inside for
2449 associativity, but won't generate any code. */
2450 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2451 || typecode == COMPLEX_TYPE
2452 || typecode == VECTOR_TYPE))
2454 error ("wrong type argument to unary plus");
2455 return error_mark_node;
2457 else if (!noconvert)
2458 arg = default_conversion (arg);
2459 arg = non_lvalue (arg);
2460 break;
2462 case NEGATE_EXPR:
2463 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2464 || typecode == COMPLEX_TYPE
2465 || typecode == VECTOR_TYPE))
2467 error ("wrong type argument to unary minus");
2468 return error_mark_node;
2470 else if (!noconvert)
2471 arg = default_conversion (arg);
2472 break;
2474 case BIT_NOT_EXPR:
2475 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2477 if (!noconvert)
2478 arg = default_conversion (arg);
2480 else if (typecode == COMPLEX_TYPE)
2482 code = CONJ_EXPR;
2483 if (pedantic)
2484 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2485 if (!noconvert)
2486 arg = default_conversion (arg);
2488 else
2490 error ("wrong type argument to bit-complement");
2491 return error_mark_node;
2493 break;
2495 case ABS_EXPR:
2496 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2498 error ("wrong type argument to abs");
2499 return error_mark_node;
2501 else if (!noconvert)
2502 arg = default_conversion (arg);
2503 break;
2505 case CONJ_EXPR:
2506 /* Conjugating a real value is a no-op, but allow it anyway. */
2507 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2508 || typecode == COMPLEX_TYPE))
2510 error ("wrong type argument to conjugation");
2511 return error_mark_node;
2513 else if (!noconvert)
2514 arg = default_conversion (arg);
2515 break;
2517 case TRUTH_NOT_EXPR:
2518 if (typecode != INTEGER_TYPE
2519 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2520 && typecode != COMPLEX_TYPE
2521 /* These will convert to a pointer. */
2522 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2524 error ("wrong type argument to unary exclamation mark");
2525 return error_mark_node;
2527 arg = lang_hooks.truthvalue_conversion (arg);
2528 return invert_truthvalue (arg);
2530 case NOP_EXPR:
2531 break;
2533 case REALPART_EXPR:
2534 if (TREE_CODE (arg) == COMPLEX_CST)
2535 return TREE_REALPART (arg);
2536 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2537 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2538 else
2539 return arg;
2541 case IMAGPART_EXPR:
2542 if (TREE_CODE (arg) == COMPLEX_CST)
2543 return TREE_IMAGPART (arg);
2544 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2545 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2546 else
2547 return convert (TREE_TYPE (arg), integer_zero_node);
2549 case PREINCREMENT_EXPR:
2550 case POSTINCREMENT_EXPR:
2551 case PREDECREMENT_EXPR:
2552 case POSTDECREMENT_EXPR:
2554 /* Increment or decrement the real part of the value,
2555 and don't change the imaginary part. */
2556 if (typecode == COMPLEX_TYPE)
2558 tree real, imag;
2560 if (pedantic)
2561 pedwarn ("ISO C does not support %<++%> and %<--%>"
2562 " on complex types");
2564 arg = stabilize_reference (arg);
2565 real = build_unary_op (REALPART_EXPR, arg, 1);
2566 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2567 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2568 build_unary_op (code, real, 1), imag);
2571 /* Report invalid types. */
2573 if (typecode != POINTER_TYPE
2574 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2576 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2577 error ("wrong type argument to increment");
2578 else
2579 error ("wrong type argument to decrement");
2581 return error_mark_node;
2585 tree inc;
2586 tree result_type = TREE_TYPE (arg);
2588 arg = get_unwidened (arg, 0);
2589 argtype = TREE_TYPE (arg);
2591 /* Compute the increment. */
2593 if (typecode == POINTER_TYPE)
2595 /* If pointer target is an undefined struct,
2596 we just cannot know how to do the arithmetic. */
2597 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2599 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2600 error ("increment of pointer to unknown structure");
2601 else
2602 error ("decrement of pointer to unknown structure");
2604 else if ((pedantic || warn_pointer_arith)
2605 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2606 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2608 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2609 pedwarn ("wrong type argument to increment");
2610 else
2611 pedwarn ("wrong type argument to decrement");
2614 inc = c_size_in_bytes (TREE_TYPE (result_type));
2616 else
2617 inc = integer_one_node;
2619 inc = convert (argtype, inc);
2621 /* Complain about anything else that is not a true lvalue. */
2622 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2623 || code == POSTINCREMENT_EXPR)
2624 ? lv_increment
2625 : lv_decrement)))
2626 return error_mark_node;
2628 /* Report a read-only lvalue. */
2629 if (TREE_READONLY (arg))
2630 readonly_error (arg,
2631 ((code == PREINCREMENT_EXPR
2632 || code == POSTINCREMENT_EXPR)
2633 ? lv_increment : lv_decrement));
2635 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2636 val = boolean_increment (code, arg);
2637 else
2638 val = build2 (code, TREE_TYPE (arg), arg, inc);
2639 TREE_SIDE_EFFECTS (val) = 1;
2640 val = convert (result_type, val);
2641 if (TREE_CODE (val) != code)
2642 TREE_NO_WARNING (val) = 1;
2643 return val;
2646 case ADDR_EXPR:
2647 /* Note that this operation never does default_conversion. */
2649 /* Let &* cancel out to simplify resulting code. */
2650 if (TREE_CODE (arg) == INDIRECT_REF)
2652 /* Don't let this be an lvalue. */
2653 if (lvalue_p (TREE_OPERAND (arg, 0)))
2654 return non_lvalue (TREE_OPERAND (arg, 0));
2655 return TREE_OPERAND (arg, 0);
2658 /* For &x[y], return x+y */
2659 if (TREE_CODE (arg) == ARRAY_REF)
2661 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2662 return error_mark_node;
2663 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2664 TREE_OPERAND (arg, 1), 1);
2667 /* Anything not already handled and not a true memory reference
2668 or a non-lvalue array is an error. */
2669 else if (typecode != FUNCTION_TYPE && !flag
2670 && !lvalue_or_else (arg, lv_addressof))
2671 return error_mark_node;
2673 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2674 argtype = TREE_TYPE (arg);
2676 /* If the lvalue is const or volatile, merge that into the type
2677 to which the address will point. Note that you can't get a
2678 restricted pointer by taking the address of something, so we
2679 only have to deal with `const' and `volatile' here. */
2680 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2681 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2682 argtype = c_build_type_variant (argtype,
2683 TREE_READONLY (arg),
2684 TREE_THIS_VOLATILE (arg));
2686 if (!c_mark_addressable (arg))
2687 return error_mark_node;
2689 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2690 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2692 argtype = build_pointer_type (argtype);
2694 /* ??? Cope with user tricks that amount to offsetof. Delete this
2695 when we have proper support for integer constant expressions. */
2696 val = get_base_address (arg);
2697 if (val && TREE_CODE (val) == INDIRECT_REF
2698 && integer_zerop (TREE_OPERAND (val, 0)))
2699 return fold_convert (argtype, fold_offsetof (arg));
2701 val = build1 (ADDR_EXPR, argtype, arg);
2703 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2704 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2706 return val;
2708 default:
2709 break;
2712 if (argtype == 0)
2713 argtype = TREE_TYPE (arg);
2714 val = build1 (code, argtype, arg);
2715 return require_constant_value ? fold_initializer (val) : fold (val);
2718 /* Return nonzero if REF is an lvalue valid for this language.
2719 Lvalues can be assigned, unless their type has TYPE_READONLY.
2720 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2723 lvalue_p (tree ref)
2725 enum tree_code code = TREE_CODE (ref);
2727 switch (code)
2729 case REALPART_EXPR:
2730 case IMAGPART_EXPR:
2731 case COMPONENT_REF:
2732 return lvalue_p (TREE_OPERAND (ref, 0));
2734 case COMPOUND_LITERAL_EXPR:
2735 case STRING_CST:
2736 return 1;
2738 case INDIRECT_REF:
2739 case ARRAY_REF:
2740 case VAR_DECL:
2741 case PARM_DECL:
2742 case RESULT_DECL:
2743 case ERROR_MARK:
2744 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2745 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2747 case BIND_EXPR:
2748 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2750 default:
2751 return 0;
2755 /* Give an error for storing in something that is 'const'. */
2757 static void
2758 readonly_error (tree arg, enum lvalue_use use)
2760 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2761 /* Using this macro rather than (for example) arrays of messages
2762 ensures that all the format strings are checked at compile
2763 time. */
2764 #define READONLY_MSG(A, I, D) (use == lv_assign \
2765 ? (A) \
2766 : (use == lv_increment ? (I) : (D)))
2767 if (TREE_CODE (arg) == COMPONENT_REF)
2769 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2770 readonly_error (TREE_OPERAND (arg, 0), use);
2771 else
2772 error (READONLY_MSG (N_("assignment of read-only member %qs"),
2773 N_("increment of read-only member %qs"),
2774 N_("decrement of read-only member %qs")),
2775 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2777 else if (TREE_CODE (arg) == VAR_DECL)
2778 error (READONLY_MSG (N_("assignment of read-only variable %qs"),
2779 N_("increment of read-only variable %qs"),
2780 N_("decrement of read-only variable %qs")),
2781 IDENTIFIER_POINTER (DECL_NAME (arg)));
2782 else
2783 error (READONLY_MSG (N_("assignment of read-only location"),
2784 N_("increment of read-only location"),
2785 N_("decrement of read-only location")));
2788 /* Mark EXP saying that we need to be able to take the
2789 address of it; it should not be allocated in a register.
2790 Returns true if successful. */
2792 bool
2793 c_mark_addressable (tree exp)
2795 tree x = exp;
2797 while (1)
2798 switch (TREE_CODE (x))
2800 case COMPONENT_REF:
2801 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2803 error
2804 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2805 return false;
2808 /* ... fall through ... */
2810 case ADDR_EXPR:
2811 case ARRAY_REF:
2812 case REALPART_EXPR:
2813 case IMAGPART_EXPR:
2814 x = TREE_OPERAND (x, 0);
2815 break;
2817 case COMPOUND_LITERAL_EXPR:
2818 case CONSTRUCTOR:
2819 TREE_ADDRESSABLE (x) = 1;
2820 return true;
2822 case VAR_DECL:
2823 case CONST_DECL:
2824 case PARM_DECL:
2825 case RESULT_DECL:
2826 if (C_DECL_REGISTER (x)
2827 && DECL_NONLOCAL (x))
2829 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2831 error
2832 ("global register variable %qD used in nested function", x);
2833 return false;
2835 pedwarn ("register variable %qD used in nested function", x);
2837 else if (C_DECL_REGISTER (x))
2839 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2840 error ("address of global register variable %qD requested", x);
2841 else
2842 error ("address of register variable %qD requested", x);
2843 return false;
2846 /* drops in */
2847 case FUNCTION_DECL:
2848 TREE_ADDRESSABLE (x) = 1;
2849 /* drops out */
2850 default:
2851 return true;
2855 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2857 tree
2858 build_conditional_expr (tree ifexp, tree op1, tree op2)
2860 tree type1;
2861 tree type2;
2862 enum tree_code code1;
2863 enum tree_code code2;
2864 tree result_type = NULL;
2865 tree orig_op1 = op1, orig_op2 = op2;
2867 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2869 /* Promote both alternatives. */
2871 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2872 op1 = default_conversion (op1);
2873 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2874 op2 = default_conversion (op2);
2876 if (TREE_CODE (ifexp) == ERROR_MARK
2877 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2878 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2879 return error_mark_node;
2881 type1 = TREE_TYPE (op1);
2882 code1 = TREE_CODE (type1);
2883 type2 = TREE_TYPE (op2);
2884 code2 = TREE_CODE (type2);
2886 /* C90 does not permit non-lvalue arrays in conditional expressions.
2887 In C99 they will be pointers by now. */
2888 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2890 error ("non-lvalue array in conditional expression");
2891 return error_mark_node;
2894 /* Quickly detect the usual case where op1 and op2 have the same type
2895 after promotion. */
2896 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2898 if (type1 == type2)
2899 result_type = type1;
2900 else
2901 result_type = TYPE_MAIN_VARIANT (type1);
2903 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2904 || code1 == COMPLEX_TYPE)
2905 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2906 || code2 == COMPLEX_TYPE))
2908 result_type = c_common_type (type1, type2);
2910 /* If -Wsign-compare, warn here if type1 and type2 have
2911 different signedness. We'll promote the signed to unsigned
2912 and later code won't know it used to be different.
2913 Do this check on the original types, so that explicit casts
2914 will be considered, but default promotions won't. */
2915 if (warn_sign_compare && !skip_evaluation)
2917 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2918 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2920 if (unsigned_op1 ^ unsigned_op2)
2922 /* Do not warn if the result type is signed, since the
2923 signed type will only be chosen if it can represent
2924 all the values of the unsigned type. */
2925 if (!TYPE_UNSIGNED (result_type))
2926 /* OK */;
2927 /* Do not warn if the signed quantity is an unsuffixed
2928 integer literal (or some static constant expression
2929 involving such literals) and it is non-negative. */
2930 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2931 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2932 /* OK */;
2933 else
2934 warning ("signed and unsigned type in conditional expression");
2938 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2940 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2941 pedwarn ("ISO C forbids conditional expr with only one void side");
2942 result_type = void_type_node;
2944 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2946 if (comp_target_types (type1, type2, 1))
2947 result_type = common_pointer_type (type1, type2);
2948 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2949 && TREE_CODE (orig_op1) != NOP_EXPR)
2950 result_type = qualify_type (type2, type1);
2951 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2952 && TREE_CODE (orig_op2) != NOP_EXPR)
2953 result_type = qualify_type (type1, type2);
2954 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2956 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2957 pedwarn ("ISO C forbids conditional expr between "
2958 "%<void *%> and function pointer");
2959 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2960 TREE_TYPE (type2)));
2962 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2964 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2965 pedwarn ("ISO C forbids conditional expr between "
2966 "%<void *%> and function pointer");
2967 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2968 TREE_TYPE (type1)));
2970 else
2972 pedwarn ("pointer type mismatch in conditional expression");
2973 result_type = build_pointer_type (void_type_node);
2976 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2978 if (!integer_zerop (op2))
2979 pedwarn ("pointer/integer type mismatch in conditional expression");
2980 else
2982 op2 = null_pointer_node;
2984 result_type = type1;
2986 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2988 if (!integer_zerop (op1))
2989 pedwarn ("pointer/integer type mismatch in conditional expression");
2990 else
2992 op1 = null_pointer_node;
2994 result_type = type2;
2997 if (!result_type)
2999 if (flag_cond_mismatch)
3000 result_type = void_type_node;
3001 else
3003 error ("type mismatch in conditional expression");
3004 return error_mark_node;
3008 /* Merge const and volatile flags of the incoming types. */
3009 result_type
3010 = build_type_variant (result_type,
3011 TREE_READONLY (op1) || TREE_READONLY (op2),
3012 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3014 if (result_type != TREE_TYPE (op1))
3015 op1 = convert_and_check (result_type, op1);
3016 if (result_type != TREE_TYPE (op2))
3017 op2 = convert_and_check (result_type, op2);
3019 if (TREE_CODE (ifexp) == INTEGER_CST)
3020 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3022 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
3025 /* Return a compound expression that performs two expressions and
3026 returns the value of the second of them. */
3028 tree
3029 build_compound_expr (tree expr1, tree expr2)
3031 /* Convert arrays and functions to pointers. */
3032 expr2 = default_function_array_conversion (expr2);
3034 if (!TREE_SIDE_EFFECTS (expr1))
3036 /* The left-hand operand of a comma expression is like an expression
3037 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3038 any side-effects, unless it was explicitly cast to (void). */
3039 if (warn_unused_value
3040 && !(TREE_CODE (expr1) == CONVERT_EXPR
3041 && VOID_TYPE_P (TREE_TYPE (expr1))))
3042 warning ("left-hand operand of comma expression has no effect");
3045 /* With -Wunused, we should also warn if the left-hand operand does have
3046 side-effects, but computes a value which is not used. For example, in
3047 `foo() + bar(), baz()' the result of the `+' operator is not used,
3048 so we should issue a warning. */
3049 else if (warn_unused_value)
3050 warn_if_unused_value (expr1, input_location);
3052 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3055 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3057 tree
3058 build_c_cast (tree type, tree expr)
3060 tree value = expr;
3062 if (type == error_mark_node || expr == error_mark_node)
3063 return error_mark_node;
3065 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3066 only in <protocol> qualifications. But when constructing cast expressions,
3067 the protocols do matter and must be kept around. */
3068 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3069 return build1 (NOP_EXPR, type, expr);
3071 type = TYPE_MAIN_VARIANT (type);
3073 if (TREE_CODE (type) == ARRAY_TYPE)
3075 error ("cast specifies array type");
3076 return error_mark_node;
3079 if (TREE_CODE (type) == FUNCTION_TYPE)
3081 error ("cast specifies function type");
3082 return error_mark_node;
3085 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3087 if (pedantic)
3089 if (TREE_CODE (type) == RECORD_TYPE
3090 || TREE_CODE (type) == UNION_TYPE)
3091 pedwarn ("ISO C forbids casting nonscalar to the same type");
3094 else if (TREE_CODE (type) == UNION_TYPE)
3096 tree field;
3097 value = default_function_array_conversion (value);
3099 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3100 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3101 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3102 break;
3104 if (field)
3106 tree t;
3108 if (pedantic)
3109 pedwarn ("ISO C forbids casts to union type");
3110 t = digest_init (type,
3111 build_constructor (type,
3112 build_tree_list (field, value)),
3113 true, 0);
3114 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3115 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3116 return t;
3118 error ("cast to union type from type not present in union");
3119 return error_mark_node;
3121 else
3123 tree otype, ovalue;
3125 /* If casting to void, avoid the error that would come
3126 from default_conversion in the case of a non-lvalue array. */
3127 if (type == void_type_node)
3128 return build1 (CONVERT_EXPR, type, value);
3130 /* Convert functions and arrays to pointers,
3131 but don't convert any other types. */
3132 value = default_function_array_conversion (value);
3133 otype = TREE_TYPE (value);
3135 /* Optionally warn about potentially worrisome casts. */
3137 if (warn_cast_qual
3138 && TREE_CODE (type) == POINTER_TYPE
3139 && TREE_CODE (otype) == POINTER_TYPE)
3141 tree in_type = type;
3142 tree in_otype = otype;
3143 int added = 0;
3144 int discarded = 0;
3146 /* Check that the qualifiers on IN_TYPE are a superset of
3147 the qualifiers of IN_OTYPE. The outermost level of
3148 POINTER_TYPE nodes is uninteresting and we stop as soon
3149 as we hit a non-POINTER_TYPE node on either type. */
3152 in_otype = TREE_TYPE (in_otype);
3153 in_type = TREE_TYPE (in_type);
3155 /* GNU C allows cv-qualified function types. 'const'
3156 means the function is very pure, 'volatile' means it
3157 can't return. We need to warn when such qualifiers
3158 are added, not when they're taken away. */
3159 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3160 && TREE_CODE (in_type) == FUNCTION_TYPE)
3161 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3162 else
3163 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3165 while (TREE_CODE (in_type) == POINTER_TYPE
3166 && TREE_CODE (in_otype) == POINTER_TYPE);
3168 if (added)
3169 warning ("cast adds new qualifiers to function type");
3171 if (discarded)
3172 /* There are qualifiers present in IN_OTYPE that are not
3173 present in IN_TYPE. */
3174 warning ("cast discards qualifiers from pointer target type");
3177 /* Warn about possible alignment problems. */
3178 if (STRICT_ALIGNMENT && warn_cast_align
3179 && TREE_CODE (type) == POINTER_TYPE
3180 && TREE_CODE (otype) == POINTER_TYPE
3181 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3182 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3183 /* Don't warn about opaque types, where the actual alignment
3184 restriction is unknown. */
3185 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3186 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3187 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3188 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3189 warning ("cast increases required alignment of target type");
3191 if (TREE_CODE (type) == INTEGER_TYPE
3192 && TREE_CODE (otype) == POINTER_TYPE
3193 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3194 && !TREE_CONSTANT (value))
3195 warning ("cast from pointer to integer of different size");
3197 if (warn_bad_function_cast
3198 && TREE_CODE (value) == CALL_EXPR
3199 && TREE_CODE (type) != TREE_CODE (otype))
3200 warning ("cast from function call of type %qT to non-matching "
3201 "type %qT", otype, type);
3203 if (TREE_CODE (type) == POINTER_TYPE
3204 && TREE_CODE (otype) == INTEGER_TYPE
3205 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3206 /* Don't warn about converting any constant. */
3207 && !TREE_CONSTANT (value))
3208 warning ("cast to pointer from integer of different size");
3210 if (TREE_CODE (type) == POINTER_TYPE
3211 && TREE_CODE (otype) == POINTER_TYPE
3212 && TREE_CODE (expr) == ADDR_EXPR
3213 && DECL_P (TREE_OPERAND (expr, 0))
3214 && flag_strict_aliasing && warn_strict_aliasing
3215 && !VOID_TYPE_P (TREE_TYPE (type)))
3217 /* Casting the address of a decl to non void pointer. Warn
3218 if the cast breaks type based aliasing. */
3219 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3220 warning ("type-punning to incomplete type might break strict-aliasing rules");
3221 else
3223 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3224 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3226 if (!alias_sets_conflict_p (set1, set2))
3227 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3228 else if (warn_strict_aliasing > 1
3229 && !alias_sets_might_conflict_p (set1, set2))
3230 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3234 /* If pedantic, warn for conversions between function and object
3235 pointer types, except for converting a null pointer constant
3236 to function pointer type. */
3237 if (pedantic
3238 && TREE_CODE (type) == POINTER_TYPE
3239 && TREE_CODE (otype) == POINTER_TYPE
3240 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3241 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3242 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3244 if (pedantic
3245 && TREE_CODE (type) == POINTER_TYPE
3246 && TREE_CODE (otype) == POINTER_TYPE
3247 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3248 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3249 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3250 && TREE_CODE (expr) != NOP_EXPR))
3251 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3253 ovalue = value;
3254 value = convert (type, value);
3256 /* Ignore any integer overflow caused by the cast. */
3257 if (TREE_CODE (value) == INTEGER_CST)
3259 if (EXPR_P (ovalue))
3260 /* If OVALUE had overflow set, then so will VALUE, so it
3261 is safe to overwrite. */
3262 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3263 else
3264 TREE_OVERFLOW (value) = 0;
3266 if (CONSTANT_CLASS_P (ovalue))
3267 /* Similarly, constant_overflow cannot have become
3268 cleared. */
3269 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3273 /* Don't let a cast be an lvalue. */
3274 if (value == expr)
3275 value = non_lvalue (value);
3277 return value;
3280 /* Interpret a cast of expression EXPR to type TYPE. */
3281 tree
3282 c_cast_expr (struct c_type_name *type_name, tree expr)
3284 tree type;
3285 int saved_wsp = warn_strict_prototypes;
3287 /* This avoids warnings about unprototyped casts on
3288 integers. E.g. "#define SIG_DFL (void(*)())0". */
3289 if (TREE_CODE (expr) == INTEGER_CST)
3290 warn_strict_prototypes = 0;
3291 type = groktypename (type_name);
3292 warn_strict_prototypes = saved_wsp;
3294 return build_c_cast (type, expr);
3298 /* Build an assignment expression of lvalue LHS from value RHS.
3299 MODIFYCODE is the code for a binary operator that we use
3300 to combine the old value of LHS with RHS to get the new value.
3301 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3303 tree
3304 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3306 tree result;
3307 tree newrhs;
3308 tree lhstype = TREE_TYPE (lhs);
3309 tree olhstype = lhstype;
3311 /* Types that aren't fully specified cannot be used in assignments. */
3312 lhs = require_complete_type (lhs);
3314 /* Avoid duplicate error messages from operands that had errors. */
3315 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3316 return error_mark_node;
3318 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3319 /* Do not use STRIP_NOPS here. We do not want an enumerator
3320 whose value is 0 to count as a null pointer constant. */
3321 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3322 rhs = TREE_OPERAND (rhs, 0);
3324 newrhs = rhs;
3326 /* If a binary op has been requested, combine the old LHS value with the RHS
3327 producing the value we should actually store into the LHS. */
3329 if (modifycode != NOP_EXPR)
3331 lhs = stabilize_reference (lhs);
3332 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3335 if (!lvalue_or_else (lhs, lv_assign))
3336 return error_mark_node;
3338 /* Give an error for storing in something that is 'const'. */
3340 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3341 || ((TREE_CODE (lhstype) == RECORD_TYPE
3342 || TREE_CODE (lhstype) == UNION_TYPE)
3343 && C_TYPE_FIELDS_READONLY (lhstype)))
3344 readonly_error (lhs, lv_assign);
3346 /* If storing into a structure or union member,
3347 it has probably been given type `int'.
3348 Compute the type that would go with
3349 the actual amount of storage the member occupies. */
3351 if (TREE_CODE (lhs) == COMPONENT_REF
3352 && (TREE_CODE (lhstype) == INTEGER_TYPE
3353 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3354 || TREE_CODE (lhstype) == REAL_TYPE
3355 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3356 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3358 /* If storing in a field that is in actuality a short or narrower than one,
3359 we must store in the field in its actual type. */
3361 if (lhstype != TREE_TYPE (lhs))
3363 lhs = copy_node (lhs);
3364 TREE_TYPE (lhs) = lhstype;
3367 /* Convert new value to destination type. */
3369 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3370 NULL_TREE, NULL_TREE, 0);
3371 if (TREE_CODE (newrhs) == ERROR_MARK)
3372 return error_mark_node;
3374 /* Scan operands. */
3376 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3377 TREE_SIDE_EFFECTS (result) = 1;
3379 /* If we got the LHS in a different type for storing in,
3380 convert the result back to the nominal type of LHS
3381 so that the value we return always has the same type
3382 as the LHS argument. */
3384 if (olhstype == TREE_TYPE (result))
3385 return result;
3386 return convert_for_assignment (olhstype, result, ic_assign,
3387 NULL_TREE, NULL_TREE, 0);
3390 /* Convert value RHS to type TYPE as preparation for an assignment
3391 to an lvalue of type TYPE.
3392 The real work of conversion is done by `convert'.
3393 The purpose of this function is to generate error messages
3394 for assignments that are not allowed in C.
3395 ERRTYPE says whether it is argument passing, assignment,
3396 initialization or return.
3398 FUNCTION is a tree for the function being called.
3399 PARMNUM is the number of the argument, for printing in error messages. */
3401 static tree
3402 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3403 tree fundecl, tree function, int parmnum)
3405 enum tree_code codel = TREE_CODE (type);
3406 tree rhstype;
3407 enum tree_code coder;
3408 tree rname = NULL_TREE;
3410 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3412 tree selector;
3413 /* Change pointer to function to the function itself for
3414 diagnostics. */
3415 if (TREE_CODE (function) == ADDR_EXPR
3416 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3417 function = TREE_OPERAND (function, 0);
3419 /* Handle an ObjC selector specially for diagnostics. */
3420 selector = objc_message_selector ();
3421 rname = function;
3422 if (selector && parmnum > 2)
3424 rname = selector;
3425 parmnum -= 2;
3429 /* This macro is used to emit diagnostics to ensure that all format
3430 strings are complete sentences, visible to gettext and checked at
3431 compile time. */
3432 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3433 do { \
3434 switch (errtype) \
3436 case ic_argpass: \
3437 pedwarn (AR, parmnum, rname); \
3438 break; \
3439 case ic_argpass_nonproto: \
3440 warning (AR, parmnum, rname); \
3441 break; \
3442 case ic_assign: \
3443 pedwarn (AS); \
3444 break; \
3445 case ic_init: \
3446 pedwarn (IN); \
3447 break; \
3448 case ic_return: \
3449 pedwarn (RE); \
3450 break; \
3451 default: \
3452 gcc_unreachable (); \
3454 } while (0)
3456 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3457 /* Do not use STRIP_NOPS here. We do not want an enumerator
3458 whose value is 0 to count as a null pointer constant. */
3459 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3460 rhs = TREE_OPERAND (rhs, 0);
3462 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3463 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3464 rhs = default_conversion (rhs);
3465 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3466 rhs = decl_constant_value_for_broken_optimization (rhs);
3468 rhstype = TREE_TYPE (rhs);
3469 coder = TREE_CODE (rhstype);
3471 if (coder == ERROR_MARK)
3472 return error_mark_node;
3474 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3476 overflow_warning (rhs);
3477 /* Check for Objective-C protocols. This will automatically
3478 issue a warning if there are protocol violations. No need to
3479 use the return value. */
3480 if (c_dialect_objc ())
3481 objc_comptypes (type, rhstype, 0);
3482 return rhs;
3485 if (coder == VOID_TYPE)
3487 /* Except for passing an argument to an unprototyped function,
3488 this is a constraint violation. When passing an argument to
3489 an unprototyped function, it is compile-time undefined;
3490 making it a constraint in that case was rejected in
3491 DR#252. */
3492 error ("void value not ignored as it ought to be");
3493 return error_mark_node;
3495 /* A type converts to a reference to it.
3496 This code doesn't fully support references, it's just for the
3497 special case of va_start and va_copy. */
3498 if (codel == REFERENCE_TYPE
3499 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3501 if (!lvalue_p (rhs))
3503 error ("cannot pass rvalue to reference parameter");
3504 return error_mark_node;
3506 if (!c_mark_addressable (rhs))
3507 return error_mark_node;
3508 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3510 /* We already know that these two types are compatible, but they
3511 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3512 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3513 likely to be va_list, a typedef to __builtin_va_list, which
3514 is different enough that it will cause problems later. */
3515 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3516 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3518 rhs = build1 (NOP_EXPR, type, rhs);
3519 return rhs;
3521 /* Some types can interconvert without explicit casts. */
3522 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3523 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3524 return convert (type, rhs);
3525 /* Arithmetic types all interconvert, and enum is treated like int. */
3526 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3527 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3528 || codel == BOOLEAN_TYPE)
3529 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3530 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3531 || coder == BOOLEAN_TYPE))
3532 return convert_and_check (type, rhs);
3534 /* Conversion to a transparent union from its member types.
3535 This applies only to function arguments. */
3536 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3537 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3539 tree memb_types;
3540 tree marginal_memb_type = 0;
3542 for (memb_types = TYPE_FIELDS (type); memb_types;
3543 memb_types = TREE_CHAIN (memb_types))
3545 tree memb_type = TREE_TYPE (memb_types);
3547 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3548 TYPE_MAIN_VARIANT (rhstype)))
3549 break;
3551 if (TREE_CODE (memb_type) != POINTER_TYPE)
3552 continue;
3554 if (coder == POINTER_TYPE)
3556 tree ttl = TREE_TYPE (memb_type);
3557 tree ttr = TREE_TYPE (rhstype);
3559 /* Any non-function converts to a [const][volatile] void *
3560 and vice versa; otherwise, targets must be the same.
3561 Meanwhile, the lhs target must have all the qualifiers of
3562 the rhs. */
3563 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3564 || comp_target_types (memb_type, rhstype, 0))
3566 /* If this type won't generate any warnings, use it. */
3567 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3568 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3569 && TREE_CODE (ttl) == FUNCTION_TYPE)
3570 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3571 == TYPE_QUALS (ttr))
3572 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3573 == TYPE_QUALS (ttl))))
3574 break;
3576 /* Keep looking for a better type, but remember this one. */
3577 if (!marginal_memb_type)
3578 marginal_memb_type = memb_type;
3582 /* Can convert integer zero to any pointer type. */
3583 if (integer_zerop (rhs)
3584 || (TREE_CODE (rhs) == NOP_EXPR
3585 && integer_zerop (TREE_OPERAND (rhs, 0))))
3587 rhs = null_pointer_node;
3588 break;
3592 if (memb_types || marginal_memb_type)
3594 if (!memb_types)
3596 /* We have only a marginally acceptable member type;
3597 it needs a warning. */
3598 tree ttl = TREE_TYPE (marginal_memb_type);
3599 tree ttr = TREE_TYPE (rhstype);
3601 /* Const and volatile mean something different for function
3602 types, so the usual warnings are not appropriate. */
3603 if (TREE_CODE (ttr) == FUNCTION_TYPE
3604 && TREE_CODE (ttl) == FUNCTION_TYPE)
3606 /* Because const and volatile on functions are
3607 restrictions that say the function will not do
3608 certain things, it is okay to use a const or volatile
3609 function where an ordinary one is wanted, but not
3610 vice-versa. */
3611 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3612 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3613 "makes qualified function "
3614 "pointer from unqualified"),
3615 N_("assignment makes qualified "
3616 "function pointer from "
3617 "unqualified"),
3618 N_("initialization makes qualified "
3619 "function pointer from "
3620 "unqualified"),
3621 N_("return makes qualified function "
3622 "pointer from unqualified"));
3624 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3625 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3626 "qualifiers from pointer target type"),
3627 N_("assignment discards qualifiers "
3628 "from pointer target type"),
3629 N_("initialization discards qualifiers "
3630 "from pointer target type"),
3631 N_("return discards qualifiers from "
3632 "pointer target type"));
3635 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3636 pedwarn ("ISO C prohibits argument conversion to union type");
3638 return build1 (NOP_EXPR, type, rhs);
3642 /* Conversions among pointers */
3643 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3644 && (coder == codel))
3646 tree ttl = TREE_TYPE (type);
3647 tree ttr = TREE_TYPE (rhstype);
3648 tree mvl = ttl;
3649 tree mvr = ttr;
3650 bool is_opaque_pointer;
3651 int target_cmp = 0; /* Cache comp_target_types () result. */
3653 if (TREE_CODE (mvl) != ARRAY_TYPE)
3654 mvl = TYPE_MAIN_VARIANT (mvl);
3655 if (TREE_CODE (mvr) != ARRAY_TYPE)
3656 mvr = TYPE_MAIN_VARIANT (mvr);
3657 /* Opaque pointers are treated like void pointers. */
3658 is_opaque_pointer = (targetm.vector_opaque_p (type)
3659 || targetm.vector_opaque_p (rhstype))
3660 && TREE_CODE (ttl) == VECTOR_TYPE
3661 && TREE_CODE (ttr) == VECTOR_TYPE;
3663 /* Any non-function converts to a [const][volatile] void *
3664 and vice versa; otherwise, targets must be the same.
3665 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3666 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3667 || (target_cmp = comp_target_types (type, rhstype, 0))
3668 || is_opaque_pointer
3669 || (c_common_unsigned_type (mvl)
3670 == c_common_unsigned_type (mvr)))
3672 if (pedantic
3673 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3675 (VOID_TYPE_P (ttr)
3676 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3677 which are not ANSI null ptr constants. */
3678 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3679 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3680 WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3681 "%qE between function pointer "
3682 "and %<void *%>"),
3683 N_("ISO C forbids assignment between "
3684 "function pointer and %<void *%>"),
3685 N_("ISO C forbids initialization between "
3686 "function pointer and %<void *%>"),
3687 N_("ISO C forbids return between function "
3688 "pointer and %<void *%>"));
3689 /* Const and volatile mean something different for function types,
3690 so the usual warnings are not appropriate. */
3691 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3692 && TREE_CODE (ttl) != FUNCTION_TYPE)
3694 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3695 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3696 "qualifiers from pointer target type"),
3697 N_("assignment discards qualifiers "
3698 "from pointer target type"),
3699 N_("initialization discards qualifiers "
3700 "from pointer target type"),
3701 N_("return discards qualifiers from "
3702 "pointer target type"));
3703 /* If this is not a case of ignoring a mismatch in signedness,
3704 no warning. */
3705 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3706 || target_cmp)
3708 /* If there is a mismatch, do warn. */
3709 else if (warn_pointer_sign)
3710 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3711 "%d of %qE differ in signedness"),
3712 N_("pointer targets in assignment "
3713 "differ in signedness"),
3714 N_("pointer targets in initialization "
3715 "differ in signedness"),
3716 N_("pointer targets in return differ "
3717 "in signedness"));
3719 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3720 && TREE_CODE (ttr) == FUNCTION_TYPE)
3722 /* Because const and volatile on functions are restrictions
3723 that say the function will not do certain things,
3724 it is okay to use a const or volatile function
3725 where an ordinary one is wanted, but not vice-versa. */
3726 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3727 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3728 "qualified function pointer "
3729 "from unqualified"),
3730 N_("assignment makes qualified function "
3731 "pointer from unqualified"),
3732 N_("initialization makes qualified "
3733 "function pointer from unqualified"),
3734 N_("return makes qualified function "
3735 "pointer from unqualified"));
3738 else
3739 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3740 "incompatible pointer type"),
3741 N_("assignment from incompatible pointer type"),
3742 N_("initialization from incompatible "
3743 "pointer type"),
3744 N_("return from incompatible pointer type"));
3745 return convert (type, rhs);
3747 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3749 /* ??? This should not be an error when inlining calls to
3750 unprototyped functions. */
3751 error ("invalid use of non-lvalue array");
3752 return error_mark_node;
3754 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3756 /* An explicit constant 0 can convert to a pointer,
3757 or one that results from arithmetic, even including
3758 a cast to integer type. */
3759 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3761 !(TREE_CODE (rhs) == NOP_EXPR
3762 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3763 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3764 && integer_zerop (TREE_OPERAND (rhs, 0))))
3765 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3766 "pointer from integer without a cast"),
3767 N_("assignment makes pointer from integer "
3768 "without a cast"),
3769 N_("initialization makes pointer from "
3770 "integer without a cast"),
3771 N_("return makes pointer from integer "
3772 "without a cast"));
3774 return convert (type, rhs);
3776 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3778 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3779 "from pointer without a cast"),
3780 N_("assignment makes integer from pointer "
3781 "without a cast"),
3782 N_("initialization makes integer from pointer "
3783 "without a cast"),
3784 N_("return makes integer from pointer "
3785 "without a cast"));
3786 return convert (type, rhs);
3788 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3789 return convert (type, rhs);
3791 switch (errtype)
3793 case ic_argpass:
3794 case ic_argpass_nonproto:
3795 /* ??? This should not be an error when inlining calls to
3796 unprototyped functions. */
3797 error ("incompatible type for argument %d of %qE", parmnum, rname);
3798 break;
3799 case ic_assign:
3800 error ("incompatible types in assignment");
3801 break;
3802 case ic_init:
3803 error ("incompatible types in initialization");
3804 break;
3805 case ic_return:
3806 error ("incompatible types in return");
3807 break;
3808 default:
3809 gcc_unreachable ();
3812 return error_mark_node;
3815 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3816 is used for error and waring reporting and indicates which argument
3817 is being processed. */
3819 tree
3820 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3822 tree ret, type;
3824 /* If FN was prototyped, the value has been converted already
3825 in convert_arguments. */
3826 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3827 return value;
3829 type = TREE_TYPE (parm);
3830 ret = convert_for_assignment (type, value,
3831 ic_argpass_nonproto, fn,
3832 fn, argnum);
3833 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3834 && INTEGRAL_TYPE_P (type)
3835 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3836 ret = default_conversion (ret);
3837 return ret;
3840 /* If VALUE is a compound expr all of whose expressions are constant, then
3841 return its value. Otherwise, return error_mark_node.
3843 This is for handling COMPOUND_EXPRs as initializer elements
3844 which is allowed with a warning when -pedantic is specified. */
3846 static tree
3847 valid_compound_expr_initializer (tree value, tree endtype)
3849 if (TREE_CODE (value) == COMPOUND_EXPR)
3851 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3852 == error_mark_node)
3853 return error_mark_node;
3854 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3855 endtype);
3857 else if (!initializer_constant_valid_p (value, endtype))
3858 return error_mark_node;
3859 else
3860 return value;
3863 /* Perform appropriate conversions on the initial value of a variable,
3864 store it in the declaration DECL,
3865 and print any error messages that are appropriate.
3866 If the init is invalid, store an ERROR_MARK. */
3868 void
3869 store_init_value (tree decl, tree init)
3871 tree value, type;
3873 /* If variable's type was invalidly declared, just ignore it. */
3875 type = TREE_TYPE (decl);
3876 if (TREE_CODE (type) == ERROR_MARK)
3877 return;
3879 /* Digest the specified initializer into an expression. */
3881 value = digest_init (type, init, true, TREE_STATIC (decl));
3883 /* Store the expression if valid; else report error. */
3885 if (warn_traditional && !in_system_header
3886 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3887 warning ("traditional C rejects automatic aggregate initialization");
3889 DECL_INITIAL (decl) = value;
3891 /* ANSI wants warnings about out-of-range constant initializers. */
3892 STRIP_TYPE_NOPS (value);
3893 constant_expression_warning (value);
3895 /* Check if we need to set array size from compound literal size. */
3896 if (TREE_CODE (type) == ARRAY_TYPE
3897 && TYPE_DOMAIN (type) == 0
3898 && value != error_mark_node)
3900 tree inside_init = init;
3902 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3903 inside_init = TREE_OPERAND (init, 0);
3904 inside_init = fold (inside_init);
3906 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3908 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3910 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3912 /* For int foo[] = (int [3]){1}; we need to set array size
3913 now since later on array initializer will be just the
3914 brace enclosed list of the compound literal. */
3915 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3916 layout_type (type);
3917 layout_decl (decl, 0);
3923 /* Methods for storing and printing names for error messages. */
3925 /* Implement a spelling stack that allows components of a name to be pushed
3926 and popped. Each element on the stack is this structure. */
3928 struct spelling
3930 int kind;
3931 union
3933 int i;
3934 const char *s;
3935 } u;
3938 #define SPELLING_STRING 1
3939 #define SPELLING_MEMBER 2
3940 #define SPELLING_BOUNDS 3
3942 static struct spelling *spelling; /* Next stack element (unused). */
3943 static struct spelling *spelling_base; /* Spelling stack base. */
3944 static int spelling_size; /* Size of the spelling stack. */
3946 /* Macros to save and restore the spelling stack around push_... functions.
3947 Alternative to SAVE_SPELLING_STACK. */
3949 #define SPELLING_DEPTH() (spelling - spelling_base)
3950 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3952 /* Push an element on the spelling stack with type KIND and assign VALUE
3953 to MEMBER. */
3955 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3957 int depth = SPELLING_DEPTH (); \
3959 if (depth >= spelling_size) \
3961 spelling_size += 10; \
3962 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3963 spelling_size); \
3964 RESTORE_SPELLING_DEPTH (depth); \
3967 spelling->kind = (KIND); \
3968 spelling->MEMBER = (VALUE); \
3969 spelling++; \
3972 /* Push STRING on the stack. Printed literally. */
3974 static void
3975 push_string (const char *string)
3977 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3980 /* Push a member name on the stack. Printed as '.' STRING. */
3982 static void
3983 push_member_name (tree decl)
3985 const char *const string
3986 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3987 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3990 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3992 static void
3993 push_array_bounds (int bounds)
3995 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3998 /* Compute the maximum size in bytes of the printed spelling. */
4000 static int
4001 spelling_length (void)
4003 int size = 0;
4004 struct spelling *p;
4006 for (p = spelling_base; p < spelling; p++)
4008 if (p->kind == SPELLING_BOUNDS)
4009 size += 25;
4010 else
4011 size += strlen (p->u.s) + 1;
4014 return size;
4017 /* Print the spelling to BUFFER and return it. */
4019 static char *
4020 print_spelling (char *buffer)
4022 char *d = buffer;
4023 struct spelling *p;
4025 for (p = spelling_base; p < spelling; p++)
4026 if (p->kind == SPELLING_BOUNDS)
4028 sprintf (d, "[%d]", p->u.i);
4029 d += strlen (d);
4031 else
4033 const char *s;
4034 if (p->kind == SPELLING_MEMBER)
4035 *d++ = '.';
4036 for (s = p->u.s; (*d = *s++); d++)
4039 *d++ = '\0';
4040 return buffer;
4043 /* Issue an error message for a bad initializer component.
4044 MSGID identifies the message.
4045 The component name is taken from the spelling stack. */
4047 void
4048 error_init (const char *msgid)
4050 char *ofwhat;
4052 error ("%s", _(msgid));
4053 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4054 if (*ofwhat)
4055 error ("(near initialization for %qs)", ofwhat);
4058 /* Issue a pedantic warning for a bad initializer component.
4059 MSGID identifies the message.
4060 The component name is taken from the spelling stack. */
4062 void
4063 pedwarn_init (const char *msgid)
4065 char *ofwhat;
4067 pedwarn ("%s", _(msgid));
4068 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4069 if (*ofwhat)
4070 pedwarn ("(near initialization for %qs)", ofwhat);
4073 /* Issue a warning for a bad initializer component.
4074 MSGID identifies the message.
4075 The component name is taken from the spelling stack. */
4077 static void
4078 warning_init (const char *msgid)
4080 char *ofwhat;
4082 warning ("%s", _(msgid));
4083 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4084 if (*ofwhat)
4085 warning ("(near initialization for %qs)", ofwhat);
4088 /* If TYPE is an array type and EXPR is a parenthesized string
4089 constant, warn if pedantic that EXPR is being used to initialize an
4090 object of type TYPE. */
4092 void
4093 maybe_warn_string_init (tree type, struct c_expr expr)
4095 if (pedantic
4096 && TREE_CODE (type) == ARRAY_TYPE
4097 && TREE_CODE (expr.value) == STRING_CST
4098 && expr.original_code != STRING_CST)
4099 pedwarn_init ("array initialized from parenthesized string constant");
4102 /* Digest the parser output INIT as an initializer for type TYPE.
4103 Return a C expression of type TYPE to represent the initial value.
4105 If INIT is a string constant, STRICT_STRING is true if it is
4106 unparenthesized or we should not warn here for it being parenthesized.
4107 For other types of INIT, STRICT_STRING is not used.
4109 REQUIRE_CONSTANT requests an error if non-constant initializers or
4110 elements are seen. */
4112 static tree
4113 digest_init (tree type, tree init, bool strict_string, int require_constant)
4115 enum tree_code code = TREE_CODE (type);
4116 tree inside_init = init;
4118 if (type == error_mark_node
4119 || init == error_mark_node
4120 || TREE_TYPE (init) == error_mark_node)
4121 return error_mark_node;
4123 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4124 /* Do not use STRIP_NOPS here. We do not want an enumerator
4125 whose value is 0 to count as a null pointer constant. */
4126 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4127 inside_init = TREE_OPERAND (init, 0);
4129 inside_init = fold (inside_init);
4131 /* Initialization of an array of chars from a string constant
4132 optionally enclosed in braces. */
4134 if (code == ARRAY_TYPE && inside_init
4135 && TREE_CODE (inside_init) == STRING_CST)
4137 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4138 /* Note that an array could be both an array of character type
4139 and an array of wchar_t if wchar_t is signed char or unsigned
4140 char. */
4141 bool char_array = (typ1 == char_type_node
4142 || typ1 == signed_char_type_node
4143 || typ1 == unsigned_char_type_node);
4144 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4145 if (char_array || wchar_array)
4147 struct c_expr expr;
4148 bool char_string;
4149 expr.value = inside_init;
4150 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4151 maybe_warn_string_init (type, expr);
4153 char_string
4154 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4155 == char_type_node);
4157 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4158 TYPE_MAIN_VARIANT (type)))
4159 return inside_init;
4161 if (!wchar_array && !char_string)
4163 error_init ("char-array initialized from wide string");
4164 return error_mark_node;
4166 if (char_string && !char_array)
4168 error_init ("wchar_t-array initialized from non-wide string");
4169 return error_mark_node;
4172 TREE_TYPE (inside_init) = type;
4173 if (TYPE_DOMAIN (type) != 0
4174 && TYPE_SIZE (type) != 0
4175 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4176 /* Subtract 1 (or sizeof (wchar_t))
4177 because it's ok to ignore the terminating null char
4178 that is counted in the length of the constant. */
4179 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4180 TREE_STRING_LENGTH (inside_init)
4181 - ((TYPE_PRECISION (typ1)
4182 != TYPE_PRECISION (char_type_node))
4183 ? (TYPE_PRECISION (wchar_type_node)
4184 / BITS_PER_UNIT)
4185 : 1)))
4186 pedwarn_init ("initializer-string for array of chars is too long");
4188 return inside_init;
4190 else if (INTEGRAL_TYPE_P (typ1))
4192 error_init ("array of inappropriate type initialized "
4193 "from string constant");
4194 return error_mark_node;
4198 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4199 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4200 below and handle as a constructor. */
4201 if (code == VECTOR_TYPE
4202 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4203 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4204 && TREE_CONSTANT (inside_init))
4206 if (TREE_CODE (inside_init) == VECTOR_CST
4207 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4208 TYPE_MAIN_VARIANT (type)))
4209 return inside_init;
4211 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4213 tree link;
4215 /* Iterate through elements and check if all constructor
4216 elements are *_CSTs. */
4217 for (link = CONSTRUCTOR_ELTS (inside_init);
4218 link;
4219 link = TREE_CHAIN (link))
4220 if (! CONSTANT_CLASS_P (TREE_VALUE (link)))
4221 break;
4223 if (link == NULL)
4224 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4228 /* Any type can be initialized
4229 from an expression of the same type, optionally with braces. */
4231 if (inside_init && TREE_TYPE (inside_init) != 0
4232 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4233 TYPE_MAIN_VARIANT (type))
4234 || (code == ARRAY_TYPE
4235 && comptypes (TREE_TYPE (inside_init), type))
4236 || (code == VECTOR_TYPE
4237 && comptypes (TREE_TYPE (inside_init), type))
4238 || (code == POINTER_TYPE
4239 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4240 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4241 TREE_TYPE (type)))
4242 || (code == POINTER_TYPE
4243 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4244 && comptypes (TREE_TYPE (inside_init),
4245 TREE_TYPE (type)))))
4247 if (code == POINTER_TYPE)
4249 inside_init = default_function_array_conversion (inside_init);
4251 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4253 error_init ("invalid use of non-lvalue array");
4254 return error_mark_node;
4258 if (code == VECTOR_TYPE)
4259 /* Although the types are compatible, we may require a
4260 conversion. */
4261 inside_init = convert (type, inside_init);
4263 if (require_constant && !flag_isoc99
4264 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4266 /* As an extension, allow initializing objects with static storage
4267 duration with compound literals (which are then treated just as
4268 the brace enclosed list they contain). */
4269 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4270 inside_init = DECL_INITIAL (decl);
4273 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4274 && TREE_CODE (inside_init) != CONSTRUCTOR)
4276 error_init ("array initialized from non-constant array expression");
4277 return error_mark_node;
4280 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4281 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4283 /* Compound expressions can only occur here if -pedantic or
4284 -pedantic-errors is specified. In the later case, we always want
4285 an error. In the former case, we simply want a warning. */
4286 if (require_constant && pedantic
4287 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4289 inside_init
4290 = valid_compound_expr_initializer (inside_init,
4291 TREE_TYPE (inside_init));
4292 if (inside_init == error_mark_node)
4293 error_init ("initializer element is not constant");
4294 else
4295 pedwarn_init ("initializer element is not constant");
4296 if (flag_pedantic_errors)
4297 inside_init = error_mark_node;
4299 else if (require_constant
4300 && !initializer_constant_valid_p (inside_init,
4301 TREE_TYPE (inside_init)))
4303 error_init ("initializer element is not constant");
4304 inside_init = error_mark_node;
4307 return inside_init;
4310 /* Handle scalar types, including conversions. */
4312 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4313 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4314 || code == VECTOR_TYPE)
4316 /* Note that convert_for_assignment calls default_conversion
4317 for arrays and functions. We must not call it in the
4318 case where inside_init is a null pointer constant. */
4319 inside_init
4320 = convert_for_assignment (type, init, ic_init,
4321 NULL_TREE, NULL_TREE, 0);
4323 /* Check to see if we have already given an error message. */
4324 if (inside_init == error_mark_node)
4326 else if (require_constant && !TREE_CONSTANT (inside_init))
4328 error_init ("initializer element is not constant");
4329 inside_init = error_mark_node;
4331 else if (require_constant
4332 && !initializer_constant_valid_p (inside_init,
4333 TREE_TYPE (inside_init)))
4335 error_init ("initializer element is not computable at load time");
4336 inside_init = error_mark_node;
4339 return inside_init;
4342 /* Come here only for records and arrays. */
4344 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4346 error_init ("variable-sized object may not be initialized");
4347 return error_mark_node;
4350 error_init ("invalid initializer");
4351 return error_mark_node;
4354 /* Handle initializers that use braces. */
4356 /* Type of object we are accumulating a constructor for.
4357 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4358 static tree constructor_type;
4360 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4361 left to fill. */
4362 static tree constructor_fields;
4364 /* For an ARRAY_TYPE, this is the specified index
4365 at which to store the next element we get. */
4366 static tree constructor_index;
4368 /* For an ARRAY_TYPE, this is the maximum index. */
4369 static tree constructor_max_index;
4371 /* For a RECORD_TYPE, this is the first field not yet written out. */
4372 static tree constructor_unfilled_fields;
4374 /* For an ARRAY_TYPE, this is the index of the first element
4375 not yet written out. */
4376 static tree constructor_unfilled_index;
4378 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4379 This is so we can generate gaps between fields, when appropriate. */
4380 static tree constructor_bit_index;
4382 /* If we are saving up the elements rather than allocating them,
4383 this is the list of elements so far (in reverse order,
4384 most recent first). */
4385 static tree constructor_elements;
4387 /* 1 if constructor should be incrementally stored into a constructor chain,
4388 0 if all the elements should be kept in AVL tree. */
4389 static int constructor_incremental;
4391 /* 1 if so far this constructor's elements are all compile-time constants. */
4392 static int constructor_constant;
4394 /* 1 if so far this constructor's elements are all valid address constants. */
4395 static int constructor_simple;
4397 /* 1 if this constructor is erroneous so far. */
4398 static int constructor_erroneous;
4400 /* Structure for managing pending initializer elements, organized as an
4401 AVL tree. */
4403 struct init_node
4405 struct init_node *left, *right;
4406 struct init_node *parent;
4407 int balance;
4408 tree purpose;
4409 tree value;
4412 /* Tree of pending elements at this constructor level.
4413 These are elements encountered out of order
4414 which belong at places we haven't reached yet in actually
4415 writing the output.
4416 Will never hold tree nodes across GC runs. */
4417 static struct init_node *constructor_pending_elts;
4419 /* The SPELLING_DEPTH of this constructor. */
4420 static int constructor_depth;
4422 /* DECL node for which an initializer is being read.
4423 0 means we are reading a constructor expression
4424 such as (struct foo) {...}. */
4425 static tree constructor_decl;
4427 /* Nonzero if this is an initializer for a top-level decl. */
4428 static int constructor_top_level;
4430 /* Nonzero if there were any member designators in this initializer. */
4431 static int constructor_designated;
4433 /* Nesting depth of designator list. */
4434 static int designator_depth;
4436 /* Nonzero if there were diagnosed errors in this designator list. */
4437 static int designator_errorneous;
4440 /* This stack has a level for each implicit or explicit level of
4441 structuring in the initializer, including the outermost one. It
4442 saves the values of most of the variables above. */
4444 struct constructor_range_stack;
4446 struct constructor_stack
4448 struct constructor_stack *next;
4449 tree type;
4450 tree fields;
4451 tree index;
4452 tree max_index;
4453 tree unfilled_index;
4454 tree unfilled_fields;
4455 tree bit_index;
4456 tree elements;
4457 struct init_node *pending_elts;
4458 int offset;
4459 int depth;
4460 /* If value nonzero, this value should replace the entire
4461 constructor at this level. */
4462 struct c_expr replacement_value;
4463 struct constructor_range_stack *range_stack;
4464 char constant;
4465 char simple;
4466 char implicit;
4467 char erroneous;
4468 char outer;
4469 char incremental;
4470 char designated;
4473 struct constructor_stack *constructor_stack;
4475 /* This stack represents designators from some range designator up to
4476 the last designator in the list. */
4478 struct constructor_range_stack
4480 struct constructor_range_stack *next, *prev;
4481 struct constructor_stack *stack;
4482 tree range_start;
4483 tree index;
4484 tree range_end;
4485 tree fields;
4488 struct constructor_range_stack *constructor_range_stack;
4490 /* This stack records separate initializers that are nested.
4491 Nested initializers can't happen in ANSI C, but GNU C allows them
4492 in cases like { ... (struct foo) { ... } ... }. */
4494 struct initializer_stack
4496 struct initializer_stack *next;
4497 tree decl;
4498 struct constructor_stack *constructor_stack;
4499 struct constructor_range_stack *constructor_range_stack;
4500 tree elements;
4501 struct spelling *spelling;
4502 struct spelling *spelling_base;
4503 int spelling_size;
4504 char top_level;
4505 char require_constant_value;
4506 char require_constant_elements;
4509 struct initializer_stack *initializer_stack;
4511 /* Prepare to parse and output the initializer for variable DECL. */
4513 void
4514 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4516 const char *locus;
4517 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4519 p->decl = constructor_decl;
4520 p->require_constant_value = require_constant_value;
4521 p->require_constant_elements = require_constant_elements;
4522 p->constructor_stack = constructor_stack;
4523 p->constructor_range_stack = constructor_range_stack;
4524 p->elements = constructor_elements;
4525 p->spelling = spelling;
4526 p->spelling_base = spelling_base;
4527 p->spelling_size = spelling_size;
4528 p->top_level = constructor_top_level;
4529 p->next = initializer_stack;
4530 initializer_stack = p;
4532 constructor_decl = decl;
4533 constructor_designated = 0;
4534 constructor_top_level = top_level;
4536 if (decl != 0 && decl != error_mark_node)
4538 require_constant_value = TREE_STATIC (decl);
4539 require_constant_elements
4540 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4541 /* For a scalar, you can always use any value to initialize,
4542 even within braces. */
4543 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4544 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4545 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4546 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4547 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4549 else
4551 require_constant_value = 0;
4552 require_constant_elements = 0;
4553 locus = "(anonymous)";
4556 constructor_stack = 0;
4557 constructor_range_stack = 0;
4559 missing_braces_mentioned = 0;
4561 spelling_base = 0;
4562 spelling_size = 0;
4563 RESTORE_SPELLING_DEPTH (0);
4565 if (locus)
4566 push_string (locus);
4569 void
4570 finish_init (void)
4572 struct initializer_stack *p = initializer_stack;
4574 /* Free the whole constructor stack of this initializer. */
4575 while (constructor_stack)
4577 struct constructor_stack *q = constructor_stack;
4578 constructor_stack = q->next;
4579 free (q);
4582 gcc_assert (!constructor_range_stack);
4584 /* Pop back to the data of the outer initializer (if any). */
4585 free (spelling_base);
4587 constructor_decl = p->decl;
4588 require_constant_value = p->require_constant_value;
4589 require_constant_elements = p->require_constant_elements;
4590 constructor_stack = p->constructor_stack;
4591 constructor_range_stack = p->constructor_range_stack;
4592 constructor_elements = p->elements;
4593 spelling = p->spelling;
4594 spelling_base = p->spelling_base;
4595 spelling_size = p->spelling_size;
4596 constructor_top_level = p->top_level;
4597 initializer_stack = p->next;
4598 free (p);
4601 /* Call here when we see the initializer is surrounded by braces.
4602 This is instead of a call to push_init_level;
4603 it is matched by a call to pop_init_level.
4605 TYPE is the type to initialize, for a constructor expression.
4606 For an initializer for a decl, TYPE is zero. */
4608 void
4609 really_start_incremental_init (tree type)
4611 struct constructor_stack *p = XNEW (struct constructor_stack);
4613 if (type == 0)
4614 type = TREE_TYPE (constructor_decl);
4616 if (targetm.vector_opaque_p (type))
4617 error ("opaque vector types cannot be initialized");
4619 p->type = constructor_type;
4620 p->fields = constructor_fields;
4621 p->index = constructor_index;
4622 p->max_index = constructor_max_index;
4623 p->unfilled_index = constructor_unfilled_index;
4624 p->unfilled_fields = constructor_unfilled_fields;
4625 p->bit_index = constructor_bit_index;
4626 p->elements = constructor_elements;
4627 p->constant = constructor_constant;
4628 p->simple = constructor_simple;
4629 p->erroneous = constructor_erroneous;
4630 p->pending_elts = constructor_pending_elts;
4631 p->depth = constructor_depth;
4632 p->replacement_value.value = 0;
4633 p->replacement_value.original_code = ERROR_MARK;
4634 p->implicit = 0;
4635 p->range_stack = 0;
4636 p->outer = 0;
4637 p->incremental = constructor_incremental;
4638 p->designated = constructor_designated;
4639 p->next = 0;
4640 constructor_stack = p;
4642 constructor_constant = 1;
4643 constructor_simple = 1;
4644 constructor_depth = SPELLING_DEPTH ();
4645 constructor_elements = 0;
4646 constructor_pending_elts = 0;
4647 constructor_type = type;
4648 constructor_incremental = 1;
4649 constructor_designated = 0;
4650 designator_depth = 0;
4651 designator_errorneous = 0;
4653 if (TREE_CODE (constructor_type) == RECORD_TYPE
4654 || TREE_CODE (constructor_type) == UNION_TYPE)
4656 constructor_fields = TYPE_FIELDS (constructor_type);
4657 /* Skip any nameless bit fields at the beginning. */
4658 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4659 && DECL_NAME (constructor_fields) == 0)
4660 constructor_fields = TREE_CHAIN (constructor_fields);
4662 constructor_unfilled_fields = constructor_fields;
4663 constructor_bit_index = bitsize_zero_node;
4665 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4667 if (TYPE_DOMAIN (constructor_type))
4669 constructor_max_index
4670 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4672 /* Detect non-empty initializations of zero-length arrays. */
4673 if (constructor_max_index == NULL_TREE
4674 && TYPE_SIZE (constructor_type))
4675 constructor_max_index = build_int_cst (NULL_TREE, -1);
4677 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4678 to initialize VLAs will cause a proper error; avoid tree
4679 checking errors as well by setting a safe value. */
4680 if (constructor_max_index
4681 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4682 constructor_max_index = build_int_cst (NULL_TREE, -1);
4684 constructor_index
4685 = convert (bitsizetype,
4686 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4688 else
4690 constructor_index = bitsize_zero_node;
4691 constructor_max_index = NULL_TREE;
4694 constructor_unfilled_index = constructor_index;
4696 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4698 /* Vectors are like simple fixed-size arrays. */
4699 constructor_max_index =
4700 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4701 constructor_index = convert (bitsizetype, bitsize_zero_node);
4702 constructor_unfilled_index = constructor_index;
4704 else
4706 /* Handle the case of int x = {5}; */
4707 constructor_fields = constructor_type;
4708 constructor_unfilled_fields = constructor_type;
4712 /* Push down into a subobject, for initialization.
4713 If this is for an explicit set of braces, IMPLICIT is 0.
4714 If it is because the next element belongs at a lower level,
4715 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4717 void
4718 push_init_level (int implicit)
4720 struct constructor_stack *p;
4721 tree value = NULL_TREE;
4723 /* If we've exhausted any levels that didn't have braces,
4724 pop them now. */
4725 while (constructor_stack->implicit)
4727 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4728 || TREE_CODE (constructor_type) == UNION_TYPE)
4729 && constructor_fields == 0)
4730 process_init_element (pop_init_level (1));
4731 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4732 && constructor_max_index
4733 && tree_int_cst_lt (constructor_max_index, constructor_index))
4734 process_init_element (pop_init_level (1));
4735 else
4736 break;
4739 /* Unless this is an explicit brace, we need to preserve previous
4740 content if any. */
4741 if (implicit)
4743 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4744 || TREE_CODE (constructor_type) == UNION_TYPE)
4745 && constructor_fields)
4746 value = find_init_member (constructor_fields);
4747 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4748 value = find_init_member (constructor_index);
4751 p = XNEW (struct constructor_stack);
4752 p->type = constructor_type;
4753 p->fields = constructor_fields;
4754 p->index = constructor_index;
4755 p->max_index = constructor_max_index;
4756 p->unfilled_index = constructor_unfilled_index;
4757 p->unfilled_fields = constructor_unfilled_fields;
4758 p->bit_index = constructor_bit_index;
4759 p->elements = constructor_elements;
4760 p->constant = constructor_constant;
4761 p->simple = constructor_simple;
4762 p->erroneous = constructor_erroneous;
4763 p->pending_elts = constructor_pending_elts;
4764 p->depth = constructor_depth;
4765 p->replacement_value.value = 0;
4766 p->replacement_value.original_code = ERROR_MARK;
4767 p->implicit = implicit;
4768 p->outer = 0;
4769 p->incremental = constructor_incremental;
4770 p->designated = constructor_designated;
4771 p->next = constructor_stack;
4772 p->range_stack = 0;
4773 constructor_stack = p;
4775 constructor_constant = 1;
4776 constructor_simple = 1;
4777 constructor_depth = SPELLING_DEPTH ();
4778 constructor_elements = 0;
4779 constructor_incremental = 1;
4780 constructor_designated = 0;
4781 constructor_pending_elts = 0;
4782 if (!implicit)
4784 p->range_stack = constructor_range_stack;
4785 constructor_range_stack = 0;
4786 designator_depth = 0;
4787 designator_errorneous = 0;
4790 /* Don't die if an entire brace-pair level is superfluous
4791 in the containing level. */
4792 if (constructor_type == 0)
4794 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4795 || TREE_CODE (constructor_type) == UNION_TYPE)
4797 /* Don't die if there are extra init elts at the end. */
4798 if (constructor_fields == 0)
4799 constructor_type = 0;
4800 else
4802 constructor_type = TREE_TYPE (constructor_fields);
4803 push_member_name (constructor_fields);
4804 constructor_depth++;
4807 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4809 constructor_type = TREE_TYPE (constructor_type);
4810 push_array_bounds (tree_low_cst (constructor_index, 0));
4811 constructor_depth++;
4814 if (constructor_type == 0)
4816 error_init ("extra brace group at end of initializer");
4817 constructor_fields = 0;
4818 constructor_unfilled_fields = 0;
4819 return;
4822 if (value && TREE_CODE (value) == CONSTRUCTOR)
4824 constructor_constant = TREE_CONSTANT (value);
4825 constructor_simple = TREE_STATIC (value);
4826 constructor_elements = CONSTRUCTOR_ELTS (value);
4827 if (constructor_elements
4828 && (TREE_CODE (constructor_type) == RECORD_TYPE
4829 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4830 set_nonincremental_init ();
4833 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4835 missing_braces_mentioned = 1;
4836 warning_init ("missing braces around initializer");
4839 if (TREE_CODE (constructor_type) == RECORD_TYPE
4840 || TREE_CODE (constructor_type) == UNION_TYPE)
4842 constructor_fields = TYPE_FIELDS (constructor_type);
4843 /* Skip any nameless bit fields at the beginning. */
4844 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4845 && DECL_NAME (constructor_fields) == 0)
4846 constructor_fields = TREE_CHAIN (constructor_fields);
4848 constructor_unfilled_fields = constructor_fields;
4849 constructor_bit_index = bitsize_zero_node;
4851 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4853 /* Vectors are like simple fixed-size arrays. */
4854 constructor_max_index =
4855 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4856 constructor_index = convert (bitsizetype, integer_zero_node);
4857 constructor_unfilled_index = constructor_index;
4859 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4861 if (TYPE_DOMAIN (constructor_type))
4863 constructor_max_index
4864 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4866 /* Detect non-empty initializations of zero-length arrays. */
4867 if (constructor_max_index == NULL_TREE
4868 && TYPE_SIZE (constructor_type))
4869 constructor_max_index = build_int_cst (NULL_TREE, -1);
4871 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4872 to initialize VLAs will cause a proper error; avoid tree
4873 checking errors as well by setting a safe value. */
4874 if (constructor_max_index
4875 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4876 constructor_max_index = build_int_cst (NULL_TREE, -1);
4878 constructor_index
4879 = convert (bitsizetype,
4880 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4882 else
4883 constructor_index = bitsize_zero_node;
4885 constructor_unfilled_index = constructor_index;
4886 if (value && TREE_CODE (value) == STRING_CST)
4888 /* We need to split the char/wchar array into individual
4889 characters, so that we don't have to special case it
4890 everywhere. */
4891 set_nonincremental_init_from_string (value);
4894 else
4896 if (constructor_type != error_mark_node)
4897 warning_init ("braces around scalar initializer");
4898 constructor_fields = constructor_type;
4899 constructor_unfilled_fields = constructor_type;
4903 /* At the end of an implicit or explicit brace level,
4904 finish up that level of constructor. If a single expression
4905 with redundant braces initialized that level, return the
4906 c_expr structure for that expression. Otherwise, the original_code
4907 element is set to ERROR_MARK.
4908 If we were outputting the elements as they are read, return 0 as the value
4909 from inner levels (process_init_element ignores that),
4910 but return error_mark_node as the value from the outermost level
4911 (that's what we want to put in DECL_INITIAL).
4912 Otherwise, return a CONSTRUCTOR expression as the value. */
4914 struct c_expr
4915 pop_init_level (int implicit)
4917 struct constructor_stack *p;
4918 struct c_expr ret;
4919 ret.value = 0;
4920 ret.original_code = ERROR_MARK;
4922 if (implicit == 0)
4924 /* When we come to an explicit close brace,
4925 pop any inner levels that didn't have explicit braces. */
4926 while (constructor_stack->implicit)
4927 process_init_element (pop_init_level (1));
4929 gcc_assert (!constructor_range_stack);
4932 /* Now output all pending elements. */
4933 constructor_incremental = 1;
4934 output_pending_init_elements (1);
4936 p = constructor_stack;
4938 /* Error for initializing a flexible array member, or a zero-length
4939 array member in an inappropriate context. */
4940 if (constructor_type && constructor_fields
4941 && TREE_CODE (constructor_type) == ARRAY_TYPE
4942 && TYPE_DOMAIN (constructor_type)
4943 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4945 /* Silently discard empty initializations. The parser will
4946 already have pedwarned for empty brackets. */
4947 if (integer_zerop (constructor_unfilled_index))
4948 constructor_type = NULL_TREE;
4949 else
4951 gcc_assert (!TYPE_SIZE (constructor_type));
4953 if (constructor_depth > 2)
4954 error_init ("initialization of flexible array member in a nested context");
4955 else if (pedantic)
4956 pedwarn_init ("initialization of a flexible array member");
4958 /* We have already issued an error message for the existence
4959 of a flexible array member not at the end of the structure.
4960 Discard the initializer so that we do not abort later. */
4961 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4962 constructor_type = NULL_TREE;
4966 /* Warn when some struct elements are implicitly initialized to zero. */
4967 if (warn_missing_field_initializers
4968 && constructor_type
4969 && TREE_CODE (constructor_type) == RECORD_TYPE
4970 && constructor_unfilled_fields)
4972 /* Do not warn for flexible array members or zero-length arrays. */
4973 while (constructor_unfilled_fields
4974 && (!DECL_SIZE (constructor_unfilled_fields)
4975 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4976 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4978 /* Do not warn if this level of the initializer uses member
4979 designators; it is likely to be deliberate. */
4980 if (constructor_unfilled_fields && !constructor_designated)
4982 push_member_name (constructor_unfilled_fields);
4983 warning_init ("missing initializer");
4984 RESTORE_SPELLING_DEPTH (constructor_depth);
4988 /* Pad out the end of the structure. */
4989 if (p->replacement_value.value)
4990 /* If this closes a superfluous brace pair,
4991 just pass out the element between them. */
4992 ret = p->replacement_value;
4993 else if (constructor_type == 0)
4995 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4996 && TREE_CODE (constructor_type) != UNION_TYPE
4997 && TREE_CODE (constructor_type) != ARRAY_TYPE
4998 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5000 /* A nonincremental scalar initializer--just return
5001 the element, after verifying there is just one. */
5002 if (constructor_elements == 0)
5004 if (!constructor_erroneous)
5005 error_init ("empty scalar initializer");
5006 ret.value = error_mark_node;
5008 else if (TREE_CHAIN (constructor_elements) != 0)
5010 error_init ("extra elements in scalar initializer");
5011 ret.value = TREE_VALUE (constructor_elements);
5013 else
5014 ret.value = TREE_VALUE (constructor_elements);
5016 else
5018 if (constructor_erroneous)
5019 ret.value = error_mark_node;
5020 else
5022 ret.value = build_constructor (constructor_type,
5023 nreverse (constructor_elements));
5024 if (constructor_constant)
5025 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5026 if (constructor_constant && constructor_simple)
5027 TREE_STATIC (ret.value) = 1;
5031 constructor_type = p->type;
5032 constructor_fields = p->fields;
5033 constructor_index = p->index;
5034 constructor_max_index = p->max_index;
5035 constructor_unfilled_index = p->unfilled_index;
5036 constructor_unfilled_fields = p->unfilled_fields;
5037 constructor_bit_index = p->bit_index;
5038 constructor_elements = p->elements;
5039 constructor_constant = p->constant;
5040 constructor_simple = p->simple;
5041 constructor_erroneous = p->erroneous;
5042 constructor_incremental = p->incremental;
5043 constructor_designated = p->designated;
5044 constructor_pending_elts = p->pending_elts;
5045 constructor_depth = p->depth;
5046 if (!p->implicit)
5047 constructor_range_stack = p->range_stack;
5048 RESTORE_SPELLING_DEPTH (constructor_depth);
5050 constructor_stack = p->next;
5051 free (p);
5053 if (ret.value == 0)
5055 if (constructor_stack == 0)
5057 ret.value = error_mark_node;
5058 return ret;
5060 return ret;
5062 return ret;
5065 /* Common handling for both array range and field name designators.
5066 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5068 static int
5069 set_designator (int array)
5071 tree subtype;
5072 enum tree_code subcode;
5074 /* Don't die if an entire brace-pair level is superfluous
5075 in the containing level. */
5076 if (constructor_type == 0)
5077 return 1;
5079 /* If there were errors in this designator list already, bail out
5080 silently. */
5081 if (designator_errorneous)
5082 return 1;
5084 if (!designator_depth)
5086 gcc_assert (!constructor_range_stack);
5088 /* Designator list starts at the level of closest explicit
5089 braces. */
5090 while (constructor_stack->implicit)
5091 process_init_element (pop_init_level (1));
5092 constructor_designated = 1;
5093 return 0;
5096 switch (TREE_CODE (constructor_type))
5098 case RECORD_TYPE:
5099 case UNION_TYPE:
5100 subtype = TREE_TYPE (constructor_fields);
5101 if (subtype != error_mark_node)
5102 subtype = TYPE_MAIN_VARIANT (subtype);
5103 break;
5104 case ARRAY_TYPE:
5105 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5106 break;
5107 default:
5108 gcc_unreachable ();
5111 subcode = TREE_CODE (subtype);
5112 if (array && subcode != ARRAY_TYPE)
5114 error_init ("array index in non-array initializer");
5115 return 1;
5117 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5119 error_init ("field name not in record or union initializer");
5120 return 1;
5123 constructor_designated = 1;
5124 push_init_level (2);
5125 return 0;
5128 /* If there are range designators in designator list, push a new designator
5129 to constructor_range_stack. RANGE_END is end of such stack range or
5130 NULL_TREE if there is no range designator at this level. */
5132 static void
5133 push_range_stack (tree range_end)
5135 struct constructor_range_stack *p;
5137 p = GGC_NEW (struct constructor_range_stack);
5138 p->prev = constructor_range_stack;
5139 p->next = 0;
5140 p->fields = constructor_fields;
5141 p->range_start = constructor_index;
5142 p->index = constructor_index;
5143 p->stack = constructor_stack;
5144 p->range_end = range_end;
5145 if (constructor_range_stack)
5146 constructor_range_stack->next = p;
5147 constructor_range_stack = p;
5150 /* Within an array initializer, specify the next index to be initialized.
5151 FIRST is that index. If LAST is nonzero, then initialize a range
5152 of indices, running from FIRST through LAST. */
5154 void
5155 set_init_index (tree first, tree last)
5157 if (set_designator (1))
5158 return;
5160 designator_errorneous = 1;
5162 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5163 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5165 error_init ("array index in initializer not of integer type");
5166 return;
5169 while ((TREE_CODE (first) == NOP_EXPR
5170 || TREE_CODE (first) == CONVERT_EXPR
5171 || TREE_CODE (first) == NON_LVALUE_EXPR)
5172 && (TYPE_MODE (TREE_TYPE (first))
5173 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5174 first = TREE_OPERAND (first, 0);
5176 if (last)
5177 while ((TREE_CODE (last) == NOP_EXPR
5178 || TREE_CODE (last) == CONVERT_EXPR
5179 || TREE_CODE (last) == NON_LVALUE_EXPR)
5180 && (TYPE_MODE (TREE_TYPE (last))
5181 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5182 last = TREE_OPERAND (last, 0);
5184 if (TREE_CODE (first) != INTEGER_CST)
5185 error_init ("nonconstant array index in initializer");
5186 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5187 error_init ("nonconstant array index in initializer");
5188 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5189 error_init ("array index in non-array initializer");
5190 else if (tree_int_cst_sgn (first) == -1)
5191 error_init ("array index in initializer exceeds array bounds");
5192 else if (constructor_max_index
5193 && tree_int_cst_lt (constructor_max_index, first))
5194 error_init ("array index in initializer exceeds array bounds");
5195 else
5197 constructor_index = convert (bitsizetype, first);
5199 if (last)
5201 if (tree_int_cst_equal (first, last))
5202 last = 0;
5203 else if (tree_int_cst_lt (last, first))
5205 error_init ("empty index range in initializer");
5206 last = 0;
5208 else
5210 last = convert (bitsizetype, last);
5211 if (constructor_max_index != 0
5212 && tree_int_cst_lt (constructor_max_index, last))
5214 error_init ("array index range in initializer exceeds array bounds");
5215 last = 0;
5220 designator_depth++;
5221 designator_errorneous = 0;
5222 if (constructor_range_stack || last)
5223 push_range_stack (last);
5227 /* Within a struct initializer, specify the next field to be initialized. */
5229 void
5230 set_init_label (tree fieldname)
5232 tree tail;
5234 if (set_designator (0))
5235 return;
5237 designator_errorneous = 1;
5239 if (TREE_CODE (constructor_type) != RECORD_TYPE
5240 && TREE_CODE (constructor_type) != UNION_TYPE)
5242 error_init ("field name not in record or union initializer");
5243 return;
5246 for (tail = TYPE_FIELDS (constructor_type); tail;
5247 tail = TREE_CHAIN (tail))
5249 if (DECL_NAME (tail) == fieldname)
5250 break;
5253 if (tail == 0)
5254 error ("unknown field %qs specified in initializer",
5255 IDENTIFIER_POINTER (fieldname));
5256 else
5258 constructor_fields = tail;
5259 designator_depth++;
5260 designator_errorneous = 0;
5261 if (constructor_range_stack)
5262 push_range_stack (NULL_TREE);
5266 /* Add a new initializer to the tree of pending initializers. PURPOSE
5267 identifies the initializer, either array index or field in a structure.
5268 VALUE is the value of that index or field. */
5270 static void
5271 add_pending_init (tree purpose, tree value)
5273 struct init_node *p, **q, *r;
5275 q = &constructor_pending_elts;
5276 p = 0;
5278 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5280 while (*q != 0)
5282 p = *q;
5283 if (tree_int_cst_lt (purpose, p->purpose))
5284 q = &p->left;
5285 else if (tree_int_cst_lt (p->purpose, purpose))
5286 q = &p->right;
5287 else
5289 if (TREE_SIDE_EFFECTS (p->value))
5290 warning_init ("initialized field with side-effects overwritten");
5291 p->value = value;
5292 return;
5296 else
5298 tree bitpos;
5300 bitpos = bit_position (purpose);
5301 while (*q != NULL)
5303 p = *q;
5304 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5305 q = &p->left;
5306 else if (p->purpose != purpose)
5307 q = &p->right;
5308 else
5310 if (TREE_SIDE_EFFECTS (p->value))
5311 warning_init ("initialized field with side-effects overwritten");
5312 p->value = value;
5313 return;
5318 r = GGC_NEW (struct init_node);
5319 r->purpose = purpose;
5320 r->value = value;
5322 *q = r;
5323 r->parent = p;
5324 r->left = 0;
5325 r->right = 0;
5326 r->balance = 0;
5328 while (p)
5330 struct init_node *s;
5332 if (r == p->left)
5334 if (p->balance == 0)
5335 p->balance = -1;
5336 else if (p->balance < 0)
5338 if (r->balance < 0)
5340 /* L rotation. */
5341 p->left = r->right;
5342 if (p->left)
5343 p->left->parent = p;
5344 r->right = p;
5346 p->balance = 0;
5347 r->balance = 0;
5349 s = p->parent;
5350 p->parent = r;
5351 r->parent = s;
5352 if (s)
5354 if (s->left == p)
5355 s->left = r;
5356 else
5357 s->right = r;
5359 else
5360 constructor_pending_elts = r;
5362 else
5364 /* LR rotation. */
5365 struct init_node *t = r->right;
5367 r->right = t->left;
5368 if (r->right)
5369 r->right->parent = r;
5370 t->left = r;
5372 p->left = t->right;
5373 if (p->left)
5374 p->left->parent = p;
5375 t->right = p;
5377 p->balance = t->balance < 0;
5378 r->balance = -(t->balance > 0);
5379 t->balance = 0;
5381 s = p->parent;
5382 p->parent = t;
5383 r->parent = t;
5384 t->parent = s;
5385 if (s)
5387 if (s->left == p)
5388 s->left = t;
5389 else
5390 s->right = t;
5392 else
5393 constructor_pending_elts = t;
5395 break;
5397 else
5399 /* p->balance == +1; growth of left side balances the node. */
5400 p->balance = 0;
5401 break;
5404 else /* r == p->right */
5406 if (p->balance == 0)
5407 /* Growth propagation from right side. */
5408 p->balance++;
5409 else if (p->balance > 0)
5411 if (r->balance > 0)
5413 /* R rotation. */
5414 p->right = r->left;
5415 if (p->right)
5416 p->right->parent = p;
5417 r->left = p;
5419 p->balance = 0;
5420 r->balance = 0;
5422 s = p->parent;
5423 p->parent = r;
5424 r->parent = s;
5425 if (s)
5427 if (s->left == p)
5428 s->left = r;
5429 else
5430 s->right = r;
5432 else
5433 constructor_pending_elts = r;
5435 else /* r->balance == -1 */
5437 /* RL rotation */
5438 struct init_node *t = r->left;
5440 r->left = t->right;
5441 if (r->left)
5442 r->left->parent = r;
5443 t->right = r;
5445 p->right = t->left;
5446 if (p->right)
5447 p->right->parent = p;
5448 t->left = p;
5450 r->balance = (t->balance < 0);
5451 p->balance = -(t->balance > 0);
5452 t->balance = 0;
5454 s = p->parent;
5455 p->parent = t;
5456 r->parent = t;
5457 t->parent = s;
5458 if (s)
5460 if (s->left == p)
5461 s->left = t;
5462 else
5463 s->right = t;
5465 else
5466 constructor_pending_elts = t;
5468 break;
5470 else
5472 /* p->balance == -1; growth of right side balances the node. */
5473 p->balance = 0;
5474 break;
5478 r = p;
5479 p = p->parent;
5483 /* Build AVL tree from a sorted chain. */
5485 static void
5486 set_nonincremental_init (void)
5488 tree chain;
5490 if (TREE_CODE (constructor_type) != RECORD_TYPE
5491 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5492 return;
5494 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5495 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5496 constructor_elements = 0;
5497 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5499 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5500 /* Skip any nameless bit fields at the beginning. */
5501 while (constructor_unfilled_fields != 0
5502 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5503 && DECL_NAME (constructor_unfilled_fields) == 0)
5504 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5507 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5509 if (TYPE_DOMAIN (constructor_type))
5510 constructor_unfilled_index
5511 = convert (bitsizetype,
5512 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5513 else
5514 constructor_unfilled_index = bitsize_zero_node;
5516 constructor_incremental = 0;
5519 /* Build AVL tree from a string constant. */
5521 static void
5522 set_nonincremental_init_from_string (tree str)
5524 tree value, purpose, type;
5525 HOST_WIDE_INT val[2];
5526 const char *p, *end;
5527 int byte, wchar_bytes, charwidth, bitpos;
5529 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5531 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5532 == TYPE_PRECISION (char_type_node))
5533 wchar_bytes = 1;
5534 else
5536 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5537 == TYPE_PRECISION (wchar_type_node));
5538 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5540 charwidth = TYPE_PRECISION (char_type_node);
5541 type = TREE_TYPE (constructor_type);
5542 p = TREE_STRING_POINTER (str);
5543 end = p + TREE_STRING_LENGTH (str);
5545 for (purpose = bitsize_zero_node;
5546 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5547 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5549 if (wchar_bytes == 1)
5551 val[1] = (unsigned char) *p++;
5552 val[0] = 0;
5554 else
5556 val[0] = 0;
5557 val[1] = 0;
5558 for (byte = 0; byte < wchar_bytes; byte++)
5560 if (BYTES_BIG_ENDIAN)
5561 bitpos = (wchar_bytes - byte - 1) * charwidth;
5562 else
5563 bitpos = byte * charwidth;
5564 val[bitpos < HOST_BITS_PER_WIDE_INT]
5565 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5566 << (bitpos % HOST_BITS_PER_WIDE_INT);
5570 if (!TYPE_UNSIGNED (type))
5572 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5573 if (bitpos < HOST_BITS_PER_WIDE_INT)
5575 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5577 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5578 val[0] = -1;
5581 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5583 if (val[1] < 0)
5584 val[0] = -1;
5586 else if (val[0] & (((HOST_WIDE_INT) 1)
5587 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5588 val[0] |= ((HOST_WIDE_INT) -1)
5589 << (bitpos - HOST_BITS_PER_WIDE_INT);
5592 value = build_int_cst_wide (type, val[1], val[0]);
5593 add_pending_init (purpose, value);
5596 constructor_incremental = 0;
5599 /* Return value of FIELD in pending initializer or zero if the field was
5600 not initialized yet. */
5602 static tree
5603 find_init_member (tree field)
5605 struct init_node *p;
5607 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5609 if (constructor_incremental
5610 && tree_int_cst_lt (field, constructor_unfilled_index))
5611 set_nonincremental_init ();
5613 p = constructor_pending_elts;
5614 while (p)
5616 if (tree_int_cst_lt (field, p->purpose))
5617 p = p->left;
5618 else if (tree_int_cst_lt (p->purpose, field))
5619 p = p->right;
5620 else
5621 return p->value;
5624 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5626 tree bitpos = bit_position (field);
5628 if (constructor_incremental
5629 && (!constructor_unfilled_fields
5630 || tree_int_cst_lt (bitpos,
5631 bit_position (constructor_unfilled_fields))))
5632 set_nonincremental_init ();
5634 p = constructor_pending_elts;
5635 while (p)
5637 if (field == p->purpose)
5638 return p->value;
5639 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5640 p = p->left;
5641 else
5642 p = p->right;
5645 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5647 if (constructor_elements
5648 && TREE_PURPOSE (constructor_elements) == field)
5649 return TREE_VALUE (constructor_elements);
5651 return 0;
5654 /* "Output" the next constructor element.
5655 At top level, really output it to assembler code now.
5656 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5657 TYPE is the data type that the containing data type wants here.
5658 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5659 If VALUE is a string constant, STRICT_STRING is true if it is
5660 unparenthesized or we should not warn here for it being parenthesized.
5661 For other types of VALUE, STRICT_STRING is not used.
5663 PENDING if non-nil means output pending elements that belong
5664 right after this element. (PENDING is normally 1;
5665 it is 0 while outputting pending elements, to avoid recursion.) */
5667 static void
5668 output_init_element (tree value, bool strict_string, tree type, tree field,
5669 int pending)
5671 if (type == error_mark_node || value == error_mark_node)
5673 constructor_erroneous = 1;
5674 return;
5676 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5677 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5678 && !(TREE_CODE (value) == STRING_CST
5679 && TREE_CODE (type) == ARRAY_TYPE
5680 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5681 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5682 TYPE_MAIN_VARIANT (type))))
5683 value = default_conversion (value);
5685 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5686 && require_constant_value && !flag_isoc99 && pending)
5688 /* As an extension, allow initializing objects with static storage
5689 duration with compound literals (which are then treated just as
5690 the brace enclosed list they contain). */
5691 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5692 value = DECL_INITIAL (decl);
5695 if (value == error_mark_node)
5696 constructor_erroneous = 1;
5697 else if (!TREE_CONSTANT (value))
5698 constructor_constant = 0;
5699 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5700 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5701 || TREE_CODE (constructor_type) == UNION_TYPE)
5702 && DECL_C_BIT_FIELD (field)
5703 && TREE_CODE (value) != INTEGER_CST))
5704 constructor_simple = 0;
5706 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5708 if (require_constant_value)
5710 error_init ("initializer element is not constant");
5711 value = error_mark_node;
5713 else if (require_constant_elements)
5714 pedwarn ("initializer element is not computable at load time");
5717 /* If this field is empty (and not at the end of structure),
5718 don't do anything other than checking the initializer. */
5719 if (field
5720 && (TREE_TYPE (field) == error_mark_node
5721 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5722 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5723 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5724 || TREE_CHAIN (field)))))
5725 return;
5727 value = digest_init (type, value, strict_string, require_constant_value);
5728 if (value == error_mark_node)
5730 constructor_erroneous = 1;
5731 return;
5734 /* If this element doesn't come next in sequence,
5735 put it on constructor_pending_elts. */
5736 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5737 && (!constructor_incremental
5738 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5740 if (constructor_incremental
5741 && tree_int_cst_lt (field, constructor_unfilled_index))
5742 set_nonincremental_init ();
5744 add_pending_init (field, value);
5745 return;
5747 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5748 && (!constructor_incremental
5749 || field != constructor_unfilled_fields))
5751 /* We do this for records but not for unions. In a union,
5752 no matter which field is specified, it can be initialized
5753 right away since it starts at the beginning of the union. */
5754 if (constructor_incremental)
5756 if (!constructor_unfilled_fields)
5757 set_nonincremental_init ();
5758 else
5760 tree bitpos, unfillpos;
5762 bitpos = bit_position (field);
5763 unfillpos = bit_position (constructor_unfilled_fields);
5765 if (tree_int_cst_lt (bitpos, unfillpos))
5766 set_nonincremental_init ();
5770 add_pending_init (field, value);
5771 return;
5773 else if (TREE_CODE (constructor_type) == UNION_TYPE
5774 && constructor_elements)
5776 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5777 warning_init ("initialized field with side-effects overwritten");
5779 /* We can have just one union field set. */
5780 constructor_elements = 0;
5783 /* Otherwise, output this element either to
5784 constructor_elements or to the assembler file. */
5786 if (field && TREE_CODE (field) == INTEGER_CST)
5787 field = copy_node (field);
5788 constructor_elements
5789 = tree_cons (field, value, constructor_elements);
5791 /* Advance the variable that indicates sequential elements output. */
5792 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5793 constructor_unfilled_index
5794 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5795 bitsize_one_node);
5796 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5798 constructor_unfilled_fields
5799 = TREE_CHAIN (constructor_unfilled_fields);
5801 /* Skip any nameless bit fields. */
5802 while (constructor_unfilled_fields != 0
5803 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5804 && DECL_NAME (constructor_unfilled_fields) == 0)
5805 constructor_unfilled_fields =
5806 TREE_CHAIN (constructor_unfilled_fields);
5808 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5809 constructor_unfilled_fields = 0;
5811 /* Now output any pending elements which have become next. */
5812 if (pending)
5813 output_pending_init_elements (0);
5816 /* Output any pending elements which have become next.
5817 As we output elements, constructor_unfilled_{fields,index}
5818 advances, which may cause other elements to become next;
5819 if so, they too are output.
5821 If ALL is 0, we return when there are
5822 no more pending elements to output now.
5824 If ALL is 1, we output space as necessary so that
5825 we can output all the pending elements. */
5827 static void
5828 output_pending_init_elements (int all)
5830 struct init_node *elt = constructor_pending_elts;
5831 tree next;
5833 retry:
5835 /* Look through the whole pending tree.
5836 If we find an element that should be output now,
5837 output it. Otherwise, set NEXT to the element
5838 that comes first among those still pending. */
5840 next = 0;
5841 while (elt)
5843 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5845 if (tree_int_cst_equal (elt->purpose,
5846 constructor_unfilled_index))
5847 output_init_element (elt->value, true,
5848 TREE_TYPE (constructor_type),
5849 constructor_unfilled_index, 0);
5850 else if (tree_int_cst_lt (constructor_unfilled_index,
5851 elt->purpose))
5853 /* Advance to the next smaller node. */
5854 if (elt->left)
5855 elt = elt->left;
5856 else
5858 /* We have reached the smallest node bigger than the
5859 current unfilled index. Fill the space first. */
5860 next = elt->purpose;
5861 break;
5864 else
5866 /* Advance to the next bigger node. */
5867 if (elt->right)
5868 elt = elt->right;
5869 else
5871 /* We have reached the biggest node in a subtree. Find
5872 the parent of it, which is the next bigger node. */
5873 while (elt->parent && elt->parent->right == elt)
5874 elt = elt->parent;
5875 elt = elt->parent;
5876 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5877 elt->purpose))
5879 next = elt->purpose;
5880 break;
5885 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5886 || TREE_CODE (constructor_type) == UNION_TYPE)
5888 tree ctor_unfilled_bitpos, elt_bitpos;
5890 /* If the current record is complete we are done. */
5891 if (constructor_unfilled_fields == 0)
5892 break;
5894 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5895 elt_bitpos = bit_position (elt->purpose);
5896 /* We can't compare fields here because there might be empty
5897 fields in between. */
5898 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5900 constructor_unfilled_fields = elt->purpose;
5901 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5902 elt->purpose, 0);
5904 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5906 /* Advance to the next smaller node. */
5907 if (elt->left)
5908 elt = elt->left;
5909 else
5911 /* We have reached the smallest node bigger than the
5912 current unfilled field. Fill the space first. */
5913 next = elt->purpose;
5914 break;
5917 else
5919 /* Advance to the next bigger node. */
5920 if (elt->right)
5921 elt = elt->right;
5922 else
5924 /* We have reached the biggest node in a subtree. Find
5925 the parent of it, which is the next bigger node. */
5926 while (elt->parent && elt->parent->right == elt)
5927 elt = elt->parent;
5928 elt = elt->parent;
5929 if (elt
5930 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5931 bit_position (elt->purpose))))
5933 next = elt->purpose;
5934 break;
5941 /* Ordinarily return, but not if we want to output all
5942 and there are elements left. */
5943 if (!(all && next != 0))
5944 return;
5946 /* If it's not incremental, just skip over the gap, so that after
5947 jumping to retry we will output the next successive element. */
5948 if (TREE_CODE (constructor_type) == RECORD_TYPE
5949 || TREE_CODE (constructor_type) == UNION_TYPE)
5950 constructor_unfilled_fields = next;
5951 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5952 constructor_unfilled_index = next;
5954 /* ELT now points to the node in the pending tree with the next
5955 initializer to output. */
5956 goto retry;
5959 /* Add one non-braced element to the current constructor level.
5960 This adjusts the current position within the constructor's type.
5961 This may also start or terminate implicit levels
5962 to handle a partly-braced initializer.
5964 Once this has found the correct level for the new element,
5965 it calls output_init_element. */
5967 void
5968 process_init_element (struct c_expr value)
5970 tree orig_value = value.value;
5971 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
5972 bool strict_string = value.original_code == STRING_CST;
5974 designator_depth = 0;
5975 designator_errorneous = 0;
5977 /* Handle superfluous braces around string cst as in
5978 char x[] = {"foo"}; */
5979 if (string_flag
5980 && constructor_type
5981 && TREE_CODE (constructor_type) == ARRAY_TYPE
5982 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
5983 && integer_zerop (constructor_unfilled_index))
5985 if (constructor_stack->replacement_value.value)
5986 error_init ("excess elements in char array initializer");
5987 constructor_stack->replacement_value = value;
5988 return;
5991 if (constructor_stack->replacement_value.value != 0)
5993 error_init ("excess elements in struct initializer");
5994 return;
5997 /* Ignore elements of a brace group if it is entirely superfluous
5998 and has already been diagnosed. */
5999 if (constructor_type == 0)
6000 return;
6002 /* If we've exhausted any levels that didn't have braces,
6003 pop them now. */
6004 while (constructor_stack->implicit)
6006 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6007 || TREE_CODE (constructor_type) == UNION_TYPE)
6008 && constructor_fields == 0)
6009 process_init_element (pop_init_level (1));
6010 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6011 && (constructor_max_index == 0
6012 || tree_int_cst_lt (constructor_max_index,
6013 constructor_index)))
6014 process_init_element (pop_init_level (1));
6015 else
6016 break;
6019 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6020 if (constructor_range_stack)
6022 /* If value is a compound literal and we'll be just using its
6023 content, don't put it into a SAVE_EXPR. */
6024 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6025 || !require_constant_value
6026 || flag_isoc99)
6027 value.value = save_expr (value.value);
6030 while (1)
6032 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6034 tree fieldtype;
6035 enum tree_code fieldcode;
6037 if (constructor_fields == 0)
6039 pedwarn_init ("excess elements in struct initializer");
6040 break;
6043 fieldtype = TREE_TYPE (constructor_fields);
6044 if (fieldtype != error_mark_node)
6045 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6046 fieldcode = TREE_CODE (fieldtype);
6048 /* Error for non-static initialization of a flexible array member. */
6049 if (fieldcode == ARRAY_TYPE
6050 && !require_constant_value
6051 && TYPE_SIZE (fieldtype) == NULL_TREE
6052 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6054 error_init ("non-static initialization of a flexible array member");
6055 break;
6058 /* Accept a string constant to initialize a subarray. */
6059 if (value.value != 0
6060 && fieldcode == ARRAY_TYPE
6061 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6062 && string_flag)
6063 value.value = orig_value;
6064 /* Otherwise, if we have come to a subaggregate,
6065 and we don't have an element of its type, push into it. */
6066 else if (value.value != 0
6067 && value.value != error_mark_node
6068 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6069 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6070 || fieldcode == UNION_TYPE))
6072 push_init_level (1);
6073 continue;
6076 if (value.value)
6078 push_member_name (constructor_fields);
6079 output_init_element (value.value, strict_string,
6080 fieldtype, constructor_fields, 1);
6081 RESTORE_SPELLING_DEPTH (constructor_depth);
6083 else
6084 /* Do the bookkeeping for an element that was
6085 directly output as a constructor. */
6087 /* For a record, keep track of end position of last field. */
6088 if (DECL_SIZE (constructor_fields))
6089 constructor_bit_index
6090 = size_binop (PLUS_EXPR,
6091 bit_position (constructor_fields),
6092 DECL_SIZE (constructor_fields));
6094 /* If the current field was the first one not yet written out,
6095 it isn't now, so update. */
6096 if (constructor_unfilled_fields == constructor_fields)
6098 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6099 /* Skip any nameless bit fields. */
6100 while (constructor_unfilled_fields != 0
6101 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6102 && DECL_NAME (constructor_unfilled_fields) == 0)
6103 constructor_unfilled_fields =
6104 TREE_CHAIN (constructor_unfilled_fields);
6108 constructor_fields = TREE_CHAIN (constructor_fields);
6109 /* Skip any nameless bit fields at the beginning. */
6110 while (constructor_fields != 0
6111 && DECL_C_BIT_FIELD (constructor_fields)
6112 && DECL_NAME (constructor_fields) == 0)
6113 constructor_fields = TREE_CHAIN (constructor_fields);
6115 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6117 tree fieldtype;
6118 enum tree_code fieldcode;
6120 if (constructor_fields == 0)
6122 pedwarn_init ("excess elements in union initializer");
6123 break;
6126 fieldtype = TREE_TYPE (constructor_fields);
6127 if (fieldtype != error_mark_node)
6128 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6129 fieldcode = TREE_CODE (fieldtype);
6131 /* Warn that traditional C rejects initialization of unions.
6132 We skip the warning if the value is zero. This is done
6133 under the assumption that the zero initializer in user
6134 code appears conditioned on e.g. __STDC__ to avoid
6135 "missing initializer" warnings and relies on default
6136 initialization to zero in the traditional C case.
6137 We also skip the warning if the initializer is designated,
6138 again on the assumption that this must be conditional on
6139 __STDC__ anyway (and we've already complained about the
6140 member-designator already). */
6141 if (warn_traditional && !in_system_header && !constructor_designated
6142 && !(value.value && (integer_zerop (value.value)
6143 || real_zerop (value.value))))
6144 warning ("traditional C rejects initialization of unions");
6146 /* Accept a string constant to initialize a subarray. */
6147 if (value.value != 0
6148 && fieldcode == ARRAY_TYPE
6149 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6150 && string_flag)
6151 value.value = orig_value;
6152 /* Otherwise, if we have come to a subaggregate,
6153 and we don't have an element of its type, push into it. */
6154 else if (value.value != 0
6155 && value.value != error_mark_node
6156 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6157 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6158 || fieldcode == UNION_TYPE))
6160 push_init_level (1);
6161 continue;
6164 if (value.value)
6166 push_member_name (constructor_fields);
6167 output_init_element (value.value, strict_string,
6168 fieldtype, constructor_fields, 1);
6169 RESTORE_SPELLING_DEPTH (constructor_depth);
6171 else
6172 /* Do the bookkeeping for an element that was
6173 directly output as a constructor. */
6175 constructor_bit_index = DECL_SIZE (constructor_fields);
6176 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6179 constructor_fields = 0;
6181 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6183 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6184 enum tree_code eltcode = TREE_CODE (elttype);
6186 /* Accept a string constant to initialize a subarray. */
6187 if (value.value != 0
6188 && eltcode == ARRAY_TYPE
6189 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6190 && string_flag)
6191 value.value = orig_value;
6192 /* Otherwise, if we have come to a subaggregate,
6193 and we don't have an element of its type, push into it. */
6194 else if (value.value != 0
6195 && value.value != error_mark_node
6196 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6197 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6198 || eltcode == UNION_TYPE))
6200 push_init_level (1);
6201 continue;
6204 if (constructor_max_index != 0
6205 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6206 || integer_all_onesp (constructor_max_index)))
6208 pedwarn_init ("excess elements in array initializer");
6209 break;
6212 /* Now output the actual element. */
6213 if (value.value)
6215 push_array_bounds (tree_low_cst (constructor_index, 0));
6216 output_init_element (value.value, strict_string,
6217 elttype, constructor_index, 1);
6218 RESTORE_SPELLING_DEPTH (constructor_depth);
6221 constructor_index
6222 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6224 if (!value.value)
6225 /* If we are doing the bookkeeping for an element that was
6226 directly output as a constructor, we must update
6227 constructor_unfilled_index. */
6228 constructor_unfilled_index = constructor_index;
6230 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6232 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6234 /* Do a basic check of initializer size. Note that vectors
6235 always have a fixed size derived from their type. */
6236 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6238 pedwarn_init ("excess elements in vector initializer");
6239 break;
6242 /* Now output the actual element. */
6243 if (value.value)
6244 output_init_element (value.value, strict_string,
6245 elttype, constructor_index, 1);
6247 constructor_index
6248 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6250 if (!value.value)
6251 /* If we are doing the bookkeeping for an element that was
6252 directly output as a constructor, we must update
6253 constructor_unfilled_index. */
6254 constructor_unfilled_index = constructor_index;
6257 /* Handle the sole element allowed in a braced initializer
6258 for a scalar variable. */
6259 else if (constructor_type != error_mark_node
6260 && constructor_fields == 0)
6262 pedwarn_init ("excess elements in scalar initializer");
6263 break;
6265 else
6267 if (value.value)
6268 output_init_element (value.value, strict_string,
6269 constructor_type, NULL_TREE, 1);
6270 constructor_fields = 0;
6273 /* Handle range initializers either at this level or anywhere higher
6274 in the designator stack. */
6275 if (constructor_range_stack)
6277 struct constructor_range_stack *p, *range_stack;
6278 int finish = 0;
6280 range_stack = constructor_range_stack;
6281 constructor_range_stack = 0;
6282 while (constructor_stack != range_stack->stack)
6284 gcc_assert (constructor_stack->implicit);
6285 process_init_element (pop_init_level (1));
6287 for (p = range_stack;
6288 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6289 p = p->prev)
6291 gcc_assert (constructor_stack->implicit);
6292 process_init_element (pop_init_level (1));
6295 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6296 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6297 finish = 1;
6299 while (1)
6301 constructor_index = p->index;
6302 constructor_fields = p->fields;
6303 if (finish && p->range_end && p->index == p->range_start)
6305 finish = 0;
6306 p->prev = 0;
6308 p = p->next;
6309 if (!p)
6310 break;
6311 push_init_level (2);
6312 p->stack = constructor_stack;
6313 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6314 p->index = p->range_start;
6317 if (!finish)
6318 constructor_range_stack = range_stack;
6319 continue;
6322 break;
6325 constructor_range_stack = 0;
6328 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6329 (guaranteed to be 'volatile' or null) and ARGS (represented using
6330 an ASM_EXPR node). */
6331 tree
6332 build_asm_stmt (tree cv_qualifier, tree args)
6334 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6335 ASM_VOLATILE_P (args) = 1;
6336 return add_stmt (args);
6339 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6340 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6341 SIMPLE indicates whether there was anything at all after the
6342 string in the asm expression -- asm("blah") and asm("blah" : )
6343 are subtly different. We use a ASM_EXPR node to represent this. */
6344 tree
6345 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6346 bool simple)
6348 tree tail;
6349 tree args;
6350 int i;
6351 const char *constraint;
6352 const char **oconstraints;
6353 bool allows_mem, allows_reg, is_inout;
6354 int ninputs, noutputs;
6356 ninputs = list_length (inputs);
6357 noutputs = list_length (outputs);
6358 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6360 string = resolve_asm_operand_names (string, outputs, inputs);
6362 /* Remove output conversions that change the type but not the mode. */
6363 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6365 tree output = TREE_VALUE (tail);
6367 /* ??? Really, this should not be here. Users should be using a
6368 proper lvalue, dammit. But there's a long history of using casts
6369 in the output operands. In cases like longlong.h, this becomes a
6370 primitive form of typechecking -- if the cast can be removed, then
6371 the output operand had a type of the proper width; otherwise we'll
6372 get an error. Gross, but ... */
6373 STRIP_NOPS (output);
6375 if (!lvalue_or_else (output, lv_asm))
6376 output = error_mark_node;
6378 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6379 oconstraints[i] = constraint;
6381 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6382 &allows_mem, &allows_reg, &is_inout))
6384 /* If the operand is going to end up in memory,
6385 mark it addressable. */
6386 if (!allows_reg && !c_mark_addressable (output))
6387 output = error_mark_node;
6389 else
6390 output = error_mark_node;
6392 TREE_VALUE (tail) = output;
6395 /* Perform default conversions on array and function inputs.
6396 Don't do this for other types as it would screw up operands
6397 expected to be in memory. */
6398 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6400 tree input;
6402 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6403 input = TREE_VALUE (tail);
6405 input = default_function_array_conversion (input);
6407 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6408 oconstraints, &allows_mem, &allows_reg))
6410 /* If the operand is going to end up in memory,
6411 mark it addressable. */
6412 if (!allows_reg && allows_mem)
6414 /* Strip the nops as we allow this case. FIXME, this really
6415 should be rejected or made deprecated. */
6416 STRIP_NOPS (input);
6417 if (!c_mark_addressable (input))
6418 input = error_mark_node;
6421 else
6422 input = error_mark_node;
6424 TREE_VALUE (tail) = input;
6427 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6429 /* Simple asm statements are treated as volatile. */
6430 if (simple)
6432 ASM_VOLATILE_P (args) = 1;
6433 ASM_INPUT_P (args) = 1;
6436 return args;
6439 /* Generate a goto statement to LABEL. */
6441 tree
6442 c_finish_goto_label (tree label)
6444 tree decl = lookup_label (label);
6445 if (!decl)
6446 return NULL_TREE;
6448 TREE_USED (decl) = 1;
6449 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6452 /* Generate a computed goto statement to EXPR. */
6454 tree
6455 c_finish_goto_ptr (tree expr)
6457 if (pedantic)
6458 pedwarn ("ISO C forbids %<goto *expr;%>");
6459 expr = convert (ptr_type_node, expr);
6460 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6463 /* Generate a C `return' statement. RETVAL is the expression for what
6464 to return, or a null pointer for `return;' with no value. */
6466 tree
6467 c_finish_return (tree retval)
6469 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6471 if (TREE_THIS_VOLATILE (current_function_decl))
6472 warning ("function declared %<noreturn%> has a %<return%> statement");
6474 if (!retval)
6476 current_function_returns_null = 1;
6477 if ((warn_return_type || flag_isoc99)
6478 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6479 pedwarn_c99 ("%<return%> with no value, in "
6480 "function returning non-void");
6482 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6484 current_function_returns_null = 1;
6485 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6486 pedwarn ("%<return%> with a value, in function returning void");
6488 else
6490 tree t = convert_for_assignment (valtype, retval, ic_return,
6491 NULL_TREE, NULL_TREE, 0);
6492 tree res = DECL_RESULT (current_function_decl);
6493 tree inner;
6495 current_function_returns_value = 1;
6496 if (t == error_mark_node)
6497 return NULL_TREE;
6499 inner = t = convert (TREE_TYPE (res), t);
6501 /* Strip any conversions, additions, and subtractions, and see if
6502 we are returning the address of a local variable. Warn if so. */
6503 while (1)
6505 switch (TREE_CODE (inner))
6507 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6508 case PLUS_EXPR:
6509 inner = TREE_OPERAND (inner, 0);
6510 continue;
6512 case MINUS_EXPR:
6513 /* If the second operand of the MINUS_EXPR has a pointer
6514 type (or is converted from it), this may be valid, so
6515 don't give a warning. */
6517 tree op1 = TREE_OPERAND (inner, 1);
6519 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6520 && (TREE_CODE (op1) == NOP_EXPR
6521 || TREE_CODE (op1) == NON_LVALUE_EXPR
6522 || TREE_CODE (op1) == CONVERT_EXPR))
6523 op1 = TREE_OPERAND (op1, 0);
6525 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6526 break;
6528 inner = TREE_OPERAND (inner, 0);
6529 continue;
6532 case ADDR_EXPR:
6533 inner = TREE_OPERAND (inner, 0);
6535 while (REFERENCE_CLASS_P (inner)
6536 && TREE_CODE (inner) != INDIRECT_REF)
6537 inner = TREE_OPERAND (inner, 0);
6539 if (DECL_P (inner)
6540 && !DECL_EXTERNAL (inner)
6541 && !TREE_STATIC (inner)
6542 && DECL_CONTEXT (inner) == current_function_decl)
6543 warning ("function returns address of local variable");
6544 break;
6546 default:
6547 break;
6550 break;
6553 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6556 return add_stmt (build_stmt (RETURN_EXPR, retval));
6559 struct c_switch {
6560 /* The SWITCH_STMT being built. */
6561 tree switch_stmt;
6563 /* The original type of the testing expression, i.e. before the
6564 default conversion is applied. */
6565 tree orig_type;
6567 /* A splay-tree mapping the low element of a case range to the high
6568 element, or NULL_TREE if there is no high element. Used to
6569 determine whether or not a new case label duplicates an old case
6570 label. We need a tree, rather than simply a hash table, because
6571 of the GNU case range extension. */
6572 splay_tree cases;
6574 /* The next node on the stack. */
6575 struct c_switch *next;
6578 /* A stack of the currently active switch statements. The innermost
6579 switch statement is on the top of the stack. There is no need to
6580 mark the stack for garbage collection because it is only active
6581 during the processing of the body of a function, and we never
6582 collect at that point. */
6584 struct c_switch *c_switch_stack;
6586 /* Start a C switch statement, testing expression EXP. Return the new
6587 SWITCH_STMT. */
6589 tree
6590 c_start_case (tree exp)
6592 enum tree_code code;
6593 tree type, orig_type = error_mark_node;
6594 struct c_switch *cs;
6596 if (exp != error_mark_node)
6598 code = TREE_CODE (TREE_TYPE (exp));
6599 orig_type = TREE_TYPE (exp);
6601 if (!INTEGRAL_TYPE_P (orig_type)
6602 && code != ERROR_MARK)
6604 error ("switch quantity not an integer");
6605 exp = integer_zero_node;
6606 orig_type = error_mark_node;
6608 else
6610 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6612 if (warn_traditional && !in_system_header
6613 && (type == long_integer_type_node
6614 || type == long_unsigned_type_node))
6615 warning ("%<long%> switch expression not converted to "
6616 "%<int%> in ISO C");
6618 exp = default_conversion (exp);
6619 type = TREE_TYPE (exp);
6623 /* Add this new SWITCH_STMT to the stack. */
6624 cs = XNEW (struct c_switch);
6625 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6626 cs->orig_type = orig_type;
6627 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6628 cs->next = c_switch_stack;
6629 c_switch_stack = cs;
6631 return add_stmt (cs->switch_stmt);
6634 /* Process a case label. */
6636 tree
6637 do_case (tree low_value, tree high_value)
6639 tree label = NULL_TREE;
6641 if (c_switch_stack)
6643 label = c_add_case_label (c_switch_stack->cases,
6644 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
6645 c_switch_stack->orig_type,
6646 low_value, high_value);
6647 if (label == error_mark_node)
6648 label = NULL_TREE;
6650 else if (low_value)
6651 error ("case label not within a switch statement");
6652 else
6653 error ("%<default%> label not within a switch statement");
6655 return label;
6658 /* Finish the switch statement. */
6660 void
6661 c_finish_case (tree body)
6663 struct c_switch *cs = c_switch_stack;
6665 SWITCH_STMT_BODY (cs->switch_stmt) = body;
6667 /* Emit warnings as needed. */
6668 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6670 /* Pop the stack. */
6671 c_switch_stack = cs->next;
6672 splay_tree_delete (cs->cases);
6673 XDELETE (cs);
6676 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6677 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6678 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6679 statement, and was not surrounded with parenthesis. */
6681 void
6682 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6683 tree else_block, bool nested_if)
6685 tree stmt;
6687 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6688 if (warn_parentheses && nested_if && else_block == NULL)
6690 tree inner_if = then_block;
6692 /* We know from the grammar productions that there is an IF nested
6693 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6694 it might not be exactly THEN_BLOCK, but should be the last
6695 non-container statement within. */
6696 while (1)
6697 switch (TREE_CODE (inner_if))
6699 case COND_EXPR:
6700 goto found;
6701 case BIND_EXPR:
6702 inner_if = BIND_EXPR_BODY (inner_if);
6703 break;
6704 case STATEMENT_LIST:
6705 inner_if = expr_last (then_block);
6706 break;
6707 case TRY_FINALLY_EXPR:
6708 case TRY_CATCH_EXPR:
6709 inner_if = TREE_OPERAND (inner_if, 0);
6710 break;
6711 default:
6712 gcc_unreachable ();
6714 found:
6716 if (COND_EXPR_ELSE (inner_if))
6717 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6718 &if_locus);
6721 /* Diagnose ";" via the special empty statement node that we create. */
6722 if (extra_warnings)
6724 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6726 if (!else_block)
6727 warning ("%Hempty body in an if-statement",
6728 EXPR_LOCUS (then_block));
6729 then_block = alloc_stmt_list ();
6731 if (else_block
6732 && TREE_CODE (else_block) == NOP_EXPR
6733 && !TREE_TYPE (else_block))
6735 warning ("%Hempty body in an else-statement",
6736 EXPR_LOCUS (else_block));
6737 else_block = alloc_stmt_list ();
6741 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6742 SET_EXPR_LOCATION (stmt, if_locus);
6743 add_stmt (stmt);
6746 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6747 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6748 is false for DO loops. INCR is the FOR increment expression. BODY is
6749 the statement controlled by the loop. BLAB is the break label. CLAB is
6750 the continue label. Everything is allowed to be NULL. */
6752 void
6753 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6754 tree blab, tree clab, bool cond_is_first)
6756 tree entry = NULL, exit = NULL, t;
6758 /* If the condition is zero don't generate a loop construct. */
6759 if (cond && integer_zerop (cond))
6761 if (cond_is_first)
6763 t = build_and_jump (&blab);
6764 SET_EXPR_LOCATION (t, start_locus);
6765 add_stmt (t);
6768 else
6770 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6772 /* If we have an exit condition, then we build an IF with gotos either
6773 out of the loop, or to the top of it. If there's no exit condition,
6774 then we just build a jump back to the top. */
6775 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6777 if (cond && !integer_nonzerop (cond))
6779 /* Canonicalize the loop condition to the end. This means
6780 generating a branch to the loop condition. Reuse the
6781 continue label, if possible. */
6782 if (cond_is_first)
6784 if (incr || !clab)
6786 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6787 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6789 else
6790 t = build1 (GOTO_EXPR, void_type_node, clab);
6791 SET_EXPR_LOCATION (t, start_locus);
6792 add_stmt (t);
6795 t = build_and_jump (&blab);
6796 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6797 exit = fold (exit);
6798 if (cond_is_first)
6799 SET_EXPR_LOCATION (exit, start_locus);
6800 else
6801 SET_EXPR_LOCATION (exit, input_location);
6804 add_stmt (top);
6807 if (body)
6808 add_stmt (body);
6809 if (clab)
6810 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6811 if (incr)
6812 add_stmt (incr);
6813 if (entry)
6814 add_stmt (entry);
6815 if (exit)
6816 add_stmt (exit);
6817 if (blab)
6818 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6821 tree
6822 c_finish_bc_stmt (tree *label_p, bool is_break)
6824 bool skip;
6825 tree label = *label_p;
6827 /* In switch statements break is sometimes stylistically used after
6828 a return statement. This can lead to spurious warnings about
6829 control reaching the end of a non-void function when it is
6830 inlined. Note that we are calling block_may_fallthru with
6831 language specific tree nodes; this works because
6832 block_may_fallthru returns true when given something it does not
6833 understand. */
6834 skip = !block_may_fallthru (cur_stmt_list);
6836 if (!label)
6838 if (!skip)
6839 *label_p = label = create_artificial_label ();
6841 else if (TREE_CODE (label) != LABEL_DECL)
6843 if (is_break)
6844 error ("break statement not within loop or switch");
6845 else
6846 error ("continue statement not within a loop");
6847 return NULL_TREE;
6850 if (skip)
6851 return NULL_TREE;
6853 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6856 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6858 static void
6859 emit_side_effect_warnings (tree expr)
6861 if (expr == error_mark_node)
6863 else if (!TREE_SIDE_EFFECTS (expr))
6865 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6866 warning ("%Hstatement with no effect",
6867 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6869 else if (warn_unused_value)
6870 warn_if_unused_value (expr, input_location);
6873 /* Process an expression as if it were a complete statement. Emit
6874 diagnostics, but do not call ADD_STMT. */
6876 tree
6877 c_process_expr_stmt (tree expr)
6879 if (!expr)
6880 return NULL_TREE;
6882 /* Do default conversion if safe and possibly important,
6883 in case within ({...}). */
6884 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6885 && (flag_isoc99 || lvalue_p (expr)))
6886 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6887 expr = default_conversion (expr);
6889 if (warn_sequence_point)
6890 verify_sequence_points (expr);
6892 if (TREE_TYPE (expr) != error_mark_node
6893 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6894 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6895 error ("expression statement has incomplete type");
6897 /* If we're not processing a statement expression, warn about unused values.
6898 Warnings for statement expressions will be emitted later, once we figure
6899 out which is the result. */
6900 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6901 && (extra_warnings || warn_unused_value))
6902 emit_side_effect_warnings (expr);
6904 /* If the expression is not of a type to which we cannot assign a line
6905 number, wrap the thing in a no-op NOP_EXPR. */
6906 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
6907 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6909 if (EXPR_P (expr))
6910 SET_EXPR_LOCATION (expr, input_location);
6912 return expr;
6915 /* Emit an expression as a statement. */
6917 tree
6918 c_finish_expr_stmt (tree expr)
6920 if (expr)
6921 return add_stmt (c_process_expr_stmt (expr));
6922 else
6923 return NULL;
6926 /* Do the opposite and emit a statement as an expression. To begin,
6927 create a new binding level and return it. */
6929 tree
6930 c_begin_stmt_expr (void)
6932 tree ret;
6934 /* We must force a BLOCK for this level so that, if it is not expanded
6935 later, there is a way to turn off the entire subtree of blocks that
6936 are contained in it. */
6937 keep_next_level ();
6938 ret = c_begin_compound_stmt (true);
6940 /* Mark the current statement list as belonging to a statement list. */
6941 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6943 return ret;
6946 tree
6947 c_finish_stmt_expr (tree body)
6949 tree last, type, tmp, val;
6950 tree *last_p;
6952 body = c_end_compound_stmt (body, true);
6954 /* Locate the last statement in BODY. See c_end_compound_stmt
6955 about always returning a BIND_EXPR. */
6956 last_p = &BIND_EXPR_BODY (body);
6957 last = BIND_EXPR_BODY (body);
6959 continue_searching:
6960 if (TREE_CODE (last) == STATEMENT_LIST)
6962 tree_stmt_iterator i;
6964 /* This can happen with degenerate cases like ({ }). No value. */
6965 if (!TREE_SIDE_EFFECTS (last))
6966 return body;
6968 /* If we're supposed to generate side effects warnings, process
6969 all of the statements except the last. */
6970 if (extra_warnings || warn_unused_value)
6972 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6973 emit_side_effect_warnings (tsi_stmt (i));
6975 else
6976 i = tsi_last (last);
6977 last_p = tsi_stmt_ptr (i);
6978 last = *last_p;
6981 /* If the end of the list is exception related, then the list was split
6982 by a call to push_cleanup. Continue searching. */
6983 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6984 || TREE_CODE (last) == TRY_CATCH_EXPR)
6986 last_p = &TREE_OPERAND (last, 0);
6987 last = *last_p;
6988 goto continue_searching;
6991 /* In the case that the BIND_EXPR is not necessary, return the
6992 expression out from inside it. */
6993 if (last == error_mark_node
6994 || (last == BIND_EXPR_BODY (body)
6995 && BIND_EXPR_VARS (body) == NULL))
6996 return last;
6998 /* Extract the type of said expression. */
6999 type = TREE_TYPE (last);
7001 /* If we're not returning a value at all, then the BIND_EXPR that
7002 we already have is a fine expression to return. */
7003 if (!type || VOID_TYPE_P (type))
7004 return body;
7006 /* Now that we've located the expression containing the value, it seems
7007 silly to make voidify_wrapper_expr repeat the process. Create a
7008 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7009 tmp = create_tmp_var_raw (type, NULL);
7011 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7012 tree_expr_nonnegative_p giving up immediately. */
7013 val = last;
7014 if (TREE_CODE (val) == NOP_EXPR
7015 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7016 val = TREE_OPERAND (val, 0);
7018 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7019 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7021 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7024 /* Begin and end compound statements. This is as simple as pushing
7025 and popping new statement lists from the tree. */
7027 tree
7028 c_begin_compound_stmt (bool do_scope)
7030 tree stmt = push_stmt_list ();
7031 if (do_scope)
7032 push_scope ();
7033 return stmt;
7036 tree
7037 c_end_compound_stmt (tree stmt, bool do_scope)
7039 tree block = NULL;
7041 if (do_scope)
7043 if (c_dialect_objc ())
7044 objc_clear_super_receiver ();
7045 block = pop_scope ();
7048 stmt = pop_stmt_list (stmt);
7049 stmt = c_build_bind_expr (block, stmt);
7051 /* If this compound statement is nested immediately inside a statement
7052 expression, then force a BIND_EXPR to be created. Otherwise we'll
7053 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7054 STATEMENT_LISTs merge, and thus we can lose track of what statement
7055 was really last. */
7056 if (cur_stmt_list
7057 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7058 && TREE_CODE (stmt) != BIND_EXPR)
7060 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7061 TREE_SIDE_EFFECTS (stmt) = 1;
7064 return stmt;
7067 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7068 when the current scope is exited. EH_ONLY is true when this is not
7069 meant to apply to normal control flow transfer. */
7071 void
7072 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7074 enum tree_code code;
7075 tree stmt, list;
7076 bool stmt_expr;
7078 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7079 stmt = build_stmt (code, NULL, cleanup);
7080 add_stmt (stmt);
7081 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7082 list = push_stmt_list ();
7083 TREE_OPERAND (stmt, 0) = list;
7084 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7087 /* Build a binary-operation expression without default conversions.
7088 CODE is the kind of expression to build.
7089 This function differs from `build' in several ways:
7090 the data type of the result is computed and recorded in it,
7091 warnings are generated if arg data types are invalid,
7092 special handling for addition and subtraction of pointers is known,
7093 and some optimization is done (operations on narrow ints
7094 are done in the narrower type when that gives the same result).
7095 Constant folding is also done before the result is returned.
7097 Note that the operands will never have enumeral types, or function
7098 or array types, because either they will have the default conversions
7099 performed or they have both just been converted to some other type in which
7100 the arithmetic is to be done. */
7102 tree
7103 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7104 int convert_p)
7106 tree type0, type1;
7107 enum tree_code code0, code1;
7108 tree op0, op1;
7110 /* Expression code to give to the expression when it is built.
7111 Normally this is CODE, which is what the caller asked for,
7112 but in some special cases we change it. */
7113 enum tree_code resultcode = code;
7115 /* Data type in which the computation is to be performed.
7116 In the simplest cases this is the common type of the arguments. */
7117 tree result_type = NULL;
7119 /* Nonzero means operands have already been type-converted
7120 in whatever way is necessary.
7121 Zero means they need to be converted to RESULT_TYPE. */
7122 int converted = 0;
7124 /* Nonzero means create the expression with this type, rather than
7125 RESULT_TYPE. */
7126 tree build_type = 0;
7128 /* Nonzero means after finally constructing the expression
7129 convert it to this type. */
7130 tree final_type = 0;
7132 /* Nonzero if this is an operation like MIN or MAX which can
7133 safely be computed in short if both args are promoted shorts.
7134 Also implies COMMON.
7135 -1 indicates a bitwise operation; this makes a difference
7136 in the exact conditions for when it is safe to do the operation
7137 in a narrower mode. */
7138 int shorten = 0;
7140 /* Nonzero if this is a comparison operation;
7141 if both args are promoted shorts, compare the original shorts.
7142 Also implies COMMON. */
7143 int short_compare = 0;
7145 /* Nonzero if this is a right-shift operation, which can be computed on the
7146 original short and then promoted if the operand is a promoted short. */
7147 int short_shift = 0;
7149 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7150 int common = 0;
7152 if (convert_p)
7154 op0 = default_conversion (orig_op0);
7155 op1 = default_conversion (orig_op1);
7157 else
7159 op0 = orig_op0;
7160 op1 = orig_op1;
7163 type0 = TREE_TYPE (op0);
7164 type1 = TREE_TYPE (op1);
7166 /* The expression codes of the data types of the arguments tell us
7167 whether the arguments are integers, floating, pointers, etc. */
7168 code0 = TREE_CODE (type0);
7169 code1 = TREE_CODE (type1);
7171 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7172 STRIP_TYPE_NOPS (op0);
7173 STRIP_TYPE_NOPS (op1);
7175 /* If an error was already reported for one of the arguments,
7176 avoid reporting another error. */
7178 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7179 return error_mark_node;
7181 switch (code)
7183 case PLUS_EXPR:
7184 /* Handle the pointer + int case. */
7185 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7186 return pointer_int_sum (PLUS_EXPR, op0, op1);
7187 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7188 return pointer_int_sum (PLUS_EXPR, op1, op0);
7189 else
7190 common = 1;
7191 break;
7193 case MINUS_EXPR:
7194 /* Subtraction of two similar pointers.
7195 We must subtract them as integers, then divide by object size. */
7196 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7197 && comp_target_types (type0, type1, 1))
7198 return pointer_diff (op0, op1);
7199 /* Handle pointer minus int. Just like pointer plus int. */
7200 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7201 return pointer_int_sum (MINUS_EXPR, op0, op1);
7202 else
7203 common = 1;
7204 break;
7206 case MULT_EXPR:
7207 common = 1;
7208 break;
7210 case TRUNC_DIV_EXPR:
7211 case CEIL_DIV_EXPR:
7212 case FLOOR_DIV_EXPR:
7213 case ROUND_DIV_EXPR:
7214 case EXACT_DIV_EXPR:
7215 /* Floating point division by zero is a legitimate way to obtain
7216 infinities and NaNs. */
7217 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7218 warning ("division by zero");
7220 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7221 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7222 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7223 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7225 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7226 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7227 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7228 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7230 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7231 resultcode = RDIV_EXPR;
7232 else
7233 /* Although it would be tempting to shorten always here, that
7234 loses on some targets, since the modulo instruction is
7235 undefined if the quotient can't be represented in the
7236 computation mode. We shorten only if unsigned or if
7237 dividing by something we know != -1. */
7238 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7239 || (TREE_CODE (op1) == INTEGER_CST
7240 && !integer_all_onesp (op1)));
7241 common = 1;
7243 break;
7245 case BIT_AND_EXPR:
7246 case BIT_IOR_EXPR:
7247 case BIT_XOR_EXPR:
7248 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7249 shorten = -1;
7250 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7251 common = 1;
7252 break;
7254 case TRUNC_MOD_EXPR:
7255 case FLOOR_MOD_EXPR:
7256 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7257 warning ("division by zero");
7259 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7261 /* Although it would be tempting to shorten always here, that loses
7262 on some targets, since the modulo instruction is undefined if the
7263 quotient can't be represented in the computation mode. We shorten
7264 only if unsigned or if dividing by something we know != -1. */
7265 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7266 || (TREE_CODE (op1) == INTEGER_CST
7267 && !integer_all_onesp (op1)));
7268 common = 1;
7270 break;
7272 case TRUTH_ANDIF_EXPR:
7273 case TRUTH_ORIF_EXPR:
7274 case TRUTH_AND_EXPR:
7275 case TRUTH_OR_EXPR:
7276 case TRUTH_XOR_EXPR:
7277 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7278 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7279 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7280 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7282 /* Result of these operations is always an int,
7283 but that does not mean the operands should be
7284 converted to ints! */
7285 result_type = integer_type_node;
7286 op0 = lang_hooks.truthvalue_conversion (op0);
7287 op1 = lang_hooks.truthvalue_conversion (op1);
7288 converted = 1;
7290 break;
7292 /* Shift operations: result has same type as first operand;
7293 always convert second operand to int.
7294 Also set SHORT_SHIFT if shifting rightward. */
7296 case RSHIFT_EXPR:
7297 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7299 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7301 if (tree_int_cst_sgn (op1) < 0)
7302 warning ("right shift count is negative");
7303 else
7305 if (!integer_zerop (op1))
7306 short_shift = 1;
7308 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7309 warning ("right shift count >= width of type");
7313 /* Use the type of the value to be shifted. */
7314 result_type = type0;
7315 /* Convert the shift-count to an integer, regardless of size
7316 of value being shifted. */
7317 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7318 op1 = convert (integer_type_node, op1);
7319 /* Avoid converting op1 to result_type later. */
7320 converted = 1;
7322 break;
7324 case LSHIFT_EXPR:
7325 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7327 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7329 if (tree_int_cst_sgn (op1) < 0)
7330 warning ("left shift count is negative");
7332 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7333 warning ("left shift count >= width of type");
7336 /* Use the type of the value to be shifted. */
7337 result_type = type0;
7338 /* Convert the shift-count to an integer, regardless of size
7339 of value being shifted. */
7340 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7341 op1 = convert (integer_type_node, op1);
7342 /* Avoid converting op1 to result_type later. */
7343 converted = 1;
7345 break;
7347 case EQ_EXPR:
7348 case NE_EXPR:
7349 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7350 warning ("comparing floating point with == or != is unsafe");
7351 /* Result of comparison is always int,
7352 but don't convert the args to int! */
7353 build_type = integer_type_node;
7354 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7355 || code0 == COMPLEX_TYPE)
7356 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7357 || code1 == COMPLEX_TYPE))
7358 short_compare = 1;
7359 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7361 tree tt0 = TREE_TYPE (type0);
7362 tree tt1 = TREE_TYPE (type1);
7363 /* Anything compares with void *. void * compares with anything.
7364 Otherwise, the targets must be compatible
7365 and both must be object or both incomplete. */
7366 if (comp_target_types (type0, type1, 1))
7367 result_type = common_pointer_type (type0, type1);
7368 else if (VOID_TYPE_P (tt0))
7370 /* op0 != orig_op0 detects the case of something
7371 whose value is 0 but which isn't a valid null ptr const. */
7372 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7373 && TREE_CODE (tt1) == FUNCTION_TYPE)
7374 pedwarn ("ISO C forbids comparison of %<void *%>"
7375 " with function pointer");
7377 else if (VOID_TYPE_P (tt1))
7379 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7380 && TREE_CODE (tt0) == FUNCTION_TYPE)
7381 pedwarn ("ISO C forbids comparison of %<void *%>"
7382 " with function pointer");
7384 else
7385 pedwarn ("comparison of distinct pointer types lacks a cast");
7387 if (result_type == NULL_TREE)
7388 result_type = ptr_type_node;
7390 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7391 && integer_zerop (op1))
7392 result_type = type0;
7393 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7394 && integer_zerop (op0))
7395 result_type = type1;
7396 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7398 result_type = type0;
7399 pedwarn ("comparison between pointer and integer");
7401 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7403 result_type = type1;
7404 pedwarn ("comparison between pointer and integer");
7406 break;
7408 case LE_EXPR:
7409 case GE_EXPR:
7410 case LT_EXPR:
7411 case GT_EXPR:
7412 build_type = integer_type_node;
7413 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7414 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7415 short_compare = 1;
7416 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7418 if (comp_target_types (type0, type1, 1))
7420 result_type = common_pointer_type (type0, type1);
7421 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7422 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7423 pedwarn ("comparison of complete and incomplete pointers");
7424 else if (pedantic
7425 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7426 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7428 else
7430 result_type = ptr_type_node;
7431 pedwarn ("comparison of distinct pointer types lacks a cast");
7434 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7435 && integer_zerop (op1))
7437 result_type = type0;
7438 if (pedantic || extra_warnings)
7439 pedwarn ("ordered comparison of pointer with integer zero");
7441 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7442 && integer_zerop (op0))
7444 result_type = type1;
7445 if (pedantic)
7446 pedwarn ("ordered comparison of pointer with integer zero");
7448 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7450 result_type = type0;
7451 pedwarn ("comparison between pointer and integer");
7453 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7455 result_type = type1;
7456 pedwarn ("comparison between pointer and integer");
7458 break;
7460 default:
7461 gcc_unreachable ();
7464 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7465 return error_mark_node;
7467 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7468 || code0 == VECTOR_TYPE)
7470 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7471 || code1 == VECTOR_TYPE))
7473 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7475 if (shorten || common || short_compare)
7476 result_type = c_common_type (type0, type1);
7478 /* For certain operations (which identify themselves by shorten != 0)
7479 if both args were extended from the same smaller type,
7480 do the arithmetic in that type and then extend.
7482 shorten !=0 and !=1 indicates a bitwise operation.
7483 For them, this optimization is safe only if
7484 both args are zero-extended or both are sign-extended.
7485 Otherwise, we might change the result.
7486 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7487 but calculated in (unsigned short) it would be (unsigned short)-1. */
7489 if (shorten && none_complex)
7491 int unsigned0, unsigned1;
7492 tree arg0 = get_narrower (op0, &unsigned0);
7493 tree arg1 = get_narrower (op1, &unsigned1);
7494 /* UNS is 1 if the operation to be done is an unsigned one. */
7495 int uns = TYPE_UNSIGNED (result_type);
7496 tree type;
7498 final_type = result_type;
7500 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7501 but it *requires* conversion to FINAL_TYPE. */
7503 if ((TYPE_PRECISION (TREE_TYPE (op0))
7504 == TYPE_PRECISION (TREE_TYPE (arg0)))
7505 && TREE_TYPE (op0) != final_type)
7506 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7507 if ((TYPE_PRECISION (TREE_TYPE (op1))
7508 == TYPE_PRECISION (TREE_TYPE (arg1)))
7509 && TREE_TYPE (op1) != final_type)
7510 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7512 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7514 /* For bitwise operations, signedness of nominal type
7515 does not matter. Consider only how operands were extended. */
7516 if (shorten == -1)
7517 uns = unsigned0;
7519 /* Note that in all three cases below we refrain from optimizing
7520 an unsigned operation on sign-extended args.
7521 That would not be valid. */
7523 /* Both args variable: if both extended in same way
7524 from same width, do it in that width.
7525 Do it unsigned if args were zero-extended. */
7526 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7527 < TYPE_PRECISION (result_type))
7528 && (TYPE_PRECISION (TREE_TYPE (arg1))
7529 == TYPE_PRECISION (TREE_TYPE (arg0)))
7530 && unsigned0 == unsigned1
7531 && (unsigned0 || !uns))
7532 result_type
7533 = c_common_signed_or_unsigned_type
7534 (unsigned0, c_common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7535 else if (TREE_CODE (arg0) == INTEGER_CST
7536 && (unsigned1 || !uns)
7537 && (TYPE_PRECISION (TREE_TYPE (arg1))
7538 < TYPE_PRECISION (result_type))
7539 && (type
7540 = c_common_signed_or_unsigned_type (unsigned1,
7541 TREE_TYPE (arg1)),
7542 int_fits_type_p (arg0, type)))
7543 result_type = type;
7544 else if (TREE_CODE (arg1) == INTEGER_CST
7545 && (unsigned0 || !uns)
7546 && (TYPE_PRECISION (TREE_TYPE (arg0))
7547 < TYPE_PRECISION (result_type))
7548 && (type
7549 = c_common_signed_or_unsigned_type (unsigned0,
7550 TREE_TYPE (arg0)),
7551 int_fits_type_p (arg1, type)))
7552 result_type = type;
7555 /* Shifts can be shortened if shifting right. */
7557 if (short_shift)
7559 int unsigned_arg;
7560 tree arg0 = get_narrower (op0, &unsigned_arg);
7562 final_type = result_type;
7564 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7565 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7567 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7568 /* We can shorten only if the shift count is less than the
7569 number of bits in the smaller type size. */
7570 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7571 /* We cannot drop an unsigned shift after sign-extension. */
7572 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7574 /* Do an unsigned shift if the operand was zero-extended. */
7575 result_type
7576 = c_common_signed_or_unsigned_type (unsigned_arg,
7577 TREE_TYPE (arg0));
7578 /* Convert value-to-be-shifted to that type. */
7579 if (TREE_TYPE (op0) != result_type)
7580 op0 = convert (result_type, op0);
7581 converted = 1;
7585 /* Comparison operations are shortened too but differently.
7586 They identify themselves by setting short_compare = 1. */
7588 if (short_compare)
7590 /* Don't write &op0, etc., because that would prevent op0
7591 from being kept in a register.
7592 Instead, make copies of the our local variables and
7593 pass the copies by reference, then copy them back afterward. */
7594 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7595 enum tree_code xresultcode = resultcode;
7596 tree val
7597 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7599 if (val != 0)
7600 return val;
7602 op0 = xop0, op1 = xop1;
7603 converted = 1;
7604 resultcode = xresultcode;
7606 if (warn_sign_compare && skip_evaluation == 0)
7608 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7609 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7610 int unsignedp0, unsignedp1;
7611 tree primop0 = get_narrower (op0, &unsignedp0);
7612 tree primop1 = get_narrower (op1, &unsignedp1);
7614 xop0 = orig_op0;
7615 xop1 = orig_op1;
7616 STRIP_TYPE_NOPS (xop0);
7617 STRIP_TYPE_NOPS (xop1);
7619 /* Give warnings for comparisons between signed and unsigned
7620 quantities that may fail.
7622 Do the checking based on the original operand trees, so that
7623 casts will be considered, but default promotions won't be.
7625 Do not warn if the comparison is being done in a signed type,
7626 since the signed type will only be chosen if it can represent
7627 all the values of the unsigned type. */
7628 if (!TYPE_UNSIGNED (result_type))
7629 /* OK */;
7630 /* Do not warn if both operands are the same signedness. */
7631 else if (op0_signed == op1_signed)
7632 /* OK */;
7633 else
7635 tree sop, uop;
7637 if (op0_signed)
7638 sop = xop0, uop = xop1;
7639 else
7640 sop = xop1, uop = xop0;
7642 /* Do not warn if the signed quantity is an
7643 unsuffixed integer literal (or some static
7644 constant expression involving such literals or a
7645 conditional expression involving such literals)
7646 and it is non-negative. */
7647 if (tree_expr_nonnegative_p (sop))
7648 /* OK */;
7649 /* Do not warn if the comparison is an equality operation,
7650 the unsigned quantity is an integral constant, and it
7651 would fit in the result if the result were signed. */
7652 else if (TREE_CODE (uop) == INTEGER_CST
7653 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7654 && int_fits_type_p
7655 (uop, c_common_signed_type (result_type)))
7656 /* OK */;
7657 /* Do not warn if the unsigned quantity is an enumeration
7658 constant and its maximum value would fit in the result
7659 if the result were signed. */
7660 else if (TREE_CODE (uop) == INTEGER_CST
7661 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7662 && int_fits_type_p
7663 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
7664 c_common_signed_type (result_type)))
7665 /* OK */;
7666 else
7667 warning ("comparison between signed and unsigned");
7670 /* Warn if two unsigned values are being compared in a size
7671 larger than their original size, and one (and only one) is the
7672 result of a `~' operator. This comparison will always fail.
7674 Also warn if one operand is a constant, and the constant
7675 does not have all bits set that are set in the ~ operand
7676 when it is extended. */
7678 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7679 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7681 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7682 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7683 &unsignedp0);
7684 else
7685 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7686 &unsignedp1);
7688 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7690 tree primop;
7691 HOST_WIDE_INT constant, mask;
7692 int unsignedp, bits;
7694 if (host_integerp (primop0, 0))
7696 primop = primop1;
7697 unsignedp = unsignedp1;
7698 constant = tree_low_cst (primop0, 0);
7700 else
7702 primop = primop0;
7703 unsignedp = unsignedp0;
7704 constant = tree_low_cst (primop1, 0);
7707 bits = TYPE_PRECISION (TREE_TYPE (primop));
7708 if (bits < TYPE_PRECISION (result_type)
7709 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7711 mask = (~(HOST_WIDE_INT) 0) << bits;
7712 if ((mask & constant) != mask)
7713 warning ("comparison of promoted ~unsigned with constant");
7716 else if (unsignedp0 && unsignedp1
7717 && (TYPE_PRECISION (TREE_TYPE (primop0))
7718 < TYPE_PRECISION (result_type))
7719 && (TYPE_PRECISION (TREE_TYPE (primop1))
7720 < TYPE_PRECISION (result_type)))
7721 warning ("comparison of promoted ~unsigned with unsigned");
7727 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7728 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7729 Then the expression will be built.
7730 It will be given type FINAL_TYPE if that is nonzero;
7731 otherwise, it will be given type RESULT_TYPE. */
7733 if (!result_type)
7735 binary_op_error (code);
7736 return error_mark_node;
7739 if (!converted)
7741 if (TREE_TYPE (op0) != result_type)
7742 op0 = convert (result_type, op0);
7743 if (TREE_TYPE (op1) != result_type)
7744 op1 = convert (result_type, op1);
7746 /* This can happen if one operand has a vector type, and the other
7747 has a different type. */
7748 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7749 return error_mark_node;
7752 if (build_type == NULL_TREE)
7753 build_type = result_type;
7756 tree result = build2 (resultcode, build_type, op0, op1);
7758 /* Treat expressions in initializers specially as they can't trap. */
7759 result = require_constant_value ? fold_initializer (result)
7760 : fold (result);
7762 if (final_type != 0)
7763 result = convert (final_type, result);
7764 return result;