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 (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
92 static tree pointer_diff (tree, tree);
93 static tree convert_for_assignment (tree, tree, tree, enum impl_conv, bool,
95 static tree valid_compound_expr_initializer (tree, tree);
96 static void push_string (const char *);
97 static void push_member_name (tree);
98 static int spelling_length (void);
99 static char *print_spelling (char *);
100 static void warning_init (int, const char *);
101 static tree digest_init (tree, tree, tree, bool, bool, int);
102 static void output_init_element (tree, tree, bool, tree, tree, int, bool);
103 static void output_pending_init_elements (int);
104 static int set_designator (int);
105 static void push_range_stack (tree);
106 static void add_pending_init (tree, tree, tree, bool);
107 static void set_nonincremental_init (void);
108 static void set_nonincremental_init_from_string (tree);
109 static tree find_init_member (tree);
110 static void readonly_error (tree, enum lvalue_use);
111 static void readonly_warning (tree, enum lvalue_use);
112 static int lvalue_or_else (const_tree, enum lvalue_use);
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 adr = build_unary_op (EXPR_LOCATION (exp), ADDR_EXPR, exp, 1);
1638 return convert (ptrtype, adr);
1641 /* Convert the function expression EXP to a pointer. */
1643 function_to_pointer_conversion (tree exp)
1645 tree orig_exp = exp;
1647 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1649 STRIP_TYPE_NOPS (exp);
1651 if (TREE_NO_WARNING (orig_exp))
1652 TREE_NO_WARNING (exp) = 1;
1654 return build_unary_op (EXPR_LOCATION (exp), ADDR_EXPR, exp, 0);
1657 /* Perform the default conversion of arrays and functions to pointers.
1658 Return the result of converting EXP. For any other expression, just
1662 default_function_array_conversion (struct c_expr exp)
1664 tree orig_exp = exp.value;
1665 tree type = TREE_TYPE (exp.value);
1666 enum tree_code code = TREE_CODE (type);
1672 bool not_lvalue = false;
1673 bool lvalue_array_p;
1675 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1676 || CONVERT_EXPR_P (exp.value))
1677 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1679 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1681 exp.value = TREE_OPERAND (exp.value, 0);
1684 if (TREE_NO_WARNING (orig_exp))
1685 TREE_NO_WARNING (exp.value) = 1;
1687 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1688 if (!flag_isoc99 && !lvalue_array_p)
1690 /* Before C99, non-lvalue arrays do not decay to pointers.
1691 Normally, using such an array would be invalid; but it can
1692 be used correctly inside sizeof or as a statement expression.
1693 Thus, do not give an error here; an error will result later. */
1697 exp.value = array_to_pointer_conversion (exp.value);
1701 exp.value = function_to_pointer_conversion (exp.value);
1711 /* EXP is an expression of integer type. Apply the integer promotions
1712 to it and return the promoted value. */
1715 perform_integral_promotions (tree exp)
1717 tree type = TREE_TYPE (exp);
1718 enum tree_code code = TREE_CODE (type);
1720 gcc_assert (INTEGRAL_TYPE_P (type));
1722 /* Normally convert enums to int,
1723 but convert wide enums to something wider. */
1724 if (code == ENUMERAL_TYPE)
1726 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1727 TYPE_PRECISION (integer_type_node)),
1728 ((TYPE_PRECISION (type)
1729 >= TYPE_PRECISION (integer_type_node))
1730 && TYPE_UNSIGNED (type)));
1732 return convert (type, exp);
1735 /* ??? This should no longer be needed now bit-fields have their
1737 if (TREE_CODE (exp) == COMPONENT_REF
1738 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1739 /* If it's thinner than an int, promote it like a
1740 c_promoting_integer_type_p, otherwise leave it alone. */
1741 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1742 TYPE_PRECISION (integer_type_node)))
1743 return convert (integer_type_node, exp);
1745 if (c_promoting_integer_type_p (type))
1747 /* Preserve unsignedness if not really getting any wider. */
1748 if (TYPE_UNSIGNED (type)
1749 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1750 return convert (unsigned_type_node, exp);
1752 return convert (integer_type_node, exp);
1759 /* Perform default promotions for C data used in expressions.
1760 Enumeral types or short or char are converted to int.
1761 In addition, manifest constants symbols are replaced by their values. */
1764 default_conversion (tree exp)
1767 tree type = TREE_TYPE (exp);
1768 enum tree_code code = TREE_CODE (type);
1770 /* Functions and arrays have been converted during parsing. */
1771 gcc_assert (code != FUNCTION_TYPE);
1772 if (code == ARRAY_TYPE)
1775 /* Constants can be used directly unless they're not loadable. */
1776 if (TREE_CODE (exp) == CONST_DECL)
1777 exp = DECL_INITIAL (exp);
1779 /* Strip no-op conversions. */
1781 STRIP_TYPE_NOPS (exp);
1783 if (TREE_NO_WARNING (orig_exp))
1784 TREE_NO_WARNING (exp) = 1;
1786 if (code == VOID_TYPE)
1788 error ("void value not ignored as it ought to be");
1789 return error_mark_node;
1792 exp = require_complete_type (exp);
1793 if (exp == error_mark_node)
1794 return error_mark_node;
1796 if (INTEGRAL_TYPE_P (type))
1797 return perform_integral_promotions (exp);
1802 /* Look up COMPONENT in a structure or union DECL.
1804 If the component name is not found, returns NULL_TREE. Otherwise,
1805 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1806 stepping down the chain to the component, which is in the last
1807 TREE_VALUE of the list. Normally the list is of length one, but if
1808 the component is embedded within (nested) anonymous structures or
1809 unions, the list steps down the chain to the component. */
1812 lookup_field (tree decl, tree component)
1814 tree type = TREE_TYPE (decl);
1817 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1818 to the field elements. Use a binary search on this array to quickly
1819 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1820 will always be set for structures which have many elements. */
1822 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1825 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1827 field = TYPE_FIELDS (type);
1829 top = TYPE_LANG_SPECIFIC (type)->s->len;
1830 while (top - bot > 1)
1832 half = (top - bot + 1) >> 1;
1833 field = field_array[bot+half];
1835 if (DECL_NAME (field) == NULL_TREE)
1837 /* Step through all anon unions in linear fashion. */
1838 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1840 field = field_array[bot++];
1841 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1842 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1844 tree anon = lookup_field (field, component);
1847 return tree_cons (NULL_TREE, field, anon);
1851 /* Entire record is only anon unions. */
1855 /* Restart the binary search, with new lower bound. */
1859 if (DECL_NAME (field) == component)
1861 if (DECL_NAME (field) < component)
1867 if (DECL_NAME (field_array[bot]) == component)
1868 field = field_array[bot];
1869 else if (DECL_NAME (field) != component)
1874 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1876 if (DECL_NAME (field) == NULL_TREE
1877 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1878 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1880 tree anon = lookup_field (field, component);
1883 return tree_cons (NULL_TREE, field, anon);
1886 if (DECL_NAME (field) == component)
1890 if (field == NULL_TREE)
1894 return tree_cons (NULL_TREE, field, NULL_TREE);
1897 /* Make an expression to refer to the COMPONENT field of
1898 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1901 build_component_ref (tree datum, tree component)
1903 tree type = TREE_TYPE (datum);
1904 enum tree_code code = TREE_CODE (type);
1907 bool datum_lvalue = lvalue_p (datum);
1909 if (!objc_is_public (datum, component))
1910 return error_mark_node;
1912 /* See if there is a field or component with name COMPONENT. */
1914 if (code == RECORD_TYPE || code == UNION_TYPE)
1916 if (!COMPLETE_TYPE_P (type))
1918 c_incomplete_type_error (NULL_TREE, type);
1919 return error_mark_node;
1922 field = lookup_field (datum, component);
1926 error ("%qT has no member named %qE", type, component);
1927 return error_mark_node;
1930 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1931 This might be better solved in future the way the C++ front
1932 end does it - by giving the anonymous entities each a
1933 separate name and type, and then have build_component_ref
1934 recursively call itself. We can't do that here. */
1937 tree subdatum = TREE_VALUE (field);
1940 bool use_datum_quals;
1942 if (TREE_TYPE (subdatum) == error_mark_node)
1943 return error_mark_node;
1945 /* If this is an rvalue, it does not have qualifiers in C
1946 standard terms and we must avoid propagating such
1947 qualifiers down to a non-lvalue array that is then
1948 converted to a pointer. */
1949 use_datum_quals = (datum_lvalue
1950 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
1952 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1953 if (use_datum_quals)
1954 quals |= TYPE_QUALS (TREE_TYPE (datum));
1955 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1957 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
1959 if (TREE_READONLY (subdatum)
1960 || (use_datum_quals && TREE_READONLY (datum)))
1961 TREE_READONLY (ref) = 1;
1962 if (TREE_THIS_VOLATILE (subdatum)
1963 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
1964 TREE_THIS_VOLATILE (ref) = 1;
1966 if (TREE_DEPRECATED (subdatum))
1967 warn_deprecated_use (subdatum, NULL_TREE);
1971 field = TREE_CHAIN (field);
1977 else if (code != ERROR_MARK)
1978 error ("request for member %qE in something not a structure or union",
1981 return error_mark_node;
1984 /* Given an expression PTR for a pointer, return an expression
1985 for the value pointed to.
1986 ERRORSTRING is the name of the operator to appear in error messages.
1988 LOC is the location to use for the generated tree. */
1991 build_indirect_ref (location_t loc, tree ptr, const char *errorstring)
1993 tree pointer = default_conversion (ptr);
1994 tree type = TREE_TYPE (pointer);
1997 if (TREE_CODE (type) == POINTER_TYPE)
1999 if (CONVERT_EXPR_P (pointer)
2000 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2002 /* If a warning is issued, mark it to avoid duplicates from
2003 the backend. This only needs to be done at
2004 warn_strict_aliasing > 2. */
2005 if (warn_strict_aliasing > 2)
2006 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2007 type, TREE_OPERAND (pointer, 0)))
2008 TREE_NO_WARNING (pointer) = 1;
2011 if (TREE_CODE (pointer) == ADDR_EXPR
2012 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2013 == TREE_TYPE (type)))
2015 ref = TREE_OPERAND (pointer, 0);
2016 protected_set_expr_location (ref, loc);
2021 tree t = TREE_TYPE (type);
2023 ref = build1 (INDIRECT_REF, t, pointer);
2025 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2027 error_at (loc, "dereferencing pointer to incomplete type");
2028 return error_mark_node;
2030 if (VOID_TYPE_P (t) && skip_evaluation == 0)
2031 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2033 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2034 so that we get the proper error message if the result is used
2035 to assign to. Also, &* is supposed to be a no-op.
2036 And ANSI C seems to specify that the type of the result
2037 should be the const type. */
2038 /* A de-reference of a pointer to const is not a const. It is valid
2039 to change it via some other pointer. */
2040 TREE_READONLY (ref) = TYPE_READONLY (t);
2041 TREE_SIDE_EFFECTS (ref)
2042 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2043 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2044 protected_set_expr_location (ref, loc);
2048 else if (TREE_CODE (pointer) != ERROR_MARK)
2050 "invalid type argument of %qs (have %qT)", errorstring, type);
2051 return error_mark_node;
2054 /* This handles expressions of the form "a[i]", which denotes
2057 This is logically equivalent in C to *(a+i), but we may do it differently.
2058 If A is a variable or a member, we generate a primitive ARRAY_REF.
2059 This avoids forcing the array out of registers, and can work on
2060 arrays that are not lvalues (for example, members of structures returned
2063 LOC is the location to use for the returned expression. */
2066 build_array_ref (tree array, tree index, location_t loc)
2069 bool swapped = false;
2070 if (TREE_TYPE (array) == error_mark_node
2071 || TREE_TYPE (index) == error_mark_node)
2072 return error_mark_node;
2074 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2075 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2078 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2079 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2081 error_at (loc, "subscripted value is neither array nor pointer");
2082 return error_mark_node;
2090 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2092 error_at (loc, "array subscript is not an integer");
2093 return error_mark_node;
2096 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2098 error_at (loc, "subscripted value is pointer to function");
2099 return error_mark_node;
2102 /* ??? Existing practice has been to warn only when the char
2103 index is syntactically the index, not for char[array]. */
2105 warn_array_subscript_with_type_char (index);
2107 /* Apply default promotions *after* noticing character types. */
2108 index = default_conversion (index);
2110 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2112 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2116 /* An array that is indexed by a non-constant
2117 cannot be stored in a register; we must be able to do
2118 address arithmetic on its address.
2119 Likewise an array of elements of variable size. */
2120 if (TREE_CODE (index) != INTEGER_CST
2121 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2122 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2124 if (!c_mark_addressable (array))
2125 return error_mark_node;
2127 /* An array that is indexed by a constant value which is not within
2128 the array bounds cannot be stored in a register either; because we
2129 would get a crash in store_bit_field/extract_bit_field when trying
2130 to access a non-existent part of the register. */
2131 if (TREE_CODE (index) == INTEGER_CST
2132 && TYPE_DOMAIN (TREE_TYPE (array))
2133 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2135 if (!c_mark_addressable (array))
2136 return error_mark_node;
2142 while (TREE_CODE (foo) == COMPONENT_REF)
2143 foo = TREE_OPERAND (foo, 0);
2144 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2145 pedwarn (loc, OPT_pedantic,
2146 "ISO C forbids subscripting %<register%> array");
2147 else if (!flag_isoc99 && !lvalue_p (foo))
2148 pedwarn (loc, OPT_pedantic,
2149 "ISO C90 forbids subscripting non-lvalue array");
2152 type = TREE_TYPE (TREE_TYPE (array));
2153 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2154 /* Array ref is const/volatile if the array elements are
2155 or if the array is. */
2156 TREE_READONLY (rval)
2157 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2158 | TREE_READONLY (array));
2159 TREE_SIDE_EFFECTS (rval)
2160 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2161 | TREE_SIDE_EFFECTS (array));
2162 TREE_THIS_VOLATILE (rval)
2163 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2164 /* This was added by rms on 16 Nov 91.
2165 It fixes vol struct foo *a; a->elts[1]
2166 in an inline function.
2167 Hope it doesn't break something else. */
2168 | TREE_THIS_VOLATILE (array));
2169 ret = require_complete_type (rval);
2170 protected_set_expr_location (ret, loc);
2175 tree ar = default_conversion (array);
2177 if (ar == error_mark_node)
2180 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2181 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2183 return build_indirect_ref
2184 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2189 /* Build an external reference to identifier ID. FUN indicates
2190 whether this will be used for a function call. LOC is the source
2191 location of the identifier. This sets *TYPE to the type of the
2192 identifier, which is not the same as the type of the returned value
2193 for CONST_DECLs defined as enum constants. If the type of the
2194 identifier is not available, *TYPE is set to NULL. */
2196 build_external_ref (tree id, int fun, location_t loc, tree *type)
2199 tree decl = lookup_name (id);
2201 /* In Objective-C, an instance variable (ivar) may be preferred to
2202 whatever lookup_name() found. */
2203 decl = objc_lookup_ivar (decl, id);
2206 if (decl && decl != error_mark_node)
2209 *type = TREE_TYPE (ref);
2212 /* Implicit function declaration. */
2213 ref = implicitly_declare (id);
2214 else if (decl == error_mark_node)
2215 /* Don't complain about something that's already been
2216 complained about. */
2217 return error_mark_node;
2220 undeclared_variable (id, loc);
2221 return error_mark_node;
2224 if (TREE_TYPE (ref) == error_mark_node)
2225 return error_mark_node;
2227 if (TREE_DEPRECATED (ref))
2228 warn_deprecated_use (ref, NULL_TREE);
2230 /* Recursive call does not count as usage. */
2231 if (ref != current_function_decl)
2233 TREE_USED (ref) = 1;
2236 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2238 if (!in_sizeof && !in_typeof)
2239 C_DECL_USED (ref) = 1;
2240 else if (DECL_INITIAL (ref) == 0
2241 && DECL_EXTERNAL (ref)
2242 && !TREE_PUBLIC (ref))
2243 record_maybe_used_decl (ref);
2246 if (TREE_CODE (ref) == CONST_DECL)
2248 used_types_insert (TREE_TYPE (ref));
2251 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2252 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2254 warning_at (loc, OPT_Wc___compat,
2255 ("enum constant defined in struct or union "
2256 "is not visible in C++"));
2257 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2260 ref = DECL_INITIAL (ref);
2261 TREE_CONSTANT (ref) = 1;
2263 else if (current_function_decl != 0
2264 && !DECL_FILE_SCOPE_P (current_function_decl)
2265 && (TREE_CODE (ref) == VAR_DECL
2266 || TREE_CODE (ref) == PARM_DECL
2267 || TREE_CODE (ref) == FUNCTION_DECL))
2269 tree context = decl_function_context (ref);
2271 if (context != 0 && context != current_function_decl)
2272 DECL_NONLOCAL (ref) = 1;
2274 /* C99 6.7.4p3: An inline definition of a function with external
2275 linkage ... shall not contain a reference to an identifier with
2276 internal linkage. */
2277 else if (current_function_decl != 0
2278 && DECL_DECLARED_INLINE_P (current_function_decl)
2279 && DECL_EXTERNAL (current_function_decl)
2280 && VAR_OR_FUNCTION_DECL_P (ref)
2281 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2282 && ! TREE_PUBLIC (ref)
2283 && DECL_CONTEXT (ref) != current_function_decl)
2284 record_inline_static (loc, current_function_decl, ref,
2290 /* Record details of decls possibly used inside sizeof or typeof. */
2291 struct maybe_used_decl
2295 /* The level seen at (in_sizeof + in_typeof). */
2297 /* The next one at this level or above, or NULL. */
2298 struct maybe_used_decl *next;
2301 static struct maybe_used_decl *maybe_used_decls;
2303 /* Record that DECL, an undefined static function reference seen
2304 inside sizeof or typeof, might be used if the operand of sizeof is
2305 a VLA type or the operand of typeof is a variably modified
2309 record_maybe_used_decl (tree decl)
2311 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2313 t->level = in_sizeof + in_typeof;
2314 t->next = maybe_used_decls;
2315 maybe_used_decls = t;
2318 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2319 USED is false, just discard them. If it is true, mark them used
2320 (if no longer inside sizeof or typeof) or move them to the next
2321 level up (if still inside sizeof or typeof). */
2324 pop_maybe_used (bool used)
2326 struct maybe_used_decl *p = maybe_used_decls;
2327 int cur_level = in_sizeof + in_typeof;
2328 while (p && p->level > cur_level)
2333 C_DECL_USED (p->decl) = 1;
2335 p->level = cur_level;
2339 if (!used || cur_level == 0)
2340 maybe_used_decls = p;
2343 /* Return the result of sizeof applied to EXPR. */
2346 c_expr_sizeof_expr (struct c_expr expr)
2349 if (expr.value == error_mark_node)
2351 ret.value = error_mark_node;
2352 ret.original_code = ERROR_MARK;
2353 ret.original_type = NULL;
2354 pop_maybe_used (false);
2358 bool expr_const_operands = true;
2359 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2360 &expr_const_operands);
2361 ret.value = c_sizeof (TREE_TYPE (folded_expr));
2362 ret.original_code = ERROR_MARK;
2363 ret.original_type = NULL;
2364 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2366 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2367 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2368 folded_expr, ret.value);
2369 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2371 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2376 /* Return the result of sizeof applied to T, a structure for the type
2377 name passed to sizeof (rather than the type itself). */
2380 c_expr_sizeof_type (struct c_type_name *t)
2384 tree type_expr = NULL_TREE;
2385 bool type_expr_const = true;
2386 type = groktypename (t, &type_expr, &type_expr_const);
2387 ret.value = c_sizeof (type);
2388 ret.original_code = ERROR_MARK;
2389 ret.original_type = NULL;
2390 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2391 && c_vla_type_p (type))
2393 /* If the type is a [*] array, it is a VLA but is represented as
2394 having a size of zero. In such a case we must ensure that
2395 the result of sizeof does not get folded to a constant by
2396 c_fully_fold, because if the size is evaluated the result is
2397 not constant and so constraints on zero or negative size
2398 arrays must not be applied when this sizeof call is inside
2399 another array declarator. */
2401 type_expr = integer_zero_node;
2402 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2403 type_expr, ret.value);
2404 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2406 pop_maybe_used (type != error_mark_node
2407 ? C_TYPE_VARIABLE_SIZE (type) : false);
2411 /* Build a function call to function FUNCTION with parameters PARAMS.
2412 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2413 TREE_VALUE of each node is a parameter-expression.
2414 FUNCTION's data type may be a function type or a pointer-to-function. */
2417 build_function_call (tree function, tree params)
2422 vec = VEC_alloc (tree, gc, list_length (params));
2423 for (; params; params = TREE_CHAIN (params))
2424 VEC_quick_push (tree, vec, TREE_VALUE (params));
2425 ret = build_function_call_vec (function, vec, NULL);
2426 VEC_free (tree, gc, vec);
2430 /* Build a function call to function FUNCTION with parameters PARAMS.
2431 ORIGTYPES, if not NULL, is a vector of types; each element is
2432 either NULL or the original type of the corresponding element in
2433 PARAMS. The original type may differ from TREE_TYPE of the
2434 parameter for enums. FUNCTION's data type may be a function type
2435 or pointer-to-function. This function changes the elements of
2439 build_function_call_vec (tree function, VEC(tree,gc) *params,
2440 VEC(tree,gc) *origtypes)
2442 tree fntype, fundecl = 0;
2443 tree name = NULL_TREE, result;
2449 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2450 STRIP_TYPE_NOPS (function);
2452 /* Convert anything with function type to a pointer-to-function. */
2453 if (TREE_CODE (function) == FUNCTION_DECL)
2455 /* Implement type-directed function overloading for builtins.
2456 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2457 handle all the type checking. The result is a complete expression
2458 that implements this function call. */
2459 tem = resolve_overloaded_builtin (function, params);
2463 name = DECL_NAME (function);
2466 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2467 function = function_to_pointer_conversion (function);
2469 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2470 expressions, like those used for ObjC messenger dispatches. */
2471 if (!VEC_empty (tree, params))
2472 function = objc_rewrite_function_call (function,
2473 VEC_index (tree, params, 0));
2475 function = c_fully_fold (function, false, NULL);
2477 fntype = TREE_TYPE (function);
2479 if (TREE_CODE (fntype) == ERROR_MARK)
2480 return error_mark_node;
2482 if (!(TREE_CODE (fntype) == POINTER_TYPE
2483 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2485 error ("called object %qE is not a function", function);
2486 return error_mark_node;
2489 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2490 current_function_returns_abnormally = 1;
2492 /* fntype now gets the type of function pointed to. */
2493 fntype = TREE_TYPE (fntype);
2495 /* Convert the parameters to the types declared in the
2496 function prototype, or apply default promotions. */
2498 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2501 return error_mark_node;
2503 /* Check that the function is called through a compatible prototype.
2504 If it is not, replace the call by a trap, wrapped up in a compound
2505 expression if necessary. This has the nice side-effect to prevent
2506 the tree-inliner from generating invalid assignment trees which may
2507 blow up in the RTL expander later. */
2508 if (CONVERT_EXPR_P (function)
2509 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2510 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2511 && !comptypes (fntype, TREE_TYPE (tem)))
2513 tree return_type = TREE_TYPE (fntype);
2514 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2518 /* This situation leads to run-time undefined behavior. We can't,
2519 therefore, simply error unless we can prove that all possible
2520 executions of the program must execute the code. */
2521 if (warning (0, "function called through a non-compatible type"))
2522 /* We can, however, treat "undefined" any way we please.
2523 Call abort to encourage the user to fix the program. */
2524 inform (input_location, "if this code is reached, the program will abort");
2525 /* Before the abort, allow the function arguments to exit or
2527 for (i = 0; i < nargs; i++)
2528 trap = build2 (COMPOUND_EXPR, void_type_node,
2529 VEC_index (tree, params, i), trap);
2531 if (VOID_TYPE_P (return_type))
2533 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2534 pedwarn (input_location, 0,
2535 "function with qualified void return type called");
2542 if (AGGREGATE_TYPE_P (return_type))
2543 rhs = build_compound_literal (return_type,
2544 build_constructor (return_type, 0),
2547 rhs = fold_convert (return_type, integer_zero_node);
2549 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2554 argarray = VEC_address (tree, params);
2556 /* Check that arguments to builtin functions match the expectations. */
2558 && DECL_BUILT_IN (fundecl)
2559 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2560 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2561 return error_mark_node;
2563 /* Check that the arguments to the function are valid. */
2564 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2565 TYPE_ARG_TYPES (fntype));
2567 if (name != NULL_TREE
2568 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2570 if (require_constant_value)
2571 result = fold_build_call_array_initializer (TREE_TYPE (fntype),
2572 function, nargs, argarray);
2574 result = fold_build_call_array (TREE_TYPE (fntype),
2575 function, nargs, argarray);
2576 if (TREE_CODE (result) == NOP_EXPR
2577 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2578 STRIP_TYPE_NOPS (result);
2581 result = build_call_array (TREE_TYPE (fntype),
2582 function, nargs, argarray);
2584 if (VOID_TYPE_P (TREE_TYPE (result)))
2586 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2587 pedwarn (input_location, 0,
2588 "function with qualified void return type called");
2591 return require_complete_type (result);
2594 /* Convert the argument expressions in the vector VALUES
2595 to the types in the list TYPELIST.
2597 If TYPELIST is exhausted, or when an element has NULL as its type,
2598 perform the default conversions.
2600 ORIGTYPES is the original types of the expressions in VALUES. This
2601 holds the type of enum values which have been converted to integral
2602 types. It may be NULL.
2604 FUNCTION is a tree for the called function. It is used only for
2605 error messages, where it is formatted with %qE.
2607 This is also where warnings about wrong number of args are generated.
2609 Returns the actual number of arguments processed (which may be less
2610 than the length of VALUES in some error situations), or -1 on
2614 convert_arguments (tree typelist, VEC(tree,gc) *values,
2615 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2618 unsigned int parmnum;
2619 const bool type_generic = fundecl
2620 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2621 bool type_generic_remove_excess_precision = false;
2624 /* Change pointer to function to the function itself for
2626 if (TREE_CODE (function) == ADDR_EXPR
2627 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2628 function = TREE_OPERAND (function, 0);
2630 /* Handle an ObjC selector specially for diagnostics. */
2631 selector = objc_message_selector ();
2633 /* For type-generic built-in functions, determine whether excess
2634 precision should be removed (classification) or not
2637 && DECL_BUILT_IN (fundecl)
2638 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2640 switch (DECL_FUNCTION_CODE (fundecl))
2642 case BUILT_IN_ISFINITE:
2643 case BUILT_IN_ISINF:
2644 case BUILT_IN_ISINF_SIGN:
2645 case BUILT_IN_ISNAN:
2646 case BUILT_IN_ISNORMAL:
2647 case BUILT_IN_FPCLASSIFY:
2648 type_generic_remove_excess_precision = true;
2652 type_generic_remove_excess_precision = false;
2657 /* Scan the given expressions and types, producing individual
2658 converted arguments. */
2660 for (typetail = typelist, parmnum = 0;
2661 VEC_iterate (tree, values, parmnum, val);
2664 tree type = typetail ? TREE_VALUE (typetail) : 0;
2665 tree valtype = TREE_TYPE (val);
2666 tree rname = function;
2667 int argnum = parmnum + 1;
2668 const char *invalid_func_diag;
2669 bool excess_precision = false;
2673 if (type == void_type_node)
2675 error ("too many arguments to function %qE", function);
2679 if (selector && argnum > 2)
2685 npc = null_pointer_constant_p (val);
2687 /* If there is excess precision and a prototype, convert once to
2688 the required type rather than converting via the semantic
2689 type. Likewise without a prototype a float value represented
2690 as long double should be converted once to double. But for
2691 type-generic classification functions excess precision must
2693 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2694 && (type || !type_generic || !type_generic_remove_excess_precision))
2696 val = TREE_OPERAND (val, 0);
2697 excess_precision = true;
2699 val = c_fully_fold (val, false, NULL);
2700 STRIP_TYPE_NOPS (val);
2702 val = require_complete_type (val);
2706 /* Formal parm type is specified by a function prototype. */
2708 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2710 error ("type of formal parameter %d is incomplete", parmnum + 1);
2717 /* Optionally warn about conversions that
2718 differ from the default conversions. */
2719 if (warn_traditional_conversion || warn_traditional)
2721 unsigned int formal_prec = TYPE_PRECISION (type);
2723 if (INTEGRAL_TYPE_P (type)
2724 && TREE_CODE (valtype) == REAL_TYPE)
2725 warning (0, "passing argument %d of %qE as integer "
2726 "rather than floating due to prototype",
2728 if (INTEGRAL_TYPE_P (type)
2729 && TREE_CODE (valtype) == COMPLEX_TYPE)
2730 warning (0, "passing argument %d of %qE as integer "
2731 "rather than complex due to prototype",
2733 else if (TREE_CODE (type) == COMPLEX_TYPE
2734 && TREE_CODE (valtype) == REAL_TYPE)
2735 warning (0, "passing argument %d of %qE as complex "
2736 "rather than floating due to prototype",
2738 else if (TREE_CODE (type) == REAL_TYPE
2739 && INTEGRAL_TYPE_P (valtype))
2740 warning (0, "passing argument %d of %qE as floating "
2741 "rather than integer due to prototype",
2743 else if (TREE_CODE (type) == COMPLEX_TYPE
2744 && INTEGRAL_TYPE_P (valtype))
2745 warning (0, "passing argument %d of %qE as complex "
2746 "rather than integer due to prototype",
2748 else if (TREE_CODE (type) == REAL_TYPE
2749 && TREE_CODE (valtype) == COMPLEX_TYPE)
2750 warning (0, "passing argument %d of %qE as floating "
2751 "rather than complex due to prototype",
2753 /* ??? At some point, messages should be written about
2754 conversions between complex types, but that's too messy
2756 else if (TREE_CODE (type) == REAL_TYPE
2757 && TREE_CODE (valtype) == REAL_TYPE)
2759 /* Warn if any argument is passed as `float',
2760 since without a prototype it would be `double'. */
2761 if (formal_prec == TYPE_PRECISION (float_type_node)
2762 && type != dfloat32_type_node)
2763 warning (0, "passing argument %d of %qE as %<float%> "
2764 "rather than %<double%> due to prototype",
2767 /* Warn if mismatch between argument and prototype
2768 for decimal float types. Warn of conversions with
2769 binary float types and of precision narrowing due to
2771 else if (type != valtype
2772 && (type == dfloat32_type_node
2773 || type == dfloat64_type_node
2774 || type == dfloat128_type_node
2775 || valtype == dfloat32_type_node
2776 || valtype == dfloat64_type_node
2777 || valtype == dfloat128_type_node)
2779 <= TYPE_PRECISION (valtype)
2780 || (type == dfloat128_type_node
2782 != dfloat64_type_node
2784 != dfloat32_type_node)))
2785 || (type == dfloat64_type_node
2787 != dfloat32_type_node))))
2788 warning (0, "passing argument %d of %qE as %qT "
2789 "rather than %qT due to prototype",
2790 argnum, rname, type, valtype);
2793 /* Detect integer changing in width or signedness.
2794 These warnings are only activated with
2795 -Wtraditional-conversion, not with -Wtraditional. */
2796 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2797 && INTEGRAL_TYPE_P (valtype))
2799 tree would_have_been = default_conversion (val);
2800 tree type1 = TREE_TYPE (would_have_been);
2802 if (TREE_CODE (type) == ENUMERAL_TYPE
2803 && (TYPE_MAIN_VARIANT (type)
2804 == TYPE_MAIN_VARIANT (valtype)))
2805 /* No warning if function asks for enum
2806 and the actual arg is that enum type. */
2808 else if (formal_prec != TYPE_PRECISION (type1))
2809 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2810 "with different width due to prototype",
2812 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2814 /* Don't complain if the formal parameter type
2815 is an enum, because we can't tell now whether
2816 the value was an enum--even the same enum. */
2817 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2819 else if (TREE_CODE (val) == INTEGER_CST
2820 && int_fits_type_p (val, type))
2821 /* Change in signedness doesn't matter
2822 if a constant value is unaffected. */
2824 /* If the value is extended from a narrower
2825 unsigned type, it doesn't matter whether we
2826 pass it as signed or unsigned; the value
2827 certainly is the same either way. */
2828 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
2829 && TYPE_UNSIGNED (valtype))
2831 else if (TYPE_UNSIGNED (type))
2832 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2833 "as unsigned due to prototype",
2836 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2837 "as signed due to prototype", argnum, rname);
2841 /* Possibly restore an EXCESS_PRECISION_EXPR for the
2842 sake of better warnings from convert_and_check. */
2843 if (excess_precision)
2844 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
2845 origtype = (origtypes == NULL
2847 : VEC_index (tree, origtypes, parmnum));
2848 parmval = convert_for_assignment (type, val, origtype,
2853 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2854 && INTEGRAL_TYPE_P (type)
2855 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2856 parmval = default_conversion (parmval);
2859 else if (TREE_CODE (valtype) == REAL_TYPE
2860 && (TYPE_PRECISION (valtype)
2861 < TYPE_PRECISION (double_type_node))
2862 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
2867 /* Convert `float' to `double'. */
2868 parmval = convert (double_type_node, val);
2870 else if (excess_precision && !type_generic)
2871 /* A "double" argument with excess precision being passed
2872 without a prototype or in variable arguments. */
2873 parmval = convert (valtype, val);
2874 else if ((invalid_func_diag =
2875 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2877 error (invalid_func_diag);
2881 /* Convert `short' and `char' to full-size `int'. */
2882 parmval = default_conversion (val);
2884 VEC_replace (tree, values, parmnum, parmval);
2887 typetail = TREE_CHAIN (typetail);
2890 gcc_assert (parmnum == VEC_length (tree, values));
2892 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2894 error ("too few arguments to function %qE", function);
2901 /* This is the entry point used by the parser to build unary operators
2902 in the input. CODE, a tree_code, specifies the unary operator, and
2903 ARG is the operand. For unary plus, the C parser currently uses
2904 CONVERT_EXPR for code.
2906 LOC is the location to use for the tree generated.
2910 parser_build_unary_op (enum tree_code code, struct c_expr arg, location_t loc)
2912 struct c_expr result;
2914 result.value = build_unary_op (loc, code, arg.value, 0);
2915 result.original_code = code;
2916 result.original_type = NULL;
2918 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2919 overflow_warning (result.value);
2924 /* This is the entry point used by the parser to build binary operators
2925 in the input. CODE, a tree_code, specifies the binary operator, and
2926 ARG1 and ARG2 are the operands. In addition to constructing the
2927 expression, we check for operands that were written with other binary
2928 operators in a way that is likely to confuse the user.
2930 LOCATION is the location of the binary operator. */
2933 parser_build_binary_op (location_t location, enum tree_code code,
2934 struct c_expr arg1, struct c_expr arg2)
2936 struct c_expr result;
2938 enum tree_code code1 = arg1.original_code;
2939 enum tree_code code2 = arg2.original_code;
2940 tree type1 = (arg1.original_type
2941 ? arg1.original_type
2942 : TREE_TYPE (arg1.value));
2943 tree type2 = (arg2.original_type
2944 ? arg2.original_type
2945 : TREE_TYPE (arg2.value));
2947 result.value = build_binary_op (location, code,
2948 arg1.value, arg2.value, 1);
2949 result.original_code = code;
2950 result.original_type = NULL;
2952 if (TREE_CODE (result.value) == ERROR_MARK)
2955 if (location != UNKNOWN_LOCATION)
2956 protected_set_expr_location (result.value, location);
2958 /* Check for cases such as x+y<<z which users are likely
2960 if (warn_parentheses)
2961 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
2963 if (warn_logical_op)
2964 warn_logical_operator (input_location, code,
2965 code1, arg1.value, code2, arg2.value);
2967 /* Warn about comparisons against string literals, with the exception
2968 of testing for equality or inequality of a string literal with NULL. */
2969 if (code == EQ_EXPR || code == NE_EXPR)
2971 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2972 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2973 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2975 else if (TREE_CODE_CLASS (code) == tcc_comparison
2976 && (code1 == STRING_CST || code2 == STRING_CST))
2977 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2979 if (TREE_OVERFLOW_P (result.value)
2980 && !TREE_OVERFLOW_P (arg1.value)
2981 && !TREE_OVERFLOW_P (arg2.value))
2982 overflow_warning (result.value);
2984 /* Warn about comparisons of different enum types. */
2985 if (warn_enum_compare
2986 && TREE_CODE_CLASS (code) == tcc_comparison
2987 && TREE_CODE (type1) == ENUMERAL_TYPE
2988 && TREE_CODE (type2) == ENUMERAL_TYPE
2989 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
2990 warning_at (location, OPT_Wenum_compare,
2991 "comparison between %qT and %qT",
2997 /* Return a tree for the difference of pointers OP0 and OP1.
2998 The resulting tree has type int. */
3001 pointer_diff (tree op0, tree op1)
3003 tree restype = ptrdiff_type_node;
3005 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3006 tree con0, con1, lit0, lit1;
3007 tree orig_op1 = op1;
3009 if (TREE_CODE (target_type) == VOID_TYPE)
3010 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3011 "pointer of type %<void *%> used in subtraction");
3012 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3013 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3014 "pointer to a function used in subtraction");
3016 /* If the conversion to ptrdiff_type does anything like widening or
3017 converting a partial to an integral mode, we get a convert_expression
3018 that is in the way to do any simplifications.
3019 (fold-const.c doesn't know that the extra bits won't be needed.
3020 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3021 different mode in place.)
3022 So first try to find a common term here 'by hand'; we want to cover
3023 at least the cases that occur in legal static initializers. */
3024 if (CONVERT_EXPR_P (op0)
3025 && (TYPE_PRECISION (TREE_TYPE (op0))
3026 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3027 con0 = TREE_OPERAND (op0, 0);
3030 if (CONVERT_EXPR_P (op1)
3031 && (TYPE_PRECISION (TREE_TYPE (op1))
3032 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3033 con1 = TREE_OPERAND (op1, 0);
3037 if (TREE_CODE (con0) == PLUS_EXPR)
3039 lit0 = TREE_OPERAND (con0, 1);
3040 con0 = TREE_OPERAND (con0, 0);
3043 lit0 = integer_zero_node;
3045 if (TREE_CODE (con1) == PLUS_EXPR)
3047 lit1 = TREE_OPERAND (con1, 1);
3048 con1 = TREE_OPERAND (con1, 0);
3051 lit1 = integer_zero_node;
3053 if (operand_equal_p (con0, con1, 0))
3060 /* First do the subtraction as integers;
3061 then drop through to build the divide operator.
3062 Do not do default conversions on the minus operator
3063 in case restype is a short type. */
3065 op0 = build_binary_op (input_location,
3066 MINUS_EXPR, convert (restype, op0),
3067 convert (restype, op1), 0);
3068 /* This generates an error if op1 is pointer to incomplete type. */
3069 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3070 error ("arithmetic on pointer to an incomplete type");
3072 /* This generates an error if op0 is pointer to incomplete type. */
3073 op1 = c_size_in_bytes (target_type);
3075 /* Divide by the size, in easiest possible way. */
3076 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3079 /* Construct and perhaps optimize a tree representation
3080 for a unary operation. CODE, a tree_code, specifies the operation
3081 and XARG is the operand.
3082 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3083 the default promotions (such as from short to int).
3084 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3085 allows non-lvalues; this is only used to handle conversion of non-lvalue
3086 arrays to pointers in C99.
3088 LOCATION is the location of the operator. */
3091 build_unary_op (location_t location,
3092 enum tree_code code, tree xarg, int flag)
3094 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3097 enum tree_code typecode;
3099 tree ret = error_mark_node;
3100 tree eptype = NULL_TREE;
3101 int noconvert = flag;
3102 const char *invalid_op_diag;
3105 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3107 arg = remove_c_maybe_const_expr (arg);
3109 if (code != ADDR_EXPR)
3110 arg = require_complete_type (arg);
3112 typecode = TREE_CODE (TREE_TYPE (arg));
3113 if (typecode == ERROR_MARK)
3114 return error_mark_node;
3115 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3116 typecode = INTEGER_TYPE;
3118 if ((invalid_op_diag
3119 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3121 error_at (location, invalid_op_diag);
3122 return error_mark_node;
3125 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3127 eptype = TREE_TYPE (arg);
3128 arg = TREE_OPERAND (arg, 0);
3134 /* This is used for unary plus, because a CONVERT_EXPR
3135 is enough to prevent anybody from looking inside for
3136 associativity, but won't generate any code. */
3137 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3138 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3139 || typecode == VECTOR_TYPE))
3141 error_at (location, "wrong type argument to unary plus");
3142 return error_mark_node;
3144 else if (!noconvert)
3145 arg = default_conversion (arg);
3146 arg = non_lvalue (arg);
3150 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3151 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3152 || typecode == VECTOR_TYPE))
3154 error_at (location, "wrong type argument to unary minus");
3155 return error_mark_node;
3157 else if (!noconvert)
3158 arg = default_conversion (arg);
3162 /* ~ works on integer types and non float vectors. */
3163 if (typecode == INTEGER_TYPE
3164 || (typecode == VECTOR_TYPE
3165 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3168 arg = default_conversion (arg);
3170 else if (typecode == COMPLEX_TYPE)
3173 pedwarn (location, OPT_pedantic,
3174 "ISO C does not support %<~%> for complex conjugation");
3176 arg = default_conversion (arg);
3180 error_at (location, "wrong type argument to bit-complement");
3181 return error_mark_node;
3186 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3188 error_at (location, "wrong type argument to abs");
3189 return error_mark_node;
3191 else if (!noconvert)
3192 arg = default_conversion (arg);
3196 /* Conjugating a real value is a no-op, but allow it anyway. */
3197 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3198 || typecode == COMPLEX_TYPE))
3200 error_at (location, "wrong type argument to conjugation");
3201 return error_mark_node;
3203 else if (!noconvert)
3204 arg = default_conversion (arg);
3207 case TRUTH_NOT_EXPR:
3208 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3209 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3210 && typecode != COMPLEX_TYPE)
3213 "wrong type argument to unary exclamation mark");
3214 return error_mark_node;
3216 arg = c_objc_common_truthvalue_conversion (location, arg);
3217 ret = invert_truthvalue (arg);
3218 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3219 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3220 location = EXPR_LOCATION (ret);
3221 goto return_build_unary_op;
3224 if (TREE_CODE (arg) == COMPLEX_CST)
3225 ret = TREE_REALPART (arg);
3226 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3227 ret = fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3230 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3231 eptype = TREE_TYPE (eptype);
3232 goto return_build_unary_op;
3235 if (TREE_CODE (arg) == COMPLEX_CST)
3236 ret = TREE_IMAGPART (arg);
3237 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3238 ret = fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3240 ret = omit_one_operand (TREE_TYPE (arg), integer_zero_node, arg);
3241 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3242 eptype = TREE_TYPE (eptype);
3243 goto return_build_unary_op;
3245 case PREINCREMENT_EXPR:
3246 case POSTINCREMENT_EXPR:
3247 case PREDECREMENT_EXPR:
3248 case POSTDECREMENT_EXPR:
3250 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3252 tree inner = build_unary_op (location, code,
3253 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3254 if (inner == error_mark_node)
3255 return error_mark_node;
3256 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3257 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3258 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3259 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3260 goto return_build_unary_op;
3263 /* Complain about anything that is not a true lvalue. */
3264 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3265 || code == POSTINCREMENT_EXPR)
3268 return error_mark_node;
3270 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3272 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3273 warning_at (location, OPT_Wc___compat,
3274 "increment of enumeration value is invalid in C++");
3276 warning_at (location, OPT_Wc___compat,
3277 "decrement of enumeration value is invalid in C++");
3280 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3281 arg = c_fully_fold (arg, false, NULL);
3283 /* Increment or decrement the real part of the value,
3284 and don't change the imaginary part. */
3285 if (typecode == COMPLEX_TYPE)
3289 pedwarn (location, OPT_pedantic,
3290 "ISO C does not support %<++%> and %<--%> on complex types");
3292 arg = stabilize_reference (arg);
3293 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3294 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3295 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3296 if (real == error_mark_node || imag == error_mark_node)
3297 return error_mark_node;
3298 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3300 goto return_build_unary_op;
3303 /* Report invalid types. */
3305 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3306 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3308 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3309 error_at (location, "wrong type argument to increment");
3311 error_at (location, "wrong type argument to decrement");
3313 return error_mark_node;
3319 argtype = TREE_TYPE (arg);
3321 /* Compute the increment. */
3323 if (typecode == POINTER_TYPE)
3325 /* If pointer target is an undefined struct,
3326 we just cannot know how to do the arithmetic. */
3327 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3329 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3331 "increment of pointer to unknown structure");
3334 "decrement of pointer to unknown structure");
3336 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3337 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3339 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3340 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3341 "wrong type argument to increment");
3343 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3344 "wrong type argument to decrement");
3347 inc = c_size_in_bytes (TREE_TYPE (argtype));
3348 inc = fold_convert (sizetype, inc);
3350 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3352 /* For signed fract types, we invert ++ to -- or
3353 -- to ++, and change inc from 1 to -1, because
3354 it is not possible to represent 1 in signed fract constants.
3355 For unsigned fract types, the result always overflows and
3356 we get an undefined (original) or the maximum value. */
3357 if (code == PREINCREMENT_EXPR)
3358 code = PREDECREMENT_EXPR;
3359 else if (code == PREDECREMENT_EXPR)
3360 code = PREINCREMENT_EXPR;
3361 else if (code == POSTINCREMENT_EXPR)
3362 code = POSTDECREMENT_EXPR;
3363 else /* code == POSTDECREMENT_EXPR */
3364 code = POSTINCREMENT_EXPR;
3366 inc = integer_minus_one_node;
3367 inc = convert (argtype, inc);
3371 inc = integer_one_node;
3372 inc = convert (argtype, inc);
3375 /* Report a read-only lvalue. */
3376 if (TYPE_READONLY (argtype))
3378 readonly_error (arg,
3379 ((code == PREINCREMENT_EXPR
3380 || code == POSTINCREMENT_EXPR)
3381 ? lv_increment : lv_decrement));
3382 return error_mark_node;
3384 else if (TREE_READONLY (arg))
3385 readonly_warning (arg,
3386 ((code == PREINCREMENT_EXPR
3387 || code == POSTINCREMENT_EXPR)
3388 ? lv_increment : lv_decrement));
3390 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3391 val = boolean_increment (code, arg);
3393 val = build2 (code, TREE_TYPE (arg), arg, inc);
3394 TREE_SIDE_EFFECTS (val) = 1;
3395 if (TREE_CODE (val) != code)
3396 TREE_NO_WARNING (val) = 1;
3398 goto return_build_unary_op;
3402 /* Note that this operation never does default_conversion. */
3404 /* The operand of unary '&' must be an lvalue (which excludes
3405 expressions of type void), or, in C99, the result of a [] or
3406 unary '*' operator. */
3407 if (VOID_TYPE_P (TREE_TYPE (arg))
3408 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3409 && (TREE_CODE (arg) != INDIRECT_REF
3411 pedwarn (location, 0, "taking address of expression of type %<void%>");
3413 /* Let &* cancel out to simplify resulting code. */
3414 if (TREE_CODE (arg) == INDIRECT_REF)
3416 /* Don't let this be an lvalue. */
3417 if (lvalue_p (TREE_OPERAND (arg, 0)))
3418 return non_lvalue (TREE_OPERAND (arg, 0));
3419 ret = TREE_OPERAND (arg, 0);
3420 goto return_build_unary_op;
3423 /* For &x[y], return x+y */
3424 if (TREE_CODE (arg) == ARRAY_REF)
3426 tree op0 = TREE_OPERAND (arg, 0);
3427 if (!c_mark_addressable (op0))
3428 return error_mark_node;
3429 return build_binary_op (location, PLUS_EXPR,
3430 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3431 ? array_to_pointer_conversion (op0)
3433 TREE_OPERAND (arg, 1), 1);
3436 /* Anything not already handled and not a true memory reference
3437 or a non-lvalue array is an error. */
3438 else if (typecode != FUNCTION_TYPE && !flag
3439 && !lvalue_or_else (arg, lv_addressof))
3440 return error_mark_node;
3442 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3444 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3446 tree inner = build_unary_op (location, code,
3447 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3448 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3449 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3450 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3451 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3452 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3453 goto return_build_unary_op;
3456 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3457 argtype = TREE_TYPE (arg);
3459 /* If the lvalue is const or volatile, merge that into the type
3460 to which the address will point. Note that you can't get a
3461 restricted pointer by taking the address of something, so we
3462 only have to deal with `const' and `volatile' here. */
3463 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3464 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3465 argtype = c_build_type_variant (argtype,
3466 TREE_READONLY (arg),
3467 TREE_THIS_VOLATILE (arg));
3469 if (!c_mark_addressable (arg))
3470 return error_mark_node;
3472 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3473 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3475 argtype = build_pointer_type (argtype);
3477 /* ??? Cope with user tricks that amount to offsetof. Delete this
3478 when we have proper support for integer constant expressions. */
3479 val = get_base_address (arg);
3480 if (val && TREE_CODE (val) == INDIRECT_REF
3481 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3483 tree op0 = fold_convert (sizetype, fold_offsetof (arg, val)), op1;
3485 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3486 ret = fold_build2 (POINTER_PLUS_EXPR, argtype, op1, op0);
3487 goto return_build_unary_op;
3490 val = build1 (ADDR_EXPR, argtype, arg);
3493 goto return_build_unary_op;
3500 argtype = TREE_TYPE (arg);
3501 if (TREE_CODE (arg) == INTEGER_CST)
3502 ret = (require_constant_value
3503 ? fold_build1_initializer (code, argtype, arg)
3504 : fold_build1 (code, argtype, arg));
3506 ret = build1 (code, argtype, arg);
3507 return_build_unary_op:
3508 gcc_assert (ret != error_mark_node);
3509 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3510 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3511 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3512 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3513 ret = note_integer_operands (ret);
3515 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3516 protected_set_expr_location (ret, location);
3520 /* Return nonzero if REF is an lvalue valid for this language.
3521 Lvalues can be assigned, unless their type has TYPE_READONLY.
3522 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3525 lvalue_p (const_tree ref)
3527 const enum tree_code code = TREE_CODE (ref);
3534 return lvalue_p (TREE_OPERAND (ref, 0));
3536 case C_MAYBE_CONST_EXPR:
3537 return lvalue_p (TREE_OPERAND (ref, 1));
3539 case COMPOUND_LITERAL_EXPR:
3549 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3550 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3553 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3560 /* Give an error for storing in something that is 'const'. */
3563 readonly_error (tree arg, enum lvalue_use use)
3565 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3567 /* Using this macro rather than (for example) arrays of messages
3568 ensures that all the format strings are checked at compile
3570 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3571 : (use == lv_increment ? (I) \
3572 : (use == lv_decrement ? (D) : (AS))))
3573 if (TREE_CODE (arg) == COMPONENT_REF)
3575 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3576 readonly_error (TREE_OPERAND (arg, 0), use);
3578 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3579 G_("increment of read-only member %qD"),
3580 G_("decrement of read-only member %qD"),
3581 G_("read-only member %qD used as %<asm%> output")),
3582 TREE_OPERAND (arg, 1));
3584 else if (TREE_CODE (arg) == VAR_DECL)
3585 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3586 G_("increment of read-only variable %qD"),
3587 G_("decrement of read-only variable %qD"),
3588 G_("read-only variable %qD used as %<asm%> output")),
3591 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3592 G_("increment of read-only location %qE"),
3593 G_("decrement of read-only location %qE"),
3594 G_("read-only location %qE used as %<asm%> output")),
3598 /* Give a warning for storing in something that is read-only in GCC
3599 terms but not const in ISO C terms. */
3602 readonly_warning (tree arg, enum lvalue_use use)
3607 warning (0, "assignment of read-only location %qE", arg);
3610 warning (0, "increment of read-only location %qE", arg);
3613 warning (0, "decrement of read-only location %qE", arg);
3622 /* Return nonzero if REF is an lvalue valid for this language;
3623 otherwise, print an error message and return zero. USE says
3624 how the lvalue is being used and so selects the error message. */
3627 lvalue_or_else (const_tree ref, enum lvalue_use use)
3629 int win = lvalue_p (ref);
3637 /* Mark EXP saying that we need to be able to take the
3638 address of it; it should not be allocated in a register.
3639 Returns true if successful. */
3642 c_mark_addressable (tree exp)
3647 switch (TREE_CODE (x))
3650 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3653 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3657 /* ... fall through ... */
3663 x = TREE_OPERAND (x, 0);
3666 case COMPOUND_LITERAL_EXPR:
3668 TREE_ADDRESSABLE (x) = 1;
3675 if (C_DECL_REGISTER (x)
3676 && DECL_NONLOCAL (x))
3678 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3681 ("global register variable %qD used in nested function", x);
3684 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3686 else if (C_DECL_REGISTER (x))
3688 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3689 error ("address of global register variable %qD requested", x);
3691 error ("address of register variable %qD requested", x);
3697 TREE_ADDRESSABLE (x) = 1;
3704 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3705 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3706 if folded to an integer constant then the unselected half may
3707 contain arbitrary operations not normally permitted in constant
3711 build_conditional_expr (tree ifexp, bool ifexp_bcp, tree op1, tree op2)
3715 enum tree_code code1;
3716 enum tree_code code2;
3717 tree result_type = NULL;
3718 tree ep_result_type = NULL;
3719 tree orig_op1 = op1, orig_op2 = op2;
3720 bool int_const, op1_int_operands, op2_int_operands, int_operands;
3721 bool ifexp_int_operands;
3725 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
3726 if (op1_int_operands)
3727 op1 = remove_c_maybe_const_expr (op1);
3728 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
3729 if (op2_int_operands)
3730 op2 = remove_c_maybe_const_expr (op2);
3731 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
3732 if (ifexp_int_operands)
3733 ifexp = remove_c_maybe_const_expr (ifexp);
3735 /* Promote both alternatives. */
3737 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3738 op1 = default_conversion (op1);
3739 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3740 op2 = default_conversion (op2);
3742 if (TREE_CODE (ifexp) == ERROR_MARK
3743 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3744 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3745 return error_mark_node;
3747 type1 = TREE_TYPE (op1);
3748 code1 = TREE_CODE (type1);
3749 type2 = TREE_TYPE (op2);
3750 code2 = TREE_CODE (type2);
3752 /* C90 does not permit non-lvalue arrays in conditional expressions.
3753 In C99 they will be pointers by now. */
3754 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3756 error ("non-lvalue array in conditional expression");
3757 return error_mark_node;
3760 objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE);
3762 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
3763 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3764 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3765 || code1 == COMPLEX_TYPE)
3766 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3767 || code2 == COMPLEX_TYPE))
3769 ep_result_type = c_common_type (type1, type2);
3770 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
3772 op1 = TREE_OPERAND (op1, 0);
3773 type1 = TREE_TYPE (op1);
3774 gcc_assert (TREE_CODE (type1) == code1);
3776 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3778 op2 = TREE_OPERAND (op2, 0);
3779 type2 = TREE_TYPE (op2);
3780 gcc_assert (TREE_CODE (type2) == code2);
3784 /* Quickly detect the usual case where op1 and op2 have the same type
3786 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3789 result_type = type1;
3791 result_type = TYPE_MAIN_VARIANT (type1);
3793 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3794 || code1 == COMPLEX_TYPE)
3795 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3796 || code2 == COMPLEX_TYPE))
3798 result_type = c_common_type (type1, type2);
3800 /* If -Wsign-compare, warn here if type1 and type2 have
3801 different signedness. We'll promote the signed to unsigned
3802 and later code won't know it used to be different.
3803 Do this check on the original types, so that explicit casts
3804 will be considered, but default promotions won't. */
3805 if (!skip_evaluation)
3807 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3808 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3810 if (unsigned_op1 ^ unsigned_op2)
3814 /* Do not warn if the result type is signed, since the
3815 signed type will only be chosen if it can represent
3816 all the values of the unsigned type. */
3817 if (!TYPE_UNSIGNED (result_type))
3821 bool op1_maybe_const = true;
3822 bool op2_maybe_const = true;
3824 /* Do not warn if the signed quantity is an
3825 unsuffixed integer literal (or some static
3826 constant expression involving such literals) and
3827 it is non-negative. This warning requires the
3828 operands to be folded for best results, so do
3829 that folding in this case even without
3830 warn_sign_compare to avoid warning options
3831 possibly affecting code generation. */
3832 op1 = c_fully_fold (op1, require_constant_value,
3834 op2 = c_fully_fold (op2, require_constant_value,
3837 if (warn_sign_compare)
3840 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3842 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3845 warning (OPT_Wsign_compare, "signed and unsigned type in conditional expression");
3847 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
3849 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
3851 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
3853 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
3855 op2 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op2),
3857 C_MAYBE_CONST_EXPR_NON_CONST (op2) = !op2_maybe_const;
3863 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3865 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
3866 pedwarn (input_location, OPT_pedantic,
3867 "ISO C forbids conditional expr with only one void side");
3868 result_type = void_type_node;
3870 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3872 if (comp_target_types (type1, type2))
3873 result_type = common_pointer_type (type1, type2);
3874 else if (null_pointer_constant_p (orig_op1))
3875 result_type = qualify_type (type2, type1);
3876 else if (null_pointer_constant_p (orig_op2))
3877 result_type = qualify_type (type1, type2);
3878 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3880 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3881 pedwarn (input_location, OPT_pedantic,
3882 "ISO C forbids conditional expr between "
3883 "%<void *%> and function pointer");
3884 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3885 TREE_TYPE (type2)));
3887 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3889 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3890 pedwarn (input_location, OPT_pedantic,
3891 "ISO C forbids conditional expr between "
3892 "%<void *%> and function pointer");
3893 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3894 TREE_TYPE (type1)));
3899 pedwarn (input_location, 0,
3900 "pointer type mismatch in conditional expression");
3901 result_type = build_pointer_type (void_type_node);
3904 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3906 if (!null_pointer_constant_p (orig_op2))
3907 pedwarn (input_location, 0,
3908 "pointer/integer type mismatch in conditional expression");
3911 op2 = null_pointer_node;
3913 result_type = type1;
3915 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3917 if (!null_pointer_constant_p (orig_op1))
3918 pedwarn (input_location, 0,
3919 "pointer/integer type mismatch in conditional expression");
3922 op1 = null_pointer_node;
3924 result_type = type2;
3929 if (flag_cond_mismatch)
3930 result_type = void_type_node;
3933 error ("type mismatch in conditional expression");
3934 return error_mark_node;
3938 /* Merge const and volatile flags of the incoming types. */
3940 = build_type_variant (result_type,
3941 TREE_READONLY (op1) || TREE_READONLY (op2),
3942 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3944 if (result_type != TREE_TYPE (op1))
3945 op1 = convert_and_check (result_type, op1);
3946 if (result_type != TREE_TYPE (op2))
3947 op2 = convert_and_check (result_type, op2);
3949 if (ifexp_bcp && ifexp == truthvalue_true_node)
3951 op2_int_operands = true;
3952 op1 = c_fully_fold (op1, require_constant_value, NULL);
3954 if (ifexp_bcp && ifexp == truthvalue_false_node)
3956 op1_int_operands = true;
3957 op2 = c_fully_fold (op2, require_constant_value, NULL);
3959 int_const = int_operands = (ifexp_int_operands
3961 && op2_int_operands);
3964 int_const = ((ifexp == truthvalue_true_node
3965 && TREE_CODE (orig_op1) == INTEGER_CST
3966 && !TREE_OVERFLOW (orig_op1))
3967 || (ifexp == truthvalue_false_node
3968 && TREE_CODE (orig_op2) == INTEGER_CST
3969 && !TREE_OVERFLOW (orig_op2)));
3971 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
3972 ret = fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3975 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
3977 ret = note_integer_operands (ret);
3980 ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret);
3985 /* Return a compound expression that performs two expressions and
3986 returns the value of the second of them. */
3989 build_compound_expr (tree expr1, tree expr2)
3991 bool expr1_int_operands, expr2_int_operands;
3992 tree eptype = NULL_TREE;
3995 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
3996 if (expr1_int_operands)
3997 expr1 = remove_c_maybe_const_expr (expr1);
3998 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
3999 if (expr2_int_operands)
4000 expr2 = remove_c_maybe_const_expr (expr2);
4002 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4003 expr1 = TREE_OPERAND (expr1, 0);
4004 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4006 eptype = TREE_TYPE (expr2);
4007 expr2 = TREE_OPERAND (expr2, 0);
4010 if (!TREE_SIDE_EFFECTS (expr1))
4012 /* The left-hand operand of a comma expression is like an expression
4013 statement: with -Wunused, we should warn if it doesn't have
4014 any side-effects, unless it was explicitly cast to (void). */
4015 if (warn_unused_value)
4017 if (VOID_TYPE_P (TREE_TYPE (expr1))
4018 && CONVERT_EXPR_P (expr1))
4020 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4021 && TREE_CODE (expr1) == COMPOUND_EXPR
4022 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4023 ; /* (void) a, (void) b, c */
4025 warning (OPT_Wunused_value,
4026 "left-hand operand of comma expression has no effect");
4030 /* With -Wunused, we should also warn if the left-hand operand does have
4031 side-effects, but computes a value which is not used. For example, in
4032 `foo() + bar(), baz()' the result of the `+' operator is not used,
4033 so we should issue a warning. */
4034 else if (warn_unused_value)
4035 warn_if_unused_value (expr1, input_location);
4037 if (expr2 == error_mark_node)
4038 return error_mark_node;
4040 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4043 && expr1_int_operands
4044 && expr2_int_operands)
4045 ret = note_integer_operands (ret);
4048 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4053 /* Build an expression representing a cast to type TYPE of expression EXPR. */
4056 build_c_cast (tree type, tree expr)
4060 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4061 expr = TREE_OPERAND (expr, 0);
4065 if (type == error_mark_node || expr == error_mark_node)
4066 return error_mark_node;
4068 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4069 only in <protocol> qualifications. But when constructing cast expressions,
4070 the protocols do matter and must be kept around. */
4071 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4072 return build1 (NOP_EXPR, type, expr);
4074 type = TYPE_MAIN_VARIANT (type);
4076 if (TREE_CODE (type) == ARRAY_TYPE)
4078 error ("cast specifies array type");
4079 return error_mark_node;
4082 if (TREE_CODE (type) == FUNCTION_TYPE)
4084 error ("cast specifies function type");
4085 return error_mark_node;
4088 if (!VOID_TYPE_P (type))
4090 value = require_complete_type (value);
4091 if (value == error_mark_node)
4092 return error_mark_node;
4095 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4097 if (TREE_CODE (type) == RECORD_TYPE
4098 || TREE_CODE (type) == UNION_TYPE)
4099 pedwarn (input_location, OPT_pedantic,
4100 "ISO C forbids casting nonscalar to the same type");
4102 else if (TREE_CODE (type) == UNION_TYPE)
4106 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4107 if (TREE_TYPE (field) != error_mark_node
4108 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4109 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4116 pedwarn (input_location, OPT_pedantic,
4117 "ISO C forbids casts to union type");
4118 t = digest_init (type,
4119 build_constructor_single (type, field, value),
4120 NULL_TREE, false, true, 0);
4121 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4124 error ("cast to union type from type not present in union");
4125 return error_mark_node;
4131 if (type == void_type_node)
4132 return build1 (CONVERT_EXPR, type, value);
4134 otype = TREE_TYPE (value);
4136 /* Optionally warn about potentially worrisome casts. */
4139 && TREE_CODE (type) == POINTER_TYPE
4140 && TREE_CODE (otype) == POINTER_TYPE)
4142 tree in_type = type;
4143 tree in_otype = otype;
4147 /* Check that the qualifiers on IN_TYPE are a superset of
4148 the qualifiers of IN_OTYPE. The outermost level of
4149 POINTER_TYPE nodes is uninteresting and we stop as soon
4150 as we hit a non-POINTER_TYPE node on either type. */
4153 in_otype = TREE_TYPE (in_otype);
4154 in_type = TREE_TYPE (in_type);
4156 /* GNU C allows cv-qualified function types. 'const'
4157 means the function is very pure, 'volatile' means it
4158 can't return. We need to warn when such qualifiers
4159 are added, not when they're taken away. */
4160 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4161 && TREE_CODE (in_type) == FUNCTION_TYPE)
4162 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
4164 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
4166 while (TREE_CODE (in_type) == POINTER_TYPE
4167 && TREE_CODE (in_otype) == POINTER_TYPE);
4170 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
4173 /* There are qualifiers present in IN_OTYPE that are not
4174 present in IN_TYPE. */
4175 warning (OPT_Wcast_qual, "cast discards qualifiers from pointer target type");
4178 /* Warn about possible alignment problems. */
4179 if (STRICT_ALIGNMENT
4180 && TREE_CODE (type) == POINTER_TYPE
4181 && TREE_CODE (otype) == POINTER_TYPE
4182 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4183 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4184 /* Don't warn about opaque types, where the actual alignment
4185 restriction is unknown. */
4186 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4187 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4188 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4189 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4190 warning (OPT_Wcast_align,
4191 "cast increases required alignment of target type");
4193 if (TREE_CODE (type) == INTEGER_TYPE
4194 && TREE_CODE (otype) == POINTER_TYPE
4195 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4196 /* Unlike conversion of integers to pointers, where the
4197 warning is disabled for converting constants because
4198 of cases such as SIG_*, warn about converting constant
4199 pointers to integers. In some cases it may cause unwanted
4200 sign extension, and a warning is appropriate. */
4201 warning (OPT_Wpointer_to_int_cast,
4202 "cast from pointer to integer of different size");
4204 if (TREE_CODE (value) == CALL_EXPR
4205 && TREE_CODE (type) != TREE_CODE (otype))
4206 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
4207 "to non-matching type %qT", otype, type);
4209 if (TREE_CODE (type) == POINTER_TYPE
4210 && TREE_CODE (otype) == INTEGER_TYPE
4211 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4212 /* Don't warn about converting any constant. */
4213 && !TREE_CONSTANT (value))
4214 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4215 "of different size");
4217 if (warn_strict_aliasing <= 2)
4218 strict_aliasing_warning (otype, type, expr);
4220 /* If pedantic, warn for conversions between function and object
4221 pointer types, except for converting a null pointer constant
4222 to function pointer type. */
4224 && TREE_CODE (type) == POINTER_TYPE
4225 && TREE_CODE (otype) == POINTER_TYPE
4226 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4227 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4228 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4229 "conversion of function pointer to object pointer type");
4232 && TREE_CODE (type) == POINTER_TYPE
4233 && TREE_CODE (otype) == POINTER_TYPE
4234 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4235 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4236 && !null_pointer_constant_p (value))
4237 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4238 "conversion of object pointer to function pointer type");
4241 value = convert (type, value);
4243 /* Ignore any integer overflow caused by the cast. */
4244 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4246 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4248 if (!TREE_OVERFLOW (value))
4250 /* Avoid clobbering a shared constant. */
4251 value = copy_node (value);
4252 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4255 else if (TREE_OVERFLOW (value))
4256 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4257 value = build_int_cst_wide (TREE_TYPE (value),
4258 TREE_INT_CST_LOW (value),
4259 TREE_INT_CST_HIGH (value));
4263 /* Don't let a cast be an lvalue. */
4265 value = non_lvalue (value);
4267 /* Don't allow the results of casting to floating-point or complex
4268 types be confused with actual constants, or casts involving
4269 integer and pointer types other than direct integer-to-integer
4270 and integer-to-pointer be confused with integer constant
4271 expressions and null pointer constants. */
4272 if (TREE_CODE (value) == REAL_CST
4273 || TREE_CODE (value) == COMPLEX_CST
4274 || (TREE_CODE (value) == INTEGER_CST
4275 && !((TREE_CODE (expr) == INTEGER_CST
4276 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4277 || TREE_CODE (expr) == REAL_CST
4278 || TREE_CODE (expr) == COMPLEX_CST)))
4279 value = build1 (NOP_EXPR, type, value);
4284 /* Interpret a cast of expression EXPR to type TYPE. */
4286 c_cast_expr (struct c_type_name *type_name, tree expr, location_t loc)
4289 tree type_expr = NULL_TREE;
4290 bool type_expr_const = true;
4292 int saved_wsp = warn_strict_prototypes;
4294 /* This avoids warnings about unprototyped casts on
4295 integers. E.g. "#define SIG_DFL (void(*)())0". */
4296 if (TREE_CODE (expr) == INTEGER_CST)
4297 warn_strict_prototypes = 0;
4298 type = groktypename (type_name, &type_expr, &type_expr_const);
4299 warn_strict_prototypes = saved_wsp;
4301 ret = build_c_cast (type, expr);
4304 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4305 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4308 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4309 SET_EXPR_LOCATION (ret, loc);
4311 /* C++ does not permits types to be defined in a cast. */
4312 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4313 warning_at (loc, OPT_Wc___compat,
4314 "defining a type in a cast is invalid in C++");
4319 /* Build an assignment expression of lvalue LHS from value RHS.
4320 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4321 may differ from TREE_TYPE (LHS) for an enum bitfield.
4322 MODIFYCODE is the code for a binary operator that we use
4323 to combine the old value of LHS with RHS to get the new value.
4324 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4325 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4326 which may differ from TREE_TYPE (RHS) for an enum value.
4328 LOCATION is the location of the MODIFYCODE operator. */
4331 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4332 enum tree_code modifycode, tree rhs, tree rhs_origtype)
4336 tree rhs_semantic_type = NULL_TREE;
4337 tree lhstype = TREE_TYPE (lhs);
4338 tree olhstype = lhstype;
4341 /* Types that aren't fully specified cannot be used in assignments. */
4342 lhs = require_complete_type (lhs);
4344 /* Avoid duplicate error messages from operands that had errors. */
4345 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4346 return error_mark_node;
4348 if (!lvalue_or_else (lhs, lv_assign))
4349 return error_mark_node;
4351 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4353 rhs_semantic_type = TREE_TYPE (rhs);
4354 rhs = TREE_OPERAND (rhs, 0);
4359 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4361 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4362 lhs_origtype, modifycode, rhs,
4364 if (inner == error_mark_node)
4365 return error_mark_node;
4366 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4367 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4368 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4369 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4370 protected_set_expr_location (result, location);
4374 /* If a binary op has been requested, combine the old LHS value with the RHS
4375 producing the value we should actually store into the LHS. */
4377 if (modifycode != NOP_EXPR)
4379 lhs = c_fully_fold (lhs, false, NULL);
4380 lhs = stabilize_reference (lhs);
4381 newrhs = build_binary_op (location,
4382 modifycode, lhs, rhs, 1);
4384 /* The original type of the right hand side is no longer
4386 rhs_origtype = NULL_TREE;
4389 /* Give an error for storing in something that is 'const'. */
4391 if (TYPE_READONLY (lhstype)
4392 || ((TREE_CODE (lhstype) == RECORD_TYPE
4393 || TREE_CODE (lhstype) == UNION_TYPE)
4394 && C_TYPE_FIELDS_READONLY (lhstype)))
4396 readonly_error (lhs, lv_assign);
4397 return error_mark_node;
4399 else if (TREE_READONLY (lhs))
4400 readonly_warning (lhs, lv_assign);
4402 /* If storing into a structure or union member,
4403 it has probably been given type `int'.
4404 Compute the type that would go with
4405 the actual amount of storage the member occupies. */
4407 if (TREE_CODE (lhs) == COMPONENT_REF
4408 && (TREE_CODE (lhstype) == INTEGER_TYPE
4409 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4410 || TREE_CODE (lhstype) == REAL_TYPE
4411 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4412 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4414 /* If storing in a field that is in actuality a short or narrower than one,
4415 we must store in the field in its actual type. */
4417 if (lhstype != TREE_TYPE (lhs))
4419 lhs = copy_node (lhs);
4420 TREE_TYPE (lhs) = lhstype;
4423 /* Issue -Wc++-compat warnings about an assignment to an enum type
4424 when LHS does not have its original type. This happens for,
4425 e.g., an enum bitfield in a struct. */
4427 && lhs_origtype != NULL_TREE
4428 && lhs_origtype != lhstype
4429 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4431 tree checktype = (rhs_origtype != NULL_TREE
4434 if (checktype != error_mark_node
4435 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4436 warning_at (location, OPT_Wc___compat,
4437 "enum conversion in assignment is invalid in C++");
4440 /* Convert new value to destination type. Fold it first, then
4441 restore any excess precision information, for the sake of
4442 conversion warnings. */
4444 npc = null_pointer_constant_p (newrhs);
4445 newrhs = c_fully_fold (newrhs, false, NULL);
4446 if (rhs_semantic_type)
4447 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4448 newrhs = convert_for_assignment (lhstype, newrhs, rhs_origtype, ic_assign,
4449 npc, NULL_TREE, NULL_TREE, 0);
4450 if (TREE_CODE (newrhs) == ERROR_MARK)
4451 return error_mark_node;
4453 /* Emit ObjC write barrier, if necessary. */
4454 if (c_dialect_objc () && flag_objc_gc)
4456 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4459 protected_set_expr_location (result, location);
4464 /* Scan operands. */
4466 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4467 TREE_SIDE_EFFECTS (result) = 1;
4468 protected_set_expr_location (result, location);
4470 /* If we got the LHS in a different type for storing in,
4471 convert the result back to the nominal type of LHS
4472 so that the value we return always has the same type
4473 as the LHS argument. */
4475 if (olhstype == TREE_TYPE (result))
4478 result = convert_for_assignment (olhstype, result, rhs_origtype, ic_assign,
4479 false, NULL_TREE, NULL_TREE, 0);
4480 protected_set_expr_location (result, location);
4484 /* Convert value RHS to type TYPE as preparation for an assignment to
4485 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
4486 original type of RHS; this differs from TREE_TYPE (RHS) for enum
4487 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
4488 constant before any folding.
4489 The real work of conversion is done by `convert'.
4490 The purpose of this function is to generate error messages
4491 for assignments that are not allowed in C.
4492 ERRTYPE says whether it is argument passing, assignment,
4493 initialization or return.
4495 FUNCTION is a tree for the function being called.
4496 PARMNUM is the number of the argument, for printing in error messages. */
4499 convert_for_assignment (tree type, tree rhs, tree origtype,
4500 enum impl_conv errtype, bool null_pointer_constant,
4501 tree fundecl, tree function, int parmnum)
4503 enum tree_code codel = TREE_CODE (type);
4504 tree orig_rhs = rhs;
4506 enum tree_code coder;
4507 tree rname = NULL_TREE;
4508 bool objc_ok = false;
4510 if (errtype == ic_argpass)
4513 /* Change pointer to function to the function itself for
4515 if (TREE_CODE (function) == ADDR_EXPR
4516 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4517 function = TREE_OPERAND (function, 0);
4519 /* Handle an ObjC selector specially for diagnostics. */
4520 selector = objc_message_selector ();
4522 if (selector && parmnum > 2)
4529 /* This macro is used to emit diagnostics to ensure that all format
4530 strings are complete sentences, visible to gettext and checked at
4532 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4537 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4538 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4539 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4540 "expected %qT but argument is of type %qT", \
4544 pedwarn (LOCATION, OPT, AS); \
4547 pedwarn (LOCATION, OPT, IN); \
4550 pedwarn (LOCATION, OPT, RE); \
4553 gcc_unreachable (); \
4557 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4558 rhs = TREE_OPERAND (rhs, 0);
4560 rhstype = TREE_TYPE (rhs);
4561 coder = TREE_CODE (rhstype);
4563 if (coder == ERROR_MARK)
4564 return error_mark_node;
4566 if (c_dialect_objc ())
4589 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4592 if (warn_cxx_compat)
4594 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
4595 if (checktype != error_mark_node
4596 && TREE_CODE (type) == ENUMERAL_TYPE
4597 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
4599 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
4600 G_("enum conversion when passing argument "
4601 "%d of %qE is invalid in C++"),
4602 G_("enum conversion in assignment is "
4604 G_("enum conversion in initialization is "
4606 G_("enum conversion in return is "
4611 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4614 if (coder == VOID_TYPE)
4616 /* Except for passing an argument to an unprototyped function,
4617 this is a constraint violation. When passing an argument to
4618 an unprototyped function, it is compile-time undefined;
4619 making it a constraint in that case was rejected in
4621 error ("void value not ignored as it ought to be");
4622 return error_mark_node;
4624 rhs = require_complete_type (rhs);
4625 if (rhs == error_mark_node)
4626 return error_mark_node;
4627 /* A type converts to a reference to it.
4628 This code doesn't fully support references, it's just for the
4629 special case of va_start and va_copy. */
4630 if (codel == REFERENCE_TYPE
4631 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4633 if (!lvalue_p (rhs))
4635 error ("cannot pass rvalue to reference parameter");
4636 return error_mark_node;
4638 if (!c_mark_addressable (rhs))
4639 return error_mark_node;
4640 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4642 /* We already know that these two types are compatible, but they
4643 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4644 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4645 likely to be va_list, a typedef to __builtin_va_list, which
4646 is different enough that it will cause problems later. */
4647 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4648 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4650 rhs = build1 (NOP_EXPR, type, rhs);
4653 /* Some types can interconvert without explicit casts. */
4654 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4655 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4656 return convert (type, rhs);
4657 /* Arithmetic types all interconvert, and enum is treated like int. */
4658 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4659 || codel == FIXED_POINT_TYPE
4660 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4661 || codel == BOOLEAN_TYPE)
4662 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4663 || coder == FIXED_POINT_TYPE
4664 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4665 || coder == BOOLEAN_TYPE))
4668 bool save = in_late_binary_op;
4669 if (codel == BOOLEAN_TYPE)
4670 in_late_binary_op = true;
4671 ret = convert_and_check (type, orig_rhs);
4672 if (codel == BOOLEAN_TYPE)
4673 in_late_binary_op = save;
4677 /* Aggregates in different TUs might need conversion. */
4678 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4680 && comptypes (type, rhstype))
4681 return convert_and_check (type, rhs);
4683 /* Conversion to a transparent union from its member types.
4684 This applies only to function arguments. */
4685 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4686 && errtype == ic_argpass)
4688 tree memb, marginal_memb = NULL_TREE;
4690 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4692 tree memb_type = TREE_TYPE (memb);
4694 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4695 TYPE_MAIN_VARIANT (rhstype)))
4698 if (TREE_CODE (memb_type) != POINTER_TYPE)
4701 if (coder == POINTER_TYPE)
4703 tree ttl = TREE_TYPE (memb_type);
4704 tree ttr = TREE_TYPE (rhstype);
4706 /* Any non-function converts to a [const][volatile] void *
4707 and vice versa; otherwise, targets must be the same.
4708 Meanwhile, the lhs target must have all the qualifiers of
4710 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4711 || comp_target_types (memb_type, rhstype))
4713 /* If this type won't generate any warnings, use it. */
4714 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4715 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4716 && TREE_CODE (ttl) == FUNCTION_TYPE)
4717 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4718 == TYPE_QUALS (ttr))
4719 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4720 == TYPE_QUALS (ttl))))
4723 /* Keep looking for a better type, but remember this one. */
4725 marginal_memb = memb;
4729 /* Can convert integer zero to any pointer type. */
4730 if (null_pointer_constant)
4732 rhs = null_pointer_node;
4737 if (memb || marginal_memb)
4741 /* We have only a marginally acceptable member type;
4742 it needs a warning. */
4743 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4744 tree ttr = TREE_TYPE (rhstype);
4746 /* Const and volatile mean something different for function
4747 types, so the usual warnings are not appropriate. */
4748 if (TREE_CODE (ttr) == FUNCTION_TYPE
4749 && TREE_CODE (ttl) == FUNCTION_TYPE)
4751 /* Because const and volatile on functions are
4752 restrictions that say the function will not do
4753 certain things, it is okay to use a const or volatile
4754 function where an ordinary one is wanted, but not
4756 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4757 WARN_FOR_ASSIGNMENT (input_location, 0,
4758 G_("passing argument %d of %qE "
4759 "makes qualified function "
4760 "pointer from unqualified"),
4761 G_("assignment makes qualified "
4762 "function pointer from "
4764 G_("initialization makes qualified "
4765 "function pointer from "
4767 G_("return makes qualified function "
4768 "pointer from unqualified"));
4770 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4771 WARN_FOR_ASSIGNMENT (input_location, 0,
4772 G_("passing argument %d of %qE discards "
4773 "qualifiers from pointer target type"),
4774 G_("assignment discards qualifiers "
4775 "from pointer target type"),
4776 G_("initialization discards qualifiers "
4777 "from pointer target type"),
4778 G_("return discards qualifiers from "
4779 "pointer target type"));
4781 memb = marginal_memb;
4784 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
4785 pedwarn (input_location, OPT_pedantic,
4786 "ISO C prohibits argument conversion to union type");
4788 rhs = fold_convert (TREE_TYPE (memb), rhs);
4789 return build_constructor_single (type, memb, rhs);
4793 /* Conversions among pointers */
4794 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4795 && (coder == codel))
4797 tree ttl = TREE_TYPE (type);
4798 tree ttr = TREE_TYPE (rhstype);
4801 bool is_opaque_pointer;
4802 int target_cmp = 0; /* Cache comp_target_types () result. */
4804 if (TREE_CODE (mvl) != ARRAY_TYPE)
4805 mvl = TYPE_MAIN_VARIANT (mvl);
4806 if (TREE_CODE (mvr) != ARRAY_TYPE)
4807 mvr = TYPE_MAIN_VARIANT (mvr);
4808 /* Opaque pointers are treated like void pointers. */
4809 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
4811 /* C++ does not allow the implicit conversion void* -> T*. However,
4812 for the purpose of reducing the number of false positives, we
4813 tolerate the special case of
4817 where NULL is typically defined in C to be '(void *) 0'. */
4818 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4819 warning (OPT_Wc___compat, "request for implicit conversion from "
4820 "%qT to %qT not permitted in C++", rhstype, type);
4822 /* Check if the right-hand side has a format attribute but the
4823 left-hand side doesn't. */
4824 if (warn_missing_format_attribute
4825 && check_missing_format_attribute (type, rhstype))
4830 warning (OPT_Wmissing_format_attribute,
4831 "argument %d of %qE might be "
4832 "a candidate for a format attribute",
4836 warning (OPT_Wmissing_format_attribute,
4837 "assignment left-hand side might be "
4838 "a candidate for a format attribute");
4841 warning (OPT_Wmissing_format_attribute,
4842 "initialization left-hand side might be "
4843 "a candidate for a format attribute");
4846 warning (OPT_Wmissing_format_attribute,
4847 "return type might be "
4848 "a candidate for a format attribute");
4855 /* Any non-function converts to a [const][volatile] void *
4856 and vice versa; otherwise, targets must be the same.
4857 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4858 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4859 || (target_cmp = comp_target_types (type, rhstype))
4860 || is_opaque_pointer
4861 || (c_common_unsigned_type (mvl)
4862 == c_common_unsigned_type (mvr)))
4865 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4868 && !null_pointer_constant
4869 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4870 WARN_FOR_ASSIGNMENT (input_location, OPT_pedantic,
4871 G_("ISO C forbids passing argument %d of "
4872 "%qE between function pointer "
4874 G_("ISO C forbids assignment between "
4875 "function pointer and %<void *%>"),
4876 G_("ISO C forbids initialization between "
4877 "function pointer and %<void *%>"),
4878 G_("ISO C forbids return between function "
4879 "pointer and %<void *%>"));
4880 /* Const and volatile mean something different for function types,
4881 so the usual warnings are not appropriate. */
4882 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4883 && TREE_CODE (ttl) != FUNCTION_TYPE)
4885 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4887 /* Types differing only by the presence of the 'volatile'
4888 qualifier are acceptable if the 'volatile' has been added
4889 in by the Objective-C EH machinery. */
4890 if (!objc_type_quals_match (ttl, ttr))
4891 WARN_FOR_ASSIGNMENT (input_location, 0,
4892 G_("passing argument %d of %qE discards "
4893 "qualifiers from pointer target type"),
4894 G_("assignment discards qualifiers "
4895 "from pointer target type"),
4896 G_("initialization discards qualifiers "
4897 "from pointer target type"),
4898 G_("return discards qualifiers from "
4899 "pointer target type"));
4901 /* If this is not a case of ignoring a mismatch in signedness,
4903 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4906 /* If there is a mismatch, do warn. */
4907 else if (warn_pointer_sign)
4908 WARN_FOR_ASSIGNMENT (input_location, OPT_Wpointer_sign,
4909 G_("pointer targets in passing argument "
4910 "%d of %qE differ in signedness"),
4911 G_("pointer targets in assignment "
4912 "differ in signedness"),
4913 G_("pointer targets in initialization "
4914 "differ in signedness"),
4915 G_("pointer targets in return differ "
4918 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4919 && TREE_CODE (ttr) == FUNCTION_TYPE)
4921 /* Because const and volatile on functions are restrictions
4922 that say the function will not do certain things,
4923 it is okay to use a const or volatile function
4924 where an ordinary one is wanted, but not vice-versa. */
4925 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4926 WARN_FOR_ASSIGNMENT (input_location, 0,
4927 G_("passing argument %d of %qE makes "
4928 "qualified function pointer "
4929 "from unqualified"),
4930 G_("assignment makes qualified function "
4931 "pointer from unqualified"),
4932 G_("initialization makes qualified "
4933 "function pointer from unqualified"),
4934 G_("return makes qualified function "
4935 "pointer from unqualified"));
4939 /* Avoid warning about the volatile ObjC EH puts on decls. */
4941 WARN_FOR_ASSIGNMENT (input_location, 0,
4942 G_("passing argument %d of %qE from "
4943 "incompatible pointer type"),
4944 G_("assignment from incompatible pointer type"),
4945 G_("initialization from incompatible "
4947 G_("return from incompatible pointer type"));
4949 return convert (type, rhs);
4951 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4953 /* ??? This should not be an error when inlining calls to
4954 unprototyped functions. */
4955 error ("invalid use of non-lvalue array");
4956 return error_mark_node;
4958 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4960 /* An explicit constant 0 can convert to a pointer,
4961 or one that results from arithmetic, even including
4962 a cast to integer type. */
4963 if (!null_pointer_constant)
4964 WARN_FOR_ASSIGNMENT (input_location, 0,
4965 G_("passing argument %d of %qE makes "
4966 "pointer from integer without a cast"),
4967 G_("assignment makes pointer from integer "
4969 G_("initialization makes pointer from "
4970 "integer without a cast"),
4971 G_("return makes pointer from integer "
4974 return convert (type, rhs);
4976 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4978 WARN_FOR_ASSIGNMENT (input_location, 0,
4979 G_("passing argument %d of %qE makes integer "
4980 "from pointer without a cast"),
4981 G_("assignment makes integer from pointer "
4983 G_("initialization makes integer from pointer "
4985 G_("return makes integer from pointer "
4987 return convert (type, rhs);
4989 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4992 bool save = in_late_binary_op;
4993 in_late_binary_op = true;
4994 ret = convert (type, rhs);
4995 in_late_binary_op = save;
5002 error ("incompatible type for argument %d of %qE", parmnum, rname);
5003 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5004 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5005 "expected %qT but argument is of type %qT", type, rhstype);
5008 error ("incompatible types when assigning to type %qT from type %qT",
5012 error ("incompatible types when initializing type %qT using type %qT",
5016 error ("incompatible types when returning type %qT but %qT was expected",
5023 return error_mark_node;
5026 /* If VALUE is a compound expr all of whose expressions are constant, then
5027 return its value. Otherwise, return error_mark_node.
5029 This is for handling COMPOUND_EXPRs as initializer elements
5030 which is allowed with a warning when -pedantic is specified. */
5033 valid_compound_expr_initializer (tree value, tree endtype)
5035 if (TREE_CODE (value) == COMPOUND_EXPR)
5037 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5039 return error_mark_node;
5040 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5043 else if (!initializer_constant_valid_p (value, endtype))
5044 return error_mark_node;
5049 /* Perform appropriate conversions on the initial value of a variable,
5050 store it in the declaration DECL,
5051 and print any error messages that are appropriate.
5052 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5053 If the init is invalid, store an ERROR_MARK. */
5056 store_init_value (tree decl, tree init, tree origtype)
5061 /* If variable's type was invalidly declared, just ignore it. */
5063 type = TREE_TYPE (decl);
5064 if (TREE_CODE (type) == ERROR_MARK)
5067 /* Digest the specified initializer into an expression. */
5070 npc = null_pointer_constant_p (init);
5071 value = digest_init (type, init, origtype, npc, true, TREE_STATIC (decl));
5073 /* Store the expression if valid; else report error. */
5075 if (!in_system_header
5076 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5077 warning (OPT_Wtraditional, "traditional C rejects automatic "
5078 "aggregate initialization");
5080 DECL_INITIAL (decl) = value;
5082 /* ANSI wants warnings about out-of-range constant initializers. */
5083 STRIP_TYPE_NOPS (value);
5084 if (TREE_STATIC (decl))
5085 constant_expression_warning (value);
5087 /* Check if we need to set array size from compound literal size. */
5088 if (TREE_CODE (type) == ARRAY_TYPE
5089 && TYPE_DOMAIN (type) == 0
5090 && value != error_mark_node)
5092 tree inside_init = init;
5094 STRIP_TYPE_NOPS (inside_init);
5095 inside_init = fold (inside_init);
5097 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5099 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5101 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5103 /* For int foo[] = (int [3]){1}; we need to set array size
5104 now since later on array initializer will be just the
5105 brace enclosed list of the compound literal. */
5106 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5107 TREE_TYPE (decl) = type;
5108 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5110 layout_decl (cldecl, 0);
5116 /* Methods for storing and printing names for error messages. */
5118 /* Implement a spelling stack that allows components of a name to be pushed
5119 and popped. Each element on the stack is this structure. */
5126 unsigned HOST_WIDE_INT i;
5131 #define SPELLING_STRING 1
5132 #define SPELLING_MEMBER 2
5133 #define SPELLING_BOUNDS 3
5135 static struct spelling *spelling; /* Next stack element (unused). */
5136 static struct spelling *spelling_base; /* Spelling stack base. */
5137 static int spelling_size; /* Size of the spelling stack. */
5139 /* Macros to save and restore the spelling stack around push_... functions.
5140 Alternative to SAVE_SPELLING_STACK. */
5142 #define SPELLING_DEPTH() (spelling - spelling_base)
5143 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5145 /* Push an element on the spelling stack with type KIND and assign VALUE
5148 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5150 int depth = SPELLING_DEPTH (); \
5152 if (depth >= spelling_size) \
5154 spelling_size += 10; \
5155 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5157 RESTORE_SPELLING_DEPTH (depth); \
5160 spelling->kind = (KIND); \
5161 spelling->MEMBER = (VALUE); \
5165 /* Push STRING on the stack. Printed literally. */
5168 push_string (const char *string)
5170 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5173 /* Push a member name on the stack. Printed as '.' STRING. */
5176 push_member_name (tree decl)
5178 const char *const string
5180 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5181 : _("<anonymous>"));
5182 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5185 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5188 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5190 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5193 /* Compute the maximum size in bytes of the printed spelling. */
5196 spelling_length (void)
5201 for (p = spelling_base; p < spelling; p++)
5203 if (p->kind == SPELLING_BOUNDS)
5206 size += strlen (p->u.s) + 1;
5212 /* Print the spelling to BUFFER and return it. */
5215 print_spelling (char *buffer)
5220 for (p = spelling_base; p < spelling; p++)
5221 if (p->kind == SPELLING_BOUNDS)
5223 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5229 if (p->kind == SPELLING_MEMBER)
5231 for (s = p->u.s; (*d = *s++); d++)
5238 /* Issue an error message for a bad initializer component.
5239 MSGID identifies the message.
5240 The component name is taken from the spelling stack. */
5243 error_init (const char *msgid)
5247 error ("%s", _(msgid));
5248 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5250 error ("(near initialization for %qs)", ofwhat);
5253 /* Issue a pedantic warning for a bad initializer component. OPT is
5254 the option OPT_* (from options.h) controlling this warning or 0 if
5255 it is unconditionally given. MSGID identifies the message. The
5256 component name is taken from the spelling stack. */
5259 pedwarn_init (location_t location, int opt, const char *msgid)
5263 pedwarn (location, opt, "%s", _(msgid));
5264 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5266 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5269 /* Issue a warning for a bad initializer component.
5271 OPT is the OPT_W* value corresponding to the warning option that
5272 controls this warning. MSGID identifies the message. The
5273 component name is taken from the spelling stack. */
5276 warning_init (int opt, const char *msgid)
5280 warning (opt, "%s", _(msgid));
5281 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5283 warning (opt, "(near initialization for %qs)", ofwhat);
5286 /* If TYPE is an array type and EXPR is a parenthesized string
5287 constant, warn if pedantic that EXPR is being used to initialize an
5288 object of type TYPE. */
5291 maybe_warn_string_init (tree type, struct c_expr expr)
5294 && TREE_CODE (type) == ARRAY_TYPE
5295 && TREE_CODE (expr.value) == STRING_CST
5296 && expr.original_code != STRING_CST)
5297 pedwarn_init (input_location, OPT_pedantic,
5298 "array initialized from parenthesized string constant");
5301 /* Digest the parser output INIT as an initializer for type TYPE.
5302 Return a C expression of type TYPE to represent the initial value.
5304 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5306 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5308 If INIT is a string constant, STRICT_STRING is true if it is
5309 unparenthesized or we should not warn here for it being parenthesized.
5310 For other types of INIT, STRICT_STRING is not used.
5312 REQUIRE_CONSTANT requests an error if non-constant initializers or
5313 elements are seen. */
5316 digest_init (tree type, tree init, tree origtype, bool null_pointer_constant,
5317 bool strict_string, int require_constant)
5319 enum tree_code code = TREE_CODE (type);
5320 tree inside_init = init;
5321 tree semantic_type = NULL_TREE;
5322 bool maybe_const = true;
5324 if (type == error_mark_node
5326 || init == error_mark_node
5327 || TREE_TYPE (init) == error_mark_node)
5328 return error_mark_node;
5330 STRIP_TYPE_NOPS (inside_init);
5332 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5334 semantic_type = TREE_TYPE (inside_init);
5335 inside_init = TREE_OPERAND (inside_init, 0);
5337 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5338 inside_init = decl_constant_value_for_optimization (inside_init);
5340 /* Initialization of an array of chars from a string constant
5341 optionally enclosed in braces. */
5343 if (code == ARRAY_TYPE && inside_init
5344 && TREE_CODE (inside_init) == STRING_CST)
5346 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5347 /* Note that an array could be both an array of character type
5348 and an array of wchar_t if wchar_t is signed char or unsigned
5350 bool char_array = (typ1 == char_type_node
5351 || typ1 == signed_char_type_node
5352 || typ1 == unsigned_char_type_node);
5353 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5354 bool char16_array = !!comptypes (typ1, char16_type_node);
5355 bool char32_array = !!comptypes (typ1, char32_type_node);
5357 if (char_array || wchar_array || char16_array || char32_array)
5360 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5361 expr.value = inside_init;
5362 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5363 expr.original_type = NULL;
5364 maybe_warn_string_init (type, expr);
5366 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5367 pedwarn_init (input_location, OPT_pedantic,
5368 "initialization of a flexible array member");
5370 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5371 TYPE_MAIN_VARIANT (type)))
5376 if (typ2 != char_type_node)
5378 error_init ("char-array initialized from wide string");
5379 return error_mark_node;
5384 if (typ2 == char_type_node)
5386 error_init ("wide character array initialized from non-wide "
5388 return error_mark_node;
5390 else if (!comptypes(typ1, typ2))
5392 error_init ("wide character array initialized from "
5393 "incompatible wide string");
5394 return error_mark_node;
5398 TREE_TYPE (inside_init) = type;
5399 if (TYPE_DOMAIN (type) != 0
5400 && TYPE_SIZE (type) != 0
5401 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
5402 /* Subtract the size of a single (possibly wide) character
5403 because it's ok to ignore the terminating null char
5404 that is counted in the length of the constant. */
5405 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5406 TREE_STRING_LENGTH (inside_init)
5407 - (TYPE_PRECISION (typ1)
5409 pedwarn_init (input_location, 0,
5410 "initializer-string for array of chars is too long");
5414 else if (INTEGRAL_TYPE_P (typ1))
5416 error_init ("array of inappropriate type initialized "
5417 "from string constant");
5418 return error_mark_node;
5422 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5423 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5424 below and handle as a constructor. */
5425 if (code == VECTOR_TYPE
5426 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5427 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5428 && TREE_CONSTANT (inside_init))
5430 if (TREE_CODE (inside_init) == VECTOR_CST
5431 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5432 TYPE_MAIN_VARIANT (type)))
5435 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5437 unsigned HOST_WIDE_INT ix;
5439 bool constant_p = true;
5441 /* Iterate through elements and check if all constructor
5442 elements are *_CSTs. */
5443 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5444 if (!CONSTANT_CLASS_P (value))
5451 return build_vector_from_ctor (type,
5452 CONSTRUCTOR_ELTS (inside_init));
5456 if (warn_sequence_point)
5457 verify_sequence_points (inside_init);
5459 /* Any type can be initialized
5460 from an expression of the same type, optionally with braces. */
5462 if (inside_init && TREE_TYPE (inside_init) != 0
5463 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5464 TYPE_MAIN_VARIANT (type))
5465 || (code == ARRAY_TYPE
5466 && comptypes (TREE_TYPE (inside_init), type))
5467 || (code == VECTOR_TYPE
5468 && comptypes (TREE_TYPE (inside_init), type))
5469 || (code == POINTER_TYPE
5470 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5471 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5472 TREE_TYPE (type)))))
5474 if (code == POINTER_TYPE)
5476 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5478 if (TREE_CODE (inside_init) == STRING_CST
5479 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5480 inside_init = array_to_pointer_conversion (inside_init);
5483 error_init ("invalid use of non-lvalue array");
5484 return error_mark_node;
5489 if (code == VECTOR_TYPE)
5490 /* Although the types are compatible, we may require a
5492 inside_init = convert (type, inside_init);
5494 if (require_constant
5495 && (code == VECTOR_TYPE || !flag_isoc99)
5496 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5498 /* As an extension, allow initializing objects with static storage
5499 duration with compound literals (which are then treated just as
5500 the brace enclosed list they contain). Also allow this for
5501 vectors, as we can only assign them with compound literals. */
5502 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5503 inside_init = DECL_INITIAL (decl);
5506 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5507 && TREE_CODE (inside_init) != CONSTRUCTOR)
5509 error_init ("array initialized from non-constant array expression");
5510 return error_mark_node;
5513 /* Compound expressions can only occur here if -pedantic or
5514 -pedantic-errors is specified. In the later case, we always want
5515 an error. In the former case, we simply want a warning. */
5516 if (require_constant && pedantic
5517 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5520 = valid_compound_expr_initializer (inside_init,
5521 TREE_TYPE (inside_init));
5522 if (inside_init == error_mark_node)
5523 error_init ("initializer element is not constant");
5525 pedwarn_init (input_location, OPT_pedantic,
5526 "initializer element is not constant");
5527 if (flag_pedantic_errors)
5528 inside_init = error_mark_node;
5530 else if (require_constant
5531 && !initializer_constant_valid_p (inside_init,
5532 TREE_TYPE (inside_init)))
5534 error_init ("initializer element is not constant");
5535 inside_init = error_mark_node;
5537 else if (require_constant && !maybe_const)
5538 pedwarn_init (input_location, 0,
5539 "initializer element is not a constant expression");
5541 /* Added to enable additional -Wmissing-format-attribute warnings. */
5542 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
5543 inside_init = convert_for_assignment (type, inside_init, origtype,
5544 ic_init, null_pointer_constant,
5545 NULL_TREE, NULL_TREE, 0);
5549 /* Handle scalar types, including conversions. */
5551 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
5552 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
5553 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
5555 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
5556 && (TREE_CODE (init) == STRING_CST
5557 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
5558 inside_init = init = array_to_pointer_conversion (init);
5560 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
5563 = convert_for_assignment (type, inside_init, origtype, ic_init,
5564 null_pointer_constant,
5565 NULL_TREE, NULL_TREE, 0);
5567 /* Check to see if we have already given an error message. */
5568 if (inside_init == error_mark_node)
5570 else if (require_constant && !TREE_CONSTANT (inside_init))
5572 error_init ("initializer element is not constant");
5573 inside_init = error_mark_node;
5575 else if (require_constant
5576 && !initializer_constant_valid_p (inside_init,
5577 TREE_TYPE (inside_init)))
5579 error_init ("initializer element is not computable at load time");
5580 inside_init = error_mark_node;
5582 else if (require_constant && !maybe_const)
5583 pedwarn_init (input_location, 0,
5584 "initializer element is not a constant expression");
5589 /* Come here only for records and arrays. */
5591 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5593 error_init ("variable-sized object may not be initialized");
5594 return error_mark_node;
5597 error_init ("invalid initializer");
5598 return error_mark_node;
5601 /* Handle initializers that use braces. */
5603 /* Type of object we are accumulating a constructor for.
5604 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5605 static tree constructor_type;
5607 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5609 static tree constructor_fields;
5611 /* For an ARRAY_TYPE, this is the specified index
5612 at which to store the next element we get. */
5613 static tree constructor_index;
5615 /* For an ARRAY_TYPE, this is the maximum index. */
5616 static tree constructor_max_index;
5618 /* For a RECORD_TYPE, this is the first field not yet written out. */
5619 static tree constructor_unfilled_fields;
5621 /* For an ARRAY_TYPE, this is the index of the first element
5622 not yet written out. */
5623 static tree constructor_unfilled_index;
5625 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5626 This is so we can generate gaps between fields, when appropriate. */
5627 static tree constructor_bit_index;
5629 /* If we are saving up the elements rather than allocating them,
5630 this is the list of elements so far (in reverse order,
5631 most recent first). */
5632 static VEC(constructor_elt,gc) *constructor_elements;
5634 /* 1 if constructor should be incrementally stored into a constructor chain,
5635 0 if all the elements should be kept in AVL tree. */
5636 static int constructor_incremental;
5638 /* 1 if so far this constructor's elements are all compile-time constants. */
5639 static int constructor_constant;
5641 /* 1 if so far this constructor's elements are all valid address constants. */
5642 static int constructor_simple;
5644 /* 1 if this constructor has an element that cannot be part of a
5645 constant expression. */
5646 static int constructor_nonconst;
5648 /* 1 if this constructor is erroneous so far. */
5649 static int constructor_erroneous;
5651 /* Structure for managing pending initializer elements, organized as an
5656 struct init_node *left, *right;
5657 struct init_node *parent;
5664 /* Tree of pending elements at this constructor level.
5665 These are elements encountered out of order
5666 which belong at places we haven't reached yet in actually
5668 Will never hold tree nodes across GC runs. */
5669 static struct init_node *constructor_pending_elts;
5671 /* The SPELLING_DEPTH of this constructor. */
5672 static int constructor_depth;
5674 /* DECL node for which an initializer is being read.
5675 0 means we are reading a constructor expression
5676 such as (struct foo) {...}. */
5677 static tree constructor_decl;
5679 /* Nonzero if this is an initializer for a top-level decl. */
5680 static int constructor_top_level;
5682 /* Nonzero if there were any member designators in this initializer. */
5683 static int constructor_designated;
5685 /* Nesting depth of designator list. */
5686 static int designator_depth;
5688 /* Nonzero if there were diagnosed errors in this designator list. */
5689 static int designator_erroneous;
5692 /* This stack has a level for each implicit or explicit level of
5693 structuring in the initializer, including the outermost one. It
5694 saves the values of most of the variables above. */
5696 struct constructor_range_stack;
5698 struct constructor_stack
5700 struct constructor_stack *next;
5705 tree unfilled_index;
5706 tree unfilled_fields;
5708 VEC(constructor_elt,gc) *elements;
5709 struct init_node *pending_elts;
5712 /* If value nonzero, this value should replace the entire
5713 constructor at this level. */
5714 struct c_expr replacement_value;
5715 struct constructor_range_stack *range_stack;
5726 static struct constructor_stack *constructor_stack;
5728 /* This stack represents designators from some range designator up to
5729 the last designator in the list. */
5731 struct constructor_range_stack
5733 struct constructor_range_stack *next, *prev;
5734 struct constructor_stack *stack;
5741 static struct constructor_range_stack *constructor_range_stack;
5743 /* This stack records separate initializers that are nested.
5744 Nested initializers can't happen in ANSI C, but GNU C allows them
5745 in cases like { ... (struct foo) { ... } ... }. */
5747 struct initializer_stack
5749 struct initializer_stack *next;
5751 struct constructor_stack *constructor_stack;
5752 struct constructor_range_stack *constructor_range_stack;
5753 VEC(constructor_elt,gc) *elements;
5754 struct spelling *spelling;
5755 struct spelling *spelling_base;
5758 char require_constant_value;
5759 char require_constant_elements;
5762 static struct initializer_stack *initializer_stack;
5764 /* Prepare to parse and output the initializer for variable DECL. */
5767 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5770 struct initializer_stack *p = XNEW (struct initializer_stack);
5772 p->decl = constructor_decl;
5773 p->require_constant_value = require_constant_value;
5774 p->require_constant_elements = require_constant_elements;
5775 p->constructor_stack = constructor_stack;
5776 p->constructor_range_stack = constructor_range_stack;
5777 p->elements = constructor_elements;
5778 p->spelling = spelling;
5779 p->spelling_base = spelling_base;
5780 p->spelling_size = spelling_size;
5781 p->top_level = constructor_top_level;
5782 p->next = initializer_stack;
5783 initializer_stack = p;
5785 constructor_decl = decl;
5786 constructor_designated = 0;
5787 constructor_top_level = top_level;
5789 if (decl != 0 && decl != error_mark_node)
5791 require_constant_value = TREE_STATIC (decl);
5792 require_constant_elements
5793 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5794 /* For a scalar, you can always use any value to initialize,
5795 even within braces. */
5796 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5797 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5798 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5799 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5800 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
5804 require_constant_value = 0;
5805 require_constant_elements = 0;
5806 locus = _("(anonymous)");
5809 constructor_stack = 0;
5810 constructor_range_stack = 0;
5812 missing_braces_mentioned = 0;
5816 RESTORE_SPELLING_DEPTH (0);
5819 push_string (locus);
5825 struct initializer_stack *p = initializer_stack;
5827 /* Free the whole constructor stack of this initializer. */
5828 while (constructor_stack)
5830 struct constructor_stack *q = constructor_stack;
5831 constructor_stack = q->next;
5835 gcc_assert (!constructor_range_stack);
5837 /* Pop back to the data of the outer initializer (if any). */
5838 free (spelling_base);
5840 constructor_decl = p->decl;
5841 require_constant_value = p->require_constant_value;
5842 require_constant_elements = p->require_constant_elements;
5843 constructor_stack = p->constructor_stack;
5844 constructor_range_stack = p->constructor_range_stack;
5845 constructor_elements = p->elements;
5846 spelling = p->spelling;
5847 spelling_base = p->spelling_base;
5848 spelling_size = p->spelling_size;
5849 constructor_top_level = p->top_level;
5850 initializer_stack = p->next;
5854 /* Call here when we see the initializer is surrounded by braces.
5855 This is instead of a call to push_init_level;
5856 it is matched by a call to pop_init_level.
5858 TYPE is the type to initialize, for a constructor expression.
5859 For an initializer for a decl, TYPE is zero. */
5862 really_start_incremental_init (tree type)
5864 struct constructor_stack *p = XNEW (struct constructor_stack);
5867 type = TREE_TYPE (constructor_decl);
5869 if (TREE_CODE (type) == VECTOR_TYPE
5870 && TYPE_VECTOR_OPAQUE (type))
5871 error ("opaque vector types cannot be initialized");
5873 p->type = constructor_type;
5874 p->fields = constructor_fields;
5875 p->index = constructor_index;
5876 p->max_index = constructor_max_index;
5877 p->unfilled_index = constructor_unfilled_index;
5878 p->unfilled_fields = constructor_unfilled_fields;
5879 p->bit_index = constructor_bit_index;
5880 p->elements = constructor_elements;
5881 p->constant = constructor_constant;
5882 p->simple = constructor_simple;
5883 p->nonconst = constructor_nonconst;
5884 p->erroneous = constructor_erroneous;
5885 p->pending_elts = constructor_pending_elts;
5886 p->depth = constructor_depth;
5887 p->replacement_value.value = 0;
5888 p->replacement_value.original_code = ERROR_MARK;
5889 p->replacement_value.original_type = NULL;
5893 p->incremental = constructor_incremental;
5894 p->designated = constructor_designated;
5896 constructor_stack = p;
5898 constructor_constant = 1;
5899 constructor_simple = 1;
5900 constructor_nonconst = 0;
5901 constructor_depth = SPELLING_DEPTH ();
5902 constructor_elements = 0;
5903 constructor_pending_elts = 0;
5904 constructor_type = type;
5905 constructor_incremental = 1;
5906 constructor_designated = 0;
5907 designator_depth = 0;
5908 designator_erroneous = 0;
5910 if (TREE_CODE (constructor_type) == RECORD_TYPE
5911 || TREE_CODE (constructor_type) == UNION_TYPE)
5913 constructor_fields = TYPE_FIELDS (constructor_type);
5914 /* Skip any nameless bit fields at the beginning. */
5915 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5916 && DECL_NAME (constructor_fields) == 0)
5917 constructor_fields = TREE_CHAIN (constructor_fields);
5919 constructor_unfilled_fields = constructor_fields;
5920 constructor_bit_index = bitsize_zero_node;
5922 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5924 if (TYPE_DOMAIN (constructor_type))
5926 constructor_max_index
5927 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5929 /* Detect non-empty initializations of zero-length arrays. */
5930 if (constructor_max_index == NULL_TREE
5931 && TYPE_SIZE (constructor_type))
5932 constructor_max_index = build_int_cst (NULL_TREE, -1);
5934 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5935 to initialize VLAs will cause a proper error; avoid tree
5936 checking errors as well by setting a safe value. */
5937 if (constructor_max_index
5938 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5939 constructor_max_index = build_int_cst (NULL_TREE, -1);
5942 = convert (bitsizetype,
5943 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5947 constructor_index = bitsize_zero_node;
5948 constructor_max_index = NULL_TREE;
5951 constructor_unfilled_index = constructor_index;
5953 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5955 /* Vectors are like simple fixed-size arrays. */
5956 constructor_max_index =
5957 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5958 constructor_index = bitsize_zero_node;
5959 constructor_unfilled_index = constructor_index;
5963 /* Handle the case of int x = {5}; */
5964 constructor_fields = constructor_type;
5965 constructor_unfilled_fields = constructor_type;
5969 /* Push down into a subobject, for initialization.
5970 If this is for an explicit set of braces, IMPLICIT is 0.
5971 If it is because the next element belongs at a lower level,
5972 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5975 push_init_level (int implicit)
5977 struct constructor_stack *p;
5978 tree value = NULL_TREE;
5980 /* If we've exhausted any levels that didn't have braces,
5981 pop them now. If implicit == 1, this will have been done in
5982 process_init_element; do not repeat it here because in the case
5983 of excess initializers for an empty aggregate this leads to an
5984 infinite cycle of popping a level and immediately recreating
5988 while (constructor_stack->implicit)
5990 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5991 || TREE_CODE (constructor_type) == UNION_TYPE)
5992 && constructor_fields == 0)
5993 process_init_element (pop_init_level (1), true);
5994 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5995 && constructor_max_index
5996 && tree_int_cst_lt (constructor_max_index,
5998 process_init_element (pop_init_level (1), true);
6004 /* Unless this is an explicit brace, we need to preserve previous
6008 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6009 || TREE_CODE (constructor_type) == UNION_TYPE)
6010 && constructor_fields)
6011 value = find_init_member (constructor_fields);
6012 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6013 value = find_init_member (constructor_index);
6016 p = XNEW (struct constructor_stack);
6017 p->type = constructor_type;
6018 p->fields = constructor_fields;
6019 p->index = constructor_index;
6020 p->max_index = constructor_max_index;
6021 p->unfilled_index = constructor_unfilled_index;
6022 p->unfilled_fields = constructor_unfilled_fields;
6023 p->bit_index = constructor_bit_index;
6024 p->elements = constructor_elements;
6025 p->constant = constructor_constant;
6026 p->simple = constructor_simple;
6027 p->nonconst = constructor_nonconst;
6028 p->erroneous = constructor_erroneous;
6029 p->pending_elts = constructor_pending_elts;
6030 p->depth = constructor_depth;
6031 p->replacement_value.value = 0;
6032 p->replacement_value.original_code = ERROR_MARK;
6033 p->replacement_value.original_type = NULL;
6034 p->implicit = implicit;
6036 p->incremental = constructor_incremental;
6037 p->designated = constructor_designated;
6038 p->next = constructor_stack;
6040 constructor_stack = p;
6042 constructor_constant = 1;
6043 constructor_simple = 1;
6044 constructor_nonconst = 0;
6045 constructor_depth = SPELLING_DEPTH ();
6046 constructor_elements = 0;
6047 constructor_incremental = 1;
6048 constructor_designated = 0;
6049 constructor_pending_elts = 0;
6052 p->range_stack = constructor_range_stack;
6053 constructor_range_stack = 0;
6054 designator_depth = 0;
6055 designator_erroneous = 0;
6058 /* Don't die if an entire brace-pair level is superfluous
6059 in the containing level. */
6060 if (constructor_type == 0)
6062 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6063 || TREE_CODE (constructor_type) == UNION_TYPE)
6065 /* Don't die if there are extra init elts at the end. */
6066 if (constructor_fields == 0)
6067 constructor_type = 0;
6070 constructor_type = TREE_TYPE (constructor_fields);
6071 push_member_name (constructor_fields);
6072 constructor_depth++;
6075 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6077 constructor_type = TREE_TYPE (constructor_type);
6078 push_array_bounds (tree_low_cst (constructor_index, 1));
6079 constructor_depth++;
6082 if (constructor_type == 0)
6084 error_init ("extra brace group at end of initializer");
6085 constructor_fields = 0;
6086 constructor_unfilled_fields = 0;
6090 if (value && TREE_CODE (value) == CONSTRUCTOR)
6092 constructor_constant = TREE_CONSTANT (value);
6093 constructor_simple = TREE_STATIC (value);
6094 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6095 constructor_elements = CONSTRUCTOR_ELTS (value);
6096 if (!VEC_empty (constructor_elt, constructor_elements)
6097 && (TREE_CODE (constructor_type) == RECORD_TYPE
6098 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6099 set_nonincremental_init ();
6102 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6104 missing_braces_mentioned = 1;
6105 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6108 if (TREE_CODE (constructor_type) == RECORD_TYPE
6109 || TREE_CODE (constructor_type) == UNION_TYPE)
6111 constructor_fields = TYPE_FIELDS (constructor_type);
6112 /* Skip any nameless bit fields at the beginning. */
6113 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6114 && DECL_NAME (constructor_fields) == 0)
6115 constructor_fields = TREE_CHAIN (constructor_fields);
6117 constructor_unfilled_fields = constructor_fields;
6118 constructor_bit_index = bitsize_zero_node;
6120 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6122 /* Vectors are like simple fixed-size arrays. */
6123 constructor_max_index =
6124 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6125 constructor_index = convert (bitsizetype, integer_zero_node);
6126 constructor_unfilled_index = constructor_index;
6128 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6130 if (TYPE_DOMAIN (constructor_type))
6132 constructor_max_index
6133 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6135 /* Detect non-empty initializations of zero-length arrays. */
6136 if (constructor_max_index == NULL_TREE
6137 && TYPE_SIZE (constructor_type))
6138 constructor_max_index = build_int_cst (NULL_TREE, -1);
6140 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6141 to initialize VLAs will cause a proper error; avoid tree
6142 checking errors as well by setting a safe value. */
6143 if (constructor_max_index
6144 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6145 constructor_max_index = build_int_cst (NULL_TREE, -1);
6148 = convert (bitsizetype,
6149 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6152 constructor_index = bitsize_zero_node;
6154 constructor_unfilled_index = constructor_index;
6155 if (value && TREE_CODE (value) == STRING_CST)
6157 /* We need to split the char/wchar array into individual
6158 characters, so that we don't have to special case it
6160 set_nonincremental_init_from_string (value);
6165 if (constructor_type != error_mark_node)
6166 warning_init (0, "braces around scalar initializer");
6167 constructor_fields = constructor_type;
6168 constructor_unfilled_fields = constructor_type;
6172 /* At the end of an implicit or explicit brace level,
6173 finish up that level of constructor. If a single expression
6174 with redundant braces initialized that level, return the
6175 c_expr structure for that expression. Otherwise, the original_code
6176 element is set to ERROR_MARK.
6177 If we were outputting the elements as they are read, return 0 as the value
6178 from inner levels (process_init_element ignores that),
6179 but return error_mark_node as the value from the outermost level
6180 (that's what we want to put in DECL_INITIAL).
6181 Otherwise, return a CONSTRUCTOR expression as the value. */
6184 pop_init_level (int implicit)
6186 struct constructor_stack *p;
6189 ret.original_code = ERROR_MARK;
6190 ret.original_type = NULL;
6194 /* When we come to an explicit close brace,
6195 pop any inner levels that didn't have explicit braces. */
6196 while (constructor_stack->implicit)
6197 process_init_element (pop_init_level (1), true);
6199 gcc_assert (!constructor_range_stack);
6202 /* Now output all pending elements. */
6203 constructor_incremental = 1;
6204 output_pending_init_elements (1);
6206 p = constructor_stack;
6208 /* Error for initializing a flexible array member, or a zero-length
6209 array member in an inappropriate context. */
6210 if (constructor_type && constructor_fields
6211 && TREE_CODE (constructor_type) == ARRAY_TYPE
6212 && TYPE_DOMAIN (constructor_type)
6213 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6215 /* Silently discard empty initializations. The parser will
6216 already have pedwarned for empty brackets. */
6217 if (integer_zerop (constructor_unfilled_index))
6218 constructor_type = NULL_TREE;
6221 gcc_assert (!TYPE_SIZE (constructor_type));
6223 if (constructor_depth > 2)
6224 error_init ("initialization of flexible array member in a nested context");
6226 pedwarn_init (input_location, OPT_pedantic,
6227 "initialization of a flexible array member");
6229 /* We have already issued an error message for the existence
6230 of a flexible array member not at the end of the structure.
6231 Discard the initializer so that we do not die later. */
6232 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
6233 constructor_type = NULL_TREE;
6237 /* Warn when some struct elements are implicitly initialized to zero. */
6238 if (warn_missing_field_initializers
6240 && TREE_CODE (constructor_type) == RECORD_TYPE
6241 && constructor_unfilled_fields)
6243 /* Do not warn for flexible array members or zero-length arrays. */
6244 while (constructor_unfilled_fields
6245 && (!DECL_SIZE (constructor_unfilled_fields)
6246 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6247 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6249 /* Do not warn if this level of the initializer uses member
6250 designators; it is likely to be deliberate. */
6251 if (constructor_unfilled_fields && !constructor_designated)
6253 push_member_name (constructor_unfilled_fields);
6254 warning_init (OPT_Wmissing_field_initializers,
6255 "missing initializer");
6256 RESTORE_SPELLING_DEPTH (constructor_depth);
6260 /* Pad out the end of the structure. */
6261 if (p->replacement_value.value)
6262 /* If this closes a superfluous brace pair,
6263 just pass out the element between them. */
6264 ret = p->replacement_value;
6265 else if (constructor_type == 0)
6267 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6268 && TREE_CODE (constructor_type) != UNION_TYPE
6269 && TREE_CODE (constructor_type) != ARRAY_TYPE
6270 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6272 /* A nonincremental scalar initializer--just return
6273 the element, after verifying there is just one. */
6274 if (VEC_empty (constructor_elt,constructor_elements))
6276 if (!constructor_erroneous)
6277 error_init ("empty scalar initializer");
6278 ret.value = error_mark_node;
6280 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6282 error_init ("extra elements in scalar initializer");
6283 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6286 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6290 if (constructor_erroneous)
6291 ret.value = error_mark_node;
6294 ret.value = build_constructor (constructor_type,
6295 constructor_elements);
6296 if (constructor_constant)
6297 TREE_CONSTANT (ret.value) = 1;
6298 if (constructor_constant && constructor_simple)
6299 TREE_STATIC (ret.value) = 1;
6300 if (constructor_nonconst)
6301 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6305 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6307 if (constructor_nonconst)
6308 ret.original_code = C_MAYBE_CONST_EXPR;
6309 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6310 ret.original_code = ERROR_MARK;
6313 constructor_type = p->type;
6314 constructor_fields = p->fields;
6315 constructor_index = p->index;
6316 constructor_max_index = p->max_index;
6317 constructor_unfilled_index = p->unfilled_index;
6318 constructor_unfilled_fields = p->unfilled_fields;
6319 constructor_bit_index = p->bit_index;
6320 constructor_elements = p->elements;
6321 constructor_constant = p->constant;
6322 constructor_simple = p->simple;
6323 constructor_nonconst = p->nonconst;
6324 constructor_erroneous = p->erroneous;
6325 constructor_incremental = p->incremental;
6326 constructor_designated = p->designated;
6327 constructor_pending_elts = p->pending_elts;
6328 constructor_depth = p->depth;
6330 constructor_range_stack = p->range_stack;
6331 RESTORE_SPELLING_DEPTH (constructor_depth);
6333 constructor_stack = p->next;
6336 if (ret.value == 0 && constructor_stack == 0)
6337 ret.value = error_mark_node;
6341 /* Common handling for both array range and field name designators.
6342 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6345 set_designator (int array)
6348 enum tree_code subcode;
6350 /* Don't die if an entire brace-pair level is superfluous
6351 in the containing level. */
6352 if (constructor_type == 0)
6355 /* If there were errors in this designator list already, bail out
6357 if (designator_erroneous)
6360 if (!designator_depth)
6362 gcc_assert (!constructor_range_stack);
6364 /* Designator list starts at the level of closest explicit
6366 while (constructor_stack->implicit)
6367 process_init_element (pop_init_level (1), true);
6368 constructor_designated = 1;
6372 switch (TREE_CODE (constructor_type))
6376 subtype = TREE_TYPE (constructor_fields);
6377 if (subtype != error_mark_node)
6378 subtype = TYPE_MAIN_VARIANT (subtype);
6381 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6387 subcode = TREE_CODE (subtype);
6388 if (array && subcode != ARRAY_TYPE)
6390 error_init ("array index in non-array initializer");
6393 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6395 error_init ("field name not in record or union initializer");
6399 constructor_designated = 1;
6400 push_init_level (2);
6404 /* If there are range designators in designator list, push a new designator
6405 to constructor_range_stack. RANGE_END is end of such stack range or
6406 NULL_TREE if there is no range designator at this level. */
6409 push_range_stack (tree range_end)
6411 struct constructor_range_stack *p;
6413 p = GGC_NEW (struct constructor_range_stack);
6414 p->prev = constructor_range_stack;
6416 p->fields = constructor_fields;
6417 p->range_start = constructor_index;
6418 p->index = constructor_index;
6419 p->stack = constructor_stack;
6420 p->range_end = range_end;
6421 if (constructor_range_stack)
6422 constructor_range_stack->next = p;
6423 constructor_range_stack = p;
6426 /* Within an array initializer, specify the next index to be initialized.
6427 FIRST is that index. If LAST is nonzero, then initialize a range
6428 of indices, running from FIRST through LAST. */
6431 set_init_index (tree first, tree last)
6433 if (set_designator (1))
6436 designator_erroneous = 1;
6438 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
6439 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
6441 error_init ("array index in initializer not of integer type");
6445 if (TREE_CODE (first) != INTEGER_CST)
6447 first = c_fully_fold (first, false, NULL);
6448 if (TREE_CODE (first) == INTEGER_CST)
6449 pedwarn_init (input_location, OPT_pedantic,
6450 "array index in initializer is not "
6451 "an integer constant expression");
6454 if (last && TREE_CODE (last) != INTEGER_CST)
6456 last = c_fully_fold (last, false, NULL);
6457 if (TREE_CODE (last) == INTEGER_CST)
6458 pedwarn_init (input_location, OPT_pedantic,
6459 "array index in initializer is not "
6460 "an integer constant expression");
6463 if (TREE_CODE (first) != INTEGER_CST)
6464 error_init ("nonconstant array index in initializer");
6465 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
6466 error_init ("nonconstant array index in initializer");
6467 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6468 error_init ("array index in non-array initializer");
6469 else if (tree_int_cst_sgn (first) == -1)
6470 error_init ("array index in initializer exceeds array bounds");
6471 else if (constructor_max_index
6472 && tree_int_cst_lt (constructor_max_index, first))
6473 error_init ("array index in initializer exceeds array bounds");
6476 constant_expression_warning (first);
6478 constant_expression_warning (last);
6479 constructor_index = convert (bitsizetype, first);
6483 if (tree_int_cst_equal (first, last))
6485 else if (tree_int_cst_lt (last, first))
6487 error_init ("empty index range in initializer");
6492 last = convert (bitsizetype, last);
6493 if (constructor_max_index != 0
6494 && tree_int_cst_lt (constructor_max_index, last))
6496 error_init ("array index range in initializer exceeds array bounds");
6503 designator_erroneous = 0;
6504 if (constructor_range_stack || last)
6505 push_range_stack (last);
6509 /* Within a struct initializer, specify the next field to be initialized. */
6512 set_init_label (tree fieldname)
6516 if (set_designator (0))
6519 designator_erroneous = 1;
6521 if (TREE_CODE (constructor_type) != RECORD_TYPE
6522 && TREE_CODE (constructor_type) != UNION_TYPE)
6524 error_init ("field name not in record or union initializer");
6528 for (tail = TYPE_FIELDS (constructor_type); tail;
6529 tail = TREE_CHAIN (tail))
6531 if (DECL_NAME (tail) == fieldname)
6536 error ("unknown field %qE specified in initializer", fieldname);
6539 constructor_fields = tail;
6541 designator_erroneous = 0;
6542 if (constructor_range_stack)
6543 push_range_stack (NULL_TREE);
6547 /* Add a new initializer to the tree of pending initializers. PURPOSE
6548 identifies the initializer, either array index or field in a structure.
6549 VALUE is the value of that index or field. If ORIGTYPE is not
6550 NULL_TREE, it is the original type of VALUE.
6552 IMPLICIT is true if value comes from pop_init_level (1),
6553 the new initializer has been merged with the existing one
6554 and thus no warnings should be emitted about overriding an
6555 existing initializer. */
6558 add_pending_init (tree purpose, tree value, tree origtype, bool implicit)
6560 struct init_node *p, **q, *r;
6562 q = &constructor_pending_elts;
6565 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6570 if (tree_int_cst_lt (purpose, p->purpose))
6572 else if (tree_int_cst_lt (p->purpose, purpose))
6578 if (TREE_SIDE_EFFECTS (p->value))
6579 warning_init (0, "initialized field with side-effects overwritten");
6580 else if (warn_override_init)
6581 warning_init (OPT_Woverride_init, "initialized field overwritten");
6584 p->origtype = origtype;
6593 bitpos = bit_position (purpose);
6597 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6599 else if (p->purpose != purpose)
6605 if (TREE_SIDE_EFFECTS (p->value))
6606 warning_init (0, "initialized field with side-effects overwritten");
6607 else if (warn_override_init)
6608 warning_init (OPT_Woverride_init, "initialized field overwritten");
6611 p->origtype = origtype;
6617 r = GGC_NEW (struct init_node);
6618 r->purpose = purpose;
6620 r->origtype = origtype;
6630 struct init_node *s;
6634 if (p->balance == 0)
6636 else if (p->balance < 0)
6643 p->left->parent = p;
6660 constructor_pending_elts = r;
6665 struct init_node *t = r->right;
6669 r->right->parent = r;
6674 p->left->parent = p;
6677 p->balance = t->balance < 0;
6678 r->balance = -(t->balance > 0);
6693 constructor_pending_elts = t;
6699 /* p->balance == +1; growth of left side balances the node. */
6704 else /* r == p->right */
6706 if (p->balance == 0)
6707 /* Growth propagation from right side. */
6709 else if (p->balance > 0)
6716 p->right->parent = p;
6733 constructor_pending_elts = r;
6735 else /* r->balance == -1 */
6738 struct init_node *t = r->left;
6742 r->left->parent = r;
6747 p->right->parent = p;
6750 r->balance = (t->balance < 0);
6751 p->balance = -(t->balance > 0);
6766 constructor_pending_elts = t;
6772 /* p->balance == -1; growth of right side balances the node. */
6783 /* Build AVL tree from a sorted chain. */
6786 set_nonincremental_init (void)
6788 unsigned HOST_WIDE_INT ix;
6791 if (TREE_CODE (constructor_type) != RECORD_TYPE
6792 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6795 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6796 add_pending_init (index, value, NULL_TREE, false);
6797 constructor_elements = 0;
6798 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6800 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6801 /* Skip any nameless bit fields at the beginning. */
6802 while (constructor_unfilled_fields != 0
6803 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6804 && DECL_NAME (constructor_unfilled_fields) == 0)
6805 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6808 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6810 if (TYPE_DOMAIN (constructor_type))
6811 constructor_unfilled_index
6812 = convert (bitsizetype,
6813 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6815 constructor_unfilled_index = bitsize_zero_node;
6817 constructor_incremental = 0;
6820 /* Build AVL tree from a string constant. */
6823 set_nonincremental_init_from_string (tree str)
6825 tree value, purpose, type;
6826 HOST_WIDE_INT val[2];
6827 const char *p, *end;
6828 int byte, wchar_bytes, charwidth, bitpos;
6830 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
6832 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
6833 charwidth = TYPE_PRECISION (char_type_node);
6834 type = TREE_TYPE (constructor_type);
6835 p = TREE_STRING_POINTER (str);
6836 end = p + TREE_STRING_LENGTH (str);
6838 for (purpose = bitsize_zero_node;
6839 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6840 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6842 if (wchar_bytes == 1)
6844 val[1] = (unsigned char) *p++;
6851 for (byte = 0; byte < wchar_bytes; byte++)
6853 if (BYTES_BIG_ENDIAN)
6854 bitpos = (wchar_bytes - byte - 1) * charwidth;
6856 bitpos = byte * charwidth;
6857 val[bitpos < HOST_BITS_PER_WIDE_INT]
6858 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6859 << (bitpos % HOST_BITS_PER_WIDE_INT);
6863 if (!TYPE_UNSIGNED (type))
6865 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6866 if (bitpos < HOST_BITS_PER_WIDE_INT)
6868 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6870 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6874 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6879 else if (val[0] & (((HOST_WIDE_INT) 1)
6880 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6881 val[0] |= ((HOST_WIDE_INT) -1)
6882 << (bitpos - HOST_BITS_PER_WIDE_INT);
6885 value = build_int_cst_wide (type, val[1], val[0]);
6886 add_pending_init (purpose, value, NULL_TREE, false);
6889 constructor_incremental = 0;
6892 /* Return value of FIELD in pending initializer or zero if the field was
6893 not initialized yet. */
6896 find_init_member (tree field)
6898 struct init_node *p;
6900 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6902 if (constructor_incremental
6903 && tree_int_cst_lt (field, constructor_unfilled_index))
6904 set_nonincremental_init ();
6906 p = constructor_pending_elts;
6909 if (tree_int_cst_lt (field, p->purpose))
6911 else if (tree_int_cst_lt (p->purpose, field))
6917 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6919 tree bitpos = bit_position (field);
6921 if (constructor_incremental
6922 && (!constructor_unfilled_fields
6923 || tree_int_cst_lt (bitpos,
6924 bit_position (constructor_unfilled_fields))))
6925 set_nonincremental_init ();
6927 p = constructor_pending_elts;
6930 if (field == p->purpose)
6932 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6938 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6940 if (!VEC_empty (constructor_elt, constructor_elements)
6941 && (VEC_last (constructor_elt, constructor_elements)->index
6943 return VEC_last (constructor_elt, constructor_elements)->value;
6948 /* "Output" the next constructor element.
6949 At top level, really output it to assembler code now.
6950 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6951 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
6952 TYPE is the data type that the containing data type wants here.
6953 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6954 If VALUE is a string constant, STRICT_STRING is true if it is
6955 unparenthesized or we should not warn here for it being parenthesized.
6956 For other types of VALUE, STRICT_STRING is not used.
6958 PENDING if non-nil means output pending elements that belong
6959 right after this element. (PENDING is normally 1;
6960 it is 0 while outputting pending elements, to avoid recursion.)
6962 IMPLICIT is true if value comes from pop_init_level (1),
6963 the new initializer has been merged with the existing one
6964 and thus no warnings should be emitted about overriding an
6965 existing initializer. */
6968 output_init_element (tree value, tree origtype, bool strict_string, tree type,
6969 tree field, int pending, bool implicit)
6971 tree semantic_type = NULL_TREE;
6972 constructor_elt *celt;
6973 bool maybe_const = true;
6976 if (type == error_mark_node || value == error_mark_node)
6978 constructor_erroneous = 1;
6981 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6982 && (TREE_CODE (value) == STRING_CST
6983 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6984 && !(TREE_CODE (value) == STRING_CST
6985 && TREE_CODE (type) == ARRAY_TYPE
6986 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6987 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6988 TYPE_MAIN_VARIANT (type)))
6989 value = array_to_pointer_conversion (value);
6991 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6992 && require_constant_value && !flag_isoc99 && pending)
6994 /* As an extension, allow initializing objects with static storage
6995 duration with compound literals (which are then treated just as
6996 the brace enclosed list they contain). */
6997 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6998 value = DECL_INITIAL (decl);
7001 npc = null_pointer_constant_p (value);
7002 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7004 semantic_type = TREE_TYPE (value);
7005 value = TREE_OPERAND (value, 0);
7007 value = c_fully_fold (value, require_constant_value, &maybe_const);
7009 if (value == error_mark_node)
7010 constructor_erroneous = 1;
7011 else if (!TREE_CONSTANT (value))
7012 constructor_constant = 0;
7013 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7014 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7015 || TREE_CODE (constructor_type) == UNION_TYPE)
7016 && DECL_C_BIT_FIELD (field)
7017 && TREE_CODE (value) != INTEGER_CST))
7018 constructor_simple = 0;
7020 constructor_nonconst = 1;
7022 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7024 if (require_constant_value)
7026 error_init ("initializer element is not constant");
7027 value = error_mark_node;
7029 else if (require_constant_elements)
7030 pedwarn (input_location, 0,
7031 "initializer element is not computable at load time");
7033 else if (!maybe_const
7034 && (require_constant_value || require_constant_elements))
7035 pedwarn_init (input_location, 0,
7036 "initializer element is not a constant expression");
7038 /* Issue -Wc++-compat warnings about initializing a bitfield with
7041 && field != NULL_TREE
7042 && TREE_CODE (field) == FIELD_DECL
7043 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7044 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7045 != TYPE_MAIN_VARIANT (type))
7046 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7048 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7049 if (checktype != error_mark_node
7050 && (TYPE_MAIN_VARIANT (checktype)
7051 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7052 warning_init (OPT_Wc___compat,
7053 "enum conversion in initialization is invalid in C++");
7056 /* If this field is empty (and not at the end of structure),
7057 don't do anything other than checking the initializer. */
7059 && (TREE_TYPE (field) == error_mark_node
7060 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7061 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7062 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7063 || TREE_CHAIN (field)))))
7067 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7068 value = digest_init (type, value, origtype, npc, strict_string,
7069 require_constant_value);
7070 if (value == error_mark_node)
7072 constructor_erroneous = 1;
7075 if (require_constant_value || require_constant_elements)
7076 constant_expression_warning (value);
7078 /* If this element doesn't come next in sequence,
7079 put it on constructor_pending_elts. */
7080 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7081 && (!constructor_incremental
7082 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7084 if (constructor_incremental
7085 && tree_int_cst_lt (field, constructor_unfilled_index))
7086 set_nonincremental_init ();
7088 add_pending_init (field, value, origtype, implicit);
7091 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7092 && (!constructor_incremental
7093 || field != constructor_unfilled_fields))
7095 /* We do this for records but not for unions. In a union,
7096 no matter which field is specified, it can be initialized
7097 right away since it starts at the beginning of the union. */
7098 if (constructor_incremental)
7100 if (!constructor_unfilled_fields)
7101 set_nonincremental_init ();
7104 tree bitpos, unfillpos;
7106 bitpos = bit_position (field);
7107 unfillpos = bit_position (constructor_unfilled_fields);
7109 if (tree_int_cst_lt (bitpos, unfillpos))
7110 set_nonincremental_init ();
7114 add_pending_init (field, value, origtype, implicit);
7117 else if (TREE_CODE (constructor_type) == UNION_TYPE
7118 && !VEC_empty (constructor_elt, constructor_elements))
7122 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7123 constructor_elements)->value))
7125 "initialized field with side-effects overwritten");
7126 else if (warn_override_init)
7127 warning_init (OPT_Woverride_init, "initialized field overwritten");
7130 /* We can have just one union field set. */
7131 constructor_elements = 0;
7134 /* Otherwise, output this element either to
7135 constructor_elements or to the assembler file. */
7137 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7138 celt->index = field;
7139 celt->value = value;
7141 /* Advance the variable that indicates sequential elements output. */
7142 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7143 constructor_unfilled_index
7144 = size_binop (PLUS_EXPR, constructor_unfilled_index,
7146 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7148 constructor_unfilled_fields
7149 = TREE_CHAIN (constructor_unfilled_fields);
7151 /* Skip any nameless bit fields. */
7152 while (constructor_unfilled_fields != 0
7153 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7154 && DECL_NAME (constructor_unfilled_fields) == 0)
7155 constructor_unfilled_fields =
7156 TREE_CHAIN (constructor_unfilled_fields);
7158 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7159 constructor_unfilled_fields = 0;
7161 /* Now output any pending elements which have become next. */
7163 output_pending_init_elements (0);
7166 /* Output any pending elements which have become next.
7167 As we output elements, constructor_unfilled_{fields,index}
7168 advances, which may cause other elements to become next;
7169 if so, they too are output.
7171 If ALL is 0, we return when there are
7172 no more pending elements to output now.
7174 If ALL is 1, we output space as necessary so that
7175 we can output all the pending elements. */
7178 output_pending_init_elements (int all)
7180 struct init_node *elt = constructor_pending_elts;
7185 /* Look through the whole pending tree.
7186 If we find an element that should be output now,
7187 output it. Otherwise, set NEXT to the element
7188 that comes first among those still pending. */
7193 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7195 if (tree_int_cst_equal (elt->purpose,
7196 constructor_unfilled_index))
7197 output_init_element (elt->value, elt->origtype, true,
7198 TREE_TYPE (constructor_type),
7199 constructor_unfilled_index, 0, false);
7200 else if (tree_int_cst_lt (constructor_unfilled_index,
7203 /* Advance to the next smaller node. */
7208 /* We have reached the smallest node bigger than the
7209 current unfilled index. Fill the space first. */
7210 next = elt->purpose;
7216 /* Advance to the next bigger node. */
7221 /* We have reached the biggest node in a subtree. Find
7222 the parent of it, which is the next bigger node. */
7223 while (elt->parent && elt->parent->right == elt)
7226 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7229 next = elt->purpose;
7235 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7236 || TREE_CODE (constructor_type) == UNION_TYPE)
7238 tree ctor_unfilled_bitpos, elt_bitpos;
7240 /* If the current record is complete we are done. */
7241 if (constructor_unfilled_fields == 0)
7244 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7245 elt_bitpos = bit_position (elt->purpose);
7246 /* We can't compare fields here because there might be empty
7247 fields in between. */
7248 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7250 constructor_unfilled_fields = elt->purpose;
7251 output_init_element (elt->value, elt->origtype, true,
7252 TREE_TYPE (elt->purpose),
7253 elt->purpose, 0, false);
7255 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7257 /* Advance to the next smaller node. */
7262 /* We have reached the smallest node bigger than the
7263 current unfilled field. Fill the space first. */
7264 next = elt->purpose;
7270 /* Advance to the next bigger node. */
7275 /* We have reached the biggest node in a subtree. Find
7276 the parent of it, which is the next bigger node. */
7277 while (elt->parent && elt->parent->right == elt)
7281 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7282 bit_position (elt->purpose))))
7284 next = elt->purpose;
7292 /* Ordinarily return, but not if we want to output all
7293 and there are elements left. */
7294 if (!(all && next != 0))
7297 /* If it's not incremental, just skip over the gap, so that after
7298 jumping to retry we will output the next successive element. */
7299 if (TREE_CODE (constructor_type) == RECORD_TYPE
7300 || TREE_CODE (constructor_type) == UNION_TYPE)
7301 constructor_unfilled_fields = next;
7302 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7303 constructor_unfilled_index = next;
7305 /* ELT now points to the node in the pending tree with the next
7306 initializer to output. */
7310 /* Add one non-braced element to the current constructor level.
7311 This adjusts the current position within the constructor's type.
7312 This may also start or terminate implicit levels
7313 to handle a partly-braced initializer.
7315 Once this has found the correct level for the new element,
7316 it calls output_init_element.
7318 IMPLICIT is true if value comes from pop_init_level (1),
7319 the new initializer has been merged with the existing one
7320 and thus no warnings should be emitted about overriding an
7321 existing initializer. */
7324 process_init_element (struct c_expr value, bool implicit)
7326 tree orig_value = value.value;
7327 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7328 bool strict_string = value.original_code == STRING_CST;
7330 designator_depth = 0;
7331 designator_erroneous = 0;
7333 /* Handle superfluous braces around string cst as in
7334 char x[] = {"foo"}; */
7337 && TREE_CODE (constructor_type) == ARRAY_TYPE
7338 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7339 && integer_zerop (constructor_unfilled_index))
7341 if (constructor_stack->replacement_value.value)
7342 error_init ("excess elements in char array initializer");
7343 constructor_stack->replacement_value = value;
7347 if (constructor_stack->replacement_value.value != 0)
7349 error_init ("excess elements in struct initializer");
7353 /* Ignore elements of a brace group if it is entirely superfluous
7354 and has already been diagnosed. */
7355 if (constructor_type == 0)
7358 /* If we've exhausted any levels that didn't have braces,
7360 while (constructor_stack->implicit)
7362 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7363 || TREE_CODE (constructor_type) == UNION_TYPE)
7364 && constructor_fields == 0)
7365 process_init_element (pop_init_level (1), true);
7366 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
7367 || TREE_CODE (constructor_type) == VECTOR_TYPE)
7368 && (constructor_max_index == 0
7369 || tree_int_cst_lt (constructor_max_index,
7370 constructor_index)))
7371 process_init_element (pop_init_level (1), true);
7376 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7377 if (constructor_range_stack)
7379 /* If value is a compound literal and we'll be just using its
7380 content, don't put it into a SAVE_EXPR. */
7381 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7382 || !require_constant_value
7385 tree semantic_type = NULL_TREE;
7386 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7388 semantic_type = TREE_TYPE (value.value);
7389 value.value = TREE_OPERAND (value.value, 0);
7391 value.value = c_save_expr (value.value);
7393 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7400 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7403 enum tree_code fieldcode;
7405 if (constructor_fields == 0)
7407 pedwarn_init (input_location, 0,
7408 "excess elements in struct initializer");
7412 fieldtype = TREE_TYPE (constructor_fields);
7413 if (fieldtype != error_mark_node)
7414 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7415 fieldcode = TREE_CODE (fieldtype);
7417 /* Error for non-static initialization of a flexible array member. */
7418 if (fieldcode == ARRAY_TYPE
7419 && !require_constant_value
7420 && TYPE_SIZE (fieldtype) == NULL_TREE
7421 && TREE_CHAIN (constructor_fields) == NULL_TREE)
7423 error_init ("non-static initialization of a flexible array member");
7427 /* Accept a string constant to initialize a subarray. */
7428 if (value.value != 0
7429 && fieldcode == ARRAY_TYPE
7430 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7432 value.value = orig_value;
7433 /* Otherwise, if we have come to a subaggregate,
7434 and we don't have an element of its type, push into it. */
7435 else if (value.value != 0
7436 && value.value != error_mark_node
7437 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7438 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7439 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7441 push_init_level (1);
7447 push_member_name (constructor_fields);
7448 output_init_element (value.value, value.original_type,
7449 strict_string, fieldtype,
7450 constructor_fields, 1, implicit);
7451 RESTORE_SPELLING_DEPTH (constructor_depth);
7454 /* Do the bookkeeping for an element that was
7455 directly output as a constructor. */
7457 /* For a record, keep track of end position of last field. */
7458 if (DECL_SIZE (constructor_fields))
7459 constructor_bit_index
7460 = size_binop (PLUS_EXPR,
7461 bit_position (constructor_fields),
7462 DECL_SIZE (constructor_fields));
7464 /* If the current field was the first one not yet written out,
7465 it isn't now, so update. */
7466 if (constructor_unfilled_fields == constructor_fields)
7468 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7469 /* Skip any nameless bit fields. */
7470 while (constructor_unfilled_fields != 0
7471 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7472 && DECL_NAME (constructor_unfilled_fields) == 0)
7473 constructor_unfilled_fields =
7474 TREE_CHAIN (constructor_unfilled_fields);
7478 constructor_fields = TREE_CHAIN (constructor_fields);
7479 /* Skip any nameless bit fields at the beginning. */
7480 while (constructor_fields != 0
7481 && DECL_C_BIT_FIELD (constructor_fields)
7482 && DECL_NAME (constructor_fields) == 0)
7483 constructor_fields = TREE_CHAIN (constructor_fields);
7485 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7488 enum tree_code fieldcode;
7490 if (constructor_fields == 0)
7492 pedwarn_init (input_location, 0,
7493 "excess elements in union initializer");
7497 fieldtype = TREE_TYPE (constructor_fields);
7498 if (fieldtype != error_mark_node)
7499 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7500 fieldcode = TREE_CODE (fieldtype);
7502 /* Warn that traditional C rejects initialization of unions.
7503 We skip the warning if the value is zero. This is done
7504 under the assumption that the zero initializer in user
7505 code appears conditioned on e.g. __STDC__ to avoid
7506 "missing initializer" warnings and relies on default
7507 initialization to zero in the traditional C case.
7508 We also skip the warning if the initializer is designated,
7509 again on the assumption that this must be conditional on
7510 __STDC__ anyway (and we've already complained about the
7511 member-designator already). */
7512 if (!in_system_header && !constructor_designated
7513 && !(value.value && (integer_zerop (value.value)
7514 || real_zerop (value.value))))
7515 warning (OPT_Wtraditional, "traditional C rejects initialization "
7518 /* Accept a string constant to initialize a subarray. */
7519 if (value.value != 0
7520 && fieldcode == ARRAY_TYPE
7521 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7523 value.value = orig_value;
7524 /* Otherwise, if we have come to a subaggregate,
7525 and we don't have an element of its type, push into it. */
7526 else if (value.value != 0
7527 && value.value != error_mark_node
7528 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7529 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7530 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7532 push_init_level (1);
7538 push_member_name (constructor_fields);
7539 output_init_element (value.value, value.original_type,
7540 strict_string, fieldtype,
7541 constructor_fields, 1, implicit);
7542 RESTORE_SPELLING_DEPTH (constructor_depth);
7545 /* Do the bookkeeping for an element that was
7546 directly output as a constructor. */
7548 constructor_bit_index = DECL_SIZE (constructor_fields);
7549 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7552 constructor_fields = 0;
7554 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7556 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7557 enum tree_code eltcode = TREE_CODE (elttype);
7559 /* Accept a string constant to initialize a subarray. */
7560 if (value.value != 0
7561 && eltcode == ARRAY_TYPE
7562 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
7564 value.value = orig_value;
7565 /* Otherwise, if we have come to a subaggregate,
7566 and we don't have an element of its type, push into it. */
7567 else if (value.value != 0
7568 && value.value != error_mark_node
7569 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
7570 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
7571 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
7573 push_init_level (1);
7577 if (constructor_max_index != 0
7578 && (tree_int_cst_lt (constructor_max_index, constructor_index)
7579 || integer_all_onesp (constructor_max_index)))
7581 pedwarn_init (input_location, 0,
7582 "excess elements in array initializer");
7586 /* Now output the actual element. */
7589 push_array_bounds (tree_low_cst (constructor_index, 1));
7590 output_init_element (value.value, value.original_type,
7591 strict_string, elttype,
7592 constructor_index, 1, implicit);
7593 RESTORE_SPELLING_DEPTH (constructor_depth);
7597 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7600 /* If we are doing the bookkeeping for an element that was
7601 directly output as a constructor, we must update
7602 constructor_unfilled_index. */
7603 constructor_unfilled_index = constructor_index;
7605 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7607 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7609 /* Do a basic check of initializer size. Note that vectors
7610 always have a fixed size derived from their type. */
7611 if (tree_int_cst_lt (constructor_max_index, constructor_index))
7613 pedwarn_init (input_location, 0,
7614 "excess elements in vector initializer");
7618 /* Now output the actual element. */
7621 if (TREE_CODE (value.value) == VECTOR_CST)
7622 elttype = TYPE_MAIN_VARIANT (constructor_type);
7623 output_init_element (value.value, value.original_type,
7624 strict_string, elttype,
7625 constructor_index, 1, implicit);
7629 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7632 /* If we are doing the bookkeeping for an element that was
7633 directly output as a constructor, we must update
7634 constructor_unfilled_index. */
7635 constructor_unfilled_index = constructor_index;
7638 /* Handle the sole element allowed in a braced initializer
7639 for a scalar variable. */
7640 else if (constructor_type != error_mark_node
7641 && constructor_fields == 0)
7643 pedwarn_init (input_location, 0,
7644 "excess elements in scalar initializer");
7650 output_init_element (value.value, value.original_type,
7651 strict_string, constructor_type,
7652 NULL_TREE, 1, implicit);
7653 constructor_fields = 0;
7656 /* Handle range initializers either at this level or anywhere higher
7657 in the designator stack. */
7658 if (constructor_range_stack)
7660 struct constructor_range_stack *p, *range_stack;
7663 range_stack = constructor_range_stack;
7664 constructor_range_stack = 0;
7665 while (constructor_stack != range_stack->stack)
7667 gcc_assert (constructor_stack->implicit);
7668 process_init_element (pop_init_level (1), true);
7670 for (p = range_stack;
7671 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
7674 gcc_assert (constructor_stack->implicit);
7675 process_init_element (pop_init_level (1), true);
7678 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
7679 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
7684 constructor_index = p->index;
7685 constructor_fields = p->fields;
7686 if (finish && p->range_end && p->index == p->range_start)
7694 push_init_level (2);
7695 p->stack = constructor_stack;
7696 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
7697 p->index = p->range_start;
7701 constructor_range_stack = range_stack;
7708 constructor_range_stack = 0;
7711 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7712 (guaranteed to be 'volatile' or null) and ARGS (represented using
7713 an ASM_EXPR node). */
7715 build_asm_stmt (tree cv_qualifier, tree args)
7717 if (!ASM_VOLATILE_P (args) && cv_qualifier)
7718 ASM_VOLATILE_P (args) = 1;
7719 return add_stmt (args);
7722 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7723 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7724 SIMPLE indicates whether there was anything at all after the
7725 string in the asm expression -- asm("blah") and asm("blah" : )
7726 are subtly different. We use a ASM_EXPR node to represent this. */
7728 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
7734 const char *constraint;
7735 const char **oconstraints;
7736 bool allows_mem, allows_reg, is_inout;
7737 int ninputs, noutputs;
7739 ninputs = list_length (inputs);
7740 noutputs = list_length (outputs);
7741 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
7743 string = resolve_asm_operand_names (string, outputs, inputs);
7745 /* Remove output conversions that change the type but not the mode. */
7746 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
7748 tree output = TREE_VALUE (tail);
7750 /* ??? Really, this should not be here. Users should be using a
7751 proper lvalue, dammit. But there's a long history of using casts
7752 in the output operands. In cases like longlong.h, this becomes a
7753 primitive form of typechecking -- if the cast can be removed, then
7754 the output operand had a type of the proper width; otherwise we'll
7755 get an error. Gross, but ... */
7756 STRIP_NOPS (output);
7758 if (!lvalue_or_else (output, lv_asm))
7759 output = error_mark_node;
7761 if (output != error_mark_node
7762 && (TREE_READONLY (output)
7763 || TYPE_READONLY (TREE_TYPE (output))
7764 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
7765 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
7766 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
7767 readonly_error (output, lv_asm);
7769 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7770 oconstraints[i] = constraint;
7772 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7773 &allows_mem, &allows_reg, &is_inout))
7775 /* If the operand is going to end up in memory,
7776 mark it addressable. */
7777 if (!allows_reg && !c_mark_addressable (output))
7778 output = error_mark_node;
7781 output = error_mark_node;
7783 TREE_VALUE (tail) = output;
7786 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
7790 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7791 input = TREE_VALUE (tail);
7793 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
7794 oconstraints, &allows_mem, &allows_reg))
7796 /* If the operand is going to end up in memory,
7797 mark it addressable. */
7798 if (!allows_reg && allows_mem)
7800 /* Strip the nops as we allow this case. FIXME, this really
7801 should be rejected or made deprecated. */
7803 if (!c_mark_addressable (input))
7804 input = error_mark_node;
7808 input = error_mark_node;
7810 TREE_VALUE (tail) = input;
7813 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
7815 /* asm statements without outputs, including simple ones, are treated
7817 ASM_INPUT_P (args) = simple;
7818 ASM_VOLATILE_P (args) = (noutputs == 0);
7823 /* Generate a goto statement to LABEL. */
7826 c_finish_goto_label (tree label)
7828 tree decl = lookup_label (label);
7832 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
7834 error ("jump into statement expression");
7838 if (C_DECL_UNJUMPABLE_VM (decl))
7840 error ("jump into scope of identifier with variably modified type");
7844 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
7846 /* No jump from outside this statement expression context, so
7847 record that there is a jump from within this context. */
7848 struct c_label_list *nlist;
7849 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7850 nlist->next = label_context_stack_se->labels_used;
7851 nlist->label = decl;
7852 label_context_stack_se->labels_used = nlist;
7855 if (!C_DECL_UNDEFINABLE_VM (decl))
7857 /* No jump from outside this context context of identifiers with
7858 variably modified type, so record that there is a jump from
7859 within this context. */
7860 struct c_label_list *nlist;
7861 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7862 nlist->next = label_context_stack_vm->labels_used;
7863 nlist->label = decl;
7864 label_context_stack_vm->labels_used = nlist;
7867 TREE_USED (decl) = 1;
7868 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
7871 /* Generate a computed goto statement to EXPR. */
7874 c_finish_goto_ptr (tree expr)
7876 pedwarn (input_location, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
7877 expr = c_fully_fold (expr, false, NULL);
7878 expr = convert (ptr_type_node, expr);
7879 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
7882 /* Generate a C `return' statement. RETVAL is the expression for what
7883 to return, or a null pointer for `return;' with no value. If
7884 ORIGTYPE is not NULL_TREE, it is the original type of RETVAL. */
7887 c_finish_return (tree retval, tree origtype)
7889 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
7890 bool no_warning = false;
7893 if (TREE_THIS_VOLATILE (current_function_decl))
7894 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7898 tree semantic_type = NULL_TREE;
7899 npc = null_pointer_constant_p (retval);
7900 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
7902 semantic_type = TREE_TYPE (retval);
7903 retval = TREE_OPERAND (retval, 0);
7905 retval = c_fully_fold (retval, false, NULL);
7907 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
7912 current_function_returns_null = 1;
7913 if ((warn_return_type || flag_isoc99)
7914 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7916 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wreturn_type,
7917 "%<return%> with no value, in "
7918 "function returning non-void");
7922 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7924 current_function_returns_null = 1;
7925 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7926 pedwarn (input_location, 0,
7927 "%<return%> with a value, in function returning void");
7929 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
7930 "%<return%> with expression, in function returning void");
7934 tree t = convert_for_assignment (valtype, retval, origtype, ic_return,
7935 npc, NULL_TREE, NULL_TREE, 0);
7936 tree res = DECL_RESULT (current_function_decl);
7939 current_function_returns_value = 1;
7940 if (t == error_mark_node)
7943 inner = t = convert (TREE_TYPE (res), t);
7945 /* Strip any conversions, additions, and subtractions, and see if
7946 we are returning the address of a local variable. Warn if so. */
7949 switch (TREE_CODE (inner))
7952 case NON_LVALUE_EXPR:
7954 case POINTER_PLUS_EXPR:
7955 inner = TREE_OPERAND (inner, 0);
7959 /* If the second operand of the MINUS_EXPR has a pointer
7960 type (or is converted from it), this may be valid, so
7961 don't give a warning. */
7963 tree op1 = TREE_OPERAND (inner, 1);
7965 while (!POINTER_TYPE_P (TREE_TYPE (op1))
7966 && (CONVERT_EXPR_P (op1)
7967 || TREE_CODE (op1) == NON_LVALUE_EXPR))
7968 op1 = TREE_OPERAND (op1, 0);
7970 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7973 inner = TREE_OPERAND (inner, 0);
7978 inner = TREE_OPERAND (inner, 0);
7980 while (REFERENCE_CLASS_P (inner)
7981 && TREE_CODE (inner) != INDIRECT_REF)
7982 inner = TREE_OPERAND (inner, 0);
7985 && !DECL_EXTERNAL (inner)
7986 && !TREE_STATIC (inner)
7987 && DECL_CONTEXT (inner) == current_function_decl)
7988 warning (0, "function returns address of local variable");
7998 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8000 if (warn_sequence_point)
8001 verify_sequence_points (retval);
8004 ret_stmt = build_stmt (RETURN_EXPR, retval);
8005 TREE_NO_WARNING (ret_stmt) |= no_warning;
8006 return add_stmt (ret_stmt);
8010 /* The SWITCH_EXPR being built. */
8013 /* The original type of the testing expression, i.e. before the
8014 default conversion is applied. */
8017 /* A splay-tree mapping the low element of a case range to the high
8018 element, or NULL_TREE if there is no high element. Used to
8019 determine whether or not a new case label duplicates an old case
8020 label. We need a tree, rather than simply a hash table, because
8021 of the GNU case range extension. */
8024 /* Number of nested statement expressions within this switch
8025 statement; if nonzero, case and default labels may not
8027 unsigned int blocked_stmt_expr;
8029 /* Scope of outermost declarations of identifiers with variably
8030 modified type within this switch statement; if nonzero, case and
8031 default labels may not appear. */
8032 unsigned int blocked_vm;
8034 /* The next node on the stack. */
8035 struct c_switch *next;
8038 /* A stack of the currently active switch statements. The innermost
8039 switch statement is on the top of the stack. There is no need to
8040 mark the stack for garbage collection because it is only active
8041 during the processing of the body of a function, and we never
8042 collect at that point. */
8044 struct c_switch *c_switch_stack;
8046 /* Start a C switch statement, testing expression EXP. Return the new
8050 c_start_case (tree exp)
8052 tree orig_type = error_mark_node;
8053 struct c_switch *cs;
8055 if (exp != error_mark_node)
8057 orig_type = TREE_TYPE (exp);
8059 if (!INTEGRAL_TYPE_P (orig_type))
8061 if (orig_type != error_mark_node)
8063 error ("switch quantity not an integer");
8064 orig_type = error_mark_node;
8066 exp = integer_zero_node;
8070 tree type = TYPE_MAIN_VARIANT (orig_type);
8072 if (!in_system_header
8073 && (type == long_integer_type_node
8074 || type == long_unsigned_type_node))
8075 warning (OPT_Wtraditional, "%<long%> switch expression not "
8076 "converted to %<int%> in ISO C");
8078 exp = c_fully_fold (exp, false, NULL);
8079 exp = default_conversion (exp);
8081 if (warn_sequence_point)
8082 verify_sequence_points (exp);
8086 /* Add this new SWITCH_EXPR to the stack. */
8087 cs = XNEW (struct c_switch);
8088 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8089 cs->orig_type = orig_type;
8090 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8091 cs->blocked_stmt_expr = 0;
8093 cs->next = c_switch_stack;
8094 c_switch_stack = cs;
8096 return add_stmt (cs->switch_expr);
8099 /* Process a case label. */
8102 do_case (tree low_value, tree high_value)
8104 tree label = NULL_TREE;
8106 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8108 low_value = c_fully_fold (low_value, false, NULL);
8109 if (TREE_CODE (low_value) == INTEGER_CST)
8110 pedwarn (input_location, OPT_pedantic,
8111 "case label is not an integer constant expression");
8114 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8116 high_value = c_fully_fold (high_value, false, NULL);
8117 if (TREE_CODE (high_value) == INTEGER_CST)
8118 pedwarn (input_location, OPT_pedantic,
8119 "case label is not an integer constant expression");
8122 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
8123 && !c_switch_stack->blocked_vm)
8125 label = c_add_case_label (c_switch_stack->cases,
8126 SWITCH_COND (c_switch_stack->switch_expr),
8127 c_switch_stack->orig_type,
8128 low_value, high_value);
8129 if (label == error_mark_node)
8132 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
8135 error ("case label in statement expression not containing "
8136 "enclosing switch statement");
8138 error ("%<default%> label in statement expression not containing "
8139 "enclosing switch statement");
8141 else if (c_switch_stack && c_switch_stack->blocked_vm)
8144 error ("case label in scope of identifier with variably modified "
8145 "type not containing enclosing switch statement");
8147 error ("%<default%> label in scope of identifier with variably "
8148 "modified type not containing enclosing switch statement");
8151 error ("case label not within a switch statement");
8153 error ("%<default%> label not within a switch statement");
8158 /* Finish the switch statement. */
8161 c_finish_case (tree body)
8163 struct c_switch *cs = c_switch_stack;
8164 location_t switch_location;
8166 SWITCH_BODY (cs->switch_expr) = body;
8168 /* We must not be within a statement expression nested in the switch
8169 at this point; we might, however, be within the scope of an
8170 identifier with variably modified type nested in the switch. */
8171 gcc_assert (!cs->blocked_stmt_expr);
8173 /* Emit warnings as needed. */
8174 if (EXPR_HAS_LOCATION (cs->switch_expr))
8175 switch_location = EXPR_LOCATION (cs->switch_expr);
8177 switch_location = input_location;
8178 c_do_switch_warnings (cs->cases, switch_location,
8179 TREE_TYPE (cs->switch_expr),
8180 SWITCH_COND (cs->switch_expr));
8182 /* Pop the stack. */
8183 c_switch_stack = cs->next;
8184 splay_tree_delete (cs->cases);
8188 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8189 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8190 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8191 statement, and was not surrounded with parenthesis. */
8194 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8195 tree else_block, bool nested_if)
8199 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8200 if (warn_parentheses && nested_if && else_block == NULL)
8202 tree inner_if = then_block;
8204 /* We know from the grammar productions that there is an IF nested
8205 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8206 it might not be exactly THEN_BLOCK, but should be the last
8207 non-container statement within. */
8209 switch (TREE_CODE (inner_if))
8214 inner_if = BIND_EXPR_BODY (inner_if);
8216 case STATEMENT_LIST:
8217 inner_if = expr_last (then_block);
8219 case TRY_FINALLY_EXPR:
8220 case TRY_CATCH_EXPR:
8221 inner_if = TREE_OPERAND (inner_if, 0);
8228 if (COND_EXPR_ELSE (inner_if))
8229 warning (OPT_Wparentheses,
8230 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
8234 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8235 SET_EXPR_LOCATION (stmt, if_locus);
8239 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8240 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8241 is false for DO loops. INCR is the FOR increment expression. BODY is
8242 the statement controlled by the loop. BLAB is the break label. CLAB is
8243 the continue label. Everything is allowed to be NULL. */
8246 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8247 tree blab, tree clab, bool cond_is_first)
8249 tree entry = NULL, exit = NULL, t;
8251 /* If the condition is zero don't generate a loop construct. */
8252 if (cond && integer_zerop (cond))
8256 t = build_and_jump (&blab);
8257 SET_EXPR_LOCATION (t, start_locus);
8263 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8265 /* If we have an exit condition, then we build an IF with gotos either
8266 out of the loop, or to the top of it. If there's no exit condition,
8267 then we just build a jump back to the top. */
8268 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8270 if (cond && !integer_nonzerop (cond))
8272 /* Canonicalize the loop condition to the end. This means
8273 generating a branch to the loop condition. Reuse the
8274 continue label, if possible. */
8279 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8280 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8283 t = build1 (GOTO_EXPR, void_type_node, clab);
8284 SET_EXPR_LOCATION (t, start_locus);
8288 t = build_and_jump (&blab);
8289 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
8291 SET_EXPR_LOCATION (exit, start_locus);
8293 SET_EXPR_LOCATION (exit, input_location);
8302 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8310 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8314 c_finish_bc_stmt (tree *label_p, bool is_break)
8317 tree label = *label_p;
8319 /* In switch statements break is sometimes stylistically used after
8320 a return statement. This can lead to spurious warnings about
8321 control reaching the end of a non-void function when it is
8322 inlined. Note that we are calling block_may_fallthru with
8323 language specific tree nodes; this works because
8324 block_may_fallthru returns true when given something it does not
8326 skip = !block_may_fallthru (cur_stmt_list);
8331 *label_p = label = create_artificial_label ();
8333 else if (TREE_CODE (label) == LABEL_DECL)
8335 else switch (TREE_INT_CST_LOW (label))
8339 error ("break statement not within loop or switch");
8341 error ("continue statement not within a loop");
8345 gcc_assert (is_break);
8346 error ("break statement used with OpenMP for loop");
8357 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8359 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8362 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8365 emit_side_effect_warnings (tree expr)
8367 if (expr == error_mark_node)
8369 else if (!TREE_SIDE_EFFECTS (expr))
8371 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8372 warning (OPT_Wunused_value, "%Hstatement with no effect",
8373 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
8376 warn_if_unused_value (expr, input_location);
8379 /* Process an expression as if it were a complete statement. Emit
8380 diagnostics, but do not call ADD_STMT. */
8383 c_process_expr_stmt (tree expr)
8388 expr = c_fully_fold (expr, false, NULL);
8390 if (warn_sequence_point)
8391 verify_sequence_points (expr);
8393 if (TREE_TYPE (expr) != error_mark_node
8394 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8395 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8396 error ("expression statement has incomplete type");
8398 /* If we're not processing a statement expression, warn about unused values.
8399 Warnings for statement expressions will be emitted later, once we figure
8400 out which is the result. */
8401 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8402 && warn_unused_value)
8403 emit_side_effect_warnings (expr);
8405 /* If the expression is not of a type to which we cannot assign a line
8406 number, wrap the thing in a no-op NOP_EXPR. */
8407 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8408 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8410 if (CAN_HAVE_LOCATION_P (expr))
8411 SET_EXPR_LOCATION (expr, input_location);
8416 /* Emit an expression as a statement. */
8419 c_finish_expr_stmt (tree expr)
8422 return add_stmt (c_process_expr_stmt (expr));
8427 /* Do the opposite and emit a statement as an expression. To begin,
8428 create a new binding level and return it. */
8431 c_begin_stmt_expr (void)
8434 struct c_label_context_se *nstack;
8435 struct c_label_list *glist;
8437 /* We must force a BLOCK for this level so that, if it is not expanded
8438 later, there is a way to turn off the entire subtree of blocks that
8439 are contained in it. */
8441 ret = c_begin_compound_stmt (true);
8444 c_switch_stack->blocked_stmt_expr++;
8445 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8447 for (glist = label_context_stack_se->labels_used;
8449 glist = glist->next)
8451 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
8453 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
8454 nstack->labels_def = NULL;
8455 nstack->labels_used = NULL;
8456 nstack->next = label_context_stack_se;
8457 label_context_stack_se = nstack;
8459 /* Mark the current statement list as belonging to a statement list. */
8460 STATEMENT_LIST_STMT_EXPR (ret) = 1;
8466 c_finish_stmt_expr (tree body)
8468 tree last, type, tmp, val;
8470 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8472 body = c_end_compound_stmt (body, true);
8475 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8476 c_switch_stack->blocked_stmt_expr--;
8478 /* It is no longer possible to jump to labels defined within this
8479 statement expression. */
8480 for (dlist = label_context_stack_se->labels_def;
8482 dlist = dlist->next)
8484 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
8486 /* It is again possible to define labels with a goto just outside
8487 this statement expression. */
8488 for (glist = label_context_stack_se->next->labels_used;
8490 glist = glist->next)
8492 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
8495 if (glist_prev != NULL)
8496 glist_prev->next = label_context_stack_se->labels_used;
8498 label_context_stack_se->next->labels_used
8499 = label_context_stack_se->labels_used;
8500 label_context_stack_se = label_context_stack_se->next;
8502 /* Locate the last statement in BODY. See c_end_compound_stmt
8503 about always returning a BIND_EXPR. */
8504 last_p = &BIND_EXPR_BODY (body);
8505 last = BIND_EXPR_BODY (body);
8508 if (TREE_CODE (last) == STATEMENT_LIST)
8510 tree_stmt_iterator i;
8512 /* This can happen with degenerate cases like ({ }). No value. */
8513 if (!TREE_SIDE_EFFECTS (last))
8516 /* If we're supposed to generate side effects warnings, process
8517 all of the statements except the last. */
8518 if (warn_unused_value)
8520 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8521 emit_side_effect_warnings (tsi_stmt (i));
8524 i = tsi_last (last);
8525 last_p = tsi_stmt_ptr (i);
8529 /* If the end of the list is exception related, then the list was split
8530 by a call to push_cleanup. Continue searching. */
8531 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8532 || TREE_CODE (last) == TRY_CATCH_EXPR)
8534 last_p = &TREE_OPERAND (last, 0);
8536 goto continue_searching;
8539 /* In the case that the BIND_EXPR is not necessary, return the
8540 expression out from inside it. */
8541 if (last == error_mark_node
8542 || (last == BIND_EXPR_BODY (body)
8543 && BIND_EXPR_VARS (body) == NULL))
8545 /* Even if this looks constant, do not allow it in a constant
8547 last = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (last), NULL_TREE, last);
8548 C_MAYBE_CONST_EXPR_NON_CONST (last) = 1;
8549 /* Do not warn if the return value of a statement expression is
8551 TREE_NO_WARNING (last) = 1;
8555 /* Extract the type of said expression. */
8556 type = TREE_TYPE (last);
8558 /* If we're not returning a value at all, then the BIND_EXPR that
8559 we already have is a fine expression to return. */
8560 if (!type || VOID_TYPE_P (type))
8563 /* Now that we've located the expression containing the value, it seems
8564 silly to make voidify_wrapper_expr repeat the process. Create a
8565 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8566 tmp = create_tmp_var_raw (type, NULL);
8568 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8569 tree_expr_nonnegative_p giving up immediately. */
8571 if (TREE_CODE (val) == NOP_EXPR
8572 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
8573 val = TREE_OPERAND (val, 0);
8575 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
8576 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
8578 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
8581 /* Begin the scope of an identifier of variably modified type, scope
8582 number SCOPE. Jumping from outside this scope to inside it is not
8586 c_begin_vm_scope (unsigned int scope)
8588 struct c_label_context_vm *nstack;
8589 struct c_label_list *glist;
8591 gcc_assert (scope > 0);
8593 /* At file_scope, we don't have to do any processing. */
8594 if (label_context_stack_vm == NULL)
8597 if (c_switch_stack && !c_switch_stack->blocked_vm)
8598 c_switch_stack->blocked_vm = scope;
8599 for (glist = label_context_stack_vm->labels_used;
8601 glist = glist->next)
8603 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
8605 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
8606 nstack->labels_def = NULL;
8607 nstack->labels_used = NULL;
8608 nstack->scope = scope;
8609 nstack->next = label_context_stack_vm;
8610 label_context_stack_vm = nstack;
8613 /* End a scope which may contain identifiers of variably modified
8614 type, scope number SCOPE. */
8617 c_end_vm_scope (unsigned int scope)
8619 if (label_context_stack_vm == NULL)
8621 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
8622 c_switch_stack->blocked_vm = 0;
8623 /* We may have a number of nested scopes of identifiers with
8624 variably modified type, all at this depth. Pop each in turn. */
8625 while (label_context_stack_vm->scope == scope)
8627 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8629 /* It is no longer possible to jump to labels defined within this
8631 for (dlist = label_context_stack_vm->labels_def;
8633 dlist = dlist->next)
8635 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
8637 /* It is again possible to define labels with a goto just outside
8639 for (glist = label_context_stack_vm->next->labels_used;
8641 glist = glist->next)
8643 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
8646 if (glist_prev != NULL)
8647 glist_prev->next = label_context_stack_vm->labels_used;
8649 label_context_stack_vm->next->labels_used
8650 = label_context_stack_vm->labels_used;
8651 label_context_stack_vm = label_context_stack_vm->next;
8655 /* Begin and end compound statements. This is as simple as pushing
8656 and popping new statement lists from the tree. */
8659 c_begin_compound_stmt (bool do_scope)
8661 tree stmt = push_stmt_list ();
8668 c_end_compound_stmt (tree stmt, bool do_scope)
8674 if (c_dialect_objc ())
8675 objc_clear_super_receiver ();
8676 block = pop_scope ();
8679 stmt = pop_stmt_list (stmt);
8680 stmt = c_build_bind_expr (block, stmt);
8682 /* If this compound statement is nested immediately inside a statement
8683 expression, then force a BIND_EXPR to be created. Otherwise we'll
8684 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8685 STATEMENT_LISTs merge, and thus we can lose track of what statement
8688 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8689 && TREE_CODE (stmt) != BIND_EXPR)
8691 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
8692 TREE_SIDE_EFFECTS (stmt) = 1;
8698 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8699 when the current scope is exited. EH_ONLY is true when this is not
8700 meant to apply to normal control flow transfer. */
8703 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
8705 enum tree_code code;
8709 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
8710 stmt = build_stmt (code, NULL, cleanup);
8712 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
8713 list = push_stmt_list ();
8714 TREE_OPERAND (stmt, 0) = list;
8715 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
8718 /* Build a binary-operation expression without default conversions.
8719 CODE is the kind of expression to build.
8720 LOCATION is the operator's location.
8721 This function differs from `build' in several ways:
8722 the data type of the result is computed and recorded in it,
8723 warnings are generated if arg data types are invalid,
8724 special handling for addition and subtraction of pointers is known,
8725 and some optimization is done (operations on narrow ints
8726 are done in the narrower type when that gives the same result).
8727 Constant folding is also done before the result is returned.
8729 Note that the operands will never have enumeral types, or function
8730 or array types, because either they will have the default conversions
8731 performed or they have both just been converted to some other type in which
8732 the arithmetic is to be done. */
8735 build_binary_op (location_t location, enum tree_code code,
8736 tree orig_op0, tree orig_op1, int convert_p)
8738 tree type0, type1, orig_type0, orig_type1;
8740 enum tree_code code0, code1;
8742 tree ret = error_mark_node;
8743 const char *invalid_op_diag;
8744 bool op0_int_operands, op1_int_operands;
8745 bool int_const, int_const_or_overflow, int_operands;
8747 /* Expression code to give to the expression when it is built.
8748 Normally this is CODE, which is what the caller asked for,
8749 but in some special cases we change it. */
8750 enum tree_code resultcode = code;
8752 /* Data type in which the computation is to be performed.
8753 In the simplest cases this is the common type of the arguments. */
8754 tree result_type = NULL;
8756 /* When the computation is in excess precision, the type of the
8757 final EXCESS_PRECISION_EXPR. */
8758 tree real_result_type = NULL;
8760 /* Nonzero means operands have already been type-converted
8761 in whatever way is necessary.
8762 Zero means they need to be converted to RESULT_TYPE. */
8765 /* Nonzero means create the expression with this type, rather than
8767 tree build_type = 0;
8769 /* Nonzero means after finally constructing the expression
8770 convert it to this type. */
8771 tree final_type = 0;
8773 /* Nonzero if this is an operation like MIN or MAX which can
8774 safely be computed in short if both args are promoted shorts.
8775 Also implies COMMON.
8776 -1 indicates a bitwise operation; this makes a difference
8777 in the exact conditions for when it is safe to do the operation
8778 in a narrower mode. */
8781 /* Nonzero if this is a comparison operation;
8782 if both args are promoted shorts, compare the original shorts.
8783 Also implies COMMON. */
8784 int short_compare = 0;
8786 /* Nonzero if this is a right-shift operation, which can be computed on the
8787 original short and then promoted if the operand is a promoted short. */
8788 int short_shift = 0;
8790 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8793 /* True means types are compatible as far as ObjC is concerned. */
8796 /* True means this is an arithmetic operation that may need excess
8798 bool may_need_excess_precision;
8800 if (location == UNKNOWN_LOCATION)
8801 location = input_location;
8806 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
8807 if (op0_int_operands)
8808 op0 = remove_c_maybe_const_expr (op0);
8809 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
8810 if (op1_int_operands)
8811 op1 = remove_c_maybe_const_expr (op1);
8812 int_operands = (op0_int_operands && op1_int_operands);
8815 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
8816 && TREE_CODE (orig_op1) == INTEGER_CST);
8817 int_const = (int_const_or_overflow
8818 && !TREE_OVERFLOW (orig_op0)
8819 && !TREE_OVERFLOW (orig_op1));
8822 int_const = int_const_or_overflow = false;
8826 op0 = default_conversion (op0);
8827 op1 = default_conversion (op1);
8830 orig_type0 = type0 = TREE_TYPE (op0);
8831 orig_type1 = type1 = TREE_TYPE (op1);
8833 /* The expression codes of the data types of the arguments tell us
8834 whether the arguments are integers, floating, pointers, etc. */
8835 code0 = TREE_CODE (type0);
8836 code1 = TREE_CODE (type1);
8838 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8839 STRIP_TYPE_NOPS (op0);
8840 STRIP_TYPE_NOPS (op1);
8842 /* If an error was already reported for one of the arguments,
8843 avoid reporting another error. */
8845 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8846 return error_mark_node;
8848 if ((invalid_op_diag
8849 = targetm.invalid_binary_op (code, type0, type1)))
8851 error_at (location, invalid_op_diag);
8852 return error_mark_node;
8860 case TRUNC_DIV_EXPR:
8862 case FLOOR_DIV_EXPR:
8863 case ROUND_DIV_EXPR:
8864 case EXACT_DIV_EXPR:
8865 may_need_excess_precision = true;
8868 may_need_excess_precision = false;
8871 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
8873 op0 = TREE_OPERAND (op0, 0);
8874 type0 = TREE_TYPE (op0);
8876 else if (may_need_excess_precision
8877 && (eptype = excess_precision_type (type0)) != NULL_TREE)
8880 op0 = convert (eptype, op0);
8882 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
8884 op1 = TREE_OPERAND (op1, 0);
8885 type1 = TREE_TYPE (op1);
8887 else if (may_need_excess_precision
8888 && (eptype = excess_precision_type (type1)) != NULL_TREE)
8891 op1 = convert (eptype, op1);
8894 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
8899 /* Handle the pointer + int case. */
8900 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8902 ret = pointer_int_sum (PLUS_EXPR, op0, op1);
8903 goto return_build_binary_op;
8905 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
8907 ret = pointer_int_sum (PLUS_EXPR, op1, op0);
8908 goto return_build_binary_op;
8915 /* Subtraction of two similar pointers.
8916 We must subtract them as integers, then divide by object size. */
8917 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
8918 && comp_target_types (type0, type1))
8920 ret = pointer_diff (op0, op1);
8921 goto return_build_binary_op;
8923 /* Handle pointer minus int. Just like pointer plus int. */
8924 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8926 ret = pointer_int_sum (MINUS_EXPR, op0, op1);
8927 goto return_build_binary_op;
8937 case TRUNC_DIV_EXPR:
8939 case FLOOR_DIV_EXPR:
8940 case ROUND_DIV_EXPR:
8941 case EXACT_DIV_EXPR:
8942 warn_for_div_by_zero (location, op1);
8944 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8945 || code0 == FIXED_POINT_TYPE
8946 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8947 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8948 || code1 == FIXED_POINT_TYPE
8949 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
8951 enum tree_code tcode0 = code0, tcode1 = code1;
8953 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8954 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
8955 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
8956 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
8958 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
8959 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
8960 resultcode = RDIV_EXPR;
8962 /* Although it would be tempting to shorten always here, that
8963 loses on some targets, since the modulo instruction is
8964 undefined if the quotient can't be represented in the
8965 computation mode. We shorten only if unsigned or if
8966 dividing by something we know != -1. */
8967 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8968 || (TREE_CODE (op1) == INTEGER_CST
8969 && !integer_all_onesp (op1)));
8977 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8979 /* Allow vector types which are not floating point types. */
8980 else if (code0 == VECTOR_TYPE
8981 && code1 == VECTOR_TYPE
8982 && !VECTOR_FLOAT_TYPE_P (type0)
8983 && !VECTOR_FLOAT_TYPE_P (type1))
8987 case TRUNC_MOD_EXPR:
8988 case FLOOR_MOD_EXPR:
8989 warn_for_div_by_zero (location, op1);
8991 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8993 /* Although it would be tempting to shorten always here, that loses
8994 on some targets, since the modulo instruction is undefined if the
8995 quotient can't be represented in the computation mode. We shorten
8996 only if unsigned or if dividing by something we know != -1. */
8997 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8998 || (TREE_CODE (op1) == INTEGER_CST
8999 && !integer_all_onesp (op1)));
9004 case TRUTH_ANDIF_EXPR:
9005 case TRUTH_ORIF_EXPR:
9006 case TRUTH_AND_EXPR:
9008 case TRUTH_XOR_EXPR:
9009 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9010 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9011 || code0 == FIXED_POINT_TYPE)
9012 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9013 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9014 || code1 == FIXED_POINT_TYPE))
9016 /* Result of these operations is always an int,
9017 but that does not mean the operands should be
9018 converted to ints! */
9019 result_type = integer_type_node;
9020 op0 = c_common_truthvalue_conversion (location, op0);
9021 op1 = c_common_truthvalue_conversion (location, op1);
9024 if (code == TRUTH_ANDIF_EXPR)
9026 int_const_or_overflow = (int_operands
9027 && TREE_CODE (orig_op0) == INTEGER_CST
9028 && (op0 == truthvalue_false_node
9029 || TREE_CODE (orig_op1) == INTEGER_CST));
9030 int_const = (int_const_or_overflow
9031 && !TREE_OVERFLOW (orig_op0)
9032 && (op0 == truthvalue_false_node
9033 || !TREE_OVERFLOW (orig_op1)));
9035 else if (code == TRUTH_ORIF_EXPR)
9037 int_const_or_overflow = (int_operands
9038 && TREE_CODE (orig_op0) == INTEGER_CST
9039 && (op0 == truthvalue_true_node
9040 || TREE_CODE (orig_op1) == INTEGER_CST));
9041 int_const = (int_const_or_overflow
9042 && !TREE_OVERFLOW (orig_op0)
9043 && (op0 == truthvalue_true_node
9044 || !TREE_OVERFLOW (orig_op1)));
9048 /* Shift operations: result has same type as first operand;
9049 always convert second operand to int.
9050 Also set SHORT_SHIFT if shifting rightward. */
9053 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9054 && code1 == INTEGER_TYPE)
9056 if (TREE_CODE (op1) == INTEGER_CST)
9058 if (tree_int_cst_sgn (op1) < 0)
9061 if (skip_evaluation == 0)
9062 warning (0, "right shift count is negative");
9066 if (!integer_zerop (op1))
9069 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9072 if (skip_evaluation == 0)
9073 warning (0, "right shift count >= width of type");
9078 /* Use the type of the value to be shifted. */
9079 result_type = type0;
9080 /* Convert the shift-count to an integer, regardless of size
9081 of value being shifted. */
9082 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9083 op1 = convert (integer_type_node, op1);
9084 /* Avoid converting op1 to result_type later. */
9090 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9091 && code1 == INTEGER_TYPE)
9093 if (TREE_CODE (op1) == INTEGER_CST)
9095 if (tree_int_cst_sgn (op1) < 0)
9098 if (skip_evaluation == 0)
9099 warning (0, "left shift count is negative");
9102 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9105 if (skip_evaluation == 0)
9106 warning (0, "left shift count >= width of type");
9110 /* Use the type of the value to be shifted. */
9111 result_type = type0;
9112 /* Convert the shift-count to an integer, regardless of size
9113 of value being shifted. */
9114 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9115 op1 = convert (integer_type_node, op1);
9116 /* Avoid converting op1 to result_type later. */
9123 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9124 warning_at (location,
9126 "comparing floating point with == or != is unsafe");
9127 /* Result of comparison is always int,
9128 but don't convert the args to int! */
9129 build_type = integer_type_node;
9130 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9131 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9132 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9133 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9135 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9137 tree tt0 = TREE_TYPE (type0);
9138 tree tt1 = TREE_TYPE (type1);
9139 /* Anything compares with void *. void * compares with anything.
9140 Otherwise, the targets must be compatible
9141 and both must be object or both incomplete. */
9142 if (comp_target_types (type0, type1))
9143 result_type = common_pointer_type (type0, type1);
9144 else if (VOID_TYPE_P (tt0))
9146 /* op0 != orig_op0 detects the case of something
9147 whose value is 0 but which isn't a valid null ptr const. */
9148 if (pedantic && !null_pointer_constant_p (orig_op0)
9149 && TREE_CODE (tt1) == FUNCTION_TYPE)
9150 pedwarn (location, OPT_pedantic, "ISO C forbids "
9151 "comparison of %<void *%> with function pointer");
9153 else if (VOID_TYPE_P (tt1))
9155 if (pedantic && !null_pointer_constant_p (orig_op1)
9156 && TREE_CODE (tt0) == FUNCTION_TYPE)
9157 pedwarn (location, OPT_pedantic, "ISO C forbids "
9158 "comparison of %<void *%> with function pointer");
9161 /* Avoid warning about the volatile ObjC EH puts on decls. */
9163 pedwarn (location, 0,
9164 "comparison of distinct pointer types lacks a cast");
9166 if (result_type == NULL_TREE)
9167 result_type = ptr_type_node;
9169 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9171 if (TREE_CODE (op0) == ADDR_EXPR
9172 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9173 warning_at (location,
9174 OPT_Waddress, "the address of %qD will never be NULL",
9175 TREE_OPERAND (op0, 0));
9176 result_type = type0;
9178 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9180 if (TREE_CODE (op1) == ADDR_EXPR
9181 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9182 warning_at (location,
9183 OPT_Waddress, "the address of %qD will never be NULL",
9184 TREE_OPERAND (op1, 0));
9185 result_type = type1;
9187 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9189 result_type = type0;
9190 pedwarn (location, 0, "comparison between pointer and integer");
9192 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9194 result_type = type1;
9195 pedwarn (location, 0, "comparison between pointer and integer");
9203 build_type = integer_type_node;
9204 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9205 || code0 == FIXED_POINT_TYPE)
9206 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9207 || code1 == FIXED_POINT_TYPE))
9209 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9211 if (comp_target_types (type0, type1))
9213 result_type = common_pointer_type (type0, type1);
9214 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9215 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9216 pedwarn (location, 0,
9217 "comparison of complete and incomplete pointers");
9218 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9219 pedwarn (location, OPT_pedantic, "ISO C forbids "
9220 "ordered comparisons of pointers to functions");
9224 result_type = ptr_type_node;
9225 pedwarn (location, 0,
9226 "comparison of distinct pointer types lacks a cast");
9229 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9231 result_type = type0;
9233 pedwarn (location, OPT_pedantic,
9234 "ordered comparison of pointer with integer zero");
9235 else if (extra_warnings)
9236 warning_at (location, OPT_Wextra,
9237 "ordered comparison of pointer with integer zero");
9239 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9241 result_type = type1;
9242 pedwarn (location, OPT_pedantic,
9243 "ordered comparison of pointer with integer zero");
9245 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9247 result_type = type0;
9248 pedwarn (location, 0, "comparison between pointer and integer");
9250 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9252 result_type = type1;
9253 pedwarn (location, 0, "comparison between pointer and integer");
9261 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9262 return error_mark_node;
9264 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9265 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9266 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9267 TREE_TYPE (type1))))
9269 binary_op_error (location, code, type0, type1);
9270 return error_mark_node;
9273 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9274 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9276 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9277 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9279 bool first_complex = (code0 == COMPLEX_TYPE);
9280 bool second_complex = (code1 == COMPLEX_TYPE);
9281 int none_complex = (!first_complex && !second_complex);
9283 if (shorten || common || short_compare)
9285 result_type = c_common_type (type0, type1);
9286 if (result_type == error_mark_node)
9287 return error_mark_node;
9290 if (first_complex != second_complex
9291 && (code == PLUS_EXPR
9292 || code == MINUS_EXPR
9293 || code == MULT_EXPR
9294 || (code == TRUNC_DIV_EXPR && first_complex))
9295 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
9296 && flag_signed_zeros)
9298 /* An operation on mixed real/complex operands must be
9299 handled specially, but the language-independent code can
9300 more easily optimize the plain complex arithmetic if
9301 -fno-signed-zeros. */
9302 tree real_type = TREE_TYPE (result_type);
9304 if (type0 != orig_type0 || type1 != orig_type1)
9306 gcc_assert (may_need_excess_precision && common);
9307 real_result_type = c_common_type (orig_type0, orig_type1);
9311 if (TREE_TYPE (op0) != result_type)
9312 op0 = convert_and_check (result_type, op0);
9313 if (TREE_TYPE (op1) != real_type)
9314 op1 = convert_and_check (real_type, op1);
9318 if (TREE_TYPE (op0) != real_type)
9319 op0 = convert_and_check (real_type, op0);
9320 if (TREE_TYPE (op1) != result_type)
9321 op1 = convert_and_check (result_type, op1);
9323 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9324 return error_mark_node;
9327 op0 = c_save_expr (op0);
9328 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
9330 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
9335 case TRUNC_DIV_EXPR:
9336 imag = build2 (resultcode, real_type, imag, op1);
9340 real = build2 (resultcode, real_type, real, op1);
9348 op1 = c_save_expr (op1);
9349 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
9351 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
9356 imag = build2 (resultcode, real_type, op0, imag);
9359 real = build2 (resultcode, real_type, op0, real);
9362 real = build2 (resultcode, real_type, op0, real);
9363 imag = build1 (NEGATE_EXPR, real_type, imag);
9369 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
9370 goto return_build_binary_op;
9373 /* For certain operations (which identify themselves by shorten != 0)
9374 if both args were extended from the same smaller type,
9375 do the arithmetic in that type and then extend.
9377 shorten !=0 and !=1 indicates a bitwise operation.
9378 For them, this optimization is safe only if
9379 both args are zero-extended or both are sign-extended.
9380 Otherwise, we might change the result.
9381 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9382 but calculated in (unsigned short) it would be (unsigned short)-1. */
9384 if (shorten && none_complex)
9386 final_type = result_type;
9387 result_type = shorten_binary_op (result_type, op0, op1,
9391 /* Shifts can be shortened if shifting right. */
9396 tree arg0 = get_narrower (op0, &unsigned_arg);
9398 final_type = result_type;
9400 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9401 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9403 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9404 /* We can shorten only if the shift count is less than the
9405 number of bits in the smaller type size. */
9406 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9407 /* We cannot drop an unsigned shift after sign-extension. */
9408 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9410 /* Do an unsigned shift if the operand was zero-extended. */
9412 = c_common_signed_or_unsigned_type (unsigned_arg,
9414 /* Convert value-to-be-shifted to that type. */
9415 if (TREE_TYPE (op0) != result_type)
9416 op0 = convert (result_type, op0);
9421 /* Comparison operations are shortened too but differently.
9422 They identify themselves by setting short_compare = 1. */
9426 /* Don't write &op0, etc., because that would prevent op0
9427 from being kept in a register.
9428 Instead, make copies of the our local variables and
9429 pass the copies by reference, then copy them back afterward. */
9430 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9431 enum tree_code xresultcode = resultcode;
9433 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9438 goto return_build_binary_op;
9441 op0 = xop0, op1 = xop1;
9443 resultcode = xresultcode;
9445 if (!skip_evaluation)
9447 bool op0_maybe_const = true;
9448 bool op1_maybe_const = true;
9449 tree orig_op0_folded, orig_op1_folded;
9451 if (in_late_binary_op)
9453 orig_op0_folded = orig_op0;
9454 orig_op1_folded = orig_op1;
9458 /* Fold for the sake of possible warnings, as in
9459 build_conditional_expr. This requires the
9460 "original" values to be folded, not just op0 and
9462 op0 = c_fully_fold (op0, require_constant_value,
9464 op1 = c_fully_fold (op1, require_constant_value,
9466 orig_op0_folded = c_fully_fold (orig_op0,
9467 require_constant_value,
9469 orig_op1_folded = c_fully_fold (orig_op1,
9470 require_constant_value,
9474 if (warn_sign_compare)
9475 warn_for_sign_compare (location, orig_op0_folded,
9476 orig_op1_folded, op0, op1,
9477 result_type, resultcode);
9478 if (!in_late_binary_op)
9480 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
9482 op0 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op0),
9484 C_MAYBE_CONST_EXPR_NON_CONST (op0) = !op0_maybe_const;
9486 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
9488 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
9490 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
9497 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9498 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9499 Then the expression will be built.
9500 It will be given type FINAL_TYPE if that is nonzero;
9501 otherwise, it will be given type RESULT_TYPE. */
9505 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
9506 return error_mark_node;
9511 if (TREE_TYPE (op0) != result_type)
9512 op0 = convert_and_check (result_type, op0);
9513 if (TREE_TYPE (op1) != result_type)
9514 op1 = convert_and_check (result_type, op1);
9516 /* This can happen if one operand has a vector type, and the other
9517 has a different type. */
9518 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9519 return error_mark_node;
9522 if (build_type == NULL_TREE)
9524 build_type = result_type;
9525 if (type0 != orig_type0 || type1 != orig_type1)
9527 gcc_assert (may_need_excess_precision && common);
9528 real_result_type = c_common_type (orig_type0, orig_type1);
9532 /* Treat expressions in initializers specially as they can't trap. */
9533 if (int_const_or_overflow)
9534 ret = (require_constant_value
9535 ? fold_build2_initializer (resultcode, build_type, op0, op1)
9536 : fold_build2 (resultcode, build_type, op0, op1));
9538 ret = build2 (resultcode, build_type, op0, op1);
9539 if (final_type != 0)
9540 ret = convert (final_type, ret);
9542 return_build_binary_op:
9543 gcc_assert (ret != error_mark_node);
9544 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
9546 ? note_integer_operands (ret)
9547 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
9548 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
9549 && !in_late_binary_op)
9550 ret = note_integer_operands (ret);
9551 if (real_result_type)
9552 ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret);
9553 protected_set_expr_location (ret, location);
9558 /* Convert EXPR to be a truth-value, validating its type for this
9559 purpose. LOCATION is the source location for the expression. */
9562 c_objc_common_truthvalue_conversion (location_t location, tree expr)
9564 bool int_const, int_operands;
9566 switch (TREE_CODE (TREE_TYPE (expr)))
9569 error_at (location, "used array that cannot be converted to pointer where scalar is required");
9570 return error_mark_node;
9573 error_at (location, "used struct type value where scalar is required");
9574 return error_mark_node;
9577 error_at (location, "used union type value where scalar is required");
9578 return error_mark_node;
9587 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
9588 int_operands = EXPR_INT_CONST_OPERANDS (expr);
9590 expr = remove_c_maybe_const_expr (expr);
9592 /* ??? Should we also give an error for void and vectors rather than
9593 leaving those to give errors later? */
9594 expr = c_common_truthvalue_conversion (location, expr);
9596 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
9598 if (TREE_OVERFLOW (expr))
9601 return note_integer_operands (expr);
9603 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
9604 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9609 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9613 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
9615 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
9617 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
9618 /* Executing a compound literal inside a function reinitializes
9620 if (!TREE_STATIC (decl))
9628 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9631 c_begin_omp_parallel (void)
9636 block = c_begin_compound_stmt (true);
9641 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound statement. */
9644 c_finish_omp_parallel (tree clauses, tree block)
9648 block = c_end_compound_stmt (block, true);
9650 stmt = make_node (OMP_PARALLEL);
9651 TREE_TYPE (stmt) = void_type_node;
9652 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9653 OMP_PARALLEL_BODY (stmt) = block;
9655 return add_stmt (stmt);
9658 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9661 c_begin_omp_task (void)
9666 block = c_begin_compound_stmt (true);
9671 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound statement. */
9674 c_finish_omp_task (tree clauses, tree block)
9678 block = c_end_compound_stmt (block, true);
9680 stmt = make_node (OMP_TASK);
9681 TREE_TYPE (stmt) = void_type_node;
9682 OMP_TASK_CLAUSES (stmt) = clauses;
9683 OMP_TASK_BODY (stmt) = block;
9685 return add_stmt (stmt);
9688 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9689 Remove any elements from the list that are invalid. */
9692 c_finish_omp_clauses (tree clauses)
9694 bitmap_head generic_head, firstprivate_head, lastprivate_head;
9695 tree c, t, *pc = &clauses;
9698 bitmap_obstack_initialize (NULL);
9699 bitmap_initialize (&generic_head, &bitmap_default_obstack);
9700 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
9701 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
9703 for (pc = &clauses, c = clauses; c ; c = *pc)
9705 bool remove = false;
9706 bool need_complete = false;
9707 bool need_implicitly_determined = false;
9709 switch (OMP_CLAUSE_CODE (c))
9711 case OMP_CLAUSE_SHARED:
9713 need_implicitly_determined = true;
9714 goto check_dup_generic;
9716 case OMP_CLAUSE_PRIVATE:
9718 need_complete = true;
9719 need_implicitly_determined = true;
9720 goto check_dup_generic;
9722 case OMP_CLAUSE_REDUCTION:
9724 need_implicitly_determined = true;
9725 t = OMP_CLAUSE_DECL (c);
9726 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
9727 || POINTER_TYPE_P (TREE_TYPE (t)))
9729 error ("%qE has invalid type for %<reduction%>", t);
9732 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
9734 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
9735 const char *r_name = NULL;
9752 case TRUTH_ANDIF_EXPR:
9755 case TRUTH_ORIF_EXPR:
9763 error ("%qE has invalid type for %<reduction(%s)%>",
9768 goto check_dup_generic;
9770 case OMP_CLAUSE_COPYPRIVATE:
9771 name = "copyprivate";
9772 goto check_dup_generic;
9774 case OMP_CLAUSE_COPYIN:
9776 t = OMP_CLAUSE_DECL (c);
9777 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
9779 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
9782 goto check_dup_generic;
9785 t = OMP_CLAUSE_DECL (c);
9786 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9788 error ("%qE is not a variable in clause %qs", t, name);
9791 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9792 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9793 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9795 error ("%qE appears more than once in data clauses", t);
9799 bitmap_set_bit (&generic_head, DECL_UID (t));
9802 case OMP_CLAUSE_FIRSTPRIVATE:
9803 name = "firstprivate";
9804 t = OMP_CLAUSE_DECL (c);
9805 need_complete = true;
9806 need_implicitly_determined = true;
9807 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9809 error ("%qE is not a variable in clause %<firstprivate%>", t);
9812 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9813 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9815 error ("%qE appears more than once in data clauses", t);
9819 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
9822 case OMP_CLAUSE_LASTPRIVATE:
9823 name = "lastprivate";
9824 t = OMP_CLAUSE_DECL (c);
9825 need_complete = true;
9826 need_implicitly_determined = true;
9827 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9829 error ("%qE is not a variable in clause %<lastprivate%>", t);
9832 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9833 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9835 error ("%qE appears more than once in data clauses", t);
9839 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
9843 case OMP_CLAUSE_NUM_THREADS:
9844 case OMP_CLAUSE_SCHEDULE:
9845 case OMP_CLAUSE_NOWAIT:
9846 case OMP_CLAUSE_ORDERED:
9847 case OMP_CLAUSE_DEFAULT:
9848 case OMP_CLAUSE_UNTIED:
9849 case OMP_CLAUSE_COLLAPSE:
9850 pc = &OMP_CLAUSE_CHAIN (c);
9859 t = OMP_CLAUSE_DECL (c);
9863 t = require_complete_type (t);
9864 if (t == error_mark_node)
9868 if (need_implicitly_determined)
9870 const char *share_name = NULL;
9872 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
9873 share_name = "threadprivate";
9874 else switch (c_omp_predetermined_sharing (t))
9876 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9878 case OMP_CLAUSE_DEFAULT_SHARED:
9879 share_name = "shared";
9881 case OMP_CLAUSE_DEFAULT_PRIVATE:
9882 share_name = "private";
9889 error ("%qE is predetermined %qs for %qs",
9890 t, share_name, name);
9897 *pc = OMP_CLAUSE_CHAIN (c);
9899 pc = &OMP_CLAUSE_CHAIN (c);
9902 bitmap_obstack_release (NULL);
9906 /* Make a variant type in the proper way for C/C++, propagating qualifiers
9907 down to the element type of an array. */
9910 c_build_qualified_type (tree type, int type_quals)
9912 if (type == error_mark_node)
9915 if (TREE_CODE (type) == ARRAY_TYPE)
9918 tree element_type = c_build_qualified_type (TREE_TYPE (type),
9921 /* See if we already have an identically qualified type. */
9922 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9924 if (TYPE_QUALS (strip_array_types (t)) == type_quals
9925 && TYPE_NAME (t) == TYPE_NAME (type)
9926 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
9927 && attribute_list_equal (TYPE_ATTRIBUTES (t),
9928 TYPE_ATTRIBUTES (type)))
9933 tree domain = TYPE_DOMAIN (type);
9935 t = build_variant_type_copy (type);
9936 TREE_TYPE (t) = element_type;
9938 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
9939 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
9940 SET_TYPE_STRUCTURAL_EQUALITY (t);
9941 else if (TYPE_CANONICAL (element_type) != element_type
9942 || (domain && TYPE_CANONICAL (domain) != domain))
9944 tree unqualified_canon
9945 = build_array_type (TYPE_CANONICAL (element_type),
9946 domain? TYPE_CANONICAL (domain)
9949 = c_build_qualified_type (unqualified_canon, type_quals);
9952 TYPE_CANONICAL (t) = t;
9957 /* A restrict-qualified pointer type must be a pointer to object or
9958 incomplete type. Note that the use of POINTER_TYPE_P also allows
9959 REFERENCE_TYPEs, which is appropriate for C++. */
9960 if ((type_quals & TYPE_QUAL_RESTRICT)
9961 && (!POINTER_TYPE_P (type)
9962 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
9964 error ("invalid use of %<restrict%>");
9965 type_quals &= ~TYPE_QUAL_RESTRICT;
9968 return build_qualified_type (type, type_quals);