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, 2010
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"
45 #include "tree-iterator.h"
47 #include "tree-flow.h"
49 /* Possible cases of implicit bad conversions. Used to select
50 diagnostic messages in convert_for_assignment. */
58 /* Whether we are building a boolean conversion inside
59 convert_for_assignment, or some other late binary operation. If
60 build_binary_op is called (from code shared with C++) in this case,
61 then the operands have already been folded and the result will not
62 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
63 bool in_late_binary_op
;
65 /* The level of nesting inside "__alignof__". */
68 /* The level of nesting inside "sizeof". */
71 /* The level of nesting inside "typeof". */
74 /* Nonzero if we've already printed a "missing braces around initializer"
75 message within this initializer. */
76 static int missing_braces_mentioned
;
78 static int require_constant_value
;
79 static int require_constant_elements
;
81 static bool null_pointer_constant_p (const_tree
);
82 static tree
qualify_type (tree
, tree
);
83 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *);
84 static int comp_target_types (location_t
, tree
, tree
);
85 static int function_types_compatible_p (const_tree
, const_tree
, bool *);
86 static int type_lists_compatible_p (const_tree
, const_tree
, bool *);
87 static tree
lookup_field (tree
, tree
);
88 static int convert_arguments (tree
, VEC(tree
,gc
) *, VEC(tree
,gc
) *, tree
,
90 static tree
pointer_diff (location_t
, tree
, tree
);
91 static tree
convert_for_assignment (location_t
, tree
, tree
, tree
,
92 enum impl_conv
, bool, tree
, tree
, int);
93 static tree
valid_compound_expr_initializer (tree
, tree
);
94 static void push_string (const char *);
95 static void push_member_name (tree
);
96 static int spelling_length (void);
97 static char *print_spelling (char *);
98 static void warning_init (int, const char *);
99 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
100 static void output_init_element (tree
, tree
, bool, tree
, tree
, int, bool,
102 static void output_pending_init_elements (int, struct obstack
*);
103 static int set_designator (int, struct obstack
*);
104 static void push_range_stack (tree
, struct obstack
*);
105 static void add_pending_init (tree
, tree
, tree
, bool, struct obstack
*);
106 static void set_nonincremental_init (struct obstack
*);
107 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
108 static tree
find_init_member (tree
, struct obstack
*);
109 static void readonly_error (tree
, enum lvalue_use
);
110 static void readonly_warning (tree
, enum lvalue_use
);
111 static int lvalue_or_else (const_tree
, enum lvalue_use
);
112 static void record_maybe_used_decl (tree
);
113 static int comptypes_internal (const_tree
, const_tree
, bool *);
115 /* Return true if EXP is a null pointer constant, false otherwise. */
118 null_pointer_constant_p (const_tree expr
)
120 /* This should really operate on c_expr structures, but they aren't
121 yet available everywhere required. */
122 tree type
= TREE_TYPE (expr
);
123 return (TREE_CODE (expr
) == INTEGER_CST
124 && !TREE_OVERFLOW (expr
)
125 && integer_zerop (expr
)
126 && (INTEGRAL_TYPE_P (type
)
127 || (TREE_CODE (type
) == POINTER_TYPE
128 && VOID_TYPE_P (TREE_TYPE (type
))
129 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
132 /* EXPR may appear in an unevaluated part of an integer constant
133 expression, but not in an evaluated part. Wrap it in a
134 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
135 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
138 note_integer_operands (tree expr
)
141 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
143 ret
= copy_node (expr
);
144 TREE_OVERFLOW (ret
) = 1;
148 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
149 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
154 /* Having checked whether EXPR may appear in an unevaluated part of an
155 integer constant expression and found that it may, remove any
156 C_MAYBE_CONST_EXPR noting this fact and return the resulting
160 remove_c_maybe_const_expr (tree expr
)
162 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
163 return C_MAYBE_CONST_EXPR_EXPR (expr
);
168 \f/* This is a cache to hold if two types are compatible or not. */
170 struct tagged_tu_seen_cache
{
171 const struct tagged_tu_seen_cache
* next
;
174 /* The return value of tagged_types_tu_compatible_p if we had seen
175 these two types already. */
179 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
180 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
182 /* Do `exp = require_complete_type (exp);' to make sure exp
183 does not have an incomplete type. (That includes void types.) */
186 require_complete_type (tree value
)
188 tree type
= TREE_TYPE (value
);
190 if (value
== error_mark_node
|| type
== error_mark_node
)
191 return error_mark_node
;
193 /* First, detect a valid value with a complete type. */
194 if (COMPLETE_TYPE_P (type
))
197 c_incomplete_type_error (value
, type
);
198 return error_mark_node
;
201 /* Print an error message for invalid use of an incomplete type.
202 VALUE is the expression that was used (or 0 if that isn't known)
203 and TYPE is the type that was invalid. */
206 c_incomplete_type_error (const_tree value
, const_tree type
)
208 const char *type_code_string
;
210 /* Avoid duplicate error message. */
211 if (TREE_CODE (type
) == ERROR_MARK
)
214 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
215 || TREE_CODE (value
) == PARM_DECL
))
216 error ("%qD has an incomplete type", value
);
220 /* We must print an error message. Be clever about what it says. */
222 switch (TREE_CODE (type
))
225 type_code_string
= "struct";
229 type_code_string
= "union";
233 type_code_string
= "enum";
237 error ("invalid use of void expression");
241 if (TYPE_DOMAIN (type
))
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
245 error ("invalid use of flexible array member");
248 type
= TREE_TYPE (type
);
251 error ("invalid use of array with unspecified bounds");
258 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
259 error ("invalid use of undefined type %<%s %E%>",
260 type_code_string
, TYPE_NAME (type
));
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type
));
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
271 c_type_promotes_to (tree type
)
273 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
274 return double_type_node
;
276 if (c_promoting_integer_type_p (type
))
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type
)
280 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
281 return unsigned_type_node
;
282 return integer_type_node
;
288 /* Return true if between two named address spaces, whether there is a superset
289 named address space that encompasses both address spaces. If there is a
290 superset, return which address space is the superset. */
293 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
300 else if (targetm
.addr_space
.subset_p (as1
, as2
))
305 else if (targetm
.addr_space
.subset_p (as2
, as1
))
314 /* Return a variant of TYPE which has all the type qualifiers of LIKE
315 as well as those of TYPE. */
318 qualify_type (tree type
, tree like
)
320 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
321 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
322 addr_space_t as_common
;
324 /* If the two named address spaces are different, determine the common
325 superset address space. If there isn't one, raise an error. */
326 if (!addr_space_superset (as_type
, as_like
, &as_common
))
329 error ("%qT and %qT are in disjoint named address spaces",
333 return c_build_qualified_type (type
,
334 TYPE_QUALS_NO_ADDR_SPACE (type
)
335 | TYPE_QUALS_NO_ADDR_SPACE (like
)
336 | ENCODE_QUAL_ADDR_SPACE (as_common
));
339 /* Return true iff the given tree T is a variable length array. */
342 c_vla_type_p (const_tree t
)
344 if (TREE_CODE (t
) == ARRAY_TYPE
345 && C_TYPE_VARIABLE_SIZE (t
))
350 /* Return the composite type of two compatible types.
352 We assume that comptypes has already been done and returned
353 nonzero; if that isn't so, this may crash. In particular, we
354 assume that qualifiers match. */
357 composite_type (tree t1
, tree t2
)
359 enum tree_code code1
;
360 enum tree_code code2
;
363 /* Save time if the two types are the same. */
365 if (t1
== t2
) return t1
;
367 /* If one type is nonsense, use the other. */
368 if (t1
== error_mark_node
)
370 if (t2
== error_mark_node
)
373 code1
= TREE_CODE (t1
);
374 code2
= TREE_CODE (t2
);
376 /* Merge the attributes. */
377 attributes
= targetm
.merge_type_attributes (t1
, t2
);
379 /* If one is an enumerated type and the other is the compatible
380 integer type, the composite type might be either of the two
381 (DR#013 question 3). For consistency, use the enumerated type as
382 the composite type. */
384 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
386 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
389 gcc_assert (code1
== code2
);
394 /* For two pointers, do this recursively on the target type. */
396 tree pointed_to_1
= TREE_TYPE (t1
);
397 tree pointed_to_2
= TREE_TYPE (t2
);
398 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
399 t1
= build_pointer_type (target
);
400 t1
= build_type_attribute_variant (t1
, attributes
);
401 return qualify_type (t1
, t2
);
406 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
409 tree d1
= TYPE_DOMAIN (t1
);
410 tree d2
= TYPE_DOMAIN (t2
);
411 bool d1_variable
, d2_variable
;
412 bool d1_zero
, d2_zero
;
413 bool t1_complete
, t2_complete
;
415 /* We should not have any type quals on arrays at all. */
416 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
417 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
419 t1_complete
= COMPLETE_TYPE_P (t1
);
420 t2_complete
= COMPLETE_TYPE_P (t2
);
422 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
423 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
425 d1_variable
= (!d1_zero
426 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
427 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
428 d2_variable
= (!d2_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
431 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
432 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
434 /* Save space: see if the result is identical to one of the args. */
435 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
436 && (d2_variable
|| d2_zero
|| !d1_variable
))
437 return build_type_attribute_variant (t1
, attributes
);
438 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
439 && (d1_variable
|| d1_zero
|| !d2_variable
))
440 return build_type_attribute_variant (t2
, attributes
);
442 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
443 return build_type_attribute_variant (t1
, attributes
);
444 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
445 return build_type_attribute_variant (t2
, attributes
);
447 /* Merge the element types, and have a size if either arg has
448 one. We may have qualifiers on the element types. To set
449 up TYPE_MAIN_VARIANT correctly, we need to form the
450 composite of the unqualified types and add the qualifiers
452 quals
= TYPE_QUALS (strip_array_types (elt
));
453 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
454 t1
= build_array_type (unqual_elt
,
455 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
461 /* Ensure a composite type involving a zero-length array type
462 is a zero-length type not an incomplete type. */
463 if (d1_zero
&& d2_zero
464 && (t1_complete
|| t2_complete
)
465 && !COMPLETE_TYPE_P (t1
))
467 TYPE_SIZE (t1
) = bitsize_zero_node
;
468 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
470 t1
= c_build_qualified_type (t1
, quals
);
471 return build_type_attribute_variant (t1
, attributes
);
477 if (attributes
!= NULL
)
479 /* Try harder not to create a new aggregate type. */
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
482 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
485 return build_type_attribute_variant (t1
, attributes
);
488 /* Function types: prefer the one that specified arg types.
489 If both do, merge the arg types. Also merge the return types. */
491 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
492 tree p1
= TYPE_ARG_TYPES (t1
);
493 tree p2
= TYPE_ARG_TYPES (t2
);
498 /* Save space: see if the result is identical to one of the args. */
499 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
500 return build_type_attribute_variant (t1
, attributes
);
501 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
502 return build_type_attribute_variant (t2
, attributes
);
504 /* Simple way if one arg fails to specify argument types. */
505 if (TYPE_ARG_TYPES (t1
) == 0)
507 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
508 t1
= build_type_attribute_variant (t1
, attributes
);
509 return qualify_type (t1
, t2
);
511 if (TYPE_ARG_TYPES (t2
) == 0)
513 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
514 t1
= build_type_attribute_variant (t1
, attributes
);
515 return qualify_type (t1
, t2
);
518 /* If both args specify argument types, we must merge the two
519 lists, argument by argument. */
520 /* Tell global_bindings_p to return false so that variable_size
521 doesn't die on VLAs in parameter types. */
522 c_override_global_bindings_to_false
= true;
524 len
= list_length (p1
);
527 for (i
= 0; i
< len
; i
++)
528 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
533 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
535 /* A null type means arg type is not specified.
536 Take whatever the other function type has. */
537 if (TREE_VALUE (p1
) == 0)
539 TREE_VALUE (n
) = TREE_VALUE (p2
);
542 if (TREE_VALUE (p2
) == 0)
544 TREE_VALUE (n
) = TREE_VALUE (p1
);
548 /* Given wait (union {union wait *u; int *i} *)
549 and wait (union wait *),
550 prefer union wait * as type of parm. */
551 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
552 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
555 tree mv2
= TREE_VALUE (p2
);
556 if (mv2
&& mv2
!= error_mark_node
557 && TREE_CODE (mv2
) != ARRAY_TYPE
)
558 mv2
= TYPE_MAIN_VARIANT (mv2
);
559 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
560 memb
; memb
= TREE_CHAIN (memb
))
562 tree mv3
= TREE_TYPE (memb
);
563 if (mv3
&& mv3
!= error_mark_node
564 && TREE_CODE (mv3
) != ARRAY_TYPE
)
565 mv3
= TYPE_MAIN_VARIANT (mv3
);
566 if (comptypes (mv3
, mv2
))
568 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
570 pedwarn (input_location
, OPT_pedantic
,
571 "function types not truly compatible in ISO C");
576 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
577 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
580 tree mv1
= TREE_VALUE (p1
);
581 if (mv1
&& mv1
!= error_mark_node
582 && TREE_CODE (mv1
) != ARRAY_TYPE
)
583 mv1
= TYPE_MAIN_VARIANT (mv1
);
584 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
585 memb
; memb
= TREE_CHAIN (memb
))
587 tree mv3
= TREE_TYPE (memb
);
588 if (mv3
&& mv3
!= error_mark_node
589 && TREE_CODE (mv3
) != ARRAY_TYPE
)
590 mv3
= TYPE_MAIN_VARIANT (mv3
);
591 if (comptypes (mv3
, mv1
))
593 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
595 pedwarn (input_location
, OPT_pedantic
,
596 "function types not truly compatible in ISO C");
601 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
605 c_override_global_bindings_to_false
= false;
606 t1
= build_function_type (valtype
, newargs
);
607 t1
= qualify_type (t1
, t2
);
608 /* ... falls through ... */
612 return build_type_attribute_variant (t1
, attributes
);
617 /* Return the type of a conditional expression between pointers to
618 possibly differently qualified versions of compatible types.
620 We assume that comp_target_types has already been done and returned
621 nonzero; if that isn't so, this may crash. */
624 common_pointer_type (tree t1
, tree t2
)
627 tree pointed_to_1
, mv1
;
628 tree pointed_to_2
, mv2
;
630 unsigned target_quals
;
631 addr_space_t as1
, as2
, as_common
;
634 /* Save time if the two types are the same. */
636 if (t1
== t2
) return t1
;
638 /* If one type is nonsense, use the other. */
639 if (t1
== error_mark_node
)
641 if (t2
== error_mark_node
)
644 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
645 && TREE_CODE (t2
) == POINTER_TYPE
);
647 /* Merge the attributes. */
648 attributes
= targetm
.merge_type_attributes (t1
, t2
);
650 /* Find the composite type of the target types, and combine the
651 qualifiers of the two types' targets. Do not lose qualifiers on
652 array element types by taking the TYPE_MAIN_VARIANT. */
653 mv1
= pointed_to_1
= TREE_TYPE (t1
);
654 mv2
= pointed_to_2
= TREE_TYPE (t2
);
655 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
656 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
657 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
658 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
659 target
= composite_type (mv1
, mv2
);
661 /* For function types do not merge const qualifiers, but drop them
662 if used inconsistently. The middle-end uses these to mark const
663 and noreturn functions. */
664 quals1
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1
);
665 quals2
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2
);
667 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
668 target_quals
= (quals1
& quals2
);
670 target_quals
= (quals1
| quals2
);
672 /* If the two named address spaces are different, determine the common
673 superset address space. This is guaranteed to exist due to the
674 assumption that comp_target_type returned non-zero. */
675 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
676 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
677 if (!addr_space_superset (as1
, as2
, &as_common
))
680 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
682 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
683 return build_type_attribute_variant (t1
, attributes
);
686 /* Return the common type for two arithmetic types under the usual
687 arithmetic conversions. The default conversions have already been
688 applied, and enumerated types converted to their compatible integer
689 types. The resulting type is unqualified and has no attributes.
691 This is the type for the result of most arithmetic operations
692 if the operands have the given two types. */
695 c_common_type (tree t1
, tree t2
)
697 enum tree_code code1
;
698 enum tree_code code2
;
700 /* If one type is nonsense, use the other. */
701 if (t1
== error_mark_node
)
703 if (t2
== error_mark_node
)
706 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
707 t1
= TYPE_MAIN_VARIANT (t1
);
709 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
710 t2
= TYPE_MAIN_VARIANT (t2
);
712 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
713 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
715 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
716 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
718 /* Save time if the two types are the same. */
720 if (t1
== t2
) return t1
;
722 code1
= TREE_CODE (t1
);
723 code2
= TREE_CODE (t2
);
725 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
726 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
727 || code1
== INTEGER_TYPE
);
728 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
729 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
730 || code2
== INTEGER_TYPE
);
732 /* When one operand is a decimal float type, the other operand cannot be
733 a generic float type or a complex type. We also disallow vector types
735 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
736 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
738 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
740 error ("can%'t mix operands of decimal float and vector types");
741 return error_mark_node
;
743 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
745 error ("can%'t mix operands of decimal float and complex types");
746 return error_mark_node
;
748 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
750 error ("can%'t mix operands of decimal float and other float types");
751 return error_mark_node
;
755 /* If one type is a vector type, return that type. (How the usual
756 arithmetic conversions apply to the vector types extension is not
757 precisely specified.) */
758 if (code1
== VECTOR_TYPE
)
761 if (code2
== VECTOR_TYPE
)
764 /* If one type is complex, form the common type of the non-complex
765 components, then make that complex. Use T1 or T2 if it is the
767 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
769 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
770 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
771 tree subtype
= c_common_type (subtype1
, subtype2
);
773 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
775 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
778 return build_complex_type (subtype
);
781 /* If only one is real, use it as the result. */
783 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
786 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
789 /* If both are real and either are decimal floating point types, use
790 the decimal floating point type with the greater precision. */
792 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
794 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
795 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
796 return dfloat128_type_node
;
797 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
798 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
799 return dfloat64_type_node
;
800 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
801 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
802 return dfloat32_type_node
;
805 /* Deal with fixed-point types. */
806 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
808 unsigned int unsignedp
= 0, satp
= 0;
809 enum machine_mode m1
, m2
;
810 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
815 /* If one input type is saturating, the result type is saturating. */
816 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
819 /* If both fixed-point types are unsigned, the result type is unsigned.
820 When mixing fixed-point and integer types, follow the sign of the
822 Otherwise, the result type is signed. */
823 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
824 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
825 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
826 && TYPE_UNSIGNED (t1
))
827 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
828 && TYPE_UNSIGNED (t2
)))
831 /* The result type is signed. */
834 /* If the input type is unsigned, we need to convert to the
836 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
838 enum mode_class mclass
= (enum mode_class
) 0;
839 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
841 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
845 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
847 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
849 enum mode_class mclass
= (enum mode_class
) 0;
850 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
852 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
856 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
860 if (code1
== FIXED_POINT_TYPE
)
862 fbit1
= GET_MODE_FBIT (m1
);
863 ibit1
= GET_MODE_IBIT (m1
);
868 /* Signed integers need to subtract one sign bit. */
869 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
872 if (code2
== FIXED_POINT_TYPE
)
874 fbit2
= GET_MODE_FBIT (m2
);
875 ibit2
= GET_MODE_IBIT (m2
);
880 /* Signed integers need to subtract one sign bit. */
881 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
884 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
885 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
886 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
890 /* Both real or both integers; use the one with greater precision. */
892 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
894 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
897 /* Same precision. Prefer long longs to longs to ints when the
898 same precision, following the C99 rules on integer type rank
899 (which are equivalent to the C90 rules for C90 types). */
901 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
902 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
903 return long_long_unsigned_type_node
;
905 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
906 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
908 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
909 return long_long_unsigned_type_node
;
911 return long_long_integer_type_node
;
914 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
915 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
916 return long_unsigned_type_node
;
918 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
919 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
921 /* But preserve unsignedness from the other type,
922 since long cannot hold all the values of an unsigned int. */
923 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
924 return long_unsigned_type_node
;
926 return long_integer_type_node
;
929 /* Likewise, prefer long double to double even if same size. */
930 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
931 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
932 return long_double_type_node
;
934 /* Otherwise prefer the unsigned one. */
936 if (TYPE_UNSIGNED (t1
))
942 /* Wrapper around c_common_type that is used by c-common.c and other
943 front end optimizations that remove promotions. ENUMERAL_TYPEs
944 are allowed here and are converted to their compatible integer types.
945 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
946 preferably a non-Boolean type as the common type. */
948 common_type (tree t1
, tree t2
)
950 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
951 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
952 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
953 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
955 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
956 if (TREE_CODE (t1
) == BOOLEAN_TYPE
957 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
958 return boolean_type_node
;
960 /* If either type is BOOLEAN_TYPE, then return the other. */
961 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
963 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
966 return c_common_type (t1
, t2
);
969 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
970 or various other operations. Return 2 if they are compatible
971 but a warning may be needed if you use them together. */
974 comptypes (tree type1
, tree type2
)
976 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
979 val
= comptypes_internal (type1
, type2
, NULL
);
980 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
985 /* Like comptypes, but if it returns non-zero because enum and int are
986 compatible, it sets *ENUM_AND_INT_P to true. */
989 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
991 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
994 val
= comptypes_internal (type1
, type2
, enum_and_int_p
);
995 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1000 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1001 or various other operations. Return 2 if they are compatible
1002 but a warning may be needed if you use them together. If
1003 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1004 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1005 *ENUM_AND_INT_P is never set to false. This differs from
1006 comptypes, in that we don't free the seen types. */
1009 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
)
1011 const_tree t1
= type1
;
1012 const_tree t2
= type2
;
1015 /* Suppress errors caused by previously reported errors. */
1017 if (t1
== t2
|| !t1
|| !t2
1018 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1021 /* If either type is the internal version of sizetype, return the
1022 language version. */
1023 if (TREE_CODE (t1
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t1
)
1024 && TYPE_ORIG_SIZE_TYPE (t1
))
1025 t1
= TYPE_ORIG_SIZE_TYPE (t1
);
1027 if (TREE_CODE (t2
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t2
)
1028 && TYPE_ORIG_SIZE_TYPE (t2
))
1029 t2
= TYPE_ORIG_SIZE_TYPE (t2
);
1032 /* Enumerated types are compatible with integer types, but this is
1033 not transitive: two enumerated types in the same translation unit
1034 are compatible with each other only if they are the same type. */
1036 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1038 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1039 if (enum_and_int_p
!= NULL
&& TREE_CODE (t2
) != VOID_TYPE
)
1040 *enum_and_int_p
= true;
1042 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1044 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1045 if (enum_and_int_p
!= NULL
&& TREE_CODE (t1
) != VOID_TYPE
)
1046 *enum_and_int_p
= true;
1052 /* Different classes of types can't be compatible. */
1054 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1057 /* Qualifiers must match. C99 6.7.3p9 */
1059 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1062 /* Allow for two different type nodes which have essentially the same
1063 definition. Note that we already checked for equality of the type
1064 qualifiers (just above). */
1066 if (TREE_CODE (t1
) != ARRAY_TYPE
1067 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1070 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1071 if (!(attrval
= targetm
.comp_type_attributes (t1
, t2
)))
1074 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1077 switch (TREE_CODE (t1
))
1080 /* Do not remove mode or aliasing information. */
1081 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1082 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
))
1084 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1085 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1090 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
);
1095 tree d1
= TYPE_DOMAIN (t1
);
1096 tree d2
= TYPE_DOMAIN (t2
);
1097 bool d1_variable
, d2_variable
;
1098 bool d1_zero
, d2_zero
;
1101 /* Target types must match incl. qualifiers. */
1102 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1103 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1107 /* Sizes must match unless one is missing or variable. */
1108 if (d1
== 0 || d2
== 0 || d1
== d2
)
1111 d1_zero
= !TYPE_MAX_VALUE (d1
);
1112 d2_zero
= !TYPE_MAX_VALUE (d2
);
1114 d1_variable
= (!d1_zero
1115 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1116 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1117 d2_variable
= (!d2_zero
1118 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1119 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1120 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1121 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1123 if (d1_variable
|| d2_variable
)
1125 if (d1_zero
&& d2_zero
)
1127 if (d1_zero
|| d2_zero
1128 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1129 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1138 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1140 tree a1
= TYPE_ATTRIBUTES (t1
);
1141 tree a2
= TYPE_ATTRIBUTES (t2
);
1143 if (! attribute_list_contained (a1
, a2
)
1144 && ! attribute_list_contained (a2
, a1
))
1148 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
);
1149 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
);
1154 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1155 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1162 return attrval
== 2 && val
== 1 ? 2 : val
;
1165 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1166 their qualifiers, except for named address spaces. If the pointers point to
1167 different named addresses, then we must determine if one address space is a
1168 subset of the other. */
1171 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1174 tree mvl
= TREE_TYPE (ttl
);
1175 tree mvr
= TREE_TYPE (ttr
);
1176 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1177 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1178 addr_space_t as_common
;
1179 bool enum_and_int_p
;
1181 /* Fail if pointers point to incompatible address spaces. */
1182 if (!addr_space_superset (asl
, asr
, &as_common
))
1185 /* Do not lose qualifiers on element types of array types that are
1186 pointer targets by taking their TYPE_MAIN_VARIANT. */
1187 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
1188 mvl
= TYPE_MAIN_VARIANT (mvl
);
1189 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
1190 mvr
= TYPE_MAIN_VARIANT (mvr
);
1191 enum_and_int_p
= false;
1192 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1195 pedwarn (location
, OPT_pedantic
, "types are not quite compatible");
1197 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1198 warning_at (location
, OPT_Wc___compat
,
1199 "pointer target types incompatible in C++");
1204 /* Subroutines of `comptypes'. */
1206 /* Determine whether two trees derive from the same translation unit.
1207 If the CONTEXT chain ends in a null, that tree's context is still
1208 being parsed, so if two trees have context chains ending in null,
1209 they're in the same translation unit. */
1211 same_translation_unit_p (const_tree t1
, const_tree t2
)
1213 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1214 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1216 case tcc_declaration
:
1217 t1
= DECL_CONTEXT (t1
); break;
1219 t1
= TYPE_CONTEXT (t1
); break;
1220 case tcc_exceptional
:
1221 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1222 default: gcc_unreachable ();
1225 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1226 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1228 case tcc_declaration
:
1229 t2
= DECL_CONTEXT (t2
); break;
1231 t2
= TYPE_CONTEXT (t2
); break;
1232 case tcc_exceptional
:
1233 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1234 default: gcc_unreachable ();
1240 /* Allocate the seen two types, assuming that they are compatible. */
1242 static struct tagged_tu_seen_cache
*
1243 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1245 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1246 tu
->next
= tagged_tu_seen_base
;
1250 tagged_tu_seen_base
= tu
;
1252 /* The C standard says that two structures in different translation
1253 units are compatible with each other only if the types of their
1254 fields are compatible (among other things). We assume that they
1255 are compatible until proven otherwise when building the cache.
1256 An example where this can occur is:
1261 If we are comparing this against a similar struct in another TU,
1262 and did not assume they were compatible, we end up with an infinite
1268 /* Free the seen types until we get to TU_TIL. */
1271 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1273 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1274 while (tu
!= tu_til
)
1276 const struct tagged_tu_seen_cache
*const tu1
1277 = (const struct tagged_tu_seen_cache
*) tu
;
1279 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1281 tagged_tu_seen_base
= tu_til
;
1284 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1285 compatible. If the two types are not the same (which has been
1286 checked earlier), this can only happen when multiple translation
1287 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1288 rules. ENUM_AND_INT_P is as in comptypes_internal. */
1291 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1292 bool *enum_and_int_p
)
1295 bool needs_warning
= false;
1297 /* We have to verify that the tags of the types are the same. This
1298 is harder than it looks because this may be a typedef, so we have
1299 to go look at the original type. It may even be a typedef of a
1301 In the case of compiler-created builtin structs the TYPE_DECL
1302 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1303 while (TYPE_NAME (t1
)
1304 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1305 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1306 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1308 while (TYPE_NAME (t2
)
1309 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1310 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1311 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1313 /* C90 didn't have the requirement that the two tags be the same. */
1314 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1317 /* C90 didn't say what happened if one or both of the types were
1318 incomplete; we choose to follow C99 rules here, which is that they
1320 if (TYPE_SIZE (t1
) == NULL
1321 || TYPE_SIZE (t2
) == NULL
)
1325 const struct tagged_tu_seen_cache
* tts_i
;
1326 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1327 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1331 switch (TREE_CODE (t1
))
1335 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1336 /* Speed up the case where the type values are in the same order. */
1337 tree tv1
= TYPE_VALUES (t1
);
1338 tree tv2
= TYPE_VALUES (t2
);
1345 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1347 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1349 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1356 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1360 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1366 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1372 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1374 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1376 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1387 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1388 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1394 /* Speed up the common case where the fields are in the same order. */
1395 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1396 s1
= TREE_CHAIN (s1
), s2
= TREE_CHAIN (s2
))
1400 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1402 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1405 if (result
!= 1 && !DECL_NAME (s1
))
1413 needs_warning
= true;
1415 if (TREE_CODE (s1
) == FIELD_DECL
1416 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1417 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1425 tu
->val
= needs_warning
? 2 : 1;
1429 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= TREE_CHAIN (s1
))
1433 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= TREE_CHAIN (s2
))
1434 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1438 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1441 if (result
!= 1 && !DECL_NAME (s1
))
1449 needs_warning
= true;
1451 if (TREE_CODE (s1
) == FIELD_DECL
1452 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1453 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1465 tu
->val
= needs_warning
? 2 : 10;
1471 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1473 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1475 s1
= TREE_CHAIN (s1
), s2
= TREE_CHAIN (s2
))
1478 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1479 || DECL_NAME (s1
) != DECL_NAME (s2
))
1481 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1486 needs_warning
= true;
1488 if (TREE_CODE (s1
) == FIELD_DECL
1489 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1490 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1496 tu
->val
= needs_warning
? 2 : 1;
1505 /* Return 1 if two function types F1 and F2 are compatible.
1506 If either type specifies no argument types,
1507 the other must specify a fixed number of self-promoting arg types.
1508 Otherwise, if one type specifies only the number of arguments,
1509 the other must specify that number of self-promoting arg types.
1510 Otherwise, the argument types must match.
1511 ENUM_AND_INT_P is as in comptypes_internal. */
1514 function_types_compatible_p (const_tree f1
, const_tree f2
,
1515 bool *enum_and_int_p
)
1518 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1523 ret1
= TREE_TYPE (f1
);
1524 ret2
= TREE_TYPE (f2
);
1526 /* 'volatile' qualifiers on a function's return type used to mean
1527 the function is noreturn. */
1528 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1529 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1530 if (TYPE_VOLATILE (ret1
))
1531 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1532 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1533 if (TYPE_VOLATILE (ret2
))
1534 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1535 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1536 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
);
1540 args1
= TYPE_ARG_TYPES (f1
);
1541 args2
= TYPE_ARG_TYPES (f2
);
1543 /* An unspecified parmlist matches any specified parmlist
1544 whose argument types don't need default promotions. */
1548 if (!self_promoting_args_p (args2
))
1550 /* If one of these types comes from a non-prototype fn definition,
1551 compare that with the other type's arglist.
1552 If they don't match, ask for a warning (but no error). */
1553 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1554 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1561 if (!self_promoting_args_p (args1
))
1563 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1564 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1570 /* Both types have argument lists: compare them and propagate results. */
1571 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
);
1572 return val1
!= 1 ? val1
: val
;
1575 /* Check two lists of types for compatibility, returning 0 for
1576 incompatible, 1 for compatible, or 2 for compatible with
1577 warning. ENUM_AND_INT_P is as in comptypes_internal. */
1580 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1581 bool *enum_and_int_p
)
1583 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1589 tree a1
, mv1
, a2
, mv2
;
1590 if (args1
== 0 && args2
== 0)
1592 /* If one list is shorter than the other,
1593 they fail to match. */
1594 if (args1
== 0 || args2
== 0)
1596 mv1
= a1
= TREE_VALUE (args1
);
1597 mv2
= a2
= TREE_VALUE (args2
);
1598 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1599 mv1
= TYPE_MAIN_VARIANT (mv1
);
1600 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1601 mv2
= TYPE_MAIN_VARIANT (mv2
);
1602 /* A null pointer instead of a type
1603 means there is supposed to be an argument
1604 but nothing is specified about what type it has.
1605 So match anything that self-promotes. */
1608 if (c_type_promotes_to (a2
) != a2
)
1613 if (c_type_promotes_to (a1
) != a1
)
1616 /* If one of the lists has an error marker, ignore this arg. */
1617 else if (TREE_CODE (a1
) == ERROR_MARK
1618 || TREE_CODE (a2
) == ERROR_MARK
)
1620 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
)))
1622 /* Allow wait (union {union wait *u; int *i} *)
1623 and wait (union wait *) to be compatible. */
1624 if (TREE_CODE (a1
) == UNION_TYPE
1625 && (TYPE_NAME (a1
) == 0
1626 || TYPE_TRANSPARENT_AGGR (a1
))
1627 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1628 && tree_int_cst_equal (TYPE_SIZE (a1
),
1632 for (memb
= TYPE_FIELDS (a1
);
1633 memb
; memb
= TREE_CHAIN (memb
))
1635 tree mv3
= TREE_TYPE (memb
);
1636 if (mv3
&& mv3
!= error_mark_node
1637 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1638 mv3
= TYPE_MAIN_VARIANT (mv3
);
1639 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
))
1645 else if (TREE_CODE (a2
) == UNION_TYPE
1646 && (TYPE_NAME (a2
) == 0
1647 || TYPE_TRANSPARENT_AGGR (a2
))
1648 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1649 && tree_int_cst_equal (TYPE_SIZE (a2
),
1653 for (memb
= TYPE_FIELDS (a2
);
1654 memb
; memb
= TREE_CHAIN (memb
))
1656 tree mv3
= TREE_TYPE (memb
);
1657 if (mv3
&& mv3
!= error_mark_node
1658 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1659 mv3
= TYPE_MAIN_VARIANT (mv3
);
1660 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
))
1670 /* comptypes said ok, but record if it said to warn. */
1674 args1
= TREE_CHAIN (args1
);
1675 args2
= TREE_CHAIN (args2
);
1679 /* Compute the size to increment a pointer by. */
1682 c_size_in_bytes (const_tree type
)
1684 enum tree_code code
= TREE_CODE (type
);
1686 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
)
1687 return size_one_node
;
1689 if (!COMPLETE_OR_VOID_TYPE_P (type
))
1691 error ("arithmetic on pointer to an incomplete type");
1692 return size_one_node
;
1695 /* Convert in case a char is more than one unit. */
1696 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1697 size_int (TYPE_PRECISION (char_type_node
)
1701 /* Return either DECL or its known constant value (if it has one). */
1704 decl_constant_value (tree decl
)
1706 if (/* Don't change a variable array bound or initial value to a constant
1707 in a place where a variable is invalid. Note that DECL_INITIAL
1708 isn't valid for a PARM_DECL. */
1709 current_function_decl
!= 0
1710 && TREE_CODE (decl
) != PARM_DECL
1711 && !TREE_THIS_VOLATILE (decl
)
1712 && TREE_READONLY (decl
)
1713 && DECL_INITIAL (decl
) != 0
1714 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1715 /* This is invalid if initial value is not constant.
1716 If it has either a function call, a memory reference,
1717 or a variable, then re-evaluating it could give different results. */
1718 && TREE_CONSTANT (DECL_INITIAL (decl
))
1719 /* Check for cases where this is sub-optimal, even though valid. */
1720 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1721 return DECL_INITIAL (decl
);
1725 /* Convert the array expression EXP to a pointer. */
1727 array_to_pointer_conversion (location_t loc
, tree exp
)
1729 tree orig_exp
= exp
;
1730 tree type
= TREE_TYPE (exp
);
1732 tree restype
= TREE_TYPE (type
);
1735 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1737 STRIP_TYPE_NOPS (exp
);
1739 if (TREE_NO_WARNING (orig_exp
))
1740 TREE_NO_WARNING (exp
) = 1;
1742 ptrtype
= build_pointer_type (restype
);
1744 if (TREE_CODE (exp
) == INDIRECT_REF
)
1745 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1747 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, 1);
1748 return convert (ptrtype
, adr
);
1751 /* Convert the function expression EXP to a pointer. */
1753 function_to_pointer_conversion (location_t loc
, tree exp
)
1755 tree orig_exp
= exp
;
1757 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1759 STRIP_TYPE_NOPS (exp
);
1761 if (TREE_NO_WARNING (orig_exp
))
1762 TREE_NO_WARNING (exp
) = 1;
1764 return build_unary_op (loc
, ADDR_EXPR
, exp
, 0);
1767 /* Mark EXP as read, not just set, for set but not used -Wunused
1768 warning purposes. */
1771 mark_exp_read (tree exp
)
1773 switch (TREE_CODE (exp
))
1777 DECL_READ_P (exp
) = 1;
1786 mark_exp_read (TREE_OPERAND (exp
, 0));
1789 mark_exp_read (TREE_OPERAND (exp
, 1));
1796 /* Perform the default conversion of arrays and functions to pointers.
1797 Return the result of converting EXP. For any other expression, just
1800 LOC is the location of the expression. */
1803 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1805 tree orig_exp
= exp
.value
;
1806 tree type
= TREE_TYPE (exp
.value
);
1807 enum tree_code code
= TREE_CODE (type
);
1813 bool not_lvalue
= false;
1814 bool lvalue_array_p
;
1816 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1817 || CONVERT_EXPR_P (exp
.value
))
1818 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1820 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1822 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1825 if (TREE_NO_WARNING (orig_exp
))
1826 TREE_NO_WARNING (exp
.value
) = 1;
1828 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1829 if (!flag_isoc99
&& !lvalue_array_p
)
1831 /* Before C99, non-lvalue arrays do not decay to pointers.
1832 Normally, using such an array would be invalid; but it can
1833 be used correctly inside sizeof or as a statement expression.
1834 Thus, do not give an error here; an error will result later. */
1838 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1842 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1852 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1854 mark_exp_read (exp
.value
);
1855 return default_function_array_conversion (loc
, exp
);
1858 /* EXP is an expression of integer type. Apply the integer promotions
1859 to it and return the promoted value. */
1862 perform_integral_promotions (tree exp
)
1864 tree type
= TREE_TYPE (exp
);
1865 enum tree_code code
= TREE_CODE (type
);
1867 gcc_assert (INTEGRAL_TYPE_P (type
));
1869 /* Normally convert enums to int,
1870 but convert wide enums to something wider. */
1871 if (code
== ENUMERAL_TYPE
)
1873 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
1874 TYPE_PRECISION (integer_type_node
)),
1875 ((TYPE_PRECISION (type
)
1876 >= TYPE_PRECISION (integer_type_node
))
1877 && TYPE_UNSIGNED (type
)));
1879 return convert (type
, exp
);
1882 /* ??? This should no longer be needed now bit-fields have their
1884 if (TREE_CODE (exp
) == COMPONENT_REF
1885 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
1886 /* If it's thinner than an int, promote it like a
1887 c_promoting_integer_type_p, otherwise leave it alone. */
1888 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
1889 TYPE_PRECISION (integer_type_node
)))
1890 return convert (integer_type_node
, exp
);
1892 if (c_promoting_integer_type_p (type
))
1894 /* Preserve unsignedness if not really getting any wider. */
1895 if (TYPE_UNSIGNED (type
)
1896 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1897 return convert (unsigned_type_node
, exp
);
1899 return convert (integer_type_node
, exp
);
1906 /* Perform default promotions for C data used in expressions.
1907 Enumeral types or short or char are converted to int.
1908 In addition, manifest constants symbols are replaced by their values. */
1911 default_conversion (tree exp
)
1914 tree type
= TREE_TYPE (exp
);
1915 enum tree_code code
= TREE_CODE (type
);
1918 mark_exp_read (exp
);
1920 /* Functions and arrays have been converted during parsing. */
1921 gcc_assert (code
!= FUNCTION_TYPE
);
1922 if (code
== ARRAY_TYPE
)
1925 /* Constants can be used directly unless they're not loadable. */
1926 if (TREE_CODE (exp
) == CONST_DECL
)
1927 exp
= DECL_INITIAL (exp
);
1929 /* Strip no-op conversions. */
1931 STRIP_TYPE_NOPS (exp
);
1933 if (TREE_NO_WARNING (orig_exp
))
1934 TREE_NO_WARNING (exp
) = 1;
1936 if (code
== VOID_TYPE
)
1938 error ("void value not ignored as it ought to be");
1939 return error_mark_node
;
1942 exp
= require_complete_type (exp
);
1943 if (exp
== error_mark_node
)
1944 return error_mark_node
;
1946 promoted_type
= targetm
.promoted_type (type
);
1948 return convert (promoted_type
, exp
);
1950 if (INTEGRAL_TYPE_P (type
))
1951 return perform_integral_promotions (exp
);
1956 /* Look up COMPONENT in a structure or union DECL.
1958 If the component name is not found, returns NULL_TREE. Otherwise,
1959 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1960 stepping down the chain to the component, which is in the last
1961 TREE_VALUE of the list. Normally the list is of length one, but if
1962 the component is embedded within (nested) anonymous structures or
1963 unions, the list steps down the chain to the component. */
1966 lookup_field (tree decl
, tree component
)
1968 tree type
= TREE_TYPE (decl
);
1971 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1972 to the field elements. Use a binary search on this array to quickly
1973 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1974 will always be set for structures which have many elements. */
1976 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
1979 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
1981 field
= TYPE_FIELDS (type
);
1983 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
1984 while (top
- bot
> 1)
1986 half
= (top
- bot
+ 1) >> 1;
1987 field
= field_array
[bot
+half
];
1989 if (DECL_NAME (field
) == NULL_TREE
)
1991 /* Step through all anon unions in linear fashion. */
1992 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
1994 field
= field_array
[bot
++];
1995 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
1996 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1998 tree anon
= lookup_field (field
, component
);
2001 return tree_cons (NULL_TREE
, field
, anon
);
2005 /* Entire record is only anon unions. */
2009 /* Restart the binary search, with new lower bound. */
2013 if (DECL_NAME (field
) == component
)
2015 if (DECL_NAME (field
) < component
)
2021 if (DECL_NAME (field_array
[bot
]) == component
)
2022 field
= field_array
[bot
];
2023 else if (DECL_NAME (field
) != component
)
2028 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
2030 if (DECL_NAME (field
) == NULL_TREE
2031 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2032 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
2034 tree anon
= lookup_field (field
, component
);
2037 return tree_cons (NULL_TREE
, field
, anon
);
2040 if (DECL_NAME (field
) == component
)
2044 if (field
== NULL_TREE
)
2048 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2051 /* Make an expression to refer to the COMPONENT field of structure or
2052 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2053 location of the COMPONENT_REF. */
2056 build_component_ref (location_t loc
, tree datum
, tree component
)
2058 tree type
= TREE_TYPE (datum
);
2059 enum tree_code code
= TREE_CODE (type
);
2062 bool datum_lvalue
= lvalue_p (datum
);
2064 if (!objc_is_public (datum
, component
))
2065 return error_mark_node
;
2067 /* See if there is a field or component with name COMPONENT. */
2069 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2071 if (!COMPLETE_TYPE_P (type
))
2073 c_incomplete_type_error (NULL_TREE
, type
);
2074 return error_mark_node
;
2077 field
= lookup_field (datum
, component
);
2081 error_at (loc
, "%qT has no member named %qE", type
, component
);
2082 return error_mark_node
;
2085 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2086 This might be better solved in future the way the C++ front
2087 end does it - by giving the anonymous entities each a
2088 separate name and type, and then have build_component_ref
2089 recursively call itself. We can't do that here. */
2092 tree subdatum
= TREE_VALUE (field
);
2095 bool use_datum_quals
;
2097 if (TREE_TYPE (subdatum
) == error_mark_node
)
2098 return error_mark_node
;
2100 /* If this is an rvalue, it does not have qualifiers in C
2101 standard terms and we must avoid propagating such
2102 qualifiers down to a non-lvalue array that is then
2103 converted to a pointer. */
2104 use_datum_quals
= (datum_lvalue
2105 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2107 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2108 if (use_datum_quals
)
2109 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2110 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2112 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2114 SET_EXPR_LOCATION (ref
, loc
);
2115 if (TREE_READONLY (subdatum
)
2116 || (use_datum_quals
&& TREE_READONLY (datum
)))
2117 TREE_READONLY (ref
) = 1;
2118 if (TREE_THIS_VOLATILE (subdatum
)
2119 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2120 TREE_THIS_VOLATILE (ref
) = 1;
2122 if (TREE_DEPRECATED (subdatum
))
2123 warn_deprecated_use (subdatum
, NULL_TREE
);
2127 field
= TREE_CHAIN (field
);
2133 else if (code
!= ERROR_MARK
)
2135 "request for member %qE in something not a structure or union",
2138 return error_mark_node
;
2141 /* Given an expression PTR for a pointer, return an expression
2142 for the value pointed to.
2143 ERRORSTRING is the name of the operator to appear in error messages.
2145 LOC is the location to use for the generated tree. */
2148 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2150 tree pointer
= default_conversion (ptr
);
2151 tree type
= TREE_TYPE (pointer
);
2154 if (TREE_CODE (type
) == POINTER_TYPE
)
2156 if (CONVERT_EXPR_P (pointer
)
2157 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2159 /* If a warning is issued, mark it to avoid duplicates from
2160 the backend. This only needs to be done at
2161 warn_strict_aliasing > 2. */
2162 if (warn_strict_aliasing
> 2)
2163 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2164 type
, TREE_OPERAND (pointer
, 0)))
2165 TREE_NO_WARNING (pointer
) = 1;
2168 if (TREE_CODE (pointer
) == ADDR_EXPR
2169 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2170 == TREE_TYPE (type
)))
2172 ref
= TREE_OPERAND (pointer
, 0);
2173 protected_set_expr_location (ref
, loc
);
2178 tree t
= TREE_TYPE (type
);
2180 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2182 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2184 error_at (loc
, "dereferencing pointer to incomplete type");
2185 return error_mark_node
;
2187 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2188 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2190 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2191 so that we get the proper error message if the result is used
2192 to assign to. Also, &* is supposed to be a no-op.
2193 And ANSI C seems to specify that the type of the result
2194 should be the const type. */
2195 /* A de-reference of a pointer to const is not a const. It is valid
2196 to change it via some other pointer. */
2197 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2198 TREE_SIDE_EFFECTS (ref
)
2199 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2200 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2201 protected_set_expr_location (ref
, loc
);
2205 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2208 case RO_ARRAY_INDEXING
:
2210 "invalid type argument of array indexing (have %qT)",
2215 "invalid type argument of unary %<*%> (have %qT)",
2220 "invalid type argument of %<->%> (have %qT)",
2226 return error_mark_node
;
2229 /* This handles expressions of the form "a[i]", which denotes
2232 This is logically equivalent in C to *(a+i), but we may do it differently.
2233 If A is a variable or a member, we generate a primitive ARRAY_REF.
2234 This avoids forcing the array out of registers, and can work on
2235 arrays that are not lvalues (for example, members of structures returned
2238 LOC is the location to use for the returned expression. */
2241 build_array_ref (location_t loc
, tree array
, tree index
)
2244 bool swapped
= false;
2245 if (TREE_TYPE (array
) == error_mark_node
2246 || TREE_TYPE (index
) == error_mark_node
)
2247 return error_mark_node
;
2249 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2250 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
)
2253 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2254 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2256 error_at (loc
, "subscripted value is neither array nor pointer");
2257 return error_mark_node
;
2265 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2267 error_at (loc
, "array subscript is not an integer");
2268 return error_mark_node
;
2271 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2273 error_at (loc
, "subscripted value is pointer to function");
2274 return error_mark_node
;
2277 /* ??? Existing practice has been to warn only when the char
2278 index is syntactically the index, not for char[array]. */
2280 warn_array_subscript_with_type_char (index
);
2282 /* Apply default promotions *after* noticing character types. */
2283 index
= default_conversion (index
);
2285 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2287 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2291 /* An array that is indexed by a non-constant
2292 cannot be stored in a register; we must be able to do
2293 address arithmetic on its address.
2294 Likewise an array of elements of variable size. */
2295 if (TREE_CODE (index
) != INTEGER_CST
2296 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2297 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2299 if (!c_mark_addressable (array
))
2300 return error_mark_node
;
2302 /* An array that is indexed by a constant value which is not within
2303 the array bounds cannot be stored in a register either; because we
2304 would get a crash in store_bit_field/extract_bit_field when trying
2305 to access a non-existent part of the register. */
2306 if (TREE_CODE (index
) == INTEGER_CST
2307 && TYPE_DOMAIN (TREE_TYPE (array
))
2308 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2310 if (!c_mark_addressable (array
))
2311 return error_mark_node
;
2317 while (TREE_CODE (foo
) == COMPONENT_REF
)
2318 foo
= TREE_OPERAND (foo
, 0);
2319 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
2320 pedwarn (loc
, OPT_pedantic
,
2321 "ISO C forbids subscripting %<register%> array");
2322 else if (!flag_isoc99
&& !lvalue_p (foo
))
2323 pedwarn (loc
, OPT_pedantic
,
2324 "ISO C90 forbids subscripting non-lvalue array");
2327 type
= TREE_TYPE (TREE_TYPE (array
));
2328 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2329 /* Array ref is const/volatile if the array elements are
2330 or if the array is. */
2331 TREE_READONLY (rval
)
2332 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2333 | TREE_READONLY (array
));
2334 TREE_SIDE_EFFECTS (rval
)
2335 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2336 | TREE_SIDE_EFFECTS (array
));
2337 TREE_THIS_VOLATILE (rval
)
2338 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2339 /* This was added by rms on 16 Nov 91.
2340 It fixes vol struct foo *a; a->elts[1]
2341 in an inline function.
2342 Hope it doesn't break something else. */
2343 | TREE_THIS_VOLATILE (array
));
2344 ret
= require_complete_type (rval
);
2345 protected_set_expr_location (ret
, loc
);
2350 tree ar
= default_conversion (array
);
2352 if (ar
== error_mark_node
)
2355 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2356 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2358 return build_indirect_ref
2359 (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
, index
, 0),
2364 /* Build an external reference to identifier ID. FUN indicates
2365 whether this will be used for a function call. LOC is the source
2366 location of the identifier. This sets *TYPE to the type of the
2367 identifier, which is not the same as the type of the returned value
2368 for CONST_DECLs defined as enum constants. If the type of the
2369 identifier is not available, *TYPE is set to NULL. */
2371 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2374 tree decl
= lookup_name (id
);
2376 /* In Objective-C, an instance variable (ivar) may be preferred to
2377 whatever lookup_name() found. */
2378 decl
= objc_lookup_ivar (decl
, id
);
2381 if (decl
&& decl
!= error_mark_node
)
2384 *type
= TREE_TYPE (ref
);
2387 /* Implicit function declaration. */
2388 ref
= implicitly_declare (loc
, id
);
2389 else if (decl
== error_mark_node
)
2390 /* Don't complain about something that's already been
2391 complained about. */
2392 return error_mark_node
;
2395 undeclared_variable (loc
, id
);
2396 return error_mark_node
;
2399 if (TREE_TYPE (ref
) == error_mark_node
)
2400 return error_mark_node
;
2402 if (TREE_DEPRECATED (ref
))
2403 warn_deprecated_use (ref
, NULL_TREE
);
2405 /* Recursive call does not count as usage. */
2406 if (ref
!= current_function_decl
)
2408 TREE_USED (ref
) = 1;
2411 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2413 if (!in_sizeof
&& !in_typeof
)
2414 C_DECL_USED (ref
) = 1;
2415 else if (DECL_INITIAL (ref
) == 0
2416 && DECL_EXTERNAL (ref
)
2417 && !TREE_PUBLIC (ref
))
2418 record_maybe_used_decl (ref
);
2421 if (TREE_CODE (ref
) == CONST_DECL
)
2423 used_types_insert (TREE_TYPE (ref
));
2426 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2427 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2429 warning_at (loc
, OPT_Wc___compat
,
2430 ("enum constant defined in struct or union "
2431 "is not visible in C++"));
2432 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2435 ref
= DECL_INITIAL (ref
);
2436 TREE_CONSTANT (ref
) = 1;
2438 else if (current_function_decl
!= 0
2439 && !DECL_FILE_SCOPE_P (current_function_decl
)
2440 && (TREE_CODE (ref
) == VAR_DECL
2441 || TREE_CODE (ref
) == PARM_DECL
2442 || TREE_CODE (ref
) == FUNCTION_DECL
))
2444 tree context
= decl_function_context (ref
);
2446 if (context
!= 0 && context
!= current_function_decl
)
2447 DECL_NONLOCAL (ref
) = 1;
2449 /* C99 6.7.4p3: An inline definition of a function with external
2450 linkage ... shall not contain a reference to an identifier with
2451 internal linkage. */
2452 else if (current_function_decl
!= 0
2453 && DECL_DECLARED_INLINE_P (current_function_decl
)
2454 && DECL_EXTERNAL (current_function_decl
)
2455 && VAR_OR_FUNCTION_DECL_P (ref
)
2456 && (TREE_CODE (ref
) != VAR_DECL
|| TREE_STATIC (ref
))
2457 && ! TREE_PUBLIC (ref
)
2458 && DECL_CONTEXT (ref
) != current_function_decl
)
2459 record_inline_static (loc
, current_function_decl
, ref
,
2465 /* Record details of decls possibly used inside sizeof or typeof. */
2466 struct maybe_used_decl
2470 /* The level seen at (in_sizeof + in_typeof). */
2472 /* The next one at this level or above, or NULL. */
2473 struct maybe_used_decl
*next
;
2476 static struct maybe_used_decl
*maybe_used_decls
;
2478 /* Record that DECL, an undefined static function reference seen
2479 inside sizeof or typeof, might be used if the operand of sizeof is
2480 a VLA type or the operand of typeof is a variably modified
2484 record_maybe_used_decl (tree decl
)
2486 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2488 t
->level
= in_sizeof
+ in_typeof
;
2489 t
->next
= maybe_used_decls
;
2490 maybe_used_decls
= t
;
2493 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2494 USED is false, just discard them. If it is true, mark them used
2495 (if no longer inside sizeof or typeof) or move them to the next
2496 level up (if still inside sizeof or typeof). */
2499 pop_maybe_used (bool used
)
2501 struct maybe_used_decl
*p
= maybe_used_decls
;
2502 int cur_level
= in_sizeof
+ in_typeof
;
2503 while (p
&& p
->level
> cur_level
)
2508 C_DECL_USED (p
->decl
) = 1;
2510 p
->level
= cur_level
;
2514 if (!used
|| cur_level
== 0)
2515 maybe_used_decls
= p
;
2518 /* Return the result of sizeof applied to EXPR. */
2521 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2524 if (expr
.value
== error_mark_node
)
2526 ret
.value
= error_mark_node
;
2527 ret
.original_code
= ERROR_MARK
;
2528 ret
.original_type
= NULL
;
2529 pop_maybe_used (false);
2533 bool expr_const_operands
= true;
2534 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2535 &expr_const_operands
);
2536 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2537 ret
.original_code
= ERROR_MARK
;
2538 ret
.original_type
= NULL
;
2539 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2541 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2542 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2543 folded_expr
, ret
.value
);
2544 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2545 SET_EXPR_LOCATION (ret
.value
, loc
);
2547 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2552 /* Return the result of sizeof applied to T, a structure for the type
2553 name passed to sizeof (rather than the type itself). LOC is the
2554 location of the original expression. */
2557 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2561 tree type_expr
= NULL_TREE
;
2562 bool type_expr_const
= true;
2563 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2564 ret
.value
= c_sizeof (loc
, type
);
2565 ret
.original_code
= ERROR_MARK
;
2566 ret
.original_type
= NULL
;
2567 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2568 && c_vla_type_p (type
))
2570 /* If the type is a [*] array, it is a VLA but is represented as
2571 having a size of zero. In such a case we must ensure that
2572 the result of sizeof does not get folded to a constant by
2573 c_fully_fold, because if the size is evaluated the result is
2574 not constant and so constraints on zero or negative size
2575 arrays must not be applied when this sizeof call is inside
2576 another array declarator. */
2578 type_expr
= integer_zero_node
;
2579 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2580 type_expr
, ret
.value
);
2581 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2583 pop_maybe_used (type
!= error_mark_node
2584 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2588 /* Build a function call to function FUNCTION with parameters PARAMS.
2589 The function call is at LOC.
2590 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2591 TREE_VALUE of each node is a parameter-expression.
2592 FUNCTION's data type may be a function type or a pointer-to-function. */
2595 build_function_call (location_t loc
, tree function
, tree params
)
2600 vec
= VEC_alloc (tree
, gc
, list_length (params
));
2601 for (; params
; params
= TREE_CHAIN (params
))
2602 VEC_quick_push (tree
, vec
, TREE_VALUE (params
));
2603 ret
= build_function_call_vec (loc
, function
, vec
, NULL
);
2604 VEC_free (tree
, gc
, vec
);
2608 /* Build a function call to function FUNCTION with parameters PARAMS.
2609 ORIGTYPES, if not NULL, is a vector of types; each element is
2610 either NULL or the original type of the corresponding element in
2611 PARAMS. The original type may differ from TREE_TYPE of the
2612 parameter for enums. FUNCTION's data type may be a function type
2613 or pointer-to-function. This function changes the elements of
2617 build_function_call_vec (location_t loc
, tree function
, VEC(tree
,gc
) *params
,
2618 VEC(tree
,gc
) *origtypes
)
2620 tree fntype
, fundecl
= 0;
2621 tree name
= NULL_TREE
, result
;
2627 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2628 STRIP_TYPE_NOPS (function
);
2630 /* Convert anything with function type to a pointer-to-function. */
2631 if (TREE_CODE (function
) == FUNCTION_DECL
)
2633 /* Implement type-directed function overloading for builtins.
2634 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2635 handle all the type checking. The result is a complete expression
2636 that implements this function call. */
2637 tem
= resolve_overloaded_builtin (loc
, function
, params
);
2641 name
= DECL_NAME (function
);
2644 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
2645 function
= function_to_pointer_conversion (loc
, function
);
2647 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2648 expressions, like those used for ObjC messenger dispatches. */
2649 if (!VEC_empty (tree
, params
))
2650 function
= objc_rewrite_function_call (function
,
2651 VEC_index (tree
, params
, 0));
2653 function
= c_fully_fold (function
, false, NULL
);
2655 fntype
= TREE_TYPE (function
);
2657 if (TREE_CODE (fntype
) == ERROR_MARK
)
2658 return error_mark_node
;
2660 if (!(TREE_CODE (fntype
) == POINTER_TYPE
2661 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
2663 error_at (loc
, "called object %qE is not a function", function
);
2664 return error_mark_node
;
2667 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
2668 current_function_returns_abnormally
= 1;
2670 /* fntype now gets the type of function pointed to. */
2671 fntype
= TREE_TYPE (fntype
);
2673 /* Convert the parameters to the types declared in the
2674 function prototype, or apply default promotions. */
2676 nargs
= convert_arguments (TYPE_ARG_TYPES (fntype
), params
, origtypes
,
2679 return error_mark_node
;
2681 /* Check that the function is called through a compatible prototype.
2682 If it is not, replace the call by a trap, wrapped up in a compound
2683 expression if necessary. This has the nice side-effect to prevent
2684 the tree-inliner from generating invalid assignment trees which may
2685 blow up in the RTL expander later. */
2686 if (CONVERT_EXPR_P (function
)
2687 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
2688 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
2689 && !comptypes (fntype
, TREE_TYPE (tem
)))
2691 tree return_type
= TREE_TYPE (fntype
);
2692 tree trap
= build_function_call (loc
, built_in_decls
[BUILT_IN_TRAP
],
2696 /* This situation leads to run-time undefined behavior. We can't,
2697 therefore, simply error unless we can prove that all possible
2698 executions of the program must execute the code. */
2699 if (warning_at (loc
, 0, "function called through a non-compatible type"))
2700 /* We can, however, treat "undefined" any way we please.
2701 Call abort to encourage the user to fix the program. */
2702 inform (loc
, "if this code is reached, the program will abort");
2703 /* Before the abort, allow the function arguments to exit or
2705 for (i
= 0; i
< nargs
; i
++)
2706 trap
= build2 (COMPOUND_EXPR
, void_type_node
,
2707 VEC_index (tree
, params
, i
), trap
);
2709 if (VOID_TYPE_P (return_type
))
2711 if (TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
2713 "function with qualified void return type called");
2720 if (AGGREGATE_TYPE_P (return_type
))
2721 rhs
= build_compound_literal (loc
, return_type
,
2722 build_constructor (return_type
, 0),
2725 rhs
= fold_convert_loc (loc
, return_type
, integer_zero_node
);
2727 return require_complete_type (build2 (COMPOUND_EXPR
, return_type
,
2732 argarray
= VEC_address (tree
, params
);
2734 /* Check that arguments to builtin functions match the expectations. */
2736 && DECL_BUILT_IN (fundecl
)
2737 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
2738 && !check_builtin_function_arguments (fundecl
, nargs
, argarray
))
2739 return error_mark_node
;
2741 /* Check that the arguments to the function are valid. */
2742 check_function_arguments (TYPE_ATTRIBUTES (fntype
), nargs
, argarray
,
2743 TYPE_ARG_TYPES (fntype
));
2745 if (name
!= NULL_TREE
2746 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
2748 if (require_constant_value
)
2750 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
2751 function
, nargs
, argarray
);
2753 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
2754 function
, nargs
, argarray
);
2755 if (TREE_CODE (result
) == NOP_EXPR
2756 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
2757 STRIP_TYPE_NOPS (result
);
2760 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
2761 function
, nargs
, argarray
);
2763 if (VOID_TYPE_P (TREE_TYPE (result
)))
2765 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
2767 "function with qualified void return type called");
2770 return require_complete_type (result
);
2773 /* Convert the argument expressions in the vector VALUES
2774 to the types in the list TYPELIST.
2776 If TYPELIST is exhausted, or when an element has NULL as its type,
2777 perform the default conversions.
2779 ORIGTYPES is the original types of the expressions in VALUES. This
2780 holds the type of enum values which have been converted to integral
2781 types. It may be NULL.
2783 FUNCTION is a tree for the called function. It is used only for
2784 error messages, where it is formatted with %qE.
2786 This is also where warnings about wrong number of args are generated.
2788 Returns the actual number of arguments processed (which may be less
2789 than the length of VALUES in some error situations), or -1 on
2793 convert_arguments (tree typelist
, VEC(tree
,gc
) *values
,
2794 VEC(tree
,gc
) *origtypes
, tree function
, tree fundecl
)
2797 unsigned int parmnum
;
2798 bool error_args
= false;
2799 const bool type_generic
= fundecl
2800 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl
)));
2801 bool type_generic_remove_excess_precision
= false;
2804 /* Change pointer to function to the function itself for
2806 if (TREE_CODE (function
) == ADDR_EXPR
2807 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
2808 function
= TREE_OPERAND (function
, 0);
2810 /* Handle an ObjC selector specially for diagnostics. */
2811 selector
= objc_message_selector ();
2813 /* For type-generic built-in functions, determine whether excess
2814 precision should be removed (classification) or not
2817 && DECL_BUILT_IN (fundecl
)
2818 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
2820 switch (DECL_FUNCTION_CODE (fundecl
))
2822 case BUILT_IN_ISFINITE
:
2823 case BUILT_IN_ISINF
:
2824 case BUILT_IN_ISINF_SIGN
:
2825 case BUILT_IN_ISNAN
:
2826 case BUILT_IN_ISNORMAL
:
2827 case BUILT_IN_FPCLASSIFY
:
2828 type_generic_remove_excess_precision
= true;
2832 type_generic_remove_excess_precision
= false;
2837 /* Scan the given expressions and types, producing individual
2838 converted arguments. */
2840 for (typetail
= typelist
, parmnum
= 0;
2841 VEC_iterate (tree
, values
, parmnum
, val
);
2844 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
2845 tree valtype
= TREE_TYPE (val
);
2846 tree rname
= function
;
2847 int argnum
= parmnum
+ 1;
2848 const char *invalid_func_diag
;
2849 bool excess_precision
= false;
2853 if (type
== void_type_node
)
2855 error_at (input_location
,
2856 "too many arguments to function %qE", function
);
2857 if (fundecl
&& !DECL_BUILT_IN (fundecl
))
2858 inform (DECL_SOURCE_LOCATION (fundecl
), "declared here");
2862 if (selector
&& argnum
> 2)
2868 npc
= null_pointer_constant_p (val
);
2870 /* If there is excess precision and a prototype, convert once to
2871 the required type rather than converting via the semantic
2872 type. Likewise without a prototype a float value represented
2873 as long double should be converted once to double. But for
2874 type-generic classification functions excess precision must
2876 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
2877 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
2879 val
= TREE_OPERAND (val
, 0);
2880 excess_precision
= true;
2882 val
= c_fully_fold (val
, false, NULL
);
2883 STRIP_TYPE_NOPS (val
);
2885 val
= require_complete_type (val
);
2889 /* Formal parm type is specified by a function prototype. */
2891 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
2893 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
2900 /* Optionally warn about conversions that
2901 differ from the default conversions. */
2902 if (warn_traditional_conversion
|| warn_traditional
)
2904 unsigned int formal_prec
= TYPE_PRECISION (type
);
2906 if (INTEGRAL_TYPE_P (type
)
2907 && TREE_CODE (valtype
) == REAL_TYPE
)
2908 warning (0, "passing argument %d of %qE as integer "
2909 "rather than floating due to prototype",
2911 if (INTEGRAL_TYPE_P (type
)
2912 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
2913 warning (0, "passing argument %d of %qE as integer "
2914 "rather than complex due to prototype",
2916 else if (TREE_CODE (type
) == COMPLEX_TYPE
2917 && TREE_CODE (valtype
) == REAL_TYPE
)
2918 warning (0, "passing argument %d of %qE as complex "
2919 "rather than floating due to prototype",
2921 else if (TREE_CODE (type
) == REAL_TYPE
2922 && INTEGRAL_TYPE_P (valtype
))
2923 warning (0, "passing argument %d of %qE as floating "
2924 "rather than integer due to prototype",
2926 else if (TREE_CODE (type
) == COMPLEX_TYPE
2927 && INTEGRAL_TYPE_P (valtype
))
2928 warning (0, "passing argument %d of %qE as complex "
2929 "rather than integer due to prototype",
2931 else if (TREE_CODE (type
) == REAL_TYPE
2932 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
2933 warning (0, "passing argument %d of %qE as floating "
2934 "rather than complex due to prototype",
2936 /* ??? At some point, messages should be written about
2937 conversions between complex types, but that's too messy
2939 else if (TREE_CODE (type
) == REAL_TYPE
2940 && TREE_CODE (valtype
) == REAL_TYPE
)
2942 /* Warn if any argument is passed as `float',
2943 since without a prototype it would be `double'. */
2944 if (formal_prec
== TYPE_PRECISION (float_type_node
)
2945 && type
!= dfloat32_type_node
)
2946 warning (0, "passing argument %d of %qE as %<float%> "
2947 "rather than %<double%> due to prototype",
2950 /* Warn if mismatch between argument and prototype
2951 for decimal float types. Warn of conversions with
2952 binary float types and of precision narrowing due to
2954 else if (type
!= valtype
2955 && (type
== dfloat32_type_node
2956 || type
== dfloat64_type_node
2957 || type
== dfloat128_type_node
2958 || valtype
== dfloat32_type_node
2959 || valtype
== dfloat64_type_node
2960 || valtype
== dfloat128_type_node
)
2962 <= TYPE_PRECISION (valtype
)
2963 || (type
== dfloat128_type_node
2965 != dfloat64_type_node
2967 != dfloat32_type_node
)))
2968 || (type
== dfloat64_type_node
2970 != dfloat32_type_node
))))
2971 warning (0, "passing argument %d of %qE as %qT "
2972 "rather than %qT due to prototype",
2973 argnum
, rname
, type
, valtype
);
2976 /* Detect integer changing in width or signedness.
2977 These warnings are only activated with
2978 -Wtraditional-conversion, not with -Wtraditional. */
2979 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
2980 && INTEGRAL_TYPE_P (valtype
))
2982 tree would_have_been
= default_conversion (val
);
2983 tree type1
= TREE_TYPE (would_have_been
);
2985 if (TREE_CODE (type
) == ENUMERAL_TYPE
2986 && (TYPE_MAIN_VARIANT (type
)
2987 == TYPE_MAIN_VARIANT (valtype
)))
2988 /* No warning if function asks for enum
2989 and the actual arg is that enum type. */
2991 else if (formal_prec
!= TYPE_PRECISION (type1
))
2992 warning (OPT_Wtraditional_conversion
,
2993 "passing argument %d of %qE "
2994 "with different width due to prototype",
2996 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
2998 /* Don't complain if the formal parameter type
2999 is an enum, because we can't tell now whether
3000 the value was an enum--even the same enum. */
3001 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3003 else if (TREE_CODE (val
) == INTEGER_CST
3004 && int_fits_type_p (val
, type
))
3005 /* Change in signedness doesn't matter
3006 if a constant value is unaffected. */
3008 /* If the value is extended from a narrower
3009 unsigned type, it doesn't matter whether we
3010 pass it as signed or unsigned; the value
3011 certainly is the same either way. */
3012 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3013 && TYPE_UNSIGNED (valtype
))
3015 else if (TYPE_UNSIGNED (type
))
3016 warning (OPT_Wtraditional_conversion
,
3017 "passing argument %d of %qE "
3018 "as unsigned due to prototype",
3021 warning (OPT_Wtraditional_conversion
,
3022 "passing argument %d of %qE "
3023 "as signed due to prototype", argnum
, rname
);
3027 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3028 sake of better warnings from convert_and_check. */
3029 if (excess_precision
)
3030 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3031 origtype
= (origtypes
== NULL
3033 : VEC_index (tree
, origtypes
, parmnum
));
3034 parmval
= convert_for_assignment (input_location
, type
, val
,
3035 origtype
, ic_argpass
, npc
,
3039 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3040 && INTEGRAL_TYPE_P (type
)
3041 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3042 parmval
= default_conversion (parmval
);
3045 else if (TREE_CODE (valtype
) == REAL_TYPE
3046 && (TYPE_PRECISION (valtype
)
3047 < TYPE_PRECISION (double_type_node
))
3048 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3053 /* Convert `float' to `double'. */
3054 parmval
= convert (double_type_node
, val
);
3056 else if (excess_precision
&& !type_generic
)
3057 /* A "double" argument with excess precision being passed
3058 without a prototype or in variable arguments. */
3059 parmval
= convert (valtype
, val
);
3060 else if ((invalid_func_diag
=
3061 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3063 error (invalid_func_diag
);
3067 /* Convert `short' and `char' to full-size `int'. */
3068 parmval
= default_conversion (val
);
3070 VEC_replace (tree
, values
, parmnum
, parmval
);
3071 if (parmval
== error_mark_node
)
3075 typetail
= TREE_CHAIN (typetail
);
3078 gcc_assert (parmnum
== VEC_length (tree
, values
));
3080 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
3082 error_at (input_location
,
3083 "too few arguments to function %qE", function
);
3084 if (fundecl
&& !DECL_BUILT_IN (fundecl
))
3085 inform (DECL_SOURCE_LOCATION (fundecl
), "declared here");
3089 return error_args
? -1 : (int) parmnum
;
3092 /* This is the entry point used by the parser to build unary operators
3093 in the input. CODE, a tree_code, specifies the unary operator, and
3094 ARG is the operand. For unary plus, the C parser currently uses
3095 CONVERT_EXPR for code.
3097 LOC is the location to use for the tree generated.
3101 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3103 struct c_expr result
;
3105 result
.value
= build_unary_op (loc
, code
, arg
.value
, 0);
3106 result
.original_code
= code
;
3107 result
.original_type
= NULL
;
3109 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3110 overflow_warning (loc
, result
.value
);
3115 /* This is the entry point used by the parser to build binary operators
3116 in the input. CODE, a tree_code, specifies the binary operator, and
3117 ARG1 and ARG2 are the operands. In addition to constructing the
3118 expression, we check for operands that were written with other binary
3119 operators in a way that is likely to confuse the user.
3121 LOCATION is the location of the binary operator. */
3124 parser_build_binary_op (location_t location
, enum tree_code code
,
3125 struct c_expr arg1
, struct c_expr arg2
)
3127 struct c_expr result
;
3129 enum tree_code code1
= arg1
.original_code
;
3130 enum tree_code code2
= arg2
.original_code
;
3131 tree type1
= (arg1
.original_type
3132 ? arg1
.original_type
3133 : TREE_TYPE (arg1
.value
));
3134 tree type2
= (arg2
.original_type
3135 ? arg2
.original_type
3136 : TREE_TYPE (arg2
.value
));
3138 result
.value
= build_binary_op (location
, code
,
3139 arg1
.value
, arg2
.value
, 1);
3140 result
.original_code
= code
;
3141 result
.original_type
= NULL
;
3143 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3146 if (location
!= UNKNOWN_LOCATION
)
3147 protected_set_expr_location (result
.value
, location
);
3149 /* Check for cases such as x+y<<z which users are likely
3151 if (warn_parentheses
)
3152 warn_about_parentheses (code
, code1
, arg1
.value
, code2
, arg2
.value
);
3154 if (warn_logical_op
)
3155 warn_logical_operator (input_location
, code
, TREE_TYPE (result
.value
),
3156 code1
, arg1
.value
, code2
, arg2
.value
);
3158 /* Warn about comparisons against string literals, with the exception
3159 of testing for equality or inequality of a string literal with NULL. */
3160 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3162 if ((code1
== STRING_CST
&& !integer_zerop (arg2
.value
))
3163 || (code2
== STRING_CST
&& !integer_zerop (arg1
.value
)))
3164 warning_at (location
, OPT_Waddress
,
3165 "comparison with string literal results in unspecified behavior");
3167 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3168 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3169 warning_at (location
, OPT_Waddress
,
3170 "comparison with string literal results in unspecified behavior");
3172 if (TREE_OVERFLOW_P (result
.value
)
3173 && !TREE_OVERFLOW_P (arg1
.value
)
3174 && !TREE_OVERFLOW_P (arg2
.value
))
3175 overflow_warning (location
, result
.value
);
3177 /* Warn about comparisons of different enum types. */
3178 if (warn_enum_compare
3179 && TREE_CODE_CLASS (code
) == tcc_comparison
3180 && TREE_CODE (type1
) == ENUMERAL_TYPE
3181 && TREE_CODE (type2
) == ENUMERAL_TYPE
3182 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3183 warning_at (location
, OPT_Wenum_compare
,
3184 "comparison between %qT and %qT",
3190 /* Return a tree for the difference of pointers OP0 and OP1.
3191 The resulting tree has type int. */
3194 pointer_diff (location_t loc
, tree op0
, tree op1
)
3196 tree restype
= ptrdiff_type_node
;
3197 tree result
, inttype
;
3199 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3200 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3201 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3202 tree con0
, con1
, lit0
, lit1
;
3203 tree orig_op1
= op1
;
3205 /* If the operands point into different address spaces, we need to
3206 explicitly convert them to pointers into the common address space
3207 before we can subtract the numerical address values. */
3210 addr_space_t as_common
;
3213 /* Determine the common superset address space. This is guaranteed
3214 to exist because the caller verified that comp_target_types
3215 returned non-zero. */
3216 if (!addr_space_superset (as0
, as1
, &as_common
))
3219 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3220 op0
= convert (common_type
, op0
);
3221 op1
= convert (common_type
, op1
);
3224 /* Determine integer type to perform computations in. This will usually
3225 be the same as the result type (ptrdiff_t), but may need to be a wider
3226 type if pointers for the address space are wider than ptrdiff_t. */
3227 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3228 inttype
= lang_hooks
.types
.type_for_size
3229 (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3234 if (TREE_CODE (target_type
) == VOID_TYPE
)
3235 pedwarn (loc
, pedantic
? OPT_pedantic
: OPT_Wpointer_arith
,
3236 "pointer of type %<void *%> used in subtraction");
3237 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3238 pedwarn (loc
, pedantic
? OPT_pedantic
: OPT_Wpointer_arith
,
3239 "pointer to a function used in subtraction");
3241 /* If the conversion to ptrdiff_type does anything like widening or
3242 converting a partial to an integral mode, we get a convert_expression
3243 that is in the way to do any simplifications.
3244 (fold-const.c doesn't know that the extra bits won't be needed.
3245 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3246 different mode in place.)
3247 So first try to find a common term here 'by hand'; we want to cover
3248 at least the cases that occur in legal static initializers. */
3249 if (CONVERT_EXPR_P (op0
)
3250 && (TYPE_PRECISION (TREE_TYPE (op0
))
3251 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))))
3252 con0
= TREE_OPERAND (op0
, 0);
3255 if (CONVERT_EXPR_P (op1
)
3256 && (TYPE_PRECISION (TREE_TYPE (op1
))
3257 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))))
3258 con1
= TREE_OPERAND (op1
, 0);
3262 if (TREE_CODE (con0
) == PLUS_EXPR
)
3264 lit0
= TREE_OPERAND (con0
, 1);
3265 con0
= TREE_OPERAND (con0
, 0);
3268 lit0
= integer_zero_node
;
3270 if (TREE_CODE (con1
) == PLUS_EXPR
)
3272 lit1
= TREE_OPERAND (con1
, 1);
3273 con1
= TREE_OPERAND (con1
, 0);
3276 lit1
= integer_zero_node
;
3278 if (operand_equal_p (con0
, con1
, 0))
3285 /* First do the subtraction as integers;
3286 then drop through to build the divide operator.
3287 Do not do default conversions on the minus operator
3288 in case restype is a short type. */
3290 op0
= build_binary_op (loc
,
3291 MINUS_EXPR
, convert (inttype
, op0
),
3292 convert (inttype
, op1
), 0);
3293 /* This generates an error if op1 is pointer to incomplete type. */
3294 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3295 error_at (loc
, "arithmetic on pointer to an incomplete type");
3297 /* This generates an error if op0 is pointer to incomplete type. */
3298 op1
= c_size_in_bytes (target_type
);
3300 /* Divide by the size, in easiest possible way. */
3301 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3302 op0
, convert (inttype
, op1
));
3304 /* Convert to final result type if necessary. */
3305 return convert (restype
, result
);
3308 /* Construct and perhaps optimize a tree representation
3309 for a unary operation. CODE, a tree_code, specifies the operation
3310 and XARG is the operand.
3311 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3312 the default promotions (such as from short to int).
3313 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3314 allows non-lvalues; this is only used to handle conversion of non-lvalue
3315 arrays to pointers in C99.
3317 LOCATION is the location of the operator. */
3320 build_unary_op (location_t location
,
3321 enum tree_code code
, tree xarg
, int flag
)
3323 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3326 enum tree_code typecode
;
3328 tree ret
= error_mark_node
;
3329 tree eptype
= NULL_TREE
;
3330 int noconvert
= flag
;
3331 const char *invalid_op_diag
;
3334 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
3336 arg
= remove_c_maybe_const_expr (arg
);
3338 if (code
!= ADDR_EXPR
)
3339 arg
= require_complete_type (arg
);
3341 typecode
= TREE_CODE (TREE_TYPE (arg
));
3342 if (typecode
== ERROR_MARK
)
3343 return error_mark_node
;
3344 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
3345 typecode
= INTEGER_TYPE
;
3347 if ((invalid_op_diag
3348 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
3350 error_at (location
, invalid_op_diag
);
3351 return error_mark_node
;
3354 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
3356 eptype
= TREE_TYPE (arg
);
3357 arg
= TREE_OPERAND (arg
, 0);
3363 /* This is used for unary plus, because a CONVERT_EXPR
3364 is enough to prevent anybody from looking inside for
3365 associativity, but won't generate any code. */
3366 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3367 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3368 || typecode
== VECTOR_TYPE
))
3370 error_at (location
, "wrong type argument to unary plus");
3371 return error_mark_node
;
3373 else if (!noconvert
)
3374 arg
= default_conversion (arg
);
3375 arg
= non_lvalue_loc (location
, arg
);
3379 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3380 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3381 || typecode
== VECTOR_TYPE
))
3383 error_at (location
, "wrong type argument to unary minus");
3384 return error_mark_node
;
3386 else if (!noconvert
)
3387 arg
= default_conversion (arg
);
3391 /* ~ works on integer types and non float vectors. */
3392 if (typecode
== INTEGER_TYPE
3393 || (typecode
== VECTOR_TYPE
3394 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
3397 arg
= default_conversion (arg
);
3399 else if (typecode
== COMPLEX_TYPE
)
3402 pedwarn (location
, OPT_pedantic
,
3403 "ISO C does not support %<~%> for complex conjugation");
3405 arg
= default_conversion (arg
);
3409 error_at (location
, "wrong type argument to bit-complement");
3410 return error_mark_node
;
3415 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
3417 error_at (location
, "wrong type argument to abs");
3418 return error_mark_node
;
3420 else if (!noconvert
)
3421 arg
= default_conversion (arg
);
3425 /* Conjugating a real value is a no-op, but allow it anyway. */
3426 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3427 || typecode
== COMPLEX_TYPE
))
3429 error_at (location
, "wrong type argument to conjugation");
3430 return error_mark_node
;
3432 else if (!noconvert
)
3433 arg
= default_conversion (arg
);
3436 case TRUTH_NOT_EXPR
:
3437 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3438 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
3439 && typecode
!= COMPLEX_TYPE
)
3442 "wrong type argument to unary exclamation mark");
3443 return error_mark_node
;
3445 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
3446 ret
= invert_truthvalue_loc (location
, arg
);
3447 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3448 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
3449 location
= EXPR_LOCATION (ret
);
3450 goto return_build_unary_op
;
3453 if (TREE_CODE (arg
) == COMPLEX_CST
)
3454 ret
= TREE_REALPART (arg
);
3455 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
3456 ret
= fold_build1_loc (location
,
3457 REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
);
3460 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3461 eptype
= TREE_TYPE (eptype
);
3462 goto return_build_unary_op
;
3465 if (TREE_CODE (arg
) == COMPLEX_CST
)
3466 ret
= TREE_IMAGPART (arg
);
3467 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
3468 ret
= fold_build1_loc (location
,
3469 IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
);
3471 ret
= omit_one_operand_loc (location
, TREE_TYPE (arg
),
3472 integer_zero_node
, arg
);
3473 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3474 eptype
= TREE_TYPE (eptype
);
3475 goto return_build_unary_op
;
3477 case PREINCREMENT_EXPR
:
3478 case POSTINCREMENT_EXPR
:
3479 case PREDECREMENT_EXPR
:
3480 case POSTDECREMENT_EXPR
:
3482 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3484 tree inner
= build_unary_op (location
, code
,
3485 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3486 if (inner
== error_mark_node
)
3487 return error_mark_node
;
3488 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3489 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3490 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3491 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
3492 goto return_build_unary_op
;
3495 /* Complain about anything that is not a true lvalue. */
3496 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
3497 || code
== POSTINCREMENT_EXPR
)
3500 return error_mark_node
;
3502 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
3504 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3505 warning_at (location
, OPT_Wc___compat
,
3506 "increment of enumeration value is invalid in C++");
3508 warning_at (location
, OPT_Wc___compat
,
3509 "decrement of enumeration value is invalid in C++");
3512 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3513 arg
= c_fully_fold (arg
, false, NULL
);
3515 /* Increment or decrement the real part of the value,
3516 and don't change the imaginary part. */
3517 if (typecode
== COMPLEX_TYPE
)
3521 pedwarn (location
, OPT_pedantic
,
3522 "ISO C does not support %<++%> and %<--%> on complex types");
3524 arg
= stabilize_reference (arg
);
3525 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
, 1);
3526 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
, 1);
3527 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, 1);
3528 if (real
== error_mark_node
|| imag
== error_mark_node
)
3529 return error_mark_node
;
3530 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
3532 goto return_build_unary_op
;
3535 /* Report invalid types. */
3537 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3538 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
)
3540 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3541 error_at (location
, "wrong type argument to increment");
3543 error_at (location
, "wrong type argument to decrement");
3545 return error_mark_node
;
3551 argtype
= TREE_TYPE (arg
);
3553 /* Compute the increment. */
3555 if (typecode
== POINTER_TYPE
)
3557 /* If pointer target is an undefined struct,
3558 we just cannot know how to do the arithmetic. */
3559 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
3561 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3563 "increment of pointer to unknown structure");
3566 "decrement of pointer to unknown structure");
3568 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
3569 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
3571 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3572 pedwarn (location
, pedantic
? OPT_pedantic
: OPT_Wpointer_arith
,
3573 "wrong type argument to increment");
3575 pedwarn (location
, pedantic
? OPT_pedantic
: OPT_Wpointer_arith
,
3576 "wrong type argument to decrement");
3579 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
3580 inc
= fold_convert_loc (location
, sizetype
, inc
);
3582 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
3584 /* For signed fract types, we invert ++ to -- or
3585 -- to ++, and change inc from 1 to -1, because
3586 it is not possible to represent 1 in signed fract constants.
3587 For unsigned fract types, the result always overflows and
3588 we get an undefined (original) or the maximum value. */
3589 if (code
== PREINCREMENT_EXPR
)
3590 code
= PREDECREMENT_EXPR
;
3591 else if (code
== PREDECREMENT_EXPR
)
3592 code
= PREINCREMENT_EXPR
;
3593 else if (code
== POSTINCREMENT_EXPR
)
3594 code
= POSTDECREMENT_EXPR
;
3595 else /* code == POSTDECREMENT_EXPR */
3596 code
= POSTINCREMENT_EXPR
;
3598 inc
= integer_minus_one_node
;
3599 inc
= convert (argtype
, inc
);
3603 inc
= integer_one_node
;
3604 inc
= convert (argtype
, inc
);
3607 /* Report a read-only lvalue. */
3608 if (TYPE_READONLY (argtype
))
3610 readonly_error (arg
,
3611 ((code
== PREINCREMENT_EXPR
3612 || code
== POSTINCREMENT_EXPR
)
3613 ? lv_increment
: lv_decrement
));
3614 return error_mark_node
;
3616 else if (TREE_READONLY (arg
))
3617 readonly_warning (arg
,
3618 ((code
== PREINCREMENT_EXPR
3619 || code
== POSTINCREMENT_EXPR
)
3620 ? lv_increment
: lv_decrement
));
3622 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
3623 val
= boolean_increment (code
, arg
);
3625 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
3626 TREE_SIDE_EFFECTS (val
) = 1;
3627 if (TREE_CODE (val
) != code
)
3628 TREE_NO_WARNING (val
) = 1;
3630 goto return_build_unary_op
;
3634 /* Note that this operation never does default_conversion. */
3636 /* The operand of unary '&' must be an lvalue (which excludes
3637 expressions of type void), or, in C99, the result of a [] or
3638 unary '*' operator. */
3639 if (VOID_TYPE_P (TREE_TYPE (arg
))
3640 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
3641 && (TREE_CODE (arg
) != INDIRECT_REF
3643 pedwarn (location
, 0, "taking address of expression of type %<void%>");
3645 /* Let &* cancel out to simplify resulting code. */
3646 if (TREE_CODE (arg
) == INDIRECT_REF
)
3648 /* Don't let this be an lvalue. */
3649 if (lvalue_p (TREE_OPERAND (arg
, 0)))
3650 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
3651 ret
= TREE_OPERAND (arg
, 0);
3652 goto return_build_unary_op
;
3655 /* For &x[y], return x+y */
3656 if (TREE_CODE (arg
) == ARRAY_REF
)
3658 tree op0
= TREE_OPERAND (arg
, 0);
3659 if (!c_mark_addressable (op0
))
3660 return error_mark_node
;
3661 return build_binary_op (location
, PLUS_EXPR
,
3662 (TREE_CODE (TREE_TYPE (op0
)) == ARRAY_TYPE
3663 ? array_to_pointer_conversion (location
,
3666 TREE_OPERAND (arg
, 1), 1);
3669 /* Anything not already handled and not a true memory reference
3670 or a non-lvalue array is an error. */
3671 else if (typecode
!= FUNCTION_TYPE
&& !flag
3672 && !lvalue_or_else (arg
, lv_addressof
))
3673 return error_mark_node
;
3675 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3677 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3679 tree inner
= build_unary_op (location
, code
,
3680 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3681 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3682 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3683 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3684 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
3685 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
3686 goto return_build_unary_op
;
3689 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3690 argtype
= TREE_TYPE (arg
);
3692 /* If the lvalue is const or volatile, merge that into the type
3693 to which the address will point. Note that you can't get a
3694 restricted pointer by taking the address of something, so we
3695 only have to deal with `const' and `volatile' here. */
3696 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
3697 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
)))
3698 argtype
= c_build_type_variant (argtype
,
3699 TREE_READONLY (arg
),
3700 TREE_THIS_VOLATILE (arg
));
3702 if (!c_mark_addressable (arg
))
3703 return error_mark_node
;
3705 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
3706 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
3708 argtype
= build_pointer_type (argtype
);
3710 /* ??? Cope with user tricks that amount to offsetof. Delete this
3711 when we have proper support for integer constant expressions. */
3712 val
= get_base_address (arg
);
3713 if (val
&& TREE_CODE (val
) == INDIRECT_REF
3714 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
3716 tree op0
= fold_convert_loc (location
, sizetype
,
3717 fold_offsetof (arg
, val
)), op1
;
3719 op1
= fold_convert_loc (location
, argtype
, TREE_OPERAND (val
, 0));
3720 ret
= fold_build2_loc (location
, POINTER_PLUS_EXPR
, argtype
, op1
, op0
);
3721 goto return_build_unary_op
;
3724 val
= build1 (ADDR_EXPR
, argtype
, arg
);
3727 goto return_build_unary_op
;
3734 argtype
= TREE_TYPE (arg
);
3735 if (TREE_CODE (arg
) == INTEGER_CST
)
3736 ret
= (require_constant_value
3737 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
3738 : fold_build1_loc (location
, code
, argtype
, arg
));
3740 ret
= build1 (code
, argtype
, arg
);
3741 return_build_unary_op
:
3742 gcc_assert (ret
!= error_mark_node
);
3743 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
3744 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
3745 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
3746 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
3747 ret
= note_integer_operands (ret
);
3749 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
3750 protected_set_expr_location (ret
, location
);
3754 /* Return nonzero if REF is an lvalue valid for this language.
3755 Lvalues can be assigned, unless their type has TYPE_READONLY.
3756 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3759 lvalue_p (const_tree ref
)
3761 const enum tree_code code
= TREE_CODE (ref
);
3768 return lvalue_p (TREE_OPERAND (ref
, 0));
3770 case C_MAYBE_CONST_EXPR
:
3771 return lvalue_p (TREE_OPERAND (ref
, 1));
3773 case COMPOUND_LITERAL_EXPR
:
3783 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
3784 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
3787 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
3794 /* Give an error for storing in something that is 'const'. */
3797 readonly_error (tree arg
, enum lvalue_use use
)
3799 gcc_assert (use
== lv_assign
|| use
== lv_increment
|| use
== lv_decrement
3801 /* Using this macro rather than (for example) arrays of messages
3802 ensures that all the format strings are checked at compile
3804 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3805 : (use == lv_increment ? (I) \
3806 : (use == lv_decrement ? (D) : (AS))))
3807 if (TREE_CODE (arg
) == COMPONENT_REF
)
3809 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
3810 readonly_error (TREE_OPERAND (arg
, 0), use
);
3812 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3813 G_("increment of read-only member %qD"),
3814 G_("decrement of read-only member %qD"),
3815 G_("read-only member %qD used as %<asm%> output")),
3816 TREE_OPERAND (arg
, 1));
3818 else if (TREE_CODE (arg
) == VAR_DECL
)
3819 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3820 G_("increment of read-only variable %qD"),
3821 G_("decrement of read-only variable %qD"),
3822 G_("read-only variable %qD used as %<asm%> output")),
3825 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3826 G_("increment of read-only location %qE"),
3827 G_("decrement of read-only location %qE"),
3828 G_("read-only location %qE used as %<asm%> output")),
3832 /* Give a warning for storing in something that is read-only in GCC
3833 terms but not const in ISO C terms. */
3836 readonly_warning (tree arg
, enum lvalue_use use
)
3841 warning (0, "assignment of read-only location %qE", arg
);
3844 warning (0, "increment of read-only location %qE", arg
);
3847 warning (0, "decrement of read-only location %qE", arg
);
3856 /* Return nonzero if REF is an lvalue valid for this language;
3857 otherwise, print an error message and return zero. USE says
3858 how the lvalue is being used and so selects the error message. */
3861 lvalue_or_else (const_tree ref
, enum lvalue_use use
)
3863 int win
= lvalue_p (ref
);
3871 /* Mark EXP saying that we need to be able to take the
3872 address of it; it should not be allocated in a register.
3873 Returns true if successful. */
3876 c_mark_addressable (tree exp
)
3881 switch (TREE_CODE (x
))
3884 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
3887 ("cannot take address of bit-field %qD", TREE_OPERAND (x
, 1));
3891 /* ... fall through ... */
3897 x
= TREE_OPERAND (x
, 0);
3900 case COMPOUND_LITERAL_EXPR
:
3902 TREE_ADDRESSABLE (x
) = 1;
3909 if (C_DECL_REGISTER (x
)
3910 && DECL_NONLOCAL (x
))
3912 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
3915 ("global register variable %qD used in nested function", x
);
3918 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
3920 else if (C_DECL_REGISTER (x
))
3922 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
3923 error ("address of global register variable %qD requested", x
);
3925 error ("address of register variable %qD requested", x
);
3931 TREE_ADDRESSABLE (x
) = 1;
3938 /* Convert EXPR to TYPE, warning about conversion problems with
3939 constants. SEMANTIC_TYPE is the type this conversion would use
3940 without excess precision. If SEMANTIC_TYPE is NULL, this function
3941 is equivalent to convert_and_check. This function is a wrapper that
3942 handles conversions that may be different than
3943 the usual ones because of excess precision. */
3946 ep_convert_and_check (tree type
, tree expr
, tree semantic_type
)
3948 if (TREE_TYPE (expr
) == type
)
3952 return convert_and_check (type
, expr
);
3954 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
3955 && TREE_TYPE (expr
) != semantic_type
)
3957 /* For integers, we need to check the real conversion, not
3958 the conversion to the excess precision type. */
3959 expr
= convert_and_check (semantic_type
, expr
);
3961 /* Result type is the excess precision type, which should be
3962 large enough, so do not check. */
3963 return convert (type
, expr
);
3966 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3967 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3968 if folded to an integer constant then the unselected half may
3969 contain arbitrary operations not normally permitted in constant
3970 expressions. Set the location of the expression to LOC. */
3973 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
3974 tree op1
, tree op1_original_type
, tree op2
,
3975 tree op2_original_type
)
3979 enum tree_code code1
;
3980 enum tree_code code2
;
3981 tree result_type
= NULL
;
3982 tree semantic_result_type
= NULL
;
3983 tree orig_op1
= op1
, orig_op2
= op2
;
3984 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
3985 bool ifexp_int_operands
;
3989 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
3990 if (op1_int_operands
)
3991 op1
= remove_c_maybe_const_expr (op1
);
3992 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
3993 if (op2_int_operands
)
3994 op2
= remove_c_maybe_const_expr (op2
);
3995 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
3996 if (ifexp_int_operands
)
3997 ifexp
= remove_c_maybe_const_expr (ifexp
);
3999 /* Promote both alternatives. */
4001 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4002 op1
= default_conversion (op1
);
4003 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4004 op2
= default_conversion (op2
);
4006 if (TREE_CODE (ifexp
) == ERROR_MARK
4007 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4008 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4009 return error_mark_node
;
4011 type1
= TREE_TYPE (op1
);
4012 code1
= TREE_CODE (type1
);
4013 type2
= TREE_TYPE (op2
);
4014 code2
= TREE_CODE (type2
);
4016 /* C90 does not permit non-lvalue arrays in conditional expressions.
4017 In C99 they will be pointers by now. */
4018 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4020 error_at (colon_loc
, "non-lvalue array in conditional expression");
4021 return error_mark_node
;
4024 objc_ok
= objc_compare_types (type1
, type2
, -3, NULL_TREE
);
4026 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4027 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4028 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4029 || code1
== COMPLEX_TYPE
)
4030 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4031 || code2
== COMPLEX_TYPE
))
4033 semantic_result_type
= c_common_type (type1
, type2
);
4034 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4036 op1
= TREE_OPERAND (op1
, 0);
4037 type1
= TREE_TYPE (op1
);
4038 gcc_assert (TREE_CODE (type1
) == code1
);
4040 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4042 op2
= TREE_OPERAND (op2
, 0);
4043 type2
= TREE_TYPE (op2
);
4044 gcc_assert (TREE_CODE (type2
) == code2
);
4048 if (warn_cxx_compat
)
4050 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4051 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4053 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4054 && TREE_CODE (t2
) == ENUMERAL_TYPE
4055 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4056 warning_at (colon_loc
, OPT_Wc___compat
,
4057 ("different enum types in conditional is "
4058 "invalid in C++: %qT vs %qT"),
4062 /* Quickly detect the usual case where op1 and op2 have the same type
4064 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4067 result_type
= type1
;
4069 result_type
= TYPE_MAIN_VARIANT (type1
);
4071 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4072 || code1
== COMPLEX_TYPE
)
4073 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4074 || code2
== COMPLEX_TYPE
))
4076 result_type
= c_common_type (type1
, type2
);
4078 /* If -Wsign-compare, warn here if type1 and type2 have
4079 different signedness. We'll promote the signed to unsigned
4080 and later code won't know it used to be different.
4081 Do this check on the original types, so that explicit casts
4082 will be considered, but default promotions won't. */
4083 if (c_inhibit_evaluation_warnings
== 0)
4085 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4086 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4088 if (unsigned_op1
^ unsigned_op2
)
4092 /* Do not warn if the result type is signed, since the
4093 signed type will only be chosen if it can represent
4094 all the values of the unsigned type. */
4095 if (!TYPE_UNSIGNED (result_type
))
4099 bool op1_maybe_const
= true;
4100 bool op2_maybe_const
= true;
4102 /* Do not warn if the signed quantity is an
4103 unsuffixed integer literal (or some static
4104 constant expression involving such literals) and
4105 it is non-negative. This warning requires the
4106 operands to be folded for best results, so do
4107 that folding in this case even without
4108 warn_sign_compare to avoid warning options
4109 possibly affecting code generation. */
4110 c_inhibit_evaluation_warnings
4111 += (ifexp
== truthvalue_false_node
);
4112 op1
= c_fully_fold (op1
, require_constant_value
,
4114 c_inhibit_evaluation_warnings
4115 -= (ifexp
== truthvalue_false_node
);
4117 c_inhibit_evaluation_warnings
4118 += (ifexp
== truthvalue_true_node
);
4119 op2
= c_fully_fold (op2
, require_constant_value
,
4121 c_inhibit_evaluation_warnings
4122 -= (ifexp
== truthvalue_true_node
);
4124 if (warn_sign_compare
)
4127 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
4129 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
4132 warning_at (colon_loc
, OPT_Wsign_compare
,
4133 ("signed and unsigned type in "
4134 "conditional expression"));
4136 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
4137 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
4138 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
4139 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
4144 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4146 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
4147 pedwarn (colon_loc
, OPT_pedantic
,
4148 "ISO C forbids conditional expr with only one void side");
4149 result_type
= void_type_node
;
4151 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4153 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
4154 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
4155 addr_space_t as_common
;
4157 if (comp_target_types (colon_loc
, type1
, type2
))
4158 result_type
= common_pointer_type (type1
, type2
);
4159 else if (null_pointer_constant_p (orig_op1
))
4160 result_type
= type2
;
4161 else if (null_pointer_constant_p (orig_op2
))
4162 result_type
= type1
;
4163 else if (!addr_space_superset (as1
, as2
, &as_common
))
4165 error_at (colon_loc
, "pointers to disjoint address spaces "
4166 "used in conditional expression");
4167 return error_mark_node
;
4169 else if (VOID_TYPE_P (TREE_TYPE (type1
)))
4171 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
4172 pedwarn (colon_loc
, OPT_pedantic
,
4173 "ISO C forbids conditional expr between "
4174 "%<void *%> and function pointer");
4175 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
4176 TREE_TYPE (type2
)));
4178 else if (VOID_TYPE_P (TREE_TYPE (type2
)))
4180 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
4181 pedwarn (colon_loc
, OPT_pedantic
,
4182 "ISO C forbids conditional expr between "
4183 "%<void *%> and function pointer");
4184 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
4185 TREE_TYPE (type1
)));
4189 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
4192 pedwarn (colon_loc
, 0,
4193 "pointer type mismatch in conditional expression");
4194 result_type
= build_pointer_type
4195 (build_qualified_type (void_type_node
, qual
));
4198 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4200 if (!null_pointer_constant_p (orig_op2
))
4201 pedwarn (colon_loc
, 0,
4202 "pointer/integer type mismatch in conditional expression");
4205 op2
= null_pointer_node
;
4207 result_type
= type1
;
4209 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4211 if (!null_pointer_constant_p (orig_op1
))
4212 pedwarn (colon_loc
, 0,
4213 "pointer/integer type mismatch in conditional expression");
4216 op1
= null_pointer_node
;
4218 result_type
= type2
;
4223 if (flag_cond_mismatch
)
4224 result_type
= void_type_node
;
4227 error_at (colon_loc
, "type mismatch in conditional expression");
4228 return error_mark_node
;
4232 /* Merge const and volatile flags of the incoming types. */
4234 = build_type_variant (result_type
,
4235 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
4236 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
4238 op1
= ep_convert_and_check (result_type
, op1
, semantic_result_type
);
4239 op2
= ep_convert_and_check (result_type
, op2
, semantic_result_type
);
4241 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
4243 op2_int_operands
= true;
4244 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
4246 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
4248 op1_int_operands
= true;
4249 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
4251 int_const
= int_operands
= (ifexp_int_operands
4253 && op2_int_operands
);
4256 int_const
= ((ifexp
== truthvalue_true_node
4257 && TREE_CODE (orig_op1
) == INTEGER_CST
4258 && !TREE_OVERFLOW (orig_op1
))
4259 || (ifexp
== truthvalue_false_node
4260 && TREE_CODE (orig_op2
) == INTEGER_CST
4261 && !TREE_OVERFLOW (orig_op2
)));
4263 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
4264 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4267 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4269 ret
= note_integer_operands (ret
);
4271 if (semantic_result_type
)
4272 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
4274 protected_set_expr_location (ret
, colon_loc
);
4278 /* Return a compound expression that performs two expressions and
4279 returns the value of the second of them.
4281 LOC is the location of the COMPOUND_EXPR. */
4284 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
4286 bool expr1_int_operands
, expr2_int_operands
;
4287 tree eptype
= NULL_TREE
;
4290 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
4291 if (expr1_int_operands
)
4292 expr1
= remove_c_maybe_const_expr (expr1
);
4293 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
4294 if (expr2_int_operands
)
4295 expr2
= remove_c_maybe_const_expr (expr2
);
4297 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
4298 expr1
= TREE_OPERAND (expr1
, 0);
4299 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
4301 eptype
= TREE_TYPE (expr2
);
4302 expr2
= TREE_OPERAND (expr2
, 0);
4305 if (!TREE_SIDE_EFFECTS (expr1
))
4307 /* The left-hand operand of a comma expression is like an expression
4308 statement: with -Wunused, we should warn if it doesn't have
4309 any side-effects, unless it was explicitly cast to (void). */
4310 if (warn_unused_value
)
4312 if (VOID_TYPE_P (TREE_TYPE (expr1
))
4313 && CONVERT_EXPR_P (expr1
))
4315 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
4316 && TREE_CODE (expr1
) == COMPOUND_EXPR
4317 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
4318 ; /* (void) a, (void) b, c */
4320 warning_at (loc
, OPT_Wunused_value
,
4321 "left-hand operand of comma expression has no effect");
4325 /* With -Wunused, we should also warn if the left-hand operand does have
4326 side-effects, but computes a value which is not used. For example, in
4327 `foo() + bar(), baz()' the result of the `+' operator is not used,
4328 so we should issue a warning. */
4329 else if (warn_unused_value
)
4330 warn_if_unused_value (expr1
, loc
);
4332 if (expr2
== error_mark_node
)
4333 return error_mark_node
;
4335 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
4338 && expr1_int_operands
4339 && expr2_int_operands
)
4340 ret
= note_integer_operands (ret
);
4343 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4345 protected_set_expr_location (ret
, loc
);
4349 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4350 which we are casting. OTYPE is the type of the expression being
4351 cast. Both TYPE and OTYPE are pointer types. -Wcast-qual appeared
4352 on the command line. Named address space qualifiers are not handled
4353 here, because they result in different warnings. */
4356 handle_warn_cast_qual (tree type
, tree otype
)
4358 tree in_type
= type
;
4359 tree in_otype
= otype
;
4364 /* Check that the qualifiers on IN_TYPE are a superset of the
4365 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4366 nodes is uninteresting and we stop as soon as we hit a
4367 non-POINTER_TYPE node on either type. */
4370 in_otype
= TREE_TYPE (in_otype
);
4371 in_type
= TREE_TYPE (in_type
);
4373 /* GNU C allows cv-qualified function types. 'const' means the
4374 function is very pure, 'volatile' means it can't return. We
4375 need to warn when such qualifiers are added, not when they're
4377 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
4378 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
4379 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
4380 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
4382 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
4383 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
4385 while (TREE_CODE (in_type
) == POINTER_TYPE
4386 && TREE_CODE (in_otype
) == POINTER_TYPE
);
4389 warning (OPT_Wcast_qual
, "cast adds new qualifiers to function type");
4392 /* There are qualifiers present in IN_OTYPE that are not present
4394 warning (OPT_Wcast_qual
,
4395 "cast discards qualifiers from pointer target type");
4397 if (added
|| discarded
)
4400 /* A cast from **T to const **T is unsafe, because it can cause a
4401 const value to be changed with no additional warning. We only
4402 issue this warning if T is the same on both sides, and we only
4403 issue the warning if there are the same number of pointers on
4404 both sides, as otherwise the cast is clearly unsafe anyhow. A
4405 cast is unsafe when a qualifier is added at one level and const
4406 is not present at all outer levels.
4408 To issue this warning, we check at each level whether the cast
4409 adds new qualifiers not already seen. We don't need to special
4410 case function types, as they won't have the same
4411 TYPE_MAIN_VARIANT. */
4413 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
4415 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
4420 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
4423 in_type
= TREE_TYPE (in_type
);
4424 in_otype
= TREE_TYPE (in_otype
);
4425 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
4428 warning (OPT_Wcast_qual
,
4429 ("new qualifiers in middle of multi-level non-const cast "
4434 is_const
= TYPE_READONLY (in_type
);
4436 while (TREE_CODE (in_type
) == POINTER_TYPE
);
4439 /* Build an expression representing a cast to type TYPE of expression EXPR.
4440 LOC is the location of the cast-- typically the open paren of the cast. */
4443 build_c_cast (location_t loc
, tree type
, tree expr
)
4447 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
4448 expr
= TREE_OPERAND (expr
, 0);
4452 if (type
== error_mark_node
|| expr
== error_mark_node
)
4453 return error_mark_node
;
4455 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4456 only in <protocol> qualifications. But when constructing cast expressions,
4457 the protocols do matter and must be kept around. */
4458 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
4459 return build1 (NOP_EXPR
, type
, expr
);
4461 type
= TYPE_MAIN_VARIANT (type
);
4463 if (TREE_CODE (type
) == ARRAY_TYPE
)
4465 error_at (loc
, "cast specifies array type");
4466 return error_mark_node
;
4469 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4471 error_at (loc
, "cast specifies function type");
4472 return error_mark_node
;
4475 if (!VOID_TYPE_P (type
))
4477 value
= require_complete_type (value
);
4478 if (value
== error_mark_node
)
4479 return error_mark_node
;
4482 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
4484 if (TREE_CODE (type
) == RECORD_TYPE
4485 || TREE_CODE (type
) == UNION_TYPE
)
4486 pedwarn (loc
, OPT_pedantic
,
4487 "ISO C forbids casting nonscalar to the same type");
4489 else if (TREE_CODE (type
) == UNION_TYPE
)
4493 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
4494 if (TREE_TYPE (field
) != error_mark_node
4495 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
4496 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
4502 bool maybe_const
= true;
4504 pedwarn (loc
, OPT_pedantic
, "ISO C forbids casts to union type");
4505 t
= c_fully_fold (value
, false, &maybe_const
);
4506 t
= build_constructor_single (type
, field
, t
);
4508 t
= c_wrap_maybe_const (t
, true);
4509 t
= digest_init (loc
, type
, t
,
4510 NULL_TREE
, false, true, 0);
4511 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
4514 error_at (loc
, "cast to union type from type not present in union");
4515 return error_mark_node
;
4521 if (type
== void_type_node
)
4523 tree t
= build1 (CONVERT_EXPR
, type
, value
);
4524 SET_EXPR_LOCATION (t
, loc
);
4528 otype
= TREE_TYPE (value
);
4530 /* Optionally warn about potentially worrisome casts. */
4532 && TREE_CODE (type
) == POINTER_TYPE
4533 && TREE_CODE (otype
) == POINTER_TYPE
)
4534 handle_warn_cast_qual (type
, otype
);
4536 /* Warn about conversions between pointers to disjoint
4538 if (TREE_CODE (type
) == POINTER_TYPE
4539 && TREE_CODE (otype
) == POINTER_TYPE
4540 && !null_pointer_constant_p (value
))
4542 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
4543 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
4544 addr_space_t as_common
;
4546 if (!addr_space_superset (as_to
, as_from
, &as_common
))
4548 if (ADDR_SPACE_GENERIC_P (as_from
))
4549 warning_at (loc
, 0, "cast to %s address space pointer "
4550 "from disjoint generic address space pointer",
4551 c_addr_space_name (as_to
));
4553 else if (ADDR_SPACE_GENERIC_P (as_to
))
4554 warning_at (loc
, 0, "cast to generic address space pointer "
4555 "from disjoint %s address space pointer",
4556 c_addr_space_name (as_from
));
4559 warning_at (loc
, 0, "cast to %s address space pointer "
4560 "from disjoint %s address space pointer",
4561 c_addr_space_name (as_to
),
4562 c_addr_space_name (as_from
));
4566 /* Warn about possible alignment problems. */
4567 if (STRICT_ALIGNMENT
4568 && TREE_CODE (type
) == POINTER_TYPE
4569 && TREE_CODE (otype
) == POINTER_TYPE
4570 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
4571 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
4572 /* Don't warn about opaque types, where the actual alignment
4573 restriction is unknown. */
4574 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
4575 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
4576 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
4577 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
4578 warning_at (loc
, OPT_Wcast_align
,
4579 "cast increases required alignment of target type");
4581 if (TREE_CODE (type
) == INTEGER_TYPE
4582 && TREE_CODE (otype
) == POINTER_TYPE
4583 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
4584 /* Unlike conversion of integers to pointers, where the
4585 warning is disabled for converting constants because
4586 of cases such as SIG_*, warn about converting constant
4587 pointers to integers. In some cases it may cause unwanted
4588 sign extension, and a warning is appropriate. */
4589 warning_at (loc
, OPT_Wpointer_to_int_cast
,
4590 "cast from pointer to integer of different size");
4592 if (TREE_CODE (value
) == CALL_EXPR
4593 && TREE_CODE (type
) != TREE_CODE (otype
))
4594 warning_at (loc
, OPT_Wbad_function_cast
,
4595 "cast from function call of type %qT "
4596 "to non-matching type %qT", otype
, type
);
4598 if (TREE_CODE (type
) == POINTER_TYPE
4599 && TREE_CODE (otype
) == INTEGER_TYPE
4600 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
4601 /* Don't warn about converting any constant. */
4602 && !TREE_CONSTANT (value
))
4604 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
4605 "of different size");
4607 if (warn_strict_aliasing
<= 2)
4608 strict_aliasing_warning (otype
, type
, expr
);
4610 /* If pedantic, warn for conversions between function and object
4611 pointer types, except for converting a null pointer constant
4612 to function pointer type. */
4614 && TREE_CODE (type
) == POINTER_TYPE
4615 && TREE_CODE (otype
) == POINTER_TYPE
4616 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
4617 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
4618 pedwarn (loc
, OPT_pedantic
, "ISO C forbids "
4619 "conversion of function pointer to object pointer type");
4622 && TREE_CODE (type
) == POINTER_TYPE
4623 && TREE_CODE (otype
) == POINTER_TYPE
4624 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
4625 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
4626 && !null_pointer_constant_p (value
))
4627 pedwarn (loc
, OPT_pedantic
, "ISO C forbids "
4628 "conversion of object pointer to function pointer type");
4631 value
= convert (type
, value
);
4633 /* Ignore any integer overflow caused by the cast. */
4634 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
4636 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
4638 if (!TREE_OVERFLOW (value
))
4640 /* Avoid clobbering a shared constant. */
4641 value
= copy_node (value
);
4642 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
4645 else if (TREE_OVERFLOW (value
))
4646 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4647 value
= build_int_cst_wide (TREE_TYPE (value
),
4648 TREE_INT_CST_LOW (value
),
4649 TREE_INT_CST_HIGH (value
));
4653 /* Don't let a cast be an lvalue. */
4655 value
= non_lvalue_loc (loc
, value
);
4657 /* Don't allow the results of casting to floating-point or complex
4658 types be confused with actual constants, or casts involving
4659 integer and pointer types other than direct integer-to-integer
4660 and integer-to-pointer be confused with integer constant
4661 expressions and null pointer constants. */
4662 if (TREE_CODE (value
) == REAL_CST
4663 || TREE_CODE (value
) == COMPLEX_CST
4664 || (TREE_CODE (value
) == INTEGER_CST
4665 && !((TREE_CODE (expr
) == INTEGER_CST
4666 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
4667 || TREE_CODE (expr
) == REAL_CST
4668 || TREE_CODE (expr
) == COMPLEX_CST
)))
4669 value
= build1 (NOP_EXPR
, type
, value
);
4671 if (CAN_HAVE_LOCATION_P (value
))
4672 SET_EXPR_LOCATION (value
, loc
);
4676 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4677 location of the open paren of the cast, or the position of the cast
4680 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
4683 tree type_expr
= NULL_TREE
;
4684 bool type_expr_const
= true;
4686 int saved_wsp
= warn_strict_prototypes
;
4688 /* This avoids warnings about unprototyped casts on
4689 integers. E.g. "#define SIG_DFL (void(*)())0". */
4690 if (TREE_CODE (expr
) == INTEGER_CST
)
4691 warn_strict_prototypes
= 0;
4692 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
4693 warn_strict_prototypes
= saved_wsp
;
4695 ret
= build_c_cast (loc
, type
, expr
);
4698 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
4699 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !type_expr_const
;
4700 SET_EXPR_LOCATION (ret
, loc
);
4703 if (CAN_HAVE_LOCATION_P (ret
) && !EXPR_HAS_LOCATION (ret
))
4704 SET_EXPR_LOCATION (ret
, loc
);
4706 /* C++ does not permits types to be defined in a cast. */
4707 if (warn_cxx_compat
&& type_name
->specs
->tag_defined_p
)
4708 warning_at (loc
, OPT_Wc___compat
,
4709 "defining a type in a cast is invalid in C++");
4714 /* Build an assignment expression of lvalue LHS from value RHS.
4715 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4716 may differ from TREE_TYPE (LHS) for an enum bitfield.
4717 MODIFYCODE is the code for a binary operator that we use
4718 to combine the old value of LHS with RHS to get the new value.
4719 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4720 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4721 which may differ from TREE_TYPE (RHS) for an enum value.
4723 LOCATION is the location of the MODIFYCODE operator.
4724 RHS_LOC is the location of the RHS. */
4727 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
4728 enum tree_code modifycode
,
4729 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
4733 tree rhs_semantic_type
= NULL_TREE
;
4734 tree lhstype
= TREE_TYPE (lhs
);
4735 tree olhstype
= lhstype
;
4738 /* Types that aren't fully specified cannot be used in assignments. */
4739 lhs
= require_complete_type (lhs
);
4741 /* Avoid duplicate error messages from operands that had errors. */
4742 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
4743 return error_mark_node
;
4745 if (!lvalue_or_else (lhs
, lv_assign
))
4746 return error_mark_node
;
4748 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4750 rhs_semantic_type
= TREE_TYPE (rhs
);
4751 rhs
= TREE_OPERAND (rhs
, 0);
4756 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
4758 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
4759 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
4761 if (inner
== error_mark_node
)
4762 return error_mark_node
;
4763 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4764 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
4765 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
4766 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
4767 protected_set_expr_location (result
, location
);
4771 /* If a binary op has been requested, combine the old LHS value with the RHS
4772 producing the value we should actually store into the LHS. */
4774 if (modifycode
!= NOP_EXPR
)
4776 lhs
= c_fully_fold (lhs
, false, NULL
);
4777 lhs
= stabilize_reference (lhs
);
4778 newrhs
= build_binary_op (location
,
4779 modifycode
, lhs
, rhs
, 1);
4781 /* The original type of the right hand side is no longer
4783 rhs_origtype
= NULL_TREE
;
4786 /* Give an error for storing in something that is 'const'. */
4788 if (TYPE_READONLY (lhstype
)
4789 || ((TREE_CODE (lhstype
) == RECORD_TYPE
4790 || TREE_CODE (lhstype
) == UNION_TYPE
)
4791 && C_TYPE_FIELDS_READONLY (lhstype
)))
4793 readonly_error (lhs
, lv_assign
);
4794 return error_mark_node
;
4796 else if (TREE_READONLY (lhs
))
4797 readonly_warning (lhs
, lv_assign
);
4799 /* If storing into a structure or union member,
4800 it has probably been given type `int'.
4801 Compute the type that would go with
4802 the actual amount of storage the member occupies. */
4804 if (TREE_CODE (lhs
) == COMPONENT_REF
4805 && (TREE_CODE (lhstype
) == INTEGER_TYPE
4806 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
4807 || TREE_CODE (lhstype
) == REAL_TYPE
4808 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
4809 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
4811 /* If storing in a field that is in actuality a short or narrower than one,
4812 we must store in the field in its actual type. */
4814 if (lhstype
!= TREE_TYPE (lhs
))
4816 lhs
= copy_node (lhs
);
4817 TREE_TYPE (lhs
) = lhstype
;
4820 /* Issue -Wc++-compat warnings about an assignment to an enum type
4821 when LHS does not have its original type. This happens for,
4822 e.g., an enum bitfield in a struct. */
4824 && lhs_origtype
!= NULL_TREE
4825 && lhs_origtype
!= lhstype
4826 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
4828 tree checktype
= (rhs_origtype
!= NULL_TREE
4831 if (checktype
!= error_mark_node
4832 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
))
4833 warning_at (location
, OPT_Wc___compat
,
4834 "enum conversion in assignment is invalid in C++");
4837 /* Convert new value to destination type. Fold it first, then
4838 restore any excess precision information, for the sake of
4839 conversion warnings. */
4841 npc
= null_pointer_constant_p (newrhs
);
4842 newrhs
= c_fully_fold (newrhs
, false, NULL
);
4843 if (rhs_semantic_type
)
4844 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
4845 newrhs
= convert_for_assignment (location
, lhstype
, newrhs
, rhs_origtype
,
4846 ic_assign
, npc
, NULL_TREE
, NULL_TREE
, 0);
4847 if (TREE_CODE (newrhs
) == ERROR_MARK
)
4848 return error_mark_node
;
4850 /* Emit ObjC write barrier, if necessary. */
4851 if (c_dialect_objc () && flag_objc_gc
)
4853 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
4856 protected_set_expr_location (result
, location
);
4861 /* Scan operands. */
4863 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
4864 TREE_SIDE_EFFECTS (result
) = 1;
4865 protected_set_expr_location (result
, location
);
4867 /* If we got the LHS in a different type for storing in,
4868 convert the result back to the nominal type of LHS
4869 so that the value we return always has the same type
4870 as the LHS argument. */
4872 if (olhstype
== TREE_TYPE (result
))
4875 result
= convert_for_assignment (location
, olhstype
, result
, rhs_origtype
,
4876 ic_assign
, false, NULL_TREE
, NULL_TREE
, 0);
4877 protected_set_expr_location (result
, location
);
4881 /* Convert value RHS to type TYPE as preparation for an assignment to
4882 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
4883 original type of RHS; this differs from TREE_TYPE (RHS) for enum
4884 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
4885 constant before any folding.
4886 The real work of conversion is done by `convert'.
4887 The purpose of this function is to generate error messages
4888 for assignments that are not allowed in C.
4889 ERRTYPE says whether it is argument passing, assignment,
4890 initialization or return.
4892 LOCATION is the location of the RHS.
4893 FUNCTION is a tree for the function being called.
4894 PARMNUM is the number of the argument, for printing in error messages. */
4897 convert_for_assignment (location_t location
, tree type
, tree rhs
,
4898 tree origtype
, enum impl_conv errtype
,
4899 bool null_pointer_constant
, tree fundecl
,
4900 tree function
, int parmnum
)
4902 enum tree_code codel
= TREE_CODE (type
);
4903 tree orig_rhs
= rhs
;
4905 enum tree_code coder
;
4906 tree rname
= NULL_TREE
;
4907 bool objc_ok
= false;
4909 if (errtype
== ic_argpass
)
4912 /* Change pointer to function to the function itself for
4914 if (TREE_CODE (function
) == ADDR_EXPR
4915 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
4916 function
= TREE_OPERAND (function
, 0);
4918 /* Handle an ObjC selector specially for diagnostics. */
4919 selector
= objc_message_selector ();
4921 if (selector
&& parmnum
> 2)
4928 /* This macro is used to emit diagnostics to ensure that all format
4929 strings are complete sentences, visible to gettext and checked at
4931 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4936 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4937 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4938 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4939 "expected %qT but argument is of type %qT", \
4943 pedwarn (LOCATION, OPT, AS); \
4946 pedwarn (LOCATION, OPT, IN); \
4949 pedwarn (LOCATION, OPT, RE); \
4952 gcc_unreachable (); \
4956 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4957 rhs
= TREE_OPERAND (rhs
, 0);
4959 rhstype
= TREE_TYPE (rhs
);
4960 coder
= TREE_CODE (rhstype
);
4962 if (coder
== ERROR_MARK
)
4963 return error_mark_node
;
4965 if (c_dialect_objc ())
4988 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
4991 if (warn_cxx_compat
)
4993 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
4994 if (checktype
!= error_mark_node
4995 && TREE_CODE (type
) == ENUMERAL_TYPE
4996 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
4998 WARN_FOR_ASSIGNMENT (input_location
, OPT_Wc___compat
,
4999 G_("enum conversion when passing argument "
5000 "%d of %qE is invalid in C++"),
5001 G_("enum conversion in assignment is "
5003 G_("enum conversion in initialization is "
5005 G_("enum conversion in return is "
5010 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
5013 if (coder
== VOID_TYPE
)
5015 /* Except for passing an argument to an unprototyped function,
5016 this is a constraint violation. When passing an argument to
5017 an unprototyped function, it is compile-time undefined;
5018 making it a constraint in that case was rejected in
5020 error_at (location
, "void value not ignored as it ought to be");
5021 return error_mark_node
;
5023 rhs
= require_complete_type (rhs
);
5024 if (rhs
== error_mark_node
)
5025 return error_mark_node
;
5026 /* A type converts to a reference to it.
5027 This code doesn't fully support references, it's just for the
5028 special case of va_start and va_copy. */
5029 if (codel
== REFERENCE_TYPE
5030 && comptypes (TREE_TYPE (type
), TREE_TYPE (rhs
)) == 1)
5032 if (!lvalue_p (rhs
))
5034 error_at (location
, "cannot pass rvalue to reference parameter");
5035 return error_mark_node
;
5037 if (!c_mark_addressable (rhs
))
5038 return error_mark_node
;
5039 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
5040 SET_EXPR_LOCATION (rhs
, location
);
5042 /* We already know that these two types are compatible, but they
5043 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5044 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5045 likely to be va_list, a typedef to __builtin_va_list, which
5046 is different enough that it will cause problems later. */
5047 if (TREE_TYPE (TREE_TYPE (rhs
)) != TREE_TYPE (type
))
5049 rhs
= build1 (NOP_EXPR
, build_pointer_type (TREE_TYPE (type
)), rhs
);
5050 SET_EXPR_LOCATION (rhs
, location
);
5053 rhs
= build1 (NOP_EXPR
, type
, rhs
);
5054 SET_EXPR_LOCATION (rhs
, location
);
5057 /* Some types can interconvert without explicit casts. */
5058 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
5059 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
5060 return convert (type
, rhs
);
5061 /* Arithmetic types all interconvert, and enum is treated like int. */
5062 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
5063 || codel
== FIXED_POINT_TYPE
5064 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
5065 || codel
== BOOLEAN_TYPE
)
5066 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
5067 || coder
== FIXED_POINT_TYPE
5068 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
5069 || coder
== BOOLEAN_TYPE
))
5072 bool save
= in_late_binary_op
;
5073 if (codel
== BOOLEAN_TYPE
)
5074 in_late_binary_op
= true;
5075 ret
= convert_and_check (type
, orig_rhs
);
5076 if (codel
== BOOLEAN_TYPE
)
5077 in_late_binary_op
= save
;
5081 /* Aggregates in different TUs might need conversion. */
5082 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
5084 && comptypes (type
, rhstype
))
5085 return convert_and_check (type
, rhs
);
5087 /* Conversion to a transparent union or record from its member types.
5088 This applies only to function arguments. */
5089 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
5090 && TYPE_TRANSPARENT_AGGR (type
))
5091 && errtype
== ic_argpass
)
5093 tree memb
, marginal_memb
= NULL_TREE
;
5095 for (memb
= TYPE_FIELDS (type
); memb
; memb
= TREE_CHAIN (memb
))
5097 tree memb_type
= TREE_TYPE (memb
);
5099 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
5100 TYPE_MAIN_VARIANT (rhstype
)))
5103 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
5106 if (coder
== POINTER_TYPE
)
5108 tree ttl
= TREE_TYPE (memb_type
);
5109 tree ttr
= TREE_TYPE (rhstype
);
5111 /* Any non-function converts to a [const][volatile] void *
5112 and vice versa; otherwise, targets must be the same.
5113 Meanwhile, the lhs target must have all the qualifiers of
5115 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
5116 || comp_target_types (location
, memb_type
, rhstype
))
5118 /* If this type won't generate any warnings, use it. */
5119 if (TYPE_QUALS (ttl
) == TYPE_QUALS (ttr
)
5120 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
5121 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5122 ? ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
5123 == TYPE_QUALS (ttr
))
5124 : ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
5125 == TYPE_QUALS (ttl
))))
5128 /* Keep looking for a better type, but remember this one. */
5130 marginal_memb
= memb
;
5134 /* Can convert integer zero to any pointer type. */
5135 if (null_pointer_constant
)
5137 rhs
= null_pointer_node
;
5142 if (memb
|| marginal_memb
)
5146 /* We have only a marginally acceptable member type;
5147 it needs a warning. */
5148 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
5149 tree ttr
= TREE_TYPE (rhstype
);
5151 /* Const and volatile mean something different for function
5152 types, so the usual warnings are not appropriate. */
5153 if (TREE_CODE (ttr
) == FUNCTION_TYPE
5154 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5156 /* Because const and volatile on functions are
5157 restrictions that say the function will not do
5158 certain things, it is okay to use a const or volatile
5159 function where an ordinary one is wanted, but not
5161 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
5162 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
5163 WARN_FOR_ASSIGNMENT (location
, 0,
5164 G_("passing argument %d of %qE "
5165 "makes qualified function "
5166 "pointer from unqualified"),
5167 G_("assignment makes qualified "
5168 "function pointer from "
5170 G_("initialization makes qualified "
5171 "function pointer from "
5173 G_("return makes qualified function "
5174 "pointer from unqualified"));
5176 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
5177 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
5178 WARN_FOR_ASSIGNMENT (location
, 0,
5179 G_("passing argument %d of %qE discards "
5180 "qualifiers from pointer target type"),
5181 G_("assignment discards qualifiers "
5182 "from pointer target type"),
5183 G_("initialization discards qualifiers "
5184 "from pointer target type"),
5185 G_("return discards qualifiers from "
5186 "pointer target type"));
5188 memb
= marginal_memb
;
5191 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
5192 pedwarn (location
, OPT_pedantic
,
5193 "ISO C prohibits argument conversion to union type");
5195 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
5196 return build_constructor_single (type
, memb
, rhs
);
5200 /* Conversions among pointers */
5201 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
5202 && (coder
== codel
))
5204 tree ttl
= TREE_TYPE (type
);
5205 tree ttr
= TREE_TYPE (rhstype
);
5208 bool is_opaque_pointer
;
5209 int target_cmp
= 0; /* Cache comp_target_types () result. */
5213 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
5214 mvl
= TYPE_MAIN_VARIANT (mvl
);
5215 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
5216 mvr
= TYPE_MAIN_VARIANT (mvr
);
5217 /* Opaque pointers are treated like void pointers. */
5218 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
5220 /* C++ does not allow the implicit conversion void* -> T*. However,
5221 for the purpose of reducing the number of false positives, we
5222 tolerate the special case of
5226 where NULL is typically defined in C to be '(void *) 0'. */
5227 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
5228 warning_at (location
, OPT_Wc___compat
,
5229 "request for implicit conversion "
5230 "from %qT to %qT not permitted in C++", rhstype
, type
);
5232 /* See if the pointers point to incompatible address spaces. */
5233 asl
= TYPE_ADDR_SPACE (ttl
);
5234 asr
= TYPE_ADDR_SPACE (ttr
);
5235 if (!null_pointer_constant_p (rhs
)
5236 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
5241 error_at (location
, "passing argument %d of %qE from pointer to "
5242 "non-enclosed address space", parmnum
, rname
);
5245 error_at (location
, "assignment from pointer to "
5246 "non-enclosed address space");
5249 error_at (location
, "initialization from pointer to "
5250 "non-enclosed address space");
5253 error_at (location
, "return from pointer to "
5254 "non-enclosed address space");
5259 return error_mark_node
;
5262 /* Check if the right-hand side has a format attribute but the
5263 left-hand side doesn't. */
5264 if (warn_missing_format_attribute
5265 && check_missing_format_attribute (type
, rhstype
))
5270 warning_at (location
, OPT_Wmissing_format_attribute
,
5271 "argument %d of %qE might be "
5272 "a candidate for a format attribute",
5276 warning_at (location
, OPT_Wmissing_format_attribute
,
5277 "assignment left-hand side might be "
5278 "a candidate for a format attribute");
5281 warning_at (location
, OPT_Wmissing_format_attribute
,
5282 "initialization left-hand side might be "
5283 "a candidate for a format attribute");
5286 warning_at (location
, OPT_Wmissing_format_attribute
,
5287 "return type might be "
5288 "a candidate for a format attribute");
5295 /* Any non-function converts to a [const][volatile] void *
5296 and vice versa; otherwise, targets must be the same.
5297 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5298 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
5299 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
5300 || is_opaque_pointer
5301 || (c_common_unsigned_type (mvl
)
5302 == c_common_unsigned_type (mvr
)))
5305 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
5308 && !null_pointer_constant
5309 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
5310 WARN_FOR_ASSIGNMENT (location
, OPT_pedantic
,
5311 G_("ISO C forbids passing argument %d of "
5312 "%qE between function pointer "
5314 G_("ISO C forbids assignment between "
5315 "function pointer and %<void *%>"),
5316 G_("ISO C forbids initialization between "
5317 "function pointer and %<void *%>"),
5318 G_("ISO C forbids return between function "
5319 "pointer and %<void *%>"));
5320 /* Const and volatile mean something different for function types,
5321 so the usual warnings are not appropriate. */
5322 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
5323 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
5325 if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
5326 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
5328 /* Types differing only by the presence of the 'volatile'
5329 qualifier are acceptable if the 'volatile' has been added
5330 in by the Objective-C EH machinery. */
5331 if (!objc_type_quals_match (ttl
, ttr
))
5332 WARN_FOR_ASSIGNMENT (location
, 0,
5333 G_("passing argument %d of %qE discards "
5334 "qualifiers from pointer target type"),
5335 G_("assignment discards qualifiers "
5336 "from pointer target type"),
5337 G_("initialization discards qualifiers "
5338 "from pointer target type"),
5339 G_("return discards qualifiers from "
5340 "pointer target type"));
5342 /* If this is not a case of ignoring a mismatch in signedness,
5344 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
5347 /* If there is a mismatch, do warn. */
5348 else if (warn_pointer_sign
)
5349 WARN_FOR_ASSIGNMENT (location
, OPT_Wpointer_sign
,
5350 G_("pointer targets in passing argument "
5351 "%d of %qE differ in signedness"),
5352 G_("pointer targets in assignment "
5353 "differ in signedness"),
5354 G_("pointer targets in initialization "
5355 "differ in signedness"),
5356 G_("pointer targets in return differ "
5359 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
5360 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
5362 /* Because const and volatile on functions are restrictions
5363 that say the function will not do certain things,
5364 it is okay to use a const or volatile function
5365 where an ordinary one is wanted, but not vice-versa. */
5366 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
5367 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
5368 WARN_FOR_ASSIGNMENT (location
, 0,
5369 G_("passing argument %d of %qE makes "
5370 "qualified function pointer "
5371 "from unqualified"),
5372 G_("assignment makes qualified function "
5373 "pointer from unqualified"),
5374 G_("initialization makes qualified "
5375 "function pointer from unqualified"),
5376 G_("return makes qualified function "
5377 "pointer from unqualified"));
5381 /* Avoid warning about the volatile ObjC EH puts on decls. */
5383 WARN_FOR_ASSIGNMENT (location
, 0,
5384 G_("passing argument %d of %qE from "
5385 "incompatible pointer type"),
5386 G_("assignment from incompatible pointer type"),
5387 G_("initialization from incompatible "
5389 G_("return from incompatible pointer type"));
5391 return convert (type
, rhs
);
5393 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
5395 /* ??? This should not be an error when inlining calls to
5396 unprototyped functions. */
5397 error_at (location
, "invalid use of non-lvalue array");
5398 return error_mark_node
;
5400 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
5402 /* An explicit constant 0 can convert to a pointer,
5403 or one that results from arithmetic, even including
5404 a cast to integer type. */
5405 if (!null_pointer_constant
)
5406 WARN_FOR_ASSIGNMENT (location
, 0,
5407 G_("passing argument %d of %qE makes "
5408 "pointer from integer without a cast"),
5409 G_("assignment makes pointer from integer "
5411 G_("initialization makes pointer from "
5412 "integer without a cast"),
5413 G_("return makes pointer from integer "
5416 return convert (type
, rhs
);
5418 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
5420 WARN_FOR_ASSIGNMENT (location
, 0,
5421 G_("passing argument %d of %qE makes integer "
5422 "from pointer without a cast"),
5423 G_("assignment makes integer from pointer "
5425 G_("initialization makes integer from pointer "
5427 G_("return makes integer from pointer "
5429 return convert (type
, rhs
);
5431 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
5434 bool save
= in_late_binary_op
;
5435 in_late_binary_op
= true;
5436 ret
= convert (type
, rhs
);
5437 in_late_binary_op
= save
;
5444 error_at (location
, "incompatible type for argument %d of %qE", parmnum
, rname
);
5445 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
5446 ? DECL_SOURCE_LOCATION (fundecl
) : input_location
,
5447 "expected %qT but argument is of type %qT", type
, rhstype
);
5450 error_at (location
, "incompatible types when assigning to type %qT from "
5451 "type %qT", type
, rhstype
);
5455 "incompatible types when initializing type %qT using type %qT",
5460 "incompatible types when returning type %qT but %qT was "
5461 "expected", rhstype
, type
);
5467 return error_mark_node
;
5470 /* If VALUE is a compound expr all of whose expressions are constant, then
5471 return its value. Otherwise, return error_mark_node.
5473 This is for handling COMPOUND_EXPRs as initializer elements
5474 which is allowed with a warning when -pedantic is specified. */
5477 valid_compound_expr_initializer (tree value
, tree endtype
)
5479 if (TREE_CODE (value
) == COMPOUND_EXPR
)
5481 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
5483 return error_mark_node
;
5484 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
5487 else if (!initializer_constant_valid_p (value
, endtype
))
5488 return error_mark_node
;
5493 /* Perform appropriate conversions on the initial value of a variable,
5494 store it in the declaration DECL,
5495 and print any error messages that are appropriate.
5496 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5497 If the init is invalid, store an ERROR_MARK.
5499 INIT_LOC is the location of the initial value. */
5502 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
5507 /* If variable's type was invalidly declared, just ignore it. */
5509 type
= TREE_TYPE (decl
);
5510 if (TREE_CODE (type
) == ERROR_MARK
)
5513 /* Digest the specified initializer into an expression. */
5516 npc
= null_pointer_constant_p (init
);
5517 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
5518 true, TREE_STATIC (decl
));
5520 /* Store the expression if valid; else report error. */
5522 if (!in_system_header
5523 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
5524 warning (OPT_Wtraditional
, "traditional C rejects automatic "
5525 "aggregate initialization");
5527 DECL_INITIAL (decl
) = value
;
5529 /* ANSI wants warnings about out-of-range constant initializers. */
5530 STRIP_TYPE_NOPS (value
);
5531 if (TREE_STATIC (decl
))
5532 constant_expression_warning (value
);
5534 /* Check if we need to set array size from compound literal size. */
5535 if (TREE_CODE (type
) == ARRAY_TYPE
5536 && TYPE_DOMAIN (type
) == 0
5537 && value
!= error_mark_node
)
5539 tree inside_init
= init
;
5541 STRIP_TYPE_NOPS (inside_init
);
5542 inside_init
= fold (inside_init
);
5544 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
5546 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
5548 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
5550 /* For int foo[] = (int [3]){1}; we need to set array size
5551 now since later on array initializer will be just the
5552 brace enclosed list of the compound literal. */
5553 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
5554 TREE_TYPE (decl
) = type
;
5555 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
5557 layout_decl (cldecl
, 0);
5563 /* Methods for storing and printing names for error messages. */
5565 /* Implement a spelling stack that allows components of a name to be pushed
5566 and popped. Each element on the stack is this structure. */
5573 unsigned HOST_WIDE_INT i
;
5578 #define SPELLING_STRING 1
5579 #define SPELLING_MEMBER 2
5580 #define SPELLING_BOUNDS 3
5582 static struct spelling
*spelling
; /* Next stack element (unused). */
5583 static struct spelling
*spelling_base
; /* Spelling stack base. */
5584 static int spelling_size
; /* Size of the spelling stack. */
5586 /* Macros to save and restore the spelling stack around push_... functions.
5587 Alternative to SAVE_SPELLING_STACK. */
5589 #define SPELLING_DEPTH() (spelling - spelling_base)
5590 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5592 /* Push an element on the spelling stack with type KIND and assign VALUE
5595 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5597 int depth = SPELLING_DEPTH (); \
5599 if (depth >= spelling_size) \
5601 spelling_size += 10; \
5602 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5604 RESTORE_SPELLING_DEPTH (depth); \
5607 spelling->kind = (KIND); \
5608 spelling->MEMBER = (VALUE); \
5612 /* Push STRING on the stack. Printed literally. */
5615 push_string (const char *string
)
5617 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
5620 /* Push a member name on the stack. Printed as '.' STRING. */
5623 push_member_name (tree decl
)
5625 const char *const string
5627 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
5628 : _("<anonymous>"));
5629 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
5632 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5635 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
5637 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
5640 /* Compute the maximum size in bytes of the printed spelling. */
5643 spelling_length (void)
5648 for (p
= spelling_base
; p
< spelling
; p
++)
5650 if (p
->kind
== SPELLING_BOUNDS
)
5653 size
+= strlen (p
->u
.s
) + 1;
5659 /* Print the spelling to BUFFER and return it. */
5662 print_spelling (char *buffer
)
5667 for (p
= spelling_base
; p
< spelling
; p
++)
5668 if (p
->kind
== SPELLING_BOUNDS
)
5670 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
5676 if (p
->kind
== SPELLING_MEMBER
)
5678 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
5685 /* Issue an error message for a bad initializer component.
5686 MSGID identifies the message.
5687 The component name is taken from the spelling stack. */
5690 error_init (const char *msgid
)
5694 error ("%s", _(msgid
));
5695 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5697 error ("(near initialization for %qs)", ofwhat
);
5700 /* Issue a pedantic warning for a bad initializer component. OPT is
5701 the option OPT_* (from options.h) controlling this warning or 0 if
5702 it is unconditionally given. MSGID identifies the message. The
5703 component name is taken from the spelling stack. */
5706 pedwarn_init (location_t location
, int opt
, const char *msgid
)
5710 pedwarn (location
, opt
, "%s", _(msgid
));
5711 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5713 pedwarn (location
, opt
, "(near initialization for %qs)", ofwhat
);
5716 /* Issue a warning for a bad initializer component.
5718 OPT is the OPT_W* value corresponding to the warning option that
5719 controls this warning. MSGID identifies the message. The
5720 component name is taken from the spelling stack. */
5723 warning_init (int opt
, const char *msgid
)
5727 warning (opt
, "%s", _(msgid
));
5728 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5730 warning (opt
, "(near initialization for %qs)", ofwhat
);
5733 /* If TYPE is an array type and EXPR is a parenthesized string
5734 constant, warn if pedantic that EXPR is being used to initialize an
5735 object of type TYPE. */
5738 maybe_warn_string_init (tree type
, struct c_expr expr
)
5741 && TREE_CODE (type
) == ARRAY_TYPE
5742 && TREE_CODE (expr
.value
) == STRING_CST
5743 && expr
.original_code
!= STRING_CST
)
5744 pedwarn_init (input_location
, OPT_pedantic
,
5745 "array initialized from parenthesized string constant");
5748 /* Digest the parser output INIT as an initializer for type TYPE.
5749 Return a C expression of type TYPE to represent the initial value.
5751 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5753 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5755 If INIT is a string constant, STRICT_STRING is true if it is
5756 unparenthesized or we should not warn here for it being parenthesized.
5757 For other types of INIT, STRICT_STRING is not used.
5759 INIT_LOC is the location of the INIT.
5761 REQUIRE_CONSTANT requests an error if non-constant initializers or
5762 elements are seen. */
5765 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
5766 bool null_pointer_constant
, bool strict_string
,
5767 int require_constant
)
5769 enum tree_code code
= TREE_CODE (type
);
5770 tree inside_init
= init
;
5771 tree semantic_type
= NULL_TREE
;
5772 bool maybe_const
= true;
5774 if (type
== error_mark_node
5776 || init
== error_mark_node
5777 || TREE_TYPE (init
) == error_mark_node
)
5778 return error_mark_node
;
5780 STRIP_TYPE_NOPS (inside_init
);
5782 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
5784 semantic_type
= TREE_TYPE (inside_init
);
5785 inside_init
= TREE_OPERAND (inside_init
, 0);
5787 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
5788 inside_init
= decl_constant_value_for_optimization (inside_init
);
5790 /* Initialization of an array of chars from a string constant
5791 optionally enclosed in braces. */
5793 if (code
== ARRAY_TYPE
&& inside_init
5794 && TREE_CODE (inside_init
) == STRING_CST
)
5796 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
5797 /* Note that an array could be both an array of character type
5798 and an array of wchar_t if wchar_t is signed char or unsigned
5800 bool char_array
= (typ1
== char_type_node
5801 || typ1
== signed_char_type_node
5802 || typ1
== unsigned_char_type_node
);
5803 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
5804 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
5805 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
5807 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
5810 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
5811 expr
.value
= inside_init
;
5812 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
5813 expr
.original_type
= NULL
;
5814 maybe_warn_string_init (type
, expr
);
5816 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
5817 pedwarn_init (init_loc
, OPT_pedantic
,
5818 "initialization of a flexible array member");
5820 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
5821 TYPE_MAIN_VARIANT (type
)))
5826 if (typ2
!= char_type_node
)
5828 error_init ("char-array initialized from wide string");
5829 return error_mark_node
;
5834 if (typ2
== char_type_node
)
5836 error_init ("wide character array initialized from non-wide "
5838 return error_mark_node
;
5840 else if (!comptypes(typ1
, typ2
))
5842 error_init ("wide character array initialized from "
5843 "incompatible wide string");
5844 return error_mark_node
;
5848 TREE_TYPE (inside_init
) = type
;
5849 if (TYPE_DOMAIN (type
) != 0
5850 && TYPE_SIZE (type
) != 0
5851 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
5853 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
5855 /* Subtract the size of a single (possibly wide) character
5856 because it's ok to ignore the terminating null char
5857 that is counted in the length of the constant. */
5858 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
5860 - (TYPE_PRECISION (typ1
)
5862 pedwarn_init (init_loc
, 0,
5863 ("initializer-string for array of chars "
5865 else if (warn_cxx_compat
5866 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
5867 warning_at (init_loc
, OPT_Wc___compat
,
5868 ("initializer-string for array chars "
5869 "is too long for C++"));
5874 else if (INTEGRAL_TYPE_P (typ1
))
5876 error_init ("array of inappropriate type initialized "
5877 "from string constant");
5878 return error_mark_node
;
5882 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5883 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5884 below and handle as a constructor. */
5885 if (code
== VECTOR_TYPE
5886 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
5887 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
5888 && TREE_CONSTANT (inside_init
))
5890 if (TREE_CODE (inside_init
) == VECTOR_CST
5891 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
5892 TYPE_MAIN_VARIANT (type
)))
5895 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
5897 unsigned HOST_WIDE_INT ix
;
5899 bool constant_p
= true;
5901 /* Iterate through elements and check if all constructor
5902 elements are *_CSTs. */
5903 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
5904 if (!CONSTANT_CLASS_P (value
))
5911 return build_vector_from_ctor (type
,
5912 CONSTRUCTOR_ELTS (inside_init
));
5916 if (warn_sequence_point
)
5917 verify_sequence_points (inside_init
);
5919 /* Any type can be initialized
5920 from an expression of the same type, optionally with braces. */
5922 if (inside_init
&& TREE_TYPE (inside_init
) != 0
5923 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
5924 TYPE_MAIN_VARIANT (type
))
5925 || (code
== ARRAY_TYPE
5926 && comptypes (TREE_TYPE (inside_init
), type
))
5927 || (code
== VECTOR_TYPE
5928 && comptypes (TREE_TYPE (inside_init
), type
))
5929 || (code
== POINTER_TYPE
5930 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
5931 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
5932 TREE_TYPE (type
)))))
5934 if (code
== POINTER_TYPE
)
5936 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
5938 if (TREE_CODE (inside_init
) == STRING_CST
5939 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
5940 inside_init
= array_to_pointer_conversion
5941 (init_loc
, inside_init
);
5944 error_init ("invalid use of non-lvalue array");
5945 return error_mark_node
;
5950 if (code
== VECTOR_TYPE
)
5951 /* Although the types are compatible, we may require a
5953 inside_init
= convert (type
, inside_init
);
5955 if (require_constant
5956 && (code
== VECTOR_TYPE
|| !flag_isoc99
)
5957 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
5959 /* As an extension, allow initializing objects with static storage
5960 duration with compound literals (which are then treated just as
5961 the brace enclosed list they contain). Also allow this for
5962 vectors, as we can only assign them with compound literals. */
5963 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
5964 inside_init
= DECL_INITIAL (decl
);
5967 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
5968 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
5970 error_init ("array initialized from non-constant array expression");
5971 return error_mark_node
;
5974 /* Compound expressions can only occur here if -pedantic or
5975 -pedantic-errors is specified. In the later case, we always want
5976 an error. In the former case, we simply want a warning. */
5977 if (require_constant
&& pedantic
5978 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
5981 = valid_compound_expr_initializer (inside_init
,
5982 TREE_TYPE (inside_init
));
5983 if (inside_init
== error_mark_node
)
5984 error_init ("initializer element is not constant");
5986 pedwarn_init (init_loc
, OPT_pedantic
,
5987 "initializer element is not constant");
5988 if (flag_pedantic_errors
)
5989 inside_init
= error_mark_node
;
5991 else if (require_constant
5992 && !initializer_constant_valid_p (inside_init
,
5993 TREE_TYPE (inside_init
)))
5995 error_init ("initializer element is not constant");
5996 inside_init
= error_mark_node
;
5998 else if (require_constant
&& !maybe_const
)
5999 pedwarn_init (init_loc
, 0,
6000 "initializer element is not a constant expression");
6002 /* Added to enable additional -Wmissing-format-attribute warnings. */
6003 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
6004 inside_init
= convert_for_assignment (init_loc
, type
, inside_init
,
6006 ic_init
, null_pointer_constant
,
6007 NULL_TREE
, NULL_TREE
, 0);
6011 /* Handle scalar types, including conversions. */
6013 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
6014 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
6015 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
6017 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
6018 && (TREE_CODE (init
) == STRING_CST
6019 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
6020 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
6022 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
6025 = convert_for_assignment (init_loc
, type
, inside_init
, origtype
,
6026 ic_init
, null_pointer_constant
,
6027 NULL_TREE
, NULL_TREE
, 0);
6029 /* Check to see if we have already given an error message. */
6030 if (inside_init
== error_mark_node
)
6032 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
6034 error_init ("initializer element is not constant");
6035 inside_init
= error_mark_node
;
6037 else if (require_constant
6038 && !initializer_constant_valid_p (inside_init
,
6039 TREE_TYPE (inside_init
)))
6041 error_init ("initializer element is not computable at load time");
6042 inside_init
= error_mark_node
;
6044 else if (require_constant
&& !maybe_const
)
6045 pedwarn_init (init_loc
, 0,
6046 "initializer element is not a constant expression");
6051 /* Come here only for records and arrays. */
6053 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
6055 error_init ("variable-sized object may not be initialized");
6056 return error_mark_node
;
6059 error_init ("invalid initializer");
6060 return error_mark_node
;
6063 /* Handle initializers that use braces. */
6065 /* Type of object we are accumulating a constructor for.
6066 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6067 static tree constructor_type
;
6069 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6071 static tree constructor_fields
;
6073 /* For an ARRAY_TYPE, this is the specified index
6074 at which to store the next element we get. */
6075 static tree constructor_index
;
6077 /* For an ARRAY_TYPE, this is the maximum index. */
6078 static tree constructor_max_index
;
6080 /* For a RECORD_TYPE, this is the first field not yet written out. */
6081 static tree constructor_unfilled_fields
;
6083 /* For an ARRAY_TYPE, this is the index of the first element
6084 not yet written out. */
6085 static tree constructor_unfilled_index
;
6087 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6088 This is so we can generate gaps between fields, when appropriate. */
6089 static tree constructor_bit_index
;
6091 /* If we are saving up the elements rather than allocating them,
6092 this is the list of elements so far (in reverse order,
6093 most recent first). */
6094 static VEC(constructor_elt
,gc
) *constructor_elements
;
6096 /* 1 if constructor should be incrementally stored into a constructor chain,
6097 0 if all the elements should be kept in AVL tree. */
6098 static int constructor_incremental
;
6100 /* 1 if so far this constructor's elements are all compile-time constants. */
6101 static int constructor_constant
;
6103 /* 1 if so far this constructor's elements are all valid address constants. */
6104 static int constructor_simple
;
6106 /* 1 if this constructor has an element that cannot be part of a
6107 constant expression. */
6108 static int constructor_nonconst
;
6110 /* 1 if this constructor is erroneous so far. */
6111 static int constructor_erroneous
;
6113 /* Structure for managing pending initializer elements, organized as an
6118 struct init_node
*left
, *right
;
6119 struct init_node
*parent
;
6126 /* Tree of pending elements at this constructor level.
6127 These are elements encountered out of order
6128 which belong at places we haven't reached yet in actually
6130 Will never hold tree nodes across GC runs. */
6131 static struct init_node
*constructor_pending_elts
;
6133 /* The SPELLING_DEPTH of this constructor. */
6134 static int constructor_depth
;
6136 /* DECL node for which an initializer is being read.
6137 0 means we are reading a constructor expression
6138 such as (struct foo) {...}. */
6139 static tree constructor_decl
;
6141 /* Nonzero if this is an initializer for a top-level decl. */
6142 static int constructor_top_level
;
6144 /* Nonzero if there were any member designators in this initializer. */
6145 static int constructor_designated
;
6147 /* Nesting depth of designator list. */
6148 static int designator_depth
;
6150 /* Nonzero if there were diagnosed errors in this designator list. */
6151 static int designator_erroneous
;
6154 /* This stack has a level for each implicit or explicit level of
6155 structuring in the initializer, including the outermost one. It
6156 saves the values of most of the variables above. */
6158 struct constructor_range_stack
;
6160 struct constructor_stack
6162 struct constructor_stack
*next
;
6167 tree unfilled_index
;
6168 tree unfilled_fields
;
6170 VEC(constructor_elt
,gc
) *elements
;
6171 struct init_node
*pending_elts
;
6174 /* If value nonzero, this value should replace the entire
6175 constructor at this level. */
6176 struct c_expr replacement_value
;
6177 struct constructor_range_stack
*range_stack
;
6188 static struct constructor_stack
*constructor_stack
;
6190 /* This stack represents designators from some range designator up to
6191 the last designator in the list. */
6193 struct constructor_range_stack
6195 struct constructor_range_stack
*next
, *prev
;
6196 struct constructor_stack
*stack
;
6203 static struct constructor_range_stack
*constructor_range_stack
;
6205 /* This stack records separate initializers that are nested.
6206 Nested initializers can't happen in ANSI C, but GNU C allows them
6207 in cases like { ... (struct foo) { ... } ... }. */
6209 struct initializer_stack
6211 struct initializer_stack
*next
;
6213 struct constructor_stack
*constructor_stack
;
6214 struct constructor_range_stack
*constructor_range_stack
;
6215 VEC(constructor_elt
,gc
) *elements
;
6216 struct spelling
*spelling
;
6217 struct spelling
*spelling_base
;
6220 char require_constant_value
;
6221 char require_constant_elements
;
6224 static struct initializer_stack
*initializer_stack
;
6226 /* Prepare to parse and output the initializer for variable DECL. */
6229 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
6232 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
6234 p
->decl
= constructor_decl
;
6235 p
->require_constant_value
= require_constant_value
;
6236 p
->require_constant_elements
= require_constant_elements
;
6237 p
->constructor_stack
= constructor_stack
;
6238 p
->constructor_range_stack
= constructor_range_stack
;
6239 p
->elements
= constructor_elements
;
6240 p
->spelling
= spelling
;
6241 p
->spelling_base
= spelling_base
;
6242 p
->spelling_size
= spelling_size
;
6243 p
->top_level
= constructor_top_level
;
6244 p
->next
= initializer_stack
;
6245 initializer_stack
= p
;
6247 constructor_decl
= decl
;
6248 constructor_designated
= 0;
6249 constructor_top_level
= top_level
;
6251 if (decl
!= 0 && decl
!= error_mark_node
)
6253 require_constant_value
= TREE_STATIC (decl
);
6254 require_constant_elements
6255 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
6256 /* For a scalar, you can always use any value to initialize,
6257 even within braces. */
6258 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
6259 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
6260 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
6261 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
6262 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
6266 require_constant_value
= 0;
6267 require_constant_elements
= 0;
6268 locus
= _("(anonymous)");
6271 constructor_stack
= 0;
6272 constructor_range_stack
= 0;
6274 missing_braces_mentioned
= 0;
6278 RESTORE_SPELLING_DEPTH (0);
6281 push_string (locus
);
6287 struct initializer_stack
*p
= initializer_stack
;
6289 /* Free the whole constructor stack of this initializer. */
6290 while (constructor_stack
)
6292 struct constructor_stack
*q
= constructor_stack
;
6293 constructor_stack
= q
->next
;
6297 gcc_assert (!constructor_range_stack
);
6299 /* Pop back to the data of the outer initializer (if any). */
6300 free (spelling_base
);
6302 constructor_decl
= p
->decl
;
6303 require_constant_value
= p
->require_constant_value
;
6304 require_constant_elements
= p
->require_constant_elements
;
6305 constructor_stack
= p
->constructor_stack
;
6306 constructor_range_stack
= p
->constructor_range_stack
;
6307 constructor_elements
= p
->elements
;
6308 spelling
= p
->spelling
;
6309 spelling_base
= p
->spelling_base
;
6310 spelling_size
= p
->spelling_size
;
6311 constructor_top_level
= p
->top_level
;
6312 initializer_stack
= p
->next
;
6316 /* Call here when we see the initializer is surrounded by braces.
6317 This is instead of a call to push_init_level;
6318 it is matched by a call to pop_init_level.
6320 TYPE is the type to initialize, for a constructor expression.
6321 For an initializer for a decl, TYPE is zero. */
6324 really_start_incremental_init (tree type
)
6326 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
6329 type
= TREE_TYPE (constructor_decl
);
6331 if (TREE_CODE (type
) == VECTOR_TYPE
6332 && TYPE_VECTOR_OPAQUE (type
))
6333 error ("opaque vector types cannot be initialized");
6335 p
->type
= constructor_type
;
6336 p
->fields
= constructor_fields
;
6337 p
->index
= constructor_index
;
6338 p
->max_index
= constructor_max_index
;
6339 p
->unfilled_index
= constructor_unfilled_index
;
6340 p
->unfilled_fields
= constructor_unfilled_fields
;
6341 p
->bit_index
= constructor_bit_index
;
6342 p
->elements
= constructor_elements
;
6343 p
->constant
= constructor_constant
;
6344 p
->simple
= constructor_simple
;
6345 p
->nonconst
= constructor_nonconst
;
6346 p
->erroneous
= constructor_erroneous
;
6347 p
->pending_elts
= constructor_pending_elts
;
6348 p
->depth
= constructor_depth
;
6349 p
->replacement_value
.value
= 0;
6350 p
->replacement_value
.original_code
= ERROR_MARK
;
6351 p
->replacement_value
.original_type
= NULL
;
6355 p
->incremental
= constructor_incremental
;
6356 p
->designated
= constructor_designated
;
6358 constructor_stack
= p
;
6360 constructor_constant
= 1;
6361 constructor_simple
= 1;
6362 constructor_nonconst
= 0;
6363 constructor_depth
= SPELLING_DEPTH ();
6364 constructor_elements
= 0;
6365 constructor_pending_elts
= 0;
6366 constructor_type
= type
;
6367 constructor_incremental
= 1;
6368 constructor_designated
= 0;
6369 designator_depth
= 0;
6370 designator_erroneous
= 0;
6372 if (TREE_CODE (constructor_type
) == RECORD_TYPE
6373 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6375 constructor_fields
= TYPE_FIELDS (constructor_type
);
6376 /* Skip any nameless bit fields at the beginning. */
6377 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
6378 && DECL_NAME (constructor_fields
) == 0)
6379 constructor_fields
= TREE_CHAIN (constructor_fields
);
6381 constructor_unfilled_fields
= constructor_fields
;
6382 constructor_bit_index
= bitsize_zero_node
;
6384 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6386 if (TYPE_DOMAIN (constructor_type
))
6388 constructor_max_index
6389 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
6391 /* Detect non-empty initializations of zero-length arrays. */
6392 if (constructor_max_index
== NULL_TREE
6393 && TYPE_SIZE (constructor_type
))
6394 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6396 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6397 to initialize VLAs will cause a proper error; avoid tree
6398 checking errors as well by setting a safe value. */
6399 if (constructor_max_index
6400 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
6401 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6404 = convert (bitsizetype
,
6405 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
6409 constructor_index
= bitsize_zero_node
;
6410 constructor_max_index
= NULL_TREE
;
6413 constructor_unfilled_index
= constructor_index
;
6415 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
6417 /* Vectors are like simple fixed-size arrays. */
6418 constructor_max_index
=
6419 build_int_cst (NULL_TREE
, TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
6420 constructor_index
= bitsize_zero_node
;
6421 constructor_unfilled_index
= constructor_index
;
6425 /* Handle the case of int x = {5}; */
6426 constructor_fields
= constructor_type
;
6427 constructor_unfilled_fields
= constructor_type
;
6431 /* Push down into a subobject, for initialization.
6432 If this is for an explicit set of braces, IMPLICIT is 0.
6433 If it is because the next element belongs at a lower level,
6434 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6437 push_init_level (int implicit
, struct obstack
* braced_init_obstack
)
6439 struct constructor_stack
*p
;
6440 tree value
= NULL_TREE
;
6442 /* If we've exhausted any levels that didn't have braces,
6443 pop them now. If implicit == 1, this will have been done in
6444 process_init_element; do not repeat it here because in the case
6445 of excess initializers for an empty aggregate this leads to an
6446 infinite cycle of popping a level and immediately recreating
6450 while (constructor_stack
->implicit
)
6452 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
6453 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6454 && constructor_fields
== 0)
6455 process_init_element (pop_init_level (1, braced_init_obstack
),
6456 true, braced_init_obstack
);
6457 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
6458 && constructor_max_index
6459 && tree_int_cst_lt (constructor_max_index
,
6461 process_init_element (pop_init_level (1, braced_init_obstack
),
6462 true, braced_init_obstack
);
6468 /* Unless this is an explicit brace, we need to preserve previous
6472 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
6473 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6474 && constructor_fields
)
6475 value
= find_init_member (constructor_fields
, braced_init_obstack
);
6476 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6477 value
= find_init_member (constructor_index
, braced_init_obstack
);
6480 p
= XNEW (struct constructor_stack
);
6481 p
->type
= constructor_type
;
6482 p
->fields
= constructor_fields
;
6483 p
->index
= constructor_index
;
6484 p
->max_index
= constructor_max_index
;
6485 p
->unfilled_index
= constructor_unfilled_index
;
6486 p
->unfilled_fields
= constructor_unfilled_fields
;
6487 p
->bit_index
= constructor_bit_index
;
6488 p
->elements
= constructor_elements
;
6489 p
->constant
= constructor_constant
;
6490 p
->simple
= constructor_simple
;
6491 p
->nonconst
= constructor_nonconst
;
6492 p
->erroneous
= constructor_erroneous
;
6493 p
->pending_elts
= constructor_pending_elts
;
6494 p
->depth
= constructor_depth
;
6495 p
->replacement_value
.value
= 0;
6496 p
->replacement_value
.original_code
= ERROR_MARK
;
6497 p
->replacement_value
.original_type
= NULL
;
6498 p
->implicit
= implicit
;
6500 p
->incremental
= constructor_incremental
;
6501 p
->designated
= constructor_designated
;
6502 p
->next
= constructor_stack
;
6504 constructor_stack
= p
;
6506 constructor_constant
= 1;
6507 constructor_simple
= 1;
6508 constructor_nonconst
= 0;
6509 constructor_depth
= SPELLING_DEPTH ();
6510 constructor_elements
= 0;
6511 constructor_incremental
= 1;
6512 constructor_designated
= 0;
6513 constructor_pending_elts
= 0;
6516 p
->range_stack
= constructor_range_stack
;
6517 constructor_range_stack
= 0;
6518 designator_depth
= 0;
6519 designator_erroneous
= 0;
6522 /* Don't die if an entire brace-pair level is superfluous
6523 in the containing level. */
6524 if (constructor_type
== 0)
6526 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
6527 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6529 /* Don't die if there are extra init elts at the end. */
6530 if (constructor_fields
== 0)
6531 constructor_type
= 0;
6534 constructor_type
= TREE_TYPE (constructor_fields
);
6535 push_member_name (constructor_fields
);
6536 constructor_depth
++;
6539 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6541 constructor_type
= TREE_TYPE (constructor_type
);
6542 push_array_bounds (tree_low_cst (constructor_index
, 1));
6543 constructor_depth
++;
6546 if (constructor_type
== 0)
6548 error_init ("extra brace group at end of initializer");
6549 constructor_fields
= 0;
6550 constructor_unfilled_fields
= 0;
6554 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
6556 constructor_constant
= TREE_CONSTANT (value
);
6557 constructor_simple
= TREE_STATIC (value
);
6558 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
6559 constructor_elements
= CONSTRUCTOR_ELTS (value
);
6560 if (!VEC_empty (constructor_elt
, constructor_elements
)
6561 && (TREE_CODE (constructor_type
) == RECORD_TYPE
6562 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
6563 set_nonincremental_init (braced_init_obstack
);
6566 if (implicit
== 1 && warn_missing_braces
&& !missing_braces_mentioned
)
6568 missing_braces_mentioned
= 1;
6569 warning_init (OPT_Wmissing_braces
, "missing braces around initializer");
6572 if (TREE_CODE (constructor_type
) == RECORD_TYPE
6573 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6575 constructor_fields
= TYPE_FIELDS (constructor_type
);
6576 /* Skip any nameless bit fields at the beginning. */
6577 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
6578 && DECL_NAME (constructor_fields
) == 0)
6579 constructor_fields
= TREE_CHAIN (constructor_fields
);
6581 constructor_unfilled_fields
= constructor_fields
;
6582 constructor_bit_index
= bitsize_zero_node
;
6584 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
6586 /* Vectors are like simple fixed-size arrays. */
6587 constructor_max_index
=
6588 build_int_cst (NULL_TREE
, TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
6589 constructor_index
= convert (bitsizetype
, integer_zero_node
);
6590 constructor_unfilled_index
= constructor_index
;
6592 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6594 if (TYPE_DOMAIN (constructor_type
))
6596 constructor_max_index
6597 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
6599 /* Detect non-empty initializations of zero-length arrays. */
6600 if (constructor_max_index
== NULL_TREE
6601 && TYPE_SIZE (constructor_type
))
6602 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6604 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6605 to initialize VLAs will cause a proper error; avoid tree
6606 checking errors as well by setting a safe value. */
6607 if (constructor_max_index
6608 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
6609 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6612 = convert (bitsizetype
,
6613 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
6616 constructor_index
= bitsize_zero_node
;
6618 constructor_unfilled_index
= constructor_index
;
6619 if (value
&& TREE_CODE (value
) == STRING_CST
)
6621 /* We need to split the char/wchar array into individual
6622 characters, so that we don't have to special case it
6624 set_nonincremental_init_from_string (value
, braced_init_obstack
);
6629 if (constructor_type
!= error_mark_node
)
6630 warning_init (0, "braces around scalar initializer");
6631 constructor_fields
= constructor_type
;
6632 constructor_unfilled_fields
= constructor_type
;
6636 /* At the end of an implicit or explicit brace level,
6637 finish up that level of constructor. If a single expression
6638 with redundant braces initialized that level, return the
6639 c_expr structure for that expression. Otherwise, the original_code
6640 element is set to ERROR_MARK.
6641 If we were outputting the elements as they are read, return 0 as the value
6642 from inner levels (process_init_element ignores that),
6643 but return error_mark_node as the value from the outermost level
6644 (that's what we want to put in DECL_INITIAL).
6645 Otherwise, return a CONSTRUCTOR expression as the value. */
6648 pop_init_level (int implicit
, struct obstack
* braced_init_obstack
)
6650 struct constructor_stack
*p
;
6653 ret
.original_code
= ERROR_MARK
;
6654 ret
.original_type
= NULL
;
6658 /* When we come to an explicit close brace,
6659 pop any inner levels that didn't have explicit braces. */
6660 while (constructor_stack
->implicit
)
6662 process_init_element (pop_init_level (1, braced_init_obstack
),
6663 true, braced_init_obstack
);
6665 gcc_assert (!constructor_range_stack
);
6668 /* Now output all pending elements. */
6669 constructor_incremental
= 1;
6670 output_pending_init_elements (1, braced_init_obstack
);
6672 p
= constructor_stack
;
6674 /* Error for initializing a flexible array member, or a zero-length
6675 array member in an inappropriate context. */
6676 if (constructor_type
&& constructor_fields
6677 && TREE_CODE (constructor_type
) == ARRAY_TYPE
6678 && TYPE_DOMAIN (constructor_type
)
6679 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
6681 /* Silently discard empty initializations. The parser will
6682 already have pedwarned for empty brackets. */
6683 if (integer_zerop (constructor_unfilled_index
))
6684 constructor_type
= NULL_TREE
;
6687 gcc_assert (!TYPE_SIZE (constructor_type
));
6689 if (constructor_depth
> 2)
6690 error_init ("initialization of flexible array member in a nested context");
6692 pedwarn_init (input_location
, OPT_pedantic
,
6693 "initialization of a flexible array member");
6695 /* We have already issued an error message for the existence
6696 of a flexible array member not at the end of the structure.
6697 Discard the initializer so that we do not die later. */
6698 if (TREE_CHAIN (constructor_fields
) != NULL_TREE
)
6699 constructor_type
= NULL_TREE
;
6703 /* Warn when some struct elements are implicitly initialized to zero. */
6704 if (warn_missing_field_initializers
6706 && TREE_CODE (constructor_type
) == RECORD_TYPE
6707 && constructor_unfilled_fields
)
6709 /* Do not warn for flexible array members or zero-length arrays. */
6710 while (constructor_unfilled_fields
6711 && (!DECL_SIZE (constructor_unfilled_fields
)
6712 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
6713 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
6715 /* Do not warn if this level of the initializer uses member
6716 designators; it is likely to be deliberate. */
6717 if (constructor_unfilled_fields
&& !constructor_designated
)
6719 push_member_name (constructor_unfilled_fields
);
6720 warning_init (OPT_Wmissing_field_initializers
,
6721 "missing initializer");
6722 RESTORE_SPELLING_DEPTH (constructor_depth
);
6726 /* Pad out the end of the structure. */
6727 if (p
->replacement_value
.value
)
6728 /* If this closes a superfluous brace pair,
6729 just pass out the element between them. */
6730 ret
= p
->replacement_value
;
6731 else if (constructor_type
== 0)
6733 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
6734 && TREE_CODE (constructor_type
) != UNION_TYPE
6735 && TREE_CODE (constructor_type
) != ARRAY_TYPE
6736 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
6738 /* A nonincremental scalar initializer--just return
6739 the element, after verifying there is just one. */
6740 if (VEC_empty (constructor_elt
,constructor_elements
))
6742 if (!constructor_erroneous
)
6743 error_init ("empty scalar initializer");
6744 ret
.value
= error_mark_node
;
6746 else if (VEC_length (constructor_elt
,constructor_elements
) != 1)
6748 error_init ("extra elements in scalar initializer");
6749 ret
.value
= VEC_index (constructor_elt
,constructor_elements
,0)->value
;
6752 ret
.value
= VEC_index (constructor_elt
,constructor_elements
,0)->value
;
6756 if (constructor_erroneous
)
6757 ret
.value
= error_mark_node
;
6760 ret
.value
= build_constructor (constructor_type
,
6761 constructor_elements
);
6762 if (constructor_constant
)
6763 TREE_CONSTANT (ret
.value
) = 1;
6764 if (constructor_constant
&& constructor_simple
)
6765 TREE_STATIC (ret
.value
) = 1;
6766 if (constructor_nonconst
)
6767 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
6771 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
6773 if (constructor_nonconst
)
6774 ret
.original_code
= C_MAYBE_CONST_EXPR
;
6775 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
6776 ret
.original_code
= ERROR_MARK
;
6779 constructor_type
= p
->type
;
6780 constructor_fields
= p
->fields
;
6781 constructor_index
= p
->index
;
6782 constructor_max_index
= p
->max_index
;
6783 constructor_unfilled_index
= p
->unfilled_index
;
6784 constructor_unfilled_fields
= p
->unfilled_fields
;
6785 constructor_bit_index
= p
->bit_index
;
6786 constructor_elements
= p
->elements
;
6787 constructor_constant
= p
->constant
;
6788 constructor_simple
= p
->simple
;
6789 constructor_nonconst
= p
->nonconst
;
6790 constructor_erroneous
= p
->erroneous
;
6791 constructor_incremental
= p
->incremental
;
6792 constructor_designated
= p
->designated
;
6793 constructor_pending_elts
= p
->pending_elts
;
6794 constructor_depth
= p
->depth
;
6796 constructor_range_stack
= p
->range_stack
;
6797 RESTORE_SPELLING_DEPTH (constructor_depth
);
6799 constructor_stack
= p
->next
;
6802 if (ret
.value
== 0 && constructor_stack
== 0)
6803 ret
.value
= error_mark_node
;
6807 /* Common handling for both array range and field name designators.
6808 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6811 set_designator (int array
, struct obstack
* braced_init_obstack
)
6814 enum tree_code subcode
;
6816 /* Don't die if an entire brace-pair level is superfluous
6817 in the containing level. */
6818 if (constructor_type
== 0)
6821 /* If there were errors in this designator list already, bail out
6823 if (designator_erroneous
)
6826 if (!designator_depth
)
6828 gcc_assert (!constructor_range_stack
);
6830 /* Designator list starts at the level of closest explicit
6832 while (constructor_stack
->implicit
)
6834 process_init_element (pop_init_level (1, braced_init_obstack
),
6835 true, braced_init_obstack
);
6837 constructor_designated
= 1;
6841 switch (TREE_CODE (constructor_type
))
6845 subtype
= TREE_TYPE (constructor_fields
);
6846 if (subtype
!= error_mark_node
)
6847 subtype
= TYPE_MAIN_VARIANT (subtype
);
6850 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
6856 subcode
= TREE_CODE (subtype
);
6857 if (array
&& subcode
!= ARRAY_TYPE
)
6859 error_init ("array index in non-array initializer");
6862 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
6864 error_init ("field name not in record or union initializer");
6868 constructor_designated
= 1;
6869 push_init_level (2, braced_init_obstack
);
6873 /* If there are range designators in designator list, push a new designator
6874 to constructor_range_stack. RANGE_END is end of such stack range or
6875 NULL_TREE if there is no range designator at this level. */
6878 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
6880 struct constructor_range_stack
*p
;
6882 p
= (struct constructor_range_stack
*)
6883 obstack_alloc (braced_init_obstack
,
6884 sizeof (struct constructor_range_stack
));
6885 p
->prev
= constructor_range_stack
;
6887 p
->fields
= constructor_fields
;
6888 p
->range_start
= constructor_index
;
6889 p
->index
= constructor_index
;
6890 p
->stack
= constructor_stack
;
6891 p
->range_end
= range_end
;
6892 if (constructor_range_stack
)
6893 constructor_range_stack
->next
= p
;
6894 constructor_range_stack
= p
;
6897 /* Within an array initializer, specify the next index to be initialized.
6898 FIRST is that index. If LAST is nonzero, then initialize a range
6899 of indices, running from FIRST through LAST. */
6902 set_init_index (tree first
, tree last
,
6903 struct obstack
* braced_init_obstack
)
6905 if (set_designator (1, braced_init_obstack
))
6908 designator_erroneous
= 1;
6910 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
6911 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
6913 error_init ("array index in initializer not of integer type");
6917 if (TREE_CODE (first
) != INTEGER_CST
)
6919 first
= c_fully_fold (first
, false, NULL
);
6920 if (TREE_CODE (first
) == INTEGER_CST
)
6921 pedwarn_init (input_location
, OPT_pedantic
,
6922 "array index in initializer is not "
6923 "an integer constant expression");
6926 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
6928 last
= c_fully_fold (last
, false, NULL
);
6929 if (TREE_CODE (last
) == INTEGER_CST
)
6930 pedwarn_init (input_location
, OPT_pedantic
,
6931 "array index in initializer is not "
6932 "an integer constant expression");
6935 if (TREE_CODE (first
) != INTEGER_CST
)
6936 error_init ("nonconstant array index in initializer");
6937 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
6938 error_init ("nonconstant array index in initializer");
6939 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
6940 error_init ("array index in non-array initializer");
6941 else if (tree_int_cst_sgn (first
) == -1)
6942 error_init ("array index in initializer exceeds array bounds");
6943 else if (constructor_max_index
6944 && tree_int_cst_lt (constructor_max_index
, first
))
6945 error_init ("array index in initializer exceeds array bounds");
6948 constant_expression_warning (first
);
6950 constant_expression_warning (last
);
6951 constructor_index
= convert (bitsizetype
, first
);
6955 if (tree_int_cst_equal (first
, last
))
6957 else if (tree_int_cst_lt (last
, first
))
6959 error_init ("empty index range in initializer");
6964 last
= convert (bitsizetype
, last
);
6965 if (constructor_max_index
!= 0
6966 && tree_int_cst_lt (constructor_max_index
, last
))
6968 error_init ("array index range in initializer exceeds array bounds");
6975 designator_erroneous
= 0;
6976 if (constructor_range_stack
|| last
)
6977 push_range_stack (last
, braced_init_obstack
);
6981 /* Within a struct initializer, specify the next field to be initialized. */
6984 set_init_label (tree fieldname
, struct obstack
* braced_init_obstack
)
6988 if (set_designator (0, braced_init_obstack
))
6991 designator_erroneous
= 1;
6993 if (TREE_CODE (constructor_type
) != RECORD_TYPE
6994 && TREE_CODE (constructor_type
) != UNION_TYPE
)
6996 error_init ("field name not in record or union initializer");
7000 for (tail
= TYPE_FIELDS (constructor_type
); tail
;
7001 tail
= TREE_CHAIN (tail
))
7003 if (DECL_NAME (tail
) == fieldname
)
7008 error ("unknown field %qE specified in initializer", fieldname
);
7011 constructor_fields
= tail
;
7013 designator_erroneous
= 0;
7014 if (constructor_range_stack
)
7015 push_range_stack (NULL_TREE
, braced_init_obstack
);
7019 /* Add a new initializer to the tree of pending initializers. PURPOSE
7020 identifies the initializer, either array index or field in a structure.
7021 VALUE is the value of that index or field. If ORIGTYPE is not
7022 NULL_TREE, it is the original type of VALUE.
7024 IMPLICIT is true if value comes from pop_init_level (1),
7025 the new initializer has been merged with the existing one
7026 and thus no warnings should be emitted about overriding an
7027 existing initializer. */
7030 add_pending_init (tree purpose
, tree value
, tree origtype
, bool implicit
,
7031 struct obstack
* braced_init_obstack
)
7033 struct init_node
*p
, **q
, *r
;
7035 q
= &constructor_pending_elts
;
7038 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7043 if (tree_int_cst_lt (purpose
, p
->purpose
))
7045 else if (tree_int_cst_lt (p
->purpose
, purpose
))
7051 if (TREE_SIDE_EFFECTS (p
->value
))
7052 warning_init (0, "initialized field with side-effects overwritten");
7053 else if (warn_override_init
)
7054 warning_init (OPT_Woverride_init
, "initialized field overwritten");
7057 p
->origtype
= origtype
;
7066 bitpos
= bit_position (purpose
);
7070 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
7072 else if (p
->purpose
!= purpose
)
7078 if (TREE_SIDE_EFFECTS (p
->value
))
7079 warning_init (0, "initialized field with side-effects overwritten");
7080 else if (warn_override_init
)
7081 warning_init (OPT_Woverride_init
, "initialized field overwritten");
7084 p
->origtype
= origtype
;
7090 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
7091 sizeof (struct init_node
));
7092 r
->purpose
= purpose
;
7094 r
->origtype
= origtype
;
7104 struct init_node
*s
;
7108 if (p
->balance
== 0)
7110 else if (p
->balance
< 0)
7117 p
->left
->parent
= p
;
7134 constructor_pending_elts
= r
;
7139 struct init_node
*t
= r
->right
;
7143 r
->right
->parent
= r
;
7148 p
->left
->parent
= p
;
7151 p
->balance
= t
->balance
< 0;
7152 r
->balance
= -(t
->balance
> 0);
7167 constructor_pending_elts
= t
;
7173 /* p->balance == +1; growth of left side balances the node. */
7178 else /* r == p->right */
7180 if (p
->balance
== 0)
7181 /* Growth propagation from right side. */
7183 else if (p
->balance
> 0)
7190 p
->right
->parent
= p
;
7207 constructor_pending_elts
= r
;
7209 else /* r->balance == -1 */
7212 struct init_node
*t
= r
->left
;
7216 r
->left
->parent
= r
;
7221 p
->right
->parent
= p
;
7224 r
->balance
= (t
->balance
< 0);
7225 p
->balance
= -(t
->balance
> 0);
7240 constructor_pending_elts
= t
;
7246 /* p->balance == -1; growth of right side balances the node. */
7257 /* Build AVL tree from a sorted chain. */
7260 set_nonincremental_init (struct obstack
* braced_init_obstack
)
7262 unsigned HOST_WIDE_INT ix
;
7265 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7266 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
7269 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
7271 add_pending_init (index
, value
, NULL_TREE
, false,
7272 braced_init_obstack
);
7274 constructor_elements
= 0;
7275 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7277 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
7278 /* Skip any nameless bit fields at the beginning. */
7279 while (constructor_unfilled_fields
!= 0
7280 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
7281 && DECL_NAME (constructor_unfilled_fields
) == 0)
7282 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
7285 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7287 if (TYPE_DOMAIN (constructor_type
))
7288 constructor_unfilled_index
7289 = convert (bitsizetype
,
7290 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7292 constructor_unfilled_index
= bitsize_zero_node
;
7294 constructor_incremental
= 0;
7297 /* Build AVL tree from a string constant. */
7300 set_nonincremental_init_from_string (tree str
,
7301 struct obstack
* braced_init_obstack
)
7303 tree value
, purpose
, type
;
7304 HOST_WIDE_INT val
[2];
7305 const char *p
, *end
;
7306 int byte
, wchar_bytes
, charwidth
, bitpos
;
7308 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
7310 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
7311 charwidth
= TYPE_PRECISION (char_type_node
);
7312 type
= TREE_TYPE (constructor_type
);
7313 p
= TREE_STRING_POINTER (str
);
7314 end
= p
+ TREE_STRING_LENGTH (str
);
7316 for (purpose
= bitsize_zero_node
;
7317 p
< end
&& !tree_int_cst_lt (constructor_max_index
, purpose
);
7318 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
7320 if (wchar_bytes
== 1)
7322 val
[1] = (unsigned char) *p
++;
7329 for (byte
= 0; byte
< wchar_bytes
; byte
++)
7331 if (BYTES_BIG_ENDIAN
)
7332 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
7334 bitpos
= byte
* charwidth
;
7335 val
[bitpos
< HOST_BITS_PER_WIDE_INT
]
7336 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
7337 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
7341 if (!TYPE_UNSIGNED (type
))
7343 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
7344 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
7346 if (val
[1] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
7348 val
[1] |= ((HOST_WIDE_INT
) -1) << bitpos
;
7352 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
7357 else if (val
[0] & (((HOST_WIDE_INT
) 1)
7358 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
7359 val
[0] |= ((HOST_WIDE_INT
) -1)
7360 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
7363 value
= build_int_cst_wide (type
, val
[1], val
[0]);
7364 add_pending_init (purpose
, value
, NULL_TREE
, false,
7365 braced_init_obstack
);
7368 constructor_incremental
= 0;
7371 /* Return value of FIELD in pending initializer or zero if the field was
7372 not initialized yet. */
7375 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
7377 struct init_node
*p
;
7379 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7381 if (constructor_incremental
7382 && tree_int_cst_lt (field
, constructor_unfilled_index
))
7383 set_nonincremental_init (braced_init_obstack
);
7385 p
= constructor_pending_elts
;
7388 if (tree_int_cst_lt (field
, p
->purpose
))
7390 else if (tree_int_cst_lt (p
->purpose
, field
))
7396 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7398 tree bitpos
= bit_position (field
);
7400 if (constructor_incremental
7401 && (!constructor_unfilled_fields
7402 || tree_int_cst_lt (bitpos
,
7403 bit_position (constructor_unfilled_fields
))))
7404 set_nonincremental_init (braced_init_obstack
);
7406 p
= constructor_pending_elts
;
7409 if (field
== p
->purpose
)
7411 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
7417 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
7419 if (!VEC_empty (constructor_elt
, constructor_elements
)
7420 && (VEC_last (constructor_elt
, constructor_elements
)->index
7422 return VEC_last (constructor_elt
, constructor_elements
)->value
;
7427 /* "Output" the next constructor element.
7428 At top level, really output it to assembler code now.
7429 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7430 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7431 TYPE is the data type that the containing data type wants here.
7432 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7433 If VALUE is a string constant, STRICT_STRING is true if it is
7434 unparenthesized or we should not warn here for it being parenthesized.
7435 For other types of VALUE, STRICT_STRING is not used.
7437 PENDING if non-nil means output pending elements that belong
7438 right after this element. (PENDING is normally 1;
7439 it is 0 while outputting pending elements, to avoid recursion.)
7441 IMPLICIT is true if value comes from pop_init_level (1),
7442 the new initializer has been merged with the existing one
7443 and thus no warnings should be emitted about overriding an
7444 existing initializer. */
7447 output_init_element (tree value
, tree origtype
, bool strict_string
, tree type
,
7448 tree field
, int pending
, bool implicit
,
7449 struct obstack
* braced_init_obstack
)
7451 tree semantic_type
= NULL_TREE
;
7452 constructor_elt
*celt
;
7453 bool maybe_const
= true;
7456 if (type
== error_mark_node
|| value
== error_mark_node
)
7458 constructor_erroneous
= 1;
7461 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
7462 && (TREE_CODE (value
) == STRING_CST
7463 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
7464 && !(TREE_CODE (value
) == STRING_CST
7465 && TREE_CODE (type
) == ARRAY_TYPE
7466 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
7467 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
7468 TYPE_MAIN_VARIANT (type
)))
7469 value
= array_to_pointer_conversion (input_location
, value
);
7471 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
7472 && require_constant_value
&& !flag_isoc99
&& pending
)
7474 /* As an extension, allow initializing objects with static storage
7475 duration with compound literals (which are then treated just as
7476 the brace enclosed list they contain). */
7477 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
7478 value
= DECL_INITIAL (decl
);
7481 npc
= null_pointer_constant_p (value
);
7482 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
7484 semantic_type
= TREE_TYPE (value
);
7485 value
= TREE_OPERAND (value
, 0);
7487 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
7489 if (value
== error_mark_node
)
7490 constructor_erroneous
= 1;
7491 else if (!TREE_CONSTANT (value
))
7492 constructor_constant
= 0;
7493 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
7494 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
7495 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7496 && DECL_C_BIT_FIELD (field
)
7497 && TREE_CODE (value
) != INTEGER_CST
))
7498 constructor_simple
= 0;
7500 constructor_nonconst
= 1;
7502 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
7504 if (require_constant_value
)
7506 error_init ("initializer element is not constant");
7507 value
= error_mark_node
;
7509 else if (require_constant_elements
)
7510 pedwarn (input_location
, 0,
7511 "initializer element is not computable at load time");
7513 else if (!maybe_const
7514 && (require_constant_value
|| require_constant_elements
))
7515 pedwarn_init (input_location
, 0,
7516 "initializer element is not a constant expression");
7518 /* Issue -Wc++-compat warnings about initializing a bitfield with
7521 && field
!= NULL_TREE
7522 && TREE_CODE (field
) == FIELD_DECL
7523 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
7524 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
7525 != TYPE_MAIN_VARIANT (type
))
7526 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
7528 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
7529 if (checktype
!= error_mark_node
7530 && (TYPE_MAIN_VARIANT (checktype
)
7531 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
7532 warning_init (OPT_Wc___compat
,
7533 "enum conversion in initialization is invalid in C++");
7536 /* If this field is empty (and not at the end of structure),
7537 don't do anything other than checking the initializer. */
7539 && (TREE_TYPE (field
) == error_mark_node
7540 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
7541 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
7542 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
7543 || TREE_CHAIN (field
)))))
7547 value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
7548 value
= digest_init (input_location
, type
, value
, origtype
, npc
,
7549 strict_string
, require_constant_value
);
7550 if (value
== error_mark_node
)
7552 constructor_erroneous
= 1;
7555 if (require_constant_value
|| require_constant_elements
)
7556 constant_expression_warning (value
);
7558 /* If this element doesn't come next in sequence,
7559 put it on constructor_pending_elts. */
7560 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7561 && (!constructor_incremental
7562 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
7564 if (constructor_incremental
7565 && tree_int_cst_lt (field
, constructor_unfilled_index
))
7566 set_nonincremental_init (braced_init_obstack
);
7568 add_pending_init (field
, value
, origtype
, implicit
,
7569 braced_init_obstack
);
7572 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7573 && (!constructor_incremental
7574 || field
!= constructor_unfilled_fields
))
7576 /* We do this for records but not for unions. In a union,
7577 no matter which field is specified, it can be initialized
7578 right away since it starts at the beginning of the union. */
7579 if (constructor_incremental
)
7581 if (!constructor_unfilled_fields
)
7582 set_nonincremental_init (braced_init_obstack
);
7585 tree bitpos
, unfillpos
;
7587 bitpos
= bit_position (field
);
7588 unfillpos
= bit_position (constructor_unfilled_fields
);
7590 if (tree_int_cst_lt (bitpos
, unfillpos
))
7591 set_nonincremental_init (braced_init_obstack
);
7595 add_pending_init (field
, value
, origtype
, implicit
,
7596 braced_init_obstack
);
7599 else if (TREE_CODE (constructor_type
) == UNION_TYPE
7600 && !VEC_empty (constructor_elt
, constructor_elements
))
7604 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt
,
7605 constructor_elements
)->value
))
7607 "initialized field with side-effects overwritten");
7608 else if (warn_override_init
)
7609 warning_init (OPT_Woverride_init
, "initialized field overwritten");
7612 /* We can have just one union field set. */
7613 constructor_elements
= 0;
7616 /* Otherwise, output this element either to
7617 constructor_elements or to the assembler file. */
7619 celt
= VEC_safe_push (constructor_elt
, gc
, constructor_elements
, NULL
);
7620 celt
->index
= field
;
7621 celt
->value
= value
;
7623 /* Advance the variable that indicates sequential elements output. */
7624 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7625 constructor_unfilled_index
7626 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
7628 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7630 constructor_unfilled_fields
7631 = TREE_CHAIN (constructor_unfilled_fields
);
7633 /* Skip any nameless bit fields. */
7634 while (constructor_unfilled_fields
!= 0
7635 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
7636 && DECL_NAME (constructor_unfilled_fields
) == 0)
7637 constructor_unfilled_fields
=
7638 TREE_CHAIN (constructor_unfilled_fields
);
7640 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
7641 constructor_unfilled_fields
= 0;
7643 /* Now output any pending elements which have become next. */
7645 output_pending_init_elements (0, braced_init_obstack
);
7648 /* Output any pending elements which have become next.
7649 As we output elements, constructor_unfilled_{fields,index}
7650 advances, which may cause other elements to become next;
7651 if so, they too are output.
7653 If ALL is 0, we return when there are
7654 no more pending elements to output now.
7656 If ALL is 1, we output space as necessary so that
7657 we can output all the pending elements. */
7659 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
7661 struct init_node
*elt
= constructor_pending_elts
;
7666 /* Look through the whole pending tree.
7667 If we find an element that should be output now,
7668 output it. Otherwise, set NEXT to the element
7669 that comes first among those still pending. */
7674 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7676 if (tree_int_cst_equal (elt
->purpose
,
7677 constructor_unfilled_index
))
7678 output_init_element (elt
->value
, elt
->origtype
, true,
7679 TREE_TYPE (constructor_type
),
7680 constructor_unfilled_index
, 0, false,
7681 braced_init_obstack
);
7682 else if (tree_int_cst_lt (constructor_unfilled_index
,
7685 /* Advance to the next smaller node. */
7690 /* We have reached the smallest node bigger than the
7691 current unfilled index. Fill the space first. */
7692 next
= elt
->purpose
;
7698 /* Advance to the next bigger node. */
7703 /* We have reached the biggest node in a subtree. Find
7704 the parent of it, which is the next bigger node. */
7705 while (elt
->parent
&& elt
->parent
->right
== elt
)
7708 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
7711 next
= elt
->purpose
;
7717 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7718 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7720 tree ctor_unfilled_bitpos
, elt_bitpos
;
7722 /* If the current record is complete we are done. */
7723 if (constructor_unfilled_fields
== 0)
7726 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
7727 elt_bitpos
= bit_position (elt
->purpose
);
7728 /* We can't compare fields here because there might be empty
7729 fields in between. */
7730 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
7732 constructor_unfilled_fields
= elt
->purpose
;
7733 output_init_element (elt
->value
, elt
->origtype
, true,
7734 TREE_TYPE (elt
->purpose
),
7735 elt
->purpose
, 0, false,
7736 braced_init_obstack
);
7738 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
7740 /* Advance to the next smaller node. */
7745 /* We have reached the smallest node bigger than the
7746 current unfilled field. Fill the space first. */
7747 next
= elt
->purpose
;
7753 /* Advance to the next bigger node. */
7758 /* We have reached the biggest node in a subtree. Find
7759 the parent of it, which is the next bigger node. */
7760 while (elt
->parent
&& elt
->parent
->right
== elt
)
7764 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
7765 bit_position (elt
->purpose
))))
7767 next
= elt
->purpose
;
7775 /* Ordinarily return, but not if we want to output all
7776 and there are elements left. */
7777 if (!(all
&& next
!= 0))
7780 /* If it's not incremental, just skip over the gap, so that after
7781 jumping to retry we will output the next successive element. */
7782 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7783 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7784 constructor_unfilled_fields
= next
;
7785 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7786 constructor_unfilled_index
= next
;
7788 /* ELT now points to the node in the pending tree with the next
7789 initializer to output. */
7793 /* Add one non-braced element to the current constructor level.
7794 This adjusts the current position within the constructor's type.
7795 This may also start or terminate implicit levels
7796 to handle a partly-braced initializer.
7798 Once this has found the correct level for the new element,
7799 it calls output_init_element.
7801 IMPLICIT is true if value comes from pop_init_level (1),
7802 the new initializer has been merged with the existing one
7803 and thus no warnings should be emitted about overriding an
7804 existing initializer. */
7807 process_init_element (struct c_expr value
, bool implicit
,
7808 struct obstack
* braced_init_obstack
)
7810 tree orig_value
= value
.value
;
7811 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
7812 bool strict_string
= value
.original_code
== STRING_CST
;
7814 designator_depth
= 0;
7815 designator_erroneous
= 0;
7817 /* Handle superfluous braces around string cst as in
7818 char x[] = {"foo"}; */
7821 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7822 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
7823 && integer_zerop (constructor_unfilled_index
))
7825 if (constructor_stack
->replacement_value
.value
)
7826 error_init ("excess elements in char array initializer");
7827 constructor_stack
->replacement_value
= value
;
7831 if (constructor_stack
->replacement_value
.value
!= 0)
7833 error_init ("excess elements in struct initializer");
7837 /* Ignore elements of a brace group if it is entirely superfluous
7838 and has already been diagnosed. */
7839 if (constructor_type
== 0)
7842 /* If we've exhausted any levels that didn't have braces,
7844 while (constructor_stack
->implicit
)
7846 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7847 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7848 && constructor_fields
== 0)
7849 process_init_element (pop_init_level (1, braced_init_obstack
),
7850 true, braced_init_obstack
);
7851 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
7852 || TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7853 && (constructor_max_index
== 0
7854 || tree_int_cst_lt (constructor_max_index
,
7855 constructor_index
)))
7856 process_init_element (pop_init_level (1, braced_init_obstack
),
7857 true, braced_init_obstack
);
7862 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7863 if (constructor_range_stack
)
7865 /* If value is a compound literal and we'll be just using its
7866 content, don't put it into a SAVE_EXPR. */
7867 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
7868 || !require_constant_value
7871 tree semantic_type
= NULL_TREE
;
7872 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
7874 semantic_type
= TREE_TYPE (value
.value
);
7875 value
.value
= TREE_OPERAND (value
.value
, 0);
7877 value
.value
= c_save_expr (value
.value
);
7879 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
7886 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7889 enum tree_code fieldcode
;
7891 if (constructor_fields
== 0)
7893 pedwarn_init (input_location
, 0,
7894 "excess elements in struct initializer");
7898 fieldtype
= TREE_TYPE (constructor_fields
);
7899 if (fieldtype
!= error_mark_node
)
7900 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
7901 fieldcode
= TREE_CODE (fieldtype
);
7903 /* Error for non-static initialization of a flexible array member. */
7904 if (fieldcode
== ARRAY_TYPE
7905 && !require_constant_value
7906 && TYPE_SIZE (fieldtype
) == NULL_TREE
7907 && TREE_CHAIN (constructor_fields
) == NULL_TREE
)
7909 error_init ("non-static initialization of a flexible array member");
7913 /* Accept a string constant to initialize a subarray. */
7914 if (value
.value
!= 0
7915 && fieldcode
== ARRAY_TYPE
7916 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
7918 value
.value
= orig_value
;
7919 /* Otherwise, if we have come to a subaggregate,
7920 and we don't have an element of its type, push into it. */
7921 else if (value
.value
!= 0
7922 && value
.value
!= error_mark_node
7923 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
7924 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
7925 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
7927 push_init_level (1, braced_init_obstack
);
7933 push_member_name (constructor_fields
);
7934 output_init_element (value
.value
, value
.original_type
,
7935 strict_string
, fieldtype
,
7936 constructor_fields
, 1, implicit
,
7937 braced_init_obstack
);
7938 RESTORE_SPELLING_DEPTH (constructor_depth
);
7941 /* Do the bookkeeping for an element that was
7942 directly output as a constructor. */
7944 /* For a record, keep track of end position of last field. */
7945 if (DECL_SIZE (constructor_fields
))
7946 constructor_bit_index
7947 = size_binop_loc (input_location
, PLUS_EXPR
,
7948 bit_position (constructor_fields
),
7949 DECL_SIZE (constructor_fields
));
7951 /* If the current field was the first one not yet written out,
7952 it isn't now, so update. */
7953 if (constructor_unfilled_fields
== constructor_fields
)
7955 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
7956 /* Skip any nameless bit fields. */
7957 while (constructor_unfilled_fields
!= 0
7958 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
7959 && DECL_NAME (constructor_unfilled_fields
) == 0)
7960 constructor_unfilled_fields
=
7961 TREE_CHAIN (constructor_unfilled_fields
);
7965 constructor_fields
= TREE_CHAIN (constructor_fields
);
7966 /* Skip any nameless bit fields at the beginning. */
7967 while (constructor_fields
!= 0
7968 && DECL_C_BIT_FIELD (constructor_fields
)
7969 && DECL_NAME (constructor_fields
) == 0)
7970 constructor_fields
= TREE_CHAIN (constructor_fields
);
7972 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
7975 enum tree_code fieldcode
;
7977 if (constructor_fields
== 0)
7979 pedwarn_init (input_location
, 0,
7980 "excess elements in union initializer");
7984 fieldtype
= TREE_TYPE (constructor_fields
);
7985 if (fieldtype
!= error_mark_node
)
7986 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
7987 fieldcode
= TREE_CODE (fieldtype
);
7989 /* Warn that traditional C rejects initialization of unions.
7990 We skip the warning if the value is zero. This is done
7991 under the assumption that the zero initializer in user
7992 code appears conditioned on e.g. __STDC__ to avoid
7993 "missing initializer" warnings and relies on default
7994 initialization to zero in the traditional C case.
7995 We also skip the warning if the initializer is designated,
7996 again on the assumption that this must be conditional on
7997 __STDC__ anyway (and we've already complained about the
7998 member-designator already). */
7999 if (!in_system_header
&& !constructor_designated
8000 && !(value
.value
&& (integer_zerop (value
.value
)
8001 || real_zerop (value
.value
))))
8002 warning (OPT_Wtraditional
, "traditional C rejects initialization "
8005 /* Accept a string constant to initialize a subarray. */
8006 if (value
.value
!= 0
8007 && fieldcode
== ARRAY_TYPE
8008 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8010 value
.value
= orig_value
;
8011 /* Otherwise, if we have come to a subaggregate,
8012 and we don't have an element of its type, push into it. */
8013 else if (value
.value
!= 0
8014 && value
.value
!= error_mark_node
8015 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8016 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8017 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8019 push_init_level (1, braced_init_obstack
);
8025 push_member_name (constructor_fields
);
8026 output_init_element (value
.value
, value
.original_type
,
8027 strict_string
, fieldtype
,
8028 constructor_fields
, 1, implicit
,
8029 braced_init_obstack
);
8030 RESTORE_SPELLING_DEPTH (constructor_depth
);
8033 /* Do the bookkeeping for an element that was
8034 directly output as a constructor. */
8036 constructor_bit_index
= DECL_SIZE (constructor_fields
);
8037 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
8040 constructor_fields
= 0;
8042 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8044 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8045 enum tree_code eltcode
= TREE_CODE (elttype
);
8047 /* Accept a string constant to initialize a subarray. */
8048 if (value
.value
!= 0
8049 && eltcode
== ARRAY_TYPE
8050 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
8052 value
.value
= orig_value
;
8053 /* Otherwise, if we have come to a subaggregate,
8054 and we don't have an element of its type, push into it. */
8055 else if (value
.value
!= 0
8056 && value
.value
!= error_mark_node
8057 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
8058 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
8059 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
8061 push_init_level (1, braced_init_obstack
);
8065 if (constructor_max_index
!= 0
8066 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
8067 || integer_all_onesp (constructor_max_index
)))
8069 pedwarn_init (input_location
, 0,
8070 "excess elements in array initializer");
8074 /* Now output the actual element. */
8077 push_array_bounds (tree_low_cst (constructor_index
, 1));
8078 output_init_element (value
.value
, value
.original_type
,
8079 strict_string
, elttype
,
8080 constructor_index
, 1, implicit
,
8081 braced_init_obstack
);
8082 RESTORE_SPELLING_DEPTH (constructor_depth
);
8086 = size_binop_loc (input_location
, PLUS_EXPR
,
8087 constructor_index
, bitsize_one_node
);
8090 /* If we are doing the bookkeeping for an element that was
8091 directly output as a constructor, we must update
8092 constructor_unfilled_index. */
8093 constructor_unfilled_index
= constructor_index
;
8095 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8097 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8099 /* Do a basic check of initializer size. Note that vectors
8100 always have a fixed size derived from their type. */
8101 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
8103 pedwarn_init (input_location
, 0,
8104 "excess elements in vector initializer");
8108 /* Now output the actual element. */
8111 if (TREE_CODE (value
.value
) == VECTOR_CST
)
8112 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
8113 output_init_element (value
.value
, value
.original_type
,
8114 strict_string
, elttype
,
8115 constructor_index
, 1, implicit
,
8116 braced_init_obstack
);
8120 = size_binop_loc (input_location
,
8121 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
8124 /* If we are doing the bookkeeping for an element that was
8125 directly output as a constructor, we must update
8126 constructor_unfilled_index. */
8127 constructor_unfilled_index
= constructor_index
;
8130 /* Handle the sole element allowed in a braced initializer
8131 for a scalar variable. */
8132 else if (constructor_type
!= error_mark_node
8133 && constructor_fields
== 0)
8135 pedwarn_init (input_location
, 0,
8136 "excess elements in scalar initializer");
8142 output_init_element (value
.value
, value
.original_type
,
8143 strict_string
, constructor_type
,
8144 NULL_TREE
, 1, implicit
,
8145 braced_init_obstack
);
8146 constructor_fields
= 0;
8149 /* Handle range initializers either at this level or anywhere higher
8150 in the designator stack. */
8151 if (constructor_range_stack
)
8153 struct constructor_range_stack
*p
, *range_stack
;
8156 range_stack
= constructor_range_stack
;
8157 constructor_range_stack
= 0;
8158 while (constructor_stack
!= range_stack
->stack
)
8160 gcc_assert (constructor_stack
->implicit
);
8161 process_init_element (pop_init_level (1,
8162 braced_init_obstack
),
8163 true, braced_init_obstack
);
8165 for (p
= range_stack
;
8166 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
8169 gcc_assert (constructor_stack
->implicit
);
8170 process_init_element (pop_init_level (1, braced_init_obstack
),
8171 true, braced_init_obstack
);
8174 p
->index
= size_binop_loc (input_location
,
8175 PLUS_EXPR
, p
->index
, bitsize_one_node
);
8176 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
8181 constructor_index
= p
->index
;
8182 constructor_fields
= p
->fields
;
8183 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
8191 push_init_level (2, braced_init_obstack
);
8192 p
->stack
= constructor_stack
;
8193 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
8194 p
->index
= p
->range_start
;
8198 constructor_range_stack
= range_stack
;
8205 constructor_range_stack
= 0;
8208 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8209 (guaranteed to be 'volatile' or null) and ARGS (represented using
8210 an ASM_EXPR node). */
8212 build_asm_stmt (tree cv_qualifier
, tree args
)
8214 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
8215 ASM_VOLATILE_P (args
) = 1;
8216 return add_stmt (args
);
8219 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8220 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8221 SIMPLE indicates whether there was anything at all after the
8222 string in the asm expression -- asm("blah") and asm("blah" : )
8223 are subtly different. We use a ASM_EXPR node to represent this. */
8225 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
8226 tree clobbers
, tree labels
, bool simple
)
8231 const char *constraint
;
8232 const char **oconstraints
;
8233 bool allows_mem
, allows_reg
, is_inout
;
8234 int ninputs
, noutputs
;
8236 ninputs
= list_length (inputs
);
8237 noutputs
= list_length (outputs
);
8238 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
8240 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
8242 /* Remove output conversions that change the type but not the mode. */
8243 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
8245 tree output
= TREE_VALUE (tail
);
8247 /* ??? Really, this should not be here. Users should be using a
8248 proper lvalue, dammit. But there's a long history of using casts
8249 in the output operands. In cases like longlong.h, this becomes a
8250 primitive form of typechecking -- if the cast can be removed, then
8251 the output operand had a type of the proper width; otherwise we'll
8252 get an error. Gross, but ... */
8253 STRIP_NOPS (output
);
8255 if (!lvalue_or_else (output
, lv_asm
))
8256 output
= error_mark_node
;
8258 if (output
!= error_mark_node
8259 && (TREE_READONLY (output
)
8260 || TYPE_READONLY (TREE_TYPE (output
))
8261 || ((TREE_CODE (TREE_TYPE (output
)) == RECORD_TYPE
8262 || TREE_CODE (TREE_TYPE (output
)) == UNION_TYPE
)
8263 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
8264 readonly_error (output
, lv_asm
);
8266 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
8267 oconstraints
[i
] = constraint
;
8269 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
8270 &allows_mem
, &allows_reg
, &is_inout
))
8272 /* If the operand is going to end up in memory,
8273 mark it addressable. */
8274 if (!allows_reg
&& !c_mark_addressable (output
))
8275 output
= error_mark_node
;
8278 output
= error_mark_node
;
8280 TREE_VALUE (tail
) = output
;
8283 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
8287 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
8288 input
= TREE_VALUE (tail
);
8290 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
8291 oconstraints
, &allows_mem
, &allows_reg
))
8293 /* If the operand is going to end up in memory,
8294 mark it addressable. */
8295 if (!allows_reg
&& allows_mem
)
8297 /* Strip the nops as we allow this case. FIXME, this really
8298 should be rejected or made deprecated. */
8300 if (!c_mark_addressable (input
))
8301 input
= error_mark_node
;
8305 input
= error_mark_node
;
8307 TREE_VALUE (tail
) = input
;
8310 /* ASMs with labels cannot have outputs. This should have been
8311 enforced by the parser. */
8312 gcc_assert (outputs
== NULL
|| labels
== NULL
);
8314 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
8316 /* asm statements without outputs, including simple ones, are treated
8318 ASM_INPUT_P (args
) = simple
;
8319 ASM_VOLATILE_P (args
) = (noutputs
== 0);
8324 /* Generate a goto statement to LABEL. LOC is the location of the
8328 c_finish_goto_label (location_t loc
, tree label
)
8330 tree decl
= lookup_label_for_goto (loc
, label
);
8333 TREE_USED (decl
) = 1;
8335 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
8336 SET_EXPR_LOCATION (t
, loc
);
8337 return add_stmt (t
);
8341 /* Generate a computed goto statement to EXPR. LOC is the location of
8345 c_finish_goto_ptr (location_t loc
, tree expr
)
8348 pedwarn (loc
, OPT_pedantic
, "ISO C forbids %<goto *expr;%>");
8349 expr
= c_fully_fold (expr
, false, NULL
);
8350 expr
= convert (ptr_type_node
, expr
);
8351 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
8352 SET_EXPR_LOCATION (t
, loc
);
8353 return add_stmt (t
);
8356 /* Generate a C `return' statement. RETVAL is the expression for what
8357 to return, or a null pointer for `return;' with no value. LOC is
8358 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8359 is the original type of RETVAL. */
8362 c_finish_return (location_t loc
, tree retval
, tree origtype
)
8364 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
8365 bool no_warning
= false;
8368 if (TREE_THIS_VOLATILE (current_function_decl
))
8370 "function declared %<noreturn%> has a %<return%> statement");
8374 tree semantic_type
= NULL_TREE
;
8375 npc
= null_pointer_constant_p (retval
);
8376 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
8378 semantic_type
= TREE_TYPE (retval
);
8379 retval
= TREE_OPERAND (retval
, 0);
8381 retval
= c_fully_fold (retval
, false, NULL
);
8383 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
8388 current_function_returns_null
= 1;
8389 if ((warn_return_type
|| flag_isoc99
)
8390 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
8392 pedwarn_c99 (loc
, flag_isoc99
? 0 : OPT_Wreturn_type
,
8393 "%<return%> with no value, in "
8394 "function returning non-void");
8398 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
8400 current_function_returns_null
= 1;
8401 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
8403 "%<return%> with a value, in function returning void");
8405 pedwarn (loc
, OPT_pedantic
, "ISO C forbids "
8406 "%<return%> with expression, in function returning void");
8410 tree t
= convert_for_assignment (loc
, valtype
, retval
, origtype
,
8412 npc
, NULL_TREE
, NULL_TREE
, 0);
8413 tree res
= DECL_RESULT (current_function_decl
);
8416 current_function_returns_value
= 1;
8417 if (t
== error_mark_node
)
8420 inner
= t
= convert (TREE_TYPE (res
), t
);
8422 /* Strip any conversions, additions, and subtractions, and see if
8423 we are returning the address of a local variable. Warn if so. */
8426 switch (TREE_CODE (inner
))
8429 case NON_LVALUE_EXPR
:
8431 case POINTER_PLUS_EXPR
:
8432 inner
= TREE_OPERAND (inner
, 0);
8436 /* If the second operand of the MINUS_EXPR has a pointer
8437 type (or is converted from it), this may be valid, so
8438 don't give a warning. */
8440 tree op1
= TREE_OPERAND (inner
, 1);
8442 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
8443 && (CONVERT_EXPR_P (op1
)
8444 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
8445 op1
= TREE_OPERAND (op1
, 0);
8447 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
8450 inner
= TREE_OPERAND (inner
, 0);
8455 inner
= TREE_OPERAND (inner
, 0);
8457 while (REFERENCE_CLASS_P (inner
)
8458 && TREE_CODE (inner
) != INDIRECT_REF
)
8459 inner
= TREE_OPERAND (inner
, 0);
8462 && !DECL_EXTERNAL (inner
)
8463 && !TREE_STATIC (inner
)
8464 && DECL_CONTEXT (inner
) == current_function_decl
)
8466 0, "function returns address of local variable");
8476 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
8477 SET_EXPR_LOCATION (retval
, loc
);
8479 if (warn_sequence_point
)
8480 verify_sequence_points (retval
);
8483 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
8484 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
8485 return add_stmt (ret_stmt
);
8489 /* The SWITCH_EXPR being built. */
8492 /* The original type of the testing expression, i.e. before the
8493 default conversion is applied. */
8496 /* A splay-tree mapping the low element of a case range to the high
8497 element, or NULL_TREE if there is no high element. Used to
8498 determine whether or not a new case label duplicates an old case
8499 label. We need a tree, rather than simply a hash table, because
8500 of the GNU case range extension. */
8503 /* The bindings at the point of the switch. This is used for
8504 warnings crossing decls when branching to a case label. */
8505 struct c_spot_bindings
*bindings
;
8507 /* The next node on the stack. */
8508 struct c_switch
*next
;
8511 /* A stack of the currently active switch statements. The innermost
8512 switch statement is on the top of the stack. There is no need to
8513 mark the stack for garbage collection because it is only active
8514 during the processing of the body of a function, and we never
8515 collect at that point. */
8517 struct c_switch
*c_switch_stack
;
8519 /* Start a C switch statement, testing expression EXP. Return the new
8520 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8521 SWITCH_COND_LOC is the location of the switch's condition. */
8524 c_start_case (location_t switch_loc
,
8525 location_t switch_cond_loc
,
8528 tree orig_type
= error_mark_node
;
8529 struct c_switch
*cs
;
8531 if (exp
!= error_mark_node
)
8533 orig_type
= TREE_TYPE (exp
);
8535 if (!INTEGRAL_TYPE_P (orig_type
))
8537 if (orig_type
!= error_mark_node
)
8539 error_at (switch_cond_loc
, "switch quantity not an integer");
8540 orig_type
= error_mark_node
;
8542 exp
= integer_zero_node
;
8546 tree type
= TYPE_MAIN_VARIANT (orig_type
);
8548 if (!in_system_header
8549 && (type
== long_integer_type_node
8550 || type
== long_unsigned_type_node
))
8551 warning_at (switch_cond_loc
,
8552 OPT_Wtraditional
, "%<long%> switch expression not "
8553 "converted to %<int%> in ISO C");
8555 exp
= c_fully_fold (exp
, false, NULL
);
8556 exp
= default_conversion (exp
);
8558 if (warn_sequence_point
)
8559 verify_sequence_points (exp
);
8563 /* Add this new SWITCH_EXPR to the stack. */
8564 cs
= XNEW (struct c_switch
);
8565 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
8566 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
8567 cs
->orig_type
= orig_type
;
8568 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
8569 cs
->bindings
= c_get_switch_bindings ();
8570 cs
->next
= c_switch_stack
;
8571 c_switch_stack
= cs
;
8573 return add_stmt (cs
->switch_expr
);
8576 /* Process a case label at location LOC. */
8579 do_case (location_t loc
, tree low_value
, tree high_value
)
8581 tree label
= NULL_TREE
;
8583 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
8585 low_value
= c_fully_fold (low_value
, false, NULL
);
8586 if (TREE_CODE (low_value
) == INTEGER_CST
)
8587 pedwarn (input_location
, OPT_pedantic
,
8588 "case label is not an integer constant expression");
8591 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
8593 high_value
= c_fully_fold (high_value
, false, NULL
);
8594 if (TREE_CODE (high_value
) == INTEGER_CST
)
8595 pedwarn (input_location
, OPT_pedantic
,
8596 "case label is not an integer constant expression");
8599 if (c_switch_stack
== NULL
)
8602 error_at (loc
, "case label not within a switch statement");
8604 error_at (loc
, "%<default%> label not within a switch statement");
8608 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
8609 EXPR_LOCATION (c_switch_stack
->switch_expr
),
8613 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
8614 SWITCH_COND (c_switch_stack
->switch_expr
),
8615 c_switch_stack
->orig_type
,
8616 low_value
, high_value
);
8617 if (label
== error_mark_node
)
8622 /* Finish the switch statement. */
8625 c_finish_case (tree body
)
8627 struct c_switch
*cs
= c_switch_stack
;
8628 location_t switch_location
;
8630 SWITCH_BODY (cs
->switch_expr
) = body
;
8632 /* Emit warnings as needed. */
8633 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
8634 c_do_switch_warnings (cs
->cases
, switch_location
,
8635 TREE_TYPE (cs
->switch_expr
),
8636 SWITCH_COND (cs
->switch_expr
));
8638 /* Pop the stack. */
8639 c_switch_stack
= cs
->next
;
8640 splay_tree_delete (cs
->cases
);
8641 c_release_switch_bindings (cs
->bindings
);
8645 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8646 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8647 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8648 statement, and was not surrounded with parenthesis. */
8651 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
8652 tree else_block
, bool nested_if
)
8656 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8657 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
8659 tree inner_if
= then_block
;
8661 /* We know from the grammar productions that there is an IF nested
8662 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8663 it might not be exactly THEN_BLOCK, but should be the last
8664 non-container statement within. */
8666 switch (TREE_CODE (inner_if
))
8671 inner_if
= BIND_EXPR_BODY (inner_if
);
8673 case STATEMENT_LIST
:
8674 inner_if
= expr_last (then_block
);
8676 case TRY_FINALLY_EXPR
:
8677 case TRY_CATCH_EXPR
:
8678 inner_if
= TREE_OPERAND (inner_if
, 0);
8685 if (COND_EXPR_ELSE (inner_if
))
8686 warning_at (if_locus
, OPT_Wparentheses
,
8687 "suggest explicit braces to avoid ambiguous %<else%>");
8690 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
8691 SET_EXPR_LOCATION (stmt
, if_locus
);
8695 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8696 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8697 is false for DO loops. INCR is the FOR increment expression. BODY is
8698 the statement controlled by the loop. BLAB is the break label. CLAB is
8699 the continue label. Everything is allowed to be NULL. */
8702 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
8703 tree blab
, tree clab
, bool cond_is_first
)
8705 tree entry
= NULL
, exit
= NULL
, t
;
8707 /* If the condition is zero don't generate a loop construct. */
8708 if (cond
&& integer_zerop (cond
))
8712 t
= build_and_jump (&blab
);
8713 SET_EXPR_LOCATION (t
, start_locus
);
8719 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
8721 /* If we have an exit condition, then we build an IF with gotos either
8722 out of the loop, or to the top of it. If there's no exit condition,
8723 then we just build a jump back to the top. */
8724 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
8726 if (cond
&& !integer_nonzerop (cond
))
8728 /* Canonicalize the loop condition to the end. This means
8729 generating a branch to the loop condition. Reuse the
8730 continue label, if possible. */
8735 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
8736 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
8739 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
8740 SET_EXPR_LOCATION (t
, start_locus
);
8744 t
= build_and_jump (&blab
);
8746 exit
= fold_build3_loc (start_locus
,
8747 COND_EXPR
, void_type_node
, cond
, exit
, t
);
8749 exit
= fold_build3_loc (input_location
,
8750 COND_EXPR
, void_type_node
, cond
, exit
, t
);
8759 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
8767 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
8771 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
8774 tree label
= *label_p
;
8776 /* In switch statements break is sometimes stylistically used after
8777 a return statement. This can lead to spurious warnings about
8778 control reaching the end of a non-void function when it is
8779 inlined. Note that we are calling block_may_fallthru with
8780 language specific tree nodes; this works because
8781 block_may_fallthru returns true when given something it does not
8783 skip
= !block_may_fallthru (cur_stmt_list
);
8788 *label_p
= label
= create_artificial_label (loc
);
8790 else if (TREE_CODE (label
) == LABEL_DECL
)
8792 else switch (TREE_INT_CST_LOW (label
))
8796 error_at (loc
, "break statement not within loop or switch");
8798 error_at (loc
, "continue statement not within a loop");
8802 gcc_assert (is_break
);
8803 error_at (loc
, "break statement used with OpenMP for loop");
8814 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
8816 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
8819 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8822 emit_side_effect_warnings (location_t loc
, tree expr
)
8824 if (expr
== error_mark_node
)
8826 else if (!TREE_SIDE_EFFECTS (expr
))
8828 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
8829 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
8832 warn_if_unused_value (expr
, loc
);
8835 /* Process an expression as if it were a complete statement. Emit
8836 diagnostics, but do not call ADD_STMT. LOC is the location of the
8840 c_process_expr_stmt (location_t loc
, tree expr
)
8845 expr
= c_fully_fold (expr
, false, NULL
);
8847 if (warn_sequence_point
)
8848 verify_sequence_points (expr
);
8850 if (TREE_TYPE (expr
) != error_mark_node
8851 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
8852 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
8853 error_at (loc
, "expression statement has incomplete type");
8855 /* If we're not processing a statement expression, warn about unused values.
8856 Warnings for statement expressions will be emitted later, once we figure
8857 out which is the result. */
8858 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
8859 && warn_unused_value
)
8860 emit_side_effect_warnings (loc
, expr
);
8862 if (DECL_P (expr
) || handled_component_p (expr
))
8863 mark_exp_read (expr
);
8865 /* If the expression is not of a type to which we cannot assign a line
8866 number, wrap the thing in a no-op NOP_EXPR. */
8867 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
8869 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
8870 SET_EXPR_LOCATION (expr
, loc
);
8876 /* Emit an expression as a statement. LOC is the location of the
8880 c_finish_expr_stmt (location_t loc
, tree expr
)
8883 return add_stmt (c_process_expr_stmt (loc
, expr
));
8888 /* Do the opposite and emit a statement as an expression. To begin,
8889 create a new binding level and return it. */
8892 c_begin_stmt_expr (void)
8896 /* We must force a BLOCK for this level so that, if it is not expanded
8897 later, there is a way to turn off the entire subtree of blocks that
8898 are contained in it. */
8900 ret
= c_begin_compound_stmt (true);
8902 c_bindings_start_stmt_expr (c_switch_stack
== NULL
8904 : c_switch_stack
->bindings
);
8906 /* Mark the current statement list as belonging to a statement list. */
8907 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
8912 /* LOC is the location of the compound statement to which this body
8916 c_finish_stmt_expr (location_t loc
, tree body
)
8918 tree last
, type
, tmp
, val
;
8921 body
= c_end_compound_stmt (loc
, body
, true);
8923 c_bindings_end_stmt_expr (c_switch_stack
== NULL
8925 : c_switch_stack
->bindings
);
8927 /* Locate the last statement in BODY. See c_end_compound_stmt
8928 about always returning a BIND_EXPR. */
8929 last_p
= &BIND_EXPR_BODY (body
);
8930 last
= BIND_EXPR_BODY (body
);
8933 if (TREE_CODE (last
) == STATEMENT_LIST
)
8935 tree_stmt_iterator i
;
8937 /* This can happen with degenerate cases like ({ }). No value. */
8938 if (!TREE_SIDE_EFFECTS (last
))
8941 /* If we're supposed to generate side effects warnings, process
8942 all of the statements except the last. */
8943 if (warn_unused_value
)
8945 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
8948 tree t
= tsi_stmt (i
);
8950 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
8951 emit_side_effect_warnings (tloc
, t
);
8955 i
= tsi_last (last
);
8956 last_p
= tsi_stmt_ptr (i
);
8960 /* If the end of the list is exception related, then the list was split
8961 by a call to push_cleanup. Continue searching. */
8962 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
8963 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
8965 last_p
= &TREE_OPERAND (last
, 0);
8967 goto continue_searching
;
8970 if (last
== error_mark_node
)
8973 /* In the case that the BIND_EXPR is not necessary, return the
8974 expression out from inside it. */
8975 if (last
== BIND_EXPR_BODY (body
)
8976 && BIND_EXPR_VARS (body
) == NULL
)
8978 /* Even if this looks constant, do not allow it in a constant
8980 last
= c_wrap_maybe_const (last
, true);
8981 /* Do not warn if the return value of a statement expression is
8983 TREE_NO_WARNING (last
) = 1;
8987 /* Extract the type of said expression. */
8988 type
= TREE_TYPE (last
);
8990 /* If we're not returning a value at all, then the BIND_EXPR that
8991 we already have is a fine expression to return. */
8992 if (!type
|| VOID_TYPE_P (type
))
8995 /* Now that we've located the expression containing the value, it seems
8996 silly to make voidify_wrapper_expr repeat the process. Create a
8997 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8998 tmp
= create_tmp_var_raw (type
, NULL
);
9000 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9001 tree_expr_nonnegative_p giving up immediately. */
9003 if (TREE_CODE (val
) == NOP_EXPR
9004 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
9005 val
= TREE_OPERAND (val
, 0);
9007 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
9008 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
9011 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
9012 SET_EXPR_LOCATION (t
, loc
);
9017 /* Begin and end compound statements. This is as simple as pushing
9018 and popping new statement lists from the tree. */
9021 c_begin_compound_stmt (bool do_scope
)
9023 tree stmt
= push_stmt_list ();
9029 /* End a compound statement. STMT is the statement. LOC is the
9030 location of the compound statement-- this is usually the location
9031 of the opening brace. */
9034 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
9040 if (c_dialect_objc ())
9041 objc_clear_super_receiver ();
9042 block
= pop_scope ();
9045 stmt
= pop_stmt_list (stmt
);
9046 stmt
= c_build_bind_expr (loc
, block
, stmt
);
9048 /* If this compound statement is nested immediately inside a statement
9049 expression, then force a BIND_EXPR to be created. Otherwise we'll
9050 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9051 STATEMENT_LISTs merge, and thus we can lose track of what statement
9054 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9055 && TREE_CODE (stmt
) != BIND_EXPR
)
9057 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
9058 TREE_SIDE_EFFECTS (stmt
) = 1;
9059 SET_EXPR_LOCATION (stmt
, loc
);
9065 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9066 when the current scope is exited. EH_ONLY is true when this is not
9067 meant to apply to normal control flow transfer. */
9070 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
9072 enum tree_code code
;
9076 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
9077 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
9079 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
9080 list
= push_stmt_list ();
9081 TREE_OPERAND (stmt
, 0) = list
;
9082 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
9085 /* Build a binary-operation expression without default conversions.
9086 CODE is the kind of expression to build.
9087 LOCATION is the operator's location.
9088 This function differs from `build' in several ways:
9089 the data type of the result is computed and recorded in it,
9090 warnings are generated if arg data types are invalid,
9091 special handling for addition and subtraction of pointers is known,
9092 and some optimization is done (operations on narrow ints
9093 are done in the narrower type when that gives the same result).
9094 Constant folding is also done before the result is returned.
9096 Note that the operands will never have enumeral types, or function
9097 or array types, because either they will have the default conversions
9098 performed or they have both just been converted to some other type in which
9099 the arithmetic is to be done. */
9102 build_binary_op (location_t location
, enum tree_code code
,
9103 tree orig_op0
, tree orig_op1
, int convert_p
)
9105 tree type0
, type1
, orig_type0
, orig_type1
;
9107 enum tree_code code0
, code1
;
9109 tree ret
= error_mark_node
;
9110 const char *invalid_op_diag
;
9111 bool op0_int_operands
, op1_int_operands
;
9112 bool int_const
, int_const_or_overflow
, int_operands
;
9114 /* Expression code to give to the expression when it is built.
9115 Normally this is CODE, which is what the caller asked for,
9116 but in some special cases we change it. */
9117 enum tree_code resultcode
= code
;
9119 /* Data type in which the computation is to be performed.
9120 In the simplest cases this is the common type of the arguments. */
9121 tree result_type
= NULL
;
9123 /* When the computation is in excess precision, the type of the
9124 final EXCESS_PRECISION_EXPR. */
9125 tree semantic_result_type
= NULL
;
9127 /* Nonzero means operands have already been type-converted
9128 in whatever way is necessary.
9129 Zero means they need to be converted to RESULT_TYPE. */
9132 /* Nonzero means create the expression with this type, rather than
9134 tree build_type
= 0;
9136 /* Nonzero means after finally constructing the expression
9137 convert it to this type. */
9138 tree final_type
= 0;
9140 /* Nonzero if this is an operation like MIN or MAX which can
9141 safely be computed in short if both args are promoted shorts.
9142 Also implies COMMON.
9143 -1 indicates a bitwise operation; this makes a difference
9144 in the exact conditions for when it is safe to do the operation
9145 in a narrower mode. */
9148 /* Nonzero if this is a comparison operation;
9149 if both args are promoted shorts, compare the original shorts.
9150 Also implies COMMON. */
9151 int short_compare
= 0;
9153 /* Nonzero if this is a right-shift operation, which can be computed on the
9154 original short and then promoted if the operand is a promoted short. */
9155 int short_shift
= 0;
9157 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9160 /* True means types are compatible as far as ObjC is concerned. */
9163 /* True means this is an arithmetic operation that may need excess
9165 bool may_need_excess_precision
;
9167 if (location
== UNKNOWN_LOCATION
)
9168 location
= input_location
;
9173 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
9174 if (op0_int_operands
)
9175 op0
= remove_c_maybe_const_expr (op0
);
9176 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
9177 if (op1_int_operands
)
9178 op1
= remove_c_maybe_const_expr (op1
);
9179 int_operands
= (op0_int_operands
&& op1_int_operands
);
9182 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
9183 && TREE_CODE (orig_op1
) == INTEGER_CST
);
9184 int_const
= (int_const_or_overflow
9185 && !TREE_OVERFLOW (orig_op0
)
9186 && !TREE_OVERFLOW (orig_op1
));
9189 int_const
= int_const_or_overflow
= false;
9193 op0
= default_conversion (op0
);
9194 op1
= default_conversion (op1
);
9197 orig_type0
= type0
= TREE_TYPE (op0
);
9198 orig_type1
= type1
= TREE_TYPE (op1
);
9200 /* The expression codes of the data types of the arguments tell us
9201 whether the arguments are integers, floating, pointers, etc. */
9202 code0
= TREE_CODE (type0
);
9203 code1
= TREE_CODE (type1
);
9205 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9206 STRIP_TYPE_NOPS (op0
);
9207 STRIP_TYPE_NOPS (op1
);
9209 /* If an error was already reported for one of the arguments,
9210 avoid reporting another error. */
9212 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
9213 return error_mark_node
;
9215 if ((invalid_op_diag
9216 = targetm
.invalid_binary_op (code
, type0
, type1
)))
9218 error_at (location
, invalid_op_diag
);
9219 return error_mark_node
;
9227 case TRUNC_DIV_EXPR
:
9229 case FLOOR_DIV_EXPR
:
9230 case ROUND_DIV_EXPR
:
9231 case EXACT_DIV_EXPR
:
9232 may_need_excess_precision
= true;
9235 may_need_excess_precision
= false;
9238 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
9240 op0
= TREE_OPERAND (op0
, 0);
9241 type0
= TREE_TYPE (op0
);
9243 else if (may_need_excess_precision
9244 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
9247 op0
= convert (eptype
, op0
);
9249 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
9251 op1
= TREE_OPERAND (op1
, 0);
9252 type1
= TREE_TYPE (op1
);
9254 else if (may_need_excess_precision
9255 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
9258 op1
= convert (eptype
, op1
);
9261 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
9266 /* Handle the pointer + int case. */
9267 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
9269 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
9270 goto return_build_binary_op
;
9272 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
9274 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
9275 goto return_build_binary_op
;
9282 /* Subtraction of two similar pointers.
9283 We must subtract them as integers, then divide by object size. */
9284 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
9285 && comp_target_types (location
, type0
, type1
))
9287 ret
= pointer_diff (location
, op0
, op1
);
9288 goto return_build_binary_op
;
9290 /* Handle pointer minus int. Just like pointer plus int. */
9291 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
9293 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
9294 goto return_build_binary_op
;
9304 case TRUNC_DIV_EXPR
:
9306 case FLOOR_DIV_EXPR
:
9307 case ROUND_DIV_EXPR
:
9308 case EXACT_DIV_EXPR
:
9309 warn_for_div_by_zero (location
, op1
);
9311 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
9312 || code0
== FIXED_POINT_TYPE
9313 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
9314 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
9315 || code1
== FIXED_POINT_TYPE
9316 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
9318 enum tree_code tcode0
= code0
, tcode1
= code1
;
9320 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
9321 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
9322 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
9323 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
9325 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
9326 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
9327 resultcode
= RDIV_EXPR
;
9329 /* Although it would be tempting to shorten always here, that
9330 loses on some targets, since the modulo instruction is
9331 undefined if the quotient can't be represented in the
9332 computation mode. We shorten only if unsigned or if
9333 dividing by something we know != -1. */
9334 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
9335 || (TREE_CODE (op1
) == INTEGER_CST
9336 && !integer_all_onesp (op1
)));
9344 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
9346 /* Allow vector types which are not floating point types. */
9347 else if (code0
== VECTOR_TYPE
9348 && code1
== VECTOR_TYPE
9349 && !VECTOR_FLOAT_TYPE_P (type0
)
9350 && !VECTOR_FLOAT_TYPE_P (type1
))
9354 case TRUNC_MOD_EXPR
:
9355 case FLOOR_MOD_EXPR
:
9356 warn_for_div_by_zero (location
, op1
);
9358 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
9359 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
9360 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
9362 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
9364 /* Although it would be tempting to shorten always here, that loses
9365 on some targets, since the modulo instruction is undefined if the
9366 quotient can't be represented in the computation mode. We shorten
9367 only if unsigned or if dividing by something we know != -1. */
9368 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
9369 || (TREE_CODE (op1
) == INTEGER_CST
9370 && !integer_all_onesp (op1
)));
9375 case TRUTH_ANDIF_EXPR
:
9376 case TRUTH_ORIF_EXPR
:
9377 case TRUTH_AND_EXPR
:
9379 case TRUTH_XOR_EXPR
:
9380 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
9381 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
9382 || code0
== FIXED_POINT_TYPE
)
9383 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
9384 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
9385 || code1
== FIXED_POINT_TYPE
))
9387 /* Result of these operations is always an int,
9388 but that does not mean the operands should be
9389 converted to ints! */
9390 result_type
= integer_type_node
;
9391 op0
= c_common_truthvalue_conversion (location
, op0
);
9392 op1
= c_common_truthvalue_conversion (location
, op1
);
9395 if (code
== TRUTH_ANDIF_EXPR
)
9397 int_const_or_overflow
= (int_operands
9398 && TREE_CODE (orig_op0
) == INTEGER_CST
9399 && (op0
== truthvalue_false_node
9400 || TREE_CODE (orig_op1
) == INTEGER_CST
));
9401 int_const
= (int_const_or_overflow
9402 && !TREE_OVERFLOW (orig_op0
)
9403 && (op0
== truthvalue_false_node
9404 || !TREE_OVERFLOW (orig_op1
)));
9406 else if (code
== TRUTH_ORIF_EXPR
)
9408 int_const_or_overflow
= (int_operands
9409 && TREE_CODE (orig_op0
) == INTEGER_CST
9410 && (op0
== truthvalue_true_node
9411 || TREE_CODE (orig_op1
) == INTEGER_CST
));
9412 int_const
= (int_const_or_overflow
9413 && !TREE_OVERFLOW (orig_op0
)
9414 && (op0
== truthvalue_true_node
9415 || !TREE_OVERFLOW (orig_op1
)));
9419 /* Shift operations: result has same type as first operand;
9420 always convert second operand to int.
9421 Also set SHORT_SHIFT if shifting rightward. */
9424 if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
9425 && code1
== INTEGER_TYPE
)
9427 if (TREE_CODE (op1
) == INTEGER_CST
)
9429 if (tree_int_cst_sgn (op1
) < 0)
9432 if (c_inhibit_evaluation_warnings
== 0)
9433 warning (0, "right shift count is negative");
9437 if (!integer_zerop (op1
))
9440 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
9443 if (c_inhibit_evaluation_warnings
== 0)
9444 warning (0, "right shift count >= width of type");
9449 /* Use the type of the value to be shifted. */
9450 result_type
= type0
;
9451 /* Convert the shift-count to an integer, regardless of size
9452 of value being shifted. */
9453 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
9454 op1
= convert (integer_type_node
, op1
);
9455 /* Avoid converting op1 to result_type later. */
9461 if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
9462 && code1
== INTEGER_TYPE
)
9464 if (TREE_CODE (op1
) == INTEGER_CST
)
9466 if (tree_int_cst_sgn (op1
) < 0)
9469 if (c_inhibit_evaluation_warnings
== 0)
9470 warning (0, "left shift count is negative");
9473 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
9476 if (c_inhibit_evaluation_warnings
== 0)
9477 warning (0, "left shift count >= width of type");
9481 /* Use the type of the value to be shifted. */
9482 result_type
= type0
;
9483 /* Convert the shift-count to an integer, regardless of size
9484 of value being shifted. */
9485 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
9486 op1
= convert (integer_type_node
, op1
);
9487 /* Avoid converting op1 to result_type later. */
9494 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
9495 warning_at (location
,
9497 "comparing floating point with == or != is unsafe");
9498 /* Result of comparison is always int,
9499 but don't convert the args to int! */
9500 build_type
= integer_type_node
;
9501 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
9502 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
9503 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
9504 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
9506 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
9508 if (TREE_CODE (op0
) == ADDR_EXPR
9509 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
9511 if (code
== EQ_EXPR
)
9512 warning_at (location
,
9514 "the comparison will always evaluate as %<false%> "
9515 "for the address of %qD will never be NULL",
9516 TREE_OPERAND (op0
, 0));
9518 warning_at (location
,
9520 "the comparison will always evaluate as %<true%> "
9521 "for the address of %qD will never be NULL",
9522 TREE_OPERAND (op0
, 0));
9524 result_type
= type0
;
9526 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
9528 if (TREE_CODE (op1
) == ADDR_EXPR
9529 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
9531 if (code
== EQ_EXPR
)
9532 warning_at (location
,
9534 "the comparison will always evaluate as %<false%> "
9535 "for the address of %qD will never be NULL",
9536 TREE_OPERAND (op1
, 0));
9538 warning_at (location
,
9540 "the comparison will always evaluate as %<true%> "
9541 "for the address of %qD will never be NULL",
9542 TREE_OPERAND (op1
, 0));
9544 result_type
= type1
;
9546 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
9548 tree tt0
= TREE_TYPE (type0
);
9549 tree tt1
= TREE_TYPE (type1
);
9550 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
9551 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
9552 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
9554 /* Anything compares with void *. void * compares with anything.
9555 Otherwise, the targets must be compatible
9556 and both must be object or both incomplete. */
9557 if (comp_target_types (location
, type0
, type1
))
9558 result_type
= common_pointer_type (type0
, type1
);
9559 else if (!addr_space_superset (as0
, as1
, &as_common
))
9561 error_at (location
, "comparison of pointers to "
9562 "disjoint address spaces");
9563 return error_mark_node
;
9565 else if (VOID_TYPE_P (tt0
))
9567 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
9568 pedwarn (location
, OPT_pedantic
, "ISO C forbids "
9569 "comparison of %<void *%> with function pointer");
9571 else if (VOID_TYPE_P (tt1
))
9573 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
9574 pedwarn (location
, OPT_pedantic
, "ISO C forbids "
9575 "comparison of %<void *%> with function pointer");
9578 /* Avoid warning about the volatile ObjC EH puts on decls. */
9580 pedwarn (location
, 0,
9581 "comparison of distinct pointer types lacks a cast");
9583 if (result_type
== NULL_TREE
)
9585 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
9586 result_type
= build_pointer_type
9587 (build_qualified_type (void_type_node
, qual
));
9590 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
9592 result_type
= type0
;
9593 pedwarn (location
, 0, "comparison between pointer and integer");
9595 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
9597 result_type
= type1
;
9598 pedwarn (location
, 0, "comparison between pointer and integer");
9606 build_type
= integer_type_node
;
9607 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
9608 || code0
== FIXED_POINT_TYPE
)
9609 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
9610 || code1
== FIXED_POINT_TYPE
))
9612 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
9614 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
9615 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
9616 addr_space_t as_common
;
9618 if (comp_target_types (location
, type0
, type1
))
9620 result_type
= common_pointer_type (type0
, type1
);
9621 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
9622 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
9623 pedwarn (location
, 0,
9624 "comparison of complete and incomplete pointers");
9625 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
9626 pedwarn (location
, OPT_pedantic
, "ISO C forbids "
9627 "ordered comparisons of pointers to functions");
9629 else if (!addr_space_superset (as0
, as1
, &as_common
))
9631 error_at (location
, "comparison of pointers to "
9632 "disjoint address spaces");
9633 return error_mark_node
;
9637 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
9638 result_type
= build_pointer_type
9639 (build_qualified_type (void_type_node
, qual
));
9640 pedwarn (location
, 0,
9641 "comparison of distinct pointer types lacks a cast");
9644 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
9646 result_type
= type0
;
9648 pedwarn (location
, OPT_pedantic
,
9649 "ordered comparison of pointer with integer zero");
9650 else if (extra_warnings
)
9651 warning_at (location
, OPT_Wextra
,
9652 "ordered comparison of pointer with integer zero");
9654 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
9656 result_type
= type1
;
9657 pedwarn (location
, OPT_pedantic
,
9658 "ordered comparison of pointer with integer zero");
9660 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
9662 result_type
= type0
;
9663 pedwarn (location
, 0, "comparison between pointer and integer");
9665 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
9667 result_type
= type1
;
9668 pedwarn (location
, 0, "comparison between pointer and integer");
9676 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
9677 return error_mark_node
;
9679 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
9680 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
9681 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0
),
9682 TREE_TYPE (type1
))))
9684 binary_op_error (location
, code
, type0
, type1
);
9685 return error_mark_node
;
9688 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
9689 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
9691 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
9692 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
9694 bool first_complex
= (code0
== COMPLEX_TYPE
);
9695 bool second_complex
= (code1
== COMPLEX_TYPE
);
9696 int none_complex
= (!first_complex
&& !second_complex
);
9698 if (shorten
|| common
|| short_compare
)
9700 result_type
= c_common_type (type0
, type1
);
9701 if (result_type
== error_mark_node
)
9702 return error_mark_node
;
9705 if (first_complex
!= second_complex
9706 && (code
== PLUS_EXPR
9707 || code
== MINUS_EXPR
9708 || code
== MULT_EXPR
9709 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
9710 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
9711 && flag_signed_zeros
)
9713 /* An operation on mixed real/complex operands must be
9714 handled specially, but the language-independent code can
9715 more easily optimize the plain complex arithmetic if
9716 -fno-signed-zeros. */
9717 tree real_type
= TREE_TYPE (result_type
);
9719 if (type0
!= orig_type0
|| type1
!= orig_type1
)
9721 gcc_assert (may_need_excess_precision
&& common
);
9722 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
9726 if (TREE_TYPE (op0
) != result_type
)
9727 op0
= convert_and_check (result_type
, op0
);
9728 if (TREE_TYPE (op1
) != real_type
)
9729 op1
= convert_and_check (real_type
, op1
);
9733 if (TREE_TYPE (op0
) != real_type
)
9734 op0
= convert_and_check (real_type
, op0
);
9735 if (TREE_TYPE (op1
) != result_type
)
9736 op1
= convert_and_check (result_type
, op1
);
9738 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
9739 return error_mark_node
;
9742 op0
= c_save_expr (op0
);
9743 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
9745 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
9750 case TRUNC_DIV_EXPR
:
9751 imag
= build2 (resultcode
, real_type
, imag
, op1
);
9755 real
= build2 (resultcode
, real_type
, real
, op1
);
9763 op1
= c_save_expr (op1
);
9764 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
9766 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
9771 imag
= build2 (resultcode
, real_type
, op0
, imag
);
9774 real
= build2 (resultcode
, real_type
, op0
, real
);
9777 real
= build2 (resultcode
, real_type
, op0
, real
);
9778 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
9784 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
9785 goto return_build_binary_op
;
9788 /* For certain operations (which identify themselves by shorten != 0)
9789 if both args were extended from the same smaller type,
9790 do the arithmetic in that type and then extend.
9792 shorten !=0 and !=1 indicates a bitwise operation.
9793 For them, this optimization is safe only if
9794 both args are zero-extended or both are sign-extended.
9795 Otherwise, we might change the result.
9796 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9797 but calculated in (unsigned short) it would be (unsigned short)-1. */
9799 if (shorten
&& none_complex
)
9801 final_type
= result_type
;
9802 result_type
= shorten_binary_op (result_type
, op0
, op1
,
9806 /* Shifts can be shortened if shifting right. */
9811 tree arg0
= get_narrower (op0
, &unsigned_arg
);
9813 final_type
= result_type
;
9815 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
9816 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
9818 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
9819 && tree_int_cst_sgn (op1
) > 0
9820 /* We can shorten only if the shift count is less than the
9821 number of bits in the smaller type size. */
9822 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
9823 /* We cannot drop an unsigned shift after sign-extension. */
9824 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
9826 /* Do an unsigned shift if the operand was zero-extended. */
9828 = c_common_signed_or_unsigned_type (unsigned_arg
,
9830 /* Convert value-to-be-shifted to that type. */
9831 if (TREE_TYPE (op0
) != result_type
)
9832 op0
= convert (result_type
, op0
);
9837 /* Comparison operations are shortened too but differently.
9838 They identify themselves by setting short_compare = 1. */
9842 /* Don't write &op0, etc., because that would prevent op0
9843 from being kept in a register.
9844 Instead, make copies of the our local variables and
9845 pass the copies by reference, then copy them back afterward. */
9846 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
9847 enum tree_code xresultcode
= resultcode
;
9849 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
9854 goto return_build_binary_op
;
9857 op0
= xop0
, op1
= xop1
;
9859 resultcode
= xresultcode
;
9861 if (c_inhibit_evaluation_warnings
== 0)
9863 bool op0_maybe_const
= true;
9864 bool op1_maybe_const
= true;
9865 tree orig_op0_folded
, orig_op1_folded
;
9867 if (in_late_binary_op
)
9869 orig_op0_folded
= orig_op0
;
9870 orig_op1_folded
= orig_op1
;
9874 /* Fold for the sake of possible warnings, as in
9875 build_conditional_expr. This requires the
9876 "original" values to be folded, not just op0 and
9878 c_inhibit_evaluation_warnings
++;
9879 op0
= c_fully_fold (op0
, require_constant_value
,
9881 op1
= c_fully_fold (op1
, require_constant_value
,
9883 c_inhibit_evaluation_warnings
--;
9884 orig_op0_folded
= c_fully_fold (orig_op0
,
9885 require_constant_value
,
9887 orig_op1_folded
= c_fully_fold (orig_op1
,
9888 require_constant_value
,
9892 if (warn_sign_compare
)
9893 warn_for_sign_compare (location
, orig_op0_folded
,
9894 orig_op1_folded
, op0
, op1
,
9895 result_type
, resultcode
);
9896 if (!in_late_binary_op
)
9898 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
9899 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
9900 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
9901 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
9907 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9908 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9909 Then the expression will be built.
9910 It will be given type FINAL_TYPE if that is nonzero;
9911 otherwise, it will be given type RESULT_TYPE. */
9915 binary_op_error (location
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
9916 return error_mark_node
;
9919 if (build_type
== NULL_TREE
)
9921 build_type
= result_type
;
9922 if (type0
!= orig_type0
|| type1
!= orig_type1
)
9924 gcc_assert (may_need_excess_precision
&& common
);
9925 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
9931 op0
= ep_convert_and_check (result_type
, op0
, semantic_result_type
);
9932 op1
= ep_convert_and_check (result_type
, op1
, semantic_result_type
);
9934 /* This can happen if one operand has a vector type, and the other
9935 has a different type. */
9936 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
9937 return error_mark_node
;
9940 /* Treat expressions in initializers specially as they can't trap. */
9941 if (int_const_or_overflow
)
9942 ret
= (require_constant_value
9943 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
9945 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
9947 ret
= build2 (resultcode
, build_type
, op0
, op1
);
9948 if (final_type
!= 0)
9949 ret
= convert (final_type
, ret
);
9951 return_build_binary_op
:
9952 gcc_assert (ret
!= error_mark_node
);
9953 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
9955 ? note_integer_operands (ret
)
9956 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
9957 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
9958 && !in_late_binary_op
)
9959 ret
= note_integer_operands (ret
);
9960 if (semantic_result_type
)
9961 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
9962 protected_set_expr_location (ret
, location
);
9967 /* Convert EXPR to be a truth-value, validating its type for this
9968 purpose. LOCATION is the source location for the expression. */
9971 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
9973 bool int_const
, int_operands
;
9975 switch (TREE_CODE (TREE_TYPE (expr
)))
9978 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
9979 return error_mark_node
;
9982 error_at (location
, "used struct type value where scalar is required");
9983 return error_mark_node
;
9986 error_at (location
, "used union type value where scalar is required");
9987 return error_mark_node
;
9996 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
9997 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
9999 expr
= remove_c_maybe_const_expr (expr
);
10001 /* ??? Should we also give an error for void and vectors rather than
10002 leaving those to give errors later? */
10003 expr
= c_common_truthvalue_conversion (location
, expr
);
10005 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
10007 if (TREE_OVERFLOW (expr
))
10010 return note_integer_operands (expr
);
10012 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
10013 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
10018 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10022 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
10024 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
10026 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
10027 /* Executing a compound literal inside a function reinitializes
10029 if (!TREE_STATIC (decl
))
10037 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10040 c_begin_omp_parallel (void)
10044 keep_next_level ();
10045 block
= c_begin_compound_stmt (true);
10050 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10051 statement. LOC is the location of the OMP_PARALLEL. */
10054 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
10058 block
= c_end_compound_stmt (loc
, block
, true);
10060 stmt
= make_node (OMP_PARALLEL
);
10061 TREE_TYPE (stmt
) = void_type_node
;
10062 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
10063 OMP_PARALLEL_BODY (stmt
) = block
;
10064 SET_EXPR_LOCATION (stmt
, loc
);
10066 return add_stmt (stmt
);
10069 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10072 c_begin_omp_task (void)
10076 keep_next_level ();
10077 block
= c_begin_compound_stmt (true);
10082 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10083 statement. LOC is the location of the #pragma. */
10086 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
10090 block
= c_end_compound_stmt (loc
, block
, true);
10092 stmt
= make_node (OMP_TASK
);
10093 TREE_TYPE (stmt
) = void_type_node
;
10094 OMP_TASK_CLAUSES (stmt
) = clauses
;
10095 OMP_TASK_BODY (stmt
) = block
;
10096 SET_EXPR_LOCATION (stmt
, loc
);
10098 return add_stmt (stmt
);
10101 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10102 Remove any elements from the list that are invalid. */
10105 c_finish_omp_clauses (tree clauses
)
10107 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
10108 tree c
, t
, *pc
= &clauses
;
10111 bitmap_obstack_initialize (NULL
);
10112 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
10113 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
10114 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
10116 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
10118 bool remove
= false;
10119 bool need_complete
= false;
10120 bool need_implicitly_determined
= false;
10122 switch (OMP_CLAUSE_CODE (c
))
10124 case OMP_CLAUSE_SHARED
:
10126 need_implicitly_determined
= true;
10127 goto check_dup_generic
;
10129 case OMP_CLAUSE_PRIVATE
:
10131 need_complete
= true;
10132 need_implicitly_determined
= true;
10133 goto check_dup_generic
;
10135 case OMP_CLAUSE_REDUCTION
:
10136 name
= "reduction";
10137 need_implicitly_determined
= true;
10138 t
= OMP_CLAUSE_DECL (c
);
10139 if (AGGREGATE_TYPE_P (TREE_TYPE (t
))
10140 || POINTER_TYPE_P (TREE_TYPE (t
)))
10142 error_at (OMP_CLAUSE_LOCATION (c
),
10143 "%qE has invalid type for %<reduction%>", t
);
10146 else if (FLOAT_TYPE_P (TREE_TYPE (t
)))
10148 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
10149 const char *r_name
= NULL
;
10166 case TRUTH_ANDIF_EXPR
:
10169 case TRUTH_ORIF_EXPR
:
10173 gcc_unreachable ();
10177 error_at (OMP_CLAUSE_LOCATION (c
),
10178 "%qE has invalid type for %<reduction(%s)%>",
10183 goto check_dup_generic
;
10185 case OMP_CLAUSE_COPYPRIVATE
:
10186 name
= "copyprivate";
10187 goto check_dup_generic
;
10189 case OMP_CLAUSE_COPYIN
:
10191 t
= OMP_CLAUSE_DECL (c
);
10192 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (t
))
10194 error_at (OMP_CLAUSE_LOCATION (c
),
10195 "%qE must be %<threadprivate%> for %<copyin%>", t
);
10198 goto check_dup_generic
;
10201 t
= OMP_CLAUSE_DECL (c
);
10202 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
10204 error_at (OMP_CLAUSE_LOCATION (c
),
10205 "%qE is not a variable in clause %qs", t
, name
);
10208 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
10209 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
10210 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
10212 error_at (OMP_CLAUSE_LOCATION (c
),
10213 "%qE appears more than once in data clauses", t
);
10217 bitmap_set_bit (&generic_head
, DECL_UID (t
));
10220 case OMP_CLAUSE_FIRSTPRIVATE
:
10221 name
= "firstprivate";
10222 t
= OMP_CLAUSE_DECL (c
);
10223 need_complete
= true;
10224 need_implicitly_determined
= true;
10225 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
10227 error_at (OMP_CLAUSE_LOCATION (c
),
10228 "%qE is not a variable in clause %<firstprivate%>", t
);
10231 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
10232 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
10234 error_at (OMP_CLAUSE_LOCATION (c
),
10235 "%qE appears more than once in data clauses", t
);
10239 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
10242 case OMP_CLAUSE_LASTPRIVATE
:
10243 name
= "lastprivate";
10244 t
= OMP_CLAUSE_DECL (c
);
10245 need_complete
= true;
10246 need_implicitly_determined
= true;
10247 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
10249 error_at (OMP_CLAUSE_LOCATION (c
),
10250 "%qE is not a variable in clause %<lastprivate%>", t
);
10253 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
10254 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
10256 error_at (OMP_CLAUSE_LOCATION (c
),
10257 "%qE appears more than once in data clauses", t
);
10261 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
10264 case OMP_CLAUSE_IF
:
10265 case OMP_CLAUSE_NUM_THREADS
:
10266 case OMP_CLAUSE_SCHEDULE
:
10267 case OMP_CLAUSE_NOWAIT
:
10268 case OMP_CLAUSE_ORDERED
:
10269 case OMP_CLAUSE_DEFAULT
:
10270 case OMP_CLAUSE_UNTIED
:
10271 case OMP_CLAUSE_COLLAPSE
:
10272 pc
= &OMP_CLAUSE_CHAIN (c
);
10276 gcc_unreachable ();
10281 t
= OMP_CLAUSE_DECL (c
);
10285 t
= require_complete_type (t
);
10286 if (t
== error_mark_node
)
10290 if (need_implicitly_determined
)
10292 const char *share_name
= NULL
;
10294 if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
10295 share_name
= "threadprivate";
10296 else switch (c_omp_predetermined_sharing (t
))
10298 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
10300 case OMP_CLAUSE_DEFAULT_SHARED
:
10301 share_name
= "shared";
10303 case OMP_CLAUSE_DEFAULT_PRIVATE
:
10304 share_name
= "private";
10307 gcc_unreachable ();
10311 error_at (OMP_CLAUSE_LOCATION (c
),
10312 "%qE is predetermined %qs for %qs",
10313 t
, share_name
, name
);
10320 *pc
= OMP_CLAUSE_CHAIN (c
);
10322 pc
= &OMP_CLAUSE_CHAIN (c
);
10325 bitmap_obstack_release (NULL
);
10329 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10330 down to the element type of an array. */
10333 c_build_qualified_type (tree type
, int type_quals
)
10335 if (type
== error_mark_node
)
10338 if (TREE_CODE (type
) == ARRAY_TYPE
)
10341 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
10344 /* See if we already have an identically qualified type. */
10345 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
10347 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
10348 && TYPE_NAME (t
) == TYPE_NAME (type
)
10349 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
10350 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
10351 TYPE_ATTRIBUTES (type
)))
10356 tree domain
= TYPE_DOMAIN (type
);
10358 t
= build_variant_type_copy (type
);
10359 TREE_TYPE (t
) = element_type
;
10361 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
10362 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
10363 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10364 else if (TYPE_CANONICAL (element_type
) != element_type
10365 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
10367 tree unqualified_canon
10368 = build_array_type (TYPE_CANONICAL (element_type
),
10369 domain
? TYPE_CANONICAL (domain
)
10372 = c_build_qualified_type (unqualified_canon
, type_quals
);
10375 TYPE_CANONICAL (t
) = t
;
10380 /* A restrict-qualified pointer type must be a pointer to object or
10381 incomplete type. Note that the use of POINTER_TYPE_P also allows
10382 REFERENCE_TYPEs, which is appropriate for C++. */
10383 if ((type_quals
& TYPE_QUAL_RESTRICT
)
10384 && (!POINTER_TYPE_P (type
)
10385 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
10387 error ("invalid use of %<restrict%>");
10388 type_quals
&= ~TYPE_QUAL_RESTRICT
;
10391 return build_qualified_type (type
, type_quals
);
10394 /* Build a VA_ARG_EXPR for the C parser. */
10397 c_build_va_arg (location_t loc
, tree expr
, tree type
)
10399 if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
10400 warning_at (loc
, OPT_Wc___compat
,
10401 "C++ requires promoted type, not enum type, in %<va_arg%>");
10402 return build_va_arg (loc
, expr
, type
);