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. This sets *TYPE to the type of the
2206 identifier, which is not the same as the type of the returned value
2207 for CONST_DECLs defined as enum constants. If the type of the
2208 identifier is not available, *TYPE is set to NULL. */
2210 build_external_ref (tree id, int fun, location_t loc, tree *type)
2213 tree decl = lookup_name (id);
2215 /* In Objective-C, an instance variable (ivar) may be preferred to
2216 whatever lookup_name() found. */
2217 decl = objc_lookup_ivar (decl, id);
2220 if (decl && decl != error_mark_node)
2223 *type = TREE_TYPE (ref);
2226 /* Implicit function declaration. */
2227 ref = implicitly_declare (id);
2228 else if (decl == error_mark_node)
2229 /* Don't complain about something that's already been
2230 complained about. */
2231 return error_mark_node;
2234 undeclared_variable (id, loc);
2235 return error_mark_node;
2238 if (TREE_TYPE (ref) == error_mark_node)
2239 return error_mark_node;
2241 if (TREE_DEPRECATED (ref))
2242 warn_deprecated_use (ref);
2244 /* Recursive call does not count as usage. */
2245 if (ref != current_function_decl)
2247 TREE_USED (ref) = 1;
2250 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2252 if (!in_sizeof && !in_typeof)
2253 C_DECL_USED (ref) = 1;
2254 else if (DECL_INITIAL (ref) == 0
2255 && DECL_EXTERNAL (ref)
2256 && !TREE_PUBLIC (ref))
2257 record_maybe_used_decl (ref);
2260 if (TREE_CODE (ref) == CONST_DECL)
2262 used_types_insert (TREE_TYPE (ref));
2263 ref = DECL_INITIAL (ref);
2264 TREE_CONSTANT (ref) = 1;
2266 else if (current_function_decl != 0
2267 && !DECL_FILE_SCOPE_P (current_function_decl)
2268 && (TREE_CODE (ref) == VAR_DECL
2269 || TREE_CODE (ref) == PARM_DECL
2270 || TREE_CODE (ref) == FUNCTION_DECL))
2272 tree context = decl_function_context (ref);
2274 if (context != 0 && context != current_function_decl)
2275 DECL_NONLOCAL (ref) = 1;
2277 /* C99 6.7.4p3: An inline definition of a function with external
2278 linkage ... shall not contain a reference to an identifier with
2279 internal linkage. */
2280 else if (current_function_decl != 0
2281 && DECL_DECLARED_INLINE_P (current_function_decl)
2282 && DECL_EXTERNAL (current_function_decl)
2283 && VAR_OR_FUNCTION_DECL_P (ref)
2284 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2285 && ! TREE_PUBLIC (ref)
2286 && DECL_CONTEXT (ref) != current_function_decl)
2287 pedwarn (loc, 0, "%qD is static but used in inline function %qD "
2288 "which is not static", ref, current_function_decl);
2293 /* Record details of decls possibly used inside sizeof or typeof. */
2294 struct maybe_used_decl
2298 /* The level seen at (in_sizeof + in_typeof). */
2300 /* The next one at this level or above, or NULL. */
2301 struct maybe_used_decl *next;
2304 static struct maybe_used_decl *maybe_used_decls;
2306 /* Record that DECL, an undefined static function reference seen
2307 inside sizeof or typeof, might be used if the operand of sizeof is
2308 a VLA type or the operand of typeof is a variably modified
2312 record_maybe_used_decl (tree decl)
2314 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2316 t->level = in_sizeof + in_typeof;
2317 t->next = maybe_used_decls;
2318 maybe_used_decls = t;
2321 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2322 USED is false, just discard them. If it is true, mark them used
2323 (if no longer inside sizeof or typeof) or move them to the next
2324 level up (if still inside sizeof or typeof). */
2327 pop_maybe_used (bool used)
2329 struct maybe_used_decl *p = maybe_used_decls;
2330 int cur_level = in_sizeof + in_typeof;
2331 while (p && p->level > cur_level)
2336 C_DECL_USED (p->decl) = 1;
2338 p->level = cur_level;
2342 if (!used || cur_level == 0)
2343 maybe_used_decls = p;
2346 /* Return the result of sizeof applied to EXPR. */
2349 c_expr_sizeof_expr (struct c_expr expr)
2352 if (expr.value == error_mark_node)
2354 ret.value = error_mark_node;
2355 ret.original_code = ERROR_MARK;
2356 ret.original_type = NULL;
2357 pop_maybe_used (false);
2361 bool expr_const_operands = true;
2362 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2363 &expr_const_operands);
2364 ret.value = c_sizeof (TREE_TYPE (folded_expr));
2365 ret.original_code = ERROR_MARK;
2366 ret.original_type = NULL;
2367 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2369 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2370 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2371 folded_expr, ret.value);
2372 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2374 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2379 /* Return the result of sizeof applied to T, a structure for the type
2380 name passed to sizeof (rather than the type itself). */
2383 c_expr_sizeof_type (struct c_type_name *t)
2387 tree type_expr = NULL_TREE;
2388 bool type_expr_const = true;
2389 type = groktypename (t, &type_expr, &type_expr_const);
2390 ret.value = c_sizeof (type);
2391 ret.original_code = ERROR_MARK;
2392 ret.original_type = NULL;
2393 if (type_expr && c_vla_type_p (type))
2395 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2396 type_expr, ret.value);
2397 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2399 pop_maybe_used (type != error_mark_node
2400 ? C_TYPE_VARIABLE_SIZE (type) : false);
2404 /* Build a function call to function FUNCTION with parameters PARAMS.
2405 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2406 TREE_VALUE of each node is a parameter-expression.
2407 FUNCTION's data type may be a function type or a pointer-to-function. */
2410 build_function_call (tree function, tree params)
2412 tree fntype, fundecl = 0;
2413 tree name = NULL_TREE, result;
2419 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2420 STRIP_TYPE_NOPS (function);
2422 /* Convert anything with function type to a pointer-to-function. */
2423 if (TREE_CODE (function) == FUNCTION_DECL)
2425 /* Implement type-directed function overloading for builtins.
2426 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2427 handle all the type checking. The result is a complete expression
2428 that implements this function call. */
2429 tem = resolve_overloaded_builtin (function, params);
2433 name = DECL_NAME (function);
2436 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2437 function = function_to_pointer_conversion (function);
2439 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2440 expressions, like those used for ObjC messenger dispatches. */
2441 function = objc_rewrite_function_call (function, params);
2443 function = c_fully_fold (function, false, NULL);
2445 fntype = TREE_TYPE (function);
2447 if (TREE_CODE (fntype) == ERROR_MARK)
2448 return error_mark_node;
2450 if (!(TREE_CODE (fntype) == POINTER_TYPE
2451 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2453 error ("called object %qE is not a function", function);
2454 return error_mark_node;
2457 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2458 current_function_returns_abnormally = 1;
2460 /* fntype now gets the type of function pointed to. */
2461 fntype = TREE_TYPE (fntype);
2463 /* Convert the parameters to the types declared in the
2464 function prototype, or apply default promotions. */
2466 nargs = list_length (params);
2467 argarray = (tree *) alloca (nargs * sizeof (tree));
2468 nargs = convert_arguments (nargs, argarray, TYPE_ARG_TYPES (fntype),
2469 params, function, fundecl);
2471 return error_mark_node;
2473 /* Check that the function is called through a compatible prototype.
2474 If it is not, replace the call by a trap, wrapped up in a compound
2475 expression if necessary. This has the nice side-effect to prevent
2476 the tree-inliner from generating invalid assignment trees which may
2477 blow up in the RTL expander later. */
2478 if (CONVERT_EXPR_P (function)
2479 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2480 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2481 && !comptypes (fntype, TREE_TYPE (tem)))
2483 tree return_type = TREE_TYPE (fntype);
2484 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2488 /* This situation leads to run-time undefined behavior. We can't,
2489 therefore, simply error unless we can prove that all possible
2490 executions of the program must execute the code. */
2491 if (warning (0, "function called through a non-compatible type"))
2492 /* We can, however, treat "undefined" any way we please.
2493 Call abort to encourage the user to fix the program. */
2494 inform (input_location, "if this code is reached, the program will abort");
2495 /* Before the abort, allow the function arguments to exit or
2497 for (i = 0; i < nargs; i++)
2498 trap = build2 (COMPOUND_EXPR, void_type_node, argarray[i], trap);
2500 if (VOID_TYPE_P (return_type))
2502 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2503 pedwarn (input_location, 0,
2504 "function with qualified void return type called");
2511 if (AGGREGATE_TYPE_P (return_type))
2512 rhs = build_compound_literal (return_type,
2513 build_constructor (return_type, 0),
2516 rhs = fold_convert (return_type, integer_zero_node);
2518 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2523 /* Check that arguments to builtin functions match the expectations. */
2525 && DECL_BUILT_IN (fundecl)
2526 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2527 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2528 return error_mark_node;
2530 /* Check that the arguments to the function are valid. */
2531 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2532 TYPE_ARG_TYPES (fntype));
2534 if (name != NULL_TREE
2535 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2537 if (require_constant_value)
2538 result = fold_build_call_array_initializer (TREE_TYPE (fntype),
2539 function, nargs, argarray);
2541 result = fold_build_call_array (TREE_TYPE (fntype),
2542 function, nargs, argarray);
2543 if (TREE_CODE (result) == NOP_EXPR
2544 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2545 STRIP_TYPE_NOPS (result);
2548 result = build_call_array (TREE_TYPE (fntype),
2549 function, nargs, argarray);
2551 if (VOID_TYPE_P (TREE_TYPE (result)))
2553 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2554 pedwarn (input_location, 0,
2555 "function with qualified void return type called");
2558 return require_complete_type (result);
2561 /* Convert the argument expressions in the list VALUES
2562 to the types in the list TYPELIST. The resulting arguments are
2563 stored in the array ARGARRAY which has size NARGS.
2565 If TYPELIST is exhausted, or when an element has NULL as its type,
2566 perform the default conversions.
2568 PARMLIST is the chain of parm decls for the function being called.
2569 It may be 0, if that info is not available.
2570 It is used only for generating error messages.
2572 FUNCTION is a tree for the called function. It is used only for
2573 error messages, where it is formatted with %qE.
2575 This is also where warnings about wrong number of args are generated.
2577 VALUES is a chain of TREE_LIST nodes with the elements of the list
2578 in the TREE_VALUE slots of those nodes.
2580 Returns the actual number of arguments processed (which may be less
2581 than NARGS in some error situations), or -1 on failure. */
2584 convert_arguments (int nargs, tree *argarray,
2585 tree typelist, tree values, tree function, tree fundecl)
2587 tree typetail, valtail;
2589 const bool type_generic = fundecl
2590 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2591 bool type_generic_remove_excess_precision = false;
2594 /* Change pointer to function to the function itself for
2596 if (TREE_CODE (function) == ADDR_EXPR
2597 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2598 function = TREE_OPERAND (function, 0);
2600 /* Handle an ObjC selector specially for diagnostics. */
2601 selector = objc_message_selector ();
2603 /* For type-generic built-in functions, determine whether excess
2604 precision should be removed (classification) or not
2607 && DECL_BUILT_IN (fundecl)
2608 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2610 switch (DECL_FUNCTION_CODE (fundecl))
2612 case BUILT_IN_ISFINITE:
2613 case BUILT_IN_ISINF:
2614 case BUILT_IN_ISINF_SIGN:
2615 case BUILT_IN_ISNAN:
2616 case BUILT_IN_ISNORMAL:
2617 case BUILT_IN_FPCLASSIFY:
2618 type_generic_remove_excess_precision = true;
2622 type_generic_remove_excess_precision = false;
2627 /* Scan the given expressions and types, producing individual
2628 converted arguments and storing them in ARGARRAY. */
2630 for (valtail = values, typetail = typelist, parmnum = 0;
2632 valtail = TREE_CHAIN (valtail), parmnum++)
2634 tree type = typetail ? TREE_VALUE (typetail) : 0;
2635 tree val = TREE_VALUE (valtail);
2636 tree valtype = TREE_TYPE (val);
2637 tree rname = function;
2638 int argnum = parmnum + 1;
2639 const char *invalid_func_diag;
2640 bool excess_precision = false;
2643 if (type == void_type_node)
2645 error ("too many arguments to function %qE", function);
2649 if (selector && argnum > 2)
2655 npc = null_pointer_constant_p (val);
2657 /* If there is excess precision and a prototype, convert once to
2658 the required type rather than converting via the semantic
2659 type. Likewise without a prototype a float value represented
2660 as long double should be converted once to double. But for
2661 type-generic classification functions excess precision must
2663 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2664 && (type || !type_generic || !type_generic_remove_excess_precision))
2666 val = TREE_OPERAND (val, 0);
2667 excess_precision = true;
2669 val = c_fully_fold (val, false, NULL);
2670 STRIP_TYPE_NOPS (val);
2672 val = require_complete_type (val);
2676 /* Formal parm type is specified by a function prototype. */
2679 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2681 error ("type of formal parameter %d is incomplete", parmnum + 1);
2686 /* Optionally warn about conversions that
2687 differ from the default conversions. */
2688 if (warn_traditional_conversion || warn_traditional)
2690 unsigned int formal_prec = TYPE_PRECISION (type);
2692 if (INTEGRAL_TYPE_P (type)
2693 && TREE_CODE (valtype) == REAL_TYPE)
2694 warning (0, "passing argument %d of %qE as integer "
2695 "rather than floating due to prototype",
2697 if (INTEGRAL_TYPE_P (type)
2698 && TREE_CODE (valtype) == COMPLEX_TYPE)
2699 warning (0, "passing argument %d of %qE as integer "
2700 "rather than complex due to prototype",
2702 else if (TREE_CODE (type) == COMPLEX_TYPE
2703 && TREE_CODE (valtype) == REAL_TYPE)
2704 warning (0, "passing argument %d of %qE as complex "
2705 "rather than floating due to prototype",
2707 else if (TREE_CODE (type) == REAL_TYPE
2708 && INTEGRAL_TYPE_P (valtype))
2709 warning (0, "passing argument %d of %qE as floating "
2710 "rather than integer due to prototype",
2712 else if (TREE_CODE (type) == COMPLEX_TYPE
2713 && INTEGRAL_TYPE_P (valtype))
2714 warning (0, "passing argument %d of %qE as complex "
2715 "rather than integer due to prototype",
2717 else if (TREE_CODE (type) == REAL_TYPE
2718 && TREE_CODE (valtype) == COMPLEX_TYPE)
2719 warning (0, "passing argument %d of %qE as floating "
2720 "rather than complex due to prototype",
2722 /* ??? At some point, messages should be written about
2723 conversions between complex types, but that's too messy
2725 else if (TREE_CODE (type) == REAL_TYPE
2726 && TREE_CODE (valtype) == REAL_TYPE)
2728 /* Warn if any argument is passed as `float',
2729 since without a prototype it would be `double'. */
2730 if (formal_prec == TYPE_PRECISION (float_type_node)
2731 && type != dfloat32_type_node)
2732 warning (0, "passing argument %d of %qE as %<float%> "
2733 "rather than %<double%> due to prototype",
2736 /* Warn if mismatch between argument and prototype
2737 for decimal float types. Warn of conversions with
2738 binary float types and of precision narrowing due to
2740 else if (type != valtype
2741 && (type == dfloat32_type_node
2742 || type == dfloat64_type_node
2743 || type == dfloat128_type_node
2744 || valtype == dfloat32_type_node
2745 || valtype == dfloat64_type_node
2746 || valtype == dfloat128_type_node)
2748 <= TYPE_PRECISION (valtype)
2749 || (type == dfloat128_type_node
2751 != dfloat64_type_node
2753 != dfloat32_type_node)))
2754 || (type == dfloat64_type_node
2756 != dfloat32_type_node))))
2757 warning (0, "passing argument %d of %qE as %qT "
2758 "rather than %qT due to prototype",
2759 argnum, rname, type, valtype);
2762 /* Detect integer changing in width or signedness.
2763 These warnings are only activated with
2764 -Wtraditional-conversion, not with -Wtraditional. */
2765 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2766 && INTEGRAL_TYPE_P (valtype))
2768 tree would_have_been = default_conversion (val);
2769 tree type1 = TREE_TYPE (would_have_been);
2771 if (TREE_CODE (type) == ENUMERAL_TYPE
2772 && (TYPE_MAIN_VARIANT (type)
2773 == TYPE_MAIN_VARIANT (valtype)))
2774 /* No warning if function asks for enum
2775 and the actual arg is that enum type. */
2777 else if (formal_prec != TYPE_PRECISION (type1))
2778 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2779 "with different width due to prototype",
2781 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2783 /* Don't complain if the formal parameter type
2784 is an enum, because we can't tell now whether
2785 the value was an enum--even the same enum. */
2786 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2788 else if (TREE_CODE (val) == INTEGER_CST
2789 && int_fits_type_p (val, type))
2790 /* Change in signedness doesn't matter
2791 if a constant value is unaffected. */
2793 /* If the value is extended from a narrower
2794 unsigned type, it doesn't matter whether we
2795 pass it as signed or unsigned; the value
2796 certainly is the same either way. */
2797 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
2798 && TYPE_UNSIGNED (valtype))
2800 else if (TYPE_UNSIGNED (type))
2801 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2802 "as unsigned due to prototype",
2805 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2806 "as signed due to prototype", argnum, rname);
2810 /* Possibly restore an EXCESS_PRECISION_EXPR for the
2811 sake of better warnings from convert_and_check. */
2812 if (excess_precision)
2813 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
2814 parmval = convert_for_assignment (type, val, ic_argpass, npc,
2818 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2819 && INTEGRAL_TYPE_P (type)
2820 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2821 parmval = default_conversion (parmval);
2823 argarray[parmnum] = parmval;
2825 else if (TREE_CODE (valtype) == REAL_TYPE
2826 && (TYPE_PRECISION (valtype)
2827 < TYPE_PRECISION (double_type_node))
2828 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
2831 argarray[parmnum] = val;
2833 /* Convert `float' to `double'. */
2834 argarray[parmnum] = convert (double_type_node, val);
2836 else if (excess_precision && !type_generic)
2837 /* A "double" argument with excess precision being passed
2838 without a prototype or in variable arguments. */
2839 argarray[parmnum] = convert (valtype, val);
2840 else if ((invalid_func_diag =
2841 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2843 error (invalid_func_diag);
2847 /* Convert `short' and `char' to full-size `int'. */
2848 argarray[parmnum] = default_conversion (val);
2851 typetail = TREE_CHAIN (typetail);
2854 gcc_assert (parmnum == nargs);
2856 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2858 error ("too few arguments to function %qE", function);
2865 /* This is the entry point used by the parser to build unary operators
2866 in the input. CODE, a tree_code, specifies the unary operator, and
2867 ARG is the operand. For unary plus, the C parser currently uses
2868 CONVERT_EXPR for code.
2870 LOC is the location to use for the tree generated.
2874 parser_build_unary_op (enum tree_code code, struct c_expr arg, location_t loc)
2876 struct c_expr result;
2878 result.value = build_unary_op (loc, code, arg.value, 0);
2879 result.original_code = code;
2880 result.original_type = NULL;
2882 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2883 overflow_warning (result.value);
2888 /* This is the entry point used by the parser to build binary operators
2889 in the input. CODE, a tree_code, specifies the binary operator, and
2890 ARG1 and ARG2 are the operands. In addition to constructing the
2891 expression, we check for operands that were written with other binary
2892 operators in a way that is likely to confuse the user.
2894 LOCATION is the location of the binary operator. */
2897 parser_build_binary_op (location_t location, enum tree_code code,
2898 struct c_expr arg1, struct c_expr arg2)
2900 struct c_expr result;
2902 enum tree_code code1 = arg1.original_code;
2903 enum tree_code code2 = arg2.original_code;
2904 tree type1 = (arg1.original_type
2905 ? arg1.original_type
2906 : TREE_TYPE (arg1.value));
2907 tree type2 = (arg2.original_type
2908 ? arg2.original_type
2909 : TREE_TYPE (arg2.value));
2911 result.value = build_binary_op (location, code,
2912 arg1.value, arg2.value, 1);
2913 result.original_code = code;
2914 result.original_type = NULL;
2916 if (TREE_CODE (result.value) == ERROR_MARK)
2919 if (location != UNKNOWN_LOCATION)
2920 protected_set_expr_location (result.value, location);
2922 /* Check for cases such as x+y<<z which users are likely
2924 if (warn_parentheses)
2925 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
2927 if (TREE_CODE_CLASS (code1) != tcc_comparison)
2928 warn_logical_operator (code, arg1.value, arg2.value);
2930 /* Warn about comparisons against string literals, with the exception
2931 of testing for equality or inequality of a string literal with NULL. */
2932 if (code == EQ_EXPR || code == NE_EXPR)
2934 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2935 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2936 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2938 else if (TREE_CODE_CLASS (code) == tcc_comparison
2939 && (code1 == STRING_CST || code2 == STRING_CST))
2940 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2942 if (TREE_OVERFLOW_P (result.value)
2943 && !TREE_OVERFLOW_P (arg1.value)
2944 && !TREE_OVERFLOW_P (arg2.value))
2945 overflow_warning (result.value);
2947 /* Warn about comparisons of different enum types. */
2948 if (warn_enum_compare
2949 && TREE_CODE_CLASS (code) == tcc_comparison
2950 && TREE_CODE (type1) == ENUMERAL_TYPE
2951 && TREE_CODE (type2) == ENUMERAL_TYPE
2952 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
2953 warning_at (location, OPT_Wenum_compare,
2954 "comparison between %qT and %qT",
2960 /* Return a tree for the difference of pointers OP0 and OP1.
2961 The resulting tree has type int. */
2964 pointer_diff (tree op0, tree op1)
2966 tree restype = ptrdiff_type_node;
2968 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2969 tree con0, con1, lit0, lit1;
2970 tree orig_op1 = op1;
2972 if (TREE_CODE (target_type) == VOID_TYPE)
2973 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
2974 "pointer of type %<void *%> used in subtraction");
2975 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2976 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
2977 "pointer to a function used in subtraction");
2979 /* If the conversion to ptrdiff_type does anything like widening or
2980 converting a partial to an integral mode, we get a convert_expression
2981 that is in the way to do any simplifications.
2982 (fold-const.c doesn't know that the extra bits won't be needed.
2983 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2984 different mode in place.)
2985 So first try to find a common term here 'by hand'; we want to cover
2986 at least the cases that occur in legal static initializers. */
2987 if (CONVERT_EXPR_P (op0)
2988 && (TYPE_PRECISION (TREE_TYPE (op0))
2989 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2990 con0 = TREE_OPERAND (op0, 0);
2993 if (CONVERT_EXPR_P (op1)
2994 && (TYPE_PRECISION (TREE_TYPE (op1))
2995 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2996 con1 = TREE_OPERAND (op1, 0);
3000 if (TREE_CODE (con0) == PLUS_EXPR)
3002 lit0 = TREE_OPERAND (con0, 1);
3003 con0 = TREE_OPERAND (con0, 0);
3006 lit0 = integer_zero_node;
3008 if (TREE_CODE (con1) == PLUS_EXPR)
3010 lit1 = TREE_OPERAND (con1, 1);
3011 con1 = TREE_OPERAND (con1, 0);
3014 lit1 = integer_zero_node;
3016 if (operand_equal_p (con0, con1, 0))
3023 /* First do the subtraction as integers;
3024 then drop through to build the divide operator.
3025 Do not do default conversions on the minus operator
3026 in case restype is a short type. */
3028 op0 = build_binary_op (input_location,
3029 MINUS_EXPR, convert (restype, op0),
3030 convert (restype, op1), 0);
3031 /* This generates an error if op1 is pointer to incomplete type. */
3032 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3033 error ("arithmetic on pointer to an incomplete type");
3035 /* This generates an error if op0 is pointer to incomplete type. */
3036 op1 = c_size_in_bytes (target_type);
3038 /* Divide by the size, in easiest possible way. */
3039 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3042 /* Construct and perhaps optimize a tree representation
3043 for a unary operation. CODE, a tree_code, specifies the operation
3044 and XARG is the operand.
3045 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3046 the default promotions (such as from short to int).
3047 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3048 allows non-lvalues; this is only used to handle conversion of non-lvalue
3049 arrays to pointers in C99.
3051 LOCATION is the location of the operator. */
3054 build_unary_op (location_t location,
3055 enum tree_code code, tree xarg, int flag)
3057 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3060 enum tree_code typecode;
3062 tree ret = error_mark_node;
3063 tree eptype = NULL_TREE;
3064 int noconvert = flag;
3065 const char *invalid_op_diag;
3068 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3070 arg = remove_c_maybe_const_expr (arg);
3072 if (code != ADDR_EXPR)
3073 arg = require_complete_type (arg);
3075 typecode = TREE_CODE (TREE_TYPE (arg));
3076 if (typecode == ERROR_MARK)
3077 return error_mark_node;
3078 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3079 typecode = INTEGER_TYPE;
3081 if ((invalid_op_diag
3082 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3084 error_at (location, invalid_op_diag);
3085 return error_mark_node;
3088 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3090 eptype = TREE_TYPE (arg);
3091 arg = TREE_OPERAND (arg, 0);
3097 /* This is used for unary plus, because a CONVERT_EXPR
3098 is enough to prevent anybody from looking inside for
3099 associativity, but won't generate any code. */
3100 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3101 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3102 || typecode == VECTOR_TYPE))
3104 error_at (location, "wrong type argument to unary plus");
3105 return error_mark_node;
3107 else if (!noconvert)
3108 arg = default_conversion (arg);
3109 arg = non_lvalue (arg);
3113 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3114 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3115 || typecode == VECTOR_TYPE))
3117 error_at (location, "wrong type argument to unary minus");
3118 return error_mark_node;
3120 else if (!noconvert)
3121 arg = default_conversion (arg);
3125 /* ~ works on integer types and non float vectors. */
3126 if (typecode == INTEGER_TYPE
3127 || (typecode == VECTOR_TYPE
3128 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3131 arg = default_conversion (arg);
3133 else if (typecode == COMPLEX_TYPE)
3136 pedwarn (location, OPT_pedantic,
3137 "ISO C does not support %<~%> for complex conjugation");
3139 arg = default_conversion (arg);
3143 error_at (location, "wrong type argument to bit-complement");
3144 return error_mark_node;
3149 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3151 error_at (location, "wrong type argument to abs");
3152 return error_mark_node;
3154 else if (!noconvert)
3155 arg = default_conversion (arg);
3159 /* Conjugating a real value is a no-op, but allow it anyway. */
3160 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3161 || typecode == COMPLEX_TYPE))
3163 error_at (location, "wrong type argument to conjugation");
3164 return error_mark_node;
3166 else if (!noconvert)
3167 arg = default_conversion (arg);
3170 case TRUTH_NOT_EXPR:
3171 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3172 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3173 && typecode != COMPLEX_TYPE)
3176 "wrong type argument to unary exclamation mark");
3177 return error_mark_node;
3179 arg = c_objc_common_truthvalue_conversion (location, arg);
3180 ret = invert_truthvalue (arg);
3181 goto return_build_unary_op;
3184 if (TREE_CODE (arg) == COMPLEX_CST)
3185 ret = TREE_REALPART (arg);
3186 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3187 ret = fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3190 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3191 eptype = TREE_TYPE (eptype);
3192 goto return_build_unary_op;
3195 if (TREE_CODE (arg) == COMPLEX_CST)
3196 ret = TREE_IMAGPART (arg);
3197 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3198 ret = fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3200 ret = omit_one_operand (TREE_TYPE (arg), integer_zero_node, arg);
3201 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3202 eptype = TREE_TYPE (eptype);
3203 goto return_build_unary_op;
3205 case PREINCREMENT_EXPR:
3206 case POSTINCREMENT_EXPR:
3207 case PREDECREMENT_EXPR:
3208 case POSTDECREMENT_EXPR:
3210 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3212 tree inner = build_unary_op (location, code,
3213 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3214 if (inner == error_mark_node)
3215 return error_mark_node;
3216 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3217 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3218 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3219 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3220 goto return_build_unary_op;
3223 /* Complain about anything that is not a true lvalue. */
3224 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3225 || code == POSTINCREMENT_EXPR)
3228 return error_mark_node;
3230 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3231 arg = c_fully_fold (arg, false, NULL);
3233 /* Increment or decrement the real part of the value,
3234 and don't change the imaginary part. */
3235 if (typecode == COMPLEX_TYPE)
3239 pedwarn (location, OPT_pedantic,
3240 "ISO C does not support %<++%> and %<--%> on complex types");
3242 arg = stabilize_reference (arg);
3243 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3244 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3245 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3246 if (real == error_mark_node || imag == error_mark_node)
3247 return error_mark_node;
3248 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3250 goto return_build_unary_op;
3253 /* Report invalid types. */
3255 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3256 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3258 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3259 error_at (location, "wrong type argument to increment");
3261 error_at (location, "wrong type argument to decrement");
3263 return error_mark_node;
3269 argtype = TREE_TYPE (arg);
3271 /* Compute the increment. */
3273 if (typecode == POINTER_TYPE)
3275 /* If pointer target is an undefined struct,
3276 we just cannot know how to do the arithmetic. */
3277 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3279 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3281 "increment of pointer to unknown structure");
3284 "decrement of pointer to unknown structure");
3286 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3287 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3289 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3290 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3291 "wrong type argument to increment");
3293 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3294 "wrong type argument to decrement");
3297 inc = c_size_in_bytes (TREE_TYPE (argtype));
3298 inc = fold_convert (sizetype, inc);
3300 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3302 /* For signed fract types, we invert ++ to -- or
3303 -- to ++, and change inc from 1 to -1, because
3304 it is not possible to represent 1 in signed fract constants.
3305 For unsigned fract types, the result always overflows and
3306 we get an undefined (original) or the maximum value. */
3307 if (code == PREINCREMENT_EXPR)
3308 code = PREDECREMENT_EXPR;
3309 else if (code == PREDECREMENT_EXPR)
3310 code = PREINCREMENT_EXPR;
3311 else if (code == POSTINCREMENT_EXPR)
3312 code = POSTDECREMENT_EXPR;
3313 else /* code == POSTDECREMENT_EXPR */
3314 code = POSTINCREMENT_EXPR;
3316 inc = integer_minus_one_node;
3317 inc = convert (argtype, inc);
3321 inc = integer_one_node;
3322 inc = convert (argtype, inc);
3325 /* Report a read-only lvalue. */
3326 if (TREE_READONLY (arg))
3328 readonly_error (arg,
3329 ((code == PREINCREMENT_EXPR
3330 || code == POSTINCREMENT_EXPR)
3331 ? lv_increment : lv_decrement));
3332 return error_mark_node;
3335 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3336 val = boolean_increment (code, arg);
3338 val = build2 (code, TREE_TYPE (arg), arg, inc);
3339 TREE_SIDE_EFFECTS (val) = 1;
3340 if (TREE_CODE (val) != code)
3341 TREE_NO_WARNING (val) = 1;
3343 goto return_build_unary_op;
3347 /* Note that this operation never does default_conversion. */
3349 /* The operand of unary '&' must be an lvalue (which excludes
3350 expressions of type void), or, in C99, the result of a [] or
3351 unary '*' operator. */
3352 if (VOID_TYPE_P (TREE_TYPE (arg))
3353 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3354 && (TREE_CODE (arg) != INDIRECT_REF
3356 pedwarn (location, 0, "taking address of expression of type %<void%>");
3358 /* Let &* cancel out to simplify resulting code. */
3359 if (TREE_CODE (arg) == INDIRECT_REF)
3361 /* Don't let this be an lvalue. */
3362 if (lvalue_p (TREE_OPERAND (arg, 0)))
3363 return non_lvalue (TREE_OPERAND (arg, 0));
3364 ret = TREE_OPERAND (arg, 0);
3365 goto return_build_unary_op;
3368 /* For &x[y], return x+y */
3369 if (TREE_CODE (arg) == ARRAY_REF)
3371 tree op0 = TREE_OPERAND (arg, 0);
3372 if (!c_mark_addressable (op0))
3373 return error_mark_node;
3374 return build_binary_op (location, PLUS_EXPR,
3375 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3376 ? array_to_pointer_conversion (op0)
3378 TREE_OPERAND (arg, 1), 1);
3381 /* Anything not already handled and not a true memory reference
3382 or a non-lvalue array is an error. */
3383 else if (typecode != FUNCTION_TYPE && !flag
3384 && !lvalue_or_else (arg, lv_addressof))
3385 return error_mark_node;
3387 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3389 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3391 tree inner = build_unary_op (location, code,
3392 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3393 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3394 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3395 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3396 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3397 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3398 goto return_build_unary_op;
3401 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3402 argtype = TREE_TYPE (arg);
3404 /* If the lvalue is const or volatile, merge that into the type
3405 to which the address will point. Note that you can't get a
3406 restricted pointer by taking the address of something, so we
3407 only have to deal with `const' and `volatile' here. */
3408 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3409 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3410 argtype = c_build_type_variant (argtype,
3411 TREE_READONLY (arg),
3412 TREE_THIS_VOLATILE (arg));
3414 if (!c_mark_addressable (arg))
3415 return error_mark_node;
3417 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3418 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3420 argtype = build_pointer_type (argtype);
3422 /* ??? Cope with user tricks that amount to offsetof. Delete this
3423 when we have proper support for integer constant expressions. */
3424 val = get_base_address (arg);
3425 if (val && TREE_CODE (val) == INDIRECT_REF
3426 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3428 tree op0 = fold_convert (sizetype, fold_offsetof (arg, val)), op1;
3430 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3431 ret = fold_build2 (POINTER_PLUS_EXPR, argtype, op1, op0);
3432 goto return_build_unary_op;
3435 val = build1 (ADDR_EXPR, argtype, arg);
3438 goto return_build_unary_op;
3445 argtype = TREE_TYPE (arg);
3446 if (TREE_CODE (arg) == INTEGER_CST)
3447 ret = (require_constant_value
3448 ? fold_build1_initializer (code, argtype, arg)
3449 : fold_build1 (code, argtype, arg));
3451 ret = build1 (code, argtype, arg);
3452 return_build_unary_op:
3453 gcc_assert (ret != error_mark_node);
3454 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3455 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3456 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3457 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3458 ret = note_integer_operands (ret);
3460 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3461 protected_set_expr_location (ret, location);
3465 /* Return nonzero if REF is an lvalue valid for this language.
3466 Lvalues can be assigned, unless their type has TYPE_READONLY.
3467 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3470 lvalue_p (const_tree ref)
3472 const enum tree_code code = TREE_CODE (ref);
3479 return lvalue_p (TREE_OPERAND (ref, 0));
3481 case C_MAYBE_CONST_EXPR:
3482 return lvalue_p (TREE_OPERAND (ref, 1));
3484 case COMPOUND_LITERAL_EXPR:
3494 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3495 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3498 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3505 /* Give an error for storing in something that is 'const'. */
3508 readonly_error (tree arg, enum lvalue_use use)
3510 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3512 /* Using this macro rather than (for example) arrays of messages
3513 ensures that all the format strings are checked at compile
3515 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3516 : (use == lv_increment ? (I) \
3517 : (use == lv_decrement ? (D) : (AS))))
3518 if (TREE_CODE (arg) == COMPONENT_REF)
3520 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3521 readonly_error (TREE_OPERAND (arg, 0), use);
3523 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3524 G_("increment of read-only member %qD"),
3525 G_("decrement of read-only member %qD"),
3526 G_("read-only member %qD used as %<asm%> output")),
3527 TREE_OPERAND (arg, 1));
3529 else if (TREE_CODE (arg) == VAR_DECL)
3530 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3531 G_("increment of read-only variable %qD"),
3532 G_("decrement of read-only variable %qD"),
3533 G_("read-only variable %qD used as %<asm%> output")),
3536 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3537 G_("increment of read-only location %qE"),
3538 G_("decrement of read-only location %qE"),
3539 G_("read-only location %qE used as %<asm%> output")),
3544 /* Return nonzero if REF is an lvalue valid for this language;
3545 otherwise, print an error message and return zero. USE says
3546 how the lvalue is being used and so selects the error message. */
3549 lvalue_or_else (const_tree ref, enum lvalue_use use)
3551 int win = lvalue_p (ref);
3559 /* Mark EXP saying that we need to be able to take the
3560 address of it; it should not be allocated in a register.
3561 Returns true if successful. */
3564 c_mark_addressable (tree exp)
3569 switch (TREE_CODE (x))
3572 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3575 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3579 /* ... fall through ... */
3585 x = TREE_OPERAND (x, 0);
3588 case COMPOUND_LITERAL_EXPR:
3590 TREE_ADDRESSABLE (x) = 1;
3597 if (C_DECL_REGISTER (x)
3598 && DECL_NONLOCAL (x))
3600 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3603 ("global register variable %qD used in nested function", x);
3606 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3608 else if (C_DECL_REGISTER (x))
3610 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3611 error ("address of global register variable %qD requested", x);
3613 error ("address of register variable %qD requested", x);
3619 TREE_ADDRESSABLE (x) = 1;
3626 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3627 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3628 if folded to an integer constant then the unselected half may
3629 contain arbitrary operations not normally permitted in constant
3633 build_conditional_expr (tree ifexp, bool ifexp_bcp, tree op1, tree op2)
3637 enum tree_code code1;
3638 enum tree_code code2;
3639 tree result_type = NULL;
3640 tree ep_result_type = NULL;
3641 tree orig_op1 = op1, orig_op2 = op2;
3642 bool int_const, op1_int_operands, op2_int_operands, int_operands;
3643 bool ifexp_int_operands;
3647 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
3648 if (op1_int_operands)
3649 op1 = remove_c_maybe_const_expr (op1);
3650 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
3651 if (op2_int_operands)
3652 op2 = remove_c_maybe_const_expr (op2);
3653 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
3654 if (ifexp_int_operands)
3655 ifexp = remove_c_maybe_const_expr (ifexp);
3657 /* Promote both alternatives. */
3659 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3660 op1 = default_conversion (op1);
3661 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3662 op2 = default_conversion (op2);
3664 if (TREE_CODE (ifexp) == ERROR_MARK
3665 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3666 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3667 return error_mark_node;
3669 type1 = TREE_TYPE (op1);
3670 code1 = TREE_CODE (type1);
3671 type2 = TREE_TYPE (op2);
3672 code2 = TREE_CODE (type2);
3674 /* C90 does not permit non-lvalue arrays in conditional expressions.
3675 In C99 they will be pointers by now. */
3676 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3678 error ("non-lvalue array in conditional expression");
3679 return error_mark_node;
3682 objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE);
3684 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
3685 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3686 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3687 || code1 == COMPLEX_TYPE)
3688 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3689 || code2 == COMPLEX_TYPE))
3691 ep_result_type = c_common_type (type1, type2);
3692 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
3694 op1 = TREE_OPERAND (op1, 0);
3695 type1 = TREE_TYPE (op1);
3696 gcc_assert (TREE_CODE (type1) == code1);
3698 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3700 op2 = TREE_OPERAND (op2, 0);
3701 type2 = TREE_TYPE (op2);
3702 gcc_assert (TREE_CODE (type2) == code2);
3706 /* Quickly detect the usual case where op1 and op2 have the same type
3708 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3711 result_type = type1;
3713 result_type = TYPE_MAIN_VARIANT (type1);
3715 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3716 || code1 == COMPLEX_TYPE)
3717 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3718 || code2 == COMPLEX_TYPE))
3720 result_type = c_common_type (type1, type2);
3722 /* If -Wsign-compare, warn here if type1 and type2 have
3723 different signedness. We'll promote the signed to unsigned
3724 and later code won't know it used to be different.
3725 Do this check on the original types, so that explicit casts
3726 will be considered, but default promotions won't. */
3727 if (!skip_evaluation)
3729 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3730 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3732 if (unsigned_op1 ^ unsigned_op2)
3736 /* Do not warn if the result type is signed, since the
3737 signed type will only be chosen if it can represent
3738 all the values of the unsigned type. */
3739 if (!TYPE_UNSIGNED (result_type))
3743 bool op1_maybe_const = true;
3744 bool op2_maybe_const = true;
3746 /* Do not warn if the signed quantity is an
3747 unsuffixed integer literal (or some static
3748 constant expression involving such literals) and
3749 it is non-negative. This warning requires the
3750 operands to be folded for best results, so do
3751 that folding in this case even without
3752 warn_sign_compare to avoid warning options
3753 possibly affecting code generation. */
3754 op1 = c_fully_fold (op1, require_constant_value,
3756 op2 = c_fully_fold (op2, require_constant_value,
3759 if (warn_sign_compare)
3762 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3764 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3767 warning (OPT_Wsign_compare, "signed and unsigned type in conditional expression");
3769 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
3771 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
3773 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
3775 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
3777 op2 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op2),
3779 C_MAYBE_CONST_EXPR_NON_CONST (op2) = !op2_maybe_const;
3785 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3787 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
3788 pedwarn (input_location, OPT_pedantic,
3789 "ISO C forbids conditional expr with only one void side");
3790 result_type = void_type_node;
3792 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3794 if (comp_target_types (type1, type2))
3795 result_type = common_pointer_type (type1, type2);
3796 else if (null_pointer_constant_p (orig_op1))
3797 result_type = qualify_type (type2, type1);
3798 else if (null_pointer_constant_p (orig_op2))
3799 result_type = qualify_type (type1, type2);
3800 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3802 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3803 pedwarn (input_location, OPT_pedantic,
3804 "ISO C forbids conditional expr between "
3805 "%<void *%> and function pointer");
3806 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3807 TREE_TYPE (type2)));
3809 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3811 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3812 pedwarn (input_location, OPT_pedantic,
3813 "ISO C forbids conditional expr between "
3814 "%<void *%> and function pointer");
3815 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3816 TREE_TYPE (type1)));
3821 pedwarn (input_location, 0,
3822 "pointer type mismatch in conditional expression");
3823 result_type = build_pointer_type (void_type_node);
3826 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3828 if (!null_pointer_constant_p (orig_op2))
3829 pedwarn (input_location, 0,
3830 "pointer/integer type mismatch in conditional expression");
3833 op2 = null_pointer_node;
3835 result_type = type1;
3837 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3839 if (!null_pointer_constant_p (orig_op1))
3840 pedwarn (input_location, 0,
3841 "pointer/integer type mismatch in conditional expression");
3844 op1 = null_pointer_node;
3846 result_type = type2;
3851 if (flag_cond_mismatch)
3852 result_type = void_type_node;
3855 error ("type mismatch in conditional expression");
3856 return error_mark_node;
3860 /* Merge const and volatile flags of the incoming types. */
3862 = build_type_variant (result_type,
3863 TREE_READONLY (op1) || TREE_READONLY (op2),
3864 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3866 if (result_type != TREE_TYPE (op1))
3867 op1 = convert_and_check (result_type, op1);
3868 if (result_type != TREE_TYPE (op2))
3869 op2 = convert_and_check (result_type, op2);
3871 if (ifexp_bcp && ifexp == truthvalue_true_node)
3873 op2_int_operands = true;
3874 op1 = c_fully_fold (op1, require_constant_value, NULL);
3876 if (ifexp_bcp && ifexp == truthvalue_false_node)
3878 op1_int_operands = true;
3879 op2 = c_fully_fold (op2, require_constant_value, NULL);
3881 int_const = int_operands = (ifexp_int_operands
3883 && op2_int_operands);
3886 int_const = ((ifexp == truthvalue_true_node
3887 && TREE_CODE (orig_op1) == INTEGER_CST
3888 && !TREE_OVERFLOW (orig_op1))
3889 || (ifexp == truthvalue_false_node
3890 && TREE_CODE (orig_op2) == INTEGER_CST
3891 && !TREE_OVERFLOW (orig_op2)));
3893 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
3894 ret = fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3897 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
3899 ret = note_integer_operands (ret);
3902 ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret);
3907 /* Return a compound expression that performs two expressions and
3908 returns the value of the second of them. */
3911 build_compound_expr (tree expr1, tree expr2)
3913 bool expr1_int_operands, expr2_int_operands;
3914 tree eptype = NULL_TREE;
3917 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
3918 if (expr1_int_operands)
3919 expr1 = remove_c_maybe_const_expr (expr1);
3920 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
3921 if (expr2_int_operands)
3922 expr2 = remove_c_maybe_const_expr (expr2);
3924 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
3925 expr1 = TREE_OPERAND (expr1, 0);
3926 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
3928 eptype = TREE_TYPE (expr2);
3929 expr2 = TREE_OPERAND (expr2, 0);
3932 if (!TREE_SIDE_EFFECTS (expr1))
3934 /* The left-hand operand of a comma expression is like an expression
3935 statement: with -Wunused, we should warn if it doesn't have
3936 any side-effects, unless it was explicitly cast to (void). */
3937 if (warn_unused_value)
3939 if (VOID_TYPE_P (TREE_TYPE (expr1))
3940 && CONVERT_EXPR_P (expr1))
3942 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3943 && TREE_CODE (expr1) == COMPOUND_EXPR
3944 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
3945 ; /* (void) a, (void) b, c */
3947 warning (OPT_Wunused_value,
3948 "left-hand operand of comma expression has no effect");
3952 /* With -Wunused, we should also warn if the left-hand operand does have
3953 side-effects, but computes a value which is not used. For example, in
3954 `foo() + bar(), baz()' the result of the `+' operator is not used,
3955 so we should issue a warning. */
3956 else if (warn_unused_value)
3957 warn_if_unused_value (expr1, input_location);
3959 if (expr2 == error_mark_node)
3960 return error_mark_node;
3962 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3965 && expr1_int_operands
3966 && expr2_int_operands)
3967 ret = note_integer_operands (ret);
3970 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3975 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3978 build_c_cast (tree type, tree expr)
3982 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3983 expr = TREE_OPERAND (expr, 0);
3987 if (type == error_mark_node || expr == error_mark_node)
3988 return error_mark_node;
3990 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3991 only in <protocol> qualifications. But when constructing cast expressions,
3992 the protocols do matter and must be kept around. */
3993 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3994 return build1 (NOP_EXPR, type, expr);
3996 type = TYPE_MAIN_VARIANT (type);
3998 if (TREE_CODE (type) == ARRAY_TYPE)
4000 error ("cast specifies array type");
4001 return error_mark_node;
4004 if (TREE_CODE (type) == FUNCTION_TYPE)
4006 error ("cast specifies function type");
4007 return error_mark_node;
4010 if (!VOID_TYPE_P (type))
4012 value = require_complete_type (value);
4013 if (value == error_mark_node)
4014 return error_mark_node;
4017 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4019 if (TREE_CODE (type) == RECORD_TYPE
4020 || TREE_CODE (type) == UNION_TYPE)
4021 pedwarn (input_location, OPT_pedantic,
4022 "ISO C forbids casting nonscalar to the same type");
4024 else if (TREE_CODE (type) == UNION_TYPE)
4028 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4029 if (TREE_TYPE (field) != error_mark_node
4030 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4031 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4038 pedwarn (input_location, OPT_pedantic,
4039 "ISO C forbids casts to union type");
4040 t = digest_init (type,
4041 build_constructor_single (type, field, value),
4043 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4046 error ("cast to union type from type not present in union");
4047 return error_mark_node;
4053 if (type == void_type_node)
4054 return build1 (CONVERT_EXPR, type, value);
4056 otype = TREE_TYPE (value);
4058 /* Optionally warn about potentially worrisome casts. */
4061 && TREE_CODE (type) == POINTER_TYPE
4062 && TREE_CODE (otype) == POINTER_TYPE)
4064 tree in_type = type;
4065 tree in_otype = otype;
4069 /* Check that the qualifiers on IN_TYPE are a superset of
4070 the qualifiers of IN_OTYPE. The outermost level of
4071 POINTER_TYPE nodes is uninteresting and we stop as soon
4072 as we hit a non-POINTER_TYPE node on either type. */
4075 in_otype = TREE_TYPE (in_otype);
4076 in_type = TREE_TYPE (in_type);
4078 /* GNU C allows cv-qualified function types. 'const'
4079 means the function is very pure, 'volatile' means it
4080 can't return. We need to warn when such qualifiers
4081 are added, not when they're taken away. */
4082 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4083 && TREE_CODE (in_type) == FUNCTION_TYPE)
4084 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
4086 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
4088 while (TREE_CODE (in_type) == POINTER_TYPE
4089 && TREE_CODE (in_otype) == POINTER_TYPE);
4092 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
4095 /* There are qualifiers present in IN_OTYPE that are not
4096 present in IN_TYPE. */
4097 warning (OPT_Wcast_qual, "cast discards qualifiers from pointer target type");
4100 /* Warn about possible alignment problems. */
4101 if (STRICT_ALIGNMENT
4102 && TREE_CODE (type) == POINTER_TYPE
4103 && TREE_CODE (otype) == POINTER_TYPE
4104 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4105 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4106 /* Don't warn about opaque types, where the actual alignment
4107 restriction is unknown. */
4108 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4109 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4110 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4111 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4112 warning (OPT_Wcast_align,
4113 "cast increases required alignment of target type");
4115 if (TREE_CODE (type) == INTEGER_TYPE
4116 && TREE_CODE (otype) == POINTER_TYPE
4117 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4118 /* Unlike conversion of integers to pointers, where the
4119 warning is disabled for converting constants because
4120 of cases such as SIG_*, warn about converting constant
4121 pointers to integers. In some cases it may cause unwanted
4122 sign extension, and a warning is appropriate. */
4123 warning (OPT_Wpointer_to_int_cast,
4124 "cast from pointer to integer of different size");
4126 if (TREE_CODE (value) == CALL_EXPR
4127 && TREE_CODE (type) != TREE_CODE (otype))
4128 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
4129 "to non-matching type %qT", otype, type);
4131 if (TREE_CODE (type) == POINTER_TYPE
4132 && TREE_CODE (otype) == INTEGER_TYPE
4133 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4134 /* Don't warn about converting any constant. */
4135 && !TREE_CONSTANT (value))
4136 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4137 "of different size");
4139 if (warn_strict_aliasing <= 2)
4140 strict_aliasing_warning (otype, type, expr);
4142 /* If pedantic, warn for conversions between function and object
4143 pointer types, except for converting a null pointer constant
4144 to function pointer type. */
4146 && TREE_CODE (type) == POINTER_TYPE
4147 && TREE_CODE (otype) == POINTER_TYPE
4148 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4149 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4150 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4151 "conversion of function pointer to object pointer type");
4154 && TREE_CODE (type) == POINTER_TYPE
4155 && TREE_CODE (otype) == POINTER_TYPE
4156 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4157 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4158 && !null_pointer_constant_p (value))
4159 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4160 "conversion of object pointer to function pointer type");
4163 value = convert (type, value);
4165 /* Ignore any integer overflow caused by the cast. */
4166 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4168 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4170 if (!TREE_OVERFLOW (value))
4172 /* Avoid clobbering a shared constant. */
4173 value = copy_node (value);
4174 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4177 else if (TREE_OVERFLOW (value))
4178 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4179 value = build_int_cst_wide (TREE_TYPE (value),
4180 TREE_INT_CST_LOW (value),
4181 TREE_INT_CST_HIGH (value));
4185 /* Don't let a cast be an lvalue. */
4187 value = non_lvalue (value);
4189 /* Don't allow the results of casting to floating-point or complex
4190 types be confused with actual constants, or casts involving
4191 integer and pointer types other than direct integer-to-integer
4192 and integer-to-pointer be confused with integer constant
4193 expressions and null pointer constants. */
4194 if (TREE_CODE (value) == REAL_CST
4195 || TREE_CODE (value) == COMPLEX_CST
4196 || (TREE_CODE (value) == INTEGER_CST
4197 && !((TREE_CODE (expr) == INTEGER_CST
4198 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4199 || TREE_CODE (expr) == REAL_CST
4200 || TREE_CODE (expr) == COMPLEX_CST)))
4201 value = build1 (NOP_EXPR, type, value);
4206 /* Interpret a cast of expression EXPR to type TYPE. */
4208 c_cast_expr (struct c_type_name *type_name, tree expr)
4211 tree type_expr = NULL_TREE;
4212 bool type_expr_const = true;
4214 int saved_wsp = warn_strict_prototypes;
4216 /* This avoids warnings about unprototyped casts on
4217 integers. E.g. "#define SIG_DFL (void(*)())0". */
4218 if (TREE_CODE (expr) == INTEGER_CST)
4219 warn_strict_prototypes = 0;
4220 type = groktypename (type_name, &type_expr, &type_expr_const);
4221 warn_strict_prototypes = saved_wsp;
4223 ret = build_c_cast (type, expr);
4226 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4227 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4232 /* Build an assignment expression of lvalue LHS from value RHS.
4233 MODIFYCODE is the code for a binary operator that we use
4234 to combine the old value of LHS with RHS to get the new value.
4235 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4237 LOCATION is the location of the MODIFYCODE operator. */
4240 build_modify_expr (location_t location,
4241 tree lhs, enum tree_code modifycode, tree rhs)
4245 tree rhs_semantic_type = NULL_TREE;
4246 tree lhstype = TREE_TYPE (lhs);
4247 tree olhstype = lhstype;
4250 /* Types that aren't fully specified cannot be used in assignments. */
4251 lhs = require_complete_type (lhs);
4253 /* Avoid duplicate error messages from operands that had errors. */
4254 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4255 return error_mark_node;
4257 if (!lvalue_or_else (lhs, lv_assign))
4258 return error_mark_node;
4260 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4262 rhs_semantic_type = TREE_TYPE (rhs);
4263 rhs = TREE_OPERAND (rhs, 0);
4268 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4270 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4272 if (inner == error_mark_node)
4273 return error_mark_node;
4274 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4275 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4276 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4277 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4278 protected_set_expr_location (result, location);
4282 /* If a binary op has been requested, combine the old LHS value with the RHS
4283 producing the value we should actually store into the LHS. */
4285 if (modifycode != NOP_EXPR)
4287 lhs = c_fully_fold (lhs, false, NULL);
4288 lhs = stabilize_reference (lhs);
4289 newrhs = build_binary_op (location,
4290 modifycode, lhs, rhs, 1);
4293 /* Give an error for storing in something that is 'const'. */
4295 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
4296 || ((TREE_CODE (lhstype) == RECORD_TYPE
4297 || TREE_CODE (lhstype) == UNION_TYPE)
4298 && C_TYPE_FIELDS_READONLY (lhstype)))
4300 readonly_error (lhs, lv_assign);
4301 return error_mark_node;
4304 /* If storing into a structure or union member,
4305 it has probably been given type `int'.
4306 Compute the type that would go with
4307 the actual amount of storage the member occupies. */
4309 if (TREE_CODE (lhs) == COMPONENT_REF
4310 && (TREE_CODE (lhstype) == INTEGER_TYPE
4311 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4312 || TREE_CODE (lhstype) == REAL_TYPE
4313 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4314 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4316 /* If storing in a field that is in actuality a short or narrower than one,
4317 we must store in the field in its actual type. */
4319 if (lhstype != TREE_TYPE (lhs))
4321 lhs = copy_node (lhs);
4322 TREE_TYPE (lhs) = lhstype;
4325 /* Convert new value to destination type. Fold it first, then
4326 restore any excess precision information, for the sake of
4327 conversion warnings. */
4329 npc = null_pointer_constant_p (newrhs);
4330 newrhs = c_fully_fold (newrhs, false, NULL);
4331 if (rhs_semantic_type)
4332 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4333 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign, npc,
4334 NULL_TREE, NULL_TREE, 0);
4335 if (TREE_CODE (newrhs) == ERROR_MARK)
4336 return error_mark_node;
4338 /* Emit ObjC write barrier, if necessary. */
4339 if (c_dialect_objc () && flag_objc_gc)
4341 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4344 protected_set_expr_location (result, location);
4349 /* Scan operands. */
4351 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4352 TREE_SIDE_EFFECTS (result) = 1;
4353 protected_set_expr_location (result, location);
4355 /* If we got the LHS in a different type for storing in,
4356 convert the result back to the nominal type of LHS
4357 so that the value we return always has the same type
4358 as the LHS argument. */
4360 if (olhstype == TREE_TYPE (result))
4363 result = convert_for_assignment (olhstype, result, ic_assign, false,
4364 NULL_TREE, NULL_TREE, 0);
4365 protected_set_expr_location (result, location);
4369 /* Convert value RHS to type TYPE as preparation for an assignment
4370 to an lvalue of type TYPE. NULL_POINTER_CONSTANT says whether RHS
4371 was a null pointer constant before any folding.
4372 The real work of conversion is done by `convert'.
4373 The purpose of this function is to generate error messages
4374 for assignments that are not allowed in C.
4375 ERRTYPE says whether it is argument passing, assignment,
4376 initialization or return.
4378 FUNCTION is a tree for the function being called.
4379 PARMNUM is the number of the argument, for printing in error messages. */
4382 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
4383 bool null_pointer_constant,
4384 tree fundecl, tree function, int parmnum)
4386 enum tree_code codel = TREE_CODE (type);
4387 tree orig_rhs = rhs;
4389 enum tree_code coder;
4390 tree rname = NULL_TREE;
4391 bool objc_ok = false;
4393 if (errtype == ic_argpass)
4396 /* Change pointer to function to the function itself for
4398 if (TREE_CODE (function) == ADDR_EXPR
4399 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4400 function = TREE_OPERAND (function, 0);
4402 /* Handle an ObjC selector specially for diagnostics. */
4403 selector = objc_message_selector ();
4405 if (selector && parmnum > 2)
4412 /* This macro is used to emit diagnostics to ensure that all format
4413 strings are complete sentences, visible to gettext and checked at
4415 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4420 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4421 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4422 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4423 "expected %qT but argument is of type %qT", \
4427 pedwarn (LOCATION, OPT, AS); \
4430 pedwarn (LOCATION, OPT, IN); \
4433 pedwarn (LOCATION, OPT, RE); \
4436 gcc_unreachable (); \
4440 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4441 rhs = TREE_OPERAND (rhs, 0);
4443 rhstype = TREE_TYPE (rhs);
4444 coder = TREE_CODE (rhstype);
4446 if (coder == ERROR_MARK)
4447 return error_mark_node;
4449 if (c_dialect_objc ())
4472 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4475 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4478 if (coder == VOID_TYPE)
4480 /* Except for passing an argument to an unprototyped function,
4481 this is a constraint violation. When passing an argument to
4482 an unprototyped function, it is compile-time undefined;
4483 making it a constraint in that case was rejected in
4485 error ("void value not ignored as it ought to be");
4486 return error_mark_node;
4488 rhs = require_complete_type (rhs);
4489 if (rhs == error_mark_node)
4490 return error_mark_node;
4491 /* A type converts to a reference to it.
4492 This code doesn't fully support references, it's just for the
4493 special case of va_start and va_copy. */
4494 if (codel == REFERENCE_TYPE
4495 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4497 if (!lvalue_p (rhs))
4499 error ("cannot pass rvalue to reference parameter");
4500 return error_mark_node;
4502 if (!c_mark_addressable (rhs))
4503 return error_mark_node;
4504 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4506 /* We already know that these two types are compatible, but they
4507 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4508 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4509 likely to be va_list, a typedef to __builtin_va_list, which
4510 is different enough that it will cause problems later. */
4511 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4512 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4514 rhs = build1 (NOP_EXPR, type, rhs);
4517 /* Some types can interconvert without explicit casts. */
4518 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4519 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4520 return convert (type, rhs);
4521 /* Arithmetic types all interconvert, and enum is treated like int. */
4522 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4523 || codel == FIXED_POINT_TYPE
4524 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4525 || codel == BOOLEAN_TYPE)
4526 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4527 || coder == FIXED_POINT_TYPE
4528 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4529 || coder == BOOLEAN_TYPE))
4532 bool save = in_late_binary_op;
4533 if (codel == BOOLEAN_TYPE)
4534 in_late_binary_op = true;
4535 ret = convert_and_check (type, orig_rhs);
4536 if (codel == BOOLEAN_TYPE)
4537 in_late_binary_op = save;
4541 /* Aggregates in different TUs might need conversion. */
4542 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4544 && comptypes (type, rhstype))
4545 return convert_and_check (type, rhs);
4547 /* Conversion to a transparent union from its member types.
4548 This applies only to function arguments. */
4549 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4550 && errtype == ic_argpass)
4552 tree memb, marginal_memb = NULL_TREE;
4554 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4556 tree memb_type = TREE_TYPE (memb);
4558 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4559 TYPE_MAIN_VARIANT (rhstype)))
4562 if (TREE_CODE (memb_type) != POINTER_TYPE)
4565 if (coder == POINTER_TYPE)
4567 tree ttl = TREE_TYPE (memb_type);
4568 tree ttr = TREE_TYPE (rhstype);
4570 /* Any non-function converts to a [const][volatile] void *
4571 and vice versa; otherwise, targets must be the same.
4572 Meanwhile, the lhs target must have all the qualifiers of
4574 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4575 || comp_target_types (memb_type, rhstype))
4577 /* If this type won't generate any warnings, use it. */
4578 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4579 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4580 && TREE_CODE (ttl) == FUNCTION_TYPE)
4581 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4582 == TYPE_QUALS (ttr))
4583 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4584 == TYPE_QUALS (ttl))))
4587 /* Keep looking for a better type, but remember this one. */
4589 marginal_memb = memb;
4593 /* Can convert integer zero to any pointer type. */
4594 if (null_pointer_constant)
4596 rhs = null_pointer_node;
4601 if (memb || marginal_memb)
4605 /* We have only a marginally acceptable member type;
4606 it needs a warning. */
4607 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4608 tree ttr = TREE_TYPE (rhstype);
4610 /* Const and volatile mean something different for function
4611 types, so the usual warnings are not appropriate. */
4612 if (TREE_CODE (ttr) == FUNCTION_TYPE
4613 && TREE_CODE (ttl) == FUNCTION_TYPE)
4615 /* Because const and volatile on functions are
4616 restrictions that say the function will not do
4617 certain things, it is okay to use a const or volatile
4618 function where an ordinary one is wanted, but not
4620 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4621 WARN_FOR_ASSIGNMENT (input_location, 0,
4622 G_("passing argument %d of %qE "
4623 "makes qualified function "
4624 "pointer from unqualified"),
4625 G_("assignment makes qualified "
4626 "function pointer from "
4628 G_("initialization makes qualified "
4629 "function pointer from "
4631 G_("return makes qualified function "
4632 "pointer from unqualified"));
4634 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4635 WARN_FOR_ASSIGNMENT (input_location, 0,
4636 G_("passing argument %d of %qE discards "
4637 "qualifiers from pointer target type"),
4638 G_("assignment discards qualifiers "
4639 "from pointer target type"),
4640 G_("initialization discards qualifiers "
4641 "from pointer target type"),
4642 G_("return discards qualifiers from "
4643 "pointer target type"));
4645 memb = marginal_memb;
4648 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
4649 pedwarn (input_location, OPT_pedantic,
4650 "ISO C prohibits argument conversion to union type");
4652 rhs = fold_convert (TREE_TYPE (memb), rhs);
4653 return build_constructor_single (type, memb, rhs);
4657 /* Conversions among pointers */
4658 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4659 && (coder == codel))
4661 tree ttl = TREE_TYPE (type);
4662 tree ttr = TREE_TYPE (rhstype);
4665 bool is_opaque_pointer;
4666 int target_cmp = 0; /* Cache comp_target_types () result. */
4668 if (TREE_CODE (mvl) != ARRAY_TYPE)
4669 mvl = TYPE_MAIN_VARIANT (mvl);
4670 if (TREE_CODE (mvr) != ARRAY_TYPE)
4671 mvr = TYPE_MAIN_VARIANT (mvr);
4672 /* Opaque pointers are treated like void pointers. */
4673 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
4675 /* C++ does not allow the implicit conversion void* -> T*. However,
4676 for the purpose of reducing the number of false positives, we
4677 tolerate the special case of
4681 where NULL is typically defined in C to be '(void *) 0'. */
4682 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4683 warning (OPT_Wc___compat, "request for implicit conversion from "
4684 "%qT to %qT not permitted in C++", rhstype, type);
4686 /* Check if the right-hand side has a format attribute but the
4687 left-hand side doesn't. */
4688 if (warn_missing_format_attribute
4689 && check_missing_format_attribute (type, rhstype))
4694 warning (OPT_Wmissing_format_attribute,
4695 "argument %d of %qE might be "
4696 "a candidate for a format attribute",
4700 warning (OPT_Wmissing_format_attribute,
4701 "assignment left-hand side might be "
4702 "a candidate for a format attribute");
4705 warning (OPT_Wmissing_format_attribute,
4706 "initialization left-hand side might be "
4707 "a candidate for a format attribute");
4710 warning (OPT_Wmissing_format_attribute,
4711 "return type might be "
4712 "a candidate for a format attribute");
4719 /* Any non-function converts to a [const][volatile] void *
4720 and vice versa; otherwise, targets must be the same.
4721 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4722 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4723 || (target_cmp = comp_target_types (type, rhstype))
4724 || is_opaque_pointer
4725 || (c_common_unsigned_type (mvl)
4726 == c_common_unsigned_type (mvr)))
4729 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4732 && !null_pointer_constant
4733 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4734 WARN_FOR_ASSIGNMENT (input_location, OPT_pedantic,
4735 G_("ISO C forbids passing argument %d of "
4736 "%qE between function pointer "
4738 G_("ISO C forbids assignment between "
4739 "function pointer and %<void *%>"),
4740 G_("ISO C forbids initialization between "
4741 "function pointer and %<void *%>"),
4742 G_("ISO C forbids return between function "
4743 "pointer and %<void *%>"));
4744 /* Const and volatile mean something different for function types,
4745 so the usual warnings are not appropriate. */
4746 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4747 && TREE_CODE (ttl) != FUNCTION_TYPE)
4749 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4751 /* Types differing only by the presence of the 'volatile'
4752 qualifier are acceptable if the 'volatile' has been added
4753 in by the Objective-C EH machinery. */
4754 if (!objc_type_quals_match (ttl, ttr))
4755 WARN_FOR_ASSIGNMENT (input_location, 0,
4756 G_("passing argument %d of %qE discards "
4757 "qualifiers from pointer target type"),
4758 G_("assignment discards qualifiers "
4759 "from pointer target type"),
4760 G_("initialization discards qualifiers "
4761 "from pointer target type"),
4762 G_("return discards qualifiers from "
4763 "pointer target type"));
4765 /* If this is not a case of ignoring a mismatch in signedness,
4767 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4770 /* If there is a mismatch, do warn. */
4771 else if (warn_pointer_sign)
4772 WARN_FOR_ASSIGNMENT (input_location, OPT_Wpointer_sign,
4773 G_("pointer targets in passing argument "
4774 "%d of %qE differ in signedness"),
4775 G_("pointer targets in assignment "
4776 "differ in signedness"),
4777 G_("pointer targets in initialization "
4778 "differ in signedness"),
4779 G_("pointer targets in return differ "
4782 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4783 && TREE_CODE (ttr) == FUNCTION_TYPE)
4785 /* Because const and volatile on functions are restrictions
4786 that say the function will not do certain things,
4787 it is okay to use a const or volatile function
4788 where an ordinary one is wanted, but not vice-versa. */
4789 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4790 WARN_FOR_ASSIGNMENT (input_location, 0,
4791 G_("passing argument %d of %qE makes "
4792 "qualified function pointer "
4793 "from unqualified"),
4794 G_("assignment makes qualified function "
4795 "pointer from unqualified"),
4796 G_("initialization makes qualified "
4797 "function pointer from unqualified"),
4798 G_("return makes qualified function "
4799 "pointer from unqualified"));
4803 /* Avoid warning about the volatile ObjC EH puts on decls. */
4805 WARN_FOR_ASSIGNMENT (input_location, 0,
4806 G_("passing argument %d of %qE from "
4807 "incompatible pointer type"),
4808 G_("assignment from incompatible pointer type"),
4809 G_("initialization from incompatible "
4811 G_("return from incompatible pointer type"));
4813 return convert (type, rhs);
4815 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4817 /* ??? This should not be an error when inlining calls to
4818 unprototyped functions. */
4819 error ("invalid use of non-lvalue array");
4820 return error_mark_node;
4822 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4824 /* An explicit constant 0 can convert to a pointer,
4825 or one that results from arithmetic, even including
4826 a cast to integer type. */
4827 if (!null_pointer_constant)
4828 WARN_FOR_ASSIGNMENT (input_location, 0,
4829 G_("passing argument %d of %qE makes "
4830 "pointer from integer without a cast"),
4831 G_("assignment makes pointer from integer "
4833 G_("initialization makes pointer from "
4834 "integer without a cast"),
4835 G_("return makes pointer from integer "
4838 return convert (type, rhs);
4840 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4842 WARN_FOR_ASSIGNMENT (input_location, 0,
4843 G_("passing argument %d of %qE makes integer "
4844 "from pointer without a cast"),
4845 G_("assignment makes integer from pointer "
4847 G_("initialization makes integer from pointer "
4849 G_("return makes integer from pointer "
4851 return convert (type, rhs);
4853 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4856 bool save = in_late_binary_op;
4857 in_late_binary_op = true;
4858 ret = convert (type, rhs);
4859 in_late_binary_op = save;
4866 error ("incompatible type for argument %d of %qE", parmnum, rname);
4867 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
4868 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
4869 "expected %qT but argument is of type %qT", type, rhstype);
4872 error ("incompatible types when assigning to type %qT from type %qT",
4876 error ("incompatible types when initializing type %qT using type %qT",
4880 error ("incompatible types when returning type %qT but %qT was expected",
4887 return error_mark_node;
4890 /* If VALUE is a compound expr all of whose expressions are constant, then
4891 return its value. Otherwise, return error_mark_node.
4893 This is for handling COMPOUND_EXPRs as initializer elements
4894 which is allowed with a warning when -pedantic is specified. */
4897 valid_compound_expr_initializer (tree value, tree endtype)
4899 if (TREE_CODE (value) == COMPOUND_EXPR)
4901 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4903 return error_mark_node;
4904 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4907 else if (!initializer_constant_valid_p (value, endtype))
4908 return error_mark_node;
4913 /* Perform appropriate conversions on the initial value of a variable,
4914 store it in the declaration DECL,
4915 and print any error messages that are appropriate.
4916 If the init is invalid, store an ERROR_MARK. */
4919 store_init_value (tree decl, tree init)
4924 /* If variable's type was invalidly declared, just ignore it. */
4926 type = TREE_TYPE (decl);
4927 if (TREE_CODE (type) == ERROR_MARK)
4930 /* Digest the specified initializer into an expression. */
4933 npc = null_pointer_constant_p (init);
4934 value = digest_init (type, init, npc, true, TREE_STATIC (decl));
4936 /* Store the expression if valid; else report error. */
4938 if (!in_system_header
4939 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4940 warning (OPT_Wtraditional, "traditional C rejects automatic "
4941 "aggregate initialization");
4943 DECL_INITIAL (decl) = value;
4945 /* ANSI wants warnings about out-of-range constant initializers. */
4946 STRIP_TYPE_NOPS (value);
4947 if (TREE_STATIC (decl))
4948 constant_expression_warning (value);
4950 /* Check if we need to set array size from compound literal size. */
4951 if (TREE_CODE (type) == ARRAY_TYPE
4952 && TYPE_DOMAIN (type) == 0
4953 && value != error_mark_node)
4955 tree inside_init = init;
4957 STRIP_TYPE_NOPS (inside_init);
4958 inside_init = fold (inside_init);
4960 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4962 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4964 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
4966 /* For int foo[] = (int [3]){1}; we need to set array size
4967 now since later on array initializer will be just the
4968 brace enclosed list of the compound literal. */
4969 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4970 TREE_TYPE (decl) = type;
4971 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
4973 layout_decl (cldecl, 0);
4979 /* Methods for storing and printing names for error messages. */
4981 /* Implement a spelling stack that allows components of a name to be pushed
4982 and popped. Each element on the stack is this structure. */
4989 unsigned HOST_WIDE_INT i;
4994 #define SPELLING_STRING 1
4995 #define SPELLING_MEMBER 2
4996 #define SPELLING_BOUNDS 3
4998 static struct spelling *spelling; /* Next stack element (unused). */
4999 static struct spelling *spelling_base; /* Spelling stack base. */
5000 static int spelling_size; /* Size of the spelling stack. */
5002 /* Macros to save and restore the spelling stack around push_... functions.
5003 Alternative to SAVE_SPELLING_STACK. */
5005 #define SPELLING_DEPTH() (spelling - spelling_base)
5006 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5008 /* Push an element on the spelling stack with type KIND and assign VALUE
5011 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5013 int depth = SPELLING_DEPTH (); \
5015 if (depth >= spelling_size) \
5017 spelling_size += 10; \
5018 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5020 RESTORE_SPELLING_DEPTH (depth); \
5023 spelling->kind = (KIND); \
5024 spelling->MEMBER = (VALUE); \
5028 /* Push STRING on the stack. Printed literally. */
5031 push_string (const char *string)
5033 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5036 /* Push a member name on the stack. Printed as '.' STRING. */
5039 push_member_name (tree decl)
5041 const char *const string
5042 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
5043 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5046 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5049 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5051 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5054 /* Compute the maximum size in bytes of the printed spelling. */
5057 spelling_length (void)
5062 for (p = spelling_base; p < spelling; p++)
5064 if (p->kind == SPELLING_BOUNDS)
5067 size += strlen (p->u.s) + 1;
5073 /* Print the spelling to BUFFER and return it. */
5076 print_spelling (char *buffer)
5081 for (p = spelling_base; p < spelling; p++)
5082 if (p->kind == SPELLING_BOUNDS)
5084 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5090 if (p->kind == SPELLING_MEMBER)
5092 for (s = p->u.s; (*d = *s++); d++)
5099 /* Issue an error message for a bad initializer component.
5100 MSGID identifies the message.
5101 The component name is taken from the spelling stack. */
5104 error_init (const char *msgid)
5108 error ("%s", _(msgid));
5109 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5111 error ("(near initialization for %qs)", ofwhat);
5114 /* Issue a pedantic warning for a bad initializer component. OPT is
5115 the option OPT_* (from options.h) controlling this warning or 0 if
5116 it is unconditionally given. MSGID identifies the message. The
5117 component name is taken from the spelling stack. */
5120 pedwarn_init (location_t location, int opt, const char *msgid)
5124 pedwarn (location, opt, "%s", _(msgid));
5125 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5127 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5130 /* Issue a warning for a bad initializer component.
5132 OPT is the OPT_W* value corresponding to the warning option that
5133 controls this warning. MSGID identifies the message. The
5134 component name is taken from the spelling stack. */
5137 warning_init (int opt, const char *msgid)
5141 warning (opt, "%s", _(msgid));
5142 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5144 warning (opt, "(near initialization for %qs)", ofwhat);
5147 /* If TYPE is an array type and EXPR is a parenthesized string
5148 constant, warn if pedantic that EXPR is being used to initialize an
5149 object of type TYPE. */
5152 maybe_warn_string_init (tree type, struct c_expr expr)
5155 && TREE_CODE (type) == ARRAY_TYPE
5156 && TREE_CODE (expr.value) == STRING_CST
5157 && expr.original_code != STRING_CST)
5158 pedwarn_init (input_location, OPT_pedantic,
5159 "array initialized from parenthesized string constant");
5162 /* Digest the parser output INIT as an initializer for type TYPE.
5163 Return a C expression of type TYPE to represent the initial value.
5165 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5167 If INIT is a string constant, STRICT_STRING is true if it is
5168 unparenthesized or we should not warn here for it being parenthesized.
5169 For other types of INIT, STRICT_STRING is not used.
5171 REQUIRE_CONSTANT requests an error if non-constant initializers or
5172 elements are seen. */
5175 digest_init (tree type, tree init, bool null_pointer_constant,
5176 bool strict_string, int require_constant)
5178 enum tree_code code = TREE_CODE (type);
5179 tree inside_init = init;
5180 tree semantic_type = NULL_TREE;
5181 bool maybe_const = true;
5183 if (type == error_mark_node
5185 || init == error_mark_node
5186 || TREE_TYPE (init) == error_mark_node)
5187 return error_mark_node;
5189 STRIP_TYPE_NOPS (inside_init);
5191 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5193 semantic_type = TREE_TYPE (inside_init);
5194 inside_init = TREE_OPERAND (inside_init, 0);
5196 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5197 inside_init = decl_constant_value_for_optimization (inside_init);
5199 /* Initialization of an array of chars from a string constant
5200 optionally enclosed in braces. */
5202 if (code == ARRAY_TYPE && inside_init
5203 && TREE_CODE (inside_init) == STRING_CST)
5205 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5206 /* Note that an array could be both an array of character type
5207 and an array of wchar_t if wchar_t is signed char or unsigned
5209 bool char_array = (typ1 == char_type_node
5210 || typ1 == signed_char_type_node
5211 || typ1 == unsigned_char_type_node);
5212 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5213 bool char16_array = !!comptypes (typ1, char16_type_node);
5214 bool char32_array = !!comptypes (typ1, char32_type_node);
5216 if (char_array || wchar_array || char16_array || char32_array)
5219 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5220 expr.value = inside_init;
5221 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5222 expr.original_type = NULL;
5223 maybe_warn_string_init (type, expr);
5225 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5226 TYPE_MAIN_VARIANT (type)))
5231 if (typ2 != char_type_node)
5233 error_init ("char-array initialized from wide string");
5234 return error_mark_node;
5239 if (typ2 == char_type_node)
5241 error_init ("wide character array initialized from non-wide "
5243 return error_mark_node;
5245 else if (!comptypes(typ1, typ2))
5247 error_init ("wide character array initialized from "
5248 "incompatible wide string");
5249 return error_mark_node;
5253 TREE_TYPE (inside_init) = type;
5254 if (TYPE_DOMAIN (type) != 0
5255 && TYPE_SIZE (type) != 0
5256 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
5257 /* Subtract the size of a single (possibly wide) character
5258 because it's ok to ignore the terminating null char
5259 that is counted in the length of the constant. */
5260 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5261 TREE_STRING_LENGTH (inside_init)
5262 - (TYPE_PRECISION (typ1)
5264 pedwarn_init (input_location, 0,
5265 "initializer-string for array of chars is too long");
5269 else if (INTEGRAL_TYPE_P (typ1))
5271 error_init ("array of inappropriate type initialized "
5272 "from string constant");
5273 return error_mark_node;
5277 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5278 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5279 below and handle as a constructor. */
5280 if (code == VECTOR_TYPE
5281 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5282 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5283 && TREE_CONSTANT (inside_init))
5285 if (TREE_CODE (inside_init) == VECTOR_CST
5286 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5287 TYPE_MAIN_VARIANT (type)))
5290 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5292 unsigned HOST_WIDE_INT ix;
5294 bool constant_p = true;
5296 /* Iterate through elements and check if all constructor
5297 elements are *_CSTs. */
5298 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5299 if (!CONSTANT_CLASS_P (value))
5306 return build_vector_from_ctor (type,
5307 CONSTRUCTOR_ELTS (inside_init));
5311 if (warn_sequence_point)
5312 verify_sequence_points (inside_init);
5314 /* Any type can be initialized
5315 from an expression of the same type, optionally with braces. */
5317 if (inside_init && TREE_TYPE (inside_init) != 0
5318 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5319 TYPE_MAIN_VARIANT (type))
5320 || (code == ARRAY_TYPE
5321 && comptypes (TREE_TYPE (inside_init), type))
5322 || (code == VECTOR_TYPE
5323 && comptypes (TREE_TYPE (inside_init), type))
5324 || (code == POINTER_TYPE
5325 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5326 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5327 TREE_TYPE (type)))))
5329 if (code == POINTER_TYPE)
5331 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5333 if (TREE_CODE (inside_init) == STRING_CST
5334 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5335 inside_init = array_to_pointer_conversion (inside_init);
5338 error_init ("invalid use of non-lvalue array");
5339 return error_mark_node;
5344 if (code == VECTOR_TYPE)
5345 /* Although the types are compatible, we may require a
5347 inside_init = convert (type, inside_init);
5349 if (require_constant
5350 && (code == VECTOR_TYPE || !flag_isoc99)
5351 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5353 /* As an extension, allow initializing objects with static storage
5354 duration with compound literals (which are then treated just as
5355 the brace enclosed list they contain). Also allow this for
5356 vectors, as we can only assign them with compound literals. */
5357 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5358 inside_init = DECL_INITIAL (decl);
5361 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5362 && TREE_CODE (inside_init) != CONSTRUCTOR)
5364 error_init ("array initialized from non-constant array expression");
5365 return error_mark_node;
5368 /* Compound expressions can only occur here if -pedantic or
5369 -pedantic-errors is specified. In the later case, we always want
5370 an error. In the former case, we simply want a warning. */
5371 if (require_constant && pedantic
5372 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5375 = valid_compound_expr_initializer (inside_init,
5376 TREE_TYPE (inside_init));
5377 if (inside_init == error_mark_node)
5378 error_init ("initializer element is not constant");
5380 pedwarn_init (input_location, OPT_pedantic,
5381 "initializer element is not constant");
5382 if (flag_pedantic_errors)
5383 inside_init = error_mark_node;
5385 else if (require_constant
5386 && !initializer_constant_valid_p (inside_init,
5387 TREE_TYPE (inside_init)))
5389 error_init ("initializer element is not constant");
5390 inside_init = error_mark_node;
5392 else if (require_constant && !maybe_const)
5393 pedwarn_init (input_location, 0,
5394 "initializer element is not a constant expression");
5396 /* Added to enable additional -Wmissing-format-attribute warnings. */
5397 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
5398 inside_init = convert_for_assignment (type, inside_init, ic_init,
5399 null_pointer_constant,
5400 NULL_TREE, NULL_TREE, 0);
5404 /* Handle scalar types, including conversions. */
5406 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
5407 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
5408 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
5410 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
5411 && (TREE_CODE (init) == STRING_CST
5412 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
5413 inside_init = init = array_to_pointer_conversion (init);
5415 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
5418 = convert_for_assignment (type, inside_init, ic_init,
5419 null_pointer_constant,
5420 NULL_TREE, NULL_TREE, 0);
5422 /* Check to see if we have already given an error message. */
5423 if (inside_init == error_mark_node)
5425 else if (require_constant && !TREE_CONSTANT (inside_init))
5427 error_init ("initializer element is not constant");
5428 inside_init = error_mark_node;
5430 else if (require_constant
5431 && !initializer_constant_valid_p (inside_init,
5432 TREE_TYPE (inside_init)))
5434 error_init ("initializer element is not computable at load time");
5435 inside_init = error_mark_node;
5437 else if (require_constant && !maybe_const)
5438 pedwarn_init (input_location, 0,
5439 "initializer element is not a constant expression");
5444 /* Come here only for records and arrays. */
5446 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5448 error_init ("variable-sized object may not be initialized");
5449 return error_mark_node;
5452 error_init ("invalid initializer");
5453 return error_mark_node;
5456 /* Handle initializers that use braces. */
5458 /* Type of object we are accumulating a constructor for.
5459 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5460 static tree constructor_type;
5462 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5464 static tree constructor_fields;
5466 /* For an ARRAY_TYPE, this is the specified index
5467 at which to store the next element we get. */
5468 static tree constructor_index;
5470 /* For an ARRAY_TYPE, this is the maximum index. */
5471 static tree constructor_max_index;
5473 /* For a RECORD_TYPE, this is the first field not yet written out. */
5474 static tree constructor_unfilled_fields;
5476 /* For an ARRAY_TYPE, this is the index of the first element
5477 not yet written out. */
5478 static tree constructor_unfilled_index;
5480 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5481 This is so we can generate gaps between fields, when appropriate. */
5482 static tree constructor_bit_index;
5484 /* If we are saving up the elements rather than allocating them,
5485 this is the list of elements so far (in reverse order,
5486 most recent first). */
5487 static VEC(constructor_elt,gc) *constructor_elements;
5489 /* 1 if constructor should be incrementally stored into a constructor chain,
5490 0 if all the elements should be kept in AVL tree. */
5491 static int constructor_incremental;
5493 /* 1 if so far this constructor's elements are all compile-time constants. */
5494 static int constructor_constant;
5496 /* 1 if so far this constructor's elements are all valid address constants. */
5497 static int constructor_simple;
5499 /* 1 if this constructor has an element that cannot be part of a
5500 constant expression. */
5501 static int constructor_nonconst;
5503 /* 1 if this constructor is erroneous so far. */
5504 static int constructor_erroneous;
5506 /* Structure for managing pending initializer elements, organized as an
5511 struct init_node *left, *right;
5512 struct init_node *parent;
5518 /* Tree of pending elements at this constructor level.
5519 These are elements encountered out of order
5520 which belong at places we haven't reached yet in actually
5522 Will never hold tree nodes across GC runs. */
5523 static struct init_node *constructor_pending_elts;
5525 /* The SPELLING_DEPTH of this constructor. */
5526 static int constructor_depth;
5528 /* DECL node for which an initializer is being read.
5529 0 means we are reading a constructor expression
5530 such as (struct foo) {...}. */
5531 static tree constructor_decl;
5533 /* Nonzero if this is an initializer for a top-level decl. */
5534 static int constructor_top_level;
5536 /* Nonzero if there were any member designators in this initializer. */
5537 static int constructor_designated;
5539 /* Nesting depth of designator list. */
5540 static int designator_depth;
5542 /* Nonzero if there were diagnosed errors in this designator list. */
5543 static int designator_erroneous;
5546 /* This stack has a level for each implicit or explicit level of
5547 structuring in the initializer, including the outermost one. It
5548 saves the values of most of the variables above. */
5550 struct constructor_range_stack;
5552 struct constructor_stack
5554 struct constructor_stack *next;
5559 tree unfilled_index;
5560 tree unfilled_fields;
5562 VEC(constructor_elt,gc) *elements;
5563 struct init_node *pending_elts;
5566 /* If value nonzero, this value should replace the entire
5567 constructor at this level. */
5568 struct c_expr replacement_value;
5569 struct constructor_range_stack *range_stack;
5580 static struct constructor_stack *constructor_stack;
5582 /* This stack represents designators from some range designator up to
5583 the last designator in the list. */
5585 struct constructor_range_stack
5587 struct constructor_range_stack *next, *prev;
5588 struct constructor_stack *stack;
5595 static struct constructor_range_stack *constructor_range_stack;
5597 /* This stack records separate initializers that are nested.
5598 Nested initializers can't happen in ANSI C, but GNU C allows them
5599 in cases like { ... (struct foo) { ... } ... }. */
5601 struct initializer_stack
5603 struct initializer_stack *next;
5605 struct constructor_stack *constructor_stack;
5606 struct constructor_range_stack *constructor_range_stack;
5607 VEC(constructor_elt,gc) *elements;
5608 struct spelling *spelling;
5609 struct spelling *spelling_base;
5612 char require_constant_value;
5613 char require_constant_elements;
5616 static struct initializer_stack *initializer_stack;
5618 /* Prepare to parse and output the initializer for variable DECL. */
5621 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5624 struct initializer_stack *p = XNEW (struct initializer_stack);
5626 p->decl = constructor_decl;
5627 p->require_constant_value = require_constant_value;
5628 p->require_constant_elements = require_constant_elements;
5629 p->constructor_stack = constructor_stack;
5630 p->constructor_range_stack = constructor_range_stack;
5631 p->elements = constructor_elements;
5632 p->spelling = spelling;
5633 p->spelling_base = spelling_base;
5634 p->spelling_size = spelling_size;
5635 p->top_level = constructor_top_level;
5636 p->next = initializer_stack;
5637 initializer_stack = p;
5639 constructor_decl = decl;
5640 constructor_designated = 0;
5641 constructor_top_level = top_level;
5643 if (decl != 0 && decl != error_mark_node)
5645 require_constant_value = TREE_STATIC (decl);
5646 require_constant_elements
5647 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5648 /* For a scalar, you can always use any value to initialize,
5649 even within braces. */
5650 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5651 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5652 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5653 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5654 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5658 require_constant_value = 0;
5659 require_constant_elements = 0;
5660 locus = "(anonymous)";
5663 constructor_stack = 0;
5664 constructor_range_stack = 0;
5666 missing_braces_mentioned = 0;
5670 RESTORE_SPELLING_DEPTH (0);
5673 push_string (locus);
5679 struct initializer_stack *p = initializer_stack;
5681 /* Free the whole constructor stack of this initializer. */
5682 while (constructor_stack)
5684 struct constructor_stack *q = constructor_stack;
5685 constructor_stack = q->next;
5689 gcc_assert (!constructor_range_stack);
5691 /* Pop back to the data of the outer initializer (if any). */
5692 free (spelling_base);
5694 constructor_decl = p->decl;
5695 require_constant_value = p->require_constant_value;
5696 require_constant_elements = p->require_constant_elements;
5697 constructor_stack = p->constructor_stack;
5698 constructor_range_stack = p->constructor_range_stack;
5699 constructor_elements = p->elements;
5700 spelling = p->spelling;
5701 spelling_base = p->spelling_base;
5702 spelling_size = p->spelling_size;
5703 constructor_top_level = p->top_level;
5704 initializer_stack = p->next;
5708 /* Call here when we see the initializer is surrounded by braces.
5709 This is instead of a call to push_init_level;
5710 it is matched by a call to pop_init_level.
5712 TYPE is the type to initialize, for a constructor expression.
5713 For an initializer for a decl, TYPE is zero. */
5716 really_start_incremental_init (tree type)
5718 struct constructor_stack *p = XNEW (struct constructor_stack);
5721 type = TREE_TYPE (constructor_decl);
5723 if (TREE_CODE (type) == VECTOR_TYPE
5724 && TYPE_VECTOR_OPAQUE (type))
5725 error ("opaque vector types cannot be initialized");
5727 p->type = constructor_type;
5728 p->fields = constructor_fields;
5729 p->index = constructor_index;
5730 p->max_index = constructor_max_index;
5731 p->unfilled_index = constructor_unfilled_index;
5732 p->unfilled_fields = constructor_unfilled_fields;
5733 p->bit_index = constructor_bit_index;
5734 p->elements = constructor_elements;
5735 p->constant = constructor_constant;
5736 p->simple = constructor_simple;
5737 p->nonconst = constructor_nonconst;
5738 p->erroneous = constructor_erroneous;
5739 p->pending_elts = constructor_pending_elts;
5740 p->depth = constructor_depth;
5741 p->replacement_value.value = 0;
5742 p->replacement_value.original_code = ERROR_MARK;
5743 p->replacement_value.original_type = NULL;
5747 p->incremental = constructor_incremental;
5748 p->designated = constructor_designated;
5750 constructor_stack = p;
5752 constructor_constant = 1;
5753 constructor_simple = 1;
5754 constructor_nonconst = 0;
5755 constructor_depth = SPELLING_DEPTH ();
5756 constructor_elements = 0;
5757 constructor_pending_elts = 0;
5758 constructor_type = type;
5759 constructor_incremental = 1;
5760 constructor_designated = 0;
5761 designator_depth = 0;
5762 designator_erroneous = 0;
5764 if (TREE_CODE (constructor_type) == RECORD_TYPE
5765 || TREE_CODE (constructor_type) == UNION_TYPE)
5767 constructor_fields = TYPE_FIELDS (constructor_type);
5768 /* Skip any nameless bit fields at the beginning. */
5769 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5770 && DECL_NAME (constructor_fields) == 0)
5771 constructor_fields = TREE_CHAIN (constructor_fields);
5773 constructor_unfilled_fields = constructor_fields;
5774 constructor_bit_index = bitsize_zero_node;
5776 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5778 if (TYPE_DOMAIN (constructor_type))
5780 constructor_max_index
5781 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5783 /* Detect non-empty initializations of zero-length arrays. */
5784 if (constructor_max_index == NULL_TREE
5785 && TYPE_SIZE (constructor_type))
5786 constructor_max_index = build_int_cst (NULL_TREE, -1);
5788 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5789 to initialize VLAs will cause a proper error; avoid tree
5790 checking errors as well by setting a safe value. */
5791 if (constructor_max_index
5792 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5793 constructor_max_index = build_int_cst (NULL_TREE, -1);
5796 = convert (bitsizetype,
5797 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5801 constructor_index = bitsize_zero_node;
5802 constructor_max_index = NULL_TREE;
5805 constructor_unfilled_index = constructor_index;
5807 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5809 /* Vectors are like simple fixed-size arrays. */
5810 constructor_max_index =
5811 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5812 constructor_index = bitsize_zero_node;
5813 constructor_unfilled_index = constructor_index;
5817 /* Handle the case of int x = {5}; */
5818 constructor_fields = constructor_type;
5819 constructor_unfilled_fields = constructor_type;
5823 /* Push down into a subobject, for initialization.
5824 If this is for an explicit set of braces, IMPLICIT is 0.
5825 If it is because the next element belongs at a lower level,
5826 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5829 push_init_level (int implicit)
5831 struct constructor_stack *p;
5832 tree value = NULL_TREE;
5834 /* If we've exhausted any levels that didn't have braces,
5835 pop them now. If implicit == 1, this will have been done in
5836 process_init_element; do not repeat it here because in the case
5837 of excess initializers for an empty aggregate this leads to an
5838 infinite cycle of popping a level and immediately recreating
5842 while (constructor_stack->implicit)
5844 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5845 || TREE_CODE (constructor_type) == UNION_TYPE)
5846 && constructor_fields == 0)
5847 process_init_element (pop_init_level (1), true);
5848 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5849 && constructor_max_index
5850 && tree_int_cst_lt (constructor_max_index,
5852 process_init_element (pop_init_level (1), true);
5858 /* Unless this is an explicit brace, we need to preserve previous
5862 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5863 || TREE_CODE (constructor_type) == UNION_TYPE)
5864 && constructor_fields)
5865 value = find_init_member (constructor_fields);
5866 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5867 value = find_init_member (constructor_index);
5870 p = XNEW (struct constructor_stack);
5871 p->type = constructor_type;
5872 p->fields = constructor_fields;
5873 p->index = constructor_index;
5874 p->max_index = constructor_max_index;
5875 p->unfilled_index = constructor_unfilled_index;
5876 p->unfilled_fields = constructor_unfilled_fields;
5877 p->bit_index = constructor_bit_index;
5878 p->elements = constructor_elements;
5879 p->constant = constructor_constant;
5880 p->simple = constructor_simple;
5881 p->nonconst = constructor_nonconst;
5882 p->erroneous = constructor_erroneous;
5883 p->pending_elts = constructor_pending_elts;
5884 p->depth = constructor_depth;
5885 p->replacement_value.value = 0;
5886 p->replacement_value.original_code = ERROR_MARK;
5887 p->replacement_value.original_type = NULL;
5888 p->implicit = implicit;
5890 p->incremental = constructor_incremental;
5891 p->designated = constructor_designated;
5892 p->next = constructor_stack;
5894 constructor_stack = p;
5896 constructor_constant = 1;
5897 constructor_simple = 1;
5898 constructor_nonconst = 0;
5899 constructor_depth = SPELLING_DEPTH ();
5900 constructor_elements = 0;
5901 constructor_incremental = 1;
5902 constructor_designated = 0;
5903 constructor_pending_elts = 0;
5906 p->range_stack = constructor_range_stack;
5907 constructor_range_stack = 0;
5908 designator_depth = 0;
5909 designator_erroneous = 0;
5912 /* Don't die if an entire brace-pair level is superfluous
5913 in the containing level. */
5914 if (constructor_type == 0)
5916 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5917 || TREE_CODE (constructor_type) == UNION_TYPE)
5919 /* Don't die if there are extra init elts at the end. */
5920 if (constructor_fields == 0)
5921 constructor_type = 0;
5924 constructor_type = TREE_TYPE (constructor_fields);
5925 push_member_name (constructor_fields);
5926 constructor_depth++;
5929 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5931 constructor_type = TREE_TYPE (constructor_type);
5932 push_array_bounds (tree_low_cst (constructor_index, 1));
5933 constructor_depth++;
5936 if (constructor_type == 0)
5938 error_init ("extra brace group at end of initializer");
5939 constructor_fields = 0;
5940 constructor_unfilled_fields = 0;
5944 if (value && TREE_CODE (value) == CONSTRUCTOR)
5946 constructor_constant = TREE_CONSTANT (value);
5947 constructor_simple = TREE_STATIC (value);
5948 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
5949 constructor_elements = CONSTRUCTOR_ELTS (value);
5950 if (!VEC_empty (constructor_elt, constructor_elements)
5951 && (TREE_CODE (constructor_type) == RECORD_TYPE
5952 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5953 set_nonincremental_init ();
5956 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5958 missing_braces_mentioned = 1;
5959 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
5962 if (TREE_CODE (constructor_type) == RECORD_TYPE
5963 || TREE_CODE (constructor_type) == UNION_TYPE)
5965 constructor_fields = TYPE_FIELDS (constructor_type);
5966 /* Skip any nameless bit fields at the beginning. */
5967 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5968 && DECL_NAME (constructor_fields) == 0)
5969 constructor_fields = TREE_CHAIN (constructor_fields);
5971 constructor_unfilled_fields = constructor_fields;
5972 constructor_bit_index = bitsize_zero_node;
5974 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5976 /* Vectors are like simple fixed-size arrays. */
5977 constructor_max_index =
5978 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5979 constructor_index = convert (bitsizetype, integer_zero_node);
5980 constructor_unfilled_index = constructor_index;
5982 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5984 if (TYPE_DOMAIN (constructor_type))
5986 constructor_max_index
5987 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5989 /* Detect non-empty initializations of zero-length arrays. */
5990 if (constructor_max_index == NULL_TREE
5991 && TYPE_SIZE (constructor_type))
5992 constructor_max_index = build_int_cst (NULL_TREE, -1);
5994 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5995 to initialize VLAs will cause a proper error; avoid tree
5996 checking errors as well by setting a safe value. */
5997 if (constructor_max_index
5998 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5999 constructor_max_index = build_int_cst (NULL_TREE, -1);
6002 = convert (bitsizetype,
6003 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6006 constructor_index = bitsize_zero_node;
6008 constructor_unfilled_index = constructor_index;
6009 if (value && TREE_CODE (value) == STRING_CST)
6011 /* We need to split the char/wchar array into individual
6012 characters, so that we don't have to special case it
6014 set_nonincremental_init_from_string (value);
6019 if (constructor_type != error_mark_node)
6020 warning_init (0, "braces around scalar initializer");
6021 constructor_fields = constructor_type;
6022 constructor_unfilled_fields = constructor_type;
6026 /* At the end of an implicit or explicit brace level,
6027 finish up that level of constructor. If a single expression
6028 with redundant braces initialized that level, return the
6029 c_expr structure for that expression. Otherwise, the original_code
6030 element is set to ERROR_MARK.
6031 If we were outputting the elements as they are read, return 0 as the value
6032 from inner levels (process_init_element ignores that),
6033 but return error_mark_node as the value from the outermost level
6034 (that's what we want to put in DECL_INITIAL).
6035 Otherwise, return a CONSTRUCTOR expression as the value. */
6038 pop_init_level (int implicit)
6040 struct constructor_stack *p;
6043 ret.original_code = ERROR_MARK;
6044 ret.original_type = NULL;
6048 /* When we come to an explicit close brace,
6049 pop any inner levels that didn't have explicit braces. */
6050 while (constructor_stack->implicit)
6051 process_init_element (pop_init_level (1), true);
6053 gcc_assert (!constructor_range_stack);
6056 /* Now output all pending elements. */
6057 constructor_incremental = 1;
6058 output_pending_init_elements (1);
6060 p = constructor_stack;
6062 /* Error for initializing a flexible array member, or a zero-length
6063 array member in an inappropriate context. */
6064 if (constructor_type && constructor_fields
6065 && TREE_CODE (constructor_type) == ARRAY_TYPE
6066 && TYPE_DOMAIN (constructor_type)
6067 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6069 /* Silently discard empty initializations. The parser will
6070 already have pedwarned for empty brackets. */
6071 if (integer_zerop (constructor_unfilled_index))
6072 constructor_type = NULL_TREE;
6075 gcc_assert (!TYPE_SIZE (constructor_type));
6077 if (constructor_depth > 2)
6078 error_init ("initialization of flexible array member in a nested context");
6080 pedwarn_init (input_location, OPT_pedantic,
6081 "initialization of a flexible array member");
6083 /* We have already issued an error message for the existence
6084 of a flexible array member not at the end of the structure.
6085 Discard the initializer so that we do not die later. */
6086 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
6087 constructor_type = NULL_TREE;
6091 /* Warn when some struct elements are implicitly initialized to zero. */
6092 if (warn_missing_field_initializers
6094 && TREE_CODE (constructor_type) == RECORD_TYPE
6095 && constructor_unfilled_fields)
6097 /* Do not warn for flexible array members or zero-length arrays. */
6098 while (constructor_unfilled_fields
6099 && (!DECL_SIZE (constructor_unfilled_fields)
6100 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6101 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6103 /* Do not warn if this level of the initializer uses member
6104 designators; it is likely to be deliberate. */
6105 if (constructor_unfilled_fields && !constructor_designated)
6107 push_member_name (constructor_unfilled_fields);
6108 warning_init (OPT_Wmissing_field_initializers,
6109 "missing initializer");
6110 RESTORE_SPELLING_DEPTH (constructor_depth);
6114 /* Pad out the end of the structure. */
6115 if (p->replacement_value.value)
6116 /* If this closes a superfluous brace pair,
6117 just pass out the element between them. */
6118 ret = p->replacement_value;
6119 else if (constructor_type == 0)
6121 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6122 && TREE_CODE (constructor_type) != UNION_TYPE
6123 && TREE_CODE (constructor_type) != ARRAY_TYPE
6124 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6126 /* A nonincremental scalar initializer--just return
6127 the element, after verifying there is just one. */
6128 if (VEC_empty (constructor_elt,constructor_elements))
6130 if (!constructor_erroneous)
6131 error_init ("empty scalar initializer");
6132 ret.value = error_mark_node;
6134 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6136 error_init ("extra elements in scalar initializer");
6137 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6140 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6144 if (constructor_erroneous)
6145 ret.value = error_mark_node;
6148 ret.value = build_constructor (constructor_type,
6149 constructor_elements);
6150 if (constructor_constant)
6151 TREE_CONSTANT (ret.value) = 1;
6152 if (constructor_constant && constructor_simple)
6153 TREE_STATIC (ret.value) = 1;
6154 if (constructor_nonconst)
6155 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6159 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6161 if (constructor_nonconst)
6162 ret.original_code = C_MAYBE_CONST_EXPR;
6163 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6164 ret.original_code = ERROR_MARK;
6167 constructor_type = p->type;
6168 constructor_fields = p->fields;
6169 constructor_index = p->index;
6170 constructor_max_index = p->max_index;
6171 constructor_unfilled_index = p->unfilled_index;
6172 constructor_unfilled_fields = p->unfilled_fields;
6173 constructor_bit_index = p->bit_index;
6174 constructor_elements = p->elements;
6175 constructor_constant = p->constant;
6176 constructor_simple = p->simple;
6177 constructor_nonconst = p->nonconst;
6178 constructor_erroneous = p->erroneous;
6179 constructor_incremental = p->incremental;
6180 constructor_designated = p->designated;
6181 constructor_pending_elts = p->pending_elts;
6182 constructor_depth = p->depth;
6184 constructor_range_stack = p->range_stack;
6185 RESTORE_SPELLING_DEPTH (constructor_depth);
6187 constructor_stack = p->next;
6190 if (ret.value == 0 && constructor_stack == 0)
6191 ret.value = error_mark_node;
6195 /* Common handling for both array range and field name designators.
6196 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6199 set_designator (int array)
6202 enum tree_code subcode;
6204 /* Don't die if an entire brace-pair level is superfluous
6205 in the containing level. */
6206 if (constructor_type == 0)
6209 /* If there were errors in this designator list already, bail out
6211 if (designator_erroneous)
6214 if (!designator_depth)
6216 gcc_assert (!constructor_range_stack);
6218 /* Designator list starts at the level of closest explicit
6220 while (constructor_stack->implicit)
6221 process_init_element (pop_init_level (1), true);
6222 constructor_designated = 1;
6226 switch (TREE_CODE (constructor_type))
6230 subtype = TREE_TYPE (constructor_fields);
6231 if (subtype != error_mark_node)
6232 subtype = TYPE_MAIN_VARIANT (subtype);
6235 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6241 subcode = TREE_CODE (subtype);
6242 if (array && subcode != ARRAY_TYPE)
6244 error_init ("array index in non-array initializer");
6247 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6249 error_init ("field name not in record or union initializer");
6253 constructor_designated = 1;
6254 push_init_level (2);
6258 /* If there are range designators in designator list, push a new designator
6259 to constructor_range_stack. RANGE_END is end of such stack range or
6260 NULL_TREE if there is no range designator at this level. */
6263 push_range_stack (tree range_end)
6265 struct constructor_range_stack *p;
6267 p = GGC_NEW (struct constructor_range_stack);
6268 p->prev = constructor_range_stack;
6270 p->fields = constructor_fields;
6271 p->range_start = constructor_index;
6272 p->index = constructor_index;
6273 p->stack = constructor_stack;
6274 p->range_end = range_end;
6275 if (constructor_range_stack)
6276 constructor_range_stack->next = p;
6277 constructor_range_stack = p;
6280 /* Within an array initializer, specify the next index to be initialized.
6281 FIRST is that index. If LAST is nonzero, then initialize a range
6282 of indices, running from FIRST through LAST. */
6285 set_init_index (tree first, tree last)
6287 if (set_designator (1))
6290 designator_erroneous = 1;
6292 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
6293 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
6295 error_init ("array index in initializer not of integer type");
6299 if (TREE_CODE (first) != INTEGER_CST)
6300 error_init ("nonconstant array index in initializer");
6301 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
6302 error_init ("nonconstant array index in initializer");
6303 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6304 error_init ("array index in non-array initializer");
6305 else if (tree_int_cst_sgn (first) == -1)
6306 error_init ("array index in initializer exceeds array bounds");
6307 else if (constructor_max_index
6308 && tree_int_cst_lt (constructor_max_index, first))
6309 error_init ("array index in initializer exceeds array bounds");
6312 constant_expression_warning (first);
6314 constant_expression_warning (last);
6315 constructor_index = convert (bitsizetype, first);
6319 if (tree_int_cst_equal (first, last))
6321 else if (tree_int_cst_lt (last, first))
6323 error_init ("empty index range in initializer");
6328 last = convert (bitsizetype, last);
6329 if (constructor_max_index != 0
6330 && tree_int_cst_lt (constructor_max_index, last))
6332 error_init ("array index range in initializer exceeds array bounds");
6339 designator_erroneous = 0;
6340 if (constructor_range_stack || last)
6341 push_range_stack (last);
6345 /* Within a struct initializer, specify the next field to be initialized. */
6348 set_init_label (tree fieldname)
6352 if (set_designator (0))
6355 designator_erroneous = 1;
6357 if (TREE_CODE (constructor_type) != RECORD_TYPE
6358 && TREE_CODE (constructor_type) != UNION_TYPE)
6360 error_init ("field name not in record or union initializer");
6364 for (tail = TYPE_FIELDS (constructor_type); tail;
6365 tail = TREE_CHAIN (tail))
6367 if (DECL_NAME (tail) == fieldname)
6372 error ("unknown field %qE specified in initializer", fieldname);
6375 constructor_fields = tail;
6377 designator_erroneous = 0;
6378 if (constructor_range_stack)
6379 push_range_stack (NULL_TREE);
6383 /* Add a new initializer to the tree of pending initializers. PURPOSE
6384 identifies the initializer, either array index or field in a structure.
6385 VALUE is the value of that index or field.
6387 IMPLICIT is true if value comes from pop_init_level (1),
6388 the new initializer has been merged with the existing one
6389 and thus no warnings should be emitted about overriding an
6390 existing initializer. */
6393 add_pending_init (tree purpose, tree value, bool implicit)
6395 struct init_node *p, **q, *r;
6397 q = &constructor_pending_elts;
6400 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6405 if (tree_int_cst_lt (purpose, p->purpose))
6407 else if (tree_int_cst_lt (p->purpose, purpose))
6413 if (TREE_SIDE_EFFECTS (p->value))
6414 warning_init (0, "initialized field with side-effects overwritten");
6415 else if (warn_override_init)
6416 warning_init (OPT_Woverride_init, "initialized field overwritten");
6427 bitpos = bit_position (purpose);
6431 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6433 else if (p->purpose != purpose)
6439 if (TREE_SIDE_EFFECTS (p->value))
6440 warning_init (0, "initialized field with side-effects overwritten");
6441 else if (warn_override_init)
6442 warning_init (OPT_Woverride_init, "initialized field overwritten");
6450 r = GGC_NEW (struct init_node);
6451 r->purpose = purpose;
6462 struct init_node *s;
6466 if (p->balance == 0)
6468 else if (p->balance < 0)
6475 p->left->parent = p;
6492 constructor_pending_elts = r;
6497 struct init_node *t = r->right;
6501 r->right->parent = r;
6506 p->left->parent = p;
6509 p->balance = t->balance < 0;
6510 r->balance = -(t->balance > 0);
6525 constructor_pending_elts = t;
6531 /* p->balance == +1; growth of left side balances the node. */
6536 else /* r == p->right */
6538 if (p->balance == 0)
6539 /* Growth propagation from right side. */
6541 else if (p->balance > 0)
6548 p->right->parent = p;
6565 constructor_pending_elts = r;
6567 else /* r->balance == -1 */
6570 struct init_node *t = r->left;
6574 r->left->parent = r;
6579 p->right->parent = p;
6582 r->balance = (t->balance < 0);
6583 p->balance = -(t->balance > 0);
6598 constructor_pending_elts = t;
6604 /* p->balance == -1; growth of right side balances the node. */
6615 /* Build AVL tree from a sorted chain. */
6618 set_nonincremental_init (void)
6620 unsigned HOST_WIDE_INT ix;
6623 if (TREE_CODE (constructor_type) != RECORD_TYPE
6624 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6627 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6628 add_pending_init (index, value, false);
6629 constructor_elements = 0;
6630 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6632 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6633 /* Skip any nameless bit fields at the beginning. */
6634 while (constructor_unfilled_fields != 0
6635 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6636 && DECL_NAME (constructor_unfilled_fields) == 0)
6637 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6640 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6642 if (TYPE_DOMAIN (constructor_type))
6643 constructor_unfilled_index
6644 = convert (bitsizetype,
6645 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6647 constructor_unfilled_index = bitsize_zero_node;
6649 constructor_incremental = 0;
6652 /* Build AVL tree from a string constant. */
6655 set_nonincremental_init_from_string (tree str)
6657 tree value, purpose, type;
6658 HOST_WIDE_INT val[2];
6659 const char *p, *end;
6660 int byte, wchar_bytes, charwidth, bitpos;
6662 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
6664 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
6665 charwidth = TYPE_PRECISION (char_type_node);
6666 type = TREE_TYPE (constructor_type);
6667 p = TREE_STRING_POINTER (str);
6668 end = p + TREE_STRING_LENGTH (str);
6670 for (purpose = bitsize_zero_node;
6671 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6672 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6674 if (wchar_bytes == 1)
6676 val[1] = (unsigned char) *p++;
6683 for (byte = 0; byte < wchar_bytes; byte++)
6685 if (BYTES_BIG_ENDIAN)
6686 bitpos = (wchar_bytes - byte - 1) * charwidth;
6688 bitpos = byte * charwidth;
6689 val[bitpos < HOST_BITS_PER_WIDE_INT]
6690 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6691 << (bitpos % HOST_BITS_PER_WIDE_INT);
6695 if (!TYPE_UNSIGNED (type))
6697 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6698 if (bitpos < HOST_BITS_PER_WIDE_INT)
6700 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6702 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6706 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6711 else if (val[0] & (((HOST_WIDE_INT) 1)
6712 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6713 val[0] |= ((HOST_WIDE_INT) -1)
6714 << (bitpos - HOST_BITS_PER_WIDE_INT);
6717 value = build_int_cst_wide (type, val[1], val[0]);
6718 add_pending_init (purpose, value, false);
6721 constructor_incremental = 0;
6724 /* Return value of FIELD in pending initializer or zero if the field was
6725 not initialized yet. */
6728 find_init_member (tree field)
6730 struct init_node *p;
6732 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6734 if (constructor_incremental
6735 && tree_int_cst_lt (field, constructor_unfilled_index))
6736 set_nonincremental_init ();
6738 p = constructor_pending_elts;
6741 if (tree_int_cst_lt (field, p->purpose))
6743 else if (tree_int_cst_lt (p->purpose, field))
6749 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6751 tree bitpos = bit_position (field);
6753 if (constructor_incremental
6754 && (!constructor_unfilled_fields
6755 || tree_int_cst_lt (bitpos,
6756 bit_position (constructor_unfilled_fields))))
6757 set_nonincremental_init ();
6759 p = constructor_pending_elts;
6762 if (field == p->purpose)
6764 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6770 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6772 if (!VEC_empty (constructor_elt, constructor_elements)
6773 && (VEC_last (constructor_elt, constructor_elements)->index
6775 return VEC_last (constructor_elt, constructor_elements)->value;
6780 /* "Output" the next constructor element.
6781 At top level, really output it to assembler code now.
6782 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6783 TYPE is the data type that the containing data type wants here.
6784 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6785 If VALUE is a string constant, STRICT_STRING is true if it is
6786 unparenthesized or we should not warn here for it being parenthesized.
6787 For other types of VALUE, STRICT_STRING is not used.
6789 PENDING if non-nil means output pending elements that belong
6790 right after this element. (PENDING is normally 1;
6791 it is 0 while outputting pending elements, to avoid recursion.)
6793 IMPLICIT is true if value comes from pop_init_level (1),
6794 the new initializer has been merged with the existing one
6795 and thus no warnings should be emitted about overriding an
6796 existing initializer. */
6799 output_init_element (tree value, bool strict_string, tree type, tree field,
6800 int pending, bool implicit)
6802 tree semantic_type = NULL_TREE;
6803 constructor_elt *celt;
6804 bool maybe_const = true;
6807 if (type == error_mark_node || value == error_mark_node)
6809 constructor_erroneous = 1;
6812 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6813 && (TREE_CODE (value) == STRING_CST
6814 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6815 && !(TREE_CODE (value) == STRING_CST
6816 && TREE_CODE (type) == ARRAY_TYPE
6817 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6818 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6819 TYPE_MAIN_VARIANT (type)))
6820 value = array_to_pointer_conversion (value);
6822 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6823 && require_constant_value && !flag_isoc99 && pending)
6825 /* As an extension, allow initializing objects with static storage
6826 duration with compound literals (which are then treated just as
6827 the brace enclosed list they contain). */
6828 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6829 value = DECL_INITIAL (decl);
6832 npc = null_pointer_constant_p (value);
6833 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
6835 semantic_type = TREE_TYPE (value);
6836 value = TREE_OPERAND (value, 0);
6838 value = c_fully_fold (value, require_constant_value, &maybe_const);
6840 if (value == error_mark_node)
6841 constructor_erroneous = 1;
6842 else if (!TREE_CONSTANT (value))
6843 constructor_constant = 0;
6844 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6845 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6846 || TREE_CODE (constructor_type) == UNION_TYPE)
6847 && DECL_C_BIT_FIELD (field)
6848 && TREE_CODE (value) != INTEGER_CST))
6849 constructor_simple = 0;
6851 constructor_nonconst = 1;
6853 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6855 if (require_constant_value)
6857 error_init ("initializer element is not constant");
6858 value = error_mark_node;
6860 else if (require_constant_elements)
6861 pedwarn (input_location, 0,
6862 "initializer element is not computable at load time");
6864 else if (!maybe_const
6865 && (require_constant_value || require_constant_elements))
6866 pedwarn_init (input_location, 0,
6867 "initializer element is not a constant expression");
6869 /* If this field is empty (and not at the end of structure),
6870 don't do anything other than checking the initializer. */
6872 && (TREE_TYPE (field) == error_mark_node
6873 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6874 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6875 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6876 || TREE_CHAIN (field)))))
6880 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
6881 value = digest_init (type, value, npc, strict_string,
6882 require_constant_value);
6883 if (value == error_mark_node)
6885 constructor_erroneous = 1;
6888 if (require_constant_value || require_constant_elements)
6889 constant_expression_warning (value);
6891 /* If this element doesn't come next in sequence,
6892 put it on constructor_pending_elts. */
6893 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6894 && (!constructor_incremental
6895 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6897 if (constructor_incremental
6898 && tree_int_cst_lt (field, constructor_unfilled_index))
6899 set_nonincremental_init ();
6901 add_pending_init (field, value, implicit);
6904 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6905 && (!constructor_incremental
6906 || field != constructor_unfilled_fields))
6908 /* We do this for records but not for unions. In a union,
6909 no matter which field is specified, it can be initialized
6910 right away since it starts at the beginning of the union. */
6911 if (constructor_incremental)
6913 if (!constructor_unfilled_fields)
6914 set_nonincremental_init ();
6917 tree bitpos, unfillpos;
6919 bitpos = bit_position (field);
6920 unfillpos = bit_position (constructor_unfilled_fields);
6922 if (tree_int_cst_lt (bitpos, unfillpos))
6923 set_nonincremental_init ();
6927 add_pending_init (field, value, implicit);
6930 else if (TREE_CODE (constructor_type) == UNION_TYPE
6931 && !VEC_empty (constructor_elt, constructor_elements))
6935 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6936 constructor_elements)->value))
6938 "initialized field with side-effects overwritten");
6939 else if (warn_override_init)
6940 warning_init (OPT_Woverride_init, "initialized field overwritten");
6943 /* We can have just one union field set. */
6944 constructor_elements = 0;
6947 /* Otherwise, output this element either to
6948 constructor_elements or to the assembler file. */
6950 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6951 celt->index = field;
6952 celt->value = value;
6954 /* Advance the variable that indicates sequential elements output. */
6955 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6956 constructor_unfilled_index
6957 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6959 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6961 constructor_unfilled_fields
6962 = TREE_CHAIN (constructor_unfilled_fields);
6964 /* Skip any nameless bit fields. */
6965 while (constructor_unfilled_fields != 0
6966 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6967 && DECL_NAME (constructor_unfilled_fields) == 0)
6968 constructor_unfilled_fields =
6969 TREE_CHAIN (constructor_unfilled_fields);
6971 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6972 constructor_unfilled_fields = 0;
6974 /* Now output any pending elements which have become next. */
6976 output_pending_init_elements (0);
6979 /* Output any pending elements which have become next.
6980 As we output elements, constructor_unfilled_{fields,index}
6981 advances, which may cause other elements to become next;
6982 if so, they too are output.
6984 If ALL is 0, we return when there are
6985 no more pending elements to output now.
6987 If ALL is 1, we output space as necessary so that
6988 we can output all the pending elements. */
6991 output_pending_init_elements (int all)
6993 struct init_node *elt = constructor_pending_elts;
6998 /* Look through the whole pending tree.
6999 If we find an element that should be output now,
7000 output it. Otherwise, set NEXT to the element
7001 that comes first among those still pending. */
7006 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7008 if (tree_int_cst_equal (elt->purpose,
7009 constructor_unfilled_index))
7010 output_init_element (elt->value, true,
7011 TREE_TYPE (constructor_type),
7012 constructor_unfilled_index, 0, false);
7013 else if (tree_int_cst_lt (constructor_unfilled_index,
7016 /* Advance to the next smaller node. */
7021 /* We have reached the smallest node bigger than the
7022 current unfilled index. 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)
7039 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7042 next = elt->purpose;
7048 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7049 || TREE_CODE (constructor_type) == UNION_TYPE)
7051 tree ctor_unfilled_bitpos, elt_bitpos;
7053 /* If the current record is complete we are done. */
7054 if (constructor_unfilled_fields == 0)
7057 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7058 elt_bitpos = bit_position (elt->purpose);
7059 /* We can't compare fields here because there might be empty
7060 fields in between. */
7061 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7063 constructor_unfilled_fields = elt->purpose;
7064 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
7065 elt->purpose, 0, false);
7067 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7069 /* Advance to the next smaller node. */
7074 /* We have reached the smallest node bigger than the
7075 current unfilled field. Fill the space first. */
7076 next = elt->purpose;
7082 /* Advance to the next bigger node. */
7087 /* We have reached the biggest node in a subtree. Find
7088 the parent of it, which is the next bigger node. */
7089 while (elt->parent && elt->parent->right == elt)
7093 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7094 bit_position (elt->purpose))))
7096 next = elt->purpose;
7104 /* Ordinarily return, but not if we want to output all
7105 and there are elements left. */
7106 if (!(all && next != 0))
7109 /* If it's not incremental, just skip over the gap, so that after
7110 jumping to retry we will output the next successive element. */
7111 if (TREE_CODE (constructor_type) == RECORD_TYPE
7112 || TREE_CODE (constructor_type) == UNION_TYPE)
7113 constructor_unfilled_fields = next;
7114 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7115 constructor_unfilled_index = next;
7117 /* ELT now points to the node in the pending tree with the next
7118 initializer to output. */
7122 /* Add one non-braced element to the current constructor level.
7123 This adjusts the current position within the constructor's type.
7124 This may also start or terminate implicit levels
7125 to handle a partly-braced initializer.
7127 Once this has found the correct level for the new element,
7128 it calls output_init_element.
7130 IMPLICIT is true if value comes from pop_init_level (1),
7131 the new initializer has been merged with the existing one
7132 and thus no warnings should be emitted about overriding an
7133 existing initializer. */
7136 process_init_element (struct c_expr value, bool implicit)
7138 tree orig_value = value.value;
7139 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7140 bool strict_string = value.original_code == STRING_CST;
7142 designator_depth = 0;
7143 designator_erroneous = 0;
7145 /* Handle superfluous braces around string cst as in
7146 char x[] = {"foo"}; */
7149 && TREE_CODE (constructor_type) == ARRAY_TYPE
7150 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7151 && integer_zerop (constructor_unfilled_index))
7153 if (constructor_stack->replacement_value.value)
7154 error_init ("excess elements in char array initializer");
7155 constructor_stack->replacement_value = value;
7159 if (constructor_stack->replacement_value.value != 0)
7161 error_init ("excess elements in struct initializer");
7165 /* Ignore elements of a brace group if it is entirely superfluous
7166 and has already been diagnosed. */
7167 if (constructor_type == 0)
7170 /* If we've exhausted any levels that didn't have braces,
7172 while (constructor_stack->implicit)
7174 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7175 || TREE_CODE (constructor_type) == UNION_TYPE)
7176 && constructor_fields == 0)
7177 process_init_element (pop_init_level (1), true);
7178 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7179 && (constructor_max_index == 0
7180 || tree_int_cst_lt (constructor_max_index,
7181 constructor_index)))
7182 process_init_element (pop_init_level (1), true);
7187 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7188 if (constructor_range_stack)
7190 /* If value is a compound literal and we'll be just using its
7191 content, don't put it into a SAVE_EXPR. */
7192 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7193 || !require_constant_value
7196 tree semantic_type = NULL_TREE;
7197 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7199 semantic_type = TREE_TYPE (value.value);
7200 value.value = TREE_OPERAND (value.value, 0);
7202 value.value = c_save_expr (value.value);
7204 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7211 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7214 enum tree_code fieldcode;
7216 if (constructor_fields == 0)
7218 pedwarn_init (input_location, 0,
7219 "excess elements in struct initializer");
7223 fieldtype = TREE_TYPE (constructor_fields);
7224 if (fieldtype != error_mark_node)
7225 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7226 fieldcode = TREE_CODE (fieldtype);
7228 /* Error for non-static initialization of a flexible array member. */
7229 if (fieldcode == ARRAY_TYPE
7230 && !require_constant_value
7231 && TYPE_SIZE (fieldtype) == NULL_TREE
7232 && TREE_CHAIN (constructor_fields) == NULL_TREE)
7234 error_init ("non-static initialization of a flexible array member");
7238 /* Accept a string constant to initialize a subarray. */
7239 if (value.value != 0
7240 && fieldcode == ARRAY_TYPE
7241 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7243 value.value = orig_value;
7244 /* Otherwise, if we have come to a subaggregate,
7245 and we don't have an element of its type, push into it. */
7246 else if (value.value != 0
7247 && value.value != error_mark_node
7248 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7249 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7250 || fieldcode == UNION_TYPE))
7252 push_init_level (1);
7258 push_member_name (constructor_fields);
7259 output_init_element (value.value, strict_string,
7260 fieldtype, constructor_fields, 1, implicit);
7261 RESTORE_SPELLING_DEPTH (constructor_depth);
7264 /* Do the bookkeeping for an element that was
7265 directly output as a constructor. */
7267 /* For a record, keep track of end position of last field. */
7268 if (DECL_SIZE (constructor_fields))
7269 constructor_bit_index
7270 = size_binop (PLUS_EXPR,
7271 bit_position (constructor_fields),
7272 DECL_SIZE (constructor_fields));
7274 /* If the current field was the first one not yet written out,
7275 it isn't now, so update. */
7276 if (constructor_unfilled_fields == constructor_fields)
7278 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7279 /* Skip any nameless bit fields. */
7280 while (constructor_unfilled_fields != 0
7281 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7282 && DECL_NAME (constructor_unfilled_fields) == 0)
7283 constructor_unfilled_fields =
7284 TREE_CHAIN (constructor_unfilled_fields);
7288 constructor_fields = TREE_CHAIN (constructor_fields);
7289 /* Skip any nameless bit fields at the beginning. */
7290 while (constructor_fields != 0
7291 && DECL_C_BIT_FIELD (constructor_fields)
7292 && DECL_NAME (constructor_fields) == 0)
7293 constructor_fields = TREE_CHAIN (constructor_fields);
7295 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7298 enum tree_code fieldcode;
7300 if (constructor_fields == 0)
7302 pedwarn_init (input_location, 0,
7303 "excess elements in union initializer");
7307 fieldtype = TREE_TYPE (constructor_fields);
7308 if (fieldtype != error_mark_node)
7309 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7310 fieldcode = TREE_CODE (fieldtype);
7312 /* Warn that traditional C rejects initialization of unions.
7313 We skip the warning if the value is zero. This is done
7314 under the assumption that the zero initializer in user
7315 code appears conditioned on e.g. __STDC__ to avoid
7316 "missing initializer" warnings and relies on default
7317 initialization to zero in the traditional C case.
7318 We also skip the warning if the initializer is designated,
7319 again on the assumption that this must be conditional on
7320 __STDC__ anyway (and we've already complained about the
7321 member-designator already). */
7322 if (!in_system_header && !constructor_designated
7323 && !(value.value && (integer_zerop (value.value)
7324 || real_zerop (value.value))))
7325 warning (OPT_Wtraditional, "traditional C rejects initialization "
7328 /* Accept a string constant to initialize a subarray. */
7329 if (value.value != 0
7330 && fieldcode == ARRAY_TYPE
7331 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7333 value.value = orig_value;
7334 /* Otherwise, if we have come to a subaggregate,
7335 and we don't have an element of its type, push into it. */
7336 else if (value.value != 0
7337 && value.value != error_mark_node
7338 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7339 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7340 || fieldcode == UNION_TYPE))
7342 push_init_level (1);
7348 push_member_name (constructor_fields);
7349 output_init_element (value.value, strict_string,
7350 fieldtype, constructor_fields, 1, implicit);
7351 RESTORE_SPELLING_DEPTH (constructor_depth);
7354 /* Do the bookkeeping for an element that was
7355 directly output as a constructor. */
7357 constructor_bit_index = DECL_SIZE (constructor_fields);
7358 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7361 constructor_fields = 0;
7363 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7365 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7366 enum tree_code eltcode = TREE_CODE (elttype);
7368 /* Accept a string constant to initialize a subarray. */
7369 if (value.value != 0
7370 && eltcode == ARRAY_TYPE
7371 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
7373 value.value = orig_value;
7374 /* Otherwise, if we have come to a subaggregate,
7375 and we don't have an element of its type, push into it. */
7376 else if (value.value != 0
7377 && value.value != error_mark_node
7378 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
7379 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
7380 || eltcode == UNION_TYPE))
7382 push_init_level (1);
7386 if (constructor_max_index != 0
7387 && (tree_int_cst_lt (constructor_max_index, constructor_index)
7388 || integer_all_onesp (constructor_max_index)))
7390 pedwarn_init (input_location, 0,
7391 "excess elements in array initializer");
7395 /* Now output the actual element. */
7398 push_array_bounds (tree_low_cst (constructor_index, 1));
7399 output_init_element (value.value, strict_string,
7400 elttype, constructor_index, 1, implicit);
7401 RESTORE_SPELLING_DEPTH (constructor_depth);
7405 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7408 /* If we are doing the bookkeeping for an element that was
7409 directly output as a constructor, we must update
7410 constructor_unfilled_index. */
7411 constructor_unfilled_index = constructor_index;
7413 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7415 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7417 /* Do a basic check of initializer size. Note that vectors
7418 always have a fixed size derived from their type. */
7419 if (tree_int_cst_lt (constructor_max_index, constructor_index))
7421 pedwarn_init (input_location, 0,
7422 "excess elements in vector initializer");
7426 /* Now output the actual element. */
7428 output_init_element (value.value, strict_string,
7429 elttype, constructor_index, 1, implicit);
7432 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7435 /* If we are doing the bookkeeping for an element that was
7436 directly output as a constructor, we must update
7437 constructor_unfilled_index. */
7438 constructor_unfilled_index = constructor_index;
7441 /* Handle the sole element allowed in a braced initializer
7442 for a scalar variable. */
7443 else if (constructor_type != error_mark_node
7444 && constructor_fields == 0)
7446 pedwarn_init (input_location, 0,
7447 "excess elements in scalar initializer");
7453 output_init_element (value.value, strict_string,
7454 constructor_type, NULL_TREE, 1, implicit);
7455 constructor_fields = 0;
7458 /* Handle range initializers either at this level or anywhere higher
7459 in the designator stack. */
7460 if (constructor_range_stack)
7462 struct constructor_range_stack *p, *range_stack;
7465 range_stack = constructor_range_stack;
7466 constructor_range_stack = 0;
7467 while (constructor_stack != range_stack->stack)
7469 gcc_assert (constructor_stack->implicit);
7470 process_init_element (pop_init_level (1), true);
7472 for (p = range_stack;
7473 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
7476 gcc_assert (constructor_stack->implicit);
7477 process_init_element (pop_init_level (1), true);
7480 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
7481 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
7486 constructor_index = p->index;
7487 constructor_fields = p->fields;
7488 if (finish && p->range_end && p->index == p->range_start)
7496 push_init_level (2);
7497 p->stack = constructor_stack;
7498 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
7499 p->index = p->range_start;
7503 constructor_range_stack = range_stack;
7510 constructor_range_stack = 0;
7513 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7514 (guaranteed to be 'volatile' or null) and ARGS (represented using
7515 an ASM_EXPR node). */
7517 build_asm_stmt (tree cv_qualifier, tree args)
7519 if (!ASM_VOLATILE_P (args) && cv_qualifier)
7520 ASM_VOLATILE_P (args) = 1;
7521 return add_stmt (args);
7524 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7525 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7526 SIMPLE indicates whether there was anything at all after the
7527 string in the asm expression -- asm("blah") and asm("blah" : )
7528 are subtly different. We use a ASM_EXPR node to represent this. */
7530 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
7536 const char *constraint;
7537 const char **oconstraints;
7538 bool allows_mem, allows_reg, is_inout;
7539 int ninputs, noutputs;
7541 ninputs = list_length (inputs);
7542 noutputs = list_length (outputs);
7543 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
7545 string = resolve_asm_operand_names (string, outputs, inputs);
7547 /* Remove output conversions that change the type but not the mode. */
7548 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
7550 tree output = TREE_VALUE (tail);
7552 /* ??? Really, this should not be here. Users should be using a
7553 proper lvalue, dammit. But there's a long history of using casts
7554 in the output operands. In cases like longlong.h, this becomes a
7555 primitive form of typechecking -- if the cast can be removed, then
7556 the output operand had a type of the proper width; otherwise we'll
7557 get an error. Gross, but ... */
7558 STRIP_NOPS (output);
7560 if (!lvalue_or_else (output, lv_asm))
7561 output = error_mark_node;
7563 if (output != error_mark_node
7564 && (TREE_READONLY (output)
7565 || TYPE_READONLY (TREE_TYPE (output))
7566 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
7567 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
7568 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
7569 readonly_error (output, lv_asm);
7571 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7572 oconstraints[i] = constraint;
7574 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7575 &allows_mem, &allows_reg, &is_inout))
7577 /* If the operand is going to end up in memory,
7578 mark it addressable. */
7579 if (!allows_reg && !c_mark_addressable (output))
7580 output = error_mark_node;
7583 output = error_mark_node;
7585 TREE_VALUE (tail) = output;
7588 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
7592 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7593 input = TREE_VALUE (tail);
7595 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
7596 oconstraints, &allows_mem, &allows_reg))
7598 /* If the operand is going to end up in memory,
7599 mark it addressable. */
7600 if (!allows_reg && allows_mem)
7602 /* Strip the nops as we allow this case. FIXME, this really
7603 should be rejected or made deprecated. */
7605 if (!c_mark_addressable (input))
7606 input = error_mark_node;
7610 input = error_mark_node;
7612 TREE_VALUE (tail) = input;
7615 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
7617 /* asm statements without outputs, including simple ones, are treated
7619 ASM_INPUT_P (args) = simple;
7620 ASM_VOLATILE_P (args) = (noutputs == 0);
7625 /* Generate a goto statement to LABEL. */
7628 c_finish_goto_label (tree label)
7630 tree decl = lookup_label (label);
7634 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
7636 error ("jump into statement expression");
7640 if (C_DECL_UNJUMPABLE_VM (decl))
7642 error ("jump into scope of identifier with variably modified type");
7646 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
7648 /* No jump from outside this statement expression context, so
7649 record that there is a jump from within this context. */
7650 struct c_label_list *nlist;
7651 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7652 nlist->next = label_context_stack_se->labels_used;
7653 nlist->label = decl;
7654 label_context_stack_se->labels_used = nlist;
7657 if (!C_DECL_UNDEFINABLE_VM (decl))
7659 /* No jump from outside this context context of identifiers with
7660 variably modified type, so record that there is a jump from
7661 within this context. */
7662 struct c_label_list *nlist;
7663 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7664 nlist->next = label_context_stack_vm->labels_used;
7665 nlist->label = decl;
7666 label_context_stack_vm->labels_used = nlist;
7669 TREE_USED (decl) = 1;
7670 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
7673 /* Generate a computed goto statement to EXPR. */
7676 c_finish_goto_ptr (tree expr)
7678 pedwarn (input_location, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
7679 expr = c_fully_fold (expr, false, NULL);
7680 expr = convert (ptr_type_node, expr);
7681 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
7684 /* Generate a C `return' statement. RETVAL is the expression for what
7685 to return, or a null pointer for `return;' with no value. */
7688 c_finish_return (tree retval)
7690 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
7691 bool no_warning = false;
7694 if (TREE_THIS_VOLATILE (current_function_decl))
7695 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7699 tree semantic_type = NULL_TREE;
7700 npc = null_pointer_constant_p (retval);
7701 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
7703 semantic_type = TREE_TYPE (retval);
7704 retval = TREE_OPERAND (retval, 0);
7706 retval = c_fully_fold (retval, false, NULL);
7708 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
7713 current_function_returns_null = 1;
7714 if ((warn_return_type || flag_isoc99)
7715 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7717 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wreturn_type,
7718 "%<return%> with no value, in "
7719 "function returning non-void");
7723 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7725 current_function_returns_null = 1;
7726 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7727 pedwarn (input_location, 0,
7728 "%<return%> with a value, in function returning void");
7730 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
7731 "%<return%> with expression, in function returning void");
7735 tree t = convert_for_assignment (valtype, retval, ic_return, npc,
7736 NULL_TREE, NULL_TREE, 0);
7737 tree res = DECL_RESULT (current_function_decl);
7740 current_function_returns_value = 1;
7741 if (t == error_mark_node)
7744 inner = t = convert (TREE_TYPE (res), t);
7746 /* Strip any conversions, additions, and subtractions, and see if
7747 we are returning the address of a local variable. Warn if so. */
7750 switch (TREE_CODE (inner))
7753 case NON_LVALUE_EXPR:
7755 case POINTER_PLUS_EXPR:
7756 inner = TREE_OPERAND (inner, 0);
7760 /* If the second operand of the MINUS_EXPR has a pointer
7761 type (or is converted from it), this may be valid, so
7762 don't give a warning. */
7764 tree op1 = TREE_OPERAND (inner, 1);
7766 while (!POINTER_TYPE_P (TREE_TYPE (op1))
7767 && (CONVERT_EXPR_P (op1)
7768 || TREE_CODE (op1) == NON_LVALUE_EXPR))
7769 op1 = TREE_OPERAND (op1, 0);
7771 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7774 inner = TREE_OPERAND (inner, 0);
7779 inner = TREE_OPERAND (inner, 0);
7781 while (REFERENCE_CLASS_P (inner)
7782 && TREE_CODE (inner) != INDIRECT_REF)
7783 inner = TREE_OPERAND (inner, 0);
7786 && !DECL_EXTERNAL (inner)
7787 && !TREE_STATIC (inner)
7788 && DECL_CONTEXT (inner) == current_function_decl)
7789 warning (0, "function returns address of local variable");
7799 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
7801 if (warn_sequence_point)
7802 verify_sequence_points (retval);
7805 ret_stmt = build_stmt (RETURN_EXPR, retval);
7806 TREE_NO_WARNING (ret_stmt) |= no_warning;
7807 return add_stmt (ret_stmt);
7811 /* The SWITCH_EXPR being built. */
7814 /* The original type of the testing expression, i.e. before the
7815 default conversion is applied. */
7818 /* A splay-tree mapping the low element of a case range to the high
7819 element, or NULL_TREE if there is no high element. Used to
7820 determine whether or not a new case label duplicates an old case
7821 label. We need a tree, rather than simply a hash table, because
7822 of the GNU case range extension. */
7825 /* Number of nested statement expressions within this switch
7826 statement; if nonzero, case and default labels may not
7828 unsigned int blocked_stmt_expr;
7830 /* Scope of outermost declarations of identifiers with variably
7831 modified type within this switch statement; if nonzero, case and
7832 default labels may not appear. */
7833 unsigned int blocked_vm;
7835 /* The next node on the stack. */
7836 struct c_switch *next;
7839 /* A stack of the currently active switch statements. The innermost
7840 switch statement is on the top of the stack. There is no need to
7841 mark the stack for garbage collection because it is only active
7842 during the processing of the body of a function, and we never
7843 collect at that point. */
7845 struct c_switch *c_switch_stack;
7847 /* Start a C switch statement, testing expression EXP. Return the new
7851 c_start_case (tree exp)
7853 tree orig_type = error_mark_node;
7854 struct c_switch *cs;
7856 if (exp != error_mark_node)
7858 orig_type = TREE_TYPE (exp);
7860 if (!INTEGRAL_TYPE_P (orig_type))
7862 if (orig_type != error_mark_node)
7864 error ("switch quantity not an integer");
7865 orig_type = error_mark_node;
7867 exp = integer_zero_node;
7871 tree type = TYPE_MAIN_VARIANT (orig_type);
7873 if (!in_system_header
7874 && (type == long_integer_type_node
7875 || type == long_unsigned_type_node))
7876 warning (OPT_Wtraditional, "%<long%> switch expression not "
7877 "converted to %<int%> in ISO C");
7879 exp = c_fully_fold (exp, false, NULL);
7880 exp = default_conversion (exp);
7882 if (warn_sequence_point)
7883 verify_sequence_points (exp);
7887 /* Add this new SWITCH_EXPR to the stack. */
7888 cs = XNEW (struct c_switch);
7889 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7890 cs->orig_type = orig_type;
7891 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7892 cs->blocked_stmt_expr = 0;
7894 cs->next = c_switch_stack;
7895 c_switch_stack = cs;
7897 return add_stmt (cs->switch_expr);
7900 /* Process a case label. */
7903 do_case (tree low_value, tree high_value)
7905 tree label = NULL_TREE;
7907 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
7909 low_value = c_fully_fold (low_value, false, NULL);
7910 if (TREE_CODE (low_value) == INTEGER_CST)
7911 pedwarn (input_location, OPT_pedantic,
7912 "case label is not an integer constant expression");
7915 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
7917 high_value = c_fully_fold (high_value, false, NULL);
7918 if (TREE_CODE (high_value) == INTEGER_CST)
7919 pedwarn (input_location, OPT_pedantic,
7920 "case label is not an integer constant expression");
7923 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7924 && !c_switch_stack->blocked_vm)
7926 label = c_add_case_label (c_switch_stack->cases,
7927 SWITCH_COND (c_switch_stack->switch_expr),
7928 c_switch_stack->orig_type,
7929 low_value, high_value);
7930 if (label == error_mark_node)
7933 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7936 error ("case label in statement expression not containing "
7937 "enclosing switch statement");
7939 error ("%<default%> label in statement expression not containing "
7940 "enclosing switch statement");
7942 else if (c_switch_stack && c_switch_stack->blocked_vm)
7945 error ("case label in scope of identifier with variably modified "
7946 "type not containing enclosing switch statement");
7948 error ("%<default%> label in scope of identifier with variably "
7949 "modified type not containing enclosing switch statement");
7952 error ("case label not within a switch statement");
7954 error ("%<default%> label not within a switch statement");
7959 /* Finish the switch statement. */
7962 c_finish_case (tree body)
7964 struct c_switch *cs = c_switch_stack;
7965 location_t switch_location;
7967 SWITCH_BODY (cs->switch_expr) = body;
7969 /* We must not be within a statement expression nested in the switch
7970 at this point; we might, however, be within the scope of an
7971 identifier with variably modified type nested in the switch. */
7972 gcc_assert (!cs->blocked_stmt_expr);
7974 /* Emit warnings as needed. */
7975 if (EXPR_HAS_LOCATION (cs->switch_expr))
7976 switch_location = EXPR_LOCATION (cs->switch_expr);
7978 switch_location = input_location;
7979 c_do_switch_warnings (cs->cases, switch_location,
7980 TREE_TYPE (cs->switch_expr),
7981 SWITCH_COND (cs->switch_expr));
7983 /* Pop the stack. */
7984 c_switch_stack = cs->next;
7985 splay_tree_delete (cs->cases);
7989 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7990 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7991 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7992 statement, and was not surrounded with parenthesis. */
7995 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7996 tree else_block, bool nested_if)
8000 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8001 if (warn_parentheses && nested_if && else_block == NULL)
8003 tree inner_if = then_block;
8005 /* We know from the grammar productions that there is an IF nested
8006 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8007 it might not be exactly THEN_BLOCK, but should be the last
8008 non-container statement within. */
8010 switch (TREE_CODE (inner_if))
8015 inner_if = BIND_EXPR_BODY (inner_if);
8017 case STATEMENT_LIST:
8018 inner_if = expr_last (then_block);
8020 case TRY_FINALLY_EXPR:
8021 case TRY_CATCH_EXPR:
8022 inner_if = TREE_OPERAND (inner_if, 0);
8029 if (COND_EXPR_ELSE (inner_if))
8030 warning (OPT_Wparentheses,
8031 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
8035 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8036 SET_EXPR_LOCATION (stmt, if_locus);
8040 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8041 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8042 is false for DO loops. INCR is the FOR increment expression. BODY is
8043 the statement controlled by the loop. BLAB is the break label. CLAB is
8044 the continue label. Everything is allowed to be NULL. */
8047 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8048 tree blab, tree clab, bool cond_is_first)
8050 tree entry = NULL, exit = NULL, t;
8052 /* If the condition is zero don't generate a loop construct. */
8053 if (cond && integer_zerop (cond))
8057 t = build_and_jump (&blab);
8058 SET_EXPR_LOCATION (t, start_locus);
8064 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8066 /* If we have an exit condition, then we build an IF with gotos either
8067 out of the loop, or to the top of it. If there's no exit condition,
8068 then we just build a jump back to the top. */
8069 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8071 if (cond && !integer_nonzerop (cond))
8073 /* Canonicalize the loop condition to the end. This means
8074 generating a branch to the loop condition. Reuse the
8075 continue label, if possible. */
8080 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8081 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8084 t = build1 (GOTO_EXPR, void_type_node, clab);
8085 SET_EXPR_LOCATION (t, start_locus);
8089 t = build_and_jump (&blab);
8090 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
8092 SET_EXPR_LOCATION (exit, start_locus);
8094 SET_EXPR_LOCATION (exit, input_location);
8103 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8111 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8115 c_finish_bc_stmt (tree *label_p, bool is_break)
8118 tree label = *label_p;
8120 /* In switch statements break is sometimes stylistically used after
8121 a return statement. This can lead to spurious warnings about
8122 control reaching the end of a non-void function when it is
8123 inlined. Note that we are calling block_may_fallthru with
8124 language specific tree nodes; this works because
8125 block_may_fallthru returns true when given something it does not
8127 skip = !block_may_fallthru (cur_stmt_list);
8132 *label_p = label = create_artificial_label ();
8134 else if (TREE_CODE (label) == LABEL_DECL)
8136 else switch (TREE_INT_CST_LOW (label))
8140 error ("break statement not within loop or switch");
8142 error ("continue statement not within a loop");
8146 gcc_assert (is_break);
8147 error ("break statement used with OpenMP for loop");
8158 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8160 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8163 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8166 emit_side_effect_warnings (tree expr)
8168 if (expr == error_mark_node)
8170 else if (!TREE_SIDE_EFFECTS (expr))
8172 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8173 warning (OPT_Wunused_value, "%Hstatement with no effect",
8174 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
8177 warn_if_unused_value (expr, input_location);
8180 /* Process an expression as if it were a complete statement. Emit
8181 diagnostics, but do not call ADD_STMT. */
8184 c_process_expr_stmt (tree expr)
8189 expr = c_fully_fold (expr, false, NULL);
8191 if (warn_sequence_point)
8192 verify_sequence_points (expr);
8194 if (TREE_TYPE (expr) != error_mark_node
8195 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8196 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8197 error ("expression statement has incomplete type");
8199 /* If we're not processing a statement expression, warn about unused values.
8200 Warnings for statement expressions will be emitted later, once we figure
8201 out which is the result. */
8202 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8203 && warn_unused_value)
8204 emit_side_effect_warnings (expr);
8206 /* If the expression is not of a type to which we cannot assign a line
8207 number, wrap the thing in a no-op NOP_EXPR. */
8208 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8209 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8211 if (CAN_HAVE_LOCATION_P (expr))
8212 SET_EXPR_LOCATION (expr, input_location);
8217 /* Emit an expression as a statement. */
8220 c_finish_expr_stmt (tree expr)
8223 return add_stmt (c_process_expr_stmt (expr));
8228 /* Do the opposite and emit a statement as an expression. To begin,
8229 create a new binding level and return it. */
8232 c_begin_stmt_expr (void)
8235 struct c_label_context_se *nstack;
8236 struct c_label_list *glist;
8238 /* We must force a BLOCK for this level so that, if it is not expanded
8239 later, there is a way to turn off the entire subtree of blocks that
8240 are contained in it. */
8242 ret = c_begin_compound_stmt (true);
8245 c_switch_stack->blocked_stmt_expr++;
8246 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8248 for (glist = label_context_stack_se->labels_used;
8250 glist = glist->next)
8252 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
8254 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
8255 nstack->labels_def = NULL;
8256 nstack->labels_used = NULL;
8257 nstack->next = label_context_stack_se;
8258 label_context_stack_se = nstack;
8260 /* Mark the current statement list as belonging to a statement list. */
8261 STATEMENT_LIST_STMT_EXPR (ret) = 1;
8267 c_finish_stmt_expr (tree body)
8269 tree last, type, tmp, val;
8271 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8273 body = c_end_compound_stmt (body, true);
8276 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8277 c_switch_stack->blocked_stmt_expr--;
8279 /* It is no longer possible to jump to labels defined within this
8280 statement expression. */
8281 for (dlist = label_context_stack_se->labels_def;
8283 dlist = dlist->next)
8285 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
8287 /* It is again possible to define labels with a goto just outside
8288 this statement expression. */
8289 for (glist = label_context_stack_se->next->labels_used;
8291 glist = glist->next)
8293 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
8296 if (glist_prev != NULL)
8297 glist_prev->next = label_context_stack_se->labels_used;
8299 label_context_stack_se->next->labels_used
8300 = label_context_stack_se->labels_used;
8301 label_context_stack_se = label_context_stack_se->next;
8303 /* Locate the last statement in BODY. See c_end_compound_stmt
8304 about always returning a BIND_EXPR. */
8305 last_p = &BIND_EXPR_BODY (body);
8306 last = BIND_EXPR_BODY (body);
8309 if (TREE_CODE (last) == STATEMENT_LIST)
8311 tree_stmt_iterator i;
8313 /* This can happen with degenerate cases like ({ }). No value. */
8314 if (!TREE_SIDE_EFFECTS (last))
8317 /* If we're supposed to generate side effects warnings, process
8318 all of the statements except the last. */
8319 if (warn_unused_value)
8321 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8322 emit_side_effect_warnings (tsi_stmt (i));
8325 i = tsi_last (last);
8326 last_p = tsi_stmt_ptr (i);
8330 /* If the end of the list is exception related, then the list was split
8331 by a call to push_cleanup. Continue searching. */
8332 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8333 || TREE_CODE (last) == TRY_CATCH_EXPR)
8335 last_p = &TREE_OPERAND (last, 0);
8337 goto continue_searching;
8340 /* In the case that the BIND_EXPR is not necessary, return the
8341 expression out from inside it. */
8342 if (last == error_mark_node
8343 || (last == BIND_EXPR_BODY (body)
8344 && BIND_EXPR_VARS (body) == NULL))
8346 /* Even if this looks constant, do not allow it in a constant
8348 last = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (last), NULL_TREE, last);
8349 C_MAYBE_CONST_EXPR_NON_CONST (last) = 1;
8350 /* Do not warn if the return value of a statement expression is
8352 TREE_NO_WARNING (last) = 1;
8356 /* Extract the type of said expression. */
8357 type = TREE_TYPE (last);
8359 /* If we're not returning a value at all, then the BIND_EXPR that
8360 we already have is a fine expression to return. */
8361 if (!type || VOID_TYPE_P (type))
8364 /* Now that we've located the expression containing the value, it seems
8365 silly to make voidify_wrapper_expr repeat the process. Create a
8366 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8367 tmp = create_tmp_var_raw (type, NULL);
8369 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8370 tree_expr_nonnegative_p giving up immediately. */
8372 if (TREE_CODE (val) == NOP_EXPR
8373 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
8374 val = TREE_OPERAND (val, 0);
8376 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
8377 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
8379 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
8382 /* Begin the scope of an identifier of variably modified type, scope
8383 number SCOPE. Jumping from outside this scope to inside it is not
8387 c_begin_vm_scope (unsigned int scope)
8389 struct c_label_context_vm *nstack;
8390 struct c_label_list *glist;
8392 gcc_assert (scope > 0);
8394 /* At file_scope, we don't have to do any processing. */
8395 if (label_context_stack_vm == NULL)
8398 if (c_switch_stack && !c_switch_stack->blocked_vm)
8399 c_switch_stack->blocked_vm = scope;
8400 for (glist = label_context_stack_vm->labels_used;
8402 glist = glist->next)
8404 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
8406 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
8407 nstack->labels_def = NULL;
8408 nstack->labels_used = NULL;
8409 nstack->scope = scope;
8410 nstack->next = label_context_stack_vm;
8411 label_context_stack_vm = nstack;
8414 /* End a scope which may contain identifiers of variably modified
8415 type, scope number SCOPE. */
8418 c_end_vm_scope (unsigned int scope)
8420 if (label_context_stack_vm == NULL)
8422 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
8423 c_switch_stack->blocked_vm = 0;
8424 /* We may have a number of nested scopes of identifiers with
8425 variably modified type, all at this depth. Pop each in turn. */
8426 while (label_context_stack_vm->scope == scope)
8428 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8430 /* It is no longer possible to jump to labels defined within this
8432 for (dlist = label_context_stack_vm->labels_def;
8434 dlist = dlist->next)
8436 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
8438 /* It is again possible to define labels with a goto just outside
8440 for (glist = label_context_stack_vm->next->labels_used;
8442 glist = glist->next)
8444 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
8447 if (glist_prev != NULL)
8448 glist_prev->next = label_context_stack_vm->labels_used;
8450 label_context_stack_vm->next->labels_used
8451 = label_context_stack_vm->labels_used;
8452 label_context_stack_vm = label_context_stack_vm->next;
8456 /* Begin and end compound statements. This is as simple as pushing
8457 and popping new statement lists from the tree. */
8460 c_begin_compound_stmt (bool do_scope)
8462 tree stmt = push_stmt_list ();
8469 c_end_compound_stmt (tree stmt, bool do_scope)
8475 if (c_dialect_objc ())
8476 objc_clear_super_receiver ();
8477 block = pop_scope ();
8480 stmt = pop_stmt_list (stmt);
8481 stmt = c_build_bind_expr (block, stmt);
8483 /* If this compound statement is nested immediately inside a statement
8484 expression, then force a BIND_EXPR to be created. Otherwise we'll
8485 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8486 STATEMENT_LISTs merge, and thus we can lose track of what statement
8489 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8490 && TREE_CODE (stmt) != BIND_EXPR)
8492 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
8493 TREE_SIDE_EFFECTS (stmt) = 1;
8499 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8500 when the current scope is exited. EH_ONLY is true when this is not
8501 meant to apply to normal control flow transfer. */
8504 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
8506 enum tree_code code;
8510 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
8511 stmt = build_stmt (code, NULL, cleanup);
8513 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
8514 list = push_stmt_list ();
8515 TREE_OPERAND (stmt, 0) = list;
8516 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
8519 /* Build a binary-operation expression without default conversions.
8520 CODE is the kind of expression to build.
8521 LOCATION is the operator's location.
8522 This function differs from `build' in several ways:
8523 the data type of the result is computed and recorded in it,
8524 warnings are generated if arg data types are invalid,
8525 special handling for addition and subtraction of pointers is known,
8526 and some optimization is done (operations on narrow ints
8527 are done in the narrower type when that gives the same result).
8528 Constant folding is also done before the result is returned.
8530 Note that the operands will never have enumeral types, or function
8531 or array types, because either they will have the default conversions
8532 performed or they have both just been converted to some other type in which
8533 the arithmetic is to be done. */
8536 build_binary_op (location_t location, enum tree_code code,
8537 tree orig_op0, tree orig_op1, int convert_p)
8539 tree type0, type1, orig_type0, orig_type1;
8541 enum tree_code code0, code1;
8543 tree ret = error_mark_node;
8544 const char *invalid_op_diag;
8545 bool op0_int_operands, op1_int_operands;
8546 bool int_const, int_const_or_overflow, int_operands;
8548 /* Expression code to give to the expression when it is built.
8549 Normally this is CODE, which is what the caller asked for,
8550 but in some special cases we change it. */
8551 enum tree_code resultcode = code;
8553 /* Data type in which the computation is to be performed.
8554 In the simplest cases this is the common type of the arguments. */
8555 tree result_type = NULL;
8557 /* When the computation is in excess precision, the type of the
8558 final EXCESS_PRECISION_EXPR. */
8559 tree real_result_type = NULL;
8561 /* Nonzero means operands have already been type-converted
8562 in whatever way is necessary.
8563 Zero means they need to be converted to RESULT_TYPE. */
8566 /* Nonzero means create the expression with this type, rather than
8568 tree build_type = 0;
8570 /* Nonzero means after finally constructing the expression
8571 convert it to this type. */
8572 tree final_type = 0;
8574 /* Nonzero if this is an operation like MIN or MAX which can
8575 safely be computed in short if both args are promoted shorts.
8576 Also implies COMMON.
8577 -1 indicates a bitwise operation; this makes a difference
8578 in the exact conditions for when it is safe to do the operation
8579 in a narrower mode. */
8582 /* Nonzero if this is a comparison operation;
8583 if both args are promoted shorts, compare the original shorts.
8584 Also implies COMMON. */
8585 int short_compare = 0;
8587 /* Nonzero if this is a right-shift operation, which can be computed on the
8588 original short and then promoted if the operand is a promoted short. */
8589 int short_shift = 0;
8591 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8594 /* True means types are compatible as far as ObjC is concerned. */
8597 /* True means this is an arithmetic operation that may need excess
8599 bool may_need_excess_precision;
8601 if (location == UNKNOWN_LOCATION)
8602 location = input_location;
8607 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
8608 if (op0_int_operands)
8609 op0 = remove_c_maybe_const_expr (op0);
8610 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
8611 if (op1_int_operands)
8612 op1 = remove_c_maybe_const_expr (op1);
8613 int_operands = (op0_int_operands && op1_int_operands);
8616 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
8617 && TREE_CODE (orig_op1) == INTEGER_CST);
8618 int_const = (int_const_or_overflow
8619 && !TREE_OVERFLOW (orig_op0)
8620 && !TREE_OVERFLOW (orig_op1));
8623 int_const = int_const_or_overflow = false;
8627 op0 = default_conversion (op0);
8628 op1 = default_conversion (op1);
8631 orig_type0 = type0 = TREE_TYPE (op0);
8632 orig_type1 = type1 = TREE_TYPE (op1);
8634 /* The expression codes of the data types of the arguments tell us
8635 whether the arguments are integers, floating, pointers, etc. */
8636 code0 = TREE_CODE (type0);
8637 code1 = TREE_CODE (type1);
8639 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8640 STRIP_TYPE_NOPS (op0);
8641 STRIP_TYPE_NOPS (op1);
8643 /* If an error was already reported for one of the arguments,
8644 avoid reporting another error. */
8646 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8647 return error_mark_node;
8649 if ((invalid_op_diag
8650 = targetm.invalid_binary_op (code, type0, type1)))
8652 error_at (location, invalid_op_diag);
8653 return error_mark_node;
8661 case TRUNC_DIV_EXPR:
8663 case FLOOR_DIV_EXPR:
8664 case ROUND_DIV_EXPR:
8665 case EXACT_DIV_EXPR:
8666 may_need_excess_precision = true;
8669 may_need_excess_precision = false;
8672 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
8674 op0 = TREE_OPERAND (op0, 0);
8675 type0 = TREE_TYPE (op0);
8677 else if (may_need_excess_precision
8678 && (eptype = excess_precision_type (type0)) != NULL_TREE)
8681 op0 = convert (eptype, op0);
8683 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
8685 op1 = TREE_OPERAND (op1, 0);
8686 type1 = TREE_TYPE (op1);
8688 else if (may_need_excess_precision
8689 && (eptype = excess_precision_type (type1)) != NULL_TREE)
8692 op1 = convert (eptype, op1);
8695 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
8700 /* Handle the pointer + int case. */
8701 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8703 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
8704 goto return_build_binary_op;
8706 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
8708 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
8709 goto return_build_binary_op;
8716 /* Subtraction of two similar pointers.
8717 We must subtract them as integers, then divide by object size. */
8718 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
8719 && comp_target_types (type0, type1))
8721 ret = pointer_diff (op0, op1);
8722 goto return_build_binary_op;
8724 /* Handle pointer minus int. Just like pointer plus int. */
8725 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8727 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
8728 goto return_build_binary_op;
8738 case TRUNC_DIV_EXPR:
8740 case FLOOR_DIV_EXPR:
8741 case ROUND_DIV_EXPR:
8742 case EXACT_DIV_EXPR:
8743 warn_for_div_by_zero (location, op1);
8745 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8746 || code0 == FIXED_POINT_TYPE
8747 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8748 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8749 || code1 == FIXED_POINT_TYPE
8750 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
8752 enum tree_code tcode0 = code0, tcode1 = code1;
8754 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8755 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
8756 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
8757 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
8759 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
8760 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
8761 resultcode = RDIV_EXPR;
8763 /* Although it would be tempting to shorten always here, that
8764 loses on some targets, since the modulo instruction is
8765 undefined if the quotient can't be represented in the
8766 computation mode. We shorten only if unsigned or if
8767 dividing by something we know != -1. */
8768 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8769 || (TREE_CODE (op1) == INTEGER_CST
8770 && !integer_all_onesp (op1)));
8778 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8780 /* Allow vector types which are not floating point types. */
8781 else if (code0 == VECTOR_TYPE
8782 && code1 == VECTOR_TYPE
8783 && !VECTOR_FLOAT_TYPE_P (type0)
8784 && !VECTOR_FLOAT_TYPE_P (type1))
8788 case TRUNC_MOD_EXPR:
8789 case FLOOR_MOD_EXPR:
8790 warn_for_div_by_zero (location, op1);
8792 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8794 /* Although it would be tempting to shorten always here, that loses
8795 on some targets, since the modulo instruction is undefined if the
8796 quotient can't be represented in the computation mode. We shorten
8797 only if unsigned or if dividing by something we know != -1. */
8798 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8799 || (TREE_CODE (op1) == INTEGER_CST
8800 && !integer_all_onesp (op1)));
8805 case TRUTH_ANDIF_EXPR:
8806 case TRUTH_ORIF_EXPR:
8807 case TRUTH_AND_EXPR:
8809 case TRUTH_XOR_EXPR:
8810 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
8811 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8812 || code0 == FIXED_POINT_TYPE)
8813 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
8814 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8815 || code1 == FIXED_POINT_TYPE))
8817 /* Result of these operations is always an int,
8818 but that does not mean the operands should be
8819 converted to ints! */
8820 result_type = integer_type_node;
8821 op0 = c_common_truthvalue_conversion (location, op0);
8822 op1 = c_common_truthvalue_conversion (location, op1);
8825 if (code == TRUTH_ANDIF_EXPR)
8827 int_const_or_overflow = (int_operands
8828 && TREE_CODE (orig_op0) == INTEGER_CST
8829 && (op0 == truthvalue_false_node
8830 || TREE_CODE (orig_op1) == INTEGER_CST));
8831 int_const = (int_const_or_overflow
8832 && !TREE_OVERFLOW (orig_op0)
8833 && (op0 == truthvalue_false_node
8834 || !TREE_OVERFLOW (orig_op1)));
8836 else if (code == TRUTH_ORIF_EXPR)
8838 int_const_or_overflow = (int_operands
8839 && TREE_CODE (orig_op0) == INTEGER_CST
8840 && (op0 == truthvalue_true_node
8841 || TREE_CODE (orig_op1) == INTEGER_CST));
8842 int_const = (int_const_or_overflow
8843 && !TREE_OVERFLOW (orig_op0)
8844 && (op0 == truthvalue_true_node
8845 || !TREE_OVERFLOW (orig_op1)));
8849 /* Shift operations: result has same type as first operand;
8850 always convert second operand to int.
8851 Also set SHORT_SHIFT if shifting rightward. */
8854 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8855 && code1 == INTEGER_TYPE)
8857 if (TREE_CODE (op1) == INTEGER_CST)
8859 if (tree_int_cst_sgn (op1) < 0)
8862 if (skip_evaluation == 0)
8863 warning (0, "right shift count is negative");
8867 if (!integer_zerop (op1))
8870 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8873 if (skip_evaluation == 0)
8874 warning (0, "right shift count >= width of type");
8879 /* Use the type of the value to be shifted. */
8880 result_type = type0;
8881 /* Convert the shift-count to an integer, regardless of size
8882 of value being shifted. */
8883 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8884 op1 = convert (integer_type_node, op1);
8885 /* Avoid converting op1 to result_type later. */
8891 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8892 && code1 == INTEGER_TYPE)
8894 if (TREE_CODE (op1) == INTEGER_CST)
8896 if (tree_int_cst_sgn (op1) < 0)
8899 if (skip_evaluation == 0)
8900 warning (0, "left shift count is negative");
8903 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8906 if (skip_evaluation == 0)
8907 warning (0, "left shift count >= width of type");
8911 /* Use the type of the value to be shifted. */
8912 result_type = type0;
8913 /* Convert the shift-count to an integer, regardless of size
8914 of value being shifted. */
8915 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8916 op1 = convert (integer_type_node, op1);
8917 /* Avoid converting op1 to result_type later. */
8924 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
8925 warning_at (location,
8927 "comparing floating point with == or != is unsafe");
8928 /* Result of comparison is always int,
8929 but don't convert the args to int! */
8930 build_type = integer_type_node;
8931 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8932 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
8933 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8934 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
8936 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8938 tree tt0 = TREE_TYPE (type0);
8939 tree tt1 = TREE_TYPE (type1);
8940 /* Anything compares with void *. void * compares with anything.
8941 Otherwise, the targets must be compatible
8942 and both must be object or both incomplete. */
8943 if (comp_target_types (type0, type1))
8944 result_type = common_pointer_type (type0, type1);
8945 else if (VOID_TYPE_P (tt0))
8947 /* op0 != orig_op0 detects the case of something
8948 whose value is 0 but which isn't a valid null ptr const. */
8949 if (pedantic && !null_pointer_constant_p (orig_op0)
8950 && TREE_CODE (tt1) == FUNCTION_TYPE)
8951 pedwarn (location, OPT_pedantic, "ISO C forbids "
8952 "comparison of %<void *%> with function pointer");
8954 else if (VOID_TYPE_P (tt1))
8956 if (pedantic && !null_pointer_constant_p (orig_op1)
8957 && TREE_CODE (tt0) == FUNCTION_TYPE)
8958 pedwarn (location, OPT_pedantic, "ISO C forbids "
8959 "comparison of %<void *%> with function pointer");
8962 /* Avoid warning about the volatile ObjC EH puts on decls. */
8964 pedwarn (location, 0,
8965 "comparison of distinct pointer types lacks a cast");
8967 if (result_type == NULL_TREE)
8968 result_type = ptr_type_node;
8970 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8972 if (TREE_CODE (op0) == ADDR_EXPR
8973 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
8974 warning_at (location,
8975 OPT_Waddress, "the address of %qD will never be NULL",
8976 TREE_OPERAND (op0, 0));
8977 result_type = type0;
8979 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8981 if (TREE_CODE (op1) == ADDR_EXPR
8982 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
8983 warning_at (location,
8984 OPT_Waddress, "the address of %qD will never be NULL",
8985 TREE_OPERAND (op1, 0));
8986 result_type = type1;
8988 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8990 result_type = type0;
8991 pedwarn (location, 0, "comparison between pointer and integer");
8993 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8995 result_type = type1;
8996 pedwarn (location, 0, "comparison between pointer and integer");
9004 build_type = integer_type_node;
9005 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9006 || code0 == FIXED_POINT_TYPE)
9007 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9008 || code1 == FIXED_POINT_TYPE))
9010 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9012 if (comp_target_types (type0, type1))
9014 result_type = common_pointer_type (type0, type1);
9015 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9016 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9017 pedwarn (location, 0,
9018 "comparison of complete and incomplete pointers");
9019 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9020 pedwarn (location, OPT_pedantic, "ISO C forbids "
9021 "ordered comparisons of pointers to functions");
9025 result_type = ptr_type_node;
9026 pedwarn (location, 0,
9027 "comparison of distinct pointer types lacks a cast");
9030 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9032 result_type = type0;
9034 pedwarn (location, OPT_pedantic,
9035 "ordered comparison of pointer with integer zero");
9036 else if (extra_warnings)
9037 warning_at (location, OPT_Wextra,
9038 "ordered comparison of pointer with integer zero");
9040 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9042 result_type = type1;
9043 pedwarn (location, OPT_pedantic,
9044 "ordered comparison of pointer with integer zero");
9046 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9048 result_type = type0;
9049 pedwarn (location, 0, "comparison between pointer and integer");
9051 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9053 result_type = type1;
9054 pedwarn (location, 0, "comparison between pointer and integer");
9062 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9063 return error_mark_node;
9065 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9066 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9067 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9068 TREE_TYPE (type1))))
9070 binary_op_error (location, code, type0, type1);
9071 return error_mark_node;
9074 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9075 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9077 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9078 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9080 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
9082 if (shorten || common || short_compare)
9084 result_type = c_common_type (type0, type1);
9085 if (result_type == error_mark_node)
9086 return error_mark_node;
9089 /* For certain operations (which identify themselves by shorten != 0)
9090 if both args were extended from the same smaller type,
9091 do the arithmetic in that type and then extend.
9093 shorten !=0 and !=1 indicates a bitwise operation.
9094 For them, this optimization is safe only if
9095 both args are zero-extended or both are sign-extended.
9096 Otherwise, we might change the result.
9097 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9098 but calculated in (unsigned short) it would be (unsigned short)-1. */
9100 if (shorten && none_complex)
9102 final_type = result_type;
9103 result_type = shorten_binary_op (result_type, op0, op1,
9107 /* Shifts can be shortened if shifting right. */
9112 tree arg0 = get_narrower (op0, &unsigned_arg);
9114 final_type = result_type;
9116 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9117 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9119 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9120 /* We can shorten only if the shift count is less than the
9121 number of bits in the smaller type size. */
9122 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9123 /* We cannot drop an unsigned shift after sign-extension. */
9124 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9126 /* Do an unsigned shift if the operand was zero-extended. */
9128 = c_common_signed_or_unsigned_type (unsigned_arg,
9130 /* Convert value-to-be-shifted to that type. */
9131 if (TREE_TYPE (op0) != result_type)
9132 op0 = convert (result_type, op0);
9137 /* Comparison operations are shortened too but differently.
9138 They identify themselves by setting short_compare = 1. */
9142 /* Don't write &op0, etc., because that would prevent op0
9143 from being kept in a register.
9144 Instead, make copies of the our local variables and
9145 pass the copies by reference, then copy them back afterward. */
9146 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9147 enum tree_code xresultcode = resultcode;
9149 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9154 goto return_build_binary_op;
9157 op0 = xop0, op1 = xop1;
9159 resultcode = xresultcode;
9161 if (!skip_evaluation)
9163 bool op0_maybe_const = true;
9164 bool op1_maybe_const = true;
9165 tree orig_op0_folded, orig_op1_folded;
9167 if (in_late_binary_op)
9169 orig_op0_folded = orig_op0;
9170 orig_op1_folded = orig_op1;
9174 /* Fold for the sake of possible warnings, as in
9175 build_conditional_expr. This requires the
9176 "original" values to be folded, not just op0 and
9178 op0 = c_fully_fold (op0, require_constant_value,
9180 op1 = c_fully_fold (op1, require_constant_value,
9182 orig_op0_folded = c_fully_fold (orig_op0,
9183 require_constant_value,
9185 orig_op1_folded = c_fully_fold (orig_op1,
9186 require_constant_value,
9190 if (warn_sign_compare)
9191 warn_for_sign_compare (location, orig_op0_folded,
9192 orig_op1_folded, op0, op1,
9193 result_type, resultcode);
9194 if (!in_late_binary_op)
9196 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
9198 op0 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op0),
9200 C_MAYBE_CONST_EXPR_NON_CONST (op0) = !op0_maybe_const;
9202 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
9204 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
9206 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
9213 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9214 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9215 Then the expression will be built.
9216 It will be given type FINAL_TYPE if that is nonzero;
9217 otherwise, it will be given type RESULT_TYPE. */
9221 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
9222 return error_mark_node;
9227 if (TREE_TYPE (op0) != result_type)
9228 op0 = convert_and_check (result_type, op0);
9229 if (TREE_TYPE (op1) != result_type)
9230 op1 = convert_and_check (result_type, op1);
9232 /* This can happen if one operand has a vector type, and the other
9233 has a different type. */
9234 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9235 return error_mark_node;
9238 if (build_type == NULL_TREE)
9240 build_type = result_type;
9241 if (type0 != orig_type0 || type1 != orig_type1)
9243 gcc_assert (may_need_excess_precision && common);
9244 real_result_type = c_common_type (orig_type0, orig_type1);
9248 /* Treat expressions in initializers specially as they can't trap. */
9249 if (int_const_or_overflow)
9250 ret = (require_constant_value
9251 ? fold_build2_initializer (resultcode, build_type, op0, op1)
9252 : fold_build2 (resultcode, build_type, op0, op1));
9254 ret = build2 (resultcode, build_type, op0, op1);
9255 if (final_type != 0)
9256 ret = convert (final_type, ret);
9258 return_build_binary_op:
9259 gcc_assert (ret != error_mark_node);
9260 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
9262 ? note_integer_operands (ret)
9263 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
9264 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
9265 && !in_late_binary_op)
9266 ret = note_integer_operands (ret);
9267 if (real_result_type)
9268 ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret);
9269 protected_set_expr_location (ret, location);
9274 /* Convert EXPR to be a truth-value, validating its type for this
9275 purpose. LOCATION is the source location for the expression. */
9278 c_objc_common_truthvalue_conversion (location_t location, tree expr)
9280 bool int_const, int_operands;
9282 switch (TREE_CODE (TREE_TYPE (expr)))
9285 error_at (location, "used array that cannot be converted to pointer where scalar is required");
9286 return error_mark_node;
9289 error_at (location, "used struct type value where scalar is required");
9290 return error_mark_node;
9293 error_at (location, "used union type value where scalar is required");
9294 return error_mark_node;
9303 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
9304 int_operands = EXPR_INT_CONST_OPERANDS (expr);
9306 expr = remove_c_maybe_const_expr (expr);
9308 /* ??? Should we also give an error for void and vectors rather than
9309 leaving those to give errors later? */
9310 expr = c_common_truthvalue_conversion (location, expr);
9312 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
9314 if (TREE_OVERFLOW (expr))
9317 return note_integer_operands (expr);
9319 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
9320 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9325 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9329 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
9331 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
9333 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
9334 /* Executing a compound literal inside a function reinitializes
9336 if (!TREE_STATIC (decl))
9344 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9347 c_begin_omp_parallel (void)
9352 block = c_begin_compound_stmt (true);
9357 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound statement. */
9360 c_finish_omp_parallel (tree clauses, tree block)
9364 block = c_end_compound_stmt (block, true);
9366 stmt = make_node (OMP_PARALLEL);
9367 TREE_TYPE (stmt) = void_type_node;
9368 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9369 OMP_PARALLEL_BODY (stmt) = block;
9371 return add_stmt (stmt);
9374 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9377 c_begin_omp_task (void)
9382 block = c_begin_compound_stmt (true);
9387 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound statement. */
9390 c_finish_omp_task (tree clauses, tree block)
9394 block = c_end_compound_stmt (block, true);
9396 stmt = make_node (OMP_TASK);
9397 TREE_TYPE (stmt) = void_type_node;
9398 OMP_TASK_CLAUSES (stmt) = clauses;
9399 OMP_TASK_BODY (stmt) = block;
9401 return add_stmt (stmt);
9404 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9405 Remove any elements from the list that are invalid. */
9408 c_finish_omp_clauses (tree clauses)
9410 bitmap_head generic_head, firstprivate_head, lastprivate_head;
9411 tree c, t, *pc = &clauses;
9414 bitmap_obstack_initialize (NULL);
9415 bitmap_initialize (&generic_head, &bitmap_default_obstack);
9416 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
9417 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
9419 for (pc = &clauses, c = clauses; c ; c = *pc)
9421 bool remove = false;
9422 bool need_complete = false;
9423 bool need_implicitly_determined = false;
9425 switch (OMP_CLAUSE_CODE (c))
9427 case OMP_CLAUSE_SHARED:
9429 need_implicitly_determined = true;
9430 goto check_dup_generic;
9432 case OMP_CLAUSE_PRIVATE:
9434 need_complete = true;
9435 need_implicitly_determined = true;
9436 goto check_dup_generic;
9438 case OMP_CLAUSE_REDUCTION:
9440 need_implicitly_determined = true;
9441 t = OMP_CLAUSE_DECL (c);
9442 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
9443 || POINTER_TYPE_P (TREE_TYPE (t)))
9445 error ("%qE has invalid type for %<reduction%>", t);
9448 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
9450 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
9451 const char *r_name = NULL;
9468 case TRUTH_ANDIF_EXPR:
9471 case TRUTH_ORIF_EXPR:
9479 error ("%qE has invalid type for %<reduction(%s)%>",
9484 goto check_dup_generic;
9486 case OMP_CLAUSE_COPYPRIVATE:
9487 name = "copyprivate";
9488 goto check_dup_generic;
9490 case OMP_CLAUSE_COPYIN:
9492 t = OMP_CLAUSE_DECL (c);
9493 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
9495 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
9498 goto check_dup_generic;
9501 t = OMP_CLAUSE_DECL (c);
9502 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9504 error ("%qE is not a variable in clause %qs", t, name);
9507 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9508 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9509 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9511 error ("%qE appears more than once in data clauses", t);
9515 bitmap_set_bit (&generic_head, DECL_UID (t));
9518 case OMP_CLAUSE_FIRSTPRIVATE:
9519 name = "firstprivate";
9520 t = OMP_CLAUSE_DECL (c);
9521 need_complete = true;
9522 need_implicitly_determined = true;
9523 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9525 error ("%qE is not a variable in clause %<firstprivate%>", t);
9528 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9529 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9531 error ("%qE appears more than once in data clauses", t);
9535 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
9538 case OMP_CLAUSE_LASTPRIVATE:
9539 name = "lastprivate";
9540 t = OMP_CLAUSE_DECL (c);
9541 need_complete = true;
9542 need_implicitly_determined = true;
9543 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9545 error ("%qE is not a variable in clause %<lastprivate%>", t);
9548 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9549 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9551 error ("%qE appears more than once in data clauses", t);
9555 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
9559 case OMP_CLAUSE_NUM_THREADS:
9560 case OMP_CLAUSE_SCHEDULE:
9561 case OMP_CLAUSE_NOWAIT:
9562 case OMP_CLAUSE_ORDERED:
9563 case OMP_CLAUSE_DEFAULT:
9564 case OMP_CLAUSE_UNTIED:
9565 case OMP_CLAUSE_COLLAPSE:
9566 pc = &OMP_CLAUSE_CHAIN (c);
9575 t = OMP_CLAUSE_DECL (c);
9579 t = require_complete_type (t);
9580 if (t == error_mark_node)
9584 if (need_implicitly_determined)
9586 const char *share_name = NULL;
9588 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
9589 share_name = "threadprivate";
9590 else switch (c_omp_predetermined_sharing (t))
9592 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9594 case OMP_CLAUSE_DEFAULT_SHARED:
9595 share_name = "shared";
9597 case OMP_CLAUSE_DEFAULT_PRIVATE:
9598 share_name = "private";
9605 error ("%qE is predetermined %qs for %qs",
9606 t, share_name, name);
9613 *pc = OMP_CLAUSE_CHAIN (c);
9615 pc = &OMP_CLAUSE_CHAIN (c);
9618 bitmap_obstack_release (NULL);
9622 /* Make a variant type in the proper way for C/C++, propagating qualifiers
9623 down to the element type of an array. */
9626 c_build_qualified_type (tree type, int type_quals)
9628 if (type == error_mark_node)
9631 if (TREE_CODE (type) == ARRAY_TYPE)
9634 tree element_type = c_build_qualified_type (TREE_TYPE (type),
9637 /* See if we already have an identically qualified type. */
9638 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9640 if (TYPE_QUALS (strip_array_types (t)) == type_quals
9641 && TYPE_NAME (t) == TYPE_NAME (type)
9642 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
9643 && attribute_list_equal (TYPE_ATTRIBUTES (t),
9644 TYPE_ATTRIBUTES (type)))
9649 tree domain = TYPE_DOMAIN (type);
9651 t = build_variant_type_copy (type);
9652 TREE_TYPE (t) = element_type;
9654 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
9655 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
9656 SET_TYPE_STRUCTURAL_EQUALITY (t);
9657 else if (TYPE_CANONICAL (element_type) != element_type
9658 || (domain && TYPE_CANONICAL (domain) != domain))
9660 tree unqualified_canon
9661 = build_array_type (TYPE_CANONICAL (element_type),
9662 domain? TYPE_CANONICAL (domain)
9665 = c_build_qualified_type (unqualified_canon, type_quals);
9668 TYPE_CANONICAL (t) = t;
9673 /* A restrict-qualified pointer type must be a pointer to object or
9674 incomplete type. Note that the use of POINTER_TYPE_P also allows
9675 REFERENCE_TYPEs, which is appropriate for C++. */
9676 if ((type_quals & TYPE_QUAL_RESTRICT)
9677 && (!POINTER_TYPE_P (type)
9678 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
9680 error ("invalid use of %<restrict%>");
9681 type_quals &= ~TYPE_QUAL_RESTRICT;
9684 return build_qualified_type (type, type_quals);