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 void readonly_warning (tree, enum lvalue_use);
111 static int lvalue_or_else (const_tree, enum lvalue_use);
112 static int lvalue_p (const_tree);
113 static void record_maybe_used_decl (tree);
114 static int comptypes_internal (const_tree, const_tree);
116 /* Return true if EXP is a null pointer constant, false otherwise. */
119 null_pointer_constant_p (const_tree expr)
121 /* This should really operate on c_expr structures, but they aren't
122 yet available everywhere required. */
123 tree type = TREE_TYPE (expr);
124 return (TREE_CODE (expr) == INTEGER_CST
125 && !TREE_OVERFLOW (expr)
126 && integer_zerop (expr)
127 && (INTEGRAL_TYPE_P (type)
128 || (TREE_CODE (type) == POINTER_TYPE
129 && VOID_TYPE_P (TREE_TYPE (type))
130 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
133 /* EXPR may appear in an unevaluated part of an integer constant
134 expression, but not in an evaluated part. Wrap it in a
135 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
136 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
139 note_integer_operands (tree expr)
142 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
144 ret = copy_node (expr);
145 TREE_OVERFLOW (ret) = 1;
149 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
150 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
155 /* Having checked whether EXPR may appear in an unevaluated part of an
156 integer constant expression and found that it may, remove any
157 C_MAYBE_CONST_EXPR noting this fact and return the resulting
161 remove_c_maybe_const_expr (tree expr)
163 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
164 return C_MAYBE_CONST_EXPR_EXPR (expr);
169 \f/* This is a cache to hold if two types are compatible or not. */
171 struct tagged_tu_seen_cache {
172 const struct tagged_tu_seen_cache * next;
175 /* The return value of tagged_types_tu_compatible_p if we had seen
176 these two types already. */
180 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
181 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
183 /* Do `exp = require_complete_type (exp);' to make sure exp
184 does not have an incomplete type. (That includes void types.) */
187 require_complete_type (tree value)
189 tree type = TREE_TYPE (value);
191 if (value == error_mark_node || type == error_mark_node)
192 return error_mark_node;
194 /* First, detect a valid value with a complete type. */
195 if (COMPLETE_TYPE_P (type))
198 c_incomplete_type_error (value, type);
199 return error_mark_node;
202 /* Print an error message for invalid use of an incomplete type.
203 VALUE is the expression that was used (or 0 if that isn't known)
204 and TYPE is the type that was invalid. */
207 c_incomplete_type_error (const_tree value, const_tree type)
209 const char *type_code_string;
211 /* Avoid duplicate error message. */
212 if (TREE_CODE (type) == ERROR_MARK)
215 if (value != 0 && (TREE_CODE (value) == VAR_DECL
216 || TREE_CODE (value) == PARM_DECL))
217 error ("%qD has an incomplete type", value);
221 /* We must print an error message. Be clever about what it says. */
223 switch (TREE_CODE (type))
226 type_code_string = "struct";
230 type_code_string = "union";
234 type_code_string = "enum";
238 error ("invalid use of void expression");
242 if (TYPE_DOMAIN (type))
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
246 error ("invalid use of flexible array member");
249 type = TREE_TYPE (type);
252 error ("invalid use of array with unspecified bounds");
259 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
260 error ("invalid use of undefined type %<%s %E%>",
261 type_code_string, TYPE_NAME (type));
263 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
264 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
268 /* Given a type, apply default promotions wrt unnamed function
269 arguments and return the new type. */
272 c_type_promotes_to (tree type)
274 if (TYPE_MAIN_VARIANT (type) == float_type_node)
275 return double_type_node;
277 if (c_promoting_integer_type_p (type))
279 /* Preserve unsignedness if not really getting any wider. */
280 if (TYPE_UNSIGNED (type)
281 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
282 return unsigned_type_node;
283 return integer_type_node;
289 /* Return a variant of TYPE which has all the type qualifiers of LIKE
290 as well as those of TYPE. */
293 qualify_type (tree type, tree like)
295 return c_build_qualified_type (type,
296 TYPE_QUALS (type) | TYPE_QUALS (like));
299 /* Return true iff the given tree T is a variable length array. */
302 c_vla_type_p (const_tree t)
304 if (TREE_CODE (t) == ARRAY_TYPE
305 && C_TYPE_VARIABLE_SIZE (t))
310 /* Return the composite type of two compatible types.
312 We assume that comptypes has already been done and returned
313 nonzero; if that isn't so, this may crash. In particular, we
314 assume that qualifiers match. */
317 composite_type (tree t1, tree t2)
319 enum tree_code code1;
320 enum tree_code code2;
323 /* Save time if the two types are the same. */
325 if (t1 == t2) return t1;
327 /* If one type is nonsense, use the other. */
328 if (t1 == error_mark_node)
330 if (t2 == error_mark_node)
333 code1 = TREE_CODE (t1);
334 code2 = TREE_CODE (t2);
336 /* Merge the attributes. */
337 attributes = targetm.merge_type_attributes (t1, t2);
339 /* If one is an enumerated type and the other is the compatible
340 integer type, the composite type might be either of the two
341 (DR#013 question 3). For consistency, use the enumerated type as
342 the composite type. */
344 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
346 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
349 gcc_assert (code1 == code2);
354 /* For two pointers, do this recursively on the target type. */
356 tree pointed_to_1 = TREE_TYPE (t1);
357 tree pointed_to_2 = TREE_TYPE (t2);
358 tree target = composite_type (pointed_to_1, pointed_to_2);
359 t1 = build_pointer_type (target);
360 t1 = build_type_attribute_variant (t1, attributes);
361 return qualify_type (t1, t2);
366 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
369 tree d1 = TYPE_DOMAIN (t1);
370 tree d2 = TYPE_DOMAIN (t2);
371 bool d1_variable, d2_variable;
372 bool d1_zero, d2_zero;
373 bool t1_complete, t2_complete;
375 /* We should not have any type quals on arrays at all. */
376 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
378 t1_complete = COMPLETE_TYPE_P (t1);
379 t2_complete = COMPLETE_TYPE_P (t2);
381 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
382 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
384 d1_variable = (!d1_zero
385 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
386 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
387 d2_variable = (!d2_zero
388 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
389 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
390 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
391 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
393 /* Save space: see if the result is identical to one of the args. */
394 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
395 && (d2_variable || d2_zero || !d1_variable))
396 return build_type_attribute_variant (t1, attributes);
397 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
398 && (d1_variable || d1_zero || !d2_variable))
399 return build_type_attribute_variant (t2, attributes);
401 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
402 return build_type_attribute_variant (t1, attributes);
403 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
404 return build_type_attribute_variant (t2, attributes);
406 /* Merge the element types, and have a size if either arg has
407 one. We may have qualifiers on the element types. To set
408 up TYPE_MAIN_VARIANT correctly, we need to form the
409 composite of the unqualified types and add the qualifiers
411 quals = TYPE_QUALS (strip_array_types (elt));
412 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
413 t1 = build_array_type (unqual_elt,
414 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
420 /* Ensure a composite type involving a zero-length array type
421 is a zero-length type not an incomplete type. */
422 if (d1_zero && d2_zero
423 && (t1_complete || t2_complete)
424 && !COMPLETE_TYPE_P (t1))
426 TYPE_SIZE (t1) = bitsize_zero_node;
427 TYPE_SIZE_UNIT (t1) = size_zero_node;
429 t1 = c_build_qualified_type (t1, quals);
430 return build_type_attribute_variant (t1, attributes);
436 if (attributes != NULL)
438 /* Try harder not to create a new aggregate type. */
439 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
441 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
444 return build_type_attribute_variant (t1, attributes);
447 /* Function types: prefer the one that specified arg types.
448 If both do, merge the arg types. Also merge the return types. */
450 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
451 tree p1 = TYPE_ARG_TYPES (t1);
452 tree p2 = TYPE_ARG_TYPES (t2);
457 /* Save space: see if the result is identical to one of the args. */
458 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
459 return build_type_attribute_variant (t1, attributes);
460 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
461 return build_type_attribute_variant (t2, attributes);
463 /* Simple way if one arg fails to specify argument types. */
464 if (TYPE_ARG_TYPES (t1) == 0)
466 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
467 t1 = build_type_attribute_variant (t1, attributes);
468 return qualify_type (t1, t2);
470 if (TYPE_ARG_TYPES (t2) == 0)
472 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
473 t1 = build_type_attribute_variant (t1, attributes);
474 return qualify_type (t1, t2);
477 /* If both args specify argument types, we must merge the two
478 lists, argument by argument. */
479 /* Tell global_bindings_p to return false so that variable_size
480 doesn't die on VLAs in parameter types. */
481 c_override_global_bindings_to_false = true;
483 len = list_length (p1);
486 for (i = 0; i < len; i++)
487 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
492 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
494 /* A null type means arg type is not specified.
495 Take whatever the other function type has. */
496 if (TREE_VALUE (p1) == 0)
498 TREE_VALUE (n) = TREE_VALUE (p2);
501 if (TREE_VALUE (p2) == 0)
503 TREE_VALUE (n) = TREE_VALUE (p1);
507 /* Given wait (union {union wait *u; int *i} *)
508 and wait (union wait *),
509 prefer union wait * as type of parm. */
510 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
511 && TREE_VALUE (p1) != TREE_VALUE (p2))
514 tree mv2 = TREE_VALUE (p2);
515 if (mv2 && mv2 != error_mark_node
516 && TREE_CODE (mv2) != ARRAY_TYPE)
517 mv2 = TYPE_MAIN_VARIANT (mv2);
518 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
519 memb; memb = TREE_CHAIN (memb))
521 tree mv3 = TREE_TYPE (memb);
522 if (mv3 && mv3 != error_mark_node
523 && TREE_CODE (mv3) != ARRAY_TYPE)
524 mv3 = TYPE_MAIN_VARIANT (mv3);
525 if (comptypes (mv3, mv2))
527 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
529 pedwarn (input_location, OPT_pedantic,
530 "function types not truly compatible in ISO C");
535 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
536 && TREE_VALUE (p2) != TREE_VALUE (p1))
539 tree mv1 = TREE_VALUE (p1);
540 if (mv1 && mv1 != error_mark_node
541 && TREE_CODE (mv1) != ARRAY_TYPE)
542 mv1 = TYPE_MAIN_VARIANT (mv1);
543 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
544 memb; memb = TREE_CHAIN (memb))
546 tree mv3 = TREE_TYPE (memb);
547 if (mv3 && mv3 != error_mark_node
548 && TREE_CODE (mv3) != ARRAY_TYPE)
549 mv3 = TYPE_MAIN_VARIANT (mv3);
550 if (comptypes (mv3, mv1))
552 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
554 pedwarn (input_location, OPT_pedantic,
555 "function types not truly compatible in ISO C");
560 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
564 c_override_global_bindings_to_false = false;
565 t1 = build_function_type (valtype, newargs);
566 t1 = qualify_type (t1, t2);
567 /* ... falls through ... */
571 return build_type_attribute_variant (t1, attributes);
576 /* Return the type of a conditional expression between pointers to
577 possibly differently qualified versions of compatible types.
579 We assume that comp_target_types has already been done and returned
580 nonzero; if that isn't so, this may crash. */
583 common_pointer_type (tree t1, tree t2)
586 tree pointed_to_1, mv1;
587 tree pointed_to_2, mv2;
589 unsigned target_quals;
591 /* Save time if the two types are the same. */
593 if (t1 == t2) return t1;
595 /* If one type is nonsense, use the other. */
596 if (t1 == error_mark_node)
598 if (t2 == error_mark_node)
601 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
602 && TREE_CODE (t2) == POINTER_TYPE);
604 /* Merge the attributes. */
605 attributes = targetm.merge_type_attributes (t1, t2);
607 /* Find the composite type of the target types, and combine the
608 qualifiers of the two types' targets. Do not lose qualifiers on
609 array element types by taking the TYPE_MAIN_VARIANT. */
610 mv1 = pointed_to_1 = TREE_TYPE (t1);
611 mv2 = pointed_to_2 = TREE_TYPE (t2);
612 if (TREE_CODE (mv1) != ARRAY_TYPE)
613 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
614 if (TREE_CODE (mv2) != ARRAY_TYPE)
615 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
616 target = composite_type (mv1, mv2);
618 /* For function types do not merge const qualifiers, but drop them
619 if used inconsistently. The middle-end uses these to mark const
620 and noreturn functions. */
621 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
622 target_quals = TYPE_QUALS (pointed_to_1) & TYPE_QUALS (pointed_to_2);
624 target_quals = TYPE_QUALS (pointed_to_1) | TYPE_QUALS (pointed_to_2);
625 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
626 return build_type_attribute_variant (t1, attributes);
629 /* Return the common type for two arithmetic types under the usual
630 arithmetic conversions. The default conversions have already been
631 applied, and enumerated types converted to their compatible integer
632 types. The resulting type is unqualified and has no attributes.
634 This is the type for the result of most arithmetic operations
635 if the operands have the given two types. */
638 c_common_type (tree t1, tree t2)
640 enum tree_code code1;
641 enum tree_code code2;
643 /* If one type is nonsense, use the other. */
644 if (t1 == error_mark_node)
646 if (t2 == error_mark_node)
649 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
650 t1 = TYPE_MAIN_VARIANT (t1);
652 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
653 t2 = TYPE_MAIN_VARIANT (t2);
655 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
656 t1 = build_type_attribute_variant (t1, NULL_TREE);
658 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
659 t2 = build_type_attribute_variant (t2, NULL_TREE);
661 /* Save time if the two types are the same. */
663 if (t1 == t2) return t1;
665 code1 = TREE_CODE (t1);
666 code2 = TREE_CODE (t2);
668 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
669 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
670 || code1 == INTEGER_TYPE);
671 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
672 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
673 || code2 == INTEGER_TYPE);
675 /* When one operand is a decimal float type, the other operand cannot be
676 a generic float type or a complex type. We also disallow vector types
678 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
679 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
681 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
683 error ("can%'t mix operands of decimal float and vector types");
684 return error_mark_node;
686 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
688 error ("can%'t mix operands of decimal float and complex types");
689 return error_mark_node;
691 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
693 error ("can%'t mix operands of decimal float and other float types");
694 return error_mark_node;
698 /* If one type is a vector type, return that type. (How the usual
699 arithmetic conversions apply to the vector types extension is not
700 precisely specified.) */
701 if (code1 == VECTOR_TYPE)
704 if (code2 == VECTOR_TYPE)
707 /* If one type is complex, form the common type of the non-complex
708 components, then make that complex. Use T1 or T2 if it is the
710 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
712 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
713 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
714 tree subtype = c_common_type (subtype1, subtype2);
716 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
718 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
721 return build_complex_type (subtype);
724 /* If only one is real, use it as the result. */
726 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
729 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
732 /* If both are real and either are decimal floating point types, use
733 the decimal floating point type with the greater precision. */
735 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
737 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
738 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
739 return dfloat128_type_node;
740 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
741 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
742 return dfloat64_type_node;
743 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
744 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
745 return dfloat32_type_node;
748 /* Deal with fixed-point types. */
749 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
751 unsigned int unsignedp = 0, satp = 0;
752 enum machine_mode m1, m2;
753 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
758 /* If one input type is saturating, the result type is saturating. */
759 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
762 /* If both fixed-point types are unsigned, the result type is unsigned.
763 When mixing fixed-point and integer types, follow the sign of the
765 Otherwise, the result type is signed. */
766 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
767 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
768 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
769 && TYPE_UNSIGNED (t1))
770 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
771 && TYPE_UNSIGNED (t2)))
774 /* The result type is signed. */
777 /* If the input type is unsigned, we need to convert to the
779 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
781 enum mode_class mclass = (enum mode_class) 0;
782 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
784 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
788 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
790 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
792 enum mode_class mclass = (enum mode_class) 0;
793 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
795 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
799 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
803 if (code1 == FIXED_POINT_TYPE)
805 fbit1 = GET_MODE_FBIT (m1);
806 ibit1 = GET_MODE_IBIT (m1);
811 /* Signed integers need to subtract one sign bit. */
812 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
815 if (code2 == FIXED_POINT_TYPE)
817 fbit2 = GET_MODE_FBIT (m2);
818 ibit2 = GET_MODE_IBIT (m2);
823 /* Signed integers need to subtract one sign bit. */
824 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
827 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
828 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
829 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
833 /* Both real or both integers; use the one with greater precision. */
835 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
837 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
840 /* Same precision. Prefer long longs to longs to ints when the
841 same precision, following the C99 rules on integer type rank
842 (which are equivalent to the C90 rules for C90 types). */
844 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
845 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
846 return long_long_unsigned_type_node;
848 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
849 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
851 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
852 return long_long_unsigned_type_node;
854 return long_long_integer_type_node;
857 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
858 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
859 return long_unsigned_type_node;
861 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
862 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
864 /* But preserve unsignedness from the other type,
865 since long cannot hold all the values of an unsigned int. */
866 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
867 return long_unsigned_type_node;
869 return long_integer_type_node;
872 /* Likewise, prefer long double to double even if same size. */
873 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
874 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
875 return long_double_type_node;
877 /* Otherwise prefer the unsigned one. */
879 if (TYPE_UNSIGNED (t1))
885 /* Wrapper around c_common_type that is used by c-common.c and other
886 front end optimizations that remove promotions. ENUMERAL_TYPEs
887 are allowed here and are converted to their compatible integer types.
888 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
889 preferably a non-Boolean type as the common type. */
891 common_type (tree t1, tree t2)
893 if (TREE_CODE (t1) == ENUMERAL_TYPE)
894 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
895 if (TREE_CODE (t2) == ENUMERAL_TYPE)
896 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
898 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
899 if (TREE_CODE (t1) == BOOLEAN_TYPE
900 && TREE_CODE (t2) == BOOLEAN_TYPE)
901 return boolean_type_node;
903 /* If either type is BOOLEAN_TYPE, then return the other. */
904 if (TREE_CODE (t1) == BOOLEAN_TYPE)
906 if (TREE_CODE (t2) == BOOLEAN_TYPE)
909 return c_common_type (t1, t2);
912 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
913 or various other operations. Return 2 if they are compatible
914 but a warning may be needed if you use them together. */
917 comptypes (tree type1, tree type2)
919 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
922 val = comptypes_internal (type1, type2);
923 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
928 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
929 or various other operations. Return 2 if they are compatible
930 but a warning may be needed if you use them together. This
931 differs from comptypes, in that we don't free the seen types. */
934 comptypes_internal (const_tree type1, const_tree type2)
936 const_tree t1 = type1;
937 const_tree t2 = type2;
940 /* Suppress errors caused by previously reported errors. */
942 if (t1 == t2 || !t1 || !t2
943 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
946 /* If either type is the internal version of sizetype, return the
948 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
949 && TYPE_ORIG_SIZE_TYPE (t1))
950 t1 = TYPE_ORIG_SIZE_TYPE (t1);
952 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
953 && TYPE_ORIG_SIZE_TYPE (t2))
954 t2 = TYPE_ORIG_SIZE_TYPE (t2);
957 /* Enumerated types are compatible with integer types, but this is
958 not transitive: two enumerated types in the same translation unit
959 are compatible with each other only if they are the same type. */
961 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
962 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
963 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
964 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
969 /* Different classes of types can't be compatible. */
971 if (TREE_CODE (t1) != TREE_CODE (t2))
974 /* Qualifiers must match. C99 6.7.3p9 */
976 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
979 /* Allow for two different type nodes which have essentially the same
980 definition. Note that we already checked for equality of the type
981 qualifiers (just above). */
983 if (TREE_CODE (t1) != ARRAY_TYPE
984 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
987 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
988 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
991 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
994 switch (TREE_CODE (t1))
997 /* Do not remove mode or aliasing information. */
998 if (TYPE_MODE (t1) != TYPE_MODE (t2)
999 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1001 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1002 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
1006 val = function_types_compatible_p (t1, t2);
1011 tree d1 = TYPE_DOMAIN (t1);
1012 tree d2 = TYPE_DOMAIN (t2);
1013 bool d1_variable, d2_variable;
1014 bool d1_zero, d2_zero;
1017 /* Target types must match incl. qualifiers. */
1018 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1019 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
1022 /* Sizes must match unless one is missing or variable. */
1023 if (d1 == 0 || d2 == 0 || d1 == d2)
1026 d1_zero = !TYPE_MAX_VALUE (d1);
1027 d2_zero = !TYPE_MAX_VALUE (d2);
1029 d1_variable = (!d1_zero
1030 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1031 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1032 d2_variable = (!d2_zero
1033 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1034 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1035 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1036 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1038 if (d1_variable || d2_variable)
1040 if (d1_zero && d2_zero)
1042 if (d1_zero || d2_zero
1043 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1044 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1053 if (val != 1 && !same_translation_unit_p (t1, t2))
1055 tree a1 = TYPE_ATTRIBUTES (t1);
1056 tree a2 = TYPE_ATTRIBUTES (t2);
1058 if (! attribute_list_contained (a1, a2)
1059 && ! attribute_list_contained (a2, a1))
1063 return tagged_types_tu_compatible_p (t1, t2);
1064 val = tagged_types_tu_compatible_p (t1, t2);
1069 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1070 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
1076 return attrval == 2 && val == 1 ? 2 : val;
1079 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
1080 ignoring their qualifiers. */
1083 comp_target_types (tree ttl, tree ttr)
1088 /* Do not lose qualifiers on element types of array types that are
1089 pointer targets by taking their TYPE_MAIN_VARIANT. */
1090 mvl = TREE_TYPE (ttl);
1091 mvr = TREE_TYPE (ttr);
1092 if (TREE_CODE (mvl) != ARRAY_TYPE)
1093 mvl = TYPE_MAIN_VARIANT (mvl);
1094 if (TREE_CODE (mvr) != ARRAY_TYPE)
1095 mvr = TYPE_MAIN_VARIANT (mvr);
1096 val = comptypes (mvl, mvr);
1099 pedwarn (input_location, OPT_pedantic, "types are not quite compatible");
1103 /* Subroutines of `comptypes'. */
1105 /* Determine whether two trees derive from the same translation unit.
1106 If the CONTEXT chain ends in a null, that tree's context is still
1107 being parsed, so if two trees have context chains ending in null,
1108 they're in the same translation unit. */
1110 same_translation_unit_p (const_tree t1, const_tree t2)
1112 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1113 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1115 case tcc_declaration:
1116 t1 = DECL_CONTEXT (t1); break;
1118 t1 = TYPE_CONTEXT (t1); break;
1119 case tcc_exceptional:
1120 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1121 default: gcc_unreachable ();
1124 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1125 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1127 case tcc_declaration:
1128 t2 = DECL_CONTEXT (t2); break;
1130 t2 = TYPE_CONTEXT (t2); break;
1131 case tcc_exceptional:
1132 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1133 default: gcc_unreachable ();
1139 /* Allocate the seen two types, assuming that they are compatible. */
1141 static struct tagged_tu_seen_cache *
1142 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1144 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1145 tu->next = tagged_tu_seen_base;
1149 tagged_tu_seen_base = tu;
1151 /* The C standard says that two structures in different translation
1152 units are compatible with each other only if the types of their
1153 fields are compatible (among other things). We assume that they
1154 are compatible until proven otherwise when building the cache.
1155 An example where this can occur is:
1160 If we are comparing this against a similar struct in another TU,
1161 and did not assume they were compatible, we end up with an infinite
1167 /* Free the seen types until we get to TU_TIL. */
1170 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1172 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1173 while (tu != tu_til)
1175 const struct tagged_tu_seen_cache *const tu1
1176 = (const struct tagged_tu_seen_cache *) tu;
1178 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1180 tagged_tu_seen_base = tu_til;
1183 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1184 compatible. If the two types are not the same (which has been
1185 checked earlier), this can only happen when multiple translation
1186 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1190 tagged_types_tu_compatible_p (const_tree t1, const_tree t2)
1193 bool needs_warning = false;
1195 /* We have to verify that the tags of the types are the same. This
1196 is harder than it looks because this may be a typedef, so we have
1197 to go look at the original type. It may even be a typedef of a
1199 In the case of compiler-created builtin structs the TYPE_DECL
1200 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1201 while (TYPE_NAME (t1)
1202 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1203 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1204 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1206 while (TYPE_NAME (t2)
1207 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1208 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1209 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1211 /* C90 didn't have the requirement that the two tags be the same. */
1212 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1215 /* C90 didn't say what happened if one or both of the types were
1216 incomplete; we choose to follow C99 rules here, which is that they
1218 if (TYPE_SIZE (t1) == NULL
1219 || TYPE_SIZE (t2) == NULL)
1223 const struct tagged_tu_seen_cache * tts_i;
1224 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1225 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1229 switch (TREE_CODE (t1))
1233 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1234 /* Speed up the case where the type values are in the same order. */
1235 tree tv1 = TYPE_VALUES (t1);
1236 tree tv2 = TYPE_VALUES (t2);
1243 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1245 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1247 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1254 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1258 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1264 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1270 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1272 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1274 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1285 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1286 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1292 /* Speed up the common case where the fields are in the same order. */
1293 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1294 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1298 if (DECL_NAME (s1) != DECL_NAME (s2))
1300 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1302 if (result != 1 && !DECL_NAME (s1))
1310 needs_warning = true;
1312 if (TREE_CODE (s1) == FIELD_DECL
1313 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1314 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1322 tu->val = needs_warning ? 2 : 1;
1326 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1330 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1331 if (DECL_NAME (s1) == DECL_NAME (s2))
1335 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1337 if (result != 1 && !DECL_NAME (s1))
1345 needs_warning = true;
1347 if (TREE_CODE (s1) == FIELD_DECL
1348 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1349 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1361 tu->val = needs_warning ? 2 : 10;
1367 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1369 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1371 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1374 if (TREE_CODE (s1) != TREE_CODE (s2)
1375 || DECL_NAME (s1) != DECL_NAME (s2))
1377 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1381 needs_warning = true;
1383 if (TREE_CODE (s1) == FIELD_DECL
1384 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1385 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1391 tu->val = needs_warning ? 2 : 1;
1400 /* Return 1 if two function types F1 and F2 are compatible.
1401 If either type specifies no argument types,
1402 the other must specify a fixed number of self-promoting arg types.
1403 Otherwise, if one type specifies only the number of arguments,
1404 the other must specify that number of self-promoting arg types.
1405 Otherwise, the argument types must match. */
1408 function_types_compatible_p (const_tree f1, const_tree f2)
1411 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1416 ret1 = TREE_TYPE (f1);
1417 ret2 = TREE_TYPE (f2);
1419 /* 'volatile' qualifiers on a function's return type used to mean
1420 the function is noreturn. */
1421 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1422 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1423 if (TYPE_VOLATILE (ret1))
1424 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1425 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1426 if (TYPE_VOLATILE (ret2))
1427 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1428 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1429 val = comptypes_internal (ret1, ret2);
1433 args1 = TYPE_ARG_TYPES (f1);
1434 args2 = TYPE_ARG_TYPES (f2);
1436 /* An unspecified parmlist matches any specified parmlist
1437 whose argument types don't need default promotions. */
1441 if (!self_promoting_args_p (args2))
1443 /* If one of these types comes from a non-prototype fn definition,
1444 compare that with the other type's arglist.
1445 If they don't match, ask for a warning (but no error). */
1446 if (TYPE_ACTUAL_ARG_TYPES (f1)
1447 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1453 if (!self_promoting_args_p (args1))
1455 if (TYPE_ACTUAL_ARG_TYPES (f2)
1456 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1461 /* Both types have argument lists: compare them and propagate results. */
1462 val1 = type_lists_compatible_p (args1, args2);
1463 return val1 != 1 ? val1 : val;
1466 /* Check two lists of types for compatibility,
1467 returning 0 for incompatible, 1 for compatible,
1468 or 2 for compatible with warning. */
1471 type_lists_compatible_p (const_tree args1, const_tree args2)
1473 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1479 tree a1, mv1, a2, mv2;
1480 if (args1 == 0 && args2 == 0)
1482 /* If one list is shorter than the other,
1483 they fail to match. */
1484 if (args1 == 0 || args2 == 0)
1486 mv1 = a1 = TREE_VALUE (args1);
1487 mv2 = a2 = TREE_VALUE (args2);
1488 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1489 mv1 = TYPE_MAIN_VARIANT (mv1);
1490 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1491 mv2 = TYPE_MAIN_VARIANT (mv2);
1492 /* A null pointer instead of a type
1493 means there is supposed to be an argument
1494 but nothing is specified about what type it has.
1495 So match anything that self-promotes. */
1498 if (c_type_promotes_to (a2) != a2)
1503 if (c_type_promotes_to (a1) != a1)
1506 /* If one of the lists has an error marker, ignore this arg. */
1507 else if (TREE_CODE (a1) == ERROR_MARK
1508 || TREE_CODE (a2) == ERROR_MARK)
1510 else if (!(newval = comptypes_internal (mv1, mv2)))
1512 /* Allow wait (union {union wait *u; int *i} *)
1513 and wait (union wait *) to be compatible. */
1514 if (TREE_CODE (a1) == UNION_TYPE
1515 && (TYPE_NAME (a1) == 0
1516 || TYPE_TRANSPARENT_UNION (a1))
1517 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1518 && tree_int_cst_equal (TYPE_SIZE (a1),
1522 for (memb = TYPE_FIELDS (a1);
1523 memb; memb = TREE_CHAIN (memb))
1525 tree mv3 = TREE_TYPE (memb);
1526 if (mv3 && mv3 != error_mark_node
1527 && TREE_CODE (mv3) != ARRAY_TYPE)
1528 mv3 = TYPE_MAIN_VARIANT (mv3);
1529 if (comptypes_internal (mv3, mv2))
1535 else if (TREE_CODE (a2) == UNION_TYPE
1536 && (TYPE_NAME (a2) == 0
1537 || TYPE_TRANSPARENT_UNION (a2))
1538 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1539 && tree_int_cst_equal (TYPE_SIZE (a2),
1543 for (memb = TYPE_FIELDS (a2);
1544 memb; memb = TREE_CHAIN (memb))
1546 tree mv3 = TREE_TYPE (memb);
1547 if (mv3 && mv3 != error_mark_node
1548 && TREE_CODE (mv3) != ARRAY_TYPE)
1549 mv3 = TYPE_MAIN_VARIANT (mv3);
1550 if (comptypes_internal (mv3, mv1))
1560 /* comptypes said ok, but record if it said to warn. */
1564 args1 = TREE_CHAIN (args1);
1565 args2 = TREE_CHAIN (args2);
1569 /* Compute the size to increment a pointer by. */
1572 c_size_in_bytes (const_tree type)
1574 enum tree_code code = TREE_CODE (type);
1576 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1577 return size_one_node;
1579 if (!COMPLETE_OR_VOID_TYPE_P (type))
1581 error ("arithmetic on pointer to an incomplete type");
1582 return size_one_node;
1585 /* Convert in case a char is more than one unit. */
1586 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1587 size_int (TYPE_PRECISION (char_type_node)
1591 /* Return either DECL or its known constant value (if it has one). */
1594 decl_constant_value (tree decl)
1596 if (/* Don't change a variable array bound or initial value to a constant
1597 in a place where a variable is invalid. Note that DECL_INITIAL
1598 isn't valid for a PARM_DECL. */
1599 current_function_decl != 0
1600 && TREE_CODE (decl) != PARM_DECL
1601 && !TREE_THIS_VOLATILE (decl)
1602 && TREE_READONLY (decl)
1603 && DECL_INITIAL (decl) != 0
1604 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1605 /* This is invalid if initial value is not constant.
1606 If it has either a function call, a memory reference,
1607 or a variable, then re-evaluating it could give different results. */
1608 && TREE_CONSTANT (DECL_INITIAL (decl))
1609 /* Check for cases where this is sub-optimal, even though valid. */
1610 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1611 return DECL_INITIAL (decl);
1615 /* Convert the array expression EXP to a pointer. */
1617 array_to_pointer_conversion (tree exp)
1619 tree orig_exp = exp;
1620 tree type = TREE_TYPE (exp);
1622 tree restype = TREE_TYPE (type);
1625 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1627 STRIP_TYPE_NOPS (exp);
1629 if (TREE_NO_WARNING (orig_exp))
1630 TREE_NO_WARNING (exp) = 1;
1632 ptrtype = build_pointer_type (restype);
1634 if (TREE_CODE (exp) == INDIRECT_REF)
1635 return convert (ptrtype, TREE_OPERAND (exp, 0));
1637 if (TREE_CODE (exp) == VAR_DECL)
1639 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1640 ADDR_EXPR because it's the best way of representing what
1641 happens in C when we take the address of an array and place
1642 it in a pointer to the element type. */
1643 adr = build1 (ADDR_EXPR, ptrtype, exp);
1644 if (!c_mark_addressable (exp))
1645 return error_mark_node;
1646 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1650 /* This way is better for a COMPONENT_REF since it can
1651 simplify the offset for a component. */
1652 adr = build_unary_op (EXPR_LOCATION (exp), ADDR_EXPR, exp, 1);
1653 return convert (ptrtype, adr);
1656 /* Convert the function expression EXP to a pointer. */
1658 function_to_pointer_conversion (tree exp)
1660 tree orig_exp = exp;
1662 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1664 STRIP_TYPE_NOPS (exp);
1666 if (TREE_NO_WARNING (orig_exp))
1667 TREE_NO_WARNING (exp) = 1;
1669 return build_unary_op (EXPR_LOCATION (exp), ADDR_EXPR, exp, 0);
1672 /* Perform the default conversion of arrays and functions to pointers.
1673 Return the result of converting EXP. For any other expression, just
1677 default_function_array_conversion (struct c_expr exp)
1679 tree orig_exp = exp.value;
1680 tree type = TREE_TYPE (exp.value);
1681 enum tree_code code = TREE_CODE (type);
1687 bool not_lvalue = false;
1688 bool lvalue_array_p;
1690 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1691 || CONVERT_EXPR_P (exp.value))
1692 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1694 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1696 exp.value = TREE_OPERAND (exp.value, 0);
1699 if (TREE_NO_WARNING (orig_exp))
1700 TREE_NO_WARNING (exp.value) = 1;
1702 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1703 if (!flag_isoc99 && !lvalue_array_p)
1705 /* Before C99, non-lvalue arrays do not decay to pointers.
1706 Normally, using such an array would be invalid; but it can
1707 be used correctly inside sizeof or as a statement expression.
1708 Thus, do not give an error here; an error will result later. */
1712 exp.value = array_to_pointer_conversion (exp.value);
1716 exp.value = function_to_pointer_conversion (exp.value);
1726 /* EXP is an expression of integer type. Apply the integer promotions
1727 to it and return the promoted value. */
1730 perform_integral_promotions (tree exp)
1732 tree type = TREE_TYPE (exp);
1733 enum tree_code code = TREE_CODE (type);
1735 gcc_assert (INTEGRAL_TYPE_P (type));
1737 /* Normally convert enums to int,
1738 but convert wide enums to something wider. */
1739 if (code == ENUMERAL_TYPE)
1741 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1742 TYPE_PRECISION (integer_type_node)),
1743 ((TYPE_PRECISION (type)
1744 >= TYPE_PRECISION (integer_type_node))
1745 && TYPE_UNSIGNED (type)));
1747 return convert (type, exp);
1750 /* ??? This should no longer be needed now bit-fields have their
1752 if (TREE_CODE (exp) == COMPONENT_REF
1753 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1754 /* If it's thinner than an int, promote it like a
1755 c_promoting_integer_type_p, otherwise leave it alone. */
1756 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1757 TYPE_PRECISION (integer_type_node)))
1758 return convert (integer_type_node, exp);
1760 if (c_promoting_integer_type_p (type))
1762 /* Preserve unsignedness if not really getting any wider. */
1763 if (TYPE_UNSIGNED (type)
1764 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1765 return convert (unsigned_type_node, exp);
1767 return convert (integer_type_node, exp);
1774 /* Perform default promotions for C data used in expressions.
1775 Enumeral types or short or char are converted to int.
1776 In addition, manifest constants symbols are replaced by their values. */
1779 default_conversion (tree exp)
1782 tree type = TREE_TYPE (exp);
1783 enum tree_code code = TREE_CODE (type);
1785 /* Functions and arrays have been converted during parsing. */
1786 gcc_assert (code != FUNCTION_TYPE);
1787 if (code == ARRAY_TYPE)
1790 /* Constants can be used directly unless they're not loadable. */
1791 if (TREE_CODE (exp) == CONST_DECL)
1792 exp = DECL_INITIAL (exp);
1794 /* Strip no-op conversions. */
1796 STRIP_TYPE_NOPS (exp);
1798 if (TREE_NO_WARNING (orig_exp))
1799 TREE_NO_WARNING (exp) = 1;
1801 if (code == VOID_TYPE)
1803 error ("void value not ignored as it ought to be");
1804 return error_mark_node;
1807 exp = require_complete_type (exp);
1808 if (exp == error_mark_node)
1809 return error_mark_node;
1811 if (INTEGRAL_TYPE_P (type))
1812 return perform_integral_promotions (exp);
1817 /* Look up COMPONENT in a structure or union DECL.
1819 If the component name is not found, returns NULL_TREE. Otherwise,
1820 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1821 stepping down the chain to the component, which is in the last
1822 TREE_VALUE of the list. Normally the list is of length one, but if
1823 the component is embedded within (nested) anonymous structures or
1824 unions, the list steps down the chain to the component. */
1827 lookup_field (tree decl, tree component)
1829 tree type = TREE_TYPE (decl);
1832 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1833 to the field elements. Use a binary search on this array to quickly
1834 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1835 will always be set for structures which have many elements. */
1837 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1840 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1842 field = TYPE_FIELDS (type);
1844 top = TYPE_LANG_SPECIFIC (type)->s->len;
1845 while (top - bot > 1)
1847 half = (top - bot + 1) >> 1;
1848 field = field_array[bot+half];
1850 if (DECL_NAME (field) == NULL_TREE)
1852 /* Step through all anon unions in linear fashion. */
1853 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1855 field = field_array[bot++];
1856 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1857 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1859 tree anon = lookup_field (field, component);
1862 return tree_cons (NULL_TREE, field, anon);
1866 /* Entire record is only anon unions. */
1870 /* Restart the binary search, with new lower bound. */
1874 if (DECL_NAME (field) == component)
1876 if (DECL_NAME (field) < component)
1882 if (DECL_NAME (field_array[bot]) == component)
1883 field = field_array[bot];
1884 else if (DECL_NAME (field) != component)
1889 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1891 if (DECL_NAME (field) == NULL_TREE
1892 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1893 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1895 tree anon = lookup_field (field, component);
1898 return tree_cons (NULL_TREE, field, anon);
1901 if (DECL_NAME (field) == component)
1905 if (field == NULL_TREE)
1909 return tree_cons (NULL_TREE, field, NULL_TREE);
1912 /* Make an expression to refer to the COMPONENT field of
1913 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1916 build_component_ref (tree datum, tree component)
1918 tree type = TREE_TYPE (datum);
1919 enum tree_code code = TREE_CODE (type);
1922 bool datum_lvalue = lvalue_p (datum);
1924 if (!objc_is_public (datum, component))
1925 return error_mark_node;
1927 /* See if there is a field or component with name COMPONENT. */
1929 if (code == RECORD_TYPE || code == UNION_TYPE)
1931 if (!COMPLETE_TYPE_P (type))
1933 c_incomplete_type_error (NULL_TREE, type);
1934 return error_mark_node;
1937 field = lookup_field (datum, component);
1941 error ("%qT has no member named %qE", type, component);
1942 return error_mark_node;
1945 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1946 This might be better solved in future the way the C++ front
1947 end does it - by giving the anonymous entities each a
1948 separate name and type, and then have build_component_ref
1949 recursively call itself. We can't do that here. */
1952 tree subdatum = TREE_VALUE (field);
1955 bool use_datum_quals;
1957 if (TREE_TYPE (subdatum) == error_mark_node)
1958 return error_mark_node;
1960 /* If this is an rvalue, it does not have qualifiers in C
1961 standard terms and we must avoid propagating such
1962 qualifiers down to a non-lvalue array that is then
1963 converted to a pointer. */
1964 use_datum_quals = (datum_lvalue
1965 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
1967 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1968 if (use_datum_quals)
1969 quals |= TYPE_QUALS (TREE_TYPE (datum));
1970 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1972 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
1974 if (TREE_READONLY (subdatum)
1975 || (use_datum_quals && TREE_READONLY (datum)))
1976 TREE_READONLY (ref) = 1;
1977 if (TREE_THIS_VOLATILE (subdatum)
1978 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
1979 TREE_THIS_VOLATILE (ref) = 1;
1981 if (TREE_DEPRECATED (subdatum))
1982 warn_deprecated_use (subdatum);
1986 field = TREE_CHAIN (field);
1992 else if (code != ERROR_MARK)
1993 error ("request for member %qE in something not a structure or union",
1996 return error_mark_node;
1999 /* Given an expression PTR for a pointer, return an expression
2000 for the value pointed to.
2001 ERRORSTRING is the name of the operator to appear in error messages.
2003 LOC is the location to use for the generated tree. */
2006 build_indirect_ref (location_t loc, tree ptr, const char *errorstring)
2008 tree pointer = default_conversion (ptr);
2009 tree type = TREE_TYPE (pointer);
2012 if (TREE_CODE (type) == POINTER_TYPE)
2014 if (CONVERT_EXPR_P (pointer)
2015 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2017 /* If a warning is issued, mark it to avoid duplicates from
2018 the backend. This only needs to be done at
2019 warn_strict_aliasing > 2. */
2020 if (warn_strict_aliasing > 2)
2021 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2022 type, TREE_OPERAND (pointer, 0)))
2023 TREE_NO_WARNING (pointer) = 1;
2026 if (TREE_CODE (pointer) == ADDR_EXPR
2027 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2028 == TREE_TYPE (type)))
2030 ref = TREE_OPERAND (pointer, 0);
2031 protected_set_expr_location (ref, loc);
2036 tree t = TREE_TYPE (type);
2038 ref = build1 (INDIRECT_REF, t, pointer);
2040 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2042 error_at (loc, "dereferencing pointer to incomplete type");
2043 return error_mark_node;
2045 if (VOID_TYPE_P (t) && skip_evaluation == 0)
2046 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2048 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2049 so that we get the proper error message if the result is used
2050 to assign to. Also, &* is supposed to be a no-op.
2051 And ANSI C seems to specify that the type of the result
2052 should be the const type. */
2053 /* A de-reference of a pointer to const is not a const. It is valid
2054 to change it via some other pointer. */
2055 TREE_READONLY (ref) = TYPE_READONLY (t);
2056 TREE_SIDE_EFFECTS (ref)
2057 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2058 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2059 protected_set_expr_location (ref, loc);
2063 else if (TREE_CODE (pointer) != ERROR_MARK)
2065 "invalid type argument of %qs (have %qT)", errorstring, type);
2066 return error_mark_node;
2069 /* This handles expressions of the form "a[i]", which denotes
2072 This is logically equivalent in C to *(a+i), but we may do it differently.
2073 If A is a variable or a member, we generate a primitive ARRAY_REF.
2074 This avoids forcing the array out of registers, and can work on
2075 arrays that are not lvalues (for example, members of structures returned
2078 LOC is the location to use for the returned expression. */
2081 build_array_ref (tree array, tree index, location_t loc)
2084 bool swapped = false;
2085 if (TREE_TYPE (array) == error_mark_node
2086 || TREE_TYPE (index) == error_mark_node)
2087 return error_mark_node;
2089 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2090 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2093 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2094 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2096 error_at (loc, "subscripted value is neither array nor pointer");
2097 return error_mark_node;
2105 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2107 error_at (loc, "array subscript is not an integer");
2108 return error_mark_node;
2111 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2113 error_at (loc, "subscripted value is pointer to function");
2114 return error_mark_node;
2117 /* ??? Existing practice has been to warn only when the char
2118 index is syntactically the index, not for char[array]. */
2120 warn_array_subscript_with_type_char (index);
2122 /* Apply default promotions *after* noticing character types. */
2123 index = default_conversion (index);
2125 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2127 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2131 /* An array that is indexed by a non-constant
2132 cannot be stored in a register; we must be able to do
2133 address arithmetic on its address.
2134 Likewise an array of elements of variable size. */
2135 if (TREE_CODE (index) != INTEGER_CST
2136 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2137 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2139 if (!c_mark_addressable (array))
2140 return error_mark_node;
2142 /* An array that is indexed by a constant value which is not within
2143 the array bounds cannot be stored in a register either; because we
2144 would get a crash in store_bit_field/extract_bit_field when trying
2145 to access a non-existent part of the register. */
2146 if (TREE_CODE (index) == INTEGER_CST
2147 && TYPE_DOMAIN (TREE_TYPE (array))
2148 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2150 if (!c_mark_addressable (array))
2151 return error_mark_node;
2157 while (TREE_CODE (foo) == COMPONENT_REF)
2158 foo = TREE_OPERAND (foo, 0);
2159 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2160 pedwarn (loc, OPT_pedantic,
2161 "ISO C forbids subscripting %<register%> array");
2162 else if (!flag_isoc99 && !lvalue_p (foo))
2163 pedwarn (loc, OPT_pedantic,
2164 "ISO C90 forbids subscripting non-lvalue array");
2167 type = TREE_TYPE (TREE_TYPE (array));
2168 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2169 /* Array ref is const/volatile if the array elements are
2170 or if the array is. */
2171 TREE_READONLY (rval)
2172 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2173 | TREE_READONLY (array));
2174 TREE_SIDE_EFFECTS (rval)
2175 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2176 | TREE_SIDE_EFFECTS (array));
2177 TREE_THIS_VOLATILE (rval)
2178 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2179 /* This was added by rms on 16 Nov 91.
2180 It fixes vol struct foo *a; a->elts[1]
2181 in an inline function.
2182 Hope it doesn't break something else. */
2183 | TREE_THIS_VOLATILE (array));
2184 ret = require_complete_type (rval);
2185 protected_set_expr_location (ret, loc);
2190 tree ar = default_conversion (array);
2192 if (ar == error_mark_node)
2195 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2196 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2198 return build_indirect_ref
2199 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2204 /* Build an external reference to identifier ID. FUN indicates
2205 whether this will be used for a function call. LOC is the source
2206 location of the identifier. This sets *TYPE to the type of the
2207 identifier, which is not the same as the type of the returned value
2208 for CONST_DECLs defined as enum constants. If the type of the
2209 identifier is not available, *TYPE is set to NULL. */
2211 build_external_ref (tree id, int fun, location_t loc, tree *type)
2214 tree decl = lookup_name (id);
2216 /* In Objective-C, an instance variable (ivar) may be preferred to
2217 whatever lookup_name() found. */
2218 decl = objc_lookup_ivar (decl, id);
2221 if (decl && decl != error_mark_node)
2224 *type = TREE_TYPE (ref);
2227 /* Implicit function declaration. */
2228 ref = implicitly_declare (id);
2229 else if (decl == error_mark_node)
2230 /* Don't complain about something that's already been
2231 complained about. */
2232 return error_mark_node;
2235 undeclared_variable (id, loc);
2236 return error_mark_node;
2239 if (TREE_TYPE (ref) == error_mark_node)
2240 return error_mark_node;
2242 if (TREE_DEPRECATED (ref))
2243 warn_deprecated_use (ref);
2245 /* Recursive call does not count as usage. */
2246 if (ref != current_function_decl)
2248 TREE_USED (ref) = 1;
2251 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2253 if (!in_sizeof && !in_typeof)
2254 C_DECL_USED (ref) = 1;
2255 else if (DECL_INITIAL (ref) == 0
2256 && DECL_EXTERNAL (ref)
2257 && !TREE_PUBLIC (ref))
2258 record_maybe_used_decl (ref);
2261 if (TREE_CODE (ref) == CONST_DECL)
2263 used_types_insert (TREE_TYPE (ref));
2264 ref = DECL_INITIAL (ref);
2265 TREE_CONSTANT (ref) = 1;
2267 else if (current_function_decl != 0
2268 && !DECL_FILE_SCOPE_P (current_function_decl)
2269 && (TREE_CODE (ref) == VAR_DECL
2270 || TREE_CODE (ref) == PARM_DECL
2271 || TREE_CODE (ref) == FUNCTION_DECL))
2273 tree context = decl_function_context (ref);
2275 if (context != 0 && context != current_function_decl)
2276 DECL_NONLOCAL (ref) = 1;
2278 /* C99 6.7.4p3: An inline definition of a function with external
2279 linkage ... shall not contain a reference to an identifier with
2280 internal linkage. */
2281 else if (current_function_decl != 0
2282 && DECL_DECLARED_INLINE_P (current_function_decl)
2283 && DECL_EXTERNAL (current_function_decl)
2284 && VAR_OR_FUNCTION_DECL_P (ref)
2285 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2286 && ! TREE_PUBLIC (ref)
2287 && DECL_CONTEXT (ref) != current_function_decl)
2288 pedwarn (loc, 0, "%qD is static but used in inline function %qD "
2289 "which is not static", ref, current_function_decl);
2294 /* Record details of decls possibly used inside sizeof or typeof. */
2295 struct maybe_used_decl
2299 /* The level seen at (in_sizeof + in_typeof). */
2301 /* The next one at this level or above, or NULL. */
2302 struct maybe_used_decl *next;
2305 static struct maybe_used_decl *maybe_used_decls;
2307 /* Record that DECL, an undefined static function reference seen
2308 inside sizeof or typeof, might be used if the operand of sizeof is
2309 a VLA type or the operand of typeof is a variably modified
2313 record_maybe_used_decl (tree decl)
2315 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2317 t->level = in_sizeof + in_typeof;
2318 t->next = maybe_used_decls;
2319 maybe_used_decls = t;
2322 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2323 USED is false, just discard them. If it is true, mark them used
2324 (if no longer inside sizeof or typeof) or move them to the next
2325 level up (if still inside sizeof or typeof). */
2328 pop_maybe_used (bool used)
2330 struct maybe_used_decl *p = maybe_used_decls;
2331 int cur_level = in_sizeof + in_typeof;
2332 while (p && p->level > cur_level)
2337 C_DECL_USED (p->decl) = 1;
2339 p->level = cur_level;
2343 if (!used || cur_level == 0)
2344 maybe_used_decls = p;
2347 /* Return the result of sizeof applied to EXPR. */
2350 c_expr_sizeof_expr (struct c_expr expr)
2353 if (expr.value == error_mark_node)
2355 ret.value = error_mark_node;
2356 ret.original_code = ERROR_MARK;
2357 ret.original_type = NULL;
2358 pop_maybe_used (false);
2362 bool expr_const_operands = true;
2363 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2364 &expr_const_operands);
2365 ret.value = c_sizeof (TREE_TYPE (folded_expr));
2366 ret.original_code = ERROR_MARK;
2367 ret.original_type = NULL;
2368 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2370 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2371 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2372 folded_expr, ret.value);
2373 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2375 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2380 /* Return the result of sizeof applied to T, a structure for the type
2381 name passed to sizeof (rather than the type itself). */
2384 c_expr_sizeof_type (struct c_type_name *t)
2388 tree type_expr = NULL_TREE;
2389 bool type_expr_const = true;
2390 type = groktypename (t, &type_expr, &type_expr_const);
2391 ret.value = c_sizeof (type);
2392 ret.original_code = ERROR_MARK;
2393 ret.original_type = NULL;
2394 if (type_expr && c_vla_type_p (type))
2396 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2397 type_expr, ret.value);
2398 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2400 pop_maybe_used (type != error_mark_node
2401 ? C_TYPE_VARIABLE_SIZE (type) : false);
2405 /* Build a function call to function FUNCTION with parameters PARAMS.
2406 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2407 TREE_VALUE of each node is a parameter-expression.
2408 FUNCTION's data type may be a function type or a pointer-to-function. */
2411 build_function_call (tree function, tree params)
2413 tree fntype, fundecl = 0;
2414 tree name = NULL_TREE, result;
2420 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2421 STRIP_TYPE_NOPS (function);
2423 /* Convert anything with function type to a pointer-to-function. */
2424 if (TREE_CODE (function) == FUNCTION_DECL)
2426 /* Implement type-directed function overloading for builtins.
2427 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2428 handle all the type checking. The result is a complete expression
2429 that implements this function call. */
2430 tem = resolve_overloaded_builtin (function, params);
2434 name = DECL_NAME (function);
2437 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2438 function = function_to_pointer_conversion (function);
2440 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2441 expressions, like those used for ObjC messenger dispatches. */
2442 function = objc_rewrite_function_call (function, params);
2444 function = c_fully_fold (function, false, NULL);
2446 fntype = TREE_TYPE (function);
2448 if (TREE_CODE (fntype) == ERROR_MARK)
2449 return error_mark_node;
2451 if (!(TREE_CODE (fntype) == POINTER_TYPE
2452 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2454 error ("called object %qE is not a function", function);
2455 return error_mark_node;
2458 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2459 current_function_returns_abnormally = 1;
2461 /* fntype now gets the type of function pointed to. */
2462 fntype = TREE_TYPE (fntype);
2464 /* Convert the parameters to the types declared in the
2465 function prototype, or apply default promotions. */
2467 nargs = list_length (params);
2468 argarray = (tree *) alloca (nargs * sizeof (tree));
2469 nargs = convert_arguments (nargs, argarray, TYPE_ARG_TYPES (fntype),
2470 params, function, fundecl);
2472 return error_mark_node;
2474 /* Check that the function is called through a compatible prototype.
2475 If it is not, replace the call by a trap, wrapped up in a compound
2476 expression if necessary. This has the nice side-effect to prevent
2477 the tree-inliner from generating invalid assignment trees which may
2478 blow up in the RTL expander later. */
2479 if (CONVERT_EXPR_P (function)
2480 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2481 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2482 && !comptypes (fntype, TREE_TYPE (tem)))
2484 tree return_type = TREE_TYPE (fntype);
2485 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2489 /* This situation leads to run-time undefined behavior. We can't,
2490 therefore, simply error unless we can prove that all possible
2491 executions of the program must execute the code. */
2492 if (warning (0, "function called through a non-compatible type"))
2493 /* We can, however, treat "undefined" any way we please.
2494 Call abort to encourage the user to fix the program. */
2495 inform (input_location, "if this code is reached, the program will abort");
2496 /* Before the abort, allow the function arguments to exit or
2498 for (i = 0; i < nargs; i++)
2499 trap = build2 (COMPOUND_EXPR, void_type_node, argarray[i], trap);
2501 if (VOID_TYPE_P (return_type))
2503 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2504 pedwarn (input_location, 0,
2505 "function with qualified void return type called");
2512 if (AGGREGATE_TYPE_P (return_type))
2513 rhs = build_compound_literal (return_type,
2514 build_constructor (return_type, 0),
2517 rhs = fold_convert (return_type, integer_zero_node);
2519 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2524 /* Check that arguments to builtin functions match the expectations. */
2526 && DECL_BUILT_IN (fundecl)
2527 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2528 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2529 return error_mark_node;
2531 /* Check that the arguments to the function are valid. */
2532 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2533 TYPE_ARG_TYPES (fntype));
2535 if (name != NULL_TREE
2536 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2538 if (require_constant_value)
2539 result = fold_build_call_array_initializer (TREE_TYPE (fntype),
2540 function, nargs, argarray);
2542 result = fold_build_call_array (TREE_TYPE (fntype),
2543 function, nargs, argarray);
2544 if (TREE_CODE (result) == NOP_EXPR
2545 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2546 STRIP_TYPE_NOPS (result);
2549 result = build_call_array (TREE_TYPE (fntype),
2550 function, nargs, argarray);
2552 if (VOID_TYPE_P (TREE_TYPE (result)))
2554 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2555 pedwarn (input_location, 0,
2556 "function with qualified void return type called");
2559 return require_complete_type (result);
2562 /* Convert the argument expressions in the list VALUES
2563 to the types in the list TYPELIST. The resulting arguments are
2564 stored in the array ARGARRAY which has size NARGS.
2566 If TYPELIST is exhausted, or when an element has NULL as its type,
2567 perform the default conversions.
2569 PARMLIST is the chain of parm decls for the function being called.
2570 It may be 0, if that info is not available.
2571 It is used only for generating error messages.
2573 FUNCTION is a tree for the called function. It is used only for
2574 error messages, where it is formatted with %qE.
2576 This is also where warnings about wrong number of args are generated.
2578 VALUES is a chain of TREE_LIST nodes with the elements of the list
2579 in the TREE_VALUE slots of those nodes.
2581 Returns the actual number of arguments processed (which may be less
2582 than NARGS in some error situations), or -1 on failure. */
2585 convert_arguments (int nargs, tree *argarray,
2586 tree typelist, tree values, tree function, tree fundecl)
2588 tree typetail, valtail;
2590 const bool type_generic = fundecl
2591 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2592 bool type_generic_remove_excess_precision = false;
2595 /* Change pointer to function to the function itself for
2597 if (TREE_CODE (function) == ADDR_EXPR
2598 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2599 function = TREE_OPERAND (function, 0);
2601 /* Handle an ObjC selector specially for diagnostics. */
2602 selector = objc_message_selector ();
2604 /* For type-generic built-in functions, determine whether excess
2605 precision should be removed (classification) or not
2608 && DECL_BUILT_IN (fundecl)
2609 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2611 switch (DECL_FUNCTION_CODE (fundecl))
2613 case BUILT_IN_ISFINITE:
2614 case BUILT_IN_ISINF:
2615 case BUILT_IN_ISINF_SIGN:
2616 case BUILT_IN_ISNAN:
2617 case BUILT_IN_ISNORMAL:
2618 case BUILT_IN_FPCLASSIFY:
2619 type_generic_remove_excess_precision = true;
2623 type_generic_remove_excess_precision = false;
2628 /* Scan the given expressions and types, producing individual
2629 converted arguments and storing them in ARGARRAY. */
2631 for (valtail = values, typetail = typelist, parmnum = 0;
2633 valtail = TREE_CHAIN (valtail), parmnum++)
2635 tree type = typetail ? TREE_VALUE (typetail) : 0;
2636 tree val = TREE_VALUE (valtail);
2637 tree valtype = TREE_TYPE (val);
2638 tree rname = function;
2639 int argnum = parmnum + 1;
2640 const char *invalid_func_diag;
2641 bool excess_precision = false;
2644 if (type == void_type_node)
2646 error ("too many arguments to function %qE", function);
2650 if (selector && argnum > 2)
2656 npc = null_pointer_constant_p (val);
2658 /* If there is excess precision and a prototype, convert once to
2659 the required type rather than converting via the semantic
2660 type. Likewise without a prototype a float value represented
2661 as long double should be converted once to double. But for
2662 type-generic classification functions excess precision must
2664 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2665 && (type || !type_generic || !type_generic_remove_excess_precision))
2667 val = TREE_OPERAND (val, 0);
2668 excess_precision = true;
2670 val = c_fully_fold (val, false, NULL);
2671 STRIP_TYPE_NOPS (val);
2673 val = require_complete_type (val);
2677 /* Formal parm type is specified by a function prototype. */
2680 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2682 error ("type of formal parameter %d is incomplete", parmnum + 1);
2687 /* Optionally warn about conversions that
2688 differ from the default conversions. */
2689 if (warn_traditional_conversion || warn_traditional)
2691 unsigned int formal_prec = TYPE_PRECISION (type);
2693 if (INTEGRAL_TYPE_P (type)
2694 && TREE_CODE (valtype) == REAL_TYPE)
2695 warning (0, "passing argument %d of %qE as integer "
2696 "rather than floating due to prototype",
2698 if (INTEGRAL_TYPE_P (type)
2699 && TREE_CODE (valtype) == COMPLEX_TYPE)
2700 warning (0, "passing argument %d of %qE as integer "
2701 "rather than complex due to prototype",
2703 else if (TREE_CODE (type) == COMPLEX_TYPE
2704 && TREE_CODE (valtype) == REAL_TYPE)
2705 warning (0, "passing argument %d of %qE as complex "
2706 "rather than floating due to prototype",
2708 else if (TREE_CODE (type) == REAL_TYPE
2709 && INTEGRAL_TYPE_P (valtype))
2710 warning (0, "passing argument %d of %qE as floating "
2711 "rather than integer due to prototype",
2713 else if (TREE_CODE (type) == COMPLEX_TYPE
2714 && INTEGRAL_TYPE_P (valtype))
2715 warning (0, "passing argument %d of %qE as complex "
2716 "rather than integer due to prototype",
2718 else if (TREE_CODE (type) == REAL_TYPE
2719 && TREE_CODE (valtype) == COMPLEX_TYPE)
2720 warning (0, "passing argument %d of %qE as floating "
2721 "rather than complex due to prototype",
2723 /* ??? At some point, messages should be written about
2724 conversions between complex types, but that's too messy
2726 else if (TREE_CODE (type) == REAL_TYPE
2727 && TREE_CODE (valtype) == REAL_TYPE)
2729 /* Warn if any argument is passed as `float',
2730 since without a prototype it would be `double'. */
2731 if (formal_prec == TYPE_PRECISION (float_type_node)
2732 && type != dfloat32_type_node)
2733 warning (0, "passing argument %d of %qE as %<float%> "
2734 "rather than %<double%> due to prototype",
2737 /* Warn if mismatch between argument and prototype
2738 for decimal float types. Warn of conversions with
2739 binary float types and of precision narrowing due to
2741 else if (type != valtype
2742 && (type == dfloat32_type_node
2743 || type == dfloat64_type_node
2744 || type == dfloat128_type_node
2745 || valtype == dfloat32_type_node
2746 || valtype == dfloat64_type_node
2747 || valtype == dfloat128_type_node)
2749 <= TYPE_PRECISION (valtype)
2750 || (type == dfloat128_type_node
2752 != dfloat64_type_node
2754 != dfloat32_type_node)))
2755 || (type == dfloat64_type_node
2757 != dfloat32_type_node))))
2758 warning (0, "passing argument %d of %qE as %qT "
2759 "rather than %qT due to prototype",
2760 argnum, rname, type, valtype);
2763 /* Detect integer changing in width or signedness.
2764 These warnings are only activated with
2765 -Wtraditional-conversion, not with -Wtraditional. */
2766 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2767 && INTEGRAL_TYPE_P (valtype))
2769 tree would_have_been = default_conversion (val);
2770 tree type1 = TREE_TYPE (would_have_been);
2772 if (TREE_CODE (type) == ENUMERAL_TYPE
2773 && (TYPE_MAIN_VARIANT (type)
2774 == TYPE_MAIN_VARIANT (valtype)))
2775 /* No warning if function asks for enum
2776 and the actual arg is that enum type. */
2778 else if (formal_prec != TYPE_PRECISION (type1))
2779 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2780 "with different width due to prototype",
2782 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2784 /* Don't complain if the formal parameter type
2785 is an enum, because we can't tell now whether
2786 the value was an enum--even the same enum. */
2787 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2789 else if (TREE_CODE (val) == INTEGER_CST
2790 && int_fits_type_p (val, type))
2791 /* Change in signedness doesn't matter
2792 if a constant value is unaffected. */
2794 /* If the value is extended from a narrower
2795 unsigned type, it doesn't matter whether we
2796 pass it as signed or unsigned; the value
2797 certainly is the same either way. */
2798 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
2799 && TYPE_UNSIGNED (valtype))
2801 else if (TYPE_UNSIGNED (type))
2802 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2803 "as unsigned due to prototype",
2806 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2807 "as signed due to prototype", argnum, rname);
2811 /* Possibly restore an EXCESS_PRECISION_EXPR for the
2812 sake of better warnings from convert_and_check. */
2813 if (excess_precision)
2814 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
2815 parmval = convert_for_assignment (type, val, ic_argpass, npc,
2819 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2820 && INTEGRAL_TYPE_P (type)
2821 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2822 parmval = default_conversion (parmval);
2824 argarray[parmnum] = parmval;
2826 else if (TREE_CODE (valtype) == REAL_TYPE
2827 && (TYPE_PRECISION (valtype)
2828 < TYPE_PRECISION (double_type_node))
2829 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
2832 argarray[parmnum] = val;
2834 /* Convert `float' to `double'. */
2835 argarray[parmnum] = convert (double_type_node, val);
2837 else if (excess_precision && !type_generic)
2838 /* A "double" argument with excess precision being passed
2839 without a prototype or in variable arguments. */
2840 argarray[parmnum] = convert (valtype, val);
2841 else if ((invalid_func_diag =
2842 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2844 error (invalid_func_diag);
2848 /* Convert `short' and `char' to full-size `int'. */
2849 argarray[parmnum] = default_conversion (val);
2852 typetail = TREE_CHAIN (typetail);
2855 gcc_assert (parmnum == nargs);
2857 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2859 error ("too few arguments to function %qE", function);
2866 /* This is the entry point used by the parser to build unary operators
2867 in the input. CODE, a tree_code, specifies the unary operator, and
2868 ARG is the operand. For unary plus, the C parser currently uses
2869 CONVERT_EXPR for code.
2871 LOC is the location to use for the tree generated.
2875 parser_build_unary_op (enum tree_code code, struct c_expr arg, location_t loc)
2877 struct c_expr result;
2879 result.value = build_unary_op (loc, code, arg.value, 0);
2880 result.original_code = code;
2881 result.original_type = NULL;
2883 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2884 overflow_warning (result.value);
2889 /* This is the entry point used by the parser to build binary operators
2890 in the input. CODE, a tree_code, specifies the binary operator, and
2891 ARG1 and ARG2 are the operands. In addition to constructing the
2892 expression, we check for operands that were written with other binary
2893 operators in a way that is likely to confuse the user.
2895 LOCATION is the location of the binary operator. */
2898 parser_build_binary_op (location_t location, enum tree_code code,
2899 struct c_expr arg1, struct c_expr arg2)
2901 struct c_expr result;
2903 enum tree_code code1 = arg1.original_code;
2904 enum tree_code code2 = arg2.original_code;
2905 tree type1 = (arg1.original_type
2906 ? arg1.original_type
2907 : TREE_TYPE (arg1.value));
2908 tree type2 = (arg2.original_type
2909 ? arg2.original_type
2910 : TREE_TYPE (arg2.value));
2912 result.value = build_binary_op (location, code,
2913 arg1.value, arg2.value, 1);
2914 result.original_code = code;
2915 result.original_type = NULL;
2917 if (TREE_CODE (result.value) == ERROR_MARK)
2920 if (location != UNKNOWN_LOCATION)
2921 protected_set_expr_location (result.value, location);
2923 /* Check for cases such as x+y<<z which users are likely
2925 if (warn_parentheses)
2926 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
2928 if (warn_logical_op)
2929 warn_logical_operator (input_location, code,
2930 code1, arg1.value, code2, arg2.value);
2932 /* Warn about comparisons against string literals, with the exception
2933 of testing for equality or inequality of a string literal with NULL. */
2934 if (code == EQ_EXPR || code == NE_EXPR)
2936 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2937 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2938 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2940 else if (TREE_CODE_CLASS (code) == tcc_comparison
2941 && (code1 == STRING_CST || code2 == STRING_CST))
2942 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2944 if (TREE_OVERFLOW_P (result.value)
2945 && !TREE_OVERFLOW_P (arg1.value)
2946 && !TREE_OVERFLOW_P (arg2.value))
2947 overflow_warning (result.value);
2949 /* Warn about comparisons of different enum types. */
2950 if (warn_enum_compare
2951 && TREE_CODE_CLASS (code) == tcc_comparison
2952 && TREE_CODE (type1) == ENUMERAL_TYPE
2953 && TREE_CODE (type2) == ENUMERAL_TYPE
2954 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
2955 warning_at (location, OPT_Wenum_compare,
2956 "comparison between %qT and %qT",
2962 /* Return a tree for the difference of pointers OP0 and OP1.
2963 The resulting tree has type int. */
2966 pointer_diff (tree op0, tree op1)
2968 tree restype = ptrdiff_type_node;
2970 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2971 tree con0, con1, lit0, lit1;
2972 tree orig_op1 = op1;
2974 if (TREE_CODE (target_type) == VOID_TYPE)
2975 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
2976 "pointer of type %<void *%> used in subtraction");
2977 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2978 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
2979 "pointer to a function used in subtraction");
2981 /* If the conversion to ptrdiff_type does anything like widening or
2982 converting a partial to an integral mode, we get a convert_expression
2983 that is in the way to do any simplifications.
2984 (fold-const.c doesn't know that the extra bits won't be needed.
2985 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2986 different mode in place.)
2987 So first try to find a common term here 'by hand'; we want to cover
2988 at least the cases that occur in legal static initializers. */
2989 if (CONVERT_EXPR_P (op0)
2990 && (TYPE_PRECISION (TREE_TYPE (op0))
2991 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2992 con0 = TREE_OPERAND (op0, 0);
2995 if (CONVERT_EXPR_P (op1)
2996 && (TYPE_PRECISION (TREE_TYPE (op1))
2997 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2998 con1 = TREE_OPERAND (op1, 0);
3002 if (TREE_CODE (con0) == PLUS_EXPR)
3004 lit0 = TREE_OPERAND (con0, 1);
3005 con0 = TREE_OPERAND (con0, 0);
3008 lit0 = integer_zero_node;
3010 if (TREE_CODE (con1) == PLUS_EXPR)
3012 lit1 = TREE_OPERAND (con1, 1);
3013 con1 = TREE_OPERAND (con1, 0);
3016 lit1 = integer_zero_node;
3018 if (operand_equal_p (con0, con1, 0))
3025 /* First do the subtraction as integers;
3026 then drop through to build the divide operator.
3027 Do not do default conversions on the minus operator
3028 in case restype is a short type. */
3030 op0 = build_binary_op (input_location,
3031 MINUS_EXPR, convert (restype, op0),
3032 convert (restype, op1), 0);
3033 /* This generates an error if op1 is pointer to incomplete type. */
3034 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3035 error ("arithmetic on pointer to an incomplete type");
3037 /* This generates an error if op0 is pointer to incomplete type. */
3038 op1 = c_size_in_bytes (target_type);
3040 /* Divide by the size, in easiest possible way. */
3041 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3044 /* Construct and perhaps optimize a tree representation
3045 for a unary operation. CODE, a tree_code, specifies the operation
3046 and XARG is the operand.
3047 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3048 the default promotions (such as from short to int).
3049 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3050 allows non-lvalues; this is only used to handle conversion of non-lvalue
3051 arrays to pointers in C99.
3053 LOCATION is the location of the operator. */
3056 build_unary_op (location_t location,
3057 enum tree_code code, tree xarg, int flag)
3059 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3062 enum tree_code typecode;
3064 tree ret = error_mark_node;
3065 tree eptype = NULL_TREE;
3066 int noconvert = flag;
3067 const char *invalid_op_diag;
3070 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3072 arg = remove_c_maybe_const_expr (arg);
3074 if (code != ADDR_EXPR)
3075 arg = require_complete_type (arg);
3077 typecode = TREE_CODE (TREE_TYPE (arg));
3078 if (typecode == ERROR_MARK)
3079 return error_mark_node;
3080 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3081 typecode = INTEGER_TYPE;
3083 if ((invalid_op_diag
3084 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3086 error_at (location, invalid_op_diag);
3087 return error_mark_node;
3090 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3092 eptype = TREE_TYPE (arg);
3093 arg = TREE_OPERAND (arg, 0);
3099 /* This is used for unary plus, because a CONVERT_EXPR
3100 is enough to prevent anybody from looking inside for
3101 associativity, but won't generate any code. */
3102 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3103 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3104 || typecode == VECTOR_TYPE))
3106 error_at (location, "wrong type argument to unary plus");
3107 return error_mark_node;
3109 else if (!noconvert)
3110 arg = default_conversion (arg);
3111 arg = non_lvalue (arg);
3115 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3116 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3117 || typecode == VECTOR_TYPE))
3119 error_at (location, "wrong type argument to unary minus");
3120 return error_mark_node;
3122 else if (!noconvert)
3123 arg = default_conversion (arg);
3127 /* ~ works on integer types and non float vectors. */
3128 if (typecode == INTEGER_TYPE
3129 || (typecode == VECTOR_TYPE
3130 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3133 arg = default_conversion (arg);
3135 else if (typecode == COMPLEX_TYPE)
3138 pedwarn (location, OPT_pedantic,
3139 "ISO C does not support %<~%> for complex conjugation");
3141 arg = default_conversion (arg);
3145 error_at (location, "wrong type argument to bit-complement");
3146 return error_mark_node;
3151 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3153 error_at (location, "wrong type argument to abs");
3154 return error_mark_node;
3156 else if (!noconvert)
3157 arg = default_conversion (arg);
3161 /* Conjugating a real value is a no-op, but allow it anyway. */
3162 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3163 || typecode == COMPLEX_TYPE))
3165 error_at (location, "wrong type argument to conjugation");
3166 return error_mark_node;
3168 else if (!noconvert)
3169 arg = default_conversion (arg);
3172 case TRUTH_NOT_EXPR:
3173 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3174 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3175 && typecode != COMPLEX_TYPE)
3178 "wrong type argument to unary exclamation mark");
3179 return error_mark_node;
3181 arg = c_objc_common_truthvalue_conversion (location, arg);
3182 ret = invert_truthvalue (arg);
3183 goto return_build_unary_op;
3186 if (TREE_CODE (arg) == COMPLEX_CST)
3187 ret = TREE_REALPART (arg);
3188 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3189 ret = fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3192 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3193 eptype = TREE_TYPE (eptype);
3194 goto return_build_unary_op;
3197 if (TREE_CODE (arg) == COMPLEX_CST)
3198 ret = TREE_IMAGPART (arg);
3199 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3200 ret = fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3202 ret = omit_one_operand (TREE_TYPE (arg), integer_zero_node, arg);
3203 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3204 eptype = TREE_TYPE (eptype);
3205 goto return_build_unary_op;
3207 case PREINCREMENT_EXPR:
3208 case POSTINCREMENT_EXPR:
3209 case PREDECREMENT_EXPR:
3210 case POSTDECREMENT_EXPR:
3212 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3214 tree inner = build_unary_op (location, code,
3215 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3216 if (inner == error_mark_node)
3217 return error_mark_node;
3218 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3219 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3220 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3221 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3222 goto return_build_unary_op;
3225 /* Complain about anything that is not a true lvalue. */
3226 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3227 || code == POSTINCREMENT_EXPR)
3230 return error_mark_node;
3232 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3233 arg = c_fully_fold (arg, false, NULL);
3235 /* Increment or decrement the real part of the value,
3236 and don't change the imaginary part. */
3237 if (typecode == COMPLEX_TYPE)
3241 pedwarn (location, OPT_pedantic,
3242 "ISO C does not support %<++%> and %<--%> on complex types");
3244 arg = stabilize_reference (arg);
3245 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3246 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3247 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3248 if (real == error_mark_node || imag == error_mark_node)
3249 return error_mark_node;
3250 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3252 goto return_build_unary_op;
3255 /* Report invalid types. */
3257 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3258 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3260 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3261 error_at (location, "wrong type argument to increment");
3263 error_at (location, "wrong type argument to decrement");
3265 return error_mark_node;
3271 argtype = TREE_TYPE (arg);
3273 /* Compute the increment. */
3275 if (typecode == POINTER_TYPE)
3277 /* If pointer target is an undefined struct,
3278 we just cannot know how to do the arithmetic. */
3279 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3281 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3283 "increment of pointer to unknown structure");
3286 "decrement of pointer to unknown structure");
3288 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3289 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3291 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3292 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3293 "wrong type argument to increment");
3295 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3296 "wrong type argument to decrement");
3299 inc = c_size_in_bytes (TREE_TYPE (argtype));
3300 inc = fold_convert (sizetype, inc);
3302 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3304 /* For signed fract types, we invert ++ to -- or
3305 -- to ++, and change inc from 1 to -1, because
3306 it is not possible to represent 1 in signed fract constants.
3307 For unsigned fract types, the result always overflows and
3308 we get an undefined (original) or the maximum value. */
3309 if (code == PREINCREMENT_EXPR)
3310 code = PREDECREMENT_EXPR;
3311 else if (code == PREDECREMENT_EXPR)
3312 code = PREINCREMENT_EXPR;
3313 else if (code == POSTINCREMENT_EXPR)
3314 code = POSTDECREMENT_EXPR;
3315 else /* code == POSTDECREMENT_EXPR */
3316 code = POSTINCREMENT_EXPR;
3318 inc = integer_minus_one_node;
3319 inc = convert (argtype, inc);
3323 inc = integer_one_node;
3324 inc = convert (argtype, inc);
3327 /* Report a read-only lvalue. */
3328 if (TYPE_READONLY (argtype))
3330 readonly_error (arg,
3331 ((code == PREINCREMENT_EXPR
3332 || code == POSTINCREMENT_EXPR)
3333 ? lv_increment : lv_decrement));
3334 return error_mark_node;
3336 else if (TREE_READONLY (arg))
3337 readonly_warning (arg,
3338 ((code == PREINCREMENT_EXPR
3339 || code == POSTINCREMENT_EXPR)
3340 ? lv_increment : lv_decrement));
3342 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3343 val = boolean_increment (code, arg);
3345 val = build2 (code, TREE_TYPE (arg), arg, inc);
3346 TREE_SIDE_EFFECTS (val) = 1;
3347 if (TREE_CODE (val) != code)
3348 TREE_NO_WARNING (val) = 1;
3350 goto return_build_unary_op;
3354 /* Note that this operation never does default_conversion. */
3356 /* The operand of unary '&' must be an lvalue (which excludes
3357 expressions of type void), or, in C99, the result of a [] or
3358 unary '*' operator. */
3359 if (VOID_TYPE_P (TREE_TYPE (arg))
3360 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3361 && (TREE_CODE (arg) != INDIRECT_REF
3363 pedwarn (location, 0, "taking address of expression of type %<void%>");
3365 /* Let &* cancel out to simplify resulting code. */
3366 if (TREE_CODE (arg) == INDIRECT_REF)
3368 /* Don't let this be an lvalue. */
3369 if (lvalue_p (TREE_OPERAND (arg, 0)))
3370 return non_lvalue (TREE_OPERAND (arg, 0));
3371 ret = TREE_OPERAND (arg, 0);
3372 goto return_build_unary_op;
3375 /* For &x[y], return x+y */
3376 if (TREE_CODE (arg) == ARRAY_REF)
3378 tree op0 = TREE_OPERAND (arg, 0);
3379 if (!c_mark_addressable (op0))
3380 return error_mark_node;
3381 return build_binary_op (location, PLUS_EXPR,
3382 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3383 ? array_to_pointer_conversion (op0)
3385 TREE_OPERAND (arg, 1), 1);
3388 /* Anything not already handled and not a true memory reference
3389 or a non-lvalue array is an error. */
3390 else if (typecode != FUNCTION_TYPE && !flag
3391 && !lvalue_or_else (arg, lv_addressof))
3392 return error_mark_node;
3394 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3396 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3398 tree inner = build_unary_op (location, code,
3399 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3400 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3401 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3402 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3403 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3404 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3405 goto return_build_unary_op;
3408 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3409 argtype = TREE_TYPE (arg);
3411 /* If the lvalue is const or volatile, merge that into the type
3412 to which the address will point. Note that you can't get a
3413 restricted pointer by taking the address of something, so we
3414 only have to deal with `const' and `volatile' here. */
3415 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3416 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3417 argtype = c_build_type_variant (argtype,
3418 TREE_READONLY (arg),
3419 TREE_THIS_VOLATILE (arg));
3421 if (!c_mark_addressable (arg))
3422 return error_mark_node;
3424 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3425 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3427 argtype = build_pointer_type (argtype);
3429 /* ??? Cope with user tricks that amount to offsetof. Delete this
3430 when we have proper support for integer constant expressions. */
3431 val = get_base_address (arg);
3432 if (val && TREE_CODE (val) == INDIRECT_REF
3433 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3435 tree op0 = fold_convert (sizetype, fold_offsetof (arg, val)), op1;
3437 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3438 ret = fold_build2 (POINTER_PLUS_EXPR, argtype, op1, op0);
3439 goto return_build_unary_op;
3442 val = build1 (ADDR_EXPR, argtype, arg);
3445 goto return_build_unary_op;
3452 argtype = TREE_TYPE (arg);
3453 if (TREE_CODE (arg) == INTEGER_CST)
3454 ret = (require_constant_value
3455 ? fold_build1_initializer (code, argtype, arg)
3456 : fold_build1 (code, argtype, arg));
3458 ret = build1 (code, argtype, arg);
3459 return_build_unary_op:
3460 gcc_assert (ret != error_mark_node);
3461 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3462 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3463 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3464 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3465 ret = note_integer_operands (ret);
3467 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3468 protected_set_expr_location (ret, location);
3472 /* Return nonzero if REF is an lvalue valid for this language.
3473 Lvalues can be assigned, unless their type has TYPE_READONLY.
3474 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3477 lvalue_p (const_tree ref)
3479 const enum tree_code code = TREE_CODE (ref);
3486 return lvalue_p (TREE_OPERAND (ref, 0));
3488 case C_MAYBE_CONST_EXPR:
3489 return lvalue_p (TREE_OPERAND (ref, 1));
3491 case COMPOUND_LITERAL_EXPR:
3501 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3502 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3505 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3512 /* Give an error for storing in something that is 'const'. */
3515 readonly_error (tree arg, enum lvalue_use use)
3517 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3519 /* Using this macro rather than (for example) arrays of messages
3520 ensures that all the format strings are checked at compile
3522 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3523 : (use == lv_increment ? (I) \
3524 : (use == lv_decrement ? (D) : (AS))))
3525 if (TREE_CODE (arg) == COMPONENT_REF)
3527 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3528 readonly_error (TREE_OPERAND (arg, 0), use);
3530 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3531 G_("increment of read-only member %qD"),
3532 G_("decrement of read-only member %qD"),
3533 G_("read-only member %qD used as %<asm%> output")),
3534 TREE_OPERAND (arg, 1));
3536 else if (TREE_CODE (arg) == VAR_DECL)
3537 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3538 G_("increment of read-only variable %qD"),
3539 G_("decrement of read-only variable %qD"),
3540 G_("read-only variable %qD used as %<asm%> output")),
3543 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3544 G_("increment of read-only location %qE"),
3545 G_("decrement of read-only location %qE"),
3546 G_("read-only location %qE used as %<asm%> output")),
3550 /* Give a warning for storing in something that is read-only in GCC
3551 terms but not const in ISO C terms. */
3554 readonly_warning (tree arg, enum lvalue_use use)
3559 warning (0, "assignment of read-only location %qE", arg);
3562 warning (0, "increment of read-only location %qE", arg);
3565 warning (0, "decrement of read-only location %qE", arg);
3574 /* Return nonzero if REF is an lvalue valid for this language;
3575 otherwise, print an error message and return zero. USE says
3576 how the lvalue is being used and so selects the error message. */
3579 lvalue_or_else (const_tree ref, enum lvalue_use use)
3581 int win = lvalue_p (ref);
3589 /* Mark EXP saying that we need to be able to take the
3590 address of it; it should not be allocated in a register.
3591 Returns true if successful. */
3594 c_mark_addressable (tree exp)
3599 switch (TREE_CODE (x))
3602 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3605 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3609 /* ... fall through ... */
3615 x = TREE_OPERAND (x, 0);
3618 case COMPOUND_LITERAL_EXPR:
3620 TREE_ADDRESSABLE (x) = 1;
3627 if (C_DECL_REGISTER (x)
3628 && DECL_NONLOCAL (x))
3630 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3633 ("global register variable %qD used in nested function", x);
3636 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3638 else if (C_DECL_REGISTER (x))
3640 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3641 error ("address of global register variable %qD requested", x);
3643 error ("address of register variable %qD requested", x);
3649 TREE_ADDRESSABLE (x) = 1;
3656 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3657 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3658 if folded to an integer constant then the unselected half may
3659 contain arbitrary operations not normally permitted in constant
3663 build_conditional_expr (tree ifexp, bool ifexp_bcp, tree op1, tree op2)
3667 enum tree_code code1;
3668 enum tree_code code2;
3669 tree result_type = NULL;
3670 tree ep_result_type = NULL;
3671 tree orig_op1 = op1, orig_op2 = op2;
3672 bool int_const, op1_int_operands, op2_int_operands, int_operands;
3673 bool ifexp_int_operands;
3677 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
3678 if (op1_int_operands)
3679 op1 = remove_c_maybe_const_expr (op1);
3680 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
3681 if (op2_int_operands)
3682 op2 = remove_c_maybe_const_expr (op2);
3683 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
3684 if (ifexp_int_operands)
3685 ifexp = remove_c_maybe_const_expr (ifexp);
3687 /* Promote both alternatives. */
3689 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3690 op1 = default_conversion (op1);
3691 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3692 op2 = default_conversion (op2);
3694 if (TREE_CODE (ifexp) == ERROR_MARK
3695 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3696 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3697 return error_mark_node;
3699 type1 = TREE_TYPE (op1);
3700 code1 = TREE_CODE (type1);
3701 type2 = TREE_TYPE (op2);
3702 code2 = TREE_CODE (type2);
3704 /* C90 does not permit non-lvalue arrays in conditional expressions.
3705 In C99 they will be pointers by now. */
3706 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3708 error ("non-lvalue array in conditional expression");
3709 return error_mark_node;
3712 objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE);
3714 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
3715 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3716 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3717 || code1 == COMPLEX_TYPE)
3718 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3719 || code2 == COMPLEX_TYPE))
3721 ep_result_type = c_common_type (type1, type2);
3722 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
3724 op1 = TREE_OPERAND (op1, 0);
3725 type1 = TREE_TYPE (op1);
3726 gcc_assert (TREE_CODE (type1) == code1);
3728 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3730 op2 = TREE_OPERAND (op2, 0);
3731 type2 = TREE_TYPE (op2);
3732 gcc_assert (TREE_CODE (type2) == code2);
3736 /* Quickly detect the usual case where op1 and op2 have the same type
3738 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3741 result_type = type1;
3743 result_type = TYPE_MAIN_VARIANT (type1);
3745 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3746 || code1 == COMPLEX_TYPE)
3747 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3748 || code2 == COMPLEX_TYPE))
3750 result_type = c_common_type (type1, type2);
3752 /* If -Wsign-compare, warn here if type1 and type2 have
3753 different signedness. We'll promote the signed to unsigned
3754 and later code won't know it used to be different.
3755 Do this check on the original types, so that explicit casts
3756 will be considered, but default promotions won't. */
3757 if (!skip_evaluation)
3759 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3760 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3762 if (unsigned_op1 ^ unsigned_op2)
3766 /* Do not warn if the result type is signed, since the
3767 signed type will only be chosen if it can represent
3768 all the values of the unsigned type. */
3769 if (!TYPE_UNSIGNED (result_type))
3773 bool op1_maybe_const = true;
3774 bool op2_maybe_const = true;
3776 /* Do not warn if the signed quantity is an
3777 unsuffixed integer literal (or some static
3778 constant expression involving such literals) and
3779 it is non-negative. This warning requires the
3780 operands to be folded for best results, so do
3781 that folding in this case even without
3782 warn_sign_compare to avoid warning options
3783 possibly affecting code generation. */
3784 op1 = c_fully_fold (op1, require_constant_value,
3786 op2 = c_fully_fold (op2, require_constant_value,
3789 if (warn_sign_compare)
3792 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3794 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3797 warning (OPT_Wsign_compare, "signed and unsigned type in conditional expression");
3799 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
3801 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
3803 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
3805 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
3807 op2 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op2),
3809 C_MAYBE_CONST_EXPR_NON_CONST (op2) = !op2_maybe_const;
3815 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3817 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
3818 pedwarn (input_location, OPT_pedantic,
3819 "ISO C forbids conditional expr with only one void side");
3820 result_type = void_type_node;
3822 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3824 if (comp_target_types (type1, type2))
3825 result_type = common_pointer_type (type1, type2);
3826 else if (null_pointer_constant_p (orig_op1))
3827 result_type = qualify_type (type2, type1);
3828 else if (null_pointer_constant_p (orig_op2))
3829 result_type = qualify_type (type1, type2);
3830 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3832 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3833 pedwarn (input_location, OPT_pedantic,
3834 "ISO C forbids conditional expr between "
3835 "%<void *%> and function pointer");
3836 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3837 TREE_TYPE (type2)));
3839 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3841 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3842 pedwarn (input_location, OPT_pedantic,
3843 "ISO C forbids conditional expr between "
3844 "%<void *%> and function pointer");
3845 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3846 TREE_TYPE (type1)));
3851 pedwarn (input_location, 0,
3852 "pointer type mismatch in conditional expression");
3853 result_type = build_pointer_type (void_type_node);
3856 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3858 if (!null_pointer_constant_p (orig_op2))
3859 pedwarn (input_location, 0,
3860 "pointer/integer type mismatch in conditional expression");
3863 op2 = null_pointer_node;
3865 result_type = type1;
3867 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3869 if (!null_pointer_constant_p (orig_op1))
3870 pedwarn (input_location, 0,
3871 "pointer/integer type mismatch in conditional expression");
3874 op1 = null_pointer_node;
3876 result_type = type2;
3881 if (flag_cond_mismatch)
3882 result_type = void_type_node;
3885 error ("type mismatch in conditional expression");
3886 return error_mark_node;
3890 /* Merge const and volatile flags of the incoming types. */
3892 = build_type_variant (result_type,
3893 TREE_READONLY (op1) || TREE_READONLY (op2),
3894 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3896 if (result_type != TREE_TYPE (op1))
3897 op1 = convert_and_check (result_type, op1);
3898 if (result_type != TREE_TYPE (op2))
3899 op2 = convert_and_check (result_type, op2);
3901 if (ifexp_bcp && ifexp == truthvalue_true_node)
3903 op2_int_operands = true;
3904 op1 = c_fully_fold (op1, require_constant_value, NULL);
3906 if (ifexp_bcp && ifexp == truthvalue_false_node)
3908 op1_int_operands = true;
3909 op2 = c_fully_fold (op2, require_constant_value, NULL);
3911 int_const = int_operands = (ifexp_int_operands
3913 && op2_int_operands);
3916 int_const = ((ifexp == truthvalue_true_node
3917 && TREE_CODE (orig_op1) == INTEGER_CST
3918 && !TREE_OVERFLOW (orig_op1))
3919 || (ifexp == truthvalue_false_node
3920 && TREE_CODE (orig_op2) == INTEGER_CST
3921 && !TREE_OVERFLOW (orig_op2)));
3923 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
3924 ret = fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3927 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
3929 ret = note_integer_operands (ret);
3932 ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret);
3937 /* Return a compound expression that performs two expressions and
3938 returns the value of the second of them. */
3941 build_compound_expr (tree expr1, tree expr2)
3943 bool expr1_int_operands, expr2_int_operands;
3944 tree eptype = NULL_TREE;
3947 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
3948 if (expr1_int_operands)
3949 expr1 = remove_c_maybe_const_expr (expr1);
3950 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
3951 if (expr2_int_operands)
3952 expr2 = remove_c_maybe_const_expr (expr2);
3954 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
3955 expr1 = TREE_OPERAND (expr1, 0);
3956 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
3958 eptype = TREE_TYPE (expr2);
3959 expr2 = TREE_OPERAND (expr2, 0);
3962 if (!TREE_SIDE_EFFECTS (expr1))
3964 /* The left-hand operand of a comma expression is like an expression
3965 statement: with -Wunused, we should warn if it doesn't have
3966 any side-effects, unless it was explicitly cast to (void). */
3967 if (warn_unused_value)
3969 if (VOID_TYPE_P (TREE_TYPE (expr1))
3970 && CONVERT_EXPR_P (expr1))
3972 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3973 && TREE_CODE (expr1) == COMPOUND_EXPR
3974 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
3975 ; /* (void) a, (void) b, c */
3977 warning (OPT_Wunused_value,
3978 "left-hand operand of comma expression has no effect");
3982 /* With -Wunused, we should also warn if the left-hand operand does have
3983 side-effects, but computes a value which is not used. For example, in
3984 `foo() + bar(), baz()' the result of the `+' operator is not used,
3985 so we should issue a warning. */
3986 else if (warn_unused_value)
3987 warn_if_unused_value (expr1, input_location);
3989 if (expr2 == error_mark_node)
3990 return error_mark_node;
3992 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3995 && expr1_int_operands
3996 && expr2_int_operands)
3997 ret = note_integer_operands (ret);
4000 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4005 /* Build an expression representing a cast to type TYPE of expression EXPR. */
4008 build_c_cast (tree type, tree expr)
4012 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4013 expr = TREE_OPERAND (expr, 0);
4017 if (type == error_mark_node || expr == error_mark_node)
4018 return error_mark_node;
4020 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4021 only in <protocol> qualifications. But when constructing cast expressions,
4022 the protocols do matter and must be kept around. */
4023 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4024 return build1 (NOP_EXPR, type, expr);
4026 type = TYPE_MAIN_VARIANT (type);
4028 if (TREE_CODE (type) == ARRAY_TYPE)
4030 error ("cast specifies array type");
4031 return error_mark_node;
4034 if (TREE_CODE (type) == FUNCTION_TYPE)
4036 error ("cast specifies function type");
4037 return error_mark_node;
4040 if (!VOID_TYPE_P (type))
4042 value = require_complete_type (value);
4043 if (value == error_mark_node)
4044 return error_mark_node;
4047 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4049 if (TREE_CODE (type) == RECORD_TYPE
4050 || TREE_CODE (type) == UNION_TYPE)
4051 pedwarn (input_location, OPT_pedantic,
4052 "ISO C forbids casting nonscalar to the same type");
4054 else if (TREE_CODE (type) == UNION_TYPE)
4058 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4059 if (TREE_TYPE (field) != error_mark_node
4060 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4061 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4068 pedwarn (input_location, OPT_pedantic,
4069 "ISO C forbids casts to union type");
4070 t = digest_init (type,
4071 build_constructor_single (type, field, value),
4073 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4076 error ("cast to union type from type not present in union");
4077 return error_mark_node;
4083 if (type == void_type_node)
4084 return build1 (CONVERT_EXPR, type, value);
4086 otype = TREE_TYPE (value);
4088 /* Optionally warn about potentially worrisome casts. */
4091 && TREE_CODE (type) == POINTER_TYPE
4092 && TREE_CODE (otype) == POINTER_TYPE)
4094 tree in_type = type;
4095 tree in_otype = otype;
4099 /* Check that the qualifiers on IN_TYPE are a superset of
4100 the qualifiers of IN_OTYPE. The outermost level of
4101 POINTER_TYPE nodes is uninteresting and we stop as soon
4102 as we hit a non-POINTER_TYPE node on either type. */
4105 in_otype = TREE_TYPE (in_otype);
4106 in_type = TREE_TYPE (in_type);
4108 /* GNU C allows cv-qualified function types. 'const'
4109 means the function is very pure, 'volatile' means it
4110 can't return. We need to warn when such qualifiers
4111 are added, not when they're taken away. */
4112 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4113 && TREE_CODE (in_type) == FUNCTION_TYPE)
4114 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
4116 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
4118 while (TREE_CODE (in_type) == POINTER_TYPE
4119 && TREE_CODE (in_otype) == POINTER_TYPE);
4122 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
4125 /* There are qualifiers present in IN_OTYPE that are not
4126 present in IN_TYPE. */
4127 warning (OPT_Wcast_qual, "cast discards qualifiers from pointer target type");
4130 /* Warn about possible alignment problems. */
4131 if (STRICT_ALIGNMENT
4132 && TREE_CODE (type) == POINTER_TYPE
4133 && TREE_CODE (otype) == POINTER_TYPE
4134 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4135 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4136 /* Don't warn about opaque types, where the actual alignment
4137 restriction is unknown. */
4138 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4139 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4140 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4141 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4142 warning (OPT_Wcast_align,
4143 "cast increases required alignment of target type");
4145 if (TREE_CODE (type) == INTEGER_TYPE
4146 && TREE_CODE (otype) == POINTER_TYPE
4147 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4148 /* Unlike conversion of integers to pointers, where the
4149 warning is disabled for converting constants because
4150 of cases such as SIG_*, warn about converting constant
4151 pointers to integers. In some cases it may cause unwanted
4152 sign extension, and a warning is appropriate. */
4153 warning (OPT_Wpointer_to_int_cast,
4154 "cast from pointer to integer of different size");
4156 if (TREE_CODE (value) == CALL_EXPR
4157 && TREE_CODE (type) != TREE_CODE (otype))
4158 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
4159 "to non-matching type %qT", otype, type);
4161 if (TREE_CODE (type) == POINTER_TYPE
4162 && TREE_CODE (otype) == INTEGER_TYPE
4163 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4164 /* Don't warn about converting any constant. */
4165 && !TREE_CONSTANT (value))
4166 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4167 "of different size");
4169 if (warn_strict_aliasing <= 2)
4170 strict_aliasing_warning (otype, type, expr);
4172 /* If pedantic, warn for conversions between function and object
4173 pointer types, except for converting a null pointer constant
4174 to function pointer type. */
4176 && TREE_CODE (type) == POINTER_TYPE
4177 && TREE_CODE (otype) == POINTER_TYPE
4178 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4179 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4180 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4181 "conversion of function pointer to object pointer type");
4184 && TREE_CODE (type) == POINTER_TYPE
4185 && TREE_CODE (otype) == POINTER_TYPE
4186 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4187 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4188 && !null_pointer_constant_p (value))
4189 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4190 "conversion of object pointer to function pointer type");
4193 value = convert (type, value);
4195 /* Ignore any integer overflow caused by the cast. */
4196 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4198 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4200 if (!TREE_OVERFLOW (value))
4202 /* Avoid clobbering a shared constant. */
4203 value = copy_node (value);
4204 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4207 else if (TREE_OVERFLOW (value))
4208 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4209 value = build_int_cst_wide (TREE_TYPE (value),
4210 TREE_INT_CST_LOW (value),
4211 TREE_INT_CST_HIGH (value));
4215 /* Don't let a cast be an lvalue. */
4217 value = non_lvalue (value);
4219 /* Don't allow the results of casting to floating-point or complex
4220 types be confused with actual constants, or casts involving
4221 integer and pointer types other than direct integer-to-integer
4222 and integer-to-pointer be confused with integer constant
4223 expressions and null pointer constants. */
4224 if (TREE_CODE (value) == REAL_CST
4225 || TREE_CODE (value) == COMPLEX_CST
4226 || (TREE_CODE (value) == INTEGER_CST
4227 && !((TREE_CODE (expr) == INTEGER_CST
4228 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4229 || TREE_CODE (expr) == REAL_CST
4230 || TREE_CODE (expr) == COMPLEX_CST)))
4231 value = build1 (NOP_EXPR, type, value);
4236 /* Interpret a cast of expression EXPR to type TYPE. */
4238 c_cast_expr (struct c_type_name *type_name, tree expr)
4241 tree type_expr = NULL_TREE;
4242 bool type_expr_const = true;
4244 int saved_wsp = warn_strict_prototypes;
4246 /* This avoids warnings about unprototyped casts on
4247 integers. E.g. "#define SIG_DFL (void(*)())0". */
4248 if (TREE_CODE (expr) == INTEGER_CST)
4249 warn_strict_prototypes = 0;
4250 type = groktypename (type_name, &type_expr, &type_expr_const);
4251 warn_strict_prototypes = saved_wsp;
4253 ret = build_c_cast (type, expr);
4256 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4257 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4262 /* Build an assignment expression of lvalue LHS from value RHS.
4263 MODIFYCODE is the code for a binary operator that we use
4264 to combine the old value of LHS with RHS to get the new value.
4265 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4267 LOCATION is the location of the MODIFYCODE operator. */
4270 build_modify_expr (location_t location,
4271 tree lhs, enum tree_code modifycode, tree rhs)
4275 tree rhs_semantic_type = NULL_TREE;
4276 tree lhstype = TREE_TYPE (lhs);
4277 tree olhstype = lhstype;
4280 /* Types that aren't fully specified cannot be used in assignments. */
4281 lhs = require_complete_type (lhs);
4283 /* Avoid duplicate error messages from operands that had errors. */
4284 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4285 return error_mark_node;
4287 if (!lvalue_or_else (lhs, lv_assign))
4288 return error_mark_node;
4290 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4292 rhs_semantic_type = TREE_TYPE (rhs);
4293 rhs = TREE_OPERAND (rhs, 0);
4298 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4300 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4302 if (inner == error_mark_node)
4303 return error_mark_node;
4304 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4305 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4306 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4307 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4308 protected_set_expr_location (result, location);
4312 /* If a binary op has been requested, combine the old LHS value with the RHS
4313 producing the value we should actually store into the LHS. */
4315 if (modifycode != NOP_EXPR)
4317 lhs = c_fully_fold (lhs, false, NULL);
4318 lhs = stabilize_reference (lhs);
4319 newrhs = build_binary_op (location,
4320 modifycode, lhs, rhs, 1);
4323 /* Give an error for storing in something that is 'const'. */
4325 if (TYPE_READONLY (lhstype)
4326 || ((TREE_CODE (lhstype) == RECORD_TYPE
4327 || TREE_CODE (lhstype) == UNION_TYPE)
4328 && C_TYPE_FIELDS_READONLY (lhstype)))
4330 readonly_error (lhs, lv_assign);
4331 return error_mark_node;
4333 else if (TREE_READONLY (lhs))
4334 readonly_warning (lhs, lv_assign);
4336 /* If storing into a structure or union member,
4337 it has probably been given type `int'.
4338 Compute the type that would go with
4339 the actual amount of storage the member occupies. */
4341 if (TREE_CODE (lhs) == COMPONENT_REF
4342 && (TREE_CODE (lhstype) == INTEGER_TYPE
4343 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4344 || TREE_CODE (lhstype) == REAL_TYPE
4345 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4346 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4348 /* If storing in a field that is in actuality a short or narrower than one,
4349 we must store in the field in its actual type. */
4351 if (lhstype != TREE_TYPE (lhs))
4353 lhs = copy_node (lhs);
4354 TREE_TYPE (lhs) = lhstype;
4357 /* Convert new value to destination type. Fold it first, then
4358 restore any excess precision information, for the sake of
4359 conversion warnings. */
4361 npc = null_pointer_constant_p (newrhs);
4362 newrhs = c_fully_fold (newrhs, false, NULL);
4363 if (rhs_semantic_type)
4364 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4365 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign, npc,
4366 NULL_TREE, NULL_TREE, 0);
4367 if (TREE_CODE (newrhs) == ERROR_MARK)
4368 return error_mark_node;
4370 /* Emit ObjC write barrier, if necessary. */
4371 if (c_dialect_objc () && flag_objc_gc)
4373 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4376 protected_set_expr_location (result, location);
4381 /* Scan operands. */
4383 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4384 TREE_SIDE_EFFECTS (result) = 1;
4385 protected_set_expr_location (result, location);
4387 /* If we got the LHS in a different type for storing in,
4388 convert the result back to the nominal type of LHS
4389 so that the value we return always has the same type
4390 as the LHS argument. */
4392 if (olhstype == TREE_TYPE (result))
4395 result = convert_for_assignment (olhstype, result, ic_assign, false,
4396 NULL_TREE, NULL_TREE, 0);
4397 protected_set_expr_location (result, location);
4401 /* Convert value RHS to type TYPE as preparation for an assignment
4402 to an lvalue of type TYPE. NULL_POINTER_CONSTANT says whether RHS
4403 was a null pointer constant before any folding.
4404 The real work of conversion is done by `convert'.
4405 The purpose of this function is to generate error messages
4406 for assignments that are not allowed in C.
4407 ERRTYPE says whether it is argument passing, assignment,
4408 initialization or return.
4410 FUNCTION is a tree for the function being called.
4411 PARMNUM is the number of the argument, for printing in error messages. */
4414 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
4415 bool null_pointer_constant,
4416 tree fundecl, tree function, int parmnum)
4418 enum tree_code codel = TREE_CODE (type);
4419 tree orig_rhs = rhs;
4421 enum tree_code coder;
4422 tree rname = NULL_TREE;
4423 bool objc_ok = false;
4425 if (errtype == ic_argpass)
4428 /* Change pointer to function to the function itself for
4430 if (TREE_CODE (function) == ADDR_EXPR
4431 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4432 function = TREE_OPERAND (function, 0);
4434 /* Handle an ObjC selector specially for diagnostics. */
4435 selector = objc_message_selector ();
4437 if (selector && parmnum > 2)
4444 /* This macro is used to emit diagnostics to ensure that all format
4445 strings are complete sentences, visible to gettext and checked at
4447 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4452 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4453 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4454 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4455 "expected %qT but argument is of type %qT", \
4459 pedwarn (LOCATION, OPT, AS); \
4462 pedwarn (LOCATION, OPT, IN); \
4465 pedwarn (LOCATION, OPT, RE); \
4468 gcc_unreachable (); \
4472 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4473 rhs = TREE_OPERAND (rhs, 0);
4475 rhstype = TREE_TYPE (rhs);
4476 coder = TREE_CODE (rhstype);
4478 if (coder == ERROR_MARK)
4479 return error_mark_node;
4481 if (c_dialect_objc ())
4504 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4507 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4510 if (coder == VOID_TYPE)
4512 /* Except for passing an argument to an unprototyped function,
4513 this is a constraint violation. When passing an argument to
4514 an unprototyped function, it is compile-time undefined;
4515 making it a constraint in that case was rejected in
4517 error ("void value not ignored as it ought to be");
4518 return error_mark_node;
4520 rhs = require_complete_type (rhs);
4521 if (rhs == error_mark_node)
4522 return error_mark_node;
4523 /* A type converts to a reference to it.
4524 This code doesn't fully support references, it's just for the
4525 special case of va_start and va_copy. */
4526 if (codel == REFERENCE_TYPE
4527 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4529 if (!lvalue_p (rhs))
4531 error ("cannot pass rvalue to reference parameter");
4532 return error_mark_node;
4534 if (!c_mark_addressable (rhs))
4535 return error_mark_node;
4536 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4538 /* We already know that these two types are compatible, but they
4539 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4540 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4541 likely to be va_list, a typedef to __builtin_va_list, which
4542 is different enough that it will cause problems later. */
4543 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4544 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4546 rhs = build1 (NOP_EXPR, type, rhs);
4549 /* Some types can interconvert without explicit casts. */
4550 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4551 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4552 return convert (type, rhs);
4553 /* Arithmetic types all interconvert, and enum is treated like int. */
4554 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4555 || codel == FIXED_POINT_TYPE
4556 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4557 || codel == BOOLEAN_TYPE)
4558 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4559 || coder == FIXED_POINT_TYPE
4560 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4561 || coder == BOOLEAN_TYPE))
4564 bool save = in_late_binary_op;
4565 if (codel == BOOLEAN_TYPE)
4566 in_late_binary_op = true;
4567 ret = convert_and_check (type, orig_rhs);
4568 if (codel == BOOLEAN_TYPE)
4569 in_late_binary_op = save;
4573 /* Aggregates in different TUs might need conversion. */
4574 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4576 && comptypes (type, rhstype))
4577 return convert_and_check (type, rhs);
4579 /* Conversion to a transparent union from its member types.
4580 This applies only to function arguments. */
4581 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4582 && errtype == ic_argpass)
4584 tree memb, marginal_memb = NULL_TREE;
4586 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4588 tree memb_type = TREE_TYPE (memb);
4590 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4591 TYPE_MAIN_VARIANT (rhstype)))
4594 if (TREE_CODE (memb_type) != POINTER_TYPE)
4597 if (coder == POINTER_TYPE)
4599 tree ttl = TREE_TYPE (memb_type);
4600 tree ttr = TREE_TYPE (rhstype);
4602 /* Any non-function converts to a [const][volatile] void *
4603 and vice versa; otherwise, targets must be the same.
4604 Meanwhile, the lhs target must have all the qualifiers of
4606 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4607 || comp_target_types (memb_type, rhstype))
4609 /* If this type won't generate any warnings, use it. */
4610 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4611 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4612 && TREE_CODE (ttl) == FUNCTION_TYPE)
4613 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4614 == TYPE_QUALS (ttr))
4615 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4616 == TYPE_QUALS (ttl))))
4619 /* Keep looking for a better type, but remember this one. */
4621 marginal_memb = memb;
4625 /* Can convert integer zero to any pointer type. */
4626 if (null_pointer_constant)
4628 rhs = null_pointer_node;
4633 if (memb || marginal_memb)
4637 /* We have only a marginally acceptable member type;
4638 it needs a warning. */
4639 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4640 tree ttr = TREE_TYPE (rhstype);
4642 /* Const and volatile mean something different for function
4643 types, so the usual warnings are not appropriate. */
4644 if (TREE_CODE (ttr) == FUNCTION_TYPE
4645 && TREE_CODE (ttl) == FUNCTION_TYPE)
4647 /* Because const and volatile on functions are
4648 restrictions that say the function will not do
4649 certain things, it is okay to use a const or volatile
4650 function where an ordinary one is wanted, but not
4652 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4653 WARN_FOR_ASSIGNMENT (input_location, 0,
4654 G_("passing argument %d of %qE "
4655 "makes qualified function "
4656 "pointer from unqualified"),
4657 G_("assignment makes qualified "
4658 "function pointer from "
4660 G_("initialization makes qualified "
4661 "function pointer from "
4663 G_("return makes qualified function "
4664 "pointer from unqualified"));
4666 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4667 WARN_FOR_ASSIGNMENT (input_location, 0,
4668 G_("passing argument %d of %qE discards "
4669 "qualifiers from pointer target type"),
4670 G_("assignment discards qualifiers "
4671 "from pointer target type"),
4672 G_("initialization discards qualifiers "
4673 "from pointer target type"),
4674 G_("return discards qualifiers from "
4675 "pointer target type"));
4677 memb = marginal_memb;
4680 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
4681 pedwarn (input_location, OPT_pedantic,
4682 "ISO C prohibits argument conversion to union type");
4684 rhs = fold_convert (TREE_TYPE (memb), rhs);
4685 return build_constructor_single (type, memb, rhs);
4689 /* Conversions among pointers */
4690 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4691 && (coder == codel))
4693 tree ttl = TREE_TYPE (type);
4694 tree ttr = TREE_TYPE (rhstype);
4697 bool is_opaque_pointer;
4698 int target_cmp = 0; /* Cache comp_target_types () result. */
4700 if (TREE_CODE (mvl) != ARRAY_TYPE)
4701 mvl = TYPE_MAIN_VARIANT (mvl);
4702 if (TREE_CODE (mvr) != ARRAY_TYPE)
4703 mvr = TYPE_MAIN_VARIANT (mvr);
4704 /* Opaque pointers are treated like void pointers. */
4705 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
4707 /* C++ does not allow the implicit conversion void* -> T*. However,
4708 for the purpose of reducing the number of false positives, we
4709 tolerate the special case of
4713 where NULL is typically defined in C to be '(void *) 0'. */
4714 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4715 warning (OPT_Wc___compat, "request for implicit conversion from "
4716 "%qT to %qT not permitted in C++", rhstype, type);
4718 /* Check if the right-hand side has a format attribute but the
4719 left-hand side doesn't. */
4720 if (warn_missing_format_attribute
4721 && check_missing_format_attribute (type, rhstype))
4726 warning (OPT_Wmissing_format_attribute,
4727 "argument %d of %qE might be "
4728 "a candidate for a format attribute",
4732 warning (OPT_Wmissing_format_attribute,
4733 "assignment left-hand side might be "
4734 "a candidate for a format attribute");
4737 warning (OPT_Wmissing_format_attribute,
4738 "initialization left-hand side might be "
4739 "a candidate for a format attribute");
4742 warning (OPT_Wmissing_format_attribute,
4743 "return type might be "
4744 "a candidate for a format attribute");
4751 /* Any non-function converts to a [const][volatile] void *
4752 and vice versa; otherwise, targets must be the same.
4753 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4754 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4755 || (target_cmp = comp_target_types (type, rhstype))
4756 || is_opaque_pointer
4757 || (c_common_unsigned_type (mvl)
4758 == c_common_unsigned_type (mvr)))
4761 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4764 && !null_pointer_constant
4765 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4766 WARN_FOR_ASSIGNMENT (input_location, OPT_pedantic,
4767 G_("ISO C forbids passing argument %d of "
4768 "%qE between function pointer "
4770 G_("ISO C forbids assignment between "
4771 "function pointer and %<void *%>"),
4772 G_("ISO C forbids initialization between "
4773 "function pointer and %<void *%>"),
4774 G_("ISO C forbids return between function "
4775 "pointer and %<void *%>"));
4776 /* Const and volatile mean something different for function types,
4777 so the usual warnings are not appropriate. */
4778 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4779 && TREE_CODE (ttl) != FUNCTION_TYPE)
4781 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4783 /* Types differing only by the presence of the 'volatile'
4784 qualifier are acceptable if the 'volatile' has been added
4785 in by the Objective-C EH machinery. */
4786 if (!objc_type_quals_match (ttl, ttr))
4787 WARN_FOR_ASSIGNMENT (input_location, 0,
4788 G_("passing argument %d of %qE discards "
4789 "qualifiers from pointer target type"),
4790 G_("assignment discards qualifiers "
4791 "from pointer target type"),
4792 G_("initialization discards qualifiers "
4793 "from pointer target type"),
4794 G_("return discards qualifiers from "
4795 "pointer target type"));
4797 /* If this is not a case of ignoring a mismatch in signedness,
4799 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4802 /* If there is a mismatch, do warn. */
4803 else if (warn_pointer_sign)
4804 WARN_FOR_ASSIGNMENT (input_location, OPT_Wpointer_sign,
4805 G_("pointer targets in passing argument "
4806 "%d of %qE differ in signedness"),
4807 G_("pointer targets in assignment "
4808 "differ in signedness"),
4809 G_("pointer targets in initialization "
4810 "differ in signedness"),
4811 G_("pointer targets in return differ "
4814 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4815 && TREE_CODE (ttr) == FUNCTION_TYPE)
4817 /* Because const and volatile on functions are restrictions
4818 that say the function will not do certain things,
4819 it is okay to use a const or volatile function
4820 where an ordinary one is wanted, but not vice-versa. */
4821 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4822 WARN_FOR_ASSIGNMENT (input_location, 0,
4823 G_("passing argument %d of %qE makes "
4824 "qualified function pointer "
4825 "from unqualified"),
4826 G_("assignment makes qualified function "
4827 "pointer from unqualified"),
4828 G_("initialization makes qualified "
4829 "function pointer from unqualified"),
4830 G_("return makes qualified function "
4831 "pointer from unqualified"));
4835 /* Avoid warning about the volatile ObjC EH puts on decls. */
4837 WARN_FOR_ASSIGNMENT (input_location, 0,
4838 G_("passing argument %d of %qE from "
4839 "incompatible pointer type"),
4840 G_("assignment from incompatible pointer type"),
4841 G_("initialization from incompatible "
4843 G_("return from incompatible pointer type"));
4845 return convert (type, rhs);
4847 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4849 /* ??? This should not be an error when inlining calls to
4850 unprototyped functions. */
4851 error ("invalid use of non-lvalue array");
4852 return error_mark_node;
4854 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4856 /* An explicit constant 0 can convert to a pointer,
4857 or one that results from arithmetic, even including
4858 a cast to integer type. */
4859 if (!null_pointer_constant)
4860 WARN_FOR_ASSIGNMENT (input_location, 0,
4861 G_("passing argument %d of %qE makes "
4862 "pointer from integer without a cast"),
4863 G_("assignment makes pointer from integer "
4865 G_("initialization makes pointer from "
4866 "integer without a cast"),
4867 G_("return makes pointer from integer "
4870 return convert (type, rhs);
4872 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4874 WARN_FOR_ASSIGNMENT (input_location, 0,
4875 G_("passing argument %d of %qE makes integer "
4876 "from pointer without a cast"),
4877 G_("assignment makes integer from pointer "
4879 G_("initialization makes integer from pointer "
4881 G_("return makes integer from pointer "
4883 return convert (type, rhs);
4885 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4888 bool save = in_late_binary_op;
4889 in_late_binary_op = true;
4890 ret = convert (type, rhs);
4891 in_late_binary_op = save;
4898 error ("incompatible type for argument %d of %qE", parmnum, rname);
4899 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
4900 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
4901 "expected %qT but argument is of type %qT", type, rhstype);
4904 error ("incompatible types when assigning to type %qT from type %qT",
4908 error ("incompatible types when initializing type %qT using type %qT",
4912 error ("incompatible types when returning type %qT but %qT was expected",
4919 return error_mark_node;
4922 /* If VALUE is a compound expr all of whose expressions are constant, then
4923 return its value. Otherwise, return error_mark_node.
4925 This is for handling COMPOUND_EXPRs as initializer elements
4926 which is allowed with a warning when -pedantic is specified. */
4929 valid_compound_expr_initializer (tree value, tree endtype)
4931 if (TREE_CODE (value) == COMPOUND_EXPR)
4933 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4935 return error_mark_node;
4936 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4939 else if (!initializer_constant_valid_p (value, endtype))
4940 return error_mark_node;
4945 /* Perform appropriate conversions on the initial value of a variable,
4946 store it in the declaration DECL,
4947 and print any error messages that are appropriate.
4948 If the init is invalid, store an ERROR_MARK. */
4951 store_init_value (tree decl, tree init)
4956 /* If variable's type was invalidly declared, just ignore it. */
4958 type = TREE_TYPE (decl);
4959 if (TREE_CODE (type) == ERROR_MARK)
4962 /* Digest the specified initializer into an expression. */
4965 npc = null_pointer_constant_p (init);
4966 value = digest_init (type, init, npc, true, TREE_STATIC (decl));
4968 /* Store the expression if valid; else report error. */
4970 if (!in_system_header
4971 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4972 warning (OPT_Wtraditional, "traditional C rejects automatic "
4973 "aggregate initialization");
4975 DECL_INITIAL (decl) = value;
4977 /* ANSI wants warnings about out-of-range constant initializers. */
4978 STRIP_TYPE_NOPS (value);
4979 if (TREE_STATIC (decl))
4980 constant_expression_warning (value);
4982 /* Check if we need to set array size from compound literal size. */
4983 if (TREE_CODE (type) == ARRAY_TYPE
4984 && TYPE_DOMAIN (type) == 0
4985 && value != error_mark_node)
4987 tree inside_init = init;
4989 STRIP_TYPE_NOPS (inside_init);
4990 inside_init = fold (inside_init);
4992 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4994 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4996 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
4998 /* For int foo[] = (int [3]){1}; we need to set array size
4999 now since later on array initializer will be just the
5000 brace enclosed list of the compound literal. */
5001 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5002 TREE_TYPE (decl) = type;
5003 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5005 layout_decl (cldecl, 0);
5011 /* Methods for storing and printing names for error messages. */
5013 /* Implement a spelling stack that allows components of a name to be pushed
5014 and popped. Each element on the stack is this structure. */
5021 unsigned HOST_WIDE_INT i;
5026 #define SPELLING_STRING 1
5027 #define SPELLING_MEMBER 2
5028 #define SPELLING_BOUNDS 3
5030 static struct spelling *spelling; /* Next stack element (unused). */
5031 static struct spelling *spelling_base; /* Spelling stack base. */
5032 static int spelling_size; /* Size of the spelling stack. */
5034 /* Macros to save and restore the spelling stack around push_... functions.
5035 Alternative to SAVE_SPELLING_STACK. */
5037 #define SPELLING_DEPTH() (spelling - spelling_base)
5038 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5040 /* Push an element on the spelling stack with type KIND and assign VALUE
5043 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5045 int depth = SPELLING_DEPTH (); \
5047 if (depth >= spelling_size) \
5049 spelling_size += 10; \
5050 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5052 RESTORE_SPELLING_DEPTH (depth); \
5055 spelling->kind = (KIND); \
5056 spelling->MEMBER = (VALUE); \
5060 /* Push STRING on the stack. Printed literally. */
5063 push_string (const char *string)
5065 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5068 /* Push a member name on the stack. Printed as '.' STRING. */
5071 push_member_name (tree decl)
5073 const char *const string
5074 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
5075 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5078 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5081 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5083 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5086 /* Compute the maximum size in bytes of the printed spelling. */
5089 spelling_length (void)
5094 for (p = spelling_base; p < spelling; p++)
5096 if (p->kind == SPELLING_BOUNDS)
5099 size += strlen (p->u.s) + 1;
5105 /* Print the spelling to BUFFER and return it. */
5108 print_spelling (char *buffer)
5113 for (p = spelling_base; p < spelling; p++)
5114 if (p->kind == SPELLING_BOUNDS)
5116 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5122 if (p->kind == SPELLING_MEMBER)
5124 for (s = p->u.s; (*d = *s++); d++)
5131 /* Issue an error message for a bad initializer component.
5132 MSGID identifies the message.
5133 The component name is taken from the spelling stack. */
5136 error_init (const char *msgid)
5140 error ("%s", _(msgid));
5141 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5143 error ("(near initialization for %qs)", ofwhat);
5146 /* Issue a pedantic warning for a bad initializer component. OPT is
5147 the option OPT_* (from options.h) controlling this warning or 0 if
5148 it is unconditionally given. MSGID identifies the message. The
5149 component name is taken from the spelling stack. */
5152 pedwarn_init (location_t location, int opt, const char *msgid)
5156 pedwarn (location, opt, "%s", _(msgid));
5157 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5159 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5162 /* Issue a warning for a bad initializer component.
5164 OPT is the OPT_W* value corresponding to the warning option that
5165 controls this warning. MSGID identifies the message. The
5166 component name is taken from the spelling stack. */
5169 warning_init (int opt, const char *msgid)
5173 warning (opt, "%s", _(msgid));
5174 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5176 warning (opt, "(near initialization for %qs)", ofwhat);
5179 /* If TYPE is an array type and EXPR is a parenthesized string
5180 constant, warn if pedantic that EXPR is being used to initialize an
5181 object of type TYPE. */
5184 maybe_warn_string_init (tree type, struct c_expr expr)
5187 && TREE_CODE (type) == ARRAY_TYPE
5188 && TREE_CODE (expr.value) == STRING_CST
5189 && expr.original_code != STRING_CST)
5190 pedwarn_init (input_location, OPT_pedantic,
5191 "array initialized from parenthesized string constant");
5194 /* Digest the parser output INIT as an initializer for type TYPE.
5195 Return a C expression of type TYPE to represent the initial value.
5197 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5199 If INIT is a string constant, STRICT_STRING is true if it is
5200 unparenthesized or we should not warn here for it being parenthesized.
5201 For other types of INIT, STRICT_STRING is not used.
5203 REQUIRE_CONSTANT requests an error if non-constant initializers or
5204 elements are seen. */
5207 digest_init (tree type, tree init, bool null_pointer_constant,
5208 bool strict_string, int require_constant)
5210 enum tree_code code = TREE_CODE (type);
5211 tree inside_init = init;
5212 tree semantic_type = NULL_TREE;
5213 bool maybe_const = true;
5215 if (type == error_mark_node
5217 || init == error_mark_node
5218 || TREE_TYPE (init) == error_mark_node)
5219 return error_mark_node;
5221 STRIP_TYPE_NOPS (inside_init);
5223 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5225 semantic_type = TREE_TYPE (inside_init);
5226 inside_init = TREE_OPERAND (inside_init, 0);
5228 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5229 inside_init = decl_constant_value_for_optimization (inside_init);
5231 /* Initialization of an array of chars from a string constant
5232 optionally enclosed in braces. */
5234 if (code == ARRAY_TYPE && inside_init
5235 && TREE_CODE (inside_init) == STRING_CST)
5237 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5238 /* Note that an array could be both an array of character type
5239 and an array of wchar_t if wchar_t is signed char or unsigned
5241 bool char_array = (typ1 == char_type_node
5242 || typ1 == signed_char_type_node
5243 || typ1 == unsigned_char_type_node);
5244 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5245 bool char16_array = !!comptypes (typ1, char16_type_node);
5246 bool char32_array = !!comptypes (typ1, char32_type_node);
5248 if (char_array || wchar_array || char16_array || char32_array)
5251 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5252 expr.value = inside_init;
5253 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5254 expr.original_type = NULL;
5255 maybe_warn_string_init (type, expr);
5257 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5258 TYPE_MAIN_VARIANT (type)))
5263 if (typ2 != char_type_node)
5265 error_init ("char-array initialized from wide string");
5266 return error_mark_node;
5271 if (typ2 == char_type_node)
5273 error_init ("wide character array initialized from non-wide "
5275 return error_mark_node;
5277 else if (!comptypes(typ1, typ2))
5279 error_init ("wide character array initialized from "
5280 "incompatible wide string");
5281 return error_mark_node;
5285 TREE_TYPE (inside_init) = type;
5286 if (TYPE_DOMAIN (type) != 0
5287 && TYPE_SIZE (type) != 0
5288 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
5289 /* Subtract the size of a single (possibly wide) character
5290 because it's ok to ignore the terminating null char
5291 that is counted in the length of the constant. */
5292 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5293 TREE_STRING_LENGTH (inside_init)
5294 - (TYPE_PRECISION (typ1)
5296 pedwarn_init (input_location, 0,
5297 "initializer-string for array of chars is too long");
5301 else if (INTEGRAL_TYPE_P (typ1))
5303 error_init ("array of inappropriate type initialized "
5304 "from string constant");
5305 return error_mark_node;
5309 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5310 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5311 below and handle as a constructor. */
5312 if (code == VECTOR_TYPE
5313 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5314 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5315 && TREE_CONSTANT (inside_init))
5317 if (TREE_CODE (inside_init) == VECTOR_CST
5318 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5319 TYPE_MAIN_VARIANT (type)))
5322 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5324 unsigned HOST_WIDE_INT ix;
5326 bool constant_p = true;
5328 /* Iterate through elements and check if all constructor
5329 elements are *_CSTs. */
5330 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5331 if (!CONSTANT_CLASS_P (value))
5338 return build_vector_from_ctor (type,
5339 CONSTRUCTOR_ELTS (inside_init));
5343 if (warn_sequence_point)
5344 verify_sequence_points (inside_init);
5346 /* Any type can be initialized
5347 from an expression of the same type, optionally with braces. */
5349 if (inside_init && TREE_TYPE (inside_init) != 0
5350 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5351 TYPE_MAIN_VARIANT (type))
5352 || (code == ARRAY_TYPE
5353 && comptypes (TREE_TYPE (inside_init), type))
5354 || (code == VECTOR_TYPE
5355 && comptypes (TREE_TYPE (inside_init), type))
5356 || (code == POINTER_TYPE
5357 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5358 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5359 TREE_TYPE (type)))))
5361 if (code == POINTER_TYPE)
5363 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5365 if (TREE_CODE (inside_init) == STRING_CST
5366 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5367 inside_init = array_to_pointer_conversion (inside_init);
5370 error_init ("invalid use of non-lvalue array");
5371 return error_mark_node;
5376 if (code == VECTOR_TYPE)
5377 /* Although the types are compatible, we may require a
5379 inside_init = convert (type, inside_init);
5381 if (require_constant
5382 && (code == VECTOR_TYPE || !flag_isoc99)
5383 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5385 /* As an extension, allow initializing objects with static storage
5386 duration with compound literals (which are then treated just as
5387 the brace enclosed list they contain). Also allow this for
5388 vectors, as we can only assign them with compound literals. */
5389 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5390 inside_init = DECL_INITIAL (decl);
5393 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5394 && TREE_CODE (inside_init) != CONSTRUCTOR)
5396 error_init ("array initialized from non-constant array expression");
5397 return error_mark_node;
5400 /* Compound expressions can only occur here if -pedantic or
5401 -pedantic-errors is specified. In the later case, we always want
5402 an error. In the former case, we simply want a warning. */
5403 if (require_constant && pedantic
5404 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5407 = valid_compound_expr_initializer (inside_init,
5408 TREE_TYPE (inside_init));
5409 if (inside_init == error_mark_node)
5410 error_init ("initializer element is not constant");
5412 pedwarn_init (input_location, OPT_pedantic,
5413 "initializer element is not constant");
5414 if (flag_pedantic_errors)
5415 inside_init = error_mark_node;
5417 else if (require_constant
5418 && !initializer_constant_valid_p (inside_init,
5419 TREE_TYPE (inside_init)))
5421 error_init ("initializer element is not constant");
5422 inside_init = error_mark_node;
5424 else if (require_constant && !maybe_const)
5425 pedwarn_init (input_location, 0,
5426 "initializer element is not a constant expression");
5428 /* Added to enable additional -Wmissing-format-attribute warnings. */
5429 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
5430 inside_init = convert_for_assignment (type, inside_init, ic_init,
5431 null_pointer_constant,
5432 NULL_TREE, NULL_TREE, 0);
5436 /* Handle scalar types, including conversions. */
5438 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
5439 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
5440 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
5442 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
5443 && (TREE_CODE (init) == STRING_CST
5444 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
5445 inside_init = init = array_to_pointer_conversion (init);
5447 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
5450 = convert_for_assignment (type, inside_init, ic_init,
5451 null_pointer_constant,
5452 NULL_TREE, NULL_TREE, 0);
5454 /* Check to see if we have already given an error message. */
5455 if (inside_init == error_mark_node)
5457 else if (require_constant && !TREE_CONSTANT (inside_init))
5459 error_init ("initializer element is not constant");
5460 inside_init = error_mark_node;
5462 else if (require_constant
5463 && !initializer_constant_valid_p (inside_init,
5464 TREE_TYPE (inside_init)))
5466 error_init ("initializer element is not computable at load time");
5467 inside_init = error_mark_node;
5469 else if (require_constant && !maybe_const)
5470 pedwarn_init (input_location, 0,
5471 "initializer element is not a constant expression");
5476 /* Come here only for records and arrays. */
5478 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5480 error_init ("variable-sized object may not be initialized");
5481 return error_mark_node;
5484 error_init ("invalid initializer");
5485 return error_mark_node;
5488 /* Handle initializers that use braces. */
5490 /* Type of object we are accumulating a constructor for.
5491 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5492 static tree constructor_type;
5494 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5496 static tree constructor_fields;
5498 /* For an ARRAY_TYPE, this is the specified index
5499 at which to store the next element we get. */
5500 static tree constructor_index;
5502 /* For an ARRAY_TYPE, this is the maximum index. */
5503 static tree constructor_max_index;
5505 /* For a RECORD_TYPE, this is the first field not yet written out. */
5506 static tree constructor_unfilled_fields;
5508 /* For an ARRAY_TYPE, this is the index of the first element
5509 not yet written out. */
5510 static tree constructor_unfilled_index;
5512 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5513 This is so we can generate gaps between fields, when appropriate. */
5514 static tree constructor_bit_index;
5516 /* If we are saving up the elements rather than allocating them,
5517 this is the list of elements so far (in reverse order,
5518 most recent first). */
5519 static VEC(constructor_elt,gc) *constructor_elements;
5521 /* 1 if constructor should be incrementally stored into a constructor chain,
5522 0 if all the elements should be kept in AVL tree. */
5523 static int constructor_incremental;
5525 /* 1 if so far this constructor's elements are all compile-time constants. */
5526 static int constructor_constant;
5528 /* 1 if so far this constructor's elements are all valid address constants. */
5529 static int constructor_simple;
5531 /* 1 if this constructor has an element that cannot be part of a
5532 constant expression. */
5533 static int constructor_nonconst;
5535 /* 1 if this constructor is erroneous so far. */
5536 static int constructor_erroneous;
5538 /* Structure for managing pending initializer elements, organized as an
5543 struct init_node *left, *right;
5544 struct init_node *parent;
5550 /* Tree of pending elements at this constructor level.
5551 These are elements encountered out of order
5552 which belong at places we haven't reached yet in actually
5554 Will never hold tree nodes across GC runs. */
5555 static struct init_node *constructor_pending_elts;
5557 /* The SPELLING_DEPTH of this constructor. */
5558 static int constructor_depth;
5560 /* DECL node for which an initializer is being read.
5561 0 means we are reading a constructor expression
5562 such as (struct foo) {...}. */
5563 static tree constructor_decl;
5565 /* Nonzero if this is an initializer for a top-level decl. */
5566 static int constructor_top_level;
5568 /* Nonzero if there were any member designators in this initializer. */
5569 static int constructor_designated;
5571 /* Nesting depth of designator list. */
5572 static int designator_depth;
5574 /* Nonzero if there were diagnosed errors in this designator list. */
5575 static int designator_erroneous;
5578 /* This stack has a level for each implicit or explicit level of
5579 structuring in the initializer, including the outermost one. It
5580 saves the values of most of the variables above. */
5582 struct constructor_range_stack;
5584 struct constructor_stack
5586 struct constructor_stack *next;
5591 tree unfilled_index;
5592 tree unfilled_fields;
5594 VEC(constructor_elt,gc) *elements;
5595 struct init_node *pending_elts;
5598 /* If value nonzero, this value should replace the entire
5599 constructor at this level. */
5600 struct c_expr replacement_value;
5601 struct constructor_range_stack *range_stack;
5612 static struct constructor_stack *constructor_stack;
5614 /* This stack represents designators from some range designator up to
5615 the last designator in the list. */
5617 struct constructor_range_stack
5619 struct constructor_range_stack *next, *prev;
5620 struct constructor_stack *stack;
5627 static struct constructor_range_stack *constructor_range_stack;
5629 /* This stack records separate initializers that are nested.
5630 Nested initializers can't happen in ANSI C, but GNU C allows them
5631 in cases like { ... (struct foo) { ... } ... }. */
5633 struct initializer_stack
5635 struct initializer_stack *next;
5637 struct constructor_stack *constructor_stack;
5638 struct constructor_range_stack *constructor_range_stack;
5639 VEC(constructor_elt,gc) *elements;
5640 struct spelling *spelling;
5641 struct spelling *spelling_base;
5644 char require_constant_value;
5645 char require_constant_elements;
5648 static struct initializer_stack *initializer_stack;
5650 /* Prepare to parse and output the initializer for variable DECL. */
5653 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5656 struct initializer_stack *p = XNEW (struct initializer_stack);
5658 p->decl = constructor_decl;
5659 p->require_constant_value = require_constant_value;
5660 p->require_constant_elements = require_constant_elements;
5661 p->constructor_stack = constructor_stack;
5662 p->constructor_range_stack = constructor_range_stack;
5663 p->elements = constructor_elements;
5664 p->spelling = spelling;
5665 p->spelling_base = spelling_base;
5666 p->spelling_size = spelling_size;
5667 p->top_level = constructor_top_level;
5668 p->next = initializer_stack;
5669 initializer_stack = p;
5671 constructor_decl = decl;
5672 constructor_designated = 0;
5673 constructor_top_level = top_level;
5675 if (decl != 0 && decl != error_mark_node)
5677 require_constant_value = TREE_STATIC (decl);
5678 require_constant_elements
5679 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5680 /* For a scalar, you can always use any value to initialize,
5681 even within braces. */
5682 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5683 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5684 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5685 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5686 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5690 require_constant_value = 0;
5691 require_constant_elements = 0;
5692 locus = "(anonymous)";
5695 constructor_stack = 0;
5696 constructor_range_stack = 0;
5698 missing_braces_mentioned = 0;
5702 RESTORE_SPELLING_DEPTH (0);
5705 push_string (locus);
5711 struct initializer_stack *p = initializer_stack;
5713 /* Free the whole constructor stack of this initializer. */
5714 while (constructor_stack)
5716 struct constructor_stack *q = constructor_stack;
5717 constructor_stack = q->next;
5721 gcc_assert (!constructor_range_stack);
5723 /* Pop back to the data of the outer initializer (if any). */
5724 free (spelling_base);
5726 constructor_decl = p->decl;
5727 require_constant_value = p->require_constant_value;
5728 require_constant_elements = p->require_constant_elements;
5729 constructor_stack = p->constructor_stack;
5730 constructor_range_stack = p->constructor_range_stack;
5731 constructor_elements = p->elements;
5732 spelling = p->spelling;
5733 spelling_base = p->spelling_base;
5734 spelling_size = p->spelling_size;
5735 constructor_top_level = p->top_level;
5736 initializer_stack = p->next;
5740 /* Call here when we see the initializer is surrounded by braces.
5741 This is instead of a call to push_init_level;
5742 it is matched by a call to pop_init_level.
5744 TYPE is the type to initialize, for a constructor expression.
5745 For an initializer for a decl, TYPE is zero. */
5748 really_start_incremental_init (tree type)
5750 struct constructor_stack *p = XNEW (struct constructor_stack);
5753 type = TREE_TYPE (constructor_decl);
5755 if (TREE_CODE (type) == VECTOR_TYPE
5756 && TYPE_VECTOR_OPAQUE (type))
5757 error ("opaque vector types cannot be initialized");
5759 p->type = constructor_type;
5760 p->fields = constructor_fields;
5761 p->index = constructor_index;
5762 p->max_index = constructor_max_index;
5763 p->unfilled_index = constructor_unfilled_index;
5764 p->unfilled_fields = constructor_unfilled_fields;
5765 p->bit_index = constructor_bit_index;
5766 p->elements = constructor_elements;
5767 p->constant = constructor_constant;
5768 p->simple = constructor_simple;
5769 p->nonconst = constructor_nonconst;
5770 p->erroneous = constructor_erroneous;
5771 p->pending_elts = constructor_pending_elts;
5772 p->depth = constructor_depth;
5773 p->replacement_value.value = 0;
5774 p->replacement_value.original_code = ERROR_MARK;
5775 p->replacement_value.original_type = NULL;
5779 p->incremental = constructor_incremental;
5780 p->designated = constructor_designated;
5782 constructor_stack = p;
5784 constructor_constant = 1;
5785 constructor_simple = 1;
5786 constructor_nonconst = 0;
5787 constructor_depth = SPELLING_DEPTH ();
5788 constructor_elements = 0;
5789 constructor_pending_elts = 0;
5790 constructor_type = type;
5791 constructor_incremental = 1;
5792 constructor_designated = 0;
5793 designator_depth = 0;
5794 designator_erroneous = 0;
5796 if (TREE_CODE (constructor_type) == RECORD_TYPE
5797 || TREE_CODE (constructor_type) == UNION_TYPE)
5799 constructor_fields = TYPE_FIELDS (constructor_type);
5800 /* Skip any nameless bit fields at the beginning. */
5801 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5802 && DECL_NAME (constructor_fields) == 0)
5803 constructor_fields = TREE_CHAIN (constructor_fields);
5805 constructor_unfilled_fields = constructor_fields;
5806 constructor_bit_index = bitsize_zero_node;
5808 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5810 if (TYPE_DOMAIN (constructor_type))
5812 constructor_max_index
5813 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5815 /* Detect non-empty initializations of zero-length arrays. */
5816 if (constructor_max_index == NULL_TREE
5817 && TYPE_SIZE (constructor_type))
5818 constructor_max_index = build_int_cst (NULL_TREE, -1);
5820 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5821 to initialize VLAs will cause a proper error; avoid tree
5822 checking errors as well by setting a safe value. */
5823 if (constructor_max_index
5824 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5825 constructor_max_index = build_int_cst (NULL_TREE, -1);
5828 = convert (bitsizetype,
5829 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5833 constructor_index = bitsize_zero_node;
5834 constructor_max_index = NULL_TREE;
5837 constructor_unfilled_index = constructor_index;
5839 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5841 /* Vectors are like simple fixed-size arrays. */
5842 constructor_max_index =
5843 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5844 constructor_index = bitsize_zero_node;
5845 constructor_unfilled_index = constructor_index;
5849 /* Handle the case of int x = {5}; */
5850 constructor_fields = constructor_type;
5851 constructor_unfilled_fields = constructor_type;
5855 /* Push down into a subobject, for initialization.
5856 If this is for an explicit set of braces, IMPLICIT is 0.
5857 If it is because the next element belongs at a lower level,
5858 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5861 push_init_level (int implicit)
5863 struct constructor_stack *p;
5864 tree value = NULL_TREE;
5866 /* If we've exhausted any levels that didn't have braces,
5867 pop them now. If implicit == 1, this will have been done in
5868 process_init_element; do not repeat it here because in the case
5869 of excess initializers for an empty aggregate this leads to an
5870 infinite cycle of popping a level and immediately recreating
5874 while (constructor_stack->implicit)
5876 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5877 || TREE_CODE (constructor_type) == UNION_TYPE)
5878 && constructor_fields == 0)
5879 process_init_element (pop_init_level (1), true);
5880 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5881 && constructor_max_index
5882 && tree_int_cst_lt (constructor_max_index,
5884 process_init_element (pop_init_level (1), true);
5890 /* Unless this is an explicit brace, we need to preserve previous
5894 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5895 || TREE_CODE (constructor_type) == UNION_TYPE)
5896 && constructor_fields)
5897 value = find_init_member (constructor_fields);
5898 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5899 value = find_init_member (constructor_index);
5902 p = XNEW (struct constructor_stack);
5903 p->type = constructor_type;
5904 p->fields = constructor_fields;
5905 p->index = constructor_index;
5906 p->max_index = constructor_max_index;
5907 p->unfilled_index = constructor_unfilled_index;
5908 p->unfilled_fields = constructor_unfilled_fields;
5909 p->bit_index = constructor_bit_index;
5910 p->elements = constructor_elements;
5911 p->constant = constructor_constant;
5912 p->simple = constructor_simple;
5913 p->nonconst = constructor_nonconst;
5914 p->erroneous = constructor_erroneous;
5915 p->pending_elts = constructor_pending_elts;
5916 p->depth = constructor_depth;
5917 p->replacement_value.value = 0;
5918 p->replacement_value.original_code = ERROR_MARK;
5919 p->replacement_value.original_type = NULL;
5920 p->implicit = implicit;
5922 p->incremental = constructor_incremental;
5923 p->designated = constructor_designated;
5924 p->next = constructor_stack;
5926 constructor_stack = p;
5928 constructor_constant = 1;
5929 constructor_simple = 1;
5930 constructor_nonconst = 0;
5931 constructor_depth = SPELLING_DEPTH ();
5932 constructor_elements = 0;
5933 constructor_incremental = 1;
5934 constructor_designated = 0;
5935 constructor_pending_elts = 0;
5938 p->range_stack = constructor_range_stack;
5939 constructor_range_stack = 0;
5940 designator_depth = 0;
5941 designator_erroneous = 0;
5944 /* Don't die if an entire brace-pair level is superfluous
5945 in the containing level. */
5946 if (constructor_type == 0)
5948 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5949 || TREE_CODE (constructor_type) == UNION_TYPE)
5951 /* Don't die if there are extra init elts at the end. */
5952 if (constructor_fields == 0)
5953 constructor_type = 0;
5956 constructor_type = TREE_TYPE (constructor_fields);
5957 push_member_name (constructor_fields);
5958 constructor_depth++;
5961 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5963 constructor_type = TREE_TYPE (constructor_type);
5964 push_array_bounds (tree_low_cst (constructor_index, 1));
5965 constructor_depth++;
5968 if (constructor_type == 0)
5970 error_init ("extra brace group at end of initializer");
5971 constructor_fields = 0;
5972 constructor_unfilled_fields = 0;
5976 if (value && TREE_CODE (value) == CONSTRUCTOR)
5978 constructor_constant = TREE_CONSTANT (value);
5979 constructor_simple = TREE_STATIC (value);
5980 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
5981 constructor_elements = CONSTRUCTOR_ELTS (value);
5982 if (!VEC_empty (constructor_elt, constructor_elements)
5983 && (TREE_CODE (constructor_type) == RECORD_TYPE
5984 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5985 set_nonincremental_init ();
5988 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5990 missing_braces_mentioned = 1;
5991 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
5994 if (TREE_CODE (constructor_type) == RECORD_TYPE
5995 || TREE_CODE (constructor_type) == UNION_TYPE)
5997 constructor_fields = TYPE_FIELDS (constructor_type);
5998 /* Skip any nameless bit fields at the beginning. */
5999 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6000 && DECL_NAME (constructor_fields) == 0)
6001 constructor_fields = TREE_CHAIN (constructor_fields);
6003 constructor_unfilled_fields = constructor_fields;
6004 constructor_bit_index = bitsize_zero_node;
6006 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6008 /* Vectors are like simple fixed-size arrays. */
6009 constructor_max_index =
6010 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6011 constructor_index = convert (bitsizetype, integer_zero_node);
6012 constructor_unfilled_index = constructor_index;
6014 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6016 if (TYPE_DOMAIN (constructor_type))
6018 constructor_max_index
6019 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6021 /* Detect non-empty initializations of zero-length arrays. */
6022 if (constructor_max_index == NULL_TREE
6023 && TYPE_SIZE (constructor_type))
6024 constructor_max_index = build_int_cst (NULL_TREE, -1);
6026 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6027 to initialize VLAs will cause a proper error; avoid tree
6028 checking errors as well by setting a safe value. */
6029 if (constructor_max_index
6030 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6031 constructor_max_index = build_int_cst (NULL_TREE, -1);
6034 = convert (bitsizetype,
6035 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6038 constructor_index = bitsize_zero_node;
6040 constructor_unfilled_index = constructor_index;
6041 if (value && TREE_CODE (value) == STRING_CST)
6043 /* We need to split the char/wchar array into individual
6044 characters, so that we don't have to special case it
6046 set_nonincremental_init_from_string (value);
6051 if (constructor_type != error_mark_node)
6052 warning_init (0, "braces around scalar initializer");
6053 constructor_fields = constructor_type;
6054 constructor_unfilled_fields = constructor_type;
6058 /* At the end of an implicit or explicit brace level,
6059 finish up that level of constructor. If a single expression
6060 with redundant braces initialized that level, return the
6061 c_expr structure for that expression. Otherwise, the original_code
6062 element is set to ERROR_MARK.
6063 If we were outputting the elements as they are read, return 0 as the value
6064 from inner levels (process_init_element ignores that),
6065 but return error_mark_node as the value from the outermost level
6066 (that's what we want to put in DECL_INITIAL).
6067 Otherwise, return a CONSTRUCTOR expression as the value. */
6070 pop_init_level (int implicit)
6072 struct constructor_stack *p;
6075 ret.original_code = ERROR_MARK;
6076 ret.original_type = NULL;
6080 /* When we come to an explicit close brace,
6081 pop any inner levels that didn't have explicit braces. */
6082 while (constructor_stack->implicit)
6083 process_init_element (pop_init_level (1), true);
6085 gcc_assert (!constructor_range_stack);
6088 /* Now output all pending elements. */
6089 constructor_incremental = 1;
6090 output_pending_init_elements (1);
6092 p = constructor_stack;
6094 /* Error for initializing a flexible array member, or a zero-length
6095 array member in an inappropriate context. */
6096 if (constructor_type && constructor_fields
6097 && TREE_CODE (constructor_type) == ARRAY_TYPE
6098 && TYPE_DOMAIN (constructor_type)
6099 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6101 /* Silently discard empty initializations. The parser will
6102 already have pedwarned for empty brackets. */
6103 if (integer_zerop (constructor_unfilled_index))
6104 constructor_type = NULL_TREE;
6107 gcc_assert (!TYPE_SIZE (constructor_type));
6109 if (constructor_depth > 2)
6110 error_init ("initialization of flexible array member in a nested context");
6112 pedwarn_init (input_location, OPT_pedantic,
6113 "initialization of a flexible array member");
6115 /* We have already issued an error message for the existence
6116 of a flexible array member not at the end of the structure.
6117 Discard the initializer so that we do not die later. */
6118 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
6119 constructor_type = NULL_TREE;
6123 /* Warn when some struct elements are implicitly initialized to zero. */
6124 if (warn_missing_field_initializers
6126 && TREE_CODE (constructor_type) == RECORD_TYPE
6127 && constructor_unfilled_fields)
6129 /* Do not warn for flexible array members or zero-length arrays. */
6130 while (constructor_unfilled_fields
6131 && (!DECL_SIZE (constructor_unfilled_fields)
6132 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6133 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6135 /* Do not warn if this level of the initializer uses member
6136 designators; it is likely to be deliberate. */
6137 if (constructor_unfilled_fields && !constructor_designated)
6139 push_member_name (constructor_unfilled_fields);
6140 warning_init (OPT_Wmissing_field_initializers,
6141 "missing initializer");
6142 RESTORE_SPELLING_DEPTH (constructor_depth);
6146 /* Pad out the end of the structure. */
6147 if (p->replacement_value.value)
6148 /* If this closes a superfluous brace pair,
6149 just pass out the element between them. */
6150 ret = p->replacement_value;
6151 else if (constructor_type == 0)
6153 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6154 && TREE_CODE (constructor_type) != UNION_TYPE
6155 && TREE_CODE (constructor_type) != ARRAY_TYPE
6156 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6158 /* A nonincremental scalar initializer--just return
6159 the element, after verifying there is just one. */
6160 if (VEC_empty (constructor_elt,constructor_elements))
6162 if (!constructor_erroneous)
6163 error_init ("empty scalar initializer");
6164 ret.value = error_mark_node;
6166 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6168 error_init ("extra elements in scalar initializer");
6169 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6172 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6176 if (constructor_erroneous)
6177 ret.value = error_mark_node;
6180 ret.value = build_constructor (constructor_type,
6181 constructor_elements);
6182 if (constructor_constant)
6183 TREE_CONSTANT (ret.value) = 1;
6184 if (constructor_constant && constructor_simple)
6185 TREE_STATIC (ret.value) = 1;
6186 if (constructor_nonconst)
6187 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6191 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6193 if (constructor_nonconst)
6194 ret.original_code = C_MAYBE_CONST_EXPR;
6195 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6196 ret.original_code = ERROR_MARK;
6199 constructor_type = p->type;
6200 constructor_fields = p->fields;
6201 constructor_index = p->index;
6202 constructor_max_index = p->max_index;
6203 constructor_unfilled_index = p->unfilled_index;
6204 constructor_unfilled_fields = p->unfilled_fields;
6205 constructor_bit_index = p->bit_index;
6206 constructor_elements = p->elements;
6207 constructor_constant = p->constant;
6208 constructor_simple = p->simple;
6209 constructor_nonconst = p->nonconst;
6210 constructor_erroneous = p->erroneous;
6211 constructor_incremental = p->incremental;
6212 constructor_designated = p->designated;
6213 constructor_pending_elts = p->pending_elts;
6214 constructor_depth = p->depth;
6216 constructor_range_stack = p->range_stack;
6217 RESTORE_SPELLING_DEPTH (constructor_depth);
6219 constructor_stack = p->next;
6222 if (ret.value == 0 && constructor_stack == 0)
6223 ret.value = error_mark_node;
6227 /* Common handling for both array range and field name designators.
6228 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6231 set_designator (int array)
6234 enum tree_code subcode;
6236 /* Don't die if an entire brace-pair level is superfluous
6237 in the containing level. */
6238 if (constructor_type == 0)
6241 /* If there were errors in this designator list already, bail out
6243 if (designator_erroneous)
6246 if (!designator_depth)
6248 gcc_assert (!constructor_range_stack);
6250 /* Designator list starts at the level of closest explicit
6252 while (constructor_stack->implicit)
6253 process_init_element (pop_init_level (1), true);
6254 constructor_designated = 1;
6258 switch (TREE_CODE (constructor_type))
6262 subtype = TREE_TYPE (constructor_fields);
6263 if (subtype != error_mark_node)
6264 subtype = TYPE_MAIN_VARIANT (subtype);
6267 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6273 subcode = TREE_CODE (subtype);
6274 if (array && subcode != ARRAY_TYPE)
6276 error_init ("array index in non-array initializer");
6279 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6281 error_init ("field name not in record or union initializer");
6285 constructor_designated = 1;
6286 push_init_level (2);
6290 /* If there are range designators in designator list, push a new designator
6291 to constructor_range_stack. RANGE_END is end of such stack range or
6292 NULL_TREE if there is no range designator at this level. */
6295 push_range_stack (tree range_end)
6297 struct constructor_range_stack *p;
6299 p = GGC_NEW (struct constructor_range_stack);
6300 p->prev = constructor_range_stack;
6302 p->fields = constructor_fields;
6303 p->range_start = constructor_index;
6304 p->index = constructor_index;
6305 p->stack = constructor_stack;
6306 p->range_end = range_end;
6307 if (constructor_range_stack)
6308 constructor_range_stack->next = p;
6309 constructor_range_stack = p;
6312 /* Within an array initializer, specify the next index to be initialized.
6313 FIRST is that index. If LAST is nonzero, then initialize a range
6314 of indices, running from FIRST through LAST. */
6317 set_init_index (tree first, tree last)
6319 if (set_designator (1))
6322 designator_erroneous = 1;
6324 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
6325 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
6327 error_init ("array index in initializer not of integer type");
6331 if (TREE_CODE (first) != INTEGER_CST)
6332 error_init ("nonconstant array index in initializer");
6333 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
6334 error_init ("nonconstant array index in initializer");
6335 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6336 error_init ("array index in non-array initializer");
6337 else if (tree_int_cst_sgn (first) == -1)
6338 error_init ("array index in initializer exceeds array bounds");
6339 else if (constructor_max_index
6340 && tree_int_cst_lt (constructor_max_index, first))
6341 error_init ("array index in initializer exceeds array bounds");
6344 constant_expression_warning (first);
6346 constant_expression_warning (last);
6347 constructor_index = convert (bitsizetype, first);
6351 if (tree_int_cst_equal (first, last))
6353 else if (tree_int_cst_lt (last, first))
6355 error_init ("empty index range in initializer");
6360 last = convert (bitsizetype, last);
6361 if (constructor_max_index != 0
6362 && tree_int_cst_lt (constructor_max_index, last))
6364 error_init ("array index range in initializer exceeds array bounds");
6371 designator_erroneous = 0;
6372 if (constructor_range_stack || last)
6373 push_range_stack (last);
6377 /* Within a struct initializer, specify the next field to be initialized. */
6380 set_init_label (tree fieldname)
6384 if (set_designator (0))
6387 designator_erroneous = 1;
6389 if (TREE_CODE (constructor_type) != RECORD_TYPE
6390 && TREE_CODE (constructor_type) != UNION_TYPE)
6392 error_init ("field name not in record or union initializer");
6396 for (tail = TYPE_FIELDS (constructor_type); tail;
6397 tail = TREE_CHAIN (tail))
6399 if (DECL_NAME (tail) == fieldname)
6404 error ("unknown field %qE specified in initializer", fieldname);
6407 constructor_fields = tail;
6409 designator_erroneous = 0;
6410 if (constructor_range_stack)
6411 push_range_stack (NULL_TREE);
6415 /* Add a new initializer to the tree of pending initializers. PURPOSE
6416 identifies the initializer, either array index or field in a structure.
6417 VALUE is the value of that index or field.
6419 IMPLICIT is true if value comes from pop_init_level (1),
6420 the new initializer has been merged with the existing one
6421 and thus no warnings should be emitted about overriding an
6422 existing initializer. */
6425 add_pending_init (tree purpose, tree value, bool implicit)
6427 struct init_node *p, **q, *r;
6429 q = &constructor_pending_elts;
6432 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6437 if (tree_int_cst_lt (purpose, p->purpose))
6439 else if (tree_int_cst_lt (p->purpose, purpose))
6445 if (TREE_SIDE_EFFECTS (p->value))
6446 warning_init (0, "initialized field with side-effects overwritten");
6447 else if (warn_override_init)
6448 warning_init (OPT_Woverride_init, "initialized field overwritten");
6459 bitpos = bit_position (purpose);
6463 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6465 else if (p->purpose != purpose)
6471 if (TREE_SIDE_EFFECTS (p->value))
6472 warning_init (0, "initialized field with side-effects overwritten");
6473 else if (warn_override_init)
6474 warning_init (OPT_Woverride_init, "initialized field overwritten");
6482 r = GGC_NEW (struct init_node);
6483 r->purpose = purpose;
6494 struct init_node *s;
6498 if (p->balance == 0)
6500 else if (p->balance < 0)
6507 p->left->parent = p;
6524 constructor_pending_elts = r;
6529 struct init_node *t = r->right;
6533 r->right->parent = r;
6538 p->left->parent = p;
6541 p->balance = t->balance < 0;
6542 r->balance = -(t->balance > 0);
6557 constructor_pending_elts = t;
6563 /* p->balance == +1; growth of left side balances the node. */
6568 else /* r == p->right */
6570 if (p->balance == 0)
6571 /* Growth propagation from right side. */
6573 else if (p->balance > 0)
6580 p->right->parent = p;
6597 constructor_pending_elts = r;
6599 else /* r->balance == -1 */
6602 struct init_node *t = r->left;
6606 r->left->parent = r;
6611 p->right->parent = p;
6614 r->balance = (t->balance < 0);
6615 p->balance = -(t->balance > 0);
6630 constructor_pending_elts = t;
6636 /* p->balance == -1; growth of right side balances the node. */
6647 /* Build AVL tree from a sorted chain. */
6650 set_nonincremental_init (void)
6652 unsigned HOST_WIDE_INT ix;
6655 if (TREE_CODE (constructor_type) != RECORD_TYPE
6656 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6659 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6660 add_pending_init (index, value, false);
6661 constructor_elements = 0;
6662 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6664 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6665 /* Skip any nameless bit fields at the beginning. */
6666 while (constructor_unfilled_fields != 0
6667 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6668 && DECL_NAME (constructor_unfilled_fields) == 0)
6669 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6672 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6674 if (TYPE_DOMAIN (constructor_type))
6675 constructor_unfilled_index
6676 = convert (bitsizetype,
6677 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6679 constructor_unfilled_index = bitsize_zero_node;
6681 constructor_incremental = 0;
6684 /* Build AVL tree from a string constant. */
6687 set_nonincremental_init_from_string (tree str)
6689 tree value, purpose, type;
6690 HOST_WIDE_INT val[2];
6691 const char *p, *end;
6692 int byte, wchar_bytes, charwidth, bitpos;
6694 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
6696 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
6697 charwidth = TYPE_PRECISION (char_type_node);
6698 type = TREE_TYPE (constructor_type);
6699 p = TREE_STRING_POINTER (str);
6700 end = p + TREE_STRING_LENGTH (str);
6702 for (purpose = bitsize_zero_node;
6703 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6704 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6706 if (wchar_bytes == 1)
6708 val[1] = (unsigned char) *p++;
6715 for (byte = 0; byte < wchar_bytes; byte++)
6717 if (BYTES_BIG_ENDIAN)
6718 bitpos = (wchar_bytes - byte - 1) * charwidth;
6720 bitpos = byte * charwidth;
6721 val[bitpos < HOST_BITS_PER_WIDE_INT]
6722 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6723 << (bitpos % HOST_BITS_PER_WIDE_INT);
6727 if (!TYPE_UNSIGNED (type))
6729 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6730 if (bitpos < HOST_BITS_PER_WIDE_INT)
6732 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6734 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6738 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6743 else if (val[0] & (((HOST_WIDE_INT) 1)
6744 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6745 val[0] |= ((HOST_WIDE_INT) -1)
6746 << (bitpos - HOST_BITS_PER_WIDE_INT);
6749 value = build_int_cst_wide (type, val[1], val[0]);
6750 add_pending_init (purpose, value, false);
6753 constructor_incremental = 0;
6756 /* Return value of FIELD in pending initializer or zero if the field was
6757 not initialized yet. */
6760 find_init_member (tree field)
6762 struct init_node *p;
6764 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6766 if (constructor_incremental
6767 && tree_int_cst_lt (field, constructor_unfilled_index))
6768 set_nonincremental_init ();
6770 p = constructor_pending_elts;
6773 if (tree_int_cst_lt (field, p->purpose))
6775 else if (tree_int_cst_lt (p->purpose, field))
6781 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6783 tree bitpos = bit_position (field);
6785 if (constructor_incremental
6786 && (!constructor_unfilled_fields
6787 || tree_int_cst_lt (bitpos,
6788 bit_position (constructor_unfilled_fields))))
6789 set_nonincremental_init ();
6791 p = constructor_pending_elts;
6794 if (field == p->purpose)
6796 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6802 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6804 if (!VEC_empty (constructor_elt, constructor_elements)
6805 && (VEC_last (constructor_elt, constructor_elements)->index
6807 return VEC_last (constructor_elt, constructor_elements)->value;
6812 /* "Output" the next constructor element.
6813 At top level, really output it to assembler code now.
6814 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6815 TYPE is the data type that the containing data type wants here.
6816 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6817 If VALUE is a string constant, STRICT_STRING is true if it is
6818 unparenthesized or we should not warn here for it being parenthesized.
6819 For other types of VALUE, STRICT_STRING is not used.
6821 PENDING if non-nil means output pending elements that belong
6822 right after this element. (PENDING is normally 1;
6823 it is 0 while outputting pending elements, to avoid recursion.)
6825 IMPLICIT is true if value comes from pop_init_level (1),
6826 the new initializer has been merged with the existing one
6827 and thus no warnings should be emitted about overriding an
6828 existing initializer. */
6831 output_init_element (tree value, bool strict_string, tree type, tree field,
6832 int pending, bool implicit)
6834 tree semantic_type = NULL_TREE;
6835 constructor_elt *celt;
6836 bool maybe_const = true;
6839 if (type == error_mark_node || value == error_mark_node)
6841 constructor_erroneous = 1;
6844 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6845 && (TREE_CODE (value) == STRING_CST
6846 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6847 && !(TREE_CODE (value) == STRING_CST
6848 && TREE_CODE (type) == ARRAY_TYPE
6849 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6850 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6851 TYPE_MAIN_VARIANT (type)))
6852 value = array_to_pointer_conversion (value);
6854 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6855 && require_constant_value && !flag_isoc99 && pending)
6857 /* As an extension, allow initializing objects with static storage
6858 duration with compound literals (which are then treated just as
6859 the brace enclosed list they contain). */
6860 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6861 value = DECL_INITIAL (decl);
6864 npc = null_pointer_constant_p (value);
6865 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
6867 semantic_type = TREE_TYPE (value);
6868 value = TREE_OPERAND (value, 0);
6870 value = c_fully_fold (value, require_constant_value, &maybe_const);
6872 if (value == error_mark_node)
6873 constructor_erroneous = 1;
6874 else if (!TREE_CONSTANT (value))
6875 constructor_constant = 0;
6876 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6877 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6878 || TREE_CODE (constructor_type) == UNION_TYPE)
6879 && DECL_C_BIT_FIELD (field)
6880 && TREE_CODE (value) != INTEGER_CST))
6881 constructor_simple = 0;
6883 constructor_nonconst = 1;
6885 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6887 if (require_constant_value)
6889 error_init ("initializer element is not constant");
6890 value = error_mark_node;
6892 else if (require_constant_elements)
6893 pedwarn (input_location, 0,
6894 "initializer element is not computable at load time");
6896 else if (!maybe_const
6897 && (require_constant_value || require_constant_elements))
6898 pedwarn_init (input_location, 0,
6899 "initializer element is not a constant expression");
6901 /* If this field is empty (and not at the end of structure),
6902 don't do anything other than checking the initializer. */
6904 && (TREE_TYPE (field) == error_mark_node
6905 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6906 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6907 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6908 || TREE_CHAIN (field)))))
6912 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
6913 value = digest_init (type, value, npc, strict_string,
6914 require_constant_value);
6915 if (value == error_mark_node)
6917 constructor_erroneous = 1;
6920 if (require_constant_value || require_constant_elements)
6921 constant_expression_warning (value);
6923 /* If this element doesn't come next in sequence,
6924 put it on constructor_pending_elts. */
6925 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6926 && (!constructor_incremental
6927 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6929 if (constructor_incremental
6930 && tree_int_cst_lt (field, constructor_unfilled_index))
6931 set_nonincremental_init ();
6933 add_pending_init (field, value, implicit);
6936 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6937 && (!constructor_incremental
6938 || field != constructor_unfilled_fields))
6940 /* We do this for records but not for unions. In a union,
6941 no matter which field is specified, it can be initialized
6942 right away since it starts at the beginning of the union. */
6943 if (constructor_incremental)
6945 if (!constructor_unfilled_fields)
6946 set_nonincremental_init ();
6949 tree bitpos, unfillpos;
6951 bitpos = bit_position (field);
6952 unfillpos = bit_position (constructor_unfilled_fields);
6954 if (tree_int_cst_lt (bitpos, unfillpos))
6955 set_nonincremental_init ();
6959 add_pending_init (field, value, implicit);
6962 else if (TREE_CODE (constructor_type) == UNION_TYPE
6963 && !VEC_empty (constructor_elt, constructor_elements))
6967 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6968 constructor_elements)->value))
6970 "initialized field with side-effects overwritten");
6971 else if (warn_override_init)
6972 warning_init (OPT_Woverride_init, "initialized field overwritten");
6975 /* We can have just one union field set. */
6976 constructor_elements = 0;
6979 /* Otherwise, output this element either to
6980 constructor_elements or to the assembler file. */
6982 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6983 celt->index = field;
6984 celt->value = value;
6986 /* Advance the variable that indicates sequential elements output. */
6987 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6988 constructor_unfilled_index
6989 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6991 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6993 constructor_unfilled_fields
6994 = TREE_CHAIN (constructor_unfilled_fields);
6996 /* Skip any nameless bit fields. */
6997 while (constructor_unfilled_fields != 0
6998 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6999 && DECL_NAME (constructor_unfilled_fields) == 0)
7000 constructor_unfilled_fields =
7001 TREE_CHAIN (constructor_unfilled_fields);
7003 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7004 constructor_unfilled_fields = 0;
7006 /* Now output any pending elements which have become next. */
7008 output_pending_init_elements (0);
7011 /* Output any pending elements which have become next.
7012 As we output elements, constructor_unfilled_{fields,index}
7013 advances, which may cause other elements to become next;
7014 if so, they too are output.
7016 If ALL is 0, we return when there are
7017 no more pending elements to output now.
7019 If ALL is 1, we output space as necessary so that
7020 we can output all the pending elements. */
7023 output_pending_init_elements (int all)
7025 struct init_node *elt = constructor_pending_elts;
7030 /* Look through the whole pending tree.
7031 If we find an element that should be output now,
7032 output it. Otherwise, set NEXT to the element
7033 that comes first among those still pending. */
7038 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7040 if (tree_int_cst_equal (elt->purpose,
7041 constructor_unfilled_index))
7042 output_init_element (elt->value, true,
7043 TREE_TYPE (constructor_type),
7044 constructor_unfilled_index, 0, false);
7045 else if (tree_int_cst_lt (constructor_unfilled_index,
7048 /* Advance to the next smaller node. */
7053 /* We have reached the smallest node bigger than the
7054 current unfilled index. Fill the space first. */
7055 next = elt->purpose;
7061 /* Advance to the next bigger node. */
7066 /* We have reached the biggest node in a subtree. Find
7067 the parent of it, which is the next bigger node. */
7068 while (elt->parent && elt->parent->right == elt)
7071 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7074 next = elt->purpose;
7080 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7081 || TREE_CODE (constructor_type) == UNION_TYPE)
7083 tree ctor_unfilled_bitpos, elt_bitpos;
7085 /* If the current record is complete we are done. */
7086 if (constructor_unfilled_fields == 0)
7089 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7090 elt_bitpos = bit_position (elt->purpose);
7091 /* We can't compare fields here because there might be empty
7092 fields in between. */
7093 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7095 constructor_unfilled_fields = elt->purpose;
7096 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
7097 elt->purpose, 0, false);
7099 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7101 /* Advance to the next smaller node. */
7106 /* We have reached the smallest node bigger than the
7107 current unfilled field. Fill the space first. */
7108 next = elt->purpose;
7114 /* Advance to the next bigger node. */
7119 /* We have reached the biggest node in a subtree. Find
7120 the parent of it, which is the next bigger node. */
7121 while (elt->parent && elt->parent->right == elt)
7125 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7126 bit_position (elt->purpose))))
7128 next = elt->purpose;
7136 /* Ordinarily return, but not if we want to output all
7137 and there are elements left. */
7138 if (!(all && next != 0))
7141 /* If it's not incremental, just skip over the gap, so that after
7142 jumping to retry we will output the next successive element. */
7143 if (TREE_CODE (constructor_type) == RECORD_TYPE
7144 || TREE_CODE (constructor_type) == UNION_TYPE)
7145 constructor_unfilled_fields = next;
7146 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7147 constructor_unfilled_index = next;
7149 /* ELT now points to the node in the pending tree with the next
7150 initializer to output. */
7154 /* Add one non-braced element to the current constructor level.
7155 This adjusts the current position within the constructor's type.
7156 This may also start or terminate implicit levels
7157 to handle a partly-braced initializer.
7159 Once this has found the correct level for the new element,
7160 it calls output_init_element.
7162 IMPLICIT is true if value comes from pop_init_level (1),
7163 the new initializer has been merged with the existing one
7164 and thus no warnings should be emitted about overriding an
7165 existing initializer. */
7168 process_init_element (struct c_expr value, bool implicit)
7170 tree orig_value = value.value;
7171 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7172 bool strict_string = value.original_code == STRING_CST;
7174 designator_depth = 0;
7175 designator_erroneous = 0;
7177 /* Handle superfluous braces around string cst as in
7178 char x[] = {"foo"}; */
7181 && TREE_CODE (constructor_type) == ARRAY_TYPE
7182 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7183 && integer_zerop (constructor_unfilled_index))
7185 if (constructor_stack->replacement_value.value)
7186 error_init ("excess elements in char array initializer");
7187 constructor_stack->replacement_value = value;
7191 if (constructor_stack->replacement_value.value != 0)
7193 error_init ("excess elements in struct initializer");
7197 /* Ignore elements of a brace group if it is entirely superfluous
7198 and has already been diagnosed. */
7199 if (constructor_type == 0)
7202 /* If we've exhausted any levels that didn't have braces,
7204 while (constructor_stack->implicit)
7206 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7207 || TREE_CODE (constructor_type) == UNION_TYPE)
7208 && constructor_fields == 0)
7209 process_init_element (pop_init_level (1), true);
7210 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7211 && (constructor_max_index == 0
7212 || tree_int_cst_lt (constructor_max_index,
7213 constructor_index)))
7214 process_init_element (pop_init_level (1), true);
7219 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7220 if (constructor_range_stack)
7222 /* If value is a compound literal and we'll be just using its
7223 content, don't put it into a SAVE_EXPR. */
7224 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7225 || !require_constant_value
7228 tree semantic_type = NULL_TREE;
7229 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7231 semantic_type = TREE_TYPE (value.value);
7232 value.value = TREE_OPERAND (value.value, 0);
7234 value.value = c_save_expr (value.value);
7236 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7243 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7246 enum tree_code fieldcode;
7248 if (constructor_fields == 0)
7250 pedwarn_init (input_location, 0,
7251 "excess elements in struct initializer");
7255 fieldtype = TREE_TYPE (constructor_fields);
7256 if (fieldtype != error_mark_node)
7257 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7258 fieldcode = TREE_CODE (fieldtype);
7260 /* Error for non-static initialization of a flexible array member. */
7261 if (fieldcode == ARRAY_TYPE
7262 && !require_constant_value
7263 && TYPE_SIZE (fieldtype) == NULL_TREE
7264 && TREE_CHAIN (constructor_fields) == NULL_TREE)
7266 error_init ("non-static initialization of a flexible array member");
7270 /* Accept a string constant to initialize a subarray. */
7271 if (value.value != 0
7272 && fieldcode == ARRAY_TYPE
7273 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7275 value.value = orig_value;
7276 /* Otherwise, if we have come to a subaggregate,
7277 and we don't have an element of its type, push into it. */
7278 else if (value.value != 0
7279 && value.value != error_mark_node
7280 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7281 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7282 || fieldcode == UNION_TYPE))
7284 push_init_level (1);
7290 push_member_name (constructor_fields);
7291 output_init_element (value.value, strict_string,
7292 fieldtype, constructor_fields, 1, implicit);
7293 RESTORE_SPELLING_DEPTH (constructor_depth);
7296 /* Do the bookkeeping for an element that was
7297 directly output as a constructor. */
7299 /* For a record, keep track of end position of last field. */
7300 if (DECL_SIZE (constructor_fields))
7301 constructor_bit_index
7302 = size_binop (PLUS_EXPR,
7303 bit_position (constructor_fields),
7304 DECL_SIZE (constructor_fields));
7306 /* If the current field was the first one not yet written out,
7307 it isn't now, so update. */
7308 if (constructor_unfilled_fields == constructor_fields)
7310 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7311 /* Skip any nameless bit fields. */
7312 while (constructor_unfilled_fields != 0
7313 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7314 && DECL_NAME (constructor_unfilled_fields) == 0)
7315 constructor_unfilled_fields =
7316 TREE_CHAIN (constructor_unfilled_fields);
7320 constructor_fields = TREE_CHAIN (constructor_fields);
7321 /* Skip any nameless bit fields at the beginning. */
7322 while (constructor_fields != 0
7323 && DECL_C_BIT_FIELD (constructor_fields)
7324 && DECL_NAME (constructor_fields) == 0)
7325 constructor_fields = TREE_CHAIN (constructor_fields);
7327 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7330 enum tree_code fieldcode;
7332 if (constructor_fields == 0)
7334 pedwarn_init (input_location, 0,
7335 "excess elements in union initializer");
7339 fieldtype = TREE_TYPE (constructor_fields);
7340 if (fieldtype != error_mark_node)
7341 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7342 fieldcode = TREE_CODE (fieldtype);
7344 /* Warn that traditional C rejects initialization of unions.
7345 We skip the warning if the value is zero. This is done
7346 under the assumption that the zero initializer in user
7347 code appears conditioned on e.g. __STDC__ to avoid
7348 "missing initializer" warnings and relies on default
7349 initialization to zero in the traditional C case.
7350 We also skip the warning if the initializer is designated,
7351 again on the assumption that this must be conditional on
7352 __STDC__ anyway (and we've already complained about the
7353 member-designator already). */
7354 if (!in_system_header && !constructor_designated
7355 && !(value.value && (integer_zerop (value.value)
7356 || real_zerop (value.value))))
7357 warning (OPT_Wtraditional, "traditional C rejects initialization "
7360 /* Accept a string constant to initialize a subarray. */
7361 if (value.value != 0
7362 && fieldcode == ARRAY_TYPE
7363 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7365 value.value = orig_value;
7366 /* Otherwise, if we have come to a subaggregate,
7367 and we don't have an element of its type, push into it. */
7368 else if (value.value != 0
7369 && value.value != error_mark_node
7370 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7371 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7372 || fieldcode == UNION_TYPE))
7374 push_init_level (1);
7380 push_member_name (constructor_fields);
7381 output_init_element (value.value, strict_string,
7382 fieldtype, constructor_fields, 1, implicit);
7383 RESTORE_SPELLING_DEPTH (constructor_depth);
7386 /* Do the bookkeeping for an element that was
7387 directly output as a constructor. */
7389 constructor_bit_index = DECL_SIZE (constructor_fields);
7390 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7393 constructor_fields = 0;
7395 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7397 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7398 enum tree_code eltcode = TREE_CODE (elttype);
7400 /* Accept a string constant to initialize a subarray. */
7401 if (value.value != 0
7402 && eltcode == ARRAY_TYPE
7403 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
7405 value.value = orig_value;
7406 /* Otherwise, if we have come to a subaggregate,
7407 and we don't have an element of its type, push into it. */
7408 else if (value.value != 0
7409 && value.value != error_mark_node
7410 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
7411 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
7412 || eltcode == UNION_TYPE))
7414 push_init_level (1);
7418 if (constructor_max_index != 0
7419 && (tree_int_cst_lt (constructor_max_index, constructor_index)
7420 || integer_all_onesp (constructor_max_index)))
7422 pedwarn_init (input_location, 0,
7423 "excess elements in array initializer");
7427 /* Now output the actual element. */
7430 push_array_bounds (tree_low_cst (constructor_index, 1));
7431 output_init_element (value.value, strict_string,
7432 elttype, constructor_index, 1, implicit);
7433 RESTORE_SPELLING_DEPTH (constructor_depth);
7437 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7440 /* If we are doing the bookkeeping for an element that was
7441 directly output as a constructor, we must update
7442 constructor_unfilled_index. */
7443 constructor_unfilled_index = constructor_index;
7445 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7447 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7449 /* Do a basic check of initializer size. Note that vectors
7450 always have a fixed size derived from their type. */
7451 if (tree_int_cst_lt (constructor_max_index, constructor_index))
7453 pedwarn_init (input_location, 0,
7454 "excess elements in vector initializer");
7458 /* Now output the actual element. */
7460 output_init_element (value.value, strict_string,
7461 elttype, constructor_index, 1, implicit);
7464 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7467 /* If we are doing the bookkeeping for an element that was
7468 directly output as a constructor, we must update
7469 constructor_unfilled_index. */
7470 constructor_unfilled_index = constructor_index;
7473 /* Handle the sole element allowed in a braced initializer
7474 for a scalar variable. */
7475 else if (constructor_type != error_mark_node
7476 && constructor_fields == 0)
7478 pedwarn_init (input_location, 0,
7479 "excess elements in scalar initializer");
7485 output_init_element (value.value, strict_string,
7486 constructor_type, NULL_TREE, 1, implicit);
7487 constructor_fields = 0;
7490 /* Handle range initializers either at this level or anywhere higher
7491 in the designator stack. */
7492 if (constructor_range_stack)
7494 struct constructor_range_stack *p, *range_stack;
7497 range_stack = constructor_range_stack;
7498 constructor_range_stack = 0;
7499 while (constructor_stack != range_stack->stack)
7501 gcc_assert (constructor_stack->implicit);
7502 process_init_element (pop_init_level (1), true);
7504 for (p = range_stack;
7505 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
7508 gcc_assert (constructor_stack->implicit);
7509 process_init_element (pop_init_level (1), true);
7512 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
7513 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
7518 constructor_index = p->index;
7519 constructor_fields = p->fields;
7520 if (finish && p->range_end && p->index == p->range_start)
7528 push_init_level (2);
7529 p->stack = constructor_stack;
7530 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
7531 p->index = p->range_start;
7535 constructor_range_stack = range_stack;
7542 constructor_range_stack = 0;
7545 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7546 (guaranteed to be 'volatile' or null) and ARGS (represented using
7547 an ASM_EXPR node). */
7549 build_asm_stmt (tree cv_qualifier, tree args)
7551 if (!ASM_VOLATILE_P (args) && cv_qualifier)
7552 ASM_VOLATILE_P (args) = 1;
7553 return add_stmt (args);
7556 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7557 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7558 SIMPLE indicates whether there was anything at all after the
7559 string in the asm expression -- asm("blah") and asm("blah" : )
7560 are subtly different. We use a ASM_EXPR node to represent this. */
7562 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
7568 const char *constraint;
7569 const char **oconstraints;
7570 bool allows_mem, allows_reg, is_inout;
7571 int ninputs, noutputs;
7573 ninputs = list_length (inputs);
7574 noutputs = list_length (outputs);
7575 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
7577 string = resolve_asm_operand_names (string, outputs, inputs);
7579 /* Remove output conversions that change the type but not the mode. */
7580 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
7582 tree output = TREE_VALUE (tail);
7584 /* ??? Really, this should not be here. Users should be using a
7585 proper lvalue, dammit. But there's a long history of using casts
7586 in the output operands. In cases like longlong.h, this becomes a
7587 primitive form of typechecking -- if the cast can be removed, then
7588 the output operand had a type of the proper width; otherwise we'll
7589 get an error. Gross, but ... */
7590 STRIP_NOPS (output);
7592 if (!lvalue_or_else (output, lv_asm))
7593 output = error_mark_node;
7595 if (output != error_mark_node
7596 && (TREE_READONLY (output)
7597 || TYPE_READONLY (TREE_TYPE (output))
7598 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
7599 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
7600 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
7601 readonly_error (output, lv_asm);
7603 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7604 oconstraints[i] = constraint;
7606 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7607 &allows_mem, &allows_reg, &is_inout))
7609 /* If the operand is going to end up in memory,
7610 mark it addressable. */
7611 if (!allows_reg && !c_mark_addressable (output))
7612 output = error_mark_node;
7615 output = error_mark_node;
7617 TREE_VALUE (tail) = output;
7620 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
7624 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7625 input = TREE_VALUE (tail);
7627 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
7628 oconstraints, &allows_mem, &allows_reg))
7630 /* If the operand is going to end up in memory,
7631 mark it addressable. */
7632 if (!allows_reg && allows_mem)
7634 /* Strip the nops as we allow this case. FIXME, this really
7635 should be rejected or made deprecated. */
7637 if (!c_mark_addressable (input))
7638 input = error_mark_node;
7642 input = error_mark_node;
7644 TREE_VALUE (tail) = input;
7647 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
7649 /* asm statements without outputs, including simple ones, are treated
7651 ASM_INPUT_P (args) = simple;
7652 ASM_VOLATILE_P (args) = (noutputs == 0);
7657 /* Generate a goto statement to LABEL. */
7660 c_finish_goto_label (tree label)
7662 tree decl = lookup_label (label);
7666 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
7668 error ("jump into statement expression");
7672 if (C_DECL_UNJUMPABLE_VM (decl))
7674 error ("jump into scope of identifier with variably modified type");
7678 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
7680 /* No jump from outside this statement expression context, so
7681 record that there is a jump from within this context. */
7682 struct c_label_list *nlist;
7683 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7684 nlist->next = label_context_stack_se->labels_used;
7685 nlist->label = decl;
7686 label_context_stack_se->labels_used = nlist;
7689 if (!C_DECL_UNDEFINABLE_VM (decl))
7691 /* No jump from outside this context context of identifiers with
7692 variably modified type, so record that there is a jump from
7693 within this context. */
7694 struct c_label_list *nlist;
7695 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7696 nlist->next = label_context_stack_vm->labels_used;
7697 nlist->label = decl;
7698 label_context_stack_vm->labels_used = nlist;
7701 TREE_USED (decl) = 1;
7702 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
7705 /* Generate a computed goto statement to EXPR. */
7708 c_finish_goto_ptr (tree expr)
7710 pedwarn (input_location, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
7711 expr = c_fully_fold (expr, false, NULL);
7712 expr = convert (ptr_type_node, expr);
7713 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
7716 /* Generate a C `return' statement. RETVAL is the expression for what
7717 to return, or a null pointer for `return;' with no value. */
7720 c_finish_return (tree retval)
7722 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
7723 bool no_warning = false;
7726 if (TREE_THIS_VOLATILE (current_function_decl))
7727 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7731 tree semantic_type = NULL_TREE;
7732 npc = null_pointer_constant_p (retval);
7733 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
7735 semantic_type = TREE_TYPE (retval);
7736 retval = TREE_OPERAND (retval, 0);
7738 retval = c_fully_fold (retval, false, NULL);
7740 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
7745 current_function_returns_null = 1;
7746 if ((warn_return_type || flag_isoc99)
7747 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7749 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wreturn_type,
7750 "%<return%> with no value, in "
7751 "function returning non-void");
7755 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7757 current_function_returns_null = 1;
7758 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7759 pedwarn (input_location, 0,
7760 "%<return%> with a value, in function returning void");
7762 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
7763 "%<return%> with expression, in function returning void");
7767 tree t = convert_for_assignment (valtype, retval, ic_return, npc,
7768 NULL_TREE, NULL_TREE, 0);
7769 tree res = DECL_RESULT (current_function_decl);
7772 current_function_returns_value = 1;
7773 if (t == error_mark_node)
7776 inner = t = convert (TREE_TYPE (res), t);
7778 /* Strip any conversions, additions, and subtractions, and see if
7779 we are returning the address of a local variable. Warn if so. */
7782 switch (TREE_CODE (inner))
7785 case NON_LVALUE_EXPR:
7787 case POINTER_PLUS_EXPR:
7788 inner = TREE_OPERAND (inner, 0);
7792 /* If the second operand of the MINUS_EXPR has a pointer
7793 type (or is converted from it), this may be valid, so
7794 don't give a warning. */
7796 tree op1 = TREE_OPERAND (inner, 1);
7798 while (!POINTER_TYPE_P (TREE_TYPE (op1))
7799 && (CONVERT_EXPR_P (op1)
7800 || TREE_CODE (op1) == NON_LVALUE_EXPR))
7801 op1 = TREE_OPERAND (op1, 0);
7803 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7806 inner = TREE_OPERAND (inner, 0);
7811 inner = TREE_OPERAND (inner, 0);
7813 while (REFERENCE_CLASS_P (inner)
7814 && TREE_CODE (inner) != INDIRECT_REF)
7815 inner = TREE_OPERAND (inner, 0);
7818 && !DECL_EXTERNAL (inner)
7819 && !TREE_STATIC (inner)
7820 && DECL_CONTEXT (inner) == current_function_decl)
7821 warning (0, "function returns address of local variable");
7831 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
7833 if (warn_sequence_point)
7834 verify_sequence_points (retval);
7837 ret_stmt = build_stmt (RETURN_EXPR, retval);
7838 TREE_NO_WARNING (ret_stmt) |= no_warning;
7839 return add_stmt (ret_stmt);
7843 /* The SWITCH_EXPR being built. */
7846 /* The original type of the testing expression, i.e. before the
7847 default conversion is applied. */
7850 /* A splay-tree mapping the low element of a case range to the high
7851 element, or NULL_TREE if there is no high element. Used to
7852 determine whether or not a new case label duplicates an old case
7853 label. We need a tree, rather than simply a hash table, because
7854 of the GNU case range extension. */
7857 /* Number of nested statement expressions within this switch
7858 statement; if nonzero, case and default labels may not
7860 unsigned int blocked_stmt_expr;
7862 /* Scope of outermost declarations of identifiers with variably
7863 modified type within this switch statement; if nonzero, case and
7864 default labels may not appear. */
7865 unsigned int blocked_vm;
7867 /* The next node on the stack. */
7868 struct c_switch *next;
7871 /* A stack of the currently active switch statements. The innermost
7872 switch statement is on the top of the stack. There is no need to
7873 mark the stack for garbage collection because it is only active
7874 during the processing of the body of a function, and we never
7875 collect at that point. */
7877 struct c_switch *c_switch_stack;
7879 /* Start a C switch statement, testing expression EXP. Return the new
7883 c_start_case (tree exp)
7885 tree orig_type = error_mark_node;
7886 struct c_switch *cs;
7888 if (exp != error_mark_node)
7890 orig_type = TREE_TYPE (exp);
7892 if (!INTEGRAL_TYPE_P (orig_type))
7894 if (orig_type != error_mark_node)
7896 error ("switch quantity not an integer");
7897 orig_type = error_mark_node;
7899 exp = integer_zero_node;
7903 tree type = TYPE_MAIN_VARIANT (orig_type);
7905 if (!in_system_header
7906 && (type == long_integer_type_node
7907 || type == long_unsigned_type_node))
7908 warning (OPT_Wtraditional, "%<long%> switch expression not "
7909 "converted to %<int%> in ISO C");
7911 exp = c_fully_fold (exp, false, NULL);
7912 exp = default_conversion (exp);
7914 if (warn_sequence_point)
7915 verify_sequence_points (exp);
7919 /* Add this new SWITCH_EXPR to the stack. */
7920 cs = XNEW (struct c_switch);
7921 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7922 cs->orig_type = orig_type;
7923 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7924 cs->blocked_stmt_expr = 0;
7926 cs->next = c_switch_stack;
7927 c_switch_stack = cs;
7929 return add_stmt (cs->switch_expr);
7932 /* Process a case label. */
7935 do_case (tree low_value, tree high_value)
7937 tree label = NULL_TREE;
7939 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
7941 low_value = c_fully_fold (low_value, false, NULL);
7942 if (TREE_CODE (low_value) == INTEGER_CST)
7943 pedwarn (input_location, OPT_pedantic,
7944 "case label is not an integer constant expression");
7947 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
7949 high_value = c_fully_fold (high_value, false, NULL);
7950 if (TREE_CODE (high_value) == INTEGER_CST)
7951 pedwarn (input_location, OPT_pedantic,
7952 "case label is not an integer constant expression");
7955 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7956 && !c_switch_stack->blocked_vm)
7958 label = c_add_case_label (c_switch_stack->cases,
7959 SWITCH_COND (c_switch_stack->switch_expr),
7960 c_switch_stack->orig_type,
7961 low_value, high_value);
7962 if (label == error_mark_node)
7965 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7968 error ("case label in statement expression not containing "
7969 "enclosing switch statement");
7971 error ("%<default%> label in statement expression not containing "
7972 "enclosing switch statement");
7974 else if (c_switch_stack && c_switch_stack->blocked_vm)
7977 error ("case label in scope of identifier with variably modified "
7978 "type not containing enclosing switch statement");
7980 error ("%<default%> label in scope of identifier with variably "
7981 "modified type not containing enclosing switch statement");
7984 error ("case label not within a switch statement");
7986 error ("%<default%> label not within a switch statement");
7991 /* Finish the switch statement. */
7994 c_finish_case (tree body)
7996 struct c_switch *cs = c_switch_stack;
7997 location_t switch_location;
7999 SWITCH_BODY (cs->switch_expr) = body;
8001 /* We must not be within a statement expression nested in the switch
8002 at this point; we might, however, be within the scope of an
8003 identifier with variably modified type nested in the switch. */
8004 gcc_assert (!cs->blocked_stmt_expr);
8006 /* Emit warnings as needed. */
8007 if (EXPR_HAS_LOCATION (cs->switch_expr))
8008 switch_location = EXPR_LOCATION (cs->switch_expr);
8010 switch_location = input_location;
8011 c_do_switch_warnings (cs->cases, switch_location,
8012 TREE_TYPE (cs->switch_expr),
8013 SWITCH_COND (cs->switch_expr));
8015 /* Pop the stack. */
8016 c_switch_stack = cs->next;
8017 splay_tree_delete (cs->cases);
8021 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8022 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8023 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8024 statement, and was not surrounded with parenthesis. */
8027 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8028 tree else_block, bool nested_if)
8032 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8033 if (warn_parentheses && nested_if && else_block == NULL)
8035 tree inner_if = then_block;
8037 /* We know from the grammar productions that there is an IF nested
8038 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8039 it might not be exactly THEN_BLOCK, but should be the last
8040 non-container statement within. */
8042 switch (TREE_CODE (inner_if))
8047 inner_if = BIND_EXPR_BODY (inner_if);
8049 case STATEMENT_LIST:
8050 inner_if = expr_last (then_block);
8052 case TRY_FINALLY_EXPR:
8053 case TRY_CATCH_EXPR:
8054 inner_if = TREE_OPERAND (inner_if, 0);
8061 if (COND_EXPR_ELSE (inner_if))
8062 warning (OPT_Wparentheses,
8063 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
8067 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8068 SET_EXPR_LOCATION (stmt, if_locus);
8072 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8073 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8074 is false for DO loops. INCR is the FOR increment expression. BODY is
8075 the statement controlled by the loop. BLAB is the break label. CLAB is
8076 the continue label. Everything is allowed to be NULL. */
8079 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8080 tree blab, tree clab, bool cond_is_first)
8082 tree entry = NULL, exit = NULL, t;
8084 /* If the condition is zero don't generate a loop construct. */
8085 if (cond && integer_zerop (cond))
8089 t = build_and_jump (&blab);
8090 SET_EXPR_LOCATION (t, start_locus);
8096 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8098 /* If we have an exit condition, then we build an IF with gotos either
8099 out of the loop, or to the top of it. If there's no exit condition,
8100 then we just build a jump back to the top. */
8101 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8103 if (cond && !integer_nonzerop (cond))
8105 /* Canonicalize the loop condition to the end. This means
8106 generating a branch to the loop condition. Reuse the
8107 continue label, if possible. */
8112 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8113 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8116 t = build1 (GOTO_EXPR, void_type_node, clab);
8117 SET_EXPR_LOCATION (t, start_locus);
8121 t = build_and_jump (&blab);
8122 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
8124 SET_EXPR_LOCATION (exit, start_locus);
8126 SET_EXPR_LOCATION (exit, input_location);
8135 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8143 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8147 c_finish_bc_stmt (tree *label_p, bool is_break)
8150 tree label = *label_p;
8152 /* In switch statements break is sometimes stylistically used after
8153 a return statement. This can lead to spurious warnings about
8154 control reaching the end of a non-void function when it is
8155 inlined. Note that we are calling block_may_fallthru with
8156 language specific tree nodes; this works because
8157 block_may_fallthru returns true when given something it does not
8159 skip = !block_may_fallthru (cur_stmt_list);
8164 *label_p = label = create_artificial_label ();
8166 else if (TREE_CODE (label) == LABEL_DECL)
8168 else switch (TREE_INT_CST_LOW (label))
8172 error ("break statement not within loop or switch");
8174 error ("continue statement not within a loop");
8178 gcc_assert (is_break);
8179 error ("break statement used with OpenMP for loop");
8190 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8192 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8195 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8198 emit_side_effect_warnings (tree expr)
8200 if (expr == error_mark_node)
8202 else if (!TREE_SIDE_EFFECTS (expr))
8204 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8205 warning (OPT_Wunused_value, "%Hstatement with no effect",
8206 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
8209 warn_if_unused_value (expr, input_location);
8212 /* Process an expression as if it were a complete statement. Emit
8213 diagnostics, but do not call ADD_STMT. */
8216 c_process_expr_stmt (tree expr)
8221 expr = c_fully_fold (expr, false, NULL);
8223 if (warn_sequence_point)
8224 verify_sequence_points (expr);
8226 if (TREE_TYPE (expr) != error_mark_node
8227 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8228 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8229 error ("expression statement has incomplete type");
8231 /* If we're not processing a statement expression, warn about unused values.
8232 Warnings for statement expressions will be emitted later, once we figure
8233 out which is the result. */
8234 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8235 && warn_unused_value)
8236 emit_side_effect_warnings (expr);
8238 /* If the expression is not of a type to which we cannot assign a line
8239 number, wrap the thing in a no-op NOP_EXPR. */
8240 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8241 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8243 if (CAN_HAVE_LOCATION_P (expr))
8244 SET_EXPR_LOCATION (expr, input_location);
8249 /* Emit an expression as a statement. */
8252 c_finish_expr_stmt (tree expr)
8255 return add_stmt (c_process_expr_stmt (expr));
8260 /* Do the opposite and emit a statement as an expression. To begin,
8261 create a new binding level and return it. */
8264 c_begin_stmt_expr (void)
8267 struct c_label_context_se *nstack;
8268 struct c_label_list *glist;
8270 /* We must force a BLOCK for this level so that, if it is not expanded
8271 later, there is a way to turn off the entire subtree of blocks that
8272 are contained in it. */
8274 ret = c_begin_compound_stmt (true);
8277 c_switch_stack->blocked_stmt_expr++;
8278 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8280 for (glist = label_context_stack_se->labels_used;
8282 glist = glist->next)
8284 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
8286 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
8287 nstack->labels_def = NULL;
8288 nstack->labels_used = NULL;
8289 nstack->next = label_context_stack_se;
8290 label_context_stack_se = nstack;
8292 /* Mark the current statement list as belonging to a statement list. */
8293 STATEMENT_LIST_STMT_EXPR (ret) = 1;
8299 c_finish_stmt_expr (tree body)
8301 tree last, type, tmp, val;
8303 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8305 body = c_end_compound_stmt (body, true);
8308 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8309 c_switch_stack->blocked_stmt_expr--;
8311 /* It is no longer possible to jump to labels defined within this
8312 statement expression. */
8313 for (dlist = label_context_stack_se->labels_def;
8315 dlist = dlist->next)
8317 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
8319 /* It is again possible to define labels with a goto just outside
8320 this statement expression. */
8321 for (glist = label_context_stack_se->next->labels_used;
8323 glist = glist->next)
8325 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
8328 if (glist_prev != NULL)
8329 glist_prev->next = label_context_stack_se->labels_used;
8331 label_context_stack_se->next->labels_used
8332 = label_context_stack_se->labels_used;
8333 label_context_stack_se = label_context_stack_se->next;
8335 /* Locate the last statement in BODY. See c_end_compound_stmt
8336 about always returning a BIND_EXPR. */
8337 last_p = &BIND_EXPR_BODY (body);
8338 last = BIND_EXPR_BODY (body);
8341 if (TREE_CODE (last) == STATEMENT_LIST)
8343 tree_stmt_iterator i;
8345 /* This can happen with degenerate cases like ({ }). No value. */
8346 if (!TREE_SIDE_EFFECTS (last))
8349 /* If we're supposed to generate side effects warnings, process
8350 all of the statements except the last. */
8351 if (warn_unused_value)
8353 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8354 emit_side_effect_warnings (tsi_stmt (i));
8357 i = tsi_last (last);
8358 last_p = tsi_stmt_ptr (i);
8362 /* If the end of the list is exception related, then the list was split
8363 by a call to push_cleanup. Continue searching. */
8364 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8365 || TREE_CODE (last) == TRY_CATCH_EXPR)
8367 last_p = &TREE_OPERAND (last, 0);
8369 goto continue_searching;
8372 /* In the case that the BIND_EXPR is not necessary, return the
8373 expression out from inside it. */
8374 if (last == error_mark_node
8375 || (last == BIND_EXPR_BODY (body)
8376 && BIND_EXPR_VARS (body) == NULL))
8378 /* Even if this looks constant, do not allow it in a constant
8380 last = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (last), NULL_TREE, last);
8381 C_MAYBE_CONST_EXPR_NON_CONST (last) = 1;
8382 /* Do not warn if the return value of a statement expression is
8384 TREE_NO_WARNING (last) = 1;
8388 /* Extract the type of said expression. */
8389 type = TREE_TYPE (last);
8391 /* If we're not returning a value at all, then the BIND_EXPR that
8392 we already have is a fine expression to return. */
8393 if (!type || VOID_TYPE_P (type))
8396 /* Now that we've located the expression containing the value, it seems
8397 silly to make voidify_wrapper_expr repeat the process. Create a
8398 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8399 tmp = create_tmp_var_raw (type, NULL);
8401 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8402 tree_expr_nonnegative_p giving up immediately. */
8404 if (TREE_CODE (val) == NOP_EXPR
8405 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
8406 val = TREE_OPERAND (val, 0);
8408 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
8409 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
8411 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
8414 /* Begin the scope of an identifier of variably modified type, scope
8415 number SCOPE. Jumping from outside this scope to inside it is not
8419 c_begin_vm_scope (unsigned int scope)
8421 struct c_label_context_vm *nstack;
8422 struct c_label_list *glist;
8424 gcc_assert (scope > 0);
8426 /* At file_scope, we don't have to do any processing. */
8427 if (label_context_stack_vm == NULL)
8430 if (c_switch_stack && !c_switch_stack->blocked_vm)
8431 c_switch_stack->blocked_vm = scope;
8432 for (glist = label_context_stack_vm->labels_used;
8434 glist = glist->next)
8436 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
8438 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
8439 nstack->labels_def = NULL;
8440 nstack->labels_used = NULL;
8441 nstack->scope = scope;
8442 nstack->next = label_context_stack_vm;
8443 label_context_stack_vm = nstack;
8446 /* End a scope which may contain identifiers of variably modified
8447 type, scope number SCOPE. */
8450 c_end_vm_scope (unsigned int scope)
8452 if (label_context_stack_vm == NULL)
8454 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
8455 c_switch_stack->blocked_vm = 0;
8456 /* We may have a number of nested scopes of identifiers with
8457 variably modified type, all at this depth. Pop each in turn. */
8458 while (label_context_stack_vm->scope == scope)
8460 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8462 /* It is no longer possible to jump to labels defined within this
8464 for (dlist = label_context_stack_vm->labels_def;
8466 dlist = dlist->next)
8468 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
8470 /* It is again possible to define labels with a goto just outside
8472 for (glist = label_context_stack_vm->next->labels_used;
8474 glist = glist->next)
8476 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
8479 if (glist_prev != NULL)
8480 glist_prev->next = label_context_stack_vm->labels_used;
8482 label_context_stack_vm->next->labels_used
8483 = label_context_stack_vm->labels_used;
8484 label_context_stack_vm = label_context_stack_vm->next;
8488 /* Begin and end compound statements. This is as simple as pushing
8489 and popping new statement lists from the tree. */
8492 c_begin_compound_stmt (bool do_scope)
8494 tree stmt = push_stmt_list ();
8501 c_end_compound_stmt (tree stmt, bool do_scope)
8507 if (c_dialect_objc ())
8508 objc_clear_super_receiver ();
8509 block = pop_scope ();
8512 stmt = pop_stmt_list (stmt);
8513 stmt = c_build_bind_expr (block, stmt);
8515 /* If this compound statement is nested immediately inside a statement
8516 expression, then force a BIND_EXPR to be created. Otherwise we'll
8517 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8518 STATEMENT_LISTs merge, and thus we can lose track of what statement
8521 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8522 && TREE_CODE (stmt) != BIND_EXPR)
8524 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
8525 TREE_SIDE_EFFECTS (stmt) = 1;
8531 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8532 when the current scope is exited. EH_ONLY is true when this is not
8533 meant to apply to normal control flow transfer. */
8536 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
8538 enum tree_code code;
8542 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
8543 stmt = build_stmt (code, NULL, cleanup);
8545 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
8546 list = push_stmt_list ();
8547 TREE_OPERAND (stmt, 0) = list;
8548 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
8551 /* Build a binary-operation expression without default conversions.
8552 CODE is the kind of expression to build.
8553 LOCATION is the operator's location.
8554 This function differs from `build' in several ways:
8555 the data type of the result is computed and recorded in it,
8556 warnings are generated if arg data types are invalid,
8557 special handling for addition and subtraction of pointers is known,
8558 and some optimization is done (operations on narrow ints
8559 are done in the narrower type when that gives the same result).
8560 Constant folding is also done before the result is returned.
8562 Note that the operands will never have enumeral types, or function
8563 or array types, because either they will have the default conversions
8564 performed or they have both just been converted to some other type in which
8565 the arithmetic is to be done. */
8568 build_binary_op (location_t location, enum tree_code code,
8569 tree orig_op0, tree orig_op1, int convert_p)
8571 tree type0, type1, orig_type0, orig_type1;
8573 enum tree_code code0, code1;
8575 tree ret = error_mark_node;
8576 const char *invalid_op_diag;
8577 bool op0_int_operands, op1_int_operands;
8578 bool int_const, int_const_or_overflow, int_operands;
8580 /* Expression code to give to the expression when it is built.
8581 Normally this is CODE, which is what the caller asked for,
8582 but in some special cases we change it. */
8583 enum tree_code resultcode = code;
8585 /* Data type in which the computation is to be performed.
8586 In the simplest cases this is the common type of the arguments. */
8587 tree result_type = NULL;
8589 /* When the computation is in excess precision, the type of the
8590 final EXCESS_PRECISION_EXPR. */
8591 tree real_result_type = NULL;
8593 /* Nonzero means operands have already been type-converted
8594 in whatever way is necessary.
8595 Zero means they need to be converted to RESULT_TYPE. */
8598 /* Nonzero means create the expression with this type, rather than
8600 tree build_type = 0;
8602 /* Nonzero means after finally constructing the expression
8603 convert it to this type. */
8604 tree final_type = 0;
8606 /* Nonzero if this is an operation like MIN or MAX which can
8607 safely be computed in short if both args are promoted shorts.
8608 Also implies COMMON.
8609 -1 indicates a bitwise operation; this makes a difference
8610 in the exact conditions for when it is safe to do the operation
8611 in a narrower mode. */
8614 /* Nonzero if this is a comparison operation;
8615 if both args are promoted shorts, compare the original shorts.
8616 Also implies COMMON. */
8617 int short_compare = 0;
8619 /* Nonzero if this is a right-shift operation, which can be computed on the
8620 original short and then promoted if the operand is a promoted short. */
8621 int short_shift = 0;
8623 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8626 /* True means types are compatible as far as ObjC is concerned. */
8629 /* True means this is an arithmetic operation that may need excess
8631 bool may_need_excess_precision;
8633 if (location == UNKNOWN_LOCATION)
8634 location = input_location;
8639 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
8640 if (op0_int_operands)
8641 op0 = remove_c_maybe_const_expr (op0);
8642 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
8643 if (op1_int_operands)
8644 op1 = remove_c_maybe_const_expr (op1);
8645 int_operands = (op0_int_operands && op1_int_operands);
8648 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
8649 && TREE_CODE (orig_op1) == INTEGER_CST);
8650 int_const = (int_const_or_overflow
8651 && !TREE_OVERFLOW (orig_op0)
8652 && !TREE_OVERFLOW (orig_op1));
8655 int_const = int_const_or_overflow = false;
8659 op0 = default_conversion (op0);
8660 op1 = default_conversion (op1);
8663 orig_type0 = type0 = TREE_TYPE (op0);
8664 orig_type1 = type1 = TREE_TYPE (op1);
8666 /* The expression codes of the data types of the arguments tell us
8667 whether the arguments are integers, floating, pointers, etc. */
8668 code0 = TREE_CODE (type0);
8669 code1 = TREE_CODE (type1);
8671 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8672 STRIP_TYPE_NOPS (op0);
8673 STRIP_TYPE_NOPS (op1);
8675 /* If an error was already reported for one of the arguments,
8676 avoid reporting another error. */
8678 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8679 return error_mark_node;
8681 if ((invalid_op_diag
8682 = targetm.invalid_binary_op (code, type0, type1)))
8684 error_at (location, invalid_op_diag);
8685 return error_mark_node;
8693 case TRUNC_DIV_EXPR:
8695 case FLOOR_DIV_EXPR:
8696 case ROUND_DIV_EXPR:
8697 case EXACT_DIV_EXPR:
8698 may_need_excess_precision = true;
8701 may_need_excess_precision = false;
8704 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
8706 op0 = TREE_OPERAND (op0, 0);
8707 type0 = TREE_TYPE (op0);
8709 else if (may_need_excess_precision
8710 && (eptype = excess_precision_type (type0)) != NULL_TREE)
8713 op0 = convert (eptype, op0);
8715 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
8717 op1 = TREE_OPERAND (op1, 0);
8718 type1 = TREE_TYPE (op1);
8720 else if (may_need_excess_precision
8721 && (eptype = excess_precision_type (type1)) != NULL_TREE)
8724 op1 = convert (eptype, op1);
8727 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
8732 /* Handle the pointer + int case. */
8733 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8735 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
8736 goto return_build_binary_op;
8738 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
8740 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
8741 goto return_build_binary_op;
8748 /* Subtraction of two similar pointers.
8749 We must subtract them as integers, then divide by object size. */
8750 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
8751 && comp_target_types (type0, type1))
8753 ret = pointer_diff (op0, op1);
8754 goto return_build_binary_op;
8756 /* Handle pointer minus int. Just like pointer plus int. */
8757 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8759 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
8760 goto return_build_binary_op;
8770 case TRUNC_DIV_EXPR:
8772 case FLOOR_DIV_EXPR:
8773 case ROUND_DIV_EXPR:
8774 case EXACT_DIV_EXPR:
8775 warn_for_div_by_zero (location, op1);
8777 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8778 || code0 == FIXED_POINT_TYPE
8779 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8780 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8781 || code1 == FIXED_POINT_TYPE
8782 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
8784 enum tree_code tcode0 = code0, tcode1 = code1;
8786 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8787 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
8788 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
8789 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
8791 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
8792 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
8793 resultcode = RDIV_EXPR;
8795 /* Although it would be tempting to shorten always here, that
8796 loses on some targets, since the modulo instruction is
8797 undefined if the quotient can't be represented in the
8798 computation mode. We shorten only if unsigned or if
8799 dividing by something we know != -1. */
8800 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8801 || (TREE_CODE (op1) == INTEGER_CST
8802 && !integer_all_onesp (op1)));
8810 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8812 /* Allow vector types which are not floating point types. */
8813 else if (code0 == VECTOR_TYPE
8814 && code1 == VECTOR_TYPE
8815 && !VECTOR_FLOAT_TYPE_P (type0)
8816 && !VECTOR_FLOAT_TYPE_P (type1))
8820 case TRUNC_MOD_EXPR:
8821 case FLOOR_MOD_EXPR:
8822 warn_for_div_by_zero (location, op1);
8824 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8826 /* Although it would be tempting to shorten always here, that loses
8827 on some targets, since the modulo instruction is undefined if the
8828 quotient can't be represented in the computation mode. We shorten
8829 only if unsigned or if dividing by something we know != -1. */
8830 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8831 || (TREE_CODE (op1) == INTEGER_CST
8832 && !integer_all_onesp (op1)));
8837 case TRUTH_ANDIF_EXPR:
8838 case TRUTH_ORIF_EXPR:
8839 case TRUTH_AND_EXPR:
8841 case TRUTH_XOR_EXPR:
8842 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
8843 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8844 || code0 == FIXED_POINT_TYPE)
8845 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
8846 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8847 || code1 == FIXED_POINT_TYPE))
8849 /* Result of these operations is always an int,
8850 but that does not mean the operands should be
8851 converted to ints! */
8852 result_type = integer_type_node;
8853 op0 = c_common_truthvalue_conversion (location, op0);
8854 op1 = c_common_truthvalue_conversion (location, op1);
8857 if (code == TRUTH_ANDIF_EXPR)
8859 int_const_or_overflow = (int_operands
8860 && TREE_CODE (orig_op0) == INTEGER_CST
8861 && (op0 == truthvalue_false_node
8862 || TREE_CODE (orig_op1) == INTEGER_CST));
8863 int_const = (int_const_or_overflow
8864 && !TREE_OVERFLOW (orig_op0)
8865 && (op0 == truthvalue_false_node
8866 || !TREE_OVERFLOW (orig_op1)));
8868 else if (code == TRUTH_ORIF_EXPR)
8870 int_const_or_overflow = (int_operands
8871 && TREE_CODE (orig_op0) == INTEGER_CST
8872 && (op0 == truthvalue_true_node
8873 || TREE_CODE (orig_op1) == INTEGER_CST));
8874 int_const = (int_const_or_overflow
8875 && !TREE_OVERFLOW (orig_op0)
8876 && (op0 == truthvalue_true_node
8877 || !TREE_OVERFLOW (orig_op1)));
8881 /* Shift operations: result has same type as first operand;
8882 always convert second operand to int.
8883 Also set SHORT_SHIFT if shifting rightward. */
8886 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8887 && code1 == INTEGER_TYPE)
8889 if (TREE_CODE (op1) == INTEGER_CST)
8891 if (tree_int_cst_sgn (op1) < 0)
8894 if (skip_evaluation == 0)
8895 warning (0, "right shift count is negative");
8899 if (!integer_zerop (op1))
8902 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8905 if (skip_evaluation == 0)
8906 warning (0, "right 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. */
8923 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8924 && code1 == INTEGER_TYPE)
8926 if (TREE_CODE (op1) == INTEGER_CST)
8928 if (tree_int_cst_sgn (op1) < 0)
8931 if (skip_evaluation == 0)
8932 warning (0, "left shift count is negative");
8935 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8938 if (skip_evaluation == 0)
8939 warning (0, "left shift count >= width of type");
8943 /* Use the type of the value to be shifted. */
8944 result_type = type0;
8945 /* Convert the shift-count to an integer, regardless of size
8946 of value being shifted. */
8947 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8948 op1 = convert (integer_type_node, op1);
8949 /* Avoid converting op1 to result_type later. */
8956 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
8957 warning_at (location,
8959 "comparing floating point with == or != is unsafe");
8960 /* Result of comparison is always int,
8961 but don't convert the args to int! */
8962 build_type = integer_type_node;
8963 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8964 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
8965 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8966 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
8968 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8970 tree tt0 = TREE_TYPE (type0);
8971 tree tt1 = TREE_TYPE (type1);
8972 /* Anything compares with void *. void * compares with anything.
8973 Otherwise, the targets must be compatible
8974 and both must be object or both incomplete. */
8975 if (comp_target_types (type0, type1))
8976 result_type = common_pointer_type (type0, type1);
8977 else if (VOID_TYPE_P (tt0))
8979 /* op0 != orig_op0 detects the case of something
8980 whose value is 0 but which isn't a valid null ptr const. */
8981 if (pedantic && !null_pointer_constant_p (orig_op0)
8982 && TREE_CODE (tt1) == FUNCTION_TYPE)
8983 pedwarn (location, OPT_pedantic, "ISO C forbids "
8984 "comparison of %<void *%> with function pointer");
8986 else if (VOID_TYPE_P (tt1))
8988 if (pedantic && !null_pointer_constant_p (orig_op1)
8989 && TREE_CODE (tt0) == FUNCTION_TYPE)
8990 pedwarn (location, OPT_pedantic, "ISO C forbids "
8991 "comparison of %<void *%> with function pointer");
8994 /* Avoid warning about the volatile ObjC EH puts on decls. */
8996 pedwarn (location, 0,
8997 "comparison of distinct pointer types lacks a cast");
8999 if (result_type == NULL_TREE)
9000 result_type = ptr_type_node;
9002 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9004 if (TREE_CODE (op0) == ADDR_EXPR
9005 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9006 warning_at (location,
9007 OPT_Waddress, "the address of %qD will never be NULL",
9008 TREE_OPERAND (op0, 0));
9009 result_type = type0;
9011 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9013 if (TREE_CODE (op1) == ADDR_EXPR
9014 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9015 warning_at (location,
9016 OPT_Waddress, "the address of %qD will never be NULL",
9017 TREE_OPERAND (op1, 0));
9018 result_type = type1;
9020 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9022 result_type = type0;
9023 pedwarn (location, 0, "comparison between pointer and integer");
9025 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9027 result_type = type1;
9028 pedwarn (location, 0, "comparison between pointer and integer");
9036 build_type = integer_type_node;
9037 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9038 || code0 == FIXED_POINT_TYPE)
9039 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9040 || code1 == FIXED_POINT_TYPE))
9042 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9044 if (comp_target_types (type0, type1))
9046 result_type = common_pointer_type (type0, type1);
9047 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9048 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9049 pedwarn (location, 0,
9050 "comparison of complete and incomplete pointers");
9051 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9052 pedwarn (location, OPT_pedantic, "ISO C forbids "
9053 "ordered comparisons of pointers to functions");
9057 result_type = ptr_type_node;
9058 pedwarn (location, 0,
9059 "comparison of distinct pointer types lacks a cast");
9062 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9064 result_type = type0;
9066 pedwarn (location, OPT_pedantic,
9067 "ordered comparison of pointer with integer zero");
9068 else if (extra_warnings)
9069 warning_at (location, OPT_Wextra,
9070 "ordered comparison of pointer with integer zero");
9072 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9074 result_type = type1;
9075 pedwarn (location, OPT_pedantic,
9076 "ordered comparison of pointer with integer zero");
9078 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9080 result_type = type0;
9081 pedwarn (location, 0, "comparison between pointer and integer");
9083 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9085 result_type = type1;
9086 pedwarn (location, 0, "comparison between pointer and integer");
9094 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9095 return error_mark_node;
9097 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9098 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9099 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9100 TREE_TYPE (type1))))
9102 binary_op_error (location, code, type0, type1);
9103 return error_mark_node;
9106 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9107 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9109 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9110 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9112 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
9114 if (shorten || common || short_compare)
9116 result_type = c_common_type (type0, type1);
9117 if (result_type == error_mark_node)
9118 return error_mark_node;
9121 /* For certain operations (which identify themselves by shorten != 0)
9122 if both args were extended from the same smaller type,
9123 do the arithmetic in that type and then extend.
9125 shorten !=0 and !=1 indicates a bitwise operation.
9126 For them, this optimization is safe only if
9127 both args are zero-extended or both are sign-extended.
9128 Otherwise, we might change the result.
9129 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9130 but calculated in (unsigned short) it would be (unsigned short)-1. */
9132 if (shorten && none_complex)
9134 final_type = result_type;
9135 result_type = shorten_binary_op (result_type, op0, op1,
9139 /* Shifts can be shortened if shifting right. */
9144 tree arg0 = get_narrower (op0, &unsigned_arg);
9146 final_type = result_type;
9148 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9149 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9151 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9152 /* We can shorten only if the shift count is less than the
9153 number of bits in the smaller type size. */
9154 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9155 /* We cannot drop an unsigned shift after sign-extension. */
9156 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9158 /* Do an unsigned shift if the operand was zero-extended. */
9160 = c_common_signed_or_unsigned_type (unsigned_arg,
9162 /* Convert value-to-be-shifted to that type. */
9163 if (TREE_TYPE (op0) != result_type)
9164 op0 = convert (result_type, op0);
9169 /* Comparison operations are shortened too but differently.
9170 They identify themselves by setting short_compare = 1. */
9174 /* Don't write &op0, etc., because that would prevent op0
9175 from being kept in a register.
9176 Instead, make copies of the our local variables and
9177 pass the copies by reference, then copy them back afterward. */
9178 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9179 enum tree_code xresultcode = resultcode;
9181 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9186 goto return_build_binary_op;
9189 op0 = xop0, op1 = xop1;
9191 resultcode = xresultcode;
9193 if (!skip_evaluation)
9195 bool op0_maybe_const = true;
9196 bool op1_maybe_const = true;
9197 tree orig_op0_folded, orig_op1_folded;
9199 if (in_late_binary_op)
9201 orig_op0_folded = orig_op0;
9202 orig_op1_folded = orig_op1;
9206 /* Fold for the sake of possible warnings, as in
9207 build_conditional_expr. This requires the
9208 "original" values to be folded, not just op0 and
9210 op0 = c_fully_fold (op0, require_constant_value,
9212 op1 = c_fully_fold (op1, require_constant_value,
9214 orig_op0_folded = c_fully_fold (orig_op0,
9215 require_constant_value,
9217 orig_op1_folded = c_fully_fold (orig_op1,
9218 require_constant_value,
9222 if (warn_sign_compare)
9223 warn_for_sign_compare (location, orig_op0_folded,
9224 orig_op1_folded, op0, op1,
9225 result_type, resultcode);
9226 if (!in_late_binary_op)
9228 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
9230 op0 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op0),
9232 C_MAYBE_CONST_EXPR_NON_CONST (op0) = !op0_maybe_const;
9234 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
9236 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
9238 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
9245 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9246 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9247 Then the expression will be built.
9248 It will be given type FINAL_TYPE if that is nonzero;
9249 otherwise, it will be given type RESULT_TYPE. */
9253 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
9254 return error_mark_node;
9259 if (TREE_TYPE (op0) != result_type)
9260 op0 = convert_and_check (result_type, op0);
9261 if (TREE_TYPE (op1) != result_type)
9262 op1 = convert_and_check (result_type, op1);
9264 /* This can happen if one operand has a vector type, and the other
9265 has a different type. */
9266 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9267 return error_mark_node;
9270 if (build_type == NULL_TREE)
9272 build_type = result_type;
9273 if (type0 != orig_type0 || type1 != orig_type1)
9275 gcc_assert (may_need_excess_precision && common);
9276 real_result_type = c_common_type (orig_type0, orig_type1);
9280 /* Treat expressions in initializers specially as they can't trap. */
9281 if (int_const_or_overflow)
9282 ret = (require_constant_value
9283 ? fold_build2_initializer (resultcode, build_type, op0, op1)
9284 : fold_build2 (resultcode, build_type, op0, op1));
9286 ret = build2 (resultcode, build_type, op0, op1);
9287 if (final_type != 0)
9288 ret = convert (final_type, ret);
9290 return_build_binary_op:
9291 gcc_assert (ret != error_mark_node);
9292 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
9294 ? note_integer_operands (ret)
9295 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
9296 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
9297 && !in_late_binary_op)
9298 ret = note_integer_operands (ret);
9299 if (real_result_type)
9300 ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret);
9301 protected_set_expr_location (ret, location);
9306 /* Convert EXPR to be a truth-value, validating its type for this
9307 purpose. LOCATION is the source location for the expression. */
9310 c_objc_common_truthvalue_conversion (location_t location, tree expr)
9312 bool int_const, int_operands;
9314 switch (TREE_CODE (TREE_TYPE (expr)))
9317 error_at (location, "used array that cannot be converted to pointer where scalar is required");
9318 return error_mark_node;
9321 error_at (location, "used struct type value where scalar is required");
9322 return error_mark_node;
9325 error_at (location, "used union type value where scalar is required");
9326 return error_mark_node;
9335 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
9336 int_operands = EXPR_INT_CONST_OPERANDS (expr);
9338 expr = remove_c_maybe_const_expr (expr);
9340 /* ??? Should we also give an error for void and vectors rather than
9341 leaving those to give errors later? */
9342 expr = c_common_truthvalue_conversion (location, expr);
9344 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
9346 if (TREE_OVERFLOW (expr))
9349 return note_integer_operands (expr);
9351 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
9352 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9357 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9361 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
9363 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
9365 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
9366 /* Executing a compound literal inside a function reinitializes
9368 if (!TREE_STATIC (decl))
9376 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9379 c_begin_omp_parallel (void)
9384 block = c_begin_compound_stmt (true);
9389 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound statement. */
9392 c_finish_omp_parallel (tree clauses, tree block)
9396 block = c_end_compound_stmt (block, true);
9398 stmt = make_node (OMP_PARALLEL);
9399 TREE_TYPE (stmt) = void_type_node;
9400 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9401 OMP_PARALLEL_BODY (stmt) = block;
9403 return add_stmt (stmt);
9406 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9409 c_begin_omp_task (void)
9414 block = c_begin_compound_stmt (true);
9419 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound statement. */
9422 c_finish_omp_task (tree clauses, tree block)
9426 block = c_end_compound_stmt (block, true);
9428 stmt = make_node (OMP_TASK);
9429 TREE_TYPE (stmt) = void_type_node;
9430 OMP_TASK_CLAUSES (stmt) = clauses;
9431 OMP_TASK_BODY (stmt) = block;
9433 return add_stmt (stmt);
9436 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9437 Remove any elements from the list that are invalid. */
9440 c_finish_omp_clauses (tree clauses)
9442 bitmap_head generic_head, firstprivate_head, lastprivate_head;
9443 tree c, t, *pc = &clauses;
9446 bitmap_obstack_initialize (NULL);
9447 bitmap_initialize (&generic_head, &bitmap_default_obstack);
9448 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
9449 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
9451 for (pc = &clauses, c = clauses; c ; c = *pc)
9453 bool remove = false;
9454 bool need_complete = false;
9455 bool need_implicitly_determined = false;
9457 switch (OMP_CLAUSE_CODE (c))
9459 case OMP_CLAUSE_SHARED:
9461 need_implicitly_determined = true;
9462 goto check_dup_generic;
9464 case OMP_CLAUSE_PRIVATE:
9466 need_complete = true;
9467 need_implicitly_determined = true;
9468 goto check_dup_generic;
9470 case OMP_CLAUSE_REDUCTION:
9472 need_implicitly_determined = true;
9473 t = OMP_CLAUSE_DECL (c);
9474 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
9475 || POINTER_TYPE_P (TREE_TYPE (t)))
9477 error ("%qE has invalid type for %<reduction%>", t);
9480 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
9482 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
9483 const char *r_name = NULL;
9500 case TRUTH_ANDIF_EXPR:
9503 case TRUTH_ORIF_EXPR:
9511 error ("%qE has invalid type for %<reduction(%s)%>",
9516 goto check_dup_generic;
9518 case OMP_CLAUSE_COPYPRIVATE:
9519 name = "copyprivate";
9520 goto check_dup_generic;
9522 case OMP_CLAUSE_COPYIN:
9524 t = OMP_CLAUSE_DECL (c);
9525 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
9527 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
9530 goto check_dup_generic;
9533 t = OMP_CLAUSE_DECL (c);
9534 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9536 error ("%qE is not a variable in clause %qs", t, name);
9539 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9540 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9541 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9543 error ("%qE appears more than once in data clauses", t);
9547 bitmap_set_bit (&generic_head, DECL_UID (t));
9550 case OMP_CLAUSE_FIRSTPRIVATE:
9551 name = "firstprivate";
9552 t = OMP_CLAUSE_DECL (c);
9553 need_complete = true;
9554 need_implicitly_determined = true;
9555 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9557 error ("%qE is not a variable in clause %<firstprivate%>", t);
9560 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9561 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9563 error ("%qE appears more than once in data clauses", t);
9567 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
9570 case OMP_CLAUSE_LASTPRIVATE:
9571 name = "lastprivate";
9572 t = OMP_CLAUSE_DECL (c);
9573 need_complete = true;
9574 need_implicitly_determined = true;
9575 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9577 error ("%qE is not a variable in clause %<lastprivate%>", t);
9580 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9581 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9583 error ("%qE appears more than once in data clauses", t);
9587 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
9591 case OMP_CLAUSE_NUM_THREADS:
9592 case OMP_CLAUSE_SCHEDULE:
9593 case OMP_CLAUSE_NOWAIT:
9594 case OMP_CLAUSE_ORDERED:
9595 case OMP_CLAUSE_DEFAULT:
9596 case OMP_CLAUSE_UNTIED:
9597 case OMP_CLAUSE_COLLAPSE:
9598 pc = &OMP_CLAUSE_CHAIN (c);
9607 t = OMP_CLAUSE_DECL (c);
9611 t = require_complete_type (t);
9612 if (t == error_mark_node)
9616 if (need_implicitly_determined)
9618 const char *share_name = NULL;
9620 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
9621 share_name = "threadprivate";
9622 else switch (c_omp_predetermined_sharing (t))
9624 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9626 case OMP_CLAUSE_DEFAULT_SHARED:
9627 share_name = "shared";
9629 case OMP_CLAUSE_DEFAULT_PRIVATE:
9630 share_name = "private";
9637 error ("%qE is predetermined %qs for %qs",
9638 t, share_name, name);
9645 *pc = OMP_CLAUSE_CHAIN (c);
9647 pc = &OMP_CLAUSE_CHAIN (c);
9650 bitmap_obstack_release (NULL);
9654 /* Make a variant type in the proper way for C/C++, propagating qualifiers
9655 down to the element type of an array. */
9658 c_build_qualified_type (tree type, int type_quals)
9660 if (type == error_mark_node)
9663 if (TREE_CODE (type) == ARRAY_TYPE)
9666 tree element_type = c_build_qualified_type (TREE_TYPE (type),
9669 /* See if we already have an identically qualified type. */
9670 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9672 if (TYPE_QUALS (strip_array_types (t)) == type_quals
9673 && TYPE_NAME (t) == TYPE_NAME (type)
9674 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
9675 && attribute_list_equal (TYPE_ATTRIBUTES (t),
9676 TYPE_ATTRIBUTES (type)))
9681 tree domain = TYPE_DOMAIN (type);
9683 t = build_variant_type_copy (type);
9684 TREE_TYPE (t) = element_type;
9686 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
9687 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
9688 SET_TYPE_STRUCTURAL_EQUALITY (t);
9689 else if (TYPE_CANONICAL (element_type) != element_type
9690 || (domain && TYPE_CANONICAL (domain) != domain))
9692 tree unqualified_canon
9693 = build_array_type (TYPE_CANONICAL (element_type),
9694 domain? TYPE_CANONICAL (domain)
9697 = c_build_qualified_type (unqualified_canon, type_quals);
9700 TYPE_CANONICAL (t) = t;
9705 /* A restrict-qualified pointer type must be a pointer to object or
9706 incomplete type. Note that the use of POINTER_TYPE_P also allows
9707 REFERENCE_TYPEs, which is appropriate for C++. */
9708 if ((type_quals & TYPE_QUAL_RESTRICT)
9709 && (!POINTER_TYPE_P (type)
9710 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
9712 error ("invalid use of %<restrict%>");
9713 type_quals &= ~TYPE_QUAL_RESTRICT;
9716 return build_qualified_type (type, type_quals);