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, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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. */
30 #include "coretypes.h"
34 #include "langhooks.h"
44 #include "tree-iterator.h"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
57 /* Whether we are building a boolean conversion inside
58 convert_for_assignment, or some other late binary operation. If
59 build_binary_op is called (from code shared with C++) in this case,
60 then the operands have already been folded and the result will not
61 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
62 bool in_late_binary_op;
64 /* The level of nesting inside "__alignof__". */
67 /* The level of nesting inside "sizeof". */
70 /* The level of nesting inside "typeof". */
73 struct c_label_context_se *label_context_stack_se;
74 struct c_label_context_vm *label_context_stack_vm;
76 /* Nonzero if we've already printed a "missing braces around initializer"
77 message within this initializer. */
78 static int missing_braces_mentioned;
80 static int require_constant_value;
81 static int require_constant_elements;
83 static bool null_pointer_constant_p (const_tree);
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (const_tree, const_tree);
86 static int comp_target_types (tree, tree);
87 static int function_types_compatible_p (const_tree, const_tree);
88 static int type_lists_compatible_p (const_tree, const_tree);
89 static tree lookup_field (tree, tree);
90 static int convert_arguments (int, tree *, tree, tree, tree, tree);
91 static tree pointer_diff (tree, tree);
92 static tree convert_for_assignment (tree, tree, enum impl_conv, bool,
94 static tree valid_compound_expr_initializer (tree, tree);
95 static void push_string (const char *);
96 static void push_member_name (tree);
97 static int spelling_length (void);
98 static char *print_spelling (char *);
99 static void warning_init (int, const char *);
100 static tree digest_init (tree, tree, bool, bool, int);
101 static void output_init_element (tree, bool, tree, tree, int, bool);
102 static void output_pending_init_elements (int);
103 static int set_designator (int);
104 static void push_range_stack (tree);
105 static void add_pending_init (tree, tree, bool);
106 static void set_nonincremental_init (void);
107 static void set_nonincremental_init_from_string (tree);
108 static tree find_init_member (tree);
109 static void readonly_error (tree, enum lvalue_use);
110 static int lvalue_or_else (const_tree, enum lvalue_use);
111 static int lvalue_p (const_tree);
112 static void record_maybe_used_decl (tree);
113 static int comptypes_internal (const_tree, const_tree);
115 /* Return true if EXP is a null pointer constant, false otherwise. */
118 null_pointer_constant_p (const_tree expr)
120 /* This should really operate on c_expr structures, but they aren't
121 yet available everywhere required. */
122 tree type = TREE_TYPE (expr);
123 return (TREE_CODE (expr) == INTEGER_CST
124 && !TREE_OVERFLOW (expr)
125 && integer_zerop (expr)
126 && (INTEGRAL_TYPE_P (type)
127 || (TREE_CODE (type) == POINTER_TYPE
128 && VOID_TYPE_P (TREE_TYPE (type))
129 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
132 /* EXPR may appear in an unevaluated part of an integer constant
133 expression, but not in an evaluated part. Wrap it in a
134 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
135 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
138 note_integer_operands (tree expr)
141 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
143 ret = copy_node (expr);
144 TREE_OVERFLOW (ret) = 1;
148 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
149 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
154 /* Having checked whether EXPR may appear in an unevaluated part of an
155 integer constant expression and found that it may, remove any
156 C_MAYBE_CONST_EXPR noting this fact and return the resulting
160 remove_c_maybe_const_expr (tree expr)
162 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
163 return C_MAYBE_CONST_EXPR_EXPR (expr);
168 \f/* This is a cache to hold if two types are compatible or not. */
170 struct tagged_tu_seen_cache {
171 const struct tagged_tu_seen_cache * next;
174 /* The return value of tagged_types_tu_compatible_p if we had seen
175 these two types already. */
179 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
180 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
182 /* Do `exp = require_complete_type (exp);' to make sure exp
183 does not have an incomplete type. (That includes void types.) */
186 require_complete_type (tree value)
188 tree type = TREE_TYPE (value);
190 if (value == error_mark_node || type == error_mark_node)
191 return error_mark_node;
193 /* First, detect a valid value with a complete type. */
194 if (COMPLETE_TYPE_P (type))
197 c_incomplete_type_error (value, type);
198 return error_mark_node;
201 /* Print an error message for invalid use of an incomplete type.
202 VALUE is the expression that was used (or 0 if that isn't known)
203 and TYPE is the type that was invalid. */
206 c_incomplete_type_error (const_tree value, const_tree type)
208 const char *type_code_string;
210 /* Avoid duplicate error message. */
211 if (TREE_CODE (type) == ERROR_MARK)
214 if (value != 0 && (TREE_CODE (value) == VAR_DECL
215 || TREE_CODE (value) == PARM_DECL))
216 error ("%qD has an incomplete type", value);
220 /* We must print an error message. Be clever about what it says. */
222 switch (TREE_CODE (type))
225 type_code_string = "struct";
229 type_code_string = "union";
233 type_code_string = "enum";
237 error ("invalid use of void expression");
241 if (TYPE_DOMAIN (type))
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 error ("invalid use of flexible array member");
248 type = TREE_TYPE (type);
251 error ("invalid use of array with unspecified bounds");
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
259 error ("invalid use of undefined type %<%s %E%>",
260 type_code_string, TYPE_NAME (type));
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
271 c_type_promotes_to (tree type)
273 if (TYPE_MAIN_VARIANT (type) == float_type_node)
274 return double_type_node;
276 if (c_promoting_integer_type_p (type))
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type)
280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
281 return unsigned_type_node;
282 return integer_type_node;
288 /* Return a variant of TYPE which has all the type qualifiers of LIKE
289 as well as those of TYPE. */
292 qualify_type (tree type, tree like)
294 return c_build_qualified_type (type,
295 TYPE_QUALS (type) | TYPE_QUALS (like));
298 /* Return true iff the given tree T is a variable length array. */
301 c_vla_type_p (const_tree t)
303 if (TREE_CODE (t) == ARRAY_TYPE
304 && C_TYPE_VARIABLE_SIZE (t))
309 /* Return the composite type of two compatible types.
311 We assume that comptypes has already been done and returned
312 nonzero; if that isn't so, this may crash. In particular, we
313 assume that qualifiers match. */
316 composite_type (tree t1, tree t2)
318 enum tree_code code1;
319 enum tree_code code2;
322 /* Save time if the two types are the same. */
324 if (t1 == t2) return t1;
326 /* If one type is nonsense, use the other. */
327 if (t1 == error_mark_node)
329 if (t2 == error_mark_node)
332 code1 = TREE_CODE (t1);
333 code2 = TREE_CODE (t2);
335 /* Merge the attributes. */
336 attributes = targetm.merge_type_attributes (t1, t2);
338 /* If one is an enumerated type and the other is the compatible
339 integer type, the composite type might be either of the two
340 (DR#013 question 3). For consistency, use the enumerated type as
341 the composite type. */
343 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
345 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
348 gcc_assert (code1 == code2);
353 /* For two pointers, do this recursively on the target type. */
355 tree pointed_to_1 = TREE_TYPE (t1);
356 tree pointed_to_2 = TREE_TYPE (t2);
357 tree target = composite_type (pointed_to_1, pointed_to_2);
358 t1 = build_pointer_type (target);
359 t1 = build_type_attribute_variant (t1, attributes);
360 return qualify_type (t1, t2);
365 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
368 tree d1 = TYPE_DOMAIN (t1);
369 tree d2 = TYPE_DOMAIN (t2);
370 bool d1_variable, d2_variable;
371 bool d1_zero, d2_zero;
372 bool t1_complete, t2_complete;
374 /* We should not have any type quals on arrays at all. */
375 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
377 t1_complete = COMPLETE_TYPE_P (t1);
378 t2_complete = COMPLETE_TYPE_P (t2);
380 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
381 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
383 d1_variable = (!d1_zero
384 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
385 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
386 d2_variable = (!d2_zero
387 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
388 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
389 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
390 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
392 /* Save space: see if the result is identical to one of the args. */
393 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
394 && (d2_variable || d2_zero || !d1_variable))
395 return build_type_attribute_variant (t1, attributes);
396 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
397 && (d1_variable || d1_zero || !d2_variable))
398 return build_type_attribute_variant (t2, attributes);
400 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
401 return build_type_attribute_variant (t1, attributes);
402 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
403 return build_type_attribute_variant (t2, attributes);
405 /* Merge the element types, and have a size if either arg has
406 one. We may have qualifiers on the element types. To set
407 up TYPE_MAIN_VARIANT correctly, we need to form the
408 composite of the unqualified types and add the qualifiers
410 quals = TYPE_QUALS (strip_array_types (elt));
411 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
412 t1 = build_array_type (unqual_elt,
413 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
419 /* Ensure a composite type involving a zero-length array type
420 is a zero-length type not an incomplete type. */
421 if (d1_zero && d2_zero
422 && (t1_complete || t2_complete)
423 && !COMPLETE_TYPE_P (t1))
425 TYPE_SIZE (t1) = bitsize_zero_node;
426 TYPE_SIZE_UNIT (t1) = size_zero_node;
428 t1 = c_build_qualified_type (t1, quals);
429 return build_type_attribute_variant (t1, attributes);
435 if (attributes != NULL)
437 /* Try harder not to create a new aggregate type. */
438 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
440 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
443 return build_type_attribute_variant (t1, attributes);
446 /* Function types: prefer the one that specified arg types.
447 If both do, merge the arg types. Also merge the return types. */
449 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
450 tree p1 = TYPE_ARG_TYPES (t1);
451 tree p2 = TYPE_ARG_TYPES (t2);
456 /* Save space: see if the result is identical to one of the args. */
457 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
458 return build_type_attribute_variant (t1, attributes);
459 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
460 return build_type_attribute_variant (t2, attributes);
462 /* Simple way if one arg fails to specify argument types. */
463 if (TYPE_ARG_TYPES (t1) == 0)
465 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
466 t1 = build_type_attribute_variant (t1, attributes);
467 return qualify_type (t1, t2);
469 if (TYPE_ARG_TYPES (t2) == 0)
471 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
472 t1 = build_type_attribute_variant (t1, attributes);
473 return qualify_type (t1, t2);
476 /* If both args specify argument types, we must merge the two
477 lists, argument by argument. */
478 /* Tell global_bindings_p to return false so that variable_size
479 doesn't die on VLAs in parameter types. */
480 c_override_global_bindings_to_false = true;
482 len = list_length (p1);
485 for (i = 0; i < len; i++)
486 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
491 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
493 /* A null type means arg type is not specified.
494 Take whatever the other function type has. */
495 if (TREE_VALUE (p1) == 0)
497 TREE_VALUE (n) = TREE_VALUE (p2);
500 if (TREE_VALUE (p2) == 0)
502 TREE_VALUE (n) = TREE_VALUE (p1);
506 /* Given wait (union {union wait *u; int *i} *)
507 and wait (union wait *),
508 prefer union wait * as type of parm. */
509 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
510 && TREE_VALUE (p1) != TREE_VALUE (p2))
513 tree mv2 = TREE_VALUE (p2);
514 if (mv2 && mv2 != error_mark_node
515 && TREE_CODE (mv2) != ARRAY_TYPE)
516 mv2 = TYPE_MAIN_VARIANT (mv2);
517 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
518 memb; memb = TREE_CHAIN (memb))
520 tree mv3 = TREE_TYPE (memb);
521 if (mv3 && mv3 != error_mark_node
522 && TREE_CODE (mv3) != ARRAY_TYPE)
523 mv3 = TYPE_MAIN_VARIANT (mv3);
524 if (comptypes (mv3, mv2))
526 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
528 pedwarn (input_location, OPT_pedantic,
529 "function types not truly compatible in ISO C");
534 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
535 && TREE_VALUE (p2) != TREE_VALUE (p1))
538 tree mv1 = TREE_VALUE (p1);
539 if (mv1 && mv1 != error_mark_node
540 && TREE_CODE (mv1) != ARRAY_TYPE)
541 mv1 = TYPE_MAIN_VARIANT (mv1);
542 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
543 memb; memb = TREE_CHAIN (memb))
545 tree mv3 = TREE_TYPE (memb);
546 if (mv3 && mv3 != error_mark_node
547 && TREE_CODE (mv3) != ARRAY_TYPE)
548 mv3 = TYPE_MAIN_VARIANT (mv3);
549 if (comptypes (mv3, mv1))
551 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
553 pedwarn (input_location, OPT_pedantic,
554 "function types not truly compatible in ISO C");
559 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
563 c_override_global_bindings_to_false = false;
564 t1 = build_function_type (valtype, newargs);
565 t1 = qualify_type (t1, t2);
566 /* ... falls through ... */
570 return build_type_attribute_variant (t1, attributes);
575 /* Return the type of a conditional expression between pointers to
576 possibly differently qualified versions of compatible types.
578 We assume that comp_target_types has already been done and returned
579 nonzero; if that isn't so, this may crash. */
582 common_pointer_type (tree t1, tree t2)
585 tree pointed_to_1, mv1;
586 tree pointed_to_2, mv2;
588 unsigned target_quals;
590 /* Save time if the two types are the same. */
592 if (t1 == t2) return t1;
594 /* If one type is nonsense, use the other. */
595 if (t1 == error_mark_node)
597 if (t2 == error_mark_node)
600 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
601 && TREE_CODE (t2) == POINTER_TYPE);
603 /* Merge the attributes. */
604 attributes = targetm.merge_type_attributes (t1, t2);
606 /* Find the composite type of the target types, and combine the
607 qualifiers of the two types' targets. Do not lose qualifiers on
608 array element types by taking the TYPE_MAIN_VARIANT. */
609 mv1 = pointed_to_1 = TREE_TYPE (t1);
610 mv2 = pointed_to_2 = TREE_TYPE (t2);
611 if (TREE_CODE (mv1) != ARRAY_TYPE)
612 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
613 if (TREE_CODE (mv2) != ARRAY_TYPE)
614 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
615 target = composite_type (mv1, mv2);
617 /* For function types do not merge const qualifiers, but drop them
618 if used inconsistently. The middle-end uses these to mark const
619 and noreturn functions. */
620 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
621 target_quals = TYPE_QUALS (pointed_to_1) & TYPE_QUALS (pointed_to_2);
623 target_quals = TYPE_QUALS (pointed_to_1) | TYPE_QUALS (pointed_to_2);
624 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
625 return build_type_attribute_variant (t1, attributes);
628 /* Return the common type for two arithmetic types under the usual
629 arithmetic conversions. The default conversions have already been
630 applied, and enumerated types converted to their compatible integer
631 types. The resulting type is unqualified and has no attributes.
633 This is the type for the result of most arithmetic operations
634 if the operands have the given two types. */
637 c_common_type (tree t1, tree t2)
639 enum tree_code code1;
640 enum tree_code code2;
642 /* If one type is nonsense, use the other. */
643 if (t1 == error_mark_node)
645 if (t2 == error_mark_node)
648 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
649 t1 = TYPE_MAIN_VARIANT (t1);
651 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
652 t2 = TYPE_MAIN_VARIANT (t2);
654 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
655 t1 = build_type_attribute_variant (t1, NULL_TREE);
657 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
658 t2 = build_type_attribute_variant (t2, NULL_TREE);
660 /* Save time if the two types are the same. */
662 if (t1 == t2) return t1;
664 code1 = TREE_CODE (t1);
665 code2 = TREE_CODE (t2);
667 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
668 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
669 || code1 == INTEGER_TYPE);
670 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
671 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
672 || code2 == INTEGER_TYPE);
674 /* When one operand is a decimal float type, the other operand cannot be
675 a generic float type or a complex type. We also disallow vector types
677 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
678 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
680 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
682 error ("can%'t mix operands of decimal float and vector types");
683 return error_mark_node;
685 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
687 error ("can%'t mix operands of decimal float and complex types");
688 return error_mark_node;
690 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
692 error ("can%'t mix operands of decimal float and other float types");
693 return error_mark_node;
697 /* If one type is a vector type, return that type. (How the usual
698 arithmetic conversions apply to the vector types extension is not
699 precisely specified.) */
700 if (code1 == VECTOR_TYPE)
703 if (code2 == VECTOR_TYPE)
706 /* If one type is complex, form the common type of the non-complex
707 components, then make that complex. Use T1 or T2 if it is the
709 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
711 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
712 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
713 tree subtype = c_common_type (subtype1, subtype2);
715 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
717 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
720 return build_complex_type (subtype);
723 /* If only one is real, use it as the result. */
725 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
728 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
731 /* If both are real and either are decimal floating point types, use
732 the decimal floating point type with the greater precision. */
734 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
736 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
737 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
738 return dfloat128_type_node;
739 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
740 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
741 return dfloat64_type_node;
742 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
743 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
744 return dfloat32_type_node;
747 /* Deal with fixed-point types. */
748 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
750 unsigned int unsignedp = 0, satp = 0;
751 enum machine_mode m1, m2;
752 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
757 /* If one input type is saturating, the result type is saturating. */
758 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
761 /* If both fixed-point types are unsigned, the result type is unsigned.
762 When mixing fixed-point and integer types, follow the sign of the
764 Otherwise, the result type is signed. */
765 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
766 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
767 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
768 && TYPE_UNSIGNED (t1))
769 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
770 && TYPE_UNSIGNED (t2)))
773 /* The result type is signed. */
776 /* If the input type is unsigned, we need to convert to the
778 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
780 enum mode_class mclass = (enum mode_class) 0;
781 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
783 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
787 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
789 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
791 enum mode_class mclass = (enum mode_class) 0;
792 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
794 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
798 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
802 if (code1 == FIXED_POINT_TYPE)
804 fbit1 = GET_MODE_FBIT (m1);
805 ibit1 = GET_MODE_IBIT (m1);
810 /* Signed integers need to subtract one sign bit. */
811 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
814 if (code2 == FIXED_POINT_TYPE)
816 fbit2 = GET_MODE_FBIT (m2);
817 ibit2 = GET_MODE_IBIT (m2);
822 /* Signed integers need to subtract one sign bit. */
823 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
826 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
827 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
828 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
832 /* Both real or both integers; use the one with greater precision. */
834 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
836 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
839 /* Same precision. Prefer long longs to longs to ints when the
840 same precision, following the C99 rules on integer type rank
841 (which are equivalent to the C90 rules for C90 types). */
843 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
844 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
845 return long_long_unsigned_type_node;
847 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
848 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
850 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
851 return long_long_unsigned_type_node;
853 return long_long_integer_type_node;
856 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
857 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
858 return long_unsigned_type_node;
860 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
861 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
863 /* But preserve unsignedness from the other type,
864 since long cannot hold all the values of an unsigned int. */
865 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
866 return long_unsigned_type_node;
868 return long_integer_type_node;
871 /* Likewise, prefer long double to double even if same size. */
872 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
873 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
874 return long_double_type_node;
876 /* Otherwise prefer the unsigned one. */
878 if (TYPE_UNSIGNED (t1))
884 /* Wrapper around c_common_type that is used by c-common.c and other
885 front end optimizations that remove promotions. ENUMERAL_TYPEs
886 are allowed here and are converted to their compatible integer types.
887 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
888 preferably a non-Boolean type as the common type. */
890 common_type (tree t1, tree t2)
892 if (TREE_CODE (t1) == ENUMERAL_TYPE)
893 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
894 if (TREE_CODE (t2) == ENUMERAL_TYPE)
895 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
897 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
898 if (TREE_CODE (t1) == BOOLEAN_TYPE
899 && TREE_CODE (t2) == BOOLEAN_TYPE)
900 return boolean_type_node;
902 /* If either type is BOOLEAN_TYPE, then return the other. */
903 if (TREE_CODE (t1) == BOOLEAN_TYPE)
905 if (TREE_CODE (t2) == BOOLEAN_TYPE)
908 return c_common_type (t1, t2);
911 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
912 or various other operations. Return 2 if they are compatible
913 but a warning may be needed if you use them together. */
916 comptypes (tree type1, tree type2)
918 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
921 val = comptypes_internal (type1, type2);
922 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
927 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
928 or various other operations. Return 2 if they are compatible
929 but a warning may be needed if you use them together. This
930 differs from comptypes, in that we don't free the seen types. */
933 comptypes_internal (const_tree type1, const_tree type2)
935 const_tree t1 = type1;
936 const_tree t2 = type2;
939 /* Suppress errors caused by previously reported errors. */
941 if (t1 == t2 || !t1 || !t2
942 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
945 /* If either type is the internal version of sizetype, return the
947 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
948 && TYPE_ORIG_SIZE_TYPE (t1))
949 t1 = TYPE_ORIG_SIZE_TYPE (t1);
951 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
952 && TYPE_ORIG_SIZE_TYPE (t2))
953 t2 = TYPE_ORIG_SIZE_TYPE (t2);
956 /* Enumerated types are compatible with integer types, but this is
957 not transitive: two enumerated types in the same translation unit
958 are compatible with each other only if they are the same type. */
960 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
961 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
962 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
963 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
968 /* Different classes of types can't be compatible. */
970 if (TREE_CODE (t1) != TREE_CODE (t2))
973 /* Qualifiers must match. C99 6.7.3p9 */
975 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
978 /* Allow for two different type nodes which have essentially the same
979 definition. Note that we already checked for equality of the type
980 qualifiers (just above). */
982 if (TREE_CODE (t1) != ARRAY_TYPE
983 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
986 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
987 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
990 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
993 switch (TREE_CODE (t1))
996 /* Do not remove mode or aliasing information. */
997 if (TYPE_MODE (t1) != TYPE_MODE (t2)
998 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1000 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1001 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
1005 val = function_types_compatible_p (t1, t2);
1010 tree d1 = TYPE_DOMAIN (t1);
1011 tree d2 = TYPE_DOMAIN (t2);
1012 bool d1_variable, d2_variable;
1013 bool d1_zero, d2_zero;
1016 /* Target types must match incl. qualifiers. */
1017 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1018 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
1021 /* Sizes must match unless one is missing or variable. */
1022 if (d1 == 0 || d2 == 0 || d1 == d2)
1025 d1_zero = !TYPE_MAX_VALUE (d1);
1026 d2_zero = !TYPE_MAX_VALUE (d2);
1028 d1_variable = (!d1_zero
1029 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1030 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1031 d2_variable = (!d2_zero
1032 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1033 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1034 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1035 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1037 if (d1_variable || d2_variable)
1039 if (d1_zero && d2_zero)
1041 if (d1_zero || d2_zero
1042 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1043 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1052 if (val != 1 && !same_translation_unit_p (t1, t2))
1054 tree a1 = TYPE_ATTRIBUTES (t1);
1055 tree a2 = TYPE_ATTRIBUTES (t2);
1057 if (! attribute_list_contained (a1, a2)
1058 && ! attribute_list_contained (a2, a1))
1062 return tagged_types_tu_compatible_p (t1, t2);
1063 val = tagged_types_tu_compatible_p (t1, t2);
1068 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1069 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
1075 return attrval == 2 && val == 1 ? 2 : val;
1078 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
1079 ignoring their qualifiers. */
1082 comp_target_types (tree ttl, tree ttr)
1087 /* Do not lose qualifiers on element types of array types that are
1088 pointer targets by taking their TYPE_MAIN_VARIANT. */
1089 mvl = TREE_TYPE (ttl);
1090 mvr = TREE_TYPE (ttr);
1091 if (TREE_CODE (mvl) != ARRAY_TYPE)
1092 mvl = TYPE_MAIN_VARIANT (mvl);
1093 if (TREE_CODE (mvr) != ARRAY_TYPE)
1094 mvr = TYPE_MAIN_VARIANT (mvr);
1095 val = comptypes (mvl, mvr);
1098 pedwarn (input_location, OPT_pedantic, "types are not quite compatible");
1102 /* Subroutines of `comptypes'. */
1104 /* Determine whether two trees derive from the same translation unit.
1105 If the CONTEXT chain ends in a null, that tree's context is still
1106 being parsed, so if two trees have context chains ending in null,
1107 they're in the same translation unit. */
1109 same_translation_unit_p (const_tree t1, const_tree t2)
1111 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1112 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1114 case tcc_declaration:
1115 t1 = DECL_CONTEXT (t1); break;
1117 t1 = TYPE_CONTEXT (t1); break;
1118 case tcc_exceptional:
1119 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1120 default: gcc_unreachable ();
1123 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1124 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1126 case tcc_declaration:
1127 t2 = DECL_CONTEXT (t2); break;
1129 t2 = TYPE_CONTEXT (t2); break;
1130 case tcc_exceptional:
1131 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1132 default: gcc_unreachable ();
1138 /* Allocate the seen two types, assuming that they are compatible. */
1140 static struct tagged_tu_seen_cache *
1141 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1143 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1144 tu->next = tagged_tu_seen_base;
1148 tagged_tu_seen_base = tu;
1150 /* The C standard says that two structures in different translation
1151 units are compatible with each other only if the types of their
1152 fields are compatible (among other things). We assume that they
1153 are compatible until proven otherwise when building the cache.
1154 An example where this can occur is:
1159 If we are comparing this against a similar struct in another TU,
1160 and did not assume they were compatible, we end up with an infinite
1166 /* Free the seen types until we get to TU_TIL. */
1169 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1171 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1172 while (tu != tu_til)
1174 const struct tagged_tu_seen_cache *const tu1
1175 = (const struct tagged_tu_seen_cache *) tu;
1177 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1179 tagged_tu_seen_base = tu_til;
1182 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1183 compatible. If the two types are not the same (which has been
1184 checked earlier), this can only happen when multiple translation
1185 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1189 tagged_types_tu_compatible_p (const_tree t1, const_tree t2)
1192 bool needs_warning = false;
1194 /* We have to verify that the tags of the types are the same. This
1195 is harder than it looks because this may be a typedef, so we have
1196 to go look at the original type. It may even be a typedef of a
1198 In the case of compiler-created builtin structs the TYPE_DECL
1199 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1200 while (TYPE_NAME (t1)
1201 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1202 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1203 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1205 while (TYPE_NAME (t2)
1206 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1207 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1208 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1210 /* C90 didn't have the requirement that the two tags be the same. */
1211 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1214 /* C90 didn't say what happened if one or both of the types were
1215 incomplete; we choose to follow C99 rules here, which is that they
1217 if (TYPE_SIZE (t1) == NULL
1218 || TYPE_SIZE (t2) == NULL)
1222 const struct tagged_tu_seen_cache * tts_i;
1223 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1224 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1228 switch (TREE_CODE (t1))
1232 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1233 /* Speed up the case where the type values are in the same order. */
1234 tree tv1 = TYPE_VALUES (t1);
1235 tree tv2 = TYPE_VALUES (t2);
1242 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1244 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1246 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1253 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1257 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1263 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1269 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1271 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1273 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1284 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1285 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1291 /* Speed up the common case where the fields are in the same order. */
1292 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1293 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1297 if (DECL_NAME (s1) != DECL_NAME (s2))
1299 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1301 if (result != 1 && !DECL_NAME (s1))
1309 needs_warning = true;
1311 if (TREE_CODE (s1) == FIELD_DECL
1312 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1313 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1321 tu->val = needs_warning ? 2 : 1;
1325 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1329 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1330 if (DECL_NAME (s1) == DECL_NAME (s2))
1334 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1336 if (result != 1 && !DECL_NAME (s1))
1344 needs_warning = true;
1346 if (TREE_CODE (s1) == FIELD_DECL
1347 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1348 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1360 tu->val = needs_warning ? 2 : 10;
1366 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1368 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1370 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1373 if (TREE_CODE (s1) != TREE_CODE (s2)
1374 || DECL_NAME (s1) != DECL_NAME (s2))
1376 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1380 needs_warning = true;
1382 if (TREE_CODE (s1) == FIELD_DECL
1383 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1384 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1390 tu->val = needs_warning ? 2 : 1;
1399 /* Return 1 if two function types F1 and F2 are compatible.
1400 If either type specifies no argument types,
1401 the other must specify a fixed number of self-promoting arg types.
1402 Otherwise, if one type specifies only the number of arguments,
1403 the other must specify that number of self-promoting arg types.
1404 Otherwise, the argument types must match. */
1407 function_types_compatible_p (const_tree f1, const_tree f2)
1410 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1415 ret1 = TREE_TYPE (f1);
1416 ret2 = TREE_TYPE (f2);
1418 /* 'volatile' qualifiers on a function's return type used to mean
1419 the function is noreturn. */
1420 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1421 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1422 if (TYPE_VOLATILE (ret1))
1423 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1424 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1425 if (TYPE_VOLATILE (ret2))
1426 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1427 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1428 val = comptypes_internal (ret1, ret2);
1432 args1 = TYPE_ARG_TYPES (f1);
1433 args2 = TYPE_ARG_TYPES (f2);
1435 /* An unspecified parmlist matches any specified parmlist
1436 whose argument types don't need default promotions. */
1440 if (!self_promoting_args_p (args2))
1442 /* If one of these types comes from a non-prototype fn definition,
1443 compare that with the other type's arglist.
1444 If they don't match, ask for a warning (but no error). */
1445 if (TYPE_ACTUAL_ARG_TYPES (f1)
1446 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1452 if (!self_promoting_args_p (args1))
1454 if (TYPE_ACTUAL_ARG_TYPES (f2)
1455 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1460 /* Both types have argument lists: compare them and propagate results. */
1461 val1 = type_lists_compatible_p (args1, args2);
1462 return val1 != 1 ? val1 : val;
1465 /* Check two lists of types for compatibility,
1466 returning 0 for incompatible, 1 for compatible,
1467 or 2 for compatible with warning. */
1470 type_lists_compatible_p (const_tree args1, const_tree args2)
1472 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1478 tree a1, mv1, a2, mv2;
1479 if (args1 == 0 && args2 == 0)
1481 /* If one list is shorter than the other,
1482 they fail to match. */
1483 if (args1 == 0 || args2 == 0)
1485 mv1 = a1 = TREE_VALUE (args1);
1486 mv2 = a2 = TREE_VALUE (args2);
1487 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1488 mv1 = TYPE_MAIN_VARIANT (mv1);
1489 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1490 mv2 = TYPE_MAIN_VARIANT (mv2);
1491 /* A null pointer instead of a type
1492 means there is supposed to be an argument
1493 but nothing is specified about what type it has.
1494 So match anything that self-promotes. */
1497 if (c_type_promotes_to (a2) != a2)
1502 if (c_type_promotes_to (a1) != a1)
1505 /* If one of the lists has an error marker, ignore this arg. */
1506 else if (TREE_CODE (a1) == ERROR_MARK
1507 || TREE_CODE (a2) == ERROR_MARK)
1509 else if (!(newval = comptypes_internal (mv1, mv2)))
1511 /* Allow wait (union {union wait *u; int *i} *)
1512 and wait (union wait *) to be compatible. */
1513 if (TREE_CODE (a1) == UNION_TYPE
1514 && (TYPE_NAME (a1) == 0
1515 || TYPE_TRANSPARENT_UNION (a1))
1516 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1517 && tree_int_cst_equal (TYPE_SIZE (a1),
1521 for (memb = TYPE_FIELDS (a1);
1522 memb; memb = TREE_CHAIN (memb))
1524 tree mv3 = TREE_TYPE (memb);
1525 if (mv3 && mv3 != error_mark_node
1526 && TREE_CODE (mv3) != ARRAY_TYPE)
1527 mv3 = TYPE_MAIN_VARIANT (mv3);
1528 if (comptypes_internal (mv3, mv2))
1534 else if (TREE_CODE (a2) == UNION_TYPE
1535 && (TYPE_NAME (a2) == 0
1536 || TYPE_TRANSPARENT_UNION (a2))
1537 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1538 && tree_int_cst_equal (TYPE_SIZE (a2),
1542 for (memb = TYPE_FIELDS (a2);
1543 memb; memb = TREE_CHAIN (memb))
1545 tree mv3 = TREE_TYPE (memb);
1546 if (mv3 && mv3 != error_mark_node
1547 && TREE_CODE (mv3) != ARRAY_TYPE)
1548 mv3 = TYPE_MAIN_VARIANT (mv3);
1549 if (comptypes_internal (mv3, mv1))
1559 /* comptypes said ok, but record if it said to warn. */
1563 args1 = TREE_CHAIN (args1);
1564 args2 = TREE_CHAIN (args2);
1568 /* Compute the size to increment a pointer by. */
1571 c_size_in_bytes (const_tree type)
1573 enum tree_code code = TREE_CODE (type);
1575 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1576 return size_one_node;
1578 if (!COMPLETE_OR_VOID_TYPE_P (type))
1580 error ("arithmetic on pointer to an incomplete type");
1581 return size_one_node;
1584 /* Convert in case a char is more than one unit. */
1585 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1586 size_int (TYPE_PRECISION (char_type_node)
1590 /* Return either DECL or its known constant value (if it has one). */
1593 decl_constant_value (tree decl)
1595 if (/* Don't change a variable array bound or initial value to a constant
1596 in a place where a variable is invalid. Note that DECL_INITIAL
1597 isn't valid for a PARM_DECL. */
1598 current_function_decl != 0
1599 && TREE_CODE (decl) != PARM_DECL
1600 && !TREE_THIS_VOLATILE (decl)
1601 && TREE_READONLY (decl)
1602 && DECL_INITIAL (decl) != 0
1603 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1604 /* This is invalid if initial value is not constant.
1605 If it has either a function call, a memory reference,
1606 or a variable, then re-evaluating it could give different results. */
1607 && TREE_CONSTANT (DECL_INITIAL (decl))
1608 /* Check for cases where this is sub-optimal, even though valid. */
1609 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1610 return DECL_INITIAL (decl);
1614 /* Convert the array expression EXP to a pointer. */
1616 array_to_pointer_conversion (tree exp)
1618 tree orig_exp = exp;
1619 tree type = TREE_TYPE (exp);
1621 tree restype = TREE_TYPE (type);
1624 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1626 STRIP_TYPE_NOPS (exp);
1628 if (TREE_NO_WARNING (orig_exp))
1629 TREE_NO_WARNING (exp) = 1;
1631 ptrtype = build_pointer_type (restype);
1633 if (TREE_CODE (exp) == INDIRECT_REF)
1634 return convert (ptrtype, TREE_OPERAND (exp, 0));
1636 if (TREE_CODE (exp) == VAR_DECL)
1638 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1639 ADDR_EXPR because it's the best way of representing what
1640 happens in C when we take the address of an array and place
1641 it in a pointer to the element type. */
1642 adr = build1 (ADDR_EXPR, ptrtype, exp);
1643 if (!c_mark_addressable (exp))
1644 return error_mark_node;
1645 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1649 /* This way is better for a COMPONENT_REF since it can
1650 simplify the offset for a component. */
1651 adr = build_unary_op (EXPR_LOCATION (exp), ADDR_EXPR, exp, 1);
1652 return convert (ptrtype, adr);
1655 /* Convert the function expression EXP to a pointer. */
1657 function_to_pointer_conversion (tree exp)
1659 tree orig_exp = exp;
1661 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1663 STRIP_TYPE_NOPS (exp);
1665 if (TREE_NO_WARNING (orig_exp))
1666 TREE_NO_WARNING (exp) = 1;
1668 return build_unary_op (EXPR_LOCATION (exp), ADDR_EXPR, exp, 0);
1671 /* Perform the default conversion of arrays and functions to pointers.
1672 Return the result of converting EXP. For any other expression, just
1676 default_function_array_conversion (struct c_expr exp)
1678 tree orig_exp = exp.value;
1679 tree type = TREE_TYPE (exp.value);
1680 enum tree_code code = TREE_CODE (type);
1686 bool not_lvalue = false;
1687 bool lvalue_array_p;
1689 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1690 || CONVERT_EXPR_P (exp.value))
1691 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1693 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1695 exp.value = TREE_OPERAND (exp.value, 0);
1698 if (TREE_NO_WARNING (orig_exp))
1699 TREE_NO_WARNING (exp.value) = 1;
1701 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1702 if (!flag_isoc99 && !lvalue_array_p)
1704 /* Before C99, non-lvalue arrays do not decay to pointers.
1705 Normally, using such an array would be invalid; but it can
1706 be used correctly inside sizeof or as a statement expression.
1707 Thus, do not give an error here; an error will result later. */
1711 exp.value = array_to_pointer_conversion (exp.value);
1715 exp.value = function_to_pointer_conversion (exp.value);
1725 /* EXP is an expression of integer type. Apply the integer promotions
1726 to it and return the promoted value. */
1729 perform_integral_promotions (tree exp)
1731 tree type = TREE_TYPE (exp);
1732 enum tree_code code = TREE_CODE (type);
1734 gcc_assert (INTEGRAL_TYPE_P (type));
1736 /* Normally convert enums to int,
1737 but convert wide enums to something wider. */
1738 if (code == ENUMERAL_TYPE)
1740 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1741 TYPE_PRECISION (integer_type_node)),
1742 ((TYPE_PRECISION (type)
1743 >= TYPE_PRECISION (integer_type_node))
1744 && TYPE_UNSIGNED (type)));
1746 return convert (type, exp);
1749 /* ??? This should no longer be needed now bit-fields have their
1751 if (TREE_CODE (exp) == COMPONENT_REF
1752 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1753 /* If it's thinner than an int, promote it like a
1754 c_promoting_integer_type_p, otherwise leave it alone. */
1755 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1756 TYPE_PRECISION (integer_type_node)))
1757 return convert (integer_type_node, exp);
1759 if (c_promoting_integer_type_p (type))
1761 /* Preserve unsignedness if not really getting any wider. */
1762 if (TYPE_UNSIGNED (type)
1763 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1764 return convert (unsigned_type_node, exp);
1766 return convert (integer_type_node, exp);
1773 /* Perform default promotions for C data used in expressions.
1774 Enumeral types or short or char are converted to int.
1775 In addition, manifest constants symbols are replaced by their values. */
1778 default_conversion (tree exp)
1781 tree type = TREE_TYPE (exp);
1782 enum tree_code code = TREE_CODE (type);
1784 /* Functions and arrays have been converted during parsing. */
1785 gcc_assert (code != FUNCTION_TYPE);
1786 if (code == ARRAY_TYPE)
1789 /* Constants can be used directly unless they're not loadable. */
1790 if (TREE_CODE (exp) == CONST_DECL)
1791 exp = DECL_INITIAL (exp);
1793 /* Strip no-op conversions. */
1795 STRIP_TYPE_NOPS (exp);
1797 if (TREE_NO_WARNING (orig_exp))
1798 TREE_NO_WARNING (exp) = 1;
1800 if (code == VOID_TYPE)
1802 error ("void value not ignored as it ought to be");
1803 return error_mark_node;
1806 exp = require_complete_type (exp);
1807 if (exp == error_mark_node)
1808 return error_mark_node;
1810 if (INTEGRAL_TYPE_P (type))
1811 return perform_integral_promotions (exp);
1816 /* Look up COMPONENT in a structure or union DECL.
1818 If the component name is not found, returns NULL_TREE. Otherwise,
1819 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1820 stepping down the chain to the component, which is in the last
1821 TREE_VALUE of the list. Normally the list is of length one, but if
1822 the component is embedded within (nested) anonymous structures or
1823 unions, the list steps down the chain to the component. */
1826 lookup_field (tree decl, tree component)
1828 tree type = TREE_TYPE (decl);
1831 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1832 to the field elements. Use a binary search on this array to quickly
1833 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1834 will always be set for structures which have many elements. */
1836 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1839 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1841 field = TYPE_FIELDS (type);
1843 top = TYPE_LANG_SPECIFIC (type)->s->len;
1844 while (top - bot > 1)
1846 half = (top - bot + 1) >> 1;
1847 field = field_array[bot+half];
1849 if (DECL_NAME (field) == NULL_TREE)
1851 /* Step through all anon unions in linear fashion. */
1852 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1854 field = field_array[bot++];
1855 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1856 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1858 tree anon = lookup_field (field, component);
1861 return tree_cons (NULL_TREE, field, anon);
1865 /* Entire record is only anon unions. */
1869 /* Restart the binary search, with new lower bound. */
1873 if (DECL_NAME (field) == component)
1875 if (DECL_NAME (field) < component)
1881 if (DECL_NAME (field_array[bot]) == component)
1882 field = field_array[bot];
1883 else if (DECL_NAME (field) != component)
1888 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1890 if (DECL_NAME (field) == NULL_TREE
1891 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1892 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1894 tree anon = lookup_field (field, component);
1897 return tree_cons (NULL_TREE, field, anon);
1900 if (DECL_NAME (field) == component)
1904 if (field == NULL_TREE)
1908 return tree_cons (NULL_TREE, field, NULL_TREE);
1911 /* Make an expression to refer to the COMPONENT field of
1912 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1915 build_component_ref (tree datum, tree component)
1917 tree type = TREE_TYPE (datum);
1918 enum tree_code code = TREE_CODE (type);
1921 bool datum_lvalue = lvalue_p (datum);
1923 if (!objc_is_public (datum, component))
1924 return error_mark_node;
1926 /* See if there is a field or component with name COMPONENT. */
1928 if (code == RECORD_TYPE || code == UNION_TYPE)
1930 if (!COMPLETE_TYPE_P (type))
1932 c_incomplete_type_error (NULL_TREE, type);
1933 return error_mark_node;
1936 field = lookup_field (datum, component);
1940 error ("%qT has no member named %qE", type, component);
1941 return error_mark_node;
1944 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1945 This might be better solved in future the way the C++ front
1946 end does it - by giving the anonymous entities each a
1947 separate name and type, and then have build_component_ref
1948 recursively call itself. We can't do that here. */
1951 tree subdatum = TREE_VALUE (field);
1954 bool use_datum_quals;
1956 if (TREE_TYPE (subdatum) == error_mark_node)
1957 return error_mark_node;
1959 /* If this is an rvalue, it does not have qualifiers in C
1960 standard terms and we must avoid propagating such
1961 qualifiers down to a non-lvalue array that is then
1962 converted to a pointer. */
1963 use_datum_quals = (datum_lvalue
1964 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
1966 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1967 if (use_datum_quals)
1968 quals |= TYPE_QUALS (TREE_TYPE (datum));
1969 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1971 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
1973 if (TREE_READONLY (subdatum)
1974 || (use_datum_quals && TREE_READONLY (datum)))
1975 TREE_READONLY (ref) = 1;
1976 if (TREE_THIS_VOLATILE (subdatum)
1977 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
1978 TREE_THIS_VOLATILE (ref) = 1;
1980 if (TREE_DEPRECATED (subdatum))
1981 warn_deprecated_use (subdatum);
1985 field = TREE_CHAIN (field);
1991 else if (code != ERROR_MARK)
1992 error ("request for member %qE in something not a structure or union",
1995 return error_mark_node;
1998 /* Given an expression PTR for a pointer, return an expression
1999 for the value pointed to.
2000 ERRORSTRING is the name of the operator to appear in error messages.
2002 LOC is the location to use for the generated tree. */
2005 build_indirect_ref (location_t loc, tree ptr, const char *errorstring)
2007 tree pointer = default_conversion (ptr);
2008 tree type = TREE_TYPE (pointer);
2011 if (TREE_CODE (type) == POINTER_TYPE)
2013 if (CONVERT_EXPR_P (pointer)
2014 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2016 /* If a warning is issued, mark it to avoid duplicates from
2017 the backend. This only needs to be done at
2018 warn_strict_aliasing > 2. */
2019 if (warn_strict_aliasing > 2)
2020 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2021 type, TREE_OPERAND (pointer, 0)))
2022 TREE_NO_WARNING (pointer) = 1;
2025 if (TREE_CODE (pointer) == ADDR_EXPR
2026 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2027 == TREE_TYPE (type)))
2029 ref = TREE_OPERAND (pointer, 0);
2030 protected_set_expr_location (ref, loc);
2035 tree t = TREE_TYPE (type);
2037 ref = build1 (INDIRECT_REF, t, pointer);
2039 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2041 error_at (loc, "dereferencing pointer to incomplete type");
2042 return error_mark_node;
2044 if (VOID_TYPE_P (t) && skip_evaluation == 0)
2045 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2047 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2048 so that we get the proper error message if the result is used
2049 to assign to. Also, &* is supposed to be a no-op.
2050 And ANSI C seems to specify that the type of the result
2051 should be the const type. */
2052 /* A de-reference of a pointer to const is not a const. It is valid
2053 to change it via some other pointer. */
2054 TREE_READONLY (ref) = TYPE_READONLY (t);
2055 TREE_SIDE_EFFECTS (ref)
2056 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2057 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2058 protected_set_expr_location (ref, loc);
2062 else if (TREE_CODE (pointer) != ERROR_MARK)
2064 "invalid type argument of %qs (have %qT)", errorstring, type);
2065 return error_mark_node;
2068 /* This handles expressions of the form "a[i]", which denotes
2071 This is logically equivalent in C to *(a+i), but we may do it differently.
2072 If A is a variable or a member, we generate a primitive ARRAY_REF.
2073 This avoids forcing the array out of registers, and can work on
2074 arrays that are not lvalues (for example, members of structures returned
2077 LOC is the location to use for the returned expression. */
2080 build_array_ref (tree array, tree index, location_t loc)
2083 bool swapped = false;
2084 if (TREE_TYPE (array) == error_mark_node
2085 || TREE_TYPE (index) == error_mark_node)
2086 return error_mark_node;
2088 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2089 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2092 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2093 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2095 error_at (loc, "subscripted value is neither array nor pointer");
2096 return error_mark_node;
2104 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2106 error_at (loc, "array subscript is not an integer");
2107 return error_mark_node;
2110 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2112 error_at (loc, "subscripted value is pointer to function");
2113 return error_mark_node;
2116 /* ??? Existing practice has been to warn only when the char
2117 index is syntactically the index, not for char[array]. */
2119 warn_array_subscript_with_type_char (index);
2121 /* Apply default promotions *after* noticing character types. */
2122 index = default_conversion (index);
2124 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2126 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2130 /* An array that is indexed by a non-constant
2131 cannot be stored in a register; we must be able to do
2132 address arithmetic on its address.
2133 Likewise an array of elements of variable size. */
2134 if (TREE_CODE (index) != INTEGER_CST
2135 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2136 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2138 if (!c_mark_addressable (array))
2139 return error_mark_node;
2141 /* An array that is indexed by a constant value which is not within
2142 the array bounds cannot be stored in a register either; because we
2143 would get a crash in store_bit_field/extract_bit_field when trying
2144 to access a non-existent part of the register. */
2145 if (TREE_CODE (index) == INTEGER_CST
2146 && TYPE_DOMAIN (TREE_TYPE (array))
2147 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2149 if (!c_mark_addressable (array))
2150 return error_mark_node;
2156 while (TREE_CODE (foo) == COMPONENT_REF)
2157 foo = TREE_OPERAND (foo, 0);
2158 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2159 pedwarn (loc, OPT_pedantic,
2160 "ISO C forbids subscripting %<register%> array");
2161 else if (!flag_isoc99 && !lvalue_p (foo))
2162 pedwarn (loc, OPT_pedantic,
2163 "ISO C90 forbids subscripting non-lvalue array");
2166 type = TREE_TYPE (TREE_TYPE (array));
2167 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2168 /* Array ref is const/volatile if the array elements are
2169 or if the array is. */
2170 TREE_READONLY (rval)
2171 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2172 | TREE_READONLY (array));
2173 TREE_SIDE_EFFECTS (rval)
2174 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2175 | TREE_SIDE_EFFECTS (array));
2176 TREE_THIS_VOLATILE (rval)
2177 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2178 /* This was added by rms on 16 Nov 91.
2179 It fixes vol struct foo *a; a->elts[1]
2180 in an inline function.
2181 Hope it doesn't break something else. */
2182 | TREE_THIS_VOLATILE (array));
2183 ret = require_complete_type (rval);
2184 protected_set_expr_location (ret, loc);
2189 tree ar = default_conversion (array);
2191 if (ar == error_mark_node)
2194 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2195 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2197 return build_indirect_ref
2198 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2203 /* Build an external reference to identifier ID. FUN indicates
2204 whether this will be used for a function call. LOC is the source
2205 location of the identifier. */
2207 build_external_ref (tree id, int fun, location_t loc)
2210 tree decl = lookup_name (id);
2212 /* In Objective-C, an instance variable (ivar) may be preferred to
2213 whatever lookup_name() found. */
2214 decl = objc_lookup_ivar (decl, id);
2216 if (decl && decl != error_mark_node)
2219 /* Implicit function declaration. */
2220 ref = implicitly_declare (id);
2221 else if (decl == error_mark_node)
2222 /* Don't complain about something that's already been
2223 complained about. */
2224 return error_mark_node;
2227 undeclared_variable (id, loc);
2228 return error_mark_node;
2231 if (TREE_TYPE (ref) == error_mark_node)
2232 return error_mark_node;
2234 if (TREE_DEPRECATED (ref))
2235 warn_deprecated_use (ref);
2237 /* Recursive call does not count as usage. */
2238 if (ref != current_function_decl)
2240 TREE_USED (ref) = 1;
2243 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2245 if (!in_sizeof && !in_typeof)
2246 C_DECL_USED (ref) = 1;
2247 else if (DECL_INITIAL (ref) == 0
2248 && DECL_EXTERNAL (ref)
2249 && !TREE_PUBLIC (ref))
2250 record_maybe_used_decl (ref);
2253 if (TREE_CODE (ref) == CONST_DECL)
2255 used_types_insert (TREE_TYPE (ref));
2256 ref = DECL_INITIAL (ref);
2257 TREE_CONSTANT (ref) = 1;
2259 else if (current_function_decl != 0
2260 && !DECL_FILE_SCOPE_P (current_function_decl)
2261 && (TREE_CODE (ref) == VAR_DECL
2262 || TREE_CODE (ref) == PARM_DECL
2263 || TREE_CODE (ref) == FUNCTION_DECL))
2265 tree context = decl_function_context (ref);
2267 if (context != 0 && context != current_function_decl)
2268 DECL_NONLOCAL (ref) = 1;
2270 /* C99 6.7.4p3: An inline definition of a function with external
2271 linkage ... shall not contain a reference to an identifier with
2272 internal linkage. */
2273 else if (current_function_decl != 0
2274 && DECL_DECLARED_INLINE_P (current_function_decl)
2275 && DECL_EXTERNAL (current_function_decl)
2276 && VAR_OR_FUNCTION_DECL_P (ref)
2277 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2278 && ! TREE_PUBLIC (ref)
2279 && DECL_CONTEXT (ref) != current_function_decl)
2280 pedwarn (loc, 0, "%qD is static but used in inline function %qD "
2281 "which is not static", ref, current_function_decl);
2286 /* Record details of decls possibly used inside sizeof or typeof. */
2287 struct maybe_used_decl
2291 /* The level seen at (in_sizeof + in_typeof). */
2293 /* The next one at this level or above, or NULL. */
2294 struct maybe_used_decl *next;
2297 static struct maybe_used_decl *maybe_used_decls;
2299 /* Record that DECL, an undefined static function reference seen
2300 inside sizeof or typeof, might be used if the operand of sizeof is
2301 a VLA type or the operand of typeof is a variably modified
2305 record_maybe_used_decl (tree decl)
2307 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2309 t->level = in_sizeof + in_typeof;
2310 t->next = maybe_used_decls;
2311 maybe_used_decls = t;
2314 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2315 USED is false, just discard them. If it is true, mark them used
2316 (if no longer inside sizeof or typeof) or move them to the next
2317 level up (if still inside sizeof or typeof). */
2320 pop_maybe_used (bool used)
2322 struct maybe_used_decl *p = maybe_used_decls;
2323 int cur_level = in_sizeof + in_typeof;
2324 while (p && p->level > cur_level)
2329 C_DECL_USED (p->decl) = 1;
2331 p->level = cur_level;
2335 if (!used || cur_level == 0)
2336 maybe_used_decls = p;
2339 /* Return the result of sizeof applied to EXPR. */
2342 c_expr_sizeof_expr (struct c_expr expr)
2345 if (expr.value == error_mark_node)
2347 ret.value = error_mark_node;
2348 ret.original_code = ERROR_MARK;
2349 pop_maybe_used (false);
2353 bool expr_const_operands = true;
2354 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2355 &expr_const_operands);
2356 ret.value = c_sizeof (TREE_TYPE (folded_expr));
2357 ret.original_code = ERROR_MARK;
2358 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2360 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2361 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2362 folded_expr, ret.value);
2363 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2365 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2370 /* Return the result of sizeof applied to T, a structure for the type
2371 name passed to sizeof (rather than the type itself). */
2374 c_expr_sizeof_type (struct c_type_name *t)
2378 tree type_expr = NULL_TREE;
2379 bool type_expr_const = true;
2380 type = groktypename (t, &type_expr, &type_expr_const);
2381 ret.value = c_sizeof (type);
2382 ret.original_code = ERROR_MARK;
2383 if (type_expr && c_vla_type_p (type))
2385 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2386 type_expr, ret.value);
2387 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2389 pop_maybe_used (type != error_mark_node
2390 ? C_TYPE_VARIABLE_SIZE (type) : false);
2394 /* Build a function call to function FUNCTION with parameters PARAMS.
2395 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2396 TREE_VALUE of each node is a parameter-expression.
2397 FUNCTION's data type may be a function type or a pointer-to-function. */
2400 build_function_call (tree function, tree params)
2402 tree fntype, fundecl = 0;
2403 tree name = NULL_TREE, result;
2409 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2410 STRIP_TYPE_NOPS (function);
2412 /* Convert anything with function type to a pointer-to-function. */
2413 if (TREE_CODE (function) == FUNCTION_DECL)
2415 /* Implement type-directed function overloading for builtins.
2416 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2417 handle all the type checking. The result is a complete expression
2418 that implements this function call. */
2419 tem = resolve_overloaded_builtin (function, params);
2423 name = DECL_NAME (function);
2426 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2427 function = function_to_pointer_conversion (function);
2429 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2430 expressions, like those used for ObjC messenger dispatches. */
2431 function = objc_rewrite_function_call (function, params);
2433 function = c_fully_fold (function, false, NULL);
2435 fntype = TREE_TYPE (function);
2437 if (TREE_CODE (fntype) == ERROR_MARK)
2438 return error_mark_node;
2440 if (!(TREE_CODE (fntype) == POINTER_TYPE
2441 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2443 error ("called object %qE is not a function", function);
2444 return error_mark_node;
2447 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2448 current_function_returns_abnormally = 1;
2450 /* fntype now gets the type of function pointed to. */
2451 fntype = TREE_TYPE (fntype);
2453 /* Convert the parameters to the types declared in the
2454 function prototype, or apply default promotions. */
2456 nargs = list_length (params);
2457 argarray = (tree *) alloca (nargs * sizeof (tree));
2458 nargs = convert_arguments (nargs, argarray, TYPE_ARG_TYPES (fntype),
2459 params, function, fundecl);
2461 return error_mark_node;
2463 /* Check that the function is called through a compatible prototype.
2464 If it is not, replace the call by a trap, wrapped up in a compound
2465 expression if necessary. This has the nice side-effect to prevent
2466 the tree-inliner from generating invalid assignment trees which may
2467 blow up in the RTL expander later. */
2468 if (CONVERT_EXPR_P (function)
2469 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2470 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2471 && !comptypes (fntype, TREE_TYPE (tem)))
2473 tree return_type = TREE_TYPE (fntype);
2474 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2478 /* This situation leads to run-time undefined behavior. We can't,
2479 therefore, simply error unless we can prove that all possible
2480 executions of the program must execute the code. */
2481 if (warning (0, "function called through a non-compatible type"))
2482 /* We can, however, treat "undefined" any way we please.
2483 Call abort to encourage the user to fix the program. */
2484 inform (input_location, "if this code is reached, the program will abort");
2485 /* Before the abort, allow the function arguments to exit or
2487 for (i = 0; i < nargs; i++)
2488 trap = build2 (COMPOUND_EXPR, void_type_node, argarray[i], trap);
2490 if (VOID_TYPE_P (return_type))
2496 if (AGGREGATE_TYPE_P (return_type))
2497 rhs = build_compound_literal (return_type,
2498 build_constructor (return_type, 0),
2501 rhs = fold_convert (return_type, integer_zero_node);
2503 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2507 /* Check that arguments to builtin functions match the expectations. */
2509 && DECL_BUILT_IN (fundecl)
2510 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2511 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2512 return error_mark_node;
2514 /* Check that the arguments to the function are valid. */
2515 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2516 TYPE_ARG_TYPES (fntype));
2518 if (name != NULL_TREE
2519 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2521 if (require_constant_value)
2522 result = fold_build_call_array_initializer (TREE_TYPE (fntype),
2523 function, nargs, argarray);
2525 result = fold_build_call_array (TREE_TYPE (fntype),
2526 function, nargs, argarray);
2527 if (TREE_CODE (result) == NOP_EXPR
2528 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2529 STRIP_TYPE_NOPS (result);
2532 result = build_call_array (TREE_TYPE (fntype),
2533 function, nargs, argarray);
2535 if (VOID_TYPE_P (TREE_TYPE (result)))
2537 return require_complete_type (result);
2540 /* Convert the argument expressions in the list VALUES
2541 to the types in the list TYPELIST. The resulting arguments are
2542 stored in the array ARGARRAY which has size NARGS.
2544 If TYPELIST is exhausted, or when an element has NULL as its type,
2545 perform the default conversions.
2547 PARMLIST is the chain of parm decls for the function being called.
2548 It may be 0, if that info is not available.
2549 It is used only for generating error messages.
2551 FUNCTION is a tree for the called function. It is used only for
2552 error messages, where it is formatted with %qE.
2554 This is also where warnings about wrong number of args are generated.
2556 VALUES is a chain of TREE_LIST nodes with the elements of the list
2557 in the TREE_VALUE slots of those nodes.
2559 Returns the actual number of arguments processed (which may be less
2560 than NARGS in some error situations), or -1 on failure. */
2563 convert_arguments (int nargs, tree *argarray,
2564 tree typelist, tree values, tree function, tree fundecl)
2566 tree typetail, valtail;
2568 const bool type_generic = fundecl
2569 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2570 bool type_generic_remove_excess_precision = false;
2573 /* Change pointer to function to the function itself for
2575 if (TREE_CODE (function) == ADDR_EXPR
2576 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2577 function = TREE_OPERAND (function, 0);
2579 /* Handle an ObjC selector specially for diagnostics. */
2580 selector = objc_message_selector ();
2582 /* For type-generic built-in functions, determine whether excess
2583 precision should be removed (classification) or not
2586 && DECL_BUILT_IN (fundecl)
2587 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2589 switch (DECL_FUNCTION_CODE (fundecl))
2591 case BUILT_IN_ISFINITE:
2592 case BUILT_IN_ISINF:
2593 case BUILT_IN_ISINF_SIGN:
2594 case BUILT_IN_ISNAN:
2595 case BUILT_IN_ISNORMAL:
2596 case BUILT_IN_FPCLASSIFY:
2597 type_generic_remove_excess_precision = true;
2601 type_generic_remove_excess_precision = false;
2606 /* Scan the given expressions and types, producing individual
2607 converted arguments and storing them in ARGARRAY. */
2609 for (valtail = values, typetail = typelist, parmnum = 0;
2611 valtail = TREE_CHAIN (valtail), parmnum++)
2613 tree type = typetail ? TREE_VALUE (typetail) : 0;
2614 tree val = TREE_VALUE (valtail);
2615 tree valtype = TREE_TYPE (val);
2616 tree rname = function;
2617 int argnum = parmnum + 1;
2618 const char *invalid_func_diag;
2619 bool excess_precision = false;
2622 if (type == void_type_node)
2624 error ("too many arguments to function %qE", function);
2628 if (selector && argnum > 2)
2634 npc = null_pointer_constant_p (val);
2636 /* If there is excess precision and a prototype, convert once to
2637 the required type rather than converting via the semantic
2638 type. Likewise without a prototype a float value represented
2639 as long double should be converted once to double. But for
2640 type-generic classification functions excess precision must
2642 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2643 && (type || !type_generic || !type_generic_remove_excess_precision))
2645 val = TREE_OPERAND (val, 0);
2646 excess_precision = true;
2648 val = c_fully_fold (val, false, NULL);
2649 STRIP_TYPE_NOPS (val);
2651 val = require_complete_type (val);
2655 /* Formal parm type is specified by a function prototype. */
2658 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2660 error ("type of formal parameter %d is incomplete", parmnum + 1);
2665 /* Optionally warn about conversions that
2666 differ from the default conversions. */
2667 if (warn_traditional_conversion || warn_traditional)
2669 unsigned int formal_prec = TYPE_PRECISION (type);
2671 if (INTEGRAL_TYPE_P (type)
2672 && TREE_CODE (valtype) == REAL_TYPE)
2673 warning (0, "passing argument %d of %qE as integer "
2674 "rather than floating due to prototype",
2676 if (INTEGRAL_TYPE_P (type)
2677 && TREE_CODE (valtype) == COMPLEX_TYPE)
2678 warning (0, "passing argument %d of %qE as integer "
2679 "rather than complex due to prototype",
2681 else if (TREE_CODE (type) == COMPLEX_TYPE
2682 && TREE_CODE (valtype) == REAL_TYPE)
2683 warning (0, "passing argument %d of %qE as complex "
2684 "rather than floating due to prototype",
2686 else if (TREE_CODE (type) == REAL_TYPE
2687 && INTEGRAL_TYPE_P (valtype))
2688 warning (0, "passing argument %d of %qE as floating "
2689 "rather than integer due to prototype",
2691 else if (TREE_CODE (type) == COMPLEX_TYPE
2692 && INTEGRAL_TYPE_P (valtype))
2693 warning (0, "passing argument %d of %qE as complex "
2694 "rather than integer due to prototype",
2696 else if (TREE_CODE (type) == REAL_TYPE
2697 && TREE_CODE (valtype) == COMPLEX_TYPE)
2698 warning (0, "passing argument %d of %qE as floating "
2699 "rather than complex due to prototype",
2701 /* ??? At some point, messages should be written about
2702 conversions between complex types, but that's too messy
2704 else if (TREE_CODE (type) == REAL_TYPE
2705 && TREE_CODE (valtype) == REAL_TYPE)
2707 /* Warn if any argument is passed as `float',
2708 since without a prototype it would be `double'. */
2709 if (formal_prec == TYPE_PRECISION (float_type_node)
2710 && type != dfloat32_type_node)
2711 warning (0, "passing argument %d of %qE as %<float%> "
2712 "rather than %<double%> due to prototype",
2715 /* Warn if mismatch between argument and prototype
2716 for decimal float types. Warn of conversions with
2717 binary float types and of precision narrowing due to
2719 else if (type != valtype
2720 && (type == dfloat32_type_node
2721 || type == dfloat64_type_node
2722 || type == dfloat128_type_node
2723 || valtype == dfloat32_type_node
2724 || valtype == dfloat64_type_node
2725 || valtype == dfloat128_type_node)
2727 <= TYPE_PRECISION (valtype)
2728 || (type == dfloat128_type_node
2730 != dfloat64_type_node
2732 != dfloat32_type_node)))
2733 || (type == dfloat64_type_node
2735 != dfloat32_type_node))))
2736 warning (0, "passing argument %d of %qE as %qT "
2737 "rather than %qT due to prototype",
2738 argnum, rname, type, valtype);
2741 /* Detect integer changing in width or signedness.
2742 These warnings are only activated with
2743 -Wtraditional-conversion, not with -Wtraditional. */
2744 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2745 && INTEGRAL_TYPE_P (valtype))
2747 tree would_have_been = default_conversion (val);
2748 tree type1 = TREE_TYPE (would_have_been);
2750 if (TREE_CODE (type) == ENUMERAL_TYPE
2751 && (TYPE_MAIN_VARIANT (type)
2752 == TYPE_MAIN_VARIANT (valtype)))
2753 /* No warning if function asks for enum
2754 and the actual arg is that enum type. */
2756 else if (formal_prec != TYPE_PRECISION (type1))
2757 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2758 "with different width due to prototype",
2760 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2762 /* Don't complain if the formal parameter type
2763 is an enum, because we can't tell now whether
2764 the value was an enum--even the same enum. */
2765 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2767 else if (TREE_CODE (val) == INTEGER_CST
2768 && int_fits_type_p (val, type))
2769 /* Change in signedness doesn't matter
2770 if a constant value is unaffected. */
2772 /* If the value is extended from a narrower
2773 unsigned type, it doesn't matter whether we
2774 pass it as signed or unsigned; the value
2775 certainly is the same either way. */
2776 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
2777 && TYPE_UNSIGNED (valtype))
2779 else if (TYPE_UNSIGNED (type))
2780 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2781 "as unsigned due to prototype",
2784 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2785 "as signed due to prototype", argnum, rname);
2789 /* Possibly restore an EXCESS_PRECISION_EXPR for the
2790 sake of better warnings from convert_and_check. */
2791 if (excess_precision)
2792 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
2793 parmval = convert_for_assignment (type, val, ic_argpass, npc,
2797 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2798 && INTEGRAL_TYPE_P (type)
2799 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2800 parmval = default_conversion (parmval);
2802 argarray[parmnum] = parmval;
2804 else if (TREE_CODE (valtype) == REAL_TYPE
2805 && (TYPE_PRECISION (valtype)
2806 < TYPE_PRECISION (double_type_node))
2807 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
2810 argarray[parmnum] = val;
2812 /* Convert `float' to `double'. */
2813 argarray[parmnum] = convert (double_type_node, val);
2815 else if (excess_precision && !type_generic)
2816 /* A "double" argument with excess precision being passed
2817 without a prototype or in variable arguments. */
2818 argarray[parmnum] = convert (valtype, val);
2819 else if ((invalid_func_diag =
2820 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2822 error (invalid_func_diag);
2826 /* Convert `short' and `char' to full-size `int'. */
2827 argarray[parmnum] = default_conversion (val);
2830 typetail = TREE_CHAIN (typetail);
2833 gcc_assert (parmnum == nargs);
2835 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2837 error ("too few arguments to function %qE", function);
2844 /* This is the entry point used by the parser to build unary operators
2845 in the input. CODE, a tree_code, specifies the unary operator, and
2846 ARG is the operand. For unary plus, the C parser currently uses
2847 CONVERT_EXPR for code.
2849 LOC is the location to use for the tree generated.
2853 parser_build_unary_op (enum tree_code code, struct c_expr arg, location_t loc)
2855 struct c_expr result;
2857 result.value = build_unary_op (loc, code, arg.value, 0);
2858 result.original_code = code;
2860 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2861 overflow_warning (result.value);
2866 /* This is the entry point used by the parser to build binary operators
2867 in the input. CODE, a tree_code, specifies the binary operator, and
2868 ARG1 and ARG2 are the operands. In addition to constructing the
2869 expression, we check for operands that were written with other binary
2870 operators in a way that is likely to confuse the user.
2872 LOCATION is the location of the binary operator. */
2875 parser_build_binary_op (location_t location, enum tree_code code,
2876 struct c_expr arg1, struct c_expr arg2)
2878 struct c_expr result;
2880 enum tree_code code1 = arg1.original_code;
2881 enum tree_code code2 = arg2.original_code;
2883 result.value = build_binary_op (location, code,
2884 arg1.value, arg2.value, 1);
2885 result.original_code = code;
2887 if (TREE_CODE (result.value) == ERROR_MARK)
2890 if (location != UNKNOWN_LOCATION)
2891 protected_set_expr_location (result.value, location);
2893 /* Check for cases such as x+y<<z which users are likely
2895 if (warn_parentheses)
2896 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
2898 if (TREE_CODE_CLASS (code1) != tcc_comparison)
2899 warn_logical_operator (code, arg1.value, arg2.value);
2901 /* Warn about comparisons against string literals, with the exception
2902 of testing for equality or inequality of a string literal with NULL. */
2903 if (code == EQ_EXPR || code == NE_EXPR)
2905 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2906 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2907 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2909 else if (TREE_CODE_CLASS (code) == tcc_comparison
2910 && (code1 == STRING_CST || code2 == STRING_CST))
2911 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2913 if (TREE_OVERFLOW_P (result.value)
2914 && !TREE_OVERFLOW_P (arg1.value)
2915 && !TREE_OVERFLOW_P (arg2.value))
2916 overflow_warning (result.value);
2921 /* Return a tree for the difference of pointers OP0 and OP1.
2922 The resulting tree has type int. */
2925 pointer_diff (tree op0, tree op1)
2927 tree restype = ptrdiff_type_node;
2929 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2930 tree con0, con1, lit0, lit1;
2931 tree orig_op1 = op1;
2933 if (TREE_CODE (target_type) == VOID_TYPE)
2934 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
2935 "pointer of type %<void *%> used in subtraction");
2936 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2937 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
2938 "pointer to a function used in subtraction");
2940 /* If the conversion to ptrdiff_type does anything like widening or
2941 converting a partial to an integral mode, we get a convert_expression
2942 that is in the way to do any simplifications.
2943 (fold-const.c doesn't know that the extra bits won't be needed.
2944 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2945 different mode in place.)
2946 So first try to find a common term here 'by hand'; we want to cover
2947 at least the cases that occur in legal static initializers. */
2948 if (CONVERT_EXPR_P (op0)
2949 && (TYPE_PRECISION (TREE_TYPE (op0))
2950 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2951 con0 = TREE_OPERAND (op0, 0);
2954 if (CONVERT_EXPR_P (op1)
2955 && (TYPE_PRECISION (TREE_TYPE (op1))
2956 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2957 con1 = TREE_OPERAND (op1, 0);
2961 if (TREE_CODE (con0) == PLUS_EXPR)
2963 lit0 = TREE_OPERAND (con0, 1);
2964 con0 = TREE_OPERAND (con0, 0);
2967 lit0 = integer_zero_node;
2969 if (TREE_CODE (con1) == PLUS_EXPR)
2971 lit1 = TREE_OPERAND (con1, 1);
2972 con1 = TREE_OPERAND (con1, 0);
2975 lit1 = integer_zero_node;
2977 if (operand_equal_p (con0, con1, 0))
2984 /* First do the subtraction as integers;
2985 then drop through to build the divide operator.
2986 Do not do default conversions on the minus operator
2987 in case restype is a short type. */
2989 op0 = build_binary_op (input_location,
2990 MINUS_EXPR, convert (restype, op0),
2991 convert (restype, op1), 0);
2992 /* This generates an error if op1 is pointer to incomplete type. */
2993 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2994 error ("arithmetic on pointer to an incomplete type");
2996 /* This generates an error if op0 is pointer to incomplete type. */
2997 op1 = c_size_in_bytes (target_type);
2999 /* Divide by the size, in easiest possible way. */
3000 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3003 /* Construct and perhaps optimize a tree representation
3004 for a unary operation. CODE, a tree_code, specifies the operation
3005 and XARG is the operand.
3006 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3007 the default promotions (such as from short to int).
3008 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3009 allows non-lvalues; this is only used to handle conversion of non-lvalue
3010 arrays to pointers in C99.
3012 LOCATION is the location of the operator. */
3015 build_unary_op (location_t location,
3016 enum tree_code code, tree xarg, int flag)
3018 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3021 enum tree_code typecode;
3023 tree ret = error_mark_node;
3024 tree eptype = NULL_TREE;
3025 int noconvert = flag;
3026 const char *invalid_op_diag;
3029 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3031 arg = remove_c_maybe_const_expr (arg);
3033 if (code != ADDR_EXPR)
3034 arg = require_complete_type (arg);
3036 typecode = TREE_CODE (TREE_TYPE (arg));
3037 if (typecode == ERROR_MARK)
3038 return error_mark_node;
3039 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3040 typecode = INTEGER_TYPE;
3042 if ((invalid_op_diag
3043 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3045 error_at (location, invalid_op_diag);
3046 return error_mark_node;
3049 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3051 eptype = TREE_TYPE (arg);
3052 arg = TREE_OPERAND (arg, 0);
3058 /* This is used for unary plus, because a CONVERT_EXPR
3059 is enough to prevent anybody from looking inside for
3060 associativity, but won't generate any code. */
3061 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3062 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3063 || typecode == VECTOR_TYPE))
3065 error_at (location, "wrong type argument to unary plus");
3066 return error_mark_node;
3068 else if (!noconvert)
3069 arg = default_conversion (arg);
3070 arg = non_lvalue (arg);
3074 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3075 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3076 || typecode == VECTOR_TYPE))
3078 error_at (location, "wrong type argument to unary minus");
3079 return error_mark_node;
3081 else if (!noconvert)
3082 arg = default_conversion (arg);
3086 /* ~ works on integer types and non float vectors. */
3087 if (typecode == INTEGER_TYPE
3088 || (typecode == VECTOR_TYPE
3089 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3092 arg = default_conversion (arg);
3094 else if (typecode == COMPLEX_TYPE)
3097 pedwarn (location, OPT_pedantic,
3098 "ISO C does not support %<~%> for complex conjugation");
3100 arg = default_conversion (arg);
3104 error_at (location, "wrong type argument to bit-complement");
3105 return error_mark_node;
3110 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3112 error_at (location, "wrong type argument to abs");
3113 return error_mark_node;
3115 else if (!noconvert)
3116 arg = default_conversion (arg);
3120 /* Conjugating a real value is a no-op, but allow it anyway. */
3121 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3122 || typecode == COMPLEX_TYPE))
3124 error_at (location, "wrong type argument to conjugation");
3125 return error_mark_node;
3127 else if (!noconvert)
3128 arg = default_conversion (arg);
3131 case TRUTH_NOT_EXPR:
3132 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3133 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3134 && typecode != COMPLEX_TYPE)
3137 "wrong type argument to unary exclamation mark");
3138 return error_mark_node;
3140 arg = c_objc_common_truthvalue_conversion (location, arg);
3141 ret = invert_truthvalue (arg);
3142 goto return_build_unary_op;
3145 if (TREE_CODE (arg) == COMPLEX_CST)
3146 ret = TREE_REALPART (arg);
3147 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3148 ret = fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3151 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3152 eptype = TREE_TYPE (eptype);
3153 goto return_build_unary_op;
3156 if (TREE_CODE (arg) == COMPLEX_CST)
3157 ret = TREE_IMAGPART (arg);
3158 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3159 ret = fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3161 ret = omit_one_operand (TREE_TYPE (arg), integer_zero_node, arg);
3162 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3163 eptype = TREE_TYPE (eptype);
3164 goto return_build_unary_op;
3166 case PREINCREMENT_EXPR:
3167 case POSTINCREMENT_EXPR:
3168 case PREDECREMENT_EXPR:
3169 case POSTDECREMENT_EXPR:
3171 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3173 tree inner = build_unary_op (location, code,
3174 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3175 if (inner == error_mark_node)
3176 return error_mark_node;
3177 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3178 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3179 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3180 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3181 goto return_build_unary_op;
3184 /* Complain about anything that is not a true lvalue. */
3185 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3186 || code == POSTINCREMENT_EXPR)
3189 return error_mark_node;
3191 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3192 arg = c_fully_fold (arg, false, NULL);
3194 /* Increment or decrement the real part of the value,
3195 and don't change the imaginary part. */
3196 if (typecode == COMPLEX_TYPE)
3200 pedwarn (location, OPT_pedantic,
3201 "ISO C does not support %<++%> and %<--%> on complex types");
3203 arg = stabilize_reference (arg);
3204 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3205 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3206 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3207 if (real == error_mark_node || imag == error_mark_node)
3208 return error_mark_node;
3209 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3211 goto return_build_unary_op;
3214 /* Report invalid types. */
3216 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3217 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3219 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3220 error_at (location, "wrong type argument to increment");
3222 error_at (location, "wrong type argument to decrement");
3224 return error_mark_node;
3230 argtype = TREE_TYPE (arg);
3232 /* Compute the increment. */
3234 if (typecode == POINTER_TYPE)
3236 /* If pointer target is an undefined struct,
3237 we just cannot know how to do the arithmetic. */
3238 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3240 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3242 "increment of pointer to unknown structure");
3245 "decrement of pointer to unknown structure");
3247 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3248 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3250 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3251 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3252 "wrong type argument to increment");
3254 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3255 "wrong type argument to decrement");
3258 inc = c_size_in_bytes (TREE_TYPE (argtype));
3259 inc = fold_convert (sizetype, inc);
3261 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3263 /* For signed fract types, we invert ++ to -- or
3264 -- to ++, and change inc from 1 to -1, because
3265 it is not possible to represent 1 in signed fract constants.
3266 For unsigned fract types, the result always overflows and
3267 we get an undefined (original) or the maximum value. */
3268 if (code == PREINCREMENT_EXPR)
3269 code = PREDECREMENT_EXPR;
3270 else if (code == PREDECREMENT_EXPR)
3271 code = PREINCREMENT_EXPR;
3272 else if (code == POSTINCREMENT_EXPR)
3273 code = POSTDECREMENT_EXPR;
3274 else /* code == POSTDECREMENT_EXPR */
3275 code = POSTINCREMENT_EXPR;
3277 inc = integer_minus_one_node;
3278 inc = convert (argtype, inc);
3282 inc = integer_one_node;
3283 inc = convert (argtype, inc);
3286 /* Report a read-only lvalue. */
3287 if (TREE_READONLY (arg))
3289 readonly_error (arg,
3290 ((code == PREINCREMENT_EXPR
3291 || code == POSTINCREMENT_EXPR)
3292 ? lv_increment : lv_decrement));
3293 return error_mark_node;
3296 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3297 val = boolean_increment (code, arg);
3299 val = build2 (code, TREE_TYPE (arg), arg, inc);
3300 TREE_SIDE_EFFECTS (val) = 1;
3301 if (TREE_CODE (val) != code)
3302 TREE_NO_WARNING (val) = 1;
3304 goto return_build_unary_op;
3308 /* Note that this operation never does default_conversion. */
3310 /* Let &* cancel out to simplify resulting code. */
3311 if (TREE_CODE (arg) == INDIRECT_REF)
3313 /* Don't let this be an lvalue. */
3314 if (lvalue_p (TREE_OPERAND (arg, 0)))
3315 return non_lvalue (TREE_OPERAND (arg, 0));
3316 ret = TREE_OPERAND (arg, 0);
3317 goto return_build_unary_op;
3320 /* For &x[y], return x+y */
3321 if (TREE_CODE (arg) == ARRAY_REF)
3323 tree op0 = TREE_OPERAND (arg, 0);
3324 if (!c_mark_addressable (op0))
3325 return error_mark_node;
3326 return build_binary_op (location, PLUS_EXPR,
3327 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3328 ? array_to_pointer_conversion (op0)
3330 TREE_OPERAND (arg, 1), 1);
3333 /* Anything not already handled and not a true memory reference
3334 or a non-lvalue array is an error. */
3335 else if (typecode != FUNCTION_TYPE && !flag
3336 && !lvalue_or_else (arg, lv_addressof))
3337 return error_mark_node;
3339 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3341 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3343 tree inner = build_unary_op (location, code,
3344 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3345 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3346 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3347 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3348 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3349 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3350 goto return_build_unary_op;
3353 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3354 argtype = TREE_TYPE (arg);
3356 /* If the lvalue is const or volatile, merge that into the type
3357 to which the address will point. Note that you can't get a
3358 restricted pointer by taking the address of something, so we
3359 only have to deal with `const' and `volatile' here. */
3360 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3361 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3362 argtype = c_build_type_variant (argtype,
3363 TREE_READONLY (arg),
3364 TREE_THIS_VOLATILE (arg));
3366 if (!c_mark_addressable (arg))
3367 return error_mark_node;
3369 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3370 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3372 argtype = build_pointer_type (argtype);
3374 /* ??? Cope with user tricks that amount to offsetof. Delete this
3375 when we have proper support for integer constant expressions. */
3376 val = get_base_address (arg);
3377 if (val && TREE_CODE (val) == INDIRECT_REF
3378 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3380 tree op0 = fold_convert (sizetype, fold_offsetof (arg, val)), op1;
3382 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3383 ret = fold_build2 (POINTER_PLUS_EXPR, argtype, op1, op0);
3384 goto return_build_unary_op;
3387 val = build1 (ADDR_EXPR, argtype, arg);
3390 goto return_build_unary_op;
3397 argtype = TREE_TYPE (arg);
3398 if (TREE_CODE (arg) == INTEGER_CST)
3399 ret = (require_constant_value
3400 ? fold_build1_initializer (code, argtype, arg)
3401 : fold_build1 (code, argtype, arg));
3403 ret = build1 (code, argtype, arg);
3404 return_build_unary_op:
3405 gcc_assert (ret != error_mark_node);
3406 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3407 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3408 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3409 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3410 ret = note_integer_operands (ret);
3412 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3413 protected_set_expr_location (ret, location);
3417 /* Return nonzero if REF is an lvalue valid for this language.
3418 Lvalues can be assigned, unless their type has TYPE_READONLY.
3419 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3422 lvalue_p (const_tree ref)
3424 const enum tree_code code = TREE_CODE (ref);
3431 return lvalue_p (TREE_OPERAND (ref, 0));
3433 case C_MAYBE_CONST_EXPR:
3434 return lvalue_p (TREE_OPERAND (ref, 1));
3436 case COMPOUND_LITERAL_EXPR:
3446 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3447 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3450 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3457 /* Give an error for storing in something that is 'const'. */
3460 readonly_error (tree arg, enum lvalue_use use)
3462 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3464 /* Using this macro rather than (for example) arrays of messages
3465 ensures that all the format strings are checked at compile
3467 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3468 : (use == lv_increment ? (I) \
3469 : (use == lv_decrement ? (D) : (AS))))
3470 if (TREE_CODE (arg) == COMPONENT_REF)
3472 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3473 readonly_error (TREE_OPERAND (arg, 0), use);
3475 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3476 G_("increment of read-only member %qD"),
3477 G_("decrement of read-only member %qD"),
3478 G_("read-only member %qD used as %<asm%> output")),
3479 TREE_OPERAND (arg, 1));
3481 else if (TREE_CODE (arg) == VAR_DECL)
3482 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3483 G_("increment of read-only variable %qD"),
3484 G_("decrement of read-only variable %qD"),
3485 G_("read-only variable %qD used as %<asm%> output")),
3488 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3489 G_("increment of read-only location %qE"),
3490 G_("decrement of read-only location %qE"),
3491 G_("read-only location %qE used as %<asm%> output")),
3496 /* Return nonzero if REF is an lvalue valid for this language;
3497 otherwise, print an error message and return zero. USE says
3498 how the lvalue is being used and so selects the error message. */
3501 lvalue_or_else (const_tree ref, enum lvalue_use use)
3503 int win = lvalue_p (ref);
3511 /* Mark EXP saying that we need to be able to take the
3512 address of it; it should not be allocated in a register.
3513 Returns true if successful. */
3516 c_mark_addressable (tree exp)
3521 switch (TREE_CODE (x))
3524 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3527 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3531 /* ... fall through ... */
3537 x = TREE_OPERAND (x, 0);
3540 case COMPOUND_LITERAL_EXPR:
3542 TREE_ADDRESSABLE (x) = 1;
3549 if (C_DECL_REGISTER (x)
3550 && DECL_NONLOCAL (x))
3552 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3555 ("global register variable %qD used in nested function", x);
3558 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3560 else if (C_DECL_REGISTER (x))
3562 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3563 error ("address of global register variable %qD requested", x);
3565 error ("address of register variable %qD requested", x);
3571 TREE_ADDRESSABLE (x) = 1;
3578 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3579 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3580 if folded to an integer constant then the unselected half may
3581 contain arbitrary operations not normally permitted in constant
3585 build_conditional_expr (tree ifexp, bool ifexp_bcp, tree op1, tree op2)
3589 enum tree_code code1;
3590 enum tree_code code2;
3591 tree result_type = NULL;
3592 tree ep_result_type = NULL;
3593 tree orig_op1 = op1, orig_op2 = op2;
3594 bool int_const, op1_int_operands, op2_int_operands, int_operands;
3595 bool ifexp_int_operands;
3599 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
3600 if (op1_int_operands)
3601 op1 = remove_c_maybe_const_expr (op1);
3602 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
3603 if (op2_int_operands)
3604 op2 = remove_c_maybe_const_expr (op2);
3605 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
3606 if (ifexp_int_operands)
3607 ifexp = remove_c_maybe_const_expr (ifexp);
3609 /* Promote both alternatives. */
3611 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3612 op1 = default_conversion (op1);
3613 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3614 op2 = default_conversion (op2);
3616 if (TREE_CODE (ifexp) == ERROR_MARK
3617 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3618 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3619 return error_mark_node;
3621 type1 = TREE_TYPE (op1);
3622 code1 = TREE_CODE (type1);
3623 type2 = TREE_TYPE (op2);
3624 code2 = TREE_CODE (type2);
3626 /* C90 does not permit non-lvalue arrays in conditional expressions.
3627 In C99 they will be pointers by now. */
3628 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3630 error ("non-lvalue array in conditional expression");
3631 return error_mark_node;
3634 objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE);
3636 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
3637 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3638 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3639 || code1 == COMPLEX_TYPE)
3640 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3641 || code2 == COMPLEX_TYPE))
3643 ep_result_type = c_common_type (type1, type2);
3644 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
3646 op1 = TREE_OPERAND (op1, 0);
3647 type1 = TREE_TYPE (op1);
3648 gcc_assert (TREE_CODE (type1) == code1);
3650 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3652 op2 = TREE_OPERAND (op2, 0);
3653 type2 = TREE_TYPE (op2);
3654 gcc_assert (TREE_CODE (type2) == code2);
3658 /* Quickly detect the usual case where op1 and op2 have the same type
3660 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3663 result_type = type1;
3665 result_type = TYPE_MAIN_VARIANT (type1);
3667 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3668 || code1 == COMPLEX_TYPE)
3669 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3670 || code2 == COMPLEX_TYPE))
3672 result_type = c_common_type (type1, type2);
3674 /* If -Wsign-compare, warn here if type1 and type2 have
3675 different signedness. We'll promote the signed to unsigned
3676 and later code won't know it used to be different.
3677 Do this check on the original types, so that explicit casts
3678 will be considered, but default promotions won't. */
3679 if (!skip_evaluation)
3681 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3682 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3684 if (unsigned_op1 ^ unsigned_op2)
3688 /* Do not warn if the result type is signed, since the
3689 signed type will only be chosen if it can represent
3690 all the values of the unsigned type. */
3691 if (!TYPE_UNSIGNED (result_type))
3695 bool op1_maybe_const = true;
3696 bool op2_maybe_const = true;
3698 /* Do not warn if the signed quantity is an
3699 unsuffixed integer literal (or some static
3700 constant expression involving such literals) and
3701 it is non-negative. This warning requires the
3702 operands to be folded for best results, so do
3703 that folding in this case even without
3704 warn_sign_compare to avoid warning options
3705 possibly affecting code generation. */
3706 op1 = c_fully_fold (op1, require_constant_value,
3708 op2 = c_fully_fold (op2, require_constant_value,
3711 if (warn_sign_compare)
3714 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3716 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3719 warning (OPT_Wsign_compare, "signed and unsigned type in conditional expression");
3721 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
3723 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
3725 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
3727 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
3729 op2 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op2),
3731 C_MAYBE_CONST_EXPR_NON_CONST (op2) = !op2_maybe_const;
3737 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3739 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
3740 pedwarn (input_location, OPT_pedantic,
3741 "ISO C forbids conditional expr with only one void side");
3742 result_type = void_type_node;
3744 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3746 if (comp_target_types (type1, type2))
3747 result_type = common_pointer_type (type1, type2);
3748 else if (null_pointer_constant_p (orig_op1))
3749 result_type = qualify_type (type2, type1);
3750 else if (null_pointer_constant_p (orig_op2))
3751 result_type = qualify_type (type1, type2);
3752 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3754 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3755 pedwarn (input_location, OPT_pedantic,
3756 "ISO C forbids conditional expr between "
3757 "%<void *%> and function pointer");
3758 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3759 TREE_TYPE (type2)));
3761 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3763 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3764 pedwarn (input_location, OPT_pedantic,
3765 "ISO C forbids conditional expr between "
3766 "%<void *%> and function pointer");
3767 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3768 TREE_TYPE (type1)));
3773 pedwarn (input_location, 0,
3774 "pointer type mismatch in conditional expression");
3775 result_type = build_pointer_type (void_type_node);
3778 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3780 if (!null_pointer_constant_p (orig_op2))
3781 pedwarn (input_location, 0,
3782 "pointer/integer type mismatch in conditional expression");
3785 op2 = null_pointer_node;
3787 result_type = type1;
3789 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3791 if (!null_pointer_constant_p (orig_op1))
3792 pedwarn (input_location, 0,
3793 "pointer/integer type mismatch in conditional expression");
3796 op1 = null_pointer_node;
3798 result_type = type2;
3803 if (flag_cond_mismatch)
3804 result_type = void_type_node;
3807 error ("type mismatch in conditional expression");
3808 return error_mark_node;
3812 /* Merge const and volatile flags of the incoming types. */
3814 = build_type_variant (result_type,
3815 TREE_READONLY (op1) || TREE_READONLY (op2),
3816 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3818 if (result_type != TREE_TYPE (op1))
3819 op1 = convert_and_check (result_type, op1);
3820 if (result_type != TREE_TYPE (op2))
3821 op2 = convert_and_check (result_type, op2);
3823 if (ifexp_bcp && ifexp == truthvalue_true_node)
3825 op2_int_operands = true;
3826 op1 = c_fully_fold (op1, require_constant_value, NULL);
3828 if (ifexp_bcp && ifexp == truthvalue_false_node)
3830 op1_int_operands = true;
3831 op2 = c_fully_fold (op2, require_constant_value, NULL);
3833 int_const = int_operands = (ifexp_int_operands
3835 && op2_int_operands);
3838 int_const = ((ifexp == truthvalue_true_node
3839 && TREE_CODE (orig_op1) == INTEGER_CST
3840 && !TREE_OVERFLOW (orig_op1))
3841 || (ifexp == truthvalue_false_node
3842 && TREE_CODE (orig_op2) == INTEGER_CST
3843 && !TREE_OVERFLOW (orig_op2)));
3845 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
3846 ret = fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3849 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
3851 ret = note_integer_operands (ret);
3854 ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret);
3859 /* Return a compound expression that performs two expressions and
3860 returns the value of the second of them. */
3863 build_compound_expr (tree expr1, tree expr2)
3865 bool expr1_int_operands, expr2_int_operands;
3866 tree eptype = NULL_TREE;
3869 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
3870 if (expr1_int_operands)
3871 expr1 = remove_c_maybe_const_expr (expr1);
3872 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
3873 if (expr2_int_operands)
3874 expr2 = remove_c_maybe_const_expr (expr2);
3876 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
3877 expr1 = TREE_OPERAND (expr1, 0);
3878 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
3880 eptype = TREE_TYPE (expr2);
3881 expr2 = TREE_OPERAND (expr2, 0);
3884 if (!TREE_SIDE_EFFECTS (expr1))
3886 /* The left-hand operand of a comma expression is like an expression
3887 statement: with -Wunused, we should warn if it doesn't have
3888 any side-effects, unless it was explicitly cast to (void). */
3889 if (warn_unused_value)
3891 if (VOID_TYPE_P (TREE_TYPE (expr1))
3892 && CONVERT_EXPR_P (expr1))
3894 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3895 && TREE_CODE (expr1) == COMPOUND_EXPR
3896 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
3897 ; /* (void) a, (void) b, c */
3899 warning (OPT_Wunused_value,
3900 "left-hand operand of comma expression has no effect");
3904 /* With -Wunused, we should also warn if the left-hand operand does have
3905 side-effects, but computes a value which is not used. For example, in
3906 `foo() + bar(), baz()' the result of the `+' operator is not used,
3907 so we should issue a warning. */
3908 else if (warn_unused_value)
3909 warn_if_unused_value (expr1, input_location);
3911 if (expr2 == error_mark_node)
3912 return error_mark_node;
3914 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3917 && expr1_int_operands
3918 && expr2_int_operands)
3919 ret = note_integer_operands (ret);
3922 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3927 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3930 build_c_cast (tree type, tree expr)
3934 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3935 expr = TREE_OPERAND (expr, 0);
3939 if (type == error_mark_node || expr == error_mark_node)
3940 return error_mark_node;
3942 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3943 only in <protocol> qualifications. But when constructing cast expressions,
3944 the protocols do matter and must be kept around. */
3945 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3946 return build1 (NOP_EXPR, type, expr);
3948 type = TYPE_MAIN_VARIANT (type);
3950 if (TREE_CODE (type) == ARRAY_TYPE)
3952 error ("cast specifies array type");
3953 return error_mark_node;
3956 if (TREE_CODE (type) == FUNCTION_TYPE)
3958 error ("cast specifies function type");
3959 return error_mark_node;
3962 if (!VOID_TYPE_P (type))
3964 value = require_complete_type (value);
3965 if (value == error_mark_node)
3966 return error_mark_node;
3969 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3971 if (TREE_CODE (type) == RECORD_TYPE
3972 || TREE_CODE (type) == UNION_TYPE)
3973 pedwarn (input_location, OPT_pedantic,
3974 "ISO C forbids casting nonscalar to the same type");
3976 else if (TREE_CODE (type) == UNION_TYPE)
3980 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3981 if (TREE_TYPE (field) != error_mark_node
3982 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3983 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3990 pedwarn (input_location, OPT_pedantic,
3991 "ISO C forbids casts to union type");
3992 t = digest_init (type,
3993 build_constructor_single (type, field, value),
3995 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3998 error ("cast to union type from type not present in union");
3999 return error_mark_node;
4005 if (type == void_type_node)
4006 return build1 (CONVERT_EXPR, type, value);
4008 otype = TREE_TYPE (value);
4010 /* Optionally warn about potentially worrisome casts. */
4013 && TREE_CODE (type) == POINTER_TYPE
4014 && TREE_CODE (otype) == POINTER_TYPE)
4016 tree in_type = type;
4017 tree in_otype = otype;
4021 /* Check that the qualifiers on IN_TYPE are a superset of
4022 the qualifiers of IN_OTYPE. The outermost level of
4023 POINTER_TYPE nodes is uninteresting and we stop as soon
4024 as we hit a non-POINTER_TYPE node on either type. */
4027 in_otype = TREE_TYPE (in_otype);
4028 in_type = TREE_TYPE (in_type);
4030 /* GNU C allows cv-qualified function types. 'const'
4031 means the function is very pure, 'volatile' means it
4032 can't return. We need to warn when such qualifiers
4033 are added, not when they're taken away. */
4034 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4035 && TREE_CODE (in_type) == FUNCTION_TYPE)
4036 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
4038 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
4040 while (TREE_CODE (in_type) == POINTER_TYPE
4041 && TREE_CODE (in_otype) == POINTER_TYPE);
4044 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
4047 /* There are qualifiers present in IN_OTYPE that are not
4048 present in IN_TYPE. */
4049 warning (OPT_Wcast_qual, "cast discards qualifiers from pointer target type");
4052 /* Warn about possible alignment problems. */
4053 if (STRICT_ALIGNMENT
4054 && TREE_CODE (type) == POINTER_TYPE
4055 && TREE_CODE (otype) == POINTER_TYPE
4056 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4057 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4058 /* Don't warn about opaque types, where the actual alignment
4059 restriction is unknown. */
4060 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4061 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4062 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4063 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4064 warning (OPT_Wcast_align,
4065 "cast increases required alignment of target type");
4067 if (TREE_CODE (type) == INTEGER_TYPE
4068 && TREE_CODE (otype) == POINTER_TYPE
4069 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4070 /* Unlike conversion of integers to pointers, where the
4071 warning is disabled for converting constants because
4072 of cases such as SIG_*, warn about converting constant
4073 pointers to integers. In some cases it may cause unwanted
4074 sign extension, and a warning is appropriate. */
4075 warning (OPT_Wpointer_to_int_cast,
4076 "cast from pointer to integer of different size");
4078 if (TREE_CODE (value) == CALL_EXPR
4079 && TREE_CODE (type) != TREE_CODE (otype))
4080 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
4081 "to non-matching type %qT", otype, type);
4083 if (TREE_CODE (type) == POINTER_TYPE
4084 && TREE_CODE (otype) == INTEGER_TYPE
4085 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4086 /* Don't warn about converting any constant. */
4087 && !TREE_CONSTANT (value))
4088 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4089 "of different size");
4091 if (warn_strict_aliasing <= 2)
4092 strict_aliasing_warning (otype, type, expr);
4094 /* If pedantic, warn for conversions between function and object
4095 pointer types, except for converting a null pointer constant
4096 to function pointer type. */
4098 && TREE_CODE (type) == POINTER_TYPE
4099 && TREE_CODE (otype) == POINTER_TYPE
4100 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4101 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4102 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4103 "conversion of function pointer to object pointer type");
4106 && TREE_CODE (type) == POINTER_TYPE
4107 && TREE_CODE (otype) == POINTER_TYPE
4108 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4109 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4110 && !null_pointer_constant_p (value))
4111 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4112 "conversion of object pointer to function pointer type");
4115 value = convert (type, value);
4117 /* Ignore any integer overflow caused by the cast. */
4118 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4120 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4122 if (!TREE_OVERFLOW (value))
4124 /* Avoid clobbering a shared constant. */
4125 value = copy_node (value);
4126 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4129 else if (TREE_OVERFLOW (value))
4130 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4131 value = build_int_cst_wide (TREE_TYPE (value),
4132 TREE_INT_CST_LOW (value),
4133 TREE_INT_CST_HIGH (value));
4137 /* Don't let a cast be an lvalue. */
4139 value = non_lvalue (value);
4141 /* Don't allow the results of casting to floating-point or complex
4142 types be confused with actual constants, or casts involving
4143 integer and pointer types other than direct integer-to-integer
4144 and integer-to-pointer be confused with integer constant
4145 expressions and null pointer constants. */
4146 if (TREE_CODE (value) == REAL_CST
4147 || TREE_CODE (value) == COMPLEX_CST
4148 || (TREE_CODE (value) == INTEGER_CST
4149 && !((TREE_CODE (expr) == INTEGER_CST
4150 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4151 || TREE_CODE (expr) == REAL_CST
4152 || TREE_CODE (expr) == COMPLEX_CST)))
4153 value = build1 (NOP_EXPR, type, value);
4158 /* Interpret a cast of expression EXPR to type TYPE. */
4160 c_cast_expr (struct c_type_name *type_name, tree expr)
4163 tree type_expr = NULL_TREE;
4164 bool type_expr_const = true;
4166 int saved_wsp = warn_strict_prototypes;
4168 /* This avoids warnings about unprototyped casts on
4169 integers. E.g. "#define SIG_DFL (void(*)())0". */
4170 if (TREE_CODE (expr) == INTEGER_CST)
4171 warn_strict_prototypes = 0;
4172 type = groktypename (type_name, &type_expr, &type_expr_const);
4173 warn_strict_prototypes = saved_wsp;
4175 ret = build_c_cast (type, expr);
4178 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4179 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4184 /* Build an assignment expression of lvalue LHS from value RHS.
4185 MODIFYCODE is the code for a binary operator that we use
4186 to combine the old value of LHS with RHS to get the new value.
4187 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4189 LOCATION is the location of the MODIFYCODE operator. */
4192 build_modify_expr (location_t location,
4193 tree lhs, enum tree_code modifycode, tree rhs)
4197 tree rhs_semantic_type = NULL_TREE;
4198 tree lhstype = TREE_TYPE (lhs);
4199 tree olhstype = lhstype;
4202 /* Types that aren't fully specified cannot be used in assignments. */
4203 lhs = require_complete_type (lhs);
4205 /* Avoid duplicate error messages from operands that had errors. */
4206 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4207 return error_mark_node;
4209 if (!lvalue_or_else (lhs, lv_assign))
4210 return error_mark_node;
4212 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4214 rhs_semantic_type = TREE_TYPE (rhs);
4215 rhs = TREE_OPERAND (rhs, 0);
4220 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4222 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4224 if (inner == error_mark_node)
4225 return error_mark_node;
4226 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4227 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4228 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4229 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4230 protected_set_expr_location (result, location);
4234 /* If a binary op has been requested, combine the old LHS value with the RHS
4235 producing the value we should actually store into the LHS. */
4237 if (modifycode != NOP_EXPR)
4239 lhs = c_fully_fold (lhs, false, NULL);
4240 lhs = stabilize_reference (lhs);
4241 newrhs = build_binary_op (location,
4242 modifycode, lhs, rhs, 1);
4245 /* Give an error for storing in something that is 'const'. */
4247 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
4248 || ((TREE_CODE (lhstype) == RECORD_TYPE
4249 || TREE_CODE (lhstype) == UNION_TYPE)
4250 && C_TYPE_FIELDS_READONLY (lhstype)))
4252 readonly_error (lhs, lv_assign);
4253 return error_mark_node;
4256 /* If storing into a structure or union member,
4257 it has probably been given type `int'.
4258 Compute the type that would go with
4259 the actual amount of storage the member occupies. */
4261 if (TREE_CODE (lhs) == COMPONENT_REF
4262 && (TREE_CODE (lhstype) == INTEGER_TYPE
4263 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4264 || TREE_CODE (lhstype) == REAL_TYPE
4265 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4266 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4268 /* If storing in a field that is in actuality a short or narrower than one,
4269 we must store in the field in its actual type. */
4271 if (lhstype != TREE_TYPE (lhs))
4273 lhs = copy_node (lhs);
4274 TREE_TYPE (lhs) = lhstype;
4277 /* Convert new value to destination type. Fold it first, then
4278 restore any excess precision information, for the sake of
4279 conversion warnings. */
4281 npc = null_pointer_constant_p (newrhs);
4282 newrhs = c_fully_fold (newrhs, false, NULL);
4283 if (rhs_semantic_type)
4284 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4285 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign, npc,
4286 NULL_TREE, NULL_TREE, 0);
4287 if (TREE_CODE (newrhs) == ERROR_MARK)
4288 return error_mark_node;
4290 /* Emit ObjC write barrier, if necessary. */
4291 if (c_dialect_objc () && flag_objc_gc)
4293 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4296 protected_set_expr_location (result, location);
4301 /* Scan operands. */
4303 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4304 TREE_SIDE_EFFECTS (result) = 1;
4305 protected_set_expr_location (result, location);
4307 /* If we got the LHS in a different type for storing in,
4308 convert the result back to the nominal type of LHS
4309 so that the value we return always has the same type
4310 as the LHS argument. */
4312 if (olhstype == TREE_TYPE (result))
4315 result = convert_for_assignment (olhstype, result, ic_assign, false,
4316 NULL_TREE, NULL_TREE, 0);
4317 protected_set_expr_location (result, location);
4321 /* Convert value RHS to type TYPE as preparation for an assignment
4322 to an lvalue of type TYPE. NULL_POINTER_CONSTANT says whether RHS
4323 was a null pointer constant before any folding.
4324 The real work of conversion is done by `convert'.
4325 The purpose of this function is to generate error messages
4326 for assignments that are not allowed in C.
4327 ERRTYPE says whether it is argument passing, assignment,
4328 initialization or return.
4330 FUNCTION is a tree for the function being called.
4331 PARMNUM is the number of the argument, for printing in error messages. */
4334 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
4335 bool null_pointer_constant,
4336 tree fundecl, tree function, int parmnum)
4338 enum tree_code codel = TREE_CODE (type);
4339 tree orig_rhs = rhs;
4341 enum tree_code coder;
4342 tree rname = NULL_TREE;
4343 bool objc_ok = false;
4345 if (errtype == ic_argpass)
4348 /* Change pointer to function to the function itself for
4350 if (TREE_CODE (function) == ADDR_EXPR
4351 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4352 function = TREE_OPERAND (function, 0);
4354 /* Handle an ObjC selector specially for diagnostics. */
4355 selector = objc_message_selector ();
4357 if (selector && parmnum > 2)
4364 /* This macro is used to emit diagnostics to ensure that all format
4365 strings are complete sentences, visible to gettext and checked at
4367 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4372 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4373 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4374 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4375 "expected %qT but argument is of type %qT", \
4379 pedwarn (LOCATION, OPT, AS); \
4382 pedwarn (LOCATION, OPT, IN); \
4385 pedwarn (LOCATION, OPT, RE); \
4388 gcc_unreachable (); \
4392 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4393 rhs = TREE_OPERAND (rhs, 0);
4395 rhstype = TREE_TYPE (rhs);
4396 coder = TREE_CODE (rhstype);
4398 if (coder == ERROR_MARK)
4399 return error_mark_node;
4401 if (c_dialect_objc ())
4424 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4427 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4430 if (coder == VOID_TYPE)
4432 /* Except for passing an argument to an unprototyped function,
4433 this is a constraint violation. When passing an argument to
4434 an unprototyped function, it is compile-time undefined;
4435 making it a constraint in that case was rejected in
4437 error ("void value not ignored as it ought to be");
4438 return error_mark_node;
4440 rhs = require_complete_type (rhs);
4441 if (rhs == error_mark_node)
4442 return error_mark_node;
4443 /* A type converts to a reference to it.
4444 This code doesn't fully support references, it's just for the
4445 special case of va_start and va_copy. */
4446 if (codel == REFERENCE_TYPE
4447 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4449 if (!lvalue_p (rhs))
4451 error ("cannot pass rvalue to reference parameter");
4452 return error_mark_node;
4454 if (!c_mark_addressable (rhs))
4455 return error_mark_node;
4456 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4458 /* We already know that these two types are compatible, but they
4459 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4460 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4461 likely to be va_list, a typedef to __builtin_va_list, which
4462 is different enough that it will cause problems later. */
4463 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4464 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4466 rhs = build1 (NOP_EXPR, type, rhs);
4469 /* Some types can interconvert without explicit casts. */
4470 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4471 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4472 return convert (type, rhs);
4473 /* Arithmetic types all interconvert, and enum is treated like int. */
4474 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4475 || codel == FIXED_POINT_TYPE
4476 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4477 || codel == BOOLEAN_TYPE)
4478 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4479 || coder == FIXED_POINT_TYPE
4480 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4481 || coder == BOOLEAN_TYPE))
4484 bool save = in_late_binary_op;
4485 if (codel == BOOLEAN_TYPE)
4486 in_late_binary_op = true;
4487 ret = convert_and_check (type, orig_rhs);
4488 if (codel == BOOLEAN_TYPE)
4489 in_late_binary_op = save;
4493 /* Aggregates in different TUs might need conversion. */
4494 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4496 && comptypes (type, rhstype))
4497 return convert_and_check (type, rhs);
4499 /* Conversion to a transparent union from its member types.
4500 This applies only to function arguments. */
4501 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4502 && errtype == ic_argpass)
4504 tree memb, marginal_memb = NULL_TREE;
4506 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4508 tree memb_type = TREE_TYPE (memb);
4510 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4511 TYPE_MAIN_VARIANT (rhstype)))
4514 if (TREE_CODE (memb_type) != POINTER_TYPE)
4517 if (coder == POINTER_TYPE)
4519 tree ttl = TREE_TYPE (memb_type);
4520 tree ttr = TREE_TYPE (rhstype);
4522 /* Any non-function converts to a [const][volatile] void *
4523 and vice versa; otherwise, targets must be the same.
4524 Meanwhile, the lhs target must have all the qualifiers of
4526 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4527 || comp_target_types (memb_type, rhstype))
4529 /* If this type won't generate any warnings, use it. */
4530 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4531 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4532 && TREE_CODE (ttl) == FUNCTION_TYPE)
4533 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4534 == TYPE_QUALS (ttr))
4535 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4536 == TYPE_QUALS (ttl))))
4539 /* Keep looking for a better type, but remember this one. */
4541 marginal_memb = memb;
4545 /* Can convert integer zero to any pointer type. */
4546 if (null_pointer_constant)
4548 rhs = null_pointer_node;
4553 if (memb || marginal_memb)
4557 /* We have only a marginally acceptable member type;
4558 it needs a warning. */
4559 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4560 tree ttr = TREE_TYPE (rhstype);
4562 /* Const and volatile mean something different for function
4563 types, so the usual warnings are not appropriate. */
4564 if (TREE_CODE (ttr) == FUNCTION_TYPE
4565 && TREE_CODE (ttl) == FUNCTION_TYPE)
4567 /* Because const and volatile on functions are
4568 restrictions that say the function will not do
4569 certain things, it is okay to use a const or volatile
4570 function where an ordinary one is wanted, but not
4572 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4573 WARN_FOR_ASSIGNMENT (input_location, 0,
4574 G_("passing argument %d of %qE "
4575 "makes qualified function "
4576 "pointer from unqualified"),
4577 G_("assignment makes qualified "
4578 "function pointer from "
4580 G_("initialization makes qualified "
4581 "function pointer from "
4583 G_("return makes qualified function "
4584 "pointer from unqualified"));
4586 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4587 WARN_FOR_ASSIGNMENT (input_location, 0,
4588 G_("passing argument %d of %qE discards "
4589 "qualifiers from pointer target type"),
4590 G_("assignment discards qualifiers "
4591 "from pointer target type"),
4592 G_("initialization discards qualifiers "
4593 "from pointer target type"),
4594 G_("return discards qualifiers from "
4595 "pointer target type"));
4597 memb = marginal_memb;
4600 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
4601 pedwarn (input_location, OPT_pedantic,
4602 "ISO C prohibits argument conversion to union type");
4604 rhs = fold_convert (TREE_TYPE (memb), rhs);
4605 return build_constructor_single (type, memb, rhs);
4609 /* Conversions among pointers */
4610 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4611 && (coder == codel))
4613 tree ttl = TREE_TYPE (type);
4614 tree ttr = TREE_TYPE (rhstype);
4617 bool is_opaque_pointer;
4618 int target_cmp = 0; /* Cache comp_target_types () result. */
4620 if (TREE_CODE (mvl) != ARRAY_TYPE)
4621 mvl = TYPE_MAIN_VARIANT (mvl);
4622 if (TREE_CODE (mvr) != ARRAY_TYPE)
4623 mvr = TYPE_MAIN_VARIANT (mvr);
4624 /* Opaque pointers are treated like void pointers. */
4625 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
4627 /* C++ does not allow the implicit conversion void* -> T*. However,
4628 for the purpose of reducing the number of false positives, we
4629 tolerate the special case of
4633 where NULL is typically defined in C to be '(void *) 0'. */
4634 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4635 warning (OPT_Wc___compat, "request for implicit conversion from "
4636 "%qT to %qT not permitted in C++", rhstype, type);
4638 /* Check if the right-hand side has a format attribute but the
4639 left-hand side doesn't. */
4640 if (warn_missing_format_attribute
4641 && check_missing_format_attribute (type, rhstype))
4646 warning (OPT_Wmissing_format_attribute,
4647 "argument %d of %qE might be "
4648 "a candidate for a format attribute",
4652 warning (OPT_Wmissing_format_attribute,
4653 "assignment left-hand side might be "
4654 "a candidate for a format attribute");
4657 warning (OPT_Wmissing_format_attribute,
4658 "initialization left-hand side might be "
4659 "a candidate for a format attribute");
4662 warning (OPT_Wmissing_format_attribute,
4663 "return type might be "
4664 "a candidate for a format attribute");
4671 /* Any non-function converts to a [const][volatile] void *
4672 and vice versa; otherwise, targets must be the same.
4673 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4674 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4675 || (target_cmp = comp_target_types (type, rhstype))
4676 || is_opaque_pointer
4677 || (c_common_unsigned_type (mvl)
4678 == c_common_unsigned_type (mvr)))
4681 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4684 && !null_pointer_constant
4685 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4686 WARN_FOR_ASSIGNMENT (input_location, OPT_pedantic,
4687 G_("ISO C forbids passing argument %d of "
4688 "%qE between function pointer "
4690 G_("ISO C forbids assignment between "
4691 "function pointer and %<void *%>"),
4692 G_("ISO C forbids initialization between "
4693 "function pointer and %<void *%>"),
4694 G_("ISO C forbids return between function "
4695 "pointer and %<void *%>"));
4696 /* Const and volatile mean something different for function types,
4697 so the usual warnings are not appropriate. */
4698 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4699 && TREE_CODE (ttl) != FUNCTION_TYPE)
4701 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4703 /* Types differing only by the presence of the 'volatile'
4704 qualifier are acceptable if the 'volatile' has been added
4705 in by the Objective-C EH machinery. */
4706 if (!objc_type_quals_match (ttl, ttr))
4707 WARN_FOR_ASSIGNMENT (input_location, 0,
4708 G_("passing argument %d of %qE discards "
4709 "qualifiers from pointer target type"),
4710 G_("assignment discards qualifiers "
4711 "from pointer target type"),
4712 G_("initialization discards qualifiers "
4713 "from pointer target type"),
4714 G_("return discards qualifiers from "
4715 "pointer target type"));
4717 /* If this is not a case of ignoring a mismatch in signedness,
4719 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4722 /* If there is a mismatch, do warn. */
4723 else if (warn_pointer_sign)
4724 WARN_FOR_ASSIGNMENT (input_location, OPT_Wpointer_sign,
4725 G_("pointer targets in passing argument "
4726 "%d of %qE differ in signedness"),
4727 G_("pointer targets in assignment "
4728 "differ in signedness"),
4729 G_("pointer targets in initialization "
4730 "differ in signedness"),
4731 G_("pointer targets in return differ "
4734 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4735 && TREE_CODE (ttr) == FUNCTION_TYPE)
4737 /* Because const and volatile on functions are restrictions
4738 that say the function will not do certain things,
4739 it is okay to use a const or volatile function
4740 where an ordinary one is wanted, but not vice-versa. */
4741 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4742 WARN_FOR_ASSIGNMENT (input_location, 0,
4743 G_("passing argument %d of %qE makes "
4744 "qualified function pointer "
4745 "from unqualified"),
4746 G_("assignment makes qualified function "
4747 "pointer from unqualified"),
4748 G_("initialization makes qualified "
4749 "function pointer from unqualified"),
4750 G_("return makes qualified function "
4751 "pointer from unqualified"));
4755 /* Avoid warning about the volatile ObjC EH puts on decls. */
4757 WARN_FOR_ASSIGNMENT (input_location, 0,
4758 G_("passing argument %d of %qE from "
4759 "incompatible pointer type"),
4760 G_("assignment from incompatible pointer type"),
4761 G_("initialization from incompatible "
4763 G_("return from incompatible pointer type"));
4765 return convert (type, rhs);
4767 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4769 /* ??? This should not be an error when inlining calls to
4770 unprototyped functions. */
4771 error ("invalid use of non-lvalue array");
4772 return error_mark_node;
4774 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4776 /* An explicit constant 0 can convert to a pointer,
4777 or one that results from arithmetic, even including
4778 a cast to integer type. */
4779 if (!null_pointer_constant)
4780 WARN_FOR_ASSIGNMENT (input_location, 0,
4781 G_("passing argument %d of %qE makes "
4782 "pointer from integer without a cast"),
4783 G_("assignment makes pointer from integer "
4785 G_("initialization makes pointer from "
4786 "integer without a cast"),
4787 G_("return makes pointer from integer "
4790 return convert (type, rhs);
4792 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4794 WARN_FOR_ASSIGNMENT (input_location, 0,
4795 G_("passing argument %d of %qE makes integer "
4796 "from pointer without a cast"),
4797 G_("assignment makes integer from pointer "
4799 G_("initialization makes integer from pointer "
4801 G_("return makes integer from pointer "
4803 return convert (type, rhs);
4805 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4808 bool save = in_late_binary_op;
4809 in_late_binary_op = true;
4810 ret = convert (type, rhs);
4811 in_late_binary_op = save;
4818 error ("incompatible type for argument %d of %qE", parmnum, rname);
4819 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
4820 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
4821 "expected %qT but argument is of type %qT", type, rhstype);
4824 error ("incompatible types when assigning to type %qT from type %qT",
4828 error ("incompatible types when initializing type %qT using type %qT",
4832 error ("incompatible types when returning type %qT but %qT was expected",
4839 return error_mark_node;
4842 /* If VALUE is a compound expr all of whose expressions are constant, then
4843 return its value. Otherwise, return error_mark_node.
4845 This is for handling COMPOUND_EXPRs as initializer elements
4846 which is allowed with a warning when -pedantic is specified. */
4849 valid_compound_expr_initializer (tree value, tree endtype)
4851 if (TREE_CODE (value) == COMPOUND_EXPR)
4853 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4855 return error_mark_node;
4856 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4859 else if (!initializer_constant_valid_p (value, endtype))
4860 return error_mark_node;
4865 /* Perform appropriate conversions on the initial value of a variable,
4866 store it in the declaration DECL,
4867 and print any error messages that are appropriate.
4868 If the init is invalid, store an ERROR_MARK. */
4871 store_init_value (tree decl, tree init)
4876 /* If variable's type was invalidly declared, just ignore it. */
4878 type = TREE_TYPE (decl);
4879 if (TREE_CODE (type) == ERROR_MARK)
4882 /* Digest the specified initializer into an expression. */
4885 npc = null_pointer_constant_p (init);
4886 value = digest_init (type, init, npc, true, TREE_STATIC (decl));
4888 /* Store the expression if valid; else report error. */
4890 if (!in_system_header
4891 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4892 warning (OPT_Wtraditional, "traditional C rejects automatic "
4893 "aggregate initialization");
4895 DECL_INITIAL (decl) = value;
4897 /* ANSI wants warnings about out-of-range constant initializers. */
4898 STRIP_TYPE_NOPS (value);
4899 if (TREE_STATIC (decl))
4900 constant_expression_warning (value);
4902 /* Check if we need to set array size from compound literal size. */
4903 if (TREE_CODE (type) == ARRAY_TYPE
4904 && TYPE_DOMAIN (type) == 0
4905 && value != error_mark_node)
4907 tree inside_init = init;
4909 STRIP_TYPE_NOPS (inside_init);
4910 inside_init = fold (inside_init);
4912 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4914 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4916 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
4918 /* For int foo[] = (int [3]){1}; we need to set array size
4919 now since later on array initializer will be just the
4920 brace enclosed list of the compound literal. */
4921 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4922 TREE_TYPE (decl) = type;
4923 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
4925 layout_decl (cldecl, 0);
4931 /* Methods for storing and printing names for error messages. */
4933 /* Implement a spelling stack that allows components of a name to be pushed
4934 and popped. Each element on the stack is this structure. */
4941 unsigned HOST_WIDE_INT i;
4946 #define SPELLING_STRING 1
4947 #define SPELLING_MEMBER 2
4948 #define SPELLING_BOUNDS 3
4950 static struct spelling *spelling; /* Next stack element (unused). */
4951 static struct spelling *spelling_base; /* Spelling stack base. */
4952 static int spelling_size; /* Size of the spelling stack. */
4954 /* Macros to save and restore the spelling stack around push_... functions.
4955 Alternative to SAVE_SPELLING_STACK. */
4957 #define SPELLING_DEPTH() (spelling - spelling_base)
4958 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4960 /* Push an element on the spelling stack with type KIND and assign VALUE
4963 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4965 int depth = SPELLING_DEPTH (); \
4967 if (depth >= spelling_size) \
4969 spelling_size += 10; \
4970 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4972 RESTORE_SPELLING_DEPTH (depth); \
4975 spelling->kind = (KIND); \
4976 spelling->MEMBER = (VALUE); \
4980 /* Push STRING on the stack. Printed literally. */
4983 push_string (const char *string)
4985 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4988 /* Push a member name on the stack. Printed as '.' STRING. */
4991 push_member_name (tree decl)
4993 const char *const string
4994 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4995 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4998 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5001 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5003 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5006 /* Compute the maximum size in bytes of the printed spelling. */
5009 spelling_length (void)
5014 for (p = spelling_base; p < spelling; p++)
5016 if (p->kind == SPELLING_BOUNDS)
5019 size += strlen (p->u.s) + 1;
5025 /* Print the spelling to BUFFER and return it. */
5028 print_spelling (char *buffer)
5033 for (p = spelling_base; p < spelling; p++)
5034 if (p->kind == SPELLING_BOUNDS)
5036 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5042 if (p->kind == SPELLING_MEMBER)
5044 for (s = p->u.s; (*d = *s++); d++)
5051 /* Issue an error message for a bad initializer component.
5052 MSGID identifies the message.
5053 The component name is taken from the spelling stack. */
5056 error_init (const char *msgid)
5060 error ("%s", _(msgid));
5061 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5063 error ("(near initialization for %qs)", ofwhat);
5066 /* Issue a pedantic warning for a bad initializer component. OPT is
5067 the option OPT_* (from options.h) controlling this warning or 0 if
5068 it is unconditionally given. MSGID identifies the message. The
5069 component name is taken from the spelling stack. */
5072 pedwarn_init (location_t location, int opt, const char *msgid)
5076 pedwarn (location, opt, "%s", _(msgid));
5077 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5079 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5082 /* Issue a warning for a bad initializer component.
5084 OPT is the OPT_W* value corresponding to the warning option that
5085 controls this warning. MSGID identifies the message. The
5086 component name is taken from the spelling stack. */
5089 warning_init (int opt, const char *msgid)
5093 warning (opt, "%s", _(msgid));
5094 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5096 warning (opt, "(near initialization for %qs)", ofwhat);
5099 /* If TYPE is an array type and EXPR is a parenthesized string
5100 constant, warn if pedantic that EXPR is being used to initialize an
5101 object of type TYPE. */
5104 maybe_warn_string_init (tree type, struct c_expr expr)
5107 && TREE_CODE (type) == ARRAY_TYPE
5108 && TREE_CODE (expr.value) == STRING_CST
5109 && expr.original_code != STRING_CST)
5110 pedwarn_init (input_location, OPT_pedantic,
5111 "array initialized from parenthesized string constant");
5114 /* Digest the parser output INIT as an initializer for type TYPE.
5115 Return a C expression of type TYPE to represent the initial value.
5117 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5119 If INIT is a string constant, STRICT_STRING is true if it is
5120 unparenthesized or we should not warn here for it being parenthesized.
5121 For other types of INIT, STRICT_STRING is not used.
5123 REQUIRE_CONSTANT requests an error if non-constant initializers or
5124 elements are seen. */
5127 digest_init (tree type, tree init, bool null_pointer_constant,
5128 bool strict_string, int require_constant)
5130 enum tree_code code = TREE_CODE (type);
5131 tree inside_init = init;
5132 tree semantic_type = NULL_TREE;
5133 bool maybe_const = true;
5135 if (type == error_mark_node
5137 || init == error_mark_node
5138 || TREE_TYPE (init) == error_mark_node)
5139 return error_mark_node;
5141 STRIP_TYPE_NOPS (inside_init);
5143 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5145 semantic_type = TREE_TYPE (inside_init);
5146 inside_init = TREE_OPERAND (inside_init, 0);
5148 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5149 inside_init = decl_constant_value_for_optimization (inside_init);
5151 /* Initialization of an array of chars from a string constant
5152 optionally enclosed in braces. */
5154 if (code == ARRAY_TYPE && inside_init
5155 && TREE_CODE (inside_init) == STRING_CST)
5157 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5158 /* Note that an array could be both an array of character type
5159 and an array of wchar_t if wchar_t is signed char or unsigned
5161 bool char_array = (typ1 == char_type_node
5162 || typ1 == signed_char_type_node
5163 || typ1 == unsigned_char_type_node);
5164 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5165 bool char16_array = !!comptypes (typ1, char16_type_node);
5166 bool char32_array = !!comptypes (typ1, char32_type_node);
5168 if (char_array || wchar_array || char16_array || char32_array)
5171 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5172 expr.value = inside_init;
5173 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5174 maybe_warn_string_init (type, expr);
5176 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5177 TYPE_MAIN_VARIANT (type)))
5182 if (typ2 != char_type_node)
5184 error_init ("char-array initialized from wide string");
5185 return error_mark_node;
5190 if (typ2 == char_type_node)
5192 error_init ("wide character array initialized from non-wide "
5194 return error_mark_node;
5196 else if (!comptypes(typ1, typ2))
5198 error_init ("wide character array initialized from "
5199 "incompatible wide string");
5200 return error_mark_node;
5204 TREE_TYPE (inside_init) = type;
5205 if (TYPE_DOMAIN (type) != 0
5206 && TYPE_SIZE (type) != 0
5207 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
5208 /* Subtract the size of a single (possibly wide) character
5209 because it's ok to ignore the terminating null char
5210 that is counted in the length of the constant. */
5211 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5212 TREE_STRING_LENGTH (inside_init)
5213 - (TYPE_PRECISION (typ1)
5215 pedwarn_init (input_location, 0,
5216 "initializer-string for array of chars is too long");
5220 else if (INTEGRAL_TYPE_P (typ1))
5222 error_init ("array of inappropriate type initialized "
5223 "from string constant");
5224 return error_mark_node;
5228 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5229 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5230 below and handle as a constructor. */
5231 if (code == VECTOR_TYPE
5232 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5233 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5234 && TREE_CONSTANT (inside_init))
5236 if (TREE_CODE (inside_init) == VECTOR_CST
5237 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5238 TYPE_MAIN_VARIANT (type)))
5241 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5243 unsigned HOST_WIDE_INT ix;
5245 bool constant_p = true;
5247 /* Iterate through elements and check if all constructor
5248 elements are *_CSTs. */
5249 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5250 if (!CONSTANT_CLASS_P (value))
5257 return build_vector_from_ctor (type,
5258 CONSTRUCTOR_ELTS (inside_init));
5262 if (warn_sequence_point)
5263 verify_sequence_points (inside_init);
5265 /* Any type can be initialized
5266 from an expression of the same type, optionally with braces. */
5268 if (inside_init && TREE_TYPE (inside_init) != 0
5269 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5270 TYPE_MAIN_VARIANT (type))
5271 || (code == ARRAY_TYPE
5272 && comptypes (TREE_TYPE (inside_init), type))
5273 || (code == VECTOR_TYPE
5274 && comptypes (TREE_TYPE (inside_init), type))
5275 || (code == POINTER_TYPE
5276 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5277 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5278 TREE_TYPE (type)))))
5280 if (code == POINTER_TYPE)
5282 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5284 if (TREE_CODE (inside_init) == STRING_CST
5285 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5286 inside_init = array_to_pointer_conversion (inside_init);
5289 error_init ("invalid use of non-lvalue array");
5290 return error_mark_node;
5295 if (code == VECTOR_TYPE)
5296 /* Although the types are compatible, we may require a
5298 inside_init = convert (type, inside_init);
5300 if (require_constant
5301 && (code == VECTOR_TYPE || !flag_isoc99)
5302 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5304 /* As an extension, allow initializing objects with static storage
5305 duration with compound literals (which are then treated just as
5306 the brace enclosed list they contain). Also allow this for
5307 vectors, as we can only assign them with compound literals. */
5308 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5309 inside_init = DECL_INITIAL (decl);
5312 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5313 && TREE_CODE (inside_init) != CONSTRUCTOR)
5315 error_init ("array initialized from non-constant array expression");
5316 return error_mark_node;
5319 /* Compound expressions can only occur here if -pedantic or
5320 -pedantic-errors is specified. In the later case, we always want
5321 an error. In the former case, we simply want a warning. */
5322 if (require_constant && pedantic
5323 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5326 = valid_compound_expr_initializer (inside_init,
5327 TREE_TYPE (inside_init));
5328 if (inside_init == error_mark_node)
5329 error_init ("initializer element is not constant");
5331 pedwarn_init (input_location, OPT_pedantic,
5332 "initializer element is not constant");
5333 if (flag_pedantic_errors)
5334 inside_init = error_mark_node;
5336 else if (require_constant
5337 && !initializer_constant_valid_p (inside_init,
5338 TREE_TYPE (inside_init)))
5340 error_init ("initializer element is not constant");
5341 inside_init = error_mark_node;
5343 else if (require_constant && !maybe_const)
5344 pedwarn_init (input_location, 0,
5345 "initializer element is not a constant expression");
5347 /* Added to enable additional -Wmissing-format-attribute warnings. */
5348 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
5349 inside_init = convert_for_assignment (type, inside_init, ic_init,
5350 null_pointer_constant,
5351 NULL_TREE, NULL_TREE, 0);
5355 /* Handle scalar types, including conversions. */
5357 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
5358 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
5359 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
5361 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
5362 && (TREE_CODE (init) == STRING_CST
5363 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
5364 inside_init = init = array_to_pointer_conversion (init);
5366 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
5369 = convert_for_assignment (type, inside_init, ic_init,
5370 null_pointer_constant,
5371 NULL_TREE, NULL_TREE, 0);
5373 /* Check to see if we have already given an error message. */
5374 if (inside_init == error_mark_node)
5376 else if (require_constant && !TREE_CONSTANT (inside_init))
5378 error_init ("initializer element is not constant");
5379 inside_init = error_mark_node;
5381 else if (require_constant
5382 && !initializer_constant_valid_p (inside_init,
5383 TREE_TYPE (inside_init)))
5385 error_init ("initializer element is not computable at load time");
5386 inside_init = error_mark_node;
5388 else if (require_constant && !maybe_const)
5389 pedwarn_init (input_location, 0,
5390 "initializer element is not a constant expression");
5395 /* Come here only for records and arrays. */
5397 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5399 error_init ("variable-sized object may not be initialized");
5400 return error_mark_node;
5403 error_init ("invalid initializer");
5404 return error_mark_node;
5407 /* Handle initializers that use braces. */
5409 /* Type of object we are accumulating a constructor for.
5410 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5411 static tree constructor_type;
5413 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5415 static tree constructor_fields;
5417 /* For an ARRAY_TYPE, this is the specified index
5418 at which to store the next element we get. */
5419 static tree constructor_index;
5421 /* For an ARRAY_TYPE, this is the maximum index. */
5422 static tree constructor_max_index;
5424 /* For a RECORD_TYPE, this is the first field not yet written out. */
5425 static tree constructor_unfilled_fields;
5427 /* For an ARRAY_TYPE, this is the index of the first element
5428 not yet written out. */
5429 static tree constructor_unfilled_index;
5431 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5432 This is so we can generate gaps between fields, when appropriate. */
5433 static tree constructor_bit_index;
5435 /* If we are saving up the elements rather than allocating them,
5436 this is the list of elements so far (in reverse order,
5437 most recent first). */
5438 static VEC(constructor_elt,gc) *constructor_elements;
5440 /* 1 if constructor should be incrementally stored into a constructor chain,
5441 0 if all the elements should be kept in AVL tree. */
5442 static int constructor_incremental;
5444 /* 1 if so far this constructor's elements are all compile-time constants. */
5445 static int constructor_constant;
5447 /* 1 if so far this constructor's elements are all valid address constants. */
5448 static int constructor_simple;
5450 /* 1 if this constructor has an element that cannot be part of a
5451 constant expression. */
5452 static int constructor_nonconst;
5454 /* 1 if this constructor is erroneous so far. */
5455 static int constructor_erroneous;
5457 /* Structure for managing pending initializer elements, organized as an
5462 struct init_node *left, *right;
5463 struct init_node *parent;
5469 /* Tree of pending elements at this constructor level.
5470 These are elements encountered out of order
5471 which belong at places we haven't reached yet in actually
5473 Will never hold tree nodes across GC runs. */
5474 static struct init_node *constructor_pending_elts;
5476 /* The SPELLING_DEPTH of this constructor. */
5477 static int constructor_depth;
5479 /* DECL node for which an initializer is being read.
5480 0 means we are reading a constructor expression
5481 such as (struct foo) {...}. */
5482 static tree constructor_decl;
5484 /* Nonzero if this is an initializer for a top-level decl. */
5485 static int constructor_top_level;
5487 /* Nonzero if there were any member designators in this initializer. */
5488 static int constructor_designated;
5490 /* Nesting depth of designator list. */
5491 static int designator_depth;
5493 /* Nonzero if there were diagnosed errors in this designator list. */
5494 static int designator_erroneous;
5497 /* This stack has a level for each implicit or explicit level of
5498 structuring in the initializer, including the outermost one. It
5499 saves the values of most of the variables above. */
5501 struct constructor_range_stack;
5503 struct constructor_stack
5505 struct constructor_stack *next;
5510 tree unfilled_index;
5511 tree unfilled_fields;
5513 VEC(constructor_elt,gc) *elements;
5514 struct init_node *pending_elts;
5517 /* If value nonzero, this value should replace the entire
5518 constructor at this level. */
5519 struct c_expr replacement_value;
5520 struct constructor_range_stack *range_stack;
5531 static struct constructor_stack *constructor_stack;
5533 /* This stack represents designators from some range designator up to
5534 the last designator in the list. */
5536 struct constructor_range_stack
5538 struct constructor_range_stack *next, *prev;
5539 struct constructor_stack *stack;
5546 static struct constructor_range_stack *constructor_range_stack;
5548 /* This stack records separate initializers that are nested.
5549 Nested initializers can't happen in ANSI C, but GNU C allows them
5550 in cases like { ... (struct foo) { ... } ... }. */
5552 struct initializer_stack
5554 struct initializer_stack *next;
5556 struct constructor_stack *constructor_stack;
5557 struct constructor_range_stack *constructor_range_stack;
5558 VEC(constructor_elt,gc) *elements;
5559 struct spelling *spelling;
5560 struct spelling *spelling_base;
5563 char require_constant_value;
5564 char require_constant_elements;
5567 static struct initializer_stack *initializer_stack;
5569 /* Prepare to parse and output the initializer for variable DECL. */
5572 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5575 struct initializer_stack *p = XNEW (struct initializer_stack);
5577 p->decl = constructor_decl;
5578 p->require_constant_value = require_constant_value;
5579 p->require_constant_elements = require_constant_elements;
5580 p->constructor_stack = constructor_stack;
5581 p->constructor_range_stack = constructor_range_stack;
5582 p->elements = constructor_elements;
5583 p->spelling = spelling;
5584 p->spelling_base = spelling_base;
5585 p->spelling_size = spelling_size;
5586 p->top_level = constructor_top_level;
5587 p->next = initializer_stack;
5588 initializer_stack = p;
5590 constructor_decl = decl;
5591 constructor_designated = 0;
5592 constructor_top_level = top_level;
5594 if (decl != 0 && decl != error_mark_node)
5596 require_constant_value = TREE_STATIC (decl);
5597 require_constant_elements
5598 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5599 /* For a scalar, you can always use any value to initialize,
5600 even within braces. */
5601 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5602 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5603 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5604 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5605 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5609 require_constant_value = 0;
5610 require_constant_elements = 0;
5611 locus = "(anonymous)";
5614 constructor_stack = 0;
5615 constructor_range_stack = 0;
5617 missing_braces_mentioned = 0;
5621 RESTORE_SPELLING_DEPTH (0);
5624 push_string (locus);
5630 struct initializer_stack *p = initializer_stack;
5632 /* Free the whole constructor stack of this initializer. */
5633 while (constructor_stack)
5635 struct constructor_stack *q = constructor_stack;
5636 constructor_stack = q->next;
5640 gcc_assert (!constructor_range_stack);
5642 /* Pop back to the data of the outer initializer (if any). */
5643 free (spelling_base);
5645 constructor_decl = p->decl;
5646 require_constant_value = p->require_constant_value;
5647 require_constant_elements = p->require_constant_elements;
5648 constructor_stack = p->constructor_stack;
5649 constructor_range_stack = p->constructor_range_stack;
5650 constructor_elements = p->elements;
5651 spelling = p->spelling;
5652 spelling_base = p->spelling_base;
5653 spelling_size = p->spelling_size;
5654 constructor_top_level = p->top_level;
5655 initializer_stack = p->next;
5659 /* Call here when we see the initializer is surrounded by braces.
5660 This is instead of a call to push_init_level;
5661 it is matched by a call to pop_init_level.
5663 TYPE is the type to initialize, for a constructor expression.
5664 For an initializer for a decl, TYPE is zero. */
5667 really_start_incremental_init (tree type)
5669 struct constructor_stack *p = XNEW (struct constructor_stack);
5672 type = TREE_TYPE (constructor_decl);
5674 if (targetm.vector_opaque_p (type))
5675 error ("opaque vector types cannot be initialized");
5677 p->type = constructor_type;
5678 p->fields = constructor_fields;
5679 p->index = constructor_index;
5680 p->max_index = constructor_max_index;
5681 p->unfilled_index = constructor_unfilled_index;
5682 p->unfilled_fields = constructor_unfilled_fields;
5683 p->bit_index = constructor_bit_index;
5684 p->elements = constructor_elements;
5685 p->constant = constructor_constant;
5686 p->simple = constructor_simple;
5687 p->nonconst = constructor_nonconst;
5688 p->erroneous = constructor_erroneous;
5689 p->pending_elts = constructor_pending_elts;
5690 p->depth = constructor_depth;
5691 p->replacement_value.value = 0;
5692 p->replacement_value.original_code = ERROR_MARK;
5696 p->incremental = constructor_incremental;
5697 p->designated = constructor_designated;
5699 constructor_stack = p;
5701 constructor_constant = 1;
5702 constructor_simple = 1;
5703 constructor_nonconst = 0;
5704 constructor_depth = SPELLING_DEPTH ();
5705 constructor_elements = 0;
5706 constructor_pending_elts = 0;
5707 constructor_type = type;
5708 constructor_incremental = 1;
5709 constructor_designated = 0;
5710 designator_depth = 0;
5711 designator_erroneous = 0;
5713 if (TREE_CODE (constructor_type) == RECORD_TYPE
5714 || TREE_CODE (constructor_type) == UNION_TYPE)
5716 constructor_fields = TYPE_FIELDS (constructor_type);
5717 /* Skip any nameless bit fields at the beginning. */
5718 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5719 && DECL_NAME (constructor_fields) == 0)
5720 constructor_fields = TREE_CHAIN (constructor_fields);
5722 constructor_unfilled_fields = constructor_fields;
5723 constructor_bit_index = bitsize_zero_node;
5725 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5727 if (TYPE_DOMAIN (constructor_type))
5729 constructor_max_index
5730 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5732 /* Detect non-empty initializations of zero-length arrays. */
5733 if (constructor_max_index == NULL_TREE
5734 && TYPE_SIZE (constructor_type))
5735 constructor_max_index = build_int_cst (NULL_TREE, -1);
5737 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5738 to initialize VLAs will cause a proper error; avoid tree
5739 checking errors as well by setting a safe value. */
5740 if (constructor_max_index
5741 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5742 constructor_max_index = build_int_cst (NULL_TREE, -1);
5745 = convert (bitsizetype,
5746 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5750 constructor_index = bitsize_zero_node;
5751 constructor_max_index = NULL_TREE;
5754 constructor_unfilled_index = constructor_index;
5756 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5758 /* Vectors are like simple fixed-size arrays. */
5759 constructor_max_index =
5760 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5761 constructor_index = bitsize_zero_node;
5762 constructor_unfilled_index = constructor_index;
5766 /* Handle the case of int x = {5}; */
5767 constructor_fields = constructor_type;
5768 constructor_unfilled_fields = constructor_type;
5772 /* Push down into a subobject, for initialization.
5773 If this is for an explicit set of braces, IMPLICIT is 0.
5774 If it is because the next element belongs at a lower level,
5775 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5778 push_init_level (int implicit)
5780 struct constructor_stack *p;
5781 tree value = NULL_TREE;
5783 /* If we've exhausted any levels that didn't have braces,
5784 pop them now. If implicit == 1, this will have been done in
5785 process_init_element; do not repeat it here because in the case
5786 of excess initializers for an empty aggregate this leads to an
5787 infinite cycle of popping a level and immediately recreating
5791 while (constructor_stack->implicit)
5793 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5794 || TREE_CODE (constructor_type) == UNION_TYPE)
5795 && constructor_fields == 0)
5796 process_init_element (pop_init_level (1), true);
5797 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5798 && constructor_max_index
5799 && tree_int_cst_lt (constructor_max_index,
5801 process_init_element (pop_init_level (1), true);
5807 /* Unless this is an explicit brace, we need to preserve previous
5811 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5812 || TREE_CODE (constructor_type) == UNION_TYPE)
5813 && constructor_fields)
5814 value = find_init_member (constructor_fields);
5815 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5816 value = find_init_member (constructor_index);
5819 p = XNEW (struct constructor_stack);
5820 p->type = constructor_type;
5821 p->fields = constructor_fields;
5822 p->index = constructor_index;
5823 p->max_index = constructor_max_index;
5824 p->unfilled_index = constructor_unfilled_index;
5825 p->unfilled_fields = constructor_unfilled_fields;
5826 p->bit_index = constructor_bit_index;
5827 p->elements = constructor_elements;
5828 p->constant = constructor_constant;
5829 p->simple = constructor_simple;
5830 p->nonconst = constructor_nonconst;
5831 p->erroneous = constructor_erroneous;
5832 p->pending_elts = constructor_pending_elts;
5833 p->depth = constructor_depth;
5834 p->replacement_value.value = 0;
5835 p->replacement_value.original_code = ERROR_MARK;
5836 p->implicit = implicit;
5838 p->incremental = constructor_incremental;
5839 p->designated = constructor_designated;
5840 p->next = constructor_stack;
5842 constructor_stack = p;
5844 constructor_constant = 1;
5845 constructor_simple = 1;
5846 constructor_nonconst = 0;
5847 constructor_depth = SPELLING_DEPTH ();
5848 constructor_elements = 0;
5849 constructor_incremental = 1;
5850 constructor_designated = 0;
5851 constructor_pending_elts = 0;
5854 p->range_stack = constructor_range_stack;
5855 constructor_range_stack = 0;
5856 designator_depth = 0;
5857 designator_erroneous = 0;
5860 /* Don't die if an entire brace-pair level is superfluous
5861 in the containing level. */
5862 if (constructor_type == 0)
5864 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5865 || TREE_CODE (constructor_type) == UNION_TYPE)
5867 /* Don't die if there are extra init elts at the end. */
5868 if (constructor_fields == 0)
5869 constructor_type = 0;
5872 constructor_type = TREE_TYPE (constructor_fields);
5873 push_member_name (constructor_fields);
5874 constructor_depth++;
5877 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5879 constructor_type = TREE_TYPE (constructor_type);
5880 push_array_bounds (tree_low_cst (constructor_index, 1));
5881 constructor_depth++;
5884 if (constructor_type == 0)
5886 error_init ("extra brace group at end of initializer");
5887 constructor_fields = 0;
5888 constructor_unfilled_fields = 0;
5892 if (value && TREE_CODE (value) == CONSTRUCTOR)
5894 constructor_constant = TREE_CONSTANT (value);
5895 constructor_simple = TREE_STATIC (value);
5896 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
5897 constructor_elements = CONSTRUCTOR_ELTS (value);
5898 if (!VEC_empty (constructor_elt, constructor_elements)
5899 && (TREE_CODE (constructor_type) == RECORD_TYPE
5900 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5901 set_nonincremental_init ();
5904 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5906 missing_braces_mentioned = 1;
5907 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
5910 if (TREE_CODE (constructor_type) == RECORD_TYPE
5911 || TREE_CODE (constructor_type) == UNION_TYPE)
5913 constructor_fields = TYPE_FIELDS (constructor_type);
5914 /* Skip any nameless bit fields at the beginning. */
5915 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5916 && DECL_NAME (constructor_fields) == 0)
5917 constructor_fields = TREE_CHAIN (constructor_fields);
5919 constructor_unfilled_fields = constructor_fields;
5920 constructor_bit_index = bitsize_zero_node;
5922 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5924 /* Vectors are like simple fixed-size arrays. */
5925 constructor_max_index =
5926 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5927 constructor_index = convert (bitsizetype, integer_zero_node);
5928 constructor_unfilled_index = constructor_index;
5930 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5932 if (TYPE_DOMAIN (constructor_type))
5934 constructor_max_index
5935 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5937 /* Detect non-empty initializations of zero-length arrays. */
5938 if (constructor_max_index == NULL_TREE
5939 && TYPE_SIZE (constructor_type))
5940 constructor_max_index = build_int_cst (NULL_TREE, -1);
5942 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5943 to initialize VLAs will cause a proper error; avoid tree
5944 checking errors as well by setting a safe value. */
5945 if (constructor_max_index
5946 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5947 constructor_max_index = build_int_cst (NULL_TREE, -1);
5950 = convert (bitsizetype,
5951 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5954 constructor_index = bitsize_zero_node;
5956 constructor_unfilled_index = constructor_index;
5957 if (value && TREE_CODE (value) == STRING_CST)
5959 /* We need to split the char/wchar array into individual
5960 characters, so that we don't have to special case it
5962 set_nonincremental_init_from_string (value);
5967 if (constructor_type != error_mark_node)
5968 warning_init (0, "braces around scalar initializer");
5969 constructor_fields = constructor_type;
5970 constructor_unfilled_fields = constructor_type;
5974 /* At the end of an implicit or explicit brace level,
5975 finish up that level of constructor. If a single expression
5976 with redundant braces initialized that level, return the
5977 c_expr structure for that expression. Otherwise, the original_code
5978 element is set to ERROR_MARK.
5979 If we were outputting the elements as they are read, return 0 as the value
5980 from inner levels (process_init_element ignores that),
5981 but return error_mark_node as the value from the outermost level
5982 (that's what we want to put in DECL_INITIAL).
5983 Otherwise, return a CONSTRUCTOR expression as the value. */
5986 pop_init_level (int implicit)
5988 struct constructor_stack *p;
5991 ret.original_code = ERROR_MARK;
5995 /* When we come to an explicit close brace,
5996 pop any inner levels that didn't have explicit braces. */
5997 while (constructor_stack->implicit)
5998 process_init_element (pop_init_level (1), true);
6000 gcc_assert (!constructor_range_stack);
6003 /* Now output all pending elements. */
6004 constructor_incremental = 1;
6005 output_pending_init_elements (1);
6007 p = constructor_stack;
6009 /* Error for initializing a flexible array member, or a zero-length
6010 array member in an inappropriate context. */
6011 if (constructor_type && constructor_fields
6012 && TREE_CODE (constructor_type) == ARRAY_TYPE
6013 && TYPE_DOMAIN (constructor_type)
6014 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6016 /* Silently discard empty initializations. The parser will
6017 already have pedwarned for empty brackets. */
6018 if (integer_zerop (constructor_unfilled_index))
6019 constructor_type = NULL_TREE;
6022 gcc_assert (!TYPE_SIZE (constructor_type));
6024 if (constructor_depth > 2)
6025 error_init ("initialization of flexible array member in a nested context");
6027 pedwarn_init (input_location, OPT_pedantic,
6028 "initialization of a flexible array member");
6030 /* We have already issued an error message for the existence
6031 of a flexible array member not at the end of the structure.
6032 Discard the initializer so that we do not die later. */
6033 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
6034 constructor_type = NULL_TREE;
6038 /* Warn when some struct elements are implicitly initialized to zero. */
6039 if (warn_missing_field_initializers
6041 && TREE_CODE (constructor_type) == RECORD_TYPE
6042 && constructor_unfilled_fields)
6044 /* Do not warn for flexible array members or zero-length arrays. */
6045 while (constructor_unfilled_fields
6046 && (!DECL_SIZE (constructor_unfilled_fields)
6047 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6048 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6050 /* Do not warn if this level of the initializer uses member
6051 designators; it is likely to be deliberate. */
6052 if (constructor_unfilled_fields && !constructor_designated)
6054 push_member_name (constructor_unfilled_fields);
6055 warning_init (OPT_Wmissing_field_initializers,
6056 "missing initializer");
6057 RESTORE_SPELLING_DEPTH (constructor_depth);
6061 /* Pad out the end of the structure. */
6062 if (p->replacement_value.value)
6063 /* If this closes a superfluous brace pair,
6064 just pass out the element between them. */
6065 ret = p->replacement_value;
6066 else if (constructor_type == 0)
6068 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6069 && TREE_CODE (constructor_type) != UNION_TYPE
6070 && TREE_CODE (constructor_type) != ARRAY_TYPE
6071 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6073 /* A nonincremental scalar initializer--just return
6074 the element, after verifying there is just one. */
6075 if (VEC_empty (constructor_elt,constructor_elements))
6077 if (!constructor_erroneous)
6078 error_init ("empty scalar initializer");
6079 ret.value = error_mark_node;
6081 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6083 error_init ("extra elements in scalar initializer");
6084 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6087 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6091 if (constructor_erroneous)
6092 ret.value = error_mark_node;
6095 ret.value = build_constructor (constructor_type,
6096 constructor_elements);
6097 if (constructor_constant)
6098 TREE_CONSTANT (ret.value) = 1;
6099 if (constructor_constant && constructor_simple)
6100 TREE_STATIC (ret.value) = 1;
6101 if (constructor_nonconst)
6102 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6106 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6108 if (constructor_nonconst)
6109 ret.original_code = C_MAYBE_CONST_EXPR;
6110 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6111 ret.original_code = ERROR_MARK;
6114 constructor_type = p->type;
6115 constructor_fields = p->fields;
6116 constructor_index = p->index;
6117 constructor_max_index = p->max_index;
6118 constructor_unfilled_index = p->unfilled_index;
6119 constructor_unfilled_fields = p->unfilled_fields;
6120 constructor_bit_index = p->bit_index;
6121 constructor_elements = p->elements;
6122 constructor_constant = p->constant;
6123 constructor_simple = p->simple;
6124 constructor_nonconst = p->nonconst;
6125 constructor_erroneous = p->erroneous;
6126 constructor_incremental = p->incremental;
6127 constructor_designated = p->designated;
6128 constructor_pending_elts = p->pending_elts;
6129 constructor_depth = p->depth;
6131 constructor_range_stack = p->range_stack;
6132 RESTORE_SPELLING_DEPTH (constructor_depth);
6134 constructor_stack = p->next;
6137 if (ret.value == 0 && constructor_stack == 0)
6138 ret.value = error_mark_node;
6142 /* Common handling for both array range and field name designators.
6143 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6146 set_designator (int array)
6149 enum tree_code subcode;
6151 /* Don't die if an entire brace-pair level is superfluous
6152 in the containing level. */
6153 if (constructor_type == 0)
6156 /* If there were errors in this designator list already, bail out
6158 if (designator_erroneous)
6161 if (!designator_depth)
6163 gcc_assert (!constructor_range_stack);
6165 /* Designator list starts at the level of closest explicit
6167 while (constructor_stack->implicit)
6168 process_init_element (pop_init_level (1), true);
6169 constructor_designated = 1;
6173 switch (TREE_CODE (constructor_type))
6177 subtype = TREE_TYPE (constructor_fields);
6178 if (subtype != error_mark_node)
6179 subtype = TYPE_MAIN_VARIANT (subtype);
6182 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6188 subcode = TREE_CODE (subtype);
6189 if (array && subcode != ARRAY_TYPE)
6191 error_init ("array index in non-array initializer");
6194 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6196 error_init ("field name not in record or union initializer");
6200 constructor_designated = 1;
6201 push_init_level (2);
6205 /* If there are range designators in designator list, push a new designator
6206 to constructor_range_stack. RANGE_END is end of such stack range or
6207 NULL_TREE if there is no range designator at this level. */
6210 push_range_stack (tree range_end)
6212 struct constructor_range_stack *p;
6214 p = GGC_NEW (struct constructor_range_stack);
6215 p->prev = constructor_range_stack;
6217 p->fields = constructor_fields;
6218 p->range_start = constructor_index;
6219 p->index = constructor_index;
6220 p->stack = constructor_stack;
6221 p->range_end = range_end;
6222 if (constructor_range_stack)
6223 constructor_range_stack->next = p;
6224 constructor_range_stack = p;
6227 /* Within an array initializer, specify the next index to be initialized.
6228 FIRST is that index. If LAST is nonzero, then initialize a range
6229 of indices, running from FIRST through LAST. */
6232 set_init_index (tree first, tree last)
6234 if (set_designator (1))
6237 designator_erroneous = 1;
6239 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
6240 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
6242 error_init ("array index in initializer not of integer type");
6246 if (TREE_CODE (first) != INTEGER_CST)
6247 error_init ("nonconstant array index in initializer");
6248 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
6249 error_init ("nonconstant array index in initializer");
6250 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6251 error_init ("array index in non-array initializer");
6252 else if (tree_int_cst_sgn (first) == -1)
6253 error_init ("array index in initializer exceeds array bounds");
6254 else if (constructor_max_index
6255 && tree_int_cst_lt (constructor_max_index, first))
6256 error_init ("array index in initializer exceeds array bounds");
6259 constant_expression_warning (first);
6261 constant_expression_warning (last);
6262 constructor_index = convert (bitsizetype, first);
6266 if (tree_int_cst_equal (first, last))
6268 else if (tree_int_cst_lt (last, first))
6270 error_init ("empty index range in initializer");
6275 last = convert (bitsizetype, last);
6276 if (constructor_max_index != 0
6277 && tree_int_cst_lt (constructor_max_index, last))
6279 error_init ("array index range in initializer exceeds array bounds");
6286 designator_erroneous = 0;
6287 if (constructor_range_stack || last)
6288 push_range_stack (last);
6292 /* Within a struct initializer, specify the next field to be initialized. */
6295 set_init_label (tree fieldname)
6299 if (set_designator (0))
6302 designator_erroneous = 1;
6304 if (TREE_CODE (constructor_type) != RECORD_TYPE
6305 && TREE_CODE (constructor_type) != UNION_TYPE)
6307 error_init ("field name not in record or union initializer");
6311 for (tail = TYPE_FIELDS (constructor_type); tail;
6312 tail = TREE_CHAIN (tail))
6314 if (DECL_NAME (tail) == fieldname)
6319 error ("unknown field %qE specified in initializer", fieldname);
6322 constructor_fields = tail;
6324 designator_erroneous = 0;
6325 if (constructor_range_stack)
6326 push_range_stack (NULL_TREE);
6330 /* Add a new initializer to the tree of pending initializers. PURPOSE
6331 identifies the initializer, either array index or field in a structure.
6332 VALUE is the value of that index or field.
6334 IMPLICIT is true if value comes from pop_init_level (1),
6335 the new initializer has been merged with the existing one
6336 and thus no warnings should be emitted about overriding an
6337 existing initializer. */
6340 add_pending_init (tree purpose, tree value, bool implicit)
6342 struct init_node *p, **q, *r;
6344 q = &constructor_pending_elts;
6347 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6352 if (tree_int_cst_lt (purpose, p->purpose))
6354 else if (tree_int_cst_lt (p->purpose, purpose))
6360 if (TREE_SIDE_EFFECTS (p->value))
6361 warning_init (0, "initialized field with side-effects overwritten");
6362 else if (warn_override_init)
6363 warning_init (OPT_Woverride_init, "initialized field overwritten");
6374 bitpos = bit_position (purpose);
6378 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6380 else if (p->purpose != purpose)
6386 if (TREE_SIDE_EFFECTS (p->value))
6387 warning_init (0, "initialized field with side-effects overwritten");
6388 else if (warn_override_init)
6389 warning_init (OPT_Woverride_init, "initialized field overwritten");
6397 r = GGC_NEW (struct init_node);
6398 r->purpose = purpose;
6409 struct init_node *s;
6413 if (p->balance == 0)
6415 else if (p->balance < 0)
6422 p->left->parent = p;
6439 constructor_pending_elts = r;
6444 struct init_node *t = r->right;
6448 r->right->parent = r;
6453 p->left->parent = p;
6456 p->balance = t->balance < 0;
6457 r->balance = -(t->balance > 0);
6472 constructor_pending_elts = t;
6478 /* p->balance == +1; growth of left side balances the node. */
6483 else /* r == p->right */
6485 if (p->balance == 0)
6486 /* Growth propagation from right side. */
6488 else if (p->balance > 0)
6495 p->right->parent = p;
6512 constructor_pending_elts = r;
6514 else /* r->balance == -1 */
6517 struct init_node *t = r->left;
6521 r->left->parent = r;
6526 p->right->parent = p;
6529 r->balance = (t->balance < 0);
6530 p->balance = -(t->balance > 0);
6545 constructor_pending_elts = t;
6551 /* p->balance == -1; growth of right side balances the node. */
6562 /* Build AVL tree from a sorted chain. */
6565 set_nonincremental_init (void)
6567 unsigned HOST_WIDE_INT ix;
6570 if (TREE_CODE (constructor_type) != RECORD_TYPE
6571 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6574 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6575 add_pending_init (index, value, false);
6576 constructor_elements = 0;
6577 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6579 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6580 /* Skip any nameless bit fields at the beginning. */
6581 while (constructor_unfilled_fields != 0
6582 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6583 && DECL_NAME (constructor_unfilled_fields) == 0)
6584 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6587 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6589 if (TYPE_DOMAIN (constructor_type))
6590 constructor_unfilled_index
6591 = convert (bitsizetype,
6592 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6594 constructor_unfilled_index = bitsize_zero_node;
6596 constructor_incremental = 0;
6599 /* Build AVL tree from a string constant. */
6602 set_nonincremental_init_from_string (tree str)
6604 tree value, purpose, type;
6605 HOST_WIDE_INT val[2];
6606 const char *p, *end;
6607 int byte, wchar_bytes, charwidth, bitpos;
6609 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
6611 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
6612 charwidth = TYPE_PRECISION (char_type_node);
6613 type = TREE_TYPE (constructor_type);
6614 p = TREE_STRING_POINTER (str);
6615 end = p + TREE_STRING_LENGTH (str);
6617 for (purpose = bitsize_zero_node;
6618 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6619 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6621 if (wchar_bytes == 1)
6623 val[1] = (unsigned char) *p++;
6630 for (byte = 0; byte < wchar_bytes; byte++)
6632 if (BYTES_BIG_ENDIAN)
6633 bitpos = (wchar_bytes - byte - 1) * charwidth;
6635 bitpos = byte * charwidth;
6636 val[bitpos < HOST_BITS_PER_WIDE_INT]
6637 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6638 << (bitpos % HOST_BITS_PER_WIDE_INT);
6642 if (!TYPE_UNSIGNED (type))
6644 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6645 if (bitpos < HOST_BITS_PER_WIDE_INT)
6647 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6649 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6653 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6658 else if (val[0] & (((HOST_WIDE_INT) 1)
6659 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6660 val[0] |= ((HOST_WIDE_INT) -1)
6661 << (bitpos - HOST_BITS_PER_WIDE_INT);
6664 value = build_int_cst_wide (type, val[1], val[0]);
6665 add_pending_init (purpose, value, false);
6668 constructor_incremental = 0;
6671 /* Return value of FIELD in pending initializer or zero if the field was
6672 not initialized yet. */
6675 find_init_member (tree field)
6677 struct init_node *p;
6679 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6681 if (constructor_incremental
6682 && tree_int_cst_lt (field, constructor_unfilled_index))
6683 set_nonincremental_init ();
6685 p = constructor_pending_elts;
6688 if (tree_int_cst_lt (field, p->purpose))
6690 else if (tree_int_cst_lt (p->purpose, field))
6696 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6698 tree bitpos = bit_position (field);
6700 if (constructor_incremental
6701 && (!constructor_unfilled_fields
6702 || tree_int_cst_lt (bitpos,
6703 bit_position (constructor_unfilled_fields))))
6704 set_nonincremental_init ();
6706 p = constructor_pending_elts;
6709 if (field == p->purpose)
6711 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6717 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6719 if (!VEC_empty (constructor_elt, constructor_elements)
6720 && (VEC_last (constructor_elt, constructor_elements)->index
6722 return VEC_last (constructor_elt, constructor_elements)->value;
6727 /* "Output" the next constructor element.
6728 At top level, really output it to assembler code now.
6729 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6730 TYPE is the data type that the containing data type wants here.
6731 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6732 If VALUE is a string constant, STRICT_STRING is true if it is
6733 unparenthesized or we should not warn here for it being parenthesized.
6734 For other types of VALUE, STRICT_STRING is not used.
6736 PENDING if non-nil means output pending elements that belong
6737 right after this element. (PENDING is normally 1;
6738 it is 0 while outputting pending elements, to avoid recursion.)
6740 IMPLICIT is true if value comes from pop_init_level (1),
6741 the new initializer has been merged with the existing one
6742 and thus no warnings should be emitted about overriding an
6743 existing initializer. */
6746 output_init_element (tree value, bool strict_string, tree type, tree field,
6747 int pending, bool implicit)
6749 tree semantic_type = NULL_TREE;
6750 constructor_elt *celt;
6751 bool maybe_const = true;
6754 if (type == error_mark_node || value == error_mark_node)
6756 constructor_erroneous = 1;
6759 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6760 && (TREE_CODE (value) == STRING_CST
6761 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6762 && !(TREE_CODE (value) == STRING_CST
6763 && TREE_CODE (type) == ARRAY_TYPE
6764 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6765 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6766 TYPE_MAIN_VARIANT (type)))
6767 value = array_to_pointer_conversion (value);
6769 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6770 && require_constant_value && !flag_isoc99 && pending)
6772 /* As an extension, allow initializing objects with static storage
6773 duration with compound literals (which are then treated just as
6774 the brace enclosed list they contain). */
6775 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6776 value = DECL_INITIAL (decl);
6779 npc = null_pointer_constant_p (value);
6780 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
6782 semantic_type = TREE_TYPE (value);
6783 value = TREE_OPERAND (value, 0);
6785 value = c_fully_fold (value, require_constant_value, &maybe_const);
6787 if (value == error_mark_node)
6788 constructor_erroneous = 1;
6789 else if (!TREE_CONSTANT (value))
6790 constructor_constant = 0;
6791 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6792 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6793 || TREE_CODE (constructor_type) == UNION_TYPE)
6794 && DECL_C_BIT_FIELD (field)
6795 && TREE_CODE (value) != INTEGER_CST))
6796 constructor_simple = 0;
6798 constructor_nonconst = 1;
6800 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6802 if (require_constant_value)
6804 error_init ("initializer element is not constant");
6805 value = error_mark_node;
6807 else if (require_constant_elements)
6808 pedwarn (input_location, 0,
6809 "initializer element is not computable at load time");
6811 else if (!maybe_const
6812 && (require_constant_value || require_constant_elements))
6813 pedwarn_init (input_location, 0,
6814 "initializer element is not a constant expression");
6816 /* If this field is empty (and not at the end of structure),
6817 don't do anything other than checking the initializer. */
6819 && (TREE_TYPE (field) == error_mark_node
6820 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6821 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6822 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6823 || TREE_CHAIN (field)))))
6827 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
6828 value = digest_init (type, value, npc, strict_string,
6829 require_constant_value);
6830 if (value == error_mark_node)
6832 constructor_erroneous = 1;
6835 if (require_constant_value || require_constant_elements)
6836 constant_expression_warning (value);
6838 /* If this element doesn't come next in sequence,
6839 put it on constructor_pending_elts. */
6840 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6841 && (!constructor_incremental
6842 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6844 if (constructor_incremental
6845 && tree_int_cst_lt (field, constructor_unfilled_index))
6846 set_nonincremental_init ();
6848 add_pending_init (field, value, implicit);
6851 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6852 && (!constructor_incremental
6853 || field != constructor_unfilled_fields))
6855 /* We do this for records but not for unions. In a union,
6856 no matter which field is specified, it can be initialized
6857 right away since it starts at the beginning of the union. */
6858 if (constructor_incremental)
6860 if (!constructor_unfilled_fields)
6861 set_nonincremental_init ();
6864 tree bitpos, unfillpos;
6866 bitpos = bit_position (field);
6867 unfillpos = bit_position (constructor_unfilled_fields);
6869 if (tree_int_cst_lt (bitpos, unfillpos))
6870 set_nonincremental_init ();
6874 add_pending_init (field, value, implicit);
6877 else if (TREE_CODE (constructor_type) == UNION_TYPE
6878 && !VEC_empty (constructor_elt, constructor_elements))
6882 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6883 constructor_elements)->value))
6885 "initialized field with side-effects overwritten");
6886 else if (warn_override_init)
6887 warning_init (OPT_Woverride_init, "initialized field overwritten");
6890 /* We can have just one union field set. */
6891 constructor_elements = 0;
6894 /* Otherwise, output this element either to
6895 constructor_elements or to the assembler file. */
6897 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6898 celt->index = field;
6899 celt->value = value;
6901 /* Advance the variable that indicates sequential elements output. */
6902 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6903 constructor_unfilled_index
6904 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6906 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6908 constructor_unfilled_fields
6909 = TREE_CHAIN (constructor_unfilled_fields);
6911 /* Skip any nameless bit fields. */
6912 while (constructor_unfilled_fields != 0
6913 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6914 && DECL_NAME (constructor_unfilled_fields) == 0)
6915 constructor_unfilled_fields =
6916 TREE_CHAIN (constructor_unfilled_fields);
6918 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6919 constructor_unfilled_fields = 0;
6921 /* Now output any pending elements which have become next. */
6923 output_pending_init_elements (0);
6926 /* Output any pending elements which have become next.
6927 As we output elements, constructor_unfilled_{fields,index}
6928 advances, which may cause other elements to become next;
6929 if so, they too are output.
6931 If ALL is 0, we return when there are
6932 no more pending elements to output now.
6934 If ALL is 1, we output space as necessary so that
6935 we can output all the pending elements. */
6938 output_pending_init_elements (int all)
6940 struct init_node *elt = constructor_pending_elts;
6945 /* Look through the whole pending tree.
6946 If we find an element that should be output now,
6947 output it. Otherwise, set NEXT to the element
6948 that comes first among those still pending. */
6953 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6955 if (tree_int_cst_equal (elt->purpose,
6956 constructor_unfilled_index))
6957 output_init_element (elt->value, true,
6958 TREE_TYPE (constructor_type),
6959 constructor_unfilled_index, 0, false);
6960 else if (tree_int_cst_lt (constructor_unfilled_index,
6963 /* Advance to the next smaller node. */
6968 /* We have reached the smallest node bigger than the
6969 current unfilled index. Fill the space first. */
6970 next = elt->purpose;
6976 /* Advance to the next bigger node. */
6981 /* We have reached the biggest node in a subtree. Find
6982 the parent of it, which is the next bigger node. */
6983 while (elt->parent && elt->parent->right == elt)
6986 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6989 next = elt->purpose;
6995 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6996 || TREE_CODE (constructor_type) == UNION_TYPE)
6998 tree ctor_unfilled_bitpos, elt_bitpos;
7000 /* If the current record is complete we are done. */
7001 if (constructor_unfilled_fields == 0)
7004 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7005 elt_bitpos = bit_position (elt->purpose);
7006 /* We can't compare fields here because there might be empty
7007 fields in between. */
7008 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7010 constructor_unfilled_fields = elt->purpose;
7011 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
7012 elt->purpose, 0, false);
7014 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7016 /* Advance to the next smaller node. */
7021 /* We have reached the smallest node bigger than the
7022 current unfilled field. Fill the space first. */
7023 next = elt->purpose;
7029 /* Advance to the next bigger node. */
7034 /* We have reached the biggest node in a subtree. Find
7035 the parent of it, which is the next bigger node. */
7036 while (elt->parent && elt->parent->right == elt)
7040 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7041 bit_position (elt->purpose))))
7043 next = elt->purpose;
7051 /* Ordinarily return, but not if we want to output all
7052 and there are elements left. */
7053 if (!(all && next != 0))
7056 /* If it's not incremental, just skip over the gap, so that after
7057 jumping to retry we will output the next successive element. */
7058 if (TREE_CODE (constructor_type) == RECORD_TYPE
7059 || TREE_CODE (constructor_type) == UNION_TYPE)
7060 constructor_unfilled_fields = next;
7061 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7062 constructor_unfilled_index = next;
7064 /* ELT now points to the node in the pending tree with the next
7065 initializer to output. */
7069 /* Add one non-braced element to the current constructor level.
7070 This adjusts the current position within the constructor's type.
7071 This may also start or terminate implicit levels
7072 to handle a partly-braced initializer.
7074 Once this has found the correct level for the new element,
7075 it calls output_init_element.
7077 IMPLICIT is true if value comes from pop_init_level (1),
7078 the new initializer has been merged with the existing one
7079 and thus no warnings should be emitted about overriding an
7080 existing initializer. */
7083 process_init_element (struct c_expr value, bool implicit)
7085 tree orig_value = value.value;
7086 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7087 bool strict_string = value.original_code == STRING_CST;
7089 designator_depth = 0;
7090 designator_erroneous = 0;
7092 /* Handle superfluous braces around string cst as in
7093 char x[] = {"foo"}; */
7096 && TREE_CODE (constructor_type) == ARRAY_TYPE
7097 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7098 && integer_zerop (constructor_unfilled_index))
7100 if (constructor_stack->replacement_value.value)
7101 error_init ("excess elements in char array initializer");
7102 constructor_stack->replacement_value = value;
7106 if (constructor_stack->replacement_value.value != 0)
7108 error_init ("excess elements in struct initializer");
7112 /* Ignore elements of a brace group if it is entirely superfluous
7113 and has already been diagnosed. */
7114 if (constructor_type == 0)
7117 /* If we've exhausted any levels that didn't have braces,
7119 while (constructor_stack->implicit)
7121 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7122 || TREE_CODE (constructor_type) == UNION_TYPE)
7123 && constructor_fields == 0)
7124 process_init_element (pop_init_level (1), true);
7125 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7126 && (constructor_max_index == 0
7127 || tree_int_cst_lt (constructor_max_index,
7128 constructor_index)))
7129 process_init_element (pop_init_level (1), true);
7134 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7135 if (constructor_range_stack)
7137 /* If value is a compound literal and we'll be just using its
7138 content, don't put it into a SAVE_EXPR. */
7139 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7140 || !require_constant_value
7143 tree semantic_type = NULL_TREE;
7144 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7146 semantic_type = TREE_TYPE (value.value);
7147 value.value = TREE_OPERAND (value.value, 0);
7149 value.value = c_save_expr (value.value);
7151 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7158 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7161 enum tree_code fieldcode;
7163 if (constructor_fields == 0)
7165 pedwarn_init (input_location, 0,
7166 "excess elements in struct initializer");
7170 fieldtype = TREE_TYPE (constructor_fields);
7171 if (fieldtype != error_mark_node)
7172 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7173 fieldcode = TREE_CODE (fieldtype);
7175 /* Error for non-static initialization of a flexible array member. */
7176 if (fieldcode == ARRAY_TYPE
7177 && !require_constant_value
7178 && TYPE_SIZE (fieldtype) == NULL_TREE
7179 && TREE_CHAIN (constructor_fields) == NULL_TREE)
7181 error_init ("non-static initialization of a flexible array member");
7185 /* Accept a string constant to initialize a subarray. */
7186 if (value.value != 0
7187 && fieldcode == ARRAY_TYPE
7188 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7190 value.value = orig_value;
7191 /* Otherwise, if we have come to a subaggregate,
7192 and we don't have an element of its type, push into it. */
7193 else if (value.value != 0
7194 && value.value != error_mark_node
7195 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7196 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7197 || fieldcode == UNION_TYPE))
7199 push_init_level (1);
7205 push_member_name (constructor_fields);
7206 output_init_element (value.value, strict_string,
7207 fieldtype, constructor_fields, 1, implicit);
7208 RESTORE_SPELLING_DEPTH (constructor_depth);
7211 /* Do the bookkeeping for an element that was
7212 directly output as a constructor. */
7214 /* For a record, keep track of end position of last field. */
7215 if (DECL_SIZE (constructor_fields))
7216 constructor_bit_index
7217 = size_binop (PLUS_EXPR,
7218 bit_position (constructor_fields),
7219 DECL_SIZE (constructor_fields));
7221 /* If the current field was the first one not yet written out,
7222 it isn't now, so update. */
7223 if (constructor_unfilled_fields == constructor_fields)
7225 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7226 /* Skip any nameless bit fields. */
7227 while (constructor_unfilled_fields != 0
7228 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7229 && DECL_NAME (constructor_unfilled_fields) == 0)
7230 constructor_unfilled_fields =
7231 TREE_CHAIN (constructor_unfilled_fields);
7235 constructor_fields = TREE_CHAIN (constructor_fields);
7236 /* Skip any nameless bit fields at the beginning. */
7237 while (constructor_fields != 0
7238 && DECL_C_BIT_FIELD (constructor_fields)
7239 && DECL_NAME (constructor_fields) == 0)
7240 constructor_fields = TREE_CHAIN (constructor_fields);
7242 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7245 enum tree_code fieldcode;
7247 if (constructor_fields == 0)
7249 pedwarn_init (input_location, 0,
7250 "excess elements in union initializer");
7254 fieldtype = TREE_TYPE (constructor_fields);
7255 if (fieldtype != error_mark_node)
7256 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7257 fieldcode = TREE_CODE (fieldtype);
7259 /* Warn that traditional C rejects initialization of unions.
7260 We skip the warning if the value is zero. This is done
7261 under the assumption that the zero initializer in user
7262 code appears conditioned on e.g. __STDC__ to avoid
7263 "missing initializer" warnings and relies on default
7264 initialization to zero in the traditional C case.
7265 We also skip the warning if the initializer is designated,
7266 again on the assumption that this must be conditional on
7267 __STDC__ anyway (and we've already complained about the
7268 member-designator already). */
7269 if (!in_system_header && !constructor_designated
7270 && !(value.value && (integer_zerop (value.value)
7271 || real_zerop (value.value))))
7272 warning (OPT_Wtraditional, "traditional C rejects initialization "
7275 /* Accept a string constant to initialize a subarray. */
7276 if (value.value != 0
7277 && fieldcode == ARRAY_TYPE
7278 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7280 value.value = orig_value;
7281 /* Otherwise, if we have come to a subaggregate,
7282 and we don't have an element of its type, push into it. */
7283 else if (value.value != 0
7284 && value.value != error_mark_node
7285 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7286 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7287 || fieldcode == UNION_TYPE))
7289 push_init_level (1);
7295 push_member_name (constructor_fields);
7296 output_init_element (value.value, strict_string,
7297 fieldtype, constructor_fields, 1, implicit);
7298 RESTORE_SPELLING_DEPTH (constructor_depth);
7301 /* Do the bookkeeping for an element that was
7302 directly output as a constructor. */
7304 constructor_bit_index = DECL_SIZE (constructor_fields);
7305 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7308 constructor_fields = 0;
7310 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7312 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7313 enum tree_code eltcode = TREE_CODE (elttype);
7315 /* Accept a string constant to initialize a subarray. */
7316 if (value.value != 0
7317 && eltcode == ARRAY_TYPE
7318 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
7320 value.value = orig_value;
7321 /* Otherwise, if we have come to a subaggregate,
7322 and we don't have an element of its type, push into it. */
7323 else if (value.value != 0
7324 && value.value != error_mark_node
7325 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
7326 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
7327 || eltcode == UNION_TYPE))
7329 push_init_level (1);
7333 if (constructor_max_index != 0
7334 && (tree_int_cst_lt (constructor_max_index, constructor_index)
7335 || integer_all_onesp (constructor_max_index)))
7337 pedwarn_init (input_location, 0,
7338 "excess elements in array initializer");
7342 /* Now output the actual element. */
7345 push_array_bounds (tree_low_cst (constructor_index, 1));
7346 output_init_element (value.value, strict_string,
7347 elttype, constructor_index, 1, implicit);
7348 RESTORE_SPELLING_DEPTH (constructor_depth);
7352 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7355 /* If we are doing the bookkeeping for an element that was
7356 directly output as a constructor, we must update
7357 constructor_unfilled_index. */
7358 constructor_unfilled_index = constructor_index;
7360 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7362 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7364 /* Do a basic check of initializer size. Note that vectors
7365 always have a fixed size derived from their type. */
7366 if (tree_int_cst_lt (constructor_max_index, constructor_index))
7368 pedwarn_init (input_location, 0,
7369 "excess elements in vector initializer");
7373 /* Now output the actual element. */
7375 output_init_element (value.value, strict_string,
7376 elttype, constructor_index, 1, implicit);
7379 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7382 /* If we are doing the bookkeeping for an element that was
7383 directly output as a constructor, we must update
7384 constructor_unfilled_index. */
7385 constructor_unfilled_index = constructor_index;
7388 /* Handle the sole element allowed in a braced initializer
7389 for a scalar variable. */
7390 else if (constructor_type != error_mark_node
7391 && constructor_fields == 0)
7393 pedwarn_init (input_location, 0,
7394 "excess elements in scalar initializer");
7400 output_init_element (value.value, strict_string,
7401 constructor_type, NULL_TREE, 1, implicit);
7402 constructor_fields = 0;
7405 /* Handle range initializers either at this level or anywhere higher
7406 in the designator stack. */
7407 if (constructor_range_stack)
7409 struct constructor_range_stack *p, *range_stack;
7412 range_stack = constructor_range_stack;
7413 constructor_range_stack = 0;
7414 while (constructor_stack != range_stack->stack)
7416 gcc_assert (constructor_stack->implicit);
7417 process_init_element (pop_init_level (1), true);
7419 for (p = range_stack;
7420 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
7423 gcc_assert (constructor_stack->implicit);
7424 process_init_element (pop_init_level (1), true);
7427 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
7428 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
7433 constructor_index = p->index;
7434 constructor_fields = p->fields;
7435 if (finish && p->range_end && p->index == p->range_start)
7443 push_init_level (2);
7444 p->stack = constructor_stack;
7445 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
7446 p->index = p->range_start;
7450 constructor_range_stack = range_stack;
7457 constructor_range_stack = 0;
7460 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7461 (guaranteed to be 'volatile' or null) and ARGS (represented using
7462 an ASM_EXPR node). */
7464 build_asm_stmt (tree cv_qualifier, tree args)
7466 if (!ASM_VOLATILE_P (args) && cv_qualifier)
7467 ASM_VOLATILE_P (args) = 1;
7468 return add_stmt (args);
7471 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7472 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7473 SIMPLE indicates whether there was anything at all after the
7474 string in the asm expression -- asm("blah") and asm("blah" : )
7475 are subtly different. We use a ASM_EXPR node to represent this. */
7477 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
7483 const char *constraint;
7484 const char **oconstraints;
7485 bool allows_mem, allows_reg, is_inout;
7486 int ninputs, noutputs;
7488 ninputs = list_length (inputs);
7489 noutputs = list_length (outputs);
7490 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
7492 string = resolve_asm_operand_names (string, outputs, inputs);
7494 /* Remove output conversions that change the type but not the mode. */
7495 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
7497 tree output = TREE_VALUE (tail);
7499 /* ??? Really, this should not be here. Users should be using a
7500 proper lvalue, dammit. But there's a long history of using casts
7501 in the output operands. In cases like longlong.h, this becomes a
7502 primitive form of typechecking -- if the cast can be removed, then
7503 the output operand had a type of the proper width; otherwise we'll
7504 get an error. Gross, but ... */
7505 STRIP_NOPS (output);
7507 if (!lvalue_or_else (output, lv_asm))
7508 output = error_mark_node;
7510 if (output != error_mark_node
7511 && (TREE_READONLY (output)
7512 || TYPE_READONLY (TREE_TYPE (output))
7513 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
7514 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
7515 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
7516 readonly_error (output, lv_asm);
7518 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7519 oconstraints[i] = constraint;
7521 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7522 &allows_mem, &allows_reg, &is_inout))
7524 /* If the operand is going to end up in memory,
7525 mark it addressable. */
7526 if (!allows_reg && !c_mark_addressable (output))
7527 output = error_mark_node;
7530 output = error_mark_node;
7532 TREE_VALUE (tail) = output;
7535 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
7539 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7540 input = TREE_VALUE (tail);
7542 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
7543 oconstraints, &allows_mem, &allows_reg))
7545 /* If the operand is going to end up in memory,
7546 mark it addressable. */
7547 if (!allows_reg && allows_mem)
7549 /* Strip the nops as we allow this case. FIXME, this really
7550 should be rejected or made deprecated. */
7552 if (!c_mark_addressable (input))
7553 input = error_mark_node;
7557 input = error_mark_node;
7559 TREE_VALUE (tail) = input;
7562 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
7564 /* asm statements without outputs, including simple ones, are treated
7566 ASM_INPUT_P (args) = simple;
7567 ASM_VOLATILE_P (args) = (noutputs == 0);
7572 /* Generate a goto statement to LABEL. */
7575 c_finish_goto_label (tree label)
7577 tree decl = lookup_label (label);
7581 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
7583 error ("jump into statement expression");
7587 if (C_DECL_UNJUMPABLE_VM (decl))
7589 error ("jump into scope of identifier with variably modified type");
7593 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
7595 /* No jump from outside this statement expression context, so
7596 record that there is a jump from within this context. */
7597 struct c_label_list *nlist;
7598 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7599 nlist->next = label_context_stack_se->labels_used;
7600 nlist->label = decl;
7601 label_context_stack_se->labels_used = nlist;
7604 if (!C_DECL_UNDEFINABLE_VM (decl))
7606 /* No jump from outside this context context of identifiers with
7607 variably modified type, so record that there is a jump from
7608 within this context. */
7609 struct c_label_list *nlist;
7610 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7611 nlist->next = label_context_stack_vm->labels_used;
7612 nlist->label = decl;
7613 label_context_stack_vm->labels_used = nlist;
7616 TREE_USED (decl) = 1;
7617 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
7620 /* Generate a computed goto statement to EXPR. */
7623 c_finish_goto_ptr (tree expr)
7625 pedwarn (input_location, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
7626 expr = c_fully_fold (expr, false, NULL);
7627 expr = convert (ptr_type_node, expr);
7628 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
7631 /* Generate a C `return' statement. RETVAL is the expression for what
7632 to return, or a null pointer for `return;' with no value. */
7635 c_finish_return (tree retval)
7637 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
7638 bool no_warning = false;
7641 if (TREE_THIS_VOLATILE (current_function_decl))
7642 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7646 tree semantic_type = NULL_TREE;
7647 npc = null_pointer_constant_p (retval);
7648 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
7650 semantic_type = TREE_TYPE (retval);
7651 retval = TREE_OPERAND (retval, 0);
7653 retval = c_fully_fold (retval, false, NULL);
7655 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
7660 current_function_returns_null = 1;
7661 if ((warn_return_type || flag_isoc99)
7662 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7664 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wreturn_type,
7665 "%<return%> with no value, in "
7666 "function returning non-void");
7670 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7672 current_function_returns_null = 1;
7673 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7674 pedwarn (input_location, 0,
7675 "%<return%> with a value, in function returning void");
7677 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
7678 "%<return%> with expression, in function returning void");
7682 tree t = convert_for_assignment (valtype, retval, ic_return, npc,
7683 NULL_TREE, NULL_TREE, 0);
7684 tree res = DECL_RESULT (current_function_decl);
7687 current_function_returns_value = 1;
7688 if (t == error_mark_node)
7691 inner = t = convert (TREE_TYPE (res), t);
7693 /* Strip any conversions, additions, and subtractions, and see if
7694 we are returning the address of a local variable. Warn if so. */
7697 switch (TREE_CODE (inner))
7700 case NON_LVALUE_EXPR:
7702 case POINTER_PLUS_EXPR:
7703 inner = TREE_OPERAND (inner, 0);
7707 /* If the second operand of the MINUS_EXPR has a pointer
7708 type (or is converted from it), this may be valid, so
7709 don't give a warning. */
7711 tree op1 = TREE_OPERAND (inner, 1);
7713 while (!POINTER_TYPE_P (TREE_TYPE (op1))
7714 && (CONVERT_EXPR_P (op1)
7715 || TREE_CODE (op1) == NON_LVALUE_EXPR))
7716 op1 = TREE_OPERAND (op1, 0);
7718 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7721 inner = TREE_OPERAND (inner, 0);
7726 inner = TREE_OPERAND (inner, 0);
7728 while (REFERENCE_CLASS_P (inner)
7729 && TREE_CODE (inner) != INDIRECT_REF)
7730 inner = TREE_OPERAND (inner, 0);
7733 && !DECL_EXTERNAL (inner)
7734 && !TREE_STATIC (inner)
7735 && DECL_CONTEXT (inner) == current_function_decl)
7736 warning (0, "function returns address of local variable");
7746 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
7748 if (warn_sequence_point)
7749 verify_sequence_points (retval);
7752 ret_stmt = build_stmt (RETURN_EXPR, retval);
7753 TREE_NO_WARNING (ret_stmt) |= no_warning;
7754 return add_stmt (ret_stmt);
7758 /* The SWITCH_EXPR being built. */
7761 /* The original type of the testing expression, i.e. before the
7762 default conversion is applied. */
7765 /* A splay-tree mapping the low element of a case range to the high
7766 element, or NULL_TREE if there is no high element. Used to
7767 determine whether or not a new case label duplicates an old case
7768 label. We need a tree, rather than simply a hash table, because
7769 of the GNU case range extension. */
7772 /* Number of nested statement expressions within this switch
7773 statement; if nonzero, case and default labels may not
7775 unsigned int blocked_stmt_expr;
7777 /* Scope of outermost declarations of identifiers with variably
7778 modified type within this switch statement; if nonzero, case and
7779 default labels may not appear. */
7780 unsigned int blocked_vm;
7782 /* The next node on the stack. */
7783 struct c_switch *next;
7786 /* A stack of the currently active switch statements. The innermost
7787 switch statement is on the top of the stack. There is no need to
7788 mark the stack for garbage collection because it is only active
7789 during the processing of the body of a function, and we never
7790 collect at that point. */
7792 struct c_switch *c_switch_stack;
7794 /* Start a C switch statement, testing expression EXP. Return the new
7798 c_start_case (tree exp)
7800 tree orig_type = error_mark_node;
7801 struct c_switch *cs;
7803 if (exp != error_mark_node)
7805 orig_type = TREE_TYPE (exp);
7807 if (!INTEGRAL_TYPE_P (orig_type))
7809 if (orig_type != error_mark_node)
7811 error ("switch quantity not an integer");
7812 orig_type = error_mark_node;
7814 exp = integer_zero_node;
7818 tree type = TYPE_MAIN_VARIANT (orig_type);
7820 if (!in_system_header
7821 && (type == long_integer_type_node
7822 || type == long_unsigned_type_node))
7823 warning (OPT_Wtraditional, "%<long%> switch expression not "
7824 "converted to %<int%> in ISO C");
7826 exp = c_fully_fold (exp, false, NULL);
7827 exp = default_conversion (exp);
7829 if (warn_sequence_point)
7830 verify_sequence_points (exp);
7834 /* Add this new SWITCH_EXPR to the stack. */
7835 cs = XNEW (struct c_switch);
7836 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7837 cs->orig_type = orig_type;
7838 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7839 cs->blocked_stmt_expr = 0;
7841 cs->next = c_switch_stack;
7842 c_switch_stack = cs;
7844 return add_stmt (cs->switch_expr);
7847 /* Process a case label. */
7850 do_case (tree low_value, tree high_value)
7852 tree label = NULL_TREE;
7854 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
7856 low_value = c_fully_fold (low_value, false, NULL);
7857 if (TREE_CODE (low_value) == INTEGER_CST)
7858 pedwarn (input_location, OPT_pedantic,
7859 "case label is not an integer constant expression");
7862 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
7864 high_value = c_fully_fold (high_value, false, NULL);
7865 if (TREE_CODE (high_value) == INTEGER_CST)
7866 pedwarn (input_location, OPT_pedantic,
7867 "case label is not an integer constant expression");
7870 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7871 && !c_switch_stack->blocked_vm)
7873 label = c_add_case_label (c_switch_stack->cases,
7874 SWITCH_COND (c_switch_stack->switch_expr),
7875 c_switch_stack->orig_type,
7876 low_value, high_value);
7877 if (label == error_mark_node)
7880 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7883 error ("case label in statement expression not containing "
7884 "enclosing switch statement");
7886 error ("%<default%> label in statement expression not containing "
7887 "enclosing switch statement");
7889 else if (c_switch_stack && c_switch_stack->blocked_vm)
7892 error ("case label in scope of identifier with variably modified "
7893 "type not containing enclosing switch statement");
7895 error ("%<default%> label in scope of identifier with variably "
7896 "modified type not containing enclosing switch statement");
7899 error ("case label not within a switch statement");
7901 error ("%<default%> label not within a switch statement");
7906 /* Finish the switch statement. */
7909 c_finish_case (tree body)
7911 struct c_switch *cs = c_switch_stack;
7912 location_t switch_location;
7914 SWITCH_BODY (cs->switch_expr) = body;
7916 /* We must not be within a statement expression nested in the switch
7917 at this point; we might, however, be within the scope of an
7918 identifier with variably modified type nested in the switch. */
7919 gcc_assert (!cs->blocked_stmt_expr);
7921 /* Emit warnings as needed. */
7922 if (EXPR_HAS_LOCATION (cs->switch_expr))
7923 switch_location = EXPR_LOCATION (cs->switch_expr);
7925 switch_location = input_location;
7926 c_do_switch_warnings (cs->cases, switch_location,
7927 TREE_TYPE (cs->switch_expr),
7928 SWITCH_COND (cs->switch_expr));
7930 /* Pop the stack. */
7931 c_switch_stack = cs->next;
7932 splay_tree_delete (cs->cases);
7936 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7937 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7938 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7939 statement, and was not surrounded with parenthesis. */
7942 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7943 tree else_block, bool nested_if)
7947 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7948 if (warn_parentheses && nested_if && else_block == NULL)
7950 tree inner_if = then_block;
7952 /* We know from the grammar productions that there is an IF nested
7953 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7954 it might not be exactly THEN_BLOCK, but should be the last
7955 non-container statement within. */
7957 switch (TREE_CODE (inner_if))
7962 inner_if = BIND_EXPR_BODY (inner_if);
7964 case STATEMENT_LIST:
7965 inner_if = expr_last (then_block);
7967 case TRY_FINALLY_EXPR:
7968 case TRY_CATCH_EXPR:
7969 inner_if = TREE_OPERAND (inner_if, 0);
7976 if (COND_EXPR_ELSE (inner_if))
7977 warning (OPT_Wparentheses,
7978 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7982 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7983 SET_EXPR_LOCATION (stmt, if_locus);
7987 /* Emit a general-purpose loop construct. START_LOCUS is the location of
7988 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7989 is false for DO loops. INCR is the FOR increment expression. BODY is
7990 the statement controlled by the loop. BLAB is the break label. CLAB is
7991 the continue label. Everything is allowed to be NULL. */
7994 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7995 tree blab, tree clab, bool cond_is_first)
7997 tree entry = NULL, exit = NULL, t;
7999 /* If the condition is zero don't generate a loop construct. */
8000 if (cond && integer_zerop (cond))
8004 t = build_and_jump (&blab);
8005 SET_EXPR_LOCATION (t, start_locus);
8011 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8013 /* If we have an exit condition, then we build an IF with gotos either
8014 out of the loop, or to the top of it. If there's no exit condition,
8015 then we just build a jump back to the top. */
8016 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8018 if (cond && !integer_nonzerop (cond))
8020 /* Canonicalize the loop condition to the end. This means
8021 generating a branch to the loop condition. Reuse the
8022 continue label, if possible. */
8027 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8028 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8031 t = build1 (GOTO_EXPR, void_type_node, clab);
8032 SET_EXPR_LOCATION (t, start_locus);
8036 t = build_and_jump (&blab);
8037 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
8039 SET_EXPR_LOCATION (exit, start_locus);
8041 SET_EXPR_LOCATION (exit, input_location);
8050 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8058 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8062 c_finish_bc_stmt (tree *label_p, bool is_break)
8065 tree label = *label_p;
8067 /* In switch statements break is sometimes stylistically used after
8068 a return statement. This can lead to spurious warnings about
8069 control reaching the end of a non-void function when it is
8070 inlined. Note that we are calling block_may_fallthru with
8071 language specific tree nodes; this works because
8072 block_may_fallthru returns true when given something it does not
8074 skip = !block_may_fallthru (cur_stmt_list);
8079 *label_p = label = create_artificial_label ();
8081 else if (TREE_CODE (label) == LABEL_DECL)
8083 else switch (TREE_INT_CST_LOW (label))
8087 error ("break statement not within loop or switch");
8089 error ("continue statement not within a loop");
8093 gcc_assert (is_break);
8094 error ("break statement used with OpenMP for loop");
8105 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8107 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8110 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8113 emit_side_effect_warnings (tree expr)
8115 if (expr == error_mark_node)
8117 else if (!TREE_SIDE_EFFECTS (expr))
8119 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8120 warning (OPT_Wunused_value, "%Hstatement with no effect",
8121 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
8124 warn_if_unused_value (expr, input_location);
8127 /* Process an expression as if it were a complete statement. Emit
8128 diagnostics, but do not call ADD_STMT. */
8131 c_process_expr_stmt (tree expr)
8136 expr = c_fully_fold (expr, false, NULL);
8138 if (warn_sequence_point)
8139 verify_sequence_points (expr);
8141 if (TREE_TYPE (expr) != error_mark_node
8142 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8143 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8144 error ("expression statement has incomplete type");
8146 /* If we're not processing a statement expression, warn about unused values.
8147 Warnings for statement expressions will be emitted later, once we figure
8148 out which is the result. */
8149 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8150 && warn_unused_value)
8151 emit_side_effect_warnings (expr);
8153 /* If the expression is not of a type to which we cannot assign a line
8154 number, wrap the thing in a no-op NOP_EXPR. */
8155 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8156 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8158 if (CAN_HAVE_LOCATION_P (expr))
8159 SET_EXPR_LOCATION (expr, input_location);
8164 /* Emit an expression as a statement. */
8167 c_finish_expr_stmt (tree expr)
8170 return add_stmt (c_process_expr_stmt (expr));
8175 /* Do the opposite and emit a statement as an expression. To begin,
8176 create a new binding level and return it. */
8179 c_begin_stmt_expr (void)
8182 struct c_label_context_se *nstack;
8183 struct c_label_list *glist;
8185 /* We must force a BLOCK for this level so that, if it is not expanded
8186 later, there is a way to turn off the entire subtree of blocks that
8187 are contained in it. */
8189 ret = c_begin_compound_stmt (true);
8192 c_switch_stack->blocked_stmt_expr++;
8193 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8195 for (glist = label_context_stack_se->labels_used;
8197 glist = glist->next)
8199 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
8201 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
8202 nstack->labels_def = NULL;
8203 nstack->labels_used = NULL;
8204 nstack->next = label_context_stack_se;
8205 label_context_stack_se = nstack;
8207 /* Mark the current statement list as belonging to a statement list. */
8208 STATEMENT_LIST_STMT_EXPR (ret) = 1;
8214 c_finish_stmt_expr (tree body)
8216 tree last, type, tmp, val;
8218 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8220 body = c_end_compound_stmt (body, true);
8223 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8224 c_switch_stack->blocked_stmt_expr--;
8226 /* It is no longer possible to jump to labels defined within this
8227 statement expression. */
8228 for (dlist = label_context_stack_se->labels_def;
8230 dlist = dlist->next)
8232 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
8234 /* It is again possible to define labels with a goto just outside
8235 this statement expression. */
8236 for (glist = label_context_stack_se->next->labels_used;
8238 glist = glist->next)
8240 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
8243 if (glist_prev != NULL)
8244 glist_prev->next = label_context_stack_se->labels_used;
8246 label_context_stack_se->next->labels_used
8247 = label_context_stack_se->labels_used;
8248 label_context_stack_se = label_context_stack_se->next;
8250 /* Locate the last statement in BODY. See c_end_compound_stmt
8251 about always returning a BIND_EXPR. */
8252 last_p = &BIND_EXPR_BODY (body);
8253 last = BIND_EXPR_BODY (body);
8256 if (TREE_CODE (last) == STATEMENT_LIST)
8258 tree_stmt_iterator i;
8260 /* This can happen with degenerate cases like ({ }). No value. */
8261 if (!TREE_SIDE_EFFECTS (last))
8264 /* If we're supposed to generate side effects warnings, process
8265 all of the statements except the last. */
8266 if (warn_unused_value)
8268 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8269 emit_side_effect_warnings (tsi_stmt (i));
8272 i = tsi_last (last);
8273 last_p = tsi_stmt_ptr (i);
8277 /* If the end of the list is exception related, then the list was split
8278 by a call to push_cleanup. Continue searching. */
8279 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8280 || TREE_CODE (last) == TRY_CATCH_EXPR)
8282 last_p = &TREE_OPERAND (last, 0);
8284 goto continue_searching;
8287 /* In the case that the BIND_EXPR is not necessary, return the
8288 expression out from inside it. */
8289 if (last == error_mark_node
8290 || (last == BIND_EXPR_BODY (body)
8291 && BIND_EXPR_VARS (body) == NULL))
8293 /* Even if this looks constant, do not allow it in a constant
8295 last = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (last), NULL_TREE, last);
8296 C_MAYBE_CONST_EXPR_NON_CONST (last) = 1;
8297 /* Do not warn if the return value of a statement expression is
8299 TREE_NO_WARNING (last) = 1;
8303 /* Extract the type of said expression. */
8304 type = TREE_TYPE (last);
8306 /* If we're not returning a value at all, then the BIND_EXPR that
8307 we already have is a fine expression to return. */
8308 if (!type || VOID_TYPE_P (type))
8311 /* Now that we've located the expression containing the value, it seems
8312 silly to make voidify_wrapper_expr repeat the process. Create a
8313 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8314 tmp = create_tmp_var_raw (type, NULL);
8316 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8317 tree_expr_nonnegative_p giving up immediately. */
8319 if (TREE_CODE (val) == NOP_EXPR
8320 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
8321 val = TREE_OPERAND (val, 0);
8323 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
8324 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
8326 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
8329 /* Begin the scope of an identifier of variably modified type, scope
8330 number SCOPE. Jumping from outside this scope to inside it is not
8334 c_begin_vm_scope (unsigned int scope)
8336 struct c_label_context_vm *nstack;
8337 struct c_label_list *glist;
8339 gcc_assert (scope > 0);
8341 /* At file_scope, we don't have to do any processing. */
8342 if (label_context_stack_vm == NULL)
8345 if (c_switch_stack && !c_switch_stack->blocked_vm)
8346 c_switch_stack->blocked_vm = scope;
8347 for (glist = label_context_stack_vm->labels_used;
8349 glist = glist->next)
8351 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
8353 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
8354 nstack->labels_def = NULL;
8355 nstack->labels_used = NULL;
8356 nstack->scope = scope;
8357 nstack->next = label_context_stack_vm;
8358 label_context_stack_vm = nstack;
8361 /* End a scope which may contain identifiers of variably modified
8362 type, scope number SCOPE. */
8365 c_end_vm_scope (unsigned int scope)
8367 if (label_context_stack_vm == NULL)
8369 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
8370 c_switch_stack->blocked_vm = 0;
8371 /* We may have a number of nested scopes of identifiers with
8372 variably modified type, all at this depth. Pop each in turn. */
8373 while (label_context_stack_vm->scope == scope)
8375 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8377 /* It is no longer possible to jump to labels defined within this
8379 for (dlist = label_context_stack_vm->labels_def;
8381 dlist = dlist->next)
8383 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
8385 /* It is again possible to define labels with a goto just outside
8387 for (glist = label_context_stack_vm->next->labels_used;
8389 glist = glist->next)
8391 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
8394 if (glist_prev != NULL)
8395 glist_prev->next = label_context_stack_vm->labels_used;
8397 label_context_stack_vm->next->labels_used
8398 = label_context_stack_vm->labels_used;
8399 label_context_stack_vm = label_context_stack_vm->next;
8403 /* Begin and end compound statements. This is as simple as pushing
8404 and popping new statement lists from the tree. */
8407 c_begin_compound_stmt (bool do_scope)
8409 tree stmt = push_stmt_list ();
8416 c_end_compound_stmt (tree stmt, bool do_scope)
8422 if (c_dialect_objc ())
8423 objc_clear_super_receiver ();
8424 block = pop_scope ();
8427 stmt = pop_stmt_list (stmt);
8428 stmt = c_build_bind_expr (block, stmt);
8430 /* If this compound statement is nested immediately inside a statement
8431 expression, then force a BIND_EXPR to be created. Otherwise we'll
8432 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8433 STATEMENT_LISTs merge, and thus we can lose track of what statement
8436 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8437 && TREE_CODE (stmt) != BIND_EXPR)
8439 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
8440 TREE_SIDE_EFFECTS (stmt) = 1;
8446 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8447 when the current scope is exited. EH_ONLY is true when this is not
8448 meant to apply to normal control flow transfer. */
8451 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
8453 enum tree_code code;
8457 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
8458 stmt = build_stmt (code, NULL, cleanup);
8460 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
8461 list = push_stmt_list ();
8462 TREE_OPERAND (stmt, 0) = list;
8463 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
8466 /* Build a binary-operation expression without default conversions.
8467 CODE is the kind of expression to build.
8468 LOCATION is the operator's location.
8469 This function differs from `build' in several ways:
8470 the data type of the result is computed and recorded in it,
8471 warnings are generated if arg data types are invalid,
8472 special handling for addition and subtraction of pointers is known,
8473 and some optimization is done (operations on narrow ints
8474 are done in the narrower type when that gives the same result).
8475 Constant folding is also done before the result is returned.
8477 Note that the operands will never have enumeral types, or function
8478 or array types, because either they will have the default conversions
8479 performed or they have both just been converted to some other type in which
8480 the arithmetic is to be done. */
8483 build_binary_op (location_t location, enum tree_code code,
8484 tree orig_op0, tree orig_op1, int convert_p)
8486 tree type0, type1, orig_type0, orig_type1;
8488 enum tree_code code0, code1;
8490 tree ret = error_mark_node;
8491 const char *invalid_op_diag;
8492 bool op0_int_operands, op1_int_operands;
8493 bool int_const, int_const_or_overflow, int_operands;
8495 /* Expression code to give to the expression when it is built.
8496 Normally this is CODE, which is what the caller asked for,
8497 but in some special cases we change it. */
8498 enum tree_code resultcode = code;
8500 /* Data type in which the computation is to be performed.
8501 In the simplest cases this is the common type of the arguments. */
8502 tree result_type = NULL;
8504 /* When the computation is in excess precision, the type of the
8505 final EXCESS_PRECISION_EXPR. */
8506 tree real_result_type = NULL;
8508 /* Nonzero means operands have already been type-converted
8509 in whatever way is necessary.
8510 Zero means they need to be converted to RESULT_TYPE. */
8513 /* Nonzero means create the expression with this type, rather than
8515 tree build_type = 0;
8517 /* Nonzero means after finally constructing the expression
8518 convert it to this type. */
8519 tree final_type = 0;
8521 /* Nonzero if this is an operation like MIN or MAX which can
8522 safely be computed in short if both args are promoted shorts.
8523 Also implies COMMON.
8524 -1 indicates a bitwise operation; this makes a difference
8525 in the exact conditions for when it is safe to do the operation
8526 in a narrower mode. */
8529 /* Nonzero if this is a comparison operation;
8530 if both args are promoted shorts, compare the original shorts.
8531 Also implies COMMON. */
8532 int short_compare = 0;
8534 /* Nonzero if this is a right-shift operation, which can be computed on the
8535 original short and then promoted if the operand is a promoted short. */
8536 int short_shift = 0;
8538 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8541 /* True means types are compatible as far as ObjC is concerned. */
8544 /* True means this is an arithmetic operation that may need excess
8546 bool may_need_excess_precision;
8548 if (location == UNKNOWN_LOCATION)
8549 location = input_location;
8554 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
8555 if (op0_int_operands)
8556 op0 = remove_c_maybe_const_expr (op0);
8557 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
8558 if (op1_int_operands)
8559 op1 = remove_c_maybe_const_expr (op1);
8560 int_operands = (op0_int_operands && op1_int_operands);
8563 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
8564 && TREE_CODE (orig_op1) == INTEGER_CST);
8565 int_const = (int_const_or_overflow
8566 && !TREE_OVERFLOW (orig_op0)
8567 && !TREE_OVERFLOW (orig_op1));
8570 int_const = int_const_or_overflow = false;
8574 op0 = default_conversion (op0);
8575 op1 = default_conversion (op1);
8578 orig_type0 = type0 = TREE_TYPE (op0);
8579 orig_type1 = type1 = TREE_TYPE (op1);
8581 /* The expression codes of the data types of the arguments tell us
8582 whether the arguments are integers, floating, pointers, etc. */
8583 code0 = TREE_CODE (type0);
8584 code1 = TREE_CODE (type1);
8586 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8587 STRIP_TYPE_NOPS (op0);
8588 STRIP_TYPE_NOPS (op1);
8590 /* If an error was already reported for one of the arguments,
8591 avoid reporting another error. */
8593 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8594 return error_mark_node;
8596 if ((invalid_op_diag
8597 = targetm.invalid_binary_op (code, type0, type1)))
8599 error_at (location, invalid_op_diag);
8600 return error_mark_node;
8608 case TRUNC_DIV_EXPR:
8610 case FLOOR_DIV_EXPR:
8611 case ROUND_DIV_EXPR:
8612 case EXACT_DIV_EXPR:
8613 may_need_excess_precision = true;
8616 may_need_excess_precision = false;
8619 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
8621 op0 = TREE_OPERAND (op0, 0);
8622 type0 = TREE_TYPE (op0);
8624 else if (may_need_excess_precision
8625 && (eptype = excess_precision_type (type0)) != NULL_TREE)
8628 op0 = convert (eptype, op0);
8630 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
8632 op1 = TREE_OPERAND (op1, 0);
8633 type1 = TREE_TYPE (op1);
8635 else if (may_need_excess_precision
8636 && (eptype = excess_precision_type (type1)) != NULL_TREE)
8639 op1 = convert (eptype, op1);
8642 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
8647 /* Handle the pointer + int case. */
8648 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8650 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
8651 goto return_build_binary_op;
8653 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
8655 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
8656 goto return_build_binary_op;
8663 /* Subtraction of two similar pointers.
8664 We must subtract them as integers, then divide by object size. */
8665 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
8666 && comp_target_types (type0, type1))
8668 ret = pointer_diff (op0, op1);
8669 goto return_build_binary_op;
8671 /* Handle pointer minus int. Just like pointer plus int. */
8672 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8674 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
8675 goto return_build_binary_op;
8685 case TRUNC_DIV_EXPR:
8687 case FLOOR_DIV_EXPR:
8688 case ROUND_DIV_EXPR:
8689 case EXACT_DIV_EXPR:
8690 warn_for_div_by_zero (location, op1);
8692 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8693 || code0 == FIXED_POINT_TYPE
8694 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8695 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8696 || code1 == FIXED_POINT_TYPE
8697 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
8699 enum tree_code tcode0 = code0, tcode1 = code1;
8701 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8702 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
8703 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
8704 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
8706 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
8707 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
8708 resultcode = RDIV_EXPR;
8710 /* Although it would be tempting to shorten always here, that
8711 loses on some targets, since the modulo instruction is
8712 undefined if the quotient can't be represented in the
8713 computation mode. We shorten only if unsigned or if
8714 dividing by something we know != -1. */
8715 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8716 || (TREE_CODE (op1) == INTEGER_CST
8717 && !integer_all_onesp (op1)));
8725 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8727 /* Allow vector types which are not floating point types. */
8728 else if (code0 == VECTOR_TYPE
8729 && code1 == VECTOR_TYPE
8730 && !VECTOR_FLOAT_TYPE_P (type0)
8731 && !VECTOR_FLOAT_TYPE_P (type1))
8735 case TRUNC_MOD_EXPR:
8736 case FLOOR_MOD_EXPR:
8737 warn_for_div_by_zero (location, op1);
8739 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8741 /* Although it would be tempting to shorten always here, that loses
8742 on some targets, since the modulo instruction is undefined if the
8743 quotient can't be represented in the computation mode. We shorten
8744 only if unsigned or if dividing by something we know != -1. */
8745 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8746 || (TREE_CODE (op1) == INTEGER_CST
8747 && !integer_all_onesp (op1)));
8752 case TRUTH_ANDIF_EXPR:
8753 case TRUTH_ORIF_EXPR:
8754 case TRUTH_AND_EXPR:
8756 case TRUTH_XOR_EXPR:
8757 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
8758 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8759 || code0 == FIXED_POINT_TYPE)
8760 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
8761 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8762 || code1 == FIXED_POINT_TYPE))
8764 /* Result of these operations is always an int,
8765 but that does not mean the operands should be
8766 converted to ints! */
8767 result_type = integer_type_node;
8768 op0 = c_common_truthvalue_conversion (location, op0);
8769 op1 = c_common_truthvalue_conversion (location, op1);
8772 if (code == TRUTH_ANDIF_EXPR)
8774 int_const_or_overflow = (int_operands
8775 && TREE_CODE (orig_op0) == INTEGER_CST
8776 && (op0 == truthvalue_false_node
8777 || TREE_CODE (orig_op1) == INTEGER_CST));
8778 int_const = (int_const_or_overflow
8779 && !TREE_OVERFLOW (orig_op0)
8780 && (op0 == truthvalue_false_node
8781 || !TREE_OVERFLOW (orig_op1)));
8783 else if (code == TRUTH_ORIF_EXPR)
8785 int_const_or_overflow = (int_operands
8786 && TREE_CODE (orig_op0) == INTEGER_CST
8787 && (op0 == truthvalue_true_node
8788 || TREE_CODE (orig_op1) == INTEGER_CST));
8789 int_const = (int_const_or_overflow
8790 && !TREE_OVERFLOW (orig_op0)
8791 && (op0 == truthvalue_true_node
8792 || !TREE_OVERFLOW (orig_op1)));
8796 /* Shift operations: result has same type as first operand;
8797 always convert second operand to int.
8798 Also set SHORT_SHIFT if shifting rightward. */
8801 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8802 && code1 == INTEGER_TYPE)
8804 if (TREE_CODE (op1) == INTEGER_CST)
8806 if (tree_int_cst_sgn (op1) < 0)
8809 if (skip_evaluation == 0)
8810 warning (0, "right shift count is negative");
8814 if (!integer_zerop (op1))
8817 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8820 if (skip_evaluation == 0)
8821 warning (0, "right shift count >= width of type");
8826 /* Use the type of the value to be shifted. */
8827 result_type = type0;
8828 /* Convert the shift-count to an integer, regardless of size
8829 of value being shifted. */
8830 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8831 op1 = convert (integer_type_node, op1);
8832 /* Avoid converting op1 to result_type later. */
8838 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8839 && code1 == INTEGER_TYPE)
8841 if (TREE_CODE (op1) == INTEGER_CST)
8843 if (tree_int_cst_sgn (op1) < 0)
8846 if (skip_evaluation == 0)
8847 warning (0, "left shift count is negative");
8850 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8853 if (skip_evaluation == 0)
8854 warning (0, "left shift count >= width of type");
8858 /* Use the type of the value to be shifted. */
8859 result_type = type0;
8860 /* Convert the shift-count to an integer, regardless of size
8861 of value being shifted. */
8862 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8863 op1 = convert (integer_type_node, op1);
8864 /* Avoid converting op1 to result_type later. */
8871 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
8872 warning_at (location,
8874 "comparing floating point with == or != is unsafe");
8875 /* Result of comparison is always int,
8876 but don't convert the args to int! */
8877 build_type = integer_type_node;
8878 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8879 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
8880 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8881 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
8883 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8885 tree tt0 = TREE_TYPE (type0);
8886 tree tt1 = TREE_TYPE (type1);
8887 /* Anything compares with void *. void * compares with anything.
8888 Otherwise, the targets must be compatible
8889 and both must be object or both incomplete. */
8890 if (comp_target_types (type0, type1))
8891 result_type = common_pointer_type (type0, type1);
8892 else if (VOID_TYPE_P (tt0))
8894 /* op0 != orig_op0 detects the case of something
8895 whose value is 0 but which isn't a valid null ptr const. */
8896 if (pedantic && !null_pointer_constant_p (orig_op0)
8897 && TREE_CODE (tt1) == FUNCTION_TYPE)
8898 pedwarn (location, OPT_pedantic, "ISO C forbids "
8899 "comparison of %<void *%> with function pointer");
8901 else if (VOID_TYPE_P (tt1))
8903 if (pedantic && !null_pointer_constant_p (orig_op1)
8904 && TREE_CODE (tt0) == FUNCTION_TYPE)
8905 pedwarn (location, OPT_pedantic, "ISO C forbids "
8906 "comparison of %<void *%> with function pointer");
8909 /* Avoid warning about the volatile ObjC EH puts on decls. */
8911 pedwarn (location, 0,
8912 "comparison of distinct pointer types lacks a cast");
8914 if (result_type == NULL_TREE)
8915 result_type = ptr_type_node;
8917 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8919 if (TREE_CODE (op0) == ADDR_EXPR
8920 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
8921 warning_at (location,
8922 OPT_Waddress, "the address of %qD will never be NULL",
8923 TREE_OPERAND (op0, 0));
8924 result_type = type0;
8926 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8928 if (TREE_CODE (op1) == ADDR_EXPR
8929 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
8930 warning_at (location,
8931 OPT_Waddress, "the address of %qD will never be NULL",
8932 TREE_OPERAND (op1, 0));
8933 result_type = type1;
8935 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8937 result_type = type0;
8938 pedwarn (location, 0, "comparison between pointer and integer");
8940 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8942 result_type = type1;
8943 pedwarn (location, 0, "comparison between pointer and integer");
8951 build_type = integer_type_node;
8952 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8953 || code0 == FIXED_POINT_TYPE)
8954 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8955 || code1 == FIXED_POINT_TYPE))
8957 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8959 if (comp_target_types (type0, type1))
8961 result_type = common_pointer_type (type0, type1);
8962 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
8963 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8964 pedwarn (location, 0,
8965 "comparison of complete and incomplete pointers");
8966 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
8967 pedwarn (location, OPT_pedantic, "ISO C forbids "
8968 "ordered comparisons of pointers to functions");
8972 result_type = ptr_type_node;
8973 pedwarn (location, 0,
8974 "comparison of distinct pointer types lacks a cast");
8977 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8979 result_type = type0;
8981 pedwarn (location, OPT_pedantic,
8982 "ordered comparison of pointer with integer zero");
8983 else if (extra_warnings)
8984 warning_at (location, OPT_Wextra,
8985 "ordered comparison of pointer with integer zero");
8987 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8989 result_type = type1;
8990 pedwarn (location, OPT_pedantic,
8991 "ordered comparison of pointer with integer zero");
8993 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8995 result_type = type0;
8996 pedwarn (location, 0, "comparison between pointer and integer");
8998 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9000 result_type = type1;
9001 pedwarn (location, 0, "comparison between pointer and integer");
9009 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9010 return error_mark_node;
9012 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9013 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9014 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9015 TREE_TYPE (type1))))
9017 binary_op_error (location, code, type0, type1);
9018 return error_mark_node;
9021 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9022 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9024 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9025 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9027 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
9029 if (shorten || common || short_compare)
9031 result_type = c_common_type (type0, type1);
9032 if (result_type == error_mark_node)
9033 return error_mark_node;
9036 /* For certain operations (which identify themselves by shorten != 0)
9037 if both args were extended from the same smaller type,
9038 do the arithmetic in that type and then extend.
9040 shorten !=0 and !=1 indicates a bitwise operation.
9041 For them, this optimization is safe only if
9042 both args are zero-extended or both are sign-extended.
9043 Otherwise, we might change the result.
9044 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9045 but calculated in (unsigned short) it would be (unsigned short)-1. */
9047 if (shorten && none_complex)
9049 final_type = result_type;
9050 result_type = shorten_binary_op (result_type, op0, op1,
9054 /* Shifts can be shortened if shifting right. */
9059 tree arg0 = get_narrower (op0, &unsigned_arg);
9061 final_type = result_type;
9063 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9064 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9066 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9067 /* We can shorten only if the shift count is less than the
9068 number of bits in the smaller type size. */
9069 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9070 /* We cannot drop an unsigned shift after sign-extension. */
9071 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9073 /* Do an unsigned shift if the operand was zero-extended. */
9075 = c_common_signed_or_unsigned_type (unsigned_arg,
9077 /* Convert value-to-be-shifted to that type. */
9078 if (TREE_TYPE (op0) != result_type)
9079 op0 = convert (result_type, op0);
9084 /* Comparison operations are shortened too but differently.
9085 They identify themselves by setting short_compare = 1. */
9089 /* Don't write &op0, etc., because that would prevent op0
9090 from being kept in a register.
9091 Instead, make copies of the our local variables and
9092 pass the copies by reference, then copy them back afterward. */
9093 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9094 enum tree_code xresultcode = resultcode;
9096 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9101 goto return_build_binary_op;
9104 op0 = xop0, op1 = xop1;
9106 resultcode = xresultcode;
9108 if (!skip_evaluation)
9110 bool op0_maybe_const = true;
9111 bool op1_maybe_const = true;
9112 tree orig_op0_folded, orig_op1_folded;
9114 if (in_late_binary_op)
9116 orig_op0_folded = orig_op0;
9117 orig_op1_folded = orig_op1;
9121 /* Fold for the sake of possible warnings, as in
9122 build_conditional_expr. This requires the
9123 "original" values to be folded, not just op0 and
9125 op0 = c_fully_fold (op0, require_constant_value,
9127 op1 = c_fully_fold (op1, require_constant_value,
9129 orig_op0_folded = c_fully_fold (orig_op0,
9130 require_constant_value,
9132 orig_op1_folded = c_fully_fold (orig_op1,
9133 require_constant_value,
9137 if (warn_sign_compare)
9138 warn_for_sign_compare (location, orig_op0_folded,
9139 orig_op1_folded, op0, op1,
9140 result_type, resultcode);
9141 if (!in_late_binary_op)
9143 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
9145 op0 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op0),
9147 C_MAYBE_CONST_EXPR_NON_CONST (op0) = !op0_maybe_const;
9149 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
9151 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
9153 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
9160 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9161 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9162 Then the expression will be built.
9163 It will be given type FINAL_TYPE if that is nonzero;
9164 otherwise, it will be given type RESULT_TYPE. */
9168 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
9169 return error_mark_node;
9174 if (TREE_TYPE (op0) != result_type)
9175 op0 = convert_and_check (result_type, op0);
9176 if (TREE_TYPE (op1) != result_type)
9177 op1 = convert_and_check (result_type, op1);
9179 /* This can happen if one operand has a vector type, and the other
9180 has a different type. */
9181 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9182 return error_mark_node;
9185 if (build_type == NULL_TREE)
9187 build_type = result_type;
9188 if (type0 != orig_type0 || type1 != orig_type1)
9190 gcc_assert (may_need_excess_precision && common);
9191 real_result_type = c_common_type (orig_type0, orig_type1);
9195 /* Treat expressions in initializers specially as they can't trap. */
9196 if (int_const_or_overflow)
9197 ret = (require_constant_value
9198 ? fold_build2_initializer (resultcode, build_type, op0, op1)
9199 : fold_build2 (resultcode, build_type, op0, op1));
9201 ret = build2 (resultcode, build_type, op0, op1);
9202 if (final_type != 0)
9203 ret = convert (final_type, ret);
9205 return_build_binary_op:
9206 gcc_assert (ret != error_mark_node);
9207 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
9209 ? note_integer_operands (ret)
9210 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
9211 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
9212 && !in_late_binary_op)
9213 ret = note_integer_operands (ret);
9214 if (real_result_type)
9215 ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret);
9216 protected_set_expr_location (ret, location);
9221 /* Convert EXPR to be a truth-value, validating its type for this
9222 purpose. LOCATION is the source location for the expression. */
9225 c_objc_common_truthvalue_conversion (location_t location, tree expr)
9227 bool int_const, int_operands;
9229 switch (TREE_CODE (TREE_TYPE (expr)))
9232 error_at (location, "used array that cannot be converted to pointer where scalar is required");
9233 return error_mark_node;
9236 error_at (location, "used struct type value where scalar is required");
9237 return error_mark_node;
9240 error_at (location, "used union type value where scalar is required");
9241 return error_mark_node;
9250 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
9251 int_operands = EXPR_INT_CONST_OPERANDS (expr);
9253 expr = remove_c_maybe_const_expr (expr);
9255 /* ??? Should we also give an error for void and vectors rather than
9256 leaving those to give errors later? */
9257 expr = c_common_truthvalue_conversion (location, expr);
9259 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
9261 if (TREE_OVERFLOW (expr))
9264 return note_integer_operands (expr);
9266 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
9267 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9272 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9276 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
9278 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
9280 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
9281 /* Executing a compound literal inside a function reinitializes
9283 if (!TREE_STATIC (decl))
9291 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9294 c_begin_omp_parallel (void)
9299 block = c_begin_compound_stmt (true);
9304 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound statement. */
9307 c_finish_omp_parallel (tree clauses, tree block)
9311 block = c_end_compound_stmt (block, true);
9313 stmt = make_node (OMP_PARALLEL);
9314 TREE_TYPE (stmt) = void_type_node;
9315 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9316 OMP_PARALLEL_BODY (stmt) = block;
9318 return add_stmt (stmt);
9321 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9324 c_begin_omp_task (void)
9329 block = c_begin_compound_stmt (true);
9334 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound statement. */
9337 c_finish_omp_task (tree clauses, tree block)
9341 block = c_end_compound_stmt (block, true);
9343 stmt = make_node (OMP_TASK);
9344 TREE_TYPE (stmt) = void_type_node;
9345 OMP_TASK_CLAUSES (stmt) = clauses;
9346 OMP_TASK_BODY (stmt) = block;
9348 return add_stmt (stmt);
9351 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9352 Remove any elements from the list that are invalid. */
9355 c_finish_omp_clauses (tree clauses)
9357 bitmap_head generic_head, firstprivate_head, lastprivate_head;
9358 tree c, t, *pc = &clauses;
9361 bitmap_obstack_initialize (NULL);
9362 bitmap_initialize (&generic_head, &bitmap_default_obstack);
9363 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
9364 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
9366 for (pc = &clauses, c = clauses; c ; c = *pc)
9368 bool remove = false;
9369 bool need_complete = false;
9370 bool need_implicitly_determined = false;
9372 switch (OMP_CLAUSE_CODE (c))
9374 case OMP_CLAUSE_SHARED:
9376 need_implicitly_determined = true;
9377 goto check_dup_generic;
9379 case OMP_CLAUSE_PRIVATE:
9381 need_complete = true;
9382 need_implicitly_determined = true;
9383 goto check_dup_generic;
9385 case OMP_CLAUSE_REDUCTION:
9387 need_implicitly_determined = true;
9388 t = OMP_CLAUSE_DECL (c);
9389 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
9390 || POINTER_TYPE_P (TREE_TYPE (t)))
9392 error ("%qE has invalid type for %<reduction%>", t);
9395 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
9397 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
9398 const char *r_name = NULL;
9415 case TRUTH_ANDIF_EXPR:
9418 case TRUTH_ORIF_EXPR:
9426 error ("%qE has invalid type for %<reduction(%s)%>",
9431 goto check_dup_generic;
9433 case OMP_CLAUSE_COPYPRIVATE:
9434 name = "copyprivate";
9435 goto check_dup_generic;
9437 case OMP_CLAUSE_COPYIN:
9439 t = OMP_CLAUSE_DECL (c);
9440 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
9442 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
9445 goto check_dup_generic;
9448 t = OMP_CLAUSE_DECL (c);
9449 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9451 error ("%qE is not a variable in clause %qs", t, name);
9454 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9455 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9456 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9458 error ("%qE appears more than once in data clauses", t);
9462 bitmap_set_bit (&generic_head, DECL_UID (t));
9465 case OMP_CLAUSE_FIRSTPRIVATE:
9466 name = "firstprivate";
9467 t = OMP_CLAUSE_DECL (c);
9468 need_complete = true;
9469 need_implicitly_determined = true;
9470 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9472 error ("%qE is not a variable in clause %<firstprivate%>", t);
9475 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9476 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9478 error ("%qE appears more than once in data clauses", t);
9482 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
9485 case OMP_CLAUSE_LASTPRIVATE:
9486 name = "lastprivate";
9487 t = OMP_CLAUSE_DECL (c);
9488 need_complete = true;
9489 need_implicitly_determined = true;
9490 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9492 error ("%qE is not a variable in clause %<lastprivate%>", t);
9495 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9496 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9498 error ("%qE appears more than once in data clauses", t);
9502 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
9506 case OMP_CLAUSE_NUM_THREADS:
9507 case OMP_CLAUSE_SCHEDULE:
9508 case OMP_CLAUSE_NOWAIT:
9509 case OMP_CLAUSE_ORDERED:
9510 case OMP_CLAUSE_DEFAULT:
9511 case OMP_CLAUSE_UNTIED:
9512 case OMP_CLAUSE_COLLAPSE:
9513 pc = &OMP_CLAUSE_CHAIN (c);
9522 t = OMP_CLAUSE_DECL (c);
9526 t = require_complete_type (t);
9527 if (t == error_mark_node)
9531 if (need_implicitly_determined)
9533 const char *share_name = NULL;
9535 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
9536 share_name = "threadprivate";
9537 else switch (c_omp_predetermined_sharing (t))
9539 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9541 case OMP_CLAUSE_DEFAULT_SHARED:
9542 share_name = "shared";
9544 case OMP_CLAUSE_DEFAULT_PRIVATE:
9545 share_name = "private";
9552 error ("%qE is predetermined %qs for %qs",
9553 t, share_name, name);
9560 *pc = OMP_CLAUSE_CHAIN (c);
9562 pc = &OMP_CLAUSE_CHAIN (c);
9565 bitmap_obstack_release (NULL);
9569 /* Make a variant type in the proper way for C/C++, propagating qualifiers
9570 down to the element type of an array. */
9573 c_build_qualified_type (tree type, int type_quals)
9575 if (type == error_mark_node)
9578 if (TREE_CODE (type) == ARRAY_TYPE)
9581 tree element_type = c_build_qualified_type (TREE_TYPE (type),
9584 /* See if we already have an identically qualified type. */
9585 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9587 if (TYPE_QUALS (strip_array_types (t)) == type_quals
9588 && TYPE_NAME (t) == TYPE_NAME (type)
9589 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
9590 && attribute_list_equal (TYPE_ATTRIBUTES (t),
9591 TYPE_ATTRIBUTES (type)))
9596 tree domain = TYPE_DOMAIN (type);
9598 t = build_variant_type_copy (type);
9599 TREE_TYPE (t) = element_type;
9601 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
9602 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
9603 SET_TYPE_STRUCTURAL_EQUALITY (t);
9604 else if (TYPE_CANONICAL (element_type) != element_type
9605 || (domain && TYPE_CANONICAL (domain) != domain))
9607 tree unqualified_canon
9608 = build_array_type (TYPE_CANONICAL (element_type),
9609 domain? TYPE_CANONICAL (domain)
9612 = c_build_qualified_type (unqualified_canon, type_quals);
9615 TYPE_CANONICAL (t) = t;
9620 /* A restrict-qualified pointer type must be a pointer to object or
9621 incomplete type. Note that the use of POINTER_TYPE_P also allows
9622 REFERENCE_TYPEs, which is appropriate for C++. */
9623 if ((type_quals & TYPE_QUAL_RESTRICT)
9624 && (!POINTER_TYPE_P (type)
9625 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
9627 error ("invalid use of %<restrict%>");
9628 type_quals &= ~TYPE_QUAL_RESTRICT;
9631 return build_qualified_type (type, type_quals);